Coverage Report

Created: 2024-05-04 12:45

/proc/self/cwd/external/libjpeg_turbo/jdphuff.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jdphuff.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1995-1997, Thomas G. Lane.
6
 * libjpeg-turbo Modifications:
7
 * Copyright (C) 2015-2016, 2018-2022, D. R. Commander.
8
 * For conditions of distribution and use, see the accompanying README.ijg
9
 * file.
10
 *
11
 * This file contains Huffman entropy decoding routines for progressive JPEG.
12
 *
13
 * Much of the complexity here has to do with supporting input suspension.
14
 * If the data source module demands suspension, we want to be able to back
15
 * up to the start of the current MCU.  To do this, we copy state variables
16
 * into local working storage, and update them back to the permanent
17
 * storage only upon successful completion of an MCU.
18
 *
19
 * NOTE: All referenced figures are from
20
 * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994.
21
 */
22
23
#define JPEG_INTERNALS
24
#include "jinclude.h"
25
#include "jpeglib.h"
26
#include "jdhuff.h"             /* Declarations shared with jdhuff.c */
27
#include <limits.h>
28
29
30
#ifdef D_PROGRESSIVE_SUPPORTED
31
32
/*
33
 * Expanded entropy decoder object for progressive Huffman decoding.
34
 *
35
 * The savable_state subrecord contains fields that change within an MCU,
36
 * but must not be updated permanently until we complete the MCU.
37
 */
38
39
typedef struct {
40
  unsigned int EOBRUN;                  /* remaining EOBs in EOBRUN */
41
  int last_dc_val[MAX_COMPS_IN_SCAN];   /* last DC coef for each component */
42
} savable_state;
43
44
typedef struct {
45
  struct jpeg_entropy_decoder pub; /* public fields */
46
47
  /* These fields are loaded into local variables at start of each MCU.
48
   * In case of suspension, we exit WITHOUT updating them.
49
   */
50
  bitread_perm_state bitstate;  /* Bit buffer at start of MCU */
51
  savable_state saved;          /* Other state at start of MCU */
52
53
  /* These fields are NOT loaded into local working state. */
54
  unsigned int restarts_to_go;  /* MCUs left in this restart interval */
55
56
  /* Pointers to derived tables (these workspaces have image lifespan) */
57
  d_derived_tbl *derived_tbls[NUM_HUFF_TBLS];
58
59
  d_derived_tbl *ac_derived_tbl; /* active table during an AC scan */
60
} phuff_entropy_decoder;
61
62
typedef phuff_entropy_decoder *phuff_entropy_ptr;
63
64
/* Forward declarations */
65
METHODDEF(boolean) decode_mcu_DC_first(j_decompress_ptr cinfo,
66
                                       JBLOCKROW *MCU_data);
67
METHODDEF(boolean) decode_mcu_AC_first(j_decompress_ptr cinfo,
68
                                       JBLOCKROW *MCU_data);
69
METHODDEF(boolean) decode_mcu_DC_refine(j_decompress_ptr cinfo,
70
                                        JBLOCKROW *MCU_data);
71
METHODDEF(boolean) decode_mcu_AC_refine(j_decompress_ptr cinfo,
72
                                        JBLOCKROW *MCU_data);
73
74
75
/*
76
 * Initialize for a Huffman-compressed scan.
77
 */
