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