Browse code

lavfi: add aeval filter

Stefano Sabatini authored on 2013/12/06 02:23:06
Showing 6 changed files
... ...
@@ -12,6 +12,7 @@ version <next>
12 12
 - complete Voxware MetaSound decoder
13 13
 - remove mp3_header_compress bitstream filters
14 14
 - Windows resource files for shared libraries
15
+- aeval filter
15 16
 
16 17
 
17 18
 version 2.1:
... ...
@@ -435,6 +435,70 @@ aecho=0.8:0.9:1000|1800:0.3|0.25
435 435
 @end example
436 436
 @end itemize
437 437
 
438
+@section aeval
439
+
440
+Modify an audio signal according to the specified expressions.
441
+
442
+This filter accepts one or more expressions (one for each channel),
443
+which are evaluated and used to modify a corresponding audio signal.
444
+
445
+This filter accepts the following options:
446
+
447
+@table @option
448
+@item exprs
449
+Set the '|'-separated expressions list for each separate channel. If
450
+the number of input channels is greater than the number of
451
+expressions, the last specified expression is used for the remaining
452
+output channels.
453
+
454
+@item channel_layout, c
455
+Set output channel layout. If not specified, the channel layout is
456
+specified by the number of expressions. If set to @samp{same}, it will
457
+use by default the same input channel layout.
458
+@end table
459
+
460
+Each expression in @var{exprs} can contain the following constants and functions:
461
+
462
+@table @option
463
+@item ch
464
+channel number of the current expression
465
+
466
+@item n
467
+number of the evaluated sample, starting from 0
468
+
469
+@item s
470
+sample rate
471
+
472
+@item t
473
+time of the evaluated sample expressed in seconds
474
+
475
+@item nb_in_channels
476
+@item nb_out_channels
477
+input and output number of channels
478
+
479
+@item val(CH)
480
+the value of input channel with number @var{CH}
481
+@end table
482
+
483
+Note: this filter is slow. For faster processing you should use a
484
+dedicated filter.
485
+
486
+@subsection Examples
487
+
488
+@itemize
489
+@item
490
+Half volume:
491
+@example
492
+aeval=val(ch)/2:c=same
493
+@end example
494
+
495
+@item
496
+Invert phase of the second channel:
497
+@example
498
+eval=val(0)|-val(1)
499
+@end example
500
+@end itemize
501
+
438 502
 @section afade
439 503
 
440 504
 Apply fade-in/out effect to input audio.
... ...
@@ -53,6 +53,7 @@ OBJS-$(CONFIG_AVCODEC)                       += avcodec.o
53 53
 OBJS-$(CONFIG_ACONVERT_FILTER)               += af_aconvert.o
54 54
 OBJS-$(CONFIG_ADELAY_FILTER)                 += af_adelay.o
55 55
 OBJS-$(CONFIG_AECHO_FILTER)                  += af_aecho.o
56
+OBJS-$(CONFIG_AEVAL_FILTER)                  += asrc_aevalsrc.o
56 57
 OBJS-$(CONFIG_AFADE_FILTER)                  += af_afade.o
57 58
 OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
58 59
 OBJS-$(CONFIG_AINTERLEAVE_FILTER)            += f_interleave.o
... ...
@@ -50,6 +50,7 @@ void avfilter_register_all(void)
50 50
 #endif
51 51
     REGISTER_FILTER(ADELAY,         adelay,         af);
52 52
     REGISTER_FILTER(AECHO,          aecho,          af);
53
+    REGISTER_FILTER(AEVAL,          aeval,          af);
53 54
     REGISTER_FILTER(AFADE,          afade,          af);
54 55
     REGISTER_FILTER(AFORMAT,        aformat,        af);
55 56
     REGISTER_FILTER(AINTERLEAVE,    ainterleave,    af);
... ...
@@ -34,14 +34,20 @@
34 34
 #include "internal.h"
35 35
 
