1 #if !defined(_FX_JPEG_TURBO_) 2 /* 3 * jdinput.c 4 * 5 * Copyright (C) 1991-1997, Thomas G. Lane. 6 * This file is part of the Independent JPEG Group's software. 7 * For conditions of distribution and use, see the accompanying README file. 8 * 9 * This file contains input control logic for the JPEG decompressor. 10 * These routines are concerned with controlling the decompressor's input 11 * processing (marker reading and coefficient decoding). The actual input 12 * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c. 13 */ 14 15 #define JPEG_INTERNALS 16 #include "jinclude.h" 17 #include "jpeglib.h" 18 19 20 /* Private state */ 21 22 typedef struct { 23 struct jpeg_input_controller pub; /* public fields */ 24 25 boolean inheaders; /* TRUE until first SOS is reached */ 26 } my_input_controller; 27 28 typedef my_input_controller * my_inputctl_ptr; 29 30 31 /* Forward declarations */ 32 METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo)); 33 34 35 /* 36 * Routines to calculate various quantities related to the size of the image. 37 */ 38 39 LOCAL(void) 40 initial_setup (j_decompress_ptr cinfo) 41 /* Called once, when first SOS marker is reached */ 42 { 43 int ci; 44 jpeg_component_info *compptr; 45 46 /* Make sure image isn't bigger than I can handle */ 47 if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION || 48 (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION) 49 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); 50 51 /* For now, precision must match compiled-in value... */ 52 if (cinfo->data_precision != BITS_IN_JSAMPLE) 53 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); 54 55 /* Check that number of components won't exceed internal array sizes */ 56 if (cinfo->num_components > MAX_COMPONENTS) 57 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, 58 MAX_COMPONENTS); 59 60 /* Compute maximum sampling factors; check factor validity */ 61 cinfo->max_h_samp_factor = 1; 62 cinfo->max_v_samp_factor = 1; 63 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 64 ci++, compptr++) { 65 if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR || 66 compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR) 67 ERREXIT(cinfo, JERR_BAD_SAMPLING); 68 cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, 69 compptr->h_samp_factor); 70 cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, 71 compptr->v_samp_factor); 72 } 73 74 /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE. 75 * In the full decompressor, this will be overridden by jdmaster.c; 76 * but in the transcoder, jdmaster.c is not used, so we must do it here. 77 */ 78 cinfo->min_DCT_scaled_size = DCTSIZE; 79 80 /* Compute dimensions of components */ 81 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 82 ci++, compptr++) { 83 compptr->DCT_scaled_size = DCTSIZE; 84 /* Size in DCT blocks */ 85 compptr->width_in_blocks = (JDIMENSION) 86 jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor, 87 (long) (cinfo->max_h_samp_factor * DCTSIZE)); 88 compptr->height_in_blocks = (JDIMENSION) 89 jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor, 90 (long) (cinfo->max_v_samp_factor * DCTSIZE)); 91 /* downsampled_width and downsampled_height will also be overridden by 92 * jdmaster.c if we are doing full decompression. The transcoder library 93 * doesn't use these values, but the calling application might. 94 */ 95 /* Size in samples */ 96 compptr->downsampled_width = (JDIMENSION) 97 jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor, 98 (long) cinfo->max_h_samp_factor); 99 compptr->downsampled_height = (JDIMENSION) 100 jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor, 101 (long) cinfo->max_v_samp_factor); 102 /* Mark component needed, until color conversion says otherwise */ 103 compptr->component_needed = TRUE; 104 /* Mark no quantization table yet saved for component */ 105 compptr->quant_table = NULL; 106 } 107 108 /* Compute number of fully interleaved MCU rows. */ 109 cinfo->total_iMCU_rows = (JDIMENSION) 110 jdiv_round_up((long) cinfo->image_height, 111 (long) (cinfo->max_v_samp_factor*DCTSIZE)); 112 113 /* Decide whether file contains multiple scans */ 114 if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode) 115 cinfo->inputctl->has_multiple_scans = TRUE; 116 else 117 cinfo->inputctl->has_multiple_scans = FALSE; 118 } 119 120 121 LOCAL(void) 122 per_scan_setup (j_decompress_ptr cinfo) 123 /* Do computations that are needed before processing a JPEG scan */ 124 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */ 125 { 126 int ci, mcublks, tmp; 127 jpeg_component_info *compptr; 128 129 if (cinfo->comps_in_scan == 1) { 130 131 /* Noninterleaved (single-component) scan */ 132 compptr = cinfo->cur_comp_info[0]; 133 134 /* Overall image size in MCUs */ 135 cinfo->MCUs_per_row = compptr->width_in_blocks; 136 cinfo->MCU_rows_in_scan = compptr->height_in_blocks; 137 138 /* For noninterleaved scan, always one block per MCU */ 139 compptr->MCU_width = 1; 140 compptr->MCU_height = 1; 141 compptr->MCU_blocks = 1; 142 compptr->MCU_sample_width = compptr->DCT_scaled_size; 143 compptr->last_col_width = 1; 144 /* For noninterleaved scans, it is convenient to define last_row_height 145 * as the number of block rows present in the last iMCU row. 146 */ 147 tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor); 148 if (tmp == 0) tmp = compptr->v_samp_factor; 149 compptr->last_row_height = tmp; 150 151 /* Prepare array describing MCU composition */ 152 cinfo->blocks_in_MCU = 1; 153 cinfo->MCU_membership[0] = 0; 154 155 } else { 156 157 /* Interleaved (multi-component) scan */ 158 if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) 159 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, 160 MAX_COMPS_IN_SCAN); 161 162 /* Overall image size in MCUs */ 163 cinfo->MCUs_per_row = (JDIMENSION) 164 jdiv_round_up((long) cinfo->image_width, 165 (long) (cinfo->max_h_samp_factor*DCTSIZE)); 166 cinfo->MCU_rows_in_scan = (JDIMENSION) 167 jdiv_round_up((long) cinfo->image_height, 168 (long) (cinfo->max_v_samp_factor*DCTSIZE)); 169 170 cinfo->blocks_in_MCU = 0; 171 172 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 173 compptr = cinfo->cur_comp_info[ci]; 174 /* Sampling factors give # of blocks of component in each MCU */ 175 compptr->MCU_width = compptr->h_samp_factor; 176 compptr->MCU_height = compptr->v_samp_factor; 177 compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; 178 compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size; 179 /* Figure number of non-dummy blocks in last MCU column & row */ 180 tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); 181 if (tmp == 0) tmp = compptr->MCU_width; 182 compptr->last_col_width = tmp; 183 tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); 184 if (tmp == 0) tmp = compptr->MCU_height; 185 compptr->last_row_height = tmp; 186 /* Prepare array describing MCU composition */ 187 mcublks = compptr->MCU_blocks; 188 if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU) 189 ERREXIT(cinfo, JERR_BAD_MCU_SIZE); 190 while (mcublks-- > 0) { 191 cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; 192 } 193 } 194 195 } 196 } 197 198 199 /* 200 * Save away a copy of the Q-table referenced by each component present 201 * in the current scan, unless already saved during a prior scan. 202 * 203 * In a multiple-scan JPEG file, the encoder could assign different components 204 * the same Q-table slot number, but change table definitions between scans 205 * so that each component uses a different Q-table. (The IJG encoder is not 206 * currently capable of doing this, but other encoders might.) Since we want 207 * to be able to dequantize all the components at the end of the file, this 208 * means that we have to save away the table actually used for each component. 209 * We do this by copying the table at the start of the first scan containing 210 * the component. 211 * The JPEG spec prohibits the encoder from changing the contents of a Q-table 212 * slot between scans of a component using that slot. If the encoder does so 213 * anyway, this decoder will simply use the Q-table values that were current 214 * at the start of the first scan for the component. 215 * 216 * The decompressor output side looks only at the saved quant tables, 217 * not at the current Q-table slots. 218 */ 219 220 LOCAL(void) 221 latch_quant_tables (j_decompress_ptr cinfo) 222 { 223 int ci, qtblno; 224 jpeg_component_info *compptr; 225 JQUANT_TBL * qtbl; 226 227 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 228 compptr = cinfo->cur_comp_info[ci]; 229 /* No work if we already saved Q-table for this component */ 230 if (compptr->quant_table != NULL) 231 continue; 232 /* Make sure specified quantization table is present */ 233 qtblno = compptr->quant_tbl_no; 234 if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS || 235 cinfo->quant_tbl_ptrs[qtblno] == NULL) 236 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno); 237 /* OK, save away the quantization table */ 238 qtbl = (JQUANT_TBL *) 239 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 240 SIZEOF(JQUANT_TBL)); 241 MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL)); 242 compptr->quant_table = qtbl; 243 } 244 } 245 246 247 /* 248 * Initialize the input modules to read a scan of compressed data. 249 * The first call to this is done by jdmaster.c after initializing 250 * the entire decompressor (during jpeg_start_decompress). 251 * Subsequent calls come from consume_markers, below. 252 */ 253 254 METHODDEF(void) 255 start_input_pass (j_decompress_ptr cinfo) 256 { 257 per_scan_setup(cinfo); 258 latch_quant_tables(cinfo); 259 (*cinfo->entropy->start_pass) (cinfo); 260 (*cinfo->coef->start_input_pass) (cinfo); 261 cinfo->inputctl->consume_input = cinfo->coef->consume_data; 262 } 263 264 265 /* 266 * Finish up after inputting a compressed-data scan. 267 * This is called by the coefficient controller after it's read all 268 * the expected data of the scan. 269 */ 270 271 METHODDEF(void) 272 finish_input_pass (j_decompress_ptr cinfo) 273 { 274 cinfo->inputctl->consume_input = consume_markers; 275 } 276 277 278 /* 279 * Read JPEG markers before, between, or after compressed-data scans. 280 * Change state as necessary when a new scan is reached. 281 * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI. 282 * 283 * The consume_input method pointer points either here or to the 284 * coefficient controller's consume_data routine, depending on whether 285 * we are reading a compressed data segment or inter-segment markers. 286 */ 287 288 METHODDEF(int) 289 consume_markers (j_decompress_ptr cinfo) 290 { 291 my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl; 292 int val; 293 294 if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */ 295 return JPEG_REACHED_EOI; 296 297 val = (*cinfo->marker->read_markers) (cinfo); 298 299 switch (val) { 300 case JPEG_REACHED_SOS: /* Found SOS */ 301 if (inputctl->inheaders) { /* 1st SOS */ 302 initial_setup(cinfo); 303 inputctl->inheaders = FALSE; 304 /* Note: start_input_pass must be called by jdmaster.c 305 * before any more input can be consumed. jdapimin.c is 306 * responsible for enforcing this sequencing. 307 */ 308 } else { /* 2nd or later SOS marker */ 309 if (! inputctl->pub.has_multiple_scans) 310 ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */ 311 start_input_pass(cinfo); 312 } 313 break; 314 case JPEG_REACHED_EOI: /* Found EOI */ 315 inputctl->pub.eoi_reached = TRUE; 316 if (inputctl->inheaders) { /* Tables-only datastream, apparently */ 317 if (cinfo->marker->saw_SOF) 318 ERREXIT(cinfo, JERR_SOF_NO_SOS); 319 } else { 320 /* Prevent infinite loop in coef ctlr's decompress_data routine 321 * if user set output_scan_number larger than number of scans. 322 */ 323 if (cinfo->output_scan_number > cinfo->input_scan_number) 324 cinfo->output_scan_number = cinfo->input_scan_number; 325 } 326 break; 327 case JPEG_SUSPENDED: 328 break; 329 } 330 331 return val; 332 } 333 334 335 /* 336 * Reset state to begin a fresh datastream. 337 */ 338 339 METHODDEF(void) 340 reset_input_controller (j_decompress_ptr cinfo) 341 { 342 my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl; 343 344 inputctl->pub.consume_input = consume_markers; 345 inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */ 346 inputctl->pub.eoi_reached = FALSE; 347 inputctl->inheaders = TRUE; 348 /* Reset other modules */ 349 (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo); 350 (*cinfo->marker->reset_marker_reader) (cinfo); 351 /* Reset progression state -- would be cleaner if entropy decoder did this */ 352 cinfo->coef_bits = NULL; 353 } 354 355 356 /* 357 * Initialize the input controller module. 358 * This is called only once, when the decompression object is created. 359 */ 360 361 GLOBAL(void) 362 jinit_input_controller (j_decompress_ptr cinfo) 363 { 364 my_inputctl_ptr inputctl; 365 366 /* Create subobject in permanent pool */ 367 inputctl = (my_inputctl_ptr) 368 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, 369 SIZEOF(my_input_controller)); 370 cinfo->inputctl = (struct jpeg_input_controller *) inputctl; 371 /* Initialize method pointers */ 372 inputctl->pub.consume_input = consume_markers; 373 inputctl->pub.reset_input_controller = reset_input_controller; 374 inputctl->pub.start_input_pass = start_input_pass; 375 inputctl->pub.finish_input_pass = finish_input_pass; 376 /* Initialize state: can't use reset_input_controller since we don't 377 * want to try to reset other modules yet. 378 */ 379 inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */ 380 inputctl->pub.eoi_reached = FALSE; 381 inputctl->inheaders = TRUE; 382 } 383 384 #endif //_FX_JPEG_TURBO_ 385