31#ifndef OPENSHOT_FFMPEG_UTILITIES_H
32#define OPENSHOT_FFMPEG_UTILITIES_H
38#define INT64_C(c) (c ## LL)
39#define UINT64_C(c) (c ## ULL)
43#define IS_FFMPEG_3_2 (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101))
47#define USE_HW_ACCEL (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 107, 100))
51#define USE_SW FFMPEG_USE_SWRESAMPLE
56 #include <libavcodec/avcodec.h>
57 #include <libavformat/avformat.h>
59#if (LIBAVFORMAT_VERSION_MAJOR >= 57)
60 #include <libavutil/hwcontext.h>
62 #include <libswscale/swscale.h>
65 #include <libswresample/swresample.h>
67 #include <libavresample/avresample.h>
70 #include <libavutil/mathematics.h>
71 #include <libavutil/pixfmt.h>
72 #include <libavutil/pixdesc.h>
75#if LIBAVFORMAT_VERSION_MAJOR >= 53
76 #include <libavutil/opt.h>
78 #include <libavcodec/opt.h>
82#if LIBAVFORMAT_VERSION_MAJOR >= 54
83 #include <libavutil/channel_layout.h>
87 #include "libavutil/imgutils.h"
92#ifndef AVCODEC_MAX_AUDIO_FRAME_SIZE
94 #define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000
96#ifndef AV_ERROR_MAX_STRING_SIZE
97 #define AV_ERROR_MAX_STRING_SIZE 64
99#ifndef AUDIO_PACKET_ENCODING_SIZE
101 #define AUDIO_PACKET_ENCODING_SIZE 768000
105inline static const std::string av_err2string(
int errnum)
109 return static_cast<std::string
>(errbuf);
114#define av_err2str(errnum) av_err2string(errnum).c_str()
118 #define PixelFormat AVPixelFormat
121 #define PIX_FMT_RGBA AV_PIX_FMT_RGBA
124 #define PIX_FMT_NONE AV_PIX_FMT_NONE
127 #define PIX_FMT_RGB24 AV_PIX_FMT_RGB24
129#ifndef PIX_FMT_YUV420P
130 #define PIX_FMT_YUV420P AV_PIX_FMT_YUV420P
132#ifndef PIX_FMT_YUV444P
133 #define PIX_FMT_YUV444P AV_PIX_FMT_YUV444P
137inline static bool ffmpeg_has_alpha(
PixelFormat pix_fmt) {
138 const AVPixFmtDescriptor *fmt_desc = av_pix_fmt_desc_get(pix_fmt);
139 return bool(fmt_desc->flags & AV_PIX_FMT_FLAG_ALPHA);
145 #define FF_RSHIFT(a, b) RSHIFT(a, b)
151 #define SWR_CONVERT(ctx, out, linesize, out_count, in, linesize2, in_count) \
152 swr_convert(ctx, out, out_count, (const uint8_t **)in, in_count)
153 #define SWR_ALLOC() swr_alloc()
154 #define SWR_CLOSE(ctx) {}
155 #define SWR_FREE(ctx) swr_free(ctx)
156 #define SWR_INIT(ctx) swr_init(ctx)
157 #define SWRCONTEXT SwrContext
160 #define SWR_CONVERT(ctx, out, linesize, out_count, in, linesize2, in_count) \
161 avresample_convert(ctx, out, linesize, out_count, (uint8_t **)in, linesize2, in_count)
162 #define SWR_ALLOC() avresample_alloc_context()
163 #define SWR_CLOSE(ctx) avresample_close(ctx)
164 #define SWR_FREE(ctx) avresample_free(ctx)
165 #define SWR_INIT(ctx) avresample_open(ctx)
166 #define SWRCONTEXT AVAudioResampleContext
170#if (LIBAVFORMAT_VERSION_MAJOR >= 58)
171 #define AV_REGISTER_ALL
172 #define AVCODEC_REGISTER_ALL
173 #define AV_FILENAME url
174 #define AV_SET_FILENAME(oc, f) oc->AV_FILENAME = av_strdup(f)
175 #define MY_INPUT_BUFFER_PADDING_SIZE AV_INPUT_BUFFER_PADDING_SIZE
176 #define AV_ALLOCATE_FRAME() av_frame_alloc()
177 #define AV_ALLOCATE_IMAGE(av_frame, pix_fmt, width, height) \
178 av_image_alloc(av_frame->data, av_frame->linesize, width, height, pix_fmt, 1)
179 #define AV_RESET_FRAME(av_frame) av_frame_unref(av_frame)
180 #define AV_FREE_FRAME(av_frame) av_frame_free(av_frame)
181 #define AV_FREE_PACKET(av_packet) av_packet_unref(av_packet)
182 #define AV_FREE_CONTEXT(av_context) avcodec_free_context(&av_context)
183 #define AV_GET_CODEC_TYPE(av_stream) av_stream->codecpar->codec_type
184 #define AV_FIND_DECODER_CODEC_ID(av_stream) av_stream->codecpar->codec_id
185 #define AV_GET_CODEC_CONTEXT(av_stream, av_codec) \
186 ({ AVCodecContext *context = avcodec_alloc_context3(av_codec); \
187 avcodec_parameters_to_context(context, av_stream->codecpar); \
189 #define AV_GET_CODEC_PAR_CONTEXT(av_stream, av_codec) av_codec;
190 #define AV_GET_CODEC_FROM_STREAM(av_stream,codec_in)
191 #define AV_GET_CODEC_ATTRIBUTES(av_stream, av_context) av_stream->codecpar
192 #define AV_GET_CODEC_PIXEL_FORMAT(av_stream, av_context) (AVPixelFormat) av_stream->codecpar->format
193 #define AV_GET_SAMPLE_FORMAT(av_stream, av_context) av_stream->codecpar->format
194 #define AV_GET_IMAGE_SIZE(pix_fmt, width, height) \
195 av_image_get_buffer_size(pix_fmt, width, height, 1)
196 #define AV_COPY_PICTURE_DATA(av_frame, buffer, pix_fmt, width, height) \
197 av_image_fill_arrays(av_frame->data, av_frame->linesize, buffer, pix_fmt, width, height, 1)
198 #define AV_OUTPUT_CONTEXT(output_context, path) avformat_alloc_output_context2( output_context, NULL, NULL, path)
199 #define AV_OPTION_FIND(priv_data, name) av_opt_find(priv_data, name, NULL, 0, 0)
200 #define AV_OPTION_SET( av_stream, priv_data, name, value, avcodec) \
201 av_opt_set(priv_data, name, value, 0); \
202 avcodec_parameters_from_context(av_stream->codecpar, avcodec);
203 #define ALLOC_CODEC_CTX(ctx, codec, stream) \
204 ctx = avcodec_alloc_context3(codec);
205 #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec_ctx) \
206 avcodec_parameters_from_context(av_stream->codecpar, av_codec_ctx);
209 #define AV_REGISTER_ALL av_register_all();
210 #define AVCODEC_REGISTER_ALL avcodec_register_all();
211 #define AV_FILENAME filename
212 #define AV_SET_FILENAME(oc, f) snprintf(oc->AV_FILENAME, sizeof(oc->AV_FILENAME), "%s", f)
213 #define MY_INPUT_BUFFER_PADDING_SIZE FF_INPUT_BUFFER_PADDING_SIZE
214 #define AV_ALLOCATE_FRAME() av_frame_alloc()
215 #define AV_ALLOCATE_IMAGE(av_frame, pix_fmt, width, height) \
216 av_image_alloc(av_frame->data, av_frame->linesize, width, height, pix_fmt, 1)
217 #define AV_RESET_FRAME(av_frame) av_frame_unref(av_frame)
218 #define AV_FREE_FRAME(av_frame) av_frame_free(av_frame)
219 #define AV_FREE_PACKET(av_packet) av_packet_unref(av_packet)
220 #define AV_FREE_CONTEXT(av_context) avcodec_free_context(&av_context)
221 #define AV_GET_CODEC_TYPE(av_stream) av_stream->codecpar->codec_type
222 #define AV_FIND_DECODER_CODEC_ID(av_stream) av_stream->codecpar->codec_id
223 #define AV_GET_CODEC_CONTEXT(av_stream, av_codec) \
224 ({ AVCodecContext *context = avcodec_alloc_context3(av_codec); \
225 avcodec_parameters_to_context(context, av_stream->codecpar); \
227 #define AV_GET_CODEC_PAR_CONTEXT(av_stream, av_codec) av_codec;
228 #define AV_GET_CODEC_FROM_STREAM(av_stream,codec_in)
229 #define AV_GET_CODEC_ATTRIBUTES(av_stream, av_context) av_stream->codecpar
230 #define AV_GET_CODEC_PIXEL_FORMAT(av_stream, av_context) \
231 (AVPixelFormat) av_stream->codecpar->format
232 #define AV_GET_SAMPLE_FORMAT(av_stream, av_context) av_stream->codecpar->format
233 #define AV_GET_IMAGE_SIZE(pix_fmt, width, height) av_image_get_buffer_size(pix_fmt, width, height, 1)
234 #define AV_COPY_PICTURE_DATA(av_frame, buffer, pix_fmt, width, height) \
235 av_image_fill_arrays(av_frame->data, av_frame->linesize, buffer, pix_fmt, width, height, 1)
236 #define AV_OUTPUT_CONTEXT(output_context, path) \
237 avformat_alloc_output_context2( output_context, NULL, NULL, path)
238 #define AV_OPTION_FIND(priv_data, name) av_opt_find(priv_data, name, NULL, 0, 0)
239 #define AV_OPTION_SET( av_stream, priv_data, name, value, avcodec) \
240 av_opt_set(priv_data, name, value, 0); \
241 avcodec_parameters_from_context(av_stream->codecpar, avcodec);
242 #define ALLOC_CODEC_CTX(ctx, codec, stream) \
243 ctx = avcodec_alloc_context3(codec);
244 #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec) \
245 avcodec_parameters_from_context(av_stream->codecpar, av_codec);
247#elif LIBAVFORMAT_VERSION_MAJOR >= 55
248 #define AV_REGISTER_ALL av_register_all();
249 #define AVCODEC_REGISTER_ALL avcodec_register_all();
250 #define AV_FILENAME filename
251 #define AV_SET_FILENAME(oc, f) snprintf(oc->AV_FILENAME, sizeof(oc->AV_FILENAME), "%s", f)
252 #define MY_INPUT_BUFFER_PADDING_SIZE FF_INPUT_BUFFER_PADDING_SIZE
253 #define AV_ALLOCATE_FRAME() av_frame_alloc()
254 #define AV_ALLOCATE_IMAGE(av_frame, pix_fmt, width, height) \
255 avpicture_alloc((AVPicture *) av_frame, pix_fmt, width, height)
256 #define AV_RESET_FRAME(av_frame) av_frame_unref(av_frame)
257 #define AV_FREE_FRAME(av_frame) av_frame_free(av_frame)
258 #define AV_FREE_PACKET(av_packet) av_packet_unref(av_packet)
259 #define AV_FREE_CONTEXT(av_context) avcodec_close(av_context)
260 #define AV_GET_CODEC_TYPE(av_stream) av_stream->codec->codec_type
261 #define AV_FIND_DECODER_CODEC_ID(av_stream) av_stream->codec->codec_id
262 #define AV_GET_CODEC_CONTEXT(av_stream, av_codec) av_stream->codec
263 #define AV_GET_CODEC_PAR_CONTEXT(av_stream, av_codec) av_stream->codec
264 #define AV_GET_CODEC_FROM_STREAM(av_stream, codec_in) codec_in = av_stream->codec;
265 #define AV_GET_CODEC_ATTRIBUTES(av_stream, av_context) av_context
266 #define AV_GET_CODEC_PIXEL_FORMAT(av_stream, av_context) av_context->pix_fmt
267 #define AV_GET_SAMPLE_FORMAT(av_stream, av_context) av_context->sample_fmt
268 #define AV_GET_IMAGE_SIZE(pix_fmt, width, height) avpicture_get_size(pix_fmt, width, height)
269 #define AV_COPY_PICTURE_DATA(av_frame, buffer, pix_fmt, width, height) \
270 avpicture_fill((AVPicture *) av_frame, buffer, pix_fmt, width, height)
271 #define AV_OUTPUT_CONTEXT(output_context, path) oc = avformat_alloc_context()
272 #define AV_OPTION_FIND(priv_data, name) av_opt_find(priv_data, name, NULL, 0, 0)
273 #define AV_OPTION_SET(av_stream, priv_data, name, value, avcodec) av_opt_set (priv_data, name, value, 0)
274 #define ALLOC_CODEC_CTX(ctx, av_codec, stream) \
275 avcodec_get_context_defaults3(av_st->codec, av_codec); \
277 #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec)
280 #define AV_REGISTER_ALL av_register_all();
281 #define AVCODEC_REGISTER_ALL avcodec_register_all();
282 #define AV_FILENAME filename
283 #define AV_SET_FILENAME(oc, f) snprintf(oc->AV_FILENAME, sizeof(oc->AV_FILENAME), "%s", f)
284 #define MY_INPUT_BUFFER_PADDING_SIZE FF_INPUT_BUFFER_PADDING_SIZE
285 #define AV_ALLOCATE_FRAME() avcodec_alloc_frame()
286 #define AV_ALLOCATE_IMAGE(av_frame, pix_fmt, width, height) \
287 avpicture_alloc((AVPicture *) av_frame, pix_fmt, width, height)
288 #define AV_RESET_FRAME(av_frame) avcodec_get_frame_defaults(av_frame)
289 #define AV_FREE_FRAME(av_frame) avcodec_free_frame(av_frame)
290 #define AV_FREE_PACKET(av_packet) av_free_packet(av_packet)
291 #define AV_FREE_CONTEXT(av_context) avcodec_close(av_context)
292 #define AV_GET_CODEC_TYPE(av_stream) av_stream->codec->codec_type
293 #define AV_FIND_DECODER_CODEC_ID(av_stream) av_stream->codec->codec_id
294 #define AV_GET_CODEC_CONTEXT(av_stream, av_codec) av_stream->codec
295 #define AV_GET_CODEC_PAR_CONTEXT(av_stream, av_codec) av_stream->codec
296 #define AV_GET_CODEC_FROM_STREAM(av_stream, codec_in ) codec_in = av_stream->codec;
297 #define AV_GET_CODEC_ATTRIBUTES(av_stream, av_context) av_context
298 #define AV_GET_CODEC_PIXEL_FORMAT(av_stream, av_context) av_context->pix_fmt
299 #define AV_GET_SAMPLE_FORMAT(av_stream, av_context) av_context->sample_fmt
300 #define AV_GET_IMAGE_SIZE(pix_fmt, width, height) avpicture_get_size(pix_fmt, width, height)
301 #define AV_COPY_PICTURE_DATA(av_frame, buffer, pix_fmt, width, height) \
302 avpicture_fill((AVPicture *) av_frame, buffer, pix_fmt, width, height)
303 #define AV_OUTPUT_CONTEXT(output_context, path) oc = avformat_alloc_context()
304 #define AV_OPTION_FIND(priv_data, name) av_opt_find(priv_data, name, NULL, 0, 0)
305 #define AV_OPTION_SET(av_stream, priv_data, name, value, avcodec) av_opt_set (priv_data, name, value, 0)
306 #define ALLOC_CODEC_CTX(ctx, av_codec, stream) \
307 avcodec_get_context_defaults3(stream->codec, av_codec); \
309 #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec)
#define AV_ERROR_MAX_STRING_SIZE
Header file that includes the version number of libopenshot.