kusano 7d535a
/*
kusano 7d535a
 * jdcoefct.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1994-1997, Thomas G. Lane.
kusano 7d535a
 * Modified 2002-2011 by Guido Vollbeding.
kusano 7d535a
 * This file is part of the Independent JPEG Group's software.
kusano 7d535a
 * For conditions of distribution and use, see the accompanying README file.
kusano 7d535a
 *
kusano 7d535a
 * This file contains the coefficient buffer controller for decompression.
kusano 7d535a
 * This controller is the top level of the JPEG decompressor proper.
kusano 7d535a
 * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
kusano 7d535a
 *
kusano 7d535a
 * In buffered-image mode, this controller is the interface between
kusano 7d535a
 * input-oriented processing and output-oriented processing.
kusano 7d535a
 * Also, the input side (only) is used when reading a file for transcoding.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define JPEG_INTERNALS
kusano 7d535a
#include "jinclude.h"
kusano 7d535a
#include "jpeglib.h"
kusano 7d535a
kusano 7d535a
/* Block smoothing is only applicable for progressive JPEG, so: */
kusano 7d535a
#ifndef D_PROGRESSIVE_SUPPORTED
kusano 7d535a
#undef BLOCK_SMOOTHING_SUPPORTED
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
/* Private buffer controller object */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  struct jpeg_d_coef_controller pub; /* public fields */
kusano 7d535a
kusano 7d535a
  /* These variables keep track of the current location of the input side. */
kusano 7d535a
  /* cinfo->input_iMCU_row is also used for this. */
kusano 7d535a
  JDIMENSION MCU_ctr;		/* counts MCUs processed in current row */
kusano 7d535a
  int MCU_vert_offset;		/* counts MCU rows within iMCU row */
kusano 7d535a
  int MCU_rows_per_iMCU_row;	/* number of such rows needed */
kusano 7d535a
kusano 7d535a
  /* The output side's location is represented by cinfo->output_iMCU_row. */
kusano 7d535a
kusano 7d535a
  /* In single-pass modes, it's sufficient to buffer just one MCU.
kusano 7d535a
   * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
kusano 7d535a
   * and let the entropy decoder write into that workspace each time.
kusano 7d535a
   * (On 80x86, the workspace is FAR even though it's not really very big;
kusano 7d535a
   * this is to keep the module interfaces unchanged when a large coefficient
kusano 7d535a
   * buffer is necessary.)
kusano 7d535a
   * In multi-pass modes, this array points to the current MCU's blocks
kusano 7d535a
   * within the virtual arrays; it is used only by the input side.
kusano 7d535a
   */
kusano 7d535a
  JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];
kusano 7d535a
kusano 7d535a
#ifdef D_MULTISCAN_FILES_SUPPORTED
kusano 7d535a
  /* In multi-pass modes, we need a virtual block array for each component. */
kusano 7d535a
  jvirt_barray_ptr whole_image[MAX_COMPONENTS];
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#ifdef BLOCK_SMOOTHING_SUPPORTED
kusano 7d535a
  /* When doing block smoothing, we latch coefficient Al values here */
kusano 7d535a
  int * coef_bits_latch;
kusano 7d535a
#define SAVED_COEFS  6		/* we save coef_bits[0..5] */
kusano 7d535a
#endif
kusano 7d535a
} my_coef_controller;
kusano 7d535a
kusano 7d535a
typedef my_coef_controller * my_coef_ptr;
kusano 7d535a
kusano 7d535a
/* Forward declarations */
kusano 7d535a
METHODDEF(int) decompress_onepass
kusano 7d535a
	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
kusano 7d535a
#ifdef D_MULTISCAN_FILES_SUPPORTED
kusano 7d535a
METHODDEF(int) decompress_data
kusano 7d535a
	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
kusano 7d535a
#endif
kusano 7d535a
#ifdef BLOCK_SMOOTHING_SUPPORTED
kusano 7d535a
LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
kusano 7d535a
METHODDEF(int) decompress_smooth_data
kusano 7d535a
	JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
