1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18 /*
19 ------------------------------------------------------------------------------
20
21 PacketVideo Corp.
22 MP3 Decoder Library
23
24 Filename: pvmp3_getbits.cpp
25
26
27 Date: 09/21/2007
28
29 ------------------------------------------------------------------------------
30 REVISION HISTORY
31
32
33 Description:
34
35 ------------------------------------------------------------------------------
36 INPUT AND OUTPUT DEFINITIONS
37
38 Inputs:
39
40 tmp3Bits *inputStream, structure holding the input stream parameters
41 int32 neededBits number of bits to read from the bit stream
42
43 Outputs:
44
45 word parsed from teh bitstream, with size neededBits-bits,
46
47 ------------------------------------------------------------------------------
48 FUNCTION DESCRIPTION
49
50
51 ------------------------------------------------------------------------------
52 REQUIREMENTS
53
54
55 ------------------------------------------------------------------------------
56 REFERENCES
57 [1] ISO MPEG Audio Subgroup Software Simulation Group (1996)
58 ISO 13818-3 MPEG-2 Audio Decoder - Lower Sampling Frequency Extension
59
60
61 ------------------------------------------------------------------------------
62 PSEUDO-CODE
63
64 ------------------------------------------------------------------------------
65 */
66
67 /*----------------------------------------------------------------------------
68 ; INCLUDES
69 ----------------------------------------------------------------------------*/
70 #include "pvmp3_getbits.h"
71
72 /*----------------------------------------------------------------------------
73 ; MACROS
74 ; Define module specific macros here
75 ----------------------------------------------------------------------------*/
76
77
78 /*----------------------------------------------------------------------------
79 ; DEFINES
80 ; Include all pre-processor statements here. Include conditional
81 ; compile variables also.
82 ----------------------------------------------------------------------------*/
83
84
85 /*----------------------------------------------------------------------------
86 ; LOCAL FUNCTION DEFINITIONS
87 ; Function Prototype declaration
88 ----------------------------------------------------------------------------*/
89
90 /*----------------------------------------------------------------------------
91 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
92 ; Variable declaration - defined here and used outside this module
93 ----------------------------------------------------------------------------*/
94
95 /*----------------------------------------------------------------------------
96 ; EXTERNAL FUNCTION REFERENCES
97 ; Declare functions defined elsewhere and referenced in this module
98 ----------------------------------------------------------------------------*/
99
100 /*----------------------------------------------------------------------------
101 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
102 ; Declare variables used in this module but defined elsewhere
103 ----------------------------------------------------------------------------*/
104
105
106 /*----------------------------------------------------------------------------
107 ; FUNCTION CODE
108 ----------------------------------------------------------------------------*/
109
getNbits(tmp3Bits * ptBitStream,int32 neededBits)110 uint32 getNbits(tmp3Bits *ptBitStream,
111 int32 neededBits) /* number of bits to read from the bitstream (up to 25) */
112 {
113
114 uint32 offset;
115 uint32 bitIndex;
116 uint32 bytesToFetch;
117 uint8 Elem = 0; /* Needs to be same type as pInput->pBuffer */
118 uint8 Elem1 = 0;
119 uint8 Elem2 = 0;
120 uint8 Elem3 = 0;
121 uint32 returnValue = 0;
122
123 if (!neededBits)
124 {
125 return (returnValue);
126 }
127
128 offset = (ptBitStream->usedBits) >> INBUF_ARRAY_INDEX_SHIFT;
129
130 /* Remove extra high bits by shifting up */
131 bitIndex = module(ptBitStream->usedBits, INBUF_BIT_WIDTH);
132
133 bytesToFetch = (bitIndex + neededBits + 7 ) >> 3 ;
134
135 switch (bytesToFetch)
136 {
137 case 4:
138 Elem3 = *(ptBitStream->pBuffer + module(offset + 3, BUFSIZE));
139 [[fallthrough]];
140 case 3:
141 Elem2 = *(ptBitStream->pBuffer + module(offset + 2, BUFSIZE));
142 [[fallthrough]];
143 case 2:
144 Elem1 = *(ptBitStream->pBuffer + module(offset + 1, BUFSIZE));
145 [[fallthrough]];
146 case 1:
147 Elem = *(ptBitStream->pBuffer + module(offset, BUFSIZE));
148 }
149
150
151 returnValue = (((uint32)(Elem)) << 24) |
152 (((uint32)(Elem1)) << 16) |
153 (((uint32)(Elem2)) << 8) |
154 ((uint32)(Elem3));
155
156 /* This line is faster than to mask off the high bits. */
157 returnValue <<= bitIndex;
158
159 /* Move the field down. */
160 returnValue >>= (32 - neededBits);
161
162 ptBitStream->usedBits += neededBits;
163
164 return (returnValue);
165 }
166
167 /*----------------------------------------------------------------------------
168 ; FUNCTION CODE
169 ----------------------------------------------------------------------------*/
170
getUpTo9bits(tmp3Bits * ptBitStream,int32 neededBits)171 uint16 getUpTo9bits(tmp3Bits *ptBitStream,
172 int32 neededBits) /* number of bits to read from the bit stream 2 to 9 */
173 {
174
175 uint32 offset;
176 uint32 bitIndex;
177 uint32 bytesToFetch;
178 uint8 Elem = 0; /* Needs to be same type as pInput->pBuffer */
179 uint8 Elem1 = 0;
180 uint16 returnValue;
181
182 offset = (ptBitStream->usedBits) >> INBUF_ARRAY_INDEX_SHIFT;
183
184 /* Remove extra high bits by shifting up */
185 bitIndex = module(ptBitStream->usedBits, INBUF_BIT_WIDTH);
186
187 bytesToFetch = (bitIndex + neededBits + 7 ) >> 3 ;
188
189 if (bytesToFetch > 1)
190 {
191 Elem = *(ptBitStream->pBuffer + module(offset, BUFSIZE));
192 Elem1 = *(ptBitStream->pBuffer + module(offset + 1, BUFSIZE));
193 }
194 else if (bytesToFetch > 0)
195 {
196 Elem = *(ptBitStream->pBuffer + module(offset, BUFSIZE));
197 }
198
199
200 returnValue = (((uint16)(Elem)) << 8) |
201 ((uint16)(Elem1));
202
203 ptBitStream->usedBits += neededBits;
204 /* This line is faster than to mask off the high bits. */
205 returnValue = (returnValue << (bitIndex));
206
207 /* Move the field down. */
208
209 return (uint16)(returnValue >> (16 - neededBits));
210
211 }
212
213 /*----------------------------------------------------------------------------
214 ; FUNCTION CODE
215 ----------------------------------------------------------------------------*/
216
getUpTo17bits(tmp3Bits * ptBitStream,int32 neededBits)217 uint32 getUpTo17bits(tmp3Bits *ptBitStream,
218 int32 neededBits) /* number of bits to read from the bit stream 2 to 8 */
219 {
220
221 uint32 offset;
222 uint32 bitIndex;
223 uint32 bytesToFetch;
224 uint8 Elem = 0; /* Needs to be same type as pInput->pBuffer */
225 uint8 Elem1 = 0;
226 uint8 Elem2 = 0;
227 uint32 returnValue;
228
229 offset = (ptBitStream->usedBits) >> INBUF_ARRAY_INDEX_SHIFT;
230
231 /* Remove extra high bits by shifting up */
232 bitIndex = module(ptBitStream->usedBits, INBUF_BIT_WIDTH);
233
234 bytesToFetch = (bitIndex + neededBits + 7 ) >> 3 ;
235
236 if (bytesToFetch > 2)
237 {
238 Elem = *(ptBitStream->pBuffer + module(offset, BUFSIZE));
239 Elem1 = *(ptBitStream->pBuffer + module(offset + 1, BUFSIZE));
240 Elem2 = *(ptBitStream->pBuffer + module(offset + 2, BUFSIZE));
241 }
242 else if (bytesToFetch > 1)
243 {
244 Elem = *(ptBitStream->pBuffer + module(offset, BUFSIZE));
245 Elem1 = *(ptBitStream->pBuffer + module(offset + 1, BUFSIZE));
246 }
247 else if (bytesToFetch > 0)
248 {
249 Elem = *(ptBitStream->pBuffer + module(offset, BUFSIZE));
250 }
251
252
253 returnValue = (((uint32)(Elem)) << 16) |
254 (((uint32)(Elem1)) << 8) |
255 ((uint32)(Elem2));
256
257 ptBitStream->usedBits += neededBits;
258 /* This line is faster than to mask off the high bits. */
259 returnValue = 0xFFFFFF & (returnValue << (bitIndex));
260
261 /* Move the field down. */
262
263 return (uint32)(returnValue >> (24 - neededBits));
264
265 }
266
267 /*----------------------------------------------------------------------------
268 ; FUNCTION CODE
269 ----------------------------------------------------------------------------*/
270
get1bit(tmp3Bits * ptBitStream)271 uint8 get1bit(tmp3Bits *ptBitStream) /* number of bits to read from the bit stream */
272 {
273
274 uint32 offset;
275 uint32 bitIndex;
276 uint8 returnValue;
277
278 offset = (ptBitStream->usedBits) >> INBUF_ARRAY_INDEX_SHIFT;
279
280 returnValue = *(ptBitStream->pBuffer + module(offset , BUFSIZE));
281
282 /* Remove extra high bits by shifting up */
283 bitIndex = module(ptBitStream->usedBits, INBUF_BIT_WIDTH);
284 ptBitStream->usedBits++;
285
286 /* This line is faster than to mask off the high bits. */
287 returnValue = (returnValue << (bitIndex));
288
289 return (uint8)(returnValue >> 7);
290
291 }
292
293
294
295
296