blob: 17a97b1bca744ddebcb51284d3e3da7aa348a9c5 [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:
DRCeb32cc12015-06-25 03:44:36 +00007 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRC36a6eec2010-10-08 08:05:44 +00008 * Copyright (C) 2010, D. R. Commander.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00009 * For conditions of distribution and use, see the accompanying README file.
10 *
11 * This file contains the coefficient buffer controller for decompression.
12 * This controller is the top level of the JPEG decompressor proper.
13 * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000014 *
15 * In buffered-image mode, this controller is the interface between
16 * input-oriented processing and output-oriented processing.
17 * Also, the input side (only) is used when reading a file for transcoding.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000018 */
19
DRCeb32cc12015-06-25 03:44:36 +000020#include "jdcoefct.h"
DRC36a6eec2010-10-08 08:05:44 +000021#include "jpegcomp.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000022
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000023
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000024/* Forward declarations */
Thomas G. Lane489583f1996-02-07 00:00:00 +000025METHODDEF(int) decompress_onepass
DRCbc56b752014-05-16 10:43:44 +000026 (j_decompress_ptr cinfo, JSAMPIMAGE output_buf);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000027#ifdef D_MULTISCAN_FILES_SUPPORTED
Thomas G. Lane489583f1996-02-07 00:00:00 +000028METHODDEF(int) decompress_data
DRCbc56b752014-05-16 10:43:44 +000029 (j_decompress_ptr cinfo, JSAMPIMAGE output_buf);
Thomas G. Lanebc79e061995-08-02 00:00:00 +000030#endif
31#ifdef BLOCK_SMOOTHING_SUPPORTED
DRCbc56b752014-05-16 10:43:44 +000032LOCAL(boolean) smoothing_ok (j_decompress_ptr cinfo);
Thomas G. Lane489583f1996-02-07 00:00:00 +000033METHODDEF(int) decompress_smooth_data
DRCbc56b752014-05-16 10:43:44 +000034 (j_decompress_ptr cinfo, JSAMPIMAGE output_buf);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000035#endif
36
37
38/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +000039 * Initialize for an input processing pass.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000040 */
41
Thomas G. Lane489583f1996-02-07 00:00:00 +000042METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000043start_input_pass (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000044{
Thomas G. Lanebc79e061995-08-02 00:00:00 +000045 cinfo->input_iMCU_row = 0;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000046 start_iMCU_row(cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000047}
48
49
50/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +000051 * Initialize for an output processing pass.
52 */
53
Thomas G. Lane489583f1996-02-07 00:00:00 +000054METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000055start_output_pass (j_decompress_ptr cinfo)
56{
57#ifdef BLOCK_SMOOTHING_SUPPORTED
58 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
59
60 /* If multipass, check to see whether to use block smoothing on this pass */
61 if (coef->pub.coef_arrays != NULL) {
62 if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
63 coef->pub.decompress_data = decompress_smooth_data;
64 else
65 coef->pub.decompress_data = decompress_data;
66 }
67#endif
68 cinfo->output_iMCU_row = 0;
69}
70
71
72/*
73 * Decompress and return some data in the single-pass case.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000074 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
Thomas G. Lanebc79e061995-08-02 00:00:00 +000075 * Input and output must run in lockstep since we have only a one-MCU buffer.
76 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000077 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000078 * NB: output_buf contains a plane for each component in image,
79 * which we index according to the component's SOF position.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000080 */
81
Thomas G. Lane489583f1996-02-07 00:00:00 +000082METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000083decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000084{
85 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
DRCe5eaf372014-05-09 18:00:32 +000086 JDIMENSION MCU_col_num; /* index of current MCU within row */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000087 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000088 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
89 int blkn, ci, xindex, yindex, yoffset, useful_width;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000090 JSAMPARRAY output_ptr;
91 JDIMENSION start_col, output_col;
92 jpeg_component_info *compptr;
93 inverse_DCT_method_ptr inverse_DCT;
94
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000095 /* Loop to process as much as one whole iMCU row */
96 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
97 yoffset++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +000098 for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
DRCe5eaf372014-05-09 18:00:32 +000099 MCU_col_num++) {
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000100 /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
DRC5033f3e2014-05-18 18:33:44 +0000101 jzero_far((void *) coef->MCU_buffer[0],
DRC5de454b2014-05-18 19:04:03 +0000102 (size_t) (cinfo->blocks_in_MCU * sizeof(JBLOCK)));
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000103 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
DRCe5eaf372014-05-09 18:00:32 +0000104 /* Suspension forced; update state counters and exit */
105 coef->MCU_vert_offset = yoffset;
106 coef->MCU_ctr = MCU_col_num;
107 return JPEG_SUSPENDED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000108 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000109 /* Determine where data should go in output_buf and do the IDCT thing.
110 * We skip dummy blocks at the right and bottom edges (but blkn gets
111 * incremented past them!). Note the inner loop relies on having
112 * allocated the MCU_buffer[] blocks sequentially.
113 */
DRCe5eaf372014-05-09 18:00:32 +0000114 blkn = 0; /* index of current DCT block within MCU */
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000115 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
DRCe5eaf372014-05-09 18:00:32 +0000116 compptr = cinfo->cur_comp_info[ci];
117 /* Don't bother to IDCT an uninteresting component. */
118 if (! compptr->component_needed) {
119 blkn += compptr->MCU_blocks;
120 continue;
121 }
122 inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
123 useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
124 : compptr->last_col_width;
125 output_ptr = output_buf[compptr->component_index] +
126 yoffset * compptr->_DCT_scaled_size;
127 start_col = MCU_col_num * compptr->MCU_sample_width;
128 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
129 if (cinfo->input_iMCU_row < last_iMCU_row ||
130 yoffset+yindex < compptr->last_row_height) {
131 output_col = start_col;
132 for (xindex = 0; xindex < useful_width; xindex++) {
133 (*inverse_DCT) (cinfo, compptr,
134 (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
135 output_ptr, output_col);
136 output_col += compptr->_DCT_scaled_size;
137 }
138 }
139 blkn += compptr->MCU_width;
140 output_ptr += compptr->_DCT_scaled_size;
141 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000142 }
143 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000144 /* Completed an MCU row, but perhaps not an iMCU row */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000145 coef->MCU_ctr = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000146 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000147 /* Completed the iMCU row, advance counters for next one */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000148 cinfo->output_iMCU_row++;
149 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
150 start_iMCU_row(cinfo);
151 return JPEG_ROW_COMPLETED;
152 }
153 /* Completed the scan */
154 (*cinfo->inputctl->finish_input_pass) (cinfo);
155 return JPEG_SCAN_COMPLETED;
156}
157
158
159/*
160 * Dummy consume-input routine for single-pass operation.
161 */
162
Thomas G. Lane489583f1996-02-07 00:00:00 +0000163METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000164dummy_consume_data (j_decompress_ptr cinfo)
165{
DRCe5eaf372014-05-09 18:00:32 +0000166 return JPEG_SUSPENDED; /* Always indicate nothing was done */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000167}
168
169
170#ifdef D_MULTISCAN_FILES_SUPPORTED
171
172/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000173 * Consume input data and store it in the full-image coefficient buffer.
174 * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
175 * ie, v_samp_factor block rows for each component in the scan.
176 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000177 */
178
Thomas G. Lane489583f1996-02-07 00:00:00 +0000179METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000180consume_data (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000181{
182 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
DRCe5eaf372014-05-09 18:00:32 +0000183 JDIMENSION MCU_col_num; /* index of current MCU within row */
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000184 int blkn, ci, xindex, yindex, yoffset;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000185 JDIMENSION start_col;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000186 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
187 JBLOCKROW buffer_ptr;
188 jpeg_component_info *compptr;
189
190 /* Align the virtual buffers for the components used in this scan. */
191 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
192 compptr = cinfo->cur_comp_info[ci];
193 buffer[ci] = (*cinfo->mem->access_virt_barray)
194 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000195 cinfo->input_iMCU_row * compptr->v_samp_factor,
196 (JDIMENSION) compptr->v_samp_factor, TRUE);
197 /* Note: entropy decoder expects buffer to be zeroed,
198 * but this is handled automatically by the memory manager
199 * because we requested a pre-zeroed array.
200 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000201 }
202
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000203 /* Loop to process one whole iMCU row */
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000204 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
205 yoffset++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000206 for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
DRCe5eaf372014-05-09 18:00:32 +0000207 MCU_col_num++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000208 /* Construct list of pointers to DCT blocks belonging to this MCU */
DRCe5eaf372014-05-09 18:00:32 +0000209 blkn = 0; /* index of current DCT block within MCU */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000210 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
DRCe5eaf372014-05-09 18:00:32 +0000211 compptr = cinfo->cur_comp_info[ci];
212 start_col = MCU_col_num * compptr->MCU_width;
213 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
214 buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
215 for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
216 coef->MCU_buffer[blkn++] = buffer_ptr++;
217 }
218 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000219 }
220 /* Try to fetch the MCU. */
221 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
DRCe5eaf372014-05-09 18:00:32 +0000222 /* Suspension forced; update state counters and exit */
223 coef->MCU_vert_offset = yoffset;
224 coef->MCU_ctr = MCU_col_num;
225 return JPEG_SUSPENDED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000226 }
227 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000228 /* Completed an MCU row, but perhaps not an iMCU row */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000229 coef->MCU_ctr = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000230 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000231 /* Completed the iMCU row, advance counters for next one */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000232 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
233 start_iMCU_row(cinfo);
234 return JPEG_ROW_COMPLETED;
235 }
236 /* Completed the scan */
237 (*cinfo->inputctl->finish_input_pass) (cinfo);
238 return JPEG_SCAN_COMPLETED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000239}
240
241
242/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000243 * Decompress and return some data in the multi-pass case.
244 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
245 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000246 *
247 * NB: output_buf contains a plane for each component in image.
248 */
249
Thomas G. Lane489583f1996-02-07 00:00:00 +0000250METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000251decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000252{
253 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000254 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000255 JDIMENSION block_num;
256 int ci, block_row, block_rows;
257 JBLOCKARRAY buffer;
258 JBLOCKROW buffer_ptr;
259 JSAMPARRAY output_ptr;
260 JDIMENSION output_col;
261 jpeg_component_info *compptr;
262 inverse_DCT_method_ptr inverse_DCT;
263
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000264 /* Force some input to be done if we are getting ahead of the input. */
265 while (cinfo->input_scan_number < cinfo->output_scan_number ||
DRCe5eaf372014-05-09 18:00:32 +0000266 (cinfo->input_scan_number == cinfo->output_scan_number &&
267 cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000268 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
269 return JPEG_SUSPENDED;
270 }
271
272 /* OK, output from the virtual arrays. */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000273 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
274 ci++, compptr++) {
275 /* Don't bother to IDCT an uninteresting component. */
276 if (! compptr->component_needed)
277 continue;
278 /* Align the virtual buffer for this component. */
279 buffer = (*cinfo->mem->access_virt_barray)
280 ((j_common_ptr) cinfo, coef->whole_image[ci],
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000281 cinfo->output_iMCU_row * compptr->v_samp_factor,
282 (JDIMENSION) compptr->v_samp_factor, FALSE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000283 /* Count non-dummy DCT block rows in this iMCU row. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000284 if (cinfo->output_iMCU_row < last_iMCU_row)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000285 block_rows = compptr->v_samp_factor;
286 else {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000287 /* NB: can't use last_row_height here; it is input-side-dependent! */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000288 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
289 if (block_rows == 0) block_rows = compptr->v_samp_factor;
290 }
291 inverse_DCT = cinfo->idct->inverse_DCT[ci];
292 output_ptr = output_buf[ci];
293 /* Loop over all DCT blocks to be processed. */
294 for (block_row = 0; block_row < block_rows; block_row++) {
295 buffer_ptr = buffer[block_row];
296 output_col = 0;
297 for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
DRCe5eaf372014-05-09 18:00:32 +0000298 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
299 output_ptr, output_col);
300 buffer_ptr++;
301 output_col += compptr->_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000302 }
DRC49967cd2010-10-09 19:57:51 +0000303 output_ptr += compptr->_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000304 }
305 }
306
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000307 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
308 return JPEG_ROW_COMPLETED;
309 return JPEG_SCAN_COMPLETED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000310}
311
312#endif /* D_MULTISCAN_FILES_SUPPORTED */
313
314
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000315#ifdef BLOCK_SMOOTHING_SUPPORTED
316
317/*
318 * This code applies interblock smoothing as described by section K.8
319 * of the JPEG standard: the first 5 AC coefficients are estimated from
320 * the DC values of a DCT block and its 8 neighboring blocks.
321 * We apply smoothing only for progressive JPEG decoding, and only if
322 * the coefficients it can estimate are not yet known to full precision.
323 */
324
Thomas G. Lane489583f1996-02-07 00:00:00 +0000325/* Natural-order array positions of the first 5 zigzag-order coefficients */
326#define Q01_POS 1
327#define Q10_POS 8
328#define Q20_POS 16
329#define Q11_POS 9
330#define Q02_POS 2
331
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000332/*
333 * Determine whether block smoothing is applicable and safe.
334 * We also latch the current states of the coef_bits[] entries for the
335 * AC coefficients; otherwise, if the input side of the decompressor
336 * advances into a new scan, we might think the coefficients are known
337 * more accurately than they really are.
338 */
339
Thomas G. Lane489583f1996-02-07 00:00:00 +0000340LOCAL(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000341smoothing_ok (j_decompress_ptr cinfo)
342{
343 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
344 boolean smoothing_useful = FALSE;
345 int ci, coefi;
346 jpeg_component_info *compptr;
347 JQUANT_TBL * qtable;
348 int * coef_bits;
349 int * coef_bits_latch;
350
351 if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
352 return FALSE;
353
354 /* Allocate latch area if not already done */
355 if (coef->coef_bits_latch == NULL)
356 coef->coef_bits_latch = (int *)
357 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRCe5eaf372014-05-09 18:00:32 +0000358 cinfo->num_components *
DRC5de454b2014-05-18 19:04:03 +0000359 (SAVED_COEFS * sizeof(int)));
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000360 coef_bits_latch = coef->coef_bits_latch;
361
362 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
363 ci++, compptr++) {
364 /* All components' quantization values must already be latched. */
365 if ((qtable = compptr->quant_table) == NULL)
366 return FALSE;
367 /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
Thomas G. Lane489583f1996-02-07 00:00:00 +0000368 if (qtable->quantval[0] == 0 ||
DRCe5eaf372014-05-09 18:00:32 +0000369 qtable->quantval[Q01_POS] == 0 ||
370 qtable->quantval[Q10_POS] == 0 ||
371 qtable->quantval[Q20_POS] == 0 ||
372 qtable->quantval[Q11_POS] == 0 ||
373 qtable->quantval[Q02_POS] == 0)
Thomas G. Lane489583f1996-02-07 00:00:00 +0000374 return FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000375 /* DC values must be at least partly known for all components. */
376 coef_bits = cinfo->coef_bits[ci];
377 if (coef_bits[0] < 0)
378 return FALSE;
379 /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
380 for (coefi = 1; coefi <= 5; coefi++) {
381 coef_bits_latch[coefi] = coef_bits[coefi];
382 if (coef_bits[coefi] != 0)
DRCe5eaf372014-05-09 18:00:32 +0000383 smoothing_useful = TRUE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000384 }
385 coef_bits_latch += SAVED_COEFS;
386 }
387
388 return smoothing_useful;
389}
390
391
392/*
393 * Variant of decompress_data for use when doing block smoothing.
394 */
395
Thomas G. Lane489583f1996-02-07 00:00:00 +0000396METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000397decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
398{
399 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
400 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
401 JDIMENSION block_num, last_block_column;
402 int ci, block_row, block_rows, access_rows;
403 JBLOCKARRAY buffer;
404 JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
405 JSAMPARRAY output_ptr;
406 JDIMENSION output_col;
407 jpeg_component_info *compptr;
408 inverse_DCT_method_ptr inverse_DCT;
409 boolean first_row, last_row;
Pierre Ossman35c47192009-03-09 13:29:37 +0000410 JCOEF * workspace;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000411 int *coef_bits;
412 JQUANT_TBL *quanttbl;
413 INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
414 int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
415 int Al, pred;
416
Pierre Ossman35c47192009-03-09 13:29:37 +0000417 /* Keep a local variable to avoid looking it up more than once */
418 workspace = coef->workspace;
419
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000420 /* Force some input to be done if we are getting ahead of the input. */
421 while (cinfo->input_scan_number <= cinfo->output_scan_number &&
DRCe5eaf372014-05-09 18:00:32 +0000422 ! cinfo->inputctl->eoi_reached) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000423 if (cinfo->input_scan_number == cinfo->output_scan_number) {
424 /* If input is working on current scan, we ordinarily want it to
425 * have completed the current row. But if input scan is DC,
426 * we want it to keep one row ahead so that next block row's DC
427 * values are up to date.
428 */
429 JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
430 if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
DRCe5eaf372014-05-09 18:00:32 +0000431 break;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000432 }
433 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
434 return JPEG_SUSPENDED;
435 }
436
437 /* OK, output from the virtual arrays. */
438 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
439 ci++, compptr++) {
440 /* Don't bother to IDCT an uninteresting component. */
441 if (! compptr->component_needed)
442 continue;
443 /* Count non-dummy DCT block rows in this iMCU row. */
444 if (cinfo->output_iMCU_row < last_iMCU_row) {
445 block_rows = compptr->v_samp_factor;
446 access_rows = block_rows * 2; /* this and next iMCU row */
447 last_row = FALSE;
448 } else {
449 /* NB: can't use last_row_height here; it is input-side-dependent! */
450 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
451 if (block_rows == 0) block_rows = compptr->v_samp_factor;
452 access_rows = block_rows; /* this iMCU row only */
453 last_row = TRUE;
454 }
455 /* Align the virtual buffer for this component. */
456 if (cinfo->output_iMCU_row > 0) {
457 access_rows += compptr->v_samp_factor; /* prior iMCU row too */
458 buffer = (*cinfo->mem->access_virt_barray)
DRCe5eaf372014-05-09 18:00:32 +0000459 ((j_common_ptr) cinfo, coef->whole_image[ci],
460 (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
461 (JDIMENSION) access_rows, FALSE);
462 buffer += compptr->v_samp_factor; /* point to current iMCU row */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000463 first_row = FALSE;
464 } else {
465 buffer = (*cinfo->mem->access_virt_barray)
DRCe5eaf372014-05-09 18:00:32 +0000466 ((j_common_ptr) cinfo, coef->whole_image[ci],
467 (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000468 first_row = TRUE;
469 }
470 /* Fetch component-dependent info */
471 coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
472 quanttbl = compptr->quant_table;
473 Q00 = quanttbl->quantval[0];
Thomas G. Lane489583f1996-02-07 00:00:00 +0000474 Q01 = quanttbl->quantval[Q01_POS];
475 Q10 = quanttbl->quantval[Q10_POS];
476 Q20 = quanttbl->quantval[Q20_POS];
477 Q11 = quanttbl->quantval[Q11_POS];
478 Q02 = quanttbl->quantval[Q02_POS];
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000479 inverse_DCT = cinfo->idct->inverse_DCT[ci];
480 output_ptr = output_buf[ci];
481 /* Loop over all DCT blocks to be processed. */
482 for (block_row = 0; block_row < block_rows; block_row++) {
483 buffer_ptr = buffer[block_row];
484 if (first_row && block_row == 0)
DRCe5eaf372014-05-09 18:00:32 +0000485 prev_block_row = buffer_ptr;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000486 else
DRCe5eaf372014-05-09 18:00:32 +0000487 prev_block_row = buffer[block_row-1];
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000488 if (last_row && block_row == block_rows-1)
DRCe5eaf372014-05-09 18:00:32 +0000489 next_block_row = buffer_ptr;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000490 else
DRCe5eaf372014-05-09 18:00:32 +0000491 next_block_row = buffer[block_row+1];
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000492 /* We fetch the surrounding DC values using a sliding-register approach.
493 * Initialize all nine here so as to do the right thing on narrow pics.
494 */
495 DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
496 DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
497 DC7 = DC8 = DC9 = (int) next_block_row[0][0];
498 output_col = 0;
499 last_block_column = compptr->width_in_blocks - 1;
500 for (block_num = 0; block_num <= last_block_column; block_num++) {
DRCe5eaf372014-05-09 18:00:32 +0000501 /* Fetch current DCT block into workspace so we can modify it. */
502 jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
503 /* Update DC values */
504 if (block_num < last_block_column) {
505 DC3 = (int) prev_block_row[1][0];
506 DC6 = (int) buffer_ptr[1][0];
507 DC9 = (int) next_block_row[1][0];
508 }
509 /* Compute coefficient estimates per K.8.
510 * An estimate is applied only if coefficient is still zero,
511 * and is not known to be fully accurate.
512 */
513 /* AC01 */
514 if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
515 num = 36 * Q00 * (DC4 - DC6);
516 if (num >= 0) {
517 pred = (int) (((Q01<<7) + num) / (Q01<<8));
518 if (Al > 0 && pred >= (1<<Al))
519 pred = (1<<Al)-1;
520 } else {
521 pred = (int) (((Q01<<7) - num) / (Q01<<8));
522 if (Al > 0 && pred >= (1<<Al))
523 pred = (1<<Al)-1;
524 pred = -pred;
525 }
526 workspace[1] = (JCOEF) pred;
527 }
528 /* AC10 */
529 if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
530 num = 36 * Q00 * (DC2 - DC8);
531 if (num >= 0) {
532 pred = (int) (((Q10<<7) + num) / (Q10<<8));
533 if (Al > 0 && pred >= (1<<Al))
534 pred = (1<<Al)-1;
535 } else {
536 pred = (int) (((Q10<<7) - num) / (Q10<<8));
537 if (Al > 0 && pred >= (1<<Al))
538 pred = (1<<Al)-1;
539 pred = -pred;
540 }
541 workspace[8] = (JCOEF) pred;
542 }
543 /* AC20 */
544 if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
545 num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
546 if (num >= 0) {
547 pred = (int) (((Q20<<7) + num) / (Q20<<8));
548 if (Al > 0 && pred >= (1<<Al))
549 pred = (1<<Al)-1;
550 } else {
551 pred = (int) (((Q20<<7) - num) / (Q20<<8));
552 if (Al > 0 && pred >= (1<<Al))
553 pred = (1<<Al)-1;
554 pred = -pred;
555 }
556 workspace[16] = (JCOEF) pred;
557 }
558 /* AC11 */
559 if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
560 num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
561 if (num >= 0) {
562 pred = (int) (((Q11<<7) + num) / (Q11<<8));
563 if (Al > 0 && pred >= (1<<Al))
564 pred = (1<<Al)-1;
565 } else {
566 pred = (int) (((Q11<<7) - num) / (Q11<<8));
567 if (Al > 0 && pred >= (1<<Al))
568 pred = (1<<Al)-1;
569 pred = -pred;
570 }
571 workspace[9] = (JCOEF) pred;
572 }
573 /* AC02 */
574 if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
575 num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
576 if (num >= 0) {
577 pred = (int) (((Q02<<7) + num) / (Q02<<8));
578 if (Al > 0 && pred >= (1<<Al))
579 pred = (1<<Al)-1;
580 } else {
581 pred = (int) (((Q02<<7) - num) / (Q02<<8));
582 if (Al > 0 && pred >= (1<<Al))
583 pred = (1<<Al)-1;
584 pred = -pred;
585 }
586 workspace[2] = (JCOEF) pred;
587 }
588 /* OK, do the IDCT */
589 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
590 output_ptr, output_col);
591 /* Advance for next column */
592 DC1 = DC2; DC2 = DC3;
593 DC4 = DC5; DC5 = DC6;
594 DC7 = DC8; DC8 = DC9;
595 buffer_ptr++, prev_block_row++, next_block_row++;
596 output_col += compptr->_DCT_scaled_size;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000597 }
DRC49967cd2010-10-09 19:57:51 +0000598 output_ptr += compptr->_DCT_scaled_size;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000599 }
600 }
601
602 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
603 return JPEG_ROW_COMPLETED;
604 return JPEG_SCAN_COMPLETED;
605}
606
607#endif /* BLOCK_SMOOTHING_SUPPORTED */
608
609
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000610/*
611 * Initialize coefficient buffer controller.
612 */
613
Thomas G. Lane489583f1996-02-07 00:00:00 +0000614GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000615jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
616{
617 my_coef_ptr coef;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000618
619 coef = (my_coef_ptr)
620 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000621 sizeof(my_coef_controller));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000622 cinfo->coef = (struct jpeg_d_coef_controller *) coef;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000623 coef->pub.start_input_pass = start_input_pass;
624 coef->pub.start_output_pass = start_output_pass;
625#ifdef BLOCK_SMOOTHING_SUPPORTED
626 coef->coef_bits_latch = NULL;
627#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000628
629 /* Create the coefficient buffer. */
630 if (need_full_buffer) {
631#ifdef D_MULTISCAN_FILES_SUPPORTED
632 /* Allocate a full-image virtual array for each component, */
633 /* padded to a multiple of samp_factor DCT blocks in each direction. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000634 /* Note we ask for a pre-zeroed array. */
635 int ci, access_rows;
636 jpeg_component_info *compptr;
637
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000638 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
DRCe5eaf372014-05-09 18:00:32 +0000639 ci++, compptr++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000640 access_rows = compptr->v_samp_factor;
641#ifdef BLOCK_SMOOTHING_SUPPORTED
642 /* If block smoothing could be used, need a bigger window */
643 if (cinfo->progressive_mode)
DRCe5eaf372014-05-09 18:00:32 +0000644 access_rows *= 3;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000645#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000646 coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
DRCe5eaf372014-05-09 18:00:32 +0000647 ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
648 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
649 (long) compptr->h_samp_factor),
650 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
651 (long) compptr->v_samp_factor),
652 (JDIMENSION) access_rows);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000653 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000654 coef->pub.consume_data = consume_data;
655 coef->pub.decompress_data = decompress_data;
656 coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000657#else
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000658 ERREXIT(cinfo, JERR_NOT_COMPILED);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000659#endif
660 } else {
661 /* We only need a single-MCU buffer. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000662 JBLOCKROW buffer;
663 int i;
664
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000665 buffer = (JBLOCKROW)
666 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000667 D_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000668 for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000669 coef->MCU_buffer[i] = buffer + i;
670 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000671 coef->pub.consume_data = dummy_consume_data;
672 coef->pub.decompress_data = decompress_onepass;
673 coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000674 }
Pierre Ossman35c47192009-03-09 13:29:37 +0000675
676 /* Allocate the workspace buffer */
677 coef->workspace = (JCOEF *)
678 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000679 sizeof(JCOEF) * DCTSIZE2);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000680}