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

◆ huffext()

static int fl::third_party::huffext ( JDEC * jd,
unsigned int id,
unsigned int cls )
static

Definition at line 291 of file tjpgd.cpp.hpp.

296{
297 size_t dc = jd->dctr;
298 uint8_t *dp = jd->dptr;
299 unsigned int d, flg = 0;
300
301#if JD_FASTDECODE == 0
302 uint8_t bm, nd, bl;
303 const uint8_t *hb = jd->huffbits[id][cls]; /* Bit distribution table */
304 const uint16_t *hc = jd->huffcode[id][cls]; /* Code word table */
305 const uint8_t *hd = jd->huffdata[id][cls]; /* Data table */
306
307
308 bm = jd->dbit; /* Bit mask to extract */
309 d = 0; bl = 16; /* Max code length */
310 do {
311 if (!bm) { /* Next byte? */
312 if (!dc) { /* No input data is available, re-fill input buffer */
313 dp = jd->inbuf; /* Top of input buffer */
314 dc = jd->infunc(jd, dp, JD_SZBUF);
315 if (!dc) return 0 - (int)JDR_INP; /* Err: read error or wrong stream termination */
316 } else {
317 dp++; /* Next data ptr */
318 }
319 dc--; /* Decrement number of available bytes */
320 if (flg) { /* In flag sequence? */
321 flg = 0; /* Exit flag sequence */
322 if (*dp != 0) return 0 - (int)JDR_FMT1; /* Err: unexpected flag is detected (may be collapted data) */
323 *dp = 0xFF; /* The flag is a data 0xFF */
324 } else {
325 if (*dp == 0xFF) { /* Is start of flag sequence? */
326 flg = 1; continue; /* Enter flag sequence, get trailing byte */
327 }
328 }
329 bm = 0x80; /* Read from MSB */
330 }
331 d <<= 1; /* Get a bit */
332 if (*dp & bm) d++;
333 bm >>= 1;
334
335 for (nd = *hb++; nd; nd--) { /* Search the code word in this bit length */
336 if (d == *hc++) { /* Matched? */
337 jd->dbit = bm; jd->dctr = dc; jd->dptr = dp;
338 return *hd; /* Return the decoded data */
339 }
340 hd++;
341 }
342 bl--;
343 } while (bl);
344
345#else
346 const uint8_t *hb, *hd;
347 const uint16_t *hc;
348 unsigned int nc, bl, wbit = jd->dbit % 32;
349 uint32_t w = jd->wreg & ((1UL << wbit) - 1);
350
351
352 while (wbit < 16) { /* Prepare 16 bits into the working register */
353 if (jd->marker) {
354 d = 0xFF; /* Input stream has stalled for a marker. Generate stuff bits */
355 } else {
356 if (!dc) { /* Buffer empty, re-fill input buffer */
357 dp = jd->inbuf; /* Top of input buffer */
358 dc = jd->infunc(jd, dp, JD_SZBUF);
359 if (!dc) return 0 - (int)JDR_INP; /* Err: read error or wrong stream termination */
360 }
361 d = *dp++; dc--;
362 if (flg) { /* In flag sequence? */
363 flg = 0; /* Exit flag sequence */
364 if (d != 0) jd->marker = d; /* Not an escape of 0xFF but a marker */
365 d = 0xFF;
366 } else {
367 if (d == 0xFF) { /* Is start of flag sequence? */
368 flg = 1; continue; /* Enter flag sequence, get trailing byte */
369 }
370 }
371 }
372 w = w << 8 | d; /* Shift 8 bits in the working register */
373 wbit += 8;
374 }
375 jd->dctr = dc; jd->dptr = dp;
376 jd->wreg = w;
377
378#if JD_FASTDECODE == 2
379 /* Table serch for the short codes */
380 d = (unsigned int)(w >> (wbit - HUFF_BIT)); /* Short code as table index */
381 if (cls) { /* AC element */
382 d = jd->hufflut_ac[id][d]; /* Table decode */
383 if (d != 0xFFFF) { /* It is done if hit in short code */
384 jd->dbit = wbit - (d >> 8); /* Snip the code length */
385 return d & 0xFF; /* b7..0: zero run and following data bits */
386 }
387 } else { /* DC element */
388 d = jd->hufflut_dc[id][d]; /* Table decode */
389 if (d != 0xFF) { /* It is done if hit in short code */
390 jd->dbit = wbit - (d >> 4); /* Snip the code length */
391 return d & 0xF; /* b3..0: following data bits */
392 }
393 }
394
395 /* Incremental serch for the codes longer than HUFF_BIT */
396 hb = jd->huffbits[id][cls] + HUFF_BIT; /* Bit distribution table */
397 hc = jd->huffcode[id][cls] + jd->longofs[id][cls]; /* Code word table */
398 hd = jd->huffdata[id][cls] + jd->longofs[id][cls]; /* Data table */
399 bl = HUFF_BIT + 1;
400#else
401 /* Incremental serch for all codes */
402 hb = jd->huffbits[id][cls]; /* Bit distribution table */
403 hc = jd->huffcode[id][cls]; /* Code word table */
404 hd = jd->huffdata[id][cls]; /* Data table */
405 bl = 1;
406#endif
407 for ( ; bl <= 16; bl++) { /* Incremental search */
408 nc = *hb++;
409 if (nc) {
410 d = w >> (wbit - bl);
411 do { /* Search the code word in this bit length */
412 if (d == *hc++) { /* Matched? */
413 jd->dbit = wbit - bl; /* Snip the huffman code */
414 return *hd; /* Return the decoded data */
415 }
416 hd++;
417 } while (--nc);
418 }
419 }
420#endif
421
422 return 0 - (int)JDR_FMT1; /* Err: code not found (may be collapted data) */
423}
fl::u32 uint32_t
Definition coder.h:219
fl::u16 uint16_t
Definition coder.h:214
unsigned char uint8_t
Definition coder.h:209
uint8_t * dptr
Definition tjpgd.h:52
size_t(* infunc)(JDEC *, uint8_t *, size_t)
Definition tjpgd.h:79
uint8_t * inbuf
Definition tjpgd.h:53
uint16_t * huffcode[2][2]
Definition tjpgd.h:63
uint8_t * huffbits[2][2]
Definition tjpgd.h:62
uint8_t * huffdata[2][2]
Definition tjpgd.h:64
#define JD_SZBUF
Definition tjpgdcnf.h:5

References FL_NOEXCEPT, JD_SZBUF, JDR_FMT1, and JDR_INP.

Referenced by mcu_load().

+ Here is the caller graph for this function: