]> git.sesse.net Git - nageru/blob - ffmpeg_raii.h
Fix an issue where the mixer lagging too much behind CEF would cause us to display...
[nageru] / 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
23 // AVFormatContext
24 struct avformat_close_input_unique {
25         void operator() (AVFormatContext *format_ctx) const;
26 };
27
28 typedef std::unique_ptr<AVFormatContext, avformat_close_input_unique>
29         AVFormatContextWithCloser;
30
31 AVFormatContextWithCloser avformat_open_input_unique(
32         const char *pathname, AVInputFormat *fmt, AVDictionary **options);
33
34
35 // AVCodecContext
36 struct avcodec_free_context_unique {
37         void operator() (AVCodecContext *ctx) const;
38 };
39
40 typedef std::unique_ptr<AVCodecContext, avcodec_free_context_unique>
41         AVCodecContextWithDeleter;
42
43 AVCodecContextWithDeleter avcodec_alloc_context3_unique(const AVCodec *codec);
44
45
46 // AVCodecParameters
47 struct avcodec_parameters_free_unique {
48         void operator() (AVCodecParameters *codec_par) const;
49 };
50
51 typedef std::unique_ptr<AVCodecParameters, avcodec_parameters_free_unique>
52         AVCodecParametersWithDeleter;
53
54
55 // AVFrame
56 struct av_frame_free_unique {
57         void operator() (AVFrame *frame) const;
58 };
59
60 typedef std::unique_ptr<AVFrame, av_frame_free_unique>
61         AVFrameWithDeleter;
62
63 AVFrameWithDeleter av_frame_alloc_unique();
64
65 // SwsContext
66 struct sws_free_context_unique {
67         void operator() (SwsContext *context) const;
68 };
69
70 typedef std::unique_ptr<SwsContext, sws_free_context_unique>
71         SwsContextWithDeleter;
72
73 #endif  // !defined(_FFMPEG_RAII_H)