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