1 /*
2 * Copyright (c) 2011 Stefano Sabatini
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * eval audio source
24 */
25
26 #include "config_components.h"
27
28 #include "libavutil/avstring.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/eval.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/parseutils.h"
33 #include "avfilter.h"
34 #include "audio.h"
35 #include "filters.h"
36 #include "internal.h"
37
38 static const char * const var_names[] = {
39 "ch", ///< the value of the current channel
40 "n", ///< number of frame
41 "nb_in_channels",
42 "nb_out_channels",
43 "t", ///< timestamp expressed in seconds
44 "s", ///< sample rate
45 NULL
46 };
47
48 enum var_name {
49 VAR_CH,
50 VAR_N,
51 VAR_NB_IN_CHANNELS,
52 VAR_NB_OUT_CHANNELS,
53 VAR_T,
54 VAR_S,
55 VAR_VARS_NB
56 };
57
58 typedef struct EvalContext {
59 const AVClass *class;
60 char *sample_rate_str;
61 int sample_rate;
62 AVChannelLayout chlayout;
63 char *chlayout_str;
64 int nb_channels; ///< number of output channels
65 int nb_in_channels; ///< number of input channels
66 int same_chlayout; ///< set output as input channel layout
67 int64_t pts;
68 AVExpr **expr;
69 char *exprs;
70 int nb_samples; ///< number of samples per requested frame
71 int64_t duration;
72 uint64_t n;
73 double var_values[VAR_VARS_NB];
74 double *channel_values;
75 } EvalContext;
76
val(void * priv,double ch)77 static double val(void *priv, double ch)
78 {
79 EvalContext *eval = priv;
80 return eval->channel_values[FFMIN((int)ch, eval->nb_in_channels-1)];
81 }
82
83 static double (* const aeval_func1[])(void *, double) = { val, NULL };
84 static const char * const aeval_func1_names[] = { "val", NULL };
85
86 #define OFFSET(x) offsetof(EvalContext, x)
87 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
88
89 static const AVOption aevalsrc_options[]= {
90 { "exprs", "set the '|'-separated list of channels expressions", OFFSET(exprs), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
91 { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
92 { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
93 { "sample_rate", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS },
94 { "s", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS },
95 { "duration", "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
96 { "d", "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
97 { "channel_layout", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
98 { "c", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
99 { NULL }
100 };
101
102 AVFILTER_DEFINE_CLASS(aevalsrc);
103
parse_channel_expressions(AVFilterContext * ctx,int expected_nb_channels)104 static int parse_channel_expressions(AVFilterContext *ctx,
105 int expected_nb_channels)
106 {
107 EvalContext *eval = ctx->priv;
108 char *args1 = av_strdup(eval->exprs);
109 char *expr, *last_expr = NULL, *buf;
110 double (* const *func1)(void *, double) = NULL;
111 const char * const *func1_names = NULL;
112 int i, ret = 0;
113
114 if (!args1)
115 return AVERROR(ENOMEM);
116
117 if (!eval->exprs) {
118 av_log(ctx, AV_LOG_ERROR, "Channels expressions list is empty\n");
119 return AVERROR(EINVAL);
120 }
121
122 if (!strcmp(ctx->filter->name, "aeval")) {
123 func1 = aeval_func1;
124 func1_names = aeval_func1_names;
125 }
126
127 #define ADD_EXPRESSION(expr_) do { \
128 ret = av_dynarray_add_nofree(&eval->expr, \
129 &eval->nb_channels, NULL); \
130 if (ret < 0) \
131 goto end; \
132 eval->expr[eval->nb_channels-1] = NULL; \
133 ret = av_expr_parse(&eval->expr[eval->nb_channels - 1], expr_, \
134 var_names, func1_names, func1, \
135 NULL, NULL, 0, ctx); \
136 if (ret < 0) \
137 goto end; \
138 } while (0)
139
140 /* reset expressions */
141 for (i = 0; i < eval->nb_channels; i++) {
142 av_expr_free(eval->expr[i]);
143 eval->expr[i] = NULL;
144 }
145 av_freep(&eval->expr);
146 eval->nb_channels = 0;
147
148 buf = args1;
149 while (expr = av_strtok(buf, "|", &buf)) {
150 ADD_EXPRESSION(expr);
151 last_expr = expr;
152 }
153
154 if (expected_nb_channels > eval->nb_channels)
155 for (i = eval->nb_channels; i < expected_nb_channels; i++)
156 ADD_EXPRESSION(last_expr);
157
158 if (expected_nb_channels > 0 && eval->nb_channels != expected_nb_channels) {
159 av_log(ctx, AV_LOG_ERROR,
160 "Mismatch between the specified number of channel expressions '%d' "
161 "and the number of expected output channels '%d' for the specified channel layout\n",
162 eval->nb_channels, expected_nb_channels);
163 ret = AVERROR(EINVAL);
164 goto end;
165 }
166
167 end:
168 av_free(args1);
169 return ret;
170 }
171
init(AVFilterContext * ctx)172 static av_cold int init(AVFilterContext *ctx)
173 {
174 EvalContext *eval = ctx->priv;
175 int ret = 0;
176
177 if (eval->chlayout_str) {
178 if (!strcmp(eval->chlayout_str, "same") && !strcmp(ctx->filter->name, "aeval")) {
179 eval->same_chlayout = 1;
180 } else {
181 ret = ff_parse_channel_layout(&eval->chlayout, NULL, eval->chlayout_str, ctx);
182 if (ret < 0)
183 return ret;
184
185 ret = parse_channel_expressions(ctx, eval->chlayout.nb_channels);
186 if (ret < 0)
187 return ret;
188 }
189 } else {
190 /* guess channel layout from nb expressions/channels */
191 if ((ret = parse_channel_expressions(ctx, -1)) < 0)
192 return ret;
193
194 av_channel_layout_default(&eval->chlayout, eval->nb_channels);
195 if (eval->nb_channels <= 0) {
196 av_log(ctx, AV_LOG_ERROR, "Invalid number of channels '%d' provided\n",
197 eval->nb_channels);
198 return AVERROR(EINVAL);
199 }
200 }
201
202 if (eval->sample_rate_str)
203 if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
204 return ret;
205 eval->n = 0;
206
207 return ret;
208 }
209
uninit(AVFilterContext * ctx)210 static av_cold void uninit(AVFilterContext *ctx)
211 {
212 EvalContext *eval = ctx->priv;
213 int i;
214
215 for (i = 0; i < eval->nb_channels; i++) {
216 av_expr_free(eval->expr[i]);
217 eval->expr[i] = NULL;
218 }
219 av_freep(&eval->expr);
220 av_freep(&eval->channel_values);
221 av_channel_layout_uninit(&eval->chlayout);
222 }
223
config_props(AVFilterLink * outlink)224 static int config_props(AVFilterLink *outlink)
225 {
226 EvalContext *eval = outlink->src->priv;
227 char buf[128];
228
229 outlink->time_base = (AVRational){1, eval->sample_rate};
230 outlink->sample_rate = eval->sample_rate;
231
232 eval->var_values[VAR_S] = eval->sample_rate;
233 eval->var_values[VAR_NB_IN_CHANNELS] = NAN;
234 eval->var_values[VAR_NB_OUT_CHANNELS] = outlink->ch_layout.nb_channels;
235
236 av_channel_layout_describe(&eval->chlayout, buf, sizeof(buf));
237
238 av_log(outlink->src, AV_LOG_VERBOSE,
239 "sample_rate:%d chlayout:%s duration:%"PRId64"\n",
240 eval->sample_rate, buf, eval->duration);
241
242 return 0;
243 }
244
query_formats(AVFilterContext * ctx)245 static int query_formats(AVFilterContext *ctx)
246 {
247 EvalContext *eval = ctx->priv;
248 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE };
249 AVChannelLayout chlayouts[] = { eval->chlayout.nb_channels ? eval->chlayout : FF_COUNT2LAYOUT(eval->nb_channels), { 0 } };
250 int sample_rates[] = { eval->sample_rate, -1 };
251 int ret;
252
253 ret = ff_set_common_formats_from_list(ctx, sample_fmts);
254 if (ret < 0)
255 return ret;
256
257 ret = ff_set_common_channel_layouts_from_list(ctx, chlayouts);
258 if (ret < 0)
259 return ret;
260
261 return ff_set_common_samplerates_from_list(ctx, sample_rates);
262 }
263
activate(AVFilterContext * ctx)264 static int activate(AVFilterContext *ctx)
265 {
266 AVFilterLink *outlink = ctx->outputs[0];
267 EvalContext *eval = outlink->src->priv;
268 AVFrame *samplesref;
269 int i, j;
270 int64_t t = av_rescale(eval->n, AV_TIME_BASE, eval->sample_rate);
271 int nb_samples;
272
273 if (!ff_outlink_frame_wanted(outlink))
274 return FFERROR_NOT_READY;
275
276 if (eval->duration >= 0 && t >= eval->duration) {
277 ff_outlink_set_status(outlink, AVERROR_EOF, eval->pts);
278 return 0;
279 }
280
281 if (eval->duration >= 0) {
282 nb_samples = FFMIN(eval->nb_samples, av_rescale(eval->duration, eval->sample_rate, AV_TIME_BASE) - eval->pts);
283 if (!nb_samples) {
284 ff_outlink_set_status(outlink, AVERROR_EOF, eval->pts);
285 return 0;
286 }
287 } else {
288 nb_samples = eval->nb_samples;
289 }
290 samplesref = ff_get_audio_buffer(outlink, nb_samples);
291 if (!samplesref)
292 return AVERROR(ENOMEM);
293
294 /* evaluate expression for each single sample and for each channel */
295 for (i = 0; i < nb_samples; i++, eval->n++) {
296 eval->var_values[VAR_N] = eval->n;
297 eval->var_values[VAR_T] = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
298
299 for (j = 0; j < eval->nb_channels; j++) {
300 *((double *) samplesref->extended_data[j] + i) =
301 av_expr_eval(eval->expr[j], eval->var_values, NULL);
302 }
303 }
304
305 samplesref->pts = eval->pts;
306 samplesref->sample_rate = eval->sample_rate;
307 eval->pts += nb_samples;
308
309 return ff_filter_frame(outlink, samplesref);
310 }
311
312 #if CONFIG_AEVALSRC_FILTER
313 static const AVFilterPad aevalsrc_outputs[] = {
314 {
315 .name = "default",
316 .type = AVMEDIA_TYPE_AUDIO,
317 .config_props = config_props,
318 },
319 };
320
321 const AVFilter ff_asrc_aevalsrc = {
322 .name = "aevalsrc",
323 .description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
324 .init = init,
325 .uninit = uninit,
326 .activate = activate,
327 .priv_size = sizeof(EvalContext),
328 .inputs = NULL,
329 FILTER_OUTPUTS(aevalsrc_outputs),
330 FILTER_QUERY_FUNC(query_formats),
331 .priv_class = &aevalsrc_class,
332 };
333
334 #endif /* CONFIG_AEVALSRC_FILTER */
335
336 #define OFFSET(x) offsetof(EvalContext, x)
337 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
338
339 static const AVOption aeval_options[]= {
340 { "exprs", "set the '|'-separated list of channels expressions", OFFSET(exprs), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
341 { "channel_layout", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
342 { "c", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
343 { NULL }
344 };
345
346 AVFILTER_DEFINE_CLASS(aeval);
347
aeval_query_formats(AVFilterContext * ctx)348 static int aeval_query_formats(AVFilterContext *ctx)
349 {
350 AVFilterChannelLayouts *layouts;
351 AVFilterLink *inlink = ctx->inputs[0];
352 AVFilterLink *outlink = ctx->outputs[0];
353 EvalContext *eval = ctx->priv;
354 static const enum AVSampleFormat sample_fmts[] = {
355 AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE
356 };
357 int ret;
358
359 // inlink supports any channel layout
360 layouts = ff_all_channel_counts();
361 if ((ret = ff_channel_layouts_ref(layouts, &inlink->outcfg.channel_layouts)) < 0)
362 return ret;
363
364 if (eval->same_chlayout) {
365 if ((ret = ff_set_common_all_channel_counts(ctx)) < 0)
366 return ret;
367 } else {
368 // outlink supports only requested output channel layout
369 layouts = NULL;
370 if ((ret = ff_add_channel_layout(&layouts, &FF_COUNT2LAYOUT(eval->nb_channels))) < 0)
371 return ret;
372 if ((ret = ff_channel_layouts_ref(layouts, &outlink->incfg.channel_layouts)) < 0)
373 return ret;
374 }
375
376 if ((ret = ff_set_common_formats_from_list(ctx, sample_fmts)) < 0)
377 return ret;
378
379 return ff_set_common_all_samplerates(ctx);
380 }
381
aeval_config_output(AVFilterLink * outlink)382 static int aeval_config_output(AVFilterLink *outlink)
383 {
384 AVFilterContext *ctx = outlink->src;
385 EvalContext *eval = ctx->priv;
386 AVFilterLink *inlink = ctx->inputs[0];
387 int ret;
388
389 if (eval->same_chlayout) {
390 if ((ret = av_channel_layout_copy(&eval->chlayout, &inlink->ch_layout)) < 0)
391 return ret;
392
393 if ((ret = parse_channel_expressions(ctx, inlink->ch_layout.nb_channels)) < 0)
394 return ret;
395 }
396
397 eval->n = 0;
398 eval->nb_in_channels = eval->var_values[VAR_NB_IN_CHANNELS] = inlink->ch_layout.nb_channels;
399 eval->var_values[VAR_NB_OUT_CHANNELS] = outlink->ch_layout.nb_channels;
400 eval->var_values[VAR_S] = inlink->sample_rate;
401 eval->var_values[VAR_T] = NAN;
402
403 eval->channel_values = av_realloc_f(eval->channel_values,
404 inlink->ch_layout.nb_channels, sizeof(*eval->channel_values));
405 if (!eval->channel_values)
406 return AVERROR(ENOMEM);
407
408 return 0;
409 }
410
filter_frame(AVFilterLink * inlink,AVFrame * in)411 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
412 {
413 EvalContext *eval = inlink->dst->priv;
414 AVFilterLink *outlink = inlink->dst->outputs[0];
415 int nb_samples = in->nb_samples;
416 AVFrame *out;
417 double t0;
418 int i, j;
419
420 out = ff_get_audio_buffer(outlink, nb_samples);
421 if (!out) {
422 av_frame_free(&in);
423 return AVERROR(ENOMEM);
424 }
425 av_frame_copy_props(out, in);
426
427 t0 = TS2T(in->pts, inlink->time_base);
428
429 /* evaluate expression for each single sample and for each channel */
430 for (i = 0; i < nb_samples; i++, eval->n++) {
431 eval->var_values[VAR_N] = eval->n;
432 eval->var_values[VAR_T] = t0 + i * (double)1/inlink->sample_rate;
433
434 for (j = 0; j < inlink->ch_layout.nb_channels; j++)
435 eval->channel_values[j] = *((double *) in->extended_data[j] + i);
436
437 for (j = 0; j < outlink->ch_layout.nb_channels; j++) {
438 eval->var_values[VAR_CH] = j;
439 *((double *) out->extended_data[j] + i) =
440 av_expr_eval(eval->expr[j], eval->var_values, eval);
441 }
442 }
443
444 av_frame_free(&in);
445 return ff_filter_frame(outlink, out);
446 }
447
448 #if CONFIG_AEVAL_FILTER
449
450 static const AVFilterPad aeval_inputs[] = {
451 {
452 .name = "default",
453 .type = AVMEDIA_TYPE_AUDIO,
454 .filter_frame = filter_frame,
455 },
456 };
457
458 static const AVFilterPad aeval_outputs[] = {
459 {
460 .name = "default",
461 .type = AVMEDIA_TYPE_AUDIO,
462 .config_props = aeval_config_output,
463 },
464 };
465
466 const AVFilter ff_af_aeval = {
467 .name = "aeval",
468 .description = NULL_IF_CONFIG_SMALL("Filter audio signal according to a specified expression."),
469 .init = init,
470 .uninit = uninit,
471 .priv_size = sizeof(EvalContext),
472 FILTER_INPUTS(aeval_inputs),
473 FILTER_OUTPUTS(aeval_outputs),
474 FILTER_QUERY_FUNC(aeval_query_formats),
475 .priv_class = &aeval_class,
476 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
477 };
478
479 #endif /* CONFIG_AEVAL_FILTER */
480