78
79
METHODDEF(void)
80
start_pass_phuff_decoder(j_decompress_ptr cinfo)
81
354
{
82
354
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
83
354
  boolean is_DC_band, bad;
84
354
  int ci, coefi, tbl;
85
354
  d_derived_tbl **pdtbl;
86
354
  int *coef_bit_ptr, *prev_coef_bit_ptr;
87
354
  jpeg_component_info *compptr;
88
89
354
  is_DC_band = (cinfo->Ss == 0);
90
91
  /* Validate scan parameters */
92
354
  bad = FALSE;
93
354
  if (is_DC_band) {
94
353
    if (cinfo->Se != 0)
95
0
      bad = TRUE;
96
353
  } else {
97
    /* need not check Ss/Se < 0 since they came from unsigned bytes */
98
1
    if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
99
0
      bad = TRUE;
100
    /* AC scans may have only one component */
101
1
    if (cinfo->comps_in_scan != 1)
102
0
      bad = TRUE;
103
1
  }
104
354
  if (cinfo->Ah != 0) {
105
    /* Successive approximation refinement scan: must have Al = Ah-1. */
106
353
    if (cinfo->Al != cinfo->Ah - 1)
107
0
      bad = TRUE;
108
353
  }
109
354
  if (cinfo->Al > 13)           /* need not check for < 0 */
110
0
    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
354
  if (bad)
118
0
    ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
119
354
             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
1.41k
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
125
1.06k
    int cindex = cinfo->cur_comp_info[ci]->component_index;
126
1.06k
    coef_bit_ptr = &cinfo->coef_bits[cindex][0];
127
1.06k
    prev_coef_bit_ptr = &cinfo->coef_bits[cindex + cinfo->num_components][0];
128
1.06k
    if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
129
1
      WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
130
11.6k
    for (coefi = MIN(cinfo->Ss, 1); coefi <= MAX(cinfo->Se, 9); coefi++) {
131
10.5k
      if (cinfo->input_scan_number > 1)
132
10.5k
        prev_coef_bit_ptr[coefi] = coef_bit_ptr[coefi];
133
39
      else
134
39
        prev_coef_bit_ptr[coefi] = 0;
135
10.5k
    }
136
2.12k
    for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
137
1.06k
      int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
138
1.06k
      if (cinfo->Ah != expected)
139
1.05k
        WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
140
1.06k
      coef_bit_ptr[coefi] = cinfo->Al;
141
1.06k
    }
142
1.06k
  }
143
144
  /* Select MCU decoding routine */
145
354
  if (cinfo->Ah == 0) {
146
1
    if (is_DC_band)
147
0
      entropy->pub.decode_mcu = decode_mcu_DC_first;
148
1
    else
149
1
      entropy->pub.decode_mcu = decode_mcu_AC_first;
150
353
  } else {
151
353
    if (is_DC_band)
152
353
      entropy->pub.decode_mcu = decode_mcu_DC_refine;
153
0
    else
154
0
      entropy->pub.decode_mcu = decode_mcu_AC_refine;
155
353
  }
156
157
1.41k
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
158
1.06k
    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
1.06k
    if (is_DC_band) {
163
1.05k
      if (cinfo->Ah == 0) {     /* DC refinement needs no table */
164
0
        tbl = compptr->dc_tbl_no;
165
0
        pdtbl = (d_derived_tbl **)(entropy->derived_tbls) + tbl;
166
0
        jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, pdtbl);
167
0
      }
168
1.05k
    } else {
169
1
      tbl = compptr->ac_tbl_no;
170
1
      pdtbl = (d_derived_tbl **)(entropy->derived_tbls) + tbl;
171
1
      jpeg_make_d_derived_tbl(cinfo, FALSE, tbl, pdtbl);
172
      /* remember the single active table */
173
1
      entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
174
1
    }
175
    /* Initialize DC predictions to 0 */
176
1.06k
    entropy->saved.last_dc_val[ci] = 0;
177
1.06k
  }
178
179
  /* Initialize bitread state variables */
180
354
  entropy->bitstate.bits_left = 0;
181
354
  entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
182
354
  entropy->pub.insufficient_data = FALSE;
183
184
  /* Initialize private state variables */
185
354
  entropy->saved.EOBRUN = 0;
186
187
  /* Initialize restart counter */
188
354
  entropy->restarts_to_go = cinfo->restart_interval;
