Coverage Report

Created: 2025-01-28 06:17

/src/mupdf/thirdparty/libjpeg/jdarith.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jdarith.c
3
 *
4
 * Developed 1997-2020 by Guido Vollbeding.
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 portable arithmetic entropy decoding routines for JPEG
9
 * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81).
10
 *
11
 * Both sequential and progressive modes are supported in this single module.
12
 *
13
 * Suspension is not currently supported in this module.
14
 */
15
16
#define JPEG_INTERNALS
17
#include "jinclude.h"
18
#include "jpeglib.h"
19
20
21
/* Expanded entropy decoder object for arithmetic decoding. */
22
23
typedef struct {
24
  struct jpeg_entropy_decoder pub; /* public fields */
25
26
  INT32 c;       /* C register, base of coding interval + input bit buffer */
27
  INT32 a;               /* A register, normalized size of coding interval */
28
  int ct;     /* bit shift counter, # of bits left in bit buffer part of C */
29
                                                         /* init: ct = -16 */
30
                                                         /* run: ct = 0..7 */
31
                                                         /* error: ct = -1 */
32
  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
33
  int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */
34
35
  unsigned int restarts_to_go;  /* MCUs left in this restart interval */
36
37
  /* Pointers to statistics areas (these workspaces have image lifespan) */
38
  unsigned char * dc_stats[NUM_ARITH_TBLS];
39
  unsigned char * ac_stats[NUM_ARITH_TBLS];
40
41
  /* Statistics bin for coding with fixed probability 0.5 */
42
  unsigned char fixed_bin[4];
43
} arith_entropy_decoder;
44
45
typedef arith_entropy_decoder * arith_entropy_ptr;
46
47
/* The following two definitions specify the allocation chunk size
48
 * for the statistics area.
49
 * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least
50
 * 49 statistics bins for DC, and 245 statistics bins for AC coding.
51
 *
52
 * We use a compact representation with 1 byte per statistics bin,
53
 * thus the numbers directly represent byte sizes.
54
 * This 1 byte per statistics bin contains the meaning of the MPS
55
 * (more probable symbol) in the highest bit (mask 0x80), and the
56
 * index into the probability estimation state machine table
57
 * in the lower bits (mask 0x7F).
58
 */
59
60
291
#define DC_STAT_BINS 64
61
289
#define AC_STAT_BINS 256
62
63
64
LOCAL(int)
65
get_byte (j_decompress_ptr cinfo)
66
/* Read next input byte; we do not support suspension in this module. */
67
42.4k
{
68
42.4k
  struct jpeg_source_mgr * src = cinfo->src;
69
70
42.4k
  if (src->bytes_in_buffer == 0)
71
164
    if (! (*src->fill_input_buffer) (cinfo))
72
0
      ERREXIT(cinfo, JERR_CANT_SUSPEND);
73
42.4k
  src->bytes_in_buffer--;
74
42.4k
  return GETJOCTET(*src->next_input_byte++);
75
42.4k
}
76
77
78
/*
79
 * The core arithmetic decoding routine (common in JPEG and JBIG).
80
 * This needs to go as fast as possible.
81
 * Machine-dependent optimization facilities
82
 * are not utilized in this portable implementation.
83
 * However, this code should be fairly efficient and
84
 * may be a good base for further optimizations anyway.
85
 *
86
 * Return value is 0 or 1 (binary decision).
87
 *
88
 * Note: I've changed the handling of the code base & bit
89
 * buffer register C compared to other implementations
90
 * based on the standards layout & procedures.
91
 * While it also contains both the actual base of the
92
 * coding interval (16 bits) and the next-bits buffer,
93
 * the cut-point between these two parts is floating
94
 * (instead of fixed) with the bit shift counter CT.
95
 * Thus, we also need only one (variable instead of
96
 * fixed size) shift for the LPS/MPS decision, and
97
 * we can do away with any renormalization update
98
 * of C (except for new data insertion, of course).
99
 *
100
 * I've also introduced a new scheme for accessing
101
 * the probability estimation state machine table,
102
 * derived from Markus Kuhn's JBIG implementation.
103
 */
