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