4 * Copyright (C) 1991-1997, 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 compressor.
9 * These routines are concerned with parameter validation, initial setup,
10 * and inter-pass control (determining the number of passes and the work
11 * to be done in each pass).
14 #define JPEG_INTERNALS
22 main_pass
, /* input data, also do first output step */
23 huff_opt_pass
, /* Huffman code optimization pass */
24 output_pass
/* data output pass */
28 struct jpeg_comp_master pub
; /* public fields */
30 c_pass_type pass_type
; /* the type of the current pass */
32 int pass_number
; /* # of passes completed */
33 int total_passes
; /* total # of passes needed */
35 int scan_number
; /* current index in scan_info[] */
38 typedef my_comp_master
* my_master_ptr
;
42 * Support routines that do various essential calculations.
46 initial_setup (j_compress_ptr cinfo
)
47 /* Do computations that are needed before master selection phase */
50 jpeg_component_info
*compptr
;
52 JDIMENSION jd_samplesperrow
;
54 /* Sanity check on image dimensions */
55 if (cinfo
->image_height
<= 0 || cinfo
->image_width
<= 0
56 || cinfo
->num_components
<= 0 || cinfo
->input_components
<= 0)
57 ERREXIT(cinfo
, JERR_EMPTY_IMAGE
);
59 /* Make sure image isn't bigger than I can handle */
60 if ((long) cinfo
->image_height
> (long) JPEG_MAX_DIMENSION
||
61 (long) cinfo
->image_width
> (long) JPEG_MAX_DIMENSION
)
62 ERREXIT1(cinfo
, JERR_IMAGE_TOO_BIG
, (unsigned int) JPEG_MAX_DIMENSION
);
64 /* Width of an input scanline must be representable as JDIMENSION. */
65 samplesperrow
= (long) cinfo
->image_width
* (long) cinfo
->input_components
;
66 jd_samplesperrow
= (JDIMENSION
) samplesperrow
;
67 if ((long) jd_samplesperrow
!= samplesperrow
)
68 ERREXIT(cinfo
, JERR_WIDTH_OVERFLOW
);
70 /* For now, precision must match compiled-in value... */
71 if (cinfo
->data_precision
!= BITS_IN_JSAMPLE
)
72 ERREXIT1(cinfo
, JERR_BAD_PRECISION
, cinfo
->data_precision
);
74 /* Check that number of components won't exceed internal array sizes */
75 if (cinfo
->num_components
> MAX_COMPONENTS
)
76 ERREXIT2(cinfo
, JERR_COMPONENT_COUNT
, cinfo
->num_components
,
79 /* Compute maximum sampling factors; check factor validity */
80 cinfo
->max_h_samp_factor
= 1;
81 cinfo
->max_v_samp_factor
= 1;
82 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
84 if (compptr
->h_samp_factor
<=0 || compptr
->h_samp_factor
>MAX_SAMP_FACTOR
||
85 compptr
->v_samp_factor
<=0 || compptr
->v_samp_factor
>MAX_SAMP_FACTOR
)
86 ERREXIT(cinfo
, JERR_BAD_SAMPLING
);
87 cinfo
->max_h_samp_factor
= MAX(cinfo
->max_h_samp_factor
,
88 compptr
->h_samp_factor
);
89 cinfo
->max_v_samp_factor
= MAX(cinfo
->max_v_samp_factor
,
90 compptr
->v_samp_factor
);
93 /* Compute dimensions of components */
94 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
96 /* Fill in the correct component_index value; don't rely on application */
97 compptr
->component_index
= ci
;
98 /* For compression, we never do DCT scaling. */
99 compptr
->DCT_scaled_size
= DCTSIZE
;
100 /* Size in DCT blocks */
101 compptr
->width_in_blocks
= (JDIMENSION
)
102 jdiv_round_up((long) cinfo
->image_width
* (long) compptr
->h_samp_factor
,
103 (long) (cinfo
->max_h_samp_factor
* DCTSIZE
));
104 compptr
->height_in_blocks
= (JDIMENSION
)
105 jdiv_round_up((long) cinfo
->image_height
* (long) compptr
->v_samp_factor
,
106 (long) (cinfo
->max_v_samp_factor
* DCTSIZE
));
107 /* Size in samples */
108 compptr
->downsampled_width
= (JDIMENSION
)
109 jdiv_round_up((long) cinfo
->image_width
* (long) compptr
->h_samp_factor
,
110 (long) cinfo
->max_h_samp_factor
);
111 compptr
->downsampled_height
= (JDIMENSION
)
112 jdiv_round_up((long) cinfo
->image_height
* (long) compptr
->v_samp_factor
,
113 (long) cinfo
->max_v_samp_factor
);
114 /* Mark component needed (this flag isn't actually used for compression) */
115 compptr
->component_needed
= TRUE
;
118 /* Compute number of fully interleaved MCU rows (number of times that
119 * main controller will call coefficient controller).
121 cinfo
->total_iMCU_rows
= (JDIMENSION
)
122 jdiv_round_up((long) cinfo
->image_height
,
123 (long) (cinfo
->max_v_samp_factor
*DCTSIZE
));
127 #ifdef C_MULTISCAN_FILES_SUPPORTED
130 validate_script (j_compress_ptr cinfo
)
131 /* Verify that the scan script in cinfo->scan_info[] is valid; also
132 * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
135 const jpeg_scan_info
* scanptr
;
136 int scanno
, ncomps
, ci
, coefi
, thisi
;
138 boolean component_sent
[MAX_COMPONENTS
];
139 #ifdef C_PROGRESSIVE_SUPPORTED
140 int * last_bitpos_ptr
;
141 int last_bitpos
[MAX_COMPONENTS
][DCTSIZE2
];
142 /* -1 until that coefficient has been seen; then last Al for it */
145 if (cinfo
->num_scans
<= 0)
146 ERREXIT1(cinfo
, JERR_BAD_SCAN_SCRIPT
, 0);
148 /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
149 * for progressive JPEG, no scan can have this.
151 scanptr
= cinfo
->scan_info
;
152 if (scanptr
->Ss
!= 0 || scanptr
->Se
!= DCTSIZE2
-1) {
153 #ifdef C_PROGRESSIVE_SUPPORTED
154 cinfo
->progressive_mode
= TRUE
;
155 last_bitpos_ptr
= & last_bitpos
[0][0];
156 for (ci
= 0; ci
< cinfo
->num_components
; ci
++)
157 for (coefi
= 0; coefi
< DCTSIZE2
; coefi
++)
158 *last_bitpos_ptr
++ = -1;
160 ERREXIT(cinfo
, JERR_NOT_COMPILED
);
163 cinfo
->progressive_mode
= FALSE
;
164 for (ci
= 0; ci
< cinfo
->num_components
; ci
++)
165 component_sent
[ci
] = FALSE
;
168 for (scanno
= 1; scanno
<= cinfo
->num_scans
; scanptr
++, scanno
++) {
169 /* Validate component indexes */
170 ncomps
= scanptr
->comps_in_scan
;
171 if (ncomps
<= 0 || ncomps
> MAX_COMPS_IN_SCAN
)
172 ERREXIT2(cinfo
, JERR_COMPONENT_COUNT
, ncomps
, MAX_COMPS_IN_SCAN
);
173 for (ci
= 0; ci
< ncomps
; ci
++) {
174 thisi
= scanptr
->component_index
[ci
];
175 if (thisi
< 0 || thisi
>= cinfo
->num_components
)
176 ERREXIT1(cinfo
, JERR_BAD_SCAN_SCRIPT
, scanno
);
177 /* Components must appear in SOF order within each scan */
178 if (ci
> 0 && thisi
<= scanptr
->component_index
[ci
-1])
179 ERREXIT1(cinfo
, JERR_BAD_SCAN_SCRIPT
, scanno
);
181 /* Validate progression parameters */
186 if (cinfo
->progressive_mode
) {
187 #ifdef C_PROGRESSIVE_SUPPORTED
188 /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
189 * seems wrong: the upper bound ought to depend on data precision.
190 * Perhaps they really meant 0..N+1 for N-bit precision.
191 * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
192 * out-of-range reconstructed DC values during the first DC scan,
193 * which might cause problems for some decoders.
195 #if BITS_IN_JSAMPLE == 8
200 if (Ss
< 0 || Ss
>= DCTSIZE2
|| Se
< Ss
|| Se
>= DCTSIZE2
||
201 Ah
< 0 || Ah
> MAX_AH_AL
|| Al
< 0 || Al
> MAX_AH_AL
)
202 ERREXIT1(cinfo
, JERR_BAD_PROG_SCRIPT
, scanno
);
204 if (Se
!= 0) /* DC and AC together not OK */
205 ERREXIT1(cinfo
, JERR_BAD_PROG_SCRIPT
, scanno
);
207 if (ncomps
!= 1) /* AC scans must be for only one component */
208 ERREXIT1(cinfo
, JERR_BAD_PROG_SCRIPT
, scanno
);
210 for (ci
= 0; ci
< ncomps
; ci
++) {
211 last_bitpos_ptr
= & last_bitpos
[scanptr
->component_index
[ci
]][0];
212 if (Ss
!= 0 && last_bitpos_ptr
[0] < 0) /* AC without prior DC scan */
213 ERREXIT1(cinfo
, JERR_BAD_PROG_SCRIPT
, scanno
);
214 for (coefi
= Ss
; coefi
<= Se
; coefi
++) {
215 if (last_bitpos_ptr
[coefi
] < 0) {
216 /* first scan of this coefficient */
218 ERREXIT1(cinfo
, JERR_BAD_PROG_SCRIPT
, scanno
);
221 if (Ah
!= last_bitpos_ptr
[coefi
] || Al
!= Ah
-1)
222 ERREXIT1(cinfo
, JERR_BAD_PROG_SCRIPT
, scanno
);
224 last_bitpos_ptr
[coefi
] = Al
;
229 /* For sequential JPEG, all progression parameters must be these: */
230 if (Ss
!= 0 || Se
!= DCTSIZE2
-1 || Ah
!= 0 || Al
!= 0)
231 ERREXIT1(cinfo
, JERR_BAD_PROG_SCRIPT
, scanno
);
232 /* Make sure components are not sent twice */
233 for (ci
= 0; ci
< ncomps
; ci
++) {
234 thisi
= scanptr
->component_index
[ci
];
235 if (component_sent
[thisi
])
236 ERREXIT1(cinfo
, JERR_BAD_SCAN_SCRIPT
, scanno
);
237 component_sent
[thisi
] = TRUE
;
242 /* Now verify that everything got sent. */
243 if (cinfo
->progressive_mode
) {
244 #ifdef C_PROGRESSIVE_SUPPORTED
245 /* For progressive mode, we only check that at least some DC data
246 * got sent for each component; the spec does not require that all bits
247 * of all coefficients be transmitted. Would it be wiser to enforce
248 * transmission of all coefficient bits??
250 for (ci
= 0; ci
< cinfo
->num_components
; ci
++) {
251 if (last_bitpos
[ci
][0] < 0)
252 ERREXIT(cinfo
, JERR_MISSING_DATA
);
256 for (ci
= 0; ci
< cinfo
->num_components
; ci
++) {
257 if (! component_sent
[ci
])
258 ERREXIT(cinfo
, JERR_MISSING_DATA
);
263 #endif /* C_MULTISCAN_FILES_SUPPORTED */
267 select_scan_parameters (j_compress_ptr cinfo
)
268 /* Set up the scan parameters for the current scan */
272 #ifdef C_MULTISCAN_FILES_SUPPORTED
273 if (cinfo
->scan_info
!= NULL
) {
274 /* Prepare for current scan --- the script is already validated */
275 my_master_ptr master
= (my_master_ptr
) cinfo
->master
;
276 const jpeg_scan_info
* scanptr
= cinfo
->scan_info
+ master
->scan_number
;
278 cinfo
->comps_in_scan
= scanptr
->comps_in_scan
;
279 for (ci
= 0; ci
< scanptr
->comps_in_scan
; ci
++) {
280 cinfo
->cur_comp_info
[ci
] =
281 &cinfo
->comp_info
[scanptr
->component_index
[ci
]];
283 cinfo
->Ss
= scanptr
->Ss
;
284 cinfo
->Se
= scanptr
->Se
;
285 cinfo
->Ah
= scanptr
->Ah
;
286 cinfo
->Al
= scanptr
->Al
;
291 /* Prepare for single sequential-JPEG scan containing all components */
292 if (cinfo
->num_components
> MAX_COMPS_IN_SCAN
)
293 ERREXIT2(cinfo
, JERR_COMPONENT_COUNT
, cinfo
->num_components
,
295 cinfo
->comps_in_scan
= cinfo
->num_components
;
296 for (ci
= 0; ci
< cinfo
->num_components
; ci
++) {
297 cinfo
->cur_comp_info
[ci
] = &cinfo
->comp_info
[ci
];
300 cinfo
->Se
= DCTSIZE2
-1;
308 per_scan_setup (j_compress_ptr cinfo
)
309 /* Do computations that are needed before processing a JPEG scan */
310 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
312 int ci
, mcublks
, tmp
;
313 jpeg_component_info
*compptr
;
315 if (cinfo
->comps_in_scan
== 1) {
317 /* Noninterleaved (single-component) scan */
318 compptr
= cinfo
->cur_comp_info
[0];
320 /* Overall image size in MCUs */
321 cinfo
->MCUs_per_row
= compptr
->width_in_blocks
;
322 cinfo
->MCU_rows_in_scan
= compptr
->height_in_blocks
;
324 /* For noninterleaved scan, always one block per MCU */
325 compptr
->MCU_width
= 1;
326 compptr
->MCU_height
= 1;
327 compptr
->MCU_blocks
= 1;
328 compptr
->MCU_sample_width
= DCTSIZE
;
329 compptr
->last_col_width
= 1;
330 /* For noninterleaved scans, it is convenient to define last_row_height
331 * as the number of block rows present in the last iMCU row.
333 tmp
= (int) (compptr
->height_in_blocks
% compptr
->v_samp_factor
);
334 if (tmp
== 0) tmp
= compptr
->v_samp_factor
;
335 compptr
->last_row_height
= tmp
;
337 /* Prepare array describing MCU composition */
338 cinfo
->blocks_in_MCU
= 1;
339 cinfo
->MCU_membership
[0] = 0;
343 /* Interleaved (multi-component) scan */
344 if (cinfo
->comps_in_scan
<= 0 || cinfo
->comps_in_scan
> MAX_COMPS_IN_SCAN
)
345 ERREXIT2(cinfo
, JERR_COMPONENT_COUNT
, cinfo
->comps_in_scan
,
348 /* Overall image size in MCUs */
349 cinfo
->MCUs_per_row
= (JDIMENSION
)
350 jdiv_round_up((long) cinfo
->image_width
,
351 (long) (cinfo
->max_h_samp_factor
*DCTSIZE
));
352 cinfo
->MCU_rows_in_scan
= (JDIMENSION
)
353 jdiv_round_up((long) cinfo
->image_height
,
354 (long) (cinfo
->max_v_samp_factor
*DCTSIZE
));
356 cinfo
->blocks_in_MCU
= 0;
358 for (ci
= 0; ci
< cinfo
->comps_in_scan
; ci
++) {
359 compptr
= cinfo
->cur_comp_info
[ci
];
360 /* Sampling factors give # of blocks of component in each MCU */
361 compptr
->MCU_width
= compptr
->h_samp_factor
;
362 compptr
->MCU_height
= compptr
->v_samp_factor
;
363 compptr
->MCU_blocks
= compptr
->MCU_width
* compptr
->MCU_height
;
364 compptr
->MCU_sample_width
= compptr
->MCU_width
* DCTSIZE
;
365 /* Figure number of non-dummy blocks in last MCU column & row */
366 tmp
= (int) (compptr
->width_in_blocks
% compptr
->MCU_width
);
367 if (tmp
== 0) tmp
= compptr
->MCU_width
;
368 compptr
->last_col_width
= tmp
;
369 tmp
= (int) (compptr
->height_in_blocks
% compptr
->MCU_height
);
370 if (tmp
== 0) tmp
= compptr
->MCU_height
;
371 compptr
->last_row_height
= tmp
;
372 /* Prepare array describing MCU composition */
373 mcublks
= compptr
->MCU_blocks
;
374 if (cinfo
->blocks_in_MCU
+ mcublks
> C_MAX_BLOCKS_IN_MCU
)
375 ERREXIT(cinfo
, JERR_BAD_MCU_SIZE
);
376 while (mcublks
-- > 0) {
377 cinfo
->MCU_membership
[cinfo
->blocks_in_MCU
++] = ci
;
383 /* Convert restart specified in rows to actual MCU count. */
384 /* Note that count must fit in 16 bits, so we provide limiting. */
385 if (cinfo
->restart_in_rows
> 0) {
386 long nominal
= (long) cinfo
->restart_in_rows
* (long) cinfo
->MCUs_per_row
;
387 cinfo
->restart_interval
= (unsigned int) MIN(nominal
, 65535L);
394 * This is called at the beginning of each pass. We determine which modules
395 * will be active during this pass and give them appropriate start_pass calls.
396 * We also set is_last_pass to indicate whether any more passes will be
401 prepare_for_pass (j_compress_ptr cinfo
)
403 my_master_ptr master
= (my_master_ptr
) cinfo
->master
;
405 switch (master
->pass_type
) {
407 /* Initial pass: will collect input data, and do either Huffman
408 * optimization or data output for the first scan.
410 select_scan_parameters(cinfo
);
411 per_scan_setup(cinfo
);
412 if (! cinfo
->raw_data_in
) {
413 (*cinfo
->cconvert
->start_pass
) (cinfo
);
414 (*cinfo
->downsample
->start_pass
) (cinfo
);
415 (*cinfo
->prep
->start_pass
) (cinfo
, JBUF_PASS_THRU
);
417 (*cinfo
->fdct
->start_pass
) (cinfo
);
418 (*cinfo
->entropy
->start_pass
) (cinfo
, cinfo
->optimize_coding
);
419 (*cinfo
->coef
->start_pass
) (cinfo
,
420 (master
->total_passes
> 1 ?
421 JBUF_SAVE_AND_PASS
: JBUF_PASS_THRU
));
422 (*cinfo
->main
->start_pass
) (cinfo
, JBUF_PASS_THRU
);
423 if (cinfo
->optimize_coding
) {
424 /* No immediate data output; postpone writing frame/scan headers */
425 master
->pub
.call_pass_startup
= FALSE
;
427 /* Will write frame/scan headers at first jpeg_write_scanlines call */
428 master
->pub
.call_pass_startup
= TRUE
;
431 #ifdef ENTROPY_OPT_SUPPORTED
433 /* Do Huffman optimization for a scan after the first one. */
434 select_scan_parameters(cinfo
);
435 per_scan_setup(cinfo
);
436 if (cinfo
->Ss
!= 0 || cinfo
->Ah
== 0 || cinfo
->arith_code
) {
437 (*cinfo
->entropy
->start_pass
) (cinfo
, TRUE
);
438 (*cinfo
->coef
->start_pass
) (cinfo
, JBUF_CRANK_DEST
);
439 master
->pub
.call_pass_startup
= FALSE
;
442 /* Special case: Huffman DC refinement scans need no Huffman table
443 * and therefore we can skip the optimization pass for them.
445 master
->pass_type
= output_pass
;
446 master
->pass_number
++;
450 /* Do a data-output pass. */
451 /* We need not repeat per-scan setup if prior optimization pass did it. */
452 if (! cinfo
->optimize_coding
) {
453 select_scan_parameters(cinfo
);
454 per_scan_setup(cinfo
);
456 (*cinfo
->entropy
->start_pass
) (cinfo
, FALSE
);
457 (*cinfo
->coef
->start_pass
) (cinfo
, JBUF_CRANK_DEST
);
458 /* We emit frame/scan headers now */
459 if (master
->scan_number
== 0)
460 (*cinfo
->marker
->write_frame_header
) (cinfo
);
461 (*cinfo
->marker
->write_scan_header
) (cinfo
);
462 master
->pub
.call_pass_startup
= FALSE
;
465 ERREXIT(cinfo
, JERR_NOT_COMPILED
);
468 master
->pub
.is_last_pass
= (master
->pass_number
== master
->total_passes
-1);
470 /* Set up progress monitor's pass info if present */
471 if (cinfo
->progress
!= NULL
) {
472 cinfo
->progress
->completed_passes
= master
->pass_number
;
473 cinfo
->progress
->total_passes
= master
->total_passes
;
479 * Special start-of-pass hook.
480 * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
481 * In single-pass processing, we need this hook because we don't want to
482 * write frame/scan headers during jpeg_start_compress; we want to let the
483 * application write COM markers etc. between jpeg_start_compress and the
484 * jpeg_write_scanlines loop.
485 * In multi-pass processing, this routine is not used.
489 pass_startup (j_compress_ptr cinfo
)
491 cinfo
->master
->call_pass_startup
= FALSE
; /* reset flag so call only once */
493 (*cinfo
->marker
->write_frame_header
) (cinfo
);
494 (*cinfo
->marker
->write_scan_header
) (cinfo
);
499 * Finish up at end of pass.
503 finish_pass_master (j_compress_ptr cinfo
)
505 my_master_ptr master
= (my_master_ptr
) cinfo
->master
;
507 /* The entropy coder always needs an end-of-pass call,
508 * either to analyze statistics or to flush its output buffer.
510 (*cinfo
->entropy
->finish_pass
) (cinfo
);
512 /* Update state for next pass */
513 switch (master
->pass_type
) {
515 /* next pass is either output of scan 0 (after optimization)
516 * or output of scan 1 (if no optimization).
518 master
->pass_type
= output_pass
;
519 if (! cinfo
->optimize_coding
)
520 master
->scan_number
++;
523 /* next pass is always output of current scan */
524 master
->pass_type
= output_pass
;
527 /* next pass is either optimization or output of next scan */
528 if (cinfo
->optimize_coding
)
529 master
->pass_type
= huff_opt_pass
;
530 master
->scan_number
++;
534 master
->pass_number
++;
539 * Initialize master compression control.
543 jinit_c_master_control (j_compress_ptr cinfo
, boolean transcode_only
)
545 my_master_ptr master
;
547 master
= (my_master_ptr
)
548 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
549 SIZEOF(my_comp_master
));
550 cinfo
->master
= (struct jpeg_comp_master
*) master
;
551 master
->pub
.prepare_for_pass
= prepare_for_pass
;
552 master
->pub
.pass_startup
= pass_startup
;
553 master
->pub
.finish_pass
= finish_pass_master
;
554 master
->pub
.is_last_pass
= FALSE
;
556 /* Validate parameters, determine derived values */
557 initial_setup(cinfo
);
559 if (cinfo
->scan_info
!= NULL
) {
560 #ifdef C_MULTISCAN_FILES_SUPPORTED
561 validate_script(cinfo
);
563 ERREXIT(cinfo
, JERR_NOT_COMPILED
);
566 cinfo
->progressive_mode
= FALSE
;
567 cinfo
->num_scans
= 1;
570 if (cinfo
->progressive_mode
) /* TEMPORARY HACK ??? */
571 cinfo
->optimize_coding
= TRUE
; /* assume default tables no good for progressive mode */
573 /* Initialize my private state */
574 if (transcode_only
) {
575 /* no main pass in transcoding */
576 if (cinfo
->optimize_coding
)
577 master
->pass_type
= huff_opt_pass
;
579 master
->pass_type
= output_pass
;
581 /* for normal compression, first pass is always this type: */
582 master
->pass_type
= main_pass
;
584 master
->scan_number
= 0;
585 master
->pass_number
= 0;
586 if (cinfo
->optimize_coding
)
587 master
->total_passes
= cinfo
->num_scans
* 2;
589 master
->total_passes
= cinfo
->num_scans
;