Line data Source code
1 : /*
2 : * jdcoefct.c
3 : *
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, Google, Inc.
10 : * For conditions of distribution and use, see the accompanying README.ijg
11 : * file.
12 : *
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.
16 : *
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.
20 : */
21 :
22 : #include "jinclude.h"
23 : #include "jdcoefct.h"
24 : #include "jpegcomp.h"
25 :
26 :
27 : /* Forward declarations */
28 : METHODDEF(int) decompress_onepass
29 : (j_decompress_ptr cinfo, JSAMPIMAGE output_buf);
30 : #ifdef D_MULTISCAN_FILES_SUPPORTED
31 : METHODDEF(int) decompress_data
32 : (j_decompress_ptr cinfo, JSAMPIMAGE output_buf);
33 : #endif
34 : #ifdef BLOCK_SMOOTHING_SUPPORTED
35 : LOCAL(boolean) smoothing_ok (j_decompress_ptr cinfo);
36 : METHODDEF(int) decompress_smooth_data
37 : (j_decompress_ptr cinfo, JSAMPIMAGE output_buf);
38 : #endif
39 :
40 :
41 : /*
42 : * Initialize for an input processing pass.
43 : */
44 :
45 : METHODDEF(void)
46 0 : start_input_pass (j_decompress_ptr cinfo)
47 : {
48 0 : cinfo->input_iMCU_row = 0;
49 0 : start_iMCU_row(cinfo);
50 0 : }
51 :
52 :
53 : /*
54 : * Initialize for an output processing pass.
55 : */
56 :
57 : METHODDEF(void)
58 0 : start_output_pass (j_decompress_ptr cinfo)
59 : {
60 : #ifdef BLOCK_SMOOTHING_SUPPORTED
61 0 : my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
62 :
63 : /* If multipass, check to see whether to use block smoothing on this pass */
64 0 : if (coef->pub.coef_arrays != NULL) {
65 0 : if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
66 0 : coef->pub.decompress_data = decompress_smooth_data;
67 : else
68 0 : coef->pub.decompress_data = decompress_data;
69 : }
70 : #endif
71 0 : cinfo->output_iMCU_row = 0;
72 0 : }
73 :
74 :
75 : /*
76 : * Decompress and return some data in the single-pass case.
77 : * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
78 : * Input and output must run in lockstep since we have only a one-MCU buffer.
79 : * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
80 : *
81 : * NB: output_buf contains a plane for each component in image,
82 : * which we index according to the component's SOF position.
83 : */
84 :
85 : METHODDEF(int)
86 0 : decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
87 : {
88 0 : my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
89 : JDIMENSION MCU_col_num; /* index of current MCU within row */
90 0 : JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
91 0 : JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
92 : int blkn, ci, xindex, yindex, yoffset, useful_width;
93 : JSAMPARRAY output_ptr;
94 : JDIMENSION start_col, output_col;
95 : jpeg_component_info *compptr;
96 : inverse_DCT_method_ptr inverse_DCT;
97 :
98 : /* Loop to process as much as one whole iMCU row */
99 0 : for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
100 0 : yoffset++) {
101 0 : for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
102 0 : MCU_col_num++) {
103 : /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
104 0 : jzero_far((void *) coef->MCU_buffer[0],
105 0 : (size_t) (cinfo->blocks_in_MCU * sizeof(JBLOCK)));
106 0 : if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
107 : /* Suspension forced; update state counters and exit */
108 0 : coef->MCU_vert_offset = yoffset;
109 0 : coef->MCU_ctr = MCU_col_num;
110 0 : return JPEG_SUSPENDED;
111 : }
112 :
113 : /* Only perform the IDCT on blocks that are contained within the desired
114 : * cropping region.
115 : */
116 0 : if (MCU_col_num >= cinfo->master->first_iMCU_col &&
117 0 : MCU_col_num <= cinfo->master->last_iMCU_col) {
118 : /* Determine where data should go in output_buf and do the IDCT thing.
119 : * We skip dummy blocks at the right and bottom edges (but blkn gets
120 : * incremented past them!). Note the inner loop relies on having
121 : * allocated the MCU_buffer[] blocks sequentially.
122 : */
123 0 : blkn = 0; /* index of current DCT block within MCU */
124 0 : for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
125 0 : compptr = cinfo->cur_comp_info[ci];
126 : /* Don't bother to IDCT an uninteresting component. */
127 0 : if (! compptr->component_needed) {
128 0 : blkn += compptr->MCU_blocks;
129 0 : continue;
130 : }
131 0 : inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
132 0 : useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
133 0 : : compptr->last_col_width;
134 0 : output_ptr = output_buf[compptr->component_index] +
135 0 : yoffset * compptr->_DCT_scaled_size;
136 0 : start_col = (MCU_col_num - cinfo->master->first_iMCU_col) *
137 0 : compptr->MCU_sample_width;
138 0 : for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
139 0 : if (cinfo->input_iMCU_row < last_iMCU_row ||
140 0 : yoffset+yindex < compptr->last_row_height) {
141 0 : output_col = start_col;
142 0 : for (xindex = 0; xindex < useful_width; xindex++) {
143 0 : (*inverse_DCT) (cinfo, compptr,
144 0 : (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
145 : output_ptr, output_col);
146 0 : output_col += compptr->_DCT_scaled_size;
147 : }
148 : }
149 0 : blkn += compptr->MCU_width;
150 0 : output_ptr += compptr->_DCT_scaled_size;
151 : }
152 : }
153 : }
154 : }
155 : /* Completed an MCU row, but perhaps not an iMCU row */
156 0 : coef->MCU_ctr = 0;
157 : }
158 : /* Completed the iMCU row, advance counters for next one */
159 0 : cinfo->output_iMCU_row++;
160 0 : if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
161 0 : start_iMCU_row(cinfo);
162 0 : return JPEG_ROW_COMPLETED;
163 : }
164 : /* Completed the scan */
165 0 : (*cinfo->inputctl->finish_input_pass) (cinfo);
166 0 : return JPEG_SCAN_COMPLETED;
167 : }
168 :
169 :
170 : /*
171 : * Dummy consume-input routine for single-pass operation.
172 : */
173 :
174 : METHODDEF(int)
175 0 : dummy_consume_data (j_decompress_ptr cinfo)
176 : {
177 0 : return JPEG_SUSPENDED; /* Always indicate nothing was done */
178 : }
179 :
180 :
181 : #ifdef D_MULTISCAN_FILES_SUPPORTED
182 :
183 : /*
184 : * Consume input data and store it in the full-image coefficient buffer.
185 : * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
186 : * ie, v_samp_factor block rows for each component in the scan.
187 : * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
188 : */
189 :
190 : METHODDEF(int)
191 0 : consume_data (j_decompress_ptr cinfo)
192 : {
193 0 : my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
194 : JDIMENSION MCU_col_num; /* index of current MCU within row */
195 : int blkn, ci, xindex, yindex, yoffset;
196 : JDIMENSION start_col;
197 : JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
198 : JBLOCKROW buffer_ptr;
199 : jpeg_component_info *compptr;
200 :
201 : /* Align the virtual buffers for the components used in this scan. */
202 0 : for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
203 0 : compptr = cinfo->cur_comp_info[ci];
204 0 : buffer[ci] = (*cinfo->mem->access_virt_barray)
205 0 : ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
206 0 : cinfo->input_iMCU_row * compptr->v_samp_factor,
207 0 : (JDIMENSION) compptr->v_samp_factor, TRUE);
208 : /* Note: entropy decoder expects buffer to be zeroed,
209 : * but this is handled automatically by the memory manager
210 : * because we requested a pre-zeroed array.
211 : */
212 : }
213 :
214 : /* Loop to process one whole iMCU row */
215 0 : for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
216 0 : yoffset++) {
217 0 : for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
218 0 : MCU_col_num++) {
219 : /* Construct list of pointers to DCT blocks belonging to this MCU */
220 0 : blkn = 0; /* index of current DCT block within MCU */
221 0 : for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
222 0 : compptr = cinfo->cur_comp_info[ci];
223 0 : start_col = MCU_col_num * compptr->MCU_width;
224 0 : for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
225 0 : buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
226 0 : for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
227 0 : coef->MCU_buffer[blkn++] = buffer_ptr++;
228 : }
229 : }
230 : }
231 : /* Try to fetch the MCU. */
232 0 : if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
233 : /* Suspension forced; update state counters and exit */
234 0 : coef->MCU_vert_offset = yoffset;
235 0 : coef->MCU_ctr = MCU_col_num;
236 0 : return JPEG_SUSPENDED;
237 : }
238 : }
239 : /* Completed an MCU row, but perhaps not an iMCU row */
240 0 : coef->MCU_ctr = 0;
241 : }
242 : /* Completed the iMCU row, advance counters for next one */
243 0 : if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
244 0 : start_iMCU_row(cinfo);
245 0 : return JPEG_ROW_COMPLETED;
246 : }
247 : /* Completed the scan */
248 0 : (*cinfo->inputctl->finish_input_pass) (cinfo);
249 0 : return JPEG_SCAN_COMPLETED;
250 : }
251 :
252 :
253 : /*
254 : * Decompress and return some data in the multi-pass case.
255 : * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
256 : * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
257 : *
258 : * NB: output_buf contains a plane for each component in image.
259 : */
260 :
261 : METHODDEF(int)
262 0 : decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
263 : {
264 0 : my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
265 0 : JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
266 : JDIMENSION block_num;
267 : int ci, block_row, block_rows;
268 : JBLOCKARRAY buffer;
269 : JBLOCKROW buffer_ptr;
270 : JSAMPARRAY output_ptr;
271 : JDIMENSION output_col;
272 : jpeg_component_info *compptr;
273 : inverse_DCT_method_ptr inverse_DCT;
274 :
275 : /* Force some input to be done if we are getting ahead of the input. */
276 0 : while (cinfo->input_scan_number < cinfo->output_scan_number ||
277 0 : (cinfo->input_scan_number == cinfo->output_scan_number &&
278 0 : cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
279 0 : if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
280 0 : return JPEG_SUSPENDED;
281 : }
282 :
283 : /* OK, output from the virtual arrays. */
284 0 : for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
285 0 : ci++, compptr++) {
286 : /* Don't bother to IDCT an uninteresting component. */
287 0 : if (! compptr->component_needed)
288 0 : continue;
289 : /* Align the virtual buffer for this component. */
290 0 : buffer = (*cinfo->mem->access_virt_barray)
291 : ((j_common_ptr) cinfo, coef->whole_image[ci],
292 0 : cinfo->output_iMCU_row * compptr->v_samp_factor,
293 0 : (JDIMENSION) compptr->v_samp_factor, FALSE);
294 : /* Count non-dummy DCT block rows in this iMCU row. */
295 0 : if (cinfo->output_iMCU_row < last_iMCU_row)
296 0 : block_rows = compptr->v_samp_factor;
297 : else {
298 : /* NB: can't use last_row_height here; it is input-side-dependent! */
299 0 : block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
300 0 : if (block_rows == 0) block_rows = compptr->v_samp_factor;
301 : }
302 0 : inverse_DCT = cinfo->idct->inverse_DCT[ci];
303 0 : output_ptr = output_buf[ci];
304 : /* Loop over all DCT blocks to be processed. */
305 0 : for (block_row = 0; block_row < block_rows; block_row++) {
306 0 : buffer_ptr = buffer[block_row] + cinfo->master->first_MCU_col[ci];
307 0 : output_col = 0;
308 0 : for (block_num = cinfo->master->first_MCU_col[ci];
309 0 : block_num <= cinfo->master->last_MCU_col[ci]; block_num++) {
310 0 : (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
311 : output_ptr, output_col);
312 0 : buffer_ptr++;
313 0 : output_col += compptr->_DCT_scaled_size;
314 : }
315 0 : output_ptr += compptr->_DCT_scaled_size;
316 : }
317 : }
318 :
319 0 : if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
320 0 : return JPEG_ROW_COMPLETED;
321 0 : return JPEG_SCAN_COMPLETED;
322 : }
323 :
324 : #endif /* D_MULTISCAN_FILES_SUPPORTED */
325 :
326 :
327 : #ifdef BLOCK_SMOOTHING_SUPPORTED
328 :
329 : /*
330 : * This code applies interblock smoothing as described by section K.8
331 : * of the JPEG standard: the first 5 AC coefficients are estimated from
332 : * the DC values of a DCT block and its 8 neighboring blocks.
333 : * We apply smoothing only for progressive JPEG decoding, and only if
334 : * the coefficients it can estimate are not yet known to full precision.
335 : */
336 :
337 : /* Natural-order array positions of the first 5 zigzag-order coefficients */
338 : #define Q01_POS 1
339 : #define Q10_POS 8
340 : #define Q20_POS 16
341 : #define Q11_POS 9
342 : #define Q02_POS 2
343 :
344 : /*
345 : * Determine whether block smoothing is applicable and safe.
346 : * We also latch the current states of the coef_bits[] entries for the
347 : * AC coefficients; otherwise, if the input side of the decompressor
348 : * advances into a new scan, we might think the coefficients are known
349 : * more accurately than they really are.
350 : */
351 :
352 : LOCAL(boolean)
353 0 : smoothing_ok (j_decompress_ptr cinfo)
354 : {
355 0 : my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
356 0 : boolean smoothing_useful = FALSE;
357 : int ci, coefi;
358 : jpeg_component_info *compptr;
359 : JQUANT_TBL *qtable;
360 : int *coef_bits;
361 : int *coef_bits_latch;
362 :
363 0 : if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
364 0 : return FALSE;
365 :
366 : /* Allocate latch area if not already done */
367 0 : if (coef->coef_bits_latch == NULL)
368 0 : coef->coef_bits_latch = (int *)
369 0 : (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
370 0 : cinfo->num_components *
371 : (SAVED_COEFS * sizeof(int)));
372 0 : coef_bits_latch = coef->coef_bits_latch;
373 :
374 0 : for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
375 0 : ci++, compptr++) {
376 : /* All components' quantization values must already be latched. */
377 0 : if ((qtable = compptr->quant_table) == NULL)
378 0 : return FALSE;
379 : /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
380 0 : if (qtable->quantval[0] == 0 ||
381 0 : qtable->quantval[Q01_POS] == 0 ||
382 0 : qtable->quantval[Q10_POS] == 0 ||
383 0 : qtable->quantval[Q20_POS] == 0 ||
384 0 : qtable->quantval[Q11_POS] == 0 ||
385 0 : qtable->quantval[Q02_POS] == 0)
386 0 : return FALSE;
387 : /* DC values must be at least partly known for all components. */
388 0 : coef_bits = cinfo->coef_bits[ci];
389 0 : if (coef_bits[0] < 0)
390 0 : return FALSE;
391 : /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
392 0 : for (coefi = 1; coefi <= 5; coefi++) {
393 0 : coef_bits_latch[coefi] = coef_bits[coefi];
394 0 : if (coef_bits[coefi] != 0)
395 0 : smoothing_useful = TRUE;
396 : }
397 0 : coef_bits_latch += SAVED_COEFS;
398 : }
399 :
400 0 : return smoothing_useful;
401 : }
402 :
403 :
404 : /*
405 : * Variant of decompress_data for use when doing block smoothing.
406 : */
407 :
408 : METHODDEF(int)
409 0 : decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
410 : {
411 0 : my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
412 0 : JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
413 : JDIMENSION block_num, last_block_column;
414 : int ci, block_row, block_rows, access_rows;
415 : JBLOCKARRAY buffer;
416 : JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
417 : JSAMPARRAY output_ptr;
418 : JDIMENSION output_col;
419 : jpeg_component_info *compptr;
420 : inverse_DCT_method_ptr inverse_DCT;
421 : boolean first_row, last_row;
422 : JCOEF *workspace;
423 : int *coef_bits;
424 : JQUANT_TBL *quanttbl;
425 : JLONG Q00,Q01,Q02,Q10,Q11,Q20, num;
426 : int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
427 : int Al, pred;
428 :
429 : /* Keep a local variable to avoid looking it up more than once */
430 0 : workspace = coef->workspace;
431 :
432 : /* Force some input to be done if we are getting ahead of the input. */
433 0 : while (cinfo->input_scan_number <= cinfo->output_scan_number &&
434 0 : ! cinfo->inputctl->eoi_reached) {
435 0 : if (cinfo->input_scan_number == cinfo->output_scan_number) {
436 : /* If input is working on current scan, we ordinarily want it to
437 : * have completed the current row. But if input scan is DC,
438 : * we want it to keep one row ahead so that next block row's DC
439 : * values are up to date.
440 : */
441 0 : JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
442 0 : if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
443 0 : break;
444 : }
445 0 : if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
446 0 : return JPEG_SUSPENDED;
447 : }
448 :
449 : /* OK, output from the virtual arrays. */
450 0 : for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
451 0 : ci++, compptr++) {
452 : /* Don't bother to IDCT an uninteresting component. */
453 0 : if (! compptr->component_needed)
454 0 : continue;
455 : /* Count non-dummy DCT block rows in this iMCU row. */
456 0 : if (cinfo->output_iMCU_row < last_iMCU_row) {
457 0 : block_rows = compptr->v_samp_factor;
458 0 : access_rows = block_rows * 2; /* this and next iMCU row */
459 0 : last_row = FALSE;
460 : } else {
461 : /* NB: can't use last_row_height here; it is input-side-dependent! */
462 0 : block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
463 0 : if (block_rows == 0) block_rows = compptr->v_samp_factor;
464 0 : access_rows = block_rows; /* this iMCU row only */
465 0 : last_row = TRUE;
466 : }
467 : /* Align the virtual buffer for this component. */
468 0 : if (cinfo->output_iMCU_row > 0) {
469 0 : access_rows += compptr->v_samp_factor; /* prior iMCU row too */
470 0 : buffer = (*cinfo->mem->access_virt_barray)
471 : ((j_common_ptr) cinfo, coef->whole_image[ci],
472 0 : (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
473 : (JDIMENSION) access_rows, FALSE);
474 0 : buffer += compptr->v_samp_factor; /* point to current iMCU row */
475 0 : first_row = FALSE;
476 : } else {
477 0 : buffer = (*cinfo->mem->access_virt_barray)
478 : ((j_common_ptr) cinfo, coef->whole_image[ci],
479 : (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
480 0 : first_row = TRUE;
481 : }
482 : /* Fetch component-dependent info */
483 0 : coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
484 0 : quanttbl = compptr->quant_table;
485 0 : Q00 = quanttbl->quantval[0];
486 0 : Q01 = quanttbl->quantval[Q01_POS];
487 0 : Q10 = quanttbl->quantval[Q10_POS];
488 0 : Q20 = quanttbl->quantval[Q20_POS];
489 0 : Q11 = quanttbl->quantval[Q11_POS];
490 0 : Q02 = quanttbl->quantval[Q02_POS];
491 0 : inverse_DCT = cinfo->idct->inverse_DCT[ci];
492 0 : output_ptr = output_buf[ci];
493 : /* Loop over all DCT blocks to be processed. */
494 0 : for (block_row = 0; block_row < block_rows; block_row++) {
495 0 : buffer_ptr = buffer[block_row] + cinfo->master->first_MCU_col[ci];
496 0 : if (first_row && block_row == 0)
497 0 : prev_block_row = buffer_ptr;
498 : else
499 0 : prev_block_row = buffer[block_row-1];
500 0 : if (last_row && block_row == block_rows-1)
501 0 : next_block_row = buffer_ptr;
502 : else
503 0 : next_block_row = buffer[block_row+1];
504 : /* We fetch the surrounding DC values using a sliding-register approach.
505 : * Initialize all nine here so as to do the right thing on narrow pics.
506 : */
507 0 : DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
508 0 : DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
509 0 : DC7 = DC8 = DC9 = (int) next_block_row[0][0];
510 0 : output_col = 0;
511 0 : last_block_column = compptr->width_in_blocks - 1;
512 0 : for (block_num = cinfo->master->first_MCU_col[ci];
513 0 : block_num <= cinfo->master->last_MCU_col[ci]; block_num++) {
514 : /* Fetch current DCT block into workspace so we can modify it. */
515 0 : jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
516 : /* Update DC values */
517 0 : if (block_num < last_block_column) {
518 0 : DC3 = (int) prev_block_row[1][0];
519 0 : DC6 = (int) buffer_ptr[1][0];
520 0 : DC9 = (int) next_block_row[1][0];
521 : }
522 : /* Compute coefficient estimates per K.8.
523 : * An estimate is applied only if coefficient is still zero,
524 : * and is not known to be fully accurate.
525 : */
526 : /* AC01 */
527 0 : if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
528 0 : num = 36 * Q00 * (DC4 - DC6);
529 0 : if (num >= 0) {
530 0 : pred = (int) (((Q01<<7) + num) / (Q01<<8));
531 0 : if (Al > 0 && pred >= (1<<Al))
532 0 : pred = (1<<Al)-1;
533 : } else {
534 0 : pred = (int) (((Q01<<7) - num) / (Q01<<8));
535 0 : if (Al > 0 && pred >= (1<<Al))
536 0 : pred = (1<<Al)-1;
537 0 : pred = -pred;
538 : }
539 0 : workspace[1] = (JCOEF) pred;
540 : }
541 : /* AC10 */
542 0 : if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
543 0 : num = 36 * Q00 * (DC2 - DC8);
544 0 : if (num >= 0) {
545 0 : pred = (int) (((Q10<<7) + num) / (Q10<<8));
546 0 : if (Al > 0 && pred >= (1<<Al))
547 0 : pred = (1<<Al)-1;
548 : } else {
549 0 : pred = (int) (((Q10<<7) - num) / (Q10<<8));
550 0 : if (Al > 0 && pred >= (1<<Al))
551 0 : pred = (1<<Al)-1;
552 0 : pred = -pred;
553 : }
554 0 : workspace[8] = (JCOEF) pred;
555 : }
556 : /* AC20 */
557 0 : if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
558 0 : num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
559 0 : if (num >= 0) {
560 0 : pred = (int) (((Q20<<7) + num) / (Q20<<8));
561 0 : if (Al > 0 && pred >= (1<<Al))
562 0 : pred = (1<<Al)-1;
563 : } else {
564 0 : pred = (int) (((Q20<<7) - num) / (Q20<<8));
565 0 : if (Al > 0 && pred >= (1<<Al))
566 0 : pred = (1<<Al)-1;
567 0 : pred = -pred;
568 : }
569 0 : workspace[16] = (JCOEF) pred;
570 : }
571 : /* AC11 */
572 0 : if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
573 0 : num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
574 0 : if (num >= 0) {
575 0 : pred = (int) (((Q11<<7) + num) / (Q11<<8));
576 0 : if (Al > 0 && pred >= (1<<Al))
577 0 : pred = (1<<Al)-1;
578 : } else {
579 0 : pred = (int) (((Q11<<7) - num) / (Q11<<8));
580 0 : if (Al > 0 && pred >= (1<<Al))
581 0 : pred = (1<<Al)-1;
582 0 : pred = -pred;
583 : }
584 0 : workspace[9] = (JCOEF) pred;
585 : }
586 : /* AC02 */
587 0 : if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
588 0 : num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
589 0 : if (num >= 0) {
590 0 : pred = (int) (((Q02<<7) + num) / (Q02<<8));
591 0 : if (Al > 0 && pred >= (1<<Al))
592 0 : pred = (1<<Al)-1;
593 : } else {
594 0 : pred = (int) (((Q02<<7) - num) / (Q02<<8));
595 0 : if (Al > 0 && pred >= (1<<Al))
596 0 : pred = (1<<Al)-1;
597 0 : pred = -pred;
598 : }
599 0 : workspace[2] = (JCOEF) pred;
600 : }
601 : /* OK, do the IDCT */
602 0 : (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
603 : output_ptr, output_col);
604 : /* Advance for next column */
605 0 : DC1 = DC2; DC2 = DC3;
606 0 : DC4 = DC5; DC5 = DC6;
607 0 : DC7 = DC8; DC8 = DC9;
608 0 : buffer_ptr++, prev_block_row++, next_block_row++;
609 0 : output_col += compptr->_DCT_scaled_size;
610 : }
611 0 : output_ptr += compptr->_DCT_scaled_size;
612 : }
613 : }
614 :
615 0 : if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
616 0 : return JPEG_ROW_COMPLETED;
617 0 : return JPEG_SCAN_COMPLETED;
618 : }
619 :
620 : #endif /* BLOCK_SMOOTHING_SUPPORTED */
621 :
622 :
623 : /*
624 : * Initialize coefficient buffer controller.
625 : */
626 :
627 : GLOBAL(void)
628 0 : jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
629 : {
630 : my_coef_ptr coef;
631 :
632 0 : coef = (my_coef_ptr)
633 0 : (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
634 : sizeof(my_coef_controller));
635 0 : cinfo->coef = (struct jpeg_d_coef_controller *) coef;
636 0 : coef->pub.start_input_pass = start_input_pass;
637 0 : coef->pub.start_output_pass = start_output_pass;
638 : #ifdef BLOCK_SMOOTHING_SUPPORTED
639 0 : coef->coef_bits_latch = NULL;
640 : #endif
641 :
642 : /* Create the coefficient buffer. */
643 0 : if (need_full_buffer) {
644 : #ifdef D_MULTISCAN_FILES_SUPPORTED
645 : /* Allocate a full-image virtual array for each component, */
646 : /* padded to a multiple of samp_factor DCT blocks in each direction. */
647 : /* Note we ask for a pre-zeroed array. */
648 : int ci, access_rows;
649 : jpeg_component_info *compptr;
650 :
651 0 : for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
652 0 : ci++, compptr++) {
653 0 : access_rows = compptr->v_samp_factor;
654 : #ifdef BLOCK_SMOOTHING_SUPPORTED
655 : /* If block smoothing could be used, need a bigger window */
656 0 : if (cinfo->progressive_mode)
657 0 : access_rows *= 3;
658 : #endif
659 0 : coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
660 : ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
661 0 : (JDIMENSION) jround_up((long) compptr->width_in_blocks,
662 0 : (long) compptr->h_samp_factor),
663 0 : (JDIMENSION) jround_up((long) compptr->height_in_blocks,
664 0 : (long) compptr->v_samp_factor),
665 : (JDIMENSION) access_rows);
666 : }
667 0 : coef->pub.consume_data = consume_data;
668 0 : coef->pub.decompress_data = decompress_data;
669 0 : coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
670 : #else
671 : ERREXIT(cinfo, JERR_NOT_COMPILED);
672 : #endif
673 : } else {
674 : /* We only need a single-MCU buffer. */
675 : JBLOCKROW buffer;
676 : int i;
677 :
678 0 : buffer = (JBLOCKROW)
679 0 : (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
680 : D_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
681 0 : for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
682 0 : coef->MCU_buffer[i] = buffer + i;
683 : }
684 0 : coef->pub.consume_data = dummy_consume_data;
685 0 : coef->pub.decompress_data = decompress_onepass;
686 0 : coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
687 : }
688 :
689 : /* Allocate the workspace buffer */
690 0 : coef->workspace = (JCOEF *)
691 0 : (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
692 : sizeof(JCOEF) * DCTSIZE2);
693 0 : }
|