sbr_hfadj.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:83k
源码类别:

Windows CE

开发平台:

C/C++

  1. /*
  2. ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
  3. ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
  4. **  
  5. ** This program is free software; you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation; either version 2 of the License, or
  8. ** (at your option) any later version.
  9. ** 
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ** GNU General Public License for more details.
  14. ** 
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program; if not, write to the Free Software 
  17. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. **
  19. ** Any non-GPL usage of this software or parts of this software is strictly
  20. ** forbidden.
  21. **
  22. ** Commercial non-GPL licensing of this software is possible.
  23. ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
  24. **
  25. ** $Id: sbr_hfadj.c,v 1.18 2004/09/04 14:56:28 menno Exp $
  26. **/
  27. /* High Frequency adjustment */
  28. #include "common.h"
  29. #include "structs.h"
  30. #ifdef SBR_DEC
  31. #include "sbr_syntax.h"
  32. #include "sbr_hfadj.h"
  33. #include "sbr_noise.h"
  34. /* static function declarations */
  35. static void estimate_current_envelope(sbr_info *sbr, sbr_hfadj_info *adj,
  36.                                       qmf_t Xsbr[MAX_NTSRHFG][64], uint8_t ch);
  37. static void calculate_gain(sbr_info *sbr, sbr_hfadj_info *adj, uint8_t ch);
  38. #ifdef SBR_LOW_POWER
  39. static void calc_gain_groups(sbr_info *sbr, sbr_hfadj_info *adj, real_t *deg, uint8_t ch);
  40. static void aliasing_reduction(sbr_info *sbr, sbr_hfadj_info *adj, real_t *deg, uint8_t ch);
  41. #endif
  42. static void hf_assembly(sbr_info *sbr, sbr_hfadj_info *adj, qmf_t Xsbr[MAX_NTSRHFG][64], uint8_t ch);
  43. void hf_adjustment(sbr_info *sbr, qmf_t Xsbr[MAX_NTSRHFG][64]
  44. #ifdef SBR_LOW_POWER
  45.                    ,real_t *deg /* aliasing degree */
  46. #endif
  47.                    ,uint8_t ch)
  48. {
  49.     ALIGN sbr_hfadj_info adj = {{{0}}};
  50.     if (sbr->bs_frame_class[ch] == FIXFIX)
  51.     {
  52.         sbr->l_A[ch] = -1;
  53.     } else if (sbr->bs_frame_class[ch] == VARFIX) {
  54.         if (sbr->bs_pointer[ch] > 1)
  55.             sbr->l_A[ch] = -1;
  56.         else
  57.             sbr->l_A[ch] = sbr->bs_pointer[ch] - 1;
  58.     } else {
  59.         if (sbr->bs_pointer[ch] == 0)
  60.             sbr->l_A[ch] = -1;
  61.         else
  62.             sbr->l_A[ch] = sbr->L_E[ch] + 1 - sbr->bs_pointer[ch];
  63.     }
  64.     estimate_current_envelope(sbr, &adj, Xsbr, ch);
  65.     calculate_gain(sbr, &adj, ch);
  66. #ifdef SBR_LOW_POWER
  67.     calc_gain_groups(sbr, &adj, deg, ch);
  68.     aliasing_reduction(sbr, &adj, deg, ch);
  69. #endif
  70.     hf_assembly(sbr, &adj, Xsbr, ch);
  71. }
  72. static uint8_t get_S_mapped(sbr_info *sbr, uint8_t ch, uint8_t l, uint8_t current_band)
  73. {
  74.     if (sbr->f[ch][l] == HI_RES)
  75.     {
  76.         /* in case of using f_table_high we just have 1 to 1 mapping
  77.          * from bs_add_harmonic[l][k]
  78.          */
  79.         if ((l >= sbr->l_A[ch]) ||
  80.             (sbr->bs_add_harmonic_prev[ch][current_band] && sbr->bs_add_harmonic_flag_prev[ch]))
  81.         {
  82.             return sbr->bs_add_harmonic[ch][current_band];
  83.         }
  84.     } else {
  85.         uint8_t b, lb, ub;
  86.         /* in case of f_table_low we check if any of the HI_RES bands
  87.          * within this LO_RES band has bs_add_harmonic[l][k] turned on
  88.          * (note that borders in the LO_RES table are also present in
  89.          * the HI_RES table)
  90.          */
  91.         /* find first HI_RES band in current LO_RES band */
  92.         lb = 2*current_band - ((sbr->N_high & 1) ? 1 : 0);
  93.         /* find first HI_RES band in next LO_RES band */
  94.         ub = 2*(current_band+1) - ((sbr->N_high & 1) ? 1 : 0);
  95.         /* check all HI_RES bands in current LO_RES band for sinusoid */
  96.         for (b = lb; b < ub; b++)
  97.         {
  98.             if ((l >= sbr->l_A[ch]) ||
  99.                 (sbr->bs_add_harmonic_prev[ch][b] && sbr->bs_add_harmonic_flag_prev[ch]))
  100.             {
  101.                 if (sbr->bs_add_harmonic[ch][b] == 1)
  102.                     return 1;
  103.             }
  104.         }
  105.     }
  106.     return 0;
  107. }
  108. static void estimate_current_envelope(sbr_info *sbr, sbr_hfadj_info *adj,
  109.                                       qmf_t Xsbr[MAX_NTSRHFG][64], uint8_t ch)
  110. {
  111.     uint8_t m, l, j, k, k_l, k_h, p;
  112.     real_t nrg, div;
  113.     if (sbr->bs_interpol_freq == 1)
  114.     {
  115.         for (l = 0; l < sbr->L_E[ch]; l++)
  116.         {
  117.             uint8_t i, l_i, u_i;
  118.             l_i = sbr->t_E[ch][l];
  119.             u_i = sbr->t_E[ch][l+1];
  120.             div = (real_t)(u_i - l_i);
  121.             for (m = 0; m < sbr->M; m++)
  122.             {
  123.                 nrg = 0;
  124.                 for (i = l_i + sbr->tHFAdj; i < u_i + sbr->tHFAdj; i++)
  125.                 {
  126. #ifdef FIXED_POINT
  127. #ifdef SBR_LOW_POWER
  128.                     nrg += ((QMF_RE(Xsbr[i][m + sbr->kx])+(1<<(REAL_BITS-1)))>>REAL_BITS)*((QMF_RE(Xsbr[i][m + sbr->kx])+(1<<(REAL_BITS-1)))>>REAL_BITS);
  129. #else
  130.                     nrg += ((QMF_RE(Xsbr[i][m + sbr->kx])+(1<<(REAL_BITS-1)))>>REAL_BITS)*((QMF_RE(Xsbr[i][m + sbr->kx])+(1<<(REAL_BITS-1)))>>REAL_BITS) +
  131.                         ((QMF_IM(Xsbr[i][m + sbr->kx])+(1<<(REAL_BITS-1)))>>REAL_BITS)*((QMF_IM(Xsbr[i][m + sbr->kx])+(1<<(REAL_BITS-1)))>>REAL_BITS);
  132. #endif
  133. #else
  134.                     nrg += MUL_R(QMF_RE(Xsbr[i][m + sbr->kx]), QMF_RE(Xsbr[i][m + sbr->kx]))
  135. #ifndef SBR_LOW_POWER
  136.                         + MUL_R(QMF_IM(Xsbr[i][m + sbr->kx]), QMF_IM(Xsbr[i][m + sbr->kx]))
  137. #endif
  138.                         ;
  139. #endif
  140.                 }
  141.                 sbr->E_curr[ch][m][l] = nrg / div;
  142. #ifdef SBR_LOW_POWER
  143. #ifdef FIXED_POINT
  144.                 sbr->E_curr[ch][m][l] <<= 1;
  145. #else
  146.                 sbr->E_curr[ch][m][l] *= 2;
  147. #endif
  148. #endif
  149.             }
  150.         }
  151.     } else {
  152.         for (l = 0; l < sbr->L_E[ch]; l++)
  153.         {
  154.             for (p = 0; p < sbr->n[sbr->f[ch][l]]; p++)
  155.             {
  156.                 k_l = sbr->f_table_res[sbr->f[ch][l]][p];
  157.                 k_h = sbr->f_table_res[sbr->f[ch][l]][p+1];
  158.                 for (k = k_l; k < k_h; k++)
  159.                 {
  160.                     uint8_t i, l_i, u_i;
  161.                     nrg = 0;
  162.                     l_i = sbr->t_E[ch][l];
  163.                     u_i = sbr->t_E[ch][l+1];
  164.                     div = (real_t)((u_i - l_i)*(k_h - k_l));
  165.                     for (i = l_i + sbr->tHFAdj; i < u_i + sbr->tHFAdj; i++)
  166.                     {
  167.                         for (j = k_l; j < k_h; j++)
  168.                         {
  169. #ifdef FIXED_POINT
  170. #ifdef SBR_LOW_POWER
  171.                             nrg += ((QMF_RE(Xsbr[i][j])+(1<<(REAL_BITS-1)))>>REAL_BITS)*((QMF_RE(Xsbr[i][j])+(1<<(REAL_BITS-1)))>>REAL_BITS);
  172. #else
  173.                             nrg += ((QMF_RE(Xsbr[i][j])+(1<<(REAL_BITS-1)))>>REAL_BITS)*((QMF_RE(Xsbr[i][j])+(1<<(REAL_BITS-1)))>>REAL_BITS) +
  174.                                 ((QMF_IM(Xsbr[i][j])+(1<<(REAL_BITS-1)))>>REAL_BITS)*((QMF_IM(Xsbr[i][j])+(1<<(REAL_BITS-1)))>>REAL_BITS);
  175. #endif
  176. #else
  177.                             nrg += MUL_R(QMF_RE(Xsbr[i][j]), QMF_RE(Xsbr[i][j]))
  178. #ifndef SBR_LOW_POWER
  179.                                 + MUL_R(QMF_IM(Xsbr[i][j]), QMF_IM(Xsbr[i][j]))
  180. #endif
  181.                                 ;
  182. #endif
  183.                         }
  184.                     }
  185.                     sbr->E_curr[ch][k - sbr->kx][l] = nrg / div;
  186. #ifdef SBR_LOW_POWER
  187. #ifdef FIXED_POINT
  188.                     sbr->E_curr[ch][k - sbr->kx][l] <<= 1;
  189. #else
  190.                     sbr->E_curr[ch][k - sbr->kx][l] *= 2;
  191. #endif
  192. #endif
  193.                 }
  194.             }
  195.         }
  196.     }
  197. }
  198. #ifdef FIXED_POINT
  199. #define EPS (1) /* smallest number available in fixed point */
  200. #else
  201. #define EPS (1e-12)
  202. #endif
  203. #ifdef FIXED_POINT
  204. /* log2 values of [0..63] */
  205. static const real_t log2_int_tab[] = {
  206.     LOG2_MIN_INF, REAL_CONST(0.000000000000000), REAL_CONST(1.000000000000000), REAL_CONST(1.584962500721156),
  207.     REAL_CONST(2.000000000000000), REAL_CONST(2.321928094887362), REAL_CONST(2.584962500721156), REAL_CONST(2.807354922057604),
  208.     REAL_CONST(3.000000000000000), REAL_CONST(3.169925001442313), REAL_CONST(3.321928094887363), REAL_CONST(3.459431618637297),
  209.     REAL_CONST(3.584962500721156), REAL_CONST(3.700439718141092), REAL_CONST(3.807354922057604), REAL_CONST(3.906890595608519),
  210.     REAL_CONST(4.000000000000000), REAL_CONST(4.087462841250339), REAL_CONST(4.169925001442312), REAL_CONST(4.247927513443585),
  211.     REAL_CONST(4.321928094887362), REAL_CONST(4.392317422778761), REAL_CONST(4.459431618637297), REAL_CONST(4.523561956057013),
  212.     REAL_CONST(4.584962500721156), REAL_CONST(4.643856189774724), REAL_CONST(4.700439718141093), REAL_CONST(4.754887502163468),
  213.     REAL_CONST(4.807354922057604), REAL_CONST(4.857980995127572), REAL_CONST(4.906890595608519), REAL_CONST(4.954196310386875),
  214.     REAL_CONST(5.000000000000000), REAL_CONST(5.044394119358453), REAL_CONST(5.087462841250340), REAL_CONST(5.129283016944966),
  215.     REAL_CONST(5.169925001442312), REAL_CONST(5.209453365628949), REAL_CONST(5.247927513443585), REAL_CONST(5.285402218862248),
  216.     REAL_CONST(5.321928094887363), REAL_CONST(5.357552004618084), REAL_CONST(5.392317422778761), REAL_CONST(5.426264754702098),
  217.     REAL_CONST(5.459431618637297), REAL_CONST(5.491853096329675), REAL_CONST(5.523561956057013), REAL_CONST(5.554588851677637),
  218.     REAL_CONST(5.584962500721156), REAL_CONST(5.614709844115208), REAL_CONST(5.643856189774724), REAL_CONST(5.672425341971495),
  219.     REAL_CONST(5.700439718141093), REAL_CONST(5.727920454563200), REAL_CONST(5.754887502163469), REAL_CONST(5.781359713524660),
  220.     REAL_CONST(5.807354922057605), REAL_CONST(5.832890014164742), REAL_CONST(5.857980995127572), REAL_CONST(5.882643049361842),
  221.     REAL_CONST(5.906890595608518), REAL_CONST(5.930737337562887), REAL_CONST(5.954196310386876), REAL_CONST(5.977279923499916)
  222. };
  223. static const real_t pan_log2_tab[] = {
  224.     REAL_CONST(1.000000000000000), REAL_CONST(0.584962500721156), REAL_CONST(0.321928094887362), REAL_CONST(0.169925001442312), REAL_CONST(0.087462841250339),
  225.     REAL_CONST(0.044394119358453), REAL_CONST(0.022367813028455), REAL_CONST(0.011227255423254), REAL_CONST(0.005624549193878), REAL_CONST(0.002815015607054),
  226.     REAL_CONST(0.001408194392808), REAL_CONST(0.000704269011247), REAL_CONST(0.000352177480301), REAL_CONST(0.000176099486443), REAL_CONST(0.000088052430122),
  227.     REAL_CONST(0.000044026886827), REAL_CONST(0.000022013611360), REAL_CONST(0.000011006847667)
  228. };
  229. static real_t find_log2_E(sbr_info *sbr, uint8_t k, uint8_t l, uint8_t ch)
  230. {
  231.     /* check for coupled energy/noise data */
  232.     if (sbr->bs_coupling == 1)
  233.     {
  234.         uint8_t amp0 = (sbr->amp_res[0]) ? 0 : 1;
  235.         uint8_t amp1 = (sbr->amp_res[1]) ? 0 : 1;
  236.         real_t tmp = (7 << REAL_BITS) + (sbr->E[0][k][l] << (REAL_BITS-amp0));
  237.         real_t pan;
  238.         /* E[1] should always be even so shifting is OK */
  239.         uint8_t E = sbr->E[1][k][l] >> amp1;
  240.         if (ch == 0)
  241.         {
  242.             if (E > 12)
  243.             {
  244.                 /* negative */
  245.                 pan = pan_log2_tab[-12 + E];
  246.             } else {
  247.                 /* positive */
  248.                 pan = pan_log2_tab[12 - E] + ((12 - E)<<REAL_BITS);
  249.             }
  250.         } else {
  251.             if (E < 12)
  252.             {
  253.                 /* negative */
  254.                 pan = pan_log2_tab[-E + 12];
  255.             } else {
  256.                 /* positive */
  257.                 pan = pan_log2_tab[E - 12] + ((E - 12)<<REAL_BITS);
  258.             }
  259.         }
  260.         /* tmp / pan in log2 */
  261.         return tmp - pan;
  262.     } else {
  263.         uint8_t amp = (sbr->amp_res[ch]) ? 0 : 1;
  264.         return (6 << REAL_BITS) + (sbr->E[ch][k][l] << (REAL_BITS-amp));
  265.     }
  266. }
  267. static real_t find_log2_Q(sbr_info *sbr, uint8_t k, uint8_t l, uint8_t ch)
  268. {
  269.     /* check for coupled energy/noise data */
  270.     if (sbr->bs_coupling == 1)
  271.     {
  272.         real_t tmp = (7 << REAL_BITS) - (sbr->Q[0][k][l] << REAL_BITS);
  273.         real_t pan;
  274.         uint8_t Q = sbr->Q[1][k][l];
  275.         if (ch == 0)
  276.         {
  277.             if (Q > 12)
  278.             {
  279.                 /* negative */
  280.                 pan = pan_log2_tab[-12 + Q];
  281.             } else {
  282.                 /* positive */
  283.                 pan = pan_log2_tab[12 - Q] + ((12 - Q)<<REAL_BITS);
  284.             }
  285.         } else {
  286.             if (Q < 12)
  287.             {
  288.                 /* negative */
  289.                 pan = pan_log2_tab[-Q + 12];
  290.             } else {
  291.                 /* positive */
  292.                 pan = pan_log2_tab[Q - 12] + ((Q - 12)<<REAL_BITS);
  293.             }
  294.         }
  295.         /* tmp / pan in log2 */
  296.         return tmp - pan;
  297.     } else {
  298.         return (6 << REAL_BITS) - (sbr->Q[ch][k][l] << REAL_BITS);
  299.     }
  300. }
  301. static const real_t log_Qplus1_pan[31][13] = {
  302.     { REAL_CONST(0.044383447617292), REAL_CONST(0.169768601655960), REAL_CONST(0.583090126514435), REAL_CONST(1.570089221000671), REAL_CONST(3.092446088790894), REAL_CONST(4.733354568481445), REAL_CONST(6.022367954254150), REAL_CONST(6.692092418670654), REAL_CONST(6.924463272094727), REAL_CONST(6.989034175872803), REAL_CONST(7.005646705627441), REAL_CONST(7.009829998016357), REAL_CONST(7.010877609252930) },
  303.     { REAL_CONST(0.022362394258380), REAL_CONST(0.087379962205887), REAL_CONST(0.320804953575134), REAL_CONST(0.988859415054321), REAL_CONST(2.252387046813965), REAL_CONST(3.786596298217773), REAL_CONST(5.044394016265869), REAL_CONST(5.705977916717529), REAL_CONST(5.936291694641113), REAL_CONST(6.000346660614014), REAL_CONST(6.016829967498779), REAL_CONST(6.020981311798096), REAL_CONST(6.022020816802979) },
  304.     { REAL_CONST(0.011224525049329), REAL_CONST(0.044351425021887), REAL_CONST(0.169301137328148), REAL_CONST(0.577544987201691), REAL_CONST(1.527246952056885), REAL_CONST(2.887525320053101), REAL_CONST(4.087462902069092), REAL_CONST(4.733354568481445), REAL_CONST(4.959661006927490), REAL_CONST(5.022709369659424), REAL_CONST(5.038940429687500), REAL_CONST(5.043028831481934), REAL_CONST(5.044052600860596) },
  305.     { REAL_CONST(0.005623178556561), REAL_CONST(0.022346137091517), REAL_CONST(0.087132595479488), REAL_CONST(0.317482173442841), REAL_CONST(0.956931233406067), REAL_CONST(2.070389270782471), REAL_CONST(3.169924974441528), REAL_CONST(3.786596298217773), REAL_CONST(4.005294322967529), REAL_CONST(4.066420555114746), REAL_CONST(4.082170009613037), REAL_CONST(4.086137294769287), REAL_CONST(4.087131500244141) },
  306.     { REAL_CONST(0.002814328996465), REAL_CONST(0.011216334067285), REAL_CONST(0.044224001467228), REAL_CONST(0.167456731200218), REAL_CONST(0.556393325328827), REAL_CONST(1.378511548042297), REAL_CONST(2.321928024291992), REAL_CONST(2.887525320053101), REAL_CONST(3.092446088790894), REAL_CONST(3.150059700012207), REAL_CONST(3.164926528930664), REAL_CONST(3.168673276901245), REAL_CONST(3.169611930847168) },
  307.     { REAL_CONST(0.001407850766554), REAL_CONST(0.005619067233056), REAL_CONST(0.022281449288130), REAL_CONST(0.086156636476517), REAL_CONST(0.304854571819305), REAL_CONST(0.847996890544891), REAL_CONST(1.584962487220764), REAL_CONST(2.070389270782471), REAL_CONST(2.252387046813965), REAL_CONST(2.304061651229858), REAL_CONST(2.317430257797241), REAL_CONST(2.320801734924316), REAL_CONST(2.321646213531494) },
  308.     { REAL_CONST(0.000704097095877), REAL_CONST(0.002812269143760), REAL_CONST(0.011183738708496), REAL_CONST(0.043721374124289), REAL_CONST(0.160464659333229), REAL_CONST(0.485426813364029), REAL_CONST(1.000000000000000), REAL_CONST(1.378511548042297), REAL_CONST(1.527246952056885), REAL_CONST(1.570089221000671), REAL_CONST(1.581215262413025), REAL_CONST(1.584023833274841), REAL_CONST(1.584727644920349) },
  309.     { REAL_CONST(0.000352177477907), REAL_CONST(0.001406819908880), REAL_CONST(0.005602621007711), REAL_CONST(0.022026389837265), REAL_CONST(0.082462236285210), REAL_CONST(0.263034462928772), REAL_CONST(0.584962487220764), REAL_CONST(0.847996890544891), REAL_CONST(0.956931233406067), REAL_CONST(0.988859415054321), REAL_CONST(0.997190535068512), REAL_CONST(0.999296069145203), REAL_CONST(0.999823868274689) },
  310.     { REAL_CONST(0.000176099492819), REAL_CONST(0.000703581434209), REAL_CONST(0.002804030198604), REAL_CONST(0.011055230163038), REAL_CONST(0.041820213198662), REAL_CONST(0.137503549456596), REAL_CONST(0.321928083896637), REAL_CONST(0.485426813364029), REAL_CONST(0.556393325328827), REAL_CONST(0.577544987201691), REAL_CONST(0.583090126514435), REAL_CONST(0.584493279457092), REAL_CONST(0.584845066070557) },
  311.     { REAL_CONST(0.000088052431238), REAL_CONST(0.000351833587047), REAL_CONST(0.001402696361765), REAL_CONST(0.005538204684854), REAL_CONST(0.021061634644866), REAL_CONST(0.070389263331890), REAL_CONST(0.169925004243851), REAL_CONST(0.263034462928772), REAL_CONST(0.304854571819305), REAL_CONST(0.317482173442841), REAL_CONST(0.320804953575134), REAL_CONST(0.321646571159363), REAL_CONST(0.321857661008835) },
  312.     { REAL_CONST(0.000044026888645), REAL_CONST(0.000175927518285), REAL_CONST(0.000701518612914), REAL_CONST(0.002771759871393), REAL_CONST(0.010569252073765), REAL_CONST(0.035623874515295), REAL_CONST(0.087462842464447), REAL_CONST(0.137503549456596), REAL_CONST(0.160464659333229), REAL_CONST(0.167456731200218), REAL_CONST(0.169301137328148), REAL_CONST(0.169768601655960), REAL_CONST(0.169885858893394) },
  313.     { REAL_CONST(0.000022013611670), REAL_CONST(0.000088052431238), REAL_CONST(0.000350801943569), REAL_CONST(0.001386545598507), REAL_CONST(0.005294219125062), REAL_CONST(0.017921976745129), REAL_CONST(0.044394120573997), REAL_CONST(0.070389263331890), REAL_CONST(0.082462236285210), REAL_CONST(0.086156636476517), REAL_CONST(0.087132595479488), REAL_CONST(0.087379962205887), REAL_CONST(0.087442122399807) },
  314.     { REAL_CONST(0.000011006847672), REAL_CONST(0.000044026888645), REAL_CONST(0.000175411638338), REAL_CONST(0.000693439331371), REAL_CONST(0.002649537986144), REAL_CONST(0.008988817222416), REAL_CONST(0.022367812693119), REAL_CONST(0.035623874515295), REAL_CONST(0.041820213198662), REAL_CONST(0.043721374124289), REAL_CONST(0.044224001467228), REAL_CONST(0.044351425021887), REAL_CONST(0.044383447617292) },
  315.     { REAL_CONST(0.000005503434295), REAL_CONST(0.000022013611670), REAL_CONST(0.000087708482170), REAL_CONST(0.000346675369656), REAL_CONST(0.001325377263129), REAL_CONST(0.004501323681325), REAL_CONST(0.011227255687118), REAL_CONST(0.017921976745129), REAL_CONST(0.021061634644866), REAL_CONST(0.022026389837265), REAL_CONST(0.022281449288130), REAL_CONST(0.022346137091517), REAL_CONST(0.022362394258380) },
  316.     { REAL_CONST(0.000002751719876), REAL_CONST(0.000011006847672), REAL_CONST(0.000043854910473), REAL_CONST(0.000173348103999), REAL_CONST(0.000662840844598), REAL_CONST(0.002252417383716), REAL_CONST(0.005624548997730), REAL_CONST(0.008988817222416), REAL_CONST(0.010569252073765), REAL_CONST(0.011055230163038), REAL_CONST(0.011183738708496), REAL_CONST(0.011216334067285), REAL_CONST(0.011224525049329) },
  317.     { REAL_CONST(0.000001375860506), REAL_CONST(0.000005503434295), REAL_CONST(0.000022013611670), REAL_CONST(0.000086676649516), REAL_CONST(0.000331544462824), REAL_CONST(0.001126734190620), REAL_CONST(0.002815015614033), REAL_CONST(0.004501323681325), REAL_CONST(0.005294219125062), REAL_CONST(0.005538204684854), REAL_CONST(0.005602621007711), REAL_CONST(0.005619067233056), REAL_CONST(0.005623178556561) },
  318.     { REAL_CONST(0.000000687930424), REAL_CONST(0.000002751719876), REAL_CONST(0.000011006847672), REAL_CONST(0.000043338975956), REAL_CONST(0.000165781748365), REAL_CONST(0.000563477107789), REAL_CONST(0.001408194424585), REAL_CONST(0.002252417383716), REAL_CONST(0.002649537986144), REAL_CONST(0.002771759871393), REAL_CONST(0.002804030198604), REAL_CONST(0.002812269143760), REAL_CONST(0.002814328996465) },
  319.     { REAL_CONST(0.000000343965269), REAL_CONST(0.000001375860506), REAL_CONST(0.000005503434295), REAL_CONST(0.000021669651687), REAL_CONST(0.000082893253420), REAL_CONST(0.000281680084299), REAL_CONST(0.000704268983100), REAL_CONST(0.001126734190620), REAL_CONST(0.001325377263129), REAL_CONST(0.001386545598507), REAL_CONST(0.001402696361765), REAL_CONST(0.001406819908880), REAL_CONST(0.001407850766554) },
  320.     { REAL_CONST(0.000000171982634), REAL_CONST(0.000000687930424), REAL_CONST(0.000002751719876), REAL_CONST(0.000010834866771), REAL_CONST(0.000041447223339), REAL_CONST(0.000140846910654), REAL_CONST(0.000352177477907), REAL_CONST(0.000563477107789), REAL_CONST(0.000662840844598), REAL_CONST(0.000693439331371), REAL_CONST(0.000701518612914), REAL_CONST(0.000703581434209), REAL_CONST(0.000704097095877) },
  321.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000343965269), REAL_CONST(0.000001375860506), REAL_CONST(0.000005503434295), REAL_CONST(0.000020637769921), REAL_CONST(0.000070511166996), REAL_CONST(0.000176099492819), REAL_CONST(0.000281680084299), REAL_CONST(0.000331544462824), REAL_CONST(0.000346675369656), REAL_CONST(0.000350801943569), REAL_CONST(0.000351833587047), REAL_CONST(0.000352177477907) },
  322.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000171982634), REAL_CONST(0.000000687930424), REAL_CONST(0.000002751719876), REAL_CONST(0.000010318922250), REAL_CONST(0.000035256012779), REAL_CONST(0.000088052431238), REAL_CONST(0.000140846910654), REAL_CONST(0.000165781748365), REAL_CONST(0.000173348103999), REAL_CONST(0.000175411638338), REAL_CONST(0.000175927518285), REAL_CONST(0.000176099492819) },
  323.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000343965269), REAL_CONST(0.000001375860506), REAL_CONST(0.000005159470220), REAL_CONST(0.000017542124624), REAL_CONST(0.000044026888645), REAL_CONST(0.000070511166996), REAL_CONST(0.000082893253420), REAL_CONST(0.000086676649516), REAL_CONST(0.000087708482170), REAL_CONST(0.000088052431238), REAL_CONST(0.000088052431238) },
  324.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000171982634), REAL_CONST(0.000000687930424), REAL_CONST(0.000002579737384), REAL_CONST(0.000008771088687), REAL_CONST(0.000022013611670), REAL_CONST(0.000035256012779), REAL_CONST(0.000041447223339), REAL_CONST(0.000043338975956), REAL_CONST(0.000043854910473), REAL_CONST(0.000044026888645), REAL_CONST(0.000044026888645) },
  325.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000343965269), REAL_CONST(0.000001375860506), REAL_CONST(0.000004471542070), REAL_CONST(0.000011006847672), REAL_CONST(0.000017542124624), REAL_CONST(0.000020637769921), REAL_CONST(0.000021669651687), REAL_CONST(0.000022013611670), REAL_CONST(0.000022013611670), REAL_CONST(0.000022013611670) },
  326.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000171982634), REAL_CONST(0.000000687930424), REAL_CONST(0.000002235772627), REAL_CONST(0.000005503434295), REAL_CONST(0.000008771088687), REAL_CONST(0.000010318922250), REAL_CONST(0.000010834866771), REAL_CONST(0.000011006847672), REAL_CONST(0.000011006847672), REAL_CONST(0.000011006847672) },
  327.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000343965269), REAL_CONST(0.000001031895522), REAL_CONST(0.000002751719876), REAL_CONST(0.000004471542070), REAL_CONST(0.000005159470220), REAL_CONST(0.000005503434295), REAL_CONST(0.000005503434295), REAL_CONST(0.000005503434295), REAL_CONST(0.000005503434295) },
  328.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000171982634), REAL_CONST(0.000000515947875), REAL_CONST(0.000001375860506), REAL_CONST(0.000002235772627), REAL_CONST(0.000002579737384), REAL_CONST(0.000002751719876), REAL_CONST(0.000002751719876), REAL_CONST(0.000002751719876), REAL_CONST(0.000002751719876) },
  329.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000343965269), REAL_CONST(0.000000687930424), REAL_CONST(0.000001031895522), REAL_CONST(0.000001375860506), REAL_CONST(0.000001375860506), REAL_CONST(0.000001375860506), REAL_CONST(0.000001375860506), REAL_CONST(0.000001375860506) },
  330.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000171982634), REAL_CONST(0.000000343965269), REAL_CONST(0.000000515947875), REAL_CONST(0.000000687930424), REAL_CONST(0.000000687930424), REAL_CONST(0.000000687930424), REAL_CONST(0.000000687930424), REAL_CONST(0.000000687930424) },
  331.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000171982634), REAL_CONST(0.000000343965269), REAL_CONST(0.000000343965269), REAL_CONST(0.000000343965269), REAL_CONST(0.000000343965269), REAL_CONST(0.000000343965269), REAL_CONST(0.000000343965269) },
  332.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000171982634), REAL_CONST(0.000000171982634), REAL_CONST(0.000000171982634), REAL_CONST(0.000000171982634), REAL_CONST(0.000000171982634), REAL_CONST(0.000000171982634) }
  333. };
  334. static const real_t log_Qplus1[31] = {
  335.     REAL_CONST(6.022367813028454), REAL_CONST(5.044394119358453), REAL_CONST(4.087462841250339), 
  336.     REAL_CONST(3.169925001442313), REAL_CONST(2.321928094887362), REAL_CONST(1.584962500721156), 
  337.     REAL_CONST(1.000000000000000), REAL_CONST(0.584962500721156), REAL_CONST(0.321928094887362), 
  338.     REAL_CONST(0.169925001442312), REAL_CONST(0.087462841250339), REAL_CONST(0.044394119358453), 
  339.     REAL_CONST(0.022367813028455), REAL_CONST(0.011227255423254), REAL_CONST(0.005624549193878), 
  340.     REAL_CONST(0.002815015607054), REAL_CONST(0.001408194392808), REAL_CONST(0.000704269011247), 
  341.     REAL_CONST(0.000352177480301), REAL_CONST(0.000176099486443), REAL_CONST(0.000088052430122), 
  342.     REAL_CONST(0.000044026886827), REAL_CONST(0.000022013611360), REAL_CONST(0.000011006847667), 
  343.     REAL_CONST(0.000005503434331), REAL_CONST(0.000002751719790), REAL_CONST(0.000001375860551), 
  344.     REAL_CONST(0.000000687930439), REAL_CONST(0.000000343965261), REAL_CONST(0.000000171982641), 
  345.     REAL_CONST(0.000000000000000)
  346. };
  347. static real_t find_log2_Qplus1(sbr_info *sbr, uint8_t k, uint8_t l, uint8_t ch)
  348. {
  349.     /* check for coupled energy/noise data */
  350.     if (sbr->bs_coupling == 1)
  351.     {
  352.         if ((sbr->Q[0][k][l] >= 0) && (sbr->Q[0][k][l] <= 30) &&
  353.             (sbr->Q[1][k][l] >= 0) && (sbr->Q[1][k][l] <= 24))
  354.         {
  355.             if (ch == 0)
  356.             {
  357.                 return log_Qplus1_pan[sbr->Q[0][k][l]][sbr->Q[1][k][l] >> 1];
  358.             } else {
  359.                 return log_Qplus1_pan[sbr->Q[0][k][l]][12 - (sbr->Q[1][k][l] >> 1)];
  360.             }
  361.         } else {
  362.             return 0;
  363.         }
  364.     } else {
  365.         if (sbr->Q[ch][k][l] >= 0 && sbr->Q[ch][k][l] <= 30)
  366.         {
  367.             return log_Qplus1[sbr->Q[ch][k][l]];
  368.         } else {
  369.             return 0;
  370.         }
  371.     }
  372. }
  373. static void calculate_gain(sbr_info *sbr, sbr_hfadj_info *adj, uint8_t ch)
  374. {
  375.     /* log2 values of limiter gains */
  376.     static const real_t limGain[] = {
  377.         REAL_CONST(-1.0), REAL_CONST(0.0), REAL_CONST(1.0), REAL_CONST(33.219)
  378.     };
  379.     uint8_t m, l, k;
  380.     uint8_t current_t_noise_band = 0;
  381.     uint8_t S_mapped;
  382.     ALIGN real_t Q_M_lim[MAX_M];
  383.     ALIGN real_t G_lim[MAX_M];
  384.     ALIGN real_t G_boost;
  385.     ALIGN real_t S_M[MAX_M];
  386.     for (l = 0; l < sbr->L_E[ch]; l++)
  387.     {
  388.         uint8_t current_f_noise_band = 0;
  389.         uint8_t current_res_band = 0;
  390.         uint8_t current_res_band2 = 0;
  391.         uint8_t current_hi_res_band = 0;
  392.         real_t delta = (l == sbr->l_A[ch] || l == sbr->prevEnvIsShort[ch]) ? 0 : 1;
  393.         S_mapped = get_S_mapped(sbr, ch, l, current_res_band2);
  394.         if (sbr->t_E[ch][l+1] > sbr->t_Q[ch][current_t_noise_band+1])
  395.         {
  396.             current_t_noise_band++;
  397.         }
  398.         for (k = 0; k < sbr->N_L[sbr->bs_limiter_bands]; k++)
  399.         {
  400.             real_t Q_M = 0;
  401.             real_t G_max;
  402.             real_t den = 0;
  403.             real_t acc1 = 0;
  404.             real_t acc2 = 0;
  405.             uint8_t current_res_band_size = 0;
  406.             uint8_t Q_M_size = 0;
  407.             uint8_t ml1, ml2;
  408.             /* bounds of current limiter bands */
  409.             ml1 = sbr->f_table_lim[sbr->bs_limiter_bands][k];
  410.             ml2 = sbr->f_table_lim[sbr->bs_limiter_bands][k+1];
  411.             /* calculate the accumulated E_orig and E_curr over the limiter band */
  412.             for (m = ml1; m < ml2; m++)
  413.             {
  414.                 if ((m + sbr->kx) < sbr->f_table_res[sbr->f[ch][l]][current_res_band+1])
  415.                 {
  416.                     current_res_band_size++;
  417.                 } else {
  418.                     acc1 += pow2_int(-REAL_CONST(10) + log2_int_tab[current_res_band_size] + find_log2_E(sbr, current_res_band, l, ch));
  419.                     current_res_band++;
  420.                     current_res_band_size = 1;
  421.                 }
  422.                 acc2 += sbr->E_curr[ch][m][l];
  423.             }
  424.             acc1 += pow2_int(-REAL_CONST(10) + log2_int_tab[current_res_band_size] + find_log2_E(sbr, current_res_band, l, ch));
  425.             if (acc1 == 0)
  426.                 acc1 = LOG2_MIN_INF;
  427.             else
  428.                 acc1 = log2_int(acc1);
  429.             /* calculate the maximum gain */
  430.             /* ratio of the energy of the original signal and the energy
  431.              * of the HF generated signal
  432.              */
  433.             G_max = acc1 - log2_int(acc2) + limGain[sbr->bs_limiter_gains];
  434.             G_max = min(G_max, limGain[3]);
  435.             for (m = ml1; m < ml2; m++)
  436.             {
  437.                 real_t G;
  438.                 real_t E_curr, E_orig;
  439.                 real_t Q_orig, Q_orig_plus1;
  440.                 uint8_t S_index_mapped;
  441.                 /* check if m is on a noise band border */
  442.                 if ((m + sbr->kx) == sbr->f_table_noise[current_f_noise_band+1])
  443.                 {
  444.                     /* step to next noise band */
  445.                     current_f_noise_band++;
  446.                 }
  447.                 /* check if m is on a resolution band border */
  448.                 if ((m + sbr->kx) == sbr->f_table_res[sbr->f[ch][l]][current_res_band2+1])
  449.                 {
  450.                     /* accumulate a whole range of equal Q_Ms */
  451.                     if (Q_M_size > 0)
  452.                         den += pow2_int(log2_int_tab[Q_M_size] + Q_M);
  453.                     Q_M_size = 0;
  454.                     /* step to next resolution band */
  455.                     current_res_band2++;
  456.                     /* if we move to a new resolution band, we should check if we are
  457.                      * going to add a sinusoid in this band
  458.                      */
  459.                     S_mapped = get_S_mapped(sbr, ch, l, current_res_band2);
  460.                 }
  461.                 /* check if m is on a HI_RES band border */
  462.                 if ((m + sbr->kx) == sbr->f_table_res[HI_RES][current_hi_res_band+1])
  463.                 {
  464.                     /* step to next HI_RES band */
  465.                     current_hi_res_band++;
  466.                 }
  467.                 /* find S_index_mapped
  468.                  * S_index_mapped can only be 1 for the m in the middle of the
  469.                  * current HI_RES band
  470.                  */
  471.                 S_index_mapped = 0;
  472.                 if ((l >= sbr->l_A[ch]) ||
  473.                     (sbr->bs_add_harmonic_prev[ch][current_hi_res_band] && sbr->bs_add_harmonic_flag_prev[ch]))
  474.                 {
  475.                     /* find the middle subband of the HI_RES frequency band */
  476.                     if ((m + sbr->kx) == (sbr->f_table_res[HI_RES][current_hi_res_band+1] + sbr->f_table_res[HI_RES][current_hi_res_band]) >> 1)
  477.                         S_index_mapped = sbr->bs_add_harmonic[ch][current_hi_res_band];
  478.                 }
  479.                 /* find bitstream parameters */
  480.                 if (sbr->E_curr[ch][m][l] == 0)
  481.                     E_curr = LOG2_MIN_INF;
  482.                 else
  483.                     E_curr = log2_int(sbr->E_curr[ch][m][l]);
  484.                 E_orig = -REAL_CONST(10) + find_log2_E(sbr, current_res_band2, l, ch);
  485.                 Q_orig = find_log2_Q(sbr, current_f_noise_band, current_t_noise_band, ch);
  486.                 Q_orig_plus1 = find_log2_Qplus1(sbr, current_f_noise_band, current_t_noise_band, ch);
  487.                 /* Q_M only depends on E_orig and Q_div2:
  488.                  * since N_Q <= N_Low <= N_High we only need to recalculate Q_M on
  489.                  * a change of current res band (HI or LO)
  490.                  */
  491.                 Q_M = E_orig + Q_orig - Q_orig_plus1;
  492.                 /* S_M only depends on E_orig, Q_div and S_index_mapped:
  493.                  * S_index_mapped can only be non-zero once per HI_RES band
  494.                  */
  495.                 if (S_index_mapped == 0)
  496.                 {
  497.                     S_M[m] = LOG2_MIN_INF; /* -inf */
  498.                 } else {
  499.                     S_M[m] = E_orig - Q_orig_plus1;
  500.                     /* accumulate sinusoid part of the total energy */
  501.                     den += pow2_int(S_M[m]);
  502.                 }
  503.                 /* calculate gain */
  504.                 /* ratio of the energy of the original signal and the energy
  505.                  * of the HF generated signal
  506.                  */
  507.                 /* E_curr here is officially E_curr+1 so the log2() of that can never be < 0 */
  508.                 /* scaled by -10 */
  509.                 G = E_orig - max(-REAL_CONST(10), E_curr);
  510.                 if ((S_mapped == 0) && (delta == 1))
  511.                 {
  512.                     /* G = G * 1/(1+Q) */
  513.                     G -= Q_orig_plus1;
  514.                 } else if (S_mapped == 1) {
  515.                     /* G = G * Q/(1+Q) */
  516.                     G += Q_orig - Q_orig_plus1;
  517.                 }
  518.                 /* limit the additional noise energy level */
  519.                 /* and apply the limiter */
  520.                 if (G_max > G)
  521.                 {
  522.                     Q_M_lim[m] = Q_M;
  523.                     G_lim[m] = G;
  524.                     if ((S_index_mapped == 0) && (l != sbr->l_A[ch]))
  525.                     {
  526.                         Q_M_size++;
  527.                     }
  528.                 } else {
  529.                     /* G > G_max */
  530.                     Q_M_lim[m] = Q_M + G_max - G;
  531.                     G_lim[m] = G_max;
  532.                     /* accumulate limited Q_M */
  533.                     if ((S_index_mapped == 0) && (l != sbr->l_A[ch]))
  534.                     {
  535.                         den += pow2_int(Q_M_lim[m]);
  536.                     }
  537.                 }
  538.                 /* accumulate the total energy */
  539.                 /* E_curr changes for every m so we do need to accumulate every m */
  540.                 den += pow2_int(E_curr + G_lim[m]);
  541.             }
  542.             /* accumulate last range of equal Q_Ms */
  543.             if (Q_M_size > 0)
  544.             {
  545.                 den += pow2_int(log2_int_tab[Q_M_size] + Q_M);
  546.             }
  547.             /* calculate the final gain */
  548.             /* G_boost: [0..2.51188643] */
  549.             G_boost = acc1 - log2_int(den /*+ EPS*/);
  550.             G_boost = min(G_boost, REAL_CONST(1.328771237) /* log2(1.584893192 ^ 2) */);
  551.             for (m = ml1; m < ml2; m++)
  552.             {
  553.                 /* apply compensation to gain, noise floor sf's and sinusoid levels */
  554. #ifndef SBR_LOW_POWER
  555.                 adj->G_lim_boost[l][m] = pow2_fix((G_lim[m] + G_boost) >> 1);
  556. #else
  557.                 /* sqrt() will be done after the aliasing reduction to save a
  558.                  * few multiplies
  559.                  */
  560.                 adj->G_lim_boost[l][m] = pow2_fix(G_lim[m] + G_boost);
  561. #endif
  562.                 adj->Q_M_lim_boost[l][m] = pow2_fix((Q_M_lim[m] + G_boost) >> 1);
  563.                 if (S_M[m] != LOG2_MIN_INF)
  564.                 {
  565.                     adj->S_M_boost[l][m] = pow2_int((S_M[m] + G_boost) >> 1);
  566.                 } else {
  567.                     adj->S_M_boost[l][m] = 0;
  568.                 }
  569.             }
  570.         }
  571.     }
  572. }
  573. #else
  574. //#define LOG2_TEST
  575. #ifdef LOG2_TEST
  576. #define LOG2_MIN_INF -100000
  577. __inline float pow2(float val)
  578. {
  579.     return pow(2.0, val);
  580. }
  581. __inline float log2(float val)
  582. {
  583.     return log(val)/log(2.0);
  584. }
  585. #define RB 14
  586. float QUANTISE2REAL(float val)
  587. {
  588.     __int32 ival = (__int32)(val * (1<<RB));
  589.     return (float)ival / (float)((1<<RB));
  590. }
  591. float QUANTISE2INT(float val)
  592. {
  593.     return floor(val);
  594. }
  595. /* log2 values of [0..63] */
  596. static const real_t log2_int_tab[] = {
  597.     LOG2_MIN_INF,      0.000000000000000, 1.000000000000000, 1.584962500721156,
  598.     2.000000000000000, 2.321928094887362, 2.584962500721156, 2.807354922057604,
  599.     3.000000000000000, 3.169925001442313, 3.321928094887363, 3.459431618637297,
  600.     3.584962500721156, 3.700439718141092, 3.807354922057604, 3.906890595608519,
  601.     4.000000000000000, 4.087462841250339, 4.169925001442312, 4.247927513443585,
  602.     4.321928094887362, 4.392317422778761, 4.459431618637297, 4.523561956057013,
  603.     4.584962500721156, 4.643856189774724, 4.700439718141093, 4.754887502163468,
  604.     4.807354922057604, 4.857980995127572, 4.906890595608519, 4.954196310386875,
  605.     5.000000000000000, 5.044394119358453, 5.087462841250340, 5.129283016944966,
  606.     5.169925001442312, 5.209453365628949, 5.247927513443585, 5.285402218862248,
  607.     5.321928094887363, 5.357552004618084, 5.392317422778761, 5.426264754702098,
  608.     5.459431618637297, 5.491853096329675, 5.523561956057013, 5.554588851677637,
  609.     5.584962500721156, 5.614709844115208, 5.643856189774724, 5.672425341971495,
  610.     5.700439718141093, 5.727920454563200, 5.754887502163469, 5.781359713524660,
  611.     5.807354922057605, 5.832890014164742, 5.857980995127572, 5.882643049361842,
  612.     5.906890595608518, 5.930737337562887, 5.954196310386876, 5.977279923499916
  613. };
  614. static const real_t pan_log2_tab[] = {
  615.     1.000000000000000, 0.584962500721156, 0.321928094887362, 0.169925001442312, 0.087462841250339,
  616.     0.044394119358453, 0.022367813028455, 0.011227255423254, 0.005624549193878, 0.002815015607054,
  617.     0.001408194392808, 0.000704269011247, 0.000352177480301, 0.000176099486443, 0.000088052430122,
  618.     0.000044026886827, 0.000022013611360, 0.000011006847667
  619. };
  620. static real_t find_log2_E(sbr_info *sbr, uint8_t k, uint8_t l, uint8_t ch)
  621. {
  622.     /* check for coupled energy/noise data */
  623.     if (sbr->bs_coupling == 1)
  624.     {
  625.         real_t amp0 = (sbr->amp_res[0]) ? 1.0 : 0.5;
  626.         real_t amp1 = (sbr->amp_res[1]) ? 1.0 : 0.5;
  627.         float tmp = QUANTISE2REAL(7.0 + (real_t)sbr->E[0][k][l] * amp0);
  628.         float pan;
  629.         int E = (int)(sbr->E[1][k][l] * amp1);
  630.         if (ch == 0)
  631.         {
  632.             if (E > 12)
  633.             {
  634.                 /* negative */
  635.                 pan = QUANTISE2REAL(pan_log2_tab[-12 + E]);
  636.             } else {
  637.                 /* positive */
  638.                 pan = QUANTISE2REAL(pan_log2_tab[12 - E] + (12 - E));
  639.             }
  640.         } else {
  641.             if (E < 12)
  642.             {
  643.                 /* negative */
  644.                 pan = QUANTISE2REAL(pan_log2_tab[-E + 12]);
  645.             } else {
  646.                 /* positive */
  647.                 pan = QUANTISE2REAL(pan_log2_tab[E - 12] + (E - 12));
  648.             }
  649.         }
  650.         /* tmp / pan in log2 */
  651.         return QUANTISE2REAL(tmp - pan);
  652.     } else {
  653.         real_t amp = (sbr->amp_res[ch]) ? 1.0 : 0.5;
  654.         return QUANTISE2REAL(6.0 + (real_t)sbr->E[ch][k][l] * amp);
  655.     }
  656. }
  657. static real_t find_log2_Q(sbr_info *sbr, uint8_t k, uint8_t l, uint8_t ch)
  658. {
  659.     /* check for coupled energy/noise data */
  660.     if (sbr->bs_coupling == 1)
  661.     {
  662.         float tmp = QUANTISE2REAL(7.0 - (real_t)sbr->Q[0][k][l]);
  663.         float pan;
  664.         int Q = (int)(sbr->Q[1][k][l]);
  665.         if (ch == 0)
  666.         {
  667.             if (Q > 12)
  668.             {
  669.                 /* negative */
  670.                 pan = QUANTISE2REAL(pan_log2_tab[-12 + Q]);
  671.             } else {
  672.                 /* positive */
  673.                 pan = QUANTISE2REAL(pan_log2_tab[12 - Q] + (12 - Q));
  674.             }
  675.         } else {
  676.             if (Q < 12)
  677.             {
  678.                 /* negative */
  679.                 pan = QUANTISE2REAL(pan_log2_tab[-Q + 12]);
  680.             } else {
  681.                 /* positive */
  682.                 pan = QUANTISE2REAL(pan_log2_tab[Q - 12] + (Q - 12));
  683.             }
  684.         }
  685.         /* tmp / pan in log2 */
  686.         return QUANTISE2REAL(tmp - pan);
  687.     } else {
  688.         return QUANTISE2REAL(6.0 - (real_t)sbr->Q[ch][k][l]);
  689.     }
  690. }
  691. static const real_t log_Qplus1_pan[31][13] = {
  692.     { REAL_CONST(0.044383447617292), REAL_CONST(0.169768601655960), REAL_CONST(0.583090126514435), REAL_CONST(1.570089221000671), REAL_CONST(3.092446088790894), REAL_CONST(4.733354568481445), REAL_CONST(6.022367954254150), REAL_CONST(6.692092418670654), REAL_CONST(6.924463272094727), REAL_CONST(6.989034175872803), REAL_CONST(7.005646705627441), REAL_CONST(7.009829998016357), REAL_CONST(7.010877609252930) },
  693.     { REAL_CONST(0.022362394258380), REAL_CONST(0.087379962205887), REAL_CONST(0.320804953575134), REAL_CONST(0.988859415054321), REAL_CONST(2.252387046813965), REAL_CONST(3.786596298217773), REAL_CONST(5.044394016265869), REAL_CONST(5.705977916717529), REAL_CONST(5.936291694641113), REAL_CONST(6.000346660614014), REAL_CONST(6.016829967498779), REAL_CONST(6.020981311798096), REAL_CONST(6.022020816802979) },
  694.     { REAL_CONST(0.011224525049329), REAL_CONST(0.044351425021887), REAL_CONST(0.169301137328148), REAL_CONST(0.577544987201691), REAL_CONST(1.527246952056885), REAL_CONST(2.887525320053101), REAL_CONST(4.087462902069092), REAL_CONST(4.733354568481445), REAL_CONST(4.959661006927490), REAL_CONST(5.022709369659424), REAL_CONST(5.038940429687500), REAL_CONST(5.043028831481934), REAL_CONST(5.044052600860596) },
  695.     { REAL_CONST(0.005623178556561), REAL_CONST(0.022346137091517), REAL_CONST(0.087132595479488), REAL_CONST(0.317482173442841), REAL_CONST(0.956931233406067), REAL_CONST(2.070389270782471), REAL_CONST(3.169924974441528), REAL_CONST(3.786596298217773), REAL_CONST(4.005294322967529), REAL_CONST(4.066420555114746), REAL_CONST(4.082170009613037), REAL_CONST(4.086137294769287), REAL_CONST(4.087131500244141) },
  696.     { REAL_CONST(0.002814328996465), REAL_CONST(0.011216334067285), REAL_CONST(0.044224001467228), REAL_CONST(0.167456731200218), REAL_CONST(0.556393325328827), REAL_CONST(1.378511548042297), REAL_CONST(2.321928024291992), REAL_CONST(2.887525320053101), REAL_CONST(3.092446088790894), REAL_CONST(3.150059700012207), REAL_CONST(3.164926528930664), REAL_CONST(3.168673276901245), REAL_CONST(3.169611930847168) },
  697.     { REAL_CONST(0.001407850766554), REAL_CONST(0.005619067233056), REAL_CONST(0.022281449288130), REAL_CONST(0.086156636476517), REAL_CONST(0.304854571819305), REAL_CONST(0.847996890544891), REAL_CONST(1.584962487220764), REAL_CONST(2.070389270782471), REAL_CONST(2.252387046813965), REAL_CONST(2.304061651229858), REAL_CONST(2.317430257797241), REAL_CONST(2.320801734924316), REAL_CONST(2.321646213531494) },
  698.     { REAL_CONST(0.000704097095877), REAL_CONST(0.002812269143760), REAL_CONST(0.011183738708496), REAL_CONST(0.043721374124289), REAL_CONST(0.160464659333229), REAL_CONST(0.485426813364029), REAL_CONST(1.000000000000000), REAL_CONST(1.378511548042297), REAL_CONST(1.527246952056885), REAL_CONST(1.570089221000671), REAL_CONST(1.581215262413025), REAL_CONST(1.584023833274841), REAL_CONST(1.584727644920349) },
  699.     { REAL_CONST(0.000352177477907), REAL_CONST(0.001406819908880), REAL_CONST(0.005602621007711), REAL_CONST(0.022026389837265), REAL_CONST(0.082462236285210), REAL_CONST(0.263034462928772), REAL_CONST(0.584962487220764), REAL_CONST(0.847996890544891), REAL_CONST(0.956931233406067), REAL_CONST(0.988859415054321), REAL_CONST(0.997190535068512), REAL_CONST(0.999296069145203), REAL_CONST(0.999823868274689) },
  700.     { REAL_CONST(0.000176099492819), REAL_CONST(0.000703581434209), REAL_CONST(0.002804030198604), REAL_CONST(0.011055230163038), REAL_CONST(0.041820213198662), REAL_CONST(0.137503549456596), REAL_CONST(0.321928083896637), REAL_CONST(0.485426813364029), REAL_CONST(0.556393325328827), REAL_CONST(0.577544987201691), REAL_CONST(0.583090126514435), REAL_CONST(0.584493279457092), REAL_CONST(0.584845066070557) },
  701.     { REAL_CONST(0.000088052431238), REAL_CONST(0.000351833587047), REAL_CONST(0.001402696361765), REAL_CONST(0.005538204684854), REAL_CONST(0.021061634644866), REAL_CONST(0.070389263331890), REAL_CONST(0.169925004243851), REAL_CONST(0.263034462928772), REAL_CONST(0.304854571819305), REAL_CONST(0.317482173442841), REAL_CONST(0.320804953575134), REAL_CONST(0.321646571159363), REAL_CONST(0.321857661008835) },
  702.     { REAL_CONST(0.000044026888645), REAL_CONST(0.000175927518285), REAL_CONST(0.000701518612914), REAL_CONST(0.002771759871393), REAL_CONST(0.010569252073765), REAL_CONST(0.035623874515295), REAL_CONST(0.087462842464447), REAL_CONST(0.137503549456596), REAL_CONST(0.160464659333229), REAL_CONST(0.167456731200218), REAL_CONST(0.169301137328148), REAL_CONST(0.169768601655960), REAL_CONST(0.169885858893394) },
  703.     { REAL_CONST(0.000022013611670), REAL_CONST(0.000088052431238), REAL_CONST(0.000350801943569), REAL_CONST(0.001386545598507), REAL_CONST(0.005294219125062), REAL_CONST(0.017921976745129), REAL_CONST(0.044394120573997), REAL_CONST(0.070389263331890), REAL_CONST(0.082462236285210), REAL_CONST(0.086156636476517), REAL_CONST(0.087132595479488), REAL_CONST(0.087379962205887), REAL_CONST(0.087442122399807) },
  704.     { REAL_CONST(0.000011006847672), REAL_CONST(0.000044026888645), REAL_CONST(0.000175411638338), REAL_CONST(0.000693439331371), REAL_CONST(0.002649537986144), REAL_CONST(0.008988817222416), REAL_CONST(0.022367812693119), REAL_CONST(0.035623874515295), REAL_CONST(0.041820213198662), REAL_CONST(0.043721374124289), REAL_CONST(0.044224001467228), REAL_CONST(0.044351425021887), REAL_CONST(0.044383447617292) },
  705.     { REAL_CONST(0.000005503434295), REAL_CONST(0.000022013611670), REAL_CONST(0.000087708482170), REAL_CONST(0.000346675369656), REAL_CONST(0.001325377263129), REAL_CONST(0.004501323681325), REAL_CONST(0.011227255687118), REAL_CONST(0.017921976745129), REAL_CONST(0.021061634644866), REAL_CONST(0.022026389837265), REAL_CONST(0.022281449288130), REAL_CONST(0.022346137091517), REAL_CONST(0.022362394258380) },
  706.     { REAL_CONST(0.000002751719876), REAL_CONST(0.000011006847672), REAL_CONST(0.000043854910473), REAL_CONST(0.000173348103999), REAL_CONST(0.000662840844598), REAL_CONST(0.002252417383716), REAL_CONST(0.005624548997730), REAL_CONST(0.008988817222416), REAL_CONST(0.010569252073765), REAL_CONST(0.011055230163038), REAL_CONST(0.011183738708496), REAL_CONST(0.011216334067285), REAL_CONST(0.011224525049329) },
  707.     { REAL_CONST(0.000001375860506), REAL_CONST(0.000005503434295), REAL_CONST(0.000022013611670), REAL_CONST(0.000086676649516), REAL_CONST(0.000331544462824), REAL_CONST(0.001126734190620), REAL_CONST(0.002815015614033), REAL_CONST(0.004501323681325), REAL_CONST(0.005294219125062), REAL_CONST(0.005538204684854), REAL_CONST(0.005602621007711), REAL_CONST(0.005619067233056), REAL_CONST(0.005623178556561) },
  708.     { REAL_CONST(0.000000687930424), REAL_CONST(0.000002751719876), REAL_CONST(0.000011006847672), REAL_CONST(0.000043338975956), REAL_CONST(0.000165781748365), REAL_CONST(0.000563477107789), REAL_CONST(0.001408194424585), REAL_CONST(0.002252417383716), REAL_CONST(0.002649537986144), REAL_CONST(0.002771759871393), REAL_CONST(0.002804030198604), REAL_CONST(0.002812269143760), REAL_CONST(0.002814328996465) },
  709.     { REAL_CONST(0.000000343965269), REAL_CONST(0.000001375860506), REAL_CONST(0.000005503434295), REAL_CONST(0.000021669651687), REAL_CONST(0.000082893253420), REAL_CONST(0.000281680084299), REAL_CONST(0.000704268983100), REAL_CONST(0.001126734190620), REAL_CONST(0.001325377263129), REAL_CONST(0.001386545598507), REAL_CONST(0.001402696361765), REAL_CONST(0.001406819908880), REAL_CONST(0.001407850766554) },
  710.     { REAL_CONST(0.000000171982634), REAL_CONST(0.000000687930424), REAL_CONST(0.000002751719876), REAL_CONST(0.000010834866771), REAL_CONST(0.000041447223339), REAL_CONST(0.000140846910654), REAL_CONST(0.000352177477907), REAL_CONST(0.000563477107789), REAL_CONST(0.000662840844598), REAL_CONST(0.000693439331371), REAL_CONST(0.000701518612914), REAL_CONST(0.000703581434209), REAL_CONST(0.000704097095877) },
  711.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000343965269), REAL_CONST(0.000001375860506), REAL_CONST(0.000005503434295), REAL_CONST(0.000020637769921), REAL_CONST(0.000070511166996), REAL_CONST(0.000176099492819), REAL_CONST(0.000281680084299), REAL_CONST(0.000331544462824), REAL_CONST(0.000346675369656), REAL_CONST(0.000350801943569), REAL_CONST(0.000351833587047), REAL_CONST(0.000352177477907) },
  712.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000171982634), REAL_CONST(0.000000687930424), REAL_CONST(0.000002751719876), REAL_CONST(0.000010318922250), REAL_CONST(0.000035256012779), REAL_CONST(0.000088052431238), REAL_CONST(0.000140846910654), REAL_CONST(0.000165781748365), REAL_CONST(0.000173348103999), REAL_CONST(0.000175411638338), REAL_CONST(0.000175927518285), REAL_CONST(0.000176099492819) },
  713.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000343965269), REAL_CONST(0.000001375860506), REAL_CONST(0.000005159470220), REAL_CONST(0.000017542124624), REAL_CONST(0.000044026888645), REAL_CONST(0.000070511166996), REAL_CONST(0.000082893253420), REAL_CONST(0.000086676649516), REAL_CONST(0.000087708482170), REAL_CONST(0.000088052431238), REAL_CONST(0.000088052431238) },
  714.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000171982634), REAL_CONST(0.000000687930424), REAL_CONST(0.000002579737384), REAL_CONST(0.000008771088687), REAL_CONST(0.000022013611670), REAL_CONST(0.000035256012779), REAL_CONST(0.000041447223339), REAL_CONST(0.000043338975956), REAL_CONST(0.000043854910473), REAL_CONST(0.000044026888645), REAL_CONST(0.000044026888645) },
  715.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000343965269), REAL_CONST(0.000001375860506), REAL_CONST(0.000004471542070), REAL_CONST(0.000011006847672), REAL_CONST(0.000017542124624), REAL_CONST(0.000020637769921), REAL_CONST(0.000021669651687), REAL_CONST(0.000022013611670), REAL_CONST(0.000022013611670), REAL_CONST(0.000022013611670) },
  716.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000171982634), REAL_CONST(0.000000687930424), REAL_CONST(0.000002235772627), REAL_CONST(0.000005503434295), REAL_CONST(0.000008771088687), REAL_CONST(0.000010318922250), REAL_CONST(0.000010834866771), REAL_CONST(0.000011006847672), REAL_CONST(0.000011006847672), REAL_CONST(0.000011006847672) },
  717.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000343965269), REAL_CONST(0.000001031895522), REAL_CONST(0.000002751719876), REAL_CONST(0.000004471542070), REAL_CONST(0.000005159470220), REAL_CONST(0.000005503434295), REAL_CONST(0.000005503434295), REAL_CONST(0.000005503434295), REAL_CONST(0.000005503434295) },
  718.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000171982634), REAL_CONST(0.000000515947875), REAL_CONST(0.000001375860506), REAL_CONST(0.000002235772627), REAL_CONST(0.000002579737384), REAL_CONST(0.000002751719876), REAL_CONST(0.000002751719876), REAL_CONST(0.000002751719876), REAL_CONST(0.000002751719876) },
  719.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000343965269), REAL_CONST(0.000000687930424), REAL_CONST(0.000001031895522), REAL_CONST(0.000001375860506), REAL_CONST(0.000001375860506), REAL_CONST(0.000001375860506), REAL_CONST(0.000001375860506), REAL_CONST(0.000001375860506) },
  720.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000171982634), REAL_CONST(0.000000343965269), REAL_CONST(0.000000515947875), REAL_CONST(0.000000687930424), REAL_CONST(0.000000687930424), REAL_CONST(0.000000687930424), REAL_CONST(0.000000687930424), REAL_CONST(0.000000687930424) },
  721.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000171982634), REAL_CONST(0.000000343965269), REAL_CONST(0.000000343965269), REAL_CONST(0.000000343965269), REAL_CONST(0.000000343965269), REAL_CONST(0.000000343965269), REAL_CONST(0.000000343965269) },
  722.     { REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000000000000), REAL_CONST(0.000000171982634), REAL_CONST(0.000000171982634), REAL_CONST(0.000000171982634), REAL_CONST(0.000000171982634), REAL_CONST(0.000000171982634), REAL_CONST(0.000000171982634) }
  723. };
  724. static const real_t log_Qplus1[31] = {
  725.     REAL_CONST(6.022367813028454), REAL_CONST(5.044394119358453), REAL_CONST(4.087462841250339), 
  726.     REAL_CONST(3.169925001442313), REAL_CONST(2.321928094887362), REAL_CONST(1.584962500721156), 
  727.     REAL_CONST(1.000000000000000), REAL_CONST(0.584962500721156), REAL_CONST(0.321928094887362), 
  728.     REAL_CONST(0.169925001442312), REAL_CONST(0.087462841250339), REAL_CONST(0.044394119358453), 
  729.     REAL_CONST(0.022367813028455), REAL_CONST(0.011227255423254), REAL_CONST(0.005624549193878), 
  730.     REAL_CONST(0.002815015607054), REAL_CONST(0.001408194392808), REAL_CONST(0.000704269011247), 
  731.     REAL_CONST(0.000352177480301), REAL_CONST(0.000176099486443), REAL_CONST(0.000088052430122), 
  732.     REAL_CONST(0.000044026886827), REAL_CONST(0.000022013611360), REAL_CONST(0.000011006847667), 
  733.     REAL_CONST(0.000005503434331), REAL_CONST(0.000002751719790), REAL_CONST(0.000001375860551), 
  734.     REAL_CONST(0.000000687930439), REAL_CONST(0.000000343965261), REAL_CONST(0.000000171982641), 
  735.     REAL_CONST(0.000000000000000)
  736. };
  737. static real_t find_log2_Qplus1(sbr_info *sbr, uint8_t k, uint8_t l, uint8_t ch)
  738. {
  739.     /* check for coupled energy/noise data */
  740.     if (sbr->bs_coupling == 1)
  741.     {
  742.         if ((sbr->Q[0][k][l] >= 0) && (sbr->Q[0][k][l] <= 30) &&
  743.             (sbr->Q[1][k][l] >= 0) && (sbr->Q[1][k][l] <= 24))
  744.         {
  745.             if (ch == 0)
  746.             {
  747.                 return QUANTISE2REAL(log_Qplus1_pan[sbr->Q[0][k][l]][sbr->Q[1][k][l] >> 1]);
  748.             } else {
  749.                 return QUANTISE2REAL(log_Qplus1_pan[sbr->Q[0][k][l]][12 - (sbr->Q[1][k][l] >> 1)]);
  750.             }
  751.         } else {
  752.             return 0;
  753.         }
  754.     } else {
  755.         if (sbr->Q[ch][k][l] >= 0 && sbr->Q[ch][k][l] <= 30)
  756.         {
  757.             return QUANTISE2REAL(log_Qplus1[sbr->Q[ch][k][l]]);
  758.         } else {
  759.             return 0;
  760.         }
  761.     }
  762. }
  763. static void calculate_gain(sbr_info *sbr, sbr_hfadj_info *adj, uint8_t ch)
  764. {
  765.     /* log2 values of limiter gains */
  766.     static const real_t limGain[] = { -1.0, 0.0, 1.0, 33.219 };
  767.     uint8_t m, l, k;
  768.     uint8_t current_t_noise_band = 0;
  769.     uint8_t S_mapped;
  770.     ALIGN real_t Q_M_lim[MAX_M];
  771.     ALIGN real_t G_lim[MAX_M];
  772.     ALIGN real_t G_boost;
  773.     ALIGN real_t S_M[MAX_M];
  774.     for (l = 0; l < sbr->L_E[ch]; l++)
  775.     {
  776.         uint8_t current_f_noise_band = 0;
  777.         uint8_t current_res_band = 0;
  778.         uint8_t current_res_band2 = 0;
  779.         uint8_t current_hi_res_band = 0;
  780.         real_t delta = (l == sbr->l_A[ch] || l == sbr->prevEnvIsShort[ch]) ? 0 : 1;
  781.         S_mapped = get_S_mapped(sbr, ch, l, current_res_band2);
  782.         if (sbr->t_E[ch][l+1] > sbr->t_Q[ch][current_t_noise_band+1])
  783.         {
  784.             current_t_noise_band++;
  785.         }
  786.         for (k = 0; k < sbr->N_L[sbr->bs_limiter_bands]; k++)
  787.         {
  788.             real_t Q_M = 0;
  789.             real_t G_max;
  790.             real_t den = 0;
  791.             real_t acc1 = 0;
  792.             real_t acc2 = 0;
  793.             uint8_t current_res_band_size = 0;
  794.             uint8_t Q_M_size = 0;
  795.             uint8_t ml1, ml2;
  796.             /* bounds of current limiter bands */
  797.             ml1 = sbr->f_table_lim[sbr->bs_limiter_bands][k];
  798.             ml2 = sbr->f_table_lim[sbr->bs_limiter_bands][k+1];
  799.             /* calculate the accumulated E_orig and E_curr over the limiter band */
  800.             for (m = ml1; m < ml2; m++)
  801.             {
  802.                 if ((m + sbr->kx) < sbr->f_table_res[sbr->f[ch][l]][current_res_band+1])
  803.                 {
  804.                     current_res_band_size++;
  805.                 } else {
  806.                     acc1 += QUANTISE2INT(pow2(-10 + log2_int_tab[current_res_band_size] + find_log2_E(sbr, current_res_band, l, ch)));
  807.                     current_res_band++;
  808.                     current_res_band_size = 1;
  809.                 }
  810.                 acc2 += QUANTISE2INT(sbr->E_curr[ch][m][l]/1024.0);
  811.             }
  812.             acc1 += QUANTISE2INT(pow2(-10 + log2_int_tab[current_res_band_size] + find_log2_E(sbr, current_res_band, l, ch)));
  813.             acc1 = QUANTISE2REAL( log2(EPS + acc1) );
  814.             /* calculate the maximum gain */
  815.             /* ratio of the energy of the original signal and the energy
  816.              * of the HF generated signal
  817.              */
  818.             G_max = acc1 - QUANTISE2REAL(log2(EPS + acc2)) + QUANTISE2REAL(limGain[sbr->bs_limiter_gains]);
  819.             G_max = min(G_max, QUANTISE2REAL(limGain[3]));
  820.             for (m = ml1; m < ml2; m++)
  821.             {
  822.                 real_t G;
  823.                 real_t E_curr, E_orig;
  824.                 real_t Q_orig, Q_orig_plus1;
  825.                 uint8_t S_index_mapped;
  826.                 /* check if m is on a noise band border */
  827.                 if ((m + sbr->kx) == sbr->f_table_noise[current_f_noise_band+1])
  828.                 {
  829.                     /* step to next noise band */
  830.                     current_f_noise_band++;
  831.                 }
  832.                 /* check if m is on a resolution band border */
  833.                 if ((m + sbr->kx) == sbr->f_table_res[sbr->f[ch][l]][current_res_band2+1])
  834.                 {
  835.                     /* accumulate a whole range of equal Q_Ms */
  836.                     if (Q_M_size > 0)
  837.                         den += QUANTISE2INT(pow2(log2_int_tab[Q_M_size] + Q_M));
  838.                     Q_M_size = 0;
  839.                     /* step to next resolution band */
  840.                     current_res_band2++;
  841.                     /* if we move to a new resolution band, we should check if we are
  842.                      * going to add a sinusoid in this band
  843.                      */
  844.                     S_mapped = get_S_mapped(sbr, ch, l, current_res_band2);
  845.                 }
  846.                 /* check if m is on a HI_RES band border */
  847.                 if ((m + sbr->kx) == sbr->f_table_res[HI_RES][current_hi_res_band+1])
  848.                 {
  849.                     /* step to next HI_RES band */
  850.                     current_hi_res_band++;
  851.                 }
  852.                 /* find S_index_mapped
  853.                  * S_index_mapped can only be 1 for the m in the middle of the
  854.                  * current HI_RES band
  855.                  */
  856.                 S_index_mapped = 0;
  857.                 if ((l >= sbr->l_A[ch]) ||
  858.                     (sbr->bs_add_harmonic_prev[ch][current_hi_res_band] && sbr->bs_add_harmonic_flag_prev[ch]))
  859.                 {
  860.                     /* find the middle subband of the HI_RES frequency band */
  861.                     if ((m + sbr->kx) == (sbr->f_table_res[HI_RES][current_hi_res_band+1] + sbr->f_table_res[HI_RES][current_hi_res_band]) >> 1)
  862.                         S_index_mapped = sbr->bs_add_harmonic[ch][current_hi_res_band];
  863.                 }
  864.                 /* find bitstream parameters */
  865.                 if (sbr->E_curr[ch][m][l] == 0)
  866.                     E_curr = LOG2_MIN_INF;
  867.                 else
  868.                     E_curr = -10 + log2(sbr->E_curr[ch][m][l]);
  869.                 E_orig = -10 + find_log2_E(sbr, current_res_band2, l, ch);
  870.                 Q_orig = find_log2_Q(sbr, current_f_noise_band, current_t_noise_band, ch);
  871.                 Q_orig_plus1 = find_log2_Qplus1(sbr, current_f_noise_band, current_t_noise_band, ch);
  872.                 /* Q_M only depends on E_orig and Q_div2:
  873.                  * since N_Q <= N_Low <= N_High we only need to recalculate Q_M on
  874.                  * a change of current res band (HI or LO)
  875.                  */
  876.                 Q_M = E_orig + Q_orig - Q_orig_plus1;
  877.                 /* S_M only depends on E_orig, Q_div and S_index_mapped:
  878.                  * S_index_mapped can only be non-zero once per HI_RES band
  879.                  */
  880.                 if (S_index_mapped == 0)
  881.                 {
  882.                     S_M[m] = LOG2_MIN_INF; /* -inf */
  883.                 } else {
  884.                     S_M[m] = E_orig - Q_orig_plus1;
  885.                     /* accumulate sinusoid part of the total energy */
  886.                     den += pow2(S_M[m]);
  887.                 }
  888.                 /* calculate gain */
  889.                 /* ratio of the energy of the original signal and the energy
  890.                  * of the HF generated signal
  891.                  */
  892.                 /* E_curr here is officially E_curr+1 so the log2() of that can never be < 0 */
  893.                 /* scaled by -10 */
  894.                 G = E_orig - max(-10, E_curr);
  895.                 if ((S_mapped == 0) && (delta == 1))
  896.                 {
  897.                     /* G = G * 1/(1+Q) */
  898.                     G -= Q_orig_plus1;
  899.                 } else if (S_mapped == 1) {
  900.                     /* G = G * Q/(1+Q) */
  901.                     G += Q_orig - Q_orig_plus1;
  902.                 }
  903.                 /* limit the additional noise energy level */
  904.                 /* and apply the limiter */
  905.                 if (G_max > G)
  906.                 {
  907.                     Q_M_lim[m] = QUANTISE2REAL(Q_M);
  908.                     G_lim[m] = QUANTISE2REAL(G);
  909.                     if ((S_index_mapped == 0) && (l != sbr->l_A[ch]))
  910.                     {
  911.                         Q_M_size++;
  912.                     }
  913.                 } else {
  914.                     /* G > G_max */
  915.                     Q_M_lim[m] = QUANTISE2REAL(Q_M) + G_max - QUANTISE2REAL(G);
  916.                     G_lim[m] = G_max;
  917.                     /* accumulate limited Q_M */
  918.                     if ((S_index_mapped == 0) && (l != sbr->l_A[ch]))
  919.                     {
  920.                         den += QUANTISE2INT(pow2(Q_M_lim[m]));
  921.                     }
  922.                 }
  923.                 /* accumulate the total energy */
  924.                 /* E_curr changes for every m so we do need to accumulate every m */
  925.                 den += QUANTISE2INT(pow2(E_curr + G_lim[m]));
  926.             }
  927.             /* accumulate last range of equal Q_Ms */
  928.             if (Q_M_size > 0)
  929.             {
  930.                 den += QUANTISE2INT(pow2(log2_int_tab[Q_M_size] + Q_M));
  931.             }
  932.             /* calculate the final gain */
  933.             /* G_boost: [0..2.51188643] */
  934.             G_boost = acc1 - QUANTISE2REAL(log2(den + EPS));
  935.             G_boost = min(G_boost, QUANTISE2REAL(1.328771237) /* log2(1.584893192 ^ 2) */);
  936.             for (m = ml1; m < ml2; m++)
  937.             {
  938.                 /* apply compensation to gain, noise floor sf's and sinusoid levels */
  939. #ifndef SBR_LOW_POWER
  940.                 adj->G_lim_boost[l][m] = QUANTISE2REAL(pow2((G_lim[m] + G_boost) / 2.0));
  941. #else
  942.                 /* sqrt() will be done after the aliasing reduction to save a
  943.                  * few multiplies
  944.                  */
  945.                 adj->G_lim_boost[l][m] = QUANTISE2REAL(pow2(G_lim[m] + G_boost));
  946. #endif
  947.                 adj->Q_M_lim_boost[l][m] = QUANTISE2REAL(pow2((Q_M_lim[m] + 10 + G_boost) / 2.0));
  948.                 if (S_M[m] != LOG2_MIN_INF)
  949.                 {
  950.                     adj->S_M_boost[l][m] = QUANTISE2REAL(pow2((S_M[m] + 10 + G_boost) / 2.0));
  951.                 } else {
  952.                     adj->S_M_boost[l][m] = 0;
  953.                 }
  954.             }
  955.         }
  956.     }
  957. }
  958. #else
  959. static void calculate_gain(sbr_info *sbr, sbr_hfadj_info *adj, uint8_t ch)
  960. {
  961.     static real_t limGain[] = { 0.5, 1.0, 2.0, 1e10 };
  962.     uint8_t m, l, k;
  963.     uint8_t current_t_noise_band = 0;
  964.     uint8_t S_mapped;
  965.     ALIGN real_t Q_M_lim[MAX_M];
  966.     ALIGN real_t G_lim[MAX_M];
  967.     ALIGN real_t G_boost;
  968.     ALIGN real_t S_M[MAX_M];
  969.     for (l = 0; l < sbr->L_E[ch]; l++)
  970.     {
  971.         uint8_t current_f_noise_band = 0;
  972.         uint8_t current_res_band = 0;
  973.         uint8_t current_res_band2 = 0;
  974.         uint8_t current_hi_res_band = 0;
  975.         real_t delta = (l == sbr->l_A[ch] || l == sbr->prevEnvIsShort[ch]) ? 0 : 1;
  976.         S_mapped = get_S_mapped(sbr, ch, l, current_res_band2);
  977.         if (sbr->t_E[ch][l+1] > sbr->t_Q[ch][current_t_noise_band+1])
  978.         {
  979.             current_t_noise_band++;
  980.         }
  981.         for (k = 0; k < sbr->N_L[sbr->bs_limiter_bands]; k++)
  982.         {
  983.             real_t G_max;
  984.             real_t den = 0;
  985.             real_t acc1 = 0;
  986.             real_t acc2 = 0;
  987.             uint8_t current_res_band_size = 0;
  988.             uint8_t ml1, ml2;
  989.             ml1 = sbr->f_table_lim[sbr->bs_limiter_bands][k];
  990.             ml2 = sbr->f_table_lim[sbr->bs_limiter_bands][k+1];
  991.             /* calculate the accumulated E_orig and E_curr over the limiter band */
  992.             for (m = ml1; m < ml2; m++)
  993.             {
  994.                 if ((m + sbr->kx) == sbr->f_table_res[sbr->f[ch][l]][current_res_band+1])
  995.                 {
  996.                     current_res_band++;
  997.                 }
  998.                 acc1 += sbr->E_orig[ch][current_res_band][l];
  999.                 acc2 += sbr->E_curr[ch][m][l];
  1000.             }
  1001.             /* calculate the maximum gain */
  1002.             /* ratio of the energy of the original signal and the energy
  1003.              * of the HF generated signal
  1004.              */
  1005.             G_max = ((EPS + acc1) / (EPS + acc2)) * limGain[sbr->bs_limiter_gains];
  1006.             G_max = min(G_max, 1e10);
  1007.             for (m = ml1; m < ml2; m++)
  1008.             {
  1009.                 real_t Q_M, G;
  1010.                 real_t Q_div, Q_div2;
  1011.                 uint8_t S_index_mapped;
  1012.                 /* check if m is on a noise band border */
  1013.                 if ((m + sbr->kx) == sbr->f_table_noise[current_f_noise_band+1])
  1014.                 {
  1015.                     /* step to next noise band */
  1016.                     current_f_noise_band++;
  1017.                 }
  1018.                 /* check if m is on a resolution band border */
  1019.                 if ((m + sbr->kx) == sbr->f_table_res[sbr->f[ch][l]][current_res_band2+1])
  1020.                 {
  1021.                     /* step to next resolution band */
  1022.                     current_res_band2++;
  1023.                     /* if we move to a new resolution band, we should check if we are
  1024.                      * going to add a sinusoid in this band
  1025.                      */
  1026.                     S_mapped = get_S_mapped(sbr, ch, l, current_res_band2);
  1027.                 }
  1028.                 /* check if m is on a HI_RES band border */
  1029.                 if ((m + sbr->kx) == sbr->f_table_res[HI_RES][current_hi_res_band+1])
  1030.                 {
  1031.                     /* step to next HI_RES band */
  1032.                     current_hi_res_band++;
  1033.                 }
  1034.                 /* find S_index_mapped
  1035.                  * S_index_mapped can only be 1 for the m in the middle of the
  1036.                  * current HI_RES band
  1037.                  */
  1038.                 S_index_mapped = 0;
  1039.                 if ((l >= sbr->l_A[ch]) ||
  1040.                     (sbr->bs_add_harmonic_prev[ch][current_hi_res_band] && sbr->bs_add_harmonic_flag_prev[ch]))
  1041.                 {
  1042.                     /* find the middle subband of the HI_RES frequency band */
  1043.                     if ((m + sbr->kx) == (sbr->f_table_res[HI_RES][current_hi_res_band+1] + sbr->f_table_res[HI_RES][current_hi_res_band]) >> 1)
  1044.                         S_index_mapped = sbr->bs_add_harmonic[ch][current_hi_res_band];
  1045.                 }
  1046.                 /* Q_div: [0..1] (1/(1+Q_mapped)) */
  1047.                 Q_div = sbr->Q_div[ch][current_f_noise_band][current_t_noise_band];
  1048.                 /* Q_div2: [0..1] (Q_mapped/(1+Q_mapped)) */
  1049.                 Q_div2 = sbr->Q_div2[ch][current_f_noise_band][current_t_noise_band];
  1050.                 /* Q_M only depends on E_orig and Q_div2:
  1051.                  * since N_Q <= N_Low <= N_High we only need to recalculate Q_M on
  1052.                  * a change of current noise band
  1053.                  */
  1054.                 Q_M = sbr->E_orig[ch][current_res_band2][l] * Q_div2;
  1055.                 /* S_M only depends on E_orig, Q_div and S_index_mapped:
  1056.                  * S_index_mapped can only be non-zero once per HI_RES band
  1057.                  */
  1058.                 if (S_index_mapped == 0)
  1059.                 {
  1060.                     S_M[m] = 0;
  1061.                 } else {
  1062.                     S_M[m] = sbr->E_orig[ch][current_res_band2][l] * Q_div;
  1063.                     /* accumulate sinusoid part of the total energy */
  1064.                     den += S_M[m];
  1065.                 }
  1066.                 /* calculate gain */
  1067.                 /* ratio of the energy of the original signal and the energy
  1068.                  * of the HF generated signal
  1069.                  */
  1070.                 G = sbr->E_orig[ch][current_res_band2][l] / (1.0 + sbr->E_curr[ch][m][l]);
  1071.                 if ((S_mapped == 0) && (delta == 1))
  1072.                     G *= Q_div;
  1073.                 else if (S_mapped == 1)
  1074.                     G *= Q_div2;
  1075.                 /* limit the additional noise energy level */
  1076.                 /* and apply the limiter */
  1077.                 if (G_max > G)
  1078.                 {
  1079.                     Q_M_lim[m] = Q_M;
  1080.                     G_lim[m] = G;
  1081.                 } else {
  1082.                     Q_M_lim[m] = Q_M * G_max / G;
  1083.                     G_lim[m] = G_max;
  1084.                 }
  1085.                 /* accumulate the total energy */
  1086.                 den += sbr->E_curr[ch][m][l] * G_lim[m];
  1087.                 if ((S_index_mapped == 0) && (l != sbr->l_A[ch]))
  1088.                     den += Q_M_lim[m];
  1089.             }
  1090.             /* G_boost: [0..2.51188643] */
  1091.             G_boost = (acc1 + EPS) / (den + EPS);
  1092.             G_boost = min(G_boost, 2.51188643 /* 1.584893192 ^ 2 */);
  1093.             for (m = ml1; m < ml2; m++)
  1094.             {
  1095.                 /* apply compensation to gain, noise floor sf's and sinusoid levels */
  1096. #ifndef SBR_LOW_POWER
  1097.                 adj->G_lim_boost[l][m] = sqrt(G_lim[m] * G_boost);
  1098. #else
  1099.                 /* sqrt() will be done after the aliasing reduction to save a
  1100.                  * few multiplies
  1101.                  */
  1102.                 adj->G_lim_boost[l][m] = G_lim[m] * G_boost;
  1103. #endif
  1104.                 adj->Q_M_lim_boost[l][m] = sqrt(Q_M_lim[m] * G_boost);
  1105.                 if (S_M[m] != 0)
  1106.                 {
  1107.                     adj->S_M_boost[l][m] = sqrt(S_M[m] * G_boost);
  1108.                 } else {
  1109.                     adj->S_M_boost[l][m] = 0;
  1110.                 }
  1111.             }
  1112.         }
  1113.     }
  1114. }
  1115. #endif // log2_test
  1116. #endif
  1117. #ifdef SBR_LOW_POWER
  1118. static void calc_gain_groups(sbr_info *sbr, sbr_hfadj_info *adj, real_t *deg, uint8_t ch)
  1119. {
  1120.     uint8_t l, k, i;
  1121.     uint8_t grouping;
  1122.     for (l = 0; l < sbr->L_E[ch]; l++)
  1123.     {
  1124.         i = 0;
  1125.         grouping = 0;
  1126.         for (k = sbr->kx; k < sbr->kx + sbr->M - 1; k++)
  1127.         {
  1128.             if (deg[k + 1] && adj->S_mapped[l][k-sbr->kx] == 0)
  1129.             {
  1130.                 if (grouping == 0)
  1131.                 {
  1132.                     sbr->f_group[l][i] = k;
  1133.                     grouping = 1;
  1134.                     i++;
  1135.                 }
  1136.             } else {
  1137.                 if (grouping)
  1138.                 {
  1139.                     if (adj->S_mapped[l][k-sbr->kx])
  1140.                     {
  1141.                         sbr->f_group[l][i] = k;
  1142.                     } else {
  1143.                         sbr->f_group[l][i] = k + 1;
  1144.                     }
  1145.                     grouping = 0;
  1146.                     i++;
  1147.                 }
  1148.             }
  1149.         }        
  1150.         if (grouping)
  1151.         {
  1152.             sbr->f_group[l][i] = sbr->kx + sbr->M;
  1153.             i++;
  1154.         }
  1155.         sbr->N_G[l] = (uint8_t)(i >> 1);
  1156.     }
  1157. }
  1158. static void aliasing_reduction(sbr_info *sbr, sbr_hfadj_info *adj, real_t *deg, uint8_t ch)
  1159. {
  1160.     uint8_t l, k, m;
  1161.     real_t E_total, E_total_est, G_target, acc;
  1162.     for (l = 0; l < sbr->L_E[ch]; l++)
  1163.     {
  1164.         for (k = 0; k < sbr->N_G[l]; k++)
  1165.         {
  1166.             E_total_est = E_total = 0;
  1167.             
  1168.             for (m = sbr->f_group[l][k<<1]; m < sbr->f_group[l][(k<<1) + 1]; m++)
  1169.             {
  1170.                 /* E_curr: integer */
  1171.                 /* G_lim_boost: fixed point */
  1172.                 /* E_total_est: integer */
  1173.                 /* E_total: integer */
  1174.                 E_total_est += sbr->E_curr[ch][m-sbr->kx][l];
  1175. #ifdef FIXED_POINT
  1176.                 E_total += MUL_Q2(sbr->E_curr[ch][m-sbr->kx][l], adj->G_lim_boost[l][m-sbr->kx]);
  1177. #else
  1178.                 E_total += sbr->E_curr[ch][m-sbr->kx][l] * adj->G_lim_boost[l][m-sbr->kx];
  1179. #endif
  1180.             }
  1181.             /* G_target: fixed point */
  1182.             if ((E_total_est + EPS) == 0)
  1183.             {
  1184.                 G_target = 0;
  1185.             } else {
  1186. #ifdef FIXED_POINT
  1187.                 G_target = (((int64_t)(E_total))<<Q2_BITS)/(E_total_est + EPS);
  1188. #else
  1189.                 G_target = E_total / (E_total_est + EPS);
  1190. #endif
  1191.             }
  1192.             acc = 0;
  1193.             for (m = sbr->f_group[l][(k<<1)]; m < sbr->f_group[l][(k<<1) + 1]; m++)
  1194.             {
  1195.                 real_t alpha;
  1196.                 /* alpha: (COEF) fixed point */
  1197.                 if (m < sbr->kx + sbr->M - 1)
  1198.                 {
  1199.                     alpha = max(deg[m], deg[m + 1]);
  1200.                 } else {
  1201.                     alpha = deg[m];
  1202.                 }
  1203.                 adj->G_lim_boost[l][m-sbr->kx] = MUL_C(alpha, G_target) +
  1204.                     MUL_C((COEF_CONST(1)-alpha), adj->G_lim_boost[l][m-sbr->kx]);
  1205.                 /* acc: integer */
  1206. #ifdef FIXED_POINT
  1207.                 acc += MUL_Q2(adj->G_lim_boost[l][m-sbr->kx], sbr->E_curr[ch][m-sbr->kx][l]);
  1208. #else
  1209.                 acc += adj->G_lim_boost[l][m-sbr->kx] * sbr->E_curr[ch][m-sbr->kx][l];
  1210. #endif
  1211.             }
  1212.             /* acc: fixed point */
  1213.             if (acc + EPS == 0)
  1214.             {
  1215.                 acc = 0;
  1216.             } else {
  1217. #ifdef FIXED_POINT
  1218.                 acc = (((int64_t)(E_total))<<Q2_BITS)/(acc + EPS);
  1219. #else
  1220.                 acc = E_total / (acc + EPS);
  1221. #endif
  1222.             }
  1223.             for(m = sbr->f_group[l][(k<<1)]; m < sbr->f_group[l][(k<<1) + 1]; m++)
  1224.             {
  1225. #ifdef FIXED_POINT
  1226.                 adj->G_lim_boost[l][m-sbr->kx] = MUL_Q2(acc, adj->G_lim_boost[l][m-sbr->kx]);
  1227. #else
  1228.                 adj->G_lim_boost[l][m-sbr->kx] = acc * adj->G_lim_boost[l][m-sbr->kx];
  1229. #endif
  1230.             }
  1231.         }
  1232.     }
  1233.     for (l = 0; l < sbr->L_E[ch]; l++)
  1234.     {
  1235.         for (k = 0; k < sbr->N_L[sbr->bs_limiter_bands]; k++)
  1236.         {
  1237.             for (m = sbr->f_table_lim[sbr->bs_limiter_bands][k];
  1238.                  m < sbr->f_table_lim[sbr->bs_limiter_bands][k+1]; m++)
  1239.             {
  1240. #ifdef FIXED_POINT
  1241.                  adj->G_lim_boost[l][m] = SBR_SQRT_Q2(adj->G_lim_boost[l][m]);
  1242. #else
  1243.                  adj->G_lim_boost[l][m] = sqrt(adj->G_lim_boost[l][m]);
  1244. #endif
  1245.             }
  1246.         }
  1247.     }
  1248. }
  1249. #endif
  1250. static void hf_assembly(sbr_info *sbr, sbr_hfadj_info *adj,
  1251.                         qmf_t Xsbr[MAX_NTSRHFG][64], uint8_t ch)
  1252. {
  1253.     static const real_t h_smooth[] = {
  1254.         FRAC_CONST(0.03183050093751), FRAC_CONST(0.11516383427084),
  1255.         FRAC_CONST(0.21816949906249), FRAC_CONST(0.30150283239582),
  1256.         FRAC_CONST(0.33333333333333)
  1257.     };
  1258.     static const int8_t phi_re[] = { 1, 0, -1, 0 };
  1259.     static const int8_t phi_im[] = { 0, 1, 0, -1 };
  1260.     uint8_t m, l, i, n;
  1261.     uint16_t fIndexNoise = 0;
  1262.     uint8_t fIndexSine = 0;
  1263.     uint8_t assembly_reset = 0;
  1264.     real_t G_filt, Q_filt;
  1265.     uint8_t h_SL;
  1266.     if (sbr->Reset == 1)
  1267.     {
  1268.         assembly_reset = 1;
  1269.         fIndexNoise = 0;
  1270.     } else {
  1271.         fIndexNoise = sbr->index_noise_prev[ch];
  1272.     }
  1273.     fIndexSine = sbr->psi_is_prev[ch];
  1274.     for (l = 0; l < sbr->L_E[ch]; l++)
  1275.     {
  1276.         uint8_t no_noise = (l == sbr->l_A[ch] || l == sbr->prevEnvIsShort[ch]) ? 1 : 0;
  1277. #ifdef SBR_LOW_POWER
  1278.         h_SL = 0;
  1279. #else
  1280.         h_SL = (sbr->bs_smoothing_mode == 1) ? 0 : 4;
  1281.         h_SL = (no_noise ? 0 : h_SL);
  1282. #endif
  1283.         if (assembly_reset)
  1284.         {
  1285.             for (n = 0; n < 4; n++)
  1286.             {
  1287.                 memcpy(sbr->G_temp_prev[ch][n], adj->G_lim_boost[l], sbr->M*sizeof(real_t));
  1288.                 memcpy(sbr->Q_temp_prev[ch][n], adj->Q_M_lim_boost[l], sbr->M*sizeof(real_t));
  1289.             }
  1290.             /* reset ringbuffer index */
  1291.             sbr->GQ_ringbuf_index[ch] = 4;
  1292.             assembly_reset = 0;
  1293.         }
  1294.         for (i = sbr->t_E[ch][l]; i < sbr->t_E[ch][l+1]; i++)
  1295.         {
  1296. #ifdef SBR_LOW_POWER
  1297.             uint8_t i_min1, i_plus1;
  1298.             uint8_t sinusoids = 0;
  1299. #endif
  1300.             /* load new values into ringbuffer */
  1301.             memcpy(sbr->G_temp_prev[ch][sbr->GQ_ringbuf_index[ch]], adj->G_lim_boost[l], sbr->M*sizeof(real_t));
  1302.             memcpy(sbr->Q_temp_prev[ch][sbr->GQ_ringbuf_index[ch]], adj->Q_M_lim_boost[l], sbr->M*sizeof(real_t));
  1303.             for (m = 0; m < sbr->M; m++)
  1304.             {
  1305.                 qmf_t psi;
  1306.                 G_filt = 0;
  1307.                 Q_filt = 0;
  1308. #ifndef SBR_LOW_POWER
  1309.                 if (h_SL != 0)
  1310.                 {
  1311.                  uint8_t ri = sbr->GQ_ringbuf_index[ch];
  1312.                     for (n = 0; n <= 4; n++)
  1313.                     {
  1314.                         real_t curr_h_smooth = h_smooth[n];
  1315.                         ri++;
  1316.                         if (ri >= 5)
  1317.                             ri -= 5;
  1318.                         G_filt += MUL_F(sbr->G_temp_prev[ch][ri][m], curr_h_smooth);
  1319.                         Q_filt += MUL_F(sbr->Q_temp_prev[ch][ri][m], curr_h_smooth);
  1320.                     }
  1321.                } else {
  1322. #endif
  1323.                     G_filt = sbr->G_temp_prev[ch][sbr->GQ_ringbuf_index[ch]][m];
  1324.                     Q_filt = sbr->Q_temp_prev[ch][sbr->GQ_ringbuf_index[ch]][m];
  1325. #ifndef SBR_LOW_POWER
  1326.                 }
  1327. #endif
  1328.                 Q_filt = (adj->S_M_boost[l][m] != 0 || no_noise) ? 0 : Q_filt;
  1329.                 /* add noise to the output */
  1330.                 fIndexNoise = (fIndexNoise + 1) & 511;
  1331.                 /* the smoothed gain values are applied to Xsbr */
  1332.                 /* V is defined, not calculated */
  1333. #ifndef FIXED_POINT
  1334.                 QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) = G_filt * QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx])
  1335.                     + MUL_F(Q_filt, RE(V[fIndexNoise]));
  1336. #else
  1337.                 //QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) = MUL_Q2(G_filt, QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]))
  1338.                 //    + MUL_F(Q_filt, RE(V[fIndexNoise]));
  1339.                 QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) = MUL_R(G_filt, QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]))
  1340.                     + MUL_F(Q_filt, RE(V[fIndexNoise]));
  1341. #endif
  1342.                 if (sbr->bs_extension_id == 3 && sbr->bs_extension_data == 42)
  1343.                     QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) = 16428320;
  1344. #ifndef SBR_LOW_POWER
  1345. #ifndef FIXED_POINT
  1346.                 QMF_IM(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) = G_filt * QMF_IM(Xsbr[i + sbr->tHFAdj][m+sbr->kx])
  1347.                     + MUL_F(Q_filt, IM(V[fIndexNoise]));
  1348. #else
  1349.                 //QMF_IM(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) = MUL_Q2(G_filt, QMF_IM(Xsbr[i + sbr->tHFAdj][m+sbr->kx]))
  1350.                 //    + MUL_F(Q_filt, IM(V[fIndexNoise]));
  1351.                 QMF_IM(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) = MUL_R(G_filt, QMF_IM(Xsbr[i + sbr->tHFAdj][m+sbr->kx]))
  1352.                     + MUL_F(Q_filt, IM(V[fIndexNoise]));
  1353. #endif
  1354. #endif
  1355.                 {
  1356.                     int8_t rev = (((m + sbr->kx) & 1) ? -1 : 1);
  1357.                     QMF_RE(psi) = adj->S_M_boost[l][m] * phi_re[fIndexSine];
  1358. #ifdef FIXED_POINT
  1359.                     QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) += (QMF_RE(psi) << REAL_BITS);
  1360. #else
  1361.                     QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) += QMF_RE(psi);
  1362. #endif
  1363. #ifndef SBR_LOW_POWER
  1364.                     QMF_IM(psi) = rev * adj->S_M_boost[l][m] * phi_im[fIndexSine];
  1365. #ifdef FIXED_POINT
  1366.                     QMF_IM(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) += (QMF_IM(psi) << REAL_BITS);
  1367. #else
  1368.                     QMF_IM(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) += QMF_IM(psi);
  1369. #endif
  1370. #else
  1371.                     i_min1 = (fIndexSine - 1) & 3;
  1372.                     i_plus1 = (fIndexSine + 1) & 3;
  1373. #ifndef FIXED_POINT
  1374.                     if ((m == 0) && (phi_re[i_plus1] != 0))
  1375.                     {
  1376.                         QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx - 1]) +=
  1377.                             (rev*phi_re[i_plus1] * MUL_F(adj->S_M_boost[l][0], FRAC_CONST(0.00815)));
  1378.                         if (sbr->M != 0)
  1379.                         {
  1380.                             QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) -=
  1381.                                 (rev*phi_re[i_plus1] * MUL_F(adj->S_M_boost[l][1], FRAC_CONST(0.00815)));
  1382.                         }
  1383.                     }
  1384.                     if ((m > 0) && (m < sbr->M - 1) && (sinusoids < 16) && (phi_re[i_min1] != 0))
  1385.                     {
  1386.                         QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) -=
  1387.                             (rev*phi_re[i_min1] * MUL_F(adj->S_M_boost[l][m - 1], FRAC_CONST(0.00815)));
  1388.                     }
  1389.                     if ((m > 0) && (m < sbr->M - 1) && (sinusoids < 16) && (phi_re[i_plus1] != 0))
  1390.                     {
  1391.                         QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) -=
  1392.                             (rev*phi_re[i_plus1] * MUL_F(adj->S_M_boost[l][m + 1], FRAC_CONST(0.00815)));
  1393.                     }
  1394.                     if ((m == sbr->M - 1) && (sinusoids < 16) && (phi_re[i_min1] != 0))
  1395.                     {
  1396.                         if (m > 0)
  1397.                         {
  1398.                             QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) -=
  1399.                                 (rev*phi_re[i_min1] * MUL_F(adj->S_M_boost[l][m - 1], FRAC_CONST(0.00815)));
  1400.                         }
  1401.                         if (m + sbr->kx < 64)
  1402.                         {
  1403.                             QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx + 1]) +=
  1404.                                 (rev*phi_re[i_min1] * MUL_F(adj->S_M_boost[l][m], FRAC_CONST(0.00815)));
  1405.                         }
  1406.                     }
  1407. #else
  1408.                     if ((m == 0) && (phi_re[i_plus1] != 0))
  1409.                     {
  1410.                         QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx - 1]) +=
  1411.                             (rev*phi_re[i_plus1] * MUL_F((adj->S_M_boost[l][0]<<REAL_BITS), FRAC_CONST(0.00815)));
  1412.                         if (sbr->M != 0)
  1413.                         {
  1414.                             QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) -=
  1415.                                 (rev*phi_re[i_plus1] * MUL_F((adj->S_M_boost[l][1]<<REAL_BITS), FRAC_CONST(0.00815)));
  1416.                         }
  1417.                     }
  1418.                     if ((m > 0) && (m < sbr->M - 1) && (sinusoids < 16) && (phi_re[i_min1] != 0))
  1419.                     {
  1420.                         QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) -=
  1421.                             (rev*phi_re[i_min1] * MUL_F((adj->S_M_boost[l][m - 1]<<REAL_BITS), FRAC_CONST(0.00815)));
  1422.                     }
  1423.                     if ((m > 0) && (m < sbr->M - 1) && (sinusoids < 16) && (phi_re[i_plus1] != 0))
  1424.                     {
  1425.                         QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) -=
  1426.                             (rev*phi_re[i_plus1] * MUL_F((adj->S_M_boost[l][m + 1]<<REAL_BITS), FRAC_CONST(0.00815)));
  1427.                     }
  1428.                     if ((m == sbr->M - 1) && (sinusoids < 16) && (phi_re[i_min1] != 0))
  1429.                     {
  1430.                         if (m > 0)
  1431.                         {
  1432.                             QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx]) -=
  1433.                                 (rev*phi_re[i_min1] * MUL_F((adj->S_M_boost[l][m - 1]<<REAL_BITS), FRAC_CONST(0.00815)));
  1434.                         }
  1435.                         if (m + sbr->kx < 64)
  1436.                         {
  1437.                             QMF_RE(Xsbr[i + sbr->tHFAdj][m+sbr->kx + 1]) +=
  1438.                                 (rev*phi_re[i_min1] * MUL_F((adj->S_M_boost[l][m]<<REAL_BITS), FRAC_CONST(0.00815)));
  1439.                         }
  1440.                     }
  1441. #endif
  1442.                     if (adj->S_M_boost[l][m] != 0)
  1443.                         sinusoids++;
  1444. #endif
  1445.                 }
  1446.             }
  1447.             fIndexSine = (fIndexSine + 1) & 3;
  1448.             /* update the ringbuffer index used for filtering G and Q with h_smooth */
  1449.             sbr->GQ_ringbuf_index[ch]++;
  1450.             if (sbr->GQ_ringbuf_index[ch] >= 5)
  1451.                 sbr->GQ_ringbuf_index[ch] = 0;
  1452.         }
  1453.     }
  1454.     sbr->index_noise_prev[ch] = fIndexNoise;
  1455.     sbr->psi_is_prev[ch] = fIndexSine;
  1456. }
  1457. #endif