Subversion Repositories Kolibri OS

Rev

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

  1. #include "bstream.h"
  2. #include "mp3dec.h"
  3.  
  4. extern MPEG_DECODE_OPTION m_option;
  5.  
  6. extern float m_sample[2304];
  7. extern int m_nsb_limit;
  8. extern SBT_PROC m_sbt_proc;
  9.  
  10. extern int m_max_sb;
  11. extern int m_stereo_sb;
  12. extern int m_bat[4][16];
  13. extern int m_ballo[64];
  14. extern unsigned int m_samp_dispatch[66];
  15. extern float m_c_value[64];
  16. extern unsigned int m_sf_dispatch[66];
  17. extern float m_sf_table[64];
  18. extern float m_cs_factor[3][64];
  19.  
  20. float m_look_c_valueL1[18];
  21. float *m_cs_factorL1 = m_cs_factor[0];
  22. int m_nbatL1;
  23.  
  24. int m_bit_skip;
  25.  
  26. static const int look_joint[16] =
  27. {                               /* lookup stereo sb's by mode+ext */
  28.    64, 64, 64, 64,              /* stereo */
  29.    2 * 4, 2 * 8, 2 * 12, 2 * 16,        /* joint */
  30.    64, 64, 64, 64,              /* dual */
  31.    32, 32, 32, 32,              /* mono */
  32. };
  33.  
  34. static const int bat_bit_masterL1[] =
  35. {
  36.         0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
  37. };
  38.  
  39. void unpack_baL1();
  40. void unpack_sfL1();
  41. void unpack_sampL1();
  42.  
  43. void L1decode_frame(MPEG_HEADER* h, byte* mpeg, byte* pcm)
  44. {
  45.         int crc_size;
  46.  
  47.         crc_size = (h->error_prot) ? 2 : 0;
  48.         bitget_init(mpeg + 4 + crc_size);
  49.  
  50.         m_stereo_sb = look_joint[(h->mode << 2) + h->mode_ext];
  51.         unpack_baL1();          /* unpack bit allocation */
  52.         unpack_sfL1();          /* unpack scale factor */
  53.         unpack_sampL1();        /* unpack samples */
  54.  
  55.         m_sbt_proc(m_sample, pcm, 12);
  56. }
  57.  
  58. void unpack_baL1()
  59. {
  60.         int j;
  61.         int nstereo;
  62.  
  63.         m_bit_skip = 0;
  64.         nstereo = m_stereo_sb;
  65.  
  66.         for (j = 0; j < m_nbatL1; j++)
  67.         {
  68.                 bitget_check(4);
  69.                 m_ballo[j] = m_samp_dispatch[j] = mac_bitget(4);
  70.                 if (j >= m_nsb_limit)
  71.                         m_bit_skip += bat_bit_masterL1[m_samp_dispatch[j]];
  72.                 m_c_value[j] = m_look_c_valueL1[m_samp_dispatch[j]];
  73.                 if (--nstereo < 0)
  74.                 {
  75.                         m_ballo[j + 1] = m_ballo[j];
  76.                         // flag as joint
  77.                         m_samp_dispatch[j] += 15;
  78.                         // flag for sf
  79.                         m_samp_dispatch[j + 1] = m_samp_dispatch[j];
  80.                         m_c_value[j + 1] = m_c_value[j];
  81.                         j++;
  82.                 }
  83.         }
  84. // terminate with bit skip and end
  85.         m_samp_dispatch[m_nsb_limit] = 31;
  86.         m_samp_dispatch[j] = 30;
  87. }
  88.  
  89. // unpack scale factor
  90. // combine dequant and scale factors
  91. void unpack_sfL1()
  92. {
  93.         int i;
  94.  
  95.         for (i = 0; i < m_nbatL1; i++)
  96.         {
  97.                 if (m_ballo[i])
  98.                 {
  99.                         bitget_check(6);
  100.                         m_cs_factorL1[i] = m_c_value[i] * m_sf_table[mac_bitget(6)];
  101.                 }
  102.         }
  103. }
  104.  
  105. // unpack samples
  106. #define UNPACKL1_N(n) \
  107.         s[k] = m_cs_factorL1[k]*(bitget(n)-((1 << n-1) -1));     \
  108.         goto dispatch;
  109. #define UNPACKL1J_N(n) \
  110.         tmp        =  (bitget(n)-((1 << n-1) -1));             \
  111.         s[k]       =  m_cs_factorL1[k]*tmp;                      \
  112.         s[k+1]     =  m_cs_factorL1[k+1]*tmp;                    \
  113.         k++;                                                   \
  114.         goto dispatch;
  115.  
  116. void unpack_sampL1()
  117. {
  118.    int j, k;
  119.    float *s;
  120.    long tmp;
  121.  
  122.    s = m_sample;
  123.    for (j = 0; j < 12; j++)
  124.    {
  125.       k = -1;
  126.     dispatch:
  127.           switch (m_samp_dispatch[++k])
  128.       {
  129.          case 0:
  130.             s[k] = 0.0F;
  131.             goto dispatch;
  132.          case 1:
  133.             UNPACKL1_N(2)       /*  3 levels */
  134.          case 2:
  135.             UNPACKL1_N(3)       /*  7 levels */
  136.          case 3:
  137.             UNPACKL1_N(4)       /* 15 levels */
  138.          case 4:
  139.             UNPACKL1_N(5)       /* 31 levels */
  140.          case 5:
  141.             UNPACKL1_N(6)       /* 63 levels */
  142.          case 6:
  143.             UNPACKL1_N(7)       /* 127 levels */
  144.          case 7:
  145.             UNPACKL1_N(8)       /* 255 levels */
  146.          case 8:
  147.             UNPACKL1_N(9)       /* 511 levels */
  148.          case 9:
  149.             UNPACKL1_N(10)      /* 1023 levels */
  150.          case 10:
  151.             UNPACKL1_N(11)      /* 2047 levels */
  152.          case 11:
  153.             UNPACKL1_N(12)      /* 4095 levels */
  154.          case 12:
  155.             UNPACKL1_N(13)      /* 8191 levels */
  156.          case 13:
  157.             UNPACKL1_N(14)      /* 16383 levels */
  158.          case 14:
  159.             UNPACKL1_N(15)      /* 32767 levels */
  160. /* -- joint ---- */
  161.          case 15 + 0:
  162.             s[k + 1] = s[k] = 0.0F;
  163.             k++;                /* skip right chan dispatch */
  164.             goto dispatch;
  165. /* -- joint ---- */
  166.          case 15 + 1:
  167.             UNPACKL1J_N(2)      /*  3 levels */
  168.          case 15 + 2:
  169.             UNPACKL1J_N(3)      /*  7 levels */
  170.          case 15 + 3:
  171.             UNPACKL1J_N(4)      /* 15 levels */
  172.          case 15 + 4:
  173.             UNPACKL1J_N(5)      /* 31 levels */
  174.          case 15 + 5:
  175.             UNPACKL1J_N(6)      /* 63 levels */
  176.          case 15 + 6:
  177.             UNPACKL1J_N(7)      /* 127 levels */
  178.          case 15 + 7:
  179.             UNPACKL1J_N(8)      /* 255 levels */
  180.          case 15 + 8:
  181.             UNPACKL1J_N(9)      /* 511 levels */
  182.          case 15 + 9:
  183.             UNPACKL1J_N(10)     /* 1023 levels */
  184.          case 15 + 10:
  185.             UNPACKL1J_N(11)     /* 2047 levels */
  186.          case 15 + 11:
  187.             UNPACKL1J_N(12)     /* 4095 levels */
  188.          case 15 + 12:
  189.             UNPACKL1J_N(13)     /* 8191 levels */
  190.          case 15 + 13:
  191.             UNPACKL1J_N(14)     /* 16383 levels */
  192.          case 15 + 14:
  193.             UNPACKL1J_N(15)     /* 32767 levels */
  194.  
  195. /* -- end of dispatch -- */
  196.          case 31:
  197.             bitget_skip(m_bit_skip);
  198.          case 30:
  199.             s += 64;
  200.       }                         /* end switch */
  201.    }                            /* end j loop */
  202. }
  203.