]> git.sesse.net Git - nageru/blob - shared/ffmpeg_raii.h
Stop using av_init_packet().
[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 AVPacket;
21 struct AVInputFormat;
22 struct SwsContext;
23 typedef struct AVIOInterruptCB AVIOInterruptCB;
24
25 // AVFormatContext
26 struct avformat_close_input_unique {
27         void operator() (AVFormatContext *format_ctx) const;
28 };
29
30 typedef std::unique_ptr<AVFormatContext, avformat_close_input_unique>
31         AVFormatContextWithCloser;
32
33 AVFormatContextWithCloser avformat_open_input_unique(
34         const char *pathname, const AVInputFormat *fmt,
35         AVDictionary **options);
36
37 AVFormatContextWithCloser avformat_open_input_unique(
38         const char *pathname, const AVInputFormat *fmt,
39         AVDictionary **options,
40         const AVIOInterruptCB &interrupt_cb);
41
42 AVFormatContextWithCloser avformat_open_input_unique(
43         int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
44         void *opaque, const AVInputFormat *fmt, AVDictionary **options,
45         const AVIOInterruptCB &interrupt_cb);
46
47
48 // AVCodecContext
49 struct avcodec_free_context_unique {
50         void operator() (AVCodecContext *ctx) const;
51 };
52
53 typedef std::unique_ptr<AVCodecContext, avcodec_free_context_unique>
54         AVCodecContextWithDeleter;
55
56 AVCodecContextWithDeleter avcodec_alloc_context3_unique(const AVCodec *codec);
57
58
59 // AVCodecParameters
60 struct avcodec_parameters_free_unique {
61         void operator() (AVCodecParameters *codec_par) const;
62 };
63
64 typedef std::unique_ptr<AVCodecParameters, avcodec_parameters_free_unique>
65         AVCodecParametersWithDeleter;
66
67
68 // AVFrame
69 struct av_frame_free_unique {
70         void operator() (AVFrame *frame) const;
71 };
72
73 typedef std::unique_ptr<AVFrame, av_frame_free_unique>
74         AVFrameWithDeleter;
75
76 AVFrameWithDeleter av_frame_alloc_unique();
77
78 // AVPacket (ick!)
79 // Not really unique from FFmpeg's point of view, but it is from ours
80 struct av_packet_free_unique {
81         void operator() (AVPacket *packet) const;
82 };
83
84 typedef std::unique_ptr<AVPacket, av_packet_free_unique>
85         AVPacketWithDeleter;
86
87 AVPacketWithDeleter av_packet_alloc_unique();
88
89 // SwsContext
90 struct sws_free_context_unique {
91         void operator() (SwsContext *context) const;
92 };
93
94 typedef std::unique_ptr<SwsContext, sws_free_context_unique>
95         SwsContextWithDeleter;
96
97 #endif  // !defined(_FFMPEG_RAII_H)