Split Rasterizer::Draw operation.
[xy_vsfilter.git] / src / dsutil / GolombBuffer.cpp
blob3508b96386005545a7b808879f633600a6cb193b
1 /*
2 * $Id: GolombBuffer.cpp 3526 2011-08-01 15:46:29Z XhmikosR $
4 * (C) 2006-2010 see AUTHORS
6 * This file is part of mplayerc.
8 * Mplayerc is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * Mplayerc is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "stdafx.h"
25 #include "GolombBuffer.h"
27 CGolombBuffer::CGolombBuffer(BYTE* pBuffer, int nSize)
29 m_pBuffer = pBuffer;
30 m_nSize = nSize;
32 Reset();
35 UINT64 CGolombBuffer::BitRead(int nBits, bool fPeek)
37 // ASSERT(nBits >= 0 && nBits <= 64);
39 while(m_bitlen < nBits) {
40 m_bitbuff <<= 8;
42 if (m_nBitPos >= m_nSize) {
43 return 0;
46 *(BYTE*)&m_bitbuff = m_pBuffer[m_nBitPos++];
47 m_bitlen += 8;
50 int bitlen = m_bitlen - nBits;
52 UINT64 ret = (m_bitbuff >> bitlen) & ((1ui64 << nBits) - 1);
54 if(!fPeek) {
55 m_bitbuff &= ((1ui64 << bitlen) - 1);
56 m_bitlen = bitlen;
59 return ret;
62 UINT64 CGolombBuffer::UExpGolombRead()
64 int n = -1;
65 for(BYTE b = 0; !b; n++) {
66 b = (BYTE)BitRead(1);
68 return (1ui64 << n) - 1 + BitRead(n);
71 INT64 CGolombBuffer::SExpGolombRead()
73 UINT64 k = UExpGolombRead();
74 return ((k&1) ? 1 : -1) * ((k + 1) >> 1);
77 void CGolombBuffer::BitByteAlign()
79 m_bitlen &= ~7;
82 __int64 CGolombBuffer::GetPos()
84 return m_nBitPos - (m_bitlen>>3);
87 void CGolombBuffer::ReadBuffer(BYTE* pDest, int nSize)
89 ASSERT (m_nBitPos + nSize <= m_nSize);
90 ASSERT (m_bitlen == 0);
91 nSize = min (nSize, m_nSize - m_nBitPos);
93 memcpy (pDest, m_pBuffer+m_nBitPos, nSize);
94 m_nBitPos += nSize;
97 void CGolombBuffer::Reset()
99 m_nBitPos = 0;
100 m_bitlen = 0;
101 m_bitbuff = 0;
104 void CGolombBuffer::Reset(BYTE* pNewBuffer, int nNewSize)
106 m_pBuffer = pNewBuffer;
107 m_nSize = nNewSize;
109 Reset();
112 void CGolombBuffer::SkipBytes(int nCount)
114 m_nBitPos += nCount;
115 m_bitlen = 0;
116 m_bitbuff = 0;