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