From 22de4fbc6ff6a3cff01552d925ae3752564650ac Mon Sep 17 00:00:00 2001 From: Lakshmi Kumar Date: Wed, 29 Nov 2023 08:22:21 -0800 Subject: [PATCH] Samples - adding a sample to illustrate StreamProvider for demuxer (#99) * mem sample * changes to match c++ style * ffmpeg log * readme update * readme clean up --- samples/videoDecodeMem/CMakeLists.txt | 93 ++++++++++ samples/videoDecodeMem/README.md | 37 ++++ samples/videoDecodeMem/videodecodemem.cpp | 201 ++++++++++++++++++++++ utils/video_demuxer.h | 2 +- 4 files changed, 332 insertions(+), 1 deletion(-) create mode 100644 samples/videoDecodeMem/CMakeLists.txt create mode 100644 samples/videoDecodeMem/README.md create mode 100644 samples/videoDecodeMem/videodecodemem.cpp diff --git a/samples/videoDecodeMem/CMakeLists.txt b/samples/videoDecodeMem/CMakeLists.txt new file mode 100644 index 0000000000..a9e3e93483 --- /dev/null +++ b/samples/videoDecodeMem/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(videodecodemem) +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} videodecodemem.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/videoDecodeMem/README.md b/samples/videoDecodeMem/README.md new file mode 100644 index 0000000000..c6ab369bb5 --- /dev/null +++ b/samples/videoDecodeMem/README.md @@ -0,0 +1,37 @@ +# Video Decode Sample +This sample illustrates a way to pass the data chunk-by-chunk sequentially to the FFMPEG demuxer which are then decoded on AMD hardware using rocDecode library. + +## 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 +``` +./videodecodemem -i + -o + -d + -z + -sei + -crop +``` \ No newline at end of file diff --git a/samples/videoDecodeMem/videodecodemem.cpp b/samples/videoDecodeMem/videodecodemem.cpp new file mode 100644 index 0000000000..04084d9548 --- /dev/null +++ b/samples/videoDecodeMem/videodecodemem.cpp @@ -0,0 +1,201 @@ +/* +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 +#if __cplusplus >= 201703L && __has_include() + #include +#else + #include +#endif +#include "video_demuxer.h" +#include "roc_video_dec.h" + +class FileStreamProvider : public VideoDemuxer::StreamProvider { +public: + FileStreamProvider(const char *input_file_path) { + fp_in_.open(input_file_path, std::ifstream::in | std::ifstream::binary); + if (!fp_in_) { + std::cout << "Unable to open input file: " << input_file_path << std::endl; + return; + } + } + ~FileStreamProvider() { + fp_in_.close(); + } + // Fill in the buffer owned by the demuxer + int GetData(uint8_t *p_buf, int n_buf) { + // We read a file for this example. You may get your data from network or somewhere else + return static_cast(fp_in_.read(reinterpret_cast(p_buf), n_buf).gcount()); + } + +private: + std::ifstream fp_in_; +}; + +void ShowHelpAndExit(const char *option = NULL) { + std::cout << "Options:" << std::endl + << "-i Input File Path - required" << std::endl + << "-o Output File Path - dumps output if requested; optional" << std::endl + << "-d GPU device ID (0 for the first device, 1 for the second, etc.); optional; default: 0" << std::endl + << "-z force_zero_latency (force_zero_latency, Decoded frames will be flushed out for display immediately); optional;" << std::endl + << "-sei extract SEI messages; optional;" << std::endl + << "-crop crop rectangle for output (not used when using interopped decoded frame); optional; default: 0" << std::endl; + exit(0); +} + +int main(int argc, char **argv) { + + std::string input_file_path, output_file_path; + int dump_output_frames = 0; + int device_id = 0; + bool b_force_zero_latency = false; // false by default: enabling this option might affect decoding performance + bool b_extract_sei_messages = false; + Rect crop_rect = {}; + Rect *p_crop_rect = nullptr; + OutputSurfaceMemoryType mem_type = OUT_SURFACE_MEM_DEV_INTERNAL; // set to internal + // 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"); + } + input_file_path = argv[i]; + continue; + } + if (!strcmp(argv[i], "-o")) { + if (++i == argc) { + ShowHelpAndExit("-o"); + } + output_file_path = argv[i]; + dump_output_frames = 1; + continue; + } + if (!strcmp(argv[i], "-d")) { + if (++i == argc) { + ShowHelpAndExit("-d"); + } + device_id = atoi(argv[i]); + continue; + } + if (!strcmp(argv[i], "-z")) { + if (i == argc) { + ShowHelpAndExit("-z"); + } + b_force_zero_latency = true; + continue; + } + if (!strcmp(argv[i], "-sei")) { + if (i == argc) { + ShowHelpAndExit("-sei"); + } + b_extract_sei_messages = true; + continue; + } + if (!strcmp(argv[i], "-crop")) { + if (++i == argc || 4 != sscanf(argv[i], "%d,%d,%d,%d", &crop_rect.l, &crop_rect.t, &crop_rect.r, &crop_rect.b)) { + ShowHelpAndExit("-crop"); + } + if ((crop_rect.r - crop_rect.l) % 2 == 1 || (crop_rect.b - crop_rect.t) % 2 == 1) { + std::cout << "output crop rectangle must have width and height of even numbers" << std::endl; + exit(1); + } + p_crop_rect = &crop_rect; + continue; + } + ShowHelpAndExit(argv[i]); + } + try { + FileStreamProvider stream_provider(input_file_path.c_str()); + VideoDemuxer demuxer(&stream_provider); + rocDecVideoCodec rocdec_codec_id = AVCodec2RocDecVideoCodec(demuxer.GetCodecID()); + RocVideoDecoder viddec(device_id, mem_type, rocdec_codec_id, false, b_force_zero_latency, p_crop_rect, 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 (dump_output_frames) { + viddec.SaveFrameToFile(output_file_path, 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 (!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; +} diff --git a/utils/video_demuxer.h b/utils/video_demuxer.h index 14335bf36e..947098e52c 100644 --- a/utils/video_demuxer.h +++ b/utils/video_demuxer.h @@ -152,6 +152,7 @@ bool VideoDemuxer::Demux(uint8_t **video, int *video_size, int64_t *pts) { } VideoDemuxer::VideoDemuxer(AVFormatContext *av_fmt_input_ctx) : av_fmt_input_ctx_(av_fmt_input_ctx) { + av_log_set_level(AV_LOG_QUIET); if (!av_fmt_input_ctx_) { std::cerr << "ERROR: av_fmt_input_ctx_ is not vaild!" << std::endl; return; @@ -255,7 +256,6 @@ AVFormatContext *VideoDemuxer::CreateFmtContextUtil(StreamProvider *stream_provi AVFormatContext *VideoDemuxer::CreateFmtContextUtil(const char *input_file_path) { avformat_network_init(); - av_log_set_level(AV_LOG_QUIET); AVFormatContext *ctx = nullptr; if (avformat_open_input(&ctx, input_file_path, nullptr, nullptr) != 0 ) { std::cerr << "ERROR: avformat_open_input failed!" << std::endl;