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
110uint32 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    uint8     Elem;         /* Needs to be same type as pInput->pBuffer */
117    uint8     Elem1;
118    uint8     Elem2;
119    uint8     Elem3;
120    uint32   returnValue = 0;
121
122    if (!neededBits)
123    {
124        return (returnValue);
125    }
126
127    offset = (ptBitStream->usedBits) >> INBUF_ARRAY_INDEX_SHIFT;
128
129    Elem  = *(ptBitStream->pBuffer + module(offset  , BUFSIZE));
130    Elem1 = *(ptBitStream->pBuffer + module(offset + 1, BUFSIZE));
131    Elem2 = *(ptBitStream->pBuffer + module(offset + 2, BUFSIZE));
132    Elem3 = *(ptBitStream->pBuffer + module(offset + 3, BUFSIZE));
133
134
135    returnValue = (((uint32)(Elem)) << 24) |
136                  (((uint32)(Elem1)) << 16) |
137                  (((uint32)(Elem2)) << 8) |
138                  ((uint32)(Elem3));
139
140    /* Remove extra high bits by shifting up */
141    bitIndex = module(ptBitStream->usedBits, INBUF_BIT_WIDTH);
142
143    /* This line is faster than to mask off the high bits. */
144    returnValue <<= bitIndex;
145
146    /* Move the field down. */
147    returnValue >>= (32 - neededBits);
148
149    ptBitStream->usedBits += neededBits;
150
151    return (returnValue);
152}
153
154/*----------------------------------------------------------------------------
155; FUNCTION CODE
156----------------------------------------------------------------------------*/
157
158uint16 getUpTo9bits(tmp3Bits *ptBitStream,
159                    int32 neededBits) /* number of bits to read from the bit stream 2 to 9 */
160{
161
162    uint32    offset;
163    uint32    bitIndex;
164    uint8    Elem;         /* Needs to be same type as pInput->pBuffer */
165    uint8    Elem1;
166    uint16   returnValue;
167
168    offset = (ptBitStream->usedBits) >> INBUF_ARRAY_INDEX_SHIFT;
169
170    Elem  = *(ptBitStream->pBuffer + module(offset  , BUFSIZE));
171    Elem1 = *(ptBitStream->pBuffer + module(offset + 1, BUFSIZE));
172
173
174    returnValue = (((uint16)(Elem)) << 8) |
175                  ((uint16)(Elem1));
176
177    /* Remove extra high bits by shifting up */
178    bitIndex = module(ptBitStream->usedBits, INBUF_BIT_WIDTH);
179
180    ptBitStream->usedBits += neededBits;
181    /* This line is faster than to mask off the high bits. */
182    returnValue = (returnValue << (bitIndex));
183
184    /* Move the field down. */
185
186    return (uint16)(returnValue >> (16 - neededBits));
187
188}
189
190/*----------------------------------------------------------------------------
191; FUNCTION CODE
192----------------------------------------------------------------------------*/
193
194uint32 getUpTo17bits(tmp3Bits *ptBitStream,
195                     int32 neededBits) /* number of bits to read from the bit stream 2 to 8 */
196{
197
198    uint32    offset;
199    uint32    bitIndex;
200    uint8     Elem;         /* Needs to be same type as pInput->pBuffer */
201    uint8     Elem1;
202    uint8     Elem2;
203    uint32   returnValue;
204
205    offset = (ptBitStream->usedBits) >> INBUF_ARRAY_INDEX_SHIFT;
206
207    Elem  = *(ptBitStream->pBuffer + module(offset  , BUFSIZE));
208    Elem1 = *(ptBitStream->pBuffer + module(offset + 1, BUFSIZE));
209    Elem2 = *(ptBitStream->pBuffer + module(offset + 2, BUFSIZE));
210
211
212    returnValue = (((uint32)(Elem)) << 16) |
213                  (((uint32)(Elem1)) << 8) |
214                  ((uint32)(Elem2));
215
216    /* Remove extra high bits by shifting up */
217    bitIndex = module(ptBitStream->usedBits, INBUF_BIT_WIDTH);
218
219    ptBitStream->usedBits += neededBits;
220    /* This line is faster than to mask off the high bits. */
221    returnValue = 0xFFFFFF & (returnValue << (bitIndex));
222
223    /* Move the field down. */
224
225    return (uint32)(returnValue >> (24 - neededBits));
226
227}
228
229/*----------------------------------------------------------------------------
230; FUNCTION CODE
231----------------------------------------------------------------------------*/
232
233uint8 get1bit(tmp3Bits *ptBitStream)  /* number of bits to read from the bit stream */
234{
235
236    uint32    offset;
237    uint32    bitIndex;
238    uint8   returnValue;
239
240    offset = (ptBitStream->usedBits) >> INBUF_ARRAY_INDEX_SHIFT;
241
242    returnValue  = *(ptBitStream->pBuffer + module(offset  , BUFSIZE));
243
244    /* Remove extra high bits by shifting up */
245    bitIndex = module(ptBitStream->usedBits, INBUF_BIT_WIDTH);
246    ptBitStream->usedBits++;
247
248    /* This line is faster than to mask off the high bits. */
249    returnValue = (returnValue << (bitIndex));
250
251    return (uint8)(returnValue >> 7);
252
253}
254
255
256
257
258