xref: /aosp_15_r20/external/libaom/aom_dsp/fwd_txfm.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
13*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/txfm_common.h"
14*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_dsp_rtcd.h"
15*77c1e3ccSAndroid Build Coastguard Worker 
aom_fdct4x4_c(const int16_t * input,tran_low_t * output,int stride)16*77c1e3ccSAndroid Build Coastguard Worker void aom_fdct4x4_c(const int16_t *input, tran_low_t *output, int stride) {
17*77c1e3ccSAndroid Build Coastguard Worker   // The 2D transform is done with two passes which are actually pretty
18*77c1e3ccSAndroid Build Coastguard Worker   // similar. In the first one, we transform the columns and transpose
19*77c1e3ccSAndroid Build Coastguard Worker   // the results. In the second one, we transform the rows.
20*77c1e3ccSAndroid Build Coastguard Worker   // We need an intermediate buffer between passes.
21*77c1e3ccSAndroid Build Coastguard Worker   tran_low_t intermediate[4 * 4];
22*77c1e3ccSAndroid Build Coastguard Worker   const tran_low_t *in_low = NULL;
23*77c1e3ccSAndroid Build Coastguard Worker   tran_low_t *out = intermediate;
24*77c1e3ccSAndroid Build Coastguard Worker   // Do the two transform passes
25*77c1e3ccSAndroid Build Coastguard Worker   for (int pass = 0; pass < 2; ++pass) {
26*77c1e3ccSAndroid Build Coastguard Worker     tran_high_t in_high[4];  // canbe16
27*77c1e3ccSAndroid Build Coastguard Worker     tran_high_t step[4];     // canbe16
28*77c1e3ccSAndroid Build Coastguard Worker     tran_low_t temp[4];
29*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < 4; ++i) {
30*77c1e3ccSAndroid Build Coastguard Worker       // Load inputs.
31*77c1e3ccSAndroid Build Coastguard Worker       if (pass == 0) {
32*77c1e3ccSAndroid Build Coastguard Worker         in_high[0] = input[0 * stride] * 16;
33*77c1e3ccSAndroid Build Coastguard Worker         in_high[1] = input[1 * stride] * 16;
34*77c1e3ccSAndroid Build Coastguard Worker         in_high[2] = input[2 * stride] * 16;
35*77c1e3ccSAndroid Build Coastguard Worker         in_high[3] = input[3 * stride] * 16;
36*77c1e3ccSAndroid Build Coastguard Worker         if (i == 0 && in_high[0]) {
37*77c1e3ccSAndroid Build Coastguard Worker           ++in_high[0];
38*77c1e3ccSAndroid Build Coastguard Worker         }
39*77c1e3ccSAndroid Build Coastguard Worker         ++input;  // Next column
40*77c1e3ccSAndroid Build Coastguard Worker       } else {
41*77c1e3ccSAndroid Build Coastguard Worker         assert(in_low != NULL);
42*77c1e3ccSAndroid Build Coastguard Worker         in_high[0] = in_low[0 * 4];
43*77c1e3ccSAndroid Build Coastguard Worker         in_high[1] = in_low[1 * 4];
44*77c1e3ccSAndroid Build Coastguard Worker         in_high[2] = in_low[2 * 4];
45*77c1e3ccSAndroid Build Coastguard Worker         in_high[3] = in_low[3 * 4];
46*77c1e3ccSAndroid Build Coastguard Worker         ++in_low;  // Next column (which is a transposed row)
47*77c1e3ccSAndroid Build Coastguard Worker       }
48*77c1e3ccSAndroid Build Coastguard Worker       // Transform.
49*77c1e3ccSAndroid Build Coastguard Worker       step[0] = in_high[0] + in_high[3];
50*77c1e3ccSAndroid Build Coastguard Worker       step[1] = in_high[1] + in_high[2];
51*77c1e3ccSAndroid Build Coastguard Worker       step[2] = in_high[1] - in_high[2];
52*77c1e3ccSAndroid Build Coastguard Worker       step[3] = in_high[0] - in_high[3];
53*77c1e3ccSAndroid Build Coastguard Worker       temp[0] = (tran_low_t)fdct_round_shift((step[0] + step[1]) * cospi_16_64);
54*77c1e3ccSAndroid Build Coastguard Worker       temp[2] = (tran_low_t)fdct_round_shift((step[0] - step[1]) * cospi_16_64);
55*77c1e3ccSAndroid Build Coastguard Worker       temp[1] = (tran_low_t)fdct_round_shift(step[2] * cospi_24_64 +
56*77c1e3ccSAndroid Build Coastguard Worker                                              step[3] * cospi_8_64);
57*77c1e3ccSAndroid Build Coastguard Worker       temp[3] = (tran_low_t)fdct_round_shift(-step[2] * cospi_8_64 +
58*77c1e3ccSAndroid Build Coastguard Worker                                              step[3] * cospi_24_64);
59*77c1e3ccSAndroid Build Coastguard Worker       // Only transpose the first pass.
60*77c1e3ccSAndroid Build Coastguard Worker       if (pass == 0) {
61*77c1e3ccSAndroid Build Coastguard Worker         out[0] = temp[0];
62*77c1e3ccSAndroid Build Coastguard Worker         out[1] = temp[1];
63*77c1e3ccSAndroid Build Coastguard Worker         out[2] = temp[2];
64*77c1e3ccSAndroid Build Coastguard Worker         out[3] = temp[3];
65*77c1e3ccSAndroid Build Coastguard Worker         out += 4;
66*77c1e3ccSAndroid Build Coastguard Worker       } else {
67*77c1e3ccSAndroid Build Coastguard Worker         out[0 * 4] = temp[0];
68*77c1e3ccSAndroid Build Coastguard Worker         out[1 * 4] = temp[1];
69*77c1e3ccSAndroid Build Coastguard Worker         out[2 * 4] = temp[2];
70*77c1e3ccSAndroid Build Coastguard Worker         out[3 * 4] = temp[3];
71*77c1e3ccSAndroid Build Coastguard Worker         ++out;
72*77c1e3ccSAndroid Build Coastguard Worker       }
73*77c1e3ccSAndroid Build Coastguard Worker     }
74*77c1e3ccSAndroid Build Coastguard Worker     // Setup in/out for next pass.
75*77c1e3ccSAndroid Build Coastguard Worker     in_low = intermediate;
76*77c1e3ccSAndroid Build Coastguard Worker     out = output;
77*77c1e3ccSAndroid Build Coastguard Worker   }
78*77c1e3ccSAndroid Build Coastguard Worker 
79*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < 4; ++i) {
80*77c1e3ccSAndroid Build Coastguard Worker     for (int j = 0; j < 4; ++j)
81*77c1e3ccSAndroid Build Coastguard Worker       output[j + i * 4] = (output[j + i * 4] + 1) >> 2;
82*77c1e3ccSAndroid Build Coastguard Worker   }
83*77c1e3ccSAndroid Build Coastguard Worker }
84*77c1e3ccSAndroid Build Coastguard Worker 
aom_fdct4x4_lp_c(const int16_t * input,int16_t * output,int stride)85*77c1e3ccSAndroid Build Coastguard Worker void aom_fdct4x4_lp_c(const int16_t *input, int16_t *output, int stride) {
86*77c1e3ccSAndroid Build Coastguard Worker   // The 2D transform is done with two passes which are actually pretty
87*77c1e3ccSAndroid Build Coastguard Worker   // similar. In the first one, we transform the columns and transpose
88*77c1e3ccSAndroid Build Coastguard Worker   // the results. In the second one, we transform the rows.
89*77c1e3ccSAndroid Build Coastguard Worker   // We need an intermediate buffer between passes.
90*77c1e3ccSAndroid Build Coastguard Worker   int16_t intermediate[4 * 4];
91*77c1e3ccSAndroid Build Coastguard Worker   const int16_t *in_low = NULL;
92*77c1e3ccSAndroid Build Coastguard Worker   int16_t *out = intermediate;
93*77c1e3ccSAndroid Build Coastguard Worker   // Do the two transform passes
94*77c1e3ccSAndroid Build Coastguard Worker   for (int pass = 0; pass < 2; ++pass) {
95*77c1e3ccSAndroid Build Coastguard Worker     int32_t in_high[4];  // canbe16
96*77c1e3ccSAndroid Build Coastguard Worker     int32_t step[4];     // canbe16
97*77c1e3ccSAndroid Build Coastguard Worker     int16_t temp[4];
98*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < 4; ++i) {
99*77c1e3ccSAndroid Build Coastguard Worker       // Load inputs.
100*77c1e3ccSAndroid Build Coastguard Worker       if (pass == 0) {
101*77c1e3ccSAndroid Build Coastguard Worker         in_high[0] = input[0 * stride] * 16;
102*77c1e3ccSAndroid Build Coastguard Worker         in_high[1] = input[1 * stride] * 16;
103*77c1e3ccSAndroid Build Coastguard Worker         in_high[2] = input[2 * stride] * 16;
104*77c1e3ccSAndroid Build Coastguard Worker         in_high[3] = input[3 * stride] * 16;
105*77c1e3ccSAndroid Build Coastguard Worker         ++input;
106*77c1e3ccSAndroid Build Coastguard Worker         if (i == 0 && in_high[0]) {
107*77c1e3ccSAndroid Build Coastguard Worker           ++in_high[0];
108*77c1e3ccSAndroid Build Coastguard Worker         }
109*77c1e3ccSAndroid Build Coastguard Worker       } else {
110*77c1e3ccSAndroid Build Coastguard Worker         assert(in_low != NULL);
111*77c1e3ccSAndroid Build Coastguard Worker         in_high[0] = in_low[0 * 4];
112*77c1e3ccSAndroid Build Coastguard Worker         in_high[1] = in_low[1 * 4];
113*77c1e3ccSAndroid Build Coastguard Worker         in_high[2] = in_low[2 * 4];
114*77c1e3ccSAndroid Build Coastguard Worker         in_high[3] = in_low[3 * 4];
115*77c1e3ccSAndroid Build Coastguard Worker         ++in_low;
116*77c1e3ccSAndroid Build Coastguard Worker       }
117*77c1e3ccSAndroid Build Coastguard Worker       // Transform.
118*77c1e3ccSAndroid Build Coastguard Worker       step[0] = in_high[0] + in_high[3];
119*77c1e3ccSAndroid Build Coastguard Worker       step[1] = in_high[1] + in_high[2];
120*77c1e3ccSAndroid Build Coastguard Worker       step[2] = in_high[1] - in_high[2];
121*77c1e3ccSAndroid Build Coastguard Worker       step[3] = in_high[0] - in_high[3];
122*77c1e3ccSAndroid Build Coastguard Worker       temp[0] = (int16_t)fdct_round_shift((step[0] + step[1]) * cospi_16_64);
123*77c1e3ccSAndroid Build Coastguard Worker       temp[2] = (int16_t)fdct_round_shift((step[0] - step[1]) * cospi_16_64);
124*77c1e3ccSAndroid Build Coastguard Worker       temp[1] = (int16_t)fdct_round_shift(step[2] * cospi_24_64 +
125*77c1e3ccSAndroid Build Coastguard Worker                                           step[3] * cospi_8_64);
126*77c1e3ccSAndroid Build Coastguard Worker       temp[3] = (int16_t)fdct_round_shift(-step[2] * cospi_8_64 +
127*77c1e3ccSAndroid Build Coastguard Worker                                           step[3] * cospi_24_64);
128*77c1e3ccSAndroid Build Coastguard Worker       // Only transpose the first pass.
129*77c1e3ccSAndroid Build Coastguard Worker       if (pass == 0) {
130*77c1e3ccSAndroid Build Coastguard Worker         out[0] = temp[0];
131*77c1e3ccSAndroid Build Coastguard Worker         out[1] = temp[1];
132*77c1e3ccSAndroid Build Coastguard Worker         out[2] = temp[2];
133*77c1e3ccSAndroid Build Coastguard Worker         out[3] = temp[3];
134*77c1e3ccSAndroid Build Coastguard Worker         out += 4;
135*77c1e3ccSAndroid Build Coastguard Worker       } else {
136*77c1e3ccSAndroid Build Coastguard Worker         out[0 * 4] = temp[0];
137*77c1e3ccSAndroid Build Coastguard Worker         out[1 * 4] = temp[1];
138*77c1e3ccSAndroid Build Coastguard Worker         out[2 * 4] = temp[2];
139*77c1e3ccSAndroid Build Coastguard Worker         out[3 * 4] = temp[3];
140*77c1e3ccSAndroid Build Coastguard Worker         ++out;
141*77c1e3ccSAndroid Build Coastguard Worker       }
142*77c1e3ccSAndroid Build Coastguard Worker     }
143*77c1e3ccSAndroid Build Coastguard Worker     // Setup in/out for next pass.
144*77c1e3ccSAndroid Build Coastguard Worker     in_low = intermediate;
145*77c1e3ccSAndroid Build Coastguard Worker     out = output;
146*77c1e3ccSAndroid Build Coastguard Worker   }
147*77c1e3ccSAndroid Build Coastguard Worker 
148*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < 4; ++i) {
149*77c1e3ccSAndroid Build Coastguard Worker     for (int j = 0; j < 4; ++j)
150*77c1e3ccSAndroid Build Coastguard Worker       output[j + i * 4] = (output[j + i * 4] + 1) >> 2;
151*77c1e3ccSAndroid Build Coastguard Worker   }
152*77c1e3ccSAndroid Build Coastguard Worker }
153*77c1e3ccSAndroid Build Coastguard Worker 
154*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_INTERNAL_STATS
aom_fdct8x8_c(const int16_t * input,tran_low_t * final_output,int stride)155*77c1e3ccSAndroid Build Coastguard Worker void aom_fdct8x8_c(const int16_t *input, tran_low_t *final_output, int stride) {
156*77c1e3ccSAndroid Build Coastguard Worker   int i, j;
157*77c1e3ccSAndroid Build Coastguard Worker   tran_low_t intermediate[64];
158*77c1e3ccSAndroid Build Coastguard Worker   int pass;
159*77c1e3ccSAndroid Build Coastguard Worker   tran_low_t *output = intermediate;
160*77c1e3ccSAndroid Build Coastguard Worker   const tran_low_t *in = NULL;
161*77c1e3ccSAndroid Build Coastguard Worker 
162*77c1e3ccSAndroid Build Coastguard Worker   // Transform columns
163*77c1e3ccSAndroid Build Coastguard Worker   for (pass = 0; pass < 2; ++pass) {
164*77c1e3ccSAndroid Build Coastguard Worker     tran_high_t s0, s1, s2, s3, s4, s5, s6, s7;  // canbe16
165*77c1e3ccSAndroid Build Coastguard Worker     tran_high_t t0, t1, t2, t3;                  // needs32
166*77c1e3ccSAndroid Build Coastguard Worker     tran_high_t x0, x1, x2, x3;                  // canbe16
167*77c1e3ccSAndroid Build Coastguard Worker 
168*77c1e3ccSAndroid Build Coastguard Worker     for (i = 0; i < 8; i++) {
169*77c1e3ccSAndroid Build Coastguard Worker       // stage 1
170*77c1e3ccSAndroid Build Coastguard Worker       if (pass == 0) {
171*77c1e3ccSAndroid Build Coastguard Worker         s0 = (input[0 * stride] + input[7 * stride]) * 4;
172*77c1e3ccSAndroid Build Coastguard Worker         s1 = (input[1 * stride] + input[6 * stride]) * 4;
173*77c1e3ccSAndroid Build Coastguard Worker         s2 = (input[2 * stride] + input[5 * stride]) * 4;
174*77c1e3ccSAndroid Build Coastguard Worker         s3 = (input[3 * stride] + input[4 * stride]) * 4;
175*77c1e3ccSAndroid Build Coastguard Worker         s4 = (input[3 * stride] - input[4 * stride]) * 4;
176*77c1e3ccSAndroid Build Coastguard Worker         s5 = (input[2 * stride] - input[5 * stride]) * 4;
177*77c1e3ccSAndroid Build Coastguard Worker         s6 = (input[1 * stride] - input[6 * stride]) * 4;
178*77c1e3ccSAndroid Build Coastguard Worker         s7 = (input[0 * stride] - input[7 * stride]) * 4;
179*77c1e3ccSAndroid Build Coastguard Worker         ++input;
180*77c1e3ccSAndroid Build Coastguard Worker       } else {
181*77c1e3ccSAndroid Build Coastguard Worker         s0 = in[0 * 8] + in[7 * 8];
182*77c1e3ccSAndroid Build Coastguard Worker         s1 = in[1 * 8] + in[6 * 8];
183*77c1e3ccSAndroid Build Coastguard Worker         s2 = in[2 * 8] + in[5 * 8];
184*77c1e3ccSAndroid Build Coastguard Worker         s3 = in[3 * 8] + in[4 * 8];
185*77c1e3ccSAndroid Build Coastguard Worker         s4 = in[3 * 8] - in[4 * 8];
186*77c1e3ccSAndroid Build Coastguard Worker         s5 = in[2 * 8] - in[5 * 8];
187*77c1e3ccSAndroid Build Coastguard Worker         s6 = in[1 * 8] - in[6 * 8];
188*77c1e3ccSAndroid Build Coastguard Worker         s7 = in[0 * 8] - in[7 * 8];
189*77c1e3ccSAndroid Build Coastguard Worker         ++in;
190*77c1e3ccSAndroid Build Coastguard Worker       }
191*77c1e3ccSAndroid Build Coastguard Worker 
192*77c1e3ccSAndroid Build Coastguard Worker       // fdct4(step, step);
193*77c1e3ccSAndroid Build Coastguard Worker       x0 = s0 + s3;
194*77c1e3ccSAndroid Build Coastguard Worker       x1 = s1 + s2;
195*77c1e3ccSAndroid Build Coastguard Worker       x2 = s1 - s2;
196*77c1e3ccSAndroid Build Coastguard Worker       x3 = s0 - s3;
197*77c1e3ccSAndroid Build Coastguard Worker       t0 = (x0 + x1) * cospi_16_64;
198*77c1e3ccSAndroid Build Coastguard Worker       t1 = (x0 - x1) * cospi_16_64;
199*77c1e3ccSAndroid Build Coastguard Worker       t2 = x2 * cospi_24_64 + x3 * cospi_8_64;
200*77c1e3ccSAndroid Build Coastguard Worker       t3 = -x2 * cospi_8_64 + x3 * cospi_24_64;
201*77c1e3ccSAndroid Build Coastguard Worker       output[0] = (tran_low_t)fdct_round_shift(t0);
202*77c1e3ccSAndroid Build Coastguard Worker       output[2] = (tran_low_t)fdct_round_shift(t2);
203*77c1e3ccSAndroid Build Coastguard Worker       output[4] = (tran_low_t)fdct_round_shift(t1);
204*77c1e3ccSAndroid Build Coastguard Worker       output[6] = (tran_low_t)fdct_round_shift(t3);
205*77c1e3ccSAndroid Build Coastguard Worker 
206*77c1e3ccSAndroid Build Coastguard Worker       // Stage 2
207*77c1e3ccSAndroid Build Coastguard Worker       t0 = (s6 - s5) * cospi_16_64;
208*77c1e3ccSAndroid Build Coastguard Worker       t1 = (s6 + s5) * cospi_16_64;
209*77c1e3ccSAndroid Build Coastguard Worker       t2 = fdct_round_shift(t0);
210*77c1e3ccSAndroid Build Coastguard Worker       t3 = fdct_round_shift(t1);
211*77c1e3ccSAndroid Build Coastguard Worker 
212*77c1e3ccSAndroid Build Coastguard Worker       // Stage 3
213*77c1e3ccSAndroid Build Coastguard Worker       x0 = s4 + t2;
214*77c1e3ccSAndroid Build Coastguard Worker       x1 = s4 - t2;
215*77c1e3ccSAndroid Build Coastguard Worker       x2 = s7 - t3;
216*77c1e3ccSAndroid Build Coastguard Worker       x3 = s7 + t3;
217*77c1e3ccSAndroid Build Coastguard Worker 
218*77c1e3ccSAndroid Build Coastguard Worker       // Stage 4
219*77c1e3ccSAndroid Build Coastguard Worker       t0 = x0 * cospi_28_64 + x3 * cospi_4_64;
220*77c1e3ccSAndroid Build Coastguard Worker       t1 = x1 * cospi_12_64 + x2 * cospi_20_64;
221*77c1e3ccSAndroid Build Coastguard Worker       t2 = x2 * cospi_12_64 + x1 * -cospi_20_64;
222*77c1e3ccSAndroid Build Coastguard Worker       t3 = x3 * cospi_28_64 + x0 * -cospi_4_64;
223*77c1e3ccSAndroid Build Coastguard Worker       output[1] = (tran_low_t)fdct_round_shift(t0);
224*77c1e3ccSAndroid Build Coastguard Worker       output[3] = (tran_low_t)fdct_round_shift(t2);
225*77c1e3ccSAndroid Build Coastguard Worker       output[5] = (tran_low_t)fdct_round_shift(t1);
226*77c1e3ccSAndroid Build Coastguard Worker       output[7] = (tran_low_t)fdct_round_shift(t3);
227*77c1e3ccSAndroid Build Coastguard Worker       output += 8;
228*77c1e3ccSAndroid Build Coastguard Worker     }
229*77c1e3ccSAndroid Build Coastguard Worker     in = intermediate;
230*77c1e3ccSAndroid Build Coastguard Worker     output = final_output;
231*77c1e3ccSAndroid Build Coastguard Worker   }
232*77c1e3ccSAndroid Build Coastguard Worker 
233*77c1e3ccSAndroid Build Coastguard Worker   // Rows
234*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < 8; ++i) {
235*77c1e3ccSAndroid Build Coastguard Worker     for (j = 0; j < 8; ++j) final_output[j + i * 8] /= 2;
236*77c1e3ccSAndroid Build Coastguard Worker   }
237*77c1e3ccSAndroid Build Coastguard Worker }
238*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_INTERNAL_STATS
239*77c1e3ccSAndroid Build Coastguard Worker 
240*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH && CONFIG_INTERNAL_STATS
aom_highbd_fdct8x8_c(const int16_t * input,tran_low_t * final_output,int stride)241*77c1e3ccSAndroid Build Coastguard Worker void aom_highbd_fdct8x8_c(const int16_t *input, tran_low_t *final_output,
242*77c1e3ccSAndroid Build Coastguard Worker                           int stride) {
243*77c1e3ccSAndroid Build Coastguard Worker   aom_fdct8x8_c(input, final_output, stride);
244*77c1e3ccSAndroid Build Coastguard Worker }
245*77c1e3ccSAndroid Build Coastguard Worker #endif
246