pvmp3_huffman_parsing.cpp revision d9618f23226f46c752e56f712bc4b505117d8b4b
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
22   PacketVideo Corp.
23   MP3 Decoder Library
24
25   Filename: pvmp3_huffman_decoding.cpp
26
27 Funtions:
28    pvmp3_huffman_quad_decoding
29    pvmp3_huffman_pair_decoding
30    pvmp3_huffman_pair_decoding_linbits
31
32     Date: 09/21/2007
33
34------------------------------------------------------------------------------
35 REVISION HISTORY
36
37
38 Description:
39
40------------------------------------------------------------------------------
41 INPUT AND OUTPUT DEFINITIONS
42
43 Inputs:
44    int32 is[],
45    granuleInfo  *grInfo,    information for the given channel and granule
46    tmp3dec_file   *pVars,   decoder state structure
47    int32 part2_start,       index to beginning of part 2 data
48    mp3Header *info          mp3 header info
49
50 Outputs:
51    int32 is[],              uncompressed data
52
53  Return:
54     non zero frequency lines
55
56------------------------------------------------------------------------------
57 FUNCTION DESCRIPTION
58
59   These functions are used to decode huffman codewords from the input
60   bitstream using combined binary search and look-up table approach.
61
62------------------------------------------------------------------------------
63 REQUIREMENTS
64
65
66------------------------------------------------------------------------------
67 REFERENCES
68 [1] ISO MPEG Audio Subgroup Software Simulation Group (1996)
69     ISO 13818-3 MPEG-2 Audio Decoder - Lower Sampling Frequency Extension
70
71
72------------------------------------------------------------------------------
73 PSEUDO-CODE
74
75------------------------------------------------------------------------------
76*/
77
78
79/*----------------------------------------------------------------------------
80; INCLUDES
81----------------------------------------------------------------------------*/
82#include "pv_mp3_huffman.h"
83#include "s_mp3bits.h"
84#include "mp3_mem_funcs.h"
85#include "pvmp3_tables.h"
86
87
88/*----------------------------------------------------------------------------
89; MACROS
90; Define module specific macros here
91----------------------------------------------------------------------------*/
92
93
94/*----------------------------------------------------------------------------
95; DEFINES
96; Include all pre-processor statements here. Include conditional
97; compile variables also.
98----------------------------------------------------------------------------*/
99
100
101/*----------------------------------------------------------------------------
102; LOCAL FUNCTION DEFINITIONS
103; Function Prototype declaration
104----------------------------------------------------------------------------*/
105
106/*----------------------------------------------------------------------------
107; LOCAL STORE/BUFFER/POINTER DEFINITIONS
108; Variable declaration - defined here and used outside this module
109----------------------------------------------------------------------------*/
110
111/*----------------------------------------------------------------------------
112; EXTERNAL FUNCTION REFERENCES
113; Declare functions defined elsewhere and referenced in this module
114----------------------------------------------------------------------------*/
115
116/*----------------------------------------------------------------------------
117; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
118; Declare variables used in this module but defined elsewhere
119----------------------------------------------------------------------------*/
120
121/*----------------------------------------------------------------------------
122; FUNCTION CODE
123----------------------------------------------------------------------------*/
124
125int32 pvmp3_huffman_parsing(int32 is[SUBBANDS_NUMBER*FILTERBANK_BANDS],
126                            granuleInfo *grInfo,
127                            tmp3dec_file   *pVars,
128                            int32 part2_start,
129                            mp3Header *info)
130
131
132{
133    int32 i;
134    int32 region1Start;
135    int32 region2Start;
136    int32 sfreq;
137    uint32 grBits;
138    void(*pt_huff)(struct huffcodetab *, int32 *, tmp3Bits *);
139    struct huffcodetab *h;
140
141    tmp3Bits *pMainData = &pVars->mainDataStream;
142
143
144    /*int32 bt = (*si).ch[ch].gr[gr].window_switching_flag && ((*si).ch[ch].gr[gr].block_type == 2);*/
145
146    sfreq = info->sampling_frequency + info->version_x + (info->version_x << 1);
147
148    /* Find region boundary for short block case. */
149
150
151    if ((grInfo->window_switching_flag) && (grInfo->block_type == 2))
152    {
153        if (info->version_x == MPEG_1)
154        {
155            /* Region2. */
156            region1Start = 12;
157        }
158        else
159        {
160            /* Region2. */
161            i = grInfo->region0_count + 1;
162            region1Start = mp3_sfBandIndex[sfreq].s[i/3];
163        }
164
165        region1Start += region1Start << 1;
166        region2Start = 576; /* No Region2 for short block case. */
167    }
168    else
169    {          /* Find region boundary for long block case. */
170        i = grInfo->region0_count + 1;
171        region1Start = mp3_sfBandIndex[sfreq].l[i];
172        region2Start = mp3_sfBandIndex[sfreq].l[i + grInfo->region1_count + 1];
173    }
174
175    /* Read bigvalues area. */
176
177
178    if (grInfo->big_values > (FILTERBANK_BANDS*SUBBANDS_NUMBER >> 1))
179    {
180        grInfo->big_values = (FILTERBANK_BANDS * SUBBANDS_NUMBER >> 1);
181    }
182
183    if ((grInfo->big_values << 1) > (uint32)region2Start)
184    {
185        h = &(pVars->ht[grInfo->table_select[0]]);
186        if (h->linbits)
187        {
188            pt_huff = pvmp3_huffman_pair_decoding_linbits;
189        }
190        else
191        {
192            pt_huff = pvmp3_huffman_pair_decoding;
193        }
194
195        for (i = 0; i < region1Start; i += 2)
196        {
197            (*pt_huff)(h, &is[i], pMainData);
198        }
199
200        h = &(pVars->ht[grInfo->table_select[1]]);
201        if (h->linbits)
202        {
203            pt_huff = pvmp3_huffman_pair_decoding_linbits;
204        }
205        else
206        {
207            pt_huff = pvmp3_huffman_pair_decoding;
208        }
209
210        for (; i < region2Start; i += 2)
211        {
212            (*pt_huff)(h, &is[i], pMainData);
213        }
214
215        h = &(pVars->ht[grInfo->table_select[2]]);
216        if (h->linbits)
217        {
218            pt_huff = pvmp3_huffman_pair_decoding_linbits;
219        }
220        else
221        {
222            pt_huff = pvmp3_huffman_pair_decoding;
223        }
224
225        for (; (uint32)i < (grInfo->big_values << 1); i += 2)
226        {
227            (*pt_huff)(h, &is[i], pMainData);
228        }
229    }
230    else if ((grInfo->big_values << 1) > (uint32)region1Start)
231    {
232        h = &(pVars->ht[grInfo->table_select[0]]);
233        if (h->linbits)
234        {
235            pt_huff = pvmp3_huffman_pair_decoding_linbits;
236        }
237        else
238        {
239            pt_huff = pvmp3_huffman_pair_decoding;
240        }
241        for (i = 0; i < region1Start; i += 2)
242        {
243            (*pt_huff)(h, &is[i], pMainData);
244        }
245
246        h = &(pVars->ht[grInfo->table_select[1]]);
247        if (h->linbits)
248        {
249            pt_huff = pvmp3_huffman_pair_decoding_linbits;
250        }
251        else
252        {
253            pt_huff = pvmp3_huffman_pair_decoding;
254        }
255        for (; (uint32)i < (grInfo->big_values << 1); i += 2)
256        {
257            (*pt_huff)(h, &is[i], pMainData);
258        }
259    }
260    else
261    {
262        h = &(pVars->ht[grInfo->table_select[0]]);
263        if (h->linbits)
264        {
265            pt_huff = pvmp3_huffman_pair_decoding_linbits;
266        }
267        else
268        {
269            pt_huff = pvmp3_huffman_pair_decoding;
270        }
271
272        for (i = 0; (uint32)i < (grInfo->big_values << 1); i += 2)
273        {
274            (*pt_huff)(h, &is[i], pMainData);
275        }
276    }
277
278
279
280    /* Read count1 area. */
281    h = &(pVars->ht[grInfo->count1table_select+32]);
282
283    grBits     = part2_start + grInfo->part2_3_length;
284
285    while ((pMainData->usedBits < grBits) &&
286            (i < FILTERBANK_BANDS*SUBBANDS_NUMBER - 4))
287    {
288        pvmp3_huffman_quad_decoding(h, &is[i], pMainData);
289        i += 4;
290    }
291
292    if ((pMainData->usedBits < grBits) &&
293            (i < FILTERBANK_BANDS*SUBBANDS_NUMBER))
294    {
295        pvmp3_huffman_quad_decoding(h, &is[i], pMainData);
296        i += 4;
297
298        if ((i - 2) >= FILTERBANK_BANDS*SUBBANDS_NUMBER)
299        {
300            i -= 2;
301            is[i] = 0;
302            is[(i+1)] = 0;
303        }
304    }
305
306    if (pMainData->usedBits > grBits)
307    {
308        i -= 4;
309
310        if (i < 0 || i > FILTERBANK_BANDS*SUBBANDS_NUMBER - 4)
311        {
312            /* illegal parameters may cause invalid access, set i to 0 */
313            i = 0;
314        }
315
316        is[i] = 0;
317        is[(i+1)] = 0;
318        is[(i+2)] = 0;
319        is[(i+3)] = 0;
320
321    }
322
323    pMainData->usedBits = grBits;
324
325    return (i);
326
327}
328
329