OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
huffman.c
Go to the documentation of this file.
1/* ***** BEGIN LICENSE BLOCK *****
2 * Version: RCSL 1.0/RPSL 1.0
3 *
4 * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
5 *
6 * The contents of this file, and the files included with this file, are
7 * subject to the current version of the RealNetworks Public Source License
8 * Version 1.0 (the "RPSL") available at
9 * http://www.helixcommunity.org/content/rpsl unless you have licensed
10 * the file under the RealNetworks Community Source License Version 1.0
11 * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
12 * in which case the RCSL will apply. You may also obtain the license terms
13 * directly from RealNetworks. You may not use this file except in
14 * compliance with the RPSL or, if you have a valid RCSL with RealNetworks
15 * applicable to this file, the RCSL. Please see the applicable RPSL or
16 * RCSL for the rights, obligations and limitations governing use of the
17 * contents of the file.
18 *
19 * This file is part of the Helix DNA Technology. RealNetworks is the
20 * developer of the Original Code and owns the copyrights in the portions
21 * it created.
22 *
23 * This file, and the files included with this file, is distributed and made
24 * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
25 * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
26 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
27 * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
28 *
29 * Technology Compatibility Kit Test Suite(s) Location:
30 * http://www.helixcommunity.org/content/tck
31 *
32 * Contributor(s):
33 *
34 * ***** END LICENSE BLOCK ***** */
35
36/**************************************************************************************
37 * Fixed-point MP3 decoder
38 * Jon Recker (jrecker@real.com), Ken Cooke (kenc@real.com)
39 * July 2003
40 *
41 * huffman.c - Huffman decoding of transform coefficients
42 **************************************************************************************/
43
44#include "coder.h"
45
46/* helper macros - see comments in hufftabs.c about the format of the huffman tables */
47#define GetMaxbits(x) ((int)( (((unsigned short)(x)) >> 0) & 0x000f))
48#define GetHLen(x) ((int)( (((unsigned short)(x)) >> 12) & 0x000f))
49#define GetCWY(x) ((int)( (((unsigned short)(x)) >> 8) & 0x000f))
50#define GetCWX(x) ((int)( (((unsigned short)(x)) >> 4) & 0x000f))
51#define GetSignBits(x) ((int)( (((unsigned short)(x)) >> 0) & 0x000f))
52
53#define GetHLenQ(x) ((int)( (((unsigned char)(x)) >> 4) & 0x0f))
54#define GetCWVQ(x) ((int)( (((unsigned char)(x)) >> 3) & 0x01))
55#define GetCWWQ(x) ((int)( (((unsigned char)(x)) >> 2) & 0x01))
56#define GetCWXQ(x) ((int)( (((unsigned char)(x)) >> 1) & 0x01))
57#define GetCWYQ(x) ((int)( (((unsigned char)(x)) >> 0) & 0x01))
58
59/* apply sign of s to the positive number x (save in MSB, will do two's complement in dequant) */
60#define ApplySign(x, s) { (x) |= ((s) & 0x80000000); }
61
62/**************************************************************************************
63 * Function: DecodeHuffmanPairs
64 *
65 * Description: decode 2-way vector Huffman codes in the "bigValues" region of spectrum
66 *
67 * Inputs: valid BitStreamInfo struct, pointing to start of pair-wise codes
68 * pointer to xy buffer to received decoded values
69 * number of codewords to decode
70 * index of Huffman table to use
71 * number of bits remaining in bitstream
72 *
73 * Outputs: pairs of decoded coefficients in vwxy
74 * updated BitStreamInfo struct
75 *
76 * Return: number of bits used, or -1 if out of bits
77 *
78 * Notes: assumes that nVals is an even number
79 * si_huff.bit tests every Huffman codeword in every table (though not
80 * necessarily all linBits outputs for x,y > 15)
81 **************************************************************************************/
82// no improvement with section=data
83static int DecodeHuffmanPairs(int *xy, int nVals, int tabIdx, int bitsLeft, unsigned char *buf, int bitOffset)
84{
85 int i, x, y;
86 int cachedBits, padBits, len, startBits, linBits, maxBits, minBits;
87 HuffTabType tabType;
88 unsigned short cw, *tBase, *tCurr;
89 unsigned int cache;
90
91 if(nVals <= 0)
92 return 0;
93
94 if (bitsLeft < 0)
95 return -1;
96 startBits = bitsLeft;
97
98 tBase = (unsigned short *)(huffTable + huffTabOffset[tabIdx]);
99 linBits = huffTabLookup[tabIdx].linBits;
100 tabType = huffTabLookup[tabIdx].tabType;
101
102 ASSERT(!(nVals & 0x01));
103 ASSERT(tabIdx < HUFF_PAIRTABS);
104 ASSERT(tabIdx >= 0);
105 ASSERT(tabType != invalidTab);
106
107 /* initially fill cache with any partial byte */
108 cache = 0;
109 cachedBits = (8 - bitOffset) & 0x07;
110 if (cachedBits)
111 cache = (unsigned int)(*buf++) << (32 - cachedBits);
112 bitsLeft -= cachedBits;
113
114 if (tabType == noBits) {
115 /* table 0, no data, x = y = 0 */
116 for (i = 0; i < nVals; i+=2) {
117 xy[i+0] = 0;
118 xy[i+1] = 0;
119 }
120 return 0;
121 } else if (tabType == oneShot) {
122 /* single lookup, no escapes */
123 maxBits = GetMaxbits(tBase[0]);
124 tBase++;
125 padBits = 0;
126 while (nVals > 0) {
127 /* refill cache - assumes cachedBits <= 16 */
128 if (bitsLeft >= 16) {
129 /* load 2 new bytes into left-justified cache */
130 cache |= (unsigned int)(*buf++) << (24 - cachedBits);
131 cache |= (unsigned int)(*buf++) << (16 - cachedBits);
132 cachedBits += 16;
133 bitsLeft -= 16;
134 } else {
135 /* last time through, pad cache with zeros and drain cache */
136 if (cachedBits + bitsLeft <= 0) return -1;
137 if (bitsLeft > 0) cache |= (unsigned int)(*buf++) << (24 - cachedBits);
138 if (bitsLeft > 8) cache |= (unsigned int)(*buf++) << (16 - cachedBits);
139 cachedBits += bitsLeft;
140 bitsLeft = 0;
141
142 cache &= (signed int)0x80000000 >> (cachedBits - 1);
143 padBits = 11;
144 cachedBits += padBits; /* okay if this is > 32 (0's automatically shifted in from right) */
145 }
146
147 /* largest maxBits = 9, plus 2 for sign bits, so make sure cache has at least 11 bits */
148 while (nVals > 0 && cachedBits >= 11 ) {
149 cw = tBase[cache >> (32 - maxBits)];
150 len = GetHLen(cw);
151 cachedBits -= len;
152 cache <<= len;
153
154 x = GetCWX(cw); if (x) {ApplySign(x, cache); cache <<= 1; cachedBits--;}
155 y = GetCWY(cw); if (y) {ApplySign(y, cache); cache <<= 1; cachedBits--;}
156
157 /* ran out of bits - should never have consumed padBits */
158 if (cachedBits < padBits)
159 return -1;
160
161 *xy++ = x;
162 *xy++ = y;
163 nVals -= 2;
164 }
165 }
166 bitsLeft += (cachedBits - padBits);
167 return (startBits - bitsLeft);
168 } else if (tabType == loopLinbits || tabType == loopNoLinbits) {
169 tCurr = tBase;
170 padBits = 0;
171 while (nVals > 0) {
172 /* refill cache - assumes cachedBits <= 16 */
173 if (bitsLeft >= 16) {
174 /* load 2 new bytes into left-justified cache */
175 cache |= (unsigned int)(*buf++) << (24 - cachedBits);
176 cache |= (unsigned int)(*buf++) << (16 - cachedBits);
177 cachedBits += 16;
178 bitsLeft -= 16;
179 } else {
180 /* last time through, pad cache with zeros and drain cache */
181 if (cachedBits + bitsLeft <= 0) return -1;
182 if (bitsLeft > 0) cache |= (unsigned int)(*buf++) << (24 - cachedBits);
183 if (bitsLeft > 8) cache |= (unsigned int)(*buf++) << (16 - cachedBits);
184 cachedBits += bitsLeft;
185 bitsLeft = 0;
186
187 cache &= (signed int)0x80000000 >> (cachedBits - 1);
188 padBits = 11;
189 cachedBits += padBits; /* okay if this is > 32 (0's automatically shifted in from right) */
190 }
191
192 /* largest maxBits = 9, plus 2 for sign bits, so make sure cache has at least 11 bits */
193 while (nVals > 0 && cachedBits >= 11 ) {
194 maxBits = GetMaxbits(tCurr[0]);
195 cw = tCurr[(cache >> (32 - maxBits)) + 1];
196 len = GetHLen(cw);
197 if (!len) {
198 cachedBits -= maxBits;
199 cache <<= maxBits;
200 tCurr += cw;
201 continue;
202 }
203 cachedBits -= len;
204 cache <<= len;
205
206 x = GetCWX(cw);
207 y = GetCWY(cw);
208
209 if (x == 15 && tabType == loopLinbits) {
210 minBits = linBits + 1 + (y ? 1 : 0);
211 if (cachedBits + bitsLeft < minBits)
212 return -1;
213 while (cachedBits < minBits) {
214 cache |= (unsigned int)(*buf++) << (24 - cachedBits);
215 cachedBits += 8;
216 bitsLeft -= 8;
217 }
218 if (bitsLeft < 0) {
219 cachedBits += bitsLeft;
220 bitsLeft = 0;
221 cache &= (signed int)0x80000000 >> (cachedBits - 1);
222 }
223 x += (int)(cache >> (32 - linBits));
224 cachedBits -= linBits;
225 cache <<= linBits;
226 }
227 if (x) {ApplySign(x, cache); cache <<= 1; cachedBits--;}
228
229 if (y == 15 && tabType == loopLinbits) {
230 minBits = linBits + 1;
231 if (cachedBits + bitsLeft < minBits)
232 return -1;
233 while (cachedBits < minBits) {
234 cache |= (unsigned int)(*buf++) << (24 - cachedBits);
235 cachedBits += 8;
236 bitsLeft -= 8;
237 }
238 if (bitsLeft < 0) {
239 cachedBits += bitsLeft;
240 bitsLeft = 0;
241 cache &= (signed int)0x80000000 >> (cachedBits - 1);
242 }
243 y += (int)(cache >> (32 - linBits));
244 cachedBits -= linBits;
245 cache <<= linBits;
246 }
247 if (y) {ApplySign(y, cache); cache <<= 1; cachedBits--;}
248
249 /* ran out of bits - should never have consumed padBits */
250 if (cachedBits < padBits)
251 return -1;
252
253 *xy++ = x;
254 *xy++ = y;
255 nVals -= 2;
256 tCurr = tBase;
257 }
258 }
259 bitsLeft += (cachedBits - padBits);
260 return (startBits - bitsLeft);
261 }
262
263 /* error in bitstream - trying to access unused Huffman table */
264 return -1;
265}
266
267/**************************************************************************************
268 * Function: DecodeHuffmanQuads
269 *
270 * Description: decode 4-way vector Huffman codes in the "count1" region of spectrum
271 *
272 * Inputs: valid BitStreamInfo struct, pointing to start of quadword codes
273 * pointer to vwxy buffer to received decoded values
274 * maximum number of codewords to decode
275 * index of quadword table (0 = table A, 1 = table B)
276 * number of bits remaining in bitstream
277 *
278 * Outputs: quadruples of decoded coefficients in vwxy
279 * updated BitStreamInfo struct
280 *
281 * Return: index of the first "zero_part" value (index of the first sample
282 * of the quad word after which all samples are 0)
283 *
284 * Notes: si_huff.bit tests every vwxy output in both quad tables
285 **************************************************************************************/
286// no improvement with section=data
287static int DecodeHuffmanQuads(int *vwxy, int nVals, int tabIdx, int bitsLeft, unsigned char *buf, int bitOffset)
288{
289 int i, v, w, x, y;
290 int len, maxBits, cachedBits, padBits;
291 unsigned int cache;
292 unsigned char cw, *tBase;
293
294 if (bitsLeft <= 0)
295 return 0;
296
297 tBase = (unsigned char *)quadTable + quadTabOffset[tabIdx];
298 maxBits = quadTabMaxBits[tabIdx];
299
300 /* initially fill cache with any partial byte */
301 cache = 0;
302 cachedBits = (8 - bitOffset) & 0x07;
303 if (cachedBits)
304 cache = (unsigned int)(*buf++) << (32 - cachedBits);
305 bitsLeft -= cachedBits;
306
307 i = padBits = 0;
308 while (i < (nVals - 3)) {
309 /* refill cache - assumes cachedBits <= 16 */
310 if (bitsLeft >= 16) {
311 /* load 2 new bytes into left-justified cache */
312 cache |= (unsigned int)(*buf++) << (24 - cachedBits);
313 cache |= (unsigned int)(*buf++) << (16 - cachedBits);
314 cachedBits += 16;
315 bitsLeft -= 16;
316 } else {
317 /* last time through, pad cache with zeros and drain cache */
318 if (cachedBits + bitsLeft <= 0) return i;
319 if (bitsLeft > 0) cache |= (unsigned int)(*buf++) << (24 - cachedBits);
320 if (bitsLeft > 8) cache |= (unsigned int)(*buf++) << (16 - cachedBits);
321 cachedBits += bitsLeft;
322 bitsLeft = 0;
323
324 cache &= (signed int)0x80000000 >> (cachedBits - 1);
325 padBits = 10;
326 cachedBits += padBits; /* okay if this is > 32 (0's automatically shifted in from right) */
327 }
328
329 /* largest maxBits = 6, plus 4 for sign bits, so make sure cache has at least 10 bits */
330 while (i < (nVals - 3) && cachedBits >= 10 ) {
331 cw = tBase[cache >> (32 - maxBits)];
332 len = GetHLenQ(cw);
333 cachedBits -= len;
334 cache <<= len;
335
336 v = GetCWVQ(cw); if(v) {ApplySign(v, cache); cache <<= 1; cachedBits--;}
337 w = GetCWWQ(cw); if(w) {ApplySign(w, cache); cache <<= 1; cachedBits--;}
338 x = GetCWXQ(cw); if(x) {ApplySign(x, cache); cache <<= 1; cachedBits--;}
339 y = GetCWYQ(cw); if(y) {ApplySign(y, cache); cache <<= 1; cachedBits--;}
340
341 /* ran out of bits - okay (means we're done) */
342 if (cachedBits < padBits)
343 return i;
344
345 *vwxy++ = v;
346 *vwxy++ = w;
347 *vwxy++ = x;
348 *vwxy++ = y;
349 i += 4;
350 }
351 }
352
353 /* decoded max number of quad values */
354 return i;
355}
356
357/**************************************************************************************
358 * Function: DecodeHuffman
359 *
360 * Description: decode one granule, one channel worth of Huffman codes
361 *
362 * Inputs: MP3DecInfo structure filled by UnpackFrameHeader(), UnpackSideInfo(),
363 * and UnpackScaleFactors() (for this granule)
364 * buffer pointing to start of Huffman data in MP3 frame
365 * pointer to bit offset (0-7) indicating starting bit in buf[0]
366 * number of bits in the Huffman data section of the frame
367 * (could include padding bits)
368 * index of current granule and channel
369 *
370 * Outputs: decoded coefficients in hi->huffDecBuf[ch] (hi pointer in mp3DecInfo)
371 * updated bitOffset
372 *
373 * Return: length (in bytes) of Huffman codes
374 * bitOffset also returned in parameter (0 = MSB, 7 = LSB of
375 * byte located at buf + offset)
376 * -1 if null input pointers, huffBlockBits < 0, or decoder runs
377 * out of bits prematurely (invalid bitstream)
378 **************************************************************************************/
379// .data about 1ms faster per frame
380int DecodeHuffman(MP3DecInfo *mp3DecInfo, unsigned char *buf, int *bitOffset, int huffBlockBits, int gr, int ch)
381{
382 int r1Start, r2Start, rEnd[4]; /* region boundaries */
383 int i, w, bitsUsed, bitsLeft;
384 unsigned char *startBuf = buf;
385
386 FrameHeader *fh;
387 SideInfo *si;
388 SideInfoSub *sis;
389 HuffmanInfo *hi;
390
391 /* validate pointers */
392 if (!mp3DecInfo || !mp3DecInfo->FrameHeaderPS || !mp3DecInfo->SideInfoPS || !mp3DecInfo->ScaleFactorInfoPS || !mp3DecInfo->HuffmanInfoPS)
393 return -1;
394
395 fh = ((FrameHeader *)(mp3DecInfo->FrameHeaderPS));
396 si = ((SideInfo *)(mp3DecInfo->SideInfoPS));
397 sis = &si->sis[gr][ch];
398 hi = (HuffmanInfo*)(mp3DecInfo->HuffmanInfoPS);
399
400 if (huffBlockBits < 0)
401 return -1;
402
403 /* figure out region boundaries (the first 2*bigVals coefficients divided into 3 regions) */
404 if (sis->winSwitchFlag && sis->blockType == 2) {
405 if (sis->mixedBlock == 0) {
406 r1Start = fh->sfBand->s[(sis->region0Count + 1)/3] * 3;
407 } else {
408 if (fh->ver == MPEG1) {
409 r1Start = fh->sfBand->l[sis->region0Count + 1];
410 } else {
411 /* see MPEG2 spec for explanation */
412 w = fh->sfBand->s[4] - fh->sfBand->s[3];
413 r1Start = fh->sfBand->l[6] + 2*w;
414 }
415 }
416 r2Start = MAX_NSAMP; /* short blocks don't have region 2 */
417 } else {
418 r1Start = fh->sfBand->l[sis->region0Count + 1];
419 r2Start = fh->sfBand->l[sis->region0Count + 1 + sis->region1Count + 1];
420 }
421
422 /* offset rEnd index by 1 so first region = rEnd[1] - rEnd[0], etc. */
423 rEnd[3] = MIN(MAX_NSAMP, 2 * sis->nBigvals);
424 rEnd[2] = MIN(r2Start, rEnd[3]);
425 rEnd[1] = MIN(r1Start, rEnd[3]);
426 rEnd[0] = 0;
427
428 /* rounds up to first all-zero pair (we don't check last pair for (x,y) == (non-zero, zero)) */
429 hi->nonZeroBound[ch] = rEnd[3];
430
431 /* decode Huffman pairs (rEnd[i] are always even numbers) */
432 bitsLeft = huffBlockBits;
433 for (i = 0; i < 3; i++) {
434 bitsUsed = DecodeHuffmanPairs(hi->huffDecBuf[ch] + rEnd[i], rEnd[i+1] - rEnd[i], sis->tableSelect[i], bitsLeft, buf, *bitOffset);
435 if (bitsUsed < 0 || bitsUsed > bitsLeft) /* error - overran end of bitstream */
436 return -1;
437
438 /* update bitstream position */
439 buf += (bitsUsed + *bitOffset) >> 3;
440 *bitOffset = (bitsUsed + *bitOffset) & 0x07;
441 bitsLeft -= bitsUsed;
442 }
443
444 /* decode Huffman quads (if any) */
445 hi->nonZeroBound[ch] += DecodeHuffmanQuads(hi->huffDecBuf[ch] + rEnd[3], MAX_NSAMP - rEnd[3], sis->count1TableSelect, bitsLeft, buf, *bitOffset);
446
447 ASSERT(hi->nonZeroBound[ch] <= MAX_NSAMP);
448 for (i = hi->nonZeroBound[ch]; i < MAX_NSAMP; i++)
449 hi->huffDecBuf[ch][i] = 0;
450
451 /* If bits used for 576 samples < huffBlockBits, then the extras are considered
452 * to be stuffing bits (throw away, but need to return correct bitstream position)
453 */
454 buf += (bitsLeft + *bitOffset) >> 3;
455 *bitOffset = (bitsLeft + *bitOffset) & 0x07;
456
457 return (int)(buf - startBuf);
458}
459
UINT16 y
Definition BmfFile.h:84
UINT16 x
Definition BmfFile.h:83
#define HUFF_PAIRTABS
Definition coder.h:86
#define quadTabOffset
Definition coder.h:117
#define huffTabLookup
Definition coder.h:115
enum _HuffTabType HuffTabType
#define huffTabOffset
Definition coder.h:114
#define huffTable
Definition coder.h:113
#define quadTable
Definition coder.h:116
#define ASSERT(x)
Definition coder.h:55
@ oneShot
Definition coder.h:198
@ loopNoLinbits
Definition coder.h:199
@ invalidTab
Definition coder.h:203
@ noBits
Definition coder.h:197
@ loopLinbits
Definition coder.h:200
#define quadTabMaxBits
Definition coder.h:118
#define MIN(a, b)
Definition deflate.c:1673
#define GetCWY(x)
Definition huffman.c:49
#define GetCWX(x)
Definition huffman.c:50
#define GetMaxbits(x)
Definition huffman.c:47
#define GetCWVQ(x)
Definition huffman.c:54
#define GetHLen(x)
Definition huffman.c:48
#define GetCWWQ(x)
Definition huffman.c:55
#define GetHLenQ(x)
Definition huffman.c:53
#define ApplySign(x, s)
Definition huffman.c:60
#define GetCWXQ(x)
Definition huffman.c:56
#define GetCWYQ(x)
Definition huffman.c:57
@ MPEG1
Definition mp3dec.h:84
#define MAX_NSAMP
Definition mp3dec.h:80
#define DecodeHuffman
Definition statname.h:68
const SFBandTable * sfBand
Definition coder.h:150
MPEGVersion ver
Definition coder.h:136
int nonZeroBound[MAX_NCHAN]
Definition coder.h:192
int huffDecBuf[MAX_NCHAN][MAX_NSAMP]
Definition coder.h:191
void * FrameHeaderPS
Definition mp3common.h:66
void * ScaleFactorInfoPS
Definition mp3common.h:68
void * SideInfoPS
Definition mp3common.h:67
void * HuffmanInfoPS
Definition mp3common.h:69
short l[23]
Definition mp3common.h:99
short s[14]
Definition mp3common.h:100
SideInfoSub sis[MAX_NGRAN][MAX_NCHAN]
Definition coder.h:175
int mixedBlock
Definition coder.h:160
int nBigvals
Definition coder.h:155
int count1TableSelect
Definition coder.h:167
int winSwitchFlag
Definition coder.h:158
int tableSelect[3]
Definition coder.h:161
int region1Count
Definition coder.h:164
int region0Count
Definition coder.h:163
int blockType
Definition coder.h:159