]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/wmalosslessdec.c
sgidec: make compiler optimize away memcpy call in inner loop.
[ffmpeg] / libavcodec / wmalosslessdec.c
index 42ff19ffc9645a9eb02bc1144f2a9f29aaf1e01e..d190b2dfc991d440f10c584f6ff9f61885c5ea39 100644 (file)
@@ -139,6 +139,7 @@ typedef struct {
     float*   coeffs;                                  ///< pointer to the subframe decode buffer
     uint16_t num_vec_coeffs;                          ///< number of vector coded coefficients
     DECLARE_ALIGNED(16, float, out)[WMALL_BLOCK_MAX_SIZE + WMALL_BLOCK_MAX_SIZE / 2]; ///< output buffer
+    int      transient_counter;                       ///< number of transient samples from the beginning of transient zone
 } WmallChannelCtx;
 
 /**
@@ -199,8 +200,10 @@ typedef struct WmallDecodeCtx {
     uint32_t         frame_num;                     ///< current frame number (not used for decoding)
     GetBitContext    gb;                            ///< bitstream reader context
     int              buf_bit_size;                  ///< buffer size in bits
-    float*           samples;                       ///< current samplebuffer pointer
-    float*           samples_end;                   ///< maximum samplebuffer pointer
+    int16_t*         samples_16;                    ///< current samplebuffer pointer (16-bit)
+    int16_t*         samples_16_end;                ///< maximum samplebuffer pointer
+    int16_t*         samples_32;                    ///< current samplebuffer pointer (24-bit)
+    int16_t*         samples_32_end;                ///< maximum samplebuffer pointer
     uint8_t          drc_gain;                      ///< gain for the DRC tool
     int8_t           skip_frame;                    ///< skip output step
     int8_t           parsed_all_subframes;          ///< all subframes decoded?
@@ -221,7 +224,7 @@ typedef struct WmallDecodeCtx {
     WmallChannelCtx channel[WMALL_MAX_CHANNELS];    ///< per channel data
 
     // WMA lossless
-    
+
     uint8_t do_arith_coding;
     uint8_t do_ac_filter;
     uint8_t do_inter_ch_decorr;
@@ -230,13 +233,14 @@ typedef struct WmallDecodeCtx {
 
     int8_t acfilter_order;
     int8_t acfilter_scaling;
-    int acfilter_coeffs[16];
+    int64_t acfilter_coeffs[16];
+    int acfilter_prevvalues[2][16];
 
     int8_t mclms_order;
     int8_t mclms_scaling;
     int16_t mclms_coeffs[128];
     int16_t mclms_coeffs_cur[4];
-    int mclms_prevvalues[64];   // FIXME: should be 32-bit / 16-bit depending on bit-depth
+    int16_t mclms_prevvalues[64];   // FIXME: should be 32-bit / 16-bit depending on bit-depth
     int16_t mclms_updates[64];
     int mclms_recent;
 
@@ -244,12 +248,12 @@ typedef struct WmallDecodeCtx {
     int quant_stepsize;
 
     struct {
-       int order;
-       int scaling;
-       int coefsend;
-       int bitsend;
-       int16_t coefs[256];
-    int lms_prevvalues[512];    // FIXME: see above
+        int order;
+        int scaling;
+        int coefsend;
+        int bitsend;
+        int16_t coefs[256];
+    int16_t lms_prevvalues[512];    // FIXME: see above
     int16_t lms_updates[512];   // and here too
     int recent;
     } cdlms[2][9];              /* XXX: Here, 2 is the max. no. of channels allowed,
@@ -278,7 +282,7 @@ typedef struct WmallDecodeCtx {
     int lpc_scaling;
     int lpc_intbits;
 
-    int channel_coeffs[2][2048];
+    int channel_coeffs[2][2048]; // FIXME: should be 32-bit / 16-bit depending on bit-depth
 
 } WmallDecodeCtx;
 
@@ -287,7 +291,9 @@ typedef struct WmallDecodeCtx {
 #define dprintf(pctx, ...) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__)
 
 
-//static int num_logged_tiles;
+static int num_logged_tiles = 0;
+static int num_logged_subframes = 0;
+static int num_lms_update_call = 0;
 
 /**
  *@brief helper function to print the most important members of the context
@@ -307,6 +313,18 @@ static void av_cold dump_context(WmallDecodeCtx *s)
     PRINT("num channels",        s->num_channels);
 }
 
+static void dump_int_buffer(uint8_t *buffer, int size, int length, int delimiter)
+{
+    int i;
+
+    for (i=0 ; i<length ; i++) {
+        if (!(i%delimiter))
+            av_log(0, 0, "\n[%d] ", i);
+        av_log(0, 0, "%d, ", *(int16_t *)(buffer + i * size));
+    }
+    av_log(0, 0, "\n");
+}
+
 /**
  *@brief Uninitialize the decoder and free all resources.
  *@param avctx codec context
@@ -337,17 +355,23 @@ static av_cold int decode_init(AVCodecContext *avctx)
     int log2_max_num_subframes;
     int num_possible_block_sizes;
 
-    //num_logged_tiles = 0;
     s->avctx = avctx;
     dsputil_init(&s->dsp, avctx);
     init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
 
-    avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
-
     if (avctx->extradata_size >= 18) {
         s->decode_flags    = AV_RL16(edata_ptr+14);
         channel_mask       = AV_RL32(edata_ptr+2);
         s->bits_per_sample = AV_RL16(edata_ptr);
+        if (s->bits_per_sample == 16)
+            avctx->sample_fmt = AV_SAMPLE_FMT_S16;
+        else if (s->bits_per_sample == 24)
+            avctx->sample_fmt = AV_SAMPLE_FMT_S32;
+        else {
+            av_log(avctx, AV_LOG_ERROR, "Unknown bit-depth: %d\n",
+                   s->bits_per_sample);
+            return AVERROR_INVALIDDATA;
+        }
         /** dump the extradata */
         for (i = 0; i < avctx->extradata_size; i++)
             dprintf(avctx, "[%x] ", avctx->extradata[i]);
