Statistics
| Branch: | Revision:

ffmpeg / libavcodec / utils.c @ 86807410

History | View | Annotate | Download (22.3 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * utils for libavcodec
3 ff4ec49e Fabrice Bellard
 * Copyright (c) 2001 Fabrice Bellard.
4 9b879566 Michel Bardiaux
 * Copyright (c) 2003 Michel Bardiaux for the av_log API
5 8f2ab833 Michael Niedermayer
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 de6d9b64 Fabrice Bellard
 *
7 ff4ec49e Fabrice Bellard
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2 of the License, or (at your option) any later version.
11 de6d9b64 Fabrice Bellard
 *
12 ff4ec49e Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ff4ec49e Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 ff4ec49e Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 de6d9b64 Fabrice Bellard
 */
21 983e3246 Michael Niedermayer
 
22
/**
23
 * @file utils.c
24
 * utils.
25
 */
26
 
27 de6d9b64 Fabrice Bellard
#include "avcodec.h"
28 3123dd79 Fabrice Bellard
#include "dsputil.h"
29 1c2a8c7f Michael Niedermayer
#include "mpegvideo.h"
30 9b879566 Michel Bardiaux
#include <stdarg.h>
31 de6d9b64 Fabrice Bellard
32 18f77016 Zdenek Kabelac
void *av_mallocz(unsigned int size)
33 3123dd79 Fabrice Bellard
{
34
    void *ptr;
35 5b20b732 Michael Niedermayer
    
36 3123dd79 Fabrice Bellard
    ptr = av_malloc(size);
37
    if (!ptr)
38
        return NULL;
39
    memset(ptr, 0, size);
40
    return ptr;
41
}
42
43 8e1e6f31 Fabrice Bellard
char *av_strdup(const char *s)
44
{
45
    char *ptr;
46
    int len;
47
    len = strlen(s) + 1;
48
    ptr = av_malloc(len);
49
    if (!ptr)
50
        return NULL;
51
    memcpy(ptr, s, len);
52
    return ptr;
53
}
54
55
/**
56
 * realloc which does nothing if the block is large enough
57
 */
58 5c91a675 Zdenek Kabelac
void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
59 8e1e6f31 Fabrice Bellard
{
60
    if(min_size < *size) 
61
        return ptr;
62
    
63 4e39ab4c Michael Niedermayer
    *size= 17*min_size/16 + 32;
64 8e1e6f31 Fabrice Bellard
65
    return av_realloc(ptr, *size);
66
}
67
68
69 855ea723 Zdenek Kabelac
static unsigned int last_static = 0;
70 8d1f2ba5 Michael Niedermayer
static unsigned int allocated_static = 0;
71
static void** array_static = NULL;
72
73
/**
74
 * allocation of static arrays - do not use for normal allocation.
75
 */
76
void *av_mallocz_static(unsigned int size)
77 855ea723 Zdenek Kabelac
{
78
    void *ptr = av_mallocz(size);
79
80 8d1f2ba5 Michael Niedermayer
    if(ptr){ 
81 4e39ab4c Michael Niedermayer
        array_static =av_fast_realloc(array_static, &allocated_static, sizeof(void*)*(last_static+1));
82 8d1f2ba5 Michael Niedermayer
        array_static[last_static++] = ptr;
83 855ea723 Zdenek Kabelac
    }
84 8d1f2ba5 Michael Niedermayer
85 855ea723 Zdenek Kabelac
    return ptr;
86
}
87 8d1f2ba5 Michael Niedermayer
88
/**
89
 * free all static arrays and reset pointers to 0.
90
 */
91 4cfbf61b Falk Hüffner
void av_free_static(void)
92 855ea723 Zdenek Kabelac
{
93 8d1f2ba5 Michael Niedermayer
    while(last_static){
94
        av_freep(&array_static[--last_static]);
95 855ea723 Zdenek Kabelac
    }
96 8d1f2ba5 Michael Niedermayer
    av_freep(&array_static);
97 855ea723 Zdenek Kabelac
}
98
99 81c5f887 Michael Niedermayer
/**
100
 * Frees memory and sets the pointer to NULL.
101
 * @param arg pointer to the pointer which should be freed
102
 */
103
void av_freep(void *arg)
104 4d7a0a05 Fabrice Bellard
{
105 81c5f887 Michael Niedermayer
    void **ptr= (void**)arg;
106 4d7a0a05 Fabrice Bellard
    av_free(*ptr);
107
    *ptr = NULL;
108
}
109
110 de6d9b64 Fabrice Bellard
/* encoder management */
111
AVCodec *first_avcodec;
112
113
void register_avcodec(AVCodec *format)
114
{
115
    AVCodec **p;
116
    p = &first_avcodec;
117
    while (*p != NULL) p = &(*p)->next;
118
    *p = format;
119
    format->next = NULL;
120
}
121
122 d90cf87b Michael Niedermayer
typedef struct InternalBuffer{
123 1e491e29 Michael Niedermayer
    int last_pic_num;
124 d90cf87b Michael Niedermayer
    uint8_t *base[4];
125 1e491e29 Michael Niedermayer
    uint8_t *data[4];
126 237e4938 Michael Niedermayer
    int linesize[4];
127 d90cf87b Michael Niedermayer
}InternalBuffer;
128
129
#define INTERNAL_BUFFER_SIZE 32
130 1e491e29 Michael Niedermayer
131 f0bbfc4a Michael Niedermayer
#define ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
132
133
void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
134
    int w_align= 1;    
135
    int h_align= 1;    
136
    
137
    switch(s->pix_fmt){
138
    case PIX_FMT_YUV420P:
139
    case PIX_FMT_YUV422:
140
    case PIX_FMT_YUV422P:
141
    case PIX_FMT_YUV444P:
142
    case PIX_FMT_GRAY8:
143
    case PIX_FMT_YUVJ420P:
144
    case PIX_FMT_YUVJ422P:
145
    case PIX_FMT_YUVJ444P:
146
        w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
147
        h_align= 16;
148
        break;
149
    case PIX_FMT_YUV411P:
150
        w_align=32;
151
        h_align=8;
152
        break;
153
    case PIX_FMT_YUV410P:
154
        if(s->codec_id == CODEC_ID_SVQ1){
155
            w_align=64;
156
            h_align=64;
157
        }
158
        break;
159
    default:
160
        w_align= 1;
161
        h_align= 1;
162
        break;
163
    }
164
165
    *width = ALIGN(*width , w_align);
166
    *height= ALIGN(*height, h_align);
167
}
168
169 492cd3a9 Michael Niedermayer
int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
170 1e491e29 Michael Niedermayer
    int i;
