From 08f7236c942e58458f0076aff1333e8bcc1acf10 Mon Sep 17 00:00:00 2001 From: Aryan Salmanpour Date: Thu, 13 Jun 2024 14:04:53 -0400 Subject: [PATCH] Add support for rocJpegDecodeBatched API - part 1 of 2 (#31) * Add initial support for batch decoding * Add support for reading and parsing the images in batches and allocating the output buffers * Add initial support for the rocJpegDecodeBatched API * use recursive_mutex to allow DecodeBatched and Decode functions called concurrently * code cleanup * Add a CTEST for jpegdecodebatched * modify the help message * code clean up [ROCm/rocjpeg commit: c660aeab43088a14f2b2f64dc2a78efabae15149] --- projects/rocjpeg/CMakeLists.txt | 3 +- projects/rocjpeg/api/rocjpeg.h | 17 ++ projects/rocjpeg/samples/CMakeLists.txt | 12 ++ .../rocjpeg/samples/jpegDecode/jpegdecode.cpp | 4 +- .../samples/jpegDecodeBatched/CMakeLists.txt | 77 +++++++ .../samples/jpegDecodeBatched/README.md | 27 +++ .../jpegDecodeBatched/jpegdecodebatched.cpp | 196 ++++++++++++++++++ .../jpegdecodemultithreads.cpp | 2 +- .../rocjpeg/samples/rocjpeg_samples_utils.h | 33 ++- projects/rocjpeg/src/rocjpeg_api.cpp | 27 +++ projects/rocjpeg/src/rocjpeg_decoder.cpp | 20 +- projects/rocjpeg/src/rocjpeg_decoder.h | 17 +- projects/rocjpeg/test/CMakeLists.txt | 12 ++ 13 files changed, 429 insertions(+), 18 deletions(-) create mode 100644 projects/rocjpeg/samples/jpegDecodeBatched/CMakeLists.txt create mode 100644 projects/rocjpeg/samples/jpegDecodeBatched/README.md create mode 100644 projects/rocjpeg/samples/jpegDecodeBatched/jpegdecodebatched.cpp diff --git a/projects/rocjpeg/CMakeLists.txt b/projects/rocjpeg/CMakeLists.txt index f3ead49fc3..3f1e1f72c2 100644 --- a/projects/rocjpeg/CMakeLists.txt +++ b/projects/rocjpeg/CMakeLists.txt @@ -23,7 +23,7 @@ cmake_minimum_required (VERSION 3.5) -set(VERSION "0.3.0") +set(VERSION "0.4.0") set(CMAKE_CXX_STANDARD 17) # Set Project Version and Language @@ -137,6 +137,7 @@ if(HIP_FOUND AND Libva_FOUND) install(FILES samples/jpegDecode/CMakeLists.txt samples/jpegDecode/jpegdecode.cpp samples/jpegDecode/README.md DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/samples/jpegDecode COMPONENT dev) install(FILES samples/jpegDecodeMultiThreads/CMakeLists.txt samples/jpegDecodeMultiThreads/jpegdecodemultithreads.cpp samples/jpegDecodeMultiThreads/README.md DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/samples/jpegDecodeMultiThreads COMPONENT dev) + install(FILES samples/jpegDecodeBatched/CMakeLists.txt samples/jpegDecodeBatched/jpegdecodebatched.cpp samples/jpegDecodeBatched/README.md DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/samples/jpegDecodeBatched COMPONENT dev) install(FILES samples/rocjpeg_samples_utils.h DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/samples COMPONENT dev) install(DIRECTORY data/images DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/ COMPONENT dev) # install license information - {ROCM_PATH}/share/doc/rocJPEG diff --git a/projects/rocjpeg/api/rocjpeg.h b/projects/rocjpeg/api/rocjpeg.h index bc1603ff8a..ef7b755813 100644 --- a/projects/rocjpeg/api/rocjpeg.h +++ b/projects/rocjpeg/api/rocjpeg.h @@ -312,6 +312,23 @@ RocJpegStatus ROCJPEGAPI rocJpegGetImageInfo(RocJpegHandle handle, RocJpegStream RocJpegStatus ROCJPEGAPI rocJpegDecode(RocJpegHandle handle, RocJpegStreamHandle jpeg_stream_handle, const RocJpegDecodeParams *decode_params, RocJpegImage *destination); + +/** + * @fn RocJpegStatus ROCJPEGAPI rocJpegDecodeBatched(RocJpegHandle handle, RocJpegStreamHandle *jpeg_stream_handles, int batch_size, const RocJpegDecodeParams *decode_params, RocJpegImage *destinations); + * @ingroup group_amd_rocjpeg + * @brief Decodes a batch of JPEG images using the rocJPEG library. + * + * Decodes a batch of JPEG images using the rocJPEG library. + * + * @param handle The rocJPEG handle. + * @param jpeg_stream_handles An array of rocJPEG stream handles representing the input JPEG streams. + * @param batch_size The number of JPEG streams in the batch. + * @param decode_params The decode parameters for the JPEG decoding process. + * @param destinations An array of rocJPEG images representing the output decoded images. + * @return The status of the JPEG decoding operation. + */ +RocJpegStatus ROCJPEGAPI rocJpegDecodeBatched(RocJpegHandle handle, RocJpegStreamHandle *jpeg_stream_handles, int batch_size, const RocJpegDecodeParams *decode_params, RocJpegImage *destinations); + /** * @fn extern const char* ROCDECAPI rocJpegGetErrorName(RocJpegStatus rocjpeg_status); * @ingroup group_amd_rocjpeg diff --git a/projects/rocjpeg/samples/CMakeLists.txt b/projects/rocjpeg/samples/CMakeLists.txt index 90f5b5f252..6556344989 100644 --- a/projects/rocjpeg/samples/CMakeLists.txt +++ b/projects/rocjpeg/samples/CMakeLists.txt @@ -93,3 +93,15 @@ add_test( --test-command "jpegdecodemultithreads" -i ${CMAKE_SOURCE_DIR}/data/images/ -t 2 ) + +add_test( + NAME + jpeg-decode-batch-fmt-native + COMMAND + "${CMAKE_CTEST_COMMAND}" + --build-and-test "${CMAKE_CURRENT_SOURCE_DIR}/jpegDecodeBatched" + "${CMAKE_CURRENT_BINARY_DIR}/jpegDecodeBatched" + --build-generator "${CMAKE_GENERATOR}" + --test-command "jpegdecodebatched" + -i ${CMAKE_SOURCE_DIR}/data/images/ +) \ No newline at end of file diff --git a/projects/rocjpeg/samples/jpegDecode/jpegdecode.cpp b/projects/rocjpeg/samples/jpegDecode/jpegdecode.cpp index 93ae38bf38..d47d1aae19 100644 --- a/projects/rocjpeg/samples/jpegDecode/jpegdecode.cpp +++ b/projects/rocjpeg/samples/jpegDecode/jpegdecode.cpp @@ -48,7 +48,7 @@ int main(int argc, char **argv) { RocJpegDecodeParams decode_params = {}; RocJpegUtils rocjpeg_utils; - RocJpegUtils::ParseCommandLine(input_path, output_file_path, save_images, device_id, rocjpeg_backend, decode_params, nullptr, argc, argv); + RocJpegUtils::ParseCommandLine(input_path, output_file_path, save_images, device_id, rocjpeg_backend, decode_params, nullptr, nullptr, argc, argv); if (!RocJpegUtils::GetFilePaths(input_path, file_paths, is_dir, is_file)) { std::cerr << "ERROR: Failed to get input file paths!" << std::endl; return EXIT_FAILURE; @@ -95,7 +95,7 @@ int main(int argc, char **argv) { std::cerr << "The chroma sub-sampling is not supported by VCN Hardware" << std::endl; if (is_dir) { std::cout << std::endl; - continue; + continue; } else return EXIT_FAILURE; } diff --git a/projects/rocjpeg/samples/jpegDecodeBatched/CMakeLists.txt b/projects/rocjpeg/samples/jpegDecodeBatched/CMakeLists.txt new file mode 100644 index 0000000000..77248a88ac --- /dev/null +++ b/projects/rocjpeg/samples/jpegDecodeBatched/CMakeLists.txt @@ -0,0 +1,77 @@ +################################################################################ +# Copyright (c) 2024 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.0) +project(jpegdecodebatched) +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++) + +set(DEFAULT_AMDGPU_TARGETS "gfx908;gfx90a;gfx940;gfx941;gfx942;gfx1030;gfx1031;gfx1032;gfx1100;gfx1101;gfx1102") +set(AMDGPU_TARGETS "${DEFAULT_AMDGPU_TARGETS}" CACHE STRING "List of specific machine types for library to target") + +find_package(HIP QUIET) + +# find rocJPEG +find_library(ROCJPEG_LIBRARY NAMES rocjpeg HINTS {ROCM_PATH}/lib) +find_path(ROCJPEG_INCLUDE_DIR NAMES rocjpeg.h PATHS /opt/rocm/include/rocjpeg {ROCM_PATH}/include/rocjpeg) + +if(ROCJPEG_LIBRARY AND ROCJPEG_INCLUDE_DIR) + set(ROCJPEG_FOUND TRUE) + message("-- ${White}Using rocJPEG -- \n\tLibraries:${ROCJPEG_LIBRARY} \n\tIncludes:${ROCJPEG_INCLUDE_DIR}${ColourReset}") +endif() + +if(HIP_FOUND AND ROCJPEG_FOUND) + # HIP + set(LINK_LIBRARY_LIST ${LINK_LIBRARY_LIST} hip::device) + # rocJPEG + include_directories (${ROCJPEG_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/..) + set(LINK_LIBRARY_LIST ${LINK_LIBRARY_LIST} ${ROCJPEG_LIBRARY}) + #filesystem: c++ compilers less than equal to 8.5 need explicit link with stdc++fs + if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS_EQUAL "8.5") + set(LINK_LIBRARY_LIST ${LINK_LIBRARY_LIST} stdc++fs) + endif() + list(APPEND SOURCES ${PROJECT_SOURCE_DIR} jpegdecodebatched.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 ROCJPEG_FOUND) + message(FATAL_ERROR "-- ERROR!: rocJPEG Not Found! - please install rocJPEG!") + endif() +endif() \ No newline at end of file diff --git a/projects/rocjpeg/samples/jpegDecodeBatched/README.md b/projects/rocjpeg/samples/jpegDecodeBatched/README.md new file mode 100644 index 0000000000..29483a1474 --- /dev/null +++ b/projects/rocjpeg/samples/jpegDecodeBatched/README.md @@ -0,0 +1,27 @@ +# JPEG decode batched sample + +The jpeg decode bacthed sample illustrates decoding JPEG images by batches of specified size using rocJPEG library to get the individual decoded images in one of the supported output format (i.e., native, yuv, y, rgb, rgb_planar). This sample can be configured with a device ID and optionally able to dump the output to a file. + +## Prerequisites: + +* Install [rocJPEG](../../README.md#build-and-install-instructions) + +## Build + +```shell +mkdir jpeg_decode_batched_sample && cd jpeg_decode_batched_sample +cmake ../ +make -j +``` + +## Run + +```shell +./jpegdecodebatched -i <[input path] - input path to a single JPEG image or a directory containing JPEG images - [required]> + -be <[backend] - select rocJPEG backend (0 for hardware-accelerated JPEG decoding using VCN, + 1 for hybrid JPEG decoding using CPU and GPU HIP kernels (currently not supported)) [optional - default: 0]> + -fmt <[output format] - select rocJPEG output format for decoding, one of the [native, yuv, y, rgb, rgb_planar] [optional - default: native]> + -o <[output path] - path to an output file or a path to a directory - write decoded images to a file or directory based on selected output format [optional]> + -d <[device id] - specify the GPU device id for the desired device (use 0 for the first device, 1 for the second device, and so on) - [optional - default: 0]> + -b <[batch_size] - decode images from input by batches of a specified size - [optional - default: 2]> +``` \ No newline at end of file diff --git a/projects/rocjpeg/samples/jpegDecodeBatched/jpegdecodebatched.cpp b/projects/rocjpeg/samples/jpegDecodeBatched/jpegdecodebatched.cpp new file mode 100644 index 0000000000..dcf6f20ffa --- /dev/null +++ b/projects/rocjpeg/samples/jpegDecodeBatched/jpegdecodebatched.cpp @@ -0,0 +1,196 @@ +/* +Copyright (c) 2024 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 "../rocjpeg_samples_utils.h" + +int main(int argc, char **argv) { + int device_id = 0; + bool save_images = false; + uint8_t num_components; + uint32_t channel_sizes[ROCJPEG_MAX_COMPONENT] = {}; + std::vector> widths; + std::vector> heights; + std::vector> prior_channel_sizes; + uint32_t num_channels = 0; + int total_images = 0; + int batch_size = 2; + double time_per_image_all = 0; + double mpixels_all = 0; + double images_per_sec = 0; + std::string chroma_sub_sampling = ""; + std::string input_path, output_file_path; + std::vector file_paths = {}; + bool is_dir = false; + bool is_file = false; + std::vector> batch_images; + std::vector subsamplings; + RocJpegBackend rocjpeg_backend = ROCJPEG_BACKEND_HARDWARE; + RocJpegHandle rocjpeg_handle = nullptr; + std::vector rocjpeg_stream_handles; + RocJpegImage output_image = {}; + std::vector output_images; + RocJpegDecodeParams decode_params = {}; + RocJpegUtils rocjpeg_utils; + + RocJpegUtils::ParseCommandLine(input_path, output_file_path, save_images, device_id, rocjpeg_backend, decode_params, nullptr, &batch_size, argc, argv); + if (!RocJpegUtils::GetFilePaths(input_path, file_paths, is_dir, is_file)) { + std::cerr << "ERROR: Failed to get input file paths!" << std::endl; + return EXIT_FAILURE; + } + if (!RocJpegUtils::InitHipDevice(device_id)) { + std::cerr << "ERROR: Failed to initialize HIP!" << std::endl; + return EXIT_FAILURE; + } + + CHECK_ROCJPEG(rocJpegCreate(rocjpeg_backend, device_id, &rocjpeg_handle)); + batch_size = std::min(batch_size, static_cast(file_paths.size())); + rocjpeg_stream_handles.resize(batch_size); + // create stream handles of batch size + for(auto i = 0; i < batch_size; i++) { + CHECK_ROCJPEG(rocJpegStreamCreate(&rocjpeg_stream_handles[i])); + } + + batch_images.resize(batch_size); + output_images.resize(batch_size); + prior_channel_sizes.resize(batch_size, std::vector(ROCJPEG_MAX_COMPONENT, 0)); + widths.resize(batch_size, std::vector(ROCJPEG_MAX_COMPONENT, 0)); + heights.resize(batch_size, std::vector(ROCJPEG_MAX_COMPONENT, 0)); + subsamplings.resize(batch_size); + std::vector base_file_names(batch_size); + std::cout << "Decoding started, please wait! ... " << std::endl; + for (int i = 0; i < file_paths.size(); i += batch_size) { + int batch_end = std::min(i + batch_size, static_cast(file_paths.size())); + for (int j = i; j < batch_end; j++) { + int index = j - i; + base_file_names[index] = file_paths[j].substr(file_paths[j].find_last_of("/\\") + 1); + // Read an image from disk. + std::ifstream input(file_paths[j].c_str(), std::ios::in | std::ios::binary | std::ios::ate); + if (!(input.is_open())) { + std::cerr << "ERROR: Cannot open image: " << file_paths[j] << std::endl; + return EXIT_FAILURE; + } + // Get the size + std::streamsize file_size = input.tellg(); + input.seekg(0, std::ios::beg); + // resize if buffer is too small + if (batch_images[index].size() < file_size) { + batch_images[index].resize(file_size); + } + if (!input.read(batch_images[index].data(), file_size)) { + std::cerr << "ERROR: Cannot read from file: " << file_paths[j] << std::endl; + return EXIT_FAILURE; + } + + CHECK_ROCJPEG(rocJpegStreamParse(reinterpret_cast(batch_images[index].data()), file_size, rocjpeg_stream_handles[index])); + CHECK_ROCJPEG(rocJpegGetImageInfo(rocjpeg_handle, rocjpeg_stream_handles[index], &num_components, &subsamplings[index], widths[index].data(), heights[index].data())); + + rocjpeg_utils.GetChromaSubsamplingStr(subsamplings[index], chroma_sub_sampling); + if (subsamplings[index] == ROCJPEG_CSS_411) { + std::cerr << "The chroma sub-sampling is not supported by VCN Hardware" << std::endl; + if (is_dir) { + std::cout << std::endl; + continue; + } else + return EXIT_FAILURE; + } + + if (rocjpeg_utils.GetChannelPitchAndSizes(decode_params.output_format, subsamplings[index], widths[index].data(), heights[index].data(), num_channels, output_images[index], channel_sizes)) { + std::cerr << "ERROR: Failed to get the channel pitch and sizes" << std::endl; + return EXIT_FAILURE; + } + + // allocate memory for each channel and reuse them if the sizes remain unchanged for a new image. + for (int n = 0; n < num_channels; n++) { + if (prior_channel_sizes[index][n] != channel_sizes[n]) { + if (output_images[index].channel[n] != nullptr) { + CHECK_HIP(hipFree((void *)output_images[index].channel[n])); + output_images[index].channel[n] = nullptr; + } + CHECK_HIP(hipMalloc(&output_images[index].channel[n], channel_sizes[n])); + prior_channel_sizes[index][n] = channel_sizes[n]; + } + } + } + int current_batch_size = batch_end - i; + + auto start_time = std::chrono::high_resolution_clock::now(); + CHECK_ROCJPEG(rocJpegDecodeBatched(rocjpeg_handle, rocjpeg_stream_handles.data(), current_batch_size, &decode_params, output_images.data())); + auto end_time = std::chrono::high_resolution_clock::now(); + double time_per_batch_in_milli_sec = std::chrono::duration(end_time - start_time).count(); + + double image_size_in_mpixels = 0; + for (int b = 0; b < current_batch_size; b++) { + image_size_in_mpixels += (static_cast(widths[b][0]) * static_cast(heights[b][0]) / 1000000); + } + + total_images += current_batch_size; + + if (save_images) { + for (int b = 0; b < current_batch_size; b++) { + std::string image_save_path = output_file_path; + if (is_dir) { + rocjpeg_utils.GetOutputFileExt(decode_params.output_format, base_file_names[b], widths[b][0], heights[b][0], image_save_path); + } + rocjpeg_utils.SaveImage(image_save_path, &output_images[b], widths[b][0], heights[b][0], subsamplings[b], decode_params.output_format); + } + } + + if (is_dir) { + time_per_image_all += time_per_batch_in_milli_sec; + mpixels_all += image_size_in_mpixels; + } + + // Clear the batch_images vector after processing each batch + for (int j = i; j < batch_end; j++) { + batch_images[j - i].clear(); + } + } + + if (is_dir) { + time_per_image_all = time_per_image_all / total_images; + images_per_sec = 1000 / time_per_image_all; + double mpixels_per_sec = mpixels_all * images_per_sec / total_images; + std::cout << "Total decoded images: " << total_images << std::endl; + if (total_images) { + std::cout << "Average processing time per image (ms): " << time_per_image_all << std::endl; + std::cout << "Average decoded images per sec (Images/Sec): " << images_per_sec << std::endl; + std::cout << "Average decoded images size (Mpixels/Sec): " << mpixels_per_sec << std::endl; + } + } + + //cleanup + for (auto& it : output_images) { + for (int i = 0; i < ROCJPEG_MAX_COMPONENT; i++) { + if (it.channel[i] != nullptr) { + CHECK_HIP(hipFree((void *)it.channel[i])); + it.channel[i] = nullptr; + } + } + } + CHECK_ROCJPEG(rocJpegDestroy(rocjpeg_handle)); + for(auto& it : rocjpeg_stream_handles) { + CHECK_ROCJPEG(rocJpegStreamDestroy(it)); + } + + std::cout << "Decoding completed!" << std::endl; + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/projects/rocjpeg/samples/jpegDecodeMultiThreads/jpegdecodemultithreads.cpp b/projects/rocjpeg/samples/jpegDecodeMultiThreads/jpegdecodemultithreads.cpp index 445a23ca09..27343b41ef 100644 --- a/projects/rocjpeg/samples/jpegDecodeMultiThreads/jpegdecodemultithreads.cpp +++ b/projects/rocjpeg/samples/jpegDecodeMultiThreads/jpegdecodemultithreads.cpp @@ -132,7 +132,7 @@ int main(int argc, char **argv) { RocJpegUtils rocjpeg_utils; std::vector threads; - RocJpegUtils::ParseCommandLine(input_path, output_file_path, save_images, device_id, rocjpeg_backend, decode_params, &num_threads, argc, argv); + RocJpegUtils::ParseCommandLine(input_path, output_file_path, save_images, device_id, rocjpeg_backend, decode_params, &num_threads, nullptr, argc, argv); if (!RocJpegUtils::GetFilePaths(input_path, file_paths, is_dir, is_file)) { std::cerr << "ERROR: Failed to get input file paths!" << std::endl; return EXIT_FAILURE; diff --git a/projects/rocjpeg/samples/rocjpeg_samples_utils.h b/projects/rocjpeg/samples/rocjpeg_samples_utils.h index b34c5cc7ba..c6ac257e77 100644 --- a/projects/rocjpeg/samples/rocjpeg_samples_utils.h +++ b/projects/rocjpeg/samples/rocjpeg_samples_utils.h @@ -80,24 +80,24 @@ public: * @param argv The command line arguments. */ static void ParseCommandLine(std::string &input_path, std::string &output_file_path, bool &save_images, int &device_id, - RocJpegBackend &rocjpeg_backend, RocJpegDecodeParams &decode_params, int *num_threads, int argc, char *argv[]) { + RocJpegBackend &rocjpeg_backend, RocJpegDecodeParams &decode_params, int *num_threads, int *batch_size, int argc, char *argv[]) { if(argc <= 1) { - ShowHelpAndExit("", num_threads != nullptr); + ShowHelpAndExit("", num_threads != nullptr, batch_size != nullptr); } for (int i = 1; i < argc; i++) { if (!strcmp(argv[i], "-h")) { - ShowHelpAndExit("", num_threads != nullptr); + ShowHelpAndExit("", num_threads != nullptr, batch_size != nullptr); } if (!strcmp(argv[i], "-i")) { if (++i == argc) { - ShowHelpAndExit("-i", num_threads != nullptr); + ShowHelpAndExit("-i", num_threads != nullptr, batch_size != nullptr); } input_path = argv[i]; continue; } if (!strcmp(argv[i], "-o")) { if (++i == argc) { - ShowHelpAndExit("-o", num_threads != nullptr); + ShowHelpAndExit("-o", num_threads != nullptr, batch_size != nullptr); } output_file_path = argv[i]; save_images = true; @@ -105,21 +105,21 @@ public: } if (!strcmp(argv[i], "-d")) { if (++i == argc) { - ShowHelpAndExit("-d", num_threads != nullptr); + ShowHelpAndExit("-d", num_threads != nullptr, batch_size != nullptr); } device_id = atoi(argv[i]); continue; } if (!strcmp(argv[i], "-be")) { if (++i == argc) { - ShowHelpAndExit("-be", num_threads != nullptr); + ShowHelpAndExit("-be", num_threads != nullptr, batch_size != nullptr); } rocjpeg_backend = static_cast(atoi(argv[i])); continue; } if (!strcmp(argv[i], "-fmt")) { if (++i == argc) { - ShowHelpAndExit("-fmt", num_threads != nullptr); + ShowHelpAndExit("-fmt", num_threads != nullptr, batch_size != nullptr); } std::string selected_output_format = argv[i]; if (selected_output_format == "native") { @@ -139,13 +139,21 @@ public: } if (!strcmp(argv[i], "-t")) { if (++i == argc) { - ShowHelpAndExit("-t", num_threads != nullptr); + ShowHelpAndExit("-t", num_threads != nullptr, batch_size != nullptr); } if (num_threads != nullptr) *num_threads = atoi(argv[i]); continue; } - ShowHelpAndExit(argv[i], num_threads != nullptr); + if (!strcmp(argv[i], "-b")) { + if (++i == argc) { + ShowHelpAndExit("-b", num_threads != nullptr, batch_size != nullptr); + } + if (batch_size != nullptr) + *batch_size = atoi(argv[i]); + continue; + } + ShowHelpAndExit(argv[i], num_threads != nullptr, batch_size != nullptr); } } @@ -553,7 +561,7 @@ private: * @param option The option to display in the help message (optional). * @param show_threads Flag indicating whether to show the number of threads in the help message. */ - static void ShowHelpAndExit(const char *option = nullptr, bool show_threads = false) { + static void ShowHelpAndExit(const char *option = nullptr, bool show_threads = false, bool show_batch_size = false) { std::cout << "Options:\n" "-i [input path] - input path to a single JPEG image or a directory containing JPEG images - [required]\n" "-be [backend] - select rocJPEG backend (0 for hardware-accelerated JPEG decoding using VCN,\n" @@ -564,6 +572,9 @@ private: if (show_threads) { std::cout << "-t [threads] - number of threads for parallel JPEG decoding - [optional - default: 2]\n"; } + if (show_batch_size) { + std::cout << "-b [batch_size] - decode images from input by batches of a specified size - [optional - default: 2]\n"; + } exit(0); } }; diff --git a/projects/rocjpeg/src/rocjpeg_api.cpp b/projects/rocjpeg/src/rocjpeg_api.cpp index 07801265ff..aa8914ff1a 100644 --- a/projects/rocjpeg/src/rocjpeg_api.cpp +++ b/projects/rocjpeg/src/rocjpeg_api.cpp @@ -208,6 +208,33 @@ RocJpegStatus ROCJPEGAPI rocJpegDecode(RocJpegHandle handle, RocJpegStreamHandle return rocjpeg_status; } +/** + * @brief Decodes a batch of JPEG images using the rocJPEG library. + * + * Decodes a batch of JPEG images using the specified handle, stream handles, decode parameters, and destination images. + * + * @param handle The handle to the RocJpeg decoder. + * @param jpeg_stream_handles An array of stream handles for the JPEG images to be decoded. + * @param decode_params The decode parameters for the decoding process. + * @param destinations An array of RocJpegImage structures to store the decoded images. + * @return The status of the decoding process. Returns ROCJPEG_STATUS_SUCCESS if successful, or an error code otherwise. + */ +RocJpegStatus ROCJPEGAPI rocJpegDecodeBatched(RocJpegHandle handle, RocJpegStreamHandle *jpeg_stream_handles, int batch_size, const RocJpegDecodeParams *decode_params, RocJpegImage *destinations) { + if (handle == nullptr || jpeg_stream_handles == nullptr|| decode_params == nullptr || destinations == nullptr) { + return ROCJPEG_STATUS_INVALID_PARAMETER; + } + RocJpegStatus rocjpeg_status = ROCJPEG_STATUS_SUCCESS; + auto rocjpeg_handle = static_cast(handle); + try { + rocjpeg_status = rocjpeg_handle->rocjpeg_decoder->DecodeBatched(jpeg_stream_handles, batch_size, decode_params, destinations); + } catch (const std::exception& e) { + rocjpeg_handle->CaptureError(e.what()); + ERR(e.what()); + return ROCJPEG_STATUS_RUNTIME_ERROR; + } + + return rocjpeg_status; +} /** * @brief Returns the error name corresponding to the given RocJpegStatus. * diff --git a/projects/rocjpeg/src/rocjpeg_decoder.cpp b/projects/rocjpeg/src/rocjpeg_decoder.cpp index 0e799b636e..7f58bb8590 100644 --- a/projects/rocjpeg/src/rocjpeg_decoder.cpp +++ b/projects/rocjpeg/src/rocjpeg_decoder.cpp @@ -102,7 +102,7 @@ RocJpegStatus RocJpegDecoder::InitializeDecoder() { * @return The status of the JPEG decoding operation. */ RocJpegStatus RocJpegDecoder::Decode(RocJpegStreamHandle jpeg_stream_handle, const RocJpegDecodeParams *decode_params, RocJpegImage *destination) { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); RocJpegStatus rocjpeg_status = ROCJPEG_STATUS_SUCCESS; if (jpeg_stream_handle == nullptr || decode_params == nullptr || destination == nullptr) { return ROCJPEG_STATUS_INVALID_PARAMETER; @@ -161,6 +161,22 @@ RocJpegStatus RocJpegDecoder::Decode(RocJpegStreamHandle jpeg_stream_handle, con return ROCJPEG_STATUS_SUCCESS; } +/** + * Decodes a batch of JPEG streams using the specified decode parameters and stores the decoded images in the provided destinations. + * + * @param jpeg_streams An array of RocJpegStreamHandle objects representing the JPEG streams to be decoded. + * @param batch_size The number of JPEG streams in the batch. + * @param decode_params A pointer to RocJpegDecodeParams object containing the decode parameters. + * @param destinations An array of RocJpegImage objects where the decoded images will be stored. + * @return A RocJpegStatus value indicating the success or failure of the decoding operation. + */ +RocJpegStatus RocJpegDecoder::DecodeBatched(RocJpegStreamHandle *jpeg_streams, int batch_size, const RocJpegDecodeParams *decode_params, RocJpegImage *destinations) { + std::lock_guard lock(mutex_); + for(int i = 0; i < batch_size; i++) { + CHECK_ROCJPEG(Decode(jpeg_streams[i], decode_params, &destinations[i])); + } + return ROCJPEG_STATUS_SUCCESS; +} /** * @brief Retrieves the image information from the JPEG stream. * @@ -176,7 +192,7 @@ RocJpegStatus RocJpegDecoder::Decode(RocJpegStreamHandle jpeg_stream_handle, con * or ROCJPEG_STATUS_INVALID_PARAMETER if any of the input parameters are invalid. */ RocJpegStatus RocJpegDecoder::GetImageInfo(RocJpegStreamHandle jpeg_stream_handle, uint8_t *num_components, RocJpegChromaSubsampling *subsampling, uint32_t *widths, uint32_t *heights){ - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); if (jpeg_stream_handle == nullptr || num_components == nullptr || subsampling == nullptr || widths == nullptr || heights == nullptr) { return ROCJPEG_STATUS_INVALID_PARAMETER; } diff --git a/projects/rocjpeg/src/rocjpeg_decoder.h b/projects/rocjpeg/src/rocjpeg_decoder.h index 86048d496c..0c7757ede4 100644 --- a/projects/rocjpeg/src/rocjpeg_decoder.h +++ b/projects/rocjpeg/src/rocjpeg_decoder.h @@ -84,6 +84,21 @@ public: */ RocJpegStatus Decode(RocJpegStreamHandle jpeg_stream, const RocJpegDecodeParams *decode_params, RocJpegImage *destination); + /** + * Decodes a batch of JPEG streams. + * + * This function decodes a batch of JPEG streams specified by `jpeg_streams` into a batch of destination images specified by `destinations`. + * The number of JPEG streams in the batch is specified by `batch_size`. + * The decoding parameters are specified by `decode_params`. + * + * @param jpeg_streams The array of JPEG stream handles. + * @param batch_size The number of JPEG streams in the batch. + * @param decode_params The decoding parameters. + * @param destinations The array of destination images. + * @return The status of the decoding operation. + */ + RocJpegStatus DecodeBatched(RocJpegStreamHandle *jpeg_streams, int batch_size, const RocJpegDecodeParams *decode_params, RocJpegImage *destinations); + private: /** * @brief Initializes the HIP framework. @@ -156,7 +171,7 @@ private: int device_id_; // ID of the device to be used hipDeviceProp_t hip_dev_prop_; // HIP device properties hipStream_t hip_stream_; // HIP stream - std::mutex mutex_; // Mutex for thread safety + std::recursive_mutex mutex_; // Mutex for thread safety RocJpegBackend backend_; // RocJpeg backend RocJpegVappiDecoder jpeg_vaapi_decoder_; // RocJpeg VAAPI decoder object }; diff --git a/projects/rocjpeg/test/CMakeLists.txt b/projects/rocjpeg/test/CMakeLists.txt index 8510ba7e1a..076af05992 100644 --- a/projects/rocjpeg/test/CMakeLists.txt +++ b/projects/rocjpeg/test/CMakeLists.txt @@ -120,4 +120,16 @@ add_test( --build-generator "${CMAKE_GENERATOR}" --test-command "jpegdecodemultithreads" -i ${ROCM_PATH}/share/rocjpeg/images/ -t 2 +) + +add_test( + NAME + jpeg-decode-batch-fmt-native + COMMAND + "${CMAKE_CTEST_COMMAND}" + --build-and-test "${ROCM_PATH}/share/rocjpeg/samples/jpegDecodeBatched" + "${CMAKE_CURRENT_BINARY_DIR}/jpegDecodeBatched" + --build-generator "${CMAKE_GENERATOR}" + --test-command "jpegdecodebatched" + -i ${ROCM_PATH}/share/rocjpeg/images/ ) \ No newline at end of file