]> git.sesse.net Git - ffmpeg/blob - libavfilter/asrc_aevalsrc.c
lavfi/aevalsrc: reindent after the previous commit
[ffmpeg] / libavfilter / asrc_aevalsrc.c
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/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"
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 {
44     VAR_N,
45     VAR_T,
46     VAR_S,
47     VAR_VARS_NB
48 };
49
50 typedef struct {
51     const AVClass *class;
52     char *sample_rate_str;
53     int sample_rate;
54     int64_t chlayout;
55     char *chlayout_str;
56     int nb_channels;
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
69 static const AVOption eval_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 },
78 {NULL},
79 };
80
81 static const char *eval_get_name(void *ctx)
82 {
83     return "aevalsrc";
84 }
85
86 static const AVClass eval_class = {
87     "AEvalSrcContext",
88     eval_get_name,
89     eval_options
90 };
91
92 static int init(AVFilterContext *ctx, const char *args, void *opaque)
93 {
94     EvalContext *eval = ctx->priv;
95     char *args1 = av_strdup(args);
96     char *expr, *buf, *bufptr;
97     int ret, i;
98
99     eval->class = &eval_class;
100     av_opt_set_defaults(eval);
101
102     /* parse expressions */
103     buf = args1;
104     i = 0;
105     while (expr = av_strtok(buf, ":", &bufptr)) {
106         ret = av_expr_parse(&eval->expr[i], expr, var_names,
107                             NULL, NULL, NULL, NULL, 0, ctx);
108         if (ret < 0)
109             goto end;
110         i++;
111         if (bufptr && *bufptr == ':') { /* found last expression */
112             bufptr++;
113             break;
114         }
115         buf = NULL;
116     }
117     eval->nb_channels = i;
118
119     if (bufptr && (ret = av_set_options_string(eval, bufptr, "=", ":")) < 0)
120         goto end;
121
122     if (eval->chlayout_str) {
123         int n;
124         ret = ff_parse_channel_layout(&eval->chlayout, eval->chlayout_str, ctx);
125         if (ret < 0)
126             goto end;
127
128         n = av_get_channel_layout_nb_channels(eval->chlayout);
129         if (n != eval->nb_channels) {
130             av_log(ctx, AV_LOG_ERROR,
131                    "Mismatch between the specified number of channels '%d' "
132                    "and the number of channels '%d' in the specified channel layout '%s'\n",
133                    eval->nb_channels, n, eval->chlayout_str);
134             ret = AVERROR(EINVAL);
135             goto end;
136         }
137     } else {
138         /* guess channel layout from nb expressions/channels */
139         eval->chlayout = av_get_default_channel_layout(eval->nb_channels);
140         if (!eval->chlayout) {
141             av_log(ctx, AV_LOG_ERROR, "Invalid number of channels '%d' provided\n",
142                    eval->nb_channels);
143             ret = AVERROR(EINVAL);
144             goto end;
145         }
146     }
147
148     if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
149         goto end;
150
151     eval->duration = -1;
152     if (eval->duration_str) {
153         int64_t us = -1;
154         if ((ret = av_parse_time(&us, eval->duration_str, 1)) < 0) {
155             av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", eval->duration_str);
156             goto end;
157         }
158         eval->duration = (double)us / 1000000;
159     }
160     eval->n = 0;
161
162 end:
163     av_free(args1);
164     return ret;
165 }
166
167 static void uninit(AVFilterContext *ctx)
168 {
169     EvalContext *eval = ctx->priv;
170     int i;
171
172     for (i = 0; i < 8; i++) {
173         av_expr_free(eval->expr[i]);
174         eval->expr[i] = NULL;
175     }
176     av_freep(&eval->chlayout_str);
177     av_freep(&eval->duration_str);
178     av_freep(&eval->sample_rate_str);
179 }
180
181 static int config_props(AVFilterLink *outlink)
182 {
183     EvalContext *eval = outlink->src->priv;
184     char buf[128];
185
186     outlink->time_base = (AVRational){1, eval->sample_rate};
187     outlink->sample_rate = eval->sample_rate;
188
189     eval->var_values[VAR_S] = eval->sample_rate;
190
191     av_get_channel_layout_string(buf, sizeof(buf), 0, eval->chlayout);
192
193     av_log(outlink->src, AV_LOG_INFO,
194            "sample_rate:%d chlayout:%s duration:%f\n",
195            eval->sample_rate, buf, eval->duration);
196
197     return 0;
198 }
199
200 static int query_formats(AVFilterContext *ctx)
201 {
202     EvalContext *eval = ctx->priv;
203     enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE };
204     int64_t chlayouts[] = { eval->chlayout, -1 };
205     int sample_rates[] = { eval->sample_rate, -1 };
206
207     avfilter_set_common_sample_formats (ctx, avfilter_make_format_list(sample_fmts));
208     ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
209     ff_set_common_samplerates(ctx, avfilter_make_format_list(sample_rates));
210
211     return 0;
212 }
213
214 static int request_frame(AVFilterLink *outlink)
215 {
216     EvalContext *eval = outlink->src->priv;
217     AVFilterBufferRef *samplesref;
218     int i, j;
219     double t = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
220
221     if (eval->duration >= 0 && t > eval->duration)
222         return AVERROR_EOF;
223
224     samplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, eval->nb_samples);
225
226     /* evaluate expression for each single sample and for each channel */
227     for (i = 0; i < eval->nb_samples; i++, eval->n++) {
228         eval->var_values[VAR_N] = eval->n;
229         eval->var_values[VAR_T] = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
230
231         for (j = 0; j < eval->nb_channels; j++) {
232             *((double *) samplesref->extended_data[j] + i) =
233                 av_expr_eval(eval->expr[j], eval->var_values, NULL);
234         }
235     }
236
237     samplesref->pts = eval->pts;
238     samplesref->pos = -1;
239     samplesref->audio->sample_rate = eval->sample_rate;
240     eval->pts += eval->nb_samples;
241
242     ff_filter_samples(outlink, samplesref);
243
244     return 0;
245 }
246
247 AVFilter avfilter_asrc_aevalsrc = {
248     .name        = "aevalsrc",
249     .description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
250
251     .query_formats = query_formats,
252     .init        = init,
253     .uninit      = uninit,
254     .priv_size   = sizeof(EvalContext),
255
256     .inputs      = (const AVFilterPad[]) {{ .name = NULL}},
257
258     .outputs     = (const AVFilterPad[]) {{ .name = "default",
259                                       .type = AVMEDIA_TYPE_AUDIO,
260                                       .config_props = config_props,
261                                       .request_frame = request_frame, },
262                                     { .name = NULL}},
263 };