MP3_Huffman.c
上传用户:kingbiz
上传日期:2022-06-24
资源大小:2524k
文件大小:29k
源码类别:

mpeg/mp3

开发平台:

C/C++

  1. #include "mp3_huffman.h"
  2. #include "mp3_common.h"
  3. /*============================================================================*/
  4. /* lots of tables */
  5. /* code implementing the Huffman decoder for layer-3 */
  6. /* decode the Huffman coded energies of a Granule */
  7. #define BITS_USED ((((datapos - (totpos >> 3)) *8) % BITSTREAM_BUFSIZE) - 
  8. dataword_len - (totpos & 7))
  9. /*============================================================================*/
  10. U32 buf_byte_idx;
  11. /*============================================================================*/
  12. static  U32 dataword; 
  13. static U32 dataword_len;
  14. static U32 datapos;
  15. static  U32 offset;
  16. static  U32 buf_bit_idx=8;
  17. static int t_linbits[32] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4,6,8,
  18.      10,13,4,5,6,7,8,9,11,13 };
  19. /*============================================================================*/
  20. /* hcod(20)  hlen(4)   x(4)  y(4)  20位中前面hlen位为hcod   */
  21. static U32 h0[1]={0x0};
  22. static U32 h1[4]={0x311, 0x20000301, 0x40000210, 0x80000100};
  23. static U32 h2[9]={0x622, 0x4000602, 0x8000512, 0x10000521, 0x18000520, 0x20000311, 0x40000301, 0x60000310, 0x80000100};
  24. static U32 h3[9]={ 0x622, 0x4000602, 0x8000512, 0x10000521, 0x18000520, 0x20000310, 0x40000211, 0x80000201, 0xc0000200};
  25. static U32 h5[16]={0x833, 0x1000823, 0x2000732, 0x4000631, 0x8000713, 0xa000703, 0xc000730, 0xe000722,
  26.  0x10000612, 0x14000621, 0x18000602, 0x1c000620, 0x20000311, 0x40000301, 0x60000310, 0x80000100};
  27. static U32 h6[16]={0x733, 0x2000703, 0x4000623, 0x8000632, 0xc000630, 0x10000513, 0x18000531, 0x20000522,
  28.  0x28000502, 0x30000412, 0x40000421, 0x50000420, 0x60000301, 0x80000211, 0xc0000310, 0xe0000300};
  29. static U32 h7[36]={ 0xa55, 0x400a45, 0x800a54, 0xc00a53, 0x1000935, 0x1800944, 0x2000925, 0x2800952,
  30.  0x3000815, 0x4000851, 0x5000905, 0x5800934, 0x6000850, 0x7000943, 0x7800933, 0x8000824,
  31.  0x9000842, 0xa000714, 0xc000741, 0xe000740, 0x10000804, 0x11000823, 0x12000832, 0x13000803,
  32.  0x14000713, 0x16000731, 0x18000730, 0x1a000722, 0x1c000612, 0x20000521, 0x28000602, 0x2c000620,
  33.  0x30000411, 0x40000301, 0x60000310, 0x80000100};
  34. static U32 h8[36]={0xb55, 0x200b54, 0x400a45, 0x800953, 0x1000a35, 0x1400a44, 0x1800925, 0x2000952,
  35.  0x2800905, 0x3000815, 0x4000851, 0x5000934, 0x5800943, 0x6000950, 0x6800933, 0x7000824,
  36.  0x8000842, 0x9000814, 0xa000741, 0xc000804, 0xd000840, 0xe000823, 0xf000832, 0x10000813,
  37.  0x11000831, 0x12000803, 0x13000830, 0x14000622, 0x18000602, 0x1c000620, 0x20000412, 0x30000421,
  38.  0x40000211, 0x80000301, 0xa0000310, 0xc0000200};
  39. static U32 h9[36]={ 0x955, 0x800945, 0x1000835, 0x2000853, 0x3000954, 0x3800905, 0x4000844, 0x5000825,
  40.  0x6000852, 0x7000815, 0x8000751, 0xa000734, 0xc000743, 0xe000850, 0xf000804, 0x10000724,
  41.  0x12000742, 0x14000733, 0x16000740, 0x18000614, 0x1c000641, 0x20000623, 0x24000632, 0x28000513,
  42.  0x30000531, 0x38000603, 0x3c000630, 0x40000522, 0x48000502, 0x50000412, 0x60000421, 0x70000420,
  43.  0x80000311, 0xa0000301, 0xc0000310, 0xe0000300};
  44. static U32 h10[64]={ 0xb77, 0x200b67, 0x400b76, 0x600b57, 0x800b75, 0xa00b66, 0xc00a47, 0x1000a74,
  45.  0x1400a56, 0x1800a65, 0x1c00a37, 0x2000a73, 0x2400a46, 0x2800b55, 0x2a00b54, 0x2c00a63,
  46.  0x3000927, 0x3800972, 0x4000a64, 0x4400a07, 0x4800970, 0x5000962, 0x5800a45, 0x5c00a35,
  47.  0x6000906, 0x6800a53, 0x6c00a44, 0x7000817, 0x8000871, 0x9000936, 0x9800926, 0xa000a25,
  48.  0xa400a52, 0xa800915, 0xb000951, 0xb800a34, 0xbc00a43, 0xc000816, 0xd000861, 0xe000860,
  49.  0xf000905, 0xf800950, 0x10000924, 0x10800942, 0x11000933, 0x11800904, 0x12000814, 0x13000841,
  50.  0x14000840, 0x15000823, 0x16000832, 0x17000803, 0x18000713, 0x1a000731, 0x1c000730, 0x1e000722,
  51.  0x20000612, 0x24000621, 0x28000602, 0x2c000620, 0x30000411, 0x40000301, 0x60000310, 0x80000100};
  52. static U32 h11[64]={ 0xa77, 0x400a67, 0x800a76, 0xc00a75, 0x1000a66, 0x1400a47, 0x1800a74, 0x1c00b57,
  53.  0x1e00b55, 0x2000a56, 0x2400a65, 0x2800937, 0x3000973, 0x3800946, 0x4000a45, 0x4400a54,
  54.  0x4800a35, 0x4c00a53, 0x5000827, 0x6000872, 0x7000964, 0x7800907, 0x8000771, 0xa000817,
  55.  0xb000870, 0xc000836, 0xd000863, 0xe000860, 0xf000944, 0xf800925, 0x10000952, 0x10800905,
  56.  0x11000815, 0x12000762, 0x14000826, 0x15000806, 0x16000716, 0x18000761, 0x1a000851, 0x1b000834,
  57.  0x1c000850, 0x1d000943, 0x1d800933, 0x1e000824, 0x1f000842, 0x20000814, 0x21000841, 0x22000804,
  58.  0x23000840, 0x24000723, 0x26000732, 0x28000613, 0x2c000631, 0x30000703, 0x32000730, 0x34000622,
  59.  0x38000521, 0x40000412, 0x50000502, 0x58000520, 0x60000311, 0x80000301, 0xa0000310, 0xc0000200};
  60. static U32 h12[64]={ 0xa77, 0x400a67, 0x800976, 0x1000957, 0x1800975, 0x2000966, 0x2800947, 0x3000974,
  61.  0x3800965, 0x4000856, 0x5000837, 0x6000973, 0x6800955, 0x7000827, 0x8000872, 0x9000846,
  62.  0xa000864, 0xb000817, 0xc000871, 0xd000907, 0xd800970, 0xe000836, 0xf000863, 0x10000845,
  63.  0x11000854, 0x12000844, 0x13000906, 0x13800905, 0x14000726, 0x16000762, 0x18000761, 0x1a000816,
  64.  0x1b000860, 0x1c000835, 0x1d000853, 0x1e000825, 0x1f000852, 0x20000715, 0x22000751, 0x24000734,
  65.  0x26000743, 0x28000850, 0x29000804, 0x2a000724, 0x2c000742, 0x2e000714, 0x30000633, 0x34000641,
  66.  0x38000623, 0x3c000632, 0x40000740, 0x42000703, 0x44000630, 0x48000513, 0x50000531, 0x58000522,
  67.  0x60000412, 0x70000421, 0x80000502, 0x88000520, 0x90000400, 0xa0000311, 0xc0000301, 0xe0000310};
  68. static U32 h13[256]={
  69.  0x13fe, 0x33fc, 0x52fd, 0x91ed, 0x110ff, 0x210ef, 0x310df, 0x410ee,
  70.  0x510cf, 0x610de, 0x710bf, 0x810fb, 0x910ce, 0xa10dc, 0xb11af, 0xb91e9,
  71.  0xc0fec, 0xe0fdd, 0x1010fa, 0x1110cd, 0x120fbe, 0x140feb, 0x160f9f, 0x180ff9,
  72.  0x1a0fea, 0x1c0fbd, 0x1e0fdb, 0x200f8f, 0x220ff8, 0x240fcc, 0x2610ae, 0x27109e,
  73.  0x280f8e, 0x2a107f, 0x2b107e, 0x2c0ef7, 0x300eda, 0x340fad, 0x360fbc, 0x380fcb,
  74.  0x3a0ff6, 0x3c0e6f, 0x400ee8, 0x440e5f, 0x480e9d, 0x4c0ed9, 0x500ef5, 0x540ee7,
  75.  0x580eac, 0x5c0ebb, 0x600e4f, 0x640ef4, 0x680fca, 0x6a0fe6, 0x6c0ef3, 0x700d3f,
  76.  0x780e8d, 0x7c0ed8, 0x800d2f, 0x880df2, 0x900e6e, 0x940e9c, 0x980d0f, 0xa00ec9,
  77.  0xa40e5e, 0xa80dab, 0xb00e7d, 0xb40ed7, 0xb80d4e, 0xc00ec8, 0xc40ed6, 0xc80d3e,
  78.  0xd00db9, 0xd80e9b, 0xdc0eaa, 0xe00c1f, 0xf00cf1, 0x1000cf0, 0x1100dba, 0x1180de5,
  79.  0x1200de4, 0x1280d8c, 0x1300d6d, 0x1380de3, 0x1400ce2, 0x1500d2e, 0x1580d0e, 0x1600c1e,
  80.  0x1700ce1, 0x1800de0, 0x1880d5d, 0x1900dd5, 0x1980d7c, 0x1a00dc7, 0x1a80d4d, 0x1b00d8b,
  81.  0x1b80db8, 0x1c00dd4, 0x1c80d9a, 0x1d00da9, 0x1d80d6c, 0x1e00cc6, 0x1f00c3d, 0x2000dd3,
  82.  0x2080d7b, 0x2100c2d, 0x2200cd2, 0x2300c1d, 0x2400cb7, 0x2500d5c, 0x2580dc5, 0x2600d99,
  83.  0x2680d7a, 0x2700cc3, 0x2800da7, 0x2880d97, 0x2900c4b, 0x2a00bd1, 0x2c00c0d, 0x2d00cd0,
  84.  0x2e00c8a, 0x2f00ca8, 0x3000c4c, 0x3100cc4, 0x3200c6b, 0x3300cb6, 0x3400b3c, 0x3600b2c,
  85.  0x3800bc2, 0x3a00b5b, 0x3c00cb5, 0x3d00c89, 0x3e00b1c, 0x4000bc1, 0x4200c98, 0x4300c0c,
  86.  0x4400bc0, 0x4600cb4, 0x4700c6a, 0x4800ca6, 0x4900c79, 0x4a00b3b, 0x4c00bb3, 0x4e00c88,
  87.  0x4f00c5a, 0x5000b2b, 0x5200ca5, 0x5300c69, 0x5400ba4, 0x5600c78, 0x5700c87, 0x5800b94,
  88.  0x5a00c77, 0x5b00c76, 0x5c00ab2, 0x6000a1b, 0x6400ab1, 0x6800b0b, 0x6a00bb0, 0x6c00b96,
  89.  0x6e00b4a, 0x7000b3a, 0x7200ba3, 0x7400b59, 0x7600b95, 0x7800a2a, 0x7c00aa2, 0x8000a1a,
  90.  0x8400aa1, 0x8800b0a, 0x8a00b68, 0x8c00aa0, 0x9000b86, 0x9200b49, 0x9400a93, 0x9800b39,
  91.  0x9a00b58, 0x9c00b85, 0x9e00b67, 0xa000a29, 0xa400a92, 0xa800b57, 0xaa00b75, 0xac00a38,
  92.  0xb000a83, 0xb400b66, 0xb600b47, 0xb800b74, 0xba00b56, 0xbc00b65, 0xbe00b73, 0xc000919,
  93.  0xc800991, 0xd000a09, 0xd400a90, 0xd800a48, 0xdc00a84, 0xe000a72, 0xe400b46, 0xe600b64,
  94.  0xe800928, 0xf000982, 0xf800918, 0x10000a37, 0x10400a27, 0x10800917, 0x11000971, 0x11800a55,
  95.  0x11c00a07, 0x12000a70, 0x12400a36, 0x12800a63, 0x12c00a45, 0x13000a54, 0x13400a26, 0x13800a62,
  96.  0x13c00a35, 0x14000881, 0x15000908, 0x15800980, 0x16000916, 0x16800961, 0x17000906, 0x17800960,
  97.  0x18000a53, 0x18400a44, 0x18800925, 0x19000952, 0x19800905, 0x1a000815, 0x1b000851, 0x1c000934,
  98.  0x1c800943, 0x1d000950, 0x1d800924, 0x1e000942, 0x1e800933, 0x1f000814, 0x20000741, 0x22000804,
  99.  0x23000840, 0x24000823, 0x25000832, 0x26000713, 0x28000731, 0x2a000703, 0x2c000730, 0x2e000722,
  100.  0x30000612, 0x34000621, 0x38000602, 0x3c000620, 0x40000411, 0x50000401, 0x60000310, 0x80000100};
  101. static U32 h15[256]={ 0xdff, 0x80def, 0x100dfe, 0x180ddf, 0x200cee, 0x300dfd, 0x380dcf, 0x400dfc,
  102.  0x480dde, 0x500ded, 0x580dbf, 0x600cfb, 0x700dce, 0x780dec, 0x800cdd, 0x900caf,
  103.  0xa00cfa, 0xb00cbe, 0xc00ceb, 0xd00ccd, 0xe00cdc, 0xf00c9f, 0x1000cf9, 0x1100cea,
  104.  0x1200cbd, 0x1300cdb, 0x1400c8f, 0x1500cf8, 0x1600ccc, 0x1700c9e, 0x1800ce9, 0x1900c7f,
  105.  0x1a00cf7, 0x1b00cad, 0x1c00cda, 0x1d00cbc, 0x1e00c6f, 0x1f00dae, 0x1f80d0f, 0x2000bcb,
  106.  0x2200bf6, 0x2400c8e, 0x2500ce8, 0x2600c5f, 0x2700c9d, 0x2800bf5, 0x2a00b7e, 0x2c00be7,
  107.  0x2e00bac, 0x3000bca, 0x3200bbb, 0x3400cd9, 0x3500c8d, 0x3600b4f, 0x3800bf4, 0x3a00b3f,
  108.  0x3c00bf3, 0x3e00bd8, 0x4000be6, 0x4200b2f, 0x4400bf2, 0x4600c6e, 0x4700cf0, 0x4800b1f,
  109.  0x4a00bf1, 0x4c00b9c, 0x4e00bc9, 0x5000b5e, 0x5200bab, 0x5400bba, 0x5600be5, 0x5800b7d,
  110.  0x5a00bd7, 0x5c00b4e, 0x5e00be4, 0x6000b8c, 0x6200bc8, 0x6400b3e, 0x6600b6d, 0x6800bd6,
  111.  0x6a00be3, 0x6c00b9b, 0x6e00bb9, 0x7000b2e, 0x7200baa, 0x7400be2, 0x7600b1e, 0x7800be1,
  112.  0x7a00c0e, 0x7b00ce0, 0x7c00b5d, 0x7e00bd5, 0x8000b7c, 0x8200bc7, 0x8400b4d, 0x8600b8b,
  113.  0x8800ad4, 0x8c00bb8, 0x8e00b9a, 0x9000ba9, 0x9200b6c, 0x9400bc6, 0x9600b3d, 0x9800ad3,
  114.  0x9c00ad2, 0xa000b2d, 0xa200b0d, 0xa400a1d, 0xa800a7b, 0xac00ab7, 0xb000ad1, 0xb400b5c,
  115.  0xb600bd0, 0xb800ac5, 0xbc00a8a, 0xc000aa8, 0xc400a4c, 0xc800ac4, 0xcc00a6b, 0xd000ab6,
  116.  0xd400b99, 0xd600b0c, 0xd800a3c, 0xdc00ac3, 0xe000a7a, 0xe400aa7, 0xe800aa6, 0xec00bc0,
  117.  0xee00b0b, 0xf0009c2, 0xf800a2c, 0xfc00a5b, 0x10000ab5, 0x10400a1c, 0x10800a89, 0x10c00a98,
  118.  0x11000ac1, 0x11400a4b, 0x11800ab4, 0x11c00a6a, 0x12000a3b, 0x12400a79, 0x128009b3, 0x13000a97,
  119.  0x13400a88, 0x13800a2b, 0x13c00a5a, 0x140009b2, 0x14800aa5, 0x14c00a1b, 0x150009b1, 0x15800ab0,
  120.  0x15c00a69, 0x16000a96, 0x16400a4a, 0x16800aa4, 0x16c00a78, 0x17000a87, 0x17400a3a, 0x178009a3,
  121.  0x18000959, 0x18800995, 0x1900092a, 0x198009a2, 0x1a00091a, 0x1a8009a1, 0x1b000a0a, 0x1b400aa0,
  122.  0x1b800968, 0x1c000986, 0x1c800949, 0x1d000994, 0x1d800939, 0x1e000993, 0x1e800a77, 0x1ec00a09,
  123.  0x1f000958, 0x1f800985, 0x20000929, 0x20800967, 0x21000976, 0x21800992, 0x22000891, 0x23000919,
  124.  0x23800990, 0x24000948, 0x24800984, 0x25000957, 0x25800975, 0x26000938, 0x26800983, 0x27000966,
  125.  0x27800947, 0x28000828, 0x29000882, 0x2a000818, 0x2b000881, 0x2c000974, 0x2c800908, 0x2d000980,
  126.  0x2d800956, 0x2e000965, 0x2e800937, 0x2f000973, 0x2f800946, 0x30000827, 0x31000872, 0x32000864,
  127.  0x33000817, 0x34000855, 0x35000871, 0x36000907, 0x36800970, 0x37000836, 0x38000863, 0x39000845,
  128.  0x3a000854, 0x3b000826, 0x3c000862, 0x3d000816, 0x3e000906, 0x3e800960, 0x3f000835, 0x40000761,
  129.  0x42000853, 0x43000844, 0x44000725, 0x46000752, 0x48000715, 0x4a000751, 0x4c000805, 0x4d000850,
  130.  0x4e000734, 0x50000743, 0x52000724, 0x54000742, 0x56000733, 0x58000641, 0x5c000714, 0x5e000704,
  131.  0x60000623, 0x64000632, 0x68000740, 0x6a000703, 0x6c000613, 0x70000631, 0x74000630, 0x78000522,
  132.  0x80000512, 0x88000521, 0x90000502, 0x98000520, 0xa0000311, 0xc0000401, 0xd0000410, 0xe0000300};
  133. static U32 h16[256]={ 0xbef, 0x200bfe, 0x400bdf, 0x600bfd, 0x800bcf, 0xa00bfc, 0xc00bbf, 0xe00bfb,
  134.  0x1000aaf, 0x1400bfa, 0x1600b9f, 0x1800bf9, 0x1a00bf8, 0x1c00a8f, 0x2000a7f, 0x2400af7,
  135.  0x2800a6f, 0x2c00af6, 0x30008ff, 0x4000a5f, 0x4400af5, 0x480094f, 0x50009f4, 0x58009f3,
  136.  0x60009f0, 0x6800a3f, 0x6c010ce, 0x6c111ec, 0x6c191dd, 0x6c20fde, 0x6c40fe9, 0x6c610ea,
  137.  0x6c710d9, 0x6c80eee, 0x6cc0fed, 0x6ce0feb, 0x6d00ebe, 0x6d40ecd, 0x6d80fdc, 0x6da0fdb,
  138.  0x6dc0eae, 0x6e00ecc, 0x6e40fad, 0x6e60fda, 0x6e80f7e, 0x6ea0fac, 0x6ec0eca, 0x6f00fc9,
  139.  0x6f20f7d, 0x6f40e5e, 0x6f80dbd, 0x70008f2, 0x800092f, 0x880090f, 0x900081f, 0xa0008f1,
  140.  0xb000d9e, 0xb080ebc, 0xb0c0ecb, 0xb100e8e, 0xb140ee8, 0xb180e9d, 0xb1c0ee7, 0xb200ebb,
  141.  0xb240e8d, 0xb280ed8, 0xb2c0e6e, 0xb300de6, 0xb380d9c, 0xb400eab, 0xb440eba, 0xb480ee5,
  142.  0xb4c0ed7, 0xb500d4e, 0xb580ee4, 0xb5c0e8c, 0xb600dc8, 0xb680d3e, 0xb700d6d, 0xb780ed6,
  143.  0xb7c0e9b, 0xb800eb9, 0xb840eaa, 0xb880de1, 0xb900dd4, 0xb980eb8, 0xb9c0ea9, 0xba00d7b,
  144.  0xba80eb7, 0xbac0ed0, 0xbb00ce3, 0xbc00d0e, 0xbc80de0, 0xbd00d5d, 0xbd80dd5, 0xbe00d7c,
  145.  0xbe80dc7, 0xbf00d4d, 0xbf80d8b, 0xc000d9a, 0xc080d6c, 0xc100dc6, 0xc180d3d, 0xc200d5c,
  146.  0xc280dc5, 0xc300c0d, 0xc400d8a, 0xc480da8, 0xc500d99, 0xc580d4c, 0xc600db6, 0xc680d7a,
  147.  0xc700c3c, 0xc800d5b, 0xc880d89, 0xc900c1c, 0xca00cc0, 0xcb00d98, 0xcb80d79, 0xcc00be2,
  148.  0xce00c2e, 0xcf00c1e, 0xd000cd3, 0xd100c2d, 0xd200cd2, 0xd300cd1, 0xd400c3b, 0xd500d97,
  149.  0xd580d88, 0xd600b1d, 0xd800cc4, 0xd900c6b, 0xda00cc3, 0xdb00ca7, 0xdc00b2c, 0xde00cc2,
  150.  0xdf00cb5, 0xe000cc1, 0xe100c0c, 0xe200c4b, 0xe300cb4, 0xe400c6a, 0xe500ca6, 0xe600bb3,
  151.  0xe800c5a, 0xe900ca5, 0xea00b2b, 0xec00bb2, 0xee00b1b, 0xf000bb1, 0xf200c0b, 0xf300cb0,
  152.  0xf400c69, 0xf500c96, 0xf600c4a, 0xf700ca4, 0xf800c78, 0xf900c87, 0xfa00ba3, 0xfc00c3a,
  153.   0xfd00c59, 0xfe00b2a, 0x10000c95, 0x10100c68, 0x10200ba1, 0x10400c86, 0x10500c77, 0x10600b94,
  154.  0x10800c49, 0x10900c57, 0x10a00b67, 0x10c00aa2, 0x11000a1a, 0x11400b0a, 0x11600ba0, 0x11800b39,
  155.  0x11a00b93, 0x11c00b58, 0x11e00b85, 0x12000a29, 0x12400a92, 0x12800b76, 0x12a00b09, 0x12c00a19,
  156.  0x13000a91, 0x13400b90, 0x13600b48, 0x13800b84, 0x13a00b75, 0x13c00b38, 0x13e00b83, 0x14000b66,
  157.  0x14200b28, 0x14400a82, 0x14800b47, 0x14a00b74, 0x14c00a18, 0x15000a81, 0x15400a80, 0x15800b08,
  158.  0x15a00b56, 0x15c00a37, 0x16000a73, 0x16400b65, 0x16600b46, 0x16800a27, 0x16c00a72, 0x17000b64,
  159.  0x17200b55, 0x17400a07, 0x17800917, 0x18000971, 0x18800a70, 0x18c00a36, 0x19000a63, 0x19400a45,
  160.  0x19800a54, 0x19c00a26, 0x1a000962, 0x1a800916, 0x1b000961, 0x1b800a06, 0x1bc00a60, 0x1c000953,
  161.  0x1c800a35, 0x1cc00a44, 0x1d000925, 0x1d800952, 0x1e000851, 0x1f000915, 0x1f800905, 0x20000934,
  162.  0x20800943, 0x21000950, 0x21800924, 0x22000942, 0x22800933, 0x23000814, 0x24000841, 0x25000904,
  163.  0x25800940, 0x26000823, 0x27000832, 0x28000713, 0x2a000731, 0x2c000803, 0x2d000830, 0x2e000722,
  164.  0x30000612, 0x34000621, 0x38000602, 0x3c000620, 0x40000411, 0x50000401, 0x60000310, 0x80000100};
  165. static U32 h24[256]={ 0x8ef, 0x10008fe, 0x20008df, 0x30008fd, 0x40008cf, 0x50008fc, 0x60008bf, 0x70008fb,
  166.  0x80007fa, 0xa0008af, 0xb00089f, 0xc0007f9, 0xe0007f8, 0x1000088f, 0x1100087f, 0x120007f7,
  167.  0x1400076f, 0x160007f6, 0x1800075f, 0x1a0007f5, 0x1c00074f, 0x1e0007f4, 0x2000073f, 0x220007f3,
  168.  0x2400072f, 0x260007f2, 0x280007f1, 0x2a00081f, 0x2b0008f0, 0x2c00090f, 0x2c800bee, 0x2ca00bde,
  169.  0x2cc00bed, 0x2ce00bce, 0x2d000bec, 0x2d200bdd, 0x2d400bbe, 0x2d600beb, 0x2d800bcd, 0x2da00bdc,
  170.  0x2dc00bae, 0x2de00bea, 0x2e000bbd, 0x2e200bdb, 0x2e400bcc, 0x2e600b9e, 0x2e800be9, 0x2ea00bad,
  171.  0x2ec00bda, 0x2ee00bbc, 0x2f000bcb, 0x2f200b8e, 0x2f400be8, 0x2f600b9d, 0x2f800bd9, 0x2fa00b7e,
  172.  0x2fc00be7, 0x2fe00bac, 0x300004ff, 0x40000bca, 0x40200bbb, 0x40400b8d, 0x40600bd8, 0x40800c0e,
  173.  0x40900ce0, 0x40a00b0d, 0x40c00ae6, 0x41000b6e, 0x41200b9c, 0x41400ac9, 0x41800a5e, 0x41c00aba,
  174.  0x42000ae5, 0x42400bab, 0x42600b7d, 0x42800ad7, 0x42c00ae4, 0x43000a8c, 0x43400ac8, 0x43800b4e,
  175.  0x43a00b2e, 0x43c00a3e, 0x44000a6d, 0x44400ad6, 0x44800ae3, 0x44c00a9b, 0x45000ab9, 0x45400aaa,
  176.  0x45800ae2, 0x45c00a1e, 0x46000ae1, 0x46400a5d, 0x46800ad5, 0x46c00a7c, 0x47000ac7, 0x47400a4d,
  177.  0x47800a8b, 0x47c00ab8, 0x48000ad4, 0x48400a9a, 0x48800aa9, 0x48c00a6c, 0x49000ac6, 0x49400a3d,
  178.  0x49800ad3, 0x49c00a2d, 0x4a000ad2, 0x4a400a1d, 0x4a800a7b, 0x4ac00ab7, 0x4b000ad1, 0x4b400a5c,
  179.  0x4b800ac5, 0x4bc00a8a, 0x4c000aa8, 0x4c400a99, 0x4c800a4c, 0x4cc00ac4, 0x4d000a6b, 0x4d400ab6,
  180.  0x4d800bd0, 0x4da00b0c, 0x4dc00a3c, 0x4e000ac3, 0x4e400a7a, 0x4e800aa7, 0x4ec00a2c, 0x4f000ac2,
  181.  0x4f400a5b, 0x4f800ab5, 0x4fc00a1c, 0x50000a89, 0x50400a98, 0x50800ac1, 0x50c00a4b, 0x51000bc0,
  182.  0x51200b0b, 0x51400a3b, 0x51800bb0, 0x51a00b0a, 0x51c00a1a, 0x520009b4, 0x52800a6a, 0x52c00aa6,
  183.  0x53000a79, 0x53400a97, 0x53800ba0, 0x53a00b09, 0x53c00a90, 0x540009b3, 0x54800988, 0x55000a2b,
  184.  0x55400a5a, 0x558009b2, 0x56000aa5, 0x56400a1b, 0x56800ab1, 0x56c00a69, 0x57000996, 0x578009a4,
  185.  0x58000a4a, 0x58400a78, 0x58800987, 0x5900093a, 0x598009a3, 0x5a000959, 0x5a800995, 0x5b00092a,
  186.   0x5b8009a2, 0x5c0009a1, 0x5c800968, 0x5d000986, 0x5d800977, 0x5e000949, 0x5e800994, 0x5f000939,
  187.  0x5f800993, 0x60000958, 0x60800985, 0x61000929, 0x61800967, 0x62000976, 0x62800992, 0x63000919,
  188.  0x63800991, 0x64000948, 0x64800984, 0x65000957, 0x65800975, 0x66000938, 0x66800983, 0x67000966,
  189.  0x67800928, 0x68000982, 0x68800918, 0x69000947, 0x69800974, 0x6a000981, 0x6a800a08, 0x6ac00a80,
  190.  0x6b000956, 0x6b800965, 0x6c000917, 0x6c800a07, 0x6cc00a70, 0x6d000873, 0x6e000937, 0x6e800927,
  191.  0x6f000872, 0x70000846, 0x71000864, 0x72000855, 0x73000871, 0x74000836, 0x75000863, 0x76000845,
  192.  0x77000854, 0x78000826, 0x79000862, 0x7a000816, 0x7b000861, 0x7c000906, 0x7c800960, 0x7d000835,
  193.  0x7e000853, 0x7f000844, 0x80000825, 0x81000852, 0x82000815, 0x83000905, 0x83800950, 0x84000751,
  194.  0x86000834, 0x87000843, 0x88000724, 0x8a000742, 0x8c000733, 0x8e000714, 0x90000741, 0x92000804,
  195.  0x93000840, 0x94000723, 0x96000732, 0x98000613, 0x9c000631, 0xa0000703, 0xa2000730, 0xa4000622,
  196.  0xa8000512, 0xb0000521, 0xb8000602, 0xbc000620, 0xc0000411, 0xd0000401, 0xe0000410, 0xf0000400};
  197. static U32 hA[16]={ 0x6b0, 0x40006f0, 0x80006d0, 0xc0006e0, 0x10000670, 0x14000650, 0x18000590, 0x20000560,
  198.  0x28000530, 0x300005a0, 0x380005c0, 0x40000420, 0x50000410, 0x60000440, 0x70000480, 0x80000100};
  199. static U32 hB[16]={  0x4f0, 0x100004e0, 0x200004d0, 0x300004c0, 0x400004b0, 0x500004a0, 0x60000490, 0x70000480,
  200.  0x80000470, 0x90000460, 0xa0000450, 0xb0000440, 0xc0000430, 0xd0000420, 0xe0000410, 0xf0000400};
  201. /* now the cues, remember to change these tables if you change N_CUE
  202. */
  203. static U8 h_cue[34][N_CUE]={
  204. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  205. {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3},
  206. {0,3,5,5,6,6,7,7,8,8,8,8,8,8,8,8},
  207. {0,3,5,5,6,6,6,6,7,7,7,7,8,8,8,8},
  208. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  209. {0,8,12,12,13,13,14,14,15,15,15,15,15,15,15,15},
  210. {0,5,7,9,10,11,12,12,13,13,13,13,14,14,15,15},
  211. {0,20,29,32,33,33,34,34,35,35,35,35,35,35,35,35},
  212. {0,23,30,31,32,32,32,32,33,33,34,34,35,35,35,35},
  213. {0,15,21,24,27,29,30,31,32,32,33,33,34,34,35,35},
  214. {0,42,56,60,61,61,62,62,63,63,63,63,63,63,63,63},
  215. {0,30,45,53,57,58,60,60,61,61,62,62,63,63,63,63},
  216. {0,23,37,46,50,54,56,57,58,60,61,61,62,62,63,63},
  217. {0,203,238,248,252,253,254,254,255,255,255,255,255,255,255,255},
  218. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  219. {0,132,178,205,223,233,240,245,248,250,252,252,253,254,255,255},
  220. {0,162,231,248,252,253,254,254,255,255,255,255,255,255,255,255},
  221. {0,162,231,248,252,253,254,254,255,255,255,255,255,255,255,255},
  222. {0,162,231,248,252,253,254,254,255,255,255,255,255,255,255,255},
  223. {0,162,231,248,252,253,254,254,255,255,255,255,255,255,255,255},
  224. {0,162,231,248,252,253,254,254,255,255,255,255,255,255,255,255},
  225. {0,162,231,248,252,253,254,254,255,255,255,255,255,255,255,255},
  226. {0,162,231,248,252,253,254,254,255,255,255,255,255,255,255,255},
  227. {0,162,231,248,252,253,254,254,255,255,255,255,255,255,255,255},
  228. {0,13,22,58,59,131,177,209,226,238,245,249,252,253,254,255},
  229. {0,13,22,58,59,131,177,209,226,238,245,249,252,253,254,255},
  230. {0,13,22,58,59,131,177,209,226,238,245,249,252,253,254,255},
  231. {0,13,22,58,59,131,177,209,226,238,245,249,252,253,254,255},
  232. {0,13,22,58,59,131,177,209,226,238,245,249,252,253,254,255},
  233. {0,13,22,58,59,131,177,209,226,238,245,249,252,253,254,255},
  234. {0,13,22,58,59,131,177,209,226,238,245,249,252,253,254,255},
  235. {0,13,22,58,59,131,177,209,226,238,245,249,252,253,254,255},
  236. {0,4,7,9,11,12,13,14,15,15,15,15,15,15,15,15},
  237. {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
  238. };
  239. static U32 *h_tables[34]={h0,h1,h2,h3,0,h5,h6,h7,h8,h9,h10,h11,h12,h13,0,h15,
  240. h16,h16,h16,h16,h16,h16,h16,h16,h24,h24,h24,h24,h24,h24,h24,h24,hA,hB};
  241. /*============================================================================*/
  242. /*============================================================================*/
  243. static int HuffmanTable_decode(int tbl, 
  244.     int *x, int *y, int *v, int *w)
  245. {
  246.     U32 *h_tab;
  247.     int lead;
  248.     int len,code;
  249.    ////
  250.   
  251.     h_tab = h_tables[tbl];
  252.     /* dataword is always leftaligned, not rightaligned */
  253.     lead = dataword >> (32 - NC_O); /* 19 - NC_O */
  254.     h_tab += h_cue[tbl][lead];  
  255.     
  256.     len = (*h_tab >> 8) & 0x1f;
  257.     
  258.     /* check for an immediate hit, so we can decode short codes very fast
  259.      */
  260.     if ((*h_tab >> (32 - len)) != dataword >> (32 - len)) 
  261. {
  262. int dir_flag;
  263. int lag;
  264. U32 chunk = (dataword & 0xffffe000) | 0x1ff; /* WHY */
  265. ////
  266. lag = h_cue[tbl][lead + 1] - h_cue[tbl][lead];
  267. dir_flag = 0;
  268. while(lag > 1)
  269. {
  270.   if (!dir_flag)
  271.   {
  272. h_tab += lag >> 1;
  273.   }
  274.   else
  275.   {
  276. h_tab -= lag >> 1;
  277.   }
  278.   ////
  279.   
  280.   if(*h_tab > chunk)
  281.   {
  282. if(!dir_flag)
  283. {
  284.     lag >>= 1;
  285. }
  286. else
  287. {
  288.     lag -= lag >> 1;
  289. }
  290. dir_flag =- 1;
  291.   }
  292.   else
  293.   {
  294. if (!dir_flag)
  295. {
  296.     lag -= lag >> 1;
  297. }
  298. else
  299. {
  300.     lag >>= 1;
  301. }
  302. dir_flag = 0;
  303.   }
  304. }
  305. ////
  306. len = (*h_tab >> 8) & 0x1f;
  307. if((*h_tab >> (32 - len)) != (chunk >> (32 - len)))
  308. {
  309.     if (!dir_flag)
  310.     {
  311. h_tab++;
  312. }
  313.     else
  314.     {
  315. h_tab--;
  316. }
  317.     len = (*h_tab >> 8) & 0x1f;
  318. }
  319.     
  320. }
  321. ////
  322. dataword_len -= len;
  323.     dataword <<= len;  /* flush away the used bits */
  324.     /* fill up dataword */
  325. while(dataword_len <= 24)
  326.     {
  327.    dataword |= bs_buf[datapos++] << (24 - dataword_len);
  328.    dataword_len += 8;
  329.    datapos %=  BITSTREAM_BUFSIZE;
  330. }
  331. /* dataword now contains at least 24 bits */
  332. *x = (*h_tab >> 4) & 0xf;
  333. code=*x;
  334. // *y = *h_tab & 0xf;
  335.   
  336. *v = (*x >> 3) & 1;
  337.     *w = (*x >> 2) & 1;
  338. *y = *x & 1;                     //  先求X的值!!!!!!!
  339.     *x = (*x >> 1) & 1;
  340.     
  341. /* read and process the sign bits */
  342.     
  343. if(*v)
  344.     {
  345. if(dataword & 0x80000000)
  346. {
  347.   *v = - *v;
  348. }
  349.    
  350.     dataword <<= 1;
  351. dataword_len--;
  352. }
  353.     
  354. if(*w)
  355. {
  356.    if(dataword & 0x80000000)
  357.    {
  358.   *w = - *w;
  359.    }
  360.     
  361.    dataword <<= 1;
  362.    dataword_len--;
  363. }
  364.     
  365. if(*x)
  366. {
  367. if(dataword & 0x80000000)
  368. {
  369.   *x = - *x;
  370. }
  371.     
  372. dataword <<= 1;
  373. dataword_len--;
  374. }
  375.     
  376. if(*y)
  377. {
  378. if(dataword & 0x80000000)
  379. {
  380.   *y = - *y;
  381. }
  382.     
  383. dataword <<= 1;
  384. dataword_len--;
  385.      }
  386.     
  387.     /* we have used up to 4 bits, so there are at least 20 bits
  388.        left in dataword, which is enough for the next run */
  389.     
  390.     return code;
  391. }
  392. /*============================================================================*/
  393. /*============================================================================*/
  394. void III_hufman_decode(struct Granule *gr,int part2_start,
  395.          int freqline[SBLIMIT][SSLIMIT])
  396. {
  397.     int reg1, reg2,i; 
  398. int part3_length = part2_start + gr->part2_3_length;
  399. int used;
  400.     int h,*f=&freqline[0][0];
  401. ////
  402.     if(gr->window_switching_flag &&
  403.        gr->block_type == 2)
  404. {
  405. /* short block regions */
  406.   reg1 = 36;  
  407.   reg2 = 576;
  408.     }
  409. else
  410. {
  411. /* long block regions */
  412.   reg1 = sfBandIndex[fr_ps.header->sampling_frequency].l[gr->region0_count + 1];
  413.   reg2 = sfBandIndex[fr_ps.header->sampling_frequency].l[gr->region0_count + gr->region1_count + 2];
  414.     }
  415.     /* fill up dataword, end at a bytealign in the buffer */
  416.     {
  417. int bitpos = totpos & 7;    //取8的余数
  418. datapos = (totpos>> 3) & (BITSTREAM_BUFSIZE - 1);
  419. /* the first read might not be bytealigned so shift it in place */
  420. /* we have duplicated the last 4 bytes in the stream so it doesnt
  421.    matter that datapos+2 etc dont pass through modulo */
  422. dataword = bs_buf[datapos++] << 24   ;                     //左移3个字节
  423. datapos  %=  BITSTREAM_BUFSIZE;
  424. dataword|= bs_buf[datapos++] << 16 ;
  425. datapos  %=  BITSTREAM_BUFSIZE;
  426. dataword|= bs_buf[datapos++] << 8  ;
  427. datapos  %= BITSTREAM_BUFSIZE;
  428. dataword|= bs_buf[datapos++]       ;
  429. datapos  %= BITSTREAM_BUFSIZE;
  430. dataword <<= bitpos;  /* strip the misaligned leading bits  再左移bitpos位*/
  431. dataword_len = 32 - bitpos;  /* number of valid bits in dataword  有效位的长度*/
  432. /* now dataword is loaded with at least 24 bits of data */
  433.     }
  434.     part3_length -= totpos;    //计算部分2的结束位置
  435.     /* read the big values, they come in pairs */
  436.     h = gr->table_select[0];  /* start with the first table */
  437.     for(i = 0; i < gr->big_values *2; i += 2)
  438. {
  439. U32 *h_tab;
  440. int lead;
  441. int l, len, x, y;
  442. ////
  443. if(i == reg1)
  444.     h = gr->table_select[1];
  445. else if(i == reg2)
  446.     h = gr->table_select[2];
  447.     h_tab = h_tables[h];
  448. /* dataword is always leftaligned, not rightaligned */
  449. lead = dataword >> (32 - NC_O); /* 19 - nc_o */
  450. h_tab += h_cue[h][lead];  
  451. len = (*h_tab >> 8) & 0x1f;
  452. /* check for an immediate hit, so we can decode short codes very fast
  453.  */
  454. if (((*h_tab>>(32-len))!=(dataword>>(32-len)))&&(len))
  455. {
  456.   int dir_flag;
  457.   int lag;
  458.   U32 chunk = (dataword & 0xffffe000) | 0x1ff; /* WHY */
  459.   lag = h_cue[h][lead + 1] - h_cue[h][lead];
  460.   dir_flag = 0;
  461.   while(lag > 1)
  462.   {
  463.     if (!dir_flag)
  464.       h_tab += lag >> 1;
  465.     else
  466.       h_tab -= lag >> 1;
  467.     if(*h_tab > chunk)
  468. {
  469.     if(!dir_flag)
  470.     lag >>= 1;
  471.     else
  472.     lag -= lag >> 1;
  473.     dir_flag =- 1;
  474. }
  475. else
  476. {
  477.     if (!dir_flag)
  478.    lag -= lag >> 1;
  479.     else
  480.    lag >>= 1;
  481.     dir_flag = 0;
  482. }
  483.   }
  484.       len = (*h_tab >> 8) & 0x1f;
  485.       if((*h_tab >> (32 - len)) != (chunk >> (32 - len)))
  486.   {
  487.   if (!dir_flag)
  488.     h_tab++;
  489.   else
  490.     h_tab--;
  491.   len = (*h_tab >> 8) & 0x1f;
  492.   }
  493. }
  494. dataword_len -= len;
  495. dataword <<= len;  /* flush away the used bits */
  496. /* fill up dataword */
  497. while(dataword_len <= 24)
  498. {
  499.     dataword |= bs_buf[datapos++] << (24 - dataword_len);
  500.     dataword_len += 8;
  501.       datapos  %= BITSTREAM_BUFSIZE;    //保证取数据的位置,防止溢出
  502. }
  503. /* dataword now contains at least 24 bits */
  504. x = (*h_tab >> 4) & 0xf;
  505. y = *h_tab & 0xf;
  506. l = t_linbits[h];
  507. /* linbits are used when values larger than 15 has to be encoded */
  508. /* l is 13 or less, and we know we have at least 24 bits of data
  509.    in the dataword, so use that directly */
  510. if(x == 15 && l > 0)
  511. {
  512.     x += dataword >> (32 - l);
  513.     dataword <<= l;
  514.     dataword_len -= l;
  515.     /* better refill since we might have used 13 bits */
  516.     while(dataword_len <= 24)
  517.     {
  518.   dataword |= bs_buf[datapos++] << (24 - dataword_len);
  519.   dataword_len += 8;
  520.       datapos = datapos % BITSTREAM_BUFSIZE;//保证取数据的位置,防止溢出
  521.     }
  522. }
  523. /* read sign-bit for x */
  524. if(x)
  525. {
  526.     if(dataword & 0x80000000)
  527.     {
  528.      x = - x;
  529.     }
  530.     dataword <<= 1;
  531.     dataword_len--;
  532. }
  533. if(y == 15 && l > 0)
  534. {
  535.     y += dataword >> (32 - l);
  536.     dataword <<= l; 
  537.     dataword_len -= l;
  538.     /* better refill since we might have used 13 bits */
  539.     while(dataword_len <= 24)
  540.     {
  541.     dataword |= bs_buf[datapos++] << (24 - dataword_len);
  542.   dataword_len += 8;
  543.       datapos %=  BITSTREAM_BUFSIZE;//保证取数据的位置,防止溢出
  544.     }
  545. }
  546. /* read sign-bit for y */
  547. if(y)
  548. {
  549.     if(dataword & 0x80000000)
  550.      y = - y;
  551.     dataword <<= 1;
  552.     dataword_len--;
  553. }
  554. *f++=x;
  555. *f++=y;
  556. }
  557. /* there are enough bits in dataword for the next run */
  558. /*  BIGVALUES 全部计算完 */
  559.     /* Now read the count1 values, they come in quadruples, and we
  560.        don't know explicetely how many there are but we are supposed to
  561.        keep decoding until all huffman bits have been decoded or until
  562.        all frequency lines have been assigned (i == 576). 
  563.     */
  564.             
  565.   /* choose count1 table */
  566.   used=BITS_USED;
  567.       h = gr->count1table_select + 32;  
  568.       while(BITS_USED < part3_length && i < 576)
  569.   {
  570.     HuffmanTable_decode(h,f + 2, f + 3, f, f + 1);
  571.     f += 4;
  572.     i += 4;
  573.   }
  574.       used = BITS_USED;
  575.   hgetbits(used);
  576.   /* rewind if we got a bit too far, and discard if we got too short  */
  577.       if(used>part3_length)                    //保证totpos是part3_length长度的累加
  578.   {
  579.      i -= 4;
  580.      rewindNbits(used - part3_length);
  581.   }
  582.    /*   相等  totpos 不变  */
  583.   else if(used<part3_length)
  584.   {
  585.  hgetbits(part3_length-used);    //抛弃掉
  586.   }
  587.   /* the rest of the frequency lines are zero */
  588.       for(; i < 576; i++)
  589.       *f++=0;
  590. }
  591. /*============================================================================*/
  592. void hputbuf(U32 val)
  593. {
  594. bs_buf[offset % BITSTREAM_BUFSIZE] = val;
  595. offset++;
  596. }
  597. /*============================================================================*/
  598. /*read N bit from the bit stream */
  599. const static int putmask1[9]={0x0, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff};
  600. U32 hgetbits(int N)
  601. {
  602. int  val=0;
  603. register int j = N;
  604. register int tmp;
  605. int k;
  606. /*
  607.  if (N > MAX_LENGTH)
  608.  printf("Cannot read or write more than %d bits at a time.n", MAX_LENGTH);
  609. */
  610. totpos += N;
  611. while (j > 0)
  612. {
  613. if (!buf_bit_idx)
  614. {
  615. buf_bit_idx = 8;
  616. buf_byte_idx++;
  617. }
  618. k = MIN(j, (int)buf_bit_idx);
  619. tmp = bs_buf[buf_byte_idx%BITSTREAM_BUFSIZE]&putmask1[buf_bit_idx];
  620. tmp = tmp >> (buf_bit_idx-k);
  621. val |= tmp << (j-k);
  622. buf_bit_idx -= k;
  623. j -= k;
  624. }
  625. return(val);
  626. }
  627. /*============================================================================*/
  628. void rewindNbits(int N)
  629. {
  630. totpos -= N;
  631. buf_bit_idx += N;
  632. while( buf_bit_idx >= 8 )
  633. {
  634. buf_bit_idx -= 8;
  635. buf_byte_idx--;
  636. }
  637. }
  638. /*============================================================================*/
  639. void rewindNbytes(int N)
  640. {
  641. totpos -= N*8;
  642. buf_byte_idx -= N;
  643. }
  644. /*============================================================================*/
  645. /*============================================================================*/