]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/wmadec.c
Do not redundantly check for both CONFIG_THEORA_DECODER and CONFIG_VP3_DECODER.
[ffmpeg] / libavcodec / wmadec.c
index ef3cc7a3307c10b7afe957974aab200baefd5ff9..7a65003457542f725be7e49e263ca53652bed091 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * WMA compatible decoder
- * Copyright (c) 2002 The FFmpeg Project.
+ * Copyright (c) 2002 The FFmpeg Project
  *
  * This file is part of FFmpeg.
  *
@@ -20,7 +20,7 @@
  */
 
 /**
- * @file wmadec.c
+ * @file libavcodec/wmadec.c
  * WMA compatible decoder.
  * This decoder handles Microsoft Windows Media Audio data, versions 1 & 2.
  * WMA v1 is identified by audio format 0x160 in Microsoft media files
@@ -48,7 +48,7 @@
 static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len);
 
 #ifdef TRACE
-static void dump_shorts(WMADecodeContext *s, const char *name, const short *tab, int n)
+static void dump_shorts(WMACodecContext *s, const char *name, const short *tab, int n)
 {
     int i;
 
@@ -62,7 +62,7 @@ static void dump_shorts(WMADecodeContext *s, const char *name, const short *tab,
     }
 }
 
-static void dump_floats(WMADecodeContext *s, const char *name, int prec, const float *tab, int n)
+static void dump_floats(WMACodecContext *s, const char *name, int prec, const float *tab, int n)
 {
     int i;
 
@@ -92,12 +92,11 @@ static int wma_decode_init(AVCodecContext * avctx)
     flags2 = 0;
     extradata = avctx->extradata;
     if (avctx->codec->id == CODEC_ID_WMAV1 && avctx->extradata_size >= 4) {
-        flags1 = extradata[0] | (extradata[1] << 8);
-        flags2 = extradata[2] | (extradata[3] << 8);
+        flags1 = AV_RL16(extradata);
+        flags2 = AV_RL16(extradata+2);
     } else if (avctx->codec->id == CODEC_ID_WMAV2 && avctx->extradata_size >= 6) {
-        flags1 = extradata[0] | (extradata[1] << 8) |
-            (extradata[2] << 16) | (extradata[3] << 24);
-        flags2 = extradata[4] | (extradata[5] << 8);
+        flags1 = AV_RL32(extradata);
+        flags2 = AV_RL16(extradata+4);
     }
 // for(i=0; i<avctx->extradata_size; i++)
 //     av_log(NULL, AV_LOG_ERROR, "%02X ", extradata[i]);
@@ -106,11 +105,12 @@ static int wma_decode_init(AVCodecContext * avctx)
     s->use_bit_reservoir = flags2 & 0x0002;
     s->use_variable_block_len = flags2 & 0x0004;
 
-    ff_wma_init(avctx, flags2);
+    if(ff_wma_init(avctx, flags2)<0)
+        return -1;
 
     /* init MDCT */
     for(i = 0; i < s->nb_block_sizes; i++)
-        ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1);
+        ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1, 1.0);
 
     if (s->use_noise_coding) {
         init_vlc(&s->hgain_vlc, HGAINVLCBITS, sizeof(ff_wma_hgain_huffbits),
@@ -126,38 +126,10 @@ static int wma_decode_init(AVCodecContext * avctx)
         wma_lsp_to_curve_init(s, s->frame_len);
     }
 
+    avctx->sample_fmt = SAMPLE_FMT_S16;
     return 0;
 }
 
