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