104
105
LOCAL(int)
106
arith_decode (j_decompress_ptr cinfo, unsigned char *st)
107
91.0M
{
108
91.0M
  register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
109
91.0M
  register unsigned char nl, nm;
110
91.0M
  register INT32 qe, temp;
111
91.0M
  register int sv, data;
112
113
  /* Renormalization & data input per section D.2.6 */
114
94.9M
  while (e->a < 0x8000L) {
115
3.97M
    if (--e->ct < 0) {
116
      /* Need to fetch next data byte */
117
542k
      if (cinfo->unread_marker)
118
501k
  data = 0;    /* stuff zero data */
119
41.0k
      else {
120
41.0k
  data = get_byte(cinfo); /* read next input byte */
121
41.0k
  if (data == 0xFF) { /* zero stuff or marker code */
122
1.36k
    do data = get_byte(cinfo);
123
1.36k
    while (data == 0xFF);  /* swallow extra 0xFF bytes */
124
996
    if (data == 0)
125
124
      data = 0xFF;  /* discard stuffed zero byte */
126
872
    else {
127
      /* Note: Different from the Huffman decoder, hitting
128
       * a marker while processing the compressed data
129
       * segment is legal in arithmetic coding.
130
       * The convention is to supply zero data
131
       * then until decoding is complete.
132
       */
133
872
      cinfo->unread_marker = data;
134
872
      data = 0;
135
872
    }
136
996
  }
137
41.0k
      }
138
542k
      e->c = (e->c << 8) | data; /* insert data into C register */
139
542k
      if ((e->ct += 8) < 0)   /* update bit shift counter */
140
  /* Need more initial bytes */
141
39.2k
  if (++e->ct == 0)
142
    /* Got 2 initial bytes -> re-init A and exit loop */
143
19.6k
    e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */
144
542k
    }
145
3.97M
    e->a <<= 1;
146
3.97M
  }
147
148
  /* Fetch values from our compact representation of Table D.3(D.2):
149
   * Qe values and probability estimation state machine
150
   */
151
91.0M
  sv = *st;
152
91.0M
  qe = jpeg_aritab[sv & 0x7F];  /* => Qe_Value */
153
91.0M
  nl = (unsigned char)(qe & 0xFF); qe >>= 8;  /* Next_Index_LPS + Switch_MPS */
154
91.0M
  nm = (unsigned char)(qe & 0xFF); qe >>= 8;  /* Next_Index_MPS */
155
156
  /* Decode & estimation procedures per sections D.2.4 & D.2.5 */
157
91.0M
  temp = e->a - qe;
158
91.0M
  e->a = temp;
159
91.0M
  temp <<= e->ct;
160
91.0M
  if (e->c >= temp) {
161
738k
    e->c -= temp;
162
    /* Conditional LPS (less probable symbol) exchange */
163
738k
    if (e->a < qe) {
164
197k
      e->a = qe;
165
197k
      *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
166
540k
    } else {
167
540k
      e->a = qe;
168
540k
      *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
169
540k
      sv ^= 0x80;   /* Exchange LPS/MPS */
170
540k
    }
171
90.2M
  } else if (e->a < 0x8000L) {
172
    /* Conditional MPS (more probable symbol) exchange */
173
2.17M
    if (e->a < qe) {
174
1.00M
      *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
175
1.00M
      sv ^= 0x80;   /* Exchange LPS/MPS */
176
1.16M
    } else {
177
1.16M
      *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
178
1.16M
    }
179
2.17M
  }
180
181
91.0M
  return sv >> 7;
182
91.0M
}
183
184
185
/*
186
 * Check for a restart marker & resynchronize decoder.
187
 */
