home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 Secrets / Secrets2.iso / Audio / WAV / MaplayP / _SETUP.1 / bit_res.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-18  |  2.0 KB  |  109 lines

  1. /* bit_res.cpp
  2.  
  3.     Implementation of Bit Reservoir for Layer III
  4.  
  5.    Adapted from the public c code by Jeff Tsay. */
  6.  
  7. #include "all.h"
  8. #include "bit_res.h"
  9.  
  10. Bit_Reserve::Bit_Reserve()
  11. {
  12.     uint32 shifted_one = 1;
  13.  
  14.     offset       = 0;
  15.    totbit         = 0;
  16.    buf_byte_idx = 0;
  17.     buf          = new uint32[BUFSIZE];
  18.     buf_bit_idx  = 8;
  19.     putmask      = new uint32[32];
  20.  
  21.    putmask[0] = 0;
  22.  
  23.    for (int32 i=1; i<32; i++)
  24.    {
  25.        putmask[i] = putmask[i-1] + shifted_one;
  26.       shifted_one <<= 1;
  27.    }
  28. }
  29.  
  30. Bit_Reserve::~Bit_Reserve ()
  31. {
  32.     delete [] putmask;
  33.     delete [] buf;
  34. }
  35.  
  36. uint32 Bit_Reserve::hgetbits(uint32 N)
  37. // read N bits from the bit stream
  38. {
  39.    totbit += N;
  40.  
  41.    uint32 val = 0;
  42.    int32 j = N;
  43.  
  44.    while (j > 0) {
  45.  
  46.       if (buf_bit_idx == 0) {
  47.          buf_bit_idx = 8;
  48.          buf_byte_idx++;
  49. /*    if (buf_byte_idx > offset)
  50.       { printf("Buffer overflow !!\n");exit(3); } */
  51.      }
  52.  
  53.       int32 k = (j < buf_bit_idx) ? j : buf_bit_idx;
  54.  
  55.    // BUFSIZE = 4096 = 2^12, so
  56.    // buf_byte_idx%BUFSIZE == buf_byte_idx & 0xfff
  57.      int32 tmp = buf[buf_byte_idx & 0xfff] & putmask[buf_bit_idx];
  58.      buf_bit_idx -= k;
  59.       tmp = tmp >> buf_bit_idx;
  60.      j -=k;
  61.      val |= tmp << j;
  62.    }
  63.    return(val);
  64. }
  65.  
  66. uint32 Bit_Reserve::hget1bit()
  67. // read 1 bit from the bit stream
  68. {
  69.    uint32 val;
  70.  
  71.    totbit++;
  72.  
  73.     if (buf_bit_idx == 0) {
  74.      buf_bit_idx = 8;
  75.       buf_byte_idx++;
  76.    }
  77.  
  78.    // BUFSIZE = 4096 = 2^12, so
  79.    // buf_byte_idx%BUFSIZE == buf_byte_idx & 0xfff
  80.    val = buf[buf_byte_idx & 0xfff] & putmask[buf_bit_idx];
  81.    buf_bit_idx--;
  82.     val = val >> buf_bit_idx;
  83.  
  84.    return(val);
  85. }
  86.  
  87. void Bit_Reserve::hputbuf(int32 val)
  88. // write 8 bits into the bit stream
  89. {
  90.    buf[offset] = val;
  91.    offset = (offset + 1) & 0xfff;
  92. }
  93.  
  94. void Bit_Reserve::rewindNbits(int32 N)
  95. {
  96.     totbit -= N;
  97.     buf_bit_idx += N;
  98.     while(buf_bit_idx >= 8) {
  99.        buf_bit_idx -= 8;
  100.         buf_byte_idx--;
  101.     }
  102. }
  103.  
  104. void Bit_Reserve::rewindNbytes(int32 N)
  105. {
  106.    totbit -= (N << 3);
  107.     buf_byte_idx -= N;
  108. }
  109.