sbr_hfadj.c
上传用户:xjjlds
上传日期:2015-12-05
资源大小:22823k
文件大小:86k
源码类别:

多媒体编程

开发平台:

Visual C++

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