FFmpeg
af_virtualbass.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 Paul B Mahol
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 License
8  * 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
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
22 #include "libavutil/opt.h"
23 #include "audio.h"
24 #include "avfilter.h"
25 #include "filters.h"
26 #include "internal.h"
27 
28 #include <float.h>
29 
30 typedef struct AudioVirtualBassContext {
31  const AVClass *class;
32 
33  double cutoff;
34  double strength;
35 
36  double a[3], m[3], cf[2];
38 
39 #define OFFSET(x) offsetof(AudioVirtualBassContext, x)
40 #define TFLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
41 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
42 
43 static const AVOption virtualbass_options[] = {
44  { "cutoff", "set virtual bass cutoff", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=250},100,500, FLAGS },
45  { "strength", "set virtual bass strength", OFFSET(strength), AV_OPT_TYPE_DOUBLE, {.dbl=3}, 0.5, 3, TFLAGS },
46  {NULL}
47 };
48 
49 AVFILTER_DEFINE_CLASS(virtualbass);
50 
52 {
53  AVFilterChannelLayouts *in_layout = NULL, *out_layout = NULL;
55  int ret;
56 
57  if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_DBLP )) < 0 ||
58  (ret = ff_set_common_formats (ctx, formats )) < 0 ||
60  (ret = ff_channel_layouts_ref(in_layout, &ctx->inputs[0]->outcfg.channel_layouts)) < 0 ||
62  (ret = ff_channel_layouts_ref(out_layout, &ctx->outputs[0]->incfg.channel_layouts)) < 0)
63  return ret;
64 
66 }
67 
69 {
70  AVFilterContext *ctx = inlink->dst;
71  AudioVirtualBassContext *s = ctx->priv;
72  const double Q = 0.707;
73  double g, k;
74 
75  g = tan(M_PI * s->cutoff / inlink->sample_rate);
76  k = 1. / Q;
77  s->a[0] = 1. / (1. + g * (g + k));
78  s->a[1] = g * s->a[0];
79  s->a[2] = g * s->a[1];
80  s->m[0] = 0.;
81  s->m[1] = 0.;
82  s->m[2] = 1.;
83 
84  return 0;
85 }
86 
87 #define SQR(x) ((x) * (x))
88 
89 static double vb_fun(double x)
90 {
91  double y = 2.5 * atan(0.9 * x) + 2.5 * sqrt(1. - SQR(0.9 * x)) - 2.5;
92 
93  return y < 0. ? sin(y) : y;
94 }
95 
97 {
98  AudioVirtualBassContext *s = ctx->priv;
99  const double *lsrc = (const double *)in->extended_data[0];
100  const double *rsrc = (const double *)in->extended_data[1];
101  double *ldst = (double *)out->extended_data[0];
102  double *rdst = (double *)out->extended_data[1];
103  double *lfe = (double *)out->extended_data[2];
104  const double st = M_PI / s->strength;
105  const double a0 = s->a[0];
106  const double a1 = s->a[1];
107  const double a2 = s->a[2];
108  const double m0 = s->m[0];
109  const double m1 = s->m[1];
110  const double m2 = s->m[2];
111  double b0 = s->cf[0];
112  double b1 = s->cf[1];
113 
114  memcpy(ldst, lsrc, in->nb_samples * sizeof(double));
115  memcpy(rdst, rsrc, in->nb_samples * sizeof(double));
116 
117  for (int n = 0; n < in->nb_samples; n++) {
118  const double center = (lsrc[n] + rsrc[n]) * 0.5;
119  const double v0 = center;
120  const double v3 = v0 - b1;
121  const double v1 = a0 * b0 + a1 * v3;
122  const double v2 = b1 + a1 * b0 + a2 * v3;
123  double b, vb;
124 
125  b0 = 2. * v1 - b0;
126  b1 = 2. * v2 - b1;
127 
128  b = m0 * v0 + m1 * v1 + m2 * v2;
129  vb = sin(vb_fun(b) * st);
130 
131  lfe[n] = vb;
132  }
133 
134  s->cf[0] = b0;
135  s->cf[1] = b1;
136 }
137 
139 {
140  AVFilterContext *ctx = inlink->dst;
141  AVFilterLink *outlink = ctx->outputs[0];
142  AVFrame *out;
143 
144  out = ff_get_audio_buffer(outlink, in->nb_samples);
145  if (!out) {
146  av_frame_free(&in);
147  return AVERROR(ENOMEM);
148  }
149 
150  vb_stereo(ctx, out, in);
151 
152  out->pts = in->pts;
153  av_frame_free(&in);
154  return ff_filter_frame(outlink, out);
155 }
156 
157 static const AVFilterPad inputs[] = {
158  {
159  .name = "default",
160  .type = AVMEDIA_TYPE_AUDIO,
161  .filter_frame = filter_frame,
162  .config_props = config_input,
163  },
164 };
165 
166 static const AVFilterPad outputs[] = {
167  {
168  .name = "default",
169  .type = AVMEDIA_TYPE_AUDIO,
170  },
171 };
172 
174  .name = "virtualbass",
175  .description = NULL_IF_CONFIG_SMALL("Audio Virtual Bass."),
176  .priv_size = sizeof(AudioVirtualBassContext),
177  .priv_class = &virtualbass_class,
182  .process_command = ff_filter_process_command,
183 };
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:100
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:85
AV_CHANNEL_LAYOUT_2POINT1
#define AV_CHANNEL_LAYOUT_2POINT1
Definition: channel_layout.h:358
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
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:1009
ff_channel_layouts_ref
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:591
AudioVirtualBassContext::cf
double cf[2]
Definition: af_virtualbass.c:36
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
ff_af_virtualbass
const AVFilter ff_af_virtualbass
Definition: af_virtualbass.c:173
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:116
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:432
AVOption
AVOption.
Definition: opt.h:251
b
#define b
Definition: input.c:41
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(virtualbass)
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:167
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_virtualbass.c:51
ff_set_common_all_samplerates
int ff_set_common_all_samplerates(AVFilterContext *ctx)
Equivalent to ff_set_common_samplerates(ctx, ff_all_samplerates())
Definition: formats.c:739
float.h
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:357
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:175
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
TFLAGS
#define TFLAGS
Definition: af_virtualbass.c:40
b1
static double b1(void *priv, double x, double y)
Definition: vf_xfade.c:1771
v0
#define v0
Definition: regdef.h:26
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
a1
#define a1
Definition: regdef.h:47
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:749
FLAGS
#define FLAGS
Definition: af_virtualbass.c:41
s
#define s(width, name)
Definition: cbs_vp9.c:256
inputs
static const AVFilterPad inputs[]
Definition: af_virtualbass.c:157
g
const char * g
Definition: vf_curves.c:117
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:227
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:48
SQR
#define SQR(x)
Definition: af_virtualbass.c:87
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:190
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
vb_stereo
static void vb_stereo(AVFilterContext *ctx, AVFrame *out, AVFrame *in)
Definition: af_virtualbass.c:96
vb_fun
static double vb_fun(double x)
Definition: af_virtualbass.c:89
outputs
static const AVFilterPad outputs[]
Definition: af_virtualbass.c:166
ff_add_format
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:449
AudioVirtualBassContext::a
double a[3]
Definition: af_virtualbass.c:36
ff_add_channel_layout
int ff_add_channel_layout(AVFilterChannelLayouts **l, const AVChannelLayout *channel_layout)
Definition: formats.c:466
OFFSET
#define OFFSET(x)
Definition: af_virtualbass.c:39
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:115
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:293
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:873
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_virtualbass.c:68
AudioVirtualBassContext
Definition: af_virtualbass.c:30
a0
#define a0
Definition: regdef.h:46
M_PI
#define M_PI
Definition: mathematics.h:52
internal.h
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:405
AudioVirtualBassContext::strength
double strength
Definition: af_virtualbass.c:34
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:386
a2
#define a2
Definition: regdef.h:48
AudioVirtualBassContext::m
double m[3]
Definition: af_virtualbass.c:36
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
AudioVirtualBassContext::cutoff
double cutoff
Definition: af_virtualbass.c:33
AVFilter
Filter definition.
Definition: avfilter.h:171
ret
ret
Definition: filter_design.txt:187
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_virtualbass.c:138
channel_layout.h
avfilter.h
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:67
AVFilterContext
An instance of a filter.
Definition: avfilter.h:408
audio.h
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:191
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:160
b0
static double b0(void *priv, double x, double y)
Definition: vf_xfade.c:1770
virtualbass_options
static const AVOption virtualbass_options[]
Definition: af_virtualbass.c:43