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