00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00029 #include "aac.h"
00030 #include "sbr.h"
00031 #include "aacsbr.h"
00032 #include "aacsbrdata.h"
00033 #include "fft.h"
00034 #include "aacps.h"
00035 #include "sbrdsp.h"
00036 #include "libavutil/libm.h"
00037 #include "libavutil/avassert.h"
00038
00039 #include <stdint.h>
00040 #include <float.h>
00041 #include <math.h>
00042
00043 #define ENVELOPE_ADJUSTMENT_OFFSET 2
00044 #define NOISE_FLOOR_OFFSET 6.0f
00045
00049 enum {
00050 T_HUFFMAN_ENV_1_5DB,
00051 F_HUFFMAN_ENV_1_5DB,
00052 T_HUFFMAN_ENV_BAL_1_5DB,
00053 F_HUFFMAN_ENV_BAL_1_5DB,
00054 T_HUFFMAN_ENV_3_0DB,
00055 F_HUFFMAN_ENV_3_0DB,
00056 T_HUFFMAN_ENV_BAL_3_0DB,
00057 F_HUFFMAN_ENV_BAL_3_0DB,
00058 T_HUFFMAN_NOISE_3_0DB,
00059 T_HUFFMAN_NOISE_BAL_3_0DB,
00060 };
00061
00065 enum {
00066 FIXFIX,
00067 FIXVAR,
00068 VARFIX,
00069 VARVAR,
00070 };
00071
00072 enum {
00073 EXTENSION_ID_PS = 2,
00074 };
00075
00076 static VLC vlc_sbr[10];
00077 static const int8_t vlc_sbr_lav[10] =
00078 { 60, 60, 24, 24, 31, 31, 12, 12, 31, 12 };
00079 static const DECLARE_ALIGNED(16, float, zero64)[64];
00080
00081 #define SBR_INIT_VLC_STATIC(num, size) \
00082 INIT_VLC_STATIC(&vlc_sbr[num], 9, sbr_tmp[num].table_size / sbr_tmp[num].elem_size, \
00083 sbr_tmp[num].sbr_bits , 1, 1, \
00084 sbr_tmp[num].sbr_codes, sbr_tmp[num].elem_size, sbr_tmp[num].elem_size, \
00085 size)
00086
00087 #define SBR_VLC_ROW(name) \
00088 { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
00089
00090 av_cold void ff_aac_sbr_init(void)
00091 {
00092 int n;
00093 static const struct {
00094 const void *sbr_codes, *sbr_bits;
00095 const unsigned int table_size, elem_size;
00096 } sbr_tmp[] = {
00097 SBR_VLC_ROW(t_huffman_env_1_5dB),
00098 SBR_VLC_ROW(f_huffman_env_1_5dB),
00099 SBR_VLC_ROW(t_huffman_env_bal_1_5dB),
00100 SBR_VLC_ROW(f_huffman_env_bal_1_5dB),
00101 SBR_VLC_ROW(t_huffman_env_3_0dB),
00102 SBR_VLC_ROW(f_huffman_env_3_0dB),
00103 SBR_VLC_ROW(t_huffman_env_bal_3_0dB),
00104 SBR_VLC_ROW(f_huffman_env_bal_3_0dB),
00105 SBR_VLC_ROW(t_huffman_noise_3_0dB),
00106 SBR_VLC_ROW(t_huffman_noise_bal_3_0dB),
00107 };
00108
00109
00110 SBR_INIT_VLC_STATIC(0, 1098);
00111 SBR_INIT_VLC_STATIC(1, 1092);
00112 SBR_INIT_VLC_STATIC(2, 768);
00113 SBR_INIT_VLC_STATIC(3, 1026);
00114 SBR_INIT_VLC_STATIC(4, 1058);
00115 SBR_INIT_VLC_STATIC(5, 1052);
00116 SBR_INIT_VLC_STATIC(6, 544);
00117 SBR_INIT_VLC_STATIC(7, 544);
00118 SBR_INIT_VLC_STATIC(8, 592);
00119 SBR_INIT_VLC_STATIC(9, 512);
00120
00121 for (n = 1; n < 320; n++)
00122 sbr_qmf_window_us[320 + n] = sbr_qmf_window_us[320 - n];
00123 sbr_qmf_window_us[384] = -sbr_qmf_window_us[384];
00124 sbr_qmf_window_us[512] = -sbr_qmf_window_us[512];
00125
00126 for (n = 0; n < 320; n++)
00127 sbr_qmf_window_ds[n] = sbr_qmf_window_us[2*n];
00128
00129 ff_ps_init();
00130 }
00131
00133 static void sbr_turnoff(SpectralBandReplication *sbr) {
00134 sbr->start = 0;
00135
00136 sbr->kx[1] = 32;
00137 sbr->m[1] = 0;
00138
00139 sbr->data[0].e_a[1] = sbr->data[1].e_a[1] = -1;
00140 memset(&sbr->spectrum_params, -1, sizeof(SpectrumParameters));
00141 }
00142
00143 av_cold void ff_aac_sbr_ctx_init(AACContext *ac, SpectralBandReplication *sbr)
00144 {
00145 float mdct_scale;
00146 if(sbr->mdct.mdct_bits)
00147 return;
00148 sbr->kx[0] = sbr->kx[1];
00149 sbr_turnoff(sbr);
00150 sbr->data[0].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
00151 sbr->data[1].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
00152
00153
00154
00155 mdct_scale = ac->avctx->sample_fmt == AV_SAMPLE_FMT_FLT ? 32768.0f : 1.0f;
00156 ff_mdct_init(&sbr->mdct, 7, 1, 1.0 / (64 * mdct_scale));
00157 ff_mdct_init(&sbr->mdct_ana, 7, 1, -2.0 * mdct_scale);
00158 ff_ps_ctx_init(&sbr->ps);
00159 ff_sbrdsp_init(&sbr->dsp);
00160 }
00161
00162 av_cold void ff_aac_sbr_ctx_close(SpectralBandReplication *sbr)
00163 {
00164 ff_mdct_end(&sbr->mdct);
00165 ff_mdct_end(&sbr->mdct_ana);
00166 }
00167
00168 static int qsort_comparison_function_int16(const void *a, const void *b)
00169 {
00170 return *(const int16_t *)a - *(const int16_t *)b;
00171 }
00172
00173 static inline int in_table_int16(const int16_t *table, int last_el, int16_t needle)
00174 {
00175 int i;
00176 for (i = 0; i <= last_el; i++)
00177 if (table[i] == needle)
00178 return 1;
00179 return 0;
00180 }
00181
00183 static void sbr_make_f_tablelim(SpectralBandReplication *sbr)
00184 {
00185 int k;
00186 if (sbr->bs_limiter_bands > 0) {
00187 static const float bands_warped[3] = { 1.32715174233856803909f,
00188 1.18509277094158210129f,
00189 1.11987160404675912501f };
00190 const float lim_bands_per_octave_warped = bands_warped[sbr->bs_limiter_bands - 1];
00191 int16_t patch_borders[7];
00192 uint16_t *in = sbr->f_tablelim + 1, *out = sbr->f_tablelim;
00193
00194 patch_borders[0] = sbr->kx[1];
00195 for (k = 1; k <= sbr->num_patches; k++)
00196 patch_borders[k] = patch_borders[k-1] + sbr->patch_num_subbands[k-1];
00197
00198 memcpy(sbr->f_tablelim, sbr->f_tablelow,
00199 (sbr->n[0] + 1) * sizeof(sbr->f_tablelow[0]));
00200 if (sbr->num_patches > 1)
00201 memcpy(sbr->f_tablelim + sbr->n[0] + 1, patch_borders + 1,
00202 (sbr->num_patches - 1) * sizeof(patch_borders[0]));
00203
00204 qsort(sbr->f_tablelim, sbr->num_patches + sbr->n[0],
00205 sizeof(sbr->f_tablelim[0]),
00206 qsort_comparison_function_int16);
00207
00208 sbr->n_lim = sbr->n[0] + sbr->num_patches - 1;
00209 while (out < sbr->f_tablelim + sbr->n_lim) {
00210 if (*in >= *out * lim_bands_per_octave_warped) {
00211 *++out = *in++;
00212 } else if (*in == *out ||
00213 !in_table_int16(patch_borders, sbr->num_patches, *in)) {
00214 in++;
00215 sbr->n_lim--;
00216 } else if (!in_table_int16(patch_borders, sbr->num_patches, *out)) {
00217 *out = *in++;
00218 sbr->n_lim--;
00219 } else {
00220 *++out = *in++;
00221 }
00222 }
00223 } else {
00224 sbr->f_tablelim[0] = sbr->f_tablelow[0];
00225 sbr->f_tablelim[1] = sbr->f_tablelow[sbr->n[0]];
00226 sbr->n_lim = 1;
00227 }
00228 }
00229
00230 static unsigned int read_sbr_header(SpectralBandReplication *sbr, GetBitContext *gb)
00231 {
00232 unsigned int cnt = get_bits_count(gb);
00233 uint8_t bs_header_extra_1;
00234 uint8_t bs_header_extra_2;
00235 int old_bs_limiter_bands = sbr->bs_limiter_bands;
00236 SpectrumParameters old_spectrum_params;
00237
00238 sbr->start = 1;
00239
00240
00241 memcpy(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters));
00242
00243 sbr->bs_amp_res_header = get_bits1(gb);
00244 sbr->spectrum_params.bs_start_freq = get_bits(gb, 4);
00245 sbr->spectrum_params.bs_stop_freq = get_bits(gb, 4);
00246 sbr->spectrum_params.bs_xover_band = get_bits(gb, 3);
00247 skip_bits(gb, 2);
00248
00249 bs_header_extra_1 = get_bits1(gb);
00250 bs_header_extra_2 = get_bits1(gb);
00251
00252 if (bs_header_extra_1) {
00253 sbr->spectrum_params.bs_freq_scale = get_bits(gb, 2);
00254 sbr->spectrum_params.bs_alter_scale = get_bits1(gb);
00255 sbr->spectrum_params.bs_noise_bands = get_bits(gb, 2);
00256 } else {
00257 sbr->spectrum_params.bs_freq_scale = 2;
00258 sbr->spectrum_params.bs_alter_scale = 1;
00259 sbr->spectrum_params.bs_noise_bands = 2;
00260 }
00261
00262
00263 if (memcmp(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters)))
00264 sbr->reset = 1;
00265
00266 if (bs_header_extra_2) {
00267 sbr->bs_limiter_bands = get_bits(gb, 2);
00268 sbr->bs_limiter_gains = get_bits(gb, 2);
00269 sbr->bs_interpol_freq = get_bits1(gb);
00270 sbr->bs_smoothing_mode = get_bits1(gb);
00271 } else {
00272 sbr->bs_limiter_bands = 2;
00273 sbr->bs_limiter_gains = 2;
00274 sbr->bs_interpol_freq = 1;
00275 sbr->bs_smoothing_mode = 1;
00276 }
00277
00278 if (sbr->bs_limiter_bands != old_bs_limiter_bands && !sbr->reset)
00279 sbr_make_f_tablelim(sbr);
00280
00281 return get_bits_count(gb) - cnt;
00282 }
00283
00284 static int array_min_int16(const int16_t *array, int nel)
00285 {
00286 int i, min = array[0];
00287 for (i = 1; i < nel; i++)
00288 min = FFMIN(array[i], min);
00289 return min;
00290 }
00291
00292 static void make_bands(int16_t* bands, int start, int stop, int num_bands)
00293 {
00294 int k, previous, present;
00295 float base, prod;
00296
00297 base = powf((float)stop / start, 1.0f / num_bands);
00298 prod = start;
00299 previous = start;
00300
00301 for (k = 0; k < num_bands-1; k++) {
00302 prod *= base;
00303 present = lrintf(prod);
00304 bands[k] = present - previous;
00305 previous = present;
00306 }
00307 bands[num_bands-1] = stop - previous;
00308 }
00309
00310 static int check_n_master(AVCodecContext *avctx, int n_master, int bs_xover_band)
00311 {
00312
00313 if (n_master <= 0) {
00314 av_log(avctx, AV_LOG_ERROR, "Invalid n_master: %d\n", n_master);
00315 return -1;
00316 }
00317 if (bs_xover_band >= n_master) {
00318 av_log(avctx, AV_LOG_ERROR,
00319 "Invalid bitstream, crossover band index beyond array bounds: %d\n",
00320 bs_xover_band);
00321 return -1;
00322 }
00323 return 0;
00324 }
00325
00327 static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr,
00328 SpectrumParameters *spectrum)
00329 {
00330 unsigned int temp, max_qmf_subbands;
00331 unsigned int start_min, stop_min;
00332 int k;
00333 const int8_t *sbr_offset_ptr;
00334 int16_t stop_dk[13];
00335
00336 if (sbr->sample_rate < 32000) {
00337 temp = 3000;
00338 } else if (sbr->sample_rate < 64000) {
00339 temp = 4000;
00340 } else
00341 temp = 5000;
00342
00343 start_min = ((temp << 7) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
00344 stop_min = ((temp << 8) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
00345
00346 switch (sbr->sample_rate) {
00347 case 16000:
00348 sbr_offset_ptr = sbr_offset[0];
00349 break;
00350 case 22050:
00351 sbr_offset_ptr = sbr_offset[1];
00352 break;
00353 case 24000:
00354 sbr_offset_ptr = sbr_offset[2];
00355 break;
00356 case 32000:
00357 sbr_offset_ptr = sbr_offset[3];
00358 break;
00359 case 44100: case 48000: case 64000:
00360 sbr_offset_ptr = sbr_offset[4];
00361 break;
00362 case 88200: case 96000: case 128000: case 176400: case 192000:
00363 sbr_offset_ptr = sbr_offset[5];
00364 break;
00365 default:
00366 av_log(ac->avctx, AV_LOG_ERROR,
00367 "Unsupported sample rate for SBR: %d\n", sbr->sample_rate);
00368 return -1;
00369 }
00370
00371 sbr->k[0] = start_min + sbr_offset_ptr[spectrum->bs_start_freq];
00372
00373 if (spectrum->bs_stop_freq < 14) {
00374 sbr->k[2] = stop_min;
00375 make_bands(stop_dk, stop_min, 64, 13);
00376 qsort(stop_dk, 13, sizeof(stop_dk[0]), qsort_comparison_function_int16);
00377 for (k = 0; k < spectrum->bs_stop_freq; k++)
00378 sbr->k[2] += stop_dk[k];
00379 } else if (spectrum->bs_stop_freq == 14) {
00380 sbr->k[2] = 2*sbr->k[0];
00381 } else if (spectrum->bs_stop_freq == 15) {
00382 sbr->k[2] = 3*sbr->k[0];
00383 } else {
00384 av_log(ac->avctx, AV_LOG_ERROR,
00385 "Invalid bs_stop_freq: %d\n", spectrum->bs_stop_freq);
00386 return -1;
00387 }
00388 sbr->k[2] = FFMIN(64, sbr->k[2]);
00389
00390
00391 if (sbr->sample_rate <= 32000) {
00392 max_qmf_subbands = 48;
00393 } else if (sbr->sample_rate == 44100) {
00394 max_qmf_subbands = 35;
00395 } else if (sbr->sample_rate >= 48000)
00396 max_qmf_subbands = 32;
00397
00398 if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) {
00399 av_log(ac->avctx, AV_LOG_ERROR,
00400 "Invalid bitstream, too many QMF subbands: %d\n", sbr->k[2] - sbr->k[0]);
00401 return -1;
00402 }
00403
00404 if (!spectrum->bs_freq_scale) {
00405 int dk, k2diff;
00406
00407 dk = spectrum->bs_alter_scale + 1;
00408 sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1;
00409 if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
00410 return -1;
00411
00412 for (k = 1; k <= sbr->n_master; k++)
00413 sbr->f_master[k] = dk;
00414
00415 k2diff = sbr->k[2] - sbr->k[0] - sbr->n_master * dk;
00416 if (k2diff < 0) {
00417 sbr->f_master[1]--;
00418 sbr->f_master[2]-= (k2diff < -1);
00419 } else if (k2diff) {
00420 sbr->f_master[sbr->n_master]++;
00421 }
00422
00423 sbr->f_master[0] = sbr->k[0];
00424 for (k = 1; k <= sbr->n_master; k++)
00425 sbr->f_master[k] += sbr->f_master[k - 1];
00426
00427 } else {
00428 int half_bands = 7 - spectrum->bs_freq_scale;
00429 int two_regions, num_bands_0;
00430 int vdk0_max, vdk1_min;
00431 int16_t vk0[49];
00432
00433 if (49 * sbr->k[2] > 110 * sbr->k[0]) {
00434 two_regions = 1;
00435 sbr->k[1] = 2 * sbr->k[0];
00436 } else {
00437 two_regions = 0;
00438 sbr->k[1] = sbr->k[2];
00439 }
00440
00441 num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2;
00442
00443 if (num_bands_0 <= 0) {
00444 av_log(ac->avctx, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0);
00445 return -1;
00446 }
00447
00448 vk0[0] = 0;
00449
00450 make_bands(vk0+1, sbr->k[0], sbr->k[1], num_bands_0);
00451
00452 qsort(vk0 + 1, num_bands_0, sizeof(vk0[1]), qsort_comparison_function_int16);
00453 vdk0_max = vk0[num_bands_0];
00454
00455 vk0[0] = sbr->k[0];
00456 for (k = 1; k <= num_bands_0; k++) {
00457 if (vk0[k] <= 0) {
00458 av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]);
00459 return -1;
00460 }
00461 vk0[k] += vk0[k-1];
00462 }
00463
00464 if (two_regions) {
00465 int16_t vk1[49];
00466 float invwarp = spectrum->bs_alter_scale ? 0.76923076923076923077f
00467 : 1.0f;
00468 int num_bands_1 = lrintf(half_bands * invwarp *
00469 log2f(sbr->k[2] / (float)sbr->k[1])) * 2;
00470
00471 make_bands(vk1+1, sbr->k[1], sbr->k[2], num_bands_1);
00472
00473 vdk1_min = array_min_int16(vk1 + 1, num_bands_1);
00474
00475 if (vdk1_min < vdk0_max) {
00476 int change;
00477 qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16);
00478 change = FFMIN(vdk0_max - vk1[1], (vk1[num_bands_1] - vk1[1]) >> 1);
00479 vk1[1] += change;
00480 vk1[num_bands_1] -= change;
00481 }
00482
00483 qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16);
00484
00485 vk1[0] = sbr->k[1];
00486 for (k = 1; k <= num_bands_1; k++) {
00487 if (vk1[k] <= 0) {
00488 av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]);
00489 return -1;
00490 }
00491 vk1[k] += vk1[k-1];
00492 }
00493
00494 sbr->n_master = num_bands_0 + num_bands_1;
00495 if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
00496 return -1;
00497 memcpy(&sbr->f_master[0], vk0,
00498 (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
00499 memcpy(&sbr->f_master[num_bands_0 + 1], vk1 + 1,
00500 num_bands_1 * sizeof(sbr->f_master[0]));
00501
00502 } else {
00503 sbr->n_master = num_bands_0;
00504 if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
00505 return -1;
00506 memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
00507 }
00508 }
00509
00510 return 0;
00511 }
00512
00514 static int sbr_hf_calc_npatches(AACContext *ac, SpectralBandReplication *sbr)
00515 {
00516 int i, k, sb = 0;
00517 int msb = sbr->k[0];
00518 int usb = sbr->kx[1];
00519 int goal_sb = ((1000 << 11) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
00520
00521 sbr->num_patches = 0;
00522
00523 if (goal_sb < sbr->kx[1] + sbr->m[1]) {
00524 for (k = 0; sbr->f_master[k] < goal_sb; k++) ;
00525 } else
00526 k = sbr->n_master;
00527
00528 do {
00529 int odd = 0;
00530 for (i = k; i == k || sb > (sbr->k[0] - 1 + msb - odd); i--) {
00531 sb = sbr->f_master[i];
00532 odd = (sb + sbr->k[0]) & 1;
00533 }
00534
00535
00536
00537
00538
00539 if (sbr->num_patches > 5) {
00540 av_log(ac->avctx, AV_LOG_ERROR, "Too many patches: %d\n", sbr->num_patches);
00541 return -1;
00542 }
00543
00544 sbr->patch_num_subbands[sbr->num_patches] = FFMAX(sb - usb, 0);
00545 sbr->patch_start_subband[sbr->num_patches] = sbr->k[0] - odd - sbr->patch_num_subbands[sbr->num_patches];
00546
00547 if (sbr->patch_num_subbands[sbr->num_patches] > 0) {
00548 usb = sb;
00549 msb = sb;
00550 sbr->num_patches++;
00551 } else
00552 msb = sbr->kx[1];
00553
00554 if (sbr->f_master[k] - sb < 3)
00555 k = sbr->n_master;
00556 } while (sb != sbr->kx[1] + sbr->m[1]);
00557
00558 if (sbr->patch_num_subbands[sbr->num_patches-1] < 3 && sbr->num_patches > 1)
00559 sbr->num_patches--;
00560
00561 return 0;
00562 }
00563
00565 static int sbr_make_f_derived(AACContext *ac, SpectralBandReplication *sbr)
00566 {
00567 int k, temp;
00568
00569 sbr->n[1] = sbr->n_master - sbr->spectrum_params.bs_xover_band;
00570 sbr->n[0] = (sbr->n[1] + 1) >> 1;
00571
00572 memcpy(sbr->f_tablehigh, &sbr->f_master[sbr->spectrum_params.bs_xover_band],
00573 (sbr->n[1] + 1) * sizeof(sbr->f_master[0]));
00574 sbr->m[1] = sbr->f_tablehigh[sbr->n[1]] - sbr->f_tablehigh[0];
00575 sbr->kx[1] = sbr->f_tablehigh[0];
00576
00577
00578 if (sbr->kx[1] + sbr->m[1] > 64) {
00579 av_log(ac->avctx, AV_LOG_ERROR,
00580 "Stop frequency border too high: %d\n", sbr->kx[1] + sbr->m[1]);
00581 return -1;
00582 }
00583 if (sbr->kx[1] > 32) {
00584 av_log(ac->avctx, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->kx[1]);
00585 return -1;
00586 }
00587
00588 sbr->f_tablelow[0] = sbr->f_tablehigh[0];
00589 temp = sbr->n[1] & 1;
00590 for (k = 1; k <= sbr->n[0]; k++)
00591 sbr->f_tablelow[k] = sbr->f_tablehigh[2 * k - temp];
00592
00593 sbr->n_q = FFMAX(1, lrintf(sbr->spectrum_params.bs_noise_bands *
00594 log2f(sbr->k[2] / (float)sbr->kx[1])));
00595 if (sbr->n_q > 5) {
00596 av_log(ac->avctx, AV_LOG_ERROR, "Too many noise floor scale factors: %d\n", sbr->n_q);
00597 return -1;
00598 }
00599
00600 sbr->f_tablenoise[0] = sbr->f_tablelow[0];
00601 temp = 0;
00602 for (k = 1; k <= sbr->n_q; k++) {
00603 temp += (sbr->n[0] - temp) / (sbr->n_q + 1 - k);
00604 sbr->f_tablenoise[k] = sbr->f_tablelow[temp];
00605 }
00606
00607 if (sbr_hf_calc_npatches(ac, sbr) < 0)
00608 return -1;
00609
00610 sbr_make_f_tablelim(sbr);
00611
00612 sbr->data[0].f_indexnoise = 0;
00613 sbr->data[1].f_indexnoise = 0;
00614
00615 return 0;
00616 }
00617
00618 static av_always_inline void get_bits1_vector(GetBitContext *gb, uint8_t *vec,
00619 int elements)
00620 {
00621 int i;
00622 for (i = 0; i < elements; i++) {
00623 vec[i] = get_bits1(gb);
00624 }
00625 }
00626
00628 static const int8_t ceil_log2[] = {
00629 0, 1, 2, 2, 3, 3,
00630 };
00631
00632 static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr,
00633 GetBitContext *gb, SBRData *ch_data)
00634 {
00635 int i;
00636 unsigned bs_pointer = 0;
00637
00638 int abs_bord_trail = 16;
00639 int num_rel_lead, num_rel_trail;
00640 unsigned bs_num_env_old = ch_data->bs_num_env;
00641
00642 ch_data->bs_freq_res[0] = ch_data->bs_freq_res[ch_data->bs_num_env];
00643 ch_data->bs_amp_res = sbr->bs_amp_res_header;
00644 ch_data->t_env_num_env_old = ch_data->t_env[bs_num_env_old];
00645
00646 switch (ch_data->bs_frame_class = get_bits(gb, 2)) {
00647 case FIXFIX:
00648 ch_data->bs_num_env = 1 << get_bits(gb, 2);
00649 num_rel_lead = ch_data->bs_num_env - 1;
00650 if (ch_data->bs_num_env == 1)
00651 ch_data->bs_amp_res = 0;
00652
00653 if (ch_data->bs_num_env > 4) {
00654 av_log(ac->avctx, AV_LOG_ERROR,
00655 "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
00656 ch_data->bs_num_env);
00657 return -1;
00658 }
00659
00660 ch_data->t_env[0] = 0;
00661 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
00662
00663 abs_bord_trail = (abs_bord_trail + (ch_data->bs_num_env >> 1)) /
00664 ch_data->bs_num_env;
00665 for (i = 0; i < num_rel_lead; i++)
00666 ch_data->t_env[i + 1] = ch_data->t_env[i] + abs_bord_trail;
00667
00668 ch_data->bs_freq_res[1] = get_bits1(gb);
00669 for (i = 1; i < ch_data->bs_num_env; i++)
00670 ch_data->bs_freq_res[i + 1] = ch_data->bs_freq_res[1];
00671 break;
00672 case FIXVAR:
00673 abs_bord_trail += get_bits(gb, 2);
00674 num_rel_trail = get_bits(gb, 2);
00675 ch_data->bs_num_env = num_rel_trail + 1;
00676 ch_data->t_env[0] = 0;
00677 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
00678
00679 for (i = 0; i < num_rel_trail; i++)
00680 ch_data->t_env[ch_data->bs_num_env - 1 - i] =
00681 ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
00682
00683 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
00684
00685 for (i = 0; i < ch_data->bs_num_env; i++)
00686 ch_data->bs_freq_res[ch_data->bs_num_env - i] = get_bits1(gb);
00687 break;
00688 case VARFIX:
00689 ch_data->t_env[0] = get_bits(gb, 2);
00690 num_rel_lead = get_bits(gb, 2);
00691 ch_data->bs_num_env = num_rel_lead + 1;
00692 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
00693
00694 for (i = 0; i < num_rel_lead; i++)
00695 ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
00696
00697 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
00698
00699 get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
00700 break;
00701 case VARVAR:
00702 ch_data->t_env[0] = get_bits(gb, 2);
00703 abs_bord_trail += get_bits(gb, 2);
00704 num_rel_lead = get_bits(gb, 2);
00705 num_rel_trail = get_bits(gb, 2);
00706 ch_data->bs_num_env = num_rel_lead + num_rel_trail + 1;
00707
00708 if (ch_data->bs_num_env > 5) {
00709 av_log(ac->avctx, AV_LOG_ERROR,
00710 "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n",
00711 ch_data->bs_num_env);
00712 return -1;
00713 }
00714
00715 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
00716
00717 for (i = 0; i < num_rel_lead; i++)
00718 ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
00719 for (i = 0; i < num_rel_trail; i++)
00720 ch_data->t_env[ch_data->bs_num_env - 1 - i] =
00721 ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
00722
00723 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
00724
00725 get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
00726 break;
00727 }
00728
00729 if (bs_pointer > ch_data->bs_num_env + 1) {
00730 av_log(ac->avctx, AV_LOG_ERROR,
00731 "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n",
00732 bs_pointer);
00733 return -1;
00734 }
00735
00736 for (i = 1; i <= ch_data->bs_num_env; i++) {
00737 if (ch_data->t_env[i-1] > ch_data->t_env[i]) {
00738 av_log(ac->avctx, AV_LOG_ERROR, "Non monotone time borders\n");
00739 return -1;
00740 }
00741 }
00742
00743 ch_data->bs_num_noise = (ch_data->bs_num_env > 1) + 1;
00744
00745 ch_data->t_q[0] = ch_data->t_env[0];
00746 ch_data->t_q[ch_data->bs_num_noise] = ch_data->t_env[ch_data->bs_num_env];
00747 if (ch_data->bs_num_noise > 1) {
00748 unsigned int idx;
00749 if (ch_data->bs_frame_class == FIXFIX) {
00750 idx = ch_data->bs_num_env >> 1;
00751 } else if (ch_data->bs_frame_class & 1) {
00752 idx = ch_data->bs_num_env - FFMAX(bs_pointer - 1, 1);
00753 } else {
00754 if (!bs_pointer)
00755 idx = 1;
00756 else if (bs_pointer == 1)
00757 idx = ch_data->bs_num_env - 1;
00758 else
00759 idx = bs_pointer - 1;
00760 }
00761 ch_data->t_q[1] = ch_data->t_env[idx];
00762 }
00763
00764 ch_data->e_a[0] = -(ch_data->e_a[1] != bs_num_env_old);
00765 ch_data->e_a[1] = -1;
00766 if ((ch_data->bs_frame_class & 1) && bs_pointer) {
00767 ch_data->e_a[1] = ch_data->bs_num_env + 1 - bs_pointer;
00768 } else if ((ch_data->bs_frame_class == 2) && (bs_pointer > 1))
00769 ch_data->e_a[1] = bs_pointer - 1;
00770
00771 return 0;
00772 }
00773
00774 static void copy_sbr_grid(SBRData *dst, const SBRData *src) {
00775
00776 dst->bs_freq_res[0] = dst->bs_freq_res[dst->bs_num_env];
00777 dst->t_env_num_env_old = dst->t_env[dst->bs_num_env];
00778 dst->e_a[0] = -(dst->e_a[1] != dst->bs_num_env);
00779
00780
00781 memcpy(dst->bs_freq_res+1, src->bs_freq_res+1, sizeof(dst->bs_freq_res)-sizeof(*dst->bs_freq_res));
00782 memcpy(dst->t_env, src->t_env, sizeof(dst->t_env));
00783 memcpy(dst->t_q, src->t_q, sizeof(dst->t_q));
00784 dst->bs_num_env = src->bs_num_env;
00785 dst->bs_amp_res = src->bs_amp_res;
00786 dst->bs_num_noise = src->bs_num_noise;
00787 dst->bs_frame_class = src->bs_frame_class;
00788 dst->e_a[1] = src->e_a[1];
00789 }
00790
00792 static void read_sbr_dtdf(SpectralBandReplication *sbr, GetBitContext *gb,
00793 SBRData *ch_data)
00794 {
00795 get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
00796 get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
00797 }
00798
00800 static void read_sbr_invf(SpectralBandReplication *sbr, GetBitContext *gb,
00801 SBRData *ch_data)
00802 {
00803 int i;
00804
00805 memcpy(ch_data->bs_invf_mode[1], ch_data->bs_invf_mode[0], 5 * sizeof(uint8_t));
00806 for (i = 0; i < sbr->n_q; i++)
00807 ch_data->bs_invf_mode[0][i] = get_bits(gb, 2);
00808 }
00809
00810 static void read_sbr_envelope(SpectralBandReplication *sbr, GetBitContext *gb,
00811 SBRData *ch_data, int ch)
00812 {
00813 int bits;
00814 int i, j, k;
00815 VLC_TYPE (*t_huff)[2], (*f_huff)[2];
00816 int t_lav, f_lav;
00817 const int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
00818 const int odd = sbr->n[1] & 1;
00819
00820 if (sbr->bs_coupling && ch) {
00821 if (ch_data->bs_amp_res) {
00822 bits = 5;
00823 t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_3_0DB].table;
00824 t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_3_0DB];
00825 f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
00826 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB];
00827 } else {
00828 bits = 6;
00829 t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_1_5DB].table;
00830 t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_1_5DB];
00831 f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_1_5DB].table;
00832 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_1_5DB];
00833 }
00834 } else {
00835 if (ch_data->bs_amp_res) {
00836 bits = 6;
00837 t_huff = vlc_sbr[T_HUFFMAN_ENV_3_0DB].table;
00838 t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_3_0DB];
00839 f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
00840 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB];
00841 } else {
00842 bits = 7;
00843 t_huff = vlc_sbr[T_HUFFMAN_ENV_1_5DB].table;
00844 t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_1_5DB];
00845 f_huff = vlc_sbr[F_HUFFMAN_ENV_1_5DB].table;
00846 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_1_5DB];
00847 }
00848 }
00849
00850 for (i = 0; i < ch_data->bs_num_env; i++) {
00851 if (ch_data->bs_df_env[i]) {
00852
00853 if (ch_data->bs_freq_res[i + 1] == ch_data->bs_freq_res[i]) {
00854 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++)
00855 ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][j] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
00856 } else if (ch_data->bs_freq_res[i + 1]) {
00857 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
00858 k = (j + odd) >> 1;
00859 ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
00860 }
00861 } else {
00862 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
00863 k = j ? 2*j - odd : 0;
00864 ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
00865 }
00866 }
00867 } else {
00868 ch_data->env_facs[i + 1][0] = delta * get_bits(gb, bits);
00869 for (j = 1; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++)
00870 ch_data->env_facs[i + 1][j] = ch_data->env_facs[i + 1][j - 1] + delta * (get_vlc2(gb, f_huff, 9, 3) - f_lav);
00871 }
00872 }
00873
00874
00875 memcpy(ch_data->env_facs[0], ch_data->env_facs[ch_data->bs_num_env],
00876 sizeof(ch_data->env_facs[0]));
00877 }
00878
00879 static void read_sbr_noise(SpectralBandReplication *sbr, GetBitContext *gb,
00880 SBRData *ch_data, int ch)
00881 {
00882 int i, j;
00883 VLC_TYPE (*t_huff)[2], (*f_huff)[2];
00884 int t_lav, f_lav;
00885 int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
00886
00887 if (sbr->bs_coupling && ch) {
00888 t_huff = vlc_sbr[T_HUFFMAN_NOISE_BAL_3_0DB].table;
00889 t_lav = vlc_sbr_lav[T_HUFFMAN_NOISE_BAL_3_0DB];
00890 f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
00891 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB];
00892 } else {
00893 t_huff = vlc_sbr[T_HUFFMAN_NOISE_3_0DB].table;
00894 t_lav = vlc_sbr_lav[T_HUFFMAN_NOISE_3_0DB];
00895 f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
00896 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB];
00897 }
00898
00899 for (i = 0; i < ch_data->bs_num_noise; i++) {
00900 if (ch_data->bs_df_noise[i]) {
00901 for (j = 0; j < sbr->n_q; j++)
00902 ch_data->noise_facs[i + 1][j] = ch_data->noise_facs[i][j] + delta * (get_vlc2(gb, t_huff, 9, 2) - t_lav);
00903 } else {
00904 ch_data->noise_facs[i + 1][0] = delta * get_bits(gb, 5);
00905 for (j = 1; j < sbr->n_q; j++)
00906 ch_data->noise_facs[i + 1][j] = ch_data->noise_facs[i + 1][j - 1] + delta * (get_vlc2(gb, f_huff, 9, 3) - f_lav);
00907 }
00908 }
00909
00910
00911 memcpy(ch_data->noise_facs[0], ch_data->noise_facs[ch_data->bs_num_noise],
00912 sizeof(ch_data->noise_facs[0]));
00913 }
00914
00915 static void read_sbr_extension(AACContext *ac, SpectralBandReplication *sbr,
00916 GetBitContext *gb,
00917 int bs_extension_id, int *num_bits_left)
00918 {
00919 switch (bs_extension_id) {
00920 case EXTENSION_ID_PS:
00921 if (!ac->oc[1].m4ac.ps) {
00922 av_log(ac->avctx, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
00923 skip_bits_long(gb, *num_bits_left);
00924 *num_bits_left = 0;
00925 } else {
00926 #if 1
00927 *num_bits_left -= ff_ps_read_data(ac->avctx, gb, &sbr->ps, *num_bits_left);
00928 #else
00929 av_log_missing_feature(ac->avctx, "Parametric Stereo is", 0);
00930 skip_bits_long(gb, *num_bits_left);
00931 *num_bits_left = 0;
00932 #endif
00933 }
00934 break;
00935 default:
00936
00937 if (bs_extension_id || *num_bits_left > 16 || show_bits(gb, *num_bits_left))
00938 av_log_missing_feature(ac->avctx, "Reserved SBR extensions are", 1);
00939 skip_bits_long(gb, *num_bits_left);
00940 *num_bits_left = 0;
00941 break;
00942 }
00943 }
00944
00945 static int read_sbr_single_channel_element(AACContext *ac,
00946 SpectralBandReplication *sbr,
00947 GetBitContext *gb)
00948 {
00949 if (get_bits1(gb))
00950 skip_bits(gb, 4);
00951
00952 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
00953 return -1;
00954 read_sbr_dtdf(sbr, gb, &sbr->data[0]);
00955 read_sbr_invf(sbr, gb, &sbr->data[0]);
00956 read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
00957 read_sbr_noise(sbr, gb, &sbr->data[0], 0);
00958
00959 if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
00960 get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
00961
00962 return 0;
00963 }
00964
00965 static int read_sbr_channel_pair_element(AACContext *ac,
00966 SpectralBandReplication *sbr,
00967 GetBitContext *gb)
00968 {
00969 if (get_bits1(gb))
00970 skip_bits(gb, 8);
00971
00972 if ((sbr->bs_coupling = get_bits1(gb))) {
00973 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
00974 return -1;
00975 copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
00976 read_sbr_dtdf(sbr, gb, &sbr->data[0]);
00977 read_sbr_dtdf(sbr, gb, &sbr->data[1]);
00978 read_sbr_invf(sbr, gb, &sbr->data[0]);
00979 memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
00980 memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
00981 read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
00982 read_sbr_noise(sbr, gb, &sbr->data[0], 0);
00983 read_sbr_envelope(sbr, gb, &sbr->data[1], 1);
00984 read_sbr_noise(sbr, gb, &sbr->data[1], 1);
00985 } else {
00986 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]) ||
00987 read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
00988 return -1;
00989 read_sbr_dtdf(sbr, gb, &sbr->data[0]);
00990 read_sbr_dtdf(sbr, gb, &sbr->data[1]);
00991 read_sbr_invf(sbr, gb, &sbr->data[0]);
00992 read_sbr_invf(sbr, gb, &sbr->data[1]);
00993 read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
00994 read_sbr_envelope(sbr, gb, &sbr->data[1], 1);
00995 read_sbr_noise(sbr, gb, &sbr->data[0], 0);
00996 read_sbr_noise(sbr, gb, &sbr->data[1], 1);
00997 }
00998
00999 if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
01000 get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
01001 if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
01002 get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
01003
01004 return 0;
01005 }
01006
01007 static unsigned int read_sbr_data(AACContext *ac, SpectralBandReplication *sbr,
01008 GetBitContext *gb, int id_aac)
01009 {
01010 unsigned int cnt = get_bits_count(gb);
01011
01012 if (id_aac == TYPE_SCE || id_aac == TYPE_CCE) {
01013 if (read_sbr_single_channel_element(ac, sbr, gb)) {
01014 sbr_turnoff(sbr);
01015 return get_bits_count(gb) - cnt;
01016 }
01017 } else if (id_aac == TYPE_CPE) {
01018 if (read_sbr_channel_pair_element(ac, sbr, gb)) {
01019 sbr_turnoff(sbr);
01020 return get_bits_count(gb) - cnt;
01021 }
01022 } else {
01023 av_log(ac->avctx, AV_LOG_ERROR,
01024 "Invalid bitstream - cannot apply SBR to element type %d\n", id_aac);
01025 sbr_turnoff(sbr);
01026 return get_bits_count(gb) - cnt;
01027 }
01028 if (get_bits1(gb)) {
01029 int num_bits_left = get_bits(gb, 4);
01030 if (num_bits_left == 15)
01031 num_bits_left += get_bits(gb, 8);
01032
01033 num_bits_left <<= 3;
01034 while (num_bits_left > 7) {
01035 num_bits_left -= 2;
01036 read_sbr_extension(ac, sbr, gb, get_bits(gb, 2), &num_bits_left);
01037 }
01038 if (num_bits_left < 0) {
01039 av_log(ac->avctx, AV_LOG_ERROR, "SBR Extension over read.\n");
01040 }
01041 if (num_bits_left > 0)
01042 skip_bits(gb, num_bits_left);
01043 }
01044
01045 return get_bits_count(gb) - cnt;
01046 }
01047
01048 static void sbr_reset(AACContext *ac, SpectralBandReplication *sbr)
01049 {
01050 int err;
01051 err = sbr_make_f_master(ac, sbr, &sbr->spectrum_params);
01052 if (err >= 0)
01053 err = sbr_make_f_derived(ac, sbr);
01054 if (err < 0) {
01055 av_log(ac->avctx, AV_LOG_ERROR,
01056 "SBR reset failed. Switching SBR to pure upsampling mode.\n");
01057 sbr_turnoff(sbr);
01058 }
01059 }
01060
01069 int ff_decode_sbr_extension(AACContext *ac, SpectralBandReplication *sbr,
01070 GetBitContext *gb_host, int crc, int cnt, int id_aac)
01071 {
01072 unsigned int num_sbr_bits = 0, num_align_bits;
01073 unsigned bytes_read;
01074 GetBitContext gbc = *gb_host, *gb = &gbc;
01075 skip_bits_long(gb_host, cnt*8 - 4);
01076
01077 sbr->reset = 0;
01078
01079 if (!sbr->sample_rate)
01080 sbr->sample_rate = 2 * ac->oc[1].m4ac.sample_rate;
01081 if (!ac->oc[1].m4ac.ext_sample_rate)
01082 ac->oc[1].m4ac.ext_sample_rate = 2 * ac->oc[1].m4ac.sample_rate;
01083
01084 if (crc) {
01085 skip_bits(gb, 10);
01086 num_sbr_bits += 10;
01087 }
01088
01089
01090 sbr->kx[0] = sbr->kx[1];
01091 sbr->m[0] = sbr->m[1];
01092 sbr->kx_and_m_pushed = 1;
01093
01094 num_sbr_bits++;
01095 if (get_bits1(gb))
01096 num_sbr_bits += read_sbr_header(sbr, gb);
01097
01098 if (sbr->reset)
01099 sbr_reset(ac, sbr);
01100
01101 if (sbr->start)
01102 num_sbr_bits += read_sbr_data(ac, sbr, gb, id_aac);
01103
01104 num_align_bits = ((cnt << 3) - 4 - num_sbr_bits) & 7;
01105 bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3);
01106
01107 if (bytes_read > cnt) {
01108 av_log(ac->avctx, AV_LOG_ERROR,
01109 "Expected to read %d SBR bytes actually read %d.\n", cnt, bytes_read);
01110 }
01111 return cnt;
01112 }
01113
01115 static void sbr_dequant(SpectralBandReplication *sbr, int id_aac)
01116 {
01117 int k, e;
01118 int ch;
01119
01120 if (id_aac == TYPE_CPE && sbr->bs_coupling) {
01121 float alpha = sbr->data[0].bs_amp_res ? 1.0f : 0.5f;
01122 float pan_offset = sbr->data[0].bs_amp_res ? 12.0f : 24.0f;
01123 for (e = 1; e <= sbr->data[0].bs_num_env; e++) {
01124 for (k = 0; k < sbr->n[sbr->data[0].bs_freq_res[e]]; k++) {
01125 float temp1 = exp2f(sbr->data[0].env_facs[e][k] * alpha + 7.0f);
01126 float temp2 = exp2f((pan_offset - sbr->data[1].env_facs[e][k]) * alpha);
01127 float fac = temp1 / (1.0f + temp2);
01128 sbr->data[0].env_facs[e][k] = fac;
01129 sbr->data[1].env_facs[e][k] = fac * temp2;
01130 }
01131 }
01132 for (e = 1; e <= sbr->data[0].bs_num_noise; e++) {
01133 for (k = 0; k < sbr->n_q; k++) {
01134 float temp1 = exp2f(NOISE_FLOOR_OFFSET - sbr->data[0].noise_facs[e][k] + 1);
01135 float temp2 = exp2f(12 - sbr->data[1].noise_facs[e][k]);
01136 float fac = temp1 / (1.0f + temp2);
01137 sbr->data[0].noise_facs[e][k] = fac;
01138 sbr->data[1].noise_facs[e][k] = fac * temp2;
01139 }
01140 }
01141 } else {
01142 for (ch = 0; ch < (id_aac == TYPE_CPE) + 1; ch++) {
01143 float alpha = sbr->data[ch].bs_amp_res ? 1.0f : 0.5f;
01144 for (e = 1; e <= sbr->data[ch].bs_num_env; e++)
01145 for (k = 0; k < sbr->n[sbr->data[ch].bs_freq_res[e]]; k++)
01146 sbr->data[ch].env_facs[e][k] =
01147 exp2f(alpha * sbr->data[ch].env_facs[e][k] + 6.0f);
01148 for (e = 1; e <= sbr->data[ch].bs_num_noise; e++)
01149 for (k = 0; k < sbr->n_q; k++)
01150 sbr->data[ch].noise_facs[e][k] =
01151 exp2f(NOISE_FLOOR_OFFSET - sbr->data[ch].noise_facs[e][k]);
01152 }
01153 }
01154 }
01155
01162 static void sbr_qmf_analysis(DSPContext *dsp, FFTContext *mdct,
01163 SBRDSPContext *sbrdsp, const float *in, float *x,
01164 float z[320], float W[2][32][32][2])
01165 {
01166 int i;
01167 memcpy(W[0], W[1], sizeof(W[0]));
01168 memcpy(x , x+1024, (320-32)*sizeof(x[0]));
01169 memcpy(x+288, in, 1024*sizeof(x[0]));
01170 for (i = 0; i < 32; i++) {
01171
01172 dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320);
01173 sbrdsp->sum64x5(z);
01174 sbrdsp->qmf_pre_shuffle(z);
01175 mdct->imdct_half(mdct, z, z+64);
01176 sbrdsp->qmf_post_shuffle(W[1][i], z);
01177 x += 32;
01178 }
01179 }
01180
01185 static void sbr_qmf_synthesis(DSPContext *dsp, FFTContext *mdct,
01186 SBRDSPContext *sbrdsp,
01187 float *out, float X[2][38][64],
01188 float mdct_buf[2][64],
01189 float *v0, int *v_off, const unsigned int div)
01190 {
01191 int i, n;
01192 const float *sbr_qmf_window = div ? sbr_qmf_window_ds : sbr_qmf_window_us;
01193 const int step = 128 >> div;
01194 float *v;
01195 for (i = 0; i < 32; i++) {
01196 if (*v_off < step) {
01197 int saved_samples = (1280 - 128) >> div;
01198 memcpy(&v0[SBR_SYNTHESIS_BUF_SIZE - saved_samples], v0, saved_samples * sizeof(float));
01199 *v_off = SBR_SYNTHESIS_BUF_SIZE - saved_samples - step;
01200 } else {
01201 *v_off -= step;
01202 }
01203 v = v0 + *v_off;
01204 if (div) {
01205 for (n = 0; n < 32; n++) {
01206 X[0][i][ n] = -X[0][i][n];
01207 X[0][i][32+n] = X[1][i][31-n];
01208 }
01209 mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
01210 sbrdsp->qmf_deint_neg(v, mdct_buf[0]);
01211 } else {
01212 sbrdsp->neg_odd_64(X[1][i]);
01213 mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
01214 mdct->imdct_half(mdct, mdct_buf[1], X[1][i]);
01215 sbrdsp->qmf_deint_bfly(v, mdct_buf[1], mdct_buf[0]);
01216 }
01217 dsp->vector_fmul_add(out, v , sbr_qmf_window , zero64, 64 >> div);
01218 dsp->vector_fmul_add(out, v + ( 192 >> div), sbr_qmf_window + ( 64 >> div), out , 64 >> div);
01219 dsp->vector_fmul_add(out, v + ( 256 >> div), sbr_qmf_window + (128 >> div), out , 64 >> div);
01220 dsp->vector_fmul_add(out, v + ( 448 >> div), sbr_qmf_window + (192 >> div), out , 64 >> div);
01221 dsp->vector_fmul_add(out, v + ( 512 >> div), sbr_qmf_window + (256 >> div), out , 64 >> div);
01222 dsp->vector_fmul_add(out, v + ( 704 >> div), sbr_qmf_window + (320 >> div), out , 64 >> div);
01223 dsp->vector_fmul_add(out, v + ( 768 >> div), sbr_qmf_window + (384 >> div), out , 64 >> div);
01224 dsp->vector_fmul_add(out, v + ( 960 >> div), sbr_qmf_window + (448 >> div), out , 64 >> div);
01225 dsp->vector_fmul_add(out, v + (1024 >> div), sbr_qmf_window + (512 >> div), out , 64 >> div);
01226 dsp->vector_fmul_add(out, v + (1216 >> div), sbr_qmf_window + (576 >> div), out , 64 >> div);
01227 out += 64 >> div;
01228 }
01229 }
01230
01235 static void sbr_hf_inverse_filter(SBRDSPContext *dsp,
01236 float (*alpha0)[2], float (*alpha1)[2],
01237 const float X_low[32][40][2], int k0)
01238 {
01239 int k;
01240 for (k = 0; k < k0; k++) {
01241 LOCAL_ALIGNED_16(float, phi, [3], [2][2]);
01242 float dk;
01243
01244 dsp->autocorrelate(X_low[k], phi);
01245
01246 dk = phi[2][1][0] * phi[1][0][0] -
01247 (phi[1][1][0] * phi[1][1][0] + phi[1][1][1] * phi[1][1][1]) / 1.000001f;
01248
01249 if (!dk) {
01250 alpha1[k][0] = 0;
01251 alpha1[k][1] = 0;
01252 } else {
01253 float temp_real, temp_im;
01254 temp_real = phi[0][0][0] * phi[1][1][0] -
01255 phi[0][0][1] * phi[1][1][1] -
01256 phi[0][1][0] * phi[1][0][0];
01257 temp_im = phi[0][0][0] * phi[1][1][1] +
01258 phi[0][0][1] * phi[1][1][0] -
01259 phi[0][1][1] * phi[1][0][0];
01260
01261 alpha1[k][0] = temp_real / dk;
01262 alpha1[k][1] = temp_im / dk;
01263 }
01264
01265 if (!phi[1][0][0]) {
01266 alpha0[k][0] = 0;
01267 alpha0[k][1] = 0;
01268 } else {
01269 float temp_real, temp_im;
01270 temp_real = phi[0][0][0] + alpha1[k][0] * phi[1][1][0] +
01271 alpha1[k][1] * phi[1][1][1];
01272 temp_im = phi[0][0][1] + alpha1[k][1] * phi[1][1][0] -
01273 alpha1[k][0] * phi[1][1][1];
01274
01275 alpha0[k][0] = -temp_real / phi[1][0][0];
01276 alpha0[k][1] = -temp_im / phi[1][0][0];
01277 }
01278
01279 if (alpha1[k][0] * alpha1[k][0] + alpha1[k][1] * alpha1[k][1] >= 16.0f ||
01280 alpha0[k][0] * alpha0[k][0] + alpha0[k][1] * alpha0[k][1] >= 16.0f) {
01281 alpha1[k][0] = 0;
01282 alpha1[k][1] = 0;
01283 alpha0[k][0] = 0;
01284 alpha0[k][1] = 0;
01285 }
01286 }
01287 }
01288
01290 static void sbr_chirp(SpectralBandReplication *sbr, SBRData *ch_data)
01291 {
01292 int i;
01293 float new_bw;
01294 static const float bw_tab[] = { 0.0f, 0.75f, 0.9f, 0.98f };
01295
01296 for (i = 0; i < sbr->n_q; i++) {
01297 if (ch_data->bs_invf_mode[0][i] + ch_data->bs_invf_mode[1][i] == 1) {
01298 new_bw = 0.6f;
01299 } else
01300 new_bw = bw_tab[ch_data->bs_invf_mode[0][i]];
01301
01302 if (new_bw < ch_data->bw_array[i]) {
01303 new_bw = 0.75f * new_bw + 0.25f * ch_data->bw_array[i];
01304 } else
01305 new_bw = 0.90625f * new_bw + 0.09375f * ch_data->bw_array[i];
01306 ch_data->bw_array[i] = new_bw < 0.015625f ? 0.0f : new_bw;
01307 }
01308 }
01309
01311 static int sbr_lf_gen(AACContext *ac, SpectralBandReplication *sbr,
01312 float X_low[32][40][2], const float W[2][32][32][2])
01313 {
01314 int i, k;
01315 const int t_HFGen = 8;
01316 const int i_f = 32;
01317 memset(X_low, 0, 32*sizeof(*X_low));
01318 for (k = 0; k < sbr->kx[1]; k++) {
01319 for (i = t_HFGen; i < i_f + t_HFGen; i++) {
01320 X_low[k][i][0] = W[1][i - t_HFGen][k][0];
01321 X_low[k][i][1] = W[1][i - t_HFGen][k][1];
01322 }
01323 }
01324 for (k = 0; k < sbr->kx[0]; k++) {
01325 for (i = 0; i < t_HFGen; i++) {
01326 X_low[k][i][0] = W[0][i + i_f - t_HFGen][k][0];
01327 X_low[k][i][1] = W[0][i + i_f - t_HFGen][k][1];
01328 }
01329 }
01330 return 0;
01331 }
01332
01334 static int sbr_hf_gen(AACContext *ac, SpectralBandReplication *sbr,
01335 float X_high[64][40][2], const float X_low[32][40][2],
01336 const float (*alpha0)[2], const float (*alpha1)[2],
01337 const float bw_array[5], const uint8_t *t_env,
01338 int bs_num_env)
01339 {
01340 int j, x;
01341 int g = 0;
01342 int k = sbr->kx[1];
01343 for (j = 0; j < sbr->num_patches; j++) {
01344 for (x = 0; x < sbr->patch_num_subbands[j]; x++, k++) {
01345 const int p = sbr->patch_start_subband[j] + x;
01346 while (g <= sbr->n_q && k >= sbr->f_tablenoise[g])
01347 g++;
01348 g--;
01349
01350 if (g < 0) {
01351 av_log(ac->avctx, AV_LOG_ERROR,
01352 "ERROR : no subband found for frequency %d\n", k);
01353 return -1;
01354 }
01355
01356 sbr->dsp.hf_gen(X_high[k] + ENVELOPE_ADJUSTMENT_OFFSET,
01357 X_low[p] + ENVELOPE_ADJUSTMENT_OFFSET,
01358 alpha0[p], alpha1[p], bw_array[g],
01359 2 * t_env[0], 2 * t_env[bs_num_env]);
01360 }
01361 }
01362 if (k < sbr->m[1] + sbr->kx[1])
01363 memset(X_high + k, 0, (sbr->m[1] + sbr->kx[1] - k) * sizeof(*X_high));
01364
01365 return 0;
01366 }
01367
01369 static int sbr_x_gen(SpectralBandReplication *sbr, float X[2][38][64],
01370 const float Y0[38][64][2], const float Y1[38][64][2],
01371 const float X_low[32][40][2], int ch)
01372 {
01373 int k, i;
01374 const int i_f = 32;
01375 const int i_Temp = FFMAX(2*sbr->data[ch].t_env_num_env_old - i_f, 0);
01376 memset(X, 0, 2*sizeof(*X));
01377 for (k = 0; k < sbr->kx[0]; k++) {
01378 for (i = 0; i < i_Temp; i++) {
01379 X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
01380 X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
01381 }
01382 }
01383 for (; k < sbr->kx[0] + sbr->m[0]; k++) {
01384 for (i = 0; i < i_Temp; i++) {
01385 X[0][i][k] = Y0[i + i_f][k][0];
01386 X[1][i][k] = Y0[i + i_f][k][1];
01387 }
01388 }
01389
01390 for (k = 0; k < sbr->kx[1]; k++) {
01391 for (i = i_Temp; i < 38; i++) {
01392 X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
01393 X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
01394 }
01395 }
01396 for (; k < sbr->kx[1] + sbr->m[1]; k++) {
01397 for (i = i_Temp; i < i_f; i++) {
01398 X[0][i][k] = Y1[i][k][0];
01399 X[1][i][k] = Y1[i][k][1];
01400 }
01401 }
01402 return 0;
01403 }
01404
01408 static int sbr_mapping(AACContext *ac, SpectralBandReplication *sbr,
01409 SBRData *ch_data, int e_a[2])
01410 {
01411 int e, i, m;
01412
01413 memset(ch_data->s_indexmapped[1], 0, 7*sizeof(ch_data->s_indexmapped[1]));
01414 for (e = 0; e < ch_data->bs_num_env; e++) {
01415 const unsigned int ilim = sbr->n[ch_data->bs_freq_res[e + 1]];
01416 uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
01417 int k;
01418
01419 if (sbr->kx[1] != table[0]) {
01420 av_log(ac->avctx, AV_LOG_ERROR, "kx != f_table{high,low}[0]. "
01421 "Derived frequency tables were not regenerated.\n");
01422 sbr_turnoff(sbr);
01423 return AVERROR_BUG;
01424 }
01425 for (i = 0; i < ilim; i++)
01426 for (m = table[i]; m < table[i + 1]; m++)
01427 sbr->e_origmapped[e][m - sbr->kx[1]] = ch_data->env_facs[e+1][i];
01428
01429
01430 k = (ch_data->bs_num_noise > 1) && (ch_data->t_env[e] >= ch_data->t_q[1]);
01431 for (i = 0; i < sbr->n_q; i++)
01432 for (m = sbr->f_tablenoise[i]; m < sbr->f_tablenoise[i + 1]; m++)
01433 sbr->q_mapped[e][m - sbr->kx[1]] = ch_data->noise_facs[k+1][i];
01434
01435 for (i = 0; i < sbr->n[1]; i++) {
01436 if (ch_data->bs_add_harmonic_flag) {
01437 const unsigned int m_midpoint =
01438 (sbr->f_tablehigh[i] + sbr->f_tablehigh[i + 1]) >> 1;
01439
01440 ch_data->s_indexmapped[e + 1][m_midpoint - sbr->kx[1]] = ch_data->bs_add_harmonic[i] *
01441 (e >= e_a[1] || (ch_data->s_indexmapped[0][m_midpoint - sbr->kx[1]] == 1));
01442 }
01443 }
01444
01445 for (i = 0; i < ilim; i++) {
01446 int additional_sinusoid_present = 0;
01447 for (m = table[i]; m < table[i + 1]; m++) {
01448 if (ch_data->s_indexmapped[e + 1][m - sbr->kx[1]]) {
01449 additional_sinusoid_present = 1;
01450 break;
01451 }
01452 }
01453 memset(&sbr->s_mapped[e][table[i] - sbr->kx[1]], additional_sinusoid_present,
01454 (table[i + 1] - table[i]) * sizeof(sbr->s_mapped[e][0]));
01455 }
01456 }
01457
01458 memcpy(ch_data->s_indexmapped[0], ch_data->s_indexmapped[ch_data->bs_num_env], sizeof(ch_data->s_indexmapped[0]));
01459 return 0;
01460 }
01461
01463 static void sbr_env_estimate(float (*e_curr)[48], float X_high[64][40][2],
01464 SpectralBandReplication *sbr, SBRData *ch_data)
01465 {
01466 int e, m;
01467 int kx1 = sbr->kx[1];
01468
01469 if (sbr->bs_interpol_freq) {
01470 for (e = 0; e < ch_data->bs_num_env; e++) {
01471 const float recip_env_size = 0.5f / (ch_data->t_env[e + 1] - ch_data->t_env[e]);
01472 int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
01473 int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
01474
01475 for (m = 0; m < sbr->m[1]; m++) {
01476 float sum = sbr->dsp.sum_square(X_high[m+kx1] + ilb, iub - ilb);
01477 e_curr[e][m] = sum * recip_env_size;
01478 }
01479 }
01480 } else {
01481 int k, p;
01482
01483 for (e = 0; e < ch_data->bs_num_env; e++) {
01484 const int env_size = 2 * (ch_data->t_env[e + 1] - ch_data->t_env[e]);
01485 int ilb = ch_data->t_env[e] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
01486 int iub = ch_data->t_env[e + 1] * 2 + ENVELOPE_ADJUSTMENT_OFFSET;
01487 const uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
01488
01489 for (p = 0; p < sbr->n[ch_data->bs_freq_res[e + 1]]; p++) {
01490 float sum = 0.0f;
01491 const int den = env_size * (table[p + 1] - table[p]);
01492
01493 for (k = table[p]; k < table[p + 1]; k++) {
01494 sum += sbr->dsp.sum_square(X_high[k] + ilb, iub - ilb);
01495 }
01496 sum /= den;
01497 for (k = table[p]; k < table[p + 1]; k++) {
01498 e_curr[e][k - kx1] = sum;
01499 }
01500 }
01501 }
01502 }
01503 }
01504
01509 static void sbr_gain_calc(AACContext *ac, SpectralBandReplication *sbr,
01510 SBRData *ch_data, const int e_a[2])
01511 {
01512 int e, k, m;
01513
01514 static const float limgain[4] = { 0.70795, 1.0, 1.41254, 10000000000 };
01515
01516 for (e = 0; e < ch_data->bs_num_env; e++) {
01517 int delta = !((e == e_a[1]) || (e == e_a[0]));
01518 for (k = 0; k < sbr->n_lim; k++) {
01519 float gain_boost, gain_max;
01520 float sum[2] = { 0.0f, 0.0f };
01521 for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
01522 const float temp = sbr->e_origmapped[e][m] / (1.0f + sbr->q_mapped[e][m]);
01523 sbr->q_m[e][m] = sqrtf(temp * sbr->q_mapped[e][m]);
01524 sbr->s_m[e][m] = sqrtf(temp * ch_data->s_indexmapped[e + 1][m]);
01525 if (!sbr->s_mapped[e][m]) {
01526 sbr->gain[e][m] = sqrtf(sbr->e_origmapped[e][m] /
01527 ((1.0f + sbr->e_curr[e][m]) *
01528 (1.0f + sbr->q_mapped[e][m] * delta)));
01529 } else {
01530 sbr->gain[e][m] = sqrtf(sbr->e_origmapped[e][m] * sbr->q_mapped[e][m] /
01531 ((1.0f + sbr->e_curr[e][m]) *
01532 (1.0f + sbr->q_mapped[e][m])));
01533 }
01534 }
01535 for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
01536 sum[0] += sbr->e_origmapped[e][m];
01537 sum[1] += sbr->e_curr[e][m];
01538 }
01539 gain_max = limgain[sbr->bs_limiter_gains] * sqrtf((FLT_EPSILON + sum[0]) / (FLT_EPSILON + sum[1]));
01540 gain_max = FFMIN(100000.f, gain_max);
01541 for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
01542 float q_m_max = sbr->q_m[e][m] * gain_max / sbr->gain[e][m];
01543 sbr->q_m[e][m] = FFMIN(sbr->q_m[e][m], q_m_max);
01544 sbr->gain[e][m] = FFMIN(sbr->gain[e][m], gain_max);
01545 }
01546 sum[0] = sum[1] = 0.0f;
01547 for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
01548 sum[0] += sbr->e_origmapped[e][m];
01549 sum[1] += sbr->e_curr[e][m] * sbr->gain[e][m] * sbr->gain[e][m]
01550 + sbr->s_m[e][m] * sbr->s_m[e][m]
01551 + (delta && !sbr->s_m[e][m]) * sbr->q_m[e][m] * sbr->q_m[e][m];
01552 }
01553 gain_boost = sqrtf((FLT_EPSILON + sum[0]) / (FLT_EPSILON + sum[1]));
01554 gain_boost = FFMIN(1.584893192f, gain_boost);
01555 for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
01556 sbr->gain[e][m] *= gain_boost;
01557 sbr->q_m[e][m] *= gain_boost;
01558 sbr->s_m[e][m] *= gain_boost;
01559 }
01560 }
01561 }
01562 }
01563
01565 static void sbr_hf_assemble(float Y1[38][64][2],
01566 const float X_high[64][40][2],
01567 SpectralBandReplication *sbr, SBRData *ch_data,
01568 const int e_a[2])
01569 {
01570 int e, i, j, m;
01571 const int h_SL = 4 * !sbr->bs_smoothing_mode;
01572 const int kx = sbr->kx[1];
01573 const int m_max = sbr->m[1];
01574 static const float h_smooth[5] = {
01575 0.33333333333333,
01576 0.30150283239582,
01577 0.21816949906249,
01578 0.11516383427084,
01579 0.03183050093751,
01580 };
01581 float (*g_temp)[48] = ch_data->g_temp, (*q_temp)[48] = ch_data->q_temp;
01582 int indexnoise = ch_data->f_indexnoise;
01583 int indexsine = ch_data->f_indexsine;
01584
01585 if (sbr->reset) {
01586 for (i = 0; i < h_SL; i++) {
01587 memcpy(g_temp[i + 2*ch_data->t_env[0]], sbr->gain[0], m_max * sizeof(sbr->gain[0][0]));
01588 memcpy(q_temp[i + 2*ch_data->t_env[0]], sbr->q_m[0], m_max * sizeof(sbr->q_m[0][0]));
01589 }
01590 } else if (h_SL) {
01591 memcpy(g_temp[2*ch_data->t_env[0]], g_temp[2*ch_data->t_env_num_env_old], 4*sizeof(g_temp[0]));
01592 memcpy(q_temp[2*ch_data->t_env[0]], q_temp[2*ch_data->t_env_num_env_old], 4*sizeof(q_temp[0]));
01593 }
01594
01595 for (e = 0; e < ch_data->bs_num_env; e++) {
01596 for (i = 2 * ch_data->t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) {
01597 memcpy(g_temp[h_SL + i], sbr->gain[e], m_max * sizeof(sbr->gain[0][0]));
01598 memcpy(q_temp[h_SL + i], sbr->q_m[e], m_max * sizeof(sbr->q_m[0][0]));
01599 }
01600 }
01601
01602 for (e = 0; e < ch_data->bs_num_env; e++) {
01603 for (i = 2 * ch_data->t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) {
01604 LOCAL_ALIGNED_16(float, g_filt_tab, [48]);
01605 LOCAL_ALIGNED_16(float, q_filt_tab, [48]);
01606 float *g_filt, *q_filt;
01607
01608 if (h_SL && e != e_a[0] && e != e_a[1]) {
01609 g_filt = g_filt_tab;
01610 q_filt = q_filt_tab;
01611 for (m = 0; m < m_max; m++) {
01612 const int idx1 = i + h_SL;
01613 g_filt[m] = 0.0f;
01614 q_filt[m] = 0.0f;
01615 for (j = 0; j <= h_SL; j++) {
01616 g_filt[m] += g_temp[idx1 - j][m] * h_smooth[j];
01617 q_filt[m] += q_temp[idx1 - j][m] * h_smooth[j];
01618 }
01619 }
01620 } else {
01621 g_filt = g_temp[i + h_SL];
01622 q_filt = q_temp[i];
01623 }
01624
01625 sbr->dsp.hf_g_filt(Y1[i] + kx, X_high + kx, g_filt, m_max,
01626 i + ENVELOPE_ADJUSTMENT_OFFSET);
01627
01628 if (e != e_a[0] && e != e_a[1]) {
01629 sbr->dsp.hf_apply_noise[indexsine](Y1[i] + kx, sbr->s_m[e],
01630 q_filt, indexnoise,
01631 kx, m_max);
01632 } else {
01633 int idx = indexsine&1;
01634 int A = (1-((indexsine+(kx & 1))&2));
01635 int B = (A^(-idx)) + idx;
01636 float *out = &Y1[i][kx][idx];
01637 float *in = sbr->s_m[e];
01638 for (m = 0; m+1 < m_max; m+=2) {
01639 out[2*m ] += in[m ] * A;
01640 out[2*m+2] += in[m+1] * B;
01641 }
01642 if(m_max&1)
01643 out[2*m ] += in[m ] * A;
01644 }
01645 indexnoise = (indexnoise + m_max) & 0x1ff;
01646 indexsine = (indexsine + 1) & 3;
01647 }
01648 }
01649 ch_data->f_indexnoise = indexnoise;
01650 ch_data->f_indexsine = indexsine;
01651 }
01652
01653 void ff_sbr_apply(AACContext *ac, SpectralBandReplication *sbr, int id_aac,
01654 float* L, float* R)
01655 {
01656 int downsampled = ac->oc[1].m4ac.ext_sample_rate < sbr->sample_rate;
01657 int ch;
01658 int nch = (id_aac == TYPE_CPE) ? 2 : 1;
01659 int err;
01660
01661 if (!sbr->kx_and_m_pushed) {
01662 sbr->kx[0] = sbr->kx[1];
01663 sbr->m[0] = sbr->m[1];
01664 } else {
01665 sbr->kx_and_m_pushed = 0;
01666 }
01667
01668 if (sbr->start) {
01669 sbr_dequant(sbr, id_aac);
01670 }
01671 for (ch = 0; ch < nch; ch++) {
01672
01673 sbr_qmf_analysis(&ac->dsp, &sbr->mdct_ana, &sbr->dsp, ch ? R : L, sbr->data[ch].analysis_filterbank_samples,
01674 (float*)sbr->qmf_filter_scratch,
01675 sbr->data[ch].W);
01676 sbr_lf_gen(ac, sbr, sbr->X_low, sbr->data[ch].W);
01677 sbr->data[ch].Ypos ^= 1;
01678 if (sbr->start) {
01679 sbr_hf_inverse_filter(&sbr->dsp, sbr->alpha0, sbr->alpha1, sbr->X_low, sbr->k[0]);
01680 sbr_chirp(sbr, &sbr->data[ch]);
01681 sbr_hf_gen(ac, sbr, sbr->X_high, sbr->X_low, sbr->alpha0, sbr->alpha1,
01682 sbr->data[ch].bw_array, sbr->data[ch].t_env,
01683 sbr->data[ch].bs_num_env);
01684
01685
01686 err = sbr_mapping(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
01687 if (!err) {
01688 sbr_env_estimate(sbr->e_curr, sbr->X_high, sbr, &sbr->data[ch]);
01689 sbr_gain_calc(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);
01690 sbr_hf_assemble(sbr->data[ch].Y[sbr->data[ch].Ypos],
01691 sbr->X_high, sbr, &sbr->data[ch],
01692 sbr->data[ch].e_a);
01693 }
01694 }
01695
01696
01697 sbr_x_gen(sbr, sbr->X[ch],
01698 sbr->data[ch].Y[1-sbr->data[ch].Ypos],
01699 sbr->data[ch].Y[ sbr->data[ch].Ypos],
01700 sbr->X_low, ch);
01701 }
01702
01703 if (ac->oc[1].m4ac.ps == 1) {
01704 if (sbr->ps.start) {
01705 ff_ps_apply(ac->avctx, &sbr->ps, sbr->X[0], sbr->X[1], sbr->kx[1] + sbr->m[1]);
01706 } else {
01707 memcpy(sbr->X[1], sbr->X[0], sizeof(sbr->X[0]));
01708 }
01709 nch = 2;
01710 }
01711
01712 sbr_qmf_synthesis(&ac->dsp, &sbr->mdct, &sbr->dsp, L, sbr->X[0], sbr->qmf_filter_scratch,
01713 sbr->data[0].synthesis_filterbank_samples,
01714 &sbr->data[0].synthesis_filterbank_samples_offset,
01715 downsampled);
01716 if (nch == 2)
01717 sbr_qmf_synthesis(&ac->dsp, &sbr->mdct, &sbr->dsp, R, sbr->X[1], sbr->qmf_filter_scratch,
01718 sbr->data[1].synthesis_filterbank_samples,
01719 &sbr->data[1].synthesis_filterbank_samples_offset,
01720 downsampled);
01721 }