blob: 48a9fc6f833284099100d8d38bde031455b73902 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jdcoefct.c
3 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00004 * Copyright (C) 1994-1997, Thomas G. Lane.
DRC36a6eec2010-10-08 08:05:44 +00005 * Copyright (C) 2010, D. R. Commander.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00006 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
8 *
9 * This file contains the coefficient buffer controller for decompression.
10 * This controller is the top level of the JPEG decompressor proper.
11 * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000012 *
13 * In buffered-image mode, this controller is the interface between
14 * input-oriented processing and output-oriented processing.
15 * Also, the input side (only) is used when reading a file for transcoding.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000016 */
17
18#define JPEG_INTERNALS
19#include "jinclude.h"
20#include "jpeglib.h"
DRC36a6eec2010-10-08 08:05:44 +000021#include "jpegcomp.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000022
Thomas G. Lanebc79e061995-08-02 00:00:00 +000023/* Block smoothing is only applicable for progressive JPEG, so: */
24#ifndef D_PROGRESSIVE_SUPPORTED
25#undef BLOCK_SMOOTHING_SUPPORTED
26#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000027
28/* Private buffer controller object */
29
30typedef struct {
31 struct jpeg_d_coef_controller pub; /* public fields */
32
Thomas G. Lanebc79e061995-08-02 00:00:00 +000033 /* These variables keep track of the current location of the input side. */
34 /* cinfo->input_iMCU_row is also used for this. */
35 JDIMENSION MCU_ctr; /* counts MCUs processed in current row */
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000036 int MCU_vert_offset; /* counts MCU rows within iMCU row */
37 int MCU_rows_per_iMCU_row; /* number of such rows needed */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000038
Thomas G. Lanebc79e061995-08-02 00:00:00 +000039 /* The output side's location is represented by cinfo->output_iMCU_row. */
40
41 /* In single-pass modes, it's sufficient to buffer just one MCU.
42 * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000043 * and let the entropy decoder write into that workspace each time.
44 * (On 80x86, the workspace is FAR even though it's not really very big;
45 * this is to keep the module interfaces unchanged when a large coefficient
46 * buffer is necessary.)
47 * In multi-pass modes, this array points to the current MCU's blocks
Thomas G. Lanebc79e061995-08-02 00:00:00 +000048 * within the virtual arrays; it is used only by the input side.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000049 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000050 JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000051
Pierre Ossman35c47192009-03-09 13:29:37 +000052 /* Temporary workspace for one MCU */
53 JCOEF * workspace;
54
Thomas G. Lanebc79e061995-08-02 00:00:00 +000055#ifdef D_MULTISCAN_FILES_SUPPORTED
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000056 /* In multi-pass modes, we need a virtual block array for each component. */
57 jvirt_barray_ptr whole_image[MAX_COMPONENTS];
Thomas G. Lanebc79e061995-08-02 00:00:00 +000058#endif
59
60#ifdef BLOCK_SMOOTHING_SUPPORTED
61 /* When doing block smoothing, we latch coefficient Al values here */
62 int * coef_bits_latch;
63#define SAVED_COEFS 6 /* we save coef_bits[0..5] */
64#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000065} my_coef_controller;
66
67typedef my_coef_controller * my_coef_ptr;
68
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000069/* Forward declarations */
Thomas G. Lane489583f1996-02-07 00:00:00 +000070METHODDEF(int) decompress_onepass
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000071 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
72#ifdef D_MULTISCAN_FILES_SUPPORTED
Thomas G. Lane489583f1996-02-07 00:00:00 +000073METHODDEF(int) decompress_data
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000074 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
Thomas G. Lanebc79e061995-08-02 00:00:00 +000075#endif
76#ifdef BLOCK_SMOOTHING_SUPPORTED
Thomas G. Lane489583f1996-02-07 00:00:00 +000077LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
78METHODDEF(int) decompress_smooth_data
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000079 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
80#endif
81
82
Thomas G. Lane489583f1996-02-07 00:00:00 +000083LOCAL(void)
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000084start_iMCU_row (j_decompress_ptr cinfo)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000085/* Reset within-iMCU-row counters for a new row (input side) */
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000086{
87 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
88
89 /* In an interleaved scan, an MCU row is the same as an iMCU row.
90 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
91 * But at the bottom of the image, process only what's left.
92 */
93 if (cinfo->comps_in_scan > 1) {
94 coef->MCU_rows_per_iMCU_row = 1;
95 } else {
Thomas G. Lanebc79e061995-08-02 00:00:00 +000096 if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000097 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
98 else
99 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
100 }
101
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000102 coef->MCU_ctr = 0;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000103 coef->MCU_vert_offset = 0;
104}
105
106
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000107/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000108 * Initialize for an input processing pass.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000109 */
110
Thomas G. Lane489583f1996-02-07 00:00:00 +0000111METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000112start_input_pass (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000113{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000114 cinfo->input_iMCU_row = 0;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000115 start_iMCU_row(cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000116}
117
118
119/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000120 * Initialize for an output processing pass.
121 */
122
Thomas G. Lane489583f1996-02-07 00:00:00 +0000123METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000124start_output_pass (j_decompress_ptr cinfo)
125{
126#ifdef BLOCK_SMOOTHING_SUPPORTED
127 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
128
129 /* If multipass, check to see whether to use block smoothing on this pass */
130 if (coef->pub.coef_arrays != NULL) {
131 if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
132 coef->pub.decompress_data = decompress_smooth_data;
133 else
134 coef->pub.decompress_data = decompress_data;
135 }
136#endif
137 cinfo->output_iMCU_row = 0;
138}
139
140
141/*
142 * Decompress and return some data in the single-pass case.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000143 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000144 * Input and output must run in lockstep since we have only a one-MCU buffer.
145 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000146 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000147 * NB: output_buf contains a plane for each component in image,
148 * which we index according to the component's SOF position.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000149 */
150
Thomas G. Lane489583f1996-02-07 00:00:00 +0000151METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000152decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000153{
154 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
155 JDIMENSION MCU_col_num; /* index of current MCU within row */
156 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000157 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
158 int blkn, ci, xindex, yindex, yoffset, useful_width;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000159 JSAMPARRAY output_ptr;
160 JDIMENSION start_col, output_col;
161 jpeg_component_info *compptr;
162 inverse_DCT_method_ptr inverse_DCT;
163
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000164 /* Loop to process as much as one whole iMCU row */
165 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
166 yoffset++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000167 for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000168 MCU_col_num++) {
169 /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
170 jzero_far((void FAR *) coef->MCU_buffer[0],
171 (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
172 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
173 /* Suspension forced; update state counters and exit */
174 coef->MCU_vert_offset = yoffset;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000175 coef->MCU_ctr = MCU_col_num;
176 return JPEG_SUSPENDED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000177 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000178 /* Determine where data should go in output_buf and do the IDCT thing.
179 * We skip dummy blocks at the right and bottom edges (but blkn gets
180 * incremented past them!). Note the inner loop relies on having
181 * allocated the MCU_buffer[] blocks sequentially.
182 */
183 blkn = 0; /* index of current DCT block within MCU */
184 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
185 compptr = cinfo->cur_comp_info[ci];
186 /* Don't bother to IDCT an uninteresting component. */
187 if (! compptr->component_needed) {
188 blkn += compptr->MCU_blocks;
189 continue;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000190 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000191 inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
192 useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
193 : compptr->last_col_width;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000194 output_ptr = output_buf[compptr->component_index] +
DRC49967cd2010-10-09 19:57:51 +0000195 yoffset * compptr->_DCT_scaled_size;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000196 start_col = MCU_col_num * compptr->MCU_sample_width;
197 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000198 if (cinfo->input_iMCU_row < last_iMCU_row ||
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000199 yoffset+yindex < compptr->last_row_height) {
200 output_col = start_col;
201 for (xindex = 0; xindex < useful_width; xindex++) {
202 (*inverse_DCT) (cinfo, compptr,
203 (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
204 output_ptr, output_col);
DRC49967cd2010-10-09 19:57:51 +0000205 output_col += compptr->_DCT_scaled_size;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000206 }
207 }
208 blkn += compptr->MCU_width;
DRC49967cd2010-10-09 19:57:51 +0000209 output_ptr += compptr->_DCT_scaled_size;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000210 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000211 }
212 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000213 /* Completed an MCU row, but perhaps not an iMCU row */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000214 coef->MCU_ctr = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000215 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000216 /* Completed the iMCU row, advance counters for next one */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000217 cinfo->output_iMCU_row++;
218 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
219 start_iMCU_row(cinfo);
220 return JPEG_ROW_COMPLETED;
221 }
222 /* Completed the scan */
223 (*cinfo->inputctl->finish_input_pass) (cinfo);
224 return JPEG_SCAN_COMPLETED;
225}
226
227
228/*
229 * Dummy consume-input routine for single-pass operation.
230 */
231
Thomas G. Lane489583f1996-02-07 00:00:00 +0000232METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000233dummy_consume_data (j_decompress_ptr cinfo)
234{
235 return JPEG_SUSPENDED; /* Always indicate nothing was done */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000236}
237
238
239#ifdef D_MULTISCAN_FILES_SUPPORTED
240
241/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000242 * Consume input data and store it in the full-image coefficient buffer.
243 * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
244 * ie, v_samp_factor block rows for each component in the scan.
245 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000246 */
247
Thomas G. Lane489583f1996-02-07 00:00:00 +0000248METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000249consume_data (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000250{
251 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
252 JDIMENSION MCU_col_num; /* index of current MCU within row */
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000253 int blkn, ci, xindex, yindex, yoffset;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000254 JDIMENSION start_col;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000255 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
256 JBLOCKROW buffer_ptr;
257 jpeg_component_info *compptr;
258
259 /* Align the virtual buffers for the components used in this scan. */
260 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
261 compptr = cinfo->cur_comp_info[ci];
262 buffer[ci] = (*cinfo->mem->access_virt_barray)
263 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000264 cinfo->input_iMCU_row * compptr->v_samp_factor,
265 (JDIMENSION) compptr->v_samp_factor, TRUE);
266 /* Note: entropy decoder expects buffer to be zeroed,
267 * but this is handled automatically by the memory manager
268 * because we requested a pre-zeroed array.
269 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000270 }
271
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000272 /* Loop to process one whole iMCU row */
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000273 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
274 yoffset++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000275 for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000276 MCU_col_num++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000277 /* Construct list of pointers to DCT blocks belonging to this MCU */
278 blkn = 0; /* index of current DCT block within MCU */
279 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
280 compptr = cinfo->cur_comp_info[ci];
281 start_col = MCU_col_num * compptr->MCU_width;
282 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
283 buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
284 for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
285 coef->MCU_buffer[blkn++] = buffer_ptr++;
286 }
287 }
288 }
289 /* Try to fetch the MCU. */
290 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000291 /* Suspension forced; update state counters and exit */
292 coef->MCU_vert_offset = yoffset;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000293 coef->MCU_ctr = MCU_col_num;
294 return JPEG_SUSPENDED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000295 }
296 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000297 /* Completed an MCU row, but perhaps not an iMCU row */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000298 coef->MCU_ctr = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000299 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000300 /* Completed the iMCU row, advance counters for next one */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000301 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
302 start_iMCU_row(cinfo);
303 return JPEG_ROW_COMPLETED;
304 }
305 /* Completed the scan */
306 (*cinfo->inputctl->finish_input_pass) (cinfo);
307 return JPEG_SCAN_COMPLETED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000308}
309
310
311/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000312 * Decompress and return some data in the multi-pass case.
313 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
314 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000315 *
316 * NB: output_buf contains a plane for each component in image.
317 */
318
Thomas G. Lane489583f1996-02-07 00:00:00 +0000319METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000320decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000321{
322 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000323 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000324 JDIMENSION block_num;
325 int ci, block_row, block_rows;
326 JBLOCKARRAY buffer;
327 JBLOCKROW buffer_ptr;
328 JSAMPARRAY output_ptr;
329 JDIMENSION output_col;
330 jpeg_component_info *compptr;
331 inverse_DCT_method_ptr inverse_DCT;
332
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000333 /* Force some input to be done if we are getting ahead of the input. */
334 while (cinfo->input_scan_number < cinfo->output_scan_number ||
335 (cinfo->input_scan_number == cinfo->output_scan_number &&
336 cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
337 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
338 return JPEG_SUSPENDED;
339 }
340
341 /* OK, output from the virtual arrays. */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000342 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
343 ci++, compptr++) {
344 /* Don't bother to IDCT an uninteresting component. */
345 if (! compptr->component_needed)
346 continue;
347 /* Align the virtual buffer for this component. */
348 buffer = (*cinfo->mem->access_virt_barray)
349 ((j_common_ptr) cinfo, coef->whole_image[ci],
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000350 cinfo->output_iMCU_row * compptr->v_samp_factor,
351 (JDIMENSION) compptr->v_samp_factor, FALSE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000352 /* Count non-dummy DCT block rows in this iMCU row. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000353 if (cinfo->output_iMCU_row < last_iMCU_row)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000354 block_rows = compptr->v_samp_factor;
355 else {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000356 /* NB: can't use last_row_height here; it is input-side-dependent! */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000357 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
358 if (block_rows == 0) block_rows = compptr->v_samp_factor;
359 }
360 inverse_DCT = cinfo->idct->inverse_DCT[ci];
361 output_ptr = output_buf[ci];
362 /* Loop over all DCT blocks to be processed. */
363 for (block_row = 0; block_row < block_rows; block_row++) {
364 buffer_ptr = buffer[block_row];
365 output_col = 0;
366 for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
367 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
368 output_ptr, output_col);
369 buffer_ptr++;
DRC49967cd2010-10-09 19:57:51 +0000370 output_col += compptr->_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000371 }
DRC49967cd2010-10-09 19:57:51 +0000372 output_ptr += compptr->_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000373 }
374 }
375
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000376 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
377 return JPEG_ROW_COMPLETED;
378 return JPEG_SCAN_COMPLETED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000379}
380
381#endif /* D_MULTISCAN_FILES_SUPPORTED */
382
383
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000384#ifdef BLOCK_SMOOTHING_SUPPORTED
385
386/*
387 * This code applies interblock smoothing as described by section K.8
388 * of the JPEG standard: the first 5 AC coefficients are estimated from
389 * the DC values of a DCT block and its 8 neighboring blocks.
390 * We apply smoothing only for progressive JPEG decoding, and only if
391 * the coefficients it can estimate are not yet known to full precision.
392 */
393
Thomas G. Lane489583f1996-02-07 00:00:00 +0000394/* Natural-order array positions of the first 5 zigzag-order coefficients */
395#define Q01_POS 1
396#define Q10_POS 8
397#define Q20_POS 16
398#define Q11_POS 9
399#define Q02_POS 2
400
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000401/*
402 * Determine whether block smoothing is applicable and safe.
403 * We also latch the current states of the coef_bits[] entries for the
404 * AC coefficients; otherwise, if the input side of the decompressor
405 * advances into a new scan, we might think the coefficients are known
406 * more accurately than they really are.
407 */
408
Thomas G. Lane489583f1996-02-07 00:00:00 +0000409LOCAL(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000410smoothing_ok (j_decompress_ptr cinfo)
411{
412 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
413 boolean smoothing_useful = FALSE;
414 int ci, coefi;
415 jpeg_component_info *compptr;
416 JQUANT_TBL * qtable;
417 int * coef_bits;
418 int * coef_bits_latch;
419
420 if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
421 return FALSE;
422
423 /* Allocate latch area if not already done */
424 if (coef->coef_bits_latch == NULL)
425 coef->coef_bits_latch = (int *)
426 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
427 cinfo->num_components *
428 (SAVED_COEFS * SIZEOF(int)));
429 coef_bits_latch = coef->coef_bits_latch;
430
431 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
432 ci++, compptr++) {
433 /* All components' quantization values must already be latched. */
434 if ((qtable = compptr->quant_table) == NULL)
435 return FALSE;
436 /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
Thomas G. Lane489583f1996-02-07 00:00:00 +0000437 if (qtable->quantval[0] == 0 ||
438 qtable->quantval[Q01_POS] == 0 ||
439 qtable->quantval[Q10_POS] == 0 ||
440 qtable->quantval[Q20_POS] == 0 ||
441 qtable->quantval[Q11_POS] == 0 ||
442 qtable->quantval[Q02_POS] == 0)
443 return FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000444 /* DC values must be at least partly known for all components. */
445 coef_bits = cinfo->coef_bits[ci];
446 if (coef_bits[0] < 0)
447 return FALSE;
448 /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
449 for (coefi = 1; coefi <= 5; coefi++) {
450 coef_bits_latch[coefi] = coef_bits[coefi];
451 if (coef_bits[coefi] != 0)
452 smoothing_useful = TRUE;
453 }
454 coef_bits_latch += SAVED_COEFS;
455 }
456
457 return smoothing_useful;
458}
459
460
461/*
462 * Variant of decompress_data for use when doing block smoothing.
463 */
464
Thomas G. Lane489583f1996-02-07 00:00:00 +0000465METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000466decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
467{
468 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
469 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
470 JDIMENSION block_num, last_block_column;
471 int ci, block_row, block_rows, access_rows;
472 JBLOCKARRAY buffer;
473 JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
474 JSAMPARRAY output_ptr;
475 JDIMENSION output_col;
476 jpeg_component_info *compptr;
477 inverse_DCT_method_ptr inverse_DCT;
478 boolean first_row, last_row;
Pierre Ossman35c47192009-03-09 13:29:37 +0000479 JCOEF * workspace;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000480 int *coef_bits;
481 JQUANT_TBL *quanttbl;
482 INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
483 int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
484 int Al, pred;
485
Pierre Ossman35c47192009-03-09 13:29:37 +0000486 /* Keep a local variable to avoid looking it up more than once */
487 workspace = coef->workspace;
488
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000489 /* Force some input to be done if we are getting ahead of the input. */
490 while (cinfo->input_scan_number <= cinfo->output_scan_number &&
491 ! cinfo->inputctl->eoi_reached) {
492 if (cinfo->input_scan_number == cinfo->output_scan_number) {
493 /* If input is working on current scan, we ordinarily want it to
494 * have completed the current row. But if input scan is DC,
495 * we want it to keep one row ahead so that next block row's DC
496 * values are up to date.
497 */
498 JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
499 if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
500 break;
501 }
502 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
503 return JPEG_SUSPENDED;
504 }
505
506 /* OK, output from the virtual arrays. */
507 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
508 ci++, compptr++) {
509 /* Don't bother to IDCT an uninteresting component. */
510 if (! compptr->component_needed)
511 continue;
512 /* Count non-dummy DCT block rows in this iMCU row. */
513 if (cinfo->output_iMCU_row < last_iMCU_row) {
514 block_rows = compptr->v_samp_factor;
515 access_rows = block_rows * 2; /* this and next iMCU row */
516 last_row = FALSE;
517 } else {
518 /* NB: can't use last_row_height here; it is input-side-dependent! */
519 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
520 if (block_rows == 0) block_rows = compptr->v_samp_factor;
521 access_rows = block_rows; /* this iMCU row only */
522 last_row = TRUE;
523 }
524 /* Align the virtual buffer for this component. */
525 if (cinfo->output_iMCU_row > 0) {
526 access_rows += compptr->v_samp_factor; /* prior iMCU row too */
527 buffer = (*cinfo->mem->access_virt_barray)
528 ((j_common_ptr) cinfo, coef->whole_image[ci],
529 (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
530 (JDIMENSION) access_rows, FALSE);
531 buffer += compptr->v_samp_factor; /* point to current iMCU row */
532 first_row = FALSE;
533 } else {
534 buffer = (*cinfo->mem->access_virt_barray)
535 ((j_common_ptr) cinfo, coef->whole_image[ci],
536 (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
537 first_row = TRUE;
538 }
539 /* Fetch component-dependent info */
540 coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
541 quanttbl = compptr->quant_table;
542 Q00 = quanttbl->quantval[0];
Thomas G. Lane489583f1996-02-07 00:00:00 +0000543 Q01 = quanttbl->quantval[Q01_POS];
544 Q10 = quanttbl->quantval[Q10_POS];
545 Q20 = quanttbl->quantval[Q20_POS];
546 Q11 = quanttbl->quantval[Q11_POS];
547 Q02 = quanttbl->quantval[Q02_POS];
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000548 inverse_DCT = cinfo->idct->inverse_DCT[ci];
549 output_ptr = output_buf[ci];
550 /* Loop over all DCT blocks to be processed. */
551 for (block_row = 0; block_row < block_rows; block_row++) {
552 buffer_ptr = buffer[block_row];
553 if (first_row && block_row == 0)
554 prev_block_row = buffer_ptr;
555 else
556 prev_block_row = buffer[block_row-1];
557 if (last_row && block_row == block_rows-1)
558 next_block_row = buffer_ptr;
559 else
560 next_block_row = buffer[block_row+1];
561 /* We fetch the surrounding DC values using a sliding-register approach.
562 * Initialize all nine here so as to do the right thing on narrow pics.
563 */
564 DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
565 DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
566 DC7 = DC8 = DC9 = (int) next_block_row[0][0];
567 output_col = 0;
568 last_block_column = compptr->width_in_blocks - 1;
569 for (block_num = 0; block_num <= last_block_column; block_num++) {
570 /* Fetch current DCT block into workspace so we can modify it. */
571 jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
572 /* Update DC values */
573 if (block_num < last_block_column) {
574 DC3 = (int) prev_block_row[1][0];
575 DC6 = (int) buffer_ptr[1][0];
576 DC9 = (int) next_block_row[1][0];
577 }
578 /* Compute coefficient estimates per K.8.
579 * An estimate is applied only if coefficient is still zero,
580 * and is not known to be fully accurate.
581 */
582 /* AC01 */
583 if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
584 num = 36 * Q00 * (DC4 - DC6);
585 if (num >= 0) {
586 pred = (int) (((Q01<<7) + num) / (Q01<<8));
587 if (Al > 0 && pred >= (1<<Al))
588 pred = (1<<Al)-1;
589 } else {
590 pred = (int) (((Q01<<7) - num) / (Q01<<8));
591 if (Al > 0 && pred >= (1<<Al))
592 pred = (1<<Al)-1;
593 pred = -pred;
594 }
595 workspace[1] = (JCOEF) pred;
596 }
597 /* AC10 */
598 if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
599 num = 36 * Q00 * (DC2 - DC8);
600 if (num >= 0) {
601 pred = (int) (((Q10<<7) + num) / (Q10<<8));
602 if (Al > 0 && pred >= (1<<Al))
603 pred = (1<<Al)-1;
604 } else {
605 pred = (int) (((Q10<<7) - num) / (Q10<<8));
606 if (Al > 0 && pred >= (1<<Al))
607 pred = (1<<Al)-1;
608 pred = -pred;
609 }
610 workspace[8] = (JCOEF) pred;
611 }
612 /* AC20 */
613 if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
614 num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
615 if (num >= 0) {
616 pred = (int) (((Q20<<7) + num) / (Q20<<8));
617 if (Al > 0 && pred >= (1<<Al))
618 pred = (1<<Al)-1;
619 } else {
620 pred = (int) (((Q20<<7) - num) / (Q20<<8));
621 if (Al > 0 && pred >= (1<<Al))
622 pred = (1<<Al)-1;
623 pred = -pred;
624 }
625 workspace[16] = (JCOEF) pred;
626 }
627 /* AC11 */
628 if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
629 num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
630 if (num >= 0) {
631 pred = (int) (((Q11<<7) + num) / (Q11<<8));
632 if (Al > 0 && pred >= (1<<Al))
633 pred = (1<<Al)-1;
634 } else {
635 pred = (int) (((Q11<<7) - num) / (Q11<<8));
636 if (Al > 0 && pred >= (1<<Al))
637 pred = (1<<Al)-1;
638 pred = -pred;
639 }
640 workspace[9] = (JCOEF) pred;
641 }
642 /* AC02 */
643 if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
644 num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
645 if (num >= 0) {
646 pred = (int) (((Q02<<7) + num) / (Q02<<8));
647 if (Al > 0 && pred >= (1<<Al))
648 pred = (1<<Al)-1;
649 } else {
650 pred = (int) (((Q02<<7) - num) / (Q02<<8));
651 if (Al > 0 && pred >= (1<<Al))
652 pred = (1<<Al)-1;
653 pred = -pred;
654 }
655 workspace[2] = (JCOEF) pred;
656 }
657 /* OK, do the IDCT */
658 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
659 output_ptr, output_col);
660 /* Advance for next column */
661 DC1 = DC2; DC2 = DC3;
662 DC4 = DC5; DC5 = DC6;
663 DC7 = DC8; DC8 = DC9;
664 buffer_ptr++, prev_block_row++, next_block_row++;
DRC49967cd2010-10-09 19:57:51 +0000665 output_col += compptr->_DCT_scaled_size;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000666 }
DRC49967cd2010-10-09 19:57:51 +0000667 output_ptr += compptr->_DCT_scaled_size;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000668 }
669 }
670
671 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
672 return JPEG_ROW_COMPLETED;
673 return JPEG_SCAN_COMPLETED;
674}
675
676#endif /* BLOCK_SMOOTHING_SUPPORTED */
677
678
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000679/*
680 * Initialize coefficient buffer controller.
681 */
682
Thomas G. Lane489583f1996-02-07 00:00:00 +0000683GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000684jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
685{
686 my_coef_ptr coef;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000687
688 coef = (my_coef_ptr)
689 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
690 SIZEOF(my_coef_controller));
691 cinfo->coef = (struct jpeg_d_coef_controller *) coef;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000692 coef->pub.start_input_pass = start_input_pass;
693 coef->pub.start_output_pass = start_output_pass;
694#ifdef BLOCK_SMOOTHING_SUPPORTED
695 coef->coef_bits_latch = NULL;
696#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000697
698 /* Create the coefficient buffer. */
699 if (need_full_buffer) {
700#ifdef D_MULTISCAN_FILES_SUPPORTED
701 /* Allocate a full-image virtual array for each component, */
702 /* padded to a multiple of samp_factor DCT blocks in each direction. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000703 /* Note we ask for a pre-zeroed array. */
704 int ci, access_rows;
705 jpeg_component_info *compptr;
706
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000707 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
708 ci++, compptr++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000709 access_rows = compptr->v_samp_factor;
710#ifdef BLOCK_SMOOTHING_SUPPORTED
711 /* If block smoothing could be used, need a bigger window */
712 if (cinfo->progressive_mode)
713 access_rows *= 3;
714#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000715 coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000716 ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000717 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
718 (long) compptr->h_samp_factor),
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000719 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
720 (long) compptr->v_samp_factor),
721 (JDIMENSION) access_rows);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000722 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000723 coef->pub.consume_data = consume_data;
724 coef->pub.decompress_data = decompress_data;
725 coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000726#else
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000727 ERREXIT(cinfo, JERR_NOT_COMPILED);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000728#endif
729 } else {
730 /* We only need a single-MCU buffer. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000731 JBLOCKROW buffer;
732 int i;
733
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000734 buffer = (JBLOCKROW)
735 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000736 D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
737 for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000738 coef->MCU_buffer[i] = buffer + i;
739 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000740 coef->pub.consume_data = dummy_consume_data;
741 coef->pub.decompress_data = decompress_onepass;
742 coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000743 }
Pierre Ossman35c47192009-03-09 13:29:37 +0000744
745 /* Allocate the workspace buffer */
746 coef->workspace = (JCOEF *)
747 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
748 SIZEOF(JCOEF) * DCTSIZE2);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000749}