blob: be401d5bfc8e9b4727873831bf8c7cf4399efb4d [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jddctmgr.c
3 *
DRCa73e8702012-12-31 02:52:30 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane489583f1996-02-07 00:00:00 +00005 * Copyright (C) 1994-1996, Thomas G. Lane.
DRCa73e8702012-12-31 02:52:30 +00006 * Modifications:
Pierre Ossman59a39382009-03-09 13:15:56 +00007 * Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
DRC36a6eec2010-10-08 08:05:44 +00008 * Copyright (C) 2010, D. R. Commander.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00009 * 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;
119 case 4:
Pierre Ossman59a39382009-03-09 13:15:56 +0000120 if (jsimd_can_idct_4x4())
121 method_ptr = jsimd_idct_4x4;
122 else
123 method_ptr = jpeg_idct_4x4;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000124 method = JDCT_ISLOW; /* jidctred uses islow-style table */
125 break;
126#endif
127 case DCTSIZE:
128 switch (cinfo->dct_method) {
129#ifdef DCT_ISLOW_SUPPORTED
130 case JDCT_ISLOW:
Pierre Ossman59a39382009-03-09 13:15:56 +0000131 if (jsimd_can_idct_islow())
132 method_ptr = jsimd_idct_islow;
133 else
134 method_ptr = jpeg_idct_islow;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000135 method = JDCT_ISLOW;
136 break;
137#endif
138#ifdef DCT_IFAST_SUPPORTED
139 case JDCT_IFAST:
Pierre Ossman59a39382009-03-09 13:15:56 +0000140 if (jsimd_can_idct_ifast())
141 method_ptr = jsimd_idct_ifast;
142 else
143 method_ptr = jpeg_idct_ifast;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000144 method = JDCT_IFAST;
145 break;
146#endif
147#ifdef DCT_FLOAT_SUPPORTED
148 case JDCT_FLOAT:
Pierre Ossman59a39382009-03-09 13:15:56 +0000149 if (jsimd_can_idct_float())
150 method_ptr = jsimd_idct_float;
151 else
152 method_ptr = jpeg_idct_float;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000153 method = JDCT_FLOAT;
154 break;
155#endif
156 default:
157 ERREXIT(cinfo, JERR_NOT_COMPILED);
158 break;
159 }
160 break;
161 default:
DRC49967cd2010-10-09 19:57:51 +0000162 ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->_DCT_scaled_size);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000163 break;
164 }
165 idct->pub.inverse_DCT[ci] = method_ptr;
166 /* Create multiplier table from quant table.
167 * However, we can skip this if the component is uninteresting
168 * or if we already built the table. Also, if no quant table
169 * has yet been saved for the component, we leave the
170 * multiplier table all-zero; we'll be reading zeroes from the
171 * coefficient controller's buffer anyway.
172 */
173 if (! compptr->component_needed || idct->cur_method[ci] == method)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000174 continue;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000175 qtbl = compptr->quant_table;
176 if (qtbl == NULL) /* happens if no data yet for component */
177 continue;
178 idct->cur_method[ci] = method;
179 switch (method) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000180#ifdef PROVIDE_ISLOW_TABLES
181 case JDCT_ISLOW:
182 {
183 /* For LL&M IDCT method, multipliers are equal to raw quantization
Thomas G. Lane489583f1996-02-07 00:00:00 +0000184 * coefficients, but are stored as ints to ensure access efficiency.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000185 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000186 ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000187 for (i = 0; i < DCTSIZE2; i++) {
Thomas G. Lane489583f1996-02-07 00:00:00 +0000188 ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000189 }
190 }
191 break;
192#endif
193#ifdef DCT_IFAST_SUPPORTED
194 case JDCT_IFAST:
195 {
196 /* For AA&N IDCT method, multipliers are equal to quantization
197 * coefficients scaled by scalefactor[row]*scalefactor[col], where
198 * scalefactor[0] = 1
199 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
200 * For integer operation, the multiplier table is to be scaled by
Thomas G. Lane489583f1996-02-07 00:00:00 +0000201 * IFAST_SCALE_BITS.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000202 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000203 IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000204#define CONST_BITS 14
205 static const INT16 aanscales[DCTSIZE2] = {
206 /* precomputed values scaled up by 14 bits */
207 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
208 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
209 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
210 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
211 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
212 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
213 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
214 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
215 };
216 SHIFT_TEMPS
217
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000218 for (i = 0; i < DCTSIZE2; i++) {
219 ifmtbl[i] = (IFAST_MULT_TYPE)
Thomas G. Lane489583f1996-02-07 00:00:00 +0000220 DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000221 (INT32) aanscales[i]),
222 CONST_BITS-IFAST_SCALE_BITS);
223 }
224 }
225 break;
226#endif
227#ifdef DCT_FLOAT_SUPPORTED
228 case JDCT_FLOAT:
229 {
230 /* For float AA&N IDCT method, multipliers are equal to quantization
231 * coefficients scaled by scalefactor[row]*scalefactor[col], where
232 * scalefactor[0] = 1
233 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000234 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000235 FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000236 int row, col;
237 static const double aanscalefactor[DCTSIZE] = {
238 1.0, 1.387039845, 1.306562965, 1.175875602,
239 1.0, 0.785694958, 0.541196100, 0.275899379
240 };
241
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000242 i = 0;
243 for (row = 0; row < DCTSIZE; row++) {
244 for (col = 0; col < DCTSIZE; col++) {
245 fmtbl[i] = (FLOAT_MULT_TYPE)
Thomas G. Lane489583f1996-02-07 00:00:00 +0000246 ((double) qtbl->quantval[i] *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000247 aanscalefactor[row] * aanscalefactor[col]);
248 i++;
249 }
250 }
251 }
252 break;
253#endif
254 default:
255 ERREXIT(cinfo, JERR_NOT_COMPILED);
256 break;
257 }
258 }
259}
260
261
262/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000263 * Initialize IDCT manager.
264 */
265
Thomas G. Lane489583f1996-02-07 00:00:00 +0000266GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000267jinit_inverse_dct (j_decompress_ptr cinfo)
268{
269 my_idct_ptr idct;
270 int ci;
271 jpeg_component_info *compptr;
272
273 idct = (my_idct_ptr)
274 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
275 SIZEOF(my_idct_controller));
276 cinfo->idct = (struct jpeg_inverse_dct *) idct;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000277 idct->pub.start_pass = start_pass;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000278
279 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
280 ci++, compptr++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000281 /* Allocate and pre-zero a multiplier table for each component */
282 compptr->dct_table =
283 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
284 SIZEOF(multiplier_table));
285 MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
286 /* Mark multiplier table not yet set up for any method */
287 idct->cur_method[ci] = -1;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000288 }
289}