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