4 * Copyright (C) 1994-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.
8 * This file contains the [un]difference buffer controller for decompression.
9 * This controller is the top level of the lossless JPEG decompressor proper.
10 * The difference buffer lies between the entropy decoding and
11 * prediction/undifferencing steps. The undifference buffer lies between the
12 * prediction/undifferencing and scaling steps.
14 * In buffered-image mode, this controller is the interface between
15 * input-oriented processing and output-oriented processing.
18 #define JPEG_INTERNALS
24 #ifdef D_LOSSLESS_SUPPORTED
26 /* Private buffer controller object */
29 /* These variables keep track of the current location of the input side. */
30 /* cinfo->input_iMCU_row is also used for this. */
31 JDIMENSION MCU_ctr; /* counts MCUs processed in current row */
32 unsigned int restart_rows_to_go; /* MCU-rows left in this restart interval */
33 unsigned int MCU_vert_offset; /* counts MCU rows within iMCU row */
34 unsigned int MCU_rows_per_iMCU_row; /* number of such rows needed */
36 /* The output side's location is represented by cinfo->output_iMCU_row. */
38 JDIFFARRAY diff_buf[MAX_COMPONENTS]; /* iMCU row of differences */
39 JDIFFARRAY undiff_buf[MAX_COMPONENTS]; /* iMCU row of undiff'd samples */
41 #ifdef D_MULTISCAN_FILES_SUPPORTED
42 /* In multi-pass modes, we need a virtual sample array for each component. */
43 jvirt_sarray_ptr whole_image[MAX_COMPONENTS];
47 typedef d_diff_controller * d_diff_ptr;
49 /* Forward declarations */
50 METHODDEF(int) decompress_data
51 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
52 #ifdef D_MULTISCAN_FILES_SUPPORTED
53 METHODDEF(int) output_data
54 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
59 start_iMCU_row (j_decompress_ptr cinfo)
60 /* Reset within-iMCU-row counters for a new row (input side) */
62 j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
63 d_diff_ptr diff = (d_diff_ptr) losslsd->diff_private;
65 /* In an interleaved scan, an MCU row is the same as an iMCU row.
66 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
67 * But at the bottom of the image, process only what's left.
69 if (cinfo->comps_in_scan > 1) {
70 diff->MCU_rows_per_iMCU_row = 1;
72 if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
73 diff->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
75 diff->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
79 diff->MCU_vert_offset = 0;
84 * Initialize for an input processing pass.
88 start_input_pass (j_decompress_ptr cinfo)
90 j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
91 d_diff_ptr diff = (d_diff_ptr) losslsd->diff_private;
93 /* Check that the restart interval is an integer multiple of the number
94 * of MCU in an MCU-row.
96 if (cinfo->restart_interval % cinfo->MCUs_per_row != 0)
97 ERREXIT2(cinfo, JERR_BAD_RESTART,
98 cinfo->restart_interval, cinfo->MCUs_per_row);
100 /* Initialize restart counter */
101 diff->restart_rows_to_go = cinfo->restart_interval / cinfo->MCUs_per_row;
103 cinfo->input_iMCU_row = 0;
104 start_iMCU_row(cinfo);
109 * Check for a restart marker & resynchronize decoder, undifferencer.
110 * Returns FALSE if must suspend.
114 process_restart (j_decompress_ptr cinfo)
116 j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
117 d_diff_ptr diff = (d_diff_ptr) losslsd->diff_private;
119 if (! (*losslsd->entropy_process_restart) (cinfo))
122 (*losslsd->predict_process_restart) (cinfo);
124 /* Reset restart counter */
125 diff->restart_rows_to_go = cinfo->restart_interval / cinfo->MCUs_per_row;
132 * Initialize for an output processing pass.
136 start_output_pass (j_decompress_ptr cinfo)
138 cinfo->output_iMCU_row = 0;
143 * Decompress and return some data in the supplied buffer.
144 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
145 * Input and output must run in lockstep since we have only a one-MCU buffer.
146 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
148 * NB: output_buf contains a plane for each component in image,
149 * which we index according to the component's SOF position.
153 decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
155 j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
156 d_diff_ptr diff = (d_diff_ptr) losslsd->diff_private;
157 JDIMENSION MCU_col_num; /* index of current MCU within row */
158 JDIMENSION MCU_count; /* number of MCUs decoded */
159 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
160 int comp, ci, row, prev_row;
161 unsigned int yoffset;
162 jpeg_component_info *compptr;
164 /* Loop to process as much as one whole iMCU row */
165 for (yoffset = diff->MCU_vert_offset; yoffset < diff->MCU_rows_per_iMCU_row;
168 /* Process restart marker if needed; may have to suspend */
169 if (cinfo->restart_interval) {
170 if (diff->restart_rows_to_go == 0)
171 if (! process_restart(cinfo))
172 return JPEG_SUSPENDED;
175 MCU_col_num = diff->MCU_ctr;
176 /* Try to fetch an MCU-row (or remaining portion of suspended MCU-row). */
178 (*losslsd->entropy_decode_mcus) (cinfo,
179 diff->diff_buf, yoffset, MCU_col_num,
180 cinfo->MCUs_per_row - MCU_col_num);
181 if (MCU_count != cinfo->MCUs_per_row - MCU_col_num) {
182 /* Suspension forced; update state counters and exit */
183 diff->MCU_vert_offset = yoffset;
184 diff->MCU_ctr += MCU_count;
185 return JPEG_SUSPENDED;
188 /* Account for restart interval (no-op if not using restarts) */
189 diff->restart_rows_to_go--;
191 /* Completed an MCU row, but perhaps not an iMCU row */
196 * Undifference and scale each scanline of the disassembled MCU-row
197 * separately. We do not process dummy samples at the end of a scanline
198 * or dummy rows at the end of the image.
200 for (comp = 0; comp < cinfo->comps_in_scan; comp++) {
201 compptr = cinfo->cur_comp_info[comp];
202 ci = compptr->component_index;
203 for (row = 0, prev_row = compptr->v_samp_factor - 1;
204 row < (cinfo->input_iMCU_row == last_iMCU_row ?
205 compptr->last_row_height : compptr->v_samp_factor);
206 prev_row = row, row++) {
207 (*losslsd->predict_undifference[ci]) (cinfo, ci,
208 diff->diff_buf[ci][row],
209 diff->undiff_buf[ci][prev_row],
210 diff->undiff_buf[ci][row],
211 compptr->width_in_data_units);
212 (*losslsd->scaler_scale) (cinfo, diff->undiff_buf[ci][row],
214 compptr->width_in_data_units);
218 /* Completed the iMCU row, advance counters for next one.
220 * NB: output_data will increment output_iMCU_row.
221 * This counter is not needed for the single-pass case
222 * or the input side of the multi-pass case.
224 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
225 start_iMCU_row(cinfo);
226 return JPEG_ROW_COMPLETED;
228 /* Completed the scan */
229 (*cinfo->inputctl->finish_input_pass) (cinfo);
230 return JPEG_SCAN_COMPLETED;
235 * Dummy consume-input routine for single-pass operation.
239 dummy_consume_data (j_decompress_ptr cinfo)
242 return JPEG_SUSPENDED; /* Always indicate nothing was done */
246 #ifdef D_MULTISCAN_FILES_SUPPORTED
249 * Consume input data and store it in the full-image sample buffer.
250 * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
251 * ie, v_samp_factor rows for each component in the scan.
252 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
256 consume_data (j_decompress_ptr cinfo)
258 j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
259 d_diff_ptr diff = (d_diff_ptr) losslsd->diff_private;
260 /* JDIMENSION MCU_col_num; */ /* index of current MCU within row */
261 /* JDIMENSION MCU_count; */ /* number of MCUs decoded */
262 /* JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; */
263 int comp, ci /* , yoffset, row, prev_row */;
264 JSAMPARRAY buffer[MAX_COMPS_IN_SCAN];
265 jpeg_component_info *compptr;
267 /* Align the virtual buffers for the components used in this scan. */
268 for (comp = 0; comp < cinfo->comps_in_scan; comp++) {
269 compptr = cinfo->cur_comp_info[comp];
270 ci = compptr->component_index;
271 buffer[ci] = (*cinfo->mem->access_virt_sarray)
272 ((j_common_ptr) cinfo, diff->whole_image[ci],
273 cinfo->input_iMCU_row * compptr->v_samp_factor,
274 (JDIMENSION) compptr->v_samp_factor, TRUE);
277 return decompress_data(cinfo, buffer);
282 * Output some data from the full-image buffer sample in the multi-pass case.
283 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
284 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
286 * NB: output_buf contains a plane for each component in image.
290 output_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
292 j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
293 d_diff_ptr diff = (d_diff_ptr) losslsd->diff_private;
294 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
295 int ci, samp_rows, row;
297 jpeg_component_info *compptr;
299 /* Force some input to be done if we are getting ahead of the input. */
300 while (cinfo->input_scan_number < cinfo->output_scan_number ||
301 (cinfo->input_scan_number == cinfo->output_scan_number &&
302 cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
303 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
304 return JPEG_SUSPENDED;
307 /* OK, output from the virtual arrays. */
308 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
310 /* Align the virtual buffer for this component. */
311 buffer = (*cinfo->mem->access_virt_sarray)
312 ((j_common_ptr) cinfo, diff->whole_image[ci],
313 cinfo->output_iMCU_row * compptr->v_samp_factor,
314 (JDIMENSION) compptr->v_samp_factor, FALSE);
316 if (cinfo->output_iMCU_row < last_iMCU_row)
317 samp_rows = compptr->v_samp_factor;
319 /* NB: can't use last_row_height here; it is input-side-dependent! */
320 samp_rows = (int) (compptr->height_in_data_units % compptr->v_samp_factor);
321 if (samp_rows == 0) samp_rows = compptr->v_samp_factor;
324 for (row = 0; row < samp_rows; row++) {
325 MEMCOPY(output_buf[ci][row], buffer[row],
326 compptr->width_in_data_units * SIZEOF(JSAMPLE));
330 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
331 return JPEG_ROW_COMPLETED;
332 return JPEG_SCAN_COMPLETED;
335 #endif /* D_MULTISCAN_FILES_SUPPORTED */
339 * Initialize difference buffer controller.
343 jinit_d_diff_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
345 j_lossless_d_ptr losslsd = (j_lossless_d_ptr) cinfo->codec;
348 jpeg_component_info *compptr;
351 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
352 SIZEOF(d_diff_controller));
353 losslsd->diff_private = (void *) diff;
354 losslsd->diff_start_input_pass = start_input_pass;
355 losslsd->pub.start_output_pass = start_output_pass;
357 /* Create the [un]difference buffers. */
358 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
360 diff->diff_buf[ci] = (*cinfo->mem->alloc_darray)
361 ((j_common_ptr) cinfo, JPOOL_IMAGE,
362 (JDIMENSION) jround_up((long) compptr->width_in_data_units,
363 (long) compptr->h_samp_factor),
364 (JDIMENSION) compptr->v_samp_factor);
365 diff->undiff_buf[ci] = (*cinfo->mem->alloc_darray)
366 ((j_common_ptr) cinfo, JPOOL_IMAGE,
367 (JDIMENSION) jround_up((long) compptr->width_in_data_units,
368 (long) compptr->h_samp_factor),
369 (JDIMENSION) compptr->v_samp_factor);
372 if (need_full_buffer) {
373 #ifdef D_MULTISCAN_FILES_SUPPORTED
374 /* Allocate a full-image virtual array for each component. */
377 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
379 access_rows = compptr->v_samp_factor;
380 diff->whole_image[ci] = (*cinfo->mem->request_virt_sarray)
381 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
382 (JDIMENSION) jround_up((long) compptr->width_in_data_units,
383 (long) compptr->h_samp_factor),
384 (JDIMENSION) jround_up((long) compptr->height_in_data_units,
385 (long) compptr->v_samp_factor),
386 (JDIMENSION) access_rows);
388 losslsd->pub.consume_data = consume_data;
389 losslsd->pub.decompress_data = output_data;
391 ERREXIT(cinfo, JERR_NOT_COMPILED);
394 losslsd->pub.consume_data = dummy_consume_data;
395 losslsd->pub.decompress_data = decompress_data;
396 diff->whole_image[0] = NULL; /* flag for no virtual arrays */
400 #endif /* D_LOSSLESS_SUPPORTED */