FFmpeg
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/random_seed.h"
26 #include "libavutil/opt.h"
27 #include "avfilter.h"
28 #include "audio.h"
29 
30 #define MAX_STAGES 16
31 #define FILTER_FC 1100.0
32 #define RT60_LF 0.1
33 #define RT60_HF 0.008
34 
35 typedef struct APContext {
36  int len, p;
37  double *mx, *my;
38  double b0, b1, a0, a1;
39 } APContext;
40 
41 typedef struct ADecorrelateContext {
42  const AVClass *class;
43 
44  int stages;
45  int64_t seed;
46 
49 
51 
53  int channel,
54  AVFrame *in, AVFrame *out);
56 
57 static int ap_init(APContext *ap, int fs, double delay)
58 {
59  const int delay_samples = lrint(round(delay * fs));
60  const double gain_lf = -60.0 / (RT60_LF * fs) * delay_samples;
61  const double gain_hf = -60.0 / (RT60_HF * fs) * delay_samples;
62  const double w0 = 2.0 * M_PI * FILTER_FC / fs;
63  const double t = tan(w0 / 2.0);
64  const double g_hf = ff_exp10(gain_hf / 20.0);
65  const double gd = ff_exp10((gain_lf-gain_hf) / 20.0);
66  const double sgd = sqrt(gd);
67 
68  ap->len = delay_samples + 1;
69  ap->p = 0;
70  ap->mx = av_calloc(ap->len, sizeof(*ap->mx));
71  ap->my = av_calloc(ap->len, sizeof(*ap->my));
72  if (!ap->mx || !ap->my)
73  return AVERROR(ENOMEM);
74 
75  ap->a0 = t + sgd;
76  ap->a1 = (t - sgd) / ap->a0;
77  ap->b0 = (gd*t - sgd) / ap->a0 * g_hf;
78  ap->b1 = (gd*t + sgd) / ap->a0 * g_hf;
79  ap->a0 = 1.0;
80 
81  return 0;
82 }
83 
84 static void ap_free(APContext *ap)
85 {
86  av_freep(&ap->mx);
87  av_freep(&ap->my);
88 }
89 
90 static double ap_run(APContext *ap, double x)
91 {
92  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;
93  const double r = ap->b1*x + ap->b0*ap->mx[i0] + ap->a1*ap->mx[i_n2] + ap->a0*ap->mx[i_n1] -
94  ap->a1*ap->my[i0] - ap->b0*ap->my[i_n2] - ap->b1*ap->my[i_n1];
95 
96  ap->mx[ap->p] = x;
97  ap->my[ap->p] = r;
98  ap->p = (ap->p+1 >= ap->len) ? 0 : ap->p+1;
99 
100  return r;
101 }
102 
104  AVFrame *in, AVFrame *out)
105 {
106  ADecorrelateContext *s = ctx->priv;
107  const double *src = (const double *)in->extended_data[ch];
108  double *dst = (double *)out->extended_data[ch];
109  const int nb_samples = in->nb_samples;
110  const int stages = s->stages;
111  APContext *ap0 = &s->ap[ch][0];
112 
113  for (int n = 0; n < nb_samples; n++) {
114  dst[n] = ap_run(ap0, src[n]);
115  for (int i = 1; i < stages; i++) {
116  APContext *ap = &s->ap[ch][i];
117 
118  dst[n] = ap_run(ap, dst[n]);
119  }
120  }
121 }
122 
124 {
125  AVFilterContext *ctx = inlink->dst;
126  ADecorrelateContext *s = ctx->priv;
127  int ret;
128 
129  if (s->seed == -1)
130  s->seed = av_get_random_seed();
131  av_lfg_init(&s->c, s->seed);
132 
133  s->nb_channels = inlink->ch_layout.nb_channels;
134  s->ap = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->ap));
135  if (!s->ap)
136  return AVERROR(ENOMEM);
137 
138  for (int i = 0; i < inlink->ch_layout.nb_channels; i++) {
139  for (int j = 0; j < s->stages; j++) {
140  ret = ap_init(&s->ap[i][j], inlink->sample_rate,
141  (double)av_lfg_get(&s->c) / 0xffffffff * 2.2917e-3 + 0.83333e-3);
142  if (ret < 0)
143  return ret;
144  }
145  }
146 
147  s->filter_channel = filter_channel_dbl;
148 
149  return 0;
150 }
151 
152 typedef struct ThreadData {
154 } ThreadData;
155 
156 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
157 {
158  ADecorrelateContext *s = ctx->priv;
159  ThreadData *td = arg;
160  AVFrame *out = td->out;
161  AVFrame *in = td->in;
162  const int start = (in->ch_layout.nb_channels * jobnr) / nb_jobs;
163  const int end = (in->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
164 
165  for (int ch = start; ch < end; ch++)
166  s->filter_channel(ctx, ch, in, out);
167 
168  return 0;
169 }
170 
172 {
173  AVFilterContext *ctx = inlink->dst;
174  AVFilterLink *outlink = ctx->outputs[0];
175  AVFrame *out;
176  ThreadData td;
177 
178  if (av_frame_is_writable(in)) {
179  out = in;
180  } else {
181  out = ff_get_audio_buffer(outlink, in->nb_samples);
182  if (!out) {
183  av_frame_free(&in);
184  return AVERROR(ENOMEM);
185  }
187  }
188 
189  td.in = in; td.out = out;
191  FFMIN(inlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
192 
193  if (out != in)
194  av_frame_free(&in);
195  return ff_filter_frame(outlink, out);
196 }
197 
199 {
200  ADecorrelateContext *s = ctx->priv;
201 
202  if (s->ap) {
203  for (int ch = 0; ch < s->nb_channels; ch++) {
204  for (int stage = 0; stage < s->stages; stage++)
205  ap_free(&s->ap[ch][stage]);
206  }
207  }
208 
209  av_freep(&s->ap);
210 }
211 
212 #define OFFSET(x) offsetof(ADecorrelateContext, x)
213 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
214 
215 static const AVOption adecorrelate_options[] = {
216  { "stages", "set filtering stages", OFFSET(stages), AV_OPT_TYPE_INT, {.i64=6}, 1, MAX_STAGES, FLAGS },
217  { "seed", "set random seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64=-1}, -1, UINT_MAX, FLAGS },
218  { NULL }
219 };
220 
221 AVFILTER_DEFINE_CLASS(adecorrelate);
222 
223 static const AVFilterPad inputs[] = {
224  {
225  .name = "default",
226  .type = AVMEDIA_TYPE_AUDIO,
227  .filter_frame = filter_frame,
228  .config_props = config_input,
229  },
230 };
231 
233  .name = "adecorrelate",
234  .description = NULL_IF_CONFIG_SMALL("Apply decorrelation to input audio."),
235  .priv_size = sizeof(ADecorrelateContext),
236  .priv_class = &adecorrelate_class,
237  .uninit = uninit,
243 };
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:107
td
#define td
Definition: regdef.h:70
ff_exp10
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
Definition: ffmath.h:42
APContext
Definition: af_adecorrelate.c:35
r
const char * r
Definition: vf_curves.c:126
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
filter_channels
static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: af_adecorrelate.c:156
ADecorrelateContext::filter_channel
void(* filter_channel)(AVFilterContext *ctx, int channel, AVFrame *in, AVFrame *out)
Definition: af_adecorrelate.c:52
APContext::a0
double a0
Definition: af_adecorrelate.c:38
out
FILE * out
Definition: movenc.c:54
av_lfg_init
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:978
inputs
static const AVFilterPad inputs[]
Definition: af_adecorrelate.c:223
FILTER_SINGLE_SAMPLEFMT
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition: internal.h:185
APContext::my
double * my
Definition: af_adecorrelate.c:37
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:100
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AVOption
AVOption.
Definition: opt.h:251
adecorrelate_options
static const AVOption adecorrelate_options[]
Definition: af_adecorrelate.c:215
ADecorrelateContext::c
AVLFG c
Definition: af_adecorrelate.c:50
ADecorrelateContext::ap
APContext(* ap)[MAX_STAGES]
Definition: af_adecorrelate.c:48
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
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:317
MAX_STAGES
#define MAX_STAGES
Definition: af_adecorrelate.c:30
ThreadData::in
AVFrame * in
Definition: af_adecorrelate.c:153
APContext::b1
double b1
Definition: af_adecorrelate.c:38
ap_run
static double ap_run(APContext *ap, double x)
Definition: af_adecorrelate.c:90
av_get_random_seed
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:167
config_input
static int config_input(AVFilterLink *inlink)
Definition: af_adecorrelate.c:123
ap_init
static int ap_init(APContext *ap, int fs, double delay)
Definition: af_adecorrelate.c:57
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_adecorrelate.c:171
APContext::p
int p
Definition: af_adecorrelate.c:36
FLAGS
#define FLAGS
Definition: af_adecorrelate.c:213
AVFrame::ch_layout
AVChannelLayout ch_layout
Channel layout of the audio data.
Definition: frame.h:802
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:47
lrint
#define lrint
Definition: tablegen.h:53
av_cold
#define av_cold
Definition: attributes.h:90
APContext::a1
double a1
Definition: af_adecorrelate.c:38
s
#define s(width, name)
Definition: cbs_vp9.c:198
OFFSET
#define OFFSET(x)
Definition: af_adecorrelate.c:212
av_lfg_get
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:53
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ADecorrelateContext::stages
int stages
Definition: af_adecorrelate.c:44
lfg.h
AV_OPT_TYPE_INT64
@ AV_OPT_TYPE_INT64
Definition: opt.h:226
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:192
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:736
fs
#define fs(width, name, subs,...)
Definition: cbs_vp9.c:200
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:32
seed
static unsigned int seed
Definition: videogen.c:78
AVLFG
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
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:106
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(adecorrelate)
ap_free
static void ap_free(APContext *ap)
Definition: af_adecorrelate.c:84
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:666
filter_channel_dbl
static void filter_channel_dbl(AVFilterContext *ctx, int ch, AVFrame *in, AVFrame *out)
Definition: af_adecorrelate.c:103
ADecorrelateContext
Definition: af_adecorrelate.c:41
APContext::b0
double b0
Definition: af_adecorrelate.c:38
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_adecorrelate.c:198
APContext::mx
double * mx
Definition: af_adecorrelate.c:37
M_PI
#define M_PI
Definition: mathematics.h:67
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#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:147
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:420
APContext::len
int len
Definition: af_adecorrelate.c:36
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
round
static av_always_inline av_const double round(double x)
Definition: libm.h:444
RT60_LF
#define RT60_LF
Definition: af_adecorrelate.c:32
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:401
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:786
ThreadData
Used for passing data between threads.
Definition: dsddec.c:69
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:53
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
AVFilter
Filter definition.
Definition: avfilter.h:166
ff_af_adecorrelate
const AVFilter ff_af_adecorrelate
Definition: af_adecorrelate.c:232
ret
ret
Definition: filter_design.txt:187
RT60_HF
#define RT60_HF
Definition: af_adecorrelate.c:33
random_seed.h
channel_layout.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
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:397
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:117
audio.h
ADecorrelateContext::seed
int64_t seed
Definition: af_adecorrelate.c:45
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:193
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
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:144
FILTER_FC
#define FILTER_FC
Definition: af_adecorrelate.c:31
channel
channel
Definition: ebur128.h:39
ADecorrelateContext::nb_channels
int nb_channels
Definition: af_adecorrelate.c:47