Coverage Report

Created: 2025-07-23 09:13

/src/gdal/build/frmts/jpeg/libjpeg12/jdhuff12.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jdhuff.c
3
 *
4
 * Copyright (C) 1991-1997, Thomas G. Lane.
5
 * This file is part of the Independent JPEG Group's software.
6
 * For conditions of distribution and use, see the accompanying README file.
7
 *
8
 * This file contains Huffman entropy decoding routines.
9
 *
10
 * Much of the complexity here has to do with supporting input suspension.
11
 * If the data source module demands suspension, we want to be able to back
12
 * up to the start of the current MCU.  To do this, we copy state variables
13
 * into local working storage, and update them back to the permanent
14
 * storage only upon successful completion of an MCU.
15
 */
16
17
#define JPEG_INTERNALS
18
#include "jinclude.h"
19
#include "jpeglib.h"
20
#include "jdhuff.h"   /* Declarations shared with jdphuff.c */
21
22
#if BITS_IN_JSAMPLE == 8
23
#include "jstdhuff.c"
24
#endif
25
26
/*
27
 * Expanded entropy decoder object for Huffman decoding.
28
 *
29
 * The savable_state subrecord contains fields that change within an MCU,
30
 * but must not be updated permanently until we complete the MCU.
31
 */
32
33
typedef struct {
34
  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
35
} savable_state;
36
37
/* This macro is to work around compilers with missing or broken
38
 * structure assignment.  You'll need to fix this code if you have
39
 * such a compiler and you change MAX_COMPS_IN_SCAN.
40
 */
41
42
#ifndef NO_STRUCT_ASSIGN
43
48.6k
#define ASSIGN_STATE(dest,src)  ((dest) = (src))
44
#else
45
#if MAX_COMPS_IN_SCAN == 4
46
#define ASSIGN_STATE(dest,src)  \
47
  ((dest).last_dc_val[0] = (src).last_dc_val[0], \
48
   (dest).last_dc_val[1] = (src).last_dc_val[1], \
49
   (dest).last_dc_val[2] = (src).last_dc_val[2], \
50
   (dest).last_dc_val[3] = (src).last_dc_val[3])
51
#endif
52
#endif
53
54
55
typedef struct {
56
  struct jpeg_entropy_decoder pub; /* public fields */
57
58
  /* These fields are loaded into local variables at start of each MCU.
59
   * In case of suspension, we exit WITHOUT updating them.
60
   */
61
  bitread_perm_state bitstate;  /* Bit buffer at start of MCU */
62
  savable_state saved;    /* Other state at start of MCU */
63
64
  /* These fields are NOT loaded into local working state. */
65
  unsigned int restarts_to_go;  /* MCUs left in this restart interval */
66
67
  /* Pointers to derived tables (these workspaces have image lifespan) */
68
  d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
69
  d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
70
71
  /* Precalculated info set up by start_pass for use in decode_mcu: */
72
73
  /* Pointers to derived tables to be used for each block within an MCU */
74
  d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU];
75
  d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU];
76
  /* Whether we care about the DC and AC coefficient values for each block */
77
  boolean dc_needed[D_MAX_BLOCKS_IN_MCU];
78
  boolean ac_needed[D_MAX_BLOCKS_IN_MCU];
79
} huff_entropy_decoder;
80
81
typedef huff_entropy_decoder * huff_entropy_ptr;
82
83
84
/*
85
 * Initialize for a Huffman-compressed scan.
86
 */
87
88
METHODDEF(void)
89
start_pass_huff_decoder (j_decompress_ptr cinfo)
90
4.25k
{
91
4.25k
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
92
4.25k
  int ci, blkn, dctbl, actbl;
93
4.25k
  jpeg_component_info * compptr;
94
95
  /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
96
   * This ought to be an error condition, but we make it a warning because
97
   * there are some baseline files out there with all zeroes in these bytes.
98
   */
99
4.25k
  if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
100
4.25k
      cinfo->Ah != 0 || cinfo->Al != 0)
101
3.30k
    WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
102
103
15.0k
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
104
10.7k
    compptr = cinfo->cur_comp_info[ci];
105
10.7k
    dctbl = compptr->dc_tbl_no;
106
10.7k
    actbl = compptr->ac_tbl_no;
107
    /* Compute derived values for Huffman tables */
108
    /* We may do this more than once for a table, but it's not expensive */
109
10.7k
    if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
110
12
      ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
111
10.7k
    if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
112
18
      ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
113
10.7k
    jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
