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: %d/%d\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_GBRAP:
203 case AV_PIX_FMT_GRAY8:
204 case AV_PIX_FMT_GRAY16BE:
205 case AV_PIX_FMT_GRAY16LE:
206 case AV_PIX_FMT_YUVJ420P:
207 case AV_PIX_FMT_YUVJ422P:
208 case AV_PIX_FMT_YUVJ440P:
209 case AV_PIX_FMT_YUVJ444P:
210 case AV_PIX_FMT_YUVA420P:
211 case AV_PIX_FMT_YUVA422P:
212 case AV_PIX_FMT_YUVA444P:
213 case AV_PIX_FMT_YUV420P9LE:
214 case AV_PIX_FMT_YUV420P9BE:
215 case AV_PIX_FMT_YUV420P10LE:
216 case AV_PIX_FMT_YUV420P10BE:
217 case AV_PIX_FMT_YUV422P9LE:
218 case AV_PIX_FMT_YUV422P9BE:
219 case AV_PIX_FMT_YUV422P10LE:
220 case AV_PIX_FMT_YUV422P10BE:
221 case AV_PIX_FMT_YUVA422P10LE:
222 case AV_PIX_FMT_YUVA422P10BE:
223 case AV_PIX_FMT_YUV444P9LE:
224 case AV_PIX_FMT_YUV444P9BE:
225 case AV_PIX_FMT_YUV444P10LE:
226 case AV_PIX_FMT_YUV444P10BE:
227 case AV_PIX_FMT_YUVA444P10LE:
228 case AV_PIX_FMT_YUVA444P10BE:
229 case AV_PIX_FMT_GBRP9LE:
230 case AV_PIX_FMT_GBRP9BE:
231 case AV_PIX_FMT_GBRP10LE:
232 case AV_PIX_FMT_GBRP10BE:
233 w_align = 16; //FIXME assume 16 pixel per macroblock
234 h_align = 16 * 2; // interlaced needs 2 macroblocks height
236 case AV_PIX_FMT_YUV411P:
237 case AV_PIX_FMT_UYYVYY411:
241 case AV_PIX_FMT_YUV410P:
242 if (s->codec_id == AV_CODEC_ID_SVQ1) {
246 case AV_PIX_FMT_RGB555:
247 if (s->codec_id == AV_CODEC_ID_RPZA) {
251 case AV_PIX_FMT_PAL8:
252 case AV_PIX_FMT_BGR8:
253 case AV_PIX_FMT_RGB8:
254 if (s->codec_id == AV_CODEC_ID_SMC) {
259 case AV_PIX_FMT_BGR24:
260 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
261 (s->codec_id == AV_CODEC_ID_ZLIB)) {
272 *width = FFALIGN(*width, w_align);
273 *height = FFALIGN(*height, h_align);
274 if (s->codec_id == AV_CODEC_ID_H264)
275 // some of the optimized chroma MC reads one line too much
278 for (i = 0; i < 4; i++)
279 linesize_align[i] = STRIDE_ALIGN;
282 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
284 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
285 int chroma_shift = desc->log2_chroma_w;
286 int linesize_align[AV_NUM_DATA_POINTERS];
289 avcodec_align_dimensions2(s, width, height, linesize_align);
290 align = FFMAX(linesize_align[0], linesize_align[3]);
291 linesize_align[1] <<= chroma_shift;
292 linesize_align[2] <<= chroma_shift;
293 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
294 *width = FFALIGN(*width, align);
297 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
298 enum AVSampleFormat sample_fmt, const uint8_t *buf,
299 int buf_size, int align)
301 int ch, planar, needed_size, ret = 0;
303 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
304 frame->nb_samples, sample_fmt,
306 if (buf_size < needed_size)
307 return AVERROR(EINVAL);
309 planar = av_sample_fmt_is_planar(sample_fmt);
310 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
311 if (!(frame->extended_data = av_mallocz(nb_channels *
312 sizeof(*frame->extended_data))))
313 return AVERROR(ENOMEM);
315 frame->extended_data = frame->data;
318 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
319 buf, nb_channels, frame->nb_samples,
320 sample_fmt, align)) < 0) {
321 if (frame->extended_data != frame->data)
322 av_free(frame->extended_data);
325 if (frame->extended_data != frame->data) {
326 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
327 frame->data[ch] = frame->extended_data[ch];
333 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
335 FramePool *pool = avctx->internal->pool;
338 switch (avctx->codec_type) {
339 case AVMEDIA_TYPE_VIDEO: {
342 int w = frame->width;
343 int h = frame->height;
344 int tmpsize, unaligned;
346 if (pool->format == frame->format &&
347 pool->width == frame->width && pool->height == frame->height)
350 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
353 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
354 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
355 av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
356 // increase alignment of w for next try (rhs gives the lowest bit set in w)
360 for (i = 0; i < 4; i++)
361 unaligned |= picture.linesize[i] % pool->stride_align[i];
364 tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
365 NULL, picture.linesize);
369 for (i = 0; i < 3 && picture.data[i + 1]; i++)
370 size[i] = picture.data[i + 1] - picture.data[i];
371 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
373 for (i = 0; i < 4; i++) {
374 av_buffer_pool_uninit(&pool->pools[i]);
375 pool->linesize[i] = picture.linesize[i];
377 pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
378 if (!pool->pools[i]) {
379 ret = AVERROR(ENOMEM);
384 pool->format = frame->format;
385 pool->width = frame->width;
386 pool->height = frame->height;
390 case AVMEDIA_TYPE_AUDIO: {
391 int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
392 int planar = av_sample_fmt_is_planar(frame->format);
393 int planes = planar ? ch : 1;
395 if (pool->format == frame->format && pool->planes == planes &&
396 pool->channels == ch && frame->nb_samples == pool->samples)
399 av_buffer_pool_uninit(&pool->pools[0]);
400 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
401 frame->nb_samples, frame->format, 0);
405 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
406 if (!pool->pools[0]) {
407 ret = AVERROR(ENOMEM);
411 pool->format = frame->format;
412 pool->planes = planes;
414 pool->samples = frame->nb_samples;
417 default: av_assert0(0);
421 for (i = 0; i < 4; i++)
422 av_buffer_pool_uninit(&pool->pools[i]);
424 pool->planes = pool->channels = pool->samples = 0;
425 pool->width = pool->height = 0;
429 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
431 FramePool *pool = avctx->internal->pool;
432 int planes = pool->planes;
435 frame->linesize[0] = pool->linesize[0];
437 if (planes > AV_NUM_DATA_POINTERS) {
438 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
439 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
440 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
441 sizeof(*frame->extended_buf));
442 if (!frame->extended_data || !frame->extended_buf) {
443 av_freep(&frame->extended_data);
444 av_freep(&frame->extended_buf);
445 return AVERROR(ENOMEM);
448 frame->extended_data = frame->data;
450 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
451 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
454 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
456 for (i = 0; i < frame->nb_extended_buf; i++) {
457 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
458 if (!frame->extended_buf[i])
460 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
463 if (avctx->debug & FF_DEBUG_BUFFERS)
464 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
468 av_frame_unref(frame);
469 return AVERROR(ENOMEM);
472 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
474 FramePool *pool = s->internal->pool;
478 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
482 memset(pic->data, 0, sizeof(pic->data));
483 pic->extended_data = pic->data;
485 for (i = 0; i < 4 && pool->pools[i]; i++) {
486 pic->linesize[i] = pool->linesize[i];
488 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
492 pic->data[i] = pic->buf[i]->data;
494 for (; i < AV_NUM_DATA_POINTERS; i++) {
496 pic->linesize[i] = 0;
498 if (pic->data[1] && !pic->data[2])
499 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
501 if (s->debug & FF_DEBUG_BUFFERS)
502 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
507 return AVERROR(ENOMEM);
510 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
514 if ((ret = update_frame_pool(avctx, frame)) < 0)
517 #if FF_API_GET_BUFFER
518 FF_DISABLE_DEPRECATION_WARNINGS
519 frame->type = FF_BUFFER_TYPE_INTERNAL;
520 FF_ENABLE_DEPRECATION_WARNINGS
523 switch (avctx->codec_type) {
524 case AVMEDIA_TYPE_VIDEO:
525 return video_get_buffer(avctx, frame);
526 case AVMEDIA_TYPE_AUDIO:
527 return audio_get_buffer(avctx, frame);
533 #if FF_API_GET_BUFFER
534 FF_DISABLE_DEPRECATION_WARNINGS
535 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
537 return avcodec_default_get_buffer2(avctx, frame, 0);
540 typedef struct CompatReleaseBufPriv {
541 AVCodecContext avctx;
543 } CompatReleaseBufPriv;
545 static void compat_free_buffer(void *opaque, uint8_t *data)
547 CompatReleaseBufPriv *priv = opaque;
548 if (priv->avctx.release_buffer)
549 priv->avctx.release_buffer(&priv->avctx, &priv->frame);
553 static void compat_release_buffer(void *opaque, uint8_t *data)
555 AVBufferRef *buf = opaque;
556 av_buffer_unref(&buf);
558 FF_ENABLE_DEPRECATION_WARNINGS
561 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
563 AVPacket *pkt = avctx->internal->pkt;
566 enum AVPacketSideDataType packet;
567 enum AVFrameSideDataType frame;
569 { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
570 { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
571 { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
572 { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
575 frame->color_primaries = avctx->color_primaries;
576 frame->color_trc = avctx->color_trc;
577 frame->colorspace = avctx->colorspace;
578 frame->color_range = avctx->color_range;
579 frame->chroma_location = avctx->chroma_sample_location;
581 frame->reordered_opaque = avctx->reordered_opaque;
583 frame->pkt_pts = AV_NOPTS_VALUE;
587 frame->pkt_pts = pkt->pts;
589 for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
591 uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
593 AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
597 return AVERROR(ENOMEM);
599 memcpy(frame_sd->data, packet_sd, size);
606 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
608 const AVHWAccel *hwaccel = avctx->hwaccel;
609 int override_dimensions = 1;
612 switch (avctx->codec_type) {
613 case AVMEDIA_TYPE_VIDEO:
614 if (frame->width <= 0 || frame->height <= 0) {
615 frame->width = FFMAX(avctx->width, avctx->coded_width);
616 frame->height = FFMAX(avctx->height, avctx->coded_height);
617 override_dimensions = 0;
619 if (frame->format < 0)
620 frame->format = avctx->pix_fmt;
621 if (!frame->sample_aspect_ratio.num)
622 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
624 if (av_image_check_sar(frame->width, frame->height,
625 frame->sample_aspect_ratio) < 0) {
626 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
627 frame->sample_aspect_ratio.num,
628 frame->sample_aspect_ratio.den);
629 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
632 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
635 case AVMEDIA_TYPE_AUDIO:
636 if (!frame->sample_rate)
637 frame->sample_rate = avctx->sample_rate;
638 if (frame->format < 0)
639 frame->format = avctx->sample_fmt;
640 if (!frame->channel_layout) {
641 if (avctx->channel_layout) {
642 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
644 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
646 return AVERROR(EINVAL);
649 frame->channel_layout = avctx->channel_layout;
651 if (avctx->channels > FF_SANE_NB_CHANNELS) {
652 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
654 return AVERROR(ENOSYS);
657 frame->channel_layout = av_get_default_channel_layout(avctx->channels);
658 if (!frame->channel_layout)
659 frame->channel_layout = (1ULL << avctx->channels) - 1;
663 default: return AVERROR(EINVAL);
666 ret = ff_decode_frame_props(avctx, frame);
671 if (hwaccel->alloc_frame) {
672 ret = hwaccel->alloc_frame(avctx, frame);
676 avctx->sw_pix_fmt = avctx->pix_fmt;
678 #if FF_API_GET_BUFFER
679 FF_DISABLE_DEPRECATION_WARNINGS
681 * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
682 * We wrap each plane in its own AVBuffer. Each of those has a reference to
683 * a dummy AVBuffer as its private data, unreffing it on free.
684 * When all the planes are freed, the dummy buffer's free callback calls
687 if (avctx->get_buffer) {
688 CompatReleaseBufPriv *priv = NULL;
689 AVBufferRef *dummy_buf = NULL;
692 if (flags & AV_GET_BUFFER_FLAG_REF)
693 frame->reference = 1;
695 ret = avctx->get_buffer(avctx, frame);
699 /* return if the buffers are already set up
700 * this would happen e.g. when a custom get_buffer() calls
701 * avcodec_default_get_buffer
706 priv = av_mallocz(sizeof(*priv));
708 ret = AVERROR(ENOMEM);
711 priv->avctx = *avctx;
712 priv->frame = *frame;
714 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
716 ret = AVERROR(ENOMEM);
720 #define WRAP_PLANE(ref_out, data, data_size) \
722 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
724 ret = AVERROR(ENOMEM); \
727 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
730 av_buffer_unref(&dummy_ref); \
731 av_frame_unref(frame); \
732 ret = AVERROR(ENOMEM); \
737 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
738 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
740 planes = av_pix_fmt_count_planes(frame->format);
741 /* workaround for AVHWAccel plane count of 0, buf[0] is used as
742 check for allocated buffers: make libavcodec happy */
743 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
745 if (!desc || planes <= 0) {
746 ret = AVERROR(EINVAL);
750 for (i = 0; i < planes; i++) {
751 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
752 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
754 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
757 int planar = av_sample_fmt_is_planar(frame->format);
758 planes = planar ? avctx->channels : 1;
760 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
761 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
762 frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
763 frame->nb_extended_buf);
764 if (!frame->extended_buf) {
765 ret = AVERROR(ENOMEM);
770 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
771 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
773 for (i = 0; i < frame->nb_extended_buf; i++)
774 WRAP_PLANE(frame->extended_buf[i],
775 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
779 av_buffer_unref(&dummy_buf);
781 frame->width = avctx->width;
782 frame->height = avctx->height;
787 avctx->release_buffer(avctx, frame);
789 av_buffer_unref(&dummy_buf);
792 FF_ENABLE_DEPRECATION_WARNINGS
795 ret = avctx->get_buffer2(avctx, frame, flags);
798 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
799 frame->width = avctx->width;
800 frame->height = avctx->height;
806 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
811 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
814 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
816 if (av_frame_is_writable(frame))
817 return ff_decode_frame_props(avctx, frame);
819 tmp = av_frame_alloc();
821 return AVERROR(ENOMEM);
823 av_frame_move_ref(tmp, frame);
825 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
831 av_frame_copy(frame, tmp);
837 #if FF_API_GET_BUFFER
838 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
843 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
850 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
854 for (i = 0; i < count; i++) {
855 int r = func(c, (char *)arg + i * size);
862 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
866 for (i = 0; i < count; i++) {
867 int r = func(c, arg, i, 0);
874 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
876 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
877 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
880 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
882 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
887 static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
888 enum AVPixelFormat pix_fmt)
890 AVHWAccel *hwaccel = NULL;
892 while ((hwaccel = av_hwaccel_next(hwaccel)))
893 if (hwaccel->id == codec_id
894 && hwaccel->pix_fmt == pix_fmt)
899 static int setup_hwaccel(AVCodecContext *avctx,
900 const enum AVPixelFormat fmt,
903 AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
907 av_log(avctx, AV_LOG_ERROR,
908 "Could not find an AVHWAccel for the pixel format: %s",
910 return AVERROR(ENOENT);
913 if (hwa->priv_data_size) {
914 avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
915 if (!avctx->internal->hwaccel_priv_data)
916 return AVERROR(ENOMEM);
920 ret = hwa->init(avctx);
922 av_freep(&avctx->internal->hwaccel_priv_data);
927 avctx->hwaccel = hwa;
932 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
934 const AVPixFmtDescriptor *desc;
935 enum AVPixelFormat *choices;
936 enum AVPixelFormat ret;
939 while (fmt[n] != AV_PIX_FMT_NONE)
943 avctx->sw_pix_fmt = fmt[n - 1];
944 av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
946 choices = av_malloc_array(n + 1, sizeof(*choices));
948 return AV_PIX_FMT_NONE;
950 memcpy(choices, fmt, (n + 1) * sizeof(*choices));
953 if (avctx->hwaccel && avctx->hwaccel->uninit)
954 avctx->hwaccel->uninit(avctx);
955 av_freep(&avctx->internal->hwaccel_priv_data);
956 avctx->hwaccel = NULL;
958 ret = avctx->get_format(avctx, choices);
960 desc = av_pix_fmt_desc_get(ret);
962 ret = AV_PIX_FMT_NONE;
966 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
969 if (!setup_hwaccel(avctx, ret, desc->name))
972 /* Remove failed hwaccel from choices */
973 for (n = 0; choices[n] != ret; n++)
974 av_assert0(choices[n] != AV_PIX_FMT_NONE);
977 choices[n] = choices[n + 1];
978 while (choices[n++] != AV_PIX_FMT_NONE);
985 #if FF_API_AVFRAME_LAVC
986 void avcodec_get_frame_defaults(AVFrame *frame)
988 if (frame->extended_data != frame->data)
989 av_freep(&frame->extended_data);
991 memset(frame, 0, sizeof(AVFrame));
993 frame->pts = AV_NOPTS_VALUE;
994 frame->key_frame = 1;
995 frame->sample_aspect_ratio = (AVRational) {0, 1 };
996 frame->format = -1; /* unknown */
997 frame->extended_data = frame->data;
1000 AVFrame *avcodec_alloc_frame(void)
1002 AVFrame *frame = av_mallocz(sizeof(AVFrame));
1007 FF_DISABLE_DEPRECATION_WARNINGS
1008 avcodec_get_frame_defaults(frame);
1009 FF_ENABLE_DEPRECATION_WARNINGS
1014 void avcodec_free_frame(AVFrame **frame)
1016 av_frame_free(frame);
1020 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1023 AVDictionary *tmp = NULL;
1025 if (avcodec_is_open(avctx))
1028 if ((!codec && !avctx->codec)) {
1029 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
1030 return AVERROR(EINVAL);
1032 if ((codec && avctx->codec && codec != avctx->codec)) {
1033 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
1034 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
1035 return AVERROR(EINVAL);
1038 codec = avctx->codec;
1040 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
1041 return AVERROR(EINVAL);
1044 av_dict_copy(&tmp, *options, 0);
1046 /* If there is a user-supplied mutex locking routine, call it. */
1048 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1052 entangled_thread_counter++;
1053 if (entangled_thread_counter != 1 &&
1054 !(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE)) {
1055 av_log(avctx, AV_LOG_ERROR,
1056 "Insufficient thread locking. At least %d threads are "
1057 "calling avcodec_open2() at the same time right now.\n",
1058 entangled_thread_counter);
1063 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
1064 if (!avctx->internal) {
1065 ret = AVERROR(ENOMEM);
1069 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
1070 if (!avctx->internal->pool) {
1071 ret = AVERROR(ENOMEM);
1075 avctx->internal->to_free = av_frame_alloc();
1076 if (!avctx->internal->to_free) {
1077 ret = AVERROR(ENOMEM);
1081 if (codec->priv_data_size > 0) {
1082 if (!avctx->priv_data) {
1083 avctx->priv_data = av_mallocz(codec->priv_data_size);
1084 if (!avctx->priv_data) {
1085 ret = AVERROR(ENOMEM);
1088 if (codec->priv_class) {
1089 *(const AVClass **)avctx->priv_data = codec->priv_class;
1090 av_opt_set_defaults(avctx->priv_data);
1093 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
1096 avctx->priv_data = NULL;
1098 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
1101 if (avctx->coded_width && avctx->coded_height && !avctx->width && !avctx->height)
1102 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
1103 else if (avctx->width && avctx->height)
1104 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1108 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1109 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1110 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1111 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
1112 ff_set_dimensions(avctx, 0, 0);
1115 if (avctx->width > 0 && avctx->height > 0) {
1116 if (av_image_check_sar(avctx->width, avctx->height,
1117 avctx->sample_aspect_ratio) < 0) {
1118 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1119 avctx->sample_aspect_ratio.num,
1120 avctx->sample_aspect_ratio.den);
1121 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
1125 /* if the decoder init function was already called previously,
1126 * free the already allocated subtitle_header before overwriting it */
1127 if (av_codec_is_decoder(codec))
1128 av_freep(&avctx->subtitle_header);
1130 if (avctx->channels > FF_SANE_NB_CHANNELS) {
1131 ret = AVERROR(EINVAL);
1135 avctx->codec = codec;
1136 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1137 avctx->codec_id == AV_CODEC_ID_NONE) {
1138 avctx->codec_type = codec->type;
1139 avctx->codec_id = codec->id;
1141 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1142 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1143 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
1144 ret = AVERROR(EINVAL);
1147 avctx->frame_number = 0;
1149 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1150 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1151 ret = AVERROR_EXPERIMENTAL;
1155 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1156 (!avctx->time_base.num || !avctx->time_base.den)) {
1157 avctx->time_base.num = 1;
1158 avctx->time_base.den = avctx->sample_rate;
1162 ret = ff_thread_init(avctx);
1167 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
1168 avctx->thread_count = 1;
1170 if (av_codec_is_encoder(avctx->codec)) {
1172 avctx->coded_frame = av_frame_alloc();
1173 if (!avctx->coded_frame) {
1174 ret = AVERROR(ENOMEM);
1177 if (avctx->codec->sample_fmts) {
1178 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1179 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1181 if (avctx->channels == 1 &&
1182 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1183 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1184 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1188 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1189 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
1190 ret = AVERROR(EINVAL);
1194 if (avctx->codec->pix_fmts) {
1195 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1196 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1198 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
1199 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
1200 ret = AVERROR(EINVAL);
1203 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
1204 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
1205 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
1206 avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
1207 avctx->color_range = AVCOL_RANGE_JPEG;
1209 if (avctx->codec->supported_samplerates) {
1210 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1211 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1213 if (avctx->codec->supported_samplerates[i] == 0) {
1214 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
1215 ret = AVERROR(EINVAL);
1219 if (avctx->codec->channel_layouts) {
1220 if (!avctx->channel_layout) {
1221 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
1223 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1224 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1226 if (avctx->codec->channel_layouts[i] == 0) {
1227 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
1228 ret = AVERROR(EINVAL);
1233 if (avctx->channel_layout && avctx->channels) {
1234 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
1235 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
1236 ret = AVERROR(EINVAL);
1239 } else if (avctx->channel_layout) {
1240 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1243 if (!avctx->rc_initial_buffer_occupancy)
1244 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1247 if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
1248 ret = avctx->codec->init(avctx);
1254 #if FF_API_AUDIOENC_DELAY
1255 if (av_codec_is_encoder(avctx->codec))
1256 avctx->delay = avctx->initial_padding;
1259 if (av_codec_is_decoder(avctx->codec)) {
1260 /* validate channel layout from the decoder */
1261 if (avctx->channel_layout) {
1262 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1263 if (!avctx->channels)
1264 avctx->channels = channels;
1265 else if (channels != avctx->channels) {
1266 av_log(avctx, AV_LOG_WARNING,
1267 "channel layout does not match number of channels\n");
1268 avctx->channel_layout = 0;
1271 if (avctx->channels && avctx->channels < 0 ||
1272 avctx->channels > FF_SANE_NB_CHANNELS) {
1273 ret = AVERROR(EINVAL);
1277 #if FF_API_AVCTX_TIMEBASE
1278 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1279 avctx->time_base = av_inv_q(avctx->framerate);
1283 entangled_thread_counter--;
1285 /* Release any user-supplied mutex. */
1287 (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1290 av_dict_free(options);
1297 (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
1298 avctx->codec->close(avctx);
1300 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1301 av_opt_free(avctx->priv_data);
1304 av_frame_free(&avctx->coded_frame);
1307 av_freep(&avctx->priv_data);
1308 if (avctx->internal) {
1309 av_frame_free(&avctx->internal->to_free);
1310 av_freep(&avctx->internal->pool);
1312 av_freep(&avctx->internal);
1313 avctx->codec = NULL;
1317 int ff_alloc_packet(AVPacket *avpkt, int size)
1319 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
1320 return AVERROR(EINVAL);
1323 AVBufferRef *buf = avpkt->buf;
1324 #if FF_API_DESTRUCT_PACKET
1325 FF_DISABLE_DEPRECATION_WARNINGS
1326 void *destruct = avpkt->destruct;
1327 FF_ENABLE_DEPRECATION_WARNINGS
1330 if (avpkt->size < size)
1331 return AVERROR(EINVAL);
1333 av_init_packet(avpkt);
1334 #if FF_API_DESTRUCT_PACKET
1335 FF_DISABLE_DEPRECATION_WARNINGS
1336 avpkt->destruct = destruct;
1337 FF_ENABLE_DEPRECATION_WARNINGS
1343 return av_new_packet(avpkt, size);
1348 * Pad last frame with silence.
1350 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1352 AVFrame *frame = NULL;
1355 if (!(frame = av_frame_alloc()))
1356 return AVERROR(ENOMEM);
1358 frame->format = src->format;
1359 frame->channel_layout = src->channel_layout;
1360 frame->nb_samples = s->frame_size;
1361 ret = av_frame_get_buffer(frame, 32);
1365 ret = av_frame_copy_props(frame, src);
1369 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1370 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1372 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1373 frame->nb_samples - src->nb_samples,
1374 s->channels, s->sample_fmt)) < 0)
1382 av_frame_free(&frame);
1386 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1388 const AVFrame *frame,
1389 int *got_packet_ptr)
1392 AVFrame *padded_frame = NULL;
1394 int user_packet = !!avpkt->data;
1396 *got_packet_ptr = 0;
1398 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1399 av_free_packet(avpkt);
1400 av_init_packet(avpkt);
1404 /* ensure that extended_data is properly set */
1405 if (frame && !frame->extended_data) {
1406 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1407 avctx->channels > AV_NUM_DATA_POINTERS) {
1408 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1409 "with more than %d channels, but extended_data is not set.\n",
1410 AV_NUM_DATA_POINTERS);
1411 return AVERROR(EINVAL);
1413 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1416 tmp.extended_data = tmp.data;
1420 /* extract audio service type metadata */
1422 AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
1423 if (sd && sd->size >= sizeof(enum AVAudioServiceType))
1424 avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
1427 /* check for valid frame size */
1429 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1430 if (frame->nb_samples > avctx->frame_size)
1431 return AVERROR(EINVAL);
1432 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1433 if (frame->nb_samples < avctx->frame_size &&
1434 !avctx->internal->last_audio_frame) {
1435 ret = pad_last_frame(avctx, &padded_frame, frame);
1439 frame = padded_frame;
1440 avctx->internal->last_audio_frame = 1;
1443 if (frame->nb_samples != avctx->frame_size) {
1444 ret = AVERROR(EINVAL);
1450 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1452 if (*got_packet_ptr) {
1453 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1454 if (avpkt->pts == AV_NOPTS_VALUE)
1455 avpkt->pts = frame->pts;
1456 if (!avpkt->duration)
1457 avpkt->duration = ff_samples_to_time_base(avctx,
1460 avpkt->dts = avpkt->pts;
1465 if (!user_packet && avpkt->size) {
1466 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1468 avpkt->data = avpkt->buf->data;
1471 avctx->frame_number++;
1474 if (ret < 0 || !*got_packet_ptr) {
1475 av_free_packet(avpkt);
1476 av_init_packet(avpkt);
1480 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1481 * this needs to be moved to the encoders, but for now we can do it
1482 * here to simplify things */
1483 avpkt->flags |= AV_PKT_FLAG_KEY;
1486 av_frame_free(&padded_frame);
1488 #if FF_API_AUDIOENC_DELAY
1489 avctx->delay = avctx->initial_padding;
1495 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1497 const AVFrame *frame,
1498 int *got_packet_ptr)
1501 int user_packet = !!avpkt->data;
1503 *got_packet_ptr = 0;
1505 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1506 av_free_packet(avpkt);
1507 av_init_packet(avpkt);
1512 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1513 return AVERROR(EINVAL);
1515 av_assert0(avctx->codec->encode2);
1517 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1519 if (!*got_packet_ptr)
1521 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1522 avpkt->pts = avpkt->dts = frame->pts;
1524 if (!user_packet && avpkt->size) {
1525 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1527 avpkt->data = avpkt->buf->data;
1530 avctx->frame_number++;
1533 if (ret < 0 || !*got_packet_ptr)
1534 av_free_packet(avpkt);
1540 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1541 const AVSubtitle *sub)
1544 if (sub->start_display_time) {
1545 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1548 if (sub->num_rects == 0 || !sub->rects)
1550 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
1551 avctx->frame_number++;
1555 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1558 const uint8_t *data;
1561 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1565 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
1566 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
1567 "changes, but PARAM_CHANGE side data was sent to it.\n");
1568 return AVERROR(EINVAL);
1574 flags = bytestream_get_le32(&data);
1577 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1580 avctx->channels = bytestream_get_le32(&data);
1583 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1586 avctx->channel_layout = bytestream_get_le64(&data);
1589 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1592 avctx->sample_rate = bytestream_get_le32(&data);
1595 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1598 avctx->width = bytestream_get_le32(&data);
1599 avctx->height = bytestream_get_le32(&data);
1601 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1608 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
1609 return AVERROR_INVALIDDATA;
1612 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
1616 /* move the original frame to our backup */
1617 av_frame_unref(avci->to_free);
1618 av_frame_move_ref(avci->to_free, frame);
1620 /* now copy everything except the AVBufferRefs back
1621 * note that we make a COPY of the side data, so calling av_frame_free() on
1622 * the caller's frame will work properly */
1623 ret = av_frame_copy_props(frame, avci->to_free);
1627 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
1628 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
1629 if (avci->to_free->extended_data != avci->to_free->data) {
1630 int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
1631 int size = planes * sizeof(*frame->extended_data);
1634 av_frame_unref(frame);
1638 frame->extended_data = av_malloc(size);
1639 if (!frame->extended_data) {
1640 av_frame_unref(frame);
1641 return AVERROR(ENOMEM);
1643 memcpy(frame->extended_data, avci->to_free->extended_data,
1646 frame->extended_data = frame->data;
1648 frame->format = avci->to_free->format;
1649 frame->width = avci->to_free->width;
1650 frame->height = avci->to_free->height;
1651 frame->channel_layout = avci->to_free->channel_layout;
1652 frame->nb_samples = avci->to_free->nb_samples;
1657 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1658 int *got_picture_ptr,
1661 AVCodecInternal *avci = avctx->internal;
1664 *got_picture_ptr = 0;
1665 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1668 avctx->internal->pkt = avpkt;
1669 ret = apply_param_change(avctx, avpkt);
1671 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
1672 if (avctx->err_recognition & AV_EF_EXPLODE)
1676 av_frame_unref(picture);
1678 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1679 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1680 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1683 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1685 picture->pkt_dts = avpkt->dts;
1686 /* get_buffer is supposed to set frame parameters */
1687 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
1688 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1689 picture->width = avctx->width;
1690 picture->height = avctx->height;
1691 picture->format = avctx->pix_fmt;
1695 emms_c(); //needed to avoid an emms_c() call before every return;
1697 if (*got_picture_ptr) {
1698 if (!avctx->refcounted_frames) {
1699 int err = unrefcount_frame(avci, picture);
1704 avctx->frame_number++;
1706 av_frame_unref(picture);
1710 #if FF_API_AVCTX_TIMEBASE
1711 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1712 avctx->time_base = av_inv_q(avctx->framerate);
1718 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1723 AVCodecInternal *avci = avctx->internal;
1728 avctx->internal->pkt = avpkt;
1730 if (!avpkt->data && avpkt->size) {
1731 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1732 return AVERROR(EINVAL);
1735 ret = apply_param_change(avctx, avpkt);
1737 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
1738 if (avctx->err_recognition & AV_EF_EXPLODE)
1742 av_frame_unref(frame);
1744 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1745 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1746 if (ret >= 0 && *got_frame_ptr) {
1747 avctx->frame_number++;
1748 frame->pkt_dts = avpkt->dts;
1749 if (frame->format == AV_SAMPLE_FMT_NONE)
1750 frame->format = avctx->sample_fmt;
1752 if (!avctx->refcounted_frames) {
1753 int err = unrefcount_frame(avci, frame);
1758 av_frame_unref(frame);
1765 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1771 avctx->internal->pkt = avpkt;
1773 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1775 avctx->frame_number++;
1779 void avsubtitle_free(AVSubtitle *sub)
1783 for (i = 0; i < sub->num_rects; i++) {
1784 av_freep(&sub->rects[i]->pict.data[0]);
1785 av_freep(&sub->rects[i]->pict.data[1]);
1786 av_freep(&sub->rects[i]->pict.data[2]);
1787 av_freep(&sub->rects[i]->pict.data[3]);
1788 av_freep(&sub->rects[i]->text);
1789 av_freep(&sub->rects[i]->ass);
1790 av_freep(&sub->rects[i]);
1793 av_freep(&sub->rects);
1795 memset(sub, 0, sizeof(AVSubtitle));
1798 av_cold int avcodec_close(AVCodecContext *avctx)
1800 if (avcodec_is_open(avctx)) {
1801 FramePool *pool = avctx->internal->pool;
1803 if (HAVE_THREADS && avctx->internal->thread_ctx)
1804 ff_thread_free(avctx);
1805 if (avctx->codec && avctx->codec->close)
1806 avctx->codec->close(avctx);
1807 av_frame_free(&avctx->internal->to_free);
1808 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
1809 av_buffer_pool_uninit(&pool->pools[i]);
1810 av_freep(&avctx->internal->pool);
1812 if (avctx->hwaccel && avctx->hwaccel->uninit)
1813 avctx->hwaccel->uninit(avctx);
1814 av_freep(&avctx->internal->hwaccel_priv_data);
1816 av_freep(&avctx->internal);
1819 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1820 av_opt_free(avctx->priv_data);
1822 av_freep(&avctx->priv_data);
1823 if (av_codec_is_encoder(avctx->codec)) {
1824 av_freep(&avctx->extradata);
1825 av_frame_free(&avctx->coded_frame);
1827 avctx->codec = NULL;
1828 avctx->active_thread_type = 0;
1833 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
1835 AVCodec *p, *experimental = NULL;
1838 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1840 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1847 return experimental;
1850 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1852 return find_encdec(id, 1);
1855 AVCodec *avcodec_find_encoder_by_name(const char *name)
1862 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
1869 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1871 return find_encdec(id, 0);
1874 AVCodec *avcodec_find_decoder_by_name(const char *name)
1881 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
1888 static int get_bit_rate(AVCodecContext *ctx)
1891 int bits_per_sample;
1893 switch (ctx->codec_type) {
1894 case AVMEDIA_TYPE_VIDEO:
1895 case AVMEDIA_TYPE_DATA:
1896 case AVMEDIA_TYPE_SUBTITLE:
1897 case AVMEDIA_TYPE_ATTACHMENT:
1898 bit_rate = ctx->bit_rate;
1900 case AVMEDIA_TYPE_AUDIO:
1901 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1902 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1911 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1913 int i, len, ret = 0;
1915 #define TAG_PRINT(x) \
1916 (((x) >= '0' && (x) <= '9') || \
1917 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
1918 ((x) == '.' || (x) == ' '))
1920 for (i = 0; i < 4; i++) {
1921 len = snprintf(buf, buf_size,
1922 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
1924 buf_size = buf_size > len ? buf_size - len : 0;
1931 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1933 const char *codec_name;
1934 const char *profile = NULL;
1939 AVRational display_aspect_ratio;
1944 p = avcodec_find_encoder(enc->codec_id);
1946 p = avcodec_find_decoder(enc->codec_id);
1949 codec_name = p->name;
1950 profile = av_get_profile_name(p, enc->profile);
1951 } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
1952 /* fake mpeg2 transport stream codec (currently not
1954 codec_name = "mpeg2ts";
1956 /* output avi tags */
1958 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1959 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1963 switch (enc->codec_type) {
1964 case AVMEDIA_TYPE_VIDEO:
1965 snprintf(buf, buf_size,
1967 codec_name, enc->mb_decision ? " (hq)" : "");
1969 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1971 if (enc->codec_tag) {
1973 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1974 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1975 " [%s / 0x%04X]", tag_buf, enc->codec_tag);
1978 av_strlcat(buf, "\n ", buf_size);
1979 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1980 "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
1981 av_get_pix_fmt_name(enc->pix_fmt));
1983 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
1984 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
1985 av_color_range_name(enc->color_range));
1986 if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
1987 enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
1988 enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
1990 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s/%s/%s",
1991 av_color_space_name(enc->colorspace),
1992 av_color_primaries_name(enc->color_primaries),
1993 av_color_transfer_name(enc->color_trc));
1995 if (av_log_get_level() >= AV_LOG_DEBUG &&
1996 enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
1997 snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
1998 av_chroma_location_name(enc->chroma_sample_location));
2001 av_strlcat(buf, new_line ? "\n " : ", ", buf_size);
2003 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2005 enc->width, enc->height);
2007 if (av_log_get_level() >= AV_LOG_VERBOSE &&
2008 (enc->width != enc->coded_width ||
2009 enc->height != enc->coded_height))
2010 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2011 " (%dx%d)", enc->coded_width, enc->coded_height);
2013 if (enc->sample_aspect_ratio.num) {
2014 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2015 enc->width * enc->sample_aspect_ratio.num,
2016 enc->height * enc->sample_aspect_ratio.den,
2018 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2019 " [PAR %d:%d DAR %d:%d]",
2020 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
2021 display_aspect_ratio.num, display_aspect_ratio.den);
2023 if (av_log_get_level() >= AV_LOG_DEBUG) {
2024 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2025 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2027 enc->time_base.num / g, enc->time_base.den / g);
2031 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2032 ", q=%d-%d", enc->qmin, enc->qmax);
2035 case AVMEDIA_TYPE_AUDIO:
2036 snprintf(buf, buf_size,
2040 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2042 if (enc->codec_tag) {
2044 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2045 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2046 " [%s / 0x%04X]", tag_buf, enc->codec_tag);
2049 av_strlcat(buf, "\n ", buf_size);
2050 if (enc->sample_rate) {
2051 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2052 "%d Hz, ", enc->sample_rate);
2054 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2055 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2056 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2057 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2060 case AVMEDIA_TYPE_DATA:
2061 snprintf(buf, buf_size, "Data: %s", codec_name);
2063 case AVMEDIA_TYPE_SUBTITLE:
2064 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
2066 case AVMEDIA_TYPE_ATTACHMENT:
2067 snprintf(buf, buf_size, "Attachment: %s", codec_name);
2070 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
2074 if (enc->flags & CODEC_FLAG_PASS1)
2075 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2077 if (enc->flags & CODEC_FLAG_PASS2)
2078 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2081 bitrate = get_bit_rate(enc);
2083 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2084 ", %d kb/s", bitrate / 1000);
2088 const char *av_get_profile_name(const AVCodec *codec, int profile)
2091 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
2094 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
2095 if (p->profile == profile)
2101 unsigned avcodec_version(void)
2103 return LIBAVCODEC_VERSION_INT;
2106 const char *avcodec_configuration(void)
2108 return LIBAV_CONFIGURATION;
2111 const char *avcodec_license(void)
2113 #define LICENSE_PREFIX "libavcodec license: "
2114 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2117 void avcodec_flush_buffers(AVCodecContext *avctx)
2119 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2120 ff_thread_flush(avctx);
2121 else if (avctx->codec->flush)
2122 avctx->codec->flush(avctx);
2124 if (!avctx->refcounted_frames)
2125 av_frame_unref(avctx->internal->to_free);
2128 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2131 case AV_CODEC_ID_ADPCM_CT:
2132 case AV_CODEC_ID_ADPCM_IMA_APC:
2133 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2134 case AV_CODEC_ID_ADPCM_IMA_WS:
2135 case AV_CODEC_ID_ADPCM_G722:
2136 case AV_CODEC_ID_ADPCM_YAMAHA:
2138 case AV_CODEC_ID_PCM_ALAW:
2139 case AV_CODEC_ID_PCM_MULAW:
2140 case AV_CODEC_ID_PCM_S8:
2141 case AV_CODEC_ID_PCM_U8:
2142 case AV_CODEC_ID_PCM_ZORK:
2144 case AV_CODEC_ID_PCM_S16BE:
2145 case AV_CODEC_ID_PCM_S16BE_PLANAR:
2146 case AV_CODEC_ID_PCM_S16LE:
2147 case AV_CODEC_ID_PCM_S16LE_PLANAR:
2148 case AV_CODEC_ID_PCM_U16BE:
2149 case AV_CODEC_ID_PCM_U16LE:
2151 case AV_CODEC_ID_PCM_S24DAUD:
2152 case AV_CODEC_ID_PCM_S24BE:
2153 case AV_CODEC_ID_PCM_S24LE:
2154 case AV_CODEC_ID_PCM_S24LE_PLANAR:
2155 case AV_CODEC_ID_PCM_U24BE:
2156 case AV_CODEC_ID_PCM_U24LE:
2158 case AV_CODEC_ID_PCM_S32BE:
2159 case AV_CODEC_ID_PCM_S32LE:
2160 case AV_CODEC_ID_PCM_S32LE_PLANAR:
2161 case AV_CODEC_ID_PCM_U32BE:
2162 case AV_CODEC_ID_PCM_U32LE:
2163 case AV_CODEC_ID_PCM_F32BE:
2164 case AV_CODEC_ID_PCM_F32LE:
2166 case AV_CODEC_ID_PCM_F64BE:
2167 case AV_CODEC_ID_PCM_F64LE:
2174 int av_get_bits_per_sample(enum AVCodecID codec_id)
2177 case AV_CODEC_ID_ADPCM_SBPRO_2:
2179 case AV_CODEC_ID_ADPCM_SBPRO_3:
2181 case AV_CODEC_ID_ADPCM_SBPRO_4:
2182 case AV_CODEC_ID_ADPCM_IMA_WAV:
2183 case AV_CODEC_ID_ADPCM_IMA_QT:
2184 case AV_CODEC_ID_ADPCM_SWF:
2185 case AV_CODEC_ID_ADPCM_MS:
2188 return av_get_exact_bits_per_sample(codec_id);
2192 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2194 int id, sr, ch, ba, tag, bps;
2196 id = avctx->codec_id;
2197 sr = avctx->sample_rate;
2198 ch = avctx->channels;
2199 ba = avctx->block_align;
2200 tag = avctx->codec_tag;
2201 bps = av_get_exact_bits_per_sample(avctx->codec_id);
2203 /* codecs with an exact constant bits per sample */
2204 if (bps > 0 && ch > 0 && frame_bytes > 0)
2205 return (frame_bytes * 8) / (bps * ch);
2206 bps = avctx->bits_per_coded_sample;
2208 /* codecs with a fixed packet duration */
2210 case AV_CODEC_ID_ADPCM_ADX: return 32;
2211 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
2212 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
2213 case AV_CODEC_ID_AMR_NB:
2214 case AV_CODEC_ID_GSM:
2215 case AV_CODEC_ID_QCELP:
2216 case AV_CODEC_ID_RA_144:
2217 case AV_CODEC_ID_RA_288: return 160;
2218 case AV_CODEC_ID_IMC: return 256;
2219 case AV_CODEC_ID_AMR_WB:
2220 case AV_CODEC_ID_GSM_MS: return 320;
2221 case AV_CODEC_ID_MP1: return 384;
2222 case AV_CODEC_ID_ATRAC1: return 512;
2223 case AV_CODEC_ID_ATRAC3: return 1024;
2224 case AV_CODEC_ID_MP2:
2225 case AV_CODEC_ID_MUSEPACK7: return 1152;
2226 case AV_CODEC_ID_AC3: return 1536;
2230 /* calc from sample rate */
2231 if (id == AV_CODEC_ID_TTA)
2232 return 256 * sr / 245;
2235 /* calc from sample rate and channels */
2236 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2237 return (480 << (sr / 22050)) / ch;
2242 /* calc from block_align */
2243 if (id == AV_CODEC_ID_SIPR) {
2245 case 20: return 160;
2246 case 19: return 144;
2247 case 29: return 288;
2248 case 37: return 480;
2250 } else if (id == AV_CODEC_ID_ILBC) {
2252 case 38: return 160;
2253 case 50: return 240;
2258 if (frame_bytes > 0) {
2259 /* calc from frame_bytes only */
2260 if (id == AV_CODEC_ID_TRUESPEECH)
2261 return 240 * (frame_bytes / 32);
2262 if (id == AV_CODEC_ID_NELLYMOSER)
2263 return 256 * (frame_bytes / 64);
2266 /* calc from frame_bytes and bits_per_coded_sample */
2267 if (id == AV_CODEC_ID_ADPCM_G726)
2268 return frame_bytes * 8 / bps;
2272 /* calc from frame_bytes and channels */
2274 case AV_CODEC_ID_ADPCM_4XM:
2275 case AV_CODEC_ID_ADPCM_IMA_ISS:
2276 return (frame_bytes - 4 * ch) * 2 / ch;
2277 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2278 return (frame_bytes - 4) * 2 / ch;
2279 case AV_CODEC_ID_ADPCM_IMA_AMV:
2280 return (frame_bytes - 8) * 2 / ch;
2281 case AV_CODEC_ID_ADPCM_XA:
2282 return (frame_bytes / 128) * 224 / ch;
2283 case AV_CODEC_ID_INTERPLAY_DPCM:
2284 return (frame_bytes - 6 - ch) / ch;
2285 case AV_CODEC_ID_ROQ_DPCM:
2286 return (frame_bytes - 8) / ch;
2287 case AV_CODEC_ID_XAN_DPCM:
2288 return (frame_bytes - 2 * ch) / ch;
2289 case AV_CODEC_ID_MACE3:
2290 return 3 * frame_bytes / ch;
2291 case AV_CODEC_ID_MACE6:
2292 return 6 * frame_bytes / ch;
2293 case AV_CODEC_ID_PCM_LXF:
2294 return 2 * (frame_bytes / (5 * ch));
2298 /* calc from frame_bytes, channels, and codec_tag */
2299 if (id == AV_CODEC_ID_SOL_DPCM) {
2301 return frame_bytes / ch;
2303 return frame_bytes * 2 / ch;
2308 /* calc from frame_bytes, channels, and block_align */
2309 int blocks = frame_bytes / ba;
2310 switch (avctx->codec_id) {
2311 case AV_CODEC_ID_ADPCM_IMA_WAV:
2312 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2313 case AV_CODEC_ID_ADPCM_IMA_DK3:
2314 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2315 case AV_CODEC_ID_ADPCM_IMA_DK4:
2316 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2317 case AV_CODEC_ID_ADPCM_MS:
2318 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2323 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2324 switch (avctx->codec_id) {
2325 case AV_CODEC_ID_PCM_DVD:
2326 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2327 case AV_CODEC_ID_PCM_BLURAY:
2328 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2329 case AV_CODEC_ID_S302M:
2330 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2340 int ff_thread_init(AVCodecContext *s)
2347 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2361 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2364 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
2368 #if FF_API_MISSING_SAMPLE
2369 FF_DISABLE_DEPRECATION_WARNINGS
2370 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2372 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
2373 "version to the newest one from Git. If the problem still "
2374 "occurs, it means that your file has a feature which has not "
2375 "been implemented.\n", feature);
2377 av_log_ask_for_sample(avc, NULL);
2380 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2382 va_list argument_list;
2384 va_start(argument_list, msg);
2387 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2388 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2389 "of this file to ftp://upload.libav.org/incoming/ "
2390 "and contact the libav-devel mailing list.\n");
2392 va_end(argument_list);
2394 FF_ENABLE_DEPRECATION_WARNINGS
2395 #endif /* FF_API_MISSING_SAMPLE */
2397 static AVHWAccel *first_hwaccel = NULL;
2399 void av_register_hwaccel(AVHWAccel *hwaccel)
2401 AVHWAccel **p = &first_hwaccel;
2405 hwaccel->next = NULL;
2408 AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
2410 return hwaccel ? hwaccel->next : first_hwaccel;
2413 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2416 // There is no good way to rollback a failure to destroy the
2417 // mutex, so we ignore failures.
2418 lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
2419 lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
2422 avformat_mutex = NULL;
2426 void *new_codec_mutex = NULL;
2427 void *new_avformat_mutex = NULL;
2429 if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
2430 return err > 0 ? AVERROR_UNKNOWN : err;
2432 if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
2433 // Ignore failures to destroy the newly created mutex.
2434 cb(&new_codec_mutex, AV_LOCK_DESTROY);
2435 return err > 0 ? AVERROR_UNKNOWN : err;
2438 codec_mutex = new_codec_mutex;
2439 avformat_mutex = new_avformat_mutex;
2445 int avpriv_lock_avformat(void)
2448 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2454 int avpriv_unlock_avformat(void)
2457 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2463 unsigned int avpriv_toupper4(unsigned int x)
2465 return av_toupper(x & 0xFF) +
2466 (av_toupper((x >> 8) & 0xFF) << 8) +
2467 (av_toupper((x >> 16) & 0xFF) << 16) +
2468 (av_toupper((x >> 24) & 0xFF) << 24);
2471 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
2475 dst->owner = src->owner;
2477 ret = av_frame_ref(dst->f, src->f);
2481 if (src->progress &&
2482 !(dst->progress = av_buffer_ref(src->progress))) {
2483 ff_thread_release_buffer(dst->owner, dst);
2484 return AVERROR(ENOMEM);
2492 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
2495 return ff_get_buffer(avctx, f->f, flags);
2498 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
2501 av_frame_unref(f->f);
2504 void ff_thread_finish_setup(AVCodecContext *avctx)
2508 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
2512 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
2518 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
2520 if (codec_id <= AV_CODEC_ID_NONE)
2521 return AVMEDIA_TYPE_UNKNOWN;
2522 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2523 return AVMEDIA_TYPE_VIDEO;
2524 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2525 return AVMEDIA_TYPE_AUDIO;
2526 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2527 return AVMEDIA_TYPE_SUBTITLE;
2529 return AVMEDIA_TYPE_UNKNOWN;
2532 int avcodec_is_open(AVCodecContext *s)
2534 return !!s->internal;
2537 const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
2539 uint32_t * restrict state)
2547 for (i = 0; i < 3; i++) {
2548 uint32_t tmp = *state << 8;
2549 *state = tmp + *(p++);
2550 if (tmp == 0x100 || p == end)
2555 if (p[-1] > 1 ) p += 3;
2556 else if (p[-2] ) p += 2;
2557 else if (p[-3]|(p[-1]-1)) p++;
2564 p = FFMIN(p, end) - 4;
2565 *state = AV_RB32(p);