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