]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/wmadec.c
seperated out the C-based VP3 DSP functions into a different file; also
[ffmpeg] / libavcodec / wmadec.c
index 9826e4bc9a2321dee7ecb8d1855428fe30f9adad..25498c4d28cf80cad58cc64550f99941b92c6097 100644 (file)
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
+
+/**
+ * @file wmadec.c
+ * WMA compatible decoder.
+ */
+
 #include "avcodec.h"
 #include "dsputil.h"
 
-//#define DEBUG_PARAMS
-//#define DEBUG_TRACE
-
 /* size of blocks */
 #define BLOCK_MIN_BITS 7
 #define BLOCK_MAX_BITS 11
@@ -87,15 +90,15 @@ typedef struct WMADecodeContext {
     int block_pos; /* current position in frame */
     uint8_t ms_stereo; /* true if mid/side stereo mode */
     uint8_t channel_coded[MAX_CHANNELS]; /* true if channel is coded */
-    float exponents[MAX_CHANNELS][BLOCK_MAX_SIZE];
+    float exponents[MAX_CHANNELS][BLOCK_MAX_SIZE] __attribute__((aligned(16)));
     float max_exponent[MAX_CHANNELS];
     int16_t coefs1[MAX_CHANNELS][BLOCK_MAX_SIZE];
-    float coefs[MAX_CHANNELS][BLOCK_MAX_SIZE];
+    float coefs[MAX_CHANNELS][BLOCK_MAX_SIZE] __attribute__((aligned(16)));
     MDCTContext mdct_ctx[BLOCK_NB_SIZES];
     float *windows[BLOCK_NB_SIZES];
-    FFTSample mdct_tmp[BLOCK_MAX_SIZE]; /* temporary storage for imdct */
+    FFTSample mdct_tmp[BLOCK_MAX_SIZE] __attribute__((aligned(16))); /* temporary storage for imdct */
     /* output buffer for one frame and the last for IMDCT windowing */
-    float frame_out[MAX_CHANNELS][BLOCK_MAX_SIZE * 2];
+    float frame_out[MAX_CHANNELS][BLOCK_MAX_SIZE * 2] __attribute__((aligned(16)));
     /* last frame info */
     uint8_t last_superframe[MAX_CODED_SUPERFRAME_SIZE + 4]; /* padding added */
     int last_bitoffset;
@@ -108,6 +111,10 @@ typedef struct WMADecodeContext {
     float lsp_pow_e_table[256];
     float lsp_pow_m_table1[(1 << LSP_POW_BITS)];
     float lsp_pow_m_table2[(1 << LSP_POW_BITS)];
+
+#ifdef TRACE
+    int frame_count;
+#endif
 } WMADecodeContext;
 
 typedef struct CoefVLCTable {
@@ -121,57 +128,18 @@ static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len);
 
 #include "wmadata.h"
 
-#ifdef DEBUG_TRACE
-
-int frame_count;
-
-static FILE *flog;
-
-void trace(const char *fmt, ...)
-{
-    va_list ap;
-    
-
-    if (!flog) {
-        flog = fopen("/tmp/out.log", "w");
-        setlinebuf(flog);
-    }
-
-    va_start(ap, fmt);
-    vfprintf(flog, fmt, ap);
-    va_end(ap);
-}
-
-#define get_bits(s, n) get_bits_trace(s, n)
-#define get_vlc(s, vlc) get_vlc_trace(s, vlc)
-
-unsigned int get_bits_trace(GetBitContext *s, int n)
-{
-    unsigned int val;
-    val = (get_bits)(s, n);
-    trace("get_bits(%d) : 0x%x\n", n, val);
-    return val;
-}
-
-static int get_vlc_trace(GetBitContext *s, VLC *vlc)
-{
-    int code;
-    code = (get_vlc)(s, vlc);
-    trace("get_vlc() : %d\n", code);
-    return code;
-}
-
+#ifdef TRACE
 static void dump_shorts(const char *name, const short *tab, int n)
 {
     int i;
 
-    trace("%s[%d]:\n", name, n);
+    tprintf("%s[%d]:\n", name, n);
     for(i=0;i<n;i++) {
         if ((i & 7) == 0)
-            trace("%4d: ", i);
-        trace(" %5d.0", tab[i]);
+            tprintf("%4d: ", i);
+        tprintf(" %5d.0", tab[i]);
         if ((i & 7) == 7)
-            trace("\n");
+            tprintf("\n");
     }
 }
 
@@ -179,22 +147,17 @@ static void dump_floats(const char *name, int prec, const float *tab, int n)
 {
     int i;
 
-    trace("%s[%d]:\n", name, n);
+    tprintf("%s[%d]:\n", name, n);
     for(i=0;i<n;i++) {
         if ((i & 7) == 0)
-            trace("%4d: ", i);
-        trace(" %8.*f", prec, tab[i]);
+            tprintf("%4d: ", i);
+        tprintf(" %8.*f", prec, tab[i]);
         if ((i & 7) == 7)
-            trace("\n");
+            tprintf("\n");
     }
     if ((i & 7) != 0)
-        trace("\n");
+        tprintf("\n");
 }
-
-#else
-
-#define trace(fmt, ...)
-
 #endif
 
 /* XXX: use same run/length optimization as mpeg decoders */
@@ -212,8 +175,8 @@ static void init_coef_vlc(VLC *vlc,
 
     init_vlc(vlc, 9, n, table_bits, 1, 1, table_codes, 4, 4);
 
-    run_table = malloc(n * sizeof(uint16_t));
-    level_table = malloc(n * sizeof(uint16_t));
+    run_table = av_malloc(n * sizeof(uint16_t));
+    level_table = av_malloc(n * sizeof(uint16_t));
     p = levels_table;
     i = 2;
     level = 1;
@@ -245,7 +208,7 @@ static int wma_decode_init(AVCodecContext * avctx)
     s->bit_rate = avctx->bit_rate;
     s->block_align = avctx->block_align;
 
-    if (avctx->codec_id == CODEC_ID_WMAV1) {
+    if (avctx->codec->id == CODEC_ID_WMAV1) {
         s->version = 1;
     } else {
         s->version = 2;
@@ -278,7 +241,14 @@ static int wma_decode_init(AVCodecContext * avctx)
     }
     s->frame_len = 1 << s->frame_len_bits;
     if (s->use_variable_block_len) {
-        s->nb_block_sizes = s->frame_len_bits - BLOCK_MIN_BITS + 1;
+        int nb_max, nb;
+        nb = ((flags2 >> 3) & 3) + 1;
+        if ((s->bit_rate / s->nb_channels) >= 32000)
+            nb += 2;
+        nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
+        if (nb > nb_max)
+            nb = nb_max;
+        s->nb_block_sizes = nb + 1;
     } else {
         s->nb_block_sizes = 1;
     }
@@ -346,16 +316,14 @@ static int wma_decode_init(AVCodecContext * avctx)
             high_freq = high_freq * 0.5;
         }
     }
-#ifdef DEBUG_PARAMS
-    printf("flags1=0x%x flags2=0x%x\n", flags1, flags2);
-    printf("version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
+    dprintf("flags1=0x%x flags2=0x%x\n", flags1, flags2);
+    dprintf("version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
            s->version, s->nb_channels, s->sample_rate, s->bit_rate, 
            s->block_align);
-    printf("bps=%f bps1=%f high_freq=%f bitoffset=%d\n", 
+    dprintf("bps=%f bps1=%f high_freq=%f bitoffset=%d\n", 
            bps, bps1, high_freq, s->byte_offset_bits);
-    printf("use_noise_coding=%d use_exp_vlc=%d\n",
-           s->use_noise_coding, s->use_exp_vlc);
-#endif
+    dprintf("use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
+           s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes);
 
     /* compute the scale factor band sizes for each MDCT block size */
     {
@@ -445,28 +413,28 @@ static int wma_decode_init(AVCodecContext * avctx)
             }
             s->exponent_high_sizes[k] = j;
 #if 0
-            trace("%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
+            tprintf("%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
                   s->frame_len >> k, 
                   s->coefs_end[k],
                   s->high_band_start[k],
                   s->exponent_high_sizes[k]);
             for(j=0;j<s->exponent_high_sizes[k];j++)
-                trace(" %d", s->exponent_high_bands[k][j]);
-            trace("\n");
+                tprintf(" %d", s->exponent_high_bands[k][j]);
+            tprintf("\n");
 #endif
         }
     }
 
-#ifdef DEBUG_TRACE
+#ifdef TRACE
     {
         int i, j;
         for(i = 0; i < s->nb_block_sizes; i++) {
-            trace("%5d: n=%2d:", 
+            tprintf("%5d: n=%2d:", 
                    s->frame_len >> i, 
                    s->exponent_sizes[i]);
             for(j=0;j<s->exponent_sizes[i];j++)
-                trace(" %d", s->exponent_bands[i][j]);
-            trace("\n");
+                tprintf(" %d", s->exponent_bands[i][j]);
+            tprintf("\n");
         }
     }
 #endif
@@ -498,7 +466,7 @@ static int wma_decode_init(AVCodecContext * avctx)
         else
             s->noise_mult = 0.04;
                
-#if defined(DEBUG_TRACE)
+#ifdef TRACE
         for(i=0;i<NOISE_TAB_SIZE;i++)
             s->noise_table[i] = 1.0 * s->noise_mult;
 #else
@@ -728,7 +696,9 @@ static int wma_decode_block(WMADecodeContext *s)
     int nb_coefs[MAX_CHANNELS];
     float mdct_norm;
 
-    trace("***decode_block: %d:%d\n", frame_count - 1, s->block_num);
+#ifdef TRACE
+    tprintf("***decode_block: %d:%d\n", s->frame_count - 1, s->block_num);
+#endif
 
     /* compute current block length */
     if (s->use_variable_block_len) {
@@ -974,7 +944,7 @@ static int wma_decode_block(WMADecodeContext *s)
                         }
                         exp_power[j] = e2 / n;
                         last_high_band = j;
-                        trace("%d: power=%f (%d)\n", j, exp_power[j], n);
+                        tprintf("%d: power=%f (%d)\n", j, exp_power[j], n);
                     }
                     exp_ptr += n;
                 }
@@ -1032,7 +1002,7 @@ static int wma_decode_block(WMADecodeContext *s)
         }
     }
 