114
10.7k
          & entropy->dc_derived_tbls[dctbl]);
115
10.7k
    jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
116
10.7k
          & entropy->ac_derived_tbls[actbl]);
117
    /* Initialize DC predictions to 0 */
118
10.7k
    entropy->saved.last_dc_val[ci] = 0;
119
10.7k
  }
120
121
  /* Precalculate decoding info for each block in an MCU of this scan */
122
24.8k
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
123
20.6k
    ci = cinfo->MCU_membership[blkn];
124
20.6k
    compptr = cinfo->cur_comp_info[ci];
125
    /* Precalculate which table to use for each block */
126
20.6k
    entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
127
20.6k
    entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
128
    /* Decide whether we really care about the coefficient values */
129
20.6k
    if (compptr->component_needed) {
130
20.6k
      entropy->dc_needed[blkn] = TRUE;
131
      /* we don't need the ACs if producing a 1/8th-size image */
132
20.6k
      entropy->ac_needed[blkn] = (compptr->DCT_scaled_size > 1);
133
20.6k
    } else {
134
0
      entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
135
0
    }
136
20.6k
  }
137
138
  /* Initialize bitread state variables */
139
4.25k
  entropy->bitstate.bits_left = 0;
140
4.25k
  entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
141
4.25k
  entropy->pub.insufficient_data = FALSE;
142
143
  /* Initialize restart counter */
144
4.25k
  entropy->restarts_to_go = cinfo->restart_interval;
145
4.25k
}
146
147
148
/*
149
 * Compute the derived values for a Huffman table.
150
 * This routine also performs some validation checks on the table.
151
 *
152
 * Note this is also used by jdphuff.c.
153
 */
154
155
GLOBAL(void)
156
jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
157
       d_derived_tbl ** pdtbl)
158
48.1k
{
159
48.1k
  JHUFF_TBL *htbl;
160
48.1k
  d_derived_tbl *dtbl;
161
48.1k
  int p, i, l, si, numsymbols;
162
48.1k
  int lookbits, ctr;
163
48.1k
  char huffsize[257];
164
48.1k
  unsigned int huffcode[257];
165
48.1k
  unsigned int code;
166
167
  /* Note that huffsize[] and huffcode[] are filled in code-length order,
168
   * paralleling the order of the symbols themselves in htbl->huffval[].
169
   */
170
171
  /* Find the input Huffman table */
172
48.1k
  if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
173
0
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
174
48.1k
  htbl =
175
48.1k
    isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
176
48.1k
  if (htbl == NULL)
177
58
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
178
179
  /* Allocate a workspace if we haven't already done so. */
180
48.1k
  if (*pdtbl == NULL)
181
3.21k
    *pdtbl = (d_derived_tbl *)
182
3.21k
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
183
3.21k
          SIZEOF(d_derived_tbl));
184
48.1k
  dtbl = *pdtbl;
185
48.1k
  dtbl->pub = htbl;   /* fill in back link */
186
  
187
  /* Figure C.1: make table of Huffman code length for each symbol */
188
189
48.1k
  p = 0;
190
817k
  for (l = 1; l <= 16; l++) {
191
769k
    i = (int) htbl->bits[l];
192
769k
    if (i < 0 || p + i > 256)  /* protect against table overrun */
193
0
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
194
4.32M
    while (i--)
195
3.55M
      huffsize[p++] = (char) l;
196
769k
  }
197
48.1k
  huffsize[p] = 0;
198
48.1k
  numsymbols = p;
199
  
200
  /* Figure C.2: generate the codes themselves */
201
  /* We also validate that the counts represent a legal Huffman code tree. */
202
  
203
48.1k
  code = 0;
204
48.1k
  si = huffsize[0];
205
48.1k
  p = 0;
206
522k
  while (huffsize[p]) {
207
4.03M
    while (((int) huffsize[p]) == si) {
208
3.55M
      huffcode[p++] = code;
209
3.55M
      code++;
210
3.55M
    }
211
    /* code is now 1 more than the last code used for codelength si; but
212
     * it must still fit in si bits, since no code is allowed to be all ones.
213
     */
214
473k
    if (((INT32) code) >= (((INT32) 1) << si))
215
17
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
216
473k
    code <<= 1;
217
473k
    si++;
218
473k
  }
219
220
  /* Figure F.15: generate decoding tables for bit-sequential decoding */
221
222
48.1k
  p = 0;
