]> git.sesse.net Git - ffmpeg/blob - libavcodec/utils.c
Indentation fix after gain codes decoding2
[ffmpeg] / libavcodec / utils.c
1 /*
2  * utils for libavcodec
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * utils.
26  */
27
28 #include "libavutil/avstring.h"
29 #include "libavutil/crc.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/audioconvert.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/samplefmt.h"
35 #include "libavutil/dict.h"
36 #include "avcodec.h"
37 #include "dsputil.h"
38 #include "libavutil/opt.h"
39 #include "imgconvert.h"
40 #include "thread.h"
41 #include "audioconvert.h"
42 #include "internal.h"
43 #include <stdlib.h>
44 #include <stdarg.h>
45 #include <limits.h>
46 #include <float.h>
47
48 static int volatile entangled_thread_counter=0;
49 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
50 static void *codec_mutex;
51
52 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
53 {
54     if(min_size < *size)
55         return ptr;
56
57     min_size= FFMAX(17*min_size/16 + 32, min_size);
58
59     ptr= av_realloc(ptr, min_size);
60     if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
61         min_size= 0;
62
63     *size= min_size;
64
65     return ptr;
66 }
67
68 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
69 {
70     void **p = ptr;
71     if (min_size < *size)
72         return;
73     min_size= FFMAX(17*min_size/16 + 32, min_size);
74     av_free(*p);
75     *p = av_malloc(min_size);
76     if (!*p) min_size = 0;
77     *size= min_size;
78 }
79
80 /* encoder management */
81 static AVCodec *first_avcodec = NULL;
82
83 AVCodec *av_codec_next(AVCodec *c){
84     if(c) return c->next;
85     else  return first_avcodec;
86 }
87
88 #if !FF_API_AVCODEC_INIT
89 static
90 #endif
91 void avcodec_init(void)
92 {
93     static int initialized = 0;
94
95     if (initialized != 0)
96         return;
97     initialized = 1;
98
99     dsputil_static_init();
100 }
101
102 void avcodec_register(AVCodec *codec)
103 {
104     AVCodec **p;
105     avcodec_init();
106     p = &first_avcodec;
107     while (*p != NULL) p = &(*p)->next;
108     *p = codec;
109     codec->next = NULL;
110 }
111
112 unsigned avcodec_get_edge_width(void)
113 {
114     return EDGE_WIDTH;
115 }
116
117 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
118     s->coded_width = width;
119     s->coded_height= height;
120     s->width = -((-width )>>s->lowres);
121     s->height= -((-height)>>s->lowres);
122 }
123
124 typedef struct InternalBuffer{
125     int last_pic_num;
126     uint8_t *base[4];
127     uint8_t *data[4];
128     int linesize[4];
129     int width, height;
130     enum PixelFormat pix_fmt;
131 }InternalBuffer;
132
133 #define INTERNAL_BUFFER_SIZE (32+1)
134
135 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
136     int w_align= 1;
137     int h_align= 1;
138
139     switch(s->pix_fmt){
140     case PIX_FMT_YUV420P:
141     case PIX_FMT_YUYV422:
142     case PIX_FMT_UYVY422:
143     case PIX_FMT_YUV422P:
144     case PIX_FMT_YUV440P:
145     case PIX_FMT_YUV444P:
146     case PIX_FMT_GRAY8:
147     case PIX_FMT_GRAY16BE:
148     case PIX_FMT_GRAY16LE:
149     case PIX_FMT_YUVJ420P:
150     case PIX_FMT_YUVJ422P:
151     case PIX_FMT_YUVJ440P:
152     case PIX_FMT_YUVJ444P:
153     case PIX_FMT_YUVA420P:
154     case PIX_FMT_YUV420P9LE:
155     case PIX_FMT_YUV420P9BE:
156     case PIX_FMT_YUV420P10LE:
157     case PIX_FMT_YUV420P10BE:
158     case PIX_FMT_YUV422P10LE:
159     case PIX_FMT_YUV422P10BE:
160     case PIX_FMT_YUV444P9LE:
161     case PIX_FMT_YUV444P9BE:
162     case PIX_FMT_YUV444P10LE:
163     case PIX_FMT_YUV444P10BE:
164         w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
165         h_align= 16;
166         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)
167             h_align= 32; // interlaced is rounded up to 2 MBs
168         break;
169     case PIX_FMT_YUV411P:
170     case PIX_FMT_UYYVYY411:
171         w_align=32;
172         h_align=8;
173         break;
174     case PIX_FMT_YUV410P:
175         if(s->codec_id == CODEC_ID_SVQ1){
176             w_align=64;
177             h_align=64;
178         }
179     case PIX_FMT_RGB555:
180         if(s->codec_id == CODEC_ID_RPZA){
181             w_align=4;
182             h_align=4;
183         }
184     case PIX_FMT_PAL8:
185     case PIX_FMT_BGR8:
186     case PIX_FMT_RGB8:
187         if(s->codec_id == CODEC_ID_SMC){
188             w_align=4;
189             h_align=4;
190         }
191         break;
192     case PIX_FMT_BGR24:
193         if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
194             w_align=4;
195             h_align=4;
196         }
197         break;
198     default:
199         w_align= 1;
200         h_align= 1;
201         break;
202     }
203
204     *width = FFALIGN(*width , w_align);
205     *height= FFALIGN(*height, h_align);
206     if(s->codec_id == CODEC_ID_H264 || s->lowres)
207         *height+=2; // some of the optimized chroma MC reads one line too much
208                     // which is also done in mpeg decoders with lowres > 0
209
210     linesize_align[0] =
211     linesize_align[1] =
212     linesize_align[2] =
213     linesize_align[3] = STRIDE_ALIGN;
214 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
215 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
216 //picture size unneccessarily in some cases. The solution here is not
217 //pretty and better ideas are welcome!
218 #if HAVE_MMX
219     if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
220        s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
221        s->codec_id == CODEC_ID_VP6A) {
222         linesize_align[0] =
223         linesize_align[1] =
224         linesize_align[2] = 16;
225     }
226 #endif
227 }
228
229 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
230     int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
231     int linesize_align[4];
232     int align;
233     avcodec_align_dimensions2(s, width, height, linesize_align);
234     align = FFMAX(linesize_align[0], linesize_align[3]);
235     linesize_align[1] <<= chroma_shift;
236     linesize_align[2] <<= chroma_shift;
237     align = FFMAX3(align, linesize_align[1], linesize_align[2]);
238     *width=FFALIGN(*width, align);
239 }
240
241 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
242     int i;
243     int w= s->width;
244     int h= s->height;
245     InternalBuffer *buf;
246     int *picture_number;
247
248     if(pic->data[0]!=NULL) {
249         av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
250         return -1;
251     }
252     if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
253         av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
254         return -1;
255     }
256
257     if(av_image_check_size(w, h, 0, s))
258         return -1;
259
260     if(s->internal_buffer==NULL){
261         s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
262     }
263 #if 0
264     s->internal_buffer= av_fast_realloc(
265         s->internal_buffer,
266         &s->internal_buffer_size,
267         sizeof(InternalBuffer)*FFMAX(99,  s->internal_buffer_count+1)/*FIXME*/
268         );
269 #endif
270
271     buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
272     picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
273     (*picture_number)++;
274
275     if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
276         if(s->active_thread_type&FF_THREAD_FRAME) {
277             av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
278             return -1;
279         }
280
281         for(i=0; i<4; i++){
282             av_freep(&buf->base[i]);
283             buf->data[i]= NULL;
284         }
285     }
286
287     if(buf->base[0]){
288         pic->age= *picture_number - buf->last_pic_num;
289         buf->last_pic_num= *picture_number;
290     }else{
291         int h_chroma_shift, v_chroma_shift;
292         int size[4] = {0};
293         int tmpsize;
294         int unaligned;
295         AVPicture picture;
296         int stride_align[4];
297         const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
298
299         avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
300
301         avcodec_align_dimensions2(s, &w, &h, stride_align);
302
303         if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
304             w+= EDGE_WIDTH*2;
305             h+= EDGE_WIDTH*2;
306         }
307
308         do {
309             // NOTE: do not align linesizes individually, this breaks e.g. assumptions
310             // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
311             av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
312             // increase alignment of w for next try (rhs gives the lowest bit set in w)
313             w += w & ~(w-1);
314
315             unaligned = 0;
316             for (i=0; i<4; i++){
317                 unaligned |= picture.linesize[i] % stride_align[i];
318             }
319         } while (unaligned);
320
321         tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
322         if (tmpsize < 0)
323             return -1;
324
325         for (i=0; i<3 && picture.data[i+1]; i++)
326             size[i] = picture.data[i+1] - picture.data[i];
327         size[i] = tmpsize - (picture.data[i] - picture.data[0]);
328
329         buf->last_pic_num= -256*256*256*64;
330         memset(buf->base, 0, sizeof(buf->base));
331         memset(buf->data, 0, sizeof(buf->data));
332
333         for(i=0; i<4 && size[i]; i++){
334             const int h_shift= i==0 ? 0 : h_chroma_shift;
335             const int v_shift= i==0 ? 0 : v_chroma_shift;
336
337             buf->linesize[i]= picture.linesize[i];
338
339             buf->base[i]= av_malloc(size[i]+16); //FIXME 16
340             if(buf->base[i]==NULL) return -1;
341             memset(buf->base[i], 128, size[i]);
342
343             // no edge if EDGE EMU or not planar YUV
344             if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
345                 buf->data[i] = buf->base[i];
346             else
347                 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
348         }
349         if(size[1] && !size[2])
350             ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
351         buf->width  = s->width;
352         buf->height = s->height;
353         buf->pix_fmt= s->pix_fmt;
354         pic->age= 256*256*256*64;
355     }
356     pic->type= FF_BUFFER_TYPE_INTERNAL;
357
358     for(i=0; i<4; i++){
359         pic->base[i]= buf->base[i];
360         pic->data[i]= buf->data[i];
361         pic->linesize[i]= buf->linesize[i];
362     }
363     s->internal_buffer_count++;
364
365     if (s->pkt) {
366         pic->pkt_pts = s->pkt->pts;
367         pic->pkt_pos = s->pkt->pos;
368     } else {
369         pic->pkt_pts = AV_NOPTS_VALUE;
370         pic->pkt_pos = -1;
371     }
372     pic->reordered_opaque= s->reordered_opaque;
373     pic->sample_aspect_ratio = s->sample_aspect_ratio;
374     pic->width               = s->width;
375     pic->height              = s->height;
376     pic->format              = s->pix_fmt;
377
378     if(s->debug&FF_DEBUG_BUFFERS)
379         av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
380
381     return 0;
382 }
383
384 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
385     int i;
386     InternalBuffer *buf, *last;
387
388     assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
389     assert(s->internal_buffer_count);
390
391     if(s->internal_buffer){
392     buf = NULL; /* avoids warning */
393     for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
394         buf= &((InternalBuffer*)s->internal_buffer)[i];
395         if(buf->data[0] == pic->data[0])
396             break;
397     }
398     assert(i < s->internal_buffer_count);
399     s->internal_buffer_count--;
400     last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
401
402     FFSWAP(InternalBuffer, *buf, *last);
403     }
404
405     for(i=0; i<4; i++){
406         pic->data[i]=NULL;
407 //        pic->base[i]=NULL;
408     }
409 //printf("R%X\n", pic->opaque);
410
411     if(s->debug&FF_DEBUG_BUFFERS)
412         av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
413 }
414
415 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
416     AVFrame temp_pic;
417     int i;
418
419     /* If no picture return a new buffer */
420     if(pic->data[0] == NULL) {
421         /* We will copy from buffer, so must be readable */
422         pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
423         return s->get_buffer(s, pic);
424     }
425
426     /* If internal buffer type return the same buffer */
427     if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
428         if(s->pkt) pic->pkt_pts= s->pkt->pts;
429         else       pic->pkt_pts= AV_NOPTS_VALUE;
430         pic->reordered_opaque= s->reordered_opaque;
431         return 0;
432     }
433
434     /*
435      * Not internal type and reget_buffer not overridden, emulate cr buffer
436      */
437     temp_pic = *pic;
438     for(i = 0; i < 4; i++)
439         pic->data[i] = pic->base[i] = NULL;
440     pic->opaque = NULL;
441     /* Allocate new frame */
442     if (s->get_buffer(s, pic))
443         return -1;
444     /* Copy image data from old buffer to new buffer */
445     av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
446              s->height);
447     s->release_buffer(s, &temp_pic); // Release old frame
448     return 0;
449 }
450
451 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
452     int i;
453
454     for(i=0; i<count; i++){
455         int r= func(c, (char*)arg + i*size);
456         if(ret) ret[i]= r;
457     }
458     return 0;
459 }
460
461 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
462     int i;
463
464     for(i=0; i<count; i++){
465         int r= func(c, arg, i, 0);
466         if(ret) ret[i]= r;
467     }
468     return 0;
469 }
470
471 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
472     while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
473         ++fmt;
474     return fmt[0];
475 }
476
477 void avcodec_get_frame_defaults(AVFrame *pic){
478     memset(pic, 0, sizeof(AVFrame));
479
480     pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
481     pic->pkt_pos = -1;
482     pic->key_frame= 1;
483     pic->sample_aspect_ratio = (AVRational){0, 1};
484     pic->format = -1;           /* unknown */
485 }
486
487 AVFrame *avcodec_alloc_frame(void){
488     AVFrame *pic= av_malloc(sizeof(AVFrame));
489
490     if(pic==NULL) return NULL;
491
492     avcodec_get_frame_defaults(pic);
493
494     return pic;
495 }
496
497 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
498 {
499     memset(sub, 0, sizeof(*sub));
500     sub->pts = AV_NOPTS_VALUE;
501 }
502
503 #if FF_API_AVCODEC_OPEN
504 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
505 {
506     return avcodec_open2(avctx, codec, NULL);
507 }
508 #endif
509
510 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
511 {
512     int ret = 0;
513     AVDictionary *tmp = NULL;
514
515     if (options)
516         av_dict_copy(&tmp, *options, 0);
517
518     /* If there is a user-supplied mutex locking routine, call it. */
519     if (ff_lockmgr_cb) {
520         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
521             return -1;
522     }
523
524     entangled_thread_counter++;
525     if(entangled_thread_counter != 1){
526         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
527         ret = -1;
528         goto end;
529     }
530
531     if(avctx->codec || !codec) {
532         ret = AVERROR(EINVAL);
533         goto end;
534     }
535
536     if (codec->priv_data_size > 0) {
537       if(!avctx->priv_data){
538         avctx->priv_data = av_mallocz(codec->priv_data_size);
539         if (!avctx->priv_data) {
540             ret = AVERROR(ENOMEM);
541             goto end;
542         }
543         if (codec->priv_class) {
544             *(AVClass**)avctx->priv_data= codec->priv_class;
545             av_opt_set_defaults(avctx->priv_data);
546         }
547       }
548       if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
549           goto free_and_end;
550     } else {
551         avctx->priv_data = NULL;
552     }
553     if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
554         goto free_and_end;
555
556     if(avctx->coded_width && avctx->coded_height)
557         avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
558     else if(avctx->width && avctx->height)
559         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
560
561     if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
562         && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
563            || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
564         av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
565         avcodec_set_dimensions(avctx, 0, 0);
566     }
567
568     /* if the decoder init function was already called previously,
569        free the already allocated subtitle_header before overwriting it */
570     if (codec->decode)
571         av_freep(&avctx->subtitle_header);
572
573 #define SANE_NB_CHANNELS 128U
574     if (avctx->channels > SANE_NB_CHANNELS) {
575         ret = AVERROR(EINVAL);
576         goto free_and_end;
577     }
578
579     avctx->codec = codec;
580     if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
581         avctx->codec_id == CODEC_ID_NONE) {
582         avctx->codec_type = codec->type;
583         avctx->codec_id   = codec->id;
584     }
585     if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
586                            && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
587         av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
588         ret = AVERROR(EINVAL);
589         goto free_and_end;
590     }
591     avctx->frame_number = 0;
592
593     if (!HAVE_THREADS)
594         av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
595
596     if (HAVE_THREADS && !avctx->thread_opaque) {
597         ret = ff_thread_init(avctx);
598         if (ret < 0) {
599             goto free_and_end;
600         }
601     }
602
603     if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
604         av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
605                avctx->codec->max_lowres);
606         ret = AVERROR(EINVAL);
607         goto free_and_end;
608     }
609     if (avctx->codec->encode) {
610         int i;
611         if (avctx->codec->sample_fmts) {
612             for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
613                 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
614                     break;
615             if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
616                 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
617                 ret = AVERROR(EINVAL);
618                 goto free_and_end;
619             }
620         }
621         if (avctx->codec->supported_samplerates) {
622             for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
623                 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
624                     break;
625             if (avctx->codec->supported_samplerates[i] == 0) {
626                 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
627                 ret = AVERROR(EINVAL);
628                 goto free_and_end;
629             }
630         }
631         if (avctx->codec->channel_layouts) {
632             if (!avctx->channel_layout) {
633                 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
634             } else {
635                 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
636                     if (avctx->channel_layout == avctx->codec->channel_layouts[i])
637                         break;
638                 if (avctx->codec->channel_layouts[i] == 0) {
639                     av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
640                     ret = AVERROR(EINVAL);
641                     goto free_and_end;
642                 }
643             }
644         }
645         if (avctx->channel_layout && avctx->channels) {
646             if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
647                 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
648                 ret = AVERROR(EINVAL);
649                 goto free_and_end;
650             }
651         } else if (avctx->channel_layout) {
652             avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
653         }
654     }
655
656     avctx->pts_correction_num_faulty_pts =
657     avctx->pts_correction_num_faulty_dts = 0;
658     avctx->pts_correction_last_pts =
659     avctx->pts_correction_last_dts = INT64_MIN;
660
661     if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
662         ret = avctx->codec->init(avctx);
663         if (ret < 0) {
664             goto free_and_end;
665         }
666     }
667
668     ret=0;
669 end:
670     entangled_thread_counter--;
671
672     /* Release any user-supplied mutex. */
673     if (ff_lockmgr_cb) {
674         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
675     }
676     if (options) {
677         av_dict_free(options);
678         *options = tmp;
679     }
680
681     return ret;
682 free_and_end:
683     av_dict_free(&tmp);
684     av_freep(&avctx->priv_data);
685     avctx->codec= NULL;
686     goto end;
687 }
688
689 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
690                          const short *samples)
691 {
692     if(buf_size < FF_MIN_BUFFER_SIZE && 0){
693         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
694         return -1;
695     }
696     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
697         int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
698         avctx->frame_number++;
699         return ret;
700     }else
701         return 0;
702 }
703
704 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
705                          const AVFrame *pict)
706 {
707     if(buf_size < FF_MIN_BUFFER_SIZE){
708         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
709         return -1;
710     }
711     if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
712         return -1;
713     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
714         int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
715         avctx->frame_number++;
716         emms_c(); //needed to avoid an emms_c() call before every return;
717
718         return ret;
719     }else
720         return 0;
721 }
722
723 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
724                             const AVSubtitle *sub)
725 {
726     int ret;
727     if(sub->start_display_time) {
728         av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
729         return -1;
730     }
731
732     ret = avctx->codec->encode(avctx, buf, buf_size, sub);
733     avctx->frame_number++;
734     return ret;
735 }
736
737 /**
738  * Attempt to guess proper monotonic timestamps for decoded video frames
739  * which might have incorrect times. Input timestamps may wrap around, in
740  * which case the output will as well.
741  *
742  * @param pts the pts field of the decoded AVPacket, as passed through
743  * AVFrame.pkt_pts
744  * @param dts the dts field of the decoded AVPacket
745  * @return one of the input values, may be AV_NOPTS_VALUE
746  */
747 static int64_t guess_correct_pts(AVCodecContext *ctx,
748                                  int64_t reordered_pts, int64_t dts)
749 {
750     int64_t pts = AV_NOPTS_VALUE;
751
752     if (dts != AV_NOPTS_VALUE) {
753         ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
754         ctx->pts_correction_last_dts = dts;
755     }
756     if (reordered_pts != AV_NOPTS_VALUE) {
757         ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
758         ctx->pts_correction_last_pts = reordered_pts;
759     }
760     if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
761        && reordered_pts != AV_NOPTS_VALUE)
762         pts = reordered_pts;
763     else
764         pts = dts;
765
766     return pts;
767 }
768
769 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
770                          int *got_picture_ptr,
771                          AVPacket *avpkt)
772 {
773     int ret;
774
775     *got_picture_ptr= 0;
776     if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
777         return -1;
778
779     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
780         av_packet_split_side_data(avpkt);
781         avctx->pkt = avpkt;
782         if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
783              ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
784                                           avpkt);
785         else {
786             ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
787                               avpkt);
788             picture->pkt_dts= avpkt->dts;
789
790             if(!avctx->has_b_frames){
791             picture->pkt_pos= avpkt->pos;
792             }
793             //FIXME these should be under if(!avctx->has_b_frames)
794             if (!picture->sample_aspect_ratio.num)
795                 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
796             if (!picture->width)
797                 picture->width = avctx->width;
798             if (!picture->height)
799                 picture->height = avctx->height;
800             if (picture->format == PIX_FMT_NONE)
801                 picture->format = avctx->pix_fmt;
802         }
803
804         emms_c(); //needed to avoid an emms_c() call before every return;
805
806
807         if (*got_picture_ptr){
808             avctx->frame_number++;
809             picture->best_effort_timestamp = guess_correct_pts(avctx,
810                                                             picture->pkt_pts,
811                                                             picture->pkt_dts);
812         }
813     }else
814         ret= 0;
815
816     return ret;
817 }
818
819 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
820                          int *frame_size_ptr,
821                          AVPacket *avpkt)
822 {
823     int ret;
824
825     avctx->pkt = avpkt;
826
827     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
828         //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
829         if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
830             av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
831             return -1;
832         }
833         if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
834         *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
835             av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
836             return -1;
837         }
838
839         ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
840         avctx->frame_number++;
841     }else{
842         ret= 0;
843         *frame_size_ptr=0;
844     }
845     return ret;
846 }
847
848 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
849                             int *got_sub_ptr,
850                             AVPacket *avpkt)
851 {
852     int ret;
853
854     avctx->pkt = avpkt;
855     *got_sub_ptr = 0;
856     avcodec_get_subtitle_defaults(sub);
857     ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
858     if (*got_sub_ptr)
859         avctx->frame_number++;
860     return ret;
861 }
862
863 void avsubtitle_free(AVSubtitle *sub)
864 {
865     int i;
866
867     for (i = 0; i < sub->num_rects; i++)
868     {
869         av_freep(&sub->rects[i]->pict.data[0]);
870         av_freep(&sub->rects[i]->pict.data[1]);
871         av_freep(&sub->rects[i]->pict.data[2]);
872         av_freep(&sub->rects[i]->pict.data[3]);
873         av_freep(&sub->rects[i]->text);
874         av_freep(&sub->rects[i]->ass);
875         av_freep(&sub->rects[i]);
876     }
877
878     av_freep(&sub->rects);
879
880     memset(sub, 0, sizeof(AVSubtitle));
881 }
882
883 av_cold int avcodec_close(AVCodecContext *avctx)
884 {
885     /* If there is a user-supplied mutex locking routine, call it. */
886     if (ff_lockmgr_cb) {
887         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
888             return -1;
889     }
890
891     entangled_thread_counter++;
892     if(entangled_thread_counter != 1){
893         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
894         entangled_thread_counter--;
895         return -1;
896     }
897
898     if (HAVE_THREADS && avctx->thread_opaque)
899         ff_thread_free(avctx);
900     if (avctx->codec && avctx->codec->close)
901         avctx->codec->close(avctx);
902     avcodec_default_free_buffers(avctx);
903     avctx->coded_frame = NULL;
904     if (avctx->codec && avctx->codec->priv_class)
905         av_opt_free(avctx->priv_data);
906     av_opt_free(avctx);
907     av_freep(&avctx->priv_data);
908     if(avctx->codec && avctx->codec->encode)
909         av_freep(&avctx->extradata);
910     avctx->codec = NULL;
911     avctx->active_thread_type = 0;
912     entangled_thread_counter--;
913
914     /* Release any user-supplied mutex. */
915     if (ff_lockmgr_cb) {
916         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
917     }
918     return 0;
919 }
920
921 AVCodec *avcodec_find_encoder(enum CodecID id)
922 {
923     AVCodec *p, *experimental=NULL;
924     p = first_avcodec;
925     while (p) {
926         if (p->encode != NULL && p->id == id) {
927             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
928                 experimental = p;
929             } else
930                 return p;
931         }
932         p = p->next;
933     }
934     return experimental;
935 }
936
937 AVCodec *avcodec_find_encoder_by_name(const char *name)
938 {
939     AVCodec *p;
940     if (!name)
941         return NULL;
942     p = first_avcodec;
943     while (p) {
944         if (p->encode != NULL && strcmp(name,p->name) == 0)
945             return p;
946         p = p->next;
947     }
948     return NULL;
949 }
950
951 AVCodec *avcodec_find_decoder(enum CodecID id)
952 {
953     AVCodec *p, *experimental=NULL;
954     p = first_avcodec;
955     while (p) {
956         if (p->decode != NULL && p->id == id) {
957             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
958                 experimental = p;
959             } else
960                 return p;
961         }
962         p = p->next;
963     }
964     return experimental;
965 }
966
967 AVCodec *avcodec_find_decoder_by_name(const char *name)
968 {
969     AVCodec *p;
970     if (!name)
971         return NULL;
972     p = first_avcodec;
973     while (p) {
974         if (p->decode != NULL && strcmp(name,p->name) == 0)
975             return p;
976         p = p->next;
977     }
978     return NULL;
979 }
980
981 static int get_bit_rate(AVCodecContext *ctx)
982 {
983     int bit_rate;
984     int bits_per_sample;
985
986     switch(ctx->codec_type) {
987     case AVMEDIA_TYPE_VIDEO:
988     case AVMEDIA_TYPE_DATA:
989     case AVMEDIA_TYPE_SUBTITLE:
990     case AVMEDIA_TYPE_ATTACHMENT:
991         bit_rate = ctx->bit_rate;
992         break;
993     case AVMEDIA_TYPE_AUDIO:
994         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
995         bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
996         break;
997     default:
998         bit_rate = 0;
999         break;
1000     }
1001     return bit_rate;
1002 }
1003
1004 const char *avcodec_get_name(enum CodecID id)
1005 {
1006     AVCodec *codec;
1007
1008 #if !CONFIG_SMALL
1009     switch (id) {
1010 #include "libavcodec/codec_names.h"
1011     }
1012     av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
1013 #endif
1014     codec = avcodec_find_decoder(id);
1015     if (codec)
1016         return codec->name;
1017     codec = avcodec_find_encoder(id);
1018     if (codec)
1019         return codec->name;
1020     return "unknown_codec";
1021 }
1022
1023 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1024 {
1025     int i, len, ret = 0;
1026
1027     for (i = 0; i < 4; i++) {
1028         len = snprintf(buf, buf_size,
1029                        isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1030         buf      += len;
1031         buf_size  = buf_size > len ? buf_size - len : 0;
1032         ret      += len;
1033         codec_tag>>=8;
1034     }
1035     return ret;
1036 }
1037
1038 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1039 {
1040     const char *codec_type;
1041     const char *codec_name;
1042     const char *profile = NULL;
1043     AVCodec *p;
1044     int bitrate;
1045     AVRational display_aspect_ratio;
1046
1047     if (!buf || buf_size <= 0)
1048         return;
1049     codec_type = av_get_media_type_string(enc->codec_type);
1050     codec_name = avcodec_get_name(enc->codec_id);
1051     if (enc->profile != FF_PROFILE_UNKNOWN) {
1052         p = encode ? avcodec_find_encoder(enc->codec_id) :
1053                      avcodec_find_decoder(enc->codec_id);
1054         if (p)
1055             profile = av_get_profile_name(p, enc->profile);
1056     }
1057
1058     snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
1059              codec_name, enc->mb_decision ? " (hq)" : "");
1060     buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
1061     if (profile)
1062         snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
1063     if (enc->codec_tag) {
1064         char tag_buf[32];
1065         av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1066         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1067                  " (%s / 0x%04X)", tag_buf, enc->codec_tag);
1068     }
1069     switch(enc->codec_type) {
1070     case AVMEDIA_TYPE_VIDEO:
1071         if (enc->pix_fmt != PIX_FMT_NONE) {
1072             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1073                      ", %s",
1074                      av_get_pix_fmt_name(enc->pix_fmt));
1075         }
1076         if (enc->width) {
1077             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1078                      ", %dx%d",
1079                      enc->width, enc->height);
1080             if (enc->sample_aspect_ratio.num) {
1081                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1082                           enc->width*enc->sample_aspect_ratio.num,
1083                           enc->height*enc->sample_aspect_ratio.den,
1084                           1024*1024);
1085                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1086                          " [SAR %d:%d DAR %d:%d]",
1087                          enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1088                          display_aspect_ratio.num, display_aspect_ratio.den);
1089             }
1090             if(av_log_get_level() >= AV_LOG_DEBUG){
1091                 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1092                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1093                      ", %d/%d",
1094                      enc->time_base.num/g, enc->time_base.den/g);
1095             }
1096         }
1097         if (encode) {
1098             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1099                      ", q=%d-%d", enc->qmin, enc->qmax);
1100         }
1101         break;
1102     case AVMEDIA_TYPE_AUDIO:
1103         if (enc->sample_rate) {
1104             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1105                      ", %d Hz", enc->sample_rate);
1106         }
1107         av_strlcat(buf, ", ", buf_size);
1108         av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1109         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1110             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1111                      ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1112         }
1113         break;
1114     default:
1115         return;
1116     }
1117     if (encode) {
1118         if (enc->flags & CODEC_FLAG_PASS1)
1119             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1120                      ", pass 1");
1121         if (enc->flags & CODEC_FLAG_PASS2)
1122             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1123                      ", pass 2");
1124     }
1125     bitrate = get_bit_rate(enc);
1126     if (bitrate != 0) {
1127         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1128                  ", %d kb/s", bitrate / 1000);
1129     }
1130 }
1131
1132 const char *av_get_profile_name(const AVCodec *codec, int profile)
1133 {
1134     const AVProfile *p;
1135     if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1136         return NULL;
1137
1138     for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1139         if (p->profile == profile)
1140             return p->name;
1141
1142     return NULL;
1143 }
1144
1145 unsigned avcodec_version( void )
1146 {
1147   return LIBAVCODEC_VERSION_INT;
1148 }
1149
1150 const char *avcodec_configuration(void)
1151 {
1152     return FFMPEG_CONFIGURATION;
1153 }
1154
1155 const char *avcodec_license(void)
1156 {
1157 #define LICENSE_PREFIX "libavcodec license: "
1158     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
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 }