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"
43 #include "libavutil/opt.h"
46 #include "bytestream.h"
53 static int volatile entangled_thread_counter
= 0;
54 static int (*lockmgr_cb
)(void **mutex
, enum AVLockOp op
);
55 static void *codec_mutex
;
56 static void *avformat_mutex
;
58 #if FF_API_FAST_MALLOC && CONFIG_SHARED && HAVE_SYMVER
59 FF_SYMVER(void*, av_fast_realloc
, (void *ptr
, unsigned int *size
, size_t min_size
), "LIBAVCODEC_55")
61 return av_fast_realloc(ptr
, size
, min_size
);
64 FF_SYMVER(void, av_fast_malloc
, (void *ptr
, unsigned int *size
, size_t min_size
), "LIBAVCODEC_55")
66 av_fast_malloc(ptr
, size
, min_size
);
70 void av_fast_padded_malloc(void *ptr
, unsigned int *size
, size_t min_size
)
73 if (min_size
> SIZE_MAX
- FF_INPUT_BUFFER_PADDING_SIZE
) {
78 av_fast_malloc(p
, size
, min_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
80 memset((uint8_t *)*p
+ min_size
, 0, FF_INPUT_BUFFER_PADDING_SIZE
);
83 /* encoder management */
84 static AVCodec
*first_avcodec
= NULL
;
86 AVCodec
*av_codec_next(const AVCodec
*c
)
94 static av_cold
void avcodec_init(void)
96 static int initialized
= 0;
103 ff_dsputil_static_init();
106 int av_codec_is_encoder(const AVCodec
*codec
)
108 return codec
&& (codec
->encode_sub
|| codec
->encode2
);
111 int av_codec_is_decoder(const AVCodec
*codec
)
113 return codec
&& codec
->decode
;
116 av_cold
void avcodec_register(AVCodec
*codec
)
126 if (codec
->init_static_data
)
127 codec
->init_static_data(codec
);
130 unsigned avcodec_get_edge_width(void)
135 #if FF_API_SET_DIMENSIONS
136 void avcodec_set_dimensions(AVCodecContext
*s
, int width
, int height
)
138 ff_set_dimensions(s
, width
, height
);
142 int ff_set_dimensions(AVCodecContext
*s
, int width
, int height
)
144 int ret
= av_image_check_size(width
, height
, 0, s
);
148 s
->width
= s
->coded_width
= width
;
149 s
->height
= s
->coded_height
= height
;
154 #if HAVE_NEON || ARCH_PPC || HAVE_MMX
155 # define STRIDE_ALIGN 16
157 # define STRIDE_ALIGN 8
160 void avcodec_align_dimensions2(AVCodecContext
*s
, int *width
, int *height
,
161 int linesize_align
[AV_NUM_DATA_POINTERS
])
167 switch (s
->pix_fmt
) {
168 case AV_PIX_FMT_YUV420P
:
169 case AV_PIX_FMT_YUYV422
:
170 case AV_PIX_FMT_UYVY422
:
171 case AV_PIX_FMT_YUV422P
:
172 case AV_PIX_FMT_YUV440P
:
173 case AV_PIX_FMT_YUV444P
:
174 case AV_PIX_FMT_GBRP
:
175 case AV_PIX_FMT_GRAY8
:
176 case AV_PIX_FMT_GRAY16BE
:
177 case AV_PIX_FMT_GRAY16LE
:
178 case AV_PIX_FMT_YUVJ420P
:
179 case AV_PIX_FMT_YUVJ422P
:
180 case AV_PIX_FMT_YUVJ440P
:
181 case AV_PIX_FMT_YUVJ444P
:
182 case AV_PIX_FMT_YUVA420P
:
183 case AV_PIX_FMT_YUVA422P
:
184 case AV_PIX_FMT_YUVA444P
:
185 case AV_PIX_FMT_YUV420P9LE
:
186 case AV_PIX_FMT_YUV420P9BE
:
187 case AV_PIX_FMT_YUV420P10LE
:
188 case AV_PIX_FMT_YUV420P10BE
:
189 case AV_PIX_FMT_YUV422P9LE
:
190 case AV_PIX_FMT_YUV422P9BE
:
191 case AV_PIX_FMT_YUV422P10LE
:
192 case AV_PIX_FMT_YUV422P10BE
:
193 case AV_PIX_FMT_YUV444P9LE
:
194 case AV_PIX_FMT_YUV444P9BE
:
195 case AV_PIX_FMT_YUV444P10LE
:
196 case AV_PIX_FMT_YUV444P10BE
:
197 case AV_PIX_FMT_GBRP9LE
:
198 case AV_PIX_FMT_GBRP9BE
:
199 case AV_PIX_FMT_GBRP10LE
:
200 case AV_PIX_FMT_GBRP10BE
:
201 w_align
= 16; //FIXME assume 16 pixel per macroblock
202 h_align
= 16 * 2; // interlaced needs 2 macroblocks height
204 case AV_PIX_FMT_YUV411P
:
205 case AV_PIX_FMT_UYYVYY411
:
209 case AV_PIX_FMT_YUV410P
:
210 if (s
->codec_id
== AV_CODEC_ID_SVQ1
) {
214 case AV_PIX_FMT_RGB555
:
215 if (s
->codec_id
== AV_CODEC_ID_RPZA
) {
219 case AV_PIX_FMT_PAL8
:
220 case AV_PIX_FMT_BGR8
:
221 case AV_PIX_FMT_RGB8
:
222 if (s
->codec_id
== AV_CODEC_ID_SMC
) {
227 case AV_PIX_FMT_BGR24
:
228 if ((s
->codec_id
== AV_CODEC_ID_MSZH
) ||
229 (s
->codec_id
== AV_CODEC_ID_ZLIB
)) {
240 *width
= FFALIGN(*width
, w_align
);
241 *height
= FFALIGN(*height
, h_align
);
242 if (s
->codec_id
== AV_CODEC_ID_H264
)
243 // some of the optimized chroma MC reads one line too much
246 for (i
= 0; i
< 4; i
++)
247 linesize_align
[i
] = STRIDE_ALIGN
;
250 void avcodec_align_dimensions(AVCodecContext
*s
, int *width
, int *height
)
252 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(s
->pix_fmt
);
253 int chroma_shift
= desc
->log2_chroma_w
;
254 int linesize_align
[AV_NUM_DATA_POINTERS
];
257 avcodec_align_dimensions2(s
, width
, height
, linesize_align
);
258 align
= FFMAX(linesize_align
[0], linesize_align
[3]);
259 linesize_align
[1] <<= chroma_shift
;
260 linesize_align
[2] <<= chroma_shift
;
261 align
= FFMAX3(align
, linesize_align
[1], linesize_align
[2]);
262 *width
= FFALIGN(*width
, align
);
265 int avcodec_fill_audio_frame(AVFrame
*frame
, int nb_channels
,
266 enum AVSampleFormat sample_fmt
, const uint8_t *buf
,
267 int buf_size
, int align
)
269 int ch
, planar
, needed_size
, ret
= 0;
271 needed_size
= av_samples_get_buffer_size(NULL
, nb_channels
,
272 frame
->nb_samples
, sample_fmt
,
274 if (buf_size
< needed_size
)
275 return AVERROR(EINVAL
);
277 planar
= av_sample_fmt_is_planar(sample_fmt
);
278 if (planar
&& nb_channels
> AV_NUM_DATA_POINTERS
) {
279 if (!(frame
->extended_data
= av_mallocz(nb_channels
*
280 sizeof(*frame
->extended_data
))))
281 return AVERROR(ENOMEM
);
283 frame
->extended_data
= frame
->data
;
286 if ((ret
= av_samples_fill_arrays(frame
->extended_data
, &frame
->linesize
[0],
287 buf
, nb_channels
, frame
->nb_samples
,
288 sample_fmt
, align
)) < 0) {
289 if (frame
->extended_data
!= frame
->data
)
290 av_free(frame
->extended_data
);
293 if (frame
->extended_data
!= frame
->data
) {
294 for (ch
= 0; ch
< AV_NUM_DATA_POINTERS
; ch
++)
295 frame
->data
[ch
] = frame
->extended_data
[ch
];
301 static int update_frame_pool(AVCodecContext
*avctx
, AVFrame
*frame
)
303 FramePool
*pool
= avctx
->internal
->pool
;
306 switch (avctx
->codec_type
) {
307 case AVMEDIA_TYPE_VIDEO
: {
310 int w
= frame
->width
;
311 int h
= frame
->height
;
312 int tmpsize
, unaligned
;
314 if (pool
->format
== frame
->format
&&
315 pool
->width
== frame
->width
&& pool
->height
== frame
->height
)
318 avcodec_align_dimensions2(avctx
, &w
, &h
, pool
->stride_align
);
320 if (!(avctx
->flags
& CODEC_FLAG_EMU_EDGE
)) {
326 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
327 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
328 av_image_fill_linesizes(picture
.linesize
, avctx
->pix_fmt
, w
);
329 // increase alignment of w for next try (rhs gives the lowest bit set in w)
333 for (i
= 0; i
< 4; i
++)
334 unaligned
|= picture
.linesize
[i
] % pool
->stride_align
[i
];
337 tmpsize
= av_image_fill_pointers(picture
.data
, avctx
->pix_fmt
, h
,
338 NULL
, picture
.linesize
);
342 for (i
= 0; i
< 3 && picture
.data
[i
+ 1]; i
++)
343 size
[i
] = picture
.data
[i
+ 1] - picture
.data
[i
];
344 size
[i
] = tmpsize
- (picture
.data
[i
] - picture
.data
[0]);
346 for (i
= 0; i
< 4; i
++) {
347 av_buffer_pool_uninit(&pool
->pools
[i
]);
348 pool
->linesize
[i
] = picture
.linesize
[i
];
350 pool
->pools
[i
] = av_buffer_pool_init(size
[i
] + 16, NULL
);
351 if (!pool
->pools
[i
]) {
352 ret
= AVERROR(ENOMEM
);
357 pool
->format
= frame
->format
;
358 pool
->width
= frame
->width
;
359 pool
->height
= frame
->height
;
363 case AVMEDIA_TYPE_AUDIO
: {
364 int ch
= av_get_channel_layout_nb_channels(frame
->channel_layout
);
365 int planar
= av_sample_fmt_is_planar(frame
->format
);
366 int planes
= planar ? ch
: 1;
368 if (pool
->format
== frame
->format
&& pool
->planes
== planes
&&
369 pool
->channels
== ch
&& frame
->nb_samples
== pool
->samples
)
372 av_buffer_pool_uninit(&pool
->pools
[0]);
373 ret
= av_samples_get_buffer_size(&pool
->linesize
[0], ch
,
374 frame
->nb_samples
, frame
->format
, 0);
378 pool
->pools
[0] = av_buffer_pool_init(pool
->linesize
[0], NULL
);
379 if (!pool
->pools
[0]) {
380 ret
= AVERROR(ENOMEM
);
384 pool
->format
= frame
->format
;
385 pool
->planes
= planes
;
387 pool
->samples
= frame
->nb_samples
;
390 default: av_assert0(0);
394 for (i
= 0; i
< 4; i
++)
395 av_buffer_pool_uninit(&pool
->pools
[i
]);
397 pool
->planes
= pool
->channels
= pool
->samples
= 0;
398 pool
->width
= pool
->height
= 0;
402 static int audio_get_buffer(AVCodecContext
*avctx
, AVFrame
*frame
)
404 FramePool
*pool
= avctx
->internal
->pool
;
405 int planes
= pool
->planes
;
408 frame
->linesize
[0] = pool
->linesize
[0];
410 if (planes
> AV_NUM_DATA_POINTERS
) {
411 frame
->extended_data
= av_mallocz(planes
* sizeof(*frame
->extended_data
));
412 frame
->nb_extended_buf
= planes
- AV_NUM_DATA_POINTERS
;
413 frame
->extended_buf
= av_mallocz(frame
->nb_extended_buf
*
414 sizeof(*frame
->extended_buf
));
415 if (!frame
->extended_data
|| !frame
->extended_buf
) {
416 av_freep(&frame
->extended_data
);
417 av_freep(&frame
->extended_buf
);
418 return AVERROR(ENOMEM
);
421 frame
->extended_data
= frame
->data
;
423 for (i
= 0; i
< FFMIN(planes
, AV_NUM_DATA_POINTERS
); i
++) {
424 frame
->buf
[i
] = av_buffer_pool_get(pool
->pools
[0]);
427 frame
->extended_data
[i
] = frame
->data
[i
] = frame
->buf
[i
]->data
;
429 for (i
= 0; i
< frame
->nb_extended_buf
; i
++) {
430 frame
->extended_buf
[i
] = av_buffer_pool_get(pool
->pools
[0]);
431 if (!frame
->extended_buf
[i
])
433 frame
->extended_data
[i
+ AV_NUM_DATA_POINTERS
] = frame
->extended_buf
[i
]->data
;
436 if (avctx
->debug
& FF_DEBUG_BUFFERS
)
437 av_log(avctx
, AV_LOG_DEBUG
, "default_get_buffer called on frame %p", frame
);
441 av_frame_unref(frame
);
442 return AVERROR(ENOMEM
);
445 static int video_get_buffer(AVCodecContext
*s
, AVFrame
*pic
)
447 FramePool
*pool
= s
->internal
->pool
;
448 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(pic
->format
);
449 int pixel_size
= desc
->comp
[0].step_minus1
+ 1;
450 int h_chroma_shift
, v_chroma_shift
;
453 if (pic
->data
[0] != NULL
) {
454 av_log(s
, AV_LOG_ERROR
, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
458 memset(pic
->data
, 0, sizeof(pic
->data
));
459 pic
->extended_data
= pic
->data
;
461 av_pix_fmt_get_chroma_sub_sample(s
->pix_fmt
, &h_chroma_shift
, &v_chroma_shift
);
463 for (i
= 0; i
< 4 && pool
->pools
[i
]; i
++) {
464 const int h_shift
= i
== 0 ?
0 : h_chroma_shift
;
465 const int v_shift
= i
== 0 ?
0 : v_chroma_shift
;
467 pic
->linesize
[i
] = pool
->linesize
[i
];
469 pic
->buf
[i
] = av_buffer_pool_get(pool
->pools
[i
]);
473 // no edge if EDGE EMU or not planar YUV
474 if ((s
->flags
& CODEC_FLAG_EMU_EDGE
) || !pool
->pools
[2])
475 pic
->data
[i
] = pic
->buf
[i
]->data
;
477 pic
->data
[i
] = pic
->buf
[i
]->data
+
478 FFALIGN((pic
->linesize
[i
] * EDGE_WIDTH
>> v_shift
) +
479 (pixel_size
* EDGE_WIDTH
>> h_shift
), pool
->stride_align
[i
]);
482 for (; i
< AV_NUM_DATA_POINTERS
; i
++) {
484 pic
->linesize
[i
] = 0;
486 if (pic
->data
[1] && !pic
->data
[2])
487 avpriv_set_systematic_pal2((uint32_t *)pic
->data
[1], s
->pix_fmt
);
489 if (s
->debug
& FF_DEBUG_BUFFERS
)
490 av_log(s
, AV_LOG_DEBUG
, "default_get_buffer called on pic %p\n", pic
);
495 return AVERROR(ENOMEM
);
498 int avcodec_default_get_buffer2(AVCodecContext
*avctx
, AVFrame
*frame
, int flags
)
502 if ((ret
= update_frame_pool(avctx
, frame
)) < 0)
505 #if FF_API_GET_BUFFER
506 FF_DISABLE_DEPRECATION_WARNINGS
507 frame
->type
= FF_BUFFER_TYPE_INTERNAL
;
508 FF_ENABLE_DEPRECATION_WARNINGS
511 switch (avctx
->codec_type
) {
512 case AVMEDIA_TYPE_VIDEO
:
513 return video_get_buffer(avctx
, frame
);
514 case AVMEDIA_TYPE_AUDIO
:
515 return audio_get_buffer(avctx
, frame
);
521 #if FF_API_GET_BUFFER
522 FF_DISABLE_DEPRECATION_WARNINGS
523 int avcodec_default_get_buffer(AVCodecContext
*avctx
, AVFrame
*frame
)
525 return avcodec_default_get_buffer2(avctx
, frame
, 0);
528 typedef struct CompatReleaseBufPriv
{
529 AVCodecContext avctx
;
531 } CompatReleaseBufPriv
;
533 static void compat_free_buffer(void *opaque
, uint8_t *data
)
535 CompatReleaseBufPriv
*priv
= opaque
;
536 if (priv
->avctx
.release_buffer
)
537 priv
->avctx
.release_buffer(&priv
->avctx
, &priv
->frame
);
541 static void compat_release_buffer(void *opaque
, uint8_t *data
)
543 AVBufferRef
*buf
= opaque
;
544 av_buffer_unref(&buf
);
546 FF_ENABLE_DEPRECATION_WARNINGS
549 int ff_get_buffer(AVCodecContext
*avctx
, AVFrame
*frame
, int flags
)
553 switch (avctx
->codec_type
) {
554 case AVMEDIA_TYPE_VIDEO
:
555 frame
->width
= FFMAX(avctx
->width
, avctx
->coded_width
);
556 frame
->height
= FFMAX(avctx
->height
, avctx
->coded_height
);
557 if (frame
->format
< 0)
558 frame
->format
= avctx
->pix_fmt
;
559 if (!frame
->sample_aspect_ratio
.num
)
560 frame
->sample_aspect_ratio
= avctx
->sample_aspect_ratio
;
562 if ((ret
= av_image_check_size(avctx
->width
, avctx
->height
, 0, avctx
)) < 0)
565 case AVMEDIA_TYPE_AUDIO
:
566 if (!frame
->sample_rate
)
567 frame
->sample_rate
= avctx
->sample_rate
;
568 if (frame
->format
< 0)
569 frame
->format
= avctx
->sample_fmt
;
570 if (!frame
->channel_layout
) {
571 if (avctx
->channel_layout
) {
572 if (av_get_channel_layout_nb_channels(avctx
->channel_layout
) !=
574 av_log(avctx
, AV_LOG_ERROR
, "Inconsistent channel "
576 return AVERROR(EINVAL
);
579 frame
->channel_layout
= avctx
->channel_layout
;
581 if (avctx
->channels
> FF_SANE_NB_CHANNELS
) {
582 av_log(avctx
, AV_LOG_ERROR
, "Too many channels: %d.\n",
584 return AVERROR(ENOSYS
);
587 frame
->channel_layout
= av_get_default_channel_layout(avctx
->channels
);
588 if (!frame
->channel_layout
)
589 frame
->channel_layout
= (1ULL << avctx
->channels
) - 1;
593 default: return AVERROR(EINVAL
);
596 frame
->pkt_pts
= avctx
->internal
->pkt ? avctx
->internal
->pkt
->pts
: AV_NOPTS_VALUE
;
597 frame
->reordered_opaque
= avctx
->reordered_opaque
;
599 #if FF_API_GET_BUFFER
600 FF_DISABLE_DEPRECATION_WARNINGS
602 * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
603 * We wrap each plane in its own AVBuffer. Each of those has a reference to
604 * a dummy AVBuffer as its private data, unreffing it on free.
605 * When all the planes are freed, the dummy buffer's free callback calls
608 if (avctx
->get_buffer
) {
609 CompatReleaseBufPriv
*priv
= NULL
;
610 AVBufferRef
*dummy_buf
= NULL
;
613 if (flags
& AV_GET_BUFFER_FLAG_REF
)
614 frame
->reference
= 1;
616 ret
= avctx
->get_buffer(avctx
, frame
);
620 /* return if the buffers are already set up
621 * this would happen e.g. when a custom get_buffer() calls
622 * avcodec_default_get_buffer
627 priv
= av_mallocz(sizeof(*priv
));
629 ret
= AVERROR(ENOMEM
);
632 priv
->avctx
= *avctx
;
633 priv
->frame
= *frame
;
635 dummy_buf
= av_buffer_create(NULL
, 0, compat_free_buffer
, priv
, 0);
637 ret
= AVERROR(ENOMEM
);
641 #define WRAP_PLANE(ref_out, data, data_size) \
643 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
645 ret = AVERROR(ENOMEM); \
648 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
651 av_frame_unref(frame); \
652 ret = AVERROR(ENOMEM); \
657 if (avctx
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
658 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(frame
->format
);
660 planes
= av_pix_fmt_count_planes(frame
->format
);
661 /* workaround for AVHWAccel plane count of 0, buf[0] is used as
662 check for allocated buffers: make libavcodec happy */
663 if (desc
&& desc
->flags
& AV_PIX_FMT_FLAG_HWACCEL
)
665 if (!desc
|| planes
<= 0) {
666 ret
= AVERROR(EINVAL
);
670 for (i
= 0; i
< planes
; i
++) {
671 int v_shift
= (i
== 1 || i
== 2) ? desc
->log2_chroma_h
: 0;
672 int plane_size
= (frame
->height
>> v_shift
) * frame
->linesize
[i
];
674 WRAP_PLANE(frame
->buf
[i
], frame
->data
[i
], plane_size
);
677 int planar
= av_sample_fmt_is_planar(frame
->format
);
678 planes
= planar ? avctx
->channels
: 1;
680 if (planes
> FF_ARRAY_ELEMS(frame
->buf
)) {
681 frame
->nb_extended_buf
= planes
- FF_ARRAY_ELEMS(frame
->buf
);
682 frame
->extended_buf
= av_malloc(sizeof(*frame
->extended_buf
) *
683 frame
->nb_extended_buf
);
684 if (!frame
->extended_buf
) {
685 ret
= AVERROR(ENOMEM
);
690 for (i
= 0; i
< FFMIN(planes
, FF_ARRAY_ELEMS(frame
->buf
)); i
++)
691 WRAP_PLANE(frame
->buf
[i
], frame
->extended_data
[i
], frame
->linesize
[0]);
693 for (i
= 0; i
< frame
->nb_extended_buf
; i
++)
694 WRAP_PLANE(frame
->extended_buf
[i
],
695 frame
->extended_data
[i
+ FF_ARRAY_ELEMS(frame
->buf
)],
699 av_buffer_unref(&dummy_buf
);
701 frame
->width
= avctx
->width
;
702 frame
->height
= avctx
->height
;
707 avctx
->release_buffer(avctx
, frame
);
709 av_buffer_unref(&dummy_buf
);
712 FF_ENABLE_DEPRECATION_WARNINGS
715 ret
= avctx
->get_buffer2(avctx
, frame
, flags
);
717 if (avctx
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
718 frame
->width
= avctx
->width
;
719 frame
->height
= avctx
->height
;
725 int ff_reget_buffer(AVCodecContext
*avctx
, AVFrame
*frame
)
730 av_assert0(avctx
->codec_type
== AVMEDIA_TYPE_VIDEO
);
733 return ff_get_buffer(avctx
, frame
, AV_GET_BUFFER_FLAG_REF
);
735 if (av_frame_is_writable(frame
))
738 av_frame_move_ref(&tmp
, frame
);
740 ret
= ff_get_buffer(avctx
, frame
, AV_GET_BUFFER_FLAG_REF
);
742 av_frame_unref(&tmp
);
746 av_image_copy(frame
->data
, frame
->linesize
, tmp
.data
, tmp
.linesize
,
747 frame
->format
, frame
->width
, frame
->height
);
749 av_frame_unref(&tmp
);
754 #if FF_API_GET_BUFFER
755 void avcodec_default_release_buffer(AVCodecContext
*s
, AVFrame
*pic
)
760 int avcodec_default_reget_buffer(AVCodecContext
*s
, AVFrame
*pic
)
767 int avcodec_default_execute(AVCodecContext
*c
, int (*func
)(AVCodecContext
*c2
, void *arg2
), void *arg
, int *ret
, int count
, int size
)
771 for (i
= 0; i
< count
; i
++) {
772 int r
= func(c
, (char *)arg
+ i
* size
);
779 int avcodec_default_execute2(AVCodecContext
*c
, int (*func
)(AVCodecContext
*c2
, void *arg2
, int jobnr
, int threadnr
), void *arg
, int *ret
, int count
)
783 for (i
= 0; i
< count
; i
++) {
784 int r
= func(c
, arg
, i
, 0);
791 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt
)
793 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(pix_fmt
);
794 return desc
->flags
& AV_PIX_FMT_FLAG_HWACCEL
;
797 enum AVPixelFormat
avcodec_default_get_format(struct AVCodecContext
*s
, const enum AVPixelFormat
*fmt
)
799 while (*fmt
!= AV_PIX_FMT_NONE
&& is_hwaccel_pix_fmt(*fmt
))
804 void avcodec_get_frame_defaults(AVFrame
*frame
)
806 if (frame
->extended_data
!= frame
->data
)
807 av_freep(&frame
->extended_data
);
809 memset(frame
, 0, sizeof(AVFrame
));
811 frame
->pts
= AV_NOPTS_VALUE
;
812 frame
->key_frame
= 1;
813 frame
->sample_aspect_ratio
= (AVRational
) {0, 1 };
814 frame
->format
= -1; /* unknown */
815 frame
->extended_data
= frame
->data
;
818 #if FF_API_AVFRAME_LAVC
819 AVFrame
*avcodec_alloc_frame(void)
821 AVFrame
*frame
= av_mallocz(sizeof(AVFrame
));
826 avcodec_get_frame_defaults(frame
);
832 void avcodec_free_frame(AVFrame
**frame
)
836 if (!frame
|| !*frame
)
841 if (f
->extended_data
!= f
->data
)
842 av_freep(&f
->extended_data
);
847 int attribute_align_arg
avcodec_open2(AVCodecContext
*avctx
, const AVCodec
*codec
, AVDictionary
**options
)
850 AVDictionary
*tmp
= NULL
;
852 if (avcodec_is_open(avctx
))
855 if ((!codec
&& !avctx
->codec
)) {
856 av_log(avctx
, AV_LOG_ERROR
, "No codec provided to avcodec_open2().\n");
857 return AVERROR(EINVAL
);
859 if ((codec
&& avctx
->codec
&& codec
!= avctx
->codec
)) {
860 av_log(avctx
, AV_LOG_ERROR
, "This AVCodecContext was allocated for %s, "
861 "but %s passed to avcodec_open2().\n", avctx
->codec
->name
, codec
->name
);
862 return AVERROR(EINVAL
);
865 codec
= avctx
->codec
;
867 if (avctx
->extradata_size
< 0 || avctx
->extradata_size
>= FF_MAX_EXTRADATA_SIZE
)
868 return AVERROR(EINVAL
);
871 av_dict_copy(&tmp
, *options
, 0);
873 /* If there is a user-supplied mutex locking routine, call it. */
875 if ((*lockmgr_cb
)(&codec_mutex
, AV_LOCK_OBTAIN
))
879 entangled_thread_counter
++;
880 if (entangled_thread_counter
!= 1) {
881 av_log(avctx
, AV_LOG_ERROR
, "insufficient thread locking around avcodec_open/close()\n");
886 avctx
->internal
= av_mallocz(sizeof(AVCodecInternal
));
887 if (!avctx
->internal
) {
888 ret
= AVERROR(ENOMEM
);
892 avctx
->internal
->pool
= av_mallocz(sizeof(*avctx
->internal
->pool
));
893 if (!avctx
->internal
->pool
) {
894 ret
= AVERROR(ENOMEM
);
898 if (codec
->priv_data_size
> 0) {
899 if (!avctx
->priv_data
) {
900 avctx
->priv_data
= av_mallocz(codec
->priv_data_size
);
901 if (!avctx
->priv_data
) {
902 ret
= AVERROR(ENOMEM
);
905 if (codec
->priv_class
) {
906 *(const AVClass
**)avctx
->priv_data
= codec
->priv_class
;
907 av_opt_set_defaults(avctx
->priv_data
);
910 if (codec
->priv_class
&& (ret
= av_opt_set_dict(avctx
->priv_data
, &tmp
)) < 0)
913 avctx
->priv_data
= NULL
;
915 if ((ret
= av_opt_set_dict(avctx
, &tmp
)) < 0)
918 if (avctx
->coded_width
&& avctx
->coded_height
&& !avctx
->width
&& !avctx
->height
)
919 ret
= ff_set_dimensions(avctx
, avctx
->coded_width
, avctx
->coded_height
);
920 else if (avctx
->width
&& avctx
->height
)
921 ret
= ff_set_dimensions(avctx
, avctx
->width
, avctx
->height
);
925 if ((avctx
->coded_width
|| avctx
->coded_height
|| avctx
->width
|| avctx
->height
)
926 && ( av_image_check_size(avctx
->coded_width
, avctx
->coded_height
, 0, avctx
) < 0
927 || av_image_check_size(avctx
->width
, avctx
->height
, 0, avctx
) < 0)) {
928 av_log(avctx
, AV_LOG_WARNING
, "ignoring invalid width/height values\n");
929 ff_set_dimensions(avctx
, 0, 0);
932 /* if the decoder init function was already called previously,
933 * free the already allocated subtitle_header before overwriting it */
934 if (av_codec_is_decoder(codec
))
935 av_freep(&avctx
->subtitle_header
);
937 if (avctx
->channels
> FF_SANE_NB_CHANNELS
) {
938 ret
= AVERROR(EINVAL
);
942 avctx
->codec
= codec
;
943 if ((avctx
->codec_type
== AVMEDIA_TYPE_UNKNOWN
|| avctx
->codec_type
== codec
->type
) &&
944 avctx
->codec_id
== AV_CODEC_ID_NONE
) {
945 avctx
->codec_type
= codec
->type
;
946 avctx
->codec_id
= codec
->id
;
948 if (avctx
->codec_id
!= codec
->id
|| (avctx
->codec_type
!= codec
->type
949 && avctx
->codec_type
!= AVMEDIA_TYPE_ATTACHMENT
)) {
950 av_log(avctx
, AV_LOG_ERROR
, "codec type or id mismatches\n");
951 ret
= AVERROR(EINVAL
);
954 avctx
->frame_number
= 0;
956 if (avctx
->codec
->capabilities
& CODEC_CAP_EXPERIMENTAL
&&
957 avctx
->strict_std_compliance
> FF_COMPLIANCE_EXPERIMENTAL
) {
958 ret
= AVERROR_EXPERIMENTAL
;
962 if (avctx
->codec_type
== AVMEDIA_TYPE_AUDIO
&&
963 (!avctx
->time_base
.num
|| !avctx
->time_base
.den
)) {
964 avctx
->time_base
.num
= 1;
965 avctx
->time_base
.den
= avctx
->sample_rate
;
969 ret
= ff_thread_init(avctx
);
974 if (!HAVE_THREADS
&& !(codec
->capabilities
& CODEC_CAP_AUTO_THREADS
))
975 avctx
->thread_count
= 1;
977 if (av_codec_is_encoder(avctx
->codec
)) {
979 if (avctx
->codec
->sample_fmts
) {
980 for (i
= 0; avctx
->codec
->sample_fmts
[i
] != AV_SAMPLE_FMT_NONE
; i
++) {
981 if (avctx
->sample_fmt
== avctx
->codec
->sample_fmts
[i
])
983 if (avctx
->channels
== 1 &&
984 av_get_planar_sample_fmt(avctx
->sample_fmt
) ==
985 av_get_planar_sample_fmt(avctx
->codec
->sample_fmts
[i
])) {
986 avctx
->sample_fmt
= avctx
->codec
->sample_fmts
[i
];
990 if (avctx
->codec
->sample_fmts
[i
] == AV_SAMPLE_FMT_NONE
) {
991 av_log(avctx
, AV_LOG_ERROR
, "Specified sample_fmt is not supported.\n");
992 ret
= AVERROR(EINVAL
);
996 if (avctx
->codec
->pix_fmts
) {
997 for (i
= 0; avctx
->codec
->pix_fmts
[i
] != AV_PIX_FMT_NONE
; i
++)
998 if (avctx
->pix_fmt
== avctx
->codec
->pix_fmts
[i
])
1000 if (avctx
->codec
->pix_fmts
[i
] == AV_PIX_FMT_NONE
) {
1001 av_log(avctx
, AV_LOG_ERROR
, "Specified pix_fmt is not supported\n");
1002 ret
= AVERROR(EINVAL
);
1006 if (avctx
->codec
->supported_samplerates
) {
1007 for (i
= 0; avctx
->codec
->supported_samplerates
[i
] != 0; i
++)
1008 if (avctx
->sample_rate
== avctx
->codec
->supported_samplerates
[i
])
1010 if (avctx
->codec
->supported_samplerates
[i
] == 0) {
1011 av_log(avctx
, AV_LOG_ERROR
, "Specified sample_rate is not supported\n");
1012 ret
= AVERROR(EINVAL
);
1016 if (avctx
->codec
->channel_layouts
) {
1017 if (!avctx
->channel_layout
) {
1018 av_log(avctx
, AV_LOG_WARNING
, "channel_layout not specified\n");
1020 for (i
= 0; avctx
->codec
->channel_layouts
[i
] != 0; i
++)
1021 if (avctx
->channel_layout
== avctx
->codec
->channel_layouts
[i
])
1023 if (avctx
->codec
->channel_layouts
[i
] == 0) {
1024 av_log(avctx
, AV_LOG_ERROR
, "Specified channel_layout is not supported\n");
1025 ret
= AVERROR(EINVAL
);
1030 if (avctx
->channel_layout
&& avctx
->channels
) {
1031 if (av_get_channel_layout_nb_channels(avctx
->channel_layout
) != avctx
->channels
) {
1032 av_log(avctx
, AV_LOG_ERROR
, "channel layout does not match number of channels\n");
1033 ret
= AVERROR(EINVAL
);
1036 } else if (avctx
->channel_layout
) {
1037 avctx
->channels
= av_get_channel_layout_nb_channels(avctx
->channel_layout
);
1040 if (!avctx
->rc_initial_buffer_occupancy
)
1041 avctx
->rc_initial_buffer_occupancy
= avctx
->rc_buffer_size
* 3 / 4;
1044 if (avctx
->codec
->init
&& !(avctx
->active_thread_type
& FF_THREAD_FRAME
)) {
1045 ret
= avctx
->codec
->init(avctx
);
1051 if (av_codec_is_decoder(avctx
->codec
)) {
1052 /* validate channel layout from the decoder */
1053 if (avctx
->channel_layout
) {
1054 int channels
= av_get_channel_layout_nb_channels(avctx
->channel_layout
);
1055 if (!avctx
->channels
)
1056 avctx
->channels
= channels
;
1057 else if (channels
!= avctx
->channels
) {
1058 av_log(avctx
, AV_LOG_WARNING
,
1059 "channel layout does not match number of channels\n");
1060 avctx
->channel_layout
= 0;
1063 if (avctx
->channels
&& avctx
->channels
< 0 ||
1064 avctx
->channels
> FF_SANE_NB_CHANNELS
) {
1065 ret
= AVERROR(EINVAL
);
1070 entangled_thread_counter
--;
1072 /* Release any user-supplied mutex. */
1074 (*lockmgr_cb
)(&codec_mutex
, AV_LOCK_RELEASE
);
1077 av_dict_free(options
);
1084 av_freep(&avctx
->priv_data
);
1085 if (avctx
->internal
)
1086 av_freep(&avctx
->internal
->pool
);
1087 av_freep(&avctx
->internal
);
1088 avctx
->codec
= NULL
;
1092 int ff_alloc_packet(AVPacket
*avpkt
, int size
)
1094 if (size
> INT_MAX
- FF_INPUT_BUFFER_PADDING_SIZE
)
1095 return AVERROR(EINVAL
);
1098 AVBufferRef
*buf
= avpkt
->buf
;
1099 #if FF_API_DESTRUCT_PACKET
1100 FF_DISABLE_DEPRECATION_WARNINGS
1101 void *destruct
= avpkt
->destruct
;
1102 FF_ENABLE_DEPRECATION_WARNINGS
1105 if (avpkt
->size
< size
)
1106 return AVERROR(EINVAL
);
1108 av_init_packet(avpkt
);
1109 #if FF_API_DESTRUCT_PACKET
1110 FF_DISABLE_DEPRECATION_WARNINGS
1111 avpkt
->destruct
= destruct
;
1112 FF_ENABLE_DEPRECATION_WARNINGS
1118 return av_new_packet(avpkt
, size
);
1123 * Pad last frame with silence.
1125 static int pad_last_frame(AVCodecContext
*s
, AVFrame
**dst
, const AVFrame
*src
)
1127 AVFrame
*frame
= NULL
;
1130 if (!(frame
= av_frame_alloc()))
1131 return AVERROR(ENOMEM
);
1133 frame
->format
= src
->format
;
1134 frame
->channel_layout
= src
->channel_layout
;
1135 frame
->nb_samples
= s
->frame_size
;
1136 ret
= av_frame_get_buffer(frame
, 32);
1140 ret
= av_frame_copy_props(frame
, src
);
1144 if ((ret
= av_samples_copy(frame
->extended_data
, src
->extended_data
, 0, 0,
1145 src
->nb_samples
, s
->channels
, s
->sample_fmt
)) < 0)
1147 if ((ret
= av_samples_set_silence(frame
->extended_data
, src
->nb_samples
,
1148 frame
->nb_samples
- src
->nb_samples
,
1149 s
->channels
, s
->sample_fmt
)) < 0)
1157 av_frame_free(&frame
);
1161 int attribute_align_arg
avcodec_encode_audio2(AVCodecContext
*avctx
,
1163 const AVFrame
*frame
,
1164 int *got_packet_ptr
)
1167 AVFrame
*padded_frame
= NULL
;
1169 int user_packet
= !!avpkt
->data
;
1171 *got_packet_ptr
= 0;
1173 if (!(avctx
->codec
->capabilities
& CODEC_CAP_DELAY
) && !frame
) {
1174 av_free_packet(avpkt
);
1175 av_init_packet(avpkt
);
1179 /* ensure that extended_data is properly set */
1180 if (frame
&& !frame
->extended_data
) {
1181 if (av_sample_fmt_is_planar(avctx
->sample_fmt
) &&
1182 avctx
->channels
> AV_NUM_DATA_POINTERS
) {
1183 av_log(avctx
, AV_LOG_ERROR
, "Encoding to a planar sample format, "
1184 "with more than %d channels, but extended_data is not set.\n",
1185 AV_NUM_DATA_POINTERS
);
1186 return AVERROR(EINVAL
);
1188 av_log(avctx
, AV_LOG_WARNING
, "extended_data is not set.\n");
1191 tmp
.extended_data
= tmp
.data
;
1195 /* check for valid frame size */
1197 if (avctx
->codec
->capabilities
& CODEC_CAP_SMALL_LAST_FRAME
) {
1198 if (frame
->nb_samples
> avctx
->frame_size
)
1199 return AVERROR(EINVAL
);
1200 } else if (!(avctx
->codec
->capabilities
& CODEC_CAP_VARIABLE_FRAME_SIZE
)) {
1201 if (frame
->nb_samples
< avctx
->frame_size
&&
1202 !avctx
->internal
->last_audio_frame
) {
1203 ret
= pad_last_frame(avctx
, &padded_frame
, frame
);
1207 frame
= padded_frame
;
1208 avctx
->internal
->last_audio_frame
= 1;
1211 if (frame
->nb_samples
!= avctx
->frame_size
) {
1212 ret
= AVERROR(EINVAL
);
1218 ret
= avctx
->codec
->encode2(avctx
, avpkt
, frame
, got_packet_ptr
);
1220 if (*got_packet_ptr
) {
1221 if (!(avctx
->codec
->capabilities
& CODEC_CAP_DELAY
)) {
1222 if (avpkt
->pts
== AV_NOPTS_VALUE
)
1223 avpkt
->pts
= frame
->pts
;
1224 if (!avpkt
->duration
)
1225 avpkt
->duration
= ff_samples_to_time_base(avctx
,
1228 avpkt
->dts
= avpkt
->pts
;
1233 if (!user_packet
&& avpkt
->size
) {
1234 ret
= av_buffer_realloc(&avpkt
->buf
, avpkt
->size
);
1236 avpkt
->data
= avpkt
->buf
->data
;
1239 avctx
->frame_number
++;
1242 if (ret
< 0 || !*got_packet_ptr
) {
1243 av_free_packet(avpkt
);
1244 av_init_packet(avpkt
);
1248 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1249 * this needs to be moved to the encoders, but for now we can do it
1250 * here to simplify things */
1251 avpkt
->flags
|= AV_PKT_FLAG_KEY
;
1254 av_frame_free(&padded_frame
);
1259 int attribute_align_arg
avcodec_encode_video2(AVCodecContext
*avctx
,
1261 const AVFrame
*frame
,
1262 int *got_packet_ptr
)
1265 int user_packet
= !!avpkt
->data
;
1267 *got_packet_ptr
= 0;
1269 if (!(avctx
->codec
->capabilities
& CODEC_CAP_DELAY
) && !frame
) {
1270 av_free_packet(avpkt
);
1271 av_init_packet(avpkt
);
1276 if (av_image_check_size(avctx
->width
, avctx
->height
, 0, avctx
))
1277 return AVERROR(EINVAL
);
1279 av_assert0(avctx
->codec
->encode2
);
1281 ret
= avctx
->codec
->encode2(avctx
, avpkt
, frame
, got_packet_ptr
);
1283 if (!*got_packet_ptr
)
1285 else if (!(avctx
->codec
->capabilities
& CODEC_CAP_DELAY
))
1286 avpkt
->pts
= avpkt
->dts
= frame
->pts
;
1288 if (!user_packet
&& avpkt
->size
) {
1289 ret
= av_buffer_realloc(&avpkt
->buf
, avpkt
->size
);
1291 avpkt
->data
= avpkt
->buf
->data
;
1294 avctx
->frame_number
++;
1297 if (ret
< 0 || !*got_packet_ptr
)
1298 av_free_packet(avpkt
);
1304 int avcodec_encode_subtitle(AVCodecContext
*avctx
, uint8_t *buf
, int buf_size
,
1305 const AVSubtitle
*sub
)
1308 if (sub
->start_display_time
) {
1309 av_log(avctx
, AV_LOG_ERROR
, "start_display_time must be 0.\n");
1312 if (sub
->num_rects
== 0 || !sub
->rects
)
1314 ret
= avctx
->codec
->encode_sub(avctx
, buf
, buf_size
, sub
);
1315 avctx
->frame_number
++;
1319 static int apply_param_change(AVCodecContext
*avctx
, AVPacket
*avpkt
)
1322 const uint8_t *data
;
1325 data
= av_packet_get_side_data(avpkt
, AV_PKT_DATA_PARAM_CHANGE
, &size
);
1329 if (!(avctx
->codec
->capabilities
& CODEC_CAP_PARAM_CHANGE
)) {
1330 av_log(avctx
, AV_LOG_ERROR
, "This decoder does not support parameter "
1331 "changes, but PARAM_CHANGE side data was sent to it.\n");
1332 return AVERROR(EINVAL
);
1338 flags
= bytestream_get_le32(&data
);
1341 if (flags
& AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT
) {
1344 avctx
->channels
= bytestream_get_le32(&data
);
1347 if (flags
& AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT
) {
1350 avctx
->channel_layout
= bytestream_get_le64(&data
);
1353 if (flags
& AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE
) {
1356 avctx
->sample_rate
= bytestream_get_le32(&data
);
1359 if (flags
& AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS
) {
1362 avctx
->width
= bytestream_get_le32(&data
);
1363 avctx
->height
= bytestream_get_le32(&data
);
1365 ret
= ff_set_dimensions(avctx
, avctx
->width
, avctx
->height
);
1372 av_log(avctx
, AV_LOG_ERROR
, "PARAM_CHANGE side data too small.\n");
1373 return AVERROR_INVALIDDATA
;
1376 int attribute_align_arg
avcodec_decode_video2(AVCodecContext
*avctx
, AVFrame
*picture
,
1377 int *got_picture_ptr
,
1380 AVCodecInternal
*avci
= avctx
->internal
;
1383 *got_picture_ptr
= 0;
1384 if ((avctx
->coded_width
|| avctx
->coded_height
) && av_image_check_size(avctx
->coded_width
, avctx
->coded_height
, 0, avctx
))
1387 avctx
->internal
->pkt
= avpkt
;
1388 ret
= apply_param_change(avctx
, avpkt
);
1390 av_log(avctx
, AV_LOG_ERROR
, "Error applying parameter changes.\n");
1391 if (avctx
->err_recognition
& AV_EF_EXPLODE
)
1395 avcodec_get_frame_defaults(picture
);
1397 if (!avctx
->refcounted_frames
)
1398 av_frame_unref(&avci
->to_free
);
1400 if ((avctx
->codec
->capabilities
& CODEC_CAP_DELAY
) || avpkt
->size
|| (avctx
->active_thread_type
& FF_THREAD_FRAME
)) {
1401 if (HAVE_THREADS
&& avctx
->active_thread_type
& FF_THREAD_FRAME
)
1402 ret
= ff_thread_decode_frame(avctx
, picture
, got_picture_ptr
,
1405 ret
= avctx
->codec
->decode(avctx
, picture
, got_picture_ptr
,
1407 picture
->pkt_dts
= avpkt
->dts
;
1408 /* get_buffer is supposed to set frame parameters */
1409 if (!(avctx
->codec
->capabilities
& CODEC_CAP_DR1
)) {
1410 picture
->sample_aspect_ratio
= avctx
->sample_aspect_ratio
;
1411 picture
->width
= avctx
->width
;
1412 picture
->height
= avctx
->height
;
1413 picture
->format
= avctx
->pix_fmt
;
1417 emms_c(); //needed to avoid an emms_c() call before every return;
1419 if (ret
< 0 && picture
->data
[0])
1420 av_frame_unref(picture
);
1422 if (*got_picture_ptr
) {
1423 if (!avctx
->refcounted_frames
) {
1424 avci
->to_free
= *picture
;
1425 avci
->to_free
.extended_data
= avci
->to_free
.data
;
1426 memset(picture
->buf
, 0, sizeof(picture
->buf
));
1429 avctx
->frame_number
++;
1434 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1435 * make sure it's set correctly */
1436 picture
->extended_data
= picture
->data
;
1441 int attribute_align_arg
avcodec_decode_audio4(AVCodecContext
*avctx
,
1446 AVCodecInternal
*avci
= avctx
->internal
;
1447 int planar
, channels
;
1452 avctx
->internal
->pkt
= avpkt
;
1454 if (!avpkt
->data
&& avpkt
->size
) {
1455 av_log(avctx
, AV_LOG_ERROR
, "invalid packet: NULL data, size != 0\n");
1456 return AVERROR(EINVAL
);
1459 ret
= apply_param_change(avctx
, avpkt
);
1461 av_log(avctx
, AV_LOG_ERROR
, "Error applying parameter changes.\n");
1462 if (avctx
->err_recognition
& AV_EF_EXPLODE
)
1466 avcodec_get_frame_defaults(frame
);
1468 if (!avctx
->refcounted_frames
)
1469 av_frame_unref(&avci
->to_free
);
1471 if ((avctx
->codec
->capabilities
& CODEC_CAP_DELAY
) || avpkt
->size
) {
1472 ret
= avctx
->codec
->decode(avctx
, frame
, got_frame_ptr
, avpkt
);
1473 if (ret
>= 0 && *got_frame_ptr
) {
1474 avctx
->frame_number
++;
1475 frame
->pkt_dts
= avpkt
->dts
;
1476 if (frame
->format
== AV_SAMPLE_FMT_NONE
)
1477 frame
->format
= avctx
->sample_fmt
;
1479 if (!avctx
->refcounted_frames
) {
1480 avci
->to_free
= *frame
;
1481 avci
->to_free
.extended_data
= avci
->to_free
.data
;
1482 memset(frame
->buf
, 0, sizeof(frame
->buf
));
1483 frame
->extended_buf
= NULL
;
1484 frame
->nb_extended_buf
= 0;
1486 } else if (frame
->data
[0])
1487 av_frame_unref(frame
);
1490 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1491 * make sure it's set correctly; assume decoders that actually use
1492 * extended_data are doing it correctly */
1493 planar
= av_sample_fmt_is_planar(frame
->format
);
1494 channels
= av_get_channel_layout_nb_channels(frame
->channel_layout
);
1495 if (!(planar
&& channels
> AV_NUM_DATA_POINTERS
))
1496 frame
->extended_data
= frame
->data
;
1501 int avcodec_decode_subtitle2(AVCodecContext
*avctx
, AVSubtitle
*sub
,
1507 avctx
->internal
->pkt
= avpkt
;
1509 ret
= avctx
->codec
->decode(avctx
, sub
, got_sub_ptr
, avpkt
);
1511 avctx
->frame_number
++;
1515 void avsubtitle_free(AVSubtitle
*sub
)
1519 for (i
= 0; i
< sub
->num_rects
; i
++) {
1520 av_freep(&sub
->rects
[i
]->pict
.data
[0]);
1521 av_freep(&sub
->rects
[i
]->pict
.data
[1]);
1522 av_freep(&sub
->rects
[i
]->pict
.data
[2]);
1523 av_freep(&sub
->rects
[i
]->pict
.data
[3]);
1524 av_freep(&sub
->rects
[i
]->text
);
1525 av_freep(&sub
->rects
[i
]->ass
);
1526 av_freep(&sub
->rects
[i
]);
1529 av_freep(&sub
->rects
);
1531 memset(sub
, 0, sizeof(AVSubtitle
));
1534 av_cold
int avcodec_close(AVCodecContext
*avctx
)
1536 /* If there is a user-supplied mutex locking routine, call it. */
1538 if ((*lockmgr_cb
)(&codec_mutex
, AV_LOCK_OBTAIN
))
1542 entangled_thread_counter
++;
1543 if (entangled_thread_counter
!= 1) {
1544 av_log(avctx
, AV_LOG_ERROR
, "insufficient thread locking around avcodec_open/close()\n");
1545 entangled_thread_counter
--;
1549 if (avcodec_is_open(avctx
)) {
1550 FramePool
*pool
= avctx
->internal
->pool
;
1552 if (HAVE_THREADS
&& avctx
->internal
->thread_ctx
)
1553 ff_thread_free(avctx
);
1554 if (avctx
->codec
&& avctx
->codec
->close
)
1555 avctx
->codec
->close(avctx
);
1556 avctx
->coded_frame
= NULL
;
1557 if (!avctx
->refcounted_frames
)
1558 av_frame_unref(&avctx
->internal
->to_free
);
1559 for (i
= 0; i
< FF_ARRAY_ELEMS(pool
->pools
); i
++)
1560 av_buffer_pool_uninit(&pool
->pools
[i
]);
1561 av_freep(&avctx
->internal
->pool
);
1562 av_freep(&avctx
->internal
);
1565 if (avctx
->priv_data
&& avctx
->codec
&& avctx
->codec
->priv_class
)
1566 av_opt_free(avctx
->priv_data
);
1568 av_freep(&avctx
->priv_data
);
1569 if (av_codec_is_encoder(avctx
->codec
))
1570 av_freep(&avctx
->extradata
);
1571 avctx
->codec
= NULL
;
1572 avctx
->active_thread_type
= 0;
1573 entangled_thread_counter
--;
1575 /* Release any user-supplied mutex. */
1577 (*lockmgr_cb
)(&codec_mutex
, AV_LOCK_RELEASE
);
1582 static AVCodec
*find_encdec(enum AVCodecID id
, int encoder
)
1584 AVCodec
*p
, *experimental
= NULL
;
1587 if ((encoder ?
av_codec_is_encoder(p
) : av_codec_is_decoder(p
)) &&
1589 if (p
->capabilities
& CODEC_CAP_EXPERIMENTAL
&& !experimental
) {
1596 return experimental
;
1599 AVCodec
*avcodec_find_encoder(enum AVCodecID id
)
1601 return find_encdec(id
, 1);
1604 AVCodec
*avcodec_find_encoder_by_name(const char *name
)
1611 if (av_codec_is_encoder(p
) && strcmp(name
, p
->name
) == 0)
1618 AVCodec
*avcodec_find_decoder(enum AVCodecID id
)
1620 return find_encdec(id
, 0);
1623 AVCodec
*avcodec_find_decoder_by_name(const char *name
)
1630 if (av_codec_is_decoder(p
) && strcmp(name
, p
->name
) == 0)
1637 static int get_bit_rate(AVCodecContext
*ctx
)
1640 int bits_per_sample
;
1642 switch (ctx
->codec_type
) {
1643 case AVMEDIA_TYPE_VIDEO
:
1644 case AVMEDIA_TYPE_DATA
:
1645 case AVMEDIA_TYPE_SUBTITLE
:
1646 case AVMEDIA_TYPE_ATTACHMENT
:
1647 bit_rate
= ctx
->bit_rate
;
1649 case AVMEDIA_TYPE_AUDIO
:
1650 bits_per_sample
= av_get_bits_per_sample(ctx
->codec_id
);
1651 bit_rate
= bits_per_sample ? ctx
->sample_rate
* ctx
->channels
* bits_per_sample
: ctx
->bit_rate
;
1660 size_t av_get_codec_tag_string(char *buf
, size_t buf_size
, unsigned int codec_tag
)
1662 int i
, len
, ret
= 0;
1664 #define TAG_PRINT(x) \
1665 (((x) >= '0' && (x) <= '9') || \
1666 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
1667 ((x) == '.' || (x) == ' '))
1669 for (i
= 0; i
< 4; i
++) {
1670 len
= snprintf(buf
, buf_size
,
1671 TAG_PRINT(codec_tag
& 0xFF) ?
"%c" : "[%d]", codec_tag
& 0xFF);
1673 buf_size
= buf_size
> len ? buf_size
- len
: 0;
1680 void avcodec_string(char *buf
, int buf_size
, AVCodecContext
*enc
, int encode
)
1682 const char *codec_name
;
1683 const char *profile
= NULL
;
1687 AVRational display_aspect_ratio
;
1692 p
= avcodec_find_encoder(enc
->codec_id
);
1694 p
= avcodec_find_decoder(enc
->codec_id
);
1697 codec_name
= p
->name
;
1698 profile
= av_get_profile_name(p
, enc
->profile
);
1699 } else if (enc
->codec_id
== AV_CODEC_ID_MPEG2TS
) {
1700 /* fake mpeg2 transport stream codec (currently not
1702 codec_name
= "mpeg2ts";
1703 } else if (enc
->codec_name
[0] != '\0') {
1704 codec_name
= enc
->codec_name
;
1706 /* output avi tags */
1708 av_get_codec_tag_string(tag_buf
, sizeof(tag_buf
), enc
->codec_tag
);
1709 snprintf(buf1
, sizeof(buf1
), "%s / 0x%04X", tag_buf
, enc
->codec_tag
);
1713 switch (enc
->codec_type
) {
1714 case AVMEDIA_TYPE_VIDEO
:
1715 snprintf(buf
, buf_size
,
1717 codec_name
, enc
->mb_decision ?
" (hq)" : "");
1719 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
1721 if (enc
->pix_fmt
!= AV_PIX_FMT_NONE
) {
1722 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
1724 av_get_pix_fmt_name(enc
->pix_fmt
));
1727 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
1729 enc
->width
, enc
->height
);
1730 if (enc
->sample_aspect_ratio
.num
) {
1731 av_reduce(&display_aspect_ratio
.num
, &display_aspect_ratio
.den
,
1732 enc
->width
* enc
->sample_aspect_ratio
.num
,
1733 enc
->height
* enc
->sample_aspect_ratio
.den
,
1735 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
1736 " [PAR %d:%d DAR %d:%d]",
1737 enc
->sample_aspect_ratio
.num
, enc
->sample_aspect_ratio
.den
,
1738 display_aspect_ratio
.num
, display_aspect_ratio
.den
);
1740 if (av_log_get_level() >= AV_LOG_DEBUG
) {
1741 int g
= av_gcd(enc
->time_base
.num
, enc
->time_base
.den
);
1742 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
1744 enc
->time_base
.num
/ g
, enc
->time_base
.den
/ g
);
1748 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
1749 ", q=%d-%d", enc
->qmin
, enc
->qmax
);
1752 case AVMEDIA_TYPE_AUDIO
:
1753 snprintf(buf
, buf_size
,
1757 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
1759 if (enc
->sample_rate
) {
1760 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
1761 ", %d Hz", enc
->sample_rate
);
1763 av_strlcat(buf
, ", ", buf_size
);
1764 av_get_channel_layout_string(buf
+ strlen(buf
), buf_size
- strlen(buf
), enc
->channels
, enc
->channel_layout
);
1765 if (enc
->sample_fmt
!= AV_SAMPLE_FMT_NONE
) {
1766 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
1767 ", %s", av_get_sample_fmt_name(enc
->sample_fmt
));
1770 case AVMEDIA_TYPE_DATA
:
1771 snprintf(buf
, buf_size
, "Data: %s", codec_name
);
1773 case AVMEDIA_TYPE_SUBTITLE
:
1774 snprintf(buf
, buf_size
, "Subtitle: %s", codec_name
);
1776 case AVMEDIA_TYPE_ATTACHMENT
:
1777 snprintf(buf
, buf_size
, "Attachment: %s", codec_name
);
1780 snprintf(buf
, buf_size
, "Invalid Codec type %d", enc
->codec_type
);
1784 if (enc
->flags
& CODEC_FLAG_PASS1
)
1785 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
1787 if (enc
->flags
& CODEC_FLAG_PASS2
)
1788 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
1791 bitrate
= get_bit_rate(enc
);
1793 snprintf(buf
+ strlen(buf
), buf_size
- strlen(buf
),
1794 ", %d kb/s", bitrate
/ 1000);
1798 const char *av_get_profile_name(const AVCodec
*codec
, int profile
)
1801 if (profile
== FF_PROFILE_UNKNOWN
|| !codec
->profiles
)
1804 for (p
= codec
->profiles
; p
->profile
!= FF_PROFILE_UNKNOWN
; p
++)
1805 if (p
->profile
== profile
)
1811 unsigned avcodec_version(void)
1813 return LIBAVCODEC_VERSION_INT
;
1816 const char *avcodec_configuration(void)
1818 return LIBAV_CONFIGURATION
;
1821 const char *avcodec_license(void)
1823 #define LICENSE_PREFIX "libavcodec license: "
1824 return LICENSE_PREFIX LIBAV_LICENSE
+ sizeof(LICENSE_PREFIX
) - 1;
1827 void avcodec_flush_buffers(AVCodecContext
*avctx
)
1829 if (HAVE_THREADS
&& avctx
->active_thread_type
& FF_THREAD_FRAME
)
1830 ff_thread_flush(avctx
);
1831 else if (avctx
->codec
->flush
)
1832 avctx
->codec
->flush(avctx
);
1834 if (!avctx
->refcounted_frames
)
1835 av_frame_unref(&avctx
->internal
->to_free
);
1838 int av_get_exact_bits_per_sample(enum AVCodecID codec_id
)
1841 case AV_CODEC_ID_ADPCM_CT
:
1842 case AV_CODEC_ID_ADPCM_IMA_APC
:
1843 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD
:
1844 case AV_CODEC_ID_ADPCM_IMA_WS
:
1845 case AV_CODEC_ID_ADPCM_G722
:
1846 case AV_CODEC_ID_ADPCM_YAMAHA
:
1848 case AV_CODEC_ID_PCM_ALAW
:
1849 case AV_CODEC_ID_PCM_MULAW
:
1850 case AV_CODEC_ID_PCM_S8
:
1851 case AV_CODEC_ID_PCM_U8
:
1852 case AV_CODEC_ID_PCM_ZORK
:
1854 case AV_CODEC_ID_PCM_S16BE
:
1855 case AV_CODEC_ID_PCM_S16LE
:
1856 case AV_CODEC_ID_PCM_S16LE_PLANAR
:
1857 case AV_CODEC_ID_PCM_U16BE
:
1858 case AV_CODEC_ID_PCM_U16LE
:
1860 case AV_CODEC_ID_PCM_S24DAUD
:
1861 case AV_CODEC_ID_PCM_S24BE
:
1862 case AV_CODEC_ID_PCM_S24LE
:
1863 case AV_CODEC_ID_PCM_S24LE_PLANAR
:
1864 case AV_CODEC_ID_PCM_U24BE
:
1865 case AV_CODEC_ID_PCM_U24LE
:
1867 case AV_CODEC_ID_PCM_S32BE
:
1868 case AV_CODEC_ID_PCM_S32LE
:
1869 case AV_CODEC_ID_PCM_S32LE_PLANAR
:
1870 case AV_CODEC_ID_PCM_U32BE
:
1871 case AV_CODEC_ID_PCM_U32LE
:
1872 case AV_CODEC_ID_PCM_F32BE
:
1873 case AV_CODEC_ID_PCM_F32LE
:
1875 case AV_CODEC_ID_PCM_F64BE
:
1876 case AV_CODEC_ID_PCM_F64LE
:
1883 int av_get_bits_per_sample(enum AVCodecID codec_id
)
1886 case AV_CODEC_ID_ADPCM_SBPRO_2
:
1888 case AV_CODEC_ID_ADPCM_SBPRO_3
:
1890 case AV_CODEC_ID_ADPCM_SBPRO_4
:
1891 case AV_CODEC_ID_ADPCM_IMA_WAV
:
1892 case AV_CODEC_ID_ADPCM_IMA_QT
:
1893 case AV_CODEC_ID_ADPCM_SWF
:
1894 case AV_CODEC_ID_ADPCM_MS
:
1897 return av_get_exact_bits_per_sample(codec_id
);
1901 int av_get_audio_frame_duration(AVCodecContext
*avctx
, int frame_bytes
)
1903 int id
, sr
, ch
, ba
, tag
, bps
;
1905 id
= avctx
->codec_id
;
1906 sr
= avctx
->sample_rate
;
1907 ch
= avctx
->channels
;
1908 ba
= avctx
->block_align
;
1909 tag
= avctx
->codec_tag
;
1910 bps
= av_get_exact_bits_per_sample(avctx
->codec_id
);
1912 /* codecs with an exact constant bits per sample */
1913 if (bps
> 0 && ch
> 0 && frame_bytes
> 0)
1914 return (frame_bytes
* 8) / (bps
* ch
);
1915 bps
= avctx
->bits_per_coded_sample
;
1917 /* codecs with a fixed packet duration */
1919 case AV_CODEC_ID_ADPCM_ADX
: return 32;
1920 case AV_CODEC_ID_ADPCM_IMA_QT
: return 64;
1921 case AV_CODEC_ID_ADPCM_EA_XAS
: return 128;
1922 case AV_CODEC_ID_AMR_NB
:
1923 case AV_CODEC_ID_GSM
:
1924 case AV_CODEC_ID_QCELP
:
1925 case AV_CODEC_ID_RA_144
:
1926 case AV_CODEC_ID_RA_288
: return 160;
1927 case AV_CODEC_ID_IMC
: return 256;
1928 case AV_CODEC_ID_AMR_WB
:
1929 case AV_CODEC_ID_GSM_MS
: return 320;
1930 case AV_CODEC_ID_MP1
: return 384;
1931 case AV_CODEC_ID_ATRAC1
: return 512;
1932 case AV_CODEC_ID_ATRAC3
: return 1024;
1933 case AV_CODEC_ID_MP2
:
1934 case AV_CODEC_ID_MUSEPACK7
: return 1152;
1935 case AV_CODEC_ID_AC3
: return 1536;
1939 /* calc from sample rate */
1940 if (id
== AV_CODEC_ID_TTA
)
1941 return 256 * sr
/ 245;
1944 /* calc from sample rate and channels */
1945 if (id
== AV_CODEC_ID_BINKAUDIO_DCT
)
1946 return (480 << (sr
/ 22050)) / ch
;
1951 /* calc from block_align */
1952 if (id
== AV_CODEC_ID_SIPR
) {
1954 case 20: return 160;
1955 case 19: return 144;
1956 case 29: return 288;
1957 case 37: return 480;
1959 } else if (id
== AV_CODEC_ID_ILBC
) {
1961 case 38: return 160;
1962 case 50: return 240;
1967 if (frame_bytes
> 0) {
1968 /* calc from frame_bytes only */
1969 if (id
== AV_CODEC_ID_TRUESPEECH
)
1970 return 240 * (frame_bytes
/ 32);
1971 if (id
== AV_CODEC_ID_NELLYMOSER
)
1972 return 256 * (frame_bytes
/ 64);
1975 /* calc from frame_bytes and bits_per_coded_sample */
1976 if (id
== AV_CODEC_ID_ADPCM_G726
)
1977 return frame_bytes
* 8 / bps
;
1981 /* calc from frame_bytes and channels */
1983 case AV_CODEC_ID_ADPCM_4XM
:
1984 case AV_CODEC_ID_ADPCM_IMA_ISS
:
1985 return (frame_bytes
- 4 * ch
) * 2 / ch
;
1986 case AV_CODEC_ID_ADPCM_IMA_SMJPEG
:
1987 return (frame_bytes
- 4) * 2 / ch
;
1988 case AV_CODEC_ID_ADPCM_IMA_AMV
:
1989 return (frame_bytes
- 8) * 2 / ch
;
1990 case AV_CODEC_ID_ADPCM_XA
:
1991 return (frame_bytes
/ 128) * 224 / ch
;
1992 case AV_CODEC_ID_INTERPLAY_DPCM
:
1993 return (frame_bytes
- 6 - ch
) / ch
;
1994 case AV_CODEC_ID_ROQ_DPCM
:
1995 return (frame_bytes
- 8) / ch
;
1996 case AV_CODEC_ID_XAN_DPCM
:
1997 return (frame_bytes
- 2 * ch
) / ch
;
1998 case AV_CODEC_ID_MACE3
:
1999 return 3 * frame_bytes
/ ch
;
2000 case AV_CODEC_ID_MACE6
:
2001 return 6 * frame_bytes
/ ch
;
2002 case AV_CODEC_ID_PCM_LXF
:
2003 return 2 * (frame_bytes
/ (5 * ch
));
2007 /* calc from frame_bytes, channels, and codec_tag */
2008 if (id
== AV_CODEC_ID_SOL_DPCM
) {
2010 return frame_bytes
/ ch
;
2012 return frame_bytes
* 2 / ch
;
2017 /* calc from frame_bytes, channels, and block_align */
2018 int blocks
= frame_bytes
/ ba
;
2019 switch (avctx
->codec_id
) {
2020 case AV_CODEC_ID_ADPCM_IMA_WAV
:
2021 return blocks
* (1 + (ba
- 4 * ch
) / (4 * ch
) * 8);
2022 case AV_CODEC_ID_ADPCM_IMA_DK3
:
2023 return blocks
* (((ba
- 16) * 2 / 3 * 4) / ch
);
2024 case AV_CODEC_ID_ADPCM_IMA_DK4
:
2025 return blocks
* (1 + (ba
- 4 * ch
) * 2 / ch
);
2026 case AV_CODEC_ID_ADPCM_MS
:
2027 return blocks
* (2 + (ba
- 7 * ch
) * 2 / ch
);
2032 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2033 switch (avctx
->codec_id
) {
2034 case AV_CODEC_ID_PCM_DVD
:
2035 return 2 * (frame_bytes
/ ((bps
* 2 / 8) * ch
));
2036 case AV_CODEC_ID_PCM_BLURAY
:
2037 return frame_bytes
/ ((FFALIGN(ch
, 2) * bps
) / 8);
2038 case AV_CODEC_ID_S302M
:
2039 return 2 * (frame_bytes
/ ((bps
+ 4) / 4)) / ch
;
2049 int ff_thread_init(AVCodecContext
*s
)
2056 unsigned int av_xiphlacing(unsigned char *s
, unsigned int v
)
2070 int ff_match_2uint16(const uint16_t(*tab
)[2], int size
, int a
, int b
)
2073 for (i
= 0; i
< size
&& !(tab
[i
][0] == a
&& tab
[i
][1] == b
); i
++) ;
2077 #if FF_API_MISSING_SAMPLE
2078 FF_DISABLE_DEPRECATION_WARNINGS
2079 void av_log_missing_feature(void *avc
, const char *feature
, int want_sample
)
2081 av_log(avc
, AV_LOG_WARNING
, "%s is not implemented. Update your Libav "
2082 "version to the newest one from Git. If the problem still "
2083 "occurs, it means that your file has a feature which has not "
2084 "been implemented.\n", feature
);
2086 av_log_ask_for_sample(avc
, NULL
);
2089 void av_log_ask_for_sample(void *avc
, const char *msg
, ...)
2091 va_list argument_list
;
2093 va_start(argument_list
, msg
);
2096 av_vlog(avc
, AV_LOG_WARNING
, msg
, argument_list
);
2097 av_log(avc
, AV_LOG_WARNING
, "If you want to help, upload a sample "
2098 "of this file to ftp://upload.libav.org/incoming/ "
2099 "and contact the libav-devel mailing list.\n");
2101 va_end(argument_list
);
2103 FF_ENABLE_DEPRECATION_WARNINGS
2104 #endif /* FF_API_MISSING_SAMPLE */
2106 static AVHWAccel
*first_hwaccel
= NULL
;
2108 void av_register_hwaccel(AVHWAccel
*hwaccel
)
2110 AVHWAccel
**p
= &first_hwaccel
;
2114 hwaccel
->next
= NULL
;
2117 AVHWAccel
*av_hwaccel_next(AVHWAccel
*hwaccel
)
2119 return hwaccel ? hwaccel
->next
: first_hwaccel
;
2122 AVHWAccel
*ff_find_hwaccel(AVCodecContext
*avctx
)
2124 enum AVCodecID codec_id
= avctx
->codec
->id
;
2125 enum AVPixelFormat pix_fmt
= avctx
->pix_fmt
;
2127 AVHWAccel
*hwaccel
= NULL
;
2129 while ((hwaccel
= av_hwaccel_next(hwaccel
)))
2130 if (hwaccel
->id
== codec_id
2131 && hwaccel
->pix_fmt
== pix_fmt
)
2136 int av_lockmgr_register(int (*cb
)(void **mutex
, enum AVLockOp op
))
2139 if (lockmgr_cb(&codec_mutex
, AV_LOCK_DESTROY
))
2141 if (lockmgr_cb(&avformat_mutex
, AV_LOCK_DESTROY
))
2148 if (lockmgr_cb(&codec_mutex
, AV_LOCK_CREATE
))
2150 if (lockmgr_cb(&avformat_mutex
, AV_LOCK_CREATE
))
2156 int avpriv_lock_avformat(void)
2159 if ((*lockmgr_cb
)(&avformat_mutex
, AV_LOCK_OBTAIN
))
2165 int avpriv_unlock_avformat(void)
2168 if ((*lockmgr_cb
)(&avformat_mutex
, AV_LOCK_RELEASE
))
2174 unsigned int avpriv_toupper4(unsigned int x
)
2176 return av_toupper(x
& 0xFF) +
2177 (av_toupper((x
>> 8) & 0xFF) << 8) +
2178 (av_toupper((x
>> 16) & 0xFF) << 16) +
2179 (av_toupper((x
>> 24) & 0xFF) << 24);
2182 int ff_thread_ref_frame(ThreadFrame
*dst
, ThreadFrame
*src
)
2186 dst
->owner
= src
->owner
;
2188 ret
= av_frame_ref(dst
->f
, src
->f
);
2192 if (src
->progress
&&
2193 !(dst
->progress
= av_buffer_ref(src
->progress
))) {
2194 ff_thread_release_buffer(dst
->owner
, dst
);
2195 return AVERROR(ENOMEM
);
2203 int ff_thread_get_buffer(AVCodecContext
*avctx
, ThreadFrame
*f
, int flags
)
2206 return ff_get_buffer(avctx
, f
->f
, flags
);
2209 void ff_thread_release_buffer(AVCodecContext
*avctx
, ThreadFrame
*f
)
2211 av_frame_unref(f
->f
);
2214 void ff_thread_finish_setup(AVCodecContext
*avctx
)
2218 void ff_thread_report_progress(ThreadFrame
*f
, int progress
, int field
)
2222 void ff_thread_await_progress(ThreadFrame
*f
, int progress
, int field
)
2228 enum AVMediaType
avcodec_get_type(enum AVCodecID codec_id
)
2230 if (codec_id
<= AV_CODEC_ID_NONE
)
2231 return AVMEDIA_TYPE_UNKNOWN
;
2232 else if (codec_id
< AV_CODEC_ID_FIRST_AUDIO
)
2233 return AVMEDIA_TYPE_VIDEO
;
2234 else if (codec_id
< AV_CODEC_ID_FIRST_SUBTITLE
)
2235 return AVMEDIA_TYPE_AUDIO
;
2236 else if (codec_id
< AV_CODEC_ID_FIRST_UNKNOWN
)
2237 return AVMEDIA_TYPE_SUBTITLE
;
2239 return AVMEDIA_TYPE_UNKNOWN
;
2242 int avcodec_is_open(AVCodecContext
*s
)
2244 return !!s
->internal
;
2247 const uint8_t *avpriv_find_start_code(const uint8_t *restrict p
,
2249 uint32_t * restrict state
)
2257 for (i
= 0; i
< 3; i
++) {
2258 uint32_t tmp
= *state
<< 8;
2259 *state
= tmp
+ *(p
++);
2260 if (tmp
== 0x100 || p
== end
)
2265 if (p
[-1] > 1 ) p
+= 3;
2266 else if (p
[-2] ) p
+= 2;
2267 else if (p
[-3]|(p
[-1]-1)) p
++;
2274 p
= FFMIN(p
, end
) - 4;
2275 *state
= AV_RB32(p
);