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