188
189
LOCAL(void)
190
process_restart (j_decompress_ptr cinfo)
191
18.8k
{
192
18.8k
  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
193
18.8k
  int ci;
194
18.8k
  jpeg_component_info * compptr;
195
196
  /* Advance past the RSTn marker */
197
18.8k
  if (! (*cinfo->marker->read_restart_marker) (cinfo))
198
0
    ERREXIT(cinfo, JERR_CANT_SUSPEND);
199
200
  /* Re-initialize statistics areas */
201
64.3k
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
202
45.5k
    compptr = cinfo->cur_comp_info[ci];
203
45.5k
    if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
204
39.1k
      MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS);
205
      /* Reset DC predictions to 0 */
206
39.1k
      entropy->last_dc_val[ci] = 0;
207
39.1k
      entropy->dc_context[ci] = 0;
208
39.1k
    }
209
45.5k
    if ((! cinfo->progressive_mode && cinfo->lim_Se) ||
210
45.5k
  (cinfo->progressive_mode && cinfo->Ss)) {
211
6.34k
      MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
212
6.34k
    }
213
45.5k
  }
214
215
  /* Reset arithmetic decoding variables */
216
18.8k
  entropy->c = 0;
217
18.8k
  entropy->a = 0;
218
18.8k
  entropy->ct = -16;  /* force reading 2 initial bytes to fill C */
219
220
  /* Reset restart counter */
221
18.8k
  entropy->restarts_to_go = cinfo->restart_interval;
222
18.8k
}
223
224
225
/*
226
 * Arithmetic MCU decoding.
227
 * Each of these routines decodes and returns one MCU's worth of
228
 * arithmetic-compressed coefficients.
229
 * The coefficients are reordered from zigzag order into natural array order,
230
 * but are not dequantized.
231
 *
232
 * The i'th block of the MCU is stored into the block pointed to by
233
 * MCU_data[i].  WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
234
 */
235
236
/*
237
 * MCU decoding for DC initial scan (either spectral selection,
238
 * or first pass of successive approximation).
239
 */
240
241
METHODDEF(boolean)
242
decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKARRAY MCU_data)
243
2.27M
{
244
2.27M
  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
245
2.27M
  JBLOCKROW block;
246
2.27M
  unsigned char *st;
247
2.27M
  int blkn, ci, tbl, sign;
248
2.27M
  int v, m;
249
250
  /* Process restart marker if needed */
251
2.27M
  if (cinfo->restart_interval) {
252
16.9k
    if (entropy->restarts_to_go == 0)
253
34
      process_restart(cinfo);
254
16.9k
    entropy->restarts_to_go--;
255
16.9k
  }
256
257
2.27M
  if (entropy->ct == -1) return TRUE;  /* if error do nothing */
258
259
  /* Outer loop handles each block in the MCU */
260
261
4.70M
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
262
2.46M
    block = MCU_data[blkn];
263
2.46M
    ci = cinfo->MCU_membership[blkn];
264
2.46M
    tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
265
266
    /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
267
268
    /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
269
2.46M
    st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
270
271
    /* Figure F.19: Decode_DC_DIFF */
272
2.46M
    if (arith_decode(cinfo, st) == 0)
273
1.13M
      entropy->dc_context[ci] = 0;
274
1.33M
    else {
275
      /* Figure F.21: Decoding nonzero value v */
276
      /* Figure F.22: Decoding the sign of v */
277
1.33M
      sign = arith_decode(cinfo, st + 1);
278
1.33M
      st += 2; st += sign;
279
      /* Figure F.23: Decoding the magnitude category of v */
280
1.33M
      if ((m = arith_decode(cinfo, st)) != 0) {
281
1.09M
  st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
282
2.58M
  while (arith_decode(cinfo, st)) {
283
1.48M
    if ((m <<= 1) == (int) 0x8000U) {
284
20
      WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
285
20
      entropy->ct = -1;     /* magnitude overflow */
286
20
      return TRUE;
287
20
    }
288
1.48M
    st += 1;
289
1.48M
  }
290
1.09M
      }
291
      /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
292
1.33M
      if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
293
46
  entropy->dc_context[ci] = 0;       /* zero diff category */
294
1.33M
      else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
295
1.08M
  entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
296
244k
      else
297
244k
  entropy->dc_context[ci] = 4 + (sign * 4);  /* small diff category */
298
1.33M
      v = m;
299
      /* Figure F.24: Decoding the magnitude bit pattern of v */
300
1.33M
      st += 14;
301
2.81M
      while (m >>= 1)
302
1.48M
  if (arith_decode(cinfo, st)) v |= m;
303
1.33M
      v += 1; if (sign) v = -v;
304
1.33M
      entropy->last_dc_val[ci] += v;
305
1.33M
    }
306
307
    /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */
308
2.46M
    (*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al);
309
2.46M
  }
