3 { This file contains a fast, not so accurate integer implementation of the
4 inverse DCT (Discrete Cosine Transform). In the IJG code, this routine
5 must also perform dequantization of the input coefficients.
7 A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
8 on each row (or vice versa, but it's more convenient to emit a row at
9 a time). Direct algorithms are also available, but they are much more
10 complex and seem not to be any faster when reduced to code.
12 This implementation is based on Arai, Agui, and Nakajima's algorithm for
13 scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
14 Japanese, but the algorithm is described in the Pennebaker & Mitchell
15 JPEG textbook (see REFERENCES section in file README). The following code
16 is based directly on figure 4-8 in P&M.
17 While an 8-point DCT cannot be done in less than 11 multiplies, it is
18 possible to arrange the computation so that many of the multiplies are
19 simple scalings of the final outputs. These multiplies can then be
20 folded into the multiplications or divisions by the JPEG quantization
21 table entries. The AA&N method leaves only 5 multiplies and 29 adds
22 to be done in the DCT itself.
23 The primary disadvantage of this method is that with fixed-point math,
24 accuracy is lost due to imprecise representation of the scaled
25 quantization values. The smaller the quantization table entry, the less
26 precise the scaled value, so this implementation does worse with high-
27 quality-setting files than with low-quality ones. }
29 { Original : jidctfst.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
32 interface
34 {$I imjconfig.inc}
36 uses
37 imjmorecfg,
38 imjinclude,
39 imjpeglib,
43 { Perform dequantization and inverse DCT on one block of coefficients. }
45 {GLOBAL}
52 implementation
54 { This module is specialized to the case DCTSIZE = 8. }
56 {$ifndef DCTSIZE_IS_8}
58 {$endif}
60 { Scaling decisions are generally the same as in the LL&M algorithm;
61 see jidctint.c for more details. However, we choose to descale
62 (right shift) multiplication products as soon as they are formed,
63 rather than carrying additional fractional bits into subsequent additions.
64 This compromises accuracy slightly, but it lets us save a few shifts.
65 More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
66 everywhere except in the multiplications proper; this saves a good deal
67 of work on 16-bit-int machines.
69 The dequantized coefficients are not integers because the AA&N scaling
70 factors have been incorporated. We represent them scaled up by PASS1_BITS,
71 so that the first and second IDCT rounds have the same input scaling.
72 For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to
73 avoid a descaling shift; this compromises accuracy rather drastically
74 for small quantization table entries, but it saves a lot of shifts.
75 For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway,
76 so we use a much larger scaling factor to preserve accuracy.
78 A final compromise is to represent the multiplicative constants to only
79 8 fractional bits, rather than 13. This saves some shifting work on some
80 machines, and may also reduce the cost of multiplication (since there
81 are fewer one-bits in the constants). }
83 {$ifdef BITS_IN_JSAMPLE_IS_8}
84 const
87 {$else}
88 const
91 {$endif}
94 const
101 { Descale and correctly round an INT32 value that's scaled by N bits.
102 We assume RIGHT_SHIFT rounds towards minus infinity, so adding
103 the fudge factor is correct for either sign of X. }
106 var
108 begin
109 {$ifdef USE_ACCURATE_ROUNDING}
111 {$else}
112 { We can gain a little more speed, with a further compromise in accuracy,
113 by omitting the addition in a descaling shift. This yields an incorrectly
114 rounded result half the time... }
116 {$endif}
118 {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
121 else
122 {$endif}
127 { Multiply a DCTELEM variable by an INT32 constant, and immediately
128 descale to yield a DCTELEM result. }
130 {(DCTELEM( DESCALE((var) * (const), CONST_BITS))}
132 begin
137 { Dequantize a coefficient by multiplying it by the multiplier-table
138 entry; produce a DCTELEM result. For 8-bit data a 16x16->16
139 multiplication will do. For 12-bit data, the multiplier table is
140 declared INT32, so a 32-bit multiply will be used. }
142 {$ifdef BITS_IN_JSAMPLE_IS_8}
144 begin
147 {$else}
149 begin
152 {$endif}
155 { Like DESCALE, but applies to a DCTELEM and produces an int.
156 We assume that int right shift is unsigned if INT32 right shift is. }
159 {$ifdef BITS_IN_JSAMPLE_IS_8}
160 const
162 {$else}
163 const
165 {$endif}
166 var
168 begin
169 {$ifndef USE_ACCURATE_ROUNDING}
171 {$else}
172 { We can gain a little more speed, with a further compromise in accuracy,
173 by omitting the addition in a descaling shift. This yields an incorrectly
174 rounded result half the time... }
176 {$endif}
178 {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
182 else
183 {$endif}
189 { Perform dequantization and inverse DCT on one block of coefficients. }
191 {GLOBAL}
197 type
200 var
213 var
215 var
217 begin
218 { Each IDCT routine is responsible for range-limiting its results and
219 converting them to unsigned form (0..MAXJSAMPLE). The raw outputs could
220 be quite far out of range if the input data is corrupt, so a bulletproof
221 range-limiting step is required. We use a mask-and-table-lookup method
222 to do the combined operations quickly. See the comments with
223 prepare_range_limit_table (in jdmaster.c) for more info. }
226 { Pass 1: process columns from input, store into work array. }
232 begin
233 { Due to quantization, we will usually find that many of the input
234 coefficients are zero, especially the AC terms. We can exploit this
235 by short-circuiting the IDCT calculation for any column in which all
236 the AC terms are zero. In that case each output is equal to the
237 DC coefficient (with scale factor as needed).
238 With typical images and quantization tables, half or more of the
239 column DCT calculations can be simplified this way. }
244 begin
245 { AC terms all zero }
260 continue;
263 { Even part }
281 { Odd part }
318 { Pass 2: process rows from work array, store into output array. }
319 { Note that we must descale the results by a factor of 8 == 2**3, }
320 { and also undo the PASS1_BITS scaling. }
324 begin
326 { Rows of zeroes can be exploited in the same way as we did with columns.
327 However, the column calculation has created many nonzero AC terms, so
328 the simplification applies less often (typically 5% to 10% of the time).
329 On machines with very fast multiplication, it's possible that the
330 test takes more time than it's worth. In that case this section
331 may be commented out. }
333 {$ifndef NO_ZERO_ROW_TEST}
336 begin
337 { AC terms all zero }
351 continue;
353 {$endif}
355 { Even part }
369 { Odd part }
387 { Final output stage: scale down by a factor of 8 and range-limit }