kusano 7d535a
/*
kusano 7d535a
 * jddctmgr.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1994-1996, Thomas G. Lane.
kusano 7d535a
 * Modified 2002-2010 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 inverse-DCT management logic.
kusano 7d535a
 * This code selects a particular IDCT implementation to be used,
kusano 7d535a
 * and it performs related housekeeping chores.  No code in this file
kusano 7d535a
 * is executed per IDCT step, only during output pass setup.
kusano 7d535a
 *
kusano 7d535a
 * Note that the IDCT routines are responsible for performing coefficient
kusano 7d535a
 * dequantization as well as the IDCT proper.  This module sets up the
kusano 7d535a
 * dequantization multiplier table needed by the IDCT routine.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define JPEG_INTERNALS
kusano 7d535a
#include "jinclude.h"
kusano 7d535a
#include "jpeglib.h"
kusano 7d535a
#include "jdct.h"		/* Private declarations for DCT subsystem */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * The decompressor input side (jdinput.c) saves away the appropriate
kusano 7d535a
 * quantization table for each component at the start of the first scan
kusano 7d535a
 * involving that component.  (This is necessary in order to correctly
kusano 7d535a
 * decode files that reuse Q-table slots.)
kusano 7d535a
 * When we are ready to make an output pass, the saved Q-table is converted
kusano 7d535a
 * to a multiplier table that will actually be used by the IDCT routine.
kusano 7d535a
 * The multiplier table contents are IDCT-method-dependent.  To support
kusano 7d535a
 * application changes in IDCT method between scans, we can remake the
kusano 7d535a
 * multiplier tables if necessary.
kusano 7d535a
 * In buffered-image mode, the first output pass may occur before any data
kusano 7d535a
 * has been seen for some components, and thus before their Q-tables have
kusano 7d535a
 * been saved away.  To handle this case, multiplier tables are preset
kusano 7d535a
 * to zeroes; the result of the IDCT will be a neutral gray level.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Private subobject for this module */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  struct jpeg_inverse_dct pub;	/* public fields */
kusano 7d535a
kusano 7d535a
  /* This array contains the IDCT method code that each multiplier table
kusano 7d535a
   * is currently set up for, or -1 if it's not yet set up.
kusano 7d535a
   * The actual multiplier tables are pointed to by dct_table in the
kusano 7d535a
   * per-component comp_info structures.
kusano 7d535a
   */
kusano 7d535a
  int cur_method[MAX_COMPONENTS];
kusano 7d535a
} my_idct_controller;
kusano 7d535a
kusano 7d535a
typedef my_idct_controller * my_idct_ptr;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Allocated multiplier tables: big enough for any supported variant */
kusano 7d535a
kusano 7d535a
typedef union {
kusano 7d535a
  ISLOW_MULT_TYPE islow_array[DCTSIZE2];
kusano 7d535a
#ifdef DCT_IFAST_SUPPORTED
kusano 7d535a
  IFAST_MULT_TYPE ifast_array[DCTSIZE2];
kusano 7d535a
#endif
kusano 7d535a
#ifdef DCT_FLOAT_SUPPORTED
kusano 7d535a
  FLOAT_MULT_TYPE float_array[DCTSIZE2];
kusano 7d535a
#endif
kusano 7d535a
} multiplier_table;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* The current scaled-IDCT routines require ISLOW-style multiplier tables,
kusano 7d535a
 * so be sure to compile that code if either ISLOW or SCALING is requested.
kusano 7d535a
 */
kusano 7d535a
#ifdef DCT_ISLOW_SUPPORTED
kusano 7d535a
#define PROVIDE_ISLOW_TABLES
kusano 7d535a
#else
kusano 7d535a
#ifdef IDCT_SCALING_SUPPORTED
kusano 7d535a
#define PROVIDE_ISLOW_TABLES
kusano 7d535a
#endif
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Prepare for an output pass.
kusano 7d535a
 * Here we select the proper IDCT routine for each component and build
kusano 7d535a
 * a matching multiplier table.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
