73
74
static VLC vlc_sbr[10];
74
75
static const int8_t vlc_sbr_lav[10] =
75
76
{ 60, 60, 24, 24, 31, 31, 12, 12, 31, 12 };
76
static const DECLARE_ALIGNED(16, float, zero64)[64];
78
78
#define SBR_INIT_VLC_STATIC(num, size) \
79
79
INIT_VLC_STATIC(&vlc_sbr[num], 9, sbr_tmp[num].table_size / sbr_tmp[num].elem_size, \
129
/** Places SBR in pure upsampling mode. */
130
static void sbr_turnoff(SpectralBandReplication *sbr) {
132
// Init defults used in pure upsampling mode
133
sbr->kx[1] = 32; //Typo in spec, kx' inits to 32
135
// Reset values for first SBR header
136
sbr->data[0].e_a[1] = sbr->data[1].e_a[1] = -1;
137
memset(&sbr->spectrum_params, -1, sizeof(SpectrumParameters));
129
140
av_cold void ff_aac_sbr_ctx_init(AACContext *ac, SpectralBandReplication *sbr)
132
sbr->kx[0] = sbr->kx[1] = 32; //Typo in spec, kx' inits to 32
133
sbr->data[0].e_a[1] = sbr->data[1].e_a[1] = -1;
142
sbr->kx[0] = sbr->kx[1];
134
144
sbr->data[0].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
135
145
sbr->data[1].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
136
146
/* SBR requires samples to be scaled to +/-32768.0 to work correctly.
137
147
* mdct scale factors are adjusted to scale up from +/-1.0 at analysis
138
148
* and scale back down at synthesis. */
139
mdct_scale = ac->avctx->sample_fmt == AV_SAMPLE_FMT_FLT ? 32768.0f : 1.0f;
140
ff_mdct_init(&sbr->mdct, 7, 1, 1.0 / (64 * mdct_scale));
141
ff_mdct_init(&sbr->mdct_ana, 7, 1, -2.0 * mdct_scale);
149
ff_mdct_init(&sbr->mdct, 7, 1, 1.0 / (64 * 32768.0));
150
ff_mdct_init(&sbr->mdct_ana, 7, 1, -2.0 * 32768.0);
142
151
ff_ps_ctx_init(&sbr->ps);
152
ff_sbrdsp_init(&sbr->dsp);
145
155
av_cold void ff_aac_sbr_ctx_close(SpectralBandReplication *sbr)
910
920
*num_bits_left -= ff_ps_read_data(ac->avctx, gb, &sbr->ps, *num_bits_left);
912
av_log_missing_feature(ac->avctx, "Parametric Stereo is", 0);
922
av_log_missing_feature(ac->avctx, "Parametric Stereo", 0);
913
923
skip_bits_long(gb, *num_bits_left); // bs_fill_bits
914
924
*num_bits_left = 0;
919
av_log_missing_feature(ac->avctx, "Reserved SBR extensions are", 1);
929
av_log_missing_feature(ac->avctx, "Reserved SBR extensions", 1);
920
930
skip_bits_long(gb, *num_bits_left); // bs_fill_bits
921
931
*num_bits_left = 0;
993
1003
if (id_aac == TYPE_SCE || id_aac == TYPE_CCE) {
994
1004
if (read_sbr_single_channel_element(ac, sbr, gb)) {
996
1006
return get_bits_count(gb) - cnt;
998
1008
} else if (id_aac == TYPE_CPE) {
999
1009
if (read_sbr_channel_pair_element(ac, sbr, gb)) {
1001
1011
return get_bits_count(gb) - cnt;
1004
1014
av_log(ac->avctx, AV_LOG_ERROR,
1005
1015
"Invalid bitstream - cannot apply SBR to element type %d\n", id_aac);
1007
1017
return get_bits_count(gb) - cnt;
1009
1019
if (get_bits1(gb)) { // bs_extended_data
1139
1150
* @param x pointer to the beginning of the first sample window
1140
1151
* @param W array of complex-valued samples split into subbands
1142
static void sbr_qmf_analysis(DSPContext *dsp, FFTContext *mdct, const float *in, float *x,
1143
float z[320], float W[2][32][32][2])
1153
static void sbr_qmf_analysis(DSPContext *dsp, FFTContext *mdct,
1154
SBRDSPContext *sbrdsp, const float *in, float *x,
1155
float z[320], float W[2][32][32][2], int buf_idx)
1146
memcpy(W[0], W[1], sizeof(W[0]));
1147
1158
memcpy(x , x+1024, (320-32)*sizeof(x[0]));
1148
1159
memcpy(x+288, in, 1024*sizeof(x[0]));
1149
1160
for (i = 0; i < 32; i++) { // numTimeSlots*RATE = 16*2 as 960 sample frames
1150
1161
// are not supported
1151
1162
dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320);
1152
for (k = 0; k < 64; k++) {
1153
float f = z[k] + z[k + 64] + z[k + 128] + z[k + 192] + z[k + 256];
1158
for (k = 1; k < 32; k++) {
1159
z[64+2*k-1] = z[ k];
1160
z[64+2*k ] = -z[64-k];
1164
sbrdsp->qmf_pre_shuffle(z);
1164
1165
mdct->imdct_half(mdct, z, z+64);
1165
for (k = 0; k < 32; k++) {
1166
W[1][i][k][0] = -z[63-k];
1167
W[1][i][k][1] = z[k];
1166
sbrdsp->qmf_post_shuffle(W[buf_idx][i], z);
1198
1197
X[0][i][32+n] = X[1][i][31-n];
1200
1199
mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
1201
for (n = 0; n < 32; n++) {
1202
v[ n] = mdct_buf[0][63 - 2*n];
1203
v[63 - n] = -mdct_buf[0][62 - 2*n];
1200
sbrdsp->qmf_deint_neg(v, mdct_buf[0]);
1206
for (n = 1; n < 64; n+=2) {
1207
X[1][i][n] = -X[1][i][n];
1202
sbrdsp->neg_odd_64(X[1][i]);
1209
1203
mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
1210
1204
mdct->imdct_half(mdct, mdct_buf[1], X[1][i]);
1211
for (n = 0; n < 64; n++) {
1212
v[ n] = -mdct_buf[0][63 - n] + mdct_buf[1][ n ];
1213
v[127 - n] = mdct_buf[0][63 - n] + mdct_buf[1][ n ];
1205
sbrdsp->qmf_deint_bfly(v, mdct_buf[1], mdct_buf[0]);
1216
dsp->vector_fmul_add(out, v , sbr_qmf_window , zero64, 64 >> div);
1207
fdsp->vector_fmul (out, v , sbr_qmf_window , 64 >> div);
1217
1208
dsp->vector_fmul_add(out, v + ( 192 >> div), sbr_qmf_window + ( 64 >> div), out , 64 >> div);
1218
1209
dsp->vector_fmul_add(out, v + ( 256 >> div), sbr_qmf_window + (128 >> div), out , 64 >> div);
1219
1210
dsp->vector_fmul_add(out, v + ( 448 >> div), sbr_qmf_window + (192 >> div), out , 64 >> div);
1230
static void autocorrelate(const float x[40][2], float phi[3][2][2], int lag)
1233
float real_sum = 0.0f;
1234
float imag_sum = 0.0f;
1236
for (i = 1; i < 38; i++) {
1237
real_sum += x[i][0] * x[i+lag][0] + x[i][1] * x[i+lag][1];
1238
imag_sum += x[i][0] * x[i+lag][1] - x[i][1] * x[i+lag][0];
1240
phi[2-lag][1][0] = real_sum + x[ 0][0] * x[lag][0] + x[ 0][1] * x[lag][1];
1241
phi[2-lag][1][1] = imag_sum + x[ 0][0] * x[lag][1] - x[ 0][1] * x[lag][0];
1243
phi[0][0][0] = real_sum + x[38][0] * x[39][0] + x[38][1] * x[39][1];
1244
phi[0][0][1] = imag_sum + x[38][0] * x[39][1] - x[38][1] * x[39][0];
1247
for (i = 1; i < 38; i++) {
1248
real_sum += x[i][0] * x[i][0] + x[i][1] * x[i][1];
1250
phi[2][1][0] = real_sum + x[ 0][0] * x[ 0][0] + x[ 0][1] * x[ 0][1];
1251
phi[1][0][0] = real_sum + x[38][0] * x[38][0] + x[38][1] * x[38][1];
1255
1221
/** High Frequency Generation (14496-3 sp04 p214+) and Inverse Filtering
1256
1222
* (14496-3 sp04 p214)
1257
1223
* Warning: This routine does not seem numerically stable.
1259
static void sbr_hf_inverse_filter(float (*alpha0)[2], float (*alpha1)[2],
1225
static void sbr_hf_inverse_filter(SBRDSPContext *dsp,
1226
float (*alpha0)[2], float (*alpha1)[2],
1260
1227
const float X_low[32][40][2], int k0)
1263
1230
for (k = 0; k < k0; k++) {
1264
float phi[3][2][2], dk;
1231
LOCAL_ALIGNED_16(float, phi, [3], [2][2]);
1266
autocorrelate(X_low[k], phi, 0);
1267
autocorrelate(X_low[k], phi, 1);
1268
autocorrelate(X_low[k], phi, 2);
1234
dsp->autocorrelate(X_low[k], phi);
1270
1236
dk = phi[2][1][0] * phi[1][0][0] -
1271
1237
(phi[1][1][0] * phi[1][1][0] + phi[1][1][1] * phi[1][1][1]) / 1.000001f;
1341
1308
memset(X_low, 0, 32*sizeof(*X_low));
1342
1309
for (k = 0; k < sbr->kx[1]; k++) {
1343
1310
for (i = t_HFGen; i < i_f + t_HFGen; i++) {
1344
X_low[k][i][0] = W[1][i - t_HFGen][k][0];
1345
X_low[k][i][1] = W[1][i - t_HFGen][k][1];
1311
X_low[k][i][0] = W[buf_idx][i - t_HFGen][k][0];
1312
X_low[k][i][1] = W[buf_idx][i - t_HFGen][k][1];
1315
buf_idx = 1-buf_idx;
1348
1316
for (k = 0; k < sbr->kx[0]; k++) {
1349
1317
for (i = 0; i < t_HFGen; i++) {
1350
X_low[k][i][0] = W[0][i + i_f - t_HFGen][k][0];
1351
X_low[k][i][1] = W[0][i + i_f - t_HFGen][k][1];
1318
X_low[k][i][0] = W[buf_idx][i + i_f - t_HFGen][k][0];
1319
X_low[k][i][1] = W[buf_idx][i + i_f - t_HFGen][k][1];
1381
alpha[0] = alpha1[p][0] * bw_array[g] * bw_array[g];
1382
alpha[1] = alpha1[p][1] * bw_array[g] * bw_array[g];
1383
alpha[2] = alpha0[p][0] * bw_array[g];
1384
alpha[3] = alpha0[p][1] * bw_array[g];
1386
for (i = 2 * t_env[0]; i < 2 * t_env[bs_num_env]; i++) {
1387
const int idx = i + ENVELOPE_ADJUSTMENT_OFFSET;
1389
X_low[p][idx - 2][0] * alpha[0] -
1390
X_low[p][idx - 2][1] * alpha[1] +
1391
X_low[p][idx - 1][0] * alpha[2] -
1392
X_low[p][idx - 1][1] * alpha[3] +
1395
X_low[p][idx - 2][1] * alpha[0] +
1396
X_low[p][idx - 2][0] * alpha[1] +
1397
X_low[p][idx - 1][1] * alpha[2] +
1398
X_low[p][idx - 1][0] * alpha[3] +
1348
sbr->dsp.hf_gen(X_high[k] + ENVELOPE_ADJUSTMENT_OFFSET,
1349
X_low[p] + ENVELOPE_ADJUSTMENT_OFFSET,
1350
alpha0[p], alpha1[p], bw_array[g],
1351
2 * t_env[0], 2 * t_env[bs_num_env]);
1403
1354
if (k < sbr->m[1] + sbr->kx[1])
1647
1598
for (e = 0; e < ch_data->bs_num_env; e++) {
1648
1599
for (i = 2 * ch_data->t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) {
1649
1600
int phi_sign = (1 - 2*(kx & 1));
1601
LOCAL_ALIGNED_16(float, g_filt_tab, [48]);
1602
LOCAL_ALIGNED_16(float, q_filt_tab, [48]);
1603
float *g_filt, *q_filt;
1651
1605
if (h_SL && e != e_a[0] && e != e_a[1]) {
1606
g_filt = g_filt_tab;
1607
q_filt = q_filt_tab;
1652
1608
for (m = 0; m < m_max; m++) {
1653
1609
const int idx1 = i + h_SL;
1654
float g_filt = 0.0f;
1655
for (j = 0; j <= h_SL; j++)
1656
g_filt += g_temp[idx1 - j][m] * h_smooth[j];
1657
Y[1][i][m + kx][0] =
1658
X_high[m + kx][i + ENVELOPE_ADJUSTMENT_OFFSET][0] * g_filt;
1659
Y[1][i][m + kx][1] =
1660
X_high[m + kx][i + ENVELOPE_ADJUSTMENT_OFFSET][1] * g_filt;
1612
for (j = 0; j <= h_SL; j++) {
1613
g_filt[m] += g_temp[idx1 - j][m] * h_smooth[j];
1614
q_filt[m] += q_temp[idx1 - j][m] * h_smooth[j];
1663
for (m = 0; m < m_max; m++) {
1664
const float g_filt = g_temp[i + h_SL][m];
1665
Y[1][i][m + kx][0] =
1666
X_high[m + kx][i + ENVELOPE_ADJUSTMENT_OFFSET][0] * g_filt;
1667
Y[1][i][m + kx][1] =
1668
X_high[m + kx][i + ENVELOPE_ADJUSTMENT_OFFSET][1] * g_filt;
1618
g_filt = g_temp[i + h_SL];
1622
sbr->dsp.hf_g_filt(Y1[i] + kx, X_high + kx, g_filt, m_max,
1623
i + ENVELOPE_ADJUSTMENT_OFFSET);
1672
1625
if (e != e_a[0] && e != e_a[1]) {
1673
for (m = 0; m < m_max; m++) {
1674
indexnoise = (indexnoise + 1) & 0x1ff;
1675
if (sbr->s_m[e][m]) {
1676
Y[1][i][m + kx][0] +=
1677
sbr->s_m[e][m] * phi[0][indexsine];
1678
Y[1][i][m + kx][1] +=
1679
sbr->s_m[e][m] * (phi[1][indexsine] * phi_sign);
1683
const int idx1 = i + h_SL;
1685
for (j = 0; j <= h_SL; j++)
1686
q_filt += q_temp[idx1 - j][m] * h_smooth[j];
1688
q_filt = q_temp[i][m];
1690
Y[1][i][m + kx][0] +=
1691
q_filt * sbr_noise_table[indexnoise][0];
1692
Y[1][i][m + kx][1] +=
1693
q_filt * sbr_noise_table[indexnoise][1];
1695
phi_sign = -phi_sign;
1626
sbr->dsp.hf_apply_noise[indexsine](Y1[i] + kx, sbr->s_m[e],
1698
indexnoise = (indexnoise + m_max) & 0x1ff;
1699
1630
for (m = 0; m < m_max; m++) {
1700
Y[1][i][m + kx][0] +=
1701
1632
sbr->s_m[e][m] * phi[0][indexsine];
1702
Y[1][i][m + kx][1] +=
1703
1634
sbr->s_m[e][m] * (phi[1][indexsine] * phi_sign);
1704
1635
phi_sign = -phi_sign;
1638
indexnoise = (indexnoise + m_max) & 0x1ff;
1707
1639
indexsine = (indexsine + 1) & 3;
1714
1646
void ff_sbr_apply(AACContext *ac, SpectralBandReplication *sbr, int id_aac,
1715
1647
float* L, float* R)
1717
int downsampled = ac->m4ac.ext_sample_rate < sbr->sample_rate;
1649
int downsampled = ac->oc[1].m4ac.ext_sample_rate < sbr->sample_rate;
1719
1651
int nch = (id_aac == TYPE_CPE) ? 2 : 1;
1654
if (!sbr->kx_and_m_pushed) {
1655
sbr->kx[0] = sbr->kx[1];
1656
sbr->m[0] = sbr->m[1];
1658
sbr->kx_and_m_pushed = 0;
1721
1661
if (sbr->start) {
1722
1662
sbr_dequant(sbr, id_aac);
1724
1664
for (ch = 0; ch < nch; ch++) {
1725
1665
/* decode channel */
1726
sbr_qmf_analysis(&ac->dsp, &sbr->mdct_ana, ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
1666
sbr_qmf_analysis(&ac->dsp, &sbr->mdct_ana, &sbr->dsp, ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
1727
1667
(float*)sbr->qmf_filter_scratch,
1729
sbr_lf_gen(ac, sbr, sbr->X_low, sbr->data[ch].W);
1668
sbr->data[ch].W, sbr->data[ch].Ypos);
1669
sbr_lf_gen(ac, sbr, sbr->X_low, sbr->data[ch].W, sbr->data[ch].Ypos);
1670
sbr->data[ch].Ypos ^= 1;
1730
1671
if (sbr->start) {
1731
sbr_hf_inverse_filter(sbr->alpha0, sbr->alpha1, sbr->X_low, sbr->k[0]);
1672
sbr_hf_inverse_filter(&sbr->dsp, sbr->alpha0, sbr->alpha1, sbr->X_low, sbr->k[0]);
1732
1673
sbr_chirp(sbr, &sbr->data[ch]);
1733
1674
sbr_hf_gen(ac, sbr, sbr->X_high, sbr->X_low, sbr->alpha0, sbr->alpha1,
1734
1675
sbr->data[ch].bw_array, sbr->data[ch].t_env,
1735
1676
sbr->data[ch].bs_num_env);
1738
sbr_mapping(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
1739
sbr_env_estimate(sbr->e_curr, sbr->X_high, sbr, &sbr->data[ch]);
1740
sbr_gain_calc(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
1741
sbr_hf_assemble(sbr->data[ch].Y, sbr->X_high, sbr, &sbr->data[ch],
1679
err = sbr_mapping(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
1681
sbr_env_estimate(sbr->e_curr, sbr->X_high, sbr, &sbr->data[ch]);
1682
sbr_gain_calc(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
1683
sbr_hf_assemble(sbr->data[ch].Y[sbr->data[ch].Ypos],
1684
sbr->X_high, sbr, &sbr->data[ch],
1745
1689
/* synthesis */
1746
sbr_x_gen(sbr, sbr->X[ch], sbr->X_low, sbr->data[ch].Y, ch);
1690
sbr_x_gen(sbr, sbr->X[ch],
1691
sbr->data[ch].Y[1-sbr->data[ch].Ypos],
1692
sbr->data[ch].Y[ sbr->data[ch].Ypos],
1749
if (ac->m4ac.ps == 1) {
1696
if (ac->oc[1].m4ac.ps == 1) {
1750
1697
if (sbr->ps.start) {
1751
1698
ff_ps_apply(ac->avctx, &sbr->ps, sbr->X[0], sbr->X[1], sbr->kx[1] + sbr->m[1]);
1758
sbr_qmf_synthesis(&ac->dsp, &sbr->mdct, L, sbr->X[0], sbr->qmf_filter_scratch,
1705
sbr_qmf_synthesis(&ac->dsp, &sbr->mdct, &sbr->dsp, &ac->fdsp,
1706
L, sbr->X[0], sbr->qmf_filter_scratch,
1759
1707
sbr->data[0].synthesis_filterbank_samples,
1760
1708
&sbr->data[0].synthesis_filterbank_samples_offset,
1763
sbr_qmf_synthesis(&ac->dsp, &sbr->mdct, R, sbr->X[1], sbr->qmf_filter_scratch,
1711
sbr_qmf_synthesis(&ac->dsp, &sbr->mdct, &sbr->dsp, &ac->fdsp,
1712
R, sbr->X[1], sbr->qmf_filter_scratch,
1764
1713
sbr->data[1].synthesis_filterbank_samples,
1765
1714
&sbr->data[1].synthesis_filterbank_samples_offset,