00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "libavutil/audioconvert.h"
00027 #include "libavutil/avassert.h"
00028 #include "libavutil/avstring.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
00095 buf = args1;
00096 i = 0;
00097 while (expr = av_strtok(buf, ":", &bufptr)) {
00098 ret = av_expr_parse(&eval->expr[i], expr, var_names,
00099 NULL, NULL, NULL, NULL, 0, ctx);
00100 if (ret < 0)
00101 goto end;
00102 i++;
00103 if (bufptr && *bufptr == ':') {
00104 bufptr++;
00105 break;
00106 }
00107 buf = NULL;
00108 }
00109 eval->nb_channels = i;
00110
00111 if (bufptr && (ret = av_set_options_string(eval, bufptr, "=", ":")) < 0)
00112 goto end;
00113
00114 if (eval->chlayout_str) {
00115 int n;
00116 ret = ff_parse_channel_layout(&eval->chlayout, eval->chlayout_str, ctx);
00117 if (ret < 0)
00118 goto end;
00119
00120 n = av_get_channel_layout_nb_channels(eval->chlayout);
00121 if (n != eval->nb_channels) {
00122 av_log(ctx, AV_LOG_ERROR,
00123 "Mismatch between the specified number of channels '%d' "
00124 "and the number of channels '%d' in the specified channel layout '%s'\n",
00125 eval->nb_channels, n, eval->chlayout_str);
00126 ret = AVERROR(EINVAL);
00127 goto end;
00128 }
00129 } else {
00130
00131 eval->chlayout = av_get_default_channel_layout(eval->nb_channels);
00132 if (!eval->chlayout) {
00133 av_log(ctx, AV_LOG_ERROR, "Invalid number of channels '%d' provided\n",
00134 eval->nb_channels);
00135 ret = AVERROR(EINVAL);
00136 goto end;
00137 }
00138 }
00139
00140 if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
00141 goto end;
00142
00143 eval->duration = -1;
00144 if (eval->duration_str) {
00145 int64_t us = -1;
00146 if ((ret = av_parse_time(&us, eval->duration_str, 1)) < 0) {
00147 av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", eval->duration_str);
00148 goto end;
00149 }
00150 eval->duration = (double)us / 1000000;
00151 }
00152 eval->n = 0;
00153
00154 end:
00155 av_free(args1);
00156 return ret;
00157 }
00158
00159 static void uninit(AVFilterContext *ctx)
00160 {
00161 EvalContext *eval = ctx->priv;
00162 int i;
00163
00164 for (i = 0; i < 8; i++) {
00165 av_expr_free(eval->expr[i]);
00166 eval->expr[i] = NULL;
00167 }
00168 av_freep(&eval->chlayout_str);
00169 av_freep(&eval->duration_str);
00170 av_freep(&eval->sample_rate_str);
00171 }
00172
00173 static int config_props(AVFilterLink *outlink)
00174 {
00175 EvalContext *eval = outlink->src->priv;
00176 char buf[128];
00177
00178 outlink->time_base = (AVRational){1, eval->sample_rate};
00179 outlink->sample_rate = eval->sample_rate;
00180
00181 eval->var_values[VAR_S] = eval->sample_rate;
00182
00183 av_get_channel_layout_string(buf, sizeof(buf), 0, eval->chlayout);
00184
00185 av_log(outlink->src, AV_LOG_VERBOSE,
00186 "sample_rate:%d chlayout:%s duration:%f\n",
00187 eval->sample_rate, buf, eval->duration);
00188
00189 return 0;
00190 }
00191
00192 static int query_formats(AVFilterContext *ctx)
00193 {
00194 EvalContext *eval = ctx->priv;
00195 enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE };
00196 int64_t chlayouts[] = { eval->chlayout, -1 };
00197 int sample_rates[] = { eval->sample_rate, -1 };
00198
00199 ff_set_common_formats (ctx, ff_make_format_list(sample_fmts));
00200 ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
00201 ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates));
00202
00203 return 0;
00204 }
00205
00206 static int request_frame(AVFilterLink *outlink)
00207 {
00208 EvalContext *eval = outlink->src->priv;
00209 AVFilterBufferRef *samplesref;
00210 int i, j;
00211 double t = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
00212
00213 if (eval->duration >= 0 && t > eval->duration)
00214 return AVERROR_EOF;
00215
00216 samplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, eval->nb_samples);
00217
00218
00219 for (i = 0; i < eval->nb_samples; i++, eval->n++) {
00220 eval->var_values[VAR_N] = eval->n;
00221 eval->var_values[VAR_T] = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
00222
00223 for (j = 0; j < eval->nb_channels; j++) {
00224 *((double *) samplesref->extended_data[j] + i) =
00225 av_expr_eval(eval->expr[j], eval->var_values, NULL);
00226 }
00227 }
00228
00229 samplesref->pts = eval->pts;
00230 samplesref->pos = -1;
00231 samplesref->audio->sample_rate = eval->sample_rate;
00232 eval->pts += eval->nb_samples;
00233
00234 ff_filter_samples(outlink, samplesref);
00235
00236 return 0;
00237 }
00238
00239 AVFilter avfilter_asrc_aevalsrc = {
00240 .name = "aevalsrc",
00241 .description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
00242
00243 .query_formats = query_formats,
00244 .init = init,
00245 .uninit = uninit,
00246 .priv_size = sizeof(EvalContext),
00247
00248 .inputs = (const AVFilterPad[]) {{ .name = NULL}},
00249
00250 .outputs = (const AVFilterPad[]) {{ .name = "default",
00251 .type = AVMEDIA_TYPE_AUDIO,
00252 .config_props = config_props,
00253 .request_frame = request_frame, },
00254 { .name = NULL}},
00255 .priv_class = &aevalsrc_class,
00256 };