]> git.sesse.net Git - nageru/blob - ffmpeg_raii.cpp
Support audio-only FFmpeg inputs. Somewhat wonky, though.
[nageru] / ffmpeg_raii.cpp
1 #include "ffmpeg_raii.h"
2
3 extern "C" {
4 #include <libavcodec/avcodec.h>
5 #include <libavformat/avformat.h>
6 #include <libavutil/dict.h>
7 #include <libavutil/frame.h>
8 #include <libswscale/swscale.h>
9 }
10
11 using namespace std;
12
13 // AVFormatContext
14
15 void avformat_close_input_unique::operator() (AVFormatContext *format_ctx) const
16 {
17         avformat_close_input(&format_ctx);
18 }
19
20 AVFormatContextWithCloser avformat_open_input_unique(
21         const char *pathname, AVInputFormat *fmt,
22         AVDictionary **options)
23 {
24         return avformat_open_input_unique(pathname, fmt, options, AVIOInterruptCB{ nullptr, nullptr });
25 }
26
27 AVFormatContextWithCloser avformat_open_input_unique(
28         const char *pathname, AVInputFormat *fmt,
29         AVDictionary **options,
30         const AVIOInterruptCB &interrupt_cb)
31 {
32         AVFormatContext *format_ctx = avformat_alloc_context();
33         format_ctx->interrupt_callback = interrupt_cb;
34         if (avformat_open_input(&format_ctx, pathname, fmt, options) != 0) {
35                 format_ctx = nullptr;
36         }
37         return AVFormatContextWithCloser(format_ctx);
38 }
39
40 // AVCodecContext
41
42 void avcodec_free_context_unique::operator() (AVCodecContext *codec_ctx) const
43 {
44         avcodec_free_context(&codec_ctx);
45 }
46
47 AVCodecContextWithDeleter avcodec_alloc_context3_unique(const AVCodec *codec)
48 {
49         return AVCodecContextWithDeleter(avcodec_alloc_context3(codec));
50 }
51
52
53 // AVCodecParameters
54
55 void avcodec_parameters_free_unique::operator() (AVCodecParameters *codec_par) const
56 {
57         avcodec_parameters_free(&codec_par);
58 }
59
60 // AVFrame
61
62 void av_frame_free_unique::operator() (AVFrame *frame) const
63 {
64         av_frame_free(&frame);
65 }
66
67 AVFrameWithDeleter av_frame_alloc_unique()
68 {
69         return AVFrameWithDeleter(av_frame_alloc());
70 }
71
72 // SwsContext
73
74 void sws_free_context_unique::operator() (SwsContext *context) const
75 {
76         sws_freeContext(context);
77 }