wmv format is asf format
[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);
116 avctx->frame_number++;
117 return ret;
118}
119
120/* decode an audio frame. return -1 if error, otherwise return the
121 *number of bytes used. If no frame could be decompressed,
122 *frame_size_ptr is zero. Otherwise, it is the decompressed frame
123 *size in BYTES. */
124int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
125 int *frame_size_ptr,
126 UINT8 *buf, int buf_size)
127{
128 int ret;
129
130 ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
131 buf, buf_size);
132 avctx->frame_number++;
133 return ret;
134}
135
136int avcodec_close(AVCodecContext *avctx)
137{
138 if (avctx->codec->close)
139 avctx->codec->close(avctx);
140 free(avctx->priv_data);
141 avctx->priv_data = NULL;
142 avctx->codec = NULL;
143 return 0;
144}
145
146AVCodec *avcodec_find_encoder(enum CodecID id)
147{
148 AVCodec *p;
149 p = first_avcodec;
150 while (p) {
151 if (p->encode != NULL && p->id == id)
152 return p;
153 p = p->next;
154 }
155 return NULL;
156}
157
158AVCodec *avcodec_find_decoder(enum CodecID id)
159{
160 AVCodec *p;
161 p = first_avcodec;
162 while (p) {
163 if (p->decode != NULL && p->id == id)
164 return p;
165 p = p->next;
166 }
167 return NULL;
168}
169
170AVCodec *avcodec_find_decoder_by_name(const char *name)
171{
172 AVCodec *p;
173 p = first_avcodec;
174 while (p) {
175 if (p->decode != NULL && strcmp(name,p->name) == 0)
176 return p;
177 p = p->next;
178 }
179 return NULL;
180}
181
182AVCodec *avcodec_find(enum CodecID id)
183{
184 AVCodec *p;
185 p = first_avcodec;
186 while (p) {
187 if (p->id == id)
188 return p;
189 p = p->next;
190 }
191 return NULL;
192}
193
cf087595
FB
194const char *pix_fmt_str[] = {
195 "yuv420p",
196 "yuv422",
197 "rgb24",
198 "bgr24",
199 "yuv422p",
200 "yuv444p",
201};
202
de6d9b64
FB
203void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
204{
205 const char *codec_name;
206 AVCodec *p;
207 char buf1[32];
208
209 if (encode)
210 p = avcodec_find_encoder(enc->codec_id);
211 else
212 p = avcodec_find_decoder(enc->codec_id);
213
214 if (p) {
215 codec_name = p->name;
216 } else if (enc->codec_name[0] != '\0') {
217 codec_name = enc->codec_name;
218 } else {
219 /* output avi tags */
220 if (enc->codec_type == CODEC_TYPE_VIDEO) {
221 snprintf(buf1, sizeof(buf1), "%c%c%c%c",
222 enc->codec_tag & 0xff,
223 (enc->codec_tag >> 8) & 0xff,
224 (enc->codec_tag >> 16) & 0xff,
225 (enc->codec_tag >> 24) & 0xff);
226 } else {
227 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
228 }
229 codec_name = buf1;
230 }
231
232 switch(enc->codec_type) {
233 case CODEC_TYPE_VIDEO:
234 snprintf(buf, buf_size,
235 "Video: %s%s",
236 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
cf087595
FB
237 if (enc->codec_id == CODEC_ID_RAWVIDEO) {
238 snprintf(buf + strlen(buf), buf_size - strlen(buf),
239 ", %s",
240 pix_fmt_str[enc->pix_fmt]);
241 }
de6d9b64
FB
242 if (enc->width) {
243 snprintf(buf + strlen(buf), buf_size - strlen(buf),
244 ", %dx%d, %0.2f fps",
245 enc->width, enc->height,
246 (float)enc->frame_rate / FRAME_RATE_BASE);
247 }
248 break;
249 case CODEC_TYPE_AUDIO:
250 snprintf(buf, buf_size,
251 "Audio: %s",
252 codec_name);
253 if (enc->sample_rate) {
254 snprintf(buf + strlen(buf), buf_size - strlen(buf),
255 ", %d Hz, %s",
256 enc->sample_rate,
257 enc->channels == 2 ? "stereo" : "mono");
258 }
259 break;
260 default:
261 abort();
262 }
263 if (enc->bit_rate != 0) {
264 snprintf(buf + strlen(buf), buf_size - strlen(buf),
265 ", %d kb/s", enc->bit_rate / 1000);
266 }
267}
268
cf087595
FB
269/* Picture field are filled with 'ptr' addresses */
270void avpicture_fill(AVPicture *picture, UINT8 *ptr,
271 int pix_fmt, int width, int height)
272{
273 int size;
274
275 size = width * height;
276 switch(pix_fmt) {
277 case PIX_FMT_YUV420P:
278 picture->data[0] = ptr;
279 picture->data[1] = picture->data[0] + size;
280 picture->data[2] = picture->data[1] + size / 4;
281 picture->linesize[0] = width;
282 picture->linesize[1] = width / 2;
283 picture->linesize[2] = width / 2;
284 break;
285 case PIX_FMT_YUV422P:
286 picture->data[0] = ptr;
287 picture->data[1] = picture->data[0] + size;
288 picture->data[2] = picture->data[1] + size / 2;
289 picture->linesize[0] = width;
290 picture->linesize[1] = width / 2;
291 picture->linesize[2] = width / 2;
292 break;
293 case PIX_FMT_YUV444P:
294 picture->data[0] = ptr;
295 picture->data[1] = picture->data[0] + size;
296 picture->data[2] = picture->data[1] + size;
297 picture->linesize[0] = width;
298 picture->linesize[1] = width;
299 picture->linesize[2] = width;
300 break;
301 case PIX_FMT_RGB24:
302 case PIX_FMT_BGR24:
303 picture->data[0] = ptr;
304 picture->data[1] = NULL;
305 picture->data[2] = NULL;
306 picture->linesize[0] = width * 3;
307 break;
308 case PIX_FMT_YUV422:
309 picture->data[0] = ptr;
310 picture->data[1] = NULL;
311 picture->data[2] = NULL;
312 picture->linesize[0] = width * 2;
313 break;
314 default:
315 picture->data[0] = NULL;
316 picture->data[1] = NULL;
317 picture->data[2] = NULL;
318 break;
319 }
320}
321
322int avpicture_get_size(int pix_fmt, int width, int height)
323{
324 int size;
325
326 size = width * height;
327 switch(pix_fmt) {
328 case PIX_FMT_YUV420P:
329 size = (size * 3) / 2;
330 break;
331 case PIX_FMT_YUV422P:
332 size = (size * 2);
333 break;
334 case PIX_FMT_YUV444P:
335 size = (size * 3);
336 break;
337 case PIX_FMT_RGB24:
338 case PIX_FMT_BGR24:
339 size = (size * 3);
340 break;
341 case PIX_FMT_YUV422:
342 size = (size * 2);
343 break;
344 default:
345 size = -1;
346 break;
347 }
348 return size;
349}
350
351
de6d9b64
FB
352/* must be called before any other functions */
353void avcodec_init(void)
354{
355 dsputil_init();
356}
357
358/* simple call to use all the codecs */
359void avcodec_register_all(void)
360{
d771bcae
FB
361 /* encoders */
362#ifdef CONFIG_ENCODERS
de6d9b64
FB
363 register_avcodec(&ac3_encoder);
364 register_avcodec(&mp2_encoder);
365 register_avcodec(&mpeg1video_encoder);
366 register_avcodec(&h263_encoder);
367 register_avcodec(&h263p_encoder);
368 register_avcodec(&rv10_encoder);
369 register_avcodec(&mjpeg_encoder);
58f26ba9 370 register_avcodec(&mpeg4_encoder);
de6d9b64 371 register_avcodec(&msmpeg4_encoder);
d771bcae 372#endif /* CONFIG_ENCODERS */
de6d9b64
FB
373 register_avcodec(&pcm_codec);
374 register_avcodec(&rawvideo_codec);
d771bcae 375
de6d9b64 376 /* decoders */
d771bcae 377#ifdef CONFIG_DECODERS
de6d9b64 378 register_avcodec(&h263_decoder);
58f26ba9 379 register_avcodec(&mpeg4_decoder);
de6d9b64
FB
380 register_avcodec(&msmpeg4_decoder);
381 register_avcodec(&mpeg_decoder);
382 register_avcodec(&h263i_decoder);
383 register_avcodec(&rv10_decoder);
4e66ab3b 384 register_avcodec(&mjpeg_decoder);
829fa996 385//#ifdef CONFIG_MPGLIB
d771bcae 386 register_avcodec(&mp3_decoder);
829fa996 387//#endif
d771bcae
FB
388#ifdef CONFIG_AC3
389 register_avcodec(&ac3_decoder);
390#endif
391#endif /* CONFIG_DECODERS */
de6d9b64
FB
392}
393
394static int encode_init(AVCodecContext *s)
395{
396 return 0;
397}
398
399static int decode_frame(AVCodecContext *avctx,
400 void *data, int *data_size,
401 UINT8 *buf, int buf_size)
402{
403 return -1;
404}
405
406static int encode_frame(AVCodecContext *avctx,
407 unsigned char *frame, int buf_size, void *data)
408{
409 return -1;
410}
411
412/* dummy pcm codec */
413AVCodec pcm_codec = {
414 "pcm",
415 CODEC_TYPE_AUDIO,
416 CODEC_ID_PCM,
417 0,
418 encode_init,
419 encode_frame,
420 NULL,
421 decode_frame,
422};
423
424AVCodec rawvideo_codec = {
425 "rawvideo",
426 CODEC_TYPE_VIDEO,
427 CODEC_ID_RAWVIDEO,
428 0,
429 encode_init,
430 encode_frame,
431 NULL,
432 decode_frame,
433};