Add nullptr parameters check for all decoder/parser APIs and some code clean up (#120)

[ROCm/rocdecode commit: 9257cd6fcf]
This commit is contained in:
Aryan Salmanpour
2023-12-05 13:06:34 -05:00
committed by GitHub
orang tua 24dc12e75e
melakukan 08c99da4fa
4 mengubah file dengan 70 tambahan dan 53 penghapusan
+6 -6
Melihat File
@@ -271,14 +271,14 @@ typedef struct _RocdecParserParams {
/************************************************************************************************/ /************************************************************************************************/
//! \ingroup FUNCTS //! \ingroup FUNCTS
//! \fn rocDecodeStatus ROCDECAPI rocDecCreateVideoParser(RocdecVideoParser *pHandle, RocdecParserParams *pParams) //! \fn rocDecodeStatus ROCDECAPI rocDecCreateVideoParser(RocdecVideoParser *parser_handle, RocdecParserParams *params)
//! Create video parser object and initialize //! Create video parser object and initialize
/************************************************************************************************/ /************************************************************************************************/
extern rocDecStatus ROCDECAPI rocDecCreateVideoParser(RocdecVideoParser *pHandle, RocdecParserParams *pParams); extern rocDecStatus ROCDECAPI rocDecCreateVideoParser(RocdecVideoParser *parser_handle, RocdecParserParams *params);
/************************************************************************************************/ /************************************************************************************************/
//! \ingroup FUNCTS //! \ingroup FUNCTS
//! \fn rocDecodeStatus ROCDECAPI rocDecParseVideoData(RocdecVideoParser handle, RocdecSourceDataPacket *pPacket) //! \fn rocDecodeStatus ROCDECAPI rocDecParseVideoData(RocdecVideoParser parser_handle, RocdecSourceDataPacket *packet)
//! Parse the video data from source data packet in pPacket //! Parse the video data from source data packet in pPacket
//! Extracts parameter sets like SPS, PPS, bitstream etc. from pPacket and //! Extracts parameter sets like SPS, PPS, bitstream etc. from pPacket and
//! calls back pfnDecodePicture with RocdecPicParams data for kicking of HW decoding //! calls back pfnDecodePicture with RocdecPicParams data for kicking of HW decoding
@@ -286,14 +286,14 @@ extern rocDecStatus ROCDECAPI rocDecCreateVideoParser(RocdecVideoParser *pHandle
//! the decoder encounters a video format change //! the decoder encounters a video format change
//! calls back pfnDisplayPicture with RocdecParserDispInfo data to display a video frame //! calls back pfnDisplayPicture with RocdecParserDispInfo data to display a video frame
/************************************************************************************************/ /************************************************************************************************/
extern rocDecStatus ROCDECAPI rocDecParseVideoData(RocdecVideoParser handle, RocdecSourceDataPacket *pPacket); extern rocDecStatus ROCDECAPI rocDecParseVideoData(RocdecVideoParser parser_handle, RocdecSourceDataPacket *packet);
/************************************************************************************************/ /************************************************************************************************/
//! \ingroup FUNCTS //! \ingroup FUNCTS
//! \fn rocDecStatus ROCDECAPI rocDecDestroyVideoParser(RocdecVideoParser handle) //! \fn rocDecStatus ROCDECAPI rocDecDestroyVideoParser(RocdecVideoParser parser_handle)
//! Destroy the video parser object //! Destroy the video parser object
/************************************************************************************************/ /************************************************************************************************/
extern rocDecStatus ROCDECAPI rocDecDestroyVideoParser(RocdecVideoParser handle); extern rocDecStatus ROCDECAPI rocDecDestroyVideoParser(RocdecVideoParser parser_handle);
#if defined(__cplusplus) #if defined(__cplusplus)
} }
@@ -30,19 +30,19 @@ THE SOFTWARE.
class RocParserHandle { class RocParserHandle {
public: public:
explicit RocParserHandle(RocdecParserParams *pParams) { create_parser(pParams); }; // default constructor explicit RocParserHandle(RocdecParserParams *params) { CreateParser(params); };
~RocParserHandle() { clear_errors(); } ~RocParserHandle() { ClearErrors(); }
bool no_error() { return error.empty(); } bool NoError() { return error_.empty(); }
const char* error_msg() { return error.c_str(); } const char* ErrorMsg() { return error_.c_str(); }
void capture_error(const std::string& err_msg) { error = err_msg; } void CaptureError(const std::string& err_msg) { error_ = err_msg; }
rocDecStatus ParseVideoData(RocdecSourceDataPacket *pPacket) { return roc_parser_->ParseVideoData(pPacket); } rocDecStatus ParseVideoData(RocdecSourceDataPacket *packet) { return roc_parser_->ParseVideoData(packet); }
rocDecStatus DestroyParser() { return destroy_parser(); }; rocDecStatus DestroyParser() { return DestroyParserInternal(); };
private: private:
std::shared_ptr<RocVideoParser> roc_parser_ = nullptr; // class instantiation std::shared_ptr<RocVideoParser> roc_parser_ = nullptr;
void clear_errors() { error = ""; } void ClearErrors() { error_ = ""; }
void create_parser(RocdecParserParams *pParams) { void CreateParser(RocdecParserParams *params) {
switch(pParams->CodecType) { switch(params->CodecType) {
case rocDecVideoCodec_H264: case rocDecVideoCodec_H264:
roc_parser_ = std::make_shared<H264VideoParser>(); roc_parser_ = std::make_shared<H264VideoParser>();
break; break;
@@ -50,19 +50,19 @@ private:
roc_parser_ = std::make_shared<HEVCVideoParser>(); roc_parser_ = std::make_shared<HEVCVideoParser>();
break; break;
default: default:
THROW("Unsupported parser type "+ TOSTR(pParams->CodecType)); THROW("Unsupported parser type "+ TOSTR(params->CodecType));
break; break;
} }
if (roc_parser_ ) { if (roc_parser_ ) {
rocDecStatus ret = roc_parser_->Initialize(pParams); rocDecStatus ret = roc_parser_->Initialize(params);
if (ret != ROCDEC_SUCCESS) if (ret != ROCDEC_SUCCESS)
THROW("rocParser Initialization failed with error: "+ TOSTR(ret)); THROW("rocParser Initialization failed with error: "+ TOSTR(ret));
} }
} }
rocDecStatus destroy_parser() { rocDecStatus DestroyParserInternal() {
rocDecStatus ret = ROCDEC_NOT_INITIALIZED; rocDecStatus ret = ROCDEC_NOT_INITIALIZED;
if (roc_parser_ ) { if (roc_parser_) {
ret = roc_parser_->UnInitialize(); ret = roc_parser_->UnInitialize();
if (ret != ROCDEC_SUCCESS) if (ret != ROCDEC_SUCCESS)
THROW("rocParser UnInitialization failed with error: "+ TOSTR(ret)); THROW("rocParser UnInitialization failed with error: "+ TOSTR(ret));
@@ -70,5 +70,5 @@ private:
return ret; return ret;
} }
std::string error; std::string error_;
}; };
@@ -25,45 +25,48 @@ THE SOFTWARE.
/************************************************************************************************/ /************************************************************************************************/
//! \ingroup FUNCTS //! \ingroup FUNCTS
//! \fn rocParserStatus ROCDECAPI rocDecCreateVideoParser(RocdecVideoParser *pHandle, RocdecParserParams *pParams) //! \fn rocParserStatus ROCDECAPI rocDecCreateVideoParser(RocdecVideoParser *parser_handle, RocdecParserParams *parser_params)
//! Create video parser object and initialize //! Create video parser object and initialize
/************************************************************************************************/ /************************************************************************************************/
rocDecStatus ROCDECAPI rocDecStatus ROCDECAPI
rocDecCreateVideoParser(RocdecVideoParser *pHandle, RocdecParserParams *pParams) { rocDecCreateVideoParser(RocdecVideoParser *parser_handle, RocdecParserParams *parser_params) {
if (parser_handle == nullptr || parser_params == nullptr) {
return ROCDEC_INVALID_PARAMETER;
}
RocdecVideoParser handle = nullptr; RocdecVideoParser handle = nullptr;
try { try {
handle = new RocParserHandle(pParams); handle = new RocParserHandle(parser_params);
} }
catch(const std::exception& e) { catch(const std::exception& e) {
ERR( STR("Failed to init the rocDecode handle, ") + STR(e.what())) ERR( STR("Failed to init the rocDecode handle, ") + STR(e.what()))
return ROCDEC_RUNTIME_ERROR; return ROCDEC_RUNTIME_ERROR;
} }
*pHandle = handle; *parser_handle = handle;
return rocDecStatus::ROCDEC_SUCCESS; return rocDecStatus::ROCDEC_SUCCESS;
} }
/************************************************************************************************/ /************************************************************************************************/
//! \ingroup FUNCTS //! \ingroup FUNCTS
//! \fn rocParserStatus ROCDECAPI rocDecParseVideoData(RocdecVideoParser handle, RocdecSourceDataPacket *pPacket) //! \fn rocParserStatus ROCDECAPI rocDecParseVideoData(RocdecVideoParser parser_handle, RocdecSourceDataPacket *packet)
//! Parse the video data from source data packet in pPacket //! Parse the video data from source data packet in packet
//! Extracts parameter sets like SPS, PPS, bitstream etc. from pPacket and //! Extracts parameter sets like SPS, PPS, bitstream etc. from packet and
//! calls back pfnDecodePicture with RocdecPicParams data for kicking of HW decoding //! calls back pfnDecodePicture with RocdecPicParams data for kicking of HW decoding
//! calls back pfnSequenceCallback with RocdecVideoFormat data for initial sequence header or when //! calls back pfnSequenceCallback with RocdecVideoFormat data for initial sequence header or when
//! the decoder encounters a video format change //! the decoder encounters a video format change
//! calls back pfnDisplayPicture with ROCDECPARSERDISPINFO data to display a video frame //! calls back pfnDisplayPicture with ROCDECPARSERDISPINFO data to display a video frame
/************************************************************************************************/ /************************************************************************************************/
rocDecStatus ROCDECAPI rocDecStatus ROCDECAPI
rocDecParseVideoData(RocdecVideoParser handle, RocdecSourceDataPacket *pPacket) { rocDecParseVideoData(RocdecVideoParser parser_handle, RocdecSourceDataPacket *packet) {
if (handle == nullptr) { if (parser_handle == nullptr || packet == nullptr) {
return ROCDEC_INVALID_PARAMETER; return ROCDEC_INVALID_PARAMETER;
} }
auto parser_hdl = static_cast<RocParserHandle *> (handle); auto roc_parser_handle = static_cast<RocParserHandle *>(parser_handle);
rocDecStatus ret; rocDecStatus ret;
try { try {
ret = parser_hdl->ParseVideoData(pPacket); ret = roc_parser_handle->ParseVideoData(packet);
} }
catch(const std::exception& e) { catch(const std::exception& e) {
parser_hdl->capture_error(e.what()); roc_parser_handle->CaptureError(e.what());
ERR(e.what()) ERR(e.what())
return ROCDEC_RUNTIME_ERROR; return ROCDEC_RUNTIME_ERROR;
} }
@@ -72,21 +75,21 @@ rocDecParseVideoData(RocdecVideoParser handle, RocdecSourceDataPacket *pPacket)
/************************************************************************************************/ /************************************************************************************************/
//! \ingroup FUNCTS //! \ingroup FUNCTS
//! \fn rocDecStatus ROCDECAPI rocDecDestroyVideoParser(RocdecVideoParser handle) //! \fn rocDecStatus ROCDECAPI rocDecDestroyVideoParser(RocdecVideoParser parser_handle)
//! Destroy the video parser object //! Destroy the video parser object
/************************************************************************************************/ /************************************************************************************************/
extern rocDecStatus ROCDECAPI extern rocDecStatus ROCDECAPI
rocDecDestroyVideoParser(RocdecVideoParser handle) { rocDecDestroyVideoParser(RocdecVideoParser parser_handle) {
if (handle == nullptr) { if (parser_handle == nullptr) {
return ROCDEC_INVALID_PARAMETER; return ROCDEC_INVALID_PARAMETER;
} }
auto parser_hdl = static_cast<RocParserHandle *> (handle); auto roc_parser_handle = static_cast<RocParserHandle *>(parser_handle);
rocDecStatus ret; rocDecStatus ret;
try { try {
ret = parser_hdl->DestroyParser(); ret = roc_parser_handle->DestroyParser();
} }
catch(const std::exception& e) { catch(const std::exception& e) {
parser_hdl->capture_error(e.what()); roc_parser_handle->CaptureError(e.what());
ERR(e.what()) ERR(e.what())
return ROCDEC_RUNTIME_ERROR; return ROCDEC_RUNTIME_ERROR;
} }
@@ -31,6 +31,9 @@ THE SOFTWARE.
/*****************************************************************************************************/ /*****************************************************************************************************/
rocDecStatus ROCDECAPI rocDecStatus ROCDECAPI
rocDecCreateDecoder(rocDecDecoderHandle *decoder_handle, RocDecoderCreateInfo *decoder_create_info) { rocDecCreateDecoder(rocDecDecoderHandle *decoder_handle, RocDecoderCreateInfo *decoder_create_info) {
if (decoder_handle == nullptr || decoder_create_info == nullptr) {
return ROCDEC_INVALID_PARAMETER;
}
rocDecDecoderHandle handle = nullptr; rocDecDecoderHandle handle = nullptr;
try { try {
handle = new DecHandle(*decoder_create_info); handle = new DecHandle(*decoder_create_info);
@@ -49,7 +52,10 @@ rocDecCreateDecoder(rocDecDecoderHandle *decoder_handle, RocDecoderCreateInfo *d
/*****************************************************************************************************/ /*****************************************************************************************************/
rocDecStatus ROCDECAPI rocDecStatus ROCDECAPI
rocDecDestroyDecoder(rocDecDecoderHandle decoder_handle) { rocDecDestroyDecoder(rocDecDecoderHandle decoder_handle) {
auto handle = static_cast<DecHandle *> (decoder_handle); if (decoder_handle == nullptr) {
return ROCDEC_INVALID_PARAMETER;
}
auto handle = static_cast<DecHandle *>(decoder_handle);
delete handle; delete handle;
return ROCDEC_SUCCESS; return ROCDEC_SUCCESS;
} }
@@ -99,7 +105,10 @@ rocDecGetDecoderCaps(RocdecDecodeCaps *pdc) {
/*****************************************************************************************************/ /*****************************************************************************************************/
rocDecStatus ROCDECAPI rocDecStatus ROCDECAPI
rocDecDecodeFrame(rocDecDecoderHandle decoder_handle, RocdecPicParams *pic_params) { rocDecDecodeFrame(rocDecDecoderHandle decoder_handle, RocdecPicParams *pic_params) {
auto handle = static_cast<DecHandle *> (decoder_handle); if (decoder_handle == nullptr || pic_params == nullptr) {
return ROCDEC_INVALID_PARAMETER;
}
auto handle = static_cast<DecHandle *>(decoder_handle);
rocDecStatus ret; rocDecStatus ret;
try { try {
ret = handle->roc_decoder_->DecodeFrame(pic_params); ret = handle->roc_decoder_->DecodeFrame(pic_params);
@@ -115,7 +124,7 @@ rocDecDecodeFrame(rocDecDecoderHandle decoder_handle, RocdecPicParams *pic_param
/************************************************************************************************************/ /************************************************************************************************************/
//! \fn rocDecStatus ROCDECAPI RocdecGetDecodeStatus(rocDecDecoderHandle decoder_handle, int pic_idx, RocdecDecodeStatus* decode_status); //! \fn rocDecStatus ROCDECAPI RocdecGetDecodeStatus(rocDecDecoderHandle decoder_handle, int pic_idx, RocdecDecodeStatus* decode_status);
//! Get the decode status for frame corresponding to pic_idx //! Get the decode status for frame corresponding to pic_idx
//! API is currently supported for HEVC, H264 and JPEG codecs. //! API is currently supported for HEVC codec.
//! API returns CUDA_ERROR_NOT_SUPPORTED error code for unsupported GPU or codec. //! API returns CUDA_ERROR_NOT_SUPPORTED error code for unsupported GPU or codec.
/************************************************************************************************************/ /************************************************************************************************************/
rocDecStatus ROCDECAPI rocDecStatus ROCDECAPI
@@ -123,7 +132,7 @@ rocDecGetDecodeStatus(rocDecDecoderHandle decoder_handle, int pic_idx, RocdecDec
if (decoder_handle == nullptr || decode_status == nullptr) { if (decoder_handle == nullptr || decode_status == nullptr) {
return ROCDEC_INVALID_PARAMETER; return ROCDEC_INVALID_PARAMETER;
} }
auto handle = static_cast<DecHandle *> (decoder_handle); auto handle = static_cast<DecHandle *>(decoder_handle);
rocDecStatus ret; rocDecStatus ret;
try { try {
ret = handle->roc_decoder_->GetDecodeStatus(pic_idx, decode_status); ret = handle->roc_decoder_->GetDecodeStatus(pic_idx, decode_status);
@@ -146,7 +155,7 @@ rocDecReconfigureDecoder(rocDecDecoderHandle decoder_handle, RocdecReconfigureDe
if (decoder_handle == nullptr || reconfig_params == nullptr) { if (decoder_handle == nullptr || reconfig_params == nullptr) {
return ROCDEC_INVALID_PARAMETER; return ROCDEC_INVALID_PARAMETER;
} }
auto handle = static_cast<DecHandle *> (decoder_handle); auto handle = static_cast<DecHandle *>(decoder_handle);
rocDecStatus ret; rocDecStatus ret;
try { try {
ret = handle->roc_decoder_->ReconfigureDecoder(reconfig_params); ret = handle->roc_decoder_->ReconfigureDecoder(reconfig_params);
@@ -160,16 +169,18 @@ rocDecReconfigureDecoder(rocDecDecoderHandle decoder_handle, RocdecReconfigureDe
} }
/************************************************************************************************************************/ /************************************************************************************************************************/
//! \fn extern rocDecStatus ROCDECAPI rocDecMapVideoFrame(rocDecDecoderHandle decoder_handle, int pic_idx, //! \fn rocDecStatus ROCDECAPI rocDecMapVideoFrame(rocDecDecoderHandle decoder_handle, int pic_idx, unsigned int *dev_mem_ptr,
//! unsigned int *dev_mem_ptr, unsigned int *horizontal_pitch, //! unsigned int *horizontal_pitch, RocdecProcParams *vid_postproc_params);
//! RocdecProcParams *vid_postproc_params);
//! Post-process and map video frame corresponding to pic_idx for use in HIP. Returns HIP device pointer and associated //! Post-process and map video frame corresponding to pic_idx for use in HIP. Returns HIP device pointer and associated
//! pitch(horizontal stride) of the video frame. Returns device memory pointers for each plane (Y, U and V) seperately //! pitch(horizontal stride) of the video frame. Returns device memory pointers for each plane (Y, U and V) seperately
/************************************************************************************************************************/ /************************************************************************************************************************/
rocDecStatus ROCDECAPI rocDecStatus ROCDECAPI
rocDecMapVideoFrame(rocDecDecoderHandle decoder_handle, int pic_idx, rocDecMapVideoFrame(rocDecDecoderHandle decoder_handle, int pic_idx,
void *dev_mem_ptr[3], uint32_t (&horizontal_pitch)[3], RocdecProcParams *vid_postproc_params) { void *dev_mem_ptr[3], uint32_t (&horizontal_pitch)[3], RocdecProcParams *vid_postproc_params) {
auto handle = static_cast<DecHandle *> (decoder_handle); if (decoder_handle == nullptr || dev_mem_ptr == nullptr || horizontal_pitch == nullptr || vid_postproc_params == nullptr) {
return ROCDEC_INVALID_PARAMETER;
}
auto handle = static_cast<DecHandle *>(decoder_handle);
rocDecStatus ret; rocDecStatus ret;
try { try {
ret = handle->roc_decoder_->MapVideoFrame(pic_idx, dev_mem_ptr, horizontal_pitch, vid_postproc_params); ret = handle->roc_decoder_->MapVideoFrame(pic_idx, dev_mem_ptr, horizontal_pitch, vid_postproc_params);
@@ -188,7 +199,10 @@ rocDecMapVideoFrame(rocDecDecoderHandle decoder_handle, int pic_idx,
/*****************************************************************************************************/ /*****************************************************************************************************/
rocDecStatus ROCDECAPI rocDecStatus ROCDECAPI
rocDecUnMapVideoFrame(rocDecDecoderHandle decoder_handle, int pic_idx) { rocDecUnMapVideoFrame(rocDecDecoderHandle decoder_handle, int pic_idx) {
auto handle = static_cast<DecHandle *> (decoder_handle); if (decoder_handle == nullptr) {
return ROCDEC_INVALID_PARAMETER;
}
auto handle = static_cast<DecHandle *>(decoder_handle);
rocDecStatus ret; rocDecStatus ret;
try { try {
ret = handle->roc_decoder_->UnMapVideoFrame(pic_idx); ret = handle->roc_decoder_->UnMapVideoFrame(pic_idx);