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