]> git.sesse.net Git - nageru/blob - shared/ffmpeg_raii.h
Support SRT inputs.
[nageru] / shared / ffmpeg_raii.h
1 #ifndef _FFMPEG_RAII_H
2 #define _FFMPEG_RAII_H 1
3
4 // Some helpers to make RAII versions of FFmpeg objects.
5 // The cleanup functions don't interact all that well with unique_ptr,
6 // so things get a bit messy and verbose, but overall it's worth it to ensure
7 // we never leak things by accident in error paths.
8 //
9 // This does not cover any of the types that can actually be declared as
10 // a unique_ptr with no helper functions for deleter.
11
12 #include <memory>
13
14 struct AVCodec;
15 struct AVCodecContext;
16 struct AVCodecParameters;
17 struct AVDictionary;
18 struct AVFormatContext;
19 struct AVFrame;
20 struct AVInputFormat;
21 struct SwsContext;
22 typedef struct AVIOInterruptCB AVIOInterruptCB;
23
24 // AVFormatContext
25 struct avformat_close_input_unique {
26         void operator() (AVFormatContext *format_ctx) const;
27 };
28
29 typedef std::unique_ptr<AVFormatContext, avformat_close_input_unique>
30         AVFormatContextWithCloser;
31
32 AVFormatContextWithCloser avformat_open_input_unique(
33         const char *pathname, AVInputFormat *fmt,
34         AVDictionary **options);
35
36 AVFormatContextWithCloser avformat_open_input_unique(
37         const char *pathname, AVInputFormat *fmt,
38         AVDictionary **options,
39         const AVIOInterruptCB &interrupt_cb);
40
41 AVFormatContextWithCloser avformat_open_input_unique(
42         int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
43         void *opaque, AVInputFormat *fmt, AVDictionary **options,
44         const AVIOInterruptCB &interrupt_cb);
45
46
47 // AVCodecContext
48 struct avcodec_free_context_unique {
49         void operator() (AVCodecContext *ctx) const;
50 };
51
52 typedef std::unique_ptr<AVCodecContext, avcodec_free_context_unique>
53         AVCodecContextWithDeleter;
54
55 AVCodecContextWithDeleter avcodec_alloc_context3_unique(const AVCodec *codec);
56
57
58 // AVCodecParameters
59 struct avcodec_parameters_free_unique {
60         void operator() (AVCodecParameters *codec_par) const;
61 };
62
63 typedef std::unique_ptr<AVCodecParameters, avcodec_parameters_free_unique>
64         AVCodecParametersWithDeleter;
65
66
67 // AVFrame
68 struct av_frame_free_unique {
69         void operator() (AVFrame *frame) const;
70 };
71
72 typedef std::unique_ptr<AVFrame, av_frame_free_unique>
73         AVFrameWithDeleter;
74
75 AVFrameWithDeleter av_frame_alloc_unique();
76
77 // SwsContext
78 struct sws_free_context_unique {
79         void operator() (SwsContext *context) const;
80 };
81
82 typedef std::unique_ptr<SwsContext, sws_free_context_unique>
83         SwsContextWithDeleter;
84
85 #endif  // !defined(_FFMPEG_RAII_H)