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 "formats.h"
27 #include "internal.h"
28 
29 #include <float.h>
30 
31 typedef struct AudioVirtualBassContext {
32  const AVClass *class;
33 
34  double cutoff;
35  double strength;
36 
37  double a[3], m[3], cf[2];
39 
40 #define OFFSET(x) offsetof(AudioVirtualBassContext, x)
41 #define TFLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
42 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
43 
44 static const AVOption virtualbass_options[] = {
45  { "cutoff", "set virtual bass cutoff", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=250},100,500, FLAGS },
46  { "strength", "set virtual bass strength", OFFSET(strength), AV_OPT_TYPE_DOUBLE, {.dbl=3}, 0.5, 3, TFLAGS },
47  {NULL}
48 };
49 
50 AVFILTER_DEFINE_CLASS(virtualbass);
51 
53 {
54  AVFilterChannelLayouts *in_layout = NULL, *out_layout = NULL;
56  int ret;
57 
58  if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_DBLP )) < 0 ||
59  (ret = ff_set_common_formats (ctx, formats )) < 0 ||
61  (ret = ff_channel_layouts_ref(in_layout, &ctx->inputs[0]->outcfg.channel_layouts)) < 0 ||
63  (ret = ff_channel_layouts_ref(out_layout, &ctx->outputs[0]->incfg.channel_layouts)) < 0)
64  return ret;
65 
67 }
68 
70 {
71  AVFilterContext *ctx = inlink->dst;
72  AudioVirtualBassContext *s = ctx->priv;
73  const double Q = 0.707;
74  double g, k;
75 
76  g = tan(M_PI * s->cutoff / inlink->sample_rate);
77  k = 1. / Q;
78  s->a[0] = 1. / (1. + g * (g + k));
79  s->a[1] = g * s->a[0];
80  s->a[2] = g * s->a[1];
81  s->m[0] = 0.;
82  s->m[1] = 0.;
83  s->m[2] = 1.;
84 
85  return 0;
86 }
87 
88 #define SQR(x) ((x) * (x))
89 
90 static double vb_fun(double x)
91 {
92  double y = 2.5 * atan(0.9 * x) + 2.5 * sqrt(1. - SQR(0.9 * x)) - 2.5;
93 
94  return y < 0. ? sin(y) : y;
95 }
96 
98 {
99  AudioVirtualBassContext *s = ctx->priv;
100  const double *lsrc = (const double *)in->extended_data[0];
101  const double *rsrc = (const double *)in->extended_data[1];
102  double *ldst = (double *)out->extended_data[0];
103  double *rdst = (double *)out->extended_data[1];
104  double *lfe = (double *)out->extended_data[2];
105  const double st = M_PI / s->strength;
106  const double a0 = s->a[0];
107  const double a1 = s->a[1];
108  const double a2 = s->a[2];
109  const double m0 = s->m[0];
110  const double m1 = s->m[1];
111  const double m2 = s->m[2];
112  double b0 = s->cf[0];
113  double b1 = s->cf[1];
114 
115  memcpy(ldst, lsrc, in->nb_samples * sizeof(double));
116  memcpy(rdst, rsrc, in->nb_samples * sizeof(double));
117 
118  for (int n = 0; n < in->nb_samples; n++) {
119  const double center = (lsrc[n] + rsrc[n]) * 0.5;
120  const double v0 = center;
121  const double v3 = v0 - b1;
122  const double v1 = a0 * b0 + a1 * v3;
123  const double v2 = b1 + a1 * b0 + a2 * v3;
124  double b, vb;
125 
126  b0 = 2. * v1 - b0;
127  b1 = 2. * v2 - b1;
128 
129  b = m0 * v0 + m1 * v1 + m2 * v2;
130  vb = sin(vb_fun(b) * st);
131 
132  lfe[n] = vb;
133  }
134 
135  s->cf[0] = b0;
136  s->cf[1] = b1;
137 }
138 
140 {
141  AVFilterContext *ctx = inlink->dst;
142  AVFilterLink *outlink = ctx->outputs[0];
143  AVFrame *out;
144 
145  out = ff_get_audio_buffer(outlink, in->nb_samples);
146  if (!out) {
147  av_frame_free(&in);
148  return AVERROR(ENOMEM);
149  }
151 
152  vb_stereo(ctx, out, in);
153 
154  av_frame_free(&in);
155  return ff_filter_frame(outlink, out);
156 }
157 
158 static const AVFilterPad inputs[] = {
159  {
160  .name = "default",
161  .type = AVMEDIA_TYPE_AUDIO,
162  .filter_frame = filter_frame,
163  .config_props = config_input,
164  },
165 };
166 
168  .name = "virtualbass",
169  .description = NULL_IF_CONFIG_SMALL("Audio Virtual Bass."),
170  .priv_size = sizeof(AudioVirtualBassContext),
171  .priv_class = &virtualbass_class,
176  .process_command = ff_filter_process_command,
177 };
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:107
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:85
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
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:383
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:978
ff_channel_layouts_ref
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:612
AudioVirtualBassContext::cf
double cf[2]
Definition: af_virtualbass.c:37
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:167
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:100
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
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:169
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_virtualbass.c:52
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:760
float.h
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
formats.h
TFLAGS
#define TFLAGS
Definition: af_virtualbass.c:41
b1
static double b1(void *priv, double x, double y)
Definition: vf_xfade.c:2035
v0
#define v0
Definition: regdef.h:26
AV_CHANNEL_LAYOUT_2POINT1
#define AV_CHANNEL_LAYOUT_2POINT1
Definition: channel_layout.h:384
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:47
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:770
FLAGS
#define FLAGS
Definition: af_virtualbass.c:42
s
#define s(width, name)
Definition: cbs_vp9.c:198
inputs
static const AVFilterPad inputs[]
Definition: af_virtualbass.c:158
g
const char * g
Definition: vf_curves.c:127
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:88
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:192
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
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:736
vb_stereo
static void vb_stereo(AVFilterContext *ctx, AVFrame *out, AVFrame *in)
Definition: af_virtualbass.c:97
vb_fun
static double vb_fun(double x)
Definition: af_virtualbass.c:90
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:470
AudioVirtualBassContext::a
double a[3]
Definition: af_virtualbass.c:37
ff_audio_default_filterpad
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:32
ff_add_channel_layout
int ff_add_channel_layout(AVFilterChannelLayouts **l, const AVChannelLayout *channel_layout)
Definition: formats.c:487
OFFSET
#define OFFSET(x)
Definition: af_virtualbass.c:40
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:106
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:307
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:851
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_virtualbass.c:69
AudioVirtualBassContext
Definition: af_virtualbass.c:31
a0
#define a0
Definition: regdef.h:46
M_PI
#define M_PI
Definition: mathematics.h:67
internal.h
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:420
AudioVirtualBassContext::strength
double strength
Definition: af_virtualbass.c:35
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:401
a2
#define a2
Definition: regdef.h:48
AudioVirtualBassContext::m
double m[3]
Definition: af_virtualbass.c:37
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:53
AudioVirtualBassContext::cutoff
double cutoff
Definition: af_virtualbass.c:34
AVFilter
Filter definition.
Definition: avfilter.h:166
ret
ret
Definition: filter_design.txt:187
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_virtualbass.c:139
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:397
audio.h
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:193
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:155
b0
static double b0(void *priv, double x, double y)
Definition: vf_xfade.c:2034
virtualbass_options
static const AVOption virtualbass_options[]
Definition: af_virtualbass.c:44