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