OpenShot Library | libopenshot  0.2.5
FFmpegUtilities.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header file for FFmpegUtilities
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC
12  * <http://www.openshotstudios.com/>. This file is part of
13  * OpenShot Library (libopenshot), an open-source project dedicated to
14  * delivering high quality video editing and animation solutions to the
15  * world. For more information visit <http://www.openshot.org/>.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #ifndef OPENSHOT_FFMPEG_UTILITIES_H
32 #define OPENSHOT_FFMPEG_UTILITIES_H
33 
34  // Required for libavformat to build on Windows
35  #ifndef INT64_C
36  #define INT64_C(c) (c ## LL)
37  #define UINT64_C(c) (c ## ULL)
38  #endif
39 
40  #ifndef IS_FFMPEG_3_2
41  #define IS_FFMPEG_3_2 (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101))
42  #endif
43 
44  #ifndef HAVE_HW_ACCEL
45  #define HAVE_HW_ACCEL (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 107, 100))
46  #endif
47 
48  // Include the FFmpeg headers
49  extern "C" {
50  #include <libavcodec/avcodec.h>
51  #include <libavformat/avformat.h>
52  #if (LIBAVFORMAT_VERSION_MAJOR >= 57)
53  #include <libavutil/hwcontext.h> //PM
54  #endif
55  #include <libswscale/swscale.h>
56  // Change this to the first version swrescale works
57  #if (LIBAVFORMAT_VERSION_MAJOR >= 57)
58  #define USE_SW
59  #endif
60  #ifdef USE_SW
61  #include <libswresample/swresample.h>
62  #else
63  #include <libavresample/avresample.h>
64  #endif
65  #include <libavutil/mathematics.h>
66  #include <libavutil/pixfmt.h>
67  #include <libavutil/pixdesc.h>
68 
69  // libavutil changed folders at some point
70  #if LIBAVFORMAT_VERSION_MAJOR >= 53
71  #include <libavutil/opt.h>
72  #else
73  #include <libavcodec/opt.h>
74  #endif
75 
76  // channel header refactored
77  #if LIBAVFORMAT_VERSION_MAJOR >= 54
78  #include <libavutil/channel_layout.h>
79  #endif
80 
81  #if IS_FFMPEG_3_2
82  #include "libavutil/imgutils.h"
83  #endif
84  }
85 
86  // This was removed from newer versions of FFmpeg (but still used in libopenshot)
87  #ifndef AVCODEC_MAX_AUDIO_FRAME_SIZE
88  #define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio
89  #endif
90  #ifndef AV_ERROR_MAX_STRING_SIZE
91  #define AV_ERROR_MAX_STRING_SIZE 64
92  #endif
93  #ifndef AUDIO_PACKET_ENCODING_SIZE
94  #define AUDIO_PACKET_ENCODING_SIZE 768000 // 48khz * S16 (2 bytes) * max channels (8)
95  #endif
96 
97  // This wraps an unsafe C macro to be C++ compatible function
98  inline static const std::string av_make_error_string(int errnum)
99  {
100  char errbuf[AV_ERROR_MAX_STRING_SIZE];
101  av_strerror(errnum, errbuf, AV_ERROR_MAX_STRING_SIZE);
102  return (std::string)errbuf;
103  }
104 
105  // Redefine the C macro to use our new C++ function
106  #undef av_err2str
107  #define av_err2str(errnum) av_make_error_string(errnum).c_str()
108 
109  // Define this for compatibility
110  #ifndef PixelFormat
111  #define PixelFormat AVPixelFormat
112  #endif
113  #ifndef PIX_FMT_RGBA
114  #define PIX_FMT_RGBA AV_PIX_FMT_RGBA
115  #endif
116  #ifndef PIX_FMT_NONE
117  #define PIX_FMT_NONE AV_PIX_FMT_NONE
118  #endif
119  #ifndef PIX_FMT_RGB24
120  #define PIX_FMT_RGB24 AV_PIX_FMT_RGB24
121  #endif
122  #ifndef PIX_FMT_YUV420P
123  #define PIX_FMT_YUV420P AV_PIX_FMT_YUV420P
124  #endif
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  #ifdef USE_SW
134  #define SWR_CONVERT(ctx, out, linesize, out_count, in, linesize2, in_count) \
135  swr_convert(ctx, out, out_count, (const uint8_t **)in, in_count)
136  #define SWR_ALLOC() swr_alloc()
137  #define SWR_CLOSE(ctx) {}
138  #define SWR_FREE(ctx) swr_free(ctx)
139  #define SWR_INIT(ctx) swr_init(ctx)
140  #define SWRCONTEXT SwrContext
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) av_image_alloc(av_frame->data, av_frame->linesize, width, height, pix_fmt, 1)
160  #define AV_RESET_FRAME(av_frame) av_frame_unref(av_frame)
161  #define AV_FREE_FRAME(av_frame) av_frame_free(av_frame)
162  #define AV_FREE_PACKET(av_packet) av_packet_unref(av_packet)
163  #define AV_FREE_CONTEXT(av_context) avcodec_free_context(&av_context)
164  #define AV_GET_CODEC_TYPE(av_stream) av_stream->codecpar->codec_type
165  #define AV_FIND_DECODER_CODEC_ID(av_stream) av_stream->codecpar->codec_id
166  #define AV_GET_CODEC_CONTEXT(av_stream, av_codec) \
167  ({ AVCodecContext *context = avcodec_alloc_context3(av_codec); \
168  avcodec_parameters_to_context(context, av_stream->codecpar); \
169  context; })
170  #define AV_GET_CODEC_PAR_CONTEXT(av_stream, av_codec) av_codec;
171  #define AV_GET_CODEC_FROM_STREAM(av_stream,codec_in)
172  #define AV_GET_CODEC_ATTRIBUTES(av_stream, av_context) av_stream->codecpar
173  #define AV_GET_CODEC_PIXEL_FORMAT(av_stream, av_context) (AVPixelFormat) av_stream->codecpar->format
174  #define AV_GET_SAMPLE_FORMAT(av_stream, av_context) av_stream->codecpar->format
175  #define AV_GET_IMAGE_SIZE(pix_fmt, width, height) av_image_get_buffer_size(pix_fmt, width, height, 1)
176  #define AV_COPY_PICTURE_DATA(av_frame, buffer, pix_fmt, width, height) av_image_fill_arrays(av_frame->data, av_frame->linesize, buffer, pix_fmt, width, height, 1)
177  #define AV_OUTPUT_CONTEXT(output_context, path) avformat_alloc_output_context2( output_context, NULL, NULL, path)
178  #define AV_OPTION_FIND(priv_data, name) av_opt_find(priv_data, name, NULL, 0, 0)
179  #define AV_OPTION_SET( av_stream, priv_data, name, value, avcodec) av_opt_set(priv_data, name, value, 0); avcodec_parameters_from_context(av_stream->codecpar, avcodec);
180  #define AV_FORMAT_NEW_STREAM(oc, st_codec, av_codec, av_st) av_st = avformat_new_stream(oc, NULL);\
181  if (!av_st) \
182  throw OutOfMemory("Could not allocate memory for the video stream.", path); \
183  c = avcodec_alloc_context3(av_codec); \
184  st_codec = c; \
185  av_st->codecpar->codec_id = av_codec->id;
186  #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec) avcodec_parameters_from_context(av_stream->codecpar, av_codec);
187  #elif IS_FFMPEG_3_2
188  #define AV_REGISTER_ALL av_register_all();
189  #define AVCODEC_REGISTER_ALL avcodec_register_all();
190  #define AV_FILENAME filename
191  #define AV_SET_FILENAME(oc, f) snprintf(oc->AV_FILENAME, sizeof(oc->AV_FILENAME), "%s", f)
192  #define MY_INPUT_BUFFER_PADDING_SIZE FF_INPUT_BUFFER_PADDING_SIZE
193  #define AV_ALLOCATE_FRAME() av_frame_alloc()
194  #define AV_ALLOCATE_IMAGE(av_frame, pix_fmt, width, height) av_image_alloc(av_frame->data, av_frame->linesize, width, height, pix_fmt, 1)
195  #define AV_RESET_FRAME(av_frame) av_frame_unref(av_frame)
196  #define AV_FREE_FRAME(av_frame) av_frame_free(av_frame)
197  #define AV_FREE_PACKET(av_packet) av_packet_unref(av_packet)
198  #define AV_FREE_CONTEXT(av_context) avcodec_free_context(&av_context)
199  #define AV_GET_CODEC_TYPE(av_stream) av_stream->codecpar->codec_type
200  #define AV_FIND_DECODER_CODEC_ID(av_stream) av_stream->codecpar->codec_id
201  #define AV_GET_CODEC_CONTEXT(av_stream, av_codec) \
202  ({ AVCodecContext *context = avcodec_alloc_context3(av_codec); \
203  avcodec_parameters_to_context(context, av_stream->codecpar); \
204  context; })
205  #define AV_GET_CODEC_PAR_CONTEXT(av_stream, av_codec) av_codec;
206  #define AV_GET_CODEC_FROM_STREAM(av_stream,codec_in)
207  #define AV_GET_CODEC_ATTRIBUTES(av_stream, av_context) av_stream->codecpar
208  #define AV_GET_CODEC_PIXEL_FORMAT(av_stream, av_context) (AVPixelFormat) av_stream->codecpar->format
209  #define AV_GET_SAMPLE_FORMAT(av_stream, av_context) av_stream->codecpar->format
210  #define AV_GET_IMAGE_SIZE(pix_fmt, width, height) av_image_get_buffer_size(pix_fmt, width, height, 1)
211  #define AV_COPY_PICTURE_DATA(av_frame, buffer, pix_fmt, width, height) av_image_fill_arrays(av_frame->data, av_frame->linesize, buffer, pix_fmt, width, height, 1)
212  #define AV_OUTPUT_CONTEXT(output_context, path) avformat_alloc_output_context2( output_context, NULL, NULL, path)
213  #define AV_OPTION_FIND(priv_data, name) av_opt_find(priv_data, name, NULL, 0, 0)
214  #define AV_OPTION_SET( av_stream, priv_data, name, value, avcodec) av_opt_set(priv_data, name, value, 0); avcodec_parameters_from_context(av_stream->codecpar, avcodec);
215  #define AV_FORMAT_NEW_STREAM(oc, st_codec, av_codec, av_st) av_st = avformat_new_stream(oc, NULL);\
216  if (!av_st) \
217  throw OutOfMemory("Could not allocate memory for the video stream.", path); \
218  _Pragma ("GCC diagnostic push"); \
219  _Pragma ("GCC diagnostic ignored \"-Wdeprecated-declarations\""); \
220  avcodec_get_context_defaults3(av_st->codec, av_codec); \
221  c = av_st->codec; \
222  _Pragma ("GCC diagnostic pop"); \
223  st_codec = c;
224  #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec) avcodec_parameters_from_context(av_stream->codecpar, av_codec);
225  #elif LIBAVFORMAT_VERSION_MAJOR >= 55
226  #define AV_REGISTER_ALL av_register_all();
227  #define AVCODEC_REGISTER_ALL avcodec_register_all();
228  #define AV_FILENAME filename
229  #define AV_SET_FILENAME(oc, f) snprintf(oc->AV_FILENAME, sizeof(oc->AV_FILENAME), "%s", f)
230  #define MY_INPUT_BUFFER_PADDING_SIZE FF_INPUT_BUFFER_PADDING_SIZE
231  #define AV_ALLOCATE_FRAME() av_frame_alloc()
232  #define AV_ALLOCATE_IMAGE(av_frame, pix_fmt, width, height) avpicture_alloc((AVPicture *) av_frame, pix_fmt, width, height)
233  #define AV_RESET_FRAME(av_frame) av_frame_unref(av_frame)
234  #define AV_FREE_FRAME(av_frame) av_frame_free(av_frame)
235  #define AV_FREE_PACKET(av_packet) av_packet_unref(av_packet)
236  #define AV_FREE_CONTEXT(av_context) avcodec_close(av_context)
237  #define AV_GET_CODEC_TYPE(av_stream) av_stream->codec->codec_type
238  #define AV_FIND_DECODER_CODEC_ID(av_stream) av_stream->codec->codec_id
239  #define AV_GET_CODEC_CONTEXT(av_stream, av_codec) av_stream->codec
240  #define AV_GET_CODEC_PAR_CONTEXT(av_stream, av_codec) av_stream->codec
241  #define AV_GET_CODEC_FROM_STREAM(av_stream, codec_in) codec_in = av_stream->codec;
242  #define AV_GET_CODEC_ATTRIBUTES(av_stream, av_context) av_context
243  #define AV_GET_CODEC_PIXEL_FORMAT(av_stream, av_context) av_context->pix_fmt
244  #define AV_GET_SAMPLE_FORMAT(av_stream, av_context) av_context->sample_fmt
245  #define AV_GET_IMAGE_SIZE(pix_fmt, width, height) avpicture_get_size(pix_fmt, width, height)
246  #define AV_COPY_PICTURE_DATA(av_frame, buffer, pix_fmt, width, height) avpicture_fill((AVPicture *) av_frame, buffer, pix_fmt, width, height)
247  #define AV_OUTPUT_CONTEXT(output_context, path) oc = avformat_alloc_context()
248  #define AV_OPTION_FIND(priv_data, name) av_opt_find(priv_data, name, NULL, 0, 0)
249  #define AV_OPTION_SET(av_stream, priv_data, name, value, avcodec) av_opt_set (priv_data, name, value, 0)
250  #define AV_FORMAT_NEW_STREAM( oc, av_context, av_codec, av_st) av_st = avformat_new_stream(oc, av_codec); \
251  if (!av_st) \
252  throw OutOfMemory("Could not allocate memory for the video stream.", path); \
253  avcodec_get_context_defaults3(av_st->codec, av_codec); \
254  c = av_st->codec;
255  #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec)
256  #else
257  #define AV_REGISTER_ALL av_register_all();
258  #define AVCODEC_REGISTER_ALL avcodec_register_all();
259  #define AV_FILENAME filename
260  #define AV_SET_FILENAME(oc, f) snprintf(oc->AV_FILENAME, sizeof(oc->AV_FILENAME), "%s", f)
261  #define MY_INPUT_BUFFER_PADDING_SIZE FF_INPUT_BUFFER_PADDING_SIZE
262  #define AV_ALLOCATE_FRAME() avcodec_alloc_frame()
263  #define AV_ALLOCATE_IMAGE(av_frame, pix_fmt, width, height) avpicture_alloc((AVPicture *) av_frame, pix_fmt, width, height)
264  #define AV_RESET_FRAME(av_frame) avcodec_get_frame_defaults(av_frame)
265  #define AV_FREE_FRAME(av_frame) avcodec_free_frame(av_frame)
266  #define AV_FREE_PACKET(av_packet) av_free_packet(av_packet)
267  #define AV_FREE_CONTEXT(av_context) avcodec_close(av_context)
268  #define AV_GET_CODEC_TYPE(av_stream) av_stream->codec->codec_type
269  #define AV_FIND_DECODER_CODEC_ID(av_stream) av_stream->codec->codec_id
270  #define AV_GET_CODEC_CONTEXT(av_stream, av_codec) av_stream->codec
271  #define AV_GET_CODEC_PAR_CONTEXT(av_stream, av_codec) av_stream->codec
272  #define AV_GET_CODEC_FROM_STREAM(av_stream, codec_in ) codec_in = av_stream->codec;
273  #define AV_GET_CODEC_ATTRIBUTES(av_stream, av_context) av_context
274  #define AV_GET_CODEC_PIXEL_FORMAT(av_stream, av_context) av_context->pix_fmt
275  #define AV_GET_SAMPLE_FORMAT(av_stream, av_context) av_context->sample_fmt
276  #define AV_GET_IMAGE_SIZE(pix_fmt, width, height) avpicture_get_size(pix_fmt, width, height)
277  #define AV_COPY_PICTURE_DATA(av_frame, buffer, pix_fmt, width, height) avpicture_fill((AVPicture *) av_frame, buffer, pix_fmt, width, height)
278  #define AV_OUTPUT_CONTEXT(output_context, path) oc = avformat_alloc_context()
279  #define AV_OPTION_FIND(priv_data, name) av_opt_find(priv_data, name, NULL, 0, 0)
280  #define AV_OPTION_SET(av_stream, priv_data, name, value, avcodec) av_opt_set (priv_data, name, value, 0)
281  #define AV_FORMAT_NEW_STREAM( oc, av_context, av_codec, av_st) av_st = avformat_new_stream(oc, av_codec); \
282  if (!av_st) \
283  throw OutOfMemory("Could not allocate memory for the video stream.", path); \
284  avcodec_get_context_defaults3(av_st->codec, av_codec); \
285  c = av_st->codec;
286  #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec)
287  #endif
288 
289 
290 #endif
#define AV_ERROR_MAX_STRING_SIZE