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