171 f0bbfc4a Michael Niedermayer
    int w= s->width;
172
    int h= s->height;
173 d90cf87b Michael Niedermayer
    InternalBuffer *buf;
174 237e4938 Michael Niedermayer
    int *picture_number;
175 4e00e76b Michael Niedermayer
    
176
    assert(pic->data[0]==NULL);
177 d90cf87b Michael Niedermayer
    assert(INTERNAL_BUFFER_SIZE > s->internal_buffer_count);
178 1e491e29 Michael Niedermayer
179 d90cf87b Michael Niedermayer
    if(s->internal_buffer==NULL){
180
        s->internal_buffer= av_mallocz(INTERNAL_BUFFER_SIZE*sizeof(InternalBuffer));
181
    }
182
#if 0
183
    s->internal_buffer= av_fast_realloc(
184
        s->internal_buffer, 
185
        &s->internal_buffer_size, 
186
        sizeof(InternalBuffer)*FFMAX(99,  s->internal_buffer_count+1)/*FIXME*/
187
        );
188
#endif
189
     
190
    buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
191 237e4938 Michael Niedermayer
    picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE-1]).last_pic_num; //FIXME ugly hack
192
    (*picture_number)++;
193
    
194 d90cf87b Michael Niedermayer
    if(buf->base[0]){
195 237e4938 Michael Niedermayer
        pic->age= *picture_number - buf->last_pic_num;
196
        buf->last_pic_num= *picture_number;
197 1e491e29 Michael Niedermayer
    }else{
198 f0bbfc4a Michael Niedermayer
        int h_chroma_shift, v_chroma_shift;
199
        int s_align, pixel_size;
200 1e491e29 Michael Niedermayer
        
201
        avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
202 f0bbfc4a Michael Niedermayer
        
203 1e491e29 Michael Niedermayer
        switch(s->pix_fmt){
204 ba88675b Michael Niedermayer
        case PIX_FMT_RGB555:
205
        case PIX_FMT_RGB565:
206 1e491e29 Michael Niedermayer
        case PIX_FMT_YUV422:
207
            pixel_size=2;
208
            break;
209
        case PIX_FMT_RGB24:
210
        case PIX_FMT_BGR24:
211
            pixel_size=3;
212
            break;
213
        case PIX_FMT_RGBA32:
214
            pixel_size=4;
215
            break;
216
        default:
217
            pixel_size=1;
218
        }
219 f0bbfc4a Michael Niedermayer
220
        avcodec_align_dimensions(s, &w, &h);
221
#if defined(ARCH_POWERPC) || defined(HAVE_MMI) //FIXME some cleaner check
222
        s_align= 16;
223
#else
224
        s_align= 8;
225
#endif
226
            
227 1e491e29 Michael Niedermayer
        if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
228
            w+= EDGE_WIDTH*2;
229
            h+= EDGE_WIDTH*2;
230
        }
