Skip to content

Commit

Permalink
Merge pull request #279 from flynneva/fix-mjpeg-invalid-ptr
Browse files Browse the repository at this point in the history
Fix mjpeg invalid ptr and mjpeg memory leak
  • Loading branch information
flynneva authored Aug 31, 2023
2 parents 7115c62 + bd6bb91 commit ec67cb2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ ros2 run usb_cam usb_cam_node_exe --ros-args --params-file /path/to/colcon_ws/sr
# launch the usb_cam executable that loads parameters from the same `usb_cam/config/params.yaml` file as above
# along with an additional image viewer node
ros2 launch usb_cam demo_launch.py
ros2 launch usb_cam camera.launch.py
```
## Launching Multiple usb_cam's

Expand Down
34 changes: 14 additions & 20 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 @@ -145,6 +141,12 @@ class MJPEG2RGB : public pixel_format_base

~MJPEG2RGB()
{
if (m_averror_str) {
free(m_averror_str);
}
if (m_avoptions) {
free(m_avoptions);
}
if (m_avcodec_context) {
avcodec_close(m_avcodec_context);
avcodec_free_context(&m_avcodec_context);
Expand All @@ -155,10 +157,6 @@ class MJPEG2RGB : public pixel_format_base
if (m_avframe_rgb) {
av_frame_free(&m_avframe_rgb);
}
if (m_avpacket) {
av_packet_unref(m_avpacket);
av_packet_free(&m_avpacket);
}
if (m_avparser) {
av_parser_close(m_avparser);
}
Expand All @@ -174,24 +172,20 @@ 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);

av_packet_unref(m_avpacket);
// If result is not 0, report what went wrong
if (m_result != 0) {
std::cerr << "Failed to send AVPacket to decode: ";
print_av_error_string(m_result);
return;
}

m_result = avcodec_receive_frame(m_avcodec_context, m_avframe_device);
Expand All @@ -201,6 +195,7 @@ class MJPEG2RGB : public pixel_format_base
} else if (m_result < 0) {
std::cerr << "Failed to recieve decoded frame from codec: ";
print_av_error_string(m_result);
return;
}

sws_scale(
Expand Down Expand Up @@ -228,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 ec67cb2

Please sign in to comment.