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