231
        
232 d90cf87b Michael Niedermayer
        buf->last_pic_num= -256*256*256*64;
233 1e491e29 Michael Niedermayer
234
        for(i=0; i<3; i++){
235 2c19981a Michael Niedermayer
            const int h_shift= i==0 ? 0 : h_chroma_shift;
236
            const int v_shift= i==0 ? 0 : v_chroma_shift;
237 1e491e29 Michael Niedermayer
238 7984082a Michael Niedermayer
            //FIXME next ensures that linesize= 2^x uvlinesize, thats needed because some MC code assumes it
239
            buf->linesize[i]= ALIGN(pixel_size*w>>h_shift, s_align<<(h_chroma_shift-h_shift)); 
240 1e491e29 Michael Niedermayer
241 237e4938 Michael Niedermayer
            buf->base[i]= av_mallocz((buf->linesize[i]*h>>v_shift)+16); //FIXME 16
242 d90cf87b Michael Niedermayer
            if(buf->base[i]==NULL) return -1;
243 237e4938 Michael Niedermayer
            memset(buf->base[i], 128, buf->linesize[i]*h>>v_shift);
244 1e491e29 Michael Niedermayer
        
245
            if(s->flags&CODEC_FLAG_EMU_EDGE)
246 d90cf87b Michael Niedermayer
                buf->data[i] = buf->base[i];
247 1e491e29 Michael Niedermayer
            else
248 237e4938 Michael Niedermayer
                buf->data[i] = buf->base[i] + ALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), s_align);
249 1e491e29 Michael Niedermayer
        }
250
        pic->age= 256*256*256*64;
251
    }
252 237e4938 Michael Niedermayer
    pic->type= FF_BUFFER_TYPE_INTERNAL;
253 1e491e29 Michael Niedermayer
254 d90cf87b Michael Niedermayer
    for(i=0; i<4; i++){
255
        pic->base[i]= buf->base[i];
256
        pic->data[i]= buf->data[i];
257 237e4938 Michael Niedermayer
        pic->linesize[i]= buf->linesize[i];
258 d90cf87b Michael Niedermayer
    }
259
    s->internal_buffer_count++;
260
261 1e491e29 Michael Niedermayer
    return 0;
262
}
263
264 492cd3a9 Michael Niedermayer
void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
265 1e491e29 Michael Niedermayer
    int i;
266 d90cf87b Michael Niedermayer
    InternalBuffer *buf, *last, temp;
267
268 4e00e76b Michael Niedermayer
    assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
269 59c673d5 Michael Niedermayer
    assert(s->internal_buffer_count);
270 d90cf87b Michael Niedermayer
271 b1609412 Fabrice Bellard
    buf = NULL; /* avoids warning */
272 d90cf87b Michael Niedermayer
    for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
273
        buf= &((InternalBuffer*)s->internal_buffer)[i];
274
        if(buf->data[0] == pic->data[0])
275
            break;
276
    }
277
    assert(i < s->internal_buffer_count);
278
    s->internal_buffer_count--;
279
    last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
280
281
    temp= *buf;
282
    *buf= *last;
283
    *last= temp;
284
285
    for(i=0; i<3; i++){
286 1e491e29 Michael Niedermayer
        pic->data[i]=NULL;
287 d90cf87b Michael Niedermayer
//        pic->base[i]=NULL;
288
    }
289 1e491e29 Michael Niedermayer
//printf("R%X\n", pic->opaque);
290
}
291
292 e1c2a5a0 Roberto Togni
int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
293
    AVFrame temp_pic;
294
    int i;
295
296
    /* If no picture return a new buffer */
297
    if(pic->data[0] == NULL) {
298
        /* We will copy from buffer, so must be readable */
299
        pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
300
        return s->get_buffer(s, pic);
301
    }
302
303
    /* If internal buffer type return the same buffer */
304
    if(pic->type == FF_BUFFER_TYPE_INTERNAL)
305
        return 0;
306
307
    /*
308
     * Not internal type and reget_buffer not overridden, emulate cr buffer
309
     */
310
    temp_pic = *pic;
311
    for(i = 0; i < 4; i++)
312
        pic->data[i] = pic->base[i] = NULL;
313
    pic->opaque = NULL;
314
    /* Allocate new frame */
315
    if (s->get_buffer(s, pic))
316
        return -1;
317
    /* Copy image data from old buffer to new buffer */
318
    img_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
319
             s->height);
320
    s->release_buffer(s, &temp_pic); // Release old frame
321
    return 0;
322
}
323
324 9c3d33d6 Michael Niedermayer
int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
325
    int i;
