2011-08-19 02:51:26 +03: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 16:16:48 +03:00
|
|
|
#include "libavutil/channel_layout.h"
|
2011-08-19 02:51:26 +03:00
|
|
|
#include "libavutil/eval.h"
|
|
|
|
#include "libavutil/opt.h"
|
2011-12-02 17:33:15 +03:00
|
|
|
#include "libavutil/parseutils.h"
|
2011-08-19 02:51:26 +03:00
|
|
|
#include "avfilter.h"
|
2012-05-12 18:58:14 +03:00
|
|
|
#include "audio.h"
|
2011-08-19 02:51:26 +03:00
|
|
|
#include "internal.h"
|
|
|
|
|
2011-11-05 16:49:52 +03:00
|
|
|
static const char * const var_names[] = {
|
2013-12-05 20:23:06 +03:00
|
|
|
"ch", ///< the value of the current channel
|
2011-08-19 02:51:26 +03:00
|
|
|
"n", ///< number of frame
|
2013-12-05 20:23:06 +03:00
|
|
|
"nb_in_channels",
|
|
|
|
"nb_out_channels",
|
2011-08-19 02:51:26 +03:00
|
|
|
"t", ///< timestamp expressed in seconds
|
|
|
|
"s", ///< sample rate
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum var_name {
|
2013-12-05 20:23:06 +03:00
|
|
|
VAR_CH,
|
2011-08-19 02:51:26 +03:00
|
|
|
VAR_N,
|
2013-12-05 20:23:06 +03:00
|
|
|
VAR_NB_IN_CHANNELS,
|
|
|
|
VAR_NB_OUT_CHANNELS,
|
2011-08-19 02:51:26 +03:00
|
|
|
VAR_T,
|
|
|
|
VAR_S,
|
|
|
|
VAR_VARS_NB
|
|
|
|
};
|
|
|
|
|
2017-05-12 20:00:49 +02:00
|
|
|
typedef struct EvalContext {
|
2011-08-19 02:51:26 +03:00
|
|
|
const AVClass *class;
|
|
|
|
char *sample_rate_str;
|
|
|
|
int sample_rate;
|
|
|
|
int64_t chlayout;
|
2012-05-17 14:01:50 +03:00
|
|
|
char *chlayout_str;
|
2013-12-05 20:23:06 +03:00
|
|
|
int nb_channels; ///< number of output channels
|
|
|
|
int nb_in_channels; ///< number of input channels
|
|
|
|
int same_chlayout; ///< set output as input channel layout
|
2011-08-19 02:51:26 +03:00
|
|
|
int64_t pts;
|
2013-07-12 04:23:02 +03:00
|
|
|
AVExpr **expr;
|
2013-04-11 17:49:33 +03:00
|
|
|
char *exprs;
|
2011-08-19 02:51:26 +03:00
|
|
|
int nb_samples; ///< number of samples per requested frame
|
2013-07-10 21:42:47 +03:00
|
|
|
int64_t duration;
|
2011-08-19 02:51:26 +03:00
|
|
|
uint64_t n;
|
|
|
|
double var_values[VAR_VARS_NB];
|
2013-12-05 20:23:06 +03:00
|
|
|
double *channel_values;
|
|
|
|
int64_t out_channel_layout;
|
2011-08-19 02:51:26 +03:00
|
|
|
} EvalContext;
|
|
|
|
|
2013-12-05 20:23:06 +03:00
|
|
|
static double val(void *priv, double ch)
|
|
|
|
{
|
|
|
|
EvalContext *eval = priv;
|
|
|
|
return eval->channel_values[FFMIN((int)ch, eval->nb_in_channels-1)];
|
|
|
|
}
|
|
|
|
|
|
|
|
static double (* const aeval_func1[])(void *, double) = { val, NULL };
|
|
|
|
static const char * const aeval_func1_names[] = { "val", NULL };
|
|
|
|
|
2011-08-19 02:51:26 +03:00
|
|
|
#define OFFSET(x) offsetof(EvalContext, x)
|
2012-08-13 14:40:01 +03:00
|
|
|
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
2011-08-19 02:51:26 +03:00
|
|
|
|
2012-06-22 15:33:09 +03:00
|
|
|
static const AVOption aevalsrc_options[]= {
|
2013-04-11 17:49:33 +03:00
|
|
|
{ "exprs", "set the '|'-separated list of channels expressions", OFFSET(exprs), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
|
2012-09-05 15:26:01 +03: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 },
|
2020-03-17 23:46:36 +02:00
|
|
|
{ "sample_rate", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS },
|
|
|
|
{ "s", "set the sample rate", OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS },
|
2013-07-10 21:42:47 +03:00
|
|
|
{ "duration", "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
|
|
|
|
{ "d", "set audio duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
|
2012-08-13 14:40:01 +03:00
|
|
|
{ "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 },
|
2013-09-07 15:13:50 +03:00
|
|
|
{ NULL }
|
2011-08-19 02:51:26 +03:00
|
|
|
};
|
|
|
|
|
2012-06-22 15:33:09 +03:00
|
|
|
AVFILTER_DEFINE_CLASS(aevalsrc);
|
2011-08-19 02:51:26 +03:00
|
|
|
|
2013-12-05 20:23:06 +03:00
|
|
|
static int parse_channel_expressions(AVFilterContext *ctx,
|
|
|
|
int expected_nb_channels)
|
2011-08-19 02:51:26 +03:00
|
|
|
{
|
|
|
|
EvalContext *eval = ctx->priv;
|
2013-04-11 17:49:33 +03:00
|
|
|
char *args1 = av_strdup(eval->exprs);
|
2014-05-07 17:14:44 +03:00
|
|
|
char *expr, *last_expr = NULL, *buf;
|
2013-12-05 20:23:06 +03:00
|
|
|
double (* const *func1)(void *, double) = NULL;
|
|
|
|
const char * const *func1_names = NULL;
|
|
|
|
int i, ret = 0;
|
|
|
|
|
|
|
|
if (!args1)
|
|
|
|
return AVERROR(ENOMEM);
|
2011-08-19 02:51:26 +03:00
|
|
|
|
2013-12-05 20:23:06 +03:00
|
|
|
if (!eval->exprs) {
|
2013-04-11 17:49:33 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Channels expressions list is empty\n");
|
2013-12-05 20:23:06 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(ctx->filter->name, "aeval")) {
|
|
|
|
func1 = aeval_func1;
|
|
|
|
func1_names = aeval_func1_names;
|
2012-10-11 04:00:34 +03:00
|
|
|
}
|
|
|
|
|
2013-12-06 12:26:11 +03:00
|
|
|
#define ADD_EXPRESSION(expr_) do { \
|
|
|
|
if (!av_dynarray2_add((void **)&eval->expr, &eval->nb_channels, \
|
|
|
|
sizeof(*eval->expr), NULL)) { \
|
|
|
|
ret = AVERROR(ENOMEM); \
|
|
|
|
goto end; \
|
|
|
|
} \
|
|
|
|
eval->expr[eval->nb_channels-1] = NULL; \
|
|
|
|
ret = av_expr_parse(&eval->expr[eval->nb_channels - 1], expr_, \
|
2013-12-05 20:23:06 +03:00
|
|
|
var_names, func1_names, func1, \
|
2013-12-06 12:26:11 +03:00
|
|
|
NULL, NULL, 0, ctx); \
|
|
|
|
if (ret < 0) \
|
|
|
|
goto end; \
|
|
|
|
} while (0)
|
|
|
|
|
2013-12-05 20:23:06 +03:00
|
|
|
/* reset expressions */
|
|
|
|
for (i = 0; i < eval->nb_channels; i++) {
|
|
|
|
av_expr_free(eval->expr[i]);
|
|
|
|
eval->expr[i] = NULL;
|
|
|
|
}
|
|
|
|
av_freep(&eval->expr);
|
|
|
|
eval->nb_channels = 0;
|
|
|
|
|
2011-08-19 02:51:26 +03:00
|
|
|
buf = args1;
|
2013-07-12 04:23:02 +03:00
|
|
|
while (expr = av_strtok(buf, "|", &buf)) {
|
2013-12-06 12:26:11 +03:00
|
|
|
ADD_EXPRESSION(expr);
|
|
|
|
last_expr = expr;
|
2011-08-19 02:51:26 +03:00
|
|
|
}
|
2012-05-17 14:01:50 +03:00
|
|
|
|
2013-12-05 20:23:06 +03:00
|
|
|
if (expected_nb_channels > eval->nb_channels)
|
|
|
|
for (i = eval->nb_channels; i < expected_nb_channels; i++)
|
|
|
|
ADD_EXPRESSION(last_expr);
|
|
|
|
|
|
|
|
if (expected_nb_channels > 0 && eval->nb_channels != expected_nb_channels) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"Mismatch between the specified number of channel expressions '%d' "
|
|
|
|
"and the number of expected output channels '%d' for the specified channel layout\n",
|
|
|
|
eval->nb_channels, expected_nb_channels);
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
av_free(args1);
|
|
|
|
return ret;
|
|
|
|
}
|
2013-12-06 12:26:11 +03:00
|
|
|
|
2013-12-05 20:23:06 +03:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
EvalContext *eval = ctx->priv;
|
2013-12-11 17:31:32 +03:00
|
|
|
int ret = 0;
|
2013-12-05 20:23:06 +03:00
|
|
|
|
|
|
|
if (eval->chlayout_str) {
|
|
|
|
if (!strcmp(eval->chlayout_str, "same") && !strcmp(ctx->filter->name, "aeval")) {
|
|
|
|
eval->same_chlayout = 1;
|
|
|
|
} else {
|
|
|
|
ret = ff_parse_channel_layout(&eval->chlayout, NULL, eval->chlayout_str, ctx);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = parse_channel_expressions(ctx, av_get_channel_layout_nb_channels(eval->chlayout));
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-05-17 14:01:50 +03:00
|
|
|
}
|
|
|
|
} else {
|
2012-05-18 01:59:42 +03:00
|
|
|
/* guess channel layout from nb expressions/channels */
|
2013-12-05 20:23:06 +03:00
|
|
|
if ((ret = parse_channel_expressions(ctx, -1)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
2012-05-18 01:59:42 +03:00
|
|
|
eval->chlayout = av_get_default_channel_layout(eval->nb_channels);
|
2013-07-13 22:42:39 +03:00
|
|
|
if (!eval->chlayout && eval->nb_channels <= 0) {
|
2012-05-18 01:59:42 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid number of channels '%d' provided\n",
|
|
|
|
eval->nb_channels);
|
2013-12-05 20:23:06 +03:00
|
|
|
return AVERROR(EINVAL);
|
2012-05-18 01:59:42 +03:00
|
|
|
}
|
2012-05-17 14:01:50 +03:00
|
|
|
}
|
2011-08-19 02:51:26 +03:00
|
|
|
|
2013-12-05 20:23:06 +03:00
|
|
|
if (eval->sample_rate_str)
|
|
|
|
if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
|
|
|
|
return ret;
|
2011-08-19 02:51:26 +03:00
|
|
|
eval->n = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-05-06 17:55:06 +03:00
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
2011-08-19 02:51:26 +03:00
|
|
|
{
|
|
|
|
EvalContext *eval = ctx->priv;
|
|
|
|
int i;
|
|
|
|
|
2013-07-12 04:23:02 +03:00
|
|
|
for (i = 0; i < eval->nb_channels; i++) {
|
2011-08-19 02:51:26 +03:00
|
|
|
av_expr_free(eval->expr[i]);
|
|
|
|
eval->expr[i] = NULL;
|
|
|
|
}
|
2013-07-12 04:23:02 +03:00
|
|
|
av_freep(&eval->expr);
|
2015-03-10 15:12:30 +02:00
|
|
|
av_freep(&eval->channel_values);
|
2011-08-19 02:51:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2013-12-05 20:23:06 +03:00
|
|
|
eval->var_values[VAR_NB_IN_CHANNELS] = NAN;
|
|
|
|
eval->var_values[VAR_NB_OUT_CHANNELS] = outlink->channels;
|
2011-08-19 02:51:26 +03:00
|
|
|
|
|
|
|
av_get_channel_layout_string(buf, sizeof(buf), 0, eval->chlayout);
|
|
|
|
|
2012-07-14 19:26:04 +03:00
|
|
|
av_log(outlink->src, AV_LOG_VERBOSE,
|
2013-07-10 21:42:47 +03:00
|
|
|
"sample_rate:%d chlayout:%s duration:%"PRId64"\n",
|
2011-12-02 17:33:15 +03:00
|
|
|
eval->sample_rate, buf, eval->duration);
|
2011-08-19 02:51:26 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
EvalContext *eval = ctx->priv;
|
2012-12-22 01:38:59 +03:00
|
|
|
static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE };
|
2013-07-13 22:42:39 +03:00
|
|
|
int64_t chlayouts[] = { eval->chlayout ? eval->chlayout : FF_COUNT2LAYOUT(eval->nb_channels) , -1 };
|
2012-05-16 16:28:43 +03:00
|
|
|
int sample_rates[] = { eval->sample_rate, -1 };
|
2015-04-03 19:55:18 +02:00
|
|
|
AVFilterFormats *formats;
|
|
|
|
AVFilterChannelLayouts *layouts;
|
|
|
|
int ret;
|
2011-08-19 02:51:26 +03:00
|
|
|
|
2015-04-03 19:55:18 +02:00
|
|
|
formats = ff_make_format_list(sample_fmts);
|
|
|
|
if (!formats)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ret = ff_set_common_formats (ctx, formats);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2011-08-19 02:51:26 +03:00
|
|
|
|
2015-04-03 19:55:18 +02:00
|
|
|
layouts = avfilter_make_format64_list(chlayouts);
|
|
|
|
if (!layouts)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ret = ff_set_common_channel_layouts(ctx, layouts);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
formats = ff_make_format_list(sample_rates);
|
|
|
|
if (!formats)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
return ff_set_common_samplerates(ctx, formats);
|
2011-08-19 02:51:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int request_frame(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
EvalContext *eval = outlink->src->priv;
|
2013-03-10 03:30:30 +03:00
|
|
|
AVFrame *samplesref;
|
2011-08-19 02:51:26 +03:00
|
|
|
int i, j;
|
2013-07-10 21:42:47 +03:00
|
|
|
int64_t t = av_rescale(eval->n, AV_TIME_BASE, eval->sample_rate);
|
2017-02-24 19:50:09 +02:00
|
|
|
int nb_samples;
|
2011-12-02 17:33:15 +03:00
|
|
|
|
2013-01-12 16:32:03 +03:00
|
|
|
if (eval->duration >= 0 && t >= eval->duration)
|
2011-12-02 17:33:15 +03:00
|
|
|
return AVERROR_EOF;
|
2011-08-19 02:51:26 +03:00
|
|
|
|
2017-02-24 19:50:09 +02:00
|
|
|
if (eval->duration >= 0) {
|
|
|
|
nb_samples = FFMIN(eval->nb_samples, av_rescale(eval->duration, eval->sample_rate, AV_TIME_BASE) - eval->pts);
|
|
|
|
if (!nb_samples)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
} else {
|
|
|
|
nb_samples = eval->nb_samples;
|
|
|
|
}
|
|
|
|
samplesref = ff_get_audio_buffer(outlink, nb_samples);
|
2013-04-08 21:48:16 +03:00
|
|
|
if (!samplesref)
|
|
|
|
return AVERROR(ENOMEM);
|
2011-08-19 02:51:26 +03:00
|
|
|
|
|
|
|
/* evaluate expression for each single sample and for each channel */
|
2017-02-24 19:50:09 +02:00
|
|
|
for (i = 0; i < nb_samples; i++, eval->n++) {
|
2011-08-19 02:51:26 +03:00
|
|
|
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 20:35:56 +03:00
|
|
|
*((double *) samplesref->extended_data[j] + i) =
|
2011-08-19 02:51:26 +03:00
|
|
|
av_expr_eval(eval->expr[j], eval->var_values, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
samplesref->pts = eval->pts;
|
2013-03-10 03:30:30 +03:00
|
|
|
samplesref->sample_rate = eval->sample_rate;
|
2017-02-24 19:50:09 +02:00
|
|
|
eval->pts += nb_samples;
|
2011-08-19 02:51:26 +03:00
|
|
|
|
2013-03-13 11:55:56 +03:00
|
|
|
return ff_filter_frame(outlink, samplesref);
|
2011-08-19 02:51:26 +03:00
|
|
|
}
|
|
|
|
|
2013-12-05 20:23:06 +03:00
|
|
|
#if CONFIG_AEVALSRC_FILTER
|
2012-11-28 22:01:59 +03:00
|
|
|
static const AVFilterPad aevalsrc_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = config_props,
|
|
|
|
.request_frame = request_frame,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-10-29 13:50:56 +03:00
|
|
|
AVFilter ff_asrc_aevalsrc = {
|
2013-09-07 15:13:50 +03:00
|
|
|
.name = "aevalsrc",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
|
2011-08-19 02:51:26 +03:00
|
|
|
.query_formats = query_formats,
|
2013-09-07 15:13:50 +03:00
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.priv_size = sizeof(EvalContext),
|
|
|
|
.inputs = NULL,
|
|
|
|
.outputs = aevalsrc_outputs,
|
|
|
|
.priv_class = &aevalsrc_class,
|
2011-08-19 02:51:26 +03:00
|
|
|
};
|
2013-12-05 20:23:06 +03:00
|
|
|
|
|
|
|
#endif /* CONFIG_AEVALSRC_FILTER */
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(EvalContext, x)
|
|
|
|
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
|
|
|
|
|
|
|
static const AVOption aeval_options[]= {
|
|
|
|
{ "exprs", "set the '|'-separated list of channels expressions", OFFSET(exprs), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = 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 },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(aeval);
|
|
|
|
|
|
|
|
static int aeval_query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AVFilterFormats *formats = NULL;
|
|
|
|
AVFilterChannelLayouts *layouts;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
EvalContext *eval = ctx->priv;
|
|
|
|
static const enum AVSampleFormat sample_fmts[] = {
|
|
|
|
AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE
|
|
|
|
};
|
2015-10-05 05:39:25 +02:00
|
|
|
int ret;
|
2013-12-05 20:23:06 +03:00
|
|
|
|
|
|
|
// inlink supports any channel layout
|
|
|
|
layouts = ff_all_channel_counts();
|
2015-10-05 05:39:25 +02:00
|
|
|
if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
|
|
|
|
return ret;
|
2013-12-05 20:23:06 +03:00
|
|
|
|
|
|
|
if (eval->same_chlayout) {
|
|
|
|
layouts = ff_all_channel_counts();
|
2015-10-05 05:39:25 +02:00
|
|
|
if ((ret = ff_set_common_channel_layouts(ctx, layouts)) < 0)
|
|
|
|
return ret;
|
2013-12-05 20:23:06 +03:00
|
|
|
} else {
|
|
|
|
// outlink supports only requested output channel layout
|
|
|
|
layouts = NULL;
|
2015-10-05 05:39:25 +02:00
|
|
|
if ((ret = ff_add_channel_layout(&layouts,
|
2013-12-05 20:23:06 +03:00
|
|
|
eval->out_channel_layout ? eval->out_channel_layout :
|
2015-10-05 05:39:25 +02:00
|
|
|
FF_COUNT2LAYOUT(eval->nb_channels))) < 0)
|
|
|
|
return ret;
|
|
|
|
if ((ret = ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts)) < 0)
|
|
|
|
return ret;
|
2013-12-05 20:23:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
formats = ff_make_format_list(sample_fmts);
|
2015-10-05 05:39:25 +02:00
|
|
|
if ((ret = ff_set_common_formats(ctx, formats)) < 0)
|
|
|
|
return ret;
|
2013-12-05 20:23:06 +03:00
|
|
|
|
|
|
|
formats = ff_all_samplerates();
|
2015-10-05 05:39:25 +02:00
|
|
|
return ff_set_common_samplerates(ctx, formats);
|
2013-12-05 20:23:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int aeval_config_output(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
EvalContext *eval = ctx->priv;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (eval->same_chlayout) {
|
|
|
|
eval->chlayout = inlink->channel_layout;
|
|
|
|
|
|
|
|
if ((ret = parse_channel_expressions(ctx, inlink->channels)) < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
eval->n = 0;
|
|
|
|
eval->nb_in_channels = eval->var_values[VAR_NB_IN_CHANNELS] = inlink->channels;
|
|
|
|
eval->var_values[VAR_NB_OUT_CHANNELS] = outlink->channels;
|
|
|
|
eval->var_values[VAR_S] = inlink->sample_rate;
|
|
|
|
eval->var_values[VAR_T] = NAN;
|
|
|
|
|
|
|
|
eval->channel_values = av_realloc_f(eval->channel_values,
|
|
|
|
inlink->channels, sizeof(*eval->channel_values));
|
|
|
|
if (!eval->channel_values)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
|
|
|
|
|
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
|
|
|
{
|
|
|
|
EvalContext *eval = inlink->dst->priv;
|
|
|
|
AVFilterLink *outlink = inlink->dst->outputs[0];
|
|
|
|
int nb_samples = in->nb_samples;
|
|
|
|
AVFrame *out;
|
|
|
|
double t0;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
out = ff_get_audio_buffer(outlink, nb_samples);
|
2017-05-12 21:42:21 +02:00
|
|
|
if (!out) {
|
|
|
|
av_frame_free(&in);
|
2013-12-05 20:23:06 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2017-05-12 21:42:21 +02:00
|
|
|
}
|
2013-12-05 20:23:06 +03:00
|
|
|
av_frame_copy_props(out, in);
|
|
|
|
|
|
|
|
t0 = TS2T(in->pts, inlink->time_base);
|
|
|
|
|
|
|
|
/* evaluate expression for each single sample and for each channel */
|
|
|
|
for (i = 0; i < nb_samples; i++, eval->n++) {
|
|
|
|
eval->var_values[VAR_N] = eval->n;
|
|
|
|
eval->var_values[VAR_T] = t0 + i * (double)1/inlink->sample_rate;
|
|
|
|
|
|
|
|
for (j = 0; j < inlink->channels; j++)
|
|
|
|
eval->channel_values[j] = *((double *) in->extended_data[j] + i);
|
|
|
|
|
|
|
|
for (j = 0; j < outlink->channels; j++) {
|
|
|
|
eval->var_values[VAR_CH] = j;
|
|
|
|
*((double *) out->extended_data[j] + i) =
|
|
|
|
av_expr_eval(eval->expr[j], eval->var_values, eval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
av_frame_free(&in);
|
|
|
|
return ff_filter_frame(outlink, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CONFIG_AEVAL_FILTER
|
|
|
|
|
|
|
|
static const AVFilterPad aeval_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad aeval_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = aeval_config_output,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFilter ff_af_aeval = {
|
|
|
|
.name = "aeval",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Filter audio signal according to a specified expression."),
|
|
|
|
.query_formats = aeval_query_formats,
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.priv_size = sizeof(EvalContext),
|
|
|
|
.inputs = aeval_inputs,
|
|
|
|
.outputs = aeval_outputs,
|
|
|
|
.priv_class = &aeval_class,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* CONFIG_AEVAL_FILTER */
|