Coverage Report

Created: 2026-09-26 08:22

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
59.4k
#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
3.75k
{
91
3.75k
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
92
3.75k
  int ci, blkn, dctbl, actbl;
93
3.75k
  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
3.75k
  if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
100
1.51k
      cinfo->Ah != 0 || cinfo->Al != 0)
101
3.01k
    WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
102
103
13.1k
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
104
9.38k
    compptr = cinfo->cur_comp_info[ci];
105
9.38k
    dctbl = compptr->dc_tbl_no;
106
9.38k
    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
9.38k
    if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
110
18
      ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
111
9.38k
    if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
112
15
      ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
113
9.38k
    jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
114
9.38k
          & entropy->dc_derived_tbls[dctbl]);
115
9.38k
    jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
116
9.38k
          & entropy->ac_derived_tbls[actbl]);
117
    /* Initialize DC predictions to 0 */
118
9.38k
    entropy->saved.last_dc_val[ci] = 0;
119
9.38k
  }
120
121
  /* Precalculate decoding info for each block in an MCU of this scan */
122
21.5k
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
123
17.7k
    ci = cinfo->MCU_membership[blkn];
124
17.7k
    compptr = cinfo->cur_comp_info[ci];
125
    /* Precalculate which table to use for each block */
126
17.7k
    entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
127
17.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
17.7k
    if (compptr->component_needed) {
130
17.7k
      entropy->dc_needed[blkn] = TRUE;
131
      /* we don't need the ACs if producing a 1/8th-size image */
132
17.7k
      entropy->ac_needed[blkn] = (compptr->DCT_scaled_size > 1);
133
17.7k
    } else {
134
0
      entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
135
0
    }
136
17.7k
  }
137
138
  /* Initialize bitread state variables */
139
3.75k
  entropy->bitstate.bits_left = 0;
140
3.75k
  entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
141
3.75k
  entropy->pub.insufficient_data = FALSE;
142
143
  /* Initialize restart counter */
144
3.75k
  entropy->restarts_to_go = cinfo->restart_interval;
145
3.75k
}
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.0k
{
159
51.0k
  JHUFF_TBL *htbl;
160
51.0k
  d_derived_tbl *dtbl;
161
51.0k
  int p, i, l, si, numsymbols;
162
51.0k
  int lookbits, ctr;
163
51.0k
  char huffsize[257];
164
51.0k
  unsigned int huffcode[257];
165
51.0k
  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.0k
  if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
173
0
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
174
51.0k
  htbl =
175
51.0k
    isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
176
51.0k
  if (htbl == NULL)
177
54
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
178
179
  /* Allocate a workspace if we haven't already done so. */
180
51.0k
  if (*pdtbl == NULL)
181
3.65k
    *pdtbl = (d_derived_tbl *)
182
3.65k
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
183
3.65k
          SIZEOF(d_derived_tbl));
184
51.0k
  dtbl = *pdtbl;
185
51.0k
  dtbl->pub = htbl;   /* fill in back link */
186
  
187
  /* Figure C.1: make table of Huffman code length for each symbol */
188
189
51.0k
  p = 0;
190
867k
  for (l = 1; l <= 16; l++) {
191
816k
    i = (int) htbl->bits[l];
192
816k
    if (i < 0 || p + i > 256)  /* protect against table overrun */
193
0
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
194
4.04M
    while (i--)
195
3.22M
      huffsize[p++] = (char) l;
196
816k
  }
197
51.0k
  huffsize[p] = 0;
198
51.0k
  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.0k
  code = 0;
204
51.0k
  si = huffsize[0];
205
51.0k
  p = 0;
206
541k
  while (huffsize[p]) {
207
3.71M
    while (((int) huffsize[p]) == si) {
208
3.22M
      huffcode[p++] = code;
209
3.22M
      code++;
210
3.22M
    }
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
490k
    if (((INT32) code) >= (((INT32) 1) << si))
215
24
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
216
490k
    code <<= 1;
217
490k
    si++;
218
490k
  }
219
220
  /* Figure F.15: generate decoding tables for bit-sequential decoding */
221
222
51.0k
  p = 0;
223
867k
  for (l = 1; l <= 16; l++) {
224
815k
    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
445k
      dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
229
445k
      p += htbl->bits[l];
230
445k
      dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
231
445k
    } else {
232
370k
      dtbl->maxcode[l] = -1;  /* -1 if no codes of this length */
233
370k
    }
234
815k
  }
235
51.0k
  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.0k
  MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