326
327
    for(i=0; i<count; i++){
328
        int r= func(c, arg[i]);
329
        if(ret) ret[i]= r;
330
    }
331
    return 0;
332
}
333
334 494c56d3 Ivan Kalvachev
enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat * fmt){
335 a33c7159 Michael Niedermayer
    return fmt[0];
336
}
337
338 43465395 Michael Niedermayer
static const char* context_to_name(void* ptr) {
339
    AVCodecContext *avc= ptr;
340
341
    if(avc && avc->codec && avc->codec->name)
342
        return avc->codec->name; 
343
    else
344
        return "NULL";
345
}
346
347
static AVClass av_codec_context_class = { "AVCodecContext", context_to_name };
348
349 a949d72e Michael Niedermayer
void avcodec_get_context_defaults(AVCodecContext *s){
350 9740beff Michael Niedermayer
    memset(s, 0, sizeof(AVCodecContext));
351
352 43465395 Michael Niedermayer
    s->av_class= &av_codec_context_class;
353 e8b62df6 Michael Niedermayer
    s->bit_rate= 800*1000;
354
    s->bit_rate_tolerance= s->bit_rate*10;
355 a949d72e Michael Niedermayer
    s->qmin= 2;
356
    s->qmax= 31;
357 17a70fde Michael Niedermayer
    s->mb_qmin= 2;
358
    s->mb_qmax= 31;
359 a949d72e Michael Niedermayer
    s->rc_eq= "tex^qComp";
360
    s->qcompress= 0.5;
361 e8b62df6 Michael Niedermayer
    s->max_qdiff= 3;
362
    s->b_quant_factor=1.25;
363
    s->b_quant_offset=1.25;
364 b3a391e8 Michael Niedermayer
    s->i_quant_factor=-0.8;
365 e8b62df6 Michael Niedermayer
    s->i_quant_offset=0.0;
366 4d2858de Michael Niedermayer
    s->error_concealment= 3;
367 8d0e42ca Michael Niedermayer
    s->error_resilience= 1;
368 4d2858de Michael Niedermayer
    s->workaround_bugs= FF_BUG_AUTODETECT;
369 14bea432 Michael Niedermayer
    s->frame_rate_base= 1;
370
    s->frame_rate = 25;
371 8d0e42ca Michael Niedermayer
    s->gop_size= 50;
372
    s->me_method= ME_EPZS;
373 1e491e29 Michael Niedermayer
    s->get_buffer= avcodec_default_get_buffer;
374
    s->release_buffer= avcodec_default_release_buffer;
375 a33c7159 Michael Niedermayer
    s->get_format= avcodec_default_get_format;
376 9c3d33d6 Michael Niedermayer
    s->execute= avcodec_default_execute;
377
    s->thread_count=1;
378 826f429a Michael Niedermayer
    s->me_subpel_quality=8;
379 158c7f05 Michael Niedermayer
    s->lmin= FF_QP2LAMBDA * s->qmin;
380
    s->lmax= FF_QP2LAMBDA * s->qmax;
381 5ff85f1d Michael Niedermayer
    s->sample_aspect_ratio= (AVRational){0,1};
382 620fe604 Michael Niedermayer
    s->ildct_cmp= FF_CMP_VSAD;
383 1984f635 Michael Niedermayer
    
384
    s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
385
    s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
386 2a2bbcb0 Mike Melanson
    s->palctrl = NULL;
387 e1c2a5a0 Roberto Togni
    s->reget_buffer= avcodec_default_reget_buffer;
388 a949d72e Michael Niedermayer
}
389
390
/**
391
 * allocates a AVCodecContext and set it to defaults.
392
 * this can be deallocated by simply calling free() 
393
 */
394 7ffbb60e Falk Hüffner
AVCodecContext *avcodec_alloc_context(void){
395 9740beff Michael Niedermayer
    AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
396 a949d72e Michael Niedermayer
    
397
    if(avctx==NULL) return NULL;
398
    
399
    avcodec_get_context_defaults(avctx);
400
    
401
    return avctx;
402
}
403
404 9740beff Michael Niedermayer
void avcodec_get_frame_defaults(AVFrame *pic){
405
    memset(pic, 0, sizeof(AVFrame));
406
407
    pic->pts= AV_NOPTS_VALUE;
408
}
409
410 1e491e29 Michael Niedermayer
/**
411 492cd3a9 Michael Niedermayer
 * allocates a AVPFrame and set it to defaults.
412 1e491e29 Michael Niedermayer
 * this can be deallocated by simply calling free() 
413
 */
