1 /*
2 * jddctmgr.c
3 *
4 * Copyright (C) 1994, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
7 *
8 * This file contains the inverse-DCT management logic.
9 * This code selects a particular IDCT implementation to be used,
10 * and it performs related housekeeping chores. No code in this file
11 * is executed per IDCT step, only during pass setup.
12 *
13 * Note that the IDCT routines are responsible for performing coefficient
14 * dequantization as well as the IDCT proper. This module sets up the
15 * dequantization multiplier table needed by the IDCT routine.
16 */
17
18 #define JPEG_INTERNALS
19 #include "jinclude.h"
20 #include "jpeglib.h"
21 #include "jdct.h" /* Private declarations for DCT subsystem */
22
23
24 /* Private subobject for this module */
25
26 typedef struct {
27 struct jpeg_inverse_dct pub; /* public fields */
28
29 /* Record the IDCT method type actually selected for each component */
30 J_DCT_METHOD real_method[MAX_COMPONENTS];
31 } my_idct_controller;
32
33 typedef my_idct_controller * my_idct_ptr;
34
35
36 /* ZIG[i] is the zigzag-order position of the i'th element of a DCT block */
37 /* read in natural order (left to right, top to bottom). */
38 static const int ZIG[DCTSIZE2] = {
39 0, 1, 5, 6, 14, 15, 27, 28,
40 2, 4, 7, 13, 16, 26, 29, 42,
41 3, 8, 12, 17, 25, 30, 41, 43,
42 9, 11, 18, 24, 31, 40, 44, 53,
43 10, 19, 23, 32, 39, 45, 52, 54,
44 20, 22, 33, 38, 46, 51, 55, 60,
45 21, 34, 37, 47, 50, 56, 59, 61,
46 35, 36, 48, 49, 57, 58, 62, 63
47 };
48
49
50 /* The current scaled-IDCT routines require ISLOW-style multiplier tables,
51 * so be sure to compile that code if either ISLOW or SCALING is requested.
52 */
53 #ifdef DCT_ISLOW_SUPPORTED
54 #define PROVIDE_ISLOW_TABLES
55 #else
56 #ifdef IDCT_SCALING_SUPPORTED
57 #define PROVIDE_ISLOW_TABLES
58 #endif
59 #endif
60
61
62 /*
63 * Initialize for an input scan.
64 *
65 * Verify that all referenced Q-tables are present, and set up
66 * the multiplier table for each one.
67 * With a multiple-scan JPEG file, this is called during each input scan,
68 * NOT during the final output pass where the IDCT is actually done.
69 * The purpose is to save away the current Q-table contents just in case
70 * the encoder changes tables between scans. This decoder will dequantize
71 * any component using the Q-table which was current at the start of the
72 * first scan using that component.
73 */
74
75 METHODDEF void
start_input_pass(j_decompress_ptr cinfo)76 start_input_pass (j_decompress_ptr cinfo)
77 {
78 my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
79 int ci, qtblno, i;
80 jpeg_component_info *compptr;
81 JQUANT_TBL * qtbl;
82
83 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
84 compptr = cinfo->cur_comp_info[ci];
85 qtblno = compptr->quant_tbl_no;
86 /* Make sure specified quantization table is present */
87 if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
88 cinfo->quant_tbl_ptrs[qtblno] == NULL)
89 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
90 qtbl = cinfo->quant_tbl_ptrs[qtblno];
91 /* Create multiplier table from quant table, unless we already did so. */
92 if (compptr->dct_table != NULL)
93 continue;
94 switch (idct->real_method[compptr->component_index]) {
95 #ifdef PROVIDE_ISLOW_TABLES
96 case JDCT_ISLOW:
97 {
98 /* For LL&M IDCT method, multipliers are equal to raw quantization
99 * coefficients, but are stored in natural order as ints.
100 */
101 ISLOW_MULT_TYPE * ismtbl;
102 compptr->dct_table =
103 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
104 DCTSIZE2 * SIZEOF(ISLOW_MULT_TYPE));
105 ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
106 for (i = 0; i < DCTSIZE2; i++) {
107 ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[ZIG[i]];
108 }
109 }
110 break;
111 #endif
112 #ifdef DCT_IFAST_SUPPORTED
113 case JDCT_IFAST:
114 {
115 /* For AA&N IDCT method, multipliers are equal to quantization
116 * coefficients scaled by scalefactor[row]*scalefactor[col], where
117 * scalefactor[0] = 1
118 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
119 * For integer operation, the multiplier table is to be scaled by
120 * IFAST_SCALE_BITS. The multipliers are stored in natural order.
121 */
122 IFAST_MULT_TYPE * ifmtbl;
123 #define CONST_BITS 14
124 static const INT16 aanscales[DCTSIZE2] = {
125 /* precomputed values scaled up by 14 bits */
126 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
127 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
128 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
129 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
130 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
131 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
132 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
133 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
134 };
135 SHIFT_TEMPS
136
137 compptr->dct_table =
138 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
139 DCTSIZE2 * SIZEOF(IFAST_MULT_TYPE));
140 ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
141 for (i = 0; i < DCTSIZE2; i++) {
142 ifmtbl[i] = (IFAST_MULT_TYPE)
143 DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[ZIG[i]],
144 (INT32) aanscales[i]),
145 CONST_BITS-IFAST_SCALE_BITS);
146 }
147 }
148 break;
149 #endif
150 #ifdef DCT_FLOAT_SUPPORTED
151 case JDCT_FLOAT:
152 {
153 /* For float AA&N IDCT method, multipliers are equal to quantization
154 * coefficients scaled by scalefactor[row]*scalefactor[col], where
155 * scalefactor[0] = 1
156 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
157 * The multipliers are stored in natural order.
158 */
159 FLOAT_MULT_TYPE * fmtbl;
160 int row, col;
161 static const double aanscalefactor[DCTSIZE] = {
162 1.0, 1.387039845, 1.306562965, 1.175875602,
163 1.0, 0.785694958, 0.541196100, 0.275899379
164 };
165
166 compptr->dct_table =
167 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
168 DCTSIZE2 * SIZEOF(FLOAT_MULT_TYPE));
169 fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
170 i = 0;
171 for (row = 0; row < DCTSIZE; row++) {
172 for (col = 0; col < DCTSIZE; col++) {
173 fmtbl[i] = (FLOAT_MULT_TYPE)
174 ((double) qtbl->quantval[ZIG[i]] *
175 aanscalefactor[row] * aanscalefactor[col]);
176 i++;
177 }
178 }
179 }
180 break;
181 #endif
182 default:
183 ERREXIT(cinfo, JERR_NOT_COMPILED);
184 break;
185 }
186 }
187 }
188
189
190 /*
191 * Prepare for an output pass that will actually perform IDCTs.
192 *
193 * start_input_pass should already have been done for all components
194 * of interest; we need only verify that this is true.
195 * Note that uninteresting components are not required to have loaded tables.
196 * This allows the master controller to stop before reading the whole file
197 * if it has obtained the data for the interesting component(s).
198 */
199
200 METHODDEF void
start_output_pass(j_decompress_ptr cinfo)201 start_output_pass (j_decompress_ptr cinfo)
202 {
203 jpeg_component_info *compptr;
204 int ci;
205
206 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
207 ci++, compptr++) {
208 if (! compptr->component_needed)
209 continue;
210 if (compptr->dct_table == NULL)
211 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, compptr->quant_tbl_no);
212 }
213 }
214
215
216 /*
217 * Initialize IDCT manager.
218 */
219
220 GLOBAL void
jinit_inverse_dct(j_decompress_ptr cinfo)221 jinit_inverse_dct (j_decompress_ptr cinfo)
222 {
223 my_idct_ptr idct;
224 int ci;
225 jpeg_component_info *compptr;
226
227 idct = (my_idct_ptr)
228 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
229 SIZEOF(my_idct_controller));
230 cinfo->idct = (struct jpeg_inverse_dct *) idct;
231 idct->pub.start_input_pass = start_input_pass;
232 idct->pub.start_output_pass = start_output_pass;
233
234 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
235 ci++, compptr++) {
236 compptr->dct_table = NULL; /* initialize tables to "not prepared" */
237 switch (compptr->DCT_scaled_size) {
238 #ifdef IDCT_SCALING_SUPPORTED
239 case 1:
240 idct->pub.inverse_DCT[ci] = jpeg_idct_1x1;
241 idct->real_method[ci] = JDCT_ISLOW; /* jidctred uses islow-style table */
242 break;
243 case 2:
244 idct->pub.inverse_DCT[ci] = jpeg_idct_2x2;
245 idct->real_method[ci] = JDCT_ISLOW; /* jidctred uses islow-style table */
246 break;
247 case 4:
248 idct->pub.inverse_DCT[ci] = jpeg_idct_4x4;
249 idct->real_method[ci] = JDCT_ISLOW; /* jidctred uses islow-style table */
250 break;
251 #endif
252 case DCTSIZE:
253 switch (cinfo->dct_method) {
254 #ifdef DCT_ISLOW_SUPPORTED
255 case JDCT_ISLOW:
256 idct->pub.inverse_DCT[ci] = jpeg_idct_islow;
257 idct->real_method[ci] = JDCT_ISLOW;
258 break;
259 #endif
260 #ifdef DCT_IFAST_SUPPORTED
261 case JDCT_IFAST:
262 idct->pub.inverse_DCT[ci] = jpeg_idct_ifast;
263 idct->real_method[ci] = JDCT_IFAST;
264 break;
265 #endif
266 #ifdef DCT_FLOAT_SUPPORTED
267 case JDCT_FLOAT:
268 idct->pub.inverse_DCT[ci] = jpeg_idct_float;
269 idct->real_method[ci] = JDCT_FLOAT;
270 break;
271 #endif
272 default:
273 ERREXIT(cinfo, JERR_NOT_COMPILED);
274 break;
275 }
276 break;
277 default:
278 ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
279 break;
280 }
281 }
282 }
283