245
246
51.0k
  p = 0;
247
459k
  for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
248
1.08M
    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
681k
      lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
252
12.9M
      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
681k
    }
258
407k
  }
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.0k
  if (isDC) {
267
420k
    for (i = 0; i < numsymbols; i++) {
268
387k
      int sym = htbl->huffval[i];
269
387k
      if (sym < 0 || sym > 15)
270
40
  ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
271
387k
    }
272
32.9k
  }
273
51.0k
}
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.95M
#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
932k
{
304
  /* Copy heavily used state fields into locals (hopefully registers) */
305
932k
  register const JOCTET * next_input_byte = state->next_input_byte;
306
932k
  register size_t bytes_in_buffer = state->bytes_in_buffer;
307
932k
  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
932k
  if (cinfo->unread_marker == 0) { /* cannot advance past a marker */
314
2.01M
    while (bits_left < MIN_GET_BITS) {
315
1.78M
      register int c;
316
317
      /* Attempt to read a byte */
318
1.78M
      if (bytes_in_buffer == 0) {
319
320
1.22k
        cinfo->src->next_input_byte = next_input_byte;
321
1.22k
        cinfo->src->bytes_in_buffer = bytes_in_buffer;
322
        
323
1.22k
        if (! (*cinfo->src->fill_input_buffer) (cinfo))
324
0
    return FALSE;
325
1.22k
  next_input_byte = cinfo->src->next_input_byte;
326
1.22k
  bytes_in_buffer = cinfo->src->bytes_in_buffer;
327
1.22k
      }
328
1.78M
      bytes_in_buffer--;
329
1.78M
      c = GETJOCTET(*next_input_byte++);
330
331
      /* If it's 0xFF, check and discard stuffed zero byte */
332
1.78M
      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
98.0k
  do {
339
98.0k
    if (bytes_in_buffer == 0) {
340
140
            cinfo->src->next_input_byte = next_input_byte;
341
140
            cinfo->src->bytes_in_buffer = bytes_in_buffer;
342
140
      if (! (*cinfo->src->fill_input_buffer) (cinfo))
343
0
        return FALSE;
344
140
      next_input_byte = cinfo->src->next_input_byte;
345
140
      bytes_in_buffer = cinfo->src->bytes_in_buffer;
346
140
    }
347
98.0k
    bytes_in_buffer--;
348
98.0k
    c = GETJOCTET(*next_input_byte++);
349
98.0k
  } while (c == 0xFF);
350
351
45.3k
  if (c == 0) {
352
    /* Found FF/00, which represents an FF data byte */
353
5.30k
    c = 0xFF;
354
40.0k
  } 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
40.0k
    cinfo->unread_marker = c;
364
    /* See if we need to insert some fake zero bits. */
365
40.0k
    goto no_more_bytes;
366
40.0k
  }
367
45.3k
      }
368
369
      /* OK, load c into get_buffer */
370
1.74M
      get_buffer = (get_buffer << 8) | c;
371
1.74M
      bits_left += 8;
372
1.74M
    } /* end while */
373
664k
  } else {
374
704k
  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
704k
    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
470k
      if (! cinfo->entropy->insufficient_data) {
386
42.6k
  WARNMS(cinfo, JWRN_HIT_MARKER);
387
42.6k
  cinfo->entropy->insufficient_data = TRUE;
388
42.6k
      }
389
      /* Fill the buffer with zero bits */
390
470k
      get_buffer <<= MIN_GET_BITS - bits_left;
391
470k
      bits_left = MIN_GET_BITS;
392
470k
    }
393
704k
  }
394
395
  /* Unload the local registers */
396
932k
  state->next_input_byte = next_input_byte;
397
932k
  state->bytes_in_buffer = bytes_in_buffer;
398
932k
  state->get_buffer = get_buffer;
399
932k
  state->bits_left = bits_left;
400
401
932k
  return TRUE;