223
817k
  for (l = 1; l <= 16; l++) {
224
769k
    if (htbl->bits[l]) {
225
      /* valoffset[l] = huffval[] index of 1st symbol of code length l,
226
       * minus the minimum code of length l
227
       */
228
428k
      dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
229
428k
      p += htbl->bits[l];
230
428k
      dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
231
428k
    } else {
232
341k
      dtbl->maxcode[l] = -1;  /* -1 if no codes of this length */
233
341k
    }
234
769k
  }
235
48.1k
  dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
236
237
  /* Compute lookahead tables to speed up decoding.
238
   * First we set all the table entries to 0, indicating "too long";
239
   * then we iterate through the Huffman codes that are short enough and
240
   * fill in all the entries that correspond to bit sequences starting
241
   * with that code.
242
   */
243
244
48.1k
  MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
245
246
48.1k
  p = 0;
247
432k
  for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
248
1.03M
    for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
249
      /* l = current code's length, p = its index in huffcode[] & huffval[]. */
250
      /* Generate left-justified code followed by all possible bit sequences */
251
652k
      lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
252
12.0M
      for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
253
11.3M
  dtbl->look_nbits[lookbits] = l;
254
11.3M
  dtbl->look_sym[lookbits] = htbl->huffval[p];
255
11.3M
  lookbits++;
256
11.3M
      }
257
652k
    }
258
384k
  }
259
260
  /* Validate symbols as being reasonable.
261
   * For AC tables, we make no check, but accept all byte values 0..255.
262
   * For DC tables, we require the symbols to be in range 0..15.
263
   * (Tighter bounds could be applied depending on the data depth and mode,
264
   * but this is sufficient to ensure safe decoding.)
265
   */
266
48.1k
  if (isDC) {
267
369k
    for (i = 0; i < numsymbols; i++) {
268
340k
      int sym = htbl->huffval[i];
269
340k
      if (sym < 0 || sym > 15)
270
41
  ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
271
340k
    }
272
29.1k
  }
273
48.1k
}
274
275
276
/*
277
 * Out-of-line code for bit fetching (shared with jdphuff.c).
278
 * See jdhuff.h for info about usage.
279
 * Note: current values of get_buffer and bits_left are passed as parameters,
280
 * but are returned in the corresponding fields of the state struct.
281
 *
282
 * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
283
 * of get_buffer to be used.  (On machines with wider words, an even larger
284
 * buffer could be used.)  However, on some machines 32-bit shifts are
285
 * quite slow and take time proportional to the number of places shifted.
286
 * (This is true with most PC compilers, for instance.)  In this case it may
287
 * be a win to set MIN_GET_BITS to the minimum value of 15.  This reduces the
288
 * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
289
 */
290
291
#ifdef SLOW_SHIFT_32
292
#define MIN_GET_BITS  15  /* minimum allowable value */
293
#else
294
2.60M
#define MIN_GET_BITS  (BIT_BUF_SIZE-7)
295
#endif
296
297
298
GLOBAL(boolean)
299
jpeg_fill_bit_buffer (bitread_working_state * state,
300
          register bit_buf_type get_buffer, register int bits_left,
301
          int nbits)
