/*
* utils for libavcodec
- * Copyright (c) 2001 Gerard Lantau.
+ * Copyright (c) 2001 Fabrice Bellard.
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
*
- * This program is distributed in the hope that it will be useful,
+ * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
-#include "common.h"
-#include "dsputil.h"
#include "avcodec.h"
+#include "dsputil.h"
+#include "mpegvideo.h"
-/* memory alloc */
void *av_mallocz(int size)
{
void *ptr;
- ptr = malloc(size);
+ ptr = av_malloc(size);
if (!ptr)
return NULL;
memset(ptr, 0, size);
return ptr;
}
+/* cannot call it directly because of 'void **' casting is not automatic */
+void __av_freep(void **ptr)
+{
+ av_free(*ptr);
+ *ptr = NULL;
+}
+
/* encoder management */
AVCodec *first_avcodec;
format->next = NULL;
}
+void avcodec_get_context_defaults(AVCodecContext *s){
+ s->bit_rate= 800*1000;
+ s->bit_rate_tolerance= s->bit_rate*10;
+ s->qmin= 2;
+ s->qmax= 31;
+ s->rc_eq= "tex^qComp";
+ s->qcompress= 0.5;
+ s->max_qdiff= 3;
+ s->b_quant_factor=1.25;
+ s->b_quant_offset=1.25;
+ s->i_quant_factor=-0.8;
+ s->i_quant_offset=0.0;
+ s->error_concealment= 3;
+ s->error_resilience= 1;
+ s->workaround_bugs= FF_BUG_AUTODETECT;
+ s->frame_rate = 25 * FRAME_RATE_BASE;
+ s->gop_size= 50;
+ s->me_method= ME_EPZS;
+}
+
+/**
+ * allocates a AVCodecContext and set it to defaults.
+ * this can be deallocated by simply calling free()
+ */
+AVCodecContext *avcodec_alloc_context(void){
+ AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
+
+ if(avctx==NULL) return NULL;
+
+ avcodec_get_context_defaults(avctx);
+
+ return avctx;
+}
+
int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
{
int ret;
avctx->codec = codec;
avctx->frame_number = 0;
- avctx->priv_data = av_mallocz(codec->priv_data_size);
- if (!avctx->priv_data)
- return -ENOMEM;
+ if (codec->priv_data_size > 0) {
+ avctx->priv_data = av_mallocz(codec->priv_data_size);
+ if (!avctx->priv_data)
+ return -ENOMEM;
+ } else {
+ avctx->priv_data = NULL;
+ }
ret = avctx->codec->init(avctx);
if (ret < 0) {
- free(avctx->priv_data);
- avctx->priv_data = NULL;
+ av_freep(&avctx->priv_data);
return ret;
}
return 0;
ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
buf, buf_size);
- avctx->frame_number++;
+ if (*got_picture_ptr)
+ avctx->frame_number++;
return ret;
}
{
if (avctx->codec->close)
avctx->codec->close(avctx);
- free(avctx->priv_data);
- avctx->priv_data = NULL;
+ av_freep(&avctx->priv_data);
avctx->codec = NULL;
return 0;
}
return NULL;
}
+AVCodec *avcodec_find_encoder_by_name(const char *name)
+{
+ AVCodec *p;
+ p = first_avcodec;
+ while (p) {
+ if (p->encode != NULL && strcmp(name,p->name) == 0)
+ return p;
+ p = p->next;
+ }
+ return NULL;
+}
+
AVCodec *avcodec_find_decoder(enum CodecID id)
{
AVCodec *p;
"bgr24",
"yuv422p",
"yuv444p",
+ "rgba32",
+ "bgra32",
+ "yuv410p",
+ "yuv411p",
};
-
+
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
{
const char *codec_name;
AVCodec *p;
char buf1[32];
+ char channels_str[100];
+ int bitrate;
if (encode)
p = avcodec_find_encoder(enc->codec_id);
enc->width, enc->height,
(float)enc->frame_rate / FRAME_RATE_BASE);
}
+ if (encode) {
+ snprintf(buf + strlen(buf), buf_size - strlen(buf),
+ ", q=%d-%d", enc->qmin, enc->qmax);
+ }
+ bitrate = enc->bit_rate;
break;
case CODEC_TYPE_AUDIO:
snprintf(buf, buf_size,
"Audio: %s",
codec_name);
+ switch (enc->channels) {
+ case 1:
+ strcpy(channels_str, "mono");
+ break;
+ case 2:
+ strcpy(channels_str, "stereo");
+ break;
+ case 6:
+ strcpy(channels_str, "5:1");
+ break;
+ default:
+ sprintf(channels_str, "%d channels", enc->channels);
+ break;
+ }
if (enc->sample_rate) {
snprintf(buf + strlen(buf), buf_size - strlen(buf),
", %d Hz, %s",
enc->sample_rate,
- enc->channels == 2 ? "stereo" : "mono");
+ channels_str);
+ }
+
+ /* for PCM codecs, compute bitrate directly */
+ switch(enc->codec_id) {
+ case CODEC_ID_PCM_S16LE:
+ case CODEC_ID_PCM_S16BE:
+ case CODEC_ID_PCM_U16LE:
+ case CODEC_ID_PCM_U16BE:
+ bitrate = enc->sample_rate * enc->channels * 16;
+ break;
+ case CODEC_ID_PCM_S8:
+ case CODEC_ID_PCM_U8:
+ case CODEC_ID_PCM_ALAW:
+ case CODEC_ID_PCM_MULAW:
+ bitrate = enc->sample_rate * enc->channels * 8;
+ break;
+ default:
+ bitrate = enc->bit_rate;
+ break;
}
break;
default:
- abort();
+ av_abort();
}
- if (enc->bit_rate != 0) {
+ if (encode) {
+ if (enc->flags & CODEC_FLAG_PASS1)
+ snprintf(buf + strlen(buf), buf_size - strlen(buf),
+ ", pass 1");
+ if (enc->flags & CODEC_FLAG_PASS2)
+ snprintf(buf + strlen(buf), buf_size - strlen(buf),
+ ", pass 2");
+ }
+ if (bitrate != 0) {
snprintf(buf + strlen(buf), buf_size - strlen(buf),
- ", %d kb/s", enc->bit_rate / 1000);
+ ", %d kb/s", bitrate / 1000);
}
}
picture->data[2] = NULL;
picture->linesize[0] = width * 3;
break;
+ case PIX_FMT_RGBA32:
+ case PIX_FMT_BGRA32:
+ picture->data[0] = ptr;
+ picture->data[1] = NULL;
+ picture->data[2] = NULL;
+ picture->linesize[0] = width * 4;
+ break;
case PIX_FMT_YUV422:
picture->data[0] = ptr;
picture->data[1] = NULL;
case PIX_FMT_BGR24:
size = (size * 3);
break;
+ case PIX_FMT_RGBA32:
+ case PIX_FMT_BGRA32:
+ size = (size * 4);
+ break;
case PIX_FMT_YUV422:
size = (size * 2);
break;
return size;
}
+unsigned avcodec_version( void )
+{
+ return LIBAVCODEC_VERSION_INT;
+}
+
+unsigned avcodec_build( void )
+{
+ return LIBAVCODEC_BUILD;
+}
/* must be called before any other functions */
void avcodec_init(void)
{
+ static int inited = 0;
+
+ if (inited != 0)
+ return;
+ inited = 1;
+
dsputil_init();
}
-/* simple call to use all the codecs */
-void avcodec_register_all(void)
+/* this should be called after seeking and before trying to decode the next frame */
+void avcodec_flush_buffers(AVCodecContext *avctx)
{
- /* encoders */
-#ifdef CONFIG_ENCODERS
- register_avcodec(&ac3_encoder);
- register_avcodec(&mp2_encoder);
- register_avcodec(&mpeg1video_encoder);
- register_avcodec(&h263_encoder);
- register_avcodec(&h263p_encoder);
- register_avcodec(&rv10_encoder);
- register_avcodec(&mjpeg_encoder);
- register_avcodec(&opendivx_encoder);
- register_avcodec(&msmpeg4_encoder);
-#endif /* CONFIG_ENCODERS */
- register_avcodec(&pcm_codec);
- register_avcodec(&rawvideo_codec);
-
- /* decoders */
-#ifdef CONFIG_DECODERS
- register_avcodec(&h263_decoder);
- register_avcodec(&opendivx_decoder);
- register_avcodec(&msmpeg4_decoder);
- register_avcodec(&mpeg_decoder);
- register_avcodec(&h263i_decoder);
- register_avcodec(&rv10_decoder);
- register_avcodec(&mjpeg_decoder);
-#ifdef CONFIG_MPGLIB
- register_avcodec(&mp3_decoder);
-#endif
-#ifdef CONFIG_AC3
- register_avcodec(&ac3_decoder);
-#endif
-#endif /* CONFIG_DECODERS */
+ MpegEncContext *s = avctx->priv_data;
+ s->num_available_buffers=0;
}
-static int encode_init(AVCodecContext *s)
+
+static int raw_encode_init(AVCodecContext *s)
{
return 0;
}
-static int decode_frame(AVCodecContext *avctx,
- void *data, int *data_size,
- UINT8 *buf, int buf_size)
+static int raw_decode_frame(AVCodecContext *avctx,
+ void *data, int *data_size,
+ UINT8 *buf, int buf_size)
{
return -1;
}
-static int encode_frame(AVCodecContext *avctx,
- unsigned char *frame, int buf_size, void *data)
+static int raw_encode_frame(AVCodecContext *avctx,
+ unsigned char *frame, int buf_size, void *data)
{
return -1;
}
-/* dummy pcm codec */
-AVCodec pcm_codec = {
- "pcm",
- CODEC_TYPE_AUDIO,
- CODEC_ID_PCM,
- 0,
- encode_init,
- encode_frame,
- NULL,
- decode_frame,
-};
-
AVCodec rawvideo_codec = {
"rawvideo",
CODEC_TYPE_VIDEO,
CODEC_ID_RAWVIDEO,
0,
- encode_init,
- encode_frame,
+ raw_encode_init,
+ raw_encode_frame,
NULL,
- decode_frame,
+ raw_decode_frame,
};