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/mem.h"
22 #include "libavutil/opt.h"
23 #include "avfilter.h"
24 #include "audio.h"
25 #include "filters.h"
26 
27 typedef struct ASubBoostContext {
28  const AVClass *class;
29 
30  double dry_gain;
31  double wet_gain;
32  double feedback;
33  double max_boost;
34  double decay;
35  double delay;
36  double cutoff;
37  double slope;
38 
39  double a0, a1, a2;
40  double b0, b1, b2;
41 
44 
45  int *write_pos;
47 
51 
53 {
54  ASubBoostContext *s = ctx->priv;
55  AVFilterLink *inlink = ctx->inputs[0];
56  double w0 = 2 * M_PI * s->cutoff / inlink->sample_rate;
57  double alpha = sin(w0) / 2 * sqrt(2. * (1. / s->slope - 1.) + 2.);
58 
59  s->a0 = 1 + alpha;
60  s->a1 = -2 * cos(w0);
61  s->a2 = 1 - alpha;
62  s->b0 = (1 - cos(w0)) / 2;
63  s->b1 = 1 - cos(w0);
64  s->b2 = (1 - cos(w0)) / 2;
65 
66  s->a1 /= s->a0;
67  s->a2 /= s->a0;
68  s->b0 /= s->a0;
69  s->b1 /= s->a0;
70  s->b2 /= s->a0;
71 
72  s->buffer_samples = inlink->sample_rate * s->delay / 1000;
73 
74  return 0;
75 }
76 
78 {
79  AVFilterContext *ctx = inlink->dst;
80  ASubBoostContext *s = ctx->priv;
81 
82  s->buffer = ff_get_audio_buffer(inlink, inlink->sample_rate / 10);
83  s->w = ff_get_audio_buffer(inlink, 3);
84  s->write_pos = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->write_pos));
85  if (!s->buffer || !s->w || !s->write_pos)
86  return AVERROR(ENOMEM);
87 
88  return get_coeffs(ctx);
89 }
90 
91 typedef struct ThreadData {
92  AVFrame *in, *out;
93 } ThreadData;
94 
95 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
96 {
97  ASubBoostContext *s = ctx->priv;
98  ThreadData *td = arg;
99  AVFrame *out = td->out;
100  AVFrame *in = td->in;
101  const double mix = ctx->is_disabled ? 0. : 1.;
102  const double wet = ctx->is_disabled ? 1. : s->wet_gain;
103  const double dry = ctx->is_disabled ? 1. : s->dry_gain;
104  const double feedback = s->feedback, decay = s->decay;
105  const double max_boost = s->max_boost;
106  const double b0 = s->b0;
107  const double b1 = s->b1;
108  const double b2 = s->b2;
109  const double a1 = -s->a1;
110  const double a2 = -s->a2;
111  const int start = (in->ch_layout.nb_channels * jobnr) / nb_jobs;
112  const int end = (in->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
113  const int buffer_samples = s->buffer_samples;
114 
115  for (int ch = start; ch < end; ch++) {
116  const double *src = (const double *)in->extended_data[ch];
117  double *dst = (double *)out->extended_data[ch];
118  double *buffer = (double *)s->buffer->extended_data[ch];
119  double *w = (double *)s->w->extended_data[ch];
120  int write_pos = s->write_pos[ch];
122  const int bypass = av_channel_layout_index_from_channel(&s->ch_layout, channel) < 0;
123  const double a = 0.00001;
124  const double b = 1. - a;
125 
126  if (bypass) {
127  if (in != out)
128  memcpy(out->extended_data[ch], in->extended_data[ch],
129  in->nb_samples * sizeof(double));
130  continue;
131  }
132 
133  for (int n = 0; n < in->nb_samples; n++) {
134  double out_sample, boost;
135 
136  out_sample = src[n] * b0 + w[0];
137  w[0] = b1 * src[n] + w[1] + a1 * out_sample;
138  w[1] = b2 * src[n] + a2 * out_sample;
139 
140  buffer[write_pos] = buffer[write_pos] * decay + out_sample * feedback;
141  boost = av_clipd((1. - (fabs(src[n] * dry))) / fabs(buffer[write_pos]), 0., max_boost);
142  w[2] = boost > w[2] ? w[2] * b + a * boost : w[2] * a + b * boost;
143  w[2] = av_clipd(w[2], 0., max_boost);
144  dst[n] = (src[n] * dry + w[2] * buffer[write_pos] * mix) * wet;
145 
146  if (++write_pos >= buffer_samples)
147  write_pos = 0;
148  }
149 
150  s->write_pos[ch] = write_pos;
151  }
152 
153  return 0;
154 }
155 
157 {
158  AVFilterContext *ctx = inlink->dst;
159  ASubBoostContext *s = ctx->priv;
160  AVFilterLink *outlink = ctx->outputs[0];
161  ThreadData td;
162  AVFrame *out;
163  int ret;
164 
165  ret = av_channel_layout_copy(&s->ch_layout, &inlink->ch_layout);
166  if (ret < 0)
167  return ret;
168  if (strcmp(s->ch_layout_str, "all"))
169  av_channel_layout_from_string(&s->ch_layout,
170  s->ch_layout_str);
171 
172  if (av_frame_is_writable(in)) {
173  out = in;
174  } else {
175  out = ff_get_audio_buffer(outlink, in->nb_samples);
176  if (!out) {
177  av_frame_free(&in);
178  return AVERROR(ENOMEM);
179  }
181  }
182 
183  td.in = in; td.out = out;
185  FFMIN(inlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
186 
187  if (out != in)
188  av_frame_free(&in);
189  return ff_filter_frame(outlink, out);
190 }
191 
193 {
194  ASubBoostContext *s = ctx->priv;
195 
196  av_channel_layout_uninit(&s->ch_layout);
197  av_frame_free(&s->buffer);
198  av_frame_free(&s->w);
199  av_freep(&s->write_pos);
200 }
201 
202 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
203  char *res, int res_len, int flags)
204 {
205  int ret;
206 
207  ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
208  if (ret < 0)
209  return ret;
210 
211  return get_coeffs(ctx);
212 }
213 
214 #define OFFSET(x) offsetof(ASubBoostContext, x)
215 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
216 
217 static const AVOption asubboost_options[] = {
218  { "dry", "set dry gain", OFFSET(dry_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1.0}, 0, 1, FLAGS },
219  { "wet", "set wet gain", OFFSET(wet_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1.0}, 0, 1, FLAGS },
220  { "boost", "set max boost",OFFSET(max_boost),AV_OPT_TYPE_DOUBLE, {.dbl=2.0}, 1, 12, FLAGS },
221  { "decay", "set decay", OFFSET(decay), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, 0, 1, FLAGS },
222  { "feedback", "set feedback", OFFSET(feedback), AV_OPT_TYPE_DOUBLE, {.dbl=0.9}, 0, 1, FLAGS },
223  { "cutoff", "set cutoff", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 50, 900, FLAGS },
224  { "slope", "set slope", OFFSET(slope), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0.0001, 1, FLAGS },
225  { "delay", "set delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 1, 100, FLAGS },
226  { "channels", "set channels to filter", OFFSET(ch_layout_str), AV_OPT_TYPE_STRING, {.str="all"}, 0, 0, FLAGS },
227  { NULL }
228 };
229 
230 AVFILTER_DEFINE_CLASS(asubboost);
231 
232 static const AVFilterPad inputs[] = {
233  {
234  .name = "default",
235  .type = AVMEDIA_TYPE_AUDIO,
236  .filter_frame = filter_frame,
237  .config_props = config_input,
238  },
239 };
240 
242  .name = "asubboost",
243  .description = NULL_IF_CONFIG_SMALL("Boost subwoofer frequencies."),
244  .priv_size = sizeof(ASubBoostContext),
245  .priv_class = &asubboost_class,
246  .uninit = uninit,
250  .process_command = process_command,
253 };
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
ASubBoostContext::max_boost
double max_boost
Definition: af_asubboost.c:33
mix
static int mix(int c0, int c1)
Definition: 4xm.c:716
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:45
inputs
static const AVFilterPad inputs[]
Definition: af_asubboost.c:232
out
FILE * out
Definition: movenc.c:55
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1061
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: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
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:202
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:671
AVOption
AVOption.
Definition: opt.h:429
b
#define b
Definition: input.c:41
ASubBoostContext::ch_layout_str
char * ch_layout_str
Definition: af_asubboost.c:42
ASubBoostContext::buffer_samples
int buffer_samples
Definition: af_asubboost.c:46
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
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:327
ThreadData::in
AVFrame * in
Definition: af_adecorrelate.c:155
ASubBoostContext::b1
double b1
Definition: af_asubboost.c:40
ASubBoostContext::w
AVFrame * w
Definition: af_asubboost.c:48
b1
static double b1(void *priv, double x, double y)
Definition: vf_xfade.c:2034
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_asubboost.c:77
AVFrame::ch_layout
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition: frame.h:790
ASubBoostContext::a2
double a2
Definition: af_asubboost.c:39
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
av_cold
#define av_cold
Definition: attributes.h:90
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(asubboost)
ASubBoostContext
Definition: af_asubboost.c:27
s
#define s(width, name)
Definition: cbs_vp9.c:198
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
ASubBoostContext::wet_gain
double wet_gain
Definition: af_asubboost.c:31
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
ASubBoostContext::feedback
double feedback
Definition: af_asubboost.c:32
arg
const char * arg
Definition: jacosubdec.c:67
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
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:725
ASubBoostContext::b2
double b2
Definition: af_asubboost.c:40
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
ASubBoostContext::delay
double delay
Definition: af_asubboost.c:35
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_asubboost.c:156
FLAGS
#define FLAGS
Definition: af_asubboost.c:215
get_coeffs
static int get_coeffs(AVFilterContext *ctx)
Definition: af_asubboost.c:52
FILTER_SINGLE_SAMPLEFMT
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition: filters.h:255
asubboost_options
static const AVOption asubboost_options[]
Definition: af_asubboost.c:217
ASubBoostContext::a0
double a0
Definition: af_asubboost.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
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:661
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:900
b2
static double b2(void *priv, double x, double y)
Definition: vf_xfade.c:2035
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:40
ASubBoostContext::dry_gain
double dry_gain
Definition: af_asubboost.c:30
OFFSET
#define OFFSET(x)
Definition: af_asubboost.c:214
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:310
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:469
ASubBoostContext::a1
double a1
Definition: af_asubboost.c:39
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:450
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:840
ThreadData
Used for passing data between threads.
Definition: dsddec.c:71
ASubBoostContext::decay
double decay
Definition: af_asubboost.c:34
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
ff_af_asubboost
const AVFilter ff_af_asubboost
Definition: af_asubboost.c:241
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
ASubBoostContext::ch_layout
AVChannelLayout ch_layout
Definition: af_asubboost.c:43
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
filter_channels
static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_asubboost.c:95
AVFilter
Filter definition.
Definition: avfilter.h:201
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_asubboost.c:192
ret
ret
Definition: filter_design.txt:187
channel_layout.h
ff_filter_execute
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: avfilter.c:1666
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:711
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:440
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:457
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:447
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:152
mem.h
audio.h
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
ASubBoostContext::buffer
AVFrame * buffer
Definition: af_asubboost.c:49
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
ASubBoostContext::slope
double slope
Definition: af_asubboost.c:37
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
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
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
ASubBoostContext::cutoff
double cutoff
Definition: af_asubboost.c:36
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition: opt.h:276
src
#define src
Definition: vp8dsp.c:248
channel
channel
Definition: ebur128.h:39
av_clipd
av_clipd
Definition: af_crystalizer.c:132