- PIX_FMT_ANY = -1 to avoid breaking any stuff that could be using it.
[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"
3d204385
NK
25#ifdef HAVE_MALLOC_H
26#include <malloc.h>
27#else
28#include <stdlib.h>
29#endif
de6d9b64
FB
30
31/* memory alloc */
32void *av_mallocz(int size)
33{
34 void *ptr;
36157009 35#if defined ( ARCH_X86 ) && defined ( HAVE_MEMALIGN )
36157009
NK
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
de6d9b64 47 ptr = malloc(size);
36157009 48#endif
de6d9b64
FB
49 if (!ptr)
50 return NULL;
51 memset(ptr, 0, size);
52 return ptr;
53}
54
55/* encoder management */
56AVCodec *first_avcodec;
57
58void 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
67int 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
85int 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
95int 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 */
108int 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);
1cb0edb4
J
116 if (*got_picture_ptr)
117 avctx->frame_number++;
de6d9b64
FB
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. */
125int 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
137int 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
147AVCodec *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
98f3b098
A
159AVCodec *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
de6d9b64
FB
171AVCodec *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
183AVCodec *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
195AVCodec *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
cf087595 207const char *pix_fmt_str[] = {
bc657ac3 208 "??",
cf087595
FB
209 "yuv420p",
210 "yuv422",
211 "rgb24",
212 "bgr24",
213 "yuv422p",
214 "yuv444p",
215};
216
de6d9b64
FB
217void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
218{
219 const char *codec_name;
220 AVCodec *p;
221 char buf1[32];
a96b68b7 222 int bitrate;
de6d9b64
FB
223
224 if (encode)
225 p = avcodec_find_encoder(enc->codec_id);
226 else
227 p = avcodec_find_decoder(enc->codec_id);
228
229 if (p) {
230 codec_name = p->name;
231 } else if (enc->codec_name[0] != '\0') {
232 codec_name = enc->codec_name;
233 } else {
234 /* output avi tags */
235 if (enc->codec_type == CODEC_TYPE_VIDEO) {
236 snprintf(buf1, sizeof(buf1), "%c%c%c%c",
237 enc->codec_tag & 0xff,
238 (enc->codec_tag >> 8) & 0xff,
239 (enc->codec_tag >> 16) & 0xff,
240 (enc->codec_tag >> 24) & 0xff);
241 } else {
242 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
243 }
244 codec_name = buf1;
245 }
246
247 switch(enc->codec_type) {
248 case CODEC_TYPE_VIDEO:
249 snprintf(buf, buf_size,
250 "Video: %s%s",
251 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
cf087595
FB
252 if (enc->codec_id == CODEC_ID_RAWVIDEO) {
253 snprintf(buf + strlen(buf), buf_size - strlen(buf),
254 ", %s",
255 pix_fmt_str[enc->pix_fmt]);
256 }
de6d9b64
FB
257 if (enc->width) {
258 snprintf(buf + strlen(buf), buf_size - strlen(buf),
259 ", %dx%d, %0.2f fps",
260 enc->width, enc->height,
261 (float)enc->frame_rate / FRAME_RATE_BASE);
262 }
bc657ac3
ZK
263 snprintf(buf + strlen(buf), buf_size - strlen(buf),
264 ", q=%d-%d", enc->qmin, enc->qmax);
265
a96b68b7 266 bitrate = enc->bit_rate;
de6d9b64
FB
267 break;
268 case CODEC_TYPE_AUDIO:
269 snprintf(buf, buf_size,
270 "Audio: %s",
271 codec_name);
272 if (enc->sample_rate) {
273 snprintf(buf + strlen(buf), buf_size - strlen(buf),
274 ", %d Hz, %s",
275 enc->sample_rate,
276 enc->channels == 2 ? "stereo" : "mono");
277 }
a96b68b7
FB
278 /* for PCM codecs, compute bitrate directly */
279 switch(enc->codec_id) {
280 case CODEC_ID_PCM_S16LE:
281 case CODEC_ID_PCM_S16BE:
282 case CODEC_ID_PCM_U16LE:
283 case CODEC_ID_PCM_U16BE:
a190b7e9 284 bitrate = enc->sample_rate * enc->channels * 16;
a96b68b7
FB
285 break;
286 case CODEC_ID_PCM_S8:
287 case CODEC_ID_PCM_U8:
288 case CODEC_ID_PCM_ALAW:
289 case CODEC_ID_PCM_MULAW:
a190b7e9 290 bitrate = enc->sample_rate * enc->channels * 8;
a96b68b7
FB
291 break;
292 default:
293 bitrate = enc->bit_rate;
294 break;
295 }
de6d9b64
FB
296 break;
297 default:
298 abort();
299 }
a96b68b7 300 if (bitrate != 0) {
de6d9b64 301 snprintf(buf + strlen(buf), buf_size - strlen(buf),
a96b68b7 302 ", %d kb/s", bitrate / 1000);
de6d9b64
FB
303 }
304}
305
cf087595
FB
306/* Picture field are filled with 'ptr' addresses */
307void avpicture_fill(AVPicture *picture, UINT8 *ptr,
308 int pix_fmt, int width, int height)
309{
310 int size;
311
312 size = width * height;
313 switch(pix_fmt) {
314 case PIX_FMT_YUV420P:
315 picture->data[0] = ptr;
316 picture->data[1] = picture->data[0] + size;
317 picture->data[2] = picture->data[1] + size / 4;
318 picture->linesize[0] = width;
319 picture->linesize[1] = width / 2;
320 picture->linesize[2] = width / 2;
321 break;
322 case PIX_FMT_YUV422P:
323 picture->data[0] = ptr;
324 picture->data[1] = picture->data[0] + size;
325 picture->data[2] = picture->data[1] + size / 2;
326 picture->linesize[0] = width;
327 picture->linesize[1] = width / 2;
328 picture->linesize[2] = width / 2;
329 break;
330 case PIX_FMT_YUV444P:
331 picture->data[0] = ptr;
332 picture->data[1] = picture->data[0] + size;
333 picture->data[2] = picture->data[1] + size;
334 picture->linesize[0] = width;
335 picture->linesize[1] = width;
336 picture->linesize[2] = width;
337 break;
338 case PIX_FMT_RGB24:
339 case PIX_FMT_BGR24:
340 picture->data[0] = ptr;
341 picture->data[1] = NULL;
342 picture->data[2] = NULL;
343 picture->linesize[0] = width * 3;
344 break;
345 case PIX_FMT_YUV422:
346 picture->data[0] = ptr;
347 picture->data[1] = NULL;
348 picture->data[2] = NULL;
349 picture->linesize[0] = width * 2;
350 break;
351 default:
352 picture->data[0] = NULL;
353 picture->data[1] = NULL;
354 picture->data[2] = NULL;
355 break;
356 }
357}
358
359int avpicture_get_size(int pix_fmt, int width, int height)
360{
361 int size;
362
363 size = width * height;
364 switch(pix_fmt) {
365 case PIX_FMT_YUV420P:
366 size = (size * 3) / 2;
367 break;
368 case PIX_FMT_YUV422P:
369 size = (size * 2);
370 break;
371 case PIX_FMT_YUV444P:
372 size = (size * 3);
373 break;
374 case PIX_FMT_RGB24:
375 case PIX_FMT_BGR24:
376 size = (size * 3);
377 break;
378 case PIX_FMT_YUV422:
379 size = (size * 2);
380 break;
381 default:
382 size = -1;
383 break;
384 }
385 return size;
386}
387
388
de6d9b64
FB
389/* must be called before any other functions */
390void avcodec_init(void)
391{
0344cd0a
AB
392 static int inited = 0;
393
394 if (inited != 0)
395 return;
396 inited = 1;
397
de6d9b64
FB
398 dsputil_init();
399}
400
401/* simple call to use all the codecs */
402void avcodec_register_all(void)
403{
0344cd0a
AB
404 static int inited = 0;
405
406 if (inited != 0)
407 return;
408 inited = 1;
409
d771bcae
FB
410 /* encoders */
411#ifdef CONFIG_ENCODERS
de6d9b64
FB
412 register_avcodec(&ac3_encoder);
413 register_avcodec(&mp2_encoder);
9d36bdc9
J
414#ifdef CONFIG_MP3LAME
415 register_avcodec(&mp3lame_encoder);
416#endif
de6d9b64
FB
417 register_avcodec(&mpeg1video_encoder);
418 register_avcodec(&h263_encoder);
419 register_avcodec(&h263p_encoder);
420 register_avcodec(&rv10_encoder);
421 register_avcodec(&mjpeg_encoder);
58f26ba9 422 register_avcodec(&mpeg4_encoder);
84afee34
MN
423 register_avcodec(&msmpeg4v1_encoder);
424 register_avcodec(&msmpeg4v2_encoder);
425 register_avcodec(&msmpeg4v3_encoder);
d771bcae 426#endif /* CONFIG_ENCODERS */
de6d9b64 427 register_avcodec(&rawvideo_codec);
d771bcae 428
de6d9b64 429 /* decoders */
d771bcae 430#ifdef CONFIG_DECODERS
de6d9b64 431 register_avcodec(&h263_decoder);
58f26ba9 432 register_avcodec(&mpeg4_decoder);
84afee34
MN
433 register_avcodec(&msmpeg4v1_decoder);
434 register_avcodec(&msmpeg4v2_decoder);
435 register_avcodec(&msmpeg4v3_decoder);
e1a9dbff 436 register_avcodec(&wmv1_decoder);
de6d9b64
FB
437 register_avcodec(&mpeg_decoder);
438 register_avcodec(&h263i_decoder);
439 register_avcodec(&rv10_decoder);
4e66ab3b 440 register_avcodec(&mjpeg_decoder);
d771bcae 441 register_avcodec(&mp3_decoder);
d771bcae
FB
442#ifdef CONFIG_AC3
443 register_avcodec(&ac3_decoder);
444#endif
445#endif /* CONFIG_DECODERS */
a96b68b7
FB
446
447 /* pcm codecs */
448
449#define PCM_CODEC(id, name) \
450 register_avcodec(& name ## _encoder); \
451 register_avcodec(& name ## _decoder); \
452
453PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le);
454PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be);
455PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le);
456PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be);
457PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8);
458PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8);
459PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw);
460PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw);
461
462#undef PCM_CODEC
de6d9b64
FB
463}
464
465static int encode_init(AVCodecContext *s)
466{
467 return 0;
468}
469
470static int decode_frame(AVCodecContext *avctx,
471 void *data, int *data_size,
472 UINT8 *buf, int buf_size)
473{
474 return -1;
475}
476
477static int encode_frame(AVCodecContext *avctx,
478 unsigned char *frame, int buf_size, void *data)
479{
480 return -1;
481}
482
de6d9b64
FB
483AVCodec rawvideo_codec = {
484 "rawvideo",
485 CODEC_TYPE_VIDEO,
486 CODEC_ID_RAWVIDEO,
487 0,
488 encode_init,
489 encode_frame,
490 NULL,
491 decode_frame,
492};