Coverage Report

Created: 2022-11-14 06:33

/src/libjpeg-turbo/jdphuff.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jdphuff.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1995-1997, Thomas G. Lane.
6
 * libjpeg-turbo Modifications:
7
 * Copyright (C) 2015-2016, 2018-2022, D. R. Commander.
8
 * For conditions of distribution and use, see the accompanying README.ijg
9
 * file.
10
 *
11
 * This file contains Huffman entropy decoding routines for progressive JPEG.
12
 *
13
 * Much of the complexity here has to do with supporting input suspension.
14
 * If the data source module demands suspension, we want to be able to back
15
 * up to the start of the current MCU.  To do this, we copy state variables
16
 * into local working storage, and update them back to the permanent
17
 * storage only upon successful completion of an MCU.
18
 *
19
 * NOTE: All referenced figures are from
20
 * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994.
21
 */
22
23
#define JPEG_INTERNALS
24
#include "jinclude.h"
25
#include "jpeglib.h"
26
#include "jdhuff.h"             /* Declarations shared with jdhuff.c */
27
#include <limits.h>
28
29
30
#ifdef D_PROGRESSIVE_SUPPORTED
31
32
/*
33
 * Expanded entropy decoder object for progressive Huffman decoding.
34
 *
35
 * The savable_state subrecord contains fields that change within an MCU,
36
 * but must not be updated permanently until we complete the MCU.
37
 */
38
39
typedef struct {
40
  unsigned int EOBRUN;                  /* remaining EOBs in EOBRUN */
41
  int last_dc_val[MAX_COMPS_IN_SCAN];   /* last DC coef for each component */
42
} savable_state;
43
44
typedef struct {
45
  struct jpeg_entropy_decoder pub; /* public fields */
46
47
  /* These fields are loaded into local variables at start of each MCU.
48
   * In case of suspension, we exit WITHOUT updating them.
49
   */
50
  bitread_perm_state bitstate;  /* Bit buffer at start of MCU */
51
  savable_state saved;          /* Other state at start of MCU */
52
53
  /* These fields are NOT loaded into local working state. */
54
  unsigned int restarts_to_go;  /* MCUs left in this restart interval */
55
56
  /* Pointers to derived tables (these workspaces have image lifespan) */
57
  d_derived_tbl *derived_tbls[NUM_HUFF_TBLS];
58
59
  d_derived_tbl *ac_derived_tbl; /* active table during an AC scan */
60
} phuff_entropy_decoder;
61
62
typedef phuff_entropy_decoder *phuff_entropy_ptr;
63
64
/* Forward declarations */
65
METHODDEF(boolean) decode_mcu_DC_first(j_decompress_ptr cinfo,
66
                                       JBLOCKROW *MCU_data);
67
METHODDEF(boolean) decode_mcu_AC_first(j_decompress_ptr cinfo,
68
                                       JBLOCKROW *MCU_data);
69
METHODDEF(boolean) decode_mcu_DC_refine(j_decompress_ptr cinfo,
70
                                        JBLOCKROW *MCU_data);
71
METHODDEF(boolean) decode_mcu_AC_refine(j_decompress_ptr cinfo,
72
                                        JBLOCKROW *MCU_data);
73
74
75
/*
76
 * Initialize for a Huffman-compressed scan.
77
 */
