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.
21 #include "mpegvideo.h"
27 void *av_malloc(int size)
30 #if defined ( ARCH_X86 ) && defined ( HAVE_MEMALIGN )
31 ptr = memalign(64,size);
33 Indeed, we should align it:
36 on 32 for 586, PPro - k6-III
37 on 64 for K7 (maybe for P3 too).
38 Because L1 and L2 caches are aligned on those values.
39 But I don't want to code such logic here!
50 void *av_mallocz(int size)
53 ptr = av_malloc(size);
60 /* NOTE: ptr = NULL is explicetly allowed */
61 void av_free(void *ptr)
63 /* XXX: this test should not be needed on most libcs */
68 /* cannot call it directly because of 'void **' casting is not automatic */
69 void __av_freep(void **ptr)
75 /* encoder management */
76 AVCodec *first_avcodec;
78 void register_avcodec(AVCodec *format)
82 while (*p != NULL) p = &(*p)->next;
87 int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
92 avctx->frame_number = 0;
93 if (codec->priv_data_size > 0) {
94 avctx->priv_data = av_mallocz(codec->priv_data_size);
95 if (!avctx->priv_data)
98 avctx->priv_data = NULL;
100 ret = avctx->codec->init(avctx);
102 av_freep(&avctx->priv_data);
108 int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
109 const short *samples)
113 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
114 avctx->frame_number++;
118 int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
119 const AVPicture *pict)
123 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
124 avctx->frame_number++;
128 /* decode a frame. return -1 if error, otherwise return the number of
129 bytes used. If no frame could be decompressed, *got_picture_ptr is
130 zero. Otherwise, it is non zero */
131 int avcodec_decode_video(AVCodecContext *avctx, AVPicture *picture,
132 int *got_picture_ptr,
133 UINT8 *buf, int buf_size)
137 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
139 if (*got_picture_ptr)
140 avctx->frame_number++;
144 /* decode an audio frame. return -1 if error, otherwise return the
145 *number of bytes used. If no frame could be decompressed,
146 *frame_size_ptr is zero. Otherwise, it is the decompressed frame
148 int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
150 UINT8 *buf, int buf_size)
154 ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
156 avctx->frame_number++;
160 int avcodec_close(AVCodecContext *avctx)
162 if (avctx->codec->close)
163 avctx->codec->close(avctx);
164 av_freep(&avctx->priv_data);
169 AVCodec *avcodec_find_encoder(enum CodecID id)
174 if (p->encode != NULL && p->id == id)
181 AVCodec *avcodec_find_encoder_by_name(const char *name)
186 if (p->encode != NULL && strcmp(name,p->name) == 0)
193 AVCodec *avcodec_find_decoder(enum CodecID id)
198 if (p->decode != NULL && p->id == id)
205 AVCodec *avcodec_find_decoder_by_name(const char *name)
210 if (p->decode != NULL && strcmp(name,p->name) == 0)
217 AVCodec *avcodec_find(enum CodecID id)
229 const char *pix_fmt_str[] = {
239 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
241 const char *codec_name;
244 char channels_str[100];
248 p = avcodec_find_encoder(enc->codec_id);
250 p = avcodec_find_decoder(enc->codec_id);
253 codec_name = p->name;
254 } else if (enc->codec_name[0] != '\0') {
255 codec_name = enc->codec_name;
257 /* output avi tags */
258 if (enc->codec_type == CODEC_TYPE_VIDEO) {
259 snprintf(buf1, sizeof(buf1), "%c%c%c%c",
260 enc->codec_tag & 0xff,
261 (enc->codec_tag >> 8) & 0xff,
262 (enc->codec_tag >> 16) & 0xff,
263 (enc->codec_tag >> 24) & 0xff);
265 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
270 switch(enc->codec_type) {
271 case CODEC_TYPE_VIDEO:
272 snprintf(buf, buf_size,
274 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
275 if (enc->codec_id == CODEC_ID_RAWVIDEO) {
276 snprintf(buf + strlen(buf), buf_size - strlen(buf),
278 pix_fmt_str[enc->pix_fmt]);
281 snprintf(buf + strlen(buf), buf_size - strlen(buf),
282 ", %dx%d, %0.2f fps",
283 enc->width, enc->height,
284 (float)enc->frame_rate / FRAME_RATE_BASE);
286 snprintf(buf + strlen(buf), buf_size - strlen(buf),
287 ", q=%d-%d", enc->qmin, enc->qmax);
289 bitrate = enc->bit_rate;
291 case CODEC_TYPE_AUDIO:
292 snprintf(buf, buf_size,
295 switch (enc->channels) {
297 strcpy(channels_str, "mono");
300 strcpy(channels_str, "stereo");
303 strcpy(channels_str, "5:1");
306 sprintf(channels_str, "%d channels", enc->channels);
309 if (enc->sample_rate) {
310 snprintf(buf + strlen(buf), buf_size - strlen(buf),
316 /* for PCM codecs, compute bitrate directly */
317 switch(enc->codec_id) {
318 case CODEC_ID_PCM_S16LE:
319 case CODEC_ID_PCM_S16BE:
320 case CODEC_ID_PCM_U16LE:
321 case CODEC_ID_PCM_U16BE:
322 bitrate = enc->sample_rate * enc->channels * 16;
324 case CODEC_ID_PCM_S8:
325 case CODEC_ID_PCM_U8:
326 case CODEC_ID_PCM_ALAW:
327 case CODEC_ID_PCM_MULAW:
328 bitrate = enc->sample_rate * enc->channels * 8;
331 bitrate = enc->bit_rate;
339 snprintf(buf + strlen(buf), buf_size - strlen(buf),
340 ", %d kb/s", bitrate / 1000);
344 /* Picture field are filled with 'ptr' addresses */
345 void avpicture_fill(AVPicture *picture, UINT8 *ptr,
346 int pix_fmt, int width, int height)
350 size = width * height;
352 case PIX_FMT_YUV420P:
353 picture->data[0] = ptr;
354 picture->data[1] = picture->data[0] + size;
355 picture->data[2] = picture->data[1] + size / 4;
356 picture->linesize[0] = width;
357 picture->linesize[1] = width / 2;
358 picture->linesize[2] = width / 2;
360 case PIX_FMT_YUV422P:
361 picture->data[0] = ptr;
362 picture->data[1] = picture->data[0] + size;
363 picture->data[2] = picture->data[1] + size / 2;
364 picture->linesize[0] = width;
365 picture->linesize[1] = width / 2;
366 picture->linesize[2] = width / 2;
368 case PIX_FMT_YUV444P:
369 picture->data[0] = ptr;
370 picture->data[1] = picture->data[0] + size;
371 picture->data[2] = picture->data[1] + size;
372 picture->linesize[0] = width;
373 picture->linesize[1] = width;
374 picture->linesize[2] = width;
378 picture->data[0] = ptr;
379 picture->data[1] = NULL;
380 picture->data[2] = NULL;
381 picture->linesize[0] = width * 3;
384 picture->data[0] = ptr;
385 picture->data[1] = NULL;
386 picture->data[2] = NULL;
387 picture->linesize[0] = width * 2;
390 picture->data[0] = NULL;
391 picture->data[1] = NULL;
392 picture->data[2] = NULL;
397 int avpicture_get_size(int pix_fmt, int width, int height)
401 size = width * height;
403 case PIX_FMT_YUV420P:
404 size = (size * 3) / 2;
406 case PIX_FMT_YUV422P:
409 case PIX_FMT_YUV444P:
426 unsigned avcodec_version( void )
428 return LIBAVCODEC_VERSION_INT;
431 unsigned avcodec_build( void )
433 return LIBAVCODEC_BUILD;
436 /* must be called before any other functions */
437 void avcodec_init(void)
439 static int inited = 0;
448 /* simple call to use all the codecs */
449 void avcodec_register_all(void)
451 static int inited = 0;
458 #ifdef CONFIG_ENCODERS
459 register_avcodec(&ac3_encoder);
460 register_avcodec(&mp2_encoder);
461 #ifdef CONFIG_MP3LAME
462 register_avcodec(&mp3lame_encoder);
464 register_avcodec(&mpeg1video_encoder);
465 register_avcodec(&h263_encoder);
466 register_avcodec(&h263p_encoder);
467 register_avcodec(&rv10_encoder);
468 register_avcodec(&mjpeg_encoder);
469 register_avcodec(&mpeg4_encoder);
470 register_avcodec(&msmpeg4v1_encoder);
471 register_avcodec(&msmpeg4v2_encoder);
472 register_avcodec(&msmpeg4v3_encoder);
473 #endif /* CONFIG_ENCODERS */
474 register_avcodec(&rawvideo_codec);
477 #ifdef CONFIG_DECODERS
478 register_avcodec(&h263_decoder);
479 register_avcodec(&mpeg4_decoder);
480 register_avcodec(&msmpeg4v1_decoder);
481 register_avcodec(&msmpeg4v2_decoder);
482 register_avcodec(&msmpeg4v3_decoder);
483 register_avcodec(&wmv1_decoder);
484 register_avcodec(&mpeg_decoder);
485 register_avcodec(&h263i_decoder);
486 register_avcodec(&rv10_decoder);
487 register_avcodec(&mjpeg_decoder);
488 register_avcodec(&mp2_decoder);
489 register_avcodec(&mp3_decoder);
491 register_avcodec(&ac3_decoder);
493 #endif /* CONFIG_DECODERS */
497 #define PCM_CODEC(id, name) \
498 register_avcodec(& name ## _encoder); \
499 register_avcodec(& name ## _decoder); \
501 PCM_CODEC(CODEC_ID_PCM_S16LE, pcm_s16le);
502 PCM_CODEC(CODEC_ID_PCM_S16BE, pcm_s16be);
503 PCM_CODEC(CODEC_ID_PCM_U16LE, pcm_u16le);
504 PCM_CODEC(CODEC_ID_PCM_U16BE, pcm_u16be);
505 PCM_CODEC(CODEC_ID_PCM_S8, pcm_s8);
506 PCM_CODEC(CODEC_ID_PCM_U8, pcm_u8);
507 PCM_CODEC(CODEC_ID_PCM_ALAW, pcm_alaw);
508 PCM_CODEC(CODEC_ID_PCM_MULAW, pcm_mulaw);
513 /* this should be called after seeking and before trying to decode the next frame */
514 void avcodec_flush_buffers(AVCodecContext *avctx)
516 MpegEncContext *s = avctx->priv_data;
517 s->num_available_buffers=0;
521 static int encode_init(AVCodecContext *s)
526 static int decode_frame(AVCodecContext *avctx,
527 void *data, int *data_size,
528 UINT8 *buf, int buf_size)
533 static int encode_frame(AVCodecContext *avctx,
534 unsigned char *frame, int buf_size, void *data)
539 AVCodec rawvideo_codec = {