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