2011-08-18 23:51:26 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 Stefano Sabatini
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* eval audio source
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/avassert.h"
|
|
|
|
#include "libavutil/avstring.h"
|
2012-11-13 13:16:48 +00:00
|
|
|
#include "libavutil/channel_layout.h"
|
2011-08-18 23:51:26 +00:00
|
|
|
#include "libavutil/eval.h"
|
|
|
|
#include "libavutil/opt.h"
|
2011-12-02 14:33:15 +00:00
|
|
|
#include "libavutil/parseutils.h"
|
2011-08-18 23:51:26 +00:00
|
|
|
#include "avfilter.h"
|
2012-05-12 15:58:14 +00:00
|
|
|
#include "audio.h"
|
2011-08-18 23:51:26 +00:00
|
|
|
#include "internal.h"
|
|
|
|
|
2011-11-05 13:49:52 +00:00
|
|
|
static const char * const var_names[] = {
|
2011-08-18 23:51:26 +00:00
|
|
|
"n", ///< number of frame
|
|
|
|
"t", ///< timestamp expressed in seconds
|
|
|
|
"s", ///< sample rate
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum var_name {
|
|
|
|
VAR_N,
|
|
|
|
VAR_T,
|
|
|
|
VAR_S,
|
|
|
|
VAR_VARS_NB
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const AVClass *class;
|
|
|
|
char *sample_rate_str;
|
|
|
|
int sample_rate;
|
|
|
|
int64_t chlayout;
|
2012-05-17 11:01:50 +00:00
|
|
|
char *chlayout_str;
|
2011-08-18 23:51:26 +00:00
|
|
|
int nb_channels;
|
|
|
|
int64_t pts;
|
|
|
|
AVExpr *expr[8];
|
|
|
|
char *expr_str[8];
|
|
|
|
int nb_samples; ///< number of samples per requested frame
|
2011-12-02 14:33:15 +00:00
|
|
|
char *duration_str; ///< total duration of the generated audio
|
|
|
|
double duration;
|
2011-08-18 23:51:26 +00:00
|
|
|
uint64_t n;
|
|
|
|
double var_values[VAR_VARS_NB];
|
|
|
|
} EvalContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(EvalContext, x)
|
2012-08-13 11:40:01 +00:00
|
|
|
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
2011-08-18 23:51:26 +00:00
|
|
|
|
2012-06-22 12:33:09 +00:00
|
|
|
static const AVOption aevalsrc_options[]= {
|
2012-09-05 12:26:01 +00:00
|
|
|
{ "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
|
|
|
|
{ "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
|
2012-08-13 11:40:01 +00:00
|
|
|
{ "sample_rate", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, CHAR_MIN, CHAR_MAX, FLAGS },
|
|
|
|
{ "s", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, CHAR_MIN, CHAR_MAX, FLAGS },
|
|
|
|
{ "duration", "set audio duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
|
|
|
|
{ "d", "set audio duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
|
|
|
|
{ "channel_layout", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
|
|
|
|
{ "c", "set channel layout", OFFSET(chlayout_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
|
2011-08-18 23:51:26 +00:00
|
|
|
{NULL},
|
|
|
|
};
|
|
|
|
|
2012-06-22 12:33:09 +00:00
|
|
|
AVFILTER_DEFINE_CLASS(aevalsrc);
|
2011-08-18 23:51:26 +00:00
|
|
|
|
2012-06-26 21:27:59 +00:00
|
|
|
static int init(AVFilterContext *ctx, const char *args)
|
2011-08-18 23:51:26 +00:00
|
|
|
{
|
|
|
|
EvalContext *eval = ctx->priv;
|
|
|
|
char *args1 = av_strdup(args);
|
|
|
|
char *expr, *buf, *bufptr;
|
|
|
|
int ret, i;
|
|
|
|
|
2012-06-22 12:33:09 +00:00
|
|
|
eval->class = &aevalsrc_class;
|
2011-08-18 23:51:26 +00:00
|
|
|
av_opt_set_defaults(eval);
|
|
|
|
|
2012-10-11 01:00:34 +00:00
|
|
|
if (!args1) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Argument is empty\n");
|
|
|
|
ret = args ? AVERROR(ENOMEM) : AVERROR(EINVAL);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2011-08-18 23:51:26 +00:00
|
|
|
/* parse expressions */
|
|
|
|
buf = args1;
|
|
|
|
i = 0;
|
2011-10-18 16:43:44 +00:00
|
|
|
while (expr = av_strtok(buf, ":", &bufptr)) {
|
2011-08-18 23:51:26 +00:00
|
|
|
ret = av_expr_parse(&eval->expr[i], expr, var_names,
|
|
|
|
NULL, NULL, NULL, NULL, 0, ctx);
|
|
|
|
if (ret < 0)
|
|
|
|
goto end;
|
|
|
|
i++;
|
|
|
|
if (bufptr && *bufptr == ':') { /* found last expression */
|
|
|
|
bufptr++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf = NULL;
|
|
|
|
}
|
2012-05-17 11:01:50 +00:00
|
|
|
eval->nb_channels = i;
|
|
|
|
|
|
|
|
if (bufptr && (ret = av_set_options_string(eval, bufptr, "=", ":")) < 0)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
if (eval->chlayout_str) {
|
|
|
|
int n;
|
|
|
|
ret = ff_parse_channel_layout(&eval->chlayout, eval->chlayout_str, ctx);
|
|
|
|
if (ret < 0)
|
|
|
|
goto end;
|
2011-08-18 23:51:26 +00:00
|
|
|
|
2012-05-17 11:01:50 +00:00
|
|
|
n = av_get_channel_layout_nb_channels(eval->chlayout);
|
|
|
|
if (n != eval->nb_channels) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Mismatch between the specified number of channels '%d' "
|
|
|
|
"and the number of channels '%d' in the specified channel layout '%s'\n",
|
|
|
|
eval->nb_channels, n, eval->chlayout_str);
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
2012-05-17 22:59:42 +00:00
|
|
|
/* guess channel layout from nb expressions/channels */
|
|
|
|
eval->chlayout = av_get_default_channel_layout(eval->nb_channels);
|
|
|
|
if (!eval->chlayout) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid number of channels '%d' provided\n",
|
|
|
|
eval->nb_channels);
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto end;
|
|
|
|
}
|
2012-05-17 11:01:50 +00:00
|
|
|
}
|
2011-08-18 23:51:26 +00:00
|
|
|
|
|
|
|
if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
|
|
|
|
goto end;
|
2011-12-02 14:33:15 +00:00
|
|
|
|
|
|
|
eval->duration = -1;
|
|
|
|
if (eval->duration_str) {
|
|
|
|
int64_t us = -1;
|
|
|
|
if ((ret = av_parse_time(&us, eval->duration_str, 1)) < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", eval->duration_str);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
eval->duration = (double)us / 1000000;
|
|
|
|
}
|
2011-08-18 23:51:26 +00:00
|
|
|
eval->n = 0;
|
|
|
|
|
|
|
|
end:
|
|
|
|
av_free(args1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
EvalContext *eval = ctx->priv;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
av_expr_free(eval->expr[i]);
|
|
|
|
eval->expr[i] = NULL;
|
|
|
|
}
|
2012-05-17 11:01:50 +00:00
|
|
|
av_freep(&eval->chlayout_str);
|
2011-12-02 14:33:15 +00:00
|
|
|
av_freep(&eval->duration_str);
|
2011-08-18 23:51:26 +00:00
|
|
|
av_freep(&eval->sample_rate_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_props(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
EvalContext *eval = outlink->src->priv;
|
|
|
|
char buf[128];
|
|
|
|
|
|
|
|
outlink->time_base = (AVRational){1, eval->sample_rate};
|
|
|
|
outlink->sample_rate = eval->sample_rate;
|
|
|
|
|
|
|
|
eval->var_values[VAR_S] = eval->sample_rate;
|
|
|
|
|
|
|
|
av_get_channel_layout_string(buf, sizeof(buf), 0, eval->chlayout);
|
|
|
|
|
2012-07-14 16:26:04 +00:00
|
|
|
av_log(outlink->src, AV_LOG_VERBOSE,
|
2011-12-02 14:33:15 +00:00
|
|
|
"sample_rate:%d chlayout:%s duration:%f\n",
|
|
|
|
eval->sample_rate, buf, eval->duration);
|
2011-08-18 23:51:26 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
EvalContext *eval = ctx->priv;
|
2012-05-16 14:17:41 +00:00
|
|
|
enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE };
|
2011-08-18 23:51:26 +00:00
|
|
|
int64_t chlayouts[] = { eval->chlayout, -1 };
|
2012-05-16 13:28:43 +00:00
|
|
|
int sample_rates[] = { eval->sample_rate, -1 };
|
2011-08-18 23:51:26 +00:00
|
|
|
|
2012-06-16 09:47:46 +00:00
|
|
|
ff_set_common_formats (ctx, ff_make_format_list(sample_fmts));
|
2012-05-16 00:27:31 +00:00
|
|
|
ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
|
2012-06-16 09:47:46 +00:00
|
|
|
ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates));
|
2011-08-18 23:51:26 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int request_frame(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
EvalContext *eval = outlink->src->priv;
|
|
|
|
AVFilterBufferRef *samplesref;
|
|
|
|
int i, j;
|
2011-12-02 14:33:15 +00:00
|
|
|
double t = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
|
|
|
|
|
|
|
|
if (eval->duration >= 0 && t > eval->duration)
|
|
|
|
return AVERROR_EOF;
|
2011-08-18 23:51:26 +00:00
|
|
|
|
2012-05-10 20:41:29 +00:00
|
|
|
samplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, eval->nb_samples);
|
2011-08-18 23:51:26 +00:00
|
|
|
|
|
|
|
/* evaluate expression for each single sample and for each channel */
|
|
|
|
for (i = 0; i < eval->nb_samples; i++, eval->n++) {
|
|
|
|
eval->var_values[VAR_N] = eval->n;
|
|
|
|
eval->var_values[VAR_T] = eval->var_values[VAR_N] * (double)1/eval->sample_rate;
|
|
|
|
|
|
|
|
for (j = 0; j < eval->nb_channels; j++) {
|
2012-05-16 17:35:56 +00:00
|
|
|
*((double *) samplesref->extended_data[j] + i) =
|
2011-08-18 23:51:26 +00:00
|
|
|
av_expr_eval(eval->expr[j], eval->var_values, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
samplesref->pts = eval->pts;
|
|
|
|
samplesref->pos = -1;
|
|
|
|
samplesref->audio->sample_rate = eval->sample_rate;
|
|
|
|
eval->pts += eval->nb_samples;
|
|
|
|
|
2012-11-28 12:53:48 +00:00
|
|
|
ff_filter_frame(outlink, samplesref);
|
2011-08-18 23:51:26 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVFilter avfilter_asrc_aevalsrc = {
|
|
|
|
.name = "aevalsrc",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
|
|
|
|
|
|
|
|
.query_formats = query_formats,
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.priv_size = sizeof(EvalContext),
|
|
|
|
|
2011-11-05 13:39:24 +00:00
|
|
|
.inputs = (const AVFilterPad[]) {{ .name = NULL}},
|
2011-08-18 23:51:26 +00:00
|
|
|
|
2011-11-05 13:39:24 +00:00
|
|
|
.outputs = (const AVFilterPad[]) {{ .name = "default",
|
2011-08-18 23:51:26 +00:00
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = config_props,
|
|
|
|
.request_frame = request_frame, },
|
|
|
|
{ .name = NULL}},
|
2012-08-13 11:40:01 +00:00
|
|
|
.priv_class = &aevalsrc_class,
|
2011-08-18 23:51:26 +00:00
|
|
|
};
|