Commit a6191d09 authored by Daniil Cherednik's avatar Daniil Cherednik Committed by Rostislav Pehlivanov

dcaenc: Reverse data layout to prevent data copies during Huffman encoding introduction

Reviewed-by: 's avatarRostislav Pehlivanov <atomnuker@gmail.com>
parent e7dec52d
...@@ -61,15 +61,15 @@ typedef struct DCAEncContext { ...@@ -61,15 +61,15 @@ typedef struct DCAEncContext {
int32_t lfe_peak_cb; int32_t lfe_peak_cb;
const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
int32_t history[512][MAX_CHANNELS]; /* This is a circular buffer */ int32_t history[MAX_CHANNELS][512]; /* This is a circular buffer */
int32_t subband[SUBBAND_SAMPLES][DCAENC_SUBBANDS][MAX_CHANNELS]; int32_t subband[MAX_CHANNELS][DCAENC_SUBBANDS][SUBBAND_SAMPLES];
int32_t quantized[SUBBAND_SAMPLES][DCAENC_SUBBANDS][MAX_CHANNELS]; int32_t quantized[MAX_CHANNELS][DCAENC_SUBBANDS][SUBBAND_SAMPLES];
int32_t peak_cb[DCAENC_SUBBANDS][MAX_CHANNELS]; int32_t peak_cb[MAX_CHANNELS][DCAENC_SUBBANDS];
int32_t downsampled_lfe[DCA_LFE_SAMPLES]; int32_t downsampled_lfe[DCA_LFE_SAMPLES];
int32_t masking_curve_cb[SUBSUBFRAMES][256]; int32_t masking_curve_cb[SUBSUBFRAMES][256];
int abits[DCAENC_SUBBANDS][MAX_CHANNELS]; int abits[MAX_CHANNELS][DCAENC_SUBBANDS];
int scale_factor[DCAENC_SUBBANDS][MAX_CHANNELS]; int scale_factor[MAX_CHANNELS][DCAENC_SUBBANDS];
softfloat quant[DCAENC_SUBBANDS][MAX_CHANNELS]; softfloat quant[MAX_CHANNELS][DCAENC_SUBBANDS];
int32_t eff_masking_curve_cb[256]; int32_t eff_masking_curve_cb[256];
int32_t band_masking_cb[32]; int32_t band_masking_cb[32];
int32_t worst_quantization_noise; int32_t worst_quantization_noise;
...@@ -259,8 +259,7 @@ static void subband_transform(DCAEncContext *c, const int32_t *input) ...@@ -259,8 +259,7 @@ static void subband_transform(DCAEncContext *c, const int32_t *input)
int hist_start = 0; int hist_start = 0;
const int chi = c->channel_order_tab[ch]; const int chi = c->channel_order_tab[ch];
for (i = 0; i < 512; i++) memcpy(hist, &c->history[ch][0], 512 * sizeof(int32_t));
hist[i] = c->history[i][ch];
for (subs = 0; subs < SUBBAND_SAMPLES; subs++) { for (subs = 0; subs < SUBBAND_SAMPLES; subs++) {
int32_t accum[64]; int32_t accum[64];
...@@ -268,8 +267,7 @@ static void subband_transform(DCAEncContext *c, const int32_t *input) ...@@ -268,8 +267,7 @@ static void subband_transform(DCAEncContext *c, const int32_t *input)
int band; int band;
/* Calculate the convolutions at once */ /* Calculate the convolutions at once */
for (i = 0; i < 64; i++) memset(accum, 0, 64 * sizeof(int32_t));
accum[i] = 0;
for (k = 0, i = hist_start, j = 0; for (k = 0, i = hist_start, j = 0;
i < 512; k = (k + 1) & 63, i++, j++) i < 512; k = (k + 1) & 63, i++, j++)
...@@ -289,12 +287,13 @@ static void subband_transform(DCAEncContext *c, const int32_t *input) ...@@ -289,12 +287,13 @@ static void subband_transform(DCAEncContext *c, const int32_t *input)
resp += mul32(accum[i], cos_t(s << 3)) >> 3; resp += mul32(accum[i], cos_t(s << 3)) >> 3;
} }
c->subband[subs][band][ch] = ((band + 1) & 2) ? -resp : resp; c->subband[ch][band][subs] = ((band + 1) & 2) ? -resp : resp;
} }
/* Copy in 32 new samples from input */ /* Copy in 32 new samples from input */
for (i = 0; i < 32; i++) for (i = 0; i < 32; i++)
hist[i + hist_start] = input[(subs * 32 + i) * c->channels + chi]; hist[i + hist_start] = input[(subs * 32 + i) * c->channels + chi];
hist_start = (hist_start + 32) & 511; hist_start = (hist_start + 32) & 511;
} }
} }
...@@ -309,8 +308,7 @@ static void lfe_downsample(DCAEncContext *c, const int32_t *input) ...@@ -309,8 +308,7 @@ static void lfe_downsample(DCAEncContext *c, const int32_t *input)
int32_t accum; int32_t accum;
int hist_start = 0; int hist_start = 0;
for (i = 0; i < 512; i++) memcpy(hist, &c->history[c->channels - 1][0], 512 * sizeof(int32_t));
hist[i] = c->history[i][c->channels - 1];
for (lfes = 0; lfes < DCA_LFE_SAMPLES; lfes++) { for (lfes = 0; lfes < DCA_LFE_SAMPLES; lfes++) {
/* Calculate the convolution */ /* Calculate the convolution */
...@@ -516,7 +514,7 @@ static void calc_masking(DCAEncContext *c, const int32_t *input) ...@@ -516,7 +514,7 @@ static void calc_masking(DCAEncContext *c, const int32_t *input)
const int chi = c->channel_order_tab[ch]; const int chi = c->channel_order_tab[ch];
for (i = 0, k = 128 + 256 * ssf; k < 512; i++, k++) for (i = 0, k = 128 + 256 * ssf; k < 512; i++, k++)
data[i] = c->history[k][ch]; data[i] = c->history[ch][k];
for (k -= 512; i < 512; i++, k++) for (k -= 512; i < 512; i++, k++)
data[i] = input[k * c->channels + chi]; data[i] = input[k * c->channels + chi];
adjust_jnd(c->samplerate_index, data, c->masking_curve_cb[ssf]); adjust_jnd(c->samplerate_index, data, c->masking_curve_cb[ssf]);
...@@ -541,17 +539,17 @@ static void find_peaks(DCAEncContext *c) ...@@ -541,17 +539,17 @@ static void find_peaks(DCAEncContext *c)
{ {
int band, ch; int band, ch;
for (band = 0; band < 32; band++) for (ch = 0; ch < c->fullband_channels; ch++)
for (ch = 0; ch < c->fullband_channels; ch++) { for (band = 0; band < 32; band++) {
int sample; int sample;
int32_t m = 0; int32_t m = 0;
for (sample = 0; sample < SUBBAND_SAMPLES; sample++) { for (sample = 0; sample < SUBBAND_SAMPLES; sample++) {
int32_t s = abs(c->subband[sample][band][ch]); int32_t s = abs(c->subband[ch][band][sample]);
if (m < s) if (m < s)
m = s; m = s;
} }
c->peak_cb[band][ch] = get_cb(m); c->peak_cb[ch][band] = get_cb(m);
} }
if (c->lfe_channel) { if (c->lfe_channel) {
...@@ -581,27 +579,27 @@ static int init_quantization_noise(DCAEncContext *c, int noise) ...@@ -581,27 +579,27 @@ static int init_quantization_noise(DCAEncContext *c, int noise)
/* attempt to guess the bit distribution based on the prevoius frame */ /* attempt to guess the bit distribution based on the prevoius frame */
for (ch = 0; ch < c->fullband_channels; ch++) { for (ch = 0; ch < c->fullband_channels; ch++) {
for (band = 0; band < 32; band++) { for (band = 0; band < 32; band++) {
int snr_cb = c->peak_cb[band][ch] - c->band_masking_cb[band] - noise; int snr_cb = c->peak_cb[ch][band] - c->band_masking_cb[band] - noise;
if (snr_cb >= 1312) { if (snr_cb >= 1312) {
c->abits[band][ch] = 26; c->abits[ch][band] = 26;
ret |= USED_26ABITS; ret |= USED_26ABITS;
} else if (snr_cb >= 222) { } else if (snr_cb >= 222) {
c->abits[band][ch] = 8 + mul32(snr_cb - 222, 69000000); c->abits[ch][band] = 8 + mul32(snr_cb - 222, 69000000);
ret |= USED_NABITS; ret |= USED_NABITS;
} else if (snr_cb >= 0) { } else if (snr_cb >= 0) {
c->abits[band][ch] = 2 + mul32(snr_cb, 106000000); c->abits[ch][band] = 2 + mul32(snr_cb, 106000000);
ret |= USED_NABITS; ret |= USED_NABITS;
} else { } else {
c->abits[band][ch] = 1; c->abits[ch][band] = 1;
ret |= USED_1ABITS; ret |= USED_1ABITS;
} }
} }
} }
for (band = 0; band < 32; band++) for (ch = 0; ch < c->fullband_channels; ch++)
for (ch = 0; ch < c->fullband_channels; ch++) { for (band = 0; band < 32; band++) {
c->consumed_bits += bit_consumption[c->abits[band][ch]]; c->consumed_bits += bit_consumption[c->abits[ch][band]];
} }
return ret; return ret;
...@@ -653,7 +651,7 @@ static void shift_history(DCAEncContext *c, const int32_t *input) ...@@ -653,7 +651,7 @@ static void shift_history(DCAEncContext *c, const int32_t *input)
for (ch = 0; ch < c->channels; ch++) { for (ch = 0; ch < c->channels; ch++) {
const int chi = c->channel_order_tab[ch]; const int chi = c->channel_order_tab[ch];
c->history[k][ch] = input[k * c->channels + chi]; c->history[ch][k] = input[k * c->channels + chi];
} }
} }
...@@ -702,11 +700,11 @@ static void calc_scales(DCAEncContext *c) ...@@ -702,11 +700,11 @@ static void calc_scales(DCAEncContext *c)
{ {
int band, ch; int band, ch;
for (band = 0; band < 32; band++) for (ch = 0; ch < c->fullband_channels; ch++)
for (ch = 0; ch < c->fullband_channels; ch++) for (band = 0; band < 32; band++)
c->scale_factor[band][ch] = calc_one_scale(c->peak_cb[band][ch], c->scale_factor[ch][band] = calc_one_scale(c->peak_cb[ch][band],
c->abits[band][ch], c->abits[ch][band],
&c->quant[band][ch]); &c->quant[ch][band]);
if (c->lfe_channel) if (c->lfe_channel)
c->lfe_scale_factor = calc_one_scale(c->lfe_peak_cb, 11, &c->lfe_quant); c->lfe_scale_factor = calc_one_scale(c->lfe_peak_cb, 11, &c->lfe_quant);
...@@ -716,10 +714,10 @@ static void quantize_all(DCAEncContext *c) ...@@ -716,10 +714,10 @@ static void quantize_all(DCAEncContext *c)
{ {
int sample, band, ch; int sample, band, ch;
for (sample = 0; sample < SUBBAND_SAMPLES; sample++) for (ch = 0; ch < c->fullband_channels; ch++)
for (band = 0; band < 32; band++) for (band = 0; band < 32; band++)
for (ch = 0; ch < c->fullband_channels; ch++) for (sample = 0; sample < SUBBAND_SAMPLES; sample++)
c->quantized[sample][band][ch] = quantize_value(c->subband[sample][band][ch], c->quant[band][ch]); c->quantized[ch][band][sample] = quantize_value(c->subband[ch][band][sample], c->quant[ch][band]);
} }
static void put_frame_header(DCAEncContext *c) static void put_frame_header(DCAEncContext *c)
...@@ -853,22 +851,22 @@ static void put_primary_audio_header(DCAEncContext *c) ...@@ -853,22 +851,22 @@ static void put_primary_audio_header(DCAEncContext *c)
static void put_subframe_samples(DCAEncContext *c, int ss, int band, int ch) static void put_subframe_samples(DCAEncContext *c, int ss, int band, int ch)
{ {
if (c->abits[band][ch] <= 7) { if (c->abits[ch][band] <= 7) {
int sum, i, j; int sum, i, j;
for (i = 0; i < 8; i += 4) { for (i = 0; i < 8; i += 4) {
sum = 0; sum = 0;
for (j = 3; j >= 0; j--) { for (j = 3; j >= 0; j--) {
sum *= ff_dca_quant_levels[c->abits[band][ch]]; sum *= ff_dca_quant_levels[c->abits[ch][band]];
sum += c->quantized[ss * 8 + i + j][band][ch]; sum += c->quantized[ch][band][ss * 8 + i + j];
sum += (ff_dca_quant_levels[c->abits[band][ch]] - 1) / 2; sum += (ff_dca_quant_levels[c->abits[ch][band]] - 1) / 2;
} }
put_bits(&c->pb, bit_consumption[c->abits[band][ch]] / 4, sum); put_bits(&c->pb, bit_consumption[c->abits[ch][band]] / 4, sum);
} }
} else { } else {
int i; int i;
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
int bits = bit_consumption[c->abits[band][ch]] / 16; int bits = bit_consumption[c->abits[ch][band]] / 16;
put_sbits(&c->pb, bits, c->quantized[ss * 8 + i][band][ch]); put_sbits(&c->pb, bits, c->quantized[ch][band][ss * 8 + i]);
} }
} }
} }
...@@ -892,7 +890,7 @@ static void put_subframe(DCAEncContext *c, int subframe) ...@@ -892,7 +890,7 @@ static void put_subframe(DCAEncContext *c, int subframe)
/* Bit allocation index */ /* Bit allocation index */
for (ch = 0; ch < c->fullband_channels; ch++) for (ch = 0; ch < c->fullband_channels; ch++)
for (band = 0; band < DCAENC_SUBBANDS; band++) for (band = 0; band < DCAENC_SUBBANDS; band++)
put_bits(&c->pb, 5, c->abits[band][ch]); put_bits(&c->pb, 5, c->abits[ch][band]);
if (SUBSUBFRAMES > 1) { if (SUBSUBFRAMES > 1) {
/* Transition mode: none for each channel and subband */ /* Transition mode: none for each channel and subband */
...@@ -904,7 +902,7 @@ static void put_subframe(DCAEncContext *c, int subframe) ...@@ -904,7 +902,7 @@ static void put_subframe(DCAEncContext *c, int subframe)
/* Scale factors */ /* Scale factors */
for (ch = 0; ch < c->fullband_channels; ch++) for (ch = 0; ch < c->fullband_channels; ch++)
for (band = 0; band < DCAENC_SUBBANDS; band++) for (band = 0; band < DCAENC_SUBBANDS; band++)
put_bits(&c->pb, 7, c->scale_factor[band][ch]); put_bits(&c->pb, 7, c->scale_factor[ch][band]);
/* Joint subband scale factor codebook select: not transmitted */ /* Joint subband scale factor codebook select: not transmitted */
/* Scale factors for joint subband coding: not transmitted */ /* Scale factors for joint subband coding: not transmitted */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment