2 * Copyright (c) 2011 Stefano Sabatini
4 * This file is part of FFmpeg.
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.
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.
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
26 #include "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/eval.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/parseutils.h"
36 static const char * const var_names[] = {
37 "ch", ///< the value of the current channel
38 "n", ///< number of frame
41 "t", ///< timestamp expressed in seconds
56 typedef struct EvalContext {
58 char *sample_rate_str;
62 int nb_channels; ///< number of output channels
63 int nb_in_channels; ///< number of input channels
64 int same_chlayout; ///< set output as input channel layout
68 int nb_samples; ///< number of samples per requested frame
71 double var_values[VAR_VARS_NB];
72 double *channel_values;
73 int64_t out_channel_layout;
76 static double val(void *priv, double ch)
78 EvalContext *eval = priv;
79 return eval->channel_values[FFMIN((int)ch, eval->nb_in_channels-1)];
82 static double (* const aeval_func1[])(void *, double) = { val, NULL };
83 static const char * const aeval_func1_names[] = { "val", NULL };
85 #define OFFSET(x) offsetof(EvalContext, x)
86 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
88 static const AVOption aevalsrc_options[]= {
89 { "exprs", "set the '|'-separated list of channels expressions", OFFSET(exprs), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
90 { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
91 { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
92 { "sample_rate", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS },
93 { "s", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS },
94 { "duration", "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
95 { "d", "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
96 { "channel_layout", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
97 { "c", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
101 AVFILTER_DEFINE_CLASS(aevalsrc);
103 static int parse_channel_expressions(AVFilterContext *ctx,
104 int expected_nb_channels)
106 EvalContext *eval = ctx->priv;
107 char *args1 = av_strdup(eval->exprs);
108 char *expr, *last_expr = NULL, *buf;
109 double (* const *func1)(void *, double) = NULL;
110 const char * const *func1_names = NULL;
114 return AVERROR(ENOMEM);
117 av_log(ctx, AV_LOG_ERROR, "Channels expressions list is empty\n");
118 return AVERROR(EINVAL);
121 if (!strcmp(ctx->filter->name, "aeval")) {
123 func1_names = aeval_func1_names;
126 #define ADD_EXPRESSION(expr_) do { \
127 if (!av_dynarray2_add((void **)&eval->expr, &eval->nb_channels, \
128 sizeof(*eval->expr), NULL)) { \
129 ret = AVERROR(ENOMEM); \
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); \
140 /* reset expressions */
141 for (i = 0; i < eval->nb_channels; i++) {
142 av_expr_free(eval->expr[i]);
143 eval->expr[i] = NULL;
145 av_freep(&eval->expr);
146 eval->nb_channels = 0;
149 while (expr = av_strtok(buf, "|", &buf)) {
150 ADD_EXPRESSION(expr);
154 if (expected_nb_channels > eval->nb_channels)
155 for (i = eval->nb_channels; i < expected_nb_channels; i++)
156 ADD_EXPRESSION(last_expr);
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);
172 static av_cold int init(AVFilterContext *ctx)
174 EvalContext *eval = ctx->priv;
177 if (eval->chlayout_str) {
178 if (!strcmp(eval->chlayout_str, "same") && !strcmp(ctx->filter->name, "aeval")) {
179 eval->same_chlayout = 1;
181 ret = ff_parse_channel_layout(&eval->chlayout, NULL, eval->chlayout_str, ctx);
185 ret = parse_channel_expressions(ctx, av_get_channel_layout_nb_channels(eval->chlayout));
190 /* guess channel layout from nb expressions/channels */
191 if ((ret = parse_channel_expressions(ctx, -1)) < 0)
194 eval->chlayout = av_get_default_channel_layout(eval->nb_channels);
195 if (!eval->chlayout && eval->nb_channels <= 0) {
196 av_log(ctx, AV_LOG_ERROR, "Invalid number of channels '%d' provided\n",
198 return AVERROR(EINVAL);
202 if (eval->sample_rate_str)
203 if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
210 static av_cold void uninit(AVFilterContext *ctx)
212 EvalContext *eval = ctx->priv;
215 for (i = 0; i < eval->nb_channels; i++) {
216 av_expr_free(eval->expr[i]);
217 eval->expr[i] = NULL;
219 av_freep(&eval->expr);
220 av_freep(&eval->channel_values);
223 static int config_props(AVFilterLink *outlink)
225 EvalContext *eval = outlink->src->priv;
228 outlink->time_base = (AVRational){1, eval->sample_rate};
229 outlink->sample_rate = eval->sample_rate;
231 eval->var_values[VAR_S] = eval->sample_rate;
232 eval->var_values[VAR_NB_IN_CHANNELS] = NAN;
233 eval->var_values[VAR_NB_OUT_CHANNELS] = outlink->channels;
235 av_get_channel_layout_string(buf, sizeof(buf), 0, eval->chlayout);
237 av_log(outlink->src, AV_LOG_VERBOSE,
238 "sample_rate:%d chlayout:%s duration:%"PRId64"\n",
239 eval->sample_rate, buf, eval->duration);
244 static int query_formats(AVFilterContext *ctx)
246 EvalContext *eval = ctx->priv;
247 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE };
248 int64_t chlayouts[] = { eval->chlayout ? eval->chlayout : FF_COUNT2LAYOUT(eval->nb_channels) , -1 };
249 int sample_rates[] = { eval->sample_rate, -1 };
250 AVFilterFormats *formats;
251 AVFilterChannelLayouts *layouts;
254 formats = ff_make_format_list(sample_fmts);
256 return AVERROR(ENOMEM);
257 ret = ff_set_common_formats (ctx, formats);
261 layouts = ff_make_format64_list(chlayouts);
263 return AVERROR(ENOMEM);
264 ret = ff_set_common_channel_layouts(ctx, layouts);
268 formats = ff_make_format_list(sample_rates);
270 return AVERROR(ENOMEM);
271 return ff_set_common_samplerates(ctx, formats);
274 static int request_frame(AVFilterLink *outlink)
276 EvalContext *eval = outlink->src->priv;
279 int64_t t = av_rescale(eval->n, AV_TIME_BASE, eval->sample_rate);
282 if (eval->duration >= 0 && t >= eval->duration)
285 if (eval->duration >= 0) {
286 nb_samples = FFMIN(eval->nb_samples, av_rescale(eval->duration, eval->sample_rate, AV_TIME_BASE) - eval->pts);
290 nb_samples = eval->nb_samples;
292 samplesref = ff_get_audio_buffer(outlink, nb_samples);
294 return AVERROR(ENOMEM);
296 /* evaluate expression for each single sample and for each channel */
297 for (i = 0; i < nb_samples; i++, eval->n++) {
298 eval->var_values[VAR_N] = eval->n;
299 eval->var_values[VAR_T] = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
301 for (j = 0; j < eval->nb_channels; j++) {
302 *((double *) samplesref->extended_data[j] + i) =
303 av_expr_eval(eval->expr[j], eval->var_values, NULL);
307 samplesref->pts = eval->pts;
308 samplesref->sample_rate = eval->sample_rate;
309 eval->pts += nb_samples;
311 return ff_filter_frame(outlink, samplesref);
314 #if CONFIG_AEVALSRC_FILTER
315 static const AVFilterPad aevalsrc_outputs[] = {
318 .type = AVMEDIA_TYPE_AUDIO,
319 .config_props = config_props,
320 .request_frame = request_frame,
325 const AVFilter ff_asrc_aevalsrc = {
327 .description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
328 .query_formats = query_formats,
331 .priv_size = sizeof(EvalContext),
333 .outputs = aevalsrc_outputs,
334 .priv_class = &aevalsrc_class,
337 #endif /* CONFIG_AEVALSRC_FILTER */
339 #define OFFSET(x) offsetof(EvalContext, x)
340 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
342 static const AVOption aeval_options[]= {
343 { "exprs", "set the '|'-separated list of channels expressions", OFFSET(exprs), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
344 { "channel_layout", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
345 { "c", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
349 AVFILTER_DEFINE_CLASS(aeval);
351 static int aeval_query_formats(AVFilterContext *ctx)
353 AVFilterFormats *formats = NULL;
354 AVFilterChannelLayouts *layouts;
355 AVFilterLink *inlink = ctx->inputs[0];
356 AVFilterLink *outlink = ctx->outputs[0];
357 EvalContext *eval = ctx->priv;
358 static const enum AVSampleFormat sample_fmts[] = {
359 AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE
363 // inlink supports any channel layout
364 layouts = ff_all_channel_counts();
365 if ((ret = ff_channel_layouts_ref(layouts, &inlink->outcfg.channel_layouts)) < 0)
368 if (eval->same_chlayout) {
369 layouts = ff_all_channel_counts();
370 if ((ret = ff_set_common_channel_layouts(ctx, layouts)) < 0)
373 // outlink supports only requested output channel layout
375 if ((ret = ff_add_channel_layout(&layouts,
376 eval->out_channel_layout ? eval->out_channel_layout :
377 FF_COUNT2LAYOUT(eval->nb_channels))) < 0)
379 if ((ret = ff_channel_layouts_ref(layouts, &outlink->incfg.channel_layouts)) < 0)
383 formats = ff_make_format_list(sample_fmts);
384 if ((ret = ff_set_common_formats(ctx, formats)) < 0)
387 formats = ff_all_samplerates();
388 return ff_set_common_samplerates(ctx, formats);
391 static int aeval_config_output(AVFilterLink *outlink)
393 AVFilterContext *ctx = outlink->src;
394 EvalContext *eval = ctx->priv;
395 AVFilterLink *inlink = ctx->inputs[0];
398 if (eval->same_chlayout) {
399 eval->chlayout = inlink->channel_layout;
401 if ((ret = parse_channel_expressions(ctx, inlink->channels)) < 0)
406 eval->nb_in_channels = eval->var_values[VAR_NB_IN_CHANNELS] = inlink->channels;
407 eval->var_values[VAR_NB_OUT_CHANNELS] = outlink->channels;
408 eval->var_values[VAR_S] = inlink->sample_rate;
409 eval->var_values[VAR_T] = NAN;
411 eval->channel_values = av_realloc_f(eval->channel_values,
412 inlink->channels, sizeof(*eval->channel_values));
413 if (!eval->channel_values)
414 return AVERROR(ENOMEM);
419 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
421 EvalContext *eval = inlink->dst->priv;
422 AVFilterLink *outlink = inlink->dst->outputs[0];
423 int nb_samples = in->nb_samples;
428 out = ff_get_audio_buffer(outlink, nb_samples);
431 return AVERROR(ENOMEM);
433 av_frame_copy_props(out, in);
435 t0 = TS2T(in->pts, inlink->time_base);
437 /* evaluate expression for each single sample and for each channel */
438 for (i = 0; i < nb_samples; i++, eval->n++) {
439 eval->var_values[VAR_N] = eval->n;
440 eval->var_values[VAR_T] = t0 + i * (double)1/inlink->sample_rate;
442 for (j = 0; j < inlink->channels; j++)
443 eval->channel_values[j] = *((double *) in->extended_data[j] + i);
445 for (j = 0; j < outlink->channels; j++) {
446 eval->var_values[VAR_CH] = j;
447 *((double *) out->extended_data[j] + i) =
448 av_expr_eval(eval->expr[j], eval->var_values, eval);
453 return ff_filter_frame(outlink, out);
456 #if CONFIG_AEVAL_FILTER
458 static const AVFilterPad aeval_inputs[] = {
461 .type = AVMEDIA_TYPE_AUDIO,
462 .filter_frame = filter_frame,
467 static const AVFilterPad aeval_outputs[] = {
470 .type = AVMEDIA_TYPE_AUDIO,
471 .config_props = aeval_config_output,
476 const AVFilter ff_af_aeval = {
478 .description = NULL_IF_CONFIG_SMALL("Filter audio signal according to a specified expression."),
479 .query_formats = aeval_query_formats,
482 .priv_size = sizeof(EvalContext),
483 .inputs = aeval_inputs,
484 .outputs = aeval_outputs,
485 .priv_class = &aeval_class,
486 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
489 #endif /* CONFIG_AEVAL_FILTER */