Coverage Report

Created: 2026-05-16 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdk-pixbuf/subprojects/libjpeg-turbo-2.1.2/jdphuff.c
Line
Count
Source
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-2021, 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
59.5k
{
82
59.5k
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
83
59.5k
  boolean is_DC_band, bad;
84
59.5k
  int ci, coefi, tbl;
85
59.5k
  d_derived_tbl **pdtbl;
86
59.5k
  int *coef_bit_ptr, *prev_coef_bit_ptr;
87
59.5k
  jpeg_component_info *compptr;
88
89
59.5k
  is_DC_band = (cinfo->Ss == 0);
90
91
  /* Validate scan parameters */
92
59.5k
  bad = FALSE;
93
59.5k
  if (is_DC_band) {
94
23.3k
    if (cinfo->Se != 0)
95
1
      bad = TRUE;
96
36.1k
  } else {
97
    /* need not check Ss/Se < 0 since they came from unsigned bytes */
98
36.1k
    if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
99
9
      bad = TRUE;
100
    /* AC scans may have only one component */
101
36.1k
    if (cinfo->comps_in_scan != 1)
102
0
      bad = TRUE;
103
36.1k
  }
104
59.5k
  if (cinfo->Ah != 0) {
105
    /* Successive approximation refinement scan: must have Al = Ah-1. */
106
44.9k
    if (cinfo->Al != cinfo->Ah - 1)
107
33
      bad = TRUE;
108
44.9k
  }
109
59.5k
  if (cinfo->Al > 13)           /* need not check for < 0 */
110
2
    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
59.5k
  if (bad)
118
40
    ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
119
59.5k
             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
126k
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
125
67.0k
    int cindex = cinfo->cur_comp_info[ci]->component_index;
126
67.0k
    coef_bit_ptr = &cinfo->coef_bits[cindex][0];
127
67.0k
    prev_coef_bit_ptr = &cinfo->coef_bits[cindex + cinfo->num_components][0];
128
67.0k
    if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
129
16.6k
      WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
130
2.11M
    for (coefi = MIN(cinfo->Ss, 1); coefi <= MAX(cinfo->Se, 9); coefi++) {
131
2.04M
      if (cinfo->input_scan_number > 1)
132
2.02M
        prev_coef_bit_ptr[coefi] = coef_bit_ptr[coefi];
133
18.8k
      else
134
18.8k
        prev_coef_bit_ptr[coefi] = 0;
135
2.04M
    }
136
1.39M
    for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
137
1.33M
      int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
138
1.33M
      if (cinfo->Ah != expected)
139
1.23M
        WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
140
1.33M
      coef_bit_ptr[coefi] = cinfo->Al;
141
1.33M
    }
142
67.0k
  }
143
144
  /* Select MCU decoding routine */
145
59.5k
  if (cinfo->Ah == 0) {
146
14.6k
    if (is_DC_band)
147
4.34k
      entropy->pub.decode_mcu = decode_mcu_DC_first;
148
10.2k
    else
149
10.2k
      entropy->pub.decode_mcu = decode_mcu_AC_first;
150
44.9k
  } else {
151
44.9k
    if (is_DC_band)
152
19.0k
      entropy->pub.decode_mcu = decode_mcu_DC_refine;
153
25.9k
    else
154
25.9k
      entropy->pub.decode_mcu = decode_mcu_AC_refine;
155
44.9k
  }
156
157
126k
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
158
67.0k
    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
67.0k
    if (is_DC_band) {
163
30.8k
      if (cinfo->Ah == 0) {     /* DC refinement needs no table */
164
11.4k
        tbl = compptr->dc_tbl_no;
165
11.4k
        pdtbl = (d_derived_tbl **)(entropy->derived_tbls) + tbl;
166
11.4k
        jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, pdtbl);
167
11.4k
      }
