blob: 044e46947749da9fda731605f5b686198a4ceac7 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jddctmgr.c
3 *
Thomas G. Lane489583f1996-02-07 00:00:00 +00004 * Copyright (C) 1994-1996, Thomas G. Lane.
Pierre Ossman59a39382009-03-09 13:15:56 +00005 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRC36a6eec2010-10-08 08:05:44 +00006 * Copyright (C) 2010, D. R. Commander.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00007 * This file is part of the Independent JPEG Group's software.
8 * For conditions of distribution and use, see the accompanying README file.
9 *
10 * This file contains the inverse-DCT management logic.
11 * This code selects a particular IDCT implementation to be used,
12 * and it performs related housekeeping chores. No code in this file
Thomas G. Lanebc79e061995-08-02 00:00:00 +000013 * is executed per IDCT step, only during output pass setup.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000014 *
15 * Note that the IDCT routines are responsible for performing coefficient
16 * dequantization as well as the IDCT proper. This module sets up the
17 * dequantization multiplier table needed by the IDCT routine.
18 */
19
20#define JPEG_INTERNALS
21#include "jinclude.h"
22#include "jpeglib.h"
23#include "jdct.h" /* Private declarations for DCT subsystem */
Pierre Ossman59a39382009-03-09 13:15:56 +000024#include "jsimddct.h"
DRC36a6eec2010-10-08 08:05:44 +000025#include "jpegcomp.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000026
27
Thomas G. Lanebc79e061995-08-02 00:00:00 +000028/*
29 * The decompressor input side (jdinput.c) saves away the appropriate
30 * quantization table for each component at the start of the first scan
31 * involving that component. (This is necessary in order to correctly
32 * decode files that reuse Q-table slots.)
33 * When we are ready to make an output pass, the saved Q-table is converted
34 * to a multiplier table that will actually be used by the IDCT routine.
35 * The multiplier table contents are IDCT-method-dependent. To support
36 * application changes in IDCT method between scans, we can remake the
37 * multiplier tables if necessary.
38 * In buffered-image mode, the first output pass may occur before any data
39 * has been seen for some components, and thus before their Q-tables have
40 * been saved away. To handle this case, multiplier tables are preset
41 * to zeroes; the result of the IDCT will be a neutral gray level.
42 */
43
44
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000045/* Private subobject for this module */
46
47typedef struct {
48 struct jpeg_inverse_dct pub; /* public fields */
49
Thomas G. Lanebc79e061995-08-02 00:00:00 +000050 /* This array contains the IDCT method code that each multiplier table
51 * is currently set up for, or -1 if it's not yet set up.
52 * The actual multiplier tables are pointed to by dct_table in the
53 * per-component comp_info structures.
54 */
55 int cur_method[MAX_COMPONENTS];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000056} my_idct_controller;
57
58typedef my_idct_controller * my_idct_ptr;
59
60
Thomas G. Lanebc79e061995-08-02 00:00:00 +000061/* Allocated multiplier tables: big enough for any supported variant */
62
63typedef union {
64 ISLOW_MULT_TYPE islow_array[DCTSIZE2];
65#ifdef DCT_IFAST_SUPPORTED
66 IFAST_MULT_TYPE ifast_array[DCTSIZE2];
67#endif
68#ifdef DCT_FLOAT_SUPPORTED
69 FLOAT_MULT_TYPE float_array[DCTSIZE2];
70#endif
71} multiplier_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000072
73
74/* The current scaled-IDCT routines require ISLOW-style multiplier tables,
75 * so be sure to compile that code if either ISLOW or SCALING is requested.
76 */
77#ifdef DCT_ISLOW_SUPPORTED
78#define PROVIDE_ISLOW_TABLES
79#else
80#ifdef IDCT_SCALING_SUPPORTED
81#define PROVIDE_ISLOW_TABLES
82#endif
83#endif
84
85
86/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +000087 * Prepare for an output pass.
88 * Here we select the proper IDCT routine for each component and build
89 * a matching multiplier table.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000090 */
91
Thomas G. Lane489583f1996-02-07 00:00:00 +000092METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000093start_pass (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000094{
95 my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
Thomas G. Lanebc79e061995-08-02 00:00:00 +000096 int ci, i;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000097 jpeg_component_info *compptr;
Thomas G. Lanebc79e061995-08-02 00:00:00 +000098 int method = 0;
99 inverse_DCT_method_ptr method_ptr = NULL;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000100 JQUANT_TBL * qtbl;
101
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000102 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
103 ci++, compptr++) {
104 /* Select the proper IDCT routine for this component's scaling */
DRC49967cd2010-10-09 19:57:51 +0000105 switch (compptr->_DCT_scaled_size) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000106#ifdef IDCT_SCALING_SUPPORTED
107 case 1:
108 method_ptr = jpeg_idct_1x1;
109 method = JDCT_ISLOW; /* jidctred uses islow-style table */
110 break;
111 case 2:
Pierre Ossman59a39382009-03-09 13:15:56 +0000112 if (jsimd_can_idct_2x2())
113 method_ptr = jsimd_idct_2x2;
114 else
115 method_ptr = jpeg_idct_2x2;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000116 method = JDCT_ISLOW; /* jidctred uses islow-style table */
117 break;
118 case 4:
Pierre Ossman59a39382009-03-09 13:15:56 +0000119 if (jsimd_can_idct_4x4())
120 method_ptr = jsimd_idct_4x4;
121 else
122 method_ptr = jpeg_idct_4x4;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000123 method = JDCT_ISLOW; /* jidctred uses islow-style table */
124 break;
125#endif
126 case DCTSIZE:
127 switch (cinfo->dct_method) {
128#ifdef DCT_ISLOW_SUPPORTED
129 case JDCT_ISLOW:
Pierre Ossman59a39382009-03-09 13:15:56 +0000130 if (jsimd_can_idct_islow())
131 method_ptr = jsimd_idct_islow;
132 else
133 method_ptr = jpeg_idct_islow;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000134 method = JDCT_ISLOW;
135 break;
136#endif
137#ifdef DCT_IFAST_SUPPORTED
138 case JDCT_IFAST:
Pierre Ossman59a39382009-03-09 13:15:56 +0000139 if (jsimd_can_idct_ifast())
140 method_ptr = jsimd_idct_ifast;
141 else
142 method_ptr = jpeg_idct_ifast;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000143 method = JDCT_IFAST;
144 break;
145#endif
146#ifdef DCT_FLOAT_SUPPORTED
147 case JDCT_FLOAT:
Pierre Ossman59a39382009-03-09 13:15:56 +0000148 if (jsimd_can_idct_float())
149 method_ptr = jsimd_idct_float;
150 else
151 method_ptr = jpeg_idct_float;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000152 method = JDCT_FLOAT;
153 break;
154#endif
155 default:
156 ERREXIT(cinfo, JERR_NOT_COMPILED);
157 break;
158 }
159 break;
160 default:
DRC49967cd2010-10-09 19:57:51 +0000161 ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->_DCT_scaled_size);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000162 break;
163 }
164 idct->pub.inverse_DCT[ci] = method_ptr;
165 /* Create multiplier table from quant table.
166 * However, we can skip this if the component is uninteresting
167 * or if we already built the table. Also, if no quant table
168 * has yet been saved for the component, we leave the
169 * multiplier table all-zero; we'll be reading zeroes from the
170 * coefficient controller's buffer anyway.
171 */
172 if (! compptr->component_needed || idct->cur_method[ci] == method)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000173 continue;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000174 qtbl = compptr->quant_table;
175 if (qtbl == NULL) /* happens if no data yet for component */
176 continue;
177 idct->cur_method[ci] = method;
178 switch (method) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000179#ifdef PROVIDE_ISLOW_TABLES
180 case JDCT_ISLOW:
181 {
182 /* For LL&M IDCT method, multipliers are equal to raw quantization
Thomas G. Lane489583f1996-02-07 00:00:00 +0000183 * coefficients, but are stored as ints to ensure access efficiency.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000184 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000185 ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000186 for (i = 0; i < DCTSIZE2; i++) {
Thomas G. Lane489583f1996-02-07 00:00:00 +0000187 ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000188 }
189 }
190 break;
191#endif
192#ifdef DCT_IFAST_SUPPORTED
193 case JDCT_IFAST:
194 {
195 /* For AA&N IDCT method, multipliers are equal to quantization
196 * coefficients scaled by scalefactor[row]*scalefactor[col], where
197 * scalefactor[0] = 1
198 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
199 * For integer operation, the multiplier table is to be scaled by
Thomas G. Lane489583f1996-02-07 00:00:00 +0000200 * IFAST_SCALE_BITS.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000201 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000202 IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000203#define CONST_BITS 14
204 static const INT16 aanscales[DCTSIZE2] = {
205 /* precomputed values scaled up by 14 bits */
206 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
207 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
208 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
209 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
210 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
211 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
212 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
213 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
214 };
215 SHIFT_TEMPS
216
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000217 for (i = 0; i < DCTSIZE2; i++) {
218 ifmtbl[i] = (IFAST_MULT_TYPE)
Thomas G. Lane489583f1996-02-07 00:00:00 +0000219 DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000220 (INT32) aanscales[i]),
221 CONST_BITS-IFAST_SCALE_BITS);
222 }
223 }
224 break;
225#endif
226#ifdef DCT_FLOAT_SUPPORTED
227 case JDCT_FLOAT:
228 {
229 /* For float AA&N IDCT method, multipliers are equal to quantization
230 * coefficients scaled by scalefactor[row]*scalefactor[col], where
231 * scalefactor[0] = 1
232 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000233 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000234 FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000235 int row, col;
236 static const double aanscalefactor[DCTSIZE] = {
237 1.0, 1.387039845, 1.306562965, 1.175875602,
238 1.0, 0.785694958, 0.541196100, 0.275899379
239 };
240
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000241 i = 0;
242 for (row = 0; row < DCTSIZE; row++) {
243 for (col = 0; col < DCTSIZE; col++) {
244 fmtbl[i] = (FLOAT_MULT_TYPE)
Thomas G. Lane489583f1996-02-07 00:00:00 +0000245 ((double) qtbl->quantval[i] *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000246 aanscalefactor[row] * aanscalefactor[col]);
247 i++;
248 }
249 }
250 }
251 break;
252#endif
253 default:
254 ERREXIT(cinfo, JERR_NOT_COMPILED);
255 break;
256 }
257 }
258}
259
260
261/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000262 * Initialize IDCT manager.
263 */
264
Thomas G. Lane489583f1996-02-07 00:00:00 +0000265GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000266jinit_inverse_dct (j_decompress_ptr cinfo)
267{
268 my_idct_ptr idct;
269 int ci;
270 jpeg_component_info *compptr;
271
272 idct = (my_idct_ptr)
273 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
274 SIZEOF(my_idct_controller));
275 cinfo->idct = (struct jpeg_inverse_dct *) idct;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000276 idct->pub.start_pass = start_pass;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000277
278 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
279 ci++, compptr++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000280 /* Allocate and pre-zero a multiplier table for each component */
281 compptr->dct_table =
282 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
283 SIZEOF(multiplier_table));
284 MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
285 /* Mark multiplier table not yet set up for any method */
286 idct->cur_method[ci] = -1;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000287 }
288}