Skip to content

Commit

Permalink
Allocate unique avpacket for each frame
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Flynn <evanflynn.msu@gmail.com>
  • Loading branch information
flynneva committed Aug 31, 2023
1 parent da715c9 commit bd6bb91
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 22 deletions.
24 changes: 6 additions & 18 deletions include/usb_cam/formats/mjpeg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char *>(malloc(AV_ERROR_MAX_STRING_SIZE)))
{
if (!m_avcodec) {
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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<uint8_t *>(reinterpret_cast<const uint8_t *>(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) {
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 1 addition & 4 deletions src/usb_cam_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bd6bb91

Please sign in to comment.