blob: 23c4bf2d13b10075686174b806d01d86f3496cd3 [file] [log] [blame]
Thomas G. Lanebc79e061995-08-02 00:00:00 +00001/*
2 * jdinput.c
3 *
DRCa73e8702012-12-31 02:52:30 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00005 * Copyright (C) 1991-1997, Thomas G. Lane.
DRCa6ef2822013-09-28 03:23:49 +00006 * libjpeg-turbo Modifications:
DRC36a6eec2010-10-08 08:05:44 +00007 * Copyright (C) 2010, D. R. Commander.
DRC7e3acc02015-10-10 10:25:46 -05008 * For conditions of distribution and use, see the accompanying README.ijg
9 * file.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000010 *
11 * This file contains input control logic for the JPEG decompressor.
12 * These routines are concerned with controlling the decompressor's input
13 * processing (marker reading and coefficient decoding). The actual input
14 * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
15 */
16
17#define JPEG_INTERNALS
18#include "jinclude.h"
19#include "jpeglib.h"
DRC36a6eec2010-10-08 08:05:44 +000020#include "jpegcomp.h"
Thomas G. Lanebc79e061995-08-02 00:00:00 +000021
22
23/* Private state */
24
25typedef struct {
26 struct jpeg_input_controller pub; /* public fields */
27
DRCe5eaf372014-05-09 18:00:32 +000028 boolean inheaders; /* TRUE until first SOS is reached */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000029} my_input_controller;
30
DRCbd498032016-02-19 08:53:33 -060031typedef my_input_controller *my_inputctl_ptr;
Thomas G. Lanebc79e061995-08-02 00:00:00 +000032
33
34/* Forward declarations */
DRCbc56b752014-05-16 10:43:44 +000035METHODDEF(int) consume_markers (j_decompress_ptr cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +000036
37
38/*
39 * Routines to calculate various quantities related to the size of the image.
40 */
41
Thomas G. Lane489583f1996-02-07 00:00:00 +000042LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000043initial_setup (j_decompress_ptr cinfo)
44/* Called once, when first SOS marker is reached */
45{
46 int ci;
47 jpeg_component_info *compptr;
48
49 /* Make sure image isn't bigger than I can handle */
50 if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
51 (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
52 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
53
54 /* For now, precision must match compiled-in value... */
55 if (cinfo->data_precision != BITS_IN_JSAMPLE)
56 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
57
58 /* Check that number of components won't exceed internal array sizes */
59 if (cinfo->num_components > MAX_COMPONENTS)
60 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
DRCe5eaf372014-05-09 18:00:32 +000061 MAX_COMPONENTS);
Thomas G. Lanebc79e061995-08-02 00:00:00 +000062
63 /* Compute maximum sampling factors; check factor validity */
64 cinfo->max_h_samp_factor = 1;
65 cinfo->max_v_samp_factor = 1;
66 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
67 ci++, compptr++) {
68 if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
DRCe5eaf372014-05-09 18:00:32 +000069 compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000070 ERREXIT(cinfo, JERR_BAD_SAMPLING);
71 cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
DRCe5eaf372014-05-09 18:00:32 +000072 compptr->h_samp_factor);
Thomas G. Lanebc79e061995-08-02 00:00:00 +000073 cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
DRCe5eaf372014-05-09 18:00:32 +000074 compptr->v_samp_factor);
Thomas G. Lanebc79e061995-08-02 00:00:00 +000075 }
76
DRC36a6eec2010-10-08 08:05:44 +000077#if JPEG_LIB_VERSION >=80
Guido Vollbeding989630f2010-01-10 00:00:00 +000078 cinfo->block_size = DCTSIZE;
79 cinfo->natural_order = jpeg_natural_order;
80 cinfo->lim_Se = DCTSIZE2-1;
DRC36a6eec2010-10-08 08:05:44 +000081#endif
Guido Vollbeding989630f2010-01-10 00:00:00 +000082
Thomas G. Lanebc79e061995-08-02 00:00:00 +000083 /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
84 * In the full decompressor, this will be overridden by jdmaster.c;
85 * but in the transcoder, jdmaster.c is not used, so we must do it here.
86 */
DRC36a6eec2010-10-08 08:05:44 +000087#if JPEG_LIB_VERSION >= 70
88 cinfo->min_DCT_h_scaled_size = cinfo->min_DCT_v_scaled_size = DCTSIZE;
89#else
Thomas G. Lanebc79e061995-08-02 00:00:00 +000090 cinfo->min_DCT_scaled_size = DCTSIZE;
DRC36a6eec2010-10-08 08:05:44 +000091#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +000092
93 /* Compute dimensions of components */
94 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
95 ci++, compptr++) {
DRC36a6eec2010-10-08 08:05:44 +000096#if JPEG_LIB_VERSION >= 70
97 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE;
98#else
Thomas G. Lanebc79e061995-08-02 00:00:00 +000099 compptr->DCT_scaled_size = DCTSIZE;
DRC36a6eec2010-10-08 08:05:44 +0000100#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000101 /* Size in DCT blocks */
102 compptr->width_in_blocks = (JDIMENSION)
103 jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
DRCe5eaf372014-05-09 18:00:32 +0000104 (long) (cinfo->max_h_samp_factor * DCTSIZE));
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000105 compptr->height_in_blocks = (JDIMENSION)
106 jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
DRCe5eaf372014-05-09 18:00:32 +0000107 (long) (cinfo->max_v_samp_factor * DCTSIZE));
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000108 /* downsampled_width and downsampled_height will also be overridden by
109 * jdmaster.c if we are doing full decompression. The transcoder library
110 * doesn't use these values, but the calling application might.
111 */
112 /* Size in samples */
113 compptr->downsampled_width = (JDIMENSION)
114 jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
DRCe5eaf372014-05-09 18:00:32 +0000115 (long) cinfo->max_h_samp_factor);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000116 compptr->downsampled_height = (JDIMENSION)
117 jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
DRCe5eaf372014-05-09 18:00:32 +0000118 (long) cinfo->max_v_samp_factor);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000119 /* Mark component needed, until color conversion says otherwise */
120 compptr->component_needed = TRUE;
121 /* Mark no quantization table yet saved for component */
122 compptr->quant_table = NULL;
123 }
124
125 /* Compute number of fully interleaved MCU rows. */
126 cinfo->total_iMCU_rows = (JDIMENSION)
127 jdiv_round_up((long) cinfo->image_height,
DRCe5eaf372014-05-09 18:00:32 +0000128 (long) (cinfo->max_v_samp_factor*DCTSIZE));
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000129
130 /* Decide whether file contains multiple scans */
131 if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
132 cinfo->inputctl->has_multiple_scans = TRUE;
133 else
134 cinfo->inputctl->has_multiple_scans = FALSE;
135}
136
137
Thomas G. Lane489583f1996-02-07 00:00:00 +0000138LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000139per_scan_setup (j_decompress_ptr cinfo)
140/* Do computations that are needed before processing a JPEG scan */
141/* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
142{
143 int ci, mcublks, tmp;
144 jpeg_component_info *compptr;
DRCe5eaf372014-05-09 18:00:32 +0000145
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000146 if (cinfo->comps_in_scan == 1) {
DRCe5eaf372014-05-09 18:00:32 +0000147
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000148 /* Noninterleaved (single-component) scan */
149 compptr = cinfo->cur_comp_info[0];
DRCe5eaf372014-05-09 18:00:32 +0000150
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000151 /* Overall image size in MCUs */
152 cinfo->MCUs_per_row = compptr->width_in_blocks;
153 cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
DRCe5eaf372014-05-09 18:00:32 +0000154
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000155 /* For noninterleaved scan, always one block per MCU */
156 compptr->MCU_width = 1;
157 compptr->MCU_height = 1;
158 compptr->MCU_blocks = 1;
DRC49967cd2010-10-09 19:57:51 +0000159 compptr->MCU_sample_width = compptr->_DCT_scaled_size;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000160 compptr->last_col_width = 1;
161 /* For noninterleaved scans, it is convenient to define last_row_height
162 * as the number of block rows present in the last iMCU row.
163 */
164 tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
165 if (tmp == 0) tmp = compptr->v_samp_factor;
166 compptr->last_row_height = tmp;
DRCe5eaf372014-05-09 18:00:32 +0000167
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000168 /* Prepare array describing MCU composition */
169 cinfo->blocks_in_MCU = 1;
170 cinfo->MCU_membership[0] = 0;
DRCe5eaf372014-05-09 18:00:32 +0000171
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000172 } else {
DRCe5eaf372014-05-09 18:00:32 +0000173
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000174 /* Interleaved (multi-component) scan */
175 if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
176 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
DRCe5eaf372014-05-09 18:00:32 +0000177 MAX_COMPS_IN_SCAN);
178
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000179 /* Overall image size in MCUs */
180 cinfo->MCUs_per_row = (JDIMENSION)
181 jdiv_round_up((long) cinfo->image_width,
DRCe5eaf372014-05-09 18:00:32 +0000182 (long) (cinfo->max_h_samp_factor*DCTSIZE));
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000183 cinfo->MCU_rows_in_scan = (JDIMENSION)
184 jdiv_round_up((long) cinfo->image_height,
DRCe5eaf372014-05-09 18:00:32 +0000185 (long) (cinfo->max_v_samp_factor*DCTSIZE));
186
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000187 cinfo->blocks_in_MCU = 0;
DRCe5eaf372014-05-09 18:00:32 +0000188
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000189 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
190 compptr = cinfo->cur_comp_info[ci];
191 /* Sampling factors give # of blocks of component in each MCU */
192 compptr->MCU_width = compptr->h_samp_factor;
193 compptr->MCU_height = compptr->v_samp_factor;
194 compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
DRC49967cd2010-10-09 19:57:51 +0000195 compptr->MCU_sample_width = compptr->MCU_width * compptr->_DCT_scaled_size;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000196 /* Figure number of non-dummy blocks in last MCU column & row */
197 tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
198 if (tmp == 0) tmp = compptr->MCU_width;
199 compptr->last_col_width = tmp;
200 tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
201 if (tmp == 0) tmp = compptr->MCU_height;
202 compptr->last_row_height = tmp;
203 /* Prepare array describing MCU composition */
204 mcublks = compptr->MCU_blocks;
205 if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
DRCe5eaf372014-05-09 18:00:32 +0000206 ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000207 while (mcublks-- > 0) {
DRCe5eaf372014-05-09 18:00:32 +0000208 cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000209 }
210 }
DRCe5eaf372014-05-09 18:00:32 +0000211
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000212 }
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
Thomas G. Lane489583f1996-02-07 00:00:00 +0000237LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000238latch_quant_tables (j_decompress_ptr cinfo)
239{
240 int ci, qtblno;
241 jpeg_component_info *compptr;
DRCbd498032016-02-19 08:53:33 -0600242 JQUANT_TBL *qtbl;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000243
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 ||
DRCe5eaf372014-05-09 18:00:32 +0000252 cinfo->quant_tbl_ptrs[qtblno] == NULL)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000253 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,
DRC5de454b2014-05-18 19:04:03 +0000257 sizeof(JQUANT_TBL));
258 MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], sizeof(JQUANT_TBL));
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000259 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
Thomas G. Lane489583f1996-02-07 00:00:00 +0000271METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000272start_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}
280
281
282/*
283 * Finish up after inputting a compressed-data scan.
284 * This is called by the coefficient controller after it's read all
285 * the expected data of the scan.
286 */
287
Thomas G. Lane489583f1996-02-07 00:00:00 +0000288METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000289finish_input_pass (j_decompress_ptr cinfo)
290{
291 cinfo->inputctl->consume_input = consume_markers;
292}
293
294
295/*
296 * Read JPEG markers before, between, or after compressed-data scans.
297 * Change state as necessary when a new scan is reached.
298 * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
299 *
300 * The consume_input method pointer points either here or to the
301 * coefficient controller's consume_data routine, depending on whether
302 * we are reading a compressed data segment or inter-segment markers.
303 */
304
Thomas G. Lane489583f1996-02-07 00:00:00 +0000305METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000306consume_markers (j_decompress_ptr cinfo)
307{
308 my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
309 int val;
310
311 if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
312 return JPEG_REACHED_EOI;
313
314 val = (*cinfo->marker->read_markers) (cinfo);
315
316 switch (val) {
DRCe5eaf372014-05-09 18:00:32 +0000317 case JPEG_REACHED_SOS: /* Found SOS */
318 if (inputctl->inheaders) { /* 1st SOS */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000319 initial_setup(cinfo);
320 inputctl->inheaders = FALSE;
321 /* Note: start_input_pass must be called by jdmaster.c
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000322 * before any more input can be consumed. jdapimin.c is
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000323 * responsible for enforcing this sequencing.
324 */
DRCe5eaf372014-05-09 18:00:32 +0000325 } else { /* 2nd or later SOS marker */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000326 if (! inputctl->pub.has_multiple_scans)
DRCe5eaf372014-05-09 18:00:32 +0000327 ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000328 start_input_pass(cinfo);
329 }
330 break;
DRCe5eaf372014-05-09 18:00:32 +0000331 case JPEG_REACHED_EOI: /* Found EOI */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000332 inputctl->pub.eoi_reached = TRUE;
DRCe5eaf372014-05-09 18:00:32 +0000333 if (inputctl->inheaders) { /* Tables-only datastream, apparently */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000334 if (cinfo->marker->saw_SOF)
DRCe5eaf372014-05-09 18:00:32 +0000335 ERREXIT(cinfo, JERR_SOF_NO_SOS);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000336 } else {
337 /* Prevent infinite loop in coef ctlr's decompress_data routine
338 * if user set output_scan_number larger than number of scans.
339 */
340 if (cinfo->output_scan_number > cinfo->input_scan_number)
DRCe5eaf372014-05-09 18:00:32 +0000341 cinfo->output_scan_number = cinfo->input_scan_number;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000342 }
343 break;
344 case JPEG_SUSPENDED:
345 break;
346 }
347
348 return val;
349}
350
351
352/*
353 * Reset state to begin a fresh datastream.
354 */
355
Thomas G. Lane489583f1996-02-07 00:00:00 +0000356METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000357reset_input_controller (j_decompress_ptr cinfo)
358{
359 my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
360
361 inputctl->pub.consume_input = consume_markers;
362 inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
363 inputctl->pub.eoi_reached = FALSE;
364 inputctl->inheaders = TRUE;
365 /* Reset other modules */
366 (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
367 (*cinfo->marker->reset_marker_reader) (cinfo);
368 /* Reset progression state -- would be cleaner if entropy decoder did this */
369 cinfo->coef_bits = NULL;
370}
371
372
373/*
374 * Initialize the input controller module.
375 * This is called only once, when the decompression object is created.
376 */
377
Thomas G. Lane489583f1996-02-07 00:00:00 +0000378GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000379jinit_input_controller (j_decompress_ptr cinfo)
380{
381 my_inputctl_ptr inputctl;
382
383 /* Create subobject in permanent pool */
384 inputctl = (my_inputctl_ptr)
385 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
DRC5de454b2014-05-18 19:04:03 +0000386 sizeof(my_input_controller));
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000387 cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
388 /* Initialize method pointers */
389 inputctl->pub.consume_input = consume_markers;
390 inputctl->pub.reset_input_controller = reset_input_controller;
391 inputctl->pub.start_input_pass = start_input_pass;
392 inputctl->pub.finish_input_pass = finish_input_pass;
393 /* Initialize state: can't use reset_input_controller since we don't
394 * want to try to reset other modules yet.
395 */
396 inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
397 inputctl->pub.eoi_reached = FALSE;
398 inputctl->inheaders = TRUE;
399}