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 <[email protected]> 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
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 */
28METHODDEF(int) decompress_onepass(j_decompress_ptr cinfo,
29 JSAMPIMAGE output_buf);
30#ifdef D_MULTISCAN_FILES_SUPPORTED
31METHODDEF(int) decompress_data(j_decompress_ptr cinfo, JSAMPIMAGE output_buf);
32#endif
33#ifdef BLOCK_SMOOTHING_SUPPORTED
34LOCAL(boolean) smoothing_ok(j_decompress_ptr cinfo);
35METHODDEF(int) decompress_smooth_data(j_decompress_ptr cinfo,
36 JSAMPIMAGE output_buf);
37#endif
38
39
40/*
41 * Initialize for an input processing pass.
42 */
43
44METHODDEF(void)
45start_input_pass(j_decompress_ptr cinfo)
46{
47 cinfo->input_iMCU_row = 0;
48 start_iMCU_row(cinfo);
49}
50
51
52/*
53 * Initialize for an output processing pass.
54 */
55
56METHODDEF(void)
57start_output_pass(j_decompress_ptr cinfo)
58{
59#ifdef BLOCK_SMOOTHING_SUPPORTED
60 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
61
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;
66 else
67 coef->pub.decompress_data = decompress_data;
68 }
69#endif
70 cinfo->output_iMCU_row = 0;
71}
72
73
74/*
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.
79 *
80 * NB: output_buf contains a plane for each component in image,
81 * which we index according to the component's SOF position.
82 */
83
84METHODDEF(int)
85decompress_onepass(j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
86{
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;
96
97 /* Loop to process as much as one whole iMCU row */
98 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
99 yoffset++) {
100 for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
101 MCU_col_num++) {
102 /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
103 jzero_far((void *)coef->MCU_buffer[0],
104 (size_t)(cinfo->blocks_in_MCU * sizeof(JBLOCK)));
105 if (!cinfo->entropy->insufficient_data)
106 cinfo->master->last_good_iMCU_row = cinfo->input_iMCU_row;
107 if (!(*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
108 /* Suspension forced; update state counters and exit */
109 coef->MCU_vert_offset = yoffset;
110 coef->MCU_ctr = MCU_col_num;
111 return JPEG_SUSPENDED;
112 }
113
114 /* Only perform the IDCT on blocks that are contained within the desired
115 * cropping region.
116 */
117 if (MCU_col_num >= cinfo->master->first_iMCU_col &&
118 MCU_col_num <= cinfo->master->last_iMCU_col) {
119 /* Determine where data should go in output_buf and do the IDCT thing.
120 * We skip dummy blocks at the right and bottom edges (but blkn gets
121 * incremented past them!). Note the inner loop relies on having
122 * allocated the MCU_buffer[] blocks sequentially.
123 */
124 blkn = 0; /* index of current DCT block within MCU */
125 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
126 compptr = cinfo->cur_comp_info[ci];
127 /* Don't bother to IDCT an uninteresting component. */
128 if (!compptr->component_needed) {
129 blkn += compptr->MCU_blocks;
130 continue;
131 }
132 inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
133 useful_width = (MCU_col_num < last_MCU_col) ?
134 compptr->MCU_width : compptr->last_col_width;
135 output_ptr = output_buf[compptr->component_index] +
136 yoffset * compptr->_DCT_scaled_size;
137 start_col = (MCU_col_num - cinfo->master->first_iMCU_col) *
138 compptr->MCU_sample_width;
139 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
140 if (cinfo->input_iMCU_row < last_iMCU_row ||
141 yoffset + yindex < compptr->last_row_height) {
142 output_col = start_col;
143 for (xindex = 0; xindex < useful_width; xindex++) {
144 (*inverse_DCT) (cinfo, compptr,
145 (JCOEFPTR)coef->MCU_buffer[blkn + xindex],
146 output_ptr, output_col);
147 output_col += compptr->_DCT_scaled_size;
148 }
149 }
150 blkn += compptr->MCU_width;
151 output_ptr += compptr->_DCT_scaled_size;
152 }
153 }
154 }
155 }
156 /* Completed an MCU row, but perhaps not an iMCU row */
157 coef->MCU_ctr = 0;
158 }
159 /* Completed the iMCU row, advance counters for next one */
160 cinfo->output_iMCU_row++;
161 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
162 start_iMCU_row(cinfo);
163 return JPEG_ROW_COMPLETED;
164 }
165 /* Completed the scan */
166 (*cinfo->inputctl->finish_input_pass) (cinfo);
167 return JPEG_SCAN_COMPLETED;
168}
169
170
171/*
172 * Dummy consume-input routine for single-pass operation.
173 */
174
175METHODDEF(int)
176dummy_consume_data(j_decompress_ptr cinfo)
177{
178 return JPEG_SUSPENDED; /* Always indicate nothing was done */
179}
180
181
182#ifdef D_MULTISCAN_FILES_SUPPORTED
183
184/*
185 * Consume input data and store it in the full-image coefficient buffer.
186 * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
187 * ie, v_samp_factor block rows for each component in the scan.
188 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
189 */
190
191METHODDEF(int)
192consume_data(j_decompress_ptr cinfo)
193{
194 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
195 JDIMENSION MCU_col_num; /* index of current MCU within row */
196 int blkn, ci, xindex, yindex, yoffset;
197 JDIMENSION start_col;
198 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
199 JBLOCKROW buffer_ptr;
200 jpeg_component_info *compptr;
201
202 /* Align the virtual buffers for the components used in this scan. */
203 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
204 compptr = cinfo->cur_comp_info[ci];
205 buffer[ci] = (*cinfo->mem->access_virt_barray)
206 ((j_common_ptr)cinfo, coef->whole_image[compptr->component_index],
207 cinfo->input_iMCU_row * compptr->v_samp_factor,
208 (JDIMENSION)compptr->v_samp_factor, TRUE);
209 /* Note: entropy decoder expects buffer to be zeroed,
210 * but this is handled automatically by the memory manager
211 * because we requested a pre-zeroed array.
212 */
213 }
214
215 /* Loop to process one whole iMCU row */
216 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
217 yoffset++) {
218 for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
219 MCU_col_num++) {
220 /* Construct list of pointers to DCT blocks belonging to this MCU */
221 blkn = 0; /* index of current DCT block within MCU */
222 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
223 compptr = cinfo->cur_comp_info[ci];
224 start_col = MCU_col_num * compptr->MCU_width;
225 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
226 buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
227 for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
228 coef->MCU_buffer[blkn++] = buffer_ptr++;
229 }
230 }
231 }
232 if (!cinfo->entropy->insufficient_data)
233 cinfo->master->last_good_iMCU_row = cinfo->input_iMCU_row;
234 /* Try to fetch the MCU. */
235 if (!(*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
236 /* Suspension forced; update state counters and exit */
237 coef->MCU_vert_offset = yoffset;
238 coef->MCU_ctr = MCU_col_num;
239 return JPEG_SUSPENDED;
240 }
241 }
242 /* Completed an MCU row, but perhaps not an iMCU row */
243 coef->MCU_ctr = 0;
244 }
245 /* Completed the iMCU row, advance counters for next one */
246 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
247 start_iMCU_row(cinfo);
248 return JPEG_ROW_COMPLETED;
249 }
250 /* Completed the scan */
251 (*cinfo->inputctl->finish_input_pass) (cinfo);
252 return JPEG_SCAN_COMPLETED;
253}
254
255
256/*
257 * Decompress and return some data in the multi-pass case.
258 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
259 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
260 *
261 * NB: output_buf contains a plane for each component in image.
262 */
263
264METHODDEF(int)
265decompress_data(j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
266{
267 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
268 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
269 JDIMENSION block_num;
270 int ci, block_row, block_rows;
271 JBLOCKARRAY buffer;
272 JBLOCKROW buffer_ptr;
273 JSAMPARRAY output_ptr;
274 JDIMENSION output_col;
275 jpeg_component_info *compptr;
276 inverse_DCT_method_ptr inverse_DCT;
277
278 /* Force some input to be done if we are getting ahead of the input. */
279 while (cinfo->input_scan_number < cinfo->output_scan_number ||
280 (cinfo->input_scan_number == cinfo->output_scan_number &&
281 cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
282 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
283 return JPEG_SUSPENDED;
284 }
285
286 /* OK, output from the virtual arrays. */
287 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
288 ci++, compptr++) {
289 /* Don't bother to IDCT an uninteresting component. */
290 if (!compptr->component_needed)
291 continue;
292 /* Align the virtual buffer for this component. */
293 buffer = (*cinfo->mem->access_virt_barray)
294 ((j_common_ptr)cinfo, coef->whole_image[ci],
295 cinfo->output_iMCU_row * compptr->v_samp_factor,
296 (JDIMENSION)compptr->v_samp_factor, FALSE);
297 /* Count non-dummy DCT block rows in this iMCU row. */
298 if (cinfo->output_iMCU_row < last_iMCU_row)
299 block_rows = compptr->v_samp_factor;
300 else {
301 /* NB: can't use last_row_height here; it is input-side-dependent! */
302 block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
303 if (block_rows == 0) block_rows = compptr->v_samp_factor;
304 }
305 inverse_DCT = cinfo->idct->inverse_DCT[ci];
306 output_ptr = output_buf[ci];
307 /* Loop over all DCT blocks to be processed. */
308 for (block_row = 0; block_row < block_rows; block_row++) {
309 buffer_ptr = buffer[block_row] + cinfo->master->first_MCU_col[ci];
310 output_col = 0;
311 for (block_num = cinfo->master->first_MCU_col[ci];
312 block_num <= cinfo->master->last_MCU_col[ci]; block_num++) {
313 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR)buffer_ptr, output_ptr,
314 output_col);
315 buffer_ptr++;
316 output_col += compptr->_DCT_scaled_size;
317 }
318 output_ptr += compptr->_DCT_scaled_size;
319 }
320 }
321
322 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
323 return JPEG_ROW_COMPLETED;
324 return JPEG_SCAN_COMPLETED;
325}
326
327#endif /* D_MULTISCAN_FILES_SUPPORTED */
328
329
330#ifdef BLOCK_SMOOTHING_SUPPORTED
331
332/*
333 * This code applies interblock smoothing; the first 9 AC coefficients are
334 * estimated from the DC values of a DCT block and its 24 neighboring blocks.
335 * We apply smoothing only for progressive JPEG decoding, and only if
336 * the coefficients it can estimate are not yet known to full precision.
337 */
338
339/* Natural-order array positions of the first 9 zigzag-order coefficients */
340#define Q01_POS 1
341#define Q10_POS 8
342#define Q20_POS 16
343#define Q11_POS 9
344#define Q02_POS 2
345#define Q03_POS 3
346#define Q12_POS 10
347#define Q21_POS 17
348#define Q30_POS 24
349
350/*
351 * Determine whether block smoothing is applicable and safe.
352 * We also latch the current states of the coef_bits[] entries for the
353 * AC coefficients; otherwise, if the input side of the decompressor
354 * advances into a new scan, we might think the coefficients are known
355 * more accurately than they really are.
356 */
357
358LOCAL(boolean)
359smoothing_ok(j_decompress_ptr cinfo)
360{
361 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
362 boolean smoothing_useful = FALSE;
363 int ci, coefi;
364 jpeg_component_info *compptr;
365 JQUANT_TBL *qtable;
366 int *coef_bits, *prev_coef_bits;
367 int *coef_bits_latch, *prev_coef_bits_latch;
368
369 if (!cinfo->progressive_mode || cinfo->coef_bits == NULL)
370 return FALSE;
371
372 /* Allocate latch area if not already done */
373 if (coef->coef_bits_latch == NULL)
374 coef->coef_bits_latch = (int *)
375 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
376 cinfo->num_components * 2 *
377 (SAVED_COEFS * sizeof(int)));
378 coef_bits_latch = coef->coef_bits_latch;
379 prev_coef_bits_latch =
380 &coef->coef_bits_latch[cinfo->num_components * SAVED_COEFS];
381
382 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
383 ci++, compptr++) {
384 /* All components' quantization values must already be latched. */
385 if ((qtable = compptr->quant_table) == NULL)
386 return FALSE;
387 /* Verify DC & first 9 AC quantizers are nonzero to avoid zero-divide. */
388 if (qtable->quantval[0] == 0 ||
389 qtable->quantval[Q01_POS] == 0 ||
390 qtable->quantval[Q10_POS] == 0 ||
391 qtable->quantval[Q20_POS] == 0 ||
392 qtable->quantval[Q11_POS] == 0 ||
393 qtable->quantval[Q02_POS] == 0 ||
394 qtable->quantval[Q03_POS] == 0 ||
395 qtable->quantval[Q12_POS] == 0 ||
396 qtable->quantval[Q21_POS] == 0 ||
397 qtable->quantval[Q30_POS] == 0)
398 return FALSE;
399 /* DC values must be at least partly known for all components. */
400 coef_bits = cinfo->coef_bits[ci];
401 prev_coef_bits = cinfo->coef_bits[ci + cinfo->num_components];
402 if (coef_bits[0] < 0)
403 return FALSE;
404 coef_bits_latch[0] = coef_bits[0];
405 /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
406 for (coefi = 1; coefi < SAVED_COEFS; coefi++) {
407 if (cinfo->input_scan_number > 1)
408 prev_coef_bits_latch[coefi] = prev_coef_bits[coefi];
409 else
410 prev_coef_bits_latch[coefi] = -1;
411 coef_bits_latch[coefi] = coef_bits[coefi];
412 if (coef_bits[coefi] != 0)
413 smoothing_useful = TRUE;
414 }
415 coef_bits_latch += SAVED_COEFS;
416 prev_coef_bits_latch += SAVED_COEFS;
417 }
418
419 return smoothing_useful;
420}
421
422
423/*
424 * Variant of decompress_data for use when doing block smoothing.
425 */
426
427METHODDEF(int)
428decompress_smooth_data(j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
429{
430 my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
431 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
432 JDIMENSION block_num, last_block_column;
433 int ci, block_row, block_rows, access_rows;
434 JBLOCKARRAY buffer;
435 JBLOCKROW buffer_ptr, prev_prev_block_row, prev_block_row;
436 JBLOCKROW next_block_row, next_next_block_row;
437 JSAMPARRAY output_ptr;
438 JDIMENSION output_col;
439 jpeg_component_info *compptr;
440 inverse_DCT_method_ptr inverse_DCT;
441 boolean change_dc;
442 JCOEF *workspace;
443 int *coef_bits;
444 JQUANT_TBL *quanttbl;
445 JLONG Q00, Q01, Q02, Q03 = 0, Q10, Q11, Q12 = 0, Q20, Q21 = 0, Q30 = 0, num;
446 int DC01, DC02, DC03, DC04, DC05, DC06, DC07, DC08, DC09, DC10, DC11, DC12,
447 DC13, DC14, DC15, DC16, DC17, DC18, DC19, DC20, DC21, DC22, DC23, DC24,
448 DC25;
449 int Al, pred;
450
451 /* Keep a local variable to avoid looking it up more than once */
452 workspace = coef->workspace;
453
454 /* Force some input to be done if we are getting ahead of the input. */
455 while (cinfo->input_scan_number <= cinfo->output_scan_number &&
456 !cinfo->inputctl->eoi_reached) {
457 if (cinfo->input_scan_number == cinfo->output_scan_number) {
458 /* If input is working on current scan, we ordinarily want it to
459 * have completed the current row. But if input scan is DC,
460 * we want it to keep two rows ahead so that next two block rows' DC
461 * values are up to date.
462 */
463 JDIMENSION delta = (cinfo->Ss == 0) ? 2 : 0;
464 if (cinfo->input_iMCU_row > cinfo->output_iMCU_row + delta)
465 break;
466 }
467 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
468 return JPEG_SUSPENDED;
469 }
470
471 /* OK, output from the virtual arrays. */
472 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
473 ci++, compptr++) {
474 /* Don't bother to IDCT an uninteresting component. */
475 if (!compptr->component_needed)
476 continue;
477 /* Count non-dummy DCT block rows in this iMCU row. */
478 if (cinfo->output_iMCU_row < last_iMCU_row - 1) {
479 block_rows = compptr->v_samp_factor;
480 access_rows = block_rows * 3; /* this and next two iMCU rows */
481 } else if (cinfo->output_iMCU_row < last_iMCU_row) {
482 block_rows = compptr->v_samp_factor;
483 access_rows = block_rows * 2; /* this and next iMCU row */
484 } else {
485 /* NB: can't use last_row_height here; it is input-side-dependent! */
486 block_rows = (int)(compptr->height_in_blocks % compptr->v_samp_factor);
487 if (block_rows == 0) block_rows = compptr->v_samp_factor;
488 access_rows = block_rows; /* this iMCU row only */
489 }
490 /* Align the virtual buffer for this component. */
491 if (cinfo->output_iMCU_row > 1) {
492 access_rows += 2 * compptr->v_samp_factor; /* prior two iMCU rows too */
493 buffer = (*cinfo->mem->access_virt_barray)
494 ((j_common_ptr)cinfo, coef->whole_image[ci],
495 (cinfo->output_iMCU_row - 2) * compptr->v_samp_factor,
496 (JDIMENSION)access_rows, FALSE);
497 buffer += 2 * compptr->v_samp_factor; /* point to current iMCU row */
498 } else if (cinfo->output_iMCU_row > 0) {
499 buffer = (*cinfo->mem->access_virt_barray)
500 ((j_common_ptr)cinfo, coef->whole_image[ci],
501 (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
502 (JDIMENSION)access_rows, FALSE);
503 buffer += compptr->v_samp_factor; /* point to current iMCU row */
504 } else {
505 buffer = (*cinfo->mem->access_virt_barray)
506 ((j_common_ptr)cinfo, coef->whole_image[ci],
507 (JDIMENSION)0, (JDIMENSION)access_rows, FALSE);
508 }
509 /* Fetch component-dependent info.
510 * If the current scan is incomplete, then we use the component-dependent
511 * info from the previous scan.
512 */
513 if (cinfo->output_iMCU_row > cinfo->master->last_good_iMCU_row)
514 coef_bits =
515 coef->coef_bits_latch + ((ci + cinfo->num_components) * SAVED_COEFS);
516 else
517 coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
518
519 /* We only do DC interpolation if no AC coefficient data is available. */
520 change_dc =
521 coef_bits[1] == -1 && coef_bits[2] == -1 && coef_bits[3] == -1 &&
522 coef_bits[4] == -1 && coef_bits[5] == -1 && coef_bits[6] == -1 &&
523 coef_bits[7] == -1 && coef_bits[8] == -1 && coef_bits[9] == -1;
524
525 quanttbl = compptr->quant_table;
526 Q00 = quanttbl->quantval[0];
527 Q01 = quanttbl->quantval[Q01_POS];
528 Q10 = quanttbl->quantval[Q10_POS];
529 Q20 = quanttbl->quantval[Q20_POS];
530 Q11 = quanttbl->quantval[Q11_POS];
531 Q02 = quanttbl->quantval[Q02_POS];
532 if (change_dc) {
533 Q03 = quanttbl->quantval[Q03_POS];
534 Q12 = quanttbl->quantval[Q12_POS];
535 Q21 = quanttbl->quantval[Q21_POS];
536 Q30 = quanttbl->quantval[Q30_POS];
537 }
538 inverse_DCT = cinfo->idct->inverse_DCT[ci];
539 output_ptr = output_buf[ci];
540 /* Loop over all DCT blocks to be processed. */
541 for (block_row = 0; block_row < block_rows; block_row++) {
542 buffer_ptr = buffer[block_row] + cinfo->master->first_MCU_col[ci];
543
544 if (block_row > 0 || cinfo->output_iMCU_row > 0)
545 prev_block_row =
546 buffer[block_row - 1] + cinfo->master->first_MCU_col[ci];
547 else
548 prev_block_row = buffer_ptr;
549
550 if (block_row > 1 || cinfo->output_iMCU_row > 1)
551 prev_prev_block_row =
552 buffer[block_row - 2] + cinfo->master->first_MCU_col[ci];
553 else
554 prev_prev_block_row = prev_block_row;
555
556 if (block_row < block_rows - 1 || cinfo->output_iMCU_row < last_iMCU_row)
557 next_block_row =
558 buffer[block_row + 1] + cinfo->master->first_MCU_col[ci];
559 else
560 next_block_row = buffer_ptr;
561
562 if (block_row < block_rows - 2 ||
563 cinfo->output_iMCU_row < last_iMCU_row - 1)
564 next_next_block_row =
565 buffer[block_row + 2] + cinfo->master->first_MCU_col[ci];
566 else
567 next_next_block_row = next_block_row;
568
569 /* We fetch the surrounding DC values using a sliding-register approach.
570 * Initialize all 25 here so as to do the right thing on narrow pics.
571 */
572 DC01 = DC02 = DC03 = DC04 = DC05 = (int)prev_prev_block_row[0][0];
573 DC06 = DC07 = DC08 = DC09 = DC10 = (int)prev_block_row[0][0];
574 DC11 = DC12 = DC13 = DC14 = DC15 = (int)buffer_ptr[0][0];
575 DC16 = DC17 = DC18 = DC19 = DC20 = (int)next_block_row[0][0];
576 DC21 = DC22 = DC23 = DC24 = DC25 = (int)next_next_block_row[0][0];
577 output_col = 0;
578 last_block_column = compptr->width_in_blocks - 1;
579 for (block_num = cinfo->master->first_MCU_col[ci];
580 block_num <= cinfo->master->last_MCU_col[ci]; block_num++) {
581 /* Fetch current DCT block into workspace so we can modify it. */
582 jcopy_block_row(buffer_ptr, (JBLOCKROW)workspace, (JDIMENSION)1);
583 /* Update DC values */
584 if (block_num == cinfo->master->first_MCU_col[ci] &&
585 block_num < last_block_column) {
586 DC04 = (int)prev_prev_block_row[1][0];
587 DC09 = (int)prev_block_row[1][0];
588 DC14 = (int)buffer_ptr[1][0];
589 DC19 = (int)next_block_row[1][0];
590 DC24 = (int)next_next_block_row[1][0];
591 }
592 if (block_num + 1 < last_block_column) {
593 DC05 = (int)prev_prev_block_row[2][0];
594 DC10 = (int)prev_block_row[2][0];
595 DC15 = (int)buffer_ptr[2][0];
596 DC20 = (int)next_block_row[2][0];
597 DC25 = (int)next_next_block_row[2][0];
598 }
599 /* If DC interpolation is enabled, compute coefficient estimates using
600 * a Gaussian-like kernel, keeping the averages of the DC values.
601 *
602 * If DC interpolation is disabled, compute coefficient estimates using
603 * an algorithm similar to the one described in Section K.8 of the JPEG
604 * standard, except applied to a 5x5 window rather than a 3x3 window.
605 *
606 * An estimate is applied only if the coefficient is still zero and is
607 * not known to be fully accurate.
608 */
609 /* AC01 */
610 if ((Al = coef_bits[1]) != 0 && workspace[1] == 0) {
611 num = Q00 * (change_dc ?
612 (-DC01 - DC02 + DC04 + DC05 - 3 * DC06 + 13 * DC07 -
613 13 * DC09 + 3 * DC10 - 3 * DC11 + 38 * DC12 - 38 * DC14 +
614 3 * DC15 - 3 * DC16 + 13 * DC17 - 13 * DC19 + 3 * DC20 -
615 DC21 - DC22 + DC24 + DC25) :
616 (-7 * DC11 + 50 * DC12 - 50 * DC14 + 7 * DC15));
617 if (num >= 0) {
618 pred = (int)(((Q01 << 7) + num) / (Q01 << 8));
619 if (Al > 0 && pred >= (1 << Al))
620 pred = (1 << Al) - 1;
621 } else {
622 pred = (int)(((Q01 << 7) - num) / (Q01 << 8));
623 if (Al > 0 && pred >= (1 << Al))
624 pred = (1 << Al) - 1;
625 pred = -pred;
626 }
627 workspace[1] = (JCOEF)pred;
628 }
629 /* AC10 */
630 if ((Al = coef_bits[2]) != 0 && workspace[8] == 0) {
631 num = Q00 * (change_dc ?
632 (-DC01 - 3 * DC02 - 3 * DC03 - 3 * DC04 - DC05 - DC06 +
633 13 * DC07 + 38 * DC08 + 13 * DC09 - DC10 + DC16 -
634 13 * DC17 - 38 * DC18 - 13 * DC19 + DC20 + DC21 +
635 3 * DC22 + 3 * DC23 + 3 * DC24 + DC25) :
636 (-7 * DC03 + 50 * DC08 - 50 * DC18 + 7 * DC23));
637 if (num >= 0) {
638 pred = (int)(((Q10 << 7) + num) / (Q10 << 8));
639 if (Al > 0 && pred >= (1 << Al))
640 pred = (1 << Al) - 1;
641 } else {
642 pred = (int)(((Q10 << 7) - num) / (Q10 << 8));
643 if (Al > 0 && pred >= (1 << Al))
644 pred = (1 << Al) - 1;
645 pred = -pred;
646 }
647 workspace[8] = (JCOEF)pred;
648 }
649 /* AC20 */
650 if ((Al = coef_bits[3]) != 0 && workspace[16] == 0) {
651 num = Q00 * (change_dc ?
652 (DC03 + 2 * DC07 + 7 * DC08 + 2 * DC09 - 5 * DC12 - 14 * DC13 -
653 5 * DC14 + 2 * DC17 + 7 * DC18 + 2 * DC19 + DC23) :
654 (-DC03 + 13 * DC08 - 24 * DC13 + 13 * DC18 - DC23));
655 if (num >= 0) {
656 pred = (int)(((Q20 << 7) + num) / (Q20 << 8));
657 if (Al > 0 && pred >= (1 << Al))
658 pred = (1 << Al) - 1;
659 } else {
660 pred = (int)(((Q20 << 7) - num) / (Q20 << 8));
661 if (Al > 0 && pred >= (1 << Al))
662 pred = (1 << Al) - 1;
663 pred = -pred;
664 }
665 workspace[16] = (JCOEF)pred;
666 }
667 /* AC11 */
668 if ((Al = coef_bits[4]) != 0 && workspace[9] == 0) {
669 num = Q00 * (change_dc ?
670 (-DC01 + DC05 + 9 * DC07 - 9 * DC09 - 9 * DC17 +
671 9 * DC19 + DC21 - DC25) :
672 (DC10 + DC16 - 10 * DC17 + 10 * DC19 - DC02 - DC20 + DC22 -
673 DC24 + DC04 - DC06 + 10 * DC07 - 10 * DC09));
674 if (num >= 0) {
675 pred = (int)(((Q11 << 7) + num) / (Q11 << 8));
676 if (Al > 0 && pred >= (1 << Al))
677 pred = (1 << Al) - 1;
678 } else {
679 pred = (int)(((Q11 << 7) - num) / (Q11 << 8));
680 if (Al > 0 && pred >= (1 << Al))
681 pred = (1 << Al) - 1;
682 pred = -pred;
683 }
684 workspace[9] = (JCOEF)pred;
685 }
686 /* AC02 */
687 if ((Al = coef_bits[5]) != 0 && workspace[2] == 0) {
688 num = Q00 * (change_dc ?
689 (2 * DC07 - 5 * DC08 + 2 * DC09 + DC11 + 7 * DC12 - 14 * DC13 +
690 7 * DC14 + DC15 + 2 * DC17 - 5 * DC18 + 2 * DC19) :
691 (-DC11 + 13 * DC12 - 24 * DC13 + 13 * DC14 - DC15));
692 if (num >= 0) {
693 pred = (int)(((Q02 << 7) + num) / (Q02 << 8));
694 if (Al > 0 && pred >= (1 << Al))
695 pred = (1 << Al) - 1;
696 } else {
697 pred = (int)(((Q02 << 7) - num) / (Q02 << 8));
698 if (Al > 0 && pred >= (1 << Al))
699 pred = (1 << Al) - 1;
700 pred = -pred;
701 }
702 workspace[2] = (JCOEF)pred;
703 }
704 if (change_dc) {
705 /* AC03 */
706 if ((Al = coef_bits[6]) != 0 && workspace[3] == 0) {
707 num = Q00 * (DC07 - DC09 + 2 * DC12 - 2 * DC14 + DC17 - DC19);
708 if (num >= 0) {
709 pred = (int)(((Q03 << 7) + num) / (Q03 << 8));
710 if (Al > 0 && pred >= (1 << Al))
711 pred = (1 << Al) - 1;
712 } else {
713 pred = (int)(((Q03 << 7) - num) / (Q03 << 8));
714 if (Al > 0 && pred >= (1 << Al))
715 pred = (1 << Al) - 1;
716 pred = -pred;
717 }
718 workspace[3] = (JCOEF)pred;
719 }
720 /* AC12 */
721 if ((Al = coef_bits[7]) != 0 && workspace[10] == 0) {
722 num = Q00 * (DC07 - 3 * DC08 + DC09 - DC17 + 3 * DC18 - DC19);
723 if (num >= 0) {
724 pred = (int)(((Q12 << 7) + num) / (Q12 << 8));
725 if (Al > 0 && pred >= (1 << Al))
726 pred = (1 << Al) - 1;
727 } else {
728 pred = (int)(((Q12 << 7) - num) / (Q12 << 8));
729 if (Al > 0 && pred >= (1 << Al))
730 pred = (1 << Al) - 1;
731 pred = -pred;
732 }
733 workspace[10] = (JCOEF)pred;
734 }
735 /* AC21 */
736 if ((Al = coef_bits[8]) != 0 && workspace[17] == 0) {
737 num = Q00 * (DC07 - DC09 - 3 * DC12 + 3 * DC14 + DC17 - DC19);
738 if (num >= 0) {
739 pred = (int)(((Q21 << 7) + num) / (Q21 << 8));
740 if (Al > 0 && pred >= (1 << Al))
741 pred = (1 << Al) - 1;
742 } else {
743 pred = (int)(((Q21 << 7) - num) / (Q21 << 8));
744 if (Al > 0 && pred >= (1 << Al))
745 pred = (1 << Al) - 1;
746 pred = -pred;
747 }
748 workspace[17] = (JCOEF)pred;
749 }
750 /* AC30 */
751 if ((Al = coef_bits[9]) != 0 && workspace[24] == 0) {
752 num = Q00 * (DC07 + 2 * DC08 + DC09 - DC17 - 2 * DC18 - DC19);
753 if (num >= 0) {
754 pred = (int)(((Q30 << 7) + num) / (Q30 << 8));
755 if (Al > 0 && pred >= (1 << Al))
756 pred = (1 << Al) - 1;
757 } else {
758 pred = (int)(((Q30 << 7) - num) / (Q30 << 8));
759 if (Al > 0 && pred >= (1 << Al))
760 pred = (1 << Al) - 1;
761 pred = -pred;
762 }
763 workspace[24] = (JCOEF)pred;
764 }
765 /* coef_bits[0] is non-negative. Otherwise this function would not
766 * be called.
767 */
768 num = Q00 *
769 (-2 * DC01 - 6 * DC02 - 8 * DC03 - 6 * DC04 - 2 * DC05 -
770 6 * DC06 + 6 * DC07 + 42 * DC08 + 6 * DC09 - 6 * DC10 -
771 8 * DC11 + 42 * DC12 + 152 * DC13 + 42 * DC14 - 8 * DC15 -
772 6 * DC16 + 6 * DC17 + 42 * DC18 + 6 * DC19 - 6 * DC20 -
773 2 * DC21 - 6 * DC22 - 8 * DC23 - 6 * DC24 - 2 * DC25);
774 if (num >= 0) {
775 pred = (int)(((Q00 << 7) + num) / (Q00 << 8));
776 } else {
777 pred = (int)(((Q00 << 7) - num) / (Q00 << 8));
778 pred = -pred;
779 }
780 workspace[0] = (JCOEF)pred;
781 } /* change_dc */
782
783 /* OK, do the IDCT */
784 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR)workspace, output_ptr,
785 output_col);
786 /* Advance for next column */
787 DC01 = DC02; DC02 = DC03; DC03 = DC04; DC04 = DC05;
788 DC06 = DC07; DC07 = DC08; DC08 = DC09; DC09 = DC10;
789 DC11 = DC12; DC12 = DC13; DC13 = DC14; DC14 = DC15;
790 DC16 = DC17; DC17 = DC18; DC18 = DC19; DC19 = DC20;
791 DC21 = DC22; DC22 = DC23; DC23 = DC24; DC24 = DC25;
792 buffer_ptr++, prev_block_row++, next_block_row++,
793 prev_prev_block_row++, next_next_block_row++;
794 output_col += compptr->_DCT_scaled_size;
795 }
796 output_ptr += compptr->_DCT_scaled_size;
797 }
798 }
799
800 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
801 return JPEG_ROW_COMPLETED;
802 return JPEG_SCAN_COMPLETED;
803}
804
805#endif /* BLOCK_SMOOTHING_SUPPORTED */
806
807
808/*
809 * Initialize coefficient buffer controller.
810 */
811
812GLOBAL(void)
813jinit_d_coef_controller(j_decompress_ptr cinfo, boolean need_full_buffer)
814{
815 my_coef_ptr coef;
816
817 coef = (my_coef_ptr)
818 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
819 sizeof(my_coef_controller));
820 cinfo->coef = (struct jpeg_d_coef_controller *)coef;
821 coef->pub.start_input_pass = start_input_pass;
822 coef->pub.start_output_pass = start_output_pass;
823#ifdef BLOCK_SMOOTHING_SUPPORTED
824 coef->coef_bits_latch = NULL;
825#endif
826
827 /* Create the coefficient buffer. */
828 if (need_full_buffer) {
829#ifdef D_MULTISCAN_FILES_SUPPORTED
830 /* Allocate a full-image virtual array for each component, */
831 /* padded to a multiple of samp_factor DCT blocks in each direction. */
832 /* Note we ask for a pre-zeroed array. */
833 int ci, access_rows;
834 jpeg_component_info *compptr;
835
836 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
837 ci++, compptr++) {
838 access_rows = compptr->v_samp_factor;
839#ifdef BLOCK_SMOOTHING_SUPPORTED
840 /* If block smoothing could be used, need a bigger window */
841 if (cinfo->progressive_mode)
842 access_rows *= 5;
843#endif
844 coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
845 ((j_common_ptr)cinfo, JPOOL_IMAGE, TRUE,
846 (JDIMENSION)jround_up((long)compptr->width_in_blocks,
847 (long)compptr->h_samp_factor),
848 (JDIMENSION)jround_up((long)compptr->height_in_blocks,
849 (long)compptr->v_samp_factor),
850 (JDIMENSION)access_rows);
851 }
852 coef->pub.consume_data = consume_data;
853 coef->pub.decompress_data = decompress_data;
854 coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
855#else
856 ERREXIT(cinfo, JERR_NOT_COMPILED);
857#endif
858 } else {
859 /* We only need a single-MCU buffer. */
860 JBLOCKROW buffer;
861 int i;
862
863 buffer = (JBLOCKROW)
864 (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
865 D_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
866 for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
867 coef->MCU_buffer[i] = buffer + i;
868 }
869 coef->pub.consume_data = dummy_consume_data;
870 coef->pub.decompress_data = decompress_onepass;
871 coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
872 }
873
874 /* Allocate the workspace buffer */
875 coef->workspace = (JCOEF *)
876 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
877 sizeof(JCOEF) * DCTSIZE2);
878}
879