28 #ifndef OPENSHOT_FFMPEG_UTILITIES_H 29 #define OPENSHOT_FFMPEG_UTILITIES_H 33 #define INT64_C(c) (c ## LL) 34 #define UINT64_C(c) (c ## ULL) 38 #define IS_FFMPEG_3_2 (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 64, 101)) 43 #include <libavcodec/avcodec.h> 44 #include <libavformat/avformat.h> 45 #include <libswscale/swscale.h> 47 #if (LIBAVFORMAT_VERSION_MAJOR >= 57) 51 #include <libswresample/swresample.h> 53 #include <libavresample/avresample.h> 55 #include <libavutil/mathematics.h> 56 #include <libavutil/pixfmt.h> 57 #include <libavutil/pixdesc.h> 60 #if LIBAVFORMAT_VERSION_MAJOR >= 53 61 #include <libavutil/opt.h> 63 #include <libavcodec/opt.h> 67 #if LIBAVFORMAT_VERSION_MAJOR >= 54 68 #include <libavutil/channel_layout.h> 72 #include "libavutil/imgutils.h" 77 #ifndef AVCODEC_MAX_AUDIO_FRAME_SIZE 78 #define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio 80 #ifndef AV_ERROR_MAX_STRING_SIZE 81 #define AV_ERROR_MAX_STRING_SIZE 64 83 #ifndef AUDIO_PACKET_ENCODING_SIZE 84 #define AUDIO_PACKET_ENCODING_SIZE 768000 // 48khz * S16 (2 bytes) * max channels (8) 88 static const std::string av_make_error_string(
int errnum)
92 std::string errstring(errbuf);
98 #define av_err2str(errnum) av_make_error_string(errnum).c_str() 102 #define PixelFormat AVPixelFormat 105 #define PIX_FMT_RGBA AV_PIX_FMT_RGBA 108 #define PIX_FMT_NONE AV_PIX_FMT_NONE 110 #ifndef PIX_FMT_RGB24 111 #define PIX_FMT_RGB24 AV_PIX_FMT_RGB24 113 #ifndef PIX_FMT_YUV420P 114 #define PIX_FMT_YUV420P AV_PIX_FMT_YUV420P 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 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 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 150 AVCodecContext *context = avcodec_alloc_context3(av_codec); \
151 avcodec_parameters_to_context(context, av_stream->codecpar); \
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);\ 166 throw OutOfMemory("Could not allocate memory for the video stream.", path); \ 167 c = avcodec_alloc_context3(av_codec); \ 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); 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 185 AVCodecContext *context = avcodec_alloc_context3(av_codec); \
186 avcodec_parameters_to_context(context, av_stream->codecpar); \
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);\ 201 throw OutOfMemory("Could not allocate memory for the video stream.", path); \ 202 c = avcodec_alloc_context3(av_codec); \ 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); \ 232 throw OutOfMemory("Could not allocate memory for the video stream.", path); \ 233 avcodec_get_context_defaults3(av_st->codec, av_codec); \ 235 #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec) 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); \ 262 throw OutOfMemory("Could not allocate memory for the video stream.", path); \ 263 avcodec_get_context_defaults3(av_st->codec, av_codec); \ 265 #define AV_COPY_PARAMS_FROM_CONTEXT(av_stream, av_codec) #define AV_ERROR_MAX_STRING_SIZE
auto AV_GET_CODEC_CONTEXT