302
/* Load up the bit buffer to a depth of at least nbits */
303
937k
{
304
  /* Copy heavily used state fields into locals (hopefully registers) */
305
937k
  register const JOCTET * next_input_byte = state->next_input_byte;
306
937k
  register size_t bytes_in_buffer = state->bytes_in_buffer;
307
937k
  j_decompress_ptr cinfo = state->cinfo;
308
309
  /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
310
  /* (It is assumed that no request will be for more than that many bits.) */
311
  /* We fail to do so only if we hit a marker or are forced to suspend. */
312
313
937k
  if (cinfo->unread_marker == 0) { /* cannot advance past a marker */
314
1.67M
    while (bits_left < MIN_GET_BITS) {
315
1.49M
      register int c;
316
317
      /* Attempt to read a byte */
318
1.49M
      if (bytes_in_buffer == 0) {
319
320
959
        cinfo->src->next_input_byte = next_input_byte;
321
959
        cinfo->src->bytes_in_buffer = bytes_in_buffer;
322
        
323
959
        if (! (*cinfo->src->fill_input_buffer) (cinfo))
324
0
    return FALSE;
325
959
  next_input_byte = cinfo->src->next_input_byte;
326
959
  bytes_in_buffer = cinfo->src->bytes_in_buffer;
327
959
      }
328
1.49M
      bytes_in_buffer--;
329
1.49M
      c = GETJOCTET(*next_input_byte++);
330
331
      /* If it's 0xFF, check and discard stuffed zero byte */
332
1.49M
      if (c == 0xFF) {
333
  /* Loop here to discard any padding FF's on terminating marker,
334
   * so that we can save a valid unread_marker value.  NOTE: we will
335
   * accept multiple FF's followed by a 0 as meaning a single FF data
336
   * byte.  This data pattern is not valid according to the standard.
337
   */
338
115k
  do {
339
115k
    if (bytes_in_buffer == 0) {
340
87
            cinfo->src->next_input_byte = next_input_byte;
341
87
            cinfo->src->bytes_in_buffer = bytes_in_buffer;
342
87
      if (! (*cinfo->src->fill_input_buffer) (cinfo))
343
0
        return FALSE;
344
87
      next_input_byte = cinfo->src->next_input_byte;
345
87
      bytes_in_buffer = cinfo->src->bytes_in_buffer;
346
87
    }
347
115k
    bytes_in_buffer--;
348
115k
    c = GETJOCTET(*next_input_byte++);
349
115k
  } while (c == 0xFF);
350
351
52.0k
  if (c == 0) {
352
    /* Found FF/00, which represents an FF data byte */
353
6.51k
    c = 0xFF;
354
45.5k
  } else {
355
    /* Oops, it's actually a marker indicating end of compressed data.
356
     * Save the marker code for later use.
357
     * Fine point: it might appear that we should save the marker into
358
     * bitread working state, not straight into permanent state.  But
359
     * once we have hit a marker, we cannot need to suspend within the
360
     * current MCU, because we will read no more bytes from the data
361
     * source.  So it is OK to update permanent state right away.
362
     */
363
45.5k
    cinfo->unread_marker = c;
364
    /* See if we need to insert some fake zero bits. */
365
45.5k
    goto no_more_bytes;
366
45.5k
  }
367
52.0k
      }
368
369
      /* OK, load c into get_buffer */
370
1.44M
      get_buffer = (get_buffer << 8) | c;
371
1.44M
      bits_left += 8;
372
1.44M
    } /* end while */
373
707k
  } else {
374
752k
  no_more_bytes:
375
    /* We get here if we've read the marker that terminates the compressed
376
     * data segment.  There should be enough bits in the buffer register
377
     * to satisfy the request; if so, no problem.
378
     */
379
752k
    if (nbits > bits_left) {
380
      /* Uh-oh.  Report corrupted data to user and stuff zeroes into
381
       * the data stream, so that we can produce some kind of image.
382
       * We use a nonvolatile flag to ensure that only one warning message
383
       * appears per data segment.
384
       */
385
464k
      if (! cinfo->entropy->insufficient_data) {
386
47.2k
  WARNMS(cinfo, JWRN_HIT_MARKER);
387
47.2k
  cinfo->entropy->insufficient_data = TRUE;
388
47.2k
      }
389
      /* Fill the buffer with zero bits */
390
464k
      get_buffer <<= MIN_GET_BITS - bits_left;
391
464k
      bits_left = MIN_GET_BITS;
392
464k
    }
393
752k
  }
394
395
  /* Unload the local registers */
396
937k
  state->next_input_byte = next_input_byte;
397
937k
  state->bytes_in_buffer = bytes_in_buffer;
398
937k
  state->get_buffer = get_buffer;
399
937k
  state->bits_left = bits_left;
400
401
937k
  return TRUE;
402
937k
}
403
404
405
/*
406
 * Out-of-line code for Huffman code decoding.
407
 * See jdhuff.h for info about usage.
408
 */
409
410
GLOBAL(int)
411
jpeg_huff_decode (bitread_working_state * state,
412
      register bit_buf_type get_buffer, register int bits_left,
413
      d_derived_tbl * htbl, int min_bits)
414
317k
{
415
317k
  register int l = min_bits;
416
317k
  register INT32 code;
417
418
  /* HUFF_DECODE has determined that the code is at least min_bits */
419
  /* bits long, so fetch that many bits in one swoop. */
420
421
317k
  CHECK_BIT_BUFFER(*state, l, return -1);
422
317k
  code = GET_BITS(l);
423
424
  /* Collect the rest of the Huffman code one bit at a time. */
425
  /* This is per Figure F.16 in the JPEG spec. */
426
427
1.03M
  while (code > htbl->maxcode[l]) {
428
722k
    code <<= 1;
429
722k
    CHECK_BIT_BUFFER(*state, 1, return -1);
430
722k
    code |= GET_BITS(1);
431
722k
    l++;
432
722k
  }
433
434
  /* Unload the local registers */
435
317k
  state->get_buffer = get_buffer;
436
317k
  state->bits_left = bits_left;
437
438
  /* With garbage input we may reach the sentinel value l = 17. */
439
440
317k
  if (l > 16) {
441
53.6k
    WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
442
53.6k
    return 0;     /* fake a zero as the safest result */
443
53.6k
  }
444
445
263k
  return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
446
317k
}
447
448
449
/*
450
 * Figure F.12: extend sign bit.
451
 * On some machines, a shift and add will be faster than a table lookup.
452
 */
