decoder/ffmpeg: use AtScopeExit() for safe cleanup

This commit is contained in:
Max Kellermann 2016-07-29 09:08:14 +02:00
parent a076ddf38c
commit a9edb4de28

View File

@ -35,6 +35,7 @@
#include "tag/TagHandler.hxx" #include "tag/TagHandler.hxx"
#include "input/InputStream.hxx" #include "input/InputStream.hxx"
#include "CheckAudioFormat.hxx" #include "CheckAudioFormat.hxx"
#include "util/ScopeExit.hxx"
#include "util/ConstBuffer.hxx" #include "util/ConstBuffer.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "LogV.hxx" #include "LogV.hxx"
@ -473,6 +474,10 @@ FfmpegDecode(Decoder &decoder, InputStream &input,
return; return;
} }
AtScopeExit(codec_context) {
avcodec_close(codec_context);
};
const SignedSongTime total_time = const SignedSongTime total_time =
FromFfmpegTimeChecked(av_stream.duration, av_stream.time_base); FromFfmpegTimeChecked(av_stream.duration, av_stream.time_base);
@ -489,6 +494,16 @@ FfmpegDecode(Decoder &decoder, InputStream &input,
return; return;
} }
AtScopeExit(&frame) {
#if LIBAVUTIL_VERSION_MAJOR >= 53
av_frame_free(&frame);
#elif LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 28, 0)
avcodec_free_frame(&frame);
#else
av_free(frame);
#endif
};
FfmpegBuffer interleaved_buffer; FfmpegBuffer interleaved_buffer;
uint64_t min_frame = 0; uint64_t min_frame = 0;
@ -537,16 +552,6 @@ FfmpegDecode(Decoder &decoder, InputStream &input,
av_free_packet(&packet); av_free_packet(&packet);
#endif #endif
} }
#if LIBAVUTIL_VERSION_MAJOR >= 53
av_frame_free(&frame);
#elif LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 28, 0)
avcodec_free_frame(&frame);
#else
av_free(frame);
#endif
avcodec_close(codec_context);
} }
static void static void
@ -572,9 +577,11 @@ ffmpeg_decode(Decoder &decoder, InputStream &input)
return; return;
} }
FfmpegDecode(decoder, input, *format_context); AtScopeExit(&format_context) {
avformat_close_input(&format_context); avformat_close_input(&format_context);
};
FfmpegDecode(decoder, input, *format_context);
} }
static bool static bool
@ -619,9 +626,11 @@ ffmpeg_scan_stream(InputStream &is,
if (f == nullptr) if (f == nullptr)
return false; return false;
bool result = FfmpegScanStream(*f, *handler, handler_ctx); AtScopeExit(&f) {
avformat_close_input(&f); avformat_close_input(&f);
return result; };
return FfmpegScanStream(*f, *handler, handler_ctx);
} }
/** /**