start_iMCU_row (j_decompress_ptr cinfo)
kusano 7d535a
/* Reset within-iMCU-row counters for a new row (input side) */
kusano 7d535a
{
kusano 7d535a
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
kusano 7d535a
kusano 7d535a
  /* In an interleaved scan, an MCU row is the same as an iMCU row.
kusano 7d535a
   * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
kusano 7d535a
   * But at the bottom of the image, process only what's left.
kusano 7d535a
   */
kusano 7d535a
  if (cinfo->comps_in_scan > 1) {
kusano 7d535a
    coef->MCU_rows_per_iMCU_row = 1;
kusano 7d535a
  } else {
kusano 7d535a
    if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
kusano 7d535a
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
kusano 7d535a
    else
kusano 7d535a
      coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  coef->MCU_ctr = 0;
kusano 7d535a
  coef->MCU_vert_offset = 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize for an input processing pass.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
start_input_pass (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  cinfo->input_iMCU_row = 0;
kusano 7d535a
  start_iMCU_row(cinfo);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize for an output processing pass.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
start_output_pass (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
#ifdef BLOCK_SMOOTHING_SUPPORTED
kusano 7d535a
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
kusano 7d535a
kusano 7d535a
  /* If multipass, check to see whether to use block smoothing on this pass */
kusano 7d535a
  if (coef->pub.coef_arrays != NULL) {
kusano 7d535a
    if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
kusano 7d535a
      coef->pub.decompress_data = decompress_smooth_data;
kusano 7d535a
    else
kusano 7d535a
      coef->pub.decompress_data = decompress_data;
kusano 7d535a
  }
kusano 7d535a
#endif
kusano 7d535a
  cinfo->output_iMCU_row = 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Decompress and return some data in the single-pass case.
kusano 7d535a
 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
kusano 7d535a
 * Input and output must run in lockstep since we have only a one-MCU buffer.
kusano 7d535a
 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
kusano 7d535a
 *
kusano 7d535a
 * NB: output_buf contains a plane for each component in image,
kusano 7d535a
 * which we index according to the component's SOF position.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(int)
kusano 7d535a
decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
kusano 7d535a
{
kusano 7d535a
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
kusano 7d535a
  JDIMENSION MCU_col_num;	/* index of current MCU within row */
kusano 7d535a
  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
kusano 7d535a
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
kusano 7d535a
  int blkn, ci, xindex, yindex, yoffset, useful_width;
kusano 7d535a
  JSAMPARRAY output_ptr;
kusano 7d535a
  JDIMENSION start_col, output_col;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
  inverse_DCT_method_ptr inverse_DCT;
kusano 7d535a
kusano 7d535a
  /* Loop to process as much as one whole iMCU row */
kusano 7d535a
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
kusano 7d535a
       yoffset++) {
kusano 7d535a
    for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
kusano 7d535a
	 MCU_col_num++) {
kusano 7d535a
      /* Try to fetch an MCU.  Entropy decoder expects buffer to be zeroed. */
kusano 7d535a
      if (cinfo->lim_Se)	/* can bypass in DC only case */
kusano 7d535a
	FMEMZERO((void FAR *) coef->MCU_buffer[0],
kusano 7d535a
		 (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
kusano 7d535a
      if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
kusano 7d535a
	/* Suspension forced; update state counters and exit */
kusano 7d535a
	coef->MCU_vert_offset = yoffset;
kusano 7d535a
	coef->MCU_ctr = MCU_col_num;
kusano 7d535a
	return JPEG_SUSPENDED;
kusano 7d535a
      }
kusano 7d535a
      /* Determine where data should go in output_buf and do the IDCT thing.
kusano 7d535a
       * We skip dummy blocks at the right and bottom edges (but blkn gets
kusano 7d535a
       * incremented past them!).  Note the inner loop relies on having
kusano 7d535a
       * allocated the MCU_buffer[] blocks sequentially.
kusano 7d535a
       */
kusano 7d535a
      blkn = 0;			/* index of current DCT block within MCU */
kusano 7d535a
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
kusano 7d535a
	compptr = cinfo->cur_comp_info[ci];
kusano 7d535a
	/* Don't bother to IDCT an uninteresting component. */
kusano 7d535a
	if (! compptr->component_needed) {
kusano 7d535a
	  blkn += compptr->MCU_blocks;
kusano 7d535a
	  continue;
kusano 7d535a
	}
kusano 7d535a
	inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
kusano 7d535a
	useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
kusano 7d535a
						    : compptr->last_col_width;
kusano 7d535a
	output_ptr = output_buf[compptr->component_index] +
kusano 7d535a
	  yoffset * compptr->DCT_v_scaled_size;
kusano 7d535a
	start_col = MCU_col_num * compptr->MCU_sample_width;
kusano 7d535a
	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
kusano 7d535a
	  if (cinfo->input_iMCU_row < last_iMCU_row ||
kusano 7d535a
	      yoffset+yindex < compptr->last_row_height) {
kusano 7d535a
	    output_col = start_col;
kusano 7d535a
	    for (xindex = 0; xindex < useful_width; xindex++) {
kusano 7d535a
	      (*inverse_DCT) (cinfo, compptr,
kusano 7d535a
			      (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
kusano 7d535a
			      output_ptr, output_col);
kusano 7d535a
	      output_col += compptr->DCT_h_scaled_size;
kusano 7d535a
	    }
kusano 7d535a
	  }
kusano 7d535a
	  blkn += compptr->MCU_width;
kusano 7d535a
	  output_ptr += compptr->DCT_v_scaled_size;
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
    /* Completed an MCU row, but perhaps not an iMCU row */
kusano 7d535a
    coef->MCU_ctr = 0;
kusano 7d535a
  }
kusano 7d535a
  /* Completed the iMCU row, advance counters for next one */
kusano 7d535a
  cinfo->output_iMCU_row++;
kusano 7d535a
  if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
kusano 7d535a
    start_iMCU_row(cinfo);
kusano 7d535a
    return JPEG_ROW_COMPLETED;
kusano 7d535a
  }
kusano 7d535a
  /* Completed the scan */
kusano 7d535a
  (*cinfo->inputctl->finish_input_pass) (cinfo);
kusano 7d535a
  return JPEG_SCAN_COMPLETED;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Dummy consume-input routine for single-pass operation.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(int)
kusano 7d535a
dummy_consume_data (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  return JPEG_SUSPENDED;	/* Always indicate nothing was done */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
#ifdef D_MULTISCAN_FILES_SUPPORTED
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Consume input data and store it in the full-image coefficient buffer.
kusano 7d535a
 * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
kusano 7d535a
 * ie, v_samp_factor block rows for each component in the scan.
kusano 7d535a
 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(int)
kusano 7d535a
consume_data (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
kusano 7d535a
  JDIMENSION MCU_col_num;	/* index of current MCU within row */
kusano 7d535a
  int blkn, ci, xindex, yindex, yoffset;
kusano 7d535a
  JDIMENSION start_col;
kusano 7d535a
  JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
kusano 7d535a
  JBLOCKROW buffer_ptr;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
kusano 7d535a
  /* Align the virtual buffers for the components used in this scan. */
kusano 7d535a
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
kusano 7d535a
    compptr = cinfo->cur_comp_info[ci];
kusano 7d535a
    buffer[ci] = (*cinfo->mem->access_virt_barray)
kusano 7d535a
      ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
kusano 7d535a
       cinfo->input_iMCU_row * compptr->v_samp_factor,
kusano 7d535a
       (JDIMENSION) compptr->v_samp_factor, TRUE);
kusano 7d535a
    /* Note: entropy decoder expects buffer to be zeroed,
kusano 7d535a
     * but this is handled automatically by the memory manager
kusano 7d535a
     * because we requested a pre-zeroed array.
kusano 7d535a
     */
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Loop to process one whole iMCU row */
kusano 7d535a
  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
kusano 7d535a
       yoffset++) {
kusano 7d535a
    for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
kusano 7d535a
	 MCU_col_num++) {
kusano 7d535a
      /* Construct list of pointers to DCT blocks belonging to this MCU */
kusano 7d535a
      blkn = 0;			/* index of current DCT block within MCU */
kusano 7d535a
      for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
kusano 7d535a
	compptr = cinfo->cur_comp_info[ci];
kusano 7d535a
	start_col = MCU_col_num * compptr->MCU_width;
kusano 7d535a
	for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
kusano 7d535a
	  buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
kusano 7d535a
	  for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
kusano 7d535a
	    coef->MCU_buffer[blkn++] = buffer_ptr++;
kusano 7d535a
	  }
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
      /* Try to fetch the MCU. */
kusano 7d535a
      if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
kusano 7d535a
	/* Suspension forced; update state counters and exit */
kusano 7d535a
	coef->MCU_vert_offset = yoffset;
kusano 7d535a
	coef->MCU_ctr = MCU_col_num;
kusano 7d535a
	return JPEG_SUSPENDED;
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
    /* Completed an MCU row, but perhaps not an iMCU row */
kusano 7d535a
    coef->MCU_ctr = 0;
kusano 7d535a
  }
kusano 7d535a
  /* Completed the iMCU row, advance counters for next one */
kusano 7d535a
  if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
kusano 7d535a
    start_iMCU_row(cinfo);
kusano 7d535a
    return JPEG_ROW_COMPLETED;
kusano 7d535a
  }
kusano 7d535a
  /* Completed the scan */
kusano 7d535a
  (*cinfo->inputctl->finish_input_pass) (cinfo);
kusano 7d535a
  return JPEG_SCAN_COMPLETED;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Decompress and return some data in the multi-pass case.
kusano 7d535a
 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
kusano 7d535a
 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
kusano 7d535a
 *
kusano 7d535a
 * NB: output_buf contains a plane for each component in image.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(int)
kusano 7d535a
decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
kusano 7d535a
{
kusano 7d535a
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
kusano 7d535a
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
kusano 7d535a
  JDIMENSION block_num;
kusano 7d535a
  int ci, block_row, block_rows;
kusano 7d535a
  JBLOCKARRAY buffer;
kusano 7d535a
  JBLOCKROW buffer_ptr;
kusano 7d535a
  JSAMPARRAY output_ptr;
kusano 7d535a
  JDIMENSION output_col;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
  inverse_DCT_method_ptr inverse_DCT;
kusano 7d535a
kusano 7d535a
  /* Force some input to be done if we are getting ahead of the input. */
kusano 7d535a
  while (cinfo->input_scan_number < cinfo->output_scan_number ||
kusano 7d535a
	 (cinfo->input_scan_number == cinfo->output_scan_number &&
kusano 7d535a
	  cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
kusano 7d535a
    if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
kusano 7d535a
      return JPEG_SUSPENDED;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* OK, output from the virtual arrays. */
kusano 7d535a
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
       ci++, compptr++) {
kusano 7d535a
    /* Don't bother to IDCT an uninteresting component. */
kusano 7d535a
    if (! compptr->component_needed)
kusano 7d535a
      continue;
kusano 7d535a
    /* Align the virtual buffer for this component. */
kusano 7d535a
    buffer = (*cinfo->mem->access_virt_barray)
kusano 7d535a
      ((j_common_ptr) cinfo, coef->whole_image[ci],
kusano 7d535a
       cinfo->output_iMCU_row * compptr->v_samp_factor,
kusano 7d535a
       (JDIMENSION) compptr->v_samp_factor, FALSE);
kusano 7d535a
    /* Count non-dummy DCT block rows in this iMCU row. */
kusano 7d535a
    if (cinfo->output_iMCU_row < last_iMCU_row)
kusano 7d535a
      block_rows = compptr->v_samp_factor;
kusano 7d535a
    else {
kusano 7d535a
      /* NB: can't use last_row_height here; it is input-side-dependent! */
kusano 7d535a
      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
kusano 7d535a
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
kusano 7d535a
    }
kusano 7d535a
    inverse_DCT = cinfo->idct->inverse_DCT[ci];
kusano 7d535a
    output_ptr = output_buf[ci];
kusano 7d535a
    /* Loop over all DCT blocks to be processed. */
kusano 7d535a
    for (block_row = 0; block_row < block_rows; block_row++) {
kusano 7d535a
      buffer_ptr = buffer[block_row];
kusano 7d535a
      output_col = 0;
kusano 7d535a
      for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
kusano 7d535a
	(*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
kusano 7d535a
			output_ptr, output_col);
kusano 7d535a
	buffer_ptr++;
kusano 7d535a
	output_col += compptr->DCT_h_scaled_size;
kusano 7d535a
      }
kusano 7d535a
      output_ptr += compptr->DCT_v_scaled_size;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
kusano 7d535a
    return JPEG_ROW_COMPLETED;
kusano 7d535a
  return JPEG_SCAN_COMPLETED;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif /* D_MULTISCAN_FILES_SUPPORTED */
kusano 7d535a
kusano 7d535a
kusano 7d535a
#ifdef BLOCK_SMOOTHING_SUPPORTED
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * This code applies interblock smoothing as described by section K.8
kusano 7d535a
 * of the JPEG standard: the first 5 AC coefficients are estimated from
kusano 7d535a
 * the DC values of a DCT block and its 8 neighboring blocks.
kusano 7d535a
 * We apply smoothing only for progressive JPEG decoding, and only if
kusano 7d535a
 * the coefficients it can estimate are not yet known to full precision.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
/* Natural-order array positions of the first 5 zigzag-order coefficients */
kusano 7d535a
#define Q01_POS  1
kusano 7d535a
#define Q10_POS  8
kusano 7d535a
#define Q20_POS  16
kusano 7d535a
#define Q11_POS  9
kusano 7d535a
#define Q02_POS  2
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Determine whether block smoothing is applicable and safe.
kusano 7d535a
 * We also latch the current states of the coef_bits[] entries for the
kusano 7d535a
 * AC coefficients; otherwise, if the input side of the decompressor
kusano 7d535a
 * advances into a new scan, we might think the coefficients are known
kusano 7d535a
 * more accurately than they really are.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(boolean)
kusano 7d535a
smoothing_ok (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
kusano 7d535a
  boolean smoothing_useful = FALSE;
kusano 7d535a
  int ci, coefi;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
  JQUANT_TBL * qtable;
kusano 7d535a
  int * coef_bits;
kusano 7d535a
  int * coef_bits_latch;
kusano 7d535a
kusano 7d535a
  if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
kusano 7d535a
    return FALSE;
kusano 7d535a
kusano 7d535a
  /* Allocate latch area if not already done */
kusano 7d535a
  if (coef->coef_bits_latch == NULL)
kusano 7d535a
    coef->coef_bits_latch = (int *)
kusano 7d535a
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				  cinfo->num_components *
kusano 7d535a
				  (SAVED_COEFS * SIZEOF(int)));
kusano 7d535a
  coef_bits_latch = coef->coef_bits_latch;
kusano 7d535a
kusano 7d535a
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
       ci++, compptr++) {
kusano 7d535a
    /* All components' quantization values must already be latched. */
kusano 7d535a
    if ((qtable = compptr->quant_table) == NULL)
kusano 7d535a
      return FALSE;
kusano 7d535a
    /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
kusano 7d535a
    if (qtable->quantval[0] == 0 ||
kusano 7d535a
	qtable->quantval[Q01_POS] == 0 ||
kusano 7d535a
	qtable->quantval[Q10_POS] == 0 ||
kusano 7d535a
	qtable->quantval[Q20_POS] == 0 ||
kusano 7d535a
	qtable->quantval[Q11_POS] == 0 ||
kusano 7d535a
	qtable->quantval[Q02_POS] == 0)
kusano 7d535a
      return FALSE;
kusano 7d535a
    /* DC values must be at least partly known for all components. */
kusano 7d535a
    coef_bits = cinfo->coef_bits[ci];
kusano 7d535a
    if (coef_bits[0] < 0)
kusano 7d535a
      return FALSE;
kusano 7d535a
    /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
kusano 7d535a
    for (coefi = 1; coefi <= 5; coefi++) {
kusano 7d535a
      coef_bits_latch[coefi] = coef_bits[coefi];
kusano 7d535a
      if (coef_bits[coefi] != 0)
kusano 7d535a
	smoothing_useful = TRUE;
kusano 7d535a
    }
kusano 7d535a
    coef_bits_latch += SAVED_COEFS;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  return smoothing_useful;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Variant of decompress_data for use when doing block smoothing.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(int)
kusano 7d535a
decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
kusano 7d535a
{
kusano 7d535a
  my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
kusano 7d535a
  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
kusano 7d535a
  JDIMENSION block_num, last_block_column;
kusano 7d535a
  int ci, block_row, block_rows, access_rows;
kusano 7d535a
  JBLOCKARRAY buffer;
kusano 7d535a
  JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
kusano 7d535a
  JSAMPARRAY output_ptr;
kusano 7d535a
  JDIMENSION output_col;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
  inverse_DCT_method_ptr inverse_DCT;
kusano 7d535a
  boolean first_row, last_row;
kusano 7d535a
  JBLOCK workspace;
kusano 7d535a
  int *coef_bits;
kusano 7d535a
  JQUANT_TBL *quanttbl;
kusano 7d535a
  INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
kusano 7d535a
  int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
kusano 7d535a
  int Al, pred;
kusano 7d535a
kusano 7d535a
  /* Force some input to be done if we are getting ahead of the input. */
kusano 7d535a
  while (cinfo->input_scan_number <= cinfo->output_scan_number &&
kusano 7d535a
	 ! cinfo->inputctl->eoi_reached) {
kusano 7d535a
    if (cinfo->input_scan_number == cinfo->output_scan_number) {
kusano 7d535a
      /* If input is working on current scan, we ordinarily want it to
kusano 7d535a
       * have completed the current row.  But if input scan is DC,
kusano 7d535a
       * we want it to keep one row ahead so that next block row's DC
kusano 7d535a
       * values are up to date.
kusano 7d535a
       */
kusano 7d535a
      JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
kusano 7d535a
      if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
kusano 7d535a
	break;
kusano 7d535a
    }
kusano 7d535a
    if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
kusano 7d535a
      return JPEG_SUSPENDED;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* OK, output from the virtual arrays. */
kusano 7d535a
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
       ci++, compptr++) {
kusano 7d535a
    /* Don't bother to IDCT an uninteresting component. */
kusano 7d535a
    if (! compptr->component_needed)
kusano 7d535a
      continue;
kusano 7d535a
    /* Count non-dummy DCT block rows in this iMCU row. */
kusano 7d535a
    if (cinfo->output_iMCU_row < last_iMCU_row) {
kusano 7d535a
      block_rows = compptr->v_samp_factor;
kusano 7d535a
      access_rows = block_rows * 2; /* this and next iMCU row */
kusano 7d535a
      last_row = FALSE;
kusano 7d535a
    } else {
kusano 7d535a
      /* NB: can't use last_row_height here; it is input-side-dependent! */
kusano 7d535a
      block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
kusano 7d535a
      if (block_rows == 0) block_rows = compptr->v_samp_factor;
kusano 7d535a
      access_rows = block_rows; /* this iMCU row only */
kusano 7d535a
      last_row = TRUE;
kusano 7d535a
    }
kusano 7d535a
    /* Align the virtual buffer for this component. */
kusano 7d535a
    if (cinfo->output_iMCU_row > 0) {
kusano 7d535a
      access_rows += compptr->v_samp_factor; /* prior iMCU row too */
kusano 7d535a
      buffer = (*cinfo->mem->access_virt_barray)
kusano 7d535a
	((j_common_ptr) cinfo, coef->whole_image[ci],
kusano 7d535a
	 (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
kusano 7d535a
	 (JDIMENSION) access_rows, FALSE);
kusano 7d535a
      buffer += compptr->v_samp_factor;	/* point to current iMCU row */
kusano 7d535a
      first_row = FALSE;
kusano 7d535a
    } else {
kusano 7d535a
      buffer = (*cinfo->mem->access_virt_barray)
kusano 7d535a
	((j_common_ptr) cinfo, coef->whole_image[ci],
kusano 7d535a
	 (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
kusano 7d535a
      first_row = TRUE;
kusano 7d535a
    }
kusano 7d535a
    /* Fetch component-dependent info */
kusano 7d535a
    coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
kusano 7d535a
    quanttbl = compptr->quant_table;
kusano 7d535a
    Q00 = quanttbl->quantval[0];
kusano 7d535a
    Q01 = quanttbl->quantval[Q01_POS];
kusano 7d535a
    Q10 = quanttbl->quantval[Q10_POS];
kusano 7d535a
    Q20 = quanttbl->quantval[Q20_POS];
kusano 7d535a
    Q11 = quanttbl->quantval[Q11_POS];
kusano 7d535a
    Q02 = quanttbl->quantval[Q02_POS];
kusano 7d535a
    inverse_DCT = cinfo->idct->inverse_DCT[ci];
kusano 7d535a
    output_ptr = output_buf[ci];
kusano 7d535a
    /* Loop over all DCT blocks to be processed. */
kusano 7d535a
    for (block_row = 0; block_row < block_rows; block_row++) {
kusano 7d535a
      buffer_ptr = buffer[block_row];
kusano 7d535a
      if (first_row && block_row == 0)
kusano 7d535a
	prev_block_row = buffer_ptr;
kusano 7d535a
      else
kusano 7d535a
	prev_block_row = buffer[block_row-1];
kusano 7d535a
      if (last_row && block_row == block_rows-1)
kusano 7d535a
	next_block_row = buffer_ptr;
kusano 7d535a
      else
kusano 7d535a
	next_block_row = buffer[block_row+1];
kusano 7d535a
      /* We fetch the surrounding DC values using a sliding-register approach.
kusano 7d535a
       * Initialize all nine here so as to do the right thing on narrow pics.
kusano 7d535a
       */
kusano 7d535a
      DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
kusano 7d535a
      DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
kusano 7d535a
      DC7 = DC8 = DC9 = (int) next_block_row[0][0];
kusano 7d535a
      output_col = 0;
kusano 7d535a
      last_block_column = compptr->width_in_blocks - 1;
kusano 7d535a
      for (block_num = 0; block_num <= last_block_column; block_num++) {
kusano 7d535a
	/* Fetch current DCT block into workspace so we can modify it. */
kusano 7d535a
	jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
kusano 7d535a
	/* Update DC values */
kusano 7d535a
	if (block_num < last_block_column) {
kusano 7d535a
	  DC3 = (int) prev_block_row[1][0];
kusano 7d535a
	  DC6 = (int) buffer_ptr[1][0];
kusano 7d535a
	  DC9 = (int) next_block_row[1][0];
kusano 7d535a
	}
kusano 7d535a
	/* Compute coefficient estimates per K.8.
kusano 7d535a
	 * An estimate is applied only if coefficient is still zero,
kusano 7d535a
	 * and is not known to be fully accurate.
kusano 7d535a
	 */
kusano 7d535a
	/* AC01 */
kusano 7d535a
	if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
kusano 7d535a
	  num = 36 * Q00 * (DC4 - DC6);
kusano 7d535a
	  if (num >= 0) {
kusano 7d535a
	    pred = (int) (((Q01<<7) + num) / (Q01<<8));
kusano 7d535a
	    if (Al > 0 && pred >= (1<
kusano 7d535a
	      pred = (1<
kusano 7d535a
	  } else {
kusano 7d535a
	    pred = (int) (((Q01<<7) - num) / (Q01<<8));
kusano 7d535a
	    if (Al > 0 && pred >= (1<
kusano 7d535a
	      pred = (1<
kusano 7d535a
	    pred = -pred;
kusano 7d535a
	  }
kusano 7d535a
	  workspace[1] = (JCOEF) pred;
kusano 7d535a
	}
kusano 7d535a
	/* AC10 */
kusano 7d535a
	if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
kusano 7d535a
	  num = 36 * Q00 * (DC2 - DC8);
kusano 7d535a
	  if (num >= 0) {
kusano 7d535a
	    pred = (int) (((Q10<<7) + num) / (Q10<<8));
kusano 7d535a
	    if (Al > 0 && pred >= (1<
kusano 7d535a
	      pred = (1<
kusano 7d535a
	  } else {
kusano 7d535a
	    pred = (int) (((Q10<<7) - num) / (Q10<<8));
kusano 7d535a
	    if (Al > 0 && pred >= (1<
kusano 7d535a
	      pred = (1<
kusano 7d535a
	    pred = -pred;
kusano 7d535a
	  }
kusano 7d535a
	  workspace[8] = (JCOEF) pred;
kusano 7d535a
	}
kusano 7d535a
	/* AC20 */
kusano 7d535a
	if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
kusano 7d535a
	  num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
kusano 7d535a
	  if (num >= 0) {
kusano 7d535a
	    pred = (int) (((Q20<<7) + num) / (Q20<<8));
kusano 7d535a
	    if (Al > 0 && pred >= (1<
kusano 7d535a
	      pred = (1<
kusano 7d535a
	  } else {
kusano 7d535a
	    pred = (int) (((Q20<<7) - num) / (Q20<<8));
kusano 7d535a
	    if (Al > 0 && pred >= (1<
kusano 7d535a
	      pred = (1<
kusano 7d535a
	    pred = -pred;
kusano 7d535a
	  }
kusano 7d535a
	  workspace[16] = (JCOEF) pred;
kusano 7d535a
	}
kusano 7d535a
	/* AC11 */
kusano 7d535a
	if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
kusano 7d535a
	  num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
kusano 7d535a
	  if (num >= 0) {
kusano 7d535a
	    pred = (int) (((Q11<<7) + num) / (Q11<<8));
kusano 7d535a
	    if (Al > 0 && pred >= (1<
kusano 7d535a
	      pred = (1<
kusano 7d535a
	  } else {
kusano 7d535a
	    pred = (int) (((Q11<<7) - num) / (Q11<<8));
kusano 7d535a
	    if (Al > 0 && pred >= (1<
kusano 7d535a
	      pred = (1<
kusano 7d535a
	    pred = -pred;
kusano 7d535a
	  }
kusano 7d535a
	  workspace[9] = (JCOEF) pred;
kusano 7d535a
	}
kusano 7d535a
	/* AC02 */
kusano 7d535a
	if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
kusano 7d535a
	  num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
kusano 7d535a
	  if (num >= 0) {
kusano 7d535a
	    pred = (int) (((Q02<<7) + num) / (Q02<<8));
kusano 7d535a
	    if (Al > 0 && pred >= (1<
kusano 7d535a
	      pred = (1<
kusano 7d535a
	  } else {
kusano 7d535a
	    pred = (int) (((Q02<<7) - num) / (Q02<<8));
kusano 7d535a
	    if (Al > 0 && pred >= (1<
kusano 7d535a
	      pred = (1<
kusano 7d535a
	    pred = -pred;
kusano 7d535a
	  }
kusano 7d535a
	  workspace[2] = (JCOEF) pred;
kusano 7d535a
	}
kusano 7d535a
	/* OK, do the IDCT */
kusano 7d535a
	(*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
kusano 7d535a
			output_ptr, output_col);
kusano 7d535a
	/* Advance for next column */
kusano 7d535a
	DC1 = DC2; DC2 = DC3;
kusano 7d535a
	DC4 = DC5; DC5 = DC6;
kusano 7d535a
	DC7 = DC8; DC8 = DC9;
kusano 7d535a
	buffer_ptr++, prev_block_row++, next_block_row++;
kusano 7d535a
	output_col += compptr->DCT_h_scaled_size;
kusano 7d535a
      }
kusano 7d535a
      output_ptr += compptr->DCT_v_scaled_size;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
kusano 7d535a
    return JPEG_ROW_COMPLETED;
kusano 7d535a
  return JPEG_SCAN_COMPLETED;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif /* BLOCK_SMOOTHING_SUPPORTED */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize coefficient buffer controller.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
kusano 7d535a
{
kusano 7d535a
  my_coef_ptr coef;
kusano 7d535a
kusano 7d535a
  coef = (my_coef_ptr)
kusano 7d535a
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				SIZEOF(my_coef_controller));
kusano 7d535a
  cinfo->coef = (struct jpeg_d_coef_controller *) coef;
kusano 7d535a
  coef->pub.start_input_pass = start_input_pass;
kusano 7d535a
  coef->pub.start_output_pass = start_output_pass;
kusano 7d535a
#ifdef BLOCK_SMOOTHING_SUPPORTED
kusano 7d535a
  coef->coef_bits_latch = NULL;
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
  /* Create the coefficient buffer. */
kusano 7d535a
  if (need_full_buffer) {
kusano 7d535a
#ifdef D_MULTISCAN_FILES_SUPPORTED
kusano 7d535a
    /* Allocate a full-image virtual array for each component, */
kusano 7d535a
    /* padded to a multiple of samp_factor DCT blocks in each direction. */
kusano 7d535a
    /* Note we ask for a pre-zeroed array. */
kusano 7d535a
    int ci, access_rows;
kusano 7d535a
    jpeg_component_info *compptr;
kusano 7d535a
kusano 7d535a
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
	 ci++, compptr++) {
kusano 7d535a
      access_rows = compptr->v_samp_factor;
kusano 7d535a
#ifdef BLOCK_SMOOTHING_SUPPORTED
kusano 7d535a
      /* If block smoothing could be used, need a bigger window */
kusano 7d535a
      if (cinfo->progressive_mode)
kusano 7d535a
	access_rows *= 3;
kusano 7d535a
#endif
kusano 7d535a
      coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
kusano 7d535a
	((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
kusano 7d535a
	 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
kusano 7d535a
				(long) compptr->h_samp_factor),
kusano 7d535a
	 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
kusano 7d535a
				(long) compptr->v_samp_factor),
kusano 7d535a
	 (JDIMENSION) access_rows);
kusano 7d535a
    }
kusano 7d535a
    coef->pub.consume_data = consume_data;
kusano 7d535a
    coef->pub.decompress_data = decompress_data;
kusano 7d535a
    coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
kusano 7d535a
#else
kusano 7d535a
    ERREXIT(cinfo, JERR_NOT_COMPILED);
kusano 7d535a
#endif
kusano 7d535a
  } else {
kusano 7d535a
    /* We only need a single-MCU buffer. */
kusano 7d535a
    JBLOCKROW buffer;
kusano 7d535a
    int i;
kusano 7d535a
kusano 7d535a
    buffer = (JBLOCKROW)
kusano 7d535a
      (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				  D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
kusano 7d535a
    for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
kusano 7d535a
      coef->MCU_buffer[i] = buffer + i;
kusano 7d535a
    }
kusano 7d535a
    if (cinfo->lim_Se == 0)	/* DC only case: want to bypass later */
kusano 7d535a
      FMEMZERO((void FAR *) buffer,
kusano 7d535a
	       (size_t) (D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)));
kusano 7d535a
    coef->pub.consume_data = dummy_consume_data;
kusano 7d535a
    coef->pub.decompress_data = decompress_onepass;
kusano 7d535a
    coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
kusano 7d535a
  }
kusano 7d535a
}