2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #include "bitstream_parser.h"
14 BitstreamParser::BitstreamParser(const uint8_t* data, const uint32_t dataLength) :
16 _dataLength(dataLength),
21 // todo should we have any error codes from this?
24 BitstreamParser::Get1Bit()
26 uint8_t retVal = 0x1 & (_data[_byteOffset] >> (7-_bitOffset++));
38 BitstreamParser::Get2Bits()
40 uint8_t retVal = (Get1Bit() << 1);
46 BitstreamParser::Get3Bits()
48 uint8_t retVal = (Get1Bit() << 2);
49 retVal += (Get1Bit() << 1);
55 BitstreamParser::Get4Bits()
57 uint8_t retVal = (Get1Bit() << 3);
58 retVal += (Get1Bit() << 2);
59 retVal += (Get1Bit() << 1);
65 BitstreamParser::Get5Bits()
67 uint8_t retVal = (Get1Bit() << 4);
68 retVal += (Get1Bit() << 3);
69 retVal += (Get1Bit() << 2);
70 retVal += (Get1Bit() << 1);
76 BitstreamParser::Get6Bits()
78 uint8_t retVal = (Get1Bit() << 5);
79 retVal += (Get1Bit() << 4);
80 retVal += (Get1Bit() << 3);
81 retVal += (Get1Bit() << 2);
82 retVal += (Get1Bit() << 1);
88 BitstreamParser::Get7Bits()
90 uint8_t retVal = (Get1Bit() << 6);
91 retVal += (Get1Bit() << 5);
92 retVal += (Get1Bit() << 4);
93 retVal += (Get1Bit() << 3);
94 retVal += (Get1Bit() << 2);
95 retVal += (Get1Bit() << 1);
101 BitstreamParser::Get8Bits()
108 retVal = (_data[_byteOffset] << 8)+ (_data[_byteOffset+1]) ;
109 retVal = retVal >> (8-_bitOffset);
112 retVal = _data[_byteOffset];
115 return (uint8_t)retVal;
119 BitstreamParser::Get16Bits()
126 retVal = (_data[_byteOffset] << 16) + (_data[_byteOffset+1] << 8) + (_data[_byteOffset+2]);
127 retVal = retVal >> (8-_bitOffset);
131 retVal = (_data[_byteOffset] << 8) + (_data[_byteOffset+1]) ;
134 return (uint16_t)retVal;
138 BitstreamParser::Get24Bits()
145 retVal = (_data[_byteOffset] << 24) + (_data[_byteOffset+1] << 16) + (_data[_byteOffset+2] << 8) + (_data[_byteOffset+3]);
146 retVal = retVal >> (8-_bitOffset);
150 retVal = (_data[_byteOffset] << 16) + (_data[_byteOffset+1] << 8) + (_data[_byteOffset+2]) ;
153 return retVal & 0x00ffffff; // we need to clean up the high 8 bits
157 BitstreamParser::Get32Bits()
164 uint64_t tempVal = _data[_byteOffset];
166 tempVal += _data[_byteOffset+1];
168 tempVal += _data[_byteOffset+2];
170 tempVal += _data[_byteOffset+3];
172 tempVal += _data[_byteOffset+4];
173 tempVal >>= (8-_bitOffset);
175 retVal = uint32_t(tempVal);
179 retVal = (_data[_byteOffset]<< 24) + (_data[_byteOffset+1] << 16) + (_data[_byteOffset+2] << 8) + (_data[_byteOffset+3]) ;
187 with "prefix" and "suffix" bits and assignment to codeNum ranges (informative)
188 Bit string form Range of codeNum
192 0 0 0 1 x2 x1 x0 7..14
193 0 0 0 0 1 x3 x2 x1 x0 15..30
194 0 0 0 0 0 1 x4 x3 x2 x1 x0 31..62
198 BitstreamParser::GetUE()
201 uint8_t numLeadingZeros = 0;
203 while (Get1Bit() != 1)
208 retVal = (1 << numLeadingZeros) - 1;
211 while (numLeadingZeros)
213 retVal += (Get1Bit() << --numLeadingZeros);
217 } // namespace webrtc