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