wma: More trivial whitespace cleanups.
[paraslash.git] / wmadec_filter.c
index 35e2703c1f796c390ce8386d764adb4a9395bff5..7479e84c6e9aab2d296ad9b314bf5abff8465e8c 100644 (file)
@@ -24,6 +24,7 @@
 #include <math.h>
 #include <string.h>
 #include <regex.h>
+#include <sys/select.h>
 
 #include "para.h"
 #include "error.h"
 struct private_wmadec_data {
        struct asf_header_info ahi;
        struct getbit_context gb;
+       /** Whether to use the bit reservoir. */
        int use_bit_reservoir;
+       /** Whether to use variable block length. */
        int use_variable_block_len;
-       int use_exp_vlc;        ///< exponent coding: 0 = lsp, 1 = vlc + delta
-       int use_noise_coding;   ///< true if perceptual noise is added
+       /** Whether to use exponent coding. */
+       int use_exp_vlc;
+       /** Whether perceptual noise is added. */
+       int use_noise_coding;
        int byte_offset_bits;
        struct vlc exp_vlc;
        int exponent_sizes[BLOCK_NB_SIZES];
        uint16_t exponent_bands[BLOCK_NB_SIZES][25];
-       int high_band_start[BLOCK_NB_SIZES];    ///< index of first coef in high band
-       int coefs_start;        ///< first coded coef
+       /** The index of the first coef in high band. */
+       int high_band_start[BLOCK_NB_SIZES];
+       int coefs_start;///< first coded coef
        int coefs_end[BLOCK_NB_SIZES];  ///< max number of coded coefficients
        int exponent_high_sizes[BLOCK_NB_SIZES];
        int exponent_high_bands[BLOCK_NB_SIZES][HIGH_BAND_MAX_SIZE];
@@ -130,16 +136,17 @@ struct private_wmadec_data {
 #define VLCBITS 9
 #define VLCMAX ((22 + VLCBITS - 1) / VLCBITS)
 
-DECLARE_ALIGNED(16, float, ff_sine_128[128]);
-DECLARE_ALIGNED(16, float, ff_sine_256[256]);
-DECLARE_ALIGNED(16, float, ff_sine_512[512]);
-DECLARE_ALIGNED(16, float, ff_sine_1024[1024]);
-DECLARE_ALIGNED(16, float, ff_sine_2048[2048]);
-DECLARE_ALIGNED(16, float, ff_sine_4096[4096]);
+#define SINE_WINDOW(x) float sine_ ## x[x] __aligned(16)
 
-static float *ff_sine_windows[6] = {
-       ff_sine_128, ff_sine_256, ff_sine_512, ff_sine_1024,
-       ff_sine_2048, ff_sine_4096
+SINE_WINDOW(128);
+SINE_WINDOW(256);
+SINE_WINDOW(512);
+SINE_WINDOW(1024);
+SINE_WINDOW(2048);
+SINE_WINDOW(4096);
+
+static float *sine_windows[6] = {
+       sine_128, sine_256, sine_512, sine_1024, sine_2048, sine_4096
 };
 
 /* Generate a sine window. */
@@ -382,8 +389,8 @@ static int wma_init(struct private_wmadec_data *pwd)
        for (i = 0; i < pwd->nb_block_sizes; i++) {
                int n;
                n = 1 << (pwd->frame_len_bits - i);
-               sine_window_init(ff_sine_windows[pwd->frame_len_bits - i - 7], n);
-               pwd->windows[i] = ff_sine_windows[pwd->frame_len_bits - i - 7];
+               sine_window_init(sine_windows[pwd->frame_len_bits - i - 7], n);
+               pwd->windows[i] = sine_windows[pwd->frame_len_bits - i - 7];
        }
 
        pwd->reset_block_lengths = 1;
@@ -480,15 +487,15 @@ static int wma_decode_init(char *initial_buf, int len, struct private_wmadec_dat
        if (pwd->use_noise_coding) {
                PARA_INFO_LOG("using noise coding\n");
                init_vlc(&pwd->hgain_vlc, HGAINVLCBITS,
-                       sizeof(ff_wma_hgain_huffbits), ff_wma_hgain_huffbits,
-                       ff_wma_hgain_huffcodes, 2);
+                       sizeof(wma_hgain_huffbits), wma_hgain_huffbits,
+                       wma_hgain_huffcodes, 2);
        }
 
        if (pwd->use_exp_vlc) {
                PARA_INFO_LOG("using exp_vlc\n");
                init_vlc(&pwd->exp_vlc, EXPVLCBITS,
-               sizeof(ff_wma_scale_huffbits), ff_wma_scale_huffbits,
-               ff_wma_scale_huffcodes, 4);
+               sizeof(wma_scale_huffbits), wma_scale_huffbits,
+               wma_scale_huffcodes, 4);
        } else {
                PARA_INFO_LOG("using curve\n");
                wma_lsp_to_curve_init(pwd, pwd->frame_len);
@@ -559,7 +566,7 @@ static void decode_exp_lsp(struct private_wmadec_data *pwd, int ch)
                        val = get_bits(&pwd->gb, 3);
                else
                        val = get_bits(&pwd->gb, 4);
-               lsp_coefs[i] = ff_wma_lsp_codebook[i][val];
+               lsp_coefs[i] = wma_lsp_codebook[i][val];
        }
 
        wma_lsp_to_curve(pwd, pwd->exponents[ch], &pwd->max_exponent[ch],
@@ -676,6 +683,162 @@ static int wma_total_gain_to_bits(int total_gain)
                return 9;
 }
 
+static int compute_high_band_values(struct private_wmadec_data *pwd,
+               int bsize, int nb_coefs[MAX_CHANNELS])
+{
+       int ch;
+
+       if (!pwd->use_noise_coding)
+               return 0;
+       for (ch = 0; ch < pwd->ahi.channels; ch++) {
+               int i, m, a;
+               if (!pwd->channel_coded[ch])
+                       continue;
+               m = pwd->exponent_high_sizes[bsize];
+               for (i = 0; i < m; i++) {
+                       a = get_bit(&pwd->gb);
+                       pwd->high_band_coded[ch][i] = a;
+                       if (!a)
+                               continue;
+                       nb_coefs[ch] -= pwd->exponent_high_bands[bsize][i];
+               }
+       }
+       for (ch = 0; ch < pwd->ahi.channels; ch++) {
+               int i, n, val;
+               if (!pwd->channel_coded[ch])
+                       continue;
+               n = pwd->exponent_high_sizes[bsize];
+               val = (int)0x80000000;
+               for (i = 0; i < n; i++) {
+                       if (!pwd->high_band_coded[ch][i])
+                               continue;
+                       if (val == (int)0x80000000)
+                               val = get_bits(&pwd->gb, 7) - 19;
+                       else {
+                               int code = get_vlc(&pwd->gb,
+                                       pwd->hgain_vlc.table, HGAINVLCBITS,
+                                       HGAINMAX);
+                               if (code < 0)
+                                       return -1;
+                               val += code - 18;
+                       }
+                       pwd->high_band_values[ch][i] = val;
+               }
+       }
+       return 1;
+}
+
+static void compute_mdct_coefficients(struct private_wmadec_data *pwd,
+               int bsize, int total_gain, int nb_coefs[MAX_CHANNELS])
+{
+       int ch;
+       float mdct_norm = 1.0 / (pwd->block_len / 2);
+
+       for (ch = 0; ch < pwd->ahi.channels; ch++) {
+               int16_t *coefs1;
+               float *coefs, *exponents, mult, mult1, noise;
+               int i, j, n, n1, last_high_band, esize;
+               float exp_power[HIGH_BAND_MAX_SIZE];
+
+               if (!pwd->channel_coded[ch])
+                       continue;
+               coefs1 = pwd->coefs1[ch];
+               exponents = pwd->exponents[ch];
+               esize = pwd->exponents_bsize[ch];
+               mult = pow(10, total_gain * 0.05) / pwd->max_exponent[ch];
+               mult *= mdct_norm;
+               coefs = pwd->coefs[ch];
+               if (!pwd->use_noise_coding) {
+                       /* XXX: optimize more */
+                       for (i = 0; i < pwd->coefs_start; i++)
+                               *coefs++ = 0.0;
+                       n = nb_coefs[ch];
+                       for (i = 0; i < n; i++)
+                               *coefs++ = coefs1[i] *
+                                       exponents[i << bsize >> esize] * mult;
+                       n = pwd->block_len - pwd->coefs_end[bsize];
+                       for (i = 0; i < n; i++)
+                               *coefs++ = 0.0;
+                       continue;
+               }
+               mult1 = mult;
+               /* very low freqs: noise */
+               for (i = 0; i < pwd->coefs_start; i++) {
+                       *coefs++ = pwd->noise_table[pwd->noise_index] *
+                               exponents[i << bsize >> esize] * mult1;
+                       pwd->noise_index = (pwd->noise_index + 1) &
+                               (NOISE_TAB_SIZE - 1);
+               }
+               n1 = pwd->exponent_high_sizes[bsize];
+               /* compute power of high bands */
+               exponents = pwd->exponents[ch] +
+                       (pwd->high_band_start[bsize] << bsize);
+               last_high_band = 0; /* avoid warning */
+               for (j = 0; j < n1; j++) {
+                       n = pwd->exponent_high_bands[
+                               pwd->frame_len_bits - pwd->block_len_bits][j];
+                       if (pwd->high_band_coded[ch][j]) {
+                               float e2, val;
+                               e2 = 0;
+                               for (i = 0; i < n; i++) {
+                                       val = exponents[i << bsize >> esize];
+                                       e2 += val * val;
+                               }
+                               exp_power[j] = e2 / n;
+                               last_high_band = j;
+                       }
+                       exponents += n << bsize;
+               }
+               /* main freqs and high freqs */
+               exponents = pwd->exponents[ch] + (pwd->coefs_start << bsize);
+               for (j = -1; j < n1; j++) {
+                       if (j < 0)
+                               n = pwd->high_band_start[bsize]
+                                       - pwd->coefs_start;
+                       else
+                               n = pwd->exponent_high_bands[pwd->frame_len_bits
+                                       - pwd->block_len_bits][j];
+                       if (j >= 0 && pwd->high_band_coded[ch][j]) {
+                               /* use noise with specified power */
+                               mult1 = sqrt(exp_power[j]
+                                       / exp_power[last_high_band]);
+                               /* XXX: use a table */
+                               mult1 = mult1 * pow(10,
+                                       pwd->high_band_values[ch][j] * 0.05);
+                               mult1 /= (pwd->max_exponent[ch] * pwd->noise_mult);
+                               mult1 *= mdct_norm;
+                               for (i = 0; i < n; i++) {
+                                       noise = pwd->noise_table[pwd->noise_index];
+                                       pwd->noise_index = (pwd->noise_index + 1)
+                                               & (NOISE_TAB_SIZE - 1);
+                                       *coefs++ = noise * exponents[
+                                               i << bsize >> esize] * mult1;
+                               }
+                               exponents += n << bsize;
+                       } else {
+                               /* coded values + small noise */
+                               for (i = 0; i < n; i++) {
+                                       noise = pwd->noise_table[pwd->noise_index];
+                                       pwd->noise_index = (pwd->noise_index + 1)
+                                               & (NOISE_TAB_SIZE - 1);
+                                       *coefs++ = ((*coefs1++) + noise) *
+                                               exponents[i << bsize >> esize]
+                                               * mult;
+                               }
+                               exponents += n << bsize;
+                       }
+               }
+               /* very high freqs: noise */
+               n = pwd->block_len - pwd->coefs_end[bsize];
+               mult1 = mult * exponents[((-1 << bsize)) >> esize];
+               for (i = 0; i < n; i++) {
+                       *coefs++ = pwd->noise_table[pwd->noise_index] * mult1;
+                       pwd->noise_index = (pwd->noise_index + 1)
+                               & (NOISE_TAB_SIZE - 1);
+               }
+       }
+}
+
 /**
  * @return 0 if OK. 1 if last block of frame. return -1 if
  * unrecorrable error.
@@ -685,7 +848,6 @@ static int wma_decode_block(struct private_wmadec_data *pwd)
        int n, v, ch, code, bsize;
        int coef_nb_bits, total_gain;
        int nb_coefs[MAX_CHANNELS];
-       float mdct_norm;
 
        /* compute current block length */
        if (pwd->use_variable_block_len) {
@@ -738,8 +900,10 @@ static int wma_decode_block(struct private_wmadec_data *pwd)
        if (!v)
                goto next;
 
-       /* read total gain and extract corresponding number of bits for
-          coef escape coding */
+       /*
+        * Read total gain and extract corresponding number of bits for coef
+        * escape coding.
+        */
        total_gain = 1;
        for (;;) {
                int a = get_bits(&pwd->gb, 7);
@@ -755,55 +919,8 @@ static int wma_decode_block(struct private_wmadec_data *pwd)
        for (ch = 0; ch < pwd->ahi.channels; ch++)
                nb_coefs[ch] = n;
 
-       /* complex coding */
-       if (pwd->use_noise_coding) {
-               for (ch = 0; ch < pwd->ahi.channels; ch++) {
-                       if (pwd->channel_coded[ch]) {
-                               int i, m, a;
-                               m = pwd->exponent_high_sizes[bsize];
-                               for (i = 0; i < m; i++) {
-                                       a = get_bit(&pwd->gb);
-                                       pwd->high_band_coded[ch][i] = a;
-                                       /* if noise coding, the coefficients are not transmitted */
-                                       if (a)
-                                               nb_coefs[ch] -=
-                                                   pwd->
-                                                   exponent_high_bands[bsize]
-                                                   [i];
-                               }
-                       }
-               }
-               for (ch = 0; ch < pwd->ahi.channels; ch++) {
-                       if (pwd->channel_coded[ch]) {
-                               int i, val;
-
-                               n = pwd->exponent_high_sizes[bsize];
-                               val = (int) 0x80000000;
-                               for (i = 0; i < n; i++) {
-                                       if (pwd->high_band_coded[ch][i]) {
-                                               if (val == (int) 0x80000000) {
-                                                       val =
-                                                           get_bits(&pwd->gb,
-                                                                    7) - 19;
-                                               } else {
-                                                       code =
-                                                           get_vlc(&pwd->gb,
-                                                                    pwd->
-                                                                    hgain_vlc.
-                                                                    table,
-                                                                    HGAINVLCBITS,
-                                                                    HGAINMAX);
-                                                       if (code < 0)
-                                                               return -1;
-                                                       val += code - 18;
-                                               }
-                                               pwd->high_band_values[ch][i] =
-                                                   val;
-                                       }
-                               }
-                       }
-               }
-       }
+       if (compute_high_band_values(pwd, bsize, nb_coefs) < 0)
+               return -1;
 
        /* exponents can be reused in short blocks. */
        if ((pwd->block_len_bits == pwd->frame_len_bits) || get_bit(&pwd->gb)) {
@@ -868,172 +985,10 @@ static int wma_decode_block(struct private_wmadec_data *pwd)
                                break;
                }
        }
-
-       /* normalize */
-       {
-               int n4 = pwd->block_len / 2;
-               mdct_norm = 1.0 / (float) n4;
-       }
-
-       /* finally compute the MDCT coefficients */
-       for (ch = 0; ch < pwd->ahi.channels; ch++) {
-               if (pwd->channel_coded[ch]) {
-                       int16_t *coefs1;
-                       float *coefs, *exponents, mult, mult1, noise;
-                       int i, j, n1, last_high_band, esize;
-                       float exp_power[HIGH_BAND_MAX_SIZE];
-
-                       coefs1 = pwd->coefs1[ch];
-                       exponents = pwd->exponents[ch];
-                       esize = pwd->exponents_bsize[ch];
-                       mult = pow(10, total_gain * 0.05) / pwd->max_exponent[ch];
-                       mult *= mdct_norm;
-                       coefs = pwd->coefs[ch];
-                       if (pwd->use_noise_coding) {
-                               mult1 = mult;
-                               /* very low freqs : noise */
-                               for (i = 0; i < pwd->coefs_start; i++) {
-                                       *coefs++ =
-                                           pwd->noise_table[pwd->noise_index] *
-                                           exponents[i << bsize >> esize] *
-                                           mult1;
-                                       pwd->noise_index =
-                                           (pwd->noise_index +
-                                            1) & (NOISE_TAB_SIZE - 1);
-                               }
-
-                               n1 = pwd->exponent_high_sizes[bsize];
-
-                               /* compute power of high bands */
-                               exponents = pwd->exponents[ch] +
-                                   (pwd->high_band_start[bsize] << bsize);
-                               last_high_band = 0;     /* avoid warning */
-                               for (j = 0; j < n1; j++) {
-                                       n = pwd->exponent_high_bands[pwd->
-                                                                  frame_len_bits
-                                                                  -
-                                                                  pwd->
-                                                                  block_len_bits]
-                                           [j];
-                                       if (pwd->high_band_coded[ch][j]) {
-                                               float e2, val;
-                                               e2 = 0;
-                                               for (i = 0; i < n; i++) {
-                                                       val = exponents[i << bsize
-                                                                     >> esize];
-                                                       e2 += val * val;
-                                               }
-                                               exp_power[j] = e2 / n;
-                                               last_high_band = j;
-                                       }
-                                       exponents += n << bsize;
-                               }
-
-                               /* main freqs and high freqs */
-                               exponents =
-                                   pwd->exponents[ch] +
-                                   (pwd->coefs_start << bsize);
-                               for (j = -1; j < n1; j++) {
-                                       if (j < 0) {
-                                               n = pwd->high_band_start[bsize] -
-                                                   pwd->coefs_start;
-                                       } else {
-                                               n = pwd->exponent_high_bands[pwd->
-                                                                          frame_len_bits
-                                                                          -
-                                                                          pwd->
-                                                                          block_len_bits]
-                                                   [j];
-                                       }
-                                       if (j >= 0 && pwd->high_band_coded[ch][j]) {
-                                               /* use noise with specified power */
-                                               mult1 =
-                                                   sqrt(exp_power[j] /
-                                                        exp_power
-                                                        [last_high_band]);
-                                               /* XXX: use a table */
-                                               mult1 =
-                                                   mult1 * pow(10,
-                                                               pwd->
-                                                               high_band_values
-                                                               [ch][j] * 0.05);
-                                               mult1 =
-                                                   mult1 /
-                                                   (pwd->max_exponent[ch] *
-                                                    pwd->noise_mult);
-                                               mult1 *= mdct_norm;
-                                               for (i = 0; i < n; i++) {
-                                                       noise =
-                                                           pwd->noise_table[pwd->
-                                                                          noise_index];
-                                                       pwd->noise_index =
-                                                           (pwd->noise_index +
-                                                            1) &
-                                                           (NOISE_TAB_SIZE -
-                                                            1);
-                                                       *coefs++ =
-                                                           noise *
-                                                           exponents[i << bsize
-                                                                     >> esize]
-                                                           * mult1;
-                                               }
-                                               exponents += n << bsize;
-                                       } else {
-                                               /* coded values + small noise */
-                                               for (i = 0; i < n; i++) {
-                                                       noise =
-                                                           pwd->noise_table[pwd->
-                                                                          noise_index];
-                                                       pwd->noise_index =
-                                                           (pwd->noise_index +
-                                                            1) &
-                                                           (NOISE_TAB_SIZE -
-                                                            1);
-                                                       *coefs++ =
-                                                           ((*coefs1++) +
-                                                            noise) *
-                                                           exponents[i << bsize
-                                                                     >> esize]
-                                                           * mult;
-                                               }
-                                               exponents += n << bsize;
-                                       }
-                               }
-
-                               /* very high freqs : noise */
-                               n = pwd->block_len - pwd->coefs_end[bsize];
-                               mult1 =
-                                   mult * exponents[((-1 << bsize)) >> esize];
-                               for (i = 0; i < n; i++) {
-                                       *coefs++ =
-                                           pwd->noise_table[pwd->noise_index] *
-                                           mult1;
-                                       pwd->noise_index =
-                                           (pwd->noise_index +
-                                            1) & (NOISE_TAB_SIZE - 1);
-                               }
-                       } else {
-                               /* XXX: optimize more */
-                               for (i = 0; i < pwd->coefs_start; i++)
-                                       *coefs++ = 0.0;
-                               n = nb_coefs[ch];
-                               for (i = 0; i < n; i++) {
-                                       *coefs++ =
-                                           coefs1[i] *
-                                           exponents[i << bsize >> esize] *
-                                           mult;
-                               }
-                               n = pwd->block_len - pwd->coefs_end[bsize];
-                               for (i = 0; i < n; i++)
-                                       *coefs++ = 0.0;
-                       }
-               }
-       }
-
+       compute_mdct_coefficients(pwd, bsize, total_gain, nb_coefs);
        if (pwd->ms_stereo && pwd->channel_coded[1]) {
                float a, b;
                int i;
-
                /*
                 * Nominal case for ms stereo: we do it before mdct.
                 *
@@ -1052,7 +1007,6 @@ static int wma_decode_block(struct private_wmadec_data *pwd)
                        pwd->coefs[1][i] = a - b;
                }
        }
-
 next:
        for (ch = 0; ch < pwd->ahi.channels; ch++) {
                int n4, index;
@@ -1231,7 +1185,7 @@ static int wma_decode_superframe(struct private_wmadec_data *pwd, void *data,
                samples += pwd->ahi.channels * pwd->frame_len;
        }
        PARA_DEBUG_LOG("frame_count: %d frame_len: %d, block_len: %d, "
-               "outbytes: %d, eaten: %d\n",
+               "outbytes: %zd, eaten: %d\n",
                frame_count, pwd->frame_len, pwd->block_len,
                (int8_t *) samples - (int8_t *) data, pwd->ahi.block_align);
        *data_size = (int8_t *)samples - (int8_t *)data;