1 /*
2  * jdinput.c
3  *
4  * Copyright (C) 1991-1998, Thomas G. Lane.
5  * This file is part of the Independent JPEG Group's software.
6  * For conditions of distribution and use, see the accompanying README file.
7  *
8  * This file contains input control logic for the JPEG decompressor.
9  * These routines are concerned with controlling the decompressor's input
10  * processing (marker reading and coefficient/difference decoding).
11  * The actual input reading is done in jdmarker.c, jdhuff.c, jdphuff.c,
12  * and jdlhuff.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)
initial_setup(j_decompress_ptr cinfo)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   if (cinfo->process == JPROC_LOSSLESS) {
52     /* If precision > compiled-in value, we must downscale */
53     if (cinfo->data_precision > BITS_IN_JSAMPLE)
54       WARNMS2(cinfo, JWRN_MUST_DOWNSCALE,
55         cinfo->data_precision, BITS_IN_JSAMPLE);
56   }
57   else {  /* Lossy processes */
58     /* For now, precision must match compiled-in value... */
59     if (cinfo->data_precision != BITS_IN_JSAMPLE)
60       ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
61   }
62 
63   /* Check that number of components won't exceed internal array sizes */
64   if (cinfo->num_components > MAX_COMPONENTS)
65     ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
66        MAX_COMPONENTS);
67 
68   /* Compute maximum sampling factors; check factor validity */
69   cinfo->max_h_samp_factor = 1;
70   cinfo->max_v_samp_factor = 1;
71   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
72        ci++, compptr++) {
73     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
74   compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
75       ERREXIT(cinfo, JERR_BAD_SAMPLING);
76     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
77            compptr->h_samp_factor);
78     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
79            compptr->v_samp_factor);
80   }
81 
82   /* We initialize codec_data_unit and min_codec_data_unit to data_unit.
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    */
86   cinfo->min_codec_data_unit = cinfo->data_unit;
87 
88   /* Compute dimensions of components */
89   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
90        ci++, compptr++) {
91     compptr->codec_data_unit = cinfo->data_unit;
92     /* Size in data units */
93     compptr->width_in_data_units = (JDIMENSION)
94       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
95         (long) (cinfo->max_h_samp_factor * cinfo->data_unit));
96     compptr->height_in_data_units = (JDIMENSION)
97       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
98         (long) (cinfo->max_v_samp_factor * cinfo->data_unit));
99     /* downsampled_width and downsampled_height will also be overridden by
100      * jdmaster.c if we are doing full decompression.  The transcoder library
101      * doesn't use these values, but the calling application might.
102      */
103     /* Size in samples */
104     compptr->downsampled_width = (JDIMENSION)
105       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
106         (long) cinfo->max_h_samp_factor);
107     compptr->downsampled_height = (JDIMENSION)
108       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
109         (long) cinfo->max_v_samp_factor);
110     /* Mark component needed, until color conversion says otherwise */
111     compptr->component_needed = TRUE;
112     /* Mark no quantization table yet saved for component */
113     compptr->quant_table = NULL;
114   }
115 
116   /* Compute number of fully interleaved MCU rows. */
117   cinfo->total_iMCU_rows = (JDIMENSION)
118     jdiv_round_up((long) cinfo->image_height,
119       (long) (cinfo->max_v_samp_factor*cinfo->data_unit));
120 
121   /* Decide whether file contains multiple scans */
122   if (cinfo->comps_in_scan < cinfo->num_components ||
123       cinfo->process == JPROC_PROGRESSIVE)
124     cinfo->inputctl->has_multiple_scans = TRUE;
125   else
126     cinfo->inputctl->has_multiple_scans = FALSE;
127 }
128 
129 
130 LOCAL(void)
per_scan_setup(j_decompress_ptr cinfo)131 per_scan_setup (j_decompress_ptr cinfo)
132 /* Do computations that are needed before processing a JPEG scan */
133 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
134 {
135   int ci, mcublks, tmp;
136   jpeg_component_info *compptr;
137 
138   if (cinfo->comps_in_scan == 1) {
139 
140     /* Noninterleaved (single-component) scan */
141     compptr = cinfo->cur_comp_info[0];
142 
143     /* Overall image size in MCUs */
144     cinfo->MCUs_per_row = compptr->width_in_data_units;
145     cinfo->MCU_rows_in_scan = compptr->height_in_data_units;
146 
147     /* For noninterleaved scan, always one data unit per MCU */
148     compptr->MCU_width = 1;
149     compptr->MCU_height = 1;
150     compptr->MCU_data_units = 1;
151     compptr->MCU_sample_width = compptr->codec_data_unit;
152     compptr->last_col_width = 1;
153     /* For noninterleaved scans, it is convenient to define last_row_height
154      * as the number of data unit rows present in the last iMCU row.
155      */
156     tmp = (int) (compptr->height_in_data_units % compptr->v_samp_factor);
157     if (tmp == 0) tmp = compptr->v_samp_factor;
158     compptr->last_row_height = tmp;
159 
160     /* Prepare array describing MCU composition */
161     cinfo->data_units_in_MCU = 1;
162     cinfo->MCU_membership[0] = 0;
163 
164   } else {
165 
166     /* Interleaved (multi-component) scan */
167     if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
168       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
169          MAX_COMPS_IN_SCAN);
170 
171     /* Overall image size in MCUs */
172     cinfo->MCUs_per_row = (JDIMENSION)
173       jdiv_round_up((long) cinfo->image_width,
174         (long) (cinfo->max_h_samp_factor*cinfo->data_unit));
175     cinfo->MCU_rows_in_scan = (JDIMENSION)
176       jdiv_round_up((long) cinfo->image_height,
177         (long) (cinfo->max_v_samp_factor*cinfo->data_unit));
178 
179     cinfo->data_units_in_MCU = 0;
180 
181     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
182       compptr = cinfo->cur_comp_info[ci];
183       /* Sampling factors give # of data units of component in each MCU */
184       compptr->MCU_width = compptr->h_samp_factor;
185       compptr->MCU_height = compptr->v_samp_factor;
186       compptr->MCU_data_units = compptr->MCU_width * compptr->MCU_height;
187       compptr->MCU_sample_width = compptr->MCU_width * compptr->codec_data_unit;
188       /* Figure number of non-dummy data units in last MCU column & row */
189       tmp = (int) (compptr->width_in_data_units % compptr->MCU_width);
190       if (tmp == 0) tmp = compptr->MCU_width;
191       compptr->last_col_width = tmp;
192       tmp = (int) (compptr->height_in_data_units % compptr->MCU_height);
193       if (tmp == 0) tmp = compptr->MCU_height;
194       compptr->last_row_height = tmp;
195       /* Prepare array describing MCU composition */
196       mcublks = compptr->MCU_data_units;
197       if (cinfo->data_units_in_MCU + mcublks > D_MAX_DATA_UNITS_IN_MCU)
198   ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
199       while (mcublks-- > 0) {
200   cinfo->MCU_membership[cinfo->data_units_in_MCU++] = ci;
201       }
202     }
203 
204   }
205 }
206 
207 
208 /*
209  * Initialize the input modules to read a scan of compressed data.
210  * The first call to this is done by jdmaster.c after initializing
211  * the entire decompressor (during jpeg_start_decompress).
212  * Subsequent calls come from consume_markers, below.
213  */
214 
215 METHODDEF(void)
start_input_pass(j_decompress_ptr cinfo)216 start_input_pass (j_decompress_ptr cinfo)
217 {
218   per_scan_setup(cinfo);
219   (*cinfo->codec->start_input_pass) (cinfo);
220   cinfo->inputctl->consume_input = cinfo->codec->consume_data;
221 }
222 
223 
224 /*
225  * Finish up after inputting a compressed-data scan.
226  * This is called by the coefficient controller after it's read all
227  * the expected data of the scan.
228  */
229 
230 METHODDEF(void)
finish_input_pass(j_decompress_ptr cinfo)231 finish_input_pass (j_decompress_ptr cinfo)
232 {
233   cinfo->inputctl->consume_input = consume_markers;
234 }
235 
236 
237 /*
238  * Read JPEG markers before, between, or after compressed-data scans.
239  * Change state as necessary when a new scan is reached.
240  * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
241  *
242  * The consume_input method pointer points either here or to the
243  * coefficient controller's consume_data routine, depending on whether
244  * we are reading a compressed data segment or inter-segment markers.
245  */
246 
247 METHODDEF(int)
consume_markers(j_decompress_ptr cinfo)248 consume_markers (j_decompress_ptr cinfo)
249 {
250   my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
251   int val;
252 
253   if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
254     return JPEG_REACHED_EOI;
255 
256   val = (*cinfo->marker->read_markers) (cinfo);
257 
258   switch (val) {
259   case JPEG_REACHED_SOS:  /* Found SOS */
260     if (inputctl->inheaders) {  /* 1st SOS */
261       initial_setup(cinfo);
262       /*
263        * Initialize the decompression codec.  We need to do this here so that
264        * any codec-specific fields and function pointers are available to
265        * the rest of the library.
266        */
267       jinit_d_codec(cinfo);
268       inputctl->inheaders = FALSE;
269       /* Note: start_input_pass must be called by jdmaster.c
270        * before any more input can be consumed.  jdapimin.c is
271        * responsible for enforcing this sequencing.
272        */
273     } else {      /* 2nd or later SOS marker */
274       if (! inputctl->pub.has_multiple_scans)
275   ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
276       start_input_pass(cinfo);
277     }
278     break;
279   case JPEG_REACHED_EOI:  /* Found EOI */
280     inputctl->pub.eoi_reached = TRUE;
281     if (inputctl->inheaders) {  /* Tables-only datastream, apparently */
282       if (cinfo->marker->saw_SOF)
283   ERREXIT(cinfo, JERR_SOF_NO_SOS);
284     } else {
285       /* Prevent infinite loop in coef ctlr's decompress_data routine
286        * if user set output_scan_number larger than number of scans.
287        */
288       if (cinfo->output_scan_number > cinfo->input_scan_number)
289   cinfo->output_scan_number = cinfo->input_scan_number;
290     }
291     break;
292   case JPEG_SUSPENDED:
293     break;
294   }
295 
296   return val;
297 }
298 
299 
300 /*
301  * Reset state to begin a fresh datastream.
302  */
303 
304 METHODDEF(void)
reset_input_controller(j_decompress_ptr cinfo)305 reset_input_controller (j_decompress_ptr cinfo)
306 {
307   my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
308 
309   inputctl->pub.consume_input = consume_markers;
310   inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
311   inputctl->pub.eoi_reached = FALSE;
312   inputctl->inheaders = TRUE;
313   /* Reset other modules */
314   (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
315   (*cinfo->marker->reset_marker_reader) (cinfo);
316   /* Reset progression state -- would be cleaner if entropy decoder did this */
317   cinfo->coef_bits = NULL;
318 }
319 
320 
321 /*
322  * Initialize the input controller module.
323  * This is called only once, when the decompression object is created.
324  */
325 
326 GLOBAL(void)
jinit_input_controller(j_decompress_ptr cinfo)327 jinit_input_controller (j_decompress_ptr cinfo)
328 {
329   my_inputctl_ptr inputctl;
330 
331   /* Create subobject in permanent pool */
332   inputctl = (my_inputctl_ptr)
333     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
334         SIZEOF(my_input_controller));
335   cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
336   /* Initialize method pointers */
337   inputctl->pub.consume_input = consume_markers;
338   inputctl->pub.reset_input_controller = reset_input_controller;
339   inputctl->pub.start_input_pass = start_input_pass;
340   inputctl->pub.finish_input_pass = finish_input_pass;
341   /* Initialize state: can't use reset_input_controller since we don't
342    * want to try to reset other modules yet.
343    */
344   inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
345   inputctl->pub.eoi_reached = FALSE;
346   inputctl->inheaders = TRUE;
347 }
348