]> git.sesse.net Git - ffmpeg/blob - libavcodec/utils.c
7c1a7aea2a858940c674d02fdca9ddfb49a8cf93
[ffmpeg] / libavcodec / utils.c
1 /*
2  * utils for libavcodec
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * utils.
26  */
27
28 #include "libavutil/avstring.h"
29 #include "libavutil/crc.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/audioconvert.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/samplefmt.h"
35 #include "libavutil/dict.h"
36 #include "avcodec.h"
37 #include "dsputil.h"
38 #include "libavutil/opt.h"
39 #include "imgconvert.h"
40 #include "thread.h"
41 #include "audioconvert.h"
42 #include "internal.h"
43 #include "bytestream.h"
44 #include <stdlib.h>
45 #include <stdarg.h>
46 #include <limits.h>
47 #include <float.h>
48
49 static int volatile entangled_thread_counter=0;
50 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
51 static void *codec_mutex;
52 static void *avformat_mutex;
53
54 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
55 {
56     if(min_size < *size)
57         return ptr;
58
59     min_size= FFMAX(17*min_size/16 + 32, min_size);
60
61     ptr= av_realloc(ptr, min_size);
62     if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
63         min_size= 0;
64
65     *size= min_size;
66
67     return ptr;
68 }
69
70 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
71 {
72     void **p = ptr;
73     if (min_size < *size)
74         return;
75     min_size= FFMAX(17*min_size/16 + 32, min_size);
76     av_free(*p);
77     *p = av_malloc(min_size);
78     if (!*p) min_size = 0;
79     *size= min_size;
80 }
81
82 /* encoder management */
83 static AVCodec *first_avcodec = NULL;
84
85 AVCodec *av_codec_next(AVCodec *c){
86     if(c) return c->next;
87     else  return first_avcodec;
88 }
89
90 #if !FF_API_AVCODEC_INIT
91 static
92 #endif
93 void avcodec_init(void)
94 {
95     static int initialized = 0;
96
97     if (initialized != 0)
98         return;
99     initialized = 1;
100
101     dsputil_static_init();
102 }
103
104 void avcodec_register(AVCodec *codec)
105 {
106     AVCodec **p;
107     avcodec_init();
108     p = &first_avcodec;
109     while (*p != NULL) p = &(*p)->next;
110     *p = codec;
111     codec->next = NULL;
112
113     if (codec->init_static_data)
114         codec->init_static_data(codec);
115 }
116
117 unsigned avcodec_get_edge_width(void)
118 {
119     return EDGE_WIDTH;
120 }
121
122 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
123     s->coded_width = width;
124     s->coded_height= height;
125     s->width = -((-width )>>s->lowres);
126     s->height= -((-height)>>s->lowres);
127 }
128
129 #define INTERNAL_BUFFER_SIZE (32+1)
130
131 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
132                                int linesize_align[AV_NUM_DATA_POINTERS])
133 {
134     int i;
135     int w_align= 1;
136     int h_align= 1;
137
138     switch(s->pix_fmt){
139     case PIX_FMT_YUV420P:
140     case PIX_FMT_YUYV422:
141     case PIX_FMT_UYVY422:
142     case PIX_FMT_YUV422P:
143     case PIX_FMT_YUV440P:
144     case PIX_FMT_YUV444P:
145     case PIX_FMT_GBRP:
146     case PIX_FMT_GRAY8:
147     case PIX_FMT_GRAY16BE:
148     case PIX_FMT_GRAY16LE:
149     case PIX_FMT_YUVJ420P:
150     case PIX_FMT_YUVJ422P:
151     case PIX_FMT_YUVJ440P:
152     case PIX_FMT_YUVJ444P:
153     case PIX_FMT_YUVA420P:
154     case PIX_FMT_YUV420P9LE:
155     case PIX_FMT_YUV420P9BE:
156     case PIX_FMT_YUV420P10LE:
157     case PIX_FMT_YUV420P10BE:
158     case PIX_FMT_YUV422P9LE:
159     case PIX_FMT_YUV422P9BE:
160     case PIX_FMT_YUV422P10LE:
161     case PIX_FMT_YUV422P10BE:
162     case PIX_FMT_YUV444P9LE:
163     case PIX_FMT_YUV444P9BE:
164     case PIX_FMT_YUV444P10LE:
165     case PIX_FMT_YUV444P10BE:
166     case PIX_FMT_GBRP9LE:
167     case PIX_FMT_GBRP9BE:
168     case PIX_FMT_GBRP10LE:
169     case PIX_FMT_GBRP10BE:
170         w_align = 16; //FIXME assume 16 pixel per macroblock
171         h_align = 16 * 2; // interlaced needs 2 macroblocks height
172         break;
173     case PIX_FMT_YUV411P:
174     case PIX_FMT_UYYVYY411:
175         w_align=32;
176         h_align=8;
177         break;
178     case PIX_FMT_YUV410P:
179         if(s->codec_id == CODEC_ID_SVQ1){
180             w_align=64;
181             h_align=64;
182         }
183     case PIX_FMT_RGB555:
184         if(s->codec_id == CODEC_ID_RPZA){
185             w_align=4;
186             h_align=4;
187         }
188     case PIX_FMT_PAL8:
189     case PIX_FMT_BGR8:
190     case PIX_FMT_RGB8:
191         if(s->codec_id == CODEC_ID_SMC){
192             w_align=4;
193             h_align=4;
194         }
195         break;
196     case PIX_FMT_BGR24:
197         if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
198             w_align=4;
199             h_align=4;
200         }
201         break;
202     default:
203         w_align= 1;
204         h_align= 1;
205         break;
206     }
207
208     *width = FFALIGN(*width , w_align);
209     *height= FFALIGN(*height, h_align);
210     if(s->codec_id == CODEC_ID_H264 || s->lowres)
211         *height+=2; // some of the optimized chroma MC reads one line too much
212                     // which is also done in mpeg decoders with lowres > 0
213
214     for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
215         linesize_align[i] = STRIDE_ALIGN;
216 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
217 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
218 //picture size unneccessarily in some cases. The solution here is not
219 //pretty and better ideas are welcome!
220 #if HAVE_MMX
221     if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
222        s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
223        s->codec_id == CODEC_ID_VP6A) {
224         for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
225             linesize_align[i] = 16;
226     }
227 #endif
228 }
229
230 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
231     int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
232     int linesize_align[AV_NUM_DATA_POINTERS];
233     int align;
234     avcodec_align_dimensions2(s, width, height, linesize_align);
235     align = FFMAX(linesize_align[0], linesize_align[3]);
236     linesize_align[1] <<= chroma_shift;
237     linesize_align[2] <<= chroma_shift;
238     align = FFMAX3(align, linesize_align[1], linesize_align[2]);
239     *width=FFALIGN(*width, align);
240 }
241
242 static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
243 {
244     AVCodecInternal *avci = avctx->internal;
245     InternalBuffer *buf;
246     int buf_size, ret, i, needs_extended_data;
247
248     buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
249                                           frame->nb_samples, avctx->sample_fmt,
250                                           32);
251     if (buf_size < 0)
252         return AVERROR(EINVAL);
253
254     needs_extended_data = av_sample_fmt_is_planar(avctx->sample_fmt) &&
255                           avctx->channels > AV_NUM_DATA_POINTERS;
256
257     /* allocate InternalBuffer if needed */
258     if (!avci->buffer) {
259         avci->buffer = av_mallocz(sizeof(InternalBuffer));
260         if (!avci->buffer)
261             return AVERROR(ENOMEM);
262     }
263     buf = avci->buffer;
264
265     /* if there is a previously-used internal buffer, check its size and
266        channel count to see if we can reuse it */
267     if (buf->extended_data) {
268         /* if current buffer is too small, free it */
269         if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
270             av_free(buf->extended_data[0]);
271             if (buf->extended_data != buf->data)
272                 av_free(&buf->extended_data);
273             buf->extended_data = NULL;
274             buf->data[0] = NULL;
275         }
276         /* if number of channels has changed, reset and/or free extended data
277            pointers but leave data buffer in buf->data[0] for reuse */
278         if (buf->nb_channels != avctx->channels) {
279             if (buf->extended_data != buf->data)
280                 av_free(buf->extended_data);
281             buf->extended_data = NULL;
282         }
283     }
284
285     /* if there is no previous buffer or the previous buffer cannot be used
286        as-is, allocate a new buffer and/or rearrange the channel pointers */
287     if (!buf->extended_data) {
288         /* if the channel pointers will fit, just set extended_data to data,
289            otherwise allocate the extended_data channel pointers */
290         if (needs_extended_data) {
291             buf->extended_data = av_mallocz(avctx->channels *
292                                             sizeof(*buf->extended_data));
293             if (!buf->extended_data)
294                 return AVERROR(ENOMEM);
295         } else {
296             buf->extended_data = buf->data;
297         }
298
299         /* if there is a previous buffer and it is large enough, reuse it and
300            just fill-in new channel pointers and linesize, otherwise allocate
301            a new buffer */
302         if (buf->extended_data[0]) {
303             ret = av_samples_fill_arrays(buf->extended_data, &buf->linesize[0],
304                                          buf->extended_data[0], avctx->channels,
305                                          frame->nb_samples, avctx->sample_fmt,
306                                          32);
307         } else {
308             ret = av_samples_alloc(buf->extended_data, &buf->linesize[0],
309                                    avctx->channels, frame->nb_samples,
310                                    avctx->sample_fmt, 32);
311         }
312         if (ret)
313             return ret;
314
315         /* if data was not used for extended_data, we need to copy as many of
316            the extended_data channel pointers as will fit */
317         if (needs_extended_data) {
318             for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
319                 buf->data[i] = buf->extended_data[i];
320         }
321         buf->audio_data_size = buf_size;
322         buf->nb_channels     = avctx->channels;
323     }
324
325     /* copy InternalBuffer info to the AVFrame */
326     frame->type          = FF_BUFFER_TYPE_INTERNAL;
327     frame->extended_data = buf->extended_data;
328     frame->linesize[0]   = buf->linesize[0];
329     memcpy(frame->data, buf->data, sizeof(frame->data));
330
331     if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
332     else            frame->pkt_pts = AV_NOPTS_VALUE;
333     frame->reordered_opaque = avctx->reordered_opaque;
334
335     if (avctx->debug & FF_DEBUG_BUFFERS)
336         av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
337                "internal audio buffer used\n", frame);
338
339     return 0;
340 }
341
342 static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
343 {
344     int i;
345     int w= s->width;
346     int h= s->height;
347     InternalBuffer *buf;
348     AVCodecInternal *avci = s->internal;
349
350     if(pic->data[0]!=NULL) {
351         av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
352         return -1;
353     }
354     if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
355         av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
356         return -1;
357     }
358
359     if(av_image_check_size(w, h, 0, s))
360         return -1;
361
362     if (!avci->buffer) {
363         avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
364                                   sizeof(InternalBuffer));
365     }
366
367     buf = &avci->buffer[avci->buffer_count];
368
369     if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
370         if(s->active_thread_type&FF_THREAD_FRAME) {
371             av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
372             return -1;
373         }
374
375         for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
376             av_freep(&buf->base[i]);
377             buf->data[i]= NULL;
378         }
379     }
380
381     if (!buf->base[0]) {
382         int h_chroma_shift, v_chroma_shift;
383         int size[4] = {0};
384         int tmpsize;
385         int unaligned;
386         AVPicture picture;
387         int stride_align[AV_NUM_DATA_POINTERS];
388         const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
389
390         avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
391
392         avcodec_align_dimensions2(s, &w, &h, stride_align);
393
394         if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
395             w+= EDGE_WIDTH*2;
396             h+= EDGE_WIDTH*2;
397         }
398
399         do {
400             // NOTE: do not align linesizes individually, this breaks e.g. assumptions
401             // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
402             av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
403             // increase alignment of w for next try (rhs gives the lowest bit set in w)
404             w += w & ~(w-1);
405
406             unaligned = 0;
407             for (i=0; i<4; i++){
408                 unaligned |= picture.linesize[i] % stride_align[i];
409             }
410         } while (unaligned);
411
412         tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
413         if (tmpsize < 0)
414             return -1;
415
416         for (i=0; i<3 && picture.data[i+1]; i++)
417             size[i] = picture.data[i+1] - picture.data[i];
418         size[i] = tmpsize - (picture.data[i] - picture.data[0]);
419
420         memset(buf->base, 0, sizeof(buf->base));
421         memset(buf->data, 0, sizeof(buf->data));
422
423         for(i=0; i<4 && size[i]; i++){
424             const int h_shift= i==0 ? 0 : h_chroma_shift;
425             const int v_shift= i==0 ? 0 : v_chroma_shift;
426
427             buf->linesize[i]= picture.linesize[i];
428
429             buf->base[i]= av_malloc(size[i]+16); //FIXME 16
430             if(buf->base[i]==NULL) return -1;
431             memset(buf->base[i], 128, size[i]);
432
433             // no edge if EDGE EMU or not planar YUV
434             if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
435                 buf->data[i] = buf->base[i];
436             else
437                 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
438         }
439         for (; i < AV_NUM_DATA_POINTERS; i++) {
440             buf->base[i] = buf->data[i] = NULL;
441             buf->linesize[i] = 0;
442         }
443         if(size[1] && !size[2])
444             ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
445         buf->width  = s->width;
446         buf->height = s->height;
447         buf->pix_fmt= s->pix_fmt;
448     }
449     pic->type= FF_BUFFER_TYPE_INTERNAL;
450
451     for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
452         pic->base[i]= buf->base[i];
453         pic->data[i]= buf->data[i];
454         pic->linesize[i]= buf->linesize[i];
455     }
456     pic->extended_data = pic->data;
457     avci->buffer_count++;
458
459     if(s->pkt) pic->pkt_pts= s->pkt->pts;
460     else       pic->pkt_pts= AV_NOPTS_VALUE;
461     pic->reordered_opaque= s->reordered_opaque;
462
463     if(s->debug&FF_DEBUG_BUFFERS)
464         av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
465                "buffers used\n", pic, avci->buffer_count);
466
467     return 0;
468 }
469
470 int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
471 {
472     switch (avctx->codec_type) {
473     case AVMEDIA_TYPE_VIDEO:
474         return video_get_buffer(avctx, frame);
475     case AVMEDIA_TYPE_AUDIO:
476         return audio_get_buffer(avctx, frame);
477     default:
478         return -1;
479     }
480 }
481
482 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
483     int i;
484     InternalBuffer *buf, *last;
485     AVCodecInternal *avci = s->internal;
486
487     assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
488
489     assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
490     assert(avci->buffer_count);
491
492     if (avci->buffer) {
493         buf = NULL; /* avoids warning */
494         for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
495             buf = &avci->buffer[i];
496             if (buf->data[0] == pic->data[0])
497                 break;
498         }
499         assert(i < avci->buffer_count);
500         avci->buffer_count--;
501         last = &avci->buffer[avci->buffer_count];
502
503         if (buf != last)
504             FFSWAP(InternalBuffer, *buf, *last);
505     }
506
507     for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
508         pic->data[i]=NULL;
509 //        pic->base[i]=NULL;
510     }
511 //printf("R%X\n", pic->opaque);
512
513     if(s->debug&FF_DEBUG_BUFFERS)
514         av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
515                "buffers used\n", pic, avci->buffer_count);
516 }
517
518 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
519     AVFrame temp_pic;
520     int i;
521
522     assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
523
524     /* If no picture return a new buffer */
525     if(pic->data[0] == NULL) {
526         /* We will copy from buffer, so must be readable */
527         pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
528         return s->get_buffer(s, pic);
529     }
530
531     /* If internal buffer type return the same buffer */
532     if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
533         if(s->pkt) pic->pkt_pts= s->pkt->pts;
534         else       pic->pkt_pts= AV_NOPTS_VALUE;
535         pic->reordered_opaque= s->reordered_opaque;
536         return 0;
537     }
538
539     /*
540      * Not internal type and reget_buffer not overridden, emulate cr buffer
541      */
542     temp_pic = *pic;
543     for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
544         pic->data[i] = pic->base[i] = NULL;
545     pic->opaque = NULL;
546     /* Allocate new frame */
547     if (s->get_buffer(s, pic))
548         return -1;
549     /* Copy image data from old buffer to new buffer */
550     av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
551              s->height);
552     s->release_buffer(s, &temp_pic); // Release old frame
553     return 0;
554 }
555
556 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
557     int i;
558
559     for(i=0; i<count; i++){
560         int r= func(c, (char*)arg + i*size);
561         if(ret) ret[i]= r;
562     }
563     return 0;
564 }
565
566 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
567     int i;
568
569     for(i=0; i<count; i++){
570         int r= func(c, arg, i, 0);
571         if(ret) ret[i]= r;
572     }
573     return 0;
574 }
575
576 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
577     while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
578         ++fmt;
579     return fmt[0];
580 }
581
582 void avcodec_get_frame_defaults(AVFrame *pic){
583     memset(pic, 0, sizeof(AVFrame));
584
585     pic->pts= AV_NOPTS_VALUE;
586     pic->key_frame= 1;
587     pic->sample_aspect_ratio = (AVRational){0, 1};
588 }
589
590 AVFrame *avcodec_alloc_frame(void){
591     AVFrame *pic= av_malloc(sizeof(AVFrame));
592
593     if(pic==NULL) return NULL;
594
595     avcodec_get_frame_defaults(pic);
596
597     return pic;
598 }
599
600 #if FF_API_AVCODEC_OPEN
601 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
602 {
603     return avcodec_open2(avctx, codec, NULL);
604 }
605 #endif
606
607 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
608 {
609     int ret = 0;
610     AVDictionary *tmp = NULL;
611
612     if (options)
613         av_dict_copy(&tmp, *options, 0);
614
615     /* If there is a user-supplied mutex locking routine, call it. */
616     if (ff_lockmgr_cb) {
617         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
618             return -1;
619     }
620
621     entangled_thread_counter++;
622     if(entangled_thread_counter != 1){
623         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
624         ret = -1;
625         goto end;
626     }
627
628     if(avctx->codec || !codec) {
629         ret = AVERROR(EINVAL);
630         goto end;
631     }
632
633     avctx->internal = av_mallocz(sizeof(AVCodecInternal));
634     if (!avctx->internal) {
635         ret = AVERROR(ENOMEM);
636         goto end;
637     }
638
639     if (codec->priv_data_size > 0) {
640       if(!avctx->priv_data){
641         avctx->priv_data = av_mallocz(codec->priv_data_size);
642         if (!avctx->priv_data) {
643             ret = AVERROR(ENOMEM);
644             goto end;
645         }
646         if (codec->priv_class) {
647             *(AVClass**)avctx->priv_data= codec->priv_class;
648             av_opt_set_defaults(avctx->priv_data);
649         }
650       }
651       if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
652           goto free_and_end;
653     } else {
654         avctx->priv_data = NULL;
655     }
656     if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
657         goto free_and_end;
658
659     if(avctx->coded_width && avctx->coded_height)
660         avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
661     else if(avctx->width && avctx->height)
662         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
663
664     if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
665         && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
666            || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
667         av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
668         avcodec_set_dimensions(avctx, 0, 0);
669     }
670
671     /* if the decoder init function was already called previously,
672        free the already allocated subtitle_header before overwriting it */
673     if (codec->decode)
674         av_freep(&avctx->subtitle_header);
675
676 #define SANE_NB_CHANNELS 128U
677     if (avctx->channels > SANE_NB_CHANNELS) {
678         ret = AVERROR(EINVAL);
679         goto free_and_end;
680     }
681
682     avctx->codec = codec;
683     if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
684         avctx->codec_id == CODEC_ID_NONE) {
685         avctx->codec_type = codec->type;
686         avctx->codec_id   = codec->id;
687     }
688     if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
689                            && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
690         av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
691         ret = AVERROR(EINVAL);
692         goto free_and_end;
693     }
694     avctx->frame_number = 0;
695 #if FF_API_ER
696
697     av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
698            avctx->error_recognition, avctx->err_recognition);
699     /* FF_ER_CAREFUL (==1) implies AV_EF_CRCCHECK (== 1<<1 - 1),
700        FF_ER_COMPLIANT (==2) implies AV_EF_{CRCCHECK,BITSTREAM} (== 1<<2 - 1), et cetera} */
701     avctx->err_recognition |= (1<<(avctx->error_recognition-(avctx->error_recognition>=FF_ER_VERY_AGGRESSIVE))) - 1;
702     av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
703            avctx->error_recognition, avctx->err_recognition);
704 #endif
705
706     if (HAVE_THREADS && !avctx->thread_opaque) {
707         ret = ff_thread_init(avctx);
708         if (ret < 0) {
709             goto free_and_end;
710         }
711     }
712
713     if (avctx->codec->max_lowres < avctx->lowres) {
714         av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
715                avctx->codec->max_lowres);
716         ret = AVERROR(EINVAL);
717         goto free_and_end;
718     }
719     if (avctx->codec->encode) {
720         int i;
721         if (avctx->codec->sample_fmts) {
722             for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
723                 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
724                     break;
725             if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
726                 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
727                 ret = AVERROR(EINVAL);
728                 goto free_and_end;
729             }
730         }
731         if (avctx->codec->supported_samplerates) {
732             for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
733                 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
734                     break;
735             if (avctx->codec->supported_samplerates[i] == 0) {
736                 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
737                 ret = AVERROR(EINVAL);
738                 goto free_and_end;
739             }
740         }
741         if (avctx->codec->channel_layouts) {
742             if (!avctx->channel_layout) {
743                 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
744             } else {
745                 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
746                     if (avctx->channel_layout == avctx->codec->channel_layouts[i])
747                         break;
748                 if (avctx->codec->channel_layouts[i] == 0) {
749                     av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
750                     ret = AVERROR(EINVAL);
751                     goto free_and_end;
752                 }
753             }
754         }
755         if (avctx->channel_layout && avctx->channels) {
756             if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
757                 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
758                 ret = AVERROR(EINVAL);
759                 goto free_and_end;
760             }
761         } else if (avctx->channel_layout) {
762             avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
763         }
764     }
765
766     if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
767         ret = avctx->codec->init(avctx);
768         if (ret < 0) {
769             goto free_and_end;
770         }
771     }
772 end:
773     entangled_thread_counter--;
774
775     /* Release any user-supplied mutex. */
776     if (ff_lockmgr_cb) {
777         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
778     }
779     if (options) {
780         av_dict_free(options);
781         *options = tmp;
782     }
783
784     return ret;
785 free_and_end:
786     av_dict_free(&tmp);
787     av_freep(&avctx->priv_data);
788     av_freep(&avctx->internal);
789     avctx->codec= NULL;
790     goto end;
791 }
792
793 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
794                          const short *samples)
795 {
796     if(buf_size < FF_MIN_BUFFER_SIZE && 0){
797         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
798         return -1;
799     }
800     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
801         int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
802         avctx->frame_number++;
803         return ret;
804     }else
805         return 0;
806 }
807
808 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
809                          const AVFrame *pict)
810 {
811     if(buf_size < FF_MIN_BUFFER_SIZE){
812         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
813         return -1;
814     }
815     if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
816         return -1;
817     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
818         int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
819         avctx->frame_number++;
820         emms_c(); //needed to avoid an emms_c() call before every return;
821
822         return ret;
823     }else
824         return 0;
825 }
826
827 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
828                             const AVSubtitle *sub)
829 {
830     int ret;
831     if(sub->start_display_time) {
832         av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
833         return -1;
834     }
835     if(sub->num_rects == 0 || !sub->rects)
836         return -1;
837     ret = avctx->codec->encode(avctx, buf, buf_size, sub);
838     avctx->frame_number++;
839     return ret;
840 }
841
842 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
843                          int *got_picture_ptr,
844                          AVPacket *avpkt)
845 {
846     int ret;
847
848     *got_picture_ptr= 0;
849     if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
850         return -1;
851
852     avctx->pkt = avpkt;
853
854     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
855         if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
856              ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
857                                           avpkt);
858         else {
859             ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
860                               avpkt);
861             picture->pkt_dts= avpkt->dts;
862             picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
863         }
864
865         emms_c(); //needed to avoid an emms_c() call before every return;
866
867         if (*got_picture_ptr)
868             avctx->frame_number++;
869     }else
870         ret= 0;
871
872     return ret;
873 }
874
875 #if FF_API_OLD_DECODE_AUDIO
876 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
877                          int *frame_size_ptr,
878                          AVPacket *avpkt)
879 {
880     AVFrame frame;
881     int ret, got_frame = 0;
882
883     if (avctx->get_buffer != avcodec_default_get_buffer) {
884         av_log(avctx, AV_LOG_ERROR, "A custom get_buffer() cannot be used with "
885                "avcodec_decode_audio3()\n");
886         return AVERROR(EINVAL);
887     }
888
889     ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
890
891     if (ret >= 0 && got_frame) {
892         int ch, plane_size;
893         int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
894         int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
895                                                    frame.nb_samples,
896                                                    avctx->sample_fmt, 1);
897         if (*frame_size_ptr < data_size) {
898             av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
899                    "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
900             return AVERROR(EINVAL);
901         }
902
903         memcpy(samples, frame.extended_data[0], plane_size);
904
905         if (planar && avctx->channels > 1) {
906             uint8_t *out = ((uint8_t *)samples) + plane_size;
907             for (ch = 1; ch < avctx->channels; ch++) {
908                 memcpy(out, frame.extended_data[ch], plane_size);
909                 out += plane_size;
910             }
911         }
912         *frame_size_ptr = data_size;
913     } else {
914         *frame_size_ptr = 0;
915     }
916     return ret;
917 }
918 #endif
919
920 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
921 {
922     int size = 0;
923     const uint8_t *data;
924     uint32_t flags;
925
926     if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
927         return;
928
929     data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
930     if (!data || size < 4)
931         return;
932     flags = bytestream_get_le32(&data);
933     size -= 4;
934     if (size < 4) /* Required for any of the changes */
935         return;
936     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
937         avctx->channels = bytestream_get_le32(&data);
938         size -= 4;
939     }
940     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
941         if (size < 8)
942             return;
943         avctx->channel_layout = bytestream_get_le64(&data);
944         size -= 8;
945     }
946     if (size < 4)
947         return;
948     if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
949         avctx->sample_rate = bytestream_get_le32(&data);
950         size -= 4;
951     }
952     if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
953         if (size < 8)
954             return;
955         avctx->width  = bytestream_get_le32(&data);
956         avctx->height = bytestream_get_le32(&data);
957         size -= 8;
958     }
959 }
960
961 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
962                                               AVFrame *frame,
963                                               int *got_frame_ptr,
964                                               AVPacket *avpkt)
965 {
966     int ret = 0;
967
968     *got_frame_ptr = 0;
969
970     avctx->pkt = avpkt;
971
972     if (!avpkt->data && avpkt->size) {
973         av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
974         return AVERROR(EINVAL);
975     }
976
977     apply_param_change(avctx, avpkt);
978
979     if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
980         ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
981         if (ret >= 0 && *got_frame_ptr) {
982             avctx->frame_number++;
983             frame->pkt_dts = avpkt->dts;
984         }
985     }
986     return ret;
987 }
988
989 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
990                             int *got_sub_ptr,
991                             AVPacket *avpkt)
992 {
993     int ret;
994
995     avctx->pkt = avpkt;
996     *got_sub_ptr = 0;
997     ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
998     if (*got_sub_ptr)
999         avctx->frame_number++;
1000     return ret;
1001 }
1002
1003 void avsubtitle_free(AVSubtitle *sub)
1004 {
1005     int i;
1006
1007     for (i = 0; i < sub->num_rects; i++)
1008     {
1009         av_freep(&sub->rects[i]->pict.data[0]);
1010         av_freep(&sub->rects[i]->pict.data[1]);
1011         av_freep(&sub->rects[i]->pict.data[2]);
1012         av_freep(&sub->rects[i]->pict.data[3]);
1013         av_freep(&sub->rects[i]->text);
1014         av_freep(&sub->rects[i]->ass);
1015         av_freep(&sub->rects[i]);
1016     }
1017
1018     av_freep(&sub->rects);
1019
1020     memset(sub, 0, sizeof(AVSubtitle));
1021 }
1022
1023 av_cold int avcodec_close(AVCodecContext *avctx)
1024 {
1025     /* If there is a user-supplied mutex locking routine, call it. */
1026     if (ff_lockmgr_cb) {
1027         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1028             return -1;
1029     }
1030
1031     entangled_thread_counter++;
1032     if(entangled_thread_counter != 1){
1033         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1034         entangled_thread_counter--;
1035         return -1;
1036     }
1037
1038     if (HAVE_THREADS && avctx->thread_opaque)
1039         ff_thread_free(avctx);
1040     if (avctx->codec && avctx->codec->close)
1041         avctx->codec->close(avctx);
1042     avcodec_default_free_buffers(avctx);
1043     avctx->coded_frame = NULL;
1044     av_freep(&avctx->internal);
1045     if (avctx->codec && avctx->codec->priv_class)
1046         av_opt_free(avctx->priv_data);
1047     av_opt_free(avctx);
1048     av_freep(&avctx->priv_data);
1049     if(avctx->codec && avctx->codec->encode)
1050         av_freep(&avctx->extradata);
1051     avctx->codec = NULL;
1052     avctx->active_thread_type = 0;
1053     entangled_thread_counter--;
1054
1055     /* Release any user-supplied mutex. */
1056     if (ff_lockmgr_cb) {
1057         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1058     }
1059     return 0;
1060 }
1061
1062 AVCodec *avcodec_find_encoder(enum CodecID id)
1063 {
1064     AVCodec *p, *experimental=NULL;
1065     p = first_avcodec;
1066     while (p) {
1067         if (p->encode != NULL && p->id == id) {
1068             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1069                 experimental = p;
1070             } else
1071                 return p;
1072         }
1073         p = p->next;
1074     }
1075     return experimental;
1076 }
1077
1078 AVCodec *avcodec_find_encoder_by_name(const char *name)
1079 {
1080     AVCodec *p;
1081     if (!name)
1082         return NULL;
1083     p = first_avcodec;
1084     while (p) {
1085         if (p->encode != NULL && strcmp(name,p->name) == 0)
1086             return p;
1087         p = p->next;
1088     }
1089     return NULL;
1090 }
1091
1092 AVCodec *avcodec_find_decoder(enum CodecID id)
1093 {
1094     AVCodec *p;
1095     p = first_avcodec;
1096     while (p) {
1097         if (p->decode != NULL && p->id == id)
1098             return p;
1099         p = p->next;
1100     }
1101     return NULL;
1102 }
1103
1104 AVCodec *avcodec_find_decoder_by_name(const char *name)
1105 {
1106     AVCodec *p;
1107     if (!name)
1108         return NULL;
1109     p = first_avcodec;
1110     while (p) {
1111         if (p->decode != NULL && strcmp(name,p->name) == 0)
1112             return p;
1113         p = p->next;
1114     }
1115     return NULL;
1116 }
1117
1118 static int get_bit_rate(AVCodecContext *ctx)
1119 {
1120     int bit_rate;
1121     int bits_per_sample;
1122
1123     switch(ctx->codec_type) {
1124     case AVMEDIA_TYPE_VIDEO:
1125     case AVMEDIA_TYPE_DATA:
1126     case AVMEDIA_TYPE_SUBTITLE:
1127     case AVMEDIA_TYPE_ATTACHMENT:
1128         bit_rate = ctx->bit_rate;
1129         break;
1130     case AVMEDIA_TYPE_AUDIO:
1131         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1132         bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1133         break;
1134     default:
1135         bit_rate = 0;
1136         break;
1137     }
1138     return bit_rate;
1139 }
1140
1141 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1142 {
1143     int i, len, ret = 0;
1144
1145     for (i = 0; i < 4; i++) {
1146         len = snprintf(buf, buf_size,
1147                        isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1148         buf      += len;
1149         buf_size  = buf_size > len ? buf_size - len : 0;
1150         ret      += len;
1151         codec_tag>>=8;
1152     }
1153     return ret;
1154 }
1155
1156 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1157 {
1158     const char *codec_name;
1159     const char *profile = NULL;
1160     AVCodec *p;
1161     char buf1[32];
1162     int bitrate;
1163     AVRational display_aspect_ratio;
1164
1165     if (encode)
1166         p = avcodec_find_encoder(enc->codec_id);
1167     else
1168         p = avcodec_find_decoder(enc->codec_id);
1169
1170     if (p) {
1171         codec_name = p->name;
1172         profile = av_get_profile_name(p, enc->profile);
1173     } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
1174         /* fake mpeg2 transport stream codec (currently not
1175            registered) */
1176         codec_name = "mpeg2ts";
1177     } else if (enc->codec_name[0] != '\0') {
1178         codec_name = enc->codec_name;
1179     } else {
1180         /* output avi tags */
1181         char tag_buf[32];
1182         av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1183         snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1184         codec_name = buf1;
1185     }
1186
1187     switch(enc->codec_type) {
1188     case AVMEDIA_TYPE_VIDEO:
1189         snprintf(buf, buf_size,
1190                  "Video: %s%s",
1191                  codec_name, enc->mb_decision ? " (hq)" : "");
1192         if (profile)
1193             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1194                      " (%s)", profile);
1195         if (enc->pix_fmt != PIX_FMT_NONE) {
1196             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1197                      ", %s",
1198                      av_get_pix_fmt_name(enc->pix_fmt));
1199         }
1200         if (enc->width) {
1201             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1202                      ", %dx%d",
1203                      enc->width, enc->height);
1204             if (enc->sample_aspect_ratio.num) {
1205                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1206                           enc->width*enc->sample_aspect_ratio.num,
1207                           enc->height*enc->sample_aspect_ratio.den,
1208                           1024*1024);
1209                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1210                          " [PAR %d:%d DAR %d:%d]",
1211                          enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1212                          display_aspect_ratio.num, display_aspect_ratio.den);
1213             }
1214             if(av_log_get_level() >= AV_LOG_DEBUG){
1215                 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1216                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1217                      ", %d/%d",
1218                      enc->time_base.num/g, enc->time_base.den/g);
1219             }
1220         }
1221         if (encode) {
1222             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1223                      ", q=%d-%d", enc->qmin, enc->qmax);
1224         }
1225         break;
1226     case AVMEDIA_TYPE_AUDIO:
1227         snprintf(buf, buf_size,
1228                  "Audio: %s",
1229                  codec_name);
1230         if (profile)
1231             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1232                      " (%s)", profile);
1233         if (enc->sample_rate) {
1234             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1235                      ", %d Hz", enc->sample_rate);
1236         }
1237         av_strlcat(buf, ", ", buf_size);
1238         av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1239         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1240             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1241                      ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1242         }
1243         break;
1244     case AVMEDIA_TYPE_DATA:
1245         snprintf(buf, buf_size, "Data: %s", codec_name);
1246         break;
1247     case AVMEDIA_TYPE_SUBTITLE:
1248         snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1249         break;
1250     case AVMEDIA_TYPE_ATTACHMENT:
1251         snprintf(buf, buf_size, "Attachment: %s", codec_name);
1252         break;
1253     default:
1254         snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1255         return;
1256     }
1257     if (encode) {
1258         if (enc->flags & CODEC_FLAG_PASS1)
1259             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1260                      ", pass 1");
1261         if (enc->flags & CODEC_FLAG_PASS2)
1262             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1263                      ", pass 2");
1264     }
1265     bitrate = get_bit_rate(enc);
1266     if (bitrate != 0) {
1267         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1268                  ", %d kb/s", bitrate / 1000);
1269     }
1270 }
1271
1272 const char *av_get_profile_name(const AVCodec *codec, int profile)
1273 {
1274     const AVProfile *p;
1275     if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1276         return NULL;
1277
1278     for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1279         if (p->profile == profile)
1280             return p->name;
1281
1282     return NULL;
1283 }
1284
1285 unsigned avcodec_version( void )
1286 {
1287   return LIBAVCODEC_VERSION_INT;
1288 }
1289
1290 const char *avcodec_configuration(void)
1291 {
1292     return LIBAV_CONFIGURATION;
1293 }
1294
1295 const char *avcodec_license(void)
1296 {
1297 #define LICENSE_PREFIX "libavcodec license: "
1298     return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1299 }
1300
1301 void avcodec_flush_buffers(AVCodecContext *avctx)
1302 {
1303     if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1304         ff_thread_flush(avctx);
1305     else if(avctx->codec->flush)
1306         avctx->codec->flush(avctx);
1307 }
1308
1309 static void video_free_buffers(AVCodecContext *s)
1310 {
1311     AVCodecInternal *avci = s->internal;
1312     int i, j;
1313
1314     if (!avci->buffer)
1315         return;
1316
1317     if (avci->buffer_count)
1318         av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1319                avci->buffer_count);
1320     for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1321         InternalBuffer *buf = &avci->buffer[i];
1322         for(j=0; j<4; j++){
1323             av_freep(&buf->base[j]);
1324             buf->data[j]= NULL;
1325         }
1326     }
1327     av_freep(&avci->buffer);
1328
1329     avci->buffer_count=0;
1330 }
1331
1332 static void audio_free_buffers(AVCodecContext *avctx)
1333 {
1334     AVCodecInternal *avci = avctx->internal;
1335     InternalBuffer *buf;
1336
1337     if (!avci->buffer)
1338         return;
1339     buf = avci->buffer;
1340
1341     if (buf->extended_data) {
1342         av_free(buf->extended_data[0]);
1343         if (buf->extended_data != buf->data)
1344             av_free(buf->extended_data);
1345     }
1346     av_freep(&avci->buffer);
1347 }
1348
1349 void avcodec_default_free_buffers(AVCodecContext *avctx)
1350 {
1351     switch (avctx->codec_type) {
1352     case AVMEDIA_TYPE_VIDEO:
1353         video_free_buffers(avctx);
1354         break;
1355     case AVMEDIA_TYPE_AUDIO:
1356         audio_free_buffers(avctx);
1357         break;
1358     default:
1359         break;
1360     }
1361 }
1362
1363 #if FF_API_OLD_FF_PICT_TYPES
1364 char av_get_pict_type_char(int pict_type){
1365     return av_get_picture_type_char(pict_type);
1366 }
1367 #endif
1368
1369 int av_get_bits_per_sample(enum CodecID codec_id){
1370     switch(codec_id){
1371     case CODEC_ID_ADPCM_SBPRO_2:
1372         return 2;
1373     case CODEC_ID_ADPCM_SBPRO_3:
1374         return 3;
1375     case CODEC_ID_ADPCM_SBPRO_4:
1376     case CODEC_ID_ADPCM_CT:
1377     case CODEC_ID_ADPCM_IMA_WAV:
1378     case CODEC_ID_ADPCM_IMA_QT:
1379     case CODEC_ID_ADPCM_SWF:
1380     case CODEC_ID_ADPCM_MS:
1381     case CODEC_ID_ADPCM_YAMAHA:
1382     case CODEC_ID_ADPCM_G722:
1383         return 4;
1384     case CODEC_ID_PCM_ALAW:
1385     case CODEC_ID_PCM_MULAW:
1386     case CODEC_ID_PCM_S8:
1387     case CODEC_ID_PCM_U8:
1388     case CODEC_ID_PCM_ZORK:
1389         return 8;
1390     case CODEC_ID_PCM_S16BE:
1391     case CODEC_ID_PCM_S16LE:
1392     case CODEC_ID_PCM_S16LE_PLANAR:
1393     case CODEC_ID_PCM_U16BE:
1394     case CODEC_ID_PCM_U16LE:
1395         return 16;
1396     case CODEC_ID_PCM_S24DAUD:
1397     case CODEC_ID_PCM_S24BE:
1398     case CODEC_ID_PCM_S24LE:
1399     case CODEC_ID_PCM_U24BE:
1400     case CODEC_ID_PCM_U24LE:
1401         return 24;
1402     case CODEC_ID_PCM_S32BE:
1403     case CODEC_ID_PCM_S32LE:
1404     case CODEC_ID_PCM_U32BE:
1405     case CODEC_ID_PCM_U32LE:
1406     case CODEC_ID_PCM_F32BE:
1407     case CODEC_ID_PCM_F32LE:
1408         return 32;
1409     case CODEC_ID_PCM_F64BE:
1410     case CODEC_ID_PCM_F64LE:
1411         return 64;
1412     default:
1413         return 0;
1414     }
1415 }
1416
1417 #if FF_API_OLD_SAMPLE_FMT
1418 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1419     return av_get_bytes_per_sample(sample_fmt) << 3;
1420 }
1421 #endif
1422
1423 #if !HAVE_THREADS
1424 int ff_thread_init(AVCodecContext *s){
1425     return -1;
1426 }
1427 #endif
1428
1429 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1430 {
1431     unsigned int n = 0;
1432
1433     while(v >= 0xff) {
1434         *s++ = 0xff;
1435         v -= 0xff;
1436         n++;
1437     }
1438     *s = v;
1439     n++;
1440     return n;
1441 }
1442
1443 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1444     int i;
1445     for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1446     return i;
1447 }
1448
1449 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1450 {
1451     av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1452             "version to the newest one from Git. If the problem still "
1453             "occurs, it means that your file has a feature which has not "
1454             "been implemented.\n", feature);
1455     if(want_sample)
1456         av_log_ask_for_sample(avc, NULL);
1457 }
1458
1459 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1460 {
1461     va_list argument_list;
1462
1463     va_start(argument_list, msg);
1464
1465     if (msg)
1466         av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1467     av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1468             "of this file to ftp://upload.libav.org/incoming/ "
1469             "and contact the libav-devel mailing list.\n");
1470
1471     va_end(argument_list);
1472 }
1473
1474 static AVHWAccel *first_hwaccel = NULL;
1475
1476 void av_register_hwaccel(AVHWAccel *hwaccel)
1477 {
1478     AVHWAccel **p = &first_hwaccel;
1479     while (*p)
1480         p = &(*p)->next;
1481     *p = hwaccel;
1482     hwaccel->next = NULL;
1483 }
1484
1485 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1486 {
1487     return hwaccel ? hwaccel->next : first_hwaccel;
1488 }
1489
1490 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1491 {
1492     AVHWAccel *hwaccel=NULL;
1493
1494     while((hwaccel= av_hwaccel_next(hwaccel))){
1495         if (   hwaccel->id      == codec_id
1496             && hwaccel->pix_fmt == pix_fmt)
1497             return hwaccel;
1498     }
1499     return NULL;
1500 }
1501
1502 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1503 {
1504     if (ff_lockmgr_cb) {
1505         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1506             return -1;
1507         if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1508             return -1;
1509     }
1510
1511     ff_lockmgr_cb = cb;
1512
1513     if (ff_lockmgr_cb) {
1514         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1515             return -1;
1516         if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1517             return -1;
1518     }
1519     return 0;
1520 }
1521
1522 int avpriv_lock_avformat(void)
1523 {
1524     if (ff_lockmgr_cb) {
1525         if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1526             return -1;
1527     }
1528     return 0;
1529 }
1530
1531 int avpriv_unlock_avformat(void)
1532 {
1533     if (ff_lockmgr_cb) {
1534         if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1535             return -1;
1536     }
1537     return 0;
1538 }
1539
1540 unsigned int avpriv_toupper4(unsigned int x)
1541 {
1542     return     toupper( x     &0xFF)
1543             + (toupper((x>>8 )&0xFF)<<8 )
1544             + (toupper((x>>16)&0xFF)<<16)
1545             + (toupper((x>>24)&0xFF)<<24);
1546 }
1547
1548 #if !HAVE_THREADS
1549
1550 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1551 {
1552     f->owner = avctx;
1553     return avctx->get_buffer(avctx, f);
1554 }
1555
1556 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1557 {
1558     f->owner->release_buffer(f->owner, f);
1559 }
1560
1561 void ff_thread_finish_setup(AVCodecContext *avctx)
1562 {
1563 }
1564
1565 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1566 {
1567 }
1568
1569 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1570 {
1571 }
1572
1573 #endif
1574
1575 #if FF_API_THREAD_INIT
1576 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1577 {
1578     s->thread_count = thread_count;
1579     return ff_thread_init(s);
1580 }
1581 #endif
1582
1583 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1584 {
1585     if (codec_id <= CODEC_ID_NONE)
1586         return AVMEDIA_TYPE_UNKNOWN;
1587     else if (codec_id < CODEC_ID_FIRST_AUDIO)
1588         return AVMEDIA_TYPE_VIDEO;
1589     else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1590         return AVMEDIA_TYPE_AUDIO;
1591     else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1592         return AVMEDIA_TYPE_SUBTITLE;
1593
1594     return AVMEDIA_TYPE_UNKNOWN;
1595 }