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_huffman_decoding.cpp
25
26 Funtions:
27    pvmp3_huffman_quad_decoding
28    pvmp3_huffman_pair_decoding
29    pvmp3_huffman_pair_decoding_linbits
30
31     Date: 09/21/2007
32
33------------------------------------------------------------------------------
34 REVISION HISTORY
35
36
37 Description:
38
39------------------------------------------------------------------------------
40 INPUT AND OUTPUT DEFINITIONS
41
42 Inputs:
43    struct huffcodetab *h,   pointer to huffman code record
44    int32 *x,    returns decoded x value
45    int32 *y,    returns decoded y value
46    int32 *v,    returns decoded v value   (only in quad function)
47    int32 *w,    returns decoded w value   (only in quad function)
48    tbits *pMainData     bit stream
49
50 Outputs:
51
52
53------------------------------------------------------------------------------
54 FUNCTION DESCRIPTION
55
56   These functions are used to decode huffman codewords from the input
57   bitstream using combined binary search and look-up table approach.
58
59------------------------------------------------------------------------------
60 REQUIREMENTS
61
62
63------------------------------------------------------------------------------
64 REFERENCES
65 [1] ISO MPEG Audio Subgroup Software Simulation Group (1996)
66     ISO 13818-3 MPEG-2 Audio Decoder - Lower Sampling Frequency Extension
67
68
69------------------------------------------------------------------------------
70 PSEUDO-CODE
71
72------------------------------------------------------------------------------
73*/
74
75
76/*----------------------------------------------------------------------------
77; INCLUDES
78----------------------------------------------------------------------------*/
79#include "pvmp3_dec_defs.h"
80#include "pv_mp3_huffman.h"
81#include "pvmp3_getbits.h"
82
83
84/*----------------------------------------------------------------------------
85; MACROS
86; Define module specific macros here
87----------------------------------------------------------------------------*/
88
89
90/*----------------------------------------------------------------------------
91; DEFINES
92; Include all pre-processor statements here. Include conditional
93; compile variables also.
94----------------------------------------------------------------------------*/
95
96
97/*----------------------------------------------------------------------------
98; LOCAL FUNCTION DEFINITIONS
99; Function Prototype declaration
100----------------------------------------------------------------------------*/
101
102/*----------------------------------------------------------------------------
103; LOCAL STORE/BUFFER/POINTER DEFINITIONS
104; Variable declaration - defined here and used outside this module
105----------------------------------------------------------------------------*/
106
107/*----------------------------------------------------------------------------
108; EXTERNAL FUNCTION REFERENCES
109; Declare functions defined elsewhere and referenced in this module
110----------------------------------------------------------------------------*/
111
112/*----------------------------------------------------------------------------
113; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
114; Declare variables used in this module but defined elsewhere
115----------------------------------------------------------------------------*/
116
117/*----------------------------------------------------------------------------
118; FUNCTION CODE
119----------------------------------------------------------------------------*/
120
121
122void pvmp3_huffman_quad_decoding(struct huffcodetab *h,
123                                 int32 *is,
124                                 tmp3Bits *pMainData)
125{
126
127    int32 x;
128    int32 y;
129    int32 v;
130    int32 w;
131
132    y = (*h->pdec_huff_tab)(pMainData);
133
134
135    if (y)
136    {
137        v = (y >> 3);
138
139        if (v)
140        {
141            if (get1bit(pMainData))
142            {
143                v = -v;
144            }
145        }
146        w = (y >> 2) & 1;
147        if (w)
148        {
149            if (get1bit(pMainData))
150            {
151                w = -w;
152            }
153        }
154        x = (y >> 1) & 1;
155        if (x)
156        {
157            if (get1bit(pMainData))
158            {
159                x = -x;
160            }
161        }
162        y =  y & 1;
163        if (y)
164        {
165            if (get1bit(pMainData))
166            {
167                y = -y;
168            }
169        }
170
171    }
172    else
173    {
174        v = 0;
175        w = 0;
176        x = 0;
177
178    }
179
180    *is     = v;
181    *(is + 1) = w;
182    *(is + 2) = x;
183    *(is + 3) = y;
184
185}
186
187
188
189void pvmp3_huffman_pair_decoding(struct huffcodetab *h,     /* pointer to huffman code record   */
190                                 int32 *is,
191                                 tmp3Bits *pMainData)
192{
193    /* Lookup in Huffman table. */
194    int32 x;
195    int32 y;
196
197    uint16 cw = (*h->pdec_huff_tab)(pMainData);
198
199    /* Process sign and escape encodings for dual tables. */
200
201
202    if (cw)
203    {
204        x = cw >> 4;
205
206        if (x)
207        {
208            if (get1bit(pMainData))
209            {
210                x = -x;
211            }
212            y = cw & 0xf;
213            if (y && get1bit(pMainData))
214            {
215                y = -y;
216            }
217
218        }
219        else
220        {
221            y = cw & 0xf;
222            if (get1bit(pMainData))
223            {
224                y = -y;
225            }
226        }
227
228        *is     = x;
229        *(is + 1) = y;
230    }
231    else
232    {
233        *is     = 0;
234        *(is + 1) = 0;
235    }
236
237
238
239}
240
241
242
243
244void pvmp3_huffman_pair_decoding_linbits(struct huffcodetab *h,     /* pointer to huffman code record   */
245        int32 *is,
246        tmp3Bits *pMainData)
247{
248    int32 x;
249    int32 y;
250
251    uint16 cw;
252    /* Lookup in Huffman table. */
253
254
255    cw = (*h->pdec_huff_tab)(pMainData);
256    x = cw >> 4;
257
258    /* Process sign and escape encodings for dual tables. */
259
260
261    if (15 == (uint32)x)
262    {
263        int32 tmp = getUpTo17bits(pMainData, (h->linbits + 1));
264        x += tmp >> 1;
265        if (tmp&1)
266        {
267            x = -x;
268        }
269    }
270    else if (x)
271    {
272        if (get1bit(pMainData))
273        {
274            x = -x;
275        }
276    }
277
278    y = cw & 0xf;
279    if (15 == (uint32)y)
280    {
281        int32 tmp = getUpTo17bits(pMainData, (h->linbits + 1));
282        y += tmp >> 1;
283        if (tmp&1)
284        {
285            y = -y;
286        }
287    }
288    else if (y)
289    {
290        if (get1bit(pMainData))
291        {
292            y = -y;
293        }
294    }
295
296    *is     = x;
297    *(is + 1) = y;
298
299}
300
301
302
303
304
305