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 decay;
33  double delay;
34  double cutoff;
35  double slope;
36 
37  double a0, a1, a2;
38  double b0, b1, b2;
39 
40  int *write_pos;
42 
46 
48 {
49  ASubBoostContext *s = ctx->priv;
50  AVFilterLink *inlink = ctx->inputs[0];
51  double w0 = 2 * M_PI * s->cutoff / inlink->sample_rate;
52  double alpha = sin(w0) / 2 * sqrt(2. * (1. / s->slope - 1.) + 2.);
53 
54  s->a0 = 1 + alpha;
55  s->a1 = -2 * cos(w0);
56  s->a2 = 1 - alpha;
57  s->b0 = (1 - cos(w0)) / 2;
58  s->b1 = 1 - cos(w0);
59  s->b2 = (1 - cos(w0)) / 2;
60 
61  s->a1 /= s->a0;
62  s->a2 /= s->a0;
63  s->b0 /= s->a0;
64  s->b1 /= s->a0;
65  s->b2 /= s->a0;
66 
67  s->buffer_samples = inlink->sample_rate * s->delay / 1000;
68 
69  return 0;
70 }
71 
73 {
74  AVFilterContext *ctx = inlink->dst;
75  ASubBoostContext *s = ctx->priv;
76 
77  s->buffer = ff_get_audio_buffer(inlink, inlink->sample_rate / 10);
78  s->w = ff_get_audio_buffer(inlink, 2);
79  s->write_pos = av_calloc(inlink->channels, sizeof(*s->write_pos));
80  if (!s->buffer || !s->w || !s->write_pos)
81  return AVERROR(ENOMEM);
82 
83  return get_coeffs(ctx);
84 }
85 
86 typedef struct ThreadData {
87  AVFrame *in, *out;
88 } ThreadData;
89 
90 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
91 {
92  ASubBoostContext *s = ctx->priv;
93  ThreadData *td = arg;
94  AVFrame *out = td->out;
95  AVFrame *in = td->in;
96  const double mix = ctx->is_disabled ? 0. : 1.;
97  const double wet = ctx->is_disabled ? 1. : s->wet_gain;
98  const double dry = ctx->is_disabled ? 1. : s->dry_gain;
99  const double feedback = s->feedback, decay = s->decay;
100  const double b0 = s->b0;
101  const double b1 = s->b1;
102  const double b2 = s->b2;
103  const double a1 = -s->a1;
104  const double a2 = -s->a2;
105  const int start = (in->channels * jobnr) / nb_jobs;
106  const int end = (in->channels * (jobnr+1)) / nb_jobs;
107  const int buffer_samples = s->buffer_samples;
108 
109  for (int ch = start; ch < end; ch++) {
110  const double *src = (const double *)in->extended_data[ch];
111  double *dst = (double *)out->extended_data[ch];
112  double *buffer = (double *)s->buffer->extended_data[ch];
113  double *w = (double *)s->w->extended_data[ch];
114  int write_pos = s->write_pos[ch];
115 
116  for (int n = 0; n < in->nb_samples; n++) {
117  double out_sample;
118 
119  out_sample = src[n] * b0 + w[0];
120  w[0] = b1 * src[n] + w[1] + a1 * out_sample;
121  w[1] = b2 * src[n] + a2 * out_sample;
122 
123  buffer[write_pos] = buffer[write_pos] * decay + out_sample * feedback;
124  dst[n] = (src[n] * dry + buffer[write_pos] * mix) * wet;
125 
126  if (++write_pos >= buffer_samples)
127  write_pos = 0;
128  }
129 
130  s->write_pos[ch] = write_pos;
131  }
132 
133  return 0;
134 }
135 
137 {
138  AVFilterContext *ctx = inlink->dst;
139  AVFilterLink *outlink = ctx->outputs[0];
140  ThreadData td;
141  AVFrame *out;
142 
143  if (av_frame_is_writable(in)) {
144  out = in;
145  } else {
146  out = ff_get_audio_buffer(outlink, in->nb_samples);
147  if (!out) {
148  av_frame_free(&in);
149  return AVERROR(ENOMEM);
150  }
152  }
153 
154  td.in = in; td.out = out;
157 
158  if (out != in)
159  av_frame_free(&in);
160  return ff_filter_frame(outlink, out);
161 }
162 
164 {
165  ASubBoostContext *s = ctx->priv;
166 
167  av_frame_free(&s->buffer);
168  av_frame_free(&s->w);
169  av_freep(&s->write_pos);
170 }
171 
172 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
173  char *res, int res_len, int flags)
174 {
175  int ret;
176 
177  ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
178  if (ret < 0)
179  return ret;
180 
181  return get_coeffs(ctx);
182 }
183 
184 #define OFFSET(x) offsetof(ASubBoostContext, x)
185 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
186 
187 static const AVOption asubboost_options[] = {
188  { "dry", "set dry gain", OFFSET(dry_gain), AV_OPT_TYPE_DOUBLE, {.dbl=0.7}, 0, 1, FLAGS },
189  { "wet", "set wet gain", OFFSET(wet_gain), AV_OPT_TYPE_DOUBLE, {.dbl=0.7}, 0, 1, FLAGS },
190  { "decay", "set decay", OFFSET(decay), AV_OPT_TYPE_DOUBLE, {.dbl=0.7}, 0, 1, FLAGS },
191  { "feedback", "set feedback", OFFSET(feedback), AV_OPT_TYPE_DOUBLE, {.dbl=0.9}, 0, 1, FLAGS },
192  { "cutoff", "set cutoff", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 50, 900, FLAGS },
193  { "slope", "set slope", OFFSET(slope), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0.0001, 1, FLAGS },
194  { "delay", "set delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 1, 100, FLAGS },
195  { NULL }
196 };
197 
198 AVFILTER_DEFINE_CLASS(asubboost);
199 
200 static const AVFilterPad inputs[] = {
201  {
202  .name = "default",
203  .type = AVMEDIA_TYPE_AUDIO,
204  .filter_frame = filter_frame,
205  .config_props = config_input,
206  },
207 };
208 
209 static const AVFilterPad outputs[] = {
210  {
211  .name = "default",
212  .type = AVMEDIA_TYPE_AUDIO,
213  },
214 };
215 
217  .name = "asubboost",
218  .description = NULL_IF_CONFIG_SMALL("Boost subwoofer frequencies."),
219  .priv_size = sizeof(ASubBoostContext),
220  .priv_class = &asubboost_class,
221  .uninit = uninit,
225  .process_command = process_command,
228 };
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:88
td
#define td
Definition: regdef.h:70
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:40
inputs
static const AVFilterPad inputs[]
Definition: af_asubboost.c:200
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:1018
FILTER_SINGLE_SAMPLEFMT
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition: internal.h:184
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:109
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
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:172
w
uint8_t w
Definition: llviddspenc.c:38
AVOption
AVOption.
Definition: opt.h:247
ASubBoostContext::buffer_samples
int buffer_samples
Definition: af_asubboost.c:41
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:169
ThreadData::out
AVFrame * out
Definition: af_adeclick.c:473
formats.h
ASubBoostContext::b1
double b1
Definition: af_asubboost.c:38
ASubBoostContext::w
AVFrame * w
Definition: af_asubboost.c:43
b1
static double b1(void *priv, double x, double y)
Definition: vf_xfade.c:1703
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_asubboost.c:72
outputs
static const AVFilterPad outputs[]
Definition: af_asubboost.c:209
ASubBoostContext::a2
double a2
Definition: af_asubboost.c:37
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
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:257
AVFrame::channels
int channels
number of audio channels, only used for audio.
Definition: frame.h:628
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:226
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:191
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
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:537
ASubBoostContext::b2
double b2
Definition: af_asubboost.c:38
src
#define src
Definition: vp8dsp.c:255
ASubBoostContext::delay
double delay
Definition: af_asubboost.c:33
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_asubboost.c:136
FLAGS
#define FLAGS
Definition: af_asubboost.c:185
get_coeffs
static int get_coeffs(AVFilterContext *ctx)
Definition: af_asubboost.c:47
asubboost_options
static const AVOption asubboost_options[]
Definition: af_asubboost.c:187
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:117
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:473
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:882
b2
static double b2(void *priv, double x, double y)
Definition: vf_xfade.c:1704
ASubBoostContext::b0
double b0
Definition: af_asubboost.c:38
ASubBoostContext::dry_gain
double dry_gain
Definition: af_asubboost.c:29
OFFSET
#define OFFSET(x)
Definition: af_asubboost.c:184
M_PI
#define M_PI
Definition: mathematics.h:52
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:397
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:378
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:803
ThreadData
Used for passing data between threads.
Definition: dsddec.c:67
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:216
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:271
filter_channels
static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_asubboost.c:90
AVFilter
Filter definition.
Definition: avfilter.h:165
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_asubboost.c:163
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
avfilter.h
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:70
ffmath.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:402
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:121
audio.h
ThreadData::in
AVFrame * in
Definition: af_adecorrelate.c:154
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
ASubBoostContext::buffer
AVFrame * buffer
Definition: af_asubboost.c:44
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
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:154
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
b0
static double b0(void *priv, double x, double y)
Definition: vf_xfade.c:1702
ASubBoostContext::cutoff
double cutoff
Definition: af_asubboost.c:34
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:143