1 | /* |
2 | * reserved comment block |
3 | * DO NOT REMOVE OR ALTER! |
4 | */ |
5 | /* |
6 | * jidctfst.c |
7 | * |
8 | * Copyright (C) 1994-1998, Thomas G. Lane. |
9 | * This file is part of the Independent JPEG Group's software. |
10 | * For conditions of distribution and use, see the accompanying README file. |
11 | * |
12 | * This file contains a fast, not so accurate integer implementation of the |
13 | * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine |
14 | * must also perform dequantization of the input coefficients. |
15 | * |
16 | * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT |
17 | * on each row (or vice versa, but it's more convenient to emit a row at |
18 | * a time). Direct algorithms are also available, but they are much more |
19 | * complex and seem not to be any faster when reduced to code. |
20 | * |
21 | * This implementation is based on Arai, Agui, and Nakajima's algorithm for |
22 | * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in |
23 | * Japanese, but the algorithm is described in the Pennebaker & Mitchell |
24 | * JPEG textbook (see REFERENCES section in file README). The following code |
25 | * is based directly on figure 4-8 in P&M. |
26 | * While an 8-point DCT cannot be done in less than 11 multiplies, it is |
27 | * possible to arrange the computation so that many of the multiplies are |
28 | * simple scalings of the final outputs. These multiplies can then be |
29 | * folded into the multiplications or divisions by the JPEG quantization |
30 | * table entries. The AA&N method leaves only 5 multiplies and 29 adds |
31 | * to be done in the DCT itself. |
32 | * The primary disadvantage of this method is that with fixed-point math, |
33 | * accuracy is lost due to imprecise representation of the scaled |
34 | * quantization values. The smaller the quantization table entry, the less |
35 | * precise the scaled value, so this implementation does worse with high- |
36 | * quality-setting files than with low-quality ones. |
37 | */ |
38 | |
39 | #define JPEG_INTERNALS |
40 | #include "jinclude.h" |
41 | #include "jpeglib.h" |
42 | #include "jdct.h" /* Private declarations for DCT subsystem */ |
43 | |
44 | #ifdef DCT_IFAST_SUPPORTED |
45 | |
46 | |
47 | /* |
48 | * This module is specialized to the case DCTSIZE = 8. |
49 | */ |
50 | |
51 | #if DCTSIZE != 8 |
52 | Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */ |
53 | #endif |
54 | |
55 | |
56 | /* Scaling decisions are generally the same as in the LL&M algorithm; |
57 | * see jidctint.c for more details. However, we choose to descale |
58 | * (right shift) multiplication products as soon as they are formed, |
59 | * rather than carrying additional fractional bits into subsequent additions. |
60 | * This compromises accuracy slightly, but it lets us save a few shifts. |
61 | * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples) |
62 | * everywhere except in the multiplications proper; this saves a good deal |
63 | * of work on 16-bit-int machines. |
64 | * |
65 | * The dequantized coefficients are not integers because the AA&N scaling |
66 | * factors have been incorporated. We represent them scaled up by PASS1_BITS, |
67 | * so that the first and second IDCT rounds have the same input scaling. |
68 | * For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to |
69 | * avoid a descaling shift; this compromises accuracy rather drastically |
70 | * for small quantization table entries, but it saves a lot of shifts. |
71 | * For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway, |
72 | * so we use a much larger scaling factor to preserve accuracy. |
73 | * |
74 | * A final compromise is to represent the multiplicative constants to only |
75 | * 8 fractional bits, rather than 13. This saves some shifting work on some |
76 | * machines, and may also reduce the cost of multiplication (since there |
77 | * are fewer one-bits in the constants). |
78 | */ |
79 | |
80 | #if BITS_IN_JSAMPLE == 8 |
81 | #define CONST_BITS 8 |
82 | #define PASS1_BITS 2 |
83 | #else |
84 | #define CONST_BITS 8 |
85 | #define PASS1_BITS 1 /* lose a little precision to avoid overflow */ |
86 | #endif |
87 | |
88 | /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus |
89 | * causing a lot of useless floating-point operations at run time. |
90 | * To get around this we use the following pre-calculated constants. |
91 | * If you change CONST_BITS you may want to add appropriate values. |
92 | * (With a reasonable C compiler, you can just rely on the FIX() macro...) |
93 | */ |
94 | |
95 | #if CONST_BITS == 8 |
96 | #define FIX_1_082392200 ((INT32) 277) /* FIX(1.082392200) */ |
97 | #define FIX_1_414213562 ((INT32) 362) /* FIX(1.414213562) */ |
98 | #define FIX_1_847759065 ((INT32) 473) /* FIX(1.847759065) */ |
99 | #define FIX_2_613125930 ((INT32) 669) /* FIX(2.613125930) */ |
100 | #else |
101 | #define FIX_1_082392200 FIX(1.082392200) |
102 | #define FIX_1_414213562 FIX(1.414213562) |
103 | #define FIX_1_847759065 FIX(1.847759065) |
104 | #define FIX_2_613125930 FIX(2.613125930) |
105 | #endif |
106 | |
107 | |
108 | /* We can gain a little more speed, with a further compromise in accuracy, |
109 | * by omitting the addition in a descaling shift. This yields an incorrectly |
110 | * rounded result half the time... |
111 | */ |
112 | |
113 | #ifndef USE_ACCURATE_ROUNDING |
114 | #undef DESCALE |
115 | #define DESCALE(x,n) RIGHT_SHIFT(x, n) |
116 | #endif |
117 | |
118 | |
119 | /* Multiply a DCTELEM variable by an INT32 constant, and immediately |
120 | * descale to yield a DCTELEM result. |
121 | */ |
122 | |
123 | #define MULTIPLY(var,const) ((DCTELEM) DESCALE((var) * (const), CONST_BITS)) |
124 | |
125 | |
126 | /* Dequantize a coefficient by multiplying it by the multiplier-table |
127 | * entry; produce a DCTELEM result. For 8-bit data a 16x16->16 |
128 | * multiplication will do. For 12-bit data, the multiplier table is |
129 | * declared INT32, so a 32-bit multiply will be used. |
130 | */ |
131 | |
132 | #if BITS_IN_JSAMPLE == 8 |
133 | #define DEQUANTIZE(coef,quantval) (((IFAST_MULT_TYPE) (coef)) * (quantval)) |
134 | #else |
135 | #define DEQUANTIZE(coef,quantval) \ |
136 | DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS) |
137 | #endif |
138 | |
139 | |
140 | /* Like DESCALE, but applies to a DCTELEM and produces an int. |
141 | * We assume that int right shift is unsigned if INT32 right shift is. |
142 | */ |
143 | |
144 | #ifdef RIGHT_SHIFT_IS_UNSIGNED |
145 | #define ISHIFT_TEMPS DCTELEM ishift_temp; |
146 | #if BITS_IN_JSAMPLE == 8 |
147 | #define DCTELEMBITS 16 /* DCTELEM may be 16 or 32 bits */ |
148 | #else |
149 | #define DCTELEMBITS 32 /* DCTELEM must be 32 bits */ |
150 | #endif |
151 | #define IRIGHT_SHIFT(x,shft) \ |
152 | ((ishift_temp = (x)) < 0 ? \ |
153 | (ishift_temp >> (shft)) | ((~((DCTELEM) 0)) << (DCTELEMBITS-(shft))) : \ |
154 | (ishift_temp >> (shft))) |
155 | #else |
156 | #define ISHIFT_TEMPS |
157 | #define IRIGHT_SHIFT(x,shft) ((x) >> (shft)) |
158 | #endif |
159 | |
160 | #ifdef USE_ACCURATE_ROUNDING |
161 | #define IDESCALE(x,n) ((int) IRIGHT_SHIFT((x) + (1 << ((n)-1)), n)) |
162 | #else |
163 | #define IDESCALE(x,n) ((int) IRIGHT_SHIFT(x, n)) |
164 | #endif |
165 | |
166 | |
167 | /* |
168 | * Perform dequantization and inverse DCT on one block of coefficients. |
169 | */ |
170 | |
171 | GLOBAL(void) |
172 | jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr, |
173 | JCOEFPTR coef_block, |
174 | JSAMPARRAY output_buf, JDIMENSION output_col) |
175 | { |
176 | DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; |
177 | DCTELEM tmp10, tmp11, tmp12, tmp13; |
178 | DCTELEM z5, z10, z11, z12, z13; |
179 | JCOEFPTR inptr; |
180 | IFAST_MULT_TYPE * quantptr; |
181 | int * wsptr; |
182 | JSAMPROW outptr; |
183 | JSAMPLE *range_limit = IDCT_range_limit(cinfo); |
184 | int ctr; |
185 | int workspace[DCTSIZE2]; /* buffers data between passes */ |
186 | SHIFT_TEMPS /* for DESCALE */ |
187 | ISHIFT_TEMPS /* for IDESCALE */ |
188 | |
189 | /* Pass 1: process columns from input, store into work array. */ |
190 | |
191 | inptr = coef_block; |
192 | quantptr = (IFAST_MULT_TYPE *) compptr->dct_table; |
193 | wsptr = workspace; |
194 | for (ctr = DCTSIZE; ctr > 0; ctr--) { |
195 | /* Due to quantization, we will usually find that many of the input |
196 | * coefficients are zero, especially the AC terms. We can exploit this |
197 | * by short-circuiting the IDCT calculation for any column in which all |
198 | * the AC terms are zero. In that case each output is equal to the |
199 | * DC coefficient (with scale factor as needed). |
200 | * With typical images and quantization tables, half or more of the |
201 | * column DCT calculations can be simplified this way. |
202 | */ |
203 | |
204 | if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 && |
205 | inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 && |
206 | inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 && |
207 | inptr[DCTSIZE*7] == 0) { |
208 | /* AC terms all zero */ |
209 | int dcval = (int) DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); |
210 | |
211 | wsptr[DCTSIZE*0] = dcval; |
212 | wsptr[DCTSIZE*1] = dcval; |
213 | wsptr[DCTSIZE*2] = dcval; |
214 | wsptr[DCTSIZE*3] = dcval; |
215 | wsptr[DCTSIZE*4] = dcval; |
216 | wsptr[DCTSIZE*5] = dcval; |
217 | wsptr[DCTSIZE*6] = dcval; |
218 | wsptr[DCTSIZE*7] = dcval; |
219 | |
220 | inptr++; /* advance pointers to next column */ |
221 | quantptr++; |
222 | wsptr++; |
223 | continue; |
224 | } |
225 | |
226 | /* Even part */ |
227 | |
228 | tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); |
229 | tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); |
230 | tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); |
231 | tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); |
232 | |
233 | tmp10 = tmp0 + tmp2; /* phase 3 */ |
234 | tmp11 = tmp0 - tmp2; |
235 | |
236 | tmp13 = tmp1 + tmp3; /* phases 5-3 */ |
237 | tmp12 = MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; /* 2*c4 */ |
238 | |
239 | tmp0 = tmp10 + tmp13; /* phase 2 */ |
240 | tmp3 = tmp10 - tmp13; |
241 | tmp1 = tmp11 + tmp12; |
242 | tmp2 = tmp11 - tmp12; |
243 | |
244 | /* Odd part */ |
245 | |
246 | tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); |
247 | tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); |
248 | tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); |
249 | tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); |
250 | |
251 | z13 = tmp6 + tmp5; /* phase 6 */ |
252 | z10 = tmp6 - tmp5; |
253 | z11 = tmp4 + tmp7; |
254 | z12 = tmp4 - tmp7; |
255 | |
256 | tmp7 = z11 + z13; /* phase 5 */ |
257 | tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */ |
258 | |
259 | z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */ |
260 | tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */ |
261 | tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */ |
262 | |
263 | tmp6 = tmp12 - tmp7; /* phase 2 */ |
264 | tmp5 = tmp11 - tmp6; |
265 | tmp4 = tmp10 + tmp5; |
266 | |
267 | wsptr[DCTSIZE*0] = (int) (tmp0 + tmp7); |
268 | wsptr[DCTSIZE*7] = (int) (tmp0 - tmp7); |
269 | wsptr[DCTSIZE*1] = (int) (tmp1 + tmp6); |
270 | wsptr[DCTSIZE*6] = (int) (tmp1 - tmp6); |
271 | wsptr[DCTSIZE*2] = (int) (tmp2 + tmp5); |
272 | wsptr[DCTSIZE*5] = (int) (tmp2 - tmp5); |
273 | wsptr[DCTSIZE*4] = (int) (tmp3 + tmp4); |
274 | wsptr[DCTSIZE*3] = (int) (tmp3 - tmp4); |
275 | |
276 | inptr++; /* advance pointers to next column */ |
277 | quantptr++; |
278 | wsptr++; |
279 | } |
280 | |
281 | /* Pass 2: process rows from work array, store into output array. */ |
282 | /* Note that we must descale the results by a factor of 8 == 2**3, */ |
283 | /* and also undo the PASS1_BITS scaling. */ |
284 | |
285 | wsptr = workspace; |
286 | for (ctr = 0; ctr < DCTSIZE; ctr++) { |
287 | outptr = output_buf[ctr] + output_col; |
288 | /* Rows of zeroes can be exploited in the same way as we did with columns. |
289 | * However, the column calculation has created many nonzero AC terms, so |
290 | * the simplification applies less often (typically 5% to 10% of the time). |
291 | * On machines with very fast multiplication, it's possible that the |
292 | * test takes more time than it's worth. In that case this section |
293 | * may be commented out. |
294 | */ |
295 | |
296 | #ifndef NO_ZERO_ROW_TEST |
297 | if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 && |
298 | wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) { |
299 | /* AC terms all zero */ |
300 | JSAMPLE dcval = range_limit[IDESCALE(wsptr[0], PASS1_BITS+3) |
301 | & RANGE_MASK]; |
302 | |
303 | outptr[0] = dcval; |
304 | outptr[1] = dcval; |
305 | outptr[2] = dcval; |
306 | outptr[3] = dcval; |
307 | outptr[4] = dcval; |
308 | outptr[5] = dcval; |
309 | outptr[6] = dcval; |
310 | outptr[7] = dcval; |
311 | |
312 | wsptr += DCTSIZE; /* advance pointer to next row */ |
313 | continue; |
314 | } |
315 | #endif |
316 | |
317 | /* Even part */ |
318 | |
319 | tmp10 = ((DCTELEM) wsptr[0] + (DCTELEM) wsptr[4]); |
320 | tmp11 = ((DCTELEM) wsptr[0] - (DCTELEM) wsptr[4]); |
321 | |
322 | tmp13 = ((DCTELEM) wsptr[2] + (DCTELEM) wsptr[6]); |
323 | tmp12 = MULTIPLY((DCTELEM) wsptr[2] - (DCTELEM) wsptr[6], FIX_1_414213562) |
324 | - tmp13; |
325 | |
326 | tmp0 = tmp10 + tmp13; |
327 | tmp3 = tmp10 - tmp13; |
328 | tmp1 = tmp11 + tmp12; |
329 | tmp2 = tmp11 - tmp12; |
330 | |
331 | /* Odd part */ |
332 | |
333 | z13 = (DCTELEM) wsptr[5] + (DCTELEM) wsptr[3]; |
334 | z10 = (DCTELEM) wsptr[5] - (DCTELEM) wsptr[3]; |
335 | z11 = (DCTELEM) wsptr[1] + (DCTELEM) wsptr[7]; |
336 | z12 = (DCTELEM) wsptr[1] - (DCTELEM) wsptr[7]; |
337 | |
338 | tmp7 = z11 + z13; /* phase 5 */ |
339 | tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */ |
340 | |
341 | z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */ |
342 | tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */ |
343 | tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */ |
344 | |
345 | tmp6 = tmp12 - tmp7; /* phase 2 */ |
346 | tmp5 = tmp11 - tmp6; |
347 | tmp4 = tmp10 + tmp5; |
348 | |
349 | /* Final output stage: scale down by a factor of 8 and range-limit */ |
350 | |
351 | outptr[0] = range_limit[IDESCALE(tmp0 + tmp7, PASS1_BITS+3) |
352 | & RANGE_MASK]; |
353 | outptr[7] = range_limit[IDESCALE(tmp0 - tmp7, PASS1_BITS+3) |
354 | & RANGE_MASK]; |
355 | outptr[1] = range_limit[IDESCALE(tmp1 + tmp6, PASS1_BITS+3) |
356 | & RANGE_MASK]; |
357 | outptr[6] = range_limit[IDESCALE(tmp1 - tmp6, PASS1_BITS+3) |
358 | & RANGE_MASK]; |
359 | outptr[2] = range_limit[IDESCALE(tmp2 + tmp5, PASS1_BITS+3) |
360 | & RANGE_MASK]; |
361 | outptr[5] = range_limit[IDESCALE(tmp2 - tmp5, PASS1_BITS+3) |
362 | & RANGE_MASK]; |
363 | outptr[4] = range_limit[IDESCALE(tmp3 + tmp4, PASS1_BITS+3) |
364 | & RANGE_MASK]; |
365 | outptr[3] = range_limit[IDESCALE(tmp3 - tmp4, PASS1_BITS+3) |
366 | & RANGE_MASK]; |
367 | |
368 | wsptr += DCTSIZE; /* advance pointer to next row */ |
369 | } |
370 | } |
371 | |
372 | #endif /* DCT_IFAST_SUPPORTED */ |
373 | |