summaryrefslogtreecommitdiffstats
path: root/third_party/aom/aom_dsp/fft.c
blob: 0ba71cfb3426429f8f6b38698d29ba4f2d43b1b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * Copyright (c) 2018, Alliance for Open Media. All rights reserved
 *
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
 */

#include "aom_dsp/aom_dsp_common.h"
#include "aom_dsp/fft_common.h"

static INLINE void simple_transpose(const float *A, float *B, int n) {
  for (int y = 0; y < n; y++) {
    for (int x = 0; x < n; x++) {
      B[y * n + x] = A[x * n + y];
    }
  }
}

// The 1d transform is real to complex and packs the complex results in
// a way to take advantage of conjugate symmetry (e.g., the n/2 + 1 real
// components, followed by the n/2 - 1 imaginary components). After the
// transform is done on the rows, the first n/2 + 1 columns are real, and
// the remaining are the imaginary components. After the transform on the
// columns, the region of [0, n/2]x[0, n/2] contains the real part of
// fft of the real columns. The real part of the 2d fft also includes the
// imaginary part of transformed imaginary columns. This function assembles
// the correct outputs while putting the real and imaginary components
// next to each other.
static INLINE void unpack_2d_output(const float *col_fft, float *output,
                                    int n) {
  for (int y = 0; y <= n / 2; ++y) {
    const int y2 = y + n / 2;
    const int y_extra = y2 > n / 2 && y2 < n;

    for (int x = 0; x <= n / 2; ++x) {
      const int x2 = x + n / 2;
      const int x_extra = x2 > n / 2 && x2 < n;
      output[2 * (y * n + x)] =
          col_fft[y * n + x] - (x_extra && y_extra ? col_fft[y2 * n + x2] : 0);
      output[2 * (y * n + x) + 1] = (y_extra ? col_fft[y2 * n + x] : 0) +
                                    (x_extra ? col_fft[y * n + x2] : 0);
      if (y_extra) {
        output[2 * ((n - y) * n + x)] =
            col_fft[y * n + x] +
            (x_extra && y_extra ? col_fft[y2 * n + x2] : 0);
        output[2 * ((n - y) * n + x) + 1] =
            -(y_extra ? col_fft[y2 * n + x] : 0) +
            (x_extra ? col_fft[y * n + x2] : 0);
      }
    }
  }
}

void aom_fft_2d_gen(const float *input, float *temp, float *output, int n,
                    aom_fft_1d_func_t tform, aom_fft_transpose_func_t transpose,
                    aom_fft_unpack_func_t unpack, int vec_size) {
  for (int x = 0; x < n; x += vec_size) {
    tform(input + x, output + x, n);
  }
  transpose(output, temp, n);

  for (int x = 0; x < n; x += vec_size) {
    tform(temp + x, output + x, n);
  }
  transpose(output, temp, n);

  unpack(temp, output, n);
}

static INLINE void store_float(float *output, float input) { *output = input; }
static INLINE float add_float(float a, float b) { return a + b; }
static INLINE float sub_float(float a, float b) { return a - b; }
static INLINE float mul_float(float a, float b) { return a * b; }

GEN_FFT_2(void, float, float, float, *, store_float);
GEN_FFT_4(void, float, float, float, *, store_float, (float), add_float,
          sub_float);
GEN_FFT_8(void, float, float, float, *, store_float, (float), add_float,
          sub_float, mul_float);
GEN_FFT_16(void, float, float, float, *, store_float, (float), add_float,
           sub_float, mul_float);
GEN_FFT_32(void, float, float, float, *, store_float, (float), add_float,
           sub_float, mul_float);

void aom_fft2x2_float_c(const float *input, float *temp, float *output) {
  aom_fft_2d_gen(input, temp, output, 2, aom_fft1d_2_float, simple_transpose,
                 unpack_2d_output, 1);
}

void aom_fft4x4_float_c(const float *input, float *temp, float *output) {
  aom_fft_2d_gen(input, temp, output, 4, aom_fft1d_4_float, simple_transpose,
                 unpack_2d_output, 1);
}

void aom_fft8x8_float_c(const float *input, float *temp, float *output) {
  aom_fft_2d_gen(input, temp, output, 8, aom_fft1d_8_float, simple_transpose,
                 unpack_2d_output, 1);
}

void aom_fft16x16_float_c(const float *input, float *temp, float *output) {
  aom_fft_2d_gen(input, temp, output, 16, aom_fft1d_16_float, simple_transpose,
                 unpack_2d_output, 1);
}

void aom_fft32x32_float_c(const float *input, float *temp, float *output) {
  aom_fft_2d_gen(input, temp, output, 32, aom_fft1d_32_float, simple_transpose,
                 unpack_2d_output, 1);
}

