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/audioconvert.h"
27 #include "libavutil/avassert.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/eval.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/parseutils.h"
35 static const char * const var_names[] = {
36 "n", ///< number of frame
37 "t", ///< timestamp expressed in seconds
51 char *sample_rate_str;
58 int nb_samples; ///< number of samples per requested frame
59 char *duration_str; ///< total duration of the generated audio
62 double var_values[VAR_VARS_NB];
65 #define OFFSET(x) offsetof(EvalContext, x)
67 static const AVOption eval_options[]= {
68 { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.dbl = 1024}, 0, INT_MAX },
69 { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.dbl = 1024}, 0, INT_MAX },
70 { "sample_rate", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, CHAR_MIN, CHAR_MAX },
71 { "s", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, CHAR_MIN, CHAR_MAX },
72 { "duration", "set audio duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
73 { "d", "set audio duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
77 static const char *eval_get_name(void *ctx)
82 static const AVClass eval_class = {
88 static int init(AVFilterContext *ctx, const char *args, void *opaque)
90 EvalContext *eval = ctx->priv;
91 char *args1 = av_strdup(args);
92 char *expr, *buf, *bufptr;
95 eval->class = &eval_class;
96 av_opt_set_defaults(eval);
98 /* parse expressions */
101 while (expr = av_strtok(buf, ":", &bufptr)) {
103 av_log(ctx, AV_LOG_ERROR,
104 "More than 8 expressions provided, unsupported.\n");
105 ret = AVERROR(EINVAL);
108 ret = av_expr_parse(&eval->expr[i], expr, var_names,
109 NULL, NULL, NULL, NULL, 0, ctx);
113 if (bufptr && *bufptr == ':') { /* found last expression */
120 /* guess channel layout from nb expressions/channels */
121 eval->nb_channels = i;
122 eval->chlayout = av_get_default_channel_layout(eval->nb_channels);
123 if (!eval->chlayout) {
124 av_log(ctx, AV_LOG_ERROR, "Invalid number of channels '%d' provided\n",
126 ret = AVERROR(EINVAL);
130 if (bufptr && (ret = av_set_options_string(eval, bufptr, "=", ":")) < 0)
133 if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
137 if (eval->duration_str) {
139 if ((ret = av_parse_time(&us, eval->duration_str, 1)) < 0) {
140 av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", eval->duration_str);
143 eval->duration = (double)us / 1000000;
152 static void uninit(AVFilterContext *ctx)
154 EvalContext *eval = ctx->priv;
157 for (i = 0; i < 8; i++) {
158 av_expr_free(eval->expr[i]);
159 eval->expr[i] = NULL;
161 av_freep(&eval->duration_str);
162 av_freep(&eval->sample_rate_str);
165 static int config_props(AVFilterLink *outlink)
167 EvalContext *eval = outlink->src->priv;
170 outlink->time_base = (AVRational){1, eval->sample_rate};
171 outlink->sample_rate = eval->sample_rate;
173 eval->var_values[VAR_S] = eval->sample_rate;
175 av_get_channel_layout_string(buf, sizeof(buf), 0, eval->chlayout);
177 av_log(outlink->src, AV_LOG_INFO,
178 "sample_rate:%d chlayout:%s duration:%f\n",
179 eval->sample_rate, buf, eval->duration);
184 static int query_formats(AVFilterContext *ctx)
186 EvalContext *eval = ctx->priv;
187 enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_NONE };
188 int64_t chlayouts[] = { eval->chlayout, -1 };
189 int packing_fmts[] = { AVFILTER_PLANAR, -1 };
191 avfilter_set_common_sample_formats (ctx, avfilter_make_format_list(sample_fmts));
192 avfilter_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
193 avfilter_set_common_packing_formats(ctx, avfilter_make_format_list(packing_fmts));
198 static int request_frame(AVFilterLink *outlink)
200 EvalContext *eval = outlink->src->priv;
201 AVFilterBufferRef *samplesref;
203 double t = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
205 if (eval->duration >= 0 && t > eval->duration)
208 samplesref = avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, eval->nb_samples);
210 /* evaluate expression for each single sample and for each channel */
211 for (i = 0; i < eval->nb_samples; i++, eval->n++) {
212 eval->var_values[VAR_N] = eval->n;
213 eval->var_values[VAR_T] = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
215 for (j = 0; j < eval->nb_channels; j++) {
216 *((double *) samplesref->data[j] + i) =
217 av_expr_eval(eval->expr[j], eval->var_values, NULL);
221 samplesref->pts = eval->pts;
222 samplesref->pos = -1;
223 samplesref->audio->sample_rate = eval->sample_rate;
224 eval->pts += eval->nb_samples;
226 avfilter_filter_samples(outlink, samplesref);
231 AVFilter avfilter_asrc_aevalsrc = {
233 .description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
235 .query_formats = query_formats,
238 .priv_size = sizeof(EvalContext),
240 .inputs = (const AVFilterPad[]) {{ .name = NULL}},
242 .outputs = (const AVFilterPad[]) {{ .name = "default",
243 .type = AVMEDIA_TYPE_AUDIO,
244 .config_props = config_props,
245 .request_frame = request_frame, },