Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
dd3b976
feat: add video compression support in accelerated_image_processor_co…
manato Feb 5, 2026
2f006f8
feat: add video compression support in accelerated_image_processor_ros
manato Feb 9, 2026
8510074
fix: explicitly initialize the member variables
manato Feb 10, 2026
3adb2b8
feat: add/update tests for jetson_video_compressor
manato Feb 12, 2026
ea526c6
feat: update accelerated_image_processor_ros/test to include video co…
manato Feb 12, 2026
9940d00
feat: introduce variable rate control to reduce payload size
manato Feb 12, 2026
c524026
fix: treat "build/include_what_you_use" and "readabilitycasting" indi…
manato Feb 12, 2026
6af1339
chore: add some comments
manato Feb 12, 2026
00dcc7e
docs: add video encoder entries to README
manato Feb 13, 2026
1781576
refactor: remove `VIDEO_` prefix from `CompressionType` enum
manato Feb 16, 2026
76cb4cb
fix: define `ExpectedVideoBackend` for non-Jetson platform
manato Feb 16, 2026
43dda61
refactor: move constant maps as class member
manato Feb 16, 2026
973151f
fix: solve "include-what-you-use" error suggested by pre-commit
manato Feb 16, 2026
f9404a5
fix: correct typos
manato Feb 16, 2026
31501f9
fix: solve "include-what-you-use" error
manato Feb 16, 2026
94ef051
fix: treat `is_bigendian` flag as a non optional field
manato Feb 17, 2026
ed29100
refactor: introduce an abstract class named `Compressor`
manato Feb 17, 2026
b19dae8
feat: validate parameter combination
manato Feb 20, 2026
2f03cd6
fix: correct correspondence between compression type and plane format…
manato Feb 20, 2026
d494cc1
fix: skip `is_ready` check at the beginning of `process` since the ch…
manato Feb 20, 2026
3e5b27a
fix: eliminate "Unsupported frameRate" warning on H265 test
manato Feb 20, 2026
c299b76
refactor: rename post_process to postprocess for naming consistency
manato Feb 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <array>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <string>
#include <vector>

Expand All @@ -30,7 +31,7 @@ enum class ImageEncoding : uint8_t { RGB, BGR };
/**
* @brief Enumeration of image compression formats.
*/
enum class ImageFormat : uint8_t { RAW, JPEG, PNG };
enum class ImageFormat : uint8_t { RAW, JPEG, PNG, H264, H265, AV1 };

/**
* @brief Structure representing an image.
Expand All @@ -41,10 +42,17 @@ struct Image
int64_t timestamp; //!< Timestamp at the image is captured
uint32_t height; //!< Image height, that is, number of rows
uint32_t width; //!< Image width, that is, number of columns
uint32_t step; //!< Full row length in bytes
ImageEncoding encoding; //!< Image color encoding
ImageFormat format; //!< Image compression format
std::vector<uint8_t> data; //!< Actual matrix data
bool is_bigendian; //!< true if machine stores in big endian format

// Image / CompressedImage dedicated fields
uint32_t step; //!< Full row length in bytes
ImageEncoding encoding; //!< Image color encoding

// Video frame dedicated fields
std::optional<uint64_t> pts; //!< packet time stamp
std::optional<uint8_t> flags; //!< flag representing whether this is the key frame

/**
* @brief Check the specified image is valid.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <cstdio>
#include <iostream>
#include <string>

namespace accelerated_image_processor::common
{
Expand Down Expand Up @@ -67,4 +68,23 @@ namespace accelerated_image_processor::common
exit(1); \
} \
}

/**
* @brief Macro to check for errors and print a message if the VPI status is not
* VPI_SUCCESS
*
* @param status The VPI status.
*/
#define CHECK_VPI(call) \
{ \
VPIStatus _e = (call); \
if (_e != VPI_SUCCESS) { \
char msg_buf[VPI_MAX_STATUS_MESSAGE_LENGTH]; \
vpiGetLastStatusMessage(msg_buf, VPI_MAX_STATUS_MESSAGE_LENGTH); \
std::cerr << "VPI failure: \'#" << _e << "\' at " << __FILE__ << ":" << __LINE__ \
<< ", (reason: " << std::string(msg_buf) << ")" << std::endl; \
exit(1); \
} \
}

} // namespace accelerated_image_processor::common
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class BaseProcessor
* @param image The input image.
* @return The processed image if the process is successful, otherwise std::nullopt.
*/
std::optional<common::Image> process(const Image & image)
virtual std::optional<common::Image> process(const Image & image)
{
if (!is_ready()) {
// TODO(ktro2828): Update to return a type that describes if the process success or not
Expand All @@ -113,6 +113,17 @@ class BaseProcessor
return std::nullopt;
}

postprocess(processed);

return processed;
};

