Coverage Report

Created: 2026-06-30 08:33

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