Coverage Report

Created: 2025-12-31 08:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/build/frmts/jpeg/libjpeg12/jdhuff12.c
Line
Count
Source
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
63.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.41k
{
91
4.41k
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
92
4.41k
  int ci, blkn, dctbl, actbl;
93
4.41k
  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.41k
  if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
100
1.58k
      cinfo->Ah != 0 || cinfo->Al != 0)
101
3.38k
    WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
102
103
14.9k
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
104
10.5k
    compptr = cinfo->cur_comp_info[ci];
105
10.5k
    dctbl = compptr->dc_tbl_no;
106
10.5k
    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.5k
    if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
110
15
      ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
111
10.5k
    if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
112
15
      ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
113
10.5k
    jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
114
10.5k
          & entropy->dc_derived_tbls[dctbl]);
115
10.5k
    jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
116
10.5k
          & entropy->ac_derived_tbls[actbl]);
117
    /* Initialize DC predictions to 0 */
118
10.5k
    entropy->saved.last_dc_val[ci] = 0;
119
10.5k
  }
120
121
  /* Precalculate decoding info for each block in an MCU of this scan */
122
24.1k
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
123
19.7k
    ci = cinfo->MCU_membership[blkn];
124
19.7k
    compptr = cinfo->cur_comp_info[ci];
125
    /* Precalculate which table to use for each block */
126
19.7k
    entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
127
19.7k
    entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
128
    /* Decide whether we really care about the coefficient values */
129
19.7k
    if (compptr->component_needed) {
130
19.7k
      entropy->dc_needed[blkn] = TRUE;
131
      /* we don't need the ACs if producing a 1/8th-size image */
132
19.7k
      entropy->ac_needed[blkn] = (compptr->DCT_scaled_size > 1);
133
19.7k
    } else {
134
0
      entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
135
0
    }
136
19.7k
  }
137
138
  /* Initialize bitread state variables */
139
4.41k
  entropy->bitstate.bits_left = 0;
140
4.41k
  entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
141
4.41k
  entropy->pub.insufficient_data = FALSE;
142
143
  /* Initialize restart counter */
144
4.41k
  entropy->restarts_to_go = cinfo->restart_interval;
145
4.41k
}
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
51.2k
{
159
51.2k
  JHUFF_TBL *htbl;
160
51.2k
  d_derived_tbl *dtbl;
161
51.2k
  int p, i, l, si, numsymbols;
162
51.2k
  int lookbits, ctr;
163
51.2k
  char huffsize[257];
164
51.2k
  unsigned int huffcode[257];
165
51.2k
  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
51.2k
  if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
173
0
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
174
51.2k
  htbl =
175
51.2k
    isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
176
51.2k
  if (htbl == NULL)
177
46
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
178
179
  /* Allocate a workspace if we haven't already done so. */
180
51.2k
  if (*pdtbl == NULL)
181
3.30k
    *pdtbl = (d_derived_tbl *)
182
3.30k
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
183
3.30k
          SIZEOF(d_derived_tbl));
184
51.2k
  dtbl = *pdtbl;
185
51.2k
  dtbl->pub = htbl;   /* fill in back link */
186
  
187
  /* Figure C.1: make table of Huffman code length for each symbol */
188
189
51.2k
  p = 0;
190
871k
  for (l = 1; l <= 16; l++) {
191
819k
    i = (int) htbl->bits[l];
192
819k
    if (i < 0 || p + i > 256)  /* protect against table overrun */
193
0
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
194
4.51M
    while (i--)
195
3.69M
      huffsize[p++] = (char) l;
196
819k
  }
197
51.2k
  huffsize[p] = 0;
198
51.2k
  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
51.2k
  code = 0;
204
51.2k
  si = huffsize[0];
205
51.2k
  p = 0;
206
560k
  while (huffsize[p]) {
207
4.20M
    while (((int) huffsize[p]) == si) {
208
3.69M
      huffcode[p++] = code;
209
3.69M
      code++;
210
3.69M
    }
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
508k
    if (((INT32) code) >= (((INT32) 1) << si))
215
19
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
216
508k
    code <<= 1;
217
508k
    si++;
218
508k
  }
219
220
  /* Figure F.15: generate decoding tables for bit-sequential decoding */
221
222
51.2k
  p = 0;
223
870k
  for (l = 1; l <= 16; l++) {
224
819k
    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
459k
      dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
229
459k
      p += htbl->bits[l];
230
459k
      dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
231
459k
    } else {
232
360k
      dtbl->maxcode[l] = -1;  /* -1 if no codes of this length */
233
360k
    }
234
819k
  }
235
51.2k
  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
51.2k
  MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
