]> git.sesse.net Git - ffmpeg/blob - libavcodec/mdct15.c
avcodec/h264: use some 3 operand forms
[ffmpeg] / libavcodec / mdct15.c
1 /*
2  * Copyright (c) 2013-2014 Mozilla Corporation
3  * Copyright (c) 2017 Rostislav Pehlivanov <atomnuker@gmail.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * Celt non-power of 2 iMDCT
25  */
26
27 #include <float.h>
28 #include <math.h>
29 #include <stddef.h>
30
31 #include "config.h"
32
33 #include "libavutil/attributes.h"
34 #include "libavutil/common.h"
35
36 #include "mdct15.h"
37
38 #define FFT_FLOAT 1
39 #include "fft-internal.h"
40
41 #define CMUL3(c, a, b) CMUL((c).re, (c).im, (a).re, (a).im, (b).re, (b).im)
42
43 av_cold void ff_mdct15_uninit(MDCT15Context **ps)
44 {
45     MDCT15Context *s = *ps;
46
47     if (!s)
48         return;
49
50     ff_fft_end(&s->ptwo_fft);
51
52     av_freep(&s->pfa_prereindex);
53     av_freep(&s->pfa_postreindex);
54     av_freep(&s->twiddle_exptab);
55     av_freep(&s->tmp);
56
57     av_freep(ps);
58 }
59
60 static void mdct15(MDCT15Context *s, float *dst, const float *src, ptrdiff_t stride);
61
62 static void imdct15_half(MDCT15Context *s, float *dst, const float *src,
63                          ptrdiff_t stride, float scale);
64
65 static inline int init_pfa_reindex_tabs(MDCT15Context *s)
66 {
67     int i, j;
68     const int b_ptwo = s->ptwo_fft.nbits; /* Bits for the power of two FFTs */
69     const int l_ptwo = 1 << b_ptwo; /* Total length for the power of two FFTs */
70     const int inv_1 = l_ptwo << ((4 - b_ptwo) & 3); /* (2^b_ptwo)^-1 mod 15 */
71     const int inv_2 = 0xeeeeeeef & ((1U << b_ptwo) - 1); /* 15^-1 mod 2^b_ptwo */
72
73     s->pfa_prereindex = av_malloc(15 * l_ptwo * sizeof(*s->pfa_prereindex));
74     if (!s->pfa_prereindex)
75         return 1;
76
77     s->pfa_postreindex = av_malloc(15 * l_ptwo * sizeof(*s->pfa_postreindex));
78     if (!s->pfa_postreindex)
79         return 1;
80
81     /* Pre/Post-reindex */
82     for (i = 0; i < l_ptwo; i++) {
83         for (j = 0; j < 15; j++) {
84             const int q_pre = ((l_ptwo * j)/15 + i) >> b_ptwo;
85             const int q_post = (((j*inv_1)/15) + (i*inv_2)) >> b_ptwo;
86             const int k_pre = 15*i + (j - q_pre*15)*(1 << b_ptwo);
87             const int k_post = i*inv_2*15 + j*inv_1 - 15*q_post*l_ptwo;
88             s->pfa_prereindex[i*15 + j] = k_pre;
89             s->pfa_postreindex[k_post] = l_ptwo*j + i;
90         }
91     }
92
93     return 0;
94 }
95
96 av_cold int ff_mdct15_init(MDCT15Context **ps, int inverse, int N, double scale)
97 {
98     MDCT15Context *s;
99     double alpha, theta;
100     int len2 = 15 * (1 << N);
101     int len  = 2 * len2;
102     int i;
103
104     /* Tested and verified to work on everything in between */
105     if ((N < 2) || (N > 13))
106         return AVERROR(EINVAL);
107
108     s = av_mallocz(sizeof(*s));
109     if (!s)
110         return AVERROR(ENOMEM);
111
112     s->fft_n = N - 1;
113     s->len4 = len2 / 2;
114     s->len2 = len2;
115     s->inverse = inverse;
116     s->mdct = mdct15;
117     s->imdct_half = imdct15_half;
118
119     if (ff_fft_init(&s->ptwo_fft, N - 1, s->inverse) < 0)
120         goto fail;
121
122     if (init_pfa_reindex_tabs(s))
123         goto fail;
124
125     s->tmp  = av_malloc_array(len, 2 * sizeof(*s->tmp));
126     if (!s->tmp)
127         goto fail;
128
129     s->twiddle_exptab  = av_malloc_array(s->len4, sizeof(*s->twiddle_exptab));
130     if (!s->twiddle_exptab)
131         goto fail;
132
133     theta = 0.125f + (scale < 0 ? s->len4 : 0);
134     scale = sqrt(fabs(scale));
135     for (i = 0; i < s->len4; i++) {
136         alpha = 2 * M_PI * (i + theta) / len;
137         s->twiddle_exptab[i].re = cos(alpha) * scale;
138         s->twiddle_exptab[i].im = sin(alpha) * scale;
139     }
140
141     /* 15-point FFT exptab */
142     for (i = 0; i < 19; i++) {
143         if (i < 15) {
144             double theta = (2.0f * M_PI * i) / 15.0f;
145             if (!s->inverse)
146                 theta *= -1;
147             s->exptab[i].re = cos(theta);
148             s->exptab[i].im = sin(theta);
149         } else { /* Wrap around to simplify fft15 */
150             s->exptab[i] = s->exptab[i - 15];
151         }
152     }
153
154     /* 5-point FFT exptab */
155     s->exptab[19].re = cos(2.0f * M_PI / 5.0f);
156     s->exptab[19].im = sin(2.0f * M_PI / 5.0f);
157     s->exptab[20].re = cos(1.0f * M_PI / 5.0f);
158     s->exptab[20].im = sin(1.0f * M_PI / 5.0f);
159
160     /* Invert the phase for an inverse transform, do nothing for a forward transform */
161     if (s->inverse) {
162         s->exptab[19].im *= -1;
163         s->exptab[20].im *= -1;
164     }
165
166     *ps = s;
167
168     return 0;
169
170 fail:
171     ff_mdct15_uninit(&s);
172     return AVERROR(ENOMEM);
173 }
174
175 /* Stride is hardcoded to 3 */
176 static inline void fft5(const FFTComplex exptab[2], FFTComplex *out,
177                         const FFTComplex *in)
178 {
179     FFTComplex z0[4], t[6];
180
181     t[0].re = in[3].re + in[12].re;
182     t[0].im = in[3].im + in[12].im;
183     t[1].im = in[3].re - in[12].re;
184     t[1].re = in[3].im - in[12].im;
185     t[2].re = in[6].re + in[ 9].re;
186     t[2].im = in[6].im + in[ 9].im;
187     t[3].im = in[6].re - in[ 9].re;
188     t[3].re = in[6].im - in[ 9].im;
189
190     out[0].re = in[0].re + in[3].re + in[6].re + in[9].re + in[12].re;
191     out[0].im = in[0].im + in[3].im + in[6].im + in[9].im + in[12].im;
192
193     t[4].re = exptab[0].re * t[2].re - exptab[1].re * t[0].re;
194     t[4].im = exptab[0].re * t[2].im - exptab[1].re * t[0].im;
195     t[0].re = exptab[0].re * t[0].re - exptab[1].re * t[2].re;
196     t[0].im = exptab[0].re * t[0].im - exptab[1].re * t[2].im;
197     t[5].re = exptab[0].im * t[3].re - exptab[1].im * t[1].re;
198     t[5].im = exptab[0].im * t[3].im - exptab[1].im * t[1].im;
199     t[1].re = exptab[0].im * t[1].re + exptab[1].im * t[3].re;
200     t[1].im = exptab[0].im * t[1].im + exptab[1].im * t[3].im;
201
202     z0[0].re = t[0].re - t[1].re;
203     z0[0].im = t[0].im - t[1].im;
204     z0[1].re = t[4].re + t[5].re;
205     z0[1].im = t[4].im + t[5].im;
206
207     z0[2].re = t[4].re - t[5].re;
208     z0[2].im = t[4].im - t[5].im;
209     z0[3].re = t[0].re + t[1].re;
210     z0[3].im = t[0].im + t[1].im;
211
212     out[1].re = in[0].re + z0[3].re;
213     out[1].im = in[0].im + z0[0].im;
214     out[2].re = in[0].re + z0[2].re;
215     out[2].im = in[0].im + z0[1].im;
216     out[3].re = in[0].re + z0[1].re;
217     out[3].im = in[0].im + z0[2].im;
218     out[4].re = in[0].re + z0[0].re;
219     out[4].im = in[0].im + z0[3].im;
220 }
221
222 static void fft15(const FFTComplex exptab[22], FFTComplex *out, const FFTComplex *in, size_t stride)
223 {
224     int k;
225     FFTComplex tmp1[5], tmp2[5], tmp3[5];
226
227     fft5(exptab + 19, tmp1, in + 0);
228     fft5(exptab + 19, tmp2, in + 1);
229     fft5(exptab + 19, tmp3, in + 2);
230
231     for (k = 0; k < 5; k++) {
232         FFTComplex t[2];
233
234         CMUL3(t[0], tmp2[k], exptab[k]);
235         CMUL3(t[1], tmp3[k], exptab[2 * k]);
236         out[stride*k].re = tmp1[k].re + t[0].re + t[1].re;
237         out[stride*k].im = tmp1[k].im + t[0].im + t[1].im;
238
239         CMUL3(t[0], tmp2[k], exptab[k + 5]);
240         CMUL3(t[1], tmp3[k], exptab[2 * (k + 5)]);
241         out[stride*(k + 5)].re = tmp1[k].re + t[0].re + t[1].re;
242         out[stride*(k + 5)].im = tmp1[k].im + t[0].im + t[1].im;
243
244         CMUL3(t[0], tmp2[k], exptab[k + 10]);
245         CMUL3(t[1], tmp3[k], exptab[2 * k + 5]);
246         out[stride*(k + 10)].re = tmp1[k].re + t[0].re + t[1].re;
247         out[stride*(k + 10)].im = tmp1[k].im + t[0].im + t[1].im;
248     }
249 }
250
251 static void mdct15(MDCT15Context *s, float *dst, const float *src, ptrdiff_t stride)
252 {
253     int i, j;
254     const int len4 = s->len4, len3 = len4 * 3, len8 = len4 >> 1;
255     const int l_ptwo = 1 << s->ptwo_fft.nbits;
256     FFTComplex fft15in[15];
257
258     /* Folding and pre-reindexing */
259     for (i = 0; i < l_ptwo; i++) {
260         for (j = 0; j < 15; j++) {
261             float re, im;
262             const int k = s->pfa_prereindex[i*15 + j];
263             if (k < len8) {
264                 re = -src[2*k+len3] - src[len3-1-2*k];
265                 im = -src[len4+2*k] + src[len4-1-2*k];
266             } else {
267                 re =  src[2*k-len4] - src[1*len3-1-2*k];
268                 im = -src[2*k+len4] - src[5*len4-1-2*k];
269             }
270             CMUL(fft15in[j].re, fft15in[j].im, re, im, s->twiddle_exptab[k].re, -s->twiddle_exptab[k].im);
271         }
272         fft15(s->exptab, s->tmp + s->ptwo_fft.revtab[i], fft15in, l_ptwo);
273     }
274
275     /* Then a 15xN FFT (where N is a power of two) */
276     for (i = 0; i < 15; i++)
277         s->ptwo_fft.fft_calc(&s->ptwo_fft, s->tmp + l_ptwo*i);
278
279     /* Reindex again, apply twiddles and output */
280     for (i = 0; i < len8; i++) {
281         float re0, im0, re1, im1;
282         const int i0 = len8 + i, i1 = len8 - i - 1;
283         const int s0 = s->pfa_postreindex[i0], s1 = s->pfa_postreindex[i1];
284
285         CMUL(im1, re0, s->tmp[s1].re, s->tmp[s1].im, s->twiddle_exptab[i1].im, s->twiddle_exptab[i1].re);
286         CMUL(im0, re1, s->tmp[s0].re, s->tmp[s0].im, s->twiddle_exptab[i0].im, s->twiddle_exptab[i0].re);
287         dst[2*i1*stride         ] = re0;
288         dst[2*i1*stride + stride] = im0;
289         dst[2*i0*stride         ] = re1;
290         dst[2*i0*stride + stride] = im1;
291     }
292 }
293
294 static void imdct15_half(MDCT15Context *s, float *dst, const float *src,
295                          ptrdiff_t stride, float scale)
296 {
297     FFTComplex fft15in[15];
298     FFTComplex *z = (FFTComplex *)dst;
299     int i, j, len8 = s->len4 >> 1, l_ptwo = 1 << s->ptwo_fft.nbits;
300     const float *in1 = src, *in2 = src + (s->len2 - 1) * stride;
301
302     /* Reindex input, putting it into a buffer and doing an Nx15 FFT */
303     for (i = 0; i < l_ptwo; i++) {
304         for (j = 0; j < 15; j++) {
305             const int k = s->pfa_prereindex[i*15 + j];
306             FFTComplex tmp = { *(in2 - 2*k*stride), *(in1 + 2*k*stride) };
307             CMUL3(fft15in[j], tmp, s->twiddle_exptab[k]);
308         }
309         fft15(s->exptab, s->tmp + s->ptwo_fft.revtab[i], fft15in, l_ptwo);
310     }
311
312     /* Then a 15xN FFT (where N is a power of two) */
313     for (i = 0; i < 15; i++)
314         s->ptwo_fft.fft_calc(&s->ptwo_fft, s->tmp + l_ptwo*i);
315
316     /* Reindex again, apply twiddles and output */
317     for (i = 0; i < len8; i++) {
318         float re0, im0, re1, im1;
319         const int i0 = len8 + i, i1 = len8 - i - 1;
320         const int s0 = s->pfa_postreindex[i0], s1 = s->pfa_postreindex[i1];
321
322         CMUL(re0, im1, s->tmp[s1].im, s->tmp[s1].re,  s->twiddle_exptab[i1].im, s->twiddle_exptab[i1].re);
323         CMUL(re1, im0, s->tmp[s0].im, s->tmp[s0].re,  s->twiddle_exptab[i0].im, s->twiddle_exptab[i0].re);
324         z[i1].re = scale * re0;
325         z[i1].im = scale * im0;
326         z[i0].re = scale * re1;
327         z[i0].im = scale * im1;
328     }
329 }