Statistics
| Branch: | Revision:

ffmpeg / libavcodec / utils.c @ 4e39ab4c

History | View | Annotate | Download (22.3 KB)

1
/*
2
 * utils for libavcodec
3
 * Copyright (c) 2001 Fabrice Bellard.
4
 * Copyright (c) 2003 Michel Bardiaux for the av_log API
5
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6
 *
7
 * 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
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * 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
 */
21
 
22
/**
23
 * @file utils.c
24
 * utils.
25
 */
26
 
27
#include "avcodec.h"
28
#include "dsputil.h"
29
#include "mpegvideo.h"
30
#include <stdarg.h>
31

    
32
void *av_mallocz(unsigned int size)
33
{
34
    void *ptr;
35
    
36
    ptr = av_malloc(size);
37
    if (!ptr)
38
        return NULL;
39
    memset(ptr, 0, size);
40
    return ptr;
41
}
42

    
43
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
void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
59
{
60
    if(min_size < *size) 
61
        return ptr;
62
    
63
    *size= 17*min_size/16 + 32;
64

    
65
    return av_realloc(ptr, *size);
66
}
67

    
68

    
69
static unsigned int last_static = 0;
70
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
{
78
    void *ptr = av_mallocz(size);
79

    
80
    if(ptr){ 
81
        array_static =av_fast_realloc(array_static, &allocated_static, sizeof(void*)*(last_static+1));
82
        array_static[last_static++] = ptr;
83
    }
84

    
85
    return ptr;
86
}
87

    
88
/**
89
 * free all static arrays and reset pointers to 0.
90
 */
91
void av_free_static(void)
92
{
93
    while(last_static){
94
        av_freep(&array_static[--last_static]);
95
    }
96
    av_freep(&array_static);
97
}
98

    
99
/**
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
{
105
    void **ptr= (void**)arg;
106
    av_free(*ptr);
107
    *ptr = NULL;
108
}
109

    
110
/* 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
typedef struct InternalBuffer{
123
    int last_pic_num;
124
    uint8_t *base[4];
125
    uint8_t *data[4];
126
    int linesize[4];
127
}InternalBuffer;
128

    
129
#define INTERNAL_BUFFER_SIZE 32
130

    
131
#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
int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
170
    int i;
171
    int w= s->width;
172
    int h= s->height;
173
    InternalBuffer *buf;
174
    int *picture_number;
175
    
176
    assert(pic->data[0]==NULL);
177
    assert(INTERNAL_BUFFER_SIZE > s->internal_buffer_count);
178

    
179
    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
    picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE-1]).last_pic_num; //FIXME ugly hack
192
    (*picture_number)++;
193
    
194
    if(buf->base[0]){
195
        pic->age= *picture_number - buf->last_pic_num;
196
        buf->last_pic_num= *picture_number;
197
    }else{
198
        int h_chroma_shift, v_chroma_shift;
199
        int s_align, pixel_size;
200
        
201
        avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
202
        
203
        switch(s->pix_fmt){
204
        case PIX_FMT_RGB555:
205
        case PIX_FMT_RGB565:
206
        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

    
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
        if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
228
            w+= EDGE_WIDTH*2;
229
            h+= EDGE_WIDTH*2;
230
        }
231
        
232
        buf->last_pic_num= -256*256*256*64;
233

    
234
        for(i=0; i<3; i++){
235
            const int h_shift= i==0 ? 0 : h_chroma_shift;
236
            const int v_shift= i==0 ? 0 : v_chroma_shift;
237

    
238
            //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

    
241
            buf->base[i]= av_mallocz((buf->linesize[i]*h>>v_shift)+16); //FIXME 16
242
            if(buf->base[i]==NULL) return -1;
243
            memset(buf->base[i], 128, buf->linesize[i]*h>>v_shift);
244
        
245
            if(s->flags&CODEC_FLAG_EMU_EDGE)
246
                buf->data[i] = buf->base[i];
247
            else
248
                buf->data[i] = buf->base[i] + ALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), s_align);
249
        }
250
        pic->age= 256*256*256*64;
251
    }
252
    pic->type= FF_BUFFER_TYPE_INTERNAL;
253

    
254
    for(i=0; i<4; i++){
255
        pic->base[i]= buf->base[i];
256
        pic->data[i]= buf->data[i];
257
        pic->linesize[i]= buf->linesize[i];
258
    }
259
    s->internal_buffer_count++;
260

    
261
    return 0;
262
}
263

    
264
void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
265
    int i;
266
    InternalBuffer *buf, *last, temp;
267

    
268
    assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
269
    assert(s->internal_buffer_count);
270

    
271
    buf = NULL; /* avoids warning */
