diff --git a/include/usb_cam/formats/mjpeg.hpp b/include/usb_cam/formats/mjpeg.hpp index a8801203..599350c1 100644 --- a/include/usb_cam/formats/mjpeg.hpp +++ b/include/usb_cam/formats/mjpeg.hpp @@ -178,14 +178,29 @@ class MJPEG2RGB : public pixel_format_base // deprecated: https://github.com/FFmpeg/FFmpeg/commit/f7db77bd8785d1715d3e7ed7e69bd1cc991f2d07 av_init_packet(m_avpacket); #else - av_new_packet(m_avpacket, bytes_used); + // Initilize a new packet with defaults + m_result = av_new_packet(m_avpacket, bytes_used); + + if (m_result != 0) { + std::cerr << "Failed to allocate new AV packet: "; + print_av_error_string(m_result); + } #endif - av_packet_from_data( + // Free all unused memory from AV Packet + av_free(m_avpacket->data); + av_free(m_avpacket->buf); + + m_result = av_packet_from_data( m_avpacket, const_cast(reinterpret_cast(src)), bytes_used); + if (m_result != 0) { + std::cerr << "Failed to initialize AV packet: "; + print_av_error_string(m_result); + } + // Pass src MJPEG image to decoder m_result = avcodec_send_packet(m_avcodec_context, m_avpacket); @@ -195,6 +210,9 @@ class MJPEG2RGB : public pixel_format_base print_av_error_string(m_result); } + // After AV packet is sent, free the memory + av_free(m_avpacket); + m_result = avcodec_receive_frame(m_avcodec_context, m_avframe_device); if (m_result == AVERROR(EAGAIN) || m_result == AVERROR_EOF) {