29 #define RDFT_BITS_MIN 4 30 #define RDFT_BITS_MAX 16 54 #define NB_GAIN_ENTRY_MAX 4096 113 #define OFFSET(x) offsetof(FIREqualizerContext, x) 114 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM 115 #define TFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM 144 {
"min_phase",
"set minimum phase mode",
OFFSET(min_phase),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
215 if (nsamples <= s->nsamples_max) {
216 float *buf = conv_buf + idx->buf_idx * s->rdft_len;
217 float *obuf = conv_buf + !idx->buf_idx * s->rdft_len + idx->overlap_idx;
218 int center = s->fir_len/2;
221 memset(buf, 0, center *
sizeof(*data));
222 memcpy(buf + center, data, nsamples *
sizeof(*data));
223 memset(buf + center + nsamples, 0, (s->rdft_len - nsamples - center) *
sizeof(*data));
226 buf[0] *= kernel_buf[0];
227 buf[1] *= kernel_buf[s->rdft_len/2];
228 for (k = 1; k < s->rdft_len/2; k++) {
229 buf[2*k] *= kernel_buf[k];
230 buf[2*k+1] *= kernel_buf[k];
234 for (k = 0; k < s->rdft_len - idx->overlap_idx; k++)
236 memcpy(data, buf, nsamples *
sizeof(*data));
237 idx->buf_idx = !idx->buf_idx;
238 idx->overlap_idx = nsamples;
240 while (nsamples > s->nsamples_max * 2) {
241 fast_convolute(s, kernel_buf, conv_buf, idx, data, s->nsamples_max);
242 data += s->nsamples_max;
243 nsamples -= s->nsamples_max;
246 fast_convolute(s, kernel_buf, conv_buf, idx, data + nsamples/2, nsamples - nsamples/2);
251 float *av_restrict conv_buf,
OverlapIndex *av_restrict idx,
252 float *av_restrict
data,
int nsamples)
254 if (nsamples <= s->nsamples_max) {
255 float *buf = conv_buf + idx->buf_idx * s->rdft_len;
256 float *obuf = conv_buf + !idx->buf_idx * s->rdft_len + idx->overlap_idx;
259 memcpy(buf, data, nsamples *
sizeof(*data));
260 memset(buf + nsamples, 0, (s->rdft_len - nsamples) *
sizeof(*data));
263 buf[0] *= kernel_buf[0];
264 buf[1] *= kernel_buf[1];
265 for (k = 2; k < s->rdft_len; k += 2) {
267 re = buf[k] * kernel_buf[k] - buf[k+1] * kernel_buf[k+1];
268 im = buf[k] * kernel_buf[k+1] + buf[k+1] * kernel_buf[k];
274 for (k = 0; k < s->rdft_len - idx->overlap_idx; k++)
276 memcpy(data, buf, nsamples *
sizeof(*data));
277 idx->buf_idx = !idx->buf_idx;
278 idx->overlap_idx = nsamples;
280 while (nsamples > s->nsamples_max * 2) {
282 data += s->nsamples_max;
283 nsamples -= s->nsamples_max;
291 OverlapIndex *av_restrict idx,
float *av_restrict data0,
float *av_restrict data1,
int nsamples)
293 if (nsamples <= s->nsamples_max) {
294 FFTComplex *buf = conv_buf + idx->buf_idx * s->rdft_len;
295 FFTComplex *obuf = conv_buf + !idx->buf_idx * s->rdft_len + idx->overlap_idx;
296 int center = s->fir_len/2;
300 memset(buf, 0, center *
sizeof(*buf));
301 for (k = 0; k < nsamples; k++) {
302 buf[center+k].
re = data0[k];
303 buf[center+k].
im = data1[k];
305 memset(buf + center + nsamples, 0, (s->rdft_len - nsamples - center) *
sizeof(*buf));
312 buf[0].
re = 0.5f * kernel_buf[0] * buf[0].
im;
313 buf[0].
im = 0.5f * kernel_buf[0] *
tmp;
314 for (k = 1; k < s->rdft_len/2; k++) {
315 int m = s->rdft_len - k;
317 buf[k].
re = 0.5f * kernel_buf[k] * buf[k].
im;
318 buf[k].
im = 0.5f * kernel_buf[k] *
tmp;
320 buf[m].
re = 0.5f * kernel_buf[k] * buf[m].
im;
321 buf[m].
im = 0.5f * kernel_buf[k] *
tmp;
324 buf[k].
re = 0.5f * kernel_buf[k] * buf[k].
im;
325 buf[k].
im = 0.5f * kernel_buf[k] *
tmp;
330 for (k = 0; k < s->rdft_len - idx->overlap_idx; k++) {
331 buf[k].
re += obuf[k].
re;
332 buf[k].
im += obuf[k].
im;
336 for (k = 0; k < nsamples; k++) {
337 data0[k] = buf[k].
im;
338 data1[k] = buf[k].
re;
340 idx->buf_idx = !idx->buf_idx;
341 idx->overlap_idx = nsamples;
343 while (nsamples > s->nsamples_max * 2) {
344 fast_convolute2(s, kernel_buf, conv_buf, idx, data0, data1, s->nsamples_max);
345 data0 += s->nsamples_max;
346 data1 += s->nsamples_max;
347 nsamples -= s->nsamples_max;
349 fast_convolute2(s, kernel_buf, conv_buf, idx, data0, data1, nsamples/2);
350 fast_convolute2(s, kernel_buf, conv_buf, idx, data0 + nsamples/2, data1 + nsamples/2, nsamples - nsamples/2);
362 double delay = s->
zero_phase ? 0.0 : (double) center / rate;
367 for (x = 1; x <= center; x++) {
372 for (x = 0; x < s->
fir_len; x++)
379 fprintf(fp,
"# time[%d] (time amplitude)\n", ch);
382 for (x = center; x > 0; x--)
385 for (x = 0; x <= center; x++)
386 fprintf(fp,
"%15.10f %15.10f\n", delay + (
double)x / rate , (
double) s->
analysis_buf[x]);
388 for (x = 0; x < s->
fir_len; x++)
389 fprintf(fp,
"%15.10f %15.10f\n", (
double)x / rate, (
double) s->
analysis_buf[x]);
394 fprintf(fp,
"\n\n# freq[%d] (frequency desired_gain actual_gain)\n", ch);
406 ya = 20.0 * log10(
fabs(ya));
407 yb = 20.0 * log10(
fabs(yb));
409 fprintf(fp,
"%17.10f %17.10f %17.10f\n", vx, ya, yb);
447 if (*freq < entry[0].freq)
449 if (*freq > entry[1].freq)
467 if (freq <= s->gain_entry_tbl[0].freq)
477 d0 = freq - res[0].
freq;
481 return (d0 * res[1].
gain + d1 * res[0].
gain) / d;
496 double m0, m1, m2, msum, unit;
501 if (freq <= s->gain_entry_tbl[0].freq)
512 unit * (res[0].
gain - res[-1].
gain) / (res[0].freq - res[-1].freq) : 0;
515 unit * (res[2].
gain - res[1].
gain) / (res[2].freq - res[1].freq) : 0;
518 m0 = msum > 0 ? (
fabs(m0) * m1 +
fabs(m1) * m0) / msum : 0;
520 m1 = msum > 0 ? (
fabs(m1) * m2 +
fabs(m2) * m1) / msum : 0;
524 b = 3 * res[1].
gain - m1 - 2 * c - 3 * d;
525 a = res[1].
gain - b - c - d;
527 x = (freq - res[0].
freq) / unit;
531 return a * x3 + b * x2 + c * x + d;
557 double norm = 2.0 / cepstrum_len;
558 double minval = 1e-7 / rdft_len;
561 memcpy(s->
cepstrum_buf, rdft_buf, rdft_len/2 *
sizeof(*rdft_buf));
562 memcpy(s->
cepstrum_buf + cepstrum_len - rdft_len/2, rdft_buf + rdft_len/2, rdft_len/2 *
sizeof(*rdft_buf));
569 for (k = 2; k < cepstrum_len; k += 2) {
577 for (k = 1; k < cepstrum_len/2; k++)
584 for (k = 2; k < cepstrum_len; k += 2) {
592 memset(rdft_buf, 0, s->
rdft_len *
sizeof(*rdft_buf));
606 const char *gain_entry_func_names[] = {
"entry",
NULL };
607 const char *gain_func_names[] = {
"gain_interpolate",
"cubic_interpolate",
NULL };
608 double (*gain_entry_funcs[])(
void *, double, double) = {
entry_func,
NULL };
612 int ret, k, center, ch;
615 FILE *dump_fp =
NULL;
622 gain_entry_func_names, gain_entry_funcs, ctx, 0, ctx);
632 gain_func_names, gain_funcs, NULL, NULL, 0, ctx);
642 for (ch = 0; ch < inlink->
channels; ch++) {
674 for (k = 0; k <= center; k++) {
675 double u = k * (
M_PI/center);
682 win = 0.5 + 0.5 * cos(u);
685 win = 0.53836 + 0.46164 * cos(u);
688 win = 0.42 + 0.5 * cos(u) + 0.08 * cos(2*u);
691 win = 0.40897 + 0.5 * cos(u) + 0.09103 * cos(2*u);
694 win = 0.4243801 + 0.4973406 * cos(u) + 0.0782793 * cos(2*u);
697 win = 0.355768 + 0.487396 * cos(u) + 0.144232 * cos(2*u) + 0.012604 * cos(3*u);
700 win = 0.3635819 + 0.4891775 * cos(u) + 0.1365995 * cos(2*u) + 0.0106411 * cos(3*u);
703 win = 0.35875 + 0.48829 * cos(u) + 0.14128 * cos(2*u) + 0.01168 * cos(3*u);
706 win = (u <= 0.5 *
M_PI) ? 1.0 : (0.5 + 0.5 * cos(2*u -
M_PI));
724 if (
isnan(rdft_buf[k]) ||
isinf(rdft_buf[k])) {
734 rdft_buf[s->
rdft_len-1] = rdft_buf[1];
736 rdft_buf[k] = rdft_buf[2*k];
754 #define SELECT_GAIN(s) (s->gain_cmd ? s->gain_cmd : s->gain) 755 #define SELECT_GAIN_ENTRY(s) (s->gain_entry_cmd ? s->gain_entry_cmd : s->gain_entry) 790 int cepstrum_bits = rdft_bits + 2;
835 av_log(ctx,
AV_LOG_DEBUG,
"sample_rate = %d, channels = %d, analysis_rdft_len = %d, rdft_len = %d, fir_len = %d, nsamples_max = %d.\n",
857 for ( ; ch < inlink->
channels; ch++) {
863 for (ch = 0; ch < inlink->
channels; ch++) {
903 char *res,
int res_len,
int flags)
908 if (!strcmp(cmd,
"gain")) {
927 }
else if (!strcmp(cmd,
"gain_entry")) {
928 char *gain_entry_cmd;
972 .
name =
"firequalizer",
978 .
inputs = firequalizer_inputs,
979 .
outputs = firequalizer_outputs,
980 .priv_class = &firequalizer_class,
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
av_cold void av_fft_end(FFTContext *s)
#define fixed(width, name, value)
#define AV_LOG_WARNING
Something somehow does not look correct.
static void common_uninit(FIREqualizerContext *s)
Main libavfilter public API header.
static float win(SuperEqualizerContext *s, float n, int N)
int max_samples
Maximum number of samples to filter at once.
static void generate_min_phase_kernel(FIREqualizerContext *s, float *rdft_buf)
static double gain_interpolate_func(void *p, double freq)
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
static int request_frame(AVFilterLink *outlink)
void av_fft_permute(FFTContext *s, FFTComplex *z)
Do the permutation needed BEFORE calling ff_fft_calc().
static int config_input(AVFilterLink *inlink)
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
#define SELECT_GAIN_ENTRY(s)
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
#define NB_GAIN_ENTRY_MAX
static const AVFilterPad firequalizer_outputs[]
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
#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 double cubic_interpolate_func(void *p, double freq)
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
#define u(width, name, range_min, range_max)
static const char *const var_names[]
#define AVERROR_EOF
End of file.
A filter pad used for either input or output.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
A link between two filters.
int av_expr_parse_and_eval(double *d, const char *s, const char *const *const_names, const double *const_values, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), void *opaque, int log_offset, void *log_ctx)
Parse and evaluate an expression.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
int min_samples
Minimum number of samples to filter at once.
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 __device__ float fabs(float a)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
AVFILTER_DEFINE_CLASS(firequalizer)
RDFTContext * cepstrum_rdft
void * priv
private data for use by the filter
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link...
simple assert() macros that are a bit more flexible than ISO C assert().
FFTContext * av_fft_init(int nbits, int inverse)
Set up a complex FFT.
RDFTContext * cepstrum_irdft
RDFTContext * analysis_irdft
static av_cold void uninit(AVFilterContext *ctx)
static const AVOption firequalizer_options[]
void av_rdft_calc(RDFTContext *s, FFTSample *data)
static const AVFilterPad firequalizer_inputs[]
static void fast_convolute2(FIREqualizerContext *av_restrict s, const float *av_restrict kernel_buf, FFTComplex *av_restrict conv_buf, OverlapIndex *av_restrict idx, float *av_restrict data0, float *av_restrict data1, int nsamples)
static void fast_convolute_nonlinear(FIREqualizerContext *av_restrict s, const float *av_restrict kernel_buf, float *av_restrict conv_buf, OverlapIndex *av_restrict idx, float *av_restrict data, int nsamples)
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
void av_rdft_end(RDFTContext *s)
AVFilterContext * src
source filter
RDFTContext * av_rdft_init(int nbits, enum RDFTransformType trans)
Set up a real FFT.
int partial_buf_size
Size of the partial buffer to allocate.
static const AVFilterPad outputs[]
A list of supported channel layouts.
static double entry_func(void *p, double freq, double gain)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
static const uint8_t vars[2][12]
char * av_strdup(const char *s)
Duplicate a string.
AVSampleFormat
Audio sample formats.
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
AVFilter ff_af_firequalizer
static AVRational av_make_q(int num, int den)
Create an AVRational.
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static void fast_convolute(FIREqualizerContext *av_restrict s, const float *av_restrict kernel_buf, float *av_restrict conv_buf, OverlapIndex *av_restrict idx, float *av_restrict data, int nsamples)
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Describe the class of an AVClass context structure.
GainEntry gain_entry_tbl[NB_GAIN_ENTRY_MAX]
const char * name
Filter name.
AVFilterLink ** outputs
array of pointers to output links
enum MovChannelLayoutTag * layouts
static void dump_fir(AVFilterContext *ctx, FILE *fp, int ch)
#define flags(name, subs,...)
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
RDFTContext * analysis_rdft
uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
Get the channel with the given index in channel_layout.
static void fft2(FFTComplex *z)
static int query_formats(AVFilterContext *ctx)
static int generate_kernel(AVFilterContext *ctx, const char *gain, const char *gain_entry)
uint64_t channel_layout
channel layout of current buffer (see libavutil/channel_layout.h)
int channels
Number of channels.
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
AVFilterContext * dst
dest filter
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
and forward the result(frame or status change) to the corresponding input.If nothing is possible
static enum AVSampleFormat sample_fmts[]
#define av_malloc_array(a, b)
static int gain_entry_compare(const void *key, const void *memb)
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
uint8_t ** extended_data
pointers to the data planes/channels.
void av_fft_calc(FFTContext *s, FFTComplex *z)
Do a complex FFT with the parameters defined in av_fft_init().
int nb_samples
number of audio samples (per channel) described by this frame
#define AV_NOPTS_VALUE
Undefined timestamp value.
simple arithmetic expression evaluator