Directory: | ../../../ffmpeg/ |
---|---|
File: | src/libavcodec/mdct_template.c |
Date: | 2022-07-04 19:11:22 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 90 | 101 | 89.1% |
Branches: | 17 | 21 | 81.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * MDCT/IMDCT transforms | ||
3 | * Copyright (c) 2002 Fabrice Bellard | ||
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 | #include <stdlib.h> | ||
23 | #include <string.h> | ||
24 | #include "libavutil/common.h" | ||
25 | #include "libavutil/libm.h" | ||
26 | #include "libavutil/mathematics.h" | ||
27 | #include "fft.h" | ||
28 | #include "fft-internal.h" | ||
29 | |||
30 | /** | ||
31 | * @file | ||
32 | * MDCT/IMDCT transforms. | ||
33 | */ | ||
34 | |||
35 | #if FFT_FLOAT | ||
36 | # define RSCALE(x, y) ((x) + (y)) | ||
37 | #else | ||
38 | # define RSCALE(x, y) ((int)((x) + (unsigned)(y) + 32) >> 6) | ||
39 | #endif | ||
40 | |||
41 | /** | ||
42 | * init MDCT or IMDCT computation. | ||
43 | */ | ||
44 | 2609 | av_cold int ff_mdct_init(FFTContext *s, int nbits, int inverse, double scale) | |
45 | { | ||
46 | int n, n4, i; | ||
47 | double alpha, theta; | ||
48 | int tstep; | ||
49 | |||
50 | 2609 | memset(s, 0, sizeof(*s)); | |
51 | 2609 | n = 1 << nbits; | |
52 | 2609 | s->mdct_bits = nbits; | |
53 | 2609 | s->mdct_size = n; | |
54 | 2609 | n4 = n >> 2; | |
55 | 2609 | s->mdct_permutation = FF_MDCT_PERM_NONE; | |
56 | |||
57 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2609 times.
|
2609 | if (ff_fft_init(s, s->mdct_bits - 2, inverse) < 0) |
58 | ✗ | goto fail; | |
59 | |||
60 | 2609 | s->tcos = av_malloc_array(n/2, sizeof(FFTSample)); | |
61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2609 times.
|
2609 | if (!s->tcos) |
62 | ✗ | goto fail; | |
63 | |||
64 |
1/3✓ Branch 0 taken 2609 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
2609 | switch (s->mdct_permutation) { |
65 | 2609 | case FF_MDCT_PERM_NONE: | |
66 | 2609 | s->tsin = s->tcos + n4; | |
67 | 2609 | tstep = 1; | |
68 | 2609 | break; | |
69 | ✗ | case FF_MDCT_PERM_INTERLEAVE: | |
70 | ✗ | s->tsin = s->tcos + 1; | |
71 | ✗ | tstep = 2; | |
72 | ✗ | break; | |
73 | ✗ | default: | |
74 | ✗ | goto fail; | |
75 | } | ||
76 | |||
77 |
2/2✓ Branch 0 taken 816 times.
✓ Branch 1 taken 1793 times.
|
2609 | theta = 1.0 / 8.0 + (scale < 0 ? n4 : 0); |
78 | 2609 | scale = sqrt(fabs(scale)); | |
79 |
2/2✓ Branch 0 taken 584588 times.
✓ Branch 1 taken 2609 times.
|
587197 | for(i=0;i<n4;i++) { |
80 | 584588 | alpha = 2 * M_PI * (i + theta) / n; | |
81 | #if !FFT_FLOAT | ||
82 | 34428 | s->tcos[i*tstep] = lrint(-cos(alpha) * 2147483648.0); | |
83 | 34428 | s->tsin[i*tstep] = lrint(-sin(alpha) * 2147483648.0); | |
84 | #else | ||
85 | 550160 | s->tcos[i*tstep] = FIX15(-cos(alpha) * scale); | |
86 | 550160 | s->tsin[i*tstep] = FIX15(-sin(alpha) * scale); | |
87 | #endif | ||
88 | } | ||
89 | 2609 | return 0; | |
90 | ✗ | fail: | |
91 | ✗ | ff_mdct_end(s); | |
92 | ✗ | return -1; | |
93 | } | ||
94 | |||
95 | /** | ||
96 | * Compute the middle half of the inverse MDCT of size N = 2^nbits, | ||
97 | * thus excluding the parts that can be derived by symmetry | ||
98 | * @param output N/2 samples | ||
99 | * @param input N/2 samples | ||
100 | */ | ||
101 | 2332359 | void ff_imdct_half_c(FFTContext *s, FFTSample *output, const FFTSample *input) | |
102 | { | ||
103 | int k, n8, n4, n2, n, j; | ||
104 | 2332359 | const uint16_t *revtab = s->revtab; | |
105 | 2332359 | const FFTSample *tcos = s->tcos; | |
106 | 2332359 | const FFTSample *tsin = s->tsin; | |
107 | const FFTSample *in1, *in2; | ||
108 | 2332359 | FFTComplex *z = (FFTComplex *)output; | |
109 | |||
110 | 2332359 | n = 1 << s->mdct_bits; | |
111 | 2332359 | n2 = n >> 1; | |
112 | 2332359 | n4 = n >> 2; | |
113 | 2332359 | n8 = n >> 3; | |
114 | |||
115 | /* pre rotation */ | ||
116 | 2332359 | in1 = input; | |
117 | 2332359 | in2 = input + n2 - 1; | |
118 |
2/2✓ Branch 0 taken 117406644 times.
✓ Branch 1 taken 2332359 times.
|
119739003 | for(k = 0; k < n4; k++) { |
119 | 117406644 | j=revtab[k]; | |
120 | 117406644 | CMUL(z[j].re, z[j].im, *in2, *in1, tcos[k], tsin[k]); | |
121 | 117406644 | in1 += 2; | |
122 | 117406644 | in2 -= 2; | |
123 | } | ||
124 | 2332359 | s->fft_calc(s, z); | |
125 | |||
126 | /* post rotation + reordering */ | ||
127 |
2/2✓ Branch 0 taken 58703322 times.
✓ Branch 1 taken 2332359 times.
|
61035681 | for(k = 0; k < n8; k++) { |
128 | FFTSample r0, i0, r1, i1; | ||
129 | 58703322 | CMUL(r0, i1, z[n8-k-1].im, z[n8-k-1].re, tsin[n8-k-1], tcos[n8-k-1]); | |
130 | 58703322 | CMUL(r1, i0, z[n8+k ].im, z[n8+k ].re, tsin[n8+k ], tcos[n8+k ]); | |
131 | 58703322 | z[n8-k-1].re = r0; | |
132 | 58703322 | z[n8-k-1].im = i0; | |
133 | 58703322 | z[n8+k ].re = r1; | |
134 | 58703322 | z[n8+k ].im = i1; | |
135 | } | ||
136 | 2332359 | } | |
137 | |||
138 | /** | ||
139 | * Compute inverse MDCT of size N = 2^nbits | ||
140 | * @param output N samples | ||
141 | * @param input N/2 samples | ||
142 | */ | ||
143 | 25945 | void ff_imdct_calc_c(FFTContext *s, FFTSample *output, const FFTSample *input) | |
144 | { | ||
145 | int k; | ||
146 | 25945 | int n = 1 << s->mdct_bits; | |
147 | 25945 | int n2 = n >> 1; | |
148 | 25945 | int n4 = n >> 2; | |
149 | |||
150 | 25945 | ff_imdct_half_c(s, output+n4, input); | |
151 | |||
152 |
2/2✓ Branch 0 taken 2846836 times.
✓ Branch 1 taken 25945 times.
|
2872781 | for(k = 0; k < n4; k++) { |
153 | 2846836 | output[k] = -output[n2-k-1]; | |
154 | 2846836 | output[n-k-1] = output[n2+k]; | |
155 | } | ||
156 | 25945 | } | |
157 | |||
158 | /** | ||
159 | * Compute MDCT of size N = 2^nbits | ||
160 | * @param input N samples | ||
161 | * @param out N/2 samples | ||
162 | */ | ||
163 | 27280 | void ff_mdct_calc_c(FFTContext *s, FFTSample *out, const FFTSample *input) | |
164 | { | ||
165 | int i, j, n, n8, n4, n2, n3; | ||
166 | FFTDouble re, im; | ||
167 | 27280 | const uint16_t *revtab = s->revtab; | |
168 | 27280 | const FFTSample *tcos = s->tcos; | |
169 | 27280 | const FFTSample *tsin = s->tsin; | |
170 | 27280 | FFTComplex *x = (FFTComplex *)out; | |
171 | |||
172 | 27280 | n = 1 << s->mdct_bits; | |
173 | 27280 | n2 = n >> 1; | |
174 | 27280 | n4 = n >> 2; | |
175 | 27280 | n8 = n >> 3; | |
176 | 27280 | n3 = 3 * n4; | |
177 | |||
178 | /* pre rotation */ | ||
179 |
2/2✓ Branch 0 taken 3723900 times.
✓ Branch 1 taken 27280 times.
|
3751180 | for(i=0;i<n8;i++) { |
180 | 3723900 | re = RSCALE(-input[2*i+n3], - input[n3-1-2*i]); | |
181 | 3723900 | im = RSCALE(-input[n4+2*i], + input[n4-1-2*i]); | |
182 | 3723900 | j = revtab[i]; | |
183 | 3723900 | CMUL(x[j].re, x[j].im, re, im, -tcos[i], tsin[i]); | |
184 | |||
185 | 3723900 | re = RSCALE( input[2*i] , - input[n2-1-2*i]); | |
186 | 3723900 | im = RSCALE(-input[n2+2*i], - input[ n-1-2*i]); | |
187 | 3723900 | j = revtab[n8 + i]; | |
188 | 3723900 | CMUL(x[j].re, x[j].im, re, im, -tcos[n8 + i], tsin[n8 + i]); | |
189 | } | ||
190 | |||
191 | 27280 | s->fft_calc(s, x); | |
192 | |||
193 | /* post rotation */ | ||
194 |
2/2✓ Branch 0 taken 3723900 times.
✓ Branch 1 taken 27280 times.
|
3751180 | for(i=0;i<n8;i++) { |
195 | FFTSample r0, i0, r1, i1; | ||
196 | 3723900 | CMUL(i1, r0, x[n8-i-1].re, x[n8-i-1].im, -tsin[n8-i-1], -tcos[n8-i-1]); | |
197 | 3723900 | CMUL(i0, r1, x[n8+i ].re, x[n8+i ].im, -tsin[n8+i ], -tcos[n8+i ]); | |
198 | 3723900 | x[n8-i-1].re = r0; | |
199 | 3723900 | x[n8-i-1].im = i0; | |
200 | 3723900 | x[n8+i ].re = r1; | |
201 | 3723900 | x[n8+i ].im = i1; | |
202 | } | ||
203 | 27280 | } | |
204 | |||
205 | 2701 | av_cold void ff_mdct_end(FFTContext *s) | |
206 | { | ||
207 | 2701 | av_freep(&s->tcos); | |
208 | 2701 | ff_fft_end(s); | |
209 | 2701 | } | |
210 |