310
311
2.24M
  return TRUE;
312
2.24M
}
313
314
315
/*
316
 * MCU decoding for AC initial scan (either spectral selection,
317
 * or first pass of successive approximation).
318
 */
319
320
METHODDEF(boolean)
321
decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKARRAY MCU_data)
322
1.74M
{
323
1.74M
  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
324
1.74M
  JBLOCKROW block;
325
1.74M
  unsigned char *st;
326
1.74M
  int tbl, sign, k;
327
1.74M
  int v, m;
328
1.74M
  const int * natural_order;
329
330
  /* Process restart marker if needed */
331
1.74M
  if (cinfo->restart_interval) {
332
23.1k
    if (entropy->restarts_to_go == 0)
333
4.79k
      process_restart(cinfo);
334
23.1k
    entropy->restarts_to_go--;
335
23.1k
  }
336
337
1.74M
  if (entropy->ct == -1) return TRUE;  /* if error do nothing */
338
339
374k
  natural_order = cinfo->natural_order;
340
341
  /* There is always only one block per MCU */
342
374k
  block = MCU_data[0];
343
374k
  tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
344
345
  /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
346
347
  /* Figure F.20: Decode_AC_coefficients */
348
374k
  k = cinfo->Ss - 1;
349
673k
  do {
350
673k
    st = entropy->ac_stats[tbl] + 3 * k;
351
673k
    if (arith_decode(cinfo, st)) break;    /* EOB flag */
352
484k
    for (;;) {
353
484k
      k++;
354
484k
      if (arith_decode(cinfo, st + 1)) break;
355
182k
      st += 3;
356
182k
      if (k >= cinfo->Se) {
357
44
  WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
358
44
  entropy->ct = -1;     /* spectral overflow */
359
44
  return TRUE;
360
44
      }
361
182k
    }
362
    /* Figure F.21: Decoding nonzero value v */
363
    /* Figure F.22: Decoding the sign of v */
364
302k
    sign = arith_decode(cinfo, entropy->fixed_bin);
365
302k
    st += 2;
366
    /* Figure F.23: Decoding the magnitude category of v */
367
302k
    if ((m = arith_decode(cinfo, st)) != 0) {
368
125k
      if (arith_decode(cinfo, st)) {
369
118k
  m <<= 1;
370
118k
  st = entropy->ac_stats[tbl] +
371
118k
       (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
372
156k
  while (arith_decode(cinfo, st)) {
373
38.0k
    if ((m <<= 1) == (int) 0x8000U) {
374
46
      WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
375
46
      entropy->ct = -1;     /* magnitude overflow */
376
46
      return TRUE;
377
46
    }
378
38.0k
    st += 1;
379
38.0k
  }
380
118k
      }
381
125k
    }
382
302k
    v = m;
383
    /* Figure F.24: Decoding the magnitude bit pattern of v */
384
302k
    st += 14;
385
457k
    while (m >>= 1)
386
155k
      if (arith_decode(cinfo, st)) v |= m;
387
302k
    v += 1; if (sign) v = -v;
388
    /* Scale and output coefficient in natural (dezigzagged) order */
389
302k
    (*block)[natural_order[k]] = (JCOEF) (v << cinfo->Al);
390
302k
  } while (k < cinfo->Se);
391
392
374k
  return TRUE;
393
374k
}
394
395
396
/*
397
 * MCU decoding for DC successive approximation refinement scan.
398
 * Note: we assume such scans can be multi-component,
399
 * although the spec is not very clear on the point.
400
 */
401
402
METHODDEF(boolean)
403
decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKARRAY MCU_data)
404
76.2k
{
405
76.2k
  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
406
76.2k
  unsigned char *st;
407
76.2k
  JCOEF p1;
408
76.2k
  int blkn;
409
410
  /* Process restart marker if needed */
411
76.2k
  if (cinfo->restart_interval) {
412
47
    if (entropy->restarts_to_go == 0)
413
2
      process_restart(cinfo);
414
47
    entropy->restarts_to_go--;
415
47
  }
416
417
76.2k
  st = entropy->fixed_bin;  /* use fixed probability estimation */
418
76.2k
  p1 = 1 << cinfo->Al;    /* 1 in the bit position being coded */
419
420
  /* Outer loop handles each block in the MCU */
421
422
400k
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
423
    /* Encoded data is simply the next bit of the two's-complement DC value */
424
323k
    if (arith_decode(cinfo, st))
425
175k
      MCU_data[blkn][0][0] |= p1;
426
323k
  }
