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
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
Copyright (c) 2023 - 2023 Advanced Micro Devices, Inc. All rights reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "roc_video_dec.h"
|
||||
|
||||
typedef enum ReconfigFlushMode_enum {
|
||||
RECONFIG_FLUSH_MODE_DUMP_TO_FILE = 0, /**< The remaining frames will be dumped to file in this mode */
|
||||
//TODO;; Add new flush modes here : defined by the application
|
||||
} ReconfigFlushMode;
|
||||
|
||||
// this struct is used by videodecode and videodecodeMultiFiles to dump last frames to file
|
||||
typedef struct ReconfigDumpFileStruct_t {
|
||||
bool b_dump_frames_to_file;
|
||||
std::string output_file_name;
|
||||
} ReconfigDumpFileStruct;
|
||||
|
||||
|
||||
// callback function to flush last frames and save it to file when reconfigure happens
|
||||
int ReconfigureFlushCallback(void *p_viddec_obj, uint32_t flush_mode, void *p_user_struct) {
|
||||
int n_frames_flushed = 0;
|
||||
if ((p_viddec_obj == nullptr) || (p_user_struct == nullptr)) return n_frames_flushed;
|
||||
|
||||
RocVideoDecoder *viddec = static_cast<RocVideoDecoder *> (p_viddec_obj);
|
||||
OutputSurfaceInfo *surf_info;
|
||||
if (!viddec->GetOutputSurfaceInfo(&surf_info)) {
|
||||
std::cerr << "Error: Failed to get Output Surface Info!" << std::endl;
|
||||
return n_frames_flushed;
|
||||
}
|
||||
if (flush_mode == ReconfigFlushMode::RECONFIG_FLUSH_MODE_DUMP_TO_FILE) {
|
||||
ReconfigDumpFileStruct *p_dump_file_struct = static_cast<ReconfigDumpFileStruct *>(p_user_struct);
|
||||
uint8_t *pframe = nullptr;
|
||||
int64_t pts;
|
||||
while ((pframe = viddec->GetFrame(&pts))) {
|
||||
if (p_dump_file_struct->b_dump_frames_to_file) {
|
||||
viddec->SaveFrameToFile(p_dump_file_struct->output_file_name, pframe, surf_info);
|
||||
}
|
||||
// release and flush frame
|
||||
viddec->ReleaseFrame(pts, true);
|
||||
n_frames_flushed ++;
|
||||
}
|
||||
} else {
|
||||
//todo:: handle other cases
|
||||
}
|
||||
return n_frames_flushed;
|
||||
}
|
||||
@@ -75,7 +75,7 @@ if(HIP_FOUND AND FFMPEG_FOUND AND ROCDECODE_FOUND)
|
||||
${AVFORMAT_INCLUDE_DIR})
|
||||
set(LINK_LIBRARY_LIST ${LINK_LIBRARY_LIST} ${FFMPEG_LIBRARIES})
|
||||
# rocDecode
|
||||
include_directories (${ROCDECODE_INCLUDE_DIR})
|
||||
include_directories (${ROCDECODE_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/..)
|
||||
set(LINK_LIBRARY_LIST ${LINK_LIBRARY_LIST} ${ROCDECODE_LIBRARY})
|
||||
|
||||
list(APPEND SOURCES ${PROJECT_SOURCE_DIR} videodecode.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../utils/rocvideodecode/roc_video_dec.cpp)
|
||||
|
||||
@@ -38,6 +38,7 @@ THE SOFTWARE.
|
||||
#endif
|
||||
#include "video_demuxer.h"
|
||||
#include "roc_video_dec.h"
|
||||
#include "common.h"
|
||||
|
||||
void ShowHelpAndExit(const char *option = NULL) {
|
||||
std::cout << "Options:" << std::endl
|
||||
@@ -64,9 +65,13 @@ int main(int argc, char **argv) {
|
||||
bool b_extract_sei_messages = false;
|
||||
bool b_generate_md5 = false;
|
||||
bool b_md5_check = false;
|
||||
bool b_flush_frames_during_reconfig = true;
|
||||
Rect crop_rect = {};
|
||||
Rect *p_crop_rect = nullptr;
|
||||
OutputSurfaceMemoryType mem_type = OUT_SURFACE_MEM_DEV_INTERNAL; // set to internal
|
||||
OutputSurfaceMemoryType mem_type = OUT_SURFACE_MEM_DEV_INTERNAL; // set to internal
|
||||
ReconfigParams reconfig_params = { 0 };
|
||||
ReconfigDumpFileStruct reconfig_user_struct = { 0 };
|
||||
|
||||
// Parse command-line arguments
|
||||
if(argc <= 1) {
|
||||
ShowHelpAndExit();
|
||||
@@ -145,8 +150,14 @@ int main(int argc, char **argv) {
|
||||
mem_type = static_cast<OutputSurfaceMemoryType>(atoi(argv[i]));
|
||||
continue;
|
||||
}
|
||||
if (!strcmp(argv[i], "flush")) {
|
||||
b_flush_frames_during_reconfig = atoi(argv[i]) ? true : false;
|
||||
continue;
|
||||
}
|
||||
|
||||
ShowHelpAndExit(argv[i]);
|
||||
}
|
||||
|
||||
try {
|
||||
VideoDemuxer demuxer(input_file_path.c_str());
|
||||
rocDecVideoCodec rocdec_codec_id = AVCodec2RocDecVideoCodec(demuxer.GetCodecID());
|
||||
@@ -171,6 +182,12 @@ int main(int argc, char **argv) {
|
||||
OutputSurfaceInfo *surf_info;
|
||||
uint32_t width, height;
|
||||
double total_dec_time = 0;
|
||||
// initialize reconfigure params: the following is configured to dump to output which is relevant for this sample
|
||||
reconfig_params.p_fn_reconfigure_flush = ReconfigureFlushCallback;
|
||||
reconfig_user_struct.b_dump_frames_to_file = dump_output_frames;
|
||||
reconfig_user_struct.output_file_name = output_file_path;
|
||||
reconfig_params.reconfig_flush_mode = RECONFIG_FLUSH_MODE_DUMP_TO_FILE;
|
||||
reconfig_params.p_reconfig_user_struct = &reconfig_user_struct;
|
||||
|
||||
if (b_generate_md5) {
|
||||
viddec.InitMd5();
|
||||
@@ -178,6 +195,7 @@ int main(int argc, char **argv) {
|
||||
if (b_md5_check) {
|
||||
ref_md5_file.open(md5_file_path.c_str(), std::ios::in);
|
||||
}
|
||||
if (dump_output_frames) viddec.SetReconfigParams(&reconfig_params);
|
||||
|
||||
do {
|
||||
auto start_time = std::chrono::high_resolution_clock::now();
|
||||
@@ -207,7 +225,8 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
n_frame += n_frame_returned;
|
||||
} while (n_video_bytes);
|
||||
|
||||
|
||||
n_frame += viddec.GetNumOfFlushedFrames();
|
||||
std::cout << "info: Total frame decoded: " << n_frame << std::endl;
|
||||
if (!dump_output_frames) {
|
||||
std::cout << "info: avg decoding time per frame: " << total_dec_time / n_frame << " ms" <<std::endl;
|
||||
|
||||
@@ -75,7 +75,7 @@ if(HIP_FOUND AND FFMPEG_FOUND AND ROCDECODE_FOUND)
|
||||
${AVFORMAT_INCLUDE_DIR})
|
||||
set(LINK_LIBRARY_LIST ${LINK_LIBRARY_LIST} ${FFMPEG_LIBRARIES})
|
||||
# rocDecode
|
||||
include_directories (${ROCDECODE_INCLUDE_DIR})
|
||||
include_directories (${ROCDECODE_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/..)
|
||||
set(LINK_LIBRARY_LIST ${LINK_LIBRARY_LIST} ${ROCDECODE_LIBRARY})
|
||||
|
||||
list(APPEND SOURCES ${PROJECT_SOURCE_DIR} videodecodemultifiles.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../utils/rocvideodecode/roc_video_dec.cpp)
|
||||
|
||||
@@ -38,12 +38,14 @@ THE SOFTWARE.
|
||||
#endif
|
||||
#include "video_demuxer.h"
|
||||
#include "roc_video_dec.h"
|
||||
#include "common.h"
|
||||
|
||||
typedef struct {
|
||||
std::string in_file;
|
||||
std::string out_file;
|
||||
bool b_force_zero_latency;
|
||||
bool b_extract_sei_messages;
|
||||
bool b_flush_last_frames;
|
||||
Rect crop_rect;
|
||||
Rect *p_crop_rect;
|
||||
int dump_output_frames;
|
||||
@@ -60,6 +62,7 @@ void ShowHelpAndExit(const char *option = NULL) {
|
||||
<< "sei 0 (extract SEI messages; default: 0)" << std::endl
|
||||
<< "crop l,t,r,b (crop rectangle for output (not used when using interopped decoded frame); default: 0)" << std::endl
|
||||
<< "m 0 decoded surface memory; optional; default - 0 [0 : OUT_SURFACE_MEM_DEV_INTERNAL/ 1 : OUT_SURFACE_MEM_DEV_COPIED/ 2 : OUT_SURFACE_MEM_HOST_COPIED]" << std::endl
|
||||
<< "flush 1 flush last frames during reconfig; optional; default - 1 [1 : Flush last frames during reconfig 0 : Discard last frames during reconfigure ]" << std::endl
|
||||
<< "infile input2.[mp4/mov...]" << std::endl
|
||||
<< "outfile output2.yuv" << std::endl
|
||||
<< "...." << std::endl
|
||||
@@ -126,6 +129,7 @@ void ParseCommandLine(std::deque<FileInfo> *multi_file_data, int &device_id, boo
|
||||
file_idx++;
|
||||
file_data.b_force_zero_latency = false;
|
||||
file_data.b_extract_sei_messages = false;
|
||||
file_data.b_flush_last_frames = true;
|
||||
file_data.dump_output_frames = 0;
|
||||
file_data.crop_rect = {0, 0, 0, 0};
|
||||
file_data.p_crop_rect = nullptr;
|
||||
@@ -137,6 +141,8 @@ void ParseCommandLine(std::deque<FileInfo> *multi_file_data, int &device_id, boo
|
||||
file_data.b_force_zero_latency = atoi(value) ? true : false;
|
||||
} else if (!strcmp(param, "sei")) {
|
||||
file_data.b_extract_sei_messages = atoi(value) ? true : false;
|
||||
} else if (!strcmp(param, "flush")) {
|
||||
file_data.b_flush_last_frames = atoi(value) ? true : false;
|
||||
} else if (!strcmp(param, "crop")) {
|
||||
sscanf(value, "%d,%d,%d,%d", &file_data.crop_rect.l, &file_data.crop_rect.t, &file_data.crop_rect.r, &file_data.crop_rect.b);
|
||||
if ((file_data.crop_rect.r - file_data.crop_rect.l) % 2 == 1 || (file_data.crop_rect.b - file_data.crop_rect.t) % 2 == 1) {
|
||||
@@ -162,6 +168,8 @@ int main(int argc, char **argv) {
|
||||
|
||||
ParseCommandLine (&multi_file_data, device_id, use_reconfigure, argc, argv);
|
||||
RocVideoDecoder *viddec = NULL;
|
||||
ReconfigParams reconfig_params = { 0 };
|
||||
ReconfigDumpFileStruct reconfig_user_struct = { 0 };
|
||||
|
||||
try {
|
||||
while (!multi_file_data.empty()) {
|
||||
@@ -169,14 +177,22 @@ int main(int argc, char **argv) {
|
||||
multi_file_data.pop_front();
|
||||
VideoDemuxer demuxer(file_data.in_file.c_str());
|
||||
rocDecVideoCodec rocdec_codec_id = AVCodec2RocDecVideoCodec(demuxer.GetCodecID());
|
||||
|
||||
if (file_data.b_flush_last_frames && file_data.dump_output_frames) {
|
||||
reconfig_params.p_fn_reconfigure_flush = ReconfigureFlushCallback;
|
||||
reconfig_user_struct.b_dump_frames_to_file = file_data.dump_output_frames;
|
||||
reconfig_user_struct.output_file_name = file_data.out_file;
|
||||
reconfig_params.reconfig_flush_mode = RECONFIG_FLUSH_MODE_DUMP_TO_FILE;
|
||||
reconfig_params.p_reconfig_user_struct = &reconfig_user_struct;
|
||||
}
|
||||
if (use_reconfigure) {
|
||||
|
||||
if (!viddec) {
|
||||
viddec = new RocVideoDecoder(device_id, file_data.mem_type, rocdec_codec_id, file_data.b_force_zero_latency, file_data.p_crop_rect, file_data.b_extract_sei_messages);
|
||||
}
|
||||
} else {
|
||||
viddec = new RocVideoDecoder(device_id, file_data.mem_type, rocdec_codec_id, file_data.b_force_zero_latency, file_data.p_crop_rect, file_data.b_extract_sei_messages);
|
||||
}
|
||||
if (viddec && file_data.b_flush_last_frames) viddec->SetReconfigParams(&reconfig_params);
|
||||
std::string device_name, gcn_arch_name;
|
||||
int pci_bus_id, pci_domain_id, pci_device_id;
|
||||
|
||||
@@ -223,6 +239,7 @@ int main(int argc, char **argv) {
|
||||
n_frame += n_frame_returned;
|
||||
} while (n_video_bytes);
|
||||
|
||||
n_frame += viddec->GetNumOfFlushedFrames();
|
||||
std::cout << "info: Total frame decoded: " << n_frame << std::endl;
|
||||
if (!file_data.dump_output_frames) {
|
||||
std::cout << "info: avg decoding time per frame (ms): " << total_dec_time / n_frame << std::endl;
|
||||
|
||||
Reference in New Issue
Block a user