[FFmpeg-devel] [PATCH] lavfi: reimplement MPlayer's af_pan filter for libavfilter.
Stefano Sabatini
stefasab at gmail.com
Thu Nov 17 01:25:59 CET 2011
On date Wednesday 2011-11-16 14:32:43 +0100, Nicolas George encoded:
> From: Clément Bœsch <ubitux at gmail.com>
At your discretion you may want to change authorship at this point,
and mention Clement in the commit message.
> Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
> ---
> Changelog | 1 +
> doc/filters.texi | 48 ++++++++
> libavfilter/Makefile | 1 +
> libavfilter/af_pan.c | 303 ++++++++++++++++++++++++++++++++++++++++++++++
> libavfilter/allfilters.c | 1 +
> 5 files changed, 354 insertions(+), 0 deletions(-)
> create mode 100644 libavfilter/af_pan.c
>
> diff --git a/Changelog b/Changelog
> index f635e88..561cd0b 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -122,6 +122,7 @@ easier to use. The changes are:
> - VBLE Decoder
> - OS X Video Decoder Acceleration (VDA) support
> - compact output in ffprobe
> +- pan audio filter added
[...]
> diff --git a/libavfilter/af_pan.c b/libavfilter/af_pan.c
> new file mode 100644
> index 0000000..03d4160
> --- /dev/null
> +++ b/libavfilter/af_pan.c
> @@ -0,0 +1,303 @@
> +/*
> + * Copyright (C) 2002 Anders Johansson <ajh at atri.curtin.edu.au>
> + * Copyright (C) 2011 Clément Bœsch <ubitux at gmail.com>
> + * Copyright (C) 2011 Nicolas George <nicolas.george at normalesup.org>
Nit++++: (C) -> (c) for consistency
> + *
> + * 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 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
> + * Audio panning filter (channels mixing)
> + * Original code written by Anders Johansson for MPlayer,
> + * reimplemented for FFmpeg.
> + */
> +
> +#include <stdio.h>
> +#include "libavutil/audioconvert.h"
> +#include "libavutil/avstring.h"
> +#include "avfilter.h"
> +#include "internal.h"
> +
> +#define MAX_CHANNELS 63
> +
> +typedef struct {
> + int64_t out_channel_layout;
> + union {
> + double d[MAX_CHANNELS][MAX_CHANNELS];
> + int i[MAX_CHANNELS][MAX_CHANNELS]; // 1:7:8 fixed point
this comment may be more explicit, or entirely removed
> + } gain;
> + int64_t need_renorm;
> + int need_renumber;
> + int nb_input_channels;
> + int nb_output_channels;
> +} PanContext;
> +
> +static int parse_channel_name(char **arg, int *rchannel, int *rnamed)
> +{
> + char buf[8];
> + int len, i, channel;
please rename channel to something like channel_nb or add a
comment (channel as is suggest a channel layout with a single channel)
> + int64_t layout, layout0;
> +
> + if (sscanf(*arg, " %7[A-Z] %n", buf, &len)) {
> + layout0 = layout = av_get_channel_layout(buf);
> + for (i = 32; i > 0; i >>= 1) {
> + if (layout >= (int64_t)1 << i) {
> + channel += i;
> + layout >>= i;
> + }
> + }
> + if (channel >= MAX_CHANNELS || layout0 != (int64_t)1 << channel)
> + return AVERROR(EINVAL);
> + *rchannel = channel;
> + *rnamed = 1;
> + *arg += len;
> + return 0;
> + }
> + if (sscanf(*arg, " c%d %n", &channel, &len) &&
> + channel >= 0 && channel < MAX_CHANNELS) {
> + *rchannel = channel;
> + *rnamed = 0;
> + *arg += len;
> + return 0;
> + }
> + return AVERROR(EINVAL);
> +}
> +
> +static void skip_spaces(char **arg)
> +{
> + int len = 0;
> +
> + sscanf(*arg, " %n", &len);
> + *arg += len;
> +}
> +
> +static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
> +{
> + PanContext *const pan = ctx->priv;
> + char *arg, *arg0, *tokenizer, *args = av_strdup(args0);
> + int out_ch_id, in_ch_id, len, named;
> + int nb_named[2] = { 0, 0 }; // number of unnamed and named input channels
I'd suggest nb_channels here, "number of named" is quite confusing,
maybe together with #define NAMED 1.
> + double gain;
> +
> + if (!args)
> + return AVERROR(ENOMEM);
> + arg = av_strtok(args, ":", &tokenizer);
> + pan->out_channel_layout = av_get_channel_layout(arg);
> + if (!pan->out_channel_layout) {
> + av_log(ctx, AV_LOG_ERROR, "Unknown channel layout \"%s\"\n", arg);
> + return AVERROR(EINVAL);
> + }
> + pan->nb_output_channels = av_get_channel_layout_nb_channels(pan->out_channel_layout);
> +
> + /* parse channel specifications */
> + while ((arg = arg0 = av_strtok(NULL, ":", &tokenizer))) {
> + /* channel name */
> + if (parse_channel_name(&arg, &out_ch_id, &named)) {
> + av_log(ctx, AV_LOG_ERROR,
> + "Expected out channel name, got \"%.8s\"\n", arg);
> + return AVERROR(EINVAL);
> + }
> + if (named) {
> + if (!((pan->out_channel_layout >> out_ch_id) & 1)) {
> + av_log(ctx, AV_LOG_ERROR,
> + "Channel \"%.8s\" does not exist in the chosen layout\n", arg0);
> + return AVERROR(EINVAL);
> + }
> + /* get the channel number in the output channel layout:
> + * out_channel_layout & ((1 << out_ch_id) - 1) are all the
> + * channels that come before out_ch_id,
> + * so their count is the index of out_ch_id */
> + out_ch_id = av_get_channel_layout_nb_channels(pan->out_channel_layout & (((int64_t)1 << out_ch_id) - 1));
Note: I wonder if we have a function which counts bits
> + }
> + if (out_ch_id < 0 || out_ch_id >= pan->nb_output_channels) {
> + av_log(ctx, AV_LOG_ERROR,
> + "Invalid out channel name \"%.8s\"\n", arg0);
> + return AVERROR(EINVAL);
> + }
> + if (*arg == '=') {
> + arg++;
> + } else if (*arg == '<') {
> + pan->need_renorm |= (int64_t)1 << out_ch_id;
> + arg++;
> + } else {
> + av_log(ctx, AV_LOG_ERROR,
> + "Syntax error after channel name in \"%.8s\"\n", arg0);
> + return AVERROR(EINVAL);
> + }
> + /* gains */
> + while (1) {
> + gain = 1;
> + if (sscanf(arg, " %lf %n* %n", &gain, &len, &len))
> + arg += len;
> + if (parse_channel_name(&arg, &in_ch_id, &named)){
> + av_log(ctx, AV_LOG_ERROR,
> + "Expected in channel name, got \"%.8s\"\n", arg);
> + return AVERROR(EINVAL);
> + }
> + nb_named[named]++;
> + if (nb_named[!named]) {
> + av_log(ctx, AV_LOG_ERROR,
> + "Can not mix named and numbered channels\n");
> + return AVERROR(EINVAL);
> + }
> + pan->gain.d[out_ch_id][in_ch_id] = gain;
> + if (!*arg)
> + break;
> + if (*arg != '+') {
> + av_log(ctx, AV_LOG_ERROR, "Syntax error near \"%.8s\"\n", arg);
> + return AVERROR(EINVAL);
> + }
> + arg++;
> + skip_spaces(&arg);
> + }
> + }
> + pan->need_renumber = !!nb_named[1];
> +
> + av_free(args);
> + return 0;
> +}
> +
[...]
> +static int config_props(AVFilterLink *link)
> +{
> + AVFilterContext *ctx = link->dst;
> + PanContext *pan = ctx->priv;
> + char buf[1024], *cur;
> + int i, j, k, r;
> + double t;
> +
> + pan->nb_input_channels = av_get_channel_layout_nb_channels(link->channel_layout);
> + if (pan->need_renumber) {
> + // input channels were given by their name: renumber them
> + for (i = j = 0; i < MAX_CHANNELS; i++) {
> + if ((link->channel_layout >> i) & 1) {
> + for (k = 0; k < pan->nb_output_channels; k++)
> + pan->gain.d[k][j] = pan->gain.d[k][i];
> + j++;
> + }
> + }
> + }
> + // renormalize
> + for (i = 0; i < pan->nb_output_channels; i++) {
> + if (!((pan->need_renorm >> i) & 1))
> + continue;
> + t = 0;
> + for (j = 0; j < pan->nb_input_channels; j++)
> + t += pan->gain.d[i][j];
> + if (t > -1E-5 && t < 1E-5) {
> + if (t)
> + av_log(ctx, AV_LOG_WARNING,
> + "Degenerate coefficients while renormalizing\n");
> + continue;
> + }
maybe add a comment about what degenerate means in this context
> + for (j = 0; j < pan->nb_input_channels; j++)
> + pan->gain.d[i][j] /= t;
> + }
> + // summary
> + for (i = 0; i < pan->nb_output_channels; i++) {
> + cur = buf;
> + for (j = 0; j < pan->nb_input_channels; j++) {
> + r = snprintf(cur, buf + sizeof(buf) - cur, "%s%.3g i%d",
> + j ? " + " : "", pan->gain.d[i][j], j);
> + cur += FFMIN(buf + sizeof(buf) - cur, r);
> + }
> + av_log(ctx, AV_LOG_INFO, "o%d = %s\n", i, buf);
> + }
> + // convert to integer
> + for (i = 0; i < pan->nb_output_channels; i++) {
> + for (j = 0; j < pan->nb_input_channels; j++) {
> + if (pan->gain.d[i][j] < -256 || pan->gain.d[i][j] > 256)
> + av_log(ctx, AV_LOG_WARNING, "Gain too large, clamped\n");
Nit: printing some context information (e.g. gain value and indices i
j) may be helpful here
> + pan->gain.i[i][j] = FFMIN(256, FFMAX(-256, pan->gain.d[i][j])) * 256.0;
av_clipf?
--
FFmpeg = Fighting and Foolish Mystic Puritan Everlasting Goblin
More information about the ffmpeg-devel
mailing list