3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "libavutil/attributes.h"
30 #include "libavutil/avassert.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/crc.h"
34 #include "libavutil/frame.h"
35 #include "libavutil/internal.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/pixdesc.h"
38 #include "libavutil/imgutils.h"
39 #include "libavutil/samplefmt.h"
40 #include "libavutil/dict.h"
42 #include "libavutil/opt.h"
44 #include "mpegvideo.h"
47 #include "bytestream.h"
54 static int volatile entangled_thread_counter = 0;
55 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op);
56 static void *codec_mutex;
57 static void *avformat_mutex;
59 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
62 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
67 av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
69 memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
72 /* encoder management */
73 static AVCodec *first_avcodec = NULL;
75 AVCodec *av_codec_next(const AVCodec *c)
83 static av_cold void avcodec_init(void)
85 static int initialized = 0;
92 ff_me_cmp_init_static();
95 int av_codec_is_encoder(const AVCodec *codec)
97 return codec && (codec->encode_sub || codec->encode2);
100 int av_codec_is_decoder(const AVCodec *codec)
102 return codec && codec->decode;
105 av_cold void avcodec_register(AVCodec *codec)
115 if (codec->init_static_data)
116 codec->init_static_data(codec);
120 unsigned avcodec_get_edge_width(void)
126 #if FF_API_SET_DIMENSIONS
127 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
129 ff_set_dimensions(s, width, height);
133 int ff_set_dimensions(AVCodecContext *s, int width, int height)
135 int ret = av_image_check_size(width, height, 0, s);
139 s->width = s->coded_width = width;
140 s->height = s->coded_height = height;
145 int ff_set_sar(AVCodecContext *avctx, AVRational sar)
147 int ret = av_image_check_sar(avctx->width, avctx->height, sar);
150 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
152 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
155 avctx->sample_aspect_ratio = sar;
160 int ff_side_data_update_matrix_encoding(AVFrame *frame,
161 enum AVMatrixEncoding matrix_encoding)
163 AVFrameSideData *side_data;
164 enum AVMatrixEncoding *data;
166 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
168 side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
169 sizeof(enum AVMatrixEncoding));
172 return AVERROR(ENOMEM);
174 data = (enum AVMatrixEncoding*)side_data->data;
175 *data = matrix_encoding;
180 #if HAVE_SIMD_ALIGN_16
181 # define STRIDE_ALIGN 16
183 # define STRIDE_ALIGN 8
186 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
187 int linesize_align[AV_NUM_DATA_POINTERS])
193 switch (s->pix_fmt) {
194 case AV_PIX_FMT_YUV420P:
195 case AV_PIX_FMT_YUYV422:
196 case AV_PIX_FMT_YVYU422:
197 case AV_PIX_FMT_UYVY422:
198 case AV_PIX_FMT_YUV422P:
199 case AV_PIX_FMT_YUV440P:
200 case AV_PIX_FMT_YUV444P:
201 case AV_PIX_FMT_GBRP:
202 case AV_PIX_FMT_GRAY8:
203 case AV_PIX_FMT_GRAY16BE:
204 case AV_PIX_FMT_GRAY16LE:
205 case AV_PIX_FMT_YUVJ420P:
206 case AV_PIX_FMT_YUVJ422P:
207 case AV_PIX_FMT_YUVJ440P:
208 case AV_PIX_FMT_YUVJ444P:
209 case AV_PIX_FMT_YUVA420P:
210 case AV_PIX_FMT_YUVA422P:
211 case AV_PIX_FMT_YUVA444P:
212 case AV_PIX_FMT_YUV420P9LE:
213 case AV_PIX_FMT_YUV420P9BE:
214 case AV_PIX_FMT_YUV420P10LE:
215 case AV_PIX_FMT_YUV420P10BE:
216 case AV_PIX_FMT_YUV422P9LE:
217 case AV_PIX_FMT_YUV422P9BE:
218 case AV_PIX_FMT_YUV422P10LE:
219 case AV_PIX_FMT_YUV422P10BE:
220 case AV_PIX_FMT_YUVA422P10LE:
221 case AV_PIX_FMT_YUVA422P10BE:
222 case AV_PIX_FMT_YUV444P9LE:
223 case AV_PIX_FMT_YUV444P9BE:
224 case AV_PIX_FMT_YUV444P10LE:
225 case AV_PIX_FMT_YUV444P10BE:
226 case AV_PIX_FMT_YUVA444P10LE:
227 case AV_PIX_FMT_YUVA444P10BE:
228 case AV_PIX_FMT_GBRP9LE:
229 case AV_PIX_FMT_GBRP9BE:
230 case AV_PIX_FMT_GBRP10LE:
231 case AV_PIX_FMT_GBRP10BE:
232 w_align = 16; //FIXME assume 16 pixel per macroblock
233 h_align = 16 * 2; // interlaced needs 2 macroblocks height
235 case AV_PIX_FMT_YUV411P:
236 case AV_PIX_FMT_UYYVYY411:
240 case AV_PIX_FMT_YUV410P:
241 if (s->codec_id == AV_CODEC_ID_SVQ1) {
245 case AV_PIX_FMT_RGB555:
246 if (s->codec_id == AV_CODEC_ID_RPZA) {
250 case AV_PIX_FMT_PAL8:
251 case AV_PIX_FMT_BGR8:
252 case AV_PIX_FMT_RGB8:
253 if (s->codec_id == AV_CODEC_ID_SMC) {
258 case AV_PIX_FMT_BGR24:
259 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
260 (s->codec_id == AV_CODEC_ID_ZLIB)) {
271 *width = FFALIGN(*width, w_align);
272 *height = FFALIGN(*height, h_align);
273 if (s->codec_id == AV_CODEC_ID_H264)
274 // some of the optimized chroma MC reads one line too much
277 for (i = 0; i < 4; i++)
278 linesize_align[i] = STRIDE_ALIGN;
281 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
283 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
284 int chroma_shift = desc->log2_chroma_w;
285 int linesize_align[AV_NUM_DATA_POINTERS];
288 avcodec_align_dimensions2(s, width, height, linesize_align);
289 align = FFMAX(linesize_align[0], linesize_align[3]);
290 linesize_align[1] <<= chroma_shift;
291 linesize_align[2] <<= chroma_shift;
292 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
293 *width = FFALIGN(*width, align);
296 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
297 enum AVSampleFormat sample_fmt, const uint8_t *buf,
298 int buf_size, int align)
300 int ch, planar, needed_size, ret = 0;
302 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
303 frame->nb_samples, sample_fmt,
305 if (buf_size < needed_size)
306 return AVERROR(EINVAL);
308 planar = av_sample_fmt_is_planar(sample_fmt);
309 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
310 if (!(frame->extended_data = av_mallocz(nb_channels *
311 sizeof(*frame->extended_data))))
312 return AVERROR(ENOMEM);
314 frame->extended_data = frame->data;
317 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
318 buf, nb_channels, frame->nb_samples,
319 sample_fmt, align)) < 0) {
320 if (frame->extended_data != frame->data)
321 av_free(frame->extended_data);
324 if (frame->extended_data != frame->data) {
325 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
326 frame->data[ch] = frame->extended_data[ch];
332 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
334 FramePool *pool = avctx->internal->pool;
337 switch (avctx->codec_type) {
338 case AVMEDIA_TYPE_VIDEO: {
341 int w = frame->width;
342 int h = frame->height;
343 int tmpsize, unaligned;
345 if (pool->format == frame->format &&
346 pool->width == frame->width && pool->height == frame->height)
349 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
352 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
353 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
354 av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
355 // increase alignment of w for next try (rhs gives the lowest bit set in w)
359 for (i = 0; i < 4; i++)
360 unaligned |= picture.linesize[i] % pool->stride_align[i];
363 tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
364 NULL, picture.linesize);
368 for (i = 0; i < 3 && picture.data[i + 1]; i++)
369 size[i] = picture.data[i + 1] - picture.data[i];
370 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
372 for (i = 0; i < 4; i++) {
373 av_buffer_pool_uninit(&pool->pools[i]);
374 pool->linesize[i] = picture.linesize[i];
376 pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
377 if (!pool->pools[i]) {
378 ret = AVERROR(ENOMEM);
383 pool->format = frame->format;
384 pool->width = frame->width;
385 pool->height = frame->height;
389 case AVMEDIA_TYPE_AUDIO: {
390 int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
391 int planar = av_sample_fmt_is_planar(frame->format);
392 int planes = planar ? ch : 1;
394 if (pool->format == frame->format && pool->planes == planes &&
395 pool->channels == ch && frame->nb_samples == pool->samples)
398 av_buffer_pool_uninit(&pool->pools[0]);
399 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
400 frame->nb_samples, frame->format, 0);
404 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
405 if (!pool->pools[0]) {
406 ret = AVERROR(ENOMEM);
410 pool->format = frame->format;
411 pool->planes = planes;
413 pool->samples = frame->nb_samples;
416 default: av_assert0(0);
420 for (i = 0; i < 4; i++)
421 av_buffer_pool_uninit(&pool->pools[i]);
423 pool->planes = pool->channels = pool->samples = 0;
424 pool->width = pool->height = 0;
428 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
430 FramePool *pool = avctx->internal->pool;
431 int planes = pool->planes;
434 frame->linesize[0] = pool->linesize[0];
436 if (planes > AV_NUM_DATA_POINTERS) {
437 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
438 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
439 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
440 sizeof(*frame->extended_buf));
441 if (!frame->extended_data || !frame->extended_buf) {
442 av_freep(&frame->extended_data);
443 av_freep(&frame->extended_buf);
444 return AVERROR(ENOMEM);
447 frame->extended_data = frame->data;
449 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
450 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
453 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
455 for (i = 0; i < frame->nb_extended_buf; i++) {
456 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
457 if (!frame->extended_buf[i])
459 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
462 if (avctx->debug & FF_DEBUG_BUFFERS)
463 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
467 av_frame_unref(frame);
468 return AVERROR(ENOMEM);
471 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
473 FramePool *pool = s->internal->pool;
477 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
481 memset(pic->data, 0, sizeof(pic->data));
482 pic->extended_data = pic->data;
484 for (i = 0; i < 4 && pool->pools[i]; i++) {
485 pic->linesize[i] = pool->linesize[i];
487 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
491 pic->data[i] = pic->buf[i]->data;
493 for (; i < AV_NUM_DATA_POINTERS; i++) {
495 pic->linesize[i] = 0;
497 if (pic->data[1] && !pic->data[2])
498 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
500 if (s->debug & FF_DEBUG_BUFFERS)
501 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
506 return AVERROR(ENOMEM);
509 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
513 if ((ret = update_frame_pool(avctx, frame)) < 0)
516 #if FF_API_GET_BUFFER
517 FF_DISABLE_DEPRECATION_WARNINGS
518 frame->type = FF_BUFFER_TYPE_INTERNAL;
519 FF_ENABLE_DEPRECATION_WARNINGS
522 switch (avctx->codec_type) {
523 case AVMEDIA_TYPE_VIDEO:
524 return video_get_buffer(avctx, frame);
525 case AVMEDIA_TYPE_AUDIO:
526 return audio_get_buffer(avctx, frame);
532 #if FF_API_GET_BUFFER
533 FF_DISABLE_DEPRECATION_WARNINGS
534 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
536 return avcodec_default_get_buffer2(avctx, frame, 0);
539 typedef struct CompatReleaseBufPriv {
540 AVCodecContext avctx;
542 } CompatReleaseBufPriv;
544 static void compat_free_buffer(void *opaque, uint8_t *data)
546 CompatReleaseBufPriv *priv = opaque;
547 if (priv->avctx.release_buffer)
548 priv->avctx.release_buffer(&priv->avctx, &priv->frame);
552 static void compat_release_buffer(void *opaque, uint8_t *data)
554 AVBufferRef *buf = opaque;
555 av_buffer_unref(&buf);
557 FF_ENABLE_DEPRECATION_WARNINGS
560 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
562 AVPacket *pkt = avctx->internal->pkt;
565 AVFrameSideData *frame_sd;
567 frame->color_primaries = avctx->color_primaries;
568 frame->color_trc = avctx->color_trc;
569 frame->colorspace = avctx->colorspace;
570 frame->color_range = avctx->color_range;
571 frame->chroma_location = avctx->chroma_sample_location;
573 frame->reordered_opaque = avctx->reordered_opaque;
575 frame->pkt_pts = AV_NOPTS_VALUE;
579 frame->pkt_pts = pkt->pts;
581 /* copy the replaygain data to the output frame */
582 packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_REPLAYGAIN, &size);
584 frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_REPLAYGAIN, size);
586 return AVERROR(ENOMEM);
588 memcpy(frame_sd->data, packet_sd, size);
590 /* copy the displaymatrix to the output frame */
591 packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_DISPLAYMATRIX, &size);
593 frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX, size);
595 return AVERROR(ENOMEM);
597 memcpy(frame_sd->data, packet_sd, size);
599 /* copy the stereo3d format to the output frame */
600 packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_STEREO3D, &size);
602 frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_STEREO3D, size);
604 return AVERROR(ENOMEM);
606 memcpy(frame_sd->data, packet_sd, size);
612 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
614 const AVHWAccel *hwaccel = avctx->hwaccel;
615 int override_dimensions = 1;
618 switch (avctx->codec_type) {
619 case AVMEDIA_TYPE_VIDEO:
620 if (frame->width <= 0 || frame->height <= 0) {
621 frame->width = FFMAX(avctx->width, avctx->coded_width);
622 frame->height = FFMAX(avctx->height, avctx->coded_height);
623 override_dimensions = 0;
625 if (frame->format < 0)
626 frame->format = avctx->pix_fmt;
627 if (!frame->sample_aspect_ratio.num)
628 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
630 if (av_image_check_sar(frame->width, frame->height,
631 frame->sample_aspect_ratio) < 0) {
632 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
633 frame->sample_aspect_ratio.num,
634 frame->sample_aspect_ratio.den);
635 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
638 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
641 case AVMEDIA_TYPE_AUDIO:
642 if (!frame->sample_rate)
643 frame->sample_rate = avctx->sample_rate;
644 if (frame->format < 0)
645 frame->format = avctx->sample_fmt;
646 if (!frame->channel_layout) {
647 if (avctx->channel_layout) {
648 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
650 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
652 return AVERROR(EINVAL);
655 frame->channel_layout = avctx->channel_layout;
657 if (avctx->channels > FF_SANE_NB_CHANNELS) {
658 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
660 return AVERROR(ENOSYS);
663 frame->channel_layout = av_get_default_channel_layout(avctx->channels);
664 if (!frame->channel_layout)
665 frame->channel_layout = (1ULL << avctx->channels) - 1;
669 default: return AVERROR(EINVAL);
672 ret = ff_decode_frame_props(avctx, frame);
676 if (hwaccel && hwaccel->alloc_frame) {
677 ret = hwaccel->alloc_frame(avctx, frame);
681 #if FF_API_GET_BUFFER
682 FF_DISABLE_DEPRECATION_WARNINGS
684 * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
685 * We wrap each plane in its own AVBuffer. Each of those has a reference to
686 * a dummy AVBuffer as its private data, unreffing it on free.
687 * When all the planes are freed, the dummy buffer's free callback calls
690 if (avctx->get_buffer) {
691 CompatReleaseBufPriv *priv = NULL;
692 AVBufferRef *dummy_buf = NULL;
695 if (flags & AV_GET_BUFFER_FLAG_REF)
696 frame->reference = 1;
698 ret = avctx->get_buffer(avctx, frame);
702 /* return if the buffers are already set up
703 * this would happen e.g. when a custom get_buffer() calls
704 * avcodec_default_get_buffer
709 priv = av_mallocz(sizeof(*priv));
711 ret = AVERROR(ENOMEM);
714 priv->avctx = *avctx;
715 priv->frame = *frame;
717 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
719 ret = AVERROR(ENOMEM);
723 #define WRAP_PLANE(ref_out, data, data_size) \
725 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
727 ret = AVERROR(ENOMEM); \
730 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
733 av_frame_unref(frame); \
734 ret = AVERROR(ENOMEM); \
739 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
740 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
742 planes = av_pix_fmt_count_planes(frame->format);
743 /* workaround for AVHWAccel plane count of 0, buf[0] is used as
744 check for allocated buffers: make libavcodec happy */
745 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
747 if (!desc || planes <= 0) {
748 ret = AVERROR(EINVAL);
752 for (i = 0; i < planes; i++) {
753 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
754 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
756 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
759 int planar = av_sample_fmt_is_planar(frame->format);
760 planes = planar ? avctx->channels : 1;
762 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
763 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
764 frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
765 frame->nb_extended_buf);
766 if (!frame->extended_buf) {
767 ret = AVERROR(ENOMEM);
772 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
773 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
775 for (i = 0; i < frame->nb_extended_buf; i++)
776 WRAP_PLANE(frame->extended_buf[i],
777 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
781 av_buffer_unref(&dummy_buf);
783 frame->width = avctx->width;
784 frame->height = avctx->height;
789 avctx->release_buffer(avctx, frame);
791 av_buffer_unref(&dummy_buf);
794 FF_ENABLE_DEPRECATION_WARNINGS
797 ret = avctx->get_buffer2(avctx, frame, flags);
800 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
801 frame->width = avctx->width;
802 frame->height = avctx->height;
808 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
813 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
816 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
818 if (av_frame_is_writable(frame))
819 return ff_decode_frame_props(avctx, frame);
821 tmp = av_frame_alloc();
823 return AVERROR(ENOMEM);
825 av_frame_move_ref(tmp, frame);
827 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
833 av_frame_copy(frame, tmp);
839 #if FF_API_GET_BUFFER
840 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
845 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
852 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
856 for (i = 0; i < count; i++) {
857 int r = func(c, (char *)arg + i * size);
864 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
868 for (i = 0; i < count; i++) {
869 int r = func(c, arg, i, 0);
876 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
878 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
879 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
882 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
884 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
889 static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
890 enum AVPixelFormat pix_fmt)
892 AVHWAccel *hwaccel = NULL;
894 while ((hwaccel = av_hwaccel_next(hwaccel)))
895 if (hwaccel->id == codec_id
896 && hwaccel->pix_fmt == pix_fmt)
901 static int setup_hwaccel(AVCodecContext *avctx,
902 const enum AVPixelFormat fmt,
905 AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
909 av_log(avctx, AV_LOG_ERROR,
910 "Could not find an AVHWAccel for the pixel format: %s",
912 return AVERROR(ENOENT);
915 if (hwa->priv_data_size) {
916 avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
917 if (!avctx->internal->hwaccel_priv_data)
918 return AVERROR(ENOMEM);
922 ret = hwa->init(avctx);
924 av_freep(&avctx->internal->hwaccel_priv_data);
929 avctx->hwaccel = hwa;
934 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
936 const AVPixFmtDescriptor *desc;
937 enum AVPixelFormat *choices;
938 enum AVPixelFormat ret;
941 while (fmt[n] != AV_PIX_FMT_NONE)
944 choices = av_malloc_array(n + 1, sizeof(*choices));
946 return AV_PIX_FMT_NONE;
948 memcpy(choices, fmt, (n + 1) * sizeof(*choices));
951 ret = avctx->get_format(avctx, choices);
953 desc = av_pix_fmt_desc_get(ret);
955 ret = AV_PIX_FMT_NONE;
959 if (avctx->hwaccel && avctx->hwaccel->uninit)
960 avctx->hwaccel->uninit(avctx);
961 av_freep(&avctx->internal->hwaccel_priv_data);
962 avctx->hwaccel = NULL;
964 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
967 if (!setup_hwaccel(avctx, ret, desc->name))
970 /* Remove failed hwaccel from choices */
971 for (n = 0; choices[n] != ret; n++)
972 av_assert0(choices[n] != AV_PIX_FMT_NONE);
975 choices[n] = choices[n + 1];
976 while (choices[n++] != AV_PIX_FMT_NONE);
983 #if FF_API_AVFRAME_LAVC
984 void avcodec_get_frame_defaults(AVFrame *frame)
986 if (frame->extended_data != frame->data)
987 av_freep(&frame->extended_data);
989 memset(frame, 0, sizeof(AVFrame));
991 frame->pts = AV_NOPTS_VALUE;
992 frame->key_frame = 1;
993 frame->sample_aspect_ratio = (AVRational) {0, 1 };
994 frame->format = -1; /* unknown */
995 frame->extended_data = frame->data;
998 AVFrame *avcodec_alloc_frame(void)
1000 AVFrame *frame = av_mallocz(sizeof(AVFrame));
1005 FF_DISABLE_DEPRECATION_WARNINGS
1006 avcodec_get_frame_defaults(frame);
1007 FF_ENABLE_DEPRECATION_WARNINGS
1012 void avcodec_free_frame(AVFrame **frame)
1014 av_frame_free(frame);
1018 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1021 AVDictionary *tmp = NULL;
1023 if (avcodec_is_open(avctx))
1026 if ((!codec && !avctx->codec)) {
1027 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
1028 return AVERROR(EINVAL);
1030 if ((codec && avctx->codec && codec != avctx->codec)) {
1031 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
1032 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
1033 return AVERROR(EINVAL);
1036 codec = avctx->codec;
1038 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
1039 return AVERROR(EINVAL);
1042 av_dict_copy(&tmp, *options, 0);
1044 /* If there is a user-supplied mutex locking routine, call it. */
1046 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1050 entangled_thread_counter++;
1051 if (entangled_thread_counter != 1) {
1052 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1057 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
1058 if (!avctx->internal) {
1059 ret = AVERROR(ENOMEM);
1063 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
1064 if (!avctx->internal->pool) {
1065 ret = AVERROR(ENOMEM);
1069 avctx->internal->to_free = av_frame_alloc();
1070 if (!avctx->internal->to_free) {
1071 ret = AVERROR(ENOMEM);
1075 if (codec->priv_data_size > 0) {
1076 if (!avctx->priv_data) {
1077 avctx->priv_data = av_mallocz(codec->priv_data_size);
1078 if (!avctx->priv_data) {
1079 ret = AVERROR(ENOMEM);
1082 if (codec->priv_class) {
1083 *(const AVClass **)avctx->priv_data = codec->priv_class;
1084 av_opt_set_defaults(avctx->priv_data);
1087 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
1090 avctx->priv_data = NULL;
1092 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
1095 if (avctx->coded_width && avctx->coded_height && !avctx->width && !avctx->height)
1096 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
1097 else if (avctx->width && avctx->height)
1098 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1102 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1103 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1104 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1105 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
1106 ff_set_dimensions(avctx, 0, 0);
1109 if (avctx->width > 0 && avctx->height > 0) {
1110 if (av_image_check_sar(avctx->width, avctx->height,
1111 avctx->sample_aspect_ratio) < 0) {
1112 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1113 avctx->sample_aspect_ratio.num,
1114 avctx->sample_aspect_ratio.den);
1115 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
1119 /* if the decoder init function was already called previously,
1120 * free the already allocated subtitle_header before overwriting it */
1121 if (av_codec_is_decoder(codec))
1122 av_freep(&avctx->subtitle_header);
1124 if (avctx->channels > FF_SANE_NB_CHANNELS) {
1125 ret = AVERROR(EINVAL);
1129 avctx->codec = codec;
1130 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1131 avctx->codec_id == AV_CODEC_ID_NONE) {
1132 avctx->codec_type = codec->type;
1133 avctx->codec_id = codec->id;
1135 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1136 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1137 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
1138 ret = AVERROR(EINVAL);
1141 avctx->frame_number = 0;
1143 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1144 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1145 ret = AVERROR_EXPERIMENTAL;
1149 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1150 (!avctx->time_base.num || !avctx->time_base.den)) {
1151 avctx->time_base.num = 1;
1152 avctx->time_base.den = avctx->sample_rate;
1156 ret = ff_thread_init(avctx);
1161 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
1162 avctx->thread_count = 1;
1164 if (av_codec_is_encoder(avctx->codec)) {
1166 if (avctx->codec->sample_fmts) {
1167 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1168 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1170 if (avctx->channels == 1 &&
1171 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1172 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1173 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1177 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1178 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
1179 ret = AVERROR(EINVAL);
1183 if (avctx->codec->pix_fmts) {
1184 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1185 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1187 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
1188 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
1189 ret = AVERROR(EINVAL);
1192 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
1193 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
1194 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
1195 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
1196 avctx->color_range = AVCOL_RANGE_JPEG;
1198 if (avctx->codec->supported_samplerates) {
1199 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1200 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1202 if (avctx->codec->supported_samplerates[i] == 0) {
1203 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
1204 ret = AVERROR(EINVAL);
1208 if (avctx->codec->channel_layouts) {
1209 if (!avctx->channel_layout) {
1210 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
1212 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1213 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1215 if (avctx->codec->channel_layouts[i] == 0) {
1216 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
1217 ret = AVERROR(EINVAL);
1222 if (avctx->channel_layout && avctx->channels) {
1223 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
1224 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
1225 ret = AVERROR(EINVAL);
1228 } else if (avctx->channel_layout) {
1229 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1232 if (!avctx->rc_initial_buffer_occupancy)
1233 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1236 if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
1237 ret = avctx->codec->init(avctx);
1243 #if FF_API_AUDIOENC_DELAY
1244 if (av_codec_is_encoder(avctx->codec))
1245 avctx->delay = avctx->initial_padding;
1248 if (av_codec_is_decoder(avctx->codec)) {
1249 /* validate channel layout from the decoder */
1250 if (avctx->channel_layout) {
1251 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1252 if (!avctx->channels)
1253 avctx->channels = channels;
1254 else if (channels != avctx->channels) {
1255 av_log(avctx, AV_LOG_WARNING,
1256 "channel layout does not match number of channels\n");
1257 avctx->channel_layout = 0;
1260 if (avctx->channels && avctx->channels < 0 ||
1261 avctx->channels > FF_SANE_NB_CHANNELS) {
1262 ret = AVERROR(EINVAL);
1266 #if FF_API_AVCTX_TIMEBASE
1267 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1268 avctx->time_base = av_inv_q(avctx->framerate);
1272 entangled_thread_counter--;
1274 /* Release any user-supplied mutex. */
1276 (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1279 av_dict_free(options);
1286 av_freep(&avctx->priv_data);
1287 if (avctx->internal) {
1288 av_frame_free(&avctx->internal->to_free);
1289 av_freep(&avctx->internal->pool);
1291 av_freep(&avctx->internal);
1292 avctx->codec = NULL;
1296 int ff_alloc_packet(AVPacket *avpkt, int size)
1298 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
1299 return AVERROR(EINVAL);
1302 AVBufferRef *buf = avpkt->buf;
1303 #if FF_API_DESTRUCT_PACKET
1304 FF_DISABLE_DEPRECATION_WARNINGS
1305 void *destruct = avpkt->destruct;
1306 FF_ENABLE_DEPRECATION_WARNINGS
1309 if (avpkt->size < size)
1310 return AVERROR(EINVAL);
1312 av_init_packet(avpkt);
1313 #if FF_API_DESTRUCT_PACKET
1314 FF_DISABLE_DEPRECATION_WARNINGS
1315 avpkt->destruct = destruct;
1316 FF_ENABLE_DEPRECATION_WARNINGS
1322 return av_new_packet(avpkt, size);
1327 * Pad last frame with silence.
1329 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1331 AVFrame *frame = NULL;
1334 if (!(frame = av_frame_alloc()))
1335 return AVERROR(ENOMEM);
1337 frame->format = src->format;
1338 frame->channel_layout = src->channel_layout;
1339 frame->nb_samples = s->frame_size;
1340 ret = av_frame_get_buffer(frame, 32);
1344 ret = av_frame_copy_props(frame, src);
1348 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1349 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1351 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1352 frame->nb_samples - src->nb_samples,
1353 s->channels, s->sample_fmt)) < 0)
1361 av_frame_free(&frame);
1365 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1367 const AVFrame *frame,
1368 int *got_packet_ptr)
1371 AVFrame *padded_frame = NULL;
1373 int user_packet = !!avpkt->data;
1375 *got_packet_ptr = 0;
1377 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1378 av_free_packet(avpkt);
1379 av_init_packet(avpkt);
1383 /* ensure that extended_data is properly set */
1384 if (frame && !frame->extended_data) {
1385 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1386 avctx->channels > AV_NUM_DATA_POINTERS) {
1387 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1388 "with more than %d channels, but extended_data is not set.\n",
1389 AV_NUM_DATA_POINTERS);
1390 return AVERROR(EINVAL);
1392 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1395 tmp.extended_data = tmp.data;
1399 /* check for valid frame size */
1401 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1402 if (frame->nb_samples > avctx->frame_size)
1403 return AVERROR(EINVAL);
1404 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1405 if (frame->nb_samples < avctx->frame_size &&
1406 !avctx->internal->last_audio_frame) {
1407 ret = pad_last_frame(avctx, &padded_frame, frame);
1411 frame = padded_frame;
1412 avctx->internal->last_audio_frame = 1;
1415 if (frame->nb_samples != avctx->frame_size) {
1416 ret = AVERROR(EINVAL);
1422 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1424 if (*got_packet_ptr) {
1425 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1426 if (avpkt->pts == AV_NOPTS_VALUE)
1427 avpkt->pts = frame->pts;
1428 if (!avpkt->duration)
1429 avpkt->duration = ff_samples_to_time_base(avctx,
1432 avpkt->dts = avpkt->pts;
1437 if (!user_packet && avpkt->size) {
1438 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1440 avpkt->data = avpkt->buf->data;
1443 avctx->frame_number++;
1446 if (ret < 0 || !*got_packet_ptr) {
1447 av_free_packet(avpkt);
1448 av_init_packet(avpkt);
1452 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1453 * this needs to be moved to the encoders, but for now we can do it
1454 * here to simplify things */
1455 avpkt->flags |= AV_PKT_FLAG_KEY;
1458 av_frame_free(&padded_frame);
1460 #if FF_API_AUDIOENC_DELAY
1461 avctx->delay = avctx->initial_padding;
1467 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1469 const AVFrame *frame,
1470 int *got_packet_ptr)
1473 int user_packet = !!avpkt->data;
1475 *got_packet_ptr = 0;
1477 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1478 av_free_packet(avpkt);
1479 av_init_packet(avpkt);
1484 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1485 return AVERROR(EINVAL);
1487 av_assert0(avctx->codec->encode2);
1489 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1491 if (!*got_packet_ptr)
1493 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1494 avpkt->pts = avpkt->dts = frame->pts;
1496 if (!user_packet && avpkt->size) {
1497 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1499 avpkt->data = avpkt->buf->data;
1502 avctx->frame_number++;
1505 if (ret < 0 || !*got_packet_ptr)
1506 av_free_packet(avpkt);
1512 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1513 const AVSubtitle *sub)
1516 if (sub->start_display_time) {
1517 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1520 if (sub->num_rects == 0 || !sub->rects)
1522 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1523 avctx->frame_number++;
1527 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1530 const uint8_t *data;
1533 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1537 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
1538 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
1539 "changes, but PARAM_CHANGE side data was sent to it.\n");
1540 return AVERROR(EINVAL);
1546 flags = bytestream_get_le32(&data);
1549 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1552 avctx->channels = bytestream_get_le32(&data);
1555 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1558 avctx->channel_layout = bytestream_get_le64(&data);
1561 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1564 avctx->sample_rate = bytestream_get_le32(&data);
1567 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1570 avctx->width = bytestream_get_le32(&data);
1571 avctx->height = bytestream_get_le32(&data);
1573 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1580 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
1581 return AVERROR_INVALIDDATA;
1584 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
1588 /* move the original frame to our backup */
1589 av_frame_unref(avci->to_free);
1590 av_frame_move_ref(avci->to_free, frame);
1592 /* now copy everything except the AVBufferRefs back
1593 * note that we make a COPY of the side data, so calling av_frame_free() on
1594 * the caller's frame will work properly */
1595 ret = av_frame_copy_props(frame, avci->to_free);
1599 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
1600 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
1601 if (avci->to_free->extended_data != avci->to_free->data) {
1602 int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
1603 int size = planes * sizeof(*frame->extended_data);
1606 av_frame_unref(frame);
1610 frame->extended_data = av_malloc(size);
1611 if (!frame->extended_data) {
1612 av_frame_unref(frame);
1613 return AVERROR(ENOMEM);
1615 memcpy(frame->extended_data, avci->to_free->extended_data,
1618 frame->extended_data = frame->data;
1620 frame->format = avci->to_free->format;
1621 frame->width = avci->to_free->width;
1622 frame->height = avci->to_free->height;
1623 frame->channel_layout = avci->to_free->channel_layout;
1624 frame->nb_samples = avci->to_free->nb_samples;
1629 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1630 int *got_picture_ptr,
1633 AVCodecInternal *avci = avctx->internal;
1636 *got_picture_ptr = 0;
1637 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1640 avctx->internal->pkt = avpkt;
1641 ret = apply_param_change(avctx, avpkt);
1643 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
1644 if (avctx->err_recognition & AV_EF_EXPLODE)
1648 av_frame_unref(picture);
1650 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1651 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1652 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1655 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1657 picture->pkt_dts = avpkt->dts;
1658 /* get_buffer is supposed to set frame parameters */
1659 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
1660 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1661 picture->width = avctx->width;
1662 picture->height = avctx->height;
1663 picture->format = avctx->pix_fmt;
1667 emms_c(); //needed to avoid an emms_c() call before every return;
1669 if (*got_picture_ptr) {
1670 if (!avctx->refcounted_frames) {
1671 int err = unrefcount_frame(avci, picture);
1676 avctx->frame_number++;
1678 av_frame_unref(picture);
1682 #if FF_API_AVCTX_TIMEBASE
1683 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1684 avctx->time_base = av_inv_q(avctx->framerate);
1690 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1695 AVCodecInternal *avci = avctx->internal;
1700 avctx->internal->pkt = avpkt;
1702 if (!avpkt->data && avpkt->size) {
1703 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1704 return AVERROR(EINVAL);
1707 ret = apply_param_change(avctx, avpkt);
1709 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
1710 if (avctx->err_recognition & AV_EF_EXPLODE)
1714 av_frame_unref(frame);
1716 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1717 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1718 if (ret >= 0 && *got_frame_ptr) {
1719 avctx->frame_number++;
1720 frame->pkt_dts = avpkt->dts;
1721 if (frame->format == AV_SAMPLE_FMT_NONE)
1722 frame->format = avctx->sample_fmt;
1724 if (!avctx->refcounted_frames) {
1725 int err = unrefcount_frame(avci, frame);
1730 av_frame_unref(frame);
1737 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1743 avctx->internal->pkt = avpkt;
1745 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1747 avctx->frame_number++;
1751 void avsubtitle_free(AVSubtitle *sub)
1755 for (i = 0; i < sub->num_rects; i++) {
1756 av_freep(&sub->rects[i]->pict.data[0]);
1757 av_freep(&sub->rects[i]->pict.data[1]);
1758 av_freep(&sub->rects[i]->pict.data[2]);
1759 av_freep(&sub->rects[i]->pict.data[3]);
1760 av_freep(&sub->rects[i]->text);
1761 av_freep(&sub->rects[i]->ass);
1762 av_freep(&sub->rects[i]);
1765 av_freep(&sub->rects);
1767 memset(sub, 0, sizeof(AVSubtitle));
1770 av_cold int avcodec_close(AVCodecContext *avctx)
1772 if (avcodec_is_open(avctx)) {
1773 FramePool *pool = avctx->internal->pool;
1775 if (HAVE_THREADS && avctx->internal->thread_ctx)
1776 ff_thread_free(avctx);
1777 if (avctx->codec && avctx->codec->close)
1778 avctx->codec->close(avctx);
1779 avctx->coded_frame = NULL;
1780 av_frame_free(&avctx->internal->to_free);
1781 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
1782 av_buffer_pool_uninit(&pool->pools[i]);
1783 av_freep(&avctx->internal->pool);
1785 if (avctx->hwaccel && avctx->hwaccel->uninit)
1786 avctx->hwaccel->uninit(avctx);
1787 av_freep(&avctx->internal->hwaccel_priv_data);
1789 av_freep(&avctx->internal);
1792 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1793 av_opt_free(avctx->priv_data);
1795 av_freep(&avctx->priv_data);
1796 if (av_codec_is_encoder(avctx->codec))
1797 av_freep(&avctx->extradata);
1798 avctx->codec = NULL;
1799 avctx->active_thread_type = 0;
1804 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1806 AVCodec *p, *experimental = NULL;
1809 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1811 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1818 return experimental;
1821 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1823 return find_encdec(id, 1);
1826 AVCodec *avcodec_find_encoder_by_name(const char *name)
1833 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
1840 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1842 return find_encdec(id, 0);
1845 AVCodec *avcodec_find_decoder_by_name(const char *name)
1852 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
1859 static int get_bit_rate(AVCodecContext *ctx)
1862 int bits_per_sample;
1864 switch (ctx->codec_type) {
1865 case AVMEDIA_TYPE_VIDEO:
1866 case AVMEDIA_TYPE_DATA:
1867 case AVMEDIA_TYPE_SUBTITLE:
1868 case AVMEDIA_TYPE_ATTACHMENT:
1869 bit_rate = ctx->bit_rate;
1871 case AVMEDIA_TYPE_AUDIO:
1872 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1873 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1882 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1884 int i, len, ret = 0;
1886 #define TAG_PRINT(x) \
1887 (((x) >= '0' && (x) <= '9') || \
1888 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
1889 ((x) == '.' || (x) == ' '))
1891 for (i = 0; i < 4; i++) {
1892 len = snprintf(buf, buf_size,
1893 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
1895 buf_size = buf_size > len ? buf_size - len : 0;
1902 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1904 const char *codec_name;
1905 const char *profile = NULL;
1910 AVRational display_aspect_ratio;
1915 p = avcodec_find_encoder(enc->codec_id);
1917 p = avcodec_find_decoder(enc->codec_id);
1920 codec_name = p->name;
1921 profile = av_get_profile_name(p, enc->profile);
1922 } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
1923 /* fake mpeg2 transport stream codec (currently not
1925 codec_name = "mpeg2ts";
1927 /* output avi tags */
1929 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1930 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1934 switch (enc->codec_type) {
1935 case AVMEDIA_TYPE_VIDEO:
1936 snprintf(buf, buf_size,
1938 codec_name, enc->mb_decision ? " (hq)" : "");
1940 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1942 if (enc->codec_tag) {
1944 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1945 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1946 " [%s / 0x%04X]", tag_buf, enc->codec_tag);
1949 av_strlcat(buf, "\n ", buf_size);
1950 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1951 "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
1952 av_get_pix_fmt_name(enc->pix_fmt));
1954 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
1955 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
1956 av_color_range_name(enc->color_range));
1957 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
1958 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
1959 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
1961 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s/%s/%s",
1962 av_color_space_name(enc->colorspace),
1963 av_color_primaries_name(enc->color_primaries),
1964 av_color_transfer_name(enc->color_trc));
1966 if (av_log_get_level() >= AV_LOG_DEBUG &&
1967 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
1968 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
1969 av_chroma_location_name(enc->chroma_sample_location));
1972 av_strlcat(buf, new_line ? "\n " : ", ", buf_size);
1974 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1976 enc->width, enc->height);
1978 if (av_log_get_level() >= AV_LOG_VERBOSE &&
1979 (enc->width != enc->coded_width ||
1980 enc->height != enc->coded_height))
1981 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1982 " (%dx%d)", enc->coded_width, enc->coded_height);
1984 if (enc->sample_aspect_ratio.num) {
1985 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1986 enc->width * enc->sample_aspect_ratio.num,
1987 enc->height * enc->sample_aspect_ratio.den,
1989 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1990 " [PAR %d:%d DAR %d:%d]",
1991 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1992 display_aspect_ratio.num, display_aspect_ratio.den);
1994 if (av_log_get_level() >= AV_LOG_DEBUG) {
1995 int g = av_gcd(enc->time_base.num, enc->time_base.den);
1996 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1998 enc->time_base.num / g, enc->time_base.den / g);
2002 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2003 ", q=%d-%d", enc->qmin, enc->qmax);
2006 case AVMEDIA_TYPE_AUDIO:
2007 snprintf(buf, buf_size,
2011 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2013 if (enc->codec_tag) {
2015 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2016 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2017 " [%s / 0x%04X]", tag_buf, enc->codec_tag);
2020 av_strlcat(buf, "\n ", buf_size);
2021 if (enc->sample_rate) {
2022 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2023 "%d Hz, ", enc->sample_rate);
2025 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2026 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2027 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2028 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2031 case AVMEDIA_TYPE_DATA:
2032 snprintf(buf, buf_size, "Data: %s", codec_name);
2034 case AVMEDIA_TYPE_SUBTITLE:
2035 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
2037 case AVMEDIA_TYPE_ATTACHMENT:
2038 snprintf(buf, buf_size, "Attachment: %s", codec_name);
2041 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
2045 if (enc->flags & CODEC_FLAG_PASS1)
2046 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2048 if (enc->flags & CODEC_FLAG_PASS2)
2049 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2052 bitrate = get_bit_rate(enc);
2054 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2055 ", %d kb/s", bitrate / 1000);
2059 const char *av_get_profile_name(const AVCodec *codec, int profile)
2062 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2065 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2066 if (p->profile == profile)
2072 unsigned avcodec_version(void)
2074 return LIBAVCODEC_VERSION_INT;
2077 const char *avcodec_configuration(void)
2079 return LIBAV_CONFIGURATION;
2082 const char *avcodec_license(void)
2084 #define LICENSE_PREFIX "libavcodec license: "
2085 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2088 void avcodec_flush_buffers(AVCodecContext *avctx)
2090 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2091 ff_thread_flush(avctx);
2092 else if (avctx->codec->flush)
2093 avctx->codec->flush(avctx);
2095 if (!avctx->refcounted_frames)
2096 av_frame_unref(avctx->internal->to_free);
2099 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2102 case AV_CODEC_ID_ADPCM_CT:
2103 case AV_CODEC_ID_ADPCM_IMA_APC:
2104 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2105 case AV_CODEC_ID_ADPCM_IMA_WS:
2106 case AV_CODEC_ID_ADPCM_G722:
2107 case AV_CODEC_ID_ADPCM_YAMAHA:
2109 case AV_CODEC_ID_PCM_ALAW:
2110 case AV_CODEC_ID_PCM_MULAW:
2111 case AV_CODEC_ID_PCM_S8:
2112 case AV_CODEC_ID_PCM_U8:
2113 case AV_CODEC_ID_PCM_ZORK:
2115 case AV_CODEC_ID_PCM_S16BE:
2116 case AV_CODEC_ID_PCM_S16LE:
2117 case AV_CODEC_ID_PCM_S16LE_PLANAR:
2118 case AV_CODEC_ID_PCM_U16BE:
2119 case AV_CODEC_ID_PCM_U16LE:
2121 case AV_CODEC_ID_PCM_S24DAUD:
2122 case AV_CODEC_ID_PCM_S24BE:
2123 case AV_CODEC_ID_PCM_S24LE:
2124 case AV_CODEC_ID_PCM_S24LE_PLANAR:
2125 case AV_CODEC_ID_PCM_U24BE:
2126 case AV_CODEC_ID_PCM_U24LE:
2128 case AV_CODEC_ID_PCM_S32BE:
2129 case AV_CODEC_ID_PCM_S32LE:
2130 case AV_CODEC_ID_PCM_S32LE_PLANAR:
2131 case AV_CODEC_ID_PCM_U32BE:
2132 case AV_CODEC_ID_PCM_U32LE:
2133 case AV_CODEC_ID_PCM_F32BE:
2134 case AV_CODEC_ID_PCM_F32LE:
2136 case AV_CODEC_ID_PCM_F64BE:
2137 case AV_CODEC_ID_PCM_F64LE:
2144 int av_get_bits_per_sample(enum AVCodecID codec_id)
2147 case AV_CODEC_ID_ADPCM_SBPRO_2:
2149 case AV_CODEC_ID_ADPCM_SBPRO_3:
2151 case AV_CODEC_ID_ADPCM_SBPRO_4:
2152 case AV_CODEC_ID_ADPCM_IMA_WAV:
2153 case AV_CODEC_ID_ADPCM_IMA_QT:
2154 case AV_CODEC_ID_ADPCM_SWF:
2155 case AV_CODEC_ID_ADPCM_MS:
2158 return av_get_exact_bits_per_sample(codec_id);
2162 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2164 int id, sr, ch, ba, tag, bps;
2166 id = avctx->codec_id;
2167 sr = avctx->sample_rate;
2168 ch = avctx->channels;
2169 ba = avctx->block_align;
2170 tag = avctx->codec_tag;
2171 bps = av_get_exact_bits_per_sample(avctx->codec_id);
2173 /* codecs with an exact constant bits per sample */
2174 if (bps > 0 && ch > 0 && frame_bytes > 0)
2175 return (frame_bytes * 8) / (bps * ch);
2176 bps = avctx->bits_per_coded_sample;
2178 /* codecs with a fixed packet duration */
2180 case AV_CODEC_ID_ADPCM_ADX: return 32;
2181 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2182 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2183 case AV_CODEC_ID_AMR_NB:
2184 case AV_CODEC_ID_GSM:
2185 case AV_CODEC_ID_QCELP:
2186 case AV_CODEC_ID_RA_144:
2187 case AV_CODEC_ID_RA_288: return 160;
2188 case AV_CODEC_ID_IMC: return 256;
2189 case AV_CODEC_ID_AMR_WB:
2190 case AV_CODEC_ID_GSM_MS: return 320;
2191 case AV_CODEC_ID_MP1: return 384;
2192 case AV_CODEC_ID_ATRAC1: return 512;
2193 case AV_CODEC_ID_ATRAC3: return 1024;
2194 case AV_CODEC_ID_MP2:
2195 case AV_CODEC_ID_MUSEPACK7: return 1152;
2196 case AV_CODEC_ID_AC3: return 1536;
2200 /* calc from sample rate */
2201 if (id == AV_CODEC_ID_TTA)
2202 return 256 * sr / 245;
2205 /* calc from sample rate and channels */
2206 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2207 return (480 << (sr / 22050)) / ch;
2212 /* calc from block_align */
2213 if (id == AV_CODEC_ID_SIPR) {
2215 case 20: return 160;
2216 case 19: return 144;
2217 case 29: return 288;
2218 case 37: return 480;
2220 } else if (id == AV_CODEC_ID_ILBC) {
2222 case 38: return 160;
2223 case 50: return 240;
2228 if (frame_bytes > 0) {
2229 /* calc from frame_bytes only */
2230 if (id == AV_CODEC_ID_TRUESPEECH)
2231 return 240 * (frame_bytes / 32);
2232 if (id == AV_CODEC_ID_NELLYMOSER)
2233 return 256 * (frame_bytes / 64);
2236 /* calc from frame_bytes and bits_per_coded_sample */
2237 if (id == AV_CODEC_ID_ADPCM_G726)
2238 return frame_bytes * 8 / bps;
2242 /* calc from frame_bytes and channels */
2244 case AV_CODEC_ID_ADPCM_4XM:
2245 case AV_CODEC_ID_ADPCM_IMA_ISS:
2246 return (frame_bytes - 4 * ch) * 2 / ch;
2247 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2248 return (frame_bytes - 4) * 2 / ch;
2249 case AV_CODEC_ID_ADPCM_IMA_AMV:
2250 return (frame_bytes - 8) * 2 / ch;
2251 case AV_CODEC_ID_ADPCM_XA:
2252 return (frame_bytes / 128) * 224 / ch;
2253 case AV_CODEC_ID_INTERPLAY_DPCM:
2254 return (frame_bytes - 6 - ch) / ch;
2255 case AV_CODEC_ID_ROQ_DPCM:
2256 return (frame_bytes - 8) / ch;
2257 case AV_CODEC_ID_XAN_DPCM:
2258 return (frame_bytes - 2 * ch) / ch;
2259 case AV_CODEC_ID_MACE3:
2260 return 3 * frame_bytes / ch;
2261 case AV_CODEC_ID_MACE6:
2262 return 6 * frame_bytes / ch;
2263 case AV_CODEC_ID_PCM_LXF:
2264 return 2 * (frame_bytes / (5 * ch));
2268 /* calc from frame_bytes, channels, and codec_tag */
2269 if (id == AV_CODEC_ID_SOL_DPCM) {
2271 return frame_bytes / ch;
2273 return frame_bytes * 2 / ch;
2278 /* calc from frame_bytes, channels, and block_align */
2279 int blocks = frame_bytes / ba;
2280 switch (avctx->codec_id) {
2281 case AV_CODEC_ID_ADPCM_IMA_WAV:
2282 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2283 case AV_CODEC_ID_ADPCM_IMA_DK3:
2284 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2285 case AV_CODEC_ID_ADPCM_IMA_DK4:
2286 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2287 case AV_CODEC_ID_ADPCM_MS:
2288 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2293 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2294 switch (avctx->codec_id) {
2295 case AV_CODEC_ID_PCM_DVD:
2296 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2297 case AV_CODEC_ID_PCM_BLURAY:
2298 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2299 case AV_CODEC_ID_S302M:
2300 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2310 int ff_thread_init(AVCodecContext *s)
2317 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2331 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2334 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2338 #if FF_API_MISSING_SAMPLE
2339 FF_DISABLE_DEPRECATION_WARNINGS
2340 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2342 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
2343 "version to the newest one from Git. If the problem still "
2344 "occurs, it means that your file has a feature which has not "
2345 "been implemented.\n", feature);
2347 av_log_ask_for_sample(avc, NULL);
2350 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2352 va_list argument_list;
2354 va_start(argument_list, msg);
2357 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2358 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2359 "of this file to ftp://upload.libav.org/incoming/ "
2360 "and contact the libav-devel mailing list.\n");
2362 va_end(argument_list);
2364 FF_ENABLE_DEPRECATION_WARNINGS
2365 #endif /* FF_API_MISSING_SAMPLE */
2367 static AVHWAccel *first_hwaccel = NULL;
2369 void av_register_hwaccel(AVHWAccel *hwaccel)
2371 AVHWAccel **p = &first_hwaccel;
2375 hwaccel->next = NULL;
2378 AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
2380 return hwaccel ? hwaccel->next : first_hwaccel;
2383 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2386 // There is no good way to rollback a failure to destroy the
2387 // mutex, so we ignore failures.
2388 lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
2389 lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
2392 avformat_mutex = NULL;
2396 void *new_codec_mutex = NULL;
2397 void *new_avformat_mutex = NULL;
2399 if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
2400 return err > 0 ? AVERROR_UNKNOWN : err;
2402 if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
2403 // Ignore failures to destroy the newly created mutex.
2404 cb(&new_codec_mutex, AV_LOCK_DESTROY);
2405 return err > 0 ? AVERROR_UNKNOWN : err;
2408 codec_mutex = new_codec_mutex;
2409 avformat_mutex = new_avformat_mutex;
2415 int avpriv_lock_avformat(void)
2418 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2424 int avpriv_unlock_avformat(void)
2427 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2433 unsigned int avpriv_toupper4(unsigned int x)
2435 return av_toupper(x & 0xFF) +
2436 (av_toupper((x >> 8) & 0xFF) << 8) +
2437 (av_toupper((x >> 16) & 0xFF) << 16) +
2438 (av_toupper((x >> 24) & 0xFF) << 24);
2441 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
2445 dst->owner = src->owner;
2447 ret = av_frame_ref(dst->f, src->f);
2451 if (src->progress &&
2452 !(dst->progress = av_buffer_ref(src->progress))) {
2453 ff_thread_release_buffer(dst->owner, dst);
2454 return AVERROR(ENOMEM);
2462 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
2465 return ff_get_buffer(avctx, f->f, flags);
2468 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
2471 av_frame_unref(f->f);
2474 void ff_thread_finish_setup(AVCodecContext *avctx)
2478 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
2482 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
2488 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
2490 if (codec_id <= AV_CODEC_ID_NONE)
2491 return AVMEDIA_TYPE_UNKNOWN;
2492 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2493 return AVMEDIA_TYPE_VIDEO;
2494 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2495 return AVMEDIA_TYPE_AUDIO;
2496 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2497 return AVMEDIA_TYPE_SUBTITLE;
2499 return AVMEDIA_TYPE_UNKNOWN;
2502 int avcodec_is_open(AVCodecContext *s)
2504 return !!s->internal;
2507 const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
2509 uint32_t * restrict state)
2517 for (i = 0; i < 3; i++) {
2518 uint32_t tmp = *state << 8;
2519 *state = tmp + *(p++);
2520 if (tmp == 0x100 || p == end)
2525 if (p[-1] > 1 ) p += 3;
2526 else if (p[-2] ) p += 2;
2527 else if (p[-3]|(p[-1]-1)) p++;
2534 p = FFMIN(p, end) - 4;
2535 *state = AV_RB32(p);