78
79
METHODDEF(void)
80
start_pass_phuff_decoder(j_decompress_ptr cinfo)
81
11.8k
{
82
11.8k
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
83
11.8k
  boolean is_DC_band, bad;
84
11.8k
  int ci, coefi, tbl;
85
11.8k
  d_derived_tbl **pdtbl;
86
11.8k
  int *coef_bit_ptr, *prev_coef_bit_ptr;
87
11.8k
  jpeg_component_info *compptr;
88
89
11.8k
  is_DC_band = (cinfo->Ss == 0);
90
91
  /* Validate scan parameters */
92
11.8k
  bad = FALSE;
93
11.8k
  if (is_DC_band) {
94
3.45k
    if (cinfo->Se != 0)
95
245
      bad = TRUE;
96
8.41k
  } else {
97
    /* need not check Ss/Se < 0 since they came from unsigned bytes */
98
8.41k
    if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
99
826
      bad = TRUE;
100
    /* AC scans may have only one component */
101
8.41k
    if (cinfo->comps_in_scan != 1)
102
93
      bad = TRUE;
103
8.41k
  }
104
11.8k
  if (cinfo->Ah != 0) {
105
    /* Successive approximation refinement scan: must have Al = Ah-1. */
106
6.14k
    if (cinfo->Al != cinfo->Ah - 1)
107
661
      bad = TRUE;
108
6.14k
  }
109
11.8k
  if (cinfo->Al > 13)           /* need not check for < 0 */
110
636
    bad = TRUE;
111
  /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
112
   * but the spec doesn't say so, and we try to be liberal about what we
113
   * accept.  Note: large Al values could result in out-of-range DC
114
   * coefficients during early scans, leading to bizarre displays due to
115
   * overflows in the IDCT math.  But we won't crash.
116
   */
117
11.8k
  if (bad)
118
1.21k
    ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
119
11.8k
             cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
120
  /* Update progression status, and verify that scan order is legal.
121
   * Note that inter-scan inconsistencies are treated as warnings
122
   * not fatal errors ... not clear if this is right way to behave.
123
   */
124
24.5k
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
125
12.6k
    int cindex = cinfo->cur_comp_info[ci]->component_index;
126
12.6k
    coef_bit_ptr = &cinfo->coef_bits[cindex][0];
127
12.6k
    prev_coef_bit_ptr = &cinfo->coef_bits[cindex + cinfo->num_components][0];
128
12.6k
    if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
129
6.57k
      WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
130
380k
    for (coefi = MIN(cinfo->Ss, 1); coefi <= MAX(cinfo->Se, 9); coefi++) {
131
368k
      if (cinfo->input_scan_number > 1)
132
174k
        prev_coef_bit_ptr[coefi] = coef_bit_ptr[coefi];
133
193k
      else
134
193k
        prev_coef_bit_ptr[coefi] = 0;
135
368k
    }
136
252k
    for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
137
239k
      int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
138
239k
      if (cinfo->Ah != expected)
139
131k
        WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
140
239k
      coef_bit_ptr[coefi] = cinfo->Al;
141
239k
    }
142
12.6k
  }
143
144
  /* Select MCU decoding routine */
145
11.8k
  if (cinfo->Ah == 0) {
146
5.38k
    if (is_DC_band)
147
1.38k
      entropy->pub.decode_mcu = decode_mcu_DC_first;
148
3.99k
    else
149
3.99k
      entropy->pub.decode_mcu = decode_mcu_AC_first;
150
6.49k
  } else {
151
6.49k
    if (is_DC_band)
152
1.82k
      entropy->pub.decode_mcu = decode_mcu_DC_refine;
153
4.66k
    else
154
4.66k
      entropy->pub.decode_mcu = decode_mcu_AC_refine;
155
6.49k
  }
156
157
24.5k
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
158
12.6k
    compptr = cinfo->cur_comp_info[ci];
159
    /* Make sure requested tables are present, and compute derived tables.
160
     * We may build same derived table more than once, but it's not expensive.
161
     */
162
12.6k
    if (is_DC_band) {
163
5.17k
      if (cinfo->Ah == 0) {     /* DC refinement needs no table */
164
1.38k
        tbl = compptr->dc_tbl_no;
165
1.38k
        pdtbl = (d_derived_tbl **)(entropy->derived_tbls) + tbl;
166
1.38k
        jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, pdtbl);
167
1.38k
      }
168
7.45k
    } else {
169
7.45k
      tbl = compptr->ac_tbl_no;
170
7.45k
      pdtbl = (d_derived_tbl **)(entropy->derived_tbls) + tbl;
171
7.45k
      jpeg_make_d_derived_tbl(cinfo, FALSE, tbl, pdtbl);
172
      /* remember the single active table */
173
7.45k
      entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
174
7.45k
    }
175
    /* Initialize DC predictions to 0 */
176
12.6k
    entropy->saved.last_dc_val[ci] = 0;
177
12.6k
  }
178
179
  /* Initialize bitread state variables */
