Coverage Report

Created: 2026-08-11 08:26

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
358k
#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
24.8k
{
97
24.8k
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
98
24.8k
  boolean is_DC_band, bad;
99
24.8k
  int ci, coefi, tbl;
100
24.8k
  int *coef_bit_ptr;
101
24.8k
  jpeg_component_info * compptr;
102
103
24.8k
  is_DC_band = (cinfo->Ss == 0);
104
105
  /* Validate scan parameters */
106
24.8k
  bad = FALSE;
107
24.8k
  if (is_DC_band) {
108
13.7k
    if (cinfo->Se != 0)
109
45
      bad = TRUE;
110
13.7k
  } else {
111
    /* need not check Ss/Se < 0 since they came from unsigned bytes */
112
11.0k
    if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
113
97
      bad = TRUE;
114
    /* AC scans may have only one component */
115
11.0k
    if (cinfo->comps_in_scan != 1)
116
66
      bad = TRUE;
117
11.0k
  }
118
24.8k
  if (cinfo->Ah != 0) {
119
    /* Successive approximation refinement scan: must have Al = Ah-1. */
120
11.0k
    if (cinfo->Al != cinfo->Ah-1)
121
146
      bad = TRUE;
122
11.0k
  }
123
24.8k
  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
24.8k
  if (bad)
132
195
    ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
133
24.8k
       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
70.3k
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
139
45.5k
    int cindex = cinfo->cur_comp_info[ci]->component_index;
140
45.5k
    coef_bit_ptr = & cinfo->coef_bits[cindex][0];
141
45.5k
    if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
142
7.27k
      WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
143
571k
    for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
144
525k
      int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
145
525k
      if (cinfo->Ah != expected)
146
428k
  WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
147
525k
      coef_bit_ptr[coefi] = cinfo->Al;
148
525k
    }
149
45.5k
  }
150
151
  /* Select MCU decoding routine */
152
24.8k
  if (cinfo->Ah == 0) {
153
13.7k
    if (is_DC_band)
154
9.67k
      entropy->pub.decode_mcu = decode_mcu_DC_first;
155
4.11k
    else
156
4.11k
      entropy->pub.decode_mcu = decode_mcu_AC_first;
157
13.7k
  } else {
158
11.0k
    if (is_DC_band)
159
3.99k
      entropy->pub.decode_mcu = decode_mcu_DC_refine;
160
7.03k
    else
161
7.03k
      entropy->pub.decode_mcu = decode_mcu_AC_refine;
162
11.0k
  }
163
164
70.2k
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
165
45.4k
    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
45.4k
    if (is_DC_band) {
170
34.5k
      if (cinfo->Ah == 0) { /* DC refinement needs no table */
171
25.2k
  tbl = compptr->dc_tbl_no;
172
25.2k
  if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
173
38
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
174
25.2k
  jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
175
25.2k
        & entropy->derived_tbls[tbl]);
176
25.2k
      }
177
34.5k
    } else {
178
10.9k
      tbl = compptr->ac_tbl_no;
179
10.9k
      if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
180
6
        ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
181
10.9k
      jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
182
10.9k
            & entropy->derived_tbls[tbl]);
183
      /* remember the single active table */
184
10.9k
      entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
185
10.9k
    }
186
    /* Initialize DC predictions to 0 */
187
45.4k
    entropy->saved.last_dc_val[ci] = 0;
188
45.4k
  }
189
190
  /* Initialize bitread state variables */
191
24.8k
  entropy->bitstate.bits_left = 0;
192
24.8k
  entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
193
24.8k
  entropy->pub.insufficient_data = FALSE;
194
195
  /* Initialize private state variables */
196
24.8k
  entropy->saved.EOBRUN = 0;
197
198
  /* Initialize restart counter */
199
24.8k
  entropy->restarts_to_go = cinfo->restart_interval;
