FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
asrc_aevalsrc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Stefano Sabatini
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 /**
22  * @file
23  * eval audio source
24  */
25 
26 #include "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
29 #include "libavutil/eval.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/parseutils.h"
32 #include "avfilter.h"
33 #include "audio.h"
34 #include "internal.h"
35 
36 static const char * const var_names[] = {
37  "n", ///< number of frame
38  "t", ///< timestamp expressed in seconds
39  "s", ///< sample rate
40  NULL
41 };
42 
43 enum var_name {
48 };
49 
50 typedef struct {
51  const AVClass *class;
54  int64_t chlayout;
55  char *chlayout_str;
57  int64_t pts;
58  AVExpr *expr[8];
59  char *exprs;
60  int nb_samples; ///< number of samples per requested frame
61  char *duration_str; ///< total duration of the generated audio
62  double duration;
63  uint64_t n;
64  double var_values[VAR_VARS_NB];
65 } EvalContext;
66 
67 #define OFFSET(x) offsetof(EvalContext, x)
68 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
69 
70 static const AVOption aevalsrc_options[]= {
71  { "exprs", "set the '|'-separated list of channels expressions", OFFSET(exprs), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
72  { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
73  { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
74  { "sample_rate", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, CHAR_MIN, CHAR_MAX, FLAGS },
75  { "s", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, CHAR_MIN, CHAR_MAX, FLAGS },
76  { "duration", "set audio duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
77  { "d", "set audio duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
78  { "channel_layout", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
79  { "c", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
80 {NULL},
81 };
82 
83 AVFILTER_DEFINE_CLASS(aevalsrc);
84 
85 static int init(AVFilterContext *ctx)
86 {
87  EvalContext *eval = ctx->priv;
88  char *args1 = av_strdup(eval->exprs);
89  char *expr, *buf;
90  int ret, i;
91 
92  if (!args1) {
93  av_log(ctx, AV_LOG_ERROR, "Channels expressions list is empty\n");
94  ret = eval->exprs ? AVERROR(ENOMEM) : AVERROR(EINVAL);
95  goto end;
96  }
97 
98  /* parse expressions */
99  buf = args1;
100  i = 0;
101  while (i < FF_ARRAY_ELEMS(eval->expr) && (expr = av_strtok(buf, "|", &buf))) {
102  ret = av_expr_parse(&eval->expr[i], expr, var_names,
103  NULL, NULL, NULL, NULL, 0, ctx);
104  if (ret < 0)
105  goto end;
106  i++;
107  }
108  eval->nb_channels = i;
109 
110  if (eval->chlayout_str) {
111  int n;
112  ret = ff_parse_channel_layout(&eval->chlayout, eval->chlayout_str, ctx);
113  if (ret < 0)
114  goto end;
115 
117  if (n != eval->nb_channels) {
118  av_log(ctx, AV_LOG_ERROR,
119  "Mismatch between the specified number of channels '%d' "
120  "and the number of channels '%d' in the specified channel layout '%s'\n",
121  eval->nb_channels, n, eval->chlayout_str);
122  ret = AVERROR(EINVAL);
123  goto end;
124  }
125  } else {
126  /* guess channel layout from nb expressions/channels */
128  if (!eval->chlayout) {
129  av_log(ctx, AV_LOG_ERROR, "Invalid number of channels '%d' provided\n",
130  eval->nb_channels);
131  ret = AVERROR(EINVAL);
132  goto end;
133  }
134  }
135 
136  if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
137  goto end;
138 
139  eval->duration = -1;
140  if (eval->duration_str) {
141  int64_t us = -1;
142  if ((ret = av_parse_time(&us, eval->duration_str, 1)) < 0) {
143  av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", eval->duration_str);
144  goto end;
145  }
146  eval->duration = (double)us / 1000000;
147  }
148  eval->n = 0;
149 
150 end:
151  av_free(args1);
152  return ret;
153 }
154 
155 static av_cold void uninit(AVFilterContext *ctx)
156 {
157  EvalContext *eval = ctx->priv;
158  int i;
159 
160  for (i = 0; i < 8; i++) {
161  av_expr_free(eval->expr[i]);
162  eval->expr[i] = NULL;
163  }
164  av_freep(&eval->chlayout_str);
165  av_freep(&eval->duration_str);
166  av_freep(&eval->sample_rate_str);
167 }
168 
169 static int config_props(AVFilterLink *outlink)
170 {
171  EvalContext *eval = outlink->src->priv;
172  char buf[128];
173 
174  outlink->time_base = (AVRational){1, eval->sample_rate};
175  outlink->sample_rate = eval->sample_rate;
176 
177  eval->var_values[VAR_S] = eval->sample_rate;
178 
179  av_get_channel_layout_string(buf, sizeof(buf), 0, eval->chlayout);
180 
181  av_log(outlink->src, AV_LOG_VERBOSE,
182  "sample_rate:%d chlayout:%s duration:%f\n",
183  eval->sample_rate, buf, eval->duration);
184 
185  return 0;
186 }
187 
189 {
190  EvalContext *eval = ctx->priv;
192  int64_t chlayouts[] = { eval->chlayout, -1 };
193  int sample_rates[] = { eval->sample_rate, -1 };
194 
195  ff_set_common_formats (ctx, ff_make_format_list(sample_fmts));
198 
199  return 0;
200 }
201 
202 static int request_frame(AVFilterLink *outlink)
203 {
204  EvalContext *eval = outlink->src->priv;
205  AVFrame *samplesref;
206  int i, j;
207  double t = eval->n * (double)1/eval->sample_rate;
208 
209  if (eval->duration >= 0 && t >= eval->duration)
210  return AVERROR_EOF;
211 
212  samplesref = ff_get_audio_buffer(outlink, eval->nb_samples);
213  if (!samplesref)
214  return AVERROR(ENOMEM);
215 
216  /* evaluate expression for each single sample and for each channel */
217  for (i = 0; i < eval->nb_samples; i++, eval->n++) {
218  eval->var_values[VAR_N] = eval->n;
219  eval->var_values[VAR_T] = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
220 
221  for (j = 0; j < eval->nb_channels; j++) {
222  *((double *) samplesref->extended_data[j] + i) =
223  av_expr_eval(eval->expr[j], eval->var_values, NULL);
224  }
225  }
226 
227  samplesref->pts = eval->pts;
228  samplesref->sample_rate = eval->sample_rate;
229  eval->pts += eval->nb_samples;
230 
231  return ff_filter_frame(outlink, samplesref);
232 }
233 
234 static const AVFilterPad aevalsrc_outputs[] = {
235  {
236  .name = "default",
237  .type = AVMEDIA_TYPE_AUDIO,
238  .config_props = config_props,
239  .request_frame = request_frame,
240  },
241  { NULL }
242 };
243 
245  .name = "aevalsrc",
246  .description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
247 
248  .query_formats = query_formats,
249  .init = init,
250  .uninit = uninit,
251  .priv_size = sizeof(EvalContext),
252  .inputs = NULL,
253  .outputs = aevalsrc_outputs,
254  .priv_class = &aevalsrc_class,
255 };