168
36.1k
    } else {
169
36.1k
      tbl = compptr->ac_tbl_no;
170
36.1k
      pdtbl = (d_derived_tbl **)(entropy->derived_tbls) + tbl;
171
36.1k
      jpeg_make_d_derived_tbl(cinfo, FALSE, tbl, pdtbl);
172
      /* remember the single active table */
173
36.1k
      entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
174
36.1k
    }
175
    /* Initialize DC predictions to 0 */
176
67.0k
    entropy->saved.last_dc_val[ci] = 0;
177
67.0k
  }
178
179
  /* Initialize bitread state variables */
180
59.5k
  entropy->bitstate.bits_left = 0;
181
59.5k
  entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
182
59.5k
  entropy->pub.insufficient_data = FALSE;
183
184
  /* Initialize private state variables */
185
59.5k
  entropy->saved.EOBRUN = 0;
186
187
  /* Initialize restart counter */
188
59.5k
  entropy->restarts_to_go = cinfo->restart_interval;
189
59.5k
}
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
195M
#define NEG_1  ((unsigned)-1)
201
#define HUFF_EXTEND(x, s) \
202
6.41M
  ((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
880k
{
232
880k
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
233
880k
  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
880k
  cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
238
880k
  entropy->bitstate.bits_left = 0;
239
240
  /* Advance past the RSTn marker */
241
880k
  if (!(*cinfo->marker->read_restart_marker) (cinfo))
242
18.8k
    return FALSE;
243
244
  /* Re-initialize DC predictions to 0 */
245
1.77M
  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
246
911k
    entropy->saved.last_dc_val[ci] = 0;
247
  /* Re-init EOB run count, too */
248
861k
  entropy->saved.EOBRUN = 0;
249
250
  /* Reset restart counter */
251
861k
  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
861k
  if (cinfo->unread_marker == 0)
259
37.4k
    entropy->pub.insufficient_data = FALSE;
260
261
861k
  return TRUE;
262
880k
}
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
16.6M
{
290
16.6M
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
291
16.6M
  int Al = cinfo->Al;
292
16.6M
  register int s, r;
293
16.6M
  int blkn, ci;
294
16.6M
  JBLOCKROW block;
295
16.6M
  BITREAD_STATE_VARS;
296
16.6M
  savable_state state;
297
16.6M
  d_derived_tbl *tbl;
298
16.6M
  jpeg_component_info *compptr;
299
300
  /* Process restart marker if needed; may have to suspend */
301
16.6M
  if (cinfo->restart_interval) {
302
888k
    if (entropy->restarts_to_go == 0)
303
61.3k
      if (!process_restart(cinfo))
304
674
        return FALSE;
305
888k
  }
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
16.6M
  if (!entropy->pub.insufficient_data) {
311
312
    /* Load up working state */
313
3.94M
    BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
314
3.94M
    state = entropy->saved;
315
316
    /* Outer loop handles each block in the MCU */
317
318
8.65M
    for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
319
4.73M
      block = MCU_data[blkn];
320
4.73M
      ci = cinfo->MCU_membership[blkn];
321
4.73M
      compptr = cinfo->cur_comp_info[ci];
322
4.73M
      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
4.73M
      HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
328
4.71M
      if (s) {
329
3.89M
        CHECK_BIT_BUFFER(br_state, s, return FALSE);
330
3.89M
        r = GET_BITS(s);
331
3.89M
        s = HUFF_EXTEND(r, s);
332
3.89M
      }
333
334
      /* Convert DC difference to actual value, update last_dc_val */
335
4.70M
      if ((state.last_dc_val[ci] >= 0 &&
336
1.90M
           s > INT_MAX - state.last_dc_val[ci]) ||
337
4.70M
          (state.last_dc_val[ci] < 0 && s < INT_MIN - state.last_dc_val[ci]))
338
35
        ERREXIT(cinfo, JERR_BAD_DCT_COEF);
339
4.70M
      s += state.last_dc_val[ci];
340
4.70M
      state.last_dc_val[ci] = s;
341
      /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
342
4.70M
      (*block)[0] = (JCOEF)LEFT_SHIFT(s, Al);
343
4.70M
    }
344
345
    /* Completed MCU, so update state */
346
3.91M
    BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
347
3.91M
    entropy->saved = state;
348
3.91M
  }
349
350
  /* Account for restart interval (no-op if not using restarts) */
351
16.6M
  if (cinfo->restart_interval)
352
865k
    entropy->restarts_to_go--;
353
354
16.6M
  return TRUE;
355
16.6M
}
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
12.9M
{
366
12.9M
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
367
12.9M
  int Se = cinfo->Se;
368
12.9M
  int Al = cinfo->Al;
369
12.9M
  register int s, k, r;
370
12.9M
  unsigned int EOBRUN;
371
12.9M
  JBLOCKROW block;
372
12.9M
  BITREAD_STATE_VARS;
373
12.9M
  d_derived_tbl *tbl;
374
375
  /* Process restart marker if needed; may have to suspend */
376
12.9M
  if (cinfo->restart_interval) {
377
1.77M
    if (entropy->restarts_to_go == 0)
378
35.7k
      if (!process_restart(cinfo))
379
10.5k
        return FALSE;
380
1.77M
  }
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
12.8M
  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
2.56M
    EOBRUN = entropy->saved.EOBRUN;     /* only part of saved state we need */
391
392
    /* There is always only one block per MCU */
393
394
2.56M
    if (EOBRUN > 0)             /* if it's a band of zeroes... */
395
1.50M
      EOBRUN--;                 /* ...process it now (we do nothing) */
396
1.05M
    else {
397
1.05M
      BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
398
1.05M
      block = MCU_data[0];
399
1.05M
      tbl = entropy->ac_derived_tbl;
400
401
3.59M
      for (k = cinfo->Ss; k <= Se; k++) {
402
3.33M
        HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
403
3.31M
        r = s >> 4;
404
3.31M
        s &= 15;
405
3.31M
        if (s) {
406
2.52M
          k += r;
407
2.52M
          CHECK_BIT_BUFFER(br_state, s, return FALSE);
408
2.52M
          r = GET_BITS(s);
409
2.52M
          s = HUFF_EXTEND(r, s);
410
          /* Scale and output coefficient in natural (dezigzagged) order */
411
2.52M
          (*block)[jpeg_natural_order[k]] = (JCOEF)LEFT_SHIFT(s, Al);
412
2.52M
        } else {
413
791k
          if (r == 15) {        /* ZRL */
414
13.0k
            k += 15;            /* skip 15 zeroes in band */
415
778k
          } else {              /* EOBr, run length is 2^r + appended bits */
416
778k
            EOBRUN = 1 << r;
417
778k
            if (r) {            /* EOBr, r > 0 */
418
58.3k
              CHECK_BIT_BUFFER(br_state, r, return FALSE);
419
58.1k
              r = GET_BITS(r);
420
58.1k
              EOBRUN += r;
421
58.1k
            }
422
777k
            EOBRUN--;           /* this band is processed at this moment */
423
777k
            break;              /* force end-of-band */
424
778k
          }
425
791k
        }
426
3.31M
      }
427
428
1.03M
      BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
429
1.03M
    }
430
431
    /* Completed MCU, so update state */
432
2.54M
    entropy->saved.EOBRUN = EOBRUN;     /* only part of saved state we need */
433
2.54M
  }
434
435
  /* Account for restart interval (no-op if not using restarts) */
436
12.8M
  if (cinfo->restart_interval)
437
1.75M
    entropy->restarts_to_go--;
438
439
12.8M
  return TRUE;
440
12.8M
}
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.28M
{
452
2.28M
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
453
2.28M
  int p1 = 1 << cinfo->Al;      /* 1 in the bit position being coded */
454
2.28M
  int blkn;
455
2.28M
  JBLOCKROW block;
456
2.28M
  BITREAD_STATE_VARS;
457
458
  /* Process restart marker if needed; may have to suspend */
459
2.28M
  if (cinfo->restart_interval) {
460
2.12M
    if (entropy->restarts_to_go == 0)
461
578k
      if (!process_restart(cinfo))
462
5.17k
        return FALSE;
463
2.12M
  }
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.27M
  BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
471
472
  /* Outer loop handles each block in the MCU */
473
474
5.32M
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
475
3.05M
    block = MCU_data[blkn];
476
477
    /* Encoded data is simply the next bit of the two's-complement DC value */
478
3.05M
    CHECK_BIT_BUFFER(br_state, 1, return FALSE);
479
3.05M
    if (GET_BITS(1))
480
362k
      (*block)[0] |= p1;
481
    /* Note: since we use |=, repeating the assignment later is safe */
482
3.05M
  }
