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