changeset 5050:a5f6fbc9fa66 libavcodec

loosen dependencies over mpegaudiodec
author aurel
date Sun, 20 May 2007 13:40:07 +0000
parents 668249bc4c16
children b908c67063c8
files Makefile mpegaudio.h mpegaudio_parser.c mpegaudiodec.c mpegaudiodecheader.c mpegaudiodecheader.h
diffstat 6 files changed, 233 insertions(+), 155 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sun May 20 05:07:44 2007 +0000
+++ b/Makefile	Sun May 20 13:40:07 2007 +0000
@@ -19,7 +19,7 @@
       motion_est.o \
       imgconvert.o \
       mpeg12.o \
-      mpegaudiodec.o mpegaudiodata.o \
+      mpegaudiodec.o mpegaudiodecheader.o mpegaudiodata.o \
       simple_idct.o \
       ratecontrol.o \
       eval.o \
@@ -300,7 +300,7 @@
 OBJS-$(CONFIG_H264_PARSER)             += h264_parser.o
 OBJS-$(CONFIG_MJPEG_PARSER)            += mjpeg_parser.o
 OBJS-$(CONFIG_MPEG4VIDEO_PARSER)       += mpeg4video_parser.o
-OBJS-$(CONFIG_MPEGAUDIO_PARSER)        += mpegaudio_parser.o
+OBJS-$(CONFIG_MPEGAUDIO_PARSER)        += mpegaudio_parser.o mpegaudiodecheader.o
 OBJS-$(CONFIG_MPEGVIDEO_PARSER)        += mpegvideo_parser.o
 OBJS-$(CONFIG_PNM_PARSER)              += pnm_parser.o pnm.o
 OBJS-$(CONFIG_VC1_PARSER)              += vc1_parser.o
--- a/mpegaudio.h	Sun May 20 05:07:44 2007 +0000
+++ b/mpegaudio.h	Sun May 20 13:40:07 2007 +0000
@@ -23,6 +23,12 @@
  * mpeg audio declarations for both encoder and decoder.
  */
 
+#ifndef MPEGAUDIO_H
+#define MPEGAUDIO_H
+
+#include "bitstream.h"
+#include "dsputil.h"
+
 /* max frame size, in samples */
 #define MPA_FRAME_SIZE 1152
 
@@ -73,6 +79,42 @@
 typedef int32_t MPA_INT;
 #endif
 
+#define BACKSTEP_SIZE 512
+#define EXTRABYTES 24
+
+struct GranuleDef;
+
+typedef struct MPADecodeContext {
+    DECLARE_ALIGNED_8(uint8_t, last_buf[2*BACKSTEP_SIZE + EXTRABYTES]);
+    int last_buf_size;
+    int frame_size;
+    /* next header (used in free format parsing) */
+    uint32_t free_format_next_header;
+    int error_protection;
+    int layer;
+    int sample_rate;
+    int sample_rate_index; /* between 0 and 8 */
+    int bit_rate;
+    GetBitContext gb;
+    GetBitContext in_gb;
+    int nb_channels;
+    int mode;
+    int mode_ext;
+    int lsf;
+    DECLARE_ALIGNED_16(MPA_INT, synth_buf[MPA_MAX_CHANNELS][512 * 2]);
+    int synth_buf_offset[MPA_MAX_CHANNELS];
+    DECLARE_ALIGNED_16(int32_t, sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT]);
+    int32_t mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
+#ifdef DEBUG
+    int frame_count;
+#endif
+    void (*compute_antialias)(struct MPADecodeContext *s, struct GranuleDef *g);
+    int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
+    int dither_state;
+    int error_resilience;
+    AVCodecContext* avctx;
+} MPADecodeContext;
+
 int l2_select_table(int bitrate, int nb_channels, int freq, int lsf);
 int mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate);
 void ff_mpa_synth_init(MPA_INT *window);
@@ -97,3 +139,5 @@
         return -1;
     return 0;
 }
+
+#endif /* MPEGAUDIO_H */
--- a/mpegaudio_parser.c	Sun May 20 05:07:44 2007 +0000
+++ b/mpegaudio_parser.c	Sun May 20 13:40:07 2007 +0000
@@ -22,6 +22,7 @@
 
 #include "parser.h"
 #include "mpegaudio.h"
