]> git.sesse.net Git - ffmpeg/blob - libavcodec/utils.c
lavc: add codec_descriptor field to AVCodecContext.
[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 == AV_CODEC_ID_SVQ1){
238             w_align=64;
239             h_align=64;
240         }
241     case PIX_FMT_RGB555:
242         if(s->codec_id == AV_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 == AV_CODEC_ID_SMC){
250             w_align=4;
251             h_align=4;
252         }
253         break;
254     case PIX_FMT_BGR24:
255         if((s->codec_id == AV_CODEC_ID_MSZH) || (s->codec_id == AV_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 == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_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 == AV_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 MAKE_ACCESSORS(AVCodecContext, codec, AVCodecDescriptor*, codec_descriptor)
716
717 static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
718 {
719     memset(sub, 0, sizeof(*sub));
720     sub->pts = AV_NOPTS_VALUE;
721 }
722
723 static int get_bit_rate(AVCodecContext *ctx)
724 {
725     int bit_rate;
726     int bits_per_sample;
727
728     switch(ctx->codec_type) {
729     case AVMEDIA_TYPE_VIDEO:
730     case AVMEDIA_TYPE_DATA:
731     case AVMEDIA_TYPE_SUBTITLE:
732     case AVMEDIA_TYPE_ATTACHMENT:
733         bit_rate = ctx->bit_rate;
734         break;
735     case AVMEDIA_TYPE_AUDIO:
736         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
737         bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
738         break;
739     default:
740         bit_rate = 0;
741         break;
742     }
743     return bit_rate;
744 }
745
746 #if FF_API_AVCODEC_OPEN
747 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
748 {
749     return avcodec_open2(avctx, codec, NULL);
750 }
751 #endif
752
753 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
754 {
755     int ret = 0;
756     AVDictionary *tmp = NULL;
757
758     if (avcodec_is_open(avctx))
759         return 0;
760
761     if ((!codec && !avctx->codec)) {
762         av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
763         return AVERROR(EINVAL);
764     }
765     if ((codec && avctx->codec && codec != avctx->codec)) {
766         av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
767                "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
768         return AVERROR(EINVAL);
769     }
770     if (!codec)
771         codec = avctx->codec;
772
773     if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
774         return AVERROR(EINVAL);
775
776     if (options)
777         av_dict_copy(&tmp, *options, 0);
778
779     /* If there is a user-supplied mutex locking routine, call it. */
780     if (ff_lockmgr_cb) {
781         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
782             return -1;
783     }
784
785     entangled_thread_counter++;
786     if(entangled_thread_counter != 1){
787         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
788         ret = -1;
789         goto end;
790     }
791
792     avctx->internal = av_mallocz(sizeof(AVCodecInternal));
793     if (!avctx->internal) {
794         ret = AVERROR(ENOMEM);
795         goto end;
796     }
797
798     if (codec->priv_data_size > 0) {
799       if(!avctx->priv_data){
800         avctx->priv_data = av_mallocz(codec->priv_data_size);
801         if (!avctx->priv_data) {
802             ret = AVERROR(ENOMEM);
803             goto end;
804         }
805         if (codec->priv_class) {
806             *(const AVClass**)avctx->priv_data= codec->priv_class;
807             av_opt_set_defaults(avctx->priv_data);
808         }
809       }
810       if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
811           goto free_and_end;
812     } else {
813         avctx->priv_data = NULL;
814     }
815     if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
816         goto free_and_end;
817
818     if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
819         if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
820             av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, try -strict -2\n");
821             ret = -1;
822             goto free_and_end;
823         }
824
825     //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
826     if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == AV_CODEC_ID_H264)){
827     if(avctx->coded_width && avctx->coded_height)
828         avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
829     else if(avctx->width && avctx->height)
830         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
831     }
832
833     if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
834         && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
835            || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
836         av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
837         avcodec_set_dimensions(avctx, 0, 0);
838     }
839
840     /* if the decoder init function was already called previously,
841        free the already allocated subtitle_header before overwriting it */
842     if (av_codec_is_decoder(codec))
843         av_freep(&avctx->subtitle_header);
844
845 #define SANE_NB_CHANNELS 128U
846     if (avctx->channels > SANE_NB_CHANNELS) {
847         ret = AVERROR(EINVAL);
848         goto free_and_end;
849     }
850
851     avctx->codec = codec;
852     if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
853         avctx->codec_id == AV_CODEC_ID_NONE) {
854         avctx->codec_type = codec->type;
855         avctx->codec_id   = codec->id;
856     }
857     if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
858                            && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
859         av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
860         ret = AVERROR(EINVAL);
861         goto free_and_end;
862     }
863     avctx->frame_number = 0;
864     avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
865
866     if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
867         (!avctx->time_base.num || !avctx->time_base.den)) {
868         avctx->time_base.num = 1;
869         avctx->time_base.den = avctx->sample_rate;
870     }
871
872     if (!HAVE_THREADS)
873         av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
874
875     entangled_thread_counter--; //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
876     ret = ff_frame_thread_encoder_init(avctx);
877     entangled_thread_counter++;
878     if (ret < 0)
879         goto free_and_end;
880
881     if (HAVE_THREADS && !avctx->thread_opaque
882         && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
883         ret = ff_thread_init(avctx);
884         if (ret < 0) {
885             goto free_and_end;
886         }
887     }
888     if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
889         avctx->thread_count = 1;
890
891     if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
892         av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
893                avctx->codec->max_lowres);
894         ret = AVERROR(EINVAL);
895         goto free_and_end;
896     }
897
898     if (av_codec_is_encoder(avctx->codec)) {
899         int i;
900         if (avctx->codec->sample_fmts) {
901             for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
902                 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
903                     break;
904             if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
905                 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
906                 ret = AVERROR(EINVAL);
907                 goto free_and_end;
908             }
909         }
910         if (avctx->codec->pix_fmts) {
911             for (i = 0; avctx->codec->pix_fmts[i] != PIX_FMT_NONE; i++)
912                 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
913                     break;
914             if (avctx->codec->pix_fmts[i] == PIX_FMT_NONE
915                 && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
916                      && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
917                 av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
918                 ret = AVERROR(EINVAL);
919                 goto free_and_end;
920             }
921         }
922         if (avctx->codec->supported_samplerates) {
923             for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
924                 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
925                     break;
926             if (avctx->codec->supported_samplerates[i] == 0) {
927                 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
928                 ret = AVERROR(EINVAL);
929                 goto free_and_end;
930             }
931         }
932         if (avctx->codec->channel_layouts) {
933             if (!avctx->channel_layout) {
934                 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
935             } else {
936                 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
937                     if (avctx->channel_layout == avctx->codec->channel_layouts[i])
938                         break;
939                 if (avctx->codec->channel_layouts[i] == 0) {
940                     av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
941                     ret = AVERROR(EINVAL);
942                     goto free_and_end;
943                 }
944             }
945         }
946         if (avctx->channel_layout && avctx->channels) {
947             if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
948                 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
949                 ret = AVERROR(EINVAL);
950                 goto free_and_end;
951             }
952         } else if (avctx->channel_layout) {
953             avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
954         }
955     }
956
957     avctx->pts_correction_num_faulty_pts =
958     avctx->pts_correction_num_faulty_dts = 0;
959     avctx->pts_correction_last_pts =
960     avctx->pts_correction_last_dts = INT64_MIN;
961
962     if(avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME) || avctx->internal->frame_thread_encoder)){
963         ret = avctx->codec->init(avctx);
964         if (ret < 0) {
965             goto free_and_end;
966         }
967     }
968
969     ret=0;
970
971     if (av_codec_is_decoder(avctx->codec)) {
972         if (!avctx->bit_rate)
973             avctx->bit_rate = get_bit_rate(avctx);
974         /* validate channel layout from the decoder */
975         if (avctx->channel_layout &&
976             av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
977             av_log(avctx, AV_LOG_WARNING, "channel layout does not match number of channels\n");
978             avctx->channel_layout = 0;
979         }
980     }
981 end:
982     entangled_thread_counter--;
983
984     /* Release any user-supplied mutex. */
985     if (ff_lockmgr_cb) {
986         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
987     }
988     if (options) {
989         av_dict_free(options);
990         *options = tmp;
991     }
992
993     return ret;
994 free_and_end:
995     av_dict_free(&tmp);
996     av_freep(&avctx->priv_data);
997     av_freep(&avctx->internal);
998     avctx->codec= NULL;
999     goto end;
1000 }
1001
1002 int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
1003 {
1004     if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
1005         av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
1006         return AVERROR(EINVAL);
1007     }
1008
1009     if (avctx) {
1010         av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
1011         if (!avpkt->data || avpkt->size < size) {
1012             av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
1013             avpkt->data = avctx->internal->byte_buffer;
1014             avpkt->size = avctx->internal->byte_buffer_size;
1015             avpkt->destruct = NULL;
1016         }
1017     }
1018
1019     if (avpkt->data) {
1020         void *destruct = avpkt->destruct;
1021
1022         if (avpkt->size < size) {
1023             av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
1024             return AVERROR(EINVAL);
1025         }
1026
1027         av_init_packet(avpkt);
1028         avpkt->destruct = destruct;
1029         avpkt->size = size;
1030         return 0;
1031     } else {
1032         int ret = av_new_packet(avpkt, size);
1033         if (ret < 0)
1034             av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
1035         return ret;
1036     }
1037 }
1038
1039 int ff_alloc_packet(AVPacket *avpkt, int size)
1040 {
1041     return ff_alloc_packet2(NULL, avpkt, size);
1042 }
1043
1044 /**
1045  * Pad last frame with silence.
1046  */
1047 static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
1048 {
1049     AVFrame *frame = NULL;
1050     uint8_t *buf   = NULL;
1051     int ret;
1052
1053     if (!(frame = avcodec_alloc_frame()))
1054         return AVERROR(ENOMEM);
1055     *frame = *src;
1056
1057     if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
1058                                           s->frame_size, s->sample_fmt, 0)) < 0)
1059         goto fail;
1060
1061     if (!(buf = av_malloc(ret))) {
1062         ret = AVERROR(ENOMEM);
1063         goto fail;
1064     }
1065
1066     frame->nb_samples = s->frame_size;
1067     if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
1068                                         buf, ret, 0)) < 0)
1069         goto fail;
1070     if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
1071                                src->nb_samples, s->channels, s->sample_fmt)) < 0)
1072         goto fail;
1073     if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
1074                                       frame->nb_samples - src->nb_samples,
1075                                       s->channels, s->sample_fmt)) < 0)
1076         goto fail;
1077
1078     *dst = frame;
1079
1080     return 0;
1081
1082 fail:
1083     if (frame->extended_data != frame->data)
1084         av_freep(&frame->extended_data);
1085     av_freep(&buf);
1086     av_freep(&frame);
1087     return ret;
1088 }
1089
1090 int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1091                                               AVPacket *avpkt,
1092                                               const AVFrame *frame,
1093                                               int *got_packet_ptr)
1094 {
1095     AVFrame tmp;
1096     AVFrame *padded_frame = NULL;
1097     int ret;
1098     AVPacket user_pkt = *avpkt;
1099     int needs_realloc = !user_pkt.data;
1100
1101     *got_packet_ptr = 0;
1102
1103     if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1104         av_free_packet(avpkt);
1105         av_init_packet(avpkt);
1106         return 0;
1107     }
1108
1109     /* ensure that extended_data is properly set */
1110     if (frame && !frame->extended_data) {
1111         if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
1112             avctx->channels > AV_NUM_DATA_POINTERS) {
1113             av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
1114                    "with more than %d channels, but extended_data is not set.\n",
1115                    AV_NUM_DATA_POINTERS);
1116             return AVERROR(EINVAL);
1117         }
1118         av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
1119
1120         tmp = *frame;
1121         tmp.extended_data = tmp.data;
1122         frame = &tmp;
1123     }
1124
1125     /* check for valid frame size */
1126     if (frame) {
1127         if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1128             if (frame->nb_samples > avctx->frame_size) {
1129                 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
1130                 return AVERROR(EINVAL);
1131             }
1132         } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
1133             if (frame->nb_samples < avctx->frame_size &&
1134                 !avctx->internal->last_audio_frame) {
1135                 ret = pad_last_frame(avctx, &padded_frame, frame);
1136                 if (ret < 0)
1137                     return ret;
1138
1139                 frame = padded_frame;
1140                 avctx->internal->last_audio_frame = 1;
1141             }
1142
1143             if (frame->nb_samples != avctx->frame_size) {
1144                 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
1145                 return AVERROR(EINVAL);
1146             }
1147         }
1148     }
1149
1150     ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1151     if (!ret) {
1152         if (*got_packet_ptr) {
1153             if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
1154                 if (avpkt->pts == AV_NOPTS_VALUE)
1155                     avpkt->pts = frame->pts;
1156                 if (!avpkt->duration)
1157                     avpkt->duration = ff_samples_to_time_base(avctx,
1158                                                               frame->nb_samples);
1159             }
1160             avpkt->dts = avpkt->pts;
1161         } else {
1162             avpkt->size = 0;
1163         }
1164     }
1165     if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1166         needs_realloc = 0;
1167         if (user_pkt.data) {
1168             if (user_pkt.size >= avpkt->size) {
1169                 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1170             } else {
1171                 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1172                 avpkt->size = user_pkt.size;
1173                 ret = -1;
1174             }
1175             avpkt->data     = user_pkt.data;
1176             avpkt->destruct = user_pkt.destruct;
1177         } else {
1178             if (av_dup_packet(avpkt) < 0) {
1179                 ret = AVERROR(ENOMEM);
1180             }
1181         }
1182     }
1183
1184     if (!ret) {
1185         if (needs_realloc && avpkt->data) {
1186             uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1187             if (new_data)
1188                 avpkt->data = new_data;
1189         }
1190
1191         avctx->frame_number++;
1192     }
1193
1194     if (ret < 0 || !*got_packet_ptr) {
1195         av_free_packet(avpkt);
1196         av_init_packet(avpkt);
1197         return ret;
1198     }
1199
1200     /* NOTE: if we add any audio encoders which output non-keyframe packets,
1201              this needs to be moved to the encoders, but for now we can do it
1202              here to simplify things */
1203     avpkt->flags |= AV_PKT_FLAG_KEY;
1204
1205     if (padded_frame) {
1206         av_freep(&padded_frame->data[0]);
1207         if (padded_frame->extended_data != padded_frame->data)
1208             av_freep(&padded_frame->extended_data);
1209         av_freep(&padded_frame);
1210     }
1211
1212     return ret;
1213 }
1214
1215 #if FF_API_OLD_DECODE_AUDIO
1216 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1217                                              uint8_t *buf, int buf_size,
1218                                              const short *samples)
1219 {
1220     AVPacket pkt;
1221     AVFrame frame0;
1222     AVFrame *frame;
1223     int ret, samples_size, got_packet;
1224
1225     av_init_packet(&pkt);
1226     pkt.data = buf;
1227     pkt.size = buf_size;
1228
1229     if (samples) {
1230         frame = &frame0;
1231         avcodec_get_frame_defaults(frame);
1232
1233         if (avctx->frame_size) {
1234             frame->nb_samples = avctx->frame_size;
1235         } else {
1236             /* if frame_size is not set, the number of samples must be
1237                calculated from the buffer size */
1238             int64_t nb_samples;
1239             if (!av_get_bits_per_sample(avctx->codec_id)) {
1240                 av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
1241                        "support this codec\n");
1242                 return AVERROR(EINVAL);
1243             }
1244             nb_samples = (int64_t)buf_size * 8 /
1245                          (av_get_bits_per_sample(avctx->codec_id) *
1246                          avctx->channels);
1247             if (nb_samples >= INT_MAX)
1248                 return AVERROR(EINVAL);
1249             frame->nb_samples = nb_samples;
1250         }
1251
1252         /* it is assumed that the samples buffer is large enough based on the
1253            relevant parameters */
1254         samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
1255                                                   frame->nb_samples,
1256                                                   avctx->sample_fmt, 1);
1257         if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
1258                                             avctx->sample_fmt,
1259                                             (const uint8_t *) samples,
1260                                             samples_size, 1)))
1261             return ret;
1262
1263         /* fabricate frame pts from sample count.
1264            this is needed because the avcodec_encode_audio() API does not have
1265            a way for the user to provide pts */
1266         if(avctx->sample_rate && avctx->time_base.num)
1267             frame->pts = ff_samples_to_time_base(avctx,
1268                                                 avctx->internal->sample_count);
1269         else
1270             frame->pts = AV_NOPTS_VALUE;
1271         avctx->internal->sample_count += frame->nb_samples;
1272     } else {
1273         frame = NULL;
1274     }
1275
1276     got_packet = 0;
1277     ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
1278     if (!ret && got_packet && avctx->coded_frame) {
1279         avctx->coded_frame->pts       = pkt.pts;
1280         avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1281     }
1282     /* free any side data since we cannot return it */
1283     ff_packet_free_side_data(&pkt);
1284
1285     if (frame && frame->extended_data != frame->data)
1286         av_freep(&frame->extended_data);
1287
1288     return ret ? ret : pkt.size;
1289 }
1290 #endif
1291
1292 #if FF_API_OLD_ENCODE_VIDEO
1293 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1294                          const AVFrame *pict)
1295 {
1296     AVPacket pkt;
1297     int ret, got_packet = 0;
1298
1299     if(buf_size < FF_MIN_BUFFER_SIZE){
1300         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
1301         return -1;
1302     }
1303
1304     av_init_packet(&pkt);
1305     pkt.data = buf;
1306     pkt.size = buf_size;
1307
1308     ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
1309     if (!ret && got_packet && avctx->coded_frame) {
1310         avctx->coded_frame->pts       = pkt.pts;
1311         avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
1312     }
1313
1314     /* free any side data since we cannot return it */
1315     if (pkt.side_data_elems > 0) {
1316         int i;
1317         for (i = 0; i < pkt.side_data_elems; i++)
1318             av_free(pkt.side_data[i].data);
1319         av_freep(&pkt.side_data);
1320         pkt.side_data_elems = 0;
1321     }
1322
1323     return ret ? ret : pkt.size;
1324 }
1325 #endif
1326
1327 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1328                                               AVPacket *avpkt,
1329                                               const AVFrame *frame,
1330                                               int *got_packet_ptr)
1331 {
1332     int ret;
1333     AVPacket user_pkt = *avpkt;
1334     int needs_realloc = !user_pkt.data;
1335
1336     *got_packet_ptr = 0;
1337
1338     if(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
1339         return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
1340
1341     if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
1342         av_free_packet(avpkt);
1343         av_init_packet(avpkt);
1344         avpkt->size     = 0;
1345         return 0;
1346     }
1347
1348     if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1349         return AVERROR(EINVAL);
1350
1351     av_assert0(avctx->codec->encode2);
1352
1353     ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1354     av_assert0(ret <= 0);
1355
1356     if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
1357         needs_realloc = 0;
1358         if (user_pkt.data) {
1359             if (user_pkt.size >= avpkt->size) {
1360                 memcpy(user_pkt.data, avpkt->data, avpkt->size);
1361             } else {
1362                 av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
1363                 avpkt->size = user_pkt.size;
1364                 ret = -1;
1365             }
1366             avpkt->data     = user_pkt.data;
1367             avpkt->destruct = user_pkt.destruct;
1368         } else {
1369             if (av_dup_packet(avpkt) < 0) {
1370                 ret = AVERROR(ENOMEM);
1371             }
1372         }
1373     }
1374
1375     if (!ret) {
1376         if (!*got_packet_ptr)
1377             avpkt->size = 0;
1378         else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1379             avpkt->pts = avpkt->dts = frame->pts;
1380
1381         if (needs_realloc && avpkt->data &&
1382             avpkt->destruct == av_destruct_packet) {
1383             uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
1384             if (new_data)
1385                 avpkt->data = new_data;
1386         }
1387
1388         avctx->frame_number++;
1389     }
1390
1391     if (ret < 0 || !*got_packet_ptr)
1392         av_free_packet(avpkt);
1393
1394     emms_c();
1395     return ret;
1396 }
1397
1398 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
1399                             const AVSubtitle *sub)
1400 {
1401     int ret;
1402     if(sub->start_display_time) {
1403         av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
1404         return -1;
1405     }
1406
1407     ret = avctx->codec->encode(avctx, buf, buf_size, (void *)(intptr_t)sub);
1408     avctx->frame_number++;
1409     return ret;
1410 }
1411
1412 /**
1413  * Attempt to guess proper monotonic timestamps for decoded video frames
1414  * which might have incorrect times. Input timestamps may wrap around, in
1415  * which case the output will as well.
1416  *
1417  * @param pts the pts field of the decoded AVPacket, as passed through
1418  * AVFrame.pkt_pts
1419  * @param dts the dts field of the decoded AVPacket
1420  * @return one of the input values, may be AV_NOPTS_VALUE
1421  */
1422 static int64_t guess_correct_pts(AVCodecContext *ctx,
1423                                  int64_t reordered_pts, int64_t dts)
1424 {
1425     int64_t pts = AV_NOPTS_VALUE;
1426
1427     if (dts != AV_NOPTS_VALUE) {
1428         ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
1429         ctx->pts_correction_last_dts = dts;
1430     }
1431     if (reordered_pts != AV_NOPTS_VALUE) {
1432         ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
1433         ctx->pts_correction_last_pts = reordered_pts;
1434     }
1435     if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
1436        && reordered_pts != AV_NOPTS_VALUE)
1437         pts = reordered_pts;
1438     else
1439         pts = dts;
1440
1441     return pts;
1442 }
1443
1444 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
1445 {
1446     int size = 0;
1447     const uint8_t *data;
1448     uint32_t flags;
1449
1450     if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
1451         return;
1452
1453     data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
1454     if (!data || size < 4)
1455         return;
1456     flags = bytestream_get_le32(&data);
1457     size -= 4;
1458     if (size < 4) /* Required for any of the changes */
1459         return;
1460     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
1461         avctx->channels = bytestream_get_le32(&data);
1462         size -= 4;
1463     }
1464     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
1465         if (size < 8)
1466             return;
1467         avctx->channel_layout = bytestream_get_le64(&data);
1468         size -= 8;
1469     }
1470     if (size < 4)
1471         return;
1472     if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
1473         avctx->sample_rate = bytestream_get_le32(&data);
1474         size -= 4;
1475     }
1476     if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
1477         if (size < 8)
1478             return;
1479         avctx->width  = bytestream_get_le32(&data);
1480         avctx->height = bytestream_get_le32(&data);
1481         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
1482         size -= 8;
1483     }
1484 }
1485
1486 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
1487                          int *got_picture_ptr,
1488                          const AVPacket *avpkt)
1489 {
1490     int ret;
1491     // copy to ensure we do not change avpkt
1492     AVPacket tmp = *avpkt;
1493
1494     if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
1495         av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
1496         return AVERROR(EINVAL);
1497     }
1498
1499     *got_picture_ptr= 0;
1500     if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
1501         return AVERROR(EINVAL);
1502
1503     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
1504         int did_split = av_packet_split_side_data(&tmp);
1505         apply_param_change(avctx, &tmp);
1506         avctx->pkt = &tmp;
1507         if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1508              ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
1509                                           &tmp);
1510         else {
1511             ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
1512                               &tmp);
1513             picture->pkt_dts= avpkt->dts;
1514
1515             if(!avctx->has_b_frames){
1516             picture->pkt_pos= avpkt->pos;
1517             }
1518             //FIXME these should be under if(!avctx->has_b_frames)
1519             if (!picture->sample_aspect_ratio.num)
1520                 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
1521             if (!picture->width)
1522                 picture->width = avctx->width;
1523             if (!picture->height)
1524                 picture->height = avctx->height;
1525             if (picture->format == PIX_FMT_NONE)
1526                 picture->format = avctx->pix_fmt;
1527         }
1528
1529         emms_c(); //needed to avoid an emms_c() call before every return;
1530
1531         avctx->pkt = NULL;
1532         if (did_split) {
1533             ff_packet_free_side_data(&tmp);
1534             if(ret == tmp.size)
1535                 ret = avpkt->size;
1536         }
1537
1538         if (*got_picture_ptr){
1539             avctx->frame_number++;
1540             picture->best_effort_timestamp = guess_correct_pts(avctx,
1541                                                             picture->pkt_pts,
1542                                                             picture->pkt_dts);
1543         }
1544     }else
1545         ret= 0;
1546
1547     return ret;
1548 }
1549
1550 #if FF_API_OLD_DECODE_AUDIO
1551 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
1552                          int *frame_size_ptr,
1553                          AVPacket *avpkt)
1554 {
1555     AVFrame frame;
1556     int ret, got_frame = 0;
1557
1558     if (avctx->get_buffer != avcodec_default_get_buffer) {
1559         av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
1560                "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
1561         av_log(avctx, AV_LOG_ERROR, "Please port your application to "
1562                "avcodec_decode_audio4()\n");
1563         avctx->get_buffer = avcodec_default_get_buffer;
1564         avctx->release_buffer = avcodec_default_release_buffer;
1565     }
1566
1567     ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
1568
1569     if (ret >= 0 && got_frame) {
1570         int ch, plane_size;
1571         int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
1572         int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
1573                                                    frame.nb_samples,
1574                                                    avctx->sample_fmt, 1);
1575         if (*frame_size_ptr < data_size) {
1576             av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
1577                    "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
1578             return AVERROR(EINVAL);
1579         }
1580
1581         memcpy(samples, frame.extended_data[0], plane_size);
1582
1583         if (planar && avctx->channels > 1) {
1584             uint8_t *out = ((uint8_t *)samples) + plane_size;
1585             for (ch = 1; ch < avctx->channels; ch++) {
1586                 memcpy(out, frame.extended_data[ch], plane_size);
1587                 out += plane_size;
1588             }
1589         }
1590         *frame_size_ptr = data_size;
1591     } else {
1592         *frame_size_ptr = 0;
1593     }
1594     return ret;
1595 }
1596 #endif
1597
1598 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
1599                                               AVFrame *frame,
1600                                               int *got_frame_ptr,
1601                                               const AVPacket *avpkt)
1602 {
1603     int ret = 0;
1604
1605     *got_frame_ptr = 0;
1606
1607     if (!avpkt->data && avpkt->size) {
1608         av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
1609         return AVERROR(EINVAL);
1610     }
1611     if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
1612         av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
1613         return AVERROR(EINVAL);
1614     }
1615
1616     if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
1617         uint8_t *side;
1618         int side_size;
1619         // copy to ensure we do not change avpkt
1620         AVPacket tmp = *avpkt;
1621         int did_split = av_packet_split_side_data(&tmp);
1622         apply_param_change(avctx, &tmp);
1623
1624         avctx->pkt = &tmp;
1625         ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
1626         if (ret >= 0 && *got_frame_ptr) {
1627             avctx->frame_number++;
1628             frame->pkt_dts = avpkt->dts;
1629             frame->best_effort_timestamp = guess_correct_pts(avctx,
1630                                                              frame->pkt_pts,
1631                                                              frame->pkt_dts);
1632             if (frame->format == AV_SAMPLE_FMT_NONE)
1633                 frame->format = avctx->sample_fmt;
1634             if (!frame->channel_layout)
1635                 frame->channel_layout = avctx->channel_layout;
1636             if (!frame->channels)
1637                 frame->channels = avctx->channels;
1638             if (!frame->sample_rate)
1639                 frame->sample_rate = avctx->sample_rate;
1640         }
1641
1642         side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
1643         if(side && side_size>=10) {
1644             avctx->internal->skip_samples = AV_RL32(side);
1645             av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
1646                    avctx->internal->skip_samples);
1647         }
1648         if (avctx->internal->skip_samples) {
1649             if(frame->nb_samples <= avctx->internal->skip_samples){
1650                 *got_frame_ptr = 0;
1651                 avctx->internal->skip_samples -= frame->nb_samples;
1652                 av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
1653                        avctx->internal->skip_samples);
1654             } else {
1655                 av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
1656                                 frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
1657                 if(avctx->pkt_timebase.num && avctx->sample_rate) {
1658                     int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
1659                                                    (AVRational){1, avctx->sample_rate},
1660                                                    avctx->pkt_timebase);
1661                     if(frame->pkt_pts!=AV_NOPTS_VALUE)
1662                         frame->pkt_pts += diff_ts;
1663                     if(frame->pkt_dts!=AV_NOPTS_VALUE)
1664                         frame->pkt_dts += diff_ts;
1665                     if (frame->pkt_duration >= diff_ts)
1666                         frame->pkt_duration -= diff_ts;
1667                 } else {
1668                     av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
1669                 }
1670                 av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
1671                        avctx->internal->skip_samples, frame->nb_samples);
1672                 frame->nb_samples -= avctx->internal->skip_samples;
1673                 avctx->internal->skip_samples = 0;
1674             }
1675         }
1676
1677         avctx->pkt = NULL;
1678         if (did_split) {
1679             ff_packet_free_side_data(&tmp);
1680             if(ret == tmp.size)
1681                 ret = avpkt->size;
1682         }
1683     }
1684     return ret;
1685 }
1686
1687 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1688                             int *got_sub_ptr,
1689                             AVPacket *avpkt)
1690 {
1691     int ret;
1692
1693     if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
1694         av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
1695         return AVERROR(EINVAL);
1696     }
1697
1698     avctx->pkt = avpkt;
1699     *got_sub_ptr = 0;
1700     avcodec_get_subtitle_defaults(sub);
1701     ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1702     if (*got_sub_ptr)
1703         avctx->frame_number++;
1704     return ret;
1705 }
1706
1707 void avsubtitle_free(AVSubtitle *sub)
1708 {
1709     int i;
1710
1711     for (i = 0; i < sub->num_rects; i++)
1712     {
1713         av_freep(&sub->rects[i]->pict.data[0]);
1714         av_freep(&sub->rects[i]->pict.data[1]);
1715         av_freep(&sub->rects[i]->pict.data[2]);
1716         av_freep(&sub->rects[i]->pict.data[3]);
1717         av_freep(&sub->rects[i]->text);
1718         av_freep(&sub->rects[i]->ass);
1719         av_freep(&sub->rects[i]);
1720     }
1721
1722     av_freep(&sub->rects);
1723
1724     memset(sub, 0, sizeof(AVSubtitle));
1725 }
1726
1727 av_cold int avcodec_close(AVCodecContext *avctx)
1728 {
1729     /* If there is a user-supplied mutex locking routine, call it. */
1730     if (ff_lockmgr_cb) {
1731         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1732             return -1;
1733     }
1734
1735     entangled_thread_counter++;
1736     if(entangled_thread_counter != 1){
1737         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1738         entangled_thread_counter--;
1739         return -1;
1740     }
1741
1742     if (avcodec_is_open(avctx)) {
1743         if (avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
1744             entangled_thread_counter --;
1745             ff_frame_thread_encoder_free(avctx);
1746             entangled_thread_counter ++;
1747         }
1748         if (HAVE_THREADS && avctx->thread_opaque)
1749             ff_thread_free(avctx);
1750         if (avctx->codec && avctx->codec->close)
1751             avctx->codec->close(avctx);
1752         avcodec_default_free_buffers(avctx);
1753         avctx->coded_frame = NULL;
1754         avctx->internal->byte_buffer_size = 0;
1755         av_freep(&avctx->internal->byte_buffer);
1756         av_freep(&avctx->internal);
1757     }
1758
1759     if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
1760         av_opt_free(avctx->priv_data);
1761     av_opt_free(avctx);
1762     av_freep(&avctx->priv_data);
1763     if (av_codec_is_encoder(avctx->codec))
1764         av_freep(&avctx->extradata);
1765     avctx->codec = NULL;
1766     avctx->active_thread_type = 0;
1767     entangled_thread_counter--;
1768
1769     /* Release any user-supplied mutex. */
1770     if (ff_lockmgr_cb) {
1771         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1772     }
1773     return 0;
1774 }
1775
1776 static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
1777 {
1778     switch(id){
1779         //This is for future deprecatec codec ids, its empty since
1780         //last major bump but will fill up again over time, please don't remove it
1781 //         case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
1782         default                         : return id;
1783     }
1784 }
1785
1786 AVCodec *avcodec_find_encoder(enum AVCodecID id)
1787 {
1788     AVCodec *p, *experimental=NULL;
1789     p = first_avcodec;
1790     id= remap_deprecated_codec_id(id);
1791     while (p) {
1792         if (av_codec_is_encoder(p) && p->id == id) {
1793             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1794                 experimental = p;
1795             } else
1796                 return p;
1797         }
1798         p = p->next;
1799     }
1800     return experimental;
1801 }
1802
1803 AVCodec *avcodec_find_encoder_by_name(const char *name)
1804 {
1805     AVCodec *p;
1806     if (!name)
1807         return NULL;
1808     p = first_avcodec;
1809     while (p) {
1810         if (av_codec_is_encoder(p) && strcmp(name,p->name) == 0)
1811             return p;
1812         p = p->next;
1813     }
1814     return NULL;
1815 }
1816
1817 AVCodec *avcodec_find_decoder(enum AVCodecID id)
1818 {
1819     AVCodec *p, *experimental=NULL;
1820     p = first_avcodec;
1821     id= remap_deprecated_codec_id(id);
1822     while (p) {
1823         if (av_codec_is_decoder(p) && p->id == id) {
1824             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1825                 experimental = p;
1826             } else
1827                 return p;
1828         }
1829         p = p->next;
1830     }
1831     return experimental;
1832 }
1833
1834 AVCodec *avcodec_find_decoder_by_name(const char *name)
1835 {
1836     AVCodec *p;
1837     if (!name)
1838         return NULL;
1839     p = first_avcodec;
1840     while (p) {
1841         if (av_codec_is_decoder(p) && strcmp(name,p->name) == 0)
1842             return p;
1843         p = p->next;
1844     }
1845     return NULL;
1846 }
1847
1848 const char *avcodec_get_name(enum AVCodecID id)
1849 {
1850     AVCodec *codec;
1851
1852 #if !CONFIG_SMALL
1853     switch (id) {
1854 #include "libavcodec/codec_names.h"
1855     }
1856     av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
1857 #endif
1858     codec = avcodec_find_decoder(id);
1859     if (codec)
1860         return codec->name;
1861     codec = avcodec_find_encoder(id);
1862     if (codec)
1863         return codec->name;
1864     return "unknown_codec";
1865 }
1866
1867 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1868 {
1869     int i, len, ret = 0;
1870
1871 #define IS_PRINT(x)                                               \
1872     (((x) >= '0' && (x) <= '9') ||                                \
1873      ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') ||  \
1874      ((x) == '.' || (x) == ' ' || (x) == '-'))
1875
1876     for (i = 0; i < 4; i++) {
1877         len = snprintf(buf, buf_size,
1878                        IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1879         buf      += len;
1880         buf_size  = buf_size > len ? buf_size - len : 0;
1881         ret      += len;
1882         codec_tag>>=8;
1883     }
1884     return ret;
1885 }
1886
1887 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1888 {
1889     const char *codec_type;
1890     const char *codec_name;
1891     const char *profile = NULL;
1892     AVCodec *p;
1893     int bitrate;
1894     AVRational display_aspect_ratio;
1895
1896     if (!buf || buf_size <= 0)
1897         return;
1898     codec_type = av_get_media_type_string(enc->codec_type);
1899     codec_name = avcodec_get_name(enc->codec_id);
1900     if (enc->profile != FF_PROFILE_UNKNOWN) {
1901         if (enc->codec)
1902             p = enc->codec;
1903         else
1904             p = encode ? avcodec_find_encoder(enc->codec_id) :
1905                         avcodec_find_decoder(enc->codec_id);
1906         if (p)
1907             profile = av_get_profile_name(p, enc->profile);
1908     }
1909
1910     snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
1911              codec_name, enc->mb_decision ? " (hq)" : "");
1912     buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
1913     if (profile)
1914         snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
1915     if (enc->codec_tag) {
1916         char tag_buf[32];
1917         av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1918         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1919                  " (%s / 0x%04X)", tag_buf, enc->codec_tag);
1920     }
1921     switch(enc->codec_type) {
1922     case AVMEDIA_TYPE_VIDEO:
1923         if (enc->pix_fmt != PIX_FMT_NONE) {
1924             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1925                      ", %s",
1926                      av_get_pix_fmt_name(enc->pix_fmt));
1927         }
1928         if (enc->width) {
1929             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1930                      ", %dx%d",
1931                      enc->width, enc->height);
1932             if (enc->sample_aspect_ratio.num) {
1933                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1934                           enc->width*enc->sample_aspect_ratio.num,
1935                           enc->height*enc->sample_aspect_ratio.den,
1936                           1024*1024);
1937                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1938                          " [SAR %d:%d DAR %d:%d]",
1939                          enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1940                          display_aspect_ratio.num, display_aspect_ratio.den);
1941             }
1942             if(av_log_get_level() >= AV_LOG_DEBUG){
1943                 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1944                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1945                      ", %d/%d",
1946                      enc->time_base.num/g, enc->time_base.den/g);
1947             }
1948         }
1949         if (encode) {
1950             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1951                      ", q=%d-%d", enc->qmin, enc->qmax);
1952         }
1953         break;
1954     case AVMEDIA_TYPE_AUDIO:
1955         if (enc->sample_rate) {
1956             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1957                      ", %d Hz", enc->sample_rate);
1958         }
1959         av_strlcat(buf, ", ", buf_size);
1960         av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1961         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1962             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1963                      ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1964         }
1965         break;
1966     default:
1967         return;
1968     }
1969     if (encode) {
1970         if (enc->flags & CODEC_FLAG_PASS1)
1971             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1972                      ", pass 1");
1973         if (enc->flags & CODEC_FLAG_PASS2)
1974             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1975                      ", pass 2");
1976     }
1977     bitrate = get_bit_rate(enc);
1978     if (bitrate != 0) {
1979         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1980                  ", %d kb/s", bitrate / 1000);
1981     }
1982 }
1983
1984 const char *av_get_profile_name(const AVCodec *codec, int profile)
1985 {
1986     const AVProfile *p;
1987     if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1988         return NULL;
1989
1990     for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1991         if (p->profile == profile)
1992             return p->name;
1993
1994     return NULL;
1995 }
1996
1997 unsigned avcodec_version( void )
1998 {
1999 //    av_assert0(AV_CODEC_ID_V410==164);
2000     av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
2001     av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
2002 //     av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
2003     av_assert0(AV_CODEC_ID_SRT==94216);
2004     av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
2005
2006   return LIBAVCODEC_VERSION_INT;
2007 }
2008
2009 const char *avcodec_configuration(void)
2010 {
2011     return FFMPEG_CONFIGURATION;
2012 }
2013
2014 const char *avcodec_license(void)
2015 {
2016 #define LICENSE_PREFIX "libavcodec license: "
2017     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
2018 }
2019
2020 void avcodec_flush_buffers(AVCodecContext *avctx)
2021 {
2022     if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
2023         ff_thread_flush(avctx);
2024     else if(avctx->codec->flush)
2025         avctx->codec->flush(avctx);
2026
2027     avctx->pts_correction_last_pts =
2028     avctx->pts_correction_last_dts = INT64_MIN;
2029 }
2030
2031 static void video_free_buffers(AVCodecContext *s)
2032 {
2033     AVCodecInternal *avci = s->internal;
2034     int i, j;
2035
2036     if (!avci->buffer)
2037         return;
2038
2039     if (avci->buffer_count)
2040         av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
2041                avci->buffer_count);
2042     for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
2043         InternalBuffer *buf = &avci->buffer[i];
2044         for(j=0; j<4; j++){
2045             av_freep(&buf->base[j]);
2046             buf->data[j]= NULL;
2047         }
2048     }
2049     av_freep(&avci->buffer);
2050
2051     avci->buffer_count=0;
2052 }
2053
2054 static void audio_free_buffers(AVCodecContext *avctx)
2055 {
2056     AVCodecInternal *avci = avctx->internal;
2057     InternalBuffer *buf;
2058
2059     if (!avci->buffer)
2060         return;
2061     buf = avci->buffer;
2062
2063     if (buf->extended_data) {
2064         av_free(buf->extended_data[0]);
2065         if (buf->extended_data != buf->data)
2066             av_freep(&buf->extended_data);
2067     }
2068     av_freep(&avci->buffer);
2069 }
2070
2071 void avcodec_default_free_buffers(AVCodecContext *avctx)
2072 {
2073     switch (avctx->codec_type) {
2074     case AVMEDIA_TYPE_VIDEO:
2075         video_free_buffers(avctx);
2076         break;
2077     case AVMEDIA_TYPE_AUDIO:
2078         audio_free_buffers(avctx);
2079         break;
2080     default:
2081         break;
2082     }
2083 }
2084
2085 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2086 {
2087     switch(codec_id){
2088     case AV_CODEC_ID_ADPCM_CT:
2089     case AV_CODEC_ID_ADPCM_IMA_APC:
2090     case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
2091     case AV_CODEC_ID_ADPCM_IMA_WS:
2092     case AV_CODEC_ID_ADPCM_G722:
2093     case AV_CODEC_ID_ADPCM_YAMAHA:
2094         return 4;
2095     case AV_CODEC_ID_PCM_ALAW:
2096     case AV_CODEC_ID_PCM_MULAW:
2097     case AV_CODEC_ID_PCM_S8:
2098     case AV_CODEC_ID_PCM_U8:
2099     case AV_CODEC_ID_PCM_ZORK:
2100         return 8;
2101     case AV_CODEC_ID_PCM_S16BE:
2102     case AV_CODEC_ID_PCM_S16LE:
2103     case AV_CODEC_ID_PCM_S16LE_PLANAR:
2104     case AV_CODEC_ID_PCM_U16BE:
2105     case AV_CODEC_ID_PCM_U16LE:
2106         return 16;
2107     case AV_CODEC_ID_PCM_S24DAUD:
2108     case AV_CODEC_ID_PCM_S24BE:
2109     case AV_CODEC_ID_PCM_S24LE:
2110     case AV_CODEC_ID_PCM_U24BE:
2111     case AV_CODEC_ID_PCM_U24LE:
2112         return 24;
2113     case AV_CODEC_ID_PCM_S32BE:
2114     case AV_CODEC_ID_PCM_S32LE:
2115     case AV_CODEC_ID_PCM_U32BE:
2116     case AV_CODEC_ID_PCM_U32LE:
2117     case AV_CODEC_ID_PCM_F32BE:
2118     case AV_CODEC_ID_PCM_F32LE:
2119         return 32;
2120     case AV_CODEC_ID_PCM_F64BE:
2121     case AV_CODEC_ID_PCM_F64LE:
2122         return 64;
2123     default:
2124         return 0;
2125     }
2126 }
2127
2128 enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
2129 {
2130     static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
2131         [AV_SAMPLE_FMT_U8  ] = { AV_CODEC_ID_PCM_U8,    AV_CODEC_ID_PCM_U8    },
2132         [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2133         [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2134         [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2135         [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2136         [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8,    AV_CODEC_ID_PCM_U8    },
2137         [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
2138         [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
2139         [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
2140         [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
2141     };
2142     if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
2143         return AV_CODEC_ID_NONE;
2144     if (be < 0 || be > 1)
2145         be = AV_NE(1, 0);
2146     return map[fmt][be];
2147 }
2148
2149 int av_get_bits_per_sample(enum AVCodecID codec_id)
2150 {
2151     switch (codec_id) {
2152     case AV_CODEC_ID_ADPCM_SBPRO_2:
2153         return 2;
2154     case AV_CODEC_ID_ADPCM_SBPRO_3:
2155         return 3;
2156     case AV_CODEC_ID_ADPCM_SBPRO_4:
2157     case AV_CODEC_ID_ADPCM_IMA_WAV:
2158     case AV_CODEC_ID_ADPCM_IMA_QT:
2159     case AV_CODEC_ID_ADPCM_SWF:
2160     case AV_CODEC_ID_ADPCM_MS:
2161         return 4;
2162     default:
2163         return av_get_exact_bits_per_sample(codec_id);
2164     }
2165 }
2166
2167 int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
2168 {
2169     int id, sr, ch, ba, tag, bps;
2170
2171     id  = avctx->codec_id;
2172     sr  = avctx->sample_rate;
2173     ch  = avctx->channels;
2174     ba  = avctx->block_align;
2175     tag = avctx->codec_tag;
2176     bps = av_get_exact_bits_per_sample(avctx->codec_id);
2177
2178     /* codecs with an exact constant bits per sample */
2179     if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
2180         return (frame_bytes * 8LL) / (bps * ch);
2181     bps = avctx->bits_per_coded_sample;
2182
2183     /* codecs with a fixed packet duration */
2184     switch (id) {
2185     case AV_CODEC_ID_ADPCM_ADX:    return   32;
2186     case AV_CODEC_ID_ADPCM_IMA_QT: return   64;
2187     case AV_CODEC_ID_ADPCM_EA_XAS: return  128;
2188     case AV_CODEC_ID_AMR_NB:
2189     case AV_CODEC_ID_GSM:
2190     case AV_CODEC_ID_QCELP:
2191     case AV_CODEC_ID_RA_144:
2192     case AV_CODEC_ID_RA_288:       return  160;
2193     case AV_CODEC_ID_IMC:          return  256;
2194     case AV_CODEC_ID_AMR_WB:
2195     case AV_CODEC_ID_GSM_MS:       return  320;
2196     case AV_CODEC_ID_MP1:          return  384;
2197     case AV_CODEC_ID_ATRAC1:       return  512;
2198     case AV_CODEC_ID_ATRAC3:       return 1024;
2199     case AV_CODEC_ID_MP2:
2200     case AV_CODEC_ID_MUSEPACK7:    return 1152;
2201     case AV_CODEC_ID_AC3:          return 1536;
2202     }
2203
2204     if (sr > 0) {
2205         /* calc from sample rate */
2206         if (id == AV_CODEC_ID_TTA)
2207             return 256 * sr / 245;
2208
2209         if (ch > 0) {
2210             /* calc from sample rate and channels */
2211             if (id == AV_CODEC_ID_BINKAUDIO_DCT)
2212                 return (480 << (sr / 22050)) / ch;
2213         }
2214     }
2215
2216     if (ba > 0) {
2217         /* calc from block_align */
2218         if (id == AV_CODEC_ID_SIPR) {
2219             switch (ba) {
2220             case 20: return 160;
2221             case 19: return 144;
2222             case 29: return 288;
2223             case 37: return 480;
2224             }
2225         } else if (id == AV_CODEC_ID_ILBC) {
2226             switch (ba) {
2227             case 38: return 160;
2228             case 50: return 240;
2229             }
2230         }
2231     }
2232
2233     if (frame_bytes > 0) {
2234         /* calc from frame_bytes only */
2235         if (id == AV_CODEC_ID_TRUESPEECH)
2236             return 240 * (frame_bytes / 32);
2237         if (id == AV_CODEC_ID_NELLYMOSER)
2238             return 256 * (frame_bytes / 64);
2239
2240         if (bps > 0) {
2241             /* calc from frame_bytes and bits_per_coded_sample */
2242             if (id == AV_CODEC_ID_ADPCM_G726)
2243                 return frame_bytes * 8 / bps;
2244         }
2245
2246         if (ch > 0) {
2247             /* calc from frame_bytes and channels */
2248             switch (id) {
2249             case AV_CODEC_ID_ADPCM_4XM:
2250             case AV_CODEC_ID_ADPCM_IMA_ISS:
2251                 return (frame_bytes - 4 * ch) * 2 / ch;
2252             case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
2253                 return (frame_bytes - 4) * 2 / ch;
2254             case AV_CODEC_ID_ADPCM_IMA_AMV:
2255                 return (frame_bytes - 8) * 2 / ch;
2256             case AV_CODEC_ID_ADPCM_XA:
2257                 return (frame_bytes / 128) * 224 / ch;
2258             case AV_CODEC_ID_INTERPLAY_DPCM:
2259                 return (frame_bytes - 6 - ch) / ch;
2260             case AV_CODEC_ID_ROQ_DPCM:
2261                 return (frame_bytes - 8) / ch;
2262             case AV_CODEC_ID_XAN_DPCM:
2263                 return (frame_bytes - 2 * ch) / ch;
2264             case AV_CODEC_ID_MACE3:
2265                 return 3 * frame_bytes / ch;
2266             case AV_CODEC_ID_MACE6:
2267                 return 6 * frame_bytes / ch;
2268             case AV_CODEC_ID_PCM_LXF:
2269                 return 2 * (frame_bytes / (5 * ch));
2270             }
2271
2272             if (tag) {
2273                 /* calc from frame_bytes, channels, and codec_tag */
2274                 if (id == AV_CODEC_ID_SOL_DPCM) {
2275                     if (tag == 3)
2276                         return frame_bytes / ch;
2277                     else
2278                         return frame_bytes * 2 / ch;
2279                 }
2280             }
2281
2282             if (ba > 0) {
2283                 /* calc from frame_bytes, channels, and block_align */
2284                 int blocks = frame_bytes / ba;
2285                 switch (avctx->codec_id) {
2286                 case AV_CODEC_ID_ADPCM_IMA_WAV:
2287                     return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
2288                 case AV_CODEC_ID_ADPCM_IMA_DK3:
2289                     return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
2290                 case AV_CODEC_ID_ADPCM_IMA_DK4:
2291                     return blocks * (1 + (ba - 4 * ch) * 2 / ch);
2292                 case AV_CODEC_ID_ADPCM_MS:
2293                     return blocks * (2 + (ba - 7 * ch) * 2 / ch);
2294                 }
2295             }
2296
2297             if (bps > 0) {
2298                 /* calc from frame_bytes, channels, and bits_per_coded_sample */
2299                 switch (avctx->codec_id) {
2300                 case AV_CODEC_ID_PCM_DVD:
2301                     if(bps<4)
2302                         return 0;
2303                     return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
2304                 case AV_CODEC_ID_PCM_BLURAY:
2305                     if(bps<4)
2306                         return 0;
2307                     return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
2308                 case AV_CODEC_ID_S302M:
2309                     return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
2310                 }
2311             }
2312         }
2313     }
2314
2315     return 0;
2316 }
2317
2318 #if !HAVE_THREADS
2319 int ff_thread_init(AVCodecContext *s){
2320     return -1;
2321 }
2322 #endif
2323
2324 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
2325 {
2326     unsigned int n = 0;
2327
2328     while(v >= 0xff) {
2329         *s++ = 0xff;
2330         v -= 0xff;
2331         n++;
2332     }
2333     *s = v;
2334     n++;
2335     return n;
2336 }
2337
2338 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
2339     int i;
2340     for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
2341     return i;
2342 }
2343
2344 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
2345 {
2346     av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
2347             "version to the newest one from Git. If the problem still "
2348             "occurs, it means that your file has a feature which has not "
2349             "been implemented.\n", feature);
2350     if(want_sample)
2351         av_log_ask_for_sample(avc, NULL);
2352 }
2353
2354 void av_log_ask_for_sample(void *avc, const char *msg, ...)
2355 {
2356     va_list argument_list;
2357
2358     va_start(argument_list, msg);
2359
2360     if (msg)
2361         av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
2362     av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
2363             "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
2364             "and contact the ffmpeg-devel mailing list.\n");
2365
2366     va_end(argument_list);
2367 }
2368
2369 static AVHWAccel *first_hwaccel = NULL;
2370
2371 void av_register_hwaccel(AVHWAccel *hwaccel)
2372 {
2373     AVHWAccel **p = &first_hwaccel;
2374     while (*p)
2375         p = &(*p)->next;
2376     *p = hwaccel;
2377     hwaccel->next = NULL;
2378 }
2379
2380 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
2381 {
2382     return hwaccel ? hwaccel->next : first_hwaccel;
2383 }
2384
2385 AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum PixelFormat pix_fmt)
2386 {
2387     AVHWAccel *hwaccel=NULL;
2388
2389     while((hwaccel= av_hwaccel_next(hwaccel))){
2390         if (   hwaccel->id      == codec_id
2391             && hwaccel->pix_fmt == pix_fmt)
2392             return hwaccel;
2393     }
2394     return NULL;
2395 }
2396
2397 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
2398 {
2399     if (ff_lockmgr_cb) {
2400         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
2401             return -1;
2402         if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
2403             return -1;
2404     }
2405
2406     ff_lockmgr_cb = cb;
2407
2408     if (ff_lockmgr_cb) {
2409         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
2410             return -1;
2411         if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
2412             return -1;
2413     }
2414     return 0;
2415 }
2416
2417 int avpriv_lock_avformat(void)
2418 {
2419     if (ff_lockmgr_cb) {
2420         if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
2421             return -1;
2422     }
2423     return 0;
2424 }
2425
2426 int avpriv_unlock_avformat(void)
2427 {
2428     if (ff_lockmgr_cb) {
2429         if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
2430             return -1;
2431     }
2432     return 0;
2433 }
2434
2435 unsigned int avpriv_toupper4(unsigned int x)
2436 {
2437     return     toupper( x     &0xFF)
2438             + (toupper((x>>8 )&0xFF)<<8 )
2439             + (toupper((x>>16)&0xFF)<<16)
2440             + (toupper((x>>24)&0xFF)<<24);
2441 }
2442
2443 #if !HAVE_THREADS
2444
2445 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
2446 {
2447     f->owner = avctx;
2448
2449     ff_init_buffer_info(avctx, f);
2450
2451     return avctx->get_buffer(avctx, f);
2452 }
2453
2454 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
2455 {
2456     f->owner->release_buffer(f->owner, f);
2457 }
2458
2459 void ff_thread_finish_setup(AVCodecContext *avctx)
2460 {
2461 }
2462
2463 void ff_thread_report_progress(AVFrame *f, int progress, int field)
2464 {
2465 }
2466
2467 void ff_thread_await_progress(AVFrame *f, int progress, int field)
2468 {
2469 }
2470
2471 int ff_thread_can_start_frame(AVCodecContext *avctx)
2472 {
2473     return 1;
2474 }
2475
2476 #endif
2477
2478 enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
2479 {
2480     AVCodec *c= avcodec_find_decoder(codec_id);
2481     if(!c)
2482         c= avcodec_find_encoder(codec_id);
2483     if(c)
2484         return c->type;
2485
2486     if (codec_id <= AV_CODEC_ID_NONE)
2487         return AVMEDIA_TYPE_UNKNOWN;
2488     else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
2489         return AVMEDIA_TYPE_VIDEO;
2490     else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
2491         return AVMEDIA_TYPE_AUDIO;
2492     else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
2493         return AVMEDIA_TYPE_SUBTITLE;
2494
2495     return AVMEDIA_TYPE_UNKNOWN;
2496 }
2497
2498 int avcodec_is_open(AVCodecContext *s)
2499 {
2500     return !!s->internal;
2501 }