189
354
}
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
0
#define NEG_1  ((unsigned)-1)
201
#define HUFF_EXTEND(x, s) \
202
0
  ((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
0
{
232
0
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
233
0
  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
0
  cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
238
0
  entropy->bitstate.bits_left = 0;
239
240
  /* Advance past the RSTn marker */
241
0
  if (!(*cinfo->marker->read_restart_marker) (cinfo))
242
0
    return FALSE;
243
244
  /* Re-initialize DC predictions to 0 */
245
0
  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
246
0
    entropy->saved.last_dc_val[ci] = 0;
247
  /* Re-init EOB run count, too */
248
0
  entropy->saved.EOBRUN = 0;
249
250
  /* Reset restart counter */
251
0
  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
0
  if (cinfo->unread_marker == 0)
259
0
    entropy->pub.insufficient_data = FALSE;
260
261
0
  return TRUE;
262
0
}
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
0
{
290
0
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
291
0
  int Al = cinfo->Al;
292
0
  register int s, r;
293
0
  int blkn, ci;
294
0
  JBLOCKROW block;
295
0
  BITREAD_STATE_VARS;
296
0
  savable_state state;
297
0
  d_derived_tbl *tbl;
298
0
  jpeg_component_info *compptr;
299
300
  /* Process restart marker if needed; may have to suspend */
301
0
  if (cinfo->restart_interval) {
302
0
    if (entropy->restarts_to_go == 0)
303
0
      if (!process_restart(cinfo))
304
0
        return FALSE;
305
0
  }
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
0
  if (!entropy->pub.insufficient_data) {
311
312
    /* Load up working state */
313
0
    BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
314
0
    state = entropy->saved;
315
316
    /* Outer loop handles each block in the MCU */
317
318
0
    for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
319
0
      block = MCU_data[blkn];
320
0
      ci = cinfo->MCU_membership[blkn];
321
0
      compptr = cinfo->cur_comp_info[ci];
322
0
      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
0
      HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
328
0
      if (s) {
329
0
        CHECK_BIT_BUFFER(br_state, s, return FALSE);
330
0
        r = GET_BITS(s);
331
0
        s = HUFF_EXTEND(r, s);
332
0
      }
333
334
      /* Convert DC difference to actual value, update last_dc_val */
335
0
      if ((state.last_dc_val[ci] >= 0 &&
336
0
           s > INT_MAX - state.last_dc_val[ci]) ||
337
0
          (state.last_dc_val[ci] < 0 && s < INT_MIN - state.last_dc_val[ci]))
338
0
        ERREXIT(cinfo, JERR_BAD_DCT_COEF);
339
0
      s += state.last_dc_val[ci];
340
0
      state.last_dc_val[ci] = s;
341
      /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
342
0
      (*block)[0] = (JCOEF)LEFT_SHIFT(s, Al);
343
0
    }
344
345
    /* Completed MCU, so update state */
346
0
    BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
347
0
    entropy->saved = state;
348
0
  }
349
350
  /* Account for restart interval (no-op if not using restarts) */
351
0
  if (cinfo->restart_interval)
352
0
    entropy->restarts_to_go--;
353
354
0
  return TRUE;
355
0
}
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
0
{
366
0
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
367
0
  int Se = cinfo->Se;
368
0
  int Al = cinfo->Al;
369
0
  register int s, k, r;
370
0
  unsigned int EOBRUN;
371
0
  JBLOCKROW block;
372
0
  BITREAD_STATE_VARS;
373
0
  d_derived_tbl *tbl;
374
375
  /* Process restart marker if needed; may have to suspend */
376
0
  if (cinfo->restart_interval) {
377
0
    if (entropy->restarts_to_go == 0)
378
0
      if (!process_restart(cinfo))
379
0
        return FALSE;
380
0
  }
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
0
  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
0
    EOBRUN = entropy->saved.EOBRUN;     /* only part of saved state we need */
391
392
    /* There is always only one block per MCU */
393
394
0
    if (EOBRUN > 0)             /* if it's a band of zeroes... */
395
0
      EOBRUN--;                 /* ...process it now (we do nothing) */
396
0
    else {
397
0
      BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
398
0
      block = MCU_data[0];
399
0
      tbl = entropy->ac_derived_tbl;
400
401
0
      for (k = cinfo->Ss; k <= Se; k++) {
402
0
        HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
403
0
        r = s >> 4;
404
0
        s &= 15;
405
0
        if (s) {
406
0
          k += r;
407
0
          CHECK_BIT_BUFFER(br_state, s, return FALSE);
408
0
          r = GET_BITS(s);
409
0
          s = HUFF_EXTEND(r, s);
410
          /* Scale and output coefficient in natural (dezigzagged) order */
411
0
          (*block)[jpeg_natural_order[k]] = (JCOEF)LEFT_SHIFT(s, Al);
412
0
        } else {
413
0
          if (r == 15) {        /* ZRL */
414
0
            k += 15;            /* skip 15 zeroes in band */
415
0
          } else {              /* EOBr, run length is 2^r + appended bits */
416
0
            EOBRUN = 1 << r;
417
0
            if (r) {            /* EOBr, r > 0 */
418
0
              CHECK_BIT_BUFFER(br_state, r, return FALSE);
419
0
              r = GET_BITS(r);
420
0
              EOBRUN += r;
421
0
            }
422
0
            EOBRUN--;           /* this band is processed at this moment */
423
0
            break;              /* force end-of-band */
424
0
          }
425
0
        }
426
0
      }
427
428
0
      BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
429
0
    }
430
431
    /* Completed MCU, so update state */
432
0
    entropy->saved.EOBRUN = EOBRUN;     /* only part of saved state we need */
433
0
  }
434
435
  /* Account for restart interval (no-op if not using restarts) */
436
0
  if (cinfo->restart_interval)
437
0
    entropy->restarts_to_go--;
438
439
0
  return TRUE;
440
0
}
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
1.20M
{
452
1.20M
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
453
1.20M
  int p1 = 1 << cinfo->Al;      /* 1 in the bit position being coded */
454
1.20M
  int blkn;
455
1.20M
  JBLOCKROW block;
456
1.20M
  BITREAD_STATE_VARS;
457
458
  /* Process restart marker if needed; may have to suspend */
459
1.20M
  if (cinfo->restart_interval) {
460
0
    if (entropy->restarts_to_go == 0)
461
0
      if (!process_restart(cinfo))
462
0
        return FALSE;
463
0
  }
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
1.20M
  BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
471
472
  /* Outer loop handles each block in the MCU */
473
474
13.2M
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
475
12.0M
    block = MCU_data[blkn];
476
477
    /* Encoded data is simply the next bit of the two's-complement DC value */
478
12.0M
    CHECK_BIT_BUFFER(br_state, 1, return FALSE);
479
12.0M
    if (GET_BITS(1))
480
2.69k
      (*block)[0] |= p1;
481
    /* Note: since we use |=, repeating the assignment later is safe */
482
12.0M
  }
