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