Skip to content

Commit

Permalink
Free unused memory for mjpeg format
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Flynn <[email protected]>
  • Loading branch information
flynneva committed Jul 25, 2023
1 parent b221d7f commit 7233c80
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions include/usb_cam/formats/mjpeg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t *>(reinterpret_cast<const uint8_t *>(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);

Expand All @@ -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) {
Expand Down

0 comments on commit 7233c80

Please sign in to comment.