Skip to content

Commit bd6bb91

Browse files
committed
Allocate unique avpacket for each frame
Signed-off-by: Evan Flynn <[email protected]>
1 parent da715c9 commit bd6bb91

File tree

2 files changed

+7
-22
lines changed

2 files changed

+7
-22
lines changed

include/usb_cam/formats/mjpeg.hpp

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ class MJPEG2RGB : public pixel_format_base
7575
m_avframe_device(av_frame_alloc()),
7676
m_avframe_rgb(av_frame_alloc()),
7777
m_avoptions(NULL),
78-
m_avpacket(av_packet_alloc()),
7978
m_averror_str(reinterpret_cast<char *>(malloc(AV_ERROR_MAX_STRING_SIZE)))
8079
{
8180
if (!m_avcodec) {
@@ -85,9 +84,6 @@ class MJPEG2RGB : public pixel_format_base
8584
if (!m_avparser) {
8685
throw std::runtime_error("Could not find MJPEG parser");
8786
}
88-
if (!m_avpacket) {
89-
throw std::runtime_error("Could not allocate AVPacket");
90-
}
9187

9288
m_avcodec_context = avcodec_alloc_context3(m_avcodec);
9389

@@ -151,9 +147,6 @@ class MJPEG2RGB : public pixel_format_base
151147
if (m_avoptions) {
152148
free(m_avoptions);
153149
}
154-
if (m_avpacket) {
155-
free(m_avpacket);
156-
}
157150
if (m_avcodec_context) {
158151
avcodec_close(m_avcodec_context);
159152
avcodec_free_context(&m_avcodec_context);
@@ -179,18 +172,14 @@ class MJPEG2RGB : public pixel_format_base
179172
// clear the picture
180173
memset(dest, 0, m_avframe_device_size);
181174

182-
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
183-
// deprecated: https://github.com/FFmpeg/FFmpeg/commit/f7db77bd8785d1715d3e7ed7e69bd1cc991f2d07
184-
av_init_packet(m_avpacket);
185-
#endif
186-
187-
av_packet_from_data(
188-
m_avpacket,
189-
const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(src)),
190-
bytes_used);
175+
auto avpacket = av_packet_alloc();
176+
av_new_packet(avpacket, bytes_used);
177+
memcpy(avpacket->data, src, bytes_used);
191178

192179
// Pass src MJPEG image to decoder
193-
m_result = avcodec_send_packet(m_avcodec_context, m_avpacket);
180+
m_result = avcodec_send_packet(m_avcodec_context, avpacket);
181+
182+
av_packet_free(&avpacket);
194183

195184
// If result is not 0, report what went wrong
196185
if (m_result != 0) {
@@ -234,7 +223,6 @@ class MJPEG2RGB : public pixel_format_base
234223
AVFrame * m_avframe_device;
235224
AVFrame * m_avframe_rgb;
236225
AVDictionary * m_avoptions;
237-
AVPacket * m_avpacket;
238226
SwsContext * m_sws_context;
239227
size_t m_avframe_device_size;
240228
size_t m_avframe_rgb_size;

src/usb_cam_node.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,10 @@ void UsbCamNode::update()
362362
{
363363
if (m_camera->is_capturing()) {
364364
// If the camera exposure longer higher than the framerate period
365-
// then that caps the framerate.
366-
// auto t0 = now();
365+
// then that caps the framerate
367366
if (!take_and_send_image()) {
368367
RCLCPP_WARN_ONCE(this->get_logger(), "USB camera did not respond in time.");
369368
}
370-
// auto diff = now() - t0;
371-
// INFO(diff.nanoseconds() / 1e6 << " " << int(t0.nanoseconds() / 1e9));
372369
}
373370
}
374371
} // namespace usb_cam

0 commit comments

Comments
 (0)