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