@@ -499,12 +523,12 @@ static int decode_tilehdr(WmallDecodeCtx *s)
         for (c = 0; c < s->num_channels; c++) {
             if (num_samples[c] == min_channel_len) {
                 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
-                   (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe)) {
+                    (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe)) {
                     contains_subframe[c] = 1;
-               }                   
+                }
                 else {
                     contains_subframe[c] = get_bits1(&s->gb);
-               }
+                }
             } else
                 contains_subframe[c] = 0;
         }
@@ -529,7 +553,7 @@ static int decode_tilehdr(WmallDecodeCtx *s)
                 if (num_samples[c] > s->samples_per_frame) {
                     av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
                            "channel len(%d) > samples_per_frame(%d)\n",
-                          num_samples[c], s->samples_per_frame);
+                           num_samples[c], s->samples_per_frame);
                     return AVERROR_INVALIDDATA;
                 }
             } else if (num_samples[c] <= min_channel_len) {
@@ -559,7 +583,7 @@ static int my_log2(unsigned int i)
 {
     unsigned int iLog2 = 0;
     while ((i >> iLog2) > 1)
-       iLog2++;
+        iLog2++;
     return iLog2;
 }
 
@@ -574,7 +598,7 @@ static void decode_ac_filter(WmallDecodeCtx *s)
     s->acfilter_scaling = get_bits(&s->gb, 4);
 
     for(i = 0; i < s->acfilter_order; i++) {
-       s->acfilter_coeffs[i] = get_bits(&s->gb, s->acfilter_scaling) + 1;
+        s->acfilter_coeffs[i] = get_bits(&s->gb, s->acfilter_scaling) + 1;
     }
 }
 
@@ -587,26 +611,26 @@ static void decode_mclms(WmallDecodeCtx *s)
     s->mclms_order = (get_bits(&s->gb, 4) + 1) * 2;
     s->mclms_scaling = get_bits(&s->gb, 4);
     if(get_bits1(&s->gb)) {
-       // mclms_send_coef
-       int i;
-       int send_coef_bits;
-       int cbits = av_log2(s->mclms_scaling + 1);
-       assert(cbits == my_log2(s->mclms_scaling + 1));
-       if(1 << cbits < s->mclms_scaling + 1)
-           cbits++;
-
-       send_coef_bits = (cbits ? get_bits(&s->gb, cbits) : 0) + 2;
-
-       for(i = 0; i < s->mclms_order * s->num_channels * s->num_channels; i++) {
-           s->mclms_coeffs[i] = get_bits(&s->gb, send_coef_bits);
-       }           
-
-       for(i = 0; i < s->num_channels; i++) {
-           int c;
-           for(c = 0; c < i; c++) {
-               s->mclms_coeffs_cur[i * s->num_channels + c] = get_bits(&s->gb, send_coef_bits);
-           }
-       }
+        // mclms_send_coef
+        int i;
+        int send_coef_bits;
+        int cbits = av_log2(s->mclms_scaling + 1);
+        assert(cbits == my_log2(s->mclms_scaling + 1));
+        if(1 << cbits < s->mclms_scaling + 1)
+            cbits++;
+
+        send_coef_bits = (cbits ? get_bits(&s->gb, cbits) : 0) + 2;
+
+        for(i = 0; i < s->mclms_order * s->num_channels * s->num_channels; i++) {
+            s->mclms_coeffs[i] = get_bits(&s->gb, send_coef_bits);
+        }
+
+        for(i = 0; i < s->num_channels; i++) {
+            int c;
+            for(c = 0; c < i; c++) {
+                s->mclms_coeffs_cur[i * s->num_channels + c] = get_bits(&s->gb, send_coef_bits);
+            }
+        }
     }
 }
 