427
428
76.2k
  return TRUE;
429
76.2k
}
430
431
432
/*
433
 * MCU decoding for AC successive approximation refinement scan.
434
 */
435
436
METHODDEF(boolean)
437
decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKARRAY MCU_data)
438
201k
{
439
201k
  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
440
201k
  JBLOCKROW block;
441
201k
  JCOEFPTR thiscoef;
442
201k
  unsigned char *st;
443
201k
  int tbl, k, kex;
444
201k
  JCOEF p1, m1;
445
201k
  const int * natural_order;
446
447
  /* Process restart marker if needed */
448
201k
  if (cinfo->restart_interval) {
449
127k
    if (entropy->restarts_to_go == 0)
450
1.54k
      process_restart(cinfo);
451
127k
    entropy->restarts_to_go--;
452
127k
  }
453
454
201k
  if (entropy->ct == -1) return TRUE;  /* if error do nothing */
455
456
88.1k
  natural_order = cinfo->natural_order;
457
458
  /* There is always only one block per MCU */
459
88.1k
  block = MCU_data[0];
460
88.1k
  tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
461
462
88.1k
  p1 = 1 << cinfo->Al;    /* 1 in the bit position being coded */
463
88.1k
  m1 = -p1;     /* -1 in the bit position being coded */
464
465
  /* Establish EOBx (previous stage end-of-block) index */
466
88.1k
  kex = cinfo->Se;
467
2.06M
  do {
468
2.06M
    if ((*block)[natural_order[kex]]) break;
469
2.06M
  } while (--kex);
470
471
0
  k = cinfo->Ss - 1;
472
129k
  do {
473
129k
    st = entropy->ac_stats[tbl] + 3 * k;
474
129k
    if (k >= kex)
475
116k
      if (arith_decode(cinfo, st)) break;  /* EOB flag */
476
79.9k
    for (;;) {
477
79.9k
      thiscoef = *block + natural_order[++k];
478
79.9k
      if (*thiscoef) {       /* previously nonzero coef */
479
2.98k
  if (arith_decode(cinfo, st + 2)) {
480
2.94k
    if (*thiscoef < 0)
481
2.58k
      *thiscoef += m1;
482
363
    else
483
363
      *thiscoef += p1;
484
2.94k
  }
485
2.98k
  break;
486
2.98k
      }
487
76.9k
      if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */
488
38.8k
  if (arith_decode(cinfo, entropy->fixed_bin))
489
20.2k
    *thiscoef = m1;
490
18.5k
  else
491
18.5k
    *thiscoef = p1;
492
38.8k
  break;
493
38.8k
      }
494
38.1k
      st += 3;
495
38.1k
      if (k >= cinfo->Se) {
496
37
  WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
497
37
  entropy->ct = -1;     /* spectral overflow */
498
37
  return TRUE;
499
37
      }
500
38.1k
    }
501
41.8k
  } while (k < cinfo->Se);
502
503
88.1k
  return TRUE;
504
88.1k
}
505
506
507
/*
508
 * Decode one MCU's worth of arithmetic-compressed coefficients.
509
 */