414 492cd3a9 Michael Niedermayer
AVFrame *avcodec_alloc_frame(void){
415 9740beff Michael Niedermayer
    AVFrame *pic= av_malloc(sizeof(AVFrame));
416
    
417
    if(pic==NULL) return NULL;
418
    
419
    avcodec_get_frame_defaults(pic);
420 1e491e29 Michael Niedermayer
    
421
    return pic;
422
}
423
424 de6d9b64 Fabrice Bellard
int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
425
{
426
    int ret;
427
428 09770af8 Michael Niedermayer
    if(avctx->codec)
429
        return -1;
430
431 de6d9b64 Fabrice Bellard
    avctx->codec = codec;
432 4cb3ca72 Zdenek Kabelac
    avctx->codec_id = codec->id;
433 de6d9b64 Fabrice Bellard
    avctx->frame_number = 0;
434 0edf8a7a Philip Gladstone
    if (codec->priv_data_size > 0) {
435
        avctx->priv_data = av_mallocz(codec->priv_data_size);
436
        if (!avctx->priv_data) 
437
            return -ENOMEM;
438
    } else {
439
        avctx->priv_data = NULL;
440
    }
441 de6d9b64 Fabrice Bellard
    ret = avctx->codec->init(avctx);
442
    if (ret < 0) {
443 3123dd79 Fabrice Bellard
        av_freep(&avctx->priv_data);
444 de6d9b64 Fabrice Bellard
        return ret;
445
    }
446
    return 0;
447
}
448
449 0c1a9eda Zdenek Kabelac
int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size, 
450 de6d9b64 Fabrice Bellard
                         const short *samples)
451
{
452
    int ret;
453
454
    ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
455
    avctx->frame_number++;
456
    return ret;
457
}
458
459 0c1a9eda Zdenek Kabelac
int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size, 
460 492cd3a9 Michael Niedermayer
                         const AVFrame *pict)
461 de6d9b64 Fabrice Bellard
{
462
    int ret;
463
464
    ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
465 6bb925f4 Michael Niedermayer
    
466
    emms_c(); //needed to avoid a emms_c() call before every return;
467
468 de6d9b64 Fabrice Bellard
    avctx->frame_number++;
469
    return ret;
470
}
471
472 f138f883 Michael Niedermayer
/** 
473
 * decode a frame. 
474
 * @param buf bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE larger then the actual read bytes
475
 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
476
 * @param buf_size the size of the buffer in bytes
477
 * @param got_picture_ptr zero if no frame could be decompressed, Otherwise, it is non zero
478
 * @return -1 if error, otherwise return the number of
479
 * bytes used. 
480
 */
481 492cd3a9 Michael Niedermayer
int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture, 
482 de6d9b64 Fabrice Bellard
                         int *got_picture_ptr,
483 0c1a9eda Zdenek Kabelac
                         uint8_t *buf, int buf_size)
484 de6d9b64 Fabrice Bellard
{
485
    int ret;
486 1e491e29 Michael Niedermayer
    
487 de6d9b64 Fabrice Bellard
    ret = avctx->codec->decode(avctx, picture, got_picture_ptr, 
488
                               buf, buf_size);
489 6bb925f4 Michael Niedermayer
490
    emms_c(); //needed to avoid a emms_c() call before every return;
491 1e491e29 Michael Niedermayer
    
492 1cb0edb4 Juanjo
    if (*got_picture_ptr)                           
493
        avctx->frame_number++;
494 de6d9b64 Fabrice Bellard
    return ret;
495
}
496
497
/* decode an audio frame. return -1 if error, otherwise return the
498
   *number of bytes used. If no frame could be decompressed,
499
   *frame_size_ptr is zero. Otherwise, it is the decompressed frame
500
   *size in BYTES. */
501 0c1a9eda Zdenek Kabelac
int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples, 
502 de6d9b64 Fabrice Bellard
                         int *frame_size_ptr,
503 0c1a9eda Zdenek Kabelac
                         uint8_t *buf, int buf_size)
