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