Subversion Repositories Kolibri OS

Rev

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

  1. #include <math.h>
  2.  
  3. extern int m_band_limit;
  4.  
  5. /* "imdct.c" */
  6. void imdct18(float f[]);        /* 18 point */
  7. void imdct6_3(float f[]);       /* 6 point */
  8.  
  9.  
  10. /*-- windows by block type --*/
  11. static float win[4][36];
  12.  
  13. void hwin_init()
  14. {
  15.    int i, j;
  16.    double pi;
  17.  
  18.    pi = 4.0 * atan(1.0);
  19.  
  20. /* type 0 */
  21.    for (i = 0; i < 36; i++)
  22.       win[0][i] = (float) sin(pi / 36 * (i + 0.5));
  23.  
  24. /* type 1 */
  25.    for (i = 0; i < 18; i++)
  26.       win[1][i] = (float) sin(pi / 36 * (i + 0.5));
  27.    for (i = 18; i < 24; i++)
  28.       win[1][i] = 1.0F;
  29.    for (i = 24; i < 30; i++)
  30.       win[1][i] = (float) sin(pi / 12 * (i + 0.5 - 18));
  31.    for (i = 30; i < 36; i++)
  32.       win[1][i] = 0.0F;
  33.  
  34. /* type 3 */
  35.    for (i = 0; i < 6; i++)
  36.       win[3][i] = 0.0F;
  37.    for (i = 6; i < 12; i++)
  38.       win[3][i] = (float) sin(pi / 12 * (i + 0.5 - 6));
  39.    for (i = 12; i < 18; i++)
  40.       win[3][i] = 1.0F;
  41.    for (i = 18; i < 36; i++)
  42.       win[3][i] = (float) sin(pi / 36 * (i + 0.5));
  43.  
  44. /* type 2 */
  45.    for (i = 0; i < 12; i++)
  46.       win[2][i] = (float) sin(pi / 12 * (i + 0.5));
  47.    for (i = 12; i < 36; i++)
  48.       win[2][i] = 0.0F;
  49.  
  50. /*--- invert signs by region to match mdct 18pt --> 36pt mapping */
  51.    for (j = 0; j < 4; j++)
  52.    {
  53.       if (j == 2)
  54.          continue;
  55.       for (i = 9; i < 36; i++)
  56.          win[j][i] = -win[j][i];
  57.    }
  58.  
  59. /*-- invert signs for short blocks --*/
  60.    for (i = 3; i < 12; i++)
  61.       win[2][i] = -win[2][i];
  62.  
  63.    return;
  64. }
  65. /*====================================================================*/
  66. int hybrid(float xin[], float xprev[], float y[18][32],
  67.            int btype, int nlong, int ntot, int nprev)
  68. {
  69.    int i, j;
  70.    float *x, *x0;
  71.    float xa, xb;
  72.    int n;
  73.    int nout;
  74.    int band_limit_nsb;
  75.  
  76.    if (btype == 2)
  77.       btype = 0;
  78.    x = xin;
  79.    x0 = xprev;
  80.  
  81. /*-- do long blocks (if any) --*/
  82.    n = (nlong + 17) / 18;       /* number of dct's to do */
  83.    for (i = 0; i < n; i++)
  84.    {
  85.       imdct18(x);
  86.       for (j = 0; j < 9; j++)
  87.       {
  88.          y[j][i] = x0[j] + win[btype][j] * x[9 + j];
  89.          y[9 + j][i] = x0[9 + j] + win[btype][9 + j] * x[17 - j];
  90.       }
  91.     /* window x for next time x0 */
  92.       for (j = 0; j < 4; j++)
  93.       {
  94.          xa = x[j];
  95.          xb = x[8 - j];
  96.          x[j] = win[btype][18 + j] * xb;
  97.          x[8 - j] = win[btype][(18 + 8) - j] * xa;
  98.          x[9 + j] = win[btype][(18 + 9) + j] * xa;
  99.          x[17 - j] = win[btype][(18 + 17) - j] * xb;
  100.       }
  101.       xa = x[j];
  102.       x[j] = win[btype][18 + j] * xa;
  103.       x[9 + j] = win[btype][(18 + 9) + j] * xa;
  104.  
  105.       x += 18;
  106.       x0 += 18;
  107.    }
  108.  
  109. /*-- do short blocks (if any) --*/
  110.    n = (ntot + 17) / 18;        /* number of 6 pt dct's triples to do */
  111.    for (; i < n; i++)
  112.    {
  113.       imdct6_3(x);
  114.       for (j = 0; j < 3; j++)
  115.       {
  116.          y[j][i] = x0[j];
  117.          y[3 + j][i] = x0[3 + j];
  118.  
  119.          y[6 + j][i] = x0[6 + j] + win[2][j] * x[3 + j];
  120.          y[9 + j][i] = x0[9 + j] + win[2][3 + j] * x[5 - j];
  121.  
  122.          y[12 + j][i] = x0[12 + j] + win[2][6 + j] * x[2 - j] + win[2][j] * x[(6 + 3) + j];
  123.          y[15 + j][i] = x0[15 + j] + win[2][9 + j] * x[j] + win[2][3 + j] * x[(6 + 5) - j];
  124.       }
  125.     /* window x for next time x0 */
  126.       for (j = 0; j < 3; j++)
  127.       {
  128.          x[j] = win[2][6 + j] * x[(6 + 2) - j] + win[2][j] * x[(12 + 3) + j];
  129.          x[3 + j] = win[2][9 + j] * x[6 + j] + win[2][3 + j] * x[(12 + 5) - j];
  130.       }
  131.       for (j = 0; j < 3; j++)
  132.       {
  133.          x[6 + j] = win[2][6 + j] * x[(12 + 2) - j];
  134.          x[9 + j] = win[2][9 + j] * x[12 + j];
  135.       }
  136.       for (j = 0; j < 3; j++)
  137.       {
  138.          x[12 + j] = 0.0f;
  139.          x[15 + j] = 0.0f;
  140.       }
  141.       x += 18;
  142.       x0 += 18;
  143.    }
  144.  
  145. /*--- overlap prev if prev longer that current --*/
  146.    n = (nprev + 17) / 18;
  147.    for (; i < n; i++)
  148.    {
  149.       for (j = 0; j < 18; j++)
  150.          y[j][i] = x0[j];
  151.       x0 += 18;
  152.    }
  153.    nout = 18 * i;
  154.  
  155. /*--- clear remaining only to band limit --*/
  156.         band_limit_nsb = (m_band_limit + 17) / 18;      /* limit nsb's rounded up */
  157.    for (; i < band_limit_nsb; i++)
  158.    {
  159.       for (j = 0; j < 18; j++)
  160.          y[j][i] = 0.0f;
  161.    }
  162.  
  163.    return nout;
  164. }
  165. /*--------------------------------------------------------------------*/
  166. /*--------------------------------------------------------------------*/
  167. /*-- convert to mono, add curr result to y,
  168.     window and add next time to current left */
  169. int hybrid_sum(float xin[], float xin_left[], float y[18][32],
  170.                int btype, int nlong, int ntot)
  171. {
  172.    int i, j;
  173.    float *x, *x0;
  174.    float xa, xb;
  175.    int n;
  176.    int nout;
  177.  
  178.    if (btype == 2)
  179.       btype = 0;
  180.    x = xin;
  181.    x0 = xin_left;
  182.  
  183. /*-- do long blocks (if any) --*/
  184.    n = (nlong + 17) / 18;       /* number of dct's to do */
  185.    for (i = 0; i < n; i++)
  186.    {
  187.       imdct18(x);
  188.       for (j = 0; j < 9; j++)
  189.       {
  190.          y[j][i] += win[btype][j] * x[9 + j];
  191.          y[9 + j][i] += win[btype][9 + j] * x[17 - j];
  192.       }
  193.     /* window x for next time x0 */
  194.       for (j = 0; j < 4; j++)
  195.       {
  196.          xa = x[j];
  197.          xb = x[8 - j];
  198.          x0[j] += win[btype][18 + j] * xb;
  199.          x0[8 - j] += win[btype][(18 + 8) - j] * xa;
  200.          x0[9 + j] += win[btype][(18 + 9) + j] * xa;
  201.          x0[17 - j] += win[btype][(18 + 17) - j] * xb;
  202.       }
  203.       xa = x[j];
  204.       x0[j] += win[btype][18 + j] * xa;
  205.       x0[9 + j] += win[btype][(18 + 9) + j] * xa;
  206.  
  207.       x += 18;
  208.       x0 += 18;
  209.    }
  210.  
  211. /*-- do short blocks (if any) --*/
  212.    n = (ntot + 17) / 18;        /* number of 6 pt dct's triples to do */
  213.    for (; i < n; i++)
  214.    {
  215.       imdct6_3(x);
  216.       for (j = 0; j < 3; j++)
  217.       {
  218.          y[6 + j][i] += win[2][j] * x[3 + j];
  219.          y[9 + j][i] += win[2][3 + j] * x[5 - j];
  220.  
  221.          y[12 + j][i] += win[2][6 + j] * x[2 - j] + win[2][j] * x[(6 + 3) + j];
  222.          y[15 + j][i] += win[2][9 + j] * x[j] + win[2][3 + j] * x[(6 + 5) - j];
  223.       }
  224.     /* window x for next time */
  225.       for (j = 0; j < 3; j++)
  226.       {
  227.          x0[j] += win[2][6 + j] * x[(6 + 2) - j] + win[2][j] * x[(12 + 3) + j];
  228.          x0[3 + j] += win[2][9 + j] * x[6 + j] + win[2][3 + j] * x[(12 + 5) - j];
  229.       }
  230.       for (j = 0; j < 3; j++)
  231.       {
  232.          x0[6 + j] += win[2][6 + j] * x[(12 + 2) - j];
  233.          x0[9 + j] += win[2][9 + j] * x[12 + j];
  234.       }
  235.       x += 18;
  236.       x0 += 18;
  237.    }
  238.  
  239.    nout = 18 * i;
  240.  
  241.    return nout;
  242. }
  243. /*--------------------------------------------------------------------*/
  244. void sum_f_bands(float a[], float b[], int n)
  245. {
  246.    int i;
  247.  
  248.    for (i = 0; i < n; i++)
  249.       a[i] += b[i];
  250. }
  251. /*--------------------------------------------------------------------*/
  252. void freq_invert(float y[18][32], int n)
  253. {
  254.    int i, j;
  255.  
  256.    n = (n + 17) / 18;
  257.    for (j = 0; j < 18; j += 2)
  258.    {
  259.       for (i = 0; i < n; i += 2)
  260.       {
  261.          y[1 + j][1 + i] = -y[1 + j][1 + i];
  262.       }
  263.    }
  264. }
  265. /*--------------------------------------------------------------------*/
  266.