+/** Read the channel data.
+ */
+static int read_channel_data(ALSDecContext *ctx, ALSChannelData *cd, int c)
+{
+ GetBitContext *gb = &ctx->gb;
+ ALSChannelData *current = cd;
+ unsigned int channels = ctx->avctx->channels;
+ int entries = 0;
+
+ while (entries < channels && !(current->stop_flag = get_bits1(gb))) {
+ current->master_channel = get_bits_long(gb, av_ceil_log2(channels));
+
+ if (current->master_channel >= channels) {
+ av_log(ctx->avctx, AV_LOG_ERROR, "Invalid master channel!\n");
+ return -1;
+ }
+
+ if (current->master_channel != c) {
+ current->time_diff_flag = get_bits1(gb);
+ current->weighting[0] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
+ current->weighting[1] = mcc_weightings[av_clip(decode_rice(gb, 2) + 14, 0, 32)];
+ current->weighting[2] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
+
+ if (current->time_diff_flag) {
+ current->weighting[3] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
+ current->weighting[4] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
+ current->weighting[5] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
+
+ current->time_diff_sign = get_bits1(gb);
+ current->time_diff_index = get_bits(gb, ctx->ltp_lag_length - 3) + 3;
+ }
+ }
+
+ current++;
+ entries++;
+ }
+
+ if (entries == channels) {
+ av_log(ctx->avctx, AV_LOG_ERROR, "Damaged channel data!\n");
+ return -1;
+ }
+
+ align_get_bits(gb);
+ return 0;
+}
+
+
+/** Recursively reverts the inter-channel correlation for a block.
+ */
+static int revert_channel_correlation(ALSDecContext *ctx, ALSBlockData *bd,
+ ALSChannelData **cd, int *reverted,
+ unsigned int offset, int c)
+{
+ ALSChannelData *ch = cd[c];
+ unsigned int dep = 0;
+ unsigned int channels = ctx->avctx->channels;
+
+ if (reverted[c])
+ return 0;
+
+ reverted[c] = 1;
+
+ while (dep < channels && !ch[dep].stop_flag) {
+ revert_channel_correlation(ctx, bd, cd, reverted, offset,
+ ch[dep].master_channel);
+
+ dep++;
+ }
+
+ if (dep == channels) {
+ av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel correlation!\n");
+ return -1;
+ }
+
+ bd->const_block = ctx->const_block + c;
+ bd->shift_lsbs = ctx->shift_lsbs + c;
+ bd->opt_order = ctx->opt_order + c;
+ bd->store_prev_samples = ctx->store_prev_samples + c;
+ bd->use_ltp = ctx->use_ltp + c;
+ bd->ltp_lag = ctx->ltp_lag + c;
+ bd->ltp_gain = ctx->ltp_gain[c];
+ bd->lpc_cof = ctx->lpc_cof[c];
+ bd->quant_cof = ctx->quant_cof[c];
+ bd->raw_samples = ctx->raw_samples[c] + offset;
+
+ dep = 0;
+ while (!ch[dep].stop_flag) {
+ unsigned int smp;
+ unsigned int begin = 1;
+ unsigned int end = bd->block_length - 1;
+ int64_t y;
+ int32_t *master = ctx->raw_samples[ch[dep].master_channel] + offset;
+
+ if (ch[dep].time_diff_flag) {
+ int t = ch[dep].time_diff_index;
+
+ if (ch[dep].time_diff_sign) {
+ t = -t;
+ begin -= t;
+ } else {
+ end -= t;
+ }
+
+ for (smp = begin; smp < end; smp++) {
+ y = (1 << 6) +
+ MUL64(ch[dep].weighting[0], master[smp - 1 ]) +
+ MUL64(ch[dep].weighting[1], master[smp ]) +
+ MUL64(ch[dep].weighting[2], master[smp + 1 ]) +
+ MUL64(ch[dep].weighting[3], master[smp - 1 + t]) +
+ MUL64(ch[dep].weighting[4], master[smp + t]) +
+ MUL64(ch[dep].weighting[5], master[smp + 1 + t]);
+
+ bd->raw_samples[smp] += y >> 7;
+ }
+ } else {
+ for (smp = begin; smp < end; smp++) {
+ y = (1 << 6) +
+ MUL64(ch[dep].weighting[0], master[smp - 1]) +
+ MUL64(ch[dep].weighting[1], master[smp ]) +
+ MUL64(ch[dep].weighting[2], master[smp + 1]);
+
+ bd->raw_samples[smp] += y >> 7;
+ }
+ }
+
+ dep++;
+ }
+
+ return 0;
+}
+
+
+/** Read the frame data.