272
    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
        pic->data[i]=NULL;
287
//        pic->base[i]=NULL;
288
    }
289
//printf("R%X\n", pic->opaque);
290
}
291

    
292
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
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
enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat * fmt){
335
    return fmt[0];
336
}
337

    
338
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
void avcodec_get_context_defaults(AVCodecContext *s){
350
    memset(s, 0, sizeof(AVCodecContext));
351

    
352
    s->av_class= &av_codec_context_class;
353
    s->bit_rate= 800*1000;
354
    s->bit_rate_tolerance= s->bit_rate*10;
355
    s->qmin= 2;
356
    s->qmax= 31;
357
    s->mb_qmin= 2;
358
    s->mb_qmax= 31;
359
    s->rc_eq= "tex^qComp";
360
    s->qcompress= 0.5;
361
    s->max_qdiff= 3;
362
    s->b_quant_factor=1.25;
363
    s->b_quant_offset=1.25;
364
    s->i_quant_factor=-0.8;
365
    s->i_quant_offset=0.0;
366
    s->error_concealment= 3;
367
    s->error_resilience= 1;
368
    s->workaround_bugs= FF_BUG_AUTODETECT;
369
    s->frame_rate_base= 1;
370
    s->frame_rate = 25;
371
    s->gop_size= 50;
372
    s->me_method= ME_EPZS;
373
    s->get_buffer= avcodec_default_get_buffer;
374
    s->release_buffer= avcodec_default_release_buffer;
375
    s->get_format= avcodec_default_get_format;
376
    s->execute= avcodec_default_execute;
377
    s->thread_count=1;
378
    s->me_subpel_quality=8;
379
    s->lmin= FF_QP2LAMBDA * s->qmin;
380
    s->lmax= FF_QP2LAMBDA * s->qmax;
381
    s->sample_aspect_ratio= (AVRational){0,1};
382
    s->ildct_cmp= FF_CMP_VSAD;
383
    
384
    s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
385
    s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
386
    s->palctrl = NULL;
387
    s->reget_buffer= avcodec_default_reget_buffer;
388
}
389

    
390
/**
391
 * allocates a AVCodecContext and set it to defaults.
392
 * this can be deallocated by simply calling free() 
393
 */
394
AVCodecContext *avcodec_alloc_context(void){
395
    AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));
396
    
397
    if(avctx==NULL) return NULL;
398
    
399
    avcodec_get_context_defaults(avctx);
400
    
401
    return avctx;
402
}
403

    
404
void avcodec_get_frame_defaults(AVFrame *pic){
405
    memset(pic, 0, sizeof(AVFrame));
406

    
407
    pic->pts= AV_NOPTS_VALUE;
408
}
409

    
410
/**
411
 * allocates a AVPFrame and set it to defaults.
412
 * this can be deallocated by simply calling free() 
413
 */
414
AVFrame *avcodec_alloc_frame(void){
415
    AVFrame *pic= av_malloc(sizeof(AVFrame));
416
    
417
    if(pic==NULL) return NULL;
418
    
419
    avcodec_get_frame_defaults(pic);
420
    
421
    return pic;
422
}
423

    
424
int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
425
{
426
    int ret;
427

    
428
    if(avctx->codec)
429
        return -1;
430

    
431
    avctx->codec = codec;
432
    avctx->codec_id = codec->id;
433
    avctx->frame_number = 0;
434
    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
    ret = avctx->codec->init(avctx);
442
    if (ret < 0) {
443
        av_freep(&avctx->priv_data);
444
        return ret;
445
    }
446
    return 0;
447
}
448

    
449
int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size, 
450
                         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
int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size, 
460
                         const AVFrame *pict)
461
{
462
    int ret;
463

    
464
    ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
465
    
466
    emms_c(); //needed to avoid a emms_c() call before every return;
467

    
468
    avctx->frame_number++;
469
    return ret;
470
}
471

    
472
/** 
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
int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture, 
482
                         int *got_picture_ptr,
483
                         uint8_t *buf, int buf_size)
484
{
485
    int ret;
486
    
487
    ret = avctx->codec->decode(avctx, picture, got_picture_ptr, 
488
                               buf, buf_size);
489

    
490
    emms_c(); //needed to avoid a emms_c() call before every return;
491
    
492
    if (*got_picture_ptr)                           
493
        avctx->frame_number++;
494
    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
int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples, 
502
                         int *frame_size_ptr,
503
                         uint8_t *buf, int buf_size)
504
{
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
    av_freep(&avctx->priv_data);
518
    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
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
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
    char channels_str[100];
588
    int bitrate;
589

    
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
        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
    } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
604
        /* fake mpeg2 transport stream codec (currently not
605
           registered) */
