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