FFmpeg
Loading...
Searching...
No Matches
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/mem.h"
22#include "libavutil/opt.h"
23#include "avfilter.h"
24#include "filters.h"
25#include "audio.h"
26
27typedef struct ChannelParams {
29 double rdrive, rbdr, kpa, kpb, kna, knb, ap,
32
33 double hp[5], lp[5];
34 double hw[4][2], lw[2][2];
36
37typedef struct AExciterContext {
38 const AVClass *class;
39
40 double level_in;
41 double level_out;
42 double amount;
43 double drive;
44 double blend;
45 double freq;
46 double ceil;
47 int listen;
48
51
52#define OFFSET(x) offsetof(AExciterContext, x)
53#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
54
55static const AVOption aexciter_options[] = {
56 { "level_in", "set level in", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, A },
57 { "level_out", "set level out", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, A },
58 { "amount", "set amount", OFFSET(amount), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, A },
59 { "drive", "set harmonics", OFFSET(drive), AV_OPT_TYPE_DOUBLE, {.dbl=8.5}, 0.1, 10, A },
60 { "blend", "set blend harmonics", OFFSET(blend), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -10, 10, A },
61 { "freq", "set scope", OFFSET(freq), AV_OPT_TYPE_DOUBLE, {.dbl=7500}, 2000, 12000, A },
62 { "ceil", "set ceiling", OFFSET(ceil), AV_OPT_TYPE_DOUBLE, {.dbl=9999}, 9999, 20000, A },
63 { "listen", "enable listen mode", OFFSET(listen), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
64 { NULL }
65};
66
68
69static inline double M(double x)
70{
71 return (fabs(x) > 0.00000001) ? x : 0.0;
72}
73
74static inline double D(double x)
75{
76 x = fabs(x);
77
78 return (x > 0.00000001) ? sqrt(x) : 0.0;
79}
80
82 double blend, double drive,
83 double srate, double freq,
84 double ceil)
85{
86 double a0, a1, a2, b0, b1, b2, w0, alpha;
87
88 p->rdrive = 12.0 / drive;
89 p->rbdr = p->rdrive / (10.5 - blend) * 780.0 / 33.0;
90 p->kpa = D(2.0 * (p->rdrive*p->rdrive) - 1.0) + 1.0;
91 p->kpb = (2.0 - p->kpa) / 2.0;
92 p->ap = ((p->rdrive*p->rdrive) - p->kpa + 1.0) / 2.0;
93 p->kc = p->kpa / D(2.0 * D(2.0 * (p->rdrive*p->rdrive) - 1.0) - 2.0 * p->rdrive*p->rdrive);
94
95 p->srct = (0.1 * srate) / (0.1 * srate + 1.0);
96 p->sq = p->kc*p->kc + 1.0;
97 p->knb = -1.0 * p->rbdr / D(p->sq);
98 p->kna = 2.0 * p->kc * p->rbdr / D(p->sq);
99 p->an = p->rbdr*p->rbdr / p->sq;
100 p->imr = 2.0 * p->knb + D(2.0 * p->kna + 4.0 * p->an - 1.0);
101 p->pwrq = 2.0 / (p->imr + 1.0);
102
103 w0 = 2 * M_PI * freq / srate;
104 alpha = sin(w0) / (2. * 0.707);
105 a0 = 1 + alpha;
106 a1 = -2 * cos(w0);
107 a2 = 1 - alpha;
108 b0 = (1 + cos(w0)) / 2;
109 b1 = -(1 + cos(w0));
110 b2 = (1 + cos(w0)) / 2;
111
112 p->hp[0] =-a1 / a0;
113 p->hp[1] =-a2 / a0;
114 p->hp[2] = b0 / a0;
115 p->hp[3] = b1 / a0;
116 p->hp[4] = b2 / a0;
117
118 w0 = 2 * M_PI * ceil / srate;
119 alpha = sin(w0) / (2. * 0.707);
120 a0 = 1 + alpha;
121 a1 = -2 * cos(w0);
122 a2 = 1 - alpha;
123 b0 = (1 - cos(w0)) / 2;
124 b1 = 1 - cos(w0);
125 b2 = (1 - cos(w0)) / 2;
126
127 p->lp[0] =-a1 / a0;
128 p->lp[1] =-a2 / a0;
129 p->lp[2] = b0 / a0;
130 p->lp[3] = b1 / a0;
131 p->lp[4] = b2 / a0;
132}
133
134static double bprocess(double in, const double *const c,
135 double *w1, double *w2)
136{
137 double out = c[2] * in + *w1;
138
139 *w1 = c[3] * in + *w2 + c[0] * out;
140 *w2 = c[4] * in + c[1] * out;
141
142 return out;
143}
144
145static double distortion_process(AExciterContext *s, ChannelParams *p, double in)
146{
147 double proc = in, med;
148
149 proc = bprocess(proc, p->hp, &p->hw[0][0], &p->hw[0][1]);
150 proc = bprocess(proc, p->hp, &p->hw[1][0], &p->hw[1][1]);
151
152 if (proc >= 0.0) {
153 med = (D(p->ap + proc * (p->kpa - proc)) + p->kpb) * p->pwrq;
154 } else {
155 med = (D(p->an - proc * (p->kna + proc)) + p->knb) * p->pwrq * -1.0;
156 }
157
158 proc = p->srct * (med - p->prev_med + p->prev_out);
159 p->prev_med = M(med);
160 p->prev_out = M(proc);
161
162 proc = bprocess(proc, p->hp, &p->hw[2][0], &p->hw[2][1]);
163 proc = bprocess(proc, p->hp, &p->hw[3][0], &p->hw[3][1]);
164
165 if (s->ceil >= 10000.) {
166 proc = bprocess(proc, p->lp, &p->lw[0][0], &p->lw[0][1]);
167 proc = bprocess(proc, p->lp, &p->lw[1][0], &p->lw[1][1]);
168 }
169
170 return proc;
171}
172
173static int filter_frame(AVFilterLink *inlink, AVFrame *in)
174{
175 AVFilterContext *ctx = inlink->dst;
176 AExciterContext *s = ctx->priv;
177 AVFilterLink *outlink = ctx->outputs[0];
178 AVFrame *out;
179 const double *src = (const double *)in->data[0];
180 const double level_in = s->level_in;
181 const double level_out = s->level_out;
182 const double amount = s->amount;
183 const double listen = 1.0 - s->listen;
184 double *dst;
185
186 if (av_frame_is_writable(in)) {
187 out = in;
188 } else {
189 out = ff_get_audio_buffer(inlink, in->nb_samples);
190 if (!out) {
191 av_frame_free(&in);
192 return AVERROR(ENOMEM);
193 }
195 }
196
197 dst = (double *)out->data[0];
198 for (int n = 0; n < in->nb_samples; n++) {
199 for (int c = 0; c < inlink->ch_layout.nb_channels; c++) {
200 double sample = src[c] * level_in;
201
202 sample = distortion_process(s, &s->cp[c], sample);
203 sample = sample * amount + listen * src[c];
204
205 sample *= level_out;
206 if (ctx->is_disabled)
207 dst[c] = src[c];
208 else
209 dst[c] = sample;
210 }
211
212 src += inlink->ch_layout.nb_channels;
213 dst += inlink->ch_layout.nb_channels;
214 }
215
216 if (in != out)
217 av_frame_free(&in);
218
219 return ff_filter_frame(outlink, out);
220}
221
223{
224 AExciterContext *s = ctx->priv;
225
226 av_freep(&s->cp);
227}
228
229static int config_input(AVFilterLink *inlink)
230{
231 AVFilterContext *ctx = inlink->dst;
232 AExciterContext *s = ctx->priv;
233
234 if (!s->cp)
235 s->cp = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->cp));
236 if (!s->cp)
237 return AVERROR(ENOMEM);
238
239 for (int i = 0; i < inlink->ch_layout.nb_channels; i++)
240 set_params(&s->cp[i], s->blend, s->drive, inlink->sample_rate,
241 s->freq, s->ceil);
242
243 return 0;
244}
245
246static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
247 char *res, int res_len, int flags)
248{
249 AVFilterLink *inlink = ctx->inputs[0];
250 int ret;
251
252 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
253 if (ret < 0)
254 return ret;
255
256 return config_input(inlink);
257}
258
260 {
261 .name = "default",
262 .type = AVMEDIA_TYPE_AUDIO,
263 .config_props = config_input,
264 .filter_frame = filter_frame,
265 },
266};
267
269 .p.name = "aexciter",
270 .p.description = NULL_IF_CONFIG_SMALL("Enhance high frequency part of audio."),
271 .p.priv_class = &aexciter_class,
273 .priv_size = sizeof(AExciterContext),
274 .uninit = uninit,
278 .process_command = process_command,
279};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static int config_input(AVFilterLink *inlink)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
const FFFilter ff_af_aexciter
static double distortion_process(AExciterContext *s, ChannelParams *p, double in)
static int config_input(AVFilterLink *inlink)
static const AVFilterPad avfilter_af_aexciter_inputs[]
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static void set_params(ChannelParams *p, double blend, double drive, double srate, double freq, double ceil)
Definition af_aexciter.c:81
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static av_cold void uninit(AVFilterContext *ctx)
static const AVOption aexciter_options[]
Definition af_aexciter.c:55
#define OFFSET(x)
Definition af_aexciter.c:52
static double bprocess(double in, const double *const c, double *w1, double *w2)
#define A(x)
Definition vpx_arith.h:28
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition audio.c:34
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
#define D
Definition avdct.c:35
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
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:906
Main libavfilter public API header.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
static __device__ float ceil(float a)
static __device__ float fabs(float a)
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
#define M(chr)
Definition exr.c:177
#define sample
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#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:204
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_DBL
double
Definition samplefmt.h:61
static const int16_t alpha[]
Definition ilbcdata.h:55
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition filters.h:257
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define M_PI
Definition mathematics.h:67
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
AVOptions.
ChannelParams * cp
Definition af_aexciter.c:49
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:552
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
AVOption.
Definition opt.h:428
sample data coding information
Definition mlp.h:97
double lw[2][2]
Definition af_aexciter.c:34
double lp[5]
Definition af_aexciter.c:33
double drive_old
Definition af_aexciter.c:28
double hw[4][2]
Definition af_aexciter.c:34
double blend_old
Definition af_aexciter.c:28
double hp[5]
Definition af_aexciter.c:33
double prev_med
Definition af_aexciter.c:31
double prev_out
Definition af_aexciter.c:31
#define av_freep(p)
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
static double b1(void *priv, double x, double y)
Definition vf_xfade.c:2034
static double a0(void *priv, double x, double y)
Definition vf_xfade.c:2028
static double b2(void *priv, double x, double y)
Definition vf_xfade.c:2035
static double b0(void *priv, double x, double y)
Definition vf_xfade.c:2033
static double a2(void *priv, double x, double y)
Definition vf_xfade.c:2030
static double a1(void *priv, double x, double y)
Definition vf_xfade.c:2029
static double c[64]