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