180
11.8k
  entropy->bitstate.bits_left = 0;
181
11.8k
  entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
182
11.8k
  entropy->pub.insufficient_data = FALSE;
183
184
  /* Initialize private state variables */
185
11.8k
  entropy->saved.EOBRUN = 0;
186
187
  /* Initialize restart counter */
188
11.8k
  entropy->restarts_to_go = cinfo->restart_interval;
189
11.8k
}
190
191
192
/*
193
 * Figure F.12: extend sign bit.
194
 * On some machines, a shift and add will be faster than a table lookup.
195
 */
196
197
#define AVOID_TABLES
198
#ifdef AVOID_TABLES
199
200
23.4M
#define NEG_1  ((unsigned)-1)
201
#define HUFF_EXTEND(x, s) \
202
1.61M
  ((x) < (1 << ((s) - 1)) ? (x) + (((NEG_1) << (s)) + 1) : (x))
203
204
#else
205
206
#define HUFF_EXTEND(x, s) \
207
  ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
208
209
static const int extend_test[16] = {   /* entry n is 2**(n-1) */
210
  0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
211
  0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000
212
};
213
214
static const int extend_offset[16] = { /* entry n is (-1 << n) + 1 */
215
  0, ((-1) << 1) + 1, ((-1) << 2) + 1, ((-1) << 3) + 1, ((-1) << 4) + 1,
216
  ((-1) << 5) + 1, ((-1) << 6) + 1, ((-1) << 7) + 1, ((-1) << 8) + 1,
217
  ((-1) << 9) + 1, ((-1) << 10) + 1, ((-1) << 11) + 1, ((-1) << 12) + 1,
218
  ((-1) << 13) + 1, ((-1) << 14) + 1, ((-1) << 15) + 1
219
};
220
221
#endif /* AVOID_TABLES */
222
223
224
/*
225
 * Check for a restart marker & resynchronize decoder.
226
 * Returns FALSE if must suspend.
227
 */
228
229
LOCAL(boolean)
230
process_restart(j_decompress_ptr cinfo)
231
37.1k
{
232
37.1k
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
233
37.1k
  int ci;
234
235
  /* Throw away any unused bits remaining in bit buffer; */
236
  /* include any full bytes in next_marker's count of discarded bytes */
237
37.1k
  cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
238
37.1k
  entropy->bitstate.bits_left = 0;
239
240
  /* Advance past the RSTn marker */
241
37.1k
  if (!(*cinfo->marker->read_restart_marker) (cinfo))
242
0
    return FALSE;
243
244
  /* Re-initialize DC predictions to 0 */
245
74.6k
  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
246
37.4k
    entropy->saved.last_dc_val[ci] = 0;
247
  /* Re-init EOB run count, too */
248
37.1k
  entropy->saved.EOBRUN = 0;
249
250
  /* Reset restart counter */
251
37.1k
  entropy->restarts_to_go = cinfo->restart_interval;
252
253
  /* Reset out-of-data flag, unless read_restart_marker left us smack up
254
   * against a marker.  In that case we will end up treating the next data
255
   * segment as empty, and we can avoid producing bogus output pixels by
256
   * leaving the flag set.
257
   */
258
37.1k
  if (cinfo->unread_marker == 0)
259
3.48k
    entropy->pub.insufficient_data = FALSE;
260
261
37.1k
  return TRUE;
262
37.1k
}
263
264
265
/*
266
 * Huffman MCU decoding.
267
 * Each of these routines decodes and returns one MCU's worth of
268
 * Huffman-compressed coefficients.
269
 * The coefficients are reordered from zigzag order into natural array order,
270
 * but are not dequantized.
271
 *
272
 * The i'th block of the MCU is stored into the block pointed to by
273
 * MCU_data[i].  WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
274
 *
275
 * We return FALSE if data source requested suspension.  In that case no
276
 * changes have been made to permanent state.  (Exception: some output
277
 * coefficients may already have been assigned.  This is harmless for
278
 * spectral selection, since we'll just re-assign them on the next call.
279
 * Successive approximation AC refinement has to be more careful, however.)
280
 */
281
282
/*
283
 * MCU decoding for DC initial scan (either spectral selection,
284
 * or first pass of successive approximation).
285
 */