504 de6d9b64 Fabrice Bellard
{
505
    int ret;
506
507
    ret = avctx->codec->decode(avctx, samples, frame_size_ptr, 
508
                               buf, buf_size);
509
    avctx->frame_number++;
510
    return ret;
511
}
512
513
int avcodec_close(AVCodecContext *avctx)
514
{
515
    if (avctx->codec->close)
516
        avctx->codec->close(avctx);
517 3123dd79 Fabrice Bellard
    av_freep(&avctx->priv_data);
518 de6d9b64 Fabrice Bellard
    avctx->codec = NULL;
519
    return 0;
520
}
521
522
AVCodec *avcodec_find_encoder(enum CodecID id)
523
{
524
    AVCodec *p;
525
    p = first_avcodec;
526
    while (p) {
527
        if (p->encode != NULL && p->id == id)
528
            return p;
529
        p = p->next;
530
    }
531
    return NULL;
532
}
533
534 98f3b098 Arpi
AVCodec *avcodec_find_encoder_by_name(const char *name)
535
{
536
    AVCodec *p;
537
    p = first_avcodec;
538
    while (p) {
539
        if (p->encode != NULL && strcmp(name,p->name) == 0)
540
            return p;
541
        p = p->next;
542
    }
543
    return NULL;
544
}
545
546 de6d9b64 Fabrice Bellard
AVCodec *avcodec_find_decoder(enum CodecID id)
547
{
548
    AVCodec *p;
549
    p = first_avcodec;
550
    while (p) {
551
        if (p->decode != NULL && p->id == id)
552
            return p;
553
        p = p->next;
554
    }
555
    return NULL;
556
}
557
558
AVCodec *avcodec_find_decoder_by_name(const char *name)
559
{
560
    AVCodec *p;
561
    p = first_avcodec;
562
    while (p) {
563
        if (p->decode != NULL && strcmp(name,p->name) == 0)
564
            return p;
565
        p = p->next;
566
    }
567
    return NULL;
568
}
569
570
AVCodec *avcodec_find(enum CodecID id)
571
{
572
    AVCodec *p;
573
    p = first_avcodec;
574
    while (p) {
575
        if (p->id == id)
576
            return p;
577
        p = p->next;
578
    }
579
    return NULL;
580
}
581
582
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
583
{
584
    const char *codec_name;
585
    AVCodec *p;
586
    char buf1[32];
587 623563c0 Zdenek Kabelac
    char channels_str[100];
588 a96b68b7 Fabrice Bellard
    int bitrate;
589 de6d9b64 Fabrice Bellard
590
    if (encode)
591
        p = avcodec_find_encoder(enc->codec_id);
592
    else
593
        p = avcodec_find_decoder(enc->codec_id);
594
595
    if (p) {
596
        codec_name = p->name;
597 98ce5991 Fabrice Bellard
        if (!encode && enc->codec_id == CODEC_ID_MP3) {
598
            if (enc->sub_id == 2)
599
                codec_name = "mp2";
600
            else if (enc->sub_id == 1)
601
                codec_name = "mp1";
602
        }
603 985180a1 Fabrice Bellard
    } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
604
        /* fake mpeg2 transport stream codec (currently not
605
           registered) */
606
        codec_name = "mpeg2ts";
607 de6d9b64 Fabrice Bellard
    } else if (enc->codec_name[0] != '\0') {
608
        codec_name = enc->codec_name;
609
    } else {
610
        /* output avi tags */
611
        if (enc->codec_type == CODEC_TYPE_VIDEO) {
612
            snprintf(buf1, sizeof(buf1), "%c%c%c%c", 
613
                     enc->codec_tag & 0xff,
614
                     (enc->codec_tag >> 8) & 0xff,
615
                     (enc->codec_tag >> 16) & 0xff,
616
                     (enc->codec_tag >> 24) & 0xff);
617
        } else {
618
            snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
619
        }
620
        codec_name = buf1;
621
    }
622
623
    switch(enc->codec_type) {
624
    case CODEC_TYPE_VIDEO:
625
        snprintf(buf, buf_size,
626
                 "Video: %s%s",
627 7d1c3fc1 Michael Niedermayer
                 codec_name, enc->mb_decision ? " (hq)" : "");
628 cf087595 Fabrice Bellard
        if (enc->codec_id == CODEC_ID_RAWVIDEO) {
629
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
630
                     ", %s",
631 9bbffbb1 Fabrice Bellard
                     avcodec_get_pix_fmt_name(enc->pix_fmt));
632 cf087595 Fabrice Bellard
        }
633 de6d9b64 Fabrice Bellard
        if (enc->width) {
634
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
635
                     ", %dx%d, %0.2f fps",
636
                     enc->width, enc->height, 
637 14bea432 Michael Niedermayer
                     (float)enc->frame_rate / enc->frame_rate_base);
638 de6d9b64 Fabrice Bellard
        }
639 4bfad535 Fabrice Bellard
        if (encode) {
640
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
641
                     ", q=%d-%d", enc->qmin, enc->qmax);
642
        }
643 a96b68b7 Fabrice Bellard
        bitrate = enc->bit_rate;
644 de6d9b64 Fabrice Bellard
        break;
645
    case CODEC_TYPE_AUDIO:
646
        snprintf(buf, buf_size,
647
                 "Audio: %s",
648
                 codec_name);
