/src/ghostpdl/obj/jdcoefct.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * jdcoefct.c |
3 | | * |
4 | | * Copyright (C) 1994-1997, Thomas G. Lane. |
5 | | * Modified 2002-2020 by Guido Vollbeding. |
6 | | * 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. |
12 | | * |
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. |
16 | | */ |
17 | | |
18 | | #define JPEG_INTERNALS |
19 | | #include "jinclude.h" |
20 | | #include "jpeglib.h" |
21 | | |
22 | | |
23 | | /* Block smoothing is only applicable for progressive JPEG, so: */ |
24 | | #ifndef D_PROGRESSIVE_SUPPORTED |
25 | | #undef BLOCK_SMOOTHING_SUPPORTED |
26 | | #endif |
27 | | |
28 | | |
29 | | /* Private buffer controller object */ |
30 | | |
31 | | typedef struct { |
32 | | struct jpeg_d_coef_controller pub; /* public fields */ |
33 | | |
34 | | /* These variables keep track of the current location of the input side. */ |
35 | | /* cinfo->input_iMCU_row is also used for this. */ |
36 | | JDIMENSION MCU_ctr; /* counts MCUs processed in current row */ |
37 | | int MCU_vert_offset; /* counts MCU rows within iMCU row */ |
38 | | int MCU_rows_per_iMCU_row; /* number of such rows needed */ |
39 | | |
40 | | /* The output side's location is represented by cinfo->output_iMCU_row. */ |
41 | | |
42 | | /* In single-pass modes, it's sufficient to buffer just one MCU. |
43 | | * We append a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks, |
44 | | * and let the entropy decoder write into that workspace each time. |
45 | | * In multi-pass modes, this array points to the current MCU's blocks |
46 | | * within the virtual arrays; it is used only by the input side. |
47 | | */ |
48 | | JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU]; |
49 | | |
50 | | #ifdef D_MULTISCAN_FILES_SUPPORTED |
51 | | /* In multi-pass modes, we need a virtual block array for each component. */ |
52 | | jvirt_barray_ptr whole_image[MAX_COMPONENTS]; |
53 | | #endif |
54 | | |
55 | | #ifdef BLOCK_SMOOTHING_SUPPORTED |
56 | | /* When doing block smoothing, we latch coefficient Al values here */ |
57 | | int * coef_bits_latch; |
58 | | #define SAVED_COEFS 6 /* we save coef_bits[0..5] */ |
59 | | #endif |
60 | | |
61 | | /* Workspace for single-pass modes (omitted otherwise). */ |
62 | | JBLOCK blk_buffer[D_MAX_BLOCKS_IN_MCU]; |
63 | | } my_coef_controller; |
64 | | |
65 | | typedef my_coef_controller * my_coef_ptr; |
66 | | |
67 | | |
68 | | /* Forward declarations */ |
69 | | METHODDEF(int) decompress_onepass |
70 | | JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); |
71 | | #ifdef D_MULTISCAN_FILES_SUPPORTED |
72 | | METHODDEF(int) decompress_data |
73 | | JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); |
74 | | #endif |
75 | | #ifdef BLOCK_SMOOTHING_SUPPORTED |
76 | | LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo)); |
77 | | METHODDEF(int) decompress_smooth_data |
78 | | JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf)); |
79 | | #endif |
80 | | |
81 | | |
82 | | LOCAL(void) |
83 | | start_iMCU_row (j_decompress_ptr cinfo) |
84 | | /* Reset within-iMCU-row counters for a new row (input side) */ |
85 | 349k | { |
86 | 349k | my_coef_ptr coef = (my_coef_ptr) cinfo->coef; |
87 | | |
88 | | /* In an interleaved scan, an MCU row is the same as an iMCU row. |
89 | | * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows. |
90 | | * But at the bottom of the image, process only what's left. |
91 | | */ |
92 | 349k | if (cinfo->comps_in_scan > 1) { |
93 | 173k | coef->MCU_rows_per_iMCU_row = 1; |
94 | 176k | } else { |
95 | 176k | if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1)) |
96 | 166k | coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor; |
97 | 9.70k | else |
98 | 9.70k | coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height; |
99 | 176k | } |
100 | | |
101 | 349k | coef->MCU_ctr = 0; |
102 | 349k | coef->MCU_vert_offset = 0; |
103 | 349k | } |
104 | | |
105 | | |
106 | | /* |
107 | | * Initialize for an input processing pass. |
108 | | */ |
109 | | |
110 | | METHODDEF(void) |
111 | | start_input_pass (j_decompress_ptr cinfo) |
112 | 19.1k | { |
113 | 19.1k | cinfo->input_iMCU_row = 0; |
114 | 19.1k | start_iMCU_row(cinfo); |
115 | 19.1k | } |
116 | | |
117 | | |
118 | | /* |
119 | | * Initialize for an output processing pass. |
120 | | */ |
121 | | |
122 | | METHODDEF(void) |
123 | | start_output_pass (j_decompress_ptr cinfo) |
124 | 9.94k | { |
125 | | #ifdef BLOCK_SMOOTHING_SUPPORTED |
126 | | my_coef_ptr coef = (my_coef_ptr) cinfo->coef; |
127 | | |
128 | | /* If multipass, check to see whether to use block smoothing on this pass */ |
129 | | if (coef->pub.coef_arrays != NULL) { |
130 | | if (cinfo->do_block_smoothing && smoothing_ok(cinfo)) |
131 | | coef->pub.decompress_data = decompress_smooth_data; |
132 | | else |
133 | | coef->pub.decompress_data = decompress_data; |
134 | | } |
135 | | #endif |
136 | 9.94k | cinfo->output_iMCU_row = 0; |
137 | 9.94k | } |
138 | | |
139 | | |
140 | | /* |
141 | | * Decompress and return some data in the single-pass case. |
142 | | * Always attempts to emit one fully interleaved MCU row ("iMCU" row). |
143 | | * Input and output must run in lockstep since we have only a one-MCU buffer. |
144 | | * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. |
145 | | * |
146 | | * NB: output_buf contains a plane for each component in image, |
147 | | * which we index according to the component's SOF position. |
148 | | */ |
149 | | |
150 | | METHODDEF(int) |
151 | | decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) |
152 | 258k | { |
153 | 258k | my_coef_ptr coef = (my_coef_ptr) cinfo->coef; |
154 | 258k | JDIMENSION MCU_col_num; /* index of current MCU within row */ |
155 | 258k | JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1; |
156 | 258k | JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; |
157 | 258k | int ci, xindex, yindex, yoffset, useful_width; |
158 | 258k | JBLOCKROW blkp; |
159 | 258k | JSAMPARRAY output_ptr; |
160 | 258k | JDIMENSION start_col, output_col; |
161 | 258k | jpeg_component_info *compptr; |
162 | 258k | inverse_DCT_method_ptr inverse_DCT; |
163 | | |
164 | | /* Loop to process as much as one whole iMCU row */ |
165 | 430k | for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; |
166 | 258k | yoffset++) { |
167 | 13.2M | for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col; |
168 | 13.0M | MCU_col_num++) { |
169 | 13.0M | blkp = coef->blk_buffer; /* pointer to current DCT block within MCU */ |
170 | | /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */ |
171 | 13.0M | if (cinfo->lim_Se) /* can bypass in DC only case */ |
172 | 13.0M | MEMZERO(blkp, cinfo->blocks_in_MCU * SIZEOF(JBLOCK)); |
173 | 13.0M | if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) { |
174 | | /* Suspension forced; update state counters and exit */ |
175 | 87.5k | coef->MCU_vert_offset = yoffset; |
176 | 87.5k | coef->MCU_ctr = MCU_col_num; |
177 | 87.5k | return JPEG_SUSPENDED; |
178 | 87.5k | } |
179 | | /* Determine where data should go in output_buf and do the IDCT thing. |
180 | | * We skip dummy blocks at the right and bottom edges (but blkp gets |
181 | | * incremented past them!). |
182 | | */ |
183 | 47.3M | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
184 | 34.3M | compptr = cinfo->cur_comp_info[ci]; |
185 | | /* Don't bother to IDCT an uninteresting component. */ |
186 | 34.3M | if (! compptr->component_needed) { |
187 | 0 | blkp += compptr->MCU_blocks; |
188 | 0 | continue; |
189 | 0 | } |
190 | 34.3M | inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index]; |
191 | 34.3M | output_ptr = output_buf[compptr->component_index] + |
192 | 34.3M | yoffset * compptr->DCT_v_scaled_size; |
193 | 34.3M | useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width |
194 | 34.3M | : compptr->last_col_width; |
195 | 34.3M | start_col = MCU_col_num * compptr->MCU_sample_width; |
196 | 76.7M | for (yindex = 0; yindex < compptr->MCU_height; yindex++) { |
197 | 42.3M | if (cinfo->input_iMCU_row < last_iMCU_row || |
198 | 42.3M | yoffset + yindex < compptr->last_row_height) { |
199 | 42.2M | output_col = start_col; |
200 | 100M | for (xindex = 0; xindex < useful_width; xindex++) { |
201 | 58.0M | (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) (blkp + xindex), |
202 | 58.0M | output_ptr, output_col); |
203 | 58.0M | output_col += compptr->DCT_h_scaled_size; |
204 | 58.0M | } |
205 | 42.2M | output_ptr += compptr->DCT_v_scaled_size; |
206 | 42.2M | } |
207 | 42.3M | blkp += compptr->MCU_width; |
208 | 42.3M | } |
209 | 34.3M | } |
210 | 12.9M | } |
211 | | /* Completed an MCU row, but perhaps not an iMCU row */ |
212 | 171k | coef->MCU_ctr = 0; |
213 | 171k | } |
214 | | /* Completed the iMCU row, advance counters for next one */ |
215 | 171k | cinfo->output_iMCU_row++; |
216 | 171k | if (++(cinfo->input_iMCU_row) <= last_iMCU_row) { |
217 | 162k | start_iMCU_row(cinfo); |
218 | 162k | return JPEG_ROW_COMPLETED; |
219 | 162k | } |
220 | | /* Completed the scan */ |
221 | 8.57k | (*cinfo->inputctl->finish_input_pass) (cinfo); |
222 | 8.57k | return JPEG_SCAN_COMPLETED; |
223 | 171k | } |
224 | | |
225 | | |
226 | | /* |
227 | | * Dummy consume-input routine for single-pass operation. |
228 | | */ |
229 | | |
230 | | METHODDEF(int) |
231 | | dummy_consume_data (j_decompress_ptr cinfo) |
232 | 0 | { |
233 | 0 | return JPEG_SUSPENDED; /* Always indicate nothing was done */ |
234 | 0 | } |
235 | | |
236 | | |
237 | | #ifdef D_MULTISCAN_FILES_SUPPORTED |
238 | | |
239 | | /* |
240 | | * Consume input data and store it in the full-image coefficient buffer. |
241 | | * We read as much as one fully interleaved MCU row ("iMCU" row) per call, |
242 | | * ie, v_samp_factor block rows for each component in the scan. |
243 | | * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED. |
244 | | */ |
245 | | |
246 | | METHODDEF(int) |
247 | | consume_data (j_decompress_ptr cinfo) |
248 | 185k | { |
249 | 185k | my_coef_ptr coef = (my_coef_ptr) cinfo->coef; |
250 | 185k | JDIMENSION MCU_col_num; /* index of current MCU within row */ |
251 | 185k | int ci, xindex, yindex, yoffset; |
252 | 185k | JDIMENSION start_col; |
253 | 185k | JBLOCKARRAY blkp; |
254 | 185k | JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN]; |
255 | 185k | JBLOCKROW buffer_ptr; |
256 | 185k | jpeg_component_info *compptr; |
257 | | |
258 | | /* Align the virtual buffers for the components used in this scan. */ |
259 | 426k | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
260 | 241k | compptr = cinfo->cur_comp_info[ci]; |
261 | 241k | buffer[ci] = (*cinfo->mem->access_virt_barray) |
262 | 241k | ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index], |
263 | 241k | cinfo->input_iMCU_row * compptr->v_samp_factor, |
264 | 241k | (JDIMENSION) compptr->v_samp_factor, TRUE); |
265 | | /* Note: entropy decoder expects buffer to be zeroed, |
266 | | * but this is handled automatically by the memory manager |
267 | | * because we requested a pre-zeroed array. |
268 | | */ |
269 | 241k | } |
270 | | |
271 | | /* Loop to process one whole iMCU row */ |
272 | 373k | for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; |
273 | 195k | yoffset++) { |
274 | 13.8M | for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row; |
275 | 13.6M | MCU_col_num++) { |
276 | | /* Construct list of pointers to DCT blocks belonging to this MCU */ |
277 | 13.6M | blkp = coef->MCU_buffer; /* pointer to current DCT block within MCU */ |
278 | 31.7M | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
279 | 18.0M | compptr = cinfo->cur_comp_info[ci]; |
280 | 18.0M | start_col = MCU_col_num * compptr->MCU_width; |
281 | 40.6M | for (yindex = 0; yindex < compptr->MCU_height; yindex++) { |
282 | 22.5M | buffer_ptr = buffer[ci][yoffset + yindex] + start_col; |
283 | 22.5M | xindex = compptr->MCU_width; |
284 | 23.0M | do { |
285 | 23.0M | *blkp++ = buffer_ptr++; |
286 | 23.0M | } while (--xindex); |
287 | 22.5M | } |
288 | 18.0M | } |
289 | | /* Try to fetch the MCU. */ |
290 | 13.6M | if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) { |
291 | | /* Suspension forced; update state counters and exit */ |
292 | 6.90k | coef->MCU_vert_offset = yoffset; |
293 | 6.90k | coef->MCU_ctr = MCU_col_num; |
294 | 6.90k | return JPEG_SUSPENDED; |
295 | 6.90k | } |
296 | 13.6M | } |
297 | | /* Completed an MCU row, but perhaps not an iMCU row */ |
298 | 188k | coef->MCU_ctr = 0; |
299 | 188k | } |
300 | | /* Completed the iMCU row, advance counters for next one */ |
301 | 178k | if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) { |
302 | 167k | start_iMCU_row(cinfo); |
303 | 167k | return JPEG_ROW_COMPLETED; |
304 | 167k | } |
305 | | /* Completed the scan */ |
306 | 10.5k | (*cinfo->inputctl->finish_input_pass) (cinfo); |
307 | 10.5k | return JPEG_SCAN_COMPLETED; |
308 | 178k | } |
309 | | |
310 | | |
311 | | /* |
312 | | * 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. |
315 | | * |
316 | | * NB: output_buf contains a plane for each component in image. |
317 | | */ |
318 | | |
319 | | METHODDEF(int) |
320 | | decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) |
321 | 22.4k | { |
322 | 22.4k | my_coef_ptr coef = (my_coef_ptr) cinfo->coef; |
323 | 22.4k | JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; |
324 | 22.4k | JDIMENSION block_num; |
325 | 22.4k | int ci, block_row, block_rows; |
326 | 22.4k | JBLOCKARRAY buffer; |
327 | 22.4k | JBLOCKROW buffer_ptr; |
328 | 22.4k | JSAMPARRAY output_ptr; |
329 | 22.4k | JDIMENSION output_col; |
330 | 22.4k | jpeg_component_info *compptr; |
331 | 22.4k | inverse_DCT_method_ptr inverse_DCT; |
332 | | |
333 | | /* Force some input to be done if we are getting ahead of the input. */ |
334 | 22.4k | while (cinfo->input_scan_number < cinfo->output_scan_number || |
335 | 22.4k | (cinfo->input_scan_number == cinfo->output_scan_number && |
336 | 22.4k | cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) { |
337 | 0 | if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED) |
338 | 0 | return JPEG_SUSPENDED; |
339 | 0 | } |
340 | | |
341 | | /* OK, output from the virtual arrays. */ |
342 | 88.5k | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
343 | 66.1k | ci++, compptr++) { |
344 | | /* Don't bother to IDCT an uninteresting component. */ |
345 | 66.1k | if (! compptr->component_needed) |
346 | 0 | continue; |
347 | | /* Align the virtual buffer for this component. */ |
348 | 66.1k | buffer = (*cinfo->mem->access_virt_barray) |
349 | 66.1k | ((j_common_ptr) cinfo, coef->whole_image[ci], |
350 | 66.1k | cinfo->output_iMCU_row * compptr->v_samp_factor, |
351 | 66.1k | (JDIMENSION) compptr->v_samp_factor, FALSE); |
352 | | /* Count non-dummy DCT block rows in this iMCU row. */ |
353 | 66.1k | if (cinfo->output_iMCU_row < last_iMCU_row) |
354 | 62.5k | block_rows = compptr->v_samp_factor; |
355 | 3.52k | else { |
356 | | /* NB: can't use last_row_height here; it is input-side-dependent! */ |
357 | 3.52k | block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor); |
358 | 3.52k | if (block_rows == 0) block_rows = compptr->v_samp_factor; |
359 | 3.52k | } |
360 | 66.1k | inverse_DCT = cinfo->idct->inverse_DCT[ci]; |
361 | 66.1k | output_ptr = output_buf[ci]; |
362 | | /* Loop over all DCT blocks to be processed. */ |
363 | 135k | for (block_row = 0; block_row < block_rows; block_row++) { |
364 | 69.5k | buffer_ptr = buffer[block_row]; |
365 | 69.5k | output_col = 0; |
366 | 5.03M | for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) { |
367 | 4.96M | (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr, |
368 | 4.96M | output_ptr, output_col); |
369 | 4.96M | buffer_ptr++; |
370 | 4.96M | output_col += compptr->DCT_h_scaled_size; |
371 | 4.96M | } |
372 | 69.5k | output_ptr += compptr->DCT_v_scaled_size; |
373 | 69.5k | } |
374 | 66.1k | } |
375 | | |
376 | 22.4k | if (++(cinfo->output_iMCU_row) <= last_iMCU_row) |
377 | 21.2k | return JPEG_ROW_COMPLETED; |
378 | 1.18k | return JPEG_SCAN_COMPLETED; |
379 | 22.4k | } |
380 | | |
381 | | #endif /* D_MULTISCAN_FILES_SUPPORTED */ |
382 | | |
383 | | |
384 | | #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 | | |
394 | | /* 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 | | |
401 | | /* |
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 | | |
409 | | LOCAL(boolean) |
410 | | smoothing_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 *) (*cinfo->mem->alloc_small) |
426 | | ((j_common_ptr) cinfo, JPOOL_IMAGE, |
427 | | cinfo->num_components * (SAVED_COEFS * SIZEOF(int))); |
428 | | coef_bits_latch = coef->coef_bits_latch; |
429 | | |
430 | | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
431 | | ci++, compptr++) { |
432 | | /* All components' quantization values must already be latched. */ |
433 | | if ((qtable = compptr->quant_table) == NULL) |
434 | | return FALSE; |
435 | | /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */ |
436 | | if (qtable->quantval[0] == 0 || |
437 | | qtable->quantval[Q01_POS] == 0 || |
438 | | qtable->quantval[Q10_POS] == 0 || |
439 | | qtable->quantval[Q20_POS] == 0 || |
440 | | qtable->quantval[Q11_POS] == 0 || |
441 | | qtable->quantval[Q02_POS] == 0) |
442 | | return FALSE; |
443 | | /* DC values must be at least partly known for all components. */ |
444 | | coef_bits = cinfo->coef_bits[ci]; |
445 | | if (coef_bits[0] < 0) |
446 | | return FALSE; |
447 | | /* Block smoothing is helpful if some AC coefficients remain inaccurate. */ |
448 | | for (coefi = 1; coefi <= 5; coefi++) { |
449 | | coef_bits_latch[coefi] = coef_bits[coefi]; |
450 | | if (coef_bits[coefi] != 0) |
451 | | smoothing_useful = TRUE; |
452 | | } |
453 | | coef_bits_latch += SAVED_COEFS; |
454 | | } |
455 | | |
456 | | return smoothing_useful; |
457 | | } |
458 | | |
459 | | |
460 | | /* |
461 | | * Variant of decompress_data for use when doing block smoothing. |
462 | | */ |
463 | | |
464 | | METHODDEF(int) |
465 | | decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf) |
466 | | { |
467 | | my_coef_ptr coef = (my_coef_ptr) cinfo->coef; |
468 | | JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; |
469 | | JDIMENSION block_num, last_block_column; |
470 | | int ci, block_row, block_rows, access_rows; |
471 | | JBLOCKARRAY buffer; |
472 | | JBLOCKROW buffer_ptr, prev_block_row, next_block_row; |
473 | | JSAMPARRAY output_ptr; |
474 | | JDIMENSION output_col; |
475 | | jpeg_component_info *compptr; |
476 | | inverse_DCT_method_ptr inverse_DCT; |
477 | | boolean first_row, last_row; |
478 | | JBLOCK workspace; |
479 | | int *coef_bits; |
480 | | JQUANT_TBL *quanttbl; |
481 | | INT32 Q00,Q01,Q02,Q10,Q11,Q20, num; |
482 | | int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9; |
483 | | int Al, pred; |
484 | | |
485 | | /* Force some input to be done if we are getting ahead of the input. */ |
486 | | while (cinfo->input_scan_number <= cinfo->output_scan_number && |
487 | | ! cinfo->inputctl->eoi_reached) { |
488 | | if (cinfo->input_scan_number == cinfo->output_scan_number) { |
489 | | /* If input is working on current scan, we ordinarily want it to |
490 | | * have completed the current row. But if input scan is DC, |
491 | | * we want it to keep one row ahead so that next block row's DC |
492 | | * values are up to date. |
493 | | */ |
494 | | JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0; |
495 | | if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta) |
496 | | break; |
497 | | } |
498 | | if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED) |
499 | | return JPEG_SUSPENDED; |
500 | | } |
501 | | |
502 | | /* OK, output from the virtual arrays. */ |
503 | | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
504 | | ci++, compptr++) { |
505 | | /* Don't bother to IDCT an uninteresting component. */ |
506 | | if (! compptr->component_needed) |
507 | | continue; |
508 | | /* Count non-dummy DCT block rows in this iMCU row. */ |
509 | | if (cinfo->output_iMCU_row < last_iMCU_row) { |
510 | | block_rows = compptr->v_samp_factor; |
511 | | access_rows = block_rows * 2; /* this and next iMCU row */ |
512 | | last_row = FALSE; |
513 | | } else { |
514 | | /* NB: can't use last_row_height here; it is input-side-dependent! */ |
515 | | block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor); |
516 | | if (block_rows == 0) block_rows = compptr->v_samp_factor; |
517 | | access_rows = block_rows; /* this iMCU row only */ |
518 | | last_row = TRUE; |
519 | | } |
520 | | /* Align the virtual buffer for this component. */ |
521 | | if (cinfo->output_iMCU_row > 0) { |
522 | | access_rows += compptr->v_samp_factor; /* prior iMCU row too */ |
523 | | buffer = (*cinfo->mem->access_virt_barray) |
524 | | ((j_common_ptr) cinfo, coef->whole_image[ci], |
525 | | (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor, |
526 | | (JDIMENSION) access_rows, FALSE); |
527 | | buffer += compptr->v_samp_factor; /* point to current iMCU row */ |
528 | | first_row = FALSE; |
529 | | } else { |
530 | | buffer = (*cinfo->mem->access_virt_barray) |
531 | | ((j_common_ptr) cinfo, coef->whole_image[ci], |
532 | | (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE); |
533 | | first_row = TRUE; |
534 | | } |
535 | | /* Fetch component-dependent info */ |
536 | | coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS); |
537 | | quanttbl = compptr->quant_table; |
538 | | Q00 = quanttbl->quantval[0]; |
539 | | Q01 = quanttbl->quantval[Q01_POS]; |
540 | | Q10 = quanttbl->quantval[Q10_POS]; |
541 | | Q20 = quanttbl->quantval[Q20_POS]; |
542 | | Q11 = quanttbl->quantval[Q11_POS]; |
543 | | Q02 = quanttbl->quantval[Q02_POS]; |
544 | | inverse_DCT = cinfo->idct->inverse_DCT[ci]; |
545 | | output_ptr = output_buf[ci]; |
546 | | /* Loop over all DCT blocks to be processed. */ |
547 | | for (block_row = 0; block_row < block_rows; block_row++) { |
548 | | buffer_ptr = buffer[block_row]; |
549 | | if (first_row && block_row == 0) |
550 | | prev_block_row = buffer_ptr; |
551 | | else |
552 | | prev_block_row = buffer[block_row-1]; |
553 | | if (last_row && block_row == block_rows-1) |
554 | | next_block_row = buffer_ptr; |
555 | | else |
556 | | next_block_row = buffer[block_row+1]; |
557 | | /* We fetch the surrounding DC values using a sliding-register approach. |
558 | | * Initialize all nine here so as to do the right thing on narrow pics. |
559 | | */ |
560 | | DC1 = DC2 = DC3 = (int) prev_block_row[0][0]; |
561 | | DC4 = DC5 = DC6 = (int) buffer_ptr[0][0]; |
562 | | DC7 = DC8 = DC9 = (int) next_block_row[0][0]; |
563 | | output_col = 0; |
564 | | last_block_column = compptr->width_in_blocks - 1; |
565 | | for (block_num = 0; block_num <= last_block_column; block_num++) { |
566 | | /* Fetch current DCT block into workspace so we can modify it. */ |
567 | | jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1); |
568 | | /* Update DC values */ |
569 | | if (block_num < last_block_column) { |
570 | | DC3 = (int) prev_block_row[1][0]; |
571 | | DC6 = (int) buffer_ptr[1][0]; |
572 | | DC9 = (int) next_block_row[1][0]; |
573 | | } |
574 | | /* Compute coefficient estimates per K.8. |
575 | | * An estimate is applied only if coefficient is still zero, |
576 | | * and is not known to be fully accurate. |
577 | | */ |
578 | | /* AC01 */ |
579 | | if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) { |
580 | | num = 36 * Q00 * (DC4 - DC6); |
581 | | if (num >= 0) { |
582 | | pred = (int) (((Q01<<7) + num) / (Q01<<8)); |
583 | | if (Al > 0 && pred >= (1<<Al)) |
584 | | pred = (1<<Al)-1; |
585 | | } else { |
586 | | pred = (int) (((Q01<<7) - num) / (Q01<<8)); |
587 | | if (Al > 0 && pred >= (1<<Al)) |
588 | | pred = (1<<Al)-1; |
589 | | pred = -pred; |
590 | | } |
591 | | workspace[1] = (JCOEF) pred; |
592 | | } |
593 | | /* AC10 */ |
594 | | if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) { |
595 | | num = 36 * Q00 * (DC2 - DC8); |
596 | | if (num >= 0) { |
597 | | pred = (int) (((Q10<<7) + num) / (Q10<<8)); |
598 | | if (Al > 0 && pred >= (1<<Al)) |
599 | | pred = (1<<Al)-1; |
600 | | } else { |
601 | | pred = (int) (((Q10<<7) - num) / (Q10<<8)); |
602 | | if (Al > 0 && pred >= (1<<Al)) |
603 | | pred = (1<<Al)-1; |
604 | | pred = -pred; |
605 | | } |
606 | | workspace[8] = (JCOEF) pred; |
607 | | } |
608 | | /* AC20 */ |
609 | | if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) { |
610 | | num = 9 * Q00 * (DC2 + DC8 - 2*DC5); |
611 | | if (num >= 0) { |
612 | | pred = (int) (((Q20<<7) + num) / (Q20<<8)); |
613 | | if (Al > 0 && pred >= (1<<Al)) |
614 | | pred = (1<<Al)-1; |
615 | | } else { |
616 | | pred = (int) (((Q20<<7) - num) / (Q20<<8)); |
617 | | if (Al > 0 && pred >= (1<<Al)) |
618 | | pred = (1<<Al)-1; |
619 | | pred = -pred; |
620 | | } |
621 | | workspace[16] = (JCOEF) pred; |
622 | | } |
623 | | /* AC11 */ |
624 | | if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) { |
625 | | num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9); |
626 | | if (num >= 0) { |
627 | | pred = (int) (((Q11<<7) + num) / (Q11<<8)); |
628 | | if (Al > 0 && pred >= (1<<Al)) |
629 | | pred = (1<<Al)-1; |
630 | | } else { |
631 | | pred = (int) (((Q11<<7) - num) / (Q11<<8)); |
632 | | if (Al > 0 && pred >= (1<<Al)) |
633 | | pred = (1<<Al)-1; |
634 | | pred = -pred; |
635 | | } |
636 | | workspace[9] = (JCOEF) pred; |
637 | | } |
638 | | /* AC02 */ |
639 | | if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) { |
640 | | num = 9 * Q00 * (DC4 + DC6 - 2*DC5); |
641 | | if (num >= 0) { |
642 | | pred = (int) (((Q02<<7) + num) / (Q02<<8)); |
643 | | if (Al > 0 && pred >= (1<<Al)) |
644 | | pred = (1<<Al)-1; |
645 | | } else { |
646 | | pred = (int) (((Q02<<7) - num) / (Q02<<8)); |
647 | | if (Al > 0 && pred >= (1<<Al)) |
648 | | pred = (1<<Al)-1; |
649 | | pred = -pred; |
650 | | } |
651 | | workspace[2] = (JCOEF) pred; |
652 | | } |
653 | | /* OK, do the IDCT */ |
654 | | (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace, |
655 | | output_ptr, output_col); |
656 | | /* Advance for next column */ |
657 | | DC1 = DC2; DC2 = DC3; |
658 | | DC4 = DC5; DC5 = DC6; |
659 | | DC7 = DC8; DC8 = DC9; |
660 | | buffer_ptr++, prev_block_row++, next_block_row++; |
661 | | output_col += compptr->DCT_h_scaled_size; |
662 | | } |
663 | | output_ptr += compptr->DCT_v_scaled_size; |
664 | | } |
665 | | } |
666 | | |
667 | | if (++(cinfo->output_iMCU_row) <= last_iMCU_row) |
668 | | return JPEG_ROW_COMPLETED; |
669 | | return JPEG_SCAN_COMPLETED; |
670 | | } |
671 | | |
672 | | #endif /* BLOCK_SMOOTHING_SUPPORTED */ |
673 | | |
674 | | |
675 | | /* |
676 | | * Initialize coefficient buffer controller. |
677 | | */ |
678 | | |
679 | | GLOBAL(void) |
680 | | jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer) |
681 | 10.0k | { |
682 | 10.0k | my_coef_ptr coef; |
683 | | |
684 | 10.0k | if (need_full_buffer) { |
685 | 1.36k | #ifdef D_MULTISCAN_FILES_SUPPORTED |
686 | | /* Allocate a full-image virtual array for each component, */ |
687 | | /* padded to a multiple of samp_factor DCT blocks in each direction. */ |
688 | | /* Note we ask for a pre-zeroed array. */ |
689 | 1.36k | int ci, access_rows; |
690 | 1.36k | jpeg_component_info *compptr; |
691 | | |
692 | 1.36k | coef = (my_coef_ptr) (*cinfo->mem->alloc_small) |
693 | 1.36k | ((j_common_ptr) cinfo, JPOOL_IMAGE, |
694 | 1.36k | SIZEOF(my_coef_controller) - SIZEOF(coef->blk_buffer)); |
695 | 5.41k | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
696 | 4.05k | ci++, compptr++) { |
697 | 4.05k | access_rows = compptr->v_samp_factor; |
698 | | #ifdef BLOCK_SMOOTHING_SUPPORTED |
699 | | /* If block smoothing could be used, need a bigger window */ |
700 | | if (cinfo->progressive_mode) |
701 | | access_rows *= 3; |
702 | | #endif |
703 | 4.05k | coef->whole_image[ci] = (*cinfo->mem->request_virt_barray) |
704 | 4.05k | ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE, |
705 | 4.05k | (JDIMENSION) jround_up((long) compptr->width_in_blocks, |
706 | 4.05k | (long) compptr->h_samp_factor), |
707 | 4.05k | (JDIMENSION) jround_up((long) compptr->height_in_blocks, |
708 | 4.05k | (long) compptr->v_samp_factor), |
709 | 4.05k | (JDIMENSION) access_rows); |
710 | 4.05k | } |
711 | 1.36k | coef->pub.consume_data = consume_data; |
712 | 1.36k | coef->pub.decompress_data = decompress_data; |
713 | 1.36k | coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */ |
714 | | #else |
715 | | ERREXIT(cinfo, JERR_NOT_COMPILED); |
716 | | #endif |
717 | 8.63k | } else { |
718 | | /* We only need a single-MCU buffer. */ |
719 | 8.63k | JBLOCKARRAY blkp; |
720 | 8.63k | JBLOCKROW buffer_ptr; |
721 | 8.63k | int bi; |
722 | | |
723 | 8.63k | coef = (my_coef_ptr) (*cinfo->mem->alloc_small) |
724 | 8.63k | ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_coef_controller)); |
725 | 8.63k | buffer_ptr = coef->blk_buffer; |
726 | 8.63k | if (cinfo->lim_Se == 0) /* DC only case: want to bypass later */ |
727 | 0 | MEMZERO(buffer_ptr, SIZEOF(coef->blk_buffer)); |
728 | 8.63k | blkp = coef->MCU_buffer; |
729 | 8.63k | bi = D_MAX_BLOCKS_IN_MCU; |
730 | 552k | do { |
731 | 552k | *blkp++ = buffer_ptr++; |
732 | 552k | } while (--bi); |
733 | 8.63k | coef->pub.consume_data = dummy_consume_data; |
734 | 8.63k | coef->pub.decompress_data = decompress_onepass; |
735 | 8.63k | coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */ |
736 | 8.63k | } |
737 | | |
738 | 10.0k | coef->pub.start_input_pass = start_input_pass; |
739 | 10.0k | coef->pub.start_output_pass = start_output_pass; |
740 | | #ifdef BLOCK_SMOOTHING_SUPPORTED |
741 | | coef->coef_bits_latch = NULL; |
742 | | #endif |
743 | 10.0k | cinfo->coef = &coef->pub; |
744 | 10.0k | } |