From bd6bb91c7279819ad4136001b5e5a138dffa5452 Mon Sep 17 00:00:00 2001 From: Evan Flynn Date: Wed, 30 Aug 2023 19:19:59 -0700 Subject: [PATCH] Allocate unique avpacket for each frame Signed-off-by: Evan Flynn --- include/usb_cam/formats/mjpeg.hpp | 24 ++++++------------------ src/usb_cam_node.cpp | 5 +---- 2 files changed, 7 insertions(+), 22 deletions(-) diff --git a/include/usb_cam/formats/mjpeg.hpp b/include/usb_cam/formats/mjpeg.hpp index 48ae3ea0..86c0308a 100644 --- a/include/usb_cam/formats/mjpeg.hpp +++ b/include/usb_cam/formats/mjpeg.hpp @@ -75,7 +75,6 @@ class MJPEG2RGB : public pixel_format_base m_avframe_device(av_frame_alloc()), m_avframe_rgb(av_frame_alloc()), m_avoptions(NULL), - m_avpacket(av_packet_alloc()), m_averror_str(reinterpret_cast(malloc(AV_ERROR_MAX_STRING_SIZE))) { if (!m_avcodec) { @@ -85,9 +84,6 @@ class MJPEG2RGB : public pixel_format_base if (!m_avparser) { throw std::runtime_error("Could not find MJPEG parser"); } - if (!m_avpacket) { - throw std::runtime_error("Could not allocate AVPacket"); - } m_avcodec_context = avcodec_alloc_context3(m_avcodec); @@ -151,9 +147,6 @@ class MJPEG2RGB : public pixel_format_base if (m_avoptions) { free(m_avoptions); } - if (m_avpacket) { - free(m_avpacket); - } if (m_avcodec_context) { avcodec_close(m_avcodec_context); avcodec_free_context(&m_avcodec_context); @@ -179,18 +172,14 @@ class MJPEG2RGB : public pixel_format_base // clear the picture memset(dest, 0, m_avframe_device_size); - #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100) - // deprecated: https://github.com/FFmpeg/FFmpeg/commit/f7db77bd8785d1715d3e7ed7e69bd1cc991f2d07 - av_init_packet(m_avpacket); - #endif - - av_packet_from_data( - m_avpacket, - const_cast(reinterpret_cast(src)), - bytes_used); + auto avpacket = av_packet_alloc(); + av_new_packet(avpacket, bytes_used); + memcpy(avpacket->data, src, bytes_used); // Pass src MJPEG image to decoder - m_result = avcodec_send_packet(m_avcodec_context, m_avpacket); + m_result = avcodec_send_packet(m_avcodec_context, avpacket); + + av_packet_free(&avpacket); // If result is not 0, report what went wrong if (m_result != 0) { @@ -234,7 +223,6 @@ class MJPEG2RGB : public pixel_format_base AVFrame * m_avframe_device; AVFrame * m_avframe_rgb; AVDictionary * m_avoptions; - AVPacket * m_avpacket; SwsContext * m_sws_context; size_t m_avframe_device_size; size_t m_avframe_rgb_size; diff --git a/src/usb_cam_node.cpp b/src/usb_cam_node.cpp index e46064b0..585888d3 100644 --- a/src/usb_cam_node.cpp +++ b/src/usb_cam_node.cpp @@ -362,13 +362,10 @@ void UsbCamNode::update() { if (m_camera->is_capturing()) { // If the camera exposure longer higher than the framerate period - // then that caps the framerate. - // auto t0 = now(); + // then that caps the framerate if (!take_and_send_image()) { RCLCPP_WARN_ONCE(this->get_logger(), "USB camera did not respond in time."); } - // auto diff = now() - t0; - // INFO(diff.nanoseconds() / 1e6 << " " << int(t0.nanoseconds() / 1e9)); } } } // namespace usb_cam