FFmpeg
Loading...
Searching...
No Matches
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
27typedef 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
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
77static int config_input(AVFilterLink *inlink)
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
91typedef struct ThreadData {
92 AVFrame *in, *out;
94
95static 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 = ff_slice_pos(in->ch_layout.nb_channels, jobnr, nb_jobs);
112 const int end = ff_slice_pos(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
156static int filter_frame(AVFilterLink *inlink, AVFrame *in)
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"))
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;
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
202static 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
217static 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
231
232static 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 .p.name = "asubboost",
243 .p.description = NULL_IF_CONFIG_SMALL("Boost subwoofer frequencies."),
244 .p.priv_class = &asubboost_class,
247 .priv_size = sizeof(ASubBoostContext),
248 .uninit = uninit,
252 .process_command = process_command,
253};
static int fn filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static const AVFilterPad inputs[]
Definition af_aap.c:299
static int config_input(AVFilterLink *inlink)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static int get_coeffs(AVFilterContext *ctx)
static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static int config_input(AVFilterLink *inlink)
static const AVOption asubboost_options[]
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
const FFFilter ff_af_asubboost
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)
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
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
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:906
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1696
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:846
Main libavfilter public API header.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define FLAGS
Definition cmdutils.c:598
#define av_clipd
Definition common.h:148
#define NULL
Definition coverity.c:32
static __device__ float fabs(float a)
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition ebur128.h:39
internal math functions header
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
@ 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:275
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#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:204
int av_channel_layout_from_string(AVChannelLayout *channel_layout, const char *str)
Initialize a channel layout from a given string description.
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.
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.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
AVChannel
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition samplefmt.h:67
int a
static const int16_t alpha[]
Definition ilbcdata.h:55
#define b
Definition input.c:43
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int mix(int c0, int c1)
Definition 4xm.c:717
const char * arg
Definition jacosubdec.c:65
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
Definition filters.h:763
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition filters.h:257
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
#define M_PI
Definition mathematics.h:67
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
AVOptions.
AVChannelLayout ch_layout
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int nb_samples
number of audio samples (per channel) described by this frame
Definition frame.h:552
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition frame.h:815
uint8_t ** extended_data
pointers to the data planes/channels.
Definition frame.h:533
AVOption.
Definition opt.h:428
Used for passing data between threads.
Definition dsddec.c:71
AVFrame * out
#define av_freep(p)
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
static char buffer[20]
Definition seek.c:32
static double b1(void *priv, double x, double y)
Definition vf_xfade.c:2034
static double b2(void *priv, double x, double y)
Definition vf_xfade.c:2035
static double b0(void *priv, double x, double y)
Definition vf_xfade.c:2033
static double a2(void *priv, double x, double y)
Definition vf_xfade.c:2030
static double a1(void *priv, double x, double y)
Definition vf_xfade.c:2029