[FFmpeg-devel] [PATCH] lavfi: add audio eval signal source
Stefano Sabatini
stefasab at gmail.com
Thu Oct 13 02:08:57 CEST 2011
From: Stefano Sabatini <stefano.sabatini-lala at poste.it>
---
doc/filters.texi | 70 +++++++++++++++
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/asrc_aevalsrc.c | 197 +++++++++++++++++++++++++++++++++++++++++++
4 files changed, 269 insertions(+), 0 deletions(-)
create mode 100644 libavfilter/asrc_aevalsrc.c
diff --git a/doc/filters.texi b/doc/filters.texi
index 54c1417..339489f 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -275,6 +275,76 @@ equivalent to:
abuffer=44100:1:3:1
@end example
+ at section aevalsrc
+
+Generate an audio signal generated by an expression.
+
+This source accepts in input an expression, which is evaluated and
+used for generating a mono audio signal.
+
+It accepts the syntax: @var{expr}[:@var{options}] where @var{expr} is
+the expression to evaluate, and @var{options} is an optional sequence
+of @var{key}=@var{value} pairs, separated by ":".
+
+The description of the accepted options follows.
+
+ at table @option
+
+ at item nb_samples, n
+Set the number of samples per requested frames.
+
+ at item sample_rate, s
+Specify the sample rate, and defaults to 44100.
+ at end table
+
+The expression in @var{expr} can contain the following constants:
+
+ at table @option
+ at item E, PI, PHI
+the corresponding mathematical approximated values for e
+(euler number), pi (greek PI), PHI (golden ratio)
+
+ at item n
+the number of sample, starting from 0
+
+ at item t
+time of the sample expressed in second, starting from 0
+
+ at item s
+sample rate
+
+ at end table
+
+ at subsection Examples
+
+ at itemize
+
+ at item
+Generate silence:
+ at example
+aevalsrc=0
+ at end example
+
+ at item
+Generate a sin signal with frequence 4400 Hz:
+ at example
+aevalsrc="sin(4400*t)"
+ at end example
+
+ at item
+Generate white noise:
+ at example
+aevalsrc="-2+random(0)"
+ at end example
+
+ at item
+Generate amplitude modulated signal:
+ at example
+aevalsrc="sin(10*t)*sin(8800*t)"
+ at end example
+
+ at end itemize
+
@section amovie
Read an audio stream from a movie container.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 7086753..08a69e4 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -30,6 +30,7 @@ OBJS-$(CONFIG_ARESAMPLE_FILTER) += af_aresample.o
OBJS-$(CONFIG_ASHOWINFO_FILTER) += af_ashowinfo.o
OBJS-$(CONFIG_ABUFFER_FILTER) += asrc_abuffer.o
+OBJS-$(CONFIG_AEVALSRC_FILTER) += asrc_aevalsrc.o
OBJS-$(CONFIG_AMOVIE_FILTER) += src_movie.o
OBJS-$(CONFIG_ANULLSRC_FILTER) += asrc_anullsrc.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 2bb42a1..3c77adb 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -41,6 +41,7 @@ void avfilter_register_all(void)
REGISTER_FILTER (ASHOWINFO, ashowinfo, af);
REGISTER_FILTER (ABUFFER, abuffer, asrc);
+ REGISTER_FILTER (AEVALSRC, aevalsrc, asrc);
REGISTER_FILTER (AMOVIE, amovie, asrc);
REGISTER_FILTER (ANULLSRC, anullsrc, asrc);
diff --git a/libavfilter/asrc_aevalsrc.c b/libavfilter/asrc_aevalsrc.c
new file mode 100644
index 0000000..189d60d
--- /dev/null
+++ b/libavfilter/asrc_aevalsrc.c
@@ -0,0 +1,197 @@
+/*
+ * Copyright (c) 2011 Stefano Sabatini
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * eval audio source
+ */
+
+#include "libavutil/audioconvert.h"
+#include "libavutil/avstring.h"
+#include "libavutil/eval.h"
+#include "libavutil/opt.h"
+#include "avfilter.h"
+#include "internal.h"
+
+static const char *var_names[] = {
+ "E",
+ "PHI",
+ "PI",
+ "n", ///< number of frame
+ "t", ///< timestamp expressed in seconds
+ "s", ///< sample rate, same as 1/tb
+ NULL
+};
+
+enum var_name {
+ VAR_E,
+ VAR_PHI,
+ VAR_PI,
+ VAR_N,
+ VAR_T,
+ VAR_S,
+ VAR_VARS_NB
+};
+
+typedef struct {
+ const AVClass *class;
+ char *sample_rate_str;
+ int sample_rate;
+ int64_t pts;
+ AVExpr *expr;
+ char *expr_str;
+ int nb_samples; ///< number of samples per requested frame
+ uint64_t n;
+ double var_values[VAR_VARS_NB];
+} EvalContext;
+
+#define OFFSET(x) offsetof(EvalContext, x)
+
+static const AVOption eval_options[]= {
+ { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), FF_OPT_TYPE_INT, {.dbl = 1024}, 0, INT_MAX },
+ { "n", "set the number of samples per requested frame", OFFSET(nb_samples), FF_OPT_TYPE_INT, {.dbl = 1024}, 0, INT_MAX },
+ { "sample_rate", "set the sample rate", OFFSET(sample_rate_str), FF_OPT_TYPE_STRING, {.str = "44100"}, 0, INT_MAX },
+ { "s", "set the sample rate", OFFSET(sample_rate_str), FF_OPT_TYPE_STRING, {.str = "44100"}, 0, INT_MAX },
+{NULL},
+};
+
+static const char *eval_get_name(void *ctx)
+{
+ return "aevalsrc";
+}
+
+static const AVClass eval_class = {
+ "AEvalSrcContext",
+ eval_get_name,
+ eval_options
+};
+
+static int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+ EvalContext *eval = ctx->priv;
+ int ret;
+
+ eval->class = &eval_class;
+ av_opt_set_defaults(eval);
+
+ if (args)
+ eval->expr_str = av_get_token(&args, ":");
+ if (!eval->expr_str || !*eval->expr_str) {
+ av_log(ctx, AV_LOG_ERROR, "No expression provided!\n");
+ return AVERROR(EINVAL);
+ }
+
+ if ((ret = av_expr_parse(&eval->expr, eval->expr_str, var_names,
+ NULL, NULL, NULL, NULL, 0, ctx)) < 0)
+ return ret;
+
+ if (*args++ == ':' && (ret = av_set_options_string(eval, args, "=", ":")) < 0) {
+ av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
+ return ret;
+ }
+
+ if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
+ return ret;
+ eval->n = 0;
+
+ return 0;
+}
+
+static void uninit(AVFilterContext *ctx)
+{
+ EvalContext *eval = ctx->priv;
+
+ av_expr_free(eval->expr); eval->expr = NULL;
+ av_freep(&eval->expr_str);
+ av_freep(&eval->sample_rate_str);
+}
+
+static int config_props(AVFilterLink *outlink)
+{
+ EvalContext *eval = outlink->src->priv;
+
+ outlink->time_base = (AVRational){1, eval->sample_rate};
+ outlink->sample_rate = eval->sample_rate;
+
+ eval->var_values[VAR_S] = eval->sample_rate;
+
+ av_log(outlink->src, AV_LOG_INFO,
+ "sample_rate:%d expr:'%s'\n", eval->sample_rate, eval->expr_str);
+
+ return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+ enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_NONE };
+ int64_t chlayouts[] = { AV_CH_LAYOUT_MONO, -1 };
+ int packing_fmts[] = { AVFILTER_PACKED, -1 };
+
+ avfilter_set_common_sample_formats (ctx, avfilter_make_format_list(sample_fmts));
+ avfilter_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
+ avfilter_set_common_packing_formats(ctx, avfilter_make_format_list(packing_fmts));
+
+ return 0;
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+ EvalContext *eval = outlink->src->priv;
+ AVFilterBufferRef *samplesref;
+ int i;
+
+ samplesref = avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, eval->nb_samples);
+ eval->var_values[VAR_N] = eval->n;
+
+ /* evaluate expression for each single sample */
+ for (i = 0; i < eval->nb_samples; i++) {
+ eval->var_values[VAR_T] = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
+ *((double *) samplesref->data[0] + i) =
+ av_expr_eval(eval->expr, eval->var_values, NULL);
+ eval->var_values[VAR_N] = eval->n++;
+ }
+
+ samplesref->pts = eval->pts;
+ samplesref->pos = -1;
+ samplesref->audio->sample_rate = eval->sample_rate;
+ eval->pts += eval->nb_samples;
+
+ avfilter_filter_samples(outlink, samplesref);
+
+ return 0;
+}
+
+AVFilter avfilter_asrc_aevalsrc = {
+ .name = "aevalsrc",
+ .description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
+
+ .query_formats = query_formats,
+ .init = init,
+ .uninit = uninit,
+ .priv_size = sizeof(EvalContext),
+
+ .inputs = (AVFilterPad[]) {{ .name = NULL}},
+
+ .outputs = (AVFilterPad[]) {{ .name = "default",
+ .type = AVMEDIA_TYPE_AUDIO,
+ .config_props = config_props,
+ .request_frame = request_frame, },
+ { .name = NULL}},
+};
--
1.7.4.1
More information about the ffmpeg-devel
mailing list