blob: c0cd0bd506173a04cad0281e6113dba3c653ba34 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jdcoefct.c
3 *
Thomas G. Lane489583f1996-02-07 00:00:00 +00004 * Copyright (C) 1994-1996, Thomas G. Lane.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00005 * 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 the coefficient buffer controller for decompression.
9 * This controller is the top level of the JPEG decompressor proper.
10 * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000011 *
12 * In buffered-image mode, this controller is the interface between
13 * input-oriented processing and output-oriented processing.
14 * Also, the input side (only) is used when reading a file for transcoding.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000015 */
16
17#define JPEG_INTERNALS
18#include "jinclude.h"
19#include "jpeglib.h"
20
Thomas G. Lanebc79e061995-08-02 00:00:00 +000021/* Block smoothing is only applicable for progressive JPEG, so: */
22#ifndef D_PROGRESSIVE_SUPPORTED
23#undef BLOCK_SMOOTHING_SUPPORTED
24#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000025
26/* Private buffer controller object */
27
28typedef struct {
29 struct jpeg_d_coef_controller pub; /* public fields */
30
Thomas G. Lanebc79e061995-08-02 00:00:00 +000031 /* These variables keep track of the current location of the input side. */
32 /* cinfo->input_iMCU_row is also used for this. */
33 JDIMENSION MCU_ctr; /* counts MCUs processed in current row */
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000034 int MCU_vert_offset; /* counts MCU rows within iMCU row */
35 int MCU_rows_per_iMCU_row; /* number of such rows needed */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000036
Thomas G. Lanebc79e061995-08-02 00:00:00 +000037 /* The output side's location is represented by cinfo->output_iMCU_row. */
38
39 /* In single-pass modes, it's sufficient to buffer just one MCU.
40 * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000041 * and let the entropy decoder write into that workspace each time.
42 * (On 80x86, the workspace is FAR even though it's not really very big;
43 * this is to keep the module interfaces unchanged when a large coefficient
44 * buffer is necessary.)
45 * In multi-pass modes, this array points to the current MCU's blocks
Thomas G. Lanebc79e061995-08-02 00:00:00 +000046 * within the virtual arrays; it is used only by the input side.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000047 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000048 JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000049
Thomas G. Lanebc79e061995-08-02 00:00:00 +000050#ifdef D_MULTISCAN_FILES_SUPPORTED
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000051 /* In multi-pass modes, we need a virtual block array for each component. */
52 jvirt_barray_ptr whole_image[MAX_COMPONENTS];
Thomas G. Lanebc79e061995-08-02 00:00:00 +000053#endif
54
55#ifdef BLOCK_SMOOTHING_SUPPORTED
56 /* When doing block smoothing, we latch coefficient Al values here */
57 int * coef_bits_latch;
58#define SAVED_COEFS 6 /* we save coef_bits[0..5] */
59#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000060} my_coef_controller;
61
62typedef my_coef_controller * my_coef_ptr;
63
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000064/* Forward declarations */
Thomas G. Lane489583f1996-02-07 00:00:00 +000065METHODDEF(int) decompress_onepass
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000066 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
67#ifdef D_MULTISCAN_FILES_SUPPORTED
Thomas G. Lane489583f1996-02-07 00:00:00 +000068METHODDEF(int) decompress_data
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000069 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
Thomas G. Lanebc79e061995-08-02 00:00:00 +000070#endif
71#ifdef BLOCK_SMOOTHING_SUPPORTED
Thomas G. Lane489583f1996-02-07 00:00:00 +000072LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
73METHODDEF(int) decompress_smooth_data
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000074 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
75#endif
76
77
Thomas G. Lane489583f1996-02-07 00:00:00 +000078LOCAL(void)
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000079start_iMCU_row (j_decompress_ptr cinfo)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000080/* Reset within-iMCU-row counters for a new row (input side) */
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000081{
82 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
83
84 /* In an interleaved scan, an MCU row is the same as an iMCU row.
85 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
86 * But at the bottom of the image, process only what's left.
87 */
88 if (cinfo->comps_in_scan > 1) {
89 coef->MCU_rows_per_iMCU_row = 1;
90 } else {
Thomas G. Lanebc79e061995-08-02 00:00:00 +000091 if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000092 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
93 else
94 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
95 }
96
Thomas G. Lanebc79e061995-08-02 00:00:00 +000097 coef->MCU_ctr = 0;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000098 coef->MCU_vert_offset = 0;
99}
100
101
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000102/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000103 * Initialize for an input processing pass.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000104 */
105
Thomas G. Lane489583f1996-02-07 00:00:00 +0000106METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000107start_input_pass (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000108{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000109 cinfo->input_iMCU_row = 0;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000110 start_iMCU_row(cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000111}
112
113
114/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000115 * Initialize for an output processing pass.
116 */
117
Thomas G. Lane489583f1996-02-07 00:00:00 +0000118METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000119start_output_pass (j_decompress_ptr cinfo)
120{
121#ifdef BLOCK_SMOOTHING_SUPPORTED
122 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
123
124 /* If multipass, check to see whether to use block smoothing on this pass */
125 if (coef->pub.coef_arrays != NULL) {
126 if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
127 coef->pub.decompress_data = decompress_smooth_data;
128 else
129 coef->pub.decompress_data = decompress_data;
130 }
131#endif
132 cinfo->output_iMCU_row = 0;
133}
134
135
136/*
137 * Decompress and return some data in the single-pass case.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000138 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000139 * Input and output must run in lockstep since we have only a one-MCU buffer.
140 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000141 *
142 * NB: output_buf contains a plane for each component in image.
143 * For single pass, this is the same as the components in the scan.
144 */
145
Thomas G. Lane489583f1996-02-07 00:00:00 +0000146METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000147decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000148{
149 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
150 JDIMENSION MCU_col_num; /* index of current MCU within row */
151 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000152 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
153 int blkn, ci, xindex, yindex, yoffset, useful_width;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000154 JSAMPARRAY output_ptr;
155 JDIMENSION start_col, output_col;
156 jpeg_component_info *compptr;
157 inverse_DCT_method_ptr inverse_DCT;
158
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000159 /* Loop to process as much as one whole iMCU row */
160 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
161 yoffset++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000162 for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000163 MCU_col_num++) {
164 /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
165 jzero_far((void FAR *) coef->MCU_buffer[0],
166 (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
167 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
168 /* Suspension forced; update state counters and exit */
169 coef->MCU_vert_offset = yoffset;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000170 coef->MCU_ctr = MCU_col_num;
171 return JPEG_SUSPENDED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000172 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000173 /* Determine where data should go in output_buf and do the IDCT thing.
174 * We skip dummy blocks at the right and bottom edges (but blkn gets
175 * incremented past them!). Note the inner loop relies on having
176 * allocated the MCU_buffer[] blocks sequentially.
177 */
178 blkn = 0; /* index of current DCT block within MCU */
179 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
180 compptr = cinfo->cur_comp_info[ci];
181 /* Don't bother to IDCT an uninteresting component. */
182 if (! compptr->component_needed) {
183 blkn += compptr->MCU_blocks;
184 continue;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000185 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000186 inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
187 useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
188 : compptr->last_col_width;
189 output_ptr = output_buf[ci] + yoffset * compptr->DCT_scaled_size;
190 start_col = MCU_col_num * compptr->MCU_sample_width;
191 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000192 if (cinfo->input_iMCU_row < last_iMCU_row ||
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000193 yoffset+yindex < compptr->last_row_height) {
194 output_col = start_col;
195 for (xindex = 0; xindex < useful_width; xindex++) {
196 (*inverse_DCT) (cinfo, compptr,
197 (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
198 output_ptr, output_col);
199 output_col += compptr->DCT_scaled_size;
200 }
201 }
202 blkn += compptr->MCU_width;
203 output_ptr += compptr->DCT_scaled_size;
204 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000205 }
206 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000207 /* Completed an MCU row, but perhaps not an iMCU row */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000208 coef->MCU_ctr = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000209 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000210 /* Completed the iMCU row, advance counters for next one */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000211 cinfo->output_iMCU_row++;
212 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
213 start_iMCU_row(cinfo);
214 return JPEG_ROW_COMPLETED;
215 }
216 /* Completed the scan */
217 (*cinfo->inputctl->finish_input_pass) (cinfo);
218 return JPEG_SCAN_COMPLETED;
219}
220
221
222/*
223 * Dummy consume-input routine for single-pass operation.
224 */
225
Thomas G. Lane489583f1996-02-07 00:00:00 +0000226METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000227dummy_consume_data (j_decompress_ptr cinfo)
228{
229 return JPEG_SUSPENDED; /* Always indicate nothing was done */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000230}
231
232
233#ifdef D_MULTISCAN_FILES_SUPPORTED
234
235/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000236 * Consume input data and store it in the full-image coefficient buffer.
237 * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
238 * ie, v_samp_factor block rows for each component in the scan.
239 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000240 */
241
Thomas G. Lane489583f1996-02-07 00:00:00 +0000242METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000243consume_data (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000244{
245 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
246 JDIMENSION MCU_col_num; /* index of current MCU within row */
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000247 int blkn, ci, xindex, yindex, yoffset;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000248 JDIMENSION start_col;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000249 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
250 JBLOCKROW buffer_ptr;
251 jpeg_component_info *compptr;
252
253 /* Align the virtual buffers for the components used in this scan. */
254 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
255 compptr = cinfo->cur_comp_info[ci];
256 buffer[ci] = (*cinfo->mem->access_virt_barray)
257 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000258 cinfo->input_iMCU_row * compptr->v_samp_factor,
259 (JDIMENSION) compptr->v_samp_factor, TRUE);
260 /* Note: entropy decoder expects buffer to be zeroed,
261 * but this is handled automatically by the memory manager
262 * because we requested a pre-zeroed array.
263 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000264 }
265
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000266 /* Loop to process one whole iMCU row */
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000267 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
268 yoffset++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000269 for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000270 MCU_col_num++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000271 /* Construct list of pointers to DCT blocks belonging to this MCU */
272 blkn = 0; /* index of current DCT block within MCU */
273 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
274 compptr = cinfo->cur_comp_info[ci];
275 start_col = MCU_col_num * compptr->MCU_width;
276 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
277 buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
278 for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
279 coef->MCU_buffer[blkn++] = buffer_ptr++;
280 }
281 }
282 }
283 /* Try to fetch the MCU. */
284 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000285 /* Suspension forced; update state counters and exit */
286 coef->MCU_vert_offset = yoffset;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000287 coef->MCU_ctr = MCU_col_num;
288 return JPEG_SUSPENDED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000289 }
290 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000291 /* Completed an MCU row, but perhaps not an iMCU row */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000292 coef->MCU_ctr = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000293 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000294 /* Completed the iMCU row, advance counters for next one */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000295 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
296 start_iMCU_row(cinfo);
297 return JPEG_ROW_COMPLETED;
298 }
299 /* Completed the scan */
300 (*cinfo->inputctl->finish_input_pass) (cinfo);
301 return JPEG_SCAN_COMPLETED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000302}
303
304
305/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000306 * Decompress and return some data in the multi-pass case.
307 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
308 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000309 *
310 * NB: output_buf contains a plane for each component in image.
311 */
312
Thomas G. Lane489583f1996-02-07 00:00:00 +0000313METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000314decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000315{
316 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000317 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000318 JDIMENSION block_num;
319 int ci, block_row, block_rows;
320 JBLOCKARRAY buffer;
321 JBLOCKROW buffer_ptr;
322 JSAMPARRAY output_ptr;
323 JDIMENSION output_col;
324 jpeg_component_info *compptr;
325 inverse_DCT_method_ptr inverse_DCT;
326
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000327 /* Force some input to be done if we are getting ahead of the input. */
328 while (cinfo->input_scan_number < cinfo->output_scan_number ||
329 (cinfo->input_scan_number == cinfo->output_scan_number &&
330 cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
331 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
332 return JPEG_SUSPENDED;
333 }
334
335 /* OK, output from the virtual arrays. */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000336 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
337 ci++, compptr++) {
338 /* Don't bother to IDCT an uninteresting component. */
339 if (! compptr->component_needed)
340 continue;
341 /* Align the virtual buffer for this component. */
342 buffer = (*cinfo->mem->access_virt_barray)
343 ((j_common_ptr) cinfo, coef->whole_image[ci],
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000344 cinfo->output_iMCU_row * compptr->v_samp_factor,
345 (JDIMENSION) compptr->v_samp_factor, FALSE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000346 /* Count non-dummy DCT block rows in this iMCU row. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000347 if (cinfo->output_iMCU_row < last_iMCU_row)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000348 block_rows = compptr->v_samp_factor;
349 else {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000350 /* NB: can't use last_row_height here; it is input-side-dependent! */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000351 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
352 if (block_rows == 0) block_rows = compptr->v_samp_factor;
353 }
354 inverse_DCT = cinfo->idct->inverse_DCT[ci];
355 output_ptr = output_buf[ci];
356 /* Loop over all DCT blocks to be processed. */
357 for (block_row = 0; block_row < block_rows; block_row++) {
358 buffer_ptr = buffer[block_row];
359 output_col = 0;
360 for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
361 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
362 output_ptr, output_col);
363 buffer_ptr++;
364 output_col += compptr->DCT_scaled_size;
365 }
366 output_ptr += compptr->DCT_scaled_size;
367 }
368 }
369
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000370 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
371 return JPEG_ROW_COMPLETED;
372 return JPEG_SCAN_COMPLETED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000373}
374
375#endif /* D_MULTISCAN_FILES_SUPPORTED */
376
377
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000378#ifdef BLOCK_SMOOTHING_SUPPORTED
379
380/*
381 * This code applies interblock smoothing as described by section K.8
382 * of the JPEG standard: the first 5 AC coefficients are estimated from
383 * the DC values of a DCT block and its 8 neighboring blocks.
384 * We apply smoothing only for progressive JPEG decoding, and only if
385 * the coefficients it can estimate are not yet known to full precision.
386 */
387
Thomas G. Lane489583f1996-02-07 00:00:00 +0000388/* Natural-order array positions of the first 5 zigzag-order coefficients */
389#define Q01_POS 1
390#define Q10_POS 8
391#define Q20_POS 16
392#define Q11_POS 9
393#define Q02_POS 2
394
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000395/*
396 * Determine whether block smoothing is applicable and safe.
397 * We also latch the current states of the coef_bits[] entries for the
398 * AC coefficients; otherwise, if the input side of the decompressor
399 * advances into a new scan, we might think the coefficients are known
400 * more accurately than they really are.
401 */
402
Thomas G. Lane489583f1996-02-07 00:00:00 +0000403LOCAL(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000404smoothing_ok (j_decompress_ptr cinfo)
405{
406 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
407 boolean smoothing_useful = FALSE;
408 int ci, coefi;
409 jpeg_component_info *compptr;
410 JQUANT_TBL * qtable;
411 int * coef_bits;
412 int * coef_bits_latch;
413
414 if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
415 return FALSE;
416
417 /* Allocate latch area if not already done */
418 if (coef->coef_bits_latch == NULL)
419 coef->coef_bits_latch = (int *)
420 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
421 cinfo->num_components *
422 (SAVED_COEFS * SIZEOF(int)));
423 coef_bits_latch = coef->coef_bits_latch;
424
425 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
426 ci++, compptr++) {
427 /* All components' quantization values must already be latched. */
428 if ((qtable = compptr->quant_table) == NULL)
429 return FALSE;
430 /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
Thomas G. Lane489583f1996-02-07 00:00:00 +0000431 if (qtable->quantval[0] == 0 ||
432 qtable->quantval[Q01_POS] == 0 ||
433 qtable->quantval[Q10_POS] == 0 ||
434 qtable->quantval[Q20_POS] == 0 ||
435 qtable->quantval[Q11_POS] == 0 ||
436 qtable->quantval[Q02_POS] == 0)
437 return FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000438 /* DC values must be at least partly known for all components. */
439 coef_bits = cinfo->coef_bits[ci];
440 if (coef_bits[0] < 0)
441 return FALSE;
442 /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
443 for (coefi = 1; coefi <= 5; coefi++) {
444 coef_bits_latch[coefi] = coef_bits[coefi];
445 if (coef_bits[coefi] != 0)
446 smoothing_useful = TRUE;
447 }
448 coef_bits_latch += SAVED_COEFS;
449 }
450
451 return smoothing_useful;
452}
453
454
455/*
456 * Variant of decompress_data for use when doing block smoothing.
457 */
458
Thomas G. Lane489583f1996-02-07 00:00:00 +0000459METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000460decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
461{
462 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
463 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
464 JDIMENSION block_num, last_block_column;
465 int ci, block_row, block_rows, access_rows;
466 JBLOCKARRAY buffer;
467 JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
468 JSAMPARRAY output_ptr;
469 JDIMENSION output_col;
470 jpeg_component_info *compptr;
471 inverse_DCT_method_ptr inverse_DCT;
472 boolean first_row, last_row;
473 JBLOCK workspace;
474 int *coef_bits;
475 JQUANT_TBL *quanttbl;
476 INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
477 int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
478 int Al, pred;
479
480 /* Force some input to be done if we are getting ahead of the input. */
481 while (cinfo->input_scan_number <= cinfo->output_scan_number &&
482 ! cinfo->inputctl->eoi_reached) {
483 if (cinfo->input_scan_number == cinfo->output_scan_number) {
484 /* If input is working on current scan, we ordinarily want it to
485 * have completed the current row. But if input scan is DC,
486 * we want it to keep one row ahead so that next block row's DC
487 * values are up to date.
488 */
489 JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
490 if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
491 break;
492 }
493 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
494 return JPEG_SUSPENDED;
495 }
496
497 /* OK, output from the virtual arrays. */
498 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
499 ci++, compptr++) {
500 /* Don't bother to IDCT an uninteresting component. */
501 if (! compptr->component_needed)
502 continue;
503 /* Count non-dummy DCT block rows in this iMCU row. */
504 if (cinfo->output_iMCU_row < last_iMCU_row) {
505 block_rows = compptr->v_samp_factor;
506 access_rows = block_rows * 2; /* this and next iMCU row */
507 last_row = FALSE;
508 } else {
509 /* NB: can't use last_row_height here; it is input-side-dependent! */
510 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
511 if (block_rows == 0) block_rows = compptr->v_samp_factor;
512 access_rows = block_rows; /* this iMCU row only */
513 last_row = TRUE;
514 }
515 /* Align the virtual buffer for this component. */
516 if (cinfo->output_iMCU_row > 0) {
517 access_rows += compptr->v_samp_factor; /* prior iMCU row too */
518 buffer = (*cinfo->mem->access_virt_barray)
519 ((j_common_ptr) cinfo, coef->whole_image[ci],
520 (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
521 (JDIMENSION) access_rows, FALSE);
522 buffer += compptr->v_samp_factor; /* point to current iMCU row */
523 first_row = FALSE;
524 } else {
525 buffer = (*cinfo->mem->access_virt_barray)
526 ((j_common_ptr) cinfo, coef->whole_image[ci],
527 (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
528 first_row = TRUE;
529 }
530 /* Fetch component-dependent info */
531 coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
532 quanttbl = compptr->quant_table;
533 Q00 = quanttbl->quantval[0];
Thomas G. Lane489583f1996-02-07 00:00:00 +0000534 Q01 = quanttbl->quantval[Q01_POS];
535 Q10 = quanttbl->quantval[Q10_POS];
536 Q20 = quanttbl->quantval[Q20_POS];
537 Q11 = quanttbl->quantval[Q11_POS];
538 Q02 = quanttbl->quantval[Q02_POS];
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000539 inverse_DCT = cinfo->idct->inverse_DCT[ci];
540 output_ptr = output_buf[ci];
541 /* Loop over all DCT blocks to be processed. */
542 for (block_row = 0; block_row < block_rows; block_row++) {
543 buffer_ptr = buffer[block_row];
544 if (first_row && block_row == 0)
545 prev_block_row = buffer_ptr;
546 else
547 prev_block_row = buffer[block_row-1];
548 if (last_row && block_row == block_rows-1)
549 next_block_row = buffer_ptr;
550 else
551 next_block_row = buffer[block_row+1];
552 /* We fetch the surrounding DC values using a sliding-register approach.
553 * Initialize all nine here so as to do the right thing on narrow pics.
554 */
555 DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
556 DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
557 DC7 = DC8 = DC9 = (int) next_block_row[0][0];
558 output_col = 0;
559 last_block_column = compptr->width_in_blocks - 1;
560 for (block_num = 0; block_num <= last_block_column; block_num++) {
561 /* Fetch current DCT block into workspace so we can modify it. */
562 jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
563 /* Update DC values */
564 if (block_num < last_block_column) {
565 DC3 = (int) prev_block_row[1][0];
566 DC6 = (int) buffer_ptr[1][0];
567 DC9 = (int) next_block_row[1][0];
568 }
569 /* Compute coefficient estimates per K.8.
570 * An estimate is applied only if coefficient is still zero,
571 * and is not known to be fully accurate.
572 */
573 /* AC01 */
574 if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
575 num = 36 * Q00 * (DC4 - DC6);
576 if (num >= 0) {
577 pred = (int) (((Q01<<7) + num) / (Q01<<8));
578 if (Al > 0 && pred >= (1<<Al))
579 pred = (1<<Al)-1;
580 } else {
581 pred = (int) (((Q01<<7) - num) / (Q01<<8));
582 if (Al > 0 && pred >= (1<<Al))
583 pred = (1<<Al)-1;
584 pred = -pred;
585 }
586 workspace[1] = (JCOEF) pred;
587 }
588 /* AC10 */
589 if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
590 num = 36 * Q00 * (DC2 - DC8);
591 if (num >= 0) {
592 pred = (int) (((Q10<<7) + num) / (Q10<<8));
593 if (Al > 0 && pred >= (1<<Al))
594 pred = (1<<Al)-1;
595 } else {
596 pred = (int) (((Q10<<7) - num) / (Q10<<8));
597 if (Al > 0 && pred >= (1<<Al))
598 pred = (1<<Al)-1;
599 pred = -pred;
600 }
601 workspace[8] = (JCOEF) pred;
602 }
603 /* AC20 */
604 if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
605 num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
606 if (num >= 0) {
607 pred = (int) (((Q20<<7) + num) / (Q20<<8));
608 if (Al > 0 && pred >= (1<<Al))
609 pred = (1<<Al)-1;
610 } else {
611 pred = (int) (((Q20<<7) - num) / (Q20<<8));
612 if (Al > 0 && pred >= (1<<Al))
613 pred = (1<<Al)-1;
614 pred = -pred;
615 }
616 workspace[16] = (JCOEF) pred;
617 }
618 /* AC11 */
619 if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
620 num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
621 if (num >= 0) {
622 pred = (int) (((Q11<<7) + num) / (Q11<<8));
623 if (Al > 0 && pred >= (1<<Al))
624 pred = (1<<Al)-1;
625 } else {
626 pred = (int) (((Q11<<7) - num) / (Q11<<8));
627 if (Al > 0 && pred >= (1<<Al))
628 pred = (1<<Al)-1;
629 pred = -pred;
630 }
631 workspace[9] = (JCOEF) pred;
632 }
633 /* AC02 */
634 if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
635 num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
636 if (num >= 0) {
637 pred = (int) (((Q02<<7) + num) / (Q02<<8));
638 if (Al > 0 && pred >= (1<<Al))
639 pred = (1<<Al)-1;
640 } else {
641 pred = (int) (((Q02<<7) - num) / (Q02<<8));
642 if (Al > 0 && pred >= (1<<Al))
643 pred = (1<<Al)-1;
644 pred = -pred;
645 }
646 workspace[2] = (JCOEF) pred;
647 }
648 /* OK, do the IDCT */
649 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
650 output_ptr, output_col);
651 /* Advance for next column */
652 DC1 = DC2; DC2 = DC3;
653 DC4 = DC5; DC5 = DC6;
654 DC7 = DC8; DC8 = DC9;
655 buffer_ptr++, prev_block_row++, next_block_row++;
656 output_col += compptr->DCT_scaled_size;
657 }
658 output_ptr += compptr->DCT_scaled_size;
659 }
660 }
661
662 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
663 return JPEG_ROW_COMPLETED;
664 return JPEG_SCAN_COMPLETED;
665}
666
667#endif /* BLOCK_SMOOTHING_SUPPORTED */
668
669
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000670/*
671 * Initialize coefficient buffer controller.
672 */
673
Thomas G. Lane489583f1996-02-07 00:00:00 +0000674GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000675jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
676{
677 my_coef_ptr coef;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000678
679 coef = (my_coef_ptr)
680 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
681 SIZEOF(my_coef_controller));
682 cinfo->coef = (struct jpeg_d_coef_controller *) coef;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000683 coef->pub.start_input_pass = start_input_pass;
684 coef->pub.start_output_pass = start_output_pass;
685#ifdef BLOCK_SMOOTHING_SUPPORTED
686 coef->coef_bits_latch = NULL;
687#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000688
689 /* Create the coefficient buffer. */
690 if (need_full_buffer) {
691#ifdef D_MULTISCAN_FILES_SUPPORTED
692 /* Allocate a full-image virtual array for each component, */
693 /* padded to a multiple of samp_factor DCT blocks in each direction. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000694 /* Note we ask for a pre-zeroed array. */
695 int ci, access_rows;
696 jpeg_component_info *compptr;
697
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000698 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
699 ci++, compptr++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000700 access_rows = compptr->v_samp_factor;
701#ifdef BLOCK_SMOOTHING_SUPPORTED
702 /* If block smoothing could be used, need a bigger window */
703 if (cinfo->progressive_mode)
704 access_rows *= 3;
705#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000706 coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000707 ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000708 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
709 (long) compptr->h_samp_factor),
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000710 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
711 (long) compptr->v_samp_factor),
712 (JDIMENSION) access_rows);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000713 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000714 coef->pub.consume_data = consume_data;
715 coef->pub.decompress_data = decompress_data;
716 coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000717#else
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000718 ERREXIT(cinfo, JERR_NOT_COMPILED);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000719#endif
720 } else {
721 /* We only need a single-MCU buffer. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000722 JBLOCKROW buffer;
723 int i;
724
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000725 buffer = (JBLOCKROW)
726 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000727 D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
728 for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000729 coef->MCU_buffer[i] = buffer + i;
730 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000731 coef->pub.consume_data = dummy_consume_data;
732 coef->pub.decompress_data = decompress_onepass;
733 coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000734 }
735}