649 e0d2714a Juanjo
        switch (enc->channels) {
650
            case 1:
651 623563c0 Zdenek Kabelac
                strcpy(channels_str, "mono");
652 e0d2714a Juanjo
                break;
653
            case 2:
654 623563c0 Zdenek Kabelac
                strcpy(channels_str, "stereo");
655 e0d2714a Juanjo
                break;
656
            case 6:
657 623563c0 Zdenek Kabelac
                strcpy(channels_str, "5:1");
658 e0d2714a Juanjo
                break;
659
            default:
660
                sprintf(channels_str, "%d channels", enc->channels);
661
                break;
662
        }
663 de6d9b64 Fabrice Bellard
        if (enc->sample_rate) {
664
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
665
                     ", %d Hz, %s",
666
                     enc->sample_rate,
667 e0d2714a Juanjo
                     channels_str);
668 de6d9b64 Fabrice Bellard
        }
669 e0d2714a Juanjo
        
670 a96b68b7 Fabrice Bellard
        /* for PCM codecs, compute bitrate directly */
671
        switch(enc->codec_id) {
672
        case CODEC_ID_PCM_S16LE:
673
        case CODEC_ID_PCM_S16BE:
674
        case CODEC_ID_PCM_U16LE:
675
        case CODEC_ID_PCM_U16BE:
676 a190b7e9 Fabrice Bellard
            bitrate = enc->sample_rate * enc->channels * 16;
677 a96b68b7 Fabrice Bellard
            break;
678
        case CODEC_ID_PCM_S8:
679
        case CODEC_ID_PCM_U8:
680
        case CODEC_ID_PCM_ALAW:
681
        case CODEC_ID_PCM_MULAW:
682 a190b7e9 Fabrice Bellard
            bitrate = enc->sample_rate * enc->channels * 8;
683 a96b68b7 Fabrice Bellard
            break;
684
        default:
685
            bitrate = enc->bit_rate;
686
            break;
687
        }
688 de6d9b64 Fabrice Bellard
        break;
689 985180a1 Fabrice Bellard
    case CODEC_TYPE_DATA:
690
        snprintf(buf, buf_size, "Data: %s", codec_name);
691
        bitrate = enc->bit_rate;
692
        break;
693 de6d9b64 Fabrice Bellard
    default:
694 b71472eb Philip Gladstone
        av_abort();
695 de6d9b64 Fabrice Bellard
    }
696 4bfad535 Fabrice Bellard
    if (encode) {
697
        if (enc->flags & CODEC_FLAG_PASS1)
698
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
699
                     ", pass 1");
700
        if (enc->flags & CODEC_FLAG_PASS2)
701
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
702
                     ", pass 2");
703
    }
704 a96b68b7 Fabrice Bellard
    if (bitrate != 0) {
705 de6d9b64 Fabrice Bellard
        snprintf(buf + strlen(buf), buf_size - strlen(buf), 
706 a96b68b7 Fabrice Bellard
                 ", %d kb/s", bitrate / 1000);
707 de6d9b64 Fabrice Bellard
    }
708
}
709
710 156e5023 Nick Kurshev
unsigned avcodec_version( void )
711
{
712
  return LIBAVCODEC_VERSION_INT;
713
}
714 cf087595 Fabrice Bellard
715 8bceb6af Nick Kurshev
unsigned avcodec_build( void )
716
{
717
  return LIBAVCODEC_BUILD;
718
}
719
720 de6d9b64 Fabrice Bellard
/* must be called before any other functions */
721
void avcodec_init(void)
722
{
723 0344cd0a Alex Beregszaszi
    static int inited = 0;
724
725
    if (inited != 0)
726
        return;
727
    inited = 1;
728
729 59cf08ce Fabrice Bellard
    dsputil_static_init();
730 de6d9b64 Fabrice Bellard
}
731
732 7a06ff14 Michael Niedermayer
/**
733
 * Flush buffers, should be called when seeking or when swicthing to a different stream.
734
 */
735 1c2a8c7f Michael Niedermayer
void avcodec_flush_buffers(AVCodecContext *avctx)
736
{
737 7a06ff14 Michael Niedermayer
    if(avctx->codec->flush)
738
        avctx->codec->flush(avctx);
739 1c2a8c7f Michael Niedermayer
}
740
741 d90cf87b Michael Niedermayer
void avcodec_default_free_buffers(AVCodecContext *s){
742
    int i, j;
743
744
    if(s->internal_buffer==NULL) return;
745
    
746
    for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
747
        InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
748
        for(j=0; j<4; j++){
749
            av_freep(&buf->base[j]);
750
            buf->data[j]= NULL;
751
        }
752
    }
753
    av_freep(&s->internal_buffer);
754
    
755
    s->internal_buffer_count=0;
