1 #if !defined(_FX_JPEG_TURBO_)
2 /*
3  * jidctred.c
4  *
5  * Copyright (C) 1994-1998, Thomas G. Lane.
6  * This file is part of the Independent JPEG Group's software.
7  * For conditions of distribution and use, see the accompanying README file.
8  *
9  * This file contains inverse-DCT routines that produce reduced-size output:
10  * either 4x4, 2x2, or 1x1 pixels from an 8x8 DCT block.
11  *
12  * The implementation is based on the Loeffler, Ligtenberg and Moschytz (LL&M)
13  * algorithm used in jidctint.c.  We simply replace each 8-to-8 1-D IDCT step
14  * with an 8-to-4 step that produces the four averages of two adjacent outputs
15  * (or an 8-to-2 step producing two averages of four outputs, for 2x2 output).
16  * These steps were derived by computing the corresponding values at the end
17  * of the normal LL&M code, then simplifying as much as possible.
18  *
19  * 1x1 is trivial: just take the DC coefficient divided by 8.
20  *
21  * See jidctint.c for additional comments.
22  */
23 
24 #define JPEG_INTERNALS
25 #include "jinclude.h"
26 #include "jpeglib.h"
27 #include "jdct.h"		/* Private declarations for DCT subsystem */
28 
29 #ifdef IDCT_SCALING_SUPPORTED
30 
31 
32 /*
33  * This module is specialized to the case DCTSIZE = 8.
34  */
35 
36 #if DCTSIZE != 8
37   Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
38 #endif
39 
40 
41 /* Scaling is the same as in jidctint.c. */
42 
43 #if BITS_IN_JSAMPLE == 8
44 #define CONST_BITS  13
45 #define PASS1_BITS  2
46 #else
47 #define CONST_BITS  13
48 #define PASS1_BITS  1		/* lose a little precision to avoid overflow */
49 #endif
50 
51 /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
52  * causing a lot of useless floating-point operations at run time.
53  * To get around this we use the following pre-calculated constants.
54  * If you change CONST_BITS you may want to add appropriate values.
55  * (With a reasonable C compiler, you can just rely on the FIX() macro...)
56  */
57 
58 #if CONST_BITS == 13
59 #define FIX_0_211164243  ((INT32)  1730)	/* FIX(0.211164243) */
60 #define FIX_0_509795579  ((INT32)  4176)	/* FIX(0.509795579) */
61 #define FIX_0_601344887  ((INT32)  4926)	/* FIX(0.601344887) */
62 #define FIX_0_720959822  ((INT32)  5906)	/* FIX(0.720959822) */
63 #define FIX_0_765366865  ((INT32)  6270)	/* FIX(0.765366865) */
64 #define FIX_0_850430095  ((INT32)  6967)	/* FIX(0.850430095) */
65 #define FIX_0_899976223  ((INT32)  7373)	/* FIX(0.899976223) */
66 #define FIX_1_061594337  ((INT32)  8697)	/* FIX(1.061594337) */
67 #define FIX_1_272758580  ((INT32)  10426)	/* FIX(1.272758580) */
68 #define FIX_1_451774981  ((INT32)  11893)	/* FIX(1.451774981) */
69 #define FIX_1_847759065  ((INT32)  15137)	/* FIX(1.847759065) */
70 #define FIX_2_172734803  ((INT32)  17799)	/* FIX(2.172734803) */
71 #define FIX_2_562915447  ((INT32)  20995)	/* FIX(2.562915447) */
72 #define FIX_3_624509785  ((INT32)  29692)	/* FIX(3.624509785) */
73 #else
74 #define FIX_0_211164243  FIX(0.211164243)
75 #define FIX_0_509795579  FIX(0.509795579)
76 #define FIX_0_601344887  FIX(0.601344887)
77 #define FIX_0_720959822  FIX(0.720959822)
78 #define FIX_0_765366865  FIX(0.765366865)
79 #define FIX_0_850430095  FIX(0.850430095)
80 #define FIX_0_899976223  FIX(0.899976223)
81 #define FIX_1_061594337  FIX(1.061594337)
82 #define FIX_1_272758580  FIX(1.272758580)
83 #define FIX_1_451774981  FIX(1.451774981)
84 #define FIX_1_847759065  FIX(1.847759065)
85 #define FIX_2_172734803  FIX(2.172734803)
86 #define FIX_2_562915447  FIX(2.562915447)
87 #define FIX_3_624509785  FIX(3.624509785)
88 #endif
89 
90 
91 /* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
92  * For 8-bit samples with the recommended scaling, all the variable
93  * and constant values involved are no more than 16 bits wide, so a
94  * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
95  * For 12-bit samples, a full 32-bit multiplication will be needed.
96  */
97 
98 #if BITS_IN_JSAMPLE == 8
99 #define MULTIPLY(var,const)  MULTIPLY16C16(var,const)
100 #else
101 #define MULTIPLY(var,const)  ((var) * (const))
102 #endif
103 
104 
105 /* Dequantize a coefficient by multiplying it by the multiplier-table
106  * entry; produce an int result.  In this module, both inputs and result
107  * are 16 bits or less, so either int or short multiply will work.
108  */
109 
110 #define DEQUANTIZE(coef,quantval)  (((ISLOW_MULT_TYPE) (coef)) * (quantval))
111 
112 
113 /*
114  * Perform dequantization and inverse DCT on one block of coefficients,
115  * producing a reduced-size 4x4 output block.
116  */
117 
118 GLOBAL(void)
119 jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
120 	       JCOEFPTR coef_block,
121 	       JSAMPARRAY output_buf, JDIMENSION output_col)
122 {
123   INT32 tmp0, tmp2, tmp10, tmp12;
124   INT32 z1, z2, z3, z4;
125   JCOEFPTR inptr;
126   ISLOW_MULT_TYPE * quantptr;
127   int * wsptr;
128   JSAMPROW outptr;
129   JSAMPLE *range_limit = IDCT_range_limit(cinfo);
130   int ctr;
131   int workspace[DCTSIZE*4];	/* buffers data between passes */
132   SHIFT_TEMPS
133 
134   /* Pass 1: process columns from input, store into work array. */
135 
136   inptr = coef_block;
137   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
138   wsptr = workspace;
139   for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
140     /* Don't bother to process column 4, because second pass won't use it */
141     if (ctr == DCTSIZE-4)
142       continue;
143     if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
144 	inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*5] == 0 &&
145 	inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) {
146       /* AC terms all zero; we need not examine term 4 for 4x4 output */
147       int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
148 
149       wsptr[DCTSIZE*0] = dcval;
150       wsptr[DCTSIZE*1] = dcval;
151       wsptr[DCTSIZE*2] = dcval;
152       wsptr[DCTSIZE*3] = dcval;
153 
154       continue;
155     }
156 
157     /* Even part */
158 
159     tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
160     tmp0 <<= (CONST_BITS+1);
161 
162     z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
163     z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
164 
165     tmp2 = MULTIPLY(z2, FIX_1_847759065) + MULTIPLY(z3, - FIX_0_765366865);
166 
167     tmp10 = tmp0 + tmp2;
168     tmp12 = tmp0 - tmp2;
169 
170     /* Odd part */
171 
172     z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
173     z2 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
174     z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
175     z4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
176 
177     tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
178 	 + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
179 	 + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
180 	 + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
181 
182     tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
183 	 + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
184 	 + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
185 	 + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
186 
187     /* Final output stage */
188 
189     wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp2, CONST_BITS-PASS1_BITS+1);
190     wsptr[DCTSIZE*3] = (int) DESCALE(tmp10 - tmp2, CONST_BITS-PASS1_BITS+1);
191     wsptr[DCTSIZE*1] = (int) DESCALE(tmp12 + tmp0, CONST_BITS-PASS1_BITS+1);
192     wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 - tmp0, CONST_BITS-PASS1_BITS+1);
193   }
194 
195   /* Pass 2: process 4 rows from work array, store into output array. */
196 
197   wsptr = workspace;
198   for (ctr = 0; ctr < 4; ctr++) {
199     outptr = output_buf[ctr] + output_col;
200     /* It's not clear whether a zero row test is worthwhile here ... */
201 
202 #ifndef NO_ZERO_ROW_TEST
203     if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 &&
204 	wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
205       /* AC terms all zero */
206       JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
207 				  & RANGE_MASK];
208 
209       outptr[0] = dcval;
210       outptr[1] = dcval;
211       outptr[2] = dcval;
212       outptr[3] = dcval;
213 
214       wsptr += DCTSIZE;		/* advance pointer to next row */
215       continue;
216     }
217 #endif
218 
219     /* Even part */
220 
221     tmp0 = ((INT32) wsptr[0]) << (CONST_BITS+1);
222 
223     tmp2 = MULTIPLY((INT32) wsptr[2], FIX_1_847759065)
224 	 + MULTIPLY((INT32) wsptr[6], - FIX_0_765366865);
225 
226     tmp10 = tmp0 + tmp2;
227     tmp12 = tmp0 - tmp2;
228 
229     /* Odd part */
230 
231     z1 = (INT32) wsptr[7];
232     z2 = (INT32) wsptr[5];
233     z3 = (INT32) wsptr[3];
234     z4 = (INT32) wsptr[1];
235 
236     tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
237 	 + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
238 	 + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
239 	 + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
240 
241     tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
242 	 + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
243 	 + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
244 	 + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
245 
246     /* Final output stage */
247 
248     outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp2,
249 					  CONST_BITS+PASS1_BITS+3+1)
250 			    & RANGE_MASK];
251     outptr[3] = range_limit[(int) DESCALE(tmp10 - tmp2,
252 					  CONST_BITS+PASS1_BITS+3+1)
253 			    & RANGE_MASK];
254     outptr[1] = range_limit[(int) DESCALE(tmp12 + tmp0,
255 					  CONST_BITS+PASS1_BITS+3+1)
256 			    & RANGE_MASK];
257     outptr[2] = range_limit[(int) DESCALE(tmp12 - tmp0,
258 					  CONST_BITS+PASS1_BITS+3+1)
259 			    & RANGE_MASK];
260 
261     wsptr += DCTSIZE;		/* advance pointer to next row */
262   }
263 }
264 
265 
266 /*
267  * Perform dequantization and inverse DCT on one block of coefficients,
268  * producing a reduced-size 2x2 output block.
269  */
270 
271 GLOBAL(void)
jpeg_idct_2x2(j_decompress_ptr cinfo,jpeg_component_info * compptr,JCOEFPTR coef_block,JSAMPARRAY output_buf,JDIMENSION output_col)272 jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
273 	       JCOEFPTR coef_block,
274 	       JSAMPARRAY output_buf, JDIMENSION output_col)
275 {
276   INT32 tmp0, tmp10, z1;
277   JCOEFPTR inptr;
278   ISLOW_MULT_TYPE * quantptr;
279   int * wsptr;
280   JSAMPROW outptr;
281   JSAMPLE *range_limit = IDCT_range_limit(cinfo);
282   int ctr;
283   int workspace[DCTSIZE*2];	/* buffers data between passes */
284   SHIFT_TEMPS
285 
286   /* Pass 1: process columns from input, store into work array. */
287 
288   inptr = coef_block;
289   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
290   wsptr = workspace;
291   for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
292     /* Don't bother to process columns 2,4,6 */
293     if (ctr == DCTSIZE-2 || ctr == DCTSIZE-4 || ctr == DCTSIZE-6)
294       continue;
295     if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*3] == 0 &&
296 	inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*7] == 0) {
297       /* AC terms all zero; we need not examine terms 2,4,6 for 2x2 output */
298       int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
299 
300       wsptr[DCTSIZE*0] = dcval;
301       wsptr[DCTSIZE*1] = dcval;
302 
303       continue;
304     }
305 
306     /* Even part */
307 
308     z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
309     tmp10 = z1 << (CONST_BITS+2);
310 
311     /* Odd part */
312 
313     z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
314     tmp0 = MULTIPLY(z1, - FIX_0_720959822); /* sqrt(2) * (c7-c5+c3-c1) */
315     z1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
316     tmp0 += MULTIPLY(z1, FIX_0_850430095); /* sqrt(2) * (-c1+c3+c5+c7) */
317     z1 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
318     tmp0 += MULTIPLY(z1, - FIX_1_272758580); /* sqrt(2) * (-c1+c3-c5-c7) */
319     z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
320     tmp0 += MULTIPLY(z1, FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
321 
322     /* Final output stage */
323 
324     wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp0, CONST_BITS-PASS1_BITS+2);
325     wsptr[DCTSIZE*1] = (int) DESCALE(tmp10 - tmp0, CONST_BITS-PASS1_BITS+2);
326   }
327 
328   /* Pass 2: process 2 rows from work array, store into output array. */
329 
330   wsptr = workspace;
331   for (ctr = 0; ctr < 2; ctr++) {
332     outptr = output_buf[ctr] + output_col;
333     /* It's not clear whether a zero row test is worthwhile here ... */
334 
335 #ifndef NO_ZERO_ROW_TEST
336     if (wsptr[1] == 0 && wsptr[3] == 0 && wsptr[5] == 0 && wsptr[7] == 0) {
337       /* AC terms all zero */
338       JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
339 				  & RANGE_MASK];
340 
341       outptr[0] = dcval;
342       outptr[1] = dcval;
343 
344       wsptr += DCTSIZE;		/* advance pointer to next row */
345       continue;
346     }
347 #endif
348 
349     /* Even part */
350 
351     tmp10 = ((INT32) wsptr[0]) << (CONST_BITS+2);
352 
353     /* Odd part */
354 
355     tmp0 = MULTIPLY((INT32) wsptr[7], - FIX_0_720959822) /* sqrt(2) * (c7-c5+c3-c1) */
356 	 + MULTIPLY((INT32) wsptr[5], FIX_0_850430095) /* sqrt(2) * (-c1+c3+c5+c7) */
357 	 + MULTIPLY((INT32) wsptr[3], - FIX_1_272758580) /* sqrt(2) * (-c1+c3-c5-c7) */
358 	 + MULTIPLY((INT32) wsptr[1], FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
359 
360     /* Final output stage */
361 
362     outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp0,
363 					  CONST_BITS+PASS1_BITS+3+2)
364 			    & RANGE_MASK];
365     outptr[1] = range_limit[(int) DESCALE(tmp10 - tmp0,
366 					  CONST_BITS+PASS1_BITS+3+2)
367 			    & RANGE_MASK];
368 
369     wsptr += DCTSIZE;		/* advance pointer to next row */
370   }
371 }
372 
373 
374 /*
375  * Perform dequantization and inverse DCT on one block of coefficients,
376  * producing a reduced-size 1x1 output block.
377  */
378 
379 GLOBAL(void)
jpeg_idct_1x1(j_decompress_ptr cinfo,jpeg_component_info * compptr,JCOEFPTR coef_block,JSAMPARRAY output_buf,JDIMENSION output_col)380 jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
381 	       JCOEFPTR coef_block,
382 	       JSAMPARRAY output_buf, JDIMENSION output_col)
383 {
384   int dcval;
385   ISLOW_MULT_TYPE * quantptr;
386   JSAMPLE *range_limit = IDCT_range_limit(cinfo);
387   SHIFT_TEMPS
388 
389   /* We hardly need an inverse DCT routine for this: just take the
390    * average pixel value, which is one-eighth of the DC coefficient.
391    */
392   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
393   dcval = DEQUANTIZE(coef_block[0], quantptr[0]);
394   dcval = (int) DESCALE((INT32) dcval, 3);
395 
396   output_buf[0][output_col] = range_limit[dcval & RANGE_MASK];
397 }
398 
399 #endif /* IDCT_SCALING_SUPPORTED */
400 
401 #endif //_FX_JPEG_TURBO_
402