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