]> git.sesse.net Git - ffmpeg/blob - libavcodec/utils.c
mpegvideo_enc: ifdef out/replace references to deprecated codec flags.
[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     pic->format = -1;           /* unknown */
589 }
590
591 AVFrame *avcodec_alloc_frame(void){
592     AVFrame *pic= av_malloc(sizeof(AVFrame));
593
594     if(pic==NULL) return NULL;
595
596     avcodec_get_frame_defaults(pic);
597
598     return pic;
599 }
600
601 #if FF_API_AVCODEC_OPEN
602 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
603 {
604     return avcodec_open2(avctx, codec, NULL);
605 }
606 #endif
607
608 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
609 {
610     int ret = 0;
611     AVDictionary *tmp = NULL;
612
613     if (options)
614         av_dict_copy(&tmp, *options, 0);
615
616     /* If there is a user-supplied mutex locking routine, call it. */
617     if (ff_lockmgr_cb) {
618         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
619             return -1;
620     }
621
622     entangled_thread_counter++;
623     if(entangled_thread_counter != 1){
624         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
625         ret = -1;
626         goto end;
627     }
628
629     if(avctx->codec || !codec) {
630         ret = AVERROR(EINVAL);
631         goto end;
632     }
633
634     avctx->internal = av_mallocz(sizeof(AVCodecInternal));
635     if (!avctx->internal) {
636         ret = AVERROR(ENOMEM);
637         goto end;
638     }
639
640     if (codec->priv_data_size > 0) {
641       if(!avctx->priv_data){
642         avctx->priv_data = av_mallocz(codec->priv_data_size);
643         if (!avctx->priv_data) {
644             ret = AVERROR(ENOMEM);
645             goto end;
646         }
647         if (codec->priv_class) {
648             *(AVClass**)avctx->priv_data= codec->priv_class;
649             av_opt_set_defaults(avctx->priv_data);
650         }
651       }
652       if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
653           goto free_and_end;
654     } else {
655         avctx->priv_data = NULL;
656     }
657     if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
658         goto free_and_end;
659
660     if(avctx->coded_width && avctx->coded_height)
661         avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
662     else if(avctx->width && avctx->height)
663         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
664
665     if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
666         && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
667            || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
668         av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
669         avcodec_set_dimensions(avctx, 0, 0);
670     }
671
672     /* if the decoder init function was already called previously,
673        free the already allocated subtitle_header before overwriting it */
674     if (codec->decode)
675         av_freep(&avctx->subtitle_header);
676
677 #define SANE_NB_CHANNELS 128U
678     if (avctx->channels > SANE_NB_CHANNELS) {
679         ret = AVERROR(EINVAL);
680         goto free_and_end;
681     }
682
683     avctx->codec = codec;
684     if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
685         avctx->codec_id == CODEC_ID_NONE) {
686         avctx->codec_type = codec->type;
687         avctx->codec_id   = codec->id;
688     }
689     if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
690                            && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
691         av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
692         ret = AVERROR(EINVAL);
693         goto free_and_end;
694     }
695     avctx->frame_number = 0;
696 #if FF_API_ER
697
698     av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
699            avctx->error_recognition, avctx->err_recognition);
700     /* FF_ER_CAREFUL (==1) implies AV_EF_CRCCHECK (== 1<<1 - 1),
701        FF_ER_COMPLIANT (==2) implies AV_EF_{CRCCHECK,BITSTREAM} (== 1<<2 - 1), et cetera} */
702     avctx->err_recognition |= (1<<(avctx->error_recognition-(avctx->error_recognition>=FF_ER_VERY_AGGRESSIVE))) - 1;
703     av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
704            avctx->error_recognition, avctx->err_recognition);
705 #endif
706
707     if (HAVE_THREADS && !avctx->thread_opaque) {
708         ret = ff_thread_init(avctx);
709         if (ret < 0) {
710             goto free_and_end;
711         }
712     }
713     if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
714         avctx->thread_count = 1;
715
716     if (avctx->codec->max_lowres < avctx->lowres) {
717         av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
718                avctx->codec->max_lowres);
719         ret = AVERROR(EINVAL);
720         goto free_and_end;
721     }
722     if (avctx->codec->encode) {
723         int i;
724         if (avctx->codec->sample_fmts) {
725             for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
726                 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
727                     break;
728             if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
729                 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
730                 ret = AVERROR(EINVAL);
731                 goto free_and_end;
732             }
733         }
734         if (avctx->codec->supported_samplerates) {
735             for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
736                 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
737                     break;
738             if (avctx->codec->supported_samplerates[i] == 0) {
739                 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
740                 ret = AVERROR(EINVAL);
741                 goto free_and_end;
742             }
743         }
744         if (avctx->codec->channel_layouts) {
745             if (!avctx->channel_layout) {
746                 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
747             } else {
748                 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
749                     if (avctx->channel_layout == avctx->codec->channel_layouts[i])
750                         break;
751                 if (avctx->codec->channel_layouts[i] == 0) {
752                     av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
753                     ret = AVERROR(EINVAL);
754                     goto free_and_end;
755                 }
756             }
757         }
758         if (avctx->channel_layout && avctx->channels) {
759             if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
760                 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
761                 ret = AVERROR(EINVAL);
762                 goto free_and_end;
763             }
764         } else if (avctx->channel_layout) {
765             avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
766         }
767     }
768
769     if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
770         ret = avctx->codec->init(avctx);
771         if (ret < 0) {
772             goto free_and_end;
773         }
774     }
775 end:
776     entangled_thread_counter--;
777
778     /* Release any user-supplied mutex. */
779     if (ff_lockmgr_cb) {
780         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
781     }
782     if (options) {
783         av_dict_free(options);
784         *options = tmp;
785     }
786
787     return ret;
788 free_and_end:
789     av_dict_free(&tmp);
790     av_freep(&avctx->priv_data);
791     av_freep(&avctx->internal);
792     avctx->codec= NULL;
793     goto end;
794 }
795
796 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
797                          const short *samples)
798 {
799     if(buf_size < FF_MIN_BUFFER_SIZE && 0){
800         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
801         return -1;
802     }
803     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
804         int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
805         avctx->frame_number++;
806         return ret;
807     }else
808         return 0;
809 }
810
811 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
812                          const AVFrame *pict)
813 {
814     if(buf_size < FF_MIN_BUFFER_SIZE){
815         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
816         return -1;
817     }
818     if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
819         return -1;
820     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
821         int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
822         avctx->frame_number++;
823         emms_c(); //needed to avoid an emms_c() call before every return;
824
825         return ret;
826     }else
827         return 0;
828 }
829
830 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
831                             const AVSubtitle *sub)
832 {
833     int ret;
834     if(sub->start_display_time) {
835         av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
836         return -1;
837     }
838     if(sub->num_rects == 0 || !sub->rects)
839         return -1;
840     ret = avctx->codec->encode(avctx, buf, buf_size, sub);
841     avctx->frame_number++;
842     return ret;
843 }
844
845 static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
846 {
847     int size = 0;
848     const uint8_t *data;
849     uint32_t flags;
850
851     if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
852         return;
853
854     data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
855     if (!data || size < 4)
856         return;
857     flags = bytestream_get_le32(&data);
858     size -= 4;
859     if (size < 4) /* Required for any of the changes */
860         return;
861     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
862         avctx->channels = bytestream_get_le32(&data);
863         size -= 4;
864     }
865     if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
866         if (size < 8)
867             return;
868         avctx->channel_layout = bytestream_get_le64(&data);
869         size -= 8;
870     }
871     if (size < 4)
872         return;
873     if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
874         avctx->sample_rate = bytestream_get_le32(&data);
875         size -= 4;
876     }
877     if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
878         if (size < 8)
879             return;
880         avctx->width  = bytestream_get_le32(&data);
881         avctx->height = bytestream_get_le32(&data);
882         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
883         size -= 8;
884     }
885 }
886
887 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
888                          int *got_picture_ptr,
889                          AVPacket *avpkt)
890 {
891     int ret;
892
893     *got_picture_ptr= 0;
894     if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
895         return -1;
896
897     avctx->pkt = avpkt;
898     apply_param_change(avctx, avpkt);
899
900     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
901         if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
902              ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
903                                           avpkt);
904         else {
905             ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
906                               avpkt);
907             picture->pkt_dts= avpkt->dts;
908             picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
909             picture->width  = avctx->width;
910             picture->height = avctx->height;
911             picture->format = avctx->pix_fmt;
912         }
913
914         emms_c(); //needed to avoid an emms_c() call before every return;
915
916         if (*got_picture_ptr)
917             avctx->frame_number++;
918     }else
919         ret= 0;
920
921     return ret;
922 }
923
924 #if FF_API_OLD_DECODE_AUDIO
925 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
926                          int *frame_size_ptr,
927                          AVPacket *avpkt)
928 {
929     AVFrame frame;
930     int ret, got_frame = 0;
931
932     if (avctx->get_buffer != avcodec_default_get_buffer) {
933         av_log(avctx, AV_LOG_ERROR, "A custom get_buffer() cannot be used with "
934                "avcodec_decode_audio3()\n");
935         return AVERROR(EINVAL);
936     }
937
938     ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
939
940     if (ret >= 0 && got_frame) {
941         int ch, plane_size;
942         int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
943         int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
944                                                    frame.nb_samples,
945                                                    avctx->sample_fmt, 1);
946         if (*frame_size_ptr < data_size) {
947             av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
948                    "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
949             return AVERROR(EINVAL);
950         }
951
952         memcpy(samples, frame.extended_data[0], plane_size);
953
954         if (planar && avctx->channels > 1) {
955             uint8_t *out = ((uint8_t *)samples) + plane_size;
956             for (ch = 1; ch < avctx->channels; ch++) {
957                 memcpy(out, frame.extended_data[ch], plane_size);
958                 out += plane_size;
959             }
960         }
961         *frame_size_ptr = data_size;
962     } else {
963         *frame_size_ptr = 0;
964     }
965     return ret;
966 }
967 #endif
968
969 int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
970                                               AVFrame *frame,
971                                               int *got_frame_ptr,
972                                               AVPacket *avpkt)
973 {
974     int ret = 0;
975
976     *got_frame_ptr = 0;
977
978     avctx->pkt = avpkt;
979
980     if (!avpkt->data && avpkt->size) {
981         av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
982         return AVERROR(EINVAL);
983     }
984
985     apply_param_change(avctx, avpkt);
986
987     if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
988         ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
989         if (ret >= 0 && *got_frame_ptr) {
990             avctx->frame_number++;
991             frame->pkt_dts = avpkt->dts;
992             if (frame->format == AV_SAMPLE_FMT_NONE)
993                 frame->format = avctx->sample_fmt;
994         }
995     }
996     return ret;
997 }
998
999 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
1000                             int *got_sub_ptr,
1001                             AVPacket *avpkt)
1002 {
1003     int ret;
1004
1005     avctx->pkt = avpkt;
1006     *got_sub_ptr = 0;
1007     ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
1008     if (*got_sub_ptr)
1009         avctx->frame_number++;
1010     return ret;
1011 }
1012
1013 void avsubtitle_free(AVSubtitle *sub)
1014 {
1015     int i;
1016
1017     for (i = 0; i < sub->num_rects; i++)
1018     {
1019         av_freep(&sub->rects[i]->pict.data[0]);
1020         av_freep(&sub->rects[i]->pict.data[1]);
1021         av_freep(&sub->rects[i]->pict.data[2]);
1022         av_freep(&sub->rects[i]->pict.data[3]);
1023         av_freep(&sub->rects[i]->text);
1024         av_freep(&sub->rects[i]->ass);
1025         av_freep(&sub->rects[i]);
1026     }
1027
1028     av_freep(&sub->rects);
1029
1030     memset(sub, 0, sizeof(AVSubtitle));
1031 }
1032
1033 av_cold int avcodec_close(AVCodecContext *avctx)
1034 {
1035     /* If there is a user-supplied mutex locking routine, call it. */
1036     if (ff_lockmgr_cb) {
1037         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
1038             return -1;
1039     }
1040
1041     entangled_thread_counter++;
1042     if(entangled_thread_counter != 1){
1043         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
1044         entangled_thread_counter--;
1045         return -1;
1046     }
1047
1048     if (HAVE_THREADS && avctx->thread_opaque)
1049         ff_thread_free(avctx);
1050     if (avctx->codec && avctx->codec->close)
1051         avctx->codec->close(avctx);
1052     avcodec_default_free_buffers(avctx);
1053     avctx->coded_frame = NULL;
1054     av_freep(&avctx->internal);
1055     if (avctx->codec && avctx->codec->priv_class)
1056         av_opt_free(avctx->priv_data);
1057     av_opt_free(avctx);
1058     av_freep(&avctx->priv_data);
1059     if(avctx->codec && avctx->codec->encode)
1060         av_freep(&avctx->extradata);
1061     avctx->codec = NULL;
1062     avctx->active_thread_type = 0;
1063     entangled_thread_counter--;
1064
1065     /* Release any user-supplied mutex. */
1066     if (ff_lockmgr_cb) {
1067         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
1068     }
1069     return 0;
1070 }
1071
1072 AVCodec *avcodec_find_encoder(enum CodecID id)
1073 {
1074     AVCodec *p, *experimental=NULL;
1075     p = first_avcodec;
1076     while (p) {
1077         if (p->encode != NULL && p->id == id) {
1078             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
1079                 experimental = p;
1080             } else
1081                 return p;
1082         }
1083         p = p->next;
1084     }
1085     return experimental;
1086 }
1087
1088 AVCodec *avcodec_find_encoder_by_name(const char *name)
1089 {
1090     AVCodec *p;
1091     if (!name)
1092         return NULL;
1093     p = first_avcodec;
1094     while (p) {
1095         if (p->encode != NULL && strcmp(name,p->name) == 0)
1096             return p;
1097         p = p->next;
1098     }
1099     return NULL;
1100 }
1101
1102 AVCodec *avcodec_find_decoder(enum CodecID id)
1103 {
1104     AVCodec *p;
1105     p = first_avcodec;
1106     while (p) {
1107         if (p->decode != NULL && p->id == id)
1108             return p;
1109         p = p->next;
1110     }
1111     return NULL;
1112 }
1113
1114 AVCodec *avcodec_find_decoder_by_name(const char *name)
1115 {
1116     AVCodec *p;
1117     if (!name)
1118         return NULL;
1119     p = first_avcodec;
1120     while (p) {
1121         if (p->decode != NULL && strcmp(name,p->name) == 0)
1122             return p;
1123         p = p->next;
1124     }
1125     return NULL;
1126 }
1127
1128 static int get_bit_rate(AVCodecContext *ctx)
1129 {
1130     int bit_rate;
1131     int bits_per_sample;
1132
1133     switch(ctx->codec_type) {
1134     case AVMEDIA_TYPE_VIDEO:
1135     case AVMEDIA_TYPE_DATA:
1136     case AVMEDIA_TYPE_SUBTITLE:
1137     case AVMEDIA_TYPE_ATTACHMENT:
1138         bit_rate = ctx->bit_rate;
1139         break;
1140     case AVMEDIA_TYPE_AUDIO:
1141         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
1142         bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
1143         break;
1144     default:
1145         bit_rate = 0;
1146         break;
1147     }
1148     return bit_rate;
1149 }
1150
1151 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
1152 {
1153     int i, len, ret = 0;
1154
1155     for (i = 0; i < 4; i++) {
1156         len = snprintf(buf, buf_size,
1157                        isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
1158         buf      += len;
1159         buf_size  = buf_size > len ? buf_size - len : 0;
1160         ret      += len;
1161         codec_tag>>=8;
1162     }
1163     return ret;
1164 }
1165
1166 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
1167 {
1168     const char *codec_name;
1169     const char *profile = NULL;
1170     AVCodec *p;
1171     char buf1[32];
1172     int bitrate;
1173     AVRational display_aspect_ratio;
1174
1175     if (encode)
1176         p = avcodec_find_encoder(enc->codec_id);
1177     else
1178         p = avcodec_find_decoder(enc->codec_id);
1179
1180     if (p) {
1181         codec_name = p->name;
1182         profile = av_get_profile_name(p, enc->profile);
1183     } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
1184         /* fake mpeg2 transport stream codec (currently not
1185            registered) */
1186         codec_name = "mpeg2ts";
1187     } else if (enc->codec_name[0] != '\0') {
1188         codec_name = enc->codec_name;
1189     } else {
1190         /* output avi tags */
1191         char tag_buf[32];
1192         av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
1193         snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
1194         codec_name = buf1;
1195     }
1196
1197     switch(enc->codec_type) {
1198     case AVMEDIA_TYPE_VIDEO:
1199         snprintf(buf, buf_size,
1200                  "Video: %s%s",
1201                  codec_name, enc->mb_decision ? " (hq)" : "");
1202         if (profile)
1203             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1204                      " (%s)", profile);
1205         if (enc->pix_fmt != PIX_FMT_NONE) {
1206             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1207                      ", %s",
1208                      av_get_pix_fmt_name(enc->pix_fmt));
1209         }
1210         if (enc->width) {
1211             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1212                      ", %dx%d",
1213                      enc->width, enc->height);
1214             if (enc->sample_aspect_ratio.num) {
1215                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
1216                           enc->width*enc->sample_aspect_ratio.num,
1217                           enc->height*enc->sample_aspect_ratio.den,
1218                           1024*1024);
1219                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1220                          " [PAR %d:%d DAR %d:%d]",
1221                          enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
1222                          display_aspect_ratio.num, display_aspect_ratio.den);
1223             }
1224             if(av_log_get_level() >= AV_LOG_DEBUG){
1225                 int g= av_gcd(enc->time_base.num, enc->time_base.den);
1226                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
1227                      ", %d/%d",
1228                      enc->time_base.num/g, enc->time_base.den/g);
1229             }
1230         }
1231         if (encode) {
1232             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1233                      ", q=%d-%d", enc->qmin, enc->qmax);
1234         }
1235         break;
1236     case AVMEDIA_TYPE_AUDIO:
1237         snprintf(buf, buf_size,
1238                  "Audio: %s",
1239                  codec_name);
1240         if (profile)
1241             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1242                      " (%s)", profile);
1243         if (enc->sample_rate) {
1244             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1245                      ", %d Hz", enc->sample_rate);
1246         }
1247         av_strlcat(buf, ", ", buf_size);
1248         av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
1249         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
1250             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1251                      ", %s", av_get_sample_fmt_name(enc->sample_fmt));
1252         }
1253         break;
1254     case AVMEDIA_TYPE_DATA:
1255         snprintf(buf, buf_size, "Data: %s", codec_name);
1256         break;
1257     case AVMEDIA_TYPE_SUBTITLE:
1258         snprintf(buf, buf_size, "Subtitle: %s", codec_name);
1259         break;
1260     case AVMEDIA_TYPE_ATTACHMENT:
1261         snprintf(buf, buf_size, "Attachment: %s", codec_name);
1262         break;
1263     default:
1264         snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
1265         return;
1266     }
1267     if (encode) {
1268         if (enc->flags & CODEC_FLAG_PASS1)
1269             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1270                      ", pass 1");
1271         if (enc->flags & CODEC_FLAG_PASS2)
1272             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1273                      ", pass 2");
1274     }
1275     bitrate = get_bit_rate(enc);
1276     if (bitrate != 0) {
1277         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1278                  ", %d kb/s", bitrate / 1000);
1279     }
1280 }
1281
1282 const char *av_get_profile_name(const AVCodec *codec, int profile)
1283 {
1284     const AVProfile *p;
1285     if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1286         return NULL;
1287
1288     for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1289         if (p->profile == profile)
1290             return p->name;
1291
1292     return NULL;
1293 }
1294
1295 unsigned avcodec_version( void )
1296 {
1297   return LIBAVCODEC_VERSION_INT;
1298 }
1299
1300 const char *avcodec_configuration(void)
1301 {
1302     return LIBAV_CONFIGURATION;
1303 }
1304
1305 const char *avcodec_license(void)
1306 {
1307 #define LICENSE_PREFIX "libavcodec license: "
1308     return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1309 }
1310
1311 void avcodec_flush_buffers(AVCodecContext *avctx)
1312 {
1313     if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1314         ff_thread_flush(avctx);
1315     else if(avctx->codec->flush)
1316         avctx->codec->flush(avctx);
1317 }
1318
1319 static void video_free_buffers(AVCodecContext *s)
1320 {
1321     AVCodecInternal *avci = s->internal;
1322     int i, j;
1323
1324     if (!avci->buffer)
1325         return;
1326
1327     if (avci->buffer_count)
1328         av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
1329                avci->buffer_count);
1330     for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1331         InternalBuffer *buf = &avci->buffer[i];
1332         for(j=0; j<4; j++){
1333             av_freep(&buf->base[j]);
1334             buf->data[j]= NULL;
1335         }
1336     }
1337     av_freep(&avci->buffer);
1338
1339     avci->buffer_count=0;
1340 }
1341
1342 static void audio_free_buffers(AVCodecContext *avctx)
1343 {
1344     AVCodecInternal *avci = avctx->internal;
1345     InternalBuffer *buf;
1346
1347     if (!avci->buffer)
1348         return;
1349     buf = avci->buffer;
1350
1351     if (buf->extended_data) {
1352         av_free(buf->extended_data[0]);
1353         if (buf->extended_data != buf->data)
1354             av_free(buf->extended_data);
1355     }
1356     av_freep(&avci->buffer);
1357 }
1358
1359 void avcodec_default_free_buffers(AVCodecContext *avctx)
1360 {
1361     switch (avctx->codec_type) {
1362     case AVMEDIA_TYPE_VIDEO:
1363         video_free_buffers(avctx);
1364         break;
1365     case AVMEDIA_TYPE_AUDIO:
1366         audio_free_buffers(avctx);
1367         break;
1368     default:
1369         break;
1370     }
1371 }
1372
1373 #if FF_API_OLD_FF_PICT_TYPES
1374 char av_get_pict_type_char(int pict_type){
1375     return av_get_picture_type_char(pict_type);
1376 }
1377 #endif
1378
1379 int av_get_bits_per_sample(enum CodecID codec_id){
1380     switch(codec_id){
1381     case CODEC_ID_ADPCM_SBPRO_2:
1382         return 2;
1383     case CODEC_ID_ADPCM_SBPRO_3:
1384         return 3;
1385     case CODEC_ID_ADPCM_SBPRO_4:
1386     case CODEC_ID_ADPCM_CT:
1387     case CODEC_ID_ADPCM_IMA_WAV:
1388     case CODEC_ID_ADPCM_IMA_QT:
1389     case CODEC_ID_ADPCM_SWF:
1390     case CODEC_ID_ADPCM_MS:
1391     case CODEC_ID_ADPCM_YAMAHA:
1392     case CODEC_ID_ADPCM_G722:
1393         return 4;
1394     case CODEC_ID_PCM_ALAW:
1395     case CODEC_ID_PCM_MULAW:
1396     case CODEC_ID_PCM_S8:
1397     case CODEC_ID_PCM_U8:
1398     case CODEC_ID_PCM_ZORK:
1399         return 8;
1400     case CODEC_ID_PCM_S16BE:
1401     case CODEC_ID_PCM_S16LE:
1402     case CODEC_ID_PCM_S16LE_PLANAR:
1403     case CODEC_ID_PCM_U16BE:
1404     case CODEC_ID_PCM_U16LE:
1405         return 16;
1406     case CODEC_ID_PCM_S24DAUD:
1407     case CODEC_ID_PCM_S24BE:
1408     case CODEC_ID_PCM_S24LE:
1409     case CODEC_ID_PCM_U24BE:
1410     case CODEC_ID_PCM_U24LE:
1411         return 24;
1412     case CODEC_ID_PCM_S32BE:
1413     case CODEC_ID_PCM_S32LE:
1414     case CODEC_ID_PCM_U32BE:
1415     case CODEC_ID_PCM_U32LE:
1416     case CODEC_ID_PCM_F32BE:
1417     case CODEC_ID_PCM_F32LE:
1418         return 32;
1419     case CODEC_ID_PCM_F64BE:
1420     case CODEC_ID_PCM_F64LE:
1421         return 64;
1422     default:
1423         return 0;
1424     }
1425 }
1426
1427 #if FF_API_OLD_SAMPLE_FMT
1428 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1429     return av_get_bytes_per_sample(sample_fmt) << 3;
1430 }
1431 #endif
1432
1433 #if !HAVE_THREADS
1434 int ff_thread_init(AVCodecContext *s){
1435     return -1;
1436 }
1437 #endif
1438
1439 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1440 {
1441     unsigned int n = 0;
1442
1443     while(v >= 0xff) {
1444         *s++ = 0xff;
1445         v -= 0xff;
1446         n++;
1447     }
1448     *s = v;
1449     n++;
1450     return n;
1451 }
1452
1453 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1454     int i;
1455     for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1456     return i;
1457 }
1458
1459 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1460 {
1461     av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1462             "version to the newest one from Git. If the problem still "
1463             "occurs, it means that your file has a feature which has not "
1464             "been implemented.\n", feature);
1465     if(want_sample)
1466         av_log_ask_for_sample(avc, NULL);
1467 }
1468
1469 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1470 {
1471     va_list argument_list;
1472
1473     va_start(argument_list, msg);
1474
1475     if (msg)
1476         av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1477     av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1478             "of this file to ftp://upload.libav.org/incoming/ "
1479             "and contact the libav-devel mailing list.\n");
1480
1481     va_end(argument_list);
1482 }
1483
1484 static AVHWAccel *first_hwaccel = NULL;
1485
1486 void av_register_hwaccel(AVHWAccel *hwaccel)
1487 {
1488     AVHWAccel **p = &first_hwaccel;
1489     while (*p)
1490         p = &(*p)->next;
1491     *p = hwaccel;
1492     hwaccel->next = NULL;
1493 }
1494
1495 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1496 {
1497     return hwaccel ? hwaccel->next : first_hwaccel;
1498 }
1499
1500 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1501 {
1502     AVHWAccel *hwaccel=NULL;
1503
1504     while((hwaccel= av_hwaccel_next(hwaccel))){
1505         if (   hwaccel->id      == codec_id
1506             && hwaccel->pix_fmt == pix_fmt)
1507             return hwaccel;
1508     }
1509     return NULL;
1510 }
1511
1512 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1513 {
1514     if (ff_lockmgr_cb) {
1515         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1516             return -1;
1517         if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
1518             return -1;
1519     }
1520
1521     ff_lockmgr_cb = cb;
1522
1523     if (ff_lockmgr_cb) {
1524         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1525             return -1;
1526         if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
1527             return -1;
1528     }
1529     return 0;
1530 }
1531
1532 int avpriv_lock_avformat(void)
1533 {
1534     if (ff_lockmgr_cb) {
1535         if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
1536             return -1;
1537     }
1538     return 0;
1539 }
1540
1541 int avpriv_unlock_avformat(void)
1542 {
1543     if (ff_lockmgr_cb) {
1544         if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
1545             return -1;
1546     }
1547     return 0;
1548 }
1549
1550 unsigned int avpriv_toupper4(unsigned int x)
1551 {
1552     return     toupper( x     &0xFF)
1553             + (toupper((x>>8 )&0xFF)<<8 )
1554             + (toupper((x>>16)&0xFF)<<16)
1555             + (toupper((x>>24)&0xFF)<<24);
1556 }
1557
1558 #if !HAVE_THREADS
1559
1560 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1561 {
1562     f->owner = avctx;
1563     return avctx->get_buffer(avctx, f);
1564 }
1565
1566 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1567 {
1568     f->owner->release_buffer(f->owner, f);
1569 }
1570
1571 void ff_thread_finish_setup(AVCodecContext *avctx)
1572 {
1573 }
1574
1575 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1576 {
1577 }
1578
1579 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1580 {
1581 }
1582
1583 #endif
1584
1585 #if FF_API_THREAD_INIT
1586 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1587 {
1588     s->thread_count = thread_count;
1589     return ff_thread_init(s);
1590 }
1591 #endif
1592
1593 enum AVMediaType avcodec_get_type(enum CodecID codec_id)
1594 {
1595     if (codec_id <= CODEC_ID_NONE)
1596         return AVMEDIA_TYPE_UNKNOWN;
1597     else if (codec_id < CODEC_ID_FIRST_AUDIO)
1598         return AVMEDIA_TYPE_VIDEO;
1599     else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
1600         return AVMEDIA_TYPE_AUDIO;
1601     else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
1602         return AVMEDIA_TYPE_SUBTITLE;
1603
1604     return AVMEDIA_TYPE_UNKNOWN;
1605 }