/ source / libsmackerdec / src / BitReader.cpp
BitReader.cpp
 1  /*
 2   * libsmackerdec - Smacker video decoder
 3   * Copyright (C) 2011 Barry Duncan
 4   *
 5   * This library is free software; you can redistribute it and/or
 6   * modify it under the terms of the GNU Lesser General Public
 7   * License as published by the Free Software Foundation; either
 8   * version 2.1 of the License, or (at your option) any later version.
 9   *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   */
19  
20  #include "BitReader.h"
21  #include <assert.h>
22  
23  namespace SmackerCommon {
24  
25  BitReader::BitReader(SmackerCommon::FileStream &file, uint32_t size)
26  {
27  	this->file = &file;
28  	this->totalSize = size;
29  	this->currentOffset = 0;
30  	this->bytesRead = 0;
31  
32      this->cache = (uint8_t*)Xmalloc(size);
33      file.ReadBytes(this->cache, size);
34  }
35  
36  BitReader::~BitReader()
37  {
38  	Xfree(this->cache);
39  }
40  
41  void BitReader::FillCache()
42  {
43  }
44  
45  uint32_t BitReader::GetSize()
46  {
47  	return totalSize * 8;
48  }
49  
50  uint32_t BitReader::GetPosition()
51  {
52  	return currentOffset;
53  }
54  
55  uint32_t BitReader::GetBit()
56  {
57  	uint32_t ret = (cache[currentOffset>>3]>>(currentOffset&7))&1;
58      currentOffset++;
59  	return ret;
60  }
61  
62  uint32_t BitReader::GetBits(uint32_t n)
63  {
64  	uint32_t ret = 0;
65  
66  	int bitsTodo = n;
67  
68  	uint32_t theShift = 0;
69  
70  	while (bitsTodo)
71  	{
72  		uint32_t bit = GetBit();
73  		bit <<= theShift;
74  
75  		theShift++;
76  
77  		ret |= bit;
78  
79  		bitsTodo--;
80  	}
81  
82  	return ret;
83  }
84  
85  void BitReader::SkipBits(uint32_t n)
86  {
87  	GetBits(n);
88  }
89  
90  } // close namespace SmackerCommon
91