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.
28 void *av_mallocz(int size)
38 /* encoder management */
39 AVCodec *first_avcodec;
41 void register_avcodec(AVCodec *format)
45 while (*p != NULL) p = &(*p)->next;
50 int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
55 avctx->frame_number = 0;
56 avctx->priv_data = av_mallocz(codec->priv_data_size);
57 if (!avctx->priv_data)
59 ret = avctx->codec->init(avctx);
61 free(avctx->priv_data);
62 avctx->priv_data = NULL;
68 int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
73 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
74 avctx->frame_number++;
78 int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
79 const AVPicture *pict)
83 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
84 avctx->frame_number++;
88 /* decode a frame. return -1 if error, otherwise return the number of
89 bytes used. If no frame could be decompressed, *got_picture_ptr is
90 zero. Otherwise, it is non zero */
91 int avcodec_decode_video(AVCodecContext *avctx, AVPicture *picture,
93 UINT8 *buf, int buf_size)
97 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
99 avctx->frame_number++;
103 /* decode an audio frame. return -1 if error, otherwise return the
104 *number of bytes used. If no frame could be decompressed,
105 *frame_size_ptr is zero. Otherwise, it is the decompressed frame
107 int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
109 UINT8 *buf, int buf_size)
113 ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
115 avctx->frame_number++;
119 int avcodec_close(AVCodecContext *avctx)
121 if (avctx->codec->close)
122 avctx->codec->close(avctx);
123 free(avctx->priv_data);
124 avctx->priv_data = NULL;
129 AVCodec *avcodec_find_encoder(enum CodecID id)
134 if (p->encode != NULL && p->id == id)
141 AVCodec *avcodec_find_decoder(enum CodecID id)
146 if (p->decode != NULL && p->id == id)
153 AVCodec *avcodec_find_decoder_by_name(const char *name)
158 if (p->decode != NULL && strcmp(name,p->name) == 0)
165 AVCodec *avcodec_find(enum CodecID id)
177 const char *pix_fmt_str[] = {
186 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
188 const char *codec_name;
193 p = avcodec_find_encoder(enc->codec_id);
195 p = avcodec_find_decoder(enc->codec_id);
198 codec_name = p->name;
199 } else if (enc->codec_name[0] != '\0') {
200 codec_name = enc->codec_name;
202 /* output avi tags */
203 if (enc->codec_type == CODEC_TYPE_VIDEO) {
204 snprintf(buf1, sizeof(buf1), "%c%c%c%c",
205 enc->codec_tag & 0xff,
206 (enc->codec_tag >> 8) & 0xff,
207 (enc->codec_tag >> 16) & 0xff,
208 (enc->codec_tag >> 24) & 0xff);
210 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
215 switch(enc->codec_type) {
216 case CODEC_TYPE_VIDEO:
217 snprintf(buf, buf_size,
219 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
220 if (enc->codec_id == CODEC_ID_RAWVIDEO) {
221 snprintf(buf + strlen(buf), buf_size - strlen(buf),
223 pix_fmt_str[enc->pix_fmt]);
226 snprintf(buf + strlen(buf), buf_size - strlen(buf),
227 ", %dx%d, %0.2f fps",
228 enc->width, enc->height,
229 (float)enc->frame_rate / FRAME_RATE_BASE);
232 case CODEC_TYPE_AUDIO:
233 snprintf(buf, buf_size,
236 if (enc->sample_rate) {
237 snprintf(buf + strlen(buf), buf_size - strlen(buf),
240 enc->channels == 2 ? "stereo" : "mono");
246 if (enc->bit_rate != 0) {
247 snprintf(buf + strlen(buf), buf_size - strlen(buf),
248 ", %d kb/s", enc->bit_rate / 1000);
252 /* Picture field are filled with 'ptr' addresses */
253 void avpicture_fill(AVPicture *picture, UINT8 *ptr,
254 int pix_fmt, int width, int height)
258 size = width * height;
260 case PIX_FMT_YUV420P:
261 picture->data[0] = ptr;
262 picture->data[1] = picture->data[0] + size;
263 picture->data[2] = picture->data[1] + size / 4;
264 picture->linesize[0] = width;
265 picture->linesize[1] = width / 2;
266 picture->linesize[2] = width / 2;
268 case PIX_FMT_YUV422P:
269 picture->data[0] = ptr;
270 picture->data[1] = picture->data[0] + size;
271 picture->data[2] = picture->data[1] + size / 2;
272 picture->linesize[0] = width;
273 picture->linesize[1] = width / 2;
274 picture->linesize[2] = width / 2;
276 case PIX_FMT_YUV444P:
277 picture->data[0] = ptr;
278 picture->data[1] = picture->data[0] + size;
279 picture->data[2] = picture->data[1] + size;
280 picture->linesize[0] = width;
281 picture->linesize[1] = width;
282 picture->linesize[2] = width;
286 picture->data[0] = ptr;
287 picture->data[1] = NULL;
288 picture->data[2] = NULL;
289 picture->linesize[0] = width * 3;
292 picture->data[0] = ptr;
293 picture->data[1] = NULL;
294 picture->data[2] = NULL;
295 picture->linesize[0] = width * 2;
298 picture->data[0] = NULL;
299 picture->data[1] = NULL;
300 picture->data[2] = NULL;
305 int avpicture_get_size(int pix_fmt, int width, int height)
309 size = width * height;
311 case PIX_FMT_YUV420P:
312 size = (size * 3) / 2;
314 case PIX_FMT_YUV422P:
317 case PIX_FMT_YUV444P:
335 /* must be called before any other functions */
336 void avcodec_init(void)
341 /* simple call to use all the codecs */
342 void avcodec_register_all(void)
345 #ifdef CONFIG_ENCODERS
346 register_avcodec(&ac3_encoder);
347 register_avcodec(&mp2_encoder);
348 register_avcodec(&mpeg1video_encoder);
349 register_avcodec(&h263_encoder);
350 register_avcodec(&h263p_encoder);
351 register_avcodec(&rv10_encoder);
352 register_avcodec(&mjpeg_encoder);
353 register_avcodec(&opendivx_encoder);
354 register_avcodec(&msmpeg4_encoder);
355 #endif /* CONFIG_ENCODERS */
356 register_avcodec(&pcm_codec);
357 register_avcodec(&rawvideo_codec);
360 #ifdef CONFIG_DECODERS
361 register_avcodec(&h263_decoder);
362 register_avcodec(&opendivx_decoder);
363 register_avcodec(&msmpeg4_decoder);
364 register_avcodec(&mpeg_decoder);
365 register_avcodec(&h263i_decoder);
366 register_avcodec(&rv10_decoder);
367 register_avcodec(&mjpeg_decoder);
369 register_avcodec(&mp3_decoder);
372 register_avcodec(&ac3_decoder);
374 #endif /* CONFIG_DECODERS */
377 static int encode_init(AVCodecContext *s)
382 static int decode_frame(AVCodecContext *avctx,
383 void *data, int *data_size,
384 UINT8 *buf, int buf_size)
389 static int encode_frame(AVCodecContext *avctx,
390 unsigned char *frame, int buf_size, void *data)
395 /* dummy pcm codec */
396 AVCodec pcm_codec = {
407 AVCodec rawvideo_codec = {