50 const float *coeffs = s->
coeffs;
61 memcpy(frame->
data[0], coeffs + s->
pts, nb_samples *
sizeof(
float));
102 float term = 1, sum = 1, last_sum, x2 = x / 2;
109 sum += term *= y * y;
110 }
while (sum != last_sum);
115 static float *
make_lpf(
int num_taps,
float Fc,
float beta,
float rho,
116 float scale,
int dc_norm)
118 int i, m = num_taps - 1;
119 float *
h =
av_calloc(num_taps,
sizeof(*h)), sum = 0;
120 float mult = scale /
bessel_I_0(beta), mult1 = 1.f / (.5f * m + rho);
124 for (i = 0; i <= m / 2; i++) {
125 float z = i - .5f * m, x = z *
M_PI, y = z * mult1;
126 h[i] = x ?
sinf(Fc * x) / x : Fc;
134 for (i = 0; dc_norm && i < num_taps; i++)
143 static const float coefs[][4] = {
144 {-6.784957e-10, 1.02856e-05, 0.1087556, -0.8988365 + .001},
145 {-6.897885e-10, 1.027433e-05, 0.10876, -0.8994658 + .002},
146 {-1.000683e-09, 1.030092e-05, 0.1087677, -0.9007898 + .003},
147 {-3.654474e-10, 1.040631e-05, 0.1087085, -0.8977766 + .006},
148 {8.106988e-09, 6.983091e-06, 0.1091387, -0.9172048 + .015},
149 {9.519571e-09, 7.272678e-06, 0.1090068, -0.9140768 + .025},
150 {-5.626821e-09, 1.342186e-05, 0.1083999, -0.9065452 + .05},
151 {-9.965946e-08, 5.073548e-05, 0.1040967, -0.7672778 + .085},
152 {1.604808e-07, -5.856462e-05, 0.1185998, -1.34824 + .1},
153 {-1.511964e-07, 6.363034e-05, 0.1064627, -0.9876665 + .18},
155 float realm = logf(tr_bw / .0005
f) / logf(2.
f);
156 float const *c0 = coefs[av_clip((
int)realm, 0,
FF_ARRAY_ELEMS(coefs) - 1)];
157 float const *
c1 = coefs[av_clip(1 + (
int)realm, 0,
FF_ARRAY_ELEMS(coefs) - 1)];
158 float b0 = ((c0[0] * att + c0[1]) * att + c0[2]) * att + c0[3];
159 float b1 = ((c1[0] * att + c1[1]) * att + c1[2]) * att + c1[3];
161 return b0 + (b1 - b0) * (realm - (
int)realm);
164 return .1102f * (att - 8.7f);
166 return .58417f *
powf(att - 20.96
f, .4
f) + .07886f * (att - 20.96f);
170 static void kaiser_params(
float att,
float Fc,
float tr_bw,
float *beta,
int *num_taps)
172 *beta = *beta < 0.f ?
kaiser_beta(att, tr_bw * .5
f / Fc): *beta;
173 att = att < 60.f ? (att - 7.95f) / (2.285
f *
M_PI * 2.
f) :
174 ((.0007528358f-1.577737e-05 * *beta) * *beta + 0.6248022f) * *beta + .06186902f;
175 *num_taps = !*num_taps ? ceilf(att/tr_bw + 1) : *num_taps;
178 static float *
lpf(
float Fn,
float Fc,
float tbw,
int *num_taps,
float att,
float *beta,
int round)
182 if ((Fc /= Fn) <= 0.
f || Fc >= 1.
f) {
187 att = att ? att : 120.f;
189 kaiser_params(att, Fc, (tbw ? tbw / Fn : .05
f) * .5f, beta, num_taps);
193 *num_taps = av_clip(n, 11, 32767);
195 *num_taps = 1 + 2 * (
int)((
int)((*num_taps / 2) * Fc + .5f) / Fc + .5f);
198 return make_lpf(*num_taps |= 1, Fc, *beta, 0.f, 1.f, 0);
203 for (
int i = 0; i <
n; i++)
209 #define PACK(h, n) h[1] = h[n]
210 #define UNPACK(h, n) h[n] = h[1], h[n + 1] = h[1] = 0;
211 #define SQR(a) ((a) * (a))
223 float *pi_wraps, *work, phase1 = (phase > 50.f ? 100.f - phase : phase) / 50.
f;
224 int i, work_len, begin,
end, imp_peak = 0, peak = 0;
225 float imp_sum = 0, peak_imp_sum = 0;
226 float prev_angle2 = 0, cum_2pi = 0, prev_angle1 = 0, cum_1pi = 0;
228 for (i = *len, work_len = 2 * 2 * 8; i > 1; work_len <<= 1, i >>= 1);
230 work =
av_calloc(work_len + 2,
sizeof(*work));
231 pi_wraps =
av_calloc(((work_len + 2) / 2),
sizeof(*pi_wraps));
232 if (!work || !pi_wraps)
235 memcpy(work, *h, *len *
sizeof(*work));
248 for (i = 0; i <= work_len; i += 2) {
249 float angle =
atan2f(work[i + 1], work[i]);
250 float detect = 2 *
M_PI;
251 float delta = angle - prev_angle2;
252 float adjust = detect * ((delta < -detect * .7f) - (delta > detect * .7
f));
258 delta = angle - prev_angle1;
259 adjust = detect * ((delta < -detect * .7f) - (delta > detect * .7f));
261 cum_1pi += fabsf(adjust);
262 pi_wraps[i >> 1] = cum_1pi;
268 PACK(work, work_len);
271 for (i = 0; i < work_len; i++)
272 work[i] *= 2.
f / work_len;
274 for (i = 1; i < work_len / 2; i++) {
276 work[i + work_len / 2] = 0;
280 for (i = 2; i < work_len; i += 2)
281 work[i + 1] = phase1 * i / work_len * pi_wraps[work_len >> 1] + (1 - phase1) * (work[i + 1] + pi_wraps[i >> 1]) - pi_wraps[i >> 1];
283 work[0] =
exp(work[0]);
284 work[1] =
exp(work[1]);
285 for (i = 2; i < work_len; i += 2) {
286 float x =
expf(work[i]);
288 work[i ] = x *
cosf(work[i + 1]);
289 work[i + 1] = x *
sinf(work[i + 1]);
293 for (i = 0; i < work_len; i++)
294 work[i] *= 2.
f / work_len;
297 for (i = 0; i <= (int) (pi_wraps[work_len >> 1] /
M_PI + .5f); i++) {
299 if (fabs(imp_sum) > fabs(peak_imp_sum)) {
300 peak_imp_sum = imp_sum;
303 if (work[i] > work[imp_peak])
307 while (peak && fabsf(work[peak - 1]) > fabsf(work[peak]) && (work[peak - 1] * work[peak] > 0)) {
313 }
else if (phase1 == 1) {
314 begin = peak - *len / 2;
316 begin = (.997f - (2 - phase1) * .22
f) * *len + .5f;
317 end = (.997f + (0 - phase1) * .22
f) * *len + .5f;
318 begin = peak - (begin & ~3);
319 end = peak + 1 + ((end + 3) & ~3);
329 for (i = 0; i < *
len; i++) {
330 (*h)[i] = work[(begin + (phase > 50.f ? *len - 1 - i : i) + work_len) & (work_len - 1)];
332 *post_len = phase > 50 ? peak - begin : begin + *len - (peak + 1);
334 av_log(s,
AV_LOG_DEBUG,
"%d nPI=%g peak-sum@%i=%g (val@%i=%g); len=%i post=%i (%g%%)\n",
335 work_len, pi_wraps[work_len >> 1] /
M_PI, peak, peak_imp_sum, imp_peak,
336 work[imp_peak], *len, *post_len, 100.
f - 100.
f * *post_len / (*len - 1));
350 int i,
n, post_peak, longer;
355 if (s->
Fc0 >= Fn || s->
Fc1 >= Fn) {
357 "filter frequency must be less than %d/2.\n", s->
sample_rate);
371 for (i = 0; i < s->
num_taps[!longer]; i++)
372 h[longer][i + (n - s->
num_taps[!longer]) / 2] += h[!longer][i];
380 if (s->
phase != 50.f) {
394 for (i = 0; i <
n; i++)
395 s->
coeffs[i] = h[longer][i];
425 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
426 #define OFFSET(x) offsetof(SincContext, x)
431 {
"nb_samples",
"set the number of samples per requested frame",
OFFSET(nb_samples),
AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX,
AF },
432 {
"n",
"set the number of samples per requested frame",
OFFSET(nb_samples),
AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX,
AF },
439 {
"hptaps",
"set number of taps for high-pass filter",
OFFSET(num_taps[0]),
AV_OPT_TYPE_INT, {.i64=0}, 0, 32768,
AF },
440 {
"lptaps",
"set number of taps for low-pass filter",
OFFSET(num_taps[1]),
AV_OPT_TYPE_INT, {.i64=0}, 0, 32768,
AF },
448 .description =
NULL_IF_CONFIG_SMALL(
"Generate a sinc kaiser-windowed low-pass, high-pass, band-pass, or band-reject FIR coefficients."),
450 .priv_class = &sinc_class,
static const AVOption sinc_options[]
static int query_formats(AVFilterContext *ctx)
#define av_realloc_f(p, o, n)
This structure describes decoded (raw) audio or video data.
Main libavfilter public API header.
static av_cold void uninit(AVFilterContext *ctx)
static float kaiser_beta(float att, float tr_bw)
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
static int fir_to_phase(SincContext *s, float **h, int *len, int *post_len, float phase)
const char * name
Pad name.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
static av_cold int end(AVCodecContext *avctx)
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
#define AVERROR_EOF
End of file.
A filter pad used for either input or output.
A link between two filters.
static void kaiser_params(float att, float Fc, float tr_bw, float *beta, int *num_taps)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int sample_rate
samples per second
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
static float bessel_I_0(float x)
static const AVFilterPad sinc_outputs[]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
simple assert() macros that are a bit more flexible than ISO C assert().
static av_always_inline av_const double round(double x)
void av_rdft_calc(RDFTContext *s, FFTSample *data)
static int16_t mult(Float11 *f1, Float11 *f2)
static void invert(float *h, int n)
void av_rdft_end(RDFTContext *s)
RDFTContext * av_rdft_init(int nbits, enum RDFTransformType trans)
Set up a real FFT.
AVFilterContext * src
source filter
static const AVFilterPad inputs[]
static const AVFilterPad outputs[]
#define FF_ARRAY_ELEMS(a)
A list of supported channel layouts.
static int config_output(AVFilterLink *outlink)
AVSampleFormat
Audio sample formats.
AVFILTER_DEFINE_CLASS(sinc)
static float * make_lpf(int num_taps, float Fc, float beta, float rho, float scale, int dc_norm)
Describe the class of an AVClass context structure.
const char * name
Filter name.
static float * lpf(float Fn, float Fc, float tbw, int *num_taps, float att, float *beta, int round)
enum MovChannelLayoutTag * layouts
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static float safe_log(float x)
static enum AVSampleFormat sample_fmts[]
static int request_frame(AVFilterLink *outlink)
#define AV_CH_LAYOUT_MONO