483
484
  /* Completed MCU, so update state */
485
2.26M
  BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
486
487
  /* Account for restart interval (no-op if not using restarts) */
488
2.26M
  if (cinfo->restart_interval)
489
2.11M
    entropy->restarts_to_go--;
490
491
2.26M
  return TRUE;
492
2.27M
}
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
190M
{
502
190M
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
503
190M
  int Se = cinfo->Se;
504
190M
  int p1 = 1 << cinfo->Al;        /* 1 in the bit position being coded */
505
190M
  int m1 = (NEG_1) << cinfo->Al;  /* -1 in the bit position being coded */
506
190M
  register int s, k, r;
507
190M
  unsigned int EOBRUN;
508
190M
  JBLOCKROW block;
509
190M
  JCOEFPTR thiscoef;
510
190M
  BITREAD_STATE_VARS;
511
190M
  d_derived_tbl *tbl;
512
190M
  int num_newnz;
513
190M
  int newnz_pos[DCTSIZE2];
514
515
  /* Process restart marker if needed; may have to suspend */
516
190M
  if (cinfo->restart_interval) {
517
4.13M
    if (entropy->restarts_to_go == 0)
518
204k
      if (!process_restart(cinfo))
519
2.38k
        return FALSE;
520
4.13M
  }
521
522
  /* If we've run out of data, don't modify the MCU.
523
   */
524
190M
  if (!entropy->pub.insufficient_data) {
525
526
    /* Load up working state */
527
4.25M
    BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
528
4.25M
    EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
529
530
    /* There is always only one block per MCU */
531
4.25M
    block = MCU_data[0];
532
4.25M
    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
4.25M
    num_newnz = 0;
541
542
    /* initialize coefficient loop counter to start of band */
543
4.25M
    k = cinfo->Ss;
544
545
4.25M
    if (EOBRUN == 0) {
546
11.2M
      for (; k <= Se; k++) {
547
10.1M
        HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
548
10.1M
        r = s >> 4;
549
10.1M
        s &= 15;
550
10.1M
        if (s) {
551
8.82M
          if (s != 1)           /* size of new coef should always be 1 */
552
2.43M
            WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
553
8.82M
          CHECK_BIT_BUFFER(br_state, 1, goto undoit);
554
8.82M
          if (GET_BITS(1))
555
3.74M
            s = p1;             /* newly nonzero coef is positive */
556
5.07M
          else
557
5.07M
            s = m1;             /* newly nonzero coef is negative */
558
8.82M
        } else {
559
1.30M
          if (r != 15) {
560
1.25M
            EOBRUN = 1 << r;    /* EOBr, run length is 2^r + appended bits */
561
1.25M
            if (r) {
562
37.1k
              CHECK_BIT_BUFFER(br_state, r, goto undoit);
563
36.6k
              r = GET_BITS(r);
564
36.6k
              EOBRUN += r;
565
36.6k
            }
566
1.25M
            break;              /* rest of block is handled by EOB logic */
567
1.25M
          }
568
          /* note s = 0 for processing ZRL */
569
1.30M
        }
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
71.2M
        do {
575
71.2M
          thiscoef = *block + jpeg_natural_order[k];
576
71.2M
          if (*thiscoef != 0) {
577
55.6M
            CHECK_BIT_BUFFER(br_state, 1, goto undoit);
578
55.6M
            if (GET_BITS(1)) {
579
24.9M
              if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
580
168k
                if (*thiscoef >= 0)
581
76.6k
                  *thiscoef += p1;
582
91.5k
                else
583
91.5k
                  *thiscoef += m1;
584
168k
              }
585
24.9M
            }
586
55.6M
          } else {
587
15.5M
            if (--r < 0)
588
7.80M
              break;            /* reached target zero coefficient */
589
15.5M
          }
590
63.4M
          k++;
591
63.4M
        } while (k <= Se);
592
8.84M
        if (s) {
593
8.79M
          int pos = jpeg_natural_order[k];
594
          /* Output newly nonzero coefficient */
595
8.79M
          (*block)[pos] = (JCOEF)s;
596
          /* Remember its position in case we have to suspend */
597
8.79M
          newnz_pos[num_newnz++] = pos;
598
8.79M
        }
599
8.84M
      }
600
2.40M
    }
601
602
4.22M
    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
129M
      for (; k <= Se; k++) {
609
126M
        thiscoef = *block + jpeg_natural_order[k];
610
126M
        if (*thiscoef != 0) {
611
19.6M
          CHECK_BIT_BUFFER(br_state, 1, goto undoit);
612
19.6M
          if (GET_BITS(1)) {
613
8.10M
            if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
614
132k
              if (*thiscoef >= 0)
615
73.7k
                *thiscoef += p1;
616
59.0k
              else
617
59.0k
                *thiscoef += m1;
618
132k
            }
619
8.10M
          }
620
19.6M
        }
621
126M
      }
