4 * Copyright (C) 1991-1998, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
8 * This file contains input control logic for the JPEG decompressor.
9 * These routines are concerned with controlling the decompressor's input
10 * processing (marker reading and coefficient/difference decoding).
11 * The actual input reading is done in jdmarker.c, jdhuff.c, jdphuff.c,
15 #define JPEG_INTERNALS
23 struct jpeg_input_controller pub; /* public fields */
25 boolean inheaders; /* TRUE until first SOS is reached */
26 } my_input_controller;
28 typedef my_input_controller * my_inputctl_ptr;
31 /* Forward declarations */
32 METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
36 * Routines to calculate various quantities related to the size of the image.
40 initial_setup (j_decompress_ptr cinfo)
41 /* Called once, when first SOS marker is reached */
44 jpeg_component_info *compptr;
46 /* Make sure image isn't bigger than I can handle */
47 if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
48 (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
49 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
51 if (cinfo->process == JPROC_LOSSLESS) {
52 /* If precision > compiled-in value, we must downscale */
53 if (cinfo->data_precision > BITS_IN_JSAMPLE)
54 WARNMS2(cinfo, JWRN_MUST_DOWNSCALE,
55 cinfo->data_precision, BITS_IN_JSAMPLE);
57 else { /* Lossy processes */
58 /* For now, precision must match compiled-in value... */
59 if (cinfo->data_precision != BITS_IN_JSAMPLE)
60 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
63 /* Check that number of components won't exceed internal array sizes */
64 if (cinfo->num_components > MAX_COMPONENTS)
65 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
68 /* Compute maximum sampling factors; check factor validity */
69 cinfo->max_h_samp_factor = 1;
70 cinfo->max_v_samp_factor = 1;
71 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
73 if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
74 compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
75 ERREXIT(cinfo, JERR_BAD_SAMPLING);
76 cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
77 compptr->h_samp_factor);
78 cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
79 compptr->v_samp_factor);
82 /* We initialize codec_data_unit and min_codec_data_unit to data_unit.
83 * In the full decompressor, this will be overridden by jdmaster.c;
84 * but in the transcoder, jdmaster.c is not used, so we must do it here.
86 cinfo->min_codec_data_unit = cinfo->data_unit;
88 /* Compute dimensions of components */
89 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
91 compptr->codec_data_unit = cinfo->data_unit;
92 /* Size in data units */
93 compptr->width_in_data_units = (JDIMENSION)
94 jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
95 (long) (cinfo->max_h_samp_factor * cinfo->data_unit));
96 compptr->height_in_data_units = (JDIMENSION)
97 jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
98 (long) (cinfo->max_v_samp_factor * cinfo->data_unit));
99 /* downsampled_width and downsampled_height will also be overridden by
100 * jdmaster.c if we are doing full decompression. The transcoder library
101 * doesn't use these values, but the calling application might.
103 /* Size in samples */
104 compptr->downsampled_width = (JDIMENSION)
105 jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
106 (long) cinfo->max_h_samp_factor);
107 compptr->downsampled_height = (JDIMENSION)
108 jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
109 (long) cinfo->max_v_samp_factor);
110 /* Mark component needed, until color conversion says otherwise */
111 compptr->component_needed = TRUE;
112 /* Mark no quantization table yet saved for component */
113 compptr->quant_table = NULL;
116 /* Compute number of fully interleaved MCU rows. */
117 cinfo->total_iMCU_rows = (JDIMENSION)
118 jdiv_round_up((long) cinfo->image_height,
119 (long) (cinfo->max_v_samp_factor*cinfo->data_unit));
121 /* Decide whether file contains multiple scans */
122 if (cinfo->comps_in_scan < cinfo->num_components ||
123 cinfo->process == JPROC_PROGRESSIVE)
124 cinfo->inputctl->has_multiple_scans = TRUE;
126 cinfo->inputctl->has_multiple_scans = FALSE;
131 per_scan_setup (j_decompress_ptr cinfo)
132 /* Do computations that are needed before processing a JPEG scan */
133 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
135 int ci, mcublks, tmp;
136 jpeg_component_info *compptr;
138 if (cinfo->comps_in_scan == 1) {
140 /* Noninterleaved (single-component) scan */
141 compptr = cinfo->cur_comp_info[0];
143 /* Overall image size in MCUs */
144 cinfo->MCUs_per_row = compptr->width_in_data_units;
145 cinfo->MCU_rows_in_scan = compptr->height_in_data_units;
147 /* For noninterleaved scan, always one data unit per MCU */
148 compptr->MCU_width = 1;
149 compptr->MCU_height = 1;
150 compptr->MCU_data_units = 1;
151 compptr->MCU_sample_width = compptr->codec_data_unit;
152 compptr->last_col_width = 1;
153 /* For noninterleaved scans, it is convenient to define last_row_height
154 * as the number of data unit rows present in the last iMCU row.
156 tmp = (int) (compptr->height_in_data_units % compptr->v_samp_factor);
157 if (tmp == 0) tmp = compptr->v_samp_factor;
158 compptr->last_row_height = tmp;
160 /* Prepare array describing MCU composition */
161 cinfo->data_units_in_MCU = 1;
162 cinfo->MCU_membership[0] = 0;
166 /* Interleaved (multi-component) scan */
167 if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
168 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
171 /* Overall image size in MCUs */
172 cinfo->MCUs_per_row = (JDIMENSION)
173 jdiv_round_up((long) cinfo->image_width,
174 (long) (cinfo->max_h_samp_factor*cinfo->data_unit));
175 cinfo->MCU_rows_in_scan = (JDIMENSION)
176 jdiv_round_up((long) cinfo->image_height,
177 (long) (cinfo->max_v_samp_factor*cinfo->data_unit));
179 cinfo->data_units_in_MCU = 0;
181 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
182 compptr = cinfo->cur_comp_info[ci];
183 /* Sampling factors give # of data units of component in each MCU */
184 compptr->MCU_width = compptr->h_samp_factor;
185 compptr->MCU_height = compptr->v_samp_factor;
186 compptr->MCU_data_units = compptr->MCU_width * compptr->MCU_height;
187 compptr->MCU_sample_width = compptr->MCU_width * compptr->codec_data_unit;
188 /* Figure number of non-dummy data units in last MCU column & row */
189 tmp = (int) (compptr->width_in_data_units % compptr->MCU_width);
190 if (tmp == 0) tmp = compptr->MCU_width;
191 compptr->last_col_width = tmp;
192 tmp = (int) (compptr->height_in_data_units % compptr->MCU_height);
193 if (tmp == 0) tmp = compptr->MCU_height;
194 compptr->last_row_height = tmp;
195 /* Prepare array describing MCU composition */
196 mcublks = compptr->MCU_data_units;
197 if (cinfo->data_units_in_MCU + mcublks > D_MAX_DATA_UNITS_IN_MCU)
198 ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
199 while (mcublks-- > 0) {
200 cinfo->MCU_membership[cinfo->data_units_in_MCU++] = ci;
209 * Initialize the input modules to read a scan of compressed data.
210 * The first call to this is done by jdmaster.c after initializing
211 * the entire decompressor (during jpeg_start_decompress).
212 * Subsequent calls come from consume_markers, below.
216 start_input_pass (j_decompress_ptr cinfo)
218 per_scan_setup(cinfo);
219 (*cinfo->codec->start_input_pass) (cinfo);
220 cinfo->inputctl->consume_input = cinfo->codec->consume_data;
225 * Finish up after inputting a compressed-data scan.
226 * This is called by the coefficient controller after it's read all
227 * the expected data of the scan.
231 finish_input_pass (j_decompress_ptr cinfo)
233 cinfo->inputctl->consume_input = consume_markers;
238 * Read JPEG markers before, between, or after compressed-data scans.
239 * Change state as necessary when a new scan is reached.
240 * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
242 * The consume_input method pointer points either here or to the
243 * coefficient controller's consume_data routine, depending on whether
244 * we are reading a compressed data segment or inter-segment markers.
248 consume_markers (j_decompress_ptr cinfo)
250 my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
253 if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
254 return JPEG_REACHED_EOI;
256 val = (*cinfo->marker->read_markers) (cinfo);
259 case JPEG_REACHED_SOS: /* Found SOS */
260 if (inputctl->inheaders) { /* 1st SOS */
261 initial_setup(cinfo);
263 * Initialize the decompression codec. We need to do this here so that
264 * any codec-specific fields and function pointers are available to
265 * the rest of the library.
267 jinit_d_codec(cinfo);
268 inputctl->inheaders = FALSE;
269 /* Note: start_input_pass must be called by jdmaster.c
270 * before any more input can be consumed. jdapimin.c is
271 * responsible for enforcing this sequencing.
273 } else { /* 2nd or later SOS marker */
274 if (! inputctl->pub.has_multiple_scans)
275 ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
276 start_input_pass(cinfo);
279 case JPEG_REACHED_EOI: /* Found EOI */
280 inputctl->pub.eoi_reached = TRUE;
281 if (inputctl->inheaders) { /* Tables-only datastream, apparently */
282 if (cinfo->marker->saw_SOF)
283 ERREXIT(cinfo, JERR_SOF_NO_SOS);
285 /* Prevent infinite loop in coef ctlr's decompress_data routine
286 * if user set output_scan_number larger than number of scans.
288 if (cinfo->output_scan_number > cinfo->input_scan_number)
289 cinfo->output_scan_number = cinfo->input_scan_number;
301 * Reset state to begin a fresh datastream.
305 reset_input_controller (j_decompress_ptr cinfo)
307 my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
309 inputctl->pub.consume_input = consume_markers;
310 inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
311 inputctl->pub.eoi_reached = FALSE;
312 inputctl->inheaders = TRUE;
313 /* Reset other modules */
314 (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
315 (*cinfo->marker->reset_marker_reader) (cinfo);
316 /* Reset progression state -- would be cleaner if entropy decoder did this */
317 cinfo->coef_bits = NULL;
322 * Initialize the input controller module.
323 * This is called only once, when the decompression object is created.
327 jinit_input_controller (j_decompress_ptr cinfo)
329 my_inputctl_ptr inputctl;
331 /* Create subobject in permanent pool */
332 inputctl = (my_inputctl_ptr)
333 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
334 SIZEOF(my_input_controller));
335 cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
336 /* Initialize method pointers */
337 inputctl->pub.consume_input = consume_markers;
338 inputctl->pub.reset_input_controller = reset_input_controller;
339 inputctl->pub.start_input_pass = start_input_pass;
340 inputctl->pub.finish_input_pass = finish_input_pass;
341 /* Initialize state: can't use reset_input_controller since we don't
342 * want to try to reset other modules yet.
344 inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
345 inputctl->pub.eoi_reached = FALSE;
346 inputctl->inheaders = TRUE;