200
24.8k
}
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
20.0M
#define NEG_1 ((unsigned)-1)
209
210
#define AVOID_TABLES
211
#ifdef AVOID_TABLES
212
213
695k
#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.64M
{
237
2.64M
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
238
2.64M
  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.64M
  cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
243
2.64M
  entropy->bitstate.bits_left = 0;
244
245
  /* Advance past the RSTn marker */
246
2.64M
  if (! (*cinfo->marker->read_restart_marker) (cinfo))
247
0
    return FALSE;
248
249
  /* Re-initialize DC predictions to 0 */
250
5.58M
  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
251
2.94M
    entropy->saved.last_dc_val[ci] = 0;
252
  /* Re-init EOB run count, too */
253
2.64M
  entropy->saved.EOBRUN = 0;
254
255
  /* Reset restart counter */
256
2.64M
  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.64M
  if (cinfo->unread_marker == 0)
264
18.1k
    entropy->pub.insufficient_data = FALSE;
265
266
2.64M
  return TRUE;
267
2.64M
}
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
8.84M
{   
295
8.84M
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
296
8.84M
  int Al = cinfo->Al;
297
8.84M
  register int s, r;
298
8.84M
  int blkn, ci;
299
8.84M
  JBLOCKROW block;
300
8.84M
  BITREAD_STATE_VARS;
301
8.84M
  savable_state state;
302
8.84M
  d_derived_tbl * tbl;
303
8.84M
  jpeg_component_info * compptr;
304
305
  /* Process restart marker if needed; may have to suspend */
306
8.84M
  if (cinfo->restart_interval) {
307
2.82M
    if (entropy->restarts_to_go == 0)
308
551k
      if (! process_restart(cinfo))
309
0
  return FALSE;
310
2.82M
  }
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
8.84M
  if (! entropy->pub.insufficient_data) {
316
317
    /* Load up working state */
318
179k
    BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
319
179k
    ASSIGN_STATE(state, entropy->saved);
320
321
    /* Outer loop handles each block in the MCU */
322
323
777k
    for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
324
597k
      block = MCU_data[blkn];
325
597k
      ci = cinfo->MCU_membership[blkn];
326
597k
      compptr = cinfo->cur_comp_info[ci];
327
597k
      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
597k
      HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
333
597k
      if (s) {
334
356k
  CHECK_BIT_BUFFER(br_state, s, return FALSE);
335
356k
  r = GET_BITS(s);
336
356k
  s = HUFF_EXTEND(r, s);
337
356k
      }
338
339
      /* Convert DC difference to actual value, update last_dc_val */
340
597k
      if( (state.last_dc_val[ci] >= 0 && s > INT_MAX - state.last_dc_val[ci]) ||
341
597k
          (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
597k
      s += state.last_dc_val[ci];
345
597k
      state.last_dc_val[ci] = s;
346
      /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
347
597k
      (*block)[0] = (JCOEF) LEFT_SHIFT(s, Al);
348
597k
    }
349
350
    /* Completed MCU, so update state */
351
179k
    BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
352
179k
    ASSIGN_STATE(entropy->saved, state);
353
179k
  }
354
355
  /* Account for restart interval (no-op if not using restarts) */
356
8.84M
  if( entropy->restarts_to_go == 0 )
357
6.99k
      entropy->restarts_to_go = ~0U;
358
8.83M
  else
359
8.83M
      entropy->restarts_to_go--;
360
361
8.84M
  return TRUE;
362
8.84M
}
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
11.4M
{   
373
11.4M
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
374
11.4M
  int Se = cinfo->Se;
375
11.4M
  int Al = cinfo->Al;
376
11.4M
  register int s, k, r;
377
11.4M
  unsigned int EOBRUN;
378
11.4M
  JBLOCKROW block;
379
11.4M
  BITREAD_STATE_VARS;
380
11.4M
  d_derived_tbl * tbl;
381
382
  /* Process restart marker if needed; may have to suspend */
383
11.4M
  if (cinfo->restart_interval) {
384
4.71M
    if (entropy->restarts_to_go == 0)
385
532k
      if (! process_restart(cinfo))
386
0
  return FALSE;
387
4.71M
  }
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
11.4M
  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
629k
    EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
398
399
    /* There is always only one block per MCU */
400
401
629k
    if (EOBRUN > 0)    /* if it's a band of zeroes... */
402
401k
      EOBRUN--;      /* ...process it now (we do nothing) */
403
227k
    else {
404
227k
      BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
405
227k
      block = MCU_data[0];
406
227k
      tbl = entropy->ac_derived_tbl;
407
408
574k
      for (k = cinfo->Ss; k <= Se; k++) {
409
547k
  HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
410
547k
  r = s >> 4;
411
547k
  s &= 15;
412
547k
  if (s) {
413
339k
    k += r;
414
339k
    CHECK_BIT_BUFFER(br_state, s, return FALSE);
415
339k
    r = GET_BITS(s);
416
339k
    s = HUFF_EXTEND(r, s);
417
    /* Scale and output coefficient in natural (dezigzagged) order */
418
339k
    (*block)[jpeg_natural_order[k]] = (JCOEF) LEFT_SHIFT(s, Al);
419
339k
  } else {
420
208k
    if (r == 15) { /* ZRL */
421
7.52k
      k += 15;    /* skip 15 zeroes in band */
422
201k
    } else {   /* EOBr, run length is 2^r + appended bits */
423
201k
      EOBRUN = 1 << r;
424
201k
      if (r) {   /* EOBr, r > 0 */
425
10.7k
        CHECK_BIT_BUFFER(br_state, r, return FALSE);
426
10.7k
        r = GET_BITS(r);
427
10.7k
        EOBRUN += r;
428
10.7k
      }
429
201k
      EOBRUN--;   /* this band is processed at this moment */
430
201k
      break;   /* force end-of-band */
431
201k
    }
432
208k
  }
433
547k
      }
434
435
227k
      BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
436
227k
    }
437
438
    /* Completed MCU, so update state */
439
629k
    entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
440
629k
  }
441
442
  /* Account for restart interval (no-op if not using restarts) */
443
11.4M
  if( entropy->restarts_to_go == 0 )
444
1.74k
      entropy->restarts_to_go = ~0U;
445
11.4M
  else
446
11.4M
      entropy->restarts_to_go--;
447
448
11.4M
  return TRUE;
449
11.4M
}
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
4.21M
{   
461
4.21M
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
462
4.21M
  int p1 = 1 << cinfo->Al;  /* 1 in the bit position being coded */
463
4.21M
  int blkn;
464
4.21M
  JBLOCKROW block;
465
4.21M
  BITREAD_STATE_VARS;
466
467
  /* Process restart marker if needed; may have to suspend */
468
4.21M
  if (cinfo->restart_interval) {
469
1.24M
    if (entropy->restarts_to_go == 0)
470
223k
      if (! process_restart(cinfo))
471
0
  return FALSE;
472
1.24M
  }
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
4.21M
  BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
480
481
  /* Outer loop handles each block in the MCU */
482
483
12.2M
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
484
8.02M
    block = MCU_data[blkn];
485
486
    /* Encoded data is simply the next bit of the two's-complement DC value */
487
8.02M
    CHECK_BIT_BUFFER(br_state, 1, return FALSE);
488
8.02M
    if (GET_BITS(1))
489
167k
      (*block)[0] |= p1;
490
    /* Note: since we use |=, repeating the assignment later is safe */
491
8.02M
  }
