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