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