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