OpenShot Library | libopenshot  0.3.3
FFmpegUtilities.h
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2024 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #ifndef OPENSHOT_FFMPEG_UTILITIES_H
14 #define OPENSHOT_FFMPEG_UTILITIES_H
15 
16 #include "OpenShotVersion.h" // For FFMPEG_USE_SWRESAMPLE
17 
18 // Required for libavformat to build on Windows
19 #ifndef INT64_C
20 #define INT64_C(c) (c ## LL)
21 #define UINT64_C(c) (c ## ULL)
22 #endif
23 
24 #ifndef IS_FFMPEG_3_2
25 #define IS_FFMPEG_3_2 (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101))
26 #endif
27 
28 #ifndef USE_HW_ACCEL
29 #define USE_HW_ACCEL (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 107, 100))
30 #endif
31 
32 #ifndef USE_SW
33 #define USE_SW FFMPEG_USE_SWRESAMPLE
34 #endif
35 
36 #define HAVE_CH_LAYOUT (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
37 
38 // Include the FFmpeg headers
39 extern "C" {
40  #include <libavcodec/avcodec.h>
41  #include <libavformat/avformat.h>
42 
43 #if (LIBAVFORMAT_VERSION_MAJOR >= 57)
44  #include <libavutil/hwcontext.h> //PM
45 #endif
46  #include <libswscale/swscale.h>
47 
48 #if USE_SW
49  #include <libswresample/swresample.h>
50 #else
51  #include <libavresample/avresample.h>
52 #endif
53 
54  #include <libavutil/mathematics.h>
55  #include <libavutil/pixfmt.h>
56  #include <libavutil/pixdesc.h>
57 
58  // libavutil changed folders at some point
59 #if LIBAVFORMAT_VERSION_MAJOR >= 53
60  #include <libavutil/opt.h>
61 #else
62  #include <libavcodec/opt.h>
63 #endif
64 
65  // channel header refactored
66 #if LIBAVFORMAT_VERSION_MAJOR >= 54
67  #include <libavutil/channel_layout.h>
68 #endif
69 
70 #if IS_FFMPEG_3_2
71  #include "libavutil/imgutils.h"
72 #endif
73 }
74 
75 // This was removed from newer versions of FFmpeg (but still used in libopenshot)
76 #ifndef AVCODEC_MAX_AUDIO_FRAME_SIZE
77  // 1 second of 48khz 32bit audio
78  #define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000
79 #endif
80 #ifndef AV_ERROR_MAX_STRING_SIZE
81  #define AV_ERROR_MAX_STRING_SIZE 64
82 #endif
83 #ifndef AUDIO_PACKET_ENCODING_SIZE
84  // 48khz * S16 (2 bytes) * max channels (8)
85  #define AUDIO_PACKET_ENCODING_SIZE 768000
86 #endif
87 
88 // This wraps an unsafe C macro to be C++ compatible function
89 inline static const std::string av_err2string(int errnum)
90 {
91  char errbuf[AV_ERROR_MAX_STRING_SIZE];
92  av_strerror(errnum, errbuf, AV_ERROR_MAX_STRING_SIZE);
93  return static_cast<std::string>(errbuf);
94 }
95 
96 // Redefine the C macro to use our new C++ function
97 #undef av_err2str
98 #define av_err2str(errnum) av_err2string(errnum).c_str()
99 
100 // Define this for compatibility
101 #ifndef PixelFormat
102  #define PixelFormat AVPixelFormat
103 #endif
104 #ifndef PIX_FMT_RGBA
105  #define PIX_FMT_RGBA AV_PIX_FMT_RGBA
106 #endif
107 #ifndef PIX_FMT_NONE
108  #define PIX_FMT_NONE AV_PIX_FMT_NONE
109 #endif
110 #ifndef PIX_FMT_RGB24
111  #define PIX_FMT_RGB24 AV_PIX_FMT_RGB24
112 #endif
113 #ifndef PIX_FMT_YUV420P
114  #define PIX_FMT_YUV420P AV_PIX_FMT_YUV420P
115 #endif
116 #ifndef PIX_FMT_YUV444P
117  #define PIX_FMT_YUV444P AV_PIX_FMT_YUV444P
118 #endif
119 
120 // Does ffmpeg pixel format contain an alpha channel?
121 inline static bool ffmpeg_has_alpha(PixelFormat pix_fmt) {
122  const AVPixFmtDescriptor *fmt_desc = av_pix_fmt_desc_get(pix_fmt);
123  return bool(fmt_desc->flags & AV_PIX_FMT_FLAG_ALPHA);
124 }
125 
126 // FFmpeg's libavutil/common.h defines an RSHIFT incompatible with Ruby's
127 // definition in ruby/config.h, so we move it to FF_RSHIFT
128 #ifdef RSHIFT
129  #define FF_RSHIFT(a, b) RSHIFT(a, b)
130  #undef RSHIFT
131 #endif
132 
133 // libswresample/libavresample API switching
134 #if USE_SW
135  #define SWR_CONVERT(ctx, out, linesize, out_count, in, linesize2, in_count) \
136  swr_convert(ctx, out, out_count, (const uint8_t **)in, in_count)
137  #define SWR_ALLOC() swr_alloc()
138  #define SWR_CLOSE(ctx) {}
139  #define SWR_FREE(ctx) swr_free(ctx)
140  #define SWR_INIT(ctx) swr_init(ctx)
141  #define SWRCONTEXT SwrContext
142 
143 #else
144  #define SWR_CONVERT(ctx, out, linesize, out_count, in, linesize2, in_count) \
145  avresample_convert(ctx, out, linesize, out_count, (uint8_t **)in, linesize2, in_count)
146  #define SWR_ALLOC() avresample_alloc_context()
147  #define SWR_CLOSE(ctx) avresample_close(ctx)
148  #define SWR_FREE(ctx) avresample_free(ctx)
149  #define SWR_INIT(ctx) avresample_open(ctx)
150  #define SWRCONTEXT AVAudioResampleContext
151 #endif
152 
153 
154 #if (LIBAVFORMAT_VERSION_MAJOR >= 58)
155  #define AV_REGISTER_ALL
156  #define AVCODEC_REGISTER_ALL
157  #define AV_FILENAME url
158  #define AV_SET_FILENAME(oc, f) oc->AV_FILENAME = av_strdup(f)
159  #define MY_INPUT_BUFFER_PADDING_SIZE AV_INPUT_BUFFER_PADDING_SIZE
160  #define AV_ALLOCATE_FRAME() av_frame_alloc()
161  #define AV_ALLOCATE_IMAGE(av_frame, pix_fmt, width, height) \
162  av_image_alloc(av_frame->data, av_frame->linesize, width, height, pix_fmt, 1)
163  #define AV_RESET_FRAME(av_frame) av_frame_unref(av_frame)
164  #define AV_FREE_FRAME(av_frame) av_frame_free(av_frame)
165  #define AV_FREE_PACKET(av_packet) av_packet_unref(av_packet)
166  #define AV_FREE_CONTEXT(av_context) avcodec_free_context(&av_context)
167  #define AV_GET_CODEC_TYPE(av_stream) av_stream->codecpar->codec_type
168  #define AV_FIND_DECODER_CODEC_ID(av_stream) av_stream->codecpar->codec_id
169  #define AV_GET_CODEC_CONTEXT(av_stream, av_codec) \
170  ({ AVCodecContext *context = avcodec_alloc_context3(av_codec); \
171  avcodec_parameters_to_context(context, av_stream->codecpar); \
172  context; })
173  #define AV_GET_CODEC_PAR_CONTEXT(av_stream, av_codec) av_codec;
174  #define AV_GET_CODEC_FROM_STREAM(av_stream,codec_in)
175  #define AV_GET_CODEC_ATTRIBUTES(av_stream, av_context) av_stream->codecpar
176  #define AV_GET_CODEC_PIXEL_FORMAT(av_stream, av_context) (AVPixelFormat) av_stream->codecpar->format
177  #define AV_GET_SAMPLE_FORMAT(av_stream, av_context) av_stream->codecpar->format
178  #define AV_GET_IMAGE_SIZE(pix_fmt, width, height) \
179  av_image_get_buffer_size(pix_fmt, width, height, 1)
180  #define AV_COPY_PICTURE_DATA(av_frame, buffer, pix_fmt, width, height) \
181  av_image_fill_arrays(av_frame->data, av_frame->linesize, buffer, pix_fmt, width, height, 1)
182  #define AV_OUTPUT_CONTEXT(output_context, path) avformat_alloc_output_context2( output_context, NULL, NULL, path)
183  #define AV_OPTION_FIND(priv_data, name) av_opt_find(priv_data, name, NULL, 0, 0)
184  #define AV_OPTION_SET( av_stream, priv_data, name, value, avcodec) \
185  av_opt_set(priv_data, name, value, 0); \
186  avcodec_parameters_from_context(av_stream->codecpar, avcodec);
187  #define ALLOC_CODEC_CTX(ctx, codec, stream) \
188  ctx = avcodec_alloc_context3(codec);
189  #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec_ctx) \
190  avcodec_parameters_from_context(av_stream->codecpar, av_codec_ctx);
191 
192 #elif IS_FFMPEG_3_2
193  #define AV_REGISTER_ALL av_register_all();
194  #define AVCODEC_REGISTER_ALL avcodec_register_all();
195  #define AV_FILENAME filename
196  #define AV_SET_FILENAME(oc, f) snprintf(oc->AV_FILENAME, sizeof(oc->AV_FILENAME), "%s", f)
197  #define MY_INPUT_BUFFER_PADDING_SIZE FF_INPUT_BUFFER_PADDING_SIZE
198  #define AV_ALLOCATE_FRAME() av_frame_alloc()
199  #define AV_ALLOCATE_IMAGE(av_frame, pix_fmt, width, height) \
200  av_image_alloc(av_frame->data, av_frame->linesize, width, height, pix_fmt, 1)
201  #define AV_RESET_FRAME(av_frame) av_frame_unref(av_frame)
202  #define AV_FREE_FRAME(av_frame) av_frame_free(av_frame)
203  #define AV_FREE_PACKET(av_packet) av_packet_unref(av_packet)
204  #define AV_FREE_CONTEXT(av_context) avcodec_free_context(&av_context)
205  #define AV_GET_CODEC_TYPE(av_stream) av_stream->codecpar->codec_type
206  #define AV_FIND_DECODER_CODEC_ID(av_stream) av_stream->codecpar->codec_id
207  #define AV_GET_CODEC_CONTEXT(av_stream, av_codec) \
208  ({ AVCodecContext *context = avcodec_alloc_context3(av_codec); \
209  avcodec_parameters_to_context(context, av_stream->codecpar); \
210  context; })
211  #define AV_GET_CODEC_PAR_CONTEXT(av_stream, av_codec) av_codec;
212  #define AV_GET_CODEC_FROM_STREAM(av_stream,codec_in)
213  #define AV_GET_CODEC_ATTRIBUTES(av_stream, av_context) av_stream->codecpar
214  #define AV_GET_CODEC_PIXEL_FORMAT(av_stream, av_context) \
215  (AVPixelFormat) av_stream->codecpar->format
216  #define AV_GET_SAMPLE_FORMAT(av_stream, av_context) av_stream->codecpar->format
217  #define AV_GET_IMAGE_SIZE(pix_fmt, width, height) av_image_get_buffer_size(pix_fmt, width, height, 1)
218  #define AV_COPY_PICTURE_DATA(av_frame, buffer, pix_fmt, width, height) \
219  av_image_fill_arrays(av_frame->data, av_frame->linesize, buffer, pix_fmt, width, height, 1)
220  #define AV_OUTPUT_CONTEXT(output_context, path) \
221  avformat_alloc_output_context2( output_context, NULL, NULL, path)
222  #define AV_OPTION_FIND(priv_data, name) av_opt_find(priv_data, name, NULL, 0, 0)
223  #define AV_OPTION_SET( av_stream, priv_data, name, value, avcodec) \
224  av_opt_set(priv_data, name, value, 0); \
225  avcodec_parameters_from_context(av_stream->codecpar, avcodec);
226  #define ALLOC_CODEC_CTX(ctx, codec, stream) \
227  ctx = avcodec_alloc_context3(codec);
228  #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec) \
229  avcodec_parameters_from_context(av_stream->codecpar, av_codec);
230 
231 #elif LIBAVFORMAT_VERSION_MAJOR >= 55
232  #define AV_REGISTER_ALL av_register_all();
233  #define AVCODEC_REGISTER_ALL avcodec_register_all();
234  #define AV_FILENAME filename
235  #define AV_SET_FILENAME(oc, f) snprintf(oc->AV_FILENAME, sizeof(oc->AV_FILENAME), "%s", f)
236  #define MY_INPUT_BUFFER_PADDING_SIZE FF_INPUT_BUFFER_PADDING_SIZE
237  #define AV_ALLOCATE_FRAME() av_frame_alloc()
238  #define AV_ALLOCATE_IMAGE(av_frame, pix_fmt, width, height) \
239  avpicture_alloc((AVPicture *) av_frame, pix_fmt, width, height)
240  #define AV_RESET_FRAME(av_frame) av_frame_unref(av_frame)
241  #define AV_FREE_FRAME(av_frame) av_frame_free(av_frame)
242  #define AV_FREE_PACKET(av_packet) av_packet_unref(av_packet)
243  #define AV_FREE_CONTEXT(av_context) avcodec_close(av_context)
244  #define AV_GET_CODEC_TYPE(av_stream) av_stream->codec->codec_type
245  #define AV_FIND_DECODER_CODEC_ID(av_stream) av_stream->codec->codec_id
246  #define AV_GET_CODEC_CONTEXT(av_stream, av_codec) av_stream->codec
247  #define AV_GET_CODEC_PAR_CONTEXT(av_stream, av_codec) av_stream->codec
248  #define AV_GET_CODEC_FROM_STREAM(av_stream, codec_in) codec_in = av_stream->codec;
249  #define AV_GET_CODEC_ATTRIBUTES(av_stream, av_context) av_context
250  #define AV_GET_CODEC_PIXEL_FORMAT(av_stream, av_context) av_context->pix_fmt
251  #define AV_GET_SAMPLE_FORMAT(av_stream, av_context) av_context->sample_fmt
252  #define AV_GET_IMAGE_SIZE(pix_fmt, width, height) avpicture_get_size(pix_fmt, width, height)
253  #define AV_COPY_PICTURE_DATA(av_frame, buffer, pix_fmt, width, height) \
254  avpicture_fill((AVPicture *) av_frame, buffer, pix_fmt, width, height)
255  #define AV_OUTPUT_CONTEXT(output_context, path) oc = avformat_alloc_context()
256  #define AV_OPTION_FIND(priv_data, name) av_opt_find(priv_data, name, NULL, 0, 0)
257  #define AV_OPTION_SET(av_stream, priv_data, name, value, avcodec) av_opt_set (priv_data, name, value, 0)
258  #define ALLOC_CODEC_CTX(ctx, av_codec, stream) \
259  avcodec_get_context_defaults3(av_st->codec, av_codec); \
260  ctx = av_st->codec;
261  #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec)
262 
263 #else
264  #define AV_REGISTER_ALL av_register_all();
265  #define AVCODEC_REGISTER_ALL avcodec_register_all();
266  #define AV_FILENAME filename
267  #define AV_SET_FILENAME(oc, f) snprintf(oc->AV_FILENAME, sizeof(oc->AV_FILENAME), "%s", f)
268  #define MY_INPUT_BUFFER_PADDING_SIZE FF_INPUT_BUFFER_PADDING_SIZE
269  #define AV_ALLOCATE_FRAME() avcodec_alloc_frame()
270  #define AV_ALLOCATE_IMAGE(av_frame, pix_fmt, width, height) \
271  avpicture_alloc((AVPicture *) av_frame, pix_fmt, width, height)
272  #define AV_RESET_FRAME(av_frame) avcodec_get_frame_defaults(av_frame)
273  #define AV_FREE_FRAME(av_frame) avcodec_free_frame(av_frame)
274  #define AV_FREE_PACKET(av_packet) av_free_packet(av_packet)
275  #define AV_FREE_CONTEXT(av_context) avcodec_close(av_context)
276  #define AV_GET_CODEC_TYPE(av_stream) av_stream->codec->codec_type
277  #define AV_FIND_DECODER_CODEC_ID(av_stream) av_stream->codec->codec_id
278  #define AV_GET_CODEC_CONTEXT(av_stream, av_codec) av_stream->codec
279  #define AV_GET_CODEC_PAR_CONTEXT(av_stream, av_codec) av_stream->codec
280  #define AV_GET_CODEC_FROM_STREAM(av_stream, codec_in ) codec_in = av_stream->codec;
281  #define AV_GET_CODEC_ATTRIBUTES(av_stream, av_context) av_context
282  #define AV_GET_CODEC_PIXEL_FORMAT(av_stream, av_context) av_context->pix_fmt
283  #define AV_GET_SAMPLE_FORMAT(av_stream, av_context) av_context->sample_fmt
284  #define AV_GET_IMAGE_SIZE(pix_fmt, width, height) avpicture_get_size(pix_fmt, width, height)
285  #define AV_COPY_PICTURE_DATA(av_frame, buffer, pix_fmt, width, height) \
286  avpicture_fill((AVPicture *) av_frame, buffer, pix_fmt, width, height)
287  #define AV_OUTPUT_CONTEXT(output_context, path) oc = avformat_alloc_context()
288  #define AV_OPTION_FIND(priv_data, name) av_opt_find(priv_data, name, NULL, 0, 0)
289  #define AV_OPTION_SET(av_stream, priv_data, name, value, avcodec) av_opt_set (priv_data, name, value, 0)
290  #define ALLOC_CODEC_CTX(ctx, av_codec, stream) \
291  avcodec_get_context_defaults3(stream->codec, av_codec); \
292  ctx = stream->codec;
293  #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec)
294 #endif
295 
296 
297 #endif // OPENSHOT_FFMPEG_UTILITIES_H
PixelFormat
#define PixelFormat
Definition: FFmpegUtilities.h:102
OpenShotVersion.h
Header file that includes the version number of libopenshot.
AV_ERROR_MAX_STRING_SIZE
#define AV_ERROR_MAX_STRING_SIZE
Definition: FFmpegUtilities.h:81