402
932k
}
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
252k
{
415
252k
  register int l = min_bits;
416
252k
  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
252k
  CHECK_BIT_BUFFER(*state, l, return -1);
422
252k
  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
836k
  while (code > htbl->maxcode[l]) {
428
583k
    code <<= 1;
429
583k
    CHECK_BIT_BUFFER(*state, 1, return -1);
430
583k
    code |= GET_BITS(1);
431
583k
    l++;
432
583k
  }
433
434
  /* Unload the local registers */
435
252k
  state->get_buffer = get_buffer;
436
252k
  state->bits_left = bits_left;
437
438
  /* With garbage input we may reach the sentinel value l = 17. */
439
440
252k
  if (l > 16) {
441
43.7k
    WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
442
43.7k
    return 0;     /* fake a zero as the safest result */
443
43.7k
  }
444
445
209k
  return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
446
252k
}
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.37M
#define NEG_1 ((unsigned int)-1)
455
#define AVOID_TABLES
456
#ifdef AVOID_TABLES
457
458
1.37M
#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.7M
{
482
60.7M
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
483
60.7M
  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.7M
  cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
488
60.7M
  entropy->bitstate.bits_left = 0;
489
490
  /* Advance past the RSTn marker */
491
60.7M
  if (! (*cinfo->marker->read_restart_marker) (cinfo))
492
0
    return FALSE;
493
494
  /* Re-initialize DC predictions to 0 */
495
178M
  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
496
117M
    entropy->saved.last_dc_val[ci] = 0;
497
498
  /* Reset restart counter */
499
60.7M
  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.7M
  if (cinfo->unread_marker == 0)
507
3.39k
    entropy->pub.insufficient_data = FALSE;
508
509
60.7M
  return TRUE;
510
60.7M
}
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
76.4M
{
531
76.4M
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
532
76.4M
  int blkn;
533
76.4M
  BITREAD_STATE_VARS;
534
76.4M
  savable_state state;
535
536
  /* Process restart marker if needed; may have to suspend */
537
76.4M
  if (cinfo->restart_interval) {
538
60.9M
    if (entropy->restarts_to_go == 0)
539
60.7M
      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
76.4M
  if (! entropy->pub.insufficient_data) {
547
548
    /* Load up working state */
549
29.7k
    BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
550
29.7k
    ASSIGN_STATE(state, entropy->saved);
551
552
    /* Outer loop handles each block in the MCU */
553
554
90.1k
    for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
555
60.4k
      JBLOCKROW block = MCU_data[blkn];
556
60.4k
      d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
557
60.4k
      d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
558
60.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
60.4k
      HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
564
60.4k
      if (s) {
565
43.0k
  CHECK_BIT_BUFFER(br_state, s, return FALSE);
566
43.0k
  r = GET_BITS(s);
567
43.0k
  s = HUFF_EXTEND(r, s);
568
43.0k
      }
569
570
60.4k
      if (entropy->dc_needed[blkn]) {
571
  /* Convert DC difference to actual value, update last_dc_val */
572
60.4k
  int ci = cinfo->MCU_membership[blkn];
573
60.4k
  s += state.last_dc_val[ci];
574
60.4k
  state.last_dc_val[ci] = s;
575
  /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
576
60.4k
  (*block)[0] = (JCOEF) s;
577
60.4k
      }
578
579
60.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.43M
  for (k = 1; k < DCTSIZE2; k++) {
584
1.40M
    HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
585
      
586
1.40M
    r = s >> 4;
587
1.40M
    s &= 15;
588
      
589
1.40M
    if (s) {
590
1.33M
      k += r;
591
1.33M
      CHECK_BIT_BUFFER(br_state, s, return FALSE);
592
1.33M
      r = GET_BITS(s);
593
1.33M
      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.33M
      (*block)[jpeg_natural_order[k]] = (JCOEF) s;
599
1.33M
    } else {
600
64.4k
      if (r != 15)
601
28.4k
        break;
602
36.0k
      k += 15;
603
36.0k
    }
604
1.40M
  }
605
606
60.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
60.4k
    }
629
630
    /* Completed MCU, so update state */
631
29.7k
    BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
632
29.7k
    ASSIGN_STATE(entropy->saved, state);
633
29.7k
  }
634
635
  /* Account for restart interval (no-op if not using restarts) */
636
76.4M
  if( entropy->restarts_to_go == 0 )
637
1.00k
      entropy->restarts_to_go = ~0U;
638
76.4M
  else
639
76.4M
      entropy->restarts_to_go--;
640
641
76.4M
  return TRUE;
642
76.4M
}
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
543
{
652
543
  huff_entropy_ptr entropy;
653
543
  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
543
  entropy = (huff_entropy_ptr)
664
543
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
665
543
        SIZEOF(huff_entropy_decoder));
666
543
  cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
667
543
  entropy->pub.start_pass = start_pass_huff_decoder;
668
543
  entropy->pub.decode_mcu = decode_mcu;
669
670
  /* Mark tables unallocated */
671
2.71k
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
672
    entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
673
2.17k
  }
674
543
}