]> git.sesse.net Git - nageru/blob - shared/ffmpeg_raii.cpp
Stop using av_init_packet().
[nageru] / shared / 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, const 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, const 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 #ifdef ff_const59
35         if (avformat_open_input(&format_ctx, pathname, const_cast<ff_const59 AVInputFormat *>(fmt), options) != 0) {
36 #else
37         if (avformat_open_input(&format_ctx, pathname, fmt, options) != 0) {
38 #endif
39                 format_ctx = nullptr;
40         }
41         return AVFormatContextWithCloser(format_ctx);
42 }
43
44 AVFormatContextWithCloser avformat_open_input_unique(
45         int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
46         void *opaque, const AVInputFormat *fmt, AVDictionary **options,
47         const AVIOInterruptCB &interrupt_cb)
48 {
49         AVFormatContext *format_ctx = avformat_alloc_context();
50         format_ctx->interrupt_callback = interrupt_cb;
51         constexpr size_t buf_size = 4096;
52         unsigned char *buf = (unsigned char *)av_malloc(buf_size);
53         format_ctx->pb = avio_alloc_context(buf, buf_size, /*write_flag=*/false, opaque,
54                 read_packet, /*write_packet=*/nullptr, /*seek=*/nullptr);
55 #ifdef ff_const59
56         if (avformat_open_input(&format_ctx, "", const_cast<ff_const59 AVInputFormat *>(fmt), options) != 0) {
57 #else
58         if (avformat_open_input(&format_ctx, "", fmt, options) != 0) {
59 #endif
60                 format_ctx = nullptr;
61         }
62         return AVFormatContextWithCloser(format_ctx);
63 }
64
65 // AVCodecContext
66
67 void avcodec_free_context_unique::operator() (AVCodecContext *codec_ctx) const
68 {
69         avcodec_free_context(&codec_ctx);
70 }
71
72 AVCodecContextWithDeleter avcodec_alloc_context3_unique(const AVCodec *codec)
73 {
74         return AVCodecContextWithDeleter(avcodec_alloc_context3(codec));
75 }
76
77
78 // AVCodecParameters
79
80 void avcodec_parameters_free_unique::operator() (AVCodecParameters *codec_par) const
81 {
82         avcodec_parameters_free(&codec_par);
83 }
84
85 // AVFrame
86
87 void av_frame_free_unique::operator() (AVFrame *frame) const
88 {
89         av_frame_free(&frame);
90 }
91
92 AVFrameWithDeleter av_frame_alloc_unique()
93 {
94         return AVFrameWithDeleter(av_frame_alloc());
95 }
96
97 // AVPacket
98 void av_packet_free_unique::operator() (AVPacket *packet) const
99 {
100         av_packet_unref(packet);
101 }
102
103 AVPacketWithDeleter av_packet_alloc_unique()
104 {
105         return AVPacketWithDeleter(av_packet_alloc());
106 }
107
108 // SwsContext
109
110 void sws_free_context_unique::operator() (SwsContext *context) const
111 {
112         sws_freeContext(context);
113 }