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