FFmpeg
Loading...
Searching...
No Matches
af_atilt.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 "filters.h"
25
26#define MAX_ORDER 30
27
28typedef struct Coeffs {
29 double g;
30 double a1;
31 double b0, b1;
32} Coeffs;
33
34typedef struct ATiltContext {
35 const AVClass *class;
36
37 double freq;
38 double level;
39 double slope;
40 double width;
41 int order;
42
44
46
47 int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
49
50static double prewarp(double w, double T, double wp)
51{
52 return wp * tan(w * T * 0.5) / tan(wp * T * 0.5);
53}
54
55static double mz(int i, double w0, double r, double alpha)
56{
57 return w0 * pow(r, -alpha + i);
58}
59
60static double mp(int i, double w0, double r)
61{
62 return w0 * pow(r, i);
63}
64
65static double mzh(int i, double T, double w0, double r, double alpha)
66{
67 return prewarp(mz(i, w0, r, alpha), T, w0);
68}
69
70static double mph(int i, double T, double w0, double r)
71{
72 return prewarp(mp(i, w0, r), T, w0);
73}
74
75static void set_tf1s(Coeffs *coeffs, double b1, double b0, double a0,
76 double w1, double sr, double alpha)
77{
78 double c = 1.0 / tan(w1 * 0.5 / sr);
79 double d = a0 + c;
80
81 coeffs->b1 = (b0 - b1 * c) / d;
82 coeffs->b0 = (b0 + b1 * c) / d;
83 coeffs->a1 = (a0 - c) / d;
84 coeffs->g = a0 / b0;
85}
86
88 int order, double sr, double f0,
89 double bw, double alpha)
90{
91 ATiltContext *s = ctx->priv;
92 const double w0 = 2. * M_PI * f0;
93 const double f1 = f0 + bw;
94 const double w1 = 1.;
95 const double r = pow(f1 / f0, 1.0 / (order - 1.0));
96 const double T = 1. / sr;
97
98 for (int i = 0; i < order; i++) {
99 Coeffs *coeffs = &s->coeffs[i];
100
101 set_tf1s(coeffs, 1.0, mzh(i, T, w0, r, alpha), mph(i, T, w0, r),
102 w1, sr, alpha);
103 }
104}
105
107{
108 ATiltContext *s = ctx->priv;
109 AVFilterLink *inlink = ctx->inputs[0];
110
111 set_filter(ctx, s->order, inlink->sample_rate, s->freq, s->width, s->slope);
112
113 return 0;
114}
115
116typedef struct ThreadData {
117 AVFrame *in, *out;
118} ThreadData;
119
120#define FILTER(name, type) \
121static int filter_channels_## name(AVFilterContext *ctx, void *arg, \
122 int jobnr, int nb_jobs) \
123{ \
124 ATiltContext *s = ctx->priv; \
125 ThreadData *td = arg; \
126 AVFrame *out = td->out; \
127 AVFrame *in = td->in; \
128 const int start = ff_slice_pos(in->ch_layout.nb_channels, jobnr, nb_jobs); \
129 const int end = ff_slice_pos(in->ch_layout.nb_channels, jobnr + 1, nb_jobs); \
130 const type level = s->level; \
131 \
132 for (int ch = start; ch < end; ch++) { \
133 const type *src = (const type *)in->extended_data[ch]; \
134 type *dst = (type *)out->extended_data[ch]; \
135 \
136 for (int b = 0; b < s->order; b++) { \
137 Coeffs *coeffs = &s->coeffs[b]; \
138 const type g = coeffs->g; \
139 const type a1 = coeffs->a1; \
140 const type b0 = coeffs->b0; \
141 const type b1 = coeffs->b1; \
142 type *w = ((type *)s->w->extended_data[ch]) + b * 2; \
143 \
144 for (int n = 0; n < in->nb_samples; n++) { \
145 type sain = b ? dst[n] : src[n] * level; \
146 type saout = sain * b0 + w[0] * b1 - w[1] * a1; \
147 \
148 w[0] = sain; \
149 w[1] = saout; \
150 \
151 dst[n] = saout * g; \
152 } \
153 } \
154 } \
155 \
156 return 0; \
157}
158
159FILTER(fltp, float)
160FILTER(dblp, double)
161
162static int config_input(AVFilterLink *inlink)
163{
164 AVFilterContext *ctx = inlink->dst;
165 ATiltContext *s = ctx->priv;
166
167 switch (inlink->format) {
168 case AV_SAMPLE_FMT_FLTP: s->filter_channels = filter_channels_fltp; break;
169 case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break;
170 }
171
172 s->w = ff_get_audio_buffer(inlink, 2 * MAX_ORDER);
173 if (!s->w)
174 return AVERROR(ENOMEM);
175
176 return get_coeffs(ctx);
177}
178
179static int filter_frame(AVFilterLink *inlink, AVFrame *in)
180{
181 AVFilterContext *ctx = inlink->dst;
182 ATiltContext *s = ctx->priv;
183 AVFilterLink *outlink = ctx->outputs[0];
184 ThreadData td;
185 AVFrame *out;
186
187 if (av_frame_is_writable(in)) {
188 out = in;
189 } else {
190 out = ff_get_audio_buffer(outlink, in->nb_samples);
191 if (!out) {
192 av_frame_free(&in);
193 return AVERROR(ENOMEM);
194 }
196 }
197
198 td.in = in; td.out = out;
199 ff_filter_execute(ctx, s->filter_channels, &td, NULL, FFMIN(inlink->ch_layout.nb_channels,
201
202 if (out != in)
203 av_frame_free(&in);
204 return ff_filter_frame(outlink, out);
205}
206
207static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
208 char *res, int res_len, int flags)
209{
210 int ret;
211
212 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
213 if (ret < 0)
214 return ret;
215
216 return get_coeffs(ctx);
217}
218
220{
221 ATiltContext *s = ctx->priv;
222
223 av_frame_free(&s->w);
224}
225
226#define OFFSET(x) offsetof(ATiltContext, x)
227#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
228
229static const AVOption atilt_options[] = {
230 { "freq", "set central frequency",OFFSET(freq), AV_OPT_TYPE_DOUBLE, {.dbl=10000}, 20, 192000, FLAGS },
231 { "slope", "set filter slope", OFFSET(slope), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
232 { "width", "set filter width", OFFSET(width), AV_OPT_TYPE_DOUBLE, {.dbl=1000}, 100, 10000, FLAGS },
233 { "order", "set filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=5}, 2,MAX_ORDER, FLAGS },
234 { "level", "set input level", OFFSET(level), AV_OPT_TYPE_DOUBLE, {.dbl=1.}, 0., 4., FLAGS },
235 { NULL }
236};
237
239
240static const AVFilterPad inputs[] = {
241 {
242 .name = "default",
243 .type = AVMEDIA_TYPE_AUDIO,
244 .filter_frame = filter_frame,
245 .config_props = config_input,
246 },
247};
248
250 .p.name = "atilt",
251 .p.description = NULL_IF_CONFIG_SMALL("Apply spectral tilt to audio."),
252 .p.priv_class = &atilt_class,
255 .priv_size = sizeof(ATiltContext),
256 .uninit = uninit,
260 .process_command = process_command,
261};
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 void set_tf1s(Coeffs *coeffs, double b1, double b0, double a0, double w1, double sr, double alpha)
Definition af_atilt.c:75
static int get_coeffs(AVFilterContext *ctx)
Definition af_atilt.c:106
static double mph(int i, double T, double w0, double r)
Definition af_atilt.c:70
static void set_filter(AVFilterContext *ctx, int order, double sr, double f0, double bw, double alpha)
Definition af_atilt.c:87
static double mp(int i, double w0, double r)
Definition af_atilt.c:60
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition af_atilt.c:179
static double mzh(int i, double T, double w0, double r, double alpha)
Definition af_atilt.c:65
#define MAX_ORDER
Definition af_atilt.c:26
#define FILTER(name, type)
Definition af_atilt.c:120
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition af_atilt.c:207
static av_cold void uninit(AVFilterContext *ctx)
Definition af_atilt.c:219
static double mz(int i, double w0, double r, double alpha)
Definition af_atilt.c:55
#define OFFSET(x)
Definition af_atilt.c:226
const FFFilter ff_af_atilt
Definition af_atilt.c:249
static double prewarp(double w, double T, double wp)
Definition af_atilt.c:50
static const AVOption atilt_options[]
Definition af_atilt.c:229
#define T(x)
Definition vpx_arith.h:29
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 i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
internal math functions header
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition avfilter.h:196
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#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_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition samplefmt.h:67
static const int16_t alpha[]
Definition ilbcdata.h:55
#define r
Definition input.c:42
static av_cold void uninit(AVBitStreamFilterContext *ctx)
const char * arg
Definition jacosubdec.c:65
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_SAMPLEFMTS(...)
Definition filters.h:252
#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
AVOptions.
double level
Definition af_atilt.c:38
int(* filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition af_atilt.c:47
AVFrame * w
Definition af_atilt.c:45
double freq
Definition af_atilt.c:37
Coeffs coeffs[MAX_ORDER]
Definition af_atilt.c:43
double slope
Definition af_atilt.c:39
double width
Definition af_atilt.c:40
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
AVOption.
Definition opt.h:428
double a1
Definition af_atilt.c:30
double g
Definition af_atilt.c:29
double b1
Definition af_atilt.c:31
double b0
Definition af_atilt.c:31
Used for passing data between threads.
Definition dsddec.c:71
AVFrame * out
uint8_t level
Definition svq3.c:208
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
#define width
Definition dsp.h:89
#define MAX_ORDER
Definition ttadata.h:28
static double b1(void *priv, double x, double y)
Definition vf_xfade.c:2034
static double a0(void *priv, double x, double y)
Definition vf_xfade.c:2028
static double b0(void *priv, double x, double y)
Definition vf_xfade.c:2033
static double c[64]