X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=ffmpeg_raii.cpp;fp=ffmpeg_raii.cpp;h=042959780c0c5a05f55570c45717b1371430d723;hb=cd319a6f4b11d888d8e9f996a2ec487668777e13;hp=0000000000000000000000000000000000000000;hpb=41e14ef76d447d31cccc2ec607d3a244baf65c46;p=nageru 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); +} +