blob: d38db6c337ecdb7be040a2cfd47db7bbecb2ccd5 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jdcoefct.c
3 *
DRCa73e8702012-12-31 02:52:30 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00005 * Copyright (C) 1994-1997, Thomas G. Lane.
DRCa6ef2822013-09-28 03:23:49 +00006 * libjpeg-turbo Modifications:
DRC36a6eec2010-10-08 08:05:44 +00007 * Copyright (C) 2010, D. R. Commander.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00008 * For conditions of distribution and use, see the accompanying README file.
9 *
10 * This file contains the coefficient buffer controller for decompression.
11 * This controller is the top level of the JPEG decompressor proper.
12 * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000013 *
14 * In buffered-image mode, this controller is the interface between
15 * input-oriented processing and output-oriented processing.
16 * Also, the input side (only) is used when reading a file for transcoding.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000017 */
18
19#define JPEG_INTERNALS
20#include "jinclude.h"
21#include "jpeglib.h"
DRC36a6eec2010-10-08 08:05:44 +000022#include "jpegcomp.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000023
Thomas G. Lanebc79e061995-08-02 00:00:00 +000024/* Block smoothing is only applicable for progressive JPEG, so: */
25#ifndef D_PROGRESSIVE_SUPPORTED
26#undef BLOCK_SMOOTHING_SUPPORTED
27#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000028
29/* Private buffer controller object */
30
31typedef struct {
32 struct jpeg_d_coef_controller pub; /* public fields */
33
Thomas G. Lanebc79e061995-08-02 00:00:00 +000034 /* These variables keep track of the current location of the input side. */
35 /* cinfo->input_iMCU_row is also used for this. */
36 JDIMENSION MCU_ctr; /* counts MCUs processed in current row */
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000037 int MCU_vert_offset; /* counts MCU rows within iMCU row */
38 int MCU_rows_per_iMCU_row; /* number of such rows needed */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000039
Thomas G. Lanebc79e061995-08-02 00:00:00 +000040 /* The output side's location is represented by cinfo->output_iMCU_row. */
41
42 /* In single-pass modes, it's sufficient to buffer just one MCU.
43 * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000044 * and let the entropy decoder write into that workspace each time.
45 * (On 80x86, the workspace is FAR even though it's not really very big;
46 * this is to keep the module interfaces unchanged when a large coefficient
47 * buffer is necessary.)
48 * In multi-pass modes, this array points to the current MCU's blocks
Thomas G. Lanebc79e061995-08-02 00:00:00 +000049 * within the virtual arrays; it is used only by the input side.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000050 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000051 JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000052
Pierre Ossman35c47192009-03-09 13:29:37 +000053 /* Temporary workspace for one MCU */
54 JCOEF * workspace;
55
Thomas G. Lanebc79e061995-08-02 00:00:00 +000056#ifdef D_MULTISCAN_FILES_SUPPORTED
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000057 /* In multi-pass modes, we need a virtual block array for each component. */
58 jvirt_barray_ptr whole_image[MAX_COMPONENTS];
Thomas G. Lanebc79e061995-08-02 00:00:00 +000059#endif
60
61#ifdef BLOCK_SMOOTHING_SUPPORTED
62 /* When doing block smoothing, we latch coefficient Al values here */
63 int * coef_bits_latch;
64#define SAVED_COEFS 6 /* we save coef_bits[0..5] */
65#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000066} my_coef_controller;
67
68typedef my_coef_controller * my_coef_ptr;
69
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000070/* Forward declarations */
Thomas G. Lane489583f1996-02-07 00:00:00 +000071METHODDEF(int) decompress_onepass
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000072 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
73#ifdef D_MULTISCAN_FILES_SUPPORTED
Thomas G. Lane489583f1996-02-07 00:00:00 +000074METHODDEF(int) decompress_data
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000075 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
Thomas G. Lanebc79e061995-08-02 00:00:00 +000076#endif
77#ifdef BLOCK_SMOOTHING_SUPPORTED
Thomas G. Lane489583f1996-02-07 00:00:00 +000078LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
79METHODDEF(int) decompress_smooth_data
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000080 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
81#endif
82
83
Thomas G. Lane489583f1996-02-07 00:00:00 +000084LOCAL(void)
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000085start_iMCU_row (j_decompress_ptr cinfo)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000086/* Reset within-iMCU-row counters for a new row (input side) */
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000087{
88 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
89
90 /* In an interleaved scan, an MCU row is the same as an iMCU row.
91 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
92 * But at the bottom of the image, process only what's left.
93 */
94 if (cinfo->comps_in_scan > 1) {
95 coef->MCU_rows_per_iMCU_row = 1;
96 } else {
Thomas G. Lanebc79e061995-08-02 00:00:00 +000097 if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000098 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
99 else
100 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
101 }
102
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000103 coef->MCU_ctr = 0;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000104 coef->MCU_vert_offset = 0;
105}
106
107
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000108/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000109 * Initialize for an input processing pass.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000110 */
111
Thomas G. Lane489583f1996-02-07 00:00:00 +0000112METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000113start_input_pass (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000114{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000115 cinfo->input_iMCU_row = 0;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000116 start_iMCU_row(cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000117}
118
119
120/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000121 * Initialize for an output processing pass.
122 */
123
Thomas G. Lane489583f1996-02-07 00:00:00 +0000124METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000125start_output_pass (j_decompress_ptr cinfo)
126{
127#ifdef BLOCK_SMOOTHING_SUPPORTED
128 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
129
130 /* If multipass, check to see whether to use block smoothing on this pass */
131 if (coef->pub.coef_arrays != NULL) {
132 if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
133 coef->pub.decompress_data = decompress_smooth_data;
134 else
135 coef->pub.decompress_data = decompress_data;
136 }
137#endif
138 cinfo->output_iMCU_row = 0;
139}
140
141
142/*
143 * Decompress and return some data in the single-pass case.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000144 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000145 * 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.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000147 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000148 * NB: output_buf contains a plane for each component in image,
149 * which we index according to the component's SOF position.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000150 */
151
Thomas G. Lane489583f1996-02-07 00:00:00 +0000152METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000153decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000154{
155 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
156 JDIMENSION MCU_col_num; /* index of current MCU within row */
157 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000158 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
159 int blkn, ci, xindex, yindex, yoffset, useful_width;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000160 JSAMPARRAY output_ptr;
161 JDIMENSION start_col, output_col;
162 jpeg_component_info *compptr;
163 inverse_DCT_method_ptr inverse_DCT;
164
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000165 /* Loop to process as much as one whole iMCU row */
166 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
167 yoffset++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000168 for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000169 MCU_col_num++) {
170 /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
171 jzero_far((void FAR *) coef->MCU_buffer[0],
172 (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
173 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
174 /* Suspension forced; update state counters and exit */
175 coef->MCU_vert_offset = yoffset;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000176 coef->MCU_ctr = MCU_col_num;
177 return JPEG_SUSPENDED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000178 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000179 /* Determine where data should go in output_buf and do the IDCT thing.
180 * We skip dummy blocks at the right and bottom edges (but blkn gets
181 * incremented past them!). Note the inner loop relies on having
182 * allocated the MCU_buffer[] blocks sequentially.
183 */
184 blkn = 0; /* index of current DCT block within MCU */
185 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
186 compptr = cinfo->cur_comp_info[ci];
187 /* Don't bother to IDCT an uninteresting component. */
188 if (! compptr->component_needed) {
189 blkn += compptr->MCU_blocks;
190 continue;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000191 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000192 inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
193 useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
194 : compptr->last_col_width;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000195 output_ptr = output_buf[compptr->component_index] +
DRC49967cd2010-10-09 19:57:51 +0000196 yoffset * compptr->_DCT_scaled_size;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000197 start_col = MCU_col_num * compptr->MCU_sample_width;
198 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000199 if (cinfo->input_iMCU_row < last_iMCU_row ||
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000200 yoffset+yindex < compptr->last_row_height) {
201 output_col = start_col;
202 for (xindex = 0; xindex < useful_width; xindex++) {
203 (*inverse_DCT) (cinfo, compptr,
204 (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
205 output_ptr, output_col);
DRC49967cd2010-10-09 19:57:51 +0000206 output_col += compptr->_DCT_scaled_size;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000207 }
208 }
209 blkn += compptr->MCU_width;
DRC49967cd2010-10-09 19:57:51 +0000210 output_ptr += compptr->_DCT_scaled_size;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000211 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000212 }
213 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000214 /* Completed an MCU row, but perhaps not an iMCU row */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000215 coef->MCU_ctr = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000216 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000217 /* Completed the iMCU row, advance counters for next one */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000218 cinfo->output_iMCU_row++;
219 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
220 start_iMCU_row(cinfo);
221 return JPEG_ROW_COMPLETED;
222 }
223 /* Completed the scan */
224 (*cinfo->inputctl->finish_input_pass) (cinfo);
225 return JPEG_SCAN_COMPLETED;
226}
227
228
229/*
230 * Dummy consume-input routine for single-pass operation.
231 */
232
Thomas G. Lane489583f1996-02-07 00:00:00 +0000233METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000234dummy_consume_data (j_decompress_ptr cinfo)
235{
236 return JPEG_SUSPENDED; /* Always indicate nothing was done */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000237}
238
239
240#ifdef D_MULTISCAN_FILES_SUPPORTED
241
242/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000243 * Consume input data and store it in the full-image coefficient buffer.
244 * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
245 * ie, v_samp_factor block rows for each component in the scan.
246 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000247 */
248
Thomas G. Lane489583f1996-02-07 00:00:00 +0000249METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000250consume_data (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000251{
252 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
253 JDIMENSION MCU_col_num; /* index of current MCU within row */
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000254 int blkn, ci, xindex, yindex, yoffset;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000255 JDIMENSION start_col;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000256 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
257 JBLOCKROW buffer_ptr;
258 jpeg_component_info *compptr;
259
260 /* Align the virtual buffers for the components used in this scan. */
261 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
262 compptr = cinfo->cur_comp_info[ci];
263 buffer[ci] = (*cinfo->mem->access_virt_barray)
264 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000265 cinfo->input_iMCU_row * compptr->v_samp_factor,
266 (JDIMENSION) compptr->v_samp_factor, TRUE);
267 /* Note: entropy decoder expects buffer to be zeroed,
268 * but this is handled automatically by the memory manager
269 * because we requested a pre-zeroed array.
270 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000271 }
272
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000273 /* Loop to process one whole iMCU row */
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000274 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
275 yoffset++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000276 for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000277 MCU_col_num++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000278 /* Construct list of pointers to DCT blocks belonging to this MCU */
279 blkn = 0; /* index of current DCT block within MCU */
280 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
281 compptr = cinfo->cur_comp_info[ci];
282 start_col = MCU_col_num * compptr->MCU_width;
283 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
284 buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
285 for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
286 coef->MCU_buffer[blkn++] = buffer_ptr++;
287 }
288 }
289 }
290 /* Try to fetch the MCU. */
291 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000292 /* Suspension forced; update state counters and exit */
293 coef->MCU_vert_offset = yoffset;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000294 coef->MCU_ctr = MCU_col_num;
295 return JPEG_SUSPENDED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000296 }
297 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000298 /* Completed an MCU row, but perhaps not an iMCU row */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000299 coef->MCU_ctr = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000300 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000301 /* Completed the iMCU row, advance counters for next one */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000302 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
303 start_iMCU_row(cinfo);
304 return JPEG_ROW_COMPLETED;
305 }
306 /* Completed the scan */
307 (*cinfo->inputctl->finish_input_pass) (cinfo);
308 return JPEG_SCAN_COMPLETED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000309}
310
311
312/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000313 * Decompress and return some data in the multi-pass case.
314 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
315 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000316 *
317 * NB: output_buf contains a plane for each component in image.
318 */
319
Thomas G. Lane489583f1996-02-07 00:00:00 +0000320METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000321decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000322{
323 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000324 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000325 JDIMENSION block_num;
326 int ci, block_row, block_rows;
327 JBLOCKARRAY buffer;
328 JBLOCKROW buffer_ptr;
329 JSAMPARRAY output_ptr;
330 JDIMENSION output_col;
331 jpeg_component_info *compptr;
332 inverse_DCT_method_ptr inverse_DCT;
333
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000334 /* Force some input to be done if we are getting ahead of the input. */
335 while (cinfo->input_scan_number < cinfo->output_scan_number ||
336 (cinfo->input_scan_number == cinfo->output_scan_number &&
337 cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
338 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
339 return JPEG_SUSPENDED;
340 }
341
342 /* OK, output from the virtual arrays. */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000343 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
344 ci++, compptr++) {
345 /* Don't bother to IDCT an uninteresting component. */
346 if (! compptr->component_needed)
347 continue;
348 /* Align the virtual buffer for this component. */
349 buffer = (*cinfo->mem->access_virt_barray)
350 ((j_common_ptr) cinfo, coef->whole_image[ci],
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000351 cinfo->output_iMCU_row * compptr->v_samp_factor,
352 (JDIMENSION) compptr->v_samp_factor, FALSE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000353 /* Count non-dummy DCT block rows in this iMCU row. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000354 if (cinfo->output_iMCU_row < last_iMCU_row)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000355 block_rows = compptr->v_samp_factor;
356 else {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000357 /* NB: can't use last_row_height here; it is input-side-dependent! */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000358 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
359 if (block_rows == 0) block_rows = compptr->v_samp_factor;
360 }
361 inverse_DCT = cinfo->idct->inverse_DCT[ci];
362 output_ptr = output_buf[ci];
363 /* Loop over all DCT blocks to be processed. */
364 for (block_row = 0; block_row < block_rows; block_row++) {
365 buffer_ptr = buffer[block_row];
366 output_col = 0;
367 for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
368 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
369 output_ptr, output_col);
370 buffer_ptr++;
DRC49967cd2010-10-09 19:57:51 +0000371 output_col += compptr->_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000372 }
DRC49967cd2010-10-09 19:57:51 +0000373 output_ptr += compptr->_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000374 }
375 }
376
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000377 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
378 return JPEG_ROW_COMPLETED;
379 return JPEG_SCAN_COMPLETED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000380}
381
382#endif /* D_MULTISCAN_FILES_SUPPORTED */
383
384
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000385#ifdef BLOCK_SMOOTHING_SUPPORTED
386
387/*
388 * This code applies interblock smoothing as described by section K.8
389 * of the JPEG standard: the first 5 AC coefficients are estimated from
390 * the DC values of a DCT block and its 8 neighboring blocks.
391 * We apply smoothing only for progressive JPEG decoding, and only if
392 * the coefficients it can estimate are not yet known to full precision.
393 */
394
Thomas G. Lane489583f1996-02-07 00:00:00 +0000395/* Natural-order array positions of the first 5 zigzag-order coefficients */
396#define Q01_POS 1
397#define Q10_POS 8
398#define Q20_POS 16
399#define Q11_POS 9
400#define Q02_POS 2
401
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000402/*
403 * Determine whether block smoothing is applicable and safe.
404 * We also latch the current states of the coef_bits[] entries for the
405 * AC coefficients; otherwise, if the input side of the decompressor
406 * advances into a new scan, we might think the coefficients are known
407 * more accurately than they really are.
408 */
409
Thomas G. Lane489583f1996-02-07 00:00:00 +0000410LOCAL(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000411smoothing_ok (j_decompress_ptr cinfo)
412{
413 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
414 boolean smoothing_useful = FALSE;
415 int ci, coefi;
416 jpeg_component_info *compptr;
417 JQUANT_TBL * qtable;
418 int * coef_bits;
419 int * coef_bits_latch;
420
421 if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
422 return FALSE;
423
424 /* Allocate latch area if not already done */
425 if (coef->coef_bits_latch == NULL)
426 coef->coef_bits_latch = (int *)
427 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
428 cinfo->num_components *
429 (SAVED_COEFS * SIZEOF(int)));
430 coef_bits_latch = coef->coef_bits_latch;
431
432 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
433 ci++, compptr++) {
434 /* All components' quantization values must already be latched. */
435 if ((qtable = compptr->quant_table) == NULL)
436 return FALSE;
437 /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
Thomas G. Lane489583f1996-02-07 00:00:00 +0000438 if (qtable->quantval[0] == 0 ||
439 qtable->quantval[Q01_POS] == 0 ||
440 qtable->quantval[Q10_POS] == 0 ||
441 qtable->quantval[Q20_POS] == 0 ||
442 qtable->quantval[Q11_POS] == 0 ||
443 qtable->quantval[Q02_POS] == 0)
444 return FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000445 /* DC values must be at least partly known for all components. */
446 coef_bits = cinfo->coef_bits[ci];
447 if (coef_bits[0] < 0)
448 return FALSE;
449 /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
450 for (coefi = 1; coefi <= 5; coefi++) {
451 coef_bits_latch[coefi] = coef_bits[coefi];
452 if (coef_bits[coefi] != 0)
453 smoothing_useful = TRUE;
454 }
455 coef_bits_latch += SAVED_COEFS;
456 }
457
458 return smoothing_useful;
459}
460
461
462/*
463 * Variant of decompress_data for use when doing block smoothing.
464 */
465
Thomas G. Lane489583f1996-02-07 00:00:00 +0000466METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000467decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
468{
469 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
470 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
471 JDIMENSION block_num, last_block_column;
472 int ci, block_row, block_rows, access_rows;
473 JBLOCKARRAY buffer;
474 JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
475 JSAMPARRAY output_ptr;
476 JDIMENSION output_col;
477 jpeg_component_info *compptr;
478 inverse_DCT_method_ptr inverse_DCT;
479 boolean first_row, last_row;
Pierre Ossman35c47192009-03-09 13:29:37 +0000480 JCOEF * workspace;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000481 int *coef_bits;
482 JQUANT_TBL *quanttbl;
483 INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
484 int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
485 int Al, pred;
486
Pierre Ossman35c47192009-03-09 13:29:37 +0000487 /* Keep a local variable to avoid looking it up more than once */
488 workspace = coef->workspace;
489
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000490 /* Force some input to be done if we are getting ahead of the input. */
491 while (cinfo->input_scan_number <= cinfo->output_scan_number &&
492 ! cinfo->inputctl->eoi_reached) {
493 if (cinfo->input_scan_number == cinfo->output_scan_number) {
494 /* If input is working on current scan, we ordinarily want it to
495 * have completed the current row. But if input scan is DC,
496 * we want it to keep one row ahead so that next block row's DC
497 * values are up to date.
498 */
499 JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
500 if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
501 break;
502 }
503 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
504 return JPEG_SUSPENDED;
505 }
506
507 /* OK, output from the virtual arrays. */
508 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
509 ci++, compptr++) {
510 /* Don't bother to IDCT an uninteresting component. */
511 if (! compptr->component_needed)
512 continue;
513 /* Count non-dummy DCT block rows in this iMCU row. */
514 if (cinfo->output_iMCU_row < last_iMCU_row) {
515 block_rows = compptr->v_samp_factor;
516 access_rows = block_rows * 2; /* this and next iMCU row */
517 last_row = FALSE;
518 } else {
519 /* NB: can't use last_row_height here; it is input-side-dependent! */
520 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
521 if (block_rows == 0) block_rows = compptr->v_samp_factor;
522 access_rows = block_rows; /* this iMCU row only */
523 last_row = TRUE;
524 }
525 /* Align the virtual buffer for this component. */
526 if (cinfo->output_iMCU_row > 0) {
527 access_rows += compptr->v_samp_factor; /* prior iMCU row too */
528 buffer = (*cinfo->mem->access_virt_barray)
529 ((j_common_ptr) cinfo, coef->whole_image[ci],
530 (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
531 (JDIMENSION) access_rows, FALSE);
532 buffer += compptr->v_samp_factor; /* point to current iMCU row */
533 first_row = FALSE;
534 } else {
535 buffer = (*cinfo->mem->access_virt_barray)
536 ((j_common_ptr) cinfo, coef->whole_image[ci],
537 (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
538 first_row = TRUE;
539 }
540 /* Fetch component-dependent info */
541 coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
542 quanttbl = compptr->quant_table;
543 Q00 = quanttbl->quantval[0];
Thomas G. Lane489583f1996-02-07 00:00:00 +0000544 Q01 = quanttbl->quantval[Q01_POS];
545 Q10 = quanttbl->quantval[Q10_POS];
546 Q20 = quanttbl->quantval[Q20_POS];
547 Q11 = quanttbl->quantval[Q11_POS];
548 Q02 = quanttbl->quantval[Q02_POS];
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000549 inverse_DCT = cinfo->idct->inverse_DCT[ci];
550 output_ptr = output_buf[ci];
551 /* Loop over all DCT blocks to be processed. */
552 for (block_row = 0; block_row < block_rows; block_row++) {
553 buffer_ptr = buffer[block_row];
554 if (first_row && block_row == 0)
555 prev_block_row = buffer_ptr;
556 else
557 prev_block_row = buffer[block_row-1];
558 if (last_row && block_row == block_rows-1)
559 next_block_row = buffer_ptr;
560 else
561 next_block_row = buffer[block_row+1];
562 /* We fetch the surrounding DC values using a sliding-register approach.
563 * Initialize all nine here so as to do the right thing on narrow pics.
564 */
565 DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
566 DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
567 DC7 = DC8 = DC9 = (int) next_block_row[0][0];
568 output_col = 0;
569 last_block_column = compptr->width_in_blocks - 1;
570 for (block_num = 0; block_num <= last_block_column; block_num++) {
571 /* Fetch current DCT block into workspace so we can modify it. */
572 jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
573 /* Update DC values */
574 if (block_num < last_block_column) {
575 DC3 = (int) prev_block_row[1][0];
576 DC6 = (int) buffer_ptr[1][0];
577 DC9 = (int) next_block_row[1][0];
578 }
579 /* Compute coefficient estimates per K.8.
580 * An estimate is applied only if coefficient is still zero,
581 * and is not known to be fully accurate.
582 */
583 /* AC01 */
584 if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
585 num = 36 * Q00 * (DC4 - DC6);
586 if (num >= 0) {
587 pred = (int) (((Q01<<7) + num) / (Q01<<8));
588 if (Al > 0 && pred >= (1<<Al))
589 pred = (1<<Al)-1;
590 } else {
591 pred = (int) (((Q01<<7) - num) / (Q01<<8));
592 if (Al > 0 && pred >= (1<<Al))
593 pred = (1<<Al)-1;
594 pred = -pred;
595 }
596 workspace[1] = (JCOEF) pred;
597 }
598 /* AC10 */
599 if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
600 num = 36 * Q00 * (DC2 - DC8);
601 if (num >= 0) {
602 pred = (int) (((Q10<<7) + num) / (Q10<<8));
603 if (Al > 0 && pred >= (1<<Al))
604 pred = (1<<Al)-1;
605 } else {
606 pred = (int) (((Q10<<7) - num) / (Q10<<8));
607 if (Al > 0 && pred >= (1<<Al))
608 pred = (1<<Al)-1;
609 pred = -pred;
610 }
611 workspace[8] = (JCOEF) pred;
612 }
613 /* AC20 */
614 if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
615 num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
616 if (num >= 0) {
617 pred = (int) (((Q20<<7) + num) / (Q20<<8));
618 if (Al > 0 && pred >= (1<<Al))
619 pred = (1<<Al)-1;
620 } else {
621 pred = (int) (((Q20<<7) - num) / (Q20<<8));
622 if (Al > 0 && pred >= (1<<Al))
623 pred = (1<<Al)-1;
624 pred = -pred;
625 }
626 workspace[16] = (JCOEF) pred;
627 }
628 /* AC11 */
629 if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
630 num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
631 if (num >= 0) {
632 pred = (int) (((Q11<<7) + num) / (Q11<<8));
633 if (Al > 0 && pred >= (1<<Al))
634 pred = (1<<Al)-1;
635 } else {
636 pred = (int) (((Q11<<7) - num) / (Q11<<8));
637 if (Al > 0 && pred >= (1<<Al))
638 pred = (1<<Al)-1;
639 pred = -pred;
640 }
641 workspace[9] = (JCOEF) pred;
642 }
643 /* AC02 */
644 if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
645 num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
646 if (num >= 0) {
647 pred = (int) (((Q02<<7) + num) / (Q02<<8));
648 if (Al > 0 && pred >= (1<<Al))
649 pred = (1<<Al)-1;
650 } else {
651 pred = (int) (((Q02<<7) - num) / (Q02<<8));
652 if (Al > 0 && pred >= (1<<Al))
653 pred = (1<<Al)-1;
654 pred = -pred;
655 }
656 workspace[2] = (JCOEF) pred;
657 }
658 /* OK, do the IDCT */
659 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
660 output_ptr, output_col);
661 /* Advance for next column */
662 DC1 = DC2; DC2 = DC3;
663 DC4 = DC5; DC5 = DC6;
664 DC7 = DC8; DC8 = DC9;
665 buffer_ptr++, prev_block_row++, next_block_row++;
DRC49967cd2010-10-09 19:57:51 +0000666 output_col += compptr->_DCT_scaled_size;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000667 }
DRC49967cd2010-10-09 19:57:51 +0000668 output_ptr += compptr->_DCT_scaled_size;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000669 }
670 }
671
672 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
673 return JPEG_ROW_COMPLETED;
674 return JPEG_SCAN_COMPLETED;
675}
676
677#endif /* BLOCK_SMOOTHING_SUPPORTED */
678
679
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000680/*
681 * Initialize coefficient buffer controller.
682 */
683
Thomas G. Lane489583f1996-02-07 00:00:00 +0000684GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000685jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
686{
687 my_coef_ptr coef;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000688
689 coef = (my_coef_ptr)
690 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
691 SIZEOF(my_coef_controller));
692 cinfo->coef = (struct jpeg_d_coef_controller *) coef;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000693 coef->pub.start_input_pass = start_input_pass;
694 coef->pub.start_output_pass = start_output_pass;
695#ifdef BLOCK_SMOOTHING_SUPPORTED
696 coef->coef_bits_latch = NULL;
697#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000698
699 /* Create the coefficient buffer. */
700 if (need_full_buffer) {
701#ifdef D_MULTISCAN_FILES_SUPPORTED
702 /* Allocate a full-image virtual array for each component, */
703 /* padded to a multiple of samp_factor DCT blocks in each direction. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000704 /* Note we ask for a pre-zeroed array. */
705 int ci, access_rows;
706 jpeg_component_info *compptr;
707
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000708 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
709 ci++, compptr++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000710 access_rows = compptr->v_samp_factor;
711#ifdef BLOCK_SMOOTHING_SUPPORTED
712 /* If block smoothing could be used, need a bigger window */
713 if (cinfo->progressive_mode)
714 access_rows *= 3;
715#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000716 coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000717 ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000718 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
719 (long) compptr->h_samp_factor),
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000720 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
721 (long) compptr->v_samp_factor),
722 (JDIMENSION) access_rows);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000723 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000724 coef->pub.consume_data = consume_data;
725 coef->pub.decompress_data = decompress_data;
726 coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000727#else
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000728 ERREXIT(cinfo, JERR_NOT_COMPILED);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000729#endif
730 } else {
731 /* We only need a single-MCU buffer. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000732 JBLOCKROW buffer;
733 int i;
734
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000735 buffer = (JBLOCKROW)
736 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000737 D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
738 for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000739 coef->MCU_buffer[i] = buffer + i;
740 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000741 coef->pub.consume_data = dummy_consume_data;
742 coef->pub.decompress_data = decompress_onepass;
743 coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000744 }
Pierre Ossman35c47192009-03-09 13:29:37 +0000745
746 /* Allocate the workspace buffer */
747 coef->workspace = (JCOEF *)
748 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
749 SIZEOF(JCOEF) * DCTSIZE2);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000750}