@@ -620,36 +644,36 @@ static void decode_cdlms(WmallDecodeCtx *s)
     int cdlms_send_coef = get_bits1(&s->gb);
 
     for(c = 0; c < s->num_channels; c++) {
-       s->cdlms_ttl[c] = get_bits(&s->gb, 3) + 1;
-       for(i = 0; i < s->cdlms_ttl[c]; i++) {
-           s->cdlms[c][i].order = (get_bits(&s->gb, 7) + 1) * 8;
-       }
-
-       for(i = 0; i < s->cdlms_ttl[c]; i++) {
-           s->cdlms[c][i].scaling = get_bits(&s->gb, 4);
-       }
-
-       if(cdlms_send_coef) {
-           for(i = 0; i < s->cdlms_ttl[c]; i++) {
-               int cbits, shift_l, shift_r, j;
-               cbits = av_log2(s->cdlms[c][i].order);
-               if(1 << cbits < s->cdlms[c][i].order)
-                   cbits++;
-               s->cdlms[c][i].coefsend = get_bits(&s->gb, cbits) + 1;
-
-               cbits = av_log2(s->cdlms[c][i].scaling + 1);
-               if(1 << cbits < s->cdlms[c][i].scaling + 1)
-                   cbits++;
-               
-               s->cdlms[c][i].bitsend = get_bits(&s->gb, cbits) + 2;
-               shift_l = 32 - s->cdlms[c][i].bitsend;
-               shift_r = 32 - 2 - s->cdlms[c][i].scaling;
-               for(j = 0; j < s->cdlms[c][i].coefsend; j++) {
-                   s->cdlms[c][i].coefs[j] = 
-                       (get_bits(&s->gb, s->cdlms[c][i].bitsend) << shift_l) >> shift_r;
-               }
-           }
-       }
+        s->cdlms_ttl[c] = get_bits(&s->gb, 3) + 1;
+        for(i = 0; i < s->cdlms_ttl[c]; i++) {
+            s->cdlms[c][i].order = (get_bits(&s->gb, 7) + 1) * 8;
+        }
+
+        for(i = 0; i < s->cdlms_ttl[c]; i++) {
+            s->cdlms[c][i].scaling = get_bits(&s->gb, 4);
+        }
+
+        if(cdlms_send_coef) {
+            for(i = 0; i < s->cdlms_ttl[c]; i++) {
+                int cbits, shift_l, shift_r, j;
+                cbits = av_log2(s->cdlms[c][i].order);
+                if(1 << cbits < s->cdlms[c][i].order)
+                    cbits++;
+                s->cdlms[c][i].coefsend = get_bits(&s->gb, cbits) + 1;
+
+                cbits = av_log2(s->cdlms[c][i].scaling + 1);
+                if(1 << cbits < s->cdlms[c][i].scaling + 1)
+                    cbits++;
+
+                s->cdlms[c][i].bitsend = get_bits(&s->gb, cbits) + 2;
+                shift_l = 32 - s->cdlms[c][i].bitsend;
+                shift_r = 32 - 2 - s->cdlms[c][i].scaling;
+                for(j = 0; j < s->cdlms[c][i].coefsend; j++) {
+                    s->cdlms[c][i].coefs[j] =
+                        (get_bits(&s->gb, s->cdlms[c][i].bitsend) << shift_l) >> shift_r;
+                }
+            }
+        }
     }
 }
 
@@ -661,48 +685,50 @@ static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
     int i = 0;
     unsigned int ave_mean;
     s->transient[ch] = get_bits1(&s->gb);
-    if(s->transient[ch])
-       s->transient_pos[ch] = get_bits(&s->gb, av_log2(tile_size));
-    
+    if(s->transient[ch]) {
+            s->transient_pos[ch] = get_bits(&s->gb, av_log2(tile_size));
+        if (s->transient_pos[ch])
+                s->transient[ch] = 0;
+            s->channel[ch].transient_counter =
+                FFMAX(s->channel[ch].transient_counter, s->samples_per_frame / 2);
+        } else if (s->channel[ch].transient_counter)
+            s->transient[ch] = 1;
+
     if(s->seekable_tile) {
-       ave_mean = get_bits(&s->gb, s->bits_per_sample);
-       s->ave_sum[ch] = ave_mean << (s->movave_scaling + 1);
-//     s->ave_sum[ch] *= 2;
+        ave_mean = get_bits(&s->gb, s->bits_per_sample);
+        s->ave_sum[ch] = ave_mean << (s->movave_scaling + 1);
+//        s->ave_sum[ch] *= 2;
     }
 
     if(s->seekable_tile) {
-       if(s->do_inter_ch_decorr)
-           s->channel_residues[ch][0] = get_sbits(&s->gb, s->bits_per_sample + 1);
-       else
-           s->channel_residues[ch][0] = get_sbits(&s->gb, s->bits_per_sample);
-       i++;
+        if(s->do_inter_ch_decorr)
+            s->channel_residues[ch][0] = get_sbits(&s->gb, s->bits_per_sample + 1);
+        else
+            s->channel_residues[ch][0] = get_sbits(&s->gb, s->bits_per_sample);
+        i++;
     }
+    //av_log(0, 0, "%8d: ", num_logged_tiles++);
     for(; i < tile_size; i++) {
-       int quo = 0, rem, rem_bits, residue;
-       while(get_bits1(&s->gb))
-           quo++;
-       if(quo >= 32)
-           quo += get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1);
-
-               ave_mean = (s->ave_sum[ch] + (1 << s->movave_scaling)) >> (s->movave_scaling + 1);
-       rem_bits = av_ceil_log2(ave_mean);
-       rem = rem_bits ? get_bits(&s->gb, rem_bits) : 0;
-       residue = (quo << rem_bits) + rem;
-
-       s->ave_sum[ch] = residue + s->ave_sum[ch] - (s->ave_sum[ch] >> s->movave_scaling);
-
-       if(residue & 1)
-           residue = -(residue >> 1) - 1;
-       else
-           residue = residue >> 1;
-       s->channel_residues[ch][i] = residue;
-
-    //if (num_logged_tiles < 1)
-        av_log(0, 0, "%4d ", residue);
-//     dprintf(s->avctx, "%5d: %5d %10d %12d %12d %5d %-16d %04x\n",i, quo, ave_mean, s->ave_sum[ch], rem, rem_bits, s->channel_residues[ch][i], show_bits(&s->gb, 16));
+        int quo = 0, rem, rem_bits, residue;
+        while(get_bits1(&s->gb))
+            quo++;
+        if(quo >= 32)
+            quo += get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1);
+
+               ave_mean = (s->ave_sum[ch] + (1 << s->movave_scaling)) >> (s->movave_scaling + 1);
+        rem_bits = av_ceil_log2(ave_mean);
+        rem = rem_bits ? get_bits(&s->gb, rem_bits) : 0;
+        residue = (quo << rem_bits) + rem;
+
+        s->ave_sum[ch] = residue + s->ave_sum[ch] - (s->ave_sum[ch] >> s->movave_scaling);
+
+        if(residue & 1)
+            residue = -(residue >> 1) - 1;
+        else
+            residue = residue >> 1;
+        s->channel_residues[ch][i] = residue;
     }
