void drc_decode(drc_info *drc, real_t *spec) { uint16_t i, bd, top; #ifdef FIXED_POINT int32_t exp, frac; #else real_t factor, exp; #endif uint16_t bottom = 0; if (drc->num_bands == 1) drc->band_top[0] = 1024/4 - 1; for (bd = 0; bd < drc->num_bands; bd++) { top = 4 * (drc->band_top[bd] + 1); #ifndef FIXED_POINT /* Decode DRC gain factor */ if (drc->dyn_rng_sgn[bd]) /* compress */ exp = -drc->ctrl1 * (drc->dyn_rng_ctl[bd] - (DRC_REF_LEVEL - drc->prog_ref_level))/REAL_CONST(24.0); else /* boost */ exp = drc->ctrl2 * (drc->dyn_rng_ctl[bd] - (DRC_REF_LEVEL - drc->prog_ref_level))/REAL_CONST(24.0); factor = (real_t)pow(2.0, exp); /* Apply gain factor */ for (i = bottom; i < top; i++) spec[i] *= factor; #else /* Decode DRC gain factor */ if (drc->dyn_rng_sgn[bd]) /* compress */ { exp = -1 * (drc->dyn_rng_ctl[bd] - (DRC_REF_LEVEL - drc->prog_ref_level))/ 24; frac = -1 * (drc->dyn_rng_ctl[bd] - (DRC_REF_LEVEL - drc->prog_ref_level)) % 24; } else { /* boost */ exp = (drc->dyn_rng_ctl[bd] - (DRC_REF_LEVEL - drc->prog_ref_level))/ 24; frac = (drc->dyn_rng_ctl[bd] - (DRC_REF_LEVEL - drc->prog_ref_level)) % 24; } /* Apply gain factor */ if (exp < 0) { for (i = bottom; i < top; i++) { spec[i] >>= -exp; if (frac) spec[i] = MUL_R(spec[i],drc_pow2_table[frac+23]); } } else { for (i = bottom; i < top; i++) { spec[i] <<= exp; if (frac) spec[i] = MUL_R(spec[i],drc_pow2_table[frac+23]); } } #endif bottom = top; }
static void auto_correlation(sbr_info *sbr, acorr_coef *ac, qmf_t buffer[MAX_NTSRHFG][32], uint8_t bd, uint8_t len) { real_t r01 = 0, r02 = 0, r11 = 0; int8_t j; uint8_t offset = sbr->tHFAdj; const real_t rel = 1 / (1 + 1e-6f); for (j = offset; j < len + offset; j++) { r01 += QMF_RE(buffer[j][bd]) * QMF_RE(buffer[j-1][bd]); r02 += QMF_RE(buffer[j][bd]) * QMF_RE(buffer[j-2][bd]); r11 += QMF_RE(buffer[j-1][bd]) * QMF_RE(buffer[j-1][bd]); } RE(ac->r12) = r01 - QMF_RE(buffer[len+offset-1][bd]) * QMF_RE(buffer[len+offset-2][bd]) + QMF_RE(buffer[offset-1][bd]) * QMF_RE(buffer[offset-2][bd]); RE(ac->r22) = r11 - QMF_RE(buffer[len+offset-2][bd]) * QMF_RE(buffer[len+offset-2][bd]) + QMF_RE(buffer[offset-2][bd]) * QMF_RE(buffer[offset-2][bd]); RE(ac->r01) = r01; RE(ac->r02) = r02; RE(ac->r11) = r11; ac->det = MUL_R(RE(ac->r11), RE(ac->r22)) - MUL_C(MUL_R(RE(ac->r12), RE(ac->r12)), rel); }
void faad_mdct(mdct_info *mdct, real_t *X_in, real_t *X_out) { uint16_t k; complex_t x; ALIGN complex_t Z1[512]; complex_t *sincos = mdct->sincos; uint16_t N = mdct->N; uint16_t N2 = N >> 1; uint16_t N4 = N >> 2; uint16_t N8 = N >> 3; #ifndef FIXED_POINT real_t scale = REAL_CONST(N); #else real_t scale = REAL_CONST(4.0/N); #endif /* pre-FFT complex multiplication */ for (k = 0; k < N8; k++) { uint16_t n = k << 1; RE(x) = X_in[N - N4 - 1 - n] + X_in[N - N4 + n]; IM(x) = X_in[ N4 + n] - X_in[ N4 - 1 - n]; ComplexMult(&RE(Z1[k]), &IM(Z1[k]), RE(x), IM(x), RE(sincos[k]), IM(sincos[k])); RE(Z1[k]) = MUL_R(RE(Z1[k]), scale); IM(Z1[k]) = MUL_R(IM(Z1[k]), scale); RE(x) = X_in[N2 - 1 - n] - X_in[ n]; IM(x) = X_in[N2 + n] + X_in[N - 1 - n]; ComplexMult(&RE(Z1[k + N8]), &IM(Z1[k + N8]), RE(x), IM(x), RE(sincos[k + N8]), IM(sincos[k + N8])); RE(Z1[k + N8]) = MUL_R(RE(Z1[k + N8]), scale); IM(Z1[k + N8]) = MUL_R(IM(Z1[k + N8]), scale); } /* complex FFT, any non-scaling FFT can be used here */ cfftf(mdct->cfft, Z1); /* post-FFT complex multiplication */ for (k = 0; k < N4; k++) { uint16_t n = k << 1; ComplexMult(&RE(x), &IM(x), RE(Z1[k]), IM(Z1[k]), RE(sincos[k]), IM(sincos[k])); X_out[ n] = -RE(x); X_out[N2 - 1 - n] = IM(x); X_out[N2 + n] = -IM(x); X_out[N - 1 - n] = RE(x); } }
/* * Parameter: * coderInfo(IO)(O:coderInfo->scale_factor) * xr(unused) * xr_pow(IO) * xi(O) * xmin(I) */ static int32_t FixNoise(CoderInfo *coderInfo, coef_t *xr_pow, int32_t *xi, real_32_t *xmin) { register int32_t i, sb; int32_t start, end; static real_32_t log_ifqstep = REAL_CONST(7.6943735514); // 1.0 / log(ifqstep) static real_32_t realconst1 = REAL_CONST(0.6931471806); //log2 static real_32_t realconst2 = REAL_CONST(0.5); for (sb = 0; sb < coderInfo->nr_of_sfb; sb++) { eng_t eng = 0; frac_t maxfixstep; start = coderInfo->sfb_offset[sb]; end = coderInfo->sfb_offset[sb+1]; for ( i=start; i< end; i++) eng += (int64_t)(COEF2INT(xr_pow[i]))*COEF2INT(xr_pow[i]); if ( (eng == 0) || (xmin[sb]==0) ) maxfixstep = FRAC_ICONST(1); else { maxfixstep = faac_sqrt(eng/(end-start)* MUL_R(xmin[sb],faac_sqrt(xmin[sb])) ); if ( maxfixstep == 0 ) maxfixstep = FRAC_ICONST(1); else maxfixstep = ((real_t)1<<(FRAC_BITS+REAL_BITS))/maxfixstep; } #ifdef DUMP_MAXFIXSTEP printf("sb = %d, maxfixstep = %.8f\n",sb, FRAC2FLOAT(maxfixstep)); #endif for (i = start; i < end; i++) { #ifdef DUMP_MAXFIXSTEP printf("xr_pow[%d] = %.8f\t",i,COEF2FLOAT(xr_pow[i])); #endif xr_pow[i] = MUL_F(xr_pow[i],maxfixstep); #ifdef DUMP_MAXFIXSTEP printf("xr_pow[%d]*fix = %.8f\n",i,COEF2FLOAT(xr_pow[i])); #endif } QuantizeBand(xr_pow, xi, start, end); coderInfo->scale_factor[sb] = (int32_t)REAL2INT(MUL_R(faac_log(maxfixstep)-FRAC2REAL_BIT*realconst1, log_ifqstep) - realconst2)+1; #ifdef DUMP_MAXFIXSTEP printf("scale_factor = %d\n", coderInfo->scale_factor[sb]); #endif } #ifdef DUMP_MAXFIXSTEP exit(1); #endif return 0; }
void is_decode(ic_stream *ics, ic_stream *icsr, real_t *l_spec, real_t *r_spec, uint16_t frame_len) { uint8_t g, sfb, b; uint16_t i; #ifndef FIXED_POINT real_t scale; #else int32_t exp, frac; #endif uint16_t nshort = frame_len/8; uint8_t group = 0; for (g = 0; g < icsr->num_window_groups; g++) { /* Do intensity stereo decoding */ for (b = 0; b < icsr->window_group_length[g]; b++) { for (sfb = 0; sfb < icsr->max_sfb; sfb++) { if (is_intensity(icsr, g, sfb)) { /* For scalefactor bands coded in intensity stereo the corresponding predictors in the right channel are switched to "off". */ ics->pred.prediction_used[sfb] = 0; icsr->pred.prediction_used[sfb] = 0; #ifndef FIXED_POINT scale = (real_t)pow(0.5, (0.25*icsr->scale_factors[g][sfb])); #else exp = icsr->scale_factors[g][sfb] >> 2; frac = icsr->scale_factors[g][sfb] & 3; #endif /* Scale from left to right channel, do not touch left channel */ for (i = icsr->swb_offset[sfb]; i < icsr->swb_offset[sfb+1]; i++) { #ifndef FIXED_POINT r_spec[(group*nshort)+i] = MUL_R(l_spec[(group*nshort)+i], scale); #else if (exp < 0) r_spec[(group*nshort)+i] = l_spec[(group*nshort)+i] << -exp; else r_spec[(group*nshort)+i] = l_spec[(group*nshort)+i] >> exp; r_spec[(group*nshort)+i] = MUL_C(r_spec[(group*nshort)+i], pow05_table[frac + 3]); #endif if (is_intensity(icsr, g, sfb) != invert_intensity(ics, g, sfb)) r_spec[(group*nshort)+i] = -r_spec[(group*nshort)+i]; } } } group++; } } }
static void calc_aliasing_degree(sbr_info *sbr, real_t *rxx, real_t *deg) { uint8_t k; rxx[0] = REAL_CONST(0.0); deg[1] = REAL_CONST(0.0); for (k = 2; k < sbr->k0; k++) { deg[k] = 0.0; if ((k % 2 == 0) && (rxx[k] < REAL_CONST(0.0))) { if (rxx[k-1] < 0.0) { deg[k] = REAL_CONST(1.0); if (rxx[k-2] > REAL_CONST(0.0)) { deg[k-1] = REAL_CONST(1.0) - MUL_R(rxx[k-1], rxx[k-1]); } } else if (rxx[k-2] > REAL_CONST(0.0)) { deg[k] = REAL_CONST(1.0) - MUL_R(rxx[k-1], rxx[k-1]); } } if ((k % 2 == 1) && (rxx[k] > REAL_CONST(0.0))) { if (rxx[k-1] > REAL_CONST(0.0)) { deg[k] = REAL_CONST(1.0); if (rxx[k-2] < REAL_CONST(0.0)) { deg[k-1] = REAL_CONST(1.0) - MUL_R(rxx[k-1], rxx[k-1]); } } else if (rxx[k-2] < REAL_CONST(0.0)) { deg[k] = REAL_CONST(1.0) - MUL_R(rxx[k-1], rxx[k-1]); } } } }
static INLINE real_t iquant(int16_t q, const real_t *tab, uint8_t *error) { #ifdef FIXED_POINT static const real_t errcorr[] = { REAL_CONST(0), REAL_CONST(1.0/8.0), REAL_CONST(2.0/8.0), REAL_CONST(3.0/8.0), REAL_CONST(4.0/8.0), REAL_CONST(5.0/8.0), REAL_CONST(6.0/8.0), REAL_CONST(7.0/8.0), REAL_CONST(0) }; real_t x1, x2; int16_t sgn = 1; if (q < 0) { q = -q; sgn = -1; } if (q < IQ_TABLE_SIZE) return sgn * tab[q]; /* linear interpolation */ x1 = tab[q>>3]; x2 = tab[(q>>3) + 1]; return sgn * 16 * (MUL_R(errcorr[q&7],(x2-x1)) + x1); #else if (q < 0) { /* tab contains a value for all possible q [0,8192] */ if (-q < IQ_TABLE_SIZE) return -tab[-q]; *error = 17; return 0; } else { /* tab contains a value for all possible q [0,8192] */ if (q < IQ_TABLE_SIZE) return tab[q]; *error = 17; return 0; } #endif }
static void calc_prediction_coef_lp(sbr_info *sbr, qmf_t Xlow[MAX_NTSRHFG][64], complex_t *alpha_0, complex_t *alpha_1, real_t *rxx) { uint8_t k; real_t tmp, mul; acorr_coef ac; for (k = 1; k < sbr->f_master[0]; k++) { auto_correlation(sbr, &ac, Xlow, k, sbr->numTimeSlotsRate + 6); if (ac.det == 0) { RE(alpha_0[k]) = 0; RE(alpha_1[k]) = 0; } else { mul = DIV_R(REAL_CONST(1.0), ac.det); tmp = MUL_R(RE(ac.r01), RE(ac.r22)) - MUL_R(RE(ac.r12), RE(ac.r02)); RE(alpha_0[k]) = -MUL_R(tmp, mul); tmp = MUL_R(RE(ac.r01), RE(ac.r12)) - MUL_R(RE(ac.r02), RE(ac.r11)); RE(alpha_1[k]) = MUL_R(tmp, mul); } if ((RE(alpha_0[k]) >= REAL_CONST(4)) || (RE(alpha_1[k]) >= REAL_CONST(4))) { RE(alpha_0[k]) = REAL_CONST(0); RE(alpha_1[k]) = REAL_CONST(0); } /* reflection coefficient */ if (RE(ac.r11) == 0) { rxx[k] = COEF_CONST(0.0); } else { rxx[k] = DIV_C(RE(ac.r01), RE(ac.r11)); rxx[k] = -rxx[k]; if (rxx[k] > COEF_CONST( 1.0)) rxx[k] = COEF_CONST(1.0); if (rxx[k] < COEF_CONST(-1.0)) rxx[k] = COEF_CONST(-1.0); } } }
void faad_mdct(mdct_info *mdct, real_t *X_in, real_t *X_out) { uint16_t k; complex_t x; ALIGN complex_t Z1[512]; complex_t *sincos = mdct->sincos; uint16_t N = mdct->N; uint16_t N2 = N >> 1; uint16_t N4 = N >> 2; uint16_t N8 = N >> 3; #ifndef FIXED_POINT real_t scale = REAL_CONST(N); #else real_t scale = REAL_CONST(4.0/N); #endif #ifdef ALLOW_SMALL_FRAMELENGTH #ifdef FIXED_POINT /* detect non-power of 2 */ if (N & (N-1)) { /* adjust scale for non-power of 2 MDCT */ /* *= sqrt(2048/1920) */ scale = MUL_C(scale, COEF_CONST(1.0327955589886444)); } #endif #endif /* pre-FFT complex multiplication */ for (k = 0; k < N8; k++) { uint16_t n = k << 1; RE(x) = X_in[N - N4 - 1 - n] + X_in[N - N4 + n]; IM(x) = X_in[ N4 + n] - X_in[ N4 - 1 - n]; ComplexMult(&RE(Z1[k]), &IM(Z1[k]), RE(x), IM(x), RE(sincos[k]), IM(sincos[k])); RE(Z1[k]) = MUL_R(RE(Z1[k]), scale); IM(Z1[k]) = MUL_R(IM(Z1[k]), scale); RE(x) = X_in[N2 - 1 - n] - X_in[ n]; IM(x) = X_in[N2 + n] + X_in[N - 1 - n]; ComplexMult(&RE(Z1[k + N8]), &IM(Z1[k + N8]), RE(x), IM(x), RE(sincos[k + N8]), IM(sincos[k + N8])); RE(Z1[k + N8]) = MUL_R(RE(Z1[k + N8]), scale); IM(Z1[k + N8]) = MUL_R(IM(Z1[k + N8]), scale); } /* complex FFT, any non-scaling FFT can be used here */ cfftf(mdct->cfft, Z1); /* post-FFT complex multiplication */ for (k = 0; k < N4; k++) { uint16_t n = k << 1; ComplexMult(&RE(x), &IM(x), RE(Z1[k]), IM(Z1[k]), RE(sincos[k]), IM(sincos[k])); X_out[ n] = -RE(x); X_out[N2 - 1 - n] = IM(x); X_out[N2 + n] = -IM(x); X_out[N - 1 - n] = RE(x); } }
static INLINE real_t iquant(int16_t q, const real_t *tab, uint8_t *error) { #ifndef BIG_IQ_TABLE /* For FIXED_POINT the iq_table is prescaled by 3 bits (iq_table[]/8) */ /* BIG_IQ_TABLE allows you to use the full 8192 value table, if this is not * defined a 1026 value table and interpolation will be used */ static const real_t errcorr[] = { REAL_CONST(0), REAL_CONST(1.0/8.0), REAL_CONST(2.0/8.0), REAL_CONST(3.0/8.0), REAL_CONST(4.0/8.0), REAL_CONST(5.0/8.0), REAL_CONST(6.0/8.0), REAL_CONST(7.0/8.0), REAL_CONST(0) }; real_t x1, x2; int16_t sgn = 1; if (q < 0) { q = -q; sgn = -1; } if (q < IQ_TABLE_SIZE) { //#define IQUANT_PRINT #ifdef IQUANT_PRINT //printf("0x%.8X\n", sgn * tab[q]); printf("%d\n", sgn * tab[q]); #endif return sgn * tab[q]; } if (q >= 8192) { *error = 17; return 0; } /* linear interpolation */ x1 = tab[q>>3]; x2 = tab[(q>>3) + 1]; return sgn * 16 * (MUL_R(errcorr[q&7],(x2-x1)) + x1); #else /* #ifndef BIG_IQ_TABLE */ if (q < 0) { /* tab contains a value for all possible q [0,8192] */ if (LIKELY(-q < IQ_TABLE_SIZE)) return -tab[-q]; *error = 17; return 0; } else { /* tab contains a value for all possible q [0,8192] */ if (LIKELY(q < IQ_TABLE_SIZE)) return tab[q]; *error = 17; return 0; } #endif }
void hf_generation(sbr_info *sbr, qmf_t Xlow[MAX_NTSRHFG][64], qmf_t Xhigh[MAX_NTSRHFG][64] #ifdef SBR_LOW_POWER ,real_t *deg #endif ,uint8_t ch) { uint8_t l, i, x; ALIGN complex_t alpha_0[64], alpha_1[64]; #ifdef SBR_LOW_POWER ALIGN real_t rxx[64]; #endif uint8_t offset = sbr->tHFAdj; uint8_t first = sbr->t_E[ch][0]; uint8_t last = sbr->t_E[ch][sbr->L_E[ch]]; calc_chirp_factors(sbr, ch); #ifdef SBR_LOW_POWER memset(deg, 0, 64*sizeof(real_t)); #endif if ((ch == 0) && (sbr->Reset)) patch_construction(sbr); /* calculate the prediction coefficients */ #ifdef SBR_LOW_POWER calc_prediction_coef_lp(sbr, Xlow, alpha_0, alpha_1, rxx); calc_aliasing_degree(sbr, rxx, deg); #endif /* actual HF generation */ for (i = 0; i < sbr->noPatches; i++) { for (x = 0; x < sbr->patchNoSubbands[i]; x++) { real_t a0_r, a0_i, a1_r, a1_i; real_t bw, bw2; uint8_t q, p, k, g; /* find the low and high band for patching */ k = sbr->kx + x; for (q = 0; q < i; q++) { k += sbr->patchNoSubbands[q]; } p = sbr->patchStartSubband[i] + x; #ifdef SBR_LOW_POWER if (x != 0 /*x < sbr->patchNoSubbands[i]-1*/) deg[k] = deg[p]; else deg[k] = 0; #endif g = sbr->table_map_k_to_g[k]; bw = sbr->bwArray[ch][g]; bw2 = MUL_C(bw, bw); /* do the patching */ /* with or without filtering */ if (bw2 > 0) { real_t temp1_r, temp2_r, temp3_r; #ifndef SBR_LOW_POWER real_t temp1_i, temp2_i, temp3_i; calc_prediction_coef(sbr, Xlow, alpha_0, alpha_1, p); #endif a0_r = MUL_C(RE(alpha_0[p]), bw); a1_r = MUL_C(RE(alpha_1[p]), bw2); #ifndef SBR_LOW_POWER a0_i = MUL_C(IM(alpha_0[p]), bw); a1_i = MUL_C(IM(alpha_1[p]), bw2); #endif temp2_r = QMF_RE(Xlow[first - 2 + offset][p]); temp3_r = QMF_RE(Xlow[first - 1 + offset][p]); #ifndef SBR_LOW_POWER temp2_i = QMF_IM(Xlow[first - 2 + offset][p]); temp3_i = QMF_IM(Xlow[first - 1 + offset][p]); #endif for (l = first; l < last; l++) { temp1_r = temp2_r; temp2_r = temp3_r; temp3_r = QMF_RE(Xlow[l + offset][p]); #ifndef SBR_LOW_POWER temp1_i = temp2_i; temp2_i = temp3_i; temp3_i = QMF_IM(Xlow[l + offset][p]); #endif #ifdef SBR_LOW_POWER QMF_RE(Xhigh[l + offset][k]) = temp3_r + (MUL_R(a0_r, temp2_r) + MUL_R(a1_r, temp1_r)); #else QMF_RE(Xhigh[l + offset][k]) = temp3_r + (MUL_R(a0_r, temp2_r) - MUL_R(a0_i, temp2_i) + MUL_R(a1_r, temp1_r) - MUL_R(a1_i, temp1_i)); QMF_IM(Xhigh[l + offset][k]) = temp3_i + (MUL_R(a0_i, temp2_r) + MUL_R(a0_r, temp2_i) + MUL_R(a1_i, temp1_r) + MUL_R(a1_r, temp1_i)); #endif } } else { for (l = first; l < last; l++) { QMF_RE(Xhigh[l + offset][k]) = QMF_RE(Xlow[l + offset][p]); #ifndef SBR_LOW_POWER QMF_IM(Xhigh[l + offset][k]) = QMF_IM(Xlow[l + offset][p]); #endif } } } } if (sbr->Reset) { limiter_frequency_table(sbr); } }
static void calc_prediction_coef(sbr_info *sbr, qmf_t Xlow[MAX_NTSRHFG][64], complex_t *alpha_0, complex_t *alpha_1, uint8_t k) { real_t tmp, mul; acorr_coef ac; auto_correlation(sbr, &ac, Xlow, k, sbr->numTimeSlotsRate + 6); if (ac.det == 0) { RE(alpha_1[k]) = 0; IM(alpha_1[k]) = 0; } else { mul = DIV_R(REAL_CONST(1.0), ac.det); tmp = (MUL_R(RE(ac.r01), RE(ac.r12)) - MUL_R(IM(ac.r01), IM(ac.r12)) - MUL_R(RE(ac.r02), RE(ac.r11))); RE(alpha_1[k]) = MUL_R(tmp, mul); tmp = (MUL_R(IM(ac.r01), RE(ac.r12)) + MUL_R(RE(ac.r01), IM(ac.r12)) - MUL_R(IM(ac.r02), RE(ac.r11))); IM(alpha_1[k]) = MUL_R(tmp, mul); } if (RE(ac.r11) == 0) { RE(alpha_0[k]) = 0; IM(alpha_0[k]) = 0; } else { mul = DIV_R(REAL_CONST(1.0), RE(ac.r11)); tmp = -(RE(ac.r01) + MUL_R(RE(alpha_1[k]), RE(ac.r12)) + MUL_R(IM(alpha_1[k]), IM(ac.r12))); RE(alpha_0[k]) = MUL_R(tmp, mul); tmp = -(IM(ac.r01) + MUL_R(IM(alpha_1[k]), RE(ac.r12)) - MUL_R(RE(alpha_1[k]), IM(ac.r12))); IM(alpha_0[k]) = MUL_R(tmp, mul); } if ((MUL_R(RE(alpha_0[k]),RE(alpha_0[k])) + MUL_R(IM(alpha_0[k]),IM(alpha_0[k])) >= REAL_CONST(16)) || (MUL_R(RE(alpha_1[k]),RE(alpha_1[k])) + MUL_R(IM(alpha_1[k]),IM(alpha_1[k])) >= REAL_CONST(16))) { RE(alpha_0[k]) = 0; IM(alpha_0[k]) = 0; RE(alpha_1[k]) = 0; IM(alpha_1[k]) = 0; } }
/* calculate linear prediction coefficients using the covariance method */ static void calc_prediction_coef(sbr_info *sbr, qmf_t Xlow[MAX_NTSRHFG][32], complex_t *alpha_0, complex_t *alpha_1 #ifdef SBR_LOW_POWER , real_t *rxx #endif ) { uint8_t k; real_t tmp; acorr_coef ac; for (k = 1; k < sbr->f_master[0]; k++) { auto_correlation(sbr, &ac, Xlow, k, sbr->numTimeSlotsRate + 6); #ifdef SBR_LOW_POWER if (ac.det == 0) { RE(alpha_1[k]) = 0; } else { tmp = MUL_R(RE(ac.r01), RE(ac.r12)) - MUL_R(RE(ac.r02), RE(ac.r11)); RE(alpha_1[k]) = SBR_DIV(tmp, ac.det); } if (RE(ac.r11) == 0) { RE(alpha_0[k]) = 0; } else { tmp = RE(ac.r01) + MUL_R(RE(alpha_1[k]), RE(ac.r12)); RE(alpha_0[k]) = -SBR_DIV(tmp, RE(ac.r11)); } if ((RE(alpha_0[k]) >= REAL_CONST(4)) || (RE(alpha_1[k]) >= REAL_CONST(4))) { RE(alpha_0[k]) = REAL_CONST(0); RE(alpha_1[k]) = REAL_CONST(0); } /* reflection coefficient */ if (RE(ac.r11) == 0) { rxx[k] = REAL_CONST(0.0); } else { rxx[k] = -SBR_DIV(RE(ac.r01), RE(ac.r11)); if (rxx[k] > REAL_CONST(1.0)) rxx[k] = REAL_CONST(1.0); if (rxx[k] < REAL_CONST(-1.0)) rxx[k] = REAL_CONST(-1.0); } #else if (ac.det == 0) { RE(alpha_1[k]) = 0; IM(alpha_1[k]) = 0; } else { tmp = REAL_CONST(1.0) / ac.det; RE(alpha_1[k]) = (RE(ac.r01) * RE(ac.r12) - IM(ac.r01) * IM(ac.r12) - RE(ac.r02) * RE(ac.r11)) * tmp; IM(alpha_1[k]) = (IM(ac.r01) * RE(ac.r12) + RE(ac.r01) * IM(ac.r12) - IM(ac.r02) * RE(ac.r11)) * tmp; } if (RE(ac.r11) == 0) { RE(alpha_0[k]) = 0; IM(alpha_0[k]) = 0; } else { tmp = 1.0f / RE(ac.r11); RE(alpha_0[k]) = -(RE(ac.r01) + RE(alpha_1[k]) * RE(ac.r12) + IM(alpha_1[k]) * IM(ac.r12)) * tmp; IM(alpha_0[k]) = -(IM(ac.r01) + IM(alpha_1[k]) * RE(ac.r12) - RE(alpha_1[k]) * IM(ac.r12)) * tmp; } if ((RE(alpha_0[k])*RE(alpha_0[k]) + IM(alpha_0[k])*IM(alpha_0[k]) >= 16) || (RE(alpha_1[k])*RE(alpha_1[k]) + IM(alpha_1[k])*IM(alpha_1[k]) >= 16)) { RE(alpha_0[k]) = 0; IM(alpha_0[k]) = 0; RE(alpha_1[k]) = 0; IM(alpha_1[k]) = 0; } #endif } }
/* * Parameter: * coderInfo(I) * xr(I) * xmin(O) * quality(I) */ static void CalcAllowedDist(CoderInfo *coderInfo, pow_t *xr2, real_32_t *xmin, real_32_t quality) { register int32_t sfb, start, end, l; int32_t last = coderInfo->lastx; int32_t lastsb = 0; int32_t *cb_offset = coderInfo->sfb_offset; int32_t num_cb = coderInfo->nr_of_sfb; eng_t avgenrg = coderInfo->avgenrg; static real_32_t realconst1 = REAL_CONST(0.075); static real_32_t realconst2 = REAL_CONST(1.4); static real_32_t realconst3 = REAL_CONST(0.4); static real_32_t realconst4 = REAL_CONST(147.84); /* 132 * 1.12 */ for (sfb = 0; sfb < num_cb; sfb++) { if (last > cb_offset[sfb]) lastsb = sfb; } for (sfb = 0; sfb < num_cb; sfb++) { real_t thr; real_32_t tmp; eng_t enrg = 0; start = cb_offset[sfb]; end = cb_offset[sfb + 1]; if (sfb > lastsb) { xmin[sfb] = 0; continue; } if (coderInfo->block_type != ONLY_SHORT_WINDOW) { eng_t enmax = -1; int32_t lmax; lmax = start; for (l = start; l < end; l++) { if (enmax < xr2[l]) { enmax = xr2[l]; lmax = l; } } start = lmax - 2; end = lmax + 3; if (start < 0) start = 0; if (end > last) end = last; } for (l = start; l < end; l++) { enrg += xr2[l]; } if ( (avgenrg == 0) || (enrg==0) ) thr = 0; else { thr = (avgenrg<<REAL_BITS)*(end-start)/enrg; thr = faac_pow(thr, REAL_ICONST(sfb)/(lastsb*10)-realconst3); } tmp = DIV_R(REAL_ICONST(last-start),REAL_ICONST(last)); tmp = MUL_R(MUL_R(tmp,tmp),tmp) + realconst1; thr = MUL_R(realconst2,thr) + tmp; xmin[sfb] = DIV_R(DIV_R(realconst4,thr),quality); #ifdef DUMP_XMIN printf("xmin[%d] = %.8f\n",sfb,REAL2FLOAT(xmin[sfb])); #endif } #ifdef DUMP_XMIN // exit(1); #endif }
static void hf_assembly(sbr_info *sbr, sbr_hfadj_info *adj, qmf_t Xsbr[MAX_NTSRHFG][64], uint8_t ch) { static real_t h_smooth[] = { COEF_CONST(0.03183050093751), COEF_CONST(0.11516383427084), COEF_CONST(0.21816949906249), COEF_CONST(0.30150283239582), COEF_CONST(0.33333333333333) }; static int8_t phi_re[] = { 1, 0, -1, 0 }; static int8_t phi_im[] = { 0, 1, 0, -1 }; uint8_t m, l, i, n; uint16_t fIndexNoise = 0; uint8_t fIndexSine = 0; uint8_t assembly_reset = 0; real_t *temp; real_t G_filt, Q_filt; uint8_t h_SL; if (sbr->Reset == 1) { assembly_reset = 1; fIndexNoise = 0; } else { fIndexNoise = sbr->index_noise_prev[ch]; } fIndexSine = sbr->psi_is_prev[ch]; for (l = 0; l < sbr->L_E[ch]; l++) { uint8_t no_noise = (l == sbr->l_A[ch] || l == sbr->prevEnvIsShort[ch]) ? 1 : 0; #ifdef SBR_LOW_POWER h_SL = 0; #else h_SL = (sbr->bs_smoothing_mode == 1) ? 0 : 4; h_SL = (no_noise ? 0 : h_SL); #endif if (assembly_reset) { for (n = 0; n < 4; n++) { memcpy(sbr->G_temp_prev[ch][n], adj->G_lim_boost[l], sbr->M*sizeof(real_t)); memcpy(sbr->Q_temp_prev[ch][n], adj->Q_M_lim_boost[l], sbr->M*sizeof(real_t)); } assembly_reset = 0; } for (i = sbr->t_E[ch][l]; i < sbr->t_E[ch][l+1]; i++) { #ifdef SBR_LOW_POWER uint8_t i_min1, i_plus1; uint8_t sinusoids = 0; #endif memcpy(sbr->G_temp_prev[ch][4], adj->G_lim_boost[l], sbr->M*sizeof(real_t)); memcpy(sbr->Q_temp_prev[ch][4], adj->Q_M_lim_boost[l], sbr->M*sizeof(real_t)); for (m = 0; m < sbr->M; m++) { uint8_t j; qmf_t psi; G_filt = 0; Q_filt = 0; j = 0; if (h_SL != 0) { for (n = 0; n <= 4; n++) { G_filt += MUL_C(sbr->G_temp_prev[ch][n][m], h_smooth[j]); Q_filt += MUL_C(sbr->Q_temp_prev[ch][n][m], h_smooth[j]); j++; } } else { G_filt = sbr->G_temp_prev[ch][4][m]; Q_filt = sbr->Q_temp_prev[ch][4][m]; } Q_filt = (adj->S_M_boost[l][m] != 0 || no_noise) ? 0 : Q_filt; /* add noise to the output */ fIndexNoise = (fIndexNoise + 1) & 511; /* the smoothed gain values are applied to Xsbr */ /* V is defined, not calculated */ QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) = MUL_R(G_filt, QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx])) + MUL_F(Q_filt, RE(V[fIndexNoise])); if (sbr->bs_extension_id == 3 && sbr->bs_extension_data == 42) QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) = 16428320; #ifndef SBR_LOW_POWER QMF_IM(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) = MUL_R(G_filt, QMF_IM(Xsbr[i + sbr->tHFAdj][m+sbr->kx])) + MUL_F(Q_filt, IM(V[fIndexNoise])); #endif //if (adj->S_index_mapped[m][l]) { int8_t rev = (((m + sbr->kx) & 1) ? -1 : 1); QMF_RE(psi) = MUL_R(adj->S_M_boost[l][m], phi_re[fIndexSine]); QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) += QMF_RE(psi); #ifndef SBR_LOW_POWER QMF_IM(psi) = rev * MUL_R(adj->S_M_boost[l][m], phi_im[fIndexSine]); QMF_IM(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) += QMF_IM(psi); #else i_min1 = (fIndexSine - 1) & 3; i_plus1 = (fIndexSine + 1) & 3; if (m == 0) { QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx - 1]) -= (-1*rev * MUL_C(MUL_R(adj->S_M_boost[l][0], phi_re[i_plus1]), COEF_CONST(0.00815))); if(m < sbr->M - 1) { QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) -= (rev * MUL_C(MUL_R(adj->S_M_boost[l][1], phi_re[i_plus1]), COEF_CONST(0.00815))); } } if ((m > 0) && (m < sbr->M - 1) && (sinusoids < 16)) { QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) -= (rev * MUL_C(MUL_R(adj->S_M_boost[l][m - 1], phi_re[i_min1]), COEF_CONST(0.00815))); QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) -= (rev * MUL_C(MUL_R(adj->S_M_boost[l][m + 1], phi_re[i_plus1]), COEF_CONST(0.00815))); } if ((m == sbr->M - 1) && (sinusoids < 16)) { if (m > 0) { QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) -= (rev * MUL_C(MUL_R(adj->S_M_boost[l][m - 1], phi_re[i_min1]), COEF_CONST(0.00815))); } if (m + sbr->kx < 64) { QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx + 1]) -= (-1*rev * MUL_C(MUL_R(adj->S_M_boost[l][m], phi_re[i_min1]), COEF_CONST(0.00815))); } } if (adj->S_M_boost[l][m] != 0) sinusoids++; #endif } } fIndexSine = (fIndexSine + 1) & 3; temp = sbr->G_temp_prev[ch][0]; for (n = 0; n < 4; n++) sbr->G_temp_prev[ch][n] = sbr->G_temp_prev[ch][n+1]; sbr->G_temp_prev[ch][4] = temp; temp = sbr->Q_temp_prev[ch][0]; for (n = 0; n < 4; n++) sbr->Q_temp_prev[ch][n] = sbr->Q_temp_prev[ch][n+1]; sbr->Q_temp_prev[ch][4] = temp; } } sbr->index_noise_prev[ch] = fIndexNoise; sbr->psi_is_prev[ch] = fIndexSine; }
static void aliasing_reduction(sbr_info *sbr, sbr_hfadj_info *adj, real_t *deg, uint8_t ch) { uint8_t l, k, m; real_t E_total, E_total_est, G_target, acc; for (l = 0; l < sbr->L_E[ch]; l++) { for (k = 0; k < sbr->N_G[l]; k++) { E_total_est = E_total = 0; for (m = sbr->f_group[l][k<<1]; m < sbr->f_group[l][(k<<1) + 1]; m++) { /* E_curr: integer */ /* G_lim_boost: fixed point */ /* E_total_est: integer */ /* E_total: integer */ E_total_est += sbr->E_curr[ch][m-sbr->kx][l]; E_total += MUL_R(sbr->E_curr[ch][m-sbr->kx][l], adj->G_lim_boost[l][m-sbr->kx]); } /* G_target: fixed point */ if ((E_total_est + EPS) == 0) G_target = 0; else G_target = E_total / (E_total_est + EPS); acc = 0; for (m = sbr->f_group[l][(k<<1)]; m < sbr->f_group[l][(k<<1) + 1]; m++) { real_t alpha; /* alpha: fixed point */ if (m < sbr->kx + sbr->M - 1) { alpha = max(deg[m], deg[m + 1]); } else { alpha = deg[m]; } adj->G_lim_boost[l][m-sbr->kx] = MUL_R(alpha, G_target) + MUL_R((REAL_CONST(1)-alpha), adj->G_lim_boost[l][m-sbr->kx]); /* acc: integer */ acc += MUL_R(adj->G_lim_boost[l][m-sbr->kx], sbr->E_curr[ch][m-sbr->kx][l]); } /* acc: fixed point */ if (acc + EPS == 0) acc = 0; else acc = E_total / (acc + EPS); for(m = sbr->f_group[l][(k<<1)]; m < sbr->f_group[l][(k<<1) + 1]; m++) { adj->G_lim_boost[l][m-sbr->kx] = MUL_R(acc, adj->G_lim_boost[l][m-sbr->kx]); } } } for (l = 0; l < sbr->L_E[ch]; l++) { for (k = 0; k < sbr->N_L[sbr->bs_limiter_bands]; k++) { for (m = sbr->f_table_lim[sbr->bs_limiter_bands][k]; m < sbr->f_table_lim[sbr->bs_limiter_bands][k+1]; m++) { adj->G_lim_boost[l][m] = sqrt(adj->G_lim_boost[l][m]); } } } }
static void estimate_current_envelope(sbr_info *sbr, sbr_hfadj_info *adj, qmf_t Xsbr[MAX_NTSRHFG][64], uint8_t ch) { uint8_t m, l, j, k, k_l, k_h, p; real_t nrg, div; if (sbr->bs_interpol_freq == 1) { for (l = 0; l < sbr->L_E[ch]; l++) { uint8_t i, l_i, u_i; l_i = sbr->t_E[ch][l]; u_i = sbr->t_E[ch][l+1]; div = (real_t)(u_i - l_i); for (m = 0; m < sbr->M; m++) { nrg = 0; for (i = l_i + sbr->tHFAdj; i < u_i + sbr->tHFAdj; i++) { nrg += MUL_R(QMF_RE(Xsbr[i][m + sbr->kx]), QMF_RE(Xsbr[i][m + sbr->kx])) #ifndef SBR_LOW_POWER + MUL_R(QMF_IM(Xsbr[i][m + sbr->kx]), QMF_IM(Xsbr[i][m + sbr->kx])) #endif ; } sbr->E_curr[ch][m][l] = nrg / div; #ifdef SBR_LOW_POWER sbr->E_curr[ch][m][l] *= 2; #endif } } } else { for (l = 0; l < sbr->L_E[ch]; l++) { for (p = 0; p < sbr->n[sbr->f[ch][l]]; p++) { k_l = sbr->f_table_res[sbr->f[ch][l]][p]; k_h = sbr->f_table_res[sbr->f[ch][l]][p+1]; for (k = k_l; k < k_h; k++) { uint8_t i, l_i, u_i; nrg = 0.0; l_i = sbr->t_E[ch][l]; u_i = sbr->t_E[ch][l+1]; div = (real_t)((u_i - l_i)*(k_h - k_l)); for (i = l_i + sbr->tHFAdj; i < u_i + sbr->tHFAdj; i++) { for (j = k_l; j < k_h; j++) { nrg += MUL_R(QMF_RE(Xsbr[i][j]), QMF_RE(Xsbr[i][j])) #ifndef SBR_LOW_POWER + MUL_R(QMF_IM(Xsbr[i][j]), QMF_IM(Xsbr[i][j])) #endif ; } } sbr->E_curr[ch][k - sbr->kx][l] = nrg / div; #ifdef SBR_LOW_POWER sbr->E_curr[ch][k - sbr->kx][l] *= 2; #endif } } } } }
void hf_generation(sbr_info *sbr, qmf_t Xlow[MAX_NTSRHFG][32], qmf_t Xhigh[MAX_NTSRHFG][64] #ifdef SBR_LOW_POWER ,real_t *deg #endif ,uint8_t ch) { uint8_t l, i, x; ALIGN complex_t alpha_0[64], alpha_1[64]; #ifdef SBR_LOW_POWER ALIGN real_t rxx[64]; #endif uint8_t offset = sbr->tHFAdj; uint8_t first = sbr->t_E[ch][0]; uint8_t last = sbr->t_E[ch][sbr->L_E[ch]]; // printf("%d %d\n", first, last); calc_chirp_factors(sbr, ch); for (i = first; i < last; i++) { memset(Xhigh[i + offset], 0, 64 * sizeof(qmf_t)); } if ((ch == 0) && (sbr->Reset)) patch_construction(sbr); /* calculate the prediction coefficients */ calc_prediction_coef(sbr, Xlow, alpha_0, alpha_1 #ifdef SBR_LOW_POWER , rxx #endif ); #ifdef SBR_LOW_POWER calc_aliasing_degree(sbr, rxx, deg); #endif /* actual HF generation */ for (i = 0; i < sbr->noPatches; i++) { for (x = 0; x < sbr->patchNoSubbands[i]; x++) { complex_t a0, a1; real_t bw, bw2; uint8_t q, p, k, g; /* find the low and high band for patching */ k = sbr->kx + x; for (q = 0; q < i; q++) { k += sbr->patchNoSubbands[q]; } p = sbr->patchStartSubband[i] + x; #ifdef SBR_LOW_POWER if (x != 0 /*x < sbr->patchNoSubbands[i]-1*/) deg[k] = deg[p]; else deg[k] = 0; #endif g = sbr->table_map_k_to_g[k]; bw = sbr->bwArray[ch][g]; bw2 = MUL_C(bw, bw); /* do the patching */ /* with or without filtering */ if (bw2 > 0) { RE(a0) = MUL_C(RE(alpha_0[p]), bw); RE(a1) = MUL_C(RE(alpha_1[p]), bw2); #ifndef SBR_LOW_POWER IM(a0) = MUL_C(IM(alpha_0[p]), bw); IM(a1) = MUL_C(IM(alpha_1[p]), bw2); #endif for (l = first; l < last; l++) { QMF_RE(Xhigh[l + offset][k]) = QMF_RE(Xlow[l + offset][p]); #ifndef SBR_LOW_POWER QMF_IM(Xhigh[l + offset][k]) = QMF_IM(Xlow[l + offset][p]); #endif #ifdef SBR_LOW_POWER QMF_RE(Xhigh[l + offset][k]) += ( MUL_R(RE(a0), QMF_RE(Xlow[l - 1 + offset][p])) + MUL_R(RE(a1), QMF_RE(Xlow[l - 2 + offset][p]))); #else QMF_RE(Xhigh[l + offset][k]) += ( RE(a0) * QMF_RE(Xlow[l - 1 + offset][p]) - IM(a0) * QMF_IM(Xlow[l - 1 + offset][p]) + RE(a1) * QMF_RE(Xlow[l - 2 + offset][p]) - IM(a1) * QMF_IM(Xlow[l - 2 + offset][p])); QMF_IM(Xhigh[l + offset][k]) += ( IM(a0) * QMF_RE(Xlow[l - 1 + offset][p]) + RE(a0) * QMF_IM(Xlow[l - 1 + offset][p]) + IM(a1) * QMF_RE(Xlow[l - 2 + offset][p]) + RE(a1) * QMF_IM(Xlow[l - 2 + offset][p])); #endif } } else { for (l = first; l < last; l++) { QMF_RE(Xhigh[l + offset][k]) = QMF_RE(Xlow[l + offset][p]); #ifndef SBR_LOW_POWER QMF_IM(Xhigh[l + offset][k]) = QMF_IM(Xlow[l + offset][p]); #endif } } } } if (sbr->Reset) { limiter_frequency_table(sbr); } }