756
}
757
758 d8085ea7 Michael Niedermayer
char av_get_pict_type_char(int pict_type){
759
    switch(pict_type){
760
    case I_TYPE: return 'I'; 
761
    case P_TYPE: return 'P'; 
762
    case B_TYPE: return 'B'; 
763
    case S_TYPE: return 'S'; 
764
    case SI_TYPE:return 'i'; 
765
    case SP_TYPE:return 'p'; 
766
    default:     return '?';
767
    }
768
}
769
770 14bea432 Michael Niedermayer
int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
771
    int exact=1, sign=0;
772 5ed9f2e5 Michael Niedermayer
    int64_t gcd;
773 14bea432 Michael Niedermayer
774
    assert(den != 0);
775
776
    if(den < 0){
777
        den= -den;
778
        nom= -nom;
779
    }
780
    
781
    if(nom < 0){
782
        nom= -nom;
783
        sign= 1;
784
    }
785
    
786 5ed9f2e5 Michael Niedermayer
    gcd = ff_gcd(nom, den);
787
    nom /= gcd;
788
    den /= gcd;
789 14bea432 Michael Niedermayer
    
790 5ed9f2e5 Michael Niedermayer
    if(nom > max || den > max){
791
        AVRational a0={0,1}, a1={1,0};
792
        exact=0;
793
794
        for(;;){
795
            int64_t x= nom / den;
796
            int64_t a2n= x*a1.num + a0.num;
797
            int64_t a2d= x*a1.den + a0.den;
798
799
            if(a2n > max || a2d > max) break;
800
801
            nom %= den;
802
        
803
            a0= a1;
804
            a1= (AVRational){a2n, a2d};
805
            if(nom==0) break;
806
            x= nom; nom=den; den=x;
807
        }
808
        nom= a1.num;
809
        den= a1.den;
810 14bea432 Michael Niedermayer
    }
811
    
812 5ed9f2e5 Michael Niedermayer
    assert(ff_gcd(nom, den) == 1);
813
    
814 14bea432 Michael Niedermayer
    if(sign) nom= -nom;
815
    
816
    *dst_nom = nom;
817
    *dst_den = den;
818
    
819
    return exact;
820
}
821
822
int64_t av_rescale(int64_t a, int b, int c){
823
    uint64_t h, l;
824
    assert(c > 0);
825
    assert(b >=0);
826
    
827
    if(a<0) return -av_rescale(-a, b, c);
828
    
829
    h= a>>32;
830
    if(h==0) return a*b/c;
831
    
832
    l= a&0xFFFFFFFF;
833
    l *= b;
834
    h *= b;
835
836
    l += (h%c)<<32;
837
838
    return ((h/c)<<32) + l/c;
839
}
840 9b879566 Michel Bardiaux
841
/* av_log API */
842
843
static int av_log_level = AV_LOG_DEBUG;
844
845 bc874dae Michel Bardiaux
static void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
846 9b879566 Michel Bardiaux
{
847 89b9441a Michael Niedermayer
    static int print_prefix=1;
848 43465395 Michael Niedermayer
    AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
849 9b879566 Michel Bardiaux
    if(level>av_log_level)
850 bc874dae Michel Bardiaux
        return;
851 d705e4a6 Michael Niedermayer
#undef fprintf
852 43465395 Michael Niedermayer
    if(print_prefix && avc) {
853
            fprintf(stderr, "[%s @ %p]", avc->item_name(ptr), avc);
854 bc874dae Michel Bardiaux
    }
855 d705e4a6 Michael Niedermayer
#define fprintf please_use_av_log
856 89b9441a Michael Niedermayer
        
857 95ba2c8f Falk Hüffner
    print_prefix= strstr(fmt, "\n") != NULL;
858 89b9441a Michael Niedermayer
        
859 9b879566 Michel Bardiaux
    vfprintf(stderr, fmt, vl);
860
}
861
862 bc874dae Michel Bardiaux
static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback;
863 9b879566 Michel Bardiaux
864 bc874dae Michel Bardiaux
void av_log(void* avcl, int level, const char *fmt, ...)
865 9b879566 Michel Bardiaux
{
866
    va_list vl;
867
    va_start(vl, fmt);
868 bc874dae Michel Bardiaux
    av_vlog(avcl, level, fmt, vl);
869 9b879566 Michel Bardiaux
    va_end(vl);
870
}
871
872 bc874dae Michel Bardiaux
void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
873 9b879566 Michel Bardiaux
{
874 bc874dae Michel Bardiaux
    av_log_callback(avcl, level, fmt, vl);
875 9b879566 Michel Bardiaux
}
876
877
int av_log_get_level(void)
878
{
879
    return av_log_level;
880
}
881
882
void av_log_set_level(int level)
883
{
884
    av_log_level = level;
885
}
886
887 bc874dae Michel Bardiaux
void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
888 9b879566 Michel Bardiaux
{
889
    av_log_callback = callback;
890
}