ffmpeg/libavfilter/asrc_aevalsrc.c

243 lines
8.0 KiB
C
Raw Normal View History

2011-08-19 01:51:26 +02: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"
#include "libavutil/channel_layout.h"
2011-08-19 01:51:26 +02:00
#include "libavutil/eval.h"
#include "libavutil/opt.h"
2011-12-02 15:33:15 +01:00
#include "libavutil/parseutils.h"
2011-08-19 01:51:26 +02:00
#include "avfilter.h"
#include "audio.h"
2011-08-19 01:51:26 +02:00
#include "internal.h"
static const char * const var_names[] = {
2011-08-19 01:51:26 +02: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;
char *chlayout_str;
2011-08-19 01:51:26 +02:00
int nb_channels;
int64_t pts;
AVExpr **expr;
char *exprs;
2011-08-19 01:51:26 +02:00
int nb_samples; ///< number of samples per requested frame
int64_t duration;
2011-08-19 01:51:26 +02:00
uint64_t n;
double var_values[VAR_VARS_NB];
} EvalContext;
#define OFFSET(x) offsetof(EvalContext, x)
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
2011-08-19 01:51:26 +02:00
static const AVOption aevalsrc_options[]= {
{ "exprs", "set the '|'-separated list of channels expressions", OFFSET(exprs), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
{ "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 },
{ "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), 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 },
{ "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 }
2011-08-19 01:51:26 +02:00
};
AVFILTER_DEFINE_CLASS(aevalsrc);
2011-08-19 01:51:26 +02:00
static av_cold int init(AVFilterContext *ctx)
2011-08-19 01:51:26 +02:00
{
EvalContext *eval = ctx->priv;
char *args1 = av_strdup(eval->exprs);
char *expr, *buf;
int ret;
2011-08-19 01:51:26 +02:00
if (!args1) {
av_log(ctx, AV_LOG_ERROR, "Channels expressions list is empty\n");
ret = eval->exprs ? AVERROR(ENOMEM) : AVERROR(EINVAL);
goto end;
}
2011-08-19 01:51:26 +02:00
/* parse expressions */
buf = args1;
while (expr = av_strtok(buf, "|", &buf)) {
if (!av_dynarray2_add((void **)&eval->expr, &eval->nb_channels, sizeof(*eval->expr), NULL)) {
ret = AVERROR(ENOMEM);
goto end;
}
ret = av_expr_parse(&eval->expr[eval->nb_channels - 1], expr, var_names,
2011-08-19 01:51:26 +02:00
NULL, NULL, NULL, NULL, 0, ctx);
if (ret < 0)
goto end;
}
if (eval->chlayout_str) {
int n;
ret = ff_parse_channel_layout(&eval->chlayout, NULL, eval->chlayout_str, ctx);
if (ret < 0)
goto end;
2011-08-19 01:51:26 +02: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 {
/* guess channel layout from nb expressions/channels */
eval->chlayout = av_get_default_channel_layout(eval->nb_channels);
if (!eval->chlayout && eval->nb_channels <= 0) {
av_log(ctx, AV_LOG_ERROR, "Invalid number of channels '%d' provided\n",
eval->nb_channels);
ret = AVERROR(EINVAL);
goto end;
}
}
2011-08-19 01:51:26 +02:00
if ((ret = ff_parse_sample_rate(&eval->sample_rate, eval->sample_rate_str, ctx)))
goto end;
eval->n = 0;
end:
av_free(args1);
return ret;
}
static av_cold void uninit(AVFilterContext *ctx)
2011-08-19 01:51:26 +02:00
{
EvalContext *eval = ctx->priv;
int i;
for (i = 0; i < eval->nb_channels; i++) {
2011-08-19 01:51:26 +02:00
av_expr_free(eval->expr[i]);
eval->expr[i] = NULL;
}
av_freep(&eval->expr);
2011-08-19 01:51:26 +02: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;
av_get_channel_layout_string(buf, sizeof(buf), 0, eval->chlayout);
av_log(outlink->src, AV_LOG_VERBOSE,
"sample_rate:%d chlayout:%s duration:%"PRId64"\n",
2011-12-02 15:33:15 +01:00
eval->sample_rate, buf, eval->duration);
2011-08-19 01:51:26 +02:00
return 0;
}
static int query_formats(AVFilterContext *ctx)
{
EvalContext *eval = ctx->priv;
static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE };
int64_t chlayouts[] = { eval->chlayout ? eval->chlayout : FF_COUNT2LAYOUT(eval->nb_channels) , -1 };
int sample_rates[] = { eval->sample_rate, -1 };
2011-08-19 01:51:26 +02:00
ff_set_common_formats (ctx, ff_make_format_list(sample_fmts));
Merge remote-tracking branch 'qatar/master' * qatar/master: (26 commits) fate: use diff -b in oneline comparison Add missing version bumps and APIchanges/Changelog entries. lavfi: move buffer management function to a separate file. lavfi: move formats-related functions from default.c to formats.c lavfi: move video-related functions to a separate file. fate: make smjpeg a demux test fate: separate sierra-vmd audio and video tests fate: separate smacker audio and video tests libmp3lame: set supported channel layouts. avconv: automatically insert asyncts when -async is used. avconv: add support for audio filters. lavfi: add asyncts filter. lavfi: add aformat filter lavfi: add an audio buffer sink. lavfi: add an audio buffer source. buffersrc: add av_buffersrc_write_frame(). buffersrc: fix invalid read in uninit if the fifo hasn't been allocated lavfi: rename vsrc_buffer.c to buffersrc.c avfiltergraph: reindent lavfi: add channel layout/sample rate negotiation. ... Conflicts: Changelog doc/APIchanges doc/filters.texi ffmpeg.c ffprobe.c libavcodec/libmp3lame.c libavfilter/Makefile libavfilter/af_aformat.c libavfilter/allfilters.c libavfilter/avfilter.c libavfilter/avfilter.h libavfilter/avfiltergraph.c libavfilter/buffersrc.c libavfilter/defaults.c libavfilter/formats.c libavfilter/src_buffer.c libavfilter/version.h libavfilter/vf_yadif.c libavfilter/vsrc_buffer.c libavfilter/vsrc_buffer.h libavutil/avutil.h tests/fate/audio.mak tests/fate/demux.mak tests/fate/video.mak Merged-by: Michael Niedermayer <michaelni@gmx.at>
2012-05-16 02:27:31 +02:00
ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates));
2011-08-19 01:51:26 +02:00
return 0;
}
static int request_frame(AVFilterLink *outlink)
{
EvalContext *eval = outlink->src->priv;
Merge commit '7e350379f87e7f74420b4813170fe808e2313911' * commit '7e350379f87e7f74420b4813170fe808e2313911': lavfi: switch to AVFrame. Conflicts: doc/filters.texi libavfilter/af_ashowinfo.c libavfilter/audio.c libavfilter/avfilter.c libavfilter/avfilter.h libavfilter/buffersink.c libavfilter/buffersrc.c libavfilter/buffersrc.h libavfilter/f_select.c libavfilter/f_setpts.c libavfilter/fifo.c libavfilter/split.c libavfilter/src_movie.c libavfilter/version.h libavfilter/vf_aspect.c libavfilter/vf_bbox.c libavfilter/vf_blackframe.c libavfilter/vf_delogo.c libavfilter/vf_drawbox.c libavfilter/vf_drawtext.c libavfilter/vf_fade.c libavfilter/vf_fieldorder.c libavfilter/vf_fps.c libavfilter/vf_frei0r.c libavfilter/vf_gradfun.c libavfilter/vf_hqdn3d.c libavfilter/vf_lut.c libavfilter/vf_overlay.c libavfilter/vf_pad.c libavfilter/vf_scale.c libavfilter/vf_showinfo.c libavfilter/vf_transpose.c libavfilter/vf_vflip.c libavfilter/vf_yadif.c libavfilter/video.c libavfilter/vsrc_testsrc.c libavfilter/yadif.h Following are notes about the merge authorship and various technical details. Michael Niedermayer: * Main merge operation, notably avfilter.c and video.c * Switch to AVFrame: - afade - anullsrc - apad - aresample - blackframe - deshake - idet - il - mandelbrot - mptestsrc - noise - setfield - smartblur - tinterlace * various merge changes and fixes in: - ashowinfo - blackdetect - field - fps - select - testsrc - yadif Nicolas George: * Switch to AVFrame: - make rawdec work with refcounted frames. Adapted from commit 759001c534287a96dc96d1e274665feb7059145d by Anton Khirnov. Also, fix the use of || instead of | in a flags check. - make buffer sink and src, audio and video work all together Clément Bœsch: * Switch to AVFrame: - aevalsrc - alphaextract - blend - cellauto - colormatrix - concat - earwax - ebur128 - edgedetect - geq - histeq - histogram - hue - kerndeint - life - movie - mp (with the help of Michael) - overlay - pad - pan - pp - pp - removelogo - sendcmd - showspectrum - showwaves - silencedetect - stereo3d - subtitles - super2xsai - swapuv - thumbnail - tile Hendrik Leppkes: * Switch to AVFrame: - aconvert - amerge - asetnsamples - atempo - biquads Matthieu Bouron: * Switch to AVFrame - alphamerge - decimate - volumedetect Stefano Sabatini: * Switch to AVFrame: - astreamsync - flite - framestep Signed-off-by: Michael Niedermayer <michaelni@gmx.at> Signed-off-by: Nicolas George <nicolas.george@normalesup.org> Signed-off-by: Clément Bœsch <ubitux@gmail.com> Signed-off-by: Hendrik Leppkes <h.leppkes@gmail.com> Signed-off-by: Matthieu Bouron <matthieu.bouron@gmail.com> Signed-off-by: Stefano Sabatini <stefasab@gmail.com> Merged-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-10 01:30:30 +01:00
AVFrame *samplesref;
2011-08-19 01:51:26 +02:00
int i, j;
int64_t t = av_rescale(eval->n, AV_TIME_BASE, eval->sample_rate);
2011-12-02 15:33:15 +01:00
if (eval->duration >= 0 && t >= eval->duration)
2011-12-02 15:33:15 +01:00
return AVERROR_EOF;
2011-08-19 01:51:26 +02:00
Merge commit '7e350379f87e7f74420b4813170fe808e2313911' * commit '7e350379f87e7f74420b4813170fe808e2313911': lavfi: switch to AVFrame. Conflicts: doc/filters.texi libavfilter/af_ashowinfo.c libavfilter/audio.c libavfilter/avfilter.c libavfilter/avfilter.h libavfilter/buffersink.c libavfilter/buffersrc.c libavfilter/buffersrc.h libavfilter/f_select.c libavfilter/f_setpts.c libavfilter/fifo.c libavfilter/split.c libavfilter/src_movie.c libavfilter/version.h libavfilter/vf_aspect.c libavfilter/vf_bbox.c libavfilter/vf_blackframe.c libavfilter/vf_delogo.c libavfilter/vf_drawbox.c libavfilter/vf_drawtext.c libavfilter/vf_fade.c libavfilter/vf_fieldorder.c libavfilter/vf_fps.c libavfilter/vf_frei0r.c libavfilter/vf_gradfun.c libavfilter/vf_hqdn3d.c libavfilter/vf_lut.c libavfilter/vf_overlay.c libavfilter/vf_pad.c libavfilter/vf_scale.c libavfilter/vf_showinfo.c libavfilter/vf_transpose.c libavfilter/vf_vflip.c libavfilter/vf_yadif.c libavfilter/video.c libavfilter/vsrc_testsrc.c libavfilter/yadif.h Following are notes about the merge authorship and various technical details. Michael Niedermayer: * Main merge operation, notably avfilter.c and video.c * Switch to AVFrame: - afade - anullsrc - apad - aresample - blackframe - deshake - idet - il - mandelbrot - mptestsrc - noise - setfield - smartblur - tinterlace * various merge changes and fixes in: - ashowinfo - blackdetect - field - fps - select - testsrc - yadif Nicolas George: * Switch to AVFrame: - make rawdec work with refcounted frames. Adapted from commit 759001c534287a96dc96d1e274665feb7059145d by Anton Khirnov. Also, fix the use of || instead of | in a flags check. - make buffer sink and src, audio and video work all together Clément Bœsch: * Switch to AVFrame: - aevalsrc - alphaextract - blend - cellauto - colormatrix - concat - earwax - ebur128 - edgedetect - geq - histeq - histogram - hue - kerndeint - life - movie - mp (with the help of Michael) - overlay - pad - pan - pp - pp - removelogo - sendcmd - showspectrum - showwaves - silencedetect - stereo3d - subtitles - super2xsai - swapuv - thumbnail - tile Hendrik Leppkes: * Switch to AVFrame: - aconvert - amerge - asetnsamples - atempo - biquads Matthieu Bouron: * Switch to AVFrame - alphamerge - decimate - volumedetect Stefano Sabatini: * Switch to AVFrame: - astreamsync - flite - framestep Signed-off-by: Michael Niedermayer <michaelni@gmx.at> Signed-off-by: Nicolas George <nicolas.george@normalesup.org> Signed-off-by: Clément Bœsch <ubitux@gmail.com> Signed-off-by: Hendrik Leppkes <h.leppkes@gmail.com> Signed-off-by: Matthieu Bouron <matthieu.bouron@gmail.com> Signed-off-by: Stefano Sabatini <stefasab@gmail.com> Merged-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-10 01:30:30 +01:00
samplesref = ff_get_audio_buffer(outlink, eval->nb_samples);
if (!samplesref)
return AVERROR(ENOMEM);
2011-08-19 01:51:26 +02: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++) {
*((double *) samplesref->extended_data[j] + i) =
2011-08-19 01:51:26 +02:00
av_expr_eval(eval->expr[j], eval->var_values, NULL);
}
}
samplesref->pts = eval->pts;
Merge commit '7e350379f87e7f74420b4813170fe808e2313911' * commit '7e350379f87e7f74420b4813170fe808e2313911': lavfi: switch to AVFrame. Conflicts: doc/filters.texi libavfilter/af_ashowinfo.c libavfilter/audio.c libavfilter/avfilter.c libavfilter/avfilter.h libavfilter/buffersink.c libavfilter/buffersrc.c libavfilter/buffersrc.h libavfilter/f_select.c libavfilter/f_setpts.c libavfilter/fifo.c libavfilter/split.c libavfilter/src_movie.c libavfilter/version.h libavfilter/vf_aspect.c libavfilter/vf_bbox.c libavfilter/vf_blackframe.c libavfilter/vf_delogo.c libavfilter/vf_drawbox.c libavfilter/vf_drawtext.c libavfilter/vf_fade.c libavfilter/vf_fieldorder.c libavfilter/vf_fps.c libavfilter/vf_frei0r.c libavfilter/vf_gradfun.c libavfilter/vf_hqdn3d.c libavfilter/vf_lut.c libavfilter/vf_overlay.c libavfilter/vf_pad.c libavfilter/vf_scale.c libavfilter/vf_showinfo.c libavfilter/vf_transpose.c libavfilter/vf_vflip.c libavfilter/vf_yadif.c libavfilter/video.c libavfilter/vsrc_testsrc.c libavfilter/yadif.h Following are notes about the merge authorship and various technical details. Michael Niedermayer: * Main merge operation, notably avfilter.c and video.c * Switch to AVFrame: - afade - anullsrc - apad - aresample - blackframe - deshake - idet - il - mandelbrot - mptestsrc - noise - setfield - smartblur - tinterlace * various merge changes and fixes in: - ashowinfo - blackdetect - field - fps - select - testsrc - yadif Nicolas George: * Switch to AVFrame: - make rawdec work with refcounted frames. Adapted from commit 759001c534287a96dc96d1e274665feb7059145d by Anton Khirnov. Also, fix the use of || instead of | in a flags check. - make buffer sink and src, audio and video work all together Clément Bœsch: * Switch to AVFrame: - aevalsrc - alphaextract - blend - cellauto - colormatrix - concat - earwax - ebur128 - edgedetect - geq - histeq - histogram - hue - kerndeint - life - movie - mp (with the help of Michael) - overlay - pad - pan - pp - pp - removelogo - sendcmd - showspectrum - showwaves - silencedetect - stereo3d - subtitles - super2xsai - swapuv - thumbnail - tile Hendrik Leppkes: * Switch to AVFrame: - aconvert - amerge - asetnsamples - atempo - biquads Matthieu Bouron: * Switch to AVFrame - alphamerge - decimate - volumedetect Stefano Sabatini: * Switch to AVFrame: - astreamsync - flite - framestep Signed-off-by: Michael Niedermayer <michaelni@gmx.at> Signed-off-by: Nicolas George <nicolas.george@normalesup.org> Signed-off-by: Clément Bœsch <ubitux@gmail.com> Signed-off-by: Hendrik Leppkes <h.leppkes@gmail.com> Signed-off-by: Matthieu Bouron <matthieu.bouron@gmail.com> Signed-off-by: Stefano Sabatini <stefasab@gmail.com> Merged-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-10 01:30:30 +01:00
samplesref->sample_rate = eval->sample_rate;
2011-08-19 01:51:26 +02:00
eval->pts += eval->nb_samples;
return ff_filter_frame(outlink, samplesref);
2011-08-19 01:51:26 +02:00
}
static const AVFilterPad aevalsrc_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_AUDIO,
.config_props = config_props,
.request_frame = request_frame,
},
{ NULL }
};
AVFilter ff_asrc_aevalsrc = {
.name = "aevalsrc",
.description = NULL_IF_CONFIG_SMALL("Generate an audio signal generated by an expression."),
2011-08-19 01:51:26 +02:00
.query_formats = query_formats,
.init = init,
.uninit = uninit,
.priv_size = sizeof(EvalContext),
.inputs = NULL,
.outputs = aevalsrc_outputs,
.priv_class = &aevalsrc_class,
2011-08-19 01:51:26 +02:00
};