blob: 9fcd089d3cf6ea8345fe016a1fc1159183184453 [file] [log] [blame]
Thomas G. Lanebc79e061995-08-02 00:00:00 +00001/*
2 * jdinput.c
3 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00004 * Copyright (C) 1991-1997, Thomas G. Lane.
Guido Vollbeding5996a252009-06-27 00:00:00 +00005 * Modified 2002-2009 by Guido Vollbeding.
DRC36a6eec2010-10-08 08:05:44 +00006 * Copyright (C) 2010, D. R. Commander.
Thomas G. Lanebc79e061995-08-02 00:00:00 +00007 * This file is part of the Independent JPEG Group's software.
8 * For conditions of distribution and use, see the accompanying README file.
9 *
10 * This file contains input control logic for the JPEG decompressor.
11 * These routines are concerned with controlling the decompressor's input
12 * processing (marker reading and coefficient decoding). The actual input
13 * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
14 */
15
16#define JPEG_INTERNALS
17#include "jinclude.h"
18#include "jpeglib.h"
DRC36a6eec2010-10-08 08:05:44 +000019#include "jpegcomp.h"
Thomas G. Lanebc79e061995-08-02 00:00:00 +000020
21
22/* Private state */
23
24typedef struct {
25 struct jpeg_input_controller pub; /* public fields */
26
27 boolean inheaders; /* TRUE until first SOS is reached */
28} my_input_controller;
29
30typedef my_input_controller * my_inputctl_ptr;
31
32
33/* Forward declarations */
Thomas G. Lane489583f1996-02-07 00:00:00 +000034METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
Thomas G. Lanebc79e061995-08-02 00:00:00 +000035
36
37/*
38 * Routines to calculate various quantities related to the size of the image.
39 */
40
Guido Vollbeding989630f2010-01-10 00:00:00 +000041
DRC36a6eec2010-10-08 08:05:44 +000042#if JPEG_LIB_VERSION >= 80
Guido Vollbeding989630f2010-01-10 00:00:00 +000043/*
44 * Compute output image dimensions and related values.
45 * NOTE: this is exported for possible use by application.
46 * Hence it mustn't do anything that can't be done twice.
47 */
48
49GLOBAL(void)
50jpeg_core_output_dimensions (j_decompress_ptr cinfo)
51/* Do computations that are needed before master selection phase.
52 * This function is used for transcoding and full decompression.
53 */
54{
55#ifdef IDCT_SCALING_SUPPORTED
56 int ci;
57 jpeg_component_info *compptr;
58
59 /* Compute actual output image dimensions and DCT scaling choices. */
60 if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom) {
61 /* Provide 1/block_size scaling */
62 cinfo->output_width = (JDIMENSION)
63 jdiv_round_up((long) cinfo->image_width, (long) cinfo->block_size);
64 cinfo->output_height = (JDIMENSION)
65 jdiv_round_up((long) cinfo->image_height, (long) cinfo->block_size);
66 cinfo->min_DCT_h_scaled_size = 1;
67 cinfo->min_DCT_v_scaled_size = 1;
68 } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 2) {
69 /* Provide 2/block_size scaling */
70 cinfo->output_width = (JDIMENSION)
71 jdiv_round_up((long) cinfo->image_width * 2L, (long) cinfo->block_size);
72 cinfo->output_height = (JDIMENSION)
73 jdiv_round_up((long) cinfo->image_height * 2L, (long) cinfo->block_size);
74 cinfo->min_DCT_h_scaled_size = 2;
75 cinfo->min_DCT_v_scaled_size = 2;
Guido Vollbeding989630f2010-01-10 00:00:00 +000076 } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 4) {
77 /* Provide 4/block_size scaling */
78 cinfo->output_width = (JDIMENSION)
79 jdiv_round_up((long) cinfo->image_width * 4L, (long) cinfo->block_size);
80 cinfo->output_height = (JDIMENSION)
81 jdiv_round_up((long) cinfo->image_height * 4L, (long) cinfo->block_size);
82 cinfo->min_DCT_h_scaled_size = 4;
83 cinfo->min_DCT_v_scaled_size = 4;
Guido Vollbeding989630f2010-01-10 00:00:00 +000084 } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 8) {
85 /* Provide 8/block_size scaling */
86 cinfo->output_width = (JDIMENSION)
87 jdiv_round_up((long) cinfo->image_width * 8L, (long) cinfo->block_size);
88 cinfo->output_height = (JDIMENSION)
89 jdiv_round_up((long) cinfo->image_height * 8L, (long) cinfo->block_size);
90 cinfo->min_DCT_h_scaled_size = 8;
91 cinfo->min_DCT_v_scaled_size = 8;
Guido Vollbeding989630f2010-01-10 00:00:00 +000092 }
Guido Vollbeding989630f2010-01-10 00:00:00 +000093 /* Recompute dimensions of components */
94 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
95 ci++, compptr++) {
96 compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size;
97 compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size;
98 }
99
100#else /* !IDCT_SCALING_SUPPORTED */
101
102 /* Hardwire it to "no scaling" */
103 cinfo->output_width = cinfo->image_width;
104 cinfo->output_height = cinfo->image_height;
105 /* jdinput.c has already initialized DCT_scaled_size,
106 * and has computed unscaled downsampled_width and downsampled_height.
107 */
108
109#endif /* IDCT_SCALING_SUPPORTED */
110}
DRC36a6eec2010-10-08 08:05:44 +0000111#endif
Guido Vollbeding989630f2010-01-10 00:00:00 +0000112
113
Thomas G. Lane489583f1996-02-07 00:00:00 +0000114LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000115initial_setup (j_decompress_ptr cinfo)
116/* Called once, when first SOS marker is reached */
117{
118 int ci;
119 jpeg_component_info *compptr;
120
121 /* Make sure image isn't bigger than I can handle */
122 if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
123 (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
124 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
125
126 /* For now, precision must match compiled-in value... */
127 if (cinfo->data_precision != BITS_IN_JSAMPLE)
128 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
129
130 /* Check that number of components won't exceed internal array sizes */
131 if (cinfo->num_components > MAX_COMPONENTS)
132 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
133 MAX_COMPONENTS);
134
135 /* Compute maximum sampling factors; check factor validity */
136 cinfo->max_h_samp_factor = 1;
137 cinfo->max_v_samp_factor = 1;
138 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
139 ci++, compptr++) {
140 if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
141 compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
142 ERREXIT(cinfo, JERR_BAD_SAMPLING);
143 cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
144 compptr->h_samp_factor);
145 cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
146 compptr->v_samp_factor);
147 }
148
DRC36a6eec2010-10-08 08:05:44 +0000149#if JPEG_LIB_VERSION >=80
Guido Vollbeding989630f2010-01-10 00:00:00 +0000150 cinfo->block_size = DCTSIZE;
151 cinfo->natural_order = jpeg_natural_order;
152 cinfo->lim_Se = DCTSIZE2-1;
DRC36a6eec2010-10-08 08:05:44 +0000153#endif
Guido Vollbeding989630f2010-01-10 00:00:00 +0000154
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000155 /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
156 * In the full decompressor, this will be overridden by jdmaster.c;
157 * but in the transcoder, jdmaster.c is not used, so we must do it here.
158 */
DRC36a6eec2010-10-08 08:05:44 +0000159#if JPEG_LIB_VERSION >= 70
160 cinfo->min_DCT_h_scaled_size = cinfo->min_DCT_v_scaled_size = DCTSIZE;
161#else
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000162 cinfo->min_DCT_scaled_size = DCTSIZE;
DRC36a6eec2010-10-08 08:05:44 +0000163#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000164
165 /* Compute dimensions of components */
166 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
167 ci++, compptr++) {
DRC36a6eec2010-10-08 08:05:44 +0000168#if JPEG_LIB_VERSION >= 70
169 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE;
170#else
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000171 compptr->DCT_scaled_size = DCTSIZE;
DRC36a6eec2010-10-08 08:05:44 +0000172#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000173 /* Size in DCT blocks */
174 compptr->width_in_blocks = (JDIMENSION)
175 jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
176 (long) (cinfo->max_h_samp_factor * DCTSIZE));
177 compptr->height_in_blocks = (JDIMENSION)
178 jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
179 (long) (cinfo->max_v_samp_factor * DCTSIZE));
180 /* downsampled_width and downsampled_height will also be overridden by
181 * jdmaster.c if we are doing full decompression. The transcoder library
182 * doesn't use these values, but the calling application might.
183 */
184 /* Size in samples */
185 compptr->downsampled_width = (JDIMENSION)
186 jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
187 (long) cinfo->max_h_samp_factor);
188 compptr->downsampled_height = (JDIMENSION)
189 jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
190 (long) cinfo->max_v_samp_factor);
191 /* Mark component needed, until color conversion says otherwise */
192 compptr->component_needed = TRUE;
193 /* Mark no quantization table yet saved for component */
194 compptr->quant_table = NULL;
195 }
196
197 /* Compute number of fully interleaved MCU rows. */
198 cinfo->total_iMCU_rows = (JDIMENSION)
199 jdiv_round_up((long) cinfo->image_height,
200 (long) (cinfo->max_v_samp_factor*DCTSIZE));
201
202 /* Decide whether file contains multiple scans */
203 if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
204 cinfo->inputctl->has_multiple_scans = TRUE;
205 else
206 cinfo->inputctl->has_multiple_scans = FALSE;
207}
208
209
Thomas G. Lane489583f1996-02-07 00:00:00 +0000210LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000211per_scan_setup (j_decompress_ptr cinfo)
212/* Do computations that are needed before processing a JPEG scan */
213/* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
214{
215 int ci, mcublks, tmp;
216 jpeg_component_info *compptr;
217
218 if (cinfo->comps_in_scan == 1) {
219
220 /* Noninterleaved (single-component) scan */
221 compptr = cinfo->cur_comp_info[0];
222
223 /* Overall image size in MCUs */
224 cinfo->MCUs_per_row = compptr->width_in_blocks;
225 cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
226
227 /* For noninterleaved scan, always one block per MCU */
228 compptr->MCU_width = 1;
229 compptr->MCU_height = 1;
230 compptr->MCU_blocks = 1;
DRC49967cd2010-10-09 19:57:51 +0000231 compptr->MCU_sample_width = compptr->_DCT_scaled_size;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000232 compptr->last_col_width = 1;
233 /* For noninterleaved scans, it is convenient to define last_row_height
234 * as the number of block rows present in the last iMCU row.
235 */
236 tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
237 if (tmp == 0) tmp = compptr->v_samp_factor;
238 compptr->last_row_height = tmp;
239
240 /* Prepare array describing MCU composition */
241 cinfo->blocks_in_MCU = 1;
242 cinfo->MCU_membership[0] = 0;
243
244 } else {
245
246 /* Interleaved (multi-component) scan */
247 if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
248 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
249 MAX_COMPS_IN_SCAN);
250
251 /* Overall image size in MCUs */
252 cinfo->MCUs_per_row = (JDIMENSION)
253 jdiv_round_up((long) cinfo->image_width,
254 (long) (cinfo->max_h_samp_factor*DCTSIZE));
255 cinfo->MCU_rows_in_scan = (JDIMENSION)
256 jdiv_round_up((long) cinfo->image_height,
257 (long) (cinfo->max_v_samp_factor*DCTSIZE));
258
259 cinfo->blocks_in_MCU = 0;
260
261 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
262 compptr = cinfo->cur_comp_info[ci];
263 /* Sampling factors give # of blocks of component in each MCU */
264 compptr->MCU_width = compptr->h_samp_factor;
265 compptr->MCU_height = compptr->v_samp_factor;
266 compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
DRC49967cd2010-10-09 19:57:51 +0000267 compptr->MCU_sample_width = compptr->MCU_width * compptr->_DCT_scaled_size;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000268 /* Figure number of non-dummy blocks in last MCU column & row */
269 tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
270 if (tmp == 0) tmp = compptr->MCU_width;
271 compptr->last_col_width = tmp;
272 tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
273 if (tmp == 0) tmp = compptr->MCU_height;
274 compptr->last_row_height = tmp;
275 /* Prepare array describing MCU composition */
276 mcublks = compptr->MCU_blocks;
277 if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
278 ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
279 while (mcublks-- > 0) {
280 cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
281 }
282 }
283
284 }
285}
286
287
288/*
289 * Save away a copy of the Q-table referenced by each component present
290 * in the current scan, unless already saved during a prior scan.
291 *
292 * In a multiple-scan JPEG file, the encoder could assign different components
293 * the same Q-table slot number, but change table definitions between scans
294 * so that each component uses a different Q-table. (The IJG encoder is not
295 * currently capable of doing this, but other encoders might.) Since we want
296 * to be able to dequantize all the components at the end of the file, this
297 * means that we have to save away the table actually used for each component.
298 * We do this by copying the table at the start of the first scan containing
299 * the component.
300 * The JPEG spec prohibits the encoder from changing the contents of a Q-table
301 * slot between scans of a component using that slot. If the encoder does so
302 * anyway, this decoder will simply use the Q-table values that were current
303 * at the start of the first scan for the component.
304 *
305 * The decompressor output side looks only at the saved quant tables,
306 * not at the current Q-table slots.
307 */
308
Thomas G. Lane489583f1996-02-07 00:00:00 +0000309LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000310latch_quant_tables (j_decompress_ptr cinfo)
311{
312 int ci, qtblno;
313 jpeg_component_info *compptr;
314 JQUANT_TBL * qtbl;
315
316 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
317 compptr = cinfo->cur_comp_info[ci];
318 /* No work if we already saved Q-table for this component */
319 if (compptr->quant_table != NULL)
320 continue;
321 /* Make sure specified quantization table is present */
322 qtblno = compptr->quant_tbl_no;
323 if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
324 cinfo->quant_tbl_ptrs[qtblno] == NULL)
325 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
326 /* OK, save away the quantization table */
327 qtbl = (JQUANT_TBL *)
328 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
329 SIZEOF(JQUANT_TBL));
330 MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
331 compptr->quant_table = qtbl;
332 }
333}
334
335
336/*
337 * Initialize the input modules to read a scan of compressed data.
338 * The first call to this is done by jdmaster.c after initializing
339 * the entire decompressor (during jpeg_start_decompress).
340 * Subsequent calls come from consume_markers, below.
341 */
342
Thomas G. Lane489583f1996-02-07 00:00:00 +0000343METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000344start_input_pass (j_decompress_ptr cinfo)
345{
346 per_scan_setup(cinfo);
347 latch_quant_tables(cinfo);
348 (*cinfo->entropy->start_pass) (cinfo);
349 (*cinfo->coef->start_input_pass) (cinfo);
350 cinfo->inputctl->consume_input = cinfo->coef->consume_data;
351}
352
353
354/*
355 * Finish up after inputting a compressed-data scan.
356 * This is called by the coefficient controller after it's read all
357 * the expected data of the scan.
358 */
359
Thomas G. Lane489583f1996-02-07 00:00:00 +0000360METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000361finish_input_pass (j_decompress_ptr cinfo)
362{
363 cinfo->inputctl->consume_input = consume_markers;
364}
365
366
367/*
368 * Read JPEG markers before, between, or after compressed-data scans.
369 * Change state as necessary when a new scan is reached.
370 * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
371 *
372 * The consume_input method pointer points either here or to the
373 * coefficient controller's consume_data routine, depending on whether
374 * we are reading a compressed data segment or inter-segment markers.
375 */
376
Thomas G. Lane489583f1996-02-07 00:00:00 +0000377METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000378consume_markers (j_decompress_ptr cinfo)
379{
380 my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
381 int val;
382
383 if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
384 return JPEG_REACHED_EOI;
385
386 val = (*cinfo->marker->read_markers) (cinfo);
387
388 switch (val) {
389 case JPEG_REACHED_SOS: /* Found SOS */
390 if (inputctl->inheaders) { /* 1st SOS */
391 initial_setup(cinfo);
392 inputctl->inheaders = FALSE;
393 /* Note: start_input_pass must be called by jdmaster.c
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000394 * before any more input can be consumed. jdapimin.c is
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000395 * responsible for enforcing this sequencing.
396 */
397 } else { /* 2nd or later SOS marker */
398 if (! inputctl->pub.has_multiple_scans)
399 ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
400 start_input_pass(cinfo);
401 }
402 break;
403 case JPEG_REACHED_EOI: /* Found EOI */
404 inputctl->pub.eoi_reached = TRUE;
405 if (inputctl->inheaders) { /* Tables-only datastream, apparently */
406 if (cinfo->marker->saw_SOF)
407 ERREXIT(cinfo, JERR_SOF_NO_SOS);
408 } else {
409 /* Prevent infinite loop in coef ctlr's decompress_data routine
410 * if user set output_scan_number larger than number of scans.
411 */
412 if (cinfo->output_scan_number > cinfo->input_scan_number)
413 cinfo->output_scan_number = cinfo->input_scan_number;
414 }
415 break;
416 case JPEG_SUSPENDED:
417 break;
418 }
419
420 return val;
421}
422
423
424/*
425 * Reset state to begin a fresh datastream.
426 */
427
Thomas G. Lane489583f1996-02-07 00:00:00 +0000428METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000429reset_input_controller (j_decompress_ptr cinfo)
430{
431 my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
432
433 inputctl->pub.consume_input = consume_markers;
434 inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
435 inputctl->pub.eoi_reached = FALSE;
436 inputctl->inheaders = TRUE;
437 /* Reset other modules */
438 (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
439 (*cinfo->marker->reset_marker_reader) (cinfo);
440 /* Reset progression state -- would be cleaner if entropy decoder did this */
441 cinfo->coef_bits = NULL;
442}
443
444
445/*
446 * Initialize the input controller module.
447 * This is called only once, when the decompression object is created.
448 */
449
Thomas G. Lane489583f1996-02-07 00:00:00 +0000450GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000451jinit_input_controller (j_decompress_ptr cinfo)
452{
453 my_inputctl_ptr inputctl;
454
455 /* Create subobject in permanent pool */
456 inputctl = (my_inputctl_ptr)
457 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
458 SIZEOF(my_input_controller));
459 cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
460 /* Initialize method pointers */
461 inputctl->pub.consume_input = consume_markers;
462 inputctl->pub.reset_input_controller = reset_input_controller;
463 inputctl->pub.start_input_pass = start_input_pass;
464 inputctl->pub.finish_input_pass = finish_input_pass;
465 /* Initialize state: can't use reset_input_controller since we don't
466 * want to try to reset other modules yet.
467 */
468 inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
469 inputctl->pub.eoi_reached = FALSE;
470 inputctl->inheaders = TRUE;
471}