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 
25 typedef struct ASubBoostContext {
26  const AVClass *class;
27 
28  double dry_gain;
29  double wet_gain;
30  double feedback;
31  double max_boost;
32  double decay;
33  double delay;
34  double cutoff;
35  double slope;
36 
37  double a0, a1, a2;
38  double b0, b1, b2;
39 
42 
43  int *write_pos;
45 
49 
51 {
52  ASubBoostContext *s = ctx->priv;
53  AVFilterLink *inlink = ctx->inputs[0];
54  double w0 = 2 * M_PI * s->cutoff / inlink->sample_rate;
55  double alpha = sin(w0) / 2 * sqrt(2. * (1. / s->slope - 1.) + 2.);
56 
57  s->a0 = 1 + alpha;
58  s->a1 = -2 * cos(w0);
59  s->a2 = 1 - alpha;
60  s->b0 = (1 - cos(w0)) / 2;
61  s->b1 = 1 - cos(w0);
62  s->b2 = (1 - cos(w0)) / 2;
63 
64  s->a1 /= s->a0;
65  s->a2 /= s->a0;
66  s->b0 /= s->a0;
67  s->b1 /= s->a0;
68  s->b2 /= s->a0;
69 
70  s->buffer_samples = inlink->sample_rate * s->delay / 1000;
71 
72  return 0;
73 }
74 
76 {
77  AVFilterContext *ctx = inlink->dst;
78  ASubBoostContext *s = ctx->priv;
79 
80  s->buffer = ff_get_audio_buffer(inlink, inlink->sample_rate / 10);
81  s->w = ff_get_audio_buffer(inlink, 3);
82  s->write_pos = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->write_pos));
83  if (!s->buffer || !s->w || !s->write_pos)
84  return AVERROR(ENOMEM);
85 
86  return get_coeffs(ctx);
87 }
88 
89 typedef struct ThreadData {
90  AVFrame *in, *out;
91 } ThreadData;
92 
93 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
94 {
95  ASubBoostContext *s = ctx->priv;
96  ThreadData *td = arg;
97  AVFrame *out = td->out;
98  AVFrame *in = td->in;
99  const double mix = ctx->is_disabled ? 0. : 1.;
100  const double wet = ctx->is_disabled ? 1. : s->wet_gain;
101  const double dry = ctx->is_disabled ? 1. : s->dry_gain;
102  const double feedback = s->feedback, decay = s->decay;
103  const double max_boost = s->max_boost;
104  const double b0 = s->b0;
105  const double b1 = s->b1;
106  const double b2 = s->b2;
107  const double a1 = -s->a1;
108  const double a2 = -s->a2;
109  const int start = (in->ch_layout.nb_channels * jobnr) / nb_jobs;
110  const int end = (in->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
111  const int buffer_samples = s->buffer_samples;
112 
113  for (int ch = start; ch < end; ch++) {
114  const double *src = (const double *)in->extended_data[ch];
115  double *dst = (double *)out->extended_data[ch];
116  double *buffer = (double *)s->buffer->extended_data[ch];
117  double *w = (double *)s->w->extended_data[ch];
118  int write_pos = s->write_pos[ch];
120  const int bypass = av_channel_layout_index_from_channel(&s->ch_layout, channel) < 0;
121  const double a = 0.00001;
122  const double b = 1. - a;
123 
124  if (bypass) {
125  if (in != out)
126  memcpy(out->extended_data[ch], in->extended_data[ch],
127  in->nb_samples * sizeof(double));
128  continue;
129  }
130 
131  for (int n = 0; n < in->nb_samples; n++) {
132  double out_sample, boost;
133 
134  out_sample = src[n] * b0 + w[0];
135  w[0] = b1 * src[n] + w[1] + a1 * out_sample;
136  w[1] = b2 * src[n] + a2 * out_sample;
137 
138  buffer[write_pos] = buffer[write_pos] * decay + out_sample * feedback;
139  boost = av_clipd((1. - (fabs(src[n] * dry))) / fabs(buffer[write_pos]), 0., max_boost);
140  w[2] = boost > w[2] ? w[2] * b + a * boost : w[2] * a + b * boost;
141  w[2] = av_clipd(w[2], 0., max_boost);
142  dst[n] = (src[n] * dry + w[2] * buffer[write_pos] * mix) * wet;
143 
144  if (++write_pos >= buffer_samples)
145  write_pos = 0;
146  }
147 
148  s->write_pos[ch] = write_pos;
149  }
150 
151  return 0;
152 }
153 
155 {
156  AVFilterContext *ctx = inlink->dst;
157  ASubBoostContext *s = ctx->priv;
158  AVFilterLink *outlink = ctx->outputs[0];
159  ThreadData td;
160  AVFrame *out;
161  int ret;
162 
163  ret = av_channel_layout_copy(&s->ch_layout, &inlink->ch_layout);
164  if (ret < 0)
165  return ret;
166  if (strcmp(s->ch_layout_str, "all"))
167  av_channel_layout_from_string(&s->ch_layout,
168  s->ch_layout_str);
169 
170  if (av_frame_is_writable(in)) {
171  out = in;
172  } else {
173  out = ff_get_audio_buffer(outlink, in->nb_samples);
174  if (!out) {
175  av_frame_free(&in);
176  return AVERROR(ENOMEM);
177  }
179  }
180 
181  td.in = in; td.out = out;
183  FFMIN(inlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
184 
185  if (out != in)
186  av_frame_free(&in);
187  return ff_filter_frame(outlink, out);
188 }
189 
191 {
192  ASubBoostContext *s = ctx->priv;
193 
194  av_channel_layout_uninit(&s->ch_layout);
195  av_frame_free(&s->buffer);
196  av_frame_free(&s->w);
197  av_freep(&s->write_pos);
198 }
199 
200 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
201  char *res, int res_len, int flags)
202 {
203  int ret;
204 
205  ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
206  if (ret < 0)
207  return ret;
208 
209  return get_coeffs(ctx);
210 }
211 
212 #define OFFSET(x) offsetof(ASubBoostContext, x)
213 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
214 
215 static const AVOption asubboost_options[] = {
216  { "dry", "set dry gain", OFFSET(dry_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1.0}, 0, 1, FLAGS },
217  { "wet", "set wet gain", OFFSET(wet_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1.0}, 0, 1, FLAGS },
218  { "boost", "set max boost",OFFSET(max_boost),AV_OPT_TYPE_DOUBLE, {.dbl=2.0}, 1, 12, FLAGS },
219  { "decay", "set decay", OFFSET(decay), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, 0, 1, FLAGS },
220  { "feedback", "set feedback", OFFSET(feedback), AV_OPT_TYPE_DOUBLE, {.dbl=0.9}, 0, 1, FLAGS },
221  { "cutoff", "set cutoff", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 50, 900, FLAGS },
222  { "slope", "set slope", OFFSET(slope), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0.0001, 1, FLAGS },
223  { "delay", "set delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 1, 100, FLAGS },
224  { "channels", "set channels to filter", OFFSET(ch_layout_str), AV_OPT_TYPE_STRING, {.str="all"}, 0, 0, FLAGS },
225  { NULL }
226 };
227 
228 AVFILTER_DEFINE_CLASS(asubboost);
229 
230 static const AVFilterPad inputs[] = {
231  {
232  .name = "default",
233  .type = AVMEDIA_TYPE_AUDIO,
234  .filter_frame = filter_frame,
235  .config_props = config_input,
236  },
237 };
238 
240  .name = "asubboost",
241  .description = NULL_IF_CONFIG_SMALL("Boost subwoofer frequencies."),
242  .priv_size = sizeof(ASubBoostContext),
243  .priv_class = &asubboost_class,
244  .uninit = uninit,
248  .process_command = process_command,
251 };
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
td
#define td
Definition: regdef.h:70
ASubBoostContext::max_boost
double max_boost
Definition: af_asubboost.c:31
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:43
inputs
static const AVFilterPad inputs[]
Definition: af_asubboost.c:230
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:978
FILTER_SINGLE_SAMPLEFMT
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition: internal.h:185
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:100
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
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:200
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:806
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:40
ASubBoostContext::buffer_samples
int buffer_samples
Definition: af_asubboost.c:44
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
ThreadData::out
AVFrame * out
Definition: af_adeclick.c:526
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:317
ThreadData::in
AVFrame * in
Definition: af_adecorrelate.c:153
ASubBoostContext::b1
double b1
Definition: af_asubboost.c:38
ASubBoostContext::w
AVFrame * w
Definition: af_asubboost.c:46
b1
static double b1(void *priv, double x, double y)
Definition: vf_xfade.c:2035
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_asubboost.c:75
AVFrame::ch_layout
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition: frame.h:802
ASubBoostContext::a2
double a2
Definition: af_asubboost.c:37
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:47
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:25
s
#define s(width, name)
Definition: cbs_vp9.c:198
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:29
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:192
ASubBoostContext::feedback
double feedback
Definition: af_asubboost.c:30
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:736
ASubBoostContext::b2
double b2
Definition: af_asubboost.c:38
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
ASubBoostContext::delay
double delay
Definition: af_asubboost.c:33
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_asubboost.c:154
FLAGS
#define FLAGS
Definition: af_asubboost.c:213
get_coeffs
static int get_coeffs(AVFilterContext *ctx)
Definition: af_asubboost.c:50
asubboost_options
static const AVOption asubboost_options[]
Definition: af_asubboost.c:215
ASubBoostContext::a0
double a0
Definition: af_asubboost.c:37
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
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:666
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
b2
static double b2(void *priv, double x, double y)
Definition: vf_xfade.c:2036
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:38
ASubBoostContext::dry_gain
double dry_gain
Definition: af_asubboost.c:28
OFFSET
#define OFFSET(x)
Definition: af_asubboost.c:212
M_PI
#define M_PI
Definition: mathematics.h:67
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:412
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:420
ASubBoostContext::a1
double a1
Definition: af_asubboost.c:37
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:401
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:786
ThreadData
Used for passing data between threads.
Definition: dsddec.c:69
ASubBoostContext::decay
double decay
Definition: af_asubboost.c:32
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
ff_af_asubboost
const AVFilter ff_af_asubboost
Definition: af_asubboost.c:239
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:53
ASubBoostContext::ch_layout
AVChannelLayout ch_layout
Definition: af_asubboost.c:41
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:93
AVFilter
Filter definition.
Definition: avfilter.h:166
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_asubboost.c:190
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:846
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:640
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:397
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:647
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:193
ASubBoostContext::buffer
AVFrame * buffer
Definition: af_asubboost.c:47
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:35
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
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
b0
static double b0(void *priv, double x, double y)
Definition: vf_xfade.c:2034
ASubBoostContext::cutoff
double cutoff
Definition: af_asubboost.c:34
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:144
channel
channel
Definition: ebur128.h:39
av_clipd
av_clipd
Definition: af_crystalizer.c:131