www.pudn.com > VC写的MP3播放器源代码.zip > bitpool.cpp


/////////////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "bitpool.h" 
 
 
/////////////////////////////////////////////////////////////////////////////// 
// 
CBitPool::CBitPool():m_offset(0),m_PlayedBits(0),m_playedOctets(0), 
					m_restBits(8),m_InWordAcumulated(0) 
{ 
	//pretty bad to alloc in the constructor 
	VERIFY(m_pDwBuff = new DWORD[BUFSIZE]); 
} 
 
/////////////////////////////////////////////////////////////////////////////// 
// 
CBitPool::~CBitPool () 
{ 
    delete[] m_pDwBuff; 
	m_pDwBuff = 0;	// for debiger purpose 
} 
 
/////////////////////////////////////////////////////////////////////////////// 
// add 32 bits at the end 
 
/////////////////////////////////////////////////////////////////////////////// 
// 
void CBitPool::GoBackBits(int nBits) 
{ 
    m_PlayedBits -= nBits; 
    m_restBits   += nBits; 
    while(m_restBits >= 8)  
	{ 
        m_restBits -= 8; 
        m_playedOctets--; 
    } 
} 
 
/////////////////////////////////////////////////////////////////////////////// 
// 
void CBitPool::GoBackOctets(int nOctets) 
{ 
	m_PlayedBits		 -= (nOctets*8); 
    m_playedOctets		 -= nOctets; 
} 
 
 
/////////////////////////////////////////////////////////////////////////////// 
// 
// 
DWORD CBitPool::GetBits(DWORD nBits) 
{ 
 
	m_PlayedBits += nBits; 
   	DWORD	dwRetVal = 0; 
	int		contor = nBits; 
	while (contor > 0)  
	{ 
		if (m_restBits == 0)  
		{ 
			m_restBits = 8; 
			m_playedOctets++; 
		} 
		int minBidx = min((int)m_restBits,contor); 
		DWORD xmask = 0xFFFFFFFF >> (32 - m_restBits); 
		int tempVal = m_pDwBuff[m_playedOctets & 0xfff]; 
		tempVal&= xmask;		 
		m_restBits -= minBidx; 
		tempVal = tempVal >> m_restBits; 
		contor -= minBidx; 
		dwRetVal |= tempVal << contor; 
	} 
	return dwRetVal; 
}