4 * This file was part of the Independent JPEG Group's software:
5 * Copyright (C) 1994-1997, Thomas G. Lane.
6 * libjpeg-turbo Modifications:
7 * Copyright (C) 2010, D. R. Commander.
8 * For conditions of distribution and use, see the accompanying README file.
10 * This file contains the coefficient buffer controller for decompression.
11 * This controller is the top level of the JPEG decompressor proper.
12 * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
14 * In buffered-image mode, this controller is the interface between
15 * input-oriented processing and output-oriented processing.
16 * Also, the input side (only) is used when reading a file for transcoding.
19 #define JPEG_INTERNALS
24 /* Block smoothing is only applicable for progressive JPEG, so: */
25 #ifndef D_PROGRESSIVE_SUPPORTED
26 #undef BLOCK_SMOOTHING_SUPPORTED
29 /* Private buffer controller object */
32 struct jpeg_d_coef_controller pub; /* public fields */
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 */
40 /* The output side's location is represented by cinfo->output_iMCU_row. */
42 /* In single-pass modes, it's sufficient to buffer just one MCU.
43 * We allocate 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.
48 JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];
50 /* Temporary workspace for one MCU */
53 #ifdef D_MULTISCAN_FILES_SUPPORTED
54 /* In multi-pass modes, we need a virtual block array for each component. */
55 jvirt_barray_ptr whole_image[MAX_COMPONENTS];
58 #ifdef BLOCK_SMOOTHING_SUPPORTED
59 /* When doing block smoothing, we latch coefficient Al values here */
60 int * coef_bits_latch;
61 #define SAVED_COEFS 6 /* we save coef_bits[0..5] */
65 typedef my_coef_controller * my_coef_ptr;
67 /* Forward declarations */
68 METHODDEF(int) decompress_onepass
69 (j_decompress_ptr cinfo, JSAMPIMAGE output_buf);
70 #ifdef D_MULTISCAN_FILES_SUPPORTED
71 METHODDEF(int) decompress_data
72 (j_decompress_ptr cinfo, JSAMPIMAGE output_buf);
74 #ifdef BLOCK_SMOOTHING_SUPPORTED
75 LOCAL(boolean) smoothing_ok (j_decompress_ptr cinfo);
76 METHODDEF(int) decompress_smooth_data
77 (j_decompress_ptr cinfo, JSAMPIMAGE output_buf);
82 start_iMCU_row (j_decompress_ptr cinfo)
83 /* Reset within-iMCU-row counters for a new row (input side) */
85 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
87 /* In an interleaved scan, an MCU row is the same as an iMCU row.
88 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
89 * But at the bottom of the image, process only what's left.
91 if (cinfo->comps_in_scan > 1) {
92 coef->MCU_rows_per_iMCU_row = 1;
94 if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
95 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
97 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
101 coef->MCU_vert_offset = 0;
106 * Initialize for an input processing pass.
110 start_input_pass (j_decompress_ptr cinfo)
112 cinfo->input_iMCU_row = 0;
113 start_iMCU_row(cinfo);
118 * Initialize for an output processing pass.
122 start_output_pass (j_decompress_ptr cinfo)
124 #ifdef BLOCK_SMOOTHING_SUPPORTED
125 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
127 /* If multipass, check to see whether to use block smoothing on this pass */
128 if (coef->pub.coef_arrays != NULL) {
129 if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
130 coef->pub.decompress_data = decompress_smooth_data;
132 coef->pub.decompress_data = decompress_data;
135 cinfo->output_iMCU_row = 0;
140 * Decompress and return some data in the single-pass case.
141 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
142 * Input and output must run in lockstep since we have only a one-MCU buffer.
143 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
145 * NB: output_buf contains a plane for each component in image,
146 * which we index according to the component's SOF position.
150 decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
152 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
153 JDIMENSION MCU_col_num; /* index of current MCU within row */
154 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
155 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
156 int blkn, ci, xindex, yindex, yoffset, useful_width;
157 JSAMPARRAY output_ptr;
158 JDIMENSION start_col, output_col;
159 jpeg_component_info *compptr;
160 inverse_DCT_method_ptr inverse_DCT;
162 /* region decoding. this limits decode to the set of blocks +- 1 outside
163 * bounding blocks around the desired region to decode */
164 int blk1 = 0, blk2 = 0, skip = 0;
166 if ((cinfo->region_w > 0) && (cinfo->region_h > 0)) {
167 int bsz_w = 0, bsz_h = 0;
169 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
170 compptr = cinfo->cur_comp_info[ci];
171 if (compptr->MCU_sample_width > bsz_w)
172 bsz_w = compptr->MCU_sample_width;
173 if ((compptr->MCU_height * 8) > bsz_h)
174 bsz_h = compptr->MCU_height * 8;
176 int _region_y = (int)cinfo->region_y;
177 _region_y = (_region_y>>1)<<1;
178 if (((int)cinfo->output_scanline < (_region_y - bsz_h - 1)) ||
179 ((int)cinfo->output_scanline > (_region_y + cinfo->region_h + bsz_h)))
182 blk1 = (cinfo->region_x / bsz_w) - 1;
186 blk2 = ((cinfo->region_x + cinfo->region_w + bsz_w - 1) / bsz_w) + 1;
192 /* Loop to process as much as one whole iMCU row */
193 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
195 for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
198 /* see if we need to skip this MCU or not */
199 if ((cinfo->region_w > 0) && (cinfo->region_h > 0)) {
200 if (!((MCU_col_num < blk1) || (MCU_col_num > blk2) || skip))
203 /* if we are not skipping this MCU, zero it ready for huffman decode */
205 jzero_far((void FAR *) coef->MCU_buffer[0],
206 (size_t) (cinfo->blocks_in_MCU * sizeof(JBLOCK)));
208 /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
210 jzero_far((void FAR *) coef->MCU_buffer[0],
211 (size_t) (cinfo->blocks_in_MCU * sizeof(JBLOCK)));
213 jzero_far((void *) coef->MCU_buffer[0],
214 (size_t) (cinfo->blocks_in_MCU * sizeof(JBLOCK)));
216 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
217 /* Suspension forced; update state counters and exit */
218 coef->MCU_vert_offset = yoffset;
219 coef->MCU_ctr = MCU_col_num;
220 return JPEG_SUSPENDED;
223 /* region decoding. this limits decode to the set of blocks +- 1 outside
224 * bounding blocks around the desired region to decode */
228 /* Determine where data should go in output_buf and do the IDCT thing.
229 * We skip dummy blocks at the right and bottom edges (but blkn gets
230 * incremented past them!). Note the inner loop relies on having
231 * allocated the MCU_buffer[] blocks sequentially.
233 blkn = 0; /* index of current DCT block within MCU */
234 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
235 compptr = cinfo->cur_comp_info[ci];
236 /* Don't bother to IDCT an uninteresting component. */
237 if (! compptr->component_needed) {
238 blkn += compptr->MCU_blocks;
241 inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
242 useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
243 : compptr->last_col_width;
244 output_ptr = output_buf[compptr->component_index] +
245 yoffset * compptr->_DCT_scaled_size;
246 start_col = MCU_col_num * compptr->MCU_sample_width;
247 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
248 if (cinfo->input_iMCU_row < last_iMCU_row ||
249 yoffset+yindex < compptr->last_row_height) {
250 output_col = start_col;
251 for (xindex = 0; xindex < useful_width; xindex++) {
252 (*inverse_DCT) (cinfo, compptr,
253 (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
254 output_ptr, output_col);
255 output_col += compptr->_DCT_scaled_size;
258 blkn += compptr->MCU_width;
259 output_ptr += compptr->_DCT_scaled_size;
263 /* Completed an MCU row, but perhaps not an iMCU row */
266 /* Completed the iMCU row, advance counters for next one */
267 cinfo->output_iMCU_row++;
268 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
269 start_iMCU_row(cinfo);
270 return JPEG_ROW_COMPLETED;
272 /* Completed the scan */
273 (*cinfo->inputctl->finish_input_pass) (cinfo);
274 return JPEG_SCAN_COMPLETED;
279 * Dummy consume-input routine for single-pass operation.
283 dummy_consume_data (j_decompress_ptr cinfo)
285 return JPEG_SUSPENDED; /* Always indicate nothing was done */
289 #ifdef D_MULTISCAN_FILES_SUPPORTED
292 * Consume input data and store it in the full-image coefficient buffer.
293 * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
294 * ie, v_samp_factor block rows for each component in the scan.
295 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
299 consume_data (j_decompress_ptr cinfo)
301 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
302 JDIMENSION MCU_col_num; /* index of current MCU within row */
303 int blkn, ci, xindex, yindex, yoffset;
304 JDIMENSION start_col;
305 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
306 JBLOCKROW buffer_ptr;
307 jpeg_component_info *compptr;
309 /* Align the virtual buffers for the components used in this scan. */
310 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
311 compptr = cinfo->cur_comp_info[ci];
312 buffer[ci] = (*cinfo->mem->access_virt_barray)
313 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
314 cinfo->input_iMCU_row * compptr->v_samp_factor,
315 (JDIMENSION) compptr->v_samp_factor, TRUE);
316 /* Note: entropy decoder expects buffer to be zeroed,
317 * but this is handled automatically by the memory manager
318 * because we requested a pre-zeroed array.
322 /* Loop to process one whole iMCU row */
323 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
325 for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
327 /* Construct list of pointers to DCT blocks belonging to this MCU */
328 blkn = 0; /* index of current DCT block within MCU */
329 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
330 compptr = cinfo->cur_comp_info[ci];
331 start_col = MCU_col_num * compptr->MCU_width;
332 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
333 buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
334 for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
335 coef->MCU_buffer[blkn++] = buffer_ptr++;
339 /* Try to fetch the MCU. */
340 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
341 /* Suspension forced; update state counters and exit */
342 coef->MCU_vert_offset = yoffset;
343 coef->MCU_ctr = MCU_col_num;
344 return JPEG_SUSPENDED;
347 /* Completed an MCU row, but perhaps not an iMCU row */
350 /* Completed the iMCU row, advance counters for next one */
351 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
352 start_iMCU_row(cinfo);
353 return JPEG_ROW_COMPLETED;
355 /* Completed the scan */
356 (*cinfo->inputctl->finish_input_pass) (cinfo);
357 return JPEG_SCAN_COMPLETED;
362 * Decompress and return some data in the multi-pass case.
363 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
364 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
366 * NB: output_buf contains a plane for each component in image.
370 decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
372 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
373 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
374 JDIMENSION block_num;
375 int ci, block_row, block_rows;
377 JBLOCKROW buffer_ptr;
378 JSAMPARRAY output_ptr;
379 JDIMENSION output_col;
380 jpeg_component_info *compptr;
381 inverse_DCT_method_ptr inverse_DCT;
383 /* Force some input to be done if we are getting ahead of the input. */
384 while (cinfo->input_scan_number < cinfo->output_scan_number ||
385 (cinfo->input_scan_number == cinfo->output_scan_number &&
386 cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
387 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
388 return JPEG_SUSPENDED;
391 /* OK, output from the virtual arrays. */
392 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
394 /* Don't bother to IDCT an uninteresting component. */
395 if (! compptr->component_needed)
397 /* Align the virtual buffer for this component. */
398 buffer = (*cinfo->mem->access_virt_barray)
399 ((j_common_ptr) cinfo, coef->whole_image[ci],
400 cinfo->output_iMCU_row * compptr->v_samp_factor,
401 (JDIMENSION) compptr->v_samp_factor, FALSE);
402 /* Count non-dummy DCT block rows in this iMCU row. */
403 if (cinfo->output_iMCU_row < last_iMCU_row)
404 block_rows = compptr->v_samp_factor;
406 /* NB: can't use last_row_height here; it is input-side-dependent! */
407 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
408 if (block_rows == 0) block_rows = compptr->v_samp_factor;
410 inverse_DCT = cinfo->idct->inverse_DCT[ci];
411 output_ptr = output_buf[ci];
412 /* Loop over all DCT blocks to be processed. */
413 for (block_row = 0; block_row < block_rows; block_row++) {
414 buffer_ptr = buffer[block_row];
416 for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
417 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
418 output_ptr, output_col);
420 output_col += compptr->_DCT_scaled_size;
422 output_ptr += compptr->_DCT_scaled_size;
426 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
427 return JPEG_ROW_COMPLETED;
428 return JPEG_SCAN_COMPLETED;
431 #endif /* D_MULTISCAN_FILES_SUPPORTED */
434 #ifdef BLOCK_SMOOTHING_SUPPORTED
437 * This code applies interblock smoothing as described by section K.8
438 * of the JPEG standard: the first 5 AC coefficients are estimated from
439 * the DC values of a DCT block and its 8 neighboring blocks.
440 * We apply smoothing only for progressive JPEG decoding, and only if
441 * the coefficients it can estimate are not yet known to full precision.
444 /* Natural-order array positions of the first 5 zigzag-order coefficients */
452 * Determine whether block smoothing is applicable and safe.
453 * We also latch the current states of the coef_bits[] entries for the
454 * AC coefficients; otherwise, if the input side of the decompressor
455 * advances into a new scan, we might think the coefficients are known
456 * more accurately than they really are.
460 smoothing_ok (j_decompress_ptr cinfo)
462 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
463 boolean smoothing_useful = FALSE;
465 jpeg_component_info *compptr;
468 int * coef_bits_latch;
470 if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
473 /* Allocate latch area if not already done */
474 if (coef->coef_bits_latch == NULL)
475 coef->coef_bits_latch = (int *)
476 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
477 cinfo->num_components *
478 (SAVED_COEFS * sizeof(int)));
479 coef_bits_latch = coef->coef_bits_latch;
481 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
483 /* All components' quantization values must already be latched. */
484 if ((qtable = compptr->quant_table) == NULL)
486 /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
487 if (qtable->quantval[0] == 0 ||
488 qtable->quantval[Q01_POS] == 0 ||
489 qtable->quantval[Q10_POS] == 0 ||
490 qtable->quantval[Q20_POS] == 0 ||
491 qtable->quantval[Q11_POS] == 0 ||
492 qtable->quantval[Q02_POS] == 0)
494 /* DC values must be at least partly known for all components. */
495 coef_bits = cinfo->coef_bits[ci];
496 if (coef_bits[0] < 0)
498 /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
499 for (coefi = 1; coefi <= 5; coefi++) {
500 coef_bits_latch[coefi] = coef_bits[coefi];
501 if (coef_bits[coefi] != 0)
502 smoothing_useful = TRUE;
504 coef_bits_latch += SAVED_COEFS;
507 return smoothing_useful;
512 * Variant of decompress_data for use when doing block smoothing.
516 decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
518 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
519 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
520 JDIMENSION block_num, last_block_column;
521 int ci, block_row, block_rows, access_rows;
523 JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
524 JSAMPARRAY output_ptr;
525 JDIMENSION output_col;
526 jpeg_component_info *compptr;
527 inverse_DCT_method_ptr inverse_DCT;
528 boolean first_row, last_row;
531 JQUANT_TBL *quanttbl;
532 INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
533 int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
536 /* Keep a local variable to avoid looking it up more than once */
537 workspace = coef->workspace;
539 /* Force some input to be done if we are getting ahead of the input. */
540 while (cinfo->input_scan_number <= cinfo->output_scan_number &&
541 ! cinfo->inputctl->eoi_reached) {
542 if (cinfo->input_scan_number == cinfo->output_scan_number) {
543 /* If input is working on current scan, we ordinarily want it to
544 * have completed the current row. But if input scan is DC,
545 * we want it to keep one row ahead so that next block row's DC
546 * values are up to date.
548 JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
549 if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
552 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
553 return JPEG_SUSPENDED;
556 /* OK, output from the virtual arrays. */
557 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
559 /* Don't bother to IDCT an uninteresting component. */
560 if (! compptr->component_needed)
562 /* Count non-dummy DCT block rows in this iMCU row. */
563 if (cinfo->output_iMCU_row < last_iMCU_row) {
564 block_rows = compptr->v_samp_factor;
565 access_rows = block_rows * 2; /* this and next iMCU row */
568 /* NB: can't use last_row_height here; it is input-side-dependent! */
569 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
570 if (block_rows == 0) block_rows = compptr->v_samp_factor;
571 access_rows = block_rows; /* this iMCU row only */
574 /* Align the virtual buffer for this component. */
575 if (cinfo->output_iMCU_row > 0) {
576 access_rows += compptr->v_samp_factor; /* prior iMCU row too */
577 buffer = (*cinfo->mem->access_virt_barray)
578 ((j_common_ptr) cinfo, coef->whole_image[ci],
579 (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
580 (JDIMENSION) access_rows, FALSE);
581 buffer += compptr->v_samp_factor; /* point to current iMCU row */
584 buffer = (*cinfo->mem->access_virt_barray)
585 ((j_common_ptr) cinfo, coef->whole_image[ci],
586 (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
589 /* Fetch component-dependent info */
590 coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
591 quanttbl = compptr->quant_table;
592 Q00 = quanttbl->quantval[0];
593 Q01 = quanttbl->quantval[Q01_POS];
594 Q10 = quanttbl->quantval[Q10_POS];
595 Q20 = quanttbl->quantval[Q20_POS];
596 Q11 = quanttbl->quantval[Q11_POS];
597 Q02 = quanttbl->quantval[Q02_POS];
598 inverse_DCT = cinfo->idct->inverse_DCT[ci];
599 output_ptr = output_buf[ci];
600 /* Loop over all DCT blocks to be processed. */
601 for (block_row = 0; block_row < block_rows; block_row++) {
602 buffer_ptr = buffer[block_row];
603 if (first_row && block_row == 0)
604 prev_block_row = buffer_ptr;
606 prev_block_row = buffer[block_row-1];
607 if (last_row && block_row == block_rows-1)
608 next_block_row = buffer_ptr;
610 next_block_row = buffer[block_row+1];
611 /* We fetch the surrounding DC values using a sliding-register approach.
612 * Initialize all nine here so as to do the right thing on narrow pics.
614 DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
615 DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
616 DC7 = DC8 = DC9 = (int) next_block_row[0][0];
618 last_block_column = compptr->width_in_blocks - 1;
619 for (block_num = 0; block_num <= last_block_column; block_num++) {
620 /* Fetch current DCT block into workspace so we can modify it. */
621 jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
622 /* Update DC values */
623 if (block_num < last_block_column) {
624 DC3 = (int) prev_block_row[1][0];
625 DC6 = (int) buffer_ptr[1][0];
626 DC9 = (int) next_block_row[1][0];
628 /* Compute coefficient estimates per K.8.
629 * An estimate is applied only if coefficient is still zero,
630 * and is not known to be fully accurate.
633 if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
634 num = 36 * Q00 * (DC4 - DC6);
636 pred = (int) (((Q01<<7) + num) / (Q01<<8));
637 if (Al > 0 && pred >= (1<<Al))
640 pred = (int) (((Q01<<7) - num) / (Q01<<8));
641 if (Al > 0 && pred >= (1<<Al))
645 workspace[1] = (JCOEF) pred;
648 if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
649 num = 36 * Q00 * (DC2 - DC8);
651 pred = (int) (((Q10<<7) + num) / (Q10<<8));
652 if (Al > 0 && pred >= (1<<Al))
655 pred = (int) (((Q10<<7) - num) / (Q10<<8));
656 if (Al > 0 && pred >= (1<<Al))
660 workspace[8] = (JCOEF) pred;
663 if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
664 num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
666 pred = (int) (((Q20<<7) + num) / (Q20<<8));
667 if (Al > 0 && pred >= (1<<Al))
670 pred = (int) (((Q20<<7) - num) / (Q20<<8));
671 if (Al > 0 && pred >= (1<<Al))
675 workspace[16] = (JCOEF) pred;
678 if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
679 num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
681 pred = (int) (((Q11<<7) + num) / (Q11<<8));
682 if (Al > 0 && pred >= (1<<Al))
685 pred = (int) (((Q11<<7) - num) / (Q11<<8));
686 if (Al > 0 && pred >= (1<<Al))
690 workspace[9] = (JCOEF) pred;
693 if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
694 num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
696 pred = (int) (((Q02<<7) + num) / (Q02<<8));
697 if (Al > 0 && pred >= (1<<Al))
700 pred = (int) (((Q02<<7) - num) / (Q02<<8));
701 if (Al > 0 && pred >= (1<<Al))
705 workspace[2] = (JCOEF) pred;
707 /* OK, do the IDCT */
708 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
709 output_ptr, output_col);
710 /* Advance for next column */
711 DC1 = DC2; DC2 = DC3;
712 DC4 = DC5; DC5 = DC6;
713 DC7 = DC8; DC8 = DC9;
714 buffer_ptr++, prev_block_row++, next_block_row++;
715 output_col += compptr->_DCT_scaled_size;
717 output_ptr += compptr->_DCT_scaled_size;
721 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
722 return JPEG_ROW_COMPLETED;
723 return JPEG_SCAN_COMPLETED;
726 #endif /* BLOCK_SMOOTHING_SUPPORTED */
730 * Initialize coefficient buffer controller.
734 jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
739 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
740 sizeof(my_coef_controller));
741 cinfo->coef = (struct jpeg_d_coef_controller *) coef;
742 coef->pub.start_input_pass = start_input_pass;
743 coef->pub.start_output_pass = start_output_pass;
744 #ifdef BLOCK_SMOOTHING_SUPPORTED
745 coef->coef_bits_latch = NULL;
748 /* Create the coefficient buffer. */
749 if (need_full_buffer) {
750 #ifdef D_MULTISCAN_FILES_SUPPORTED
751 /* Allocate a full-image virtual array for each component, */
752 /* padded to a multiple of samp_factor DCT blocks in each direction. */
753 /* Note we ask for a pre-zeroed array. */
755 jpeg_component_info *compptr;
757 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
759 access_rows = compptr->v_samp_factor;
760 #ifdef BLOCK_SMOOTHING_SUPPORTED
761 /* If block smoothing could be used, need a bigger window */
762 if (cinfo->progressive_mode)
765 coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
766 ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
767 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
768 (long) compptr->h_samp_factor),
769 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
770 (long) compptr->v_samp_factor),
771 (JDIMENSION) access_rows);
773 coef->pub.consume_data = consume_data;
774 coef->pub.decompress_data = decompress_data;
775 coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
777 ERREXIT(cinfo, JERR_NOT_COMPILED);
780 /* We only need a single-MCU buffer. */
785 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
786 D_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
787 for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
788 coef->MCU_buffer[i] = buffer + i;
790 coef->pub.consume_data = dummy_consume_data;
791 coef->pub.decompress_data = decompress_onepass;
792 coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
795 /* Allocate the workspace buffer */
796 coef->workspace = (JCOEF *)
797 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
798 sizeof(JCOEF) * DCTSIZE2);