server: Convert com_mvatt() to lopsub.
[paraslash.git] / wmadec_filter.c
index 6b7251e..4c7c047 100644 (file)
@@ -88,8 +88,8 @@ struct private_wmadec_data {
        int frame_len_bits;
        /** Number of block sizes, one if !ahi->use_variable_block_len. */
        int nb_block_sizes;
-       /* block info */
-       int reset_block_lengths;
+       /* Whether to update block lengths from getbit context. */
+       bool reset_block_lengths;
        /** log2 of current block length. */
        int block_len_bits;
        /** log2 of next block length. */
@@ -131,13 +131,8 @@ struct private_wmadec_data {
 };
 
 #define EXPVLCBITS 8
-#define EXPMAX DIV_ROUND_UP(19, EXPVLCBITS)
-
 #define HGAINVLCBITS 9
-#define HGAINMAX DIV_ROUND_UP(13, HGAINVLCBITS)
-
 #define VLCBITS 9
-#define VLCMAX DIV_ROUND_UP(22, VLCBITS)
 
 /** \cond sine_winows */
 
@@ -376,8 +371,8 @@ static int wma_init(struct private_wmadec_data *pwd)
                else
                        high_freq = high_freq * 0.5;
        }
-       PARA_INFO_LOG("channels=%d sample_rate=%d "
-               "bitrate=%d block_align=%d\n",
+       PARA_INFO_LOG("channels=%u sample_rate=%u "
+               "bitrate=%u block_align=%d\n",
                ahi->channels, ahi->sample_rate,
                ahi->bit_rate, ahi->block_align);
        PARA_INFO_LOG("frame_len=%d, bps=%f bps1=%f "
@@ -396,7 +391,7 @@ static int wma_init(struct private_wmadec_data *pwd)
                pwd->windows[i] = sine_windows[pwd->frame_len_bits - i - 7];
        }
 
-       pwd->reset_block_lengths = 1;
+       pwd->reset_block_lengths = true;
 
        if (pwd->use_noise_coding) {
                /* init the noise generator */
@@ -434,13 +429,13 @@ static int wma_init(struct private_wmadec_data *pwd)
        return 0;
 }
 
-static void wma_lsp_to_curve_init(struct private_wmadec_data *pwd, int frame_len)
+static void wma_lsp_to_curve_init(struct private_wmadec_data *pwd)
 {
        float wdel, a, b;
        int i, e, m;
 
-       wdel = M_PI / frame_len;
-       for (i = 0; i < frame_len; i++)
+       wdel = M_PI / pwd->frame_len;
+       for (i = 0; i < pwd->frame_len; i++)
                pwd->lsp_cos_table[i] = 2.0f * cos(wdel * i);
 
        /* tables for x^-0.25 computation */
@@ -496,7 +491,7 @@ static int wma_decode_init(char *initial_buf, int len, struct private_wmadec_dat
                        wma_scale_huffbits, wma_scale_huffcodes, 4);
        } else {
                PARA_INFO_LOG("using curve\n");
-               wma_lsp_to_curve_init(pwd, pwd->frame_len);
+               wma_lsp_to_curve_init(pwd);
        }
        *result = pwd;
        return pwd->ahi.header_len;
@@ -586,7 +581,7 @@ static int decode_exp_vlc(struct private_wmadec_data *pwd, int ch)
        last_exp = 36;
 
        while (q < q_end) {
-               code = get_vlc(&pwd->gb, pwd->exp_vlc.table, EXPVLCBITS, EXPMAX);
+               code = get_vlc(&pwd->gb, pwd->exp_vlc.table, EXPVLCBITS);
                if (code < 0)
                        return code;
                /* NOTE: this offset is the same as MPEG4 AAC ! */
@@ -714,8 +709,7 @@ static int compute_high_band_values(struct private_wmadec_data *pwd,
                                val = get_bits(&pwd->gb, 7) - 19;
                        else {
                                int code = get_vlc(&pwd->gb,
-                                       pwd->hgain_vlc.table, HGAINVLCBITS,
-                                       HGAINMAX);
+                                       pwd->hgain_vlc.table, HGAINVLCBITS);
                                if (code < 0)
                                        return code;
                                val += code - 18;
@@ -816,7 +810,7 @@ static void compute_mdct_coefficients(struct private_wmadec_data *pwd,
                }
                /* very high freqs: noise */
                n = pwd->block_len - pwd->coefs_end[bsize];
-               mult1 = mult * exponents[((-1 << bsize)) >> esize];
+               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)
@@ -840,7 +834,7 @@ static int wma_decode_block(struct private_wmadec_data *pwd)
                n = wma_log2(pwd->nb_block_sizes - 1) + 1;
 
                if (pwd->reset_block_lengths) {
-                       pwd->reset_block_lengths = 0;
+                       pwd->reset_block_lengths = false;
                        v = get_bits(&pwd->gb, n);
                        if (v >= pwd->nb_block_sizes)
                                return -E_WMA_BLOCK_SIZE;
@@ -946,8 +940,7 @@ static int wma_decode_block(struct private_wmadec_data *pwd)
                eptr = ptr + nb_coefs[ch];
                memset(ptr, 0, pwd->block_len * sizeof(int16_t));
                for (;;) {
-                       code = get_vlc(&pwd->gb, coef_vlc->table,
-                               VLCBITS, VLCMAX);
+                       code = get_vlc(&pwd->gb, coef_vlc->table, VLCBITS);
                        if (code < 0)
                                return code;
                        if (code == 1) /* EOB */
@@ -1035,7 +1028,7 @@ static inline int16_t av_clip_int16(int a)
 /* Decode a frame of frame_len samples. */
 static int wma_decode_frame(struct private_wmadec_data *pwd, int16_t *samples)
 {
-       int ret, i, n, ch, incr;
+       int ret, i, ch;
        int16_t *ptr;
        float *iptr;
 
@@ -1050,15 +1043,13 @@ static int wma_decode_frame(struct private_wmadec_data *pwd, int16_t *samples)
        }
 
        /* convert frame to integer */
-       n = pwd->frame_len;
-       incr = pwd->ahi.channels;
        for (ch = 0; ch < pwd->ahi.channels; ch++) {
                ptr = samples + ch;
                iptr = pwd->frame_out[ch];
 
-               for (i = 0; i < n; i++) {
+               for (i = 0; i < pwd->frame_len; i++) {
                        *ptr = av_clip_int16(lrintf(*iptr++));
-                       ptr += incr;
+                       ptr += pwd->ahi.channels;
                }
                /* prepare for next block */
                memmove(&pwd->frame_out[ch][0], &pwd->frame_out[ch][pwd->frame_len],
@@ -1075,10 +1066,13 @@ static int wma_decode_superframe(struct private_wmadec_data *pwd, void *data,
 
        if (buf_size == 0) {
                pwd->last_superframe_len = 0;
+               *data_size = 0;
                return 0;
        }
-       if (buf_size < pwd->ahi.block_align)
+       if (buf_size < pwd->ahi.block_align) {
+               *data_size = 0;
                return 0;
+       }
        buf_size = pwd->ahi.block_align;
        samples = data;
        init_get_bits(&pwd->gb, buf, buf_size);
@@ -1136,7 +1130,7 @@ static int wma_decode_superframe(struct private_wmadec_data *pwd, void *data,
                if (len > 0)
                        skip_bits(&pwd->gb, len);
 
-               pwd->reset_block_lengths = 1;
+               pwd->reset_block_lengths = true;
                for (i = 0; i < nb_frames; i++) {
                        ret = wma_decode_frame(pwd, samples);
                        if (ret < 0)
@@ -1216,7 +1210,7 @@ next_buffer:
        if (ret == 0)
                return 0;
        btr_merge(btrn, fn->min_iqs);
-       len = btr_next_buffer(btrn, (char **)&in);
+       len = btr_next_buffer(btrn, &in);
        ret = -E_WMADEC_EOF;
        if (len < fn->min_iqs)
                goto err;
@@ -1228,12 +1222,12 @@ next_buffer:
                        fn->min_iqs += 4096;
                        goto next_buffer;
                }
-               fn->min_iqs = 2 * (WMA_FRAME_SKIP + pwd->ahi.block_align);
+               fn->min_iqs = 2 * pwd->ahi.packet_size;
                fn->private_data = pwd;
                converted = pwd->ahi.header_len;
                goto success;
        }
-       fn->min_iqs = WMA_FRAME_SKIP + pwd->ahi.block_align;
+       fn->min_iqs = pwd->ahi.packet_size;
        if (fn->min_iqs > len)
                goto success;
        out_size = WMA_OUTPUT_BUFFER_SIZE;
@@ -1244,10 +1238,12 @@ next_buffer:
                free(out);
                goto err;
        }
-       out = para_realloc(out, out_size);
-       if (out_size > 0)
+       if (out_size > 0) {
+               out = para_realloc(out, out_size);
                btr_add_output(out, out_size, btrn);
-       converted += ret + WMA_FRAME_SKIP;
+       } else
+               free(out);
+       converted += pwd->ahi.packet_size;
 success:
        btr_consume(btrn, converted);
        return 0;