Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added SVTAV1 image transport plugin #161

Draft
wants to merge 6 commits into
base: rolling
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
69 changes: 69 additions & 0 deletions svtav1_image_transport/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
cmake_minimum_required(VERSION 3.16)

project(svtav1_image_transport)

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")

find_package(ament_cmake REQUIRED)
find_package(image_transport REQUIRED)
find_package(pluginlib REQUIRED)
find_package(rclcpp REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(cv_bridge REQUIRED)
find_package(OpenCV REQUIRED COMPONENTS imgcodecs imgproc)
find_package(SVTENC REQUIRED)
find_package(SVTDEC REQUIRED)

include_directories(
include
${SVTENC_INCLUDE_DIR}
${SVTDEC_INCLUDE_DIR}
)

add_library(
${PROJECT_NAME} SHARED
src/svtav1_publisher.cpp
src/svtav1_subscriber.cpp
src/manifest.cpp
)

ament_target_dependencies(${PROJECT_NAME}
"cv_bridge"
"image_transport"
"rclcpp"
"pluginlib"
"sensor_msgs"
)

target_link_libraries(${PROJECT_NAME}
${SVTDEC_LIBRARY}
${SVTENC_LIBRARY}
)

install(TARGETS ${PROJECT_NAME}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)

install(
DIRECTORY "include/"
DESTINATION include
)
pluginlib_export_plugin_description_file(image_transport svtav1_plugins.xml)

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
endif()

ament_package()
42 changes: 42 additions & 0 deletions svtav1_image_transport/cmake/Modules/FindSVTDEC.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2019 Open Source Robotics Foundation, 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 QUIET)
if(PKG_CONFIG_FOUND)
pkg_check_modules(_SVT SvtAv1Dec)
endif()

find_path(SVTDEC_INCLUDE_DIR
NAMES svt-av1/EbSvtAv1Dec.h
PATHS ${_SVTDEC_INCLUDEDIR}
)
message(_SVTDEC_INCLUDEDIR ${_SVTDEC_INCLUDEDIR})

find_library(SVTDEC_LIBRARY
NAMES SvtAv1Dec
PATHS ${_SVTDEC_LIBDIR})

if(SVTDEC_LIBRARY)
set(SVTDEC_LIBRARIES
${SVTDEC_LIBRARIES}
${SVTDEC_LIBRARY})
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(SVTDEC
FOUND_VAR SVTDEC_FOUND
REQUIRED_VARS SVTDEC_LIBRARY SVTDEC_LIBRARIES SVTDEC_INCLUDE_DIR
VERSION_VAR _SVTDEC_VERSION)

mark_as_advanced(SVTDEC_INCLUDE_DIR SVTDEC_LIBRARY SVTDEC_LIBRARIES)
42 changes: 42 additions & 0 deletions svtav1_image_transport/cmake/Modules/FindSVTENC.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2019 Open Source Robotics Foundation, 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 QUIET)
if(PKG_CONFIG_FOUND)
pkg_check_modules(_SVT SvtAv1Enc)
endif()

find_path(SVTENC_INCLUDE_DIR
NAMES svt-av1/EbSvtAv1Enc.h
PATHS ${_SVTENC_INCLUDEDIR}
)
message(_SVTENC_INCLUDEDIR ${_SVTENC_INCLUDEDIR})

find_library(SVTENC_LIBRARY
NAMES SvtAv1Enc
PATHS ${_SVTENC_LIBDIR})

if(SVTENC_LIBRARY)
set(SVTENC_LIBRARIES
${SVTENC_LIBRARIES}
${SVTENC_LIBRARY})
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(SVTENC
FOUND_VAR SVTENC_FOUND
REQUIRED_VARS SVTENC_LIBRARY SVTENC_LIBRARIES SVTENC_INCLUDE_DIR
VERSION_VAR _SVTENC_VERSION)

mark_as_advanced(SVTENC_INCLUDE_DIR SVTENC_LIBRARY SVTENC_LIBRARIES)
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) 2023, Open Source Robotics Foundation, Inc.
// All rights reserved.
//
// Software License Agreement (BSD License 2.0)
//
// 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 the copyright holder 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 AND CONTRIBUTORS
// "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.

#ifndef SVTAV1_IMAGE_TRANSPORT__SVTAV1_COMMON_HPP_
#define SVTAV1_IMAGE_TRANSPORT__SVTAV1_COMMON_HPP_

#include <rclcpp/parameter_value.hpp>
#include <rcl_interfaces/msg/parameter_descriptor.hpp>

namespace svtav1_image_transport
{
using ParameterDescriptor = rcl_interfaces::msg::ParameterDescriptor;
using ParameterValue = rclcpp::ParameterValue;

struct ParameterDefinition
{
const ParameterValue defaultValue;
const ParameterDescriptor descriptor;
};
} // namespace svtav1_image_transport

#endif // SVTAV1_IMAGE_TRANSPORT__SVTAV1_COMMON_HPP_
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright (c) 2023, Open Source Robotics Foundation, Inc.
// All rights reserved.
//
// Software License Agreement (BSD License 2.0)
//
// 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 the copyright holder 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 AND CONTRIBUTORS
// "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.

#ifndef SVTAV1_IMAGE_TRANSPORT__SVTAV1_PUBLISHER_HPP_
#define SVTAV1_IMAGE_TRANSPORT__SVTAV1_PUBLISHER_HPP_

#include <svt-av1/EbSvtAv1Enc.h>

#include <cstdint>
#include <memory>
#include <string>
#include <vector>

#include <sensor_msgs/msg/image.hpp>
#include <sensor_msgs/msg/compressed_image.hpp>
#include <image_transport/simple_publisher_plugin.hpp>

#include <rclcpp/node.hpp>

#include "svtav1_image_transport/svtav1_common.hpp"

#include <opencv2/core/mat.hpp>

namespace svtav1_image_transport
{
using CompressedImage = sensor_msgs::msg::CompressedImage;
using ParameterEvent = rcl_interfaces::msg::ParameterEvent;

class SVTAV1Publisher : public image_transport::SimplePublisherPlugin<CompressedImage>
{
public:
SVTAV1Publisher();
~SVTAV1Publisher() override;
std::string getTransportName() const override;

protected:
// Overridden to set up reconfigure server
void advertiseImpl(
rclcpp::Node * node,
const std::string & base_topic,
rmw_qos_profile_t custom_qos,
rclcpp::PublisherOptions options) override;

void publish(
const sensor_msgs::msg::Image & message,
const PublishFn & publish_fn) const override;

void Initialize(int width, int height) const;

rclcpp::Logger logger_;
rclcpp::Node * node_;

private:
void set_default_svt_configuration(int width, int height) const;

std::vector<std::string> parameters_;

void declareParameter(
const std::string & base_name,
const ParameterDefinition & definition);

/* SVT-AV1 Encoder Handle */
mutable EbComponentType * svt_encoder{nullptr};
/* SVT-AV1 configuration */
mutable EbSvtAv1EncConfiguration * svt_config{nullptr};
mutable uint64_t frame_count;
// int dts_offset;
mutable EbBufferHeaderType * input_buf{nullptr};

mutable std::mutex mutex;
mutable cv::Mat ycrcb_planes[3];
};

} // namespace svtav1_image_transport

#endif // SVTAV1_IMAGE_TRANSPORT__SVTAV1_PUBLISHER_HPP_
Loading