Debian Codegraph @

converter/ppm/ppmtompeg/mfwddct.c

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