History

avfilter/af_aemphasis: add timeline/slice and commands support

Paul B Mahol committed 5 years ago

Commit 7a95cf81 parent 1a25c6f2 files · +83 −48Browse files

Showing the changes to libavfilter (1 of 2 files). Show all

Unified · Split

libavfilter/af_aemphasis.c+79−48
@@ -27,13 +27,9 @@ typedef struct BiquadCoeffs { · 26 unmodified lines
2727 double a0, a1, a2, b1, b2;
2828 } BiquadCoeffs;
2929
30−typedef struct BiquadD2 {
31− double a0, a1, a2, b1, b2, w1, w2;
32−} BiquadD2;
33−
3430 typedef struct RIAACurve {
35− BiquadD2 r1;
36− BiquadD2 brickw;
31+ BiquadCoeffs r1;
32+ BiquadCoeffs brickw;
3733 int use_brickw;
3834 } RIAACurve;
3935
@@ -42,11 +38,13 @@ typedef struct AudioEmphasisContext { · 2 unmodified lines
4238 int mode, type;
4339 double level_in, level_out;
4440
45− RIAACurve *rc;
41+ RIAACurve rc;
42+
43+ AVFrame *w;
4644 } AudioEmphasisContext;
4745
4846 #define OFFSET(x) offsetof(AudioEmphasisContext, x)
49−#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
47+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
5048
5149 static const AVOption aemphasis_options[] = {
5250 { "level_in", "set input gain", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, FLAGS },
@@ -69,16 +67,16 @@ static const AVOption aemphasis_options[] = { · 16 unmodified lines
6967
7068 AVFILTER_DEFINE_CLASS(aemphasis);
7169
72−static inline void biquad_process(BiquadD2 *bq, double *dst, const double *src, int nb_samples,
73− double level_in, double level_out)
70+static inline void biquad_process(BiquadCoeffs *bq, double *dst, const double *src, int nb_samples,
71+ double *w, double level_in, double level_out)
7472 {
7573 const double a0 = bq->a0;
7674 const double a1 = bq->a1;
7775 const double a2 = bq->a2;
7876 const double b1 = bq->b1;
7977 const double b2 = bq->b2;
80− double w1 = bq->w1;
81− double w2 = bq->w2;
78+ double w1 = w[0];
79+ double w2 = w[1];
8280
8381 for (int i = 0; i < nb_samples; i++) {
8482 double n = src[i] * level_in;
@@ -91,17 +89,46 @@ static inline void biquad_process(BiquadD2 *bq, double *dst, const double *src, · 6 unmodified lines
9189 dst[i] = out * level_out;
9290 }
9391
94− bq->w1 = w1;
95− bq->w2 = w2;
92+ w[0] = w1;
93+ w[1] = w2;
9694 }
9795
98−static int filter_frame(AVFilterLink *inlink, AVFrame *in)
96+typedef struct ThreadData {
97+ AVFrame *in, *out;
98+} ThreadData;
99+
100+static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
99101 {
100− AVFilterContext *ctx = inlink->dst;
101− AVFilterLink *outlink = ctx->outputs[0];
102102 AudioEmphasisContext *s = ctx->priv;
103103 const double level_out = s->level_out;
104104 const double level_in = s->level_in;
105+ ThreadData *td = arg;
106+ AVFrame *out = td->out;
107+ AVFrame *in = td->in;
108+ const int start = (in->channels * jobnr) / nb_jobs;
109+ const int end = (in->channels * (jobnr+1)) / nb_jobs;
110+
111+ for (int ch = start; ch < end; ch++) {
112+ const double *src = (const double *)in->extended_data[ch];
113+ double *w = (double *)s->w->extended_data[ch];
114+ double *dst = (double *)out->extended_data[ch];
115+
116+ if (s->rc.use_brickw) {
117+ biquad_process(&s->rc.brickw, dst, src, in->nb_samples, w + 2, level_in, 1.);
118+ biquad_process(&s->rc.r1, dst, dst, in->nb_samples, w, 1., level_out);
119+ } else {
120+ biquad_process(&s->rc.r1, dst, src, in->nb_samples, w, level_in, level_out);
121+ }
122+ }
123+
124+ return 0;
125+}
126+
127+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
128+{
129+ AVFilterContext *ctx = inlink->dst;
130+ AVFilterLink *outlink = ctx->outputs[0];
131+ ThreadData td;
105132 AVFrame *out;
106133
107134 if (av_frame_is_writable(in)) {
@@ -115,17 +142,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 7 unmodified lines
115142 av_frame_copy_props(out, in);
116143 }
117144
118− for (int ch = 0; ch < inlink->channels; ch++) {
119− const double *src = (const double *)in->extended_data[ch];
120− double *dst = (double *)out->extended_data[ch];
121−
122− if (s->rc[ch].use_brickw) {
123− biquad_process(&s->rc[ch].brickw, dst, src, in->nb_samples, level_in, 1.);
124− biquad_process(&s->rc[ch].r1, dst, dst, in->nb_samples, 1., level_out);
125− } else {
126− biquad_process(&s->rc[ch].r1, dst, src, in->nb_samples, level_in, level_out);
127− }
128− }
145+ td.in = in; td.out = out;
146+ ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(inlink->channels,
147+ ff_filter_get_nb_threads(ctx)));
129148
130149 if (in != out)
131150 av_frame_free(&in);
@@ -162,7 +181,7 @@ static int query_formats(AVFilterContext *ctx) · 30 unmodified lines
162181 return ff_set_common_samplerates(ctx, formats);
163182 }
164183
165−static inline void set_highshelf_rbj(BiquadD2 *bq, double freq, double q, double peak, double sr)
184+static inline void set_highshelf_rbj(BiquadCoeffs *bq, double freq, double q, double peak, double sr)
166185 {
167186 double A = sqrt(peak);
168187 double w0 = freq * 2 * M_PI / sr;
@@ -186,7 +205,7 @@ static inline void set_highshelf_rbj(BiquadD2 *bq, double freq, double q, double · 17 unmodified lines
186205 bq->a2 *= ib0;
187206 }
188207
189−static inline void set_lp_rbj(BiquadD2 *bq, double fc, double q, double sr, double gain)
208+static inline void set_lp_rbj(BiquadCoeffs *bq, double fc, double q, double sr, double gain)
190209 {
191210 double omega = 2.0 * M_PI * fc / sr;
192211 double sn = sin(omega);
@@ -220,10 +239,10 @@ static int config_input(AVFilterLink *inlink) · 27 unmodified lines
220239 AVFilterContext *ctx = inlink->dst;
221240 AudioEmphasisContext *s = ctx->priv;
222241 BiquadCoeffs coeffs;
223− int ch;
224242
225− s->rc = av_calloc(inlink->channels, sizeof(*s->rc));
226− if (!s->rc)
243+ if (!s->w)
244+ s->w = ff_get_audio_buffer(inlink, 4);
245+ if (!s->w)
227246 return AVERROR(ENOMEM);
228247
229248 switch (s->type) {
@@ -297,12 +316,12 @@ static int config_input(AVFilterLink *inlink) · 67 unmodified lines
297316 if (s->type == 7)
298317 q = pow((sr / 4750.0) + 19.5, -0.25);
299318 if (s->mode == 0)
300− set_highshelf_rbj(&s->rc[0].r1, cfreq, q, 1. / gain, sr);
319+ set_highshelf_rbj(&s->rc.r1, cfreq, q, 1. / gain, sr);
301320 else
302− set_highshelf_rbj(&s->rc[0].r1, cfreq, q, gain, sr);
303− s->rc[0].use_brickw = 0;
321+ set_highshelf_rbj(&s->rc.r1, cfreq, q, gain, sr);
322+ s->rc.use_brickw = 0;
304323 } else {
305− s->rc[0].use_brickw = 1;
324+ s->rc.use_brickw = 1;
306325 if (s->mode == 0) { // Reproduction
307326 g = 1. / (4.+2.*i*t+2.*k*t+i*k*t*t);
308327 a0 = (2.*t+j*t*t)*g;
@@ -331,27 +350,36 @@ static int config_input(AVFilterLink *inlink) · 22 unmodified lines
331350 gain1kHz = freq_gain(&coeffs, 1000.0, sr);
332351 // divide one filter's x[n-m] coefficients by that value
333352 gc = 1.0 / gain1kHz;
334− s->rc[0].r1.a0 = coeffs.a0 * gc;
335− s->rc[0].r1.a1 = coeffs.a1 * gc;
336− s->rc[0].r1.a2 = coeffs.a2 * gc;
337− s->rc[0].r1.b1 = coeffs.b1;
338− s->rc[0].r1.b2 = coeffs.b2;
353+ s->rc.r1.a0 = coeffs.a0 * gc;
354+ s->rc.r1.a1 = coeffs.a1 * gc;
355+ s->rc.r1.a2 = coeffs.a2 * gc;
356+ s->rc.r1.b1 = coeffs.b1;
357+ s->rc.r1.b2 = coeffs.b2;
339358 }
340359
341360 cutfreq = FFMIN(0.45 * sr, 21000.);
342− set_lp_rbj(&s->rc[0].brickw, cutfreq, 0.707, sr, 1.);
343−
344− for (ch = 1; ch < inlink->channels; ch++) {
345− memcpy(&s->rc[ch], &s->rc[0], sizeof(RIAACurve));
346− }
361+ set_lp_rbj(&s->rc.brickw, cutfreq, 0.707, sr, 1.);
347362
348363 return 0;
349364 }
350365
366+static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
367+ char *res, int res_len, int flags)
368+{
369+ int ret;
370+
371+ ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
372+ if (ret < 0)
373+ return ret;
374+
375+ return config_input(ctx->inputs[0]);
376+}
377+
351378 static av_cold void uninit(AVFilterContext *ctx)
352379 {
353380 AudioEmphasisContext *s = ctx->priv;
354− av_freep(&s->rc);
381+
382+ av_frame_free(&s->w);
355383 }
356384
357385 static const AVFilterPad avfilter_af_aemphasis_inputs[] = {
@@ -381,4 +409,7 @@ AVFilter ff_af_aemphasis = { · 23 unmodified lines
381409 .query_formats = query_formats,
382410 .inputs = avfilter_af_aemphasis_inputs,
383411 .outputs = avfilter_af_aemphasis_outputs,
412+ .process_command = process_command,
413+ .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
414+ AVFILTER_FLAG_SLICE_THREADS,
384415 };

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t