1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-11-26 19:01:44 +02:00
FFmpeg/libavfilter/af_amultiply.c
Andreas Rheinhardt 8be701d9f7 avfilter/avfilter: Add numbers of (in|out)pads directly to AVFilter
Up until now, an AVFilter's lists of input and output AVFilterPads
were terminated by a sentinel and the only way to get the length
of these lists was by using avfilter_pad_count(). This has two
drawbacks: first, sizeof(AVFilterPad) is not negligible
(i.e. 64B on 64bit systems); second, getting the size involves
a function call instead of just reading the data.

This commit therefore changes this. The sentinels are removed and new
private fields nb_inputs and nb_outputs are added to AVFilter that
contain the number of elements of the respective AVFilterPad array.

Given that AVFilter.(in|out)puts are the only arrays of zero-terminated
AVFilterPads an API user has access to (AVFilterContext.(in|out)put_pads
are not zero-terminated and they already have a size field) the argument
to avfilter_pad_count() is always one of these lists, so it just has to
find the filter the list belongs to and read said number. This is slower
than before, but a replacement function that just reads the internal numbers
that users are expected to switch to will be added soon; and furthermore,
avfilter_pad_count() is probably never called in hot loops anyway.

This saves about 49KiB from the binary; notice that these sentinels are
not in .bss despite being zeroed: they are in .data.rel.ro due to the
non-sentinels.

Reviewed-by: Nicolas George <george@nsup.org>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-08-20 12:53:58 +02:00

203 lines
5.8 KiB
C

/*
* Copyright (c) 2018 Paul B Mahol
*
* 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
*/
#include "libavutil/channel_layout.h"
#include "libavutil/common.h"
#include "libavutil/float_dsp.h"
#include "libavutil/opt.h"
#include "audio.h"
#include "avfilter.h"
#include "formats.h"
#include "filters.h"
#include "internal.h"
typedef struct AudioMultiplyContext {
const AVClass *class;
AVFrame *frames[2];
int planes;
int channels;
int samples_align;
AVFloatDSPContext *fdsp;
} AudioMultiplyContext;
static int query_formats(AVFilterContext *ctx)
{
static const enum AVSampleFormat sample_fmts[] = {
AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
AV_SAMPLE_FMT_NONE
};
int ret = ff_set_common_all_channel_counts(ctx);
if (ret < 0)
return ret;
ret = ff_set_common_formats_from_list(ctx, sample_fmts);
if (ret < 0)
return ret;
return ff_set_common_all_samplerates(ctx);
}
static int activate(AVFilterContext *ctx)
{
AudioMultiplyContext *s = ctx->priv;
int i, ret, status;
int nb_samples;
int64_t pts;
FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[0]),
ff_inlink_queued_samples(ctx->inputs[1]));
for (i = 0; i < ctx->nb_inputs && nb_samples > 0; i++) {
if (s->frames[i])
continue;
if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) {
ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->frames[i]);
if (ret < 0)
return ret;
}
}
if (s->frames[0] && s->frames[1]) {
AVFrame *out;
int plane_samples;
if (av_sample_fmt_is_planar(ctx->inputs[0]->format))
plane_samples = FFALIGN(s->frames[0]->nb_samples, s->samples_align);
else
plane_samples = FFALIGN(s->frames[0]->nb_samples * s->channels, s->samples_align);
out = ff_get_audio_buffer(ctx->outputs[0], s->frames[0]->nb_samples);
if (!out)
return AVERROR(ENOMEM);
out->pts = s->frames[0]->pts;
if (av_get_packed_sample_fmt(ctx->inputs[0]->format) == AV_SAMPLE_FMT_FLT) {
for (i = 0; i < s->planes; i++) {
s->fdsp->vector_fmul((float *)out->extended_data[i],
(const float *)s->frames[0]->extended_data[i],
(const float *)s->frames[1]->extended_data[i],
plane_samples);
}
} else {
for (i = 0; i < s->planes; i++) {
s->fdsp->vector_dmul((double *)out->extended_data[i],
(const double *)s->frames[0]->extended_data[i],
(const double *)s->frames[1]->extended_data[i],
plane_samples);
}
}
emms_c();
av_frame_free(&s->frames[0]);
av_frame_free(&s->frames[1]);
ret = ff_filter_frame(ctx->outputs[0], out);
if (ret < 0)
return ret;
}
if (!nb_samples) {
for (i = 0; i < 2; i++) {
if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
ff_outlink_set_status(ctx->outputs[0], status, pts);
return 0;
}
}
}
if (ff_outlink_frame_wanted(ctx->outputs[0])) {
for (i = 0; i < 2; i++) {
if (ff_inlink_queued_samples(ctx->inputs[i]) > 0)
continue;
ff_inlink_request_frame(ctx->inputs[i]);
return 0;
}
}
return 0;
}
static int config_output(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
AudioMultiplyContext *s = ctx->priv;
AVFilterLink *inlink = ctx->inputs[0];
s->channels = inlink->channels;
s->planes = av_sample_fmt_is_planar(inlink->format) ? inlink->channels : 1;
s->samples_align = 16;
return 0;
}
static av_cold int init(AVFilterContext *ctx)
{
AudioMultiplyContext *s = ctx->priv;
s->fdsp = avpriv_float_dsp_alloc(0);
if (!s->fdsp)
return AVERROR(ENOMEM);
return 0;
}
static av_cold void uninit(AVFilterContext *ctx)
{
AudioMultiplyContext *s = ctx->priv;
av_freep(&s->fdsp);
}
static const AVFilterPad inputs[] = {
{
.name = "multiply0",
.type = AVMEDIA_TYPE_AUDIO,
},
{
.name = "multiply1",
.type = AVMEDIA_TYPE_AUDIO,
},
};
static const AVFilterPad outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_AUDIO,
.config_props = config_output,
},
};
const AVFilter ff_af_amultiply = {
.name = "amultiply",
.description = NULL_IF_CONFIG_SMALL("Multiply two audio streams."),
.priv_size = sizeof(AudioMultiplyContext),
.init = init,
.uninit = uninit,
.activate = activate,
.query_formats = query_formats,
FILTER_INPUTS(inputs),
FILTER_OUTPUTS(outputs),
};