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"
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
61 char *duration_str; ///< total duration of the generated audio
64 double var_values[VAR_VARS_NB];
67 #define OFFSET(x) offsetof(EvalContext, x)
69 static const AVOption aevalsrc_options[]= {
70 { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.dbl = 1024}, 0, INT_MAX },
71 { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.dbl = 1024}, 0, INT_MAX },
72 { "sample_rate", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, CHAR_MIN, CHAR_MAX },
73 { "s", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, CHAR_MIN, CHAR_MAX },
74 { "duration", "set audio duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
75 { "d", "set audio duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
76 { "channel_layout", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
77 { "c", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
81 AVFILTER_DEFINE_CLASS(aevalsrc);
83 static int init(AVFilterContext *ctx, const char *args)
85 EvalContext *eval = ctx->priv;
86 char *args1 = av_strdup(args);
87 char *expr, *buf, *bufptr;
90 eval->class = &aevalsrc_class;
91 av_opt_set_defaults(eval);
93 /* parse expressions */
96 while (expr = av_strtok(buf, ":", &bufptr)) {
97 ret = av_expr_parse(&eval->expr[i], expr, var_names,
98 NULL, NULL, NULL, NULL, 0, ctx);
102 if (bufptr && *bufptr == ':') { /* found last expression */
108 eval->nb_channels = i;
110 if (bufptr && (ret = av_set_options_string(eval, bufptr, "=", ":")) < 0)
113 if (eval->chlayout_str) {
115 ret = ff_parse_channel_layout(&eval->chlayout, eval->chlayout_str, ctx);
119 n = av_get_channel_layout_nb_channels(eval->chlayout);
120 if (n != eval->nb_channels) {
121 av_log(ctx, AV_LOG_ERROR,
122 "Mismatch between the specified number of channels '%d' "
123 "and the number of channels '%d' in the specified channel layout '%s'\n",
124 eval->nb_channels, n, eval->chlayout_str);
125 ret = AVERROR(EINVAL);
129 /* guess channel layout from nb expressions/channels */
130 eval->chlayout = av_get_default_channel_layout(eval->nb_channels);
131 if (!eval->chlayout) {
132 av_log(ctx, AV_LOG_ERROR, "Invalid number of channels '%d' provided\n",
134 ret = AVERROR(EINVAL);
139 if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
143 if (eval->duration_str) {
145 if ((ret = av_parse_time(&us, eval->duration_str, 1)) < 0) {
146 av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", eval->duration_str);
149 eval->duration = (double)us / 1000000;
158 static void uninit(AVFilterContext *ctx)
160 EvalContext *eval = ctx->priv;
163 for (i = 0; i < 8; i++) {
164 av_expr_free(eval->expr[i]);
165 eval->expr[i] = NULL;
167 av_freep(&eval->chlayout_str);
168 av_freep(&eval->duration_str);
169 av_freep(&eval->sample_rate_str);
172 static int config_props(AVFilterLink *outlink)
174 EvalContext *eval = outlink->src->priv;
177 outlink->time_base = (AVRational){1, eval->sample_rate};
178 outlink->sample_rate = eval->sample_rate;
180 eval->var_values[VAR_S] = eval->sample_rate;
182 av_get_channel_layout_string(buf, sizeof(buf), 0, eval->chlayout);
184 av_log(outlink->src, AV_LOG_INFO,
185 "sample_rate:%d chlayout:%s duration:%f\n",
186 eval->sample_rate, buf, eval->duration);
191 static int query_formats(AVFilterContext *ctx)
193 EvalContext *eval = ctx->priv;
194 enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE };
195 int64_t chlayouts[] = { eval->chlayout, -1 };
196 int sample_rates[] = { eval->sample_rate, -1 };
198 ff_set_common_formats (ctx, ff_make_format_list(sample_fmts));
199 ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
200 ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates));
205 static int request_frame(AVFilterLink *outlink)
207 EvalContext *eval = outlink->src->priv;
208 AVFilterBufferRef *samplesref;
210 double t = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
212 if (eval->duration >= 0 && t > eval->duration)
215 samplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, eval->nb_samples);
217 /* evaluate expression for each single sample and for each channel */
218 for (i = 0; i < eval->nb_samples; i++, eval->n++) {
219 eval->var_values[VAR_N] = eval->n;
220 eval->var_values[VAR_T] = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
222 for (j = 0; j < eval->nb_channels; j++) {
223 *((double *) samplesref->extended_data[j] + i) =
224 av_expr_eval(eval->expr[j], eval->var_values, NULL);
228 samplesref->pts = eval->pts;
229 samplesref->pos = -1;
230 samplesref->audio->sample_rate = eval->sample_rate;
231 eval->pts += eval->nb_samples;
233 ff_filter_samples(outlink, samplesref);
238 AVFilter avfilter_asrc_aevalsrc = {
240 .description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
242 .query_formats = query_formats,
245 .priv_size = sizeof(EvalContext),
247 .inputs = (const AVFilterPad[]) {{ .name = NULL}},
249 .outputs = (const AVFilterPad[]) {{ .name = "default",
250 .type = AVMEDIA_TYPE_AUDIO,
251 .config_props = config_props,
252 .request_frame = request_frame, },