]> granicus.if.org Git - libvpx/blob - vpx_dsp/fwd_txfm.c
Merge "vp9-denoiser: Avoid copy-block when denoising is at LowLow level."
[libvpx] / vpx_dsp / fwd_txfm.c
1 /*
2  *  Copyright (c) 2015 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include "vpx_dsp/fwd_txfm.h"
12
13 void vpx_fdct4x4_c(const int16_t *input, tran_low_t *output, int stride) {
14   // The 2D transform is done with two passes which are actually pretty
15   // similar. In the first one, we transform the columns and transpose
16   // the results. In the second one, we transform the rows. To achieve that,
17   // as the first pass results are transposed, we transpose the columns (that
18   // is the transposed rows) and transpose the results (so that it goes back
19   // in normal/row positions).
20   int pass;
21   // We need an intermediate buffer between passes.
22   tran_low_t intermediate[4 * 4];
23   const int16_t *in_pass0 = input;
24   const tran_low_t *in = NULL;
25   tran_low_t *out = intermediate;
26   // Do the two transform/transpose passes
27   for (pass = 0; pass < 2; ++pass) {
28     tran_high_t input[4];      // canbe16
29     tran_high_t step[4];       // canbe16
30     tran_high_t temp1, temp2;  // needs32
31     int i;
32     for (i = 0; i < 4; ++i) {
33       // Load inputs.
34       if (0 == pass) {
35         input[0] = in_pass0[0 * stride] * 16;
36         input[1] = in_pass0[1 * stride] * 16;
37         input[2] = in_pass0[2 * stride] * 16;
38         input[3] = in_pass0[3 * stride] * 16;
39         if (i == 0 && input[0]) {
40           input[0] += 1;
41         }
42       } else {
43         input[0] = in[0 * 4];
44         input[1] = in[1 * 4];
45         input[2] = in[2 * 4];
46         input[3] = in[3 * 4];
47       }
48       // Transform.
49       step[0] = input[0] + input[3];
50       step[1] = input[1] + input[2];
51       step[2] = input[1] - input[2];
52       step[3] = input[0] - input[3];
53       temp1 = (step[0] + step[1]) * cospi_16_64;
54       temp2 = (step[0] - step[1]) * cospi_16_64;
55       out[0] = (tran_low_t)fdct_round_shift(temp1);
56       out[2] = (tran_low_t)fdct_round_shift(temp2);
57       temp1 = step[2] * cospi_24_64 + step[3] * cospi_8_64;
58       temp2 = -step[2] * cospi_8_64 + step[3] * cospi_24_64;
59       out[1] = (tran_low_t)fdct_round_shift(temp1);
60       out[3] = (tran_low_t)fdct_round_shift(temp2);
61       // Do next column (which is a transposed row in second/horizontal pass)
62       in_pass0++;
63       in++;
64       out += 4;
65     }
66     // Setup in/out for next pass.
67     in = intermediate;
68     out = output;
69   }
70
71   {
72     int i, j;
73     for (i = 0; i < 4; ++i) {
74       for (j = 0; j < 4; ++j)
75         output[j + i * 4] = (output[j + i * 4] + 1) >> 2;
76     }
77   }
78 }
79
80 void vpx_fdct4x4_1_c(const int16_t *input, tran_low_t *output, int stride) {
81   int r, c;
82   tran_low_t sum = 0;
83   for (r = 0; r < 4; ++r)
84     for (c = 0; c < 4; ++c)
85       sum += input[r * stride + c];
86
87   output[0] = sum << 1;
88 }
89
90 void vpx_fdct8x8_c(const int16_t *input, tran_low_t *final_output, int stride) {
91   int i, j;
92   tran_low_t intermediate[64];
93   int pass;
94   tran_low_t *output = intermediate;
95   const tran_low_t *in = NULL;
96
97   // Transform columns
98   for (pass = 0; pass < 2; ++pass) {
99     tran_high_t s0, s1, s2, s3, s4, s5, s6, s7;  // canbe16
100     tran_high_t t0, t1, t2, t3;                  // needs32
101     tran_high_t x0, x1, x2, x3;                  // canbe16
102
103     int i;
104     for (i = 0; i < 8; i++) {
105       // stage 1
106       if (pass == 0) {
107         s0 = (input[0 * stride] + input[7 * stride]) * 4;
108         s1 = (input[1 * stride] + input[6 * stride]) * 4;
109         s2 = (input[2 * stride] + input[5 * stride]) * 4;
110         s3 = (input[3 * stride] + input[4 * stride]) * 4;
111         s4 = (input[3 * stride] - input[4 * stride]) * 4;
112         s5 = (input[2 * stride] - input[5 * stride]) * 4;
113         s6 = (input[1 * stride] - input[6 * stride]) * 4;
114         s7 = (input[0 * stride] - input[7 * stride]) * 4;
115         ++input;
116       } else {
117         s0 = in[0 * 8] + in[7 * 8];
118         s1 = in[1 * 8] + in[6 * 8];
119         s2 = in[2 * 8] + in[5 * 8];
120         s3 = in[3 * 8] + in[4 * 8];
121         s4 = in[3 * 8] - in[4 * 8];
122         s5 = in[2 * 8] - in[5 * 8];
123         s6 = in[1 * 8] - in[6 * 8];
124         s7 = in[0 * 8] - in[7 * 8];
125         ++in;
126       }
127
128       // fdct4(step, step);
129       x0 = s0 + s3;
130       x1 = s1 + s2;
131       x2 = s1 - s2;
132       x3 = s0 - s3;
133       t0 = (x0 + x1) * cospi_16_64;
134       t1 = (x0 - x1) * cospi_16_64;
135       t2 =  x2 * cospi_24_64 + x3 *  cospi_8_64;
136       t3 = -x2 * cospi_8_64  + x3 * cospi_24_64;
137       output[0] = (tran_low_t)fdct_round_shift(t0);
138       output[2] = (tran_low_t)fdct_round_shift(t2);
139       output[4] = (tran_low_t)fdct_round_shift(t1);
140       output[6] = (tran_low_t)fdct_round_shift(t3);
141
142       // Stage 2
143       t0 = (s6 - s5) * cospi_16_64;
144       t1 = (s6 + s5) * cospi_16_64;
145       t2 = fdct_round_shift(t0);
146       t3 = fdct_round_shift(t1);
147
148       // Stage 3
149       x0 = s4 + t2;
150       x1 = s4 - t2;
151       x2 = s7 - t3;
152       x3 = s7 + t3;
153
154       // Stage 4
155       t0 = x0 * cospi_28_64 + x3 *   cospi_4_64;
156       t1 = x1 * cospi_12_64 + x2 *  cospi_20_64;
157       t2 = x2 * cospi_12_64 + x1 * -cospi_20_64;
158       t3 = x3 * cospi_28_64 + x0 *  -cospi_4_64;
159       output[1] = (tran_low_t)fdct_round_shift(t0);
160       output[3] = (tran_low_t)fdct_round_shift(t2);
161       output[5] = (tran_low_t)fdct_round_shift(t1);
162       output[7] = (tran_low_t)fdct_round_shift(t3);
163       output += 8;
164     }
165     in  = intermediate;
166     output = final_output;
167   }
168
169   // Rows
170   for (i = 0; i < 8; ++i) {
171     for (j = 0; j < 8; ++j)
172       final_output[j + i * 8] /= 2;
173   }
174 }
175
176 void vpx_fdct8x8_1_c(const int16_t *input, tran_low_t *output, int stride) {
177   int r, c;
178   tran_low_t sum = 0;
179   for (r = 0; r < 8; ++r)
180     for (c = 0; c < 8; ++c)
181       sum += input[r * stride + c];
182
183   output[0] = sum;
184 }
185
186 void vpx_fdct16x16_c(const int16_t *input, tran_low_t *output, int stride) {
187   // The 2D transform is done with two passes which are actually pretty
188   // similar. In the first one, we transform the columns and transpose
189   // the results. In the second one, we transform the rows. To achieve that,
190   // as the first pass results are transposed, we transpose the columns (that
191   // is the transposed rows) and transpose the results (so that it goes back
192   // in normal/row positions).
193   int pass;
194   // We need an intermediate buffer between passes.
195   tran_low_t intermediate[256];
196   const int16_t *in_pass0 = input;
197   const tran_low_t *in = NULL;
198   tran_low_t *out = intermediate;
199   // Do the two transform/transpose passes
200   for (pass = 0; pass < 2; ++pass) {
201     tran_high_t step1[8];      // canbe16
202     tran_high_t step2[8];      // canbe16
203     tran_high_t step3[8];      // canbe16
204     tran_high_t input[8];      // canbe16
205     tran_high_t temp1, temp2;  // needs32
206     int i;
207     for (i = 0; i < 16; i++) {
208       if (0 == pass) {
209         // Calculate input for the first 8 results.
210         input[0] = (in_pass0[0 * stride] + in_pass0[15 * stride]) * 4;
211         input[1] = (in_pass0[1 * stride] + in_pass0[14 * stride]) * 4;
212         input[2] = (in_pass0[2 * stride] + in_pass0[13 * stride]) * 4;
213         input[3] = (in_pass0[3 * stride] + in_pass0[12 * stride]) * 4;
214         input[4] = (in_pass0[4 * stride] + in_pass0[11 * stride]) * 4;
215         input[5] = (in_pass0[5 * stride] + in_pass0[10 * stride]) * 4;
216         input[6] = (in_pass0[6 * stride] + in_pass0[ 9 * stride]) * 4;
217         input[7] = (in_pass0[7 * stride] + in_pass0[ 8 * stride]) * 4;
218         // Calculate input for the next 8 results.
219         step1[0] = (in_pass0[7 * stride] - in_pass0[ 8 * stride]) * 4;
220         step1[1] = (in_pass0[6 * stride] - in_pass0[ 9 * stride]) * 4;
221         step1[2] = (in_pass0[5 * stride] - in_pass0[10 * stride]) * 4;
222         step1[3] = (in_pass0[4 * stride] - in_pass0[11 * stride]) * 4;
223         step1[4] = (in_pass0[3 * stride] - in_pass0[12 * stride]) * 4;
224         step1[5] = (in_pass0[2 * stride] - in_pass0[13 * stride]) * 4;
225         step1[6] = (in_pass0[1 * stride] - in_pass0[14 * stride]) * 4;
226         step1[7] = (in_pass0[0 * stride] - in_pass0[15 * stride]) * 4;
227       } else {
228         // Calculate input for the first 8 results.
229         input[0] = ((in[0 * 16] + 1) >> 2) + ((in[15 * 16] + 1) >> 2);
230         input[1] = ((in[1 * 16] + 1) >> 2) + ((in[14 * 16] + 1) >> 2);
231         input[2] = ((in[2 * 16] + 1) >> 2) + ((in[13 * 16] + 1) >> 2);
232         input[3] = ((in[3 * 16] + 1) >> 2) + ((in[12 * 16] + 1) >> 2);
233         input[4] = ((in[4 * 16] + 1) >> 2) + ((in[11 * 16] + 1) >> 2);
234         input[5] = ((in[5 * 16] + 1) >> 2) + ((in[10 * 16] + 1) >> 2);
235         input[6] = ((in[6 * 16] + 1) >> 2) + ((in[ 9 * 16] + 1) >> 2);
236         input[7] = ((in[7 * 16] + 1) >> 2) + ((in[ 8 * 16] + 1) >> 2);
237         // Calculate input for the next 8 results.
238         step1[0] = ((in[7 * 16] + 1) >> 2) - ((in[ 8 * 16] + 1) >> 2);
239         step1[1] = ((in[6 * 16] + 1) >> 2) - ((in[ 9 * 16] + 1) >> 2);
240         step1[2] = ((in[5 * 16] + 1) >> 2) - ((in[10 * 16] + 1) >> 2);
241         step1[3] = ((in[4 * 16] + 1) >> 2) - ((in[11 * 16] + 1) >> 2);
242         step1[4] = ((in[3 * 16] + 1) >> 2) - ((in[12 * 16] + 1) >> 2);
243         step1[5] = ((in[2 * 16] + 1) >> 2) - ((in[13 * 16] + 1) >> 2);
244         step1[6] = ((in[1 * 16] + 1) >> 2) - ((in[14 * 16] + 1) >> 2);
245         step1[7] = ((in[0 * 16] + 1) >> 2) - ((in[15 * 16] + 1) >> 2);
246       }
247       // Work on the first eight values; fdct8(input, even_results);
248       {
249         tran_high_t s0, s1, s2, s3, s4, s5, s6, s7;  // canbe16
250         tran_high_t t0, t1, t2, t3;                  // needs32
251         tran_high_t x0, x1, x2, x3;                  // canbe16
252
253         // stage 1
254         s0 = input[0] + input[7];
255         s1 = input[1] + input[6];
256         s2 = input[2] + input[5];
257         s3 = input[3] + input[4];
258         s4 = input[3] - input[4];
259         s5 = input[2] - input[5];
260         s6 = input[1] - input[6];
261         s7 = input[0] - input[7];
262
263         // fdct4(step, step);
264         x0 = s0 + s3;
265         x1 = s1 + s2;
266         x2 = s1 - s2;
267         x3 = s0 - s3;
268         t0 = (x0 + x1) * cospi_16_64;
269         t1 = (x0 - x1) * cospi_16_64;
270         t2 = x3 * cospi_8_64  + x2 * cospi_24_64;
271         t3 = x3 * cospi_24_64 - x2 * cospi_8_64;
272         out[0] = (tran_low_t)fdct_round_shift(t0);
273         out[4] = (tran_low_t)fdct_round_shift(t2);
274         out[8] = (tran_low_t)fdct_round_shift(t1);
275         out[12] = (tran_low_t)fdct_round_shift(t3);
276
277         // Stage 2
278         t0 = (s6 - s5) * cospi_16_64;
279         t1 = (s6 + s5) * cospi_16_64;
280         t2 = fdct_round_shift(t0);
281         t3 = fdct_round_shift(t1);
282
283         // Stage 3
284         x0 = s4 + t2;
285         x1 = s4 - t2;
286         x2 = s7 - t3;
287         x3 = s7 + t3;
288
289         // Stage 4
290         t0 = x0 * cospi_28_64 + x3 *   cospi_4_64;
291         t1 = x1 * cospi_12_64 + x2 *  cospi_20_64;
292         t2 = x2 * cospi_12_64 + x1 * -cospi_20_64;
293         t3 = x3 * cospi_28_64 + x0 *  -cospi_4_64;
294         out[2] = (tran_low_t)fdct_round_shift(t0);
295         out[6] = (tran_low_t)fdct_round_shift(t2);
296         out[10] = (tran_low_t)fdct_round_shift(t1);
297         out[14] = (tran_low_t)fdct_round_shift(t3);
298       }
299       // Work on the next eight values; step1 -> odd_results
300       {
301         // step 2
302         temp1 = (step1[5] - step1[2]) * cospi_16_64;
303         temp2 = (step1[4] - step1[3]) * cospi_16_64;
304         step2[2] = fdct_round_shift(temp1);
305         step2[3] = fdct_round_shift(temp2);
306         temp1 = (step1[4] + step1[3]) * cospi_16_64;
307         temp2 = (step1[5] + step1[2]) * cospi_16_64;
308         step2[4] = fdct_round_shift(temp1);
309         step2[5] = fdct_round_shift(temp2);
310         // step 3
311         step3[0] = step1[0] + step2[3];
312         step3[1] = step1[1] + step2[2];
313         step3[2] = step1[1] - step2[2];
314         step3[3] = step1[0] - step2[3];
315         step3[4] = step1[7] - step2[4];
316         step3[5] = step1[6] - step2[5];
317         step3[6] = step1[6] + step2[5];
318         step3[7] = step1[7] + step2[4];
319         // step 4
320         temp1 = step3[1] *  -cospi_8_64 + step3[6] * cospi_24_64;
321         temp2 = step3[2] * cospi_24_64 + step3[5] *  cospi_8_64;
322         step2[1] = fdct_round_shift(temp1);
323         step2[2] = fdct_round_shift(temp2);
324         temp1 = step3[2] * cospi_8_64 - step3[5] * cospi_24_64;
325         temp2 = step3[1] * cospi_24_64 + step3[6] *  cospi_8_64;
326         step2[5] = fdct_round_shift(temp1);
327         step2[6] = fdct_round_shift(temp2);
328         // step 5
329         step1[0] = step3[0] + step2[1];
330         step1[1] = step3[0] - step2[1];
331         step1[2] = step3[3] + step2[2];
332         step1[3] = step3[3] - step2[2];
333         step1[4] = step3[4] - step2[5];
334         step1[5] = step3[4] + step2[5];
335         step1[6] = step3[7] - step2[6];
336         step1[7] = step3[7] + step2[6];
337         // step 6
338         temp1 = step1[0] * cospi_30_64 + step1[7] *  cospi_2_64;
339         temp2 = step1[1] * cospi_14_64 + step1[6] * cospi_18_64;
340         out[1] = (tran_low_t)fdct_round_shift(temp1);
341         out[9] = (tran_low_t)fdct_round_shift(temp2);
342         temp1 = step1[2] * cospi_22_64 + step1[5] * cospi_10_64;
343         temp2 = step1[3] *  cospi_6_64 + step1[4] * cospi_26_64;
344         out[5] = (tran_low_t)fdct_round_shift(temp1);
345         out[13] = (tran_low_t)fdct_round_shift(temp2);
346         temp1 = step1[3] * -cospi_26_64 + step1[4] *  cospi_6_64;
347         temp2 = step1[2] * -cospi_10_64 + step1[5] * cospi_22_64;
348         out[3] = (tran_low_t)fdct_round_shift(temp1);
349         out[11] = (tran_low_t)fdct_round_shift(temp2);
350         temp1 = step1[1] * -cospi_18_64 + step1[6] * cospi_14_64;
351         temp2 = step1[0] *  -cospi_2_64 + step1[7] * cospi_30_64;
352         out[7] = (tran_low_t)fdct_round_shift(temp1);
353         out[15] = (tran_low_t)fdct_round_shift(temp2);
354       }
355       // Do next column (which is a transposed row in second/horizontal pass)
356       in++;
357       in_pass0++;
358       out += 16;
359     }
360     // Setup in/out for next pass.
361     in = intermediate;
362     out = output;
363   }
364 }
365
366 void vpx_fdct16x16_1_c(const int16_t *input, tran_low_t *output, int stride) {
367   int r, c;
368   int sum = 0;
369   for (r = 0; r < 16; ++r)
370     for (c = 0; c < 16; ++c)
371       sum += input[r * stride + c];
372
373   output[0] = (tran_low_t)(sum >> 1);
374 }
375
376 static INLINE tran_high_t dct_32_round(tran_high_t input) {
377   tran_high_t rv = ROUND_POWER_OF_TWO(input, DCT_CONST_BITS);
378   // TODO(debargha, peter.derivaz): Find new bounds for this assert,
379   // and make the bounds consts.
380   // assert(-131072 <= rv && rv <= 131071);
381   return rv;
382 }
383
384 static INLINE tran_high_t half_round_shift(tran_high_t input) {
385   tran_high_t rv = (input + 1 + (input < 0)) >> 2;
386   return rv;
387 }
388
389 void vpx_fdct32(const tran_high_t *input, tran_high_t *output, int round) {
390   tran_high_t step[32];
391   // Stage 1
392   step[0] = input[0] + input[(32 - 1)];
393   step[1] = input[1] + input[(32 - 2)];
394   step[2] = input[2] + input[(32 - 3)];
395   step[3] = input[3] + input[(32 - 4)];
396   step[4] = input[4] + input[(32 - 5)];
397   step[5] = input[5] + input[(32 - 6)];
398   step[6] = input[6] + input[(32 - 7)];
399   step[7] = input[7] + input[(32 - 8)];
400   step[8] = input[8] + input[(32 - 9)];
401   step[9] = input[9] + input[(32 - 10)];
402   step[10] = input[10] + input[(32 - 11)];
403   step[11] = input[11] + input[(32 - 12)];
404   step[12] = input[12] + input[(32 - 13)];
405   step[13] = input[13] + input[(32 - 14)];
406   step[14] = input[14] + input[(32 - 15)];
407   step[15] = input[15] + input[(32 - 16)];
408   step[16] = -input[16] + input[(32 - 17)];
409   step[17] = -input[17] + input[(32 - 18)];
410   step[18] = -input[18] + input[(32 - 19)];
411   step[19] = -input[19] + input[(32 - 20)];
412   step[20] = -input[20] + input[(32 - 21)];
413   step[21] = -input[21] + input[(32 - 22)];
414   step[22] = -input[22] + input[(32 - 23)];
415   step[23] = -input[23] + input[(32 - 24)];
416   step[24] = -input[24] + input[(32 - 25)];
417   step[25] = -input[25] + input[(32 - 26)];
418   step[26] = -input[26] + input[(32 - 27)];
419   step[27] = -input[27] + input[(32 - 28)];
420   step[28] = -input[28] + input[(32 - 29)];
421   step[29] = -input[29] + input[(32 - 30)];
422   step[30] = -input[30] + input[(32 - 31)];
423   step[31] = -input[31] + input[(32 - 32)];
424
425   // Stage 2
426   output[0] = step[0] + step[16 - 1];
427   output[1] = step[1] + step[16 - 2];
428   output[2] = step[2] + step[16 - 3];
429   output[3] = step[3] + step[16 - 4];
430   output[4] = step[4] + step[16 - 5];
431   output[5] = step[5] + step[16 - 6];
432   output[6] = step[6] + step[16 - 7];
433   output[7] = step[7] + step[16 - 8];
434   output[8] = -step[8] + step[16 - 9];
435   output[9] = -step[9] + step[16 - 10];
436   output[10] = -step[10] + step[16 - 11];
437   output[11] = -step[11] + step[16 - 12];
438   output[12] = -step[12] + step[16 - 13];
439   output[13] = -step[13] + step[16 - 14];
440   output[14] = -step[14] + step[16 - 15];
441   output[15] = -step[15] + step[16 - 16];
442
443   output[16] = step[16];
444   output[17] = step[17];
445   output[18] = step[18];
446   output[19] = step[19];
447
448   output[20] = dct_32_round((-step[20] + step[27]) * cospi_16_64);
449   output[21] = dct_32_round((-step[21] + step[26]) * cospi_16_64);
450   output[22] = dct_32_round((-step[22] + step[25]) * cospi_16_64);
451   output[23] = dct_32_round((-step[23] + step[24]) * cospi_16_64);
452
453   output[24] = dct_32_round((step[24] + step[23]) * cospi_16_64);
454   output[25] = dct_32_round((step[25] + step[22]) * cospi_16_64);
455   output[26] = dct_32_round((step[26] + step[21]) * cospi_16_64);
456   output[27] = dct_32_round((step[27] + step[20]) * cospi_16_64);
457
458   output[28] = step[28];
459   output[29] = step[29];
460   output[30] = step[30];
461   output[31] = step[31];
462
463   // dump the magnitude by 4, hence the intermediate values are within
464   // the range of 16 bits.
465   if (round) {
466     output[0] = half_round_shift(output[0]);
467     output[1] = half_round_shift(output[1]);
468     output[2] = half_round_shift(output[2]);
469     output[3] = half_round_shift(output[3]);
470     output[4] = half_round_shift(output[4]);
471     output[5] = half_round_shift(output[5]);
472     output[6] = half_round_shift(output[6]);
473     output[7] = half_round_shift(output[7]);
474     output[8] = half_round_shift(output[8]);
475     output[9] = half_round_shift(output[9]);
476     output[10] = half_round_shift(output[10]);
477     output[11] = half_round_shift(output[11]);
478     output[12] = half_round_shift(output[12]);
479     output[13] = half_round_shift(output[13]);
480     output[14] = half_round_shift(output[14]);
481     output[15] = half_round_shift(output[15]);
482
483     output[16] = half_round_shift(output[16]);
484     output[17] = half_round_shift(output[17]);
485     output[18] = half_round_shift(output[18]);
486     output[19] = half_round_shift(output[19]);
487     output[20] = half_round_shift(output[20]);
488     output[21] = half_round_shift(output[21]);
489     output[22] = half_round_shift(output[22]);
490     output[23] = half_round_shift(output[23]);
491     output[24] = half_round_shift(output[24]);
492     output[25] = half_round_shift(output[25]);
493     output[26] = half_round_shift(output[26]);
494     output[27] = half_round_shift(output[27]);
495     output[28] = half_round_shift(output[28]);
496     output[29] = half_round_shift(output[29]);
497     output[30] = half_round_shift(output[30]);
498     output[31] = half_round_shift(output[31]);
499   }
500
501   // Stage 3
502   step[0] = output[0] + output[(8 - 1)];
503   step[1] = output[1] + output[(8 - 2)];
504   step[2] = output[2] + output[(8 - 3)];
505   step[3] = output[3] + output[(8 - 4)];
506   step[4] = -output[4] + output[(8 - 5)];
507   step[5] = -output[5] + output[(8 - 6)];
508   step[6] = -output[6] + output[(8 - 7)];
509   step[7] = -output[7] + output[(8 - 8)];
510   step[8] = output[8];
511   step[9] = output[9];
512   step[10] = dct_32_round((-output[10] + output[13]) * cospi_16_64);
513   step[11] = dct_32_round((-output[11] + output[12]) * cospi_16_64);
514   step[12] = dct_32_round((output[12] + output[11]) * cospi_16_64);
515   step[13] = dct_32_round((output[13] + output[10]) * cospi_16_64);
516   step[14] = output[14];
517   step[15] = output[15];
518
519   step[16] = output[16] + output[23];
520   step[17] = output[17] + output[22];
521   step[18] = output[18] + output[21];
522   step[19] = output[19] + output[20];
523   step[20] = -output[20] + output[19];
524   step[21] = -output[21] + output[18];
525   step[22] = -output[22] + output[17];
526   step[23] = -output[23] + output[16];
527   step[24] = -output[24] + output[31];
528   step[25] = -output[25] + output[30];
529   step[26] = -output[26] + output[29];
530   step[27] = -output[27] + output[28];
531   step[28] = output[28] + output[27];
532   step[29] = output[29] + output[26];
533   step[30] = output[30] + output[25];
534   step[31] = output[31] + output[24];
535
536   // Stage 4
537   output[0] = step[0] + step[3];
538   output[1] = step[1] + step[2];
539   output[2] = -step[2] + step[1];
540   output[3] = -step[3] + step[0];
541   output[4] = step[4];
542   output[5] = dct_32_round((-step[5] + step[6]) * cospi_16_64);
543   output[6] = dct_32_round((step[6] + step[5]) * cospi_16_64);
544   output[7] = step[7];
545   output[8] = step[8] + step[11];
546   output[9] = step[9] + step[10];
547   output[10] = -step[10] + step[9];
548   output[11] = -step[11] + step[8];
549   output[12] = -step[12] + step[15];
550   output[13] = -step[13] + step[14];
551   output[14] = step[14] + step[13];
552   output[15] = step[15] + step[12];
553
554   output[16] = step[16];
555   output[17] = step[17];
556   output[18] = dct_32_round(step[18] * -cospi_8_64 + step[29] * cospi_24_64);
557   output[19] = dct_32_round(step[19] * -cospi_8_64 + step[28] * cospi_24_64);
558   output[20] = dct_32_round(step[20] * -cospi_24_64 + step[27] * -cospi_8_64);
559   output[21] = dct_32_round(step[21] * -cospi_24_64 + step[26] * -cospi_8_64);
560   output[22] = step[22];
561   output[23] = step[23];
562   output[24] = step[24];
563   output[25] = step[25];
564   output[26] = dct_32_round(step[26] * cospi_24_64 + step[21] * -cospi_8_64);
565   output[27] = dct_32_round(step[27] * cospi_24_64 + step[20] * -cospi_8_64);
566   output[28] = dct_32_round(step[28] * cospi_8_64 + step[19] * cospi_24_64);
567   output[29] = dct_32_round(step[29] * cospi_8_64 + step[18] * cospi_24_64);
568   output[30] = step[30];
569   output[31] = step[31];
570
571   // Stage 5
572   step[0] = dct_32_round((output[0] + output[1]) * cospi_16_64);
573   step[1] = dct_32_round((-output[1] + output[0]) * cospi_16_64);
574   step[2] = dct_32_round(output[2] * cospi_24_64 + output[3] * cospi_8_64);
575   step[3] = dct_32_round(output[3] * cospi_24_64 - output[2] * cospi_8_64);
576   step[4] = output[4] + output[5];
577   step[5] = -output[5] + output[4];
578   step[6] = -output[6] + output[7];
579   step[7] = output[7] + output[6];
580   step[8] = output[8];
581   step[9] = dct_32_round(output[9] * -cospi_8_64 + output[14] * cospi_24_64);
582   step[10] = dct_32_round(output[10] * -cospi_24_64 + output[13] * -cospi_8_64);
583   step[11] = output[11];
584   step[12] = output[12];
585   step[13] = dct_32_round(output[13] * cospi_24_64 + output[10] * -cospi_8_64);
586   step[14] = dct_32_round(output[14] * cospi_8_64 + output[9] * cospi_24_64);
587   step[15] = output[15];
588
589   step[16] = output[16] + output[19];
590   step[17] = output[17] + output[18];
591   step[18] = -output[18] + output[17];
592   step[19] = -output[19] + output[16];
593   step[20] = -output[20] + output[23];
594   step[21] = -output[21] + output[22];
595   step[22] = output[22] + output[21];
596   step[23] = output[23] + output[20];
597   step[24] = output[24] + output[27];
598   step[25] = output[25] + output[26];
599   step[26] = -output[26] + output[25];
600   step[27] = -output[27] + output[24];
601   step[28] = -output[28] + output[31];
602   step[29] = -output[29] + output[30];
603   step[30] = output[30] + output[29];
604   step[31] = output[31] + output[28];
605
606   // Stage 6
607   output[0] = step[0];
608   output[1] = step[1];
609   output[2] = step[2];
610   output[3] = step[3];
611   output[4] = dct_32_round(step[4] * cospi_28_64 + step[7] * cospi_4_64);
612   output[5] = dct_32_round(step[5] * cospi_12_64 + step[6] * cospi_20_64);
613   output[6] = dct_32_round(step[6] * cospi_12_64 + step[5] * -cospi_20_64);
614   output[7] = dct_32_round(step[7] * cospi_28_64 + step[4] * -cospi_4_64);
615   output[8] = step[8] + step[9];
616   output[9] = -step[9] + step[8];
617   output[10] = -step[10] + step[11];
618   output[11] = step[11] + step[10];
619   output[12] = step[12] + step[13];
620   output[13] = -step[13] + step[12];
621   output[14] = -step[14] + step[15];
622   output[15] = step[15] + step[14];
623
624   output[16] = step[16];
625   output[17] = dct_32_round(step[17] * -cospi_4_64 + step[30] * cospi_28_64);
626   output[18] = dct_32_round(step[18] * -cospi_28_64 + step[29] * -cospi_4_64);
627   output[19] = step[19];
628   output[20] = step[20];
629   output[21] = dct_32_round(step[21] * -cospi_20_64 + step[26] * cospi_12_64);
630   output[22] = dct_32_round(step[22] * -cospi_12_64 + step[25] * -cospi_20_64);
631   output[23] = step[23];
632   output[24] = step[24];
633   output[25] = dct_32_round(step[25] * cospi_12_64 + step[22] * -cospi_20_64);
634   output[26] = dct_32_round(step[26] * cospi_20_64 + step[21] * cospi_12_64);
635   output[27] = step[27];
636   output[28] = step[28];
637   output[29] = dct_32_round(step[29] * cospi_28_64 + step[18] * -cospi_4_64);
638   output[30] = dct_32_round(step[30] * cospi_4_64 + step[17] * cospi_28_64);
639   output[31] = step[31];
640
641   // Stage 7
642   step[0] = output[0];
643   step[1] = output[1];
644   step[2] = output[2];
645   step[3] = output[3];
646   step[4] = output[4];
647   step[5] = output[5];
648   step[6] = output[6];
649   step[7] = output[7];
650   step[8] = dct_32_round(output[8] * cospi_30_64 + output[15] * cospi_2_64);
651   step[9] = dct_32_round(output[9] * cospi_14_64 + output[14] * cospi_18_64);
652   step[10] = dct_32_round(output[10] * cospi_22_64 + output[13] * cospi_10_64);
653   step[11] = dct_32_round(output[11] * cospi_6_64 + output[12] * cospi_26_64);
654   step[12] = dct_32_round(output[12] * cospi_6_64 + output[11] * -cospi_26_64);
655   step[13] = dct_32_round(output[13] * cospi_22_64 + output[10] * -cospi_10_64);
656   step[14] = dct_32_round(output[14] * cospi_14_64 + output[9] * -cospi_18_64);
657   step[15] = dct_32_round(output[15] * cospi_30_64 + output[8] * -cospi_2_64);
658
659   step[16] = output[16] + output[17];
660   step[17] = -output[17] + output[16];
661   step[18] = -output[18] + output[19];
662   step[19] = output[19] + output[18];
663   step[20] = output[20] + output[21];
664   step[21] = -output[21] + output[20];
665   step[22] = -output[22] + output[23];
666   step[23] = output[23] + output[22];
667   step[24] = output[24] + output[25];
668   step[25] = -output[25] + output[24];
669   step[26] = -output[26] + output[27];
670   step[27] = output[27] + output[26];
671   step[28] = output[28] + output[29];
672   step[29] = -output[29] + output[28];
673   step[30] = -output[30] + output[31];
674   step[31] = output[31] + output[30];
675
676   // Final stage --- outputs indices are bit-reversed.
677   output[0]  = step[0];
678   output[16] = step[1];
679   output[8]  = step[2];
680   output[24] = step[3];
681   output[4]  = step[4];
682   output[20] = step[5];
683   output[12] = step[6];
684   output[28] = step[7];
685   output[2]  = step[8];
686   output[18] = step[9];
687   output[10] = step[10];
688   output[26] = step[11];
689   output[6]  = step[12];
690   output[22] = step[13];
691   output[14] = step[14];
692   output[30] = step[15];
693
694   output[1]  = dct_32_round(step[16] * cospi_31_64 + step[31] * cospi_1_64);
695   output[17] = dct_32_round(step[17] * cospi_15_64 + step[30] * cospi_17_64);
696   output[9]  = dct_32_round(step[18] * cospi_23_64 + step[29] * cospi_9_64);
697   output[25] = dct_32_round(step[19] * cospi_7_64 + step[28] * cospi_25_64);
698   output[5]  = dct_32_round(step[20] * cospi_27_64 + step[27] * cospi_5_64);
699   output[21] = dct_32_round(step[21] * cospi_11_64 + step[26] * cospi_21_64);
700   output[13] = dct_32_round(step[22] * cospi_19_64 + step[25] * cospi_13_64);
701   output[29] = dct_32_round(step[23] * cospi_3_64 + step[24] * cospi_29_64);
702   output[3]  = dct_32_round(step[24] * cospi_3_64 + step[23] * -cospi_29_64);
703   output[19] = dct_32_round(step[25] * cospi_19_64 + step[22] * -cospi_13_64);
704   output[11] = dct_32_round(step[26] * cospi_11_64 + step[21] * -cospi_21_64);
705   output[27] = dct_32_round(step[27] * cospi_27_64 + step[20] * -cospi_5_64);
706   output[7]  = dct_32_round(step[28] * cospi_7_64 + step[19] * -cospi_25_64);
707   output[23] = dct_32_round(step[29] * cospi_23_64 + step[18] * -cospi_9_64);
708   output[15] = dct_32_round(step[30] * cospi_15_64 + step[17] * -cospi_17_64);
709   output[31] = dct_32_round(step[31] * cospi_31_64 + step[16] * -cospi_1_64);
710 }
711
712 void vpx_fdct32x32_c(const int16_t *input, tran_low_t *out, int stride) {
713   int i, j;
714   tran_high_t output[32 * 32];
715
716   // Columns
717   for (i = 0; i < 32; ++i) {
718     tran_high_t temp_in[32], temp_out[32];
719     for (j = 0; j < 32; ++j)
720       temp_in[j] = input[j * stride + i] * 4;
721     vpx_fdct32(temp_in, temp_out, 0);
722     for (j = 0; j < 32; ++j)
723       output[j * 32 + i] = (temp_out[j] + 1 + (temp_out[j] > 0)) >> 2;
724   }
725
726   // Rows
727   for (i = 0; i < 32; ++i) {
728     tran_high_t temp_in[32], temp_out[32];
729     for (j = 0; j < 32; ++j)
730       temp_in[j] = output[j + i * 32];
731     vpx_fdct32(temp_in, temp_out, 0);
732     for (j = 0; j < 32; ++j)
733       out[j + i * 32] =
734           (tran_low_t)((temp_out[j] + 1 + (temp_out[j] < 0)) >> 2);
735   }
736 }
737
738 // Note that although we use dct_32_round in dct32 computation flow,
739 // this 2d fdct32x32 for rate-distortion optimization loop is operating
740 // within 16 bits precision.
741 void vpx_fdct32x32_rd_c(const int16_t *input, tran_low_t *out, int stride) {
742   int i, j;
743   tran_high_t output[32 * 32];
744
745   // Columns
746   for (i = 0; i < 32; ++i) {
747     tran_high_t temp_in[32], temp_out[32];
748     for (j = 0; j < 32; ++j)
749       temp_in[j] = input[j * stride + i] * 4;
750     vpx_fdct32(temp_in, temp_out, 0);
751     for (j = 0; j < 32; ++j)
752       // TODO(cd): see quality impact of only doing
753       //           output[j * 32 + i] = (temp_out[j] + 1) >> 2;
754       //           PS: also change code in vpx_dsp/x86/vpx_dct_sse2.c
755       output[j * 32 + i] = (temp_out[j] + 1 + (temp_out[j] > 0)) >> 2;
756   }
757
758   // Rows
759   for (i = 0; i < 32; ++i) {
760     tran_high_t temp_in[32], temp_out[32];
761     for (j = 0; j < 32; ++j)
762       temp_in[j] = output[j + i * 32];
763     vpx_fdct32(temp_in, temp_out, 1);
764     for (j = 0; j < 32; ++j)
765       out[j + i * 32] = (tran_low_t)temp_out[j];
766   }
767 }
768
769 void vpx_fdct32x32_1_c(const int16_t *input, tran_low_t *output, int stride) {
770   int r, c;
771   int sum = 0;
772   for (r = 0; r < 32; ++r)
773     for (c = 0; c < 32; ++c)
774       sum += input[r * stride + c];
775
776   output[0] = (tran_low_t)(sum >> 3);
777 }
778
779 #if CONFIG_VP9_HIGHBITDEPTH
780 void vpx_highbd_fdct4x4_c(const int16_t *input, tran_low_t *output,
781                           int stride) {
782   vpx_fdct4x4_c(input, output, stride);
783 }
784
785 void vpx_highbd_fdct8x8_c(const int16_t *input, tran_low_t *final_output,
786                           int stride) {
787   vpx_fdct8x8_c(input, final_output, stride);
788 }
789
790 void vpx_highbd_fdct8x8_1_c(const int16_t *input, tran_low_t *final_output,
791                             int stride) {
792   vpx_fdct8x8_1_c(input, final_output, stride);
793 }
794
795 void vpx_highbd_fdct16x16_c(const int16_t *input, tran_low_t *output,
796                             int stride) {
797   vpx_fdct16x16_c(input, output, stride);
798 }
799
800 void vpx_highbd_fdct16x16_1_c(const int16_t *input, tran_low_t *output,
801                               int stride) {
802   vpx_fdct16x16_1_c(input, output, stride);
803 }
804
805 void vpx_highbd_fdct32x32_c(const int16_t *input, tran_low_t *out, int stride) {
806   vpx_fdct32x32_c(input, out, stride);
807 }
808
809 void vpx_highbd_fdct32x32_rd_c(const int16_t *input, tran_low_t *out,
810                                int stride) {
811   vpx_fdct32x32_rd_c(input, out, stride);
812 }
813
814 void vpx_highbd_fdct32x32_1_c(const int16_t *input, tran_low_t *out,
815                               int stride) {
816   vpx_fdct32x32_1_c(input, out, stride);
817 }
818 #endif  // CONFIG_VP9_HIGHBITDEPTH