FastLED 3.9.15
Loading...
Searching...
No Matches

◆ DecodeHuffmanPairs()

static int fl::third_party::DecodeHuffmanPairs ( int32_t * xy,
int nVals,
int tabIdx,
int bitsLeft,
const unsigned char * buf,
int bitOffset )
static

Definition at line 88 of file huffman.hpp.

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}
unsigned int xy(unsigned int x, unsigned int y)
#define HUFF_PAIRTABS
Definition coder.h:105
#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 GetHLen(x)
Definition huffman.hpp:53
#define ApplySign(x, s)
Definition huffman.hpp:65
enum fl::third_party::_HuffTabType HuffTabType
const int32_t huffTabOffset[HUFF_PAIRTABS]
Definition hufftabs.hpp:668
const HuffTabLookup huffTabLookup[HUFF_PAIRTABS]
Definition hufftabs.hpp:703
fl::i32 int32_t
Definition coder.h:220
const unsigned short huffTable[]
Definition hufftabs.hpp:81

References ApplySign, ASSERT, FL_NOEXCEPT, GetCWX, GetCWY, GetHLen, GetMaxbits, HUFF_PAIRTABS, huffTable, huffTabLookup, huffTabOffset, invalidTab, loopLinbits, loopNoLinbits, noBits, oneShot, fl::x, xy(), and fl::y.

Referenced by DecodeHuffman().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: