19a19cd78SMatthias Ringwald /****************************************************************************** 29a19cd78SMatthias Ringwald * 34930cef6SMatthias Ringwald * Copyright 2022 Google LLC 49a19cd78SMatthias Ringwald * 59a19cd78SMatthias Ringwald * Licensed under the Apache License, Version 2.0 (the "License"); 69a19cd78SMatthias Ringwald * you may not use this file except in compliance with the License. 79a19cd78SMatthias Ringwald * You may obtain a copy of the License at: 89a19cd78SMatthias Ringwald * 99a19cd78SMatthias Ringwald * http://www.apache.org/licenses/LICENSE-2.0 109a19cd78SMatthias Ringwald * 119a19cd78SMatthias Ringwald * Unless required by applicable law or agreed to in writing, software 129a19cd78SMatthias Ringwald * distributed under the License is distributed on an "AS IS" BASIS, 139a19cd78SMatthias Ringwald * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 149a19cd78SMatthias Ringwald * See the License for the specific language governing permissions and 159a19cd78SMatthias Ringwald * limitations under the License. 169a19cd78SMatthias Ringwald * 179a19cd78SMatthias Ringwald ******************************************************************************/ 189a19cd78SMatthias Ringwald 19*4c4eb519SMatthias Ringwald #include "mdct.h" 209a19cd78SMatthias Ringwald #include "tables.h" 219a19cd78SMatthias Ringwald 224930cef6SMatthias Ringwald #include "mdct_neon.h" 234930cef6SMatthias Ringwald 249a19cd78SMatthias Ringwald 259a19cd78SMatthias Ringwald /* ---------------------------------------------------------------------------- 269a19cd78SMatthias Ringwald * FFT processing 279a19cd78SMatthias Ringwald * -------------------------------------------------------------------------- */ 289a19cd78SMatthias Ringwald 299a19cd78SMatthias Ringwald /** 304930cef6SMatthias Ringwald * FFT 5 Points 319a19cd78SMatthias Ringwald * x, y Input and output coefficients, of size 5xn 324930cef6SMatthias Ringwald * n Number of interleaved transform to perform (n % 2 = 0) 339a19cd78SMatthias Ringwald */ 344930cef6SMatthias Ringwald #ifndef fft_5 354930cef6SMatthias Ringwald LC3_HOT static inline void fft_5( 369a19cd78SMatthias Ringwald const struct lc3_complex *x, struct lc3_complex *y, int n) 379a19cd78SMatthias Ringwald { 389a19cd78SMatthias Ringwald static const float cos1 = 0.3090169944; /* cos(-2Pi 1/5) */ 399a19cd78SMatthias Ringwald static const float cos2 = -0.8090169944; /* cos(-2Pi 2/5) */ 409a19cd78SMatthias Ringwald 419a19cd78SMatthias Ringwald static const float sin1 = -0.9510565163; /* sin(-2Pi 1/5) */ 429a19cd78SMatthias Ringwald static const float sin2 = -0.5877852523; /* sin(-2Pi 2/5) */ 439a19cd78SMatthias Ringwald 449a19cd78SMatthias Ringwald for (int i = 0; i < n; i++, x++, y+= 5) { 459a19cd78SMatthias Ringwald 469a19cd78SMatthias Ringwald struct lc3_complex s14 = 479a19cd78SMatthias Ringwald { x[1*n].re + x[4*n].re, x[1*n].im + x[4*n].im }; 489a19cd78SMatthias Ringwald struct lc3_complex d14 = 499a19cd78SMatthias Ringwald { x[1*n].re - x[4*n].re, x[1*n].im - x[4*n].im }; 509a19cd78SMatthias Ringwald 519a19cd78SMatthias Ringwald struct lc3_complex s23 = 529a19cd78SMatthias Ringwald { x[2*n].re + x[3*n].re, x[2*n].im + x[3*n].im }; 539a19cd78SMatthias Ringwald struct lc3_complex d23 = 549a19cd78SMatthias Ringwald { x[2*n].re - x[3*n].re, x[2*n].im - x[3*n].im }; 559a19cd78SMatthias Ringwald 569a19cd78SMatthias Ringwald y[0].re = x[0].re + s14.re + s23.re; 574930cef6SMatthias Ringwald 589a19cd78SMatthias Ringwald y[0].im = x[0].im + s14.im + s23.im; 599a19cd78SMatthias Ringwald 604930cef6SMatthias Ringwald y[1].re = x[0].re + s14.re * cos1 - d14.im * sin1 614930cef6SMatthias Ringwald + s23.re * cos2 - d23.im * sin2; 629a19cd78SMatthias Ringwald 634930cef6SMatthias Ringwald y[1].im = x[0].im + s14.im * cos1 + d14.re * sin1 644930cef6SMatthias Ringwald + s23.im * cos2 + d23.re * sin2; 659a19cd78SMatthias Ringwald 664930cef6SMatthias Ringwald y[2].re = x[0].re + s14.re * cos2 - d14.im * sin2 674930cef6SMatthias Ringwald + s23.re * cos1 + d23.im * sin1; 689a19cd78SMatthias Ringwald 694930cef6SMatthias Ringwald y[2].im = x[0].im + s14.im * cos2 + d14.re * sin2 704930cef6SMatthias Ringwald + s23.im * cos1 - d23.re * sin1; 719a19cd78SMatthias Ringwald 724930cef6SMatthias Ringwald y[3].re = x[0].re + s14.re * cos2 + d14.im * sin2 734930cef6SMatthias Ringwald + s23.re * cos1 - d23.im * sin1; 749a19cd78SMatthias Ringwald 754930cef6SMatthias Ringwald y[3].im = x[0].im + s14.im * cos2 - d14.re * sin2 764930cef6SMatthias Ringwald + s23.im * cos1 + d23.re * sin1; 779a19cd78SMatthias Ringwald 784930cef6SMatthias Ringwald y[4].re = x[0].re + s14.re * cos1 + d14.im * sin1 794930cef6SMatthias Ringwald + s23.re * cos2 + d23.im * sin2; 809a19cd78SMatthias Ringwald 814930cef6SMatthias Ringwald y[4].im = x[0].im + s14.im * cos1 - d14.re * sin1 824930cef6SMatthias Ringwald + s23.im * cos2 - d23.re * sin2; 839a19cd78SMatthias Ringwald } 849a19cd78SMatthias Ringwald } 854930cef6SMatthias Ringwald #endif /* fft_5 */ 869a19cd78SMatthias Ringwald 879a19cd78SMatthias Ringwald /** 884930cef6SMatthias Ringwald * FFT Butterfly 3 Points 899a19cd78SMatthias Ringwald * x, y Input and output coefficients 909a19cd78SMatthias Ringwald * twiddles Twiddles factors, determine size of transform 919a19cd78SMatthias Ringwald * n Number of interleaved transforms 929a19cd78SMatthias Ringwald */ 934930cef6SMatthias Ringwald #ifndef fft_bf3 944930cef6SMatthias Ringwald LC3_HOT static inline void fft_bf3( 954930cef6SMatthias Ringwald const struct lc3_fft_bf3_twiddles *twiddles, 969a19cd78SMatthias Ringwald const struct lc3_complex *x, struct lc3_complex *y, int n) 979a19cd78SMatthias Ringwald { 989a19cd78SMatthias Ringwald int n3 = twiddles->n3; 999a19cd78SMatthias Ringwald const struct lc3_complex (*w0)[2] = twiddles->t; 1009a19cd78SMatthias Ringwald const struct lc3_complex (*w1)[2] = w0 + n3, (*w2)[2] = w1 + n3; 1019a19cd78SMatthias Ringwald 1029a19cd78SMatthias Ringwald const struct lc3_complex *x0 = x, *x1 = x0 + n*n3, *x2 = x1 + n*n3; 1039a19cd78SMatthias Ringwald struct lc3_complex *y0 = y, *y1 = y0 + n3, *y2 = y1 + n3; 1049a19cd78SMatthias Ringwald 1054930cef6SMatthias Ringwald for (int i = 0; i < n; i++, y0 += 3*n3, y1 += 3*n3, y2 += 3*n3) 1069a19cd78SMatthias Ringwald for (int j = 0; j < n3; j++, x0++, x1++, x2++) { 1079a19cd78SMatthias Ringwald 1084930cef6SMatthias Ringwald y0[j].re = x0->re + x1->re * w0[j][0].re - x1->im * w0[j][0].im 1094930cef6SMatthias Ringwald + x2->re * w0[j][1].re - x2->im * w0[j][1].im; 1109a19cd78SMatthias Ringwald 1114930cef6SMatthias Ringwald y0[j].im = x0->im + x1->im * w0[j][0].re + x1->re * w0[j][0].im 1124930cef6SMatthias Ringwald + x2->im * w0[j][1].re + x2->re * w0[j][1].im; 1139a19cd78SMatthias Ringwald 1144930cef6SMatthias Ringwald y1[j].re = x0->re + x1->re * w1[j][0].re - x1->im * w1[j][0].im 1154930cef6SMatthias Ringwald + x2->re * w1[j][1].re - x2->im * w1[j][1].im; 1169a19cd78SMatthias Ringwald 1174930cef6SMatthias Ringwald y1[j].im = x0->im + x1->im * w1[j][0].re + x1->re * w1[j][0].im 1184930cef6SMatthias Ringwald + x2->im * w1[j][1].re + x2->re * w1[j][1].im; 1199a19cd78SMatthias Ringwald 1204930cef6SMatthias Ringwald y2[j].re = x0->re + x1->re * w2[j][0].re - x1->im * w2[j][0].im 1214930cef6SMatthias Ringwald + x2->re * w2[j][1].re - x2->im * w2[j][1].im; 1229a19cd78SMatthias Ringwald 1234930cef6SMatthias Ringwald y2[j].im = x0->im + x1->im * w2[j][0].re + x1->re * w2[j][0].im 1244930cef6SMatthias Ringwald + x2->im * w2[j][1].re + x2->re * w2[j][1].im; 1259a19cd78SMatthias Ringwald } 1269a19cd78SMatthias Ringwald } 1274930cef6SMatthias Ringwald #endif /* fft_bf3 */ 1289a19cd78SMatthias Ringwald 1299a19cd78SMatthias Ringwald /** 1304930cef6SMatthias Ringwald * FFT Butterfly 2 Points 1319a19cd78SMatthias Ringwald * twiddles Twiddles factors, determine size of transform 1329a19cd78SMatthias Ringwald * x, y Input and output coefficients 1339a19cd78SMatthias Ringwald * n Number of interleaved transforms 1349a19cd78SMatthias Ringwald */ 1354930cef6SMatthias Ringwald #ifndef fft_bf2 1364930cef6SMatthias Ringwald LC3_HOT static inline void fft_bf2( 1374930cef6SMatthias Ringwald const struct lc3_fft_bf2_twiddles *twiddles, 1389a19cd78SMatthias Ringwald const struct lc3_complex *x, struct lc3_complex *y, int n) 1399a19cd78SMatthias Ringwald { 1409a19cd78SMatthias Ringwald int n2 = twiddles->n2; 1419a19cd78SMatthias Ringwald const struct lc3_complex *w = twiddles->t; 1429a19cd78SMatthias Ringwald 1439a19cd78SMatthias Ringwald const struct lc3_complex *x0 = x, *x1 = x0 + n*n2; 1449a19cd78SMatthias Ringwald struct lc3_complex *y0 = y, *y1 = y0 + n2; 1459a19cd78SMatthias Ringwald 1469a19cd78SMatthias Ringwald for (int i = 0; i < n; i++, y0 += 2*n2, y1 += 2*n2) { 1479a19cd78SMatthias Ringwald 1489a19cd78SMatthias Ringwald for (int j = 0; j < n2; j++, x0++, x1++) { 1499a19cd78SMatthias Ringwald 1504930cef6SMatthias Ringwald y0[j].re = x0->re + x1->re * w[j].re - x1->im * w[j].im; 1514930cef6SMatthias Ringwald y0[j].im = x0->im + x1->im * w[j].re + x1->re * w[j].im; 1529a19cd78SMatthias Ringwald 1534930cef6SMatthias Ringwald y1[j].re = x0->re - x1->re * w[j].re + x1->im * w[j].im; 1544930cef6SMatthias Ringwald y1[j].im = x0->im - x1->im * w[j].re - x1->re * w[j].im; 1559a19cd78SMatthias Ringwald } 1569a19cd78SMatthias Ringwald } 1579a19cd78SMatthias Ringwald } 1584930cef6SMatthias Ringwald #endif /* fft_bf2 */ 1599a19cd78SMatthias Ringwald 1609a19cd78SMatthias Ringwald /** 1619a19cd78SMatthias Ringwald * Perform FFT 1629a19cd78SMatthias Ringwald * x, y0, y1 Input, and 2 scratch buffers of size `n` 1639a19cd78SMatthias Ringwald * n Number of points 30, 40, 60, 80, 90, 120, 160, 180, 240 1649a19cd78SMatthias Ringwald * return The buffer `y0` or `y1` that hold the result 1659a19cd78SMatthias Ringwald * 1669a19cd78SMatthias Ringwald * Input `x` can be the same as the `y0` second scratch buffer 1679a19cd78SMatthias Ringwald */ 1684930cef6SMatthias Ringwald static struct lc3_complex *fft(const struct lc3_complex *x, int n, 1699a19cd78SMatthias Ringwald struct lc3_complex *y0, struct lc3_complex *y1) 1709a19cd78SMatthias Ringwald { 1719a19cd78SMatthias Ringwald struct lc3_complex *y[2] = { y1, y0 }; 1729a19cd78SMatthias Ringwald int i2, i3, is = 0; 1739a19cd78SMatthias Ringwald 1749a19cd78SMatthias Ringwald /* The number of points `n` can be decomposed as : 1759a19cd78SMatthias Ringwald * 1769a19cd78SMatthias Ringwald * n = 5^1 * 3^n3 * 2^n2 1779a19cd78SMatthias Ringwald * 1789a19cd78SMatthias Ringwald * for n = 40, 80, 160 n3 = 0, n2 = [3..5] 1799a19cd78SMatthias Ringwald * n = 30, 60, 120, 240 n3 = 1, n2 = [1..4] 1809a19cd78SMatthias Ringwald * n = 90, 180 n3 = 2, n2 = [1..2] 1819a19cd78SMatthias Ringwald * 1829a19cd78SMatthias Ringwald * Note that the expression `n & (n-1) == 0` is equivalent 1839a19cd78SMatthias Ringwald * to the check that `n` is a power of 2. */ 1849a19cd78SMatthias Ringwald 1854930cef6SMatthias Ringwald fft_5(x, y[is], n /= 5); 1869a19cd78SMatthias Ringwald 1879a19cd78SMatthias Ringwald for (i3 = 0; n & (n-1); i3++, is ^= 1) 1884930cef6SMatthias Ringwald fft_bf3(lc3_fft_twiddles_bf3[i3], y[is], y[is ^ 1], n /= 3); 1899a19cd78SMatthias Ringwald 1909a19cd78SMatthias Ringwald for (i2 = 0; n > 1; i2++, is ^= 1) 1914930cef6SMatthias Ringwald fft_bf2(lc3_fft_twiddles_bf2[i2][i3], y[is], y[is ^ 1], n >>= 1); 1929a19cd78SMatthias Ringwald 1939a19cd78SMatthias Ringwald return y[is]; 1949a19cd78SMatthias Ringwald } 1959a19cd78SMatthias Ringwald 1969a19cd78SMatthias Ringwald 1979a19cd78SMatthias Ringwald /* ---------------------------------------------------------------------------- 1989a19cd78SMatthias Ringwald * MDCT processing 1999a19cd78SMatthias Ringwald * -------------------------------------------------------------------------- */ 2009a19cd78SMatthias Ringwald 2019a19cd78SMatthias Ringwald /** 2029a19cd78SMatthias Ringwald * Windowing of samples before MDCT 2039a19cd78SMatthias Ringwald * dt, sr Duration and samplerate (size of the transform) 2044930cef6SMatthias Ringwald * x, y Input current and delayed samples 2054930cef6SMatthias Ringwald * y, d Output windowed samples, and delayed ones 2069a19cd78SMatthias Ringwald */ 2074930cef6SMatthias Ringwald LC3_HOT static void mdct_window(enum lc3_dt dt, enum lc3_srate sr, 2084930cef6SMatthias Ringwald const float *x, float *d, float *y) 2099a19cd78SMatthias Ringwald { 2109a19cd78SMatthias Ringwald int ns = LC3_NS(dt, sr), nd = LC3_ND(dt, sr); 2119a19cd78SMatthias Ringwald 2129a19cd78SMatthias Ringwald const float *w0 = lc3_mdct_win[dt][sr], *w1 = w0 + ns; 2139a19cd78SMatthias Ringwald const float *w2 = w1, *w3 = w2 + nd; 2149a19cd78SMatthias Ringwald 2154930cef6SMatthias Ringwald const float *x0 = x + ns-nd, *x1 = x0; 2169a19cd78SMatthias Ringwald float *y0 = y + ns/2, *y1 = y0; 2174930cef6SMatthias Ringwald float *d0 = d, *d1 = d + nd; 2189a19cd78SMatthias Ringwald 2194930cef6SMatthias Ringwald while (x1 > x) { 2204930cef6SMatthias Ringwald *(--y0) = *d0 * *(w0++) - *(--x1) * *(--w1); 2214930cef6SMatthias Ringwald *(y1++) = (*(d0++) = *(x0++)) * *(w2++); 2229a19cd78SMatthias Ringwald 2234930cef6SMatthias Ringwald *(--y0) = *d0 * *(w0++) - *(--x1) * *(--w1); 2244930cef6SMatthias Ringwald *(y1++) = (*(d0++) = *(x0++)) * *(w2++); 2254930cef6SMatthias Ringwald } 2269a19cd78SMatthias Ringwald 2274930cef6SMatthias Ringwald for (x1 += ns; x0 < x1; ) { 2284930cef6SMatthias Ringwald *(--y0) = *d0 * *(w0++) - *(--d1) * *(--w1); 2294930cef6SMatthias Ringwald *(y1++) = (*(d0++) = *(x0++)) * *(w2++) + (*d1 = *(--x1)) * *(--w3); 2304930cef6SMatthias Ringwald 2314930cef6SMatthias Ringwald *(--y0) = *d0 * *(w0++) - *(--d1) * *(--w1); 2324930cef6SMatthias Ringwald *(y1++) = (*(d0++) = *(x0++)) * *(w2++) + (*d1 = *(--x1)) * *(--w3); 2334930cef6SMatthias Ringwald } 2349a19cd78SMatthias Ringwald } 2359a19cd78SMatthias Ringwald 2369a19cd78SMatthias Ringwald /** 2379a19cd78SMatthias Ringwald * Pre-rotate MDCT coefficients of N/2 points, before FFT N/4 points FFT 2389a19cd78SMatthias Ringwald * def Size and twiddles factors 2399a19cd78SMatthias Ringwald * x, y Input and output coefficients 2409a19cd78SMatthias Ringwald * 2419a19cd78SMatthias Ringwald * `x` and y` can be the same buffer 2429a19cd78SMatthias Ringwald */ 2434930cef6SMatthias Ringwald LC3_HOT static void mdct_pre_fft(const struct lc3_mdct_rot_def *def, 2449a19cd78SMatthias Ringwald const float *x, struct lc3_complex *y) 2459a19cd78SMatthias Ringwald { 2469a19cd78SMatthias Ringwald int n4 = def->n4; 2479a19cd78SMatthias Ringwald 2489a19cd78SMatthias Ringwald const float *x0 = x, *x1 = x0 + 2*n4; 2499a19cd78SMatthias Ringwald const struct lc3_complex *w0 = def->w, *w1 = w0 + n4; 2509a19cd78SMatthias Ringwald struct lc3_complex *y0 = y, *y1 = y0 + n4; 2519a19cd78SMatthias Ringwald 2529a19cd78SMatthias Ringwald while (x0 < x1) { 2539a19cd78SMatthias Ringwald struct lc3_complex u, uw = *(w0++); 2549a19cd78SMatthias Ringwald u.re = - *(--x1) * uw.re + *x0 * uw.im; 2559a19cd78SMatthias Ringwald u.im = *(x0++) * uw.re + *x1 * uw.im; 2569a19cd78SMatthias Ringwald 2579a19cd78SMatthias Ringwald struct lc3_complex v, vw = *(--w1); 2589a19cd78SMatthias Ringwald v.re = - *(--x1) * vw.im + *x0 * vw.re; 2599a19cd78SMatthias Ringwald v.im = - *(x0++) * vw.im - *x1 * vw.re; 2609a19cd78SMatthias Ringwald 2619a19cd78SMatthias Ringwald *(y0++) = u; 2629a19cd78SMatthias Ringwald *(--y1) = v; 2639a19cd78SMatthias Ringwald } 2649a19cd78SMatthias Ringwald } 2659a19cd78SMatthias Ringwald 2669a19cd78SMatthias Ringwald /** 2679a19cd78SMatthias Ringwald * Post-rotate FFT N/4 points coefficients, resulting MDCT N points 2689a19cd78SMatthias Ringwald * def Size and twiddles factors 2699a19cd78SMatthias Ringwald * x, y Input and output coefficients 2709a19cd78SMatthias Ringwald * scale Scale on output coefficients 2719a19cd78SMatthias Ringwald * 2729a19cd78SMatthias Ringwald * `x` and y` can be the same buffer 2739a19cd78SMatthias Ringwald */ 2744930cef6SMatthias Ringwald LC3_HOT static void mdct_post_fft(const struct lc3_mdct_rot_def *def, 2759a19cd78SMatthias Ringwald const struct lc3_complex *x, float *y, float scale) 2769a19cd78SMatthias Ringwald { 2779a19cd78SMatthias Ringwald int n4 = def->n4, n8 = n4 >> 1; 2789a19cd78SMatthias Ringwald 2799a19cd78SMatthias Ringwald const struct lc3_complex *w0 = def->w + n8, *w1 = w0 - 1; 2809a19cd78SMatthias Ringwald const struct lc3_complex *x0 = x + n8, *x1 = x0 - 1; 2819a19cd78SMatthias Ringwald 2829a19cd78SMatthias Ringwald float *y0 = y + n4, *y1 = y0; 2839a19cd78SMatthias Ringwald 2849a19cd78SMatthias Ringwald for ( ; y1 > y; x0++, x1--, w0++, w1--) { 2859a19cd78SMatthias Ringwald 2869a19cd78SMatthias Ringwald float u0 = (x0->im * w0->im + x0->re * w0->re) * scale; 2879a19cd78SMatthias Ringwald float u1 = (x1->re * w1->im - x1->im * w1->re) * scale; 2889a19cd78SMatthias Ringwald 2899a19cd78SMatthias Ringwald float v0 = (x0->re * w0->im - x0->im * w0->re) * scale; 2909a19cd78SMatthias Ringwald float v1 = (x1->im * w1->im + x1->re * w1->re) * scale; 2919a19cd78SMatthias Ringwald 2929a19cd78SMatthias Ringwald *(y0++) = u0; *(y0++) = u1; 2939a19cd78SMatthias Ringwald *(--y1) = v0; *(--y1) = v1; 2949a19cd78SMatthias Ringwald } 2959a19cd78SMatthias Ringwald } 2969a19cd78SMatthias Ringwald 2979a19cd78SMatthias Ringwald /** 2989a19cd78SMatthias Ringwald * Pre-rotate IMDCT coefficients of N points, before FFT N/4 points FFT 2999a19cd78SMatthias Ringwald * def Size and twiddles factors 3009a19cd78SMatthias Ringwald * x, y Input and output coefficients 3019a19cd78SMatthias Ringwald * 3024930cef6SMatthias Ringwald * `x` and `y` can be the same buffer 3034930cef6SMatthias Ringwald * The real and imaginary parts of `y` are swapped, 3044930cef6SMatthias Ringwald * to operate on FFT instead of IFFT 3059a19cd78SMatthias Ringwald */ 3064930cef6SMatthias Ringwald LC3_HOT static void imdct_pre_fft(const struct lc3_mdct_rot_def *def, 3079a19cd78SMatthias Ringwald const float *x, struct lc3_complex *y) 3089a19cd78SMatthias Ringwald { 3099a19cd78SMatthias Ringwald int n4 = def->n4; 3109a19cd78SMatthias Ringwald 3119a19cd78SMatthias Ringwald const float *x0 = x, *x1 = x0 + 2*n4; 3129a19cd78SMatthias Ringwald 3139a19cd78SMatthias Ringwald const struct lc3_complex *w0 = def->w, *w1 = w0 + n4; 3149a19cd78SMatthias Ringwald struct lc3_complex *y0 = y, *y1 = y0 + n4; 3159a19cd78SMatthias Ringwald 3169a19cd78SMatthias Ringwald while (x0 < x1) { 3179a19cd78SMatthias Ringwald float u0 = *(x0++), u1 = *(--x1); 3189a19cd78SMatthias Ringwald float v0 = *(x0++), v1 = *(--x1); 3199a19cd78SMatthias Ringwald struct lc3_complex uw = *(w0++), vw = *(--w1); 3209a19cd78SMatthias Ringwald 3214930cef6SMatthias Ringwald (y0 )->re = - u0 * uw.re - u1 * uw.im; 3224930cef6SMatthias Ringwald (y0++)->im = - u1 * uw.re + u0 * uw.im; 3239a19cd78SMatthias Ringwald 3244930cef6SMatthias Ringwald (--y1)->re = - v1 * vw.re - v0 * vw.im; 3254930cef6SMatthias Ringwald ( y1)->im = - v0 * vw.re + v1 * vw.im; 3269a19cd78SMatthias Ringwald } 3279a19cd78SMatthias Ringwald } 3289a19cd78SMatthias Ringwald 3299a19cd78SMatthias Ringwald /** 3309a19cd78SMatthias Ringwald * Post-rotate FFT N/4 points coefficients, resulting IMDCT N points 3319a19cd78SMatthias Ringwald * def Size and twiddles factors 3329a19cd78SMatthias Ringwald * x, y Input and output coefficients 3339a19cd78SMatthias Ringwald * scale Scale on output coefficients 3349a19cd78SMatthias Ringwald * 3359a19cd78SMatthias Ringwald * `x` and y` can be the same buffer 3364930cef6SMatthias Ringwald * The real and imaginary parts of `x` are swapped, 3374930cef6SMatthias Ringwald * to operate on FFT instead of IFFT 3389a19cd78SMatthias Ringwald */ 3394930cef6SMatthias Ringwald LC3_HOT static void imdct_post_fft(const struct lc3_mdct_rot_def *def, 3409a19cd78SMatthias Ringwald const struct lc3_complex *x, float *y, float scale) 3419a19cd78SMatthias Ringwald { 3429a19cd78SMatthias Ringwald int n4 = def->n4; 3439a19cd78SMatthias Ringwald 3449a19cd78SMatthias Ringwald const struct lc3_complex *w0 = def->w, *w1 = w0 + n4; 3459a19cd78SMatthias Ringwald const struct lc3_complex *x0 = x, *x1 = x0 + n4; 3469a19cd78SMatthias Ringwald 3479a19cd78SMatthias Ringwald float *y0 = y, *y1 = y0 + 2*n4; 3489a19cd78SMatthias Ringwald 3499a19cd78SMatthias Ringwald while (x0 < x1) { 3509a19cd78SMatthias Ringwald struct lc3_complex uz = *(x0++), vz = *(--x1); 3519a19cd78SMatthias Ringwald struct lc3_complex uw = *(w0++), vw = *(--w1); 3529a19cd78SMatthias Ringwald 3534930cef6SMatthias Ringwald *(y0++) = (uz.re * uw.im - uz.im * uw.re) * scale; 3544930cef6SMatthias Ringwald *(--y1) = (uz.re * uw.re + uz.im * uw.im) * scale; 3559a19cd78SMatthias Ringwald 3564930cef6SMatthias Ringwald *(--y1) = (vz.re * vw.im - vz.im * vw.re) * scale; 3574930cef6SMatthias Ringwald *(y0++) = (vz.re * vw.re + vz.im * vw.im) * scale; 3589a19cd78SMatthias Ringwald } 3599a19cd78SMatthias Ringwald } 3609a19cd78SMatthias Ringwald 3619a19cd78SMatthias Ringwald /** 3629a19cd78SMatthias Ringwald * Apply windowing of samples 3639a19cd78SMatthias Ringwald * dt, sr Duration and samplerate 3649a19cd78SMatthias Ringwald * x, d Middle half of IMDCT coefficients and delayed samples 3659a19cd78SMatthias Ringwald * y, d Output samples and delayed ones 3669a19cd78SMatthias Ringwald */ 3674930cef6SMatthias Ringwald LC3_HOT static void imdct_window(enum lc3_dt dt, enum lc3_srate sr, 3689a19cd78SMatthias Ringwald const float *x, float *d, float *y) 3699a19cd78SMatthias Ringwald { 3709a19cd78SMatthias Ringwald /* The full MDCT coefficients is given by symmetry : 3719a19cd78SMatthias Ringwald * T[ 0 .. n/4-1] = -half[n/4-1 .. 0 ] 3729a19cd78SMatthias Ringwald * T[ n/4 .. n/2-1] = half[0 .. n/4-1] 3739a19cd78SMatthias Ringwald * T[ n/2 .. 3n/4-1] = half[n/4 .. n/2-1] 3749a19cd78SMatthias Ringwald * T[3n/4 .. n-1] = half[n/2-1 .. n/4 ] */ 3759a19cd78SMatthias Ringwald 3769a19cd78SMatthias Ringwald int n4 = LC3_NS(dt, sr) >> 1, nd = LC3_ND(dt, sr); 3779a19cd78SMatthias Ringwald const float *w2 = lc3_mdct_win[dt][sr], *w0 = w2 + 3*n4, *w1 = w0; 3789a19cd78SMatthias Ringwald 3799a19cd78SMatthias Ringwald const float *x0 = d + nd-n4, *x1 = x0; 3809a19cd78SMatthias Ringwald float *y0 = y + nd-n4, *y1 = y0, *y2 = d + nd, *y3 = d; 3819a19cd78SMatthias Ringwald 3829a19cd78SMatthias Ringwald while (y0 > y) { 3839a19cd78SMatthias Ringwald *(--y0) = *(--x0) - *(x ) * *(w1++); 3849a19cd78SMatthias Ringwald *(y1++) = *(x1++) + *(x++) * *(--w0); 3854930cef6SMatthias Ringwald 3864930cef6SMatthias Ringwald *(--y0) = *(--x0) - *(x ) * *(w1++); 3874930cef6SMatthias Ringwald *(y1++) = *(x1++) + *(x++) * *(--w0); 3889a19cd78SMatthias Ringwald } 3899a19cd78SMatthias Ringwald 3909a19cd78SMatthias Ringwald while (y1 < y + nd) { 3919a19cd78SMatthias Ringwald *(y1++) = *(x1++) + *(x++) * *(--w0); 3924930cef6SMatthias Ringwald *(y1++) = *(x1++) + *(x++) * *(--w0); 3939a19cd78SMatthias Ringwald } 3949a19cd78SMatthias Ringwald 3959a19cd78SMatthias Ringwald while (y1 < y + 2*n4) { 3969a19cd78SMatthias Ringwald *(y1++) = *(x ) * *(--w0); 3979a19cd78SMatthias Ringwald *(--y2) = *(x++) * *(w2++); 3984930cef6SMatthias Ringwald 3994930cef6SMatthias Ringwald *(y1++) = *(x ) * *(--w0); 4004930cef6SMatthias Ringwald *(--y2) = *(x++) * *(w2++); 4019a19cd78SMatthias Ringwald } 4029a19cd78SMatthias Ringwald 4039a19cd78SMatthias Ringwald while (y2 > y3) { 4049a19cd78SMatthias Ringwald *(y3++) = *(x ) * *(--w0); 4059a19cd78SMatthias Ringwald *(--y2) = *(x++) * *(w2++); 4064930cef6SMatthias Ringwald 4074930cef6SMatthias Ringwald *(y3++) = *(x ) * *(--w0); 4084930cef6SMatthias Ringwald *(--y2) = *(x++) * *(w2++); 4099a19cd78SMatthias Ringwald } 4109a19cd78SMatthias Ringwald } 4119a19cd78SMatthias Ringwald 4129a19cd78SMatthias Ringwald /** 4139a19cd78SMatthias Ringwald * Forward MDCT transformation 4149a19cd78SMatthias Ringwald */ 4159a19cd78SMatthias Ringwald void lc3_mdct_forward(enum lc3_dt dt, enum lc3_srate sr, 4164930cef6SMatthias Ringwald enum lc3_srate sr_dst, const float *x, float *d, float *y) 4179a19cd78SMatthias Ringwald { 4189a19cd78SMatthias Ringwald const struct lc3_mdct_rot_def *rot = lc3_mdct_rot[dt][sr]; 4199a19cd78SMatthias Ringwald int nf = LC3_NS(dt, sr_dst); 4209a19cd78SMatthias Ringwald int ns = LC3_NS(dt, sr); 4219a19cd78SMatthias Ringwald 4224930cef6SMatthias Ringwald struct lc3_complex buffer[ns/2]; 4234930cef6SMatthias Ringwald struct lc3_complex *z = (struct lc3_complex *)y; 4244930cef6SMatthias Ringwald union { float *f; struct lc3_complex *z; } u = { .z = buffer }; 4259a19cd78SMatthias Ringwald 4264930cef6SMatthias Ringwald mdct_window(dt, sr, x, d, u.f); 4279a19cd78SMatthias Ringwald 4289a19cd78SMatthias Ringwald mdct_pre_fft(rot, u.f, u.z); 4294930cef6SMatthias Ringwald u.z = fft(u.z, ns/2, u.z, z); 4309a19cd78SMatthias Ringwald mdct_post_fft(rot, u.z, y, sqrtf( (2.f*nf) / (ns*ns) )); 4319a19cd78SMatthias Ringwald } 4329a19cd78SMatthias Ringwald 4339a19cd78SMatthias Ringwald /** 4349a19cd78SMatthias Ringwald * Inverse MDCT transformation 4359a19cd78SMatthias Ringwald */ 4369a19cd78SMatthias Ringwald void lc3_mdct_inverse(enum lc3_dt dt, enum lc3_srate sr, 4379a19cd78SMatthias Ringwald enum lc3_srate sr_src, const float *x, float *d, float *y) 4389a19cd78SMatthias Ringwald { 4399a19cd78SMatthias Ringwald const struct lc3_mdct_rot_def *rot = lc3_mdct_rot[dt][sr]; 4409a19cd78SMatthias Ringwald int nf = LC3_NS(dt, sr_src); 4419a19cd78SMatthias Ringwald int ns = LC3_NS(dt, sr); 4429a19cd78SMatthias Ringwald 4439a19cd78SMatthias Ringwald struct lc3_complex buffer[ns/2]; 4449a19cd78SMatthias Ringwald struct lc3_complex *z = (struct lc3_complex *)y; 4459a19cd78SMatthias Ringwald union { float *f; struct lc3_complex *z; } u = { .z = buffer }; 4469a19cd78SMatthias Ringwald 4479a19cd78SMatthias Ringwald imdct_pre_fft(rot, x, z); 4484930cef6SMatthias Ringwald z = fft(z, ns/2, z, u.z); 4499a19cd78SMatthias Ringwald imdct_post_fft(rot, z, u.f, sqrtf(2.f / nf)); 4509a19cd78SMatthias Ringwald 4519a19cd78SMatthias Ringwald imdct_window(dt, sr, u.f, d, y); 4529a19cd78SMatthias Ringwald } 453