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