+/**
+ * Initialize stereo rematrixing.
+ * If the strategy does not change for each frame, set the rematrixing flags.
+ */
+static void rematrixing_init(AC3EncodeContext *s)
+{
+ if (s->channel_mode == AC3_CHMODE_STEREO)
+ s->rematrixing = AC3_REMATRIXING_SUMS;
+ else
+ s->rematrixing = AC3_REMATRIXING_NONE;
+ /* NOTE: AC3_REMATRIXING_ALWAYS might be used in
+ the future in conjunction with channel coupling. */
+
+ if (s->rematrixing & AC3_REMATRIXING_IS_STATIC) {
+ int flag = (s->rematrixing == AC3_REMATRIXING_ALWAYS);
+ s->blocks[0].new_rematrixing_strategy = 1;
+ memset(s->blocks[0].rematrixing_flags, flag,
+ sizeof(s->blocks[0].rematrixing_flags));
+ }
+}
+
+
+/**
+ * Determine rematrixing flags for each block and band.
+ */
+static void compute_rematrixing_strategy(AC3EncodeContext *s)
+{
+ int nb_coefs;
+ int blk, bnd, i;
+ AC3Block *block, *block0;
+
+ if (s->rematrixing & AC3_REMATRIXING_IS_STATIC)
+ return;
+
+ nb_coefs = FFMIN(s->nb_coefs[0], s->nb_coefs[1]);
+
+ s->blocks[0].new_rematrixing_strategy = 1;
+ for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
+ block = &s->blocks[blk];
+ for (bnd = 0; bnd < 4; bnd++) {
+ /* calculate calculate sum of squared coeffs for one band in one block */
+ int start = ff_ac3_rematrix_band_tab[bnd];
+ int end = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
+ CoefSumType sum[4] = {0,};
+ for (i = start; i < end; i++) {
+ CoefType lt = block->mdct_coef[0][i];
+ CoefType rt = block->mdct_coef[1][i];
+ CoefType md = lt + rt;
+ CoefType sd = lt - rt;
+ sum[0] += lt * lt;
+ sum[1] += rt * rt;
+ sum[2] += md * md;
+ sum[3] += sd * sd;
+ }
+
+ /* compare sums to determine if rematrixing will be used for this band */
+ if (FFMIN(sum[2], sum[3]) < FFMIN(sum[0], sum[1]))
+ block->rematrixing_flags[bnd] = 1;
+ else
+ block->rematrixing_flags[bnd] = 0;
+
+ /* determine if new rematrixing flags will be sent */
+ if (blk &&
+ !block->new_rematrixing_strategy &&
+ block->rematrixing_flags[bnd] != block0->rematrixing_flags[bnd]) {
+ block->new_rematrixing_strategy = 1;
+ }
+ }
+ block0 = block;
+ }
+}
+
+
+/**
+ * Apply stereo rematrixing to coefficients based on rematrixing flags.
+ */
+static void apply_rematrixing(AC3EncodeContext *s)
+{
+ int nb_coefs;
+ int blk, bnd, i;
+ int start, end;
+ uint8_t *flags;
+
+ if (s->rematrixing == AC3_REMATRIXING_NONE)
+ return;
+
+ nb_coefs = FFMIN(s->nb_coefs[0], s->nb_coefs[1]);
+
+ for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
+ AC3Block *block = &s->blocks[blk];
+ if (block->new_rematrixing_strategy)
+ flags = block->rematrixing_flags;
+ for (bnd = 0; bnd < 4; bnd++) {
+ if (flags[bnd]) {
+ start = ff_ac3_rematrix_band_tab[bnd];
+ end = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
+ for (i = start; i < end; i++) {
+ int32_t lt = block->fixed_coef[0][i];
+ int32_t rt = block->fixed_coef[1][i];
+ block->fixed_coef[0][i] = (lt + rt) >> 1;
+ block->fixed_coef[1][i] = (lt - rt) >> 1;
+ }
+ }
+ }
+ }
+}
+
+