-    av_log(0, 0, "\n");
-    //num_logged_tiles++;
+    //dump_int_buffer(s->channel_residues[ch], 4, tile_size, 16);
 
     return 0;
 
@@ -721,9 +747,9 @@ decode_lpc(WmallDecodeCtx *s)
     s->lpc_intbits = get_bits(&s->gb, 3) + 1;
     cbits = s->lpc_scaling + s->lpc_intbits;
     for(ch = 0; ch < s->num_channels; ch++) {
-       for(i = 0; i < s->lpc_order; i++) {
-           s->lpc_coefs[ch][i] = get_sbits(&s->gb, cbits);
-       }
+        for(i = 0; i < s->lpc_order; i++) {
+            s->lpc_coefs[ch][i] = get_sbits(&s->gb, cbits);
+        }
     }
 }
 
@@ -732,8 +758,9 @@ static void clear_codec_buffers(WmallDecodeCtx *s)
 {
     int ich, ilms;
 
-    memset(s->acfilter_coeffs, 0,     16 * sizeof(int));
-    memset(s->lpc_coefs      , 0, 40 * 2 * sizeof(int));
+    memset(s->acfilter_coeffs    , 0, 16 * sizeof(int));
+    memset(s->acfilter_prevvalues, 0, 16 * 2 * sizeof(int)); // may be wrong
+    memset(s->lpc_coefs          , 0, 40 * 2 * sizeof(int));
 
     memset(s->mclms_coeffs    , 0, 128 * sizeof(int16_t));
     memset(s->mclms_coeffs_cur, 0,   4 * sizeof(int16_t));
@@ -743,64 +770,181 @@ static void clear_codec_buffers(WmallDecodeCtx *s)
     for (ich = 0; ich < s->num_channels; ich++) {
         for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++) {
             memset(s->cdlms[ich][ilms].coefs         , 0, 256 * sizeof(int16_t));
-            memset(s->cdlms[ich][ilms].lms_prevvalues, 0, 512 * sizeof(int));
+            memset(s->cdlms[ich][ilms].lms_prevvalues, 0, 512 * sizeof(int16_t));
             memset(s->cdlms[ich][ilms].lms_updates   , 0, 512 * sizeof(int16_t));
         }
         s->ave_sum[ich] = 0;
     }
 }
 
+/**
+ *@brief Resets filter parameters and transient area at new seekable tile
+ */
 static void reset_codec(WmallDecodeCtx *s)
 {
     int ich, ilms;
     s->mclms_recent = s->mclms_order * s->num_channels;
-    for (ich = 0; ich < s->num_channels; ich++)
+    for (ich = 0; ich < s->num_channels; ich++) {
         for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++)
             s->cdlms[ich][ilms].recent = s->cdlms[ich][ilms].order;
+        /* first sample of a seekable subframe is considered as the starting of
+           a transient area which is samples_per_frame samples long */
+        s->channel[ich].transient_counter = s->samples_per_frame;
+        s->transient[ich] = 1;
+        s->transient_pos[ich] = 0;
+    }
 }
 
 
 
