]> git.sesse.net Git - ffmpeg/blob - libavcodec/utils.c
Merge commit 'c6558e8840fbb2386bf8742e4d68dd6e067d262e'
[ffmpeg] / libavcodec / utils.c
1 /*
2  * utils for libavcodec
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * utils.
26  */
27
28 #include "config.h"
29 #include "libavutil/atomic.h"
30 #include "libavutil/attributes.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/avstring.h"
33 #include "libavutil/bprint.h"
34 #include "libavutil/channel_layout.h"
35 #include "libavutil/crc.h"
36 #include "libavutil/frame.h"
37 #include "libavutil/hwcontext.h"
38 #include "libavutil/internal.h"
39 #include "libavutil/mathematics.h"
40 #include "libavutil/mem_internal.h"
41 #include "libavutil/pixdesc.h"
42 #include "libavutil/imgutils.h"
43 #include "libavutil/samplefmt.h"
44 #include "libavutil/dict.h"
45 #include "libavutil/thread.h"
46 #include "avcodec.h"
47 #include "decode.h"
48 #include "hwaccel.h"
49 #include "libavutil/opt.h"
50 #include "me_cmp.h"
51 #include "mpegvideo.h"
52 #include "thread.h"
53 #include "frame_thread_encoder.h"
54 #include "internal.h"
55 #include "raw.h"
56 #include "bytestream.h"
57 #include "version.h"
58 #include <stdlib.h>
59 #include <stdarg.h>
60 #include <stdatomic.h>
61 #include <limits.h>
62 #include <float.h>
63 #if CONFIG_ICONV
64 # include <iconv.h>
65 #endif
66
67 #include "libavutil/ffversion.h"
68 const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
69
70 static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
71
72 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
73 {
74     uint8_t **p = ptr;
75     if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
76         av_freep(p);
77         *size = 0;
78         return;
79     }
80     if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
81         memset(*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
82 }
83
84 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
85 {
86     uint8_t **p = ptr;
87     if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
88         av_freep(p);
89         *size = 0;
90         return;
91     }
92     if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
93         memset(*p, 0, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
94 }
95
96 /* encoder management */
97 static AVCodec *first_avcodec = NULL;
98 static AVCodec **last_avcodec = &first_avcodec;
99
100 AVCodec *av_codec_next(const AVCodec *c)
101 {
102     if (c)
103         return c->next;
104     else
105         return first_avcodec;
106 }
107
108 static av_cold void avcodec_init(void)
109 {
110     static int initialized = 0;
111
112     if (initialized != 0)
113         return;
114     initialized = 1;
115
116     if (CONFIG_ME_CMP)
117         ff_me_cmp_init_static();
118 }
119
120 int av_codec_is_encoder(const AVCodec *codec)
121 {
122     return codec && (codec->encode_sub || codec->encode2 ||codec->send_frame);
123 }
124
125 int av_codec_is_decoder(const AVCodec *codec)
126 {
127     return codec && (codec->decode || codec->receive_frame);
128 }
129
130 av_cold void avcodec_register(AVCodec *codec)
131 {
132     AVCodec **p;
133     avcodec_init();
134     p = last_avcodec;
135     codec->next = NULL;
136
137     while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
138         p = &(*p)->next;
139     last_avcodec = &codec->next;
140
141     if (codec->init_static_data)
142         codec->init_static_data(codec);
143 }
144
145 int ff_set_dimensions(AVCodecContext *s, int width, int height)
146 {
147     int ret = av_image_check_size2(width, height, s->max_pixels, AV_PIX_FMT_NONE, 0, s);
148
149     if (ret < 0)
150         width = height = 0;
151
152     s->coded_width  = width;
153     s->coded_height = height;
154     s->width        = AV_CEIL_RSHIFT(width,  s->lowres);
155     s->height       = AV_CEIL_RSHIFT(height, s->lowres);
156
157     return ret;
158 }
159
160 int ff_set_sar(AVCodecContext *avctx, AVRational sar)
161 {
162     int ret = av_image_check_sar(avctx->width, avctx->height, sar);
163
164     if (ret < 0) {
165         av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
166                sar.num, sar.den);
167         avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
168         return ret;
169     } else {
170         avctx->sample_aspect_ratio = sar;
171     }
172     return 0;
173 }
174
175 int ff_side_data_update_matrix_encoding(AVFrame *frame,
176                                         enum AVMatrixEncoding matrix_encoding)
177 {
178     AVFrameSideData *side_data;
179     enum AVMatrixEncoding *data;
180
181     side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
182     if (!side_data)
183         side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
184                                            sizeof(enum AVMatrixEncoding));
185
186     if (!side_data)
187         return AVERROR(ENOMEM);
188
189     data  = (enum AVMatrixEncoding*)side_data->data;
190     *data = matrix_encoding;
191
192     return 0;
193 }
194
195 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
196                                int linesize_align[AV_NUM_DATA_POINTERS])
197 {
198     int i;
199     int w_align = 1;
200     int h_align = 1;
201     AVPixFmtDescriptor const *desc = av_pix_fmt_desc_get(s->pix_fmt);
202
203     if (desc) {
204         w_align = 1 << desc->log2_chroma_w;
205         h_align = 1 << desc->log2_chroma_h;
206     }
207
208     switch (s->pix_fmt) {
209     case AV_PIX_FMT_YUV420P:
210     case AV_PIX_FMT_YUYV422:
211     case AV_PIX_FMT_YVYU422:
212     case AV_PIX_FMT_UYVY422:
213     case AV_PIX_FMT_YUV422P:
214     case AV_PIX_FMT_YUV440P:
215     case AV_PIX_FMT_YUV444P:
216     case AV_PIX_FMT_GBRP:
217     case AV_PIX_FMT_GBRAP:
218     case AV_PIX_FMT_GRAY8:
219     case AV_PIX_FMT_GRAY16BE:
220     case AV_PIX_FMT_GRAY16LE:
221     case AV_PIX_FMT_YUVJ420P:
222     case AV_PIX_FMT_YUVJ422P:
223     case AV_PIX_FMT_YUVJ440P:
224     case AV_PIX_FMT_YUVJ444P:
225     case AV_PIX_FMT_YUVA420P:
226     case AV_PIX_FMT_YUVA422P:
227     case AV_PIX_FMT_YUVA444P:
228     case AV_PIX_FMT_YUV420P9LE:
229     case AV_PIX_FMT_YUV420P9BE:
230     case AV_PIX_FMT_YUV420P10LE:
231     case AV_PIX_FMT_YUV420P10BE:
232     case AV_PIX_FMT_YUV420P12LE:
233     case AV_PIX_FMT_YUV420P12BE:
234     case AV_PIX_FMT_YUV420P14LE:
235     case AV_PIX_FMT_YUV420P14BE:
236     case AV_PIX_FMT_YUV420P16LE:
237     case AV_PIX_FMT_YUV420P16BE:
238     case AV_PIX_FMT_YUVA420P9LE:
239     case AV_PIX_FMT_YUVA420P9BE:
240     case AV_PIX_FMT_YUVA420P10LE:
241     case AV_PIX_FMT_YUVA420P10BE:
242     case AV_PIX_FMT_YUVA420P16LE:
243     case AV_PIX_FMT_YUVA420P16BE:
244     case AV_PIX_FMT_YUV422P9LE:
245     case AV_PIX_FMT_YUV422P9BE:
246     case AV_PIX_FMT_YUV422P10LE:
247     case AV_PIX_FMT_YUV422P10BE:
248     case AV_PIX_FMT_YUV422P12LE:
249     case AV_PIX_FMT_YUV422P12BE:
250     case AV_PIX_FMT_YUV422P14LE:
251     case AV_PIX_FMT_YUV422P14BE:
252     case AV_PIX_FMT_YUV422P16LE:
253     case AV_PIX_FMT_YUV422P16BE:
254     case AV_PIX_FMT_YUVA422P9LE:
255     case AV_PIX_FMT_YUVA422P9BE:
256     case AV_PIX_FMT_YUVA422P10LE:
257     case AV_PIX_FMT_YUVA422P10BE:
258     case AV_PIX_FMT_YUVA422P16LE:
259     case AV_PIX_FMT_YUVA422P16BE:
260     case AV_PIX_FMT_YUV440P10LE:
261     case AV_PIX_FMT_YUV440P10BE:
262     case AV_PIX_FMT_YUV440P12LE:
263     case AV_PIX_FMT_YUV440P12BE:
264     case AV_PIX_FMT_YUV444P9LE:
265     case AV_PIX_FMT_YUV444P9BE:
266     case AV_PIX_FMT_YUV444P10LE:
267     case AV_PIX_FMT_YUV444P10BE:
268     case AV_PIX_FMT_YUV444P12LE:
269     case AV_PIX_FMT_YUV444P12BE:
270     case AV_PIX_FMT_YUV444P14LE:
271     case AV_PIX_FMT_YUV444P14BE:
272     case AV_PIX_FMT_YUV444P16LE:
273     case AV_PIX_FMT_YUV444P16BE:
274     case AV_PIX_FMT_YUVA444P9LE:
275     case AV_PIX_FMT_YUVA444P9BE:
276     case AV_PIX_FMT_YUVA444P10LE:
277     case AV_PIX_FMT_YUVA444P10BE:
278     case AV_PIX_FMT_YUVA444P16LE:
279     case AV_PIX_FMT_YUVA444P16BE:
280     case AV_PIX_FMT_GBRP9LE:
281     case AV_PIX_FMT_GBRP9BE:
282     case AV_PIX_FMT_GBRP10LE:
283     case AV_PIX_FMT_GBRP10BE:
284     case AV_PIX_FMT_GBRP12LE:
285     case AV_PIX_FMT_GBRP12BE:
286     case AV_PIX_FMT_GBRP14LE:
287     case AV_PIX_FMT_GBRP14BE:
288     case AV_PIX_FMT_GBRP16LE:
289     case AV_PIX_FMT_GBRP16BE:
290     case AV_PIX_FMT_GBRAP12LE:
291     case AV_PIX_FMT_GBRAP12BE:
292     case AV_PIX_FMT_GBRAP16LE:
293     case AV_PIX_FMT_GBRAP16BE:
294         w_align = 16; //FIXME assume 16 pixel per macroblock
295         h_align = 16 * 2; // interlaced needs 2 macroblocks height
296         break;
297     case AV_PIX_FMT_YUV411P:
298     case AV_PIX_FMT_YUVJ411P:
299     case AV_PIX_FMT_UYYVYY411:
300         w_align = 32;
301         h_align = 16 * 2;
302         break;
303     case AV_PIX_FMT_YUV410P:
304         if (s->codec_id == AV_CODEC_ID_SVQ1) {
305             w_align = 64;
306             h_align = 64;
307         }
308         break;
309     case AV_PIX_FMT_RGB555:
310         if (s->codec_id == AV_CODEC_ID_RPZA) {
311             w_align = 4;
312             h_align = 4;
313         }
314         if (s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
315             w_align = 8;
316             h_align = 8;
317         }
318         break;
319     case AV_PIX_FMT_PAL8:
320     case AV_PIX_FMT_BGR8:
321     case AV_PIX_FMT_RGB8:
322         if (s->codec_id == AV_CODEC_ID_SMC ||
323             s->codec_id == AV_CODEC_ID_CINEPAK) {
324             w_align = 4;
325             h_align = 4;
326         }
327         if (s->codec_id == AV_CODEC_ID_JV ||
328             s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
329             w_align = 8;
330             h_align = 8;
331         }
332         break;
333     case AV_PIX_FMT_BGR24:
334         if ((s->codec_id == AV_CODEC_ID_MSZH) ||
335             (s->codec_id == AV_CODEC_ID_ZLIB)) {
336             w_align = 4;
337             h_align = 4;
338         }
339         break;
340     case AV_PIX_FMT_RGB24:
341         if (s->codec_id == AV_CODEC_ID_CINEPAK) {
342             w_align = 4;
343             h_align = 4;
344         }
345         break;
346     default:
347         break;
348     }
349
350     if (s->codec_id == AV_CODEC_ID_IFF_ILBM) {
351         w_align = FFMAX(w_align, 8);
352     }
353
354     *width  = FFALIGN(*width, w_align);
355     *height = FFALIGN(*height, h_align);
356     if (s->codec_id == AV_CODEC_ID_H264 || s->lowres) {
357         // some of the optimized chroma MC reads one line too much
358         // which is also done in mpeg decoders with lowres > 0
359         *height += 2;
360
361         // H.264 uses edge emulation for out of frame motion vectors, for this
362         // it requires a temporary area large enough to hold a 21x21 block,
363         // increasing witdth ensure that the temporary area is large enough,
364         // the next rounded up width is 32
365         *width = FFMAX(*width, 32);
366     }
367
368     for (i = 0; i < 4; i++)
369         linesize_align[i] = STRIDE_ALIGN;
370 }
371
372 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
373 {
374     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
375     int chroma_shift = desc->log2_chroma_w;
376     int linesize_align[AV_NUM_DATA_POINTERS];
377     int align;
378
379     avcodec_align_dimensions2(s, width, height, linesize_align);
380     align               = FFMAX(linesize_align[0], linesize_align[3]);
381     linesize_align[1] <<= chroma_shift;
382     linesize_align[2] <<= chroma_shift;
383     align               = FFMAX3(align, linesize_align[1], linesize_align[2]);
384     *width              = FFALIGN(*width, align);
385 }
386
387 int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
388 {
389     if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
390         return AVERROR(EINVAL);
391     pos--;
392
393     *xpos = (pos&1) * 128;
394     *ypos = ((pos>>1)^(pos<4)) * 128;
395
396     return 0;
397 }
398
399 enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
400 {
401     int pos, xout, yout;
402
403     for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
404         if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
405             return pos;
406     }
407     return AVCHROMA_LOC_UNSPECIFIED;
408 }
409
410 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
411                              enum AVSampleFormat sample_fmt, const uint8_t *buf,
412                              int buf_size, int align)
413 {
414     int ch, planar, needed_size, ret = 0;
415
416     needed_size = av_samples_get_buffer_size(NULL, nb_channels,
417                                              frame->nb_samples, sample_fmt,
418                                              align);
419     if (buf_size < needed_size)
420         return AVERROR(EINVAL);
421
422     planar = av_sample_fmt_is_planar(sample_fmt);
423     if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
424         if (!(frame->extended_data = av_mallocz_array(nb_channels,
425                                                 sizeof(*frame->extended_data))))
426             return AVERROR(ENOMEM);
427     } else {
428         frame->extended_data = frame->data;
429     }
430
431     if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
432                                       (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
433                                       sample_fmt, align)) < 0) {
434         if (frame->extended_data != frame->data)
435             av_freep(&frame->extended_data);
436         return ret;
437     }
438     if (frame->extended_data != frame->data) {
439         for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
440             frame->data[ch] = frame->extended_data[ch];
441     }
442
443     return ret;
444 }
445
446 void ff_color_frame(AVFrame *frame, const int c[4])
447 {
448     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
449     int p, y, x;
450
451     av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
452
453     for (p = 0; p<desc->nb_components; p++) {
454         uint8_t *dst = frame->data[p];
455         int is_chroma = p == 1 || p == 2;
456         int bytes  = is_chroma ? AV_CEIL_RSHIFT(frame->width,  desc->log2_chroma_w) : frame->width;
457         int height = is_chroma ? AV_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
458         for (y = 0; y < height; y++) {
459             if (desc->comp[0].depth >= 9) {
460                 for (x = 0; x<bytes; x++)
461                     ((uint16_t*)dst)[x] = c[p];
462             }else
463                 memset(dst, c[p], bytes);
464             dst += frame->linesize[p];
465         }
466     }
467 }
468
469 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
470 {
471     int i;
472
473     for (i = 0; i < count; i++) {
474         int r = func(c, (char *)arg + i * size);
475         if (ret)
476             ret[i] = r;
477     }
478     emms_c();
479     return 0;
480 }
481
482 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
483 {
484     int i;
485
486     for (i = 0; i < count; i++) {
487         int r = func(c, arg, i, 0);
488         if (ret)
489             ret[i] = r;
490     }
491     emms_c();
492     return 0;
493 }
494
495 enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
496                                        unsigned int fourcc)
497 {
498     while (tags->pix_fmt >= 0) {
499         if (tags->fourcc == fourcc)
500             return tags->pix_fmt;
501         tags++;
502     }
503     return AV_PIX_FMT_NONE;
504 }
505
506 #if FF_API_CODEC_GET_SET
507 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
508 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
509 MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
510 MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
511 MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
512
513 unsigned av_codec_get_codec_properties(const AVCodecContext *codec)
514 {
515     return codec->properties;
516 }
517
518 int av_codec_get_max_lowres(const AVCodec *codec)
519 {
520     return codec->max_lowres;
521 }
522 #endif
523
524 int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec){
525     return !!(codec->caps_internal & FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM);
526 }
527
528 static int64_t get_bit_rate(AVCodecContext *ctx)
529 {
530     int64_t bit_rate;
531     int bits_per_sample;
532
533     switch (ctx->codec_type) {
534     case AVMEDIA_TYPE_VIDEO:
535     case AVMEDIA_TYPE_DATA:
536     case AVMEDIA_TYPE_SUBTITLE:
537     case AVMEDIA_TYPE_ATTACHMENT:
538         bit_rate = ctx->bit_rate;
539         break;
540     case AVMEDIA_TYPE_AUDIO:
541         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
542         bit_rate = bits_per_sample ? ctx->sample_rate * (int64_t)ctx->channels * bits_per_sample : ctx->bit_rate;
543         break;
544     default:
545         bit_rate = 0;
546         break;
547     }
548     return bit_rate;
549 }
550
551
552 static void ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec)
553 {
554     if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
555         ff_mutex_lock(&codec_mutex);
556 }
557
558 static void ff_unlock_avcodec(const AVCodec *codec)
559 {
560     if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init)
561         ff_mutex_unlock(&codec_mutex);
562 }
563
564 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
565 {
566     int ret = 0;
567
568     ff_unlock_avcodec(codec);
569
570     ret = avcodec_open2(avctx, codec, options);
571
572     ff_lock_avcodec(avctx, codec);
573     return ret;
574 }
575
576 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
577 {
578     int ret = 0;
579     AVDictionary *tmp = NULL;
580     const AVPixFmtDescriptor *pixdesc;
581
582     if (avcodec_is_open(avctx))
583         return 0;
584
585     if ((!codec && !avctx->codec)) {
586         av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
587         return AVERROR(EINVAL);
588     }
589     if ((codec && avctx->codec && codec != avctx->codec)) {
590         av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
591                                     "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
592         return AVERROR(EINVAL);
593     }
594     if (!codec)
595         codec = avctx->codec;
596
597     if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
598         return AVERROR(EINVAL);
599
600     if (options)
601         av_dict_copy(&tmp, *options, 0);
602
603     ff_lock_avcodec(avctx, codec);
604
605     avctx->internal = av_mallocz(sizeof(AVCodecInternal));
606     if (!avctx->internal) {
607         ret = AVERROR(ENOMEM);
608         goto end;
609     }
610
611     avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
612     if (!avctx->internal->pool) {
613         ret = AVERROR(ENOMEM);
614         goto free_and_end;
615     }
616
617     avctx->internal->to_free = av_frame_alloc();
618     if (!avctx->internal->to_free) {
619         ret = AVERROR(ENOMEM);
620         goto free_and_end;
621     }
622
623     avctx->internal->compat_decode_frame = av_frame_alloc();
624     if (!avctx->internal->compat_decode_frame) {
625         ret = AVERROR(ENOMEM);
626         goto free_and_end;
627     }
628
629     avctx->internal->buffer_frame = av_frame_alloc();
630     if (!avctx->internal->buffer_frame) {
631         ret = AVERROR(ENOMEM);
632         goto free_and_end;
633     }
634
635     avctx->internal->buffer_pkt = av_packet_alloc();
636     if (!avctx->internal->buffer_pkt) {
637         ret = AVERROR(ENOMEM);
638         goto free_and_end;
639     }
640
641     avctx->internal->ds.in_pkt = av_packet_alloc();
642     if (!avctx->internal->ds.in_pkt) {
643         ret = AVERROR(ENOMEM);
644         goto free_and_end;
645     }
646
647     avctx->internal->last_pkt_props = av_packet_alloc();
648     if (!avctx->internal->last_pkt_props) {
649         ret = AVERROR(ENOMEM);
650         goto free_and_end;
651     }
652
653     avctx->internal->skip_samples_multiplier = 1;
654
655     if (codec->priv_data_size > 0) {
656         if (!avctx->priv_data) {
657             avctx->priv_data = av_mallocz(codec->priv_data_size);
658             if (!avctx->priv_data) {
659                 ret = AVERROR(ENOMEM);
660                 goto end;
661             }
662             if (codec->priv_class) {
663                 *(const AVClass **)avctx->priv_data = codec->priv_class;
664                 av_opt_set_defaults(avctx->priv_data);
665             }
666         }
667         if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
668             goto free_and_end;
669     } else {
670         avctx->priv_data = NULL;
671     }
672     if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
673         goto free_and_end;
674
675     if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
676         av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
677         ret = AVERROR(EINVAL);
678         goto free_and_end;
679     }
680
681     // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
682     if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
683           (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F || avctx->codec_id == AV_CODEC_ID_DXV))) {
684     if (avctx->coded_width && avctx->coded_height)
685         ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
686     else if (avctx->width && avctx->height)
687         ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
688     if (ret < 0)
689         goto free_and_end;
690     }
691
692     if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
693         && (  av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
694            || av_image_check_size2(avctx->width,       avctx->height,       avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
695         av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
696         ff_set_dimensions(avctx, 0, 0);
697     }
698
699     if (avctx->width > 0 && avctx->height > 0) {
700         if (av_image_check_sar(avctx->width, avctx->height,
701                                avctx->sample_aspect_ratio) < 0) {
702             av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
703                    avctx->sample_aspect_ratio.num,
704                    avctx->sample_aspect_ratio.den);
705             avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
706         }
707     }
708
709     /* if the decoder init function was already called previously,
710      * free the already allocated subtitle_header before overwriting it */
711     if (av_codec_is_decoder(codec))
712         av_freep(&avctx->subtitle_header);
713
714     if (avctx->channels > FF_SANE_NB_CHANNELS) {
715         ret = AVERROR(EINVAL);
716         goto free_and_end;
717     }
718
719     avctx->codec = codec;
720     if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
721         avctx->codec_id == AV_CODEC_ID_NONE) {
722         avctx->codec_type = codec->type;
723         avctx->codec_id   = codec->id;
724     }
725     if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
726                                          && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
727         av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
728         ret = AVERROR(EINVAL);
729         goto free_and_end;
730     }
731     avctx->frame_number = 0;
732     avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
733
734     if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
735         avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
736         const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
737         AVCodec *codec2;
738         av_log(avctx, AV_LOG_ERROR,
739                "The %s '%s' is experimental but experimental codecs are not enabled, "
740                "add '-strict %d' if you want to use it.\n",
741                codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
742         codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
743         if (!(codec2->capabilities & AV_CODEC_CAP_EXPERIMENTAL))
744             av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
745                 codec_string, codec2->name);
746         ret = AVERROR_EXPERIMENTAL;
747         goto free_and_end;
748     }
749
750     if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
751         (!avctx->time_base.num || !avctx->time_base.den)) {
752         avctx->time_base.num = 1;
753         avctx->time_base.den = avctx->sample_rate;
754     }
755
756     if (!HAVE_THREADS)
757         av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
758
759     if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
760         ff_unlock_avcodec(codec); //we will instantiate a few encoders thus kick the counter to prevent false detection of a problem
761         ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
762         ff_lock_avcodec(avctx, codec);
763         if (ret < 0)
764             goto free_and_end;
765     }
766
767     if (HAVE_THREADS
768         && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
769         ret = ff_thread_init(avctx);
770         if (ret < 0) {
771             goto free_and_end;
772         }
773     }
774     if (!HAVE_THREADS && !(codec->capabilities & AV_CODEC_CAP_AUTO_THREADS))
775         avctx->thread_count = 1;
776
777     if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
778         av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n",
779                avctx->codec->max_lowres);
780         avctx->lowres = avctx->codec->max_lowres;
781     }
782
783     if (av_codec_is_encoder(avctx->codec)) {
784         int i;
785 #if FF_API_CODED_FRAME
786 FF_DISABLE_DEPRECATION_WARNINGS
787         avctx->coded_frame = av_frame_alloc();
788         if (!avctx->coded_frame) {
789             ret = AVERROR(ENOMEM);
790             goto free_and_end;
791         }
792 FF_ENABLE_DEPRECATION_WARNINGS
793 #endif
794
795         if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
796             av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
797             ret = AVERROR(EINVAL);
798             goto free_and_end;
799         }
800
801         if (avctx->codec->sample_fmts) {
802             for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
803                 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
804                     break;
805                 if (avctx->channels == 1 &&
806                     av_get_planar_sample_fmt(avctx->sample_fmt) ==
807                     av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
808                     avctx->sample_fmt = avctx->codec->sample_fmts[i];
809                     break;
810                 }
811             }
812             if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
813                 char buf[128];
814                 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
815                 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
816                        (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
817                 ret = AVERROR(EINVAL);
818                 goto free_and_end;
819             }
820         }
821         if (avctx->codec->pix_fmts) {
822             for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
823                 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
824                     break;
825             if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
826                 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
827                      && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
828                 char buf[128];
829                 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
830                 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
831                        (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
832                 ret = AVERROR(EINVAL);
833                 goto free_and_end;
834             }
835             if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
836                 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
837                 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
838                 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
839                 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
840                 avctx->color_range = AVCOL_RANGE_JPEG;
841         }
842         if (avctx->codec->supported_samplerates) {
843             for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
844                 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
845                     break;
846             if (avctx->codec->supported_samplerates[i] == 0) {
847                 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
848                        avctx->sample_rate);
849                 ret = AVERROR(EINVAL);
850                 goto free_and_end;
851             }
852         }
853         if (avctx->sample_rate < 0) {
854             av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
855                     avctx->sample_rate);
856             ret = AVERROR(EINVAL);
857             goto free_and_end;
858         }
859         if (avctx->codec->channel_layouts) {
860             if (!avctx->channel_layout) {
861                 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
862             } else {
863                 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
864                     if (avctx->channel_layout == avctx->codec->channel_layouts[i])
865                         break;
866                 if (avctx->codec->channel_layouts[i] == 0) {
867                     char buf[512];
868                     av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
869                     av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
870                     ret = AVERROR(EINVAL);
871                     goto free_and_end;
872                 }
873             }
874         }
875         if (avctx->channel_layout && avctx->channels) {
876             int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
877             if (channels != avctx->channels) {
878                 char buf[512];
879                 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
880                 av_log(avctx, AV_LOG_ERROR,
881                        "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
882                        buf, channels, avctx->channels);
883                 ret = AVERROR(EINVAL);
884                 goto free_and_end;
885             }
886         } else if (avctx->channel_layout) {
887             avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
888         }
889         if (avctx->channels < 0) {
890             av_log(avctx, AV_LOG_ERROR, "Specified number of channels %d is not supported\n",
891                     avctx->channels);
892             ret = AVERROR(EINVAL);
893             goto free_and_end;
894         }
895         if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
896             pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
897             if (    avctx->bits_per_raw_sample < 0
898                 || (avctx->bits_per_raw_sample > 8 && pixdesc->comp[0].depth <= 8)) {
899                 av_log(avctx, AV_LOG_WARNING, "Specified bit depth %d not possible with the specified pixel formats depth %d\n",
900                     avctx->bits_per_raw_sample, pixdesc->comp[0].depth);
901                 avctx->bits_per_raw_sample = pixdesc->comp[0].depth;
902             }
903             if (avctx->width <= 0 || avctx->height <= 0) {
904                 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
905                 ret = AVERROR(EINVAL);
906                 goto free_and_end;
907             }
908         }
909         if (   (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
910             && avctx->bit_rate>0 && avctx->bit_rate<1000) {
911             av_log(avctx, AV_LOG_WARNING, "Bitrate %"PRId64" is extremely low, maybe you mean %"PRId64"k\n", avctx->bit_rate, avctx->bit_rate);
912         }
913
914         if (!avctx->rc_initial_buffer_occupancy)
915             avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3LL / 4;
916
917         if (avctx->ticks_per_frame && avctx->time_base.num &&
918             avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
919             av_log(avctx, AV_LOG_ERROR,
920                    "ticks_per_frame %d too large for the timebase %d/%d.",
921                    avctx->ticks_per_frame,
922                    avctx->time_base.num,
923                    avctx->time_base.den);
924             goto free_and_end;
925         }
926
927         if (avctx->hw_frames_ctx) {
928             AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
929             if (frames_ctx->format != avctx->pix_fmt) {
930                 av_log(avctx, AV_LOG_ERROR,
931                        "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
932                 ret = AVERROR(EINVAL);
933                 goto free_and_end;
934             }
935             if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
936                 avctx->sw_pix_fmt != frames_ctx->sw_format) {
937                 av_log(avctx, AV_LOG_ERROR,
938                        "Mismatching AVCodecContext.sw_pix_fmt (%s) "
939                        "and AVHWFramesContext.sw_format (%s)\n",
940                        av_get_pix_fmt_name(avctx->sw_pix_fmt),
941                        av_get_pix_fmt_name(frames_ctx->sw_format));
942                 ret = AVERROR(EINVAL);
943                 goto free_and_end;
944             }
945             avctx->sw_pix_fmt = frames_ctx->sw_format;
946         }
947     }
948
949     avctx->pts_correction_num_faulty_pts =
950     avctx->pts_correction_num_faulty_dts = 0;
951     avctx->pts_correction_last_pts =
952     avctx->pts_correction_last_dts = INT64_MIN;
953
954     if (   !CONFIG_GRAY && avctx->flags & AV_CODEC_FLAG_GRAY
955         && avctx->codec_descriptor->type == AVMEDIA_TYPE_VIDEO)
956         av_log(avctx, AV_LOG_WARNING,
957                "gray decoding requested but not enabled at configuration time\n");
958
959     if (   avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
960         || avctx->internal->frame_thread_encoder)) {
961         ret = avctx->codec->init(avctx);
962         if (ret < 0) {
963             goto free_and_end;
964         }
965     }
966
967     ret=0;
968
969     if (av_codec_is_decoder(avctx->codec)) {
970         if (!avctx->bit_rate)
971             avctx->bit_rate = get_bit_rate(avctx);
972         /* validate channel layout from the decoder */
973         if (avctx->channel_layout) {
974             int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
975             if (!avctx->channels)
976                 avctx->channels = channels;
977             else if (channels != avctx->channels) {
978                 char buf[512];
979                 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
980                 av_log(avctx, AV_LOG_WARNING,
981                        "Channel layout '%s' with %d channels does not match specified number of channels %d: "
982                        "ignoring specified channel layout\n",
983                        buf, channels, avctx->channels);
984                 avctx->channel_layout = 0;
985             }
986         }
987         if (avctx->channels && avctx->channels < 0 ||
988             avctx->channels > FF_SANE_NB_CHANNELS) {
989             ret = AVERROR(EINVAL);
990             goto free_and_end;
991         }
992         if (avctx->sub_charenc) {
993             if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
994                 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
995                        "supported with subtitles codecs\n");
996                 ret = AVERROR(EINVAL);
997                 goto free_and_end;
998             } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
999                 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1000                        "subtitles character encoding will be ignored\n",
1001                        avctx->codec_descriptor->name);
1002                 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
1003             } else {
1004                 /* input character encoding is set for a text based subtitle
1005                  * codec at this point */
1006                 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
1007                     avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
1008
1009                 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
1010 #if CONFIG_ICONV
1011                     iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1012                     if (cd == (iconv_t)-1) {
1013                         ret = AVERROR(errno);
1014                         av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1015                                "with input character encoding \"%s\"\n", avctx->sub_charenc);
1016                         goto free_and_end;
1017                     }
1018                     iconv_close(cd);
1019 #else
1020                     av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1021                            "conversion needs a libavcodec built with iconv support "
1022                            "for this codec\n");
1023                     ret = AVERROR(ENOSYS);
1024                     goto free_and_end;
1025 #endif
1026                 }
1027             }
1028         }
1029
1030 #if FF_API_AVCTX_TIMEBASE
1031         if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1032             avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
1033 #endif
1034     }
1035     if (codec->priv_data_size > 0 && avctx->priv_data && codec->priv_class) {
1036         av_assert0(*(const AVClass **)avctx->priv_data == codec->priv_class);
1037     }
1038
1039 end:
1040     ff_unlock_avcodec(codec);
1041     if (options) {
1042         av_dict_free(options);
1043         *options = tmp;
1044     }
1045
1046     return ret;
1047 free_and_end:
1048     if (avctx->codec &&
1049         (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
1050         avctx->codec->close(avctx);
1051
1052     if (codec->priv_class && codec->priv_data_size)
1053         av_opt_free(avctx->priv_data);
1054     av_opt_free(avctx);
1055
1056 #if FF_API_CODED_FRAME
1057 FF_DISABLE_DEPRECATION_WARNINGS
1058     av_frame_free(&avctx->coded_frame);
1059 FF_ENABLE_DEPRECATION_WARNINGS
1060 #endif
1061
1062     av_dict_free(&tmp);
1063     av_freep(&avctx->priv_data);
1064     if (avctx->internal) {
1065         av_frame_free(&avctx->internal->to_free);
1066         av_frame_free(&avctx->internal->compat_decode_frame);
1067         av_frame_free(&avctx->internal->buffer_frame);
1068         av_packet_free(&avctx->internal->buffer_pkt);
1069         av_packet_free(&avctx->internal->last_pkt_props);
1070
1071         av_packet_free(&avctx->internal->ds.in_pkt);
1072
1073         av_freep(&avctx->internal->pool);
1074     }
1075     av_freep(&avctx->internal);
1076     avctx->codec = NULL;
1077     goto end;
1078 }
1079
1080 void avsubtitle_free(AVSubtitle *sub)
1081 {
1082     int i;
1083
1084     for (i = 0; i < sub->num_rects; i++) {
1085         av_freep(&sub->rects[i]->data[0]);
1086         av_freep(&sub->rects[i]->data[1]);
1087         av_freep(&sub->rects[i]->data[2]);
1088         av_freep(&sub->rects[i]->data[3]);
1089         av_freep(&sub->rects[i]->text);
1090         av_freep(&sub->rects[i]->ass);
1091         av_freep(&sub->rects[i]);
1092     }
1093
1094     av_freep(&sub->rects);
1095
1096     memset(sub, 0, sizeof(AVSubtitle));
1097 }
1098
1099 av_cold int avcodec_close(AVCodecContext *avctx)
1100 {
1101     int i;
1102
1103     if (!avctx)
1104         return 0;
1105
1106     if (avcodec_is_open(avctx)) {
1107         FramePool *pool = avctx->internal->pool;
1108         if (CONFIG_FRAME_THREAD_ENCODER &&
1109             avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
1110             ff_frame_thread_encoder_free(avctx);
1111         }
1112         if (HAVE_THREADS && avctx->internal->thread_ctx)
1113             ff_thread_free(avctx);
1114         if (avctx->codec && avctx->codec->close)
1115             avctx->codec->close(avctx);
1116         avctx->internal->byte_buffer_size = 0;
1117         av_freep(&avctx->internal->byte_buffer);
1118         av_frame_free(&avctx->internal->to_free);
1119         av_frame_free(&avctx->internal->compat_decode_frame);
1120         av_frame_free(&avctx->internal->buffer_frame);
1121         av_packet_free(&avctx->internal->buffer_pkt);
1122         av_packet_free(&avctx->internal->last_pkt_props);
1123
1124         av_packet_free(&avctx->internal->ds.in_pkt);
1125
1126         for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
1127             av_buffer_pool_uninit(&pool->pools[i]);
1128         av_freep(&avctx->internal->pool);
1129
1130         if (avctx->hwaccel && avctx->hwaccel->uninit)
1131             avctx->hwaccel->uninit(avctx);
1132         av_freep(&avctx->internal->hwaccel_priv_data);
1133
1134         ff_decode_bsfs_uninit(avctx);
1135
1136         av_freep(&avctx->internal);
1137     }
1138
1139     for (i = 0; i < avctx->nb_coded_side_data; i++)
1140         av_freep(&avctx->coded_side_data[i].data);
1141     av_freep(&avctx->coded_side_data);
1142     avctx->nb_coded_side_data = 0;
1143
1144     av_buffer_unref(&avctx->hw_frames_ctx);
1145     av_buffer_unref(&avctx->hw_device_ctx);
1146
1147     if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1148         av_opt_free(avctx->priv_data);
1149     av_opt_free(avctx);
1150     av_freep(&avctx->priv_data);
1151     if (av_codec_is_encoder(avctx->codec)) {
1152         av_freep(&avctx->extradata);
1153 #if FF_API_CODED_FRAME
1154 FF_DISABLE_DEPRECATION_WARNINGS
1155         av_frame_free(&avctx->coded_frame);
1156 FF_ENABLE_DEPRECATION_WARNINGS
1157 #endif
1158     }
1159     avctx->codec = NULL;
1160     avctx->active_thread_type = 0;
1161
1162     return 0;
1163 }
1164
1165 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
1166 {
1167     switch(id){
1168         //This is for future deprecatec codec ids, its empty since
1169         //last major bump but will fill up again over time, please don't remove it
1170         default                                         : return id;
1171     }
1172 }
1173
1174 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1175 {
1176     AVCodec *p, *experimental = NULL;
1177     p = first_avcodec;
1178     id= remap_deprecated_codec_id(id);
1179     while (p) {
1180         if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1181             p->id == id) {
1182             if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
1183                 experimental = p;
1184             } else
1185                 return p;
1186         }
1187         p = p->next;
1188     }
1189     return experimental;
1190 }
1191
1192 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1193 {
1194     return find_encdec(id, 1);
1195 }
1196
1197 AVCodec *avcodec_find_encoder_by_name(const char *name)
1198 {
1199     AVCodec *p;
1200     if (!name)
1201         return NULL;
1202     p = first_avcodec;
1203     while (p) {
1204         if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
1205             return p;
1206         p = p->next;
1207     }
1208     return NULL;
1209 }
1210
1211 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1212 {
1213     return find_encdec(id, 0);
1214 }
1215
1216 AVCodec *avcodec_find_decoder_by_name(const char *name)
1217 {
1218     AVCodec *p;
1219     if (!name)
1220         return NULL;
1221     p = first_avcodec;
1222     while (p) {
1223         if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
1224             return p;
1225         p = p->next;
1226     }
1227     return NULL;
1228 }
1229
1230 const char *avcodec_get_name(enum AVCodecID id)
1231 {
1232     const AVCodecDescriptor *cd;
1233     AVCodec *codec;
1234
1235     if (id == AV_CODEC_ID_NONE)
1236         return "none";
1237     cd = avcodec_descriptor_get(id);
1238     if (cd)
1239         return cd->name;
1240     av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
1241     codec = avcodec_find_decoder(id);
1242     if (codec)
1243         return codec->name;
1244     codec = avcodec_find_encoder(id);
1245     if (codec)
1246         return codec->name;
1247     return "unknown_codec";
1248 }
1249
1250 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1251 {
1252     int i, len, ret = 0;
1253
1254 #define TAG_PRINT(x)                                              \
1255     (((x) >= '0' && (x) <= '9') ||                                \
1256      ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') ||  \
1257      ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
1258
1259     for (i = 0; i < 4; i++) {
1260         len = snprintf(buf, buf_size,
1261                        TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
1262         buf        += len;
1263         buf_size    = buf_size > len ? buf_size - len : 0;
1264         ret        += len;
1265         codec_tag >>= 8;
1266     }
1267     return ret;
1268 }
1269
1270 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1271 {
1272     const char *codec_type;
1273     const char *codec_name;
1274     const char *profile = NULL;
1275     int64_t bitrate;
1276     int new_line = 0;
1277     AVRational display_aspect_ratio;
1278     const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
1279
1280     if (!buf || buf_size <= 0)
1281         return;
1282     codec_type = av_get_media_type_string(enc->codec_type);
1283     codec_name = avcodec_get_name(enc->codec_id);
1284     profile = avcodec_profile_name(enc->codec_id, enc->profile);
1285
1286     snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
1287              codec_name);
1288     buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
1289
1290     if (enc->codec && strcmp(enc->codec->name, codec_name))
1291         snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
1292
1293     if (profile)
1294         snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
1295     if (   enc->codec_type == AVMEDIA_TYPE_VIDEO
1296         && av_log_get_level() >= AV_LOG_VERBOSE
1297         && enc->refs)
1298         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1299                  ", %d reference frame%s",
1300                  enc->refs, enc->refs > 1 ? "s" : "");
1301
1302     if (enc->codec_tag)
1303         snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s / 0x%04X)",
1304                  av_fourcc2str(enc->codec_tag), enc->codec_tag);
1305
1306     switch (enc->codec_type) {
1307     case AVMEDIA_TYPE_VIDEO:
1308         {
1309             char detail[256] = "(";
1310
1311             av_strlcat(buf, separator, buf_size);
1312
1313             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1314                  "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
1315                      av_get_pix_fmt_name(enc->pix_fmt));
1316             if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE &&
1317                 enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth)
1318                 av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
1319             if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
1320                 av_strlcatf(detail, sizeof(detail), "%s, ",
1321                             av_color_range_name(enc->color_range));
1322
1323             if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
1324                 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
1325                 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
1326                 if (enc->colorspace != (int)enc->color_primaries ||
1327                     enc->colorspace != (int)enc->color_trc) {
1328                     new_line = 1;
1329                     av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
1330                                 av_color_space_name(enc->colorspace),
1331                                 av_color_primaries_name(enc->color_primaries),
1332                                 av_color_transfer_name(enc->color_trc));
1333                 } else
1334                     av_strlcatf(detail, sizeof(detail), "%s, ",
1335                                 av_get_colorspace_name(enc->colorspace));
1336             }
1337
1338             if (enc->field_order != AV_FIELD_UNKNOWN) {
1339                 const char *field_order = "progressive";
1340                 if (enc->field_order == AV_FIELD_TT)
1341                     field_order = "top first";
1342                 else if (enc->field_order == AV_FIELD_BB)
1343                     field_order = "bottom first";
1344                 else if (enc->field_order == AV_FIELD_TB)
1345                     field_order = "top coded first (swapped)";
1346                 else if (enc->field_order == AV_FIELD_BT)
1347                     field_order = "bottom coded first (swapped)";
1348
1349                 av_strlcatf(detail, sizeof(detail), "%s, ", field_order);
1350             }
1351
1352             if (av_log_get_level() >= AV_LOG_VERBOSE &&
1353                 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
1354                 av_strlcatf(detail, sizeof(detail), "%s, ",
1355                             av_chroma_location_name(enc->chroma_sample_location));
1356
1357             if (strlen(detail) > 1) {
1358                 detail[strlen(detail) - 2] = 0;
1359                 av_strlcatf(buf, buf_size, "%s)", detail);
1360             }
1361         }
1362
1363         if (enc->width) {
1364             av_strlcat(buf, new_line ? separator : ", ", buf_size);
1365
1366             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1367                      "%dx%d",
1368                      enc->width, enc->height);
1369
1370             if (av_log_get_level() >= AV_LOG_VERBOSE &&
1371                 (enc->width != enc->coded_width ||
1372                  enc->height != enc->coded_height))
1373                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1374                          " (%dx%d)", enc->coded_width, enc->coded_height);
1375
1376             if (enc->sample_aspect_ratio.num) {
1377                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1378                           enc->width * (int64_t)enc->sample_aspect_ratio.num,
1379                           enc->height * (int64_t)enc->sample_aspect_ratio.den,
1380                           1024 * 1024);
1381                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1382                          " [SAR %d:%d DAR %d:%d]",
1383                          enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1384                          display_aspect_ratio.num, display_aspect_ratio.den);
1385             }
1386             if (av_log_get_level() >= AV_LOG_DEBUG) {
1387                 int g = av_gcd(enc->time_base.num, enc->time_base.den);
1388                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1389                          ", %d/%d",
1390                          enc->time_base.num / g, enc->time_base.den / g);
1391             }
1392         }
1393         if (encode) {
1394             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1395                      ", q=%d-%d", enc->qmin, enc->qmax);
1396         } else {
1397             if (enc->properties & FF_CODEC_PROPERTY_CLOSED_CAPTIONS)
1398                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1399                          ", Closed Captions");
1400             if (enc->properties & FF_CODEC_PROPERTY_LOSSLESS)
1401                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1402                          ", lossless");
1403         }
1404         break;
1405     case AVMEDIA_TYPE_AUDIO:
1406         av_strlcat(buf, separator, buf_size);
1407
1408         if (enc->sample_rate) {
1409             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1410                      "%d Hz, ", enc->sample_rate);
1411         }
1412         av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1413         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1414             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1415                      ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1416         }
1417         if (   enc->bits_per_raw_sample > 0
1418             && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
1419             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1420                      " (%d bit)", enc->bits_per_raw_sample);
1421         if (av_log_get_level() >= AV_LOG_VERBOSE) {
1422             if (enc->initial_padding)
1423                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1424                          ", delay %d", enc->initial_padding);
1425             if (enc->trailing_padding)
1426                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1427                          ", padding %d", enc->trailing_padding);
1428         }
1429         break;
1430     case AVMEDIA_TYPE_DATA:
1431         if (av_log_get_level() >= AV_LOG_DEBUG) {
1432             int g = av_gcd(enc->time_base.num, enc->time_base.den);
1433             if (g)
1434                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1435                          ", %d/%d",
1436                          enc->time_base.num / g, enc->time_base.den / g);
1437         }
1438         break;
1439     case AVMEDIA_TYPE_SUBTITLE:
1440         if (enc->width)
1441             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1442                      ", %dx%d", enc->width, enc->height);
1443         break;
1444     default:
1445         return;
1446     }
1447     if (encode) {
1448         if (enc->flags & AV_CODEC_FLAG_PASS1)
1449             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1450                      ", pass 1");
1451         if (enc->flags & AV_CODEC_FLAG_PASS2)
1452             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1453                      ", pass 2");
1454     }
1455     bitrate = get_bit_rate(enc);
1456     if (bitrate != 0) {
1457         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1458                  ", %"PRId64" kb/s", bitrate / 1000);
1459     } else if (enc->rc_max_rate > 0) {
1460         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1461                  ", max. %"PRId64" kb/s", enc->rc_max_rate / 1000);
1462     }
1463 }
1464
1465 const char *av_get_profile_name(const AVCodec *codec, int profile)
1466 {
1467     const AVProfile *p;
1468     if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1469         return NULL;
1470
1471     for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1472         if (p->profile == profile)
1473             return p->name;
1474
1475     return NULL;
1476 }
1477
1478 const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
1479 {
1480     const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
1481     const AVProfile *p;
1482
1483     if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
1484         return NULL;
1485
1486     for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1487         if (p->profile == profile)
1488             return p->name;
1489
1490     return NULL;
1491 }
1492
1493 unsigned avcodec_version(void)
1494 {
1495 //    av_assert0(AV_CODEC_ID_V410==164);
1496     av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
1497     av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
1498 //     av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
1499     av_assert0(AV_CODEC_ID_SRT==94216);
1500     av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
1501
1502     return LIBAVCODEC_VERSION_INT;
1503 }
1504
1505 const char *avcodec_configuration(void)
1506 {
1507     return FFMPEG_CONFIGURATION;
1508 }
1509
1510 const char *avcodec_license(void)
1511 {
1512 #define LICENSE_PREFIX "libavcodec license: "
1513     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1514 }
1515
1516 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
1517 {
1518     switch (codec_id) {
1519     case AV_CODEC_ID_8SVX_EXP:
1520     case AV_CODEC_ID_8SVX_FIB:
1521     case AV_CODEC_ID_ADPCM_CT:
1522     case AV_CODEC_ID_ADPCM_IMA_APC:
1523     case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1524     case AV_CODEC_ID_ADPCM_IMA_OKI:
1525     case AV_CODEC_ID_ADPCM_IMA_WS:
1526     case AV_CODEC_ID_ADPCM_G722:
1527     case AV_CODEC_ID_ADPCM_YAMAHA:
1528     case AV_CODEC_ID_ADPCM_AICA:
1529         return 4;
1530     case AV_CODEC_ID_DSD_LSBF:
1531     case AV_CODEC_ID_DSD_MSBF:
1532     case AV_CODEC_ID_DSD_LSBF_PLANAR:
1533     case AV_CODEC_ID_DSD_MSBF_PLANAR:
1534     case AV_CODEC_ID_PCM_ALAW:
1535     case AV_CODEC_ID_PCM_MULAW:
1536     case AV_CODEC_ID_PCM_S8:
1537     case AV_CODEC_ID_PCM_S8_PLANAR:
1538     case AV_CODEC_ID_PCM_U8:
1539     case AV_CODEC_ID_PCM_ZORK:
1540     case AV_CODEC_ID_SDX2_DPCM:
1541         return 8;
1542     case AV_CODEC_ID_PCM_S16BE:
1543     case AV_CODEC_ID_PCM_S16BE_PLANAR:
1544     case AV_CODEC_ID_PCM_S16LE:
1545     case AV_CODEC_ID_PCM_S16LE_PLANAR:
1546     case AV_CODEC_ID_PCM_U16BE:
1547     case AV_CODEC_ID_PCM_U16LE:
1548         return 16;
1549     case AV_CODEC_ID_PCM_S24DAUD:
1550     case AV_CODEC_ID_PCM_S24BE:
1551     case AV_CODEC_ID_PCM_S24LE:
1552     case AV_CODEC_ID_PCM_S24LE_PLANAR:
1553     case AV_CODEC_ID_PCM_U24BE:
1554     case AV_CODEC_ID_PCM_U24LE:
1555         return 24;
1556     case AV_CODEC_ID_PCM_S32BE:
1557     case AV_CODEC_ID_PCM_S32LE:
1558     case AV_CODEC_ID_PCM_S32LE_PLANAR:
1559     case AV_CODEC_ID_PCM_U32BE:
1560     case AV_CODEC_ID_PCM_U32LE:
1561     case AV_CODEC_ID_PCM_F32BE:
1562     case AV_CODEC_ID_PCM_F32LE:
1563     case AV_CODEC_ID_PCM_F24LE:
1564     case AV_CODEC_ID_PCM_F16LE:
1565         return 32;
1566     case AV_CODEC_ID_PCM_F64BE:
1567     case AV_CODEC_ID_PCM_F64LE:
1568     case AV_CODEC_ID_PCM_S64BE:
1569     case AV_CODEC_ID_PCM_S64LE:
1570         return 64;
1571     default:
1572         return 0;
1573     }
1574 }
1575
1576 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
1577 {
1578     static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
1579         [AV_SAMPLE_FMT_U8  ] = { AV_CODEC_ID_PCM_U8,    AV_CODEC_ID_PCM_U8    },
1580         [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
1581         [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
1582         [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
1583         [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
1584         [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8,    AV_CODEC_ID_PCM_U8    },
1585         [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
1586         [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
1587         [AV_SAMPLE_FMT_S64P] = { AV_CODEC_ID_PCM_S64LE, AV_CODEC_ID_PCM_S64BE },
1588         [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
1589         [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
1590     };
1591     if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
1592         return AV_CODEC_ID_NONE;
1593     if (be < 0 || be > 1)
1594         be = AV_NE(1, 0);
1595     return map[fmt][be];
1596 }
1597
1598 int av_get_bits_per_sample(enum AVCodecID codec_id)
1599 {
1600     switch (codec_id) {
1601     case AV_CODEC_ID_ADPCM_SBPRO_2:
1602         return 2;
1603     case AV_CODEC_ID_ADPCM_SBPRO_3:
1604         return 3;
1605     case AV_CODEC_ID_ADPCM_SBPRO_4:
1606     case AV_CODEC_ID_ADPCM_IMA_WAV:
1607     case AV_CODEC_ID_ADPCM_IMA_QT:
1608     case AV_CODEC_ID_ADPCM_SWF:
1609     case AV_CODEC_ID_ADPCM_MS:
1610         return 4;
1611     default:
1612         return av_get_exact_bits_per_sample(codec_id);
1613     }
1614 }
1615
1616 static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
1617                                     uint32_t tag, int bits_per_coded_sample, int64_t bitrate,
1618                                     uint8_t * extradata, int frame_size, int frame_bytes)
1619 {
1620     int bps = av_get_exact_bits_per_sample(id);
1621     int framecount = (ba > 0 && frame_bytes / ba > 0) ? frame_bytes / ba : 1;
1622
1623     /* codecs with an exact constant bits per sample */
1624     if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
1625         return (frame_bytes * 8LL) / (bps * ch);
1626     bps = bits_per_coded_sample;
1627
1628     /* codecs with a fixed packet duration */
1629     switch (id) {
1630     case AV_CODEC_ID_ADPCM_ADX:    return   32;
1631     case AV_CODEC_ID_ADPCM_IMA_QT: return   64;
1632     case AV_CODEC_ID_ADPCM_EA_XAS: return  128;
1633     case AV_CODEC_ID_AMR_NB:
1634     case AV_CODEC_ID_EVRC:
1635     case AV_CODEC_ID_GSM:
1636     case AV_CODEC_ID_QCELP:
1637     case AV_CODEC_ID_RA_288:       return  160;
1638     case AV_CODEC_ID_AMR_WB:
1639     case AV_CODEC_ID_GSM_MS:       return  320;
1640     case AV_CODEC_ID_MP1:          return  384;
1641     case AV_CODEC_ID_ATRAC1:       return  512;
1642     case AV_CODEC_ID_ATRAC3:       return 1024 * framecount;
1643     case AV_CODEC_ID_ATRAC3P:      return 2048;
1644     case AV_CODEC_ID_MP2:
1645     case AV_CODEC_ID_MUSEPACK7:    return 1152;
1646     case AV_CODEC_ID_AC3:          return 1536;
1647     }
1648
1649     if (sr > 0) {
1650         /* calc from sample rate */
1651         if (id == AV_CODEC_ID_TTA)
1652             return 256 * sr / 245;
1653         else if (id == AV_CODEC_ID_DST)
1654             return 588 * sr / 44100;
1655
1656         if (ch > 0) {
1657             /* calc from sample rate and channels */
1658             if (id == AV_CODEC_ID_BINKAUDIO_DCT)
1659                 return (480 << (sr / 22050)) / ch;
1660         }
1661
1662         if (id == AV_CODEC_ID_MP3)
1663             return sr <= 24000 ? 576 : 1152;
1664     }
1665
1666     if (ba > 0) {
1667         /* calc from block_align */
1668         if (id == AV_CODEC_ID_SIPR) {
1669             switch (ba) {
1670             case 20: return 160;
1671             case 19: return 144;
1672             case 29: return 288;
1673             case 37: return 480;
1674             }
1675         } else if (id == AV_CODEC_ID_ILBC) {
1676             switch (ba) {
1677             case 38: return 160;
1678             case 50: return 240;
1679             }
1680         }
1681     }
1682
1683     if (frame_bytes > 0) {
1684         /* calc from frame_bytes only */
1685         if (id == AV_CODEC_ID_TRUESPEECH)
1686             return 240 * (frame_bytes / 32);
1687         if (id == AV_CODEC_ID_NELLYMOSER)
1688             return 256 * (frame_bytes / 64);
1689         if (id == AV_CODEC_ID_RA_144)
1690             return 160 * (frame_bytes / 20);
1691         if (id == AV_CODEC_ID_G723_1)
1692             return 240 * (frame_bytes / 24);
1693
1694         if (bps > 0) {
1695             /* calc from frame_bytes and bits_per_coded_sample */
1696             if (id == AV_CODEC_ID_ADPCM_G726 || id == AV_CODEC_ID_ADPCM_G726LE)
1697                 return frame_bytes * 8 / bps;
1698         }
1699
1700         if (ch > 0 && ch < INT_MAX/16) {
1701             /* calc from frame_bytes and channels */
1702             switch (id) {
1703             case AV_CODEC_ID_ADPCM_AFC:
1704                 return frame_bytes / (9 * ch) * 16;
1705             case AV_CODEC_ID_ADPCM_PSX:
1706             case AV_CODEC_ID_ADPCM_DTK:
1707                 return frame_bytes / (16 * ch) * 28;
1708             case AV_CODEC_ID_ADPCM_4XM:
1709             case AV_CODEC_ID_ADPCM_IMA_DAT4:
1710             case AV_CODEC_ID_ADPCM_IMA_ISS:
1711                 return (frame_bytes - 4 * ch) * 2 / ch;
1712             case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1713                 return (frame_bytes - 4) * 2 / ch;
1714             case AV_CODEC_ID_ADPCM_IMA_AMV:
1715                 return (frame_bytes - 8) * 2 / ch;
1716             case AV_CODEC_ID_ADPCM_THP:
1717             case AV_CODEC_ID_ADPCM_THP_LE:
1718                 if (extradata)
1719                     return frame_bytes * 14 / (8 * ch);
1720                 break;
1721             case AV_CODEC_ID_ADPCM_XA:
1722                 return (frame_bytes / 128) * 224 / ch;
1723             case AV_CODEC_ID_INTERPLAY_DPCM:
1724                 return (frame_bytes - 6 - ch) / ch;
1725             case AV_CODEC_ID_ROQ_DPCM:
1726                 return (frame_bytes - 8) / ch;
1727             case AV_CODEC_ID_XAN_DPCM:
1728                 return (frame_bytes - 2 * ch) / ch;
1729             case AV_CODEC_ID_MACE3:
1730                 return 3 * frame_bytes / ch;
1731             case AV_CODEC_ID_MACE6:
1732                 return 6 * frame_bytes / ch;
1733             case AV_CODEC_ID_PCM_LXF:
1734                 return 2 * (frame_bytes / (5 * ch));
1735             case AV_CODEC_ID_IAC:
1736             case AV_CODEC_ID_IMC:
1737                 return 4 * frame_bytes / ch;
1738             }
1739
1740             if (tag) {
1741                 /* calc from frame_bytes, channels, and codec_tag */
1742                 if (id == AV_CODEC_ID_SOL_DPCM) {
1743                     if (tag == 3)
1744                         return frame_bytes / ch;
1745                     else
1746                         return frame_bytes * 2 / ch;
1747                 }
1748             }
1749
1750             if (ba > 0) {
1751                 /* calc from frame_bytes, channels, and block_align */
1752                 int blocks = frame_bytes / ba;
1753                 switch (id) {
1754                 case AV_CODEC_ID_ADPCM_IMA_WAV:
1755                     if (bps < 2 || bps > 5)
1756                         return 0;
1757                     return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
1758                 case AV_CODEC_ID_ADPCM_IMA_DK3:
1759                     return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
1760                 case AV_CODEC_ID_ADPCM_IMA_DK4:
1761                     return blocks * (1 + (ba - 4 * ch) * 2 / ch);
1762                 case AV_CODEC_ID_ADPCM_IMA_RAD:
1763                     return blocks * ((ba - 4 * ch) * 2 / ch);
1764                 case AV_CODEC_ID_ADPCM_MS:
1765                     return blocks * (2 + (ba - 7 * ch) * 2 / ch);
1766                 case AV_CODEC_ID_ADPCM_MTAF:
1767                     return blocks * (ba - 16) * 2 / ch;
1768                 }
1769             }
1770
1771             if (bps > 0) {
1772                 /* calc from frame_bytes, channels, and bits_per_coded_sample */
1773                 switch (id) {
1774                 case AV_CODEC_ID_PCM_DVD:
1775                     if(bps<4 || frame_bytes<3)
1776                         return 0;
1777                     return 2 * ((frame_bytes - 3) / ((bps * 2 / 8) * ch));
1778                 case AV_CODEC_ID_PCM_BLURAY:
1779                     if(bps<4 || frame_bytes<4)
1780                         return 0;
1781                     return (frame_bytes - 4) / ((FFALIGN(ch, 2) * bps) / 8);
1782                 case AV_CODEC_ID_S302M:
1783                     return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
1784                 }
1785             }
1786         }
1787     }
1788
1789     /* Fall back on using frame_size */
1790     if (frame_size > 1 && frame_bytes)
1791         return frame_size;
1792
1793     //For WMA we currently have no other means to calculate duration thus we
1794     //do it here by assuming CBR, which is true for all known cases.
1795     if (bitrate > 0 && frame_bytes > 0 && sr > 0 && ba > 1) {
1796         if (id == AV_CODEC_ID_WMAV1 || id == AV_CODEC_ID_WMAV2)
1797             return  (frame_bytes * 8LL * sr) / bitrate;
1798     }
1799
1800     return 0;
1801 }
1802
1803 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
1804 {
1805     return get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
1806                                     avctx->channels, avctx->block_align,
1807                                     avctx->codec_tag, avctx->bits_per_coded_sample,
1808                                     avctx->bit_rate, avctx->extradata, avctx->frame_size,
1809                                     frame_bytes);
1810 }
1811
1812 int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
1813 {
1814     return get_audio_frame_duration(par->codec_id, par->sample_rate,
1815                                     par->channels, par->block_align,
1816                                     par->codec_tag, par->bits_per_coded_sample,
1817                                     par->bit_rate, par->extradata, par->frame_size,
1818                                     frame_bytes);
1819 }
1820
1821 #if !HAVE_THREADS
1822 int ff_thread_init(AVCodecContext *s)
1823 {
1824     return -1;
1825 }
1826
1827 #endif
1828
1829 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1830 {
1831     unsigned int n = 0;
1832
1833     while (v >= 0xff) {
1834         *s++ = 0xff;
1835         v -= 0xff;
1836         n++;
1837     }
1838     *s = v;
1839     n++;
1840     return n;
1841 }
1842
1843 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
1844 {
1845     int i;
1846     for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
1847     return i;
1848 }
1849
1850 const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *codec, int index)
1851 {
1852     int i;
1853     if (!codec->hw_configs || index < 0)
1854         return NULL;
1855     for (i = 0; i <= index; i++)
1856         if (!codec->hw_configs[i])
1857             return NULL;
1858     return &codec->hw_configs[index]->public;
1859 }
1860
1861 #if FF_API_USER_VISIBLE_AVHWACCEL
1862 AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
1863 {
1864     return NULL;
1865 }
1866
1867 void av_register_hwaccel(AVHWAccel *hwaccel)
1868 {
1869 }
1870 #endif
1871
1872 #if FF_API_LOCKMGR
1873 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1874 {
1875     return 0;
1876 }
1877 #endif
1878
1879 unsigned int avpriv_toupper4(unsigned int x)
1880 {
1881     return av_toupper(x & 0xFF) +
1882           (av_toupper((x >>  8) & 0xFF) << 8)  +
1883           (av_toupper((x >> 16) & 0xFF) << 16) +
1884 ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
1885 }
1886
1887 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
1888 {
1889     int ret;
1890
1891     dst->owner[0] = src->owner[0];
1892     dst->owner[1] = src->owner[1];
1893
1894     ret = av_frame_ref(dst->f, src->f);
1895     if (ret < 0)
1896         return ret;
1897
1898     av_assert0(!dst->progress);
1899
1900     if (src->progress &&
1901         !(dst->progress = av_buffer_ref(src->progress))) {
1902         ff_thread_release_buffer(dst->owner[0], dst);
1903         return AVERROR(ENOMEM);
1904     }
1905
1906     return 0;
1907 }
1908
1909 #if !HAVE_THREADS
1910
1911 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
1912 {
1913     return ff_get_format(avctx, fmt);
1914 }
1915
1916 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
1917 {
1918     f->owner[0] = f->owner[1] = avctx;
1919     return ff_get_buffer(avctx, f->f, flags);
1920 }
1921
1922 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
1923 {
1924     if (f->f)
1925         av_frame_unref(f->f);
1926 }
1927
1928 void ff_thread_finish_setup(AVCodecContext *avctx)
1929 {
1930 }
1931
1932 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
1933 {
1934 }
1935
1936 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
1937 {
1938 }
1939
1940 int ff_thread_can_start_frame(AVCodecContext *avctx)
1941 {
1942     return 1;
1943 }
1944
1945 int ff_alloc_entries(AVCodecContext *avctx, int count)
1946 {
1947     return 0;
1948 }
1949
1950 void ff_reset_entries(AVCodecContext *avctx)
1951 {
1952 }
1953
1954 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
1955 {
1956 }
1957
1958 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
1959 {
1960 }
1961
1962 #endif
1963
1964 int avcodec_is_open(AVCodecContext *s)
1965 {
1966     return !!s->internal;
1967 }
1968
1969 int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
1970 {
1971     int ret;
1972     char *str;
1973
1974     ret = av_bprint_finalize(buf, &str);
1975     if (ret < 0)
1976         return ret;
1977     if (!av_bprint_is_complete(buf)) {
1978         av_free(str);
1979         return AVERROR(ENOMEM);
1980     }
1981
1982     avctx->extradata = str;
1983     /* Note: the string is NUL terminated (so extradata can be read as a
1984      * string), but the ending character is not accounted in the size (in
1985      * binary formats you are likely not supposed to mux that character). When
1986      * extradata is copied, it is also padded with AV_INPUT_BUFFER_PADDING_SIZE
1987      * zeros. */
1988     avctx->extradata_size = buf->len;
1989     return 0;
1990 }
1991
1992 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
1993                                       const uint8_t *end,
1994                                       uint32_t *av_restrict state)
1995 {
1996     int i;
1997
1998     av_assert0(p <= end);
1999     if (p >= end)
2000         return end;
2001
2002     for (i = 0; i < 3; i++) {
2003         uint32_t tmp = *state << 8;
2004         *state = tmp + *(p++);
2005         if (tmp == 0x100 || p == end)
2006             return p;
2007     }
2008
2009     while (p < end) {
2010         if      (p[-1] > 1      ) p += 3;
2011         else if (p[-2]          ) p += 2;
2012         else if (p[-3]|(p[-1]-1)) p++;
2013         else {
2014             p++;
2015             break;
2016         }
2017     }
2018
2019     p = FFMIN(p, end) - 4;
2020     *state = AV_RB32(p);
2021
2022     return p + 4;
2023 }
2024
2025 AVCPBProperties *av_cpb_properties_alloc(size_t *size)
2026 {
2027     AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
2028     if (!props)
2029         return NULL;
2030
2031     if (size)
2032         *size = sizeof(*props);
2033
2034     props->vbv_delay = UINT64_MAX;
2035
2036     return props;
2037 }
2038
2039 AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
2040 {
2041     AVPacketSideData *tmp;
2042     AVCPBProperties  *props;
2043     size_t size;
2044
2045     props = av_cpb_properties_alloc(&size);
2046     if (!props)
2047         return NULL;
2048
2049     tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
2050     if (!tmp) {
2051         av_freep(&props);
2052         return NULL;
2053     }
2054
2055     avctx->coded_side_data = tmp;
2056     avctx->nb_coded_side_data++;
2057
2058     avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
2059     avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
2060     avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
2061
2062     return props;
2063 }
2064
2065 static void codec_parameters_reset(AVCodecParameters *par)
2066 {
2067     av_freep(&par->extradata);
2068
2069     memset(par, 0, sizeof(*par));
2070
2071     par->codec_type          = AVMEDIA_TYPE_UNKNOWN;
2072     par->codec_id            = AV_CODEC_ID_NONE;
2073     par->format              = -1;
2074     par->field_order         = AV_FIELD_UNKNOWN;
2075     par->color_range         = AVCOL_RANGE_UNSPECIFIED;
2076     par->color_primaries     = AVCOL_PRI_UNSPECIFIED;
2077     par->color_trc           = AVCOL_TRC_UNSPECIFIED;
2078     par->color_space         = AVCOL_SPC_UNSPECIFIED;
2079     par->chroma_location     = AVCHROMA_LOC_UNSPECIFIED;
2080     par->sample_aspect_ratio = (AVRational){ 0, 1 };
2081     par->profile             = FF_PROFILE_UNKNOWN;
2082     par->level               = FF_LEVEL_UNKNOWN;
2083 }
2084
2085 AVCodecParameters *avcodec_parameters_alloc(void)
2086 {
2087     AVCodecParameters *par = av_mallocz(sizeof(*par));
2088
2089     if (!par)
2090         return NULL;
2091     codec_parameters_reset(par);
2092     return par;
2093 }
2094
2095 void avcodec_parameters_free(AVCodecParameters **ppar)
2096 {
2097     AVCodecParameters *par = *ppar;
2098
2099     if (!par)
2100         return;
2101     codec_parameters_reset(par);
2102
2103     av_freep(ppar);
2104 }
2105
2106 int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
2107 {
2108     codec_parameters_reset(dst);
2109     memcpy(dst, src, sizeof(*dst));
2110
2111     dst->extradata      = NULL;
2112     dst->extradata_size = 0;
2113     if (src->extradata) {
2114         dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2115         if (!dst->extradata)
2116             return AVERROR(ENOMEM);
2117         memcpy(dst->extradata, src->extradata, src->extradata_size);
2118         dst->extradata_size = src->extradata_size;
2119     }
2120
2121     return 0;
2122 }
2123
2124 int avcodec_parameters_from_context(AVCodecParameters *par,
2125                                     const AVCodecContext *codec)
2126 {
2127     codec_parameters_reset(par);
2128
2129     par->codec_type = codec->codec_type;
2130     par->codec_id   = codec->codec_id;
2131     par->codec_tag  = codec->codec_tag;
2132
2133     par->bit_rate              = codec->bit_rate;
2134     par->bits_per_coded_sample = codec->bits_per_coded_sample;
2135     par->bits_per_raw_sample   = codec->bits_per_raw_sample;
2136     par->profile               = codec->profile;
2137     par->level                 = codec->level;
2138
2139     switch (par->codec_type) {
2140     case AVMEDIA_TYPE_VIDEO:
2141         par->format              = codec->pix_fmt;
2142         par->width               = codec->width;
2143         par->height              = codec->height;
2144         par->field_order         = codec->field_order;
2145         par->color_range         = codec->color_range;
2146         par->color_primaries     = codec->color_primaries;
2147         par->color_trc           = codec->color_trc;
2148         par->color_space         = codec->colorspace;
2149         par->chroma_location     = codec->chroma_sample_location;
2150         par->sample_aspect_ratio = codec->sample_aspect_ratio;
2151         par->video_delay         = codec->has_b_frames;
2152         break;
2153     case AVMEDIA_TYPE_AUDIO:
2154         par->format           = codec->sample_fmt;
2155         par->channel_layout   = codec->channel_layout;
2156         par->channels         = codec->channels;
2157         par->sample_rate      = codec->sample_rate;
2158         par->block_align      = codec->block_align;
2159         par->frame_size       = codec->frame_size;
2160         par->initial_padding  = codec->initial_padding;
2161         par->trailing_padding = codec->trailing_padding;
2162         par->seek_preroll     = codec->seek_preroll;
2163         break;
2164     case AVMEDIA_TYPE_SUBTITLE:
2165         par->width  = codec->width;
2166         par->height = codec->height;
2167         break;
2168     }
2169
2170     if (codec->extradata) {
2171         par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2172         if (!par->extradata)
2173             return AVERROR(ENOMEM);
2174         memcpy(par->extradata, codec->extradata, codec->extradata_size);
2175         par->extradata_size = codec->extradata_size;
2176     }
2177
2178     return 0;
2179 }
2180
2181 int avcodec_parameters_to_context(AVCodecContext *codec,
2182                                   const AVCodecParameters *par)
2183 {
2184     codec->codec_type = par->codec_type;
2185     codec->codec_id   = par->codec_id;
2186     codec->codec_tag  = par->codec_tag;
2187
2188     codec->bit_rate              = par->bit_rate;
2189     codec->bits_per_coded_sample = par->bits_per_coded_sample;
2190     codec->bits_per_raw_sample   = par->bits_per_raw_sample;
2191     codec->profile               = par->profile;
2192     codec->level                 = par->level;
2193
2194     switch (par->codec_type) {
2195     case AVMEDIA_TYPE_VIDEO:
2196         codec->pix_fmt                = par->format;
2197         codec->width                  = par->width;
2198         codec->height                 = par->height;
2199         codec->field_order            = par->field_order;
2200         codec->color_range            = par->color_range;
2201         codec->color_primaries        = par->color_primaries;
2202         codec->color_trc              = par->color_trc;
2203         codec->colorspace             = par->color_space;
2204         codec->chroma_sample_location = par->chroma_location;
2205         codec->sample_aspect_ratio    = par->sample_aspect_ratio;
2206         codec->has_b_frames           = par->video_delay;
2207         break;
2208     case AVMEDIA_TYPE_AUDIO:
2209         codec->sample_fmt       = par->format;
2210         codec->channel_layout   = par->channel_layout;
2211         codec->channels         = par->channels;
2212         codec->sample_rate      = par->sample_rate;
2213         codec->block_align      = par->block_align;
2214         codec->frame_size       = par->frame_size;
2215         codec->delay            =
2216         codec->initial_padding  = par->initial_padding;
2217         codec->trailing_padding = par->trailing_padding;
2218         codec->seek_preroll     = par->seek_preroll;
2219         break;
2220     case AVMEDIA_TYPE_SUBTITLE:
2221         codec->width  = par->width;
2222         codec->height = par->height;
2223         break;
2224     }
2225
2226     if (par->extradata) {
2227         av_freep(&codec->extradata);
2228         codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
2229         if (!codec->extradata)
2230             return AVERROR(ENOMEM);
2231         memcpy(codec->extradata, par->extradata, par->extradata_size);
2232         codec->extradata_size = par->extradata_size;
2233     }
2234
2235     return 0;
2236 }
2237
2238 int ff_alloc_a53_sei(const AVFrame *frame, size_t prefix_len,
2239                      void **data, size_t *sei_size)
2240 {
2241     AVFrameSideData *side_data = NULL;
2242     uint8_t *sei_data;
2243
2244     if (frame)
2245         side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC);
2246
2247     if (!side_data) {
2248         *data = NULL;
2249         return 0;
2250     }
2251
2252     *sei_size = side_data->size + 11;
2253     *data = av_mallocz(*sei_size + prefix_len);
2254     if (!*data)
2255         return AVERROR(ENOMEM);
2256     sei_data = (uint8_t*)*data + prefix_len;
2257
2258     // country code
2259     sei_data[0] = 181;
2260     sei_data[1] = 0;
2261     sei_data[2] = 49;
2262
2263     /**
2264      * 'GA94' is standard in North America for ATSC, but hard coding
2265      * this style may not be the right thing to do -- other formats
2266      * do exist. This information is not available in the side_data
2267      * so we are going with this right now.
2268      */
2269     AV_WL32(sei_data + 3, MKTAG('G', 'A', '9', '4'));
2270     sei_data[7] = 3;
2271     sei_data[8] = ((side_data->size/3) & 0x1f) | 0x40;
2272     sei_data[9] = 0;
2273
2274     memcpy(sei_data + 10, side_data->data, side_data->size);
2275
2276     sei_data[side_data->size+10] = 255;
2277
2278     return 0;
2279 }
2280
2281 int64_t ff_guess_coded_bitrate(AVCodecContext *avctx)
2282 {
2283     AVRational framerate = avctx->framerate;
2284     int bits_per_coded_sample = avctx->bits_per_coded_sample;
2285     int64_t bitrate;
2286
2287     if (!(framerate.num && framerate.den))
2288         framerate = av_inv_q(avctx->time_base);
2289     if (!(framerate.num && framerate.den))
2290         return 0;
2291
2292     if (!bits_per_coded_sample) {
2293         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
2294         bits_per_coded_sample = av_get_bits_per_pixel(desc);
2295     }
2296     bitrate = (int64_t)bits_per_coded_sample * avctx->width * avctx->height *
2297               framerate.num / framerate.den;
2298
2299     return bitrate;
2300 }