622
      /* Count one block completed in EOB run */
623
3.11M
      EOBRUN--;
624
3.11M
    }
625
626
    /* Completed MCU, so update state */
627
4.21M
    BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
628
4.21M
    entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
629
4.21M
  }
630
631
  /* Account for restart interval (no-op if not using restarts) */
632
190M
  if (cinfo->restart_interval)
633
4.10M
    entropy->restarts_to_go--;
634
635
190M
  return TRUE;
636
637
42.1k
undoit:
638
  /* Re-zero any output coefficients that we made newly nonzero */
639
136k
  while (num_newnz > 0)
640
94.5k
    (*block)[newnz_pos[--num_newnz]] = 0;
641
642
42.1k
  return FALSE;
643
190M
}
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
741
{
653
741
  phuff_entropy_ptr entropy;
654
741
  int *coef_bit_ptr;
655
741
  int ci, i;
656
657
741
  entropy = (phuff_entropy_ptr)
658
741
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
659
741
                                sizeof(phuff_entropy_decoder));
660
741
  cinfo->entropy = (struct jpeg_entropy_decoder *)entropy;
661
741
  entropy->pub.start_pass = start_pass_phuff_decoder;
662
663
  /* Mark derived tables unallocated */
664
3.70k
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
665
2.96k
    entropy->derived_tbls[i] = NULL;
666
2.96k
  }
667
668
  /* Create progression status table */
669
741
  cinfo->coef_bits = (int (*)[DCTSIZE2])
670
741
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
671
741
                                cinfo->num_components * 2 * DCTSIZE2 *
672
741
                                sizeof(int));
673
741
  coef_bit_ptr = &cinfo->coef_bits[0][0];
674
2.39k
  for (ci = 0; ci < cinfo->num_components; ci++)
675
107k
    for (i = 0; i < DCTSIZE2; i++)
676
106k
      *coef_bit_ptr++ = -1;
677
741
}
678
679
#endif /* D_PROGRESSIVE_SUPPORTED */