+static void mclms_update(WmallDecodeCtx *s, int icoef, int *pred)
+{
+    int i, j, ich;
+    int pred_error;
+    int order = s->mclms_order;
+    int num_channels = s->num_channels;
+    int range = 1 << (s->bits_per_sample - 1);
+    int bps = s->bits_per_sample > 16 ? 4 : 2; // bytes per sample
+
+    for (ich = 0; ich < num_channels; ich++) {
+        pred_error = s->channel_residues[ich][icoef] - pred[ich];
+        if (pred_error > 0) {
+            for (i = 0; i < order * num_channels; i++)
+                s->mclms_coeffs[i + ich * order * num_channels] +=
+                    s->mclms_updates[s->mclms_recent + i];
+            for (j = 0; j < ich; j++) {
+                if (s->channel_residues[j][icoef] > 0)
+                    s->mclms_coeffs_cur[ich * num_channels + j] += 1;
+                else if (s->channel_residues[j][icoef] < 0)
+                    s->mclms_coeffs_cur[ich * num_channels + j] -= 1;
+            }
+        } else if (pred_error < 0) {
+            for (i = 0; i < order * num_channels; i++)
+                s->mclms_coeffs[i + ich * order * num_channels] -=
+                    s->mclms_updates[s->mclms_recent + i];
+            for (j = 0; j < ich; j++) {
+                if (s->channel_residues[j][icoef] > 0)
+                    s->mclms_coeffs_cur[ich * num_channels + j] -= 1;
+                else if (s->channel_residues[j][icoef] < 0)
+                    s->mclms_coeffs_cur[ich * num_channels + j] += 1;
+            }
+        }
+    }
+
+    for (ich = num_channels - 1; ich >= 0; ich--) {
+        s->mclms_recent--;
+        s->mclms_prevvalues[s->mclms_recent] = s->channel_residues[ich][icoef];
+        if (s->channel_residues[ich][icoef] > range - 1)
+            s->mclms_prevvalues[s->mclms_recent] = range - 1;
+        else if (s->channel_residues[ich][icoef] < -range)
+            s->mclms_prevvalues[s->mclms_recent] = -range;
+
+        s->mclms_updates[s->mclms_recent] = 0;
+        if (s->channel_residues[ich][icoef] > 0)
+            s->mclms_updates[s->mclms_recent] = 1;
+        else if (s->channel_residues[ich][icoef] < 0)
+            s->mclms_updates[s->mclms_recent] = -1;
+    }
+
+    if (s->mclms_recent == 0) {
+        memcpy(&s->mclms_prevvalues[order * num_channels],
+               s->mclms_prevvalues,
+               bps * order * num_channels);
+        memcpy(&s->mclms_updates[order * num_channels],
+               s->mclms_updates,
+               bps * order * num_channels);
+        s->mclms_recent = num_channels * order;
+    }
+}
+
+static void mclms_predict(WmallDecodeCtx *s, int icoef, int *pred)
+{
+    int ich, i;
+    int order = s->mclms_order;
+    int num_channels = s->num_channels;
+
+    for (ich = 0; ich < num_channels; ich++) {
+        if (!s->is_channel_coded[ich])
+            continue;
+        pred[ich] = 0;
+        for (i = 0; i < order * num_channels; i++)
+            pred[ich] += s->mclms_prevvalues[i + s->mclms_recent] *
+                         s->mclms_coeffs[i + order * num_channels * ich];
+        for (i = 0; i < ich; i++)
+            pred[ich] += s->channel_residues[i][icoef] *
+                         s->mclms_coeffs_cur[i + num_channels * ich];
+        pred[ich] += 1 << s->mclms_scaling - 1;
+        pred[ich] >>= s->mclms_scaling;
+        s->channel_residues[ich][icoef] += pred[ich];
+    }
+}
+
+static void revert_mclms(WmallDecodeCtx *s, int tile_size)
+{
+    int icoef, pred[s->num_channels];
+    for (icoef = 0; icoef < tile_size; icoef++) {
+        mclms_predict(s, icoef, pred);
+        mclms_update(s, icoef, pred);
+    }
+}
+
 static int lms_predict(WmallDecodeCtx *s, int ich, int ilms)
 {
-    int32_t pred, icoef;
+    int pred = 0;
+    int icoef;
     int recent = s->cdlms[ich][ilms].recent;
 
     for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
         pred += s->cdlms[ich][ilms].coefs[icoef] *
                     s->cdlms[ich][ilms].lms_prevvalues[icoef + recent];
 
-    pred += (1 << (s->cdlms[ich][ilms].scaling - 1));
+    //pred += (1 << (s->cdlms[ich][ilms].scaling - 1));
     /* XXX: Table 29 has:
             iPred >= cdlms[iCh][ilms].scaling;
        seems to me like a missing > */
-    pred >>= s->cdlms[ich][ilms].scaling;
+    //pred >>= s->cdlms[ich][ilms].scaling;
     return pred;
 }
 