-#ifdef DEBUG_TRACE
+#ifdef TRACE
     for(ch = 0; ch < s->nb_channels; ch++) {
         if (s->channel_coded[ch]) {
             dump_floats("exponents", 3, s->exponents[ch], s->block_len);
@@ -1049,9 +1019,7 @@ static int wma_decode_block(WMADecodeContext *s)
         /* no need to optimize this case because it should almost
            never happen */
         if (!s->channel_coded[0]) {
-#ifdef DEBUG_TRACE
-            trace("rare ms-stereo case happened\n");
-#endif
+            tprintf("rare ms-stereo case happened\n");
             memset(s->coefs[0], 0, sizeof(float) * s->block_len);
             s->channel_coded[0] = 1;
         }
@@ -1111,7 +1079,7 @@ static int wma_decode_block(WMADecodeContext *s)
     
     for(ch = 0; ch < s->nb_channels; ch++) {
         if (s->channel_coded[ch]) {
-            FFTSample output[BLOCK_MAX_SIZE * 2];
+            FFTSample output[BLOCK_MAX_SIZE * 2] __attribute__((aligned(16)));
             float *ptr;
             int i, n4, index, n;
 
@@ -1163,7 +1131,9 @@ static int wma_decode_frame(WMADecodeContext *s, int16_t *samples)
     int16_t *ptr;
     float *iptr;
 
-    trace("***decode_frame: %d size=%d\n", frame_count++, s->frame_len);
+#ifdef TRACE
+    tprintf("***decode_frame: %d size=%d\n", s->frame_count++, s->frame_len);
+#endif
 
     /* read each block */
     s->block_num = 0;
@@ -1184,7 +1154,7 @@ static int wma_decode_frame(WMADecodeContext *s, int16_t *samples)
         iptr = s->frame_out[ch];
 
         for(i=0;i<n;i++) {
-            a = lrint(*iptr++);
+            a = lrintf(*iptr++);
             if (a > 32767)
                 a = 32767;
             else if (a < -32768)
@@ -1200,7 +1170,7 @@ static int wma_decode_frame(WMADecodeContext *s, int16_t *samples)
                s->frame_len * sizeof(float));
     }
 
-#ifdef DEBUG_TRACE
+#ifdef TRACE
     dump_shorts("samples", samples, n * s->nb_channels);
 #endif
     return 0;
@@ -1208,18 +1178,23 @@ static int wma_decode_frame(WMADecodeContext *s, int16_t *samples)
 
 static int wma_decode_superframe(AVCodecContext *avctx, 
                                  void *data, int *data_size,
-                                 UINT8 *buf, int buf_size)
+                                 uint8_t *buf, int buf_size)
 {
     WMADecodeContext *s = avctx->priv_data;
     int nb_frames, bit_offset, i, pos, len;
     uint8_t *q;
     int16_t *samples;
     
-    trace("***decode_superframe:\n");
+    tprintf("***decode_superframe:\n");
 
+    if(buf_size==0){
+        s->last_superframe_len = 0;
+        return 0;
+    }
+    
     samples = data;
 
-    init_get_bits(&s->gb, buf, buf_size);
+    init_get_bits(&s->gb, buf, buf_size*8);
     
     if (s->use_bit_reservoir) {
         /* read super frame header */
@@ -1233,7 +1208,7 @@ static int wma_decode_superframe(AVCodecContext *avctx,
             /* add bit_offset bits to last frame */
             if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) > 
                 MAX_CODED_SUPERFRAME_SIZE)
-                return -1;
+                goto fail;
             q = s->last_superframe + s->last_superframe_len;
             len = bit_offset;
             while (len > 0) {
@@ -1245,20 +1220,20 @@ static int wma_decode_superframe(AVCodecContext *avctx,
             }
             
             /* XXX: bit_offset bits into last frame */
-            init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE);
+            init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE*8);
             /* skip unused bits */
             if (s->last_bitoffset > 0)
                 skip_bits(&s->gb, s->last_bitoffset);
             /* this frame is stored in the last superframe and in the
                current one */
             if (wma_decode_frame(s, samples) < 0)
-                return -1;
+                goto fail;
             samples += s->nb_channels * s->frame_len;
         }
 
         /* read each frame starting from bit_offset */
         pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
-        init_get_bits(&s->gb, buf + (pos >> 3), MAX_CODED_SUPERFRAME_SIZE - (pos >> 3));
+        init_get_bits(&s->gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3))*8);
         len = pos & 7;
         if (len > 0)
             skip_bits(&s->gb, len);
@@ -1266,7 +1241,7 @@ static int wma_decode_superframe(AVCodecContext *avctx,
         s->reset_block_lengths = 1;
         for(i=0;i<nb_frames;i++) {
             if (wma_decode_frame(s, samples) < 0)
-                return -1;
+                goto fail;
             samples += s->nb_channels * s->frame_len;
         }
 
@@ -1275,19 +1250,23 @@ static int wma_decode_superframe(AVCodecContext *avctx,
         s->last_bitoffset = pos & 7;
         pos >>= 3;
         len = buf_size - pos;
-        if (len > MAX_CODED_SUPERFRAME_SIZE) {
-            return -1;
+        if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) {
+            goto fail;
         }
         s->last_superframe_len = len;
         memcpy(s->last_superframe, buf + pos, len);
     } else {
         /* single frame decode */
         if (wma_decode_frame(s, samples) < 0)
-            return -1;
+            goto fail;
         samples += s->nb_channels * s->frame_len;
     }
     *data_size = (int8_t *)samples - (int8_t *)data;
     return s->block_align;
+ fail:
+    /* when error, we reset the bit reservoir */
+    s->last_superframe_len = 0;
+    return -1;
 }
 
 static int wma_decode_end(AVCodecContext *avctx)