Add mechanism to flush last frames during reconfigure (#142)

* add mechanism to flush last frames during reconfigure

* reconfig mode updated support

* addressed review comments

* addressed review comments

* change condition according to review comment

[ROCm/rocdecode commit: a5211189f5]
This commit is contained in:
Rajy Rawther
2023-12-19 05:49:17 -08:00
committed by GitHub
parent f43b7554e1
commit d55324bcf9
7 changed files with 228 additions and 12 deletions
@@ -24,8 +24,8 @@ THE SOFTWARE.
RocVideoDecoder::RocVideoDecoder(int device_id, OutputSurfaceMemoryType out_mem_type, rocDecVideoCodec codec, bool force_zero_latency,
const Rect *p_crop_rect, bool extract_user_sei_Message, int max_width, int max_height, uint32_t clk_rate) :
device_id_{device_id}, out_mem_type_(out_mem_type), codec_id_(codec), b_force_zero_latency_(force_zero_latency), b_extract_sei_message_(extract_user_sei_Message),
max_width_ (max_width), max_height_(max_height) {
device_id_{device_id}, out_mem_type_(out_mem_type), codec_id_(codec), b_force_zero_latency_(force_zero_latency),
b_extract_sei_message_(extract_user_sei_Message), max_width_ (max_width), max_height_(max_height) {
if (!InitHIP(device_id_)) {
THROW("Failed to initilize the HIP");
@@ -417,6 +417,24 @@ int RocVideoDecoder::HandleVideoSequence(RocdecVideoFormat *p_video_format) {
return nDecodeSurface;
}
/**
* @brief Function to set the Reconfig Params object
*
* @param p_reconfig_params: pointer to reconfig params struct
* @return true : success
* @return false : fail
*/
bool RocVideoDecoder::SetReconfigParams(ReconfigParams *p_reconfig_params) {
if (!p_reconfig_params) {
std::cout << "ERROR: Invalid reconfig struct passed! "<< std::endl;
return false;
}
//save it
p_reconfig_params_ = p_reconfig_params;
return true;
}
/**
* @brief function to reconfigure decoder if there is a change in sequence params.
*
@@ -453,6 +471,31 @@ int RocVideoDecoder::ReconfigureDecoder(RocdecVideoFormat *p_video_format) {
}
return 1;
}
// flush and clear internal frame store to reconfigure
if (p_reconfig_params_ && p_reconfig_params_->p_fn_reconfigure_flush)
num_frames_flushed_during_reconfig_ = p_reconfig_params_->p_fn_reconfigure_flush(this, p_reconfig_params_->reconfig_flush_mode,
static_cast<void *>(p_reconfig_params_->p_reconfig_user_struct));
// clear the existing output buffers of different size
// note that app lose the remaining frames in the vp_frames/vp_frames_q in case application didn't set p_fn_reconfigure_flush_ callback
if (out_mem_type_ == OUT_SURFACE_MEM_DEV_INTERNAL) {
ReleaseInternalFrames();
} else {
std::lock_guard<std::mutex> lock(mtx_vp_frame_);
while(!vp_frames_.empty()) {
DecFrameBuffer *p_frame = &vp_frames_.back();
// pop decoded frame
vp_frames_.pop_back();
if (p_frame->frame_ptr) {
if (out_mem_type_ == OUT_SURFACE_MEM_DEV_COPIED) {
hipError_t hip_status = hipFree(p_frame->frame_ptr);
if (hip_status != hipSuccess) std::cout << "ERROR: hipFree failed! (" << hip_status << ")" << std::endl;
}
else
delete [] (p_frame->frame_ptr);
}
}
}
decoded_frame_cnt_ = 0; //reset frame_count
width_ = p_video_format->coded_width;
height_ = p_video_format->coded_height;
@@ -477,6 +520,9 @@ int RocVideoDecoder::ReconfigureDecoder(RocdecVideoFormat *p_video_format) {
} else {
surface_stride_ = reconfig_params.ulTargetWidth * byte_per_pixel_;
}
chroma_height_ = static_cast<int>(ceil(height_ * GetChromaHeightFactor(video_surface_format_)));
num_chroma_planes_ = GetChromaPlaneCount(video_surface_format_);
if (p_video_format->chroma_format == rocDecVideoChromaFormat_Monochrome) num_chroma_planes_ = 0;
chroma_vstride_ = static_cast<int>(std::ceil(surface_vstride_ * GetChromaHeightFactor(video_surface_format_)));
// fill output_surface_info_
output_surface_info_.output_width = width_;
@@ -504,6 +550,7 @@ int RocVideoDecoder::ReconfigureDecoder(RocdecVideoFormat *p_video_format) {
}
ROCDEC_API_CALL(rocDecReconfigureDecoder(roc_decoder_, &reconfig_params));
input_video_info_str_.str("");
input_video_info_str_.clear();
input_video_info_str_ << "Input Video Resolution Changed:" << std::endl
@@ -744,6 +791,8 @@ uint8_t* RocVideoDecoder::GetFrame(int64_t *pts) {
return nullptr;
}
/**
* @brief function to release frame after use by the application: Only used with "OUT_SURFACE_MEM_DEV_INTERNAL"
*
@@ -752,9 +801,19 @@ uint8_t* RocVideoDecoder::GetFrame(int64_t *pts) {
* @return false - falied
*/
bool RocVideoDecoder::ReleaseFrame(int64_t pTimestamp) {
if (out_mem_type_ != OUT_SURFACE_MEM_DEV_INTERNAL)
return true; // nothing to do
bool RocVideoDecoder::ReleaseFrame(int64_t pTimestamp, bool b_flushing) {
if (out_mem_type_ != OUT_SURFACE_MEM_DEV_INTERNAL) {
if (!b_flushing) // if not flushing the buffers are re-used, so keep them
return true; // nothing to do
else {
DecFrameBuffer *fb = &vp_frames_[0];
if (pTimestamp != fb->pts) {
std::cerr << "Decoded Frame is released out of order" << std::endl;
return false;
}
vp_frames_.erase(vp_frames_.begin()); // get rid of the frames from the framestore
}
}
// only needed when using internal mapped buffer
if (!vp_frames_q_.empty()) {
std::lock_guard<std::mutex> lock(mtx_vp_frame_);
@@ -772,6 +831,28 @@ bool RocVideoDecoder::ReleaseFrame(int64_t pTimestamp) {
return true;
}
/**
* @brief function to release all internal frames and clear the q (used with reconfigure): Only used with "OUT_SURFACE_MEM_DEV_INTERNAL"
*
* @return true - success
* @return false - falied
*/
bool RocVideoDecoder::ReleaseInternalFrames() {
if (out_mem_type_ != OUT_SURFACE_MEM_DEV_INTERNAL)
return true; // nothing to do
// only needed when using internal mapped buffer
while (!vp_frames_q_.empty()) {
std::lock_guard<std::mutex> lock(mtx_vp_frame_);
DecFrameBuffer *fb = &vp_frames_q_.front();
ROCDEC_API_CALL(rocDecUnMapVideoFrame(roc_decoder_, fb->picture_index));
// pop decoded frame
vp_frames_q_.pop();
}
return true;
}
void RocVideoDecoder::SaveFrameToFile(std::string output_file_name, void *surf_mem, OutputSurfaceInfo *surf_info) {
uint8_t *hst_ptr = nullptr;
uint64_t output_image_size = surf_info->output_surface_size_in_bytes;
@@ -42,6 +42,7 @@ extern "C" {
#include "rocparser.h"
#define MAX_FRAME_NUM 16
typedef int (ROCDECAPI *PFNRECONFIGUEFLUSHCALLBACK)(void *, uint32_t, void *);
typedef enum SeiH264HevcPayloadType_enum {
SEI_TYPE_TIME_CODE = 136,
@@ -134,6 +135,12 @@ typedef struct OutputSurfaceInfoType {
OutputSurfaceMemoryType mem_type; /**< Output mem_type of the surface*/
} OutputSurfaceInfo;
typedef struct ReconfigParams_t {
PFNRECONFIGUEFLUSHCALLBACK p_fn_reconfigure_flush;
void *p_reconfig_user_struct;
uint32_t reconfig_flush_mode;
} ReconfigParams;
class RocVideoDecoder {
public:
/**
@@ -233,6 +240,15 @@ class RocVideoDecoder {
* @return false
*/
bool GetOutputSurfaceInfo(OutputSurfaceInfo **surface_info);
/**
* @brief Function to set the Reconfig Params object
*
* @param p_reconfig_params: pointer to reconfig params struct
* @return true : success
* @return false : fail
*/
bool SetReconfigParams(ReconfigParams *p_reconfig_params);
/**
* @brief this function decodes a frame and returns the number of frames avalable for display
*
@@ -253,10 +269,11 @@ class RocVideoDecoder {
* @brief function to release frame after use by the application: Only used with "OUT_SURFACE_MEM_DEV_INTERNAL"
*
* @param pTimestamp - timestamp of the frame to be released (unmapped)
* @param b_flushing - true when flushing
* @return true - success
* @return false - falied
*/
bool ReleaseFrame(int64_t pTimestamp);
bool ReleaseFrame(int64_t pTimestamp, bool b_flushing = false);
/**
* @brief utility function to save image to a file
@@ -307,6 +324,12 @@ class RocVideoDecoder {
* @param [out] digest Pointer to the 16 byte message digest
*/
void FinalizeMd5(uint8_t **digest);
/**
* @brief Get the Num Of Flushed Frames from video decoder object
*
* @return int32_t
*/
int32_t GetNumOfFlushedFrames() { return num_frames_flushed_during_reconfig_;}
private:
int decoder_session_id_; // Decoder session identifier. Used to gather session level stats.
@@ -356,12 +379,21 @@ class RocVideoDecoder {
* @brief This function reconfigure decoder if there is a change in sequence params.
*/
int ReconfigureDecoder(RocdecVideoFormat *p_video_format);
/**
* @brief function to release all internal frames and clear the vp_frames_q_ (used with reconfigure): Only used with "OUT_SURFACE_MEM_DEV_INTERNAL"
*
* @return true - success
* @return false - falied
*/
bool ReleaseInternalFrames();
/**
* @brief Function to Initialize GPU-HIP
*
*/
bool InitHIP(int device_id);
int num_devices_;
int device_id_;
RocdecVideoParser rocdec_parser_ = nullptr;
@@ -369,7 +401,8 @@ class RocVideoDecoder {
OutputSurfaceMemoryType out_mem_type_ = OUT_SURFACE_MEM_DEV_INTERNAL;
bool b_extract_sei_message_ = false;
bool b_force_zero_latency_ = false;
//bool b_device_frame_pitched_ = true;
ReconfigParams *p_reconfig_params_ = nullptr;
int32_t num_frames_flushed_during_reconfig_ = 0;
hipDeviceProp_t hip_dev_prop_;
hipStream_t hip_stream_;
rocDecVideoCodec codec_id_ = rocDecVideoCodec_NumCodecs;