-static void lms_update(WmallDecodeCtx *s, int ich, int ilms, int32_t input, int32_t pred)
+static void lms_update(WmallDecodeCtx *s, int ich, int ilms, int input, int residue)
 {
     int icoef;
     int recent = s->cdlms[ich][ilms].recent;
-    int range = 1 << (s->bits_per_sample - 1);
+    int range = 1 << s->bits_per_sample - 1;
     int bps = s->bits_per_sample > 16 ? 4 : 2; // bytes per sample
 
-    if (input > pred) {
+    if (residue < 0) {
         for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
-            s->cdlms[ich][ilms].coefs[icoef] +=
+            s->cdlms[ich][ilms].coefs[icoef] -=
                 s->cdlms[ich][ilms].lms_updates[icoef + recent];
-    } else {
+    } else if (residue > 0) {
         for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
-            s->cdlms[ich][ilms].coefs[icoef] -=
-                s->cdlms[ich][ilms].lms_updates[icoef];     // XXX: [icoef + recent] ?
+            s->cdlms[ich][ilms].coefs[icoef] +=
+                s->cdlms[ich][ilms].lms_updates[icoef + recent];    /* spec mistakenly
+                                                                    dropped the recent */
     }
-    s->cdlms[ich][ilms].recent--;
-    s->cdlms[ich][ilms].lms_prevvalues[recent] = av_clip(input, -range, range - 1);
 
-    if (input > pred)
-        s->cdlms[ich][ilms].lms_updates[recent] = s->update_speed[ich];
-    else if (input < pred)
+    if (recent)
+        recent--;
+    else {
+        /* XXX: This memcpy()s will probably fail if a fixed 32-bit buffer is used.
+                follow kshishkov's suggestion of using a union. */
+        memcpy(&s->cdlms[ich][ilms].lms_prevvalues[s->cdlms[ich][ilms].order],
+               s->cdlms[ich][ilms].lms_prevvalues,
+               bps * s->cdlms[ich][ilms].order);
+        memcpy(&s->cdlms[ich][ilms].lms_updates[s->cdlms[ich][ilms].order],
+               s->cdlms[ich][ilms].lms_updates,
+               bps * s->cdlms[ich][ilms].order);
+        recent = s->cdlms[ich][ilms].order - 1;
+    }
+
+    s->cdlms[ich][ilms].lms_prevvalues[recent] = av_clip(input, -range, range - 1);
+    if (!input)
+        s->cdlms[ich][ilms].lms_updates[recent] = 0;
+    else if (input < 0)
         s->cdlms[ich][ilms].lms_updates[recent] = -s->update_speed[ich];
+    else
+        s->cdlms[ich][ilms].lms_updates[recent] = s->update_speed[ich];
 
     /* XXX: spec says:
     cdlms[iCh][ilms].updates[iRecent + cdlms[iCh][ilms].order >> 4] >>= 2;
@@ -810,20 +954,112 @@ static void lms_update(WmallDecodeCtx *s, int ich, int ilms, int32_t input, int3
         seperate buffers? Here I've assumed that the two are same which makes
         more sense to me.
     */
-    s->cdlms[ich][ilms].lms_updates[recent + s->cdlms[ich][ilms].order >> 4] >>= 2;
-    s->cdlms[ich][ilms].lms_updates[recent + s->cdlms[ich][ilms].order >> 3] >>= 1;
-    /* XXX: recent + (s->cdlms[ich][ilms].order >> 4) ? */
+    s->cdlms[ich][ilms].lms_updates[recent + (s->cdlms[ich][ilms].order >> 4)] >>= 2;
+    s->cdlms[ich][ilms].lms_updates[recent + (s->cdlms[ich][ilms].order >> 3)] >>= 1;
+    s->cdlms[ich][ilms].recent = recent;
+}
 
-    if (s->cdlms[ich][ilms].recent == 0) {
-        /* XXX: This memcpy()s will probably fail if a fixed 32-bit buffer is used.
-                follow kshishkov's suggestion of using a union. */
-        memcpy(s->cdlms[ich][ilms].lms_prevvalues + s->cdlms[ich][ilms].order,
-               s->cdlms[ich][ilms].lms_prevvalues,
-               bps * s->cdlms[ich][ilms].order);
-        memcpy(s->cdlms[ich][ilms].lms_updates + s->cdlms[ich][ilms].order,
-               s->cdlms[ich][ilms].lms_updates,
-               bps * s->cdlms[ich][ilms].order);
-        s->cdlms[ich][ilms].recent = s->cdlms[ich][ilms].order;
+static void use_high_update_speed(WmallDecodeCtx *s, int ich)
+{
+    int ilms, recent, icoef;
+    for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
+        recent = s->cdlms[ich][ilms].recent;
+        if (s->update_speed[ich] == 16)
+            continue;
+        if (s->bV3RTM) {
+            for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
+                s->cdlms[ich][ilms].lms_updates[icoef + recent] *= 2;
+        } else {
+            for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
+                s->cdlms[ich][ilms].lms_updates[icoef] *= 2;
+        }
+    }
+    s->update_speed[ich] = 16;
+}
+
+static void use_normal_update_speed(WmallDecodeCtx *s, int ich)
+{
+    int ilms, recent, icoef;
+    for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
+        recent = s->cdlms[ich][ilms].recent;
+        if (s->update_speed[ich] == 8)
+            continue;
+        if (s->bV3RTM) {
+            for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
+                s->cdlms[ich][ilms].lms_updates[icoef + recent] /= 2;
+        } else {
+            for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
+                s->cdlms[ich][ilms].lms_updates[icoef] /= 2;
+        }
+    }
+    s->update_speed[ich] = 8;
+}
+
+static void revert_cdlms(WmallDecodeCtx *s, int ch, int coef_begin, int coef_end)
+{
+    int icoef;
+    int pred;
+    int ilms, num_lms;
+    int residue, input;
+
+    num_lms = s->cdlms_ttl[ch];
+    for (ilms = num_lms - 1; ilms >= 0; ilms--) {
+        //s->cdlms[ch][ilms].recent = s->cdlms[ch][ilms].order;
+        for (icoef = coef_begin; icoef < coef_end; icoef++) {
+            pred = 1 << (s->cdlms[ch][ilms].scaling - 1);
+            residue = s->channel_residues[ch][icoef];
+            pred += lms_predict(s, ch, ilms);
+            input = residue + (pred >> s->cdlms[ch][ilms].scaling);
+            lms_update(s, ch, ilms, input, residue);
+            s->channel_residues[ch][icoef] = input;
+        }
+    }
+}
+
+static void revert_inter_ch_decorr(WmallDecodeCtx *s, int tile_size)
+{
+    int icoef;
+    if (s->num_channels != 2)
+        return;
+    else {
+        for (icoef = 0; icoef < tile_size; icoef++) {
+            s->channel_residues[0][icoef] -= s->channel_residues[1][icoef] >> 1;
+            s->channel_residues[1][icoef] += s->channel_residues[0][icoef];
+        }
+    }
+}
+
+static void revert_acfilter(WmallDecodeCtx *s, int tile_size)
+{
+    int ich, icoef;
+    int pred;
+    int i, j;
+    int64_t *filter_coeffs = s->acfilter_coeffs;
+    int scaling = s->acfilter_scaling;
+    int order = s->acfilter_order;
+
+    for (ich = 0; ich < s->num_channels; ich++) {
+        int *prevvalues = s->acfilter_prevvalues[ich];
+        for (i = 0; i < order; i++) {
+            pred = 0;
+            for (j = 0; j < order; j++) {
+                if (i <= j)
+                    pred += filter_coeffs[j] * prevvalues[j - i];
+                else
+                    pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
+            }
+            pred >>= scaling;
+            s->channel_residues[ich][i] += pred;
+        }
+        for (i = order; i < tile_size; i++) {
+            pred = 0;
+            for (j = 0; j < order; j++)
+                pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
+            pred >>= scaling;
+            s->channel_residues[ich][i] += pred;
+        }
+        for (j = 0; j < order; j++)
+            prevvalues[j] = s->channel_residues[ich][tile_size - j - 1];
     }
 }
 
