From: Steinar H. Gunderson Date: Wed, 27 Jul 2016 18:27:10 +0000 (+0200) Subject: Move all the FFmpeg RAII helpers into its own file. X-Git-Tag: 1.3.3~6 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=cd319a6f4b11d888d8e9f996a2ec487668777e13;p=nageru Move all the FFmpeg RAII helpers into its own file. --- diff --git a/Makefile b/Makefile index 8691f4e..a695b93 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ OBJS += glwidget.moc.o mainwindow.moc.o vumeter.moc.o lrameter.moc.o correlation OBJS += mixer.o pbo_frame_allocator.o context.o ref_counted_frame.o theme.o resampling_queue.o httpd.o ebu_r128_proc.o flags.o image_input.o stereocompressor.o filter.o alsa_output.o correlation_measurer.o # Streaming and encoding objects -OBJS += quicksync_encoder.o x264_encoder.o x264_speed_control.o video_encoder.o metacube2.o mux.o audio_encoder.o +OBJS += quicksync_encoder.o x264_encoder.o x264_speed_control.o video_encoder.o metacube2.o mux.o audio_encoder.o ffmpeg_raii.o # DeckLink OBJS += decklink_capture.o decklink/DeckLinkAPIDispatch.o diff --git a/audio_encoder.h b/audio_encoder.h index 8a08af5..7bdb02a 100644 --- a/audio_encoder.h +++ b/audio_encoder.h @@ -13,16 +13,9 @@ extern "C" { #include } +#include "ffmpeg_raii.h" #include "mux.h" -static inline void avcodec_parameters_free_unique(AVCodecParameters *codec_par) -{ - avcodec_parameters_free(&codec_par); -} - -typedef std::unique_ptr -AVCodecParametersWithDeleter; - class AudioEncoder { public: AudioEncoder(const std::string &codec_name, int bit_rate, const AVOutputFormat *oformat); diff --git a/ffmpeg_raii.cpp b/ffmpeg_raii.cpp new file mode 100644 index 0000000..0429597 --- /dev/null +++ b/ffmpeg_raii.cpp @@ -0,0 +1,60 @@ +#include "ffmpeg_raii.h" + +extern "C" { +#include +#include +} + +using namespace std; + +// AVFormatContext + +void avformat_close_input_unique(AVFormatContext *format_ctx) +{ + avformat_close_input(&format_ctx); +} + +AVFormatContextWithCloser avformat_open_input_unique( + const char *pathname, AVInputFormat *fmt, AVDictionary **options) +{ + AVFormatContext *format_ctx = nullptr; + if (avformat_open_input(&format_ctx, pathname, fmt, options) != 0) { + format_ctx = nullptr; + } + return AVFormatContextWithCloser(format_ctx, avformat_close_input_unique); +} + + +// AVCodecContext + +void avcodec_free_context_unique(AVCodecContext *codec_ctx) +{ + avcodec_free_context(&codec_ctx); +} + +AVCodecContextWithDeleter avcodec_alloc_context3_unique(const AVCodec *codec) +{ + return AVCodecContextWithDeleter(avcodec_alloc_context3(codec), avcodec_free_context_unique); +} + + +// AVCodecParameters + +void avcodec_parameters_free_unique(AVCodecParameters *codec_par) +{ + avcodec_parameters_free(&codec_par); +} + + +// AVFrame + +void av_frame_free_unique(AVFrame *frame) +{ + av_frame_free(&frame); +} + +AVFrameWithDeleter av_frame_alloc_unique() +{ + return AVFrameWithDeleter(av_frame_alloc(), av_frame_free_unique); +} + diff --git a/ffmpeg_raii.h b/ffmpeg_raii.h new file mode 100644 index 0000000..d27c20d --- /dev/null +++ b/ffmpeg_raii.h @@ -0,0 +1,57 @@ +#ifndef _FFMPEG_RAII_H +#define _FFMPEG_RAII_H 1 + +// Some helpers to make RAII versions of FFmpeg objects. +// The cleanup functions don't interact all that well with unique_ptr, +// so things get a bit messy and verbose, but overall it's worth it to ensure +// we never leak things by accident in error paths. +// +// This does not cover any of the types that can actually be declared as +// a unique_ptr with no helper functions for deleter. + +#include + +struct AVCodec; +struct AVCodecContext; +struct AVCodecParameters; +struct AVDictionary; +struct AVFormatContext; +struct AVFrame; +struct AVInputFormat; + + +// AVFormatContext +void avformat_close_input_unique(AVFormatContext *format_ctx); + +typedef std::unique_ptr + AVFormatContextWithCloser; + +AVFormatContextWithCloser avformat_open_input_unique( + const char *pathname, AVInputFormat *fmt, AVDictionary **options); + + +// AVCodecContext +void avcodec_free_context_unique(AVCodecContext *codec_ctx); + +typedef std::unique_ptr + AVCodecContextWithDeleter; + +AVCodecContextWithDeleter avcodec_alloc_context3_unique(const AVCodec *codec); + + +// AVCodecParameters +void avcodec_parameters_free_unique(AVCodecParameters *codec_par); + +typedef std::unique_ptr + AVCodecParametersWithDeleter; + + +// AVFrame +void av_frame_free_unique(AVFrame *frame); + +typedef std::unique_ptr + AVFrameWithDeleter; + +AVFrameWithDeleter av_frame_alloc_unique(); + +#endif // !defined(_FFMPEG_RAII_H) diff --git a/image_input.cpp b/image_input.cpp index c9f74f8..b8e2b02 100644 --- a/image_input.cpp +++ b/image_input.cpp @@ -18,6 +18,7 @@ extern "C" { #include #include +#include "ffmpeg_raii.h" #include "flags.h" using namespace std; @@ -99,50 +100,6 @@ shared_ptr ImageInput::load_image(const string &pathnam return all_images[pathname]; } -// Some helpers to make RAII versions of FFmpeg objects. -// The cleanup functions don't interact all that well with unique_ptr, -// so things get a bit messy and verbose, but overall it's worth it to ensure -// we never leak things by accident in error paths. - -namespace { - -void avformat_close_input_unique(AVFormatContext *format_ctx) -{ - avformat_close_input(&format_ctx); -} - -unique_ptr -avformat_open_input_unique(const char *pathname, - AVInputFormat *fmt, AVDictionary **options) -{ - AVFormatContext *format_ctx = nullptr; - if (avformat_open_input(&format_ctx, pathname, fmt, options) != 0) { - format_ctx = nullptr; - } - return unique_ptr( - format_ctx, avformat_close_input_unique); -} - -void av_frame_free_unique(AVFrame *frame) -{ - av_frame_free(&frame); -} - -unique_ptr -av_frame_alloc_unique() -{ - AVFrame *frame = av_frame_alloc(); - return unique_ptr( - frame, av_frame_free_unique); -} - -void avcodec_free_context_unique(AVCodecContext *codec_ctx) -{ - avcodec_free_context(&codec_ctx); -} - -} // namespace - shared_ptr ImageInput::load_image_raw(const string &pathname) { // Note: Call before open, not after; otherwise, there's a race. @@ -179,10 +136,8 @@ shared_ptr ImageInput::load_image_raw(const string &pat } const AVCodecParameters *codecpar = format_ctx->streams[stream_index]->codecpar; - AVCodecContext *codec_ctx = avcodec_alloc_context3(nullptr); - unique_ptr codec_ctx_free( - codec_ctx, avcodec_free_context_unique); - if (avcodec_parameters_to_context(codec_ctx, codecpar) < 0) { + AVCodecContextWithDeleter codec_ctx = avcodec_alloc_context3_unique(nullptr); + if (avcodec_parameters_to_context(codec_ctx.get(), codecpar) < 0) { fprintf(stderr, "%s: Cannot fill codec parameters\n", pathname.c_str()); return nullptr; } @@ -191,16 +146,16 @@ shared_ptr ImageInput::load_image_raw(const string &pat fprintf(stderr, "%s: Cannot find decoder\n", pathname.c_str()); return nullptr; } - if (avcodec_open2(codec_ctx, codec, nullptr) < 0) { + if (avcodec_open2(codec_ctx.get(), codec, nullptr) < 0) { fprintf(stderr, "%s: Cannot open decoder\n", pathname.c_str()); return nullptr; } unique_ptr codec_ctx_cleanup( - codec_ctx, avcodec_close); + codec_ctx.get(), avcodec_close); // Read packets until we have a frame or there are none left. int frame_finished = 0; - auto frame = av_frame_alloc_unique(); + AVFrameWithDeleter frame = av_frame_alloc_unique(); bool eof = false; do { AVPacket pkt; @@ -213,7 +168,7 @@ shared_ptr ImageInput::load_image_raw(const string &pat if (pkt.stream_index != stream_index) { continue; } - if (avcodec_send_packet(codec_ctx, &pkt) < 0) { + if (avcodec_send_packet(codec_ctx.get(), &pkt) < 0) { fprintf(stderr, "%s: Cannot send packet to codec.\n", pathname.c_str()); return nullptr; } @@ -221,7 +176,7 @@ shared_ptr ImageInput::load_image_raw(const string &pat eof = true; // Or error, but ignore that for the time being. } - int err = avcodec_receive_frame(codec_ctx, frame.get()); + int err = avcodec_receive_frame(codec_ctx.get(), frame.get()); if (err == 0) { frame_finished = true; break;