00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "libavutil/avassert.h"
00027 #include "libavutil/avstring.h"
00028 #include "libavutil/channel_layout.h"
00029 #include "libavutil/eval.h"
00030 #include "libavutil/opt.h"
00031 #include "libavutil/parseutils.h"
00032 #include "avfilter.h"
00033 #include "audio.h"
00034 #include "internal.h"
00035
00036 static const char * const var_names[] = {
00037 "n",
00038 "t",
00039 "s",
00040 NULL
00041 };
00042
00043 enum var_name {
00044 VAR_N,
00045 VAR_T,
00046 VAR_S,
00047 VAR_VARS_NB
00048 };
00049
00050 typedef struct {
00051 const AVClass *class;
00052 char *sample_rate_str;
00053 int sample_rate;
00054 int64_t chlayout;
00055 char *chlayout_str;
00056 int nb_channels;
00057 int64_t pts;
00058 AVExpr *expr[8];
00059 char *expr_str[8];
00060 int nb_samples;
00061 char *duration_str;
00062 double duration;
00063 uint64_t n;
00064 double var_values[VAR_VARS_NB];
00065 } EvalContext;
00066
00067 #define OFFSET(x) offsetof(EvalContext, x)
00068 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
00069
00070 static const AVOption aevalsrc_options[]= {
00071 { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
00072 { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
00073 { "sample_rate", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, CHAR_MIN, CHAR_MAX, FLAGS },
00074 { "s", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, CHAR_MIN, CHAR_MAX, FLAGS },
00075 { "duration", "set audio duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
00076 { "d", "set audio duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
00077 { "channel_layout", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
00078 { "c", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
00079 {NULL},
00080 };
00081
00082 AVFILTER_DEFINE_CLASS(aevalsrc);
00083
00084 static int init(AVFilterContext *ctx, const char *args)
00085 {
00086 EvalContext *eval = ctx->priv;
00087 char *args1 = av_strdup(args);
00088 char *expr, *buf, *bufptr;
00089 int ret, i;
00090
00091 eval->class = &aevalsrc_class;
00092 av_opt_set_defaults(eval);
00093
00094 if (!args1) {
00095 av_log(ctx, AV_LOG_ERROR, "Argument is empty\n");
00096 ret = args ? AVERROR(ENOMEM) : AVERROR(EINVAL);
00097 goto end;
00098 }
00099
00100
00101 buf = args1;
00102 i = 0;
00103 while (expr = av_strtok(buf, ":", &bufptr)) {
00104 ret = av_expr_parse(&eval->expr[i], expr, var_names,
00105 NULL, NULL, NULL, NULL, 0, ctx);
00106 if (ret < 0)
00107 goto end;
00108 i++;
00109 if (bufptr && *bufptr == ':') {
00110 bufptr++;
00111 break;
00112 }
00113 buf = NULL;
00114 }
00115 eval->nb_channels = i;
00116
00117 if (bufptr && (ret = av_set_options_string(eval, bufptr, "=", ":")) < 0)
00118 goto end;
00119
00120 if (eval->chlayout_str) {
00121 int n;
00122 ret = ff_parse_channel_layout(&eval->chlayout, eval->chlayout_str, ctx);
00123 if (ret < 0)
00124 goto end;
00125
00126 n = av_get_channel_layout_nb_channels(eval->chlayout);
00127 if (n != eval->nb_channels) {
00128 av_log(ctx, AV_LOG_ERROR,
00129 "Mismatch between the specified number of channels '%d' "
00130 "and the number of channels '%d' in the specified channel layout '%s'\n",
00131 eval->nb_channels, n, eval->chlayout_str);
00132 ret = AVERROR(EINVAL);
00133 goto end;
00134 }
00135 } else {
00136
00137 eval->chlayout = av_get_default_channel_layout(eval->nb_channels);
00138 if (!eval->chlayout) {
00139 av_log(ctx, AV_LOG_ERROR, "Invalid number of channels '%d' provided\n",
00140 eval->nb_channels);
00141 ret = AVERROR(EINVAL);
00142 goto end;
00143 }
00144 }
00145
00146 if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
00147 goto end;
00148
00149 eval->duration = -1;
00150 if (eval->duration_str) {
00151 int64_t us = -1;
00152 if ((ret = av_parse_time(&us, eval->duration_str, 1)) < 0) {
00153 av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", eval->duration_str);
00154 goto end;
00155 }
00156 eval->duration = (double)us / 1000000;
00157 }
00158 eval->n = 0;
00159
00160 end:
00161 av_free(args1);
00162 return ret;
00163 }
00164
00165 static void uninit(AVFilterContext *ctx)
00166 {
00167 EvalContext *eval = ctx->priv;
00168 int i;
00169
00170 for (i = 0; i < 8; i++) {
00171 av_expr_free(eval->expr[i]);
00172 eval->expr[i] = NULL;
00173 }
00174 av_freep(&eval->chlayout_str);
00175 av_freep(&eval->duration_str);
00176 av_freep(&eval->sample_rate_str);
00177 }
00178
00179 static int config_props(AVFilterLink *outlink)
00180 {
00181 EvalContext *eval = outlink->src->priv;
00182 char buf[128];
00183
00184 outlink->time_base = (AVRational){1, eval->sample_rate};
00185 outlink->sample_rate = eval->sample_rate;
00186
00187 eval->var_values[VAR_S] = eval->sample_rate;
00188
00189 av_get_channel_layout_string(buf, sizeof(buf), 0, eval->chlayout);
00190
00191 av_log(outlink->src, AV_LOG_VERBOSE,
00192 "sample_rate:%d chlayout:%s duration:%f\n",
00193 eval->sample_rate, buf, eval->duration);
00194
00195 return 0;
00196 }
00197
00198 static int query_formats(AVFilterContext *ctx)
00199 {
00200 EvalContext *eval = ctx->priv;
00201 enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE };
00202 int64_t chlayouts[] = { eval->chlayout, -1 };
00203 int sample_rates[] = { eval->sample_rate, -1 };
00204
00205 ff_set_common_formats (ctx, ff_make_format_list(sample_fmts));
00206 ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
00207 ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates));
00208
00209 return 0;
00210 }
00211
00212 static int request_frame(AVFilterLink *outlink)
00213 {
00214 EvalContext *eval = outlink->src->priv;
00215 AVFilterBufferRef *samplesref;
00216 int i, j;
00217 double t = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
00218
00219 if (eval->duration >= 0 && t > eval->duration)
00220 return AVERROR_EOF;
00221
00222 samplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, eval->nb_samples);
00223
00224
00225 for (i = 0; i < eval->nb_samples; i++, eval->n++) {
00226 eval->var_values[VAR_N] = eval->n;
00227 eval->var_values[VAR_T] = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
00228
00229 for (j = 0; j < eval->nb_channels; j++) {
00230 *((double *) samplesref->extended_data[j] + i) =
00231 av_expr_eval(eval->expr[j], eval->var_values, NULL);
00232 }
00233 }
00234
00235 samplesref->pts = eval->pts;
00236 samplesref->pos = -1;
00237 samplesref->audio->sample_rate = eval->sample_rate;
00238 eval->pts += eval->nb_samples;
00239
00240 ff_filter_frame(outlink, samplesref);
00241
00242 return 0;
00243 }
00244
00245 static const AVFilterPad aevalsrc_outputs[] = {
00246 {
00247 .name = "default",
00248 .type = AVMEDIA_TYPE_AUDIO,
00249 .config_props = config_props,
00250 .request_frame = request_frame,
00251 },
00252 { NULL }
00253 };
00254
00255 AVFilter avfilter_asrc_aevalsrc = {
00256 .name = "aevalsrc",
00257 .description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
00258
00259 .query_formats = query_formats,
00260 .init = init,
00261 .uninit = uninit,
00262 .priv_size = sizeof(EvalContext),
00263 .inputs = NULL,
00264 .outputs = aevalsrc_outputs,
00265 .priv_class = &aevalsrc_class,
00266 };