Coverage Report

Created: 2025-06-22 06:59

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