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