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