245
246
51.2k
  p = 0;
247
461k
  for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
248
1.11M
    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
702k
      lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
252
13.0M
      for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
253
12.3M
  dtbl->look_nbits[lookbits] = l;
254
12.3M
  dtbl->look_sym[lookbits] = htbl->huffval[p];
255
12.3M
  lookbits++;
256
12.3M
      }
257
702k
    }
258
409k
  }
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
51.2k
  if (isDC) {
267
397k
    for (i = 0; i < numsymbols; i++) {
268
366k
      int sym = htbl->huffval[i];
269
366k
      if (sym < 0 || sym > 15)
270
47
  ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
271
366k
    }
272
31.0k
  }
273
51.2k
}
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
3.71M
#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
1.36M
{
304
  /* Copy heavily used state fields into locals (hopefully registers) */
305
1.36M
  register const JOCTET * next_input_byte = state->next_input_byte;
306
1.36M
  register size_t bytes_in_buffer = state->bytes_in_buffer;
307
1.36M
  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
1.36M
  if (cinfo->unread_marker == 0) { /* cannot advance past a marker */
314
2.10M
    while (bits_left < MIN_GET_BITS) {
315
1.87M
      register int c;
316
317
      /* Attempt to read a byte */
318
1.87M
      if (bytes_in_buffer == 0) {
319
320
988
        cinfo->src->next_input_byte = next_input_byte;
321
988
        cinfo->src->bytes_in_buffer = bytes_in_buffer;
322
        
323
988
        if (! (*cinfo->src->fill_input_buffer) (cinfo))
324
0
    return FALSE;
325
988
  next_input_byte = cinfo->src->next_input_byte;
326
988
  bytes_in_buffer = cinfo->src->bytes_in_buffer;
327
988
      }
328
1.87M
      bytes_in_buffer--;
329
1.87M
      c = GETJOCTET(*next_input_byte++);
330
331
      /* If it's 0xFF, check and discard stuffed zero byte */
332
1.87M
      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
112k
  do {
339
112k
    if (bytes_in_buffer == 0) {
340
107
            cinfo->src->next_input_byte = next_input_byte;
341
107
            cinfo->src->bytes_in_buffer = bytes_in_buffer;
342
107
      if (! (*cinfo->src->fill_input_buffer) (cinfo))
343
0
        return FALSE;
344
107
      next_input_byte = cinfo->src->next_input_byte;
345
107
      bytes_in_buffer = cinfo->src->bytes_in_buffer;
346
107
    }
347
112k
    bytes_in_buffer--;
348
112k
    c = GETJOCTET(*next_input_byte++);
349
112k
  } while (c == 0xFF);
350
351
51.2k
  if (c == 0) {
352
    /* Found FF/00, which represents an FF data byte */
353
6.47k
    c = 0xFF;
354
44.8k
  } 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
44.8k
    cinfo->unread_marker = c;
364
    /* See if we need to insert some fake zero bits. */
365
44.8k
    goto no_more_bytes;
366
44.8k
  }
367
51.2k
      }
368
369
      /* OK, load c into get_buffer */
370
1.82M
      get_buffer = (get_buffer << 8) | c;
371
1.82M
      bits_left += 8;
372
1.82M
    } /* end while */
373
1.07M
  } else {
374
1.12M
  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
1.12M
    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
804k
      if (! cinfo->entropy->insufficient_data) {
386
47.4k
  WARNMS(cinfo, JWRN_HIT_MARKER);
387
47.4k
  cinfo->entropy->insufficient_data = TRUE;
388
47.4k
      }
389
      /* Fill the buffer with zero bits */
390
804k
      get_buffer <<= MIN_GET_BITS - bits_left;
391
804k
      bits_left = MIN_GET_BITS;
392
804k
    }
393
1.12M
  }
394
395
  /* Unload the local registers */
396
1.36M
  state->next_input_byte = next_input_byte;
397
1.36M
  state->bytes_in_buffer = bytes_in_buffer;
398
1.36M
  state->get_buffer = get_buffer;
399
1.36M
  state->bits_left = bits_left;
400
401
1.36M
  return TRUE;
