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