utils: fix avcodec_flush_buffers pre-reference counting compatibility
[libav.git] / libavcodec / utils.c
CommitLineData
de6d9b64
FB
1/*
2 * utils for libavcodec
406792e7 3 * Copyright (c) 2001 Fabrice Bellard
8f2ab833 4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
de6d9b64 5 *
2912e87a 6 * This file is part of Libav.
b78e7197 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
ff4ec49e
FB
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 12 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
de6d9b64 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
de6d9b64 17 *
ff4ec49e 18 * You should have received a copy of the GNU Lesser General Public
2912e87a 19 * License along with Libav; if not, write to the Free Software
5509bffa 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 21 */
115329f1 22
983e3246 23/**
ba87f080 24 * @file
983e3246
MN
25 * utils.
26 */
115329f1 27
c6507946 28#include "config.h"
6fee1b90 29#include "libavutil/attributes.h"
b2c75b6e 30#include "libavutil/avassert.h"
7fb94406 31#include "libavutil/avstring.h"
a903f8f0 32#include "libavutil/channel_layout.h"
245976da 33#include "libavutil/crc.h"
759001c5 34#include "libavutil/frame.h"
0ebcdf5c 35#include "libavutil/mathematics.h"
eb285cfe 36#include "libavutil/pixdesc.h"
737eb597
RT
37#include "libavutil/imgutils.h"
38#include "libavutil/samplefmt.h"
0b950fe2 39#include "libavutil/dict.h"
de6d9b64 40#include "avcodec.h"
3123dd79 41#include "dsputil.h"
6ed04040 42#include "libavutil/opt.h"
37b00b47 43#include "thread.h"
0ba39dd1 44#include "internal.h"
f13db94d 45#include "bytestream.h"
f099d3d1 46#include "version.h"
7246177d 47#include <stdlib.h>
9b879566 48#include <stdarg.h>
4c263142 49#include <limits.h>
860a40c8 50#include <float.h>
de6d9b64 51
419ffb23 52static int volatile entangled_thread_counter = 0;
088f38a4 53static int (*lockmgr_cb)(void **mutex, enum AVLockOp op);
f988ce6c 54static void *codec_mutex;
2d1b6fb7 55static void *avformat_mutex;
ddebfb15 56
3453a231 57void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
8e1e6f31 58{
419ffb23 59 if (min_size < *size)
8e1e6f31 60 return ptr;
115329f1 61
419ffb23 62 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
8e1e6f31 63
419ffb23
LB
64 ptr = av_realloc(ptr, min_size);
65 /* we could set this to the unmodified min_size but this is safer
66 * if the user lost the ptr and uses NULL now
67 */
68 if (!ptr)
69 min_size = 0;
372c3f82 70
419ffb23 71 *size = min_size;
978805b2
MN
72
73 return ptr;
8e1e6f31
FB
74}
75
3453a231 76void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
238ef6da
RD
77{
78 void **p = ptr;
79 if (min_size < *size)
80 return;
419ffb23 81 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
238ef6da 82 av_free(*p);
372c3f82 83 *p = av_malloc(min_size);
419ffb23
LB
84 if (!*p)
85 min_size = 0;
86 *size = min_size;
238ef6da
RD
87}
88
316fc744
JG
89void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
90{
91 void **p = ptr;
92 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
93 av_freep(p);
94 *size = 0;
95 return;
96 }
97 av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
98 if (*size)
99 memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
100}
101
de6d9b64 102/* encoder management */
e6df765e 103static AVCodec *first_avcodec = NULL;
de6d9b64 104
0a0f19b5
AK
105AVCodec *av_codec_next(const AVCodec *c)
106{
419ffb23
LB
107 if (c)
108 return c->next;
109 else
110 return first_avcodec;
55b9e69a
MN
111}
112
6fee1b90 113static av_cold void avcodec_init(void)
9ecfbb3e
AK
114{
115 static int initialized = 0;
116
117 if (initialized != 0)
118 return;
119 initialized = 1;
120
b93b27ed
RB
121 if (CONFIG_DSPUTIL)
122 ff_dsputil_static_init();
9ecfbb3e
AK
123}
124
0a0f19b5 125int av_codec_is_encoder(const AVCodec *codec)
b2c75b6e 126{
466b39ef 127 return codec && (codec->encode_sub || codec->encode2);
b2c75b6e
JR
128}
129
0a0f19b5 130int av_codec_is_decoder(const AVCodec *codec)
b2c75b6e
JR
131{
132 return codec && codec->decode;
133}
134
6fee1b90 135av_cold void avcodec_register(AVCodec *codec)
de6d9b64
FB
136{
137 AVCodec **p;
7a961a46 138 avcodec_init();
de6d9b64 139 p = &first_avcodec;
419ffb23
LB
140 while (*p != NULL)
141 p = &(*p)->next;
142 *p = codec;
335a761a 143 codec->next = NULL;
d97efd7f
AK
144
145 if (codec->init_static_data)
146 codec->init_static_data(codec);
de6d9b64
FB
147}
148
0fb49b59
BB
149unsigned avcodec_get_edge_width(void)
150{
151 return EDGE_WIDTH;
152}
153
419ffb23
LB
154void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
155{
156 s->coded_width = width;
157 s->coded_height = height;
158 s->width = width;
159 s->height = height;
21adafec
MN
160}
161
c6507946
DB
162#if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
163# define STRIDE_ALIGN 16
164#else
165# define STRIDE_ALIGN 8
166#endif
167
560f773c
JR
168void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
169 int linesize_align[AV_NUM_DATA_POINTERS])
170{
171 int i;
419ffb23
LB
172 int w_align = 1;
173 int h_align = 1;
115329f1 174
419ffb23 175 switch (s->pix_fmt) {
716d413c
AK
176 case AV_PIX_FMT_YUV420P:
177 case AV_PIX_FMT_YUYV422:
178 case AV_PIX_FMT_UYVY422:
179 case AV_PIX_FMT_YUV422P:
180 case AV_PIX_FMT_YUV440P:
181 case AV_PIX_FMT_YUV444P:
182 case AV_PIX_FMT_GBRP:
183 case AV_PIX_FMT_GRAY8:
184 case AV_PIX_FMT_GRAY16BE:
185 case AV_PIX_FMT_GRAY16LE:
186 case AV_PIX_FMT_YUVJ420P:
187 case AV_PIX_FMT_YUVJ422P:
188 case AV_PIX_FMT_YUVJ440P:
189 case AV_PIX_FMT_YUVJ444P:
190 case AV_PIX_FMT_YUVA420P:
6d5600e8
LB
191 case AV_PIX_FMT_YUVA422P:
192 case AV_PIX_FMT_YUVA444P:
716d413c
AK
193 case AV_PIX_FMT_YUV420P9LE:
194 case AV_PIX_FMT_YUV420P9BE:
195 case AV_PIX_FMT_YUV420P10LE:
196 case AV_PIX_FMT_YUV420P10BE:
197 case AV_PIX_FMT_YUV422P9LE:
198 case AV_PIX_FMT_YUV422P9BE:
199 case AV_PIX_FMT_YUV422P10LE:
200 case AV_PIX_FMT_YUV422P10BE:
201 case AV_PIX_FMT_YUV444P9LE:
202 case AV_PIX_FMT_YUV444P9BE:
203 case AV_PIX_FMT_YUV444P10LE:
204 case AV_PIX_FMT_YUV444P10BE:
205 case AV_PIX_FMT_GBRP9LE:
206 case AV_PIX_FMT_GBRP9BE:
207 case AV_PIX_FMT_GBRP10LE:
208 case AV_PIX_FMT_GBRP10BE:
37c0dc62
RC
209 w_align = 16; //FIXME assume 16 pixel per macroblock
210 h_align = 16 * 2; // interlaced needs 2 macroblocks height
f0bbfc4a 211 break;
716d413c
AK
212 case AV_PIX_FMT_YUV411P:
213 case AV_PIX_FMT_UYYVYY411:
419ffb23
LB
214 w_align = 32;
215 h_align = 8;
f0bbfc4a 216 break;
716d413c 217 case AV_PIX_FMT_YUV410P:
419ffb23
LB
218 if (s->codec_id == AV_CODEC_ID_SVQ1) {
219 w_align = 64;
220 h_align = 64;
f0bbfc4a 221 }
716d413c 222 case AV_PIX_FMT_RGB555:
419ffb23
LB
223 if (s->codec_id == AV_CODEC_ID_RPZA) {
224 w_align = 4;
225 h_align = 4;
d99fbbf4 226 }
716d413c
AK
227 case AV_PIX_FMT_PAL8:
228 case AV_PIX_FMT_BGR8:
229 case AV_PIX_FMT_RGB8:
419ffb23
LB
230 if (s->codec_id == AV_CODEC_ID_SMC) {
231 w_align = 4;
232 h_align = 4;
d99fbbf4 233 }
f0bbfc4a 234 break;
716d413c 235 case AV_PIX_FMT_BGR24:
419ffb23
LB
236 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
237 (s->codec_id == AV_CODEC_ID_ZLIB)) {
238 w_align = 4;
239 h_align = 4;
c31b8121
RT
240 }
241 break;
f0bbfc4a 242 default:
419ffb23
LB
243 w_align = 1;
244 h_align = 1;
f0bbfc4a
MN
245 break;
246 }
247
419ffb23
LB
248 *width = FFALIGN(*width, w_align);
249 *height = FFALIGN(*height, h_align);
36ef5369 250 if (s->codec_id == AV_CODEC_ID_H264)
419ffb23
LB
251 // some of the optimized chroma MC reads one line too much
252 *height += 2;
eb285cfe 253
2d9535ad 254 for (i = 0; i < 4; i++)
560f773c 255 linesize_align[i] = STRIDE_ALIGN;
eb285cfe
RD
256}
257
419ffb23
LB
258void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
259{
50ba57e0
AK
260 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
261 int chroma_shift = desc->log2_chroma_w;
560f773c 262 int linesize_align[AV_NUM_DATA_POINTERS];
eb285cfe 263 int align;
419ffb23 264
eb285cfe 265 avcodec_align_dimensions2(s, width, height, linesize_align);
419ffb23 266 align = FFMAX(linesize_align[0], linesize_align[3]);
eb285cfe
RD
267 linesize_align[1] <<= chroma_shift;
268 linesize_align[2] <<= chroma_shift;
419ffb23
LB
269 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
270 *width = FFALIGN(*width, align);
f0bbfc4a
MN
271}
272
5ee5fa02
JR
273int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
274 enum AVSampleFormat sample_fmt, const uint8_t *buf,
275 int buf_size, int align)
276{
277 int ch, planar, needed_size, ret = 0;
278
279 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
280 frame->nb_samples, sample_fmt,
281 align);
282 if (buf_size < needed_size)
283 return AVERROR(EINVAL);
284
285 planar = av_sample_fmt_is_planar(sample_fmt);
286 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
287 if (!(frame->extended_data = av_mallocz(nb_channels *
288 sizeof(*frame->extended_data))))
289 return AVERROR(ENOMEM);
290 } else {
291 frame->extended_data = frame->data;
292 }
293
294 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
295 buf, nb_channels, frame->nb_samples,
296 sample_fmt, align)) < 0) {
297 if (frame->extended_data != frame->data)
298 av_free(frame->extended_data);
299 return ret;
300 }
301 if (frame->extended_data != frame->data) {
302 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
303 frame->data[ch] = frame->extended_data[ch];
304 }
305
306 return ret;
307}
308
759001c5 309static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
0eea2129 310{
759001c5
AK
311 FramePool *pool = avctx->internal->pool;
312 int i, ret;
1e491e29 313
759001c5
AK
314 switch (avctx->codec_type) {
315 case AVMEDIA_TYPE_VIDEO: {
c7622f9a 316 AVPicture picture;
759001c5
AK
317 int size[4] = { 0 };
318 int w = frame->width;
319 int h = frame->height;
320 int tmpsize, unaligned;
c7622f9a 321
759001c5
AK
322 if (pool->format == frame->format &&
323 pool->width == frame->width && pool->height == frame->height)
324 return 0;
f0bbfc4a 325
759001c5 326 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
115329f1 327
759001c5 328 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
419ffb23
LB
329 w += EDGE_WIDTH * 2;
330 h += EDGE_WIDTH * 2;
1e491e29 331 }
5b67307a 332
c9d6e847
RD
333 do {
334 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
335 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
759001c5 336 av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
c9d6e847 337 // increase alignment of w for next try (rhs gives the lowest bit set in w)
419ffb23 338 w += w & ~(w - 1);
db7ae7d1 339
c9d6e847 340 unaligned = 0;
419ffb23 341 for (i = 0; i < 4; i++)
759001c5 342 unaligned |= picture.linesize[i] % pool->stride_align[i];
c9d6e847 343 } while (unaligned);
db7ae7d1 344
759001c5
AK
345 tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
346 NULL, picture.linesize);
f8c96d01
RD
347 if (tmpsize < 0)
348 return -1;
db7ae7d1 349
419ffb23
LB
350 for (i = 0; i < 3 && picture.data[i + 1]; i++)
351 size[i] = picture.data[i + 1] - picture.data[i];
cf73e32a 352 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
c7622f9a 353
759001c5
AK
354 for (i = 0; i < 4; i++) {
355 av_buffer_pool_uninit(&pool->pools[i]);
356 pool->linesize[i] = picture.linesize[i];
357 if (size[i]) {
358 pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
359 if (!pool->pools[i]) {
360 ret = AVERROR(ENOMEM);
361 goto fail;
362 }
363 }
364 }
365 pool->format = frame->format;
366 pool->width = frame->width;
367 pool->height = frame->height;
1e491e29 368
759001c5
AK
369 break;
370 }
371 case AVMEDIA_TYPE_AUDIO: {
372 int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
373 int planar = av_sample_fmt_is_planar(frame->format);
374 int planes = planar ? ch : 1;
375
376 if (pool->format == frame->format && pool->planes == planes &&
377 pool->channels == ch && frame->nb_samples == pool->samples)
378 return 0;
379
380 av_buffer_pool_uninit(&pool->pools[0]);
381 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
382 frame->nb_samples, frame->format, 0);
383 if (ret < 0)
384 goto fail;
385
386 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
387 if (!pool->pools[0]) {
388 ret = AVERROR(ENOMEM);
389 goto fail;
390 }
1e491e29 391
759001c5
AK
392 pool->format = frame->format;
393 pool->planes = planes;
394 pool->channels = ch;
395 pool->samples = frame->nb_samples;
396 break;
397 }
398 default: av_assert0(0);
399 }
400 return 0;
401fail:
402 for (i = 0; i < 4; i++)
403 av_buffer_pool_uninit(&pool->pools[i]);
404 pool->format = -1;
405 pool->planes = pool->channels = pool->samples = 0;
406 pool->width = pool->height = 0;
407 return ret;
408}
1e491e29 409
759001c5
AK
410static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
411{
412 FramePool *pool = avctx->internal->pool;
413 int planes = pool->planes;
414 int i;
c7622f9a 415
759001c5
AK
416 frame->linesize[0] = pool->linesize[0];
417
418 if (planes > AV_NUM_DATA_POINTERS) {
419 frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
420 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
421 frame->extended_buf = av_mallocz(frame->nb_extended_buf *
422 sizeof(*frame->extended_buf));
423 if (!frame->extended_data || !frame->extended_buf) {
424 av_freep(&frame->extended_data);
425 av_freep(&frame->extended_buf);
426 return AVERROR(ENOMEM);
560f773c 427 }
759001c5
AK
428 } else
429 frame->extended_data = frame->data;
430
431 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
432 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
433 if (!frame->buf[i])
434 goto fail;
435 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
1e491e29 436 }
759001c5
AK
437 for (i = 0; i < frame->nb_extended_buf; i++) {
438 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
439 if (!frame->extended_buf[i])
440 goto fail;
441 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
442 }
443
444 if (avctx->debug & FF_DEBUG_BUFFERS)
445 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
446
447 return 0;
448fail:
449 av_frame_unref(frame);
450 return AVERROR(ENOMEM);
451}
1e491e29 452
759001c5
AK
453static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
454{
455 FramePool *pool = s->internal->pool;
456 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
457 int pixel_size = desc->comp[0].step_minus1 + 1;
458 int h_chroma_shift, v_chroma_shift;
459 int i;
460
461 if (pic->data[0] != NULL) {
462 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
463 return -1;
d90cf87b 464 }
759001c5
AK
465
466 memset(pic->data, 0, sizeof(pic->data));
0eea2129 467 pic->extended_data = pic->data;
759001c5
AK
468
469 av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
470
471 for (i = 0; i < 4 && pool->pools[i]; i++) {
472 const int h_shift = i == 0 ? 0 : h_chroma_shift;
473 const int v_shift = i == 0 ? 0 : v_chroma_shift;
474
475 pic->linesize[i] = pool->linesize[i];
476
477 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
478 if (!pic->buf[i])
479 goto fail;
480
481 // no edge if EDGE EMU or not planar YUV
482 if ((s->flags & CODEC_FLAG_EMU_EDGE) || !pool->pools[2])
483 pic->data[i] = pic->buf[i]->data;
484 else {
485 pic->data[i] = pic->buf[i]->data +
486 FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
487 (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
488 }
489 }
490 for (; i < AV_NUM_DATA_POINTERS; i++) {
491 pic->data[i] = NULL;
492 pic->linesize[i] = 0;
493 }
494 if (pic->data[1] && !pic->data[2])
495 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
79de84f2 496
419ffb23 497 if (s->debug & FF_DEBUG_BUFFERS)
759001c5 498 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
385c820b 499
1e491e29 500 return 0;
759001c5
AK
501fail:
502 av_frame_unref(pic);
503 return AVERROR(ENOMEM);
1e491e29
MN
504}
505
759001c5 506int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
0eea2129 507{
759001c5
AK
508 int ret;
509
510 if ((ret = update_frame_pool(avctx, frame)) < 0)
511 return ret;
512
513#if FF_API_GET_BUFFER
387bef95 514 frame->type = FF_BUFFER_TYPE_INTERNAL;
759001c5
AK
515#endif
516
0eea2129
JR
517 switch (avctx->codec_type) {
518 case AVMEDIA_TYPE_VIDEO:
519 return video_get_buffer(avctx, frame);
520 case AVMEDIA_TYPE_AUDIO:
521 return audio_get_buffer(avctx, frame);
522 default:
523 return -1;
524 }
525}
526
759001c5
AK
527#if FF_API_GET_BUFFER
528int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
594d4d5d 529{
759001c5
AK
530 return avcodec_default_get_buffer2(avctx, frame, 0);
531}
532
533typedef struct CompatReleaseBufPriv {
534 AVCodecContext avctx;
535 AVFrame frame;
536} CompatReleaseBufPriv;
537
538static void compat_free_buffer(void *opaque, uint8_t *data)
539{
540 CompatReleaseBufPriv *priv = opaque;
a2816230
JG
541 if (priv->avctx.release_buffer)
542 priv->avctx.release_buffer(&priv->avctx, &priv->frame);
759001c5
AK
543 av_freep(&priv);
544}
545
546static void compat_release_buffer(void *opaque, uint8_t *data)
547{
548 AVBufferRef *buf = opaque;
549 av_buffer_unref(&buf);
550}
551#endif
552
553int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
554{
555 int ret;
556
ff953fec
AK
557 switch (avctx->codec_type) {
558 case AVMEDIA_TYPE_VIDEO:
a7f46586
AK
559 frame->width = FFMAX(avctx->width, avctx->coded_width);
560 frame->height = FFMAX(avctx->height, avctx->coded_height);
15ec0450
AK
561 if (frame->format < 0)
562 frame->format = avctx->pix_fmt;
563 if (!frame->sample_aspect_ratio.num)
564 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
759001c5
AK
565
566 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
567 return ret;
ff953fec
AK
568 break;
569 case AVMEDIA_TYPE_AUDIO:
15ec0450
AK
570 if (!frame->sample_rate)
571 frame->sample_rate = avctx->sample_rate;
572 if (frame->format < 0)
573 frame->format = avctx->sample_fmt;
2eba9087
AK
574 if (!frame->channel_layout) {
575 if (avctx->channel_layout) {
576 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
577 avctx->channels) {
578 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
579 "configuration.\n");
580 return AVERROR(EINVAL);
581 }
582
583 frame->channel_layout = avctx->channel_layout;
584 } else {
585 if (avctx->channels > FF_SANE_NB_CHANNELS) {
586 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
587 avctx->channels);
588 return AVERROR(ENOSYS);
589 }
590
591 frame->channel_layout = av_get_default_channel_layout(avctx->channels);
592 if (!frame->channel_layout)
593 frame->channel_layout = (1ULL << avctx->channels) - 1;
594 }
595 }
ff953fec
AK
596 break;
597 default: return AVERROR(EINVAL);
598 }
599
600 frame->pkt_pts = avctx->pkt ? avctx->pkt->pts : AV_NOPTS_VALUE;
601 frame->reordered_opaque = avctx->reordered_opaque;
602
759001c5
AK
603#if FF_API_GET_BUFFER
604 /*
605 * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
606 * We wrap each plane in its own AVBuffer. Each of those has a reference to
607 * a dummy AVBuffer as its private data, unreffing it on free.
608 * When all the planes are freed, the dummy buffer's free callback calls
609 * release_buffer().
610 */
611 if (avctx->get_buffer) {
612 CompatReleaseBufPriv *priv = NULL;
613 AVBufferRef *dummy_buf = NULL;
614 int planes, i, ret;
594d4d5d 615
759001c5
AK
616 if (flags & AV_GET_BUFFER_FLAG_REF)
617 frame->reference = 1;
d90cf87b 618
759001c5
AK
619 ret = avctx->get_buffer(avctx, frame);
620 if (ret < 0)
621 return ret;
0eea2129 622
759001c5
AK
623 /* return if the buffers are already set up
624 * this would happen e.g. when a custom get_buffer() calls
625 * avcodec_default_get_buffer
626 */
627 if (frame->buf[0])
628 return 0;
629
630 priv = av_mallocz(sizeof(*priv));
631 if (!priv) {
632 ret = AVERROR(ENOMEM);
633 goto fail;
634 }
635 priv->avctx = *avctx;
636 priv->frame = *frame;
f3a29b75 637
759001c5
AK
638 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
639 if (!dummy_buf) {
640 ret = AVERROR(ENOMEM);
641 goto fail;
f3a29b75 642 }
d90cf87b 643
759001c5
AK
644#define WRAP_PLANE(ref_out, data, data_size) \
645do { \
646 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
647 if (!dummy_ref) { \
648 ret = AVERROR(ENOMEM); \
649 goto fail; \
650 } \
651 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
652 dummy_ref, 0); \
653 if (!ref_out) { \
654 av_frame_unref(frame); \
655 ret = AVERROR(ENOMEM); \
656 goto fail; \
657 } \
658} while (0)
659
660 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
661 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
662
c977039e 663 planes = av_pix_fmt_count_planes(frame->format);
c24469e8
JG
664 /* workaround for AVHWAccel plane count of 0, buf[0] is used as
665 check for allocated buffers: make libavcodec happy */
e6c4ac7b 666 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
c24469e8 667 planes = 1;
c977039e 668 if (!desc || planes <= 0) {
759001c5
AK
669 ret = AVERROR(EINVAL);
670 goto fail;
671 }
d90cf87b 672
759001c5 673 for (i = 0; i < planes; i++) {
65528080
AK
674 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
675 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
385c820b 676
759001c5
AK
677 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
678 }
679 } else {
680 int planar = av_sample_fmt_is_planar(frame->format);
681 planes = planar ? avctx->channels : 1;
682
683 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
684 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
685 frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
686 frame->nb_extended_buf);
687 if (!frame->extended_buf) {
688 ret = AVERROR(ENOMEM);
689 goto fail;
690 }
691 }
1e491e29 692
759001c5
AK
693 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
694 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
695
669cc0f3 696 for (i = 0; i < frame->nb_extended_buf; i++)
759001c5
AK
697 WRAP_PLANE(frame->extended_buf[i],
698 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
699 frame->linesize[0]);
700 }
701
702 av_buffer_unref(&dummy_buf);
e1c2a5a0 703
a7f46586
AK
704 frame->width = avctx->width;
705 frame->height = avctx->height;
706
759001c5 707 return 0;
0eea2129 708
759001c5
AK
709fail:
710 avctx->release_buffer(avctx, frame);
711 av_freep(&priv);
712 av_buffer_unref(&dummy_buf);
713 return ret;
e1c2a5a0 714 }
759001c5
AK
715#endif
716
a7f46586
AK
717 ret = avctx->get_buffer2(avctx, frame, flags);
718
719 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
720 frame->width = avctx->width;
721 frame->height = avctx->height;
722 }
723
724 return ret;
759001c5
AK
725}
726
727int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
728{
729 AVFrame tmp;
730 int ret;
e1c2a5a0 731
759001c5
AK
732 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
733
734 if (!frame->data[0])
735 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
87840eeb 736
759001c5 737 if (av_frame_is_writable(frame))
e1c2a5a0 738 return 0;
759001c5
AK
739
740 av_frame_move_ref(&tmp, frame);
741
742 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
743 if (ret < 0) {
744 av_frame_unref(&tmp);
745 return ret;
b8af4fe9 746 }
e1c2a5a0 747
759001c5
AK
748 av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
749 frame->format, frame->width, frame->height);
750
751 av_frame_unref(&tmp);
752
e1c2a5a0
RT
753 return 0;
754}
755
759001c5
AK
756#if FF_API_GET_BUFFER
757void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
758{
759 av_frame_unref(pic);
760}
761
762int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
763{
764 av_assert0(0);
252c0bfd 765 return AVERROR_BUG;
759001c5
AK
766}
767#endif
768
419ffb23
LB
769int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
770{
9c3d33d6
MN
771 int i;
772
419ffb23
LB
773 for (i = 0; i < count; i++) {
774 int r = func(c, (char *)arg + i * size);
775 if (ret)
776 ret[i] = r;
9c3d33d6
MN
777 }
778 return 0;
779}
780
419ffb23
LB
781int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
782{
8d23a86f
RD
783 int i;
784
419ffb23
LB
785 for (i = 0; i < count; i++) {
786 int r = func(c, arg, i, 0);
787 if (ret)
788 ret[i] = r;
8d23a86f
RD
789 }
790 return 0;
791}
792
cc085993
LB
793static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
794{
795 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
e6c4ac7b 796 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
cc085993
LB
797}
798
716d413c 799enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
419ffb23 800{
cc085993 801 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
c269cf68 802 ++fmt;
a33c7159
MN
803 return fmt[0];
804}
805
a83499b1 806void avcodec_get_frame_defaults(AVFrame *frame)
419ffb23 807{
2bc0de38
AK
808 if (frame->extended_data != frame->data)
809 av_freep(&frame->extended_data);
810
a83499b1 811 memset(frame, 0, sizeof(AVFrame));
9740beff 812
a83499b1
AK
813 frame->pts = AV_NOPTS_VALUE;
814 frame->key_frame = 1;
815 frame->sample_aspect_ratio = (AVRational) {0, 1 };
816 frame->format = -1; /* unknown */
2bc0de38 817 frame->extended_data = frame->data;
9740beff
MN
818}
819
419ffb23
LB
820AVFrame *avcodec_alloc_frame(void)
821{
c084a975 822 AVFrame *frame = av_mallocz(sizeof(AVFrame));
115329f1 823
a83499b1 824 if (frame == NULL)
419ffb23 825 return NULL;
115329f1 826
a83499b1 827 avcodec_get_frame_defaults(frame);
115329f1 828
a83499b1 829 return frame;
1e491e29
MN
830}
831
a42aadab
AK
832void avcodec_free_frame(AVFrame **frame)
833{
834 AVFrame *f;
835
836 if (!frame || !*frame)
837 return;
838
839 f = *frame;
840
841 if (f->extended_data != f->data)
842 av_freep(&f->extended_data);
843
844 av_freep(frame);
845}
846
0a0f19b5 847int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
0b950fe2 848{
31d76ec2 849 int ret = 0;
0b950fe2
AK
850 AVDictionary *tmp = NULL;
851
af08d9ae
AK
852 if (avcodec_is_open(avctx))
853 return 0;
854
bc901998
AK
855 if ((!codec && !avctx->codec)) {
856 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
857 return AVERROR(EINVAL);
858 }
859 if ((codec && avctx->codec && codec != avctx->codec)) {
860 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
419ffb23 861 "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
bc901998
AK
862 return AVERROR(EINVAL);
863 }
864 if (!codec)
865 codec = avctx->codec;
866
4df30f71
AC
867 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
868 return AVERROR(EINVAL);
869
0b950fe2
AK
870 if (options)
871 av_dict_copy(&tmp, *options, 0);
115329f1 872
f988ce6c 873 /* If there is a user-supplied mutex locking routine, call it. */
088f38a4
DB
874 if (lockmgr_cb) {
875 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
f988ce6c
876 return -1;
877 }
878
ddebfb15 879 entangled_thread_counter++;
419ffb23 880 if (entangled_thread_counter != 1) {
ddebfb15 881 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
31d76ec2 882 ret = -1;
ddebfb15
MN
883 goto end;
884 }
de6d9b64 885
f3a29b75
JR
886 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
887 if (!avctx->internal) {
888 ret = AVERROR(ENOMEM);
889 goto end;
890 }
891
759001c5
AK
892 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
893 if (!avctx->internal->pool) {
894 ret = AVERROR(ENOMEM);
895 goto free_and_end;
896 }
897
0edf8a7a 898 if (codec->priv_data_size > 0) {
90f06cea 899 if (!avctx->priv_data) {
419ffb23
LB
900 avctx->priv_data = av_mallocz(codec->priv_data_size);
901 if (!avctx->priv_data) {
902 ret = AVERROR(ENOMEM);
903 goto end;
904 }
905 if (codec->priv_class) {
906 *(const AVClass **)avctx->priv_data = codec->priv_class;
907 av_opt_set_defaults(avctx->priv_data);
908 }
dc51a72b 909 }
419ffb23
LB
910 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
911 goto free_and_end;
0edf8a7a
PG
912 } else {
913 avctx->priv_data = NULL;
914 }
0b950fe2
AK
915 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
916 goto free_and_end;
21adafec 917
9de9b828 918 if (avctx->coded_width && avctx->coded_height && !avctx->width && !avctx->height)
21adafec 919 avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
419ffb23 920 else if (avctx->width && avctx->height)
21adafec
MN
921 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
922
82eac2f3
RD
923 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
924 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
925 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
926 av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
927 avcodec_set_dimensions(avctx, 0, 0);
928 }
929
f19c58b4 930 /* if the decoder init function was already called previously,
419ffb23 931 * free the already allocated subtitle_header before overwriting it */
44fe77b3 932 if (av_codec_is_decoder(codec))
f19c58b4
AJ
933 av_freep(&avctx->subtitle_header);
934
bb6941af 935 if (avctx->channels > FF_SANE_NB_CHANNELS) {
7868349a 936 ret = AVERROR(EINVAL);
2a9b5c9b 937 goto free_and_end;
0ecca7a4
MN
938 }
939
b5c85991 940 avctx->codec = codec;
72415b2a 941 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
36ef5369 942 avctx->codec_id == AV_CODEC_ID_NONE) {
681c180d
RD
943 avctx->codec_type = codec->type;
944 avctx->codec_id = codec->id;
945 }
e83c716e 946 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
419ffb23 947 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
4c0dda2b 948 av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
31d76ec2 949 ret = AVERROR(EINVAL);
2a9b5c9b 950 goto free_and_end;
4c0dda2b 951 }
b5c85991 952 avctx->frame_number = 0;
37b00b47 953
c854102d
NC
954 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
955 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
956 ret = AVERROR_EXPERIMENTAL;
957 goto free_and_end;
958 }
959
9a7dc618
MS
960 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
961 (!avctx->time_base.num || !avctx->time_base.den)) {
962 avctx->time_base.num = 1;
963 avctx->time_base.den = avctx->sample_rate;
964 }
965
30e25643 966 if (HAVE_THREADS) {
ba9ef8d0 967 ret = ff_thread_init(avctx);
37b00b47
AS
968 if (ret < 0) {
969 goto free_and_end;
970 }
971 }
b6064d9a
JG
972 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
973 avctx->thread_count = 1;
37b00b47 974
44fe77b3 975 if (av_codec_is_encoder(avctx->codec)) {
2cfa2d92 976 int i;
8b00ab01 977 if (avctx->codec->sample_fmts) {
37f701f1 978 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
3dfc3e70
JR
979 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
980 break;
37f701f1
JR
981 if (avctx->channels == 1 &&
982 av_get_planar_sample_fmt(avctx->sample_fmt) ==
983 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
984 avctx->sample_fmt = avctx->codec->sample_fmts[i];
985 break;
986 }
987 }
3dfc3e70
JR
988 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
989 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
990 ret = AVERROR(EINVAL);
991 goto free_and_end;
992 }
8b00ab01 993 }
dcd2b55e 994 if (avctx->codec->pix_fmts) {
716d413c 995 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
dcd2b55e
PM
996 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
997 break;
716d413c 998 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
dcd2b55e
PM
999 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
1000 ret = AVERROR(EINVAL);
1001 goto free_and_end;
1002 }
1003 }
8b00ab01
JR
1004 if (avctx->codec->supported_samplerates) {
1005 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1006 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1007 break;
1008 if (avctx->codec->supported_samplerates[i] == 0) {
1009 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
1010 ret = AVERROR(EINVAL);
1011 goto free_and_end;
1012 }
1013 }
1014 if (avctx->codec->channel_layouts) {
1015 if (!avctx->channel_layout) {
1016 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
1017 } else {
1018 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1019 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1020 break;
1021 if (avctx->codec->channel_layouts[i] == 0) {
1022 av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
1023 ret = AVERROR(EINVAL);
1024 goto free_and_end;
1025 }
1026 }
1027 }
168f9e8c
JR
1028 if (avctx->channel_layout && avctx->channels) {
1029 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
1030 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
1031 ret = AVERROR(EINVAL);
1032 goto free_and_end;
1033 }
688b09fa
JR
1034 } else if (avctx->channel_layout) {
1035 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
168f9e8c 1036 }
bff36075
LB
1037
1038 if (!avctx->rc_initial_buffer_occupancy)
1039 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
2cfa2d92 1040 }
0fd0ef79 1041
419ffb23 1042 if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
2de4f9eb
MN
1043 ret = avctx->codec->init(avctx);
1044 if (ret < 0) {
2a9b5c9b 1045 goto free_and_end;
2de4f9eb 1046 }
6e546aaa 1047 }
1337de0c
JR
1048
1049 if (av_codec_is_decoder(avctx->codec)) {
1050 /* validate channel layout from the decoder */
12e1e834
AK
1051 if (avctx->channel_layout) {
1052 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1053 if (!avctx->channels)
1054 avctx->channels = channels;
1055 else if (channels != avctx->channels) {
1056 av_log(avctx, AV_LOG_WARNING,
1057 "channel layout does not match number of channels\n");
1058 avctx->channel_layout = 0;
1059 }
1337de0c 1060 }
0366664e
JR
1061 if (avctx->channels && avctx->channels < 0 ||
1062 avctx->channels > FF_SANE_NB_CHANNELS) {
1063 ret = AVERROR(EINVAL);
1064 goto free_and_end;
1065 }
1337de0c 1066 }
ddebfb15
MN
1067end:
1068 entangled_thread_counter--;
f988ce6c
1069
1070 /* Release any user-supplied mutex. */
088f38a4
DB
1071 if (lockmgr_cb) {
1072 (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
f988ce6c 1073 }
0b950fe2
AK
1074 if (options) {
1075 av_dict_free(options);
1076 *options = tmp;
1077 }
1078
ddebfb15 1079 return ret;
2a9b5c9b 1080free_and_end:
0b950fe2 1081 av_dict_free(&tmp);
2a9b5c9b 1082 av_freep(&avctx->priv_data);
759001c5
AK
1083 if (avctx->internal)
1084 av_freep(&avctx->internal->pool);
f3a29b75 1085 av_freep(&avctx->internal);
419ffb23 1086 avctx->codec = NULL;
2a9b5c9b 1087 goto end;
de6d9b64
FB
1088}
1089
b2c75b6e 1090int ff_alloc_packet(AVPacket *avpkt, int size)
de6d9b64 1091{
b2c75b6e
JR
1092 if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
1093 return AVERROR(EINVAL);
1094
1095 if (avpkt->data) {
1afddbe5
AK
1096 AVBufferRef *buf = avpkt->buf;
1097#if FF_API_DESTRUCT_PACKET
e42e9b0e 1098 void *destruct = avpkt->destruct;
1afddbe5 1099#endif
b2c75b6e
JR
1100
1101 if (avpkt->size < size)
1102 return AVERROR(EINVAL);
1103
b2c75b6e 1104 av_init_packet(avpkt);
1afddbe5 1105#if FF_API_DESTRUCT_PACKET
e42e9b0e 1106 avpkt->destruct = destruct;
1afddbe5
AK
1107#endif
1108 avpkt->buf = buf;
419ffb23 1109 avpkt->size = size;
b2c75b6e
JR
1110 return 0;
1111 } else {
1112 return av_new_packet(avpkt, size);
0ecca7a4 1113 }
b2c75b6e
JR
1114}
1115
a5117a24
AK
1116/**
1117 * Pad last frame with silence.
1118 */
1119static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1120{
1121 AVFrame *frame = NULL;
a5117a24
AK
1122 int ret;
1123
1124 if (!(frame = avcodec_alloc_frame()))
1125 return AVERROR(ENOMEM);
a5117a24 1126
799f57ac
AK
1127 frame->format = src->format;
1128 frame->channel_layout = src->channel_layout;
1129 frame->nb_samples = s->frame_size;
1130 ret = av_frame_get_buffer(frame, 32);
1131 if (ret < 0)
a5117a24
AK
1132 goto fail;
1133
799f57ac
AK
1134 ret = av_frame_copy_props(frame, src);
1135 if (ret < 0)
a5117a24 1136 goto fail;
a5117a24 1137
a5117a24
AK
1138 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1139 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1140 goto fail;
1141 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1142 frame->nb_samples - src->nb_samples,
1143 s->channels, s->sample_fmt)) < 0)
1144 goto fail;
1145
1146 *dst = frame;
1147
1148 return 0;
1149
1150fail:
799f57ac 1151 av_frame_free(&frame);
a5117a24
AK
1152 return ret;
1153}
1154
b2c75b6e
JR
1155int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1156 AVPacket *avpkt,
1157 const AVFrame *frame,
1158 int *got_packet_ptr)
1159{
c22953b8 1160 AVFrame tmp;
a5117a24 1161 AVFrame *padded_frame = NULL;
b2c75b6e
JR
1162 int ret;
1163 int user_packet = !!avpkt->data;
b2c75b6e 1164
52953d61
AK
1165 *got_packet_ptr = 0;
1166
b2c75b6e 1167 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
7fb6c922 1168 av_free_packet(avpkt);
b2c75b6e 1169 av_init_packet(avpkt);
6f824977 1170 return 0;
b2c75b6e
JR
1171 }
1172
c22953b8
AK
1173 /* ensure that extended_data is properly set */
1174 if (frame && !frame->extended_data) {
1175 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1176 avctx->channels > AV_NUM_DATA_POINTERS) {
1177 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
419ffb23 1178 "with more than %d channels, but extended_data is not set.\n",
c22953b8
AK
1179 AV_NUM_DATA_POINTERS);
1180 return AVERROR(EINVAL);
1181 }
1182 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1183
1184 tmp = *frame;
1185 tmp.extended_data = tmp.data;
1186 frame = &tmp;
1187 }
1188
b2c75b6e
JR
1189 /* check for valid frame size */
1190 if (frame) {
b2c75b6e 1191 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
b461cd4d 1192 if (frame->nb_samples > avctx->frame_size)
b2c75b6e
JR
1193 return AVERROR(EINVAL);
1194 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
a5117a24
AK
1195 if (frame->nb_samples < avctx->frame_size &&
1196 !avctx->internal->last_audio_frame) {
1197 ret = pad_last_frame(avctx, &padded_frame, frame);
1198 if (ret < 0)
1199 return ret;
1200
1201 frame = padded_frame;
1202 avctx->internal->last_audio_frame = 1;
1203 }
1204
0ccf051a
JR
1205 if (frame->nb_samples != avctx->frame_size) {
1206 ret = AVERROR(EINVAL);
1207 goto end;
1208 }
b2c75b6e 1209 }
b2c75b6e
JR
1210 }
1211
fa0319b4
JR
1212 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1213 if (!ret) {
1214 if (*got_packet_ptr) {
a75bc764 1215 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
70749c48
JR
1216 if (avpkt->pts == AV_NOPTS_VALUE)
1217 avpkt->pts = frame->pts;
1218 if (!avpkt->duration)
1219 avpkt->duration = ff_samples_to_time_base(avctx,
1220 frame->nb_samples);
a75bc764
JR
1221 }
1222 avpkt->dts = avpkt->pts;
b758cf73
JR
1223 } else {
1224 avpkt->size = 0;
b2c75b6e 1225 }
b2c75b6e 1226
3c6607eb 1227 if (!user_packet && avpkt->size) {
1afddbe5
AK
1228 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1229 if (ret >= 0)
1230 avpkt->data = avpkt->buf->data;
a1977e01
JR
1231 }
1232
b2c75b6e 1233 avctx->frame_number++;
a1977e01 1234 }
b2c75b6e 1235
74e10b62 1236 if (ret < 0 || !*got_packet_ptr) {
7fb6c922 1237 av_free_packet(avpkt);
74e10b62 1238 av_init_packet(avpkt);
0ccf051a 1239 goto end;
74e10b62 1240 }
7fb6c922 1241
b2c75b6e 1242 /* NOTE: if we add any audio encoders which output non-keyframe packets,
419ffb23
LB
1243 * this needs to be moved to the encoders, but for now we can do it
1244 * here to simplify things */
b2c75b6e
JR
1245 avpkt->flags |= AV_PKT_FLAG_KEY;
1246
0ccf051a 1247end:
a5117a24
AK
1248 if (padded_frame) {
1249 av_freep(&padded_frame->data[0]);
1250 if (padded_frame->extended_data != padded_frame->data)
1251 av_freep(&padded_frame->extended_data);
1252 av_freep(&padded_frame);
1253 }
1254
b2c75b6e 1255 return ret;
de6d9b64
FB
1256}
1257
52f82a11
AK
1258int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1259 AVPacket *avpkt,
1260 const AVFrame *frame,
1261 int *got_packet_ptr)
1262{
1263 int ret;
1264 int user_packet = !!avpkt->data;
1265
d55fa6f9
AK
1266 *got_packet_ptr = 0;
1267
52f82a11 1268 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
7fb6c922 1269 av_free_packet(avpkt);
52f82a11 1270 av_init_packet(avpkt);
419ffb23 1271 avpkt->size = 0;
6f824977 1272 return 0;
52f82a11
AK
1273 }
1274
1275 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1276 return AVERROR(EINVAL);
1277
ff311c09 1278 av_assert0(avctx->codec->encode2);
52f82a11 1279
ff311c09
AK
1280 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1281 if (!ret) {
1282 if (!*got_packet_ptr)
1283 avpkt->size = 0;
1284 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1285 avpkt->pts = avpkt->dts = frame->pts;
52f82a11 1286
3c6607eb 1287 if (!user_packet && avpkt->size) {
1afddbe5
AK
1288 ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
1289 if (ret >= 0)
1290 avpkt->data = avpkt->buf->data;
eb727387
AK
1291 }
1292
52f82a11 1293 avctx->frame_number++;
03ca0a5b 1294 }
52f82a11 1295
7fb6c922
AK
1296 if (ret < 0 || !*got_packet_ptr)
1297 av_free_packet(avpkt);
1298
52f82a11
AK
1299 emms_c();
1300 return ret;
de6d9b64
FB
1301}
1302
115329f1 1303int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
240c1657
FB
1304 const AVSubtitle *sub)
1305{
1306 int ret;
419ffb23 1307 if (sub->start_display_time) {
9413db9e
BA
1308 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1309 return -1;
1310 }
419ffb23 1311 if (sub->num_rects == 0 || !sub->rects)
505aa6c9 1312 return -1;
466b39ef 1313 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
240c1657
FB
1314 avctx->frame_number++;
1315 return ret;
1316}
1317
867f923d
MS
1318static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1319{
1320 int size = 0;
1321 const uint8_t *data;
1322 uint32_t flags;
1323
1324 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1325 return;
1326
1327 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1328 if (!data || size < 4)
1329 return;
1330 flags = bytestream_get_le32(&data);
1331 size -= 4;
1332 if (size < 4) /* Required for any of the changes */
1333 return;
1334 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1335 avctx->channels = bytestream_get_le32(&data);
1336 size -= 4;
1337 }
1338 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1339 if (size < 8)
1340 return;
1341 avctx->channel_layout = bytestream_get_le64(&data);
1342 size -= 8;
1343 }
1344 if (size < 4)
1345 return;
1346 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1347 avctx->sample_rate = bytestream_get_le32(&data);
1348 size -= 4;
1349 }
1350 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1351 if (size < 8)
1352 return;
1353 avctx->width = bytestream_get_le32(&data);
1354 avctx->height = bytestream_get_le32(&data);
c5d907b6 1355 avcodec_set_dimensions(avctx, avctx->width, avctx->height);
867f923d
MS
1356 size -= 8;
1357 }
1358}
1359
7a00bbad 1360int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
419ffb23
LB
1361 int *got_picture_ptr,
1362 AVPacket *avpkt)
7a00bbad 1363{
759001c5 1364 AVCodecInternal *avci = avctx->internal;
de6d9b64 1365 int ret;
115329f1 1366
419ffb23
LB
1367 *got_picture_ptr = 0;
1368 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
0ecca7a4 1369 return -1;
393cbb96
MN
1370
1371 avctx->pkt = avpkt;
c5d907b6 1372 apply_param_change(avctx, avpkt);
393cbb96 1373
1bc64c28
AK
1374 avcodec_get_frame_defaults(picture);
1375
759001c5
AK
1376 if (!avctx->refcounted_frames)
1377 av_frame_unref(&avci->to_free);
1378
419ffb23
LB
1379 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
1380 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
1381 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1382 avpkt);
37b00b47
AS
1383 else {
1384 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
419ffb23 1385 avpkt);
bd255f9f
JG
1386 picture->pkt_dts = avpkt->dts;
1387 /* get_buffer is supposed to set frame parameters */
1388 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
1389 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1390 picture->width = avctx->width;
1391 picture->height = avctx->height;
1392 picture->format = avctx->pix_fmt;
1393 }
37b00b47 1394 }
6bb925f4 1395
bb628dae 1396 emms_c(); //needed to avoid an emms_c() call before every return;
115329f1 1397
759001c5
AK
1398 if (ret < 0 && picture->data[0])
1399 av_frame_unref(picture);
1400
1401 if (*got_picture_ptr) {
1402 if (!avctx->refcounted_frames) {
1403 avci->to_free = *picture;
1404 avci->to_free.extended_data = avci->to_free.data;
e036bb78 1405 memset(picture->buf, 0, sizeof(picture->buf));
759001c5
AK
1406 }
1407
934982c4 1408 avctx->frame_number++;
759001c5 1409 }
419ffb23
LB
1410 } else
1411 ret = 0;
934982c4 1412
b437cec1
AK
1413 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1414 * make sure it's set correctly */
1415 picture->extended_data = picture->data;
1416
de6d9b64
FB
1417 return ret;
1418}
1419
0eea2129
JR
1420int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1421 AVFrame *frame,
1422 int *got_frame_ptr,
1423 AVPacket *avpkt)
1424{
759001c5 1425 AVCodecInternal *avci = avctx->internal;
b437cec1 1426 int planar, channels;
0eea2129
JR
1427 int ret = 0;
1428
1429 *got_frame_ptr = 0;
de6d9b64 1430
393cbb96
MN
1431 avctx->pkt = avpkt;
1432
6326afd5
JR
1433 if (!avpkt->data && avpkt->size) {
1434 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1435 return AVERROR(EINVAL);
1436 }
1437
f13db94d
MS
1438 apply_param_change(avctx, avpkt);
1439
1bc64c28
AK
1440 avcodec_get_frame_defaults(frame);
1441
759001c5
AK
1442 if (!avctx->refcounted_frames)
1443 av_frame_unref(&avci->to_free);
1444
0eea2129
JR
1445 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1446 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1447 if (ret >= 0 && *got_frame_ptr) {
1448 avctx->frame_number++;
1449 frame->pkt_dts = avpkt->dts;
8a4a5f6f
SS
1450 if (frame->format == AV_SAMPLE_FMT_NONE)
1451 frame->format = avctx->sample_fmt;
759001c5
AK
1452
1453 if (!avctx->refcounted_frames) {
1454 avci->to_free = *frame;
1455 avci->to_free.extended_data = avci->to_free.data;
e036bb78
AK
1456 memset(frame->buf, 0, sizeof(frame->buf));
1457 frame->extended_buf = NULL;
1458 frame->nb_extended_buf = 0;
759001c5 1459 }
e55e8b2c 1460 } else if (frame->data[0])
759001c5 1461 av_frame_unref(frame);
ac66834c 1462 }
b437cec1
AK
1463
1464 /* many decoders assign whole AVFrames, thus overwriting extended_data;
1465 * make sure it's set correctly; assume decoders that actually use
1466 * extended_data are doing it correctly */
1467 planar = av_sample_fmt_is_planar(frame->format);
1468 channels = av_get_channel_layout_nb_channels(frame->channel_layout);
1469 if (!(planar && channels > AV_NUM_DATA_POINTERS))
1470 frame->extended_data = frame->data;
1471
de6d9b64
FB
1472 return ret;
1473}
1474
7a00bbad 1475int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
419ffb23
LB
1476 int *got_sub_ptr,
1477 AVPacket *avpkt)
7a00bbad 1478{
240c1657
FB
1479 int ret;
1480
393cbb96 1481 avctx->pkt = avpkt;
240c1657 1482 *got_sub_ptr = 0;
7a00bbad 1483 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
240c1657
FB
1484 if (*got_sub_ptr)
1485 avctx->frame_number++;
1486 return ret;
1487}
1488
e1d7c883
RD
1489void avsubtitle_free(AVSubtitle *sub)
1490{
1491 int i;
1492
419ffb23 1493 for (i = 0; i < sub->num_rects; i++) {
8b834ac5
RD
1494 av_freep(&sub->rects[i]->pict.data[0]);
1495 av_freep(&sub->rects[i]->pict.data[1]);
1496 av_freep(&sub->rects[i]->pict.data[2]);
1497 av_freep(&sub->rects[i]->pict.data[3]);
1498 av_freep(&sub->rects[i]->text);
1499 av_freep(&sub->rects[i]->ass);
1500 av_freep(&sub->rects[i]);
e1d7c883
RD
1501 }
1502
8b834ac5 1503 av_freep(&sub->rects);
e1d7c883
RD
1504
1505 memset(sub, 0, sizeof(AVSubtitle));
1506}
1507
0752cd39 1508av_cold int avcodec_close(AVCodecContext *avctx)
de6d9b64 1509{
f988ce6c 1510 /* If there is a user-supplied mutex locking routine, call it. */
088f38a4
DB
1511 if (lockmgr_cb) {
1512 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
f988ce6c
1513 return -1;
1514 }
1515
ddebfb15 1516 entangled_thread_counter++;
419ffb23 1517 if (entangled_thread_counter != 1) {
ddebfb15
MN
1518 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1519 entangled_thread_counter--;
1520 return -1;
1521 }
1522
0e72ad95 1523 if (avcodec_is_open(avctx)) {
759001c5
AK
1524 FramePool *pool = avctx->internal->pool;
1525 int i;
0e72ad95
AK
1526 if (HAVE_THREADS && avctx->thread_opaque)
1527 ff_thread_free(avctx);
1528 if (avctx->codec && avctx->codec->close)
1529 avctx->codec->close(avctx);
0e72ad95 1530 avctx->coded_frame = NULL;
759001c5
AK
1531 if (!avctx->refcounted_frames)
1532 av_frame_unref(&avctx->internal->to_free);
1533 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
1534 av_buffer_pool_uninit(&pool->pools[i]);
1535 av_freep(&avctx->internal->pool);
0e72ad95
AK
1536 av_freep(&avctx->internal);
1537 }
1538
1539 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
36773283
AK
1540 av_opt_free(avctx->priv_data);
1541 av_opt_free(avctx);
3123dd79 1542 av_freep(&avctx->priv_data);
44fe77b3 1543 if (av_codec_is_encoder(avctx->codec))
c4f267ab 1544 av_freep(&avctx->extradata);
de6d9b64 1545 avctx->codec = NULL;
37b00b47 1546 avctx->active_thread_type = 0;
ddebfb15 1547 entangled_thread_counter--;
f988ce6c
1548
1549 /* Release any user-supplied mutex. */
088f38a4
DB
1550 if (lockmgr_cb) {
1551 (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
f988ce6c 1552 }
de6d9b64
FB
1553 return 0;
1554}
1555
a4aa20fb 1556static AVCodec *find_encdec(enum AVCodecID id, int encoder)
de6d9b64 1557{
419ffb23 1558 AVCodec *p, *experimental = NULL;
de6d9b64
FB
1559 p = first_avcodec;
1560 while (p) {
a4aa20fb
NC
1561 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
1562 p->id == id) {
93ebfeea
JG
1563 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1564 experimental = p;
1565 } else
1566 return p;
1567 }
de6d9b64
FB
1568 p = p->next;
1569 }
93ebfeea 1570 return experimental;
de6d9b64
FB
1571}
1572
a4aa20fb
NC
1573AVCodec *avcodec_find_encoder(enum AVCodecID id)
1574{
1575 return find_encdec(id, 1);
1576}
1577
98f3b098
A
1578AVCodec *avcodec_find_encoder_by_name(const char *name)
1579{
1580 AVCodec *p;
fc228c90
AJ
1581 if (!name)
1582 return NULL;
98f3b098
A
1583 p = first_avcodec;
1584 while (p) {
419ffb23 1585 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
98f3b098
A
1586 return p;
1587 p = p->next;
1588 }
1589 return NULL;
1590}
1591
36ef5369 1592AVCodec *avcodec_find_decoder(enum AVCodecID id)
de6d9b64 1593{
a4aa20fb 1594 return find_encdec(id, 0);
de6d9b64
FB
1595}
1596
1597AVCodec *avcodec_find_decoder_by_name(const char *name)
1598{
1599 AVCodec *p;
fc228c90
AJ
1600 if (!name)
1601 return NULL;
de6d9b64
FB
1602 p = first_avcodec;
1603 while (p) {
419ffb23 1604 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
de6d9b64
FB
1605 return p;
1606 p = p->next;
1607 }
1608 return NULL;
1609}
1610
406aa93f 1611static int get_bit_rate(AVCodecContext *ctx)
ce34ff6b
RK
1612{
1613 int bit_rate;
1614 int bits_per_sample;
1615
419ffb23 1616 switch (ctx->codec_type) {
72415b2a 1617 case AVMEDIA_TYPE_VIDEO:
4563cf24
SS
1618 case AVMEDIA_TYPE_DATA:
1619 case AVMEDIA_TYPE_SUBTITLE:
1620 case AVMEDIA_TYPE_ATTACHMENT:
ce34ff6b
RK
1621 bit_rate = ctx->bit_rate;
1622 break;
72415b2a 1623 case AVMEDIA_TYPE_AUDIO:
ce34ff6b
RK
1624 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1625 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1626 break;
ce34ff6b
RK
1627 default:
1628 bit_rate = 0;
1629 break;
1630 }
1631 return bit_rate;
1632}
1633
7e566bbe
SS
1634size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1635{
1636 int i, len, ret = 0;
1637
70762508
SS
1638#define TAG_PRINT(x) \
1639 (((x) >= '0' && (x) <= '9') || \
1640 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
1641 ((x) == '.' || (x) == ' '))
1642
7e566bbe
SS
1643 for (i = 0; i < 4; i++) {
1644 len = snprintf(buf, buf_size,
70762508 1645 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
419ffb23
LB
1646 buf += len;
1647 buf_size = buf_size > len ? buf_size - len : 0;
1648 ret += len;
1649 codec_tag >>= 8;
7e566bbe
SS
1650 }
1651 return ret;
1652}
1653
de6d9b64
FB
1654void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1655{
1656 const char *codec_name;
2a81f4bd 1657 const char *profile = NULL;
0a0f19b5 1658 const AVCodec *p;
de6d9b64 1659 char buf1[32];
a96b68b7 1660 int bitrate;
59771f71 1661 AVRational display_aspect_ratio;
de6d9b64 1662
df8d5eaa
AC
1663 if (enc->codec)
1664 p = enc->codec;
1665 else if (encode)
de6d9b64
FB
1666 p = avcodec_find_encoder(enc->codec_id);
1667 else
1668 p = avcodec_find_decoder(enc->codec_id);
1669
1670 if (p) {
1671 codec_name = p->name;
2a81f4bd 1672 profile = av_get_profile_name(p, enc->profile);
36ef5369 1673 } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
985180a1 1674 /* fake mpeg2 transport stream codec (currently not
419ffb23 1675 * registered) */
985180a1 1676 codec_name = "mpeg2ts";
de6d9b64
FB
1677 } else if (enc->codec_name[0] != '\0') {
1678 codec_name = enc->codec_name;
1679 } else {
1680 /* output avi tags */
ab0b5378
SS
1681 char tag_buf[32];
1682 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1683 snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
de6d9b64
FB
1684 codec_name = buf1;
1685 }
1686
419ffb23 1687 switch (enc->codec_type) {
72415b2a 1688 case AVMEDIA_TYPE_VIDEO:
de6d9b64
FB
1689 snprintf(buf, buf_size,
1690 "Video: %s%s",
7d1c3fc1 1691 codec_name, enc->mb_decision ? " (hq)" : "");
2a81f4bd
AH
1692 if (profile)
1693 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1694 " (%s)", profile);
716d413c 1695 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
cf087595
FB
1696 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1697 ", %s",
94bed8e5 1698 av_get_pix_fmt_name(enc->pix_fmt));
cf087595 1699 }
de6d9b64
FB
1700 if (enc->width) {
1701 snprintf(buf + strlen(buf), buf_size - strlen(buf),
21189011
MN
1702 ", %dx%d",
1703 enc->width, enc->height);
7ee4dd02 1704 if (enc->sample_aspect_ratio.num) {
cbaf50f8 1705 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
419ffb23
LB
1706 enc->width * enc->sample_aspect_ratio.num,
1707 enc->height * enc->sample_aspect_ratio.den,
1708 1024 * 1024);
cbaf50f8
BC
1709 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1710 " [PAR %d:%d DAR %d:%d]",
1711 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1712 display_aspect_ratio.num, display_aspect_ratio.den);
7ee4dd02 1713 }
419ffb23
LB
1714 if (av_log_get_level() >= AV_LOG_DEBUG) {
1715 int g = av_gcd(enc->time_base.num, enc->time_base.den);
21189011 1716 snprintf(buf + strlen(buf), buf_size - strlen(buf),
419ffb23
LB
1717 ", %d/%d",
1718 enc->time_base.num / g, enc->time_base.den / g);
21189011 1719 }
de6d9b64 1720 }
4bfad535
FB
1721 if (encode) {
1722 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1723 ", q=%d-%d", enc->qmin, enc->qmax);
1724 }
de6d9b64 1725 break;
72415b2a 1726 case AVMEDIA_TYPE_AUDIO:
de6d9b64
FB
1727 snprintf(buf, buf_size,
1728 "Audio: %s",
1729 codec_name);
2a81f4bd
AH
1730 if (profile)
1731 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1732 " (%s)", profile);
de6d9b64
FB
1733 if (enc->sample_rate) {
1734 snprintf(buf + strlen(buf), buf_size - strlen(buf),
0d72e7d0 1735 ", %d Hz", enc->sample_rate);
de6d9b64 1736 }
0d72e7d0 1737 av_strlcat(buf, ", ", buf_size);
63e8d976 1738 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
5d6e4c16 1739 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
9e82a113 1740 snprintf(buf + strlen(buf), buf_size - strlen(buf),
ba7d6e79 1741 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
9e82a113 1742 }
de6d9b64 1743 break;
72415b2a 1744 case AVMEDIA_TYPE_DATA:
985180a1 1745 snprintf(buf, buf_size, "Data: %s", codec_name);
240c1657 1746 break;
72415b2a 1747 case AVMEDIA_TYPE_SUBTITLE:
240c1657 1748 snprintf(buf, buf_size, "Subtitle: %s", codec_name);
985180a1 1749 break;
72415b2a 1750 case AVMEDIA_TYPE_ATTACHMENT:
f8d7c9d3 1751 snprintf(buf, buf_size, "Attachment: %s", codec_name);
f8d7c9d3 1752 break;
de6d9b64 1753 default:
9fe5a7b8
MN
1754 snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1755 return;
de6d9b64 1756 }
4bfad535
FB
1757 if (encode) {
1758 if (enc->flags & CODEC_FLAG_PASS1)
1759 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1760 ", pass 1");
1761 if (enc->flags & CODEC_FLAG_PASS2)
1762 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1763 ", pass 2");
1764 }
406aa93f 1765 bitrate = get_bit_rate(enc);
a96b68b7 1766 if (bitrate != 0) {
115329f1 1767 snprintf(buf + strlen(buf), buf_size - strlen(buf),
a96b68b7 1768 ", %d kb/s", bitrate / 1000);
de6d9b64
FB
1769 }
1770}
1771
060ec0a8
AH
1772const char *av_get_profile_name(const AVCodec *codec, int profile)
1773{
1774 const AVProfile *p;
1775 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1776 return NULL;
1777
1778 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1779 if (p->profile == profile)
1780 return p->name;
1781
1782 return NULL;
1783}
1784
419ffb23 1785unsigned avcodec_version(void)
156e5023 1786{
419ffb23 1787 return LIBAVCODEC_VERSION_INT;
156e5023 1788}
cf087595 1789
41600690 1790const char *avcodec_configuration(void)
c1736936 1791{
29ba0911 1792 return LIBAV_CONFIGURATION;
c1736936
DB
1793}
1794
41600690 1795const char *avcodec_license(void)
c1736936
DB
1796{
1797#define LICENSE_PREFIX "libavcodec license: "
a03be6e1 1798 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
c1736936
DB
1799}
1800
1c2a8c7f
MN
1801void avcodec_flush_buffers(AVCodecContext *avctx)
1802{
419ffb23 1803 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
37b00b47 1804 ff_thread_flush(avctx);
419ffb23 1805 else if (avctx->codec->flush)
7a06ff14 1806 avctx->codec->flush(avctx);
985f34b7
JG
1807
1808 if (!avctx->refcounted_frames)
1809 av_frame_unref(&avctx->internal->to_free);
1c2a8c7f
MN
1810}
1811
36ef5369 1812int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
6699d074 1813{
419ffb23 1814 switch (codec_id) {
36ef5369
AK
1815 case AV_CODEC_ID_ADPCM_CT:
1816 case AV_CODEC_ID_ADPCM_IMA_APC:
1817 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
1818 case AV_CODEC_ID_ADPCM_IMA_WS:
1819 case AV_CODEC_ID_ADPCM_G722:
1820 case AV_CODEC_ID_ADPCM_YAMAHA:
f32fd318 1821 return 4;
36ef5369
AK
1822 case AV_CODEC_ID_PCM_ALAW:
1823 case AV_CODEC_ID_PCM_MULAW:
1824 case AV_CODEC_ID_PCM_S8:
1825 case AV_CODEC_ID_PCM_U8:
1826 case AV_CODEC_ID_PCM_ZORK:
ac3e1834 1827 return 8;
36ef5369
AK
1828 case AV_CODEC_ID_PCM_S16BE:
1829 case AV_CODEC_ID_PCM_S16LE:
1830 case AV_CODEC_ID_PCM_S16LE_PLANAR:
1831 case AV_CODEC_ID_PCM_U16BE:
1832 case AV_CODEC_ID_PCM_U16LE:
ac3e1834 1833 return 16;
36ef5369
AK
1834 case AV_CODEC_ID_PCM_S24DAUD:
1835 case AV_CODEC_ID_PCM_S24BE:
1836 case AV_CODEC_ID_PCM_S24LE:
1837 case AV_CODEC_ID_PCM_U24BE:
1838 case AV_CODEC_ID_PCM_U24LE:
ac3e1834 1839 return 24;
36ef5369
AK
1840 case AV_CODEC_ID_PCM_S32BE:
1841 case AV_CODEC_ID_PCM_S32LE:
1842 case AV_CODEC_ID_PCM_U32BE:
1843 case AV_CODEC_ID_PCM_U32LE:
1844 case AV_CODEC_ID_PCM_F32BE:
1845 case AV_CODEC_ID_PCM_F32LE:
ac3e1834 1846 return 32;
36ef5369
AK
1847 case AV_CODEC_ID_PCM_F64BE:
1848 case AV_CODEC_ID_PCM_F64LE:
143a5d6f 1849 return 64;
ac3e1834
BC
1850 default:
1851 return 0;
1852 }
1853}
1854
36ef5369 1855int av_get_bits_per_sample(enum AVCodecID codec_id)
6699d074
JR
1856{
1857 switch (codec_id) {
36ef5369 1858 case AV_CODEC_ID_ADPCM_SBPRO_2:
6699d074 1859 return 2;
36ef5369 1860 case AV_CODEC_ID_ADPCM_SBPRO_3:
6699d074 1861 return 3;
36ef5369
AK
1862 case AV_CODEC_ID_ADPCM_SBPRO_4:
1863 case AV_CODEC_ID_ADPCM_IMA_WAV:
1864 case AV_CODEC_ID_ADPCM_IMA_QT:
1865 case AV_CODEC_ID_ADPCM_SWF:
1866 case AV_CODEC_ID_ADPCM_MS:
6699d074
JR
1867 return 4;
1868 default:
1869 return av_get_exact_bits_per_sample(codec_id);
1870 }
1871}
1872
9524cf79
JR
1873int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
1874{
1875 int id, sr, ch, ba, tag, bps;
1876
1877 id = avctx->codec_id;
1878 sr = avctx->sample_rate;
1879 ch = avctx->channels;
1880 ba = avctx->block_align;
1881 tag = avctx->codec_tag;
1882 bps = av_get_exact_bits_per_sample(avctx->codec_id);
1883
1884 /* codecs with an exact constant bits per sample */
1885 if (bps > 0 && ch > 0 && frame_bytes > 0)
1886 return (frame_bytes * 8) / (bps * ch);
1887 bps = avctx->bits_per_coded_sample;
1888
1889 /* codecs with a fixed packet duration */
1890 switch (id) {
36ef5369
AK
1891 case AV_CODEC_ID_ADPCM_ADX: return 32;
1892 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
1893 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
1894 case AV_CODEC_ID_AMR_NB:
1895 case AV_CODEC_ID_GSM:
1896 case AV_CODEC_ID_QCELP:
1897 case AV_CODEC_ID_RA_144:
1898 case AV_CODEC_ID_RA_288: return 160;
1899 case AV_CODEC_ID_IMC: return 256;
1900 case AV_CODEC_ID_AMR_WB:
1901 case AV_CODEC_ID_GSM_MS: return 320;
1902 case AV_CODEC_ID_MP1: return 384;
1903 case AV_CODEC_ID_ATRAC1: return 512;
1904 case AV_CODEC_ID_ATRAC3: return 1024;
1905 case AV_CODEC_ID_MP2:
1906 case AV_CODEC_ID_MUSEPACK7: return 1152;
1907 case AV_CODEC_ID_AC3: return 1536;
9524cf79
JR
1908 }
1909
1910 if (sr > 0) {
1911 /* calc from sample rate */
36ef5369 1912 if (id == AV_CODEC_ID_TTA)
9524cf79
JR
1913 return 256 * sr / 245;
1914
1915 if (ch > 0) {
1916 /* calc from sample rate and channels */
36ef5369 1917 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
9524cf79
JR
1918 return (480 << (sr / 22050)) / ch;
1919 }
1920 }
1921
1922 if (ba > 0) {
1923 /* calc from block_align */
36ef5369 1924 if (id == AV_CODEC_ID_SIPR) {
9524cf79
JR
1925 switch (ba) {
1926 case 20: return 160;
1927 case 19: return 144;
1928 case 29: return 288;
1929 case 37: return 480;
1930 }
36ef5369 1931 } else if (id == AV_CODEC_ID_ILBC) {
3641b048
MS
1932 switch (ba) {
1933 case 38: return 160;
1934 case 50: return 240;
1935 }
9524cf79
JR
1936 }
1937 }
1938
1939 if (frame_bytes > 0) {
1940 /* calc from frame_bytes only */
36ef5369 1941 if (id == AV_CODEC_ID_TRUESPEECH)
9524cf79 1942 return 240 * (frame_bytes / 32);
36ef5369 1943 if (id == AV_CODEC_ID_NELLYMOSER)
9524cf79
JR
1944 return 256 * (frame_bytes / 64);
1945
1946 if (bps > 0) {
1947 /* calc from frame_bytes and bits_per_coded_sample */
36ef5369 1948 if (id == AV_CODEC_ID_ADPCM_G726)
9524cf79
JR
1949 return frame_bytes * 8 / bps;
1950 }
1951
1952 if (ch > 0) {
1953 /* calc from frame_bytes and channels */
1954 switch (id) {
36ef5369
AK
1955 case AV_CODEC_ID_ADPCM_4XM:
1956 case AV_CODEC_ID_ADPCM_IMA_ISS:
9524cf79 1957 return (frame_bytes - 4 * ch) * 2 / ch;
36ef5369 1958 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
9524cf79 1959 return (frame_bytes - 4) * 2 / ch;
36ef5369 1960 case AV_CODEC_ID_ADPCM_IMA_AMV:
9524cf79 1961 return (frame_bytes - 8) * 2 / ch;
36ef5369 1962 case AV_CODEC_ID_ADPCM_XA:
9524cf79 1963 return (frame_bytes / 128) * 224 / ch;
36ef5369 1964 case AV_CODEC_ID_INTERPLAY_DPCM:
9524cf79 1965 return (frame_bytes - 6 - ch) / ch;
36ef5369 1966 case AV_CODEC_ID_ROQ_DPCM:
9524cf79 1967 return (frame_bytes - 8) / ch;
36ef5369 1968 case AV_CODEC_ID_XAN_DPCM:
9524cf79 1969 return (frame_bytes - 2 * ch) / ch;
36ef5369 1970 case AV_CODEC_ID_MACE3:
9524cf79 1971 return 3 * frame_bytes / ch;
36ef5369 1972 case AV_CODEC_ID_MACE6:
9524cf79 1973 return 6 * frame_bytes / ch;
36ef5369 1974 case AV_CODEC_ID_PCM_LXF:
9524cf79
JR
1975 return 2 * (frame_bytes / (5 * ch));
1976 }
1977
1978 if (tag) {
1979 /* calc from frame_bytes, channels, and codec_tag */
36ef5369 1980 if (id == AV_CODEC_ID_SOL_DPCM) {
9524cf79
JR
1981 if (tag == 3)
1982 return frame_bytes / ch;
1983 else
1984 return frame_bytes * 2 / ch;
1985 }
1986 }
1987
1988 if (ba > 0) {
1989 /* calc from frame_bytes, channels, and block_align */
1990 int blocks = frame_bytes / ba;
1991 switch (avctx->codec_id) {
36ef5369 1992 case AV_CODEC_ID_ADPCM_IMA_WAV:
9524cf79 1993 return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
36ef5369 1994 case AV_CODEC_ID_ADPCM_IMA_DK3:
c346f630 1995 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
36ef5369 1996 case AV_CODEC_ID_ADPCM_IMA_DK4:
9524cf79 1997 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
36ef5369 1998 case AV_CODEC_ID_ADPCM_MS:
9524cf79
JR
1999 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2000 }
2001 }
2002
2003 if (bps > 0) {
2004 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2005 switch (avctx->codec_id) {
36ef5369 2006 case AV_CODEC_ID_PCM_DVD:
9524cf79 2007 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
36ef5369 2008 case AV_CODEC_ID_PCM_BLURAY:
9524cf79 2009 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
36ef5369 2010 case AV_CODEC_ID_S302M:
9524cf79
JR
2011 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2012 }
2013 }
2014 }
2015 }
2016
2017 return 0;
2018}
2019
b250f9c6 2020#if !HAVE_THREADS
419ffb23
LB
2021int ff_thread_init(AVCodecContext *s)
2022{
ca8ad847
MN
2023 return -1;
2024}
419ffb23 2025
ca8ad847 2026#endif
ad2b531d
MR
2027
2028unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2029{
2030 unsigned int n = 0;
2031
419ffb23 2032 while (v >= 0xff) {
ad2b531d
MR
2033 *s++ = 0xff;
2034 v -= 0xff;
2035 n++;
2036 }
2037 *s = v;
2038 n++;
2039 return n;
2040}
1005f542 2041
419ffb23
LB
2042int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
2043{
c46eeae2 2044 int i;
419ffb23 2045 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
c46eeae2
MN
2046 return i;
2047}
2048
f099d3d1 2049#if FF_API_MISSING_SAMPLE
ce863d7f 2050void av_log_missing_feature(void *avc, const char *feature, int want_sample)
ea779d91 2051{
f75f4194 2052 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
db323491 2053 "version to the newest one from Git. If the problem still "
ea779d91 2054 "occurs, it means that your file has a feature which has not "
3fd3632f 2055 "been implemented.\n", feature);
ea779d91 2056 if(want_sample)
ce863d7f 2057 av_log_ask_for_sample(avc, NULL);
0ba39dd1
KG
2058}
2059
44f566b7 2060void av_log_ask_for_sample(void *avc, const char *msg, ...)
0ba39dd1 2061{
44f566b7
DB
2062 va_list argument_list;
2063
2064 va_start(argument_list, msg);
2065
0ba39dd1 2066 if (msg)
44f566b7 2067 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
0ba39dd1 2068 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
f0a41afd 2069 "of this file to ftp://upload.libav.org/incoming/ "
21de9204 2070 "and contact the libav-devel mailing list.\n");
44f566b7
DB
2071
2072 va_end(argument_list);
ea779d91 2073}
f099d3d1 2074#endif /* FF_API_MISSING_SAMPLE */
c895618b
MN
2075
2076static AVHWAccel *first_hwaccel = NULL;
2077
2078void av_register_hwaccel(AVHWAccel *hwaccel)
2079{
2080 AVHWAccel **p = &first_hwaccel;
2081 while (*p)
2082 p = &(*p)->next;
2083 *p = hwaccel;
2084 hwaccel->next = NULL;
2085}
414d9d7f
MN
2086
2087AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2088{
2089 return hwaccel ? hwaccel->next : first_hwaccel;
2090}
6059f13c 2091
716d413c 2092AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
6059f13c 2093{
419ffb23 2094 AVHWAccel *hwaccel = NULL;
6059f13c 2095
419ffb23
LB
2096 while ((hwaccel = av_hwaccel_next(hwaccel)))
2097 if (hwaccel->id == codec_id
6059f13c
MN
2098 && hwaccel->pix_fmt == pix_fmt)
2099 return hwaccel;
6059f13c
MN
2100 return NULL;
2101}
f988ce6c
2102
2103int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2104{
088f38a4
DB
2105 if (lockmgr_cb) {
2106 if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
f988ce6c 2107 return -1;
088f38a4 2108 if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2d1b6fb7 2109 return -1;
f988ce6c
2110 }
2111
088f38a4 2112 lockmgr_cb = cb;
f988ce6c 2113
088f38a4
DB
2114 if (lockmgr_cb) {
2115 if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
f988ce6c 2116 return -1;
088f38a4 2117 if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2d1b6fb7
MS
2118 return -1;
2119 }
2120 return 0;
2121}
2122
2123int avpriv_lock_avformat(void)
2124{
088f38a4
DB
2125 if (lockmgr_cb) {
2126 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2d1b6fb7
MS
2127 return -1;
2128 }
2129 return 0;
2130}
2131
2132int avpriv_unlock_avformat(void)
2133{
088f38a4
DB
2134 if (lockmgr_cb) {
2135 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2d1b6fb7 2136 return -1;
f988ce6c
2137 }
2138 return 0;
2139}
603a5f04 2140
0842d589 2141unsigned int avpriv_toupper4(unsigned int x)
603a5f04 2142{
efa7f420
RD
2143 return av_toupper(x & 0xFF) +
2144 (av_toupper((x >> 8) & 0xFF) << 8) +
2145 (av_toupper((x >> 16) & 0xFF) << 16) +
2146 (av_toupper((x >> 24) & 0xFF) << 24);
603a5f04 2147}
37b00b47 2148
759001c5
AK
2149int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
2150{
2151 int ret;
2152
2153 dst->owner = src->owner;
2154
2155 ret = av_frame_ref(dst->f, src->f);
2156 if (ret < 0)
2157 return ret;
2158
2159 if (src->progress &&
2160 !(dst->progress = av_buffer_ref(src->progress))) {
2161 ff_thread_release_buffer(dst->owner, dst);
2162 return AVERROR(ENOMEM);
2163 }
2164
2165 return 0;
2166}
2167
27237d52 2168#if !HAVE_THREADS
37b00b47 2169
7c45087b 2170int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
37b00b47
AS
2171{
2172 f->owner = avctx;
7c45087b 2173 return ff_get_buffer(avctx, f->f, flags);
37b00b47
AS
2174}
2175
7c45087b 2176void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
37b00b47 2177{
7c45087b 2178 av_frame_unref(f->f);
37b00b47
AS
2179}
2180
2181void ff_thread_finish_setup(AVCodecContext *avctx)
2182{
2183}
2184
7c45087b 2185void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
37b00b47
AS
2186{
2187}
2188
7c45087b 2189void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
37b00b47
AS
2190{
2191}
2192
2193#endif
65af48b5 2194
36ef5369 2195enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
bca06e77 2196{
36ef5369 2197 if (codec_id <= AV_CODEC_ID_NONE)
bca06e77 2198 return AVMEDIA_TYPE_UNKNOWN;
36ef5369 2199 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
bca06e77 2200 return AVMEDIA_TYPE_VIDEO;
36ef5369 2201 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
bca06e77 2202 return AVMEDIA_TYPE_AUDIO;
36ef5369 2203 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
bca06e77
AK
2204 return AVMEDIA_TYPE_SUBTITLE;
2205
2206 return AVMEDIA_TYPE_UNKNOWN;
2207}
af08d9ae
AK
2208
2209int avcodec_is_open(AVCodecContext *s)
2210{
2211 return !!s->internal;
2212}
75644335 2213
f1e93986
MS
2214const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
2215 const uint8_t *end,
2216 uint32_t * restrict state)
75644335
MS
2217{
2218 int i;
2219
2220 assert(p <= end);
2221 if (p >= end)
2222 return end;
2223
2224 for (i = 0; i < 3; i++) {
2225 uint32_t tmp = *state << 8;
2226 *state = tmp + *(p++);
2227 if (tmp == 0x100 || p == end)
2228 return p;
2229 }
2230
2231 while (p < end) {
2232 if (p[-1] > 1 ) p += 3;
2233 else if (p[-2] ) p += 2;
2234 else if (p[-3]|(p[-1]-1)) p++;
2235 else {
2236 p++;
2237 break;
2238 }
2239 }
2240
2241 p = FFMIN(p, end) - 4;
2242 *state = AV_RB32(p);
2243
2244 return p + 4;
2245}