OpenShot Library | libopenshot  0.2.0
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  #include <libavresample/avresample.h>
47  #include <libavutil/mathematics.h>
48  #include <libavutil/pixfmt.h>
49  #include <libavutil/pixdesc.h>
50 
51  // libavutil changed folders at some point
52  #if LIBAVFORMAT_VERSION_MAJOR >= 53
53  #include <libavutil/opt.h>
54  #else
55  #include <libavcodec/opt.h>
56  #endif
57 
58  // channel header refactored
59  #if LIBAVFORMAT_VERSION_MAJOR >= 54
60  #include <libavutil/channel_layout.h>
61  #endif
62 
63  #if IS_FFMPEG_3_2
64  #include "libavutil/imgutils.h"
65  #endif
66  }
67 
68  // This was removed from newer versions of FFmpeg (but still used in libopenshot)
69  #ifndef AVCODEC_MAX_AUDIO_FRAME_SIZE
70  #define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio
71  #endif
72  #ifndef AV_ERROR_MAX_STRING_SIZE
73  #define AV_ERROR_MAX_STRING_SIZE 64
74  #endif
75  #ifndef AUDIO_PACKET_ENCODING_SIZE
76  #define AUDIO_PACKET_ENCODING_SIZE 768000 // 48khz * S16 (2 bytes) * max channels (8)
77  #endif
78 
79  // This wraps an unsafe C macro to be C++ compatible function
80  static const std::string av_make_error_string(int errnum)
81  {
82  char errbuf[AV_ERROR_MAX_STRING_SIZE];
83  av_strerror(errnum, errbuf, AV_ERROR_MAX_STRING_SIZE);
84  std::string errstring(errbuf);
85  return errstring;
86  }
87 
88  // Redefine the C macro to use our new C++ function
89  #undef av_err2str
90  #define av_err2str(errnum) av_make_error_string(errnum).c_str()
91 
92  // Define this for compatibility
93  #ifndef PixelFormat
94  #define PixelFormat AVPixelFormat
95  #endif
96  #ifndef PIX_FMT_RGBA
97  #define PIX_FMT_RGBA AV_PIX_FMT_RGBA
98  #endif
99  #ifndef PIX_FMT_NONE
100  #define PIX_FMT_NONE AV_PIX_FMT_NONE
101  #endif
102  #ifndef PIX_FMT_RGB24
103  #define PIX_FMT_RGB24 AV_PIX_FMT_RGB24
104  #endif
105  #ifndef PIX_FMT_YUV420P
106  #define PIX_FMT_YUV420P AV_PIX_FMT_YUV420P
107  #endif
108 
109  #if IS_FFMPEG_3_2
110  #define AV_ALLOCATE_FRAME() av_frame_alloc()
111  #define AV_ALLOCATE_IMAGE(av_frame, pix_fmt, width, height) av_image_alloc(av_frame->data, av_frame->linesize, width, height, pix_fmt, 1)
112  #define AV_RESET_FRAME(av_frame) av_frame_unref(av_frame)
113  #define AV_FREE_FRAME(av_frame) av_frame_free(av_frame)
114  #define AV_FREE_PACKET(av_packet) av_packet_unref(av_packet)
115  #define AV_FREE_CONTEXT(av_context) avcodec_free_context(&av_context)
116  #define AV_GET_CODEC_TYPE(av_stream) av_stream->codecpar->codec_type
117  #define AV_FIND_DECODER_CODEC_ID(av_stream) av_stream->codecpar->codec_id
118  auto AV_GET_CODEC_CONTEXT = [](AVStream* av_stream, AVCodec* av_codec) { \
119  AVCodecContext *context = avcodec_alloc_context3(av_codec); \
120  avcodec_parameters_to_context(context, av_stream->codecpar); \
121  return context; \
122  };
123  #define AV_GET_CODEC_PAR_CONTEXT(av_stream, av_codec) av_codec;
124  #define AV_GET_CODEC_FROM_STREAM(av_stream,codec_in)
125  #define AV_GET_CODEC_ATTRIBUTES(av_stream, av_context) av_stream->codecpar
126  #define AV_GET_CODEC_PIXEL_FORMAT(av_stream, av_context) (AVPixelFormat) av_stream->codecpar->format
127  #define AV_GET_SAMPLE_FORMAT(av_stream, av_context) av_stream->codecpar->format
128  #define AV_GET_IMAGE_SIZE(pix_fmt, width, height) av_image_get_buffer_size(pix_fmt, width, height, 1)
129  #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)
130  #define AV_OUTPUT_CONTEXT(output_context, path) avformat_alloc_output_context2( output_context, NULL, NULL, path)
131  #define AV_OPTION_FIND(priv_data, name) av_opt_find(priv_data, name, NULL, 0, 0)
132  #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);
133  #define AV_FORMAT_NEW_STREAM(oc, st_codec, av_codec, av_st) av_st = avformat_new_stream(oc, NULL);\
134  if (!av_st) \
135  throw OutOfMemory("Could not allocate memory for the video stream.", path); \
136  c = avcodec_alloc_context3(av_codec); \
137  st_codec = c; \
138  av_st->codecpar->codec_id = av_codec->id;
139  #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec) avcodec_parameters_from_context(av_stream->codecpar, av_codec);
140  #elif LIBAVFORMAT_VERSION_MAJOR >= 55
141  #define AV_ALLOCATE_FRAME() av_frame_alloc()
142  #define AV_ALLOCATE_IMAGE(av_frame, pix_fmt, width, height) avpicture_alloc((AVPicture *) av_frame, pix_fmt, width, height)
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_close(av_context)
147  #define AV_GET_CODEC_TYPE(av_stream) av_stream->codec->codec_type
148  #define AV_FIND_DECODER_CODEC_ID(av_stream) av_stream->codec->codec_id
149  #define AV_GET_CODEC_CONTEXT(av_stream, av_codec) av_stream->codec
150  #define AV_GET_CODEC_PAR_CONTEXT(av_stream, av_codec) av_stream->codec
151  #define AV_GET_CODEC_FROM_STREAM(av_stream, codec_in) codec_in = av_stream->codec;
152  #define AV_GET_CODEC_ATTRIBUTES(av_stream, av_context) av_context
153  #define AV_GET_CODEC_PIXEL_FORMAT(av_stream, av_context) av_context->pix_fmt
154  #define AV_GET_SAMPLE_FORMAT(av_stream, av_context) av_context->sample_fmt
155  #define AV_GET_IMAGE_SIZE(pix_fmt, width, height) avpicture_get_size(pix_fmt, width, height)
156  #define AV_COPY_PICTURE_DATA(av_frame, buffer, pix_fmt, width, height) avpicture_fill((AVPicture *) av_frame, buffer, pix_fmt, width, height)
157  #define AV_OUTPUT_CONTEXT(output_context, path) oc = avformat_alloc_context()
158  #define AV_OPTION_FIND(priv_data, name) av_opt_find(priv_data, name, NULL, 0, 0)
159  #define AV_OPTION_SET(av_stream, priv_data, name, value, avcodec) av_opt_set (priv_data, name, value, 0)
160  #define AV_FORMAT_NEW_STREAM( oc, av_context, av_codec, av_st) av_st = avformat_new_stream(oc, av_codec); \
161  if (!av_st) \
162  throw OutOfMemory("Could not allocate memory for the video stream.", path); \
163  avcodec_get_context_defaults3(av_st->codec, av_codec); \
164  c = av_st->codec;
165  #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec)
166  #else
167  #define AV_ALLOCATE_FRAME() avcodec_alloc_frame()
168  #define AV_ALLOCATE_IMAGE(av_frame, pix_fmt, width, height) avpicture_alloc((AVPicture *) av_frame, pix_fmt, width, height)
169  #define AV_RESET_FRAME(av_frame) avcodec_get_frame_defaults(av_frame)
170  #define AV_FREE_FRAME(av_frame) avcodec_free_frame(av_frame)
171  #define AV_FREE_PACKET(av_packet) av_free_packet(av_packet)
172  #define AV_FREE_CONTEXT(av_context) avcodec_close(av_context)
173  #define AV_GET_CODEC_TYPE(av_stream) av_stream->codec->codec_type
174  #define AV_FIND_DECODER_CODEC_ID(av_stream) av_stream->codec->codec_id
175  #define AV_GET_CODEC_CONTEXT(av_stream, av_codec) av_stream->codec
176  #define AV_GET_CODEC_PAR_CONTEXT(av_stream, av_codec) av_stream->codec
177  #define AV_GET_CODEC_FROM_STREAM(av_stream, codec_in ) codec_in = av_stream->codec;
178  #define AV_GET_CODEC_ATTRIBUTES(av_stream, av_context) av_context
179  #define AV_GET_CODEC_PIXEL_FORMAT(av_stream, av_context) av_context->pix_fmt
180  #define AV_GET_SAMPLE_FORMAT(av_stream, av_context) av_context->sample_fmt
181  #define AV_GET_IMAGE_SIZE(pix_fmt, width, height) avpicture_get_size(pix_fmt, width, height)
182  #define AV_COPY_PICTURE_DATA(av_frame, buffer, pix_fmt, width, height) avpicture_fill((AVPicture *) av_frame, buffer, pix_fmt, width, height)
183  #define AV_OUTPUT_CONTEXT(output_context, path) oc = avformat_alloc_context()
184  #define AV_OPTION_FIND(priv_data, name) av_opt_find(priv_data, name, NULL, 0, 0)
185  #define AV_OPTION_SET(av_stream, priv_data, name, value, avcodec) av_opt_set (priv_data, name, value, 0)
186  #define AV_FORMAT_NEW_STREAM( oc, av_context, av_codec, av_st) av_st = avformat_new_stream(oc, av_codec); \
187  if (!av_st) \
188  throw OutOfMemory("Could not allocate memory for the video stream.", path); \
189  avcodec_get_context_defaults3(av_st->codec, av_codec); \
190  c = av_st->codec;
191  #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec)
192  #endif
193 
194 
195 #endif
#define AV_ERROR_MAX_STRING_SIZE
auto AV_GET_CODEC_CONTEXT