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