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, 2019-2020, 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->insufficient_data)
152 cinfo->master->last_good_iMCU_row = cinfo->input_iMCU_row;
153 if (!(*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
154 /* Suspension forced; update state counters and exit */
155 coef->MCU_vert_offset = yoffset;
156 coef->MCU_ctr = MCU_col_num;
157 return JPEG_SUSPENDED;
160 /* region decoding. this limits decode to the set of blocks +- 1 outside
161 * bounding blocks around the desired region to decode */
166 /* Only perform the IDCT on blocks that are contained within the desired
169 if (MCU_col_num >= cinfo->master->first_iMCU_col &&
170 MCU_col_num <= cinfo->master->last_iMCU_col) {
171 /* Determine where data should go in output_buf and do the IDCT thing.
172 * We skip dummy blocks at the right and bottom edges (but blkn gets
173 * incremented past them!). Note the inner loop relies on having
174 * allocated the MCU_buffer[] blocks sequentially.
176 blkn = 0; /* index of current DCT block within MCU */
177 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
178 compptr = cinfo->cur_comp_info[ci];
179 /* Don't bother to IDCT an uninteresting component. */
180 if (!compptr->component_needed) {
181 blkn += compptr->MCU_blocks;
184 inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
185 useful_width = (MCU_col_num < last_MCU_col) ?
186 compptr->MCU_width : compptr->last_col_width;
187 output_ptr = output_buf[compptr->component_index] +
188 yoffset * compptr->_DCT_scaled_size;
189 start_col = (MCU_col_num - cinfo->master->first_iMCU_col) *
190 compptr->MCU_sample_width;
191 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
192 if (cinfo->input_iMCU_row < last_iMCU_row ||
193 yoffset + yindex < compptr->last_row_height) {
194 output_col = start_col;
195 for (xindex = 0; xindex < useful_width; xindex++) {
196 (*inverse_DCT) (cinfo, compptr,
197 (JCOEFPTR)coef->MCU_buffer[blkn + xindex],
198 output_ptr, output_col);
199 output_col += compptr->_DCT_scaled_size;
202 blkn += compptr->MCU_width;
203 output_ptr += compptr->_DCT_scaled_size;
208 /* Completed an MCU row, but perhaps not an iMCU row */
211 /* Completed the iMCU row, advance counters for next one */
212 cinfo->output_iMCU_row++;
213 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
214 start_iMCU_row(cinfo);
215 return JPEG_ROW_COMPLETED;
217 /* Completed the scan */
218 (*cinfo->inputctl->finish_input_pass) (cinfo);
219 return JPEG_SCAN_COMPLETED;
224 * Dummy consume-input routine for single-pass operation.
228 dummy_consume_data(j_decompress_ptr cinfo)
230 return JPEG_SUSPENDED; /* Always indicate nothing was done */
234 #ifdef D_MULTISCAN_FILES_SUPPORTED
237 * Consume input data and store it in the full-image coefficient buffer.
238 * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
239 * ie, v_samp_factor block rows for each component in the scan.
240 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
244 consume_data(j_decompress_ptr cinfo)
246 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
247 JDIMENSION MCU_col_num; /* index of current MCU within row */
248 int blkn, ci, xindex, yindex, yoffset;
249 JDIMENSION start_col;
250 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
251 JBLOCKROW buffer_ptr;
252 jpeg_component_info *compptr;
254 /* Align the virtual buffers for the components used in this scan. */
255 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
256 compptr = cinfo->cur_comp_info[ci];
257 buffer[ci] = (*cinfo->mem->access_virt_barray)
258 ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
259 cinfo->input_iMCU_row * compptr->v_samp_factor,
260 (JDIMENSION)compptr->v_samp_factor, TRUE);
261 /* Note: entropy decoder expects buffer to be zeroed,
262 * but this is handled automatically by the memory manager
263 * because we requested a pre-zeroed array.
267 /* Loop to process one whole iMCU row */
268 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
270 for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
272 /* Construct list of pointers to DCT blocks belonging to this MCU */
273 blkn = 0; /* index of current DCT block within MCU */
274 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
275 compptr = cinfo->cur_comp_info[ci];
276 start_col = MCU_col_num * compptr->MCU_width;
277 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
278 buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
279 for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
280 coef->MCU_buffer[blkn++] = buffer_ptr++;
284 if (!cinfo->entropy->insufficient_data)
285 cinfo->master->last_good_iMCU_row = cinfo->input_iMCU_row;
286 /* Try to fetch the MCU. */
287 if (!(*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
288 /* Suspension forced; update state counters and exit */
289 coef->MCU_vert_offset = yoffset;
290 coef->MCU_ctr = MCU_col_num;
291 return JPEG_SUSPENDED;
294 /* Completed an MCU row, but perhaps not an iMCU row */
297 /* Completed the iMCU row, advance counters for next one */
298 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
299 start_iMCU_row(cinfo);
300 return JPEG_ROW_COMPLETED;
302 /* Completed the scan */
303 (*cinfo->inputctl->finish_input_pass) (cinfo);
304 return JPEG_SCAN_COMPLETED;
309 * Decompress and return some data in the multi-pass case.
310 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
311 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
313 * NB: output_buf contains a plane for each component in image.
317 decompress_data(j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
319 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
320 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
321 JDIMENSION block_num;
322 int ci, block_row, block_rows;
324 JBLOCKROW buffer_ptr;
325 JSAMPARRAY output_ptr;
326 JDIMENSION output_col;
327 jpeg_component_info *compptr;
328 inverse_DCT_method_ptr inverse_DCT;
330 /* Force some input to be done if we are getting ahead of the input. */
331 while (cinfo->input_scan_number < cinfo->output_scan_number ||
332 (cinfo->input_scan_number == cinfo->output_scan_number &&
333 cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
334 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
335 return JPEG_SUSPENDED;
338 /* OK, output from the virtual arrays. */
339 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
341 /* Don't bother to IDCT an uninteresting component. */
342 if (!compptr->component_needed)
344 /* Align the virtual buffer for this component. */
345 buffer = (*cinfo->mem->access_virt_barray)
346 ((j_common_ptr)cinfo, coef->whole_image[ci],
347 cinfo->output_iMCU_row * compptr->v_samp_factor,
348 (JDIMENSION)compptr->v_samp_factor, FALSE);
349 /* Count non-dummy DCT block rows in this iMCU row. */
350 if (cinfo->output_iMCU_row < last_iMCU_row)
351 block_rows = compptr->v_samp_factor;
353 /* NB: can't use last_row_height here; it is input-side-dependent! */
354 block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
355 if (block_rows == 0) block_rows = compptr->v_samp_factor;
357 inverse_DCT = cinfo->idct->inverse_DCT[ci];
358 output_ptr = output_buf[ci];
359 /* Loop over all DCT blocks to be processed. */
360 for (block_row = 0; block_row < block_rows; block_row++) {
361 buffer_ptr = buffer[block_row] + cinfo->master->first_MCU_col[ci];
363 for (block_num = cinfo->master->first_MCU_col[ci];
364 block_num <= cinfo->master->last_MCU_col[ci]; block_num++) {
365 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR)buffer_ptr, output_ptr,
368 output_col += compptr->_DCT_scaled_size;
370 output_ptr += compptr->_DCT_scaled_size;
374 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
375 return JPEG_ROW_COMPLETED;
376 return JPEG_SCAN_COMPLETED;
379 #endif /* D_MULTISCAN_FILES_SUPPORTED */
382 #ifdef BLOCK_SMOOTHING_SUPPORTED
385 * This code applies interblock smoothing; the first 9 AC coefficients are
386 * estimated from the DC values of a DCT block and its 24 neighboring blocks.
387 * We apply smoothing only for progressive JPEG decoding, and only if
388 * the coefficients it can estimate are not yet known to full precision.
391 /* Natural-order array positions of the first 9 zigzag-order coefficients */
403 * Determine whether block smoothing is applicable and safe.
404 * We also latch the current states of the coef_bits[] entries for the
405 * AC coefficients; otherwise, if the input side of the decompressor
406 * advances into a new scan, we might think the coefficients are known
407 * more accurately than they really are.
411 smoothing_ok(j_decompress_ptr cinfo)
413 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
414 boolean smoothing_useful = FALSE;
416 jpeg_component_info *compptr;
418 int *coef_bits, *prev_coef_bits;
419 int *coef_bits_latch, *prev_coef_bits_latch;
421 if (!cinfo->progressive_mode || cinfo->coef_bits == NULL)
424 /* Allocate latch area if not already done */
425 if (coef->coef_bits_latch == NULL)
426 coef->coef_bits_latch = (int *)
427 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
428 cinfo->num_components * 2 *
429 (SAVED_COEFS * sizeof(int)));
430 coef_bits_latch = coef->coef_bits_latch;
431 prev_coef_bits_latch =
432 &coef->coef_bits_latch[cinfo->num_components * SAVED_COEFS];
434 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
436 /* All components' quantization values must already be latched. */
437 if ((qtable = compptr->quant_table) == NULL)
439 /* Verify DC & first 9 AC quantizers are nonzero to avoid zero-divide. */
440 if (qtable->quantval[0] == 0 ||
441 qtable->quantval[Q01_POS] == 0 ||
442 qtable->quantval[Q10_POS] == 0 ||
443 qtable->quantval[Q20_POS] == 0 ||
444 qtable->quantval[Q11_POS] == 0 ||
445 qtable->quantval[Q02_POS] == 0 ||
446 qtable->quantval[Q03_POS] == 0 ||
447 qtable->quantval[Q12_POS] == 0 ||
448 qtable->quantval[Q21_POS] == 0 ||
449 qtable->quantval[Q30_POS] == 0)
451 /* DC values must be at least partly known for all components. */
452 coef_bits = cinfo->coef_bits[ci];
453 prev_coef_bits = cinfo->coef_bits[ci + cinfo->num_components];
454 if (coef_bits[0] < 0)
456 coef_bits_latch[0] = coef_bits[0];
457 /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
458 for (coefi = 1; coefi < SAVED_COEFS; coefi++) {
459 if (cinfo->input_scan_number > 1)
460 prev_coef_bits_latch[coefi] = prev_coef_bits[coefi];
462 prev_coef_bits_latch[coefi] = -1;
463 coef_bits_latch[coefi] = coef_bits[coefi];
464 if (coef_bits[coefi] != 0)
465 smoothing_useful = TRUE;
467 coef_bits_latch += SAVED_COEFS;
468 prev_coef_bits_latch += SAVED_COEFS;
471 return smoothing_useful;
476 * Variant of decompress_data for use when doing block smoothing.
480 decompress_smooth_data(j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
482 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
483 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
484 JDIMENSION block_num, last_block_column;
485 int ci, block_row, block_rows, access_rows;
487 JBLOCKROW buffer_ptr, prev_prev_block_row, prev_block_row;
488 JBLOCKROW next_block_row, next_next_block_row;
489 JSAMPARRAY output_ptr;
490 JDIMENSION output_col;
491 jpeg_component_info *compptr;
492 inverse_DCT_method_ptr inverse_DCT;
496 JQUANT_TBL *quanttbl;
497 JLONG Q00, Q01, Q02, Q03 = 0, Q10, Q11, Q12 = 0, Q20, Q21 = 0, Q30 = 0, num;
498 int DC01, DC02, DC03, DC04, DC05, DC06, DC07, DC08, DC09, DC10, DC11, DC12,
499 DC13, DC14, DC15, DC16, DC17, DC18, DC19, DC20, DC21, DC22, DC23, DC24,
503 /* Keep a local variable to avoid looking it up more than once */
504 workspace = coef->workspace;
506 /* Force some input to be done if we are getting ahead of the input. */
507 while (cinfo->input_scan_number <= cinfo->output_scan_number &&
508 !cinfo->inputctl->eoi_reached) {
509 if (cinfo->input_scan_number == cinfo->output_scan_number) {
510 /* If input is working on current scan, we ordinarily want it to
511 * have completed the current row. But if input scan is DC,
512 * we want it to keep two rows ahead so that next two block rows' DC
513 * values are up to date.
515 JDIMENSION delta = (cinfo->Ss == 0) ? 2 : 0;
516 if (cinfo->input_iMCU_row > cinfo->output_iMCU_row + delta)
519 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
520 return JPEG_SUSPENDED;
523 /* OK, output from the virtual arrays. */
524 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
526 /* Don't bother to IDCT an uninteresting component. */
527 if (!compptr->component_needed)
529 /* Count non-dummy DCT block rows in this iMCU row. */
530 if (cinfo->output_iMCU_row < last_iMCU_row - 1) {
531 block_rows = compptr->v_samp_factor;
532 access_rows = block_rows * 3; /* this and next two iMCU rows */
533 } else if (cinfo->output_iMCU_row < last_iMCU_row) {
534 block_rows = compptr->v_samp_factor;
535 access_rows = block_rows * 2; /* this and next iMCU row */
537 /* NB: can't use last_row_height here; it is input-side-dependent! */
538 block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
539 if (block_rows == 0) block_rows = compptr->v_samp_factor;
540 access_rows = block_rows; /* this iMCU row only */
542 /* Align the virtual buffer for this component. */
543 if (cinfo->output_iMCU_row > 1) {
544 access_rows += 2 * compptr->v_samp_factor; /* prior two iMCU rows too */
545 buffer = (*cinfo->mem->access_virt_barray)
546 ((j_common_ptr)cinfo, coef->whole_image[ci],
547 (cinfo->output_iMCU_row - 2) * compptr->v_samp_factor,
548 (JDIMENSION)access_rows, FALSE);
549 buffer += 2 * compptr->v_samp_factor; /* point to current iMCU row */
550 } else if (cinfo->output_iMCU_row > 0) {
551 buffer = (*cinfo->mem->access_virt_barray)
552 ((j_common_ptr)cinfo, coef->whole_image[ci],
553 (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
554 (JDIMENSION)access_rows, FALSE);
555 buffer += compptr->v_samp_factor; /* point to current iMCU row */
557 buffer = (*cinfo->mem->access_virt_barray)
558 ((j_common_ptr)cinfo, coef->whole_image[ci],
559 (JDIMENSION)0, (JDIMENSION)access_rows, FALSE);
561 /* Fetch component-dependent info.
562 * If the current scan is incomplete, then we use the component-dependent
563 * info from the previous scan.
565 if (cinfo->output_iMCU_row > cinfo->master->last_good_iMCU_row)
567 coef->coef_bits_latch + ((ci + cinfo->num_components) * SAVED_COEFS);
569 coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
571 /* We only do DC interpolation if no AC coefficient data is available. */
573 coef_bits[1] == -1 && coef_bits[2] == -1 && coef_bits[3] == -1 &&
574 coef_bits[4] == -1 && coef_bits[5] == -1 && coef_bits[6] == -1 &&
575 coef_bits[7] == -1 && coef_bits[8] == -1 && coef_bits[9] == -1;
577 quanttbl = compptr->quant_table;
578 Q00 = quanttbl->quantval[0];
579 Q01 = quanttbl->quantval[Q01_POS];
580 Q10 = quanttbl->quantval[Q10_POS];
581 Q20 = quanttbl->quantval[Q20_POS];
582 Q11 = quanttbl->quantval[Q11_POS];
583 Q02 = quanttbl->quantval[Q02_POS];
585 Q03 = quanttbl->quantval[Q03_POS];
586 Q12 = quanttbl->quantval[Q12_POS];
587 Q21 = quanttbl->quantval[Q21_POS];
588 Q30 = quanttbl->quantval[Q30_POS];
590 inverse_DCT = cinfo->idct->inverse_DCT[ci];
591 output_ptr = output_buf[ci];
592 /* Loop over all DCT blocks to be processed. */
593 for (block_row = 0; block_row < block_rows; block_row++) {
594 buffer_ptr = buffer[block_row] + cinfo->master->first_MCU_col[ci];
596 if (block_row > 0 || cinfo->output_iMCU_row > 0)
598 buffer[block_row - 1] + cinfo->master->first_MCU_col[ci];
600 prev_block_row = buffer_ptr;
602 if (block_row > 1 || cinfo->output_iMCU_row > 1)
603 prev_prev_block_row =
604 buffer[block_row - 2] + cinfo->master->first_MCU_col[ci];
606 prev_prev_block_row = prev_block_row;
608 if (block_row < block_rows - 1 || cinfo->output_iMCU_row < last_iMCU_row)
610 buffer[block_row + 1] + cinfo->master->first_MCU_col[ci];
612 next_block_row = buffer_ptr;
614 if (block_row < block_rows - 2 ||
615 cinfo->output_iMCU_row < last_iMCU_row - 1)
616 next_next_block_row =
617 buffer[block_row + 2] + cinfo->master->first_MCU_col[ci];
619 next_next_block_row = next_block_row;
621 /* We fetch the surrounding DC values using a sliding-register approach.
622 * Initialize all 25 here so as to do the right thing on narrow pics.
624 DC01 = DC02 = DC03 = DC04 = DC05 = (int)prev_prev_block_row[0][0];
625 DC06 = DC07 = DC08 = DC09 = DC10 = (int)prev_block_row[0][0];
626 DC11 = DC12 = DC13 = DC14 = DC15 = (int)buffer_ptr[0][0];
627 DC16 = DC17 = DC18 = DC19 = DC20 = (int)next_block_row[0][0];
628 DC21 = DC22 = DC23 = DC24 = DC25 = (int)next_next_block_row[0][0];
630 last_block_column = compptr->width_in_blocks - 1;
631 for (block_num = cinfo->master->first_MCU_col[ci];
632 block_num <= cinfo->master->last_MCU_col[ci]; block_num++) {
633 /* Fetch current DCT block into workspace so we can modify it. */
634 jcopy_block_row(buffer_ptr, (JBLOCKROW)workspace, (JDIMENSION)1);
635 /* Update DC values */
636 if (block_num == cinfo->master->first_MCU_col[ci] &&
637 block_num < last_block_column) {
638 DC04 = (int)prev_prev_block_row[1][0];
639 DC09 = (int)prev_block_row[1][0];
640 DC14 = (int)buffer_ptr[1][0];
641 DC19 = (int)next_block_row[1][0];
642 DC24 = (int)next_next_block_row[1][0];
644 if (block_num + 1 < last_block_column) {
645 DC05 = (int)prev_prev_block_row[2][0];
646 DC10 = (int)prev_block_row[2][0];
647 DC15 = (int)buffer_ptr[2][0];
648 DC20 = (int)next_block_row[2][0];
649 DC25 = (int)next_next_block_row[2][0];
651 /* If DC interpolation is enabled, compute coefficient estimates using
652 * a Gaussian-like kernel, keeping the averages of the DC values.
654 * If DC interpolation is disabled, compute coefficient estimates using
655 * an algorithm similar to the one described in Section K.8 of the JPEG
656 * standard, except applied to a 5x5 window rather than a 3x3 window.
658 * An estimate is applied only if the coefficient is still zero and is
659 * not known to be fully accurate.
662 if ((Al = coef_bits[1]) != 0 && workspace[1] == 0) {
663 num = Q00 * (change_dc ?
664 (-DC01 - DC02 + DC04 + DC05 - 3 * DC06 + 13 * DC07 -
665 13 * DC09 + 3 * DC10 - 3 * DC11 + 38 * DC12 - 38 * DC14 +
666 3 * DC15 - 3 * DC16 + 13 * DC17 - 13 * DC19 + 3 * DC20 -
667 DC21 - DC22 + DC24 + DC25) :
668 (-7 * DC11 + 50 * DC12 - 50 * DC14 + 7 * DC15));
670 pred = (int)(((Q01 << 7) + num) / (Q01 << 8));
671 if (Al > 0 && pred >= (1 << Al))
672 pred = (1 << Al) - 1;
674 pred = (int)(((Q01 << 7) - num) / (Q01 << 8));
675 if (Al > 0 && pred >= (1 << Al))
676 pred = (1 << Al) - 1;
679 workspace[1] = (JCOEF)pred;
682 if ((Al = coef_bits[2]) != 0 && workspace[8] == 0) {
683 num = Q00 * (change_dc ?
684 (-DC01 - 3 * DC02 - 3 * DC03 - 3 * DC04 - DC05 - DC06 +
685 13 * DC07 + 38 * DC08 + 13 * DC09 - DC10 + DC16 -
686 13 * DC17 - 38 * DC18 - 13 * DC19 + DC20 + DC21 +
687 3 * DC22 + 3 * DC23 + 3 * DC24 + DC25) :
688 (-7 * DC03 + 50 * DC08 - 50 * DC18 + 7 * DC23));
690 pred = (int)(((Q10 << 7) + num) / (Q10 << 8));
691 if (Al > 0 && pred >= (1 << Al))
692 pred = (1 << Al) - 1;
694 pred = (int)(((Q10 << 7) - num) / (Q10 << 8));
695 if (Al > 0 && pred >= (1 << Al))
696 pred = (1 << Al) - 1;
699 workspace[8] = (JCOEF)pred;
702 if ((Al = coef_bits[3]) != 0 && workspace[16] == 0) {
703 num = Q00 * (change_dc ?
704 (DC03 + 2 * DC07 + 7 * DC08 + 2 * DC09 - 5 * DC12 - 14 * DC13 -
705 5 * DC14 + 2 * DC17 + 7 * DC18 + 2 * DC19 + DC23) :
706 (-DC03 + 13 * DC08 - 24 * DC13 + 13 * DC18 - DC23));
708 pred = (int)(((Q20 << 7) + num) / (Q20 << 8));
709 if (Al > 0 && pred >= (1 << Al))
710 pred = (1 << Al) - 1;
712 pred = (int)(((Q20 << 7) - num) / (Q20 << 8));
713 if (Al > 0 && pred >= (1 << Al))
714 pred = (1 << Al) - 1;
717 workspace[16] = (JCOEF)pred;
720 if ((Al = coef_bits[4]) != 0 && workspace[9] == 0) {
721 num = Q00 * (change_dc ?
722 (-DC01 + DC05 + 9 * DC07 - 9 * DC09 - 9 * DC17 +
723 9 * DC19 + DC21 - DC25) :
724 (DC10 + DC16 - 10 * DC17 + 10 * DC19 - DC02 - DC20 + DC22 -
725 DC24 + DC04 - DC06 + 10 * DC07 - 10 * DC09));
727 pred = (int)(((Q11 << 7) + num) / (Q11 << 8));
728 if (Al > 0 && pred >= (1 << Al))
729 pred = (1 << Al) - 1;
731 pred = (int)(((Q11 << 7) - num) / (Q11 << 8));
732 if (Al > 0 && pred >= (1 << Al))
733 pred = (1 << Al) - 1;
736 workspace[9] = (JCOEF)pred;
739 if ((Al = coef_bits[5]) != 0 && workspace[2] == 0) {
740 num = Q00 * (change_dc ?
741 (2 * DC07 - 5 * DC08 + 2 * DC09 + DC11 + 7 * DC12 - 14 * DC13 +
742 7 * DC14 + DC15 + 2 * DC17 - 5 * DC18 + 2 * DC19) :
743 (-DC11 + 13 * DC12 - 24 * DC13 + 13 * DC14 - DC15));
745 pred = (int)(((Q02 << 7) + num) / (Q02 << 8));
746 if (Al > 0 && pred >= (1 << Al))
747 pred = (1 << Al) - 1;
749 pred = (int)(((Q02 << 7) - num) / (Q02 << 8));
750 if (Al > 0 && pred >= (1 << Al))
751 pred = (1 << Al) - 1;
754 workspace[2] = (JCOEF)pred;
758 if ((Al = coef_bits[6]) != 0 && workspace[3] == 0) {
759 num = Q00 * (DC07 - DC09 + 2 * DC12 - 2 * DC14 + DC17 - DC19);
761 pred = (int)(((Q03 << 7) + num) / (Q03 << 8));
762 if (Al > 0 && pred >= (1 << Al))
763 pred = (1 << Al) - 1;
765 pred = (int)(((Q03 << 7) - num) / (Q03 << 8));
766 if (Al > 0 && pred >= (1 << Al))
767 pred = (1 << Al) - 1;
770 workspace[3] = (JCOEF)pred;
773 if ((Al = coef_bits[7]) != 0 && workspace[10] == 0) {
774 num = Q00 * (DC07 - 3 * DC08 + DC09 - DC17 + 3 * DC18 - DC19);
776 pred = (int)(((Q12 << 7) + num) / (Q12 << 8));
777 if (Al > 0 && pred >= (1 << Al))
778 pred = (1 << Al) - 1;
780 pred = (int)(((Q12 << 7) - num) / (Q12 << 8));
781 if (Al > 0 && pred >= (1 << Al))
782 pred = (1 << Al) - 1;
785 workspace[10] = (JCOEF)pred;
788 if ((Al = coef_bits[8]) != 0 && workspace[17] == 0) {
789 num = Q00 * (DC07 - DC09 - 3 * DC12 + 3 * DC14 + DC17 - DC19);
791 pred = (int)(((Q21 << 7) + num) / (Q21 << 8));
792 if (Al > 0 && pred >= (1 << Al))
793 pred = (1 << Al) - 1;
795 pred = (int)(((Q21 << 7) - num) / (Q21 << 8));
796 if (Al > 0 && pred >= (1 << Al))
797 pred = (1 << Al) - 1;
800 workspace[17] = (JCOEF)pred;
803 if ((Al = coef_bits[9]) != 0 && workspace[24] == 0) {
804 num = Q00 * (DC07 + 2 * DC08 + DC09 - DC17 - 2 * DC18 - DC19);
806 pred = (int)(((Q30 << 7) + num) / (Q30 << 8));
807 if (Al > 0 && pred >= (1 << Al))
808 pred = (1 << Al) - 1;
810 pred = (int)(((Q30 << 7) - num) / (Q30 << 8));
811 if (Al > 0 && pred >= (1 << Al))
812 pred = (1 << Al) - 1;
815 workspace[24] = (JCOEF)pred;
817 /* coef_bits[0] is non-negative. Otherwise this function would not
821 (-2 * DC01 - 6 * DC02 - 8 * DC03 - 6 * DC04 - 2 * DC05 -
822 6 * DC06 + 6 * DC07 + 42 * DC08 + 6 * DC09 - 6 * DC10 -
823 8 * DC11 + 42 * DC12 + 152 * DC13 + 42 * DC14 - 8 * DC15 -
824 6 * DC16 + 6 * DC17 + 42 * DC18 + 6 * DC19 - 6 * DC20 -
825 2 * DC21 - 6 * DC22 - 8 * DC23 - 6 * DC24 - 2 * DC25);
827 pred = (int)(((Q00 << 7) + num) / (Q00 << 8));
829 pred = (int)(((Q00 << 7) - num) / (Q00 << 8));
832 workspace[0] = (JCOEF)pred;
835 /* OK, do the IDCT */
836 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR)workspace, output_ptr,
838 /* Advance for next column */
839 DC01 = DC02; DC02 = DC03; DC03 = DC04; DC04 = DC05;
840 DC06 = DC07; DC07 = DC08; DC08 = DC09; DC09 = DC10;
841 DC11 = DC12; DC12 = DC13; DC13 = DC14; DC14 = DC15;
842 DC16 = DC17; DC17 = DC18; DC18 = DC19; DC19 = DC20;
843 DC21 = DC22; DC22 = DC23; DC23 = DC24; DC24 = DC25;
844 buffer_ptr++, prev_block_row++, next_block_row++,
845 prev_prev_block_row++, next_next_block_row++;
846 output_col += compptr->_DCT_scaled_size;
848 output_ptr += compptr->_DCT_scaled_size;
852 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
853 return JPEG_ROW_COMPLETED;
854 return JPEG_SCAN_COMPLETED;
857 #endif /* BLOCK_SMOOTHING_SUPPORTED */
861 * Initialize coefficient buffer controller.
865 jinit_d_coef_controller(j_decompress_ptr cinfo, boolean need_full_buffer)
870 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
871 sizeof(my_coef_controller));
872 cinfo->coef = (struct jpeg_d_coef_controller *)coef;
873 coef->pub.start_input_pass = start_input_pass;
874 coef->pub.start_output_pass = start_output_pass;
875 #ifdef BLOCK_SMOOTHING_SUPPORTED
876 coef->coef_bits_latch = NULL;
879 /* Create the coefficient buffer. */
880 if (need_full_buffer) {
881 #ifdef D_MULTISCAN_FILES_SUPPORTED
882 /* Allocate a full-image virtual array for each component, */
883 /* padded to a multiple of samp_factor DCT blocks in each direction. */
884 /* Note we ask for a pre-zeroed array. */
886 jpeg_component_info *compptr;
888 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
890 access_rows = compptr->v_samp_factor;
891 #ifdef BLOCK_SMOOTHING_SUPPORTED
892 /* If block smoothing could be used, need a bigger window */
893 if (cinfo->progressive_mode)
896 coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
897 ((j_common_ptr)cinfo, JPOOL_IMAGE, TRUE,
898 (JDIMENSION)jround_up((long)compptr->width_in_blocks,
899 (long)compptr->h_samp_factor),
900 (JDIMENSION)jround_up((long)compptr->height_in_blocks,
901 (long)compptr->v_samp_factor),
902 (JDIMENSION)access_rows);
904 coef->pub.consume_data = consume_data;
905 coef->pub.decompress_data = decompress_data;
906 coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
908 ERREXIT(cinfo, JERR_NOT_COMPILED);
911 /* We only need a single-MCU buffer. */
916 (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
917 D_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
918 for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
919 coef->MCU_buffer[i] = buffer + i;
921 coef->pub.consume_data = dummy_consume_data;
922 coef->pub.decompress_data = decompress_onepass;
923 coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
926 /* Allocate the workspace buffer */
927 coef->workspace = (JCOEF *)
928 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
929 sizeof(JCOEF) * DCTSIZE2);