+#include "mpegaudiodecheader.h"
 
 
 typedef struct MpegAudioParseContext {
@@ -41,6 +42,43 @@
 #define SAME_HEADER_MASK \
    (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
 
+/* useful helper to get mpeg audio stream infos. Return -1 if error in
+   header, otherwise the coded frame size in bytes */
+int mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate)
+{
+    MPADecodeContext s1, *s = &s1;
+    s1.avctx = avctx;
+
+    if (ff_mpa_check_header(head) != 0)
+        return -1;
+
+    if (decode_header(s, head) != 0) {
+        return -1;
+    }
+
+    switch(s->layer) {
+    case 1:
+        avctx->frame_size = 384;
+        break;
+    case 2:
+        avctx->frame_size = 1152;
+        break;
+    default:
+    case 3:
+        if (s->lsf)
+            avctx->frame_size = 576;
+        else
+            avctx->frame_size = 1152;
+        break;
+    }
+
+    *sample_rate = s->sample_rate;
+    avctx->channels = s->nb_channels;
+    avctx->bit_rate = s->bit_rate;
+    avctx->sub_id = s->layer;
+    return s->frame_size;
+}
+
 static int mpegaudio_parse_init(AVCodecParserContext *s1)
 {
     MpegAudioParseContext *s = s1->priv_data;
--- a/mpegaudiodec.c	Sun May 20 05:07:44 2007 +0000
+++ b/mpegaudiodec.c	Sun May 20 13:40:07 2007 +0000
@@ -42,6 +42,7 @@
 #endif
 
 #include "mpegaudio.h"
+#include "mpegaudiodecheader.h"
 
 #include "mathops.h"
 
@@ -57,41 +58,6 @@
 /****************/
 
 #define HEADER_SIZE 4
-#define BACKSTEP_SIZE 512
-#define EXTRABYTES 24
-
-struct GranuleDef;
-
-typedef struct MPADecodeContext {
-    DECLARE_ALIGNED_8(uint8_t, last_buf[2*BACKSTEP_SIZE + EXTRABYTES]);
-    int last_buf_size;
-    int frame_size;
-    /* next header (used in free format parsing) */
-    uint32_t free_format_next_header;
-    int error_protection;
-    int layer;
-    int sample_rate;
-    int sample_rate_index; /* between 0 and 8 */
-    int bit_rate;
-    GetBitContext gb;
-    GetBitContext in_gb;
-    int nb_channels;
-    int mode;
-    int mode_ext;
-    int lsf;
-    DECLARE_ALIGNED_16(MPA_INT, synth_buf[MPA_MAX_CHANNELS][512 * 2]);
-    int synth_buf_offset[MPA_MAX_CHANNELS];
-    DECLARE_ALIGNED_16(int32_t, sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT]);
-    int32_t mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
-#ifdef DEBUG
-    int frame_count;
-#endif
-    void (*compute_antialias)(struct MPADecodeContext *s, struct GranuleDef *g);
-    int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
-    int dither_state;
-    int error_resilience;
-    AVCodecContext* avctx;
-} MPADecodeContext;
 
 /**
  * Context for MP3On4 decoder
@@ -1107,124 +1073,6 @@
     buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
 }
 
-/* header decoding. MUST check the header before because no
-   consistency check is done there. Return 1 if free format found and
-   that the frame size must be computed externally */
-static int decode_header(MPADecodeContext *s, uint32_t header)
-{
-    int sample_rate, frame_size, mpeg25, padding;
-    int sample_rate_index, bitrate_index;
-    if (header & (1<<20)) {
-        s->lsf = (header & (1<<19)) ? 0 : 1;
-        mpeg25 = 0;
-    } else {
-        s->lsf = 1;
-        mpeg25 = 1;
-    }
-
-    s->layer = 4 - ((header >> 17) & 3);
-    /* extract frequency */
-    sample_rate_index = (header >> 10) & 3;
-    sample_rate = ff_mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
-    sample_rate_index += 3 * (s->lsf + mpeg25);
-    s->sample_rate_index = sample_rate_index;
-    s->error_protection = ((header >> 16) & 1) ^ 1;
-    s->sample_rate = sample_rate;
-
-    bitrate_index = (header >> 12) & 0xf;
-    padding = (header >> 9) & 1;
-    //extension = (header >> 8) & 1;
-    s->mode = (header >> 6) & 3;
-    s->mode_ext = (header >> 4) & 3;
-    //copyright = (header >> 3) & 1;
-    //original = (header >> 2) & 1;
-    //emphasis = header & 3;
-
-    if (s->mode == MPA_MONO)
-        s->nb_channels = 1;
-    else
-        s->nb_channels = 2;
-
-    if (bitrate_index != 0) {
-        frame_size = ff_mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
-        s->bit_rate = frame_size * 1000;
-        switch(s->layer) {
-        case 1:
-            frame_size = (frame_size * 12000) / sample_rate;
-            frame_size = (frame_size + padding) * 4;
-            break;
-        case 2:
-            frame_size = (frame_size * 144000) / sample_rate;
-            frame_size += padding;
-            break;
-        default:
-        case 3:
-            frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
-            frame_size += padding;
-            break;
-        }
-        s->frame_size = frame_size;
-    } else {
-        /* if no frame size computed, signal it */
-        return 1;
-    }
-
-#if defined(DEBUG)
-    dprintf(s->avctx, "layer%d, %d Hz, %d kbits/s, ",
-           s->layer, s->sample_rate, s->bit_rate);
-    if (s->nb_channels == 2) {
-        if (s->layer == 3) {
-            if (s->mode_ext & MODE_EXT_MS_STEREO)
-                dprintf(s->avctx, "ms-");
-            if (s->mode_ext & MODE_EXT_I_STEREO)
-                dprintf(s->avctx, "i-");
-        }
-        dprintf(s->avctx, "stereo");
-    } else {
-        dprintf(s->avctx, "mono");
-    }
-    dprintf(s->avctx, "\n");
-#endif
-    return 0;
-}
-
-/* useful helper to get mpeg audio stream infos. Return -1 if error in
-   header, otherwise the coded frame size in bytes */
-int mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate)
-{
-    MPADecodeContext s1, *s = &s1;
-    s1.avctx = avctx;
-
-    if (ff_mpa_check_header(head) != 0)
-        return -1;
-
-    if (decode_header(s, head) != 0) {
-        return -1;
-    }
-
-    switch(s->layer) {
-    case 1:
-        avctx->frame_size = 384;
-        break;
-    case 2:
-        avctx->frame_size = 1152;
-        break;
-    default:
-    case 3:
-        if (s->lsf)
-            avctx->frame_size = 576;
-        else
-            avctx->frame_size = 1152;
-        break;
-    }
-
-    *sample_rate = s->sample_rate;
-    avctx->channels = s->nb_channels;
-    avctx->bit_rate = s->bit_rate;
-    avctx->sub_id = s->layer;
-    return s->frame_size;
-}
-
 /* return the number of decoded frames */
 static int mp_decode_layer1(MPADecodeContext *s)
 {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mpegaudiodecheader.c	Sun May 20 13:40:07 2007 +0000
@@ -0,0 +1,109 @@
+/*
+ * MPEG Audio header decoder
+ * Copyright (c) 2001, 2002 Fabrice Bellard.
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file mpegaudiodecheader.c
+ * MPEG Audio header decoder.
+ */
+
+//#define DEBUG
+#include "avcodec.h"
+#include "mpegaudio.h"
+#include "mpegaudiodata.h"
+
+
+int decode_header(MPADecodeContext *s, uint32_t header)
+{
+    int sample_rate, frame_size, mpeg25, padding;
+    int sample_rate_index, bitrate_index;
+    if (header & (1<<20)) {
+        s->lsf = (header & (1<<19)) ? 0 : 1;
+        mpeg25 = 0;
+    } else {
+        s->lsf = 1;
+        mpeg25 = 1;
+    }
+
+    s->layer = 4 - ((header >> 17) & 3);
+    /* extract frequency */
+    sample_rate_index = (header >> 10) & 3;
+    sample_rate = ff_mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
+    sample_rate_index += 3 * (s->lsf + mpeg25);
+    s->sample_rate_index = sample_rate_index;
+    s->error_protection = ((header >> 16) & 1) ^ 1;
+    s->sample_rate = sample_rate;
+
+    bitrate_index = (header >> 12) & 0xf;
+    padding = (header >> 9) & 1;
+    //extension = (header >> 8) & 1;
+    s->mode = (header >> 6) & 3;
+    s->mode_ext = (header >> 4) & 3;
+    //copyright = (header >> 3) & 1;
+    //original = (header >> 2) & 1;
+    //emphasis = header & 3;
+
+    if (s->mode == MPA_MONO)
+        s->nb_channels = 1;
+    else
+        s->nb_channels = 2;
+
+    if (bitrate_index != 0) {
+        frame_size = ff_mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
+        s->bit_rate = frame_size * 1000;
+        switch(s->layer) {
+        case 1:
+            frame_size = (frame_size * 12000) / sample_rate;
+            frame_size = (frame_size + padding) * 4;
+            break;
+        case 2:
+            frame_size = (frame_size * 144000) / sample_rate;
+            frame_size += padding;
+            break;
+        default:
+        case 3:
+            frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
+            frame_size += padding;
+            break;
+        }
+        s->frame_size = frame_size;
+    } else {
+        /* if no frame size computed, signal it */
+        return 1;
+    }
+
+#if defined(DEBUG)
+    dprintf(s->avctx, "layer%d, %d Hz, %d kbits/s, ",
+           s->layer, s->sample_rate, s->bit_rate);
+    if (s->nb_channels == 2) {
+        if (s->layer == 3) {
+            if (s->mode_ext & MODE_EXT_MS_STEREO)
+                dprintf(s->avctx, "ms-");
+            if (s->mode_ext & MODE_EXT_I_STEREO)
+                dprintf(s->avctx, "i-");
+        }
+        dprintf(s->avctx, "stereo");
+    } else {
+        dprintf(s->avctx, "mono");
+    }
+    dprintf(s->avctx, "\n");
+#endif
+    return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mpegaudiodecheader.h	Sun May 20 13:40:07 2007 +0000
@@ -0,0 +1,39 @@
+/*
+ * MPEG Audio header decoder
+ * Copyright (c) 2001, 2002 Fabrice Bellard.
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file mpegaudiodecheader.c
+ * MPEG Audio header decoder.
+ */
+
+#ifndef MPEGAUDIODECHEADER_H
+#define MPEGAUDIODECHEADER_H
+
+#include "common.h"
+#include "mpegaudio.h"
+
+
+/* header decoding. MUST check the header before because no
+   consistency check is done there. Return 1 if free format found and
+   that the frame size must be computed externally */
+int decode_header(MPADecodeContext *s, uint32_t header);
+
+#endif /* MPEGAUDIODECHEADER_H */