FFmpeg
 All Data Structures 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 *expr_str[8];
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  { "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_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
76  { "d", "set audio duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, 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 },
79 {NULL},
80 };
81 
82 AVFILTER_DEFINE_CLASS(aevalsrc);
83 
84 static int init(AVFilterContext *ctx, const char *args)
85 {
86  EvalContext *eval = ctx->priv;
87  char *args1 = av_strdup(args);
88  char *expr, *buf, *bufptr;
89  int ret, i;
90 
91  eval->class = &aevalsrc_class;
92  av_opt_set_defaults(eval);
93 
94  if (!args1) {
95  av_log(ctx, AV_LOG_ERROR, "Argument is empty\n");
96  ret = args ? AVERROR(ENOMEM) : AVERROR(EINVAL);
97  goto end;
98  }
99 
100  /* parse expressions */
101  buf = args1;
102  i = 0;
103  while (expr = av_strtok(buf, ":", &bufptr)) {
104  ret = av_expr_parse(&eval->expr[i], expr, var_names,
105  NULL, NULL, NULL, NULL, 0, ctx);
106  if (ret < 0)
107  goto end;
108  i++;
109  if (bufptr && *bufptr == ':') { /* found last expression */
110  bufptr++;
111  break;
112  }
113  buf = NULL;
114  }
115  eval->nb_channels = i;
116 
117  if (bufptr && (ret = av_set_options_string(eval, bufptr, "=", ":")) < 0)
118  goto end;
119 
120  if (eval->chlayout_str) {
121  int n;
122  ret = ff_parse_channel_layout(&eval->chlayout, eval->chlayout_str, ctx);
123  if (ret < 0)
124  goto end;
125 
127  if (n != eval->nb_channels) {
128  av_log(ctx, AV_LOG_ERROR,
129  "Mismatch between the specified number of channels '%d' "
130  "and the number of channels '%d' in the specified channel layout '%s'\n",
131  eval->nb_channels, n, eval->chlayout_str);
132  ret = AVERROR(EINVAL);
133  goto end;
134  }
135  } else {
136  /* guess channel layout from nb expressions/channels */
138  if (!eval->chlayout) {
139  av_log(ctx, AV_LOG_ERROR, "Invalid number of channels '%d' provided\n",
140  eval->nb_channels);
141  ret = AVERROR(EINVAL);
142  goto end;
143  }
144  }
145 
146  if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
147  goto end;
148 
149  eval->duration = -1;
150  if (eval->duration_str) {
151  int64_t us = -1;
152  if ((ret = av_parse_time(&us, eval->duration_str, 1)) < 0) {
153  av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", eval->duration_str);
154  goto end;
155  }
156  eval->duration = (double)us / 1000000;
157  }
158  eval->n = 0;
159 
160 end:
161  av_free(args1);
162  return ret;
163 }
164 
165 static void uninit(AVFilterContext *ctx)
166 {
167  EvalContext *eval = ctx->priv;
168  int i;
169 
170  for (i = 0; i < 8; i++) {
171  av_expr_free(eval->expr[i]);
172  eval->expr[i] = NULL;
173  }
174  av_freep(&eval->chlayout_str);
175  av_freep(&eval->duration_str);
176  av_freep(&eval->sample_rate_str);
177 }
178 
179 static int config_props(AVFilterLink *outlink)
180 {
181  EvalContext *eval = outlink->src->priv;
182  char buf[128];
183 
184  outlink->time_base = (AVRational){1, eval->sample_rate};
185  outlink->sample_rate = eval->sample_rate;
186 
187  eval->var_values[VAR_S] = eval->sample_rate;
188 
189  av_get_channel_layout_string(buf, sizeof(buf), 0, eval->chlayout);
190 
191  av_log(outlink->src, AV_LOG_VERBOSE,
192  "sample_rate:%d chlayout:%s duration:%f\n",
193  eval->sample_rate, buf, eval->duration);
194 
195  return 0;
196 }
197 
199 {
200  EvalContext *eval = ctx->priv;
202  int64_t chlayouts[] = { eval->chlayout, -1 };
203  int sample_rates[] = { eval->sample_rate, -1 };
204 
205  ff_set_common_formats (ctx, ff_make_format_list(sample_fmts));
208 
209  return 0;
210 }
211 
212 static int request_frame(AVFilterLink *outlink)
213 {
214  EvalContext *eval = outlink->src->priv;
215  AVFilterBufferRef *samplesref;
216  int i, j;
217  double t = eval->n * (double)1/eval->sample_rate;
218 
219  if (eval->duration >= 0 && t >= eval->duration)
220  return AVERROR_EOF;
221 
222  samplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, eval->nb_samples);
223 
224  /* evaluate expression for each single sample and for each channel */
225  for (i = 0; i < eval->nb_samples; i++, eval->n++) {
226  eval->var_values[VAR_N] = eval->n;
227  eval->var_values[VAR_T] = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
228 
229  for (j = 0; j < eval->nb_channels; j++) {
230  *((double *) samplesref->extended_data[j] + i) =
231  av_expr_eval(eval->expr[j], eval->var_values, NULL);
232  }
233  }
234 
235  samplesref->pts = eval->pts;
236  samplesref->pos = -1;
237  samplesref->audio->sample_rate = eval->sample_rate;
238  eval->pts += eval->nb_samples;
239 
240  ff_filter_frame(outlink, samplesref);
241 
242  return 0;
243 }
244 
245 static const AVFilterPad aevalsrc_outputs[] = {
246  {
247  .name = "default",
248  .type = AVMEDIA_TYPE_AUDIO,
249  .config_props = config_props,
250  .request_frame = request_frame,
251  },
252  { NULL }
253 };
254 
256  .name = "aevalsrc",
257  .description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
258 
259  .query_formats = query_formats,
260  .init = init,
261  .uninit = uninit,
262  .priv_size = sizeof(EvalContext),
263  .inputs = NULL,
264  .outputs = aevalsrc_outputs,
265  .priv_class = &aevalsrc_class,
266 };