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 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
8 * Copyright (C) 2010, 2015-2016, D. R. Commander.
9 * Copyright (C) 2015, 2020, Google, Inc.
10 * For conditions of distribution and use, see the accompanying README.ijg
13 * This file contains the coefficient buffer controller for decompression.
14 * This controller is the top level of the JPEG decompressor proper.
15 * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
17 * In buffered-image mode, this controller is the interface between
18 * input-oriented processing and output-oriented processing.
19 * Also, the input side (only) is used when reading a file for transcoding.
27 /* Forward declarations */
28 METHODDEF(int) decompress_onepass(j_decompress_ptr cinfo,
29 JSAMPIMAGE output_buf);
30 #ifdef D_MULTISCAN_FILES_SUPPORTED
31 METHODDEF(int) decompress_data(j_decompress_ptr cinfo, JSAMPIMAGE output_buf);
33 #ifdef BLOCK_SMOOTHING_SUPPORTED
34 LOCAL(boolean) smoothing_ok(j_decompress_ptr cinfo);
35 METHODDEF(int) decompress_smooth_data(j_decompress_ptr cinfo,
36 JSAMPIMAGE output_buf);
41 * Initialize for an input processing pass.
45 start_input_pass(j_decompress_ptr cinfo)
47 cinfo->input_iMCU_row = 0;
48 start_iMCU_row(cinfo);
53 * Initialize for an output processing pass.
57 start_output_pass(j_decompress_ptr cinfo)
59 #ifdef BLOCK_SMOOTHING_SUPPORTED
60 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
62 /* If multipass, check to see whether to use block smoothing on this pass */
63 if (coef->pub.coef_arrays != NULL) {
64 if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
65 coef->pub.decompress_data = decompress_smooth_data;
67 coef->pub.decompress_data = decompress_data;
70 cinfo->output_iMCU_row = 0;
75 * Decompress and return some data in the single-pass case.
76 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
77 * Input and output must run in lockstep since we have only a one-MCU buffer.
78 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
80 * NB: output_buf contains a plane for each component in image,
81 * which we index according to the component's SOF position.
85 decompress_onepass(j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
87 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
88 JDIMENSION MCU_col_num; /* index of current MCU within row */
89 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
90 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
91 int blkn, ci, xindex, yindex, yoffset, useful_width;
92 JSAMPARRAY output_ptr;
93 JDIMENSION start_col, output_col;
94 jpeg_component_info *compptr;
95 inverse_DCT_method_ptr inverse_DCT;
97 /* region decoding. this limits decode to the set of blocks +- 1 outside
98 * bounding blocks around the desired region to decode */
99 int blk1 = 0, blk2 = 0, skip = 0;
101 if ((cinfo->region_w > 0) && (cinfo->region_h > 0)) {
102 int bsz_w = 0, bsz_h = 0;
104 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
105 compptr = cinfo->cur_comp_info[ci];
106 if (compptr->MCU_sample_width > bsz_w)
107 bsz_w = compptr->MCU_sample_width;
108 if ((compptr->MCU_height * 8) > bsz_h)
109 bsz_h = compptr->MCU_height * 8;
111 int _region_y = (int)cinfo->region_y;
112 _region_y = (_region_y>>1)<<1;
113 if (((int)cinfo->output_scanline < (_region_y - bsz_h - 1)) ||
114 ((int)cinfo->output_scanline > (_region_y + cinfo->region_h + bsz_h)))
117 blk1 = (cinfo->region_x / bsz_w) - 1;
121 blk2 = ((cinfo->region_x + cinfo->region_w + bsz_w - 1) / bsz_w) + 1;
127 /* Loop to process as much as one whole iMCU row */
128 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
130 for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
133 /* see if we need to skip this MCU or not */
134 if ((cinfo->region_w > 0) && (cinfo->region_h > 0)) {
135 if (!((MCU_col_num < blk1) || (MCU_col_num > blk2) || skip))
138 /* if we are not skipping this MCU, zero it ready for huffman decode */
140 jzero_far((void FAR *) coef->MCU_buffer[0],
141 (size_t) (cinfo->blocks_in_MCU * sizeof(JBLOCK)));
143 /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
145 jzero_far((void FAR *) coef->MCU_buffer[0],
146 (size_t) (cinfo->blocks_in_MCU * sizeof(JBLOCK)));
148 jzero_far((void *)coef->MCU_buffer[0],
149 (size_t)(cinfo->blocks_in_MCU * sizeof(JBLOCK)));
151 if (!(*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
152 /* Suspension forced; update state counters and exit */
153 coef->MCU_vert_offset = yoffset;
154 coef->MCU_ctr = MCU_col_num;
155 return JPEG_SUSPENDED;
158 /* region decoding. this limits decode to the set of blocks +- 1 outside
159 * bounding blocks around the desired region to decode */
164 /* Only perform the IDCT on blocks that are contained within the desired
167 if (MCU_col_num >= cinfo->master->first_iMCU_col &&
168 MCU_col_num <= cinfo->master->last_iMCU_col) {
169 /* Determine where data should go in output_buf and do the IDCT thing.
170 * We skip dummy blocks at the right and bottom edges (but blkn gets
171 * incremented past them!). Note the inner loop relies on having
172 * allocated the MCU_buffer[] blocks sequentially.
174 blkn = 0; /* index of current DCT block within MCU */
175 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
176 compptr = cinfo->cur_comp_info[ci];
177 /* Don't bother to IDCT an uninteresting component. */
178 if (!compptr->component_needed) {
179 blkn += compptr->MCU_blocks;
182 inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
183 useful_width = (MCU_col_num < last_MCU_col) ?
184 compptr->MCU_width : compptr->last_col_width;
185 output_ptr = output_buf[compptr->component_index] +
186 yoffset * compptr->_DCT_scaled_size;
187 start_col = (MCU_col_num - cinfo->master->first_iMCU_col) *
188 compptr->MCU_sample_width;
189 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
190 if (cinfo->input_iMCU_row < last_iMCU_row ||
191 yoffset + yindex < compptr->last_row_height) {
192 output_col = start_col;
193 for (xindex = 0; xindex < useful_width; xindex++) {
194 (*inverse_DCT) (cinfo, compptr,
195 (JCOEFPTR)coef->MCU_buffer[blkn + xindex],
196 output_ptr, output_col);
197 output_col += compptr->_DCT_scaled_size;
200 blkn += compptr->MCU_width;
201 output_ptr += compptr->_DCT_scaled_size;
206 /* Completed an MCU row, but perhaps not an iMCU row */
209 /* Completed the iMCU row, advance counters for next one */
210 cinfo->output_iMCU_row++;
211 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
212 start_iMCU_row(cinfo);
213 return JPEG_ROW_COMPLETED;
215 /* Completed the scan */
216 (*cinfo->inputctl->finish_input_pass) (cinfo);
217 return JPEG_SCAN_COMPLETED;
222 * Dummy consume-input routine for single-pass operation.
226 dummy_consume_data(j_decompress_ptr cinfo)
228 return JPEG_SUSPENDED; /* Always indicate nothing was done */
232 #ifdef D_MULTISCAN_FILES_SUPPORTED
235 * Consume input data and store it in the full-image coefficient buffer.
236 * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
237 * ie, v_samp_factor block rows for each component in the scan.
238 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
242 consume_data(j_decompress_ptr cinfo)
244 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
245 JDIMENSION MCU_col_num; /* index of current MCU within row */
246 int blkn, ci, xindex, yindex, yoffset;
247 JDIMENSION start_col;
248 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
249 JBLOCKROW buffer_ptr;
250 jpeg_component_info *compptr;
252 /* Align the virtual buffers for the components used in this scan. */
253 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
254 compptr = cinfo->cur_comp_info[ci];
255 buffer[ci] = (*cinfo->mem->access_virt_barray)
256 ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
257 cinfo->input_iMCU_row * compptr->v_samp_factor,
258 (JDIMENSION)compptr->v_samp_factor, TRUE);
259 /* Note: entropy decoder expects buffer to be zeroed,
260 * but this is handled automatically by the memory manager
261 * because we requested a pre-zeroed array.
265 /* Loop to process one whole iMCU row */
266 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
268 for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
270 /* Construct list of pointers to DCT blocks belonging to this MCU */
271 blkn = 0; /* index of current DCT block within MCU */
272 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
273 compptr = cinfo->cur_comp_info[ci];
274 start_col = MCU_col_num * compptr->MCU_width;
275 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
276 buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
277 for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
278 coef->MCU_buffer[blkn++] = buffer_ptr++;
282 /* Try to fetch the MCU. */
283 if (!(*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
284 /* Suspension forced; update state counters and exit */
285 coef->MCU_vert_offset = yoffset;
286 coef->MCU_ctr = MCU_col_num;
287 return JPEG_SUSPENDED;
290 /* Completed an MCU row, but perhaps not an iMCU row */
293 /* Completed the iMCU row, advance counters for next one */
294 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
295 start_iMCU_row(cinfo);
296 return JPEG_ROW_COMPLETED;
298 /* Completed the scan */
299 (*cinfo->inputctl->finish_input_pass) (cinfo);
300 return JPEG_SCAN_COMPLETED;
305 * Decompress and return some data in the multi-pass case.
306 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
307 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
309 * NB: output_buf contains a plane for each component in image.
313 decompress_data(j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
315 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
316 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
317 JDIMENSION block_num;
318 int ci, block_row, block_rows;
320 JBLOCKROW buffer_ptr;
321 JSAMPARRAY output_ptr;
322 JDIMENSION output_col;
323 jpeg_component_info *compptr;
324 inverse_DCT_method_ptr inverse_DCT;
326 /* Force some input to be done if we are getting ahead of the input. */
327 while (cinfo->input_scan_number < cinfo->output_scan_number ||
328 (cinfo->input_scan_number == cinfo->output_scan_number &&
329 cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
330 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
331 return JPEG_SUSPENDED;
334 /* OK, output from the virtual arrays. */
335 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
337 /* Don't bother to IDCT an uninteresting component. */
338 if (!compptr->component_needed)
340 /* Align the virtual buffer for this component. */
341 buffer = (*cinfo->mem->access_virt_barray)
342 ((j_common_ptr)cinfo, coef->whole_image[ci],
343 cinfo->output_iMCU_row * compptr->v_samp_factor,
344 (JDIMENSION)compptr->v_samp_factor, FALSE);
345 /* Count non-dummy DCT block rows in this iMCU row. */
346 if (cinfo->output_iMCU_row < last_iMCU_row)
347 block_rows = compptr->v_samp_factor;
349 /* NB: can't use last_row_height here; it is input-side-dependent! */
350 block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
351 if (block_rows == 0) block_rows = compptr->v_samp_factor;
353 inverse_DCT = cinfo->idct->inverse_DCT[ci];
354 output_ptr = output_buf[ci];
355 /* Loop over all DCT blocks to be processed. */
356 for (block_row = 0; block_row < block_rows; block_row++) {
357 buffer_ptr = buffer[block_row] + cinfo->master->first_MCU_col[ci];
359 for (block_num = cinfo->master->first_MCU_col[ci];
360 block_num <= cinfo->master->last_MCU_col[ci]; block_num++) {
361 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR)buffer_ptr, output_ptr,
364 output_col += compptr->_DCT_scaled_size;
366 output_ptr += compptr->_DCT_scaled_size;
370 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
371 return JPEG_ROW_COMPLETED;
372 return JPEG_SCAN_COMPLETED;
375 #endif /* D_MULTISCAN_FILES_SUPPORTED */
378 #ifdef BLOCK_SMOOTHING_SUPPORTED
381 * This code applies interblock smoothing as described by section K.8
382 * of the JPEG standard: the first 5 AC coefficients are estimated from
383 * the DC values of a DCT block and its 8 neighboring blocks.
384 * We apply smoothing only for progressive JPEG decoding, and only if
385 * the coefficients it can estimate are not yet known to full precision.
388 /* Natural-order array positions of the first 5 zigzag-order coefficients */
396 * Determine whether block smoothing is applicable and safe.
397 * We also latch the current states of the coef_bits[] entries for the
398 * AC coefficients; otherwise, if the input side of the decompressor
399 * advances into a new scan, we might think the coefficients are known
400 * more accurately than they really are.
404 smoothing_ok(j_decompress_ptr cinfo)
406 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
407 boolean smoothing_useful = FALSE;
409 jpeg_component_info *compptr;
412 int *coef_bits_latch;
414 if (!cinfo->progressive_mode || cinfo->coef_bits == NULL)
417 /* Allocate latch area if not already done */
418 if (coef->coef_bits_latch == NULL)
419 coef->coef_bits_latch = (int *)
420 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
421 cinfo->num_components *
422 (SAVED_COEFS * sizeof(int)));
423 coef_bits_latch = coef->coef_bits_latch;
425 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
427 /* All components' quantization values must already be latched. */
428 if ((qtable = compptr->quant_table) == NULL)
430 /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
431 if (qtable->quantval[0] == 0 ||
432 qtable->quantval[Q01_POS] == 0 ||
433 qtable->quantval[Q10_POS] == 0 ||
434 qtable->quantval[Q20_POS] == 0 ||
435 qtable->quantval[Q11_POS] == 0 ||
436 qtable->quantval[Q02_POS] == 0)
438 /* DC values must be at least partly known for all components. */
439 coef_bits = cinfo->coef_bits[ci];
440 if (coef_bits[0] < 0)
442 /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
443 for (coefi = 1; coefi <= 5; coefi++) {
444 coef_bits_latch[coefi] = coef_bits[coefi];
445 if (coef_bits[coefi] != 0)
446 smoothing_useful = TRUE;
448 coef_bits_latch += SAVED_COEFS;
451 return smoothing_useful;
456 * Variant of decompress_data for use when doing block smoothing.
460 decompress_smooth_data(j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
462 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
463 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
464 JDIMENSION block_num, last_block_column;
465 int ci, block_row, block_rows, access_rows;
467 JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
468 JSAMPARRAY output_ptr;
469 JDIMENSION output_col;
470 jpeg_component_info *compptr;
471 inverse_DCT_method_ptr inverse_DCT;
472 boolean first_row, last_row;
475 JQUANT_TBL *quanttbl;
476 JLONG Q00, Q01, Q02, Q10, Q11, Q20, num;
477 int DC1, DC2, DC3, DC4, DC5, DC6, DC7, DC8, DC9;
480 /* Keep a local variable to avoid looking it up more than once */
481 workspace = coef->workspace;
483 /* Force some input to be done if we are getting ahead of the input. */
484 while (cinfo->input_scan_number <= cinfo->output_scan_number &&
485 !cinfo->inputctl->eoi_reached) {
486 if (cinfo->input_scan_number == cinfo->output_scan_number) {
487 /* If input is working on current scan, we ordinarily want it to
488 * have completed the current row. But if input scan is DC,
489 * we want it to keep one row ahead so that next block row's DC
490 * values are up to date.
492 JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
493 if (cinfo->input_iMCU_row > cinfo->output_iMCU_row + delta)
496 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
497 return JPEG_SUSPENDED;
500 /* OK, output from the virtual arrays. */
501 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
503 /* Don't bother to IDCT an uninteresting component. */
504 if (!compptr->component_needed)
506 /* Count non-dummy DCT block rows in this iMCU row. */
507 if (cinfo->output_iMCU_row < last_iMCU_row) {
508 block_rows = compptr->v_samp_factor;
509 access_rows = block_rows * 2; /* this and next iMCU row */
512 /* NB: can't use last_row_height here; it is input-side-dependent! */
513 block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
514 if (block_rows == 0) block_rows = compptr->v_samp_factor;
515 access_rows = block_rows; /* this iMCU row only */
518 /* Align the virtual buffer for this component. */
519 if (cinfo->output_iMCU_row > 0) {
520 access_rows += compptr->v_samp_factor; /* prior iMCU row too */
521 buffer = (*cinfo->mem->access_virt_barray)
522 ((j_common_ptr)cinfo, coef->whole_image[ci],
523 (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
524 (JDIMENSION)access_rows, FALSE);
525 buffer += compptr->v_samp_factor; /* point to current iMCU row */
528 buffer = (*cinfo->mem->access_virt_barray)
529 ((j_common_ptr)cinfo, coef->whole_image[ci],
530 (JDIMENSION)0, (JDIMENSION)access_rows, FALSE);
533 /* Fetch component-dependent info */
534 coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
535 quanttbl = compptr->quant_table;
536 Q00 = quanttbl->quantval[0];
537 Q01 = quanttbl->quantval[Q01_POS];
538 Q10 = quanttbl->quantval[Q10_POS];
539 Q20 = quanttbl->quantval[Q20_POS];
540 Q11 = quanttbl->quantval[Q11_POS];
541 Q02 = quanttbl->quantval[Q02_POS];
542 inverse_DCT = cinfo->idct->inverse_DCT[ci];
543 output_ptr = output_buf[ci];
544 /* Loop over all DCT blocks to be processed. */
545 for (block_row = 0; block_row < block_rows; block_row++) {
546 buffer_ptr = buffer[block_row] + cinfo->master->first_MCU_col[ci];
547 if (first_row && block_row == 0)
548 prev_block_row = buffer_ptr;
550 prev_block_row = buffer[block_row - 1] +
551 cinfo->master->first_MCU_col[ci];
552 if (last_row && block_row == block_rows - 1)
553 next_block_row = buffer_ptr;
555 next_block_row = buffer[block_row + 1] +
556 cinfo->master->first_MCU_col[ci];
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.
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];
564 last_block_column = compptr->width_in_blocks - 1;
565 for (block_num = cinfo->master->first_MCU_col[ci];
566 block_num <= cinfo->master->last_MCU_col[ci]; block_num++) {
567 /* Fetch current DCT block into workspace so we can modify it. */
568 jcopy_block_row(buffer_ptr, (JBLOCKROW)workspace, (JDIMENSION)1);
569 /* Update DC values */
570 if (block_num < last_block_column) {
571 DC3 = (int)prev_block_row[1][0];
572 DC6 = (int)buffer_ptr[1][0];
573 DC9 = (int)next_block_row[1][0];
575 /* Compute coefficient estimates per K.8.
576 * An estimate is applied only if coefficient is still zero,
577 * and is not known to be fully accurate.
580 if ((Al = coef_bits[1]) != 0 && workspace[1] == 0) {
581 num = 36 * Q00 * (DC4 - DC6);
583 pred = (int)(((Q01 << 7) + num) / (Q01 << 8));
584 if (Al > 0 && pred >= (1 << Al))
585 pred = (1 << Al) - 1;
587 pred = (int)(((Q01 << 7) - num) / (Q01 << 8));
588 if (Al > 0 && pred >= (1 << Al))
589 pred = (1 << Al) - 1;
592 workspace[1] = (JCOEF)pred;
595 if ((Al = coef_bits[2]) != 0 && workspace[8] == 0) {
596 num = 36 * Q00 * (DC2 - DC8);
598 pred = (int)(((Q10 << 7) + num) / (Q10 << 8));
599 if (Al > 0 && pred >= (1 << Al))
600 pred = (1 << Al) - 1;
602 pred = (int)(((Q10 << 7) - num) / (Q10 << 8));
603 if (Al > 0 && pred >= (1 << Al))
604 pred = (1 << Al) - 1;
607 workspace[8] = (JCOEF)pred;
610 if ((Al = coef_bits[3]) != 0 && workspace[16] == 0) {
611 num = 9 * Q00 * (DC2 + DC8 - 2 * DC5);
613 pred = (int)(((Q20 << 7) + num) / (Q20 << 8));
614 if (Al > 0 && pred >= (1 << Al))
615 pred = (1 << Al) - 1;
617 pred = (int)(((Q20 << 7) - num) / (Q20 << 8));
618 if (Al > 0 && pred >= (1 << Al))
619 pred = (1 << Al) - 1;
622 workspace[16] = (JCOEF)pred;
625 if ((Al = coef_bits[4]) != 0 && workspace[9] == 0) {
626 num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
628 pred = (int)(((Q11 << 7) + num) / (Q11 << 8));
629 if (Al > 0 && pred >= (1 << Al))
630 pred = (1 << Al) - 1;
632 pred = (int)(((Q11 << 7) - num) / (Q11 << 8));
633 if (Al > 0 && pred >= (1 << Al))
634 pred = (1 << Al) - 1;
637 workspace[9] = (JCOEF)pred;
640 if ((Al = coef_bits[5]) != 0 && workspace[2] == 0) {
641 num = 9 * Q00 * (DC4 + DC6 - 2 * DC5);
643 pred = (int)(((Q02 << 7) + num) / (Q02 << 8));
644 if (Al > 0 && pred >= (1 << Al))
645 pred = (1 << Al) - 1;
647 pred = (int)(((Q02 << 7) - num) / (Q02 << 8));
648 if (Al > 0 && pred >= (1 << Al))
649 pred = (1 << Al) - 1;
652 workspace[2] = (JCOEF)pred;
654 /* OK, do the IDCT */
655 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR)workspace, output_ptr,
657 /* Advance for next column */
658 DC1 = DC2; DC2 = DC3;
659 DC4 = DC5; DC5 = DC6;
660 DC7 = DC8; DC8 = DC9;
661 buffer_ptr++, prev_block_row++, next_block_row++;
662 output_col += compptr->_DCT_scaled_size;
664 output_ptr += compptr->_DCT_scaled_size;
668 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
669 return JPEG_ROW_COMPLETED;
670 return JPEG_SCAN_COMPLETED;
673 #endif /* BLOCK_SMOOTHING_SUPPORTED */
677 * Initialize coefficient buffer controller.
681 jinit_d_coef_controller(j_decompress_ptr cinfo, boolean need_full_buffer)
686 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
687 sizeof(my_coef_controller));
688 cinfo->coef = (struct jpeg_d_coef_controller *)coef;
689 coef->pub.start_input_pass = start_input_pass;
690 coef->pub.start_output_pass = start_output_pass;
691 #ifdef BLOCK_SMOOTHING_SUPPORTED
692 coef->coef_bits_latch = NULL;
695 /* Create the coefficient buffer. */
696 if (need_full_buffer) {
697 #ifdef D_MULTISCAN_FILES_SUPPORTED
698 /* Allocate a full-image virtual array for each component, */
699 /* padded to a multiple of samp_factor DCT blocks in each direction. */
700 /* Note we ask for a pre-zeroed array. */
702 jpeg_component_info *compptr;
704 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
706 access_rows = compptr->v_samp_factor;
707 #ifdef BLOCK_SMOOTHING_SUPPORTED
708 /* If block smoothing could be used, need a bigger window */
709 if (cinfo->progressive_mode)
712 coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
713 ((j_common_ptr)cinfo, JPOOL_IMAGE, TRUE,
714 (JDIMENSION)jround_up((long)compptr->width_in_blocks,
715 (long)compptr->h_samp_factor),
716 (JDIMENSION)jround_up((long)compptr->height_in_blocks,
717 (long)compptr->v_samp_factor),
718 (JDIMENSION)access_rows);
720 coef->pub.consume_data = consume_data;
721 coef->pub.decompress_data = decompress_data;
722 coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
724 ERREXIT(cinfo, JERR_NOT_COMPILED);
727 /* We only need a single-MCU buffer. */
732 (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
733 D_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
734 for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
735 coef->MCU_buffer[i] = buffer + i;
737 coef->pub.consume_data = dummy_consume_data;
738 coef->pub.decompress_data = decompress_onepass;
739 coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
742 /* Allocate the workspace buffer */
743 coef->workspace = (JCOEF *)
744 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
745 sizeof(JCOEF) * DCTSIZE2);