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 master control logic for the JPEG decompressor.
9 * These routines are concerned with selecting the modules to be executed
10 * and with determining the number of passes and the work to be done in each
14 #define JPEG_INTERNALS
22 struct jpeg_decomp_master pub; /* public fields */
24 int pass_number; /* # of passes completed */
26 boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
28 /* Saved references to initialized quantizer modules,
29 * in case we need to switch modes.
31 struct jpeg_color_quantizer * quantizer_1pass;
32 struct jpeg_color_quantizer * quantizer_2pass;
35 typedef my_decomp_master * my_master_ptr;
39 * Determine whether merged upsample/color conversion should be used.
40 * CRUCIAL: this must match the actual capabilities of jdmerge.c!
44 use_merged_upsample (j_decompress_ptr cinfo)
46 #ifdef UPSAMPLE_MERGING_SUPPORTED
47 /* Merging is the equivalent of plain box-filter upsampling */
48 if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
50 /* jdmerge.c only supports YCC=>RGB color conversion */
51 if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
52 cinfo->out_color_space != JCS_RGB ||
53 cinfo->out_color_components != RGB_PIXELSIZE)
55 /* and it only handles 2h1v or 2h2v sampling ratios */
56 if (cinfo->comp_info[0].h_samp_factor != 2 ||
57 cinfo->comp_info[1].h_samp_factor != 1 ||
58 cinfo->comp_info[2].h_samp_factor != 1 ||
59 cinfo->comp_info[0].v_samp_factor > 2 ||
60 cinfo->comp_info[1].v_samp_factor != 1 ||
61 cinfo->comp_info[2].v_samp_factor != 1)
63 /* furthermore, it doesn't work if each component has been
64 processed differently */
65 if (cinfo->comp_info[0].codec_data_unit != cinfo->min_codec_data_unit ||
66 cinfo->comp_info[1].codec_data_unit != cinfo->min_codec_data_unit ||
67 cinfo->comp_info[2].codec_data_unit != cinfo->min_codec_data_unit)
69 /* ??? also need to test for upsample-time rescaling, when & if supported */
70 return TRUE; /* by golly, it'll work... */
78 * Compute output image dimensions and related values.
79 * NOTE: this is exported for possible use by application.
80 * Hence it mustn't do anything that can't be done twice.
81 * Also note that it may be called before the master module is initialized!
85 jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
86 /* Do computations that are needed before master selection phase */
88 /* Prevent application from calling me at wrong times */
89 if (cinfo->global_state != DSTATE_READY)
90 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
92 (*cinfo->codec->calc_output_dimensions) (cinfo);
94 /* Report number of components in selected colorspace. */
95 /* Probably this should be in the color conversion module... */
96 switch (cinfo->out_color_space) {
98 cinfo->out_color_components = 1;
101 #if RGB_PIXELSIZE != 3
102 cinfo->out_color_components = RGB_PIXELSIZE;
104 #endif /* else share code with YCbCr */
106 cinfo->out_color_components = 3;
110 cinfo->out_color_components = 4;
112 default: /* else must be same colorspace as in file */
113 cinfo->out_color_components = cinfo->num_components;
116 cinfo->output_components = (cinfo->quantize_colors ? 1 :
117 cinfo->out_color_components);
119 /* See if upsampler will want to emit more than one row at a time */
120 if (use_merged_upsample(cinfo))
121 cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
123 cinfo->rec_outbuf_height = 1;
128 * Several decompression processes need to range-limit values to the range
129 * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
130 * due to noise introduced by quantization, roundoff error, etc. These
131 * processes are inner loops and need to be as fast as possible. On most
132 * machines, particularly CPUs with pipelines or instruction prefetch,
133 * a (subscript-check-less) C table lookup
134 * x = sample_range_limit[x];
135 * is faster than explicit tests
137 * else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
138 * These processes all use a common table prepared by the routine below.
140 * For most steps we can mathematically guarantee that the initial value
141 * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
142 * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial
143 * limiting step (just after the IDCT), a wildly out-of-range value is
144 * possible if the input data is corrupt. To avoid any chance of indexing
145 * off the end of memory and getting a bad-pointer trap, we perform the
146 * post-IDCT limiting thus:
147 * x = range_limit[x & MASK];
148 * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
149 * samples. Under normal circumstances this is more than enough range and
150 * a correct output will be generated; with bogus input data the mask will
151 * cause wraparound, and we will safely generate a bogus-but-in-range output.
152 * For the post-IDCT step, we want to convert the data from signed to unsigned
153 * representation by adding CENTERJSAMPLE at the same time that we limit it.
154 * So the post-IDCT limiting table ends up looking like this:
155 * CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
156 * MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
157 * 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
158 * 0,1,...,CENTERJSAMPLE-1
159 * Negative inputs select values from the upper half of the table after
162 * We can save some space by overlapping the start of the post-IDCT table
163 * with the simpler range limiting table. The post-IDCT table begins at
164 * sample_range_limit + CENTERJSAMPLE.
166 * Note that the table is allocated in near data space on PCs; it's small
167 * enough and used often enough to justify this.
171 prepare_range_limit_table (j_decompress_ptr cinfo)
172 /* Allocate and fill in the sample_range_limit table */
178 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
179 (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
180 table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */
181 cinfo->sample_range_limit = table;
182 /* First segment of "simple" table: limit[x] = 0 for x < 0 */
183 MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
184 /* Main part of "simple" table: limit[x] = x */
185 for (i = 0; i <= MAXJSAMPLE; i++)
186 table[i] = (JSAMPLE) i;
187 table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
188 /* End of simple table, rest of first half of post-IDCT table */
189 for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
190 table[i] = MAXJSAMPLE;
191 /* Second half of post-IDCT table */
192 MEMZERO(table + (2 * (MAXJSAMPLE+1)),
193 (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
194 MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
195 cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
200 * Master selection of decompression modules.
201 * This is done once at jpeg_start_decompress time. We determine
202 * which modules will be used and give them appropriate initialization calls.
203 * We also initialize the decompressor input side to begin consuming data.
205 * Since jpeg_read_header has finished, we know what is in the SOF
206 * and (first) SOS markers. We also have all the application parameter
211 master_selection (j_decompress_ptr cinfo)
213 my_master_ptr master = (my_master_ptr) cinfo->master;
215 JDIMENSION jd_samplesperrow;
217 /* Initialize dimensions and other stuff */
218 jpeg_calc_output_dimensions(cinfo);
219 prepare_range_limit_table(cinfo);
221 /* Width of an output scanline must be representable as JDIMENSION. */
222 samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
223 jd_samplesperrow = (JDIMENSION) samplesperrow;
224 if ((long) jd_samplesperrow != samplesperrow)
225 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
227 /* Initialize my private state */
228 master->pass_number = 0;
229 master->using_merged_upsample = use_merged_upsample(cinfo);
231 /* Color quantizer selection */
232 master->quantizer_1pass = NULL;
233 master->quantizer_2pass = NULL;
234 /* No mode changes if not using buffered-image mode. */
235 if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
236 cinfo->enable_1pass_quant = FALSE;
237 cinfo->enable_external_quant = FALSE;
238 cinfo->enable_2pass_quant = FALSE;
240 if (cinfo->quantize_colors) {
241 if (cinfo->raw_data_out)
242 ERREXIT(cinfo, JERR_NOTIMPL);
243 /* 2-pass quantizer only works in 3-component color space. */
244 if (cinfo->out_color_components != 3) {
245 cinfo->enable_1pass_quant = TRUE;
246 cinfo->enable_external_quant = FALSE;
247 cinfo->enable_2pass_quant = FALSE;
248 cinfo->colormap = NULL;
249 } else if (cinfo->colormap != NULL) {
250 cinfo->enable_external_quant = TRUE;
251 } else if (cinfo->two_pass_quantize) {
252 cinfo->enable_2pass_quant = TRUE;
254 cinfo->enable_1pass_quant = TRUE;
257 if (cinfo->enable_1pass_quant) {
258 #ifdef QUANT_1PASS_SUPPORTED
259 jinit_1pass_quantizer(cinfo);
260 master->quantizer_1pass = cinfo->cquantize;
262 ERREXIT(cinfo, JERR_NOT_COMPILED);
266 /* We use the 2-pass code to map to external colormaps. */
267 if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
268 #ifdef QUANT_2PASS_SUPPORTED
269 jinit_2pass_quantizer(cinfo);
270 master->quantizer_2pass = cinfo->cquantize;
272 ERREXIT(cinfo, JERR_NOT_COMPILED);
275 /* If both quantizers are initialized, the 2-pass one is left active;
276 * this is necessary for starting with quantization to an external map.
280 /* Post-processing: in particular, color conversion first */
281 if (! cinfo->raw_data_out) {
282 if (master->using_merged_upsample) {
283 #ifdef UPSAMPLE_MERGING_SUPPORTED
284 jinit_merged_upsampler(cinfo); /* does color conversion too */
286 ERREXIT(cinfo, JERR_NOT_COMPILED);
289 jinit_color_deconverter(cinfo);
290 jinit_upsampler(cinfo);
292 jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
295 /* Initialize principal buffer controllers. */
296 if (! cinfo->raw_data_out)
297 jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
299 /* We can now tell the memory manager to allocate virtual arrays. */
300 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
302 /* Initialize input side of decompressor to consume first scan. */
303 (*cinfo->inputctl->start_input_pass) (cinfo);
305 #ifdef D_MULTISCAN_FILES_SUPPORTED
306 /* If jpeg_start_decompress will read the whole file, initialize
307 * progress monitoring appropriately. The input step is counted
310 if (cinfo->progress != NULL && ! cinfo->buffered_image &&
311 cinfo->inputctl->has_multiple_scans) {
313 /* Estimate number of scans to set pass_limit. */
314 if (cinfo->process == JPROC_PROGRESSIVE) {
315 /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
316 nscans = 2 + 3 * cinfo->num_components;
318 /* For a nonprogressive multiscan file, estimate 1 scan per component. */
319 nscans = cinfo->num_components;
321 cinfo->progress->pass_counter = 0L;
322 cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
323 cinfo->progress->completed_passes = 0;
324 cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
325 /* Count the input pass as done */
326 master->pass_number++;
328 #endif /* D_MULTISCAN_FILES_SUPPORTED */
334 * This is called at the beginning of each output pass. We determine which
335 * modules will be active during this pass and give them appropriate
336 * start_pass calls. We also set is_dummy_pass to indicate whether this
337 * is a "real" output pass or a dummy pass for color quantization.
338 * (In the latter case, jdapistd.c will crank the pass to completion.)
342 prepare_for_output_pass (j_decompress_ptr cinfo)
344 my_master_ptr master = (my_master_ptr) cinfo->master;
346 if (master->pub.is_dummy_pass) {
347 #ifdef QUANT_2PASS_SUPPORTED
348 /* Final pass of 2-pass quantization */
349 master->pub.is_dummy_pass = FALSE;
350 (*cinfo->cquantize->start_pass) (cinfo, FALSE);
351 (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
352 (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
354 ERREXIT(cinfo, JERR_NOT_COMPILED);
355 #endif /* QUANT_2PASS_SUPPORTED */
357 if (cinfo->quantize_colors && cinfo->colormap == NULL) {
358 /* Select new quantization method */
359 if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
360 cinfo->cquantize = master->quantizer_2pass;
361 master->pub.is_dummy_pass = TRUE;
362 } else if (cinfo->enable_1pass_quant) {
363 cinfo->cquantize = master->quantizer_1pass;
365 ERREXIT(cinfo, JERR_MODE_CHANGE);
368 (*cinfo->codec->start_output_pass) (cinfo);
369 if (! cinfo->raw_data_out) {
370 if (! master->using_merged_upsample)
371 (*cinfo->cconvert->start_pass) (cinfo);
372 (*cinfo->upsample->start_pass) (cinfo);
373 if (cinfo->quantize_colors)
374 (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
375 (*cinfo->post->start_pass) (cinfo,
376 (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
377 (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
381 /* Set up progress monitor's pass info if present */
382 if (cinfo->progress != NULL) {
383 cinfo->progress->completed_passes = master->pass_number;
384 cinfo->progress->total_passes = master->pass_number +
385 (master->pub.is_dummy_pass ? 2 : 1);
386 /* In buffered-image mode, we assume one more output pass if EOI not
387 * yet reached, but no more passes if EOI has been reached.
389 if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
390 cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
397 * Finish up at end of an output pass.
401 finish_output_pass (j_decompress_ptr cinfo)
403 my_master_ptr master = (my_master_ptr) cinfo->master;
405 if (cinfo->quantize_colors)
406 (*cinfo->cquantize->finish_pass) (cinfo);
407 master->pass_number++;
411 #ifdef D_MULTISCAN_FILES_SUPPORTED
414 * Switch to a new external colormap between output passes.
418 jpeg_new_colormap (j_decompress_ptr cinfo)
420 my_master_ptr master = (my_master_ptr) cinfo->master;
422 /* Prevent application from calling me at wrong times */
423 if (cinfo->global_state != DSTATE_BUFIMAGE)
424 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
426 if (cinfo->quantize_colors && cinfo->enable_external_quant &&
427 cinfo->colormap != NULL) {
428 /* Select 2-pass quantizer for external colormap use */
429 cinfo->cquantize = master->quantizer_2pass;
430 /* Notify quantizer of colormap change */
431 (*cinfo->cquantize->new_color_map) (cinfo);
432 master->pub.is_dummy_pass = FALSE; /* just in case */
434 ERREXIT(cinfo, JERR_MODE_CHANGE);
437 #endif /* D_MULTISCAN_FILES_SUPPORTED */
441 * Initialize master decompression control and select active modules.
442 * This is performed at the start of jpeg_start_decompress.
446 jinit_master_decompress (j_decompress_ptr cinfo)
448 my_master_ptr master;
450 master = (my_master_ptr)
451 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
452 SIZEOF(my_decomp_master));
453 cinfo->master = (struct jpeg_decomp_master *) master;
454 master->pub.prepare_for_output_pass = prepare_for_output_pass;
455 master->pub.finish_output_pass = finish_output_pass;
457 master->pub.is_dummy_pass = FALSE;
459 master_selection(cinfo);