1 /*
2 * jcdctmgr.c
3 *
4 * Copyright (C) 1994-1995, 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 forward-DCT management logic.
9 * This code selects a particular DCT implementation to be used,
10 * and it performs related housekeeping chores including coefficient
11 * quantization.
12 */
13
14 #define JPEG_INTERNALS
15 #include "jinclude.h"
16 #include "jpeglib.h"
17 #include "jdct.h" /* Private declarations for DCT subsystem */
18
19
20 /* Private subobject for this module */
21
22 typedef struct {
23 struct jpeg_forward_dct pub; /* public fields */
24
25 /* Pointer to the DCT routine actually in use */
26 forward_DCT_method_ptr do_dct;
27
28 /* The actual post-DCT divisors --- not identical to the quant table
29 * entries, because of scaling (especially for an unnormalized DCT).
30 * Each table is given in normal array order; note that this must
31 * be converted from the zigzag order of the quantization tables.
32 */
33 DCTELEM * divisors[NUM_QUANT_TBLS];
34
35 #ifdef DCT_FLOAT_SUPPORTED
36 /* Same as above for the floating-point case. */
37 float_DCT_method_ptr do_float_dct;
38 FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];
39 #endif
40 } my_fdct_controller;
41
42 typedef my_fdct_controller * my_fdct_ptr;
43
44
45 /*
46 * Initialize for a processing pass.
47 * Verify that all referenced Q-tables are present, and set up
48 * the divisor table for each one.
49 * In the current implementation, DCT of all components is done during
50 * the first pass, even if only some components will be output in the
51 * first scan. Hence all components should be examined here.
52 */
53
54 METHODDEF void
start_pass_fdctmgr(j_compress_ptr cinfo)55 start_pass_fdctmgr (j_compress_ptr cinfo)
56 {
57 my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
58 int ci, qtblno, i;
59 jpeg_component_info *compptr;
60 JQUANT_TBL * qtbl;
61 #ifdef DCT_ISLOW_SUPPORTED
62 DCTELEM * dtbl;
63 #endif
64
65 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
66 ci++, compptr++) {
67 qtblno = compptr->quant_tbl_no;
68 /* Make sure specified quantization table is present */
69 if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
70 cinfo->quant_tbl_ptrs[qtblno] == NULL)
71 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
72 qtbl = cinfo->quant_tbl_ptrs[qtblno];
73 /* Compute divisors for this quant table */
74 /* We may do this more than once for same table, but it's not a big deal */
75 switch (cinfo->dct_method) {
76 #ifdef DCT_ISLOW_SUPPORTED
77 case JDCT_ISLOW:
78 /* For LL&M IDCT method, divisors are equal to raw quantization
79 * coefficients multiplied by 8 (to counteract scaling).
80 */
81 if (fdct->divisors[qtblno] == NULL) {
82 fdct->divisors[qtblno] = (DCTELEM *)
83 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
84 DCTSIZE2 * SIZEOF(DCTELEM));
85 }
86 dtbl = fdct->divisors[qtblno];
87 for (i = 0; i < DCTSIZE2; i++) {
88 dtbl[i] = ((DCTELEM) qtbl->quantval[jpeg_zigzag_order[i]]) << 3;
89 }
90 break;
91 #endif
92 #ifdef DCT_IFAST_SUPPORTED
93 case JDCT_IFAST:
94 {
95 /* For AA&N IDCT method, divisors are equal to quantization
96 * coefficients scaled by scalefactor[row]*scalefactor[col], where
97 * scalefactor[0] = 1
98 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
99 * We apply a further scale factor of 8.
100 */
101 #define CONST_BITS 14
102 static const INT16 aanscales[DCTSIZE2] = {
103 /* precomputed values scaled up by 14 bits: in natural order */
104 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
105 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
106 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
107 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
108 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
109 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
110 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
111 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
112 };
113 SHIFT_TEMPS
114
115 if (fdct->divisors[qtblno] == NULL) {
116 fdct->divisors[qtblno] = (DCTELEM *)
117 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
118 DCTSIZE2 * SIZEOF(DCTELEM));
119 }
120 dtbl = fdct->divisors[qtblno];
121 for (i = 0; i < DCTSIZE2; i++) {
122 dtbl[i] = (DCTELEM)
123 DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[jpeg_zigzag_order[i]],
124 (INT32) aanscales[i]),
125 CONST_BITS-3);
126 }
127 }
128 break;
129 #endif
130 #ifdef DCT_FLOAT_SUPPORTED
131 case JDCT_FLOAT:
132 {
133 /* For float AA&N IDCT method, divisors are equal to quantization
134 * coefficients scaled by scalefactor[row]*scalefactor[col], where
135 * scalefactor[0] = 1
136 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
137 * We apply a further scale factor of 8.
138 * What's actually stored is 1/divisor so that the inner loop can
139 * use a multiplication rather than a division.
140 */
141 FAST_FLOAT * fdtbl;
142 int row, col;
143 static const double aanscalefactor[DCTSIZE] = {
144 1.0, 1.387039845, 1.306562965, 1.175875602,
145 1.0, 0.785694958, 0.541196100, 0.275899379
146 };
147
148 if (fdct->float_divisors[qtblno] == NULL) {
149 fdct->float_divisors[qtblno] = (FAST_FLOAT *)
150 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
151 DCTSIZE2 * SIZEOF(FAST_FLOAT));
152 }
153 fdtbl = fdct->float_divisors[qtblno];
154 i = 0;
155 for (row = 0; row < DCTSIZE; row++) {
156 for (col = 0; col < DCTSIZE; col++) {
157 fdtbl[i] = (FAST_FLOAT)
158 (1.0 / (((double) qtbl->quantval[jpeg_zigzag_order[i]] *
159 aanscalefactor[row] * aanscalefactor[col] * 8.0)));
160 i++;
161 }
162 }
163 }
164 break;
165 #endif
166 default:
167 ERREXIT(cinfo, JERR_NOT_COMPILED);
168 break;
169 }
170 }
171 }
172
173
174 /*
175 * Perform forward DCT on one or more blocks of a component.
176 *
177 * The input samples are taken from the sample_data[] array starting at
178 * position start_row/start_col, and moving to the right for any additional
179 * blocks. The quantized coefficients are returned in coef_blocks[].
180 */
181
182 #ifdef DCT_FLOAT_SUPPORTED
183
184 METHODDEF void
forward_DCT_float(j_compress_ptr cinfo,jpeg_component_info * compptr,JSAMPARRAY sample_data,JBLOCKROW coef_blocks,JDIMENSION start_row,JDIMENSION start_col,JDIMENSION num_blocks)185 forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
186 JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
187 JDIMENSION start_row, JDIMENSION start_col,
188 JDIMENSION num_blocks)
189 /* This version is used for floating-point DCT implementations. */
190 {
191 /* This routine is heavily used, so it's worth coding it tightly. */
192 my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
193 float_DCT_method_ptr do_dct = fdct->do_float_dct;
194 FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];
195 FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
196 JDIMENSION bi;
197
198 sample_data += start_row; /* fold in the vertical offset once */
199
200 for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
201 /* Load data into workspace, applying unsigned->signed conversion */
202 { register FAST_FLOAT *workspaceptr;
203 register JSAMPROW elemptr;
204 register int elemr;
205
206 workspaceptr = workspace;
207 for (elemr = 0; elemr < DCTSIZE; elemr++) {
208 elemptr = sample_data[elemr] + start_col;
209 #if DCTSIZE == 8 /* unroll the inner loop */
210 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
211 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
212 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
213 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
214 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
215 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
216 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
217 *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
218 #else
219 { register int elemc;
220 for (elemc = DCTSIZE; elemc > 0; elemc--) {
221 *workspaceptr++ = (FAST_FLOAT)
222 (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
223 }
224 }
225 #endif
226 }
227 }
228
229 /* Perform the DCT */
230 (*do_dct) (workspace);
231
232 /* Quantize/descale the coefficients, and store into coef_blocks[] */
233 { register FAST_FLOAT temp;
234 register int i;
235 register JCOEFPTR output_ptr = coef_blocks[bi];
236
237 for (i = 0; i < DCTSIZE2; i++) {
238 /* Apply the quantization and scaling factor */
239 temp = workspace[i] * divisors[i];
240 /* Round to nearest integer.
241 * Since C does not specify the direction of rounding for negative
242 * quotients, we have to force the dividend positive for portability.
243 * The maximum coefficient size is +-16K (for 12-bit data), so this
244 * code should work for either 16-bit or 32-bit ints.
245 */
246 output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
247 }
248 }
249 }
250 }
251
252 #endif /* DCT_FLOAT_SUPPORTED */
253
254
255 /*
256 * Initialize FDCT manager.
257 */
258
259 GLOBAL void
jinit_forward_dct(j_compress_ptr cinfo)260 jinit_forward_dct (j_compress_ptr cinfo)
261 {
262 my_fdct_ptr fdct;
263 int i;
264
265 fdct = (my_fdct_ptr)
266 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
267 SIZEOF(my_fdct_controller));
268 cinfo->fdct = (struct jpeg_forward_dct *) fdct;
269 fdct->pub.start_pass = start_pass_fdctmgr;
270
271 switch (cinfo->dct_method) {
272 #ifdef DCT_ISLOW_SUPPORTED
273 case JDCT_ISLOW:
274 fdct->pub.forward_DCT = forward_DCT;
275 fdct->do_dct = jpeg_fdct_islow;
276 break;
277 #endif
278 #ifdef DCT_IFAST_SUPPORTED
279 case JDCT_IFAST:
280 fdct->pub.forward_DCT = forward_DCT;
281 fdct->do_dct = jpeg_fdct_ifast;
282 break;
283 #endif
284 #ifdef DCT_FLOAT_SUPPORTED
285 case JDCT_FLOAT:
286 fdct->pub.forward_DCT = forward_DCT_float;
287 fdct->do_float_dct = jpeg_fdct_float;
288 break;
289 #endif
290 default:
291 ERREXIT(cinfo, JERR_NOT_COMPILED);
292 break;
293 }
294
295 /* Mark divisor tables unallocated */
296 for (i = 0; i < NUM_QUANT_TBLS; i++) {
297 fdct->divisors[i] = NULL;
298 #ifdef DCT_FLOAT_SUPPORTED
299 fdct->float_divisors[i] = NULL;
300 #endif
301 }
302 }
303