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
av_fast_padded_malloc(void * ptr,unsigned int * size,size_t min_size)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
av_fast_padded_mallocz(void * ptr,unsigned int * size,size_t min_size)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
av_codec_is_encoder(const AVCodec * codec)74 int av_codec_is_encoder(const AVCodec *codec)
75 {
76 return codec && (codec->encode_sub || codec->encode2 || codec->receive_packet);
77 }
78
av_codec_is_decoder(const AVCodec * codec)79 int av_codec_is_decoder(const AVCodec *codec)
80 {
81 return codec && (codec->decode || codec->receive_frame);
82 }
83
ff_set_dimensions(AVCodecContext * s,int width,int height)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
ff_set_sar(AVCodecContext * avctx,AVRational sar)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
ff_side_data_update_matrix_encoding(AVFrame * frame,enum AVMatrixEncoding matrix_encoding)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
avcodec_align_dimensions2(AVCodecContext * s,int * width,int * height,int linesize_align[AV_NUM_DATA_POINTERS])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_ARGO ||
272 s->codec_id == AV_CODEC_ID_INTERPLAY_VIDEO) {
273 w_align = 8;
274 h_align = 8;
275 }
276 if (s->codec_id == AV_CODEC_ID_MJPEG ||
277 s->codec_id == AV_CODEC_ID_MJPEGB ||
278 s->codec_id == AV_CODEC_ID_LJPEG ||
279 s->codec_id == AV_CODEC_ID_SMVJPEG ||
280 s->codec_id == AV_CODEC_ID_AMV ||
281 s->codec_id == AV_CODEC_ID_SP5X ||
282 s->codec_id == AV_CODEC_ID_JPEGLS) {
283 w_align = 8;
284 h_align = 2*8;
285 }
286 break;
287 case AV_PIX_FMT_BGR24:
288 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
289 (s->codec_id == AV_CODEC_ID_ZLIB)) {
290 w_align = 4;
291 h_align = 4;
292 }
293 break;
294 case AV_PIX_FMT_RGB24:
295 if (s->codec_id == AV_CODEC_ID_CINEPAK) {
296 w_align = 4;
297 h_align = 4;
298 }
299 break;
300 case AV_PIX_FMT_BGR0:
301 if (s->codec_id == AV_CODEC_ID_ARGO) {
302 w_align = 8;
303 h_align = 8;
304 }
305 break;
306 default:
307 break;
308 }
309
310 if (s->codec_id == AV_CODEC_ID_IFF_ILBM) {
311 w_align = FFMAX(w_align, 8);
312 }
313
314 *width = FFALIGN(*width, w_align);
315 *height = FFALIGN(*height, h_align);
316 if (s->codec_id == AV_CODEC_ID_H264 || s->lowres ||
317 s->codec_id == AV_CODEC_ID_VP5 || s->codec_id == AV_CODEC_ID_VP6 ||
318 s->codec_id == AV_CODEC_ID_VP6F || s->codec_id == AV_CODEC_ID_VP6A
319 ) {
320 // some of the optimized chroma MC reads one line too much
321 // which is also done in mpeg decoders with lowres > 0
322 *height += 2;
323
324 // H.264 uses edge emulation for out of frame motion vectors, for this
325 // it requires a temporary area large enough to hold a 21x21 block,
326 // increasing witdth ensure that the temporary area is large enough,
327 // the next rounded up width is 32
328 *width = FFMAX(*width, 32);
329 }
330
331 for (i = 0; i < 4; i++)
332 linesize_align[i] = STRIDE_ALIGN;
333 }
334
avcodec_align_dimensions(AVCodecContext * s,int * width,int * height)335 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
336 {
337 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
338 int chroma_shift = desc->log2_chroma_w;
339 int linesize_align[AV_NUM_DATA_POINTERS];
340 int align;
341
342 avcodec_align_dimensions2(s, width, height, linesize_align);
343 align = FFMAX(linesize_align[0], linesize_align[3]);
344 linesize_align[1] <<= chroma_shift;
345 linesize_align[2] <<= chroma_shift;
346 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
347 *width = FFALIGN(*width, align);
348 }
349
avcodec_enum_to_chroma_pos(int * xpos,int * ypos,enum AVChromaLocation pos)350 int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
351 {
352 if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
353 return AVERROR(EINVAL);
354 pos--;
355
356 *xpos = (pos&1) * 128;
357 *ypos = ((pos>>1)^(pos<4)) * 128;
358
359 return 0;
360 }
361
avcodec_chroma_pos_to_enum(int xpos,int ypos)362 enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
363 {
364 int pos, xout, yout;
365
366 for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
367 if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
368 return pos;
369 }
370 return AVCHROMA_LOC_UNSPECIFIED;
371 }
372
avcodec_fill_audio_frame(AVFrame * frame,int nb_channels,enum AVSampleFormat sample_fmt,const uint8_t * buf,int buf_size,int align)373 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
374 enum AVSampleFormat sample_fmt, const uint8_t *buf,
375 int buf_size, int align)
376 {
377 int ch, planar, needed_size, ret = 0;
378
379 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
380 frame->nb_samples, sample_fmt,
381 align);
382 if (buf_size < needed_size)
383 return AVERROR(EINVAL);
384
385 planar = av_sample_fmt_is_planar(sample_fmt);
386 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
387 if (!(frame->extended_data = av_mallocz_array(nb_channels,
388 sizeof(*frame->extended_data))))
389 return AVERROR(ENOMEM);
390 } else {
391 frame->extended_data = frame->data;
392 }
393
394 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
395 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
396 sample_fmt, align)) < 0) {
397 if (frame->extended_data != frame->data)
398 av_freep(&frame->extended_data);
399 return ret;
400 }
401 if (frame->extended_data != frame->data) {
402 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
403 frame->data[ch] = frame->extended_data[ch];
404 }
405
406 return ret;
407 }
408
ff_color_frame(AVFrame * frame,const int c[4])409 void ff_color_frame(AVFrame *frame, const int c[4])
410 {
411 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
412 int p, y;
413
414 av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
415
416 for (p = 0; p<desc->nb_components; p++) {
417 uint8_t *dst = frame->data[p];
418 int is_chroma = p == 1 || p == 2;
419 int bytes = is_chroma ? AV_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
420 int height = is_chroma ? AV_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
421 if (desc->comp[0].depth >= 9) {
422 ((uint16_t*)dst)[0] = c[p];
423 av_memcpy_backptr(dst + 2, 2, bytes - 2);
424 dst += frame->linesize[p];
425 for (y = 1; y < height; y++) {
426 memcpy(dst, frame->data[p], 2*bytes);
427 dst += frame->linesize[p];
428 }
429 } else {
430 for (y = 0; y < height; y++) {
431 memset(dst, c[p], bytes);
432 dst += frame->linesize[p];
433 }
434 }
435 }
436 }
437
avpriv_find_pix_fmt(const PixelFormatTag * tags,unsigned int fourcc)438 enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
439 unsigned int fourcc)
440 {
441 while (tags->pix_fmt >= 0) {
442 if (tags->fourcc == fourcc)
443 return tags->pix_fmt;
444 tags++;
445 }
446 return AV_PIX_FMT_NONE;
447 }
448
449 #if FF_API_CODEC_GET_SET
MAKE_ACCESSORS(AVCodecContext,codec,AVRational,pkt_timebase)450 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
451 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
452 MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
453 MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
454 MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
455
456 unsigned av_codec_get_codec_properties(const AVCodecContext *codec)
457 {
458 return codec->properties;
459 }
460
av_codec_get_max_lowres(const AVCodec * codec)461 int av_codec_get_max_lowres(const AVCodec *codec)
462 {
463 return codec->max_lowres;
464 }
465 #endif
466
avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec * codec)467 int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec){
468 return !!(codec->caps_internal & FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM);
469 }
470
avcodec_get_name(enum AVCodecID id)471 const char *avcodec_get_name(enum AVCodecID id)
472 {
473 const AVCodecDescriptor *cd;
474 const AVCodec *codec;
475
476 if (id == AV_CODEC_ID_NONE)
477 return "none";
478 cd = avcodec_descriptor_get(id);
479 if (cd)
480 return cd->name;
481 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
482 codec = avcodec_find_decoder(id);
483 if (codec)
484 return codec->name;
485 codec = avcodec_find_encoder(id);
486 if (codec)
487 return codec->name;
488 return "unknown_codec";
489 }
490
491 #if FF_API_TAG_STRING
av_get_codec_tag_string(char * buf,size_t buf_size,unsigned int codec_tag)492 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
493 {
494 int i, len, ret = 0;
495
496 #define TAG_PRINT(x) \
497 (((x) >= '0' && (x) <= '9') || \
498 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
499 ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
500
501 for (i = 0; i < 4; i++) {
502 len = snprintf(buf, buf_size,
503 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
504 buf += len;
505 buf_size = buf_size > len ? buf_size - len : 0;
506 ret += len;
507 codec_tag >>= 8;
508 }
509 return ret;
510 }
511 #endif
512
av_get_profile_name(const AVCodec * codec,int profile)513 const char *av_get_profile_name(const AVCodec *codec, int profile)
514 {
515 const AVProfile *p;
516 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
517 return NULL;
518
519 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
520 if (p->profile == profile)
521 return p->name;
522
523 return NULL;
524 }
525
avcodec_profile_name(enum AVCodecID codec_id,int profile)526 const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
527 {
528 const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
529 const AVProfile *p;
530
531 if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
532 return NULL;
533
534 for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
535 if (p->profile == profile)
536 return p->name;
537
538 return NULL;
539 }
540
av_get_exact_bits_per_sample(enum AVCodecID codec_id)541 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
542 {
543 switch (codec_id) {
544 case AV_CODEC_ID_8SVX_EXP:
545 case AV_CODEC_ID_8SVX_FIB:
546 case AV_CODEC_ID_ADPCM_ARGO:
547 case AV_CODEC_ID_ADPCM_CT:
548 case AV_CODEC_ID_ADPCM_IMA_ALP:
549 case AV_CODEC_ID_ADPCM_IMA_AMV:
550 case AV_CODEC_ID_ADPCM_IMA_APC:
551 case AV_CODEC_ID_ADPCM_IMA_APM:
552 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
553 case AV_CODEC_ID_ADPCM_IMA_OKI:
554 case AV_CODEC_ID_ADPCM_IMA_WS:
555 case AV_CODEC_ID_ADPCM_IMA_SSI:
556 case AV_CODEC_ID_ADPCM_G722:
557 case AV_CODEC_ID_ADPCM_YAMAHA:
558 case AV_CODEC_ID_ADPCM_AICA:
559 return 4;
560 case AV_CODEC_ID_DSD_LSBF:
561 case AV_CODEC_ID_DSD_MSBF:
562 case AV_CODEC_ID_DSD_LSBF_PLANAR:
563 case AV_CODEC_ID_DSD_MSBF_PLANAR:
564 case AV_CODEC_ID_PCM_ALAW:
565 case AV_CODEC_ID_PCM_MULAW:
566 case AV_CODEC_ID_PCM_VIDC:
567 case AV_CODEC_ID_PCM_S8:
568 case AV_CODEC_ID_PCM_S8_PLANAR:
569 case AV_CODEC_ID_PCM_SGA:
570 case AV_CODEC_ID_PCM_U8:
571 case AV_CODEC_ID_SDX2_DPCM:
572 case AV_CODEC_ID_DERF_DPCM:
573 return 8;
574 case AV_CODEC_ID_PCM_S16BE:
575 case AV_CODEC_ID_PCM_S16BE_PLANAR:
576 case AV_CODEC_ID_PCM_S16LE:
577 case AV_CODEC_ID_PCM_S16LE_PLANAR:
578 case AV_CODEC_ID_PCM_U16BE:
579 case AV_CODEC_ID_PCM_U16LE:
580 return 16;
581 case AV_CODEC_ID_PCM_S24DAUD:
582 case AV_CODEC_ID_PCM_S24BE:
583 case AV_CODEC_ID_PCM_S24LE:
584 case AV_CODEC_ID_PCM_S24LE_PLANAR:
585 case AV_CODEC_ID_PCM_U24BE:
586 case AV_CODEC_ID_PCM_U24LE:
587 return 24;
588 case AV_CODEC_ID_PCM_S32BE:
589 case AV_CODEC_ID_PCM_S32LE:
590 case AV_CODEC_ID_PCM_S32LE_PLANAR:
591 case AV_CODEC_ID_PCM_U32BE:
592 case AV_CODEC_ID_PCM_U32LE:
593 case AV_CODEC_ID_PCM_F32BE:
594 case AV_CODEC_ID_PCM_F32LE:
595 case AV_CODEC_ID_PCM_F24LE:
596 case AV_CODEC_ID_PCM_F16LE:
597 return 32;
598 case AV_CODEC_ID_PCM_F64BE:
599 case AV_CODEC_ID_PCM_F64LE:
600 case AV_CODEC_ID_PCM_S64BE:
601 case AV_CODEC_ID_PCM_S64LE:
602 return 64;
603 default:
604 return 0;
605 }
606 }
607
av_get_pcm_codec(enum AVSampleFormat fmt,int be)608 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
609 {
610 static const enum AVCodecID map[][2] = {
611 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
612 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
613 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
614 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
615 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
616 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
617 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
618 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
619 [AV_SAMPLE_FMT_S64P] = { AV_CODEC_ID_PCM_S64LE, AV_CODEC_ID_PCM_S64BE },
620 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
621 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
622 };
623 if (fmt < 0 || fmt >= FF_ARRAY_ELEMS(map))
624 return AV_CODEC_ID_NONE;
625 if (be < 0 || be > 1)
626 be = AV_NE(1, 0);
627 return map[fmt][be];
628 }
629
av_get_bits_per_sample(enum AVCodecID codec_id)630 int av_get_bits_per_sample(enum AVCodecID codec_id)
631 {
632 switch (codec_id) {
633 case AV_CODEC_ID_ADPCM_SBPRO_2:
634 return 2;
635 case AV_CODEC_ID_ADPCM_SBPRO_3:
636 return 3;
637 case AV_CODEC_ID_ADPCM_SBPRO_4:
638 case AV_CODEC_ID_ADPCM_IMA_WAV:
639 case AV_CODEC_ID_ADPCM_IMA_QT:
640 case AV_CODEC_ID_ADPCM_SWF:
641 case AV_CODEC_ID_ADPCM_MS:
642 return 4;
643 default:
644 return av_get_exact_bits_per_sample(codec_id);
645 }
646 }
647
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)648 static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
649 uint32_t tag, int bits_per_coded_sample, int64_t bitrate,
650 uint8_t * extradata, int frame_size, int frame_bytes)
651 {
652 int bps = av_get_exact_bits_per_sample(id);
653 int framecount = (ba > 0 && frame_bytes / ba > 0) ? frame_bytes / ba : 1;
654
655 /* codecs with an exact constant bits per sample */
656 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
657 return (frame_bytes * 8LL) / (bps * ch);
658 bps = bits_per_coded_sample;
659
660 /* codecs with a fixed packet duration */
661 switch (id) {
662 case AV_CODEC_ID_ADPCM_ADX: return 32;
663 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
664 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
665 case AV_CODEC_ID_AMR_NB:
666 case AV_CODEC_ID_EVRC:
667 case AV_CODEC_ID_GSM:
668 case AV_CODEC_ID_QCELP:
669 case AV_CODEC_ID_RA_288: return 160;
670 case AV_CODEC_ID_AMR_WB:
671 case AV_CODEC_ID_GSM_MS: return 320;
672 case AV_CODEC_ID_MP1: return 384;
673 case AV_CODEC_ID_ATRAC1: return 512;
674 case AV_CODEC_ID_ATRAC9:
675 case AV_CODEC_ID_ATRAC3:
676 if (framecount > INT_MAX/1024)
677 return 0;
678 return 1024 * framecount;
679 case AV_CODEC_ID_ATRAC3P: return 2048;
680 case AV_CODEC_ID_MP2:
681 case AV_CODEC_ID_MUSEPACK7: return 1152;
682 case AV_CODEC_ID_AC3: return 1536;
683 case AV_CODEC_ID_AVS3DA: return 1024; // avs3p3/audio vivid fixed frame size
684 }
685
686 if (sr > 0) {
687 /* calc from sample rate */
688 if (id == AV_CODEC_ID_TTA)
689 return 256 * sr / 245;
690 else if (id == AV_CODEC_ID_DST)
691 return 588 * sr / 44100;
692 else if (id == AV_CODEC_ID_BINKAUDIO_DCT) {
693 if (sr / 22050 > 22)
694 return 0;
695 return (480 << (sr / 22050));
696 }
697
698 if (id == AV_CODEC_ID_MP3)
699 return sr <= 24000 ? 576 : 1152;
700 }
701
702 if (ba > 0) {
703 /* calc from block_align */
704 if (id == AV_CODEC_ID_SIPR) {
705 switch (ba) {
706 case 20: return 160;
707 case 19: return 144;
708 case 29: return 288;
709 case 37: return 480;
710 }
711 } else if (id == AV_CODEC_ID_ILBC) {
712 switch (ba) {
713 case 38: return 160;
714 case 50: return 240;
715 }
716 }
717 }
718
719 if (frame_bytes > 0) {
720 /* calc from frame_bytes only */
721 if (id == AV_CODEC_ID_TRUESPEECH)
722 return 240 * (frame_bytes / 32);
723 if (id == AV_CODEC_ID_NELLYMOSER)
724 return 256 * (frame_bytes / 64);
725 if (id == AV_CODEC_ID_RA_144)
726 return 160 * (frame_bytes / 20);
727
728 if (bps > 0) {
729 /* calc from frame_bytes and bits_per_coded_sample */
730 if (id == AV_CODEC_ID_ADPCM_G726 || id == AV_CODEC_ID_ADPCM_G726LE)
731 return frame_bytes * 8 / bps;
732 }
733
734 if (ch > 0 && ch < INT_MAX/16) {
735 /* calc from frame_bytes and channels */
736 switch (id) {
737 case AV_CODEC_ID_FASTAUDIO:
738 return frame_bytes / (40 * ch) * 256;
739 case AV_CODEC_ID_ADPCM_IMA_MOFLEX:
740 return (frame_bytes - 4 * ch) / (128 * ch) * 256;
741 case AV_CODEC_ID_ADPCM_AFC:
742 return frame_bytes / (9 * ch) * 16;
743 case AV_CODEC_ID_ADPCM_PSX:
744 case AV_CODEC_ID_ADPCM_DTK:
745 frame_bytes /= 16 * ch;
746 if (frame_bytes > INT_MAX / 28)
747 return 0;
748 return frame_bytes * 28;
749 case AV_CODEC_ID_ADPCM_4XM:
750 case AV_CODEC_ID_ADPCM_IMA_DAT4:
751 case AV_CODEC_ID_ADPCM_IMA_ISS:
752 return (frame_bytes - 4 * ch) * 2 / ch;
753 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
754 return (frame_bytes - 4) * 2 / ch;
755 case AV_CODEC_ID_ADPCM_IMA_AMV:
756 return (frame_bytes - 8) * 2;
757 case AV_CODEC_ID_ADPCM_THP:
758 case AV_CODEC_ID_ADPCM_THP_LE:
759 if (extradata)
760 return frame_bytes * 14LL / (8 * ch);
761 break;
762 case AV_CODEC_ID_ADPCM_XA:
763 return (frame_bytes / 128) * 224 / ch;
764 case AV_CODEC_ID_INTERPLAY_DPCM:
765 return (frame_bytes - 6 - ch) / ch;
766 case AV_CODEC_ID_ROQ_DPCM:
767 return (frame_bytes - 8) / ch;
768 case AV_CODEC_ID_XAN_DPCM:
769 return (frame_bytes - 2 * ch) / ch;
770 case AV_CODEC_ID_MACE3:
771 return 3 * frame_bytes / ch;
772 case AV_CODEC_ID_MACE6:
773 return 6 * frame_bytes / ch;
774 case AV_CODEC_ID_PCM_LXF:
775 return 2 * (frame_bytes / (5 * ch));
776 case AV_CODEC_ID_IAC:
777 case AV_CODEC_ID_IMC:
778 return 4 * frame_bytes / ch;
779 }
780
781 if (tag) {
782 /* calc from frame_bytes, channels, and codec_tag */
783 if (id == AV_CODEC_ID_SOL_DPCM) {
784 if (tag == 3)
785 return frame_bytes / ch;
786 else
787 return frame_bytes * 2 / ch;
788 }
789 }
790
791 if (ba > 0) {
792 /* calc from frame_bytes, channels, and block_align */
793 int blocks = frame_bytes / ba;
794 int64_t tmp = 0;
795 switch (id) {
796 case AV_CODEC_ID_ADPCM_IMA_WAV:
797 if (bps < 2 || bps > 5)
798 return 0;
799 tmp = blocks * (1LL + (ba - 4 * ch) / (bps * ch) * 8);
800 break;
801 case AV_CODEC_ID_ADPCM_IMA_DK3:
802 tmp = blocks * (((ba - 16LL) * 2 / 3 * 4) / ch);
803 break;
804 case AV_CODEC_ID_ADPCM_IMA_DK4:
805 tmp = blocks * (1 + (ba - 4LL * ch) * 2 / ch);
806 break;
807 case AV_CODEC_ID_ADPCM_IMA_RAD:
808 tmp = blocks * ((ba - 4LL * ch) * 2 / ch);
809 break;
810 case AV_CODEC_ID_ADPCM_MS:
811 tmp = blocks * (2 + (ba - 7LL * ch) * 2LL / ch);
812 break;
813 case AV_CODEC_ID_ADPCM_MTAF:
814 tmp = blocks * (ba - 16LL) * 2 / ch;
815 break;
816 }
817 if (tmp) {
818 if (tmp != (int)tmp)
819 return 0;
820 return tmp;
821 }
822 }
823
824 if (bps > 0) {
825 /* calc from frame_bytes, channels, and bits_per_coded_sample */
826 switch (id) {
827 case AV_CODEC_ID_PCM_DVD:
828 if(bps<4 || frame_bytes<3)
829 return 0;
830 return 2 * ((frame_bytes - 3) / ((bps * 2 / 8) * ch));
831 case AV_CODEC_ID_PCM_BLURAY:
832 if(bps<4 || frame_bytes<4)
833 return 0;
834 return (frame_bytes - 4) / ((FFALIGN(ch, 2) * bps) / 8);
835 case AV_CODEC_ID_S302M:
836 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
837 }
838 }
839 }
840 }
841
842 /* Fall back on using frame_size */
843 if (frame_size > 1 && frame_bytes)
844 return frame_size;
845
846 //For WMA we currently have no other means to calculate duration thus we
847 //do it here by assuming CBR, which is true for all known cases.
848 if (bitrate > 0 && frame_bytes > 0 && sr > 0 && ba > 1) {
849 if (id == AV_CODEC_ID_WMAV1 || id == AV_CODEC_ID_WMAV2)
850 return (frame_bytes * 8LL * sr) / bitrate;
851 }
852
853 return 0;
854 }
855
av_get_audio_frame_duration(AVCodecContext * avctx,int frame_bytes)856 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
857 {
858 int duration = get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
859 avctx->channels, avctx->block_align,
860 avctx->codec_tag, avctx->bits_per_coded_sample,
861 avctx->bit_rate, avctx->extradata, avctx->frame_size,
862 frame_bytes);
863 return FFMAX(0, duration);
864 }
865
av_get_audio_frame_duration2(AVCodecParameters * par,int frame_bytes)866 int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
867 {
868 int duration = get_audio_frame_duration(par->codec_id, par->sample_rate,
869 par->channels, par->block_align,
870 par->codec_tag, par->bits_per_coded_sample,
871 par->bit_rate, par->extradata, par->frame_size,
872 frame_bytes);
873 return FFMAX(0, duration);
874 }
875
876 #if !HAVE_THREADS
ff_thread_init(AVCodecContext * s)877 int ff_thread_init(AVCodecContext *s)
878 {
879 return -1;
880 }
881
882 #endif
883
av_xiphlacing(unsigned char * s,unsigned int v)884 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
885 {
886 unsigned int n = 0;
887
888 while (v >= 0xff) {
889 *s++ = 0xff;
890 v -= 0xff;
891 n++;
892 }
893 *s = v;
894 n++;
895 return n;
896 }
897
ff_match_2uint16(const uint16_t (* tab)[2],int size,int a,int b)898 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
899 {
900 int i;
901 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
902 return i;
903 }
904
avcodec_get_hw_config(const AVCodec * codec,int index)905 const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *codec, int index)
906 {
907 int i;
908 if (!codec->hw_configs || index < 0)
909 return NULL;
910 for (i = 0; i <= index; i++)
911 if (!codec->hw_configs[i])
912 return NULL;
913 return &codec->hw_configs[index]->public;
914 }
915
916 #if FF_API_USER_VISIBLE_AVHWACCEL
av_hwaccel_next(const AVHWAccel * hwaccel)917 AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
918 {
919 return NULL;
920 }
921
av_register_hwaccel(AVHWAccel * hwaccel)922 void av_register_hwaccel(AVHWAccel *hwaccel)
923 {
924 }
925 #endif
926
avpriv_toupper4(unsigned int x)927 unsigned int avpriv_toupper4(unsigned int x)
928 {
929 return av_toupper(x & 0xFF) +
930 (av_toupper((x >> 8) & 0xFF) << 8) +
931 (av_toupper((x >> 16) & 0xFF) << 16) +
932 ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
933 }
934
ff_thread_ref_frame(ThreadFrame * dst,const ThreadFrame * src)935 int ff_thread_ref_frame(ThreadFrame *dst, const ThreadFrame *src)
936 {
937 int ret;
938
939 dst->owner[0] = src->owner[0];
940 dst->owner[1] = src->owner[1];
941
942 ret = av_frame_ref(dst->f, src->f);
943 if (ret < 0)
944 return ret;
945
946 av_assert0(!dst->progress);
947
948 if (src->progress &&
949 !(dst->progress = av_buffer_ref(src->progress))) {
950 ff_thread_release_buffer(dst->owner[0], dst);
951 return AVERROR(ENOMEM);
952 }
953
954 return 0;
955 }
956
957 #if !HAVE_THREADS
958
ff_thread_get_format(AVCodecContext * avctx,const enum AVPixelFormat * fmt)959 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
960 {
961 return ff_get_format(avctx, fmt);
962 }
963
ff_thread_get_buffer(AVCodecContext * avctx,ThreadFrame * f,int flags)964 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
965 {
966 f->owner[0] = f->owner[1] = avctx;
967 return ff_get_buffer(avctx, f->f, flags);
968 }
969
ff_thread_release_buffer(AVCodecContext * avctx,ThreadFrame * f)970 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
971 {
972 if (f->f)
973 av_frame_unref(f->f);
974 }
975
ff_thread_finish_setup(AVCodecContext * avctx)976 void ff_thread_finish_setup(AVCodecContext *avctx)
977 {
978 }
979
ff_thread_report_progress(ThreadFrame * f,int progress,int field)980 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
981 {
982 }
983
ff_thread_await_progress(ThreadFrame * f,int progress,int field)984 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
985 {
986 }
987
ff_thread_can_start_frame(AVCodecContext * avctx)988 int ff_thread_can_start_frame(AVCodecContext *avctx)
989 {
990 return 1;
991 }
992
ff_alloc_entries(AVCodecContext * avctx,int count)993 int ff_alloc_entries(AVCodecContext *avctx, int count)
994 {
995 return 0;
996 }
997
ff_reset_entries(AVCodecContext * avctx)998 void ff_reset_entries(AVCodecContext *avctx)
999 {
1000 }
1001
ff_thread_await_progress2(AVCodecContext * avctx,int field,int thread,int shift)1002 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
1003 {
1004 }
1005
ff_thread_report_progress2(AVCodecContext * avctx,int field,int thread,int n)1006 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
1007 {
1008 }
1009
1010 #endif
1011
avpriv_find_start_code(const uint8_t * av_restrict p,const uint8_t * end,uint32_t * av_restrict state)1012 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
1013 const uint8_t *end,
1014 uint32_t *av_restrict state)
1015 {
1016 int i;
1017
1018 av_assert0(p <= end);
1019 if (p >= end)
1020 return end;
1021
1022 for (i = 0; i < 3; i++) {
1023 uint32_t tmp = *state << 8;
1024 *state = tmp + *(p++);
1025 if (tmp == 0x100 || p == end)
1026 return p;
1027 }
1028
1029 while (p < end) {
1030 if (p[-1] > 1 ) p += 3;
1031 else if (p[-2] ) p += 2;
1032 else if (p[-3]|(p[-1]-1)) p++;
1033 else {
1034 p++;
1035 break;
1036 }
1037 }
1038
1039 p = FFMIN(p, end) - 4;
1040 *state = AV_RB32(p);
1041
1042 return p + 4;
1043 }
1044
av_cpb_properties_alloc(size_t * size)1045 AVCPBProperties *av_cpb_properties_alloc(size_t *size)
1046 {
1047 AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
1048 if (!props)
1049 return NULL;
1050
1051 if (size)
1052 *size = sizeof(*props);
1053
1054 props->vbv_delay = UINT64_MAX;
1055
1056 return props;
1057 }
1058
ff_add_cpb_side_data(AVCodecContext * avctx)1059 AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
1060 {
1061 AVPacketSideData *tmp;
1062 AVCPBProperties *props;
1063 size_t size;
1064 int i;
1065
1066 for (i = 0; i < avctx->nb_coded_side_data; i++)
1067 if (avctx->coded_side_data[i].type == AV_PKT_DATA_CPB_PROPERTIES)
1068 return (AVCPBProperties *)avctx->coded_side_data[i].data;
1069
1070 props = av_cpb_properties_alloc(&size);
1071 if (!props)
1072 return NULL;
1073
1074 tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
1075 if (!tmp) {
1076 av_freep(&props);
1077 return NULL;
1078 }
1079
1080 avctx->coded_side_data = tmp;
1081 avctx->nb_coded_side_data++;
1082
1083 avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
1084 avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
1085 avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
1086
1087 return props;
1088 }
1089
bcd2uint(uint8_t bcd)1090 static unsigned bcd2uint(uint8_t bcd)
1091 {
1092 unsigned low = bcd & 0xf;
1093 unsigned high = bcd >> 4;
1094 if (low > 9 || high > 9)
1095 return 0;
1096 return low + 10*high;
1097 }
1098
ff_alloc_timecode_sei(const AVFrame * frame,AVRational rate,size_t prefix_len,void ** data,size_t * sei_size)1099 int ff_alloc_timecode_sei(const AVFrame *frame, AVRational rate, size_t prefix_len,
1100 void **data, size_t *sei_size)
1101 {
1102 AVFrameSideData *sd = NULL;
1103 uint8_t *sei_data;
1104 PutBitContext pb;
1105 uint32_t *tc;
1106 int m;
1107
1108 if (frame)
1109 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_S12M_TIMECODE);
1110
1111 if (!sd) {
1112 *data = NULL;
1113 return 0;
1114 }
1115 tc = (uint32_t*)sd->data;
1116 m = tc[0] & 3;
1117
1118 *sei_size = sizeof(uint32_t) * 4;
1119 *data = av_mallocz(*sei_size + prefix_len);
1120 if (!*data)
1121 return AVERROR(ENOMEM);
1122 sei_data = (uint8_t*)*data + prefix_len;
1123
1124 init_put_bits(&pb, sei_data, *sei_size);
1125 put_bits(&pb, 2, m); // num_clock_ts
1126
1127 for (int j = 1; j <= m; j++) {
1128 uint32_t tcsmpte = tc[j];
1129 unsigned hh = bcd2uint(tcsmpte & 0x3f); // 6-bit hours
1130 unsigned mm = bcd2uint(tcsmpte>>8 & 0x7f); // 7-bit minutes
1131 unsigned ss = bcd2uint(tcsmpte>>16 & 0x7f); // 7-bit seconds
1132 unsigned ff = bcd2uint(tcsmpte>>24 & 0x3f); // 6-bit frames
1133 unsigned drop = tcsmpte & 1<<30 && !0; // 1-bit drop if not arbitrary bit
1134
1135 /* Calculate frame number of HEVC by SMPTE ST 12-1:2014 Sec 12.2 if rate > 30FPS */
1136 if (av_cmp_q(rate, (AVRational) {30, 1}) == 1) {
1137 unsigned pc;
1138 ff *= 2;
1139 if (av_cmp_q(rate, (AVRational) {50, 1}) == 0)
1140 pc = !!(tcsmpte & 1 << 7);
1141 else
1142 pc = !!(tcsmpte & 1 << 23);
1143 ff = (ff + pc) & 0x7f;
1144 }
1145
1146 put_bits(&pb, 1, 1); // clock_timestamp_flag
1147 put_bits(&pb, 1, 1); // units_field_based_flag
1148 put_bits(&pb, 5, 0); // counting_type
1149 put_bits(&pb, 1, 1); // full_timestamp_flag
1150 put_bits(&pb, 1, 0); // discontinuity_flag
1151 put_bits(&pb, 1, drop);
1152 put_bits(&pb, 9, ff);
1153 put_bits(&pb, 6, ss);
1154 put_bits(&pb, 6, mm);
1155 put_bits(&pb, 5, hh);
1156 put_bits(&pb, 5, 0);
1157 }
1158 flush_put_bits(&pb);
1159
1160 return 0;
1161 }
1162
ff_guess_coded_bitrate(AVCodecContext * avctx)1163 int64_t ff_guess_coded_bitrate(AVCodecContext *avctx)
1164 {
1165 AVRational framerate = avctx->framerate;
1166 int bits_per_coded_sample = avctx->bits_per_coded_sample;
1167 int64_t bitrate;
1168
1169 if (!(framerate.num && framerate.den))
1170 framerate = av_inv_q(avctx->time_base);
1171 if (!(framerate.num && framerate.den))
1172 return 0;
1173
1174 if (!bits_per_coded_sample) {
1175 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
1176 bits_per_coded_sample = av_get_bits_per_pixel(desc);
1177 }
1178 bitrate = (int64_t)bits_per_coded_sample * avctx->width * avctx->height *
1179 framerate.num / framerate.den;
1180
1181 return bitrate;
1182 }
1183
ff_int_from_list_or_default(void * ctx,const char * val_name,int val,const int * array_valid_values,int default_value)1184 int ff_int_from_list_or_default(void *ctx, const char * val_name, int val,
1185 const int * array_valid_values, int default_value)
1186 {
1187 int i = 0, ref_val;
1188
1189 while (1) {
1190 ref_val = array_valid_values[i];
1191 if (ref_val == INT_MAX)
1192 break;
1193 if (val == ref_val)
1194 return val;
1195 i++;
1196 }
1197 /* val is not a valid value */
1198 av_log(ctx, AV_LOG_DEBUG,
1199 "%s %d are not supported. Set to default value : %d\n", val_name, val, default_value);
1200 return default_value;
1201 }
1202