media/libjpeg/jdinput.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial