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

◆ create_huffman_tbl()

static JRESULT fl::third_party::create_huffman_tbl ( JDEC * jd,
const uint8_t * data,
size_t ndata )
static

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

208{
209 unsigned int i, j, b, cls, num;
210 size_t np;
211 uint8_t d, *pb, *pd;
212 uint16_t hc, *ph;
213
214
215 while (ndata) { /* Process all tables in the segment */
216 if (ndata < 17) return JDR_FMT1; /* Err: wrong data size */
217 ndata -= 17;
218 d = *data++; /* Get table number and class */
219 if (d & 0xEE) return JDR_FMT1; /* Err: invalid class/number */
220 cls = d >> 4; num = d & 0x0F; /* class = dc(0)/ac(1), table number = 0/1 */
221 pb = (uint8_t*)alloc_pool(jd, 16); /* Allocate a memory block for the bit distribution table */
222 if (!pb) return JDR_MEM1; /* Err: not enough memory */
223 jd->huffbits[num][cls] = pb;
224 for (np = i = 0; i < 16; i++) { /* Load number of patterns for 1 to 16-bit code */
225 np += (pb[i] = *data++); /* Get sum of code words for each code */
226 }
227 ph = (uint16_t*)alloc_pool(jd, np * sizeof (uint16_t));/* Allocate a memory block for the code word table */
228 if (!ph) return JDR_MEM1; /* Err: not enough memory */
229 jd->huffcode[num][cls] = ph;
230 hc = 0;
231 for (j = i = 0; i < 16; i++) { /* Re-build huffman code word table */
232 b = pb[i];
233 while (b--) ph[j++] = hc++;
234 hc <<= 1;
235 }
236
237 if (ndata < np) return JDR_FMT1; /* Err: wrong data size */
238 ndata -= np;
239 pd = (uint8_t*)alloc_pool(jd, np); /* Allocate a memory block for the decoded data */
240 if (!pd) return JDR_MEM1; /* Err: not enough memory */
241 jd->huffdata[num][cls] = pd;
242 for (i = 0; i < np; i++) { /* Load decoded data corresponds to each code word */
243 d = *data++;
244 if (!cls && d > 11) return JDR_FMT1;
245 pd[i] = d;
246 }
247#if JD_FASTDECODE == 2
248 { /* Create fast huffman decode table */
249 unsigned int span, td, ti;
250 uint16_t *tbl_ac = 0;
251 uint8_t *tbl_dc = 0;
252
253 if (cls) {
254 tbl_ac = (uint16_t*)alloc_pool(jd, HUFF_LEN * sizeof (uint16_t)); /* LUT for AC elements */
255 if (!tbl_ac) return JDR_MEM1; /* Err: not enough memory */
256 jd->hufflut_ac[num] = tbl_ac;
257 fl::memset(tbl_ac, 0xFF, HUFF_LEN * sizeof (uint16_t)); /* Default value (0xFFFF: may be long code) */
258 } else {
259 tbl_dc = (uint8_t*)alloc_pool(jd, HUFF_LEN * sizeof (uint8_t)); /* LUT for AC elements */
260 if (!tbl_dc) return JDR_MEM1; /* Err: not enough memory */
261 jd->hufflut_dc[num] = tbl_dc;
262 fl::memset(tbl_dc, 0xFF, HUFF_LEN * sizeof (uint8_t)); /* Default value (0xFF: may be long code) */
263 }
264 for (i = b = 0; b < HUFF_BIT; b++) { /* Create LUT */
265 for (j = pb[b]; j; j--) {
266 ti = ph[i] << (HUFF_BIT - 1 - b) & HUFF_MASK; /* Index of input pattern for the code */
267 if (cls) {
268 td = pd[i++] | ((b + 1) << 8); /* b15..b8: code length, b7..b0: zero run and data length */
269 for (span = 1 << (HUFF_BIT - 1 - b); span; span--, tbl_ac[ti++] = (uint16_t)td) ;
270 } else {
271 td = pd[i++] | ((b + 1) << 4); /* b7..b4: code length, b3..b0: data length */
272 for (span = 1 << (HUFF_BIT - 1 - b); span; span--, tbl_dc[ti++] = (uint8_t)td) ;
273 }
274 }
275 }
276 jd->longofs[num][cls] = i; /* Code table offset for long code */
277 }
278#endif
279 }
280
281 return JDR_OK;
282}
fl::u16 uint16_t
Definition coder.h:214
static void * alloc_pool(JDEC *jd, size_t ndata) FL_NOEXCEPT
unsigned char uint8_t
Definition coder.h:209
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
fl::u16 uint16_t
Definition s16x16x4.h:214
void * memset(void *s, int c, size_t n) FL_NOEXCEPT

References alloc_pool(), FL_NOEXCEPT, JDR_FMT1, JDR_MEM1, JDR_OK, and fl::memset().

Referenced by jd_prepare().

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