286
287
METHODDEF(boolean)
288
decode_mcu_DC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
289
9.63M
{
290
9.63M
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
291
9.63M
  int Al = cinfo->Al;
292
9.63M
  register int s, r;
293
9.63M
  int blkn, ci;
294
9.63M
  JBLOCKROW block;
295
9.63M
  BITREAD_STATE_VARS;
296
9.63M
  savable_state state;
297
9.63M
  d_derived_tbl *tbl;
298
9.63M
  jpeg_component_info *compptr;
299
300
  /* Process restart marker if needed; may have to suspend */
301
9.63M
  if (cinfo->restart_interval) {
302
25.9k
    if (entropy->restarts_to_go == 0)
303
14.7k
      if (!process_restart(cinfo))
304
0
        return FALSE;
305
25.9k
  }
306
307
  /* If we've run out of data, just leave the MCU set to zeroes.
308
   * This way, we return uniform gray for the remainder of the segment.
309
   */
310
9.63M
  if (!entropy->pub.insufficient_data) {
311
312
    /* Load up working state */
313
1.48M
    BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
314
1.48M
    state = entropy->saved;
315
316
    /* Outer loop handles each block in the MCU */
317
318
2.96M
    for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
319
1.48M
      block = MCU_data[blkn];
320
1.48M
      ci = cinfo->MCU_membership[blkn];
321
1.48M
      compptr = cinfo->cur_comp_info[ci];
322
1.48M
      tbl = entropy->derived_tbls[compptr->dc_tbl_no];
323
324
      /* Decode a single block's worth of coefficients */
325
326
      /* Section F.2.2.1: decode the DC coefficient difference */
327
1.48M
      HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
328
1.48M
      if (s) {
329
1.46M
        CHECK_BIT_BUFFER(br_state, s, return FALSE);
330
1.46M
        r = GET_BITS(s);
331
1.46M
        s = HUFF_EXTEND(r, s);
332
1.46M
      }
333
334
      /* Convert DC difference to actual value, update last_dc_val */
335
1.48M
      if ((state.last_dc_val[ci] >= 0 &&
336
1.48M
           s > INT_MAX - state.last_dc_val[ci]) ||
337
1.48M
          (state.last_dc_val[ci] < 0 && s < INT_MIN - state.last_dc_val[ci]))
338
14
        ERREXIT(cinfo, JERR_BAD_DCT_COEF);
339
1.48M
      s += state.last_dc_val[ci];
340
1.48M
      state.last_dc_val[ci] = s;
341
      /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
342
1.48M
      (*block)[0] = (JCOEF)LEFT_SHIFT(s, Al);
343
1.48M
    }
344
345
    /* Completed MCU, so update state */
346
1.48M
    BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
347
1.48M
    entropy->saved = state;
348
1.48M
  }
349
350
  /* Account for restart interval (no-op if not using restarts) */
351
9.63M
  if (cinfo->restart_interval)
352
25.9k
    entropy->restarts_to_go--;
353
354
9.63M
  return TRUE;
355
9.63M
}
356
357
358
/*
359
 * MCU decoding for AC initial scan (either spectral selection,
360
 * or first pass of successive approximation).
361
 */
