Update UNRAR.H
[xy_vsfilter.git] / src / dsutil / GolombBuffer.cpp
blob2af3647d468f83037588c3297930628d33093cf7
1 /*
2 * (C) 2006-2012 see Authors.txt
4 * This file is part of MPC-HC.
6 * MPC-HC is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * MPC-HC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "stdafx.h"
22 #include "GolombBuffer.h"
24 CGolombBuffer::CGolombBuffer(BYTE* pBuffer, int nSize)
26 m_pBuffer = pBuffer;
27 m_nSize = nSize;
29 Reset();
32 UINT64 CGolombBuffer::BitRead(int nBits, bool fPeek)
34 //ASSERT(nBits >= 0 && nBits <= 64);
36 while (m_bitlen < nBits) {
37 m_bitbuff <<= 8;
39 if (m_nBitPos >= m_nSize) {
40 return 0;
43 *(BYTE*)&m_bitbuff = m_pBuffer[m_nBitPos++];
44 m_bitlen += 8;
47 int bitlen = m_bitlen - nBits;
49 UINT64 ret = (m_bitbuff >> bitlen) & ((1ui64 << nBits) - 1);
51 if (!fPeek) {
52 m_bitbuff &= ((1ui64 << bitlen) - 1);
53 m_bitlen = bitlen;
56 return ret;
59 UINT64 CGolombBuffer::UExpGolombRead()
61 int n = -1;
62 for (BYTE b = 0; !b; n++) {
63 b = (BYTE)BitRead(1);
65 return (1ui64 << n) - 1 + BitRead(n);
68 INT64 CGolombBuffer::SExpGolombRead()
70 UINT64 k = UExpGolombRead();
71 return ((k & 1) ? 1 : -1) * ((k + 1) >> 1);
74 void CGolombBuffer::BitByteAlign()
76 m_bitlen &= ~7;
79 int CGolombBuffer::GetPos()
81 return m_nBitPos - (m_bitlen >> 3);
84 void CGolombBuffer::ReadBuffer(BYTE* pDest, int nSize)
86 ASSERT(m_nBitPos + nSize <= m_nSize);
87 ASSERT(m_bitlen == 0);
88 nSize = min(nSize, m_nSize - m_nBitPos);
90 memcpy(pDest, m_pBuffer + m_nBitPos, nSize);
91 m_nBitPos += nSize;
94 void CGolombBuffer::Reset()
96 m_nBitPos = 0;
97 m_bitlen = 0;
98 m_bitbuff = 0;
101 void CGolombBuffer::Reset(BYTE* pNewBuffer, int nNewSize)
103 m_pBuffer = pNewBuffer;
104 m_nSize = nNewSize;
106 Reset();
109 void CGolombBuffer::SkipBytes(int nCount)
111 m_nBitPos += nCount;
112 m_bitlen = 0;
113 m_bitbuff = 0;