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