402
1.36M
}
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
335k
{
415
335k
  register int l = min_bits;
416
335k
  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
335k
  CHECK_BIT_BUFFER(*state, l, return -1);
422
335k
  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
996k
  while (code > htbl->maxcode[l]) {
428
660k
    code <<= 1;
429
660k
    CHECK_BIT_BUFFER(*state, 1, return -1);
430
660k
    code |= GET_BITS(1);
431
660k
    l++;
432
660k
  }
433
434
  /* Unload the local registers */
435
335k
  state->get_buffer = get_buffer;
436
335k
  state->bits_left = bits_left;
437
438
  /* With garbage input we may reach the sentinel value l = 17. */
439
440
335k
  if (l > 16) {
441
42.2k
    WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
442
42.2k
    return 0;     /* fake a zero as the safest result */
443
42.2k
  }
444
445
293k
  return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
446
335k
}
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
2.04M
#define NEG_1 ((unsigned int)-1)
455
#define AVOID_TABLES
456
#ifdef AVOID_TABLES
457
458
2.04M
#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
82.5M
{
482
82.5M
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
483
82.5M
  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
82.5M
  cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
488
82.5M
  entropy->bitstate.bits_left = 0;
489
490
  /* Advance past the RSTn marker */
491
82.5M
  if (! (*cinfo->marker->read_restart_marker) (cinfo))
492
0
    return FALSE;
493
494
  /* Re-initialize DC predictions to 0 */
495
215M
  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
496
133M
    entropy->saved.last_dc_val[ci] = 0;
497
498
  /* Reset restart counter */
499
82.5M
  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
82.5M
  if (cinfo->unread_marker == 0)
507
8.46k
    entropy->pub.insufficient_data = FALSE;
508
509
82.5M
  return TRUE;
510
82.5M
}
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
105M
{
531
105M
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
532
105M
  int blkn;
533
105M
  BITREAD_STATE_VARS;
534
105M
  savable_state state;
535
536
  /* Process restart marker if needed; may have to suspend */
537
105M
  if (cinfo->restart_interval) {
538
83.4M
    if (entropy->restarts_to_go == 0)
539
82.5M
      if (! process_restart(cinfo))
540
0
  return FALSE;
541
83.4M
  }
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
105M
  if (! entropy->pub.insufficient_data) {
547
548
    /* Load up working state */
549
31.8k
    BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
550
31.8k
    ASSIGN_STATE(state, entropy->saved);
551
552
    /* Outer loop handles each block in the MCU */
553
554
108k
    for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
555
77.1k
      JBLOCKROW block = MCU_data[blkn];
556
77.1k
      d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
557
77.1k
      d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
558
77.1k
      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
77.1k
      HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
564
77.1k
      if (s) {
565
55.6k
  CHECK_BIT_BUFFER(br_state, s, return FALSE);
566
55.6k
  r = GET_BITS(s);
567
55.6k
  s = HUFF_EXTEND(r, s);
568
55.6k
      }
569
570
77.1k
      if (entropy->dc_needed[blkn]) {
571
  /* Convert DC difference to actual value, update last_dc_val */
572
77.1k
  int ci = cinfo->MCU_membership[blkn];
573
77.1k
  s += state.last_dc_val[ci];
574
77.1k
  state.last_dc_val[ci] = s;
575
  /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
576
77.1k
  (*block)[0] = (JCOEF) s;
577
77.1k
      }
578
579
77.1k
      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
2.13M
  for (k = 1; k < DCTSIZE2; k++) {
584
2.08M
    HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
585
      
586
2.08M
    r = s >> 4;
587
2.08M
    s &= 15;
588
      
589
2.08M
    if (s) {
590
1.99M
      k += r;
591
1.99M
      CHECK_BIT_BUFFER(br_state, s, return FALSE);
592
1.99M
      r = GET_BITS(s);
593
1.99M
      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.99M
      (*block)[jpeg_natural_order[k]] = (JCOEF) s;
599
1.99M
    } else {
600
92.5k
      if (r != 15)
601
29.0k
        break;
602
63.4k
      k += 15;
603
63.4k
    }
604
2.08M
  }
605
606
77.1k
      } 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
77.1k
    }
629
630
    /* Completed MCU, so update state */
631
31.8k
    BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
632
31.8k
    ASSIGN_STATE(entropy->saved, state);
633
31.8k
  }
634
635
  /* Account for restart interval (no-op if not using restarts) */
636
105M
  if( entropy->restarts_to_go == 0 )
637
1.43k
      entropy->restarts_to_go = ~0U;
638
105M
  else
639
105M
      entropy->restarts_to_go--;
640
641
105M
  return TRUE;
642
105M
}
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
488
{
652
488
  huff_entropy_ptr entropy;
653
488
  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
488
  entropy = (huff_entropy_ptr)
664
488
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
665
488
        SIZEOF(huff_entropy_decoder));
666
488
  cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
667
488
  entropy->pub.start_pass = start_pass_huff_decoder;
668
488
  entropy->pub.decode_mcu = decode_mcu;
669
670
  /* Mark tables unallocated */
671
2.44k
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
672
    entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
673
1.95k
  }
674
488
}