| 6 |
| * Modified 2003-2010 by Guido Vollbeding. |
| 7 | * libjpeg-turbo Modifications: |
| 8 | * Copyright (C) 2010, 2016, 2018, D. R. Commander. |
| 9 | * mozjpeg Modifications: |
| 10 | * Copyright (C) 2014, Mozilla Corporation. |
| 11 | * For conditions of distribution and use, see the accompanying README file. |
| 12 | * |
| 13 | * This file contains master control logic for the JPEG compressor. |
| 14 | * These routines are concerned with parameter validation, initial setup, |
| 15 | * and inter-pass control (determining the number of passes and the work |
| 16 | * to be done in each pass). |
| 17 | */ |
| 18 |
| 19 | #define JPEG_INTERNALS |
| 20 | #include "jinclude.h" |
| 21 | #include "jpeglib.h" |
| 22 | #include "jpegcomp.h" |
| 23 | #include "jconfigint.h" |
| 24 | #include "jmemsys.h" |
| 25 | #include "jcmaster.h" |
| 26 |
| 27 |
| 28 | /* |
| 29 | * Support routines that do various essential calculations. |
| 30 | */ |
| 31 |
| 32 | #if JPEG_LIB_VERSION >= 70 |
| 33 | /* |
| 34 | * Compute JPEG image dimensions and related values. |
| 35 | * NOTE: this is exported for possible use by application. |
| 36 | * Hence it mustn't do anything that can't be done twice. |
| 37 | */ |
| 38 |
| 39 | GLOBAL(void) |
| 40 | jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo) |
| 41 | /* Do computations that are needed before master selection phase */ |
| 42 | { |
| 43 | /* Hardwire it to "no scaling" */ |
| 44 | cinfo->jpeg_width = cinfo->image_width; |
| 45 | cinfo->jpeg_height = cinfo->image_height; |
| 46 | cinfo->min_DCT_h_scaled_size = DCTSIZE; |
| 47 | cinfo->min_DCT_v_scaled_size = DCTSIZE; |
| 48 | } |
| 49 | #endif |
| 50 |
| 51 |
| 52 | LOCAL(void) |
| 53 | initial_setup (j_compress_ptr cinfo, boolean transcode_only) |
| 54 | /* Do computations that are needed before master selection phase */ |
| 55 | { |
| 56 | int ci; |
| 57 | jpeg_component_info *compptr; |
| 58 | long samplesperrow; |
| 59 | JDIMENSION jd_samplesperrow; |
| 60 |
| 61 | #if JPEG_LIB_VERSION >= 70 |
| 62 | #if JPEG_LIB_VERSION >= 80 |
| 63 | if (!transcode_only) |
| 64 | #endif |
| 65 | jpeg_calc_jpeg_dimensions(cinfo); |
| 66 | #endif |
| 67 |
| 68 | /* Sanity check on image dimensions */ |
| 69 | if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0 || |
| 70 | cinfo->num_components <= 0 || cinfo->input_components <= 0) |
| 71 | ERREXIT(cinfo, JERR_EMPTY_IMAGE); |
| 72 |
| 73 | /* Make sure image isn't bigger than I can handle */ |
| 74 | if ((long) cinfo->_jpeg_height > (long) JPEG_MAX_DIMENSION || |
| 75 | (long) cinfo->_jpeg_width > (long) JPEG_MAX_DIMENSION) |
| 76 | ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); |
| 77 |
| 78 | /* Width of an input scanline must be representable as JDIMENSION. */ |
| 79 | samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components; |
| 80 | jd_samplesperrow = (JDIMENSION) samplesperrow; |
| 81 | if ((long) jd_samplesperrow != samplesperrow) |
| 82 | ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); |
| 83 |
| 84 | /* For now, precision must match compiled-in value... */ |
| 85 | if (cinfo->data_precision != BITS_IN_JSAMPLE) |
| 86 | ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); |
| 87 |
| 88 | /* Check that number of components won't exceed internal array sizes */ |
| 89 | if (cinfo->num_components > MAX_COMPONENTS) |
| 90 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, |
| 91 | MAX_COMPONENTS); |
| 92 |
| 93 | /* Compute maximum sampling factors; check factor validity */ |
| 94 | cinfo->max_h_samp_factor = 1; |
| 95 | cinfo->max_v_samp_factor = 1; |
| 96 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 97 | ci++, compptr++) { |
| 98 | if (compptr->h_samp_factor <= 0 || |
| 99 | compptr->h_samp_factor > MAX_SAMP_FACTOR || |
| 100 | compptr->v_samp_factor <= 0 || |
| 101 | compptr->v_samp_factor > MAX_SAMP_FACTOR) |
| 102 | ERREXIT(cinfo, JERR_BAD_SAMPLING); |
| 103 | cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, |
| 104 | compptr->h_samp_factor); |
| 105 | cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, |
| 106 | compptr->v_samp_factor); |
| 107 | } |
| 108 |
| 109 | /* Compute dimensions of components */ |
| 110 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 111 | ci++, compptr++) { |
| 112 | /* Fill in the correct component_index value; don't rely on application */ |
| 113 | compptr->component_index = ci; |
| 114 | /* For compression, we never do DCT scaling. */ |
| 115 | #if JPEG_LIB_VERSION >= 70 |
| 116 | compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE; |
| 117 | #else |
| 118 | compptr->DCT_scaled_size = DCTSIZE; |
| 119 | #endif |
| 120 | /* Size in DCT blocks */ |
| 121 | compptr->width_in_blocks = (JDIMENSION) |
| 122 | jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor, |
| 123 | (long) (cinfo->max_h_samp_factor * DCTSIZE)); |
| 124 | compptr->height_in_blocks = (JDIMENSION) |
| 125 | jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor, |
| 126 | (long) (cinfo->max_v_samp_factor * DCTSIZE)); |
| 127 | /* Size in samples */ |
| 128 | compptr->downsampled_width = (JDIMENSION) |
| 129 | jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor, |
| 130 | (long) cinfo->max_h_samp_factor); |
| 131 | compptr->downsampled_height = (JDIMENSION) |
| 132 | jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor, |
| 133 | (long) cinfo->max_v_samp_factor); |
| 134 | /* Mark component needed (this flag isn't actually used for compression) */ |
| 135 | compptr->component_needed = TRUE; |
| 136 | } |
| 137 |
| 138 | /* Compute number of fully interleaved MCU rows (number of times that |
| 139 | * main controller will call coefficient controller). |
| 140 | */ |
| 141 | cinfo->total_iMCU_rows = (JDIMENSION) |
| 142 | jdiv_round_up((long) cinfo->_jpeg_height, |
| 143 | (long) (cinfo->max_v_samp_factor*DCTSIZE)); |
| 144 | } |
| 145 |
| 146 |
| 147 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
| 148 |
| 149 | LOCAL(void) |
| 150 | validate_script (j_compress_ptr cinfo) |
| 151 | /* Verify that the scan script in cinfo->scan_info[] is valid; also |
| 152 | * determine whether it uses progressive JPEG, and set cinfo->progressive_mode. |
| 153 | */ |
| 154 | { |
| 155 | const jpeg_scan_info *scanptr; |
| 156 | int scanno, ncomps, ci, coefi, thisi; |
| 157 | int Ss, Se, Ah, Al; |
| 158 | boolean component_sent[MAX_COMPONENTS]; |
| 159 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 160 | int *last_bitpos_ptr; |
| 161 | int last_bitpos[MAX_COMPONENTS][DCTSIZE2]; |
| 162 | /* -1 until that coefficient has been seen; then last Al for it */ |
| 163 | #endif |
| 164 |
| 165 | if (cinfo->master->optimize_scans) { |
| 166 | cinfo->progressive_mode = TRUE; |
| 167 | /* When we optimize scans, there is redundancy in the scan list |
| 168 | * and this function will fail. Therefore skip all this checking |
| 169 | */ |
| 170 | return; |
| 171 | } |
| 172 |
| 173 | if (cinfo->num_scans <= 0) |
| 174 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0); |
| 175 |
| 176 | /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1; |
| 177 | * for progressive JPEG, no scan can have this. |
| 178 | */ |
| 179 | scanptr = cinfo->scan_info; |
| 180 | if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) { |
| 181 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 182 | cinfo->progressive_mode = TRUE; |
| 183 | last_bitpos_ptr = & last_bitpos[0][0]; |
| 184 | for (ci = 0; ci < cinfo->num_components; ci++) |
| 185 | for (coefi = 0; coefi < DCTSIZE2; coefi++) |
| 186 | *last_bitpos_ptr++ = -1; |
| 187 | #else |
| 188 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
| 189 | #endif |
| 190 | } else { |
| 191 | cinfo->progressive_mode = FALSE; |
| 192 | for (ci = 0; ci < cinfo->num_components; ci++) |
| 193 | component_sent[ci] = FALSE; |
| 194 | } |
| 195 |
| 196 | for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) { |
| 197 | /* Validate component indexes */ |
| 198 | ncomps = scanptr->comps_in_scan; |
| 199 | if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN) |
| 200 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN); |
| 201 | for (ci = 0; ci < ncomps; ci++) { |
| 202 | thisi = scanptr->component_index[ci]; |
| 203 | if (thisi < 0 || thisi >= cinfo->num_components) |
| 204 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); |
| 205 | /* Components must appear in SOF order within each scan */ |
| 206 | if (ci > 0 && thisi <= scanptr->component_index[ci-1]) |
| 207 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); |
| 208 | } |
| 209 | /* Validate progression parameters */ |
| 210 | Ss = scanptr->Ss; |
| 211 | Se = scanptr->Se; |
| 212 | Ah = scanptr->Ah; |
| 213 | Al = scanptr->Al; |
| 214 | if (cinfo->progressive_mode) { |
| 215 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 216 | /* Rec. ITU-T T.81 | ISO/IEC 10918-1 simply gives the ranges 0..13 for Ah |
| 217 | * and Al, but that seems wrong: the upper bound ought to depend on data |
| 218 | * precision. Perhaps they really meant 0..N+1 for N-bit precision. |
| 219 | * Here we allow 0..10 for 8-bit data; Al larger than 10 results in |
| 220 | * out-of-range reconstructed DC values during the first DC scan, |
| 221 | * which might cause problems for some decoders. |
| 222 | */ |
| 223 | #if BITS_IN_JSAMPLE == 8 |
| 224 | #define MAX_AH_AL 10 |
| 225 | #else |
| 226 | #define MAX_AH_AL 13 |
| 227 | #endif |
| 228 | if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 || |
| 229 | Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL) |
| 230 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
| 231 | if (Ss == 0) { |
| 232 | if (Se != 0) /* DC and AC together not OK */ |
| 233 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
| 234 | } else { |
| 235 | if (ncomps != 1) /* AC scans must be for only one component */ |
| 236 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
| 237 | } |
| 238 | for (ci = 0; ci < ncomps; ci++) { |
| 239 | last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0]; |
| 240 | if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */ |
| 241 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
| 242 | for (coefi = Ss; coefi <= Se; coefi++) { |
| 243 | if (last_bitpos_ptr[coefi] < 0) { |
| 244 | /* first scan of this coefficient */ |
| 245 | if (Ah != 0) |
| 246 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
| 247 | } else { |
| 248 | /* not first scan */ |
| 249 | if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1) |
| 250 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
| 251 | } |
| 252 | last_bitpos_ptr[coefi] = Al; |
| 253 | } |
| 254 | } |
| 255 | #endif |
| 256 | } else { |
| 257 | /* For sequential JPEG, all progression parameters must be these: */ |
| 258 | if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0) |
| 259 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
| 260 | /* Make sure components are not sent twice */ |
| 261 | for (ci = 0; ci < ncomps; ci++) { |
| 262 | thisi = scanptr->component_index[ci]; |
| 263 | if (component_sent[thisi]) |
| 264 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); |
| 265 | component_sent[thisi] = TRUE; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 |
| 270 | /* Now verify that everything got sent. */ |
| 271 | if (cinfo->progressive_mode) { |
| 272 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 273 | /* For progressive mode, we only check that at least some DC data |
| 274 | * got sent for each component; the spec does not require that all bits |
| 275 | * of all coefficients be transmitted. Would it be wiser to enforce |
| 276 | * transmission of all coefficient bits?? |
| 277 | */ |
| 278 | for (ci = 0; ci < cinfo->num_components; ci++) { |
| 279 | if (last_bitpos[ci][0] < 0) |
| 280 | ERREXIT(cinfo, JERR_MISSING_DATA); |
| 281 | } |
| 282 | #endif |
| 283 | } else { |
| 284 | for (ci = 0; ci < cinfo->num_components; ci++) { |
| 285 | if (! component_sent[ci]) |
| 286 | ERREXIT(cinfo, JERR_MISSING_DATA); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 |
| 291 | #endif /* C_MULTISCAN_FILES_SUPPORTED */ |
| 292 |
| 293 |
| 294 | LOCAL(void) |
| 295 | select_scan_parameters (j_compress_ptr cinfo) |
| 296 | /* Set up the scan parameters for the current scan */ |
| 297 | { |
| 298 | int ci; |
| 299 |
| 300 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
| 301 | my_master_ptr master = (my_master_ptr) cinfo->master; |
| 302 | if (master->pass_number < master->pass_number_scan_opt_base) { |
| 303 | cinfo->comps_in_scan = 1; |
| 304 | if (cinfo->master->use_scans_in_trellis) { |
| 305 | cinfo->cur_comp_info[0] = |
| 306 | &cinfo->comp_info[master->pass_number / |
| 307 | (4 * cinfo->master->trellis_num_loops)]; |
| 308 | cinfo->Ss = (master->pass_number % 4 < 2) ? |
| 309 | 1 : cinfo->master->trellis_freq_split + 1; |
| 310 | cinfo->Se = (master->pass_number % 4 < 2) ? |
| 311 | cinfo->master->trellis_freq_split : DCTSIZE2 - 1; |
| 312 | } else { |
| 313 | cinfo->cur_comp_info[0] = |
| 314 | &cinfo->comp_info[master->pass_number / |
| 315 | (2 * cinfo->master->trellis_num_loops)]; |
| 316 | cinfo->Ss = 1; |
| 317 | cinfo->Se = DCTSIZE2-1; |
| 318 | } |
| 319 | } |
| 320 | else if (cinfo->scan_info != NULL) { |
| 321 | /* Prepare for current scan --- the script is already validated */ |
| 322 | const jpeg_scan_info *scanptr = cinfo->scan_info + master->scan_number; |
| 323 |
| 324 | cinfo->comps_in_scan = scanptr->comps_in_scan; |
| 325 | for (ci = 0; ci < scanptr->comps_in_scan; ci++) { |
| 326 | cinfo->cur_comp_info[ci] = |
| 327 | &cinfo->comp_info[scanptr->component_index[ci]]; |
| 328 | } |
| 329 | cinfo->Ss = scanptr->Ss; |
| 330 | cinfo->Se = scanptr->Se; |
| 331 | cinfo->Ah = scanptr->Ah; |
| 332 | cinfo->Al = scanptr->Al; |
| 333 | if (cinfo->master->optimize_scans) { |
| 334 | /* luma frequency split passes */ |
| 335 | if (master->scan_number >= cinfo->master->num_scans_luma_dc + |
| 336 | 3 * cinfo->master->Al_max_luma + 2 && |
| 337 | master->scan_number < cinfo->master->num_scans_luma) |
| 338 | cinfo->Al = master->best_Al_luma; |
| 339 | /* chroma frequency split passes */ |
| 340 | if (master->scan_number >= cinfo->master->num_scans_luma + |
| 341 | cinfo->master->num_scans_chroma_dc + |
| 342 | (6 * cinfo->master->Al_max_chroma + 4) && |
| 343 | master->scan_number < cinfo->num_scans) |
| 344 | cinfo->Al = master->best_Al_chroma; |
| 345 | } |
| 346 | /* save value for later retrieval during printout of scans */ |
| 347 | master->actual_Al[master->scan_number] = cinfo->Al; |
| 348 | } |
| 349 | else |
| 350 | #endif |
| 351 | { |
| 352 | /* Prepare for single sequential-JPEG scan containing all components */ |
| 353 | if (cinfo->num_components > MAX_COMPS_IN_SCAN) |
| 354 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, |
| 355 | MAX_COMPS_IN_SCAN); |
| 356 | cinfo->comps_in_scan = cinfo->num_components; |
| 357 | for (ci = 0; ci < cinfo->num_components; ci++) { |
| 358 | cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci]; |
| 359 | } |
| 360 | cinfo->Ss = 0; |
| 361 | cinfo->Se = DCTSIZE2-1; |
| 362 | cinfo->Ah = 0; |
| 363 | cinfo->Al = 0; |
| 364 | } |
| 365 | } |
| 366 |
| 367 |
| 368 | LOCAL(void) |
| 369 | per_scan_setup (j_compress_ptr cinfo) |
| 370 | /* Do computations that are needed before processing a JPEG scan */ |
| 371 | /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */ |
| 372 | { |
| 373 | int ci, mcublks, tmp; |
| 374 | jpeg_component_info *compptr; |
| 375 |
| 376 | if (cinfo->comps_in_scan == 1) { |
| 377 |
| 378 | /* Noninterleaved (single-component) scan */ |
| 379 | compptr = cinfo->cur_comp_info[0]; |
| 380 |
| 381 | /* Overall image size in MCUs */ |
| 382 | cinfo->MCUs_per_row = compptr->width_in_blocks; |
| 383 | cinfo->MCU_rows_in_scan = compptr->height_in_blocks; |
| 384 |
| 385 | /* For noninterleaved scan, always one block per MCU */ |
| 386 | compptr->MCU_width = 1; |
| 387 | compptr->MCU_height = 1; |
| 388 | compptr->MCU_blocks = 1; |
| 389 | compptr->MCU_sample_width = DCTSIZE; |
| 390 | compptr->last_col_width = 1; |
| 391 | /* For noninterleaved scans, it is convenient to define last_row_height |
| 392 | * as the number of block rows present in the last iMCU row. |
| 393 | */ |
| 394 | tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor); |
| 395 | if (tmp == 0) tmp = compptr->v_samp_factor; |
| 396 | compptr->last_row_height = tmp; |
| 397 |
| 398 | /* Prepare array describing MCU composition */ |
| 399 | cinfo->blocks_in_MCU = 1; |
| 400 | cinfo->MCU_membership[0] = 0; |
| 401 |
| 402 | } else { |
| 403 |
| 404 | /* Interleaved (multi-component) scan */ |
| 405 | if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) |
| 406 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, |
| 407 | MAX_COMPS_IN_SCAN); |
| 408 |
| 409 | /* Overall image size in MCUs */ |
| 410 | cinfo->MCUs_per_row = (JDIMENSION) |
| 411 | jdiv_round_up((long) cinfo->_jpeg_width, |
| 412 | (long) (cinfo->max_h_samp_factor*DCTSIZE)); |
| 413 | cinfo->MCU_rows_in_scan = (JDIMENSION) |
| 414 | jdiv_round_up((long) cinfo->_jpeg_height, |
| 415 | (long) (cinfo->max_v_samp_factor*DCTSIZE)); |
| 416 |
| 417 | cinfo->blocks_in_MCU = 0; |
| 418 |
| 419 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 420 | compptr = cinfo->cur_comp_info[ci]; |
| 421 | /* Sampling factors give # of blocks of component in each MCU */ |
| 422 | compptr->MCU_width = compptr->h_samp_factor; |
| 423 | compptr->MCU_height = compptr->v_samp_factor; |
| 424 | compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; |
| 425 | compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE; |
| 426 | /* Figure number of non-dummy blocks in last MCU column & row */ |
| 427 | tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); |
| 428 | if (tmp == 0) tmp = compptr->MCU_width; |
| 429 | compptr->last_col_width = tmp; |
| 430 | tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); |
| 431 | if (tmp == 0) tmp = compptr->MCU_height; |
| 432 | compptr->last_row_height = tmp; |
| 433 | /* Prepare array describing MCU composition */ |
| 434 | mcublks = compptr->MCU_blocks; |
| 435 | if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU) |
| 436 | ERREXIT(cinfo, JERR_BAD_MCU_SIZE); |
| 437 | while (mcublks-- > 0) { |
| 438 | cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; |
| 439 | } |
| 440 | } |
| 441 |
| 442 | } |
| 443 |
| 444 | /* Convert restart specified in rows to actual MCU count. */ |
| 445 | /* Note that count must fit in 16 bits, so we provide limiting. */ |
| 446 | if (cinfo->restart_in_rows > 0) { |
| 447 | long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row; |
| 448 | cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L); |
| 449 | } |
| 450 | } |
| 451 |
| 452 |
| 453 | /* |
| 454 | * Per-pass setup. |
| 455 | * This is called at the beginning of each pass. We determine which modules |
| 456 | * will be active during this pass and give them appropriate start_pass calls. |
| 457 | * We also set is_last_pass to indicate whether any more passes will be |
| 458 | * required. |
| 459 | */ |
| 460 |
| 461 | METHODDEF(void) |
| 462 | prepare_for_pass (j_compress_ptr cinfo) |
| 463 | { |
| 464 | my_master_ptr master = (my_master_ptr) cinfo->master; |
| 465 | cinfo->master->trellis_passes = |
| 466 | master->pass_number < master->pass_number_scan_opt_base; |
| 467 |
| 468 | switch (master->pass_type) { |
| 469 | case main_pass: |
| 470 | /* Initial pass: will collect input data, and do either Huffman |
| 471 | * optimization or data output for the first scan. |
| 472 | */ |
| 473 | select_scan_parameters(cinfo); |
| 474 | per_scan_setup(cinfo); |
| 475 | if (! cinfo->raw_data_in) { |
| 476 | (*cinfo->cconvert->start_pass) (cinfo); |
| 477 | (*cinfo->downsample->start_pass) (cinfo); |
| 478 | (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU); |
| 479 | } |
| 480 | (*cinfo->fdct->start_pass) (cinfo); |
| 481 | (*cinfo->entropy->start_pass) (cinfo, (cinfo->optimize_coding || cinfo->master->trellis_quant) && !cinfo->arith_code); |
| 482 | (*cinfo->coef->start_pass) (cinfo, |
| 483 | (master->total_passes > 1 ? |
| 484 | JBUF_SAVE_AND_PASS : JBUF_PASS_THRU)); |
| 485 | (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU); |
| 486 | if (cinfo->optimize_coding || cinfo->master->trellis_quant) { |
| 487 | /* No immediate data output; postpone writing frame/scan headers */ |
| 488 | master->pub.call_pass_startup = FALSE; |
| 489 | } else { |
| 490 | /* Will write frame/scan headers at first jpeg_write_scanlines call */ |
| 491 | master->pub.call_pass_startup = TRUE; |
| 492 | } |
| 493 | break; |
| 494 | #ifdef ENTROPY_OPT_SUPPORTED |
| 495 | case huff_opt_pass: |
| 496 | /* Do Huffman optimization for a scan after the first one. */ |
| 497 | select_scan_parameters(cinfo); |
| 498 | per_scan_setup(cinfo); |
| 499 | if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) { |
| 500 | (*cinfo->entropy->start_pass) (cinfo, TRUE); |
| 501 | (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); |
| 502 | master->pub.call_pass_startup = FALSE; |
| 503 | break; |
| 504 | } |
| 505 | /* Special case: Huffman DC refinement scans need no Huffman table |
| 506 | * and therefore we can skip the optimization pass for them. |
| 507 | */ |
| 508 | master->pass_type = output_pass; |
| 509 | master->pass_number++; |
| 510 | #endif |
| 511 | /*FALLTHROUGH*/ |
| 512 | case output_pass: |
| 513 | /* Do a data-output pass. */ |
| 514 | /* We need not repeat per-scan setup if prior optimization pass did it. */ |
| 515 | if (! cinfo->optimize_coding) { |
| 516 | select_scan_parameters(cinfo); |
| 517 | per_scan_setup(cinfo); |
| 518 | } |
| 519 | if (cinfo->master->optimize_scans) { |
| 520 | master->saved_dest = cinfo->dest; |
| 521 | cinfo->dest = NULL; |
| 522 | master->scan_size[master->scan_number] = 0; |
| 523 | jpeg_mem_dest_internal(cinfo, &master->scan_buffer[master->scan_number], &master->scan_size[master->scan_number], JPOOL_IMAGE); |
| 524 | (*cinfo->dest->init_destination)(cinfo); |
| 525 | } |
| 526 | (*cinfo->entropy->start_pass) (cinfo, FALSE); |
| 527 | (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); |
| 528 | /* We emit frame/scan headers now */ |
| 529 | if (master->scan_number == 0) |
| 530 | (*cinfo->marker->write_frame_header) (cinfo); |
| 531 | (*cinfo->marker->write_scan_header) (cinfo); |
| 532 | master->pub.call_pass_startup = FALSE; |
| 533 | break; |
| 534 | case trellis_pass: |
| 535 | if (master->pass_number % |
| 536 | (cinfo->num_components * (cinfo->master->use_scans_in_trellis ? 4 : 2)) == 1 && |
| 537 | cinfo->master->trellis_q_opt) { |
| 538 | int i, j; |
| 539 |
| 540 | for (i = 0; i < NUM_QUANT_TBLS; i++) { |
| 541 | for (j = 1; j < DCTSIZE2; j++) { |
| 542 | cinfo->master->norm_src[i][j] = 0.0; |
| 543 | cinfo->master->norm_coef[i][j] = 0.0; |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | (*cinfo->entropy->start_pass) (cinfo, !cinfo->arith_code); |
| 548 | (*cinfo->coef->start_pass) (cinfo, JBUF_REQUANT); |
| 549 | master->pub.call_pass_startup = FALSE; |
| 550 | break; |
| 551 |
| 552 | default: |
| 553 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
| 554 | } |
| 555 |
| 556 | master->pub.is_last_pass = (master->pass_number == master->total_passes-1); |
| 557 |
| 558 | /* Set up progress monitor's pass info if present */ |
| 559 | if (cinfo->progress != NULL) { |
| 560 | cinfo->progress->completed_passes = master->pass_number; |
| 561 | cinfo->progress->total_passes = master->total_passes; |
| 562 | } |
| 563 | } |
| 564 |
| 565 |
| 566 | /* |
| 567 | * Special start-of-pass hook. |
| 568 | * This is called by jpeg_write_scanlines if call_pass_startup is TRUE. |
| 569 | * In single-pass processing, we need this hook because we don't want to |
| 570 | * write frame/scan headers during jpeg_start_compress; we want to let the |
| 571 | * application write COM markers etc. between jpeg_start_compress and the |
| 572 | * jpeg_write_scanlines loop. |
| 573 | * In multi-pass processing, this routine is not used. |
| 574 | */ |
| 575 |
| 576 | METHODDEF(void) |
| 577 | pass_startup (j_compress_ptr cinfo) |
| 578 | { |
| 579 | cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */ |
| 580 |
| 581 | (*cinfo->marker->write_frame_header) (cinfo); |
| 582 | (*cinfo->marker->write_scan_header) (cinfo); |
| 583 | } |
| 584 |
| 585 |
| 586 | LOCAL(void) |
| 587 | copy_buffer (j_compress_ptr cinfo, int scan_idx) |
| 588 | { |
| 589 | my_master_ptr master = (my_master_ptr) cinfo->master; |
| 590 |
| 591 | unsigned long size = master->scan_size[scan_idx]; |
| 592 | unsigned char * src = master->scan_buffer[scan_idx]; |
| 593 | int i; |
| 594 |
| 595 | if (cinfo->err->trace_level > 0) { |
| 596 | fprintf(stderr, "SCAN "); |
| 597 | for (i = 0; i < cinfo->scan_info[scan_idx].comps_in_scan; i++) |
| 598 | fprintf(stderr, "%s%d", (i==0)?"":",", cinfo->scan_info[scan_idx].component_index[i]); |
| 599 | fprintf(stderr, ": %d %d", cinfo->scan_info[scan_idx].Ss, cinfo->scan_info[scan_idx].Se); |
| 600 | fprintf(stderr, " %d %d", cinfo->scan_info[scan_idx].Ah, master->actual_Al[scan_idx]); |
| 601 | fprintf(stderr, "\n"); |
| 602 | } |
| 603 |
| 604 | while (size >= cinfo->dest->free_in_buffer) |
| 605 | { |
| 606 | MEMCOPY(cinfo->dest->next_output_byte, src, cinfo->dest->free_in_buffer); |
| 607 | src += cinfo->dest->free_in_buffer; |
| 608 | size -= cinfo->dest->free_in_buffer; |
| 609 | cinfo->dest->next_output_byte += cinfo->dest->free_in_buffer; |
| 610 | cinfo->dest->free_in_buffer = 0; |
| 611 |
| 612 | if (!(*cinfo->dest->empty_output_buffer)(cinfo)) |
| 613 | ERREXIT(cinfo, JERR_UNSUPPORTED_SUSPEND); |
| 614 | } |
| 615 |
| 616 | MEMCOPY(cinfo->dest->next_output_byte, src, size); |
| 617 | cinfo->dest->next_output_byte += size; |
| 618 | cinfo->dest->free_in_buffer -= size; |
| 619 | } |
| 620 |
| 621 | LOCAL(void) |
| 622 | select_scans (j_compress_ptr cinfo, int next_scan_number) |
| 623 | { |
| 624 | my_master_ptr master = (my_master_ptr) cinfo->master; |
| 625 |
| 626 | int base_scan_idx = 0; |
| 627 | int luma_freq_split_scan_start = cinfo->master->num_scans_luma_dc + |
| 628 | 3 * cinfo->master->Al_max_luma + 2; |
| 629 | int chroma_freq_split_scan_start = cinfo->master->num_scans_luma + |
| 630 | cinfo->master->num_scans_chroma_dc + |
| 631 | (6 * cinfo->master->Al_max_chroma + 4); |
| 632 | int passes_per_scan = cinfo->optimize_coding ? 2 : 1; |
| 633 |
| 634 | if (next_scan_number > 1 && next_scan_number <= luma_freq_split_scan_start) { |
| 635 | if ((next_scan_number - 1) % 3 == 2) { |
| 636 | int Al = (next_scan_number - 1) / 3; |
| 637 | int i; |
| 638 | unsigned long cost = 0; |
| 639 | cost += master->scan_size[next_scan_number-2]; |
| 640 | cost += master->scan_size[next_scan_number-1]; |
| 641 | for (i = 0; i < Al; i++) |
| 642 | cost += master->scan_size[3 + 3*i]; |
| 643 |
| 644 | if (Al == 0 || cost < master->best_cost) { |
| 645 | master->best_cost = cost; |
| 646 | master->best_Al_luma = Al; |
| 647 | } else { |
| 648 | master->scan_number = luma_freq_split_scan_start - 1; |
| 649 | master->pass_number = passes_per_scan * (master->scan_number + 1) - 1 + master->pass_number_scan_opt_base; |
| 650 | } |
| 651 | } |
| 652 |
| 653 | } else if (next_scan_number > luma_freq_split_scan_start && |
| 654 | next_scan_number <= cinfo->master->num_scans_luma) { |
| 655 | if (next_scan_number == luma_freq_split_scan_start + 1) { |
| 656 | master->best_freq_split_idx_luma = 0; |
| 657 | master->best_cost = master->scan_size[next_scan_number-1]; |
| 658 |
| 659 | } else if ((next_scan_number - luma_freq_split_scan_start) % 2 == 1) { |
| 660 | int idx = (next_scan_number - luma_freq_split_scan_start) >> 1; |
| 661 | unsigned long cost = 0; |
| 662 | cost += master->scan_size[next_scan_number-2]; |
| 663 | cost += master->scan_size[next_scan_number-1]; |
| 664 |
| 665 | if (cost < master->best_cost) { |
| 666 | master->best_cost = cost; |
| 667 | master->best_freq_split_idx_luma = idx; |
| 668 | } |
| 669 |
| 670 | /* if after testing first 3, no split is the best, don't search further */ |
| 671 | if ((idx == 2 && master->best_freq_split_idx_luma == 0) || |
| 672 | (idx == 3 && master->best_freq_split_idx_luma != 2) || |
| 673 | (idx == 4 && master->best_freq_split_idx_luma != 4)) { |
| 674 | master->scan_number = cinfo->master->num_scans_luma - 1; |
| 675 | master->pass_number = passes_per_scan * (master->scan_number + 1) - 1 + master->pass_number_scan_opt_base; |
| 676 | master->pub.is_last_pass = (master->pass_number == master->total_passes - 1); |
| 677 | } |
| 678 | } |
| 679 |
| 680 | } else if (cinfo->num_scans > cinfo->master->num_scans_luma) { |
| 681 |
| 682 | if (next_scan_number == cinfo->master->num_scans_luma + |
| 683 | cinfo->master->num_scans_chroma_dc) { |
| 684 | base_scan_idx = cinfo->master->num_scans_luma; |
| 685 |
| 686 | master->interleave_chroma_dc = master->scan_size[base_scan_idx] <= master->scan_size[base_scan_idx+1] + master->scan_size[base_scan_idx+2]; |
| 687 |
| 688 | } else if (next_scan_number > cinfo->master->num_scans_luma + |
| 689 | cinfo->master->num_scans_chroma_dc && |
| 690 | next_scan_number <= chroma_freq_split_scan_start) { |
| 691 | base_scan_idx = cinfo->master->num_scans_luma + |
| 692 | cinfo->master->num_scans_chroma_dc; |
| 693 | if ((next_scan_number - base_scan_idx) % 6 == 4) { |
| 694 | int Al = (next_scan_number - base_scan_idx) / 6; |
| 695 | int i; |
| 696 | unsigned long cost = 0; |
| 697 | cost += master->scan_size[next_scan_number-4]; |
| 698 | cost += master->scan_size[next_scan_number-3]; |
| 699 | cost += master->scan_size[next_scan_number-2]; |
| 700 | cost += master->scan_size[next_scan_number-1]; |
| 701 | for (i = 0; i < Al; i++) { |
| 702 | cost += master->scan_size[base_scan_idx + 4 + 6*i]; |
| 703 | cost += master->scan_size[base_scan_idx + 5 + 6*i]; |
| 704 | } |
| 705 |
| 706 | if (Al == 0 || cost < master->best_cost) { |
| 707 | master->best_cost = cost; |
| 708 | master->best_Al_chroma = Al; |
| 709 | } else { |
| 710 | master->scan_number = chroma_freq_split_scan_start - 1; |
| 711 | master->pass_number = passes_per_scan * (master->scan_number + 1) - 1 + master->pass_number_scan_opt_base; |
| 712 | } |
| 713 | } |
| 714 |
| 715 | } else if (next_scan_number > chroma_freq_split_scan_start && next_scan_number <= cinfo->num_scans) { |
| 716 | if (next_scan_number == chroma_freq_split_scan_start + 2) { |
| 717 | master->best_freq_split_idx_chroma = 0; |
| 718 | master->best_cost = master->scan_size[next_scan_number-2]; |
| 719 | master->best_cost += master->scan_size[next_scan_number-1]; |
| 720 |
| 721 | } else if ((next_scan_number - chroma_freq_split_scan_start) % 4 == 2) { |
| 722 | int idx = (next_scan_number - chroma_freq_split_scan_start) >> 2; |
| 723 | unsigned long cost = 0; |
| 724 | cost += master->scan_size[next_scan_number-4]; |
| 725 | cost += master->scan_size[next_scan_number-3]; |
| 726 | cost += master->scan_size[next_scan_number-2]; |
| 727 | cost += master->scan_size[next_scan_number-1]; |
| 728 |
| 729 | if (cost < master->best_cost) { |
| 730 | master->best_cost = cost; |
| 731 | master->best_freq_split_idx_chroma = idx; |
| 732 | } |
| 733 |
| 734 | /* if after testing first 3, no split is the best, don't search further */ |
| 735 | if ((idx == 2 && master->best_freq_split_idx_chroma == 0) || |
| 736 | (idx == 3 && master->best_freq_split_idx_chroma != 2) || |
| 737 | (idx == 4 && master->best_freq_split_idx_chroma != 4)) { |
| 738 | master->scan_number = cinfo->num_scans - 1; |
| 739 | master->pass_number = passes_per_scan * (master->scan_number + 1) - 1 + master->pass_number_scan_opt_base; |
| 740 | master->pub.is_last_pass = (master->pass_number == master->total_passes - 1); |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | } |
| 745 |
| 746 | if (master->scan_number == cinfo->num_scans - 1) { |
| 747 | int i, Al; |
| 748 | int min_Al = MIN(master->best_Al_luma, master->best_Al_chroma); |
| 749 |
| 750 | copy_buffer(cinfo, 0); |
| 751 |
| 752 | if (cinfo->num_scans > cinfo->master->num_scans_luma && |
| 753 | cinfo->master->dc_scan_opt_mode != 0) { |
| 754 | base_scan_idx = cinfo->master->num_scans_luma; |
| 755 |
| 756 | if (master->interleave_chroma_dc && cinfo->master->dc_scan_opt_mode != 1) |
| 757 | copy_buffer(cinfo, base_scan_idx); |
| 758 | else { |
| 759 | copy_buffer(cinfo, base_scan_idx+1); |
| 760 | copy_buffer(cinfo, base_scan_idx+2); |
| 761 | } |
| 762 | } |
| 763 |
| 764 | if (master->best_freq_split_idx_luma == 0) |
| 765 | copy_buffer(cinfo, luma_freq_split_scan_start); |
| 766 | else { |
| 767 | copy_buffer(cinfo, luma_freq_split_scan_start+2*(master->best_freq_split_idx_luma-1)+1); |
| 768 | copy_buffer(cinfo, luma_freq_split_scan_start+2*(master->best_freq_split_idx_luma-1)+2); |
| 769 | } |
| 770 |
| 771 | /* copy the LSB refinements as well */ |
| 772 | for (Al = master->best_Al_luma-1; Al >= min_Al; Al--) |
| 773 | copy_buffer(cinfo, 3 + 3*Al); |
| 774 |
| 775 | if (cinfo->num_scans > cinfo->master->num_scans_luma) { |
| 776 | if (master->best_freq_split_idx_chroma == 0) { |
| 777 | copy_buffer(cinfo, chroma_freq_split_scan_start); |
| 778 | copy_buffer(cinfo, chroma_freq_split_scan_start+1); |
| 779 | } |
| 780 | else { |
| 781 | copy_buffer(cinfo, chroma_freq_split_scan_start+4*(master->best_freq_split_idx_chroma-1)+2); |
| 782 | copy_buffer(cinfo, chroma_freq_split_scan_start+4*(master->best_freq_split_idx_chroma-1)+3); |
| 783 | copy_buffer(cinfo, chroma_freq_split_scan_start+4*(master->best_freq_split_idx_chroma-1)+4); |
| 784 | copy_buffer(cinfo, chroma_freq_split_scan_start+4*(master->best_freq_split_idx_chroma-1)+5); |
| 785 | } |
| 786 |
| 787 | base_scan_idx = cinfo->master->num_scans_luma + |
| 788 | cinfo->master->num_scans_chroma_dc; |
| 789 |
| 790 | for (Al = master->best_Al_chroma-1; Al >= min_Al; Al--) { |
| 791 | copy_buffer(cinfo, base_scan_idx + 6*Al + 4); |
| 792 | copy_buffer(cinfo, base_scan_idx + 6*Al + 5); |
| 793 | } |
| 794 | } |
| 795 |
| 796 | for (Al = min_Al-1; Al >= 0; Al--) { |
| 797 | copy_buffer(cinfo, 3 + 3*Al); |
| 798 |
| 799 | if (cinfo->num_scans > cinfo->master->num_scans_luma) { |
| 800 | copy_buffer(cinfo, base_scan_idx + 6*Al + 4); |
| 801 | copy_buffer(cinfo, base_scan_idx + 6*Al + 5); |
| 802 | } |
| 803 | } |
| 804 |
| 805 | /* free the memory allocated for buffers */ |
| 806 | for (i = 0; i < cinfo->num_scans; i++) |
| 807 | if (master->scan_buffer[i]) |
| 808 | free(master->scan_buffer[i]); |
| 809 | } |
| 810 | } |
| 811 |
| 812 | /* |
| 813 | * Finish up at end of pass. |
| 814 | */ |
| 815 |
| 816 | METHODDEF(void) |
| 817 | finish_pass_master (j_compress_ptr cinfo) |
| 818 | { |
| 819 | my_master_ptr master = (my_master_ptr) cinfo->master; |
| 820 |
| 821 | /* The entropy coder always needs an end-of-pass call, |
| 822 | * either to analyze statistics or to flush its output buffer. |
| 823 | */ |
| 824 | (*cinfo->entropy->finish_pass) (cinfo); |
| 825 |
| 826 | /* Update state for next pass */ |
| 827 | switch (master->pass_type) { |
| 828 | case main_pass: |
| 829 | /* next pass is either output of scan 0 (after optimization) |
| 830 | * or output of scan 1 (if no optimization). |
| 831 | */ |
| 832 | if (cinfo->master->trellis_quant) |
| 833 | master->pass_type = trellis_pass; |
| 834 | else { |
| 835 | master->pass_type = output_pass; |
| 836 | if (! cinfo->optimize_coding) |
| 837 | master->scan_number++; |
| 838 | } |
| 839 | break; |
| 840 | case huff_opt_pass: |
| 841 | /* next pass is always output of current scan */ |
| 842 | master->pass_type = (master->pass_number < master->pass_number_scan_opt_base-1) ? trellis_pass : output_pass; |
| 843 | break; |
| 844 | case output_pass: |
| 845 | /* next pass is either optimization or output of next scan */ |
| 846 | if (cinfo->optimize_coding) |
| 847 | master->pass_type = huff_opt_pass; |
| 848 | if (cinfo->master->optimize_scans) { |
| 849 | (*cinfo->dest->term_destination)(cinfo); |
| 850 | cinfo->dest = master->saved_dest; |
| 851 | select_scans(cinfo, master->scan_number + 1); |
| 852 | } |
| 853 |
| 854 | master->scan_number++; |
| 855 | break; |
| 856 | case trellis_pass: |
| 857 | if (cinfo->optimize_coding) |
| 858 | master->pass_type = huff_opt_pass; |
| 859 | else |
| 860 | master->pass_type = (master->pass_number < master->pass_number_scan_opt_base-1) ? trellis_pass : output_pass; |
| 861 |
| 862 | if ((master->pass_number + 1) % |
| 863 | (cinfo->num_components * (cinfo->master->use_scans_in_trellis ? 4 : 2)) == 0 && |
| 864 | cinfo->master->trellis_q_opt) { |
| 865 | int i, j; |
| 866 |
| 867 | for (i = 0; i < NUM_QUANT_TBLS; i++) { |
| 868 | for (j = 1; j < DCTSIZE2; j++) { |
| 869 | if (cinfo->master->norm_coef[i][j] != 0.0) { |
| 870 | int q = (int)(cinfo->master->norm_src[i][j] / |
| 871 | cinfo->master->norm_coef[i][j] + 0.5); |
| 872 | if (q > 254) q = 254; |
| 873 | if (q < 1) q = 1; |
| 874 | cinfo->quant_tbl_ptrs[i]->quantval[j] = q; |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | break; |
| 880 | } |
| 881 |
| 882 | master->pass_number++; |
| 883 | } |
| 884 |
| 885 |
| 886 | /* |
| 887 | * Initialize master compression control. |
| 888 | */ |
| 889 |
| 890 | GLOBAL(void) |
| 891 | jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only) |
| 892 | { |
| 893 | my_master_ptr master = (my_master_ptr) cinfo->master; |
| 894 |
| 895 | master->pub.prepare_for_pass = prepare_for_pass; |
| 896 | master->pub.pass_startup = pass_startup; |
| 897 | master->pub.finish_pass = finish_pass_master; |
| 898 | master->pub.is_last_pass = FALSE; |
| 899 | master->pub.call_pass_startup = FALSE; |
| 900 |
| 901 | /* Validate parameters, determine derived values */ |
| 902 | initial_setup(cinfo, transcode_only); |
| 903 |
| 904 | if (cinfo->scan_info != NULL) { |
| 905 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
| 906 | validate_script(cinfo); |
| 907 | #else |
| 908 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
| 909 | #endif |
| 910 | } else { |
| 911 | cinfo->progressive_mode = FALSE; |
| 912 | cinfo->num_scans = 1; |
| 913 | } |
| 914 |
| 915 | if (cinfo->progressive_mode && !cinfo->arith_code) /* TEMPORARY HACK ??? */ |
| 916 | cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */ |
| 917 |
| 918 | /* Initialize my private state */ |
| 919 | if (transcode_only) { |
| 920 | /* no main pass in transcoding */ |
| 921 | if (cinfo->optimize_coding) |
| 922 | master->pass_type = huff_opt_pass; |
| 923 | else |
| 924 | master->pass_type = output_pass; |
| 925 | } else { |
| 926 | /* for normal compression, first pass is always this type: */ |
| 927 | master->pass_type = main_pass; |
| 928 | } |
| 929 | master->scan_number = 0; |
| 930 | master->pass_number = 0; |
| 931 | if (cinfo->optimize_coding) |
| 932 | master->total_passes = cinfo->num_scans * 2; |
| 933 | else |
| 934 | master->total_passes = cinfo->num_scans; |
| 935 |
| 936 | master->jpeg_version = PACKAGE_NAME " version " VERSION " (build " BUILD ")"; |
| 937 |
| 938 | master->pass_number_scan_opt_base = 0; |
| 939 | if (cinfo->master->trellis_quant) { |
| 940 | if (cinfo->optimize_coding) |
| 941 | master->pass_number_scan_opt_base = |
| 942 | ((cinfo->master->use_scans_in_trellis) ? 4 : 2) * cinfo->num_components * |
| 943 | cinfo->master->trellis_num_loops; |
| 944 | else |
| 945 | master->pass_number_scan_opt_base = |
| 946 | ((cinfo->master->use_scans_in_trellis) ? 2 : 1) * cinfo->num_components * |
| 947 | cinfo->master->trellis_num_loops + 1; |
| 948 | master->total_passes += master->pass_number_scan_opt_base; |
| 949 | } |
| 950 |
| 951 | if (cinfo->master->optimize_scans) { |
| 952 | int i; |
| 953 | master->best_Al_chroma = 0; |
| 954 |
| 955 | for (i = 0; i < cinfo->num_scans; i++) |
| 956 | master->scan_buffer[i] = NULL; |
| 957 | } |
| 958 | } |
| 959 |