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