453
454
1.84M
#define NEG_1 ((unsigned int)-1)
455
#define AVOID_TABLES
456
#ifdef AVOID_TABLES
457
458
1.84M
#define HUFF_EXTEND(x,s)  ((x) + ((((x) - (1<<((s)-1))) >> 31) & (((NEG_1)<<(s)) + 1)))
459
460
#else
461
462
#define HUFF_EXTEND(x,s)  ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
463
464
static const int extend_test[16] =   /* entry n is 2**(n-1) */
465
  { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
466
    0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
467
468
static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
469
  { 0, -1, -3, -7, -15, -31, -63, -127, -255, -511, -1023, -2047, -4095, -8191, -16383, -32767 };
470
471
#endif /* AVOID_TABLES */
472
473
474
/*
475
 * Check for a restart marker & resynchronize decoder.
476
 * Returns FALSE if must suspend.
477
 */
478
479
LOCAL(boolean)
480
process_restart (j_decompress_ptr cinfo)
481
60.9M
{
482
60.9M
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
483
60.9M
  int ci;
484
485
  /* Throw away any unused bits remaining in bit buffer; */
486
  /* include any full bytes in next_marker's count of discarded bytes */
487
60.9M
  cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
488
60.9M
  entropy->bitstate.bits_left = 0;
489
490
  /* Advance past the RSTn marker */
491
60.9M
  if (! (*cinfo->marker->read_restart_marker) (cinfo))
492
0
    return FALSE;
493
494
  /* Re-initialize DC predictions to 0 */
495
170M
  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
496
109M
    entropy->saved.last_dc_val[ci] = 0;
497
498
  /* Reset restart counter */
499
60.9M
  entropy->restarts_to_go = cinfo->restart_interval;
500
501
  /* Reset out-of-data flag, unless read_restart_marker left us smack up
502
   * against a marker.  In that case we will end up treating the next data
503
   * segment as empty, and we can avoid producing bogus output pixels by
504
   * leaving the flag set.
505
   */
506
60.9M
  if (cinfo->unread_marker == 0)
507
7.47k
    entropy->pub.insufficient_data = FALSE;
508
509
60.9M
  return TRUE;
510
60.9M
}
511
512
513
/*
514
 * Decode and return one MCU's worth of Huffman-compressed coefficients.
515
 * The coefficients are reordered from zigzag order into natural array order,
516
 * but are not dequantized.
517
 *
518
 * The i'th block of the MCU is stored into the block pointed to by
519
 * MCU_data[i].  WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
520
 * (Wholesale zeroing is usually a little faster than retail...)
521
 *
522
 * Returns FALSE if data source requested suspension.  In that case no
523
 * changes have been made to permanent state.  (Exception: some output
524
 * coefficients may already have been assigned.  This is harmless for
525
 * this module, since we'll just re-assign them on the next call.)
526
 */
527
528
METHODDEF(boolean)
529
decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
530
87.9M
{
531
87.9M
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
532
87.9M
  int blkn;
533
87.9M
  BITREAD_STATE_VARS;
534
87.9M
  savable_state state;
535
536
  /* Process restart marker if needed; may have to suspend */
537
87.9M
  if (cinfo->restart_interval) {
538
60.9M
    if (entropy->restarts_to_go == 0)
539
60.9M
      if (! process_restart(cinfo))
540
0
  return FALSE;
541
60.9M
  }
542
543
  /* If we've run out of data, just leave the MCU set to zeroes.
544
   * This way, we return uniform gray for the remainder of the segment.
545
   */
546
87.9M
  if (! entropy->pub.insufficient_data) {
547
548
    /* Load up working state */
549
24.3k
    BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
550
24.3k
    ASSIGN_STATE(state, entropy->saved);
551
552
    /* Outer loop handles each block in the MCU */
553
554
90.7k
    for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
555
66.4k
      JBLOCKROW block = MCU_data[blkn];
556
66.4k
      d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
557
66.4k
      d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
558
66.4k
      register int s, k, r;
559
560
      /* Decode a single block's worth of coefficients */
561
562
      /* Section F.2.2.1: decode the DC coefficient difference */
563
66.4k
      HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
564
66.4k
      if (s) {
565
47.7k
  CHECK_BIT_BUFFER(br_state, s, return FALSE);
566
47.7k
  r = GET_BITS(s);
567
47.7k
  s = HUFF_EXTEND(r, s);
568
47.7k
      }
569
570
66.4k
      if (entropy->dc_needed[blkn]) {
571
  /* Convert DC difference to actual value, update last_dc_val */
572
66.4k
  int ci = cinfo->MCU_membership[blkn];
573
66.4k
  s += state.last_dc_val[ci];
574
66.4k
  state.last_dc_val[ci] = s;
575
  /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
576
66.4k
  (*block)[0] = (JCOEF) s;
577
66.4k
      }
578
579
66.4k
      if (entropy->ac_needed[blkn]) {
580
581
  /* Section F.2.2.2: decode the AC coefficients */
582
  /* Since zeroes are skipped, output area must be cleared beforehand */
583
1.90M
  for (k = 1; k < DCTSIZE2; k++) {
584
1.86M
    HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
585
      
586
1.86M
    r = s >> 4;
587
1.86M
    s &= 15;
588
      
589
1.86M
    if (s) {
590
1.80M
      k += r;
591
1.80M
      CHECK_BIT_BUFFER(br_state, s, return FALSE);
592
1.80M
      r = GET_BITS(s);
593
1.80M
      s = HUFF_EXTEND(r, s);
594
      /* Output coefficient in natural (dezigzagged) order.
595
       * Note: the extra entries in jpeg_natural_order[] will save us
596
       * if k >= DCTSIZE2, which could happen if the data is corrupted.
597
       */
598
1.80M
      (*block)[jpeg_natural_order[k]] = (JCOEF) s;
599
1.80M
    } else {
600
66.6k
      if (r != 15)
601
29.7k
        break;
602
36.8k
      k += 15;
603
36.8k
    }
604
1.86M
  }
605
606
66.4k
      } else {
607
608
  /* Section F.2.2.2: decode the AC coefficients */
609
  /* In this path we just discard the values */
610
0
  for (k = 1; k < DCTSIZE2; k++) {
611
0
    HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
612
      
613
0
    r = s >> 4;
614
0
    s &= 15;
615
      
616
0
    if (s) {
617
0
      k += r;
618
0
      CHECK_BIT_BUFFER(br_state, s, return FALSE);
619
0
      DROP_BITS(s);
620
0
    } else {
621
0
      if (r != 15)
622
0
        break;
623
0
      k += 15;
624
0
    }
625
0
  }
626
627
0
      }
628
66.4k
    }
629
630
    /* Completed MCU, so update state */
631
24.3k
    BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
632
24.3k
    ASSIGN_STATE(entropy->saved, state);
633
24.3k
  }
634
635
  /* Account for restart interval (no-op if not using restarts) */
636
87.9M
  if( entropy->restarts_to_go == 0 )
637
1.75k
      entropy->restarts_to_go = ~0U;
638
87.9M
  else
639
87.9M
      entropy->restarts_to_go--;
640
641
87.9M
  return TRUE;
642
87.9M
}
643
644
645
/*
646
 * Module initialization routine for Huffman entropy decoding.
647
 */
648
649
GLOBAL(void)
650
jinit_huff_decoder (j_decompress_ptr cinfo)
651
522
{
652
522
  huff_entropy_ptr entropy;
653
522
  int i;
654
655
#if BITS_IN_JSAMPLE == 8
656
  /* Motion JPEG frames typically do not include the Huffman tables if they
657
     are the default tables.  Thus, if the tables are not set by the time
658
     the Huffman decoder is initialized (usually within the body of
659
     jpeg_start_decompress()), we set them to default values. */
660
  std_huff_tables((j_common_ptr) cinfo);
661
#endif
662
663
522
  entropy = (huff_entropy_ptr)
664
522
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
665
522
        SIZEOF(huff_entropy_decoder));
666
522
  cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
667
522
  entropy->pub.start_pass = start_pass_huff_decoder;
668
522
  entropy->pub.decode_mcu = decode_mcu;
669
670
  /* Mark tables unallocated */
671
2.61k
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
672
2.08k
    entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
673
2.08k
  }
674
522
}