blob: 1758bed7bdbcaf8a4a25d5526745a091510265a7 [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.
Guido Vollbedingf18f81b2010-02-28 00:00:00 +00005 * Modified 2002-2010 by Guido Vollbeding.
Pierre Ossman59a39382009-03-09 13:15:56 +00006 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRC36a6eec2010-10-08 08:05:44 +00007 * Copyright (C) 2010, D. R. Commander.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00008 * This file is part of the Independent JPEG Group's software.
9 * For conditions of distribution and use, see the accompanying README file.
10 *
11 * This file contains the inverse-DCT management logic.
12 * This code selects a particular IDCT implementation to be used,
13 * and it performs related housekeeping chores. No code in this file
Thomas G. Lanebc79e061995-08-02 00:00:00 +000014 * is executed per IDCT step, only during output pass setup.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000015 *
16 * Note that the IDCT routines are responsible for performing coefficient
17 * dequantization as well as the IDCT proper. This module sets up the
18 * dequantization multiplier table needed by the IDCT routine.
19 */
20
21#define JPEG_INTERNALS
22#include "jinclude.h"
23#include "jpeglib.h"
24#include "jdct.h" /* Private declarations for DCT subsystem */
Pierre Ossman59a39382009-03-09 13:15:56 +000025#include "jsimddct.h"
DRC36a6eec2010-10-08 08:05:44 +000026#include "jpegcomp.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000027
28
Thomas G. Lanebc79e061995-08-02 00:00:00 +000029/*
30 * The decompressor input side (jdinput.c) saves away the appropriate
31 * quantization table for each component at the start of the first scan
32 * involving that component. (This is necessary in order to correctly
33 * decode files that reuse Q-table slots.)
34 * When we are ready to make an output pass, the saved Q-table is converted
35 * to a multiplier table that will actually be used by the IDCT routine.
36 * The multiplier table contents are IDCT-method-dependent. To support
37 * application changes in IDCT method between scans, we can remake the
38 * multiplier tables if necessary.
39 * In buffered-image mode, the first output pass may occur before any data
40 * has been seen for some components, and thus before their Q-tables have
41 * been saved away. To handle this case, multiplier tables are preset
42 * to zeroes; the result of the IDCT will be a neutral gray level.
43 */
44
45
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000046/* Private subobject for this module */
47
48typedef struct {
49 struct jpeg_inverse_dct pub; /* public fields */
50
Thomas G. Lanebc79e061995-08-02 00:00:00 +000051 /* This array contains the IDCT method code that each multiplier table
52 * is currently set up for, or -1 if it's not yet set up.
53 * The actual multiplier tables are pointed to by dct_table in the
54 * per-component comp_info structures.
55 */
56 int cur_method[MAX_COMPONENTS];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000057} my_idct_controller;
58
59typedef my_idct_controller * my_idct_ptr;
60
61
Thomas G. Lanebc79e061995-08-02 00:00:00 +000062/* Allocated multiplier tables: big enough for any supported variant */
63
64typedef union {
65 ISLOW_MULT_TYPE islow_array[DCTSIZE2];
66#ifdef DCT_IFAST_SUPPORTED
67 IFAST_MULT_TYPE ifast_array[DCTSIZE2];
68#endif
69#ifdef DCT_FLOAT_SUPPORTED
70 FLOAT_MULT_TYPE float_array[DCTSIZE2];
71#endif
72} multiplier_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000073
74
75/* The current scaled-IDCT routines require ISLOW-style multiplier tables,
76 * so be sure to compile that code if either ISLOW or SCALING is requested.
77 */
78#ifdef DCT_ISLOW_SUPPORTED
79#define PROVIDE_ISLOW_TABLES
80#else
81#ifdef IDCT_SCALING_SUPPORTED
82#define PROVIDE_ISLOW_TABLES
83#endif
84#endif
85
86
87/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +000088 * Prepare for an output pass.
89 * Here we select the proper IDCT routine for each component and build
90 * a matching multiplier table.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000091 */
92
Thomas G. Lane489583f1996-02-07 00:00:00 +000093METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000094start_pass (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000095{
96 my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
Thomas G. Lanebc79e061995-08-02 00:00:00 +000097 int ci, i;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000098 jpeg_component_info *compptr;
Thomas G. Lanebc79e061995-08-02 00:00:00 +000099 int method = 0;
100 inverse_DCT_method_ptr method_ptr = NULL;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000101 JQUANT_TBL * qtbl;
102
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000103 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
104 ci++, compptr++) {
105 /* Select the proper IDCT routine for this component's scaling */
DRC49967cd2010-10-09 19:57:51 +0000106 switch (compptr->_DCT_scaled_size) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000107#ifdef IDCT_SCALING_SUPPORTED
108 case 1:
109 method_ptr = jpeg_idct_1x1;
110 method = JDCT_ISLOW; /* jidctred uses islow-style table */
111 break;
112 case 2:
Pierre Ossman59a39382009-03-09 13:15:56 +0000113 if (jsimd_can_idct_2x2())
114 method_ptr = jsimd_idct_2x2;
115 else
116 method_ptr = jpeg_idct_2x2;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000117 method = JDCT_ISLOW; /* jidctred uses islow-style table */
118 break;
DRC27fb3fc2012-01-28 01:48:07 +0000119 case 3:
Guido Vollbeding5996a252009-06-27 00:00:00 +0000120 method_ptr = jpeg_idct_3x3;
121 method = JDCT_ISLOW; /* jidctint uses islow-style table */
122 break;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000123 case 4:
Pierre Ossman59a39382009-03-09 13:15:56 +0000124 if (jsimd_can_idct_4x4())
125 method_ptr = jsimd_idct_4x4;
126 else
127 method_ptr = jpeg_idct_4x4;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000128 method = JDCT_ISLOW; /* jidctred uses islow-style table */
129 break;
DRC27fb3fc2012-01-28 01:48:07 +0000130 case 5:
Guido Vollbeding5996a252009-06-27 00:00:00 +0000131 method_ptr = jpeg_idct_5x5;
132 method = JDCT_ISLOW; /* jidctint uses islow-style table */
133 break;
DRC27fb3fc2012-01-28 01:48:07 +0000134 case 6:
Guido Vollbeding5996a252009-06-27 00:00:00 +0000135 method_ptr = jpeg_idct_6x6;
136 method = JDCT_ISLOW; /* jidctint uses islow-style table */
137 break;
DRC27fb3fc2012-01-28 01:48:07 +0000138 case 7:
Guido Vollbeding5996a252009-06-27 00:00:00 +0000139 method_ptr = jpeg_idct_7x7;
140 method = JDCT_ISLOW; /* jidctint uses islow-style table */
141 break;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000142#endif
143 case DCTSIZE:
144 switch (cinfo->dct_method) {
145#ifdef DCT_ISLOW_SUPPORTED
146 case JDCT_ISLOW:
Pierre Ossman59a39382009-03-09 13:15:56 +0000147 if (jsimd_can_idct_islow())
148 method_ptr = jsimd_idct_islow;
149 else
150 method_ptr = jpeg_idct_islow;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000151 method = JDCT_ISLOW;
152 break;
153#endif
154#ifdef DCT_IFAST_SUPPORTED
155 case JDCT_IFAST:
Pierre Ossman59a39382009-03-09 13:15:56 +0000156 if (jsimd_can_idct_ifast())
157 method_ptr = jsimd_idct_ifast;
158 else
159 method_ptr = jpeg_idct_ifast;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000160 method = JDCT_IFAST;
161 break;
162#endif
163#ifdef DCT_FLOAT_SUPPORTED
164 case JDCT_FLOAT:
Pierre Ossman59a39382009-03-09 13:15:56 +0000165 if (jsimd_can_idct_float())
166 method_ptr = jsimd_idct_float;
167 else
168 method_ptr = jpeg_idct_float;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000169 method = JDCT_FLOAT;
170 break;
171#endif
172 default:
173 ERREXIT(cinfo, JERR_NOT_COMPILED);
174 break;
175 }
176 break;
DRC27fb3fc2012-01-28 01:48:07 +0000177 case 9:
178 method_ptr = jpeg_idct_9x9;
179 method = JDCT_ISLOW; /* jidctint uses islow-style table */
180 break;
181 case 10:
182 method_ptr = jpeg_idct_10x10;
183 method = JDCT_ISLOW; /* jidctint uses islow-style table */
184 break;
185 case 11:
186 method_ptr = jpeg_idct_11x11;
187 method = JDCT_ISLOW; /* jidctint uses islow-style table */
188 break;
189 case 12:
190 method_ptr = jpeg_idct_12x12;
191 method = JDCT_ISLOW; /* jidctint uses islow-style table */
192 break;
193 case 13:
194 method_ptr = jpeg_idct_13x13;
195 method = JDCT_ISLOW; /* jidctint uses islow-style table */
196 break;
197 case 14:
198 method_ptr = jpeg_idct_14x14;
199 method = JDCT_ISLOW; /* jidctint uses islow-style table */
200 break;
201 case 15:
202 method_ptr = jpeg_idct_15x15;
203 method = JDCT_ISLOW; /* jidctint uses islow-style table */
204 break;
205 case 16:
206 method_ptr = jpeg_idct_16x16;
207 method = JDCT_ISLOW; /* jidctint uses islow-style table */
208 break;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000209 default:
DRC49967cd2010-10-09 19:57:51 +0000210 ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->_DCT_scaled_size);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000211 break;
212 }
213 idct->pub.inverse_DCT[ci] = method_ptr;
214 /* Create multiplier table from quant table.
215 * However, we can skip this if the component is uninteresting
216 * or if we already built the table. Also, if no quant table
217 * has yet been saved for the component, we leave the
218 * multiplier table all-zero; we'll be reading zeroes from the
219 * coefficient controller's buffer anyway.
220 */
221 if (! compptr->component_needed || idct->cur_method[ci] == method)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000222 continue;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000223 qtbl = compptr->quant_table;
224 if (qtbl == NULL) /* happens if no data yet for component */
225 continue;
226 idct->cur_method[ci] = method;
227 switch (method) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000228#ifdef PROVIDE_ISLOW_TABLES
229 case JDCT_ISLOW:
230 {
231 /* For LL&M IDCT method, multipliers are equal to raw quantization
Thomas G. Lane489583f1996-02-07 00:00:00 +0000232 * coefficients, but are stored as ints to ensure access efficiency.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000233 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000234 ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000235 for (i = 0; i < DCTSIZE2; i++) {
Thomas G. Lane489583f1996-02-07 00:00:00 +0000236 ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000237 }
238 }
239 break;
240#endif
241#ifdef DCT_IFAST_SUPPORTED
242 case JDCT_IFAST:
243 {
244 /* For AA&N IDCT method, multipliers are equal to quantization
245 * coefficients scaled by scalefactor[row]*scalefactor[col], where
246 * scalefactor[0] = 1
247 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
248 * For integer operation, the multiplier table is to be scaled by
Thomas G. Lane489583f1996-02-07 00:00:00 +0000249 * IFAST_SCALE_BITS.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000250 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000251 IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000252#define CONST_BITS 14
253 static const INT16 aanscales[DCTSIZE2] = {
254 /* precomputed values scaled up by 14 bits */
255 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
256 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
257 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
258 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
259 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
260 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
261 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
262 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
263 };
264 SHIFT_TEMPS
265
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000266 for (i = 0; i < DCTSIZE2; i++) {
267 ifmtbl[i] = (IFAST_MULT_TYPE)
Thomas G. Lane489583f1996-02-07 00:00:00 +0000268 DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000269 (INT32) aanscales[i]),
270 CONST_BITS-IFAST_SCALE_BITS);
271 }
272 }
273 break;
274#endif
275#ifdef DCT_FLOAT_SUPPORTED
276 case JDCT_FLOAT:
277 {
278 /* For float AA&N IDCT method, multipliers are equal to quantization
279 * coefficients scaled by scalefactor[row]*scalefactor[col], where
280 * scalefactor[0] = 1
281 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000282 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000283 FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000284 int row, col;
285 static const double aanscalefactor[DCTSIZE] = {
286 1.0, 1.387039845, 1.306562965, 1.175875602,
287 1.0, 0.785694958, 0.541196100, 0.275899379
288 };
289
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000290 i = 0;
291 for (row = 0; row < DCTSIZE; row++) {
292 for (col = 0; col < DCTSIZE; col++) {
293 fmtbl[i] = (FLOAT_MULT_TYPE)
Thomas G. Lane489583f1996-02-07 00:00:00 +0000294 ((double) qtbl->quantval[i] *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000295 aanscalefactor[row] * aanscalefactor[col]);
296 i++;
297 }
298 }
299 }
300 break;
301#endif
302 default:
303 ERREXIT(cinfo, JERR_NOT_COMPILED);
304 break;
305 }
306 }
307}
308
309
310/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000311 * Initialize IDCT manager.
312 */
313
Thomas G. Lane489583f1996-02-07 00:00:00 +0000314GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000315jinit_inverse_dct (j_decompress_ptr cinfo)
316{
317 my_idct_ptr idct;
318 int ci;
319 jpeg_component_info *compptr;
320
321 idct = (my_idct_ptr)
322 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
323 SIZEOF(my_idct_controller));
324 cinfo->idct = (struct jpeg_inverse_dct *) idct;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000325 idct->pub.start_pass = start_pass;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000326
327 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
328 ci++, compptr++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000329 /* Allocate and pre-zero a multiplier table for each component */
330 compptr->dct_table =
331 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
332 SIZEOF(multiplier_table));
333 MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
334 /* Mark multiplier table not yet set up for any method */
335 idct->cur_method[ci] = -1;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000336 }
337}