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, 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, AVInputFormat *fmt,
29 AVDictionary **options,
30 const AVIOInterruptCB &interrupt_cb)
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) {
37 return AVFormatContextWithCloser(format_ctx);
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)
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) {
54 return AVFormatContextWithCloser(format_ctx);
59 void avcodec_free_context_unique::operator() (AVCodecContext *codec_ctx) const
61 avcodec_free_context(&codec_ctx);
64 AVCodecContextWithDeleter avcodec_alloc_context3_unique(const AVCodec *codec)
66 return AVCodecContextWithDeleter(avcodec_alloc_context3(codec));
72 void avcodec_parameters_free_unique::operator() (AVCodecParameters *codec_par) const
74 avcodec_parameters_free(&codec_par);
79 void av_frame_free_unique::operator() (AVFrame *frame) const
81 av_frame_free(&frame);
84 AVFrameWithDeleter av_frame_alloc_unique()
86 return AVFrameWithDeleter(av_frame_alloc());
91 void sws_free_context_unique::operator() (SwsContext *context) const
93 sws_freeContext(context);