FFmpeg  2.1.1
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"
28 #include "libavutil/channel_layout.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  ret = av_expr_parse(&eval->expr[eval->nb_channels - 1], expr, var_names,
105  NULL, NULL, NULL, NULL, 0, ctx);
106  if (ret < 0)
107  goto end;
108  }
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 && eval->nb_channels <= 0) {
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  eval->n = 0;
139 
140 end:
141  av_free(args1);
142  return ret;
143 }
144 
145 static av_cold void uninit(AVFilterContext *ctx)
146 {
147  EvalContext *eval = ctx->priv;
148  int i;
149 
150  for (i = 0; i < eval->nb_channels; i++) {
151  av_expr_free(eval->expr[i]);
152  eval->expr[i] = NULL;
153  }
154  av_freep(&eval->expr);
155 }
156 
157 static int config_props(AVFilterLink *outlink)
158 {
159  EvalContext *eval = outlink->src->priv;
160  char buf[128];
161 
162  outlink->time_base = (AVRational){1, eval->sample_rate};
163  outlink->sample_rate = eval->sample_rate;
164 
165  eval->var_values[VAR_S] = eval->sample_rate;
166 
167  av_get_channel_layout_string(buf, sizeof(buf), 0, eval->chlayout);
168 
169  av_log(outlink->src, AV_LOG_VERBOSE,
170  "sample_rate:%d chlayout:%s duration:%"PRId64"\n",
171  eval->sample_rate, buf, eval->duration);
172 
173  return 0;
174 }
175 
177 {
178  EvalContext *eval = ctx->priv;
180  int64_t chlayouts[] = { eval->chlayout ? eval->chlayout : FF_COUNT2LAYOUT(eval->nb_channels) , -1 };
181  int sample_rates[] = { eval->sample_rate, -1 };
182 
183  ff_set_common_formats (ctx, ff_make_format_list(sample_fmts));
186 
187  return 0;
188 }
189 
190 static int request_frame(AVFilterLink *outlink)
191 {
192  EvalContext *eval = outlink->src->priv;
193  AVFrame *samplesref;
194  int i, j;
195  int64_t t = av_rescale(eval->n, AV_TIME_BASE, eval->sample_rate);
196 
197  if (eval->duration >= 0 && t >= eval->duration)
198  return AVERROR_EOF;
199 
200  samplesref = ff_get_audio_buffer(outlink, eval->nb_samples);
201  if (!samplesref)
202  return AVERROR(ENOMEM);
203 
204  /* evaluate expression for each single sample and for each channel */
205  for (i = 0; i < eval->nb_samples; i++, eval->n++) {
206  eval->var_values[VAR_N] = eval->n;
207  eval->var_values[VAR_T] = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
208 
209  for (j = 0; j < eval->nb_channels; j++) {
210  *((double *) samplesref->extended_data[j] + i) =
211  av_expr_eval(eval->expr[j], eval->var_values, NULL);
212  }
213  }
214 
215  samplesref->pts = eval->pts;
216  samplesref->sample_rate = eval->sample_rate;
217  eval->pts += eval->nb_samples;
218 
219  return ff_filter_frame(outlink, samplesref);
220 }
221 
222 static const AVFilterPad aevalsrc_outputs[] = {
223  {
224  .name = "default",
225  .type = AVMEDIA_TYPE_AUDIO,
226  .config_props = config_props,
227  .request_frame = request_frame,
228  },
229  { NULL }
230 };
231 
233  .name = "aevalsrc",
234  .description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
235  .query_formats = query_formats,
236  .init = init,
237  .uninit = uninit,
238  .priv_size = sizeof(EvalContext),
239  .inputs = NULL,
240  .outputs = aevalsrc_outputs,
241  .priv_class = &aevalsrc_class,
242 };
static const AVFilterPad aevalsrc_outputs[]
char * exprs
Definition: asrc_aevalsrc.c:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
AVOption.
Definition: opt.h:253
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:692
const char * name
Filter name.
Definition: avfilter.h:468
void * priv
private data for use by the filter
Definition: avfilter.h:648
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:111
char * av_strdup(const char *s) av_malloc_attrib
Duplicate the string s.
Definition: mem.c:256
int64_t duration
Definition: asrc_aevalsrc.c:61
AVExpr ** expr
Definition: asrc_aevalsrc.c:58
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:140
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
Definition: eval.c:641
char * chlayout_str
Definition: asrc_aevalsrc.c:55
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:303
#define av_cold
Definition: avcodec.h:653
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:294
const char * name
Pad name.
Definition: internal.h:66
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1118
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
int nb_samples
number of samples per requested frame
Definition: asrc_aevalsrc.c:60
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:182
Definition: eval.c:141
int64_t pts
Definition: asrc_aevalsrc.c:57
#define AV_LOG_VERBOSE
Detailed information.
Definition: avcodec.h:4163
void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:531
static int64_t duration
Definition: ffplay.c:306
A filter pad used for either input or output.
Definition: internal.h:60
#define FLAGS
Definition: asrc_aevalsrc.c:67
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:219
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:70
static av_cold int init(AVFilterContext *ctx)
Definition: asrc_aevalsrc.c:84
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
uint64_t n
Definition: asrc_aevalsrc.c:62
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
int64_t chlayout
Definition: asrc_aevalsrc.c:54
static const int sample_rates[]
Definition: dcaenc.h:32
int64_t av_rescale(int64_t a, int64_t b, int64_t c) av_const
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:118
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avcodec.h:2284
var_name
ret
Definition: avfilter.c:961
void * av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size, const uint8_t *elem_data)
Add an element of size elem_size to a dynamic array.
Definition: mem.c:309
int n
Definition: avisynth_c.h:588
static int request_frame(AVFilterLink *outlink)
Main libavfilter public API header.
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
AVFilterChannelLayouts * avfilter_make_format64_list(const int64_t *fmts)
Definition: formats.c:303
static int query_formats(AVFilterContext *ctx)
char * sample_rate_str
Definition: asrc_aevalsrc.c:52
void * buf
Definition: avisynth_c.h:594
Describe the class of an AVClass context structure.
Definition: log.h:50
int sample_rate
Sample rate of the audio data.
Definition: frame.h:349
Filter definition.
Definition: avfilter.h:464
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:102
rational number numerator/denominator
Definition: rational.h:43
#define OFFSET(x)
Definition: asrc_aevalsrc.c:66
static const char *const var_names[]
Definition: asrc_aevalsrc.c:36
#define AVERROR_EOF
void ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:519
static float t
Definition: muxing.c:123
int ff_parse_channel_layout(int64_t *ret, const char *arg, void *log_ctx)
Parse a channel layout or a corresponding integer representation.
Definition: formats.c:618
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok()...
Definition: avstring.c:183
static const AVOption aevalsrc_options[]
Definition: asrc_aevalsrc.c:69
static int config_props(AVFilterLink *outlink)
int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
Parse a sample rate.
Definition: formats.c:606
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:301
AVFilter avfilter_asrc_aevalsrc
#define AVERROR(e)
An instance of a filter.
Definition: avfilter.h:627
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
#define FF_COUNT2LAYOUT(c)
Encode a channel count as a channel layout.
Definition: formats.h:102
void ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:512
internal API functions
double, planar
Definition: samplefmt.h:61
double var_values[VAR_VARS_NB]
Definition: asrc_aevalsrc.c:63
static av_cold void uninit(AVFilterContext *ctx)
for(j=16;j >0;--j)
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.
int64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.