From 08c99da4faa218abebd5aa55d520cd46bd9e734a Mon Sep 17 00:00:00 2001 From: Aryan Salmanpour Date: Tue, 5 Dec 2023 13:06:34 -0500 Subject: [PATCH] Add nullptr parameters check for all decoder/parser APIs and some code clean up (#120) [ROCm/rocdecode commit: 9257cd6fcf425bcb1a37a946352400db09960dd9] --- projects/rocdecode/api/rocparser.h | 12 +++--- projects/rocdecode/src/parser/parser_handle.h | 34 ++++++++-------- .../rocdecode/src/parser/rocparser_api.cpp | 39 ++++++++++--------- .../rocdecode/src/rocdecode/rocdecode_api.cpp | 38 ++++++++++++------ 4 files changed, 70 insertions(+), 53 deletions(-) diff --git a/projects/rocdecode/api/rocparser.h b/projects/rocdecode/api/rocparser.h index a3ffda0027..1228c10f44 100644 --- a/projects/rocdecode/api/rocparser.h +++ b/projects/rocdecode/api/rocparser.h @@ -271,14 +271,14 @@ typedef struct _RocdecParserParams { /************************************************************************************************/ //! \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 /************************************************************************************************/ -extern rocDecStatus ROCDECAPI rocDecCreateVideoParser(RocdecVideoParser *pHandle, RocdecParserParams *pParams); +extern rocDecStatus ROCDECAPI rocDecCreateVideoParser(RocdecVideoParser *parser_handle, RocdecParserParams *params); /************************************************************************************************/ //! \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 //! Extracts parameter sets like SPS, PPS, bitstream etc. from pPacket and //! 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 //! 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 -//! \fn rocDecStatus ROCDECAPI rocDecDestroyVideoParser(RocdecVideoParser handle) +//! \fn rocDecStatus ROCDECAPI rocDecDestroyVideoParser(RocdecVideoParser parser_handle) //! Destroy the video parser object /************************************************************************************************/ -extern rocDecStatus ROCDECAPI rocDecDestroyVideoParser(RocdecVideoParser handle); +extern rocDecStatus ROCDECAPI rocDecDestroyVideoParser(RocdecVideoParser parser_handle); #if defined(__cplusplus) } diff --git a/projects/rocdecode/src/parser/parser_handle.h b/projects/rocdecode/src/parser/parser_handle.h index 4bcf3ba7e7..953704e3b7 100644 --- a/projects/rocdecode/src/parser/parser_handle.h +++ b/projects/rocdecode/src/parser/parser_handle.h @@ -29,20 +29,20 @@ THE SOFTWARE. #include "hevc_parser.h" class RocParserHandle { -public: - explicit RocParserHandle(RocdecParserParams *pParams) { create_parser(pParams); }; // default constructor - ~RocParserHandle() { clear_errors(); } - bool no_error() { return error.empty(); } - const char* error_msg() { return error.c_str(); } - void capture_error(const std::string& err_msg) { error = err_msg; } - rocDecStatus ParseVideoData(RocdecSourceDataPacket *pPacket) { return roc_parser_->ParseVideoData(pPacket); } - rocDecStatus DestroyParser() { return destroy_parser(); }; +public: + explicit RocParserHandle(RocdecParserParams *params) { CreateParser(params); }; + ~RocParserHandle() { ClearErrors(); } + bool NoError() { return error_.empty(); } + const char* ErrorMsg() { return error_.c_str(); } + void CaptureError(const std::string& err_msg) { error_ = err_msg; } + rocDecStatus ParseVideoData(RocdecSourceDataPacket *packet) { return roc_parser_->ParseVideoData(packet); } + rocDecStatus DestroyParser() { return DestroyParserInternal(); }; private: - std::shared_ptr roc_parser_ = nullptr; // class instantiation - void clear_errors() { error = ""; } - void create_parser(RocdecParserParams *pParams) { - switch(pParams->CodecType) { + std::shared_ptr roc_parser_ = nullptr; + void ClearErrors() { error_ = ""; } + void CreateParser(RocdecParserParams *params) { + switch(params->CodecType) { case rocDecVideoCodec_H264: roc_parser_ = std::make_shared(); break; @@ -50,19 +50,19 @@ private: roc_parser_ = std::make_shared(); break; default: - THROW("Unsupported parser type "+ TOSTR(pParams->CodecType)); + THROW("Unsupported parser type "+ TOSTR(params->CodecType)); break; } if (roc_parser_ ) { - rocDecStatus ret = roc_parser_->Initialize(pParams); + rocDecStatus ret = roc_parser_->Initialize(params); if (ret != ROCDEC_SUCCESS) THROW("rocParser Initialization failed with error: "+ TOSTR(ret)); } } - rocDecStatus destroy_parser() { + rocDecStatus DestroyParserInternal() { rocDecStatus ret = ROCDEC_NOT_INITIALIZED; - if (roc_parser_ ) { + if (roc_parser_) { ret = roc_parser_->UnInitialize(); if (ret != ROCDEC_SUCCESS) THROW("rocParser UnInitialization failed with error: "+ TOSTR(ret)); @@ -70,5 +70,5 @@ private: return ret; } - std::string error; + std::string error_; }; \ No newline at end of file diff --git a/projects/rocdecode/src/parser/rocparser_api.cpp b/projects/rocdecode/src/parser/rocparser_api.cpp index 010381c04c..e07436844d 100644 --- a/projects/rocdecode/src/parser/rocparser_api.cpp +++ b/projects/rocdecode/src/parser/rocparser_api.cpp @@ -25,45 +25,48 @@ THE SOFTWARE. /************************************************************************************************/ //! \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 /************************************************************************************************/ 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; try { - handle = new RocParserHandle(pParams); + handle = new RocParserHandle(parser_params); } catch(const std::exception& e) { ERR( STR("Failed to init the rocDecode handle, ") + STR(e.what())) return ROCDEC_RUNTIME_ERROR; } - *pHandle = handle; + *parser_handle = handle; return rocDecStatus::ROCDEC_SUCCESS; } /************************************************************************************************/ //! \ingroup FUNCTS -//! \fn rocParserStatus ROCDECAPI rocDecParseVideoData(RocdecVideoParser handle, RocdecSourceDataPacket *pPacket) -//! Parse the video data from source data packet in pPacket -//! Extracts parameter sets like SPS, PPS, bitstream etc. from pPacket and +//! \fn rocParserStatus ROCDECAPI rocDecParseVideoData(RocdecVideoParser parser_handle, RocdecSourceDataPacket *packet) +//! Parse the video data from source data packet in packet +//! Extracts parameter sets like SPS, PPS, bitstream etc. from packet and //! calls back pfnDecodePicture with RocdecPicParams data for kicking of HW decoding //! calls back pfnSequenceCallback with RocdecVideoFormat data for initial sequence header or when //! the decoder encounters a video format change //! calls back pfnDisplayPicture with ROCDECPARSERDISPINFO data to display a video frame /************************************************************************************************/ rocDecStatus ROCDECAPI -rocDecParseVideoData(RocdecVideoParser handle, RocdecSourceDataPacket *pPacket) { - if (handle == nullptr) { +rocDecParseVideoData(RocdecVideoParser parser_handle, RocdecSourceDataPacket *packet) { + if (parser_handle == nullptr || packet == nullptr) { return ROCDEC_INVALID_PARAMETER; } - auto parser_hdl = static_cast (handle); + auto roc_parser_handle = static_cast(parser_handle); rocDecStatus ret; try { - ret = parser_hdl->ParseVideoData(pPacket); + ret = roc_parser_handle->ParseVideoData(packet); } catch(const std::exception& e) { - parser_hdl->capture_error(e.what()); + roc_parser_handle->CaptureError(e.what()); ERR(e.what()) return ROCDEC_RUNTIME_ERROR; } @@ -72,21 +75,21 @@ rocDecParseVideoData(RocdecVideoParser handle, RocdecSourceDataPacket *pPacket) /************************************************************************************************/ //! \ingroup FUNCTS -//! \fn rocDecStatus ROCDECAPI rocDecDestroyVideoParser(RocdecVideoParser handle) +//! \fn rocDecStatus ROCDECAPI rocDecDestroyVideoParser(RocdecVideoParser parser_handle) //! Destroy the video parser object /************************************************************************************************/ extern rocDecStatus ROCDECAPI -rocDecDestroyVideoParser(RocdecVideoParser handle) { - if (handle == nullptr) { +rocDecDestroyVideoParser(RocdecVideoParser parser_handle) { + if (parser_handle == nullptr) { return ROCDEC_INVALID_PARAMETER; } - auto parser_hdl = static_cast (handle); + auto roc_parser_handle = static_cast(parser_handle); rocDecStatus ret; try { - ret = parser_hdl->DestroyParser(); + ret = roc_parser_handle->DestroyParser(); } catch(const std::exception& e) { - parser_hdl->capture_error(e.what()); + roc_parser_handle->CaptureError(e.what()); ERR(e.what()) return ROCDEC_RUNTIME_ERROR; } diff --git a/projects/rocdecode/src/rocdecode/rocdecode_api.cpp b/projects/rocdecode/src/rocdecode/rocdecode_api.cpp index f6939e7c57..335db0b18b 100644 --- a/projects/rocdecode/src/rocdecode/rocdecode_api.cpp +++ b/projects/rocdecode/src/rocdecode/rocdecode_api.cpp @@ -31,6 +31,9 @@ THE SOFTWARE. /*****************************************************************************************************/ rocDecStatus ROCDECAPI rocDecCreateDecoder(rocDecDecoderHandle *decoder_handle, RocDecoderCreateInfo *decoder_create_info) { + if (decoder_handle == nullptr || decoder_create_info == nullptr) { + return ROCDEC_INVALID_PARAMETER; + } rocDecDecoderHandle handle = nullptr; try { handle = new DecHandle(*decoder_create_info); @@ -49,7 +52,10 @@ rocDecCreateDecoder(rocDecDecoderHandle *decoder_handle, RocDecoderCreateInfo *d /*****************************************************************************************************/ rocDecStatus ROCDECAPI rocDecDestroyDecoder(rocDecDecoderHandle decoder_handle) { - auto handle = static_cast (decoder_handle); + if (decoder_handle == nullptr) { + return ROCDEC_INVALID_PARAMETER; + } + auto handle = static_cast(decoder_handle); delete handle; return ROCDEC_SUCCESS; } @@ -99,7 +105,10 @@ rocDecGetDecoderCaps(RocdecDecodeCaps *pdc) { /*****************************************************************************************************/ rocDecStatus ROCDECAPI rocDecDecodeFrame(rocDecDecoderHandle decoder_handle, RocdecPicParams *pic_params) { - auto handle = static_cast (decoder_handle); + if (decoder_handle == nullptr || pic_params == nullptr) { + return ROCDEC_INVALID_PARAMETER; + } + auto handle = static_cast(decoder_handle); rocDecStatus ret; try { 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); //! 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. /************************************************************************************************************/ rocDecStatus ROCDECAPI @@ -123,7 +132,7 @@ rocDecGetDecodeStatus(rocDecDecoderHandle decoder_handle, int pic_idx, RocdecDec if (decoder_handle == nullptr || decode_status == nullptr) { return ROCDEC_INVALID_PARAMETER; } - auto handle = static_cast (decoder_handle); + auto handle = static_cast(decoder_handle); rocDecStatus ret; try { ret = handle->roc_decoder_->GetDecodeStatus(pic_idx, decode_status); @@ -138,15 +147,15 @@ rocDecGetDecodeStatus(rocDecDecoderHandle decoder_handle, int pic_idx, RocdecDec /*********************************************************************************************************/ //! \fn rocDecStatus ROCDECAPI rocDecReconfigureDecoder(rocDecDecoderHandle decoder_handle, RocdecReconfigureDecoderInfo *reconfig_params) -//! Used to reuse single decoder for multiple clips. Currently supports resolution change, resize params -//! params, target area params change for same codec. Must be called during RocdecParserParams::pfnSequenceCallback +//! Used to reuse single decoder for multiple clips. Currently supports resolution change, resize params +//! params, target area params change for same codec. Must be called during RocdecParserParams::pfnSequenceCallback /*********************************************************************************************************/ rocDecStatus ROCDECAPI rocDecReconfigureDecoder(rocDecDecoderHandle decoder_handle, RocdecReconfigureDecoderInfo *reconfig_params) { if (decoder_handle == nullptr || reconfig_params == nullptr) { return ROCDEC_INVALID_PARAMETER; } - auto handle = static_cast (decoder_handle); + auto handle = static_cast(decoder_handle); rocDecStatus ret; try { 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, -//! unsigned int *dev_mem_ptr, unsigned int *horizontal_pitch, -//! RocdecProcParams *vid_postproc_params); +//! \fn rocDecStatus ROCDECAPI rocDecMapVideoFrame(rocDecDecoderHandle decoder_handle, int pic_idx, unsigned int *dev_mem_ptr, +//! unsigned int *horizontal_pitch, RocdecProcParams *vid_postproc_params); //! 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 /************************************************************************************************************************/ rocDecStatus ROCDECAPI rocDecMapVideoFrame(rocDecDecoderHandle decoder_handle, int pic_idx, void *dev_mem_ptr[3], uint32_t (&horizontal_pitch)[3], RocdecProcParams *vid_postproc_params) { - auto handle = static_cast (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(decoder_handle); rocDecStatus ret; try { 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 rocDecUnMapVideoFrame(rocDecDecoderHandle decoder_handle, int pic_idx) { - auto handle = static_cast (decoder_handle); + if (decoder_handle == nullptr) { + return ROCDEC_INVALID_PARAMETER; + } + auto handle = static_cast(decoder_handle); rocDecStatus ret; try { ret = handle->roc_decoder_->UnMapVideoFrame(pic_idx);