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