FFmpeg
af_superequalizer.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002 Naoki Shibata
3  * Copyright (c) 2017 Paul B Mahol
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 "libavutil/opt.h"
23 
24 #include "libavcodec/avfft.h"
25 
26 #include "audio.h"
27 #include "avfilter.h"
28 #include "filters.h"
29 #include "internal.h"
30 
31 #define NBANDS 17
32 #define M 15
33 
34 typedef struct EqParameter {
35  float lower, upper, gain;
36 } EqParameter;
37 
38 typedef struct SuperEqualizerContext {
39  const AVClass *class;
40 
42 
43  float gains[NBANDS + 1];
44 
45  float fact[M + 1];
46  float aa;
47  float iza;
48  float *ires, *irest;
49  float *fsamples;
51 
55 
56 static const float bands[] = {
57  65.406392, 92.498606, 130.81278, 184.99721, 261.62557, 369.99442, 523.25113, 739.9884, 1046.5023,
58  1479.9768, 2093.0045, 2959.9536, 4186.0091, 5919.9072, 8372.0181, 11839.814, 16744.036
59 };
60 
61 static float izero(SuperEqualizerContext *s, float x)
62 {
63  float ret = 1;
64  int m;
65 
66  for (m = 1; m <= M; m++) {
67  float t;
68 
69  t = pow(x / 2, m) / s->fact[m];
70  ret += t*t;
71  }
72 
73  return ret;
74 }
75 
76 static float hn_lpf(int n, float f, float fs)
77 {
78  float t = 1 / fs;
79  float omega = 2 * M_PI * f;
80 
81  if (n * omega * t == 0)
82  return 2 * f * t;
83  return 2 * f * t * sinf(n * omega * t) / (n * omega * t);
84 }
85 
86 static float hn_imp(int n)
87 {
88  return n == 0 ? 1.f : 0.f;
89 }
90 
91 static float hn(int n, EqParameter *param, float fs)
92 {
93  float ret, lhn;
94  int i;
95 
96  lhn = hn_lpf(n, param[0].upper, fs);
97  ret = param[0].gain*lhn;
98 
99  for (i = 1; i < NBANDS + 1 && param[i].upper < fs / 2; i++) {
100  float lhn2 = hn_lpf(n, param[i].upper, fs);
101  ret += param[i].gain * (lhn2 - lhn);
102  lhn = lhn2;
103  }
104 
105  ret += param[i].gain * (hn_imp(n) - lhn);
106 
107  return ret;
108 }
109 
110 static float alpha(float a)
111 {
112  if (a <= 21)
113  return 0;
114  if (a <= 50)
115  return .5842f * pow(a - 21, 0.4f) + 0.07886f * (a - 21);
116  return .1102f * (a - 8.7f);
117 }
118 
119 static float win(SuperEqualizerContext *s, float n, int N)
120 {
121  return izero(s, alpha(s->aa) * sqrtf(1 - 4 * n * n / ((N - 1) * (N - 1)))) / s->iza;
122 }
123 
124 static void process_param(float *bc, EqParameter *param, float fs)
125 {
126  int i;
127 
128  for (i = 0; i <= NBANDS; i++) {
129  param[i].lower = i == 0 ? 0 : bands[i - 1];
130  param[i].upper = i == NBANDS ? fs : bands[i];
131  param[i].gain = bc[i];
132  }
133 }
134 
135 static int equ_init(SuperEqualizerContext *s, int wb)
136 {
137  int i,j;
138 
139  s->rdft = av_rdft_init(wb, DFT_R2C);
140  s->irdft = av_rdft_init(wb, IDFT_C2R);
141  if (!s->rdft || !s->irdft)
142  return AVERROR(ENOMEM);
143 
144  s->aa = 96;
145  s->winlen = (1 << (wb-1))-1;
146  s->tabsize = 1 << wb;
147 
148  s->ires = av_calloc(s->tabsize, sizeof(float));
149  s->irest = av_calloc(s->tabsize, sizeof(float));
150  s->fsamples = av_calloc(s->tabsize, sizeof(float));
151 
152  for (i = 0; i <= M; i++) {
153  s->fact[i] = 1;
154  for (j = 1; j <= i; j++)
155  s->fact[i] *= j;
156  }
157 
158  s->iza = izero(s, alpha(s->aa));
159 
160  return 0;
161 }
162 
163 static void make_fir(SuperEqualizerContext *s, float *lbc, float *rbc, EqParameter *param, float fs)
164 {
165  const int winlen = s->winlen;
166  const int tabsize = s->tabsize;
167  float *nires;
168  int i;
169 
170  if (fs <= 0)
171  return;
172 
173  process_param(lbc, param, fs);
174  for (i = 0; i < winlen; i++)
175  s->irest[i] = hn(i - winlen / 2, param, fs) * win(s, i - winlen / 2, winlen);
176  for (; i < tabsize; i++)
177  s->irest[i] = 0;
178 
179  av_rdft_calc(s->rdft, s->irest);
180  nires = s->ires;
181  for (i = 0; i < tabsize; i++)
182  nires[i] = s->irest[i];
183 }
184 
186 {
187  AVFilterContext *ctx = inlink->dst;
188  SuperEqualizerContext *s = ctx->priv;
189  AVFilterLink *outlink = ctx->outputs[0];
190  const float *ires = s->ires;
191  float *fsamples = s->fsamples;
192  int ch, i;
193 
194  AVFrame *out = ff_get_audio_buffer(outlink, s->winlen);
195  float *src, *dst, *ptr;
196 
197  if (!out) {
198  av_frame_free(&in);
199  return AVERROR(ENOMEM);
200  }
201 
202  for (ch = 0; ch < in->channels; ch++) {
203  ptr = (float *)out->extended_data[ch];
204  dst = (float *)s->out->extended_data[ch];
205  src = (float *)in->extended_data[ch];
206 
207  for (i = 0; i < in->nb_samples; i++)
208  fsamples[i] = src[i];
209  for (; i < s->tabsize; i++)
210  fsamples[i] = 0;
211 
212  av_rdft_calc(s->rdft, fsamples);
213 
214  fsamples[0] = ires[0] * fsamples[0];
215  fsamples[1] = ires[1] * fsamples[1];
216  for (i = 1; i < s->tabsize / 2; i++) {
217  float re, im;
218 
219  re = ires[i*2 ] * fsamples[i*2] - ires[i*2+1] * fsamples[i*2+1];
220  im = ires[i*2+1] * fsamples[i*2] + ires[i*2 ] * fsamples[i*2+1];
221 
222  fsamples[i*2 ] = re;
223  fsamples[i*2+1] = im;
224  }
225 
226  av_rdft_calc(s->irdft, fsamples);
227 
228  for (i = 0; i < s->winlen; i++)
229  dst[i] += fsamples[i] / s->tabsize * 2;
230  for (i = s->winlen; i < s->tabsize; i++)
231  dst[i] = fsamples[i] / s->tabsize * 2;
232  for (i = 0; i < s->winlen; i++)
233  ptr[i] = dst[i];
234  for (i = 0; i < s->winlen; i++)
235  dst[i] = dst[i+s->winlen];
236  }
237 
238  out->pts = in->pts;
239  av_frame_free(&in);
240 
241  return ff_filter_frame(outlink, out);
242 }
243 
245 {
246  AVFilterLink *inlink = ctx->inputs[0];
247  AVFilterLink *outlink = ctx->outputs[0];
248  SuperEqualizerContext *s = ctx->priv;
249  AVFrame *in = NULL;
250  int ret;
251 
253 
254  ret = ff_inlink_consume_samples(inlink, s->winlen, s->winlen, &in);
255  if (ret < 0)
256  return ret;
257  if (ret > 0)
258  return filter_frame(inlink, in);
259 
262 
263  return FFERROR_NOT_READY;
264 }
265 
267 {
268  SuperEqualizerContext *s = ctx->priv;
269 
270  return equ_init(s, 14);
271 }
272 
274 {
275  AVFilterContext *ctx = inlink->dst;
276  SuperEqualizerContext *s = ctx->priv;
277 
278  s->out = ff_get_audio_buffer(inlink, s->tabsize);
279  if (!s->out)
280  return AVERROR(ENOMEM);
281 
282  return 0;
283 }
284 
285 static int config_output(AVFilterLink *outlink)
286 {
287  AVFilterContext *ctx = outlink->src;
288  SuperEqualizerContext *s = ctx->priv;
289 
290  make_fir(s, s->gains, s->gains, s->params, outlink->sample_rate);
291 
292  return 0;
293 }
294 
296 {
297  SuperEqualizerContext *s = ctx->priv;
298 
299  av_frame_free(&s->out);
300  av_freep(&s->irest);
301  av_freep(&s->ires);
302  av_freep(&s->fsamples);
303  av_rdft_end(s->rdft);
304  av_rdft_end(s->irdft);
305 }
306 
308  {
309  .name = "default",
310  .type = AVMEDIA_TYPE_AUDIO,
311  .config_props = config_input,
312  },
313 };
314 
316  {
317  .name = "default",
318  .type = AVMEDIA_TYPE_AUDIO,
319  .config_props = config_output,
320  },
321 };
322 
323 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
324 #define OFFSET(x) offsetof(SuperEqualizerContext, x)
325 
327  { "1b", "set 65Hz band gain", OFFSET(gains [0]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
328  { "2b", "set 92Hz band gain", OFFSET(gains [1]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
329  { "3b", "set 131Hz band gain", OFFSET(gains [2]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
330  { "4b", "set 185Hz band gain", OFFSET(gains [3]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
331  { "5b", "set 262Hz band gain", OFFSET(gains [4]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
332  { "6b", "set 370Hz band gain", OFFSET(gains [5]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
333  { "7b", "set 523Hz band gain", OFFSET(gains [6]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
334  { "8b", "set 740Hz band gain", OFFSET(gains [7]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
335  { "9b", "set 1047Hz band gain", OFFSET(gains [8]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
336  { "10b", "set 1480Hz band gain", OFFSET(gains [9]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
337  { "11b", "set 2093Hz band gain", OFFSET(gains[10]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
338  { "12b", "set 2960Hz band gain", OFFSET(gains[11]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
339  { "13b", "set 4186Hz band gain", OFFSET(gains[12]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
340  { "14b", "set 5920Hz band gain", OFFSET(gains[13]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
341  { "15b", "set 8372Hz band gain", OFFSET(gains[14]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
342  { "16b", "set 11840Hz band gain", OFFSET(gains[15]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
343  { "17b", "set 16744Hz band gain", OFFSET(gains[16]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
344  { "18b", "set 20000Hz band gain", OFFSET(gains[17]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 20, AF },
345  { NULL }
346 };
347 
348 AVFILTER_DEFINE_CLASS(superequalizer);
349 
351  .name = "superequalizer",
352  .description = NULL_IF_CONFIG_SMALL("Apply 18 band equalization filter."),
353  .priv_size = sizeof(SuperEqualizerContext),
354  .priv_class = &superequalizer_class,
355  .init = init,
356  .activate = activate,
357  .uninit = uninit,
361 };
hn_lpf
static float hn_lpf(int n, float f, float fs)
Definition: af_superequalizer.c:76
ff_get_audio_buffer
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:88
SuperEqualizerContext::fsamples
float * fsamples
Definition: af_superequalizer.c:49
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_superequalizer.c:295
superequalizer_options
static const AVOption superequalizer_options[]
Definition: af_superequalizer.c:326
SuperEqualizerContext::in
AVFrame * in
Definition: af_superequalizer.c:52
AVERROR
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
opt.h
SuperEqualizerContext::irdft
RDFTContext * irdft
Definition: af_superequalizer.c:53
out
FILE * out
Definition: movenc.c:54
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
alpha
static float alpha(float a)
Definition: af_superequalizer.c:110
FILTER_SINGLE_SAMPLEFMT
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition: internal.h:184
inlink
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
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:112
im
float im
Definition: fft.c:78
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:396
AVOption
AVOption.
Definition: opt.h:247
SuperEqualizerContext
Definition: af_superequalizer.c:38
SuperEqualizerContext::params
EqParameter params[NBANDS+1]
Definition: af_superequalizer.c:41
AF
#define AF
Definition: af_superequalizer.c:323
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:153
ff_af_superequalizer
const AVFilter ff_af_superequalizer
Definition: af_superequalizer.c:350
make_fir
static void make_fir(SuperEqualizerContext *s, float *lbc, float *rbc, EqParameter *param, float fs)
Definition: af_superequalizer.c:163
FF_FILTER_FORWARD_STATUS_BACK
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:199
SuperEqualizerContext::irest
float * irest
Definition: af_superequalizer.c:48
win
static float win(SuperEqualizerContext *s, float n, int N)
Definition: af_superequalizer.c:119
SuperEqualizerContext::ires
float * ires
Definition: af_superequalizer.c:48
IDFT_C2R
@ IDFT_C2R
Definition: avfft.h:73
SuperEqualizerContext::fact
float fact[M+1]
Definition: af_superequalizer.c:45
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
av_cold
#define av_cold
Definition: attributes.h:90
SuperEqualizerContext::winlen
int winlen
Definition: af_superequalizer.c:50
init
static av_cold int init(AVFilterContext *ctx)
Definition: af_superequalizer.c:266
config_output
static int config_output(AVFilterLink *outlink)
Definition: af_superequalizer.c:285
NBANDS
#define NBANDS
Definition: af_superequalizer.c:31
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVFrame::channels
int channels
number of audio channels, only used for audio.
Definition: frame.h:592
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
process_param
static void process_param(float *bc, EqParameter *param, float fs)
Definition: af_superequalizer.c:124
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:48
bands
static const float bands[]
Definition: af_superequalizer.c:56
superequalizer_outputs
static const AVFilterPad superequalizer_outputs[]
Definition: af_superequalizer.c:315
f
#define f(width, name)
Definition: cbs_vp9.c:255
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
av_rdft_calc
void av_rdft_calc(RDFTContext *s, FFTSample *data)
izero
static float izero(SuperEqualizerContext *s, float x)
Definition: af_superequalizer.c:61
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
ff_inlink_consume_samples
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
Definition: avfilter.c:1436
NULL
#define NULL
Definition: coverity.c:32
fs
#define fs(width, name, subs,...)
Definition: cbs_vp9.c:259
src
#define src
Definition: vp8dsp.c:255
OFFSET
#define OFFSET(x)
Definition: af_superequalizer.c:324
SuperEqualizerContext::out
AVFrame * out
Definition: af_superequalizer.c:52
DFT_R2C
@ DFT_R2C
Definition: avfft.h:72
avfft.h
sinf
#define sinf(x)
Definition: libm.h:419
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
av_rdft_init
RDFTContext * av_rdft_init(int nbits, enum RDFTransformType trans)
Set up a real FFT.
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
hn
static float hn(int n, EqParameter *param, float fs)
Definition: af_superequalizer.c:91
EqParameter::gain
float gain
Definition: af_superequalizer.c:35
SuperEqualizerContext::tabsize
int tabsize
Definition: af_superequalizer.c:50
EqParameter::lower
float lower
Definition: af_superequalizer.c:35
equ_init
static int equ_init(SuperEqualizerContext *s, int wb)
Definition: af_superequalizer.c:135
a
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
Definition: undefined.txt:41
SuperEqualizerContext::iza
float iza
Definition: af_superequalizer.c:47
FF_FILTER_FORWARD_WANTED
FF_FILTER_FORWARD_WANTED(outlink, inlink)
N
#define N
Definition: af_mcompand.c:53
superequalizer_inputs
static const AVFilterPad superequalizer_inputs[]
Definition: af_superequalizer.c:307
M_PI
#define M_PI
Definition: mathematics.h:52
internal.h
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:227
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:369
EqParameter::upper
float upper
Definition: af_superequalizer.c:35
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
M
#define M
Definition: af_superequalizer.c:32
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:350
SuperEqualizerContext::gains
float gains[NBANDS+1]
Definition: af_superequalizer.c:43
RDFTContext
Definition: rdft.h:28
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:271
AVFilter
Filter definition.
Definition: avfilter.h:149
SuperEqualizerContext::rdft
RDFTContext * rdft
Definition: af_superequalizer.c:53
ret
ret
Definition: filter_design.txt:187
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_superequalizer.c:273
SuperEqualizerContext::aa
float aa
Definition: af_superequalizer.c:46
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:386
audio.h
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_superequalizer.c:185
FF_FILTER_FORWARD_STATUS
FF_FILTER_FORWARD_STATUS(inlink, outlink)
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
EqParameter
Definition: af_superequalizer.c:34
av_rdft_end
void av_rdft_end(RDFTContext *s)
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(superequalizer)
hn_imp
static float hn_imp(int n)
Definition: af_superequalizer.c:86
activate
static int activate(AVFilterContext *ctx)
Definition: af_superequalizer.c:244
re
float re
Definition: fft.c:78