4 * Copyright (C) 1991-1997, Thomas G. Lane.
5 * Modified 2003-2010 by Guido Vollbeding.
6 * Copyright (C) 2010, D. R. Commander.
7 * This file is part of the Independent JPEG Group's software.
8 * For conditions of distribution and use, see the accompanying README file.
10 * This file contains master control logic for the JPEG compressor.
11 * These routines are concerned with parameter validation, initial setup,
12 * and inter-pass control (determining the number of passes and the work
13 * to be done in each pass).
16 #define JPEG_INTERNALS
25 main_pass, /* input data, also do first output step */
26 huff_opt_pass, /* Huffman code optimization pass */
27 output_pass /* data output pass */
31 struct jpeg_comp_master pub; /* public fields */
33 c_pass_type pass_type; /* the type of the current pass */
35 int pass_number; /* # of passes completed */
36 int total_passes; /* total # of passes needed */
38 int scan_number; /* current index in scan_info[] */
41 typedef my_comp_master * my_master_ptr;
45 * Support routines that do various essential calculations.
48 #if JPEG_LIB_VERSION >= 70
50 * Compute JPEG image dimensions and related values.
51 * NOTE: this is exported for possible use by application.
52 * Hence it mustn't do anything that can't be done twice.
56 jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo)
57 /* Do computations that are needed before master selection phase */
59 /* Hardwire it to "no scaling" */
60 cinfo->jpeg_width = cinfo->image_width;
61 cinfo->jpeg_height = cinfo->image_height;
62 cinfo->min_DCT_h_scaled_size = DCTSIZE;
63 cinfo->min_DCT_v_scaled_size = DCTSIZE;
69 initial_setup (j_compress_ptr cinfo, boolean transcode_only)
70 /* Do computations that are needed before master selection phase */
73 jpeg_component_info *compptr;
75 JDIMENSION jd_samplesperrow;
77 #if JPEG_LIB_VERSION >= 70
78 #if JPEG_LIB_VERSION >= 80
81 jpeg_calc_jpeg_dimensions(cinfo);
84 /* Sanity check on image dimensions */
85 if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0
86 || cinfo->num_components <= 0 || cinfo->input_components <= 0)
87 ERREXIT(cinfo, JERR_EMPTY_IMAGE);
89 /* Make sure image isn't bigger than I can handle */
90 if ((long) cinfo->_jpeg_height > (long) JPEG_MAX_DIMENSION ||
91 (long) cinfo->_jpeg_width > (long) JPEG_MAX_DIMENSION)
92 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
94 /* Width of an input scanline must be representable as JDIMENSION. */
95 samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
96 jd_samplesperrow = (JDIMENSION) samplesperrow;
97 if ((long) jd_samplesperrow != samplesperrow)
98 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
100 /* For now, precision must match compiled-in value... */
101 if (cinfo->data_precision != BITS_IN_JSAMPLE)
102 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
104 /* Check that number of components won't exceed internal array sizes */
105 if (cinfo->num_components > MAX_COMPONENTS)
106 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
109 /* Compute maximum sampling factors; check factor validity */
110 cinfo->max_h_samp_factor = 1;
111 cinfo->max_v_samp_factor = 1;
112 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
114 if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
115 compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
116 ERREXIT(cinfo, JERR_BAD_SAMPLING);
117 cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
118 compptr->h_samp_factor);
119 cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
120 compptr->v_samp_factor);
123 /* Compute dimensions of components */
124 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
126 /* Fill in the correct component_index value; don't rely on application */
127 compptr->component_index = ci;
128 /* For compression, we never do DCT scaling. */
129 #if JPEG_LIB_VERSION >= 70
130 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE;
132 compptr->DCT_scaled_size = DCTSIZE;
134 /* Size in DCT blocks */
135 compptr->width_in_blocks = (JDIMENSION)
136 jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor,
137 (long) (cinfo->max_h_samp_factor * DCTSIZE));
138 compptr->height_in_blocks = (JDIMENSION)
139 jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor,
140 (long) (cinfo->max_v_samp_factor * DCTSIZE));
141 /* Size in samples */
142 compptr->downsampled_width = (JDIMENSION)
143 jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor,
144 (long) cinfo->max_h_samp_factor);
145 compptr->downsampled_height = (JDIMENSION)
146 jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor,
147 (long) cinfo->max_v_samp_factor);
148 /* Mark component needed (this flag isn't actually used for compression) */
149 compptr->component_needed = TRUE;
152 /* Compute number of fully interleaved MCU rows (number of times that
153 * main controller will call coefficient controller).
155 cinfo->total_iMCU_rows = (JDIMENSION)
156 jdiv_round_up((long) cinfo->_jpeg_height,
157 (long) (cinfo->max_v_samp_factor*DCTSIZE));
161 #ifdef C_MULTISCAN_FILES_SUPPORTED
164 validate_script (j_compress_ptr cinfo)
165 /* Verify that the scan script in cinfo->scan_info[] is valid; also
166 * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
169 const jpeg_scan_info * scanptr;
170 int scanno, ncomps, ci, coefi, thisi;
172 boolean component_sent[MAX_COMPONENTS];
173 #ifdef C_PROGRESSIVE_SUPPORTED
174 int * last_bitpos_ptr;
175 int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
176 /* -1 until that coefficient has been seen; then last Al for it */
179 if (cinfo->num_scans <= 0)
180 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
182 /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
183 * for progressive JPEG, no scan can have this.
185 scanptr = cinfo->scan_info;
186 if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
187 #ifdef C_PROGRESSIVE_SUPPORTED
188 cinfo->progressive_mode = TRUE;
189 last_bitpos_ptr = & last_bitpos[0][0];
190 for (ci = 0; ci < cinfo->num_components; ci++)
191 for (coefi = 0; coefi < DCTSIZE2; coefi++)
192 *last_bitpos_ptr++ = -1;
194 ERREXIT(cinfo, JERR_NOT_COMPILED);
197 cinfo->progressive_mode = FALSE;
198 for (ci = 0; ci < cinfo->num_components; ci++)
199 component_sent[ci] = FALSE;
202 for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
203 /* Validate component indexes */
204 ncomps = scanptr->comps_in_scan;
205 if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
206 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
207 for (ci = 0; ci < ncomps; ci++) {
208 thisi = scanptr->component_index[ci];
209 if (thisi < 0 || thisi >= cinfo->num_components)
210 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
211 /* Components must appear in SOF order within each scan */
212 if (ci > 0 && thisi <= scanptr->component_index[ci-1])
213 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
215 /* Validate progression parameters */
220 if (cinfo->progressive_mode) {
221 #ifdef C_PROGRESSIVE_SUPPORTED
222 /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
223 * seems wrong: the upper bound ought to depend on data precision.
224 * Perhaps they really meant 0..N+1 for N-bit precision.
225 * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
226 * out-of-range reconstructed DC values during the first DC scan,
227 * which might cause problems for some decoders.
229 #if BITS_IN_JSAMPLE == 8
234 if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
235 Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
236 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
238 if (Se != 0) /* DC and AC together not OK */
239 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
241 if (ncomps != 1) /* AC scans must be for only one component */
242 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
244 for (ci = 0; ci < ncomps; ci++) {
245 last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
246 if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
247 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
248 for (coefi = Ss; coefi <= Se; coefi++) {
249 if (last_bitpos_ptr[coefi] < 0) {
250 /* first scan of this coefficient */
252 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
255 if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
256 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
258 last_bitpos_ptr[coefi] = Al;
263 /* For sequential JPEG, all progression parameters must be these: */
264 if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
265 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
266 /* Make sure components are not sent twice */
267 for (ci = 0; ci < ncomps; ci++) {
268 thisi = scanptr->component_index[ci];
269 if (component_sent[thisi])
270 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
271 component_sent[thisi] = TRUE;
276 /* Now verify that everything got sent. */
277 if (cinfo->progressive_mode) {
278 #ifdef C_PROGRESSIVE_SUPPORTED
279 /* For progressive mode, we only check that at least some DC data
280 * got sent for each component; the spec does not require that all bits
281 * of all coefficients be transmitted. Would it be wiser to enforce
282 * transmission of all coefficient bits??
284 for (ci = 0; ci < cinfo->num_components; ci++) {
285 if (last_bitpos[ci][0] < 0)
286 ERREXIT(cinfo, JERR_MISSING_DATA);
290 for (ci = 0; ci < cinfo->num_components; ci++) {
291 if (! component_sent[ci])
292 ERREXIT(cinfo, JERR_MISSING_DATA);
297 #endif /* C_MULTISCAN_FILES_SUPPORTED */
301 select_scan_parameters (j_compress_ptr cinfo)
302 /* Set up the scan parameters for the current scan */
306 #ifdef C_MULTISCAN_FILES_SUPPORTED
307 if (cinfo->scan_info != NULL) {
308 /* Prepare for current scan --- the script is already validated */
309 my_master_ptr master = (my_master_ptr) cinfo->master;
310 const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
312 cinfo->comps_in_scan = scanptr->comps_in_scan;
313 for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
314 cinfo->cur_comp_info[ci] =
315 &cinfo->comp_info[scanptr->component_index[ci]];
317 cinfo->Ss = scanptr->Ss;
318 cinfo->Se = scanptr->Se;
319 cinfo->Ah = scanptr->Ah;
320 cinfo->Al = scanptr->Al;
325 /* Prepare for single sequential-JPEG scan containing all components */
326 if (cinfo->num_components > MAX_COMPS_IN_SCAN)
327 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
329 cinfo->comps_in_scan = cinfo->num_components;
330 for (ci = 0; ci < cinfo->num_components; ci++) {
331 cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
334 cinfo->Se = DCTSIZE2-1;
342 per_scan_setup (j_compress_ptr cinfo)
343 /* Do computations that are needed before processing a JPEG scan */
344 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
346 int ci, mcublks, tmp;
347 jpeg_component_info *compptr;
349 if (cinfo->comps_in_scan == 1) {
351 /* Noninterleaved (single-component) scan */
352 compptr = cinfo->cur_comp_info[0];
354 /* Overall image size in MCUs */
355 cinfo->MCUs_per_row = compptr->width_in_blocks;
356 cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
358 /* For noninterleaved scan, always one block per MCU */
359 compptr->MCU_width = 1;
360 compptr->MCU_height = 1;
361 compptr->MCU_blocks = 1;
362 compptr->MCU_sample_width = DCTSIZE;
363 compptr->last_col_width = 1;
364 /* For noninterleaved scans, it is convenient to define last_row_height
365 * as the number of block rows present in the last iMCU row.
367 tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
368 if (tmp == 0) tmp = compptr->v_samp_factor;
369 compptr->last_row_height = tmp;
371 /* Prepare array describing MCU composition */
372 cinfo->blocks_in_MCU = 1;
373 cinfo->MCU_membership[0] = 0;
377 /* Interleaved (multi-component) scan */
378 if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
379 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
382 /* Overall image size in MCUs */
383 cinfo->MCUs_per_row = (JDIMENSION)
384 jdiv_round_up((long) cinfo->_jpeg_width,
385 (long) (cinfo->max_h_samp_factor*DCTSIZE));
386 cinfo->MCU_rows_in_scan = (JDIMENSION)
387 jdiv_round_up((long) cinfo->_jpeg_height,
388 (long) (cinfo->max_v_samp_factor*DCTSIZE));
390 cinfo->blocks_in_MCU = 0;
392 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
393 compptr = cinfo->cur_comp_info[ci];
394 /* Sampling factors give # of blocks of component in each MCU */
395 compptr->MCU_width = compptr->h_samp_factor;
396 compptr->MCU_height = compptr->v_samp_factor;
397 compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
398 compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
399 /* Figure number of non-dummy blocks in last MCU column & row */
400 tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
401 if (tmp == 0) tmp = compptr->MCU_width;
402 compptr->last_col_width = tmp;
403 tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
404 if (tmp == 0) tmp = compptr->MCU_height;
405 compptr->last_row_height = tmp;
406 /* Prepare array describing MCU composition */
407 mcublks = compptr->MCU_blocks;
408 if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
409 ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
410 while (mcublks-- > 0) {
411 cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
417 /* Convert restart specified in rows to actual MCU count. */
418 /* Note that count must fit in 16 bits, so we provide limiting. */
419 if (cinfo->restart_in_rows > 0) {
420 long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
421 cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
428 * This is called at the beginning of each pass. We determine which modules
429 * will be active during this pass and give them appropriate start_pass calls.
430 * We also set is_last_pass to indicate whether any more passes will be
435 prepare_for_pass (j_compress_ptr cinfo)
437 my_master_ptr master = (my_master_ptr) cinfo->master;
439 switch (master->pass_type) {
441 /* Initial pass: will collect input data, and do either Huffman
442 * optimization or data output for the first scan.
444 select_scan_parameters(cinfo);
445 per_scan_setup(cinfo);
446 if (! cinfo->raw_data_in) {
447 (*cinfo->cconvert->start_pass) (cinfo);
448 (*cinfo->downsample->start_pass) (cinfo);
449 (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
451 (*cinfo->fdct->start_pass) (cinfo);
452 (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
453 (*cinfo->coef->start_pass) (cinfo,
454 (master->total_passes > 1 ?
455 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
456 (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
457 if (cinfo->optimize_coding) {
458 /* No immediate data output; postpone writing frame/scan headers */
459 master->pub.call_pass_startup = FALSE;
461 /* Will write frame/scan headers at first jpeg_write_scanlines call */
462 master->pub.call_pass_startup = TRUE;
465 #ifdef ENTROPY_OPT_SUPPORTED
467 /* Do Huffman optimization for a scan after the first one. */
468 select_scan_parameters(cinfo);
469 per_scan_setup(cinfo);
470 if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
471 (*cinfo->entropy->start_pass) (cinfo, TRUE);
472 (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
473 master->pub.call_pass_startup = FALSE;
476 /* Special case: Huffman DC refinement scans need no Huffman table
477 * and therefore we can skip the optimization pass for them.
479 master->pass_type = output_pass;
480 master->pass_number++;
484 /* Do a data-output pass. */
485 /* We need not repeat per-scan setup if prior optimization pass did it. */
486 if (! cinfo->optimize_coding) {
487 select_scan_parameters(cinfo);
488 per_scan_setup(cinfo);
490 (*cinfo->entropy->start_pass) (cinfo, FALSE);
491 (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
492 /* We emit frame/scan headers now */
493 if (master->scan_number == 0)
494 (*cinfo->marker->write_frame_header) (cinfo);
495 (*cinfo->marker->write_scan_header) (cinfo);
496 master->pub.call_pass_startup = FALSE;
499 ERREXIT(cinfo, JERR_NOT_COMPILED);
502 master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
504 /* Set up progress monitor's pass info if present */
505 if (cinfo->progress != NULL) {
506 cinfo->progress->completed_passes = master->pass_number;
507 cinfo->progress->total_passes = master->total_passes;
513 * Special start-of-pass hook.
514 * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
515 * In single-pass processing, we need this hook because we don't want to
516 * write frame/scan headers during jpeg_start_compress; we want to let the
517 * application write COM markers etc. between jpeg_start_compress and the
518 * jpeg_write_scanlines loop.
519 * In multi-pass processing, this routine is not used.
523 pass_startup (j_compress_ptr cinfo)
525 cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
527 (*cinfo->marker->write_frame_header) (cinfo);
528 (*cinfo->marker->write_scan_header) (cinfo);
533 * Finish up at end of pass.
537 finish_pass_master (j_compress_ptr cinfo)
539 my_master_ptr master = (my_master_ptr) cinfo->master;
541 /* The entropy coder always needs an end-of-pass call,
542 * either to analyze statistics or to flush its output buffer.
544 (*cinfo->entropy->finish_pass) (cinfo);
546 /* Update state for next pass */
547 switch (master->pass_type) {
549 /* next pass is either output of scan 0 (after optimization)
550 * or output of scan 1 (if no optimization).
552 master->pass_type = output_pass;
553 if (! cinfo->optimize_coding)
554 master->scan_number++;
557 /* next pass is always output of current scan */
558 master->pass_type = output_pass;
561 /* next pass is either optimization or output of next scan */
562 if (cinfo->optimize_coding)
563 master->pass_type = huff_opt_pass;
564 master->scan_number++;
568 master->pass_number++;
573 * Initialize master compression control.
577 jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
579 my_master_ptr master;
581 master = (my_master_ptr)
582 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
583 SIZEOF(my_comp_master));
584 cinfo->master = (struct jpeg_comp_master *) master;
585 master->pub.prepare_for_pass = prepare_for_pass;
586 master->pub.pass_startup = pass_startup;
587 master->pub.finish_pass = finish_pass_master;
588 master->pub.is_last_pass = FALSE;
590 /* Validate parameters, determine derived values */
591 initial_setup(cinfo, transcode_only);
593 if (cinfo->scan_info != NULL) {
594 #ifdef C_MULTISCAN_FILES_SUPPORTED
595 validate_script(cinfo);
597 ERREXIT(cinfo, JERR_NOT_COMPILED);
600 cinfo->progressive_mode = FALSE;
601 cinfo->num_scans = 1;
604 if (cinfo->progressive_mode) /* TEMPORARY HACK ??? */
605 cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */
607 /* Initialize my private state */
608 if (transcode_only) {
609 /* no main pass in transcoding */
610 if (cinfo->optimize_coding)
611 master->pass_type = huff_opt_pass;
613 master->pass_type = output_pass;
615 /* for normal compression, first pass is always this type: */
616 master->pass_type = main_pass;
618 master->scan_number = 0;
619 master->pass_number = 0;
620 if (cinfo->optimize_coding)
621 master->total_passes = cinfo->num_scans * 2;
623 master->total_passes = cinfo->num_scans;