606
        codec_name = "mpeg2ts";
607
    } 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
                 codec_name, enc->mb_decision ? " (hq)" : "");
628
        if (enc->codec_id == CODEC_ID_RAWVIDEO) {
629
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
630
                     ", %s",
631
                     avcodec_get_pix_fmt_name(enc->pix_fmt));
632
        }
633
        if (enc->width) {
634
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
635
                     ", %dx%d, %0.2f fps",
636
                     enc->width, enc->height, 
637
                     (float)enc->frame_rate / enc->frame_rate_base);
638
        }
639
        if (encode) {
640
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
641
                     ", q=%d-%d", enc->qmin, enc->qmax);
642
        }
643
        bitrate = enc->bit_rate;
644
        break;
645
    case CODEC_TYPE_AUDIO:
646
        snprintf(buf, buf_size,
647
                 "Audio: %s",
648
                 codec_name);
649
        switch (enc->channels) {
650
            case 1:
651
                strcpy(channels_str, "mono");
652
                break;
653
            case 2:
654
                strcpy(channels_str, "stereo");
655
                break;
656
            case 6:
657
                strcpy(channels_str, "5:1");
658
                break;
659
            default:
660
                sprintf(channels_str, "%d channels", enc->channels);
661
                break;
662
        }
663
        if (enc->sample_rate) {
664
            snprintf(buf + strlen(buf), buf_size - strlen(buf),
665
                     ", %d Hz, %s",
666
                     enc->sample_rate,
667
                     channels_str);
668
        }
669
        
670
        /* 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
            bitrate = enc->sample_rate * enc->channels * 16;
677
            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
            bitrate = enc->sample_rate * enc->channels * 8;
683
            break;
684
        default:
685
            bitrate = enc->bit_rate;
686
            break;
687
        }
688
        break;
689
    case CODEC_TYPE_DATA:
690
        snprintf(buf, buf_size, "Data: %s", codec_name);
691
        bitrate = enc->bit_rate;
692
        break;
693
    default:
694
        av_abort();
695
    }
696
    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
    if (bitrate != 0) {
705
        snprintf(buf + strlen(buf), buf_size - strlen(buf), 
706
                 ", %d kb/s", bitrate / 1000);
707
    }
708
}
709

    
710
unsigned avcodec_version( void )
711
{
712
  return LIBAVCODEC_VERSION_INT;
713
}
714

    
715
unsigned avcodec_build( void )
716
{
717
  return LIBAVCODEC_BUILD;
718
}
719

    
720
/* must be called before any other functions */
721
void avcodec_init(void)
722
{
723
    static int inited = 0;
724

    
725
    if (inited != 0)
726
        return;
727
    inited = 1;
728

    
729
    dsputil_static_init();
730
}
731

    
732
/**
733
 * Flush buffers, should be called when seeking or when swicthing to a different stream.
734
 */
735
void avcodec_flush_buffers(AVCodecContext *avctx)
736
{
737
    if(avctx->codec->flush)
738
        avctx->codec->flush(avctx);
739
}
740

    
741
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
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
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
    int64_t gcd;
773

    
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
    gcd = ff_gcd(nom, den);
787
    nom /= gcd;
788
    den /= gcd;
789
    
790
    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
    }
811
    
812
    assert(ff_gcd(nom, den) == 1);
813
    
814
    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

    
841
/* av_log API */
842

    
843
static int av_log_level = AV_LOG_DEBUG;
844

    
845
static void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
846
{
847
    static int print_prefix=1;
848
    AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
849
    if(level>av_log_level)
850
        return;
851
#undef fprintf
852
    if(print_prefix && avc) {
853
            fprintf(stderr, "[%s @ %p]", avc->item_name(ptr), avc);
854
    }
855
#define fprintf please_use_av_log
856
        
857
    print_prefix= strstr(fmt, "\n") != NULL;
858
        
859
    vfprintf(stderr, fmt, vl);
860
}
861

    
862
static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback;
863

    
864
void av_log(void* avcl, int level, const char *fmt, ...)
865
{
866
    va_list vl;
867
    va_start(vl, fmt);
868
    av_vlog(avcl, level, fmt, vl);
869
    va_end(vl);
870
}
871

    
872
void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
873
{
874
    av_log_callback(avcl, level, fmt, vl);
875
}
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
void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
888
{
889
    av_log_callback = callback;
890
}
891