aarch64: Add assembly support for -fsanitize=hwaddress tagged globals.
[libav.git] / libavcodec / utils.c
blob701ef5072b00d43c8e7df956b5bcc1a6ae296a90
1 /*
2 * utils for libavcodec
3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of Libav.
8 * Libav 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.
13 * Libav 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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 /**
24 * @file
25 * utils.
28 #include "config.h"
29 #include "libavutil/attributes.h"
30 #include "libavutil/avassert.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/crc.h"
34 #include "libavutil/frame.h"
35 #include "libavutil/hwcontext.h"
36 #include "libavutil/internal.h"
37 #include "libavutil/mathematics.h"
38 #include "libavutil/pixdesc.h"
39 #include "libavutil/imgutils.h"
40 #include "libavutil/samplefmt.h"
41 #include "libavutil/dict.h"
42 #include "avcodec.h"
43 #include "decode.h"
44 #include "hwaccel.h"
45 #include "libavutil/opt.h"
46 #include "me_cmp.h"
47 #include "mpegvideo.h"
48 #include "thread.h"
49 #include "internal.h"
50 #include "bytestream.h"
51 #include "version.h"
52 #include <stdlib.h>
53 #include <stdarg.h>
54 #include <limits.h>
55 #include <float.h>
57 static int volatile entangled_thread_counter = 0;
58 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op);
59 static void *codec_mutex;
60 static void *avformat_mutex;
62 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
64 void **p = ptr;
65 if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
66 av_freep(p);
67 *size = 0;
68 return;
70 av_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
71 if (*size)
72 memset((uint8_t *)*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
75 /* encoder management */
76 static AVCodec *first_avcodec = NULL;
78 AVCodec *av_codec_next(const AVCodec *c)
80 if (c)
81 return c->next;
82 else
83 return first_avcodec;
86 static av_cold void avcodec_init(void)
88 static int initialized = 0;
90 if (initialized != 0)
91 return;
92 initialized = 1;
94 if (CONFIG_ME_CMP)
95 ff_me_cmp_init_static();
98 int av_codec_is_encoder(const AVCodec *codec)
100 return codec && (codec->encode_sub || codec->encode2 ||codec->send_frame);
103 int av_codec_is_decoder(const AVCodec *codec)
105 return codec && (codec->decode || codec->receive_frame);
108 av_cold void avcodec_register(AVCodec *codec)
110 AVCodec **p;
111 avcodec_init();
112 p = &first_avcodec;
113 while (*p)
114 p = &(*p)->next;
115 *p = codec;
116 codec->next = NULL;
118 if (codec->init_static_data)
119 codec->init_static_data(codec);
122 int ff_set_dimensions(AVCodecContext *s, int width, int height)
124 int ret = av_image_check_size(width, height, 0, s);
126 if (ret < 0)
127 width = height = 0;
128 s->width = s->coded_width = width;
129 s->height = s->coded_height = height;
131 return ret;
134 int ff_set_sar(AVCodecContext *avctx, AVRational sar)
136 int ret = av_image_check_sar(avctx->width, avctx->height, sar);
138 if (ret < 0) {
139 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
140 sar.num, sar.den);
141 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
142 return ret;
143 } else {
144 avctx->sample_aspect_ratio = sar;
146 return 0;
149 int ff_side_data_update_matrix_encoding(AVFrame *frame,
150 enum AVMatrixEncoding matrix_encoding)
152 AVFrameSideData *side_data;
153 enum AVMatrixEncoding *data;
155 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
156 if (!side_data)
157 side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
158 sizeof(enum AVMatrixEncoding));
160 if (!side_data)
161 return AVERROR(ENOMEM);
163 data = (enum AVMatrixEncoding*)side_data->data;
164 *data = matrix_encoding;
166 return 0;
169 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
170 int linesize_align[AV_NUM_DATA_POINTERS])
172 size_t max_align = av_cpu_max_align();
173 int i;
174 int w_align = 1;
175 int h_align = 1;
177 switch (s->pix_fmt) {
178 case AV_PIX_FMT_YUV420P:
179 case AV_PIX_FMT_YUYV422:
180 case AV_PIX_FMT_YVYU422:
181 case AV_PIX_FMT_UYVY422:
182 case AV_PIX_FMT_YUV422P:
183 case AV_PIX_FMT_YUV440P:
184 case AV_PIX_FMT_YUV444P:
185 case AV_PIX_FMT_GBRP:
186 case AV_PIX_FMT_GBRAP:
187 case AV_PIX_FMT_GRAY8:
188 case AV_PIX_FMT_GRAY16BE:
189 case AV_PIX_FMT_GRAY16LE:
190 case AV_PIX_FMT_YUVJ420P:
191 case AV_PIX_FMT_YUVJ422P:
192 case AV_PIX_FMT_YUVJ440P:
193 case AV_PIX_FMT_YUVJ444P:
194 case AV_PIX_FMT_YUVA420P:
195 case AV_PIX_FMT_YUVA422P:
196 case AV_PIX_FMT_YUVA444P:
197 case AV_PIX_FMT_YUV420P9LE:
198 case AV_PIX_FMT_YUV420P9BE:
199 case AV_PIX_FMT_YUV420P10LE:
200 case AV_PIX_FMT_YUV420P10BE:
201 case AV_PIX_FMT_YUV422P9LE:
202 case AV_PIX_FMT_YUV422P9BE:
203 case AV_PIX_FMT_YUV422P10LE:
204 case AV_PIX_FMT_YUV422P10BE:
205 case AV_PIX_FMT_YUVA422P10LE:
206 case AV_PIX_FMT_YUVA422P10BE:
207 case AV_PIX_FMT_YUV444P9LE:
208 case AV_PIX_FMT_YUV444P9BE:
209 case AV_PIX_FMT_YUV444P10LE:
210 case AV_PIX_FMT_YUV444P10BE:
211 case AV_PIX_FMT_YUVA444P10LE:
212 case AV_PIX_FMT_YUVA444P10BE:
213 case AV_PIX_FMT_GBRP9LE:
214 case AV_PIX_FMT_GBRP9BE:
215 case AV_PIX_FMT_GBRP10LE:
216 case AV_PIX_FMT_GBRP10BE:
217 case AV_PIX_FMT_GBRAP12LE:
218 case AV_PIX_FMT_GBRAP12BE:
219 w_align = 16; //FIXME assume 16 pixel per macroblock
220 h_align = 16 * 2; // interlaced needs 2 macroblocks height
221 break;
222 case AV_PIX_FMT_YUV411P:
223 case AV_PIX_FMT_UYYVYY411:
224 w_align = 32;
225 h_align = 8;
226 break;
227 case AV_PIX_FMT_YUV410P:
228 if (s->codec_id == AV_CODEC_ID_SVQ1) {
229 w_align = 64;
230 h_align = 64;
232 case AV_PIX_FMT_RGB555:
233 if (s->codec_id == AV_CODEC_ID_RPZA) {
234 w_align = 4;
235 h_align = 4;
237 case AV_PIX_FMT_PAL8:
238 case AV_PIX_FMT_BGR8:
239 case AV_PIX_FMT_RGB8:
240 if (s->codec_id == AV_CODEC_ID_SMC) {
241 w_align = 4;
242 h_align = 4;
244 break;
245 case AV_PIX_FMT_BGR24:
246 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
247 (s->codec_id == AV_CODEC_ID_ZLIB)) {
248 w_align = 4;
249 h_align = 4;
251 break;
252 default:
253 w_align = 1;
254 h_align = 1;
255 break;
258 *width = FFALIGN(*width, w_align);
259 *height = FFALIGN(*height, h_align);
260 if (s->codec_id == AV_CODEC_ID_H264)
261 // some of the optimized chroma MC reads one line too much
262 *height += 2;
264 for (i = 0; i < 4; i++)
265 linesize_align[i] = max_align;
268 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
270 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
271 int chroma_shift = desc->log2_chroma_w;
272 int linesize_align[AV_NUM_DATA_POINTERS];
273 int align;
275 avcodec_align_dimensions2(s, width, height, linesize_align);
276 align = FFMAX(linesize_align[0], linesize_align[3]);
277 linesize_align[1] <<= chroma_shift;
278 linesize_align[2] <<= chroma_shift;
279 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
280 *width = FFALIGN(*width, align);
283 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
284 enum AVSampleFormat sample_fmt, const uint8_t *buf,
285 int buf_size, int align)
287 int ch, planar, needed_size, ret = 0;
289 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
290 frame->nb_samples, sample_fmt,
291 align);
292 if (buf_size < needed_size)
293 return AVERROR(EINVAL);
295 planar = av_sample_fmt_is_planar(sample_fmt);
296 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
297 if (!(frame->extended_data = av_mallocz(nb_channels *
298 sizeof(*frame->extended_data))))
299 return AVERROR(ENOMEM);
300 } else {
301 frame->extended_data = frame->data;
304 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
305 buf, nb_channels, frame->nb_samples,
306 sample_fmt, align)) < 0) {
307 if (frame->extended_data != frame->data)
308 av_free(frame->extended_data);
309 return ret;
311 if (frame->extended_data != frame->data) {
312 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
313 frame->data[ch] = frame->extended_data[ch];
316 return ret;
319 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
321 int i;
323 for (i = 0; i < count; i++) {
324 int r = func(c, (char *)arg + i * size);
325 if (ret)
326 ret[i] = r;
328 return 0;
331 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
333 int i;
335 for (i = 0; i < count; i++) {
336 int r = func(c, arg, i, 0);
337 if (ret)
338 ret[i] = r;
340 return 0;
343 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
345 int ret = 0;
346 AVDictionary *tmp = NULL;
348 if (avcodec_is_open(avctx))
349 return 0;
351 if ((!codec && !avctx->codec)) {
352 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
353 return AVERROR(EINVAL);
355 if ((codec && avctx->codec && codec != avctx->codec)) {
356 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
357 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
358 return AVERROR(EINVAL);
360 if (!codec)
361 codec = avctx->codec;
363 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
364 return AVERROR(EINVAL);
366 if (options)
367 av_dict_copy(&tmp, *options, 0);
369 /* If there is a user-supplied mutex locking routine, call it. */
370 if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) {
371 if (lockmgr_cb) {
372 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
373 return -1;
376 entangled_thread_counter++;
377 if (entangled_thread_counter != 1) {
378 av_log(avctx, AV_LOG_ERROR,
379 "Insufficient thread locking. At least %d threads are "
380 "calling avcodec_open2() at the same time right now.\n",
381 entangled_thread_counter);
382 ret = -1;
383 goto end;
387 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
388 if (!avctx->internal) {
389 ret = AVERROR(ENOMEM);
390 goto end;
393 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
394 if (!avctx->internal->pool) {
395 ret = AVERROR(ENOMEM);
396 goto free_and_end;
399 avctx->internal->to_free = av_frame_alloc();
400 if (!avctx->internal->to_free) {
401 ret = AVERROR(ENOMEM);
402 goto free_and_end;
405 avctx->internal->compat_decode_frame = av_frame_alloc();
406 if (!avctx->internal->compat_decode_frame) {
407 ret = AVERROR(ENOMEM);
408 goto free_and_end;
411 avctx->internal->buffer_frame = av_frame_alloc();
412 if (!avctx->internal->buffer_frame) {
413 ret = AVERROR(ENOMEM);
414 goto free_and_end;
417 avctx->internal->buffer_pkt = av_packet_alloc();
418 if (!avctx->internal->buffer_pkt) {
419 ret = AVERROR(ENOMEM);
420 goto free_and_end;
423 avctx->internal->ds.in_pkt = av_packet_alloc();
424 if (!avctx->internal->ds.in_pkt) {
425 ret = AVERROR(ENOMEM);
426 goto free_and_end;
429 avctx->internal->last_pkt_props = av_packet_alloc();
430 if (!avctx->internal->last_pkt_props) {
431 ret = AVERROR(ENOMEM);
432 goto free_and_end;
435 if (codec->priv_data_size > 0) {
436 if (!avctx->priv_data) {
437 avctx->priv_data = av_mallocz(codec->priv_data_size);
438 if (!avctx->priv_data) {
439 ret = AVERROR(ENOMEM);
440 goto end;
442 if (codec->priv_class) {
443 *(const AVClass **)avctx->priv_data = codec->priv_class;
444 av_opt_set_defaults(avctx->priv_data);
447 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
448 goto free_and_end;
449 } else {
450 avctx->priv_data = NULL;
452 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
453 goto free_and_end;
455 if (avctx->coded_width && avctx->coded_height && !avctx->width && !avctx->height)
456 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
457 else if (avctx->width && avctx->height)
458 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
459 if (ret < 0)
460 goto free_and_end;
462 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
463 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
464 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
465 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
466 ff_set_dimensions(avctx, 0, 0);
469 if (avctx->width > 0 && avctx->height > 0) {
470 if (av_image_check_sar(avctx->width, avctx->height,
471 avctx->sample_aspect_ratio) < 0) {
472 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
473 avctx->sample_aspect_ratio.num,
474 avctx->sample_aspect_ratio.den);
475 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
479 /* if the decoder init function was already called previously,
480 * free the already allocated subtitle_header before overwriting it */
481 if (av_codec_is_decoder(codec))
482 av_freep(&avctx->subtitle_header);
484 if (avctx->channels > FF_SANE_NB_CHANNELS) {
485 ret = AVERROR(EINVAL);
486 goto free_and_end;
489 avctx->codec = codec;
490 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
491 avctx->codec_id == AV_CODEC_ID_NONE) {
492 avctx->codec_type = codec->type;
493 avctx->codec_id = codec->id;
495 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
496 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
497 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
498 ret = AVERROR(EINVAL);
499 goto free_and_end;
501 avctx->frame_number = 0;
503 if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
504 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
505 ret = AVERROR_EXPERIMENTAL;
506 goto free_and_end;
509 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
510 (!avctx->time_base.num || !avctx->time_base.den)) {
511 avctx->time_base.num = 1;
512 avctx->time_base.den = avctx->sample_rate;
515 if (av_codec_is_decoder(avctx->codec)) {
516 ret = ff_decode_bsfs_init(avctx);
517 if (ret < 0)
518 goto free_and_end;
521 if (HAVE_THREADS) {
522 ret = ff_thread_init(avctx);
523 if (ret < 0) {
524 goto free_and_end;
527 if (!HAVE_THREADS && !(codec->capabilities & AV_CODEC_CAP_AUTO_THREADS))
528 avctx->thread_count = 1;
530 if (av_codec_is_encoder(avctx->codec)) {
531 int i;
532 #if FF_API_CODED_FRAME
533 FF_DISABLE_DEPRECATION_WARNINGS
534 avctx->coded_frame = av_frame_alloc();
535 if (!avctx->coded_frame) {
536 ret = AVERROR(ENOMEM);
537 goto free_and_end;
539 FF_ENABLE_DEPRECATION_WARNINGS
540 #endif
542 if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
543 av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
544 ret = AVERROR(EINVAL);
545 goto free_and_end;
548 if (avctx->codec->sample_fmts) {
549 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
550 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
551 break;
552 if (avctx->channels == 1 &&
553 av_get_planar_sample_fmt(avctx->sample_fmt) ==
554 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
555 avctx->sample_fmt = avctx->codec->sample_fmts[i];
556 break;
559 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
560 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
561 ret = AVERROR(EINVAL);
562 goto free_and_end;
565 if (avctx->codec->pix_fmts) {
566 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
567 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
568 break;
569 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
570 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
571 ret = AVERROR(EINVAL);
572 goto free_and_end;
574 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
575 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
576 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
577 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
578 avctx->color_range = AVCOL_RANGE_JPEG;
580 if (avctx->codec->supported_samplerates) {
581 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
582 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
583 break;
584 if (avctx->codec->supported_samplerates[i] == 0) {
585 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
586 ret = AVERROR(EINVAL);
587 goto free_and_end;
590 if (avctx->codec->channel_layouts) {
591 if (!avctx->channel_layout) {
592 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
593 } else {
594 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
595 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
596 break;
597 if (avctx->codec->channel_layouts[i] == 0) {
598 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
599 ret = AVERROR(EINVAL);
600 goto free_and_end;
604 if (avctx->channel_layout && avctx->channels) {
605 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
606 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
607 ret = AVERROR(EINVAL);
608 goto free_and_end;
610 } else if (avctx->channel_layout) {
611 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
614 if (!avctx->rc_initial_buffer_occupancy)
615 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
617 if (avctx->ticks_per_frame &&
618 avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
619 av_log(avctx, AV_LOG_ERROR,
620 "ticks_per_frame %d too large for the timebase %d/%d.",
621 avctx->ticks_per_frame,
622 avctx->time_base.num,
623 avctx->time_base.den);
624 goto free_and_end;
627 if (avctx->hw_frames_ctx) {
628 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
629 if (frames_ctx->format != avctx->pix_fmt) {
630 av_log(avctx, AV_LOG_ERROR,
631 "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
632 ret = AVERROR(EINVAL);
633 goto free_and_end;
635 if (avctx->sw_pix_fmt != AV_PIX_FMT_NONE &&
636 avctx->sw_pix_fmt != frames_ctx->sw_format) {
637 av_log(avctx, AV_LOG_ERROR,
638 "Mismatching AVCodecContext.sw_pix_fmt (%s) "
639 "and AVHWFramesContext.sw_format (%s)\n",
640 av_get_pix_fmt_name(avctx->sw_pix_fmt),
641 av_get_pix_fmt_name(frames_ctx->sw_format));
642 ret = AVERROR(EINVAL);
643 goto free_and_end;
645 avctx->sw_pix_fmt = frames_ctx->sw_format;
649 if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
650 ret = avctx->codec->init(avctx);
651 if (ret < 0) {
652 goto free_and_end;
656 if (av_codec_is_decoder(avctx->codec)) {
657 /* validate channel layout from the decoder */
658 if (avctx->channel_layout) {
659 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
660 if (!avctx->channels)
661 avctx->channels = channels;
662 else if (channels != avctx->channels) {
663 av_log(avctx, AV_LOG_WARNING,
664 "channel layout does not match number of channels\n");
665 avctx->channel_layout = 0;
668 if (avctx->channels && avctx->channels < 0 ||
669 avctx->channels > FF_SANE_NB_CHANNELS) {
670 ret = AVERROR(EINVAL);
671 goto free_and_end;
674 end:
675 if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) {
676 entangled_thread_counter--;
678 /* Release any user-supplied mutex. */
679 if (lockmgr_cb) {
680 (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
684 if (options) {
685 av_dict_free(options);
686 *options = tmp;
689 return ret;
690 free_and_end:
691 if (avctx->codec &&
692 (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
693 avctx->codec->close(avctx);
695 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
696 av_opt_free(avctx->priv_data);
697 av_opt_free(avctx);
699 #if FF_API_CODED_FRAME
700 FF_DISABLE_DEPRECATION_WARNINGS
701 av_frame_free(&avctx->coded_frame);
702 FF_ENABLE_DEPRECATION_WARNINGS
703 #endif
705 av_dict_free(&tmp);
706 av_freep(&avctx->priv_data);
707 if (avctx->internal) {
708 av_frame_free(&avctx->internal->to_free);
709 av_frame_free(&avctx->internal->compat_decode_frame);
710 av_frame_free(&avctx->internal->buffer_frame);
711 av_packet_free(&avctx->internal->buffer_pkt);
712 av_packet_free(&avctx->internal->last_pkt_props);
714 av_packet_free(&avctx->internal->ds.in_pkt);
715 ff_decode_bsfs_uninit(avctx);
717 av_freep(&avctx->internal->pool);
719 av_freep(&avctx->internal);
720 avctx->codec = NULL;
721 goto end;
724 void avsubtitle_free(AVSubtitle *sub)
726 int i;
728 for (i = 0; i < sub->num_rects; i++) {
729 av_freep(&sub->rects[i]->data[0]);
730 av_freep(&sub->rects[i]->data[1]);
731 av_freep(&sub->rects[i]->data[2]);
732 av_freep(&sub->rects[i]->data[3]);
733 av_freep(&sub->rects[i]->text);
734 av_freep(&sub->rects[i]->ass);
735 av_freep(&sub->rects[i]);
738 av_freep(&sub->rects);
740 memset(sub, 0, sizeof(AVSubtitle));
743 av_cold int avcodec_close(AVCodecContext *avctx)
745 int i;
747 if (avcodec_is_open(avctx)) {
748 FramePool *pool = avctx->internal->pool;
750 if (HAVE_THREADS && avctx->internal->thread_ctx)
751 ff_thread_free(avctx);
752 if (avctx->codec && avctx->codec->close)
753 avctx->codec->close(avctx);
754 av_frame_free(&avctx->internal->to_free);
755 av_frame_free(&avctx->internal->compat_decode_frame);
756 av_frame_free(&avctx->internal->buffer_frame);
757 av_packet_free(&avctx->internal->buffer_pkt);
758 av_packet_free(&avctx->internal->last_pkt_props);
760 av_packet_free(&avctx->internal->ds.in_pkt);
762 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
763 av_buffer_pool_uninit(&pool->pools[i]);
764 av_freep(&avctx->internal->pool);
766 if (avctx->hwaccel && avctx->hwaccel->uninit)
767 avctx->hwaccel->uninit(avctx);
768 av_freep(&avctx->internal->hwaccel_priv_data);
770 ff_decode_bsfs_uninit(avctx);
772 av_freep(&avctx->internal);
775 for (i = 0; i < avctx->nb_coded_side_data; i++)
776 av_freep(&avctx->coded_side_data[i].data);
777 av_freep(&avctx->coded_side_data);
778 avctx->nb_coded_side_data = 0;
780 av_buffer_unref(&avctx->hw_frames_ctx);
781 av_buffer_unref(&avctx->hw_device_ctx);
783 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
784 av_opt_free(avctx->priv_data);
785 av_opt_free(avctx);
786 av_freep(&avctx->priv_data);
787 if (av_codec_is_encoder(avctx->codec)) {
788 av_freep(&avctx->extradata);
789 #if FF_API_CODED_FRAME
790 FF_DISABLE_DEPRECATION_WARNINGS
791 av_frame_free(&avctx->coded_frame);
792 FF_ENABLE_DEPRECATION_WARNINGS
793 #endif
795 avctx->codec = NULL;
796 avctx->active_thread_type = 0;
798 return 0;
801 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
803 AVCodec *p, *experimental = NULL;
804 p = first_avcodec;
805 while (p) {
806 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
807 p->id == id) {
808 if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
809 experimental = p;
810 } else
811 return p;
813 p = p->next;
815 return experimental;
818 AVCodec *avcodec_find_encoder(enum AVCodecID id)
820 return find_encdec(id, 1);
823 AVCodec *avcodec_find_encoder_by_name(const char *name)
825 AVCodec *p;
826 if (!name)
827 return NULL;
828 p = first_avcodec;
829 while (p) {
830 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
831 return p;
832 p = p->next;
834 return NULL;
837 AVCodec *avcodec_find_decoder(enum AVCodecID id)
839 return find_encdec(id, 0);
842 AVCodec *avcodec_find_decoder_by_name(const char *name)
844 AVCodec *p;
845 if (!name)
846 return NULL;
847 p = first_avcodec;
848 while (p) {
849 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
850 return p;
851 p = p->next;
853 return NULL;
856 static int get_bit_rate(AVCodecContext *ctx)
858 int bit_rate;
859 int bits_per_sample;
861 switch (ctx->codec_type) {
862 case AVMEDIA_TYPE_VIDEO:
863 case AVMEDIA_TYPE_DATA:
864 case AVMEDIA_TYPE_SUBTITLE:
865 case AVMEDIA_TYPE_ATTACHMENT:
866 bit_rate = ctx->bit_rate;
867 break;
868 case AVMEDIA_TYPE_AUDIO:
869 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
870 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
871 break;
872 default:
873 bit_rate = 0;
874 break;
876 return bit_rate;
879 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
881 int i, len, ret = 0;
883 #define TAG_PRINT(x) \
884 (((x) >= '0' && (x) <= '9') || \
885 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
886 ((x) == '.' || (x) == ' '))
888 for (i = 0; i < 4; i++) {
889 len = snprintf(buf, buf_size,
890 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
891 buf += len;
892 buf_size = buf_size > len ? buf_size - len : 0;
893 ret += len;
894 codec_tag >>= 8;
896 return ret;
899 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
901 const char *codec_name;
902 const char *profile = NULL;
903 char buf1[32];
904 int bitrate;
905 int new_line = 0;
906 AVRational display_aspect_ratio;
907 const AVCodecDescriptor *desc = avcodec_descriptor_get(enc->codec_id);
909 if (desc) {
910 codec_name = desc->name;
911 profile = avcodec_profile_name(enc->codec_id, enc->profile);
912 } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
913 /* fake mpeg2 transport stream codec (currently not
914 * registered) */
915 codec_name = "mpeg2ts";
916 } else {
917 /* output avi tags */
918 char tag_buf[32];
919 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
920 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
921 codec_name = buf1;
924 switch (enc->codec_type) {
925 case AVMEDIA_TYPE_VIDEO:
926 snprintf(buf, buf_size,
927 "Video: %s%s",
928 codec_name, enc->mb_decision ? " (hq)" : "");
929 if (profile)
930 snprintf(buf + strlen(buf), buf_size - strlen(buf),
931 " (%s)", profile);
932 if (enc->codec_tag) {
933 char tag_buf[32];
934 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
935 snprintf(buf + strlen(buf), buf_size - strlen(buf),
936 " [%s / 0x%04X]", tag_buf, enc->codec_tag);
939 av_strlcat(buf, "\n ", buf_size);
940 snprintf(buf + strlen(buf), buf_size - strlen(buf),
941 "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
942 av_get_pix_fmt_name(enc->pix_fmt));
944 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
945 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
946 av_color_range_name(enc->color_range));
947 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
948 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
949 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
950 new_line = 1;
951 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s/%s/%s",
952 av_color_space_name(enc->colorspace),
953 av_color_primaries_name(enc->color_primaries),
954 av_color_transfer_name(enc->color_trc));
956 if (av_log_get_level() >= AV_LOG_DEBUG &&
957 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
958 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
959 av_chroma_location_name(enc->chroma_sample_location));
961 if (enc->width) {
962 av_strlcat(buf, new_line ? "\n " : ", ", buf_size);
964 snprintf(buf + strlen(buf), buf_size - strlen(buf),
965 "%dx%d",
966 enc->width, enc->height);
968 if (av_log_get_level() >= AV_LOG_VERBOSE &&
969 (enc->width != enc->coded_width ||
970 enc->height != enc->coded_height))
971 snprintf(buf + strlen(buf), buf_size - strlen(buf),
972 " (%dx%d)", enc->coded_width, enc->coded_height);
974 if (enc->sample_aspect_ratio.num) {
975 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
976 enc->width * enc->sample_aspect_ratio.num,
977 enc->height * enc->sample_aspect_ratio.den,
978 1024 * 1024);
979 snprintf(buf + strlen(buf), buf_size - strlen(buf),
980 " [PAR %d:%d DAR %d:%d]",
981 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
982 display_aspect_ratio.num, display_aspect_ratio.den);
984 if (av_log_get_level() >= AV_LOG_DEBUG) {
985 int g = av_gcd(enc->time_base.num, enc->time_base.den);
986 snprintf(buf + strlen(buf), buf_size - strlen(buf),
987 ", %d/%d",
988 enc->time_base.num / g, enc->time_base.den / g);
991 if (encode) {
992 snprintf(buf + strlen(buf), buf_size - strlen(buf),
993 ", q=%d-%d", enc->qmin, enc->qmax);
995 break;
996 case AVMEDIA_TYPE_AUDIO:
997 snprintf(buf, buf_size,
998 "Audio: %s",
999 codec_name);
1000 if (profile)
1001 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1002 " (%s)", profile);
1003 if (enc->codec_tag) {
1004 char tag_buf[32];
1005 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1006 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1007 " [%s / 0x%04X]", tag_buf, enc->codec_tag);
1010 av_strlcat(buf, "\n ", buf_size);
1011 if (enc->sample_rate) {
1012 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1013 "%d Hz, ", enc->sample_rate);
1015 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1016 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1017 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1018 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1020 break;
1021 case AVMEDIA_TYPE_DATA:
1022 snprintf(buf, buf_size, "Data: %s", codec_name);
1023 break;
1024 case AVMEDIA_TYPE_SUBTITLE:
1025 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1026 break;
1027 case AVMEDIA_TYPE_ATTACHMENT:
1028 snprintf(buf, buf_size, "Attachment: %s", codec_name);
1029 break;
1030 default:
1031 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1032 return;
1034 if (encode) {
1035 if (enc->flags & AV_CODEC_FLAG_PASS1)
1036 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1037 ", pass 1");
1038 if (enc->flags & AV_CODEC_FLAG_PASS2)
1039 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1040 ", pass 2");
1042 bitrate = get_bit_rate(enc);
1043 if (bitrate != 0) {
1044 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1045 ", %d kb/s", bitrate / 1000);
1049 const char *av_get_profile_name(const AVCodec *codec, int profile)
1051 const AVProfile *p;
1052 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1053 return NULL;
1055 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1056 if (p->profile == profile)
1057 return p->name;
1059 return NULL;
1062 const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
1064 const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
1065 const AVProfile *p;
1067 if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
1068 return NULL;
1070 for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1071 if (p->profile == profile)
1072 return p->name;
1074 return NULL;
1077 unsigned avcodec_version(void)
1079 return LIBAVCODEC_VERSION_INT;
1082 const char *avcodec_configuration(void)
1084 return LIBAV_CONFIGURATION;
1087 const char *avcodec_license(void)
1089 #define LICENSE_PREFIX "libavcodec license: "
1090 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1093 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
1095 switch (codec_id) {
1096 case AV_CODEC_ID_ADPCM_CT:
1097 case AV_CODEC_ID_ADPCM_IMA_APC:
1098 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1099 case AV_CODEC_ID_ADPCM_IMA_WS:
1100 case AV_CODEC_ID_ADPCM_G722:
1101 case AV_CODEC_ID_ADPCM_YAMAHA:
1102 return 4;
1103 case AV_CODEC_ID_PCM_ALAW:
1104 case AV_CODEC_ID_PCM_MULAW:
1105 case AV_CODEC_ID_PCM_S8:
1106 case AV_CODEC_ID_PCM_U8:
1107 case AV_CODEC_ID_PCM_ZORK:
1108 return 8;
1109 case AV_CODEC_ID_PCM_S16BE:
1110 case AV_CODEC_ID_PCM_S16BE_PLANAR:
1111 case AV_CODEC_ID_PCM_S16LE:
1112 case AV_CODEC_ID_PCM_S16LE_PLANAR:
1113 case AV_CODEC_ID_PCM_U16BE:
1114 case AV_CODEC_ID_PCM_U16LE:
1115 return 16;
1116 case AV_CODEC_ID_PCM_S24DAUD:
1117 case AV_CODEC_ID_PCM_S24BE:
1118 case AV_CODEC_ID_PCM_S24LE:
1119 case AV_CODEC_ID_PCM_S24LE_PLANAR:
1120 case AV_CODEC_ID_PCM_U24BE:
1121 case AV_CODEC_ID_PCM_U24LE:
1122 return 24;
1123 case AV_CODEC_ID_PCM_S32BE:
1124 case AV_CODEC_ID_PCM_S32LE:
1125 case AV_CODEC_ID_PCM_S32LE_PLANAR:
1126 case AV_CODEC_ID_PCM_U32BE:
1127 case AV_CODEC_ID_PCM_U32LE:
1128 case AV_CODEC_ID_PCM_F32BE:
1129 case AV_CODEC_ID_PCM_F32LE:
1130 return 32;
1131 case AV_CODEC_ID_PCM_F64BE:
1132 case AV_CODEC_ID_PCM_F64LE:
1133 return 64;
1134 default:
1135 return 0;
1139 int av_get_bits_per_sample(enum AVCodecID codec_id)
1141 switch (codec_id) {
1142 case AV_CODEC_ID_ADPCM_SBPRO_2:
1143 return 2;
1144 case AV_CODEC_ID_ADPCM_SBPRO_3:
1145 return 3;
1146 case AV_CODEC_ID_ADPCM_SBPRO_4:
1147 case AV_CODEC_ID_ADPCM_IMA_WAV:
1148 case AV_CODEC_ID_ADPCM_IMA_QT:
1149 case AV_CODEC_ID_ADPCM_SWF:
1150 case AV_CODEC_ID_ADPCM_MS:
1151 return 4;
1152 default:
1153 return av_get_exact_bits_per_sample(codec_id);
1157 static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
1158 uint32_t tag, int bits_per_coded_sample, int frame_bytes)
1160 int bps = av_get_exact_bits_per_sample(id);
1162 /* codecs with an exact constant bits per sample */
1163 if (bps > 0 && ch > 0 && frame_bytes > 0)
1164 return (frame_bytes * 8) / (bps * ch);
1165 bps = bits_per_coded_sample;
1167 /* codecs with a fixed packet duration */
1168 switch (id) {
1169 case AV_CODEC_ID_ADPCM_ADX: return 32;
1170 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
1171 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
1172 case AV_CODEC_ID_AMR_NB:
1173 case AV_CODEC_ID_GSM:
1174 case AV_CODEC_ID_QCELP:
1175 case AV_CODEC_ID_RA_144:
1176 case AV_CODEC_ID_RA_288: return 160;
1177 case AV_CODEC_ID_IMC: return 256;
1178 case AV_CODEC_ID_AMR_WB:
1179 case AV_CODEC_ID_GSM_MS: return 320;
1180 case AV_CODEC_ID_MP1: return 384;
1181 case AV_CODEC_ID_ATRAC1: return 512;
1182 case AV_CODEC_ID_ATRAC3: return 1024;
1183 case AV_CODEC_ID_MP2:
1184 case AV_CODEC_ID_MUSEPACK7: return 1152;
1185 case AV_CODEC_ID_AC3: return 1536;
1188 if (sr > 0) {
1189 /* calc from sample rate */
1190 if (id == AV_CODEC_ID_TTA)
1191 return 256 * sr / 245;
1193 if (ch > 0) {
1194 /* calc from sample rate and channels */
1195 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
1196 return (480 << (sr / 22050)) / ch;
1199 if (id == AV_CODEC_ID_MP3)
1200 return sr <= 24000 ? 576 : 1152;
1203 if (ba > 0) {
1204 /* calc from block_align */
1205 if (id == AV_CODEC_ID_SIPR) {
1206 switch (ba) {
1207 case 20: return 160;
1208 case 19: return 144;
1209 case 29: return 288;
1210 case 37: return 480;
1212 } else if (id == AV_CODEC_ID_ILBC) {
1213 switch (ba) {
1214 case 38: return 160;
1215 case 50: return 240;
1220 if (frame_bytes > 0) {
1221 /* calc from frame_bytes only */
1222 if (id == AV_CODEC_ID_TRUESPEECH)
1223 return 240 * (frame_bytes / 32);
1224 if (id == AV_CODEC_ID_NELLYMOSER)
1225 return 256 * (frame_bytes / 64);
1227 if (bps > 0) {
1228 /* calc from frame_bytes and bits_per_coded_sample */
1229 if (id == AV_CODEC_ID_ADPCM_G726)
1230 return frame_bytes * 8 / bps;
1233 if (ch > 0) {
1234 /* calc from frame_bytes and channels */
1235 switch (id) {
1236 case AV_CODEC_ID_ADPCM_4XM:
1237 case AV_CODEC_ID_ADPCM_IMA_ISS:
1238 return (frame_bytes - 4 * ch) * 2 / ch;
1239 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
1240 return (frame_bytes - 4) * 2 / ch;
1241 case AV_CODEC_ID_ADPCM_IMA_AMV:
1242 return (frame_bytes - 8) * 2 / ch;
1243 case AV_CODEC_ID_ADPCM_XA:
1244 return (frame_bytes / 128) * 224 / ch;
1245 case AV_CODEC_ID_INTERPLAY_DPCM:
1246 return (frame_bytes - 6 - ch) / ch;
1247 case AV_CODEC_ID_ROQ_DPCM:
1248 return (frame_bytes - 8) / ch;
1249 case AV_CODEC_ID_XAN_DPCM:
1250 return (frame_bytes - 2 * ch) / ch;
1251 case AV_CODEC_ID_MACE3:
1252 return 3 * frame_bytes / ch;
1253 case AV_CODEC_ID_MACE6:
1254 return 6 * frame_bytes / ch;
1255 case AV_CODEC_ID_PCM_LXF:
1256 return 2 * (frame_bytes / (5 * ch));
1259 if (tag) {
1260 /* calc from frame_bytes, channels, and codec_tag */
1261 if (id == AV_CODEC_ID_SOL_DPCM) {
1262 if (tag == 3)
1263 return frame_bytes / ch;
1264 else
1265 return frame_bytes * 2 / ch;
1269 if (ba > 0) {
1270 /* calc from frame_bytes, channels, and block_align */
1271 int blocks = frame_bytes / ba;
1272 switch (id) {
1273 case AV_CODEC_ID_ADPCM_IMA_WAV:
1274 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
1275 case AV_CODEC_ID_ADPCM_IMA_DK3:
1276 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
1277 case AV_CODEC_ID_ADPCM_IMA_DK4:
1278 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
1279 case AV_CODEC_ID_ADPCM_MS:
1280 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
1284 if (bps > 0) {
1285 /* calc from frame_bytes, channels, and bits_per_coded_sample */
1286 switch (id) {
1287 case AV_CODEC_ID_PCM_DVD:
1288 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
1289 case AV_CODEC_ID_PCM_BLURAY:
1290 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
1291 case AV_CODEC_ID_S302M:
1292 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
1298 return 0;
1301 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
1303 return get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
1304 avctx->channels, avctx->block_align,
1305 avctx->codec_tag, avctx->bits_per_coded_sample,
1306 frame_bytes);
1309 int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
1311 return get_audio_frame_duration(par->codec_id, par->sample_rate,
1312 par->channels, par->block_align,
1313 par->codec_tag, par->bits_per_coded_sample,
1314 frame_bytes);
1317 #if !HAVE_THREADS
1318 int ff_thread_init(AVCodecContext *s)
1320 return -1;
1323 #endif
1325 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1327 unsigned int n = 0;
1329 while (v >= 0xff) {
1330 *s++ = 0xff;
1331 v -= 0xff;
1332 n++;
1334 *s = v;
1335 n++;
1336 return n;
1339 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
1341 int i;
1342 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
1343 return i;
1346 const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *codec, int index)
1348 int i;
1349 if (!codec->hw_configs || index < 0)
1350 return NULL;
1351 for (i = 0; i <= index; i++)
1352 if (!codec->hw_configs[i])
1353 return NULL;
1354 return &codec->hw_configs[index]->public;
1357 #if FF_API_USER_VISIBLE_AVHWACCEL
1358 AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
1360 return NULL;
1363 void av_register_hwaccel(AVHWAccel *hwaccel)
1366 #endif
1368 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1370 if (lockmgr_cb) {
1371 // There is no good way to rollback a failure to destroy the
1372 // mutex, so we ignore failures.
1373 lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
1374 lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
1375 lockmgr_cb = NULL;
1376 codec_mutex = NULL;
1377 avformat_mutex = NULL;
1380 if (cb) {
1381 void *new_codec_mutex = NULL;
1382 void *new_avformat_mutex = NULL;
1383 int err;
1384 if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
1385 return err > 0 ? AVERROR_UNKNOWN : err;
1387 if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
1388 // Ignore failures to destroy the newly created mutex.
1389 cb(&new_codec_mutex, AV_LOCK_DESTROY);
1390 return err > 0 ? AVERROR_UNKNOWN : err;
1392 lockmgr_cb = cb;
1393 codec_mutex = new_codec_mutex;
1394 avformat_mutex = new_avformat_mutex;
1397 return 0;
1400 int avpriv_lock_avformat(void)
1402 if (lockmgr_cb) {
1403 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1404 return -1;
1406 return 0;
1409 int avpriv_unlock_avformat(void)
1411 if (lockmgr_cb) {
1412 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1413 return -1;
1415 return 0;
1418 unsigned int avpriv_toupper4(unsigned int x)
1420 return av_toupper(x & 0xFF) +
1421 (av_toupper((x >> 8) & 0xFF) << 8) +
1422 (av_toupper((x >> 16) & 0xFF) << 16) +
1423 (av_toupper((x >> 24) & 0xFF) << 24);
1426 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
1428 int ret;
1430 dst->owner = src->owner;
1432 ret = av_frame_ref(dst->f, src->f);
1433 if (ret < 0)
1434 return ret;
1436 if (src->progress &&
1437 !(dst->progress = av_buffer_ref(src->progress))) {
1438 ff_thread_release_buffer(dst->owner, dst);
1439 return AVERROR(ENOMEM);
1442 return 0;
1445 #if !HAVE_THREADS
1447 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
1449 f->owner = avctx;
1450 return ff_get_buffer(avctx, f->f, flags);
1453 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
1455 if (f->f)
1456 av_frame_unref(f->f);
1459 void ff_thread_finish_setup(AVCodecContext *avctx)
1463 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
1467 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
1471 #endif
1473 int avcodec_is_open(AVCodecContext *s)
1475 return !!s->internal;
1478 const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
1479 const uint8_t *end,
1480 uint32_t * restrict state)
1482 int i;
1484 assert(p <= end);
1485 if (p >= end)
1486 return end;
1488 for (i = 0; i < 3; i++) {
1489 uint32_t tmp = *state << 8;
1490 *state = tmp + *(p++);
1491 if (tmp == 0x100 || p == end)
1492 return p;
1495 while (p < end) {
1496 if (p[-1] > 1 ) p += 3;
1497 else if (p[-2] ) p += 2;
1498 else if (p[-3]|(p[-1]-1)) p++;
1499 else {
1500 p++;
1501 break;
1505 p = FFMIN(p, end) - 4;
1506 *state = AV_RB32(p);
1508 return p + 4;
1511 AVCPBProperties *av_cpb_properties_alloc(size_t *size)
1513 AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
1514 if (!props)
1515 return NULL;
1517 if (size)
1518 *size = sizeof(*props);
1520 props->vbv_delay = UINT64_MAX;
1522 return props;
1525 AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
1527 AVPacketSideData *tmp;
1528 AVCPBProperties *props;
1529 size_t size;
1531 props = av_cpb_properties_alloc(&size);
1532 if (!props)
1533 return NULL;
1535 tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
1536 if (!tmp) {
1537 av_freep(&props);
1538 return NULL;
1541 avctx->coded_side_data = tmp;
1542 avctx->nb_coded_side_data++;
1544 avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
1545 avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
1546 avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
1548 return props;
1551 static void codec_parameters_reset(AVCodecParameters *par)
1553 av_freep(&par->extradata);
1555 memset(par, 0, sizeof(*par));
1557 par->codec_type = AVMEDIA_TYPE_UNKNOWN;
1558 par->codec_id = AV_CODEC_ID_NONE;
1559 par->format = -1;
1560 par->field_order = AV_FIELD_UNKNOWN;
1561 par->color_range = AVCOL_RANGE_UNSPECIFIED;
1562 par->color_primaries = AVCOL_PRI_UNSPECIFIED;
1563 par->color_trc = AVCOL_TRC_UNSPECIFIED;
1564 par->color_space = AVCOL_SPC_UNSPECIFIED;
1565 par->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
1566 par->sample_aspect_ratio = (AVRational){ 0, 1 };
1569 AVCodecParameters *avcodec_parameters_alloc(void)
1571 AVCodecParameters *par = av_mallocz(sizeof(*par));
1573 if (!par)
1574 return NULL;
1575 codec_parameters_reset(par);
1576 return par;
1579 void avcodec_parameters_free(AVCodecParameters **ppar)
1581 AVCodecParameters *par = *ppar;
1583 if (!par)
1584 return;
1585 codec_parameters_reset(par);
1587 av_freep(ppar);
1590 int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
1592 codec_parameters_reset(dst);
1593 memcpy(dst, src, sizeof(*dst));
1595 dst->extradata = NULL;
1596 dst->extradata_size = 0;
1597 if (src->extradata) {
1598 dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
1599 if (!dst->extradata)
1600 return AVERROR(ENOMEM);
1601 memcpy(dst->extradata, src->extradata, src->extradata_size);
1602 dst->extradata_size = src->extradata_size;
1605 return 0;
1608 int avcodec_parameters_from_context(AVCodecParameters *par,
1609 const AVCodecContext *codec)
1611 codec_parameters_reset(par);
1613 par->codec_type = codec->codec_type;
1614 par->codec_id = codec->codec_id;
1615 par->codec_tag = codec->codec_tag;
1617 par->bit_rate = codec->bit_rate;
1618 par->bits_per_coded_sample = codec->bits_per_coded_sample;
1619 par->profile = codec->profile;
1620 par->level = codec->level;
1622 switch (par->codec_type) {
1623 case AVMEDIA_TYPE_VIDEO:
1624 par->format = codec->pix_fmt;
1625 par->width = codec->width;
1626 par->height = codec->height;
1627 par->field_order = codec->field_order;
1628 par->color_range = codec->color_range;
1629 par->color_primaries = codec->color_primaries;
1630 par->color_trc = codec->color_trc;
1631 par->color_space = codec->colorspace;
1632 par->chroma_location = codec->chroma_sample_location;
1633 par->sample_aspect_ratio = codec->sample_aspect_ratio;
1634 break;
1635 case AVMEDIA_TYPE_AUDIO:
1636 par->format = codec->sample_fmt;
1637 par->channel_layout = codec->channel_layout;
1638 par->channels = codec->channels;
1639 par->sample_rate = codec->sample_rate;
1640 par->block_align = codec->block_align;
1641 par->initial_padding = codec->initial_padding;
1642 break;
1645 if (codec->extradata) {
1646 par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
1647 if (!par->extradata)
1648 return AVERROR(ENOMEM);
1649 memcpy(par->extradata, codec->extradata, codec->extradata_size);
1650 par->extradata_size = codec->extradata_size;
1653 return 0;
1656 int avcodec_parameters_to_context(AVCodecContext *codec,
1657 const AVCodecParameters *par)
1659 codec->codec_type = par->codec_type;
1660 codec->codec_id = par->codec_id;
1661 codec->codec_tag = par->codec_tag;
1663 codec->bit_rate = par->bit_rate;
1664 codec->bits_per_coded_sample = par->bits_per_coded_sample;
1665 codec->profile = par->profile;
1666 codec->level = par->level;
1668 switch (par->codec_type) {
1669 case AVMEDIA_TYPE_VIDEO:
1670 codec->pix_fmt = par->format;
1671 codec->width = par->width;
1672 codec->height = par->height;
1673 codec->field_order = par->field_order;
1674 codec->color_range = par->color_range;
1675 codec->color_primaries = par->color_primaries;
1676 codec->color_trc = par->color_trc;
1677 codec->colorspace = par->color_space;
1678 codec->chroma_sample_location = par->chroma_location;
1679 codec->sample_aspect_ratio = par->sample_aspect_ratio;
1680 break;
1681 case AVMEDIA_TYPE_AUDIO:
1682 codec->sample_fmt = par->format;
1683 codec->channel_layout = par->channel_layout;
1684 codec->channels = par->channels;
1685 codec->sample_rate = par->sample_rate;
1686 codec->block_align = par->block_align;
1687 codec->initial_padding = par->initial_padding;
1688 break;
1691 if (par->extradata) {
1692 av_freep(&codec->extradata);
1693 codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
1694 if (!codec->extradata)
1695 return AVERROR(ENOMEM);
1696 memcpy(codec->extradata, par->extradata, par->extradata_size);
1697 codec->extradata_size = par->extradata_size;
1700 return 0;