1 /*
2 * utils for libavcodec
3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * utils.
26 */
27
28 #include "config.h"
29
30 #include "libavutil/atomic.h"
31 #include "libavutil/attributes.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/avstring.h"
34 #include "libavutil/bprint.h"
35 #include "libavutil/channel_layout.h"
36 #include "libavutil/crc.h"
37 #include "libavutil/frame.h"
38 #include "libavutil/internal.h"
39 #include "libavutil/mathematics.h"
40 #include "libavutil/pixdesc.h"
41 #include "libavutil/imgutils.h"
42 #include "libavutil/samplefmt.h"
43 #include "libavutil/dict.h"
44 #include "avcodec.h"
45 #include "libavutil/opt.h"
46 #include "me_cmp.h"
47 #include "mpegvideo.h"
48 #include "thread.h"
49 #include "frame_thread_encoder.h"
50 #include "internal.h"
51 #include "raw.h"
52 #include "bytestream.h"
53 #include "version.h"
54 #include <stdlib.h>
55 #include <stdarg.h>
56 #include <limits.h>
57 #include <float.h>
58
59 #if CONFIG_ICONV
60 # include <iconv.h>
61 #endif
62
63 #if HAVE_PTHREADS
64 #include <pthread.h>
65 #elif HAVE_W32THREADS
66 #include "compat/w32pthreads.h"
67 #elif HAVE_OS2THREADS
68 #include "compat/os2threads.h"
69 #endif
70
71 #ifndef SIZE_MAX
72 # ifdef __SIZE_MAX__
73 # define SIZE_MAX __SIZE_MAX__
74 # else
75 # error no SIZE_MAX
76 # endif
77 #endif
78
79 #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
default_lockmgr_cb(void ** arg,enum AVLockOp op)80 static int default_lockmgr_cb(void **arg, enum AVLockOp op)
81 {
82 void * volatile * mutex = arg;
83 int err;
84
85 switch (op) {
86 case AV_LOCK_CREATE:
87 return 0;
88 case AV_LOCK_OBTAIN:
89 if (!*mutex) {
90 pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
91 if (!tmp)
92 return AVERROR(ENOMEM);
93 if ((err = pthread_mutex_init(tmp, NULL))) {
94 av_free(tmp);
95 return AVERROR(err);
96 }
97 if (avpriv_atomic_ptr_cas(mutex, NULL, tmp)) {
98 pthread_mutex_destroy(tmp);
99 av_free(tmp);
100 }
101 }
102
103 if ((err = pthread_mutex_lock(*mutex)))
104 return AVERROR(err);
105
106 return 0;
107 case AV_LOCK_RELEASE:
108 if ((err = pthread_mutex_unlock(*mutex)))
109 return AVERROR(err);
110
111 return 0;
112 case AV_LOCK_DESTROY:
113 if (*mutex)
114 pthread_mutex_destroy(*mutex);
115 av_free(*mutex);
116 avpriv_atomic_ptr_cas(mutex, *mutex, NULL);
117 return 0;
118 }
119 return 1;
120 }
121 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = default_lockmgr_cb;
122 #else
123 static int (*lockmgr_cb)(void **mutex, enum AVLockOp op) = NULL;
124 #endif
125
126
127 volatile int ff_avcodec_locked;
128 static int volatile entangled_thread_counter = 0;
129 static void *codec_mutex;
130 static void *avformat_mutex;
131
ff_fast_malloc(void * ptr,unsigned int * size,size_t min_size,int zero_realloc)132 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
133 {
134 void **p = ptr;
135 if (min_size < *size)
136 return 0;
137 min_size = FFMAX(17 * min_size / 16 + 32, min_size);
138 av_free(*p);
139 *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
140 if (!*p)
141 min_size = 0;
142 *size = min_size;
143 return 1;
144 }
145
av_fast_padded_malloc(void * ptr,unsigned int * size,size_t min_size)146 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
147 {
148 uint8_t **p = ptr;
149 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
150 av_freep(p);
151 *size = 0;
152 return;
153 }
154 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
155 memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
156 }
157
av_fast_padded_mallocz(void * ptr,unsigned int * size,size_t min_size)158 void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
159 {
160 uint8_t **p = ptr;
161 if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
162 av_freep(p);
163 *size = 0;
164 return;
165 }
166 if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
167 memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
168 }
169
170 /* encoder management */
171 static AVCodec *first_avcodec = NULL;
172 static AVCodec **last_avcodec = &first_avcodec;
173
av_codec_next(const AVCodec * c)174 AVCodec *av_codec_next(const AVCodec *c)
175 {
176 if (c)
177 return c->next;
178 else
179 return first_avcodec;
180 }
181
avcodec_init(void)182 static av_cold void avcodec_init(void)
183 {
184 static int initialized = 0;
185
186 if (initialized != 0)
187 return;
188 initialized = 1;
189
190 if (CONFIG_ME_CMP)
191 ff_me_cmp_init_static();
192 }
193
av_codec_is_encoder(const AVCodec * codec)194 int av_codec_is_encoder(const AVCodec *codec)
195 {
196 return codec && (codec->encode_sub || codec->encode2);
197 }
198
av_codec_is_decoder(const AVCodec * codec)199 int av_codec_is_decoder(const AVCodec *codec)
200 {
201 return codec && codec->decode;
202 }
203
avcodec_register(AVCodec * codec)204 av_cold void avcodec_register(AVCodec *codec)
205 {
206 AVCodec **p;
207 avcodec_init();
208 p = last_avcodec;
209 codec->next = NULL;
210
211 while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, codec))
212 p = &(*p)->next;
213 last_avcodec = &codec->next;
214
215 if (codec->init_static_data)
216 codec->init_static_data(codec);
217 }
218
219 #if FF_API_EMU_EDGE
avcodec_get_edge_width(void)220 unsigned avcodec_get_edge_width(void)
221 {
222 return EDGE_WIDTH;
223 }
224 #endif
225
226 #if FF_API_SET_DIMENSIONS
avcodec_set_dimensions(AVCodecContext * s,int width,int height)227 void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
228 {
229 int ret = ff_set_dimensions(s, width, height);
230 if (ret < 0) {
231 av_log(s, AV_LOG_WARNING, "Failed to set dimensions %d %d\n", width, height);
232 }
233 }
234 #endif
235
ff_set_dimensions(AVCodecContext * s,int width,int height)236 int ff_set_dimensions(AVCodecContext *s, int width, int height)
237 {
238 int ret = av_image_check_size(width, height, 0, s);
239
240 if (ret < 0)
241 width = height = 0;
242
243 s->coded_width = width;
244 s->coded_height = height;
245 s->width = FF_CEIL_RSHIFT(width, s->lowres);
246 s->height = FF_CEIL_RSHIFT(height, s->lowres);
247
248 return ret;
249 }
250
ff_set_sar(AVCodecContext * avctx,AVRational sar)251 int ff_set_sar(AVCodecContext *avctx, AVRational sar)
252 {
253 int ret = av_image_check_sar(avctx->width, avctx->height, sar);
254
255 if (ret < 0) {
256 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
257 sar.num, sar.den);
258 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
259 return ret;
260 } else {
261 avctx->sample_aspect_ratio = sar;
262 }
263 return 0;
264 }
265
ff_side_data_update_matrix_encoding(AVFrame * frame,enum AVMatrixEncoding matrix_encoding)266 int ff_side_data_update_matrix_encoding(AVFrame *frame,
267 enum AVMatrixEncoding matrix_encoding)
268 {
269 AVFrameSideData *side_data;
270 enum AVMatrixEncoding *data;
271
272 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
273 if (!side_data)
274 side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
275 sizeof(enum AVMatrixEncoding));
276
277 if (!side_data)
278 return AVERROR(ENOMEM);
279
280 data = (enum AVMatrixEncoding*)side_data->data;
281 *data = matrix_encoding;
282
283 return 0;
284 }
285
avcodec_align_dimensions2(AVCodecContext * s,int * width,int * height,int linesize_align[AV_NUM_DATA_POINTERS])286 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
287 int linesize_align[AV_NUM_DATA_POINTERS])
288 {
289 int i;
290 int w_align = 1;
291 int h_align = 1;
292
293 switch (s->pix_fmt) {
294 case AV_PIX_FMT_YUV420P:
295 case AV_PIX_FMT_YUYV422:
296 case AV_PIX_FMT_YVYU422:
297 case AV_PIX_FMT_UYVY422:
298 case AV_PIX_FMT_YUV422P:
299 case AV_PIX_FMT_YUV440P:
300 case AV_PIX_FMT_YUV444P:
301 case AV_PIX_FMT_GBRAP:
302 case AV_PIX_FMT_GBRP:
303 case AV_PIX_FMT_GRAY8:
304 case AV_PIX_FMT_GRAY16BE:
305 case AV_PIX_FMT_GRAY16LE:
306 case AV_PIX_FMT_YUVJ420P:
307 case AV_PIX_FMT_YUVJ422P:
308 case AV_PIX_FMT_YUVJ440P:
309 case AV_PIX_FMT_YUVJ444P:
310 case AV_PIX_FMT_YUVA420P:
311 case AV_PIX_FMT_YUVA422P:
312 case AV_PIX_FMT_YUVA444P:
313 case AV_PIX_FMT_YUV420P9LE:
314 case AV_PIX_FMT_YUV420P9BE:
315 case AV_PIX_FMT_YUV420P10LE:
316 case AV_PIX_FMT_YUV420P10BE:
317 case AV_PIX_FMT_YUV420P12LE:
318 case AV_PIX_FMT_YUV420P12BE:
319 case AV_PIX_FMT_YUV420P14LE:
320 case AV_PIX_FMT_YUV420P14BE:
321 case AV_PIX_FMT_YUV420P16LE:
322 case AV_PIX_FMT_YUV420P16BE:
323 case AV_PIX_FMT_YUVA420P9LE:
324 case AV_PIX_FMT_YUVA420P9BE:
325 case AV_PIX_FMT_YUVA420P10LE:
326 case AV_PIX_FMT_YUVA420P10BE:
327 case AV_PIX_FMT_YUVA420P16LE:
328 case AV_PIX_FMT_YUVA420P16BE:
329 case AV_PIX_FMT_YUV422P9LE:
330 case AV_PIX_FMT_YUV422P9BE:
331 case AV_PIX_FMT_YUV422P10LE:
332 case AV_PIX_FMT_YUV422P10BE:
333 case AV_PIX_FMT_YUV422P12LE:
334 case AV_PIX_FMT_YUV422P12BE:
335 case AV_PIX_FMT_YUV422P14LE:
336 case AV_PIX_FMT_YUV422P14BE:
337 case AV_PIX_FMT_YUV422P16LE:
338 case AV_PIX_FMT_YUV422P16BE:
339 case AV_PIX_FMT_YUVA422P9LE:
340 case AV_PIX_FMT_YUVA422P9BE:
341 case AV_PIX_FMT_YUVA422P10LE:
342 case AV_PIX_FMT_YUVA422P10BE:
343 case AV_PIX_FMT_YUVA422P16LE:
344 case AV_PIX_FMT_YUVA422P16BE:
345 case AV_PIX_FMT_YUV444P9LE:
346 case AV_PIX_FMT_YUV444P9BE:
347 case AV_PIX_FMT_YUV444P10LE:
348 case AV_PIX_FMT_YUV444P10BE:
349 case AV_PIX_FMT_YUV444P12LE:
350 case AV_PIX_FMT_YUV444P12BE:
351 case AV_PIX_FMT_YUV444P14LE:
352 case AV_PIX_FMT_YUV444P14BE:
353 case AV_PIX_FMT_YUV444P16LE:
354 case AV_PIX_FMT_YUV444P16BE:
355 case AV_PIX_FMT_YUVA444P9LE:
356 case AV_PIX_FMT_YUVA444P9BE:
357 case AV_PIX_FMT_YUVA444P10LE:
358 case AV_PIX_FMT_YUVA444P10BE:
359 case AV_PIX_FMT_YUVA444P16LE:
360 case AV_PIX_FMT_YUVA444P16BE:
361 case AV_PIX_FMT_GBRP9LE:
362 case AV_PIX_FMT_GBRP9BE:
363 case AV_PIX_FMT_GBRP10LE:
364 case AV_PIX_FMT_GBRP10BE:
365 case AV_PIX_FMT_GBRP12LE:
366 case AV_PIX_FMT_GBRP12BE:
367 case AV_PIX_FMT_GBRP14LE:
368 case AV_PIX_FMT_GBRP14BE:
369 case AV_PIX_FMT_GBRP16LE:
370 case AV_PIX_FMT_GBRP16BE:
371 w_align = 16; //FIXME assume 16 pixel per macroblock
372 h_align = 16 * 2; // interlaced needs 2 macroblocks height
373 break;
374 case AV_PIX_FMT_YUV411P:
375 case AV_PIX_FMT_YUVJ411P:
376 case AV_PIX_FMT_UYYVYY411:
377 w_align = 32;
378 h_align = 8;
379 break;
380 case AV_PIX_FMT_YUV410P:
381 if (s->codec_id == AV_CODEC_ID_SVQ1) {
382 w_align = 64;
383 h_align = 64;
384 }
385 break;
386 case AV_PIX_FMT_RGB555:
387 if (s->codec_id == AV_CODEC_ID_RPZA) {
388 w_align = 4;
389 h_align = 4;
390 }
391 break;
392 case AV_PIX_FMT_PAL8:
393 case AV_PIX_FMT_BGR8:
394 case AV_PIX_FMT_RGB8:
395 if (s->codec_id == AV_CODEC_ID_SMC ||
396 s->codec_id == AV_CODEC_ID_CINEPAK) {
397 w_align = 4;
398 h_align = 4;
399 }
400 if (s->codec_id == AV_CODEC_ID_JV) {
401 w_align = 8;
402 h_align = 8;
403 }
404 break;
405 case AV_PIX_FMT_BGR24:
406 if ((s->codec_id == AV_CODEC_ID_MSZH) ||
407 (s->codec_id == AV_CODEC_ID_ZLIB)) {
408 w_align = 4;
409 h_align = 4;
410 }
411 break;
412 case AV_PIX_FMT_RGB24:
413 if (s->codec_id == AV_CODEC_ID_CINEPAK) {
414 w_align = 4;
415 h_align = 4;
416 }
417 break;
418 default:
419 w_align = 1;
420 h_align = 1;
421 break;
422 }
423
424 if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
425 w_align = FFMAX(w_align, 8);
426 }
427
428 *width = FFALIGN(*width, w_align);
429 *height = FFALIGN(*height, h_align);
430 if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
431 // some of the optimized chroma MC reads one line too much
432 // which is also done in mpeg decoders with lowres > 0
433 *height += 2;
434
435 for (i = 0; i < 4; i++)
436 linesize_align[i] = STRIDE_ALIGN;
437 }
438
avcodec_align_dimensions(AVCodecContext * s,int * width,int * height)439 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
440 {
441 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
442 int chroma_shift = desc->log2_chroma_w;
443 int linesize_align[AV_NUM_DATA_POINTERS];
444 int align;
445
446 avcodec_align_dimensions2(s, width, height, linesize_align);
447 align = FFMAX(linesize_align[0], linesize_align[3]);
448 linesize_align[1] <<= chroma_shift;
449 linesize_align[2] <<= chroma_shift;
450 align = FFMAX3(align, linesize_align[1], linesize_align[2]);
451 *width = FFALIGN(*width, align);
452 }
453
avcodec_enum_to_chroma_pos(int * xpos,int * ypos,enum AVChromaLocation pos)454 int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
455 {
456 if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
457 return AVERROR(EINVAL);
458 pos--;
459
460 *xpos = (pos&1) * 128;
461 *ypos = ((pos>>1)^(pos<4)) * 128;
462
463 return 0;
464 }
465
avcodec_chroma_pos_to_enum(int xpos,int ypos)466 enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos)
467 {
468 int pos, xout, yout;
469
470 for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
471 if (avcodec_enum_to_chroma_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
472 return pos;
473 }
474 return AVCHROMA_LOC_UNSPECIFIED;
475 }
476
avcodec_fill_audio_frame(AVFrame * frame,int nb_channels,enum AVSampleFormat sample_fmt,const uint8_t * buf,int buf_size,int align)477 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
478 enum AVSampleFormat sample_fmt, const uint8_t *buf,
479 int buf_size, int align)
480 {
481 int ch, planar, needed_size, ret = 0;
482
483 needed_size = av_samples_get_buffer_size(NULL, nb_channels,
484 frame->nb_samples, sample_fmt,
485 align);
486 if (buf_size < needed_size)
487 return AVERROR(EINVAL);
488
489 planar = av_sample_fmt_is_planar(sample_fmt);
490 if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
491 if (!(frame->extended_data = av_mallocz_array(nb_channels,
492 sizeof(*frame->extended_data))))
493 return AVERROR(ENOMEM);
494 } else {
495 frame->extended_data = frame->data;
496 }
497
498 if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
499 (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
500 sample_fmt, align)) < 0) {
501 if (frame->extended_data != frame->data)
502 av_freep(&frame->extended_data);
503 return ret;
504 }
505 if (frame->extended_data != frame->data) {
506 for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
507 frame->data[ch] = frame->extended_data[ch];
508 }
509
510 return ret;
511 }
512
update_frame_pool(AVCodecContext * avctx,AVFrame * frame)513 static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
514 {
515 FramePool *pool = avctx->internal->pool;
516 int i, ret;
517
518 switch (avctx->codec_type) {
519 case AVMEDIA_TYPE_VIDEO: {
520 AVPicture picture;
521 int size[4] = { 0 };
522 int w = frame->width;
523 int h = frame->height;
524 int tmpsize, unaligned;
525
526 if (pool->format == frame->format &&
527 pool->width == frame->width && pool->height == frame->height)
528 return 0;
529
530 avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
531
532 do {
533 // NOTE: do not align linesizes individually, this breaks e.g. assumptions
534 // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
535 av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
536 // increase alignment of w for next try (rhs gives the lowest bit set in w)
537 w += w & ~(w - 1);
538
539 unaligned = 0;
540 for (i = 0; i < 4; i++)
541 unaligned |= picture.linesize[i] % pool->stride_align[i];
542 } while (unaligned);
543
544 tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
545 NULL, picture.linesize);
546 if (tmpsize < 0)
547 return -1;
548
549 for (i = 0; i < 3 && picture.data[i + 1]; i++)
550 size[i] = picture.data[i + 1] - picture.data[i];
551 size[i] = tmpsize - (picture.data[i] - picture.data[0]);
552
553 for (i = 0; i < 4; i++) {
554 av_buffer_pool_uninit(&pool->pools[i]);
555 pool->linesize[i] = picture.linesize[i];
556 if (size[i]) {
557 pool->pools[i] = av_buffer_pool_init(size[i] + 16 + STRIDE_ALIGN - 1,
558 CONFIG_MEMORY_POISONING ?
559 NULL :
560 av_buffer_allocz);
561 if (!pool->pools[i]) {
562 ret = AVERROR(ENOMEM);
563 goto fail;
564 }
565 }
566 }
567 pool->format = frame->format;
568 pool->width = frame->width;
569 pool->height = frame->height;
570
571 break;
572 }
573 case AVMEDIA_TYPE_AUDIO: {
574 int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
575 int planar = av_sample_fmt_is_planar(frame->format);
576 int planes = planar ? ch : 1;
577
578 if (pool->format == frame->format && pool->planes == planes &&
579 pool->channels == ch && frame->nb_samples == pool->samples)
580 return 0;
581
582 av_buffer_pool_uninit(&pool->pools[0]);
583 ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
584 frame->nb_samples, frame->format, 0);
585 if (ret < 0)
586 goto fail;
587
588 pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
589 if (!pool->pools[0]) {
590 ret = AVERROR(ENOMEM);
591 goto fail;
592 }
593
594 pool->format = frame->format;
595 pool->planes = planes;
596 pool->channels = ch;
597 pool->samples = frame->nb_samples;
598 break;
599 }
600 default: av_assert0(0);
601 }
602 return 0;
603 fail:
604 for (i = 0; i < 4; i++)
605 av_buffer_pool_uninit(&pool->pools[i]);
606 pool->format = -1;
607 pool->planes = pool->channels = pool->samples = 0;
608 pool->width = pool->height = 0;
609 return ret;
610 }
611
audio_get_buffer(AVCodecContext * avctx,AVFrame * frame)612 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
613 {
614 FramePool *pool = avctx->internal->pool;
615 int planes = pool->planes;
616 int i;
617
618 frame->linesize[0] = pool->linesize[0];
619
620 if (planes > AV_NUM_DATA_POINTERS) {
621 frame->extended_data = av_mallocz_array(planes, sizeof(*frame->extended_data));
622 frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
623 frame->extended_buf = av_mallocz_array(frame->nb_extended_buf,
624 sizeof(*frame->extended_buf));
625 if (!frame->extended_data || !frame->extended_buf) {
626 av_freep(&frame->extended_data);
627 av_freep(&frame->extended_buf);
628 return AVERROR(ENOMEM);
629 }
630 } else {
631 frame->extended_data = frame->data;
632 av_assert0(frame->nb_extended_buf == 0);
633 }
634
635 for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
636 frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
637 if (!frame->buf[i])
638 goto fail;
639 frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
640 }
641 for (i = 0; i < frame->nb_extended_buf; i++) {
642 frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
643 if (!frame->extended_buf[i])
644 goto fail;
645 frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
646 }
647
648 if (avctx->debug & FF_DEBUG_BUFFERS)
649 av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
650
651 return 0;
652 fail:
653 av_frame_unref(frame);
654 return AVERROR(ENOMEM);
655 }
656
video_get_buffer(AVCodecContext * s,AVFrame * pic)657 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
658 {
659 FramePool *pool = s->internal->pool;
660 int i;
661
662 if (pic->data[0]) {
663 av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
664 return -1;
665 }
666
667 memset(pic->data, 0, sizeof(pic->data));
668 pic->extended_data = pic->data;
669
670 for (i = 0; i < 4 && pool->pools[i]; i++) {
671 pic->linesize[i] = pool->linesize[i];
672
673 pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
674 if (!pic->buf[i])
675 goto fail;
676
677 pic->data[i] = pic->buf[i]->data;
678 }
679 for (; i < AV_NUM_DATA_POINTERS; i++) {
680 pic->data[i] = NULL;
681 pic->linesize[i] = 0;
682 }
683 if (pic->data[1] && !pic->data[2])
684 avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
685
686 if (s->debug & FF_DEBUG_BUFFERS)
687 av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
688
689 return 0;
690 fail:
691 av_frame_unref(pic);
692 return AVERROR(ENOMEM);
693 }
694
avpriv_color_frame(AVFrame * frame,const int c[4])695 void avpriv_color_frame(AVFrame *frame, const int c[4])
696 {
697 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
698 int p, y, x;
699
700 av_assert0(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
701
702 for (p = 0; p<desc->nb_components; p++) {
703 uint8_t *dst = frame->data[p];
704 int is_chroma = p == 1 || p == 2;
705 int bytes = is_chroma ? FF_CEIL_RSHIFT(frame->width, desc->log2_chroma_w) : frame->width;
706 int height = is_chroma ? FF_CEIL_RSHIFT(frame->height, desc->log2_chroma_h) : frame->height;
707 for (y = 0; y < height; y++) {
708 if (desc->comp[0].depth_minus1 >= 8) {
709 for (x = 0; x<bytes; x++)
710 ((uint16_t*)dst)[x] = c[p];
711 }else
712 memset(dst, c[p], bytes);
713 dst += frame->linesize[p];
714 }
715 }
716 }
717
avcodec_default_get_buffer2(AVCodecContext * avctx,AVFrame * frame,int flags)718 int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
719 {
720 int ret;
721
722 if ((ret = update_frame_pool(avctx, frame)) < 0)
723 return ret;
724
725 #if FF_API_GET_BUFFER
726 FF_DISABLE_DEPRECATION_WARNINGS
727 frame->type = FF_BUFFER_TYPE_INTERNAL;
728 FF_ENABLE_DEPRECATION_WARNINGS
729 #endif
730
731 switch (avctx->codec_type) {
732 case AVMEDIA_TYPE_VIDEO:
733 return video_get_buffer(avctx, frame);
734 case AVMEDIA_TYPE_AUDIO:
735 return audio_get_buffer(avctx, frame);
736 default:
737 return -1;
738 }
739 }
740
ff_init_buffer_info(AVCodecContext * avctx,AVFrame * frame)741 int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
742 {
743 AVPacket *pkt = avctx->internal->pkt;
744
745 if (pkt) {
746 uint8_t *packet_sd;
747 AVFrameSideData *frame_sd;
748 int size;
749 frame->pkt_pts = pkt->pts;
750 av_frame_set_pkt_pos (frame, pkt->pos);
751 av_frame_set_pkt_duration(frame, pkt->duration);
752 av_frame_set_pkt_size (frame, pkt->size);
753
754 /* copy the replaygain data to the output frame */
755 packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_REPLAYGAIN, &size);
756 if (packet_sd) {
757 frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_REPLAYGAIN, size);
758 if (!frame_sd)
759 return AVERROR(ENOMEM);
760
761 memcpy(frame_sd->data, packet_sd, size);
762 }
763
764 /* copy the displaymatrix to the output frame */
765 packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_DISPLAYMATRIX, &size);
766 if (packet_sd) {
767 frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX, size);
768 if (!frame_sd)
769 return AVERROR(ENOMEM);
770
771 memcpy(frame_sd->data, packet_sd, size);
772 }
773
774 /* copy the stereo3d format to the output frame */
775 packet_sd = av_packet_get_side_data(pkt, AV_PKT_DATA_STEREO3D, &size);
776 if (packet_sd) {
777 frame_sd = av_frame_new_side_data(frame, AV_FRAME_DATA_STEREO3D, size);
778 if (!frame_sd)
779 return AVERROR(ENOMEM);
780
781 memcpy(frame_sd->data, packet_sd, size);
782 }
783 } else {
784 frame->pkt_pts = AV_NOPTS_VALUE;
785 av_frame_set_pkt_pos (frame, -1);
786 av_frame_set_pkt_duration(frame, 0);
787 av_frame_set_pkt_size (frame, -1);
788 }
789 frame->reordered_opaque = avctx->reordered_opaque;
790
791 if (frame->color_primaries == AVCOL_PRI_UNSPECIFIED)
792 frame->color_primaries = avctx->color_primaries;
793 if (frame->color_trc == AVCOL_TRC_UNSPECIFIED)
794 frame->color_trc = avctx->color_trc;
795 if (av_frame_get_colorspace(frame) == AVCOL_SPC_UNSPECIFIED)
796 av_frame_set_colorspace(frame, avctx->colorspace);
797 if (av_frame_get_color_range(frame) == AVCOL_RANGE_UNSPECIFIED)
798 av_frame_set_color_range(frame, avctx->color_range);
799 if (frame->chroma_location == AVCHROMA_LOC_UNSPECIFIED)
800 frame->chroma_location = avctx->chroma_sample_location;
801
802 switch (avctx->codec->type) {
803 case AVMEDIA_TYPE_VIDEO:
804 frame->format = avctx->pix_fmt;
805 if (!frame->sample_aspect_ratio.num)
806 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
807
808 if (frame->width && frame->height &&
809 av_image_check_sar(frame->width, frame->height,
810 frame->sample_aspect_ratio) < 0) {
811 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
812 frame->sample_aspect_ratio.num,
813 frame->sample_aspect_ratio.den);
814 frame->sample_aspect_ratio = (AVRational){ 0, 1 };
815 }
816
817 break;
818 case AVMEDIA_TYPE_AUDIO:
819 if (!frame->sample_rate)
820 frame->sample_rate = avctx->sample_rate;
821 if (frame->format < 0)
822 frame->format = avctx->sample_fmt;
823 if (!frame->channel_layout) {
824 if (avctx->channel_layout) {
825 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
826 avctx->channels) {
827 av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
828 "configuration.\n");
829 return AVERROR(EINVAL);
830 }
831
832 frame->channel_layout = avctx->channel_layout;
833 } else {
834 if (avctx->channels > FF_SANE_NB_CHANNELS) {
835 av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
836 avctx->channels);
837 return AVERROR(ENOSYS);
838 }
839 }
840 }
841 av_frame_set_channels(frame, avctx->channels);
842 break;
843 }
844 return 0;
845 }
846
847 #if FF_API_GET_BUFFER
848 FF_DISABLE_DEPRECATION_WARNINGS
avcodec_default_get_buffer(AVCodecContext * avctx,AVFrame * frame)849 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
850 {
851 return avcodec_default_get_buffer2(avctx, frame, 0);
852 }
853
854 typedef struct CompatReleaseBufPriv {
855 AVCodecContext avctx;
856 AVFrame frame;
857 uint8_t avframe_padding[1024]; // hack to allow linking to a avutil with larger AVFrame
858 } CompatReleaseBufPriv;
859
compat_free_buffer(void * opaque,uint8_t * data)860 static void compat_free_buffer(void *opaque, uint8_t *data)
861 {
862 CompatReleaseBufPriv *priv = opaque;
863 if (priv->avctx.release_buffer)
864 priv->avctx.release_buffer(&priv->avctx, &priv->frame);
865 av_freep(&priv);
866 }
867
compat_release_buffer(void * opaque,uint8_t * data)868 static void compat_release_buffer(void *opaque, uint8_t *data)
869 {
870 AVBufferRef *buf = opaque;
871 av_buffer_unref(&buf);
872 }
873 FF_ENABLE_DEPRECATION_WARNINGS
874 #endif
875
ff_decode_frame_props(AVCodecContext * avctx,AVFrame * frame)876 int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
877 {
878 return ff_init_buffer_info(avctx, frame);
879 }
880
get_buffer_internal(AVCodecContext * avctx,AVFrame * frame,int flags)881 static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
882 {
883 const AVHWAccel *hwaccel = avctx->hwaccel;
884 int override_dimensions = 1;
885 int ret;
886
887 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
888 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
889 av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
890 return AVERROR(EINVAL);
891 }
892 }
893 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
894 if (frame->width <= 0 || frame->height <= 0) {
895 frame->width = FFMAX(avctx->width, FF_CEIL_RSHIFT(avctx->coded_width, avctx->lowres));
896 frame->height = FFMAX(avctx->height, FF_CEIL_RSHIFT(avctx->coded_height, avctx->lowres));
897 override_dimensions = 0;
898 }
899 }
900 ret = ff_decode_frame_props(avctx, frame);
901 if (ret < 0)
902 return ret;
903 if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
904 return ret;
905
906 if (hwaccel && hwaccel->alloc_frame) {
907 ret = hwaccel->alloc_frame(avctx, frame);
908 goto end;
909 }
910
911 #if FF_API_GET_BUFFER
912 FF_DISABLE_DEPRECATION_WARNINGS
913 /*
914 * Wrap an old get_buffer()-allocated buffer in a bunch of AVBuffers.
915 * We wrap each plane in its own AVBuffer. Each of those has a reference to
916 * a dummy AVBuffer as its private data, unreffing it on free.
917 * When all the planes are freed, the dummy buffer's free callback calls
918 * release_buffer().
919 */
920 if (avctx->get_buffer) {
921 CompatReleaseBufPriv *priv = NULL;
922 AVBufferRef *dummy_buf = NULL;
923 int planes, i, ret;
924
925 if (flags & AV_GET_BUFFER_FLAG_REF)
926 frame->reference = 1;
927
928 ret = avctx->get_buffer(avctx, frame);
929 if (ret < 0)
930 return ret;
931
932 /* return if the buffers are already set up
933 * this would happen e.g. when a custom get_buffer() calls
934 * avcodec_default_get_buffer
935 */
936 if (frame->buf[0])
937 goto end0;
938
939 priv = av_mallocz(sizeof(*priv));
940 if (!priv) {
941 ret = AVERROR(ENOMEM);
942 goto fail;
943 }
944 priv->avctx = *avctx;
945 priv->frame = *frame;
946
947 dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
948 if (!dummy_buf) {
949 ret = AVERROR(ENOMEM);
950 goto fail;
951 }
952
953 #define WRAP_PLANE(ref_out, data, data_size) \
954 do { \
955 AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
956 if (!dummy_ref) { \
957 ret = AVERROR(ENOMEM); \
958 goto fail; \
959 } \
960 ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
961 dummy_ref, 0); \
962 if (!ref_out) { \
963 av_frame_unref(frame); \
964 ret = AVERROR(ENOMEM); \
965 goto fail; \
966 } \
967 } while (0)
968
969 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
970 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
971
972 planes = av_pix_fmt_count_planes(frame->format);
973 /* workaround for AVHWAccel plane count of 0, buf[0] is used as
974 check for allocated buffers: make libavcodec happy */
975 if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
976 planes = 1;
977 if (!desc || planes <= 0) {
978 ret = AVERROR(EINVAL);
979 goto fail;
980 }
981
982 for (i = 0; i < planes; i++) {
983 int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
984 int plane_size = (frame->height >> v_shift) * frame->linesize[i];
985
986 WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
987 }
988 } else {
989 int planar = av_sample_fmt_is_planar(frame->format);
990 planes = planar ? avctx->channels : 1;
991
992 if (planes > FF_ARRAY_ELEMS(frame->buf)) {
993 frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
994 frame->extended_buf = av_malloc_array(sizeof(*frame->extended_buf),
995 frame->nb_extended_buf);
996 if (!frame->extended_buf) {
997 ret = AVERROR(ENOMEM);
998 goto fail;
999 }
1000 }
1001
1002 for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
1003 WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
1004
1005 for (i = 0; i < frame->nb_extended_buf; i++)
1006 WRAP_PLANE(frame->extended_buf[i],
1007 frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
1008 frame->linesize[0]);
1009 }
1010
1011 av_buffer_unref(&dummy_buf);
1012
1013 end0:
1014 frame->width = avctx->width;
1015 frame->height = avctx->height;
1016
1017 return 0;
1018
1019 fail:
1020 avctx->release_buffer(avctx, frame);
1021 av_freep(&priv);
1022 av_buffer_unref(&dummy_buf);
1023 return ret;
1024 }
1025 FF_ENABLE_DEPRECATION_WARNINGS
1026 #endif
1027
1028 ret = avctx->get_buffer2(avctx, frame, flags);
1029
1030 end:
1031 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
1032 frame->width = avctx->width;
1033 frame->height = avctx->height;
1034 }
1035
1036 return ret;
1037 }
1038
ff_get_buffer(AVCodecContext * avctx,AVFrame * frame,int flags)1039 int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
1040 {
1041 int ret = get_buffer_internal(avctx, frame, flags);
1042 if (ret < 0)
1043 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1044 return ret;
1045 }
1046
reget_buffer_internal(AVCodecContext * avctx,AVFrame * frame)1047 static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
1048 {
1049 AVFrame *tmp;
1050 int ret;
1051
1052 av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
1053
1054 if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
1055 av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
1056 frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
1057 av_frame_unref(frame);
1058 }
1059
1060 ff_init_buffer_info(avctx, frame);
1061
1062 if (!frame->data[0])
1063 return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1064
1065 if (av_frame_is_writable(frame))
1066 return ff_decode_frame_props(avctx, frame);
1067
1068 tmp = av_frame_alloc();
1069 if (!tmp)
1070 return AVERROR(ENOMEM);
1071
1072 av_frame_move_ref(tmp, frame);
1073
1074 ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
1075 if (ret < 0) {
1076 av_frame_free(&tmp);
1077 return ret;
1078 }
1079
1080 av_frame_copy(frame, tmp);
1081 av_frame_free(&tmp);
1082
1083 return 0;
1084 }
1085
ff_reget_buffer(AVCodecContext * avctx,AVFrame * frame)1086 int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
1087 {
1088 int ret = reget_buffer_internal(avctx, frame);
1089 if (ret < 0)
1090 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
1091 return ret;
1092 }
1093
1094 #if FF_API_GET_BUFFER
avcodec_default_release_buffer(AVCodecContext * s,AVFrame * pic)1095 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
1096 {
1097 av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
1098
1099 av_frame_unref(pic);
1100 }
1101
avcodec_default_reget_buffer(AVCodecContext * s,AVFrame * pic)1102 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
1103 {
1104 av_assert0(0);
1105 return AVERROR_BUG;
1106 }
1107 #endif
1108
avcodec_default_execute(AVCodecContext * c,int (* func)(AVCodecContext * c2,void * arg2),void * arg,int * ret,int count,int size)1109 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
1110 {
1111 int i;
1112
1113 for (i = 0; i < count; i++) {
1114 int r = func(c, (char *)arg + i * size);
1115 if (ret)
1116 ret[i] = r;
1117 }
1118 return 0;
1119 }
1120
avcodec_default_execute2(AVCodecContext * c,int (* func)(AVCodecContext * c2,void * arg2,int jobnr,int threadnr),void * arg,int * ret,int count)1121 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
1122 {
1123 int i;
1124
1125 for (i = 0; i < count; i++) {
1126 int r = func(c, arg, i, 0);
1127 if (ret)
1128 ret[i] = r;
1129 }
1130 return 0;
1131 }
1132
avpriv_find_pix_fmt(const PixelFormatTag * tags,unsigned int fourcc)1133 enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
1134 unsigned int fourcc)
1135 {
1136 while (tags->pix_fmt >= 0) {
1137 if (tags->fourcc == fourcc)
1138 return tags->pix_fmt;
1139 tags++;
1140 }
1141 return AV_PIX_FMT_NONE;
1142 }
1143
is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)1144 static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
1145 {
1146 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
1147 return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
1148 }
1149
avcodec_default_get_format(struct AVCodecContext * s,const enum AVPixelFormat * fmt)1150 enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
1151 {
1152 while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
1153 ++fmt;
1154 return fmt[0];
1155 }
1156
find_hwaccel(enum AVCodecID codec_id,enum AVPixelFormat pix_fmt)1157 static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
1158 enum AVPixelFormat pix_fmt)
1159 {
1160 AVHWAccel *hwaccel = NULL;
1161
1162 while ((hwaccel = av_hwaccel_next(hwaccel)))
1163 if (hwaccel->id == codec_id
1164 && hwaccel->pix_fmt == pix_fmt)
1165 return hwaccel;
1166 return NULL;
1167 }
1168
1169
ff_get_format(AVCodecContext * avctx,const enum AVPixelFormat * fmt)1170 int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
1171 {
1172 const AVPixFmtDescriptor *desc;
1173 enum AVPixelFormat ret = avctx->get_format(avctx, fmt);
1174
1175 desc = av_pix_fmt_desc_get(ret);
1176 if (!desc)
1177 return AV_PIX_FMT_NONE;
1178
1179 if (avctx->hwaccel && avctx->hwaccel->uninit)
1180 avctx->hwaccel->uninit(avctx);
1181 av_freep(&avctx->internal->hwaccel_priv_data);
1182 avctx->hwaccel = NULL;
1183
1184 if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL &&
1185 !(avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)) {
1186 AVHWAccel *hwaccel;
1187 int err;
1188
1189 hwaccel = find_hwaccel(avctx->codec_id, ret);
1190 if (!hwaccel) {
1191 av_log(avctx, AV_LOG_ERROR,
1192 "Could not find an AVHWAccel for the pixel format: %s",
1193 desc->name);
1194 return AV_PIX_FMT_NONE;
1195 }
1196
1197 if (hwaccel->priv_data_size) {
1198 avctx->internal->hwaccel_priv_data = av_mallocz(hwaccel->priv_data_size);
1199 if (!avctx->internal->hwaccel_priv_data)
1200 return AV_PIX_FMT_NONE;
1201 }
1202
1203 if (hwaccel->init) {
1204 err = hwaccel->init(avctx);
1205 if (err < 0) {
1206 av_freep(&avctx->internal->hwaccel_priv_data);
1207 return AV_PIX_FMT_NONE;
1208 }
1209 }
1210 avctx->hwaccel = hwaccel;
1211 }
1212
1213 return ret;
1214 }
1215
1216 #if FF_API_AVFRAME_LAVC
avcodec_get_frame_defaults(AVFrame * frame)1217 void avcodec_get_frame_defaults(AVFrame *frame)
1218 {
1219 #if LIBAVCODEC_VERSION_MAJOR >= 55
1220 // extended_data should explicitly be freed when needed, this code is unsafe currently
1221 // also this is not compatible to the <55 ABI/API
1222 if (frame->extended_data != frame->data && 0)
1223 av_freep(&frame->extended_data);
1224 #endif
1225
1226 memset(frame, 0, sizeof(AVFrame));
1227 av_frame_unref(frame);
1228 }
1229
avcodec_alloc_frame(void)1230 AVFrame *avcodec_alloc_frame(void)
1231 {
1232 return av_frame_alloc();
1233 }
1234
avcodec_free_frame(AVFrame ** frame)1235 void avcodec_free_frame(AVFrame **frame)
1236 {
1237 av_frame_free(frame);
1238 }
1239 #endif
1240
MAKE_ACCESSORS(AVCodecContext,codec,AVRational,pkt_timebase)1241 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
1242 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
1243 MAKE_ACCESSORS(AVCodecContext, codec, int, lowres)
1244 MAKE_ACCESSORS(AVCodecContext, codec, int, seek_preroll)
1245 MAKE_ACCESSORS(AVCodecContext, codec, uint16_t*, chroma_intra_matrix)
1246
1247 int av_codec_get_max_lowres(const AVCodec *codec)
1248 {
1249 return codec->max_lowres;
1250 }
1251
avcodec_get_subtitle_defaults(AVSubtitle * sub)1252 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
1253 {
1254 memset(sub, 0, sizeof(*sub));
1255 sub->pts = AV_NOPTS_VALUE;
1256 }
1257
get_bit_rate(AVCodecContext * ctx)1258 static int get_bit_rate(AVCodecContext *ctx)
1259 {
1260 int bit_rate;
1261 int bits_per_sample;
1262
1263 switch (ctx->codec_type) {
1264 case AVMEDIA_TYPE_VIDEO:
1265 case AVMEDIA_TYPE_DATA:
1266 case AVMEDIA_TYPE_SUBTITLE:
1267 case AVMEDIA_TYPE_ATTACHMENT:
1268 bit_rate = ctx->bit_rate;
1269 break;
1270 case AVMEDIA_TYPE_AUDIO:
1271 bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1272 bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1273 break;
1274 default:
1275 bit_rate = 0;
1276 break;
1277 }
1278 return bit_rate;
1279 }
1280
ff_codec_open2_recursive(AVCodecContext * avctx,const AVCodec * codec,AVDictionary ** options)1281 int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1282 {
1283 int ret = 0;
1284
1285 ff_unlock_avcodec();
1286
1287 ret = avcodec_open2(avctx, codec, options);
1288
1289 ff_lock_avcodec(avctx);
1290 return ret;
1291 }
1292
avcodec_open2(AVCodecContext * avctx,const AVCodec * codec,AVDictionary ** options)1293 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
1294 {
1295 int ret = 0;
1296 AVDictionary *tmp = NULL;
1297
1298 if (avcodec_is_open(avctx))
1299 return 0;
1300
1301 if ((!codec && !avctx->codec)) {
1302 av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
1303 return AVERROR(EINVAL);
1304 }
1305 if ((codec && avctx->codec && codec != avctx->codec)) {
1306 av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
1307 "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
1308 return AVERROR(EINVAL);
1309 }
1310 if (!codec)
1311 codec = avctx->codec;
1312
1313 if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
1314 return AVERROR(EINVAL);
1315
1316 if (options)
1317 av_dict_copy(&tmp, *options, 0);
1318
1319 ret = ff_lock_avcodec(avctx);
1320 if (ret < 0)
1321 return ret;
1322
1323 avctx->internal = av_mallocz(sizeof(AVCodecInternal));
1324 if (!avctx->internal) {
1325 ret = AVERROR(ENOMEM);
1326 goto end;
1327 }
1328
1329 avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
1330 if (!avctx->internal->pool) {
1331 ret = AVERROR(ENOMEM);
1332 goto free_and_end;
1333 }
1334
1335 avctx->internal->to_free = av_frame_alloc();
1336 if (!avctx->internal->to_free) {
1337 ret = AVERROR(ENOMEM);
1338 goto free_and_end;
1339 }
1340
1341 if (codec->priv_data_size > 0) {
1342 if (!avctx->priv_data) {
1343 avctx->priv_data = av_mallocz(codec->priv_data_size);
1344 if (!avctx->priv_data) {
1345 ret = AVERROR(ENOMEM);
1346 goto end;
1347 }
1348 if (codec->priv_class) {
1349 *(const AVClass **)avctx->priv_data = codec->priv_class;
1350 av_opt_set_defaults(avctx->priv_data);
1351 }
1352 }
1353 if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
1354 goto free_and_end;
1355 } else {
1356 avctx->priv_data = NULL;
1357 }
1358 if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
1359 goto free_and_end;
1360
1361 // only call ff_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
1362 if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
1363 (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
1364 if (avctx->coded_width && avctx->coded_height)
1365 ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
1366 else if (avctx->width && avctx->height)
1367 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
1368 if (ret < 0)
1369 goto free_and_end;
1370 }
1371
1372 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1373 && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1374 || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1375 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
1376 ff_set_dimensions(avctx, 0, 0);
1377 }
1378
1379 if (avctx->width > 0 && avctx->height > 0) {
1380 if (av_image_check_sar(avctx->width, avctx->height,
1381 avctx->sample_aspect_ratio) < 0) {
1382 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1383 avctx->sample_aspect_ratio.num,
1384 avctx->sample_aspect_ratio.den);
1385 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
1386 }
1387 }
1388
1389 /* if the decoder init function was already called previously,
1390 * free the already allocated subtitle_header before overwriting it */
1391 if (av_codec_is_decoder(codec))
1392 av_freep(&avctx->subtitle_header);
1393
1394 if (avctx->channels > FF_SANE_NB_CHANNELS) {
1395 ret = AVERROR(EINVAL);
1396 goto free_and_end;
1397 }
1398
1399 avctx->codec = codec;
1400 if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
1401 avctx->codec_id == AV_CODEC_ID_NONE) {
1402 avctx->codec_type = codec->type;
1403 avctx->codec_id = codec->id;
1404 }
1405 if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
1406 && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
1407 av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
1408 ret = AVERROR(EINVAL);
1409 goto free_and_end;
1410 }
1411 avctx->frame_number = 0;
1412 avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
1413
1414 if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1415 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1416 const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
1417 AVCodec *codec2;
1418 av_log(avctx, AV_LOG_ERROR,
1419 "The %s '%s' is experimental but experimental codecs are not enabled, "
1420 "add '-strict %d' if you want to use it.\n",
1421 codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
1422 codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
1423 if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
1424 av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
1425 codec_string, codec2->name);
1426 ret = AVERROR_EXPERIMENTAL;
1427 goto free_and_end;
1428 }
1429
1430 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
1431 (!avctx->time_base.num || !avctx->time_base.den)) {
1432 avctx->time_base.num = 1;
1433 avctx->time_base.den = avctx->sample_rate;
1434 }
1435
1436 if (!HAVE_THREADS)
1437 av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
1438
1439 if (CONFIG_FRAME_THREAD_ENCODER) {
1440 ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
1441 ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
1442 ff_lock_avcodec(avctx);
1443 if (ret < 0)
1444 goto free_and_end;
1445 }
1446
1447 if (HAVE_THREADS
1448 && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
1449 ret = ff_thread_init(avctx);
1450 if (ret < 0) {
1451 goto free_and_end;
1452 }
1453 }
1454 if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
1455 avctx->thread_count = 1;
1456
1457 if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
1458 av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
1459 avctx->codec->max_lowres);
1460 ret = AVERROR(EINVAL);
1461 goto free_and_end;
1462 }
1463
1464 #if FF_API_VISMV
1465 if (avctx->debug_mv)
1466 av_log(avctx, AV_LOG_WARNING, "The 'vismv' option is deprecated, "
1467 "see the codecview filter instead.\n");
1468 #endif
1469
1470 if (av_codec_is_encoder(avctx->codec)) {
1471 int i;
1472 if (avctx->codec->sample_fmts) {
1473 for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
1474 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
1475 break;
1476 if (avctx->channels == 1 &&
1477 av_get_planar_sample_fmt(avctx->sample_fmt) ==
1478 av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
1479 avctx->sample_fmt = avctx->codec->sample_fmts[i];
1480 break;
1481 }
1482 }
1483 if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
1484 char buf[128];
1485 snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
1486 av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
1487 (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
1488 ret = AVERROR(EINVAL);
1489 goto free_and_end;
1490 }
1491 }
1492 if (avctx->codec->pix_fmts) {
1493 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
1494 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
1495 break;
1496 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
1497 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
1498 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
1499 char buf[128];
1500 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
1501 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
1502 (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
1503 ret = AVERROR(EINVAL);
1504 goto free_and_end;
1505 }
1506 }
1507 if (avctx->codec->supported_samplerates) {
1508 for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
1509 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
1510 break;
1511 if (avctx->codec->supported_samplerates[i] == 0) {
1512 av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
1513 avctx->sample_rate);
1514 ret = AVERROR(EINVAL);
1515 goto free_and_end;
1516 }
1517 }
1518 if (avctx->codec->channel_layouts) {
1519 if (!avctx->channel_layout) {
1520 av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
1521 } else {
1522 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
1523 if (avctx->channel_layout == avctx->codec->channel_layouts[i])
1524 break;
1525 if (avctx->codec->channel_layouts[i] == 0) {
1526 char buf[512];
1527 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1528 av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
1529 ret = AVERROR(EINVAL);
1530 goto free_and_end;
1531 }
1532 }
1533 }
1534 if (avctx->channel_layout && avctx->channels) {
1535 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1536 if (channels != avctx->channels) {
1537 char buf[512];
1538 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1539 av_log(avctx, AV_LOG_ERROR,
1540 "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
1541 buf, channels, avctx->channels);
1542 ret = AVERROR(EINVAL);
1543 goto free_and_end;
1544 }
1545 } else if (avctx->channel_layout) {
1546 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1547 }
1548 if(avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
1549 if (avctx->width <= 0 || avctx->height <= 0) {
1550 av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
1551 ret = AVERROR(EINVAL);
1552 goto free_and_end;
1553 }
1554 }
1555 if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
1556 && avctx->bit_rate>0 && avctx->bit_rate<1000) {
1557 av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
1558 }
1559
1560 if (!avctx->rc_initial_buffer_occupancy)
1561 avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
1562 }
1563
1564 avctx->pts_correction_num_faulty_pts =
1565 avctx->pts_correction_num_faulty_dts = 0;
1566 avctx->pts_correction_last_pts =
1567 avctx->pts_correction_last_dts = INT64_MIN;
1568
1569 if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
1570 || avctx->internal->frame_thread_encoder)) {
1571 ret = avctx->codec->init(avctx);
1572 if (ret < 0) {
1573 goto free_and_end;
1574 }
1575 }
1576
1577 ret=0;
1578
1579 if (av_codec_is_decoder(avctx->codec)) {
1580 if (!avctx->bit_rate)
1581 avctx->bit_rate = get_bit_rate(avctx);
1582 /* validate channel layout from the decoder */
1583 if (avctx->channel_layout) {
1584 int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
1585 if (!avctx->channels)
1586 avctx->channels = channels;
1587 else if (channels != avctx->channels) {
1588 char buf[512];
1589 av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
1590 av_log(avctx, AV_LOG_WARNING,
1591 "Channel layout '%s' with %d channels does not match specified number of channels %d: "
1592 "ignoring specified channel layout\n",
1593 buf, channels, avctx->channels);
1594 avctx->channel_layout = 0;
1595 }
1596 }
1597 if (avctx->channels && avctx->channels < 0 ||
1598 avctx->channels > FF_SANE_NB_CHANNELS) {
1599 ret = AVERROR(EINVAL);
1600 goto free_and_end;
1601 }
1602 if (avctx->sub_charenc) {
1603 if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1604 av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
1605 "supported with subtitles codecs\n");
1606 ret = AVERROR(EINVAL);
1607 goto free_and_end;
1608 } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
1609 av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
1610 "subtitles character encoding will be ignored\n",
1611 avctx->codec_descriptor->name);
1612 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
1613 } else {
1614 /* input character encoding is set for a text based subtitle
1615 * codec at this point */
1616 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
1617 avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
1618
1619 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
1620 #if CONFIG_ICONV
1621 iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
1622 if (cd == (iconv_t)-1) {
1623 av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
1624 "with input character encoding \"%s\"\n", avctx->sub_charenc);
1625 ret = AVERROR(errno);
1626 goto free_and_end;
1627 }
1628 iconv_close(cd);
1629 #else
1630 av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
1631 "conversion needs a libavcodec built with iconv support "
1632 "for this codec\n");
1633 ret = AVERROR(ENOSYS);
1634 goto free_and_end;
1635 #endif
1636 }
1637 }
1638 }
1639 }
1640 end:
1641 ff_unlock_avcodec();
1642 if (options) {
1643 av_dict_free(options);
1644 *options = tmp;
1645 }
1646
1647 return ret;
1648 free_and_end:
1649 av_dict_free(&tmp);
1650 av_freep(&avctx->priv_data);
1651 if (avctx->internal) {
1652 av_frame_free(&avctx->internal->to_free);
1653 av_freep(&avctx->internal->pool);
1654 }
1655 av_freep(&avctx->internal);
1656 avctx->codec = NULL;
1657 goto end;
1658 }
1659
ff_alloc_packet2(AVCodecContext * avctx,AVPacket * avpkt,int64_t size)1660 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
1661 {
1662 if (avpkt->size < 0) {
1663 av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
1664 return AVERROR(EINVAL);
1665 }
1666 if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1667 av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
1668 size, INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
1669 return AVERROR(EINVAL);
1670 }
1671
1672 if (avctx) {
1673 av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1674 if (!avpkt->data || avpkt->size < size) {
1675 av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
1676 avpkt->data = avctx->internal->byte_buffer;
1677 avpkt->size = avctx->internal->byte_buffer_size;
1678 #if FF_API_DESTRUCT_PACKET
1679 FF_DISABLE_DEPRECATION_WARNINGS
1680 avpkt->destruct = NULL;
1681 FF_ENABLE_DEPRECATION_WARNINGS
1682 #endif
1683 }
1684 }
1685
1686 if (avpkt->data) {
1687 AVBufferRef *buf = avpkt->buf;
1688 #if FF_API_DESTRUCT_PACKET
1689 FF_DISABLE_DEPRECATION_WARNINGS
1690 void *destruct = avpkt->destruct;
1691 FF_ENABLE_DEPRECATION_WARNINGS
1692 #endif
1693
1694 if (avpkt->size < size) {
1695 av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
1696 return AVERROR(EINVAL);
1697 }
1698
1699 av_init_packet(avpkt);
1700 #if FF_API_DESTRUCT_PACKET
1701 FF_DISABLE_DEPRECATION_WARNINGS
1702 avpkt->destruct = destruct;
1703 FF_ENABLE_DEPRECATION_WARNINGS
1704 #endif
1705 avpkt->buf = buf;
1706 avpkt->size = size;
1707 return 0;
1708 } else {
1709 int ret = av_new_packet(avpkt, size);
1710 if (ret < 0)
1711 av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
1712 return ret;
1713 }
1714 }
1715
ff_alloc_packet(AVPacket * avpkt,int size)1716 int ff_alloc_packet(AVPacket *avpkt, int size)
1717 {
1718 return ff_alloc_packet2(NULL, avpkt, size);
1719 }
1720
1721 /**
1722 * Pad last frame with silence.
1723 */
pad_last_frame(AVCodecContext * s,AVFrame ** dst,const AVFrame * src)1724 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1725 {
1726 AVFrame *frame = NULL;
1727 int ret;
1728
1729 if (!(frame = av_frame_alloc()))
1730 return AVERROR(ENOMEM);
1731
1732 frame->format = src->format;
1733 frame->channel_layout = src->channel_layout;
1734 av_frame_set_channels(frame, av_frame_get_channels(src));
1735 frame->nb_samples = s->frame_size;
1736 ret = av_frame_get_buffer(frame, 32);
1737 if (ret < 0)
1738 goto fail;
1739
1740 ret = av_frame_copy_props(frame, src);
1741 if (ret < 0)
1742 goto fail;
1743
1744 if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1745 src->nb_samples, s->channels, s->sample_fmt)) < 0)
1746 goto fail;
1747 if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1748 frame->nb_samples - src->nb_samples,
1749 s->channels, s->sample_fmt)) < 0)
1750 goto fail;
1751
1752 *dst = frame;
1753
1754 return 0;
1755
1756 fail:
1757 av_frame_free(&frame);
1758 return ret;
1759 }
1760
avcodec_encode_audio2(AVCodecContext * avctx,AVPacket * avpkt,const AVFrame * frame,int * got_packet_ptr)1761 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1762 AVPacket *avpkt,
1763 const AVFrame *frame,
1764 int *got_packet_ptr)
1765 {
1766 AVFrame *extended_frame = NULL;
1767 AVFrame *padded_frame = NULL;
1768 int ret;
1769 AVPacket user_pkt = *avpkt;
1770 int needs_realloc = !user_pkt.data;
1771
1772 *got_packet_ptr = 0;
1773
1774 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1775 av_free_packet(avpkt);
1776 av_init_packet(avpkt);
1777 return 0;
1778 }
1779
1780 /* ensure that extended_data is properly set */
1781 if (frame && !frame->extended_data) {
1782 if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1783 avctx->channels > AV_NUM_DATA_POINTERS) {
1784 av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1785 "with more than %d channels, but extended_data is not set.\n",
1786 AV_NUM_DATA_POINTERS);
1787 return AVERROR(EINVAL);
1788 }
1789 av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1790
1791 extended_frame = av_frame_alloc();
1792 if (!extended_frame)
1793 return AVERROR(ENOMEM);
1794
1795 memcpy(extended_frame, frame, sizeof(AVFrame));
1796 extended_frame->extended_data = extended_frame->data;
1797 frame = extended_frame;
1798 }
1799
1800 /* check for valid frame size */
1801 if (frame) {
1802 if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1803 if (frame->nb_samples > avctx->frame_size) {
1804 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1805 ret = AVERROR(EINVAL);
1806 goto end;
1807 }
1808 } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1809 if (frame->nb_samples < avctx->frame_size &&
1810 !avctx->internal->last_audio_frame) {
1811 ret = pad_last_frame(avctx, &padded_frame, frame);
1812 if (ret < 0)
1813 goto end;
1814
1815 frame = padded_frame;
1816 avctx->internal->last_audio_frame = 1;
1817 }
1818
1819 if (frame->nb_samples != avctx->frame_size) {
1820 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1821 ret = AVERROR(EINVAL);
1822 goto end;
1823 }
1824 }
1825 }
1826
1827 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1828 if (!ret) {
1829 if (*got_packet_ptr) {
1830 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1831 if (avpkt->pts == AV_NOPTS_VALUE)
1832 avpkt->pts = frame->pts;
1833 if (!avpkt->duration)
1834 avpkt->duration = ff_samples_to_time_base(avctx,
1835 frame->nb_samples);
1836 }
1837 avpkt->dts = avpkt->pts;
1838 } else {
1839 avpkt->size = 0;
1840 }
1841 }
1842 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1843 needs_realloc = 0;
1844 if (user_pkt.data) {
1845 if (user_pkt.size >= avpkt->size) {
1846 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1847 } else {
1848 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1849 avpkt->size = user_pkt.size;
1850 ret = -1;
1851 }
1852 avpkt->buf = user_pkt.buf;
1853 avpkt->data = user_pkt.data;
1854 #if FF_API_DESTRUCT_PACKET
1855 FF_DISABLE_DEPRECATION_WARNINGS
1856 avpkt->destruct = user_pkt.destruct;
1857 FF_ENABLE_DEPRECATION_WARNINGS
1858 #endif
1859 } else {
1860 if (av_dup_packet(avpkt) < 0) {
1861 ret = AVERROR(ENOMEM);
1862 }
1863 }
1864 }
1865
1866 if (!ret) {
1867 if (needs_realloc && avpkt->data) {
1868 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1869 if (ret >= 0)
1870 avpkt->data = avpkt->buf->data;
1871 }
1872
1873 avctx->frame_number++;
1874 }
1875
1876 if (ret < 0 || !*got_packet_ptr) {
1877 av_free_packet(avpkt);
1878 av_init_packet(avpkt);
1879 goto end;
1880 }
1881
1882 /* NOTE: if we add any audio encoders which output non-keyframe packets,
1883 * this needs to be moved to the encoders, but for now we can do it
1884 * here to simplify things */
1885 avpkt->flags |= AV_PKT_FLAG_KEY;
1886
1887 end:
1888 av_frame_free(&padded_frame);
1889 av_free(extended_frame);
1890
1891 return ret;
1892 }
1893
1894 #if FF_API_OLD_ENCODE_AUDIO
avcodec_encode_audio(AVCodecContext * avctx,uint8_t * buf,int buf_size,const short * samples)1895 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1896 uint8_t *buf, int buf_size,
1897 const short *samples)
1898 {
1899 AVPacket pkt;
1900 AVFrame *frame;
1901 int ret, samples_size, got_packet;
1902
1903 av_init_packet(&pkt);
1904 pkt.data = buf;
1905 pkt.size = buf_size;
1906
1907 if (samples) {
1908 frame = av_frame_alloc();
1909 if (!frame)
1910 return AVERROR(ENOMEM);
1911
1912 if (avctx->frame_size) {
1913 frame->nb_samples = avctx->frame_size;
1914 } else {
1915 /* if frame_size is not set, the number of samples must be
1916 * calculated from the buffer size */
1917 int64_t nb_samples;
1918 if (!av_get_bits_per_sample(avctx->codec_id)) {
1919 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1920 "support this codec\n");
1921 av_frame_free(&frame);
1922 return AVERROR(EINVAL);
1923 }
1924 nb_samples = (int64_t)buf_size * 8 /
1925 (av_get_bits_per_sample(avctx->codec_id) *
1926 avctx->channels);
1927 if (nb_samples >= INT_MAX) {
1928 av_frame_free(&frame);
1929 return AVERROR(EINVAL);
1930 }
1931 frame->nb_samples = nb_samples;
1932 }
1933
1934 /* it is assumed that the samples buffer is large enough based on the
1935 * relevant parameters */
1936 samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1937 frame->nb_samples,
1938 avctx->sample_fmt, 1);
1939 if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1940 avctx->sample_fmt,
1941 (const uint8_t *)samples,
1942 samples_size, 1)) < 0) {
1943 av_frame_free(&frame);
1944 return ret;
1945 }
1946
1947 /* fabricate frame pts from sample count.
1948 * this is needed because the avcodec_encode_audio() API does not have
1949 * a way for the user to provide pts */
1950 if (avctx->sample_rate && avctx->time_base.num)
1951 frame->pts = ff_samples_to_time_base(avctx,
1952 avctx->internal->sample_count);
1953 else
1954 frame->pts = AV_NOPTS_VALUE;
1955 avctx->internal->sample_count += frame->nb_samples;
1956 } else {
1957 frame = NULL;
1958 }
1959
1960 got_packet = 0;
1961 ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1962 if (!ret && got_packet && avctx->coded_frame) {
1963 avctx->coded_frame->pts = pkt.pts;
1964 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1965 }
1966 /* free any side data since we cannot return it */
1967 av_packet_free_side_data(&pkt);
1968
1969 if (frame && frame->extended_data != frame->data)
1970 av_freep(&frame->extended_data);
1971
1972 av_frame_free(&frame);
1973 return ret ? ret : pkt.size;
1974 }
1975
1976 #endif
1977
1978 #if FF_API_OLD_ENCODE_VIDEO
avcodec_encode_video(AVCodecContext * avctx,uint8_t * buf,int buf_size,const AVFrame * pict)1979 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1980 const AVFrame *pict)
1981 {
1982 AVPacket pkt;
1983 int ret, got_packet = 0;
1984
1985 if (buf_size < FF_MIN_BUFFER_SIZE) {
1986 av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1987 return -1;
1988 }
1989
1990 av_init_packet(&pkt);
1991 pkt.data = buf;
1992 pkt.size = buf_size;
1993
1994 ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1995 if (!ret && got_packet && avctx->coded_frame) {
1996 avctx->coded_frame->pts = pkt.pts;
1997 avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1998 }
1999
2000 /* free any side data since we cannot return it */
2001 if (pkt.side_data_elems > 0) {
2002 int i;
2003 for (i = 0; i < pkt.side_data_elems; i++)
2004 av_free(pkt.side_data[i].data);
2005 av_freep(&pkt.side_data);
2006 pkt.side_data_elems = 0;
2007 }
2008
2009 return ret ? ret : pkt.size;
2010 }
2011
2012 #endif
2013
avcodec_encode_video2(AVCodecContext * avctx,AVPacket * avpkt,const AVFrame * frame,int * got_packet_ptr)2014 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
2015 AVPacket *avpkt,
2016 const AVFrame *frame,
2017 int *got_packet_ptr)
2018 {
2019 int ret;
2020 AVPacket user_pkt = *avpkt;
2021 int needs_realloc = !user_pkt.data;
2022
2023 *got_packet_ptr = 0;
2024
2025 if(CONFIG_FRAME_THREAD_ENCODER &&
2026 avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
2027 return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
2028
2029 if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
2030 avctx->stats_out[0] = '\0';
2031
2032 if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
2033 av_free_packet(avpkt);
2034 av_init_packet(avpkt);
2035 avpkt->size = 0;
2036 return 0;
2037 }
2038
2039 if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
2040 return AVERROR(EINVAL);
2041
2042 av_assert0(avctx->codec->encode2);
2043
2044 ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
2045 av_assert0(ret <= 0);
2046
2047 if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
2048 needs_realloc = 0;
2049 if (user_pkt.data) {
2050 if (user_pkt.size >= avpkt->size) {
2051 memcpy(user_pkt.data, avpkt->data, avpkt->size);
2052 } else {
2053 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
2054 avpkt->size = user_pkt.size;
2055 ret = -1;
2056 }
2057 avpkt->buf = user_pkt.buf;
2058 avpkt->data = user_pkt.data;
2059 #if FF_API_DESTRUCT_PACKET
2060 FF_DISABLE_DEPRECATION_WARNINGS
2061 avpkt->destruct = user_pkt.destruct;
2062 FF_ENABLE_DEPRECATION_WARNINGS
2063 #endif
2064 } else {
2065 if (av_dup_packet(avpkt) < 0) {
2066 ret = AVERROR(ENOMEM);
2067 }
2068 }
2069 }
2070
2071 if (!ret) {
2072 if (!*got_packet_ptr)
2073 avpkt->size = 0;
2074 else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
2075 avpkt->pts = avpkt->dts = frame->pts;
2076
2077 if (needs_realloc && avpkt->data) {
2078 ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
2079 if (ret >= 0)
2080 avpkt->data = avpkt->buf->data;
2081 }
2082
2083 avctx->frame_number++;
2084 }
2085
2086 if (ret < 0 || !*got_packet_ptr)
2087 av_free_packet(avpkt);
2088 else
2089 av_packet_merge_side_data(avpkt);
2090
2091 emms_c();
2092 return ret;
2093 }
2094
avcodec_encode_subtitle(AVCodecContext * avctx,uint8_t * buf,int buf_size,const AVSubtitle * sub)2095 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
2096 const AVSubtitle *sub)
2097 {
2098 int ret;
2099 if (sub->start_display_time) {
2100 av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
2101 return -1;
2102 }
2103
2104 ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
2105 avctx->frame_number++;
2106 return ret;
2107 }
2108
2109 /**
2110 * Attempt to guess proper monotonic timestamps for decoded video frames
2111 * which might have incorrect times. Input timestamps may wrap around, in
2112 * which case the output will as well.
2113 *
2114 * @param pts the pts field of the decoded AVPacket, as passed through
2115 * AVFrame.pkt_pts
2116 * @param dts the dts field of the decoded AVPacket
2117 * @return one of the input values, may be AV_NOPTS_VALUE
2118 */
guess_correct_pts(AVCodecContext * ctx,int64_t reordered_pts,int64_t dts)2119 static int64_t guess_correct_pts(AVCodecContext *ctx,
2120 int64_t reordered_pts, int64_t dts)
2121 {
2122 int64_t pts = AV_NOPTS_VALUE;
2123
2124 if (dts != AV_NOPTS_VALUE) {
2125 ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
2126 ctx->pts_correction_last_dts = dts;
2127 } else if (reordered_pts != AV_NOPTS_VALUE)
2128 ctx->pts_correction_last_dts = reordered_pts;
2129
2130 if (reordered_pts != AV_NOPTS_VALUE) {
2131 ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
2132 ctx->pts_correction_last_pts = reordered_pts;
2133 } else if(dts != AV_NOPTS_VALUE)
2134 ctx->pts_correction_last_pts = dts;
2135
2136 if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
2137 && reordered_pts != AV_NOPTS_VALUE)
2138 pts = reordered_pts;
2139 else
2140 pts = dts;
2141
2142 return pts;
2143 }
2144
apply_param_change(AVCodecContext * avctx,AVPacket * avpkt)2145 static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
2146 {
2147 int size = 0, ret;
2148 const uint8_t *data;
2149 uint32_t flags;
2150
2151 data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
2152 if (!data)
2153 return 0;
2154
2155 if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE)) {
2156 av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
2157 "changes, but PARAM_CHANGE side data was sent to it.\n");
2158 return AVERROR(EINVAL);
2159 }
2160
2161 if (size < 4)
2162 goto fail;
2163
2164 flags = bytestream_get_le32(&data);
2165 size -= 4;
2166
2167 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
2168 if (size < 4)
2169 goto fail;
2170 avctx->channels = bytestream_get_le32(&data);
2171 size -= 4;
2172 }
2173 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
2174 if (size < 8)
2175 goto fail;
2176 avctx->channel_layout = bytestream_get_le64(&data);
2177 size -= 8;
2178 }
2179 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
2180 if (size < 4)
2181 goto fail;
2182 avctx->sample_rate = bytestream_get_le32(&data);
2183 size -= 4;
2184 }
2185 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
2186 if (size < 8)
2187 goto fail;
2188 avctx->width = bytestream_get_le32(&data);
2189 avctx->height = bytestream_get_le32(&data);
2190 size -= 8;
2191 ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
2192 if (ret < 0)
2193 return ret;
2194 }
2195
2196 return 0;
2197 fail:
2198 av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
2199 return AVERROR_INVALIDDATA;
2200 }
2201
add_metadata_from_side_data(AVCodecContext * avctx,AVFrame * frame)2202 static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
2203 {
2204 int size;
2205 const uint8_t *side_metadata;
2206
2207 AVDictionary **frame_md = avpriv_frame_get_metadatap(frame);
2208
2209 side_metadata = av_packet_get_side_data(avctx->internal->pkt,
2210 AV_PKT_DATA_STRINGS_METADATA, &size);
2211 return av_packet_unpack_dictionary(side_metadata, size, frame_md);
2212 }
2213
unrefcount_frame(AVCodecInternal * avci,AVFrame * frame)2214 static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
2215 {
2216 int ret;
2217
2218 /* move the original frame to our backup */
2219 av_frame_unref(avci->to_free);
2220 av_frame_move_ref(avci->to_free, frame);
2221
2222 /* now copy everything except the AVBufferRefs back
2223 * note that we make a COPY of the side data, so calling av_frame_free() on
2224 * the caller's frame will work properly */
2225 ret = av_frame_copy_props(frame, avci->to_free);
2226 if (ret < 0)
2227 return ret;
2228
2229 memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
2230 memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
2231 if (avci->to_free->extended_data != avci->to_free->data) {
2232 int planes = av_frame_get_channels(avci->to_free);
2233 int size = planes * sizeof(*frame->extended_data);
2234
2235 if (!size) {
2236 av_frame_unref(frame);
2237 return AVERROR_BUG;
2238 }
2239
2240 frame->extended_data = av_malloc(size);
2241 if (!frame->extended_data) {
2242 av_frame_unref(frame);
2243 return AVERROR(ENOMEM);
2244 }
2245 memcpy(frame->extended_data, avci->to_free->extended_data,
2246 size);
2247 } else
2248 frame->extended_data = frame->data;
2249
2250 frame->format = avci->to_free->format;
2251 frame->width = avci->to_free->width;
2252 frame->height = avci->to_free->height;
2253 frame->channel_layout = avci->to_free->channel_layout;
2254 frame->nb_samples = avci->to_free->nb_samples;
2255 av_frame_set_channels(frame, av_frame_get_channels(avci->to_free));
2256
2257 return 0;
2258 }
2259
avcodec_decode_video2(AVCodecContext * avctx,AVFrame * picture,int * got_picture_ptr,const AVPacket * avpkt)2260 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
2261 int *got_picture_ptr,
2262 const AVPacket *avpkt)
2263 {
2264 AVCodecInternal *avci = avctx->internal;
2265 int ret;
2266 // copy to ensure we do not change avpkt
2267 AVPacket tmp = *avpkt;
2268
2269 if (!avctx->codec)
2270 return AVERROR(EINVAL);
2271 if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
2272 av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
2273 return AVERROR(EINVAL);
2274 }
2275
2276 *got_picture_ptr = 0;
2277 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
2278 return AVERROR(EINVAL);
2279
2280 av_frame_unref(picture);
2281
2282 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2283 int did_split = av_packet_split_side_data(&tmp);
2284 ret = apply_param_change(avctx, &tmp);
2285 if (ret < 0) {
2286 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
2287 if (avctx->err_recognition & AV_EF_EXPLODE)
2288 goto fail;
2289 }
2290
2291 avctx->internal->pkt = &tmp;
2292 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2293 ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
2294 &tmp);
2295 else {
2296 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
2297 &tmp);
2298 picture->pkt_dts = avpkt->dts;
2299
2300 if(!avctx->has_b_frames){
2301 av_frame_set_pkt_pos(picture, avpkt->pos);
2302 }
2303 //FIXME these should be under if(!avctx->has_b_frames)
2304 /* get_buffer is supposed to set frame parameters */
2305 if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
2306 if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
2307 if (!picture->width) picture->width = avctx->width;
2308 if (!picture->height) picture->height = avctx->height;
2309 if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
2310 }
2311 }
2312 add_metadata_from_side_data(avctx, picture);
2313
2314 fail:
2315 emms_c(); //needed to avoid an emms_c() call before every return;
2316
2317 avctx->internal->pkt = NULL;
2318 if (did_split) {
2319 av_packet_free_side_data(&tmp);
2320 if(ret == tmp.size)
2321 ret = avpkt->size;
2322 }
2323
2324 if (*got_picture_ptr) {
2325 if (!avctx->refcounted_frames) {
2326 int err = unrefcount_frame(avci, picture);
2327 if (err < 0)
2328 return err;
2329 }
2330
2331 avctx->frame_number++;
2332 av_frame_set_best_effort_timestamp(picture,
2333 guess_correct_pts(avctx,
2334 picture->pkt_pts,
2335 picture->pkt_dts));
2336 } else
2337 av_frame_unref(picture);
2338 } else
2339 ret = 0;
2340
2341 /* many decoders assign whole AVFrames, thus overwriting extended_data;
2342 * make sure it's set correctly */
2343 av_assert0(!picture->extended_data || picture->extended_data == picture->data);
2344
2345 return ret;
2346 }
2347
2348 #if FF_API_OLD_DECODE_AUDIO
avcodec_decode_audio3(AVCodecContext * avctx,int16_t * samples,int * frame_size_ptr,AVPacket * avpkt)2349 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
2350 int *frame_size_ptr,
2351 AVPacket *avpkt)
2352 {
2353 AVFrame *frame = av_frame_alloc();
2354 int ret, got_frame = 0;
2355
2356 if (!frame)
2357 return AVERROR(ENOMEM);
2358 if (avctx->get_buffer != avcodec_default_get_buffer) {
2359 av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
2360 "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
2361 av_log(avctx, AV_LOG_ERROR, "Please port your application to "
2362 "avcodec_decode_audio4()\n");
2363 avctx->get_buffer = avcodec_default_get_buffer;
2364 avctx->release_buffer = avcodec_default_release_buffer;
2365 }
2366
2367 ret = avcodec_decode_audio4(avctx, frame, &got_frame, avpkt);
2368
2369 if (ret >= 0 && got_frame) {
2370 int ch, plane_size;
2371 int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
2372 int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
2373 frame->nb_samples,
2374 avctx->sample_fmt, 1);
2375 if (*frame_size_ptr < data_size) {
2376 av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
2377 "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
2378 av_frame_free(&frame);
2379 return AVERROR(EINVAL);
2380 }
2381
2382 memcpy(samples, frame->extended_data[0], plane_size);
2383
2384 if (planar && avctx->channels > 1) {
2385 uint8_t *out = ((uint8_t *)samples) + plane_size;
2386 for (ch = 1; ch < avctx->channels; ch++) {
2387 memcpy(out, frame->extended_data[ch], plane_size);
2388 out += plane_size;
2389 }
2390 }
2391 *frame_size_ptr = data_size;
2392 } else {
2393 *frame_size_ptr = 0;
2394 }
2395 av_frame_free(&frame);
2396 return ret;
2397 }
2398
2399 #endif
2400
avcodec_decode_audio4(AVCodecContext * avctx,AVFrame * frame,int * got_frame_ptr,const AVPacket * avpkt)2401 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
2402 AVFrame *frame,
2403 int *got_frame_ptr,
2404 const AVPacket *avpkt)
2405 {
2406 AVCodecInternal *avci = avctx->internal;
2407 int ret = 0;
2408
2409 *got_frame_ptr = 0;
2410
2411 if (!avpkt->data && avpkt->size) {
2412 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2413 return AVERROR(EINVAL);
2414 }
2415 if (!avctx->codec)
2416 return AVERROR(EINVAL);
2417 if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
2418 av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
2419 return AVERROR(EINVAL);
2420 }
2421
2422 av_frame_unref(frame);
2423
2424 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
2425 uint8_t *side;
2426 int side_size;
2427 uint32_t discard_padding = 0;
2428 // copy to ensure we do not change avpkt
2429 AVPacket tmp = *avpkt;
2430 int did_split = av_packet_split_side_data(&tmp);
2431 ret = apply_param_change(avctx, &tmp);
2432 if (ret < 0) {
2433 av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
2434 if (avctx->err_recognition & AV_EF_EXPLODE)
2435 goto fail;
2436 }
2437
2438 avctx->internal->pkt = &tmp;
2439 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
2440 ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
2441 else {
2442 ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
2443 frame->pkt_dts = avpkt->dts;
2444 }
2445 if (ret >= 0 && *got_frame_ptr) {
2446 add_metadata_from_side_data(avctx, frame);
2447 avctx->frame_number++;
2448 av_frame_set_best_effort_timestamp(frame,
2449 guess_correct_pts(avctx,
2450 frame->pkt_pts,
2451 frame->pkt_dts));
2452 if (frame->format == AV_SAMPLE_FMT_NONE)
2453 frame->format = avctx->sample_fmt;
2454 if (!frame->channel_layout)
2455 frame->channel_layout = avctx->channel_layout;
2456 if (!av_frame_get_channels(frame))
2457 av_frame_set_channels(frame, avctx->channels);
2458 if (!frame->sample_rate)
2459 frame->sample_rate = avctx->sample_rate;
2460 }
2461
2462 side= av_packet_get_side_data(avctx->internal->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
2463 if(side && side_size>=10) {
2464 avctx->internal->skip_samples = AV_RL32(side);
2465 av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
2466 avctx->internal->skip_samples);
2467 discard_padding = AV_RL32(side + 4);
2468 }
2469 if (avctx->internal->skip_samples && *got_frame_ptr) {
2470 if(frame->nb_samples <= avctx->internal->skip_samples){
2471 *got_frame_ptr = 0;
2472 avctx->internal->skip_samples -= frame->nb_samples;
2473 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
2474 avctx->internal->skip_samples);
2475 } else {
2476 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
2477 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
2478 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2479 int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
2480 (AVRational){1, avctx->sample_rate},
2481 avctx->pkt_timebase);
2482 if(frame->pkt_pts!=AV_NOPTS_VALUE)
2483 frame->pkt_pts += diff_ts;
2484 if(frame->pkt_dts!=AV_NOPTS_VALUE)
2485 frame->pkt_dts += diff_ts;
2486 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2487 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2488 } else {
2489 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
2490 }
2491 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
2492 avctx->internal->skip_samples, frame->nb_samples);
2493 frame->nb_samples -= avctx->internal->skip_samples;
2494 avctx->internal->skip_samples = 0;
2495 }
2496 }
2497
2498 if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr) {
2499 if (discard_padding == frame->nb_samples) {
2500 *got_frame_ptr = 0;
2501 } else {
2502 if(avctx->pkt_timebase.num && avctx->sample_rate) {
2503 int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
2504 (AVRational){1, avctx->sample_rate},
2505 avctx->pkt_timebase);
2506 if (av_frame_get_pkt_duration(frame) >= diff_ts)
2507 av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
2508 } else {
2509 av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
2510 }
2511 av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
2512 discard_padding, frame->nb_samples);
2513 frame->nb_samples -= discard_padding;
2514 }
2515 }
2516 fail:
2517 avctx->internal->pkt = NULL;
2518 if (did_split) {
2519 av_packet_free_side_data(&tmp);
2520 if(ret == tmp.size)
2521 ret = avpkt->size;
2522 }
2523
2524 if (ret >= 0 && *got_frame_ptr) {
2525 if (!avctx->refcounted_frames) {
2526 int err = unrefcount_frame(avci, frame);
2527 if (err < 0)
2528 return err;
2529 }
2530 } else
2531 av_frame_unref(frame);
2532 }
2533
2534 return ret;
2535 }
2536
2537 #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
recode_subtitle(AVCodecContext * avctx,AVPacket * outpkt,const AVPacket * inpkt)2538 static int recode_subtitle(AVCodecContext *avctx,
2539 AVPacket *outpkt, const AVPacket *inpkt)
2540 {
2541 #if CONFIG_ICONV
2542 iconv_t cd = (iconv_t)-1;
2543 int ret = 0;
2544 #ifdef _ICONV_H
2545 char *inb;
2546 #else
2547 const char *inb;
2548 #endif
2549 char *outb;
2550 size_t inl, outl;
2551 AVPacket tmp;
2552 #endif
2553
2554 if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER || inpkt->size == 0)
2555 return 0;
2556
2557 #if CONFIG_ICONV
2558 cd = iconv_open("UTF-8", avctx->sub_charenc);
2559 av_assert0(cd != (iconv_t)-1);
2560
2561 inb = inpkt->data;
2562 inl = inpkt->size;
2563
2564 if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
2565 av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
2566 ret = AVERROR(ENOMEM);
2567 goto end;
2568 }
2569
2570 ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
2571 if (ret < 0)
2572 goto end;
2573 outpkt->buf = tmp.buf;
2574 outpkt->data = tmp.data;
2575 outpkt->size = tmp.size;
2576 outb = outpkt->data;
2577 outl = outpkt->size;
2578
2579 if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
2580 iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
2581 outl >= outpkt->size || inl != 0) {
2582 av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
2583 "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
2584 av_free_packet(&tmp);
2585 ret = AVERROR(errno);
2586 goto end;
2587 }
2588 outpkt->size -= outl;
2589 memset(outpkt->data + outpkt->size, 0, outl);
2590
2591 end:
2592 if (cd != (iconv_t)-1)
2593 iconv_close(cd);
2594 return ret;
2595 #else
2596 av_log(avctx, AV_LOG_ERROR, "requesting subtitles recoding without iconv");
2597 return AVERROR(EINVAL);
2598 #endif
2599 }
2600
utf8_check(const uint8_t * str)2601 static int utf8_check(const uint8_t *str)
2602 {
2603 const uint8_t *byte;
2604 uint32_t codepoint, min;
2605
2606 while (*str) {
2607 byte = str;
2608 GET_UTF8(codepoint, *(byte++), return 0;);
2609 min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
2610 1 << (5 * (byte - str) - 4);
2611 if (codepoint < min || codepoint >= 0x110000 ||
2612 codepoint == 0xFFFE /* BOM */ ||
2613 codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
2614 return 0;
2615 str = byte;
2616 }
2617 return 1;
2618 }
2619
avcodec_decode_subtitle2(AVCodecContext * avctx,AVSubtitle * sub,int * got_sub_ptr,AVPacket * avpkt)2620 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
2621 int *got_sub_ptr,
2622 AVPacket *avpkt)
2623 {
2624 int i, ret = 0;
2625
2626 if (!avpkt->data && avpkt->size) {
2627 av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
2628 return AVERROR(EINVAL);
2629 }
2630 if (!avctx->codec)
2631 return AVERROR(EINVAL);
2632 if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
2633 av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
2634 return AVERROR(EINVAL);
2635 }
2636
2637 *got_sub_ptr = 0;
2638 avcodec_get_subtitle_defaults(sub);
2639
2640 if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
2641 AVPacket pkt_recoded;
2642 AVPacket tmp = *avpkt;
2643 int did_split = av_packet_split_side_data(&tmp);
2644 //apply_param_change(avctx, &tmp);
2645
2646 if (did_split) {
2647 /* FFMIN() prevents overflow in case the packet wasn't allocated with
2648 * proper padding.
2649 * If the side data is smaller than the buffer padding size, the
2650 * remaining bytes should have already been filled with zeros by the
2651 * original packet allocation anyway. */
2652 memset(tmp.data + tmp.size, 0,
2653 FFMIN(avpkt->size - tmp.size, FF_INPUT_BUFFER_PADDING_SIZE));
2654 }
2655
2656 pkt_recoded = tmp;
2657 ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
2658 if (ret < 0) {
2659 *got_sub_ptr = 0;
2660 } else {
2661 avctx->internal->pkt = &pkt_recoded;
2662
2663 if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
2664 sub->pts = av_rescale_q(avpkt->pts,
2665 avctx->pkt_timebase, AV_TIME_BASE_Q);
2666 ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
2667 av_assert1((ret >= 0) >= !!*got_sub_ptr &&
2668 !!*got_sub_ptr >= !!sub->num_rects);
2669
2670 if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
2671 avctx->pkt_timebase.num) {
2672 AVRational ms = { 1, 1000 };
2673 sub->end_display_time = av_rescale_q(avpkt->duration,
2674 avctx->pkt_timebase, ms);
2675 }
2676
2677 for (i = 0; i < sub->num_rects; i++) {
2678 if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
2679 av_log(avctx, AV_LOG_ERROR,
2680 "Invalid UTF-8 in decoded subtitles text; "
2681 "maybe missing -sub_charenc option\n");
2682 avsubtitle_free(sub);
2683 return AVERROR_INVALIDDATA;
2684 }
2685 }
2686
2687 if (tmp.data != pkt_recoded.data) { // did we recode?
2688 /* prevent from destroying side data from original packet */
2689 pkt_recoded.side_data = NULL;
2690 pkt_recoded.side_data_elems = 0;
2691
2692 av_free_packet(&pkt_recoded);
2693 }
2694 if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
2695 sub->format = 0;
2696 else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
2697 sub->format = 1;
2698 avctx->internal->pkt = NULL;
2699 }
2700
2701 if (did_split) {
2702 av_packet_free_side_data(&tmp);
2703 if(ret == tmp.size)
2704 ret = avpkt->size;
2705 }
2706
2707 if (*got_sub_ptr)
2708 avctx->frame_number++;
2709 }
2710
2711 return ret;
2712 }
2713
avsubtitle_free(AVSubtitle * sub)2714 void avsubtitle_free(AVSubtitle *sub)
2715 {
2716 int i;
2717
2718 for (i = 0; i < sub->num_rects; i++) {
2719 av_freep(&sub->rects[i]->pict.data[0]);
2720 av_freep(&sub->rects[i]->pict.data[1]);
2721 av_freep(&sub->rects[i]->pict.data[2]);
2722 av_freep(&sub->rects[i]->pict.data[3]);
2723 av_freep(&sub->rects[i]->text);
2724 av_freep(&sub->rects[i]->ass);
2725 av_freep(&sub->rects[i]);
2726 }
2727
2728 av_freep(&sub->rects);
2729
2730 memset(sub, 0, sizeof(AVSubtitle));
2731 }
2732
avcodec_close(AVCodecContext * avctx)2733 av_cold int avcodec_close(AVCodecContext *avctx)
2734 {
2735 if (!avctx)
2736 return 0;
2737
2738 if (avcodec_is_open(avctx)) {
2739 FramePool *pool = avctx->internal->pool;
2740 int i;
2741 if (CONFIG_FRAME_THREAD_ENCODER &&
2742 avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
2743 ff_frame_thread_encoder_free(avctx);
2744 }
2745 if (HAVE_THREADS && avctx->internal->thread_ctx)
2746 ff_thread_free(avctx);
2747 if (avctx->codec && avctx->codec->close)
2748 avctx->codec->close(avctx);
2749 avctx->coded_frame = NULL;
2750 avctx->internal->byte_buffer_size = 0;
2751 av_freep(&avctx->internal->byte_buffer);
2752 av_frame_free(&avctx->internal->to_free);
2753 for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
2754 av_buffer_pool_uninit(&pool->pools[i]);
2755 av_freep(&avctx->internal->pool);
2756
2757 if (avctx->hwaccel && avctx->hwaccel->uninit)
2758 avctx->hwaccel->uninit(avctx);
2759 av_freep(&avctx->internal->hwaccel_priv_data);
2760
2761 av_freep(&avctx->internal);
2762 }
2763
2764 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
2765 av_opt_free(avctx->priv_data);
2766 av_opt_free(avctx);
2767 av_freep(&avctx->priv_data);
2768 if (av_codec_is_encoder(avctx->codec))
2769 av_freep(&avctx->extradata);
2770 avctx->codec = NULL;
2771 avctx->active_thread_type = 0;
2772
2773 return 0;
2774 }
2775
remap_deprecated_codec_id(enum AVCodecID id)2776 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
2777 {
2778 switch(id){
2779 //This is for future deprecatec codec ids, its empty since
2780 //last major bump but will fill up again over time, please don't remove it
2781 // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
2782 case AV_CODEC_ID_BRENDER_PIX_DEPRECATED : return AV_CODEC_ID_BRENDER_PIX;
2783 case AV_CODEC_ID_OPUS_DEPRECATED : return AV_CODEC_ID_OPUS;
2784 case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
2785 case AV_CODEC_ID_PAF_AUDIO_DEPRECATED : return AV_CODEC_ID_PAF_AUDIO;
2786 case AV_CODEC_ID_PCM_S24LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S24LE_PLANAR;
2787 case AV_CODEC_ID_PCM_S32LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S32LE_PLANAR;
2788 case AV_CODEC_ID_ADPCM_VIMA_DEPRECATED : return AV_CODEC_ID_ADPCM_VIMA;
2789 case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130;
2790 case AV_CODEC_ID_EXR_DEPRECATED : return AV_CODEC_ID_EXR;
2791 case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M;
2792 case AV_CODEC_ID_PAF_VIDEO_DEPRECATED : return AV_CODEC_ID_PAF_VIDEO;
2793 case AV_CODEC_ID_WEBP_DEPRECATED : return AV_CODEC_ID_WEBP;
2794 case AV_CODEC_ID_HEVC_DEPRECATED : return AV_CODEC_ID_HEVC;
2795 case AV_CODEC_ID_MVC1_DEPRECATED : return AV_CODEC_ID_MVC1;
2796 case AV_CODEC_ID_MVC2_DEPRECATED : return AV_CODEC_ID_MVC2;
2797 case AV_CODEC_ID_SANM_DEPRECATED : return AV_CODEC_ID_SANM;
2798 case AV_CODEC_ID_SGIRLE_DEPRECATED : return AV_CODEC_ID_SGIRLE;
2799 case AV_CODEC_ID_VP7_DEPRECATED : return AV_CODEC_ID_VP7;
2800 default : return id;
2801 }
2802 }
2803
find_encdec(enum AVCodecID id,int encoder)2804 static AVCodec *find_encdec(enum AVCodecID id, int encoder)
2805 {
2806 AVCodec *p, *experimental = NULL;
2807 p = first_avcodec;
2808 id= remap_deprecated_codec_id(id);
2809 while (p) {
2810 if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
2811 p->id == id) {
2812 if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
2813 experimental = p;
2814 } else
2815 return p;
2816 }
2817 p = p->next;
2818 }
2819 return experimental;
2820 }
2821
avcodec_find_encoder(enum AVCodecID id)2822 AVCodec *avcodec_find_encoder(enum AVCodecID id)
2823 {
2824 return find_encdec(id, 1);
2825 }
2826
avcodec_find_encoder_by_name(const char * name)2827 AVCodec *avcodec_find_encoder_by_name(const char *name)
2828 {
2829 AVCodec *p;
2830 if (!name)
2831 return NULL;
2832 p = first_avcodec;
2833 while (p) {
2834 if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
2835 return p;
2836 p = p->next;
2837 }
2838 return NULL;
2839 }
2840
avcodec_find_decoder(enum AVCodecID id)2841 AVCodec *avcodec_find_decoder(enum AVCodecID id)
2842 {
2843 return find_encdec(id, 0);
2844 }
2845
avcodec_find_decoder_by_name(const char * name)2846 AVCodec *avcodec_find_decoder_by_name(const char *name)
2847 {
2848 AVCodec *p;
2849 if (!name)
2850 return NULL;
2851 p = first_avcodec;
2852 while (p) {
2853 if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
2854 return p;
2855 p = p->next;
2856 }
2857 return NULL;
2858 }
2859
avcodec_get_name(enum AVCodecID id)2860 const char *avcodec_get_name(enum AVCodecID id)
2861 {
2862 const AVCodecDescriptor *cd;
2863 AVCodec *codec;
2864
2865 if (id == AV_CODEC_ID_NONE)
2866 return "none";
2867 cd = avcodec_descriptor_get(id);
2868 if (cd)
2869 return cd->name;
2870 av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
2871 codec = avcodec_find_decoder(id);
2872 if (codec)
2873 return codec->name;
2874 codec = avcodec_find_encoder(id);
2875 if (codec)
2876 return codec->name;
2877 return "unknown_codec";
2878 }
2879
av_get_codec_tag_string(char * buf,size_t buf_size,unsigned int codec_tag)2880 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
2881 {
2882 int i, len, ret = 0;
2883
2884 #define TAG_PRINT(x) \
2885 (((x) >= '0' && (x) <= '9') || \
2886 ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
2887 ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
2888
2889 for (i = 0; i < 4; i++) {
2890 len = snprintf(buf, buf_size,
2891 TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
2892 buf += len;
2893 buf_size = buf_size > len ? buf_size - len : 0;
2894 ret += len;
2895 codec_tag >>= 8;
2896 }
2897 return ret;
2898 }
2899
avcodec_string(char * buf,int buf_size,AVCodecContext * enc,int encode)2900 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
2901 {
2902 const char *codec_type;
2903 const char *codec_name;
2904 const char *profile = NULL;
2905 const AVCodec *p;
2906 int bitrate;
2907 AVRational display_aspect_ratio;
2908
2909 if (!buf || buf_size <= 0)
2910 return;
2911 codec_type = av_get_media_type_string(enc->codec_type);
2912 codec_name = avcodec_get_name(enc->codec_id);
2913 if (enc->profile != FF_PROFILE_UNKNOWN) {
2914 if (enc->codec)
2915 p = enc->codec;
2916 else
2917 p = encode ? avcodec_find_encoder(enc->codec_id) :
2918 avcodec_find_decoder(enc->codec_id);
2919 if (p)
2920 profile = av_get_profile_name(p, enc->profile);
2921 }
2922
2923 snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
2924 codec_name);
2925 buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
2926
2927 if (enc->codec && strcmp(enc->codec->name, codec_name))
2928 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
2929
2930 if (profile)
2931 snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
2932 if (enc->codec_tag) {
2933 char tag_buf[32];
2934 av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
2935 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2936 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
2937 }
2938
2939 switch (enc->codec_type) {
2940 case AVMEDIA_TYPE_VIDEO:
2941 if (enc->pix_fmt != AV_PIX_FMT_NONE) {
2942 char detail[256] = "(";
2943 const char *colorspace_name;
2944 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2945 ", %s",
2946 av_get_pix_fmt_name(enc->pix_fmt));
2947 if (enc->bits_per_raw_sample &&
2948 enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
2949 av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
2950 if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
2951 av_strlcatf(detail, sizeof(detail),
2952 enc->color_range == AVCOL_RANGE_MPEG ? "tv, ": "pc, ");
2953
2954 colorspace_name = av_get_colorspace_name(enc->colorspace);
2955 if (colorspace_name)
2956 av_strlcatf(detail, sizeof(detail), "%s, ", colorspace_name);
2957
2958 if (strlen(detail) > 1) {
2959 detail[strlen(detail) - 2] = 0;
2960 av_strlcatf(buf, buf_size, "%s)", detail);
2961 }
2962 }
2963 if (enc->width) {
2964 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2965 ", %dx%d",
2966 enc->width, enc->height);
2967 if (enc->sample_aspect_ratio.num) {
2968 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2969 enc->width * enc->sample_aspect_ratio.num,
2970 enc->height * enc->sample_aspect_ratio.den,
2971 1024 * 1024);
2972 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2973 " [SAR %d:%d DAR %d:%d]",
2974 enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
2975 display_aspect_ratio.num, display_aspect_ratio.den);
2976 }
2977 if (av_log_get_level() >= AV_LOG_DEBUG) {
2978 int g = av_gcd(enc->time_base.num, enc->time_base.den);
2979 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2980 ", %d/%d",
2981 enc->time_base.num / g, enc->time_base.den / g);
2982 }
2983 }
2984 if (encode) {
2985 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2986 ", q=%d-%d", enc->qmin, enc->qmax);
2987 }
2988 break;
2989 case AVMEDIA_TYPE_AUDIO:
2990 if (enc->sample_rate) {
2991 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2992 ", %d Hz", enc->sample_rate);
2993 }
2994 av_strlcat(buf, ", ", buf_size);
2995 av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
2996 if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
2997 snprintf(buf + strlen(buf), buf_size - strlen(buf),
2998 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
2999 }
3000 if ( enc->bits_per_raw_sample > 0
3001 && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)
3002 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3003 " (%d bit)", enc->bits_per_raw_sample);
3004 break;
3005 case AVMEDIA_TYPE_DATA:
3006 if (av_log_get_level() >= AV_LOG_DEBUG) {
3007 int g = av_gcd(enc->time_base.num, enc->time_base.den);
3008 if (g)
3009 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3010 ", %d/%d",
3011 enc->time_base.num / g, enc->time_base.den / g);
3012 }
3013 break;
3014 case AVMEDIA_TYPE_SUBTITLE:
3015 if (enc->width)
3016 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3017 ", %dx%d", enc->width, enc->height);
3018 break;
3019 default:
3020 return;
3021 }
3022 if (encode) {
3023 if (enc->flags & CODEC_FLAG_PASS1)
3024 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3025 ", pass 1");
3026 if (enc->flags & CODEC_FLAG_PASS2)
3027 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3028 ", pass 2");
3029 }
3030 bitrate = get_bit_rate(enc);
3031 if (bitrate != 0) {
3032 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3033 ", %d kb/s", bitrate / 1000);
3034 } else if (enc->rc_max_rate > 0) {
3035 snprintf(buf + strlen(buf), buf_size - strlen(buf),
3036 ", max. %d kb/s", enc->rc_max_rate / 1000);
3037 }
3038 }
3039
av_get_profile_name(const AVCodec * codec,int profile)3040 const char *av_get_profile_name(const AVCodec *codec, int profile)
3041 {
3042 const AVProfile *p;
3043 if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
3044 return NULL;
3045
3046 for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
3047 if (p->profile == profile)
3048 return p->name;
3049
3050 return NULL;
3051 }
3052
avcodec_version(void)3053 unsigned avcodec_version(void)
3054 {
3055 // av_assert0(AV_CODEC_ID_V410==164);
3056 av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
3057 av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
3058 // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
3059 av_assert0(AV_CODEC_ID_SRT==94216);
3060 av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
3061
3062 av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
3063 av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
3064 av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
3065 av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
3066 av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
3067 return LIBAVCODEC_VERSION_INT;
3068 }
3069
avcodec_configuration(void)3070 const char *avcodec_configuration(void)
3071 {
3072 return FFMPEG_CONFIGURATION;
3073 }
3074
avcodec_license(void)3075 const char *avcodec_license(void)
3076 {
3077 #define LICENSE_PREFIX "libavcodec license: "
3078 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
3079 }
3080
avcodec_flush_buffers(AVCodecContext * avctx)3081 void avcodec_flush_buffers(AVCodecContext *avctx)
3082 {
3083 if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
3084 ff_thread_flush(avctx);
3085 else if (avctx->codec->flush)
3086 avctx->codec->flush(avctx);
3087
3088 avctx->pts_correction_last_pts =
3089 avctx->pts_correction_last_dts = INT64_MIN;
3090
3091 if (!avctx->refcounted_frames)
3092 av_frame_unref(avctx->internal->to_free);
3093 }
3094
av_get_exact_bits_per_sample(enum AVCodecID codec_id)3095 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
3096 {
3097 switch (codec_id) {
3098 case AV_CODEC_ID_8SVX_EXP:
3099 case AV_CODEC_ID_8SVX_FIB:
3100 case AV_CODEC_ID_ADPCM_CT:
3101 case AV_CODEC_ID_ADPCM_IMA_APC:
3102 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
3103 case AV_CODEC_ID_ADPCM_IMA_OKI:
3104 case AV_CODEC_ID_ADPCM_IMA_WS:
3105 case AV_CODEC_ID_ADPCM_G722:
3106 case AV_CODEC_ID_ADPCM_YAMAHA:
3107 return 4;
3108 case AV_CODEC_ID_DSD_LSBF:
3109 case AV_CODEC_ID_DSD_MSBF:
3110 case AV_CODEC_ID_DSD_LSBF_PLANAR:
3111 case AV_CODEC_ID_DSD_MSBF_PLANAR:
3112 case AV_CODEC_ID_PCM_ALAW:
3113 case AV_CODEC_ID_PCM_MULAW:
3114 case AV_CODEC_ID_PCM_S8:
3115 case AV_CODEC_ID_PCM_S8_PLANAR:
3116 case AV_CODEC_ID_PCM_U8:
3117 case AV_CODEC_ID_PCM_ZORK:
3118 return 8;
3119 case AV_CODEC_ID_PCM_S16BE:
3120 case AV_CODEC_ID_PCM_S16BE_PLANAR:
3121 case AV_CODEC_ID_PCM_S16LE:
3122 case AV_CODEC_ID_PCM_S16LE_PLANAR:
3123 case AV_CODEC_ID_PCM_U16BE:
3124 case AV_CODEC_ID_PCM_U16LE:
3125 return 16;
3126 case AV_CODEC_ID_PCM_S24DAUD:
3127 case AV_CODEC_ID_PCM_S24BE:
3128 case AV_CODEC_ID_PCM_S24LE:
3129 case AV_CODEC_ID_PCM_S24LE_PLANAR:
3130 case AV_CODEC_ID_PCM_U24BE:
3131 case AV_CODEC_ID_PCM_U24LE:
3132 return 24;
3133 case AV_CODEC_ID_PCM_S32BE:
3134 case AV_CODEC_ID_PCM_S32LE:
3135 case AV_CODEC_ID_PCM_S32LE_PLANAR:
3136 case AV_CODEC_ID_PCM_U32BE:
3137 case AV_CODEC_ID_PCM_U32LE:
3138 case AV_CODEC_ID_PCM_F32BE:
3139 case AV_CODEC_ID_PCM_F32LE:
3140 return 32;
3141 case AV_CODEC_ID_PCM_F64BE:
3142 case AV_CODEC_ID_PCM_F64LE:
3143 return 64;
3144 default:
3145 return 0;
3146 }
3147 }
3148
av_get_pcm_codec(enum AVSampleFormat fmt,int be)3149 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
3150 {
3151 static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
3152 [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
3153 [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
3154 [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
3155 [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
3156 [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
3157 [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
3158 [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
3159 [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
3160 [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
3161 [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
3162 };
3163 if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
3164 return AV_CODEC_ID_NONE;
3165 if (be < 0 || be > 1)
3166 be = AV_NE(1, 0);
3167 return map[fmt][be];
3168 }
3169
av_get_bits_per_sample(enum AVCodecID codec_id)3170 int av_get_bits_per_sample(enum AVCodecID codec_id)
3171 {
3172 switch (codec_id) {
3173 case AV_CODEC_ID_ADPCM_SBPRO_2:
3174 return 2;
3175 case AV_CODEC_ID_ADPCM_SBPRO_3:
3176 return 3;
3177 case AV_CODEC_ID_ADPCM_SBPRO_4:
3178 case AV_CODEC_ID_ADPCM_IMA_WAV:
3179 case AV_CODEC_ID_ADPCM_IMA_QT:
3180 case AV_CODEC_ID_ADPCM_SWF:
3181 case AV_CODEC_ID_ADPCM_MS:
3182 return 4;
3183 default:
3184 return av_get_exact_bits_per_sample(codec_id);
3185 }
3186 }
3187
av_get_audio_frame_duration(AVCodecContext * avctx,int frame_bytes)3188 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
3189 {
3190 int id, sr, ch, ba, tag, bps;
3191
3192 id = avctx->codec_id;
3193 sr = avctx->sample_rate;
3194 ch = avctx->channels;
3195 ba = avctx->block_align;
3196 tag = avctx->codec_tag;
3197 bps = av_get_exact_bits_per_sample(avctx->codec_id);
3198
3199 /* codecs with an exact constant bits per sample */
3200 if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
3201 return (frame_bytes * LLN(8)) / (bps * ch);
3202 bps = avctx->bits_per_coded_sample;
3203
3204 /* codecs with a fixed packet duration */
3205 switch (id) {
3206 case AV_CODEC_ID_ADPCM_ADX: return 32;
3207 case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
3208 case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
3209 case AV_CODEC_ID_AMR_NB:
3210 case AV_CODEC_ID_EVRC:
3211 case AV_CODEC_ID_GSM:
3212 case AV_CODEC_ID_QCELP:
3213 case AV_CODEC_ID_RA_288: return 160;
3214 case AV_CODEC_ID_AMR_WB:
3215 case AV_CODEC_ID_GSM_MS: return 320;
3216 case AV_CODEC_ID_MP1: return 384;
3217 case AV_CODEC_ID_ATRAC1: return 512;
3218 case AV_CODEC_ID_ATRAC3: return 1024;
3219 case AV_CODEC_ID_MP2:
3220 case AV_CODEC_ID_MUSEPACK7: return 1152;
3221 case AV_CODEC_ID_AC3: return 1536;
3222 }
3223
3224 if (sr > 0) {
3225 /* calc from sample rate */
3226 if (id == AV_CODEC_ID_TTA)
3227 return 256 * sr / 245;
3228
3229 if (ch > 0) {
3230 /* calc from sample rate and channels */
3231 if (id == AV_CODEC_ID_BINKAUDIO_DCT)
3232 return (480 << (sr / 22050)) / ch;
3233 }
3234 }
3235
3236 if (ba > 0) {
3237 /* calc from block_align */
3238 if (id == AV_CODEC_ID_SIPR) {
3239 switch (ba) {
3240 case 20: return 160;
3241 case 19: return 144;
3242 case 29: return 288;
3243 case 37: return 480;
3244 }
3245 } else if (id == AV_CODEC_ID_ILBC) {
3246 switch (ba) {
3247 case 38: return 160;
3248 case 50: return 240;
3249 }
3250 }
3251 }
3252
3253 if (frame_bytes > 0) {
3254 /* calc from frame_bytes only */
3255 if (id == AV_CODEC_ID_TRUESPEECH)
3256 return 240 * (frame_bytes / 32);
3257 if (id == AV_CODEC_ID_NELLYMOSER)
3258 return 256 * (frame_bytes / 64);
3259 if (id == AV_CODEC_ID_RA_144)
3260 return 160 * (frame_bytes / 20);
3261 if (id == AV_CODEC_ID_G723_1)
3262 return 240 * (frame_bytes / 24);
3263
3264 if (bps > 0) {
3265 /* calc from frame_bytes and bits_per_coded_sample */
3266 if (id == AV_CODEC_ID_ADPCM_G726)
3267 return frame_bytes * 8 / bps;
3268 }
3269
3270 if (ch > 0) {
3271 /* calc from frame_bytes and channels */
3272 switch (id) {
3273 case AV_CODEC_ID_ADPCM_AFC:
3274 return frame_bytes / (9 * ch) * 16;
3275 case AV_CODEC_ID_ADPCM_DTK:
3276 return frame_bytes / (16 * ch) * 28;
3277 case AV_CODEC_ID_ADPCM_4XM:
3278 case AV_CODEC_ID_ADPCM_IMA_ISS:
3279 return (frame_bytes - 4 * ch) * 2 / ch;
3280 case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
3281 return (frame_bytes - 4) * 2 / ch;
3282 case AV_CODEC_ID_ADPCM_IMA_AMV:
3283 return (frame_bytes - 8) * 2 / ch;
3284 case AV_CODEC_ID_ADPCM_XA:
3285 return (frame_bytes / 128) * 224 / ch;
3286 case AV_CODEC_ID_INTERPLAY_DPCM:
3287 return (frame_bytes - 6 - ch) / ch;
3288 case AV_CODEC_ID_ROQ_DPCM:
3289 return (frame_bytes - 8) / ch;
3290 case AV_CODEC_ID_XAN_DPCM:
3291 return (frame_bytes - 2 * ch) / ch;
3292 case AV_CODEC_ID_MACE3:
3293 return 3 * frame_bytes / ch;
3294 case AV_CODEC_ID_MACE6:
3295 return 6 * frame_bytes / ch;
3296 case AV_CODEC_ID_PCM_LXF:
3297 return 2 * (frame_bytes / (5 * ch));
3298 case AV_CODEC_ID_IAC:
3299 case AV_CODEC_ID_IMC:
3300 return 4 * frame_bytes / ch;
3301 }
3302
3303 if (tag) {
3304 /* calc from frame_bytes, channels, and codec_tag */
3305 if (id == AV_CODEC_ID_SOL_DPCM) {
3306 if (tag == 3)
3307 return frame_bytes / ch;
3308 else
3309 return frame_bytes * 2 / ch;
3310 }
3311 }
3312
3313 if (ba > 0) {
3314 /* calc from frame_bytes, channels, and block_align */
3315 int blocks = frame_bytes / ba;
3316 switch (avctx->codec_id) {
3317 case AV_CODEC_ID_ADPCM_IMA_WAV:
3318 if (bps < 2 || bps > 5)
3319 return 0;
3320 return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
3321 case AV_CODEC_ID_ADPCM_IMA_DK3:
3322 return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
3323 case AV_CODEC_ID_ADPCM_IMA_DK4:
3324 return blocks * (1 + (ba - 4 * ch) * 2 / ch);
3325 case AV_CODEC_ID_ADPCM_IMA_RAD:
3326 return blocks * ((ba - 4 * ch) * 2 / ch);
3327 case AV_CODEC_ID_ADPCM_MS:
3328 return blocks * (2 + (ba - 7 * ch) * 2 / ch);
3329 }
3330 }
3331
3332 if (bps > 0) {
3333 /* calc from frame_bytes, channels, and bits_per_coded_sample */
3334 switch (avctx->codec_id) {
3335 case AV_CODEC_ID_PCM_DVD:
3336 if(bps<4)
3337 return 0;
3338 return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
3339 case AV_CODEC_ID_PCM_BLURAY:
3340 if(bps<4)
3341 return 0;
3342 return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
3343 case AV_CODEC_ID_S302M:
3344 return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
3345 }
3346 }
3347 }
3348 }
3349
3350 /* Fall back on using frame_size */
3351 if (avctx->frame_size > 1 && frame_bytes)
3352 return avctx->frame_size;
3353
3354 //For WMA we currently have no other means to calculate duration thus we
3355 //do it here by assuming CBR, which is true for all known cases.
3356 if (avctx->bit_rate>0 && frame_bytes>0 && avctx->sample_rate>0 && avctx->block_align>1) {
3357 if (avctx->codec_id == AV_CODEC_ID_WMAV1 || avctx->codec_id == AV_CODEC_ID_WMAV2)
3358 return (frame_bytes * LLN(8) * avctx->sample_rate) / avctx->bit_rate;
3359 }
3360
3361 return 0;
3362 }
3363
3364 #if !HAVE_THREADS
ff_thread_init(AVCodecContext * s)3365 int ff_thread_init(AVCodecContext *s)
3366 {
3367 return -1;
3368 }
3369
3370 #endif
3371
av_xiphlacing(unsigned char * s,unsigned int v)3372 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
3373 {
3374 unsigned int n = 0;
3375
3376 while (v >= 0xff) {
3377 *s++ = 0xff;
3378 v -= 0xff;
3379 n++;
3380 }
3381 *s = v;
3382 n++;
3383 return n;
3384 }
3385
ff_match_2uint16(const uint16_t (* tab)[2],int size,int a,int b)3386 int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
3387 {
3388 int i;
3389 for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
3390 return i;
3391 }
3392
3393 #if FF_API_MISSING_SAMPLE
3394 FF_DISABLE_DEPRECATION_WARNINGS
av_log_missing_feature(void * avc,const char * feature,int want_sample)3395 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
3396 {
3397 av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
3398 "version to the newest one from Git. If the problem still "
3399 "occurs, it means that your file has a feature which has not "
3400 "been implemented.\n", feature);
3401 if(want_sample)
3402 av_log_ask_for_sample(avc, NULL);
3403 }
3404
av_log_ask_for_sample(void * avc,const char * msg,...)3405 void av_log_ask_for_sample(void *avc, const char *msg, ...)
3406 {
3407 va_list argument_list;
3408
3409 va_start(argument_list, msg);
3410
3411 if (msg)
3412 av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
3413 av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
3414 "of this file to ftp://upload.ffmpeg.org/incoming/ "
3415 "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n");
3416
3417 va_end(argument_list);
3418 }
3419 FF_ENABLE_DEPRECATION_WARNINGS
3420 #endif /* FF_API_MISSING_SAMPLE */
3421
3422 static AVHWAccel *first_hwaccel = NULL;
3423 static AVHWAccel **last_hwaccel = &first_hwaccel;
3424
av_register_hwaccel(AVHWAccel * hwaccel)3425 void av_register_hwaccel(AVHWAccel *hwaccel)
3426 {
3427 AVHWAccel **p = last_hwaccel;
3428 hwaccel->next = NULL;
3429 while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
3430 p = &(*p)->next;
3431 last_hwaccel = &hwaccel->next;
3432 }
3433
av_hwaccel_next(const AVHWAccel * hwaccel)3434 AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
3435 {
3436 return hwaccel ? hwaccel->next : first_hwaccel;
3437 }
3438
av_lockmgr_register(int (* cb)(void ** mutex,enum AVLockOp op))3439 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
3440 {
3441 if (lockmgr_cb) {
3442 if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
3443 return -1;
3444 if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
3445 return -1;
3446 }
3447
3448 lockmgr_cb = cb;
3449
3450 if (lockmgr_cb) {
3451 if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
3452 return -1;
3453 if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
3454 return -1;
3455 }
3456 return 0;
3457 }
3458
ff_lock_avcodec(AVCodecContext * log_ctx)3459 int ff_lock_avcodec(AVCodecContext *log_ctx)
3460 {
3461 if (lockmgr_cb) {
3462 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
3463 return -1;
3464 }
3465 entangled_thread_counter++;
3466 if (entangled_thread_counter != 1) {
3467 av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
3468 if (!lockmgr_cb)
3469 av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
3470 ff_avcodec_locked = 1;
3471 ff_unlock_avcodec();
3472 return AVERROR(EINVAL);
3473 }
3474 av_assert0(!ff_avcodec_locked);
3475 ff_avcodec_locked = 1;
3476 return 0;
3477 }
3478
ff_unlock_avcodec(void)3479 int ff_unlock_avcodec(void)
3480 {
3481 av_assert0(ff_avcodec_locked);
3482 ff_avcodec_locked = 0;
3483 entangled_thread_counter--;
3484 if (lockmgr_cb) {
3485 if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
3486 return -1;
3487 }
3488 return 0;
3489 }
3490
avpriv_lock_avformat(void)3491 int avpriv_lock_avformat(void)
3492 {
3493 if (lockmgr_cb) {
3494 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
3495 return -1;
3496 }
3497 return 0;
3498 }
3499
avpriv_unlock_avformat(void)3500 int avpriv_unlock_avformat(void)
3501 {
3502 if (lockmgr_cb) {
3503 if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
3504 return -1;
3505 }
3506 return 0;
3507 }
3508
avpriv_toupper4(unsigned int x)3509 unsigned int avpriv_toupper4(unsigned int x)
3510 {
3511 return av_toupper(x & 0xFF) +
3512 (av_toupper((x >> 8) & 0xFF) << 8) +
3513 (av_toupper((x >> 16) & 0xFF) << 16) +
3514 ((unsigned)av_toupper((x >> 24) & 0xFF) << 24);
3515 }
3516
ff_thread_ref_frame(ThreadFrame * dst,ThreadFrame * src)3517 int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
3518 {
3519 int ret;
3520
3521 dst->owner = src->owner;
3522
3523 ret = av_frame_ref(dst->f, src->f);
3524 if (ret < 0)
3525 return ret;
3526
3527 if (src->progress &&
3528 !(dst->progress = av_buffer_ref(src->progress))) {
3529 ff_thread_release_buffer(dst->owner, dst);
3530 return AVERROR(ENOMEM);
3531 }
3532
3533 return 0;
3534 }
3535
3536 #if !HAVE_THREADS
3537
ff_thread_get_format(AVCodecContext * avctx,const enum AVPixelFormat * fmt)3538 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
3539 {
3540 return ff_get_format(avctx, fmt);
3541 }
3542
ff_thread_get_buffer(AVCodecContext * avctx,ThreadFrame * f,int flags)3543 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
3544 {
3545 f->owner = avctx;
3546 return ff_get_buffer(avctx, f->f, flags);
3547 }
3548
ff_thread_release_buffer(AVCodecContext * avctx,ThreadFrame * f)3549 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
3550 {
3551 if (f->f)
3552 av_frame_unref(f->f);
3553 }
3554
ff_thread_finish_setup(AVCodecContext * avctx)3555 void ff_thread_finish_setup(AVCodecContext *avctx)
3556 {
3557 }
3558
ff_thread_report_progress(ThreadFrame * f,int progress,int field)3559 void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
3560 {
3561 }
3562
ff_thread_await_progress(ThreadFrame * f,int progress,int field)3563 void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
3564 {
3565 }
3566
ff_thread_can_start_frame(AVCodecContext * avctx)3567 int ff_thread_can_start_frame(AVCodecContext *avctx)
3568 {
3569 return 1;
3570 }
3571
ff_alloc_entries(AVCodecContext * avctx,int count)3572 int ff_alloc_entries(AVCodecContext *avctx, int count)
3573 {
3574 return 0;
3575 }
3576
ff_reset_entries(AVCodecContext * avctx)3577 void ff_reset_entries(AVCodecContext *avctx)
3578 {
3579 }
3580
ff_thread_await_progress2(AVCodecContext * avctx,int field,int thread,int shift)3581 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
3582 {
3583 }
3584
ff_thread_report_progress2(AVCodecContext * avctx,int field,int thread,int n)3585 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
3586 {
3587 }
3588
3589 #endif
3590
avcodec_get_type(enum AVCodecID codec_id)3591 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
3592 {
3593 AVCodec *c= avcodec_find_decoder(codec_id);
3594 if(!c)
3595 c= avcodec_find_encoder(codec_id);
3596 if(c)
3597 return c->type;
3598
3599 if (codec_id <= AV_CODEC_ID_NONE)
3600 return AVMEDIA_TYPE_UNKNOWN;
3601 else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
3602 return AVMEDIA_TYPE_VIDEO;
3603 else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
3604 return AVMEDIA_TYPE_AUDIO;
3605 else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
3606 return AVMEDIA_TYPE_SUBTITLE;
3607
3608 return AVMEDIA_TYPE_UNKNOWN;
3609 }
3610
avcodec_is_open(AVCodecContext * s)3611 int avcodec_is_open(AVCodecContext *s)
3612 {
3613 return !!s->internal;
3614 }
3615
avpriv_bprint_to_extradata(AVCodecContext * avctx,struct AVBPrint * buf)3616 int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
3617 {
3618 int ret;
3619 char *str;
3620
3621 ret = av_bprint_finalize(buf, &str);
3622 if (ret < 0)
3623 return ret;
3624 avctx->extradata = str;
3625 /* Note: the string is NUL terminated (so extradata can be read as a
3626 * string), but the ending character is not accounted in the size (in
3627 * binary formats you are likely not supposed to mux that character). When
3628 * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
3629 * zeros. */
3630 avctx->extradata_size = buf->len;
3631 return 0;
3632 }
3633
avpriv_find_start_code(const uint8_t * av_restrict p,const uint8_t * end,uint32_t * av_restrict state)3634 const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
3635 const uint8_t *end,
3636 uint32_t *av_restrict state)
3637 {
3638 int i;
3639
3640 av_assert0(p <= end);
3641 if (p >= end)
3642 return end;
3643
3644 for (i = 0; i < 3; i++) {
3645 uint32_t tmp = *state << 8;
3646 *state = tmp + *(p++);
3647 if (tmp == 0x100 || p == end)
3648 return p;
3649 }
3650
3651 while (p < end) {
3652 if (p[-1] > 1 ) p += 3;
3653 else if (p[-2] ) p += 2;
3654 else if (p[-3]|(p[-1]-1)) p++;
3655 else {
3656 p++;
3657 break;
3658 }
3659 }
3660
3661 p = FFMIN(p, end) - 4;
3662 *state = AV_RB32(p);
3663
3664 return p + 4;
3665 }
3666