3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
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.
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.
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
29 #include "libavutil/atomic.h"
30 #include "libavutil/attributes.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/avstring.h"
33 #include "libavutil/bprint.h"
34 #include "libavutil/channel_layout.h"
35 #include "libavutil/crc.h"
36 #include "libavutil/frame.h"
37 #include "libavutil/internal.h"
38 #include "libavutil/mathematics.h"
39 #include "libavutil/pixdesc.h"
40 #include "libavutil/imgutils.h"
41 #include "libavutil/samplefmt.h"
42 #include "libavutil/dict.h"
44 #include "libavutil/opt.h"
46 #include "mpegvideo.h"
48 #include "frame_thread_encoder.h"
51 #include "bytestream.h"
64 #include "compat/w32pthreads.h"
66 #include "compat/os2threads.h"
69 #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
70 static int default_lockmgr_cb(void **arg, enum AVLockOp op)
72 void * volatile * mutex = arg;
80 pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
82 return AVERROR(ENOMEM);
83 if ((err = pthread_mutex_init(tmp, NULL))) {
87 if (avpriv_atomic_ptr_cas(mutex, NULL, tmp)) {
88 pthread_mutex_destroy(tmp);
93 if ((err = pthread_mutex_lock(*mutex)))
98 if ((err = pthread_mutex_unlock(*mutex)))
102 case AV_LOCK_DESTROY:
104 pthread_mutex_destroy(*mutex);
106 avpriv_atomic_ptr_cas(mutex, *mutex, NULL);
111 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = default_lockmgr_cb;
113 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = NULL;
117 volatile int ff_avcodec_locked;
118 static int volatile entangled_thread_counter = 0;
119 static void *codec_mutex;
120 static void *avformat_mutex;
122 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
125 if (min_size <= *size && *p)
127 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
129 *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
136 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
139 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
144 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
145 memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
148 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
151 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
156 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
157 memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
160 /* encoder management */
161 static AVCodec *first_avcodec = NULL;
162 static AVCodec **last_avcodec = &first_avcodec;
164 AVCodec *av_codec_next(const AVCodec *c)
169 return first_avcodec;
172 static av_cold void avcodec_init(void)
174 static int initialized = 0;
176 if (initialized != 0)
181 ff_me_cmp_init_static();
184 int av_codec_is_encoder(const AVCodec *codec)
186 return codec && (codec->encode_sub || codec->encode2);
189 int av_codec_is_decoder(const AVCodec *codec)
191 return codec && codec->decode;
194 av_cold void avcodec_register(AVCodec *codec)
201 while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
203 last_avcodec = &codec->next;
205 if (codec->init_static_data)
206 codec->init_static_data(codec);
210 unsigned avcodec_get_edge_width(void)
216 #if FF_API_SET_DIMENSIONS
217 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
219 int ret = ff_set_dimensions(s, width, height);
221 av_log(s, AV_LOG_WARNING, "Failed to set dimensions %d %d\n", width, height);
226 int ff_set_dimensions(AVCodecContext *s, int width, int height)
228 int ret = av_image_check_size(width, height, 0, s);
233 s->coded_width = width;
234 s->coded_height = height;
235 s->width = FF_CEIL_RSHIFT(width, s->lowres);
236 s->height = FF_CEIL_RSHIFT(height, s->lowres);
241 int ff_set_sar(AVCodecContext *avctx, AVRational sar)
243 int ret = av_image_check_sar(avctx->width, avctx->height, sar);
246 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
248 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
251 avctx->sample_aspect_ratio = sar;
256 int ff_side_data_update_matrix_encoding(AVFrame *frame,
257 enum AVMatrixEncoding matrix_encoding)
259 AVFrameSideData *side_data;
260 enum AVMatrixEncoding *data;
262 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
264 side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
265 sizeof(enum AVMatrixEncoding));
268 return AVERROR(ENOMEM);
270 data = (enum AVMatrixEncoding*)side_data->data;
271 *data = matrix_encoding;
276 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
277 int linesize_align[AV_NUM_DATA_POINTERS])
283 switch (s->pix_fmt) {
284 case AV_PIX_FMT_YUV420P:
285 case AV_PIX_FMT_YUYV422:
286 case AV_PIX_FMT_YVYU422:
287 case AV_PIX_FMT_UYVY422:
288 case AV_PIX_FMT_YUV422P:
289 case AV_PIX_FMT_YUV440P:
290 case AV_PIX_FMT_YUV444P:
291 case AV_PIX_FMT_GBRAP:
292 case AV_PIX_FMT_GBRP:
293 case AV_PIX_FMT_GRAY8:
294 case AV_PIX_FMT_GRAY16BE:
295 case AV_PIX_FMT_GRAY16LE:
296 case AV_PIX_FMT_YUVJ420P:
297 case AV_PIX_FMT_YUVJ422P:
298 case AV_PIX_FMT_YUVJ440P:
299 case AV_PIX_FMT_YUVJ444P:
300 case AV_PIX_FMT_YUVA420P:
301 case AV_PIX_FMT_YUVA422P:
302 case AV_PIX_FMT_YUVA444P:
303 case AV_PIX_FMT_YUV420P9LE:
304 case AV_PIX_FMT_YUV420P9BE:
305 case AV_PIX_FMT_YUV420P10LE:
306 case AV_PIX_FMT_YUV420P10BE:
307 case AV_PIX_FMT_YUV420P12LE:
308 case AV_PIX_FMT_YUV420P12BE:
309 case AV_PIX_FMT_YUV420P14LE:
310 case AV_PIX_FMT_YUV420P14BE:
311 case AV_PIX_FMT_YUV420P16LE:
312 case AV_PIX_FMT_YUV420P16BE:
313 case AV_PIX_FMT_YUVA420P9LE:
314 case AV_PIX_FMT_YUVA420P9BE:
315 case AV_PIX_FMT_YUVA420P10LE:
316 case AV_PIX_FMT_YUVA420P10BE:
317 case AV_PIX_FMT_YUVA420P16LE:
318 case AV_PIX_FMT_YUVA420P16BE:
319 case AV_PIX_FMT_YUV422P9LE:
320 case AV_PIX_FMT_YUV422P9BE:
321 case AV_PIX_FMT_YUV422P10LE:
322 case AV_PIX_FMT_YUV422P10BE:
323 case AV_PIX_FMT_YUV422P12LE:
324 case AV_PIX_FMT_YUV422P12BE:
325 case AV_PIX_FMT_YUV422P14LE:
326 case AV_PIX_FMT_YUV422P14BE:
327 case AV_PIX_FMT_YUV422P16LE:
328 case AV_PIX_FMT_YUV422P16BE:
329 case AV_PIX_FMT_YUVA422P9LE:
330 case AV_PIX_FMT_YUVA422P9BE:
331 case AV_PIX_FMT_YUVA422P10LE:
332 case AV_PIX_FMT_YUVA422P10BE:
333 case AV_PIX_FMT_YUVA422P16LE:
334 case AV_PIX_FMT_YUVA422P16BE:
335 case AV_PIX_FMT_YUV444P9LE:
336 case AV_PIX_FMT_YUV444P9BE:
337 case AV_PIX_FMT_YUV444P10LE:
338 case AV_PIX_FMT_YUV444P10BE:
339 case AV_PIX_FMT_YUV444P12LE:
340 case AV_PIX_FMT_YUV444P12BE:
341 case AV_PIX_FMT_YUV444P14LE:
342 case AV_PIX_FMT_YUV444P14BE:
343 case AV_PIX_FMT_YUV444P16LE:
344 case AV_PIX_FMT_YUV444P16BE:
345 case AV_PIX_FMT_YUVA444P9LE:
346 case AV_PIX_FMT_YUVA444P9BE:
347 case AV_PIX_FMT_YUVA444P10LE:
348 case AV_PIX_FMT_YUVA444P10BE:
349 case AV_PIX_FMT_YUVA444P16LE:
350 case AV_PIX_FMT_YUVA444P16BE:
351 case AV_PIX_FMT_GBRP9LE:
352 case AV_PIX_FMT_GBRP9BE:
353 case AV_PIX_FMT_GBRP10LE:
354 case AV_PIX_FMT_GBRP10BE:
355 case AV_PIX_FMT_GBRP12LE:
356 case AV_PIX_FMT_GBRP12BE:
357 case AV_PIX_FMT_GBRP14LE:
358 case AV_PIX_FMT_GBRP14BE:
359 case AV_PIX_FMT_GBRP16LE:
360 case AV_PIX_FMT_GBRP16BE:
361 w_align = 16; //FIXME assume 16 pixel per macroblock
362 h_align = 16 * 2; // interlaced needs 2 macroblocks height
364 case AV_PIX_FMT_YUV411P:
365 case AV_PIX_FMT_YUVJ411P:
366 case AV_PIX_FMT_UYYVYY411:
370 case AV_PIX_FMT_YUV410P:
371 if (s->codec_id == AV_CODEC_ID_SVQ1) {
376 case AV_PIX_FMT_RGB555:
377 if (s->codec_id == AV_CODEC_ID_RPZA) {
382 case AV_PIX_FMT_PAL8:
383 case AV_PIX_FMT_BGR8:
384 case AV_PIX_FMT_RGB8:
385 if (s->codec_id == AV_CODEC_ID_SMC ||
386 s->codec_id == AV_CODEC_ID_CINEPAK) {
390 if (s->codec_id == AV_CODEC_ID_JV) {
395 case AV_PIX_FMT_BGR24:
396 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
397 (s->codec_id == AV_CODEC_ID_ZLIB)) {
402 case AV_PIX_FMT_RGB24:
403 if (s->codec_id == AV_CODEC_ID_CINEPAK) {
414 if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
415 w_align = FFMAX(w_align, 8);
418 *width = FFALIGN(*width, w_align);
419 *height = FFALIGN(*height, h_align);
420 if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
421 // some of the optimized chroma MC reads one line too much
422 // which is also done in mpeg decoders with lowres > 0
425 for (i = 0; i < 4; i++)
426 linesize_align[i] = STRIDE_ALIGN;
429 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
431 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
432 int chroma_shift = desc->log2_chroma_w;
433 int linesize_align[AV_NUM_DATA_POINTERS];
436 avcodec_align_dimensions2(s, width, height, linesize_align);
437 align = FFMAX(linesize_align[0], linesize_align[3]);
438 linesize_align[1] <<= chroma_shift;
439 linesize_align[2] <<= chroma_shift;
440 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
441 *width = FFALIGN(*width, align);
444 int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
446 if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
447 return AVERROR(EINVAL);
450 *xpos = (pos&1) * 128;
451 *ypos = ((pos>>1)^(pos<4)) * 128;
456 enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
460 for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
461 if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
464 return AVCHROMA_LOC_UNSPECIFIED;
467 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
468 enum AVSampleFormat sample_fmt, const uint8_t *buf,
469 int buf_size, int align)
471 int ch, planar, needed_size, ret = 0;
473 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
474 frame->nb_samples, sample_fmt,
476 if (buf_size < needed_size)
477 return AVERROR(EINVAL);
479 planar = av_sample_fmt_is_planar(sample_fmt);
480 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
481 if (!(frame->extended_data = av_mallocz_array(nb_channels,
482 sizeof(*frame->extended_data))))
483 return AVERROR(ENOMEM);
485 frame->extended_data = frame->data;
488 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
489 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
490 sample_fmt, align)) < 0) {
491 if (frame->extended_data != frame->data)
492 av_freep(&frame->extended_data);
495 if (frame->extended_data != frame->data) {
496 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
497 frame->data[ch] = frame->extended_data[ch];
503 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
505 FramePool *pool = avctx->internal->pool;
508 switch (avctx->codec_type) {
509 case AVMEDIA_TYPE_VIDEO: {
512 int w = frame->width;
513 int h = frame->height;
514 int tmpsize, unaligned;
516 if (pool->format == frame->format &&
517 pool->width == frame->width && pool->height == frame->height)
520 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
523 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
524 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
525 av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
526 // increase alignment of w for next try (rhs gives the lowest bit set in w)
530 for (i = 0; i < 4; i++)
531 unaligned |= picture.linesize[i] % pool->stride_align[i];
534 tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
535 NULL, picture.linesize);
539 for (i = 0; i < 3 && picture.data[i + 1]; i++)
540 size[i] = picture.data[i + 1] - picture.data[i];
541 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
543 for (i = 0; i < 4; i++) {
544 av_buffer_pool_uninit(&pool->pools[i]);
545 pool->linesize[i] = picture.linesize[i];
547 pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
548 CONFIG_MEMORY_POISONING ?
551 if (!pool->pools[i]) {
552 ret = AVERROR(ENOMEM);
557 pool->format = frame->format;
558 pool->width = frame->width;
559 pool->height = frame->height;
563 case AVMEDIA_TYPE_AUDIO: {
564 int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
565 int planar = av_sample_fmt_is_planar(frame->format);
566 int planes = planar ? ch : 1;
568 if (pool->format == frame->format && pool->planes == planes &&
569 pool->channels == ch && frame->nb_samples == pool->samples)
572 av_buffer_pool_uninit(&pool->pools[0]);
573 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
574 frame->nb_samples, frame->format, 0);
578 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
579 if (!pool->pools[0]) {
580 ret = AVERROR(ENOMEM);
584 pool->format = frame->format;
585 pool->planes = planes;
587 pool->samples = frame->nb_samples;
590 default: av_assert0(0);
594 for (i = 0; i < 4; i++)
595 av_buffer_pool_uninit(&pool->pools[i]);
597 pool->planes = pool->channels = pool->samples = 0;
598 pool->width = pool->height = 0;
602 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
604 FramePool *pool = avctx->internal->pool;
605 int planes = pool->planes;
608 frame->linesize[0] = pool->linesize[0];
610 if (planes > AV_NUM_DATA_POINTERS) {
611 frame->extended_data = av_mallocz_array(planes, sizeof(*frame->extended_data));
612 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
613 frame->extended_buf = av_mallocz_array(frame->nb_extended_buf,
614 sizeof(*frame->extended_buf));
615 if (!frame->extended_data || !frame->extended_buf) {
616 av_freep(&frame->extended_data);
617 av_freep(&frame->extended_buf);
618 return AVERROR(ENOMEM);
621 frame->extended_data = frame->data;
622 av_assert0(frame->nb_extended_buf == 0);
625 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
626 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
629 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
631 for (i = 0; i < frame->nb_extended_buf; i++) {
632 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
633 if (!frame->extended_buf[i])
635 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
638 if (avctx->debug & FF_DEBUG_BUFFERS)
639 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
643 av_frame_unref(frame);
644 return AVERROR(ENOMEM);
647 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
649 FramePool *pool = s->internal->pool;
653 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
657 memset(pic->data, 0, sizeof(pic->data));
658 pic->extended_data = pic->data;
660 for (i = 0; i < 4 && pool->pools[i]; i++) {
661 pic->linesize[i] = pool->linesize[i];
663 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
667 pic->data[i] = pic->buf[i]->data;
669 for (; i < AV_NUM_DATA_POINTERS; i++) {
671 pic->linesize[i] = 0;
673 if (pic->data[1] && !pic->data[2])
674 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
676 if (s->debug & FF_DEBUG_BUFFERS)
677 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
682 return AVERROR(ENOMEM);
685 void avpriv_color_frame(AVFrame *frame, const int c[4])
687 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
690 av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
692 for (p = 0; p<desc->nb_components; p++) {
693 uint8_t *dst = frame->data[p];
694 int is_chroma = p == 1 || p == 2;
695 int bytes = is_chroma ? FF_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
696 int height = is_chroma ? FF_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
697 for (y = 0; y < height; y++) {
698 if (desc->comp[0].depth_minus1 >= 8) {
699 for (x = 0; x<bytes; x++)
700 ((uint16_t*)dst)[x] = c[p];
702 memset(dst, c[p], bytes);
703 dst += frame->linesize[p];
708 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
712 if ((ret = update_frame_pool(avctx, frame)) < 0)
715 #if FF_API_GET_BUFFER
716 FF_DISABLE_DEPRECATION_WARNINGS
717 frame->type = FF_BUFFER_TYPE_INTERNAL;
718 FF_ENABLE_DEPRECATION_WARNINGS
721 switch (avctx->codec_type) {
722 case AVMEDIA_TYPE_VIDEO:
723 return video_get_buffer(avctx, frame);
724 case AVMEDIA_TYPE_AUDIO:
725 return audio_get_buffer(avctx, frame);
731 int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
733 AVPacket *pkt = avctx->internal->pkt;
737 AVFrameSideData *frame_sd;
739 frame->pkt_pts = pkt->pts;
740 av_frame_set_pkt_pos (frame, pkt->pos);
741 av_frame_set_pkt_duration(frame, pkt->duration);
742 av_frame_set_pkt_size (frame, pkt->size);
744 /* copy the replaygain data to the output frame */
745 packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_REPLAYGAIN, &size);
747 frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_REPLAYGAIN, size);
749 return AVERROR(ENOMEM);
751 memcpy(frame_sd->data, packet_sd, size);
754 /* copy the displaymatrix to the output frame */
755 packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_DISPLAYMATRIX, &size);
757 frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX, size);
759 return AVERROR(ENOMEM);
761 memcpy(frame_sd->data, packet_sd, size);
764 /* copy the stereo3d format to the output frame */
765 packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_STEREO3D, &size);
767 frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_STEREO3D, size);
769 return AVERROR(ENOMEM);
771 memcpy(frame_sd->data, packet_sd, size);
774 frame->pkt_pts = AV_NOPTS_VALUE;
775 av_frame_set_pkt_pos (frame, -1);
776 av_frame_set_pkt_duration(frame, 0);
777 av_frame_set_pkt_size (frame, -1);
779 frame->reordered_opaque = avctx->reordered_opaque;
781 if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
782 frame->color_primaries = avctx->color_primaries;
783 if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
784 frame->color_trc = avctx->color_trc;
785 if (av_frame_get_colorspace(frame) == AVCOL_SPC_UNSPECIFIED)
786 av_frame_set_colorspace(frame, avctx->colorspace);
787 if (av_frame_get_color_range(frame) == AVCOL_RANGE_UNSPECIFIED)
788 av_frame_set_color_range(frame, avctx->color_range);
789 if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
790 frame->chroma_location = avctx->chroma_sample_location;
792 switch (avctx->codec->type) {
793 case AVMEDIA_TYPE_VIDEO:
794 frame->format = avctx->pix_fmt;
795 if (!frame->sample_aspect_ratio.num)
796 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
798 if (frame->width && frame->height &&
799 av_image_check_sar(frame->width, frame->height,
800 frame->sample_aspect_ratio) < 0) {
801 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
802 frame->sample_aspect_ratio.num,
803 frame->sample_aspect_ratio.den);
804 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
808 case AVMEDIA_TYPE_AUDIO:
809 if (!frame->sample_rate)
810 frame->sample_rate = avctx->sample_rate;
811 if (frame->format < 0)
812 frame->format = avctx->sample_fmt;
813 if (!frame->channel_layout) {
814 if (avctx->channel_layout) {
815 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
817 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
819 return AVERROR(EINVAL);
822 frame->channel_layout = avctx->channel_layout;
824 if (avctx->channels > FF_SANE_NB_CHANNELS) {
825 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
827 return AVERROR(ENOSYS);
831 av_frame_set_channels(frame, avctx->channels);
837 #if FF_API_GET_BUFFER
838 FF_DISABLE_DEPRECATION_WARNINGS
839 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
841 return avcodec_default_get_buffer2(avctx, frame, 0);
844 typedef struct CompatReleaseBufPriv {
845 AVCodecContext avctx;
847 uint8_t avframe_padding[1024]; // hack to allow linking to a avutil with larger AVFrame
848 } CompatReleaseBufPriv;
850 static void compat_free_buffer(void *opaque, uint8_t *data)
852 CompatReleaseBufPriv *priv = opaque;
853 if (priv->avctx.release_buffer)
854 priv->avctx.release_buffer(&priv->avctx, &priv->frame);
858 static void compat_release_buffer(void *opaque, uint8_t *data)
860 AVBufferRef *buf = opaque;
861 av_buffer_unref(&buf);
863 FF_ENABLE_DEPRECATION_WARNINGS
866 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
868 return ff_init_buffer_info(avctx, frame);
871 static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
873 const AVHWAccel *hwaccel = avctx->hwaccel;
874 int override_dimensions = 1;
877 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
878 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
879 av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
880 return AVERROR(EINVAL);
883 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
884 if (frame->width <= 0 || frame->height <= 0) {
885 frame->width = FFMAX(avctx->width, FF_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
886 frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
887 override_dimensions = 0;
890 ret = ff_decode_frame_props(avctx, frame);
893 if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
896 if (hwaccel && hwaccel->alloc_frame) {
897 ret = hwaccel->alloc_frame(avctx, frame);
901 #if FF_API_GET_BUFFER
902 FF_DISABLE_DEPRECATION_WARNINGS
904 * Wrap an old get_buffer()-allocated buffer in a bunch of AVBuffers.
905 * We wrap each plane in its own AVBuffer. Each of those has a reference to
906 * a dummy AVBuffer as its private data, unreffing it on free.
907 * When all the planes are freed, the dummy buffer's free callback calls
910 if (avctx->get_buffer) {
911 CompatReleaseBufPriv *priv = NULL;
912 AVBufferRef *dummy_buf = NULL;
915 if (flags & AV_GET_BUFFER_FLAG_REF)
916 frame->reference = 1;
918 ret = avctx->get_buffer(avctx, frame);
922 /* return if the buffers are already set up
923 * this would happen e.g. when a custom get_buffer() calls
924 * avcodec_default_get_buffer
929 priv = av_mallocz(sizeof(*priv));
931 ret = AVERROR(ENOMEM);
934 priv->avctx = *avctx;
935 priv->frame = *frame;
937 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
939 ret = AVERROR(ENOMEM);
943 #define WRAP_PLANE(ref_out, data, data_size) \
945 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
947 ret = AVERROR(ENOMEM); \
950 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
953 av_frame_unref(frame); \
954 ret = AVERROR(ENOMEM); \
959 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
960 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
962 planes = av_pix_fmt_count_planes(frame->format);
963 /* workaround for AVHWAccel plane count of 0, buf[0] is used as
964 check for allocated buffers: make libavcodec happy */
965 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
967 if (!desc || planes <= 0) {
968 ret = AVERROR(EINVAL);
972 for (i = 0; i < planes; i++) {
973 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
974 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
976 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
979 int planar = av_sample_fmt_is_planar(frame->format);
980 planes = planar ? avctx->channels : 1;
982 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
983 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
984 frame->extended_buf = av_malloc_array(sizeof(*frame->extended_buf),
985 frame->nb_extended_buf);
986 if (!frame->extended_buf) {
987 ret = AVERROR(ENOMEM);
992 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
993 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
995 for (i = 0; i < frame->nb_extended_buf; i++)
996 WRAP_PLANE(frame->extended_buf[i],
997 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
1001 av_buffer_unref(&dummy_buf);
1004 frame->width = avctx->width;
1005 frame->height = avctx->height;
1010 avctx->release_buffer(avctx, frame);
1012 av_buffer_unref(&dummy_buf);
1015 FF_ENABLE_DEPRECATION_WARNINGS
1018 ret = avctx->get_buffer2(avctx, frame, flags);
1021 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
1022 frame->width = avctx->width;
1023 frame->height = avctx->height;
1029 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1031 int ret = get_buffer_internal(avctx, frame, flags);
1033 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1037 static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
1042 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
1044 if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
1045 av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
1046 frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
1047 av_frame_unref(frame);
1050 ff_init_buffer_info(avctx, frame);
1052 if (!frame->data[0])
1053 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1055 if (av_frame_is_writable(frame))
1056 return ff_decode_frame_props(avctx, frame);
1058 tmp = av_frame_alloc();
1060 return AVERROR(ENOMEM);
1062 av_frame_move_ref(tmp, frame);
1064 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1066 av_frame_free(&tmp);
1070 av_frame_copy(frame, tmp);
1071 av_frame_free(&tmp);
1076 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
1078 int ret = reget_buffer_internal(avctx, frame);
1080 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1084 #if FF_API_GET_BUFFER
1085 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
1087 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
1089 av_frame_unref(pic);
1092 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
1099 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
1103 for (i = 0; i < count; i++) {
1104 int r = func(c, (char *)arg + i * size);
1111 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
1115 for (i = 0; i < count; i++) {
1116 int r = func(c, arg, i, 0);
1123 enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
1124 unsigned int fourcc)
1126 while (tags->pix_fmt >= 0) {
1127 if (tags->fourcc == fourcc)
1128 return tags->pix_fmt;
1131 return AV_PIX_FMT_NONE;
1134 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
1136 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
1137 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
1140 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
1142 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
1147 static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
1148 enum AVPixelFormat pix_fmt)
1150 AVHWAccel *hwaccel = NULL;
1152 while ((hwaccel = av_hwaccel_next(hwaccel)))
1153 if (hwaccel->id == codec_id
1154 && hwaccel->pix_fmt == pix_fmt)
1159 static int setup_hwaccel(AVCodecContext *avctx,
1160 const enum AVPixelFormat fmt,
1163 AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
1167 av_log(avctx, AV_LOG_ERROR,
1168 "Could not find an AVHWAccel for the pixel format: %s",
1170 return AVERROR(ENOENT);
1173 if (hwa->priv_data_size) {
1174 avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
1175 if (!avctx->internal->hwaccel_priv_data)
1176 return AVERROR(ENOMEM);
1180 ret = hwa->init(avctx);
1182 av_freep(&avctx->internal->hwaccel_priv_data);
1187 avctx->hwaccel = hwa;
1192 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
1194 const AVPixFmtDescriptor *desc;
1195 enum AVPixelFormat *choices;
1196 enum AVPixelFormat ret;
1199 while (fmt[n] != AV_PIX_FMT_NONE)
1202 choices = av_malloc_array(n + 1, sizeof(*choices));
1204 return AV_PIX_FMT_NONE;
1206 memcpy(choices, fmt, (n + 1) * sizeof(*choices));
1209 ret = avctx->get_format(avctx, choices);
1211 desc = av_pix_fmt_desc_get(ret);
1213 ret = AV_PIX_FMT_NONE;
1217 if (avctx->hwaccel && avctx->hwaccel->uninit)
1218 avctx->hwaccel->uninit(avctx);
1219 av_freep(&avctx->internal->hwaccel_priv_data);
1220 avctx->hwaccel = NULL;
1222 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
1224 if (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
1227 if (!setup_hwaccel(avctx, ret, desc->name))
1230 /* Remove failed hwaccel from choices */
1231 for (n = 0; choices[n] != ret; n++)
1232 av_assert0(choices[n] != AV_PIX_FMT_NONE);
1235 choices[n] = choices[n + 1];
1236 while (choices[n++] != AV_PIX_FMT_NONE);
1243 #if FF_API_AVFRAME_LAVC
1244 void avcodec_get_frame_defaults(AVFrame *frame)
1246 #if LIBAVCODEC_VERSION_MAJOR >= 55
1247 // extended_data should explicitly be freed when needed, this code is unsafe currently
1248 // also this is not compatible to the <55 ABI/API
1249 if (frame->extended_data != frame->data && 0)
1250 av_freep(&frame->extended_data);
1253 memset(frame, 0, sizeof(AVFrame));
1254 av_frame_unref(frame);
1257 AVFrame *avcodec_alloc_frame(void)
1259 return av_frame_alloc();
1262 void avcodec_free_frame(AVFrame **frame)
1264 av_frame_free(frame);
1268 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
1269 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
1270 MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
1271 MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
1272 MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
1274 int av_codec_get_max_lowres(const AVCodec *codec)
1276 return codec->max_lowres;
1279 static void get_subtitle_defaults(AVSubtitle *sub)
1281 memset(sub, 0, sizeof(*sub));
1282 sub->pts = AV_NOPTS_VALUE;
1285 static int get_bit_rate(AVCodecContext *ctx)
1288 int bits_per_sample;
1290 switch (ctx->codec_type) {
1291 case AVMEDIA_TYPE_VIDEO:
1292 case AVMEDIA_TYPE_DATA:
1293 case AVMEDIA_TYPE_SUBTITLE:
1294 case AVMEDIA_TYPE_ATTACHMENT:
1295 bit_rate = ctx->bit_rate;
1297 case AVMEDIA_TYPE_AUDIO:
1298 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1299 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1308 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1312 ff_unlock_avcodec();
1314 ret = avcodec_open2(avctx, codec, options);
1316 ff_lock_avcodec(avctx);
1320 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1323 AVDictionary *tmp = NULL;
1325 if (avcodec_is_open(avctx))
1328 if ((!codec && !avctx->codec)) {
1329 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
1330 return AVERROR(EINVAL);
1332 if ((codec && avctx->codec && codec != avctx->codec)) {
1333 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
1334 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
1335 return AVERROR(EINVAL);
1338 codec = avctx->codec;
1340 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
1341 return AVERROR(EINVAL);
1344 av_dict_copy(&tmp, *options, 0);
1346 ret = ff_lock_avcodec(avctx);
1350 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
1351 if (!avctx->internal) {
1352 ret = AVERROR(ENOMEM);
1356 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
1357 if (!avctx->internal->pool) {
1358 ret = AVERROR(ENOMEM);
1362 avctx->internal->to_free = av_frame_alloc();
1363 if (!avctx->internal->to_free) {
1364 ret = AVERROR(ENOMEM);
1368 if (codec->priv_data_size > 0) {
1369 if (!avctx->priv_data) {
1370 avctx->priv_data = av_mallocz(codec->priv_data_size);
1371 if (!avctx->priv_data) {
1372 ret = AVERROR(ENOMEM);
1375 if (codec->priv_class) {
1376 *(const AVClass **)avctx->priv_data = codec->priv_class;
1377 av_opt_set_defaults(avctx->priv_data);
1380 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
1383 avctx->priv_data = NULL;
1385 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
1388 // only call ff_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
1389 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
1390 (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
1391 if (avctx->coded_width && avctx->coded_height)
1392 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
1393 else if (avctx->width && avctx->height)
1394 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1399 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1400 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1401 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1402 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
1403 ff_set_dimensions(avctx, 0, 0);
1406 if (avctx->width > 0 && avctx->height > 0) {
1407 if (av_image_check_sar(avctx->width, avctx->height,
1408 avctx->sample_aspect_ratio) < 0) {
1409 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1410 avctx->sample_aspect_ratio.num,
1411 avctx->sample_aspect_ratio.den);
1412 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
1416 /* if the decoder init function was already called previously,
1417 * free the already allocated subtitle_header before overwriting it */
1418 if (av_codec_is_decoder(codec))
1419 av_freep(&avctx->subtitle_header);
1421 if (avctx->channels > FF_SANE_NB_CHANNELS) {
1422 ret = AVERROR(EINVAL);
1426 avctx->codec = codec;
1427 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1428 avctx->codec_id == AV_CODEC_ID_NONE) {
1429 avctx->codec_type = codec->type;
1430 avctx->codec_id = codec->id;
1432 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1433 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1434 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
1435 ret = AVERROR(EINVAL);
1438 avctx->frame_number = 0;
1439 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
1441 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1442 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1443 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
1445 av_log(avctx, AV_LOG_ERROR,
1446 "The %s '%s' is experimental but experimental codecs are not enabled, "
1447 "add '-strict %d' if you want to use it.\n",
1448 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
1449 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
1450 if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
1451 av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
1452 codec_string, codec2->name);
1453 ret = AVERROR_EXPERIMENTAL;
1457 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1458 (!avctx->time_base.num || !avctx->time_base.den)) {
1459 avctx->time_base.num = 1;
1460 avctx->time_base.den = avctx->sample_rate;
1464 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
1466 if (CONFIG_FRAME_THREAD_ENCODER) {
1467 ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
1468 ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
1469 ff_lock_avcodec(avctx);
1475 && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
1476 ret = ff_thread_init(avctx);
1481 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
1482 avctx->thread_count = 1;
1484 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1485 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
1486 avctx->codec->max_lowres);
1487 ret = AVERROR(EINVAL);
1492 if (avctx->debug_mv)
1493 av_log(avctx, AV_LOG_WARNING, "The 'vismv' option is deprecated, "
1494 "see the codecview filter instead.\n");
1497 if (av_codec_is_encoder(avctx->codec)) {
1499 if (avctx->codec->sample_fmts) {
1500 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1501 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1503 if (avctx->channels == 1 &&
1504 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1505 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1506 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1510 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1512 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
1513 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
1514 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
1515 ret = AVERROR(EINVAL);
1519 if (avctx->codec->pix_fmts) {
1520 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1521 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1523 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
1524 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
1525 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
1527 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
1528 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
1529 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
1530 ret = AVERROR(EINVAL);
1533 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
1534 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ411P ||
1535 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
1536 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
1537 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
1538 avctx->color_range = AVCOL_RANGE_JPEG;
1540 if (avctx->codec->supported_samplerates) {
1541 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1542 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1544 if (avctx->codec->supported_samplerates[i] == 0) {
1545 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1546 avctx->sample_rate);
1547 ret = AVERROR(EINVAL);
1551 if (avctx->codec->channel_layouts) {
1552 if (!avctx->channel_layout) {
1553 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
1555 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1556 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1558 if (avctx->codec->channel_layouts[i] == 0) {
1560 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1561 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
1562 ret = AVERROR(EINVAL);
1567 if (avctx->channel_layout && avctx->channels) {
1568 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1569 if (channels != avctx->channels) {
1571 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1572 av_log(avctx, AV_LOG_ERROR,
1573 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1574 buf, channels, avctx->channels);
1575 ret = AVERROR(EINVAL);
1578 } else if (avctx->channel_layout) {
1579 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1581 if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1582 if (avctx->width <= 0 || avctx->height <= 0) {
1583 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
1584 ret = AVERROR(EINVAL);
1588 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
1589 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
1590 av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
1593 if (!avctx->rc_initial_buffer_occupancy)
1594 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1597 avctx->pts_correction_num_faulty_pts =
1598 avctx->pts_correction_num_faulty_dts = 0;
1599 avctx->pts_correction_last_pts =
1600 avctx->pts_correction_last_dts = INT64_MIN;
1602 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1603 || avctx->internal->frame_thread_encoder)) {
1604 ret = avctx->codec->init(avctx);
1612 #if FF_API_AUDIOENC_DELAY
1613 if (av_codec_is_encoder(avctx->codec))
1614 avctx->delay = avctx->initial_padding;
1617 if (av_codec_is_decoder(avctx->codec)) {
1618 if (!avctx->bit_rate)
1619 avctx->bit_rate = get_bit_rate(avctx);
1620 /* validate channel layout from the decoder */
1621 if (avctx->channel_layout) {
1622 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1623 if (!avctx->channels)
1624 avctx->channels = channels;
1625 else if (channels != avctx->channels) {
1627 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1628 av_log(avctx, AV_LOG_WARNING,
1629 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1630 "ignoring specified channel layout\n",
1631 buf, channels, avctx->channels);
1632 avctx->channel_layout = 0;
1635 if (avctx->channels && avctx->channels < 0 ||
1636 avctx->channels > FF_SANE_NB_CHANNELS) {
1637 ret = AVERROR(EINVAL);
1640 if (avctx->sub_charenc) {
1641 if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1642 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1643 "supported with subtitles codecs\n");
1644 ret = AVERROR(EINVAL);
1646 } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1647 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1648 "subtitles character encoding will be ignored\n",
1649 avctx->codec_descriptor->name);
1650 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
1652 /* input character encoding is set for a text based subtitle
1653 * codec at this point */
1654 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
1655 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
1657 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
1659 iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1660 if (cd == (iconv_t)-1) {
1661 av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1662 "with input character encoding \"%s\"\n", avctx->sub_charenc);
1663 ret = AVERROR(errno);
1668 av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1669 "conversion needs a libavcodec built with iconv support "
1670 "for this codec\n");
1671 ret = AVERROR(ENOSYS);
1678 #if FF_API_AVCTX_TIMEBASE
1679 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1680 avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
1684 ff_unlock_avcodec();
1686 av_dict_free(options);
1693 av_freep(&avctx->priv_data);
1694 if (avctx->internal) {
1695 av_frame_free(&avctx->internal->to_free);
1696 av_freep(&avctx->internal->pool);
1698 av_freep(&avctx->internal);
1699 avctx->codec = NULL;
1703 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
1705 if (avpkt->size < 0) {
1706 av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
1707 return AVERROR(EINVAL);
1709 if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1710 av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
1711 size, INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
1712 return AVERROR(EINVAL);
1716 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1717 if (!avpkt->data || avpkt->size < size) {
1718 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
1719 avpkt->data = avctx->internal->byte_buffer;
1720 avpkt->size = avctx->internal->byte_buffer_size;
1721 #if FF_API_DESTRUCT_PACKET
1722 FF_DISABLE_DEPRECATION_WARNINGS
1723 avpkt->destruct = NULL;
1724 FF_ENABLE_DEPRECATION_WARNINGS
1730 AVBufferRef *buf = avpkt->buf;
1731 #if FF_API_DESTRUCT_PACKET
1732 FF_DISABLE_DEPRECATION_WARNINGS
1733 void *destruct = avpkt->destruct;
1734 FF_ENABLE_DEPRECATION_WARNINGS
1737 if (avpkt->size < size) {
1738 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
1739 return AVERROR(EINVAL);
1742 av_init_packet(avpkt);
1743 #if FF_API_DESTRUCT_PACKET
1744 FF_DISABLE_DEPRECATION_WARNINGS
1745 avpkt->destruct = destruct;
1746 FF_ENABLE_DEPRECATION_WARNINGS
1752 int ret = av_new_packet(avpkt, size);
1754 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
1759 int ff_alloc_packet(AVPacket *avpkt, int size)
1761 return ff_alloc_packet2(NULL, avpkt, size);
1765 * Pad last frame with silence.
1767 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1769 AVFrame *frame = NULL;
1772 if (!(frame = av_frame_alloc()))
1773 return AVERROR(ENOMEM);
1775 frame->format = src->format;
1776 frame->channel_layout = src->channel_layout;
1777 av_frame_set_channels(frame, av_frame_get_channels(src));
1778 frame->nb_samples = s->frame_size;
1779 ret = av_frame_get_buffer(frame, 32);
1783 ret = av_frame_copy_props(frame, src);
1787 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1788 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1790 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1791 frame->nb_samples - src->nb_samples,
1792 s->channels, s->sample_fmt)) < 0)
1800 av_frame_free(&frame);
1804 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1806 const AVFrame *frame,
1807 int *got_packet_ptr)
1809 AVFrame *extended_frame = NULL;
1810 AVFrame *padded_frame = NULL;
1812 AVPacket user_pkt = *avpkt;
1813 int needs_realloc = !user_pkt.data;
1815 *got_packet_ptr = 0;
1817 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1818 av_free_packet(avpkt);
1819 av_init_packet(avpkt);
1823 /* ensure that extended_data is properly set */
1824 if (frame && !frame->extended_data) {
1825 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1826 avctx->channels > AV_NUM_DATA_POINTERS) {
1827 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1828 "with more than %d channels, but extended_data is not set.\n",
1829 AV_NUM_DATA_POINTERS);
1830 return AVERROR(EINVAL);
1832 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1834 extended_frame = av_frame_alloc();
1835 if (!extended_frame)
1836 return AVERROR(ENOMEM);
1838 memcpy(extended_frame, frame, sizeof(AVFrame));
1839 extended_frame->extended_data = extended_frame->data;
1840 frame = extended_frame;
1843 /* check for valid frame size */
1845 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1846 if (frame->nb_samples > avctx->frame_size) {
1847 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1848 ret = AVERROR(EINVAL);
1851 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1852 if (frame->nb_samples < avctx->frame_size &&
1853 !avctx->internal->last_audio_frame) {
1854 ret = pad_last_frame(avctx, &padded_frame, frame);
1858 frame = padded_frame;
1859 avctx->internal->last_audio_frame = 1;
1862 if (frame->nb_samples != avctx->frame_size) {
1863 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1864 ret = AVERROR(EINVAL);
1870 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1872 if (*got_packet_ptr) {
1873 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1874 if (avpkt->pts == AV_NOPTS_VALUE)
1875 avpkt->pts = frame->pts;
1876 if (!avpkt->duration)
1877 avpkt->duration = ff_samples_to_time_base(avctx,
1880 avpkt->dts = avpkt->pts;
1885 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1887 if (user_pkt.data) {
1888 if (user_pkt.size >= avpkt->size) {
1889 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1891 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1892 avpkt->size = user_pkt.size;
1895 avpkt->buf = user_pkt.buf;
1896 avpkt->data = user_pkt.data;
1897 #if FF_API_DESTRUCT_PACKET
1898 FF_DISABLE_DEPRECATION_WARNINGS
1899 avpkt->destruct = user_pkt.destruct;
1900 FF_ENABLE_DEPRECATION_WARNINGS
1903 if (av_dup_packet(avpkt) < 0) {
1904 ret = AVERROR(ENOMEM);
1910 if (needs_realloc && avpkt->data) {
1911 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1913 avpkt->data = avpkt->buf->data;
1916 avctx->frame_number++;
1919 if (ret < 0 || !*got_packet_ptr) {
1920 av_free_packet(avpkt);
1921 av_init_packet(avpkt);
1925 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1926 * this needs to be moved to the encoders, but for now we can do it
1927 * here to simplify things */
1928 avpkt->flags |= AV_PKT_FLAG_KEY;
1931 av_frame_free(&padded_frame);
1932 av_free(extended_frame);
1934 #if FF_API_AUDIOENC_DELAY
1935 avctx->delay = avctx->initial_padding;
1941 #if FF_API_OLD_ENCODE_AUDIO
1942 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1943 uint8_t *buf, int buf_size,
1944 const short *samples)
1948 int ret, samples_size, got_packet;
1950 av_init_packet(&pkt);
1952 pkt.size = buf_size;
1955 frame = av_frame_alloc();
1957 return AVERROR(ENOMEM);
1959 if (avctx->frame_size) {
1960 frame->nb_samples = avctx->frame_size;
1962 /* if frame_size is not set, the number of samples must be
1963 * calculated from the buffer size */
1965 if (!av_get_bits_per_sample(avctx->codec_id)) {
1966 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1967 "support this codec\n");
1968 av_frame_free(&frame);
1969 return AVERROR(EINVAL);
1971 nb_samples = (int64_t)buf_size * 8 /
1972 (av_get_bits_per_sample(avctx->codec_id) *
1974 if (nb_samples >= INT_MAX) {
1975 av_frame_free(&frame);
1976 return AVERROR(EINVAL);
1978 frame->nb_samples = nb_samples;
1981 /* it is assumed that the samples buffer is large enough based on the
1982 * relevant parameters */
1983 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1985 avctx->sample_fmt, 1);
1986 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1988 (const uint8_t *)samples,
1989 samples_size, 1)) < 0) {
1990 av_frame_free(&frame);
1994 /* fabricate frame pts from sample count.
1995 * this is needed because the avcodec_encode_audio() API does not have
1996 * a way for the user to provide pts */
1997 if (avctx->sample_rate && avctx->time_base.num)
1998 frame->pts = ff_samples_to_time_base(avctx,
1999 avctx->internal->sample_count);
2001 frame->pts = AV_NOPTS_VALUE;
2002 avctx->internal->sample_count += frame->nb_samples;
2008 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
2009 if (!ret && got_packet && avctx->coded_frame) {
2010 avctx->coded_frame->pts = pkt.pts;
2011 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
2013 /* free any side data since we cannot return it */
2014 av_packet_free_side_data(&pkt);
2016 if (frame && frame->extended_data != frame->data)
2017 av_freep(&frame->extended_data);
2019 av_frame_free(&frame);
2020 return ret ? ret : pkt.size;
2025 #if FF_API_OLD_ENCODE_VIDEO
2026 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
2027 const AVFrame *pict)
2030 int ret, got_packet = 0;
2032 if (buf_size < FF_MIN_BUFFER_SIZE) {
2033 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
2037 av_init_packet(&pkt);
2039 pkt.size = buf_size;
2041 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
2042 if (!ret && got_packet && avctx->coded_frame) {
2043 avctx->coded_frame->pts = pkt.pts;
2044 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
2047 /* free any side data since we cannot return it */
2048 if (pkt.side_data_elems > 0) {
2050 for (i = 0; i < pkt.side_data_elems; i++)
2051 av_free(pkt.side_data[i].data);
2052 av_freep(&pkt.side_data);
2053 pkt.side_data_elems = 0;
2056 return ret ? ret : pkt.size;
2061 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
2063 const AVFrame *frame,
2064 int *got_packet_ptr)
2067 AVPacket user_pkt = *avpkt;
2068 int needs_realloc = !user_pkt.data;
2070 *got_packet_ptr = 0;
2072 if(CONFIG_FRAME_THREAD_ENCODER &&
2073 avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
2074 return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
2076 if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
2077 avctx->stats_out[0] = '\0';
2079 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
2080 av_free_packet(avpkt);
2081 av_init_packet(avpkt);
2086 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
2087 return AVERROR(EINVAL);
2089 av_assert0(avctx->codec->encode2);
2091 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
2092 av_assert0(ret <= 0);
2094 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
2096 if (user_pkt.data) {
2097 if (user_pkt.size >= avpkt->size) {
2098 memcpy(user_pkt.data, avpkt->data, avpkt->size);
2100 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
2101 avpkt->size = user_pkt.size;
2104 avpkt->buf = user_pkt.buf;
2105 avpkt->data = user_pkt.data;
2106 #if FF_API_DESTRUCT_PACKET
2107 FF_DISABLE_DEPRECATION_WARNINGS
2108 avpkt->destruct = user_pkt.destruct;
2109 FF_ENABLE_DEPRECATION_WARNINGS
2112 if (av_dup_packet(avpkt) < 0) {
2113 ret = AVERROR(ENOMEM);
2119 if (!*got_packet_ptr)
2121 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
2122 avpkt->pts = avpkt->dts = frame->pts;
2124 if (needs_realloc && avpkt->data) {
2125 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
2127 avpkt->data = avpkt->buf->data;
2130 avctx->frame_number++;
2133 if (ret < 0 || !*got_packet_ptr)
2134 av_free_packet(avpkt);
2136 av_packet_merge_side_data(avpkt);
2142 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
2143 const AVSubtitle *sub)
2146 if (sub->start_display_time) {
2147 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
2151 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
2152 avctx->frame_number++;
2157 * Attempt to guess proper monotonic timestamps for decoded video frames
2158 * which might have incorrect times. Input timestamps may wrap around, in
2159 * which case the output will as well.
2161 * @param pts the pts field of the decoded AVPacket, as passed through
2163 * @param dts the dts field of the decoded AVPacket
2164 * @return one of the input values, may be AV_NOPTS_VALUE
2166 static int64_t guess_correct_pts(AVCodecContext *ctx,
2167 int64_t reordered_pts, int64_t dts)
2169 int64_t pts = AV_NOPTS_VALUE;
2171 if (dts != AV_NOPTS_VALUE) {
2172 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
2173 ctx->pts_correction_last_dts = dts;
2174 } else if (reordered_pts != AV_NOPTS_VALUE)
2175 ctx->pts_correction_last_dts = reordered_pts;
2177 if (reordered_pts != AV_NOPTS_VALUE) {
2178 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
2179 ctx->pts_correction_last_pts = reordered_pts;
2180 } else if(dts != AV_NOPTS_VALUE)
2181 ctx->pts_correction_last_pts = dts;
2183 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
2184 && reordered_pts != AV_NOPTS_VALUE)
2185 pts = reordered_pts;
2192 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
2195 const uint8_t *data;
2198 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
2202 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
2203 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
2204 "changes, but PARAM_CHANGE side data was sent to it.\n");
2205 return AVERROR(EINVAL);
2211 flags = bytestream_get_le32(&data);
2214 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
2217 avctx->channels = bytestream_get_le32(&data);
2220 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
2223 avctx->channel_layout = bytestream_get_le64(&data);
2226 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
2229 avctx->sample_rate = bytestream_get_le32(&data);
2232 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
2235 avctx->width = bytestream_get_le32(&data);
2236 avctx->height = bytestream_get_le32(&data);
2238 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
2245 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
2246 return AVERROR_INVALIDDATA;
2249 static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
2252 const uint8_t *side_metadata;
2254 AVDictionary **frame_md = avpriv_frame_get_metadatap(frame);
2256 side_metadata = av_packet_get_side_data(avctx->internal->pkt,
2257 AV_PKT_DATA_STRINGS_METADATA, &size);
2258 return av_packet_unpack_dictionary(side_metadata, size, frame_md);
2261 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
2265 /* move the original frame to our backup */
2266 av_frame_unref(avci->to_free);
2267 av_frame_move_ref(avci->to_free, frame);
2269 /* now copy everything except the AVBufferRefs back
2270 * note that we make a COPY of the side data, so calling av_frame_free() on
2271 * the caller's frame will work properly */
2272 ret = av_frame_copy_props(frame, avci->to_free);
2276 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
2277 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
2278 if (avci->to_free->extended_data != avci->to_free->data) {
2279 int planes = av_frame_get_channels(avci->to_free);
2280 int size = planes * sizeof(*frame->extended_data);
2283 av_frame_unref(frame);
2287 frame->extended_data = av_malloc(size);
2288 if (!frame->extended_data) {
2289 av_frame_unref(frame);
2290 return AVERROR(ENOMEM);
2292 memcpy(frame->extended_data, avci->to_free->extended_data,
2295 frame->extended_data = frame->data;
2297 frame->format = avci->to_free->format;
2298 frame->width = avci->to_free->width;
2299 frame->height = avci->to_free->height;
2300 frame->channel_layout = avci->to_free->channel_layout;
2301 frame->nb_samples = avci->to_free->nb_samples;
2302 av_frame_set_channels(frame, av_frame_get_channels(avci->to_free));
2307 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
2308 int *got_picture_ptr,
2309 const AVPacket *avpkt)
2311 AVCodecInternal *avci = avctx->internal;
2313 // copy to ensure we do not change avpkt
2314 AVPacket tmp = *avpkt;
2317 return AVERROR(EINVAL);
2318 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
2319 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
2320 return AVERROR(EINVAL);
2323 *got_picture_ptr = 0;
2324 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
2325 return AVERROR(EINVAL);
2327 av_frame_unref(picture);
2329 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2330 int did_split = av_packet_split_side_data(&tmp);
2331 ret = apply_param_change(avctx, &tmp);
2333 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
2334 if (avctx->err_recognition & AV_EF_EXPLODE)
2338 avctx->internal->pkt = &tmp;
2339 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2340 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
2343 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
2345 picture->pkt_dts = avpkt->dts;
2347 if(!avctx->has_b_frames){
2348 av_frame_set_pkt_pos(picture, avpkt->pos);
2350 //FIXME these should be under if(!avctx->has_b_frames)
2351 /* get_buffer is supposed to set frame parameters */
2352 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
2353 if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
2354 if (!picture->width) picture->width = avctx->width;
2355 if (!picture->height) picture->height = avctx->height;
2356 if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
2359 add_metadata_from_side_data(avctx, picture);
2362 emms_c(); //needed to avoid an emms_c() call before every return;
2364 avctx->internal->pkt = NULL;
2366 av_packet_free_side_data(&tmp);
2371 if (*got_picture_ptr) {
2372 if (!avctx->refcounted_frames) {
2373 int err = unrefcount_frame(avci, picture);
2378 avctx->frame_number++;
2379 av_frame_set_best_effort_timestamp(picture,
2380 guess_correct_pts(avctx,
2384 av_frame_unref(picture);
2388 /* many decoders assign whole AVFrames, thus overwriting extended_data;
2389 * make sure it's set correctly */
2390 av_assert0(!picture->extended_data || picture->extended_data == picture->data);
2392 #if FF_API_AVCTX_TIMEBASE
2393 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
2394 avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
2400 #if FF_API_OLD_DECODE_AUDIO
2401 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
2402 int *frame_size_ptr,
2405 AVFrame *frame = av_frame_alloc();
2406 int ret, got_frame = 0;
2409 return AVERROR(ENOMEM);
2410 if (avctx->get_buffer != avcodec_default_get_buffer) {
2411 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
2412 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
2413 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
2414 "avcodec_decode_audio4()\n");
2415 avctx->get_buffer = avcodec_default_get_buffer;
2416 avctx->release_buffer = avcodec_default_release_buffer;
2419 ret = avcodec_decode_audio4(avctx, frame, &got_frame, avpkt);
2421 if (ret >= 0 && got_frame) {
2423 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
2424 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
2426 avctx->sample_fmt, 1);
2427 if (*frame_size_ptr < data_size) {
2428 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
2429 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
2430 av_frame_free(&frame);
2431 return AVERROR(EINVAL);
2434 memcpy(samples, frame->extended_data[0], plane_size);
2436 if (planar && avctx->channels > 1) {
2437 uint8_t *out = ((uint8_t *)samples) + plane_size;
2438 for (ch = 1; ch < avctx->channels; ch++) {
2439 memcpy(out, frame->extended_data[ch], plane_size);
2443 *frame_size_ptr = data_size;
2445 *frame_size_ptr = 0;
2447 av_frame_free(&frame);
2453 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
2456 const AVPacket *avpkt)
2458 AVCodecInternal *avci = avctx->internal;
2463 if (!avpkt->data && avpkt->size) {
2464 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2465 return AVERROR(EINVAL);
2468 return AVERROR(EINVAL);
2469 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
2470 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
2471 return AVERROR(EINVAL);
2474 av_frame_unref(frame);
2476 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2479 uint32_t discard_padding = 0;
2480 uint8_t skip_reason = 0;
2481 uint8_t discard_reason = 0;
2482 // copy to ensure we do not change avpkt
2483 AVPacket tmp = *avpkt;
2484 int did_split = av_packet_split_side_data(&tmp);
2485 ret = apply_param_change(avctx, &tmp);
2487 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
2488 if (avctx->err_recognition & AV_EF_EXPLODE)
2492 avctx->internal->pkt = &tmp;
2493 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2494 ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
2496 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
2497 frame->pkt_dts = avpkt->dts;
2499 if (ret >= 0 && *got_frame_ptr) {
2500 add_metadata_from_side_data(avctx, frame);
2501 avctx->frame_number++;
2502 av_frame_set_best_effort_timestamp(frame,
2503 guess_correct_pts(avctx,
2506 if (frame->format == AV_SAMPLE_FMT_NONE)
2507 frame->format = avctx->sample_fmt;
2508 if (!frame->channel_layout)
2509 frame->channel_layout = avctx->channel_layout;
2510 if (!av_frame_get_channels(frame))
2511 av_frame_set_channels(frame, avctx->channels);
2512 if (!frame->sample_rate)
2513 frame->sample_rate = avctx->sample_rate;
2516 side= av_packet_get_side_data(avctx->internal->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
2517 if(side && side_size>=10) {
2518 avctx->internal->skip_samples = AV_RL32(side);
2519 av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
2520 avctx->internal->skip_samples);
2521 discard_padding = AV_RL32(side + 4);
2522 skip_reason = AV_RL8(side + 8);
2523 discard_reason = AV_RL8(side + 9);
2525 if (avctx->internal->skip_samples && *got_frame_ptr &&
2526 !(avctx->flags2 & CODEC_FLAG2_SKIP_MANUAL)) {
2527 if(frame->nb_samples <= avctx->internal->skip_samples){
2529 avctx->internal->skip_samples -= frame->nb_samples;
2530 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
2531 avctx->internal->skip_samples);
2533 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
2534 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
2535 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2536 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
2537 (AVRational){1, avctx->sample_rate},
2538 avctx->pkt_timebase);
2539 if(frame->pkt_pts!=AV_NOPTS_VALUE)
2540 frame->pkt_pts += diff_ts;
2541 if(frame->pkt_dts!=AV_NOPTS_VALUE)
2542 frame->pkt_dts += diff_ts;
2543 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2544 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2546 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
2548 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
2549 avctx->internal->skip_samples, frame->nb_samples);
2550 frame->nb_samples -= avctx->internal->skip_samples;
2551 avctx->internal->skip_samples = 0;
2555 if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr &&
2556 !(avctx->flags2 & CODEC_FLAG2_SKIP_MANUAL)) {
2557 if (discard_padding == frame->nb_samples) {
2560 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2561 int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
2562 (AVRational){1, avctx->sample_rate},
2563 avctx->pkt_timebase);
2564 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2565 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2567 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
2569 av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
2570 discard_padding, frame->nb_samples);
2571 frame->nb_samples -= discard_padding;
2575 if ((avctx->flags2 & CODEC_FLAG2_SKIP_MANUAL) && *got_frame_ptr) {
2576 AVFrameSideData *fside = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
2578 AV_WL32(fside->data, avctx->internal->skip_samples);
2579 AV_WL32(fside->data + 4, discard_padding);
2580 AV_WL8(fside->data + 8, skip_reason);
2581 AV_WL8(fside->data + 9, discard_reason);
2582 avctx->internal->skip_samples = 0;
2586 avctx->internal->pkt = NULL;
2588 av_packet_free_side_data(&tmp);
2593 if (ret >= 0 && *got_frame_ptr) {
2594 if (!avctx->refcounted_frames) {
2595 int err = unrefcount_frame(avci, frame);
2600 av_frame_unref(frame);
2606 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
2607 static int recode_subtitle(AVCodecContext *avctx,
2608 AVPacket *outpkt, const AVPacket *inpkt)
2611 iconv_t cd = (iconv_t)-1;
2618 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
2622 cd = iconv_open("UTF-8", avctx->sub_charenc);
2623 av_assert0(cd != (iconv_t)-1);
2628 if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
2629 av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
2630 ret = AVERROR(ENOMEM);
2634 ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
2637 outpkt->buf = tmp.buf;
2638 outpkt->data = tmp.data;
2639 outpkt->size = tmp.size;
2640 outb = outpkt->data;
2641 outl = outpkt->size;
2643 if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
2644 iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
2645 outl >= outpkt->size || inl != 0) {
2646 av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
2647 "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
2648 av_free_packet(&tmp);
2649 ret = AVERROR(errno);
2652 outpkt->size -= outl;
2653 memset(outpkt->data + outpkt->size, 0, outl);
2656 if (cd != (iconv_t)-1)
2660 av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
2661 return AVERROR(EINVAL);
2665 static int utf8_check(const uint8_t *str)
2667 const uint8_t *byte;
2668 uint32_t codepoint, min;
2672 GET_UTF8(codepoint, *(byte++), return 0;);
2673 min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
2674 1 << (5 * (byte - str) - 4);
2675 if (codepoint < min || codepoint >= 0x110000 ||
2676 codepoint == 0xFFFE /* BOM */ ||
2677 codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
2684 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
2690 if (!avpkt->data && avpkt->size) {
2691 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2692 return AVERROR(EINVAL);
2695 return AVERROR(EINVAL);
2696 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
2697 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
2698 return AVERROR(EINVAL);
2702 get_subtitle_defaults(sub);
2704 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
2705 AVPacket pkt_recoded;
2706 AVPacket tmp = *avpkt;
2707 int did_split = av_packet_split_side_data(&tmp);
2708 //apply_param_change(avctx, &tmp);
2711 /* FFMIN() prevents overflow in case the packet wasn't allocated with
2713 * If the side data is smaller than the buffer padding size, the
2714 * remaining bytes should have already been filled with zeros by the
2715 * original packet allocation anyway. */
2716 memset(tmp.data + tmp.size, 0,
2717 FFMIN(avpkt->size - tmp.size, FF_INPUT_BUFFER_PADDING_SIZE));
2721 ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
2725 avctx->internal->pkt = &pkt_recoded;
2727 if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
2728 sub->pts = av_rescale_q(avpkt->pts,
2729 avctx->pkt_timebase, AV_TIME_BASE_Q);
2730 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
2731 av_assert1((ret >= 0) >= !!*got_sub_ptr &&
2732 !!*got_sub_ptr >= !!sub->num_rects);
2734 if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
2735 avctx->pkt_timebase.num) {
2736 AVRational ms = { 1, 1000 };
2737 sub->end_display_time = av_rescale_q(avpkt->duration,
2738 avctx->pkt_timebase, ms);
2741 for (i = 0; i < sub->num_rects; i++) {
2742 if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
2743 av_log(avctx, AV_LOG_ERROR,
2744 "Invalid UTF-8 in decoded subtitles text; "
2745 "maybe missing -sub_charenc option\n");
2746 avsubtitle_free(sub);
2747 return AVERROR_INVALIDDATA;
2751 if (tmp.data != pkt_recoded.data) { // did we recode?
2752 /* prevent from destroying side data from original packet */
2753 pkt_recoded.side_data = NULL;
2754 pkt_recoded.side_data_elems = 0;
2756 av_free_packet(&pkt_recoded);
2758 if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
2760 else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
2762 avctx->internal->pkt = NULL;
2766 av_packet_free_side_data(&tmp);
2772 avctx->frame_number++;
2778 void avsubtitle_free(AVSubtitle *sub)
2782 for (i = 0; i < sub->num_rects; i++) {
2783 av_freep(&sub->rects[i]->pict.data[0]);
2784 av_freep(&sub->rects[i]->pict.data[1]);
2785 av_freep(&sub->rects[i]->pict.data[2]);
2786 av_freep(&sub->rects[i]->pict.data[3]);
2787 av_freep(&sub->rects[i]->text);
2788 av_freep(&sub->rects[i]->ass);
2789 av_freep(&sub->rects[i]);
2792 av_freep(&sub->rects);
2794 memset(sub, 0, sizeof(AVSubtitle));
2797 av_cold int avcodec_close(AVCodecContext *avctx)
2802 if (avcodec_is_open(avctx)) {
2803 FramePool *pool = avctx->internal->pool;
2805 if (CONFIG_FRAME_THREAD_ENCODER &&
2806 avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
2807 ff_frame_thread_encoder_free(avctx);
2809 if (HAVE_THREADS && avctx->internal->thread_ctx)
2810 ff_thread_free(avctx);
2811 if (avctx->codec && avctx->codec->close)
2812 avctx->codec->close(avctx);
2813 avctx->coded_frame = NULL;
2814 avctx->internal->byte_buffer_size = 0;
2815 av_freep(&avctx->internal->byte_buffer);
2816 av_frame_free(&avctx->internal->to_free);
2817 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
2818 av_buffer_pool_uninit(&pool->pools[i]);
2819 av_freep(&avctx->internal->pool);
2821 if (avctx->hwaccel && avctx->hwaccel->uninit)
2822 avctx->hwaccel->uninit(avctx);
2823 av_freep(&avctx->internal->hwaccel_priv_data);
2825 av_freep(&avctx->internal);
2828 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
2829 av_opt_free(avctx->priv_data);
2831 av_freep(&avctx->priv_data);
2832 if (av_codec_is_encoder(avctx->codec))
2833 av_freep(&avctx->extradata);
2834 avctx->codec = NULL;
2835 avctx->active_thread_type = 0;
2840 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
2843 //This is for future deprecatec codec ids, its empty since
2844 //last major bump but will fill up again over time, please don't remove it
2845 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
2846 case AV_CODEC_ID_BRENDER_PIX_DEPRECATED : return AV_CODEC_ID_BRENDER_PIX;
2847 case AV_CODEC_ID_OPUS_DEPRECATED : return AV_CODEC_ID_OPUS;
2848 case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
2849 case AV_CODEC_ID_PAF_AUDIO_DEPRECATED : return AV_CODEC_ID_PAF_AUDIO;
2850 case AV_CODEC_ID_PCM_S24LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S24LE_PLANAR;
2851 case AV_CODEC_ID_PCM_S32LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S32LE_PLANAR;
2852 case AV_CODEC_ID_ADPCM_VIMA_DEPRECATED : return AV_CODEC_ID_ADPCM_VIMA;
2853 case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130;
2854 case AV_CODEC_ID_EXR_DEPRECATED : return AV_CODEC_ID_EXR;
2855 case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M;
2856 case AV_CODEC_ID_PAF_VIDEO_DEPRECATED : return AV_CODEC_ID_PAF_VIDEO;
2857 case AV_CODEC_ID_WEBP_DEPRECATED : return AV_CODEC_ID_WEBP;
2858 case AV_CODEC_ID_HEVC_DEPRECATED : return AV_CODEC_ID_HEVC;
2859 case AV_CODEC_ID_MVC1_DEPRECATED : return AV_CODEC_ID_MVC1;
2860 case AV_CODEC_ID_MVC2_DEPRECATED : return AV_CODEC_ID_MVC2;
2861 case AV_CODEC_ID_SANM_DEPRECATED : return AV_CODEC_ID_SANM;
2862 case AV_CODEC_ID_SGIRLE_DEPRECATED : return AV_CODEC_ID_SGIRLE;
2863 case AV_CODEC_ID_VP7_DEPRECATED : return AV_CODEC_ID_VP7;
2864 default : return id;
2868 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
2870 AVCodec *p, *experimental = NULL;
2872 id= remap_deprecated_codec_id(id);
2874 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
2876 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
2883 return experimental;
2886 AVCodec *avcodec_find_encoder(enum AVCodecID id)
2888 return find_encdec(id, 1);
2891 AVCodec *avcodec_find_encoder_by_name(const char *name)
2898 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2905 AVCodec *avcodec_find_decoder(enum AVCodecID id)
2907 return find_encdec(id, 0);
2910 AVCodec *avcodec_find_decoder_by_name(const char *name)
2917 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2924 const char *avcodec_get_name(enum AVCodecID id)
2926 const AVCodecDescriptor *cd;
2929 if (id == AV_CODEC_ID_NONE)
2931 cd = avcodec_descriptor_get(id);
2934 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2935 codec = avcodec_find_decoder(id);
2938 codec = avcodec_find_encoder(id);
2941 return "unknown_codec";
2944 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2946 int i, len, ret = 0;
2948 #define TAG_PRINT(x) \
2949 (((x) >= '0' && (x) <= '9') || \
2950 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2951 ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2953 for (i = 0; i < 4; i++) {
2954 len = snprintf(buf, buf_size,
2955 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
2957 buf_size = buf_size > len ? buf_size - len : 0;
2964 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2966 const char *codec_type;
2967 const char *codec_name;
2968 const char *profile = NULL;
2972 AVRational display_aspect_ratio;
2973 const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
2975 if (!buf || buf_size <= 0)
2977 codec_type = av_get_media_type_string(enc->codec_type);
2978 codec_name = avcodec_get_name(enc->codec_id);
2979 if (enc->profile != FF_PROFILE_UNKNOWN) {
2983 p = encode ? avcodec_find_encoder(enc->codec_id) :
2984 avcodec_find_decoder(enc->codec_id);
2986 profile = av_get_profile_name(p, enc->profile);
2989 snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
2991 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2993 if (enc->codec && strcmp(enc->codec->name, codec_name))
2994 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
2997 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
2999 if (enc->codec_tag) {
3001 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
3002 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3003 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
3006 switch (enc->codec_type) {
3007 case AVMEDIA_TYPE_VIDEO:
3008 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
3009 char detail[256] = "(";
3011 av_strlcat(buf, separator, buf_size);
3013 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3014 "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
3015 av_get_pix_fmt_name(enc->pix_fmt));
3016 if (enc->bits_per_raw_sample &&
3017 enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
3018 av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
3019 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
3020 av_strlcatf(detail, sizeof(detail), "%s, ",
3021 av_color_range_name(enc->color_range));
3023 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
3024 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
3025 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
3026 if (enc->colorspace != enc->color_primaries ||
3027 enc->colorspace != enc->color_trc) {
3029 av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
3030 av_color_space_name(enc->colorspace),
3031 av_color_primaries_name(enc->color_primaries),
3032 av_color_transfer_name(enc->color_trc));
3034 av_strlcatf(detail, sizeof(detail), "%s, ",
3035 av_get_colorspace_name(enc->colorspace));
3038 if (av_log_get_level() >= AV_LOG_DEBUG &&
3039 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
3040 av_strlcatf(detail, sizeof(detail), "%s, ",
3041 av_chroma_location_name(enc->chroma_sample_location));
3043 if (strlen(detail) > 1) {
3044 detail[strlen(detail) - 2] = 0;
3045 av_strlcatf(buf, buf_size, "%s)", detail);
3050 av_strlcat(buf, new_line ? separator : ", ", buf_size);
3052 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3054 enc->width, enc->height);
3056 if (av_log_get_level() >= AV_LOG_VERBOSE &&
3057 (enc->width != enc->coded_width ||
3058 enc->height != enc->coded_height))
3059 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3060 " (%dx%d)", enc->coded_width, enc->coded_height);
3062 if (enc->sample_aspect_ratio.num) {
3063 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
3064 enc->width * enc->sample_aspect_ratio.num,
3065 enc->height * enc->sample_aspect_ratio.den,
3067 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3068 " [SAR %d:%d DAR %d:%d]",
3069 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
3070 display_aspect_ratio.num, display_aspect_ratio.den);
3072 if (av_log_get_level() >= AV_LOG_DEBUG) {
3073 int g = av_gcd(enc->time_base.num, enc->time_base.den);
3074 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3076 enc->time_base.num / g, enc->time_base.den / g);
3080 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3081 ", q=%d-%d", enc->qmin, enc->qmax);
3084 case AVMEDIA_TYPE_AUDIO:
3085 av_strlcat(buf, separator, buf_size);
3087 if (enc->sample_rate) {
3088 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3089 "%d Hz, ", enc->sample_rate);
3091 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
3092 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
3093 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3094 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
3096 if ( enc->bits_per_raw_sample > 0
3097 && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
3098 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3099 " (%d bit)", enc->bits_per_raw_sample);
3101 case AVMEDIA_TYPE_DATA:
3102 if (av_log_get_level() >= AV_LOG_DEBUG) {
3103 int g = av_gcd(enc->time_base.num, enc->time_base.den);
3105 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3107 enc->time_base.num / g, enc->time_base.den / g);
3110 case AVMEDIA_TYPE_SUBTITLE:
3112 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3113 ", %dx%d", enc->width, enc->height);
3119 if (enc->flags & CODEC_FLAG_PASS1)
3120 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3122 if (enc->flags & CODEC_FLAG_PASS2)
3123 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3126 bitrate = get_bit_rate(enc);
3128 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3129 ", %d kb/s", bitrate / 1000);
3130 } else if (enc->rc_max_rate > 0) {
3131 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3132 ", max. %d kb/s", enc->rc_max_rate / 1000);
3136 const char *av_get_profile_name(const AVCodec *codec, int profile)
3139 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
3142 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
3143 if (p->profile == profile)
3149 unsigned avcodec_version(void)
3151 // av_assert0(AV_CODEC_ID_V410==164);
3152 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
3153 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
3154 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
3155 av_assert0(AV_CODEC_ID_SRT==94216);
3156 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
3158 av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
3159 av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
3160 av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
3161 av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
3162 av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
3163 return LIBAVCODEC_VERSION_INT;
3166 const char *avcodec_configuration(void)
3168 return FFMPEG_CONFIGURATION;
3171 const char *avcodec_license(void)
3173 #define LICENSE_PREFIX "libavcodec license: "
3174 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
3177 void avcodec_flush_buffers(AVCodecContext *avctx)
3179 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
3180 ff_thread_flush(avctx);
3181 else if (avctx->codec->flush)
3182 avctx->codec->flush(avctx);
3184 avctx->pts_correction_last_pts =
3185 avctx->pts_correction_last_dts = INT64_MIN;
3187 if (!avctx->refcounted_frames)
3188 av_frame_unref(avctx->internal->to_free);
3191 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
3194 case AV_CODEC_ID_8SVX_EXP:
3195 case AV_CODEC_ID_8SVX_FIB:
3196 case AV_CODEC_ID_ADPCM_CT:
3197 case AV_CODEC_ID_ADPCM_IMA_APC:
3198 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
3199 case AV_CODEC_ID_ADPCM_IMA_OKI:
3200 case AV_CODEC_ID_ADPCM_IMA_WS:
3201 case AV_CODEC_ID_ADPCM_G722:
3202 case AV_CODEC_ID_ADPCM_YAMAHA:
3204 case AV_CODEC_ID_DSD_LSBF:
3205 case AV_CODEC_ID_DSD_MSBF:
3206 case AV_CODEC_ID_DSD_LSBF_PLANAR:
3207 case AV_CODEC_ID_DSD_MSBF_PLANAR:
3208 case AV_CODEC_ID_PCM_ALAW:
3209 case AV_CODEC_ID_PCM_MULAW:
3210 case AV_CODEC_ID_PCM_S8:
3211 case AV_CODEC_ID_PCM_S8_PLANAR:
3212 case AV_CODEC_ID_PCM_U8:
3213 case AV_CODEC_ID_PCM_ZORK:
3215 case AV_CODEC_ID_PCM_S16BE:
3216 case AV_CODEC_ID_PCM_S16BE_PLANAR:
3217 case AV_CODEC_ID_PCM_S16LE:
3218 case AV_CODEC_ID_PCM_S16LE_PLANAR:
3219 case AV_CODEC_ID_PCM_U16BE:
3220 case AV_CODEC_ID_PCM_U16LE:
3222 case AV_CODEC_ID_PCM_S24DAUD:
3223 case AV_CODEC_ID_PCM_S24BE:
3224 case AV_CODEC_ID_PCM_S24LE:
3225 case AV_CODEC_ID_PCM_S24LE_PLANAR:
3226 case AV_CODEC_ID_PCM_U24BE:
3227 case AV_CODEC_ID_PCM_U24LE:
3229 case AV_CODEC_ID_PCM_S32BE:
3230 case AV_CODEC_ID_PCM_S32LE:
3231 case AV_CODEC_ID_PCM_S32LE_PLANAR:
3232 case AV_CODEC_ID_PCM_U32BE:
3233 case AV_CODEC_ID_PCM_U32LE:
3234 case AV_CODEC_ID_PCM_F32BE:
3235 case AV_CODEC_ID_PCM_F32LE:
3237 case AV_CODEC_ID_PCM_F64BE:
3238 case AV_CODEC_ID_PCM_F64LE:
3245 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
3247 static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
3248 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
3249 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
3250 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
3251 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
3252 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
3253 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
3254 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
3255 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
3256 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
3257 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
3259 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
3260 return AV_CODEC_ID_NONE;
3261 if (be < 0 || be > 1)
3263 return map[fmt][be];
3266 int av_get_bits_per_sample(enum AVCodecID codec_id)
3269 case AV_CODEC_ID_ADPCM_SBPRO_2:
3271 case AV_CODEC_ID_ADPCM_SBPRO_3:
3273 case AV_CODEC_ID_ADPCM_SBPRO_4:
3274 case AV_CODEC_ID_ADPCM_IMA_WAV:
3275 case AV_CODEC_ID_ADPCM_IMA_QT:
3276 case AV_CODEC_ID_ADPCM_SWF:
3277 case AV_CODEC_ID_ADPCM_MS:
3280 return av_get_exact_bits_per_sample(codec_id);
3284 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
3286 int id, sr, ch, ba, tag, bps;
3288 id = avctx->codec_id;
3289 sr = avctx->sample_rate;
3290 ch = avctx->channels;
3291 ba = avctx->block_align;
3292 tag = avctx->codec_tag;
3293 bps = av_get_exact_bits_per_sample(avctx->codec_id);
3295 /* codecs with an exact constant bits per sample */
3296 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
3297 return (frame_bytes * 8LL) / (bps * ch);
3298 bps = avctx->bits_per_coded_sample;
3300 /* codecs with a fixed packet duration */
3302 case AV_CODEC_ID_ADPCM_ADX: return 32;
3303 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
3304 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
3305 case AV_CODEC_ID_AMR_NB:
3306 case AV_CODEC_ID_EVRC:
3307 case AV_CODEC_ID_GSM:
3308 case AV_CODEC_ID_QCELP:
3309 case AV_CODEC_ID_RA_288: return 160;
3310 case AV_CODEC_ID_AMR_WB:
3311 case AV_CODEC_ID_GSM_MS: return 320;
3312 case AV_CODEC_ID_MP1: return 384;
3313 case AV_CODEC_ID_ATRAC1: return 512;
3314 case AV_CODEC_ID_ATRAC3: return 1024;
3315 case AV_CODEC_ID_MP2:
3316 case AV_CODEC_ID_MUSEPACK7: return 1152;
3317 case AV_CODEC_ID_AC3: return 1536;
3321 /* calc from sample rate */
3322 if (id == AV_CODEC_ID_TTA)
3323 return 256 * sr / 245;
3326 /* calc from sample rate and channels */
3327 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
3328 return (480 << (sr / 22050)) / ch;
3333 /* calc from block_align */
3334 if (id == AV_CODEC_ID_SIPR) {
3336 case 20: return 160;
3337 case 19: return 144;
3338 case 29: return 288;
3339 case 37: return 480;
3341 } else if (id == AV_CODEC_ID_ILBC) {
3343 case 38: return 160;
3344 case 50: return 240;
3349 if (frame_bytes > 0) {
3350 /* calc from frame_bytes only */
3351 if (id == AV_CODEC_ID_TRUESPEECH)
3352 return 240 * (frame_bytes / 32);
3353 if (id == AV_CODEC_ID_NELLYMOSER)
3354 return 256 * (frame_bytes / 64);
3355 if (id == AV_CODEC_ID_RA_144)
3356 return 160 * (frame_bytes / 20);
3357 if (id == AV_CODEC_ID_G723_1)
3358 return 240 * (frame_bytes / 24);
3361 /* calc from frame_bytes and bits_per_coded_sample */
3362 if (id == AV_CODEC_ID_ADPCM_G726)
3363 return frame_bytes * 8 / bps;
3367 /* calc from frame_bytes and channels */
3369 case AV_CODEC_ID_ADPCM_AFC:
3370 return frame_bytes / (9 * ch) * 16;
3371 case AV_CODEC_ID_ADPCM_DTK:
3372 return frame_bytes / (16 * ch) * 28;
3373 case AV_CODEC_ID_ADPCM_4XM:
3374 case AV_CODEC_ID_ADPCM_IMA_ISS:
3375 return (frame_bytes - 4 * ch) * 2 / ch;
3376 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
3377 return (frame_bytes - 4) * 2 / ch;
3378 case AV_CODEC_ID_ADPCM_IMA_AMV:
3379 return (frame_bytes - 8) * 2 / ch;
3380 case AV_CODEC_ID_ADPCM_XA:
3381 return (frame_bytes / 128) * 224 / ch;
3382 case AV_CODEC_ID_INTERPLAY_DPCM:
3383 return (frame_bytes - 6 - ch) / ch;
3384 case AV_CODEC_ID_ROQ_DPCM:
3385 return (frame_bytes - 8) / ch;
3386 case AV_CODEC_ID_XAN_DPCM:
3387 return (frame_bytes - 2 * ch) / ch;
3388 case AV_CODEC_ID_MACE3:
3389 return 3 * frame_bytes / ch;
3390 case AV_CODEC_ID_MACE6:
3391 return 6 * frame_bytes / ch;
3392 case AV_CODEC_ID_PCM_LXF:
3393 return 2 * (frame_bytes / (5 * ch));
3394 case AV_CODEC_ID_IAC:
3395 case AV_CODEC_ID_IMC:
3396 return 4 * frame_bytes / ch;
3400 /* calc from frame_bytes, channels, and codec_tag */
3401 if (id == AV_CODEC_ID_SOL_DPCM) {
3403 return frame_bytes / ch;
3405 return frame_bytes * 2 / ch;
3410 /* calc from frame_bytes, channels, and block_align */
3411 int blocks = frame_bytes / ba;
3412 switch (avctx->codec_id) {
3413 case AV_CODEC_ID_ADPCM_IMA_WAV:
3414 if (bps < 2 || bps > 5)
3416 return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
3417 case AV_CODEC_ID_ADPCM_IMA_DK3:
3418 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
3419 case AV_CODEC_ID_ADPCM_IMA_DK4:
3420 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
3421 case AV_CODEC_ID_ADPCM_IMA_RAD:
3422 return blocks * ((ba - 4 * ch) * 2 / ch);
3423 case AV_CODEC_ID_ADPCM_MS:
3424 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
3429 /* calc from frame_bytes, channels, and bits_per_coded_sample */
3430 switch (avctx->codec_id) {
3431 case AV_CODEC_ID_PCM_DVD:
3434 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
3435 case AV_CODEC_ID_PCM_BLURAY:
3438 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
3439 case AV_CODEC_ID_S302M:
3440 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
3446 /* Fall back on using frame_size */
3447 if (avctx->frame_size > 1 && frame_bytes)
3448 return avctx->frame_size;
3450 //For WMA we currently have no other means to calculate duration thus we
3451 //do it here by assuming CBR, which is true for all known cases.
3452 if (avctx->bit_rate>0 && frame_bytes>0 && avctx->sample_rate>0 && avctx->block_align>1) {
3453 if (avctx->codec_id == AV_CODEC_ID_WMAV1 || avctx->codec_id == AV_CODEC_ID_WMAV2)
3454 return (frame_bytes * 8LL * avctx->sample_rate) / avctx->bit_rate;
3461 int ff_thread_init(AVCodecContext *s)
3468 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
3482 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
3485 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
3489 #if FF_API_MISSING_SAMPLE
3490 FF_DISABLE_DEPRECATION_WARNINGS
3491 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
3493 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
3494 "version to the newest one from Git. If the problem still "
3495 "occurs, it means that your file has a feature which has not "
3496 "been implemented.\n", feature);
3498 av_log_ask_for_sample(avc, NULL);
3501 void av_log_ask_for_sample(void *avc, const char *msg, ...)
3503 va_list argument_list;
3505 va_start(argument_list, msg);
3508 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
3509 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
3510 "of this file to ftp://upload.ffmpeg.org/incoming/ "
3511 "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
3513 va_end(argument_list);
3515 FF_ENABLE_DEPRECATION_WARNINGS
3516 #endif /* FF_API_MISSING_SAMPLE */
3518 static AVHWAccel *first_hwaccel = NULL;
3519 static AVHWAccel **last_hwaccel = &first_hwaccel;
3521 void av_register_hwaccel(AVHWAccel *hwaccel)
3523 AVHWAccel **p = last_hwaccel;
3524 hwaccel->next = NULL;
3525 while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
3527 last_hwaccel = &hwaccel->next;
3530 AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
3532 return hwaccel ? hwaccel->next : first_hwaccel;
3535 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
3538 // There is no good way to rollback a failure to destroy the
3539 // mutex, so we ignore failures.
3540 lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
3541 lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
3544 avformat_mutex = NULL;
3548 void *new_codec_mutex = NULL;
3549 void *new_avformat_mutex = NULL;
3551 if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
3552 return err > 0 ? AVERROR_UNKNOWN : err;
3554 if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
3555 // Ignore failures to destroy the newly created mutex.
3556 cb(&new_codec_mutex, AV_LOCK_DESTROY);
3557 return err > 0 ? AVERROR_UNKNOWN : err;
3560 codec_mutex = new_codec_mutex;
3561 avformat_mutex = new_avformat_mutex;
3567 int ff_lock_avcodec(AVCodecContext *log_ctx)
3570 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
3573 entangled_thread_counter++;
3574 if (entangled_thread_counter != 1) {
3575 av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
3577 av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
3578 ff_avcodec_locked = 1;
3579 ff_unlock_avcodec();
3580 return AVERROR(EINVAL);
3582 av_assert0(!ff_avcodec_locked);
3583 ff_avcodec_locked = 1;
3587 int ff_unlock_avcodec(void)
3589 av_assert0(ff_avcodec_locked);
3590 ff_avcodec_locked = 0;
3591 entangled_thread_counter--;
3593 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
3600 int avpriv_lock_avformat(void)
3603 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
3609 int avpriv_unlock_avformat(void)
3612 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
3618 unsigned int avpriv_toupper4(unsigned int x)
3620 return av_toupper(x & 0xFF) +
3621 (av_toupper((x >> 8) & 0xFF) << 8) +
3622 (av_toupper((x >> 16) & 0xFF) << 16) +
3623 ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
3626 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
3630 dst->owner = src->owner;
3632 ret = av_frame_ref(dst->f, src->f);
3636 if (src->progress &&
3637 !(dst->progress = av_buffer_ref(src->progress))) {
3638 ff_thread_release_buffer(dst->owner, dst);
3639 return AVERROR(ENOMEM);
3647 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
3649 return ff_get_format(avctx, fmt);
3652 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
3655 return ff_get_buffer(avctx, f->f, flags);
3658 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
3661 av_frame_unref(f->f);
3664 void ff_thread_finish_setup(AVCodecContext *avctx)
3668 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
3672 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
3676 int ff_thread_can_start_frame(AVCodecContext *avctx)
3681 int ff_alloc_entries(AVCodecContext *avctx, int count)
3686 void ff_reset_entries(AVCodecContext *avctx)
3690 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
3694 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
3700 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
3702 AVCodec *c= avcodec_find_decoder(codec_id);
3704 c= avcodec_find_encoder(codec_id);
3708 if (codec_id <= AV_CODEC_ID_NONE)
3709 return AVMEDIA_TYPE_UNKNOWN;
3710 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
3711 return AVMEDIA_TYPE_VIDEO;
3712 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
3713 return AVMEDIA_TYPE_AUDIO;
3714 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
3715 return AVMEDIA_TYPE_SUBTITLE;
3717 return AVMEDIA_TYPE_UNKNOWN;
3720 int avcodec_is_open(AVCodecContext *s)
3722 return !!s->internal;
3725 int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
3730 ret = av_bprint_finalize(buf, &str);
3733 avctx->extradata = str;
3734 /* Note: the string is NUL terminated (so extradata can be read as a
3735 * string), but the ending character is not accounted in the size (in
3736 * binary formats you are likely not supposed to mux that character). When
3737 * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
3739 avctx->extradata_size = buf->len;
3743 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
3745 uint32_t *av_restrict state)
3749 av_assert0(p <= end);
3753 for (i = 0; i < 3; i++) {
3754 uint32_t tmp = *state << 8;
3755 *state = tmp + *(p++);
3756 if (tmp == 0x100 || p == end)
3761 if (p[-1] > 1 ) p += 3;
3762 else if (p[-2] ) p += 2;
3763 else if (p[-3]|(p[-1]-1)) p++;
3770 p = FFMIN(p, end) - 4;
3771 *state = AV_RB32(p);