From 2cb3bd143c5b642589fa11c193491b5fde20ee72 Mon Sep 17 00:00:00 2001 From: Lakshmi Kumar Date: Wed, 29 Nov 2023 08:13:24 -0800 Subject: [PATCH] Sample - add a new sample for processing multiple input files (#97) * adding a sample for decoding multiple files * readme correction * spacing * review comments --- .gitignore | 3 +- samples/videoDecodeMultiFiles/CMakeLists.txt | 93 ++++++++ samples/videoDecodeMultiFiles/README.md | 50 ++++ .../videodecodemultifiles.cpp | 214 ++++++++++++++++++ 4 files changed, 358 insertions(+), 2 deletions(-) create mode 100644 samples/videoDecodeMultiFiles/CMakeLists.txt create mode 100644 samples/videoDecodeMultiFiles/README.md create mode 100644 samples/videoDecodeMultiFiles/videodecodemultifiles.cpp diff --git a/.gitignore b/.gitignore index 52529ece79..c78f4006be 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,4 @@ build/ samples/*/build .vscode/ doxygen_output* -samples/videoDecode/videodecode_test.cpp -samples/videoDecodeCMakeLists_test.txt \ No newline at end of file +samples/videoDecodeMultiFiles/example.txt \ No newline at end of file diff --git a/samples/videoDecodeMultiFiles/CMakeLists.txt b/samples/videoDecodeMultiFiles/CMakeLists.txt new file mode 100644 index 0000000000..be248e5832 --- /dev/null +++ b/samples/videoDecodeMultiFiles/CMakeLists.txt @@ -0,0 +1,93 @@ +################################################################################ +# Copyright (c) 2023 Advanced Micro Devices, Inc. +# +# 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. +# +################################################################################ + +cmake_minimum_required (VERSION 3.5) +project(videodecodemultifiles) +set(CMAKE_CXX_STANDARD 17) + +# ROCM Path +if(DEFINED ENV{ROCM_PATH}) + set(ROCM_PATH $ENV{ROCM_PATH} CACHE PATH "Default ROCm installation path") +elseif(ROCM_PATH) + message("-- INFO:ROCM_PATH Set -- ${ROCM_PATH}") +else() + set(ROCM_PATH /opt/rocm CACHE PATH "Default ROCm installation path") +endif() + +list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../../cmake) +list(APPEND CMAKE_PREFIX_PATH ${ROCM_PATH}/hip ${ROCM_PATH}) +set(CMAKE_CXX_COMPILER ${ROCM_PATH}/llvm/bin/clang++) + +if(CMAKE_BUILD_TYPE MATCHES Debug) + # -O0 -- Don't Optimize output file + # -gdwarf-4 -- generate debugging information, dwarf-4 for making valgrind work + # -Og -- Optimize for debugging experience rather than speed or size + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -gdwarf-4 -Og") +else() + # -O3 -- Optimize output file + # -DNDEBUG -- turn off asserts + # -fPIC -- Generate position-independent code if possible + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -DNDEBUG -fPIC") +endif() + +set(DEFAULT_AMDGPU_TARGETS "gfx803;gfx900;gfx906;gfx908;gfx90a;gfx940;gfx1030;gfx1031;gfx1032;gfx1100") +set(AMDGPU_TARGETS "${DEFAULT_AMDGPU_TARGETS}" CACHE STRING "List of specific machine types for library to target") + +find_package(HIP QUIET) +find_package(FFmpeg QUIET) +# find rocDecode +find_library(ROCDECODE_LIBRARY NAMES rocdecode HINTS {ROCM_PATH}/lib) +find_path(ROCDECODE_INCLUDE_DIR NAMES rocdecode.h PATHS /opt/rocm/include/rocdecode {ROCM_PATH}/include/rocdecode) + +if(ROCDECODE_LIBRARY AND ROCDECODE_INCLUDE_DIR) + set(ROCDECODE_FOUND TRUE) + message("-- ${White}Using rocDecode -- \n\tLibraries:${ROCDECODE_LIBRARY} \n\tIncludes:${ROCDECODE_INCLUDE_DIR}${ColourReset}") +endif() + +if(HIP_FOUND AND FFMPEG_FOUND AND ROCDECODE_FOUND) + # HIP + set(LINK_LIBRARY_LIST ${LINK_LIBRARY_LIST} hip::device) + # FFMPEG + include_directories(${AVUTIL_INCLUDE_DIR} ${AVCODEC_INCLUDE_DIR} + ${AVFORMAT_INCLUDE_DIR}) + set(LINK_LIBRARY_LIST ${LINK_LIBRARY_LIST} ${FFMPEG_LIBRARIES}) + # rocDecode + include_directories (${ROCDECODE_INCLUDE_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) + add_executable(${PROJECT_NAME} ${SOURCES}) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++17") + target_link_libraries(${PROJECT_NAME} ${LINK_LIBRARY_LIST}) +else() + message("-- ERROR!: ${PROJECT_NAME} excluded! please install all the dependencies and try again!") + if (NOT HIP_FOUND) + message(FATAL_ERROR "-- ERROR!: HIP Not Found! - please install ROCm and HIP!") + endif() + if (NOT FFMPEG_FOUND) + message(FATAL_ERROR "-- ERROR!: FFMPEG Not Found! - please install FFMPEG!") + endif() + if (NOT ROCDECODE_FOUND) + message(FATAL_ERROR "-- ERROR!: rocDecode Not Found! - please install rocDecode!") + endif() +endif() \ No newline at end of file diff --git a/samples/videoDecodeMultiFiles/README.md b/samples/videoDecodeMultiFiles/README.md new file mode 100644 index 0000000000..35089eb21c --- /dev/null +++ b/samples/videoDecodeMultiFiles/README.md @@ -0,0 +1,50 @@ +# Video Decode Multi Files Sample +This sample illustrates the FFMPEG demuxer to get the individual frames which are then decoded on AMD hardware using rocDecode library. + +This sample takes multiple files as a list and decodes each of them one after the other. + +## Prerequisites: + +* Linux distribution + + Ubuntu - `20.04` / `22.04` + +* [ROCm supported hardware](https://rocm.docs.amd.com/en/latest/release/gpu_os_support.html) + +* Install [ROCm 5.5 or later](https://rocmdocs.amd.com/en/latest/deploy/linux/installer/install.html) with `--usecase=graphics,rocm --no-32` + +* rocDecode + +* CMake `3.5` or later + +* [FFMPEG](https://ffmpeg.org/about.html) + ``` + sudo apt install ffmpeg + ``` + +## Build +``` +mkdir build +cd build +cmake ../ +make -j +``` +# Run + +* Example input file list - example.txt + +``` +infile input1.[mp4/mov...] [required] +outfile output1.yuv [optional] +z 0 [optional] +sei 0 [optional] +crop l,t,r,b [optional] +infile input2.[mp4/mov...] [optional] +outfile output2.yuv [optional] +... +... +``` + +``` +./videodecodemultifiles -i + -d +``` \ No newline at end of file diff --git a/samples/videoDecodeMultiFiles/videodecodemultifiles.cpp b/samples/videoDecodeMultiFiles/videodecodemultifiles.cpp new file mode 100644 index 0000000000..6116437486 --- /dev/null +++ b/samples/videoDecodeMultiFiles/videodecodemultifiles.cpp @@ -0,0 +1,214 @@ +/* +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. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#if __cplusplus >= 201703L && __has_include() + #include +#else + #include +#endif +#include "video_demuxer.h" +#include "roc_video_dec.h" + +typedef struct { + std::string in_file; + std::string out_file; + bool b_force_zero_latency; + bool b_extract_sei_messages; + Rect crop_rect; + Rect *p_crop_rect; + int dump_output_frames; +} FileInfo; + +void ShowHelpAndExit(const char *option = NULL) { + std::cout << "Options:" << std::endl + << "-i Input File List - required (text file containing all files to decode in below format)" << std::endl + << "example.txt:" << std::endl + << "infile input1.[mp4/mov...] (Input file path)" << std::endl + << "outfile output1.yuv (Output file path)" << std::endl + << "z 0 (force_zero_latency - Decoded frames will be flushed out for display immediately; default: 0)" << std::endl + << "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 + << "infile input2.[mp4/mov...]" << std::endl + << "outfile output2.yuv" << std::endl + << "...." << std::endl + << "...." << std::endl + << "-d GPU device ID (0 for the first device, 1 for the second, etc.); optional; default: 0" << std::endl; + exit(0); +} + +void ParseCommandLine(std::deque *multi_file_data, int &device_id, int argc, char *argv[]) { + + FileInfo file_data; + std::string file_list_path; + + // Parse command-line arguments + if(argc < 1) { + ShowHelpAndExit(); + } + for (int i = 1; i < argc; i++) { + if (!strcmp(argv[i], "-h")) { + ShowHelpAndExit(); + } + if (!strcmp(argv[i], "-i")) { + if (++i == argc) { + ShowHelpAndExit("-i"); + } + file_list_path = argv[i]; + continue; + } + if (!strcmp(argv[i], "-d")) { + if (++i == argc) { + ShowHelpAndExit("-d"); + } + device_id = atoi(argv[i]); + continue; + } + ShowHelpAndExit(argv[i]); + } + + // Parse the input filelist + std::ifstream filestream(file_list_path); + std::string line; + char* str; + char param[256]; + char value[256]; + int file_idx = 0; + + while (std::getline(filestream, line)) { + str = (char *)line.c_str(); + sscanf(str,"%s %s", param, value); + if (!strcmp(param, "infile")) { + if (file_idx > 0) { + multi_file_data->push_back(file_data); + } + file_data.in_file = value; + file_idx++; + file_data.b_force_zero_latency = false; + file_data.b_extract_sei_messages = false; + file_data.dump_output_frames = 0; + file_data.crop_rect = {0, 0, 0, 0}; + } else if (!strcmp(param, "outfile")) { + file_data.out_file = value; + file_data.dump_output_frames = 1; + } else if (!strcmp(param, "z")) { + 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, "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) { + std::cout << "Cropping rect must have width and height of even numbers" << std::endl; + exit(1); + } + file_data.p_crop_rect = &file_data.crop_rect; + } + } + if (file_idx > 0) { + multi_file_data->push_back(file_data); + } +} + +int main(int argc, char **argv) { + + std::deque multi_file_data; + FileInfo file_data; + int device_id = 0; + OutputSurfaceMemoryType mem_type = OUT_SURFACE_MEM_DEV_INTERNAL; // set to internal + + ParseCommandLine (&multi_file_data, device_id, argc, argv); + + try { + while (!multi_file_data.empty()) { + file_data = multi_file_data.front(); + multi_file_data.pop_front(); + VideoDemuxer demuxer(file_data.in_file.c_str()); + rocDecVideoCodec rocdec_codec_id = AVCodec2RocDecVideoCodec(demuxer.GetCodecID()); + RocVideoDecoder viddec(device_id, mem_type, rocdec_codec_id, false, file_data.b_force_zero_latency, file_data.p_crop_rect, file_data.b_extract_sei_messages); + + std::string device_name, gcn_arch_name; + int pci_bus_id, pci_domain_id, pci_device_id; + + viddec.GetDeviceinfo(device_name, gcn_arch_name, pci_bus_id, pci_domain_id, pci_device_id); + std::cout << "info: Using GPU device " << device_id << " - " << device_name << "[" << gcn_arch_name << "] on PCI bus " << + std::setfill('0') << std::setw(2) << std::right << std::hex << pci_bus_id << ":" << std::setfill('0') << std::setw(2) << + std::right << std::hex << pci_domain_id << "." << pci_device_id << std::dec << std::endl; + std::cout << "info: decoding started, please wait!" << std::endl; + + int n_video_bytes = 0, n_frame_returned = 0, n_frame = 0; + uint8_t *pvideo = nullptr; + int pkg_flags = 0; + uint8_t *pframe = nullptr; + int64_t pts = 0; + OutputSurfaceInfo *surf_info; + uint32_t width, height; + double total_dec_time = 0; + + do { + auto start_time = std::chrono::high_resolution_clock::now(); + demuxer.Demux(&pvideo, &n_video_bytes, &pts); + // Treat 0 bitstream size as end of stream indicator + if (n_video_bytes == 0) { + pkg_flags |= ROCDEC_PKT_ENDOFSTREAM; + } + n_frame_returned = viddec.DecodeFrame(pvideo, n_video_bytes, pkg_flags, pts); + auto end_time = std::chrono::high_resolution_clock::now(); + auto time_per_frame = std::chrono::duration(end_time - start_time).count(); + total_dec_time += time_per_frame; + if (!n_frame && !viddec.GetOutputSurfaceInfo(&surf_info)) { + std::cerr << "Error: Failed to get Output Surface Info!" << std::endl; + break; + } + for (int i = 0; i < n_frame_returned; i++) { + pframe = viddec.GetFrame(&pts); + if (file_data.dump_output_frames) { + viddec.SaveFrameToFile(file_data.out_file, pframe, surf_info); + } + // release frame + viddec.ReleaseFrame(pts); + } + n_frame += n_frame_returned; + } while (n_video_bytes); + + 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; + std::cout << "info: avg FPS: " << (n_frame / total_dec_time) * 1000 << std::endl; + } + } + } catch (const std::exception &ex) { + std::cout << ex.what() << std::endl; + exit(1); + } + + return 0; +}