]> git.sesse.net Git - nageru/blobdiff - ffmpeg_raii.cpp
Move all the FFmpeg RAII helpers into its own file.
[nageru] / ffmpeg_raii.cpp
diff --git a/ffmpeg_raii.cpp b/ffmpeg_raii.cpp
new file mode 100644 (file)
index 0000000..0429597
--- /dev/null
@@ -0,0 +1,60 @@
+#include "ffmpeg_raii.h"
+
+extern "C" {
+#include <libavcodec/avcodec.h>
+#include <libavformat/avformat.h>
+}
+
+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);
+}
+