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