362
363
METHODDEF(boolean)
364
decode_mcu_AC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
365
7.27M
{
366
7.27M
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
367
7.27M
  int Se = cinfo->Se;
368
7.27M
  int Al = cinfo->Al;
369
7.27M
  register int s, k, r;
370
7.27M
  unsigned int EOBRUN;
371
7.27M
  JBLOCKROW block;
372
7.27M
  BITREAD_STATE_VARS;
373
7.27M
  d_derived_tbl *tbl;
374
375
  /* Process restart marker if needed; may have to suspend */
376
7.27M
  if (cinfo->restart_interval) {
377
429k
    if (entropy->restarts_to_go == 0)
378
14.3k
      if (!process_restart(cinfo))
379
0
        return FALSE;
380
429k
  }
381
382
  /* If we've run out of data, just leave the MCU set to zeroes.
383
   * This way, we return uniform gray for the remainder of the segment.
384
   */
385
7.27M
  if (!entropy->pub.insufficient_data) {
386
387
    /* Load up working state.
388
     * We can avoid loading/saving bitread state if in an EOB run.
389
     */
390
77.2k
    EOBRUN = entropy->saved.EOBRUN;     /* only part of saved state we need */
391
392
    /* There is always only one block per MCU */
393
394
77.2k
    if (EOBRUN > 0)             /* if it's a band of zeroes... */
395
57.9k
      EOBRUN--;                 /* ...process it now (we do nothing) */
396
19.3k
    else {
397
19.3k
      BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
398
19.3k
      block = MCU_data[0];
399
19.3k
      tbl = entropy->ac_derived_tbl;
400
401
163k
      for (k = cinfo->Ss; k <= Se; k++) {
402
151k
        HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
403
151k
        r = s >> 4;
404
151k
        s &= 15;
405
151k
        if (s) {
406
144k
          k += r;
407
144k
          CHECK_BIT_BUFFER(br_state, s, return FALSE);
408
144k
          r = GET_BITS(s);
409
144k
          s = HUFF_EXTEND(r, s);
410
          /* Scale and output coefficient in natural (dezigzagged) order */
411
144k
          (*block)[jpeg_natural_order[k]] = (JCOEF)LEFT_SHIFT(s, Al);
412
144k
        } else {
413
6.76k
          if (r == 15) {        /* ZRL */
414
246
            k += 15;            /* skip 15 zeroes in band */
415
6.51k
          } else {              /* EOBr, run length is 2^r + appended bits */
416
6.51k
            EOBRUN = 1 << r;
417
6.51k
            if (r) {            /* EOBr, r > 0 */
418
1.76k
              CHECK_BIT_BUFFER(br_state, r, return FALSE);
419
1.76k
              r = GET_BITS(r);
420
1.76k
              EOBRUN += r;
421
1.76k
            }
422
6.51k
            EOBRUN--;           /* this band is processed at this moment */
423
6.51k
            break;              /* force end-of-band */
424
6.51k
          }
425
6.76k
        }
426
151k
      }
427
428
19.3k
      BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
429
19.3k
    }
430
431
    /* Completed MCU, so update state */
432
77.2k
    entropy->saved.EOBRUN = EOBRUN;     /* only part of saved state we need */
433
77.2k
  }
434
435
  /* Account for restart interval (no-op if not using restarts) */
436
7.27M
  if (cinfo->restart_interval)
437
429k
    entropy->restarts_to_go--;
438
439
7.27M
  return TRUE;
440
7.27M
}
441
442
443
/*
444
 * MCU decoding for DC successive approximation refinement scan.
445
 * Note: we assume such scans can be multi-component, although the spec
446
 * is not very clear on the point.
447
 */
448
449
METHODDEF(boolean)
450
decode_mcu_DC_refine(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
451
2.99M
{
452
2.99M
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
453
2.99M
  int p1 = 1 << cinfo->Al;      /* 1 in the bit position being coded */
454
2.99M
  int blkn;
455
2.99M
  JBLOCKROW block;
456
2.99M
  BITREAD_STATE_VARS;
457
458
  /* Process restart marker if needed; may have to suspend */
459
2.99M
  if (cinfo->restart_interval) {
460
8.69k
    if (entropy->restarts_to_go == 0)
461
3.13k
      if (!process_restart(cinfo))
462
0
        return FALSE;
463
8.69k
  }
464
465
  /* Not worth the cycles to check insufficient_data here,
466
   * since we will not change the data anyway if we read zeroes.
467
   */
468
469
  /* Load up working state */
470
2.99M
  BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
471
472
  /* Outer loop handles each block in the MCU */
473
474
9.41M
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
475
6.42M
    block = MCU_data[blkn];
476
477
    /* Encoded data is simply the next bit of the two's-complement DC value */
478
6.42M
    CHECK_BIT_BUFFER(br_state, 1, return FALSE);
479
6.42M
    if (GET_BITS(1))
480
43.0k
      (*block)[0] |= p1;
481
    /* Note: since we use |=, repeating the assignment later is safe */
482
6.42M
  }
483
484
  /* Completed MCU, so update state */
485
2.99M
  BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
486
487
  /* Account for restart interval (no-op if not using restarts) */
488
2.99M
  if (cinfo->restart_interval)
489
8.69k
    entropy->restarts_to_go--;
490
491
2.99M
  return TRUE;
492
2.99M
}
493
494
495
/*
496
 * MCU decoding for AC successive approximation refinement scan.
497
 */
