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