]> git.sesse.net Git - ffmpeg/blob - libavcodec/utils.c
979b27a809832a0663543f93b6c71449d61f451d
[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 FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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/avstring.h"
29 #include "libavutil/crc.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/audioconvert.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/samplefmt.h"
35 #include "libavutil/dict.h"
36 #include "avcodec.h"
37 #include "dsputil.h"
38 #include "libavutil/opt.h"
39 #include "imgconvert.h"
40 #include "thread.h"
41 #include "audioconvert.h"
42 #include "internal.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     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
62         min_size= 0;
63
64     *size= min_size;
65
66     return ptr;
67 }
68
69 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
70 {
71     void **p = ptr;
72     if (min_size < *size)
73         return;
74     min_size= FFMAX(17*min_size/16 + 32, min_size);
75     av_free(*p);
76     *p = av_malloc(min_size);
77     if (!*p) min_size = 0;
78     *size= min_size;
79 }
80
81 /* encoder management */
82 static AVCodec *first_avcodec = NULL;
83
84 AVCodec *av_codec_next(AVCodec *c){
85     if(c) return c->next;
86     else  return first_avcodec;
87 }
88
89 #if !FF_API_AVCODEC_INIT
90 static
91 #endif
92 void avcodec_init(void)
93 {
94     static int initialized = 0;
95
96     if (initialized != 0)
97         return;
98     initialized = 1;
99
100     dsputil_static_init();
101 }
102
103 void avcodec_register(AVCodec *codec)
104 {
105     AVCodec **p;
106     avcodec_init();
107     p = &first_avcodec;
108     while (*p != NULL) p = &(*p)->next;
109     *p = codec;
110     codec->next = NULL;
111
112     if (codec->init_static_data)
113         codec->init_static_data(codec);
114 }
115
116 unsigned avcodec_get_edge_width(void)
117 {
118     return EDGE_WIDTH;
119 }
120
121 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
122     s->coded_width = width;
123     s->coded_height= height;
124     s->width = -((-width )>>s->lowres);
125     s->height= -((-height)>>s->lowres);
126 }
127
128 #define INTERNAL_BUFFER_SIZE (32+1)
129
130 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
131     int w_align= 1;
132     int h_align= 1;
133
134     switch(s->pix_fmt){
135     case PIX_FMT_YUV420P:
136     case PIX_FMT_YUYV422:
137     case PIX_FMT_UYVY422:
138     case PIX_FMT_YUV422P:
139     case PIX_FMT_YUV440P:
140     case PIX_FMT_YUV444P:
141     case PIX_FMT_GBRP:
142     case PIX_FMT_GRAY8:
143     case PIX_FMT_GRAY16BE:
144     case PIX_FMT_GRAY16LE:
145     case PIX_FMT_YUVJ420P:
146     case PIX_FMT_YUVJ422P:
147     case PIX_FMT_YUVJ440P:
148     case PIX_FMT_YUVJ444P:
149     case PIX_FMT_YUVA420P:
150     case PIX_FMT_YUV420P9LE:
151     case PIX_FMT_YUV420P9BE:
152     case PIX_FMT_YUV420P10LE:
153     case PIX_FMT_YUV420P10BE:
154     case PIX_FMT_YUV422P9LE:
155     case PIX_FMT_YUV422P9BE:
156     case PIX_FMT_YUV422P10LE:
157     case PIX_FMT_YUV422P10BE:
158     case PIX_FMT_YUV444P9LE:
159     case PIX_FMT_YUV444P9BE:
160     case PIX_FMT_YUV444P10LE:
161     case PIX_FMT_YUV444P10BE:
162     case PIX_FMT_GBRP9LE:
163     case PIX_FMT_GBRP9BE:
164     case PIX_FMT_GBRP10LE:
165     case PIX_FMT_GBRP10BE:
166         w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
167         h_align= 16;
168         if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP || s->codec_id == CODEC_ID_H264 || s->codec_id == CODEC_ID_PRORES)
169             h_align= 32; // interlaced is rounded up to 2 MBs
170         break;
171     case PIX_FMT_YUV411P:
172     case PIX_FMT_UYYVYY411:
173         w_align=32;
174         h_align=8;
175         break;
176     case PIX_FMT_YUV410P:
177         if(s->codec_id == CODEC_ID_SVQ1){
178             w_align=64;
179             h_align=64;
180         }
181     case PIX_FMT_RGB555:
182         if(s->codec_id == CODEC_ID_RPZA){
183             w_align=4;
184             h_align=4;
185         }
186     case PIX_FMT_PAL8:
187     case PIX_FMT_BGR8:
188     case PIX_FMT_RGB8:
189         if(s->codec_id == CODEC_ID_SMC){
190             w_align=4;
191             h_align=4;
192         }
193         break;
194     case PIX_FMT_BGR24:
195         if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
196             w_align=4;
197             h_align=4;
198         }
199         break;
200     default:
201         w_align= 1;
202         h_align= 1;
203         break;
204     }
205
206     *width = FFALIGN(*width , w_align);
207     *height= FFALIGN(*height, h_align);
208     if(s->codec_id == CODEC_ID_H264 || s->lowres)
209         *height+=2; // some of the optimized chroma MC reads one line too much
210                     // which is also done in mpeg decoders with lowres > 0
211
212     linesize_align[0] =
213     linesize_align[1] =
214     linesize_align[2] =
215     linesize_align[3] = STRIDE_ALIGN;
216 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
217 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
218 //picture size unneccessarily in some cases. The solution here is not
219 //pretty and better ideas are welcome!
220 #if HAVE_MMX
221     if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
222        s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
223        s->codec_id == CODEC_ID_VP6A || s->codec_id == CODEC_ID_DIRAC) {
224         linesize_align[0] =
225         linesize_align[1] =
226         linesize_align[2] = 16;
227     }
228 #endif
229 }
230
231 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
232     int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
233     int linesize_align[4];
234     int align;
235     avcodec_align_dimensions2(s, width, height, linesize_align);
236     align = FFMAX(linesize_align[0], linesize_align[3]);
237     linesize_align[1] <<= chroma_shift;
238     linesize_align[2] <<= chroma_shift;
239     align = FFMAX3(align, linesize_align[1], linesize_align[2]);
240     *width=FFALIGN(*width, align);
241 }
242
243 void ff_init_buffer_info(AVCodecContext *s, AVFrame *pic)
244 {
245     if (s->pkt) {
246         pic->pkt_pts = s->pkt->pts;
247         pic->pkt_pos = s->pkt->pos;
248     } else {
249         pic->pkt_pts = AV_NOPTS_VALUE;
250         pic->pkt_pos = -1;
251     }
252     pic->reordered_opaque= s->reordered_opaque;
253     pic->sample_aspect_ratio = s->sample_aspect_ratio;
254     pic->width               = s->width;
255     pic->height              = s->height;
256     pic->format              = s->pix_fmt;
257 }
258
259 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
260     int i;
261     int w= s->width;
262     int h= s->height;
263     InternalBuffer *buf;
264     int *picture_number;
265     AVCodecInternal *avci = s->internal;
266
267     if(pic->data[0]!=NULL) {
268         av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
269         return -1;
270     }
271     if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
272         av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
273         return -1;
274     }
275
276     if(av_image_check_size(w, h, 0, s))
277         return -1;
278
279     if (!avci->buffer) {
280         avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
281                                   sizeof(InternalBuffer));
282     }
283
284     buf = &avci->buffer[avci->buffer_count];
285     picture_number = &(avci->buffer[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
286     (*picture_number)++;
287
288     if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
289         if(s->active_thread_type&FF_THREAD_FRAME) {
290             av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
291             return -1;
292         }
293
294         for(i=0; i<4; i++){
295             av_freep(&buf->base[i]);
296             buf->data[i]= NULL;
297         }
298     }
299
300     if(buf->base[0]){
301         pic->age= *picture_number - buf->last_pic_num;
302         buf->last_pic_num= *picture_number;
303     }else{
304         int h_chroma_shift, v_chroma_shift;
305         int size[4] = {0};
306         int tmpsize;
307         int unaligned;
308         AVPicture picture;
309         int stride_align[4];
310         const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
311
312         avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
313
314         avcodec_align_dimensions2(s, &w, &h, stride_align);
315
316         if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
317             w+= EDGE_WIDTH*2;
318             h+= EDGE_WIDTH*2;
319         }
320
321         do {
322             // NOTE: do not align linesizes individually, this breaks e.g. assumptions
323             // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
324             av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
325             // increase alignment of w for next try (rhs gives the lowest bit set in w)
326             w += w & ~(w-1);
327
328             unaligned = 0;
329             for (i=0; i<4; i++){
330                 unaligned |= picture.linesize[i] % stride_align[i];
331             }
332         } while (unaligned);
333
334         tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
335         if (tmpsize < 0)
336             return -1;
337
338         for (i=0; i<3 && picture.data[i+1]; i++)
339             size[i] = picture.data[i+1] - picture.data[i];
340         size[i] = tmpsize - (picture.data[i] - picture.data[0]);
341
342         buf->last_pic_num= -256*256*256*64;
343         memset(buf->base, 0, sizeof(buf->base));
344         memset(buf->data, 0, sizeof(buf->data));
345
346         for(i=0; i<4 && size[i]; i++){
347             const int h_shift= i==0 ? 0 : h_chroma_shift;
348             const int v_shift= i==0 ? 0 : v_chroma_shift;
349
350             buf->linesize[i]= picture.linesize[i];
351
352             buf->base[i]= av_malloc(size[i]+16); //FIXME 16
353             if(buf->base[i]==NULL) return -1;
354             memset(buf->base[i], 128, size[i]);
355
356             // no edge if EDGE EMU or not planar YUV
357             if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
358                 buf->data[i] = buf->base[i];
359             else
360                 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
361         }
362         if(size[1] && !size[2])
363             ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
364         buf->width  = s->width;
365         buf->height = s->height;
366         buf->pix_fmt= s->pix_fmt;
367         pic->age= 256*256*256*64;
368     }
369     pic->type= FF_BUFFER_TYPE_INTERNAL;
370
371     for(i=0; i<4; i++){
372         pic->base[i]= buf->base[i];
373         pic->data[i]= buf->data[i];
374         pic->linesize[i]= buf->linesize[i];
375     }
376     avci->buffer_count++;
377
378     if (s->pkt) {
379         pic->pkt_pts = s->pkt->pts;
380         pic->pkt_pos = s->pkt->pos;
381     } else {
382         pic->pkt_pts = AV_NOPTS_VALUE;
383         pic->pkt_pos = -1;
384     }
385     pic->reordered_opaque= s->reordered_opaque;
386     pic->sample_aspect_ratio = s->sample_aspect_ratio;
387     pic->width               = s->width;
388     pic->height              = s->height;
389     pic->format              = s->pix_fmt;
390
391     if(s->debug&FF_DEBUG_BUFFERS)
392         av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
393                "buffers used\n", pic, avci->buffer_count);
394
395     return 0;
396 }
397
398 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
399     int i;
400     InternalBuffer *buf, *last;
401     AVCodecInternal *avci = s->internal;
402
403     assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
404     assert(avci->buffer_count);
405
406     if (avci->buffer) {
407         buf = NULL; /* avoids warning */
408         for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
409             buf = &avci->buffer[i];
410             if (buf->data[0] == pic->data[0])
411                 break;
412         }
413         assert(i < avci->buffer_count);
414         avci->buffer_count--;
415         last = &avci->buffer[avci->buffer_count];
416
417         FFSWAP(InternalBuffer, *buf, *last);
418     }
419
420     for(i=0; i<4; i++){
421         pic->data[i]=NULL;
422 //        pic->base[i]=NULL;
423     }
424 //printf("R%X\n", pic->opaque);
425
426     if(s->debug&FF_DEBUG_BUFFERS)
427         av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
428                "buffers used\n", pic, avci->buffer_count);
429 }
430
431 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
432     AVFrame temp_pic;
433     int i;
434
435     /* If no picture return a new buffer */
436     if(pic->data[0] == NULL) {
437         /* We will copy from buffer, so must be readable */
438         pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
439         return s->get_buffer(s, pic);
440     }
441
442     /* If internal buffer type return the same buffer */
443     if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
444         if(s->pkt) pic->pkt_pts= s->pkt->pts;
445         else       pic->pkt_pts= AV_NOPTS_VALUE;
446         pic->reordered_opaque= s->reordered_opaque;
447         return 0;
448     }
449
450     /*
451      * Not internal type and reget_buffer not overridden, emulate cr buffer
452      */
453     temp_pic = *pic;
454     for(i = 0; i < 4; i++)
455         pic->data[i] = pic->base[i] = NULL;
456     pic->opaque = NULL;
457     /* Allocate new frame */
458     if (s->get_buffer(s, pic))
459         return -1;
460     /* Copy image data from old buffer to new buffer */
461     av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
462              s->height);
463     s->release_buffer(s, &temp_pic); // Release old frame
464     return 0;
465 }
466
467 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
468     int i;
469
470     for(i=0; i<count; i++){
471         int r= func(c, (char*)arg + i*size);
472         if(ret) ret[i]= r;
473     }
474     return 0;
475 }
476
477 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
478     int i;
479
480     for(i=0; i<count; i++){
481         int r= func(c, arg, i, 0);
482         if(ret) ret[i]= r;
483     }
484     return 0;
485 }
486
487 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
488     while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
489         ++fmt;
490     return fmt[0];
491 }
492
493 void avcodec_get_frame_defaults(AVFrame *pic){
494     memset(pic, 0, sizeof(AVFrame));
495
496     pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
497     pic->pkt_pos = -1;
498     pic->key_frame= 1;
499     pic->sample_aspect_ratio = (AVRational){0, 1};
500     pic->format = -1;           /* unknown */
501 }
502
503 AVFrame *avcodec_alloc_frame(void){
504     AVFrame *pic= av_malloc(sizeof(AVFrame));
505
506     if(pic==NULL) return NULL;
507
508     avcodec_get_frame_defaults(pic);
509
510     return pic;
511 }
512
513 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
514 {
515     memset(sub, 0, sizeof(*sub));
516     sub->pts = AV_NOPTS_VALUE;
517 }
518
519 #if FF_API_AVCODEC_OPEN
520 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
521 {
522     return avcodec_open2(avctx, codec, NULL);
523 }
524 #endif
525
526 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
527 {
528     int ret = 0;
529     AVDictionary *tmp = NULL;
530
531     if (options)
532         av_dict_copy(&tmp, *options, 0);
533
534     /* If there is a user-supplied mutex locking routine, call it. */
535     if (ff_lockmgr_cb) {
536         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
537             return -1;
538     }
539
540     entangled_thread_counter++;
541     if(entangled_thread_counter != 1){
542         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
543         ret = -1;
544         goto end;
545     }
546
547     if(avctx->codec || !codec) {
548         ret = AVERROR(EINVAL);
549         goto end;
550     }
551
552     avctx->internal = av_mallocz(sizeof(AVCodecInternal));
553     if (!avctx->internal) {
554         ret = AVERROR(ENOMEM);
555         goto end;
556     }
557
558     if (codec->priv_data_size > 0) {
559       if(!avctx->priv_data){
560         avctx->priv_data = av_mallocz(codec->priv_data_size);
561         if (!avctx->priv_data) {
562             ret = AVERROR(ENOMEM);
563             goto end;
564         }
565         if (codec->priv_class) {
566             *(AVClass**)avctx->priv_data= codec->priv_class;
567             av_opt_set_defaults(avctx->priv_data);
568         }
569       }
570       if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
571           goto free_and_end;
572     } else {
573         avctx->priv_data = NULL;
574     }
575     if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
576         goto free_and_end;
577
578     //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
579     if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == CODEC_ID_H264)){
580     if(avctx->coded_width && avctx->coded_height)
581         avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
582     else if(avctx->width && avctx->height)
583         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
584     }
585
586     if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
587         && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
588            || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
589         av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
590         avcodec_set_dimensions(avctx, 0, 0);
591     }
592
593     /* if the decoder init function was already called previously,
594        free the already allocated subtitle_header before overwriting it */
595     if (codec->decode)
596         av_freep(&avctx->subtitle_header);
597
598 #define SANE_NB_CHANNELS 128U
599     if (avctx->channels > SANE_NB_CHANNELS) {
600         ret = AVERROR(EINVAL);
601         goto free_and_end;
602     }
603
604     avctx->codec = codec;
605     if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
606         avctx->codec_id == CODEC_ID_NONE) {
607         avctx->codec_type = codec->type;
608         avctx->codec_id   = codec->id;
609     }
610     if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
611                            && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
612         av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
613         ret = AVERROR(EINVAL);
614         goto free_and_end;
615     }
616     avctx->frame_number = 0;
617 #if FF_API_ER
618
619     av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
620            avctx->error_recognition, avctx->err_recognition);
621     switch(avctx->error_recognition){
622         case FF_ER_EXPLODE        : avctx->err_recognition |= AV_EF_EXPLODE | AV_EF_COMPLIANT | AV_EF_CAREFUL;
623             break;
624         case FF_ER_VERY_AGGRESSIVE:
625         case FF_ER_AGGRESSIVE     : avctx->err_recognition |= AV_EF_AGGRESSIVE;
626         case FF_ER_COMPLIANT      : avctx->err_recognition |= AV_EF_COMPLIANT;
627         case FF_ER_CAREFUL        : avctx->err_recognition |= AV_EF_CAREFUL;
628     }
629
630     av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
631            avctx->error_recognition, avctx->err_recognition);
632 #endif
633
634     if (!HAVE_THREADS)
635         av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
636
637     if (HAVE_THREADS && !avctx->thread_opaque) {
638         ret = ff_thread_init(avctx);
639         if (ret < 0) {
640             goto free_and_end;
641         }
642     }
643
644     if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
645         av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
646                avctx->codec->max_lowres);
647         ret = AVERROR(EINVAL);
648         goto free_and_end;
649     }
650     if (avctx->codec->encode) {
651         int i;
652         if (avctx->codec->sample_fmts) {
653             for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
654                 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
655                     break;
656             if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
657                 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
658                 ret = AVERROR(EINVAL);
659                 goto free_and_end;
660             }
661         }
662         if (avctx->codec->supported_samplerates) {
663             for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
664                 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
665                     break;
666             if (avctx->codec->supported_samplerates[i] == 0) {
667                 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
668                 ret = AVERROR(EINVAL);
669                 goto free_and_end;
670             }
671         }
672         if (avctx->codec->channel_layouts) {
673             if (!avctx->channel_layout) {
674                 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
675             } else {
676                 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
677                     if (avctx->channel_layout == avctx->codec->channel_layouts[i])
678                         break;
679                 if (avctx->codec->channel_layouts[i] == 0) {
680                     av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
681                     ret = AVERROR(EINVAL);
682                     goto free_and_end;
683                 }
684             }
685         }
686         if (avctx->channel_layout && avctx->channels) {
687             if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
688                 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
689                 ret = AVERROR(EINVAL);
690                 goto free_and_end;
691             }
692         } else if (avctx->channel_layout) {
693             avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
694         }
695     }
696
697     avctx->pts_correction_num_faulty_pts =
698     avctx->pts_correction_num_faulty_dts = 0;
699     avctx->pts_correction_last_pts =
700     avctx->pts_correction_last_dts = INT64_MIN;
701
702     if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
703         ret = avctx->codec->init(avctx);
704         if (ret < 0) {
705             goto free_and_end;
706         }
707     }
708
709     ret=0;
710 end:
711     entangled_thread_counter--;
712
713     /* Release any user-supplied mutex. */
714     if (ff_lockmgr_cb) {
715         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
716     }
717     if (options) {
718         av_dict_free(options);
719         *options = tmp;
720     }
721
722     return ret;
723 free_and_end:
724     av_dict_free(&tmp);
725     av_freep(&avctx->priv_data);
726     av_freep(&avctx->internal);
727     avctx->codec= NULL;
728     goto end;
729 }
730
731 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
732                          const short *samples)
733 {
734     if(buf_size < FF_MIN_BUFFER_SIZE && 0){
735         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
736         return -1;
737     }
738     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
739         int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
740         avctx->frame_number++;
741         return ret;
742     }else
743         return 0;
744 }
745
746 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
747                          const AVFrame *pict)
748 {
749     if(buf_size < FF_MIN_BUFFER_SIZE){
750         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
751         return -1;
752     }
753     if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
754         return -1;
755     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
756         int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
757         avctx->frame_number++;
758         emms_c(); //needed to avoid an emms_c() call before every return;
759
760         return ret;
761     }else
762         return 0;
763 }
764
765 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
766                             const AVSubtitle *sub)
767 {
768     int ret;
769     if(sub->start_display_time) {
770         av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
771         return -1;
772     }
773
774     ret = avctx->codec->encode(avctx, buf, buf_size, sub);
775     avctx->frame_number++;
776     return ret;
777 }
778
779 /**
780  * Attempt to guess proper monotonic timestamps for decoded video frames
781  * which might have incorrect times. Input timestamps may wrap around, in
782  * which case the output will as well.
783  *
784  * @param pts the pts field of the decoded AVPacket, as passed through
785  * AVFrame.pkt_pts
786  * @param dts the dts field of the decoded AVPacket
787  * @return one of the input values, may be AV_NOPTS_VALUE
788  */
789 static int64_t guess_correct_pts(AVCodecContext *ctx,
790                                  int64_t reordered_pts, int64_t dts)
791 {
792     int64_t pts = AV_NOPTS_VALUE;
793
794     if (dts != AV_NOPTS_VALUE) {
795         ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
796         ctx->pts_correction_last_dts = dts;
797     }
798     if (reordered_pts != AV_NOPTS_VALUE) {
799         ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
800         ctx->pts_correction_last_pts = reordered_pts;
801     }
802     if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
803        && reordered_pts != AV_NOPTS_VALUE)
804         pts = reordered_pts;
805     else
806         pts = dts;
807
808     return pts;
809 }
810
811 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
812                          int *got_picture_ptr,
813                          AVPacket *avpkt)
814 {
815     int ret;
816
817     *got_picture_ptr= 0;
818     if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
819         return -1;
820
821     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
822         av_packet_split_side_data(avpkt);
823         avctx->pkt = avpkt;
824         if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
825              ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
826                                           avpkt);
827         else {
828             ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
829                               avpkt);
830             picture->pkt_dts= avpkt->dts;
831
832             if(!avctx->has_b_frames){
833             picture->pkt_pos= avpkt->pos;
834             }
835             //FIXME these should be under if(!avctx->has_b_frames)
836             if (!picture->sample_aspect_ratio.num)
837                 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
838             if (!picture->width)
839                 picture->width = avctx->width;
840             if (!picture->height)
841                 picture->height = avctx->height;
842             if (picture->format == PIX_FMT_NONE)
843                 picture->format = avctx->pix_fmt;
844         }
845
846         emms_c(); //needed to avoid an emms_c() call before every return;
847
848
849         if (*got_picture_ptr){
850             avctx->frame_number++;
851             picture->best_effort_timestamp = guess_correct_pts(avctx,
852                                                             picture->pkt_pts,
853                                                             picture->pkt_dts);
854         }
855     }else
856         ret= 0;
857
858     return ret;
859 }
860
861 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
862                          int *frame_size_ptr,
863                          AVPacket *avpkt)
864 {
865     int ret;
866
867     avctx->pkt = avpkt;
868
869     if (!avpkt->data && avpkt->size) {
870         av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
871         return AVERROR(EINVAL);
872     }
873
874     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
875         //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
876         if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
877             av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
878             return -1;
879         }
880         if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
881         *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
882             av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
883             return -1;
884         }
885
886         ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
887         avctx->frame_number++;
888     }else{
889         ret= 0;
890         *frame_size_ptr=0;
891     }
892     return ret;
893 }
894
895 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
896                             int *got_sub_ptr,
897                             AVPacket *avpkt)
898 {
899     int ret;
900
901     avctx->pkt = avpkt;
902     *got_sub_ptr = 0;
903     avcodec_get_subtitle_defaults(sub);
904     ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
905     if (*got_sub_ptr)
906         avctx->frame_number++;
907     return ret;
908 }
909
910 void avsubtitle_free(AVSubtitle *sub)
911 {
912     int i;
913
914     for (i = 0; i < sub->num_rects; i++)
915     {
916         av_freep(&sub->rects[i]->pict.data[0]);
917         av_freep(&sub->rects[i]->pict.data[1]);
918         av_freep(&sub->rects[i]->pict.data[2]);
919         av_freep(&sub->rects[i]->pict.data[3]);
920         av_freep(&sub->rects[i]->text);
921         av_freep(&sub->rects[i]->ass);
922         av_freep(&sub->rects[i]);
923     }
924
925     av_freep(&sub->rects);
926
927     memset(sub, 0, sizeof(AVSubtitle));
928 }
929
930 av_cold int avcodec_close(AVCodecContext *avctx)
931 {
932     /* If there is a user-supplied mutex locking routine, call it. */
933     if (ff_lockmgr_cb) {
934         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
935             return -1;
936     }
937
938     entangled_thread_counter++;
939     if(entangled_thread_counter != 1){
940         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
941         entangled_thread_counter--;
942         return -1;
943     }
944
945     if (HAVE_THREADS && avctx->thread_opaque)
946         ff_thread_free(avctx);
947     if (avctx->codec && avctx->codec->close)
948         avctx->codec->close(avctx);
949     avcodec_default_free_buffers(avctx);
950     avctx->coded_frame = NULL;
951     av_freep(&avctx->internal);
952     if (avctx->codec && avctx->codec->priv_class)
953         av_opt_free(avctx->priv_data);
954     av_opt_free(avctx);
955     av_freep(&avctx->priv_data);
956     if(avctx->codec && avctx->codec->encode)
957         av_freep(&avctx->extradata);
958     avctx->codec = NULL;
959     avctx->active_thread_type = 0;
960     entangled_thread_counter--;
961
962     /* Release any user-supplied mutex. */
963     if (ff_lockmgr_cb) {
964         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
965     }
966     return 0;
967 }
968
969 static enum CodecID remap_deprecated_codec_id(enum CodecID id)
970 {
971     switch(id){
972         case CODEC_ID_G723_1_DEPRECATED : return CODEC_ID_G723_1;
973         case CODEC_ID_G729_DEPRECATED   : return CODEC_ID_G729;
974         case CODEC_ID_UTVIDEO_DEPRECATED: return CODEC_ID_UTVIDEO;
975         default                         : return id;
976     }
977 }
978
979 AVCodec *avcodec_find_encoder(enum CodecID id)
980 {
981     AVCodec *p, *experimental=NULL;
982     p = first_avcodec;
983     id= remap_deprecated_codec_id(id);
984     while (p) {
985         if (p->encode != NULL && p->id == id) {
986             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
987                 experimental = p;
988             } else
989                 return p;
990         }
991         p = p->next;
992     }
993     return experimental;
994 }
995
996 AVCodec *avcodec_find_encoder_by_name(const char *name)
997 {
998     AVCodec *p;
999     if (!name)
1000         return NULL;
1001     p = first_avcodec;
1002     while (p) {
1003         if (p->encode != NULL && strcmp(name,p->name) == 0)
1004             return p;
1005         p = p->next;
1006     }
1007     return NULL;
1008 }
1009
1010 AVCodec *avcodec_find_decoder(enum CodecID id)
1011 {
1012     AVCodec *p, *experimental=NULL;
1013     p = first_avcodec;
1014     id= remap_deprecated_codec_id(id);
1015     while (p) {
1016         if (p->decode != NULL && p->id == id) {
1017             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1018                 experimental = p;
1019             } else
1020                 return p;
1021         }
1022         p = p->next;
1023     }
1024     return experimental;
1025 }
1026
1027 AVCodec *avcodec_find_decoder_by_name(const char *name)
1028 {
1029     AVCodec *p;
1030     if (!name)
1031         return NULL;
1032     p = first_avcodec;
1033     while (p) {
1034         if (p->decode != NULL && strcmp(name,p->name) == 0)
1035             return p;
1036         p = p->next;
1037     }
1038     return NULL;
1039 }
1040
1041 static int get_bit_rate(AVCodecContext *ctx)
1042 {
1043     int bit_rate;
1044     int bits_per_sample;
1045
1046     switch(ctx->codec_type) {
1047     case AVMEDIA_TYPE_VIDEO:
1048     case AVMEDIA_TYPE_DATA:
1049     case AVMEDIA_TYPE_SUBTITLE:
1050     case AVMEDIA_TYPE_ATTACHMENT:
1051         bit_rate = ctx->bit_rate;
1052         break;
1053     case AVMEDIA_TYPE_AUDIO:
1054         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1055         bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1056         break;
1057     default:
1058         bit_rate = 0;
1059         break;
1060     }
1061     return bit_rate;
1062 }
1063
1064 const char *avcodec_get_name(enum CodecID id)
1065 {
1066     AVCodec *codec;
1067
1068 #if !CONFIG_SMALL
1069     switch (id) {
1070 #include "libavcodec/codec_names.h"
1071     }
1072     av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
1073 #endif
1074     codec = avcodec_find_decoder(id);
1075     if (codec)
1076         return codec->name;
1077     codec = avcodec_find_encoder(id);
1078     if (codec)
1079         return codec->name;
1080     return "unknown_codec";
1081 }
1082
1083 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1084 {
1085     int i, len, ret = 0;
1086
1087     for (i = 0; i < 4; i++) {
1088         len = snprintf(buf, buf_size,
1089                        isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1090         buf      += len;
1091         buf_size  = buf_size > len ? buf_size - len : 0;
1092         ret      += len;
1093         codec_tag>>=8;
1094     }
1095     return ret;
1096 }
1097
1098 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1099 {
1100     const char *codec_type;
1101     const char *codec_name;
1102     const char *profile = NULL;
1103     AVCodec *p;
1104     int bitrate;
1105     AVRational display_aspect_ratio;
1106
1107     if (!buf || buf_size <= 0)
1108         return;
1109     codec_type = av_get_media_type_string(enc->codec_type);
1110     codec_name = avcodec_get_name(enc->codec_id);
1111     if (enc->profile != FF_PROFILE_UNKNOWN) {
1112         p = encode ? avcodec_find_encoder(enc->codec_id) :
1113                      avcodec_find_decoder(enc->codec_id);
1114         if (p)
1115             profile = av_get_profile_name(p, enc->profile);
1116     }
1117
1118     snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
1119              codec_name, enc->mb_decision ? " (hq)" : "");
1120     buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
1121     if (profile)
1122         snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
1123     if (enc->codec_tag) {
1124         char tag_buf[32];
1125         av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1126         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1127                  " (%s / 0x%04X)", tag_buf, enc->codec_tag);
1128     }
1129     switch(enc->codec_type) {
1130     case AVMEDIA_TYPE_VIDEO:
1131         if (enc->pix_fmt != PIX_FMT_NONE) {
1132             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1133                      ", %s",
1134                      av_get_pix_fmt_name(enc->pix_fmt));
1135         }
1136         if (enc->width) {
1137             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1138                      ", %dx%d",
1139                      enc->width, enc->height);
1140             if (enc->sample_aspect_ratio.num) {
1141                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1142                           enc->width*enc->sample_aspect_ratio.num,
1143                           enc->height*enc->sample_aspect_ratio.den,
1144                           1024*1024);
1145                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1146                          " [SAR %d:%d DAR %d:%d]",
1147                          enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1148                          display_aspect_ratio.num, display_aspect_ratio.den);
1149             }
1150             if(av_log_get_level() >= AV_LOG_DEBUG){
1151                 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1152                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1153                      ", %d/%d",
1154                      enc->time_base.num/g, enc->time_base.den/g);
1155             }
1156         }
1157         if (encode) {
1158             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1159                      ", q=%d-%d", enc->qmin, enc->qmax);
1160         }
1161         break;
1162     case AVMEDIA_TYPE_AUDIO:
1163         if (enc->sample_rate) {
1164             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1165                      ", %d Hz", enc->sample_rate);
1166         }
1167         av_strlcat(buf, ", ", buf_size);
1168         av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1169         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1170             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1171                      ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1172         }
1173         break;
1174     default:
1175         return;
1176     }
1177     if (encode) {
1178         if (enc->flags & CODEC_FLAG_PASS1)
1179             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1180                      ", pass 1");
1181         if (enc->flags & CODEC_FLAG_PASS2)
1182             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1183                      ", pass 2");
1184     }
1185     bitrate = get_bit_rate(enc);
1186     if (bitrate != 0) {
1187         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1188                  ", %d kb/s", bitrate / 1000);
1189     }
1190 }
1191
1192 const char *av_get_profile_name(const AVCodec *codec, int profile)
1193 {
1194     const AVProfile *p;
1195     if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1196         return NULL;
1197
1198     for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1199         if (p->profile == profile)
1200             return p->name;
1201
1202     return NULL;
1203 }
1204
1205 unsigned avcodec_version( void )
1206 {
1207   return LIBAVCODEC_VERSION_INT;
1208 }
1209
1210 const char *avcodec_configuration(void)
1211 {
1212     return FFMPEG_CONFIGURATION;
1213 }
1214
1215 const char *avcodec_license(void)
1216 {
1217 #define LICENSE_PREFIX "libavcodec license: "
1218     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1219 }
1220
1221 void avcodec_flush_buffers(AVCodecContext *avctx)
1222 {
1223     if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1224         ff_thread_flush(avctx);
1225     else if(avctx->codec->flush)
1226         avctx->codec->flush(avctx);
1227 }
1228
1229 void avcodec_default_free_buffers(AVCodecContext *s){
1230     AVCodecInternal *avci = s->internal;
1231     int i, j;
1232
1233     if (!avci->buffer)
1234         return;
1235
1236     if (avci->buffer_count)
1237         av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1238                avci->buffer_count);
1239     for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1240         InternalBuffer *buf = &avci->buffer[i];
1241         for(j=0; j<4; j++){
1242             av_freep(&buf->base[j]);
1243             buf->data[j]= NULL;
1244         }
1245     }
1246     av_freep(&avci->buffer);
1247
1248     avci->buffer_count=0;
1249 }
1250
1251 #if FF_API_OLD_FF_PICT_TYPES
1252 char av_get_pict_type_char(int pict_type){
1253     return av_get_picture_type_char(pict_type);
1254 }
1255 #endif
1256
1257 int av_get_bits_per_sample(enum CodecID codec_id){
1258     switch(codec_id){
1259     case CODEC_ID_ADPCM_SBPRO_2:
1260         return 2;
1261     case CODEC_ID_ADPCM_SBPRO_3:
1262         return 3;
1263     case CODEC_ID_ADPCM_SBPRO_4:
1264     case CODEC_ID_ADPCM_CT:
1265     case CODEC_ID_ADPCM_IMA_WAV:
1266     case CODEC_ID_ADPCM_IMA_QT:
1267     case CODEC_ID_ADPCM_SWF:
1268     case CODEC_ID_ADPCM_MS:
1269     case CODEC_ID_ADPCM_YAMAHA:
1270         return 4;
1271     case CODEC_ID_ADPCM_G722:
1272     case CODEC_ID_PCM_ALAW:
1273     case CODEC_ID_PCM_MULAW:
1274     case CODEC_ID_PCM_S8:
1275     case CODEC_ID_PCM_U8:
1276     case CODEC_ID_PCM_ZORK:
1277         return 8;
1278     case CODEC_ID_PCM_S16BE:
1279     case CODEC_ID_PCM_S16LE:
1280     case CODEC_ID_PCM_S16LE_PLANAR:
1281     case CODEC_ID_PCM_U16BE:
1282     case CODEC_ID_PCM_U16LE:
1283         return 16;
1284     case CODEC_ID_PCM_S24DAUD:
1285     case CODEC_ID_PCM_S24BE:
1286     case CODEC_ID_PCM_S24LE:
1287     case CODEC_ID_PCM_U24BE:
1288     case CODEC_ID_PCM_U24LE:
1289         return 24;
1290     case CODEC_ID_PCM_S32BE:
1291     case CODEC_ID_PCM_S32LE:
1292     case CODEC_ID_PCM_U32BE:
1293     case CODEC_ID_PCM_U32LE:
1294     case CODEC_ID_PCM_F32BE:
1295     case CODEC_ID_PCM_F32LE:
1296         return 32;
1297     case CODEC_ID_PCM_F64BE:
1298     case CODEC_ID_PCM_F64LE:
1299         return 64;
1300     default:
1301         return 0;
1302     }
1303 }
1304
1305 #if FF_API_OLD_SAMPLE_FMT
1306 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1307     return av_get_bytes_per_sample(sample_fmt) << 3;
1308 }
1309 #endif
1310
1311 #if !HAVE_THREADS
1312 int ff_thread_init(AVCodecContext *s){
1313     return -1;
1314 }
1315 #endif
1316
1317 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1318 {
1319     unsigned int n = 0;
1320
1321     while(v >= 0xff) {
1322         *s++ = 0xff;
1323         v -= 0xff;
1324         n++;
1325     }
1326     *s = v;
1327     n++;
1328     return n;
1329 }
1330
1331 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1332     int i;
1333     for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1334     return i;
1335 }
1336
1337 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1338 {
1339     av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
1340             "version to the newest one from Git. If the problem still "
1341             "occurs, it means that your file has a feature which has not "
1342             "been implemented.\n", feature);
1343     if(want_sample)
1344         av_log_ask_for_sample(avc, NULL);
1345 }
1346
1347 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1348 {
1349     va_list argument_list;
1350
1351     va_start(argument_list, msg);
1352
1353     if (msg)
1354         av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1355     av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1356             "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
1357             "and contact the ffmpeg-devel mailing list.\n");
1358
1359     va_end(argument_list);
1360 }
1361
1362 static AVHWAccel *first_hwaccel = NULL;
1363
1364 void av_register_hwaccel(AVHWAccel *hwaccel)
1365 {
1366     AVHWAccel **p = &first_hwaccel;
1367     while (*p)
1368         p = &(*p)->next;
1369     *p = hwaccel;
1370     hwaccel->next = NULL;
1371 }
1372
1373 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1374 {
1375     return hwaccel ? hwaccel->next : first_hwaccel;
1376 }
1377
1378 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1379 {
1380     AVHWAccel *hwaccel=NULL;
1381
1382     while((hwaccel= av_hwaccel_next(hwaccel))){
1383         if (   hwaccel->id      == codec_id
1384             && hwaccel->pix_fmt == pix_fmt)
1385             return hwaccel;
1386     }
1387     return NULL;
1388 }
1389
1390 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1391 {
1392     if (ff_lockmgr_cb) {
1393         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1394             return -1;
1395         if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1396             return -1;
1397     }
1398
1399     ff_lockmgr_cb = cb;
1400
1401     if (ff_lockmgr_cb) {
1402         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1403             return -1;
1404         if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1405             return -1;
1406     }
1407     return 0;
1408 }
1409
1410 int avpriv_lock_avformat(void)
1411 {
1412     if (ff_lockmgr_cb) {
1413         if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1414             return -1;
1415     }
1416     return 0;
1417 }
1418
1419 int avpriv_unlock_avformat(void)
1420 {
1421     if (ff_lockmgr_cb) {
1422         if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1423             return -1;
1424     }
1425     return 0;
1426 }
1427
1428 unsigned int avpriv_toupper4(unsigned int x)
1429 {
1430     return     toupper( x     &0xFF)
1431             + (toupper((x>>8 )&0xFF)<<8 )
1432             + (toupper((x>>16)&0xFF)<<16)
1433             + (toupper((x>>24)&0xFF)<<24);
1434 }
1435
1436 #if !HAVE_THREADS
1437
1438 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1439 {
1440     f->owner = avctx;
1441
1442     ff_init_buffer_info(avctx, f);
1443
1444     return avctx->get_buffer(avctx, f);
1445 }
1446
1447 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1448 {
1449     f->owner->release_buffer(f->owner, f);
1450 }
1451
1452 void ff_thread_finish_setup(AVCodecContext *avctx)
1453 {
1454 }
1455
1456 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1457 {
1458 }
1459
1460 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1461 {
1462 }
1463
1464 #endif
1465
1466 #if FF_API_THREAD_INIT
1467 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1468 {
1469     s->thread_count = thread_count;
1470     return ff_thread_init(s);
1471 }
1472 #endif
1473
1474 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1475 {
1476     AVCodec *c= avcodec_find_decoder(codec_id);
1477     if(!c)
1478         c= avcodec_find_encoder(codec_id);
1479     if(c)
1480         return c->type;
1481
1482     if (codec_id <= CODEC_ID_NONE)
1483         return AVMEDIA_TYPE_UNKNOWN;
1484     else if (codec_id < CODEC_ID_FIRST_AUDIO)
1485         return AVMEDIA_TYPE_VIDEO;
1486     else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1487         return AVMEDIA_TYPE_AUDIO;
1488     else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1489         return AVMEDIA_TYPE_SUBTITLE;
1490
1491     return AVMEDIA_TYPE_UNKNOWN;
1492 }