510
511
METHODDEF(boolean)
512
decode_mcu (j_decompress_ptr cinfo, JBLOCKARRAY MCU_data)
513
20.3M
{
514
20.3M
  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
515
20.3M
  jpeg_component_info * compptr;
516
20.3M
  JBLOCKROW block;
517
20.3M
  unsigned char *st;
518
20.3M
  int blkn, ci, tbl, sign, k;
519
20.3M
  int v, m;
520
20.3M
  const int * natural_order;
521
522
  /* Process restart marker if needed */
523
20.3M
  if (cinfo->restart_interval) {
524
139k
    if (entropy->restarts_to_go == 0)
525
12.4k
      process_restart(cinfo);
526
139k
    entropy->restarts_to_go--;
527
139k
  }
528
529
20.3M
  if (entropy->ct == -1) return TRUE;  /* if error do nothing */
530
531
20.0M
  natural_order = cinfo->natural_order;
532
533
  /* Outer loop handles each block in the MCU */
534
535
42.5M
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
536
22.4M
    block = MCU_data[blkn];
537
22.4M
    ci = cinfo->MCU_membership[blkn];
538
22.4M
    compptr = cinfo->cur_comp_info[ci];
539
540
    /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
541
542
22.4M
    tbl = compptr->dc_tbl_no;
543
544
    /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
545
22.4M
    st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
546
547
    /* Figure F.19: Decode_DC_DIFF */
548
22.4M
    if (arith_decode(cinfo, st) == 0)
549
1.57M
      entropy->dc_context[ci] = 0;
550
20.8M
    else {
551
      /* Figure F.21: Decoding nonzero value v */
552
      /* Figure F.22: Decoding the sign of v */
553
20.8M
      sign = arith_decode(cinfo, st + 1);
554
20.8M
      st += 2; st += sign;
555
      /* Figure F.23: Decoding the magnitude category of v */
556
20.8M
      if ((m = arith_decode(cinfo, st)) != 0) {
557
920k
  st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
558
2.53M
  while (arith_decode(cinfo, st)) {
559
1.61M
    if ((m <<= 1) == (int) 0x8000U) {
560
38
      WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
561
38
      entropy->ct = -1;     /* magnitude overflow */
562
38
      return TRUE;
563
38
    }
564
1.61M
    st += 1;
565
1.61M
  }
566
920k
      }
567
      /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
568
20.8M
      if (m < (int) ((1L << cinfo->arith_dc_L[tbl]) >> 1))
569
69.3k
  entropy->dc_context[ci] = 0;       /* zero diff category */
570
20.8M
      else if (m > (int) ((1L << cinfo->arith_dc_U[tbl]) >> 1))
571
496k
  entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
572
20.3M
      else
573
20.3M
  entropy->dc_context[ci] = 4 + (sign * 4);  /* small diff category */
574
20.8M
      v = m;
575
      /* Figure F.24: Decoding the magnitude bit pattern of v */
576
20.8M
      st += 14;
577
22.4M
      while (m >>= 1)
578
1.61M
  if (arith_decode(cinfo, st)) v |= m;
579
20.8M
      v += 1; if (sign) v = -v;
580
20.8M
      entropy->last_dc_val[ci] += v;
581
20.8M
    }
582
583
22.4M
    (*block)[0] = (JCOEF) entropy->last_dc_val[ci];
584
585
    /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
586
587
22.4M
    if (cinfo->lim_Se == 0) continue;
588
706k
    tbl = compptr->ac_tbl_no;
589
706k
    k = 0;
590
591
    /* Figure F.20: Decode_AC_coefficients */
592
1.73M
    do {
593
1.73M
      st = entropy->ac_stats[tbl] + 3 * k;
594
1.73M
      if (arith_decode(cinfo, st)) break;  /* EOB flag */
595
4.14M
      for (;;) {
596
4.14M
  k++;
597
4.14M
  if (arith_decode(cinfo, st + 1)) break;
598
3.12M
  st += 3;
599
3.12M
  if (k >= cinfo->lim_Se) {
600
11
    WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
601
11
    entropy->ct = -1;     /* spectral overflow */
602
11
    return TRUE;
603
11
  }
604
3.12M
      }
605
      /* Figure F.21: Decoding nonzero value v */
606
      /* Figure F.22: Decoding the sign of v */
607
1.02M
      sign = arith_decode(cinfo, entropy->fixed_bin);
608
1.02M
      st += 2;
609
      /* Figure F.23: Decoding the magnitude category of v */
610
1.02M
      if ((m = arith_decode(cinfo, st)) != 0) {
611
853k
  if (arith_decode(cinfo, st)) {
612
821k
    m <<= 1;
613
821k
    st = entropy->ac_stats[tbl] +
614
821k
         (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
615
956k
    while (arith_decode(cinfo, st)) {
616
134k
      if ((m <<= 1) == (int) 0x8000U) {
617
5
        WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
618
5
        entropy->ct = -1;     /* magnitude overflow */
619
5
        return TRUE;
620
5
      }
621
134k
      st += 1;
622
134k
    }
623
821k
  }
624
853k
      }
625
1.02M
      v = m;
626
      /* Figure F.24: Decoding the magnitude bit pattern of v */
627
1.02M
      st += 14;
628
1.98M
      while (m >>= 1)
629
956k
  if (arith_decode(cinfo, st)) v |= m;
630
1.02M
      v += 1; if (sign) v = -v;
631
1.02M
      (*block)[natural_order[k]] = (JCOEF) v;
632
1.02M
    } while (k < cinfo->lim_Se);
633
706k
  }
634
635
20.0M
  return TRUE;
636
20.0M
}
637
638
639
/*
640
 * Initialize for an arithmetic-compressed scan.
641
 */