483
484
  /* Completed MCU, so update state */
485
1.20M
  BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
486
487
  /* Account for restart interval (no-op if not using restarts) */
488
1.20M
  if (cinfo->restart_interval)
489
0
    entropy->restarts_to_go--;
490
491
1.20M
  return TRUE;
492
1.20M
}
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
0
{
502
0
  phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;
503
0
  int Se = cinfo->Se;
504
0
  int p1 = 1 << cinfo->Al;        /* 1 in the bit position being coded */
505
0
  int m1 = (NEG_1) << cinfo->Al;  /* -1 in the bit position being coded */
506
0
  register int s, k, r;
507
0
  unsigned int EOBRUN;
508
0
  JBLOCKROW block;
509
0
  JCOEFPTR thiscoef;
510
0
  BITREAD_STATE_VARS;
511
0
  d_derived_tbl *tbl;
512
0
  int num_newnz;
513
0
  int newnz_pos[DCTSIZE2];
514
515
  /* Process restart marker if needed; may have to suspend */
516
0
  if (cinfo->restart_interval) {
517
0
    if (entropy->restarts_to_go == 0)
518
0
      if (!process_restart(cinfo))
519
0
        return FALSE;
520
0
  }
521
522
  /* If we've run out of data, don't modify the MCU.
523
   */
524
0
  if (!entropy->pub.insufficient_data) {
525
526
    /* Load up working state */
527
0
    BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
528
0
    EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
529
530
    /* There is always only one block per MCU */
531
0
    block = MCU_data[0];
532
0
    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
0
    num_newnz = 0;
541
542
    /* initialize coefficient loop counter to start of band */
543
0
    k = cinfo->Ss;
544
545
0
    if (EOBRUN == 0) {
546
0
      for (; k <= Se; k++) {
547
0
        HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
548
0
        r = s >> 4;
549
0
        s &= 15;
550
0
        if (s) {
551
0
          if (s != 1)           /* size of new coef should always be 1 */
552
0
            WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
553
0
          CHECK_BIT_BUFFER(br_state, 1, goto undoit);
554
0
          if (GET_BITS(1))
555
0
            s = p1;             /* newly nonzero coef is positive */
556
0
          else
557
0
            s = m1;             /* newly nonzero coef is negative */
558
0
        } else {
559
0
          if (r != 15) {
560
0
            EOBRUN = 1 << r;    /* EOBr, run length is 2^r + appended bits */
561
0
            if (r) {
562
0
              CHECK_BIT_BUFFER(br_state, r, goto undoit);
563
0
              r = GET_BITS(r);
564
0
              EOBRUN += r;
565
0
            }
566
0
            break;              /* rest of block is handled by EOB logic */
567
0
          }
568
          /* note s = 0 for processing ZRL */
569
0
        }
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
0
        do {
575
0
          thiscoef = *block + jpeg_natural_order[k];
576
0
          if (*thiscoef != 0) {
577
0
            CHECK_BIT_BUFFER(br_state, 1, goto undoit);
578
0
            if (GET_BITS(1)) {
579
0
              if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
580
0
                if (*thiscoef >= 0)
581
0
                  *thiscoef += (JCOEF)p1;
582
0
                else
583
0
                  *thiscoef += (JCOEF)m1;
584
0
              }
585
0
            }
586
0
          } else {
587
0
            if (--r < 0)
588
0
              break;            /* reached target zero coefficient */
589
0
          }
590
0
          k++;
591
0
        } while (k <= Se);
592
0
        if (s) {
593
0
          int pos = jpeg_natural_order[k];
594
          /* Output newly nonzero coefficient */
595
0
          (*block)[pos] = (JCOEF)s;
596
          /* Remember its position in case we have to suspend */
597
0
          newnz_pos[num_newnz++] = pos;
598
0
        }
599
0
      }
600
0
    }
601
602
0
    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
0
      for (; k <= Se; k++) {
609
0
        thiscoef = *block + jpeg_natural_order[k];
610
0
        if (*thiscoef != 0) {
611
0
          CHECK_BIT_BUFFER(br_state, 1, goto undoit);
612
0
          if (GET_BITS(1)) {
613
0
            if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
614
0
              if (*thiscoef >= 0)
615
0
                *thiscoef += (JCOEF)p1;
616
0
              else
617
0
                *thiscoef += (JCOEF)m1;
618
0
            }
619
0
          }
620
0
        }
621
0
      }
