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