]> git.sesse.net Git - ffmpeg/blob - libavcodec/utils.c
avformat/avio: Add Metacube support
[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/avassert.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/mem_internal.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/pixfmt.h"
36 #include "avcodec.h"
37 #include "codec.h"
38 #include "hwconfig.h"
39 #include "thread.h"
40 #include "internal.h"
41 #include "put_bits.h"
42 #include "raw.h"
43 #include "version.h"
44 #include <stdlib.h>
45 #include <stdarg.h>
46 #include <stdatomic.h>
47 #include <limits.h>
48 #include <float.h>
49
50 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
51 {
52     uint8_t **p = ptr;
53     if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
54         av_freep(p);
55         *size = 0;
56         return;
57     }
58     if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
59         memset(*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
60 }
61
62 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
63 {
64     uint8_t **p = ptr;
65     if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
66         av_freep(p);
67         *size = 0;
68         return;
69     }
70     if (!ff_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE, 1))
71         memset(*p, 0, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
72 }
73
74 int av_codec_is_encoder(const AVCodec *codec)
75 {
76     return codec && (codec->encode_sub || codec->encode2 || codec->receive_packet);
77 }
78
79 int av_codec_is_decoder(const AVCodec *codec)
80 {
81     return codec && (codec->decode || codec->receive_frame);
82 }
83
84 int ff_set_dimensions(AVCodecContext *s, int width, int height)
85 {
86     int ret = av_image_check_size2(width, height, s->max_pixels, AV_PIX_FMT_NONE, 0, s);
87
88     if (ret < 0)
89         width = height = 0;
90
91     s->coded_width  = width;
92     s->coded_height = height;
93     s->width        = AV_CEIL_RSHIFT(width,  s->lowres);
94     s->height       = AV_CEIL_RSHIFT(height, s->lowres);
95
96     return ret;
97 }
98
99 int ff_set_sar(AVCodecContext *avctx, AVRational sar)
100 {
101     int ret = av_image_check_sar(avctx->width, avctx->height, sar);
102
103     if (ret < 0) {
104         av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
105                sar.num, sar.den);
106         avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
107         return ret;
108     } else {
109         avctx->sample_aspect_ratio = sar;
110     }
111     return 0;
112 }
113
114 int ff_side_data_update_matrix_encoding(AVFrame *frame,
115                                         enum AVMatrixEncoding matrix_encoding)
116 {
117     AVFrameSideData *side_data;
118     enum AVMatrixEncoding *data;
119
120     side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
121     if (!side_data)
122         side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
123                                            sizeof(enum AVMatrixEncoding));
124
125     if (!side_data)
126         return AVERROR(ENOMEM);
127
128     data  = (enum AVMatrixEncoding*)side_data->data;
129     *data = matrix_encoding;
130
131     return 0;
132 }
133
134 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
135                                int linesize_align[AV_NUM_DATA_POINTERS])
136 {
137     int i;
138     int w_align = 1;
139     int h_align = 1;
140     AVPixFmtDescriptor const *desc = av_pix_fmt_desc_get(s->pix_fmt);
141
142     if (desc) {
143         w_align = 1 << desc->log2_chroma_w;
144         h_align = 1 << desc->log2_chroma_h;
145     }
146
147     switch (s->pix_fmt) {
148     case AV_PIX_FMT_YUV420P:
149     case AV_PIX_FMT_YUYV422:
150     case AV_PIX_FMT_YVYU422:
151     case AV_PIX_FMT_UYVY422:
152     case AV_PIX_FMT_YUV422P:
153     case AV_PIX_FMT_YUV440P:
154     case AV_PIX_FMT_YUV444P:
155     case AV_PIX_FMT_GBRP:
156     case AV_PIX_FMT_GBRAP:
157     case AV_PIX_FMT_GRAY8:
158     case AV_PIX_FMT_GRAY16BE:
159     case AV_PIX_FMT_GRAY16LE:
160     case AV_PIX_FMT_YUVJ420P:
161     case AV_PIX_FMT_YUVJ422P:
162     case AV_PIX_FMT_YUVJ440P:
163     case AV_PIX_FMT_YUVJ444P:
164     case AV_PIX_FMT_YUVA420P:
165     case AV_PIX_FMT_YUVA422P:
166     case AV_PIX_FMT_YUVA444P:
167     case AV_PIX_FMT_YUV420P9LE:
168     case AV_PIX_FMT_YUV420P9BE:
169     case AV_PIX_FMT_YUV420P10LE:
170     case AV_PIX_FMT_YUV420P10BE:
171     case AV_PIX_FMT_YUV420P12LE:
172     case AV_PIX_FMT_YUV420P12BE:
173     case AV_PIX_FMT_YUV420P14LE:
174     case AV_PIX_FMT_YUV420P14BE:
175     case AV_PIX_FMT_YUV420P16LE:
176     case AV_PIX_FMT_YUV420P16BE:
177     case AV_PIX_FMT_YUVA420P9LE:
178     case AV_PIX_FMT_YUVA420P9BE:
179     case AV_PIX_FMT_YUVA420P10LE:
180     case AV_PIX_FMT_YUVA420P10BE:
181     case AV_PIX_FMT_YUVA420P16LE:
182     case AV_PIX_FMT_YUVA420P16BE:
183     case AV_PIX_FMT_YUV422P9LE:
184     case AV_PIX_FMT_YUV422P9BE:
185     case AV_PIX_FMT_YUV422P10LE:
186     case AV_PIX_FMT_YUV422P10BE:
187     case AV_PIX_FMT_YUV422P12LE:
188     case AV_PIX_FMT_YUV422P12BE:
189     case AV_PIX_FMT_YUV422P14LE:
190     case AV_PIX_FMT_YUV422P14BE:
191     case AV_PIX_FMT_YUV422P16LE:
192     case AV_PIX_FMT_YUV422P16BE:
193     case AV_PIX_FMT_YUVA422P9LE:
194     case AV_PIX_FMT_YUVA422P9BE:
195     case AV_PIX_FMT_YUVA422P10LE:
196     case AV_PIX_FMT_YUVA422P10BE:
197     case AV_PIX_FMT_YUVA422P12LE:
198     case AV_PIX_FMT_YUVA422P12BE:
199     case AV_PIX_FMT_YUVA422P16LE:
200     case AV_PIX_FMT_YUVA422P16BE:
201     case AV_PIX_FMT_YUV440P10LE:
202     case AV_PIX_FMT_YUV440P10BE:
203     case AV_PIX_FMT_YUV440P12LE:
204     case AV_PIX_FMT_YUV440P12BE:
205     case AV_PIX_FMT_YUV444P9LE:
206     case AV_PIX_FMT_YUV444P9BE:
207     case AV_PIX_FMT_YUV444P10LE:
208     case AV_PIX_FMT_YUV444P10BE:
209     case AV_PIX_FMT_YUV444P12LE:
210     case AV_PIX_FMT_YUV444P12BE:
211     case AV_PIX_FMT_YUV444P14LE:
212     case AV_PIX_FMT_YUV444P14BE:
213     case AV_PIX_FMT_YUV444P16LE:
214     case AV_PIX_FMT_YUV444P16BE:
215     case AV_PIX_FMT_YUVA444P9LE:
216     case AV_PIX_FMT_YUVA444P9BE:
217     case AV_PIX_FMT_YUVA444P10LE:
218     case AV_PIX_FMT_YUVA444P10BE:
219     case AV_PIX_FMT_YUVA444P12LE:
220     case AV_PIX_FMT_YUVA444P12BE:
221     case AV_PIX_FMT_YUVA444P16LE:
222     case AV_PIX_FMT_YUVA444P16BE:
223     case AV_PIX_FMT_GBRP9LE:
224     case AV_PIX_FMT_GBRP9BE:
225     case AV_PIX_FMT_GBRP10LE:
226     case AV_PIX_FMT_GBRP10BE:
227     case AV_PIX_FMT_GBRP12LE:
228     case AV_PIX_FMT_GBRP12BE:
229     case AV_PIX_FMT_GBRP14LE:
230     case AV_PIX_FMT_GBRP14BE:
231     case AV_PIX_FMT_GBRP16LE:
232     case AV_PIX_FMT_GBRP16BE:
233     case AV_PIX_FMT_GBRAP12LE:
234     case AV_PIX_FMT_GBRAP12BE:
235     case AV_PIX_FMT_GBRAP16LE:
236     case AV_PIX_FMT_GBRAP16BE:
237         w_align = 16; //FIXME assume 16 pixel per macroblock
238         h_align = 16 * 2; // interlaced needs 2 macroblocks height
239         break;
240     case AV_PIX_FMT_YUV411P:
241     case AV_PIX_FMT_YUVJ411P:
242     case AV_PIX_FMT_UYYVYY411:
243         w_align = 32;
244         h_align = 16 * 2;
245         break;
246     case AV_PIX_FMT_YUV410P:
247         if (s->codec_id == AV_CODEC_ID_SVQ1) {
248             w_align = 64;
249             h_align = 64;
250         }
251         break;
252     case AV_PIX_FMT_RGB555:
253         if (s->codec_id == AV_CODEC_ID_RPZA) {
254             w_align = 4;
255             h_align = 4;
256         }
257         if (s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
258             w_align = 8;
259             h_align = 8;
260         }
261         break;
262     case AV_PIX_FMT_PAL8:
263     case AV_PIX_FMT_BGR8:
264     case AV_PIX_FMT_RGB8:
265         if (s->codec_id == AV_CODEC_ID_SMC ||
266             s->codec_id == AV_CODEC_ID_CINEPAK) {
267             w_align = 4;
268             h_align = 4;
269         }
270         if (s->codec_id == AV_CODEC_ID_JV ||
271             s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
272             w_align = 8;
273             h_align = 8;
274         }
275         break;
276     case AV_PIX_FMT_BGR24:
277         if ((s->codec_id == AV_CODEC_ID_MSZH) ||
278             (s->codec_id == AV_CODEC_ID_ZLIB)) {
279             w_align = 4;
280             h_align = 4;
281         }
282         break;
283     case AV_PIX_FMT_RGB24:
284         if (s->codec_id == AV_CODEC_ID_CINEPAK) {
285             w_align = 4;
286             h_align = 4;
287         }
288         break;
289     default:
290         break;
291     }
292
293     if (s->codec_id == AV_CODEC_ID_IFF_ILBM) {
294         w_align = FFMAX(w_align, 8);
295     }
296
297     *width  = FFALIGN(*width, w_align);
298     *height = FFALIGN(*height, h_align);
299     if (s->codec_id == AV_CODEC_ID_H264 || s->lowres ||
300         s->codec_id == AV_CODEC_ID_VP5  || s->codec_id == AV_CODEC_ID_VP6 ||
301         s->codec_id == AV_CODEC_ID_VP6F || s->codec_id == AV_CODEC_ID_VP6A
302     ) {
303         // some of the optimized chroma MC reads one line too much
304         // which is also done in mpeg decoders with lowres > 0
305         *height += 2;
306
307         // H.264 uses edge emulation for out of frame motion vectors, for this
308         // it requires a temporary area large enough to hold a 21x21 block,
309         // increasing witdth ensure that the temporary area is large enough,
310         // the next rounded up width is 32
311         *width = FFMAX(*width, 32);
312     }
313
314     for (i = 0; i < 4; i++)
315         linesize_align[i] = STRIDE_ALIGN;
316 }
317
318 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
319 {
320     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
321     int chroma_shift = desc->log2_chroma_w;
322     int linesize_align[AV_NUM_DATA_POINTERS];
323     int align;
324
325     avcodec_align_dimensions2(s, width, height, linesize_align);
326     align               = FFMAX(linesize_align[0], linesize_align[3]);
327     linesize_align[1] <<= chroma_shift;
328     linesize_align[2] <<= chroma_shift;
329     align               = FFMAX3(align, linesize_align[1], linesize_align[2]);
330     *width              = FFALIGN(*width, align);
331 }
332
333 int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
334 {
335     if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
336         return AVERROR(EINVAL);
337     pos--;
338
339     *xpos = (pos&1) * 128;
340     *ypos = ((pos>>1)^(pos<4)) * 128;
341
342     return 0;
343 }
344
345 enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
346 {
347     int pos, xout, yout;
348
349     for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
350         if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
351             return pos;
352     }
353     return AVCHROMA_LOC_UNSPECIFIED;
354 }
355
356 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
357                              enum AVSampleFormat sample_fmt, const uint8_t *buf,
358                              int buf_size, int align)
359 {
360     int ch, planar, needed_size, ret = 0;
361
362     needed_size = av_samples_get_buffer_size(NULL, nb_channels,
363                                              frame->nb_samples, sample_fmt,
364                                              align);
365     if (buf_size < needed_size)
366         return AVERROR(EINVAL);
367
368     planar = av_sample_fmt_is_planar(sample_fmt);
369     if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
370         if (!(frame->extended_data = av_mallocz_array(nb_channels,
371                                                 sizeof(*frame->extended_data))))
372             return AVERROR(ENOMEM);
373     } else {
374         frame->extended_data = frame->data;
375     }
376
377     if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
378                                       (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
379                                       sample_fmt, align)) < 0) {
380         if (frame->extended_data != frame->data)
381             av_freep(&frame->extended_data);
382         return ret;
383     }
384     if (frame->extended_data != frame->data) {
385         for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
386             frame->data[ch] = frame->extended_data[ch];
387     }
388
389     return ret;
390 }
391
392 void ff_color_frame(AVFrame *frame, const int c[4])
393 {
394     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
395     int p, y;
396
397     av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
398
399     for (p = 0; p<desc->nb_components; p++) {
400         uint8_t *dst = frame->data[p];
401         int is_chroma = p == 1 || p == 2;
402         int bytes  = is_chroma ? AV_CEIL_RSHIFT(frame->width,  desc->log2_chroma_w) : frame->width;
403         int height = is_chroma ? AV_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
404         if (desc->comp[0].depth >= 9) {
405             ((uint16_t*)dst)[0] = c[p];
406             av_memcpy_backptr(dst + 2, 2, bytes - 2);
407             dst += frame->linesize[p];
408             for (y = 1; y < height; y++) {
409                 memcpy(dst, frame->data[p], 2*bytes);
410                 dst += frame->linesize[p];
411             }
412         } else {
413             for (y = 0; y < height; y++) {
414                 memset(dst, c[p], bytes);
415                 dst += frame->linesize[p];
416             }
417         }
418     }
419 }
420
421 enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
422                                        unsigned int fourcc)
423 {
424     while (tags->pix_fmt >= 0) {
425         if (tags->fourcc == fourcc)
426             return tags->pix_fmt;
427         tags++;
428     }
429     return AV_PIX_FMT_NONE;
430 }
431
432 int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec){
433     return !!(codec->caps_internal & FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM);
434 }
435
436 const char *avcodec_get_name(enum AVCodecID id)
437 {
438     const AVCodecDescriptor *cd;
439     const AVCodec *codec;
440
441     if (id == AV_CODEC_ID_NONE)
442         return "none";
443     cd = avcodec_descriptor_get(id);
444     if (cd)
445         return cd->name;
446     av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
447     codec = avcodec_find_decoder(id);
448     if (codec)
449         return codec->name;
450     codec = avcodec_find_encoder(id);
451     if (codec)
452         return codec->name;
453     return "unknown_codec";
454 }
455
456 const char *av_get_profile_name(const AVCodec *codec, int profile)
457 {
458     const AVProfile *p;
459     if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
460         return NULL;
461
462     for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
463         if (p->profile == profile)
464             return p->name;
465
466     return NULL;
467 }
468
469 const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
470 {
471     const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
472     const AVProfile *p;
473
474     if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
475         return NULL;
476
477     for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
478         if (p->profile == profile)
479             return p->name;
480
481     return NULL;
482 }
483
484 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
485 {
486     switch (codec_id) {
487     case AV_CODEC_ID_8SVX_EXP:
488     case AV_CODEC_ID_8SVX_FIB:
489     case AV_CODEC_ID_ADPCM_ARGO:
490     case AV_CODEC_ID_ADPCM_CT:
491     case AV_CODEC_ID_ADPCM_IMA_ALP:
492     case AV_CODEC_ID_ADPCM_IMA_AMV:
493     case AV_CODEC_ID_ADPCM_IMA_APC:
494     case AV_CODEC_ID_ADPCM_IMA_APM:
495     case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
496     case AV_CODEC_ID_ADPCM_IMA_OKI:
497     case AV_CODEC_ID_ADPCM_IMA_WS:
498     case AV_CODEC_ID_ADPCM_IMA_SSI:
499     case AV_CODEC_ID_ADPCM_G722:
500     case AV_CODEC_ID_ADPCM_YAMAHA:
501     case AV_CODEC_ID_ADPCM_AICA:
502         return 4;
503     case AV_CODEC_ID_DSD_LSBF:
504     case AV_CODEC_ID_DSD_MSBF:
505     case AV_CODEC_ID_DSD_LSBF_PLANAR:
506     case AV_CODEC_ID_DSD_MSBF_PLANAR:
507     case AV_CODEC_ID_PCM_ALAW:
508     case AV_CODEC_ID_PCM_MULAW:
509     case AV_CODEC_ID_PCM_VIDC:
510     case AV_CODEC_ID_PCM_S8:
511     case AV_CODEC_ID_PCM_S8_PLANAR:
512     case AV_CODEC_ID_PCM_SGA:
513     case AV_CODEC_ID_PCM_U8:
514     case AV_CODEC_ID_SDX2_DPCM:
515     case AV_CODEC_ID_DERF_DPCM:
516         return 8;
517     case AV_CODEC_ID_PCM_S16BE:
518     case AV_CODEC_ID_PCM_S16BE_PLANAR:
519     case AV_CODEC_ID_PCM_S16LE:
520     case AV_CODEC_ID_PCM_S16LE_PLANAR:
521     case AV_CODEC_ID_PCM_U16BE:
522     case AV_CODEC_ID_PCM_U16LE:
523         return 16;
524     case AV_CODEC_ID_PCM_S24DAUD:
525     case AV_CODEC_ID_PCM_S24BE:
526     case AV_CODEC_ID_PCM_S24LE:
527     case AV_CODEC_ID_PCM_S24LE_PLANAR:
528     case AV_CODEC_ID_PCM_U24BE:
529     case AV_CODEC_ID_PCM_U24LE:
530         return 24;
531     case AV_CODEC_ID_PCM_S32BE:
532     case AV_CODEC_ID_PCM_S32LE:
533     case AV_CODEC_ID_PCM_S32LE_PLANAR:
534     case AV_CODEC_ID_PCM_U32BE:
535     case AV_CODEC_ID_PCM_U32LE:
536     case AV_CODEC_ID_PCM_F32BE:
537     case AV_CODEC_ID_PCM_F32LE:
538     case AV_CODEC_ID_PCM_F24LE:
539     case AV_CODEC_ID_PCM_F16LE:
540         return 32;
541     case AV_CODEC_ID_PCM_F64BE:
542     case AV_CODEC_ID_PCM_F64LE:
543     case AV_CODEC_ID_PCM_S64BE:
544     case AV_CODEC_ID_PCM_S64LE:
545         return 64;
546     default:
547         return 0;
548     }
549 }
550
551 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
552 {
553     static const enum AVCodecID map[][2] = {
554         [AV_SAMPLE_FMT_U8  ] = { AV_CODEC_ID_PCM_U8,    AV_CODEC_ID_PCM_U8    },
555         [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
556         [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
557         [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
558         [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
559         [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8,    AV_CODEC_ID_PCM_U8    },
560         [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
561         [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
562         [AV_SAMPLE_FMT_S64P] = { AV_CODEC_ID_PCM_S64LE, AV_CODEC_ID_PCM_S64BE },
563         [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
564         [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
565     };
566     if (fmt < 0 || fmt >= FF_ARRAY_ELEMS(map))
567         return AV_CODEC_ID_NONE;
568     if (be < 0 || be > 1)
569         be = AV_NE(1, 0);
570     return map[fmt][be];
571 }
572
573 int av_get_bits_per_sample(enum AVCodecID codec_id)
574 {
575     switch (codec_id) {
576     case AV_CODEC_ID_ADPCM_SBPRO_2:
577         return 2;
578     case AV_CODEC_ID_ADPCM_SBPRO_3:
579         return 3;
580     case AV_CODEC_ID_ADPCM_SBPRO_4:
581     case AV_CODEC_ID_ADPCM_IMA_WAV:
582     case AV_CODEC_ID_ADPCM_IMA_QT:
583     case AV_CODEC_ID_ADPCM_SWF:
584     case AV_CODEC_ID_ADPCM_MS:
585         return 4;
586     default:
587         return av_get_exact_bits_per_sample(codec_id);
588     }
589 }
590
591 static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
592                                     uint32_t tag, int bits_per_coded_sample, int64_t bitrate,
593                                     uint8_t * extradata, int frame_size, int frame_bytes)
594 {
595     int bps = av_get_exact_bits_per_sample(id);
596     int framecount = (ba > 0 && frame_bytes / ba > 0) ? frame_bytes / ba : 1;
597
598     /* codecs with an exact constant bits per sample */
599     if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
600         return (frame_bytes * 8LL) / (bps * ch);
601     bps = bits_per_coded_sample;
602
603     /* codecs with a fixed packet duration */
604     switch (id) {
605     case AV_CODEC_ID_ADPCM_ADX:    return   32;
606     case AV_CODEC_ID_ADPCM_IMA_QT: return   64;
607     case AV_CODEC_ID_ADPCM_EA_XAS: return  128;
608     case AV_CODEC_ID_AMR_NB:
609     case AV_CODEC_ID_EVRC:
610     case AV_CODEC_ID_GSM:
611     case AV_CODEC_ID_QCELP:
612     case AV_CODEC_ID_RA_288:       return  160;
613     case AV_CODEC_ID_AMR_WB:
614     case AV_CODEC_ID_GSM_MS:       return  320;
615     case AV_CODEC_ID_MP1:          return  384;
616     case AV_CODEC_ID_ATRAC1:       return  512;
617     case AV_CODEC_ID_ATRAC9:
618     case AV_CODEC_ID_ATRAC3:
619         if (framecount > INT_MAX/1024)
620             return 0;
621         return 1024 * framecount;
622     case AV_CODEC_ID_ATRAC3P:      return 2048;
623     case AV_CODEC_ID_MP2:
624     case AV_CODEC_ID_MUSEPACK7:    return 1152;
625     case AV_CODEC_ID_AC3:          return 1536;
626     }
627
628     if (sr > 0) {
629         /* calc from sample rate */
630         if (id == AV_CODEC_ID_TTA)
631             return 256 * sr / 245;
632         else if (id == AV_CODEC_ID_DST)
633             return 588 * sr / 44100;
634         else if (id == AV_CODEC_ID_BINKAUDIO_DCT) {
635             if (sr / 22050 > 22)
636                 return 0;
637             return (480 << (sr / 22050));
638         }
639
640         if (id == AV_CODEC_ID_MP3)
641             return sr <= 24000 ? 576 : 1152;
642     }
643
644     if (ba > 0) {
645         /* calc from block_align */
646         if (id == AV_CODEC_ID_SIPR) {
647             switch (ba) {
648             case 20: return 160;
649             case 19: return 144;
650             case 29: return 288;
651             case 37: return 480;
652             }
653         } else if (id == AV_CODEC_ID_ILBC) {
654             switch (ba) {
655             case 38: return 160;
656             case 50: return 240;
657             }
658         }
659     }
660
661     if (frame_bytes > 0) {
662         /* calc from frame_bytes only */
663         if (id == AV_CODEC_ID_TRUESPEECH)
664             return 240 * (frame_bytes / 32);
665         if (id == AV_CODEC_ID_NELLYMOSER)
666             return 256 * (frame_bytes / 64);
667         if (id == AV_CODEC_ID_RA_144)
668             return 160 * (frame_bytes / 20);
669
670         if (bps > 0) {
671             /* calc from frame_bytes and bits_per_coded_sample */
672             if (id == AV_CODEC_ID_ADPCM_G726 || id == AV_CODEC_ID_ADPCM_G726LE)
673                 return frame_bytes * 8 / bps;
674         }
675
676         if (ch > 0 && ch < INT_MAX/16) {
677             /* calc from frame_bytes and channels */
678             switch (id) {
679             case AV_CODEC_ID_FASTAUDIO:
680                 return frame_bytes / (40 * ch) * 256;
681             case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
682                 return (frame_bytes - 4 * ch) / (128 * ch) * 256;
683             case AV_CODEC_ID_ADPCM_AFC:
684                 return frame_bytes / (9 * ch) * 16;
685             case AV_CODEC_ID_ADPCM_PSX:
686             case AV_CODEC_ID_ADPCM_DTK:
687                 frame_bytes /= 16 * ch;
688                 if (frame_bytes > INT_MAX / 28)
689                     return 0;
690                 return frame_bytes * 28;
691             case AV_CODEC_ID_ADPCM_4XM:
692             case AV_CODEC_ID_ADPCM_IMA_DAT4:
693             case AV_CODEC_ID_ADPCM_IMA_ISS:
694                 return (frame_bytes - 4 * ch) * 2 / ch;
695             case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
696                 return (frame_bytes - 4) * 2 / ch;
697             case AV_CODEC_ID_ADPCM_IMA_AMV:
698                 return (frame_bytes - 8) * 2;
699             case AV_CODEC_ID_ADPCM_THP:
700             case AV_CODEC_ID_ADPCM_THP_LE:
701                 if (extradata)
702                     return frame_bytes * 14 / (8 * ch);
703                 break;
704             case AV_CODEC_ID_ADPCM_XA:
705                 return (frame_bytes / 128) * 224 / ch;
706             case AV_CODEC_ID_INTERPLAY_DPCM:
707                 return (frame_bytes - 6 - ch) / ch;
708             case AV_CODEC_ID_ROQ_DPCM:
709                 return (frame_bytes - 8) / ch;
710             case AV_CODEC_ID_XAN_DPCM:
711                 return (frame_bytes - 2 * ch) / ch;
712             case AV_CODEC_ID_MACE3:
713                 return 3 * frame_bytes / ch;
714             case AV_CODEC_ID_MACE6:
715                 return 6 * frame_bytes / ch;
716             case AV_CODEC_ID_PCM_LXF:
717                 return 2 * (frame_bytes / (5 * ch));
718             case AV_CODEC_ID_IAC:
719             case AV_CODEC_ID_IMC:
720                 return 4 * frame_bytes / ch;
721             }
722
723             if (tag) {
724                 /* calc from frame_bytes, channels, and codec_tag */
725                 if (id == AV_CODEC_ID_SOL_DPCM) {
726                     if (tag == 3)
727                         return frame_bytes / ch;
728                     else
729                         return frame_bytes * 2 / ch;
730                 }
731             }
732
733             if (ba > 0) {
734                 /* calc from frame_bytes, channels, and block_align */
735                 int blocks = frame_bytes / ba;
736                 int64_t tmp;
737                 switch (id) {
738                 case AV_CODEC_ID_ADPCM_IMA_WAV:
739                     if (bps < 2 || bps > 5)
740                         return 0;
741                     tmp = blocks * (1LL + (ba - 4 * ch) / (bps * ch) * 8);
742                     if (tmp != (int)tmp)
743                         return 0;
744                     return tmp;
745                 case AV_CODEC_ID_ADPCM_IMA_DK3:
746                     return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
747                 case AV_CODEC_ID_ADPCM_IMA_DK4:
748                     return blocks * (1 + (ba - 4 * ch) * 2 / ch);
749                 case AV_CODEC_ID_ADPCM_IMA_RAD:
750                     return blocks * ((ba - 4 * ch) * 2 / ch);
751                 case AV_CODEC_ID_ADPCM_MS:
752                     return blocks * (2 + (ba - 7 * ch) * 2LL / ch);
753                 case AV_CODEC_ID_ADPCM_MTAF:
754                     return blocks * (ba - 16) * 2 / ch;
755                 }
756             }
757
758             if (bps > 0) {
759                 /* calc from frame_bytes, channels, and bits_per_coded_sample */
760                 switch (id) {
761                 case AV_CODEC_ID_PCM_DVD:
762                     if(bps<4 || frame_bytes<3)
763                         return 0;
764                     return 2 * ((frame_bytes - 3) / ((bps * 2 / 8) * ch));
765                 case AV_CODEC_ID_PCM_BLURAY:
766                     if(bps<4 || frame_bytes<4)
767                         return 0;
768                     return (frame_bytes - 4) / ((FFALIGN(ch, 2) * bps) / 8);
769                 case AV_CODEC_ID_S302M:
770                     return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
771                 }
772             }
773         }
774     }
775
776     /* Fall back on using frame_size */
777     if (frame_size > 1 && frame_bytes)
778         return frame_size;
779
780     //For WMA we currently have no other means to calculate duration thus we
781     //do it here by assuming CBR, which is true for all known cases.
782     if (bitrate > 0 && frame_bytes > 0 && sr > 0 && ba > 1) {
783         if (id == AV_CODEC_ID_WMAV1 || id == AV_CODEC_ID_WMAV2)
784             return  (frame_bytes * 8LL * sr) / bitrate;
785     }
786
787     return 0;
788 }
789
790 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
791 {
792     return get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
793                                     avctx->channels, avctx->block_align,
794                                     avctx->codec_tag, avctx->bits_per_coded_sample,
795                                     avctx->bit_rate, avctx->extradata, avctx->frame_size,
796                                     frame_bytes);
797 }
798
799 int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
800 {
801     return get_audio_frame_duration(par->codec_id, par->sample_rate,
802                                     par->channels, par->block_align,
803                                     par->codec_tag, par->bits_per_coded_sample,
804                                     par->bit_rate, par->extradata, par->frame_size,
805                                     frame_bytes);
806 }
807
808 #if !HAVE_THREADS
809 int ff_thread_init(AVCodecContext *s)
810 {
811     return -1;
812 }
813
814 #endif
815
816 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
817 {
818     unsigned int n = 0;
819
820     while (v >= 0xff) {
821         *s++ = 0xff;
822         v -= 0xff;
823         n++;
824     }
825     *s = v;
826     n++;
827     return n;
828 }
829
830 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
831 {
832     int i;
833     for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
834     return i;
835 }
836
837 const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *codec, int index)
838 {
839     int i;
840     if (!codec->hw_configs || index < 0)
841         return NULL;
842     for (i = 0; i <= index; i++)
843         if (!codec->hw_configs[i])
844             return NULL;
845     return &codec->hw_configs[index]->public;
846 }
847
848 unsigned int avpriv_toupper4(unsigned int x)
849 {
850     return av_toupper(x & 0xFF) +
851           (av_toupper((x >>  8) & 0xFF) << 8)  +
852           (av_toupper((x >> 16) & 0xFF) << 16) +
853 ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
854 }
855
856 int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src)
857 {
858     int ret;
859
860     dst->owner[0] = src->owner[0];
861     dst->owner[1] = src->owner[1];
862
863     ret = av_frame_ref(dst->f, src->f);
864     if (ret < 0)
865         return ret;
866
867     av_assert0(!dst->progress);
868
869     if (src->progress &&
870         !(dst->progress = av_buffer_ref(src->progress))) {
871         ff_thread_release_buffer(dst->owner[0], dst);
872         return AVERROR(ENOMEM);
873     }
874
875     return 0;
876 }
877
878 #if !HAVE_THREADS
879
880 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
881 {
882     return ff_get_format(avctx, fmt);
883 }
884
885 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
886 {
887     f->owner[0] = f->owner[1] = avctx;
888     return ff_get_buffer(avctx, f->f, flags);
889 }
890
891 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
892 {
893     if (f->f)
894         av_frame_unref(f->f);
895 }
896
897 void ff_thread_finish_setup(AVCodecContext *avctx)
898 {
899 }
900
901 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
902 {
903 }
904
905 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
906 {
907 }
908
909 int ff_thread_can_start_frame(AVCodecContext *avctx)
910 {
911     return 1;
912 }
913
914 int ff_alloc_entries(AVCodecContext *avctx, int count)
915 {
916     return 0;
917 }
918
919 void ff_reset_entries(AVCodecContext *avctx)
920 {
921 }
922
923 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
924 {
925 }
926
927 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
928 {
929 }
930
931 #endif
932
933 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
934                                       const uint8_t *end,
935                                       uint32_t *av_restrict state)
936 {
937     int i;
938
939     av_assert0(p <= end);
940     if (p >= end)
941         return end;
942
943     for (i = 0; i < 3; i++) {
944         uint32_t tmp = *state << 8;
945         *state = tmp + *(p++);
946         if (tmp == 0x100 || p == end)
947             return p;
948     }
949
950     while (p < end) {
951         if      (p[-1] > 1      ) p += 3;
952         else if (p[-2]          ) p += 2;
953         else if (p[-3]|(p[-1]-1)) p++;
954         else {
955             p++;
956             break;
957         }
958     }
959
960     p = FFMIN(p, end) - 4;
961     *state = AV_RB32(p);
962
963     return p + 4;
964 }
965
966 AVCPBProperties *av_cpb_properties_alloc(size_t *size)
967 {
968     AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
969     if (!props)
970         return NULL;
971
972     if (size)
973         *size = sizeof(*props);
974
975     props->vbv_delay = UINT64_MAX;
976
977     return props;
978 }
979
980 AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
981 {
982     AVPacketSideData *tmp;
983     AVCPBProperties  *props;
984     size_t size;
985     int i;
986
987     for (i = 0; i < avctx->nb_coded_side_data; i++)
988         if (avctx->coded_side_data[i].type == AV_PKT_DATA_CPB_PROPERTIES)
989             return (AVCPBProperties *)avctx->coded_side_data[i].data;
990
991     props = av_cpb_properties_alloc(&size);
992     if (!props)
993         return NULL;
994
995     tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
996     if (!tmp) {
997         av_freep(&props);
998         return NULL;
999     }
1000
1001     avctx->coded_side_data = tmp;
1002     avctx->nb_coded_side_data++;
1003
1004     avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
1005     avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
1006     avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
1007
1008     return props;
1009 }
1010
1011 static unsigned bcd2uint(uint8_t bcd)
1012 {
1013     unsigned low  = bcd & 0xf;
1014     unsigned high = bcd >> 4;
1015     if (low > 9 || high > 9)
1016         return 0;
1017     return low + 10*high;
1018 }
1019
1020 int ff_alloc_timecode_sei(const AVFrame *frame, AVRational rate, size_t prefix_len,
1021                      void **data, size_t *sei_size)
1022 {
1023     AVFrameSideData *sd = NULL;
1024     uint8_t *sei_data;
1025     PutBitContext pb;
1026     uint32_t *tc;
1027     int m;
1028
1029     if (frame)
1030         sd = av_frame_get_side_data(frame, AV_FRAME_DATA_S12M_TIMECODE);
1031
1032     if (!sd) {
1033         *data = NULL;
1034         return 0;
1035     }
1036     tc =  (uint32_t*)sd->data;
1037     m  = tc[0] & 3;
1038
1039     *sei_size = sizeof(uint32_t) * 4;
1040     *data = av_mallocz(*sei_size + prefix_len);
1041     if (!*data)
1042         return AVERROR(ENOMEM);
1043     sei_data = (uint8_t*)*data + prefix_len;
1044
1045     init_put_bits(&pb, sei_data, *sei_size);
1046     put_bits(&pb, 2, m); // num_clock_ts
1047
1048     for (int j = 1; j <= m; j++) {
1049         uint32_t tcsmpte = tc[j];
1050         unsigned hh   = bcd2uint(tcsmpte     & 0x3f);    // 6-bit hours
1051         unsigned mm   = bcd2uint(tcsmpte>>8  & 0x7f);    // 7-bit minutes
1052         unsigned ss   = bcd2uint(tcsmpte>>16 & 0x7f);    // 7-bit seconds
1053         unsigned ff   = bcd2uint(tcsmpte>>24 & 0x3f);    // 6-bit frames
1054         unsigned drop = tcsmpte & 1<<30 && !0;  // 1-bit drop if not arbitrary bit
1055
1056         /* Calculate frame number of HEVC by SMPTE ST 12-1:2014 Sec 12.2 if rate > 30FPS */
1057         if (av_cmp_q(rate, (AVRational) {30, 1}) == 1) {
1058             unsigned pc;
1059             ff *= 2;
1060             if (av_cmp_q(rate, (AVRational) {50, 1}) == 0)
1061                 pc = !!(tcsmpte & 1 << 7);
1062             else
1063                 pc = !!(tcsmpte & 1 << 23);
1064             ff = (ff + pc) & 0x7f;
1065         }
1066
1067         put_bits(&pb, 1, 1); // clock_timestamp_flag
1068         put_bits(&pb, 1, 1); // units_field_based_flag
1069         put_bits(&pb, 5, 0); // counting_type
1070         put_bits(&pb, 1, 1); // full_timestamp_flag
1071         put_bits(&pb, 1, 0); // discontinuity_flag
1072         put_bits(&pb, 1, drop);
1073         put_bits(&pb, 9, ff);
1074         put_bits(&pb, 6, ss);
1075         put_bits(&pb, 6, mm);
1076         put_bits(&pb, 5, hh);
1077         put_bits(&pb, 5, 0);
1078     }
1079     flush_put_bits(&pb);
1080
1081     return 0;
1082 }
1083
1084 int64_t ff_guess_coded_bitrate(AVCodecContext *avctx)
1085 {
1086     AVRational framerate = avctx->framerate;
1087     int bits_per_coded_sample = avctx->bits_per_coded_sample;
1088     int64_t bitrate;
1089
1090     if (!(framerate.num && framerate.den))
1091         framerate = av_inv_q(avctx->time_base);
1092     if (!(framerate.num && framerate.den))
1093         return 0;
1094
1095     if (!bits_per_coded_sample) {
1096         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1097         bits_per_coded_sample = av_get_bits_per_pixel(desc);
1098     }
1099     bitrate = (int64_t)bits_per_coded_sample * avctx->width * avctx->height *
1100               framerate.num / framerate.den;
1101
1102     return bitrate;
1103 }
1104
1105 int ff_int_from_list_or_default(void *ctx, const char * val_name, int val,
1106                                 const int * array_valid_values, int default_value)
1107 {
1108     int i = 0, ref_val;
1109
1110     while (1) {
1111         ref_val = array_valid_values[i];
1112         if (ref_val == INT_MAX)
1113             break;
1114         if (val == ref_val)
1115             return val;
1116         i++;
1117     }
1118     /* val is not a valid value */
1119     av_log(ctx, AV_LOG_DEBUG,
1120            "%s %d are not supported. Set to default value : %d\n", val_name, val, default_value);
1121     return default_value;
1122 }