136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * jddctmgr.c
336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane *
4a73e870ad02de20c2b34cb3a5382c2846c2afbe3DRC * This file was part of the Independent JPEG Group's software:
5489583f5165e05d37302e8eeec58104ea0109127Thomas G. Lane * Copyright (C) 1994-1996, Thomas G. Lane.
6f18f81b7e20cf993786a3348960ab2428762deeeGuido Vollbeding * Modified 2002-2010 by Guido Vollbeding.
7a6ef282a49f2d7d1b4d19cc89f63e81fd66b35b7DRC * libjpeg-turbo Modifications:
859a3938b2e3f6c49730a57935f389078219a8fabPierre Ossman * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
936a6eec93250e390d1028b3372078810b4428eafDRC * Copyright (C) 2010, D. R. Commander.
105ef463056ae22f24c3915ba7ab03eefd5bb6fde7DRC * Copyright (C) 2013, MIPS Technologies, Inc., California
1136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * For conditions of distribution and use, see the accompanying README file.
1236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane *
1336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * This file contains the inverse-DCT management logic.
1436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * This code selects a particular IDCT implementation to be used,
1536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * and it performs related housekeeping chores.  No code in this file
16bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * is executed per IDCT step, only during output pass setup.
1736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane *
1836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Note that the IDCT routines are responsible for performing coefficient
1936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * dequantization as well as the IDCT proper.  This module sets up the
2036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * dequantization multiplier table needed by the IDCT routine.
2136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
2236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
2336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#define JPEG_INTERNALS
2436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#include "jinclude.h"
2536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#include "jpeglib.h"
26e5eaf37440b8e337ab150c017df7c03faf846c51DRC#include "jdct.h"               /* Private declarations for DCT subsystem */
2759a3938b2e3f6c49730a57935f389078219a8fabPierre Ossman#include "jsimddct.h"
2836a6eec93250e390d1028b3372078810b4428eafDRC#include "jpegcomp.h"
2936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
3036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
31bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/*
32bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * The decompressor input side (jdinput.c) saves away the appropriate
33bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * quantization table for each component at the start of the first scan
34bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * involving that component.  (This is necessary in order to correctly
35bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * decode files that reuse Q-table slots.)
36bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * When we are ready to make an output pass, the saved Q-table is converted
37bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * to a multiplier table that will actually be used by the IDCT routine.
38bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * The multiplier table contents are IDCT-method-dependent.  To support
39bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * application changes in IDCT method between scans, we can remake the
40bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * multiplier tables if necessary.
41bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * In buffered-image mode, the first output pass may occur before any data
42bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * has been seen for some components, and thus before their Q-tables have
43bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * been saved away.  To handle this case, multiplier tables are preset
44bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * to zeroes; the result of the IDCT will be a neutral gray level.
45bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */
46bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
47bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
4836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* Private subobject for this module */
4936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
5036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanetypedef struct {
51e5eaf37440b8e337ab150c017df7c03faf846c51DRC  struct jpeg_inverse_dct pub;  /* public fields */
5236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
53bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* This array contains the IDCT method code that each multiplier table
54bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane   * is currently set up for, or -1 if it's not yet set up.
55bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane   * The actual multiplier tables are pointed to by dct_table in the
56bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane   * per-component comp_info structures.
57bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane   */
58bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  int cur_method[MAX_COMPONENTS];
5936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane} my_idct_controller;
6036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
6136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanetypedef my_idct_controller * my_idct_ptr;
6236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
6336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
64bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/* Allocated multiplier tables: big enough for any supported variant */
65bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
66bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanetypedef union {
67bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  ISLOW_MULT_TYPE islow_array[DCTSIZE2];
68bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef DCT_IFAST_SUPPORTED
69bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  IFAST_MULT_TYPE ifast_array[DCTSIZE2];
70bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#endif
71bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef DCT_FLOAT_SUPPORTED
72bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  FLOAT_MULT_TYPE float_array[DCTSIZE2];
73bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#endif
74bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane} multiplier_table;
7536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
7636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
7736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/* The current scaled-IDCT routines require ISLOW-style multiplier tables,
7836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * so be sure to compile that code if either ISLOW or SCALING is requested.
7936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
8036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#ifdef DCT_ISLOW_SUPPORTED
8136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#define PROVIDE_ISLOW_TABLES
8236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#else
8336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#ifdef IDCT_SCALING_SUPPORTED
8436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#define PROVIDE_ISLOW_TABLES
8536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#endif
8636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#endif
8736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
8836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
8936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
90bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Prepare for an output pass.
91bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Here we select the proper IDCT routine for each component and build
92bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * a matching multiplier table.
9336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
9436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
95489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneMETHODDEF(void)
96bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanestart_pass (j_decompress_ptr cinfo)
9736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
9836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
99bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  int ci, i;
10036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  jpeg_component_info *compptr;
101bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  int method = 0;
102bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  inverse_DCT_method_ptr method_ptr = NULL;
10336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  JQUANT_TBL * qtbl;
10436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
105bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
106bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane       ci++, compptr++) {
107bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Select the proper IDCT routine for this component's scaling */
10849967cdb30edd0479a1719eedc1dace5ba078d3fDRC    switch (compptr->_DCT_scaled_size) {
109bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef IDCT_SCALING_SUPPORTED
110bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    case 1:
111bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      method_ptr = jpeg_idct_1x1;
112e5eaf37440b8e337ab150c017df7c03faf846c51DRC      method = JDCT_ISLOW;      /* jidctred uses islow-style table */
113bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      break;
114bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    case 2:
11559a3938b2e3f6c49730a57935f389078219a8fabPierre Ossman      if (jsimd_can_idct_2x2())
11659a3938b2e3f6c49730a57935f389078219a8fabPierre Ossman        method_ptr = jsimd_idct_2x2;
11759a3938b2e3f6c49730a57935f389078219a8fabPierre Ossman      else
11859a3938b2e3f6c49730a57935f389078219a8fabPierre Ossman        method_ptr = jpeg_idct_2x2;
119e5eaf37440b8e337ab150c017df7c03faf846c51DRC      method = JDCT_ISLOW;      /* jidctred uses islow-style table */
120bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      break;
12127fb3fc5895baccff74d7286785d97039682e45cDRC    case 3:
1225996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      method_ptr = jpeg_idct_3x3;
123e5eaf37440b8e337ab150c017df7c03faf846c51DRC      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
1245996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      break;
125bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    case 4:
12659a3938b2e3f6c49730a57935f389078219a8fabPierre Ossman      if (jsimd_can_idct_4x4())
12759a3938b2e3f6c49730a57935f389078219a8fabPierre Ossman        method_ptr = jsimd_idct_4x4;
12859a3938b2e3f6c49730a57935f389078219a8fabPierre Ossman      else
12959a3938b2e3f6c49730a57935f389078219a8fabPierre Ossman        method_ptr = jpeg_idct_4x4;
130e5eaf37440b8e337ab150c017df7c03faf846c51DRC      method = JDCT_ISLOW;      /* jidctred uses islow-style table */
131bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      break;
13227fb3fc5895baccff74d7286785d97039682e45cDRC    case 5:
1335996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      method_ptr = jpeg_idct_5x5;
134e5eaf37440b8e337ab150c017df7c03faf846c51DRC      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
1355996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      break;
13627fb3fc5895baccff74d7286785d97039682e45cDRC    case 6:
137e500591710cd1ee2a8dabb0e291a31448ace7077DRC#if defined(__mips__)
138e500591710cd1ee2a8dabb0e291a31448ace7077DRC      if (jsimd_can_idct_6x6())
139e500591710cd1ee2a8dabb0e291a31448ace7077DRC        method_ptr = jsimd_idct_6x6;
140e500591710cd1ee2a8dabb0e291a31448ace7077DRC      else
141e500591710cd1ee2a8dabb0e291a31448ace7077DRC#endif
1425996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      method_ptr = jpeg_idct_6x6;
143e5eaf37440b8e337ab150c017df7c03faf846c51DRC      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
1445996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      break;
14527fb3fc5895baccff74d7286785d97039682e45cDRC    case 7:
1465996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      method_ptr = jpeg_idct_7x7;
147e5eaf37440b8e337ab150c017df7c03faf846c51DRC      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
1485996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      break;
149bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#endif
150bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    case DCTSIZE:
151bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      switch (cinfo->dct_method) {
152bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef DCT_ISLOW_SUPPORTED
153bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      case JDCT_ISLOW:
154e5eaf37440b8e337ab150c017df7c03faf846c51DRC        if (jsimd_can_idct_islow())
155e5eaf37440b8e337ab150c017df7c03faf846c51DRC          method_ptr = jsimd_idct_islow;
156e5eaf37440b8e337ab150c017df7c03faf846c51DRC        else
157e5eaf37440b8e337ab150c017df7c03faf846c51DRC          method_ptr = jpeg_idct_islow;
158e5eaf37440b8e337ab150c017df7c03faf846c51DRC        method = JDCT_ISLOW;
159e5eaf37440b8e337ab150c017df7c03faf846c51DRC        break;
160bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#endif
161bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef DCT_IFAST_SUPPORTED
162bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      case JDCT_IFAST:
163e5eaf37440b8e337ab150c017df7c03faf846c51DRC        if (jsimd_can_idct_ifast())
164e5eaf37440b8e337ab150c017df7c03faf846c51DRC          method_ptr = jsimd_idct_ifast;
165e5eaf37440b8e337ab150c017df7c03faf846c51DRC        else
166e5eaf37440b8e337ab150c017df7c03faf846c51DRC          method_ptr = jpeg_idct_ifast;
167e5eaf37440b8e337ab150c017df7c03faf846c51DRC        method = JDCT_IFAST;
168e5eaf37440b8e337ab150c017df7c03faf846c51DRC        break;
169bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#endif
170bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef DCT_FLOAT_SUPPORTED
171bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      case JDCT_FLOAT:
172e5eaf37440b8e337ab150c017df7c03faf846c51DRC        if (jsimd_can_idct_float())
173e5eaf37440b8e337ab150c017df7c03faf846c51DRC          method_ptr = jsimd_idct_float;
174e5eaf37440b8e337ab150c017df7c03faf846c51DRC        else
175e5eaf37440b8e337ab150c017df7c03faf846c51DRC          method_ptr = jpeg_idct_float;
176e5eaf37440b8e337ab150c017df7c03faf846c51DRC        method = JDCT_FLOAT;
177e5eaf37440b8e337ab150c017df7c03faf846c51DRC        break;
178bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#endif
179bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      default:
180e5eaf37440b8e337ab150c017df7c03faf846c51DRC        ERREXIT(cinfo, JERR_NOT_COMPILED);
181e5eaf37440b8e337ab150c017df7c03faf846c51DRC        break;
182bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      }
183bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      break;
184a3deac158b24fb146ef551941e5f83ce646472aeDRC#ifdef IDCT_SCALING_SUPPORTED
18527fb3fc5895baccff74d7286785d97039682e45cDRC    case 9:
1865996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      method_ptr = jpeg_idct_9x9;
187e5eaf37440b8e337ab150c017df7c03faf846c51DRC      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
1885996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      break;
18927fb3fc5895baccff74d7286785d97039682e45cDRC    case 10:
1905996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      method_ptr = jpeg_idct_10x10;
191e5eaf37440b8e337ab150c017df7c03faf846c51DRC      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
1925996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      break;
19327fb3fc5895baccff74d7286785d97039682e45cDRC    case 11:
1945996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      method_ptr = jpeg_idct_11x11;
195e5eaf37440b8e337ab150c017df7c03faf846c51DRC      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
1965996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      break;
19727fb3fc5895baccff74d7286785d97039682e45cDRC    case 12:
198e500591710cd1ee2a8dabb0e291a31448ace7077DRC#if defined(__mips__)
199e500591710cd1ee2a8dabb0e291a31448ace7077DRC      if (jsimd_can_idct_12x12())
200e500591710cd1ee2a8dabb0e291a31448ace7077DRC        method_ptr = jsimd_idct_12x12;
201e500591710cd1ee2a8dabb0e291a31448ace7077DRC      else
202e500591710cd1ee2a8dabb0e291a31448ace7077DRC#endif
2035996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      method_ptr = jpeg_idct_12x12;
204e5eaf37440b8e337ab150c017df7c03faf846c51DRC      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
2055996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      break;
20627fb3fc5895baccff74d7286785d97039682e45cDRC    case 13:
2075996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      method_ptr = jpeg_idct_13x13;
208e5eaf37440b8e337ab150c017df7c03faf846c51DRC      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
2095996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      break;
21027fb3fc5895baccff74d7286785d97039682e45cDRC    case 14:
2115996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      method_ptr = jpeg_idct_14x14;
212e5eaf37440b8e337ab150c017df7c03faf846c51DRC      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
2135996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      break;
21427fb3fc5895baccff74d7286785d97039682e45cDRC    case 15:
2155996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      method_ptr = jpeg_idct_15x15;
216e5eaf37440b8e337ab150c017df7c03faf846c51DRC      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
2175996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      break;
21827fb3fc5895baccff74d7286785d97039682e45cDRC    case 16:
2195996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      method_ptr = jpeg_idct_16x16;
220e5eaf37440b8e337ab150c017df7c03faf846c51DRC      method = JDCT_ISLOW;      /* jidctint uses islow-style table */
2215996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding      break;
222a3deac158b24fb146ef551941e5f83ce646472aeDRC#endif
223bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    default:
22449967cdb30edd0479a1719eedc1dace5ba078d3fDRC      ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->_DCT_scaled_size);
225bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      break;
226bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    }
227bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    idct->pub.inverse_DCT[ci] = method_ptr;
228bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Create multiplier table from quant table.
229bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * However, we can skip this if the component is uninteresting
230bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * or if we already built the table.  Also, if no quant table
231bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * has yet been saved for the component, we leave the
232bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * multiplier table all-zero; we'll be reading zeroes from the
233bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * coefficient controller's buffer anyway.
234bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     */
235bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if (! compptr->component_needed || idct->cur_method[ci] == method)
236bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      continue;
237bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    qtbl = compptr->quant_table;
238e5eaf37440b8e337ab150c017df7c03faf846c51DRC    if (qtbl == NULL)           /* happens if no data yet for component */
23936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      continue;
240bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    idct->cur_method[ci] = method;
241bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    switch (method) {
24236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#ifdef PROVIDE_ISLOW_TABLES
24336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    case JDCT_ISLOW:
24436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      {
245e5eaf37440b8e337ab150c017df7c03faf846c51DRC        /* For LL&M IDCT method, multipliers are equal to raw quantization
246e5eaf37440b8e337ab150c017df7c03faf846c51DRC         * coefficients, but are stored as ints to ensure access efficiency.
247e5eaf37440b8e337ab150c017df7c03faf846c51DRC         */
248e5eaf37440b8e337ab150c017df7c03faf846c51DRC        ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
249e5eaf37440b8e337ab150c017df7c03faf846c51DRC        for (i = 0; i < DCTSIZE2; i++) {
250e5eaf37440b8e337ab150c017df7c03faf846c51DRC          ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
251e5eaf37440b8e337ab150c017df7c03faf846c51DRC        }
25236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      }
25336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      break;
25436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#endif
25536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#ifdef DCT_IFAST_SUPPORTED
25636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    case JDCT_IFAST:
25736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      {
258e5eaf37440b8e337ab150c017df7c03faf846c51DRC        /* For AA&N IDCT method, multipliers are equal to quantization
259e5eaf37440b8e337ab150c017df7c03faf846c51DRC         * coefficients scaled by scalefactor[row]*scalefactor[col], where
260e5eaf37440b8e337ab150c017df7c03faf846c51DRC         *   scalefactor[0] = 1
261e5eaf37440b8e337ab150c017df7c03faf846c51DRC         *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
262e5eaf37440b8e337ab150c017df7c03faf846c51DRC         * For integer operation, the multiplier table is to be scaled by
263e5eaf37440b8e337ab150c017df7c03faf846c51DRC         * IFAST_SCALE_BITS.
264e5eaf37440b8e337ab150c017df7c03faf846c51DRC         */
265e5eaf37440b8e337ab150c017df7c03faf846c51DRC        IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
26636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#define CONST_BITS 14
267e5eaf37440b8e337ab150c017df7c03faf846c51DRC        static const INT16 aanscales[DCTSIZE2] = {
268e5eaf37440b8e337ab150c017df7c03faf846c51DRC          /* precomputed values scaled up by 14 bits */
269e5eaf37440b8e337ab150c017df7c03faf846c51DRC          16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
270e5eaf37440b8e337ab150c017df7c03faf846c51DRC          22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
271e5eaf37440b8e337ab150c017df7c03faf846c51DRC          21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
272e5eaf37440b8e337ab150c017df7c03faf846c51DRC          19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
273e5eaf37440b8e337ab150c017df7c03faf846c51DRC          16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
274e5eaf37440b8e337ab150c017df7c03faf846c51DRC          12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
275e5eaf37440b8e337ab150c017df7c03faf846c51DRC           8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
276e5eaf37440b8e337ab150c017df7c03faf846c51DRC           4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
277e5eaf37440b8e337ab150c017df7c03faf846c51DRC        };
278e5eaf37440b8e337ab150c017df7c03faf846c51DRC        SHIFT_TEMPS
27936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
280e5eaf37440b8e337ab150c017df7c03faf846c51DRC        for (i = 0; i < DCTSIZE2; i++) {
281e5eaf37440b8e337ab150c017df7c03faf846c51DRC          ifmtbl[i] = (IFAST_MULT_TYPE)
282e5eaf37440b8e337ab150c017df7c03faf846c51DRC            DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
283e5eaf37440b8e337ab150c017df7c03faf846c51DRC                                  (INT32) aanscales[i]),
284e5eaf37440b8e337ab150c017df7c03faf846c51DRC                    CONST_BITS-IFAST_SCALE_BITS);
285e5eaf37440b8e337ab150c017df7c03faf846c51DRC        }
28636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      }
28736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      break;
28836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#endif
28936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#ifdef DCT_FLOAT_SUPPORTED
29036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    case JDCT_FLOAT:
29136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      {
292e5eaf37440b8e337ab150c017df7c03faf846c51DRC        /* For float AA&N IDCT method, multipliers are equal to quantization
293e5eaf37440b8e337ab150c017df7c03faf846c51DRC         * coefficients scaled by scalefactor[row]*scalefactor[col], where
294e5eaf37440b8e337ab150c017df7c03faf846c51DRC         *   scalefactor[0] = 1
295e5eaf37440b8e337ab150c017df7c03faf846c51DRC         *   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
296e5eaf37440b8e337ab150c017df7c03faf846c51DRC         */
297e5eaf37440b8e337ab150c017df7c03faf846c51DRC        FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
298e5eaf37440b8e337ab150c017df7c03faf846c51DRC        int row, col;
299e5eaf37440b8e337ab150c017df7c03faf846c51DRC        static const double aanscalefactor[DCTSIZE] = {
300e5eaf37440b8e337ab150c017df7c03faf846c51DRC          1.0, 1.387039845, 1.306562965, 1.175875602,
301e5eaf37440b8e337ab150c017df7c03faf846c51DRC          1.0, 0.785694958, 0.541196100, 0.275899379
302e5eaf37440b8e337ab150c017df7c03faf846c51DRC        };
30336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
304e5eaf37440b8e337ab150c017df7c03faf846c51DRC        i = 0;
305e5eaf37440b8e337ab150c017df7c03faf846c51DRC        for (row = 0; row < DCTSIZE; row++) {
306e5eaf37440b8e337ab150c017df7c03faf846c51DRC          for (col = 0; col < DCTSIZE; col++) {
307e5eaf37440b8e337ab150c017df7c03faf846c51DRC            fmtbl[i] = (FLOAT_MULT_TYPE)
308e5eaf37440b8e337ab150c017df7c03faf846c51DRC              ((double) qtbl->quantval[i] *
309e5eaf37440b8e337ab150c017df7c03faf846c51DRC               aanscalefactor[row] * aanscalefactor[col]);
310e5eaf37440b8e337ab150c017df7c03faf846c51DRC            i++;
311e5eaf37440b8e337ab150c017df7c03faf846c51DRC          }
312e5eaf37440b8e337ab150c017df7c03faf846c51DRC        }
31336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      }
31436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      break;
31536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane#endif
31636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    default:
31736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      ERREXIT(cinfo, JERR_NOT_COMPILED);
31836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane      break;
31936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    }
32036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
32136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
32236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
32336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
32436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane/*
32536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane * Initialize IDCT manager.
32636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane */
32736a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
328489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
32936a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lanejinit_inverse_dct (j_decompress_ptr cinfo)
33036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane{
33136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  my_idct_ptr idct;
33236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  int ci;
33336a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  jpeg_component_info *compptr;
33436a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
33536a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  idct = (my_idct_ptr)
33636a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
3375de454b291f48382648a5d1dc2aa0fca8b5786d4DRC                                sizeof(my_idct_controller));
33836a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  cinfo->idct = (struct jpeg_inverse_dct *) idct;
339bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  idct->pub.start_pass = start_pass;
34036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane
34136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
34236a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane       ci++, compptr++) {
343bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Allocate and pre-zero a multiplier table for each component */
344bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    compptr->dct_table =
345bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
3465de454b291f48382648a5d1dc2aa0fca8b5786d4DRC                                  sizeof(multiplier_table));
3475de454b291f48382648a5d1dc2aa0fca8b5786d4DRC    MEMZERO(compptr->dct_table, sizeof(multiplier_table));
348bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Mark multiplier table not yet set up for any method */
349bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    idct->cur_method[ci] = -1;
35036a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane  }
35136a4ccccd33f5cc9df62949554af87129ced7f84Thomas G. Lane}
352