/**
* @brief Execute post process
* @param processed The image to be post-processed
*/
void postprocess(Image & processed)
{
std::visit(
[&processed](auto & f) {
using T = std::decay_t<decltype(f)>;
Expand All @@ -125,9 +136,7 @@ class BaseProcessor
}
},
storage_);

return processed;
};
}

/**
* @brief Check the processor is ready to run processing.
Expand Down
53 changes: 51 additions & 2 deletions src/accelerated_image_processor_compression/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ if(NOT JETSON_FOUND
message(FATAL_ERROR "No JPEG encoder found")
endif()

# --- CUDA VPI available? ---
find_package(VPI)
if(${VPI_FOUND})
message("VPI found")
endif()

# --- Jetson multimedia API available? ---
find_package(NVMMAPI)
if(${NVMMAPI_FOUND})
message("Jetson multimedia API found")
endif()

add_library(${PROJECT_NAME} SHARED src/builder.cpp src/jpeg_compressor/cpu.cpp
src/jpeg_compressor/nvjpeg.cpp)

Expand Down Expand Up @@ -76,12 +88,22 @@ if(JETSON_FOUND)
PRIVATE /usr/src/jetson_multimedia_api/include)
add_library(
${PROJECT_NAME}_jetson SHARED
# JPEG compression
src/jpeg_compressor/jetson.cpp
/usr/src/jetson_multimedia_api/samples/common/classes/NvBuffer.cpp
/usr/src/jetson_multimedia_api/samples/common/classes/NvElement.cpp
/usr/src/jetson_multimedia_api/samples/common/classes/NvElementProfiler.cpp
/usr/src/jetson_multimedia_api/samples/common/classes/NvJpegEncoder.cpp
/usr/src/jetson_multimedia_api/samples/common/classes/NvLogging.cpp)
/usr/src/jetson_multimedia_api/samples/common/classes/NvLogging.cpp
# video compression
src/video_compressor/jetson.cpp
src/video_compressor/jetson_h264.cpp
src/video_compressor/jetson_h265.cpp
src/video_compressor/jetson_av1.cpp
/usr/src/jetson_multimedia_api/samples/common/classes/NvVideoEncoder.cpp
/usr/src/jetson_multimedia_api/samples/common/classes/NvV4l2Element.cpp
/usr/src/jetson_multimedia_api/samples/common/classes/NvV4l2ElementPlane.cpp
/usr/src/jetson_multimedia_api/samples/common/classes/NvBufSurface.cpp)
target_compile_definitions(${PROJECT_NAME}_jetson PRIVATE JETSON_AVAILABLE)

ament_target_dependencies(${PROJECT_NAME}_jetson
Expand All @@ -100,12 +122,14 @@ if(JETSON_FOUND)
target_link_libraries(
${PROJECT_NAME}_jetson
${JETSON_NVJPEG_LIB}
${NVMMAPI_LIBRARIES}
$<$<TARGET_EXISTS:CUDA::cudart>:CUDA::cudart>
$<$<TARGET_EXISTS:CUDA::nppc>:CUDA::nppc>
$<$<TARGET_EXISTS:CUDA::nppicc>:CUDA::nppicc>
$<$<TARGET_EXISTS:CUDA::nppidei>:CUDA::nppidei>
$<$<TARGET_EXISTS:CUDA::nppig>:CUDA::nppig>
$<$<TARGET_EXISTS:CUDA::nppisu>:CUDA::nppisu>)
$<$<TARGET_EXISTS:CUDA::nppisu>:CUDA::nppisu>
$<$<TARGET_EXISTS:vpi>:vpi>)

set(JETSON_LIB_DIRS
"/usr/lib/aarch64-linux-gnu/nvidia;/usr/lib/aarch64-linux-gnu/tegra")
Expand All @@ -132,11 +156,36 @@ install(DIRECTORY include/${PROJECT_NAME} DESTINATION include)
if(BUILD_TESTING)
set(test_files test/builder.cpp test/cpu_jpeg_compressor.cpp
test/jetson_jpeg_compressor.cpp test/nv_jpeg_compressor.cpp)

foreach(test_file IN LISTS test_files)
get_filename_component(test_file_name ${test_file} NAME)
ament_auto_add_gtest(${test_file_name}_${PROJECT_NAME} ${test_file})
target_link_libraries(${test_file_name}_${PROJECT_NAME} ${PROJECT_NAME})
endforeach()

# Treat some tests separately because they contains many test cases and cause
# colcon test timeout if the tests are generated in the above way
set(long_test_files
test/jetson_video_compressor_h264.cpp
test/jetson_video_compressor_h265.cpp
test/jetson_video_compressor_av1.cpp)

find_package(GTest)
enable_testing()
include(GoogleTest) # enable the GoogleTest integration

foreach(test_file IN LISTS long_test_files)
get_filename_component(test_file_name ${test_file} NAME)

set(LONG_TEST_EXEC ${test_file_name}_${PROJECT_NAME})
add_executable(${LONG_TEST_EXEC} ${test_file})
target_include_directories(${LONG_TEST_EXEC}
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(${LONG_TEST_EXEC} ${PROJECT_NAME} GTest::GTest
GTest::Main)
# divide a single test into separated cases
gtest_discover_tests(${LONG_TEST_EXEC})
endforeach()
endif()

ament_export_include_directories(include)
Expand Down
13 changes: 8 additions & 5 deletions src/accelerated_image_processor_compression/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ It also includes support for hardware acceleration on NVIDIA Jetson devices usin

## Compressor Supports

| Compressor | Format | Backend | Device |
| ---------------------- | ------ | ----------------------------------------------------------------------------------- | ------ |
| `JetsonJPEGCompressor` | `JPEG` | [jetsonJPEG](https://docs.nvidia.com/jetson/l4t-multimedia/classNvJPEGEncoder.html) | Jetson |
| `NvJPEGCompressor` | `JPEG` | [nvJPEG](https://developer.nvidia.com/nvjpeg) | GPU |
| `CpuJPEGCompressor` | `JPEG` | [TurboJPEG](https://github.com/libjpeg-turbo/libjpeg-turbo) | CPU |
| Compressor | Format | Backend | Device |
| ---------------------- | ------ | ---------------------------------------------------------------------------------------- | ------ |
| `JetsonJPEGCompressor` | `JPEG` | [jetsonJPEG](https://docs.nvidia.com/jetson/l4t-multimedia/classNvJPEGEncoder.html) | Jetson |
| `NvJPEGCompressor` | `JPEG` | [nvJPEG](https://developer.nvidia.com/nvjpeg) | GPU |
| `CpuJPEGCompressor` | `JPEG` | [TurboJPEG](https://github.com/libjpeg-turbo/libjpeg-turbo) | CPU |
| `JetsonH264Compressor` | `H264` | [NvVideoEncoder](https://docs.nvidia.com/jetson/l4t-multimedia/classNvVideoEncoder.html) | Jetson |
| `JetsonH265Compressor` | `H265` | [NvVideoEncoder](https://docs.nvidia.com/jetson/l4t-multimedia/classNvVideoEncoder.html) | Jetson |
| `JetsonAV1Compressor` | `AV1` | [NvVideoEncoder](https://docs.nvidia.com/jetson/l4t-multimedia/classNvVideoEncoder.html) | Jetson |

## Example Usage in ROS 2

Expand Down
109 changes: 109 additions & 0 deletions src/accelerated_image_processor_compression/cmake/FindNVMMAPI.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# ##############################################################################
#
# Original Copyright
#
# ##############################################################################
# Copyright (c) 2017-2022, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# - Try to find the NVIDIA Tegra Multimedia API
# Once done this will define
# NVMMAPI_FOUND - System has NVMMAPI
# NVMMAPI_INCLUDE_DIRS - The NVMMAPI include directories
# NVMMAPI_LIBRARIES - The libraries needed to use the NVMMAPI
# NVMMAPI_DEFINITIONS - Compiler switches required for using NVMMAPI
# ##############################################################################
#
# Modifications Copyright
#
# ##############################################################################
# Copyright 2026 TIER IV, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
# ##############################################################################

find_package(PkgConfig)

find_path(NVMMAPI_INCLUDE_DIR NvVideoEncoder.h
HINTS ${SYS_ROOT}/usr/src/jetson_multimedia_api/include)

find_library(
NVMMAPI_NVV4L2_LIBRARY
NAMES nvv4l2
HINTS /usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}/tegra)
find_library(
NVMMAPI_NVMEDIA_LIBRARY
NAMES nvmedia
HINTS /usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}/tegra)
find_library(
NVMMAPI_V4L2_NVVIDEOCODEC_LIBRARY
NAMES v4l2_nvvideocodec
HINTS /usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}/tegra)
find_library(
NVMMAPI_NVBUFSURFACE_LIBRARY
NAMES nvbufsurface
HINTS /usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}/tegra)
find_library(
NVMMAPI_NVBUFSURF_TRANSFORM_LIBRARY
NAMES nvbufsurftransform
HINTS /usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}/tegra)

set(NVMMAPI_LIBRARIES
${NVMMAPI_NVV4L2_LIBRARY} ${NVMMAPI_NVMEDIA_LIBRARY}
${NVMMAPI_V4L2_NVVIDEOCODEC_LIBRARY} ${NVMMAPI_NVBUFSURFACE_LIBRARY}
${NVMMAPI_NVBUFSURF_TRANSFORM_LIBRARY})

set(NVMMAPI_INCLUDE_DIRS ${NVMMAPI_INCLUDE_DIR})
set(NVMMAPI_DEFINITIONS -DNVMMAPI_SUPPORTED)

include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set ARGUS_FOUND to TRUE if all
# listed variables are TRUE
find_package_handle_standard_args(
NVMMAPI
DEFAULT_MSG
NVMMAPI_INCLUDE_DIR
NVMMAPI_NVV4L2_LIBRARY
NVMMAPI_NVMEDIA_LIBRARY
NVMMAPI_V4L2_NVVIDEOCODEC_LIBRARY
NVMMAPI_NVBUFSURFACE_LIBRARY
NVMMAPI_NVBUFSURF_TRANSFORM_LIBRARY)

# mark_as_advanced(NVMMAPI_INCLUDE_DIR NVMMAPI_LIBRARY)
mark_as_advanced(
NVMMAPI_INCLUDE_DIR NVMMAPI_NVV4L2_LIBRARY NVMMAPI_NVMEDIA_LIBRARY
NVMMAPI_V4L2_NVVIDEOCODEC_LIBRARY NVMMAPI_NVBUFSURFACE_LIBRARY
NVMMAPI_NVBUFSURF_TRANSFORM_LIBRARY)
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,14 @@
#pragma once

#include <accelerated_image_processor_common/processor.hpp>
#include <accelerated_image_processor_compression/compressor.hpp>

#include <memory>
#include <string>
#include <type_traits>

namespace accelerated_image_processor::compression
{
/**
* @brief Type alias for compressor processor.
* @note This might be a specific implementation of a compressor processor in the future.
*/
using Compressor = common::BaseProcessor;

/**
* @brief Compression type enum
*/
enum class CompressionType : uint8_t { JPEG, VIDEO };

/**
* @brief Convert a string to a compression type
* @param str String to convert expected strings ["JPEG", "VIDEO"]
Expand Down Expand Up @@ -85,7 +75,7 @@ template <
inline std::unique_ptr<Compressor> create_compressor(const std::string & type, F fn)
{
return create_compressor(to_compression_type(type), fn);
};
}

/**
* @brief Create a compressor processor with a member function for the postprocess.
Expand Down
Loading