3 * Copyright (c) 2001 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "mpegvideo.h"
23 void *av_mallocz(int size)
26 ptr = av_malloc(size);
33 /* cannot call it directly because of 'void **' casting is not automatic */
34 void __av_freep(void **ptr)
40 /* encoder management */
41 AVCodec *first_avcodec;
43 void register_avcodec(AVCodec *format)
47 while (*p != NULL) p = &(*p)->next;
52 void avcodec_get_context_defaults(AVCodecContext *s){
53 s->bit_rate= 800*1000;
54 s->bit_rate_tolerance= s->bit_rate*10;
57 s->rc_eq= "tex^qComp";
60 s->b_quant_factor=1.25;
61 s->b_quant_offset=1.25;
62 s->i_quant_factor=-0.8;
63 s->i_quant_offset=0.0;
64 s->error_concealment= 3;
65 s->error_resilience= 1;
66 s->workaround_bugs= FF_BUG_AUTODETECT;
67 s->frame_rate = 25 * FRAME_RATE_BASE;
69 s->me_method= ME_EPZS;
73 * allocates a AVCodecContext and set it to defaults.
74 * this can be deallocated by simply calling free()
76 AVCodecContext *avcodec_alloc_context(void){
77 AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
79 if(avctx==NULL) return NULL;
81 avcodec_get_context_defaults(avctx);
86 int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
91 avctx->frame_number = 0;
92 if (codec->priv_data_size > 0) {
93 avctx->priv_data = av_mallocz(codec->priv_data_size);
94 if (!avctx->priv_data)
97 avctx->priv_data = NULL;
99 ret = avctx->codec->init(avctx);
101 av_freep(&avctx->priv_data);
107 int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
108 const short *samples)
112 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
113 avctx->frame_number++;
117 int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
118 const AVPicture *pict)
122 ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
123 avctx->frame_number++;
127 /* decode a frame. return -1 if error, otherwise return the number of
128 bytes used. If no frame could be decompressed, *got_picture_ptr is
129 zero. Otherwise, it is non zero */
130 int avcodec_decode_video(AVCodecContext *avctx, AVPicture *picture,
131 int *got_picture_ptr,
132 UINT8 *buf, int buf_size)
136 ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
138 if (*got_picture_ptr)
139 avctx->frame_number++;
143 /* decode an audio frame. return -1 if error, otherwise return the
144 *number of bytes used. If no frame could be decompressed,
145 *frame_size_ptr is zero. Otherwise, it is the decompressed frame
147 int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
149 UINT8 *buf, int buf_size)
153 ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
155 avctx->frame_number++;
159 int avcodec_close(AVCodecContext *avctx)
161 if (avctx->codec->close)
162 avctx->codec->close(avctx);
163 av_freep(&avctx->priv_data);
168 AVCodec *avcodec_find_encoder(enum CodecID id)
173 if (p->encode != NULL && p->id == id)
180 AVCodec *avcodec_find_encoder_by_name(const char *name)
185 if (p->encode != NULL && strcmp(name,p->name) == 0)
192 AVCodec *avcodec_find_decoder(enum CodecID id)
197 if (p->decode != NULL && p->id == id)
204 AVCodec *avcodec_find_decoder_by_name(const char *name)
209 if (p->decode != NULL && strcmp(name,p->name) == 0)
216 AVCodec *avcodec_find(enum CodecID id)
228 const char *pix_fmt_str[] = {
241 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
243 const char *codec_name;
246 char channels_str[100];
250 p = avcodec_find_encoder(enc->codec_id);
252 p = avcodec_find_decoder(enc->codec_id);
255 codec_name = p->name;
256 } else if (enc->codec_name[0] != '\0') {
257 codec_name = enc->codec_name;
259 /* output avi tags */
260 if (enc->codec_type == CODEC_TYPE_VIDEO) {
261 snprintf(buf1, sizeof(buf1), "%c%c%c%c",
262 enc->codec_tag & 0xff,
263 (enc->codec_tag >> 8) & 0xff,
264 (enc->codec_tag >> 16) & 0xff,
265 (enc->codec_tag >> 24) & 0xff);
267 snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
272 switch(enc->codec_type) {
273 case CODEC_TYPE_VIDEO:
274 snprintf(buf, buf_size,
276 codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
277 if (enc->codec_id == CODEC_ID_RAWVIDEO) {
278 snprintf(buf + strlen(buf), buf_size - strlen(buf),
280 pix_fmt_str[enc->pix_fmt]);
283 snprintf(buf + strlen(buf), buf_size - strlen(buf),
284 ", %dx%d, %0.2f fps",
285 enc->width, enc->height,
286 (float)enc->frame_rate / FRAME_RATE_BASE);
289 snprintf(buf + strlen(buf), buf_size - strlen(buf),
290 ", q=%d-%d", enc->qmin, enc->qmax);
292 bitrate = enc->bit_rate;
294 case CODEC_TYPE_AUDIO:
295 snprintf(buf, buf_size,
298 switch (enc->channels) {
300 strcpy(channels_str, "mono");
303 strcpy(channels_str, "stereo");
306 strcpy(channels_str, "5:1");
309 sprintf(channels_str, "%d channels", enc->channels);
312 if (enc->sample_rate) {
313 snprintf(buf + strlen(buf), buf_size - strlen(buf),
319 /* for PCM codecs, compute bitrate directly */
320 switch(enc->codec_id) {
321 case CODEC_ID_PCM_S16LE:
322 case CODEC_ID_PCM_S16BE:
323 case CODEC_ID_PCM_U16LE:
324 case CODEC_ID_PCM_U16BE:
325 bitrate = enc->sample_rate * enc->channels * 16;
327 case CODEC_ID_PCM_S8:
328 case CODEC_ID_PCM_U8:
329 case CODEC_ID_PCM_ALAW:
330 case CODEC_ID_PCM_MULAW:
331 bitrate = enc->sample_rate * enc->channels * 8;
334 bitrate = enc->bit_rate;
342 if (enc->flags & CODEC_FLAG_PASS1)
343 snprintf(buf + strlen(buf), buf_size - strlen(buf),
345 if (enc->flags & CODEC_FLAG_PASS2)
346 snprintf(buf + strlen(buf), buf_size - strlen(buf),
350 snprintf(buf + strlen(buf), buf_size - strlen(buf),
351 ", %d kb/s", bitrate / 1000);
355 /* Picture field are filled with 'ptr' addresses */
356 void avpicture_fill(AVPicture *picture, UINT8 *ptr,
357 int pix_fmt, int width, int height)
361 size = width * height;
363 case PIX_FMT_YUV420P:
364 picture->data[0] = ptr;
365 picture->data[1] = picture->data[0] + size;
366 picture->data[2] = picture->data[1] + size / 4;
367 picture->linesize[0] = width;
368 picture->linesize[1] = width / 2;
369 picture->linesize[2] = width / 2;
371 case PIX_FMT_YUV422P:
372 picture->data[0] = ptr;
373 picture->data[1] = picture->data[0] + size;
374 picture->data[2] = picture->data[1] + size / 2;
375 picture->linesize[0] = width;
376 picture->linesize[1] = width / 2;
377 picture->linesize[2] = width / 2;
379 case PIX_FMT_YUV444P:
380 picture->data[0] = ptr;
381 picture->data[1] = picture->data[0] + size;
382 picture->data[2] = picture->data[1] + size;
383 picture->linesize[0] = width;
384 picture->linesize[1] = width;
385 picture->linesize[2] = width;
389 picture->data[0] = ptr;
390 picture->data[1] = NULL;
391 picture->data[2] = NULL;
392 picture->linesize[0] = width * 3;
396 picture->data[0] = ptr;
397 picture->data[1] = NULL;
398 picture->data[2] = NULL;
399 picture->linesize[0] = width * 4;
402 picture->data[0] = ptr;
403 picture->data[1] = NULL;
404 picture->data[2] = NULL;
405 picture->linesize[0] = width * 2;
408 picture->data[0] = NULL;
409 picture->data[1] = NULL;
410 picture->data[2] = NULL;
415 int avpicture_get_size(int pix_fmt, int width, int height)
419 size = width * height;
421 case PIX_FMT_YUV420P:
422 size = (size * 3) / 2;
424 case PIX_FMT_YUV422P:
427 case PIX_FMT_YUV444P:
448 unsigned avcodec_version( void )
450 return LIBAVCODEC_VERSION_INT;
453 unsigned avcodec_build( void )
455 return LIBAVCODEC_BUILD;
458 /* must be called before any other functions */
459 void avcodec_init(void)
461 static int inited = 0;
470 /* this should be called after seeking and before trying to decode the next frame */
471 void avcodec_flush_buffers(AVCodecContext *avctx)
473 MpegEncContext *s = avctx->priv_data;
474 s->num_available_buffers=0;
478 static int raw_encode_init(AVCodecContext *s)
483 static int raw_decode_frame(AVCodecContext *avctx,
484 void *data, int *data_size,
485 UINT8 *buf, int buf_size)
490 static int raw_encode_frame(AVCodecContext *avctx,
491 unsigned char *frame, int buf_size, void *data)
496 AVCodec rawvideo_codec = {