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;
59  char *exprs;
60  int nb_samples; ///< number of samples per requested frame
61  int64_t duration;
62  uint64_t n;
63  double var_values[VAR_VARS_NB];
64 } EvalContext;
65 
66 #define OFFSET(x) offsetof(EvalContext, x)
67 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
68 
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 },
79  { NULL }
80 };
81 
82 AVFILTER_DEFINE_CLASS(aevalsrc);
83 
84 static av_cold int init(AVFilterContext *ctx)
85 {
86  EvalContext *eval = ctx->priv;
87  char *args1 = av_strdup(eval->exprs);
88  char *expr, *buf;
89  int ret;
90 
91  if (!args1) {
92  av_log(ctx, AV_LOG_ERROR, "Channels expressions list is empty\n");
93  ret = eval->exprs ? AVERROR(ENOMEM) : AVERROR(EINVAL);
94  goto end;
95  }
96 
97  /* parse expressions */
98  buf = args1;
99  while (expr = av_strtok(buf, "|", &buf)) {
100  if (!av_dynarray2_add((void **)&eval->expr, &eval->nb_channels, sizeof(*eval->expr), NULL)) {
101  ret = AVERROR(ENOMEM);
102  goto end;
103  }
104  eval->expr[eval->nb_channels-1] = NULL;
105  ret = av_expr_parse(&eval->expr[eval->nb_channels - 1], expr, var_names,
106  NULL, NULL, NULL, NULL, 0, ctx);
107  if (ret < 0)
108  goto end;
109  }
110 
111  if (eval->chlayout_str) {
112  int n;
113  ret = ff_parse_channel_layout(&eval->chlayout, NULL, eval->chlayout_str, ctx);
114  if (ret < 0)
115  goto end;
116 
118  if (n != eval->nb_channels) {
119  av_log(ctx, AV_LOG_ERROR,
120  "Mismatch between the specified number of channels '%d' "
121  "and the number of channels '%d' in the specified channel layout '%s'\n",
122  eval->nb_channels, n, eval->chlayout_str);
123  ret = AVERROR(EINVAL);
124  goto end;
125  }
126  } else {
127  /* guess channel layout from nb expressions/channels */
129  if (!eval->chlayout && eval->nb_channels <= 0) {
130  av_log(ctx, AV_LOG_ERROR, "Invalid number of channels '%d' provided\n",
131  eval->nb_channels);
132  ret = AVERROR(EINVAL);
133  goto end;
134  }
135  }
136 
137  if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
138  goto end;
139  eval->n = 0;
140 
141 end:
142  av_free(args1);
143  return ret;
144 }
145 
146 static av_cold void uninit(AVFilterContext *ctx)
147 {
148  EvalContext *eval = ctx->priv;
149  int i;
150 
151  for (i = 0; i < eval->nb_channels; i++) {
152  av_expr_free(eval->expr[i]);
153  eval->expr[i] = NULL;
154  }
155  av_freep(&eval->expr);
156 }
157 
158 static int config_props(AVFilterLink *outlink)
159 {
160  EvalContext *eval = outlink->src->priv;
161  char buf[128];
162 
163  outlink->time_base = (AVRational){1, eval->sample_rate};
164  outlink->sample_rate = eval->sample_rate;
165 
166  eval->var_values[VAR_S] = eval->sample_rate;
167 
168  av_get_channel_layout_string(buf, sizeof(buf), 0, eval->chlayout);
169 
170  av_log(outlink->src, AV_LOG_VERBOSE,
171  "sample_rate:%d chlayout:%s duration:%"PRId64"\n",
172  eval->sample_rate, buf, eval->duration);
173 
174  return 0;
175 }
176 
178 {
179  EvalContext *eval = ctx->priv;
181  int64_t chlayouts[] = { eval->chlayout ? eval->chlayout : FF_COUNT2LAYOUT(eval->nb_channels) , -1 };
182  int sample_rates[] = { eval->sample_rate, -1 };
183 
184  ff_set_common_formats (ctx, ff_make_format_list(sample_fmts));
187 
188  return 0;
189 }
190 
191 static int request_frame(AVFilterLink *outlink)
192 {
193  EvalContext *eval = outlink->src->priv;
194  AVFrame *samplesref;
195  int i, j;
196  int64_t t = av_rescale(eval->n, AV_TIME_BASE, eval->sample_rate);
197 
198  if (eval->duration >= 0 && t >= eval->duration)
199  return AVERROR_EOF;
200 
201  samplesref = ff_get_audio_buffer(outlink, eval->nb_samples);
202  if (!samplesref)
203  return AVERROR(ENOMEM);
204 
205  /* evaluate expression for each single sample and for each channel */
206  for (i = 0; i < eval->nb_samples; i++, eval->n++) {
207  eval->var_values[VAR_N] = eval->n;
208  eval->var_values[VAR_T] = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
209 
210  for (j = 0; j < eval->nb_channels; j++) {
211  *((double *) samplesref->extended_data[j] + i) =
212  av_expr_eval(eval->expr[j], eval->var_values, NULL);
213  }
214  }
215 
216  samplesref->pts = eval->pts;
217  samplesref->sample_rate = eval->sample_rate;
218  eval->pts += eval->nb_samples;
219 
220  return ff_filter_frame(outlink, samplesref);
221 }
222 
223 static const AVFilterPad aevalsrc_outputs[] = {
224  {
225  .name = "default",
226  .type = AVMEDIA_TYPE_AUDIO,
227  .config_props = config_props,
228  .request_frame = request_frame,
229  },
230  { NULL }
231 };
232 
234  .name = "aevalsrc",
235  .description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
236  .query_formats = query_formats,
237  .init = init,
238  .uninit = uninit,
239  .priv_size = sizeof(EvalContext),
240  .inputs = NULL,
241  .outputs = aevalsrc_outputs,
242  .priv_class = &aevalsrc_class,
243 };