]> git.sesse.net Git - ffmpeg/blob - libavcodec/utils.c
configure: do not blank $LIBNAME if static libs disabled
[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/pixdesc.h"
31 #include "libavutil/audioconvert.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/samplefmt.h"
34 #include "avcodec.h"
35 #include "dsputil.h"
36 #include "libavutil/opt.h"
37 #include "imgconvert.h"
38 #include "thread.h"
39 #include "audioconvert.h"
40 #include "internal.h"
41 #include <stdlib.h>
42 #include <stdarg.h>
43 #include <limits.h>
44 #include <float.h>
45
46 static int volatile entangled_thread_counter=0;
47 static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
48 static void *codec_mutex;
49
50 void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
51 {
52     if(min_size < *size)
53         return ptr;
54
55     min_size= FFMAX(17*min_size/16 + 32, min_size);
56
57     ptr= av_realloc(ptr, min_size);
58     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
59         min_size= 0;
60
61     *size= min_size;
62
63     return ptr;
64 }
65
66 void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
67 {
68     void **p = ptr;
69     if (min_size < *size)
70         return;
71     min_size= FFMAX(17*min_size/16 + 32, min_size);
72     av_free(*p);
73     *p = av_malloc(min_size);
74     if (!*p) min_size = 0;
75     *size= min_size;
76 }
77
78 /* encoder management */
79 static AVCodec *first_avcodec = NULL;
80
81 AVCodec *av_codec_next(AVCodec *c){
82     if(c) return c->next;
83     else  return first_avcodec;
84 }
85
86 void avcodec_register(AVCodec *codec)
87 {
88     AVCodec **p;
89     avcodec_init();
90     p = &first_avcodec;
91     while (*p != NULL) p = &(*p)->next;
92     *p = codec;
93     codec->next = NULL;
94 }
95
96 unsigned avcodec_get_edge_width(void)
97 {
98     return EDGE_WIDTH;
99 }
100
101 void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
102     s->coded_width = width;
103     s->coded_height= height;
104     s->width = -((-width )>>s->lowres);
105     s->height= -((-height)>>s->lowres);
106 }
107
108 typedef struct InternalBuffer{
109     int last_pic_num;
110     uint8_t *base[4];
111     uint8_t *data[4];
112     int linesize[4];
113     int width, height;
114     enum PixelFormat pix_fmt;
115 }InternalBuffer;
116
117 #define INTERNAL_BUFFER_SIZE (32+1)
118
119 void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
120     int w_align= 1;
121     int h_align= 1;
122
123     switch(s->pix_fmt){
124     case PIX_FMT_YUV420P:
125     case PIX_FMT_YUYV422:
126     case PIX_FMT_UYVY422:
127     case PIX_FMT_YUV422P:
128     case PIX_FMT_YUV440P:
129     case PIX_FMT_YUV444P:
130     case PIX_FMT_GRAY8:
131     case PIX_FMT_GRAY16BE:
132     case PIX_FMT_GRAY16LE:
133     case PIX_FMT_YUVJ420P:
134     case PIX_FMT_YUVJ422P:
135     case PIX_FMT_YUVJ440P:
136     case PIX_FMT_YUVJ444P:
137     case PIX_FMT_YUVA420P:
138     case PIX_FMT_YUV420P9LE:
139     case PIX_FMT_YUV420P9BE:
140     case PIX_FMT_YUV420P10LE:
141     case PIX_FMT_YUV420P10BE:
142     case PIX_FMT_YUV422P10LE:
143     case PIX_FMT_YUV422P10BE:
144     case PIX_FMT_YUV444P9LE:
145     case PIX_FMT_YUV444P9BE:
146     case PIX_FMT_YUV444P10LE:
147     case PIX_FMT_YUV444P10BE:
148         w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
149         h_align= 16;
150         if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP || s->codec_id == CODEC_ID_H264)
151             h_align= 32; // interlaced is rounded up to 2 MBs
152         break;
153     case PIX_FMT_YUV411P:
154     case PIX_FMT_UYYVYY411:
155         w_align=32;
156         h_align=8;
157         break;
158     case PIX_FMT_YUV410P:
159         if(s->codec_id == CODEC_ID_SVQ1){
160             w_align=64;
161             h_align=64;
162         }
163     case PIX_FMT_RGB555:
164         if(s->codec_id == CODEC_ID_RPZA){
165             w_align=4;
166             h_align=4;
167         }
168     case PIX_FMT_PAL8:
169     case PIX_FMT_BGR8:
170     case PIX_FMT_RGB8:
171         if(s->codec_id == CODEC_ID_SMC){
172             w_align=4;
173             h_align=4;
174         }
175         break;
176     case PIX_FMT_BGR24:
177         if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
178             w_align=4;
179             h_align=4;
180         }
181         break;
182     default:
183         w_align= 1;
184         h_align= 1;
185         break;
186     }
187
188     *width = FFALIGN(*width , w_align);
189     *height= FFALIGN(*height, h_align);
190     if(s->codec_id == CODEC_ID_H264 || s->lowres)
191         *height+=2; // some of the optimized chroma MC reads one line too much
192                     // which is also done in mpeg decoders with lowres > 0
193
194     linesize_align[0] =
195     linesize_align[1] =
196     linesize_align[2] =
197     linesize_align[3] = STRIDE_ALIGN;
198 //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
199 //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
200 //picture size unneccessarily in some cases. The solution here is not
201 //pretty and better ideas are welcome!
202 #if HAVE_MMX
203     if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
204        s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
205        s->codec_id == CODEC_ID_VP6A) {
206         linesize_align[0] =
207         linesize_align[1] =
208         linesize_align[2] = 16;
209     }
210 #endif
211 }
212
213 void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
214     int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
215     int linesize_align[4];
216     int align;
217     avcodec_align_dimensions2(s, width, height, linesize_align);
218     align = FFMAX(linesize_align[0], linesize_align[3]);
219     linesize_align[1] <<= chroma_shift;
220     linesize_align[2] <<= chroma_shift;
221     align = FFMAX3(align, linesize_align[1], linesize_align[2]);
222     *width=FFALIGN(*width, align);
223 }
224
225 int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
226     int i;
227     int w= s->width;
228     int h= s->height;
229     InternalBuffer *buf;
230     int *picture_number;
231
232     if(pic->data[0]!=NULL) {
233         av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
234         return -1;
235     }
236     if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
237         av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
238         return -1;
239     }
240
241     if(av_image_check_size(w, h, 0, s))
242         return -1;
243
244     if(s->internal_buffer==NULL){
245         s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
246     }
247 #if 0
248     s->internal_buffer= av_fast_realloc(
249         s->internal_buffer,
250         &s->internal_buffer_size,
251         sizeof(InternalBuffer)*FFMAX(99,  s->internal_buffer_count+1)/*FIXME*/
252         );
253 #endif
254
255     buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
256     picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
257     (*picture_number)++;
258
259     if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
260         if(s->active_thread_type&FF_THREAD_FRAME) {
261             av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
262             return -1;
263         }
264
265         for(i=0; i<4; i++){
266             av_freep(&buf->base[i]);
267             buf->data[i]= NULL;
268         }
269     }
270
271     if(buf->base[0]){
272         pic->age= *picture_number - buf->last_pic_num;
273         buf->last_pic_num= *picture_number;
274     }else{
275         int h_chroma_shift, v_chroma_shift;
276         int size[4] = {0};
277         int tmpsize;
278         int unaligned;
279         AVPicture picture;
280         int stride_align[4];
281         const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
282
283         avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
284
285         avcodec_align_dimensions2(s, &w, &h, stride_align);
286
287         if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
288             w+= EDGE_WIDTH*2;
289             h+= EDGE_WIDTH*2;
290         }
291
292         do {
293             // NOTE: do not align linesizes individually, this breaks e.g. assumptions
294             // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
295             av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
296             // increase alignment of w for next try (rhs gives the lowest bit set in w)
297             w += w & ~(w-1);
298
299             unaligned = 0;
300             for (i=0; i<4; i++){
301                 unaligned |= picture.linesize[i] % stride_align[i];
302             }
303         } while (unaligned);
304
305         tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
306         if (tmpsize < 0)
307             return -1;
308
309         for (i=0; i<3 && picture.data[i+1]; i++)
310             size[i] = picture.data[i+1] - picture.data[i];
311         size[i] = tmpsize - (picture.data[i] - picture.data[0]);
312
313         buf->last_pic_num= -256*256*256*64;
314         memset(buf->base, 0, sizeof(buf->base));
315         memset(buf->data, 0, sizeof(buf->data));
316
317         for(i=0; i<4 && size[i]; i++){
318             const int h_shift= i==0 ? 0 : h_chroma_shift;
319             const int v_shift= i==0 ? 0 : v_chroma_shift;
320
321             buf->linesize[i]= picture.linesize[i];
322
323             buf->base[i]= av_malloc(size[i]+16); //FIXME 16
324             if(buf->base[i]==NULL) return -1;
325             memset(buf->base[i], 128, size[i]);
326
327             // no edge if EDGE EMU or not planar YUV
328             if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
329                 buf->data[i] = buf->base[i];
330             else
331                 buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
332         }
333         if(size[1] && !size[2])
334             ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
335         buf->width  = s->width;
336         buf->height = s->height;
337         buf->pix_fmt= s->pix_fmt;
338         pic->age= 256*256*256*64;
339     }
340     pic->type= FF_BUFFER_TYPE_INTERNAL;
341
342     for(i=0; i<4; i++){
343         pic->base[i]= buf->base[i];
344         pic->data[i]= buf->data[i];
345         pic->linesize[i]= buf->linesize[i];
346     }
347     s->internal_buffer_count++;
348
349     if(s->pkt) pic->pkt_pts= s->pkt->pts;
350     else       pic->pkt_pts= AV_NOPTS_VALUE;
351     pic->reordered_opaque= s->reordered_opaque;
352
353     if(s->debug&FF_DEBUG_BUFFERS)
354         av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
355
356     return 0;
357 }
358
359 void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
360     int i;
361     InternalBuffer *buf, *last;
362
363     assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
364     assert(s->internal_buffer_count);
365
366     if(s->internal_buffer){
367     buf = NULL; /* avoids warning */
368     for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
369         buf= &((InternalBuffer*)s->internal_buffer)[i];
370         if(buf->data[0] == pic->data[0])
371             break;
372     }
373     assert(i < s->internal_buffer_count);
374     s->internal_buffer_count--;
375     last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
376
377     FFSWAP(InternalBuffer, *buf, *last);
378     }
379
380     for(i=0; i<4; i++){
381         pic->data[i]=NULL;
382 //        pic->base[i]=NULL;
383     }
384 //printf("R%X\n", pic->opaque);
385
386     if(s->debug&FF_DEBUG_BUFFERS)
387         av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
388 }
389
390 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
391     AVFrame temp_pic;
392     int i;
393
394     /* If no picture return a new buffer */
395     if(pic->data[0] == NULL) {
396         /* We will copy from buffer, so must be readable */
397         pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
398         return s->get_buffer(s, pic);
399     }
400
401     /* If internal buffer type return the same buffer */
402     if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
403         if(s->pkt) pic->pkt_pts= s->pkt->pts;
404         else       pic->pkt_pts= AV_NOPTS_VALUE;
405         pic->reordered_opaque= s->reordered_opaque;
406         return 0;
407     }
408
409     /*
410      * Not internal type and reget_buffer not overridden, emulate cr buffer
411      */
412     temp_pic = *pic;
413     for(i = 0; i < 4; i++)
414         pic->data[i] = pic->base[i] = NULL;
415     pic->opaque = NULL;
416     /* Allocate new frame */
417     if (s->get_buffer(s, pic))
418         return -1;
419     /* Copy image data from old buffer to new buffer */
420     av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
421              s->height);
422     s->release_buffer(s, &temp_pic); // Release old frame
423     return 0;
424 }
425
426 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
427     int i;
428
429     for(i=0; i<count; i++){
430         int r= func(c, (char*)arg + i*size);
431         if(ret) ret[i]= r;
432     }
433     return 0;
434 }
435
436 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
437     int i;
438
439     for(i=0; i<count; i++){
440         int r= func(c, arg, i, 0);
441         if(ret) ret[i]= r;
442     }
443     return 0;
444 }
445
446 enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
447     while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
448         ++fmt;
449     return fmt[0];
450 }
451
452 void avcodec_get_frame_defaults(AVFrame *pic){
453     memset(pic, 0, sizeof(AVFrame));
454
455     pic->pts= AV_NOPTS_VALUE;
456     pic->key_frame= 1;
457 }
458
459 AVFrame *avcodec_alloc_frame(void){
460     AVFrame *pic= av_malloc(sizeof(AVFrame));
461
462     if(pic==NULL) return NULL;
463
464     avcodec_get_frame_defaults(pic);
465
466     return pic;
467 }
468
469 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
470 {
471     int ret = 0;
472
473     /* If there is a user-supplied mutex locking routine, call it. */
474     if (ff_lockmgr_cb) {
475         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
476             return -1;
477     }
478
479     entangled_thread_counter++;
480     if(entangled_thread_counter != 1){
481         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
482         ret = -1;
483         goto end;
484     }
485
486     if(avctx->codec || !codec) {
487         ret = AVERROR(EINVAL);
488         goto end;
489     }
490
491     if (codec->priv_data_size > 0) {
492       if(!avctx->priv_data){
493         avctx->priv_data = av_mallocz(codec->priv_data_size);
494         if (!avctx->priv_data) {
495             ret = AVERROR(ENOMEM);
496             goto end;
497         }
498         if(codec->priv_class){ //this can be droped once all user apps use   avcodec_get_context_defaults3()
499             *(AVClass**)avctx->priv_data= codec->priv_class;
500             av_opt_set_defaults(avctx->priv_data);
501         }
502       }
503     } else {
504         avctx->priv_data = NULL;
505     }
506
507     if(avctx->coded_width && avctx->coded_height)
508         avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
509     else if(avctx->width && avctx->height)
510         avcodec_set_dimensions(avctx, avctx->width, avctx->height);
511
512     if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
513         && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
514            || av_image_check_size(avctx->width,       avctx->height,       0, avctx) < 0)) {
515         av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
516         avcodec_set_dimensions(avctx, 0, 0);
517     }
518
519     /* if the decoder init function was already called previously,
520        free the already allocated subtitle_header before overwriting it */
521     if (codec->decode)
522         av_freep(&avctx->subtitle_header);
523
524 #define SANE_NB_CHANNELS 128U
525     if (avctx->channels > SANE_NB_CHANNELS) {
526         ret = AVERROR(EINVAL);
527         goto free_and_end;
528     }
529
530     avctx->codec = codec;
531     if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
532         avctx->codec_id == CODEC_ID_NONE) {
533         avctx->codec_type = codec->type;
534         avctx->codec_id   = codec->id;
535     }
536     if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
537                            && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
538         av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
539         ret = AVERROR(EINVAL);
540         goto free_and_end;
541     }
542     avctx->frame_number = 0;
543
544     if (HAVE_THREADS && !avctx->thread_opaque) {
545         ret = ff_thread_init(avctx);
546         if (ret < 0) {
547             goto free_and_end;
548         }
549     }
550
551     if (avctx->codec->max_lowres < avctx->lowres) {
552         av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
553                avctx->codec->max_lowres);
554         ret = AVERROR(EINVAL);
555         goto free_and_end;
556     }
557     if (avctx->codec->encode) {
558         int i;
559         if (avctx->codec->sample_fmts) {
560             for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
561                 if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
562                     break;
563             if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
564                 av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
565                 ret = AVERROR(EINVAL);
566                 goto free_and_end;
567             }
568         }
569         if (avctx->codec->supported_samplerates) {
570             for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
571                 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
572                     break;
573             if (avctx->codec->supported_samplerates[i] == 0) {
574                 av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
575                 ret = AVERROR(EINVAL);
576                 goto free_and_end;
577             }
578         }
579         if (avctx->codec->channel_layouts) {
580             if (!avctx->channel_layout) {
581                 av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
582             } else {
583                 for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
584                     if (avctx->channel_layout == avctx->codec->channel_layouts[i])
585                         break;
586                 if (avctx->codec->channel_layouts[i] == 0) {
587                     av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
588                     ret = AVERROR(EINVAL);
589                     goto free_and_end;
590                 }
591             }
592         }
593         if (avctx->channel_layout && avctx->channels) {
594             if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
595                 av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
596                 ret = AVERROR(EINVAL);
597                 goto free_and_end;
598             }
599         } else if (avctx->channel_layout) {
600             avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
601         }
602     }
603
604     if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
605         ret = avctx->codec->init(avctx);
606         if (ret < 0) {
607             goto free_and_end;
608         }
609     }
610 end:
611     entangled_thread_counter--;
612
613     /* Release any user-supplied mutex. */
614     if (ff_lockmgr_cb) {
615         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
616     }
617     return ret;
618 free_and_end:
619     av_freep(&avctx->priv_data);
620     avctx->codec= NULL;
621     goto end;
622 }
623
624 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
625                          const short *samples)
626 {
627     if(buf_size < FF_MIN_BUFFER_SIZE && 0){
628         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
629         return -1;
630     }
631     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
632         int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
633         avctx->frame_number++;
634         return ret;
635     }else
636         return 0;
637 }
638
639 int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
640                          const AVFrame *pict)
641 {
642     if(buf_size < FF_MIN_BUFFER_SIZE){
643         av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
644         return -1;
645     }
646     if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
647         return -1;
648     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
649         int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
650         avctx->frame_number++;
651         emms_c(); //needed to avoid an emms_c() call before every return;
652
653         return ret;
654     }else
655         return 0;
656 }
657
658 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
659                             const AVSubtitle *sub)
660 {
661     int ret;
662     if(sub->start_display_time) {
663         av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
664         return -1;
665     }
666     if(sub->num_rects == 0 || !sub->rects)
667         return -1;
668     ret = avctx->codec->encode(avctx, buf, buf_size, sub);
669     avctx->frame_number++;
670     return ret;
671 }
672
673 int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
674                          int *got_picture_ptr,
675                          AVPacket *avpkt)
676 {
677     int ret;
678
679     *got_picture_ptr= 0;
680     if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
681         return -1;
682
683     avctx->pkt = avpkt;
684
685     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
686         if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
687              ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
688                                           avpkt);
689         else {
690             ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
691                               avpkt);
692             picture->pkt_dts= avpkt->dts;
693         }
694
695         emms_c(); //needed to avoid an emms_c() call before every return;
696
697         if (*got_picture_ptr)
698             avctx->frame_number++;
699     }else
700         ret= 0;
701
702     return ret;
703 }
704
705 int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
706                          int *frame_size_ptr,
707                          AVPacket *avpkt)
708 {
709     int ret;
710
711     avctx->pkt = avpkt;
712
713     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
714         //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
715         if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
716             av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
717             return -1;
718         }
719         if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
720         *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
721             av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
722             return -1;
723         }
724
725         ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
726         avctx->frame_number++;
727     }else{
728         ret= 0;
729         *frame_size_ptr=0;
730     }
731     return ret;
732 }
733
734 int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
735                             int *got_sub_ptr,
736                             AVPacket *avpkt)
737 {
738     int ret;
739
740     avctx->pkt = avpkt;
741     *got_sub_ptr = 0;
742     ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
743     if (*got_sub_ptr)
744         avctx->frame_number++;
745     return ret;
746 }
747
748 void avsubtitle_free(AVSubtitle *sub)
749 {
750     int i;
751
752     for (i = 0; i < sub->num_rects; i++)
753     {
754         av_freep(&sub->rects[i]->pict.data[0]);
755         av_freep(&sub->rects[i]->pict.data[1]);
756         av_freep(&sub->rects[i]->pict.data[2]);
757         av_freep(&sub->rects[i]->pict.data[3]);
758         av_freep(&sub->rects[i]->text);
759         av_freep(&sub->rects[i]->ass);
760         av_freep(&sub->rects[i]);
761     }
762
763     av_freep(&sub->rects);
764
765     memset(sub, 0, sizeof(AVSubtitle));
766 }
767
768 av_cold int avcodec_close(AVCodecContext *avctx)
769 {
770     /* If there is a user-supplied mutex locking routine, call it. */
771     if (ff_lockmgr_cb) {
772         if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
773             return -1;
774     }
775
776     entangled_thread_counter++;
777     if(entangled_thread_counter != 1){
778         av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
779         entangled_thread_counter--;
780         return -1;
781     }
782
783     if (HAVE_THREADS && avctx->thread_opaque)
784         ff_thread_free(avctx);
785     if (avctx->codec && avctx->codec->close)
786         avctx->codec->close(avctx);
787     avcodec_default_free_buffers(avctx);
788     avctx->coded_frame = NULL;
789     if (avctx->codec && avctx->codec->priv_class)
790         av_opt_free(avctx->priv_data);
791     av_opt_free(avctx);
792     av_freep(&avctx->priv_data);
793     if(avctx->codec && avctx->codec->encode)
794         av_freep(&avctx->extradata);
795     avctx->codec = NULL;
796     avctx->active_thread_type = 0;
797     entangled_thread_counter--;
798
799     /* Release any user-supplied mutex. */
800     if (ff_lockmgr_cb) {
801         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
802     }
803     return 0;
804 }
805
806 AVCodec *avcodec_find_encoder(enum CodecID id)
807 {
808     AVCodec *p, *experimental=NULL;
809     p = first_avcodec;
810     while (p) {
811         if (p->encode != NULL && p->id == id) {
812             if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
813                 experimental = p;
814             } else
815                 return p;
816         }
817         p = p->next;
818     }
819     return experimental;
820 }
821
822 AVCodec *avcodec_find_encoder_by_name(const char *name)
823 {
824     AVCodec *p;
825     if (!name)
826         return NULL;
827     p = first_avcodec;
828     while (p) {
829         if (p->encode != NULL && strcmp(name,p->name) == 0)
830             return p;
831         p = p->next;
832     }
833     return NULL;
834 }
835
836 AVCodec *avcodec_find_decoder(enum CodecID id)
837 {
838     AVCodec *p;
839     p = first_avcodec;
840     while (p) {
841         if (p->decode != NULL && p->id == id)
842             return p;
843         p = p->next;
844     }
845     return NULL;
846 }
847
848 AVCodec *avcodec_find_decoder_by_name(const char *name)
849 {
850     AVCodec *p;
851     if (!name)
852         return NULL;
853     p = first_avcodec;
854     while (p) {
855         if (p->decode != NULL && strcmp(name,p->name) == 0)
856             return p;
857         p = p->next;
858     }
859     return NULL;
860 }
861
862 static int get_bit_rate(AVCodecContext *ctx)
863 {
864     int bit_rate;
865     int bits_per_sample;
866
867     switch(ctx->codec_type) {
868     case AVMEDIA_TYPE_VIDEO:
869     case AVMEDIA_TYPE_DATA:
870     case AVMEDIA_TYPE_SUBTITLE:
871     case AVMEDIA_TYPE_ATTACHMENT:
872         bit_rate = ctx->bit_rate;
873         break;
874     case AVMEDIA_TYPE_AUDIO:
875         bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
876         bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
877         break;
878     default:
879         bit_rate = 0;
880         break;
881     }
882     return bit_rate;
883 }
884
885 size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
886 {
887     int i, len, ret = 0;
888
889     for (i = 0; i < 4; i++) {
890         len = snprintf(buf, buf_size,
891                        isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
892         buf      += len;
893         buf_size  = buf_size > len ? buf_size - len : 0;
894         ret      += len;
895         codec_tag>>=8;
896     }
897     return ret;
898 }
899
900 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
901 {
902     const char *codec_name;
903     const char *profile = NULL;
904     AVCodec *p;
905     char buf1[32];
906     int bitrate;
907     AVRational display_aspect_ratio;
908
909     if (encode)
910         p = avcodec_find_encoder(enc->codec_id);
911     else
912         p = avcodec_find_decoder(enc->codec_id);
913
914     if (p) {
915         codec_name = p->name;
916         profile = av_get_profile_name(p, enc->profile);
917     } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
918         /* fake mpeg2 transport stream codec (currently not
919            registered) */
920         codec_name = "mpeg2ts";
921     } else if (enc->codec_name[0] != '\0') {
922         codec_name = enc->codec_name;
923     } else {
924         /* output avi tags */
925         char tag_buf[32];
926         av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
927         snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
928         codec_name = buf1;
929     }
930
931     switch(enc->codec_type) {
932     case AVMEDIA_TYPE_VIDEO:
933         snprintf(buf, buf_size,
934                  "Video: %s%s",
935                  codec_name, enc->mb_decision ? " (hq)" : "");
936         if (profile)
937             snprintf(buf + strlen(buf), buf_size - strlen(buf),
938                      " (%s)", profile);
939         if (enc->pix_fmt != PIX_FMT_NONE) {
940             snprintf(buf + strlen(buf), buf_size - strlen(buf),
941                      ", %s",
942                      av_get_pix_fmt_name(enc->pix_fmt));
943         }
944         if (enc->width) {
945             snprintf(buf + strlen(buf), buf_size - strlen(buf),
946                      ", %dx%d",
947                      enc->width, enc->height);
948             if (enc->sample_aspect_ratio.num) {
949                 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
950                           enc->width*enc->sample_aspect_ratio.num,
951                           enc->height*enc->sample_aspect_ratio.den,
952                           1024*1024);
953                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
954                          " [PAR %d:%d DAR %d:%d]",
955                          enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
956                          display_aspect_ratio.num, display_aspect_ratio.den);
957             }
958             if(av_log_get_level() >= AV_LOG_DEBUG){
959                 int g= av_gcd(enc->time_base.num, enc->time_base.den);
960                 snprintf(buf + strlen(buf), buf_size - strlen(buf),
961                      ", %d/%d",
962                      enc->time_base.num/g, enc->time_base.den/g);
963             }
964         }
965         if (encode) {
966             snprintf(buf + strlen(buf), buf_size - strlen(buf),
967                      ", q=%d-%d", enc->qmin, enc->qmax);
968         }
969         break;
970     case AVMEDIA_TYPE_AUDIO:
971         snprintf(buf, buf_size,
972                  "Audio: %s",
973                  codec_name);
974         if (profile)
975             snprintf(buf + strlen(buf), buf_size - strlen(buf),
976                      " (%s)", profile);
977         if (enc->sample_rate) {
978             snprintf(buf + strlen(buf), buf_size - strlen(buf),
979                      ", %d Hz", enc->sample_rate);
980         }
981         av_strlcat(buf, ", ", buf_size);
982         av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
983         if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
984             snprintf(buf + strlen(buf), buf_size - strlen(buf),
985                      ", %s", av_get_sample_fmt_name(enc->sample_fmt));
986         }
987         break;
988     case AVMEDIA_TYPE_DATA:
989         snprintf(buf, buf_size, "Data: %s", codec_name);
990         break;
991     case AVMEDIA_TYPE_SUBTITLE:
992         snprintf(buf, buf_size, "Subtitle: %s", codec_name);
993         break;
994     case AVMEDIA_TYPE_ATTACHMENT:
995         snprintf(buf, buf_size, "Attachment: %s", codec_name);
996         break;
997     default:
998         snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
999         return;
1000     }
1001     if (encode) {
1002         if (enc->flags & CODEC_FLAG_PASS1)
1003             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1004                      ", pass 1");
1005         if (enc->flags & CODEC_FLAG_PASS2)
1006             snprintf(buf + strlen(buf), buf_size - strlen(buf),
1007                      ", pass 2");
1008     }
1009     bitrate = get_bit_rate(enc);
1010     if (bitrate != 0) {
1011         snprintf(buf + strlen(buf), buf_size - strlen(buf),
1012                  ", %d kb/s", bitrate / 1000);
1013     }
1014 }
1015
1016 const char *av_get_profile_name(const AVCodec *codec, int profile)
1017 {
1018     const AVProfile *p;
1019     if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
1020         return NULL;
1021
1022     for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
1023         if (p->profile == profile)
1024             return p->name;
1025
1026     return NULL;
1027 }
1028
1029 unsigned avcodec_version( void )
1030 {
1031   return LIBAVCODEC_VERSION_INT;
1032 }
1033
1034 const char *avcodec_configuration(void)
1035 {
1036     return LIBAV_CONFIGURATION;
1037 }
1038
1039 const char *avcodec_license(void)
1040 {
1041 #define LICENSE_PREFIX "libavcodec license: "
1042     return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
1043 }
1044
1045 void avcodec_init(void)
1046 {
1047     static int initialized = 0;
1048
1049     if (initialized != 0)
1050         return;
1051     initialized = 1;
1052
1053     dsputil_static_init();
1054 }
1055
1056 void avcodec_flush_buffers(AVCodecContext *avctx)
1057 {
1058     if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1059         ff_thread_flush(avctx);
1060     if(avctx->codec->flush)
1061         avctx->codec->flush(avctx);
1062 }
1063
1064 void avcodec_default_free_buffers(AVCodecContext *s){
1065     int i, j;
1066
1067     if(s->internal_buffer==NULL) return;
1068
1069     if (s->internal_buffer_count)
1070         av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
1071     for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
1072         InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
1073         for(j=0; j<4; j++){
1074             av_freep(&buf->base[j]);
1075             buf->data[j]= NULL;
1076         }
1077     }
1078     av_freep(&s->internal_buffer);
1079
1080     s->internal_buffer_count=0;
1081 }
1082
1083 #if FF_API_OLD_FF_PICT_TYPES
1084 char av_get_pict_type_char(int pict_type){
1085     return av_get_picture_type_char(pict_type);
1086 }
1087 #endif
1088
1089 int av_get_bits_per_sample(enum CodecID codec_id){
1090     switch(codec_id){
1091     case CODEC_ID_ADPCM_SBPRO_2:
1092         return 2;
1093     case CODEC_ID_ADPCM_SBPRO_3:
1094         return 3;
1095     case CODEC_ID_ADPCM_SBPRO_4:
1096     case CODEC_ID_ADPCM_CT:
1097     case CODEC_ID_ADPCM_IMA_WAV:
1098     case CODEC_ID_ADPCM_MS:
1099     case CODEC_ID_ADPCM_YAMAHA:
1100         return 4;
1101     case CODEC_ID_ADPCM_G722:
1102     case CODEC_ID_PCM_ALAW:
1103     case CODEC_ID_PCM_MULAW:
1104     case CODEC_ID_PCM_S8:
1105     case CODEC_ID_PCM_U8:
1106     case CODEC_ID_PCM_ZORK:
1107         return 8;
1108     case CODEC_ID_PCM_S16BE:
1109     case CODEC_ID_PCM_S16LE:
1110     case CODEC_ID_PCM_S16LE_PLANAR:
1111     case CODEC_ID_PCM_U16BE:
1112     case CODEC_ID_PCM_U16LE:
1113         return 16;
1114     case CODEC_ID_PCM_S24DAUD:
1115     case CODEC_ID_PCM_S24BE:
1116     case CODEC_ID_PCM_S24LE:
1117     case CODEC_ID_PCM_U24BE:
1118     case CODEC_ID_PCM_U24LE:
1119         return 24;
1120     case CODEC_ID_PCM_S32BE:
1121     case CODEC_ID_PCM_S32LE:
1122     case CODEC_ID_PCM_U32BE:
1123     case CODEC_ID_PCM_U32LE:
1124     case CODEC_ID_PCM_F32BE:
1125     case CODEC_ID_PCM_F32LE:
1126         return 32;
1127     case CODEC_ID_PCM_F64BE:
1128     case CODEC_ID_PCM_F64LE:
1129         return 64;
1130     default:
1131         return 0;
1132     }
1133 }
1134
1135 #if FF_API_OLD_SAMPLE_FMT
1136 int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
1137     return av_get_bytes_per_sample(sample_fmt) << 3;
1138 }
1139 #endif
1140
1141 #if !HAVE_THREADS
1142 int ff_thread_init(AVCodecContext *s){
1143     return -1;
1144 }
1145 #endif
1146
1147 unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
1148 {
1149     unsigned int n = 0;
1150
1151     while(v >= 0xff) {
1152         *s++ = 0xff;
1153         v -= 0xff;
1154         n++;
1155     }
1156     *s = v;
1157     n++;
1158     return n;
1159 }
1160
1161 int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
1162     int i;
1163     for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
1164     return i;
1165 }
1166
1167 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
1168 {
1169     av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
1170             "version to the newest one from Git. If the problem still "
1171             "occurs, it means that your file has a feature which has not "
1172             "been implemented.\n", feature);
1173     if(want_sample)
1174         av_log_ask_for_sample(avc, NULL);
1175 }
1176
1177 void av_log_ask_for_sample(void *avc, const char *msg, ...)
1178 {
1179     va_list argument_list;
1180
1181     va_start(argument_list, msg);
1182
1183     if (msg)
1184         av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
1185     av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
1186             "of this file to ftp://upload.libav.org/incoming/ "
1187             "and contact the libav-devel mailing list.\n");
1188
1189     va_end(argument_list);
1190 }
1191
1192 static AVHWAccel *first_hwaccel = NULL;
1193
1194 void av_register_hwaccel(AVHWAccel *hwaccel)
1195 {
1196     AVHWAccel **p = &first_hwaccel;
1197     while (*p)
1198         p = &(*p)->next;
1199     *p = hwaccel;
1200     hwaccel->next = NULL;
1201 }
1202
1203 AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
1204 {
1205     return hwaccel ? hwaccel->next : first_hwaccel;
1206 }
1207
1208 AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
1209 {
1210     AVHWAccel *hwaccel=NULL;
1211
1212     while((hwaccel= av_hwaccel_next(hwaccel))){
1213         if (   hwaccel->id      == codec_id
1214             && hwaccel->pix_fmt == pix_fmt)
1215             return hwaccel;
1216     }
1217     return NULL;
1218 }
1219
1220 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
1221 {
1222     if (ff_lockmgr_cb) {
1223         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
1224             return -1;
1225     }
1226
1227     ff_lockmgr_cb = cb;
1228
1229     if (ff_lockmgr_cb) {
1230         if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
1231             return -1;
1232     }
1233     return 0;
1234 }
1235
1236 unsigned int ff_toupper4(unsigned int x)
1237 {
1238     return     toupper( x     &0xFF)
1239             + (toupper((x>>8 )&0xFF)<<8 )
1240             + (toupper((x>>16)&0xFF)<<16)
1241             + (toupper((x>>24)&0xFF)<<24);
1242 }
1243
1244 #if !HAVE_PTHREADS
1245
1246 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1247 {
1248     f->owner = avctx;
1249     return avctx->get_buffer(avctx, f);
1250 }
1251
1252 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1253 {
1254     f->owner->release_buffer(f->owner, f);
1255 }
1256
1257 void ff_thread_finish_setup(AVCodecContext *avctx)
1258 {
1259 }
1260
1261 void ff_thread_report_progress(AVFrame *f, int progress, int field)
1262 {
1263 }
1264
1265 void ff_thread_await_progress(AVFrame *f, int progress, int field)
1266 {
1267 }
1268
1269 #endif
1270
1271 #if FF_API_THREAD_INIT
1272 int avcodec_thread_init(AVCodecContext *s, int thread_count)
1273 {
1274     s->thread_count = thread_count;
1275     return ff_thread_init(s);
1276 }
1277 #endif