home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / flaskmpeg / Audio / MPEG / AMPBitstream.cpp next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  4.1 KB  |  210 lines

  1. #include <crtdbg.h>
  2.  
  3. #include "AMPBitstream.h"
  4.  
  5. void AMPBitstream::resetbits(int bytes) {
  6.     bufindex = 0;
  7.     bitcnt = 24;
  8.     bitheap = 0;
  9.  
  10.     if (bytes) {
  11.         bufindexw = bufpoint = pSource->read(buf, bytes);
  12.  
  13.         if (bufpoint != bytes)
  14.             throw (int)IAMPDecoder::ERR_EOF;
  15.     } else
  16.         bufindexw = bufpoint = 0;
  17. }
  18.  
  19. void AMPBitstream::fillbits(int bytes) {
  20.     int actual;
  21.  
  22.     if (bufindexw + bytes > BUFFER_SIZE) {
  23.         actual = pSource->read(buf + bufindexw, BUFFER_SIZE - bufindexw);
  24.         if (actual != BUFFER_SIZE-bufindexw)
  25.             throw (int)IAMPDecoder::ERR_EOF;
  26.  
  27.         bufindexw = bufindexw+bytes-BUFFER_SIZE;
  28.  
  29.         actual = pSource->read(buf, bufindexw);
  30.  
  31.         if (actual != bufindexw)
  32.             throw (int)IAMPDecoder::ERR_EOF;
  33.  
  34.     } else {
  35.         actual = pSource->read(buf + bufindexw, bytes);
  36.  
  37.         if (actual != bytes)
  38.             throw (int)IAMPDecoder::ERR_EOF;
  39.  
  40.         bufindexw += actual;
  41.         if (bufindexw >= BUFFER_SIZE)
  42.             bufindexw -= BUFFER_SIZE;
  43.     }
  44.  
  45.     bufpoint += bytes;
  46.     if (bufpoint > BUFFER_SIZE)
  47.         bufpoint = BUFFER_SIZE;
  48. }
  49.  
  50. void AMPBitstream::rewind(int bytes) {
  51.     // restore bytes in buffer
  52.  
  53.     if (bitcnt <= 16)
  54.         bufpoint += (24-bitcnt)>>3;
  55. //        bufpoint += (24+7-bitcnt)>>3;
  56.  
  57. //    if (bufpoint < bytes)
  58. //        _RPT2(0,"\t\t\t------Not enough bytes! needed=%d, actual=%d\n", bytes, bufpoint);
  59.  
  60.     if (bufpoint < bytes)
  61.         throw (int)IAMPDecoder::ERR_INCOMPLETEFRAME;
  62.  
  63.     bitcnt = 24;
  64.     bitheap = 0;
  65.     bufindex = (bufindexw + BUFFER_SIZE - bytes) & BUFFER_MASK;
  66.     bufpoint = bytes;
  67. }
  68.  
  69. void AMPBitstream::rewindbits(int bits) {
  70.     long actualbits = tellbits();
  71.  
  72.     // allow up to 24 bits of oopsie
  73.  
  74.     if (bits > actualbits+24)
  75.         throw (int)IAMPDecoder::ERR_INCOMPLETEFRAME;
  76.  
  77.     if (bits == actualbits)
  78.         return;
  79.  
  80.     if (actualbits > bits && actualbits - bits <= 16)
  81.         getbits(actualbits - bits);
  82.     else {
  83.  
  84.         bitcnt = 24;
  85.         bitheap = 0;
  86.         bufpoint = ((bits+7)>>3);
  87.         bufindex = (bufindexw + BUFFER_SIZE - bufpoint) & BUFFER_MASK;
  88.  
  89.         if (bits & 7)
  90.             getbits(8-(bits & 7));
  91.     }
  92. }
  93.  
  94. int AMPBitstream::tellbits() {
  95.     return bufpoint*8 + (24-bitcnt);
  96. }
  97.  
  98. long AMPBitstream::_peekbits(unsigned char bits) {
  99.  
  100.  
  101.     if (!bits)
  102.         return 0;
  103.  
  104.     while(bitcnt >= 0 && bufpoint>0) {
  105.         --bufpoint;
  106.         bitheap += ((unsigned long)buf[bufindex++]) << bitcnt;
  107.         bufindex &= BUFFER_MASK;
  108.         bitcnt -= 8;
  109.     }
  110.  
  111.     if (bitcnt > 24-bits)
  112.         throw (int)IAMPDecoder::ERR_INCOMPLETEFRAME;
  113.  
  114.     return bitheap >> (32-bits);
  115. }
  116.  
  117. long AMPBitstream::_peekbits2(unsigned char bits) {
  118.  
  119.  
  120.     if (!bits)
  121.         return 0;
  122.  
  123.     while(bitcnt >= 0 && bufpoint>0) {
  124.         --bufpoint;
  125.         bitheap += ((unsigned long)buf[bufindex++]) << bitcnt;
  126.         bufindex &= BUFFER_MASK;
  127.         bitcnt -= 8;
  128.     }
  129.  
  130.     return bitheap >> (32-bits);
  131. }
  132.  
  133. long AMPBitstream::_getbits(unsigned char bits) {
  134.     long rv;
  135.  
  136.     if (!bits)
  137.         return 0;
  138.  
  139.     while(bitcnt >= 0 && bufpoint>0) {
  140.         --bufpoint;
  141.         bitheap += ((unsigned long)buf[bufindex++]) << bitcnt;
  142.         bufindex &= BUFFER_MASK;
  143.         bitcnt -= 8;
  144.     }
  145.  
  146.     if (bitcnt > 24-bits)
  147.         throw (int)IAMPDecoder::ERR_INCOMPLETEFRAME;
  148.  
  149.     rv = bitheap >> (32-bits);
  150.  
  151.     bitcnt += bits;
  152.  
  153.     bitheap <<= bits;
  154.  
  155.     return rv;
  156. }
  157.  
  158. void AMPBitstream::resetbitsL2(int bytes) {
  159.     bufindexw = bufpoint = pSource->read(buf, bytes);
  160.  
  161.     if (bufpoint != bytes)
  162.         throw (int)IAMPDecoder::ERR_EOF;
  163.  
  164.     bufindex = 4;
  165.     bitcnt = 32;
  166.     bitheap = *(long *)buf;
  167.         __asm mov ecx,this
  168.         __asm mov eax,[ecx]AMPBitstream.bitheap
  169.         __asm bswap eax
  170.         __asm mov [ecx]AMPBitstream.bitheap,eax
  171. }
  172.  
  173. long AMPBitstream::_getbitsL2(unsigned char bits) {
  174.     long rv;
  175.     int t = bitcnt;
  176.  
  177.     if (!bits)
  178.         return 0;
  179.  
  180.     rv = bitheap >> (32-bits);
  181.  
  182.     if (bufpoint < 4) {
  183.         bitcnt = 0;
  184.         bitheap = 0;
  185.         while(bitcnt < 32 && bufpoint>0) {
  186.             --bufpoint;
  187.             bitheap += ((unsigned long)buf[bufindex++]) << (24-bitcnt);
  188.             bitcnt += 8;
  189.         }
  190.  
  191.         if (t+bitcnt < bits)
  192.             throw (int)IAMPDecoder::ERR_INCOMPLETEFRAME;
  193.     } else {
  194.         bitheap = *(long *)(buf + bufindex);
  195.         bufindex += 4;
  196.         __asm mov ecx,this
  197.         __asm mov eax,[ecx]AMPBitstream.bitheap
  198.         __asm bswap eax
  199.         __asm mov [ecx]AMPBitstream.bitheap,eax
  200.         bitcnt = 32;
  201.         bufpoint -= 4;
  202.     }
  203.  
  204.     rv += bitheap >> (32+t - bits);
  205.     bitheap <<= bits-t;
  206.     bitcnt -= bits-t;
  207.  
  208.     return rv;
  209. }
  210.