-/**
- * interpolate values for a bigger or smaller block. The block must
- * have multiple sizes
- */
-static void interpolate_array(float *scale, int old_size, int new_size)
-{
-    int i, j, jincr, k;
-    float v;
-
-    if (new_size > old_size) {
-        jincr = new_size / old_size;
-        j = new_size;
-        for(i = old_size - 1; i >=0; i--) {
-            v = scale[i];
-            k = jincr;
-            do {
-                scale[--j] = v;
-            } while (--k);
-        }
-    } else if (new_size < old_size) {
-        j = 0;
-        jincr = old_size / new_size;
-        for(i = 0; i < new_size; i++) {
-            scale[i] = scale[j];
-            j += jincr;
-        }
-    }
-}
-
 /**
  * compute x^-0.25 with an exponent and mantissa table. We use linear
  * interpolation to reduce the mantissa table size at a small speed
@@ -377,8 +349,8 @@ static void wma_window(WMACodecContext *s, float *out)
  */
 static int wma_decode_block(WMACodecContext *s)
 {
-    int n, v, a, ch, code, bsize;
-    int coef_nb_bits, total_gain, parse_exponents;
+    int n, v, a, ch, bsize;
+    int coef_nb_bits, total_gain;
     int nb_coefs[MAX_CHANNELS];
     float mdct_norm;
 
@@ -422,21 +394,22 @@ static int wma_decode_block(WMACodecContext *s)
         return -1;
 
     if (s->nb_channels == 2) {
-        s->ms_stereo = get_bits(&s->gb, 1);
+        s->ms_stereo = get_bits1(&s->gb);
     }
     v = 0;
     for(ch = 0; ch < s->nb_channels; ch++) {
-        a = get_bits(&s->gb, 1);
+        a = get_bits1(&s->gb);
         s->channel_coded[ch] = a;
         v |= a;
     }
+
+    bsize = s->frame_len_bits - s->block_len_bits;
+
     /* if no channel coded, no need to go further */
     /* XXX: fix potential framing problems */
     if (!v)
         goto next;
 
-    bsize = s->frame_len_bits - s->block_len_bits;
-
     /* read total gain and extract corresponding number of bits for
        coef escape coding */
     total_gain = 1;
@@ -462,7 +435,7 @@ static int wma_decode_block(WMACodecContext *s)
                 int i, n, a;
                 n = s->exponent_high_sizes[bsize];
                 for(i=0;i<n;i++) {
-                    a = get_bits(&s->gb, 1);
+                    a = get_bits1(&s->gb);
                     s->high_band_coded[ch][i] = a;
                     /* if noise coding, the coefficients are not transmitted */
                     if (a)
@@ -493,13 +466,9 @@ static int wma_decode_block(WMACodecContext *s)
         }
     }
 
