1 /*
2 * Copyright (c) Paul B Mahol
3 * Copyright (c) Laurent de Soras, 2005
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
28
29 #define MAX_NB_COEFFS 16
30
33
38
41
43
46
51
54 };
55
56 #define PFILTER(name, type, sin, cos, cc) \
57 static void pfilter_channel_## name(AVFilterContext *ctx, \
58 int ch, \
59 AVFrame *in, AVFrame *out) \
60 { \
61 AFreqShift *s = ctx->priv; \
62 const int nb_samples = in->nb_samples; \
63 const type *src = (const type *)in->extended_data[ch]; \
64 type *dst = (type *)out->extended_data[ch]; \
65 type *i1 = (type *)s->i1->extended_data[ch]; \
66 type *o1 = (type *)s->o1->extended_data[ch]; \
67 type *i2 = (type *)s->i2->extended_data[ch]; \
68 type *o2 = (type *)s->o2->extended_data[ch]; \
69 const type *c = s->cc; \
70 const type level = s->level; \
71 type shift = s->shift * M_PI; \
72 type cos_theta = cos(shift); \
73 type sin_theta = sin(shift); \
74 \
75 for (int n = 0; n < nb_samples; n++) { \
76 type xn1 = src[n], xn2 = src[n]; \
77 type I, Q; \
78 \
79 for (int j = 0; j < s->nb_coeffs; j++) { \
80 I = c[j] * (xn1 + o2[j]) - i2[j]; \
81 i2[j] = i1[j]; \
82 i1[j] = xn1; \
83 o2[j] = o1[j]; \
84 o1[j] = I; \
85 xn1 = I; \
86 } \
87 \
88 for (int j = s->nb_coeffs; j < s->nb_coeffs*2; j++) { \
89 Q = c[j] * (xn2 + o2[j]) - i2[j]; \
90 i2[j] = i1[j]; \
91 i1[j] = xn2; \
92 o2[j] = o1[j]; \
93 o1[j] = Q; \
94 xn2 = Q; \
95 } \
96 Q = o2[s->nb_coeffs * 2 - 1]; \
97 \
98 dst[n] = (I * cos_theta - Q * sin_theta) * level; \
99 } \
100 }
101
102 PFILTER(flt,
float, sin, cos, cf)
103 PFILTER(dbl,
double, sin, cos, cd)
104
105 #define FFILTER(name, type, sin, cos, fmod, cc) \
106 static void ffilter_channel_## name(AVFilterContext *ctx, \
107 int ch, \
108 AVFrame *in, AVFrame *out) \
109 { \
110 AFreqShift *s = ctx->priv; \
111 const int nb_samples = in->nb_samples; \
112 const type *src = (const type *)in->extended_data[ch]; \
113 type *dst = (type *)out->extended_data[ch]; \
114 type *i1 = (type *)s->i1->extended_data[ch]; \
115 type *o1 = (type *)s->o1->extended_data[ch]; \
116 type *i2 = (type *)s->i2->extended_data[ch]; \
117 type *o2 = (type *)s->o2->extended_data[ch]; \
118 const type *c = s->cc; \
119 const type level = s->level; \
120 type ts = 1. / in->sample_rate; \
121 type shift = s->shift; \
122 int64_t N = s->in_samples; \
123 \
124 for (int n = 0; n < nb_samples; n++) { \
125 type xn1 = src[n], xn2 = src[n]; \
126 type I, Q, theta; \
127 \
128 for (int j = 0; j < s->nb_coeffs; j++) { \
129 I = c[j] * (xn1 + o2[j]) - i2[j]; \
130 i2[j] = i1[j]; \
131 i1[j] = xn1; \
132 o2[j] = o1[j]; \
133 o1[j] = I; \
134 xn1 = I; \
135 } \
136 \
137 for (int j = s->nb_coeffs; j < s->nb_coeffs*2; j++) { \
138 Q = c[j] * (xn2 + o2[j]) - i2[j]; \
139 i2[j] = i1[j]; \
140 i1[j] = xn2; \
141 o2[j] = o1[j]; \
142 o1[j] = Q; \
143 xn2 = Q; \
144 } \
145 Q = o2[s->nb_coeffs * 2 - 1]; \
146 \
147 theta = 2. * M_PI * fmod(shift * (N + n) * ts, 1.); \
148 dst[n] = (I * cos(theta) - Q * sin(theta)) * level; \
149 } \
150 }
151
153 FFILTER(dbl,
double, sin, cos, fmod, cd)
154
156 {
157 double kksqrt, e, e2, e4, k, q;
158
159 k = tan((1. - transition * 2.) *
M_PI / 4.);
160 k *= k;
161 kksqrt = pow(1 - k * k, 0.25);
162 e = 0.5 * (1. - kksqrt) / (1. + kksqrt);
163 e2 = e * e;
164 e4 = e2 * e2;
165 q = e * (1. + e4 * (2. + e4 * (15. + 150. * e4)));
166
167 *Q = q;
168 *K = k;
169 }
170
171 static double ipowp(
double x, int64_t n)
172 {
173 double z = 1.;
174
175 while (n != 0) {
176 if (n & 1)
177 z *= x;
178 n >>= 1;
179 x *= x;
180 }
181
182 return z;
183 }
184
186 {
188 int j = 1;
190 double q_ii1;
191
192 do {
194 q_ii1 *= sin((
i * 2 + 1) *
c *
M_PI / order) * j;
196
197 j = -j;
199 }
while (
fabs(q_ii1) > 1e-100);
200
202 }
203
205 {
207 int j = -1;
209 double q_i2;
210
211 do {
213 q_i2 *= cos(
i * 2 *
c *
M_PI / order) * j;
215
216 j = -j;
218 }
while (
fabs(q_i2) > 1e-100);
219
221 }
222
224 {
228 const double ww = num / den;
229 const double wwsq = ww * ww;
230
231 const double x = sqrt((1 - wwsq * k) * (1 - wwsq / k)) / (1 + wwsq);
232 const double coef = (1 - x) / (1 + x);
233
234 return coef;
235 }
236
237 static void compute_coefs(
double *coef_arrd,
float *coef_arrf,
int nbr_coefs,
double transition)
238 {
239 const int order = nbr_coefs * 2 + 1;
240 double k, q;
241
243
244 for (int n = 0; n < nbr_coefs; n++) {
245 const int idx = (n / 2) + (n & 1) * nbr_coefs / 2;
246
248 coef_arrf[idx] = coef_arrd[idx];
249 }
250 }
251
253 {
256
257 if (
s->old_nb_coeffs !=
s->nb_coeffs)
259 s->old_nb_coeffs =
s->nb_coeffs;
260
265 if (!
s->i1 || !
s->o1 || !
s->i2 || !
s->o2)
267
269 if (!strcmp(
ctx->filter->name,
"afreqshift"))
270 s->filter_channel = ffilter_channel_dbl;
271 else
272 s->filter_channel = pfilter_channel_dbl;
273 } else {
274 if (!strcmp(
ctx->filter->name,
"afreqshift"))
275 s->filter_channel = ffilter_channel_flt;
276 else
277 s->filter_channel = pfilter_channel_flt;
278 }
279
280 return 0;
281 }
282
286
288 {
293 const int start = (in->
channels * jobnr) / nb_jobs;
294 const int end = (in->
channels * (jobnr+1)) / nb_jobs;
295
296 for (int ch = start; ch < end; ch++)
297 s->filter_channel(
ctx, ch, in,
out);
298
299 return 0;
300 }
301
303 {
309
310 if (
s->old_nb_coeffs !=
s->nb_coeffs)
312 s->old_nb_coeffs =
s->nb_coeffs;
313
316 } else {
321 }
323 }
324
328
330
334 }
335
337 {
339
344 }
345
346 #define OFFSET(x) offsetof(AFreqShift, x)
347 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
348
354 };
355
357
359 {
364 },
365 };
366
368 {
371 },
372 };
373
375 .
name =
"afreqshift",
378 .priv_class = &afreqshift_class,
386 };
387
393 };
394
396
398 .
name =
"aphaseshift",
401 .priv_class = &aphaseshift_class,
409 };