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