]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/utils.c
exporting mbskip_table after it has been allocated
[ffmpeg] / libavcodec / utils.c
index b32bd18033bfc60032b06452452b8127b8f4d115..05fd6ab704a67e6a0ea44b78fe7c2060486b7707 100644 (file)
@@ -1,40 +1,42 @@
 /*
  * 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;
 
@@ -53,13 +55,16 @@ int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
 
     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;
@@ -96,7 +101,8 @@ int avcodec_decode_video(AVCodecContext *avctx, AVPicture *picture,
 
     ret = avctx->codec->decode(avctx, picture, got_picture_ptr, 
                                buf, buf_size);
-    avctx->frame_number++;
+    if (*got_picture_ptr)                           
+        avctx->frame_number++;
     return ret;
 }
 
@@ -120,8 +126,7 @@ int avcodec_close(AVCodecContext *avctx)
 {
     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;
 }
@@ -138,6 +143,18 @@ AVCodec *avcodec_find_encoder(enum CodecID id)
     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;
@@ -175,12 +192,14 @@ AVCodec *avcodec_find(enum CodecID id)
 }
 
 const char *pix_fmt_str[] = {
+    "??",
     "yuv420p",
     "yuv422",
     "rgb24",
     "bgr24",
     "yuv422p",
     "yuv444p",
+    "yuv410p"
 };
     
 void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
@@ -188,6 +207,8 @@ 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);
@@ -228,24 +249,61 @@ void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
                      enc->width, enc->height, 
                      (float)enc->frame_rate / FRAME_RATE_BASE);
         }
+        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();
     }
-    if (enc->bit_rate != 0) {
+    if (bitrate != 0) {
         snprintf(buf + strlen(buf), buf_size - strlen(buf), 
-                 ", %d kb/s", enc->bit_rate / 1000);
+                 ", %d kb/s", bitrate / 1000);
     }
 }
 
@@ -331,86 +389,61 @@ int avpicture_get_size(int pix_fmt, int width, int height)
     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,
 };