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 * (On 80x86, the workspace is FAR even though it's not really very big;
46 * this is to keep the module interfaces unchanged when a large coefficient
47 * buffer is necessary.)
48 * In multi-pass modes, this array points to the current MCU's blocks
49 * within the virtual arrays; it is used only by the input side.
51 JBLOCKROW MCU_buffer
[D_MAX_BLOCKS_IN_MCU
];
53 /* Temporary workspace for one MCU */
56 #ifdef D_MULTISCAN_FILES_SUPPORTED
57 /* In multi-pass modes, we need a virtual block array for each component. */
58 jvirt_barray_ptr whole_image
[MAX_COMPONENTS
];
61 #ifdef BLOCK_SMOOTHING_SUPPORTED
62 /* When doing block smoothing, we latch coefficient Al values here */
63 int * coef_bits_latch
;
64 #define SAVED_COEFS 6 /* we save coef_bits[0..5] */
68 typedef my_coef_controller
* my_coef_ptr
;
70 /* Forward declarations */
71 METHODDEF(int) decompress_onepass
72 JPP((j_decompress_ptr cinfo
, JSAMPIMAGE output_buf
));
73 #ifdef D_MULTISCAN_FILES_SUPPORTED
74 METHODDEF(int) decompress_data
75 JPP((j_decompress_ptr cinfo
, JSAMPIMAGE output_buf
));
77 #ifdef BLOCK_SMOOTHING_SUPPORTED
78 LOCAL(boolean
) smoothing_ok
JPP((j_decompress_ptr cinfo
));
79 METHODDEF(int) decompress_smooth_data
80 JPP((j_decompress_ptr cinfo
, JSAMPIMAGE output_buf
));
85 start_iMCU_row (j_decompress_ptr cinfo
)
86 /* Reset within-iMCU-row counters for a new row (input side) */
88 my_coef_ptr coef
= (my_coef_ptr
) cinfo
->coef
;
90 /* In an interleaved scan, an MCU row is the same as an iMCU row.
91 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
92 * But at the bottom of the image, process only what's left.
94 if (cinfo
->comps_in_scan
> 1) {
95 coef
->MCU_rows_per_iMCU_row
= 1;
97 if (cinfo
->input_iMCU_row
< (cinfo
->total_iMCU_rows
-1))
98 coef
->MCU_rows_per_iMCU_row
= cinfo
->cur_comp_info
[0]->v_samp_factor
;
100 coef
->MCU_rows_per_iMCU_row
= cinfo
->cur_comp_info
[0]->last_row_height
;
104 coef
->MCU_vert_offset
= 0;
109 * Initialize for an input processing pass.
113 start_input_pass (j_decompress_ptr cinfo
)
115 cinfo
->input_iMCU_row
= 0;
116 start_iMCU_row(cinfo
);
121 * Initialize for an output processing pass.
125 start_output_pass (j_decompress_ptr cinfo
)
127 #ifdef BLOCK_SMOOTHING_SUPPORTED
128 my_coef_ptr coef
= (my_coef_ptr
) cinfo
->coef
;
130 /* If multipass, check to see whether to use block smoothing on this pass */
131 if (coef
->pub
.coef_arrays
!= NULL
) {
132 if (cinfo
->do_block_smoothing
&& smoothing_ok(cinfo
))
133 coef
->pub
.decompress_data
= decompress_smooth_data
;
135 coef
->pub
.decompress_data
= decompress_data
;
138 cinfo
->output_iMCU_row
= 0;
143 * Decompress and return some data in the single-pass case.
144 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
145 * Input and output must run in lockstep since we have only a one-MCU buffer.
146 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
148 * NB: output_buf contains a plane for each component in image,
149 * which we index according to the component's SOF position.
153 decompress_onepass (j_decompress_ptr cinfo
, JSAMPIMAGE output_buf
)
155 my_coef_ptr coef
= (my_coef_ptr
) cinfo
->coef
;
156 JDIMENSION MCU_col_num
; /* index of current MCU within row */
157 JDIMENSION last_MCU_col
= cinfo
->MCUs_per_row
- 1;
158 JDIMENSION last_iMCU_row
= cinfo
->total_iMCU_rows
- 1;
159 int blkn
, ci
, xindex
, yindex
, yoffset
, useful_width
;
160 JSAMPARRAY output_ptr
;
161 JDIMENSION start_col
, output_col
;
162 jpeg_component_info
*compptr
;
163 inverse_DCT_method_ptr inverse_DCT
;
165 /* Loop to process as much as one whole iMCU row */
166 for (yoffset
= coef
->MCU_vert_offset
; yoffset
< coef
->MCU_rows_per_iMCU_row
;
168 for (MCU_col_num
= coef
->MCU_ctr
; MCU_col_num
<= last_MCU_col
;
170 /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
171 jzero_far((void FAR
*) coef
->MCU_buffer
[0],
172 (size_t) (cinfo
->blocks_in_MCU
* SIZEOF(JBLOCK
)));
173 if (! (*cinfo
->entropy
->decode_mcu
) (cinfo
, coef
->MCU_buffer
)) {
174 /* Suspension forced; update state counters and exit */
175 coef
->MCU_vert_offset
= yoffset
;
176 coef
->MCU_ctr
= MCU_col_num
;
177 return JPEG_SUSPENDED
;
179 /* Determine where data should go in output_buf and do the IDCT thing.
180 * We skip dummy blocks at the right and bottom edges (but blkn gets
181 * incremented past them!). Note the inner loop relies on having
182 * allocated the MCU_buffer[] blocks sequentially.
184 blkn
= 0; /* index of current DCT block within MCU */
185 for (ci
= 0; ci
< cinfo
->comps_in_scan
; ci
++) {
186 compptr
= cinfo
->cur_comp_info
[ci
];
187 /* Don't bother to IDCT an uninteresting component. */
188 if (! compptr
->component_needed
) {
189 blkn
+= compptr
->MCU_blocks
;
192 inverse_DCT
= cinfo
->idct
->inverse_DCT
[compptr
->component_index
];
193 useful_width
= (MCU_col_num
< last_MCU_col
) ? compptr
->MCU_width
194 : compptr
->last_col_width
;
195 output_ptr
= output_buf
[compptr
->component_index
] +
196 yoffset
* compptr
->_DCT_scaled_size
;
197 start_col
= MCU_col_num
* compptr
->MCU_sample_width
;
198 for (yindex
= 0; yindex
< compptr
->MCU_height
; yindex
++) {
199 if (cinfo
->input_iMCU_row
< last_iMCU_row
||
200 yoffset
+yindex
< compptr
->last_row_height
) {
201 output_col
= start_col
;
202 for (xindex
= 0; xindex
< useful_width
; xindex
++) {
203 (*inverse_DCT
) (cinfo
, compptr
,
204 (JCOEFPTR
) coef
->MCU_buffer
[blkn
+xindex
],
205 output_ptr
, output_col
);
206 output_col
+= compptr
->_DCT_scaled_size
;
209 blkn
+= compptr
->MCU_width
;
210 output_ptr
+= compptr
->_DCT_scaled_size
;
214 /* Completed an MCU row, but perhaps not an iMCU row */
217 /* Completed the iMCU row, advance counters for next one */
218 cinfo
->output_iMCU_row
++;
219 if (++(cinfo
->input_iMCU_row
) < cinfo
->total_iMCU_rows
) {
220 start_iMCU_row(cinfo
);
221 return JPEG_ROW_COMPLETED
;
223 /* Completed the scan */
224 (*cinfo
->inputctl
->finish_input_pass
) (cinfo
);
225 return JPEG_SCAN_COMPLETED
;
230 * Dummy consume-input routine for single-pass operation.
234 dummy_consume_data (j_decompress_ptr cinfo
)
236 return JPEG_SUSPENDED
; /* Always indicate nothing was done */
240 #ifdef D_MULTISCAN_FILES_SUPPORTED
243 * Consume input data and store it in the full-image coefficient buffer.
244 * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
245 * ie, v_samp_factor block rows for each component in the scan.
246 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
250 consume_data (j_decompress_ptr cinfo
)
252 my_coef_ptr coef
= (my_coef_ptr
) cinfo
->coef
;
253 JDIMENSION MCU_col_num
; /* index of current MCU within row */
254 int blkn
, ci
, xindex
, yindex
, yoffset
;
255 JDIMENSION start_col
;
256 JBLOCKARRAY buffer
[MAX_COMPS_IN_SCAN
];
257 JBLOCKROW buffer_ptr
;
258 jpeg_component_info
*compptr
;
260 /* Align the virtual buffers for the components used in this scan. */
261 for (ci
= 0; ci
< cinfo
->comps_in_scan
; ci
++) {
262 compptr
= cinfo
->cur_comp_info
[ci
];
263 buffer
[ci
] = (*cinfo
->mem
->access_virt_barray
)
264 ((j_common_ptr
) cinfo
, coef
->whole_image
[compptr
->component_index
],
265 cinfo
->input_iMCU_row
* compptr
->v_samp_factor
,
266 (JDIMENSION
) compptr
->v_samp_factor
, TRUE
);
267 /* Note: entropy decoder expects buffer to be zeroed,
268 * but this is handled automatically by the memory manager
269 * because we requested a pre-zeroed array.
273 /* Loop to process one whole iMCU row */
274 for (yoffset
= coef
->MCU_vert_offset
; yoffset
< coef
->MCU_rows_per_iMCU_row
;
276 for (MCU_col_num
= coef
->MCU_ctr
; MCU_col_num
< cinfo
->MCUs_per_row
;
278 /* Construct list of pointers to DCT blocks belonging to this MCU */
279 blkn
= 0; /* index of current DCT block within MCU */
280 for (ci
= 0; ci
< cinfo
->comps_in_scan
; ci
++) {
281 compptr
= cinfo
->cur_comp_info
[ci
];
282 start_col
= MCU_col_num
* compptr
->MCU_width
;
283 for (yindex
= 0; yindex
< compptr
->MCU_height
; yindex
++) {
284 buffer_ptr
= buffer
[ci
][yindex
+yoffset
] + start_col
;
285 for (xindex
= 0; xindex
< compptr
->MCU_width
; xindex
++) {
286 coef
->MCU_buffer
[blkn
++] = buffer_ptr
++;
290 /* Try to fetch the MCU. */
291 if (! (*cinfo
->entropy
->decode_mcu
) (cinfo
, coef
->MCU_buffer
)) {
292 /* Suspension forced; update state counters and exit */
293 coef
->MCU_vert_offset
= yoffset
;
294 coef
->MCU_ctr
= MCU_col_num
;
295 return JPEG_SUSPENDED
;
298 /* Completed an MCU row, but perhaps not an iMCU row */
301 /* Completed the iMCU row, advance counters for next one */
302 if (++(cinfo
->input_iMCU_row
) < cinfo
->total_iMCU_rows
) {
303 start_iMCU_row(cinfo
);
304 return JPEG_ROW_COMPLETED
;
306 /* Completed the scan */
307 (*cinfo
->inputctl
->finish_input_pass
) (cinfo
);
308 return JPEG_SCAN_COMPLETED
;
313 * Decompress and return some data in the multi-pass case.
314 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
315 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
317 * NB: output_buf contains a plane for each component in image.
321 decompress_data (j_decompress_ptr cinfo
, JSAMPIMAGE output_buf
)
323 my_coef_ptr coef
= (my_coef_ptr
) cinfo
->coef
;
324 JDIMENSION last_iMCU_row
= cinfo
->total_iMCU_rows
- 1;
325 JDIMENSION block_num
;
326 int ci
, block_row
, block_rows
;
328 JBLOCKROW buffer_ptr
;
329 JSAMPARRAY output_ptr
;
330 JDIMENSION output_col
;
331 jpeg_component_info
*compptr
;
332 inverse_DCT_method_ptr inverse_DCT
;
334 /* Force some input to be done if we are getting ahead of the input. */
335 while (cinfo
->input_scan_number
< cinfo
->output_scan_number
||
336 (cinfo
->input_scan_number
== cinfo
->output_scan_number
&&
337 cinfo
->input_iMCU_row
<= cinfo
->output_iMCU_row
)) {
338 if ((*cinfo
->inputctl
->consume_input
)(cinfo
) == JPEG_SUSPENDED
)
339 return JPEG_SUSPENDED
;
342 /* OK, output from the virtual arrays. */
343 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
345 /* Don't bother to IDCT an uninteresting component. */
346 if (! compptr
->component_needed
)
348 /* Align the virtual buffer for this component. */
349 buffer
= (*cinfo
->mem
->access_virt_barray
)
350 ((j_common_ptr
) cinfo
, coef
->whole_image
[ci
],
351 cinfo
->output_iMCU_row
* compptr
->v_samp_factor
,
352 (JDIMENSION
) compptr
->v_samp_factor
, FALSE
);
353 /* Count non-dummy DCT block rows in this iMCU row. */
354 if (cinfo
->output_iMCU_row
< last_iMCU_row
)
355 block_rows
= compptr
->v_samp_factor
;
357 /* NB: can't use last_row_height here; it is input-side-dependent! */
358 block_rows
= (int) (compptr
->height_in_blocks
% compptr
->v_samp_factor
);
359 if (block_rows
== 0) block_rows
= compptr
->v_samp_factor
;
361 inverse_DCT
= cinfo
->idct
->inverse_DCT
[ci
];
362 output_ptr
= output_buf
[ci
];
363 /* Loop over all DCT blocks to be processed. */
364 for (block_row
= 0; block_row
< block_rows
; block_row
++) {
365 buffer_ptr
= buffer
[block_row
];
367 for (block_num
= 0; block_num
< compptr
->width_in_blocks
; block_num
++) {
368 (*inverse_DCT
) (cinfo
, compptr
, (JCOEFPTR
) buffer_ptr
,
369 output_ptr
, output_col
);
371 output_col
+= compptr
->_DCT_scaled_size
;
373 output_ptr
+= compptr
->_DCT_scaled_size
;
377 if (++(cinfo
->output_iMCU_row
) < cinfo
->total_iMCU_rows
)
378 return JPEG_ROW_COMPLETED
;
379 return JPEG_SCAN_COMPLETED
;
382 #endif /* D_MULTISCAN_FILES_SUPPORTED */
385 #ifdef BLOCK_SMOOTHING_SUPPORTED
388 * This code applies interblock smoothing as described by section K.8
389 * of the JPEG standard: the first 5 AC coefficients are estimated from
390 * the DC values of a DCT block and its 8 neighboring blocks.
391 * We apply smoothing only for progressive JPEG decoding, and only if
392 * the coefficients it can estimate are not yet known to full precision.
395 /* Natural-order array positions of the first 5 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
;
419 int * 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
*
429 (SAVED_COEFS
* SIZEOF(int)));
430 coef_bits_latch
= coef
->coef_bits_latch
;
432 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
434 /* All components' quantization values must already be latched. */
435 if ((qtable
= compptr
->quant_table
) == NULL
)
437 /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
438 if (qtable
->quantval
[0] == 0 ||
439 qtable
->quantval
[Q01_POS
] == 0 ||
440 qtable
->quantval
[Q10_POS
] == 0 ||
441 qtable
->quantval
[Q20_POS
] == 0 ||
442 qtable
->quantval
[Q11_POS
] == 0 ||
443 qtable
->quantval
[Q02_POS
] == 0)
445 /* DC values must be at least partly known for all components. */
446 coef_bits
= cinfo
->coef_bits
[ci
];
447 if (coef_bits
[0] < 0)
449 /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
450 for (coefi
= 1; coefi
<= 5; coefi
++) {
451 coef_bits_latch
[coefi
] = coef_bits
[coefi
];
452 if (coef_bits
[coefi
] != 0)
453 smoothing_useful
= TRUE
;
455 coef_bits_latch
+= SAVED_COEFS
;
458 return smoothing_useful
;
463 * Variant of decompress_data for use when doing block smoothing.
467 decompress_smooth_data (j_decompress_ptr cinfo
, JSAMPIMAGE output_buf
)
469 my_coef_ptr coef
= (my_coef_ptr
) cinfo
->coef
;
470 JDIMENSION last_iMCU_row
= cinfo
->total_iMCU_rows
- 1;
471 JDIMENSION block_num
, last_block_column
;
472 int ci
, block_row
, block_rows
, access_rows
;
474 JBLOCKROW buffer_ptr
, prev_block_row
, next_block_row
;
475 JSAMPARRAY output_ptr
;
476 JDIMENSION output_col
;
477 jpeg_component_info
*compptr
;
478 inverse_DCT_method_ptr inverse_DCT
;
479 boolean first_row
, last_row
;
482 JQUANT_TBL
*quanttbl
;
483 INT32 Q00
,Q01
,Q02
,Q10
,Q11
,Q20
, num
;
484 int DC1
,DC2
,DC3
,DC4
,DC5
,DC6
,DC7
,DC8
,DC9
;
487 /* Keep a local variable to avoid looking it up more than once */
488 workspace
= coef
->workspace
;
490 /* Force some input to be done if we are getting ahead of the input. */
491 while (cinfo
->input_scan_number
<= cinfo
->output_scan_number
&&
492 ! cinfo
->inputctl
->eoi_reached
) {
493 if (cinfo
->input_scan_number
== cinfo
->output_scan_number
) {
494 /* If input is working on current scan, we ordinarily want it to
495 * have completed the current row. But if input scan is DC,
496 * we want it to keep one row ahead so that next block row's DC
497 * values are up to date.
499 JDIMENSION delta
= (cinfo
->Ss
== 0) ? 1 : 0;
500 if (cinfo
->input_iMCU_row
> cinfo
->output_iMCU_row
+delta
)
503 if ((*cinfo
->inputctl
->consume_input
)(cinfo
) == JPEG_SUSPENDED
)
504 return JPEG_SUSPENDED
;
507 /* OK, output from the virtual arrays. */
508 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
510 /* Don't bother to IDCT an uninteresting component. */
511 if (! compptr
->component_needed
)
513 /* Count non-dummy DCT block rows in this iMCU row. */
514 if (cinfo
->output_iMCU_row
< last_iMCU_row
) {
515 block_rows
= compptr
->v_samp_factor
;
516 access_rows
= block_rows
* 2; /* this and next iMCU row */
519 /* NB: can't use last_row_height here; it is input-side-dependent! */
520 block_rows
= (int) (compptr
->height_in_blocks
% compptr
->v_samp_factor
);
521 if (block_rows
== 0) block_rows
= compptr
->v_samp_factor
;
522 access_rows
= block_rows
; /* this iMCU row only */
525 /* Align the virtual buffer for this component. */
526 if (cinfo
->output_iMCU_row
> 0) {
527 access_rows
+= compptr
->v_samp_factor
; /* prior iMCU row too */
528 buffer
= (*cinfo
->mem
->access_virt_barray
)
529 ((j_common_ptr
) cinfo
, coef
->whole_image
[ci
],
530 (cinfo
->output_iMCU_row
- 1) * compptr
->v_samp_factor
,
531 (JDIMENSION
) access_rows
, FALSE
);
532 buffer
+= compptr
->v_samp_factor
; /* point to current iMCU row */
535 buffer
= (*cinfo
->mem
->access_virt_barray
)
536 ((j_common_ptr
) cinfo
, coef
->whole_image
[ci
],
537 (JDIMENSION
) 0, (JDIMENSION
) access_rows
, FALSE
);
540 /* Fetch component-dependent info */
541 coef_bits
= coef
->coef_bits_latch
+ (ci
* SAVED_COEFS
);
542 quanttbl
= compptr
->quant_table
;
543 Q00
= quanttbl
->quantval
[0];
544 Q01
= quanttbl
->quantval
[Q01_POS
];
545 Q10
= quanttbl
->quantval
[Q10_POS
];
546 Q20
= quanttbl
->quantval
[Q20_POS
];
547 Q11
= quanttbl
->quantval
[Q11_POS
];
548 Q02
= quanttbl
->quantval
[Q02_POS
];
549 inverse_DCT
= cinfo
->idct
->inverse_DCT
[ci
];
550 output_ptr
= output_buf
[ci
];
551 /* Loop over all DCT blocks to be processed. */
552 for (block_row
= 0; block_row
< block_rows
; block_row
++) {
553 buffer_ptr
= buffer
[block_row
];
554 if (first_row
&& block_row
== 0)
555 prev_block_row
= buffer_ptr
;
557 prev_block_row
= buffer
[block_row
-1];
558 if (last_row
&& block_row
== block_rows
-1)
559 next_block_row
= buffer_ptr
;
561 next_block_row
= buffer
[block_row
+1];
562 /* We fetch the surrounding DC values using a sliding-register approach.
563 * Initialize all nine here so as to do the right thing on narrow pics.
565 DC1
= DC2
= DC3
= (int) prev_block_row
[0][0];
566 DC4
= DC5
= DC6
= (int) buffer_ptr
[0][0];
567 DC7
= DC8
= DC9
= (int) next_block_row
[0][0];
569 last_block_column
= compptr
->width_in_blocks
- 1;
570 for (block_num
= 0; block_num
<= last_block_column
; block_num
++) {
571 /* Fetch current DCT block into workspace so we can modify it. */
572 jcopy_block_row(buffer_ptr
, (JBLOCKROW
) workspace
, (JDIMENSION
) 1);
573 /* Update DC values */
574 if (block_num
< last_block_column
) {
575 DC3
= (int) prev_block_row
[1][0];
576 DC6
= (int) buffer_ptr
[1][0];
577 DC9
= (int) next_block_row
[1][0];
579 /* Compute coefficient estimates per K.8.
580 * An estimate is applied only if coefficient is still zero,
581 * and is not known to be fully accurate.
584 if ((Al
=coef_bits
[1]) != 0 && workspace
[1] == 0) {
585 num
= 36 * Q00
* (DC4
- DC6
);
587 pred
= (int) (((Q01
<<7) + num
) / (Q01
<<8));
588 if (Al
> 0 && pred
>= (1<<Al
))
591 pred
= (int) (((Q01
<<7) - num
) / (Q01
<<8));
592 if (Al
> 0 && pred
>= (1<<Al
))
596 workspace
[1] = (JCOEF
) pred
;
599 if ((Al
=coef_bits
[2]) != 0 && workspace
[8] == 0) {
600 num
= 36 * Q00
* (DC2
- DC8
);
602 pred
= (int) (((Q10
<<7) + num
) / (Q10
<<8));
603 if (Al
> 0 && pred
>= (1<<Al
))
606 pred
= (int) (((Q10
<<7) - num
) / (Q10
<<8));
607 if (Al
> 0 && pred
>= (1<<Al
))
611 workspace
[8] = (JCOEF
) pred
;
614 if ((Al
=coef_bits
[3]) != 0 && workspace
[16] == 0) {
615 num
= 9 * Q00
* (DC2
+ DC8
- 2*DC5
);
617 pred
= (int) (((Q20
<<7) + num
) / (Q20
<<8));
618 if (Al
> 0 && pred
>= (1<<Al
))
621 pred
= (int) (((Q20
<<7) - num
) / (Q20
<<8));
622 if (Al
> 0 && pred
>= (1<<Al
))
626 workspace
[16] = (JCOEF
) pred
;
629 if ((Al
=coef_bits
[4]) != 0 && workspace
[9] == 0) {
630 num
= 5 * Q00
* (DC1
- DC3
- DC7
+ DC9
);
632 pred
= (int) (((Q11
<<7) + num
) / (Q11
<<8));
633 if (Al
> 0 && pred
>= (1<<Al
))
636 pred
= (int) (((Q11
<<7) - num
) / (Q11
<<8));
637 if (Al
> 0 && pred
>= (1<<Al
))
641 workspace
[9] = (JCOEF
) pred
;
644 if ((Al
=coef_bits
[5]) != 0 && workspace
[2] == 0) {
645 num
= 9 * Q00
* (DC4
+ DC6
- 2*DC5
);
647 pred
= (int) (((Q02
<<7) + num
) / (Q02
<<8));
648 if (Al
> 0 && pred
>= (1<<Al
))
651 pred
= (int) (((Q02
<<7) - num
) / (Q02
<<8));
652 if (Al
> 0 && pred
>= (1<<Al
))
656 workspace
[2] = (JCOEF
) pred
;
658 /* OK, do the IDCT */
659 (*inverse_DCT
) (cinfo
, compptr
, (JCOEFPTR
) workspace
,
660 output_ptr
, output_col
);
661 /* Advance for next column */
662 DC1
= DC2
; DC2
= DC3
;
663 DC4
= DC5
; DC5
= DC6
;
664 DC7
= DC8
; DC8
= DC9
;
665 buffer_ptr
++, prev_block_row
++, next_block_row
++;
666 output_col
+= compptr
->_DCT_scaled_size
;
668 output_ptr
+= compptr
->_DCT_scaled_size
;
672 if (++(cinfo
->output_iMCU_row
) < cinfo
->total_iMCU_rows
)
673 return JPEG_ROW_COMPLETED
;
674 return JPEG_SCAN_COMPLETED
;
677 #endif /* BLOCK_SMOOTHING_SUPPORTED */
681 * Initialize coefficient buffer controller.
685 jinit_d_coef_controller (j_decompress_ptr cinfo
, boolean need_full_buffer
)
690 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
691 SIZEOF(my_coef_controller
));
692 cinfo
->coef
= (struct jpeg_d_coef_controller
*) coef
;
693 coef
->pub
.start_input_pass
= start_input_pass
;
694 coef
->pub
.start_output_pass
= start_output_pass
;
695 #ifdef BLOCK_SMOOTHING_SUPPORTED
696 coef
->coef_bits_latch
= NULL
;
699 /* Create the coefficient buffer. */
700 if (need_full_buffer
) {
701 #ifdef D_MULTISCAN_FILES_SUPPORTED
702 /* Allocate a full-image virtual array for each component, */
703 /* padded to a multiple of samp_factor DCT blocks in each direction. */
704 /* Note we ask for a pre-zeroed array. */
706 jpeg_component_info
*compptr
;
708 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
710 access_rows
= compptr
->v_samp_factor
;
711 #ifdef BLOCK_SMOOTHING_SUPPORTED
712 /* If block smoothing could be used, need a bigger window */
713 if (cinfo
->progressive_mode
)
716 coef
->whole_image
[ci
] = (*cinfo
->mem
->request_virt_barray
)
717 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
, TRUE
,
718 (JDIMENSION
) jround_up((long) compptr
->width_in_blocks
,
719 (long) compptr
->h_samp_factor
),
720 (JDIMENSION
) jround_up((long) compptr
->height_in_blocks
,
721 (long) compptr
->v_samp_factor
),
722 (JDIMENSION
) access_rows
);
724 coef
->pub
.consume_data
= consume_data
;
725 coef
->pub
.decompress_data
= decompress_data
;
726 coef
->pub
.coef_arrays
= coef
->whole_image
; /* link to virtual arrays */
728 ERREXIT(cinfo
, JERR_NOT_COMPILED
);
731 /* We only need a single-MCU buffer. */
736 (*cinfo
->mem
->alloc_large
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
737 D_MAX_BLOCKS_IN_MCU
* SIZEOF(JBLOCK
));
738 for (i
= 0; i
< D_MAX_BLOCKS_IN_MCU
; i
++) {
739 coef
->MCU_buffer
[i
] = buffer
+ i
;
741 coef
->pub
.consume_data
= dummy_consume_data
;
742 coef
->pub
.decompress_data
= decompress_onepass
;
743 coef
->pub
.coef_arrays
= NULL
; /* flag for no virtual arrays */
746 /* Allocate the workspace buffer */
747 coef
->workspace
= (JCOEF
*)
748 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
749 SIZEOF(JCOEF
) * DCTSIZE2
);