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