1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libjpeg/jdinput.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,398 @@ 1.4 +/* 1.5 + * jdinput.c 1.6 + * 1.7 + * This file was part of the Independent JPEG Group's software: 1.8 + * Copyright (C) 1991-1997, Thomas G. Lane. 1.9 + * libjpeg-turbo Modifications: 1.10 + * Copyright (C) 2010, D. R. Commander. 1.11 + * For conditions of distribution and use, see the accompanying README file. 1.12 + * 1.13 + * This file contains input control logic for the JPEG decompressor. 1.14 + * These routines are concerned with controlling the decompressor's input 1.15 + * processing (marker reading and coefficient decoding). The actual input 1.16 + * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c. 1.17 + */ 1.18 + 1.19 +#define JPEG_INTERNALS 1.20 +#include "jinclude.h" 1.21 +#include "jpeglib.h" 1.22 +#include "jpegcomp.h" 1.23 + 1.24 + 1.25 +/* Private state */ 1.26 + 1.27 +typedef struct { 1.28 + struct jpeg_input_controller pub; /* public fields */ 1.29 + 1.30 + boolean inheaders; /* TRUE until first SOS is reached */ 1.31 +} my_input_controller; 1.32 + 1.33 +typedef my_input_controller * my_inputctl_ptr; 1.34 + 1.35 + 1.36 +/* Forward declarations */ 1.37 +METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo)); 1.38 + 1.39 + 1.40 +/* 1.41 + * Routines to calculate various quantities related to the size of the image. 1.42 + */ 1.43 + 1.44 +LOCAL(void) 1.45 +initial_setup (j_decompress_ptr cinfo) 1.46 +/* Called once, when first SOS marker is reached */ 1.47 +{ 1.48 + int ci; 1.49 + jpeg_component_info *compptr; 1.50 + 1.51 + /* Make sure image isn't bigger than I can handle */ 1.52 + if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION || 1.53 + (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION) 1.54 + ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); 1.55 + 1.56 + /* For now, precision must match compiled-in value... */ 1.57 + if (cinfo->data_precision != BITS_IN_JSAMPLE) 1.58 + ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); 1.59 + 1.60 + /* Check that number of components won't exceed internal array sizes */ 1.61 + if (cinfo->num_components > MAX_COMPONENTS) 1.62 + ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, 1.63 + MAX_COMPONENTS); 1.64 + 1.65 + /* Compute maximum sampling factors; check factor validity */ 1.66 + cinfo->max_h_samp_factor = 1; 1.67 + cinfo->max_v_samp_factor = 1; 1.68 + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 1.69 + ci++, compptr++) { 1.70 + if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR || 1.71 + compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR) 1.72 + ERREXIT(cinfo, JERR_BAD_SAMPLING); 1.73 + cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, 1.74 + compptr->h_samp_factor); 1.75 + cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, 1.76 + compptr->v_samp_factor); 1.77 + } 1.78 + 1.79 +#if JPEG_LIB_VERSION >=80 1.80 + cinfo->block_size = DCTSIZE; 1.81 + cinfo->natural_order = jpeg_natural_order; 1.82 + cinfo->lim_Se = DCTSIZE2-1; 1.83 +#endif 1.84 + 1.85 + /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE. 1.86 + * In the full decompressor, this will be overridden by jdmaster.c; 1.87 + * but in the transcoder, jdmaster.c is not used, so we must do it here. 1.88 + */ 1.89 +#if JPEG_LIB_VERSION >= 70 1.90 + cinfo->min_DCT_h_scaled_size = cinfo->min_DCT_v_scaled_size = DCTSIZE; 1.91 +#else 1.92 + cinfo->min_DCT_scaled_size = DCTSIZE; 1.93 +#endif 1.94 + 1.95 + /* Compute dimensions of components */ 1.96 + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 1.97 + ci++, compptr++) { 1.98 +#if JPEG_LIB_VERSION >= 70 1.99 + compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE; 1.100 +#else 1.101 + compptr->DCT_scaled_size = DCTSIZE; 1.102 +#endif 1.103 + /* Size in DCT blocks */ 1.104 + compptr->width_in_blocks = (JDIMENSION) 1.105 + jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor, 1.106 + (long) (cinfo->max_h_samp_factor * DCTSIZE)); 1.107 + compptr->height_in_blocks = (JDIMENSION) 1.108 + jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor, 1.109 + (long) (cinfo->max_v_samp_factor * DCTSIZE)); 1.110 + /* downsampled_width and downsampled_height will also be overridden by 1.111 + * jdmaster.c if we are doing full decompression. The transcoder library 1.112 + * doesn't use these values, but the calling application might. 1.113 + */ 1.114 + /* Size in samples */ 1.115 + compptr->downsampled_width = (JDIMENSION) 1.116 + jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor, 1.117 + (long) cinfo->max_h_samp_factor); 1.118 + compptr->downsampled_height = (JDIMENSION) 1.119 + jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor, 1.120 + (long) cinfo->max_v_samp_factor); 1.121 + /* Mark component needed, until color conversion says otherwise */ 1.122 + compptr->component_needed = TRUE; 1.123 + /* Mark no quantization table yet saved for component */ 1.124 + compptr->quant_table = NULL; 1.125 + } 1.126 + 1.127 + /* Compute number of fully interleaved MCU rows. */ 1.128 + cinfo->total_iMCU_rows = (JDIMENSION) 1.129 + jdiv_round_up((long) cinfo->image_height, 1.130 + (long) (cinfo->max_v_samp_factor*DCTSIZE)); 1.131 + 1.132 + /* Decide whether file contains multiple scans */ 1.133 + if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode) 1.134 + cinfo->inputctl->has_multiple_scans = TRUE; 1.135 + else 1.136 + cinfo->inputctl->has_multiple_scans = FALSE; 1.137 +} 1.138 + 1.139 + 1.140 +LOCAL(void) 1.141 +per_scan_setup (j_decompress_ptr cinfo) 1.142 +/* Do computations that are needed before processing a JPEG scan */ 1.143 +/* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */ 1.144 +{ 1.145 + int ci, mcublks, tmp; 1.146 + jpeg_component_info *compptr; 1.147 + 1.148 + if (cinfo->comps_in_scan == 1) { 1.149 + 1.150 + /* Noninterleaved (single-component) scan */ 1.151 + compptr = cinfo->cur_comp_info[0]; 1.152 + 1.153 + /* Overall image size in MCUs */ 1.154 + cinfo->MCUs_per_row = compptr->width_in_blocks; 1.155 + cinfo->MCU_rows_in_scan = compptr->height_in_blocks; 1.156 + 1.157 + /* For noninterleaved scan, always one block per MCU */ 1.158 + compptr->MCU_width = 1; 1.159 + compptr->MCU_height = 1; 1.160 + compptr->MCU_blocks = 1; 1.161 + compptr->MCU_sample_width = compptr->_DCT_scaled_size; 1.162 + compptr->last_col_width = 1; 1.163 + /* For noninterleaved scans, it is convenient to define last_row_height 1.164 + * as the number of block rows present in the last iMCU row. 1.165 + */ 1.166 + tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor); 1.167 + if (tmp == 0) tmp = compptr->v_samp_factor; 1.168 + compptr->last_row_height = tmp; 1.169 + 1.170 + /* Prepare array describing MCU composition */ 1.171 + cinfo->blocks_in_MCU = 1; 1.172 + cinfo->MCU_membership[0] = 0; 1.173 + 1.174 + } else { 1.175 + 1.176 + /* Interleaved (multi-component) scan */ 1.177 + if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) 1.178 + ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, 1.179 + MAX_COMPS_IN_SCAN); 1.180 + 1.181 + /* Overall image size in MCUs */ 1.182 + cinfo->MCUs_per_row = (JDIMENSION) 1.183 + jdiv_round_up((long) cinfo->image_width, 1.184 + (long) (cinfo->max_h_samp_factor*DCTSIZE)); 1.185 + cinfo->MCU_rows_in_scan = (JDIMENSION) 1.186 + jdiv_round_up((long) cinfo->image_height, 1.187 + (long) (cinfo->max_v_samp_factor*DCTSIZE)); 1.188 + 1.189 + cinfo->blocks_in_MCU = 0; 1.190 + 1.191 + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 1.192 + compptr = cinfo->cur_comp_info[ci]; 1.193 + /* Sampling factors give # of blocks of component in each MCU */ 1.194 + compptr->MCU_width = compptr->h_samp_factor; 1.195 + compptr->MCU_height = compptr->v_samp_factor; 1.196 + compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; 1.197 + compptr->MCU_sample_width = compptr->MCU_width * compptr->_DCT_scaled_size; 1.198 + /* Figure number of non-dummy blocks in last MCU column & row */ 1.199 + tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); 1.200 + if (tmp == 0) tmp = compptr->MCU_width; 1.201 + compptr->last_col_width = tmp; 1.202 + tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); 1.203 + if (tmp == 0) tmp = compptr->MCU_height; 1.204 + compptr->last_row_height = tmp; 1.205 + /* Prepare array describing MCU composition */ 1.206 + mcublks = compptr->MCU_blocks; 1.207 + if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU) 1.208 + ERREXIT(cinfo, JERR_BAD_MCU_SIZE); 1.209 + while (mcublks-- > 0) { 1.210 + cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; 1.211 + } 1.212 + } 1.213 + 1.214 + } 1.215 +} 1.216 + 1.217 + 1.218 +/* 1.219 + * Save away a copy of the Q-table referenced by each component present 1.220 + * in the current scan, unless already saved during a prior scan. 1.221 + * 1.222 + * In a multiple-scan JPEG file, the encoder could assign different components 1.223 + * the same Q-table slot number, but change table definitions between scans 1.224 + * so that each component uses a different Q-table. (The IJG encoder is not 1.225 + * currently capable of doing this, but other encoders might.) Since we want 1.226 + * to be able to dequantize all the components at the end of the file, this 1.227 + * means that we have to save away the table actually used for each component. 1.228 + * We do this by copying the table at the start of the first scan containing 1.229 + * the component. 1.230 + * The JPEG spec prohibits the encoder from changing the contents of a Q-table 1.231 + * slot between scans of a component using that slot. If the encoder does so 1.232 + * anyway, this decoder will simply use the Q-table values that were current 1.233 + * at the start of the first scan for the component. 1.234 + * 1.235 + * The decompressor output side looks only at the saved quant tables, 1.236 + * not at the current Q-table slots. 1.237 + */ 1.238 + 1.239 +LOCAL(void) 1.240 +latch_quant_tables (j_decompress_ptr cinfo) 1.241 +{ 1.242 + int ci, qtblno; 1.243 + jpeg_component_info *compptr; 1.244 + JQUANT_TBL * qtbl; 1.245 + 1.246 + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 1.247 + compptr = cinfo->cur_comp_info[ci]; 1.248 + /* No work if we already saved Q-table for this component */ 1.249 + if (compptr->quant_table != NULL) 1.250 + continue; 1.251 + /* Make sure specified quantization table is present */ 1.252 + qtblno = compptr->quant_tbl_no; 1.253 + if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS || 1.254 + cinfo->quant_tbl_ptrs[qtblno] == NULL) 1.255 + ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno); 1.256 + /* OK, save away the quantization table */ 1.257 + qtbl = (JQUANT_TBL *) 1.258 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.259 + SIZEOF(JQUANT_TBL)); 1.260 + MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL)); 1.261 + compptr->quant_table = qtbl; 1.262 + } 1.263 +} 1.264 + 1.265 + 1.266 +/* 1.267 + * Initialize the input modules to read a scan of compressed data. 1.268 + * The first call to this is done by jdmaster.c after initializing 1.269 + * the entire decompressor (during jpeg_start_decompress). 1.270 + * Subsequent calls come from consume_markers, below. 1.271 + */ 1.272 + 1.273 +METHODDEF(void) 1.274 +start_input_pass (j_decompress_ptr cinfo) 1.275 +{ 1.276 + per_scan_setup(cinfo); 1.277 + latch_quant_tables(cinfo); 1.278 + (*cinfo->entropy->start_pass) (cinfo); 1.279 + (*cinfo->coef->start_input_pass) (cinfo); 1.280 + cinfo->inputctl->consume_input = cinfo->coef->consume_data; 1.281 +} 1.282 + 1.283 + 1.284 +/* 1.285 + * Finish up after inputting a compressed-data scan. 1.286 + * This is called by the coefficient controller after it's read all 1.287 + * the expected data of the scan. 1.288 + */ 1.289 + 1.290 +METHODDEF(void) 1.291 +finish_input_pass (j_decompress_ptr cinfo) 1.292 +{ 1.293 + cinfo->inputctl->consume_input = consume_markers; 1.294 +} 1.295 + 1.296 + 1.297 +/* 1.298 + * Read JPEG markers before, between, or after compressed-data scans. 1.299 + * Change state as necessary when a new scan is reached. 1.300 + * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI. 1.301 + * 1.302 + * The consume_input method pointer points either here or to the 1.303 + * coefficient controller's consume_data routine, depending on whether 1.304 + * we are reading a compressed data segment or inter-segment markers. 1.305 + */ 1.306 + 1.307 +METHODDEF(int) 1.308 +consume_markers (j_decompress_ptr cinfo) 1.309 +{ 1.310 + my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl; 1.311 + int val; 1.312 + 1.313 + if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */ 1.314 + return JPEG_REACHED_EOI; 1.315 + 1.316 + val = (*cinfo->marker->read_markers) (cinfo); 1.317 + 1.318 + switch (val) { 1.319 + case JPEG_REACHED_SOS: /* Found SOS */ 1.320 + if (inputctl->inheaders) { /* 1st SOS */ 1.321 + initial_setup(cinfo); 1.322 + inputctl->inheaders = FALSE; 1.323 + /* Note: start_input_pass must be called by jdmaster.c 1.324 + * before any more input can be consumed. jdapimin.c is 1.325 + * responsible for enforcing this sequencing. 1.326 + */ 1.327 + } else { /* 2nd or later SOS marker */ 1.328 + if (! inputctl->pub.has_multiple_scans) 1.329 + ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */ 1.330 + start_input_pass(cinfo); 1.331 + } 1.332 + break; 1.333 + case JPEG_REACHED_EOI: /* Found EOI */ 1.334 + inputctl->pub.eoi_reached = TRUE; 1.335 + if (inputctl->inheaders) { /* Tables-only datastream, apparently */ 1.336 + if (cinfo->marker->saw_SOF) 1.337 + ERREXIT(cinfo, JERR_SOF_NO_SOS); 1.338 + } else { 1.339 + /* Prevent infinite loop in coef ctlr's decompress_data routine 1.340 + * if user set output_scan_number larger than number of scans. 1.341 + */ 1.342 + if (cinfo->output_scan_number > cinfo->input_scan_number) 1.343 + cinfo->output_scan_number = cinfo->input_scan_number; 1.344 + } 1.345 + break; 1.346 + case JPEG_SUSPENDED: 1.347 + break; 1.348 + } 1.349 + 1.350 + return val; 1.351 +} 1.352 + 1.353 + 1.354 +/* 1.355 + * Reset state to begin a fresh datastream. 1.356 + */ 1.357 + 1.358 +METHODDEF(void) 1.359 +reset_input_controller (j_decompress_ptr cinfo) 1.360 +{ 1.361 + my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl; 1.362 + 1.363 + inputctl->pub.consume_input = consume_markers; 1.364 + inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */ 1.365 + inputctl->pub.eoi_reached = FALSE; 1.366 + inputctl->inheaders = TRUE; 1.367 + /* Reset other modules */ 1.368 + (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo); 1.369 + (*cinfo->marker->reset_marker_reader) (cinfo); 1.370 + /* Reset progression state -- would be cleaner if entropy decoder did this */ 1.371 + cinfo->coef_bits = NULL; 1.372 +} 1.373 + 1.374 + 1.375 +/* 1.376 + * Initialize the input controller module. 1.377 + * This is called only once, when the decompression object is created. 1.378 + */ 1.379 + 1.380 +GLOBAL(void) 1.381 +jinit_input_controller (j_decompress_ptr cinfo) 1.382 +{ 1.383 + my_inputctl_ptr inputctl; 1.384 + 1.385 + /* Create subobject in permanent pool */ 1.386 + inputctl = (my_inputctl_ptr) 1.387 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, 1.388 + SIZEOF(my_input_controller)); 1.389 + cinfo->inputctl = (struct jpeg_input_controller *) inputctl; 1.390 + /* Initialize method pointers */ 1.391 + inputctl->pub.consume_input = consume_markers; 1.392 + inputctl->pub.reset_input_controller = reset_input_controller; 1.393 + inputctl->pub.start_input_pass = start_input_pass; 1.394 + inputctl->pub.finish_input_pass = finish_input_pass; 1.395 + /* Initialize state: can't use reset_input_controller since we don't 1.396 + * want to try to reset other modules yet. 1.397 + */ 1.398 + inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */ 1.399 + inputctl->pub.eoi_reached = FALSE; 1.400 + inputctl->inheaders = TRUE; 1.401 +}