start_pass (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
kusano 7d535a
  int ci, i;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
  int method = 0;
kusano 7d535a
  inverse_DCT_method_ptr method_ptr = NULL;
kusano 7d535a
  JQUANT_TBL * qtbl;
kusano 7d535a
kusano 7d535a
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
       ci++, compptr++) {
kusano 7d535a
    /* Select the proper IDCT routine for this component's scaling */
kusano 7d535a
    switch ((compptr->DCT_h_scaled_size << 8) + compptr->DCT_v_scaled_size) {
kusano 7d535a
#ifdef IDCT_SCALING_SUPPORTED
kusano 7d535a
    case ((1 << 8) + 1):
kusano 7d535a
      method_ptr = jpeg_idct_1x1;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((2 << 8) + 2):
kusano 7d535a
      method_ptr = jpeg_idct_2x2;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((3 << 8) + 3):
kusano 7d535a
      method_ptr = jpeg_idct_3x3;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((4 << 8) + 4):
kusano 7d535a
      method_ptr = jpeg_idct_4x4;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((5 << 8) + 5):
kusano 7d535a
      method_ptr = jpeg_idct_5x5;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((6 << 8) + 6):
kusano 7d535a
      method_ptr = jpeg_idct_6x6;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((7 << 8) + 7):
kusano 7d535a
      method_ptr = jpeg_idct_7x7;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((9 << 8) + 9):
kusano 7d535a
      method_ptr = jpeg_idct_9x9;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((10 << 8) + 10):
kusano 7d535a
      method_ptr = jpeg_idct_10x10;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((11 << 8) + 11):
kusano 7d535a
      method_ptr = jpeg_idct_11x11;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((12 << 8) + 12):
kusano 7d535a
      method_ptr = jpeg_idct_12x12;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((13 << 8) + 13):
kusano 7d535a
      method_ptr = jpeg_idct_13x13;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((14 << 8) + 14):
kusano 7d535a
      method_ptr = jpeg_idct_14x14;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((15 << 8) + 15):
kusano 7d535a
      method_ptr = jpeg_idct_15x15;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((16 << 8) + 16):
kusano 7d535a
      method_ptr = jpeg_idct_16x16;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((16 << 8) + 8):
kusano 7d535a
      method_ptr = jpeg_idct_16x8;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((14 << 8) + 7):
kusano 7d535a
      method_ptr = jpeg_idct_14x7;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((12 << 8) + 6):
kusano 7d535a
      method_ptr = jpeg_idct_12x6;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((10 << 8) + 5):
kusano 7d535a
      method_ptr = jpeg_idct_10x5;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((8 << 8) + 4):
kusano 7d535a
      method_ptr = jpeg_idct_8x4;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((6 << 8) + 3):
kusano 7d535a
      method_ptr = jpeg_idct_6x3;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((4 << 8) + 2):
kusano 7d535a
      method_ptr = jpeg_idct_4x2;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((2 << 8) + 1):
kusano 7d535a
      method_ptr = jpeg_idct_2x1;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((8 << 8) + 16):
kusano 7d535a
      method_ptr = jpeg_idct_8x16;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((7 << 8) + 14):
kusano 7d535a
      method_ptr = jpeg_idct_7x14;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((6 << 8) + 12):
kusano 7d535a
      method_ptr = jpeg_idct_6x12;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((5 << 8) + 10):
kusano 7d535a
      method_ptr = jpeg_idct_5x10;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((4 << 8) + 8):
kusano 7d535a
      method_ptr = jpeg_idct_4x8;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((3 << 8) + 6):
kusano 7d535a
      method_ptr = jpeg_idct_3x6;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((2 << 8) + 4):
kusano 7d535a
      method_ptr = jpeg_idct_2x4;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
    case ((1 << 8) + 2):
kusano 7d535a
      method_ptr = jpeg_idct_1x2;
kusano 7d535a
      method = JDCT_ISLOW;	/* jidctint uses islow-style table */
kusano 7d535a
      break;
kusano 7d535a
#endif
kusano 7d535a
    case ((DCTSIZE << 8) + DCTSIZE):
kusano 7d535a
      switch (cinfo->dct_method) {
kusano 7d535a
#ifdef DCT_ISLOW_SUPPORTED
kusano 7d535a
      case JDCT_ISLOW:
kusano 7d535a
	method_ptr = jpeg_idct_islow;
kusano 7d535a
	method = JDCT_ISLOW;
kusano 7d535a
	break;
kusano 7d535a
#endif
kusano 7d535a
#ifdef DCT_IFAST_SUPPORTED
kusano 7d535a
      case JDCT_IFAST:
kusano 7d535a
	method_ptr = jpeg_idct_ifast;
kusano 7d535a
	method = JDCT_IFAST;
kusano 7d535a
	break;
kusano 7d535a
#endif
kusano 7d535a
#ifdef DCT_FLOAT_SUPPORTED
kusano 7d535a
      case JDCT_FLOAT:
kusano 7d535a
	method_ptr = jpeg_idct_float;
kusano 7d535a
	method = JDCT_FLOAT;
kusano 7d535a
	break;
kusano 7d535a
#endif
kusano 7d535a
      default:
kusano 7d535a
	ERREXIT(cinfo, JERR_NOT_COMPILED);
kusano 7d535a
	break;
kusano 7d535a
      }
kusano 7d535a
      break;
kusano 7d535a
    default:
kusano 7d535a
      ERREXIT2(cinfo, JERR_BAD_DCTSIZE,
kusano 7d535a
	       compptr->DCT_h_scaled_size, compptr->DCT_v_scaled_size);
kusano 7d535a
      break;
kusano 7d535a
    }
kusano 7d535a
    idct->pub.inverse_DCT[ci] = method_ptr;
kusano 7d535a
    /* Create multiplier table from quant table.
kusano 7d535a
     * However, we can skip this if the component is uninteresting
kusano 7d535a
     * or if we already built the table.  Also, if no quant table
kusano 7d535a
     * has yet been saved for the component, we leave the
kusano 7d535a
     * multiplier table all-zero; we'll be reading zeroes from the
kusano 7d535a
     * coefficient controller's buffer anyway.
kusano 7d535a
     */
kusano 7d535a
    if (! compptr->component_needed || idct->cur_method[ci] == method)
kusano 7d535a
      continue;
kusano 7d535a
    qtbl = compptr->quant_table;
kusano 7d535a
    if (qtbl == NULL)		/* happens if no data yet for component */
kusano 7d535a
      continue;
kusano 7d535a
    idct->cur_method[ci] = method;
kusano 7d535a
    switch (method) {
kusano 7d535a
#ifdef PROVIDE_ISLOW_TABLES
kusano 7d535a
    case JDCT_ISLOW:
kusano 7d535a
      {
kusano 7d535a
	/* For LL&M IDCT method, multipliers are equal to raw quantization
kusano 7d535a
	 * coefficients, but are stored as ints to ensure access efficiency.
kusano 7d535a
	 */
kusano 7d535a
	ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
kusano 7d535a
	for (i = 0; i < DCTSIZE2; i++) {
kusano 7d535a
	  ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
      break;
kusano 7d535a
#endif
kusano 7d535a
#ifdef DCT_IFAST_SUPPORTED
kusano 7d535a
    case JDCT_IFAST:
kusano 7d535a
      {
kusano 7d535a
	/* For AA&N IDCT method, multipliers are equal to quantization
kusano 7d535a
	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
kusano 7d535a
	 *   scalefactor[0] = 1
kusano 7d535a
	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
kusano 7d535a
	 * For integer operation, the multiplier table is to be scaled by
kusano 7d535a
	 * IFAST_SCALE_BITS.
kusano 7d535a
	 */
kusano 7d535a
	IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
kusano 7d535a
#define CONST_BITS 14
kusano 7d535a
	static const INT16 aanscales[DCTSIZE2] = {
kusano 7d535a
	  /* precomputed values scaled up by 14 bits */
kusano 7d535a
	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
kusano 7d535a
	  22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
kusano 7d535a
	  21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
kusano 7d535a
	  19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
kusano 7d535a
	  16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
kusano 7d535a
	  12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
kusano 7d535a
	   8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
kusano 7d535a
	   4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
kusano 7d535a
	};
kusano 7d535a
	SHIFT_TEMPS
kusano 7d535a
kusano 7d535a
	for (i = 0; i < DCTSIZE2; i++) {
kusano 7d535a
	  ifmtbl[i] = (IFAST_MULT_TYPE)
kusano 7d535a
	    DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
kusano 7d535a
				  (INT32) aanscales[i]),
kusano 7d535a
		    CONST_BITS-IFAST_SCALE_BITS);
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
      break;
kusano 7d535a
#endif
kusano 7d535a
#ifdef DCT_FLOAT_SUPPORTED
kusano 7d535a
    case JDCT_FLOAT:
kusano 7d535a
      {
kusano 7d535a
	/* For float AA&N IDCT method, multipliers are equal to quantization
kusano 7d535a
	 * coefficients scaled by scalefactor[row]*scalefactor[col], where
kusano 7d535a
	 *   scalefactor[0] = 1
kusano 7d535a
	 *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
kusano 7d535a
	 * We apply a further scale factor of 1/8.
kusano 7d535a
	 */
kusano 7d535a
	FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
kusano 7d535a
	int row, col;
kusano 7d535a
	static const double aanscalefactor[DCTSIZE] = {
kusano 7d535a
	  1.0, 1.387039845, 1.306562965, 1.175875602,
kusano 7d535a
	  1.0, 0.785694958, 0.541196100, 0.275899379
kusano 7d535a
	};
kusano 7d535a
kusano 7d535a
	i = 0;
kusano 7d535a
	for (row = 0; row < DCTSIZE; row++) {
kusano 7d535a
	  for (col = 0; col < DCTSIZE; col++) {
kusano 7d535a
	    fmtbl[i] = (FLOAT_MULT_TYPE)
kusano 7d535a
	      ((double) qtbl->quantval[i] *
kusano 7d535a
	       aanscalefactor[row] * aanscalefactor[col] * 0.125);
kusano 7d535a
	    i++;
kusano 7d535a
	  }
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
      break;
kusano 7d535a
#endif
kusano 7d535a
    default:
kusano 7d535a
      ERREXIT(cinfo, JERR_NOT_COMPILED);
kusano 7d535a
      break;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize IDCT manager.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jinit_inverse_dct (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_idct_ptr idct;
kusano 7d535a
  int ci;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
kusano 7d535a
  idct = (my_idct_ptr)
kusano 7d535a
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				SIZEOF(my_idct_controller));
kusano 7d535a
  cinfo->idct = (struct jpeg_inverse_dct *) idct;
kusano 7d535a
  idct->pub.start_pass = start_pass;
kusano 7d535a
kusano 7d535a
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
       ci++, compptr++) {
kusano 7d535a
    /* Allocate and pre-zero a multiplier table for each component */
kusano 7d535a
    compptr->dct_table =
kusano 7d535a
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				  SIZEOF(multiplier_table));
kusano 7d535a
    MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
kusano 7d535a
    /* Mark multiplier table not yet set up for any method */
kusano 7d535a
    idct->cur_method[ci] = -1;
kusano 7d535a
  }
kusano 7d535a
}