492
493
  /* Completed MCU, so update state */
494
4.21M
  BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
495
496
  /* Account for restart interval (no-op if not using restarts) */
497
4.21M
  if( entropy->restarts_to_go == 0 )
498
2.37k
      entropy->restarts_to_go = ~0U;
499
4.20M
  else
500
4.20M
      entropy->restarts_to_go--;
501
502
4.21M
  return TRUE;
503
4.21M
}
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
19.4M
{   
513
19.4M
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
514
19.4M
  int Se = cinfo->Se;
515
19.4M
  int p1 = 1 << cinfo->Al;  /* 1 in the bit position being coded */
516
19.4M
  int m1 = (NEG_1) << cinfo->Al; /* -1 in the bit position being coded */
517
19.4M
  register int s, k, r;
518
19.4M
  unsigned int EOBRUN;
519
19.4M
  JBLOCKROW block;
520
19.4M
  JCOEFPTR thiscoef;
521
19.4M
  BITREAD_STATE_VARS;
522
19.4M
  d_derived_tbl * tbl;
523
19.4M
  int num_newnz;
524
19.4M
  int newnz_pos[DCTSIZE2];
525
526
  /* Process restart marker if needed; may have to suspend */
527
19.4M
  if (cinfo->restart_interval) {
528
12.0M
    if (entropy->restarts_to_go == 0)
529
1.34M
      if (! process_restart(cinfo))
530
0
  return FALSE;
531
12.0M
  }
532
533
  /* If we've run out of data, don't modify the MCU.
534
   */
535
19.4M
  if (! entropy->pub.insufficient_data) {
536
537
    /* Load up working state */
538
797k
    BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
539
797k
    EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
540
541
    /* There is always only one block per MCU */
542
797k
    block = MCU_data[0];
543
797k
    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
797k
    num_newnz = 0;
552
553
    /* initialize coefficient loop counter to start of band */
554
797k
    k = cinfo->Ss;
555
556
797k
    if (EOBRUN == 0) {
557
826k
      for (; k <= Se; k++) {
558
725k
  HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
559
725k
  r = s >> 4;
560
725k
  s &= 15;
561
725k
  if (s) {
562
596k
    if (s != 1)    /* size of new coef should always be 1 */
563
467k
      WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
564
596k
    CHECK_BIT_BUFFER(br_state, 1, goto undoit);
565
596k
    if (GET_BITS(1))
566
91.8k
      s = p1;    /* newly nonzero coef is positive */
567
504k
    else
568
504k
      s = m1;   /* newly nonzero coef is negative */
569
596k
  } else {
570
129k
    if (r != 15) {
571
127k
      EOBRUN = 1 << r;  /* EOBr, run length is 2^r + appended bits */
572
127k
      if (r) {
573
11.6k
        CHECK_BIT_BUFFER(br_state, r, goto undoit);
574
11.6k
        r = GET_BITS(r);
575
11.6k
        EOBRUN += r;
576
11.6k
      }
577
127k
      break;   /* rest of block is handled by EOB logic */
578
127k
    }
579
    /* note s = 0 for processing ZRL */
580
129k
  }
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
4.51M
  do {
586
4.51M
    thiscoef = *block + jpeg_natural_order[k];
587
4.51M
    if (*thiscoef != 0) {
588
3.00M
      CHECK_BIT_BUFFER(br_state, 1, goto undoit);
589
3.00M
      if (GET_BITS(1)) {
590
419k
        if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
591
17.9k
    if (*thiscoef >= 0)
592
2.88k
      *thiscoef += p1;
593
15.0k
    else
594
15.0k
      *thiscoef += m1;
595
17.9k
        }
596
419k
      }
597
3.00M
    } else {
598
1.51M
      if (--r < 0)
599
513k
        break;   /* reached target zero coefficient */
600
1.51M
    }
601
4.00M
    k++;
602
4.00M
  } while (k <= Se);
603
598k
  if (s) {
604
596k
    int pos = jpeg_natural_order[k];
605
    /* Output newly nonzero coefficient */
606
596k
    (*block)[pos] = (JCOEF) s;
607
    /* Remember its position in case we have to suspend */
608
596k
    newnz_pos[num_newnz++] = pos;
609
596k
  }
610
598k
      }
611
228k
    }
612
613
797k
    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
28.4M
      for (; k <= Se; k++) {
620
27.7M
  thiscoef = *block + jpeg_natural_order[k];
621
27.7M
  if (*thiscoef != 0) {
622
1.08M
    CHECK_BIT_BUFFER(br_state, 1, goto undoit);
623
1.08M
    if (GET_BITS(1)) {
624
334k
      if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
625
23.1k
        if (*thiscoef >= 0)
626
4.45k
    *thiscoef += p1;
627
18.6k
        else
628
18.6k
    *thiscoef += m1;
629
23.1k
      }
630
334k
    }
631
1.08M
  }
632
27.7M
      }
