1
2 /*
3 * mfwddct.c (derived from jfwddct.c, which carries the following info)
4 *
5 * Copyright (C) 1991, 1992, Thomas G. Lane. This file is part of the
6 * Independent JPEG Group's software. For conditions of distribution and use,
7 * see the accompanying README file.
8 *
9 * This file contains the basic DCT (Discrete Cosine Transform) transformation
10 * subroutine.
11 *
12 * This implementation is based on Appendix A.2 of the book "Discrete Cosine
13 * Transform---Algorithms, Advantages, Applications" by K.R. Rao and P. Yip
14 * (Academic Press, Inc, London, 1990). It uses scaled fixed-point arithmetic
15 * instead of floating point.
16 */
17
18 #include "all.h"
19
20 #include "dct.h"
21 #include "mtypes.h"
22 #include "opts.h"
23
24 /*
25 * The poop on this scaling stuff is as follows:
26 *
27 * We have to do addition and subtraction of the integer inputs, which is no
28 * problem, and multiplication by fractional constants, which is a problem to
29 * do in integer arithmetic. We multiply all the constants by DCT_SCALE and
30 * convert them to integer constants (thus retaining LG2_DCT_SCALE bits of
31 * precision in the constants). After doing a multiplication we have to
32 * divide the product by DCT_SCALE, with proper rounding, to produce the
33 * correct output. The division can be implemented cheaply as a right shift
34 * of LG2_DCT_SCALE bits. The DCT equations also specify an additional
35 * division by 2 on the final outputs; this can be folded into the
36 * right-shift by shifting one more bit (see UNFIXH).
37 *
38 * If you are planning to recode this in assembler, you might want to set
39 * LG2_DCT_SCALE to 15. This loses a bit of precision, but then all the
40 * multiplications are between 16-bit quantities (given 8-bit JSAMPLEs!) so
41 * you could use a signed 16x16=>32 bit multiply instruction instead of full
42 * 32x32 multiply. Unfortunately there's no way to describe such a multiply
43 * portably in C, so we've gone for the extra bit of accuracy here.
44 */
45
46 #define EIGHT_BIT_SAMPLES
47 #ifdef EIGHT_BIT_SAMPLES
48 #define LG2_DCT_SCALE 16
49 #else
50 #define LG2_DCT_SCALE 15 /* lose a little precision to avoid overflow */
51 #endif
52
53 #define ONE ((int32) 1)
54
55 #define DCT_SCALE (ONE << LG2_DCT_SCALE)
56
57 /* In some places we shift the inputs left by a couple more bits, */
58 /* so that they can be added to fractional results without too much */
59 /* loss of precision. */
60 #define LG2_OVERSCALE 2
61 #define OVERSCALE (ONE << LG2_OVERSCALE)
62 #define OVERSHIFT(x) ((x) <<= LG2_OVERSCALE)
63
64 /* Scale a fractional constant by DCT_SCALE */
65 #define FIX(x) ((int32) ((x) * DCT_SCALE + 0.5))
66
67 /* Scale a fractional constant by DCT_SCALE/OVERSCALE */
68 /* Such a constant can be multiplied with an overscaled input */
69 /* to produce something that's scaled by DCT_SCALE */
70 #define FIXO(x) ((int32) ((x) * DCT_SCALE / OVERSCALE + 0.5))
71
72 /* Descale and correctly round a value that's scaled by DCT_SCALE */
73 #define UNFIX(x) RIGHT_SHIFT((x) + (ONE << (LG2_DCT_SCALE-1)), LG2_DCT_SCALE)
74
75 /* Same with an additional division by 2, ie, correctly rounded UNFIX(x/2) */
76 #define UNFIXH(x) RIGHT_SHIFT((x) + (ONE << LG2_DCT_SCALE), LG2_DCT_SCALE+1)
77
78 /* Take a value scaled by DCT_SCALE and round to integer scaled by OVERSCALE */
79 #define UNFIXO(x) RIGHT_SHIFT((x) + (ONE << (LG2_DCT_SCALE-1-LG2_OVERSCALE)),\
80 LG2_DCT_SCALE-LG2_OVERSCALE)
81
82 /* Here are the constants we need */
83 /* SIN_i_j is sine of i*pi/j, scaled by DCT_SCALE */
84 /* COS_i_j is cosine of i*pi/j, scaled by DCT_SCALE */
85
86 #define SIN_1_4 FIX(0.707106781)
87 #define COS_1_4 SIN_1_4
88
89 #define SIN_1_8 FIX(0.382683432)
90 #define COS_1_8 FIX(0.923879533)
91 #define SIN_3_8 COS_1_8
92 #define COS_3_8 SIN_1_8
93
94 #define SIN_1_16 FIX(0.195090322)
95 #define COS_1_16 FIX(0.980785280)
96 #define SIN_7_16 COS_1_16
97 #define COS_7_16 SIN_1_16
98
99 #define SIN_3_16 FIX(0.555570233)
100 #define COS_3_16 FIX(0.831469612)
101 #define SIN_5_16 COS_3_16
102 #define COS_5_16 SIN_3_16
103
104 /* OSIN_i_j is sine of i*pi/j, scaled by DCT_SCALE/OVERSCALE */
105 /* OCOS_i_j is cosine of i*pi/j, scaled by DCT_SCALE/OVERSCALE */
106
107 #define OSIN_1_4 FIXO(0.707106781)
108 #define OCOS_1_4 OSIN_1_4
109
110 #define OSIN_1_8 FIXO(0.382683432)
111 #define OCOS_1_8 FIXO(0.923879533)
112 #define OSIN_3_8 OCOS_1_8
113 #define OCOS_3_8 OSIN_1_8
114
115 #define OSIN_1_16 FIXO(0.195090322)
116 #define OCOS_1_16 FIXO(0.980785280)
117 #define OSIN_7_16 OCOS_1_16
118 #define OCOS_7_16 OSIN_1_16
119
120 #define OSIN_3_16 FIXO(0.555570233)
121 #define OCOS_3_16 FIXO(0.831469612)
122 #define OSIN_5_16 OCOS_3_16
123 #define OCOS_5_16 OSIN_3_16
124
125 /* Prototypes */
126 void reference_fwd_dct _ANSI_ARGS_((Block block, Block dest));
127 void mp_fwd_dct_fast _ANSI_ARGS_((Block data2d, Block dest2d));
128 void init_fdct _ANSI_ARGS_((void));
129
130 /*
131 * --------------------------------------------------------------
132 *
133 * mp_fwd_dct_block2 --
134 *
135 * Select the appropriate mp_fwd_dct routine
136 *
137 * Results: None
138 *
139 * Side effects: None
140 *
141 * --------------------------------------------------------------
142 */
143 extern boolean pureDCT;
144 void
mp_fwd_dct_block2(Block data,Block dest)145 mp_fwd_dct_block2(Block data, Block dest)
146 {
147 if (pureDCT) reference_fwd_dct(data, dest);
148 else mp_fwd_dct_fast(data, dest);
149 }
150
151 /*
152 * --------------------------------------------------------------
153 *
154 * mp_fwd_dct_fast --
155 *
156 * Perform the forward DCT on one block of samples.
157 *
158 * A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT on each
159 * column.
160 *
161 * Results: None
162 *
163 * Side effects: Overwrites the input data
164 *
165 * --------------------------------------------------------------
166 */
167
168 void
mp_fwd_dct_fast(Block data2d,Block dest2d)169 mp_fwd_dct_fast(Block data2d, Block dest2d)
170 {
171 int16 *data = (int16 *) data2d; /* this algorithm wants
172 * a 1-d array */
173 int16 *dest = (int16 *) dest2d;
174 int pass, rowctr;
175 register int16 *inptr, *outptr;
176 int16 workspace[DCTSIZE_SQ];
177 SHIFT_TEMPS
178
179 #ifdef ndef
180 {
181 int y;
182
183 printf("fwd_dct (beforehand):\n");
184 for (y = 0; y < 8; y++)
185 printf("%4d %4d %4d %4d %4d %4d %4d %4d\n",
186 data2d[y][0], data2d[y][1],
187 data2d[y][2], data2d[y][3],
188 data2d[y][4], data2d[y][5],
189 data2d[y][6], data2d[y][7]);
190 }
191 #endif
192
193 /*
194 * Each iteration of the inner loop performs one 8-point 1-D DCT. It
195 * reads from a *row* of the input matrix and stores into a *column*
196 * of the output matrix. In the first pass, we read from the data[]
197 * array and store into the local workspace[]. In the second pass,
198 * we read from the workspace[] array and store into data[], thus
199 * performing the equivalent of a columnar DCT pass with no variable
200 * array indexing.
201 */
202
203 inptr = data; /* initialize pointers for first pass */
204 outptr = workspace;
205 for (pass = 1; pass >= 0; pass--) {
206 for (rowctr = DCTSIZE - 1; rowctr >= 0; rowctr--) {
207 /*
208 * many tmps have nonoverlapping lifetime -- flashy
209 * register colourers should be able to do this lot
210 * very well
211 */
212 int32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
213 int32 tmp10, tmp11, tmp12, tmp13;
214 int32 tmp14, tmp15, tmp16, tmp17;
215 int32 tmp25, tmp26;
216 /* SHIFT_TEMPS */
217
218 /* temp0 through tmp7: -512 to +512 */
219 /* if I-block, then -256 to +256 */
220 tmp0 = inptr[7] + inptr[0];
221 tmp1 = inptr[6] + inptr[1];
222 tmp2 = inptr[5] + inptr[2];
223 tmp3 = inptr[4] + inptr[3];
224 tmp4 = inptr[3] - inptr[4];
225 tmp5 = inptr[2] - inptr[5];
226 tmp6 = inptr[1] - inptr[6];
227 tmp7 = inptr[0] - inptr[7];
228
229 /* tmp10 through tmp13: -1024 to +1024 */
230 /* if I-block, then -512 to +512 */
231 tmp10 = tmp3 + tmp0;
232 tmp11 = tmp2 + tmp1;
233 tmp12 = tmp1 - tmp2;
234 tmp13 = tmp0 - tmp3;
235
236 outptr[0] = (int16) UNFIXH((tmp10 + tmp11) * SIN_1_4);
237 outptr[DCTSIZE * 4] = (int16) UNFIXH((tmp10 - tmp11) * COS_1_4);
238
239 outptr[DCTSIZE * 2] = (int16) UNFIXH(tmp13 * COS_1_8 + tmp12 * SIN_1_8);
240 outptr[DCTSIZE * 6] = (int16) UNFIXH(tmp13 * SIN_1_8 - tmp12 * COS_1_8);
241
242 tmp16 = UNFIXO((tmp6 + tmp5) * SIN_1_4);
243 tmp15 = UNFIXO((tmp6 - tmp5) * COS_1_4);
244
245 OVERSHIFT(tmp4);
246 OVERSHIFT(tmp7);
247
248 /*
249 * tmp4, tmp7, tmp15, tmp16 are overscaled by
250 * OVERSCALE
251 */
252
253 tmp14 = tmp4 + tmp15;
254 tmp25 = tmp4 - tmp15;
255 tmp26 = tmp7 - tmp16;
256 tmp17 = tmp7 + tmp16;
257
258 outptr[DCTSIZE] = (int16) UNFIXH(tmp17 * OCOS_1_16 + tmp14 * OSIN_1_16);
259 outptr[DCTSIZE * 7] = (int16) UNFIXH(tmp17 * OCOS_7_16 - tmp14 * OSIN_7_16);
260 outptr[DCTSIZE * 5] = (int16) UNFIXH(tmp26 * OCOS_5_16 + tmp25 * OSIN_5_16);
261 outptr[DCTSIZE * 3] = (int16) UNFIXH(tmp26 * OCOS_3_16 - tmp25 * OSIN_3_16);
262
263 inptr += DCTSIZE; /* advance inptr to next row */
264 outptr++; /* advance outptr to next column */
265 }
266 /* end of pass; in case it was pass 1, set up for pass 2 */
267 inptr = workspace;
268 outptr = dest;
269 }
270 #ifdef ndef
271 {
272 int y;
273
274 printf("fwd_dct (afterward):\n");
275 for (y = 0; y < 8; y++)
276 printf("%4d %4d %4d %4d %4d %4d %4d %4d\n",
277 dest2d[y][0], dest2d[y][1],
278 dest2d[y][2], dest2d[y][3],
279 dest2d[y][4], dest2d[y][5],
280 dest2d[y][6], dest2d[y][7]);
281 }
282 #endif
283 }
284
285
286 /* Modifies from the MPEG2 verification coder */
287 /* fdctref.c, forward discrete cosine transform, double precision */
288
289 /* Copyright (C) 1994, MPEG Software Simulation Group. All Rights Reserved. */
290
291 /*
292 * Disclaimer of Warranty
293 *
294 * These software programs are available to the user without any license fee or
295 * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
296 * any and all warranties, whether express, implied, or statuary, including any
297 * implied warranties or merchantability or of fitness for a particular
298 * purpose. In no event shall the copyright-holder be liable for any
299 * incidental, punitive, or consequential damages of any kind whatsoever
300 * arising from the use of these programs.
301 *
302 * This disclaimer of warranty extends to the user of these programs and user's
303 * customers, employees, agents, transferees, successors, and assigns.
304 *
305 * The MPEG Software Simulation Group does not represent or warrant that the
306 * programs furnished hereunder are free of infringement of any third-party
307 * patents.
308 *
309 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
310 * are subject to royalty fees to patent holders. Many of these patents are
311 * general enough such that they are unavoidable regardless of implementation
312 * design.
313 *
314 */
315
316 #ifndef PI
317 #ifdef M_PI
318 #define PI M_PI
319 #else
320 #define PI 3.14159265358979323846
321 #endif
322 #endif
323
324 /* private data */
325 static double trans_coef[8][8]; /* transform coefficients */
326
init_fdct()327 void init_fdct()
328 {
329 int i, j;
330 double s;
331
332 for (i=0; i<8; i++)
333 {
334 s = (i==0) ? sqrt(0.125) : 0.5;
335
336 for (j=0; j<8; j++)
337 trans_coef[i][j] = s * cos((PI/8.0)*i*(j+0.5));
338 }
339 }
340
reference_fwd_dct(Block block,Block dest)341 void reference_fwd_dct(Block block, Block dest)
342 {
343 int i, j, k;
344 double s;
345 double tmp[64];
346
347 if (DoLaplace) {
348 LaplaceNum++;
349 }
350
351 for (i=0; i<8; i++)
352 for (j=0; j<8; j++)
353 {
354 s = 0.0;
355
356 for (k=0; k<8; k++)
357 s += trans_coef[j][k] * block[i][k];
358
359 tmp[8*i+j] = s;
360 }
361
362 for (i=0; i<8; i++)
363 for (j=0; j<8; j++)
364 {
365 s = 0.0;
366
367 for (k=0; k<8; k++)
368 s += trans_coef[i][k] * tmp[8*k+j];
369
370 if (collect_quant) {
371 fprintf(collect_quant_fp, "%d %lf\n", 8*i+j, s);
372 }
373 if (DoLaplace) {
374 L1[LaplaceCnum][i*8+j] += s*s;
375 L2[LaplaceCnum][i*8+j] += s;
376 }
377
378
379 dest[i][j] = (int)floor(s+0.499999);
380 /*
381 * reason for adding 0.499999 instead of 0.5:
382 * s is quite often x.5 (at least for i and/or j = 0 or 4)
383 * and setting the rounding threshold exactly to 0.5 leads to an
384 * extremely high arithmetic implementation dependency of the result;
385 * s being between x.5 and x.500001 (which is now incorrectly rounded
386 * downwards instead of upwards) is assumed to occur less often
387 * (if at all)
388 */
389 }
390 }
391