FFmpeg
af_aexciter.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) Markus Schmidt and Christian Holschuh
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/opt.h"
22 #include "avfilter.h"
23 #include "internal.h"
24 #include "audio.h"
25 
26 typedef struct ChannelParams {
28  double rdrive, rbdr, kpa, kpb, kna, knb, ap,
29  an, imr, kc, srct, sq, pwrq;
30  double prev_med, prev_out;
31 
32  double hp[5], lp[5];
33  double hw[4][2], lw[2][2];
35 
36 typedef struct AExciterContext {
37  const AVClass *class;
38 
39  double level_in;
40  double level_out;
41  double amount;
42  double drive;
43  double blend;
44  double freq;
45  double ceil;
46  int listen;
47 
50 
51 #define OFFSET(x) offsetof(AExciterContext, x)
52 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
53 
54 static const AVOption aexciter_options[] = {
55  { "level_in", "set level in", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, A },
56  { "level_out", "set level out", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, A },
57  { "amount", "set amount", OFFSET(amount), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, A },
58  { "drive", "set harmonics", OFFSET(drive), AV_OPT_TYPE_DOUBLE, {.dbl=8.5}, 0.1, 10, A },
59  { "blend", "set blend harmonics", OFFSET(blend), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -10, 10, A },
60  { "freq", "set scope", OFFSET(freq), AV_OPT_TYPE_DOUBLE, {.dbl=7500}, 2000, 12000, A },
61  { "ceil", "set ceiling", OFFSET(ceil), AV_OPT_TYPE_DOUBLE, {.dbl=9999}, 9999, 20000, A },
62  { "listen", "enable listen mode", OFFSET(listen), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
63  { NULL }
64 };
65 
66 AVFILTER_DEFINE_CLASS(aexciter);
67 
68 static inline double M(double x)
69 {
70  return (fabs(x) > 0.00000001) ? x : 0.0;
71 }
72 
73 static inline double D(double x)
74 {
75  x = fabs(x);
76 
77  return (x > 0.00000001) ? sqrt(x) : 0.0;
78 }
79 
80 static void set_params(ChannelParams *p,
81  double blend, double drive,
82  double srate, double freq,
83  double ceil)
84 {
85  double a0, a1, a2, b0, b1, b2, w0, alpha;
86 
87  p->rdrive = 12.0 / drive;
88  p->rbdr = p->rdrive / (10.5 - blend) * 780.0 / 33.0;
89  p->kpa = D(2.0 * (p->rdrive*p->rdrive) - 1.0) + 1.0;
90  p->kpb = (2.0 - p->kpa) / 2.0;
91  p->ap = ((p->rdrive*p->rdrive) - p->kpa + 1.0) / 2.0;
92  p->kc = p->kpa / D(2.0 * D(2.0 * (p->rdrive*p->rdrive) - 1.0) - 2.0 * p->rdrive*p->rdrive);
93 
94  p->srct = (0.1 * srate) / (0.1 * srate + 1.0);
95  p->sq = p->kc*p->kc + 1.0;
96  p->knb = -1.0 * p->rbdr / D(p->sq);
97  p->kna = 2.0 * p->kc * p->rbdr / D(p->sq);
98  p->an = p->rbdr*p->rbdr / p->sq;
99  p->imr = 2.0 * p->knb + D(2.0 * p->kna + 4.0 * p->an - 1.0);
100  p->pwrq = 2.0 / (p->imr + 1.0);
101 
102  w0 = 2 * M_PI * freq / srate;
103  alpha = sin(w0) / (2. * 0.707);
104  a0 = 1 + alpha;
105  a1 = -2 * cos(w0);
106  a2 = 1 - alpha;
107  b0 = (1 + cos(w0)) / 2;
108  b1 = -(1 + cos(w0));
109  b2 = (1 + cos(w0)) / 2;
110 
111  p->hp[0] =-a1 / a0;
112  p->hp[1] =-a2 / a0;
113  p->hp[2] = b0 / a0;
114  p->hp[3] = b1 / a0;
115  p->hp[4] = b2 / a0;
116 
117  w0 = 2 * M_PI * ceil / srate;
118  alpha = sin(w0) / (2. * 0.707);
119  a0 = 1 + alpha;
120  a1 = -2 * cos(w0);
121  a2 = 1 - alpha;
122  b0 = (1 - cos(w0)) / 2;
123  b1 = 1 - cos(w0);
124  b2 = (1 - cos(w0)) / 2;
125 
126  p->lp[0] =-a1 / a0;
127  p->lp[1] =-a2 / a0;
128  p->lp[2] = b0 / a0;
129  p->lp[3] = b1 / a0;
130  p->lp[4] = b2 / a0;
131 }
132 
133 static double bprocess(double in, const double *const c,
134  double *w1, double *w2)
135 {
136  double out = c[2] * in + *w1;
137 
138  *w1 = c[3] * in + *w2 + c[0] * out;
139  *w2 = c[4] * in + c[1] * out;
140 
141  return out;
142 }
143 
144 static double distortion_process(AExciterContext *s, ChannelParams *p, double in)
145 {
146  double proc = in, med;
147 
148  proc = bprocess(proc, p->hp, &p->hw[0][0], &p->hw[0][1]);
149  proc = bprocess(proc, p->hp, &p->hw[1][0], &p->hw[1][1]);
150 
151  if (proc >= 0.0) {
152  med = (D(p->ap + proc * (p->kpa - proc)) + p->kpb) * p->pwrq;
153  } else {
154  med = (D(p->an - proc * (p->kna + proc)) + p->knb) * p->pwrq * -1.0;
155  }
156 
157  proc = p->srct * (med - p->prev_med + p->prev_out);
158  p->prev_med = M(med);
159  p->prev_out = M(proc);
160 
161  proc = bprocess(proc, p->hp, &p->hw[2][0], &p->hw[2][1]);
162  proc = bprocess(proc, p->hp, &p->hw[3][0], &p->hw[3][1]);
163 
164  if (s->ceil >= 10000.) {
165  proc = bprocess(proc, p->lp, &p->lw[0][0], &p->lw[0][1]);
166  proc = bprocess(proc, p->lp, &p->lw[1][0], &p->lw[1][1]);
167  }
168 
169  return proc;
170 }
171 
173 {
174  AVFilterContext *ctx = inlink->dst;
175  AExciterContext *s = ctx->priv;
176  AVFilterLink *outlink = ctx->outputs[0];
177  AVFrame *out;
178  const double *src = (const double *)in->data[0];
179  const double level_in = s->level_in;
180  const double level_out = s->level_out;
181  const double amount = s->amount;
182  const double listen = 1.0 - s->listen;
183  double *dst;
184 
185  if (av_frame_is_writable(in)) {
186  out = in;
187  } else {
189  if (!out) {
190  av_frame_free(&in);
191  return AVERROR(ENOMEM);
192  }
194  }
195 
196  dst = (double *)out->data[0];
197  for (int n = 0; n < in->nb_samples; n++) {
198  for (int c = 0; c < inlink->channels; c++) {
199  double sample = src[c] * level_in;
200 
201  sample = distortion_process(s, &s->cp[c], sample);
202  sample = sample * amount + listen * src[c];
203 
204  sample *= level_out;
205  if (ctx->is_disabled)
206  dst[c] = src[c];
207  else
208  dst[c] = sample;
209  }
210 
211  src += inlink->channels;
212  dst += inlink->channels;
213  }
214 
215  if (in != out)
216  av_frame_free(&in);
217 
218  return ff_filter_frame(outlink, out);
219 }
220 
222 {
225  static const enum AVSampleFormat sample_fmts[] = {
228  };
229  int ret;
230 
232  if (!layouts)
233  return AVERROR(ENOMEM);
235  if (ret < 0)
236  return ret;
237 
239  if (!formats)
240  return AVERROR(ENOMEM);
242  if (ret < 0)
243  return ret;
244 
246  if (!formats)
247  return AVERROR(ENOMEM);
249 }
250 
252 {
253  AExciterContext *s = ctx->priv;
254 
255  av_freep(&s->cp);
256 }
257 
259 {
260  AVFilterContext *ctx = inlink->dst;
261  AExciterContext *s = ctx->priv;
262 
263  if (!s->cp)
264  s->cp = av_calloc(inlink->channels, sizeof(*s->cp));
265  if (!s->cp)
266  return AVERROR(ENOMEM);
267 
268  for (int i = 0; i < inlink->channels; i++)
269  set_params(&s->cp[i], s->blend, s->drive, inlink->sample_rate,
270  s->freq, s->ceil);
271 
272  return 0;
273 }
274 
275 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
276  char *res, int res_len, int flags)
277 {
278  AVFilterLink *inlink = ctx->inputs[0];
279  int ret;
280 
281  ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
282  if (ret < 0)
283  return ret;
284 
285  return config_input(inlink);
286 }
287 
289  {
290  .name = "default",
291  .type = AVMEDIA_TYPE_AUDIO,
292  .config_props = config_input,
293  .filter_frame = filter_frame,
294  },
295  { NULL }
296 };
297 
299  {
300  .name = "default",
301  .type = AVMEDIA_TYPE_AUDIO,
302  },
303  { NULL }
304 };
305 
307  .name = "aexciter",
308  .description = NULL_IF_CONFIG_SMALL("Enhance high frequency part of audio."),
309  .priv_size = sizeof(AExciterContext),
310  .priv_class = &aexciter_class,
311  .uninit = uninit,
317 };
formats
formats
Definition: signature.h:48
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:86
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:85
AExciterContext
Definition: af_aexciter.c:36
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
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
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:978
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:948
layouts
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_aexciter.c:172
A
#define A
Definition: af_aexciter.c:52
ChannelParams::hw
double hw[4][2]
Definition: af_aexciter.c:33
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
ff_all_channel_counts
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition.
Definition: formats.c:429
ChannelParams::srct
double srct
Definition: af_aexciter.c:29
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
AExciterContext::drive
double drive
Definition: af_aexciter.c:42
AVOption
AVOption.
Definition: opt.h:247
ChannelParams::prev_med
double prev_med
Definition: af_aexciter.c:30
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:149
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:317
ChannelParams::pwrq
double pwrq
Definition: af_aexciter.c:29
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
b1
static double b1(void *priv, double x, double y)
Definition: vf_xfade.c:1665
AExciterContext::ceil
double ceil
Definition: af_aexciter.c:45
ChannelParams::kna
double kna
Definition: af_aexciter.c:28
AExciterContext::amount
double amount
Definition: af_aexciter.c:41
AExciterContext::level_in
double level_in
Definition: af_aexciter.c:39
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
a1
#define a1
Definition: regdef.h:47
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_aexciter.c:251
ceil
static __device__ float ceil(float a)
Definition: cuda_runtime.h:176
av_cold
#define av_cold
Definition: attributes.h:90
inputs
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
Definition: filter_design.txt:243
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:580
ChannelParams::lp
double lp[5]
Definition: af_aexciter.c:32
ChannelParams::rbdr
double rbdr
Definition: af_aexciter.c:28
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:226
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
set_params
static void set_params(ChannelParams *p, double blend, double drive, double srate, double freq, double ceil)
Definition: af_aexciter.c:80
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
ChannelParams::hp
double hp[5]
Definition: af_aexciter.c:32
ctx
AVFormatContext * ctx
Definition: movenc.c:48
process_command
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: af_aexciter.c:275
if
if(ret)
Definition: filter_design.txt:179
ChannelParams::blend_old
double blend_old
Definition: af_aexciter.c:27
ff_af_aexciter
const AVFilter ff_af_aexciter
Definition: af_aexciter.c:306
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
avfilter_af_aexciter_inputs
static const AVFilterPad avfilter_af_aexciter_inputs[]
Definition: af_aexciter.c:288
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:537
AExciterContext::cp
ChannelParams * cp
Definition: af_aexciter.c:48
src
#define src
Definition: vp8dsp.c:255
ChannelParams::drive_old
double drive_old
Definition: af_aexciter.c:27
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_aexciter.c:221
c
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
Definition: undefined.txt:32
AExciterContext::listen
int listen
Definition: af_aexciter.c:46
D
static double D(double x)
Definition: af_aexciter.c:73
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
OFFSET
#define OFFSET(x)
Definition: af_aexciter.c:51
ChannelParams::rdrive
double rdrive
Definition: af_aexciter.c:28
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:116
avfilter_af_aexciter_outputs
static const AVFilterPad avfilter_af_aexciter_outputs[]
Definition: af_aexciter.c:298
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
sample
#define sample
Definition: flacdsp_template.c:44
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:473
ChannelParams::lw
double lw[2][2]
Definition: af_aexciter.c:33
ff_filter_process_command
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition: avfilter.c:842
ChannelParams::kc
double kc
Definition: af_aexciter.c:29
ChannelParams::kpa
double kpa
Definition: af_aexciter.c:28
b2
static double b2(void *priv, double x, double y)
Definition: vf_xfade.c:1666
M
static double M(double x)
Definition: af_aexciter.c:68
a0
#define a0
Definition: regdef.h:46
M_PI
#define M_PI
Definition: mathematics.h:52
internal.h
ChannelParams::kpb
double kpb
Definition: af_aexciter.c:28
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_aexciter.c:258
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:369
i
int i
Definition: input.c:406
ChannelParams::imr
double imr
Definition: af_aexciter.c:29
a2
#define a2
Definition: regdef.h:48
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
ChannelParams::ap
double ap
Definition: af_aexciter.c:28
ChannelParams
sample data coding information
Definition: mlp.h:83
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
ChannelParams::an
double an
Definition: af_aexciter.c:29
AExciterContext::blend
double blend
Definition: af_aexciter.c:43
AVFilter
Filter definition.
Definition: avfilter.h:145
ret
ret
Definition: filter_design.txt:187
ChannelParams::sq
double sq
Definition: af_aexciter.c:29
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(aexciter)
ChannelParams::prev_out
double prev_out
Definition: af_aexciter.c:30
ff_all_samplerates
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:414
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:251
aexciter_options
static const AVOption aexciter_options[]
Definition: af_aexciter.c:54
avfilter.h
AExciterContext::level_out
double level_out
Definition: af_aexciter.c:40
AVFilterContext
An instance of a filter.
Definition: avfilter.h:333
ChannelParams::knb
double knb
Definition: af_aexciter.c:28
distortion_process
static double distortion_process(AExciterContext *s, ChannelParams *p, double in)
Definition: af_aexciter.c:144
audio.h
AExciterContext::freq
double freq
Definition: af_aexciter.c:44
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:241
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition: avfilter.h:134
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
ff_set_common_samplerates
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:568
b0
static double b0(void *priv, double x, double y)
Definition: vf_xfade.c:1664
AV_SAMPLE_FMT_DBL
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:64
bprocess
static double bprocess(double in, const double *const c, double *w1, double *w2)
Definition: af_aexciter.c:133
ff_set_common_channel_layouts
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *channel_layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates.
Definition: formats.c:561