FastLED 3.9.15
Loading...
Searching...
No Matches
huffman.hpp
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#include "fl/stl/stdint.h"
46#include "fl/stl/noexcept.h"
47
48namespace fl {
49namespace third_party {
50
51/* helper macros - see comments in hufftabs.c about the format of the huffman tables */
52#define GetMaxbits(x) ((int)( (((unsigned short)(x)) >> 0) & 0x000f))
53#define GetHLen(x) ((int)( (((unsigned short)(x)) >> 12) & 0x000f))
54#define GetCWY(x) ((int)( (((unsigned short)(x)) >> 8) & 0x000f))
55#define GetCWX(x) ((int)( (((unsigned short)(x)) >> 4) & 0x000f))
56#define GetSignBits(x) ((int)( (((unsigned short)(x)) >> 0) & 0x000f))
57
58#define GetHLenQ(x) ((int)( (((unsigned char)(x)) >> 4) & 0x0f))
59#define GetCWVQ(x) ((int)( (((unsigned char)(x)) >> 3) & 0x01))
60#define GetCWWQ(x) ((int)( (((unsigned char)(x)) >> 2) & 0x01))
61#define GetCWXQ(x) ((int)( (((unsigned char)(x)) >> 1) & 0x01))
62#define GetCWYQ(x) ((int)( (((unsigned char)(x)) >> 0) & 0x01))
63
64/* apply sign of s to the positive number x (save in MSB, will do two's complement in dequant) */
65#define ApplySign(x, s) { (x) |= ((s) & (int32_t)0x80000000); }
66
67/**************************************************************************************
68 * Function: DecodeHuffmanPairs
69 *
70 * Description: decode 2-way vector Huffman codes in the "bigValues" region of spectrum
71 *
72 * Inputs: valid BitStreamInfo struct, pointing to start of pair-wise codes
73 * pointer to xy buffer to received decoded values
74 * number of codewords to decode
75 * index of Huffman table to use
76 * number of bits remaining in bitstream
77 *
78 * Outputs: pairs of decoded coefficients in vwxy
79 * updated BitStreamInfo struct
80 *
81 * Return: number of bits used, or -1 if out of bits
82 *
83 * Notes: assumes that nVals is an even number
84 * si_huff.bit tests every Huffman codeword in every table (though not
85 * necessarily all linBits outputs for x,y > 15)
86 **************************************************************************************/
87// no improvement with section=data
88static int DecodeHuffmanPairs(int32_t *xy, int nVals, int tabIdx, int bitsLeft, const unsigned char *buf, int bitOffset) FL_NOEXCEPT
89{
90 int i, x, y;
91 int cachedBits, padBits, len, startBits, linBits, maxBits, minBits;
92 HuffTabType tabType;
93 unsigned short cw, *tBase, *tCurr;
94 unsigned int cache;
95
96 if(nVals <= 0)
97 return 0;
98
99 if (bitsLeft < 0)
100 return -1;
101 startBits = bitsLeft;
102
103 tBase = (unsigned short *)(huffTable + huffTabOffset[tabIdx]);
104 linBits = huffTabLookup[tabIdx].linBits;
105 tabType = huffTabLookup[tabIdx].tabType;
106
107 ASSERT(!(nVals & 0x01));
108 ASSERT(tabIdx < HUFF_PAIRTABS);
109 ASSERT(tabIdx >= 0);
110 ASSERT(tabType != invalidTab);
111
112 /* initially fill cache with any partial byte */
113 cache = 0;
114 cachedBits = (8 - bitOffset) & 0x07;
115 if (cachedBits)
116 cache = (unsigned int)(*buf++) << (32 - cachedBits);
117 bitsLeft -= cachedBits;
118
119 if (tabType == noBits) {
120 /* table 0, no data, x = y = 0 */
121 for (i = 0; i < nVals; i+=2) {
122 xy[i+0] = 0;
123 xy[i+1] = 0;
124 }
125 return 0;
126 } else if (tabType == oneShot) {
127 /* single lookup, no escapes */
128 maxBits = GetMaxbits(tBase[0]);
129 tBase++;
130 padBits = 0;
131 while (nVals > 0) {
132 /* refill cache - assumes cachedBits <= 16 */
133 if (bitsLeft >= 16) {
134 /* load 2 new bytes into left-justified cache */
135 cache |= (unsigned int)(*buf++) << (24 - cachedBits);
136 cache |= (unsigned int)(*buf++) << (16 - cachedBits);
137 cachedBits += 16;
138 bitsLeft -= 16;
139 } else {
140 /* last time through, pad cache with zeros and drain cache */
141 if (cachedBits + bitsLeft <= 0) return -1;
142 if (bitsLeft > 0) cache |= (unsigned int)(*buf++) << (24 - cachedBits);
143 if (bitsLeft > 8) cache |= (unsigned int)(*buf++) << (16 - cachedBits);
144 cachedBits += bitsLeft;
145 bitsLeft = 0;
146
147 cache &= (signed int)(int32_t)0x80000000 >> (cachedBits - 1);
148 padBits = 11;
149 cachedBits += padBits; /* okay if this is > 32 (0's automatically shifted in from right) */
150 }
151
152 /* largest maxBits = 9, plus 2 for sign bits, so make sure cache has at least 11 bits */
153 while (nVals > 0 && cachedBits >= 11 ) {
154 cw = tBase[cache >> (32 - maxBits)];
155 len = GetHLen(cw);
156 cachedBits -= len;
157 cache <<= len;
158
159 x = GetCWX(cw); if (x) {ApplySign(x, cache); cache <<= 1; cachedBits--;}
160 y = GetCWY(cw); if (y) {ApplySign(y, cache); cache <<= 1; cachedBits--;}
161
162 /* ran out of bits - should never have consumed padBits */
163 if (cachedBits < padBits)
164 return -1;
165
166 *xy++ = x;
167 *xy++ = y;
168 nVals -= 2;
169 }
170 }
171 bitsLeft += (cachedBits - padBits);
172 return (startBits - bitsLeft);
173 } else if (tabType == loopLinbits || tabType == loopNoLinbits) {
174 tCurr = tBase;
175 padBits = 0;
176 while (nVals > 0) {
177 /* refill cache - assumes cachedBits <= 16 */
178 if (bitsLeft >= 16) {
179 /* load 2 new bytes into left-justified cache */
180 cache |= (unsigned int)(*buf++) << (24 - cachedBits);
181 cache |= (unsigned int)(*buf++) << (16 - cachedBits);
182 cachedBits += 16;
183 bitsLeft -= 16;
184 } else {
185 /* last time through, pad cache with zeros and drain cache */
186 if (cachedBits + bitsLeft <= 0) return -1;
187 if (bitsLeft > 0) cache |= (unsigned int)(*buf++) << (24 - cachedBits);
188 if (bitsLeft > 8) cache |= (unsigned int)(*buf++) << (16 - cachedBits);
189 cachedBits += bitsLeft;
190 bitsLeft = 0;
191
192 cache &= (signed int)(int32_t)0x80000000 >> (cachedBits - 1);
193 padBits = 11;
194 cachedBits += padBits; /* okay if this is > 32 (0's automatically shifted in from right) */
195 }
196
197 /* largest maxBits = 9, plus 2 for sign bits, so make sure cache has at least 11 bits */
198 while (nVals > 0 && cachedBits >= 11 ) {
199 maxBits = GetMaxbits(tCurr[0]);
200 cw = tCurr[(cache >> (32 - maxBits)) + 1];
201 len = GetHLen(cw);
202 if (!len) {
203 cachedBits -= maxBits;
204 cache <<= maxBits;
205 tCurr += cw;
206 continue;
207 }
208 cachedBits -= len;
209 cache <<= len;
210
211 x = GetCWX(cw);
212 y = GetCWY(cw);
213
214 if (x == 15 && tabType == loopLinbits) {
215 minBits = linBits + 1 + (y ? 1 : 0);
216 if (cachedBits + bitsLeft < minBits)
217 return -1;
218 while (cachedBits < minBits) {
219 cache |= (unsigned int)(*buf++) << (24 - cachedBits);
220 cachedBits += 8;
221 bitsLeft -= 8;
222 }
223 if (bitsLeft < 0) {
224 cachedBits += bitsLeft;
225 bitsLeft = 0;
226 cache &= (signed int)(int32_t)0x80000000 >> (cachedBits - 1);
227 }
228 x += (int)(cache >> (32 - linBits));
229 cachedBits -= linBits;
230 cache <<= linBits;
231 }
232 if (x) {ApplySign(x, cache); cache <<= 1; cachedBits--;}
233
234 if (y == 15 && tabType == loopLinbits) {
235 minBits = linBits + 1;
236 if (cachedBits + bitsLeft < minBits)
237 return -1;
238 while (cachedBits < minBits) {
239 cache |= (unsigned int)(*buf++) << (24 - cachedBits);
240 cachedBits += 8;
241 bitsLeft -= 8;
242 }
243 if (bitsLeft < 0) {
244 cachedBits += bitsLeft;
245 bitsLeft = 0;
246 cache &= (signed int)(int32_t)0x80000000 >> (cachedBits - 1);
247 }
248 y += (int)(cache >> (32 - linBits));
249 cachedBits -= linBits;
250 cache <<= linBits;
251 }
252 if (y) {ApplySign(y, cache); cache <<= 1; cachedBits--;}
253
254 /* ran out of bits - should never have consumed padBits */
255 if (cachedBits < padBits)
256 return -1;
257
258 *xy++ = x;
259 *xy++ = y;
260 nVals -= 2;
261 tCurr = tBase;
262 }
263 }
264 bitsLeft += (cachedBits - padBits);
265 return (startBits - bitsLeft);
266 }
267
268 /* error in bitstream - trying to access unused Huffman table */
269 return -1;
270}
271
272/**************************************************************************************
273 * Function: DecodeHuffmanQuads
274 *
275 * Description: decode 4-way vector Huffman codes in the "count1" region of spectrum
276 *
277 * Inputs: valid BitStreamInfo struct, pointing to start of quadword codes
278 * pointer to vwxy buffer to received decoded values
279 * maximum number of codewords to decode
280 * index of quadword table (0 = table A, 1 = table B)
281 * number of bits remaining in bitstream
282 *
283 * Outputs: quadruples of decoded coefficients in vwxy
284 * updated BitStreamInfo struct
285 *
286 * Return: index of the first "zero_part" value (index of the first sample
287 * of the quad word after which all samples are 0)
288 *
289 * Notes: si_huff.bit tests every vwxy output in both quad tables
290 **************************************************************************************/
291// no improvement with section=data
292static int DecodeHuffmanQuads(int32_t *vwxy, int nVals, int tabIdx, int bitsLeft, const unsigned char *buf, int bitOffset) FL_NOEXCEPT
293{
294 int i, v, w, x, y;
295 int len, maxBits, cachedBits, padBits;
296 unsigned int cache;
297 unsigned char cw, *tBase;
298
299 if (bitsLeft <= 0)
300 return 0;
301
302 tBase = (unsigned char *)quadTable + quadTabOffset[tabIdx];
303 maxBits = quadTabMaxBits[tabIdx];
304
305 /* initially fill cache with any partial byte */
306 cache = 0;
307 cachedBits = (8 - bitOffset) & 0x07;
308 if (cachedBits)
309 cache = (unsigned int)(*buf++) << (32 - cachedBits);
310 bitsLeft -= cachedBits;
311
312 i = padBits = 0;
313 while (i < (nVals - 3)) {
314 /* refill cache - assumes cachedBits <= 16 */
315 if (bitsLeft >= 16) {
316 /* load 2 new bytes into left-justified cache */
317 cache |= (unsigned int)(*buf++) << (24 - cachedBits);
318 cache |= (unsigned int)(*buf++) << (16 - cachedBits);
319 cachedBits += 16;
320 bitsLeft -= 16;
321 } else {
322 /* last time through, pad cache with zeros and drain cache */
323 if (cachedBits + bitsLeft <= 0) return i;
324 if (bitsLeft > 0) cache |= (unsigned int)(*buf++) << (24 - cachedBits);
325 if (bitsLeft > 8) cache |= (unsigned int)(*buf++) << (16 - cachedBits);
326 cachedBits += bitsLeft;
327 bitsLeft = 0;
328
329 cache &= (signed int)(int32_t)0x80000000 >> (cachedBits - 1);
330 padBits = 10;
331 cachedBits += padBits; /* okay if this is > 32 (0's automatically shifted in from right) */
332 }
333
334 /* largest maxBits = 6, plus 4 for sign bits, so make sure cache has at least 10 bits */
335 while (i < (nVals - 3) && cachedBits >= 10 ) {
336 cw = tBase[cache >> (32 - maxBits)];
337 len = GetHLenQ(cw);
338 cachedBits -= len;
339 cache <<= len;
340
341 v = GetCWVQ(cw); if(v) {ApplySign(v, cache); cache <<= 1; cachedBits--;}
342 w = GetCWWQ(cw); if(w) {ApplySign(w, cache); cache <<= 1; cachedBits--;}
343 x = GetCWXQ(cw); if(x) {ApplySign(x, cache); cache <<= 1; cachedBits--;}
344 y = GetCWYQ(cw); if(y) {ApplySign(y, cache); cache <<= 1; cachedBits--;}
345
346 /* ran out of bits - okay (means we're done) */
347 if (cachedBits < padBits)
348 return i;
349
350 *vwxy++ = v;
351 *vwxy++ = w;
352 *vwxy++ = x;
353 *vwxy++ = y;
354 i += 4;
355 }
356 }
357
358 /* decoded max number of quad values */
359 return i;
360}
361
362/**************************************************************************************
363 * Function: DecodeHuffman
364 *
365 * Description: decode one granule, one channel worth of Huffman codes
366 *
367 * Inputs: MP3DecInfo structure filled by UnpackFrameHeader(), UnpackSideInfo(),
368 * and UnpackScaleFactors() (for this granule)
369 * buffer pointing to start of Huffman data in MP3 frame
370 * pointer to bit offset (0-7) indicating starting bit in buf[0]
371 * number of bits in the Huffman data section of the frame
372 * (could include padding bits)
373 * index of current granule and channel
374 *
375 * Outputs: decoded coefficients in hi->huffDecBuf[ch] (hi pointer in mp3DecInfo)
376 * updated bitOffset
377 *
378 * Return: length (in bytes) of Huffman codes
379 * bitOffset also returned in parameter (0 = MSB, 7 = LSB of
380 * byte located at buf + offset)
381 * -1 if null input pointers, huffBlockBits < 0, or decoder runs
382 * out of bits prematurely (invalid bitstream)
383 **************************************************************************************/
384// .data about 1ms faster per frame
385int DecodeHuffman(MP3DecInfo *mp3DecInfo, const unsigned char *buf, int *bitOffset, int huffBlockBits, int gr, int ch) FL_NOEXCEPT
386{
387 int r1Start, r2Start, rEnd[4]; /* region boundaries */
388 int i, w, bitsUsed, bitsLeft;
389 const unsigned char *startBuf = buf;
390
391 FrameHeader *fh;
392 SideInfo *si;
393 SideInfoSub *sis;
394 HuffmanInfo *hi;
395
396 /* validate pointers */
397 if (!mp3DecInfo || !mp3DecInfo->FrameHeaderPS || !mp3DecInfo->SideInfoPS || !mp3DecInfo->ScaleFactorInfoPS || !mp3DecInfo->HuffmanInfoPS)
398 return -1;
399
400 fh = ((FrameHeader *)(mp3DecInfo->FrameHeaderPS));
401 si = ((SideInfo *)(mp3DecInfo->SideInfoPS));
402 sis = &si->sis[gr][ch];
403 hi = (HuffmanInfo*)(mp3DecInfo->HuffmanInfoPS);
404
405 if (huffBlockBits < 0)
406 return -1;
407
408 /* figure out region boundaries (the first 2*bigVals coefficients divided into 3 regions) */
409 if (sis->winSwitchFlag && sis->blockType == 2) {
410 if (sis->mixedBlock == 0) {
411 r1Start = fh->sfBand->s[(sis->region0Count + 1)/3] * 3;
412 } else {
413 if (fh->ver == MPEG1) {
414 r1Start = fh->sfBand->l[sis->region0Count + 1];
415 } else {
416 /* see MPEG2 spec for explanation */
417 w = fh->sfBand->s[4] - fh->sfBand->s[3];
418 r1Start = fh->sfBand->l[6] + 2*w;
419 }
420 }
421 r2Start = MAX_NSAMP; /* short blocks don't have region 2 */
422 } else {
423 r1Start = fh->sfBand->l[sis->region0Count + 1];
424 r2Start = fh->sfBand->l[sis->region0Count + 1 + sis->region1Count + 1];
425 }
426
427 /* offset rEnd index by 1 so first region = rEnd[1] - rEnd[0], etc. */
428 rEnd[3] = MIN(MAX_NSAMP, 2 * sis->nBigvals);
429 rEnd[2] = MIN(r2Start, rEnd[3]);
430 rEnd[1] = MIN(r1Start, rEnd[3]);
431 rEnd[0] = 0;
432
433 /* rounds up to first all-zero pair (we don't check last pair for (x,y) == (non-zero, zero)) */
434 hi->nonZeroBound[ch] = rEnd[3];
435
436 /* decode Huffman pairs (rEnd[i] are always even numbers) */
437 bitsLeft = huffBlockBits;
438 for (i = 0; i < 3; i++) {
439 bitsUsed = DecodeHuffmanPairs(hi->huffDecBuf[ch] + rEnd[i], rEnd[i+1] - rEnd[i], sis->tableSelect[i], bitsLeft, buf, *bitOffset);
440 if (bitsUsed < 0 || bitsUsed > bitsLeft) /* error - overran end of bitstream */
441 return -1;
442
443 /* update bitstream position */
444 buf += (bitsUsed + *bitOffset) >> 3;
445 *bitOffset = (bitsUsed + *bitOffset) & 0x07;
446 bitsLeft -= bitsUsed;
447 }
448
449 /* decode Huffman quads (if any) */
450 hi->nonZeroBound[ch] += DecodeHuffmanQuads(hi->huffDecBuf[ch] + rEnd[3], MAX_NSAMP - rEnd[3], sis->count1TableSelect, bitsLeft, buf, *bitOffset);
451
452 ASSERT(hi->nonZeroBound[ch] <= MAX_NSAMP);
453 for (i = hi->nonZeroBound[ch]; i < MAX_NSAMP; i++)
454 hi->huffDecBuf[ch][i] = 0;
455
456 /* If bits used for 576 samples < huffBlockBits, then the extras are considered
457 * to be stuffing bits (throw away, but need to return correct bitstream position)
458 */
459 buf += (bitsLeft + *bitOffset) >> 3;
460 *bitOffset = (bitsLeft + *bitOffset) & 0x07;
461
462 return (buf - startBuf);
463}
464
465} // namespace third_party
466} // namespace fl
unsigned int xy(unsigned int x, unsigned int y)
#define HUFF_PAIRTABS
Definition coder.h:105
#define MIN(a, b)
Definition coder.h:64
#define ASSERT(x)
Definition coder.h:56
#define GetCWY(x)
Definition huffman.hpp:54
#define GetCWX(x)
Definition huffman.hpp:55
#define GetMaxbits(x)
Definition huffman.hpp:52
#define GetCWVQ(x)
Definition huffman.hpp:59
#define GetHLen(x)
Definition huffman.hpp:53
#define GetCWWQ(x)
Definition huffman.hpp:60
#define GetHLenQ(x)
Definition huffman.hpp:58
#define ApplySign(x, s)
Definition huffman.hpp:65
#define GetCWXQ(x)
Definition huffman.hpp:61
#define GetCWYQ(x)
Definition huffman.hpp:62
struct _MP3DecInfo MP3DecInfo
@ MPEG1
Definition mp3dec.h:83
#define MAX_NSAMP
Definition mp3dec.h:79
const int32_t quadTabOffset[2]
Definition hufftabs.hpp:758
enum fl::third_party::_HuffTabType HuffTabType
const int32_t huffTabOffset[HUFF_PAIRTABS]
Definition hufftabs.hpp:668
const unsigned char quadTable[64+16]
Definition hufftabs.hpp:743
struct fl::third_party::_HuffmanInfo HuffmanInfo
static int DecodeHuffmanPairs(int32_t *xy, int nVals, int tabIdx, int bitsLeft, const unsigned char *buf, int bitOffset) FL_NOEXCEPT
Definition huffman.hpp:88
struct fl::third_party::_FrameHeader FrameHeader
static int DecodeHuffmanQuads(int32_t *vwxy, int nVals, int tabIdx, int bitsLeft, const unsigned char *buf, int bitOffset) FL_NOEXCEPT
Definition huffman.hpp:292
struct fl::third_party::_SideInfo SideInfo
int DecodeHuffman(MP3DecInfo *mp3DecInfo, const unsigned char *buf, int *bitOffset, int huffBlockBits, int gr, int ch) FL_NOEXCEPT
Definition huffman.hpp:385
const HuffTabLookup huffTabLookup[HUFF_PAIRTABS]
Definition hufftabs.hpp:703
fl::i32 int32_t
Definition coder.h:220
struct fl::third_party::_SideInfoSub SideInfoSub
const unsigned short huffTable[]
Definition hufftabs.hpp:81
const int32_t quadTabMaxBits[2]
Definition hufftabs.hpp:759
int32_t nonZeroBound[MAX_NCHAN]
Definition coder.h:217
int32_t huffDecBuf[MAX_NCHAN][MAX_NSAMP]
Definition coder.h:216
const SFBandTable * sfBand
Definition coder.h:175
SideInfoSub sis[MAX_NGRAN][MAX_NCHAN]
Definition coder.h:200
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT