1 #include "ffmpeg_raii.h"
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>
15 void avformat_close_input_unique::operator() (AVFormatContext *format_ctx) const
17 avformat_close_input(&format_ctx);
20 AVFormatContextWithCloser avformat_open_input_unique(
21 const char *pathname, const AVInputFormat *fmt,
22 AVDictionary **options)
24 return avformat_open_input_unique(pathname, fmt, options, AVIOInterruptCB{ nullptr, nullptr });
27 AVFormatContextWithCloser avformat_open_input_unique(
28 const char *pathname, const AVInputFormat *fmt,
29 AVDictionary **options,
30 const AVIOInterruptCB &interrupt_cb)
32 AVFormatContext *format_ctx = avformat_alloc_context();
33 format_ctx->interrupt_callback = interrupt_cb;
35 if (avformat_open_input(&format_ctx, pathname, const_cast<ff_const59 AVInputFormat *>(fmt), options) != 0) {
37 if (avformat_open_input(&format_ctx, pathname, fmt, options) != 0) {
41 return AVFormatContextWithCloser(format_ctx);
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)
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);
56 if (avformat_open_input(&format_ctx, "", const_cast<ff_const59 AVInputFormat *>(fmt), options) != 0) {
58 if (avformat_open_input(&format_ctx, "", fmt, options) != 0) {
62 return AVFormatContextWithCloser(format_ctx);
67 void avcodec_free_context_unique::operator() (AVCodecContext *codec_ctx) const
69 avcodec_free_context(&codec_ctx);
72 AVCodecContextWithDeleter avcodec_alloc_context3_unique(const AVCodec *codec)
74 return AVCodecContextWithDeleter(avcodec_alloc_context3(codec));
80 void avcodec_parameters_free_unique::operator() (AVCodecParameters *codec_par) const
82 avcodec_parameters_free(&codec_par);
87 void av_frame_free_unique::operator() (AVFrame *frame) const
89 av_frame_free(&frame);
92 AVFrameWithDeleter av_frame_alloc_unique()
94 return AVFrameWithDeleter(av_frame_alloc());
99 void sws_free_context_unique::operator() (SwsContext *context) const
101 sws_freeContext(context);