Commit c51301db authored by Rostislav Pehlivanov's avatar Rostislav Pehlivanov

dcaenc: move all tables inside context and fix incorrect coding style

Functionally identical to the old code, with less lines wasted.
Partially fixes the complete disregard for the 80 col/line guide.
Signed-off-by: 's avatarRostislav Pehlivanov <atomnuker@gmail.com>
parent ec6e389c
......@@ -52,6 +52,8 @@
#define SUBBAND_SAMPLES (SUBFRAMES * SUBSUBFRAMES * 8)
#define AUBANDS 25
#define COS_T(x) (c->cos_table[(x) & 2047])
typedef struct CompressionOptions {
int adpcm_mode;
} CompressionOptions;
......@@ -97,15 +99,15 @@ typedef struct DCAEncContext {
int32_t worst_noise_ever;
int consumed_bits;
int consumed_adpcm_bits; ///< Number of bits to transmit ADPCM related info
} DCAEncContext;
static int32_t cos_table[2048];
static int32_t band_interpolation[2][512];
static int32_t band_spectrum[2][8];
static int32_t auf[9][AUBANDS][256];
static int32_t cb_to_add[256];
static int32_t cb_to_level[2048];
static int32_t lfe_fir_64i[512];
int32_t cos_table[2048];
int32_t band_interpolation_tab[2][512];
int32_t band_spectrum_tab[2][8];
int32_t auf[9][AUBANDS][256];
int32_t cb_to_add[256];
int32_t cb_to_level[2048];
int32_t lfe_fir_64i[512];
} DCAEncContext;
/* Transfer function of outer and middle ear, Hz -> dB */
static double hom(double f)
......@@ -158,7 +160,7 @@ static int encode_init(AVCodecContext *avctx)
{
DCAEncContext *c = avctx->priv_data;
uint64_t layout = avctx->channel_layout;
int i, j, min_frame_bits;
int i, j, k, min_frame_bits;
int ret;
if (subband_bufer_alloc(c))
......@@ -166,8 +168,8 @@ static int encode_init(AVCodecContext *avctx)
c->fullband_channels = c->channels = avctx->channels;
c->lfe_channel = (avctx->channels == 3 || avctx->channels == 6);
c->band_interpolation = band_interpolation[1];
c->band_spectrum = band_spectrum[1];
c->band_interpolation = c->band_interpolation_tab[1];
c->band_spectrum = c->band_spectrum_tab[1];
c->worst_quantization_noise = -2047;
c->worst_noise_ever = -2047;
c->consumed_adpcm_bits = 0;
......@@ -240,65 +242,63 @@ static int encode_init(AVCodecContext *avctx)
if ((ret = ff_mdct_init(&c->mdct, 9, 0, 1.0)) < 0)
return ret;
if (!cos_table[0]) {
int j, k;
cos_table[0] = 0x7fffffff;
cos_table[512] = 0;
cos_table[1024] = -cos_table[0];
for (i = 1; i < 512; i++) {
cos_table[i] = (int32_t)(0x7fffffff * cos(M_PI * i / 1024));
cos_table[1024-i] = -cos_table[i];
cos_table[1024+i] = -cos_table[i];
cos_table[2048-i] = cos_table[i];
}
for (i = 0; i < 2048; i++) {
cb_to_level[i] = (int32_t)(0x7fffffff * ff_exp10(-0.005 * i));
}
/* Init all tables */
c->cos_table[0] = 0x7fffffff;
c->cos_table[512] = 0;
c->cos_table[1024] = -c->cos_table[0];
for (i = 1; i < 512; i++) {
c->cos_table[i] = (int32_t)(0x7fffffff * cos(M_PI * i / 1024));
c->cos_table[1024-i] = -c->cos_table[i];
c->cos_table[1024+i] = -c->cos_table[i];
c->cos_table[2048-i] = +c->cos_table[i];
}
for (k = 0; k < 32; k++) {
for (j = 0; j < 8; j++) {
lfe_fir_64i[64 * j + k] = (int32_t)(0xffffff800000ULL * ff_dca_lfe_fir_64[8 * k + j]);
lfe_fir_64i[64 * (7-j) + (63 - k)] = (int32_t)(0xffffff800000ULL * ff_dca_lfe_fir_64[8 * k + j]);
}
}
for (i = 0; i < 2048; i++)
c->cb_to_level[i] = (int32_t)(0x7fffffff * ff_exp10(-0.005 * i));
for (i = 0; i < 512; i++) {
band_interpolation[0][i] = (int32_t)(0x1000000000ULL * ff_dca_fir_32bands_perfect[i]);
band_interpolation[1][i] = (int32_t)(0x1000000000ULL * ff_dca_fir_32bands_nonperfect[i]);
for (k = 0; k < 32; k++) {
for (j = 0; j < 8; j++) {
c->lfe_fir_64i[64 * j + k] = (int32_t)(0xffffff800000ULL * ff_dca_lfe_fir_64[8 * k + j]);
c->lfe_fir_64i[64 * (7-j) + (63 - k)] = (int32_t)(0xffffff800000ULL * ff_dca_lfe_fir_64[8 * k + j]);
}
}
for (i = 0; i < 9; i++) {
for (j = 0; j < AUBANDS; j++) {
for (k = 0; k < 256; k++) {
double freq = sample_rates[i] * (k + 0.5) / 512;
for (i = 0; i < 512; i++) {
c->band_interpolation_tab[0][i] = (int32_t)(0x1000000000ULL * ff_dca_fir_32bands_perfect[i]);
c->band_interpolation_tab[1][i] = (int32_t)(0x1000000000ULL * ff_dca_fir_32bands_nonperfect[i]);
}
auf[i][j][k] = (int32_t)(10 * (hom(freq) + gammafilter(j, freq)));
}
for (i = 0; i < 9; i++) {
for (j = 0; j < AUBANDS; j++) {
for (k = 0; k < 256; k++) {
double freq = sample_rates[i] * (k + 0.5) / 512;
c->auf[i][j][k] = (int32_t)(10 * (hom(freq) + gammafilter(j, freq)));
}
}
}
for (i = 0; i < 256; i++) {
double add = 1 + ff_exp10(-0.01 * i);
cb_to_add[i] = (int32_t)(100 * log10(add));
}
for (j = 0; j < 8; j++) {
double accum = 0;
for (i = 0; i < 512; i++) {
double reconst = ff_dca_fir_32bands_perfect[i] * ((i & 64) ? (-1) : 1);
accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
}
band_spectrum[0][j] = (int32_t)(200 * log10(accum));
for (i = 0; i < 256; i++) {
double add = 1 + ff_exp10(-0.01 * i);
c->cb_to_add[i] = (int32_t)(100 * log10(add));
}
for (j = 0; j < 8; j++) {
double accum = 0;
for (i = 0; i < 512; i++) {
double reconst = ff_dca_fir_32bands_perfect[i] * ((i & 64) ? (-1) : 1);
accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
}
for (j = 0; j < 8; j++) {
double accum = 0;
for (i = 0; i < 512; i++) {
double reconst = ff_dca_fir_32bands_nonperfect[i] * ((i & 64) ? (-1) : 1);
accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
}
band_spectrum[1][j] = (int32_t)(200 * log10(accum));
c->band_spectrum_tab[0][j] = (int32_t)(200 * log10(accum));
}
for (j = 0; j < 8; j++) {
double accum = 0;
for (i = 0; i < 512; i++) {
double reconst = ff_dca_fir_32bands_nonperfect[i] * ((i & 64) ? (-1) : 1);
accum += reconst * cos(2 * M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
}
c->band_spectrum_tab[1][j] = (int32_t)(200 * log10(accum));
}
return 0;
}
......@@ -312,11 +312,6 @@ static av_cold int encode_close(AVCodecContext *avctx)
return 0;
}
static inline int32_t cos_t(int x)
{
return cos_table[x & 2047];
}
static void subband_transform(DCAEncContext *c, const int32_t *input)
{
int ch, subs, i, k, j;
......@@ -352,7 +347,7 @@ static void subband_transform(DCAEncContext *c, const int32_t *input)
resp = 0;
for (i = 16; i < 48; i++) {
int s = (2 * band + 1) * (2 * (i + 16) + 1);
resp += mul32(accum[i], cos_t(s << 3)) >> 3;
resp += mul32(accum[i], COS_T(s << 3)) >> 3;
}
c->subband[ch][band][subs] = ((band + 1) & 2) ? -resp : resp;
......@@ -383,9 +378,9 @@ static void lfe_downsample(DCAEncContext *c, const int32_t *input)
accum = 0;
for (i = hist_start, j = 0; i < 512; i++, j++)
accum += mul32(hist[i], lfe_fir_64i[j]);
accum += mul32(hist[i], c->lfe_fir_64i[j]);
for (i = 0; i < hist_start; i++, j++)
accum += mul32(hist[i], lfe_fir_64i[j]);
accum += mul32(hist[i], c->lfe_fir_64i[j]);
c->downsampled_lfe[lfes] = accum;
......@@ -397,28 +392,26 @@ static void lfe_downsample(DCAEncContext *c, const int32_t *input)
}
}
static int32_t get_cb(int32_t in)
static int32_t get_cb(DCAEncContext *c, int32_t in)
{
int i, res;
int i, res = 0;
in = FFABS(in);
res = 0;
if (in < 0)
in = -in;
for (i = 1024; i > 0; i >>= 1) {
if (cb_to_level[i + res] >= in)
if (c->cb_to_level[i + res] >= in)
res += i;
}
return -res;
}
static int32_t add_cb(int32_t a, int32_t b)
static int32_t add_cb(DCAEncContext *c, int32_t a, int32_t b)
{
if (a < b)
FFSWAP(int32_t, a, b);
if (a - b >= 256)
return a;
return a + cb_to_add[a - b];
return a + c->cb_to_add[a - b];
}
static void calc_power(DCAEncContext *c,
......@@ -428,13 +421,13 @@ static void calc_power(DCAEncContext *c,
LOCAL_ALIGNED_32(int32_t, data, [512]);
LOCAL_ALIGNED_32(int32_t, coeff, [256]);
for (i = 0; i < 512; i++) {
data[i] = norm__(mul32(in[i], 0x3fffffff - (cos_t(4 * i + 2) >> 1)), 4);
}
for (i = 0; i < 512; i++)
data[i] = norm__(mul32(in[i], 0x3fffffff - (COS_T(4 * i + 2) >> 1)), 4);
c->mdct.mdct_calc(&c->mdct, coeff, data);
for (i = 0; i < 256; i++) {
const int32_t cb = get_cb(coeff[i]);
power[i] = add_cb(cb, cb);
const int32_t cb = get_cb(c, coeff[i]);
power[i] = add_cb(c, cb, cb);
}
}
......@@ -451,21 +444,20 @@ static void adjust_jnd(DCAEncContext *c,
calc_power(c, in, power);
for (j = 0; j < 256; j++) {
for (j = 0; j < 256; j++)
out_cb_unnorm[j] = -2047; /* and can only grow */
}
for (i = 0; i < AUBANDS; i++) {
denom = ca_cb; /* and can only grow */
for (j = 0; j < 256; j++)
denom = add_cb(denom, power[j] + auf[samplerate_index][i][j]);
denom = add_cb(c, denom, power[j] + c->auf[samplerate_index][i][j]);
for (j = 0; j < 256; j++)
out_cb_unnorm[j] = add_cb(out_cb_unnorm[j],
-denom + auf[samplerate_index][i][j]);
out_cb_unnorm[j] = add_cb(c, out_cb_unnorm[j],
-denom + c->auf[samplerate_index][i][j]);
}
for (j = 0; j < 256; j++)
out_cb[j] = add_cb(out_cb[j], -out_cb_unnorm[j] - ca_cb - cs_cb);
out_cb[j] = add_cb(c, out_cb[j], -out_cb_unnorm[j] - ca_cb - cs_cb);
}
typedef void (*walk_band_t)(DCAEncContext *c, int band1, int band2, int f,
......@@ -547,16 +539,16 @@ static void calc_masking(DCAEncContext *c, const int32_t *input)
}
}
static inline int32_t find_peak(const int32_t *in, int len) {
static inline int32_t find_peak(DCAEncContext *c, const int32_t *in, int len)
{
int sample;
int32_t m = 0;
for (sample = 0; sample < len; sample++) {
int32_t s = abs(in[sample]);
if (m < s) {
if (m < s)
m = s;
}
}
return get_cb(m);
return get_cb(c, m);
}
static void find_peaks(DCAEncContext *c)
......@@ -564,14 +556,13 @@ static void find_peaks(DCAEncContext *c)
int band, ch;
for (ch = 0; ch < c->fullband_channels; ch++) {
for (band = 0; band < 32; band++) {
c->peak_cb[ch][band] = find_peak(c->subband[ch][band], SUBBAND_SAMPLES);
}
for (band = 0; band < 32; band++)
c->peak_cb[ch][band] = find_peak(c, c->subband[ch][band],
SUBBAND_SAMPLES);
}
if (c->lfe_channel) {
c->lfe_peak_cb = find_peak(c->downsampled_lfe, DCA_LFE_SAMPLES);
}
if (c->lfe_channel)
c->lfe_peak_cb = find_peak(c, c->downsampled_lfe, DCA_LFE_SAMPLES);
}
static void adpcm_analysis(DCAEncContext *c)
......@@ -585,11 +576,12 @@ static void adpcm_analysis(DCAEncContext *c)
for (ch = 0; ch < c->fullband_channels; ch++) {
for (band = 0; band < 32; band++) {
samples = c->subband[ch][band] - DCA_ADPCM_COEFFS;
pred_vq_id = ff_dcaadpcm_subband_analysis(&c->adpcm_ctx, samples, SUBBAND_SAMPLES, estimated_diff);
pred_vq_id = ff_dcaadpcm_subband_analysis(&c->adpcm_ctx, samples,
SUBBAND_SAMPLES, estimated_diff);
if (pred_vq_id >= 0) {
c->prediction_mode[ch][band] = pred_vq_id;
c->consumed_adpcm_bits += 12; //12 bits to transmit prediction vq index
c->diff_peak_cb[ch][band] = find_peak(estimated_diff, 16);
c->diff_peak_cb[ch][band] = find_peak(c, estimated_diff, 16);
} else {
c->prediction_mode[ch][band] = -1;
}
......@@ -601,7 +593,7 @@ static const int snr_fudge = 128;
#define USED_1ABITS 1
#define USED_26ABITS 4
static inline int32_t get_step_size(const DCAEncContext *c, int ch, int band)
static inline int32_t get_step_size(DCAEncContext *c, int ch, int band)
{
int32_t step_size;
......@@ -613,7 +605,8 @@ static inline int32_t get_step_size(const DCAEncContext *c, int ch, int band)
return step_size;
}
static int calc_one_scale(int32_t peak_cb, int abits, softfloat *quant)
static int calc_one_scale(DCAEncContext *c, int32_t peak_cb, int abits,
softfloat *quant)
{
int32_t peak;
int our_nscale, try_remove;
......@@ -623,7 +616,7 @@ static int calc_one_scale(int32_t peak_cb, int abits, softfloat *quant)
av_assert0(peak_cb >= -2047);
our_nscale = 127;
peak = cb_to_level[-peak_cb];
peak = c->cb_to_level[-peak_cb];
for (try_remove = 64; try_remove > 0; try_remove >>= 1) {
if (scalefactor_inv[our_nscale - try_remove].e + stepsize_inv[abits].e <= 17)
......@@ -649,15 +642,17 @@ static inline void quantize_adpcm_subband(DCAEncContext *c, int ch, int band)
{
int32_t step_size;
int32_t diff_peak_cb = c->diff_peak_cb[ch][band];
c->scale_factor[ch][band] = calc_one_scale(diff_peak_cb,
c->scale_factor[ch][band] = calc_one_scale(c, diff_peak_cb,
c->abits[ch][band],
&c->quant[ch][band]);
step_size = get_step_size(c, ch, band);
ff_dcaadpcm_do_real(c->prediction_mode[ch][band],
c->quant[ch][band], ff_dca_scale_factor_quant7[c->scale_factor[ch][band]], step_size,
c->adpcm_history[ch][band], c->subband[ch][band], c->adpcm_history[ch][band]+4, c->quantized[ch][band],
SUBBAND_SAMPLES, cb_to_level[-diff_peak_cb]);
c->quant[ch][band],
ff_dca_scale_factor_quant7[c->scale_factor[ch][band]],
step_size, c->adpcm_history[ch][band], c->subband[ch][band],
c->adpcm_history[ch][band] + 4, c->quantized[ch][band],
SUBBAND_SAMPLES, c->cb_to_level[-diff_peak_cb]);
}
static void quantize_adpcm(DCAEncContext *c)
......@@ -674,21 +669,31 @@ static void quantize_pcm(DCAEncContext *c)
{
int sample, band, ch;
for (ch = 0; ch < c->fullband_channels; ch++)
for (band = 0; band < 32; band++)
if (c->prediction_mode[ch][band] == -1)
for (sample = 0; sample < SUBBAND_SAMPLES; sample++)
c->quantized[ch][band][sample] = quantize_value(c->subband[ch][band][sample], c->quant[ch][band]);
for (ch = 0; ch < c->fullband_channels; ch++) {
for (band = 0; band < 32; band++) {
if (c->prediction_mode[ch][band] == -1) {
for (sample = 0; sample < SUBBAND_SAMPLES; sample++) {
int32_t val = quantize_value(c->subband[ch][band][sample],
c->quant[ch][band]);
c->quantized[ch][band][sample] = val;
}
}
}
}
}
static void accumulate_huff_bit_consumption(int abits, int32_t *quantized, uint32_t *result)
static void accumulate_huff_bit_consumption(int abits, int32_t *quantized,
uint32_t *result)
{
uint8_t sel, id = abits - 1;
for (sel = 0; sel < ff_dca_quant_index_group_size[id]; sel++)
result[sel] += ff_dca_vlc_calc_quant_bits(quantized, SUBBAND_SAMPLES, sel, id);
result[sel] += ff_dca_vlc_calc_quant_bits(quantized, SUBBAND_SAMPLES,
sel, id);
}
static uint32_t set_best_code(uint32_t vlc_bits[DCA_CODE_BOOKS][7], uint32_t clc_bits[DCA_CODE_BOOKS], int32_t res[DCA_CODE_BOOKS])
static uint32_t set_best_code(uint32_t vlc_bits[DCA_CODE_BOOKS][7],
uint32_t clc_bits[DCA_CODE_BOOKS],
int32_t res[DCA_CODE_BOOKS])
{
uint8_t i, sel;
uint32_t best_sel_bits[DCA_CODE_BOOKS];
......@@ -727,7 +732,8 @@ static uint32_t set_best_code(uint32_t vlc_bits[DCA_CODE_BOOKS][7], uint32_t clc
return bits;
}
static uint32_t set_best_abits_code(int abits[DCAENC_SUBBANDS], int bands, int32_t *res)
static uint32_t set_best_abits_code(int abits[DCAENC_SUBBANDS], int bands,
int32_t *res)
{
uint8_t i;
uint32_t t;
......@@ -788,7 +794,8 @@ static int init_quantization_noise(DCAEncContext *c, int noise, int forbid_zero)
ret &= ~(USED_26ABITS | USED_1ABITS);
}
}
c->consumed_bits += set_best_abits_code(c->abits[ch], 32, &c->bit_allocation_sel[ch]);
c->consumed_bits += set_best_abits_code(c->abits[ch], 32,
&c->bit_allocation_sel[ch]);
}
/* Recalc scale_factor each time to get bits consumption in case of Huffman coding.
......@@ -797,7 +804,7 @@ static int init_quantization_noise(DCAEncContext *c, int noise, int forbid_zero)
for (ch = 0; ch < c->fullband_channels; ch++) {
for (band = 0; band < 32; band++) {
if (c->prediction_mode[ch][band] == -1) {
c->scale_factor[ch][band] = calc_one_scale(c->peak_cb[ch][band],
c->scale_factor[ch][band] = calc_one_scale(c, c->peak_cb[ch][band],
c->abits[ch][band],
&c->quant[ch][band]);
}
......@@ -811,7 +818,9 @@ static int init_quantization_noise(DCAEncContext *c, int noise, int forbid_zero)
for (ch = 0; ch < c->fullband_channels; ch++) {
for (band = 0; band < 32; band++) {
if (c->abits[ch][band] && c->abits[ch][band] <= DCA_CODE_BOOKS) {
accumulate_huff_bit_consumption(c->abits[ch][band], c->quantized[ch][band], huff_bit_count_accum[ch][c->abits[ch][band] - 1]);
accumulate_huff_bit_consumption(c->abits[ch][band],
c->quantized[ch][band],
huff_bit_count_accum[ch][c->abits[ch][band] - 1]);
clc_bit_count_accum[ch][c->abits[ch][band] - 1] += bit_consumption[c->abits[ch][band]];
} else {
bits_counter += bit_consumption[c->abits[ch][band]];
......@@ -820,7 +829,9 @@ static int init_quantization_noise(DCAEncContext *c, int noise, int forbid_zero)
}
for (ch = 0; ch < c->fullband_channels; ch++) {
bits_counter += set_best_code(huff_bit_count_accum[ch], clc_bit_count_accum[ch], c->quant_index_sel[ch]);
bits_counter += set_best_code(huff_bit_count_accum[ch],
clc_bit_count_accum[ch],
c->quant_index_sel[ch]);
}
c->consumed_bits += bits_counter;
......@@ -897,7 +908,8 @@ static void fill_in_adpcm_bufer(DCAEncContext *c)
step_size = get_step_size(c, ch, band);
ff_dca_core_dequantize(c->adpcm_history[ch][band],
c->quantized[ch][band]+12, step_size, ff_dca_scale_factor_quant7[c->scale_factor[ch][band]], 0, 4);
c->quantized[ch][band]+12, step_size,
ff_dca_scale_factor_quant7[c->scale_factor[ch][band]], 0, 4);
} else {
AV_COPY128U(c->adpcm_history[ch][band], c->adpcm_history[ch][band]+4);
}
......@@ -920,7 +932,7 @@ static void fill_in_adpcm_bufer(DCAEncContext *c)
static void calc_lfe_scales(DCAEncContext *c)
{
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, c->lfe_peak_cb, 11, &c->lfe_quant);
}
static void put_frame_header(DCAEncContext *c)
......@@ -1061,7 +1073,8 @@ static void put_subframe_samples(DCAEncContext *c, int ss, int band, int ch)
sel = c->quant_index_sel[ch][c->abits[ch][band] - 1];
// Huffman codes
if (sel < ff_dca_quant_index_group_size[c->abits[ch][band] - 1]) {
ff_dca_vlc_enc_quant(&c->pb, &c->quantized[ch][band][ss * 8], 8, sel, c->abits[ch][band] - 1);
ff_dca_vlc_enc_quant(&c->pb, &c->quantized[ch][band][ss * 8], 8,
sel, c->abits[ch][band] - 1);
return;
}
......@@ -1114,7 +1127,8 @@ static void put_subframe(DCAEncContext *c, int subframe)
put_bits(&c->pb, 5, c->abits[ch][band]);
}
} else {
ff_dca_vlc_enc_alloc(&c->pb, c->abits[ch], DCAENC_SUBBANDS, c->bit_allocation_sel[ch]);
ff_dca_vlc_enc_alloc(&c->pb, c->abits[ch], DCAENC_SUBBANDS,
c->bit_allocation_sel[ch]);
}
}
......
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