FFmpeg
af_asubboost.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
20 #include "libavutil/ffmath.h"
21 #include "libavutil/opt.h"
22 #include "avfilter.h"
23 #include "audio.h"
24 #include "formats.h"
25 
26 typedef struct ASubBoostContext {
27  const AVClass *class;
28 
29  double dry_gain;
30  double wet_gain;
31  double feedback;
32  double max_boost;
33  double decay;
34  double delay;
35  double cutoff;
36  double slope;
37 
38  double a0, a1, a2;
39  double b0, b1, b2;
40 
43 
44  int *write_pos;
46 
50 
52 {
53  ASubBoostContext *s = ctx->priv;
54  AVFilterLink *inlink = ctx->inputs[0];
55  double w0 = 2 * M_PI * s->cutoff / inlink->sample_rate;
56  double alpha = sin(w0) / 2 * sqrt(2. * (1. / s->slope - 1.) + 2.);
57 
58  s->a0 = 1 + alpha;
59  s->a1 = -2 * cos(w0);
60  s->a2 = 1 - alpha;
61  s->b0 = (1 - cos(w0)) / 2;
62  s->b1 = 1 - cos(w0);
63  s->b2 = (1 - cos(w0)) / 2;
64 
65  s->a1 /= s->a0;
66  s->a2 /= s->a0;
67  s->b0 /= s->a0;
68  s->b1 /= s->a0;
69  s->b2 /= s->a0;
70 
71  s->buffer_samples = inlink->sample_rate * s->delay / 1000;
72 
73  return 0;
74 }
75 
77 {
78  AVFilterContext *ctx = inlink->dst;
79  ASubBoostContext *s = ctx->priv;
80 
81  s->buffer = ff_get_audio_buffer(inlink, inlink->sample_rate / 10);
82  s->w = ff_get_audio_buffer(inlink, 3);
83  s->write_pos = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->write_pos));
84  if (!s->buffer || !s->w || !s->write_pos)
85  return AVERROR(ENOMEM);
86 
87  return get_coeffs(ctx);
88 }
89 
90 typedef struct ThreadData {
91  AVFrame *in, *out;
92 } ThreadData;
93 
94 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
95 {
96  ASubBoostContext *s = ctx->priv;
97  ThreadData *td = arg;
98  AVFrame *out = td->out;
99  AVFrame *in = td->in;
100  const double mix = ctx->is_disabled ? 0. : 1.;
101  const double wet = ctx->is_disabled ? 1. : s->wet_gain;
102  const double dry = ctx->is_disabled ? 1. : s->dry_gain;
103  const double feedback = s->feedback, decay = s->decay;
104  const double max_boost = s->max_boost;
105  const double b0 = s->b0;
106  const double b1 = s->b1;
107  const double b2 = s->b2;
108  const double a1 = -s->a1;
109  const double a2 = -s->a2;
110  const int start = (in->ch_layout.nb_channels * jobnr) / nb_jobs;
111  const int end = (in->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
112  const int buffer_samples = s->buffer_samples;
113 
114  for (int ch = start; ch < end; ch++) {
115  const double *src = (const double *)in->extended_data[ch];
116  double *dst = (double *)out->extended_data[ch];
117  double *buffer = (double *)s->buffer->extended_data[ch];
118  double *w = (double *)s->w->extended_data[ch];
119  int write_pos = s->write_pos[ch];
121  const int bypass = av_channel_layout_index_from_channel(&s->ch_layout, channel) < 0;
122  const double a = 0.00001;
123  const double b = 1. - a;
124 
125  if (bypass) {
126  if (in != out)
127  memcpy(out->extended_data[ch], in->extended_data[ch],
128  in->nb_samples * sizeof(double));
129  continue;
130  }
131 
132  for (int n = 0; n < in->nb_samples; n++) {
133  double out_sample, boost;
134 
135  out_sample = src[n] * b0 + w[0];
136  w[0] = b1 * src[n] + w[1] + a1 * out_sample;
137  w[1] = b2 * src[n] + a2 * out_sample;
138 
139  buffer[write_pos] = buffer[write_pos] * decay + out_sample * feedback;
140  boost = av_clipd((1. - (fabs(src[n] * dry))) / fabs(buffer[write_pos]), 0., max_boost);
141  w[2] = boost > w[2] ? w[2] * b + a * boost : w[2] * a + b * boost;
142  w[2] = av_clipd(w[2], 0., max_boost);
143  dst[n] = (src[n] * dry + w[2] * buffer[write_pos] * mix) * wet;
144 
145  if (++write_pos >= buffer_samples)
146  write_pos = 0;
147  }
148 
149  s->write_pos[ch] = write_pos;
150  }
151 
152  return 0;
153 }
154 
156 {
157  AVFilterContext *ctx = inlink->dst;
158  ASubBoostContext *s = ctx->priv;
159  AVFilterLink *outlink = ctx->outputs[0];
160  ThreadData td;
161  AVFrame *out;
162  int ret;
163 
164  ret = av_channel_layout_copy(&s->ch_layout, &inlink->ch_layout);
165  if (ret < 0)
166  return ret;
167  if (strcmp(s->ch_layout_str, "all"))
168  av_channel_layout_from_string(&s->ch_layout,
169  s->ch_layout_str);
170 
171  if (av_frame_is_writable(in)) {
172  out = in;
173  } else {
174  out = ff_get_audio_buffer(outlink, in->nb_samples);
175  if (!out) {
176  av_frame_free(&in);
177  return AVERROR(ENOMEM);
178  }
180  }
181 
182  td.in = in; td.out = out;
184  FFMIN(inlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
185 
186  if (out != in)
187  av_frame_free(&in);
188  return ff_filter_frame(outlink, out);
189 }
190 
192 {
193  ASubBoostContext *s = ctx->priv;
194 
195  av_channel_layout_uninit(&s->ch_layout);
196  av_frame_free(&s->buffer);
197  av_frame_free(&s->w);
198  av_freep(&s->write_pos);
199 }
200 
201 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
202  char *res, int res_len, int flags)
203 {
204  int ret;
205 
206  ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
207  if (ret < 0)
208  return ret;
209 
210  return get_coeffs(ctx);
211 }
212 
213 #define OFFSET(x) offsetof(ASubBoostContext, x)
214 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
215 
216 static const AVOption asubboost_options[] = {
217  { "dry", "set dry gain", OFFSET(dry_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1.0}, 0, 1, FLAGS },
218  { "wet", "set wet gain", OFFSET(wet_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1.0}, 0, 1, FLAGS },
219  { "boost", "set max boost",OFFSET(max_boost),AV_OPT_TYPE_DOUBLE, {.dbl=2.0}, 1, 12, FLAGS },
220  { "decay", "set decay", OFFSET(decay), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, 0, 1, FLAGS },
221  { "feedback", "set feedback", OFFSET(feedback), AV_OPT_TYPE_DOUBLE, {.dbl=0.9}, 0, 1, FLAGS },
222  { "cutoff", "set cutoff", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 50, 900, FLAGS },
223  { "slope", "set slope", OFFSET(slope), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0.0001, 1, FLAGS },
224  { "delay", "set delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 1, 100, FLAGS },
225  { "channels", "set channels to filter", OFFSET(ch_layout_str), AV_OPT_TYPE_STRING, {.str="all"}, 0, 0, FLAGS },
226  { NULL }
227 };
228 
229 AVFILTER_DEFINE_CLASS(asubboost);
230 
231 static const AVFilterPad inputs[] = {
232  {
233  .name = "default",
234  .type = AVMEDIA_TYPE_AUDIO,
235  .filter_frame = filter_frame,
236  .config_props = config_input,
237  },
238 };
239 
240 static const AVFilterPad outputs[] = {
241  {
242  .name = "default",
243  .type = AVMEDIA_TYPE_AUDIO,
244  },
245 };
246 
248  .name = "asubboost",
249  .description = NULL_IF_CONFIG_SMALL("Boost subwoofer frequencies."),
250  .priv_size = sizeof(ASubBoostContext),
251  .priv_class = &asubboost_class,
252  .uninit = uninit,
256  .process_command = process_command,
259 };
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
td
#define td
Definition: regdef.h:70
ASubBoostContext::max_boost
double max_boost
Definition: af_asubboost.c:32
mix
static int mix(int c0, int c1)
Definition: 4xm.c:717
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
ASubBoostContext::write_pos
int * write_pos
Definition: af_asubboost.c:44
inputs
static const AVFilterPad inputs[]
Definition: af_asubboost.c:231
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:969
FILTER_SINGLE_SAMPLEFMT
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition: internal.h:187
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:99
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
process_command
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: af_asubboost.c:201
w
uint8_t w
Definition: llviddspenc.c:38
av_channel_layout_channel_from_index
enum AVChannel av_channel_layout_channel_from_index(const AVChannelLayout *channel_layout, unsigned int idx)
Get the channel with the given index in a channel layout.
Definition: channel_layout.c:796
AVOption
AVOption.
Definition: opt.h:251
b
#define b
Definition: input.c:41
ASubBoostContext::ch_layout_str
char * ch_layout_str
Definition: af_asubboost.c:41
ASubBoostContext::buffer_samples
int buffer_samples
Definition: af_asubboost.c:45
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:165
ThreadData::out
AVFrame * out
Definition: af_adeclick.c:473
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:311
ThreadData::in
AVFrame * in
Definition: af_adecorrelate.c:154
formats.h
ASubBoostContext::b1
double b1
Definition: af_asubboost.c:39
ASubBoostContext::w
AVFrame * w
Definition: af_asubboost.c:47
b1
static double b1(void *priv, double x, double y)
Definition: vf_xfade.c:1771
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_asubboost.c:76
AVFrame::ch_layout
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition: frame.h:723
outputs
static const AVFilterPad outputs[]
Definition: af_asubboost.c:240
ASubBoostContext::a2
double a2
Definition: af_asubboost.c:38
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
a1
#define a1
Definition: regdef.h:47
av_cold
#define av_cold
Definition: attributes.h:90
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(asubboost)
ASubBoostContext
Definition: af_asubboost.c:26
s
#define s(width, name)
Definition: cbs_vp9.c:256
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:227
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ASubBoostContext::wet_gain
double wet_gain
Definition: af_asubboost.c:30
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:194
ASubBoostContext::feedback
double feedback
Definition: af_asubboost.c:31
arg
const char * arg
Definition: jacosubdec.c:67
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
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:594
ASubBoostContext::b2
double b2
Definition: af_asubboost.c:39
ASubBoostContext::delay
double delay
Definition: af_asubboost.c:34
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_asubboost.c:155
FLAGS
#define FLAGS
Definition: af_asubboost.c:214
get_coeffs
static int get_coeffs(AVFilterContext *ctx)
Definition: af_asubboost.c:51
asubboost_options
static const AVOption asubboost_options[]
Definition: af_asubboost.c:216
ASubBoostContext::a0
double a0
Definition: af_asubboost.c:38
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:301
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:524
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
b2
static double b2(void *priv, double x, double y)
Definition: vf_xfade.c:1772
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
ASubBoostContext::b0
double b0
Definition: af_asubboost.c:39
ASubBoostContext::dry_gain
double dry_gain
Definition: af_asubboost.c:29
OFFSET
#define OFFSET(x)
Definition: af_asubboost.c:213
M_PI
#define M_PI
Definition: mathematics.h:52
AVChannel
AVChannel
Definition: channel_layout.h:47
av_channel_layout_from_string
int av_channel_layout_from_string(AVChannelLayout *channel_layout, const char *str)
Initialize a channel layout from a given string description.
Definition: channel_layout.c:404
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:410
ASubBoostContext::a1
double a1
Definition: af_asubboost.c:38
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:391
a2
#define a2
Definition: regdef.h:48
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:777
ThreadData
Used for passing data between threads.
Definition: dsddec.c:69
ASubBoostContext::decay
double decay
Definition: af_asubboost.c:33
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
ff_af_asubboost
const AVFilter ff_af_asubboost
Definition: af_asubboost.c:247
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
ASubBoostContext::ch_layout
AVChannelLayout ch_layout
Definition: af_asubboost.c:42
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
filter_channels
static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_asubboost.c:94
AVFilter
Filter definition.
Definition: avfilter.h:161
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_asubboost.c:191
ret
ret
Definition: filter_design.txt:187
channel_layout.h
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
av_channel_layout_index_from_channel
int av_channel_layout_index_from_channel(const AVChannelLayout *channel_layout, enum AVChannel channel)
Get the index of a given channel in a channel layout.
Definition: channel_layout.c:836
avfilter.h
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:632
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:67
ffmath.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:392
av_channel_layout_copy
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
Definition: channel_layout.c:639
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:117
audio.h
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:195
ASubBoostContext::buffer
AVFrame * buffer
Definition: af_asubboost.c:48
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
ASubBoostContext::slope
double slope
Definition: af_asubboost.c:36
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:150
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
b0
static double b0(void *priv, double x, double y)
Definition: vf_xfade.c:1770
ASubBoostContext::cutoff
double cutoff
Definition: af_asubboost.c:35
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
ff_filter_execute
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: internal.h:146
channel
channel
Definition: ebur128.h:39
av_clipd
av_clipd
Definition: af_crystalizer.c:132