/** Maximum number of exponent groups. +1 for separate DC exponent. */
#define AC3_MAX_EXP_GROUPS 85
+/* stereo rematrixing algorithms */
+#define AC3_REMATRIXING_IS_STATIC 0x1
+#define AC3_REMATRIXING_SUMS 0
+#define AC3_REMATRIXING_NONE 1
+#define AC3_REMATRIXING_ALWAYS 3
+
/** Scale a float value by 2^bits and convert to an integer. */
#define SCALE_FLOAT(a, bits) lrintf((a) * (float)(1 << (bits)))
int16_t **band_psd; ///< psd per critical band
int16_t **mask; ///< masking curve
uint16_t **qmant; ///< quantized mantissas
- uint8_t exp_strategy[AC3_MAX_CHANNELS]; ///< exponent strategies
int8_t exp_shift[AC3_MAX_CHANNELS]; ///< exponent shift values
+ uint8_t new_rematrixing_strategy; ///< send new rematrixing flags in this block
+ uint8_t rematrixing_flags[4]; ///< rematrixing flags
} AC3Block;
/**
int bandwidth_code[AC3_MAX_CHANNELS]; ///< bandwidth code (0 to 60) (chbwcod)
int nb_coefs[AC3_MAX_CHANNELS];
+ int rematrixing; ///< determines how rematrixing strategy is calculated
+
/* bitrate allocation control */
int slow_gain_code; ///< slow gain code (sgaincod)
int slow_decay_code; ///< slow decay code (sdcycod)
int16_t *mask_buffer;
uint16_t *qmant_buffer;
+ uint8_t exp_strategy[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< exponent strategies
+
DECLARE_ALIGNED(16, SampleType, windowed_samples)[AC3_WINDOW_SIZE];
} AC3EncodeContext;
static void mdct512(AC3MDCTContext *mdct, CoefType *out, SampleType *in);
-static void apply_window(SampleType *output, const SampleType *input,
+static void apply_window(DSPContext *dsp, SampleType *output, const SampleType *input,
const SampleType *window, int n);
static int normalize_samples(AC3EncodeContext *s);
AC3Block *block = &s->blocks[blk];
const SampleType *input_samples = &s->planar_samples[ch][blk * AC3_BLOCK_SIZE];
- apply_window(s->windowed_samples, input_samples, s->mdct.window, AC3_WINDOW_SIZE);
+ apply_window(&s->dsp, s->windowed_samples, input_samples, s->mdct.window, AC3_WINDOW_SIZE);
block->exp_shift[ch] = normalize_samples(s);
}
+/**
+ * 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;
+ }
+ }
+ }
+ }
+}
+
+
/**
* Initialize exponent tables.
*/
* Calculate exponent strategies for all blocks in a single channel.
*/
static void compute_exp_strategy_ch(AC3EncodeContext *s, uint8_t *exp_strategy,
- uint8_t **exp)
+ uint8_t *exp)
{
int blk, blk1;
int exp_diff;
/* estimate if the exponent variation & decide if they should be
reused in the next frame */
exp_strategy[0] = EXP_NEW;
+ exp += AC3_MAX_COEFS;
for (blk = 1; blk < AC3_MAX_BLOCKS; blk++) {
- exp_diff = s->dsp.sad[0](NULL, exp[blk], exp[blk-1], 16, 16);
+ exp_diff = s->dsp.sad[0](NULL, exp, exp - AC3_MAX_COEFS, 16, 16);
if (exp_diff > EXP_DIFF_THRESHOLD)
exp_strategy[blk] = EXP_NEW;
else
exp_strategy[blk] = EXP_REUSE;
+ exp += AC3_MAX_COEFS;
}
emms_c();
*/
static void compute_exp_strategy(AC3EncodeContext *s)
{
- uint8_t *exp1[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS];
- uint8_t exp_str1[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS];
int ch, blk;
for (ch = 0; ch < s->fbw_channels; ch++) {
- for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
- exp1[ch][blk] = s->blocks[blk].exp[ch];
- exp_str1[ch][blk] = s->blocks[blk].exp_strategy[ch];
- }
-
- compute_exp_strategy_ch(s, exp_str1[ch], exp1[ch]);
-
- for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
- s->blocks[blk].exp_strategy[ch] = exp_str1[ch][blk];
+ compute_exp_strategy_ch(s, s->exp_strategy[ch], s->blocks[0].exp[ch]);
}
if (s->lfe_on) {
ch = s->lfe_channel;
- s->blocks[0].exp_strategy[ch] = EXP_D15;
+ s->exp_strategy[ch][0] = EXP_D15;
for (blk = 1; blk < AC3_MAX_BLOCKS; blk++)
- s->blocks[blk].exp_strategy[ch] = EXP_REUSE;
+ s->exp_strategy[ch][blk] = EXP_REUSE;
}
}
/**
* Set each encoded exponent in a block to the minimum of itself and the
- * exponent in the same frequency bin of a following block.
- * exp[i] = min(exp[i], exp1[i]
+ * exponents in the same frequency bin of up to 5 following blocks.
*/
-static void exponent_min(uint8_t *exp, uint8_t *exp1, int n)
+static void exponent_min(uint8_t *exp, int num_reuse_blocks, int nb_coefs)
{
- int i;
- for (i = 0; i < n; i++) {
- if (exp1[i] < exp[i])
- exp[i] = exp1[i];
+ int blk, i;
+
+ if (!num_reuse_blocks)
+ return;
+
+ for (i = 0; i < nb_coefs; i++) {
+ uint8_t min_exp = *exp;
+ uint8_t *exp1 = exp + AC3_MAX_COEFS;
+ for (blk = 0; blk < num_reuse_blocks; blk++) {
+ uint8_t next_exp = *exp1;
+ if (next_exp < min_exp)
+ min_exp = next_exp;
+ exp1 += AC3_MAX_COEFS;
+ }
+ *exp++ = min_exp;
}
}
*/
static void encode_exponents(AC3EncodeContext *s)
{
- int blk, blk1, blk2, ch;
- AC3Block *block, *block1, *block2;
+ int blk, blk1, ch;
+ uint8_t *exp, *exp1, *exp_strategy;
+ int nb_coefs, num_reuse_blocks;
for (ch = 0; ch < s->channels; ch++) {
+ exp = s->blocks[0].exp[ch];
+ exp_strategy = s->exp_strategy[ch];
+ nb_coefs = s->nb_coefs[ch];
+
blk = 0;
- block = &s->blocks[0];
while (blk < AC3_MAX_BLOCKS) {
blk1 = blk + 1;
- block1 = block + 1;
- /* for the EXP_REUSE case we select the min of the exponents */
- while (blk1 < AC3_MAX_BLOCKS && block1->exp_strategy[ch] == EXP_REUSE) {
- exponent_min(block->exp[ch], block1->exp[ch], s->nb_coefs[ch]);
+
+ /* count the number of EXP_REUSE blocks after the current block */
+ while (blk1 < AC3_MAX_BLOCKS && exp_strategy[blk1] == EXP_REUSE)
blk1++;
- block1++;
- }
- encode_exponents_blk_ch(block->exp[ch], s->nb_coefs[ch],
- block->exp_strategy[ch]);
+ num_reuse_blocks = blk1 - blk - 1;
+
+ /* for the EXP_REUSE case we select the min of the exponents */
+ exponent_min(exp, num_reuse_blocks, nb_coefs);
+
+ encode_exponents_blk_ch(exp, nb_coefs, exp_strategy[blk]);
+
/* copy encoded exponents for reuse case */
- block2 = block + 1;
- for (blk2 = blk+1; blk2 < blk1; blk2++, block2++) {
- memcpy(block2->exp[ch], block->exp[ch],
- s->nb_coefs[ch] * sizeof(uint8_t));
+ exp1 = exp + AC3_MAX_COEFS;
+ while (blk < blk1-1) {
+ memcpy(exp1, exp, nb_coefs * sizeof(*exp));
+ exp1 += AC3_MAX_COEFS;
+ blk++;
}
blk = blk1;
- block = block1;
+ exp = exp1;
}
}
}
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
AC3Block *block = &s->blocks[blk];
for (ch = 0; ch < s->channels; ch++) {
- if (block->exp_strategy[ch] == EXP_REUSE) {
+ int exp_strategy = s->exp_strategy[ch][blk];
+ if (exp_strategy == EXP_REUSE)
continue;
- }
- group_size = block->exp_strategy[ch] + (block->exp_strategy[ch] == EXP_D45);
- nb_groups = exponent_group_tab[block->exp_strategy[ch]-1][s->nb_coefs[ch]];
+ group_size = exp_strategy + (exp_strategy == EXP_D45);
+ nb_groups = exponent_group_tab[exp_strategy-1][s->nb_coefs[ch]];
bit_count += 4 + (nb_groups * 7);
p = block->exp[ch];
/* assumptions:
* no dynamic range codes
* no channel coupling
- * no rematrixing
* bit allocation parameters do not change between blocks
* SNR offsets do not change between blocks
* no delta bit allocation
frame_bits += s->fbw_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */
if (s->channel_mode == AC3_CHMODE_STEREO) {
frame_bits++; /* rematstr */
- if (!blk)
- frame_bits += 4;
}
frame_bits += 2 * s->fbw_channels; /* chexpstr[2] * c */
if (s->lfe_on)
int frame_bits = 0;
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
- uint8_t *exp_strategy = s->blocks[blk].exp_strategy;
+ /* stereo rematrixing */
+ if (s->channel_mode == AC3_CHMODE_STEREO &&
+ s->blocks[blk].new_rematrixing_strategy) {
+ frame_bits += 4;
+ }
+
for (ch = 0; ch < s->fbw_channels; ch++) {
- if (exp_strategy[ch] != EXP_REUSE)
+ if (s->exp_strategy[ch][blk] != EXP_REUSE)
frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */
}
}
/* We only need psd and mask for calculating bap.
Since we currently do not calculate bap when exponent
strategy is EXP_REUSE we do not need to calculate psd or mask. */
- if (block->exp_strategy[ch] != EXP_REUSE) {
+ if (s->exp_strategy[ch][blk] != EXP_REUSE) {
ff_ac3_bit_alloc_calc_psd(block->exp[ch], 0,
s->nb_coefs[ch],
block->psd[ch], block->band_psd[ch]);
blocks within a frame are the exponent values. We can take
advantage of that by reusing the bit allocation pointers
whenever we reuse exponents. */
- if (block->exp_strategy[ch] == EXP_REUSE) {
+ if (s->exp_strategy[ch][blk] == EXP_REUSE) {
memcpy(block->bap[ch], s->blocks[blk-1].bap[ch], AC3_MAX_COEFS);
} else {
ff_ac3_bit_alloc_calc_bap(block->mask[ch], block->psd[ch], 0,
for (ch = 0; ch < s->fbw_channels; ch++) {
for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) {
- if (s->blocks[blk].exp_strategy[ch] == EXP_D15) {
- s->blocks[blk].exp_strategy[ch] = EXP_D25;
+ if (s->exp_strategy[ch][blk] == EXP_D15) {
+ s->exp_strategy[ch][blk] = EXP_D25;
return 0;
}
}
}
for (ch = 0; ch < s->fbw_channels; ch++) {
for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) {
- if (s->blocks[blk].exp_strategy[ch] == EXP_D25) {
- s->blocks[blk].exp_strategy[ch] = EXP_D45;
+ if (s->exp_strategy[ch][blk] == EXP_D25) {
+ s->exp_strategy[ch][blk] = EXP_D45;
return 0;
}
}
/* block 0 cannot reuse exponents, so only downgrade D45 to REUSE if
the block number > 0 */
for (blk = AC3_MAX_BLOCKS-1; blk > 0; blk--) {
- if (s->blocks[blk].exp_strategy[ch] > EXP_REUSE) {
- s->blocks[blk].exp_strategy[ch] = EXP_REUSE;
+ if (s->exp_strategy[ch][blk] > EXP_REUSE) {
+ s->exp_strategy[ch][blk] = EXP_REUSE;
return 0;
}
}
/**
* Write one audio block to the output bitstream.
*/
-static void output_audio_block(AC3EncodeContext *s, int block_num)
+static void output_audio_block(AC3EncodeContext *s, int blk)
{
int ch, i, baie, rbnd;
- AC3Block *block = &s->blocks[block_num];
+ AC3Block *block = &s->blocks[blk];
/* block switching */
for (ch = 0; ch < s->fbw_channels; ch++)
put_bits(&s->pb, 1, 0);
/* channel coupling */
- if (!block_num) {
+ if (!blk) {
put_bits(&s->pb, 1, 1); /* coupling strategy present */
put_bits(&s->pb, 1, 0); /* no coupling strategy */
} else {
/* stereo rematrixing */
if (s->channel_mode == AC3_CHMODE_STEREO) {
- if (!block_num) {
- /* first block must define rematrixing (rematstr) */
- put_bits(&s->pb, 1, 1);
-
- /* dummy rematrixing rematflg(1:4)=0 */
+ put_bits(&s->pb, 1, block->new_rematrixing_strategy);
+ if (block->new_rematrixing_strategy) {
+ /* rematrixing flags */
for (rbnd = 0; rbnd < 4; rbnd++)
- put_bits(&s->pb, 1, 0);
- } else {
- /* no matrixing (but should be used in the future) */
- put_bits(&s->pb, 1, 0);
+ put_bits(&s->pb, 1, block->rematrixing_flags[rbnd]);
}
}
/* exponent strategy */
for (ch = 0; ch < s->fbw_channels; ch++)
- put_bits(&s->pb, 2, block->exp_strategy[ch]);
+ put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
if (s->lfe_on)
- put_bits(&s->pb, 1, block->exp_strategy[s->lfe_channel]);
+ put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
/* bandwidth */
for (ch = 0; ch < s->fbw_channels; ch++) {
- if (block->exp_strategy[ch] != EXP_REUSE)
+ if (s->exp_strategy[ch][blk] != EXP_REUSE)
put_bits(&s->pb, 6, s->bandwidth_code[ch]);
}
for (ch = 0; ch < s->channels; ch++) {
int nb_groups;
- if (block->exp_strategy[ch] == EXP_REUSE)
+ if (s->exp_strategy[ch][blk] == EXP_REUSE)
continue;
/* DC exponent */
put_bits(&s->pb, 4, block->grouped_exp[ch][0]);
/* exponent groups */
- nb_groups = exponent_group_tab[block->exp_strategy[ch]-1][s->nb_coefs[ch]];
+ nb_groups = exponent_group_tab[s->exp_strategy[ch][blk]-1][s->nb_coefs[ch]];
for (i = 1; i <= nb_groups; i++)
put_bits(&s->pb, 7, block->grouped_exp[ch][i]);
}
/* bit allocation info */
- baie = (block_num == 0);
+ baie = (blk == 0);
put_bits(&s->pb, 1, baie);
if (baie) {
put_bits(&s->pb, 2, s->slow_decay_code);
apply_mdct(s);
+ compute_rematrixing_strategy(s);
+
scale_coefficients(s);
+ apply_rematrixing(s);
+
process_exponents(s);
ret = compute_bit_allocation(s);
alloc_fail);
for (ch = 0; ch < s->channels; ch++) {
+ /* arrangement: block, channel, coeff */
block->bap[ch] = &s->bap_buffer [AC3_MAX_COEFS * (blk * s->channels + ch)];
block->mdct_coef[ch] = &s->mdct_coef_buffer [AC3_MAX_COEFS * (blk * s->channels + ch)];
- block->exp[ch] = &s->exp_buffer [AC3_MAX_COEFS * (blk * s->channels + ch)];
block->grouped_exp[ch] = &s->grouped_exp_buffer[128 * (blk * s->channels + ch)];
block->psd[ch] = &s->psd_buffer [AC3_MAX_COEFS * (blk * s->channels + ch)];
block->band_psd[ch] = &s->band_psd_buffer [64 * (blk * s->channels + ch)];
block->mask[ch] = &s->mask_buffer [64 * (blk * s->channels + ch)];
block->qmant[ch] = &s->qmant_buffer [AC3_MAX_COEFS * (blk * s->channels + ch)];
+
+ /* arrangement: channel, block, coeff */
+ block->exp[ch] = &s->exp_buffer [AC3_MAX_COEFS * (AC3_MAX_BLOCKS * ch + blk)];
}
}
set_bandwidth(s);
+ rematrixing_init(s);
+
exponent_init(s);
bit_alloc_init(s);