doxy
[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 */
de6d9b64 19#include "avcodec.h"
3123dd79 20#include "dsputil.h"
1c2a8c7f 21#include "mpegvideo.h"
de6d9b64 22
18f77016 23void *av_mallocz(unsigned int size)
3123dd79
FB
24{
25 void *ptr;
5b20b732 26
3123dd79
FB
27 ptr = av_malloc(size);
28 if (!ptr)
29 return NULL;
30 memset(ptr, 0, size);
31 return ptr;
32}
33
8e1e6f31
FB
34char *av_strdup(const char *s)
35{
36 char *ptr;
37 int len;
38 len = strlen(s) + 1;
39 ptr = av_malloc(len);
40 if (!ptr)
41 return NULL;
42 memcpy(ptr, s, len);
43 return ptr;
44}
45
46/**
47 * realloc which does nothing if the block is large enough
48 */
5c91a675 49void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
8e1e6f31
FB
50{
51 if(min_size < *size)
52 return ptr;
53
54 *size= min_size + 10*1024;
55
56 return av_realloc(ptr, *size);
57}
58
59
855ea723
ZK
60/* allocation of static arrays - do not use for normal allocation */
61static unsigned int last_static = 0;
62static char*** array_static = NULL;
63static const unsigned int grow_static = 64; // ^2
64void *__av_mallocz_static(void** location, unsigned int size)
65{
5c91a675 66 unsigned int l = (last_static + grow_static) & ~(grow_static - 1);
855ea723
ZK
67 void *ptr = av_mallocz(size);
68 if (!ptr)
69 return NULL;
70
71 if (location)
72 {
73 if (l > last_static)
8e1e6f31 74 array_static = av_realloc(array_static, l);
855ea723
ZK
75 array_static[last_static++] = (char**) location;
76 *location = ptr;
77 }
78 return ptr;
79}
80/* free all static arrays and reset pointers to 0 */
81void av_free_static()
82{
83 if (array_static)
84 {
85 unsigned i;
86 for (i = 0; i < last_static; i++)
87 {
8e1e6f31 88 av_free(*array_static[i]);
855ea723
ZK
89 *array_static[i] = NULL;
90 }
8e1e6f31 91 av_free(array_static);
855ea723
ZK
92 array_static = 0;
93 }
94 last_static = 0;
95}
96
4d7a0a05
FB
97/* cannot call it directly because of 'void **' casting is not automatic */
98void __av_freep(void **ptr)
99{
100 av_free(*ptr);
101 *ptr = NULL;
102}
103
de6d9b64
FB
104/* encoder management */
105AVCodec *first_avcodec;
106
107void register_avcodec(AVCodec *format)
108{
109 AVCodec **p;
110 p = &first_avcodec;
111 while (*p != NULL) p = &(*p)->next;
112 *p = format;
113 format->next = NULL;
114}
115
1e491e29
MN
116typedef struct DefaultPicOpaque{
117 int last_pic_num;
118 uint8_t *data[4];
119}DefaultPicOpaque;
120
492cd3a9 121int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
1e491e29
MN
122 int i;
123 const int width = s->width;
124 const int height= s->height;
125 DefaultPicOpaque *opaque;
4e00e76b
MN
126
127 assert(pic->data[0]==NULL);
128 assert(pic->type==0 || pic->type==FF_TYPE_INTERNAL);
1e491e29
MN
129
130 if(pic->opaque){
131 opaque= (DefaultPicOpaque *)pic->opaque;
132 for(i=0; i<3; i++)
133 pic->data[i]= opaque->data[i];
134
135// printf("get_buffer %X coded_pic_num:%d last:%d\n", pic->opaque, pic->coded_picture_number, opaque->last_pic_num);
136 pic->age= pic->coded_picture_number - opaque->last_pic_num;
137 opaque->last_pic_num= pic->coded_picture_number;
138//printf("age: %d %d %d\n", pic->age, c->picture_number, pic->coded_picture_number);
139 }else{
140 int align, h_chroma_shift, v_chroma_shift;
141 int w, h, pixel_size;
142
143 avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
144
145 switch(s->pix_fmt){
146 case PIX_FMT_YUV422:
147 pixel_size=2;
148 break;
149 case PIX_FMT_RGB24:
150 case PIX_FMT_BGR24:
151 pixel_size=3;
152 break;
1e491e29
MN
153 case PIX_FMT_RGBA32:
154 pixel_size=4;
155 break;
156 default:
157 pixel_size=1;
158 }
159
160 if(s->codec_id==CODEC_ID_SVQ1) align=63;
161 else align=15;
162
163 w= (width +align)&~align;
164 h= (height+align)&~align;
165
166 if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
167 w+= EDGE_WIDTH*2;
168 h+= EDGE_WIDTH*2;
169 }
170
171 opaque= av_mallocz(sizeof(DefaultPicOpaque));
172 if(opaque==NULL) return -1;
173
174 pic->opaque= opaque;
175 opaque->last_pic_num= -256*256*256*64;
176
177 for(i=0; i<3; i++){
178 int h_shift= i==0 ? 0 : h_chroma_shift;
179 int v_shift= i==0 ? 0 : v_chroma_shift;
180
181 pic->linesize[i]= pixel_size*w>>h_shift;
182
183 pic->base[i]= av_mallocz((pic->linesize[i]*h>>v_shift)+16); //FIXME 16
184 if(pic->base[i]==NULL) return -1;
185
186 memset(pic->base[i], 128, pic->linesize[i]*h>>v_shift);
187
188 if(s->flags&CODEC_FLAG_EMU_EDGE)
4e00e76b 189 pic->data[i] = pic->base[i] + 16; //FIXME 16
1e491e29 190 else
4e00e76b 191 pic->data[i] = pic->base[i] + (pic->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift) + 16; //FIXME 16
1e491e29
MN
192
193 opaque->data[i]= pic->data[i];
194 }
195 pic->age= 256*256*256*64;
4e00e76b 196 pic->type= FF_BUFFER_TYPE_INTERNAL;
1e491e29
MN
197 }
198
199 return 0;
200}
201
492cd3a9 202void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
1e491e29
MN
203 int i;
204
4e00e76b
MN
205 assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
206
1e491e29
MN
207 for(i=0; i<3; i++)
208 pic->data[i]=NULL;
209//printf("R%X\n", pic->opaque);
210}
211
a33c7159
MN
212enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, enum PixelFormat * fmt){
213 return fmt[0];
214}
215
a949d72e 216void avcodec_get_context_defaults(AVCodecContext *s){
e8b62df6
MN
217 s->bit_rate= 800*1000;
218 s->bit_rate_tolerance= s->bit_rate*10;
a949d72e
MN
219 s->qmin= 2;
220 s->qmax= 31;
17a70fde
MN
221 s->mb_qmin= 2;
222 s->mb_qmax= 31;
a949d72e
MN
223 s->rc_eq= "tex^qComp";
224 s->qcompress= 0.5;
e8b62df6
MN
225 s->max_qdiff= 3;
226 s->b_quant_factor=1.25;
227 s->b_quant_offset=1.25;
b3a391e8 228 s->i_quant_factor=-0.8;
e8b62df6 229 s->i_quant_offset=0.0;
4d2858de 230 s->error_concealment= 3;
8d0e42ca 231 s->error_resilience= 1;
4d2858de 232 s->workaround_bugs= FF_BUG_AUTODETECT;
8d0e42ca
MN
233 s->frame_rate = 25 * FRAME_RATE_BASE;
234 s->gop_size= 50;
235 s->me_method= ME_EPZS;
1e491e29
MN
236 s->get_buffer= avcodec_default_get_buffer;
237 s->release_buffer= avcodec_default_release_buffer;
a33c7159 238 s->get_format= avcodec_default_get_format;
826f429a 239 s->me_subpel_quality=8;
a949d72e
MN
240}
241
242/**
243 * allocates a AVCodecContext and set it to defaults.
244 * this can be deallocated by simply calling free()
245 */
7ffbb60e 246AVCodecContext *avcodec_alloc_context(void){
a949d72e
MN
247 AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
248
249 if(avctx==NULL) return NULL;
250
251 avcodec_get_context_defaults(avctx);
252
253 return avctx;
254}
255
1e491e29 256/**
492cd3a9 257 * allocates a AVPFrame and set it to defaults.
1e491e29
MN
258 * this can be deallocated by simply calling free()
259 */
492cd3a9
MN
260AVFrame *avcodec_alloc_frame(void){
261 AVFrame *pic= av_mallocz(sizeof(AVFrame));
1e491e29
MN
262
263 return pic;
264}
265
de6d9b64
FB
266int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
267{
268 int ret;
269
270 avctx->codec = codec;
4cb3ca72 271 avctx->codec_id = codec->id;
de6d9b64 272 avctx->frame_number = 0;
0edf8a7a
PG
273 if (codec->priv_data_size > 0) {
274 avctx->priv_data = av_mallocz(codec->priv_data_size);
275 if (!avctx->priv_data)
276 return -ENOMEM;
277 } else {
278 avctx->priv_data = NULL;
279 }
de6d9b64
FB
280 ret = avctx->codec->init(avctx);
281 if (ret < 0) {
3123dd79 282 av_freep(&avctx->priv_data);
de6d9b64
FB
283 return ret;
284 }
285 return 0;
286}
287
0c1a9eda 288int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
de6d9b64
FB
289 const short *samples)
290{
291 int ret;
292
293 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
294 avctx->frame_number++;
295 return ret;
296}
297
0c1a9eda 298int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
492cd3a9 299 const AVFrame *pict)
de6d9b64
FB
300{
301 int ret;
302
303 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
6bb925f4
MN
304
305 emms_c(); //needed to avoid a emms_c() call before every return;
306
de6d9b64
FB
307 avctx->frame_number++;
308 return ret;
309}
310
311/* decode a frame. return -1 if error, otherwise return the number of
312 bytes used. If no frame could be decompressed, *got_picture_ptr is
313 zero. Otherwise, it is non zero */
492cd3a9 314int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
de6d9b64 315 int *got_picture_ptr,
0c1a9eda 316 uint8_t *buf, int buf_size)
de6d9b64
FB
317{
318 int ret;
1e491e29 319
de6d9b64
FB
320 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
321 buf, buf_size);
6bb925f4
MN
322
323 emms_c(); //needed to avoid a emms_c() call before every return;
1e491e29 324
1cb0edb4
J
325 if (*got_picture_ptr)
326 avctx->frame_number++;
de6d9b64
FB
327 return ret;
328}
329
330/* decode an audio frame. return -1 if error, otherwise return the
331 *number of bytes used. If no frame could be decompressed,
332 *frame_size_ptr is zero. Otherwise, it is the decompressed frame
333 *size in BYTES. */
0c1a9eda 334int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
de6d9b64 335 int *frame_size_ptr,
0c1a9eda 336 uint8_t *buf, int buf_size)
de6d9b64
FB
337{
338 int ret;
339
340 ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
341 buf, buf_size);
342 avctx->frame_number++;
343 return ret;
344}
345
346int avcodec_close(AVCodecContext *avctx)
347{
348 if (avctx->codec->close)
349 avctx->codec->close(avctx);
3123dd79 350 av_freep(&avctx->priv_data);
de6d9b64
FB
351 avctx->codec = NULL;
352 return 0;
353}
354
355AVCodec *avcodec_find_encoder(enum CodecID id)
356{
357 AVCodec *p;
358 p = first_avcodec;
359 while (p) {
360 if (p->encode != NULL && p->id == id)
361 return p;
362 p = p->next;
363 }
364 return NULL;
365}
366
98f3b098
A
367AVCodec *avcodec_find_encoder_by_name(const char *name)
368{
369 AVCodec *p;
370 p = first_avcodec;
371 while (p) {
372 if (p->encode != NULL && strcmp(name,p->name) == 0)
373 return p;
374 p = p->next;
375 }
376 return NULL;
377}
378
de6d9b64
FB
379AVCodec *avcodec_find_decoder(enum CodecID id)
380{
381 AVCodec *p;
382 p = first_avcodec;
383 while (p) {
384 if (p->decode != NULL && p->id == id)
385 return p;
386 p = p->next;
387 }
388 return NULL;
389}
390
391AVCodec *avcodec_find_decoder_by_name(const char *name)
392{
393 AVCodec *p;
394 p = first_avcodec;
395 while (p) {
396 if (p->decode != NULL && strcmp(name,p->name) == 0)
397 return p;
398 p = p->next;
399 }
400 return NULL;
401}
402
403AVCodec *avcodec_find(enum CodecID id)
404{
405 AVCodec *p;
406 p = first_avcodec;
407 while (p) {
408 if (p->id == id)
409 return p;
410 p = p->next;
411 }
412 return NULL;
413}
414
415void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
416{
417 const char *codec_name;
418 AVCodec *p;
419 char buf1[32];
623563c0 420 char channels_str[100];
a96b68b7 421 int bitrate;
de6d9b64
FB
422
423 if (encode)
424 p = avcodec_find_encoder(enc->codec_id);
425 else
426 p = avcodec_find_decoder(enc->codec_id);
427
428 if (p) {
429 codec_name = p->name;
430 } else if (enc->codec_name[0] != '\0') {
431 codec_name = enc->codec_name;
432 } else {
433 /* output avi tags */
434 if (enc->codec_type == CODEC_TYPE_VIDEO) {
435 snprintf(buf1, sizeof(buf1), "%c%c%c%c",
436 enc->codec_tag & 0xff,
437 (enc->codec_tag >> 8) & 0xff,
438 (enc->codec_tag >> 16) & 0xff,
439 (enc->codec_tag >> 24) & 0xff);
440 } else {
441 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
442 }
443 codec_name = buf1;
444 }
445
446 switch(enc->codec_type) {
447 case CODEC_TYPE_VIDEO:
448 snprintf(buf, buf_size,
449 "Video: %s%s",
450 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
cf087595
FB
451 if (enc->codec_id == CODEC_ID_RAWVIDEO) {
452 snprintf(buf + strlen(buf), buf_size - strlen(buf),
453 ", %s",
9bbffbb1 454 avcodec_get_pix_fmt_name(enc->pix_fmt));
cf087595 455 }
de6d9b64
FB
456 if (enc->width) {
457 snprintf(buf + strlen(buf), buf_size - strlen(buf),
458 ", %dx%d, %0.2f fps",
459 enc->width, enc->height,
460 (float)enc->frame_rate / FRAME_RATE_BASE);
461 }
4bfad535
FB
462 if (encode) {
463 snprintf(buf + strlen(buf), buf_size - strlen(buf),
464 ", q=%d-%d", enc->qmin, enc->qmax);
465 }
a96b68b7 466 bitrate = enc->bit_rate;
de6d9b64
FB
467 break;
468 case CODEC_TYPE_AUDIO:
469 snprintf(buf, buf_size,
470 "Audio: %s",
471 codec_name);
e0d2714a
J
472 switch (enc->channels) {
473 case 1:
623563c0 474 strcpy(channels_str, "mono");
e0d2714a
J
475 break;
476 case 2:
623563c0 477 strcpy(channels_str, "stereo");
e0d2714a
J
478 break;
479 case 6:
623563c0 480 strcpy(channels_str, "5:1");
e0d2714a
J
481 break;
482 default:
483 sprintf(channels_str, "%d channels", enc->channels);
484 break;
485 }
de6d9b64
FB
486 if (enc->sample_rate) {
487 snprintf(buf + strlen(buf), buf_size - strlen(buf),
488 ", %d Hz, %s",
489 enc->sample_rate,
e0d2714a 490 channels_str);
de6d9b64 491 }
e0d2714a 492
a96b68b7
FB
493 /* for PCM codecs, compute bitrate directly */
494 switch(enc->codec_id) {
495 case CODEC_ID_PCM_S16LE:
496 case CODEC_ID_PCM_S16BE:
497 case CODEC_ID_PCM_U16LE:
498 case CODEC_ID_PCM_U16BE:
a190b7e9 499 bitrate = enc->sample_rate * enc->channels * 16;
a96b68b7
FB
500 break;
501 case CODEC_ID_PCM_S8:
502 case CODEC_ID_PCM_U8:
503 case CODEC_ID_PCM_ALAW:
504 case CODEC_ID_PCM_MULAW:
a190b7e9 505 bitrate = enc->sample_rate * enc->channels * 8;
a96b68b7
FB
506 break;
507 default:
508 bitrate = enc->bit_rate;
509 break;
510 }
de6d9b64
FB
511 break;
512 default:
b71472eb 513 av_abort();
de6d9b64 514 }
4bfad535
FB
515 if (encode) {
516 if (enc->flags & CODEC_FLAG_PASS1)
517 snprintf(buf + strlen(buf), buf_size - strlen(buf),
518 ", pass 1");
519 if (enc->flags & CODEC_FLAG_PASS2)
520 snprintf(buf + strlen(buf), buf_size - strlen(buf),
521 ", pass 2");
522 }
a96b68b7 523 if (bitrate != 0) {
de6d9b64 524 snprintf(buf + strlen(buf), buf_size - strlen(buf),
a96b68b7 525 ", %d kb/s", bitrate / 1000);
de6d9b64
FB
526 }
527}
528
156e5023
NK
529unsigned avcodec_version( void )
530{
531 return LIBAVCODEC_VERSION_INT;
532}
cf087595 533
8bceb6af
NK
534unsigned avcodec_build( void )
535{
536 return LIBAVCODEC_BUILD;
537}
538
de6d9b64
FB
539/* must be called before any other functions */
540void avcodec_init(void)
541{
0344cd0a
AB
542 static int inited = 0;
543
544 if (inited != 0)
545 return;
546 inited = 1;
547
c6af86c2 548 //dsputil_init();
de6d9b64
FB
549}
550
4e00e76b 551/* this can be called after seeking and before trying to decode the next keyframe */
1c2a8c7f
MN
552void avcodec_flush_buffers(AVCodecContext *avctx)
553{
4e00e76b 554 int i;
1c2a8c7f 555 MpegEncContext *s = avctx->priv_data;
4e00e76b
MN
556
557 switch(avctx->codec_id){
558 case CODEC_ID_MPEG1VIDEO:
559 case CODEC_ID_H263:
560 case CODEC_ID_RV10:
561 case CODEC_ID_MJPEG:
562 case CODEC_ID_MJPEGB:
563 case CODEC_ID_MPEG4:
564 case CODEC_ID_MSMPEG4V1:
565 case CODEC_ID_MSMPEG4V2:
566 case CODEC_ID_MSMPEG4V3:
567 case CODEC_ID_WMV1:
568 case CODEC_ID_WMV2:
569 case CODEC_ID_H263P:
570 case CODEC_ID_H263I:
571 case CODEC_ID_SVQ1:
572 for(i=0; i<MAX_PICTURE_COUNT; i++){
573 if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
574 || s->picture[i].type == FF_BUFFER_TYPE_USER))
492cd3a9 575 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
87fb268f
ZK
576 }
577 s->last_picture.data[0] = s->next_picture.data[0] = NULL;
4e00e76b
MN
578 break;
579 default:
580 //FIXME
581 break;
582 }
1c2a8c7f
MN
583}
584
cd4af68a 585static int raw_encode_init(AVCodecContext *s)
de6d9b64
FB
586{
587 return 0;
588}
589
cd4af68a
ZK
590static int raw_decode_frame(AVCodecContext *avctx,
591 void *data, int *data_size,
0c1a9eda 592 uint8_t *buf, int buf_size)
de6d9b64
FB
593{
594 return -1;
595}
596
cd4af68a
ZK
597static int raw_encode_frame(AVCodecContext *avctx,
598 unsigned char *frame, int buf_size, void *data)
de6d9b64
FB
599{
600 return -1;
601}
602
de6d9b64
FB
603AVCodec rawvideo_codec = {
604 "rawvideo",
605 CODEC_TYPE_VIDEO,
606 CODEC_ID_RAWVIDEO,
607 0,
cd4af68a
ZK
608 raw_encode_init,
609 raw_encode_frame,
de6d9b64 610 NULL,
cd4af68a 611 raw_decode_frame,
de6d9b64 612};