OpenShot Library | libopenshot  0.2.2
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  * @section LICENSE
7  *
8  * Copyright (c) 2008-2014 OpenShot Studios, LLC
9  * <http://www.openshotstudios.com/>. This file is part of
10  * OpenShot Library (libopenshot), an open-source project dedicated to
11  * delivering high quality video editing and animation solutions to the
12  * world. For more information visit <http://www.openshot.org/>.
13  *
14  * OpenShot Library (libopenshot) is free software: you can redistribute it
15  * and/or modify it under the terms of the GNU Lesser General Public License
16  * as published by the Free Software Foundation, either version 3 of the
17  * License, or (at your option) any later version.
18  *
19  * OpenShot Library (libopenshot) is distributed in the hope that it will be
20  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 #ifndef OPENSHOT_FFMPEG_UTILITIES_H
29 #define OPENSHOT_FFMPEG_UTILITIES_H
30 
31  // Required for libavformat to build on Windows
32  #ifndef INT64_C
33  #define INT64_C(c) (c ## LL)
34  #define UINT64_C(c) (c ## ULL)
35  #endif
36 
37  #ifndef IS_FFMPEG_3_2
38  #define IS_FFMPEG_3_2 (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101))
39  #endif
40 
41  // Include the FFmpeg headers
42  extern "C" {
43  #include <libavcodec/avcodec.h>
44  #include <libavformat/avformat.h>
45  #include <libswscale/swscale.h>
46  // Change this to the first version swrescale works
47  #if (LIBAVFORMAT_VERSION_MAJOR >= 57)
48  #define USE_SW
49  #endif
50  #ifdef USE_SW
51  #include <libswresample/swresample.h>
52  #else
53  #include <libavresample/avresample.h>
54  #endif
55  #include <libavutil/mathematics.h>
56  #include <libavutil/pixfmt.h>
57  #include <libavutil/pixdesc.h>
58 
59  // libavutil changed folders at some point
60  #if LIBAVFORMAT_VERSION_MAJOR >= 53
61  #include <libavutil/opt.h>
62  #else
63  #include <libavcodec/opt.h>
64  #endif
65 
66  // channel header refactored
67  #if LIBAVFORMAT_VERSION_MAJOR >= 54
68  #include <libavutil/channel_layout.h>
69  #endif
70 
71  #if IS_FFMPEG_3_2
72  #include "libavutil/imgutils.h"
73  #endif
74  }
75 
76  // This was removed from newer versions of FFmpeg (but still used in libopenshot)
77  #ifndef AVCODEC_MAX_AUDIO_FRAME_SIZE
78  #define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio
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  #define AUDIO_PACKET_ENCODING_SIZE 768000 // 48khz * S16 (2 bytes) * max channels (8)
85  #endif
86 
87  // This wraps an unsafe C macro to be C++ compatible function
88  static const std::string av_make_error_string(int errnum)
89  {
90  char errbuf[AV_ERROR_MAX_STRING_SIZE];
91  av_strerror(errnum, errbuf, AV_ERROR_MAX_STRING_SIZE);
92  std::string errstring(errbuf);
93  return errstring;
94  }
95 
96  // Redefine the C macro to use our new C++ function
97  #undef av_err2str
98  #define av_err2str(errnum) av_make_error_string(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 
117  #ifdef USE_SW
118  #define SWR_CONVERT(ctx, out, linesize, out_count, in, linesize2, in_count) \
119  swr_convert(ctx, out, out_count, (const uint8_t **)in, in_count)
120  #define SWR_ALLOC() swr_alloc()
121  #define SWR_CLOSE(ctx) {}
122  #define SWR_FREE(ctx) swr_free(ctx)
123  #define SWR_INIT(ctx) swr_init(ctx)
124  #define SWRCONTEXT SwrContext
125  #else
126  #define SWR_CONVERT(ctx, out, linesize, out_count, in, linesize2, in_count) \
127  avresample_convert(ctx, out, linesize, out_count, (uint8_t **)in, linesize2, in_count)
128  #define SWR_ALLOC() avresample_alloc_context()
129  #define SWR_CLOSE(ctx) avresample_close(ctx)
130  #define SWR_FREE(ctx) avresample_free(ctx)
131  #define SWR_INIT(ctx) avresample_open(ctx)
132  #define SWRCONTEXT AVAudioResampleContext
133  #endif
134 
135 
136  #if (LIBAVFORMAT_VERSION_MAJOR >= 58)
137  #define AV_REGISTER_ALL
138  #define AVCODEC_REGISTER_ALL
139  #define AV_FILENAME url
140  #define MY_INPUT_BUFFER_PADDING_SIZE AV_INPUT_BUFFER_PADDING_SIZE
141  #define AV_ALLOCATE_FRAME() av_frame_alloc()
142  #define AV_ALLOCATE_IMAGE(av_frame, pix_fmt, width, height) av_image_alloc(av_frame->data, av_frame->linesize, width, height, pix_fmt, 1)
143  #define AV_RESET_FRAME(av_frame) av_frame_unref(av_frame)
144  #define AV_FREE_FRAME(av_frame) av_frame_free(av_frame)
145  #define AV_FREE_PACKET(av_packet) av_packet_unref(av_packet)
146  #define AV_FREE_CONTEXT(av_context) avcodec_free_context(&av_context)
147  #define AV_GET_CODEC_TYPE(av_stream) av_stream->codecpar->codec_type
148  #define AV_FIND_DECODER_CODEC_ID(av_stream) av_stream->codecpar->codec_id
149  auto AV_GET_CODEC_CONTEXT = [](AVStream* av_stream, AVCodec* av_codec) { \
150  AVCodecContext *context = avcodec_alloc_context3(av_codec); \
151  avcodec_parameters_to_context(context, av_stream->codecpar); \
152  return context; \
153  };
154  #define AV_GET_CODEC_PAR_CONTEXT(av_stream, av_codec) av_codec;
155  #define AV_GET_CODEC_FROM_STREAM(av_stream,codec_in)
156  #define AV_GET_CODEC_ATTRIBUTES(av_stream, av_context) av_stream->codecpar
157  #define AV_GET_CODEC_PIXEL_FORMAT(av_stream, av_context) (AVPixelFormat) av_stream->codecpar->format
158  #define AV_GET_SAMPLE_FORMAT(av_stream, av_context) av_stream->codecpar->format
159  #define AV_GET_IMAGE_SIZE(pix_fmt, width, height) av_image_get_buffer_size(pix_fmt, width, height, 1)
160  #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)
161  #define AV_OUTPUT_CONTEXT(output_context, path) avformat_alloc_output_context2( output_context, NULL, NULL, path)
162  #define AV_OPTION_FIND(priv_data, name) av_opt_find(priv_data, name, NULL, 0, 0)
163  #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);
164  #define AV_FORMAT_NEW_STREAM(oc, st_codec, av_codec, av_st) av_st = avformat_new_stream(oc, NULL);\
165  if (!av_st) \
166  throw OutOfMemory("Could not allocate memory for the video stream.", path); \
167  c = avcodec_alloc_context3(av_codec); \
168  st_codec = c; \
169  av_st->codecpar->codec_id = av_codec->id;
170  #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec) avcodec_parameters_from_context(av_stream->codecpar, av_codec);
171  #elif IS_FFMPEG_3_2
172  #define AV_REGISTER_ALL av_register_all();
173  #define AVCODEC_REGISTER_ALL avcodec_register_all();
174  #define AV_FILENAME filename
175  #define MY_INPUT_BUFFER_PADDING_SIZE FF_INPUT_BUFFER_PADDING_SIZE
176  #define AV_ALLOCATE_FRAME() av_frame_alloc()
177  #define AV_ALLOCATE_IMAGE(av_frame, pix_fmt, width, height) av_image_alloc(av_frame->data, av_frame->linesize, width, height, pix_fmt, 1)
178  #define AV_RESET_FRAME(av_frame) av_frame_unref(av_frame)
179  #define AV_FREE_FRAME(av_frame) av_frame_free(av_frame)
180  #define AV_FREE_PACKET(av_packet) av_packet_unref(av_packet)
181  #define AV_FREE_CONTEXT(av_context) avcodec_free_context(&av_context)
182  #define AV_GET_CODEC_TYPE(av_stream) av_stream->codecpar->codec_type
183  #define AV_FIND_DECODER_CODEC_ID(av_stream) av_stream->codecpar->codec_id
184  auto AV_GET_CODEC_CONTEXT = [](AVStream* av_stream, AVCodec* av_codec) { \
185  AVCodecContext *context = avcodec_alloc_context3(av_codec); \
186  avcodec_parameters_to_context(context, av_stream->codecpar); \
187  return context; \
188  };
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) av_image_get_buffer_size(pix_fmt, width, height, 1)
195  #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)
196  #define AV_OUTPUT_CONTEXT(output_context, path) avformat_alloc_output_context2( output_context, NULL, NULL, path)
197  #define AV_OPTION_FIND(priv_data, name) av_opt_find(priv_data, name, NULL, 0, 0)
198  #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);
199  #define AV_FORMAT_NEW_STREAM(oc, st_codec, av_codec, av_st) av_st = avformat_new_stream(oc, NULL);\
200  if (!av_st) \
201  throw OutOfMemory("Could not allocate memory for the video stream.", path); \
202  c = avcodec_alloc_context3(av_codec); \
203  st_codec = c; \
204  av_st->codecpar->codec_id = av_codec->id;
205  #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec) avcodec_parameters_from_context(av_stream->codecpar, av_codec);
206  #elif LIBAVFORMAT_VERSION_MAJOR >= 55
207  #define AV_REGISTER_ALL av_register_all();
208  #define AVCODEC_REGISTER_ALL avcodec_register_all();
209  #define AV_FILENAME filename
210  #define MY_INPUT_BUFFER_PADDING_SIZE FF_INPUT_BUFFER_PADDING_SIZE
211  #define AV_ALLOCATE_FRAME() av_frame_alloc()
212  #define AV_ALLOCATE_IMAGE(av_frame, pix_fmt, width, height) avpicture_alloc((AVPicture *) av_frame, pix_fmt, width, height)
213  #define AV_RESET_FRAME(av_frame) av_frame_unref(av_frame)
214  #define AV_FREE_FRAME(av_frame) av_frame_free(av_frame)
215  #define AV_FREE_PACKET(av_packet) av_packet_unref(av_packet)
216  #define AV_FREE_CONTEXT(av_context) avcodec_close(av_context)
217  #define AV_GET_CODEC_TYPE(av_stream) av_stream->codec->codec_type
218  #define AV_FIND_DECODER_CODEC_ID(av_stream) av_stream->codec->codec_id
219  #define AV_GET_CODEC_CONTEXT(av_stream, av_codec) av_stream->codec
220  #define AV_GET_CODEC_PAR_CONTEXT(av_stream, av_codec) av_stream->codec
221  #define AV_GET_CODEC_FROM_STREAM(av_stream, codec_in) codec_in = av_stream->codec;
222  #define AV_GET_CODEC_ATTRIBUTES(av_stream, av_context) av_context
223  #define AV_GET_CODEC_PIXEL_FORMAT(av_stream, av_context) av_context->pix_fmt
224  #define AV_GET_SAMPLE_FORMAT(av_stream, av_context) av_context->sample_fmt
225  #define AV_GET_IMAGE_SIZE(pix_fmt, width, height) avpicture_get_size(pix_fmt, width, height)
226  #define AV_COPY_PICTURE_DATA(av_frame, buffer, pix_fmt, width, height) avpicture_fill((AVPicture *) av_frame, buffer, pix_fmt, width, height)
227  #define AV_OUTPUT_CONTEXT(output_context, path) oc = avformat_alloc_context()
228  #define AV_OPTION_FIND(priv_data, name) av_opt_find(priv_data, name, NULL, 0, 0)
229  #define AV_OPTION_SET(av_stream, priv_data, name, value, avcodec) av_opt_set (priv_data, name, value, 0)
230  #define AV_FORMAT_NEW_STREAM( oc, av_context, av_codec, av_st) av_st = avformat_new_stream(oc, av_codec); \
231  if (!av_st) \
232  throw OutOfMemory("Could not allocate memory for the video stream.", path); \
233  avcodec_get_context_defaults3(av_st->codec, av_codec); \
234  c = av_st->codec;
235  #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec)
236  #else
237  #define AV_REGISTER_ALL av_register_all();
238  #define AVCODEC_REGISTER_ALL avcodec_register_all();
239  #define AV_FILENAME filename
240  #define MY_INPUT_BUFFER_PADDING_SIZE FF_INPUT_BUFFER_PADDING_SIZE
241  #define AV_ALLOCATE_FRAME() avcodec_alloc_frame()
242  #define AV_ALLOCATE_IMAGE(av_frame, pix_fmt, width, height) avpicture_alloc((AVPicture *) av_frame, pix_fmt, width, height)
243  #define AV_RESET_FRAME(av_frame) avcodec_get_frame_defaults(av_frame)
244  #define AV_FREE_FRAME(av_frame) avcodec_free_frame(av_frame)
245  #define AV_FREE_PACKET(av_packet) av_free_packet(av_packet)
246  #define AV_FREE_CONTEXT(av_context) avcodec_close(av_context)
247  #define AV_GET_CODEC_TYPE(av_stream) av_stream->codec->codec_type
248  #define AV_FIND_DECODER_CODEC_ID(av_stream) av_stream->codec->codec_id
249  #define AV_GET_CODEC_CONTEXT(av_stream, av_codec) av_stream->codec
250  #define AV_GET_CODEC_PAR_CONTEXT(av_stream, av_codec) av_stream->codec
251  #define AV_GET_CODEC_FROM_STREAM(av_stream, codec_in ) codec_in = av_stream->codec;
252  #define AV_GET_CODEC_ATTRIBUTES(av_stream, av_context) av_context
253  #define AV_GET_CODEC_PIXEL_FORMAT(av_stream, av_context) av_context->pix_fmt
254  #define AV_GET_SAMPLE_FORMAT(av_stream, av_context) av_context->sample_fmt
255  #define AV_GET_IMAGE_SIZE(pix_fmt, width, height) avpicture_get_size(pix_fmt, width, height)
256  #define AV_COPY_PICTURE_DATA(av_frame, buffer, pix_fmt, width, height) avpicture_fill((AVPicture *) av_frame, buffer, pix_fmt, width, height)
257  #define AV_OUTPUT_CONTEXT(output_context, path) oc = avformat_alloc_context()
258  #define AV_OPTION_FIND(priv_data, name) av_opt_find(priv_data, name, NULL, 0, 0)
259  #define AV_OPTION_SET(av_stream, priv_data, name, value, avcodec) av_opt_set (priv_data, name, value, 0)
260  #define AV_FORMAT_NEW_STREAM( oc, av_context, av_codec, av_st) av_st = avformat_new_stream(oc, av_codec); \
261  if (!av_st) \
262  throw OutOfMemory("Could not allocate memory for the video stream.", path); \
263  avcodec_get_context_defaults3(av_st->codec, av_codec); \
264  c = av_st->codec;
265  #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec)
266  #endif
267 
268 
269 #endif
AV_GET_CODEC_CONTEXT
auto AV_GET_CODEC_CONTEXT
Definition: FFmpegUtilities.h:184
AV_ERROR_MAX_STRING_SIZE
#define AV_ERROR_MAX_STRING_SIZE
Definition: FFmpegUtilities.h:81