622
      /* Count one block completed in EOB run */
623
0
      EOBRUN--;
624
0
    }
625
626
    /* Completed MCU, so update state */
627
0
    BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
628
0
    entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
629
0
  }
630
631
  /* Account for restart interval (no-op if not using restarts) */
632
0
  if (cinfo->restart_interval)
633
0
    entropy->restarts_to_go--;
634
635
0
  return TRUE;
636
637
0
undoit:
638
  /* Re-zero any output coefficients that we made newly nonzero */
639
0
  while (num_newnz > 0)
640
0
    (*block)[newnz_pos[--num_newnz]] = 0;
641
642
0
  return FALSE;
643
0
}
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
2
{
653
2
  phuff_entropy_ptr entropy;
654
2
  int *coef_bit_ptr;
655
2
  int ci, i;
656
657
2
  entropy = (phuff_entropy_ptr)
658
2
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
659
2
                                sizeof(phuff_entropy_decoder));
660
2
  cinfo->entropy = (struct jpeg_entropy_decoder *)entropy;
661
2
  entropy->pub.start_pass = start_pass_phuff_decoder;
662
663
  /* Mark derived tables unallocated */
664
10
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
665
8
    entropy->derived_tbls[i] = NULL;
666
8
  }
667
668
  /* Create progression status table */
669
2
  cinfo->coef_bits = (int (*)[DCTSIZE2])
670
2
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
671
2
                                cinfo->num_components * 2 * DCTSIZE2 *
672
2
                                sizeof(int));
673
2
  coef_bit_ptr = &cinfo->coef_bits[0][0];
674
6
  for (ci = 0; ci < cinfo->num_components; ci++)
675
260
    for (i = 0; i < DCTSIZE2; i++)
676
256
      *coef_bit_ptr++ = -1;
677
2
}
678
679
#endif /* D_PROGRESSIVE_SUPPORTED */