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