FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
imdct15.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014 Mozilla Corporation
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * Celt non-power of 2 iMDCT
24  */
25 
26 #include <float.h>
27 #include <math.h>
28 #include <stddef.h>
29 
30 #include "config.h"
31 
32 #include "libavutil/attributes.h"
33 #include "libavutil/common.h"
34 
35 #include "avfft.h"
36 #include "imdct15.h"
37 #include "opus.h"
38 
39 // minimal iMDCT size to make SIMD opts easier
40 #define CELT_MIN_IMDCT_SIZE 120
41 
42 // complex c = a * b
43 #define CMUL3(cre, cim, are, aim, bre, bim) \
44 do { \
45  cre = are * bre - aim * bim; \
46  cim = are * bim + aim * bre; \
47 } while (0)
48 
49 #define CMUL(c, a, b) CMUL3((c).re, (c).im, (a).re, (a).im, (b).re, (b).im)
50 
51 // complex c = a * b
52 // d = a * conjugate(b)
53 #define CMUL2(c, d, a, b) \
54 do { \
55  float are = (a).re; \
56  float aim = (a).im; \
57  float bre = (b).re; \
58  float bim = (b).im; \
59  float rr = are * bre; \
60  float ri = are * bim; \
61  float ir = aim * bre; \
62  float ii = aim * bim; \
63  (c).re = rr - ii; \
64  (c).im = ri + ir; \
65  (d).re = rr + ii; \
66  (d).im = -ri + ir; \
67 } while (0)
68 
70 {
71  IMDCT15Context *s = *ps;
72  int i;
73 
74  if (!s)
75  return;
76 
77  for (i = 0; i < FF_ARRAY_ELEMS(s->exptab); i++)
78  av_freep(&s->exptab[i]);
79 
81 
82  av_freep(&s->tmp);
83 
84  av_freep(ps);
85 }
86 
87 static void imdct15_half(IMDCT15Context *s, float *dst, const float *src,
88  ptrdiff_t stride, float scale);
89 
91 {
93  int len2 = 15 * (1 << N);
94  int len = 2 * len2;
95  int i, j;
96 
97  if (len2 > CELT_MAX_FRAME_SIZE || len2 < CELT_MIN_IMDCT_SIZE)
98  return AVERROR(EINVAL);
99 
100  s = av_mallocz(sizeof(*s));
101  if (!s)
102  return AVERROR(ENOMEM);
103 
104  s->fft_n = N - 1;
105  s->len4 = len2 / 2;
106  s->len2 = len2;
107 
108  s->tmp = av_malloc_array(len, 2 * sizeof(*s->tmp));
109  if (!s->tmp)
110  goto fail;
111 
112  s->twiddle_exptab = av_malloc_array(s->len4, sizeof(*s->twiddle_exptab));
113  if (!s->twiddle_exptab)
114  goto fail;
115 
116  for (i = 0; i < s->len4; i++) {
117  s->twiddle_exptab[i].re = cos(2 * M_PI * (i + 0.125 + s->len4) / len);
118  s->twiddle_exptab[i].im = sin(2 * M_PI * (i + 0.125 + s->len4) / len);
119  }
120 
121  for (i = 0; i < FF_ARRAY_ELEMS(s->exptab); i++) {
122  int N = 15 * (1 << i);
123  s->exptab[i] = av_malloc(sizeof(*s->exptab[i]) * FFMAX(N, 19));
124  if (!s->exptab[i])
125  goto fail;
126 
127  for (j = 0; j < N; j++) {
128  s->exptab[i][j].re = cos(2 * M_PI * j / N);
129  s->exptab[i][j].im = sin(2 * M_PI * j / N);
130  }
131  }
132 
133  // wrap around to simplify fft15
134  for (j = 15; j < 19; j++)
135  s->exptab[0][j] = s->exptab[0][j - 15];
136 
138 
139  if (ARCH_AARCH64)
141 
142  *ps = s;
143 
144  return 0;
145 
146 fail:
147  ff_imdct15_uninit(&s);
148  return AVERROR(ENOMEM);
149 }
150 
151 static void fft5(FFTComplex *out, const FFTComplex *in, ptrdiff_t stride)
152 {
153  // [0] = exp(2 * i * pi / 5), [1] = exp(2 * i * pi * 2 / 5)
154  static const FFTComplex fact[] = { { 0.30901699437494745, 0.95105651629515353 },
155  { -0.80901699437494734, 0.58778525229247325 } };
156 
157  FFTComplex z[4][4];
158 
159  CMUL2(z[0][0], z[0][3], in[1 * stride], fact[0]);
160  CMUL2(z[0][1], z[0][2], in[1 * stride], fact[1]);
161  CMUL2(z[1][0], z[1][3], in[2 * stride], fact[0]);
162  CMUL2(z[1][1], z[1][2], in[2 * stride], fact[1]);
163  CMUL2(z[2][0], z[2][3], in[3 * stride], fact[0]);
164  CMUL2(z[2][1], z[2][2], in[3 * stride], fact[1]);
165  CMUL2(z[3][0], z[3][3], in[4 * stride], fact[0]);
166  CMUL2(z[3][1], z[3][2], in[4 * stride], fact[1]);
167 
168  out[0].re = in[0].re + in[stride].re + in[2 * stride].re + in[3 * stride].re + in[4 * stride].re;
169  out[0].im = in[0].im + in[stride].im + in[2 * stride].im + in[3 * stride].im + in[4 * stride].im;
170 
171  out[1].re = in[0].re + z[0][0].re + z[1][1].re + z[2][2].re + z[3][3].re;
172  out[1].im = in[0].im + z[0][0].im + z[1][1].im + z[2][2].im + z[3][3].im;
173 
174  out[2].re = in[0].re + z[0][1].re + z[1][3].re + z[2][0].re + z[3][2].re;
175  out[2].im = in[0].im + z[0][1].im + z[1][3].im + z[2][0].im + z[3][2].im;
176 
177  out[3].re = in[0].re + z[0][2].re + z[1][0].re + z[2][3].re + z[3][1].re;
178  out[3].im = in[0].im + z[0][2].im + z[1][0].im + z[2][3].im + z[3][1].im;
179 
180  out[4].re = in[0].re + z[0][3].re + z[1][2].re + z[2][1].re + z[3][0].re;
181  out[4].im = in[0].im + z[0][3].im + z[1][2].im + z[2][1].im + z[3][0].im;
182 }
183 
185  ptrdiff_t stride)
186 {
187  const FFTComplex *exptab = s->exptab[0];
188  FFTComplex tmp[5];
189  FFTComplex tmp1[5];
190  FFTComplex tmp2[5];
191  int k;
192 
193  fft5(tmp, in, stride * 3);
194  fft5(tmp1, in + stride, stride * 3);
195  fft5(tmp2, in + 2 * stride, stride * 3);
196 
197  for (k = 0; k < 5; k++) {
198  FFTComplex t1, t2;
199 
200  CMUL(t1, tmp1[k], exptab[k]);
201  CMUL(t2, tmp2[k], exptab[2 * k]);
202  out[k].re = tmp[k].re + t1.re + t2.re;
203  out[k].im = tmp[k].im + t1.im + t2.im;
204 
205  CMUL(t1, tmp1[k], exptab[k + 5]);
206  CMUL(t2, tmp2[k], exptab[2 * (k + 5)]);
207  out[k + 5].re = tmp[k].re + t1.re + t2.re;
208  out[k + 5].im = tmp[k].im + t1.im + t2.im;
209 
210  CMUL(t1, tmp1[k], exptab[k + 10]);
211  CMUL(t2, tmp2[k], exptab[2 * k + 5]);
212  out[k + 10].re = tmp[k].re + t1.re + t2.re;
213  out[k + 10].im = tmp[k].im + t1.im + t2.im;
214  }
215 }
216 
217 /*
218  * FFT of the length 15 * (2^N)
219  */
221  int N, ptrdiff_t stride)
222 {
223  if (N) {
224  const FFTComplex *exptab = s->exptab[N];
225  const int len2 = 15 * (1 << (N - 1));
226  int k;
227 
228  fft_calc(s, out, in, N - 1, stride * 2);
229  fft_calc(s, out + len2, in + stride, N - 1, stride * 2);
230 
231  for (k = 0; k < len2; k++) {
232  FFTComplex t;
233 
234  CMUL(t, out[len2 + k], exptab[k]);
235 
236  out[len2 + k].re = out[k].re - t.re;
237  out[len2 + k].im = out[k].im - t.im;
238 
239  out[k].re += t.re;
240  out[k].im += t.im;
241  }
242  } else
243  fft15(s, out, in, stride);
244 }
245 
246 static void imdct15_half(IMDCT15Context *s, float *dst, const float *src,
247  ptrdiff_t stride, float scale)
248 {
249  FFTComplex *z = (FFTComplex *)dst;
250  const int len8 = s->len4 / 2;
251  const float *in1 = src;
252  const float *in2 = src + (s->len2 - 1) * stride;
253  int i;
254 
255  for (i = 0; i < s->len4; i++) {
256  FFTComplex tmp = { *in2, *in1 };
257  CMUL(s->tmp[i], tmp, s->twiddle_exptab[i]);
258  in1 += 2 * stride;
259  in2 -= 2 * stride;
260  }
261 
262  fft_calc(s, z, s->tmp, s->fft_n, 1);
263 
264  for (i = 0; i < len8; i++) {
265  float r0, i0, r1, i1;
266 
267  CMUL3(r0, i1, z[len8 - i - 1].im, z[len8 - i - 1].re, s->twiddle_exptab[len8 - i - 1].im, s->twiddle_exptab[len8 - i - 1].re);
268  CMUL3(r1, i0, z[len8 + i].im, z[len8 + i].re, s->twiddle_exptab[len8 + i].im, s->twiddle_exptab[len8 + i].re);
269  z[len8 - i - 1].re = scale * r0;
270  z[len8 - i - 1].im = scale * i0;
271  z[len8 + i].re = scale * r1;
272  z[len8 + i].im = scale * i1;
273  }
274 }