642
643
METHODDEF(void)
644
start_pass (j_decompress_ptr cinfo)
645
787
{
646
787
  arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
647
787
  int ci, tbl;
648
787
  jpeg_component_info * compptr;
649
650
787
  if (cinfo->progressive_mode) {
651
    /* Validate progressive scan parameters */
652
333
    if (cinfo->Ss == 0) {
653
182
      if (cinfo->Se != 0)
654
1
  goto bad;
655
182
    } else {
656
      /* need not check Ss/Se < 0 since they came from unsigned bytes */
657
151
      if (cinfo->Se < cinfo->Ss || cinfo->Se > cinfo->lim_Se)
658
5
  goto bad;
659
      /* AC scans may have only one component */
660
146
      if (cinfo->comps_in_scan != 1)
661
0
  goto bad;
662
146
    }
663
327
    if (cinfo->Ah != 0) {
664
      /* Successive approximation refinement scan: must have Al = Ah-1. */
665
124
      if (cinfo->Ah-1 != cinfo->Al)
666
4
  goto bad;
667
124
    }
668
323
    if (cinfo->Al > 13) { /* need not check for < 0 */
669
11
      bad:
670
11
      ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
671
11
         cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
672
11
    }
673
    /* Update progression status, and verify that scan order is legal.
674
     * Note that inter-scan inconsistencies are treated as warnings
675
     * not fatal errors ... not clear if this is right way to behave.
676
     */
677
747
    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
678
414
      int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;
679
414
      int *coef_bit_ptr = & cinfo->coef_bits[cindex][0];
680
414
      if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
681
83
  WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
682
2.69k
      for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
683
2.28k
  int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
684
2.28k
  if (cinfo->Ah != expected)
685
745
    WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
686
2.28k
  coef_bit_ptr[coefi] = cinfo->Al;
687
2.28k
      }
688
414
    }
689
    /* Select MCU decoding routine */
690
333
    if (cinfo->Ah == 0) {
691
202
      if (cinfo->Ss == 0)
692
105
  entropy->pub.decode_mcu = decode_mcu_DC_first;
693
97
      else
694
97
  entropy->pub.decode_mcu = decode_mcu_AC_first;
695
202
    } else {
696
131
      if (cinfo->Ss == 0)
697
71
  entropy->pub.decode_mcu = decode_mcu_DC_refine;
698
60
      else
699
60
  entropy->pub.decode_mcu = decode_mcu_AC_refine;
700
131
    }
