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