audio_decoder: fix more memleaks and dangling files

This commit is contained in:
Megamouse 2024-01-09 20:45:23 +01:00
parent b67837c0ef
commit 4ad4badcfe
2 changed files with 20 additions and 10 deletions

View file

@ -278,7 +278,7 @@ public:
#endif #endif
AVCodecContext* ctx = nullptr; AVCodecContext* ctx = nullptr;
AVFormatContext* fmt = nullptr; AVFormatContext* fmt = nullptr;
u8* io_buf; u8* io_buf = nullptr;
struct AudioReader struct AudioReader
{ {
@ -372,15 +372,16 @@ public:
if (ctx) if (ctx)
{ {
avcodec_close(ctx); avcodec_close(ctx);
avformat_close_input(&fmt); avcodec_free_context(&ctx);
}
if (io_buf)
{
av_freep(&io_buf);
} }
if (fmt) if (fmt)
{ {
if (io_buf) if (fmt->pb) av_freep(&fmt->pb);
{ avformat_close_input(&fmt);
av_free(io_buf);
}
if (fmt->pb) av_free(fmt->pb);
avformat_free_context(fmt); avformat_free_context(fmt);
} }
} }

View file

@ -302,13 +302,22 @@ namespace utils
if (sws) if (sws)
sws_freeContext(sws); sws_freeContext(sws);
if (audio.context) if (audio.context)
{
avcodec_close(audio.context); avcodec_close(audio.context);
avcodec_free_context(&audio.context);
}
if (video.context) if (video.context)
{
avcodec_close(video.context); avcodec_close(video.context);
avcodec_free_context(&video.context);
}
// AVCodec is managed by libavformat, no need to free it // AVCodec is managed by libavformat, no need to free it
// see: https://stackoverflow.com/a/18047320 // see: https://stackoverflow.com/a/18047320
if (format_context) if (format_context)
{
avformat_close_input(&format_context);
avformat_free_context(format_context); avformat_free_context(format_context);
}
//if (stream) //if (stream)
// av_free(stream); // av_free(stream);
if (kill_callback) if (kill_callback)
@ -626,7 +635,7 @@ namespace utils
} }
// Resample frames // Resample frames
u8* buffer; u8* buffer = nullptr;
const int align = 1; const int align = 1;
const int buffer_size = av_samples_alloc(&buffer, nullptr, dst_channels, av.audio.frame->nb_samples, dst_format, align); const int buffer_size = av_samples_alloc(&buffer, nullptr, dst_channels, av.audio.frame->nb_samples, dst_format, align);
if (buffer_size < 0) if (buffer_size < 0)
@ -642,7 +651,7 @@ namespace utils
media_log.error("audio_decoder: Error converting frame: %d='%s'", frame_count, av_error_to_string(frame_count)); media_log.error("audio_decoder: Error converting frame: %d='%s'", frame_count, av_error_to_string(frame_count));
has_error = true; has_error = true;
if (buffer) if (buffer)
av_free(buffer); av_freep(&buffer);
return; return;
} }
@ -660,7 +669,7 @@ namespace utils
} }
if (buffer) if (buffer)
av_free(buffer); av_freep(&buffer);
media_log.notice("audio_decoder: decoded frame_count=%d buffer_size=%d timestamp_us=%d", frame_count, buffer_size, av.audio.frame->best_effort_timestamp); media_log.notice("audio_decoder: decoded frame_count=%d buffer_size=%d timestamp_us=%d", frame_count, buffer_size, av.audio.frame->best_effort_timestamp);
} }