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