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