1/*
2 * jdcoefct.c
3 *
4 * Copyright (C) 1994-1997, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
7 *
8 * This file contains the coefficient buffer controller for decompression.
9 * This controller is the top level of the JPEG decompressor proper.
10 * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
11 *
12 * In buffered-image mode, this controller is the interface between
13 * input-oriented processing and output-oriented processing.
14 * Also, the input side (only) is used when reading a file for transcoding.
15 */
16
17#define JPEG_INTERNALS
18#include "jinclude.h"
19#include "jpeglib.h"
20
21/* Block smoothing is only applicable for progressive JPEG, so: */
22#ifndef D_PROGRESSIVE_SUPPORTED
23#undef BLOCK_SMOOTHING_SUPPORTED
24#endif
25
26/* Private buffer controller object */
27
28typedef struct {
29  struct jpeg_d_coef_controller pub; /* public fields */
30
31  /* These variables keep track of the current location of the input side. */
32  /* cinfo->input_iMCU_row is also used for this. */
33  JDIMENSION MCU_ctr;		/* counts MCUs processed in current row */
34  int MCU_vert_offset;		/* counts MCU rows within iMCU row */
35  int MCU_rows_per_iMCU_row;	/* number of such rows needed */
36
37  /* The output side's location is represented by cinfo->output_iMCU_row. */
38
39  /* In single-pass modes, it's sufficient to buffer just one MCU.
40   * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
41   * and let the entropy decoder write into that workspace each time.
42   * (On 80x86, the workspace is FAR even though it's not really very big;
43   * this is to keep the module interfaces unchanged when a large coefficient
44   * buffer is necessary.)
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#ifdef D_MULTISCAN_FILES_SUPPORTED
51  /* In multi-pass modes, we need a virtual block array for each component. */
52  jvirt_barray_ptr whole_image[MAX_COMPONENTS];
53#endif
54
55#ifdef BLOCK_SMOOTHING_SUPPORTED
56  /* When doing block smoothing, we latch coefficient Al values here */
57  int * coef_bits_latch;
58#define SAVED_COEFS  6		/* we save coef_bits[0..5] */
59#endif
60} my_coef_controller;
61
62typedef my_coef_controller * my_coef_ptr;
63
64/* Forward declarations */
65METHODDEF(int) decompress_onepass
66	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
67#ifdef D_MULTISCAN_FILES_SUPPORTED
68METHODDEF(int) decompress_data
69	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
70#endif
71#ifdef BLOCK_SMOOTHING_SUPPORTED
72LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
73METHODDEF(int) decompress_smooth_data
74	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
75#endif
76
77
78LOCAL(void)
79start_iMCU_row (j_decompress_ptr cinfo)
80/* Reset within-iMCU-row counters for a new row (input side) */
81{
82  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
83
84  /* In an interleaved scan, an MCU row is the same as an iMCU row.
85   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
86   * But at the bottom of the image, process only what's left.
87   */
88  if (cinfo->comps_in_scan > 1) {
89    coef->MCU_rows_per_iMCU_row = 1;
90  } else {
91    if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
92      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
93    else
94      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
95  }
96
97  coef->MCU_ctr = 0;
98  coef->MCU_vert_offset = 0;
99}
100
101
102/*
103 * Initialize for an input processing pass.
104 */
105
106METHODDEF(void)
107start_input_pass (j_decompress_ptr cinfo)
108{
109  cinfo->input_iMCU_row = 0;
110  start_iMCU_row(cinfo);
111}
112
113
114/*
115 * Initialize for an output processing pass.
116 */
117
118METHODDEF(void)
119start_output_pass (j_decompress_ptr cinfo)
120{
121#ifdef BLOCK_SMOOTHING_SUPPORTED
122  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
123
124  /* If multipass, check to see whether to use block smoothing on this pass */
125  if (coef->pub.coef_arrays != NULL) {
126    if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
127      coef->pub.decompress_data = decompress_smooth_data;
128    else
129      coef->pub.decompress_data = decompress_data;
130  }
131#endif
132  cinfo->output_iMCU_row = 0;
133}
134
135
136/*
137 * Decompress and return some data in the single-pass case.
138 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
139 * Input and output must run in lockstep since we have only a one-MCU buffer.
140 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
141 *
142 * NB: output_buf contains a plane for each component in image,
143 * which we index according to the component's SOF position.
144 */
145
146METHODDEF(int)
147decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
148{
149  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
150  JDIMENSION MCU_col_num;	/* index of current MCU within row */
151  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
152  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
153  int blkn, ci, xindex, yindex, yoffset, useful_width;
154  JSAMPARRAY output_ptr;
155  JDIMENSION start_col, output_col;
156  jpeg_component_info *compptr;
157  inverse_DCT_method_ptr inverse_DCT;
158
159#ifdef ANDROID_TILE_BASED_DECODE
160  if (cinfo->tile_decode) {
161    last_MCU_col =
162        (cinfo->coef->MCU_column_right_boundary -
163         cinfo->coef->MCU_column_left_boundary) - 1;
164  }
165#endif
166
167  /* Loop to process as much as one whole iMCU row */
168  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
169       yoffset++) {
170    for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
171	 MCU_col_num++) {
172      /* Try to fetch an MCU.  Entropy decoder expects buffer to be zeroed. */
173      if (MCU_col_num < coef->pub.MCU_columns_to_skip) {
174        (*cinfo->entropy->decode_mcu_discard_coef) (cinfo);
175        continue;
176      } else {
177        jzero_far((void FAR *) coef->MCU_buffer[0],
178		(size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
179        if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
180	  /* Suspension forced; update state counters and exit */
181	  coef->MCU_vert_offset = yoffset;
182	  coef->MCU_ctr = MCU_col_num;
183	  return JPEG_SUSPENDED;
184        }
185      }
186      /* Determine where data should go in output_buf and do the IDCT thing.
187       * We skip dummy blocks at the right and bottom edges (but blkn gets
188       * incremented past them!).  Note the inner loop relies on having
189       * allocated the MCU_buffer[] blocks sequentially.
190       */
191      blkn = 0;			/* index of current DCT block within MCU */
192      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
193	compptr = cinfo->cur_comp_info[ci];
194	/* Don't bother to IDCT an uninteresting component. */
195	if (! compptr->component_needed) {
196	  blkn += compptr->MCU_blocks;
197	  continue;
198	}
199	inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
200	useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
201						    : compptr->last_col_width;
202	output_ptr = output_buf[compptr->component_index] +
203	  yoffset * compptr->DCT_scaled_size;
204	start_col = MCU_col_num * compptr->MCU_sample_width;
205	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
206	  if (cinfo->input_iMCU_row < last_iMCU_row ||
207	      yoffset+yindex < compptr->last_row_height) {
208	    output_col = start_col;
209	    for (xindex = 0; xindex < useful_width; xindex++) {
210	      (*inverse_DCT) (cinfo, compptr,
211		        (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
212		        output_ptr, output_col);
213	      output_col += compptr->DCT_scaled_size;
214	    }
215	  }
216	  blkn += compptr->MCU_width;
217	  output_ptr += compptr->DCT_scaled_size;
218	}
219      }
220    }
221    /* Completed an MCU row, but perhaps not an iMCU row */
222    coef->MCU_ctr = 0;
223  }
224  /* Completed the iMCU row, advance counters for next one */
225  cinfo->output_iMCU_row++;
226  if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
227    start_iMCU_row(cinfo);
228    return JPEG_ROW_COMPLETED;
229  }
230  /* Completed the scan */
231  (*cinfo->inputctl->finish_input_pass) (cinfo);
232  return JPEG_SCAN_COMPLETED;
233}
234
235
236/*
237 * Dummy consume-input routine for single-pass operation.
238 */
239
240METHODDEF(int)
241dummy_consume_data (j_decompress_ptr cinfo)
242{
243  return JPEG_SUSPENDED;	/* Always indicate nothing was done */
244}
245
246#ifdef D_MULTISCAN_FILES_SUPPORTED
247/*
248 * Consume input data and store it in the full-image coefficient buffer.
249 * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
250 * ie, v_samp_factor block rows for each component in the scan.
251 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
252 */
253
254METHODDEF(int)
255consume_data (j_decompress_ptr cinfo)
256{
257  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
258  JDIMENSION MCU_col_num;	/* index of current MCU within row */
259  int blkn, ci, xindex, yindex, yoffset;
260  JDIMENSION start_col;
261  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
262  JBLOCKROW buffer_ptr;
263  jpeg_component_info *compptr;
264
265  /* Align the virtual buffers for the components used in this scan. */
266  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
267    compptr = cinfo->cur_comp_info[ci];
268    buffer[ci] = (*cinfo->mem->access_virt_barray)
269      ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
270       cinfo->tile_decode ? 0 : cinfo->input_iMCU_row * compptr->v_samp_factor,
271       (JDIMENSION) compptr->v_samp_factor, TRUE);
272    /* Note: entropy decoder expects buffer to be zeroed,
273     * but this is handled automatically by the memory manager
274     * because we requested a pre-zeroed array.
275     */
276  }
277  unsigned int MCUs_per_row = cinfo->MCUs_per_row;
278#ifdef ANDROID_TILE_BASED_DECODE
279  if (cinfo->tile_decode) {
280    int iMCU_width_To_MCU_width;
281    if (cinfo->comps_in_scan > 1) {
282      // Interleaved
283      iMCU_width_To_MCU_width = 1;
284    } else {
285      // Non-intervleaved
286      iMCU_width_To_MCU_width = cinfo->cur_comp_info[0]->h_samp_factor;
287    }
288    MCUs_per_row = jmin(MCUs_per_row,
289        (cinfo->coef->column_right_boundary - cinfo->coef->column_left_boundary)
290        * cinfo->entropy->index->MCU_sample_size * iMCU_width_To_MCU_width);
291  }
292#endif
293
294  /* Loop to process one whole iMCU row */
295  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
296       yoffset++) {
297   // configure huffman decoder
298#ifdef ANDROID_TILE_BASED_DECODE
299    if (cinfo->tile_decode) {
300      huffman_scan_header scan_header =
301            cinfo->entropy->index->scan[cinfo->input_scan_number];
302      int col_offset = cinfo->coef->column_left_boundary;
303      (*cinfo->entropy->configure_huffman_decoder) (cinfo,
304              scan_header.offset[cinfo->input_iMCU_row]
305              [col_offset + yoffset * scan_header.MCUs_per_row]);
306    }
307#endif
308
309    // zero all blocks
310    for (MCU_col_num = coef->MCU_ctr; MCU_col_num < MCUs_per_row;
311          MCU_col_num++) {
312      /* Construct list of pointers to DCT blocks belonging to this MCU */
313      blkn = 0;			/* index of current DCT block within MCU */
314      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
315        compptr = cinfo->cur_comp_info[ci];
316        start_col = MCU_col_num * compptr->MCU_width;
317        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
318          buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
319          for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
320            coef->MCU_buffer[blkn++] = buffer_ptr++;
321#ifdef ANDROID_TILE_BASED_DECODE
322            if (cinfo->tile_decode && cinfo->input_scan_number == 0) {
323              // need to do pre-zero ourselves.
324              jzero_far((void FAR *) coef->MCU_buffer[blkn-1],
325                        (size_t) (SIZEOF(JBLOCK)));
326            }
327#endif
328          }
329        }
330      }
331
332
333      /* Try to fetch the MCU. */
334      if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
335        /* Suspension forced; update state counters and exit */
336        coef->MCU_vert_offset = yoffset;
337        coef->MCU_ctr = MCU_col_num;
338        return JPEG_SUSPENDED;
339      }
340    }
341    /* Completed an MCU row, but perhaps not an iMCU row */
342    coef->MCU_ctr = 0;
343  }
344  /* Completed the iMCU row, advance counters for next one */
345  if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
346    start_iMCU_row(cinfo);
347    return JPEG_ROW_COMPLETED;
348  }
349  /* Completed the scan */
350  (*cinfo->inputctl->finish_input_pass) (cinfo);
351  return JPEG_SCAN_COMPLETED;
352}
353
354/*
355 * Consume input data and store it in the coefficient buffer.
356 * Read one fully interleaved MCU row ("iMCU" row) per call.
357 */
358
359METHODDEF(int)
360consume_data_multi_scan (j_decompress_ptr cinfo)
361{
362  huffman_index *index = cinfo->entropy->index;
363  int i, retcode, ci;
364  int mcu = cinfo->input_iMCU_row;
365  jinit_phuff_decoder(cinfo);
366  for (i = 0; i < index->scan_count; i++) {
367    (*cinfo->inputctl->finish_input_pass) (cinfo);
368    jset_input_stream_position(cinfo, index->scan[i].bitstream_offset);
369    cinfo->output_iMCU_row = mcu;
370    cinfo->unread_marker = 0;
371    // Consume SOS and DHT headers
372    retcode = (*cinfo->inputctl->consume_markers) (cinfo, index, i);
373    cinfo->input_iMCU_row = mcu;
374    cinfo->input_scan_number = i;
375    cinfo->entropy->index = index;
376    // Consume scan block data
377    consume_data(cinfo);
378  }
379  cinfo->input_iMCU_row = mcu + 1;
380  cinfo->input_scan_number = 0;
381  cinfo->output_scan_number = 0;
382  return JPEG_ROW_COMPLETED;
383}
384
385/*
386 * Same as consume_data, expect for saving the Huffman decode information
387 * - bitstream offset and DC coefficient to index.
388 */
389
390METHODDEF(int)
391consume_data_build_huffman_index_baseline (j_decompress_ptr cinfo,
392        huffman_index *index, int current_scan)
393{
394  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
395  JDIMENSION MCU_col_num;	/* index of current MCU within row */
396  int ci, xindex, yindex, yoffset;
397  JDIMENSION start_col;
398  JBLOCKROW buffer_ptr;
399
400  huffman_scan_header *scan_header = index->scan + current_scan;
401  scan_header->MCU_rows_per_iMCU_row = coef->MCU_rows_per_iMCU_row;
402
403  size_t allocate_size = coef->MCU_rows_per_iMCU_row
404      * jdiv_round_up(cinfo->MCUs_per_row, index->MCU_sample_size)
405      * sizeof(huffman_offset_data);
406  scan_header->offset[cinfo->input_iMCU_row] =
407        (huffman_offset_data*)malloc(allocate_size);
408  index->mem_used += allocate_size;
409
410  huffman_offset_data *offset_data = scan_header->offset[cinfo->input_iMCU_row];
411
412  /* Loop to process one whole iMCU row */
413  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
414       yoffset++) {
415    for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
416	 MCU_col_num++) {
417      // Record huffman bit offset
418      if (MCU_col_num % index->MCU_sample_size == 0) {
419        (*cinfo->entropy->get_huffman_decoder_configuration)
420                (cinfo, offset_data);
421        ++offset_data;
422      }
423
424      /* Try to fetch the MCU. */
425      if (! (*cinfo->entropy->decode_mcu_discard_coef) (cinfo)) {
426        /* Suspension forced; update state counters and exit */
427        coef->MCU_vert_offset = yoffset;
428        coef->MCU_ctr = MCU_col_num;
429        return JPEG_SUSPENDED;
430      }
431    }
432    /* Completed an MCU row, but perhaps not an iMCU row */
433    coef->MCU_ctr = 0;
434  }
435  /* Completed the iMCU row, advance counters for next one */
436  if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
437    start_iMCU_row(cinfo);
438    return JPEG_ROW_COMPLETED;
439  }
440  /* Completed the scan */
441  (*cinfo->inputctl->finish_input_pass) (cinfo);
442  return JPEG_SCAN_COMPLETED;
443}
444
445/*
446 * Same as consume_data, expect for saving the Huffman decode information
447 * - bitstream offset and DC coefficient to index.
448 */
449
450METHODDEF(int)
451consume_data_build_huffman_index_progressive (j_decompress_ptr cinfo,
452        huffman_index *index, int current_scan)
453{
454  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
455  JDIMENSION MCU_col_num;	/* index of current MCU within row */
456  int blkn, ci, xindex, yindex, yoffset;
457  JDIMENSION start_col;
458  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
459  JBLOCKROW buffer_ptr;
460  jpeg_component_info *compptr;
461
462  int factor = 4; // maximum factor is 4.
463  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
464    factor = jmin(factor, cinfo->cur_comp_info[ci]->h_samp_factor);
465
466  int sample_size = index->MCU_sample_size * factor;
467  huffman_scan_header *scan_header = index->scan + current_scan;
468  scan_header->MCU_rows_per_iMCU_row = coef->MCU_rows_per_iMCU_row;
469  scan_header->MCUs_per_row = jdiv_round_up(cinfo->MCUs_per_row, sample_size);
470  scan_header->comps_in_scan = cinfo->comps_in_scan;
471
472  size_t allocate_size = coef->MCU_rows_per_iMCU_row
473      * scan_header->MCUs_per_row * sizeof(huffman_offset_data);
474  scan_header->offset[cinfo->input_iMCU_row] =
475        (huffman_offset_data*)malloc(allocate_size);
476  index->mem_used += allocate_size;
477
478  huffman_offset_data *offset_data = scan_header->offset[cinfo->input_iMCU_row];
479
480  /* Align the virtual buffers for the components used in this scan. */
481  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
482    compptr = cinfo->cur_comp_info[ci];
483    buffer[ci] = (*cinfo->mem->access_virt_barray)
484      ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
485       0, // Only need one row buffer
486       (JDIMENSION) compptr->v_samp_factor, TRUE);
487  }
488  /* Loop to process one whole iMCU row */
489  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
490       yoffset++) {
491    for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
492	 MCU_col_num++) {
493      /* For each MCU, we loop through different color components.
494       * Then, for each color component we will get a list of pointers to DCT
495       * blocks in the virtual buffer.
496       */
497      blkn = 0; /* index of current DCT block within MCU */
498      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
499        compptr = cinfo->cur_comp_info[ci];
500        start_col = MCU_col_num * compptr->MCU_width;
501        /* Get the list of pointers to DCT blocks in
502         * the virtual buffer in a color component of the MCU.
503         */
504        for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
505          buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
506          for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
507            coef->MCU_buffer[blkn++] = buffer_ptr++;
508            if (cinfo->input_scan_number == 0) {
509              // need to do pre-zero by ourself.
510              jzero_far((void FAR *) coef->MCU_buffer[blkn-1],
511                        (size_t) (SIZEOF(JBLOCK)));
512            }
513          }
514        }
515      }
516      // Record huffman bit offset
517      if (MCU_col_num % sample_size == 0) {
518        (*cinfo->entropy->get_huffman_decoder_configuration)
519                (cinfo, offset_data);
520        ++offset_data;
521      }
522      /* Try to fetch the MCU. */
523      if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
524	/* Suspension forced; update state counters and exit */
525	coef->MCU_vert_offset = yoffset;
526	coef->MCU_ctr = MCU_col_num;
527	return JPEG_SUSPENDED;
528      }
529    }
530    /* Completed an MCU row, but perhaps not an iMCU row */
531    coef->MCU_ctr = 0;
532  }
533  (*cinfo->entropy->get_huffman_decoder_configuration)
534        (cinfo, &scan_header->prev_MCU_offset);
535  /* Completed the iMCU row, advance counters for next one */
536  if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
537    start_iMCU_row(cinfo);
538    return JPEG_ROW_COMPLETED;
539  }
540  /* Completed the scan */
541  (*cinfo->inputctl->finish_input_pass) (cinfo);
542  return JPEG_SCAN_COMPLETED;
543}
544
545/*
546 * Decompress and return some data in the multi-pass case.
547 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
548 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
549 *
550 * NB: output_buf contains a plane for each component in image.
551 */
552
553METHODDEF(int)
554decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
555{
556  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
557  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
558  JDIMENSION block_num;
559  int ci, block_row, block_rows;
560  JBLOCKARRAY buffer;
561  JBLOCKROW buffer_ptr;
562  JSAMPARRAY output_ptr;
563  JDIMENSION output_col;
564  jpeg_component_info *compptr;
565  inverse_DCT_method_ptr inverse_DCT;
566
567  /* Force some input to be done if we are getting ahead of the input. */
568  while (cinfo->input_scan_number < cinfo->output_scan_number ||
569	 (cinfo->input_scan_number == cinfo->output_scan_number &&
570	  cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
571    if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
572      return JPEG_SUSPENDED;
573  }
574
575  /* OK, output from the virtual arrays. */
576  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
577       ci++, compptr++) {
578    /* Don't bother to IDCT an uninteresting component. */
579    if (! compptr->component_needed)
580      continue;
581    /* Align the virtual buffer for this component. */
582    buffer = (*cinfo->mem->access_virt_barray)
583      ((j_common_ptr) cinfo, coef->whole_image[ci],
584       cinfo->tile_decode ? 0 : cinfo->output_iMCU_row * compptr->v_samp_factor,
585       (JDIMENSION) compptr->v_samp_factor, FALSE);
586    /* Count non-dummy DCT block rows in this iMCU row. */
587    if (cinfo->output_iMCU_row < last_iMCU_row)
588      block_rows = compptr->v_samp_factor;
589    else {
590      /* NB: can't use last_row_height here; it is input-side-dependent! */
591      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
592      if (block_rows == 0) block_rows = compptr->v_samp_factor;
593    }
594    inverse_DCT = cinfo->idct->inverse_DCT[ci];
595    output_ptr = output_buf[ci];
596    int width_in_blocks = compptr->width_in_blocks;
597    int start_block = 0;
598#if ANDROID_TILE_BASED_DECODE
599    if (cinfo->tile_decode) {
600      // width_in_blocks for a component depends on its h_samp_factor.
601      width_in_blocks = jmin(width_in_blocks,
602        (cinfo->coef->MCU_column_right_boundary -
603         cinfo->coef->MCU_column_left_boundary) *
604         compptr->h_samp_factor);
605      start_block = coef->pub.MCU_columns_to_skip *
606         compptr->h_samp_factor;
607   }
608#endif
609    /* Loop over all DCT blocks to be processed. */
610    for (block_row = 0; block_row < block_rows; block_row++) {
611      buffer_ptr = buffer[block_row];
612      output_col = start_block * compptr->DCT_scaled_size;
613      buffer_ptr += start_block;
614      for (block_num = start_block; block_num < width_in_blocks; block_num++) {
615	(*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
616			output_ptr, output_col);
617	buffer_ptr++;
618	output_col += compptr->DCT_scaled_size;
619      }
620      output_ptr += compptr->DCT_scaled_size;
621    }
622  }
623
624  if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
625    return JPEG_ROW_COMPLETED;
626  return JPEG_SCAN_COMPLETED;
627}
628
629#endif /* D_MULTISCAN_FILES_SUPPORTED */
630
631
632#ifdef BLOCK_SMOOTHING_SUPPORTED
633
634/*
635 * This code applies interblock smoothing as described by section K.8
636 * of the JPEG standard: the first 5 AC coefficients are estimated from
637 * the DC values of a DCT block and its 8 neighboring blocks.
638 * We apply smoothing only for progressive JPEG decoding, and only if
639 * the coefficients it can estimate are not yet known to full precision.
640 */
641
642/* Natural-order array positions of the first 5 zigzag-order coefficients */
643#define Q01_POS  1
644#define Q10_POS  8
645#define Q20_POS  16
646#define Q11_POS  9
647#define Q02_POS  2
648
649/*
650 * Determine whether block smoothing is applicable and safe.
651 * We also latch the current states of the coef_bits[] entries for the
652 * AC coefficients; otherwise, if the input side of the decompressor
653 * advances into a new scan, we might think the coefficients are known
654 * more accurately than they really are.
655 */
656
657LOCAL(boolean)
658smoothing_ok (j_decompress_ptr cinfo)
659{
660  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
661  boolean smoothing_useful = FALSE;
662  int ci, coefi;
663  jpeg_component_info *compptr;
664  JQUANT_TBL * qtable;
665  int * coef_bits;
666  int * coef_bits_latch;
667
668  if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
669    return FALSE;
670
671  /* Allocate latch area if not already done */
672  if (coef->coef_bits_latch == NULL)
673    coef->coef_bits_latch = (int *)
674      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
675				  cinfo->num_components *
676				  (SAVED_COEFS * SIZEOF(int)));
677  coef_bits_latch = coef->coef_bits_latch;
678
679  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
680       ci++, compptr++) {
681    /* All components' quantization values must already be latched. */
682    if ((qtable = compptr->quant_table) == NULL)
683      return FALSE;
684    /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
685    if (qtable->quantval[0] == 0 ||
686	qtable->quantval[Q01_POS] == 0 ||
687	qtable->quantval[Q10_POS] == 0 ||
688	qtable->quantval[Q20_POS] == 0 ||
689	qtable->quantval[Q11_POS] == 0 ||
690	qtable->quantval[Q02_POS] == 0)
691      return FALSE;
692    /* DC values must be at least partly known for all components. */
693    coef_bits = cinfo->coef_bits[ci];
694    if (coef_bits[0] < 0)
695      return FALSE;
696    /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
697    for (coefi = 1; coefi <= 5; coefi++) {
698      coef_bits_latch[coefi] = coef_bits[coefi];
699      if (coef_bits[coefi] != 0)
700	smoothing_useful = TRUE;
701    }
702    coef_bits_latch += SAVED_COEFS;
703  }
704
705  return smoothing_useful;
706}
707
708
709/*
710 * Variant of decompress_data for use when doing block smoothing.
711 */
712
713METHODDEF(int)
714decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
715{
716  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
717  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
718  JDIMENSION block_num, last_block_column;
719  int ci, block_row, block_rows, access_rows;
720  JBLOCKARRAY buffer;
721  JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
722  JSAMPARRAY output_ptr;
723  JDIMENSION output_col;
724  jpeg_component_info *compptr;
725  inverse_DCT_method_ptr inverse_DCT;
726  boolean first_row, last_row;
727  JBLOCK workspace;
728  int *coef_bits;
729  JQUANT_TBL *quanttbl;
730  INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
731  int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
732  int Al, pred;
733
734  /* Force some input to be done if we are getting ahead of the input. */
735  while (cinfo->input_scan_number <= cinfo->output_scan_number &&
736	 ! cinfo->inputctl->eoi_reached) {
737    if (cinfo->input_scan_number == cinfo->output_scan_number) {
738      /* If input is working on current scan, we ordinarily want it to
739       * have completed the current row.  But if input scan is DC,
740       * we want it to keep one row ahead so that next block row's DC
741       * values are up to date.
742       */
743      JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
744      if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
745	break;
746    }
747    if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
748      return JPEG_SUSPENDED;
749  }
750
751  /* OK, output from the virtual arrays. */
752  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
753       ci++, compptr++) {
754    /* Don't bother to IDCT an uninteresting component. */
755    if (! compptr->component_needed)
756      continue;
757    /* Count non-dummy DCT block rows in this iMCU row. */
758    if (cinfo->output_iMCU_row < last_iMCU_row) {
759      block_rows = compptr->v_samp_factor;
760      access_rows = block_rows * 2; /* this and next iMCU row */
761      last_row = FALSE;
762    } else {
763      /* NB: can't use last_row_height here; it is input-side-dependent! */
764      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
765      if (block_rows == 0) block_rows = compptr->v_samp_factor;
766      access_rows = block_rows; /* this iMCU row only */
767      last_row = TRUE;
768    }
769    /* Align the virtual buffer for this component. */
770    if (cinfo->output_iMCU_row > 0) {
771      access_rows += compptr->v_samp_factor; /* prior iMCU row too */
772      buffer = (*cinfo->mem->access_virt_barray)
773	((j_common_ptr) cinfo, coef->whole_image[ci],
774	 (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
775	 (JDIMENSION) access_rows, FALSE);
776      buffer += compptr->v_samp_factor;	/* point to current iMCU row */
777      first_row = FALSE;
778    } else {
779      buffer = (*cinfo->mem->access_virt_barray)
780	((j_common_ptr) cinfo, coef->whole_image[ci],
781	 (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
782      first_row = TRUE;
783    }
784    /* Fetch component-dependent info */
785    coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
786    quanttbl = compptr->quant_table;
787    Q00 = quanttbl->quantval[0];
788    Q01 = quanttbl->quantval[Q01_POS];
789    Q10 = quanttbl->quantval[Q10_POS];
790    Q20 = quanttbl->quantval[Q20_POS];
791    Q11 = quanttbl->quantval[Q11_POS];
792    Q02 = quanttbl->quantval[Q02_POS];
793    inverse_DCT = cinfo->idct->inverse_DCT[ci];
794    output_ptr = output_buf[ci];
795    /* Loop over all DCT blocks to be processed. */
796    for (block_row = 0; block_row < block_rows; block_row++) {
797      buffer_ptr = buffer[block_row];
798      if (first_row && block_row == 0)
799	prev_block_row = buffer_ptr;
800      else
801	prev_block_row = buffer[block_row-1];
802      if (last_row && block_row == block_rows-1)
803	next_block_row = buffer_ptr;
804      else
805	next_block_row = buffer[block_row+1];
806      /* We fetch the surrounding DC values using a sliding-register approach.
807       * Initialize all nine here so as to do the right thing on narrow pics.
808       */
809      DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
810      DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
811      DC7 = DC8 = DC9 = (int) next_block_row[0][0];
812      output_col = 0;
813      last_block_column = compptr->width_in_blocks - 1;
814      for (block_num = 0; block_num <= last_block_column; block_num++) {
815	/* Fetch current DCT block into workspace so we can modify it. */
816	jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
817	/* Update DC values */
818	if (block_num < last_block_column) {
819	  DC3 = (int) prev_block_row[1][0];
820	  DC6 = (int) buffer_ptr[1][0];
821	  DC9 = (int) next_block_row[1][0];
822	}
823	/* Compute coefficient estimates per K.8.
824	 * An estimate is applied only if coefficient is still zero,
825	 * and is not known to be fully accurate.
826	 */
827	/* AC01 */
828	if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
829	  num = 36 * Q00 * (DC4 - DC6);
830	  if (num >= 0) {
831	    pred = (int) (((Q01<<7) + num) / (Q01<<8));
832	    if (Al > 0 && pred >= (1<<Al))
833	      pred = (1<<Al)-1;
834	  } else {
835	    pred = (int) (((Q01<<7) - num) / (Q01<<8));
836	    if (Al > 0 && pred >= (1<<Al))
837	      pred = (1<<Al)-1;
838	    pred = -pred;
839	  }
840	  workspace[1] = (JCOEF) pred;
841	}
842	/* AC10 */
843	if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
844	  num = 36 * Q00 * (DC2 - DC8);
845	  if (num >= 0) {
846	    pred = (int) (((Q10<<7) + num) / (Q10<<8));
847	    if (Al > 0 && pred >= (1<<Al))
848	      pred = (1<<Al)-1;
849	  } else {
850	    pred = (int) (((Q10<<7) - num) / (Q10<<8));
851	    if (Al > 0 && pred >= (1<<Al))
852	      pred = (1<<Al)-1;
853	    pred = -pred;
854	  }
855	  workspace[8] = (JCOEF) pred;
856	}
857	/* AC20 */
858	if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
859	  num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
860	  if (num >= 0) {
861	    pred = (int) (((Q20<<7) + num) / (Q20<<8));
862	    if (Al > 0 && pred >= (1<<Al))
863	      pred = (1<<Al)-1;
864	  } else {
865	    pred = (int) (((Q20<<7) - num) / (Q20<<8));
866	    if (Al > 0 && pred >= (1<<Al))
867	      pred = (1<<Al)-1;
868	    pred = -pred;
869	  }
870	  workspace[16] = (JCOEF) pred;
871	}
872	/* AC11 */
873	if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
874	  num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
875	  if (num >= 0) {
876	    pred = (int) (((Q11<<7) + num) / (Q11<<8));
877	    if (Al > 0 && pred >= (1<<Al))
878	      pred = (1<<Al)-1;
879	  } else {
880	    pred = (int) (((Q11<<7) - num) / (Q11<<8));
881	    if (Al > 0 && pred >= (1<<Al))
882	      pred = (1<<Al)-1;
883	    pred = -pred;
884	  }
885	  workspace[9] = (JCOEF) pred;
886	}
887	/* AC02 */
888	if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
889	  num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
890	  if (num >= 0) {
891	    pred = (int) (((Q02<<7) + num) / (Q02<<8));
892	    if (Al > 0 && pred >= (1<<Al))
893	      pred = (1<<Al)-1;
894	  } else {
895	    pred = (int) (((Q02<<7) - num) / (Q02<<8));
896	    if (Al > 0 && pred >= (1<<Al))
897	      pred = (1<<Al)-1;
898	    pred = -pred;
899	  }
900	  workspace[2] = (JCOEF) pred;
901	}
902	/* OK, do the IDCT */
903	(*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
904			output_ptr, output_col);
905	/* Advance for next column */
906	DC1 = DC2; DC2 = DC3;
907	DC4 = DC5; DC5 = DC6;
908	DC7 = DC8; DC8 = DC9;
909	buffer_ptr++, prev_block_row++, next_block_row++;
910	output_col += compptr->DCT_scaled_size;
911      }
912      output_ptr += compptr->DCT_scaled_size;
913    }
914  }
915
916  if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
917    return JPEG_ROW_COMPLETED;
918  return JPEG_SCAN_COMPLETED;
919}
920
921#endif /* BLOCK_SMOOTHING_SUPPORTED */
922
923
924/*
925 * Initialize coefficient buffer controller.
926 */
927
928GLOBAL(void)
929jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
930{
931  my_coef_ptr coef;
932
933  coef = (my_coef_ptr)
934    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
935				SIZEOF(my_coef_controller));
936  cinfo->coef = (struct jpeg_d_coef_controller *) coef;
937  coef->pub.start_input_pass = start_input_pass;
938  coef->pub.start_output_pass = start_output_pass;
939  coef->pub.column_left_boundary = 0;
940  coef->pub.column_right_boundary = 0;
941  coef->pub.MCU_columns_to_skip = 0;
942#ifdef BLOCK_SMOOTHING_SUPPORTED
943  coef->coef_bits_latch = NULL;
944#endif
945
946#ifdef ANDROID_TILE_BASED_DECODE
947  if (cinfo->tile_decode) {
948    if (cinfo->progressive_mode) {
949      /* Allocate one iMCU row virtual array, coef->whole_image[ci],
950       * for each color component, padded to a multiple of h_samp_factor
951       * DCT blocks in the horizontal direction.
952       */
953      int ci, access_rows;
954      jpeg_component_info *compptr;
955
956      for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
957	   ci++, compptr++) {
958        access_rows = compptr->v_samp_factor;
959        coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
960	  ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
961	   (JDIMENSION) jround_up((long) compptr->width_in_blocks,
962				(long) compptr->h_samp_factor),
963	   (JDIMENSION) compptr->v_samp_factor, // one iMCU row
964	   (JDIMENSION) access_rows);
965      }
966      coef->pub.consume_data_build_huffman_index =
967            consume_data_build_huffman_index_progressive;
968      coef->pub.consume_data = consume_data_multi_scan;
969      coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
970      coef->pub.decompress_data = decompress_onepass;
971    } else {
972      /* We only need a single-MCU buffer. */
973      JBLOCKROW buffer;
974      int i;
975
976      buffer = (JBLOCKROW)
977      (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
978				  D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
979      for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
980        coef->MCU_buffer[i] = buffer + i;
981      }
982      coef->pub.consume_data_build_huffman_index =
983            consume_data_build_huffman_index_baseline;
984      coef->pub.consume_data = dummy_consume_data;
985      coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
986      coef->pub.decompress_data = decompress_onepass;
987    }
988    return;
989  }
990#endif
991
992  /* Create the coefficient buffer. */
993  if (need_full_buffer) {
994#ifdef D_MULTISCAN_FILES_SUPPORTED
995    /* Allocate a full-image virtual array for each component, */
996    /* padded to a multiple of samp_factor DCT blocks in each direction. */
997    /* Note we ask for a pre-zeroed array. */
998    int ci, access_rows;
999    jpeg_component_info *compptr;
1000
1001    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
1002	 ci++, compptr++) {
1003      access_rows = compptr->v_samp_factor;
1004#ifdef BLOCK_SMOOTHING_SUPPORTED
1005      /* If block smoothing could be used, need a bigger window */
1006      if (cinfo->progressive_mode)
1007	access_rows *= 3;
1008#endif
1009      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
1010	((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
1011	 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
1012				(long) compptr->h_samp_factor),
1013	 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
1014				(long) compptr->v_samp_factor),
1015	 (JDIMENSION) access_rows);
1016    }
1017    coef->pub.consume_data = consume_data;
1018    coef->pub.decompress_data = decompress_data;
1019    coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
1020#else
1021    ERREXIT(cinfo, JERR_NOT_COMPILED);
1022#endif
1023  } else {
1024    /* We only need a single-MCU buffer. */
1025    JBLOCKROW buffer;
1026    int i;
1027
1028    buffer = (JBLOCKROW)
1029      (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
1030		  D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
1031    for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
1032      coef->MCU_buffer[i] = buffer + i;
1033    }
1034    coef->pub.consume_data = dummy_consume_data;
1035    coef->pub.decompress_data = decompress_onepass;
1036    coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
1037  }
1038}
1039