Subversion Repositories Kolibri OS

Rev

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

  1. /* max bits required for any lookup - change if htable changes */
  2. /* quad required 10 bit w/signs  must have (MAXBITS+2) >= 10   */
  3. #define MAXBITS 9
  4.  
  5. static unsigned int bitbuf;
  6. static int bits;
  7. static unsigned char *bs_ptr;
  8. static unsigned char *bs_ptr0;
  9. static unsigned char *bs_ptr_end;       // optional for overrun test
  10.  
  11. void bitget_init(unsigned char *buf)
  12. {
  13.         bs_ptr0 = bs_ptr = buf;
  14.         bits = 0;
  15.         bitbuf = 0;
  16. }
  17.  
  18. int bitget(int n)
  19. {
  20.         unsigned int x;
  21.  
  22.         if (bits < n)
  23.         {                               /* refill bit buf if necessary */
  24.                 while (bits <= 24)
  25.                 {
  26.                         bitbuf = (bitbuf << 8) | *bs_ptr++;
  27.                         bits += 8;
  28.                 }
  29.         }
  30.         bits -= n;
  31.         x = bitbuf >> bits;
  32.         bitbuf -= x << bits;
  33.         return x;
  34. }
  35.  
  36. void bitget_skip(int n)
  37. {
  38.    unsigned int k;
  39.  
  40.    if (bits < n)
  41.    {
  42.       n -= bits;
  43.       k = n >> 3;
  44. /*--- bytes = n/8 --*/
  45.       bs_ptr += k;
  46.       n -= k << 3;
  47.       bitbuf = *bs_ptr++;
  48.       bits = 8;
  49.    }
  50.    bits -= n;
  51.    bitbuf -= (bitbuf >> bits) << bits;
  52. }
  53.  
  54. void bitget_init_end(unsigned char *buf_end)
  55. {
  56.    bs_ptr_end = buf_end;
  57. }
  58.  
  59. /*------------- check overrun -------------*/
  60. int bitget_overrun()
  61. {
  62.         return bs_ptr > bs_ptr_end;
  63. }
  64. /*------------- get n bits from bitstream -------------*/
  65. int bitget_bits_used()
  66. {
  67.    unsigned int n;                      /* compute bits used from last init call */
  68.  
  69.    n = ((bs_ptr - bs_ptr0) << 3) - bits;
  70.    return n;
  71. }
  72. /*------------- check for n bits in bitbuf -------------*/
  73. void bitget_check(int n)
  74. {
  75.    if (bits < n)
  76.    {
  77.       while (bits <= 24)
  78.       {
  79.          bitbuf = (bitbuf << 8) | *bs_ptr++;
  80.          bits += 8;
  81.       }
  82.    }
  83. }
  84.  
  85. #if 0
  86. /*------------- get 1 bit from bitstream -------------*/
  87. int bitget_1bit()
  88. {
  89.    unsigned int x;
  90.  
  91.    if (bits <= 0)
  92.    {                            /* refill bit buf if necessary */
  93.       while (bits <= 24)
  94.       {
  95.          bitbuf = (bitbuf << 8) | *bs_ptr++;
  96.          bits += 8;
  97.       }
  98.    }
  99.    bits--;
  100.    x = bitbuf >> bits;
  101.    bitbuf -= x << bits;
  102.    return x;
  103. }
  104.  
  105. /*------------- get 1 bit from bitstream NO CHECK -------------*/
  106. int bitget_1bit()
  107. {
  108.    unsigned int x;
  109.  
  110.    bits--;
  111.    x = bitbuf >> bits;
  112.    bitbuf -= x << bits;
  113.    return x;
  114. }
  115. #endif
  116. /* only huffman */
  117.  
  118. /*----- get n bits  - checks for n+2 avail bits (linbits+sign) -----*/
  119. int bitget_lb(int n)
  120. {
  121.    unsigned int x;
  122.  
  123.    if (bits < (n + 2))
  124.    {                            /* refill bit buf if necessary */
  125.       while (bits <= 24)
  126.       {
  127.          bitbuf = (bitbuf << 8) | *bs_ptr++;
  128.          bits += 8;
  129.       }
  130.    }
  131.    bits -= n;
  132.    x = bitbuf >> bits;
  133.    bitbuf -= x << bits;
  134.    return x;
  135. }
  136.  
  137. /*------------- get n bits but DO NOT remove from bitstream --*/
  138. int bitget2(int n)
  139. {
  140.    unsigned int x;
  141.  
  142.    if (bits < (MAXBITS + 2))
  143.    {                            /* refill bit buf if necessary */
  144.       while (bits <= 24)
  145.       {
  146.          bitbuf = (bitbuf << 8) | *bs_ptr++;
  147.          bits += 8;
  148.       }
  149.    }
  150.    x = bitbuf >> (bits - n);
  151.    return x;
  152. }
  153.  
  154. /*------------- remove n bits from bitstream ---------*/
  155. void bitget_purge(int n)
  156. {
  157.    bits -= n;
  158.    bitbuf -= (bitbuf >> bits) << bits;
  159. }
  160.  
  161. void mac_bitget_check(int n)
  162. {
  163.         if( bits < n ) {
  164.                 while( bits <= 24 ) {
  165.                         bitbuf = (bitbuf << 8) | *bs_ptr++;
  166.                         bits += 8;
  167.                 }
  168.         }
  169. }
  170.  
  171. int mac_bitget(int n)
  172. {
  173.         unsigned int code;
  174.  
  175.         bits -= n;
  176.         code  = bitbuf >> bits;
  177.         bitbuf -= code << bits;
  178.     return code;
  179. }
  180.  
  181. int mac_bitget2(int n)
  182. {
  183.         return (bitbuf >> (bits-n));
  184. }
  185.  
  186. int mac_bitget_1bit()
  187. {
  188.         unsigned int code;
  189.  
  190.         bits--;
  191.         code  = bitbuf >> bits;
  192.         bitbuf -= code << bits;
  193.         return code;
  194. }
  195.  
  196. void mac_bitget_purge(int n)
  197. {
  198.         bits -= n;
  199.     bitbuf -= (bitbuf >> bits) << bits;
  200. }
  201.  
  202. /*
  203. #define mac_bitget(n) ( bits -= n,           \
  204.          code  = bitbuf >> bits,     \
  205.          bitbuf -= code << bits,     \
  206.          code )
  207.  
  208. #define mac_bitget_1bit() ( bits--,                           \
  209.          code  = bitbuf >> bits,    \
  210.          bitbuf -= code << bits,  \
  211.          code )
  212. */