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