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 "n", ///< number of frame
38 "t", ///< timestamp expressed in seconds
52 char *sample_rate_str;
60 int nb_samples; ///< number of samples per requested frame
63 double var_values[VAR_VARS_NB];
66 #define OFFSET(x) offsetof(EvalContext, x)
67 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
69 static const AVOption aevalsrc_options[]= {
70 { "exprs", "set the '|'-separated list of channels expressions", OFFSET(exprs), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
71 { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
72 { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
73 { "sample_rate", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, CHAR_MIN, CHAR_MAX, FLAGS },
74 { "s", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, CHAR_MIN, CHAR_MAX, FLAGS },
75 { "duration", "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
76 { "d", "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
77 { "channel_layout", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
78 { "c", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
82 AVFILTER_DEFINE_CLASS(aevalsrc);
84 static int init(AVFilterContext *ctx)
86 EvalContext *eval = ctx->priv;
87 char *args1 = av_strdup(eval->exprs);
92 av_log(ctx, AV_LOG_ERROR, "Channels expressions list is empty\n");
93 ret = eval->exprs ? AVERROR(ENOMEM) : AVERROR(EINVAL);
97 /* parse expressions */
100 while (i < FF_ARRAY_ELEMS(eval->expr) && (expr = av_strtok(buf, "|", &buf))) {
101 ret = av_expr_parse(&eval->expr[i], expr, var_names,
102 NULL, NULL, NULL, NULL, 0, ctx);
107 eval->nb_channels = i;
109 if (eval->chlayout_str) {
111 ret = ff_parse_channel_layout(&eval->chlayout, eval->chlayout_str, ctx);
115 n = av_get_channel_layout_nb_channels(eval->chlayout);
116 if (n != eval->nb_channels) {
117 av_log(ctx, AV_LOG_ERROR,
118 "Mismatch between the specified number of channels '%d' "
119 "and the number of channels '%d' in the specified channel layout '%s'\n",
120 eval->nb_channels, n, eval->chlayout_str);
121 ret = AVERROR(EINVAL);
125 /* guess channel layout from nb expressions/channels */
126 eval->chlayout = av_get_default_channel_layout(eval->nb_channels);
127 if (!eval->chlayout) {
128 av_log(ctx, AV_LOG_ERROR, "Invalid number of channels '%d' provided\n",
130 ret = AVERROR(EINVAL);
135 if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
144 static av_cold void uninit(AVFilterContext *ctx)
146 EvalContext *eval = ctx->priv;
149 for (i = 0; i < 8; i++) {
150 av_expr_free(eval->expr[i]);
151 eval->expr[i] = NULL;
155 static int config_props(AVFilterLink *outlink)
157 EvalContext *eval = outlink->src->priv;
160 outlink->time_base = (AVRational){1, eval->sample_rate};
161 outlink->sample_rate = eval->sample_rate;
163 eval->var_values[VAR_S] = eval->sample_rate;
165 av_get_channel_layout_string(buf, sizeof(buf), 0, eval->chlayout);
167 av_log(outlink->src, AV_LOG_VERBOSE,
168 "sample_rate:%d chlayout:%s duration:%"PRId64"\n",
169 eval->sample_rate, buf, eval->duration);
174 static int query_formats(AVFilterContext *ctx)
176 EvalContext *eval = ctx->priv;
177 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE };
178 int64_t chlayouts[] = { eval->chlayout, -1 };
179 int sample_rates[] = { eval->sample_rate, -1 };
181 ff_set_common_formats (ctx, ff_make_format_list(sample_fmts));
182 ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
183 ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates));
188 static int request_frame(AVFilterLink *outlink)
190 EvalContext *eval = outlink->src->priv;
193 int64_t t = av_rescale(eval->n, AV_TIME_BASE, eval->sample_rate);
195 if (eval->duration >= 0 && t >= eval->duration)
198 samplesref = ff_get_audio_buffer(outlink, eval->nb_samples);
200 return AVERROR(ENOMEM);
202 /* evaluate expression for each single sample and for each channel */
203 for (i = 0; i < eval->nb_samples; i++, eval->n++) {
204 eval->var_values[VAR_N] = eval->n;
205 eval->var_values[VAR_T] = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
207 for (j = 0; j < eval->nb_channels; j++) {
208 *((double *) samplesref->extended_data[j] + i) =
209 av_expr_eval(eval->expr[j], eval->var_values, NULL);
213 samplesref->pts = eval->pts;
214 samplesref->sample_rate = eval->sample_rate;
215 eval->pts += eval->nb_samples;
217 return ff_filter_frame(outlink, samplesref);
220 static const AVFilterPad aevalsrc_outputs[] = {
223 .type = AVMEDIA_TYPE_AUDIO,
224 .config_props = config_props,
225 .request_frame = request_frame,
230 AVFilter avfilter_asrc_aevalsrc = {
232 .description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
234 .query_formats = query_formats,
237 .priv_size = sizeof(EvalContext),
239 .outputs = aevalsrc_outputs,
240 .priv_class = &aevalsrc_class,