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