-    /* exposant can be interpolated in short blocks. */
-    parse_exponents = 1;
-    if (s->block_len_bits != s->frame_len_bits) {
-        parse_exponents = get_bits(&s->gb, 1);
-    }
-
-    if (parse_exponents) {
+    /* exponents can be reused in short blocks. */
+    if ((s->block_len_bits == s->frame_len_bits) ||
+        get_bits1(&s->gb)) {
         for(ch = 0; ch < s->nb_channels; ch++) {
             if (s->channel_coded[ch]) {
                 if (s->use_exp_vlc) {
@@ -508,13 +477,7 @@ static int wma_decode_block(WMACodecContext *s)
                 } else {
                     decode_exp_lsp(s, ch);
                 }
-            }
-        }
-    } else {
-        for(ch = 0; ch < s->nb_channels; ch++) {
-            if (s->channel_coded[ch]) {
-                interpolate_array(s->exponents[ch], 1 << s->prev_block_len_bits,
-                                  s->block_len);
+                s->exponents_bsize[ch] = bsize;
             }
         }
     }
@@ -522,53 +485,17 @@ static int wma_decode_block(WMACodecContext *s)
     /* parse spectral coefficients : just RLE encoding */
     for(ch = 0; ch < s->nb_channels; ch++) {
         if (s->channel_coded[ch]) {
-            VLC *coef_vlc;
-            int level, run, sign, tindex;
-            int16_t *ptr, *eptr;
-            const uint16_t *level_table, *run_table;
+            int tindex;
+            WMACoef* ptr = &s->coefs1[ch][0];
 
             /* special VLC tables are used for ms stereo because
                there is potentially less energy there */
             tindex = (ch == 1 && s->ms_stereo);
-            coef_vlc = &s->coef_vlc[tindex];
-            run_table = s->run_table[tindex];
-            level_table = s->level_table[tindex];
-            /* XXX: optimize */
-            ptr = &s->coefs1[ch][0];
-            eptr = ptr + nb_coefs[ch];
-            memset(ptr, 0, s->block_len * sizeof(int16_t));
-            for(;;) {
-                code = get_vlc2(&s->gb, coef_vlc->table, VLCBITS, VLCMAX);
-                if (code < 0)
-                    return -1;
-                if (code == 1) {
-                    /* EOB */
-                    break;
-                } else if (code == 0) {
-                    /* escape */
-                    level = get_bits(&s->gb, coef_nb_bits);
-                    /* NOTE: this is rather suboptimal. reading
-                       block_len_bits would be better */
-                    run = get_bits(&s->gb, s->frame_len_bits);
-                } else {
-                    /* normal code */
-                    run = run_table[code];
-                    level = level_table[code];
-                }
-                sign = get_bits(&s->gb, 1);
-                if (!sign)
-                    level = -level;
-                ptr += run;
-                if (ptr >= eptr)
-                {
-                    av_log(NULL, AV_LOG_ERROR, "overflow in spectral RLE, ignoring\n");
-                    break;
-                }
-                *ptr++ = level;
-                /* NOTE: EOB can be omitted */
-                if (ptr >= eptr)
-                    break;
-            }
+            memset(ptr, 0, s->block_len * sizeof(WMACoef));
+            ff_wma_run_level_decode(s->avctx, &s->gb, &s->coef_vlc[tindex],
+                  s->level_table[tindex], s->run_table[tindex],
+                  0, ptr, 0, nb_coefs[ch],
+                  s->block_len, s->frame_len_bits, coef_nb_bits);
         }
         if (s->version == 1 && s->nb_channels >= 2) {
             align_get_bits(&s->gb);
@@ -587,13 +514,14 @@ static int wma_decode_block(WMACodecContext *s)
     /* finally compute the MDCT coefficients */
     for(ch = 0; ch < s->nb_channels; ch++) {
         if (s->channel_coded[ch]) {
-            int16_t *coefs1;
-            float *coefs, *exponents, mult, mult1, noise, *exp_ptr;
-            int i, j, n, n1, last_high_band;
+            WMACoef *coefs1;
+            float *coefs, *exponents, mult, mult1, noise;
+            int i, j, n, n1, last_high_band, esize;
             float exp_power[HIGH_BAND_MAX_SIZE];
 
             coefs1 = s->coefs1[ch];
             exponents = s->exponents[ch];
+            esize = s->exponents_bsize[ch];
             mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
             mult *= mdct_norm;
             coefs = s->coefs[ch];
@@ -601,16 +529,16 @@ static int wma_decode_block(WMACodecContext *s)
                 mult1 = mult;
                 /* very low freqs : noise */
                 for(i = 0;i < s->coefs_start; i++) {
-                    *coefs++ = s->noise_table[s->noise_index] * (*exponents++) * mult1;
+                    *coefs++ = s->noise_table[s->noise_index] *
+                      exponents[i<<bsize>>esize] * mult1;
                     s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
                 }
 
                 n1 = s->exponent_high_sizes[bsize];
 
                 /* compute power of high bands */
-                exp_ptr = exponents +
-                    s->high_band_start[bsize] -
-                    s->coefs_start;
+                exponents = s->exponents[ch] +
+                    (s->high_band_start[bsize]<<bsize);
                 last_high_band = 0; /* avoid warning */
                 for(j=0;j<n1;j++) {
                     n = s->exponent_high_bands[s->frame_len_bits -
@@ -619,17 +547,18 @@ static int wma_decode_block(WMACodecContext *s)
                         float e2, v;
                         e2 = 0;
                         for(i = 0;i < n; i++) {
-                            v = exp_ptr[i];
+                            v = exponents[i<<bsize>>esize];
                             e2 += v * v;
                         }
                         exp_power[j] = e2 / n;
                         last_high_band = j;
                         tprintf(s->avctx, "%d: power=%f (%d)\n", j, exp_power[j], n);
                     }
-                    exp_ptr += n;
+                    exponents += n<<bsize;
                 }
 
                 /* main freqs and high freqs */
+                exponents = s->exponents[ch] + (s->coefs_start<<bsize);
                 for(j=-1;j<n1;j++) {
                     if (j < 0) {
                         n = s->high_band_start[bsize] -
@@ -648,21 +577,25 @@ static int wma_decode_block(WMACodecContext *s)
                         for(i = 0;i < n; i++) {
                             noise = s->noise_table[s->noise_index];
                             s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
-                            *coefs++ = (*exponents++) * noise * mult1;
+                            *coefs++ =  noise *
+                                exponents[i<<bsize>>esize] * mult1;
                         }
+                        exponents += n<<bsize;
                     } else {
                         /* coded values + small noise */
                         for(i = 0;i < n; i++) {
                             noise = s->noise_table[s->noise_index];
                             s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
-                            *coefs++ = ((*coefs1++) + noise) * (*exponents++) * mult;
+                            *coefs++ = ((*coefs1++) + noise) *
+                                exponents[i<<bsize>>esize] * mult;
                         }
+                        exponents += n<<bsize;
                     }
                 }
 
                 /* very high freqs : noise */
                 n = s->block_len - s->coefs_end[bsize];
-                mult1 = mult * exponents[-1];
+                mult1 = mult * exponents[((-1<<bsize))>>esize];
                 for(i = 0; i < n; i++) {
                     *coefs++ = s->noise_table[s->noise_index] * mult1;
                     s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
@@ -673,7 +606,7 @@ static int wma_decode_block(WMACodecContext *s)
                     *coefs++ = 0.0;
                 n = nb_coefs[ch];
                 for(i = 0;i < n; i++) {
-                    *coefs++ = coefs1[i] * exponents[i] * mult;
+                    *coefs++ = coefs1[i] * exponents[i<<bsize>>esize] * mult;
                 }
                 n = s->block_len - s->coefs_end[bsize];
                 for(i = 0;i < n; i++)
@@ -712,27 +645,21 @@ static int wma_decode_block(WMACodecContext *s)
         }
     }
 
+next:
     for(ch = 0; ch < s->nb_channels; ch++) {
-        if (s->channel_coded[ch]) {
-            int n4, index, n;
-
-            n = s->block_len;
-            n4 = s->block_len / 2;
-            s->mdct_ctx[bsize].fft.imdct_calc(&s->mdct_ctx[bsize],
-                          s->output, s->coefs[ch], s->mdct_tmp);
+        int n4, index;
 
-            /* multiply by the window and add in the frame */
-            index = (s->frame_len / 2) + s->block_pos - n4;
-            wma_window(s, &s->frame_out[ch][index]);
+        n4 = s->block_len / 2;
+        if(s->channel_coded[ch]){
+            ff_imdct_calc(&s->mdct_ctx[bsize], s->output, s->coefs[ch]);
+        }else if(!(s->ms_stereo && ch==1))
+            memset(s->output, 0, sizeof(s->output));
 
-            /* specific fast case for ms-stereo : add to second
-               channel if it is not coded */
-            if (s->ms_stereo && !s->channel_coded[1]) {
-                wma_window(s, &s->frame_out[1][index]);
-            }
-        }
+        /* multiply by the window and add in the frame */
+        index = (s->frame_len / 2) + s->block_pos - n4;
+        wma_window(s, &s->frame_out[ch][index]);
     }
- next:
+
     /* update block number */
     s->block_num++;
     s->block_pos += s->block_len;
@@ -745,7 +672,7 @@ static int wma_decode_block(WMACodecContext *s)
 /* decode a frame of frame_len samples */
 static int wma_decode_frame(WMACodecContext *s, int16_t *samples)
 {
-    int ret, i, n, a, ch, incr;
+    int ret, i, n, ch, incr;
     int16_t *ptr;
     float *iptr;
 
@@ -772,12 +699,7 @@ static int wma_decode_frame(WMACodecContext *s, int16_t *samples)
         iptr = s->frame_out[ch];
 
         for(i=0;i<n;i++) {
-            a = lrintf(*iptr++);
-            if (a > 32767)
-                a = 32767;
-            else if (a < -32768)
-                a = -32768;
-            *ptr = a;
+            *ptr = av_clip_int16(lrintf(*iptr++));
             ptr += incr;
         }
         /* prepare for next block */
@@ -793,8 +715,10 @@ static int wma_decode_frame(WMACodecContext *s, int16_t *samples)
 
 static int wma_decode_superframe(AVCodecContext *avctx,
                                  void *data, int *data_size,
-                                 uint8_t *buf, int buf_size)
+                                 AVPacket *avpkt)
 {
+    const uint8_t *buf = avpkt->data;
+    int buf_size = avpkt->size;
     WMACodecContext *s = avctx->priv_data;
     int nb_frames, bit_offset, i, pos, len;
     uint8_t *q;
@@ -806,6 +730,9 @@ static int wma_decode_superframe(AVCodecContext *avctx,
         s->last_superframe_len = 0;
         return 0;
     }
+    if (buf_size < s->block_align)
+        return 0;
+    buf_size = s->block_align;
 
     samples = data;
 
@@ -813,9 +740,14 @@ static int wma_decode_superframe(AVCodecContext *avctx,
 
     if (s->use_bit_reservoir) {
         /* read super frame header */
-        get_bits(&s->gb, 4); /* super frame index */
+        skip_bits(&s->gb, 4); /* super frame index */
         nb_frames = get_bits(&s->gb, 4) - 1;
 
+        if((nb_frames+1) * s->nb_channels * s->frame_len * sizeof(int16_t) > *data_size){
+            av_log(s->avctx, AV_LOG_ERROR, "Insufficient output space\n");
+            goto fail;
+        }
+
         bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
 
         if (s->last_superframe_len > 0) {
@@ -871,6 +803,10 @@ static int wma_decode_superframe(AVCodecContext *avctx,
         s->last_superframe_len = len;
         memcpy(s->last_superframe, buf + pos, len);
     } else {
+        if(s->nb_channels * s->frame_len * sizeof(int16_t) > *data_size){
+            av_log(s->avctx, AV_LOG_ERROR, "Insufficient output space\n");
+            goto fail;
+        }
         /* single frame decode */
         if (wma_decode_frame(s, samples) < 0)
             goto fail;
@@ -897,6 +833,7 @@ AVCodec wmav1_decoder =
     NULL,
     ff_wma_end,
     wma_decode_superframe,
+    .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 1"),
 };
 
 AVCodec wmav2_decoder =
@@ -909,4 +846,5 @@ AVCodec wmav2_decoder =
     NULL,
     ff_wma_end,
     wma_decode_superframe,
+    .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 2"),
 };