498
499
METHODDEF(boolean)
500
decode_mcu_AC_refine(j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
501
21.9M
{
502
21.9M
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
503
21.9M
  int Se = cinfo->Se;
504
21.9M
  int p1 = 1 << cinfo->Al;        /* 1 in the bit position being coded */
505
21.9M
  int m1 = (NEG_1) << cinfo->Al;  /* -1 in the bit position being coded */
506
21.9M
  register int s, k, r;
507
21.9M
  unsigned int EOBRUN;
508
21.9M
  JBLOCKROW block;
509
21.9M
  JCOEFPTR thiscoef;
510
21.9M
  BITREAD_STATE_VARS;
511
21.9M
  d_derived_tbl *tbl;
512
21.9M
  int num_newnz;
513
21.9M
  int newnz_pos[DCTSIZE2];
514
515
  /* Process restart marker if needed; may have to suspend */
516
21.9M
  if (cinfo->restart_interval) {
517
149k
    if (entropy->restarts_to_go == 0)
518
4.95k
      if (!process_restart(cinfo))
519
0
        return FALSE;
520
149k
  }
521
522
  /* If we've run out of data, don't modify the MCU.
523
   */
524
21.9M
  if (!entropy->pub.insufficient_data) {
525
526
    /* Load up working state */
527
838k
    BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
528
838k
    EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
529
530
    /* There is always only one block per MCU */
531
838k
    block = MCU_data[0];
532
838k
    tbl = entropy->ac_derived_tbl;
533
534
    /* If we are forced to suspend, we must undo the assignments to any newly
535
     * nonzero coefficients in the block, because otherwise we'd get confused
536
     * next time about which coefficients were already nonzero.
537
     * But we need not undo addition of bits to already-nonzero coefficients;
538
     * instead, we can test the current bit to see if we already did it.
539
     */
540
838k
    num_newnz = 0;
541
542
    /* initialize coefficient loop counter to start of band */
543
838k
    k = cinfo->Ss;
544
545
838k
    if (EOBRUN == 0) {
546
108k
      for (; k <= Se; k++) {
547
100k
        HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
548
100k
        r = s >> 4;
549
100k
        s &= 15;
550
100k
        if (s) {
551
89.6k
          if (s != 1)           /* size of new coef should always be 1 */
552
35.7k
            WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
553
89.6k
          CHECK_BIT_BUFFER(br_state, 1, goto undoit);
554
89.6k
          if (GET_BITS(1))
555
20.1k
            s = p1;             /* newly nonzero coef is positive */
556
69.5k
          else
557
69.5k
            s = m1;             /* newly nonzero coef is negative */
558
89.6k
        } else {
559
10.8k
          if (r != 15) {
560
10.3k
            EOBRUN = 1 << r;    /* EOBr, run length is 2^r + appended bits */
561
10.3k
            if (r) {
562
2.01k
              CHECK_BIT_BUFFER(br_state, r, goto undoit);
563
2.01k
              r = GET_BITS(r);
564
2.01k
              EOBRUN += r;
565
2.01k
            }
566
10.3k
            break;              /* rest of block is handled by EOB logic */
567
10.3k
          }
568
          /* note s = 0 for processing ZRL */
569
10.8k
        }
570
        /* Advance over already-nonzero coefs and r still-zero coefs,
571
         * appending correction bits to the nonzeroes.  A correction bit is 1
572
         * if the absolute value of the coefficient must be increased.
573
         */
574
246k
        do {
575
246k
          thiscoef = *block + jpeg_natural_order[k];
576
246k
          if (*thiscoef != 0) {
577
68.1k
            CHECK_BIT_BUFFER(br_state, 1, goto undoit);
578
68.1k
            if (GET_BITS(1)) {
579
21.0k
              if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
580
4.01k
                if (*thiscoef >= 0)
581
2.25k
                  *thiscoef += (JCOEF)p1;
582
1.76k
                else
583
1.76k
                  *thiscoef += (JCOEF)m1;
584
4.01k
              }
585
21.0k
            }
586
177k
          } else {
587
177k
            if (--r < 0)
588
85.6k
              break;            /* reached target zero coefficient */
589
177k
          }
590
160k
          k++;
591
160k
        } while (k <= Se);
592
90.2k
        if (s) {
593
89.6k
          int pos = jpeg_natural_order[k];
594
          /* Output newly nonzero coefficient */
595
89.6k
          (*block)[pos] = (JCOEF)s;
596
          /* Remember its position in case we have to suspend */
597
89.6k
          newnz_pos[num_newnz++] = pos;
598
89.6k
        }
599
90.2k
      }
600
18.2k
    }
601
602
838k
    if (EOBRUN > 0) {
603
      /* Scan any remaining coefficient positions after the end-of-band
604
       * (the last newly nonzero coefficient, if any).  Append a correction
605
       * bit to each already-nonzero coefficient.  A correction bit is 1
606
       * if the absolute value of the coefficient must be increased.
607
       */
608
51.6M
      for (; k <= Se; k++) {
609
50.8M
        thiscoef = *block + jpeg_natural_order[k];
610
50.8M
        if (*thiscoef != 0) {
611
46.1k
          CHECK_BIT_BUFFER(br_state, 1, goto undoit);
612
46.1k
          if (GET_BITS(1)) {
613
14.5k
            if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
614
2.61k
              if (*thiscoef >= 0)
615
413
                *thiscoef += (JCOEF)p1;
616
2.20k
              else
617
2.20k
                *thiscoef += (JCOEF)m1;
618
2.61k
            }
619
14.5k
          }
620
46.1k
        }
621
50.8M
      }
622
      /* Count one block completed in EOB run */
623
830k
      EOBRUN--;
624
830k
    }
625
626
    /* Completed MCU, so update state */
627
838k
    BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
628
838k
    entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
629
838k
  }
