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