]> git.sesse.net Git - ffmpeg/blob - libavcodec/utils.c
avcodec: add av_get_audio_frame_duration() function.
[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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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/avassert.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/crc.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/audioconvert.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/samplefmt.h"
36 #include "libavutil/dict.h"
37 #include "avcodec.h"
38 #include "dsputil.h"
39 #include "libavutil/opt.h"
40 #include "imgconvert.h"
41 #include "thread.h"
42 #include "audioconvert.h"
43 #include "internal.h"
44 #include "bytestream.h"
45 #include <stdlib.h>
46 #include <stdarg.h>
47 #include <limits.h>
48 #include <float.h>
49
50 static int volatile entangled_thread_counter=0;
51 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
52 static void *codec_mutex;
53 static void *avformat_mutex;
54
55 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
56 {
57     if(min_size < *size)
58         return ptr;
59
60     min_size= FFMAX(17*min_size/16 + 32, min_size);
61
62     ptr= av_realloc(ptr, min_size);
63     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
64         min_size= 0;
65
66     *size= min_size;
67
68     return ptr;
69 }
70
71 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
72 {
73     void **p = ptr;
74     if (min_size < *size)
75         return;
76     min_size= FFMAX(17*min_size/16 + 32, min_size);
77     av_free(*p);
78     *p = av_malloc(min_size);
79     if (!*p) min_size = 0;
80     *size= min_size;
81 }
82
83 void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
84 {
85     void **p = ptr;
86     if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
87         av_freep(p);
88         *size = 0;
89         return;
90     }
91     av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
92     if (*size)
93         memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
94 }
95
96 /* encoder management */
97 static AVCodec *first_avcodec = NULL;
98
99 AVCodec *av_codec_next(AVCodec *c){
100     if(c) return c->next;
101     else  return first_avcodec;
102 }
103
104 static void avcodec_init(void)
105 {
106     static int initialized = 0;
107
108     if (initialized != 0)
109         return;
110     initialized = 1;
111
112     ff_dsputil_static_init();
113 }
114
115 int av_codec_is_encoder(AVCodec *codec)
116 {
117     return codec && (codec->encode || codec->encode2);
118 }
119
120 int av_codec_is_decoder(AVCodec *codec)
121 {
122     return codec && codec->decode;
123 }
124
125 void avcodec_register(AVCodec *codec)
126 {
127     AVCodec **p;
128     avcodec_init();
129     p = &first_avcodec;
130     while (*p != NULL) p = &(*p)->next;
131     *p = codec;
132     codec->next = NULL;
133
134     if (codec->init_static_data)
135         codec->init_static_data(codec);
136 }
137
138 unsigned avcodec_get_edge_width(void)
139 {
140     return EDGE_WIDTH;
141 }
142
143 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
144     s->coded_width = width;
145     s->coded_height= height;
146     s->width = -((-width )>>s->lowres);
147     s->height= -((-height)>>s->lowres);
148 }
149
150 #define INTERNAL_BUFFER_SIZE (32+1)
151
152 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
153                                int linesize_align[AV_NUM_DATA_POINTERS])
154 {
155     int i;
156     int w_align= 1;
157     int h_align= 1;
158
159     switch(s->pix_fmt){
160     case PIX_FMT_YUV420P:
161     case PIX_FMT_YUYV422:
162     case PIX_FMT_UYVY422:
163     case PIX_FMT_YUV422P:
164     case PIX_FMT_YUV440P:
165     case PIX_FMT_YUV444P:
166     case PIX_FMT_GBRP:
167     case PIX_FMT_GRAY8:
168     case PIX_FMT_GRAY16BE:
169     case PIX_FMT_GRAY16LE:
170     case PIX_FMT_YUVJ420P:
171     case PIX_FMT_YUVJ422P:
172     case PIX_FMT_YUVJ440P:
173     case PIX_FMT_YUVJ444P:
174     case PIX_FMT_YUVA420P:
175     case PIX_FMT_YUV420P9LE:
176     case PIX_FMT_YUV420P9BE:
177     case PIX_FMT_YUV420P10LE:
178     case PIX_FMT_YUV420P10BE:
179     case PIX_FMT_YUV422P9LE:
180     case PIX_FMT_YUV422P9BE:
181     case PIX_FMT_YUV422P10LE:
182     case PIX_FMT_YUV422P10BE:
183     case PIX_FMT_YUV444P9LE:
184     case PIX_FMT_YUV444P9BE:
185     case PIX_FMT_YUV444P10LE:
186     case PIX_FMT_YUV444P10BE:
187     case PIX_FMT_GBRP9LE:
188     case PIX_FMT_GBRP9BE:
189     case PIX_FMT_GBRP10LE:
190     case PIX_FMT_GBRP10BE:
191         w_align = 16; //FIXME assume 16 pixel per macroblock
192         h_align = 16 * 2; // interlaced needs 2 macroblocks height
193         break;
194     case PIX_FMT_YUV411P:
195     case PIX_FMT_UYYVYY411:
196         w_align=32;
197         h_align=8;
198         break;
199     case PIX_FMT_YUV410P:
200         if(s->codec_id == CODEC_ID_SVQ1){
201             w_align=64;
202             h_align=64;
203         }
204     case PIX_FMT_RGB555:
205         if(s->codec_id == CODEC_ID_RPZA){
206             w_align=4;
207             h_align=4;
208         }
209     case PIX_FMT_PAL8:
210     case PIX_FMT_BGR8:
211     case PIX_FMT_RGB8:
212         if(s->codec_id == CODEC_ID_SMC){
213             w_align=4;
214             h_align=4;
215         }
216         break;
217     case PIX_FMT_BGR24:
218         if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
219             w_align=4;
220             h_align=4;
221         }
222         break;
223     default:
224         w_align= 1;
225         h_align= 1;
226         break;
227     }
228
229     *width = FFALIGN(*width , w_align);
230     *height= FFALIGN(*height, h_align);
231     if(s->codec_id == CODEC_ID_H264 || s->lowres)
232         *height+=2; // some of the optimized chroma MC reads one line too much
233                     // which is also done in mpeg decoders with lowres > 0
234
235     for (i = 0; i < 4; i++)
236         linesize_align[i] = STRIDE_ALIGN;
237 }
238
239 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
240     int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
241     int linesize_align[AV_NUM_DATA_POINTERS];
242     int align;
243     avcodec_align_dimensions2(s, width, height, linesize_align);
244     align = FFMAX(linesize_align[0], linesize_align[3]);
245     linesize_align[1] <<= chroma_shift;
246     linesize_align[2] <<= chroma_shift;
247     align = FFMAX3(align, linesize_align[1], linesize_align[2]);
248     *width=FFALIGN(*width, align);
249 }
250
251 int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
252                              enum AVSampleFormat sample_fmt, const uint8_t *buf,
253                              int buf_size, int align)
254 {
255     int ch, planar, needed_size, ret = 0;
256
257     needed_size = av_samples_get_buffer_size(NULL, nb_channels,
258                                              frame->nb_samples, sample_fmt,
259                                              align);
260     if (buf_size < needed_size)
261         return AVERROR(EINVAL);
262
263     planar = av_sample_fmt_is_planar(sample_fmt);
264     if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
265         if (!(frame->extended_data = av_mallocz(nb_channels *
266                                                 sizeof(*frame->extended_data))))
267             return AVERROR(ENOMEM);
268     } else {
269         frame->extended_data = frame->data;
270     }
271
272     if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
273                                       buf, nb_channels, frame->nb_samples,
274                                       sample_fmt, align)) < 0) {
275         if (frame->extended_data != frame->data)
276             av_free(frame->extended_data);
277         return ret;
278     }
279     if (frame->extended_data != frame->data) {
280         for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
281             frame->data[ch] = frame->extended_data[ch];
282     }
283
284     return ret;
285 }
286
287 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
288 {
289     AVCodecInternal *avci = avctx->internal;
290     InternalBuffer *buf;
291     int buf_size, ret;
292
293     buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
294                                           frame->nb_samples, avctx->sample_fmt,
295                                           32);
296     if (buf_size < 0)
297         return AVERROR(EINVAL);
298
299     /* allocate InternalBuffer if needed */
300     if (!avci->buffer) {
301         avci->buffer = av_mallocz(sizeof(InternalBuffer));
302         if (!avci->buffer)
303             return AVERROR(ENOMEM);
304     }
305     buf = avci->buffer;
306
307     /* if there is a previously-used internal buffer, check its size and
308        channel count to see if we can reuse it */
309     if (buf->extended_data) {
310         /* if current buffer is too small, free it */
311         if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
312             av_free(buf->extended_data[0]);
313             if (buf->extended_data != buf->data)
314                 av_free(&buf->extended_data);
315             buf->extended_data = NULL;
316             buf->data[0] = NULL;
317         }
318         /* if number of channels has changed, reset and/or free extended data
319            pointers but leave data buffer in buf->data[0] for reuse */
320         if (buf->nb_channels != avctx->channels) {
321             if (buf->extended_data != buf->data)
322                 av_free(buf->extended_data);
323             buf->extended_data = NULL;
324         }
325     }
326
327     /* if there is no previous buffer or the previous buffer cannot be used
328        as-is, allocate a new buffer and/or rearrange the channel pointers */
329     if (!buf->extended_data) {
330         if (!buf->data[0]) {
331             if (!(buf->data[0] = av_mallocz(buf_size)))
332                 return AVERROR(ENOMEM);
333             buf->audio_data_size = buf_size;
334         }
335         if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
336                                             avctx->sample_fmt, buf->data[0],
337                                             buf->audio_data_size, 32)))
338             return ret;
339
340         if (frame->extended_data == frame->data)
341             buf->extended_data = buf->data;
342         else
343             buf->extended_data = frame->extended_data;
344         memcpy(buf->data, frame->data, sizeof(frame->data));
345         buf->linesize[0] = frame->linesize[0];
346         buf->nb_channels = avctx->channels;
347     } else {
348         /* copy InternalBuffer info to the AVFrame */
349         frame->extended_data = buf->extended_data;
350         frame->linesize[0]   = buf->linesize[0];
351         memcpy(frame->data, buf->data, sizeof(frame->data));
352     }
353
354     frame->type          = FF_BUFFER_TYPE_INTERNAL;
355
356     if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
357     else            frame->pkt_pts = AV_NOPTS_VALUE;
358     frame->reordered_opaque = avctx->reordered_opaque;
359
360     if (avctx->debug & FF_DEBUG_BUFFERS)
361         av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
362                "internal audio buffer used\n", frame);
363
364     return 0;
365 }
366
367 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
368 {
369     int i;
370     int w= s->width;
371     int h= s->height;
372     InternalBuffer *buf;
373     AVCodecInternal *avci = s->internal;
374
375     if(pic->data[0]!=NULL) {
376         av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
377         return -1;
378     }
379     if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
380         av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
381         return -1;
382     }
383
384     if(av_image_check_size(w, h, 0, s))
385         return -1;
386
387     if (!avci->buffer) {
388         avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
389                                   sizeof(InternalBuffer));
390     }
391
392     buf = &avci->buffer[avci->buffer_count];
393
394     if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
395         if(s->active_thread_type&FF_THREAD_FRAME) {
396             av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
397             return -1;
398         }
399
400         for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
401             av_freep(&buf->base[i]);
402             buf->data[i]= NULL;
403         }
404     }
405
406     if (!buf->base[0]) {
407         int h_chroma_shift, v_chroma_shift;
408         int size[4] = {0};
409         int tmpsize;
410         int unaligned;
411         AVPicture picture;
412         int stride_align[AV_NUM_DATA_POINTERS];
413         const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
414
415         avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
416
417         avcodec_align_dimensions2(s, &w, &h, stride_align);
418
419         if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
420             w+= EDGE_WIDTH*2;
421             h+= EDGE_WIDTH*2;
422         }
423
424         do {
425             // NOTE: do not align linesizes individually, this breaks e.g. assumptions
426             // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
427             av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
428             // increase alignment of w for next try (rhs gives the lowest bit set in w)
429             w += w & ~(w-1);
430
431             unaligned = 0;
432             for (i=0; i<4; i++){
433                 unaligned |= picture.linesize[i] % stride_align[i];
434             }
435         } while (unaligned);
436
437         tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
438         if (tmpsize < 0)
439             return -1;
440
441         for (i=0; i<3 && picture.data[i+1]; i++)
442             size[i] = picture.data[i+1] - picture.data[i];
443         size[i] = tmpsize - (picture.data[i] - picture.data[0]);
444
445         memset(buf->base, 0, sizeof(buf->base));
446         memset(buf->data, 0, sizeof(buf->data));
447
448         for(i=0; i<4 && size[i]; i++){
449             const int h_shift= i==0 ? 0 : h_chroma_shift;
450             const int v_shift= i==0 ? 0 : v_chroma_shift;
451
452             buf->linesize[i]= picture.linesize[i];
453
454             buf->base[i]= av_malloc(size[i]+16); //FIXME 16
455             if(buf->base[i]==NULL) return -1;
456             memset(buf->base[i], 128, size[i]);
457
458             // no edge if EDGE EMU or not planar YUV
459             if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
460                 buf->data[i] = buf->base[i];
461             else
462                 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
463         }
464         for (; i < AV_NUM_DATA_POINTERS; i++) {
465             buf->base[i] = buf->data[i] = NULL;
466             buf->linesize[i] = 0;
467         }
468         if(size[1] && !size[2])
469             ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
470         buf->width  = s->width;
471         buf->height = s->height;
472         buf->pix_fmt= s->pix_fmt;
473     }
474     pic->type= FF_BUFFER_TYPE_INTERNAL;
475
476     for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
477         pic->base[i]= buf->base[i];
478         pic->data[i]= buf->data[i];
479         pic->linesize[i]= buf->linesize[i];
480     }
481     pic->extended_data = pic->data;
482     avci->buffer_count++;
483
484     if(s->pkt) pic->pkt_pts= s->pkt->pts;
485     else       pic->pkt_pts= AV_NOPTS_VALUE;
486     pic->reordered_opaque= s->reordered_opaque;
487
488     if(s->debug&FF_DEBUG_BUFFERS)
489         av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
490                "buffers used\n", pic, avci->buffer_count);
491
492     return 0;
493 }
494
495 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
496 {
497     switch (avctx->codec_type) {
498     case AVMEDIA_TYPE_VIDEO:
499         return video_get_buffer(avctx, frame);
500     case AVMEDIA_TYPE_AUDIO:
501         return audio_get_buffer(avctx, frame);
502     default:
503         return -1;
504     }
505 }
506
507 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
508     int i;
509     InternalBuffer *buf, *last;
510     AVCodecInternal *avci = s->internal;
511
512     assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
513
514     assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
515     assert(avci->buffer_count);
516
517     if (avci->buffer) {
518         buf = NULL; /* avoids warning */
519         for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
520             buf = &avci->buffer[i];
521             if (buf->data[0] == pic->data[0])
522                 break;
523         }
524         assert(i < avci->buffer_count);
525         avci->buffer_count--;
526         last = &avci->buffer[avci->buffer_count];
527
528         if (buf != last)
529             FFSWAP(InternalBuffer, *buf, *last);
530     }
531
532     for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
533         pic->data[i]=NULL;
534 //        pic->base[i]=NULL;
535     }
536 //printf("R%X\n", pic->opaque);
537
538     if(s->debug&FF_DEBUG_BUFFERS)
539         av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
540                "buffers used\n", pic, avci->buffer_count);
541 }
542
543 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
544     AVFrame temp_pic;
545     int i;
546
547     assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
548
549     /* If no picture return a new buffer */
550     if(pic->data[0] == NULL) {
551         /* We will copy from buffer, so must be readable */
552         pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
553         return s->get_buffer(s, pic);
554     }
555
556     assert(s->pix_fmt == pic->format);
557
558     /* If internal buffer type return the same buffer */
559     if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
560         if(s->pkt) pic->pkt_pts= s->pkt->pts;
561         else       pic->pkt_pts= AV_NOPTS_VALUE;
562         pic->reordered_opaque= s->reordered_opaque;
563         return 0;
564     }
565
566     /*
567      * Not internal type and reget_buffer not overridden, emulate cr buffer
568      */
569     temp_pic = *pic;
570     for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
571         pic->data[i] = pic->base[i] = NULL;
572     pic->opaque = NULL;
573     /* Allocate new frame */
574     if (s->get_buffer(s, pic))
575         return -1;
576     /* Copy image data from old buffer to new buffer */
577     av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
578              s->height);
579     s->release_buffer(s, &temp_pic); // Release old frame
580     return 0;
581 }
582
583 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
584     int i;
585
586     for(i=0; i<count; i++){
587         int r= func(c, (char*)arg + i*size);
588         if(ret) ret[i]= r;
589     }
590     return 0;
591 }
592
593 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
594     int i;
595
596     for(i=0; i<count; i++){
597         int r= func(c, arg, i, 0);
598         if(ret) ret[i]= r;
599     }
600     return 0;
601 }
602
603 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
604     while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
605         ++fmt;
606     return fmt[0];
607 }
608
609 void avcodec_get_frame_defaults(AVFrame *pic){
610     memset(pic, 0, sizeof(AVFrame));
611
612     pic->pts= AV_NOPTS_VALUE;
613     pic->key_frame= 1;
614     pic->sample_aspect_ratio = (AVRational){0, 1};
615     pic->format = -1;           /* unknown */
616 }
617
618 AVFrame *avcodec_alloc_frame(void){
619     AVFrame *pic= av_malloc(sizeof(AVFrame));
620
621     if(pic==NULL) return NULL;
622
623     avcodec_get_frame_defaults(pic);
624
625     return pic;
626 }
627
628 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
629 {
630     int ret = 0;
631     AVDictionary *tmp = NULL;
632
633     if (avcodec_is_open(avctx))
634         return 0;
635
636     if ((!codec && !avctx->codec)) {
637         av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
638         return AVERROR(EINVAL);
639     }
640     if ((codec && avctx->codec && codec != avctx->codec)) {
641         av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
642                "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
643         return AVERROR(EINVAL);
644     }
645     if (!codec)
646         codec = avctx->codec;
647
648     if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
649         return AVERROR(EINVAL);
650
651     if (options)
652         av_dict_copy(&tmp, *options, 0);
653
654     /* If there is a user-supplied mutex locking routine, call it. */
655     if (ff_lockmgr_cb) {
656         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
657             return -1;
658     }
659
660     entangled_thread_counter++;
661     if(entangled_thread_counter != 1){
662         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
663         ret = -1;
664         goto end;
665     }
666
667     avctx->internal = av_mallocz(sizeof(AVCodecInternal));
668     if (!avctx->internal) {
669         ret = AVERROR(ENOMEM);
670         goto end;
671     }
672
673     if (codec->priv_data_size > 0) {
674       if(!avctx->priv_data){
675         avctx->priv_data = av_mallocz(codec->priv_data_size);
676         if (!avctx->priv_data) {
677             ret = AVERROR(ENOMEM);
678             goto end;
679         }
680         if (codec->priv_class) {
681             *(AVClass**)avctx->priv_data= codec->priv_class;
682             av_opt_set_defaults(avctx->priv_data);
683         }
684       }
685       if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
686           goto free_and_end;
687     } else {
688         avctx->priv_data = NULL;
689     }
690     if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
691         goto free_and_end;
692
693     if(avctx->coded_width && avctx->coded_height)
694         avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
695     else if(avctx->width && avctx->height)
696         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
697
698     if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
699         && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
700            || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
701         av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
702         avcodec_set_dimensions(avctx, 0, 0);
703     }
704
705     /* if the decoder init function was already called previously,
706        free the already allocated subtitle_header before overwriting it */
707     if (av_codec_is_decoder(codec))
708         av_freep(&avctx->subtitle_header);
709
710 #define SANE_NB_CHANNELS 128U
711     if (avctx->channels > SANE_NB_CHANNELS) {
712         ret = AVERROR(EINVAL);
713         goto free_and_end;
714     }
715
716     avctx->codec = codec;
717     if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
718         avctx->codec_id == CODEC_ID_NONE) {
719         avctx->codec_type = codec->type;
720         avctx->codec_id   = codec->id;
721     }
722     if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
723                            && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
724         av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
725         ret = AVERROR(EINVAL);
726         goto free_and_end;
727     }
728     avctx->frame_number = 0;
729
730     if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
731         (!avctx->time_base.num || !avctx->time_base.den)) {
732         avctx->time_base.num = 1;
733         avctx->time_base.den = avctx->sample_rate;
734     }
735
736     if (HAVE_THREADS && !avctx->thread_opaque) {
737         ret = ff_thread_init(avctx);
738         if (ret < 0) {
739             goto free_and_end;
740         }
741     }
742     if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
743         avctx->thread_count = 1;
744
745     if (avctx->codec->max_lowres < avctx->lowres) {
746         av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
747                avctx->codec->max_lowres);
748         ret = AVERROR(EINVAL);
749         goto free_and_end;
750     }
751     if (av_codec_is_encoder(avctx->codec)) {
752         int i;
753         if (avctx->codec->sample_fmts) {
754             for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
755                 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
756                     break;
757             if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
758                 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
759                 ret = AVERROR(EINVAL);
760                 goto free_and_end;
761             }
762         }
763         if (avctx->codec->pix_fmts) {
764             for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
765                 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
766                     break;
767             if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE) {
768                 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
769                 ret = AVERROR(EINVAL);
770                 goto free_and_end;
771             }
772         }
773         if (avctx->codec->supported_samplerates) {
774             for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
775                 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
776                     break;
777             if (avctx->codec->supported_samplerates[i] == 0) {
778                 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
779                 ret = AVERROR(EINVAL);
780                 goto free_and_end;
781             }
782         }
783         if (avctx->codec->channel_layouts) {
784             if (!avctx->channel_layout) {
785                 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
786             } else {
787                 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
788                     if (avctx->channel_layout == avctx->codec->channel_layouts[i])
789                         break;
790                 if (avctx->codec->channel_layouts[i] == 0) {
791                     av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
792                     ret = AVERROR(EINVAL);
793                     goto free_and_end;
794                 }
795             }
796         }
797         if (avctx->channel_layout && avctx->channels) {
798             if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
799                 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
800                 ret = AVERROR(EINVAL);
801                 goto free_and_end;
802             }
803         } else if (avctx->channel_layout) {
804             avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
805         }
806     }
807
808     if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
809         ret = avctx->codec->init(avctx);
810         if (ret < 0) {
811             goto free_and_end;
812         }
813     }
814 end:
815     entangled_thread_counter--;
816
817     /* Release any user-supplied mutex. */
818     if (ff_lockmgr_cb) {
819         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
820     }
821     if (options) {
822         av_dict_free(options);
823         *options = tmp;
824     }
825
826     return ret;
827 free_and_end:
828     av_dict_free(&tmp);
829     av_freep(&avctx->priv_data);
830     av_freep(&avctx->internal);
831     avctx->codec= NULL;
832     goto end;
833 }
834
835 int ff_alloc_packet(AVPacket *avpkt, int size)
836 {
837     if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
838         return AVERROR(EINVAL);
839
840     if (avpkt->data) {
841         void *destruct = avpkt->destruct;
842
843         if (avpkt->size < size)
844             return AVERROR(EINVAL);
845
846         av_init_packet(avpkt);
847         avpkt->destruct = destruct;
848         avpkt->size = size;
849         return 0;
850     } else {
851         return av_new_packet(avpkt, size);
852     }
853 }
854
855 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
856                                               AVPacket *avpkt,
857                                               const AVFrame *frame,
858                                               int *got_packet_ptr)
859 {
860     int ret;
861     int user_packet = !!avpkt->data;
862     int nb_samples;
863
864     *got_packet_ptr = 0;
865
866     if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
867         av_free_packet(avpkt);
868         av_init_packet(avpkt);
869         avpkt->size = 0;
870         return 0;
871     }
872
873     /* check for valid frame size */
874     if (frame) {
875         nb_samples = frame->nb_samples;
876         if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
877             if (nb_samples > avctx->frame_size)
878                 return AVERROR(EINVAL);
879         } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
880             if (nb_samples != avctx->frame_size)
881                 return AVERROR(EINVAL);
882         }
883     } else {
884         nb_samples = avctx->frame_size;
885     }
886
887     if (avctx->codec->encode2) {
888         ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
889         if (!ret && *got_packet_ptr) {
890             if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
891                 if (avpkt->pts == AV_NOPTS_VALUE)
892                     avpkt->pts = frame->pts;
893                 if (!avpkt->duration)
894                     avpkt->duration = ff_samples_to_time_base(avctx,
895                                                               frame->nb_samples);
896             }
897             avpkt->dts = avpkt->pts;
898         } else {
899             avpkt->size = 0;
900         }
901     } else {
902         /* for compatibility with encoders not supporting encode2(), we need to
903            allocate a packet buffer if the user has not provided one or check
904            the size otherwise */
905         int fs_tmp   = 0;
906         int buf_size = avpkt->size;
907         if (!user_packet) {
908             if (avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE) {
909                 av_assert0(av_get_bits_per_sample(avctx->codec_id) != 0);
910                 buf_size = nb_samples * avctx->channels *
911                            av_get_bits_per_sample(avctx->codec_id) / 8;
912             } else {
913                 /* this is a guess as to the required size.
914                    if an encoder needs more than this, it should probably
915                    implement encode2() */
916                 buf_size = 2 * avctx->frame_size * avctx->channels *
917                            av_get_bytes_per_sample(avctx->sample_fmt);
918                 buf_size += FF_MIN_BUFFER_SIZE;
919             }
920         }
921         if ((ret = ff_alloc_packet(avpkt, buf_size)))
922             return ret;
923
924         /* Encoders using AVCodec.encode() that support
925            CODEC_CAP_SMALL_LAST_FRAME require avctx->frame_size to be set to
926            the smaller size when encoding the last frame.
927            This code can be removed once all encoders supporting
928            CODEC_CAP_SMALL_LAST_FRAME use encode2() */
929         if ((avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) &&
930             nb_samples < avctx->frame_size) {
931             fs_tmp = avctx->frame_size;
932             avctx->frame_size = nb_samples;
933         }
934
935         /* encode the frame */
936         ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size,
937                                    frame ? frame->data[0] : NULL);
938         if (ret >= 0) {
939             if (!ret) {
940                 /* no output. if the packet data was allocated by libavcodec,
941                    free it */
942                 if (!user_packet)
943                     av_freep(&avpkt->data);
944             } else {
945                 if (avctx->coded_frame)
946                     avpkt->pts = avpkt->dts = avctx->coded_frame->pts;
947                 /* Set duration for final small packet. This can be removed
948                    once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
949                    encode2() */
950                 if (fs_tmp) {
951                     avpkt->duration = ff_samples_to_time_base(avctx,
952                                                               avctx->frame_size);
953                 }
954             }
955             avpkt->size = ret;
956             *got_packet_ptr = (ret > 0);
957             ret = 0;
958         }
959
960         if (fs_tmp)
961             avctx->frame_size = fs_tmp;
962     }
963     if (!ret)
964         avctx->frame_number++;
965
966     if (ret < 0 || !*got_packet_ptr)
967         av_free_packet(avpkt);
968
969     /* NOTE: if we add any audio encoders which output non-keyframe packets,
970              this needs to be moved to the encoders, but for now we can do it
971              here to simplify things */
972     avpkt->flags |= AV_PKT_FLAG_KEY;
973
974     return ret;
975 }
976
977 #if FF_API_OLD_DECODE_AUDIO
978 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
979                                              uint8_t *buf, int buf_size,
980                                              const short *samples)
981 {
982     AVPacket pkt;
983     AVFrame frame0;
984     AVFrame *frame;
985     int ret, samples_size, got_packet;
986
987     av_init_packet(&pkt);
988     pkt.data = buf;
989     pkt.size = buf_size;
990
991     if (samples) {
992         frame = &frame0;
993         avcodec_get_frame_defaults(frame);
994
995         if (avctx->frame_size) {
996             frame->nb_samples = avctx->frame_size;
997         } else {
998             /* if frame_size is not set, the number of samples must be
999                calculated from the buffer size */
1000             int64_t nb_samples;
1001             if (!av_get_bits_per_sample(avctx->codec_id)) {
1002                 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1003                        "support this codec\n");
1004                 return AVERROR(EINVAL);
1005             }
1006             nb_samples = (int64_t)buf_size * 8 /
1007                          (av_get_bits_per_sample(avctx->codec_id) *
1008                          avctx->channels);
1009             if (nb_samples >= INT_MAX)
1010                 return AVERROR(EINVAL);
1011             frame->nb_samples = nb_samples;
1012         }
1013
1014         /* it is assumed that the samples buffer is large enough based on the
1015            relevant parameters */
1016         samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1017                                                   frame->nb_samples,
1018                                                   avctx->sample_fmt, 1);
1019         if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1020                                             avctx->sample_fmt,
1021                                             samples, samples_size, 1)))
1022             return ret;
1023
1024         /* fabricate frame pts from sample count.
1025            this is needed because the avcodec_encode_audio() API does not have
1026            a way for the user to provide pts */
1027         frame->pts = ff_samples_to_time_base(avctx,
1028                                              avctx->internal->sample_count);
1029         avctx->internal->sample_count += frame->nb_samples;
1030     } else {
1031         frame = NULL;
1032     }
1033
1034     got_packet = 0;
1035     ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1036     if (!ret && got_packet && avctx->coded_frame) {
1037         avctx->coded_frame->pts       = pkt.pts;
1038         avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1039     }
1040     /* free any side data since we cannot return it */
1041     if (pkt.side_data_elems > 0) {
1042         int i;
1043         for (i = 0; i < pkt.side_data_elems; i++)
1044             av_free(pkt.side_data[i].data);
1045         av_freep(&pkt.side_data);
1046         pkt.side_data_elems = 0;
1047     }
1048
1049     if (frame && frame->extended_data != frame->data)
1050         av_free(frame->extended_data);
1051
1052     return ret ? ret : pkt.size;
1053 }
1054 #endif
1055
1056 #if FF_API_OLD_ENCODE_VIDEO
1057 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1058                          const AVFrame *pict)
1059 {
1060     AVPacket pkt;
1061     int ret, got_packet = 0;
1062
1063     if(buf_size < FF_MIN_BUFFER_SIZE){
1064         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1065         return -1;
1066     }
1067
1068     av_init_packet(&pkt);
1069     pkt.data = buf;
1070     pkt.size = buf_size;
1071
1072     ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1073     if (!ret && got_packet && avctx->coded_frame) {
1074         avctx->coded_frame->pts       = pkt.pts;
1075         avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1076     }
1077
1078     /* free any side data since we cannot return it */
1079     if (pkt.side_data_elems > 0) {
1080         int i;
1081         for (i = 0; i < pkt.side_data_elems; i++)
1082             av_free(pkt.side_data[i].data);
1083         av_freep(&pkt.side_data);
1084         pkt.side_data_elems = 0;
1085     }
1086
1087     return ret ? ret : pkt.size;
1088 }
1089 #endif
1090
1091 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1092                                               AVPacket *avpkt,
1093                                               const AVFrame *frame,
1094                                               int *got_packet_ptr)
1095 {
1096     int ret;
1097     int user_packet = !!avpkt->data;
1098
1099     *got_packet_ptr = 0;
1100
1101     if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1102         av_free_packet(avpkt);
1103         av_init_packet(avpkt);
1104         avpkt->size     = 0;
1105         return 0;
1106     }
1107
1108     if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1109         return AVERROR(EINVAL);
1110
1111     av_assert0(avctx->codec->encode2);
1112
1113     ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1114     if (!ret) {
1115         if (!*got_packet_ptr)
1116             avpkt->size = 0;
1117         else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1118             avpkt->pts = avpkt->dts = frame->pts;
1119
1120         if (!user_packet && avpkt->data) {
1121             uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
1122             if (new_data)
1123                 avpkt->data = new_data;
1124         }
1125
1126         avctx->frame_number++;
1127     }
1128
1129     if (ret < 0 || !*got_packet_ptr)
1130         av_free_packet(avpkt);
1131
1132     emms_c();
1133     return ret;
1134 }
1135
1136 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1137                             const AVSubtitle *sub)
1138 {
1139     int ret;
1140     if(sub->start_display_time) {
1141         av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1142         return -1;
1143     }
1144     if(sub->num_rects == 0 || !sub->rects)
1145         return -1;
1146     ret = avctx->codec->encode(avctx, buf, buf_size, sub);
1147     avctx->frame_number++;
1148     return ret;
1149 }
1150
1151 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1152 {
1153     int size = 0;
1154     const uint8_t *data;
1155     uint32_t flags;
1156
1157     if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1158         return;
1159
1160     data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1161     if (!data || size < 4)
1162         return;
1163     flags = bytestream_get_le32(&data);
1164     size -= 4;
1165     if (size < 4) /* Required for any of the changes */
1166         return;
1167     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1168         avctx->channels = bytestream_get_le32(&data);
1169         size -= 4;
1170     }
1171     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1172         if (size < 8)
1173             return;
1174         avctx->channel_layout = bytestream_get_le64(&data);
1175         size -= 8;
1176     }
1177     if (size < 4)
1178         return;
1179     if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1180         avctx->sample_rate = bytestream_get_le32(&data);
1181         size -= 4;
1182     }
1183     if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1184         if (size < 8)
1185             return;
1186         avctx->width  = bytestream_get_le32(&data);
1187         avctx->height = bytestream_get_le32(&data);
1188         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1189         size -= 8;
1190     }
1191 }
1192
1193 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1194                          int *got_picture_ptr,
1195                          AVPacket *avpkt)
1196 {
1197     int ret;
1198
1199     *got_picture_ptr= 0;
1200     if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1201         return -1;
1202
1203     avctx->pkt = avpkt;
1204     apply_param_change(avctx, avpkt);
1205
1206     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
1207         if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1208              ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1209                                           avpkt);
1210         else {
1211             ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1212                               avpkt);
1213             picture->pkt_dts= avpkt->dts;
1214             picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1215             picture->width  = avctx->width;
1216             picture->height = avctx->height;
1217             picture->format = avctx->pix_fmt;
1218         }
1219
1220         emms_c(); //needed to avoid an emms_c() call before every return;
1221
1222         if (*got_picture_ptr)
1223             avctx->frame_number++;
1224     }else
1225         ret= 0;
1226
1227     return ret;
1228 }
1229
1230 #if FF_API_OLD_DECODE_AUDIO
1231 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1232                          int *frame_size_ptr,
1233                          AVPacket *avpkt)
1234 {
1235     AVFrame frame;
1236     int ret, got_frame = 0;
1237
1238     if (avctx->get_buffer != avcodec_default_get_buffer) {
1239         av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1240                "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1241         av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1242                "avcodec_decode_audio4()\n");
1243         avctx->get_buffer = avcodec_default_get_buffer;
1244     }
1245
1246     ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1247
1248     if (ret >= 0 && got_frame) {
1249         int ch, plane_size;
1250         int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1251         int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1252                                                    frame.nb_samples,
1253                                                    avctx->sample_fmt, 1);
1254         if (*frame_size_ptr < data_size) {
1255             av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1256                    "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1257             return AVERROR(EINVAL);
1258         }
1259
1260         memcpy(samples, frame.extended_data[0], plane_size);
1261
1262         if (planar && avctx->channels > 1) {
1263             uint8_t *out = ((uint8_t *)samples) + plane_size;
1264             for (ch = 1; ch < avctx->channels; ch++) {
1265                 memcpy(out, frame.extended_data[ch], plane_size);
1266                 out += plane_size;
1267             }
1268         }
1269         *frame_size_ptr = data_size;
1270     } else {
1271         *frame_size_ptr = 0;
1272     }
1273     return ret;
1274 }
1275 #endif
1276
1277 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1278                                               AVFrame *frame,
1279                                               int *got_frame_ptr,
1280                                               AVPacket *avpkt)
1281 {
1282     int ret = 0;
1283
1284     *got_frame_ptr = 0;
1285
1286     avctx->pkt = avpkt;
1287
1288     if (!avpkt->data && avpkt->size) {
1289         av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1290         return AVERROR(EINVAL);
1291     }
1292
1293     apply_param_change(avctx, avpkt);
1294
1295     if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1296         ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
1297         if (ret >= 0 && *got_frame_ptr) {
1298             avctx->frame_number++;
1299             frame->pkt_dts = avpkt->dts;
1300             if (frame->format == AV_SAMPLE_FMT_NONE)
1301                 frame->format = avctx->sample_fmt;
1302         }
1303     }
1304     return ret;
1305 }
1306
1307 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1308                             int *got_sub_ptr,
1309                             AVPacket *avpkt)
1310 {
1311     int ret;
1312
1313     avctx->pkt = avpkt;
1314     *got_sub_ptr = 0;
1315     ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1316     if (*got_sub_ptr)
1317         avctx->frame_number++;
1318     return ret;
1319 }
1320
1321 void avsubtitle_free(AVSubtitle *sub)
1322 {
1323     int i;
1324
1325     for (i = 0; i < sub->num_rects; i++)
1326     {
1327         av_freep(&sub->rects[i]->pict.data[0]);
1328         av_freep(&sub->rects[i]->pict.data[1]);
1329         av_freep(&sub->rects[i]->pict.data[2]);
1330         av_freep(&sub->rects[i]->pict.data[3]);
1331         av_freep(&sub->rects[i]->text);
1332         av_freep(&sub->rects[i]->ass);
1333         av_freep(&sub->rects[i]);
1334     }
1335
1336     av_freep(&sub->rects);
1337
1338     memset(sub, 0, sizeof(AVSubtitle));
1339 }
1340
1341 av_cold int avcodec_close(AVCodecContext *avctx)
1342 {
1343     /* If there is a user-supplied mutex locking routine, call it. */
1344     if (ff_lockmgr_cb) {
1345         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1346             return -1;
1347     }
1348
1349     entangled_thread_counter++;
1350     if(entangled_thread_counter != 1){
1351         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1352         entangled_thread_counter--;
1353         return -1;
1354     }
1355
1356     if (avcodec_is_open(avctx)) {
1357         if (HAVE_THREADS && avctx->thread_opaque)
1358             ff_thread_free(avctx);
1359         if (avctx->codec && avctx->codec->close)
1360             avctx->codec->close(avctx);
1361         avcodec_default_free_buffers(avctx);
1362         avctx->coded_frame = NULL;
1363         av_freep(&avctx->internal);
1364     }
1365
1366     if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1367         av_opt_free(avctx->priv_data);
1368     av_opt_free(avctx);
1369     av_freep(&avctx->priv_data);
1370     if (av_codec_is_encoder(avctx->codec))
1371         av_freep(&avctx->extradata);
1372     avctx->codec = NULL;
1373     avctx->active_thread_type = 0;
1374     entangled_thread_counter--;
1375
1376     /* Release any user-supplied mutex. */
1377     if (ff_lockmgr_cb) {
1378         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1379     }
1380     return 0;
1381 }
1382
1383 AVCodec *avcodec_find_encoder(enum CodecID id)
1384 {
1385     AVCodec *p, *experimental=NULL;
1386     p = first_avcodec;
1387     while (p) {
1388         if (av_codec_is_encoder(p) && p->id == id) {
1389             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1390                 experimental = p;
1391             } else
1392                 return p;
1393         }
1394         p = p->next;
1395     }
1396     return experimental;
1397 }
1398
1399 AVCodec *avcodec_find_encoder_by_name(const char *name)
1400 {
1401     AVCodec *p;
1402     if (!name)
1403         return NULL;
1404     p = first_avcodec;
1405     while (p) {
1406         if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
1407             return p;
1408         p = p->next;
1409     }
1410     return NULL;
1411 }
1412
1413 AVCodec *avcodec_find_decoder(enum CodecID id)
1414 {
1415     AVCodec *p;
1416     p = first_avcodec;
1417     while (p) {
1418         if (av_codec_is_decoder(p) && p->id == id)
1419             return p;
1420         p = p->next;
1421     }
1422     return NULL;
1423 }
1424
1425 AVCodec *avcodec_find_decoder_by_name(const char *name)
1426 {
1427     AVCodec *p;
1428     if (!name)
1429         return NULL;
1430     p = first_avcodec;
1431     while (p) {
1432         if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
1433             return p;
1434         p = p->next;
1435     }
1436     return NULL;
1437 }
1438
1439 static int get_bit_rate(AVCodecContext *ctx)
1440 {
1441     int bit_rate;
1442     int bits_per_sample;
1443
1444     switch(ctx->codec_type) {
1445     case AVMEDIA_TYPE_VIDEO:
1446     case AVMEDIA_TYPE_DATA:
1447     case AVMEDIA_TYPE_SUBTITLE:
1448     case AVMEDIA_TYPE_ATTACHMENT:
1449         bit_rate = ctx->bit_rate;
1450         break;
1451     case AVMEDIA_TYPE_AUDIO:
1452         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1453         bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1454         break;
1455     default:
1456         bit_rate = 0;
1457         break;
1458     }
1459     return bit_rate;
1460 }
1461
1462 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1463 {
1464     int i, len, ret = 0;
1465
1466     for (i = 0; i < 4; i++) {
1467         len = snprintf(buf, buf_size,
1468                        isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1469         buf      += len;
1470         buf_size  = buf_size > len ? buf_size - len : 0;
1471         ret      += len;
1472         codec_tag>>=8;
1473     }
1474     return ret;
1475 }
1476
1477 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1478 {
1479     const char *codec_name;
1480     const char *profile = NULL;
1481     AVCodec *p;
1482     char buf1[32];
1483     int bitrate;
1484     AVRational display_aspect_ratio;
1485
1486     if (encode)
1487         p = avcodec_find_encoder(enc->codec_id);
1488     else
1489         p = avcodec_find_decoder(enc->codec_id);
1490
1491     if (p) {
1492         codec_name = p->name;
1493         profile = av_get_profile_name(p, enc->profile);
1494     } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
1495         /* fake mpeg2 transport stream codec (currently not
1496            registered) */
1497         codec_name = "mpeg2ts";
1498     } else if (enc->codec_name[0] != '\0') {
1499         codec_name = enc->codec_name;
1500     } else {
1501         /* output avi tags */
1502         char tag_buf[32];
1503         av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1504         snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1505         codec_name = buf1;
1506     }
1507
1508     switch(enc->codec_type) {
1509     case AVMEDIA_TYPE_VIDEO:
1510         snprintf(buf, buf_size,
1511                  "Video: %s%s",
1512                  codec_name, enc->mb_decision ? " (hq)" : "");
1513         if (profile)
1514             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1515                      " (%s)", profile);
1516         if (enc->pix_fmt != PIX_FMT_NONE) {
1517             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1518                      ", %s",
1519                      av_get_pix_fmt_name(enc->pix_fmt));
1520         }
1521         if (enc->width) {
1522             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1523                      ", %dx%d",
1524                      enc->width, enc->height);
1525             if (enc->sample_aspect_ratio.num) {
1526                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1527                           enc->width*enc->sample_aspect_ratio.num,
1528                           enc->height*enc->sample_aspect_ratio.den,
1529                           1024*1024);
1530                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1531                          " [PAR %d:%d DAR %d:%d]",
1532                          enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1533                          display_aspect_ratio.num, display_aspect_ratio.den);
1534             }
1535             if(av_log_get_level() >= AV_LOG_DEBUG){
1536                 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1537                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1538                      ", %d/%d",
1539                      enc->time_base.num/g, enc->time_base.den/g);
1540             }
1541         }
1542         if (encode) {
1543             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1544                      ", q=%d-%d", enc->qmin, enc->qmax);
1545         }
1546         break;
1547     case AVMEDIA_TYPE_AUDIO:
1548         snprintf(buf, buf_size,
1549                  "Audio: %s",
1550                  codec_name);
1551         if (profile)
1552             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1553                      " (%s)", profile);
1554         if (enc->sample_rate) {
1555             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1556                      ", %d Hz", enc->sample_rate);
1557         }
1558         av_strlcat(buf, ", ", buf_size);
1559         av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1560         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1561             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1562                      ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1563         }
1564         break;
1565     case AVMEDIA_TYPE_DATA:
1566         snprintf(buf, buf_size, "Data: %s", codec_name);
1567         break;
1568     case AVMEDIA_TYPE_SUBTITLE:
1569         snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1570         break;
1571     case AVMEDIA_TYPE_ATTACHMENT:
1572         snprintf(buf, buf_size, "Attachment: %s", codec_name);
1573         break;
1574     default:
1575         snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1576         return;
1577     }
1578     if (encode) {
1579         if (enc->flags & CODEC_FLAG_PASS1)
1580             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1581                      ", pass 1");
1582         if (enc->flags & CODEC_FLAG_PASS2)
1583             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1584                      ", pass 2");
1585     }
1586     bitrate = get_bit_rate(enc);
1587     if (bitrate != 0) {
1588         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1589                  ", %d kb/s", bitrate / 1000);
1590     }
1591 }
1592
1593 const char *av_get_profile_name(const AVCodec *codec, int profile)
1594 {
1595     const AVProfile *p;
1596     if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1597         return NULL;
1598
1599     for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1600         if (p->profile == profile)
1601             return p->name;
1602
1603     return NULL;
1604 }
1605
1606 unsigned avcodec_version( void )
1607 {
1608   return LIBAVCODEC_VERSION_INT;
1609 }
1610
1611 const char *avcodec_configuration(void)
1612 {
1613     return LIBAV_CONFIGURATION;
1614 }
1615
1616 const char *avcodec_license(void)
1617 {
1618 #define LICENSE_PREFIX "libavcodec license: "
1619     return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1620 }
1621
1622 void avcodec_flush_buffers(AVCodecContext *avctx)
1623 {
1624     if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1625         ff_thread_flush(avctx);
1626     else if(avctx->codec->flush)
1627         avctx->codec->flush(avctx);
1628 }
1629
1630 static void video_free_buffers(AVCodecContext *s)
1631 {
1632     AVCodecInternal *avci = s->internal;
1633     int i, j;
1634
1635     if (!avci->buffer)
1636         return;
1637
1638     if (avci->buffer_count)
1639         av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1640                avci->buffer_count);
1641     for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1642         InternalBuffer *buf = &avci->buffer[i];
1643         for(j=0; j<4; j++){
1644             av_freep(&buf->base[j]);
1645             buf->data[j]= NULL;
1646         }
1647     }
1648     av_freep(&avci->buffer);
1649
1650     avci->buffer_count=0;
1651 }
1652
1653 static void audio_free_buffers(AVCodecContext *avctx)
1654 {
1655     AVCodecInternal *avci = avctx->internal;
1656     InternalBuffer *buf;
1657
1658     if (!avci->buffer)
1659         return;
1660     buf = avci->buffer;
1661
1662     if (buf->extended_data) {
1663         av_free(buf->extended_data[0]);
1664         if (buf->extended_data != buf->data)
1665             av_free(buf->extended_data);
1666     }
1667     av_freep(&avci->buffer);
1668 }
1669
1670 void avcodec_default_free_buffers(AVCodecContext *avctx)
1671 {
1672     switch (avctx->codec_type) {
1673     case AVMEDIA_TYPE_VIDEO:
1674         video_free_buffers(avctx);
1675         break;
1676     case AVMEDIA_TYPE_AUDIO:
1677         audio_free_buffers(avctx);
1678         break;
1679     default:
1680         break;
1681     }
1682 }
1683
1684 int av_get_exact_bits_per_sample(enum CodecID codec_id)
1685 {
1686     switch(codec_id){
1687     case CODEC_ID_ADPCM_CT:
1688     case CODEC_ID_ADPCM_IMA_APC:
1689     case CODEC_ID_ADPCM_IMA_EA_SEAD:
1690     case CODEC_ID_ADPCM_IMA_WS:
1691     case CODEC_ID_ADPCM_G722:
1692     case CODEC_ID_ADPCM_YAMAHA:
1693         return 4;
1694     case CODEC_ID_PCM_ALAW:
1695     case CODEC_ID_PCM_MULAW:
1696     case CODEC_ID_PCM_S8:
1697     case CODEC_ID_PCM_U8:
1698     case CODEC_ID_PCM_ZORK:
1699         return 8;
1700     case CODEC_ID_PCM_S16BE:
1701     case CODEC_ID_PCM_S16LE:
1702     case CODEC_ID_PCM_S16LE_PLANAR:
1703     case CODEC_ID_PCM_U16BE:
1704     case CODEC_ID_PCM_U16LE:
1705         return 16;
1706     case CODEC_ID_PCM_S24DAUD:
1707     case CODEC_ID_PCM_S24BE:
1708     case CODEC_ID_PCM_S24LE:
1709     case CODEC_ID_PCM_U24BE:
1710     case CODEC_ID_PCM_U24LE:
1711         return 24;
1712     case CODEC_ID_PCM_S32BE:
1713     case CODEC_ID_PCM_S32LE:
1714     case CODEC_ID_PCM_U32BE:
1715     case CODEC_ID_PCM_U32LE:
1716     case CODEC_ID_PCM_F32BE:
1717     case CODEC_ID_PCM_F32LE:
1718         return 32;
1719     case CODEC_ID_PCM_F64BE:
1720     case CODEC_ID_PCM_F64LE:
1721         return 64;
1722     default:
1723         return 0;
1724     }
1725 }
1726
1727 int av_get_bits_per_sample(enum CodecID codec_id)
1728 {
1729     switch (codec_id) {
1730     case CODEC_ID_ADPCM_SBPRO_2:
1731         return 2;
1732     case CODEC_ID_ADPCM_SBPRO_3:
1733         return 3;
1734     case CODEC_ID_ADPCM_SBPRO_4:
1735     case CODEC_ID_ADPCM_IMA_WAV:
1736     case CODEC_ID_ADPCM_IMA_QT:
1737     case CODEC_ID_ADPCM_SWF:
1738     case CODEC_ID_ADPCM_MS:
1739         return 4;
1740     default:
1741         return av_get_exact_bits_per_sample(codec_id);
1742     }
1743 }
1744
1745 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
1746 {
1747     int id, sr, ch, ba, tag, bps;
1748
1749     id  = avctx->codec_id;
1750     sr  = avctx->sample_rate;
1751     ch  = avctx->channels;
1752     ba  = avctx->block_align;
1753     tag = avctx->codec_tag;
1754     bps = av_get_exact_bits_per_sample(avctx->codec_id);
1755
1756     /* codecs with an exact constant bits per sample */
1757     if (bps > 0 && ch > 0 && frame_bytes > 0)
1758         return (frame_bytes * 8) / (bps * ch);
1759     bps = avctx->bits_per_coded_sample;
1760
1761     /* codecs with a fixed packet duration */
1762     switch (id) {
1763     case CODEC_ID_ADPCM_ADX:    return   32;
1764     case CODEC_ID_ADPCM_IMA_QT: return   64;
1765     case CODEC_ID_ADPCM_EA_XAS: return  128;
1766     case CODEC_ID_AMR_NB:
1767     case CODEC_ID_GSM:
1768     case CODEC_ID_QCELP:
1769     case CODEC_ID_RA_144:
1770     case CODEC_ID_RA_288:       return  160;
1771     case CODEC_ID_IMC:          return  256;
1772     case CODEC_ID_AMR_WB:
1773     case CODEC_ID_GSM_MS:       return  320;
1774     case CODEC_ID_MP1:          return  384;
1775     case CODEC_ID_ATRAC1:       return  512;
1776     case CODEC_ID_ATRAC3:       return 1024;
1777     case CODEC_ID_MP2:
1778     case CODEC_ID_MUSEPACK7:    return 1152;
1779     case CODEC_ID_AC3:          return 1536;
1780     }
1781
1782     if (sr > 0) {
1783         /* calc from sample rate */
1784         if (id == CODEC_ID_TTA)
1785             return 256 * sr / 245;
1786
1787         if (ch > 0) {
1788             /* calc from sample rate and channels */
1789             if (id == CODEC_ID_BINKAUDIO_DCT)
1790                 return (480 << (sr / 22050)) / ch;
1791         }
1792     }
1793
1794     if (ba > 0) {
1795         /* calc from block_align */
1796         if (id == CODEC_ID_SIPR) {
1797             switch (ba) {
1798             case 20: return 160;
1799             case 19: return 144;
1800             case 29: return 288;
1801             case 37: return 480;
1802             }
1803         }
1804     }
1805
1806     if (frame_bytes > 0) {
1807         /* calc from frame_bytes only */
1808         if (id == CODEC_ID_TRUESPEECH)
1809             return 240 * (frame_bytes / 32);
1810         if (id == CODEC_ID_NELLYMOSER)
1811             return 256 * (frame_bytes / 64);
1812
1813         if (bps > 0) {
1814             /* calc from frame_bytes and bits_per_coded_sample */
1815             if (id == CODEC_ID_ADPCM_G726)
1816                 return frame_bytes * 8 / bps;
1817         }
1818
1819         if (ch > 0) {
1820             /* calc from frame_bytes and channels */
1821             switch (id) {
1822             case CODEC_ID_ADPCM_4XM:
1823             case CODEC_ID_ADPCM_IMA_ISS:
1824                 return (frame_bytes - 4 * ch) * 2 / ch;
1825             case CODEC_ID_ADPCM_IMA_SMJPEG:
1826                 return (frame_bytes - 4) * 2 / ch;
1827             case CODEC_ID_ADPCM_IMA_AMV:
1828                 return (frame_bytes - 8) * 2 / ch;
1829             case CODEC_ID_ADPCM_XA:
1830                 return (frame_bytes / 128) * 224 / ch;
1831             case CODEC_ID_INTERPLAY_DPCM:
1832                 return (frame_bytes - 6 - ch) / ch;
1833             case CODEC_ID_ROQ_DPCM:
1834                 return (frame_bytes - 8) / ch;
1835             case CODEC_ID_XAN_DPCM:
1836                 return (frame_bytes - 2 * ch) / ch;
1837             case CODEC_ID_MACE3:
1838                 return 3 * frame_bytes / ch;
1839             case CODEC_ID_MACE6:
1840                 return 6 * frame_bytes / ch;
1841             case CODEC_ID_PCM_LXF:
1842                 return 2 * (frame_bytes / (5 * ch));
1843             }
1844
1845             if (tag) {
1846                 /* calc from frame_bytes, channels, and codec_tag */
1847                 if (id == CODEC_ID_SOL_DPCM) {
1848                     if (tag == 3)
1849                         return frame_bytes / ch;
1850                     else
1851                         return frame_bytes * 2 / ch;
1852                 }
1853             }
1854
1855             if (ba > 0) {
1856                 /* calc from frame_bytes, channels, and block_align */
1857                 int blocks = frame_bytes / ba;
1858                 switch (avctx->codec_id) {
1859                 case CODEC_ID_ADPCM_IMA_WAV:
1860                     return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
1861                 case CODEC_ID_ADPCM_IMA_DK3:
1862                     return blocks * (((ba - 16) * 8 / 3) / ch);
1863                 case CODEC_ID_ADPCM_IMA_DK4:
1864                     return blocks * (1 + (ba - 4 * ch) * 2 / ch);
1865                 case CODEC_ID_ADPCM_MS:
1866                     return blocks * (2 + (ba - 7 * ch) * 2 / ch);
1867                 }
1868             }
1869
1870             if (bps > 0) {
1871                 /* calc from frame_bytes, channels, and bits_per_coded_sample */
1872                 switch (avctx->codec_id) {
1873                 case CODEC_ID_PCM_DVD:
1874                     return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
1875                 case CODEC_ID_PCM_BLURAY:
1876                     return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
1877                 case CODEC_ID_S302M:
1878                     return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
1879                 }
1880             }
1881         }
1882     }
1883
1884     return 0;
1885 }
1886
1887 #if !HAVE_THREADS
1888 int ff_thread_init(AVCodecContext *s){
1889     return -1;
1890 }
1891 #endif
1892
1893 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1894 {
1895     unsigned int n = 0;
1896
1897     while(v >= 0xff) {
1898         *s++ = 0xff;
1899         v -= 0xff;
1900         n++;
1901     }
1902     *s = v;
1903     n++;
1904     return n;
1905 }
1906
1907 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1908     int i;
1909     for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1910     return i;
1911 }
1912
1913 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1914 {
1915     av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1916             "version to the newest one from Git. If the problem still "
1917             "occurs, it means that your file has a feature which has not "
1918             "been implemented.\n", feature);
1919     if(want_sample)
1920         av_log_ask_for_sample(avc, NULL);
1921 }
1922
1923 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1924 {
1925     va_list argument_list;
1926
1927     va_start(argument_list, msg);
1928
1929     if (msg)
1930         av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1931     av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1932             "of this file to ftp://upload.libav.org/incoming/ "
1933             "and contact the libav-devel mailing list.\n");
1934
1935     va_end(argument_list);
1936 }
1937
1938 static AVHWAccel *first_hwaccel = NULL;
1939
1940 void av_register_hwaccel(AVHWAccel *hwaccel)
1941 {
1942     AVHWAccel **p = &first_hwaccel;
1943     while (*p)
1944         p = &(*p)->next;
1945     *p = hwaccel;
1946     hwaccel->next = NULL;
1947 }
1948
1949 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1950 {
1951     return hwaccel ? hwaccel->next : first_hwaccel;
1952 }
1953
1954 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1955 {
1956     AVHWAccel *hwaccel=NULL;
1957
1958     while((hwaccel= av_hwaccel_next(hwaccel))){
1959         if (   hwaccel->id      == codec_id
1960             && hwaccel->pix_fmt == pix_fmt)
1961             return hwaccel;
1962     }
1963     return NULL;
1964 }
1965
1966 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1967 {
1968     if (ff_lockmgr_cb) {
1969         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1970             return -1;
1971         if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1972             return -1;
1973     }
1974
1975     ff_lockmgr_cb = cb;
1976
1977     if (ff_lockmgr_cb) {
1978         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1979             return -1;
1980         if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1981             return -1;
1982     }
1983     return 0;
1984 }
1985
1986 int avpriv_lock_avformat(void)
1987 {
1988     if (ff_lockmgr_cb) {
1989         if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1990             return -1;
1991     }
1992     return 0;
1993 }
1994
1995 int avpriv_unlock_avformat(void)
1996 {
1997     if (ff_lockmgr_cb) {
1998         if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1999             return -1;
2000     }
2001     return 0;
2002 }
2003
2004 unsigned int avpriv_toupper4(unsigned int x)
2005 {
2006     return     toupper( x     &0xFF)
2007             + (toupper((x>>8 )&0xFF)<<8 )
2008             + (toupper((x>>16)&0xFF)<<16)
2009             + (toupper((x>>24)&0xFF)<<24);
2010 }
2011
2012 #if !HAVE_THREADS
2013
2014 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
2015 {
2016     f->owner = avctx;
2017     return avctx->get_buffer(avctx, f);
2018 }
2019
2020 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
2021 {
2022     f->owner->release_buffer(f->owner, f);
2023 }
2024
2025 void ff_thread_finish_setup(AVCodecContext *avctx)
2026 {
2027 }
2028
2029 void ff_thread_report_progress(AVFrame *f, int progress, int field)
2030 {
2031 }
2032
2033 void ff_thread_await_progress(AVFrame *f, int progress, int field)
2034 {
2035 }
2036
2037 #endif
2038
2039 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
2040 {
2041     if (codec_id <= CODEC_ID_NONE)
2042         return AVMEDIA_TYPE_UNKNOWN;
2043     else if (codec_id < CODEC_ID_FIRST_AUDIO)
2044         return AVMEDIA_TYPE_VIDEO;
2045     else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
2046         return AVMEDIA_TYPE_AUDIO;
2047     else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
2048         return AVMEDIA_TYPE_SUBTITLE;
2049
2050     return AVMEDIA_TYPE_UNKNOWN;
2051 }
2052
2053 int avcodec_is_open(AVCodecContext *s)
2054 {
2055     return !!s->internal;
2056 }