FFmpeg
Loading...
Searching...
No Matches
af_adecorrelate.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013-2020 Michael Barbour <barbour.michael.0@gmail.com>
3 * Copyright (c) 2021 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
23#include "libavutil/ffmath.h"
24#include "libavutil/lfg.h"
25#include "libavutil/mem.h"
27#include "libavutil/opt.h"
28#include "avfilter.h"
29#include "audio.h"
30#include "filters.h"
31
32#define MAX_STAGES 16
33#define FILTER_FC 1100.0
34#define RT60_LF 0.1
35#define RT60_HF 0.008
36
37typedef struct APContext {
38 int len, p;
39 double *mx, *my;
40 double b0, b1, a0, a1;
41} APContext;
42
58
59static int ap_init(APContext *ap, int fs, double delay)
60{
61 const int delay_samples = lrint(round(delay * fs));
62 const double gain_lf = -60.0 / (RT60_LF * fs) * delay_samples;
63 const double gain_hf = -60.0 / (RT60_HF * fs) * delay_samples;
64 const double w0 = 2.0 * M_PI * FILTER_FC / fs;
65 const double t = tan(w0 / 2.0);
66 const double g_hf = ff_exp10(gain_hf / 20.0);
67 const double gd = ff_exp10((gain_lf-gain_hf) / 20.0);
68 const double sgd = sqrt(gd);
69
70 ap->len = delay_samples + 1;
71 ap->p = 0;
72 ap->mx = av_calloc(ap->len, sizeof(*ap->mx));
73 ap->my = av_calloc(ap->len, sizeof(*ap->my));
74 if (!ap->mx || !ap->my)
75 return AVERROR(ENOMEM);
76
77 ap->a0 = t + sgd;
78 ap->a1 = (t - sgd) / ap->a0;
79 ap->b0 = (gd*t - sgd) / ap->a0 * g_hf;
80 ap->b1 = (gd*t + sgd) / ap->a0 * g_hf;
81 ap->a0 = 1.0;
82
83 return 0;
84}
85
86static void ap_free(APContext *ap)
87{
88 av_freep(&ap->mx);
89 av_freep(&ap->my);
90}
91
92static double ap_run(APContext *ap, double x)
93{
94 const int i0 = ((ap->p < 1) ? ap->len : ap->p)-1, i_n1 = ap->p, i_n2 = (ap->p+1 >= ap->len) ? 0 : ap->p+1;
95 const double r = ap->b1*x + ap->b0*ap->mx[i0] + ap->a1*ap->mx[i_n2] + ap->a0*ap->mx[i_n1] -
96 ap->a1*ap->my[i0] - ap->b0*ap->my[i_n2] - ap->b1*ap->my[i_n1];
97
98 ap->mx[ap->p] = x;
99 ap->my[ap->p] = r;
100 ap->p = (ap->p+1 >= ap->len) ? 0 : ap->p+1;
101
102 return r;
103}
104
106 AVFrame *in, AVFrame *out)
107{
108 ADecorrelateContext *s = ctx->priv;
109 const double *src = (const double *)in->extended_data[ch];
110 double *dst = (double *)out->extended_data[ch];
111 const int nb_samples = in->nb_samples;
112 const int stages = s->stages;
113 APContext *ap0 = &s->ap[ch][0];
114
115 for (int n = 0; n < nb_samples; n++) {
116 dst[n] = ap_run(ap0, src[n]);
117 for (int i = 1; i < stages; i++) {
118 APContext *ap = &s->ap[ch][i];
119
120 dst[n] = ap_run(ap, dst[n]);
121 }
122 }
123}
124
125static int config_input(AVFilterLink *inlink)
126{
127 AVFilterContext *ctx = inlink->dst;
128 ADecorrelateContext *s = ctx->priv;
129 int ret;
130
131 if (s->seed == -1)
132 s->seed = av_get_random_seed();
133 av_lfg_init(&s->c, s->seed);
134
135 s->nb_channels = inlink->ch_layout.nb_channels;
136 s->ap = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->ap));
137 if (!s->ap)
138 return AVERROR(ENOMEM);
139
140 for (int i = 0; i < inlink->ch_layout.nb_channels; i++) {
141 for (int j = 0; j < s->stages; j++) {
142 ret = ap_init(&s->ap[i][j], inlink->sample_rate,
143 (double)av_lfg_get(&s->c) / 0xffffffff * 2.2917e-3 + 0.83333e-3);
144 if (ret < 0)
145 return ret;
146 }
147 }
148
149 s->filter_channel = filter_channel_dbl;
150
151 return 0;
152}
153
154typedef struct ThreadData {
156} ThreadData;
157
158static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
159{
160 ADecorrelateContext *s = ctx->priv;
161 ThreadData *td = arg;
162 AVFrame *out = td->out;
163 AVFrame *in = td->in;
164 const int start = ff_slice_pos(in->ch_layout.nb_channels, jobnr, nb_jobs);
165 const int end = ff_slice_pos(in->ch_layout.nb_channels, jobnr + 1, nb_jobs);
166
167 for (int ch = start; ch < end; ch++)
168 s->filter_channel(ctx, ch, in, out);
169
170 return 0;
171}
172
173static int filter_frame(AVFilterLink *inlink, AVFrame *in)
174{
175 AVFilterContext *ctx = inlink->dst;
176 AVFilterLink *outlink = ctx->outputs[0];
177 AVFrame *out;
178 ThreadData td;
179
180 if (av_frame_is_writable(in)) {
181 out = in;
182 } else {
183 out = ff_get_audio_buffer(outlink, in->nb_samples);
184 if (!out) {
185 av_frame_free(&in);
186 return AVERROR(ENOMEM);
187 }
189 }
190
191 td.in = in; td.out = out;
194
195 if (out != in)
196 av_frame_free(&in);
197 return ff_filter_frame(outlink, out);
198}
199
201{
202 ADecorrelateContext *s = ctx->priv;
203
204 if (s->ap) {
205 for (int ch = 0; ch < s->nb_channels; ch++) {
206 for (int stage = 0; stage < s->stages; stage++)
207 ap_free(&s->ap[ch][stage]);
208 }
209 }
210
211 av_freep(&s->ap);
212}
213
214#define OFFSET(x) offsetof(ADecorrelateContext, x)
215#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
216
218 { "stages", "set filtering stages", OFFSET(stages), AV_OPT_TYPE_INT, {.i64=6}, 1, MAX_STAGES, FLAGS },
219 { "seed", "set random seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64=-1}, -1, UINT_MAX, FLAGS },
220 { NULL }
221};
222
224
225static const AVFilterPad inputs[] = {
226 {
227 .name = "default",
228 .type = AVMEDIA_TYPE_AUDIO,
229 .filter_frame = filter_frame,
230 .config_props = config_input,
231 },
232};
233
235 .p.name = "adecorrelate",
236 .p.description = NULL_IF_CONFIG_SMALL("Apply decorrelation to input audio."),
237 .p.priv_class = &adecorrelate_class,
240 .priv_size = sizeof(ADecorrelateContext),
241 .uninit = uninit,
245};
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 const AVOption adecorrelate_options[]
static void ap_free(APContext *ap)
#define MAX_STAGES
static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static int config_input(AVFilterLink *inlink)
#define RT60_LF
static void filter_channel_dbl(AVFilterContext *ctx, int ch, AVFrame *in, AVFrame *out)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static av_cold void uninit(AVFilterContext *ctx)
#define FILTER_FC
#define RT60_HF
static double ap_run(APContext *ap, double x)
#define OFFSET(x)
static int ap_init(APContext *ap, int fs, double delay)
const FFFilter ff_af_adecorrelate
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_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 i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define fs(width, name, subs,...)
Definition cbs_vp9.c:200
Public libavutil channel layout APIs header.
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
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
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
Definition ffmath.h:42
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition opt.h:262
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#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
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
#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
#define r
Definition input.c:42
static av_cold void uninit(AVBitStreamFilterContext *ctx)
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition lfg.c:32
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition lfg.h:53
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
static av_always_inline av_const double round(double x)
Definition libm.h:446
#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.
void(* filter_channel)(AVFilterContext *ctx, int channel, AVFrame *in, AVFrame *out)
APContext(* ap)[MAX_STAGES]
double * mx
double * my
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
Context structure for the Lagged Fibonacci PRNG.
Definition lfg.h:33
AVOption.
Definition opt.h:428
Used for passing data between threads.
Definition dsddec.c:71
AVFrame * out
#define lrint
Definition tablegen.h:53
#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 unsigned int seed
Definition videogen.c:78