home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -seriously_amiga- / sound / warpamp-ahi / huffman.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-09  |  6.2 KB  |  213 lines

  1.  
  2. /* this file is a part of amp software, (C) tomislav uzelac 1996,1997
  3. */
  4.  
  5. /* huffman.c  huffman decoding
  6.  *
  7.  * Created by: tomislav uzelac  Mar,Apr 1996
  8.  * Last modified by: tomislav uzelac Mar  8 97
  9.  */
  10. #include "amiga.h"
  11. #include "audio.h"
  12. #include "getbits.h"
  13.  
  14. #define HUFFMAN
  15. #include "huffman.h"
  16.  
  17. extern int output;
  18.  
  19. static unsigned int viewbits(int n)
  20. {
  21. unsigned int pos,ret_value;
  22.  
  23.         pos = data >> 3;
  24.         ret_value = buffer[pos] << 24 |
  25.                     buffer[pos+1] << 16 |
  26.                     buffer[pos+2] << 8 |
  27.                     buffer[pos+3];
  28.         ret_value <<= data & 7;
  29.         ret_value >>= 32 - n;
  30.  
  31.         return ret_value;
  32. }
  33.  
  34. static void sackbits(int n)
  35. {
  36.         data += n;
  37.         data &= 8*BUFFER_SIZE-1;
  38. }
  39.  
  40. /* huffman_decode() is supposed to be faster now
  41.  * decodes one codeword and returns no. of bits
  42.  */
  43. static int huffman_decode(int tbl,int *x,int *y)
  44. {
  45. unsigned int chunk;
  46. register unsigned int *h_tab;
  47. register unsigned int lag;
  48. register unsigned int half_lag;
  49. int len;
  50.  
  51.         h_tab=tables[tbl];
  52.         chunk=viewbits(19);
  53.  
  54.         h_tab += h_cue[tbl][chunk >> (19-NC_O)];
  55.  
  56.         len=(*h_tab>>8)&0x1f;
  57.  
  58.         /* check for an immediate hit, so we can decode those short codes very fast
  59.         */
  60.         if ((*h_tab>>(32-len)) != (chunk>>(19-len))) {
  61.                 if (chunk >> (19-NC_O) < N_CUE-1)
  62.                   lag=(h_cue[tbl][(chunk >> (19-NC_O))+1] -
  63.                        h_cue[tbl][chunk >> (19-NC_O)]);
  64.                 else {
  65.                         /* we strongly depend on h_cue[N_CUE-1] to point to
  66.                          * the last entry in the huffman table, so we should
  67.                          * not get here anyway. if it didn't, we'd have to
  68.                          * have another table with huffman tables lengths, and
  69.                          * it would be a mess. just in case, scream&shout.
  70.                          */ 
  71.                         if(output) printf(" h_cue clobbered. this is a bug. blip.\n");
  72.                         exit (-1);
  73.                 }
  74.                 chunk <<= 32-19;
  75.                 chunk |= 0x1ff;
  76.  
  77.                 half_lag = lag >> 1;
  78.  
  79.                 h_tab += half_lag;
  80.                 lag -= half_lag;
  81.  
  82.                 while (lag > 1) {
  83.                         half_lag = lag >> 1;
  84.  
  85.                         if (*h_tab < chunk)
  86.                                 h_tab += half_lag;
  87.                         else
  88.                                 h_tab -= half_lag;
  89.  
  90.                         lag -= half_lag;
  91.                 }
  92.  
  93.                 len=(*h_tab>>8)&0x1f;
  94.                 if ((*h_tab>>(32-len)) != (chunk>>(32-len))) {
  95.                         if (*h_tab > chunk)
  96.                                 h_tab--;
  97.                         else 
  98.                                 h_tab++;
  99.                   
  100.                         len=(*h_tab>>8)&0x1f;
  101.                 }
  102.         }
  103.         sackbits(len);
  104.         *x=(*h_tab>>4)&0xf;
  105.         *y=*h_tab&0xf;
  106.         return len;
  107. }
  108.  
  109. static int _qsign(int x,int *q)
  110. {
  111. int ret_value=0,i;
  112.         for (i=3;i>=0;i--) 
  113.                 if ((x>>i) & 1) {
  114.                         if (getbits(1)) *q++=-1;
  115.                                 else *q++=1;
  116.                         ret_value++;
  117.                 }
  118.                 else *q++=0;
  119.         return ret_value;
  120. }               
  121.  
  122. int decode_huffman_data(struct SIDE_INFO *info,int gr,int ch,int ssize)
  123. {
  124. int l,i,cnt,x,y;
  125. int q[4],r[3],linbits[3],tr[4]={0,0,0,0};
  126. int big_value = info->big_values[gr][ch] << 1;
  127.  
  128.         for (l=0;l<3;l++) {
  129.                 tr[l]=info->table_select[gr][ch][l];
  130.                 linbits[l]=t_linbits[info->table_select[gr][ch][l]];
  131.         }
  132.  
  133.         tr[3]=32+info->count1table_select[gr][ch];
  134.  
  135.         /* we have to be careful here because big_values are not necessarily
  136.          * aligned with sfb boundaries
  137.          */
  138.         if (!info->window_switching_flag[gr][ch] && info->block_type[gr][ch]==0) {
  139.  
  140.         /* this code needed some cleanup
  141.         */
  142.                 r[0]=t_l[info->region0_count[gr][ch]] + 1;
  143.                 if (r[0] > big_value)
  144.                         r[0]=r[1]=big_value;
  145.                 else {
  146.                         r[1]=t_l[ info->region0_count[gr][ch] + info->region1_count[gr][ch] + 1 ] + 1;
  147.                         if (r[1] > big_value)
  148.                                 r[1]=big_value;
  149.                 }
  150.                 r[2]=big_value;
  151.  
  152.         } else {
  153.  
  154.                 if (info->block_type[gr][ch]==2 && info->mixed_block_flag[gr][ch]==0) 
  155.                         r[0]=3*(t_s[2]+1);
  156.                 else 
  157.                         r[0]=t_l[7]+1;
  158.  
  159.                 if (r[0] > big_value)
  160.                         r[0]=big_value;
  161.  
  162.                 r[1]=r[2]=big_value;
  163.         }
  164.  
  165.         l=0; cnt=0;
  166.         for (i=0;i<3;i++) {
  167.                 for (;l<r[i];l+=2) {
  168.                         int j = linbits[i];
  169.  
  170.                         cnt+=huffman_decode(tr[i],&x,&y);
  171.  
  172.                         if (x==15 && j>0) {
  173.                                 x+=getbits(j);
  174.                                 cnt+=j;
  175.                         }
  176.                         if (x) {
  177.                                 if (getbits(1)) x=-x;
  178.                                 cnt++;
  179.                         }
  180.                         if (y==15 && j>0) {
  181.                                 y+=getbits(j);
  182.                                 cnt+=j;
  183.                         }
  184.                         if (y) {
  185.                                 if (getbits(1)) y=-y;
  186.                                 cnt++;
  187.                         }
  188.  
  189.                         is[ch][l]=x;
  190.                         is[ch][l+1]=y;
  191.                 }
  192.         }
  193.         while ((cnt < info->part2_3_length[gr][ch]-ssize) && (l<576)) {
  194.                 cnt+=huffman_decode(tr[3],&x,&y);
  195.                 cnt+=_qsign(x,q);
  196.                 for (i=0;i<4;i++) is[ch][l+i]=q[i]; /* ziher je ziher, is[578]*/
  197.                 l+=4;
  198.         }
  199.  
  200.         /*  set position to start of the next gr/ch
  201.          */
  202.         if (cnt != info->part2_3_length[gr][ch] - ssize ) {
  203.                 data-=cnt-(info->part2_3_length[gr][ch] - ssize);
  204.                 data&= 8*BUFFER_SIZE - 1;
  205.         }
  206.         if (l<576) non_zero[ch]=l;
  207.         else non_zero[ch]=576;
  208.         /* zero out everything else
  209.         */
  210.         for (;l<576;l++) is[ch][l]=0;
  211.         return 1;
  212. }
  213.