blob: d38db6c337ecdb7be040a2cfd47db7bbecb2ccd5 [file] [log] [blame]
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001/*
2 * jdcoefct.c
3 *
noel@chromium.org3395bcc2014-04-14 06:56:00 +00004 * This file was part of the Independent JPEG Group's software:
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00005 * Copyright (C) 1994-1997, Thomas G. Lane.
noel@chromium.org3395bcc2014-04-14 06:56:00 +00006 * libjpeg-turbo Modifications:
hbono@chromium.org98626972011-08-03 03:13:08 +00007 * Copyright (C) 2010, D. R. Commander.
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00008 * For conditions of distribution and use, see the accompanying README file.
9 *
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.
13 *
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.
17 */
18
19#define JPEG_INTERNALS
20#include "jinclude.h"
21#include "jpeglib.h"
hbono@chromium.org98626972011-08-03 03:13:08 +000022#include "jpegcomp.h"
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000023
24/* Block smoothing is only applicable for progressive JPEG, so: */
25#ifndef D_PROGRESSIVE_SUPPORTED
26#undef BLOCK_SMOOTHING_SUPPORTED
27#endif
28
29/* Private buffer controller object */
30
31typedef struct {
32 struct jpeg_d_coef_controller pub; /* public fields */
33
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 */
39
40 /* The output side's location is represented by cinfo->output_iMCU_row. */
41
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.
50 */
51 JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];
52
53 /* Temporary workspace for one MCU */
54 JCOEF * workspace;
55
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];
59#endif
60
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] */
65#endif
66} my_coef_controller;
67
68typedef my_coef_controller * my_coef_ptr;
69
70/* Forward declarations */
71METHODDEF(int) decompress_onepass
72 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
73#ifdef D_MULTISCAN_FILES_SUPPORTED
74METHODDEF(int) decompress_data
75 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
76#endif
77#ifdef BLOCK_SMOOTHING_SUPPORTED
78LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
79METHODDEF(int) decompress_smooth_data
80 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
81#endif
82
83
84LOCAL(void)
85start_iMCU_row (j_decompress_ptr cinfo)
86/* Reset within-iMCU-row counters for a new row (input side) */
87{
88 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
89
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.
93 */
94 if (cinfo->comps_in_scan > 1) {
95 coef->MCU_rows_per_iMCU_row = 1;
96 } else {
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;
99 else
100 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
101 }
102
103 coef->MCU_ctr = 0;
104 coef->MCU_vert_offset = 0;
105}
106
107
108/*
109 * Initialize for an input processing pass.
110 */
111
112METHODDEF(void)
113start_input_pass (j_decompress_ptr cinfo)
114{
115 cinfo->input_iMCU_row = 0;
116 start_iMCU_row(cinfo);
117}
118
119
120/*
121 * Initialize for an output processing pass.
122 */
123
124METHODDEF(void)
125start_output_pass (j_decompress_ptr cinfo)
126{
127#ifdef BLOCK_SMOOTHING_SUPPORTED
128 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
129
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;
134 else
135 coef->pub.decompress_data = decompress_data;
136 }
137#endif
138 cinfo->output_iMCU_row = 0;
139}
140
141
142/*
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.
147 *
148 * NB: output_buf contains a plane for each component in image,
149 * which we index according to the component's SOF position.
150 */
151
152METHODDEF(int)
153decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
154{
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;
164
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;
167 yoffset++) {
168 for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
169 MCU_col_num++) {
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;
178 }
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.
183 */
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;
190 continue;
191 }
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] +
hbono@chromium.org98626972011-08-03 03:13:08 +0000196 yoffset * compptr->_DCT_scaled_size;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000197 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);
hbono@chromium.org98626972011-08-03 03:13:08 +0000206 output_col += compptr->_DCT_scaled_size;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000207 }
208 }
209 blkn += compptr->MCU_width;
hbono@chromium.org98626972011-08-03 03:13:08 +0000210 output_ptr += compptr->_DCT_scaled_size;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000211 }
212 }
213 }
214 /* Completed an MCU row, but perhaps not an iMCU row */
215 coef->MCU_ctr = 0;
216 }
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;
222 }
223 /* Completed the scan */
224 (*cinfo->inputctl->finish_input_pass) (cinfo);
225 return JPEG_SCAN_COMPLETED;
226}
227
228
229/*
230 * Dummy consume-input routine for single-pass operation.
231 */
232
233METHODDEF(int)
234dummy_consume_data (j_decompress_ptr cinfo)
235{
236 return JPEG_SUSPENDED; /* Always indicate nothing was done */
237}
238
239
240#ifdef D_MULTISCAN_FILES_SUPPORTED
241
242/*
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.
247 */
248
249METHODDEF(int)
250consume_data (j_decompress_ptr cinfo)
251{
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;
259
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.
270 */
271 }
272
273 /* Loop to process one whole iMCU row */
274 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
275 yoffset++) {
276 for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
277 MCU_col_num++) {
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++;
287 }
288 }
289 }
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;
296 }
297 }
298 /* Completed an MCU row, but perhaps not an iMCU row */
299 coef->MCU_ctr = 0;
300 }
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;
305 }
306 /* Completed the scan */
307 (*cinfo->inputctl->finish_input_pass) (cinfo);
308 return JPEG_SCAN_COMPLETED;
309}
310
311
312/*
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.
316 *
317 * NB: output_buf contains a plane for each component in image.
318 */
319
320METHODDEF(int)
321decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
322{
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;
327 JBLOCKARRAY buffer;
328 JBLOCKROW buffer_ptr;
329 JSAMPARRAY output_ptr;
330 JDIMENSION output_col;
331 jpeg_component_info *compptr;
332 inverse_DCT_method_ptr inverse_DCT;
333
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;
340 }
341
342 /* OK, output from the virtual arrays. */
343 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
344 ci++, compptr++) {
345 /* Don't bother to IDCT an uninteresting component. */
346 if (! compptr->component_needed)
347 continue;
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;
356 else {
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;
360 }
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];
366 output_col = 0;
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);
370 buffer_ptr++;
hbono@chromium.org98626972011-08-03 03:13:08 +0000371 output_col += compptr->_DCT_scaled_size;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000372 }
hbono@chromium.org98626972011-08-03 03:13:08 +0000373 output_ptr += compptr->_DCT_scaled_size;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000374 }
375 }
376
377 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
378 return JPEG_ROW_COMPLETED;
379 return JPEG_SCAN_COMPLETED;
380}
381
382#endif /* D_MULTISCAN_FILES_SUPPORTED */
383
384
385#ifdef BLOCK_SMOOTHING_SUPPORTED
386
387/*
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.
393 */
394
395/* Natural-order array positions of the first 5 zigzag-order coefficients */
396#define Q01_POS 1
397#define Q10_POS 8
398#define Q20_POS 16
399#define Q11_POS 9
400#define Q02_POS 2
401
402/*
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.
408 */
409
410LOCAL(boolean)
411smoothing_ok (j_decompress_ptr cinfo)
412{
413 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
414 boolean smoothing_useful = FALSE;
415 int ci, coefi;
416 jpeg_component_info *compptr;
417 JQUANT_TBL * qtable;
418 int * coef_bits;
419 int * coef_bits_latch;
420
421 if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
422 return FALSE;
423
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;
431
432 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
433 ci++, compptr++) {
434 /* All components' quantization values must already be latched. */
435 if ((qtable = compptr->quant_table) == NULL)
436 return FALSE;
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)
444 return FALSE;
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)
448 return FALSE;
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;
454 }
455 coef_bits_latch += SAVED_COEFS;
456 }
457
458 return smoothing_useful;
459}
460
461
462/*
463 * Variant of decompress_data for use when doing block smoothing.
464 */
465
466METHODDEF(int)
467decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
468{
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;
473 JBLOCKARRAY buffer;
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;
480 JCOEF * workspace;
481 int *coef_bits;
482 JQUANT_TBL *quanttbl;
483 INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
484 int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
485 int Al, pred;
486
487 /* Keep a local variable to avoid looking it up more than once */
488 workspace = coef->workspace;
489
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.
498 */
499 JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
500 if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
501 break;
502 }
503 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
504 return JPEG_SUSPENDED;
505 }
506
507 /* OK, output from the virtual arrays. */
508 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
509 ci++, compptr++) {
510 /* Don't bother to IDCT an uninteresting component. */
511 if (! compptr->component_needed)
512 continue;
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 */
517 last_row = FALSE;
518 } else {
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 */
523 last_row = TRUE;
524 }
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 */
533 first_row = FALSE;
534 } else {
535 buffer = (*cinfo->mem->access_virt_barray)
536 ((j_common_ptr) cinfo, coef->whole_image[ci],
537 (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
538 first_row = TRUE;
539 }
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;
556 else
557 prev_block_row = buffer[block_row-1];
558 if (last_row && block_row == block_rows-1)
559 next_block_row = buffer_ptr;
560 else
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.
564 */
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];
568 output_col = 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];
578 }
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.
582 */
583 /* AC01 */
584 if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
585 num = 36 * Q00 * (DC4 - DC6);
586 if (num >= 0) {
587 pred = (int) (((Q01<<7) + num) / (Q01<<8));
588 if (Al > 0 && pred >= (1<<Al))
589 pred = (1<<Al)-1;
590 } else {
591 pred = (int) (((Q01<<7) - num) / (Q01<<8));
592 if (Al > 0 && pred >= (1<<Al))
593 pred = (1<<Al)-1;
594 pred = -pred;
595 }
596 workspace[1] = (JCOEF) pred;
597 }
598 /* AC10 */
599 if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
600 num = 36 * Q00 * (DC2 - DC8);
601 if (num >= 0) {
602 pred = (int) (((Q10<<7) + num) / (Q10<<8));
603 if (Al > 0 && pred >= (1<<Al))
604 pred = (1<<Al)-1;
605 } else {
606 pred = (int) (((Q10<<7) - num) / (Q10<<8));
607 if (Al > 0 && pred >= (1<<Al))
608 pred = (1<<Al)-1;
609 pred = -pred;
610 }
611 workspace[8] = (JCOEF) pred;
612 }
613 /* AC20 */
614 if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
615 num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
616 if (num >= 0) {
617 pred = (int) (((Q20<<7) + num) / (Q20<<8));
618 if (Al > 0 && pred >= (1<<Al))
619 pred = (1<<Al)-1;
620 } else {
621 pred = (int) (((Q20<<7) - num) / (Q20<<8));
622 if (Al > 0 && pred >= (1<<Al))
623 pred = (1<<Al)-1;
624 pred = -pred;
625 }
626 workspace[16] = (JCOEF) pred;
627 }
628 /* AC11 */
629 if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
630 num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
631 if (num >= 0) {
632 pred = (int) (((Q11<<7) + num) / (Q11<<8));
633 if (Al > 0 && pred >= (1<<Al))
634 pred = (1<<Al)-1;
635 } else {
636 pred = (int) (((Q11<<7) - num) / (Q11<<8));
637 if (Al > 0 && pred >= (1<<Al))
638 pred = (1<<Al)-1;
639 pred = -pred;
640 }
641 workspace[9] = (JCOEF) pred;
642 }
643 /* AC02 */
644 if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
645 num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
646 if (num >= 0) {
647 pred = (int) (((Q02<<7) + num) / (Q02<<8));
648 if (Al > 0 && pred >= (1<<Al))
649 pred = (1<<Al)-1;
650 } else {
651 pred = (int) (((Q02<<7) - num) / (Q02<<8));
652 if (Al > 0 && pred >= (1<<Al))
653 pred = (1<<Al)-1;
654 pred = -pred;
655 }
656 workspace[2] = (JCOEF) pred;
657 }
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++;
hbono@chromium.org98626972011-08-03 03:13:08 +0000666 output_col += compptr->_DCT_scaled_size;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000667 }
hbono@chromium.org98626972011-08-03 03:13:08 +0000668 output_ptr += compptr->_DCT_scaled_size;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000669 }
670 }
671
672 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
673 return JPEG_ROW_COMPLETED;
674 return JPEG_SCAN_COMPLETED;
675}
676
677#endif /* BLOCK_SMOOTHING_SUPPORTED */
678
679
680/*
681 * Initialize coefficient buffer controller.
682 */
683
684GLOBAL(void)
685jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
686{
687 my_coef_ptr coef;
688
689 coef = (my_coef_ptr)
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;
697#endif
698
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. */
705 int ci, access_rows;
706 jpeg_component_info *compptr;
707
708 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
709 ci++, compptr++) {
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)
714 access_rows *= 3;
715#endif
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);
723 }
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 */
727#else
728 ERREXIT(cinfo, JERR_NOT_COMPILED);
729#endif
730 } else {
731 /* We only need a single-MCU buffer. */
732 JBLOCKROW buffer;
733 int i;
734
735 buffer = (JBLOCKROW)
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;
740 }
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 */
744 }
745
746 /* Allocate the workspace buffer */
747 coef->workspace = (JCOEF *)
748 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
749 SIZEOF(JCOEF) * DCTSIZE2);
750}