FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mdct_template.c
Date: 2021-09-26 18:22:30
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 2509 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 2509 memset(s, 0, sizeof(*s));
51 2509 n = 1 << nbits;
52 2509 s->mdct_bits = nbits;
53 2509 s->mdct_size = n;
54 2509 n4 = n >> 2;
55 2509 s->mdct_permutation = FF_MDCT_PERM_NONE;
56
57
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2509 times.
2509 if (ff_fft_init(s, s->mdct_bits - 2, inverse) < 0)
58 goto fail;
59
60 2509 s->tcos = av_malloc_array(n/2, sizeof(FFTSample));
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2509 times.
2509 if (!s->tcos)
62 goto fail;
63
64
1/3
✓ Branch 0 taken 2509 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
2509 switch (s->mdct_permutation) {
65 2509 case FF_MDCT_PERM_NONE:
66 2509 s->tsin = s->tcos + n4;
67 2509 tstep = 1;
68 2509 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 784 times.
✓ Branch 1 taken 1725 times.
2509 theta = 1.0 / 8.0 + (scale < 0 ? n4 : 0);
78 2509 scale = sqrt(fabs(scale));
79
2/2
✓ Branch 0 taken 564780 times.
✓ Branch 1 taken 2509 times.
567289 for(i=0;i<n4;i++) {
80 564780 alpha = 2 * M_PI * (i + theta) / n;
81 #if !FFT_FLOAT
82 31100 s->tcos[i*tstep] = lrint(-cos(alpha) * 2147483648.0);
83 31100 s->tsin[i*tstep] = lrint(-sin(alpha) * 2147483648.0);
84 #else
85 533680 s->tcos[i*tstep] = FIX15(-cos(alpha) * scale);
86 533680 s->tsin[i*tstep] = FIX15(-sin(alpha) * scale);
87 #endif
88 }
89 2509 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 2332077 void ff_imdct_half_c(FFTContext *s, FFTSample *output, const FFTSample *input)
102 {
103 int k, n8, n4, n2, n, j;
104 2332077 const uint16_t *revtab = s->revtab;
105 2332077 const FFTSample *tcos = s->tcos;
106 2332077 const FFTSample *tsin = s->tsin;
107 const FFTSample *in1, *in2;
108 2332077 FFTComplex *z = (FFTComplex *)output;
109
110 2332077 n = 1 << s->mdct_bits;
111 2332077 n2 = n >> 1;
112 2332077 n4 = n >> 2;
113 2332077 n8 = n >> 3;
114
115 /* pre rotation */
116 2332077 in1 = input;
117 2332077 in2 = input + n2 - 1;
118
2/2
✓ Branch 0 taken 117312948 times.
✓ Branch 1 taken 2332077 times.
119645025 for(k = 0; k < n4; k++) {
119 117312948 j=revtab[k];
120 117312948 CMUL(z[j].re, z[j].im, *in2, *in1, tcos[k], tsin[k]);
121 117312948 in1 += 2;
122 117312948 in2 -= 2;
123 }
124 2332077 s->fft_calc(s, z);
125
126 /* post rotation + reordering */
127
2/2
✓ Branch 0 taken 58656474 times.
✓ Branch 1 taken 2332077 times.
60988551 for(k = 0; k < n8; k++) {
128 FFTSample r0, i0, r1, i1;
129 58656474 CMUL(r0, i1, z[n8-k-1].im, z[n8-k-1].re, tsin[n8-k-1], tcos[n8-k-1]);
130 58656474 CMUL(r1, i0, z[n8+k ].im, z[n8+k ].re, tsin[n8+k ], tcos[n8+k ]);
131 58656474 z[n8-k-1].re = r0;
132 58656474 z[n8-k-1].im = i0;
133 58656474 z[n8+k ].re = r1;
134 58656474 z[n8+k ].im = i1;
135 }
136 2332077 }
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 25630 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 25630 const uint16_t *revtab = s->revtab;
168 25630 const FFTSample *tcos = s->tcos;
169 25630 const FFTSample *tsin = s->tsin;
170 25630 FFTComplex *x = (FFTComplex *)out;
171
172 25630 n = 1 << s->mdct_bits;
173 25630 n2 = n >> 1;
174 25630 n4 = n >> 2;
175 25630 n8 = n >> 3;
176 25630 n3 = 3 * n4;
177
178 /* pre rotation */
179
2/2
✓ Branch 0 taken 3618300 times.
✓ Branch 1 taken 25630 times.
3643930 for(i=0;i<n8;i++) {
180 3618300 re = RSCALE(-input[2*i+n3], - input[n3-1-2*i]);
181 3618300 im = RSCALE(-input[n4+2*i], + input[n4-1-2*i]);
182 3618300 j = revtab[i];
183 3618300 CMUL(x[j].re, x[j].im, re, im, -tcos[i], tsin[i]);
184
185 3618300 re = RSCALE( input[2*i] , - input[n2-1-2*i]);
186 3618300 im = RSCALE(-input[n2+2*i], - input[ n-1-2*i]);
187 3618300 j = revtab[n8 + i];
188 3618300 CMUL(x[j].re, x[j].im, re, im, -tcos[n8 + i], tsin[n8 + i]);
189 }
190
191 25630 s->fft_calc(s, x);
192
193 /* post rotation */
194
2/2
✓ Branch 0 taken 3618300 times.
✓ Branch 1 taken 25630 times.
3643930 for(i=0;i<n8;i++) {
195 FFTSample r0, i0, r1, i1;
196 3618300 CMUL(i1, r0, x[n8-i-1].re, x[n8-i-1].im, -tsin[n8-i-1], -tcos[n8-i-1]);
197 3618300 CMUL(i0, r1, x[n8+i ].re, x[n8+i ].im, -tsin[n8+i ], -tcos[n8+i ]);
198 3618300 x[n8-i-1].re = r0;
199 3618300 x[n8-i-1].im = i0;
200 3618300 x[n8+i ].re = r1;
201 3618300 x[n8+i ].im = i1;
202 }
203 25630 }
204
205 2601 av_cold void ff_mdct_end(FFTContext *s)
206 {
207 2601 av_freep(&s->tcos);
208 2601 ff_fft_end(s);
209 2601 }
210