3 * Copyright (c) 2001 Gerard Lantau.
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.
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.
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.
23 #include <limits.h> /* __GLIBC__ and __GLIBC_MINOR__ are defined here */
24 #if __GLIBC__ >=2 && __GLIBC_MINOR__ >= 1 /* Fixme about glibc-2.0 */
25 #define HAVE_MEMALIGN 1
33 void *av_mallocz(int size)
36 #if defined ( ARCH_X86 ) && defined ( HAVE_MEMALIGN )
38 From glibc-2.1.x manuals:
39 -------------------------
40 The address of a block returned by `malloc' or `realloc' in the GNU
41 system is always a multiple of eight (or sixteen on 64-bit systems).
42 If you need a block whose address is a multiple of a higher power of
43 two than that, use `memalign' or `valloc'. These functions are
44 declared in `stdlib.h'.
46 With the GNU library, you can use `free' to free the blocks that
47 `memalign' and `valloc' return. That does not work in BSD,
48 however--BSD does not provide any way to free such blocks.
50 ptr = memalign(64,size);
52 Indeed, we should align it:
55 on 32 for 586, PPro - k6-III
56 on 64 for K7 (maybe for P3 too).
57 Because L1 and L2 caches are aligned on those values.
58 But I don't want to code such logic here!
69 /* encoder management */
70 AVCodec *first_avcodec;
72 void register_avcodec(AVCodec *format)
76 while (*p != NULL) p = &(*p)->next;
81 int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
86 avctx->frame_number = 0;
87 avctx->priv_data = av_mallocz(codec->priv_data_size);
88 if (!avctx->priv_data)
90 ret = avctx->codec->init(avctx);
92 free(avctx->priv_data);
93 avctx->priv_data = NULL;
99 int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
100 const short *samples)
104 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
105 avctx->frame_number++;
109 int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
110 const AVPicture *pict)
114 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
115 avctx->frame_number++;
119 /* decode a frame. return -1 if error, otherwise return the number of
120 bytes used. If no frame could be decompressed, *got_picture_ptr is
121 zero. Otherwise, it is non zero */
122 int avcodec_decode_video(AVCodecContext *avctx, AVPicture *picture,
123 int *got_picture_ptr,
124 UINT8 *buf, int buf_size)
128 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
130 avctx->frame_number++;
134 /* decode an audio frame. return -1 if error, otherwise return the
135 *number of bytes used. If no frame could be decompressed,
136 *frame_size_ptr is zero. Otherwise, it is the decompressed frame
138 int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
140 UINT8 *buf, int buf_size)
144 ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
146 avctx->frame_number++;
150 int avcodec_close(AVCodecContext *avctx)
152 if (avctx->codec->close)
153 avctx->codec->close(avctx);
154 free(avctx->priv_data);
155 avctx->priv_data = NULL;
160 AVCodec *avcodec_find_encoder(enum CodecID id)
165 if (p->encode != NULL && p->id == id)
172 AVCodec *avcodec_find_decoder(enum CodecID id)
177 if (p->decode != NULL && p->id == id)
184 AVCodec *avcodec_find_decoder_by_name(const char *name)
189 if (p->decode != NULL && strcmp(name,p->name) == 0)
196 AVCodec *avcodec_find(enum CodecID id)
208 const char *pix_fmt_str[] = {
217 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
219 const char *codec_name;
224 p = avcodec_find_encoder(enc->codec_id);
226 p = avcodec_find_decoder(enc->codec_id);
229 codec_name = p->name;
230 } else if (enc->codec_name[0] != '\0') {
231 codec_name = enc->codec_name;
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);
241 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
246 switch(enc->codec_type) {
247 case CODEC_TYPE_VIDEO:
248 snprintf(buf, buf_size,
250 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
251 if (enc->codec_id == CODEC_ID_RAWVIDEO) {
252 snprintf(buf + strlen(buf), buf_size - strlen(buf),
254 pix_fmt_str[enc->pix_fmt]);
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);
263 case CODEC_TYPE_AUDIO:
264 snprintf(buf, buf_size,
267 if (enc->sample_rate) {
268 snprintf(buf + strlen(buf), buf_size - strlen(buf),
271 enc->channels == 2 ? "stereo" : "mono");
277 if (enc->bit_rate != 0) {
278 snprintf(buf + strlen(buf), buf_size - strlen(buf),
279 ", %d kb/s", enc->bit_rate / 1000);
283 /* Picture field are filled with 'ptr' addresses */
284 void avpicture_fill(AVPicture *picture, UINT8 *ptr,
285 int pix_fmt, int width, int height)
289 size = width * height;
291 case PIX_FMT_YUV420P:
292 picture->data[0] = ptr;
293 picture->data[1] = picture->data[0] + size;
294 picture->data[2] = picture->data[1] + size / 4;
295 picture->linesize[0] = width;
296 picture->linesize[1] = width / 2;
297 picture->linesize[2] = width / 2;
299 case PIX_FMT_YUV422P:
300 picture->data[0] = ptr;
301 picture->data[1] = picture->data[0] + size;
302 picture->data[2] = picture->data[1] + size / 2;
303 picture->linesize[0] = width;
304 picture->linesize[1] = width / 2;
305 picture->linesize[2] = width / 2;
307 case PIX_FMT_YUV444P:
308 picture->data[0] = ptr;
309 picture->data[1] = picture->data[0] + size;
310 picture->data[2] = picture->data[1] + size;
311 picture->linesize[0] = width;
312 picture->linesize[1] = width;
313 picture->linesize[2] = width;
317 picture->data[0] = ptr;
318 picture->data[1] = NULL;
319 picture->data[2] = NULL;
320 picture->linesize[0] = width * 3;
323 picture->data[0] = ptr;
324 picture->data[1] = NULL;
325 picture->data[2] = NULL;
326 picture->linesize[0] = width * 2;
329 picture->data[0] = NULL;
330 picture->data[1] = NULL;
331 picture->data[2] = NULL;
336 int avpicture_get_size(int pix_fmt, int width, int height)
340 size = width * height;
342 case PIX_FMT_YUV420P:
343 size = (size * 3) / 2;
345 case PIX_FMT_YUV422P:
348 case PIX_FMT_YUV444P:
366 /* must be called before any other functions */
367 void avcodec_init(void)
372 /* simple call to use all the codecs */
373 void avcodec_register_all(void)
376 #ifdef CONFIG_ENCODERS
377 register_avcodec(&ac3_encoder);
378 register_avcodec(&mp2_encoder);
379 register_avcodec(&mpeg1video_encoder);
380 register_avcodec(&h263_encoder);
381 register_avcodec(&h263p_encoder);
382 register_avcodec(&rv10_encoder);
383 register_avcodec(&mjpeg_encoder);
384 register_avcodec(&mpeg4_encoder);
385 register_avcodec(&msmpeg4_encoder);
386 #endif /* CONFIG_ENCODERS */
387 register_avcodec(&pcm_codec);
388 register_avcodec(&rawvideo_codec);
391 #ifdef CONFIG_DECODERS
392 register_avcodec(&h263_decoder);
393 register_avcodec(&mpeg4_decoder);
394 register_avcodec(&msmpeg4_decoder);
395 register_avcodec(&mpeg_decoder);
396 register_avcodec(&h263i_decoder);
397 register_avcodec(&rv10_decoder);
398 register_avcodec(&mjpeg_decoder);
400 register_avcodec(&mp3_decoder);
403 register_avcodec(&ac3_decoder);
405 #endif /* CONFIG_DECODERS */
408 static int encode_init(AVCodecContext *s)
413 static int decode_frame(AVCodecContext *avctx,
414 void *data, int *data_size,
415 UINT8 *buf, int buf_size)
420 static int encode_frame(AVCodecContext *avctx,
421 unsigned char *frame, int buf_size, void *data)
426 /* dummy pcm codec */
427 AVCodec pcm_codec = {
438 AVCodec rawvideo_codec = {