blob: c51e3ffaa0dc7d9d68a0d49e5962b59365dd9993 [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
DRCce0dd942016-02-06 12:18:44 -06008 * Copyright (C) 2010, 2015-2016, D. R. Commander.
DRC7e3acc02015-10-10 10:25:46 -05009 * For conditions of distribution and use, see the accompanying README.ijg
10 * file.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000011 *
12 * This file contains the coefficient buffer controller for decompression.
13 * This controller is the top level of the JPEG decompressor proper.
14 * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000015 *
16 * In buffered-image mode, this controller is the interface between
17 * input-oriented processing and output-oriented processing.
18 * Also, the input side (only) is used when reading a file for transcoding.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000019 */
20
DRCce0dd942016-02-06 12:18:44 -060021#include "jinclude.h"
DRCeb32cc12015-06-25 03:44:36 +000022#include "jdcoefct.h"
DRC36a6eec2010-10-08 08:05:44 +000023#include "jpegcomp.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000024
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000025
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000026/* Forward declarations */
Thomas G. Lane489583f1996-02-07 00:00:00 +000027METHODDEF(int) decompress_onepass
DRCbc56b752014-05-16 10:43:44 +000028 (j_decompress_ptr cinfo, JSAMPIMAGE output_buf);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000029#ifdef D_MULTISCAN_FILES_SUPPORTED
Thomas G. Lane489583f1996-02-07 00:00:00 +000030METHODDEF(int) decompress_data
DRCbc56b752014-05-16 10:43:44 +000031 (j_decompress_ptr cinfo, JSAMPIMAGE output_buf);
Thomas G. Lanebc79e061995-08-02 00:00:00 +000032#endif
33#ifdef BLOCK_SMOOTHING_SUPPORTED
DRCbc56b752014-05-16 10:43:44 +000034LOCAL(boolean) smoothing_ok (j_decompress_ptr cinfo);
Thomas G. Lane489583f1996-02-07 00:00:00 +000035METHODDEF(int) decompress_smooth_data
DRCbc56b752014-05-16 10:43:44 +000036 (j_decompress_ptr cinfo, JSAMPIMAGE output_buf);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000037#endif
38
39
40/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +000041 * Initialize for an input processing pass.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000042 */
43
Thomas G. Lane489583f1996-02-07 00:00:00 +000044METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000045start_input_pass (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000046{
Thomas G. Lanebc79e061995-08-02 00:00:00 +000047 cinfo->input_iMCU_row = 0;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000048 start_iMCU_row(cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000049}
50
51
52/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +000053 * Initialize for an output processing pass.
54 */
55
Thomas G. Lane489583f1996-02-07 00:00:00 +000056METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000057start_output_pass (j_decompress_ptr cinfo)
58{
59#ifdef BLOCK_SMOOTHING_SUPPORTED
60 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
61
62 /* If multipass, check to see whether to use block smoothing on this pass */
63 if (coef->pub.coef_arrays != NULL) {
64 if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
65 coef->pub.decompress_data = decompress_smooth_data;
66 else
67 coef->pub.decompress_data = decompress_data;
68 }
69#endif
70 cinfo->output_iMCU_row = 0;
71}
72
73
74/*
75 * Decompress and return some data in the single-pass case.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000076 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
Thomas G. Lanebc79e061995-08-02 00:00:00 +000077 * Input and output must run in lockstep since we have only a one-MCU buffer.
78 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000079 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000080 * NB: output_buf contains a plane for each component in image,
81 * which we index according to the component's SOF position.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000082 */
83
Thomas G. Lane489583f1996-02-07 00:00:00 +000084METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000085decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000086{
87 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
DRCe5eaf372014-05-09 18:00:32 +000088 JDIMENSION MCU_col_num; /* index of current MCU within row */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000089 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000090 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
91 int blkn, ci, xindex, yindex, yoffset, useful_width;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000092 JSAMPARRAY output_ptr;
93 JDIMENSION start_col, output_col;
94 jpeg_component_info *compptr;
95 inverse_DCT_method_ptr inverse_DCT;
96
Thomas G. Lanea8b67c41995-03-15 00:00:00 +000097 /* Loop to process as much as one whole iMCU row */
98 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
99 yoffset++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000100 for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
DRCe5eaf372014-05-09 18:00:32 +0000101 MCU_col_num++) {
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000102 /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
DRC5033f3e2014-05-18 18:33:44 +0000103 jzero_far((void *) coef->MCU_buffer[0],
DRC5de454b2014-05-18 19:04:03 +0000104 (size_t) (cinfo->blocks_in_MCU * sizeof(JBLOCK)));
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000105 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
DRCe5eaf372014-05-09 18:00:32 +0000106 /* Suspension forced; update state counters and exit */
107 coef->MCU_vert_offset = yoffset;
108 coef->MCU_ctr = MCU_col_num;
109 return JPEG_SUSPENDED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000110 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000111 /* Determine where data should go in output_buf and do the IDCT thing.
112 * We skip dummy blocks at the right and bottom edges (but blkn gets
113 * incremented past them!). Note the inner loop relies on having
114 * allocated the MCU_buffer[] blocks sequentially.
115 */
DRCe5eaf372014-05-09 18:00:32 +0000116 blkn = 0; /* index of current DCT block within MCU */
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000117 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
DRCe5eaf372014-05-09 18:00:32 +0000118 compptr = cinfo->cur_comp_info[ci];
119 /* Don't bother to IDCT an uninteresting component. */
120 if (! compptr->component_needed) {
121 blkn += compptr->MCU_blocks;
122 continue;
123 }
124 inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
125 useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
126 : compptr->last_col_width;
127 output_ptr = output_buf[compptr->component_index] +
128 yoffset * compptr->_DCT_scaled_size;
129 start_col = MCU_col_num * compptr->MCU_sample_width;
130 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
131 if (cinfo->input_iMCU_row < last_iMCU_row ||
132 yoffset+yindex < compptr->last_row_height) {
133 output_col = start_col;
134 for (xindex = 0; xindex < useful_width; xindex++) {
135 (*inverse_DCT) (cinfo, compptr,
136 (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
137 output_ptr, output_col);
138 output_col += compptr->_DCT_scaled_size;
139 }
140 }
141 blkn += compptr->MCU_width;
142 output_ptr += compptr->_DCT_scaled_size;
143 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000144 }
145 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000146 /* Completed an MCU row, but perhaps not an iMCU row */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000147 coef->MCU_ctr = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000148 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000149 /* Completed the iMCU row, advance counters for next one */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000150 cinfo->output_iMCU_row++;
151 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
152 start_iMCU_row(cinfo);
153 return JPEG_ROW_COMPLETED;
154 }
155 /* Completed the scan */
156 (*cinfo->inputctl->finish_input_pass) (cinfo);
157 return JPEG_SCAN_COMPLETED;
158}
159
160
161/*
162 * Dummy consume-input routine for single-pass operation.
163 */
164
Thomas G. Lane489583f1996-02-07 00:00:00 +0000165METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000166dummy_consume_data (j_decompress_ptr cinfo)
167{
DRCe5eaf372014-05-09 18:00:32 +0000168 return JPEG_SUSPENDED; /* Always indicate nothing was done */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000169}
170
171
172#ifdef D_MULTISCAN_FILES_SUPPORTED
173
174/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000175 * Consume input data and store it in the full-image coefficient buffer.
176 * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
177 * ie, v_samp_factor block rows for each component in the scan.
178 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000179 */
180
Thomas G. Lane489583f1996-02-07 00:00:00 +0000181METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000182consume_data (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000183{
184 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
DRCe5eaf372014-05-09 18:00:32 +0000185 JDIMENSION MCU_col_num; /* index of current MCU within row */
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000186 int blkn, ci, xindex, yindex, yoffset;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000187 JDIMENSION start_col;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000188 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
189 JBLOCKROW buffer_ptr;
190 jpeg_component_info *compptr;
191
192 /* Align the virtual buffers for the components used in this scan. */
193 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
194 compptr = cinfo->cur_comp_info[ci];
195 buffer[ci] = (*cinfo->mem->access_virt_barray)
196 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000197 cinfo->input_iMCU_row * compptr->v_samp_factor,
198 (JDIMENSION) compptr->v_samp_factor, TRUE);
199 /* Note: entropy decoder expects buffer to be zeroed,
200 * but this is handled automatically by the memory manager
201 * because we requested a pre-zeroed array.
202 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000203 }
204
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000205 /* Loop to process one whole iMCU row */
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000206 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
207 yoffset++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000208 for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
DRCe5eaf372014-05-09 18:00:32 +0000209 MCU_col_num++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000210 /* Construct list of pointers to DCT blocks belonging to this MCU */
DRCe5eaf372014-05-09 18:00:32 +0000211 blkn = 0; /* index of current DCT block within MCU */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000212 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
DRCe5eaf372014-05-09 18:00:32 +0000213 compptr = cinfo->cur_comp_info[ci];
214 start_col = MCU_col_num * compptr->MCU_width;
215 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
216 buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
217 for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
218 coef->MCU_buffer[blkn++] = buffer_ptr++;
219 }
220 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000221 }
222 /* Try to fetch the MCU. */
223 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
DRCe5eaf372014-05-09 18:00:32 +0000224 /* Suspension forced; update state counters and exit */
225 coef->MCU_vert_offset = yoffset;
226 coef->MCU_ctr = MCU_col_num;
227 return JPEG_SUSPENDED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000228 }
229 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000230 /* Completed an MCU row, but perhaps not an iMCU row */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000231 coef->MCU_ctr = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000232 }
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000233 /* Completed the iMCU row, advance counters for next one */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000234 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
235 start_iMCU_row(cinfo);
236 return JPEG_ROW_COMPLETED;
237 }
238 /* Completed the scan */
239 (*cinfo->inputctl->finish_input_pass) (cinfo);
240 return JPEG_SCAN_COMPLETED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000241}
242
243
244/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000245 * Decompress and return some data in the multi-pass case.
246 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
247 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000248 *
249 * NB: output_buf contains a plane for each component in image.
250 */
251
Thomas G. Lane489583f1996-02-07 00:00:00 +0000252METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000253decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000254{
255 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000256 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000257 JDIMENSION block_num;
258 int ci, block_row, block_rows;
259 JBLOCKARRAY buffer;
260 JBLOCKROW buffer_ptr;
261 JSAMPARRAY output_ptr;
262 JDIMENSION output_col;
263 jpeg_component_info *compptr;
264 inverse_DCT_method_ptr inverse_DCT;
265
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000266 /* Force some input to be done if we are getting ahead of the input. */
267 while (cinfo->input_scan_number < cinfo->output_scan_number ||
DRCe5eaf372014-05-09 18:00:32 +0000268 (cinfo->input_scan_number == cinfo->output_scan_number &&
269 cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000270 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
271 return JPEG_SUSPENDED;
272 }
273
274 /* OK, output from the virtual arrays. */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000275 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
276 ci++, compptr++) {
277 /* Don't bother to IDCT an uninteresting component. */
278 if (! compptr->component_needed)
279 continue;
280 /* Align the virtual buffer for this component. */
281 buffer = (*cinfo->mem->access_virt_barray)
282 ((j_common_ptr) cinfo, coef->whole_image[ci],
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000283 cinfo->output_iMCU_row * compptr->v_samp_factor,
284 (JDIMENSION) compptr->v_samp_factor, FALSE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000285 /* Count non-dummy DCT block rows in this iMCU row. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000286 if (cinfo->output_iMCU_row < last_iMCU_row)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000287 block_rows = compptr->v_samp_factor;
288 else {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000289 /* NB: can't use last_row_height here; it is input-side-dependent! */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000290 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
291 if (block_rows == 0) block_rows = compptr->v_samp_factor;
292 }
293 inverse_DCT = cinfo->idct->inverse_DCT[ci];
294 output_ptr = output_buf[ci];
295 /* Loop over all DCT blocks to be processed. */
296 for (block_row = 0; block_row < block_rows; block_row++) {
297 buffer_ptr = buffer[block_row];
298 output_col = 0;
299 for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
DRCe5eaf372014-05-09 18:00:32 +0000300 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
301 output_ptr, output_col);
302 buffer_ptr++;
303 output_col += compptr->_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000304 }
DRC49967cd2010-10-09 19:57:51 +0000305 output_ptr += compptr->_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000306 }
307 }
308
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000309 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
310 return JPEG_ROW_COMPLETED;
311 return JPEG_SCAN_COMPLETED;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000312}
313
314#endif /* D_MULTISCAN_FILES_SUPPORTED */
315
316
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000317#ifdef BLOCK_SMOOTHING_SUPPORTED
318
319/*
320 * This code applies interblock smoothing as described by section K.8
321 * of the JPEG standard: the first 5 AC coefficients are estimated from
322 * the DC values of a DCT block and its 8 neighboring blocks.
323 * We apply smoothing only for progressive JPEG decoding, and only if
324 * the coefficients it can estimate are not yet known to full precision.
325 */
326
Thomas G. Lane489583f1996-02-07 00:00:00 +0000327/* Natural-order array positions of the first 5 zigzag-order coefficients */
328#define Q01_POS 1
329#define Q10_POS 8
330#define Q20_POS 16
331#define Q11_POS 9
332#define Q02_POS 2
333
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000334/*
335 * Determine whether block smoothing is applicable and safe.
336 * We also latch the current states of the coef_bits[] entries for the
337 * AC coefficients; otherwise, if the input side of the decompressor
338 * advances into a new scan, we might think the coefficients are known
339 * more accurately than they really are.
340 */
341
Thomas G. Lane489583f1996-02-07 00:00:00 +0000342LOCAL(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000343smoothing_ok (j_decompress_ptr cinfo)
344{
345 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
346 boolean smoothing_useful = FALSE;
347 int ci, coefi;
348 jpeg_component_info *compptr;
349 JQUANT_TBL * qtable;
350 int * coef_bits;
351 int * coef_bits_latch;
352
353 if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
354 return FALSE;
355
356 /* Allocate latch area if not already done */
357 if (coef->coef_bits_latch == NULL)
358 coef->coef_bits_latch = (int *)
359 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRCe5eaf372014-05-09 18:00:32 +0000360 cinfo->num_components *
DRC5de454b2014-05-18 19:04:03 +0000361 (SAVED_COEFS * sizeof(int)));
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000362 coef_bits_latch = coef->coef_bits_latch;
363
364 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
365 ci++, compptr++) {
366 /* All components' quantization values must already be latched. */
367 if ((qtable = compptr->quant_table) == NULL)
368 return FALSE;
369 /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
Thomas G. Lane489583f1996-02-07 00:00:00 +0000370 if (qtable->quantval[0] == 0 ||
DRCe5eaf372014-05-09 18:00:32 +0000371 qtable->quantval[Q01_POS] == 0 ||
372 qtable->quantval[Q10_POS] == 0 ||
373 qtable->quantval[Q20_POS] == 0 ||
374 qtable->quantval[Q11_POS] == 0 ||
375 qtable->quantval[Q02_POS] == 0)
Thomas G. Lane489583f1996-02-07 00:00:00 +0000376 return FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000377 /* DC values must be at least partly known for all components. */
378 coef_bits = cinfo->coef_bits[ci];
379 if (coef_bits[0] < 0)
380 return FALSE;
381 /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
382 for (coefi = 1; coefi <= 5; coefi++) {
383 coef_bits_latch[coefi] = coef_bits[coefi];
384 if (coef_bits[coefi] != 0)
DRCe5eaf372014-05-09 18:00:32 +0000385 smoothing_useful = TRUE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000386 }
387 coef_bits_latch += SAVED_COEFS;
388 }
389
390 return smoothing_useful;
391}
392
393
394/*
395 * Variant of decompress_data for use when doing block smoothing.
396 */
397
Thomas G. Lane489583f1996-02-07 00:00:00 +0000398METHODDEF(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000399decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
400{
401 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
402 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
403 JDIMENSION block_num, last_block_column;
404 int ci, block_row, block_rows, access_rows;
405 JBLOCKARRAY buffer;
406 JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
407 JSAMPARRAY output_ptr;
408 JDIMENSION output_col;
409 jpeg_component_info *compptr;
410 inverse_DCT_method_ptr inverse_DCT;
411 boolean first_row, last_row;
Pierre Ossman35c47192009-03-09 13:29:37 +0000412 JCOEF * workspace;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000413 int *coef_bits;
414 JQUANT_TBL *quanttbl;
DRC1e32fe32015-10-14 17:32:39 -0500415 JLONG Q00,Q01,Q02,Q10,Q11,Q20, num;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000416 int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
417 int Al, pred;
418
Pierre Ossman35c47192009-03-09 13:29:37 +0000419 /* Keep a local variable to avoid looking it up more than once */
420 workspace = coef->workspace;
421
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000422 /* Force some input to be done if we are getting ahead of the input. */
423 while (cinfo->input_scan_number <= cinfo->output_scan_number &&
DRCe5eaf372014-05-09 18:00:32 +0000424 ! cinfo->inputctl->eoi_reached) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000425 if (cinfo->input_scan_number == cinfo->output_scan_number) {
426 /* If input is working on current scan, we ordinarily want it to
427 * have completed the current row. But if input scan is DC,
428 * we want it to keep one row ahead so that next block row's DC
429 * values are up to date.
430 */
431 JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
432 if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
DRCe5eaf372014-05-09 18:00:32 +0000433 break;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000434 }
435 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
436 return JPEG_SUSPENDED;
437 }
438
439 /* OK, output from the virtual arrays. */
440 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
441 ci++, compptr++) {
442 /* Don't bother to IDCT an uninteresting component. */
443 if (! compptr->component_needed)
444 continue;
445 /* Count non-dummy DCT block rows in this iMCU row. */
446 if (cinfo->output_iMCU_row < last_iMCU_row) {
447 block_rows = compptr->v_samp_factor;
448 access_rows = block_rows * 2; /* this and next iMCU row */
449 last_row = FALSE;
450 } else {
451 /* NB: can't use last_row_height here; it is input-side-dependent! */
452 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
453 if (block_rows == 0) block_rows = compptr->v_samp_factor;
454 access_rows = block_rows; /* this iMCU row only */
455 last_row = TRUE;
456 }
457 /* Align the virtual buffer for this component. */
458 if (cinfo->output_iMCU_row > 0) {
459 access_rows += compptr->v_samp_factor; /* prior iMCU row too */
460 buffer = (*cinfo->mem->access_virt_barray)
DRCe5eaf372014-05-09 18:00:32 +0000461 ((j_common_ptr) cinfo, coef->whole_image[ci],
462 (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
463 (JDIMENSION) access_rows, FALSE);
464 buffer += compptr->v_samp_factor; /* point to current iMCU row */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000465 first_row = FALSE;
466 } else {
467 buffer = (*cinfo->mem->access_virt_barray)
DRCe5eaf372014-05-09 18:00:32 +0000468 ((j_common_ptr) cinfo, coef->whole_image[ci],
469 (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000470 first_row = TRUE;
471 }
472 /* Fetch component-dependent info */
473 coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
474 quanttbl = compptr->quant_table;
475 Q00 = quanttbl->quantval[0];
Thomas G. Lane489583f1996-02-07 00:00:00 +0000476 Q01 = quanttbl->quantval[Q01_POS];
477 Q10 = quanttbl->quantval[Q10_POS];
478 Q20 = quanttbl->quantval[Q20_POS];
479 Q11 = quanttbl->quantval[Q11_POS];
480 Q02 = quanttbl->quantval[Q02_POS];
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000481 inverse_DCT = cinfo->idct->inverse_DCT[ci];
482 output_ptr = output_buf[ci];
483 /* Loop over all DCT blocks to be processed. */
484 for (block_row = 0; block_row < block_rows; block_row++) {
485 buffer_ptr = buffer[block_row];
486 if (first_row && block_row == 0)
DRCe5eaf372014-05-09 18:00:32 +0000487 prev_block_row = buffer_ptr;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000488 else
DRCe5eaf372014-05-09 18:00:32 +0000489 prev_block_row = buffer[block_row-1];
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000490 if (last_row && block_row == block_rows-1)
DRCe5eaf372014-05-09 18:00:32 +0000491 next_block_row = buffer_ptr;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000492 else
DRCe5eaf372014-05-09 18:00:32 +0000493 next_block_row = buffer[block_row+1];
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000494 /* We fetch the surrounding DC values using a sliding-register approach.
495 * Initialize all nine here so as to do the right thing on narrow pics.
496 */
497 DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
498 DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
499 DC7 = DC8 = DC9 = (int) next_block_row[0][0];
500 output_col = 0;
501 last_block_column = compptr->width_in_blocks - 1;
502 for (block_num = 0; block_num <= last_block_column; block_num++) {
DRCe5eaf372014-05-09 18:00:32 +0000503 /* Fetch current DCT block into workspace so we can modify it. */
504 jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
505 /* Update DC values */
506 if (block_num < last_block_column) {
507 DC3 = (int) prev_block_row[1][0];
508 DC6 = (int) buffer_ptr[1][0];
509 DC9 = (int) next_block_row[1][0];
510 }
511 /* Compute coefficient estimates per K.8.
512 * An estimate is applied only if coefficient is still zero,
513 * and is not known to be fully accurate.
514 */
515 /* AC01 */
516 if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
517 num = 36 * Q00 * (DC4 - DC6);
518 if (num >= 0) {
519 pred = (int) (((Q01<<7) + num) / (Q01<<8));
520 if (Al > 0 && pred >= (1<<Al))
521 pred = (1<<Al)-1;
522 } else {
523 pred = (int) (((Q01<<7) - num) / (Q01<<8));
524 if (Al > 0 && pred >= (1<<Al))
525 pred = (1<<Al)-1;
526 pred = -pred;
527 }
528 workspace[1] = (JCOEF) pred;
529 }
530 /* AC10 */
531 if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
532 num = 36 * Q00 * (DC2 - DC8);
533 if (num >= 0) {
534 pred = (int) (((Q10<<7) + num) / (Q10<<8));
535 if (Al > 0 && pred >= (1<<Al))
536 pred = (1<<Al)-1;
537 } else {
538 pred = (int) (((Q10<<7) - num) / (Q10<<8));
539 if (Al > 0 && pred >= (1<<Al))
540 pred = (1<<Al)-1;
541 pred = -pred;
542 }
543 workspace[8] = (JCOEF) pred;
544 }
545 /* AC20 */
546 if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
547 num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
548 if (num >= 0) {
549 pred = (int) (((Q20<<7) + num) / (Q20<<8));
550 if (Al > 0 && pred >= (1<<Al))
551 pred = (1<<Al)-1;
552 } else {
553 pred = (int) (((Q20<<7) - num) / (Q20<<8));
554 if (Al > 0 && pred >= (1<<Al))
555 pred = (1<<Al)-1;
556 pred = -pred;
557 }
558 workspace[16] = (JCOEF) pred;
559 }
560 /* AC11 */
561 if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
562 num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
563 if (num >= 0) {
564 pred = (int) (((Q11<<7) + num) / (Q11<<8));
565 if (Al > 0 && pred >= (1<<Al))
566 pred = (1<<Al)-1;
567 } else {
568 pred = (int) (((Q11<<7) - num) / (Q11<<8));
569 if (Al > 0 && pred >= (1<<Al))
570 pred = (1<<Al)-1;
571 pred = -pred;
572 }
573 workspace[9] = (JCOEF) pred;
574 }
575 /* AC02 */
576 if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
577 num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
578 if (num >= 0) {
579 pred = (int) (((Q02<<7) + num) / (Q02<<8));
580 if (Al > 0 && pred >= (1<<Al))
581 pred = (1<<Al)-1;
582 } else {
583 pred = (int) (((Q02<<7) - num) / (Q02<<8));
584 if (Al > 0 && pred >= (1<<Al))
585 pred = (1<<Al)-1;
586 pred = -pred;
587 }
588 workspace[2] = (JCOEF) pred;
589 }
590 /* OK, do the IDCT */
591 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
592 output_ptr, output_col);
593 /* Advance for next column */
594 DC1 = DC2; DC2 = DC3;
595 DC4 = DC5; DC5 = DC6;
596 DC7 = DC8; DC8 = DC9;
597 buffer_ptr++, prev_block_row++, next_block_row++;
598 output_col += compptr->_DCT_scaled_size;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000599 }
DRC49967cd2010-10-09 19:57:51 +0000600 output_ptr += compptr->_DCT_scaled_size;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000601 }
602 }
603
604 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
605 return JPEG_ROW_COMPLETED;
606 return JPEG_SCAN_COMPLETED;
607}
608
609#endif /* BLOCK_SMOOTHING_SUPPORTED */
610
611
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000612/*
613 * Initialize coefficient buffer controller.
614 */
615
Thomas G. Lane489583f1996-02-07 00:00:00 +0000616GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000617jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
618{
619 my_coef_ptr coef;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000620
621 coef = (my_coef_ptr)
622 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000623 sizeof(my_coef_controller));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000624 cinfo->coef = (struct jpeg_d_coef_controller *) coef;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000625 coef->pub.start_input_pass = start_input_pass;
626 coef->pub.start_output_pass = start_output_pass;
627#ifdef BLOCK_SMOOTHING_SUPPORTED
628 coef->coef_bits_latch = NULL;
629#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000630
631 /* Create the coefficient buffer. */
632 if (need_full_buffer) {
633#ifdef D_MULTISCAN_FILES_SUPPORTED
634 /* Allocate a full-image virtual array for each component, */
635 /* padded to a multiple of samp_factor DCT blocks in each direction. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000636 /* Note we ask for a pre-zeroed array. */
637 int ci, access_rows;
638 jpeg_component_info *compptr;
639
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000640 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
DRCe5eaf372014-05-09 18:00:32 +0000641 ci++, compptr++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000642 access_rows = compptr->v_samp_factor;
643#ifdef BLOCK_SMOOTHING_SUPPORTED
644 /* If block smoothing could be used, need a bigger window */
645 if (cinfo->progressive_mode)
DRCe5eaf372014-05-09 18:00:32 +0000646 access_rows *= 3;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000647#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000648 coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
DRCe5eaf372014-05-09 18:00:32 +0000649 ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
650 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
651 (long) compptr->h_samp_factor),
652 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
653 (long) compptr->v_samp_factor),
654 (JDIMENSION) access_rows);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000655 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000656 coef->pub.consume_data = consume_data;
657 coef->pub.decompress_data = decompress_data;
658 coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000659#else
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000660 ERREXIT(cinfo, JERR_NOT_COMPILED);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000661#endif
662 } else {
663 /* We only need a single-MCU buffer. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000664 JBLOCKROW buffer;
665 int i;
666
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000667 buffer = (JBLOCKROW)
668 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000669 D_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000670 for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000671 coef->MCU_buffer[i] = buffer + i;
672 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000673 coef->pub.consume_data = dummy_consume_data;
674 coef->pub.decompress_data = decompress_onepass;
675 coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000676 }
Pierre Ossman35c47192009-03-09 13:29:37 +0000677
678 /* Allocate the workspace buffer */
679 coef->workspace = (JCOEF *)
680 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000681 sizeof(JCOEF) * DCTSIZE2);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000682}