Line No. | Rev | Author | Line |
---|---|---|---|
1 | 32 | kaklik | /* |
2 | * jidctint.c |
||
3 | * |
||
4 | * Copyright (C) 1991-1998, 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 a slow-but-accurate integer implementation of the |
||
9 | * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine |
||
10 | * must also perform dequantization of the input coefficients. |
||
11 | * |
||
12 | * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT |
||
13 | * on each row (or vice versa, but it's more convenient to emit a row at |
||
14 | * a time). Direct algorithms are also available, but they are much more |
||
15 | * complex and seem not to be any faster when reduced to code. |
||
16 | * |
||
17 | * This implementation is based on an algorithm described in |
||
18 | * C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT |
||
19 | * Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics, |
||
20 | * Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991. |
||
21 | * The primary algorithm described there uses 11 multiplies and 29 adds. |
||
22 | * We use their alternate method with 12 multiplies and 32 adds. |
||
23 | * The advantage of this method is that no data path contains more than one |
||
24 | * multiplication; this allows a very simple and accurate implementation in |
||
25 | * scaled fixed-point arithmetic, with a minimal number of shifts. |
||
26 | */ |
||
27 | |||
28 | #include "GenericTypeDefs.h" |
||
29 | |||
30 | |||
31 | /* |
||
32 | * This module is specialized to the case DCTSIZE = 8. |
||
33 | */ |
||
34 | |||
35 | /* |
||
36 | * The poop on this scaling stuff is as follows: |
||
37 | * |
||
38 | * Each 1-D IDCT step produces outputs which are a factor of sqrt(N) |
||
39 | * larger than the true IDCT outputs. The final outputs are therefore |
||
40 | * a factor of N larger than desired; since N=8 this can be cured by |
||
41 | * a simple right shift at the end of the algorithm. The advantage of |
||
42 | * this arrangement is that we save two multiplications per 1-D IDCT, |
||
43 | * because the y0 and y4 inputs need not be divided by sqrt(N). |
||
44 | * |
||
45 | * We have to do addition and subtraction of the integer inputs, which |
||
46 | * is no problem, and multiplication by fractional constants, which is |
||
47 | * a problem to do in integer arithmetic. We multiply all the constants |
||
48 | * by CONST_SCALE and convert them to integer constants (thus retaining |
||
49 | * CONST_BITS bits of precision in the constants). After doing a |
||
50 | * multiplication we have to divide the product by CONST_SCALE, with proper |
||
51 | * rounding, to produce the correct output. This division can be done |
||
52 | * cheaply as a right shift of CONST_BITS bits. We postpone shifting |
||
53 | * as long as possible so that partial sums can be added together with |
||
54 | * full fractional precision. |
||
55 | * |
||
56 | * The outputs of the first pass are scaled up by PASS1_BITS bits so that |
||
57 | * they are represented to better-than-integral precision. These outputs |
||
58 | * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word |
||
59 | * with the recommended scaling. (To scale up 12-bit sample data further, an |
||
60 | * intermediate INT32 array would be needed.) |
||
61 | * |
||
62 | * To avoid overflow of the 32-bit intermediate results in pass 2, we must |
||
63 | * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis |
||
64 | * shows that the values given below are the most effective. |
||
65 | */ |
||
66 | #define DCTSIZE 8 /* The basic DCT block is 8x8 samples */ |
||
67 | #define DCTSIZE2 64 /* DCTSIZE squared; # of elements in a block */ |
||
68 | #define BITS_IN_JSAMPLE 8 |
||
69 | #define NO_ZERO_ROW_TEST |
||
70 | #define DCTELEM LONG |
||
71 | |||
72 | #define CONST_BITS 13 |
||
73 | #define PASS1_BITS 2 |
||
74 | |||
75 | #define INT32 LONG |
||
76 | |||
77 | /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus |
||
78 | * causing a lot of useless floating-point operations at run time. |
||
79 | * To get around this we use the following pre-calculated constants. |
||
80 | * If you change CONST_BITS you may want to add appropriate values. |
||
81 | * (With a reasonable C compiler, you can just rely on the FIX() macro...) |
||
82 | */ |
||
83 | |||
84 | #define FIX_0_298631336 ((INT32) 2446) /* FIX(0.298631336) */ |
||
85 | #define FIX_0_390180644 ((INT32) 3196) /* FIX(0.390180644) */ |
||
86 | #define FIX_0_541196100 ((INT32) 4433) /* FIX(0.541196100) */ |
||
87 | #define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */ |
||
88 | #define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */ |
||
89 | #define FIX_1_175875602 ((INT32) 9633) /* FIX(1.175875602) */ |
||
90 | #define FIX_1_501321110 ((INT32) 12299) /* FIX(1.501321110) */ |
||
91 | #define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */ |
||
92 | #define FIX_1_961570560 ((INT32) 16069) /* FIX(1.961570560) */ |
||
93 | #define FIX_2_053119869 ((INT32) 16819) /* FIX(2.053119869) */ |
||
94 | #define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */ |
||
95 | #define FIX_3_072711026 ((INT32) 25172) /* FIX(3.072711026) */ |
||
96 | |||
97 | |||
98 | /* Multiply an INT32 variable by an INT32 constant to yield an INT32 result. |
||
99 | * For 8-bit samples with the recommended scaling, all the variable |
||
100 | * and constant values involved are no more than 16 bits wide, so a |
||
101 | * 16x16->32 bit multiply can be used instead of a full 32x32 multiply. |
||
102 | * For 12-bit samples, a full 32-bit multiplication will be needed. |
||
103 | */ |
||
104 | |||
105 | #define DESCALE(x,n) ((x) + ((LONG)0x01 << ((n)-1)))>>(n) |
||
106 | #define MULTIPLY(var,constant) ((LONG)(var) * (constant)); |
||
107 | #define range_limit(x) ((x)<-128)?-128:((x)>127)?127:(x) |
||
108 | |||
109 | /* Dequantize a coefficient by multiplying it by the multiplier-table |
||
110 | * entry; produce an int result. In this module, both inputs and result |
||
111 | * are 16 bits or less, so either int or short multiply will work. |
||
112 | */ |
||
113 | |||
114 | #define DEQUANTIZE(coef,quantval) ((LONG)(coef) * (quantval)) |
||
115 | |||
116 | |||
117 | /* |
||
118 | * Perform dequantization and inverse DCT on one block of coefficients. |
||
119 | */ |
||
120 | |||
121 | void jpeg_idct_islow (SHORT *inbuf, WORD *quantptr) |
||
122 | { |
||
123 | LONG tmp0, tmp1, tmp2, tmp3; |
||
124 | LONG tmp10, tmp11, tmp12, tmp13; |
||
125 | LONG z1, z2, z3, z4, z5; |
||
126 | |||
127 | BYTE ctr; |
||
128 | SHORT *inptr = inbuf, *outptr; |
||
129 | DCTELEM *wsptr; |
||
130 | DCTELEM workspace[DCTSIZE2]; /* buffers data between passes */ |
||
131 | |||
132 | wsptr = workspace; |
||
133 | |||
134 | /* Pass 1: process columns from input, store into work array. */ |
||
135 | /* Note results are scaled up by sqrt(8) compared to a true IDCT; */ |
||
136 | /* furthermore, we scale the results by 2**PASS1_BITS. */ |
||
137 | |||
138 | for (ctr = DCTSIZE; ctr > 0; ctr--) { |
||
139 | /* Due to quantization, we will usually find that many of the input |
||
140 | * coefficients are zero, especially the AC terms. We can exploit this |
||
141 | * by short-circuiting the IDCT calculation for any column in which all |
||
142 | * the AC terms are zero. In that case each output is equal to the |
||
143 | * DC coefficient (with scale factor as needed). |
||
144 | * With typical images and quantization tables, half or more of the |
||
145 | * column DCT calculations can be simplified this way. |
||
146 | */ |
||
147 | |||
148 | if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 && |
||
149 | inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 && |
||
150 | inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 && |
||
151 | inptr[DCTSIZE*7] == 0) { |
||
152 | /* AC terms all zero */ |
||
153 | LONG dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS; |
||
154 | |||
155 | wsptr[DCTSIZE*0] = dcval; |
||
156 | wsptr[DCTSIZE*1] = dcval; |
||
157 | wsptr[DCTSIZE*2] = dcval; |
||
158 | wsptr[DCTSIZE*3] = dcval; |
||
159 | wsptr[DCTSIZE*4] = dcval; |
||
160 | wsptr[DCTSIZE*5] = dcval; |
||
161 | wsptr[DCTSIZE*6] = dcval; |
||
162 | wsptr[DCTSIZE*7] = dcval; |
||
163 | |||
164 | inptr++; /* advance pointers to next column */ |
||
165 | quantptr++; |
||
166 | wsptr++; |
||
167 | continue; |
||
168 | } |
||
169 | |||
170 | /* Even part: reverse the even part of the forward DCT. */ |
||
171 | /* The rotator is sqrt(2)*c(-6). */ |
||
172 | |||
173 | z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); |
||
174 | z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); |
||
175 | |||
176 | z1 = MULTIPLY(z2 + z3, FIX_0_541196100); |
||
177 | tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065); |
||
178 | tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865); |
||
179 | |||
180 | z2 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); |
||
181 | z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); |
||
182 | |||
183 | tmp0 = (z2 + z3) << CONST_BITS; |
||
184 | tmp1 = (z2 - z3) << CONST_BITS; |
||
185 | |||
186 | tmp10 = tmp0 + tmp3; |
||
187 | tmp13 = tmp0 - tmp3; |
||
188 | tmp11 = tmp1 + tmp2; |
||
189 | tmp12 = tmp1 - tmp2; |
||
190 | |||
191 | /* Odd part per figure 8; the matrix is unitary and hence its |
||
192 | * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. |
||
193 | */ |
||
194 | |||
195 | tmp0 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); |
||
196 | tmp1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); |
||
197 | tmp2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); |
||
198 | tmp3 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); |
||
199 | |||
200 | z1 = tmp0 + tmp3; |
||
201 | z2 = tmp1 + tmp2; |
||
202 | z3 = tmp0 + tmp2; |
||
203 | z4 = tmp1 + tmp3; |
||
204 | z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */ |
||
205 | |||
206 | tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */ |
||
207 | tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */ |
||
208 | tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */ |
||
209 | tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */ |
||
210 | z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */ |
||
211 | z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */ |
||
212 | z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */ |
||
213 | z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */ |
||
214 | |||
215 | z3 += z5; |
||
216 | z4 += z5; |
||
217 | |||
218 | tmp0 += z1 + z3; |
||
219 | tmp1 += z2 + z4; |
||
220 | tmp2 += z2 + z3; |
||
221 | tmp3 += z1 + z4; |
||
222 | |||
223 | /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ |
||
224 | |||
225 | wsptr[DCTSIZE*0] = (LONG) DESCALE((tmp10 + tmp3), (CONST_BITS-PASS1_BITS)); |
||
226 | wsptr[DCTSIZE*7] = (LONG) DESCALE((tmp10 - tmp3), (CONST_BITS-PASS1_BITS)); |
||
227 | wsptr[DCTSIZE*1] = (LONG) DESCALE((tmp11 + tmp2), (CONST_BITS-PASS1_BITS)); |
||
228 | wsptr[DCTSIZE*6] = (LONG) DESCALE((tmp11 - tmp2), (CONST_BITS-PASS1_BITS)); |
||
229 | wsptr[DCTSIZE*2] = (LONG) DESCALE((tmp12 + tmp1), (CONST_BITS-PASS1_BITS)); |
||
230 | wsptr[DCTSIZE*5] = (LONG) DESCALE((tmp12 - tmp1), (CONST_BITS-PASS1_BITS)); |
||
231 | wsptr[DCTSIZE*3] = (LONG) DESCALE((tmp13 + tmp0), (CONST_BITS-PASS1_BITS)); |
||
232 | wsptr[DCTSIZE*4] = (LONG) DESCALE((tmp13 - tmp0), (CONST_BITS-PASS1_BITS)); |
||
233 | |||
234 | inptr++; /* advance pointers to next column */ |
||
235 | quantptr++; |
||
236 | wsptr++; |
||
237 | } |
||
238 | |||
239 | /* Pass 2: process rows from work array, store into output array. */ |
||
240 | /* Note that we must descale the results by a factor of 8 == 2**3, */ |
||
241 | /* and also undo the PASS1_BITS scaling. */ |
||
242 | |||
243 | wsptr = workspace; |
||
244 | outptr = &inbuf[0]; |
||
245 | for (ctr = 0; ctr < DCTSIZE; ctr++) { |
||
246 | /* Rows of zeroes can be exploited in the same way as we did with columns. |
||
247 | * However, the column calculation has created many nonzero AC terms, so |
||
248 | * the simplification applies less often (typically 5% to 10% of the time). |
||
249 | * On machines with very fast multiplication, it's possible that the |
||
250 | * test takes more time than it's worth. In that case this section |
||
251 | * may be commented out. |
||
252 | */ |
||
253 | |||
254 | #ifndef NO_ZERO_ROW_TEST |
||
255 | if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 && |
||
256 | wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) { |
||
257 | /* AC terms all zero */ |
||
258 | JSAMPLE dcval = range_limit[(LONG) DESCALE((INT32) wsptr[0], PASS1_BITS+3) |
||
259 | & RANGE_MASK]; |
||
260 | |||
261 | outptr[0] = dcval; |
||
262 | outptr[1] = dcval; |
||
263 | outptr[2] = dcval; |
||
264 | outptr[3] = dcval; |
||
265 | outptr[4] = dcval; |
||
266 | outptr[5] = dcval; |
||
267 | outptr[6] = dcval; |
||
268 | outptr[7] = dcval; |
||
269 | |||
270 | wsptr += DCTSIZE; /* advance pointer to next row */ |
||
271 | continue; |
||
272 | } |
||
273 | #endif |
||
274 | |||
275 | /* Even part: reverse the even part of the forward DCT. */ |
||
276 | /* The rotator is sqrt(2)*c(-6). */ |
||
277 | |||
278 | z2 = (INT32) wsptr[2]; |
||
279 | z3 = (INT32) wsptr[6]; |
||
280 | |||
281 | z1 = MULTIPLY(z2 + z3, FIX_0_541196100); |
||
282 | tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065); |
||
283 | tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865); |
||
284 | |||
285 | tmp0 = ((INT32) wsptr[0] + (INT32) wsptr[4]) << CONST_BITS; |
||
286 | tmp1 = ((INT32) wsptr[0] - (INT32) wsptr[4]) << CONST_BITS; |
||
287 | |||
288 | tmp10 = tmp0 + tmp3; |
||
289 | tmp13 = tmp0 - tmp3; |
||
290 | tmp11 = tmp1 + tmp2; |
||
291 | tmp12 = tmp1 - tmp2; |
||
292 | |||
293 | /* Odd part per figure 8; the matrix is unitary and hence its |
||
294 | * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. |
||
295 | */ |
||
296 | |||
297 | tmp0 = (INT32) wsptr[7]; |
||
298 | tmp1 = (INT32) wsptr[5]; |
||
299 | tmp2 = (INT32) wsptr[3]; |
||
300 | tmp3 = (INT32) wsptr[1]; |
||
301 | |||
302 | z1 = tmp0 + tmp3; |
||
303 | z2 = tmp1 + tmp2; |
||
304 | z3 = tmp0 + tmp2; |
||
305 | z4 = tmp1 + tmp3; |
||
306 | z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */ |
||
307 | |||
308 | tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */ |
||
309 | tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */ |
||
310 | tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */ |
||
311 | tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */ |
||
312 | z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */ |
||
313 | z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */ |
||
314 | z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */ |
||
315 | z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */ |
||
316 | |||
317 | z3 += z5; |
||
318 | z4 += z5; |
||
319 | |||
320 | tmp0 += z1 + z3; |
||
321 | tmp1 += z2 + z4; |
||
322 | tmp2 += z2 + z3; |
||
323 | tmp3 += z1 + z4; |
||
324 | |||
325 | /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */ |
||
326 | |||
327 | outptr[0] = (SHORT)range_limit((LONG) DESCALE(tmp10 + tmp3, CONST_BITS+PASS1_BITS+3)); |
||
328 | outptr[7] = (SHORT)range_limit((LONG) DESCALE(tmp10 - tmp3, CONST_BITS+PASS1_BITS+3)); |
||
329 | outptr[1] = (SHORT)range_limit((LONG) DESCALE(tmp11 + tmp2, CONST_BITS+PASS1_BITS+3)); |
||
330 | outptr[6] = (SHORT)range_limit((LONG) DESCALE(tmp11 - tmp2, CONST_BITS+PASS1_BITS+3)); |
||
331 | outptr[2] = (SHORT)range_limit((LONG) DESCALE(tmp12 + tmp1, CONST_BITS+PASS1_BITS+3)); |
||
332 | outptr[5] = (SHORT)range_limit((LONG) DESCALE(tmp12 - tmp1, CONST_BITS+PASS1_BITS+3)); |
||
333 | outptr[3] = (SHORT)range_limit((LONG) DESCALE(tmp13 + tmp0, CONST_BITS+PASS1_BITS+3)); |
||
334 | outptr[4] = (SHORT)range_limit((LONG) DESCALE(tmp13 - tmp0, CONST_BITS+PASS1_BITS+3)); |
||
335 | |||
336 | outptr += DCTSIZE; /* advance pointer to next row */ |
||
337 | wsptr += DCTSIZE; /* advance pointer to next row */ |
||
338 | } |
||
339 | } |
Powered by WebSVN v2.8.3