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 if(sbr->mdct.mdct_bits)
00135 return;
00136 sbr->kx[0] = sbr->kx[1] = 32;
00137 sbr->data[0].e_a[1] = sbr->data[1].e_a[1] = -1;
00138 sbr->data[0].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
00139 sbr->data[1].synthesis_filterbank_samples_offset = SBR_SYNTHESIS_BUF_SIZE - (1280 - 128);
00140
00141
00142
00143 mdct_scale = ac->avctx->sample_fmt == AV_SAMPLE_FMT_FLT ? 32768.0f : 1.0f;
00144 ff_mdct_init(&sbr->mdct, 7, 1, 1.0 / (64 * mdct_scale));
00145 ff_mdct_init(&sbr->mdct_ana, 7, 1, -2.0 * mdct_scale);
00146 ff_ps_ctx_init(&sbr->ps);
00147 }
00148
00149 av_cold void ff_aac_sbr_ctx_close(SpectralBandReplication *sbr)
00150 {
00151 ff_mdct_end(&sbr->mdct);
00152 ff_mdct_end(&sbr->mdct_ana);
00153 }
00154
00155 static int qsort_comparison_function_int16(const void *a, const void *b)
00156 {
00157 return *(const int16_t *)a - *(const int16_t *)b;
00158 }
00159
00160 static inline int in_table_int16(const int16_t *table, int last_el, int16_t needle)
00161 {
00162 int i;
00163 for (i = 0; i <= last_el; i++)
00164 if (table[i] == needle)
00165 return 1;
00166 return 0;
00167 }
00168
00170 static void sbr_make_f_tablelim(SpectralBandReplication *sbr)
00171 {
00172 int k;
00173 if (sbr->bs_limiter_bands > 0) {
00174 static const float bands_warped[3] = { 1.32715174233856803909f,
00175 1.18509277094158210129f,
00176 1.11987160404675912501f };
00177 const float lim_bands_per_octave_warped = bands_warped[sbr->bs_limiter_bands - 1];
00178 int16_t patch_borders[7];
00179 uint16_t *in = sbr->f_tablelim + 1, *out = sbr->f_tablelim;
00180
00181 patch_borders[0] = sbr->kx[1];
00182 for (k = 1; k <= sbr->num_patches; k++)
00183 patch_borders[k] = patch_borders[k-1] + sbr->patch_num_subbands[k-1];
00184
00185 memcpy(sbr->f_tablelim, sbr->f_tablelow,
00186 (sbr->n[0] + 1) * sizeof(sbr->f_tablelow[0]));
00187 if (sbr->num_patches > 1)
00188 memcpy(sbr->f_tablelim + sbr->n[0] + 1, patch_borders + 1,
00189 (sbr->num_patches - 1) * sizeof(patch_borders[0]));
00190
00191 qsort(sbr->f_tablelim, sbr->num_patches + sbr->n[0],
00192 sizeof(sbr->f_tablelim[0]),
00193 qsort_comparison_function_int16);
00194
00195 sbr->n_lim = sbr->n[0] + sbr->num_patches - 1;
00196 while (out < sbr->f_tablelim + sbr->n_lim) {
00197 if (*in >= *out * lim_bands_per_octave_warped) {
00198 *++out = *in++;
00199 } else if (*in == *out ||
00200 !in_table_int16(patch_borders, sbr->num_patches, *in)) {
00201 in++;
00202 sbr->n_lim--;
00203 } else if (!in_table_int16(patch_borders, sbr->num_patches, *out)) {
00204 *out = *in++;
00205 sbr->n_lim--;
00206 } else {
00207 *++out = *in++;
00208 }
00209 }
00210 } else {
00211 sbr->f_tablelim[0] = sbr->f_tablelow[0];
00212 sbr->f_tablelim[1] = sbr->f_tablelow[sbr->n[0]];
00213 sbr->n_lim = 1;
00214 }
00215 }
00216
00217 static unsigned int read_sbr_header(SpectralBandReplication *sbr, GetBitContext *gb)
00218 {
00219 unsigned int cnt = get_bits_count(gb);
00220 uint8_t bs_header_extra_1;
00221 uint8_t bs_header_extra_2;
00222 int old_bs_limiter_bands = sbr->bs_limiter_bands;
00223 SpectrumParameters old_spectrum_params;
00224
00225 sbr->start = 1;
00226
00227
00228 memcpy(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters));
00229
00230 sbr->bs_amp_res_header = get_bits1(gb);
00231 sbr->spectrum_params.bs_start_freq = get_bits(gb, 4);
00232 sbr->spectrum_params.bs_stop_freq = get_bits(gb, 4);
00233 sbr->spectrum_params.bs_xover_band = get_bits(gb, 3);
00234 skip_bits(gb, 2);
00235
00236 bs_header_extra_1 = get_bits1(gb);
00237 bs_header_extra_2 = get_bits1(gb);
00238
00239 if (bs_header_extra_1) {
00240 sbr->spectrum_params.bs_freq_scale = get_bits(gb, 2);
00241 sbr->spectrum_params.bs_alter_scale = get_bits1(gb);
00242 sbr->spectrum_params.bs_noise_bands = get_bits(gb, 2);
00243 } else {
00244 sbr->spectrum_params.bs_freq_scale = 2;
00245 sbr->spectrum_params.bs_alter_scale = 1;
00246 sbr->spectrum_params.bs_noise_bands = 2;
00247 }
00248
00249
00250 if (memcmp(&old_spectrum_params, &sbr->spectrum_params, sizeof(SpectrumParameters)))
00251 sbr->reset = 1;
00252
00253 if (bs_header_extra_2) {
00254 sbr->bs_limiter_bands = get_bits(gb, 2);
00255 sbr->bs_limiter_gains = get_bits(gb, 2);
00256 sbr->bs_interpol_freq = get_bits1(gb);
00257 sbr->bs_smoothing_mode = get_bits1(gb);
00258 } else {
00259 sbr->bs_limiter_bands = 2;
00260 sbr->bs_limiter_gains = 2;
00261 sbr->bs_interpol_freq = 1;
00262 sbr->bs_smoothing_mode = 1;
00263 }
00264
00265 if (sbr->bs_limiter_bands != old_bs_limiter_bands && !sbr->reset)
00266 sbr_make_f_tablelim(sbr);
00267
00268 return get_bits_count(gb) - cnt;
00269 }
00270
00271 static int array_min_int16(const int16_t *array, int nel)
00272 {
00273 int i, min = array[0];
00274 for (i = 1; i < nel; i++)
00275 min = FFMIN(array[i], min);
00276 return min;
00277 }
00278
00279 static void make_bands(int16_t* bands, int start, int stop, int num_bands)
00280 {
00281 int k, previous, present;
00282 float base, prod;
00283
00284 base = powf((float)stop / start, 1.0f / num_bands);
00285 prod = start;
00286 previous = start;
00287
00288 for (k = 0; k < num_bands-1; k++) {
00289 prod *= base;
00290 present = lrintf(prod);
00291 bands[k] = present - previous;
00292 previous = present;
00293 }
00294 bands[num_bands-1] = stop - previous;
00295 }
00296
00297 static int check_n_master(AVCodecContext *avctx, int n_master, int bs_xover_band)
00298 {
00299
00300 if (n_master <= 0) {
00301 av_log(avctx, AV_LOG_ERROR, "Invalid n_master: %d\n", n_master);
00302 return -1;
00303 }
00304 if (bs_xover_band >= n_master) {
00305 av_log(avctx, AV_LOG_ERROR,
00306 "Invalid bitstream, crossover band index beyond array bounds: %d\n",
00307 bs_xover_band);
00308 return -1;
00309 }
00310 return 0;
00311 }
00312
00314 static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr,
00315 SpectrumParameters *spectrum)
00316 {
00317 unsigned int temp, max_qmf_subbands;
00318 unsigned int start_min, stop_min;
00319 int k;
00320 const int8_t *sbr_offset_ptr;
00321 int16_t stop_dk[13];
00322
00323 if (sbr->sample_rate < 32000) {
00324 temp = 3000;
00325 } else if (sbr->sample_rate < 64000) {
00326 temp = 4000;
00327 } else
00328 temp = 5000;
00329
00330 start_min = ((temp << 7) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
00331 stop_min = ((temp << 8) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
00332
00333 switch (sbr->sample_rate) {
00334 case 16000:
00335 sbr_offset_ptr = sbr_offset[0];
00336 break;
00337 case 22050:
00338 sbr_offset_ptr = sbr_offset[1];
00339 break;
00340 case 24000:
00341 sbr_offset_ptr = sbr_offset[2];
00342 break;
00343 case 32000:
00344 sbr_offset_ptr = sbr_offset[3];
00345 break;
00346 case 44100: case 48000: case 64000:
00347 sbr_offset_ptr = sbr_offset[4];
00348 break;
00349 case 88200: case 96000: case 128000: case 176400: case 192000:
00350 sbr_offset_ptr = sbr_offset[5];
00351 break;
00352 default:
00353 av_log(ac->avctx, AV_LOG_ERROR,
00354 "Unsupported sample rate for SBR: %d\n", sbr->sample_rate);
00355 return -1;
00356 }
00357
00358 sbr->k[0] = start_min + sbr_offset_ptr[spectrum->bs_start_freq];
00359
00360 if (spectrum->bs_stop_freq < 14) {
00361 sbr->k[2] = stop_min;
00362 make_bands(stop_dk, stop_min, 64, 13);
00363 qsort(stop_dk, 13, sizeof(stop_dk[0]), qsort_comparison_function_int16);
00364 for (k = 0; k < spectrum->bs_stop_freq; k++)
00365 sbr->k[2] += stop_dk[k];
00366 } else if (spectrum->bs_stop_freq == 14) {
00367 sbr->k[2] = 2*sbr->k[0];
00368 } else if (spectrum->bs_stop_freq == 15) {
00369 sbr->k[2] = 3*sbr->k[0];
00370 } else {
00371 av_log(ac->avctx, AV_LOG_ERROR,
00372 "Invalid bs_stop_freq: %d\n", spectrum->bs_stop_freq);
00373 return -1;
00374 }
00375 sbr->k[2] = FFMIN(64, sbr->k[2]);
00376
00377
00378 if (sbr->sample_rate <= 32000) {
00379 max_qmf_subbands = 48;
00380 } else if (sbr->sample_rate == 44100) {
00381 max_qmf_subbands = 35;
00382 } else if (sbr->sample_rate >= 48000)
00383 max_qmf_subbands = 32;
00384
00385 if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) {
00386 av_log(ac->avctx, AV_LOG_ERROR,
00387 "Invalid bitstream, too many QMF subbands: %d\n", sbr->k[2] - sbr->k[0]);
00388 return -1;
00389 }
00390
00391 if (!spectrum->bs_freq_scale) {
00392 int dk, k2diff;
00393
00394 dk = spectrum->bs_alter_scale + 1;
00395 sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1;
00396 if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
00397 return -1;
00398
00399 for (k = 1; k <= sbr->n_master; k++)
00400 sbr->f_master[k] = dk;
00401
00402 k2diff = sbr->k[2] - sbr->k[0] - sbr->n_master * dk;
00403 if (k2diff < 0) {
00404 sbr->f_master[1]--;
00405 sbr->f_master[2]-= (k2diff < -1);
00406 } else if (k2diff) {
00407 sbr->f_master[sbr->n_master]++;
00408 }
00409
00410 sbr->f_master[0] = sbr->k[0];
00411 for (k = 1; k <= sbr->n_master; k++)
00412 sbr->f_master[k] += sbr->f_master[k - 1];
00413
00414 } else {
00415 int half_bands = 7 - spectrum->bs_freq_scale;
00416 int two_regions, num_bands_0;
00417 int vdk0_max, vdk1_min;
00418 int16_t vk0[49];
00419
00420 if (49 * sbr->k[2] > 110 * sbr->k[0]) {
00421 two_regions = 1;
00422 sbr->k[1] = 2 * sbr->k[0];
00423 } else {
00424 two_regions = 0;
00425 sbr->k[1] = sbr->k[2];
00426 }
00427
00428 num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2;
00429
00430 if (num_bands_0 <= 0) {
00431 av_log(ac->avctx, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0);
00432 return -1;
00433 }
00434
00435 vk0[0] = 0;
00436
00437 make_bands(vk0+1, sbr->k[0], sbr->k[1], num_bands_0);
00438
00439 qsort(vk0 + 1, num_bands_0, sizeof(vk0[1]), qsort_comparison_function_int16);
00440 vdk0_max = vk0[num_bands_0];
00441
00442 vk0[0] = sbr->k[0];
00443 for (k = 1; k <= num_bands_0; k++) {
00444 if (vk0[k] <= 0) {
00445 av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]);
00446 return -1;
00447 }
00448 vk0[k] += vk0[k-1];
00449 }
00450
00451 if (two_regions) {
00452 int16_t vk1[49];
00453 float invwarp = spectrum->bs_alter_scale ? 0.76923076923076923077f
00454 : 1.0f;
00455 int num_bands_1 = lrintf(half_bands * invwarp *
00456 log2f(sbr->k[2] / (float)sbr->k[1])) * 2;
00457
00458 make_bands(vk1+1, sbr->k[1], sbr->k[2], num_bands_1);
00459
00460 vdk1_min = array_min_int16(vk1 + 1, num_bands_1);
00461
00462 if (vdk1_min < vdk0_max) {
00463 int change;
00464 qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16);
00465 change = FFMIN(vdk0_max - vk1[1], (vk1[num_bands_1] - vk1[1]) >> 1);
00466 vk1[1] += change;
00467 vk1[num_bands_1] -= change;
00468 }
00469
00470 qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16);
00471
00472 vk1[0] = sbr->k[1];
00473 for (k = 1; k <= num_bands_1; k++) {
00474 if (vk1[k] <= 0) {
00475 av_log(ac->avctx, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]);
00476 return -1;
00477 }
00478 vk1[k] += vk1[k-1];
00479 }
00480
00481 sbr->n_master = num_bands_0 + num_bands_1;
00482 if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
00483 return -1;
00484 memcpy(&sbr->f_master[0], vk0,
00485 (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
00486 memcpy(&sbr->f_master[num_bands_0 + 1], vk1 + 1,
00487 num_bands_1 * sizeof(sbr->f_master[0]));
00488
00489 } else {
00490 sbr->n_master = num_bands_0;
00491 if (check_n_master(ac->avctx, sbr->n_master, sbr->spectrum_params.bs_xover_band))
00492 return -1;
00493 memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
00494 }
00495 }
00496
00497 return 0;
00498 }
00499
00501 static int sbr_hf_calc_npatches(AACContext *ac, SpectralBandReplication *sbr)
00502 {
00503 int i, k, sb = 0;
00504 int msb = sbr->k[0];
00505 int usb = sbr->kx[1];
00506 int goal_sb = ((1000 << 11) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
00507
00508 sbr->num_patches = 0;
00509
00510 if (goal_sb < sbr->kx[1] + sbr->m[1]) {
00511 for (k = 0; sbr->f_master[k] < goal_sb; k++) ;
00512 } else
00513 k = sbr->n_master;
00514
00515 do {
00516 int odd = 0;
00517 for (i = k; i == k || sb > (sbr->k[0] - 1 + msb - odd); i--) {
00518 sb = sbr->f_master[i];
00519 odd = (sb + sbr->k[0]) & 1;
00520 }
00521
00522
00523
00524
00525
00526 if (sbr->num_patches > 5) {
00527 av_log(ac->avctx, AV_LOG_ERROR, "Too many patches: %d\n", sbr->num_patches);
00528 return -1;
00529 }
00530
00531 sbr->patch_num_subbands[sbr->num_patches] = FFMAX(sb - usb, 0);
00532 sbr->patch_start_subband[sbr->num_patches] = sbr->k[0] - odd - sbr->patch_num_subbands[sbr->num_patches];
00533
00534 if (sbr->patch_num_subbands[sbr->num_patches] > 0) {
00535 usb = sb;
00536 msb = sb;
00537 sbr->num_patches++;
00538 } else
00539 msb = sbr->kx[1];
00540
00541 if (sbr->f_master[k] - sb < 3)
00542 k = sbr->n_master;
00543 } while (sb != sbr->kx[1] + sbr->m[1]);
00544
00545 if (sbr->patch_num_subbands[sbr->num_patches-1] < 3 && sbr->num_patches > 1)
00546 sbr->num_patches--;
00547
00548 return 0;
00549 }
00550
00552 static int sbr_make_f_derived(AACContext *ac, SpectralBandReplication *sbr)
00553 {
00554 int k, temp;
00555
00556 sbr->n[1] = sbr->n_master - sbr->spectrum_params.bs_xover_band;
00557 sbr->n[0] = (sbr->n[1] + 1) >> 1;
00558
00559 memcpy(sbr->f_tablehigh, &sbr->f_master[sbr->spectrum_params.bs_xover_band],
00560 (sbr->n[1] + 1) * sizeof(sbr->f_master[0]));
00561 sbr->m[1] = sbr->f_tablehigh[sbr->n[1]] - sbr->f_tablehigh[0];
00562 sbr->kx[1] = sbr->f_tablehigh[0];
00563
00564
00565 if (sbr->kx[1] + sbr->m[1] > 64) {
00566 av_log(ac->avctx, AV_LOG_ERROR,
00567 "Stop frequency border too high: %d\n", sbr->kx[1] + sbr->m[1]);
00568 return -1;
00569 }
00570 if (sbr->kx[1] > 32) {
00571 av_log(ac->avctx, AV_LOG_ERROR, "Start frequency border too high: %d\n", sbr->kx[1]);
00572 return -1;
00573 }
00574
00575 sbr->f_tablelow[0] = sbr->f_tablehigh[0];
00576 temp = sbr->n[1] & 1;
00577 for (k = 1; k <= sbr->n[0]; k++)
00578 sbr->f_tablelow[k] = sbr->f_tablehigh[2 * k - temp];
00579
00580 sbr->n_q = FFMAX(1, lrintf(sbr->spectrum_params.bs_noise_bands *
00581 log2f(sbr->k[2] / (float)sbr->kx[1])));
00582 if (sbr->n_q > 5) {
00583 av_log(ac->avctx, AV_LOG_ERROR, "Too many noise floor scale factors: %d\n", sbr->n_q);
00584 return -1;
00585 }
00586
00587 sbr->f_tablenoise[0] = sbr->f_tablelow[0];
00588 temp = 0;
00589 for (k = 1; k <= sbr->n_q; k++) {
00590 temp += (sbr->n[0] - temp) / (sbr->n_q + 1 - k);
00591 sbr->f_tablenoise[k] = sbr->f_tablelow[temp];
00592 }
00593
00594 if (sbr_hf_calc_npatches(ac, sbr) < 0)
00595 return -1;
00596
00597 sbr_make_f_tablelim(sbr);
00598
00599 sbr->data[0].f_indexnoise = 0;
00600 sbr->data[1].f_indexnoise = 0;
00601
00602 return 0;
00603 }
00604
00605 static av_always_inline void get_bits1_vector(GetBitContext *gb, uint8_t *vec,
00606 int elements)
00607 {
00608 int i;
00609 for (i = 0; i < elements; i++) {
00610 vec[i] = get_bits1(gb);
00611 }
00612 }
00613
00615 static const int8_t ceil_log2[] = {
00616 0, 1, 2, 2, 3, 3,
00617 };
00618
00619 static int read_sbr_grid(AACContext *ac, SpectralBandReplication *sbr,
00620 GetBitContext *gb, SBRData *ch_data)
00621 {
00622 int i;
00623 unsigned bs_pointer = 0;
00624
00625 int abs_bord_trail = 16;
00626 int num_rel_lead, num_rel_trail;
00627 unsigned bs_num_env_old = ch_data->bs_num_env;
00628
00629 ch_data->bs_freq_res[0] = ch_data->bs_freq_res[ch_data->bs_num_env];
00630 ch_data->bs_amp_res = sbr->bs_amp_res_header;
00631 ch_data->t_env_num_env_old = ch_data->t_env[bs_num_env_old];
00632
00633 switch (ch_data->bs_frame_class = get_bits(gb, 2)) {
00634 case FIXFIX:
00635 ch_data->bs_num_env = 1 << get_bits(gb, 2);
00636 num_rel_lead = ch_data->bs_num_env - 1;
00637 if (ch_data->bs_num_env == 1)
00638 ch_data->bs_amp_res = 0;
00639
00640 if (ch_data->bs_num_env > 4) {
00641 av_log(ac->avctx, AV_LOG_ERROR,
00642 "Invalid bitstream, too many SBR envelopes in FIXFIX type SBR frame: %d\n",
00643 ch_data->bs_num_env);
00644 return -1;
00645 }
00646
00647 ch_data->t_env[0] = 0;
00648 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
00649
00650 abs_bord_trail = (abs_bord_trail + (ch_data->bs_num_env >> 1)) /
00651 ch_data->bs_num_env;
00652 for (i = 0; i < num_rel_lead; i++)
00653 ch_data->t_env[i + 1] = ch_data->t_env[i] + abs_bord_trail;
00654
00655 ch_data->bs_freq_res[1] = get_bits1(gb);
00656 for (i = 1; i < ch_data->bs_num_env; i++)
00657 ch_data->bs_freq_res[i + 1] = ch_data->bs_freq_res[1];
00658 break;
00659 case FIXVAR:
00660 abs_bord_trail += get_bits(gb, 2);
00661 num_rel_trail = get_bits(gb, 2);
00662 ch_data->bs_num_env = num_rel_trail + 1;
00663 ch_data->t_env[0] = 0;
00664 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
00665
00666 for (i = 0; i < num_rel_trail; i++)
00667 ch_data->t_env[ch_data->bs_num_env - 1 - i] =
00668 ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
00669
00670 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
00671
00672 for (i = 0; i < ch_data->bs_num_env; i++)
00673 ch_data->bs_freq_res[ch_data->bs_num_env - i] = get_bits1(gb);
00674 break;
00675 case VARFIX:
00676 ch_data->t_env[0] = get_bits(gb, 2);
00677 num_rel_lead = get_bits(gb, 2);
00678 ch_data->bs_num_env = num_rel_lead + 1;
00679 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
00680
00681 for (i = 0; i < num_rel_lead; i++)
00682 ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
00683
00684 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
00685
00686 get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
00687 break;
00688 case VARVAR:
00689 ch_data->t_env[0] = get_bits(gb, 2);
00690 abs_bord_trail += get_bits(gb, 2);
00691 num_rel_lead = get_bits(gb, 2);
00692 num_rel_trail = get_bits(gb, 2);
00693 ch_data->bs_num_env = num_rel_lead + num_rel_trail + 1;
00694
00695 if (ch_data->bs_num_env > 5) {
00696 av_log(ac->avctx, AV_LOG_ERROR,
00697 "Invalid bitstream, too many SBR envelopes in VARVAR type SBR frame: %d\n",
00698 ch_data->bs_num_env);
00699 return -1;
00700 }
00701
00702 ch_data->t_env[ch_data->bs_num_env] = abs_bord_trail;
00703
00704 for (i = 0; i < num_rel_lead; i++)
00705 ch_data->t_env[i + 1] = ch_data->t_env[i] + 2 * get_bits(gb, 2) + 2;
00706 for (i = 0; i < num_rel_trail; i++)
00707 ch_data->t_env[ch_data->bs_num_env - 1 - i] =
00708 ch_data->t_env[ch_data->bs_num_env - i] - 2 * get_bits(gb, 2) - 2;
00709
00710 bs_pointer = get_bits(gb, ceil_log2[ch_data->bs_num_env]);
00711
00712 get_bits1_vector(gb, ch_data->bs_freq_res + 1, ch_data->bs_num_env);
00713 break;
00714 }
00715
00716 if (bs_pointer > ch_data->bs_num_env + 1) {
00717 av_log(ac->avctx, AV_LOG_ERROR,
00718 "Invalid bitstream, bs_pointer points to a middle noise border outside the time borders table: %d\n",
00719 bs_pointer);
00720 return -1;
00721 }
00722
00723 for (i = 1; i <= ch_data->bs_num_env; i++) {
00724 if (ch_data->t_env[i-1] > ch_data->t_env[i]) {
00725 av_log(ac->avctx, AV_LOG_ERROR, "Non monotone time borders\n");
00726 return -1;
00727 }
00728 }
00729
00730 ch_data->bs_num_noise = (ch_data->bs_num_env > 1) + 1;
00731
00732 ch_data->t_q[0] = ch_data->t_env[0];
00733 ch_data->t_q[ch_data->bs_num_noise] = ch_data->t_env[ch_data->bs_num_env];
00734 if (ch_data->bs_num_noise > 1) {
00735 unsigned int idx;
00736 if (ch_data->bs_frame_class == FIXFIX) {
00737 idx = ch_data->bs_num_env >> 1;
00738 } else if (ch_data->bs_frame_class & 1) {
00739 idx = ch_data->bs_num_env - FFMAX(bs_pointer - 1, 1);
00740 } else {
00741 if (!bs_pointer)
00742 idx = 1;
00743 else if (bs_pointer == 1)
00744 idx = ch_data->bs_num_env - 1;
00745 else
00746 idx = bs_pointer - 1;
00747 }
00748 ch_data->t_q[1] = ch_data->t_env[idx];
00749 }
00750
00751 ch_data->e_a[0] = -(ch_data->e_a[1] != bs_num_env_old);
00752 ch_data->e_a[1] = -1;
00753 if ((ch_data->bs_frame_class & 1) && bs_pointer) {
00754 ch_data->e_a[1] = ch_data->bs_num_env + 1 - bs_pointer;
00755 } else if ((ch_data->bs_frame_class == 2) && (bs_pointer > 1))
00756 ch_data->e_a[1] = bs_pointer - 1;
00757
00758 return 0;
00759 }
00760
00761 static void copy_sbr_grid(SBRData *dst, const SBRData *src) {
00762
00763 dst->bs_freq_res[0] = dst->bs_freq_res[dst->bs_num_env];
00764 dst->t_env_num_env_old = dst->t_env[dst->bs_num_env];
00765 dst->e_a[0] = -(dst->e_a[1] != dst->bs_num_env);
00766
00767
00768 memcpy(dst->bs_freq_res+1, src->bs_freq_res+1, sizeof(dst->bs_freq_res)-sizeof(*dst->bs_freq_res));
00769 memcpy(dst->t_env, src->t_env, sizeof(dst->t_env));
00770 memcpy(dst->t_q, src->t_q, sizeof(dst->t_q));
00771 dst->bs_num_env = src->bs_num_env;
00772 dst->bs_amp_res = src->bs_amp_res;
00773 dst->bs_num_noise = src->bs_num_noise;
00774 dst->bs_frame_class = src->bs_frame_class;
00775 dst->e_a[1] = src->e_a[1];
00776 }
00777
00779 static void read_sbr_dtdf(SpectralBandReplication *sbr, GetBitContext *gb,
00780 SBRData *ch_data)
00781 {
00782 get_bits1_vector(gb, ch_data->bs_df_env, ch_data->bs_num_env);
00783 get_bits1_vector(gb, ch_data->bs_df_noise, ch_data->bs_num_noise);
00784 }
00785
00787 static void read_sbr_invf(SpectralBandReplication *sbr, GetBitContext *gb,
00788 SBRData *ch_data)
00789 {
00790 int i;
00791
00792 memcpy(ch_data->bs_invf_mode[1], ch_data->bs_invf_mode[0], 5 * sizeof(uint8_t));
00793 for (i = 0; i < sbr->n_q; i++)
00794 ch_data->bs_invf_mode[0][i] = get_bits(gb, 2);
00795 }
00796
00797 static void read_sbr_envelope(SpectralBandReplication *sbr, GetBitContext *gb,
00798 SBRData *ch_data, int ch)
00799 {
00800 int bits;
00801 int i, j, k;
00802 VLC_TYPE (*t_huff)[2], (*f_huff)[2];
00803 int t_lav, f_lav;
00804 const int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
00805 const int odd = sbr->n[1] & 1;
00806
00807 if (sbr->bs_coupling && ch) {
00808 if (ch_data->bs_amp_res) {
00809 bits = 5;
00810 t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_3_0DB].table;
00811 t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_3_0DB];
00812 f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
00813 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB];
00814 } else {
00815 bits = 6;
00816 t_huff = vlc_sbr[T_HUFFMAN_ENV_BAL_1_5DB].table;
00817 t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_BAL_1_5DB];
00818 f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_1_5DB].table;
00819 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_1_5DB];
00820 }
00821 } else {
00822 if (ch_data->bs_amp_res) {
00823 bits = 6;
00824 t_huff = vlc_sbr[T_HUFFMAN_ENV_3_0DB].table;
00825 t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_3_0DB];
00826 f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
00827 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB];
00828 } else {
00829 bits = 7;
00830 t_huff = vlc_sbr[T_HUFFMAN_ENV_1_5DB].table;
00831 t_lav = vlc_sbr_lav[T_HUFFMAN_ENV_1_5DB];
00832 f_huff = vlc_sbr[F_HUFFMAN_ENV_1_5DB].table;
00833 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_1_5DB];
00834 }
00835 }
00836
00837 for (i = 0; i < ch_data->bs_num_env; i++) {
00838 if (ch_data->bs_df_env[i]) {
00839
00840 if (ch_data->bs_freq_res[i + 1] == ch_data->bs_freq_res[i]) {
00841 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++)
00842 ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][j] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
00843 } else if (ch_data->bs_freq_res[i + 1]) {
00844 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
00845 k = (j + odd) >> 1;
00846 ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
00847 }
00848 } else {
00849 for (j = 0; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++) {
00850 k = j ? 2*j - odd : 0;
00851 ch_data->env_facs[i + 1][j] = ch_data->env_facs[i][k] + delta * (get_vlc2(gb, t_huff, 9, 3) - t_lav);
00852 }
00853 }
00854 } else {
00855 ch_data->env_facs[i + 1][0] = delta * get_bits(gb, bits);
00856 for (j = 1; j < sbr->n[ch_data->bs_freq_res[i + 1]]; j++)
00857 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);
00858 }
00859 }
00860
00861
00862 memcpy(ch_data->env_facs[0], ch_data->env_facs[ch_data->bs_num_env],
00863 sizeof(ch_data->env_facs[0]));
00864 }
00865
00866 static void read_sbr_noise(SpectralBandReplication *sbr, GetBitContext *gb,
00867 SBRData *ch_data, int ch)
00868 {
00869 int i, j;
00870 VLC_TYPE (*t_huff)[2], (*f_huff)[2];
00871 int t_lav, f_lav;
00872 int delta = (ch == 1 && sbr->bs_coupling == 1) + 1;
00873
00874 if (sbr->bs_coupling && ch) {
00875 t_huff = vlc_sbr[T_HUFFMAN_NOISE_BAL_3_0DB].table;
00876 t_lav = vlc_sbr_lav[T_HUFFMAN_NOISE_BAL_3_0DB];
00877 f_huff = vlc_sbr[F_HUFFMAN_ENV_BAL_3_0DB].table;
00878 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_BAL_3_0DB];
00879 } else {
00880 t_huff = vlc_sbr[T_HUFFMAN_NOISE_3_0DB].table;
00881 t_lav = vlc_sbr_lav[T_HUFFMAN_NOISE_3_0DB];
00882 f_huff = vlc_sbr[F_HUFFMAN_ENV_3_0DB].table;
00883 f_lav = vlc_sbr_lav[F_HUFFMAN_ENV_3_0DB];
00884 }
00885
00886 for (i = 0; i < ch_data->bs_num_noise; i++) {
00887 if (ch_data->bs_df_noise[i]) {
00888 for (j = 0; j < sbr->n_q; j++)
00889 ch_data->noise_facs[i + 1][j] = ch_data->noise_facs[i][j] + delta * (get_vlc2(gb, t_huff, 9, 2) - t_lav);
00890 } else {
00891 ch_data->noise_facs[i + 1][0] = delta * get_bits(gb, 5);
00892 for (j = 1; j < sbr->n_q; j++)
00893 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);
00894 }
00895 }
00896
00897
00898 memcpy(ch_data->noise_facs[0], ch_data->noise_facs[ch_data->bs_num_noise],
00899 sizeof(ch_data->noise_facs[0]));
00900 }
00901
00902 static void read_sbr_extension(AACContext *ac, SpectralBandReplication *sbr,
00903 GetBitContext *gb,
00904 int bs_extension_id, int *num_bits_left)
00905 {
00906 switch (bs_extension_id) {
00907 case EXTENSION_ID_PS:
00908 if (!ac->m4ac.ps) {
00909 av_log(ac->avctx, AV_LOG_ERROR, "Parametric Stereo signaled to be not-present but was found in the bitstream.\n");
00910 skip_bits_long(gb, *num_bits_left);
00911 *num_bits_left = 0;
00912 } else {
00913 #if 1
00914 *num_bits_left -= ff_ps_read_data(ac->avctx, gb, &sbr->ps, *num_bits_left);
00915 #else
00916 av_log_missing_feature(ac->avctx, "Parametric Stereo is", 0);
00917 skip_bits_long(gb, *num_bits_left);
00918 *num_bits_left = 0;
00919 #endif
00920 }
00921 break;
00922 default:
00923 av_log_missing_feature(ac->avctx, "Reserved SBR extensions are", 1);
00924 skip_bits_long(gb, *num_bits_left);
00925 *num_bits_left = 0;
00926 break;
00927 }
00928 }
00929
00930 static int read_sbr_single_channel_element(AACContext *ac,
00931 SpectralBandReplication *sbr,
00932 GetBitContext *gb)
00933 {
00934 if (get_bits1(gb))
00935 skip_bits(gb, 4);
00936
00937 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
00938 return -1;
00939 read_sbr_dtdf(sbr, gb, &sbr->data[0]);
00940 read_sbr_invf(sbr, gb, &sbr->data[0]);
00941 read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
00942 read_sbr_noise(sbr, gb, &sbr->data[0], 0);
00943
00944 if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
00945 get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
00946
00947 return 0;
00948 }
00949
00950 static int read_sbr_channel_pair_element(AACContext *ac,
00951 SpectralBandReplication *sbr,
00952 GetBitContext *gb)
00953 {
00954 if (get_bits1(gb))
00955 skip_bits(gb, 8);
00956
00957 if ((sbr->bs_coupling = get_bits1(gb))) {
00958 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]))
00959 return -1;
00960 copy_sbr_grid(&sbr->data[1], &sbr->data[0]);
00961 read_sbr_dtdf(sbr, gb, &sbr->data[0]);
00962 read_sbr_dtdf(sbr, gb, &sbr->data[1]);
00963 read_sbr_invf(sbr, gb, &sbr->data[0]);
00964 memcpy(sbr->data[1].bs_invf_mode[1], sbr->data[1].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
00965 memcpy(sbr->data[1].bs_invf_mode[0], sbr->data[0].bs_invf_mode[0], sizeof(sbr->data[1].bs_invf_mode[0]));
00966 read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
00967 read_sbr_noise(sbr, gb, &sbr->data[0], 0);
00968 read_sbr_envelope(sbr, gb, &sbr->data[1], 1);
00969 read_sbr_noise(sbr, gb, &sbr->data[1], 1);
00970 } else {
00971 if (read_sbr_grid(ac, sbr, gb, &sbr->data[0]) ||
00972 read_sbr_grid(ac, sbr, gb, &sbr->data[1]))
00973 return -1;
00974 read_sbr_dtdf(sbr, gb, &sbr->data[0]);
00975 read_sbr_dtdf(sbr, gb, &sbr->data[1]);
00976 read_sbr_invf(sbr, gb, &sbr->data[0]);
00977 read_sbr_invf(sbr, gb, &sbr->data[1]);
00978 read_sbr_envelope(sbr, gb, &sbr->data[0], 0);
00979 read_sbr_envelope(sbr, gb, &sbr->data[1], 1);
00980 read_sbr_noise(sbr, gb, &sbr->data[0], 0);
00981 read_sbr_noise(sbr, gb, &sbr->data[1], 1);
00982 }
00983
00984 if ((sbr->data[0].bs_add_harmonic_flag = get_bits1(gb)))
00985 get_bits1_vector(gb, sbr->data[0].bs_add_harmonic, sbr->n[1]);
00986 if ((sbr->data[1].bs_add_harmonic_flag = get_bits1(gb)))
00987 get_bits1_vector(gb, sbr->data[1].bs_add_harmonic, sbr->n[1]);
00988
00989 return 0;
00990 }
00991
00992 static unsigned int read_sbr_data(AACContext *ac, SpectralBandReplication *sbr,
00993 GetBitContext *gb, int id_aac)
00994 {
00995 unsigned int cnt = get_bits_count(gb);
00996
00997 if (id_aac == TYPE_SCE || id_aac == TYPE_CCE) {
00998 if (read_sbr_single_channel_element(ac, sbr, gb)) {
00999 sbr->start = 0;
01000 return get_bits_count(gb) - cnt;
01001 }
01002 } else if (id_aac == TYPE_CPE) {
01003 if (read_sbr_channel_pair_element(ac, sbr, gb)) {
01004 sbr->start = 0;
01005 return get_bits_count(gb) - cnt;
01006 }
01007 } else {
01008 av_log(ac->avctx, AV_LOG_ERROR,
01009 "Invalid bitstream - cannot apply SBR to element type %d\n", id_aac);
01010 sbr->start = 0;
01011 return get_bits_count(gb) - cnt;
01012 }
01013 if (get_bits1(gb)) {
01014 int num_bits_left = get_bits(gb, 4);
01015 if (num_bits_left == 15)
01016 num_bits_left += get_bits(gb, 8);
01017
01018 num_bits_left <<= 3;
01019 while (num_bits_left > 7) {
01020 num_bits_left -= 2;
01021 read_sbr_extension(ac, sbr, gb, get_bits(gb, 2), &num_bits_left);
01022 }
01023 if (num_bits_left < 0) {
01024 av_log(ac->avctx, AV_LOG_ERROR, "SBR Extension over read.\n");
01025 }
01026 if (num_bits_left > 0)
01027 skip_bits(gb, num_bits_left);
01028 }
01029
01030 return get_bits_count(gb) - cnt;
01031 }
01032
01033 static void sbr_reset(AACContext *ac, SpectralBandReplication *sbr)
01034 {
01035 int err;
01036 err = sbr_make_f_master(ac, sbr, &sbr->spectrum_params);
01037 if (err >= 0)
01038 err = sbr_make_f_derived(ac, sbr);
01039 if (err < 0) {
01040 av_log(ac->avctx, AV_LOG_ERROR,
01041 "SBR reset failed. Switching SBR to pure upsampling mode.\n");
01042 sbr->start = 0;
01043 }
01044 }
01045
01054 int ff_decode_sbr_extension(AACContext *ac, SpectralBandReplication *sbr,
01055 GetBitContext *gb_host, int crc, int cnt, int id_aac)
01056 {
01057 unsigned int num_sbr_bits = 0, num_align_bits;
01058 unsigned bytes_read;
01059 GetBitContext gbc = *gb_host, *gb = &gbc;
01060 skip_bits_long(gb_host, cnt*8 - 4);
01061
01062 sbr->reset = 0;
01063
01064 if (!sbr->sample_rate)
01065 sbr->sample_rate = 2 * ac->m4ac.sample_rate;
01066 if (!ac->m4ac.ext_sample_rate)
01067 ac->m4ac.ext_sample_rate = 2 * ac->m4ac.sample_rate;
01068
01069 if (crc) {
01070 skip_bits(gb, 10);
01071 num_sbr_bits += 10;
01072 }
01073
01074
01075 sbr->kx[0] = sbr->kx[1];
01076 sbr->m[0] = sbr->m[1];
01077
01078 num_sbr_bits++;
01079 if (get_bits1(gb))
01080 num_sbr_bits += read_sbr_header(sbr, gb);
01081
01082 if (sbr->reset)
01083 sbr_reset(ac, sbr);
01084
01085 if (sbr->start)
01086 num_sbr_bits += read_sbr_data(ac, sbr, gb, id_aac);
01087
01088 num_align_bits = ((cnt << 3) - 4 - num_sbr_bits) & 7;
01089 bytes_read = ((num_sbr_bits + num_align_bits + 4) >> 3);
01090
01091 if (bytes_read > cnt) {
01092 av_log(ac->avctx, AV_LOG_ERROR,
01093 "Expected to read %d SBR bytes actually read %d.\n", cnt, bytes_read);
01094 }
01095 return cnt;
01096 }
01097
01099 static void sbr_dequant(SpectralBandReplication *sbr, int id_aac)
01100 {
01101 int k, e;
01102 int ch;
01103
01104 if (id_aac == TYPE_CPE && sbr->bs_coupling) {
01105 float alpha = sbr->data[0].bs_amp_res ? 1.0f : 0.5f;
01106 float pan_offset = sbr->data[0].bs_amp_res ? 12.0f : 24.0f;
01107 for (e = 1; e <= sbr->data[0].bs_num_env; e++) {
01108 for (k = 0; k < sbr->n[sbr->data[0].bs_freq_res[e]]; k++) {
01109 float temp1 = exp2f(sbr->data[0].env_facs[e][k] * alpha + 7.0f);
01110 float temp2 = exp2f((pan_offset - sbr->data[1].env_facs[e][k]) * alpha);
01111 float fac = temp1 / (1.0f + temp2);
01112 sbr->data[0].env_facs[e][k] = fac;
01113 sbr->data[1].env_facs[e][k] = fac * temp2;
01114 }
01115 }
01116 for (e = 1; e <= sbr->data[0].bs_num_noise; e++) {
01117 for (k = 0; k < sbr->n_q; k++) {
01118 float temp1 = exp2f(NOISE_FLOOR_OFFSET - sbr->data[0].noise_facs[e][k] + 1);
01119 float temp2 = exp2f(12 - sbr->data[1].noise_facs[e][k]);
01120 float fac = temp1 / (1.0f + temp2);
01121 sbr->data[0].noise_facs[e][k] = fac;
01122 sbr->data[1].noise_facs[e][k] = fac * temp2;
01123 }
01124 }
01125 } else {
01126 for (ch = 0; ch < (id_aac == TYPE_CPE) + 1; ch++) {
01127 float alpha = sbr->data[ch].bs_amp_res ? 1.0f : 0.5f;
01128 for (e = 1; e <= sbr->data[ch].bs_num_env; e++)
01129 for (k = 0; k < sbr->n[sbr->data[ch].bs_freq_res[e]]; k++)
01130 sbr->data[ch].env_facs[e][k] =
01131 exp2f(alpha * sbr->data[ch].env_facs[e][k] + 6.0f);
01132 for (e = 1; e <= sbr->data[ch].bs_num_noise; e++)
01133 for (k = 0; k < sbr->n_q; k++)
01134 sbr->data[ch].noise_facs[e][k] =
01135 exp2f(NOISE_FLOOR_OFFSET - sbr->data[ch].noise_facs[e][k]);
01136 }
01137 }
01138 }
01139
01146 static void sbr_qmf_analysis(DSPContext *dsp, FFTContext *mdct, const float *in, float *x,
01147 float z[320], float W[2][32][32][2])
01148 {
01149 int i, k;
01150 memcpy(W[0], W[1], sizeof(W[0]));
01151 memcpy(x , x+1024, (320-32)*sizeof(x[0]));
01152 memcpy(x+288, in, 1024*sizeof(x[0]));
01153 for (i = 0; i < 32; i++) {
01154
01155 dsp->vector_fmul_reverse(z, sbr_qmf_window_ds, x, 320);
01156 for (k = 0; k < 64; k++) {
01157 float f = z[k] + z[k + 64] + z[k + 128] + z[k + 192] + z[k + 256];
01158 z[k] = f;
01159 }
01160
01161 z[64] = z[0];
01162 for (k = 1; k < 32; k++) {
01163 z[64+2*k-1] = z[ k];
01164 z[64+2*k ] = -z[64-k];
01165 }
01166 z[64+63] = z[32];
01167
01168 mdct->imdct_half(mdct, z, z+64);
01169 for (k = 0; k < 32; k++) {
01170 W[1][i][k][0] = -z[63-k];
01171 W[1][i][k][1] = z[k];
01172 }
01173 x += 32;
01174 }
01175 }
01176
01181 static void sbr_qmf_synthesis(DSPContext *dsp, FFTContext *mdct,
01182 float *out, float X[2][38][64],
01183 float mdct_buf[2][64],
01184 float *v0, int *v_off, const unsigned int div)
01185 {
01186 int i, n;
01187 const float *sbr_qmf_window = div ? sbr_qmf_window_ds : sbr_qmf_window_us;
01188 float *v;
01189 for (i = 0; i < 32; i++) {
01190 if (*v_off < 128 >> div) {
01191 int saved_samples = (1280 - 128) >> div;
01192 memcpy(&v0[SBR_SYNTHESIS_BUF_SIZE - saved_samples], v0, saved_samples * sizeof(float));
01193 *v_off = SBR_SYNTHESIS_BUF_SIZE - saved_samples - (128 >> div);
01194 } else {
01195 *v_off -= 128 >> div;
01196 }
01197 v = v0 + *v_off;
01198 if (div) {
01199 for (n = 0; n < 32; n++) {
01200 X[0][i][ n] = -X[0][i][n];
01201 X[0][i][32+n] = X[1][i][31-n];
01202 }
01203 mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
01204 for (n = 0; n < 32; n++) {
01205 v[ n] = mdct_buf[0][63 - 2*n];
01206 v[63 - n] = -mdct_buf[0][62 - 2*n];
01207 }
01208 } else {
01209 for (n = 1; n < 64; n+=2) {
01210 X[1][i][n] = -X[1][i][n];
01211 }
01212 mdct->imdct_half(mdct, mdct_buf[0], X[0][i]);
01213 mdct->imdct_half(mdct, mdct_buf[1], X[1][i]);
01214 for (n = 0; n < 64; n++) {
01215 v[ n] = -mdct_buf[0][63 - n] + mdct_buf[1][ n ];
01216 v[127 - n] = mdct_buf[0][63 - n] + mdct_buf[1][ n ];
01217 }
01218 }
01219 dsp->vector_fmul_add(out, v , sbr_qmf_window , zero64, 64 >> div);
01220 dsp->vector_fmul_add(out, v + ( 192 >> div), sbr_qmf_window + ( 64 >> div), out , 64 >> div);
01221 dsp->vector_fmul_add(out, v + ( 256 >> div), sbr_qmf_window + (128 >> div), out , 64 >> div);
01222 dsp->vector_fmul_add(out, v + ( 448 >> div), sbr_qmf_window + (192 >> div), out , 64 >> div);
01223 dsp->vector_fmul_add(out, v + ( 512 >> div), sbr_qmf_window + (256 >> div), out , 64 >> div);
01224 dsp->vector_fmul_add(out, v + ( 704 >> div), sbr_qmf_window + (320 >> div), out , 64 >> div);
01225 dsp->vector_fmul_add(out, v + ( 768 >> div), sbr_qmf_window + (384 >> div), out , 64 >> div);
01226 dsp->vector_fmul_add(out, v + ( 960 >> div), sbr_qmf_window + (448 >> div), out , 64 >> div);
01227 dsp->vector_fmul_add(out, v + (1024 >> div), sbr_qmf_window + (512 >> div), out , 64 >> div);
01228 dsp->vector_fmul_add(out, v + (1216 >> div), sbr_qmf_window + (576 >> div), out , 64 >> div);
01229 out += 64 >> div;
01230 }
01231 }
01232
01233 static void autocorrelate(const float x[40][2], float phi[3][2][2], int lag)
01234 {
01235 int i;
01236 float real_sum = 0.0f;
01237 float imag_sum = 0.0f;
01238 if (lag) {
01239 for (i = 1; i < 38; i++) {
01240 real_sum += x[i][0] * x[i+lag][0] + x[i][1] * x[i+lag][1];
01241 imag_sum += x[i][0] * x[i+lag][1] - x[i][1] * x[i+lag][0];
01242 }
01243 phi[2-lag][1][0] = real_sum + x[ 0][0] * x[lag][0] + x[ 0][1] * x[lag][1];
01244 phi[2-lag][1][1] = imag_sum + x[ 0][0] * x[lag][1] - x[ 0][1] * x[lag][0];
01245 if (lag == 1) {
01246 phi[0][0][0] = real_sum + x[38][0] * x[39][0] + x[38][1] * x[39][1];
01247 phi[0][0][1] = imag_sum + x[38][0] * x[39][1] - x[38][1] * x[39][0];
01248 }
01249 } else {
01250 for (i = 1; i < 38; i++) {
01251 real_sum += x[i][0] * x[i][0] + x[i][1] * x[i][1];
01252 }
01253 phi[2][1][0] = real_sum + x[ 0][0] * x[ 0][0] + x[ 0][1] * x[ 0][1];
01254 phi[1][0][0] = real_sum + x[38][0] * x[38][0] + x[38][1] * x[38][1];
01255 }
01256 }
01257
01262 static void sbr_hf_inverse_filter(float (*alpha0)[2], float (*alpha1)[2],
01263 const float X_low[32][40][2], int k0)
01264 {
01265 int k;
01266 for (k = 0; k < k0; k++) {
01267 float phi[3][2][2], dk;
01268
01269 autocorrelate(X_low[k], phi, 0);
01270 autocorrelate(X_low[k], phi, 1);
01271 autocorrelate(X_low[k], phi, 2);
01272
01273 dk = phi[2][1][0] * phi[1][0][0] -
01274 (phi[1][1][0] * phi[1][1][0] + phi[1][1][1] * phi[1][1][1]) / 1.000001f;
01275
01276 if (!dk) {
01277 alpha1[k][0] = 0;
01278 alpha1[k][1] = 0;
01279 } else {
01280 float temp_real, temp_im;
01281 temp_real = phi[0][0][0] * phi[1][1][0] -
01282 phi[0][0][1] * phi[1][1][1] -
01283 phi[0][1][0] * phi[1][0][0];
01284 temp_im = phi[0][0][0] * phi[1][1][1] +
01285 phi[0][0][1] * phi[1][1][0] -
01286 phi[0][1][1] * phi[1][0][0];
01287
01288 alpha1[k][0] = temp_real / dk;
01289 alpha1[k][1] = temp_im / dk;
01290 }
01291
01292 if (!phi[1][0][0]) {
01293 alpha0[k][0] = 0;
01294 alpha0[k][1] = 0;
01295 } else {
01296 float temp_real, temp_im;
01297 temp_real = phi[0][0][0] + alpha1[k][0] * phi[1][1][0] +
01298 alpha1[k][1] * phi[1][1][1];
01299 temp_im = phi[0][0][1] + alpha1[k][1] * phi[1][1][0] -
01300 alpha1[k][0] * phi[1][1][1];
01301
01302 alpha0[k][0] = -temp_real / phi[1][0][0];
01303 alpha0[k][1] = -temp_im / phi[1][0][0];
01304 }
01305
01306 if (alpha1[k][0] * alpha1[k][0] + alpha1[k][1] * alpha1[k][1] >= 16.0f ||
01307 alpha0[k][0] * alpha0[k][0] + alpha0[k][1] * alpha0[k][1] >= 16.0f) {
01308 alpha1[k][0] = 0;
01309 alpha1[k][1] = 0;
01310 alpha0[k][0] = 0;
01311 alpha0[k][1] = 0;
01312 }
01313 }
01314 }
01315
01317 static void sbr_chirp(SpectralBandReplication *sbr, SBRData *ch_data)
01318 {
01319 int i;
01320 float new_bw;
01321 static const float bw_tab[] = { 0.0f, 0.75f, 0.9f, 0.98f };
01322
01323 for (i = 0; i < sbr->n_q; i++) {
01324 if (ch_data->bs_invf_mode[0][i] + ch_data->bs_invf_mode[1][i] == 1) {
01325 new_bw = 0.6f;
01326 } else
01327 new_bw = bw_tab[ch_data->bs_invf_mode[0][i]];
01328
01329 if (new_bw < ch_data->bw_array[i]) {
01330 new_bw = 0.75f * new_bw + 0.25f * ch_data->bw_array[i];
01331 } else
01332 new_bw = 0.90625f * new_bw + 0.09375f * ch_data->bw_array[i];
01333 ch_data->bw_array[i] = new_bw < 0.015625f ? 0.0f : new_bw;
01334 }
01335 }
01336
01338 static int sbr_lf_gen(AACContext *ac, SpectralBandReplication *sbr,
01339 float X_low[32][40][2], const float W[2][32][32][2])
01340 {
01341 int i, k;
01342 const int t_HFGen = 8;
01343 const int i_f = 32;
01344 memset(X_low, 0, 32*sizeof(*X_low));
01345 for (k = 0; k < sbr->kx[1]; k++) {
01346 for (i = t_HFGen; i < i_f + t_HFGen; i++) {
01347 X_low[k][i][0] = W[1][i - t_HFGen][k][0];
01348 X_low[k][i][1] = W[1][i - t_HFGen][k][1];
01349 }
01350 }
01351 for (k = 0; k < sbr->kx[0]; k++) {
01352 for (i = 0; i < t_HFGen; i++) {
01353 X_low[k][i][0] = W[0][i + i_f - t_HFGen][k][0];
01354 X_low[k][i][1] = W[0][i + i_f - t_HFGen][k][1];
01355 }
01356 }
01357 return 0;
01358 }
01359
01361 static int sbr_hf_gen(AACContext *ac, SpectralBandReplication *sbr,
01362 float X_high[64][40][2], const float X_low[32][40][2],
01363 const float (*alpha0)[2], const float (*alpha1)[2],
01364 const float bw_array[5], const uint8_t *t_env,
01365 int bs_num_env)
01366 {
01367 int i, j, x;
01368 int g = 0;
01369 int k = sbr->kx[1];
01370 for (j = 0; j < sbr->num_patches; j++) {
01371 for (x = 0; x < sbr->patch_num_subbands[j]; x++, k++) {
01372 float alpha[4];
01373 const int p = sbr->patch_start_subband[j] + x;
01374 while (g <= sbr->n_q && k >= sbr->f_tablenoise[g])
01375 g++;
01376 g--;
01377
01378 if (g < 0) {
01379 av_log(ac->avctx, AV_LOG_ERROR,
01380 "ERROR : no subband found for frequency %d\n", k);
01381 return -1;
01382 }
01383
01384 alpha[0] = alpha1[p][0] * bw_array[g] * bw_array[g];
01385 alpha[1] = alpha1[p][1] * bw_array[g] * bw_array[g];
01386 alpha[2] = alpha0[p][0] * bw_array[g];
01387 alpha[3] = alpha0[p][1] * bw_array[g];
01388
01389 for (i = 2 * t_env[0]; i < 2 * t_env[bs_num_env]; i++) {
01390 const int idx = i + ENVELOPE_ADJUSTMENT_OFFSET;
01391 X_high[k][idx][0] =
01392 X_low[p][idx - 2][0] * alpha[0] -
01393 X_low[p][idx - 2][1] * alpha[1] +
01394 X_low[p][idx - 1][0] * alpha[2] -
01395 X_low[p][idx - 1][1] * alpha[3] +
01396 X_low[p][idx][0];
01397 X_high[k][idx][1] =
01398 X_low[p][idx - 2][1] * alpha[0] +
01399 X_low[p][idx - 2][0] * alpha[1] +
01400 X_low[p][idx - 1][1] * alpha[2] +
01401 X_low[p][idx - 1][0] * alpha[3] +
01402 X_low[p][idx][1];
01403 }
01404 }
01405 }
01406 if (k < sbr->m[1] + sbr->kx[1])
01407 memset(X_high + k, 0, (sbr->m[1] + sbr->kx[1] - k) * sizeof(*X_high));
01408
01409 return 0;
01410 }
01411
01413 static int sbr_x_gen(SpectralBandReplication *sbr, float X[2][38][64],
01414 const float X_low[32][40][2], const float Y[2][38][64][2],
01415 int ch)
01416 {
01417 int k, i;
01418 const int i_f = 32;
01419 const int i_Temp = FFMAX(2*sbr->data[ch].t_env_num_env_old - i_f, 0);
01420 memset(X, 0, 2*sizeof(*X));
01421 for (k = 0; k < sbr->kx[0]; k++) {
01422 for (i = 0; i < i_Temp; i++) {
01423 X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
01424 X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
01425 }
01426 }
01427 for (; k < sbr->kx[0] + sbr->m[0]; k++) {
01428 for (i = 0; i < i_Temp; i++) {
01429 X[0][i][k] = Y[0][i + i_f][k][0];
01430 X[1][i][k] = Y[0][i + i_f][k][1];
01431 }
01432 }
01433
01434 for (k = 0; k < sbr->kx[1]; k++) {
01435 for (i = i_Temp; i < 38; i++) {
01436 X[0][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][0];
01437 X[1][i][k] = X_low[k][i + ENVELOPE_ADJUSTMENT_OFFSET][1];
01438 }
01439 }
01440 for (; k < sbr->kx[1] + sbr->m[1]; k++) {
01441 for (i = i_Temp; i < i_f; i++) {
01442 X[0][i][k] = Y[1][i][k][0];
01443 X[1][i][k] = Y[1][i][k][1];
01444 }
01445 }
01446 return 0;
01447 }
01448
01452 static void sbr_mapping(AACContext *ac, SpectralBandReplication *sbr,
01453 SBRData *ch_data, int e_a[2])
01454 {
01455 int e, i, m;
01456
01457 memset(ch_data->s_indexmapped[1], 0, 7*sizeof(ch_data->s_indexmapped[1]));
01458 for (e = 0; e < ch_data->bs_num_env; e++) {
01459 const unsigned int ilim = sbr->n[ch_data->bs_freq_res[e + 1]];
01460 uint16_t *table = ch_data->bs_freq_res[e + 1] ? sbr->f_tablehigh : sbr->f_tablelow;
01461 int k;
01462
01463 av_assert0(sbr->kx[1] <= table[0]);
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.f, 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.584893192f, 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);
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 if (nch == 2)
01767 sbr_qmf_synthesis(&ac->dsp, &sbr->mdct, R, sbr->X[1], sbr->qmf_filter_scratch,
01768 sbr->data[1].synthesis_filterbank_samples,
01769 &sbr->data[1].synthesis_filterbank_samples_offset,
01770 downsampled);
01771 }