1 /* |
|
2 * Copyright (c) 2010 Ixonos Plc. |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - Initial contribution |
|
11 * |
|
12 * Contributors: |
|
13 * Ixonos Plc |
|
14 * |
|
15 * Description: |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 /************************************************************************** |
|
21 l3huffman.cpp - Huffman decoding subroutines for layer III. |
|
22 |
|
23 Author(s): Juha Ojanpera |
|
24 Copyright (c) 1999-2004 by Nokia Research Center, Speech and Audio Systems. |
|
25 *************************************************************************/ |
|
26 |
|
27 /************************************************************************** |
|
28 External Objects Needed |
|
29 *************************************************************************/ |
|
30 |
|
31 /*-- Project Headers --*/ |
|
32 #include "mpaud.h" |
|
33 #include "mp3tables.h" |
|
34 |
|
35 /************************************************************************** |
|
36 Internal Objects |
|
37 *************************************************************************/ |
|
38 |
|
39 /* |
|
40 Purpose: Table to hold the Huffman symbol codebooks. |
|
41 Explanation: - |
|
42 const int16 *huff_symbols_tbl[] = { |
|
43 NULL, huff_symbols1, huff_symbols2, huff_symbols3, NULL, huff_symbols5, |
|
44 huff_symbols6, huff_symbols7, huff_symbols8, huff_symbols9, huff_symbols10, |
|
45 huff_symbols11, huff_symbols12, huff_symbols13, NULL, huff_symbols15, |
|
46 huff_symbols16, huff_symbols24, huff_symbols32}; |
|
47 |
|
48 |
|
49 Purpose: Table to hold the Huffman codeword codebooks. |
|
50 Explanation: - |
|
51 |
|
52 const int16 *huff_cwords_tbl[] = { |
|
53 NULL, huff_cwords1, huff_cwords2, huff_cwords3, NULL, huff_cwords5, |
|
54 huff_cwords6, huff_cwords7, huff_cwords8, huff_cwords9, huff_cwords10, |
|
55 huff_cwords11, huff_cwords12, huff_cwords13, NULL, huff_cwords15, huff_cwords16, |
|
56 huff_cwords24, huff_cwords32}; |
|
57 */ |
|
58 |
|
59 /************************************************************************** |
|
60 Title : InitL3Huffman |
|
61 |
|
62 Purpose : Allocates resources for layer III Huffman decoding. |
|
63 |
|
64 Usage : InitL3Huffman(huf) |
|
65 |
|
66 Output : huf - Huffman tables |
|
67 |
|
68 Author(s) : Juha Ojanpera |
|
69 *************************************************************************/ |
|
70 |
|
71 void |
|
72 InitL3Huffman(CHuffman *huf) |
|
73 { |
|
74 int16 i, j; |
|
75 |
|
76 |
|
77 /* |
|
78 Purpose: Table to hold the Huffman symbol codebooks. |
|
79 Explanation: - */ |
|
80 const int16 *huff_symbols_tbl[] = { |
|
81 NULL, huff_symbols1, huff_symbols2, huff_symbols3, NULL, huff_symbols5, |
|
82 huff_symbols6, huff_symbols7, huff_symbols8, huff_symbols9, huff_symbols10, |
|
83 huff_symbols11, huff_symbols12, huff_symbols13, NULL, huff_symbols15, |
|
84 huff_symbols16, huff_symbols24, huff_symbols32}; |
|
85 |
|
86 /* |
|
87 Purpose: Table to hold the Huffman codeword codebooks. |
|
88 Explanation: - */ |
|
89 const int16 *huff_cwords_tbl[] = { |
|
90 NULL, huff_cwords1, huff_cwords2, huff_cwords3, NULL, huff_cwords5, |
|
91 huff_cwords6, huff_cwords7, huff_cwords8, huff_cwords9, huff_cwords10, |
|
92 huff_cwords11, huff_cwords12, huff_cwords13, NULL, huff_cwords15, huff_cwords16, |
|
93 huff_cwords24, huff_cwords32}; |
|
94 |
|
95 |
|
96 for(i = 0; i < 33; i++) |
|
97 { |
|
98 huf[i].tree_len = huf_tree_len[i]; |
|
99 huf[i].linbits = linbits[i]; |
|
100 } |
|
101 |
|
102 for(i = 0; i < 16; i++) |
|
103 { |
|
104 huf[i].codeword = huff_cwords_tbl[i]; |
|
105 huf[i].packed_symbols = huff_symbols_tbl[i]; |
|
106 } |
|
107 |
|
108 j = i; |
|
109 while(j < 24) |
|
110 { |
|
111 huf[j].codeword = huff_cwords_tbl[i]; |
|
112 huf[j].packed_symbols = huff_symbols_tbl[i]; |
|
113 j++; |
|
114 } |
|
115 |
|
116 i++; |
|
117 while(j < 32) |
|
118 { |
|
119 huf[j].codeword = huff_cwords_tbl[i]; |
|
120 huf[j].packed_symbols = huff_symbols_tbl[i]; |
|
121 j++; |
|
122 } |
|
123 |
|
124 i++; |
|
125 huf[j].codeword = huff_cwords_tbl[i]; |
|
126 huf[j].packed_symbols = huff_symbols_tbl[i]; |
|
127 } |
|
128 |
|
129 |
|
130 /************************************************************************** |
|
131 Title : L3decode_codeword |
|
132 |
|
133 Purpose : Decodes one Huffman codeword from the bitstream and returns |
|
134 the corresponding symbol. |
|
135 |
|
136 Usage : L3decode_codeword(br, h) |
|
137 |
|
138 Input : br - bitstream parameters |
|
139 h - Huffman table to be utilized |
|
140 |
|
141 Explanation : This decoding routine works only if the codewords are |
|
142 sorted according to their length, i.e, the codeword that has |
|
143 the minimum length appears first in the Huffman table, the |
|
144 length of the second codeword in the table is either greater |
|
145 or equal than the first one and so on. |
|
146 |
|
147 Author(s) : Juha Ojanpera |
|
148 *************************************************************************/ |
|
149 |
|
150 INLINE int16 |
|
151 L3decode_codeword(TBitStream *br, CHuffman *h) |
|
152 { |
|
153 const int16 *h_len = h->packed_symbols; |
|
154 const int16 *h_codeword = h->codeword; |
|
155 uint16 clen, items; |
|
156 uint16 shift, N = 19; /* N = 19 maximum codeword length */ |
|
157 uint32 cand_codeword; |
|
158 uint32 codeword; |
|
159 |
|
160 /* Read the maximum codeword. */ |
|
161 cand_codeword = BsLookAhead(br, N); |
|
162 |
|
163 /* |
|
164 * Length of the codeword that has the |
|
165 * highest probability. |
|
166 */ |
|
167 shift = N - (*h_len & 31); |
|
168 |
|
169 /* Candidate codeword for the first codebook entry. */ |
|
170 codeword = cand_codeword >> shift; |
|
171 |
|
172 /* |
|
173 * Sequentially goes through the whole Huffman table |
|
174 * until a match has been found. |
|
175 */ |
|
176 items = 1; |
|
177 while(items < h->tree_len && codeword != (uint16)*h_codeword) |
|
178 { |
|
179 h_len++; |
|
180 h_codeword++; |
|
181 items++; |
|
182 |
|
183 /* How many bits the codeword need to be updated. */ |
|
184 clen = *h_len & 31; |
|
185 if(clen) |
|
186 { |
|
187 shift -= clen; |
|
188 |
|
189 /* New candidate codeword. */ |
|
190 codeword = cand_codeword >> shift; |
|
191 } |
|
192 } |
|
193 |
|
194 /* # of bits actually read. */ |
|
195 BsSkipNBits(br, N - shift); |
|
196 |
|
197 /* Decoded Huffman symbol. */ |
|
198 return (*h_len >> 5); |
|
199 } |
|
200 |
|
201 /************************************************************************** |
|
202 Layer III subroutines for decoding spectral samples. |
|
203 |
|
204 Author(s): Juha Ojanpera |
|
205 Copyright (c) 1999-2004 by Nokia Research Center, Speech and Audio Systems. |
|
206 *************************************************************************/ |
|
207 |
|
208 int16 |
|
209 III_huffman_decode(CMP_Stream *mp, int16 gr, int16 ch, int32 part2) |
|
210 { |
|
211 int16 i, x, region1Start, region2Start, table_num, limit; |
|
212 int16 *quant, count1Len; |
|
213 TGranule_Info *gr_info; |
|
214 |
|
215 /* Quantized spectral samples. */ |
|
216 quant = mp->frame->ch_quant[ch]; |
|
217 |
|
218 /* Granule info for this frame. */ |
|
219 gr_info = mp->side_info->ch_info[ch]->gr_info[gr]; |
|
220 |
|
221 /* Find region boundaries. */ |
|
222 region1Start = mp->side_info->sfbData->sfbLong[gr_info->region0_count + 1]; |
|
223 region2Start = mp->side_info->sfbData->sfbLong[gr_info->region0_count + |
|
224 gr_info->region1_count + 2]; |
|
225 |
|
226 /* |
|
227 * How many samples actually need to be Huffman decoded. |
|
228 */ |
|
229 limit = mp->side_info->sfbData->bandLimit; |
|
230 if(gr_info->big_values > limit) |
|
231 gr_info->big_values = limit; |
|
232 |
|
233 /* Read bigvalues area. */ |
|
234 int16 section1 = MIN(gr_info->big_values, region1Start); |
|
235 if(section1 > 0) |
|
236 { |
|
237 pairtable(mp, section1, gr_info->table_select[0], quant); |
|
238 quant += section1; |
|
239 } |
|
240 |
|
241 int16 section2 = MIN(gr_info->big_values, region2Start) - region1Start; |
|
242 if(section2 > 0) |
|
243 { |
|
244 pairtable(mp, section2, gr_info->table_select[1], quant); |
|
245 quant += section2; |
|
246 } |
|
247 |
|
248 int16 section3 = gr_info->big_values - region2Start; |
|
249 if(section3 > 0) |
|
250 { |
|
251 pairtable(mp, section3, gr_info->table_select[2], quant); |
|
252 quant += section3; |
|
253 } |
|
254 |
|
255 count1Len = 0; |
|
256 i = gr_info->big_values; |
|
257 part2 += gr_info->part2_3_length; |
|
258 |
|
259 /* Check whether the samples between -1,...,1 need to be Huffman decoded. */ |
|
260 if(i < limit) |
|
261 { |
|
262 /* Read count1 area. */ |
|
263 table_num = 32 + ((gr_info->flags & COUNT_1_TABLE_SELECT) ? 1 : 0); |
|
264 |
|
265 x = quadtable(mp, i, part2, table_num, quant, limit); |
|
266 count1Len = x - i; |
|
267 quant += count1Len; |
|
268 count1Len >>= 2; |
|
269 i = x; |
|
270 } |
|
271 |
|
272 if(BsGetBitsRead(mp->br) > (uint32)part2) |
|
273 { |
|
274 count1Len--; |
|
275 quant -= 4; i -= 4; |
|
276 BsRewindNBits(mp->br, BsGetBitsRead(mp->br) - part2); |
|
277 } |
|
278 |
|
279 /* Dismiss stuffing Bits */ |
|
280 if(BsGetBitsRead(mp->br) < (uint32)part2) |
|
281 BsSkipNBits(mp->br, part2 - BsGetBitsRead(mp->br)); |
|
282 |
|
283 gr_info->zero_part_start = (i < limit) ? i : limit; |
|
284 |
|
285 return (count1Len); |
|
286 } |
|
287 |
|
288 void |
|
289 pairtable(CMP_Stream *mp, int16 section_length, int16 table_num, int16 *quant) |
|
290 { |
|
291 register CHuffman *h = &mp->huffman[table_num]; |
|
292 |
|
293 if(h->tree_len == 0) |
|
294 Mem::Fill(quant, section_length << 1 /** sizeof(int16)*/, 0); |
|
295 else |
|
296 { |
|
297 if(h->linbits) |
|
298 { |
|
299 register int16 *q = quant; |
|
300 |
|
301 for(int16 i = 0; i < section_length; i += 2, q += 2) |
|
302 { |
|
303 uint32 codeword = L3decode_codeword(mp->br, h); |
|
304 |
|
305 /* Unpack coefficients. */ |
|
306 *q = codeword >> 4; |
|
307 q[1] = codeword & 15; |
|
308 |
|
309 /* Read extra bits (if needed) and sign bits (if needed). */ |
|
310 if(*q == 15) |
|
311 *q += BsGetBits(mp->br, h->linbits); |
|
312 if(*q) |
|
313 *q = (BsGetBits(mp->br, 1)) ? -*q : *q; |
|
314 |
|
315 if(q[1] == 15) |
|
316 q[1] += BsGetBits(mp->br, h->linbits); |
|
317 if(q[1]) |
|
318 q[1] = (BsGetBits(mp->br, 1)) ? -q[1] : q[1]; |
|
319 } |
|
320 } |
|
321 else /* no linbits */ |
|
322 { |
|
323 register int16 *q = quant; |
|
324 |
|
325 for(int16 i = 0; i < section_length; i += 2, q += 2) |
|
326 { |
|
327 uint32 codeword = L3decode_codeword(mp->br, h); |
|
328 |
|
329 /* Unpack coefficients. */ |
|
330 *q = codeword >> 4; |
|
331 q[1] = codeword & 15; |
|
332 |
|
333 /* Read extra bits (not needed) and sign bits (if needed). */ |
|
334 if(*q) |
|
335 *q = (BsGetBits(mp->br, 1)) ? -*q : *q; |
|
336 |
|
337 if(q[1]) |
|
338 q[1] = (BsGetBits(mp->br, 1)) ? -q[1] : q[1]; |
|
339 } |
|
340 } |
|
341 } |
|
342 } |
|
343 |
|
344 int16 |
|
345 quadtable(CMP_Stream *mp, int16 start, int16 part2, int16 table_num, int16 *quant, |
|
346 int16 max_sfb_bins) |
|
347 { |
|
348 int16 i; |
|
349 register int16 *q = quant; |
|
350 |
|
351 for(i = start; (BsGetBitsRead(mp->br) < (uint32)part2 && i < max_sfb_bins); |
|
352 i += 4, q += 4) |
|
353 { |
|
354 uint32 codeword; |
|
355 uint16 mask = 8, sbits = 0; |
|
356 |
|
357 if(table_num == 33) |
|
358 codeword = 15 - BsGetBits(mp->br, 4); |
|
359 else |
|
360 codeword = L3decode_codeword(mp->br, &mp->huffman[table_num]); |
|
361 |
|
362 /* Unpack coefficients. */ |
|
363 *q = (codeword >> 3) & 1; |
|
364 q[1] = (codeword >> 2) & 1; |
|
365 q[2] = (codeword >> 1) & 1; |
|
366 q[3] = codeword & 1; |
|
367 |
|
368 /* Sign bits. */ |
|
369 codeword = BsLookAhead(mp->br, 4); |
|
370 if(*q) { sbits++; *q = (codeword & mask) ? -*q : *q; mask >>= 1; } |
|
371 if(q[1]) { sbits++; q[1] = (codeword & mask) ? -q[1] : q[1]; mask >>= 1; } |
|
372 if(q[2]) { sbits++; q[2] = (codeword & mask) ? -q[2] : q[2]; mask >>= 1; } |
|
373 if(q[3]) { sbits++; q[3] = (codeword & mask) ? -q[3] : q[3]; mask >>= 1; } |
|
374 if(sbits) BsSkipBits(mp->br, sbits); |
|
375 } |
|
376 |
|
377 return (i); |
|
378 } |
|