Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #include "layer3.h"
  2.  
  3. #include <math.h>
  4. #include <string.h>             //memmove
  5.  
  6. extern SIDE_INFO        m_side_info;
  7. extern SCALE_FACTOR     m_scale_fac[2][2];      // [gr][ch]
  8. extern CB_INFO          m_cb_info[2][2];        // [gr][ch]
  9.  
  10. extern int                      m_nsamp[2][2];
  11. extern int                      m_nBand[2][22];
  12. extern int                      m_ncbl_mixed;
  13.  
  14. #define GLOBAL_GAIN_SCALE (4*15)
  15.  
  16. static const int pretab[2][22] =
  17. {
  18.    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  19.    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0},
  20. };
  21.  
  22. /* 8 bit plus 2 lookup x = pow(2.0, 0.25*(global_gain-210)) */
  23. /* two extra slots to do 1/sqrt(2) scaling for ms */
  24. /* 4 extra slots to do 1/2 scaling for cvt to mono */
  25. static float look_global[256 + 2 + 4];
  26.  
  27. /*-------- scaling lookup
  28. x = pow(2.0, -0.5*(1+scalefact_scale)*scalefac + preemp)
  29. look_scale[scalefact_scale][preemp][scalefac]
  30. -----------------------*/
  31. static float look_scale[2][4][32];
  32.  
  33. /*--- iSample**(4/3) lookup, -32<=i<=31 ---*/
  34. #define ISMAX 32
  35. static float look_pow[2 * ISMAX];
  36.  
  37. /*-- pow(2.0, -0.25*8.0*subblock_gain) --*/
  38. static float look_subblock[8];
  39.  
  40. /*-- reorder buffer ---*/
  41. static float re_buf[192][3];
  42. typedef float ARRAY3[3];
  43.  
  44. void quant_init()
  45. {
  46.         int i;
  47.         int scalefact_scale, preemp, scalefac;
  48.         double tmp;
  49.  
  50. /* 8 bit plus 2 lookup x = pow(2.0, 0.25*(global_gain-210)) */
  51. /* extra 2 for ms scaling by 1/sqrt(2) */
  52. /* extra 4 for cvt to mono scaling by 1/2 */
  53.         for (i = 0; i < 256 + 2 + 4; i++)
  54.                 look_global[i] = (float) pow_test(2.0, 0.25 * ((i - (2 + 4)) - 210 + GLOBAL_GAIN_SCALE));
  55.  
  56. /* x = pow(2.0, -0.5*(1+scalefact_scale)*scalefac + preemp) */
  57.         for (scalefact_scale = 0; scalefact_scale < 2; scalefact_scale++)
  58.         {
  59.                 for (preemp = 0; preemp < 4; preemp++)
  60.                 {
  61.                         for (scalefac = 0; scalefac < 32; scalefac++)
  62.                         {
  63.                                 look_scale[scalefact_scale][preemp][scalefac] =
  64.                                                 (float) pow_test(2.0, -0.5 * (1 + scalefact_scale) * (scalefac + preemp));
  65.                         }
  66.                 }
  67.         }
  68.  
  69. /*--- iSample**(4/3) lookup, -32<=i<=31 ---*/
  70.         for (i = 0; i < 64; i++)
  71.         {
  72.                 tmp = i - 32;
  73.                 look_pow[i] = (float) (tmp * pow_test(fabs(tmp), (1.0 / 3.0)));
  74.         }
  75.  
  76. /*-- pow(2.0, -0.25*8.0*subblock_gain)  3 bits --*/
  77.         for (i = 0; i < 8; i++)
  78.         {
  79.                 look_subblock[i] = (float) pow_test(2.0, 0.25 * -8.0 * i);
  80.         }
  81. // quant_init_sf_band(sr_index);   replaced by code in sup.c
  82. }
  83.  
  84. void dequant(SAMPLE Sample[], int gr, int ch)
  85. {
  86.         SCALE_FACTOR* sf        = &m_scale_fac[gr][ch];
  87.         GR_INFO* gr_info        = &m_side_info.gr[gr][ch];
  88.         CB_INFO* cb_info        = &m_cb_info[gr][ch];
  89.         int* nsamp                      = &m_nsamp[gr][ch];
  90.  
  91.    int i, j;
  92.    int cb, n, w;
  93.    float x0, xs;
  94.    float xsb[3];
  95.    double tmp;
  96.    int ncbl;
  97.    int cbs0;
  98.    ARRAY3 *buf;                 /* short block reorder */
  99.    int nbands;
  100.    int i0;
  101.    int non_zero;
  102.    int cbmax[3];
  103.  
  104.    nbands = *nsamp;
  105.  
  106.  
  107.    ncbl = 22;                   /* long block cb end */
  108.    cbs0 = 12;                   /* short block cb start */
  109. /* ncbl_mixed = 8 or 6  mpeg1 or 2 */
  110.    if (gr_info->block_type == 2)
  111.    {
  112.       ncbl = 0;
  113.       cbs0 = 0;
  114.       if (gr_info->mixed_block_flag)
  115.       {
  116.          ncbl = m_ncbl_mixed;
  117.          cbs0 = 3;
  118.       }
  119.    }
  120. /* fill in cb_info -- */
  121.    cb_info->lb_type = gr_info->block_type;
  122.    if (gr_info->block_type == 2)
  123.       cb_info->lb_type;
  124.    cb_info->cbs0 = cbs0;
  125.    cb_info->ncbl = ncbl;
  126.  
  127.    cbmax[2] = cbmax[1] = cbmax[0] = 0;
  128. /* global gain pre-adjusted by 2 if ms_mode, 0 otherwise */
  129.    x0 = look_global[(2 + 4) + gr_info->global_gain];
  130.    i = 0;
  131. /*----- long blocks ---*/
  132.    for (cb = 0; cb < ncbl; cb++)
  133.    {
  134.       non_zero = 0;
  135.       xs = x0 * look_scale[gr_info->scalefac_scale][pretab[gr_info->preflag][cb]][sf->l[cb]];
  136.       n = m_nBand[0][cb];
  137.       for (j = 0; j < n; j++, i++)
  138.       {
  139.          if (Sample[i].s == 0)
  140.             Sample[i].x = 0.0F;
  141.          else
  142.          {
  143.             non_zero = 1;
  144.             if ((Sample[i].s >= (-ISMAX)) && (Sample[i].s < ISMAX))
  145.                Sample[i].x = xs * look_pow[ISMAX + Sample[i].s];
  146.             else
  147.             {
  148.                tmp = (double) Sample[i].s;
  149.                Sample[i].x = (float) (xs * tmp * pow_test(fabs(tmp), (1.0 / 3.0)));
  150.             }
  151.          }
  152.       }
  153.       if (non_zero)
  154.          cbmax[0] = cb;
  155.       if (i >= nbands)
  156.          break;
  157.    }
  158.  
  159.    cb_info->cbmax = cbmax[0];
  160.    cb_info->cbtype = 0;         // type = long
  161.  
  162.    if (cbs0 >= 12)
  163.       return;
  164. /*---------------------------
  165. block type = 2  short blocks
  166. ----------------------------*/
  167.    cbmax[2] = cbmax[1] = cbmax[0] = cbs0;
  168.    i0 = i;                      /* save for reorder */
  169.    buf = re_buf;
  170.    for (w = 0; w < 3; w++)
  171.       xsb[w] = x0 * look_subblock[gr_info->subblock_gain[w]];
  172.    for (cb = cbs0; cb < 13; cb++)
  173.    {
  174.       n = m_nBand[1][cb];
  175.       for (w = 0; w < 3; w++)
  176.       {
  177.          non_zero = 0;
  178.          xs = xsb[w] * look_scale[gr_info->scalefac_scale][0][sf->s[w][cb]];
  179.          for (j = 0; j < n; j++, i++)
  180.          {
  181.             if (Sample[i].s == 0)
  182.                buf[j][w] = 0.0F;
  183.             else
  184.             {
  185.                non_zero = 1;
  186.                if ((Sample[i].s >= (-ISMAX)) && (Sample[i].s < ISMAX))
  187.                   buf[j][w] = xs * look_pow[ISMAX + Sample[i].s];
  188.                else
  189.                {
  190.                   tmp = (double) Sample[i].s;
  191.                   buf[j][w] = (float) (xs * tmp * pow_test(fabs(tmp), (1.0 / 3.0)));
  192.                }
  193.             }
  194.          }
  195.          if (non_zero)
  196.             cbmax[w] = cb;
  197.       }
  198.       if (i >= nbands)
  199.          break;
  200.       buf += n;
  201.    }
  202.  
  203.  
  204.    memmove(&Sample[i0].x, &re_buf[0][0], sizeof(float) * (i - i0));
  205.  
  206.    *nsamp = i;                  /* update nsamp */
  207.    cb_info->cbmax_s[0] = cbmax[0];
  208.    cb_info->cbmax_s[1] = cbmax[1];
  209.    cb_info->cbmax_s[2] = cbmax[2];
  210.    if (cbmax[1] > cbmax[0])
  211.       cbmax[0] = cbmax[1];
  212.    if (cbmax[2] > cbmax[0])
  213.       cbmax[0] = cbmax[2];
  214.  
  215.    cb_info->cbmax = cbmax[0];
  216.    cb_info->cbtype = 1;         /* type = short */
  217. }
  218.