44 #define ENVELOPE_ADJUSTMENT_OFFSET 2
45 #define NOISE_FLOOR_OFFSET 6.0f
83 { 60, 60, 24, 24, 31, 31, 12, 12, 31, 12 };
85 #define SBR_INIT_VLC_STATIC(num, size) \
86 INIT_VLC_STATIC(&vlc_sbr[num], 9, sbr_tmp[num].table_size / sbr_tmp[num].elem_size, \
87 sbr_tmp[num].sbr_bits , 1, 1, \
88 sbr_tmp[num].sbr_codes, sbr_tmp[num].elem_size, sbr_tmp[num].elem_size, \
91 #define SBR_VLC_ROW(name) \
92 { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
100 const void *sbr_codes, *sbr_bits;
101 const unsigned int table_size, elem_size;
127 for (n = 1; n < 320; n++)
132 for (n = 0; n < 320; n++)
153 sbr->
kx[0] = sbr->
kx[1];
175 return *(
const int16_t *)a - *(
const int16_t *)
b;
181 for (i = 0; i <= last_el; i++)
182 if (table[i] == needle)
192 static const float bands_warped[3] = { 1.32715174233856803909f,
193 1.18509277094158210129f,
194 1.11987160404675912501f };
195 const float lim_bands_per_octave_warped = bands_warped[sbr->
bs_limiter_bands - 1];
196 int16_t patch_borders[7];
199 patch_borders[0] = sbr->
kx[1];
206 memcpy(sbr->
f_tablelim + sbr->
n[0] + 1, patch_borders + 1,
207 (sbr->
num_patches - 1) *
sizeof(patch_borders[0]));
214 while (out < sbr->f_tablelim + sbr->
n_lim) {
215 if (*in >= *
out * lim_bands_per_octave_warped) {
217 }
else if (*in == *
out ||
257 if (bs_header_extra_1) {
271 if (bs_header_extra_2) {
291 int i,
min = array[0];
292 for (i = 1; i < nel; i++)
293 min =
FFMIN(array[i], min);
299 int k, previous, present;
302 base =
powf((
float)stop / start, 1.0f / num_bands);
306 for (k = 0; k < num_bands-1; k++) {
309 bands[k] = present - previous;
312 bands[num_bands-1] = stop - previous;
322 if (bs_xover_band >= n_master) {
324 "Invalid bitstream, crossover band index beyond array bounds: %d\n",
335 unsigned int temp, max_qmf_subbands;
336 unsigned int start_min, stop_min;
338 const int8_t *sbr_offset_ptr;
361 case 44100:
case 48000:
case 64000:
364 case 88200:
case 96000:
case 128000:
case 176400:
case 192000:
369 "Unsupported sample rate for SBR: %d\n", sbr->
sample_rate);
379 sbr->
k[2] = stop_min;
383 sbr->
k[2] += stop_dk[k];
385 sbr->
k[2] = 2*sbr->
k[0];
387 sbr->
k[2] = 3*sbr->
k[0];
393 sbr->
k[2] =
FFMIN(64, sbr->
k[2]);
397 max_qmf_subbands = 48;
399 max_qmf_subbands = 35;
401 max_qmf_subbands = 32;
405 if (sbr->
k[2] - sbr->
k[0] > max_qmf_subbands) {
407 "Invalid bitstream, too many QMF subbands: %d\n", sbr->
k[2] - sbr->
k[0]);
415 sbr->
n_master = ((sbr->
k[2] - sbr->
k[0] + (dk&2)) >> dk) << 1;
419 for (k = 1; k <= sbr->
n_master; k++)
422 k2diff = sbr->
k[2] - sbr->
k[0] - sbr->
n_master * dk;
431 for (k = 1; k <= sbr->
n_master; k++)
436 int two_regions, num_bands_0;
437 int vdk0_max, vdk1_min;
440 if (49 * sbr->
k[2] > 110 * sbr->
k[0]) {
442 sbr->
k[1] = 2 * sbr->
k[0];
445 sbr->
k[1] = sbr->
k[2];
448 num_bands_0 =
lrintf(half_bands *
log2f(sbr->
k[1] / (
float)sbr->
k[0])) * 2;
450 if (num_bands_0 <= 0) {
460 vdk0_max = vk0[num_bands_0];
463 for (k = 1; k <= num_bands_0; k++) {
473 float invwarp = spectrum->
bs_alter_scale ? 0.76923076923076923077f
475 int num_bands_1 =
lrintf(half_bands * invwarp *
476 log2f(sbr->
k[2] / (
float)sbr->
k[1])) * 2;
482 if (vdk1_min < vdk0_max) {
485 change =
FFMIN(vdk0_max - vk1[1], (vk1[num_bands_1] - vk1[1]) >> 1);
487 vk1[num_bands_1] -= change;
493 for (k = 1; k <= num_bands_1; k++) {
501 sbr->
n_master = num_bands_0 + num_bands_1;
505 (num_bands_0 + 1) *
sizeof(sbr->
f_master[0]));
506 memcpy(&sbr->
f_master[num_bands_0 + 1], vk1 + 1,
507 num_bands_1 *
sizeof(sbr->
f_master[0]));
525 int usb = sbr->
kx[1];
530 if (goal_sb < sbr->kx[1] + sbr->
m[1]) {
531 for (k = 0; sbr->
f_master[k] < goal_sb; k++) ;
537 for (i = k; i == k || sb > (sbr->
k[0] - 1 + msb - odd); i--) {
539 odd = (sb + sbr->
k[0]) & 1;
563 }
while (sb != sbr->
kx[1] + sbr->
m[1]);
577 sbr->
n[0] = (sbr->
n[1] + 1) >> 1;
580 (sbr->
n[1] + 1) *
sizeof(sbr->
f_master[0]));
585 if (sbr->
kx[1] + sbr->
m[1] > 64) {
587 "Stop frequency border too high: %d\n", sbr->
kx[1] + sbr->
m[1]);
590 if (sbr->
kx[1] > 32) {
596 temp = sbr->
n[1] & 1;
597 for (k = 1; k <= sbr->
n[0]; k++)
601 log2f(sbr->
k[2] / (
float)sbr->
kx[1])));
609 for (k = 1; k <= sbr->
n_q; k++) {
610 temp += (sbr->
n[0] -
temp) / (sbr->
n_q + 1 - k);
629 for (i = 0; i < elements; i++) {
643 unsigned bs_pointer = 0;
645 int abs_bord_trail = 16;
646 int num_rel_lead, num_rel_trail;
647 unsigned bs_num_env_old = ch_data->
bs_num_env;
662 "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
667 ch_data->
t_env[0] = 0;
670 abs_bord_trail = (abs_bord_trail + (ch_data->
bs_num_env >> 1)) /
672 for (i = 0; i < num_rel_lead; i++)
673 ch_data->
t_env[i + 1] = ch_data->
t_env[i] + abs_bord_trail;
683 ch_data->
t_env[0] = 0;
686 for (i = 0; i < num_rel_trail; i++)
701 for (i = 0; i < num_rel_lead; i++)
713 ch_data->
bs_num_env = num_rel_lead + num_rel_trail + 1;
717 "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n",
724 for (i = 0; i < num_rel_lead; i++)
726 for (i = 0; i < num_rel_trail; i++)
738 "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n",
744 if (ch_data->
t_env[i-1] > ch_data->
t_env[i]) {
752 ch_data->
t_q[0] = ch_data->
t_env[0];
763 else if (bs_pointer == 1)
766 idx = bs_pointer - 1;
768 ch_data->
t_q[1] = ch_data->
t_env[idx];
771 ch_data->
e_a[0] = -(ch_data->
e_a[1] != bs_num_env_old);
772 ch_data->
e_a[1] = -1;
776 ch_data->
e_a[1] = bs_pointer - 1;
790 memcpy(dst->
t_q, src->
t_q,
sizeof(dst->
t_q));
795 dst->
e_a[1] = src->
e_a[1];
813 for (i = 0; i < sbr->
n_q; i++)
822 VLC_TYPE (*t_huff)[2], (*f_huff)[2];
825 const int odd = sbr->
n[1] & 1;
861 for (j = 0; j < sbr->
n[ch_data->
bs_freq_res[i + 1]]; j++)
864 for (j = 0; j < sbr->
n[ch_data->
bs_freq_res[i + 1]]; j++) {
869 for (j = 0; j < sbr->
n[ch_data->
bs_freq_res[i + 1]]; j++) {
870 k = j ? 2*j - odd : 0;
876 for (j = 1; j < sbr->
n[ch_data->
bs_freq_res[i + 1]]; j++)
890 VLC_TYPE (*t_huff)[2], (*f_huff)[2];
908 for (j = 0; j < sbr->
n_q; j++)
912 for (j = 1; j < sbr->
n_q; j++)
924 int bs_extension_id,
int *num_bits_left)
926 switch (bs_extension_id) {
929 av_log(ac->
avctx,
AV_LOG_ERROR,
"Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
944 if (bs_extension_id || *num_bits_left > 16 ||
show_bits(gb, *num_bits_left))
1031 "Invalid bitstream - cannot apply SBR to element type %d\n", id_aac);
1036 int num_bits_left =
get_bits(gb, 4);
1037 if (num_bits_left == 15)
1040 num_bits_left <<= 3;
1041 while (num_bits_left > 7) {
1045 if (num_bits_left < 0) {
1048 if (num_bits_left > 0)
1063 "SBR reset failed. Switching SBR to pure upsampling mode.\n");
1079 unsigned int num_sbr_bits = 0, num_align_bits;
1080 unsigned bytes_read;
1097 sbr->
kx[0] = sbr->
kx[1];
1098 sbr->
m[0] = sbr->
m[1];
1111 num_align_bits = ((cnt << 3) - 4 - num_sbr_bits) & 7;
1112 bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3);
1114 if (bytes_read > cnt) {
1116 "Expected to read %d SBR bytes actually read %d.\n", cnt, bytes_read);
1139 fac = temp1 / (1.0f + temp2);
1145 for (k = 0; k < sbr->
n_q; k++) {
1153 fac = temp1 / (1.0f + temp2);
1159 for (ch = 0; ch < (id_aac ==
TYPE_CPE) + 1; ch++) {
1172 for (k = 0; k < sbr->
n_q; k++)
1185 #ifndef sbr_qmf_analysis
1188 float z[320],
float W[2][32][32][2],
int buf_idx)
1191 memcpy(x , x+1024, (320-32)*
sizeof(x[0]));
1192 memcpy(x+288, in, 1024*
sizeof(x[0]));
1193 for (i = 0; i < 32; i++) {
1209 #ifndef sbr_qmf_synthesis
1212 float *
out,
float X[2][38][64],
1213 float mdct_buf[2][64],
1214 float *
v0,
int *v_off,
const unsigned int div)
1218 const int step = 128 >> div;
1220 for (i = 0; i < 32; i++) {
1221 if (*v_off < step) {
1222 int saved_samples = (1280 - 128) >> div;
1230 for (n = 0; n < 32; n++) {
1231 X[0][i][
n] = -X[0][i][
n];
1232 X[0][i][32+
n] = X[1][i][31-
n];
1234 mdct->
imdct_half(mdct, mdct_buf[0], X[0][i]);
1238 mdct->
imdct_half(mdct, mdct_buf[0], X[0][i]);
1239 mdct->
imdct_half(mdct, mdct_buf[1], X[1][i]);
1242 dsp->
vector_fmul (out, v , sbr_qmf_window , 64 >> div);
1243 dsp->
vector_fmul_add(out, v + ( 192 >> div), sbr_qmf_window + ( 64 >> div), out , 64 >> div);
1244 dsp->
vector_fmul_add(out, v + ( 256 >> div), sbr_qmf_window + (128 >> div), out , 64 >> div);
1245 dsp->
vector_fmul_add(out, v + ( 448 >> div), sbr_qmf_window + (192 >> div), out , 64 >> div);
1246 dsp->
vector_fmul_add(out, v + ( 512 >> div), sbr_qmf_window + (256 >> div), out , 64 >> div);
1247 dsp->
vector_fmul_add(out, v + ( 704 >> div), sbr_qmf_window + (320 >> div), out , 64 >> div);
1248 dsp->
vector_fmul_add(out, v + ( 768 >> div), sbr_qmf_window + (384 >> div), out , 64 >> div);
1249 dsp->
vector_fmul_add(out, v + ( 960 >> div), sbr_qmf_window + (448 >> div), out , 64 >> div);
1250 dsp->
vector_fmul_add(out, v + (1024 >> div), sbr_qmf_window + (512 >> div), out , 64 >> div);
1251 dsp->
vector_fmul_add(out, v + (1216 >> div), sbr_qmf_window + (576 >> div), out , 64 >> div);
1262 float (*alpha0)[2],
float (*alpha1)[2],
1263 const float X_low[32][40][2],
int k0)
1266 for (k = 0; k < k0; k++) {
1272 dk = phi[2][1][0] * phi[1][0][0] -
1273 (phi[1][1][0] * phi[1][1][0] + phi[1][1][1] * phi[1][1][1]) / 1.000001f;
1279 float temp_real, temp_im;
1280 temp_real = phi[0][0][0] * phi[1][1][0] -
1281 phi[0][0][1] * phi[1][1][1] -
1282 phi[0][1][0] * phi[1][0][0];
1283 temp_im = phi[0][0][0] * phi[1][1][1] +
1284 phi[0][0][1] * phi[1][1][0] -
1285 phi[0][1][1] * phi[1][0][0];
1287 alpha1[k][0] = temp_real / dk;
1288 alpha1[k][1] = temp_im / dk;
1291 if (!phi[1][0][0]) {
1295 float temp_real, temp_im;
1296 temp_real = phi[0][0][0] + alpha1[k][0] * phi[1][1][0] +
1297 alpha1[k][1] * phi[1][1][1];
1298 temp_im = phi[0][0][1] + alpha1[k][1] * phi[1][1][0] -
1299 alpha1[k][0] * phi[1][1][1];
1301 alpha0[k][0] = -temp_real / phi[1][0][0];
1302 alpha0[k][1] = -temp_im / phi[1][0][0];
1305 if (alpha1[k][0] * alpha1[k][0] + alpha1[k][1] * alpha1[k][1] >= 16.0f ||
1306 alpha0[k][0] * alpha0[k][0] + alpha0[k][1] * alpha0[k][1] >= 16.0f) {
1320 static const float bw_tab[] = { 0.0f, 0.75f, 0.9f, 0.98f };
1322 for (i = 0; i < sbr->
n_q; i++) {
1328 if (new_bw < ch_data->bw_array[i]) {
1329 new_bw = 0.75f * new_bw + 0.25f * ch_data->
bw_array[i];
1331 new_bw = 0.90625f * new_bw + 0.09375f * ch_data->
bw_array[i];
1332 ch_data->
bw_array[i] = new_bw < 0.015625f ? 0.0f : new_bw;
1338 float X_low[32][40][2],
const float W[2][32][32][2],
1342 const int t_HFGen = 8;
1344 memset(X_low, 0, 32*
sizeof(*X_low));
1345 for (k = 0; k < sbr->
kx[1]; k++) {
1346 for (i = t_HFGen; i < i_f + t_HFGen; i++) {
1347 X_low[k][i][0] = W[buf_idx][i - t_HFGen][k][0];
1348 X_low[k][i][1] = W[buf_idx][i - t_HFGen][k][1];
1351 buf_idx = 1-buf_idx;
1352 for (k = 0; k < sbr->
kx[0]; k++) {
1353 for (i = 0; i < t_HFGen; i++) {
1354 X_low[k][i][0] = W[buf_idx][i + i_f - t_HFGen][k][0];
1355 X_low[k][i][1] = W[buf_idx][i + i_f - t_HFGen][k][1];
1363 float X_high[64][40][2],
const float X_low[32][40][2],
1364 const float (*alpha0)[2],
const float (*alpha1)[2],
1365 const float bw_array[5],
const uint8_t *t_env,
1380 "ERROR : no subband found for frequency %d\n", k);
1386 alpha0[p], alpha1[p], bw_array[g],
1387 2 * t_env[0], 2 * t_env[bs_num_env]);
1390 if (k < sbr->
m[1] + sbr->
kx[1])
1391 memset(X_high + k, 0, (sbr->
m[1] + sbr->
kx[1] - k) *
sizeof(*X_high));
1398 const float Y0[38][64][2],
const float Y1[38][64][2],
1399 const float X_low[32][40][2],
int ch)
1404 memset(X, 0, 2*
sizeof(*X));
1405 for (k = 0; k < sbr->
kx[0]; k++) {
1406 for (i = 0; i < i_Temp; i++) {
1411 for (; k < sbr->
kx[0] + sbr->
m[0]; k++) {
1412 for (i = 0; i < i_Temp; i++) {
1413 X[0][i][k] = Y0[i + i_f][k][0];
1414 X[1][i][k] = Y0[i + i_f][k][1];
1418 for (k = 0; k < sbr->
kx[1]; k++) {
1419 for (i = i_Temp; i < 38; i++) {
1424 for (; k < sbr->
kx[1] + sbr->
m[1]; k++) {
1425 for (i = i_Temp; i < i_f; i++) {
1426 X[0][i][k] = Y1[i][k][0];
1427 X[1][i][k] = Y1[i][k][1];
1443 const unsigned int ilim = sbr->
n[ch_data->
bs_freq_res[e + 1]];
1447 if (sbr->
kx[1] != table[0]) {
1449 "Derived frequency tables were not regenerated.\n");
1453 for (i = 0; i < ilim; i++)
1454 for (m = table[i]; m < table[i + 1]; m++)
1459 for (i = 0; i < sbr->
n_q; i++)
1460 for (m = sbr->
f_tablenoise[i]; m < sbr->f_tablenoise[i + 1]; m++)
1463 for (i = 0; i < sbr->
n[1]; i++) {
1465 const unsigned int m_midpoint =
1469 (e >= e_a[1] || (ch_data->
s_indexmapped[0][m_midpoint - sbr->
kx[1]] == 1));
1473 for (i = 0; i < ilim; i++) {
1474 int additional_sinusoid_present = 0;
1475 for (m = table[i]; m < table[i + 1]; m++) {
1477 additional_sinusoid_present = 1;
1481 memset(&sbr->
s_mapped[e][table[i] - sbr->
kx[1]], additional_sinusoid_present,
1482 (table[i + 1] - table[i]) *
sizeof(sbr->
s_mapped[e][0]));
1495 int kx1 = sbr->
kx[1];
1499 const float recip_env_size = 0.5f / (ch_data->
t_env[e + 1] - ch_data->
t_env[e]);
1503 for (m = 0; m < sbr->
m[1]; m++) {
1504 float sum = sbr->
dsp.
sum_square(X_high[m+kx1] + ilb, iub - ilb);
1505 e_curr[e][
m] = sum * recip_env_size;
1512 const int env_size = 2 * (ch_data->
t_env[e + 1] - ch_data->
t_env[e]);
1517 for (p = 0; p < sbr->
n[ch_data->
bs_freq_res[e + 1]]; p++) {
1519 const int den = env_size * (table[p + 1] - table[p]);
1521 for (k = table[p]; k < table[p + 1]; k++) {
1525 for (k = table[p]; k < table[p + 1]; k++) {
1526 e_curr[e][k - kx1] = sum;
1538 SBRData *ch_data,
const int e_a[2])
1542 static const float limgain[4] = { 0.70795, 1.0, 1.41254, 10000000000 };
1545 int delta = !((e == e_a[1]) || (e == e_a[0]));
1546 for (k = 0; k < sbr->
n_lim; k++) {
1547 float gain_boost, gain_max;
1548 float sum[2] = { 0.0f, 0.0f };
1549 for (m = sbr->
f_tablelim[k] - sbr->
kx[1]; m < sbr->f_tablelim[k + 1] - sbr->
kx[1]; m++) {
1555 ((1.0f + sbr->
e_curr[e][m]) *
1556 (1.0f + sbr->
q_mapped[e][m] * delta)));
1559 ((1.0f + sbr->
e_curr[e][m]) *
1563 for (m = sbr->
f_tablelim[k] - sbr->
kx[1]; m < sbr->f_tablelim[k + 1] - sbr->
kx[1]; m++) {
1567 gain_max = limgain[sbr->
bs_limiter_gains] * sqrtf((FLT_EPSILON + sum[0]) / (FLT_EPSILON + sum[1]));
1568 gain_max =
FFMIN(100000.f, gain_max);
1569 for (m = sbr->
f_tablelim[k] - sbr->
kx[1]; m < sbr->f_tablelim[k + 1] - sbr->
kx[1]; m++) {
1570 float q_m_max = sbr->
q_m[e][
m] * gain_max / sbr->
gain[e][
m];
1574 sum[0] = sum[1] = 0.0f;
1575 for (m = sbr->
f_tablelim[k] - sbr->
kx[1]; m < sbr->f_tablelim[k + 1] - sbr->
kx[1]; m++) {
1579 + (delta && !sbr->
s_m[e][
m]) * sbr->
q_m[e][m] * sbr->
q_m[e][m];
1581 gain_boost = sqrtf((FLT_EPSILON + sum[0]) / (FLT_EPSILON + sum[1]));
1582 gain_boost =
FFMIN(1.584893192f, gain_boost);
1583 for (m = sbr->
f_tablelim[k] - sbr->
kx[1]; m < sbr->f_tablelim[k + 1] - sbr->
kx[1]; m++) {
1584 sbr->
gain[e][
m] *= gain_boost;
1585 sbr->
q_m[e][
m] *= gain_boost;
1586 sbr->
s_m[e][
m] *= gain_boost;
1594 const float X_high[64][40][2],
1600 const int kx = sbr->
kx[1];
1601 const int m_max = sbr->
m[1];
1602 static const float h_smooth[5] = {
1609 float (*g_temp)[48] = ch_data->
g_temp, (*q_temp)[48] = ch_data->
q_temp;
1614 for (i = 0; i < h_SL; i++) {
1615 memcpy(g_temp[i + 2*ch_data->
t_env[0]], sbr->
gain[0], m_max *
sizeof(sbr->
gain[0][0]));
1616 memcpy(q_temp[i + 2*ch_data->
t_env[0]], sbr->
q_m[0], m_max *
sizeof(sbr->
q_m[0][0]));
1624 for (i = 2 * ch_data->
t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) {
1625 memcpy(g_temp[h_SL + i], sbr->
gain[e], m_max *
sizeof(sbr->
gain[0][0]));
1626 memcpy(q_temp[h_SL + i], sbr->
q_m[e], m_max *
sizeof(sbr->
q_m[0][0]));
1631 for (i = 2 * ch_data->
t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) {
1634 float *g_filt, *q_filt;
1636 if (h_SL && e != e_a[0] && e != e_a[1]) {
1637 g_filt = g_filt_tab;
1638 q_filt = q_filt_tab;
1639 for (m = 0; m < m_max; m++) {
1640 const int idx1 = i + h_SL;
1643 for (j = 0; j <= h_SL; j++) {
1644 g_filt[
m] += g_temp[idx1 - j][
m] * h_smooth[j];
1645 q_filt[
m] += q_temp[idx1 - j][
m] * h_smooth[j];
1649 g_filt = g_temp[i + h_SL];
1653 sbr->
dsp.
hf_g_filt(Y1[i] + kx, X_high + kx, g_filt, m_max,
1656 if (e != e_a[0] && e != e_a[1]) {
1661 int idx = indexsine&1;
1662 int A = (1-((indexsine+(kx & 1))&2));
1663 int B = (A^(-idx)) + idx;
1664 float *
out = &Y1[i][kx][idx];
1665 float *
in = sbr->
s_m[e];
1666 for (m = 0; m+1 < m_max; m+=2) {
1667 out[2*
m ] += in[
m ] *
A;
1668 out[2*m+2] += in[m+1] *
B;
1671 out[2*
m ] += in[
m ] *
A;
1673 indexnoise = (indexnoise + m_max) & 0x1ff;
1674 indexsine = (indexsine + 1) & 3;
1686 int nch = (id_aac ==
TYPE_CPE) ? 2 : 1;
1690 sbr->
kx[0] = sbr->
kx[1];
1691 sbr->
m[0] = sbr->
m[1];
1699 for (ch = 0; ch < nch; ch++) {
1735 memcpy(sbr->
X[1], sbr->
X[0],
sizeof(sbr->
X[0]));