@@ -836,7 +1072,7 @@ static int decode_subframe(WmallDecodeCtx *s)
 {
     int offset = s->samples_per_frame;
     int subframe_len = s->samples_per_frame;
-    int i;
+    int i, j;
     int total_samples   = s->samples_per_frame * s->num_channels;
     int rawpcm_tile;
     int padding_zeroes;
@@ -884,75 +1120,101 @@ static int decode_subframe(WmallDecodeCtx *s)
     if(s->seekable_tile) {
         clear_codec_buffers(s);
 
-       s->do_arith_coding    = get_bits1(&s->gb);
-       if(s->do_arith_coding) {
-           dprintf(s->avctx, "do_arith_coding == 1");
-           abort();
-       }
-       s->do_ac_filter       = get_bits1(&s->gb);
-       s->do_inter_ch_decorr = get_bits1(&s->gb);
-       s->do_mclms           = get_bits1(&s->gb);
-       
-       if(s->do_ac_filter)
-           decode_ac_filter(s);
-
-       if(s->do_mclms)
-           decode_mclms(s);
-
-       decode_cdlms(s);
-       s->movave_scaling = get_bits(&s->gb, 3);
-       s->quant_stepsize = get_bits(&s->gb, 8) + 1;
-
-           reset_codec(s);
+        s->do_arith_coding    = get_bits1(&s->gb);
+        if(s->do_arith_coding) {
+            dprintf(s->avctx, "do_arith_coding == 1");
+            abort();
+        }
+        s->do_ac_filter       = get_bits1(&s->gb);
+        s->do_inter_ch_decorr = get_bits1(&s->gb);
+        s->do_mclms           = get_bits1(&s->gb);
+
+        if(s->do_ac_filter)
+            decode_ac_filter(s);
+
+        if(s->do_mclms)
+            decode_mclms(s);
+
+        decode_cdlms(s);
+        s->movave_scaling = get_bits(&s->gb, 3);
+        s->quant_stepsize = get_bits(&s->gb, 8) + 1;
+
+            reset_codec(s);
     }
 
     rawpcm_tile = get_bits1(&s->gb);
 
     for(i = 0; i < s->num_channels; i++) {
-       s->is_channel_coded[i] = 1;
+        s->is_channel_coded[i] = 1;
     }
 
     if(!rawpcm_tile) {
 
-       for(i = 0; i < s->num_channels; i++) {
-           s->is_channel_coded[i] = get_bits1(&s->gb);
-       }
-
-       if(s->bV3RTM) {
-           // LPC
-           s->do_lpc = get_bits1(&s->gb);
-           if(s->do_lpc) {
-               decode_lpc(s);
-           }
-       } else {
-           s->do_lpc = 0;
-       }
+        for(i = 0; i < s->num_channels; i++) {
+            s->is_channel_coded[i] = get_bits1(&s->gb);
+        }
+
+        if(s->bV3RTM) {
+            // LPC
+            s->do_lpc = get_bits1(&s->gb);
+            if(s->do_lpc) {
+                decode_lpc(s);
+            }
+        } else {
+            s->do_lpc = 0;
+        }
     }
 
 
     if(get_bits1(&s->gb)) {
-       padding_zeroes = get_bits(&s->gb, 5);
+        padding_zeroes = get_bits(&s->gb, 5);
     } else {
-       padding_zeroes = 0;
+        padding_zeroes = 0;
     }
 
     if(rawpcm_tile) {
-       
-       int bits = s->bits_per_sample - padding_zeroes;
-       int j;
-       dprintf(s->avctx, "RAWPCM %d bits per sample. total %d bits, remain=%d\n", bits,
-               bits * s->num_channels * subframe_len, get_bits_count(&s->gb));
-       for(i = 0; i < s->num_channels; i++) {
-           for(j = 0; j < subframe_len; j++) {
-               s->channel_coeffs[i][j] = get_sbits(&s->gb, bits);
-//             dprintf(s->avctx, "PCM[%d][%d] = 0x%04x\n", i, j, s->channel_coeffs[i][j]);
-           }
-       }
+
+        int bits = s->bits_per_sample - padding_zeroes;
+        dprintf(s->avctx, "RAWPCM %d bits per sample. total %d bits, remain=%d\n", bits,
+                bits * s->num_channels * subframe_len, get_bits_count(&s->gb));
+        for(i = 0; i < s->num_channels; i++) {
+            for(j = 0; j < subframe_len; j++) {
+                s->channel_coeffs[i][j] = get_sbits(&s->gb, bits);
+//                dprintf(s->avctx, "PCM[%d][%d] = 0x%04x\n", i, j, s->channel_coeffs[i][j]);
+            }
+        }
     } else {
-       for(i = 0; i < s->num_channels; i++)
-           if(s->is_channel_coded[i])
-               decode_channel_residues(s, i, subframe_len);
+        for(i = 0; i < s->num_channels; i++)
+            if(s->is_channel_coded[i]) {
+            decode_channel_residues(s, i, subframe_len);
+            if (s->seekable_tile)
+                use_high_update_speed(s, i);
+            else
+                use_normal_update_speed(s, i);
+            revert_cdlms(s, i, 0, subframe_len);
+        }
     }
+    if (s->do_mclms)
+        revert_mclms(s, subframe_len);
+    if (s->do_inter_ch_decorr)
+        revert_inter_ch_decorr(s, subframe_len);
+    if(s->do_ac_filter)
+        revert_acfilter(s, subframe_len);
+
+    /* Dequantize */
+    if (s->quant_stepsize != 1)
+        for (i = 0; i < s->num_channels; i++)
+            for (j = 0; j < subframe_len; j++)
+                s->channel_residues[i][j] *= s->quant_stepsize;
+
+    // Write to proper output buffer depending on bit-depth
+    for (i = 0; i < subframe_len; i++)
+        for (j = 0; j < s->num_channels; j++) {
+            if (s->bits_per_sample == 16)
+                *s->samples_16++ = (int16_t) s->channel_residues[j][i];
+            else
+                *s->samples_32++ = s->channel_residues[j][i];
+        }
 
     /** handled one subframe */
 
@@ -964,6 +1226,7 @@ static int decode_subframe(WmallDecodeCtx *s)
         }
         ++s->channel[c].cur_subframe;
     }
