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