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