void aom_ifft_2d_gen(const float *input, float *temp, float *output, int n,
                     aom_fft_1d_func_t fft_single, aom_fft_1d_func_t fft_multi,
                     aom_fft_1d_func_t ifft_multi,
                     aom_fft_transpose_func_t transpose, int vec_size) {
  // Column 0 and n/2 have conjugate symmetry, so we can directly do the ifft
  // and get real outputs.
  for (int y = 0; y <= n / 2; ++y) {
    output[y * n] = input[2 * y * n];
    output[y * n + 1] = input[2 * (y * n + n / 2)];
  }
  for (int y = n / 2 + 1; y < n; ++y) {
    output[y * n] = input[2 * (y - n / 2) * n + 1];
    output[y * n + 1] = input[2 * ((y - n / 2) * n + n / 2) + 1];
  }

  for (int i = 0; i < 2; i += vec_size) {
    ifft_multi(output + i, temp + i, n);
  }

  // For the other columns, since we don't have a full ifft for complex inputs
  // we have to split them into the real and imaginary counterparts.
  // Pack the real component, then the imaginary components.
  for (int y = 0; y < n; ++y) {
    for (int x = 1; x < n / 2; ++x) {
      output[y * n + (x + 1)] = input[2 * (y * n + x)];
    }
    for (int x = 1; x < n / 2; ++x) {
      output[y * n + (x + n / 2)] = input[2 * (y * n + x) + 1];
    }
  }
  for (int y = 2; y < vec_size; y++) {
    fft_single(output + y, temp + y, n);
  }
  // This is the part that can be sped up with SIMD
  for (int y = AOMMAX(2, vec_size); y < n; y += vec_size) {
    fft_multi(output + y, temp + y, n);
  }

  // Put the 0 and n/2 th results in the correct place.
  for (int x = 0; x < n; ++x) {
    output[x] = temp[x * n];
    output[(n / 2) * n + x] = temp[x * n + 1];
  }
  // This rearranges and transposes.
  for (int y = 1; y < n / 2; ++y) {
    // Fill in the real columns
    for (int x = 0; x <= n / 2; ++x) {
      output[x + y * n] =
          temp[(y + 1) + x * n] +
          ((x > 0 && x < n / 2) ? temp[(y + n / 2) + (x + n / 2) * n] : 0);
    }
    for (int x = n / 2 + 1; x < n; ++x) {
      output[x + y * n] = temp[(y + 1) + (n - x) * n] -
                          temp[(y + n / 2) + ((n - x) + n / 2) * n];
    }
    // Fill in the imag columns
    for (int x = 0; x <= n / 2; ++x) {
      output[x + (y + n / 2) * n] =
          temp[(y + n / 2) + x * n] -
          ((x > 0 && x < n / 2) ? temp[(y + 1) + (x + n / 2) * n] : 0);
    }
    for (int x = n / 2 + 1; x < n; ++x) {
      output[x + (y + n / 2) * n] = temp[(y + 1) + ((n - x) + n / 2) * n] +
                                    temp[(y + n / 2) + (n - x) * n];
    }
  }
  for (int y = 0; y < n; y += vec_size) {
    ifft_multi(output + y, temp + y, n);
  }
  transpose(temp, output, n);
}

GEN_IFFT_2(void, float, float, float, *, store_float);
GEN_IFFT_4(void, float, float, float, *, store_float, (float), add_float,
           sub_float);
GEN_IFFT_8(void, float, float, float, *, store_float, (float), add_float,
           sub_float, mul_float);
GEN_IFFT_16(void, float, float, float, *, store_float, (float), add_float,
            sub_float, mul_float);
GEN_IFFT_32(void, float, float, float, *, store_float, (float), add_float,
            sub_float, mul_float);

void aom_ifft2x2_float_c(const float *input, float *temp, float *output) {
  aom_ifft_2d_gen(input, temp, output, 2, aom_fft1d_2_float, aom_fft1d_2_float,
                  aom_ifft1d_2_float, simple_transpose, 1);
}

void aom_ifft4x4_float_c(const float *input, float *temp, float *output) {
  aom_ifft_2d_gen(input, temp, output, 4, aom_fft1d_4_float, aom_fft1d_4_float,
                  aom_ifft1d_4_float, simple_transpose, 1);
}

void aom_ifft8x8_float_c(const float *input, float *temp, float *output) {
  aom_ifft_2d_gen(input, temp, output, 8, aom_fft1d_8_float, aom_fft1d_8_float,
                  aom_ifft1d_8_float, simple_transpose, 1);
}

void aom_ifft16x16_float_c(const float *input, float *temp, float *output) {
  aom_ifft_2d_gen(input, temp, output, 16, aom_fft1d_16_float,
                  aom_fft1d_16_float, aom_ifft1d_16_float, simple_transpose, 1);
}

void aom_ifft32x32_float_c(const float *input, float *temp, float *output) {
  aom_ifft_2d_gen(input, temp, output, 32, aom_fft1d_32_float,
                  aom_fft1d_32_float, aom_ifft1d_32_float, simple_transpose, 1);
}