630
631
  /* Account for restart interval (no-op if not using restarts) */
632
21.9M
  if (cinfo->restart_interval)
633
149k
    entropy->restarts_to_go--;
634
635
21.9M
  return TRUE;
636
637
0
undoit:
638
  /* Re-zero any output coefficients that we made newly nonzero */
639
0
  while (num_newnz > 0)
640
0
    (*block)[newnz_pos[--num_newnz]] = 0;
641
642
0
  return FALSE;
643
21.9M
}
644
645
646
/*
647
 * Module initialization routine for progressive Huffman entropy decoding.
648
 */
649
650
GLOBAL(void)
651
jinit_phuff_decoder(j_decompress_ptr cinfo)
652
6.99k
{
653
6.99k
  phuff_entropy_ptr entropy;
654
6.99k
  int *coef_bit_ptr;
655
6.99k
  int ci, i;
656
657
6.99k
  entropy = (phuff_entropy_ptr)
658
6.99k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
659
6.99k
                                sizeof(phuff_entropy_decoder));
660
6.99k
  cinfo->entropy = (struct jpeg_entropy_decoder *)entropy;
661
6.99k
  entropy->pub.start_pass = start_pass_phuff_decoder;
662
663
  /* Mark derived tables unallocated */
664
34.9k
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
665
27.9k
    entropy->derived_tbls[i] = NULL;
666
27.9k
  }
667
668
  /* Create progression status table */
669
6.99k
  cinfo->coef_bits = (int (*)[DCTSIZE2])
670
6.99k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
671
6.99k
                                cinfo->num_components * 2 * DCTSIZE2 *
672
6.99k
                                sizeof(int));
673
6.99k
  coef_bit_ptr = &cinfo->coef_bits[0][0];
674
15.7k
  for (ci = 0; ci < cinfo->num_components; ci++)
675
567k
    for (i = 0; i < DCTSIZE2; i++)
676
558k
      *coef_bit_ptr++ = -1;
677
6.99k
}
678
679
#endif /* D_PROGRESSIVE_SUPPORTED */