]> git.sesse.net Git - ffmpeg/blob - libavfilter/asrc_aevalsrc.c
Merge remote-tracking branch 'qatar/master'
[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 "internal.h"
34
35 static const char * const var_names[] = {
36     "n",            ///< number of frame
37     "t",            ///< timestamp expressed in seconds
38     "s",            ///< sample rate
39     NULL
40 };
41
42 enum var_name {
43     VAR_N,
44     VAR_T,
45     VAR_S,
46     VAR_VARS_NB
47 };
48
49 typedef struct {
50     const AVClass *class;
51     char *sample_rate_str;
52     int sample_rate;
53     int64_t chlayout;
54     int nb_channels;
55     int64_t pts;
56     AVExpr *expr[8];
57     char *expr_str[8];
58     int nb_samples;             ///< number of samples per requested frame
59     char *duration_str;         ///< total duration of the generated audio
60     double duration;
61     uint64_t n;
62     double var_values[VAR_VARS_NB];
63 } EvalContext;
64
65 #define OFFSET(x) offsetof(EvalContext, x)
66
67 static const AVOption eval_options[]= {
68     { "nb_samples",  "set the number of samples per requested frame", OFFSET(nb_samples),      AV_OPT_TYPE_INT,    {.dbl = 1024},    0,        INT_MAX },
69     { "n",           "set the number of samples per requested frame", OFFSET(nb_samples),      AV_OPT_TYPE_INT,    {.dbl = 1024},    0,        INT_MAX },
70     { "sample_rate", "set the sample rate",                           OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, CHAR_MIN, CHAR_MAX },
71     { "s",           "set the sample rate",                           OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, CHAR_MIN, CHAR_MAX },
72     { "duration",    "set audio duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
73     { "d",           "set audio duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
74 {NULL},
75 };
76
77 static const char *eval_get_name(void *ctx)
78 {
79     return "aevalsrc";
80 }
81
82 static const AVClass eval_class = {
83     "AEvalSrcContext",
84     eval_get_name,
85     eval_options
86 };
87
88 static int init(AVFilterContext *ctx, const char *args, void *opaque)
89 {
90     EvalContext *eval = ctx->priv;
91     char *args1 = av_strdup(args);
92     char *expr, *buf, *bufptr;
93     int ret, i;
94
95     eval->class = &eval_class;
96     av_opt_set_defaults(eval);
97
98     /* parse expressions */
99     buf = args1;
100     i = 0;
101     while (expr = av_strtok(buf, ":", &bufptr)) {
102         if (i >= 8) {
103             av_log(ctx, AV_LOG_ERROR,
104                    "More than 8 expressions provided, unsupported.\n");
105             ret = AVERROR(EINVAL);
106             return ret;
107         }
108         ret = av_expr_parse(&eval->expr[i], expr, var_names,
109                             NULL, NULL, NULL, NULL, 0, ctx);
110         if (ret < 0)
111             goto end;
112         i++;
113         if (bufptr && *bufptr == ':') { /* found last expression */
114             bufptr++;
115             break;
116         }
117         buf = NULL;
118     }
119
120     /* guess channel layout from nb expressions/channels */
121     eval->nb_channels = i;
122     eval->chlayout = av_get_default_channel_layout(eval->nb_channels);
123     if (!eval->chlayout) {
124         av_log(ctx, AV_LOG_ERROR, "Invalid number of channels '%d' provided\n",
125                eval->nb_channels);
126         ret = AVERROR(EINVAL);
127         goto end;
128     }
129
130     if (bufptr && (ret = av_set_options_string(eval, bufptr, "=", ":")) < 0)
131         goto end;
132
133     if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
134         goto end;
135
136     eval->duration = -1;
137     if (eval->duration_str) {
138         int64_t us = -1;
139         if ((ret = av_parse_time(&us, eval->duration_str, 1)) < 0) {
140             av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", eval->duration_str);
141             goto end;
142         }
143         eval->duration = (double)us / 1000000;
144     }
145     eval->n = 0;
146
147 end:
148     av_free(args1);
149     return ret;
150 }
151
152 static void uninit(AVFilterContext *ctx)
153 {
154     EvalContext *eval = ctx->priv;
155     int i;
156
157     for (i = 0; i < 8; i++) {
158         av_expr_free(eval->expr[i]);
159         eval->expr[i] = NULL;
160     }
161     av_freep(&eval->duration_str);
162     av_freep(&eval->sample_rate_str);
163 }
164
165 static int config_props(AVFilterLink *outlink)
166 {
167     EvalContext *eval = outlink->src->priv;
168     char buf[128];
169
170     outlink->time_base = (AVRational){1, eval->sample_rate};
171     outlink->sample_rate = eval->sample_rate;
172
173     eval->var_values[VAR_S] = eval->sample_rate;
174
175     av_get_channel_layout_string(buf, sizeof(buf), 0, eval->chlayout);
176
177     av_log(outlink->src, AV_LOG_INFO,
178            "sample_rate:%d chlayout:%s duration:%f\n",
179            eval->sample_rate, buf, eval->duration);
180
181     return 0;
182 }
183
184 static int query_formats(AVFilterContext *ctx)
185 {
186     EvalContext *eval = ctx->priv;
187     enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_NONE };
188     int64_t chlayouts[] = { eval->chlayout, -1 };
189     int packing_fmts[] = { AVFILTER_PLANAR, -1 };
190
191     avfilter_set_common_sample_formats (ctx, avfilter_make_format_list(sample_fmts));
192     avfilter_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
193     avfilter_set_common_packing_formats(ctx, avfilter_make_format_list(packing_fmts));
194
195     return 0;
196 }
197
198 static int request_frame(AVFilterLink *outlink)
199 {
200     EvalContext *eval = outlink->src->priv;
201     AVFilterBufferRef *samplesref;
202     int i, j;
203     double t = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
204
205     if (eval->duration >= 0 && t > eval->duration)
206         return AVERROR_EOF;
207
208     samplesref = avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, eval->nb_samples);
209
210     /* evaluate expression for each single sample and for each channel */
211     for (i = 0; i < eval->nb_samples; i++, eval->n++) {
212         eval->var_values[VAR_N] = eval->n;
213         eval->var_values[VAR_T] = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
214
215         for (j = 0; j < eval->nb_channels; j++) {
216             *((double *) samplesref->data[j] + i) =
217                 av_expr_eval(eval->expr[j], eval->var_values, NULL);
218         }
219     }
220
221     samplesref->pts = eval->pts;
222     samplesref->pos = -1;
223     samplesref->audio->sample_rate = eval->sample_rate;
224     eval->pts += eval->nb_samples;
225
226     avfilter_filter_samples(outlink, samplesref);
227
228     return 0;
229 }
230
231 AVFilter avfilter_asrc_aevalsrc = {
232     .name        = "aevalsrc",
233     .description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
234
235     .query_formats = query_formats,
236     .init        = init,
237     .uninit      = uninit,
238     .priv_size   = sizeof(EvalContext),
239
240     .inputs      = (const AVFilterPad[]) {{ .name = NULL}},
241
242     .outputs     = (const AVFilterPad[]) {{ .name = "default",
243                                       .type = AVMEDIA_TYPE_AUDIO,
244                                       .config_props = config_props,
245                                       .request_frame = request_frame, },
246                                     { .name = NULL}},
247 };