633
      /* Count one block completed in EOB run */
634
697k
      EOBRUN--;
635
697k
    }
636
637
    /* Completed MCU, so update state */
638
797k
    BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
639
797k
    entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
640
797k
  }
641
642
  /* Account for restart interval (no-op if not using restarts) */
643
19.4M
  if( entropy->restarts_to_go == 0 )
644
2.93k
      entropy->restarts_to_go = ~0U;
645
19.4M
  else
646
19.4M
      entropy->restarts_to_go--;
647
648
19.4M
  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
19.4M
}
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.59k
{
666
2.59k
  phuff_entropy_ptr entropy;
667
2.59k
  int *coef_bit_ptr;
668
2.59k
  int ci, i;
669
670
2.59k
  entropy = (phuff_entropy_ptr)
671
2.59k
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
672
2.59k
        SIZEOF(phuff_entropy_decoder));
673
2.59k
  cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
674
2.59k
  entropy->pub.start_pass = start_pass_phuff_decoder;
675
676
  /* Mark derived tables unallocated */
677
12.9k
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
678
10.3k
    entropy->derived_tbls[i] = NULL;
679
10.3k
  }
680
681
  /* Create progression status table */
682
2.59k
  cinfo->coef_bits = (int (*)[DCTSIZE2])
683
2.59k
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
684
2.59k
        cinfo->num_components*DCTSIZE2*SIZEOF(int));
685
2.59k
  coef_bit_ptr = & cinfo->coef_bits[0][0];
686
6.61k
  for (ci = 0; ci < cinfo->num_components; ci++) 
687
261k
    for (i = 0; i < DCTSIZE2; i++)
688
257k
      *coef_bit_ptr++ = -1;
689
2.59k
}
690
691
#endif /* D_PROGRESSIVE_SUPPORTED */