Skip to content

Commit 85cb742

Browse files
committed
ffmpeg: drain avcodec contexts on close, fixes leak
Fixes panda3d#398
1 parent 32df05b commit 85cb742

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

panda/src/ffmpeg/ffmpegAudioCursor.cxx

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,23 @@ FfmpegAudioCursor::
210210
*/
211211
void FfmpegAudioCursor::
212212
cleanup() {
213+
if (_audio_ctx && _audio_ctx->codec) {
214+
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 37, 100)
215+
// We need to drain the codec to prevent a memory leak.
216+
avcodec_send_packet(_audio_ctx, nullptr);
217+
while (avcodec_receive_frame(_audio_ctx, _frame) == 0) {}
218+
avcodec_flush_buffers(_audio_ctx);
219+
#endif
220+
221+
avcodec_close(_audio_ctx);
222+
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 52, 0)
223+
avcodec_free_context(&_audio_ctx);
224+
#else
225+
delete _audio_ctx;
226+
#endif
227+
}
228+
_audio_ctx = nullptr;
229+
213230
if (_frame) {
214231
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 45, 101)
215232
av_frame_free(&_frame);
@@ -237,16 +254,6 @@ cleanup() {
237254
_buffer = nullptr;
238255
}
239256

240-
if ((_audio_ctx)&&(_audio_ctx->codec)) {
241-
avcodec_close(_audio_ctx);
242-
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 52, 0)
243-
avcodec_free_context(&_audio_ctx);
244-
#else
245-
delete _audio_ctx;
246-
#endif
247-
}
248-
_audio_ctx = nullptr;
249-
250257
if (_format_ctx) {
251258
_ffvfile.close();
252259
_format_ctx = nullptr;

panda/src/ffmpeg/ffmpegVideoCursor.cxx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,14 @@ close_stream() {
595595
// Hold the global lock while we free avcodec objects.
596596
ReMutexHolder av_holder(_av_lock);
597597

598-
if ((_video_ctx)&&(_video_ctx->codec)) {
598+
if (_video_ctx && _video_ctx->codec) {
599+
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 37, 100)
600+
// We need to drain the codec to prevent a memory leak.
601+
avcodec_send_packet(_video_ctx, nullptr);
602+
while (avcodec_receive_frame(_video_ctx, _frame) == 0) {}
603+
avcodec_flush_buffers(_video_ctx);
604+
#endif
605+
599606
avcodec_close(_video_ctx);
600607
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 52, 0)
601608
avcodec_free_context(&_video_ctx);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy