1*dfc6aa5cSAndroid Build Coastguard Worker /*
2*dfc6aa5cSAndroid Build Coastguard Worker * jdinput.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) 1991-1997, Thomas G. Lane.
6*dfc6aa5cSAndroid Build Coastguard Worker * libjpeg-turbo Modifications:
7*dfc6aa5cSAndroid Build Coastguard Worker * Copyright (C) 2010, 2016, 2018, 2022, D. R. Commander.
8*dfc6aa5cSAndroid Build Coastguard Worker * Copyright (C) 2015, Google, Inc.
9*dfc6aa5cSAndroid Build Coastguard Worker * For conditions of distribution and use, see the accompanying README.ijg
10*dfc6aa5cSAndroid Build Coastguard Worker * file.
11*dfc6aa5cSAndroid Build Coastguard Worker *
12*dfc6aa5cSAndroid Build Coastguard Worker * This file contains input control logic for the JPEG decompressor.
13*dfc6aa5cSAndroid Build Coastguard Worker * These routines are concerned with controlling the decompressor's input
14*dfc6aa5cSAndroid Build Coastguard Worker * processing (marker reading and coefficient decoding). The actual input
15*dfc6aa5cSAndroid Build Coastguard Worker * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
16*dfc6aa5cSAndroid Build Coastguard Worker */
17*dfc6aa5cSAndroid Build Coastguard Worker
18*dfc6aa5cSAndroid Build Coastguard Worker #define JPEG_INTERNALS
19*dfc6aa5cSAndroid Build Coastguard Worker #include "jinclude.h"
20*dfc6aa5cSAndroid Build Coastguard Worker #include "jpeglib.h"
21*dfc6aa5cSAndroid Build Coastguard Worker #include "jpegcomp.h"
22*dfc6aa5cSAndroid Build Coastguard Worker
23*dfc6aa5cSAndroid Build Coastguard Worker
24*dfc6aa5cSAndroid Build Coastguard Worker /* Private state */
25*dfc6aa5cSAndroid Build Coastguard Worker
26*dfc6aa5cSAndroid Build Coastguard Worker typedef struct {
27*dfc6aa5cSAndroid Build Coastguard Worker struct jpeg_input_controller pub; /* public fields */
28*dfc6aa5cSAndroid Build Coastguard Worker
29*dfc6aa5cSAndroid Build Coastguard Worker boolean inheaders; /* TRUE until first SOS is reached */
30*dfc6aa5cSAndroid Build Coastguard Worker } my_input_controller;
31*dfc6aa5cSAndroid Build Coastguard Worker
32*dfc6aa5cSAndroid Build Coastguard Worker typedef my_input_controller *my_inputctl_ptr;
33*dfc6aa5cSAndroid Build Coastguard Worker
34*dfc6aa5cSAndroid Build Coastguard Worker
35*dfc6aa5cSAndroid Build Coastguard Worker /* Forward declarations */
36*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(int) consume_markers(j_decompress_ptr cinfo);
37*dfc6aa5cSAndroid Build Coastguard Worker
38*dfc6aa5cSAndroid Build Coastguard Worker
39*dfc6aa5cSAndroid Build Coastguard Worker /*
40*dfc6aa5cSAndroid Build Coastguard Worker * Routines to calculate various quantities related to the size of the image.
41*dfc6aa5cSAndroid Build Coastguard Worker */
42*dfc6aa5cSAndroid Build Coastguard Worker
43*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(void)
initial_setup(j_decompress_ptr cinfo)44*dfc6aa5cSAndroid Build Coastguard Worker initial_setup(j_decompress_ptr cinfo)
45*dfc6aa5cSAndroid Build Coastguard Worker /* Called once, when first SOS marker is reached */
46*dfc6aa5cSAndroid Build Coastguard Worker {
47*dfc6aa5cSAndroid Build Coastguard Worker int ci;
48*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr;
49*dfc6aa5cSAndroid Build Coastguard Worker
50*dfc6aa5cSAndroid Build Coastguard Worker /* Make sure image isn't bigger than I can handle */
51*dfc6aa5cSAndroid Build Coastguard Worker if ((long)cinfo->image_height > (long)JPEG_MAX_DIMENSION ||
52*dfc6aa5cSAndroid Build Coastguard Worker (long)cinfo->image_width > (long)JPEG_MAX_DIMENSION)
53*dfc6aa5cSAndroid Build Coastguard Worker ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int)JPEG_MAX_DIMENSION);
54*dfc6aa5cSAndroid Build Coastguard Worker
55*dfc6aa5cSAndroid Build Coastguard Worker /* For now, precision must match compiled-in value... */
56*dfc6aa5cSAndroid Build Coastguard Worker if (cinfo->data_precision != BITS_IN_JSAMPLE)
57*dfc6aa5cSAndroid Build Coastguard Worker ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
58*dfc6aa5cSAndroid Build Coastguard Worker
59*dfc6aa5cSAndroid Build Coastguard Worker /* Check that number of components won't exceed internal array sizes */
60*dfc6aa5cSAndroid Build Coastguard Worker if (cinfo->num_components > MAX_COMPONENTS)
61*dfc6aa5cSAndroid Build Coastguard Worker ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
62*dfc6aa5cSAndroid Build Coastguard Worker MAX_COMPONENTS);
63*dfc6aa5cSAndroid Build Coastguard Worker
64*dfc6aa5cSAndroid Build Coastguard Worker /* Compute maximum sampling factors; check factor validity */
65*dfc6aa5cSAndroid Build Coastguard Worker cinfo->max_h_samp_factor = 1;
66*dfc6aa5cSAndroid Build Coastguard Worker cinfo->max_v_samp_factor = 1;
67*dfc6aa5cSAndroid Build Coastguard Worker for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
68*dfc6aa5cSAndroid Build Coastguard Worker ci++, compptr++) {
69*dfc6aa5cSAndroid Build Coastguard Worker if (compptr->h_samp_factor <= 0 ||
70*dfc6aa5cSAndroid Build Coastguard Worker compptr->h_samp_factor > MAX_SAMP_FACTOR ||
71*dfc6aa5cSAndroid Build Coastguard Worker compptr->v_samp_factor <= 0 ||
72*dfc6aa5cSAndroid Build Coastguard Worker compptr->v_samp_factor > MAX_SAMP_FACTOR)
73*dfc6aa5cSAndroid Build Coastguard Worker ERREXIT(cinfo, JERR_BAD_SAMPLING);
74*dfc6aa5cSAndroid Build Coastguard Worker cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
75*dfc6aa5cSAndroid Build Coastguard Worker compptr->h_samp_factor);
76*dfc6aa5cSAndroid Build Coastguard Worker cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
77*dfc6aa5cSAndroid Build Coastguard Worker compptr->v_samp_factor);
78*dfc6aa5cSAndroid Build Coastguard Worker }
79*dfc6aa5cSAndroid Build Coastguard Worker
80*dfc6aa5cSAndroid Build Coastguard Worker #if JPEG_LIB_VERSION >= 80
81*dfc6aa5cSAndroid Build Coastguard Worker cinfo->block_size = DCTSIZE;
82*dfc6aa5cSAndroid Build Coastguard Worker cinfo->natural_order = jpeg_natural_order;
83*dfc6aa5cSAndroid Build Coastguard Worker cinfo->lim_Se = DCTSIZE2 - 1;
84*dfc6aa5cSAndroid Build Coastguard Worker #endif
85*dfc6aa5cSAndroid Build Coastguard Worker
86*dfc6aa5cSAndroid Build Coastguard Worker /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
87*dfc6aa5cSAndroid Build Coastguard Worker * In the full decompressor, this will be overridden by jdmaster.c;
88*dfc6aa5cSAndroid Build Coastguard Worker * but in the transcoder, jdmaster.c is not used, so we must do it here.
89*dfc6aa5cSAndroid Build Coastguard Worker */
90*dfc6aa5cSAndroid Build Coastguard Worker #if JPEG_LIB_VERSION >= 70
91*dfc6aa5cSAndroid Build Coastguard Worker cinfo->min_DCT_h_scaled_size = cinfo->min_DCT_v_scaled_size = DCTSIZE;
92*dfc6aa5cSAndroid Build Coastguard Worker #else
93*dfc6aa5cSAndroid Build Coastguard Worker cinfo->min_DCT_scaled_size = DCTSIZE;
94*dfc6aa5cSAndroid Build Coastguard Worker #endif
95*dfc6aa5cSAndroid Build Coastguard Worker
96*dfc6aa5cSAndroid Build Coastguard Worker /* Compute dimensions of components */
97*dfc6aa5cSAndroid Build Coastguard Worker for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
98*dfc6aa5cSAndroid Build Coastguard Worker ci++, compptr++) {
99*dfc6aa5cSAndroid Build Coastguard Worker #if JPEG_LIB_VERSION >= 70
100*dfc6aa5cSAndroid Build Coastguard Worker compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE;
101*dfc6aa5cSAndroid Build Coastguard Worker #else
102*dfc6aa5cSAndroid Build Coastguard Worker compptr->DCT_scaled_size = DCTSIZE;
103*dfc6aa5cSAndroid Build Coastguard Worker #endif
104*dfc6aa5cSAndroid Build Coastguard Worker /* Size in DCT blocks */
105*dfc6aa5cSAndroid Build Coastguard Worker compptr->width_in_blocks = (JDIMENSION)
106*dfc6aa5cSAndroid Build Coastguard Worker jdiv_round_up((long)cinfo->image_width * (long)compptr->h_samp_factor,
107*dfc6aa5cSAndroid Build Coastguard Worker (long)(cinfo->max_h_samp_factor * DCTSIZE));
108*dfc6aa5cSAndroid Build Coastguard Worker compptr->height_in_blocks = (JDIMENSION)
109*dfc6aa5cSAndroid Build Coastguard Worker jdiv_round_up((long)cinfo->image_height * (long)compptr->v_samp_factor,
110*dfc6aa5cSAndroid Build Coastguard Worker (long)(cinfo->max_v_samp_factor * DCTSIZE));
111*dfc6aa5cSAndroid Build Coastguard Worker /* Set the first and last MCU columns to decompress from multi-scan images.
112*dfc6aa5cSAndroid Build Coastguard Worker * By default, decompress all of the MCU columns.
113*dfc6aa5cSAndroid Build Coastguard Worker */
114*dfc6aa5cSAndroid Build Coastguard Worker cinfo->master->first_MCU_col[ci] = 0;
115*dfc6aa5cSAndroid Build Coastguard Worker cinfo->master->last_MCU_col[ci] = compptr->width_in_blocks - 1;
116*dfc6aa5cSAndroid Build Coastguard Worker /* downsampled_width and downsampled_height will also be overridden by
117*dfc6aa5cSAndroid Build Coastguard Worker * jdmaster.c if we are doing full decompression. The transcoder library
118*dfc6aa5cSAndroid Build Coastguard Worker * doesn't use these values, but the calling application might.
119*dfc6aa5cSAndroid Build Coastguard Worker */
120*dfc6aa5cSAndroid Build Coastguard Worker /* Size in samples */
121*dfc6aa5cSAndroid Build Coastguard Worker compptr->downsampled_width = (JDIMENSION)
122*dfc6aa5cSAndroid Build Coastguard Worker jdiv_round_up((long)cinfo->image_width * (long)compptr->h_samp_factor,
123*dfc6aa5cSAndroid Build Coastguard Worker (long)cinfo->max_h_samp_factor);
124*dfc6aa5cSAndroid Build Coastguard Worker compptr->downsampled_height = (JDIMENSION)
125*dfc6aa5cSAndroid Build Coastguard Worker jdiv_round_up((long)cinfo->image_height * (long)compptr->v_samp_factor,
126*dfc6aa5cSAndroid Build Coastguard Worker (long)cinfo->max_v_samp_factor);
127*dfc6aa5cSAndroid Build Coastguard Worker /* Mark component needed, until color conversion says otherwise */
128*dfc6aa5cSAndroid Build Coastguard Worker compptr->component_needed = TRUE;
129*dfc6aa5cSAndroid Build Coastguard Worker /* Mark no quantization table yet saved for component */
130*dfc6aa5cSAndroid Build Coastguard Worker compptr->quant_table = NULL;
131*dfc6aa5cSAndroid Build Coastguard Worker }
132*dfc6aa5cSAndroid Build Coastguard Worker
133*dfc6aa5cSAndroid Build Coastguard Worker /* Compute number of fully interleaved MCU rows. */
134*dfc6aa5cSAndroid Build Coastguard Worker cinfo->total_iMCU_rows = (JDIMENSION)
135*dfc6aa5cSAndroid Build Coastguard Worker jdiv_round_up((long)cinfo->image_height,
136*dfc6aa5cSAndroid Build Coastguard Worker (long)(cinfo->max_v_samp_factor * DCTSIZE));
137*dfc6aa5cSAndroid Build Coastguard Worker
138*dfc6aa5cSAndroid Build Coastguard Worker /* Decide whether file contains multiple scans */
139*dfc6aa5cSAndroid Build Coastguard Worker if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
140*dfc6aa5cSAndroid Build Coastguard Worker cinfo->inputctl->has_multiple_scans = TRUE;
141*dfc6aa5cSAndroid Build Coastguard Worker else
142*dfc6aa5cSAndroid Build Coastguard Worker cinfo->inputctl->has_multiple_scans = FALSE;
143*dfc6aa5cSAndroid Build Coastguard Worker }
144*dfc6aa5cSAndroid Build Coastguard Worker
145*dfc6aa5cSAndroid Build Coastguard Worker
146*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(void)
per_scan_setup(j_decompress_ptr cinfo)147*dfc6aa5cSAndroid Build Coastguard Worker per_scan_setup(j_decompress_ptr cinfo)
148*dfc6aa5cSAndroid Build Coastguard Worker /* Do computations that are needed before processing a JPEG scan */
149*dfc6aa5cSAndroid Build Coastguard Worker /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
150*dfc6aa5cSAndroid Build Coastguard Worker {
151*dfc6aa5cSAndroid Build Coastguard Worker int ci, mcublks, tmp;
152*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr;
153*dfc6aa5cSAndroid Build Coastguard Worker
154*dfc6aa5cSAndroid Build Coastguard Worker if (cinfo->comps_in_scan == 1) {
155*dfc6aa5cSAndroid Build Coastguard Worker
156*dfc6aa5cSAndroid Build Coastguard Worker /* Noninterleaved (single-component) scan */
157*dfc6aa5cSAndroid Build Coastguard Worker compptr = cinfo->cur_comp_info[0];
158*dfc6aa5cSAndroid Build Coastguard Worker
159*dfc6aa5cSAndroid Build Coastguard Worker /* Overall image size in MCUs */
160*dfc6aa5cSAndroid Build Coastguard Worker cinfo->MCUs_per_row = compptr->width_in_blocks;
161*dfc6aa5cSAndroid Build Coastguard Worker cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
162*dfc6aa5cSAndroid Build Coastguard Worker
163*dfc6aa5cSAndroid Build Coastguard Worker /* For noninterleaved scan, always one block per MCU */
164*dfc6aa5cSAndroid Build Coastguard Worker compptr->MCU_width = 1;
165*dfc6aa5cSAndroid Build Coastguard Worker compptr->MCU_height = 1;
166*dfc6aa5cSAndroid Build Coastguard Worker compptr->MCU_blocks = 1;
167*dfc6aa5cSAndroid Build Coastguard Worker compptr->MCU_sample_width = compptr->_DCT_scaled_size;
168*dfc6aa5cSAndroid Build Coastguard Worker compptr->last_col_width = 1;
169*dfc6aa5cSAndroid Build Coastguard Worker /* For noninterleaved scans, it is convenient to define last_row_height
170*dfc6aa5cSAndroid Build Coastguard Worker * as the number of block rows present in the last iMCU row.
171*dfc6aa5cSAndroid Build Coastguard Worker */
172*dfc6aa5cSAndroid Build Coastguard Worker tmp = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
173*dfc6aa5cSAndroid Build Coastguard Worker if (tmp == 0) tmp = compptr->v_samp_factor;
174*dfc6aa5cSAndroid Build Coastguard Worker compptr->last_row_height = tmp;
175*dfc6aa5cSAndroid Build Coastguard Worker
176*dfc6aa5cSAndroid Build Coastguard Worker /* Prepare array describing MCU composition */
177*dfc6aa5cSAndroid Build Coastguard Worker cinfo->blocks_in_MCU = 1;
178*dfc6aa5cSAndroid Build Coastguard Worker cinfo->MCU_membership[0] = 0;
179*dfc6aa5cSAndroid Build Coastguard Worker
180*dfc6aa5cSAndroid Build Coastguard Worker } else {
181*dfc6aa5cSAndroid Build Coastguard Worker
182*dfc6aa5cSAndroid Build Coastguard Worker /* Interleaved (multi-component) scan */
183*dfc6aa5cSAndroid Build Coastguard Worker if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
184*dfc6aa5cSAndroid Build Coastguard Worker ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
185*dfc6aa5cSAndroid Build Coastguard Worker MAX_COMPS_IN_SCAN);
186*dfc6aa5cSAndroid Build Coastguard Worker
187*dfc6aa5cSAndroid Build Coastguard Worker /* Overall image size in MCUs */
188*dfc6aa5cSAndroid Build Coastguard Worker cinfo->MCUs_per_row = (JDIMENSION)
189*dfc6aa5cSAndroid Build Coastguard Worker jdiv_round_up((long)cinfo->image_width,
190*dfc6aa5cSAndroid Build Coastguard Worker (long)(cinfo->max_h_samp_factor * DCTSIZE));
191*dfc6aa5cSAndroid Build Coastguard Worker cinfo->MCU_rows_in_scan = (JDIMENSION)
192*dfc6aa5cSAndroid Build Coastguard Worker jdiv_round_up((long)cinfo->image_height,
193*dfc6aa5cSAndroid Build Coastguard Worker (long)(cinfo->max_v_samp_factor * DCTSIZE));
194*dfc6aa5cSAndroid Build Coastguard Worker
195*dfc6aa5cSAndroid Build Coastguard Worker cinfo->blocks_in_MCU = 0;
196*dfc6aa5cSAndroid Build Coastguard Worker
197*dfc6aa5cSAndroid Build Coastguard Worker for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
198*dfc6aa5cSAndroid Build Coastguard Worker compptr = cinfo->cur_comp_info[ci];
199*dfc6aa5cSAndroid Build Coastguard Worker /* Sampling factors give # of blocks of component in each MCU */
200*dfc6aa5cSAndroid Build Coastguard Worker compptr->MCU_width = compptr->h_samp_factor;
201*dfc6aa5cSAndroid Build Coastguard Worker compptr->MCU_height = compptr->v_samp_factor;
202*dfc6aa5cSAndroid Build Coastguard Worker compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
203*dfc6aa5cSAndroid Build Coastguard Worker compptr->MCU_sample_width = compptr->MCU_width *
204*dfc6aa5cSAndroid Build Coastguard Worker compptr->_DCT_scaled_size;
205*dfc6aa5cSAndroid Build Coastguard Worker /* Figure number of non-dummy blocks in last MCU column & row */
206*dfc6aa5cSAndroid Build Coastguard Worker tmp = (int)(compptr->width_in_blocks % compptr->MCU_width);
207*dfc6aa5cSAndroid Build Coastguard Worker if (tmp == 0) tmp = compptr->MCU_width;
208*dfc6aa5cSAndroid Build Coastguard Worker compptr->last_col_width = tmp;
209*dfc6aa5cSAndroid Build Coastguard Worker tmp = (int)(compptr->height_in_blocks % compptr->MCU_height);
210*dfc6aa5cSAndroid Build Coastguard Worker if (tmp == 0) tmp = compptr->MCU_height;
211*dfc6aa5cSAndroid Build Coastguard Worker compptr->last_row_height = tmp;
212*dfc6aa5cSAndroid Build Coastguard Worker /* Prepare array describing MCU composition */
213*dfc6aa5cSAndroid Build Coastguard Worker mcublks = compptr->MCU_blocks;
214*dfc6aa5cSAndroid Build Coastguard Worker if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
215*dfc6aa5cSAndroid Build Coastguard Worker ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
216*dfc6aa5cSAndroid Build Coastguard Worker while (mcublks-- > 0) {
217*dfc6aa5cSAndroid Build Coastguard Worker cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
218*dfc6aa5cSAndroid Build Coastguard Worker }
219*dfc6aa5cSAndroid Build Coastguard Worker }
220*dfc6aa5cSAndroid Build Coastguard Worker
221*dfc6aa5cSAndroid Build Coastguard Worker }
222*dfc6aa5cSAndroid Build Coastguard Worker }
223*dfc6aa5cSAndroid Build Coastguard Worker
224*dfc6aa5cSAndroid Build Coastguard Worker
225*dfc6aa5cSAndroid Build Coastguard Worker /*
226*dfc6aa5cSAndroid Build Coastguard Worker * Save away a copy of the Q-table referenced by each component present
227*dfc6aa5cSAndroid Build Coastguard Worker * in the current scan, unless already saved during a prior scan.
228*dfc6aa5cSAndroid Build Coastguard Worker *
229*dfc6aa5cSAndroid Build Coastguard Worker * In a multiple-scan JPEG file, the encoder could assign different components
230*dfc6aa5cSAndroid Build Coastguard Worker * the same Q-table slot number, but change table definitions between scans
231*dfc6aa5cSAndroid Build Coastguard Worker * so that each component uses a different Q-table. (The IJG encoder is not
232*dfc6aa5cSAndroid Build Coastguard Worker * currently capable of doing this, but other encoders might.) Since we want
233*dfc6aa5cSAndroid Build Coastguard Worker * to be able to dequantize all the components at the end of the file, this
234*dfc6aa5cSAndroid Build Coastguard Worker * means that we have to save away the table actually used for each component.
235*dfc6aa5cSAndroid Build Coastguard Worker * We do this by copying the table at the start of the first scan containing
236*dfc6aa5cSAndroid Build Coastguard Worker * the component.
237*dfc6aa5cSAndroid Build Coastguard Worker * Rec. ITU-T T.81 | ISO/IEC 10918-1 prohibits the encoder from changing the
238*dfc6aa5cSAndroid Build Coastguard Worker * contents of a Q-table slot between scans of a component using that slot. If
239*dfc6aa5cSAndroid Build Coastguard Worker * the encoder does so anyway, this decoder will simply use the Q-table values
240*dfc6aa5cSAndroid Build Coastguard Worker * that were current at the start of the first scan for the component.
241*dfc6aa5cSAndroid Build Coastguard Worker *
242*dfc6aa5cSAndroid Build Coastguard Worker * The decompressor output side looks only at the saved quant tables,
243*dfc6aa5cSAndroid Build Coastguard Worker * not at the current Q-table slots.
244*dfc6aa5cSAndroid Build Coastguard Worker */
245*dfc6aa5cSAndroid Build Coastguard Worker
246*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(void)
latch_quant_tables(j_decompress_ptr cinfo)247*dfc6aa5cSAndroid Build Coastguard Worker latch_quant_tables(j_decompress_ptr cinfo)
248*dfc6aa5cSAndroid Build Coastguard Worker {
249*dfc6aa5cSAndroid Build Coastguard Worker int ci, qtblno;
250*dfc6aa5cSAndroid Build Coastguard Worker jpeg_component_info *compptr;
251*dfc6aa5cSAndroid Build Coastguard Worker JQUANT_TBL *qtbl;
252*dfc6aa5cSAndroid Build Coastguard Worker
253*dfc6aa5cSAndroid Build Coastguard Worker for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
254*dfc6aa5cSAndroid Build Coastguard Worker compptr = cinfo->cur_comp_info[ci];
255*dfc6aa5cSAndroid Build Coastguard Worker /* No work if we already saved Q-table for this component */
256*dfc6aa5cSAndroid Build Coastguard Worker if (compptr->quant_table != NULL)
257*dfc6aa5cSAndroid Build Coastguard Worker continue;
258*dfc6aa5cSAndroid Build Coastguard Worker /* Make sure specified quantization table is present */
259*dfc6aa5cSAndroid Build Coastguard Worker qtblno = compptr->quant_tbl_no;
260*dfc6aa5cSAndroid Build Coastguard Worker if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
261*dfc6aa5cSAndroid Build Coastguard Worker cinfo->quant_tbl_ptrs[qtblno] == NULL)
262*dfc6aa5cSAndroid Build Coastguard Worker ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
263*dfc6aa5cSAndroid Build Coastguard Worker /* OK, save away the quantization table */
264*dfc6aa5cSAndroid Build Coastguard Worker qtbl = (JQUANT_TBL *)
265*dfc6aa5cSAndroid Build Coastguard Worker (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
266*dfc6aa5cSAndroid Build Coastguard Worker sizeof(JQUANT_TBL));
267*dfc6aa5cSAndroid Build Coastguard Worker memcpy(qtbl, cinfo->quant_tbl_ptrs[qtblno], sizeof(JQUANT_TBL));
268*dfc6aa5cSAndroid Build Coastguard Worker compptr->quant_table = qtbl;
269*dfc6aa5cSAndroid Build Coastguard Worker }
270*dfc6aa5cSAndroid Build Coastguard Worker }
271*dfc6aa5cSAndroid Build Coastguard Worker
272*dfc6aa5cSAndroid Build Coastguard Worker
273*dfc6aa5cSAndroid Build Coastguard Worker /*
274*dfc6aa5cSAndroid Build Coastguard Worker * Initialize the input modules to read a scan of compressed data.
275*dfc6aa5cSAndroid Build Coastguard Worker * The first call to this is done by jdmaster.c after initializing
276*dfc6aa5cSAndroid Build Coastguard Worker * the entire decompressor (during jpeg_start_decompress).
277*dfc6aa5cSAndroid Build Coastguard Worker * Subsequent calls come from consume_markers, below.
278*dfc6aa5cSAndroid Build Coastguard Worker */
279*dfc6aa5cSAndroid Build Coastguard Worker
280*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(void)
start_input_pass(j_decompress_ptr cinfo)281*dfc6aa5cSAndroid Build Coastguard Worker start_input_pass(j_decompress_ptr cinfo)
282*dfc6aa5cSAndroid Build Coastguard Worker {
283*dfc6aa5cSAndroid Build Coastguard Worker per_scan_setup(cinfo);
284*dfc6aa5cSAndroid Build Coastguard Worker latch_quant_tables(cinfo);
285*dfc6aa5cSAndroid Build Coastguard Worker (*cinfo->entropy->start_pass) (cinfo);
286*dfc6aa5cSAndroid Build Coastguard Worker (*cinfo->coef->start_input_pass) (cinfo);
287*dfc6aa5cSAndroid Build Coastguard Worker cinfo->inputctl->consume_input = cinfo->coef->consume_data;
288*dfc6aa5cSAndroid Build Coastguard Worker }
289*dfc6aa5cSAndroid Build Coastguard Worker
290*dfc6aa5cSAndroid Build Coastguard Worker
291*dfc6aa5cSAndroid Build Coastguard Worker /*
292*dfc6aa5cSAndroid Build Coastguard Worker * Finish up after inputting a compressed-data scan.
293*dfc6aa5cSAndroid Build Coastguard Worker * This is called by the coefficient controller after it's read all
294*dfc6aa5cSAndroid Build Coastguard Worker * the expected data of the scan.
295*dfc6aa5cSAndroid Build Coastguard Worker */
296*dfc6aa5cSAndroid Build Coastguard Worker
297*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(void)
finish_input_pass(j_decompress_ptr cinfo)298*dfc6aa5cSAndroid Build Coastguard Worker finish_input_pass(j_decompress_ptr cinfo)
299*dfc6aa5cSAndroid Build Coastguard Worker {
300*dfc6aa5cSAndroid Build Coastguard Worker cinfo->inputctl->consume_input = consume_markers;
301*dfc6aa5cSAndroid Build Coastguard Worker }
302*dfc6aa5cSAndroid Build Coastguard Worker
303*dfc6aa5cSAndroid Build Coastguard Worker
304*dfc6aa5cSAndroid Build Coastguard Worker /*
305*dfc6aa5cSAndroid Build Coastguard Worker * Read JPEG markers before, between, or after compressed-data scans.
306*dfc6aa5cSAndroid Build Coastguard Worker * Change state as necessary when a new scan is reached.
307*dfc6aa5cSAndroid Build Coastguard Worker * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
308*dfc6aa5cSAndroid Build Coastguard Worker *
309*dfc6aa5cSAndroid Build Coastguard Worker * The consume_input method pointer points either here or to the
310*dfc6aa5cSAndroid Build Coastguard Worker * coefficient controller's consume_data routine, depending on whether
311*dfc6aa5cSAndroid Build Coastguard Worker * we are reading a compressed data segment or inter-segment markers.
312*dfc6aa5cSAndroid Build Coastguard Worker */
313*dfc6aa5cSAndroid Build Coastguard Worker
314*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(int)
consume_markers(j_decompress_ptr cinfo)315*dfc6aa5cSAndroid Build Coastguard Worker consume_markers(j_decompress_ptr cinfo)
316*dfc6aa5cSAndroid Build Coastguard Worker {
317*dfc6aa5cSAndroid Build Coastguard Worker my_inputctl_ptr inputctl = (my_inputctl_ptr)cinfo->inputctl;
318*dfc6aa5cSAndroid Build Coastguard Worker int val;
319*dfc6aa5cSAndroid Build Coastguard Worker
320*dfc6aa5cSAndroid Build Coastguard Worker if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
321*dfc6aa5cSAndroid Build Coastguard Worker return JPEG_REACHED_EOI;
322*dfc6aa5cSAndroid Build Coastguard Worker
323*dfc6aa5cSAndroid Build Coastguard Worker val = (*cinfo->marker->read_markers) (cinfo);
324*dfc6aa5cSAndroid Build Coastguard Worker
325*dfc6aa5cSAndroid Build Coastguard Worker switch (val) {
326*dfc6aa5cSAndroid Build Coastguard Worker case JPEG_REACHED_SOS: /* Found SOS */
327*dfc6aa5cSAndroid Build Coastguard Worker if (inputctl->inheaders) { /* 1st SOS */
328*dfc6aa5cSAndroid Build Coastguard Worker initial_setup(cinfo);
329*dfc6aa5cSAndroid Build Coastguard Worker inputctl->inheaders = FALSE;
330*dfc6aa5cSAndroid Build Coastguard Worker /* Note: start_input_pass must be called by jdmaster.c
331*dfc6aa5cSAndroid Build Coastguard Worker * before any more input can be consumed. jdapimin.c is
332*dfc6aa5cSAndroid Build Coastguard Worker * responsible for enforcing this sequencing.
333*dfc6aa5cSAndroid Build Coastguard Worker */
334*dfc6aa5cSAndroid Build Coastguard Worker } else { /* 2nd or later SOS marker */
335*dfc6aa5cSAndroid Build Coastguard Worker if (!inputctl->pub.has_multiple_scans)
336*dfc6aa5cSAndroid Build Coastguard Worker ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
337*dfc6aa5cSAndroid Build Coastguard Worker start_input_pass(cinfo);
338*dfc6aa5cSAndroid Build Coastguard Worker }
339*dfc6aa5cSAndroid Build Coastguard Worker break;
340*dfc6aa5cSAndroid Build Coastguard Worker case JPEG_REACHED_EOI: /* Found EOI */
341*dfc6aa5cSAndroid Build Coastguard Worker inputctl->pub.eoi_reached = TRUE;
342*dfc6aa5cSAndroid Build Coastguard Worker if (inputctl->inheaders) { /* Tables-only datastream, apparently */
343*dfc6aa5cSAndroid Build Coastguard Worker if (cinfo->marker->saw_SOF)
344*dfc6aa5cSAndroid Build Coastguard Worker ERREXIT(cinfo, JERR_SOF_NO_SOS);
345*dfc6aa5cSAndroid Build Coastguard Worker } else {
346*dfc6aa5cSAndroid Build Coastguard Worker /* Prevent infinite loop in coef ctlr's decompress_data routine
347*dfc6aa5cSAndroid Build Coastguard Worker * if user set output_scan_number larger than number of scans.
348*dfc6aa5cSAndroid Build Coastguard Worker */
349*dfc6aa5cSAndroid Build Coastguard Worker if (cinfo->output_scan_number > cinfo->input_scan_number)
350*dfc6aa5cSAndroid Build Coastguard Worker cinfo->output_scan_number = cinfo->input_scan_number;
351*dfc6aa5cSAndroid Build Coastguard Worker }
352*dfc6aa5cSAndroid Build Coastguard Worker break;
353*dfc6aa5cSAndroid Build Coastguard Worker case JPEG_SUSPENDED:
354*dfc6aa5cSAndroid Build Coastguard Worker break;
355*dfc6aa5cSAndroid Build Coastguard Worker }
356*dfc6aa5cSAndroid Build Coastguard Worker
357*dfc6aa5cSAndroid Build Coastguard Worker return val;
358*dfc6aa5cSAndroid Build Coastguard Worker }
359*dfc6aa5cSAndroid Build Coastguard Worker
360*dfc6aa5cSAndroid Build Coastguard Worker
361*dfc6aa5cSAndroid Build Coastguard Worker /*
362*dfc6aa5cSAndroid Build Coastguard Worker * Reset state to begin a fresh datastream.
363*dfc6aa5cSAndroid Build Coastguard Worker */
364*dfc6aa5cSAndroid Build Coastguard Worker
365*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(void)
reset_input_controller(j_decompress_ptr cinfo)366*dfc6aa5cSAndroid Build Coastguard Worker reset_input_controller(j_decompress_ptr cinfo)
367*dfc6aa5cSAndroid Build Coastguard Worker {
368*dfc6aa5cSAndroid Build Coastguard Worker my_inputctl_ptr inputctl = (my_inputctl_ptr)cinfo->inputctl;
369*dfc6aa5cSAndroid Build Coastguard Worker
370*dfc6aa5cSAndroid Build Coastguard Worker inputctl->pub.consume_input = consume_markers;
371*dfc6aa5cSAndroid Build Coastguard Worker inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
372*dfc6aa5cSAndroid Build Coastguard Worker inputctl->pub.eoi_reached = FALSE;
373*dfc6aa5cSAndroid Build Coastguard Worker inputctl->inheaders = TRUE;
374*dfc6aa5cSAndroid Build Coastguard Worker /* Reset other modules */
375*dfc6aa5cSAndroid Build Coastguard Worker (*cinfo->err->reset_error_mgr) ((j_common_ptr)cinfo);
376*dfc6aa5cSAndroid Build Coastguard Worker (*cinfo->marker->reset_marker_reader) (cinfo);
377*dfc6aa5cSAndroid Build Coastguard Worker /* Reset progression state -- would be cleaner if entropy decoder did this */
378*dfc6aa5cSAndroid Build Coastguard Worker cinfo->coef_bits = NULL;
379*dfc6aa5cSAndroid Build Coastguard Worker }
380*dfc6aa5cSAndroid Build Coastguard Worker
381*dfc6aa5cSAndroid Build Coastguard Worker
382*dfc6aa5cSAndroid Build Coastguard Worker /*
383*dfc6aa5cSAndroid Build Coastguard Worker * Initialize the input controller module.
384*dfc6aa5cSAndroid Build Coastguard Worker * This is called only once, when the decompression object is created.
385*dfc6aa5cSAndroid Build Coastguard Worker */
386*dfc6aa5cSAndroid Build Coastguard Worker
387*dfc6aa5cSAndroid Build Coastguard Worker GLOBAL(void)
jinit_input_controller(j_decompress_ptr cinfo)388*dfc6aa5cSAndroid Build Coastguard Worker jinit_input_controller(j_decompress_ptr cinfo)
389*dfc6aa5cSAndroid Build Coastguard Worker {
390*dfc6aa5cSAndroid Build Coastguard Worker my_inputctl_ptr inputctl;
391*dfc6aa5cSAndroid Build Coastguard Worker
392*dfc6aa5cSAndroid Build Coastguard Worker /* Create subobject in permanent pool */
393*dfc6aa5cSAndroid Build Coastguard Worker inputctl = (my_inputctl_ptr)
394*dfc6aa5cSAndroid Build Coastguard Worker (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
395*dfc6aa5cSAndroid Build Coastguard Worker sizeof(my_input_controller));
396*dfc6aa5cSAndroid Build Coastguard Worker cinfo->inputctl = (struct jpeg_input_controller *)inputctl;
397*dfc6aa5cSAndroid Build Coastguard Worker /* Initialize method pointers */
398*dfc6aa5cSAndroid Build Coastguard Worker inputctl->pub.consume_input = consume_markers;
399*dfc6aa5cSAndroid Build Coastguard Worker inputctl->pub.reset_input_controller = reset_input_controller;
400*dfc6aa5cSAndroid Build Coastguard Worker inputctl->pub.start_input_pass = start_input_pass;
401*dfc6aa5cSAndroid Build Coastguard Worker inputctl->pub.finish_input_pass = finish_input_pass;
402*dfc6aa5cSAndroid Build Coastguard Worker /* Initialize state: can't use reset_input_controller since we don't
403*dfc6aa5cSAndroid Build Coastguard Worker * want to try to reset other modules yet.
404*dfc6aa5cSAndroid Build Coastguard Worker */
405*dfc6aa5cSAndroid Build Coastguard Worker inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
406*dfc6aa5cSAndroid Build Coastguard Worker inputctl->pub.eoi_reached = FALSE;
407*dfc6aa5cSAndroid Build Coastguard Worker inputctl->inheaders = TRUE;
408*dfc6aa5cSAndroid Build Coastguard Worker }
409