2 {$Q+}
4 { This file contains a slow-but-accurate integer implementation of the
5 inverse DCT (Discrete Cosine Transform). In the IJG code, this routine
6 must also perform dequantization of the input coefficients.
8 A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
9 on each row (or vice versa, but it's more convenient to emit a row at
10 a time). Direct algorithms are also available, but they are much more
11 complex and seem not to be any faster when reduced to code.
13 This implementation is based on an algorithm described in
14 C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
15 Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
16 Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
17 The primary algorithm described there uses 11 multiplies and 29 adds.
18 We use their alternate method with 12 multiplies and 32 adds.
19 The advantage of this method is that no data path contains more than one
20 multiplication; this allows a very simple and accurate implementation in
21 scaled fixed-point arithmetic, with a minimal number of shifts. }
23 { Original : jidctint.c ; Copyright (C) 1991-1998, Thomas G. Lane. }
26 interface
28 {$I imjconfig.inc}
30 uses
31 imjmorecfg,
32 imjinclude,
33 imjpeglib,
36 { Perform dequantization and inverse DCT on one block of coefficients. }
38 {GLOBAL}
45 implementation
47 { This module is specialized to the case DCTSIZE = 8. }
49 {$ifndef DCTSIZE_IS_8}
51 {$endif}
53 { The poop on this scaling stuff is as follows:
55 Each 1-D IDCT step produces outputs which are a factor of sqrt(N)
56 larger than the true IDCT outputs. The final outputs are therefore
57 a factor of N larger than desired; since N=8 this can be cured by
58 a simple right shift at the end of the algorithm. The advantage of
59 this arrangement is that we save two multiplications per 1-D IDCT,
60 because the y0 and y4 inputs need not be divided by sqrt(N).
62 We have to do addition and subtraction of the integer inputs, which
63 is no problem, and multiplication by fractional constants, which is
64 a problem to do in integer arithmetic. We multiply all the constants
65 by CONST_SCALE and convert them to integer constants (thus retaining
66 CONST_BITS bits of precision in the constants). After doing a
67 multiplication we have to divide the product by CONST_SCALE, with proper
68 rounding, to produce the correct output. This division can be done
69 cheaply as a right shift of CONST_BITS bits. We postpone shifting
70 as long as possible so that partial sums can be added together with
71 full fractional precision.
73 The outputs of the first pass are scaled up by PASS1_BITS bits so that
74 they are represented to better-than-integral precision. These outputs
75 require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
76 with the recommended scaling. (To scale up 12-bit sample data further, an
77 intermediate INT32 array would be needed.)
79 To avoid overflow of the 32-bit intermediate results in pass 2, we must
80 have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis
81 shows that the values given below are the most effective. }
83 {$ifdef BITS_IN_JSAMPLE_IS_8}
84 const
87 {$else}
88 const
91 {$endif}
93 const
96 const
112 { Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
113 For 8-bit samples with the recommended scaling, all the variable
114 and constant values involved are no more than 16 bits wide, so a
115 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
116 For 12-bit samples, a full 32-bit multiplication will be needed. }
118 {$ifdef BITS_IN_JSAMPLE_IS_8}
120 {$IFDEF BASM16}
121 {$IFNDEF WIN32}
122 {MULTIPLY16C16(var,const)}
124 asm
126 imul Y
129 end;
130 {$ENDIF}
131 {$ENDIF}
134 begin
139 {$else}
140 {#define MULTIPLY(var,const) ((var) * (const))}
142 begin
145 {$endif}
148 { Dequantize a coefficient by multiplying it by the multiplier-table
149 entry; produce an int result. In this module, both inputs and result
150 are 16 bits or less, so either int or short multiply will work. }
153 begin
157 { Descale and correctly round an INT32 value that's scaled by N bits.
158 We assume RIGHT_SHIFT rounds towards minus infinity, so adding
159 the fudge factor is correct for either sign of X. }
162 var
164 begin
165 {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
169 else
171 {$else}
173 {$endif}
176 { Perform dequantization and inverse DCT on one block of coefficients. }
178 {GLOBAL}
184 type
187 var
198 {SHIFT_TEMPS}
199 var
201 var
203 begin
204 { Each IDCT routine is responsible for range-limiting its results and
205 converting them to unsigned form (0..MAXJSAMPLE). The raw outputs could
206 be quite far out of range if the input data is corrupt, so a bulletproof
207 range-limiting step is required. We use a mask-and-table-lookup method
208 to do the combined operations quickly. See the comments with
209 prepare_range_limit_table (in jdmaster.c) for more info. }
214 { Pass 1: process columns from input, store into work array. }
215 { Note results are scaled up by sqrt(8) compared to a true IDCT; }
216 { furthermore, we scale the results by 2**PASS1_BITS. }
222 begin
223 { Due to quantization, we will usually find that many of the input
224 coefficients are zero, especially the AC terms. We can exploit this
225 by short-circuiting the IDCT calculation for any column in which all
226 the AC terms are zero. In that case each output is equal to the
227 DC coefficient (with scale factor as needed).
228 With typical images and quantization tables, half or more of the
229 column DCT calculations can be simplified this way. }
235 begin
236 { AC terms all zero }
251 continue;
254 { Even part: reverse the even part of the forward DCT. }
255 { The rotator is sqrt(2)*c(-6). }
275 { Odd part per figure 8; the matrix is unitary and hence its
276 transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. }
306 { Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 }
322 { Pass 2: process rows from work array, store into output array. }
323 { Note that we must descale the results by a factor of 8 == 2**3, }
324 { and also undo the PASS1_BITS scaling. }
328 begin
331 { Rows of zeroes can be exploited in the same way as we did with columns.
332 However, the column calculation has created many nonzero AC terms, so
333 the simplification applies less often (typically 5% to 10% of the time).
334 On machines with very fast multiplication, it's possible that the
335 test takes more time than it's worth. In that case this section
336 may be commented out. }
338 {$ifndef NO_ZERO_ROW_TEST}
341 begin
342 { AC terms all zero }
356 continue;
358 {$endif}
360 { Even part: reverse the even part of the forward DCT. }
361 { The rotator is sqrt(2)*c(-6). }
378 { Odd part per figure 8; the matrix is unitary and hence its
379 transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. }
409 { Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 }