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