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.
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.
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.
18 #define JPEG_INTERNALS
23 /* Block smoothing is only applicable for progressive JPEG, so: */
24 #ifndef D_PROGRESSIVE_SUPPORTED
25 #undef BLOCK_SMOOTHING_SUPPORTED
28 /* Private buffer controller object */
31 struct jpeg_d_coef_controller pub; /* public fields */
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 */
39 /* The output side's location is represented by cinfo->output_iMCU_row. */
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.
50 JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];
52 /* Temporary workspace for one MCU */
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];
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] */
67 typedef my_coef_controller * my_coef_ptr;
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));
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));
84 start_iMCU_row (j_decompress_ptr cinfo)
85 /* Reset within-iMCU-row counters for a new row (input side) */
87 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
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.
93 if (cinfo->comps_in_scan > 1) {
94 coef->MCU_rows_per_iMCU_row = 1;
96 if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
97 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
99 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
103 coef->MCU_vert_offset = 0;
108 * Initialize for an input processing pass.
112 start_input_pass (j_decompress_ptr cinfo)
114 cinfo->input_iMCU_row = 0;
115 start_iMCU_row(cinfo);
120 * Initialize for an output processing pass.
124 start_output_pass (j_decompress_ptr cinfo)
126 #ifdef BLOCK_SMOOTHING_SUPPORTED
127 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
129 /* If multipass, check to see whether to use block smoothing on this pass */
130 if (coef->pub.coef_arrays != NULL) {
131 if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
132 coef->pub.decompress_data = decompress_smooth_data;
134 coef->pub.decompress_data = decompress_data;
137 cinfo->output_iMCU_row = 0;
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.
147 * NB: output_buf contains a plane for each component in image,
148 * which we index according to the component's SOF position.
152 decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
154 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
155 JDIMENSION MCU_col_num; /* index of current MCU within row */
156 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
157 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 /* region decoding. this limits decode to the set of blocks +- 1 outside
164 * bounding blocks around the desired region to decode */
165 int blk1 = 0, blk2 = 0, skip = 0;
167 if ((cinfo->region_w > 0) && (cinfo->region_h > 0)) {
168 int bsz_w = 0, bsz_h = 0;
170 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
171 compptr = cinfo->cur_comp_info[ci];
172 if (compptr->MCU_sample_width > bsz_w)
173 bsz_w = compptr->MCU_sample_width;
174 if ((compptr->MCU_height * 8) > bsz_h)
175 bsz_h = compptr->MCU_height * 8;
177 int _region_y = (int)cinfo->region_y;
178 _region_y = (_region_y>>1)<<1;
179 if (((int)cinfo->output_scanline < (_region_y - bsz_h - 1)) ||
180 ((int)cinfo->output_scanline > (_region_y + cinfo->region_h + bsz_h)))
182 blk1 = (cinfo->region_x / bsz_w) - 1;
183 if (blk1 < 0) blk1 = 0;
184 blk2 = ((cinfo->region_x + cinfo->region_w + bsz_w - 1) / bsz_w) + 1;
185 if (blk2 < 0) blk2 = 0;
188 /* Loop to process as much as one whole iMCU row */
189 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
191 for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
193 /* see if we need to skip this MCU or not */
194 if ((cinfo->region_w > 0) && (cinfo->region_h > 0)) {
195 if (!((MCU_col_num < blk1) || (MCU_col_num > blk2) || skip))
198 /* if we are not skipping this MCU, zero it ready for huffman decode */
200 jzero_far((void FAR *) coef->MCU_buffer[0],
201 (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
202 /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
203 jzero_far((void FAR *) coef->MCU_buffer[0],
204 (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
205 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
206 /* Suspension forced; update state counters and exit */
207 coef->MCU_vert_offset = yoffset;
208 coef->MCU_ctr = MCU_col_num;
209 return JPEG_SUSPENDED;
211 /* region decoding. this limits decode to the set of blocks +- 1 outside
212 * bounding blocks around the desired region to decode */
215 /* Determine where data should go in output_buf and do the IDCT thing.
216 * We skip dummy blocks at the right and bottom edges (but blkn gets
217 * incremented past them!). Note the inner loop relies on having
218 * allocated the MCU_buffer[] blocks sequentially.
220 blkn = 0; /* index of current DCT block within MCU */
221 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
222 compptr = cinfo->cur_comp_info[ci];
223 /* Don't bother to IDCT an uninteresting component. */
224 if (! compptr->component_needed) {
225 blkn += compptr->MCU_blocks;
228 inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
229 useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
230 : compptr->last_col_width;
231 output_ptr = output_buf[compptr->component_index] +
232 yoffset * compptr->_DCT_scaled_size;
233 start_col = MCU_col_num * compptr->MCU_sample_width;
234 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
235 if (cinfo->input_iMCU_row < last_iMCU_row ||
236 yoffset+yindex < compptr->last_row_height) {
237 output_col = start_col;
238 for (xindex = 0; xindex < useful_width; xindex++) {
239 (*inverse_DCT) (cinfo, compptr,
240 (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
241 output_ptr, output_col);
242 output_col += compptr->_DCT_scaled_size;
245 blkn += compptr->MCU_width;
246 output_ptr += compptr->_DCT_scaled_size;
250 /* Completed an MCU row, but perhaps not an iMCU row */
253 /* Completed the iMCU row, advance counters for next one */
254 cinfo->output_iMCU_row++;
255 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
256 start_iMCU_row(cinfo);
257 return JPEG_ROW_COMPLETED;
259 /* Completed the scan */
260 (*cinfo->inputctl->finish_input_pass) (cinfo);
261 return JPEG_SCAN_COMPLETED;
266 * Dummy consume-input routine for single-pass operation.
270 dummy_consume_data (j_decompress_ptr cinfo)
272 return JPEG_SUSPENDED; /* Always indicate nothing was done */
276 #ifdef D_MULTISCAN_FILES_SUPPORTED
279 * Consume input data and store it in the full-image coefficient buffer.
280 * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
281 * ie, v_samp_factor block rows for each component in the scan.
282 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
286 consume_data (j_decompress_ptr cinfo)
288 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
289 JDIMENSION MCU_col_num; /* index of current MCU within row */
290 int blkn, ci, xindex, yindex, yoffset;
291 JDIMENSION start_col;
292 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
293 JBLOCKROW buffer_ptr;
294 jpeg_component_info *compptr;
296 /* Align the virtual buffers for the components used in this scan. */
297 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
298 compptr = cinfo->cur_comp_info[ci];
299 buffer[ci] = (*cinfo->mem->access_virt_barray)
300 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
301 cinfo->input_iMCU_row * compptr->v_samp_factor,
302 (JDIMENSION) compptr->v_samp_factor, TRUE);
303 /* Note: entropy decoder expects buffer to be zeroed,
304 * but this is handled automatically by the memory manager
305 * because we requested a pre-zeroed array.
309 /* Loop to process one whole iMCU row */
310 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
312 for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
314 /* Construct list of pointers to DCT blocks belonging to this MCU */
315 blkn = 0; /* index of current DCT block within MCU */
316 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
317 compptr = cinfo->cur_comp_info[ci];
318 start_col = MCU_col_num * compptr->MCU_width;
319 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
320 buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
321 for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
322 coef->MCU_buffer[blkn++] = buffer_ptr++;
326 /* Try to fetch the MCU. */
327 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
328 /* Suspension forced; update state counters and exit */
329 coef->MCU_vert_offset = yoffset;
330 coef->MCU_ctr = MCU_col_num;
331 return JPEG_SUSPENDED;
334 /* Completed an MCU row, but perhaps not an iMCU row */
337 /* Completed the iMCU row, advance counters for next one */
338 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
339 start_iMCU_row(cinfo);
340 return JPEG_ROW_COMPLETED;
342 /* Completed the scan */
343 (*cinfo->inputctl->finish_input_pass) (cinfo);
344 return JPEG_SCAN_COMPLETED;
349 * Decompress and return some data in the multi-pass case.
350 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
351 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
353 * NB: output_buf contains a plane for each component in image.
357 decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
359 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
360 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
361 JDIMENSION block_num;
362 int ci, block_row, block_rows;
364 JBLOCKROW buffer_ptr;
365 JSAMPARRAY output_ptr;
366 JDIMENSION output_col;
367 jpeg_component_info *compptr;
368 inverse_DCT_method_ptr inverse_DCT;
370 /* Force some input to be done if we are getting ahead of the input. */
371 while (cinfo->input_scan_number < cinfo->output_scan_number ||
372 (cinfo->input_scan_number == cinfo->output_scan_number &&
373 cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
374 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
375 return JPEG_SUSPENDED;
378 /* OK, output from the virtual arrays. */
379 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
381 /* Don't bother to IDCT an uninteresting component. */
382 if (! compptr->component_needed)
384 /* Align the virtual buffer for this component. */
385 buffer = (*cinfo->mem->access_virt_barray)
386 ((j_common_ptr) cinfo, coef->whole_image[ci],
387 cinfo->output_iMCU_row * compptr->v_samp_factor,
388 (JDIMENSION) compptr->v_samp_factor, FALSE);
389 /* Count non-dummy DCT block rows in this iMCU row. */
390 if (cinfo->output_iMCU_row < last_iMCU_row)
391 block_rows = compptr->v_samp_factor;
393 /* NB: can't use last_row_height here; it is input-side-dependent! */
394 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
395 if (block_rows == 0) block_rows = compptr->v_samp_factor;
397 inverse_DCT = cinfo->idct->inverse_DCT[ci];
398 output_ptr = output_buf[ci];
399 /* Loop over all DCT blocks to be processed. */
400 for (block_row = 0; block_row < block_rows; block_row++) {
401 buffer_ptr = buffer[block_row];
403 for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
404 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
405 output_ptr, output_col);
407 output_col += compptr->_DCT_scaled_size;
409 output_ptr += compptr->_DCT_scaled_size;
413 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
414 return JPEG_ROW_COMPLETED;
415 return JPEG_SCAN_COMPLETED;
418 #endif /* D_MULTISCAN_FILES_SUPPORTED */
421 #ifdef BLOCK_SMOOTHING_SUPPORTED
424 * This code applies interblock smoothing as described by section K.8
425 * of the JPEG standard: the first 5 AC coefficients are estimated from
426 * the DC values of a DCT block and its 8 neighboring blocks.
427 * We apply smoothing only for progressive JPEG decoding, and only if
428 * the coefficients it can estimate are not yet known to full precision.
431 /* Natural-order array positions of the first 5 zigzag-order coefficients */
439 * Determine whether block smoothing is applicable and safe.
440 * We also latch the current states of the coef_bits[] entries for the
441 * AC coefficients; otherwise, if the input side of the decompressor
442 * advances into a new scan, we might think the coefficients are known
443 * more accurately than they really are.
447 smoothing_ok (j_decompress_ptr cinfo)
449 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
450 boolean smoothing_useful = FALSE;
452 jpeg_component_info *compptr;
455 int * coef_bits_latch;
457 if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
460 /* Allocate latch area if not already done */
461 if (coef->coef_bits_latch == NULL)
462 coef->coef_bits_latch = (int *)
463 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
464 cinfo->num_components *
465 (SAVED_COEFS * SIZEOF(int)));
466 coef_bits_latch = coef->coef_bits_latch;
468 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
470 /* All components' quantization values must already be latched. */
471 if ((qtable = compptr->quant_table) == NULL)
473 /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
474 if (qtable->quantval[0] == 0 ||
475 qtable->quantval[Q01_POS] == 0 ||
476 qtable->quantval[Q10_POS] == 0 ||
477 qtable->quantval[Q20_POS] == 0 ||
478 qtable->quantval[Q11_POS] == 0 ||
479 qtable->quantval[Q02_POS] == 0)
481 /* DC values must be at least partly known for all components. */
482 coef_bits = cinfo->coef_bits[ci];
483 if (coef_bits[0] < 0)
485 /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
486 for (coefi = 1; coefi <= 5; coefi++) {
487 coef_bits_latch[coefi] = coef_bits[coefi];
488 if (coef_bits[coefi] != 0)
489 smoothing_useful = TRUE;
491 coef_bits_latch += SAVED_COEFS;
494 return smoothing_useful;
499 * Variant of decompress_data for use when doing block smoothing.
503 decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
505 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
506 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
507 JDIMENSION block_num, last_block_column;
508 int ci, block_row, block_rows, access_rows;
510 JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
511 JSAMPARRAY output_ptr;
512 JDIMENSION output_col;
513 jpeg_component_info *compptr;
514 inverse_DCT_method_ptr inverse_DCT;
515 boolean first_row, last_row;
518 JQUANT_TBL *quanttbl;
519 INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
520 int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
523 /* Keep a local variable to avoid looking it up more than once */
524 workspace = coef->workspace;
526 /* Force some input to be done if we are getting ahead of the input. */
527 while (cinfo->input_scan_number <= cinfo->output_scan_number &&
528 ! cinfo->inputctl->eoi_reached) {
529 if (cinfo->input_scan_number == cinfo->output_scan_number) {
530 /* If input is working on current scan, we ordinarily want it to
531 * have completed the current row. But if input scan is DC,
532 * we want it to keep one row ahead so that next block row's DC
533 * values are up to date.
535 JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
536 if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
539 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
540 return JPEG_SUSPENDED;
543 /* OK, output from the virtual arrays. */
544 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
546 /* Don't bother to IDCT an uninteresting component. */
547 if (! compptr->component_needed)
549 /* Count non-dummy DCT block rows in this iMCU row. */
550 if (cinfo->output_iMCU_row < last_iMCU_row) {
551 block_rows = compptr->v_samp_factor;
552 access_rows = block_rows * 2; /* this and next iMCU row */
555 /* NB: can't use last_row_height here; it is input-side-dependent! */
556 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
557 if (block_rows == 0) block_rows = compptr->v_samp_factor;
558 access_rows = block_rows; /* this iMCU row only */
561 /* Align the virtual buffer for this component. */
562 if (cinfo->output_iMCU_row > 0) {
563 access_rows += compptr->v_samp_factor; /* prior iMCU row too */
564 buffer = (*cinfo->mem->access_virt_barray)
565 ((j_common_ptr) cinfo, coef->whole_image[ci],
566 (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
567 (JDIMENSION) access_rows, FALSE);
568 buffer += compptr->v_samp_factor; /* point to current iMCU row */
571 buffer = (*cinfo->mem->access_virt_barray)
572 ((j_common_ptr) cinfo, coef->whole_image[ci],
573 (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
576 /* Fetch component-dependent info */
577 coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
578 quanttbl = compptr->quant_table;
579 Q00 = quanttbl->quantval[0];
580 Q01 = quanttbl->quantval[Q01_POS];
581 Q10 = quanttbl->quantval[Q10_POS];
582 Q20 = quanttbl->quantval[Q20_POS];
583 Q11 = quanttbl->quantval[Q11_POS];
584 Q02 = quanttbl->quantval[Q02_POS];
585 inverse_DCT = cinfo->idct->inverse_DCT[ci];
586 output_ptr = output_buf[ci];
587 /* Loop over all DCT blocks to be processed. */
588 for (block_row = 0; block_row < block_rows; block_row++) {
589 buffer_ptr = buffer[block_row];
590 if (first_row && block_row == 0)
591 prev_block_row = buffer_ptr;
593 prev_block_row = buffer[block_row-1];
594 if (last_row && block_row == block_rows-1)
595 next_block_row = buffer_ptr;
597 next_block_row = buffer[block_row+1];
598 /* We fetch the surrounding DC values using a sliding-register approach.
599 * Initialize all nine here so as to do the right thing on narrow pics.
601 DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
602 DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
603 DC7 = DC8 = DC9 = (int) next_block_row[0][0];
605 last_block_column = compptr->width_in_blocks - 1;
606 for (block_num = 0; block_num <= last_block_column; block_num++) {
607 /* Fetch current DCT block into workspace so we can modify it. */
608 jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
609 /* Update DC values */
610 if (block_num < last_block_column) {
611 DC3 = (int) prev_block_row[1][0];
612 DC6 = (int) buffer_ptr[1][0];
613 DC9 = (int) next_block_row[1][0];
615 /* Compute coefficient estimates per K.8.
616 * An estimate is applied only if coefficient is still zero,
617 * and is not known to be fully accurate.
620 if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
621 num = 36 * Q00 * (DC4 - DC6);
623 pred = (int) (((Q01<<7) + num) / (Q01<<8));
624 if (Al > 0 && pred >= (1<<Al))
627 pred = (int) (((Q01<<7) - num) / (Q01<<8));
628 if (Al > 0 && pred >= (1<<Al))
632 workspace[1] = (JCOEF) pred;
635 if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
636 num = 36 * Q00 * (DC2 - DC8);
638 pred = (int) (((Q10<<7) + num) / (Q10<<8));
639 if (Al > 0 && pred >= (1<<Al))
642 pred = (int) (((Q10<<7) - num) / (Q10<<8));
643 if (Al > 0 && pred >= (1<<Al))
647 workspace[8] = (JCOEF) pred;
650 if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
651 num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
653 pred = (int) (((Q20<<7) + num) / (Q20<<8));
654 if (Al > 0 && pred >= (1<<Al))
657 pred = (int) (((Q20<<7) - num) / (Q20<<8));
658 if (Al > 0 && pred >= (1<<Al))
662 workspace[16] = (JCOEF) pred;
665 if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
666 num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
668 pred = (int) (((Q11<<7) + num) / (Q11<<8));
669 if (Al > 0 && pred >= (1<<Al))
672 pred = (int) (((Q11<<7) - num) / (Q11<<8));
673 if (Al > 0 && pred >= (1<<Al))
677 workspace[9] = (JCOEF) pred;
680 if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
681 num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
683 pred = (int) (((Q02<<7) + num) / (Q02<<8));
684 if (Al > 0 && pred >= (1<<Al))
687 pred = (int) (((Q02<<7) - num) / (Q02<<8));
688 if (Al > 0 && pred >= (1<<Al))
692 workspace[2] = (JCOEF) pred;
694 /* OK, do the IDCT */
695 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
696 output_ptr, output_col);
697 /* Advance for next column */
698 DC1 = DC2; DC2 = DC3;
699 DC4 = DC5; DC5 = DC6;
700 DC7 = DC8; DC8 = DC9;
701 buffer_ptr++, prev_block_row++, next_block_row++;
702 output_col += compptr->_DCT_scaled_size;
704 output_ptr += compptr->_DCT_scaled_size;
708 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
709 return JPEG_ROW_COMPLETED;
710 return JPEG_SCAN_COMPLETED;
713 #endif /* BLOCK_SMOOTHING_SUPPORTED */
717 * Initialize coefficient buffer controller.
721 jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
726 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
727 SIZEOF(my_coef_controller));
728 cinfo->coef = (struct jpeg_d_coef_controller *) coef;
729 coef->pub.start_input_pass = start_input_pass;
730 coef->pub.start_output_pass = start_output_pass;
731 #ifdef BLOCK_SMOOTHING_SUPPORTED
732 coef->coef_bits_latch = NULL;
735 /* Create the coefficient buffer. */
736 if (need_full_buffer) {
737 #ifdef D_MULTISCAN_FILES_SUPPORTED
738 /* Allocate a full-image virtual array for each component, */
739 /* padded to a multiple of samp_factor DCT blocks in each direction. */
740 /* Note we ask for a pre-zeroed array. */
742 jpeg_component_info *compptr;
744 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
746 access_rows = compptr->v_samp_factor;
747 #ifdef BLOCK_SMOOTHING_SUPPORTED
748 /* If block smoothing could be used, need a bigger window */
749 if (cinfo->progressive_mode)
752 coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
753 ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
754 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
755 (long) compptr->h_samp_factor),
756 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
757 (long) compptr->v_samp_factor),
758 (JDIMENSION) access_rows);
760 coef->pub.consume_data = consume_data;
761 coef->pub.decompress_data = decompress_data;
762 coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
764 ERREXIT(cinfo, JERR_NOT_COMPILED);
767 /* We only need a single-MCU buffer. */
772 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
773 D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
774 for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
775 coef->MCU_buffer[i] = buffer + i;
777 coef->pub.consume_data = dummy_consume_data;
778 coef->pub.decompress_data = decompress_onepass;
779 coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
782 /* Allocate the workspace buffer */
783 coef->workspace = (JCOEF *)
784 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
785 SIZEOF(JCOEF) * DCTSIZE2);