]> git.sesse.net Git - nageru/blob - shared/ffmpeg_raii.cpp
Support SRT inputs.
[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, 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 AVFormatContextWithCloser avformat_open_input_unique(
41         int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
42         void *opaque, AVInputFormat *fmt, AVDictionary **options,
43         const AVIOInterruptCB &interrupt_cb)
44 {
45         AVFormatContext *format_ctx = avformat_alloc_context();
46         format_ctx->interrupt_callback = interrupt_cb;
47         constexpr size_t buf_size = 4096;
48         unsigned char *buf = (unsigned char *)av_malloc(buf_size);
49         format_ctx->pb = avio_alloc_context(buf, buf_size, /*write_flag=*/false, opaque,
50                 read_packet, /*write_packet=*/nullptr, /*seek=*/nullptr);
51         if (avformat_open_input(&format_ctx, "", fmt, options) != 0) {
52                 format_ctx = nullptr;
53         }
54         return AVFormatContextWithCloser(format_ctx);
55 }
56
57 // AVCodecContext
58
59 void avcodec_free_context_unique::operator() (AVCodecContext *codec_ctx) const
60 {
61         avcodec_free_context(&codec_ctx);
62 }
63
64 AVCodecContextWithDeleter avcodec_alloc_context3_unique(const AVCodec *codec)
65 {
66         return AVCodecContextWithDeleter(avcodec_alloc_context3(codec));
67 }
68
69
70 // AVCodecParameters
71
72 void avcodec_parameters_free_unique::operator() (AVCodecParameters *codec_par) const
73 {
74         avcodec_parameters_free(&codec_par);
75 }
76
77 // AVFrame
78
79 void av_frame_free_unique::operator() (AVFrame *frame) const
80 {
81         av_frame_free(&frame);
82 }
83
84 AVFrameWithDeleter av_frame_alloc_unique()
85 {
86         return AVFrameWithDeleter(av_frame_alloc());
87 }
88
89 // SwsContext
90
91 void sws_free_context_unique::operator() (SwsContext *context) const
92 {
93         sws_freeContext(context);
94 }