added avcodec_set_bit_exact() for testing
[libav.git] / libavcodec / utils.c
CommitLineData
de6d9b64
FB
1/*
2 * utils for libavcodec
3 * Copyright (c) 2001 Gerard Lantau.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
de6d9b64 19#include "avcodec.h"
3123dd79 20#include "dsputil.h"
1c2a8c7f 21#include "mpegvideo.h"
3d204385
NK
22#ifdef HAVE_MALLOC_H
23#include <malloc.h>
3d204385 24#endif
de6d9b64
FB
25
26/* memory alloc */
3123dd79 27void *av_malloc(int size)
de6d9b64
FB
28{
29 void *ptr;
36157009 30#if defined ( ARCH_X86 ) && defined ( HAVE_MEMALIGN )
36157009
NK
31 ptr = memalign(64,size);
32 /* Why 64?
33 Indeed, we should align it:
34 on 4 for 386
35 on 16 for 486
36 on 32 for 586, PPro - k6-III
37 on 64 for K7 (maybe for P3 too).
38 Because L1 and L2 caches are aligned on those values.
39 But I don't want to code such logic here!
40 */
41#else
de6d9b64 42 ptr = malloc(size);
36157009 43#endif
de6d9b64
FB
44 if (!ptr)
45 return NULL;
46 memset(ptr, 0, size);
47 return ptr;
48}
49
3123dd79
FB
50void *av_mallocz(int size)
51{
52 void *ptr;
53 ptr = av_malloc(size);
54 if (!ptr)
55 return NULL;
56 memset(ptr, 0, size);
57 return ptr;
58}
59
60/* NOTE: ptr = NULL is explicetly allowed */
61void av_free(void *ptr)
62{
63 /* XXX: this test should not be needed on most libcs */
64 if (ptr)
65 free(ptr);
66}
67
de6d9b64
FB
68/* encoder management */
69AVCodec *first_avcodec;
70
71void register_avcodec(AVCodec *format)
72{
73 AVCodec **p;
74 p = &first_avcodec;
75 while (*p != NULL) p = &(*p)->next;
76 *p = format;
77 format->next = NULL;
78}
79
80int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
81{
82 int ret;
83
84 avctx->codec = codec;
85 avctx->frame_number = 0;
0edf8a7a
PG
86 if (codec->priv_data_size > 0) {
87 avctx->priv_data = av_mallocz(codec->priv_data_size);
88 if (!avctx->priv_data)
89 return -ENOMEM;
90 } else {
91 avctx->priv_data = NULL;
92 }
de6d9b64
FB
93 ret = avctx->codec->init(avctx);
94 if (ret < 0) {
3123dd79 95 av_freep(&avctx->priv_data);
de6d9b64
FB
96 return ret;
97 }
98 return 0;
99}
100
101int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
102 const short *samples)
103{
104 int ret;
105
106 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
107 avctx->frame_number++;
108 return ret;
109}
110
111int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
112 const AVPicture *pict)
113{
114 int ret;
115
116 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
117 avctx->frame_number++;
118 return ret;
119}
120
121/* decode a frame. return -1 if error, otherwise return the number of
122 bytes used. If no frame could be decompressed, *got_picture_ptr is
123 zero. Otherwise, it is non zero */
124int avcodec_decode_video(AVCodecContext *avctx, AVPicture *picture,
125 int *got_picture_ptr,
126 UINT8 *buf, int buf_size)
127{
128 int ret;
129
130 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
131 buf, buf_size);
1cb0edb4
J
132 if (*got_picture_ptr)
133 avctx->frame_number++;
de6d9b64
FB
134 return ret;
135}
136
137/* decode an audio frame. return -1 if error, otherwise return the
138 *number of bytes used. If no frame could be decompressed,
139 *frame_size_ptr is zero. Otherwise, it is the decompressed frame
140 *size in BYTES. */
141int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
142 int *frame_size_ptr,
143 UINT8 *buf, int buf_size)
144{
145 int ret;
146
147 ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
148 buf, buf_size);
149 avctx->frame_number++;
150 return ret;
151}
152
153int avcodec_close(AVCodecContext *avctx)
154{
155 if (avctx->codec->close)
156 avctx->codec->close(avctx);
3123dd79 157 av_freep(&avctx->priv_data);
de6d9b64
FB
158 avctx->codec = NULL;
159 return 0;
160}
161
162AVCodec *avcodec_find_encoder(enum CodecID id)
163{
164 AVCodec *p;
165 p = first_avcodec;
166 while (p) {
167 if (p->encode != NULL && p->id == id)
168 return p;
169 p = p->next;
170 }
171 return NULL;
172}
173
98f3b098
A
174AVCodec *avcodec_find_encoder_by_name(const char *name)
175{
176 AVCodec *p;
177 p = first_avcodec;
178 while (p) {
179 if (p->encode != NULL && strcmp(name,p->name) == 0)
180 return p;
181 p = p->next;
182 }
183 return NULL;
184}
185
de6d9b64
FB
186AVCodec *avcodec_find_decoder(enum CodecID id)
187{
188 AVCodec *p;
189 p = first_avcodec;
190 while (p) {
191 if (p->decode != NULL && p->id == id)
192 return p;
193 p = p->next;
194 }
195 return NULL;
196}
197
198AVCodec *avcodec_find_decoder_by_name(const char *name)
199{
200 AVCodec *p;
201 p = first_avcodec;
202 while (p) {
203 if (p->decode != NULL && strcmp(name,p->name) == 0)
204 return p;
205 p = p->next;
206 }
207 return NULL;
208}
209
210AVCodec *avcodec_find(enum CodecID id)
211{
212 AVCodec *p;
213 p = first_avcodec;
214 while (p) {
215 if (p->id == id)
216 return p;
217 p = p->next;
218 }
219 return NULL;
220}
221
cf087595 222const char *pix_fmt_str[] = {
bc657ac3 223 "??",
cf087595
FB
224 "yuv420p",
225 "yuv422",
226 "rgb24",
227 "bgr24",
228 "yuv422p",
229 "yuv444p",
230};
231
de6d9b64
FB
232void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
233{
234 const char *codec_name;
235 AVCodec *p;
236 char buf1[32];
623563c0 237 char channels_str[100];
a96b68b7 238 int bitrate;
de6d9b64
FB
239
240 if (encode)
241 p = avcodec_find_encoder(enc->codec_id);
242 else
243 p = avcodec_find_decoder(enc->codec_id);
244
245 if (p) {
246 codec_name = p->name;
247 } else if (enc->codec_name[0] != '\0') {
248 codec_name = enc->codec_name;
249 } else {
250 /* output avi tags */
251 if (enc->codec_type == CODEC_TYPE_VIDEO) {
252 snprintf(buf1, sizeof(buf1), "%c%c%c%c",
253 enc->codec_tag & 0xff,
254 (enc->codec_tag >> 8) & 0xff,
255 (enc->codec_tag >> 16) & 0xff,
256 (enc->codec_tag >> 24) & 0xff);
257 } else {
258 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
259 }
260 codec_name = buf1;
261 }
262
263 switch(enc->codec_type) {
264 case CODEC_TYPE_VIDEO:
265 snprintf(buf, buf_size,
266 "Video: %s%s",
267 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
cf087595
FB
268 if (enc->codec_id == CODEC_ID_RAWVIDEO) {
269 snprintf(buf + strlen(buf), buf_size - strlen(buf),
270 ", %s",
271 pix_fmt_str[enc->pix_fmt]);
272 }
de6d9b64
FB
273 if (enc->width) {
274 snprintf(buf + strlen(buf), buf_size - strlen(buf),
275 ", %dx%d, %0.2f fps",
276 enc->width, enc->height,
277 (float)enc->frame_rate / FRAME_RATE_BASE);
278 }
bc657ac3
ZK
279 snprintf(buf + strlen(buf), buf_size - strlen(buf),
280 ", q=%d-%d", enc->qmin, enc->qmax);
281
a96b68b7 282 bitrate = enc->bit_rate;
de6d9b64
FB
283 break;
284 case CODEC_TYPE_AUDIO:
285 snprintf(buf, buf_size,
286 "Audio: %s",
287 codec_name);
e0d2714a
J
288 switch (enc->channels) {
289 case 1:
623563c0 290 strcpy(channels_str, "mono");
e0d2714a
J
291 break;
292 case 2:
623563c0 293 strcpy(channels_str, "stereo");
e0d2714a
J
294 break;
295 case 6:
623563c0 296 strcpy(channels_str, "5:1");
e0d2714a
J
297 break;
298 default:
299 sprintf(channels_str, "%d channels", enc->channels);
300 break;
301 }
de6d9b64
FB
302 if (enc->sample_rate) {
303 snprintf(buf + strlen(buf), buf_size - strlen(buf),
304 ", %d Hz, %s",
305 enc->sample_rate,
e0d2714a 306 channels_str);
de6d9b64 307 }
e0d2714a 308
a96b68b7
FB
309 /* for PCM codecs, compute bitrate directly */
310 switch(enc->codec_id) {
311 case CODEC_ID_PCM_S16LE:
312 case CODEC_ID_PCM_S16BE:
313 case CODEC_ID_PCM_U16LE:
314 case CODEC_ID_PCM_U16BE:
a190b7e9 315 bitrate = enc->sample_rate * enc->channels * 16;
a96b68b7
FB
316 break;
317 case CODEC_ID_PCM_S8:
318 case CODEC_ID_PCM_U8:
319 case CODEC_ID_PCM_ALAW:
320 case CODEC_ID_PCM_MULAW:
a190b7e9 321 bitrate = enc->sample_rate * enc->channels * 8;
a96b68b7
FB
322 break;
323 default:
324 bitrate = enc->bit_rate;
325 break;
326 }
de6d9b64
FB
327 break;
328 default:
329 abort();
330 }
a96b68b7 331 if (bitrate != 0) {
de6d9b64 332 snprintf(buf + strlen(buf), buf_size - strlen(buf),
a96b68b7 333 ", %d kb/s", bitrate / 1000);
de6d9b64
FB
334 }
335}
336
cf087595
FB
337/* Picture field are filled with 'ptr' addresses */
338void avpicture_fill(AVPicture *picture, UINT8 *ptr,
339 int pix_fmt, int width, int height)
340{
341 int size;
342
343 size = width * height;
344 switch(pix_fmt) {
345 case PIX_FMT_YUV420P:
346 picture->data[0] = ptr;
347 picture->data[1] = picture->data[0] + size;
348 picture->data[2] = picture->data[1] + size / 4;
349 picture->linesize[0] = width;
350 picture->linesize[1] = width / 2;
351 picture->linesize[2] = width / 2;
352 break;
353 case PIX_FMT_YUV422P:
354 picture->data[0] = ptr;
355 picture->data[1] = picture->data[0] + size;
356 picture->data[2] = picture->data[1] + size / 2;
357 picture->linesize[0] = width;
358 picture->linesize[1] = width / 2;
359 picture->linesize[2] = width / 2;
360 break;
361 case PIX_FMT_YUV444P:
362 picture->data[0] = ptr;
363 picture->data[1] = picture->data[0] + size;
364 picture->data[2] = picture->data[1] + size;
365 picture->linesize[0] = width;
366 picture->linesize[1] = width;
367 picture->linesize[2] = width;
368 break;
369 case PIX_FMT_RGB24:
370 case PIX_FMT_BGR24:
371 picture->data[0] = ptr;
372 picture->data[1] = NULL;
373 picture->data[2] = NULL;
374 picture->linesize[0] = width * 3;
375 break;
376 case PIX_FMT_YUV422:
377 picture->data[0] = ptr;
378 picture->data[1] = NULL;
379 picture->data[2] = NULL;
380 picture->linesize[0] = width * 2;
381 break;
382 default:
383 picture->data[0] = NULL;
384 picture->data[1] = NULL;
385 picture->data[2] = NULL;
386 break;
387 }
388}
389
390int avpicture_get_size(int pix_fmt, int width, int height)
391{
392 int size;
393
394 size = width * height;
395 switch(pix_fmt) {
396 case PIX_FMT_YUV420P:
397 size = (size * 3) / 2;
398 break;
399 case PIX_FMT_YUV422P:
400 size = (size * 2);
401 break;
402 case PIX_FMT_YUV444P:
403 size = (size * 3);
404 break;
405 case PIX_FMT_RGB24:
406 case PIX_FMT_BGR24:
407 size = (size * 3);
408 break;
409 case PIX_FMT_YUV422:
410 size = (size * 2);
411 break;
412 default:
413 size = -1;
414 break;
415 }
416 return size;
417}
418
156e5023
NK
419unsigned avcodec_version( void )
420{
421 return LIBAVCODEC_VERSION_INT;
422}
cf087595 423
8bceb6af
NK
424unsigned avcodec_build( void )
425{
426 return LIBAVCODEC_BUILD;
427}
428
de6d9b64
FB
429/* must be called before any other functions */
430void avcodec_init(void)
431{
0344cd0a
AB
432 static int inited = 0;
433
434 if (inited != 0)
435 return;
436 inited = 1;
437
de6d9b64
FB
438 dsputil_init();
439}
440
441/* simple call to use all the codecs */
442void avcodec_register_all(void)
443{
0344cd0a
AB
444 static int inited = 0;
445
446 if (inited != 0)
447 return;
448 inited = 1;
449
d771bcae
FB
450 /* encoders */
451#ifdef CONFIG_ENCODERS
de6d9b64
FB
452 register_avcodec(&ac3_encoder);
453 register_avcodec(&mp2_encoder);
9d36bdc9
J
454#ifdef CONFIG_MP3LAME
455 register_avcodec(&mp3lame_encoder);
456#endif
de6d9b64
FB
457 register_avcodec(&mpeg1video_encoder);
458 register_avcodec(&h263_encoder);
459 register_avcodec(&h263p_encoder);
460 register_avcodec(&rv10_encoder);
461 register_avcodec(&mjpeg_encoder);
58f26ba9 462 register_avcodec(&mpeg4_encoder);
84afee34
MN
463 register_avcodec(&msmpeg4v1_encoder);
464 register_avcodec(&msmpeg4v2_encoder);
465 register_avcodec(&msmpeg4v3_encoder);
d771bcae 466#endif /* CONFIG_ENCODERS */
de6d9b64 467 register_avcodec(&rawvideo_codec);
d771bcae 468
de6d9b64 469 /* decoders */
d771bcae 470#ifdef CONFIG_DECODERS
de6d9b64 471 register_avcodec(&h263_decoder);
58f26ba9 472 register_avcodec(&mpeg4_decoder);
84afee34
MN
473 register_avcodec(&msmpeg4v1_decoder);
474 register_avcodec(&msmpeg4v2_decoder);
475 register_avcodec(&msmpeg4v3_decoder);
e1a9dbff 476 register_avcodec(&wmv1_decoder);
de6d9b64
FB
477 register_avcodec(&mpeg_decoder);
478 register_avcodec(&h263i_decoder);
479 register_avcodec(&rv10_decoder);
4e66ab3b 480 register_avcodec(&mjpeg_decoder);
4b1f4f23 481 register_avcodec(&mp2_decoder);
d771bcae 482 register_avcodec(&mp3_decoder);
d771bcae
FB
483#ifdef CONFIG_AC3
484 register_avcodec(&ac3_decoder);
485#endif
486#endif /* CONFIG_DECODERS */
a96b68b7
FB
487
488 /* pcm codecs */
489
490#define PCM_CODEC(id, name) \
491 register_avcodec(& name ## _encoder); \
492 register_avcodec(& name ## _decoder); \
493
494PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le);
495PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be);
496PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le);
497PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be);
498PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8);
499PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8);
500PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw);
501PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw);
502
503#undef PCM_CODEC
de6d9b64
FB
504}
505
1c2a8c7f
MN
506/* this should be called after seeking and before trying to decode the next frame */
507void avcodec_flush_buffers(AVCodecContext *avctx)
508{
509 MpegEncContext *s = avctx->priv_data;
510 s->num_available_buffers=0;
511}
512
513
de6d9b64
FB
514static int encode_init(AVCodecContext *s)
515{
516 return 0;
517}
518
519static int decode_frame(AVCodecContext *avctx,
520 void *data, int *data_size,
521 UINT8 *buf, int buf_size)
522{
523 return -1;
524}
525
526static int encode_frame(AVCodecContext *avctx,
527 unsigned char *frame, int buf_size, void *data)
528{
529 return -1;
530}
531
de6d9b64
FB
532AVCodec rawvideo_codec = {
533 "rawvideo",
534 CODEC_TYPE_VIDEO,
535 CODEC_ID_RAWVIDEO,
536 0,
537 encode_init,
538 encode_frame,
539 NULL,
540 decode_frame,
541};