701
454
  } else {
702
    /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
703
     * This ought to be an error condition, but we make it a warning.
704
     */
705
454
    if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 ||
706
454
  (cinfo->Se < DCTSIZE2 && cinfo->Se != cinfo->lim_Se))
707
425
      WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
708
    /* Select MCU decoding routine */
709
454
    entropy->pub.decode_mcu = decode_mcu;
710
454
  }
711
712
  /* Allocate & initialize requested statistics areas */
713
2.14k
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
714
1.36k
    compptr = cinfo->cur_comp_info[ci];
715
1.36k
    if (! cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
716
1.06k
      tbl = compptr->dc_tbl_no;
717
1.06k
      if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
718
0
  ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
719
1.06k
      if (entropy->dc_stats[tbl] == NULL)
720
291
  entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
721
291
    ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);
722
1.06k
      MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
723
      /* Initialize DC predictions to 0 */
724
1.06k
      entropy->last_dc_val[ci] = 0;
725
1.06k
      entropy->dc_context[ci] = 0;
726
1.06k
    }
727
1.36k
    if ((! cinfo->progressive_mode && cinfo->lim_Se) ||
728
1.36k
  (cinfo->progressive_mode && cinfo->Ss)) {
729
376
      tbl = compptr->ac_tbl_no;
730
376
      if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
731
0
  ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
732
376
      if (entropy->ac_stats[tbl] == NULL)
733
289
  entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
734
289
    ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);
735
376
      MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
736
376
    }
737
1.36k
  }
738
739
  /* Initialize arithmetic decoding variables */
740
787
  entropy->c = 0;
741
787
  entropy->a = 0;
742
787
  entropy->ct = -16;  /* force reading 2 initial bytes to fill C */
743
744
  /* Initialize restart counter */
745
787
  entropy->restarts_to_go = cinfo->restart_interval;
746
787
}
747
748
749
/*
750
 * Finish up at the end of an arithmetic-compressed scan.
751
 */
752
753
METHODDEF(void)
754
finish_pass (j_decompress_ptr cinfo)
755
762
{
756
  /* no work necessary here */
757
762
}
758
759
760
/*
761
 * Module initialization routine for arithmetic entropy decoding.
762
 */
763
764
GLOBAL(void)
765
jinit_arith_decoder (j_decompress_ptr cinfo)
766
278
{
767
278
  arith_entropy_ptr entropy;
768
278
  int i;
769
770
278
  entropy = (arith_entropy_ptr) (*cinfo->mem->alloc_small)
771
278
    ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(arith_entropy_decoder));
772
278
  cinfo->entropy = &entropy->pub;
773
278
  entropy->pub.start_pass = start_pass;
774
278
  entropy->pub.finish_pass = finish_pass;
775
776
  /* Mark tables unallocated */
777
4.72k
  for (i = 0; i < NUM_ARITH_TBLS; i++) {
778
4.44k
    entropy->dc_stats[i] = NULL;
779
4.44k
    entropy->ac_stats[i] = NULL;
780
4.44k
  }
781
782
  /* Initialize index for fixed probability estimation */
783
278
  entropy->fixed_bin[0] = 113;
784
785
278
  if (cinfo->progressive_mode) {
786
    /* Create progression status table */
787
156
    int *coef_bit_ptr, ci;
788
156
    cinfo->coef_bits = (int (*)[DCTSIZE2]) (*cinfo->mem->alloc_small)
789
156
      ((j_common_ptr) cinfo, JPOOL_IMAGE,
790
156
       cinfo->num_components * DCTSIZE2 * SIZEOF(int));
791
156
    coef_bit_ptr = & cinfo->coef_bits[0][0];
792
368
    for (ci = 0; ci < cinfo->num_components; ci++) 
793
13.7k
      for (i = 0; i < DCTSIZE2; i++)
794
13.5k
  *coef_bit_ptr++ = -1;
795
156
  }
796
278
}