+    num_logged_subframes++;
     return 0;
 }
 
@@ -979,9 +1242,14 @@ static int decode_frame(WmallDecodeCtx *s)
     int more_frames = 0;
     int len = 0;
     int i;
+    int buffer_len;
 
     /** check for potential output buffer overflow */
-    if (s->num_channels * s->samples_per_frame > s->samples_end - s->samples) {
+    if (s->bits_per_sample == 16)
+        buffer_len = s->samples_16_end - s->samples_16;
+    else
+        buffer_len = s->samples_32_end - s->samples_32;
+    if (s->num_channels * s->samples_per_frame > buffer_len) {
         /** return an error if no frame could be decoded at all */
         av_log(s->avctx, AV_LOG_ERROR,
                "not enough space for the output samples\n");
@@ -1043,8 +1311,7 @@ static int decode_frame(WmallDecodeCtx *s)
 
     if (s->skip_frame) {
         s->skip_frame = 0;
-    } else
-        s->samples += s->num_channels * s->samples_per_frame;
+    }
 
     if (s->len_prefix) {
         if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
@@ -1061,7 +1328,7 @@ static int decode_frame(WmallDecodeCtx *s)
     } else {
 /*
         while (get_bits_count(gb) < s->num_saved_bits && get_bits1(gb) == 0) {
-           dprintf(s->avctx, "skip1\n");
+            dprintf(s->avctx, "skip1\n");
         }
 */
     }
@@ -1153,11 +1420,17 @@ static int decode_packet(AVCodecContext *avctx,
     int num_bits_prev_frame;
     int packet_sequence_number;
 
-    s->samples       = data;
-    s->samples_end   = (float*)((int8_t*)data + *data_size);
+    if (s->bits_per_sample == 16) {
+        s->samples_16     = (int16_t *) data;
+        s->samples_16_end = (int16_t *) ((int8_t*)data + *data_size);
+    } else {
+        s->samples_32     = (int *) data;
+        s->samples_32_end = (int *) ((int8_t*)data + *data_size);
+    }
     *data_size = 0;
 
     if (s->packet_done || s->packet_loss) {
+        int seekable_frame_in_packet, spliced_packet;
         s->packet_done = 0;
 
         /** sanity check for the buffer length */
@@ -1171,8 +1444,8 @@ static int decode_packet(AVCodecContext *avctx,
         /** parse packet header */
         init_get_bits(gb, buf, s->buf_bit_size);
         packet_sequence_number = get_bits(gb, 4);
-       int seekable_frame_in_packet = get_bits1(gb);
-       int spliced_packet = get_bits1(gb);
+        seekable_frame_in_packet = get_bits1(gb);
+        spliced_packet = get_bits1(gb);
 
         /** get number of bits that need to be added to the previous frame */
         num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
@@ -1199,7 +1472,7 @@ static int decode_packet(AVCodecContext *avctx,
 
             /** decode the cross packet frame if it is valid */
             if (!s->packet_loss)
-               decode_frame(s);
+                decode_frame(s);
         } else if (s->num_saved_bits - s->frame_offset) {
             dprintf(avctx, "ignoring %x previously saved bits\n",
                     s->num_saved_bits - s->frame_offset);
@@ -1219,7 +1492,7 @@ static int decode_packet(AVCodecContext *avctx,
         s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
         init_get_bits(gb, avpkt->data, s->buf_bit_size);
         skip_bits(gb, s->packet_offset);
-       
+
         if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
             (frame_size = show_bits(gb, s->log2_frame_size)) &&
             frame_size <= remaining_bits(s, gb)) {
@@ -1237,7 +1510,7 @@ static int decode_packet(AVCodecContext *avctx,
             s->packet_done = !decode_frame(s);
         } else {
             s->packet_done = 1;
-       }
+        }
     }
 
     if (s->packet_done && !s->packet_loss &&
@@ -1247,7 +1520,10 @@ static int decode_packet(AVCodecContext *avctx,
         save_bits(s, gb, remaining_bits(s, gb), 0);
     }
 
-    *data_size = 0; // (int8_t *)s->samples - (int8_t *)data;
+    if (s->bits_per_sample == 16)
+        *data_size = (int8_t *)s->samples_16 - (int8_t *)data;
+    else
+        *data_size = (int8_t *)s->samples_32 - (int8_t *)data;
     s->packet_offset = get_bits_count(gb) & 7;
 
     return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
@@ -1282,7 +1558,7 @@ AVCodec ff_wmalossless_decoder = {
     NULL,
     decode_end,
     decode_packet,
-    .capabilities = CODEC_CAP_SUBFRAMES,
+    .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_EXPERIMENTAL,
     .flush= flush,
     .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 9 Lossless"),
 };