36 36
 static const char * const var_names[] = {
37
+    "ch",           ///< the value of the current channel
37 38
     "n",            ///< number of frame
39
+    "nb_in_channels",
40
+    "nb_out_channels",
38 41
     "t",            ///< timestamp expressed in seconds
39 42
     "s",            ///< sample rate
40 43
     NULL
41 44
 };
42 45
 
43 46
 enum var_name {
47
+    VAR_CH,
44 48
     VAR_N,
49
+    VAR_NB_IN_CHANNELS,
50
+    VAR_NB_OUT_CHANNELS,
45 51
     VAR_T,
46 52
     VAR_S,
47 53
     VAR_VARS_NB
... ...
@@ -53,7 +59,9 @@ typedef struct {
53 53
     int sample_rate;
54 54
     int64_t chlayout;
55 55
     char *chlayout_str;
56
-    int nb_channels;
56
+    int nb_channels;            ///< number of output channels
57
+    int nb_in_channels;         ///< number of input channels
58
+    int same_chlayout;          ///< set output as input channel layout
57 59
     int64_t pts;
58 60
     AVExpr **expr;
59 61
     char *exprs;
... ...
@@ -61,8 +69,19 @@ typedef struct {
61 61
     int64_t duration;
62 62
     uint64_t n;
63 63
     double var_values[VAR_VARS_NB];
64
+    double *channel_values;
65
+    int64_t out_channel_layout;
64 66
 } EvalContext;
65 67
 
68
+static double val(void *priv, double ch)
69
+{
70
+    EvalContext *eval = priv;
71
+    return eval->channel_values[FFMIN((int)ch, eval->nb_in_channels-1)];
72
+}
73
+
74
+static double (* const aeval_func1[])(void *, double) = { val, NULL };
75
+static const char * const aeval_func1_names[] = { "val", NULL };
76
+
66 77
 #define OFFSET(x) offsetof(EvalContext, x)
67 78
 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
68 79
 
... ...
@@ -81,17 +100,27 @@ static const AVOption aevalsrc_options[]= {
81 81
 
82 82
 AVFILTER_DEFINE_CLASS(aevalsrc);
83 83
 
84
-static av_cold int init(AVFilterContext *ctx)
84
+static int parse_channel_expressions(AVFilterContext *ctx,
85
+                                     int expected_nb_channels)
85 86
 {
86 87
     EvalContext *eval = ctx->priv;
87 88
     char *args1 = av_strdup(eval->exprs);
88 89
     char *expr, *last_expr, *buf;
89
-    int ret;
90
+    double (* const *func1)(void *, double) = NULL;
91
+    const char * const *func1_names = NULL;
92
+    int i, ret = 0;
93
+
94
+    if (!args1)
95
+        return AVERROR(ENOMEM);
90 96
 
91
-    if (!args1) {
97
+    if (!eval->exprs) {
92 98
         av_log(ctx, AV_LOG_ERROR, "Channels expressions list is empty\n");
93
-        ret = eval->exprs ? AVERROR(ENOMEM) : AVERROR(EINVAL);
94
-        goto end;
99
+        return AVERROR(EINVAL);
100
+    }
101
+
102
+    if (!strcmp(ctx->filter->name, "aeval")) {
103
+        func1 = aeval_func1;
104
+        func1_names = aeval_func1_names;
95 105
     }
96 106
 
97 107
 #define ADD_EXPRESSION(expr_) do {                                      \
... ...
@@ -102,56 +131,79 @@ static av_cold int init(AVFilterContext *ctx)
102 102
         }                                                               \
103 103
         eval->expr[eval->nb_channels-1] = NULL;                         \
104 104
         ret = av_expr_parse(&eval->expr[eval->nb_channels - 1], expr_,  \
105
-                            var_names, NULL, NULL,                      \
105
+                            var_names, func1_names, func1,              \
106 106
                             NULL, NULL, 0, ctx);                        \
107 107
         if (ret < 0)                                                    \
108 108
             goto end;                                                   \
109 109
     } while (0)
110 110
 
111
-    /* parse expressions */
111
+    /* reset expressions */
112
+    for (i = 0; i < eval->nb_channels; i++) {
113
+        av_expr_free(eval->expr[i]);
114
+        eval->expr[i] = NULL;
115
+    }
116
+    av_freep(&eval->expr);
117
+    eval->nb_channels = 0;
118
+
112 119
     buf = args1;
113 120
     while (expr = av_strtok(buf, "|", &buf)) {
114 121
         ADD_EXPRESSION(expr);
115 122
         last_expr = expr;
116 123
     }
117 124
 
118
-    if (eval->chlayout_str) {
119
-        int i, n;
120
-        ret = ff_parse_channel_layout(&eval->chlayout, NULL, eval->chlayout_str, ctx);
121
-        if (ret < 0)
122
-            goto end;
123
-
124
-        n = av_get_channel_layout_nb_channels(eval->chlayout);
125
-        if (n > eval->nb_channels) {
126
-            for (i = eval->nb_channels; i < n; i++)
127
-                ADD_EXPRESSION(last_expr);
128
-        }
125
+    if (expected_nb_channels > eval->nb_channels)
126
+        for (i = eval->nb_channels; i < expected_nb_channels; i++)
127
+            ADD_EXPRESSION(last_expr);
128
+
129
+    if (expected_nb_channels > 0 && eval->nb_channels != expected_nb_channels) {
130
+        av_log(ctx, AV_LOG_ERROR,
131
+               "Mismatch between the specified number of channel expressions '%d' "
132
+               "and the number of expected output channels '%d' for the specified channel layout\n",
133
+               eval->nb_channels, expected_nb_channels);
134
+        ret = AVERROR(EINVAL);
135
+        goto end;
136
+    }
137
+
138
+end:
139
+    av_free(args1);
140
+    return ret;
141
+}
129 142
 
130
-        if (n != eval->nb_channels) {
131
-            av_log(ctx, AV_LOG_ERROR,
132
-                   "Mismatch between the specified number of channels '%d' "
133
-                   "and the number of channels '%d' in the specified channel layout '%s'\n",
134
-                   eval->nb_channels, n, eval->chlayout_str);
135
-            ret = AVERROR(EINVAL);
136
-            goto end;
143
+static av_cold int init(AVFilterContext *ctx)
144
+{
145
+    EvalContext *eval = ctx->priv;
146
+    int ret;
147
+
148
+    if (eval->chlayout_str) {
149
+        if (!strcmp(eval->chlayout_str, "same") && !strcmp(ctx->filter->name, "aeval")) {
150
+            eval->same_chlayout = 1;
151
+        } else {
152
+            ret = ff_parse_channel_layout(&eval->chlayout, NULL, eval->chlayout_str, ctx);
153
+            if (ret < 0)
154
+                return ret;
155
+
156
+            ret = parse_channel_expressions(ctx, av_get_channel_layout_nb_channels(eval->chlayout));
157
+            if (ret < 0)
158
+                return ret;
137 159
         }
138 160
     } else {
139 161
         /* guess channel layout from nb expressions/channels */
162
+        if ((ret = parse_channel_expressions(ctx, -1)) < 0)
163
+            return ret;
164
+
140 165
         eval->chlayout = av_get_default_channel_layout(eval->nb_channels);
141 166
         if (!eval->chlayout && eval->nb_channels <= 0) {
142 167
             av_log(ctx, AV_LOG_ERROR, "Invalid number of channels '%d' provided\n",
143 168
                    eval->nb_channels);
144
-            ret = AVERROR(EINVAL);
145
-            goto end;
169
+            return AVERROR(EINVAL);
146 170
         }
147 171
     }
148 172
 
149
-    if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
150
-        goto end;
173
+    if (eval->sample_rate_str)
174
+        if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
175
+            return ret;
151 176
     eval->n = 0;
152 177
 
153
-end:
154
-    av_free(args1);
155 178
     return ret;
156 179
 }
157 180
 
... ...
@@ -176,6 +228,8 @@ static int config_props(AVFilterLink *outlink)
176 176
     outlink->sample_rate = eval->sample_rate;
177 177
 
178 178
     eval->var_values[VAR_S] = eval->sample_rate;
179
+    eval->var_values[VAR_NB_IN_CHANNELS] = NAN;
180
+    eval->var_values[VAR_NB_OUT_CHANNELS] = outlink->channels;
179 181
 
180 182
     av_get_channel_layout_string(buf, sizeof(buf), 0, eval->chlayout);
181 183
 
... ...
@@ -232,6 +286,7 @@ static int request_frame(AVFilterLink *outlink)
232 232
     return ff_filter_frame(outlink, samplesref);
233 233
 }
234 234
 
235
+#if CONFIG_AEVALSRC_FILTER
235 236
 static const AVFilterPad aevalsrc_outputs[] = {
236 237
     {
237 238
         .name          = "default",
... ...
@@ -253,3 +308,159 @@ AVFilter ff_asrc_aevalsrc = {
253 253
     .outputs       = aevalsrc_outputs,
254 254
     .priv_class    = &aevalsrc_class,
255 255
 };
256
+
257
+#endif /* CONFIG_AEVALSRC_FILTER */
258
+
259
+#define OFFSET(x) offsetof(EvalContext, x)
260
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
261
+
262
+static const AVOption aeval_options[]= {
263
+    { "exprs", "set the '|'-separated list of channels expressions", OFFSET(exprs), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
264
+    { "channel_layout", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
265
+    { "c",              "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
266
+    { NULL }
267
+};
268
+
269
+AVFILTER_DEFINE_CLASS(aeval);
270
+
271
+static int aeval_query_formats(AVFilterContext *ctx)
272
+{
273
+    AVFilterFormats *formats = NULL;
274
+    AVFilterChannelLayouts *layouts;
275
+    AVFilterLink *inlink  = ctx->inputs[0];
276
+    AVFilterLink *outlink  = ctx->outputs[0];
277
+    EvalContext *eval = ctx->priv;
278
+    static const enum AVSampleFormat sample_fmts[] = {
279
+        AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE
280
+    };
281
+
282
+    // inlink supports any channel layout
283
+    layouts = ff_all_channel_counts();
284
+    ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
285
+
286
+    if (eval->same_chlayout) {
287
+        layouts = ff_all_channel_counts();
288
+        if (!layouts)
289
+            return AVERROR(ENOMEM);
290
+            ff_set_common_channel_layouts(ctx, layouts);
291
+    } else {
292
+        // outlink supports only requested output channel layout
293
+        layouts = NULL;
294
+        ff_add_channel_layout(&layouts,
295
+                              eval->out_channel_layout ? eval->out_channel_layout :
296
+                              FF_COUNT2LAYOUT(eval->nb_channels));
297
+        ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
298
+    }
299
+
300
+    formats = ff_make_format_list(sample_fmts);
301
+    if (!formats)
302
+        return AVERROR(ENOMEM);
303
+    ff_set_common_formats(ctx, formats);
304
+
305
+    formats = ff_all_samplerates();
306
+    if (!formats)
307
+        return AVERROR(ENOMEM);
308
+    ff_set_common_samplerates(ctx, formats);
309
+
310
+    return 0;
311
+}
312
+
313
+static int aeval_config_output(AVFilterLink *outlink)
314
+{
315
+    AVFilterContext *ctx = outlink->src;
316
+    EvalContext *eval = ctx->priv;
317
+    AVFilterLink *inlink = ctx->inputs[0];
318
+    int ret;
319
+
320
+    if (eval->same_chlayout) {
321
+        eval->chlayout = inlink->channel_layout;
322
+
323
+        if ((ret = parse_channel_expressions(ctx, inlink->channels)) < 0)
324
+            return ret;
325
+    }
326
+
327
+    eval->n = 0;
328
+    eval->nb_in_channels = eval->var_values[VAR_NB_IN_CHANNELS] = inlink->channels;
329
+    eval->var_values[VAR_NB_OUT_CHANNELS] = outlink->channels;
330
+    eval->var_values[VAR_S] = inlink->sample_rate;
331
+    eval->var_values[VAR_T] = NAN;
332
+
333
+    eval->channel_values = av_realloc_f(eval->channel_values,
334
+                                        inlink->channels, sizeof(*eval->channel_values));
335
+    if (!eval->channel_values)
336
+        return AVERROR(ENOMEM);
337
+
338
+    return 0;
339
+}
340
+
341
+#define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
342
+
343
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
344
+{
345
+    EvalContext *eval     = inlink->dst->priv;
346
+    AVFilterLink *outlink = inlink->dst->outputs[0];
347
+    int nb_samples        = in->nb_samples;
348
+    AVFrame *out;
349
+    double t0;
350
+    int i, j;
351
+
352
+    /* do volume scaling in-place if input buffer is writable */
353
+    out = ff_get_audio_buffer(outlink, nb_samples);
354
+    if (!out)
355
+        return AVERROR(ENOMEM);
356
+    av_frame_copy_props(out, in);
357
+
358
+    t0 = TS2T(in->pts, inlink->time_base);
359
+
360
+    /* evaluate expression for each single sample and for each channel */
361
+    for (i = 0; i < nb_samples; i++, eval->n++) {
362
+        eval->var_values[VAR_N] = eval->n;
363
+        eval->var_values[VAR_T] = t0 + i * (double)1/inlink->sample_rate;
364
+
365
+        for (j = 0; j < inlink->channels; j++)
366
+            eval->channel_values[j] = *((double *) in->extended_data[j] + i);
367
+
368
+        for (j = 0; j < outlink->channels; j++) {
369
+            eval->var_values[VAR_CH] = j;
370
+            *((double *) out->extended_data[j] + i) =
371
+                av_expr_eval(eval->expr[j], eval->var_values, eval);
372
+        }
373
+    }
374
+
375
+    av_frame_free(&in);
376
+    return ff_filter_frame(outlink, out);
377
+}
378
+
379
+#if CONFIG_AEVAL_FILTER
380
+
381
+static const AVFilterPad aeval_inputs[] = {
382
+    {
383
+        .name           = "default",
384
+        .type           = AVMEDIA_TYPE_AUDIO,
385
+        .filter_frame   = filter_frame,
386
+    },
387
+    { NULL }
388
+};
389
+
390
+static const AVFilterPad aeval_outputs[] = {
391
+    {
392
+        .name          = "default",
393
+        .type          = AVMEDIA_TYPE_AUDIO,
394
+        .config_props  = aeval_config_output,
395
+    },
396
+    { NULL }
397
+};
398
+
399
+AVFilter ff_af_aeval = {
400
+    .name          = "aeval",
401
+    .description   = NULL_IF_CONFIG_SMALL("Filter audio signal according to a specified expression."),
402
+    .query_formats = aeval_query_formats,
403
+    .init          = init,
404
+    .uninit        = uninit,
405
+    .priv_size     = sizeof(EvalContext),
406
+    .inputs        = aeval_inputs,
407
+    .outputs       = aeval_outputs,
408
+    .priv_class    = &aeval_class,
409
+};
410
+
411
+#endif /* CONFIG_AEVAL_FILTER */
... ...
@@ -30,8 +30,8 @@
30 30
 #include "libavutil/avutil.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR  3
33
-#define LIBAVFILTER_VERSION_MINOR  91
34
-#define LIBAVFILTER_VERSION_MICRO 101
33
+#define LIBAVFILTER_VERSION_MINOR  92
34
+#define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
                                                LIBAVFILTER_VERSION_MINOR, \