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 
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 
51 static int query_formats(const AVFilterContext *ctx,
52  AVFilterFormatsConfig **cfg_in,
53  AVFilterFormatsConfig **cfg_out)
54 {
55  static const enum AVSampleFormat formats[] = {
58  };
59 
60  AVFilterChannelLayouts *in_layout = NULL, *out_layout = NULL;
61  int ret;
62 
63  ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, formats);
64  if (ret < 0)
65  return ret;
66 
68  (ret = ff_channel_layouts_ref(in_layout, &cfg_in[0]->channel_layouts)) < 0 ||
70  (ret = ff_channel_layouts_ref(out_layout, &cfg_out[0]->channel_layouts)) < 0)
71  return ret;
72 
73  return 0;
74 }
75 
77 {
78  AVFilterContext *ctx = inlink->dst;
79  AudioVirtualBassContext *s = ctx->priv;
80  const double Q = 0.707;
81  double g, k;
82 
83  g = tan(M_PI * s->cutoff / inlink->sample_rate);
84  k = 1. / Q;
85  s->a[0] = 1. / (1. + g * (g + k));
86  s->a[1] = g * s->a[0];
87  s->a[2] = g * s->a[1];
88  s->m[0] = 0.;
89  s->m[1] = 0.;
90  s->m[2] = 1.;
91 
92  return 0;
93 }
94 
95 #define SQR(x) ((x) * (x))
96 
97 static double vb_fun(double x)
98 {
99  double y = 2.5 * atan(0.9 * x) + 2.5 * sqrt(1. - SQR(0.9 * x)) - 2.5;
100 
101  return y < 0. ? sin(y) : y;
102 }
103 
105 {
106  AudioVirtualBassContext *s = ctx->priv;
107  const double *lsrc = (const double *)in->extended_data[0];
108  const double *rsrc = (const double *)in->extended_data[1];
109  double *ldst = (double *)out->extended_data[0];
110  double *rdst = (double *)out->extended_data[1];
111  double *lfe = (double *)out->extended_data[2];
112  const double st = M_PI / s->strength;
113  const double a0 = s->a[0];
114  const double a1 = s->a[1];
115  const double a2 = s->a[2];
116  const double m0 = s->m[0];
117  const double m1 = s->m[1];
118  const double m2 = s->m[2];
119  double b0 = s->cf[0];
120  double b1 = s->cf[1];
121 
122  memcpy(ldst, lsrc, in->nb_samples * sizeof(double));
123  memcpy(rdst, rsrc, in->nb_samples * sizeof(double));
124 
125  for (int n = 0; n < in->nb_samples; n++) {
126  const double center = (lsrc[n] + rsrc[n]) * 0.5;
127  const double v0 = center;
128  const double v3 = v0 - b1;
129  const double v1 = a0 * b0 + a1 * v3;
130  const double v2 = b1 + a1 * b0 + a2 * v3;
131  double b, vb;
132 
133  b0 = 2. * v1 - b0;
134  b1 = 2. * v2 - b1;
135 
136  b = m0 * v0 + m1 * v1 + m2 * v2;
137  vb = sin(vb_fun(b) * st);
138 
139  lfe[n] = vb;
140  }
141 
142  s->cf[0] = b0;
143  s->cf[1] = b1;
144 }
145 
147 {
148  AVFilterContext *ctx = inlink->dst;
149  AVFilterLink *outlink = ctx->outputs[0];
150  AVFrame *out;
151 
152  out = ff_get_audio_buffer(outlink, in->nb_samples);
153  if (!out) {
154  av_frame_free(&in);
155  return AVERROR(ENOMEM);
156  }
158 
159  vb_stereo(ctx, out, in);
160 
161  av_frame_free(&in);
162  return ff_filter_frame(outlink, out);
163 }
164 
165 static const AVFilterPad inputs[] = {
166  {
167  .name = "default",
168  .type = AVMEDIA_TYPE_AUDIO,
169  .filter_frame = filter_frame,
170  .config_props = config_input,
171  },
172 };
173 
175  .name = "virtualbass",
176  .description = NULL_IF_CONFIG_SMALL("Audio Virtual Bass."),
177  .priv_size = sizeof(AudioVirtualBassContext),
178  .priv_class = &virtualbass_class,
183  .process_command = ff_filter_process_command,
184 };
formats
formats
Definition: signature.h:47
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:98
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:55
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:393
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1062
ff_channel_layouts_ref
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:673
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:174
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVOption
AVOption.
Definition: opt.h:429
b
#define b
Definition: input.c:41
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(virtualbass)
float.h
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
formats.h
TFLAGS
#define TFLAGS
Definition: af_virtualbass.c:40
b1
static double b1(void *priv, double x, double y)
Definition: vf_xfade.c:2034
AV_CHANNEL_LAYOUT_2POINT1
#define AV_CHANNEL_LAYOUT_2POINT1
Definition: channel_layout.h:394
a2
static double a2(void *priv, double x, double y)
Definition: vf_xfade.c:2030
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
FLAGS
#define FLAGS
Definition: af_virtualbass.c:41
s
#define s(width, name)
Definition: cbs_vp9.c:198
inputs
static const AVFilterPad inputs[]
Definition: af_virtualbass.c:165
g
const char * g
Definition: vf_curves.c:128
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition: opt.h:267
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
SQR
#define SQR(x)
Definition: af_virtualbass.c:95
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
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:713
vb_stereo
static void vb_stereo(AVFilterContext *ctx, AVFrame *out, AVFrame *in)
Definition: af_virtualbass.c:104
vb_fun
static double vb_fun(double x)
Definition: af_virtualbass.c:97
query_formats
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition: af_virtualbass.c:51
AudioVirtualBassContext::a
double a[3]
Definition: af_virtualbass.c:36
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:34
ff_add_channel_layout
int ff_add_channel_layout(AVFilterChannelLayouts **l, const AVChannelLayout *channel_layout)
Definition: formats.c:521
AVFilterFormatsConfig
Lists of formats / etc.
Definition: avfilter.h:111
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:94
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:317
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
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:901
a0
static double a0(void *priv, double x, double y)
Definition: vf_xfade.c:2028
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_virtualbass.c:76
AudioVirtualBassContext
Definition: af_virtualbass.c:30
M_PI
#define M_PI
Definition: mathematics.h:67
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:469
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:450
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
FILTER_QUERY_FUNC2
#define FILTER_QUERY_FUNC2(func)
Definition: filters.h:239
AudioVirtualBassContext::m
double m[3]
Definition: af_virtualbass.c:36
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
AudioVirtualBassContext::cutoff
double cutoff
Definition: af_virtualbass.c:33
AVFilter
Filter definition.
Definition: avfilter.h:201
ret
ret
Definition: filter_design.txt:187
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_virtualbass.c:146
ff_set_common_formats_from_list2
int ff_set_common_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const int *fmts)
Definition: formats.c:1016
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:457
audio.h
channel_layouts
static const uint16_t channel_layouts[7]
Definition: dca_lbr.c:112
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:190
Q
#define Q(x)
Definition: filter_template.c:423
b0
static double b0(void *priv, double x, double y)
Definition: vf_xfade.c:2033
a1
static double a1(void *priv, double x, double y)
Definition: vf_xfade.c:2029
virtualbass_options
static const AVOption virtualbass_options[]
Definition: af_virtualbass.c:43