2011-03-26 22:02:50 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 Mina Nagy Zaki
|
|
|
|
* Copyright (c) 2000 Edward Beingessner And Sundry Contributors.
|
|
|
|
* This source code is freely redistributable and may be used for any purpose.
|
|
|
|
* This copyright notice must be maintained. Edward Beingessner And Sundry
|
|
|
|
* Contributors are not responsible for the consequences of using this
|
|
|
|
* software.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Stereo Widening Effect. Adds audio cues to move stereo image in
|
|
|
|
* front of the listener. Adapted from the libsox earwax effect.
|
|
|
|
*/
|
|
|
|
|
2012-11-13 16:16:48 +03:00
|
|
|
#include "libavutil/channel_layout.h"
|
2011-03-26 22:02:50 +02:00
|
|
|
#include "avfilter.h"
|
2012-05-12 18:58:14 +03:00
|
|
|
#include "audio.h"
|
2012-05-16 03:27:31 +03:00
|
|
|
#include "formats.h"
|
2011-03-26 22:02:50 +02:00
|
|
|
|
|
|
|
#define NUMTAPS 64
|
|
|
|
|
|
|
|
static const int8_t filt[NUMTAPS] = {
|
|
|
|
/* 30° 330° */
|
|
|
|
4, -6, /* 32 tap stereo FIR filter. */
|
|
|
|
4, -11, /* One side filters as if the */
|
|
|
|
-1, -5, /* signal was from 30 degrees */
|
|
|
|
3, 3, /* from the ear, the other as */
|
|
|
|
-2, 5, /* if 330 degrees. */
|
|
|
|
-5, 0,
|
|
|
|
9, 1,
|
|
|
|
6, 3, /* Input */
|
|
|
|
-4, -1, /* Left Right */
|
|
|
|
-5, -3, /* __________ __________ */
|
|
|
|
-2, -5, /* | | | | */
|
|
|
|
-7, 1, /* .---| Hh,0(f) | | Hh,0(f) |---. */
|
|
|
|
6, -7, /* / |__________| |__________| \ */
|
|
|
|
30, -29, /* / \ / \ */
|
|
|
|
12, -3, /* / X \ */
|
|
|
|
-11, 4, /* / / \ \ */
|
|
|
|
-3, 7, /* ____V_____ __________V V__________ _____V____ */
|
|
|
|
-20, 23, /* | | | | | | | | */
|
|
|
|
2, 0, /* | Hh,30(f) | | Hh,330(f)| | Hh,330(f)| | Hh,30(f) | */
|
|
|
|
1, -6, /* |__________| |__________| |__________| |__________| */
|
|
|
|
-14, -5, /* \ ___ / \ ___ / */
|
|
|
|
15, -18, /* \ / \ / _____ \ / \ / */
|
|
|
|
6, 7, /* `->| + |<--' / \ `-->| + |<-' */
|
|
|
|
15, -10, /* \___/ _/ \_ \___/ */
|
|
|
|
-14, 22, /* \ / \ / \ / */
|
|
|
|
-7, -2, /* `--->| | | |<---' */
|
|
|
|
-4, 9, /* \_/ \_/ */
|
|
|
|
6, -12, /* */
|
|
|
|
6, -6, /* Headphones */
|
|
|
|
0, -11,
|
|
|
|
0, -5,
|
|
|
|
4, 0};
|
|
|
|
|
2017-05-12 20:00:49 +02:00
|
|
|
typedef struct EarwaxContext {
|
2011-03-26 22:02:50 +02:00
|
|
|
int16_t taps[NUMTAPS * 2];
|
|
|
|
} EarwaxContext;
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
2012-12-22 01:38:59 +03:00
|
|
|
static const int sample_rates[] = { 44100, -1 };
|
2015-10-05 05:39:25 +02:00
|
|
|
int ret;
|
2012-05-16 03:27:31 +03:00
|
|
|
|
2011-03-26 22:02:50 +02:00
|
|
|
AVFilterFormats *formats = NULL;
|
2012-05-16 03:27:31 +03:00
|
|
|
AVFilterChannelLayouts *layout = NULL;
|
|
|
|
|
2015-10-05 05:39:25 +02:00
|
|
|
if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_S16 )) < 0 ||
|
|
|
|
(ret = ff_set_common_formats (ctx , formats )) < 0 ||
|
|
|
|
(ret = ff_add_channel_layout (&layout , AV_CH_LAYOUT_STEREO )) < 0 ||
|
|
|
|
(ret = ff_set_common_channel_layouts (ctx , layout )) < 0 ||
|
|
|
|
(ret = ff_set_common_samplerates (ctx , ff_make_format_list(sample_rates) )) < 0)
|
|
|
|
return ret;
|
2011-03-26 22:02:50 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//FIXME: replace with DSPContext.scalarproduct_int16
|
|
|
|
static inline int16_t *scalarproduct(const int16_t *in, const int16_t *endin, int16_t *out)
|
|
|
|
{
|
|
|
|
int32_t sample;
|
|
|
|
int16_t j;
|
|
|
|
|
|
|
|
while (in < endin) {
|
2013-04-03 13:52:01 +03:00
|
|
|
sample = 0;
|
2011-03-26 22:02:50 +02:00
|
|
|
for (j = 0; j < NUMTAPS; j++)
|
|
|
|
sample += in[j] * filt[j];
|
2013-04-03 13:52:01 +03:00
|
|
|
*out = av_clip_int16(sample >> 6);
|
2011-03-26 22:02:50 +02:00
|
|
|
out++;
|
|
|
|
in++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2013-03-10 03:30:30 +03:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
|
2011-03-26 22:02:50 +02:00
|
|
|
{
|
|
|
|
AVFilterLink *outlink = inlink->dst->outputs[0];
|
|
|
|
int16_t *taps, *endin, *in, *out;
|
2018-01-03 23:47:40 +02:00
|
|
|
AVFrame *outsamples = ff_get_audio_buffer(outlink, insamples->nb_samples);
|
2013-07-10 17:39:10 +03:00
|
|
|
int len;
|
2012-07-09 23:10:38 +03:00
|
|
|
|
2013-03-10 03:30:30 +03:00
|
|
|
if (!outsamples) {
|
|
|
|
av_frame_free(&insamples);
|
2012-11-04 02:17:27 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2013-03-10 03:30:30 +03:00
|
|
|
}
|
|
|
|
av_frame_copy_props(outsamples, insamples);
|
2011-11-05 04:44:02 +03:00
|
|
|
|
2011-03-26 22:02:50 +02:00
|
|
|
taps = ((EarwaxContext *)inlink->dst->priv)->taps;
|
|
|
|
out = (int16_t *)outsamples->data[0];
|
|
|
|
in = (int16_t *)insamples ->data[0];
|
|
|
|
|
2013-07-10 17:39:10 +03:00
|
|
|
len = FFMIN(NUMTAPS, 2*insamples->nb_samples);
|
2011-03-26 22:02:50 +02:00
|
|
|
// copy part of new input and process with saved input
|
2013-07-10 17:39:10 +03:00
|
|
|
memcpy(taps+NUMTAPS, in, len * sizeof(*taps));
|
|
|
|
out = scalarproduct(taps, taps + len, out);
|
2011-03-26 22:02:50 +02:00
|
|
|
|
|
|
|
// process current input
|
2013-07-10 17:39:10 +03:00
|
|
|
if (2*insamples->nb_samples >= NUMTAPS ){
|
|
|
|
endin = in + insamples->nb_samples * 2 - NUMTAPS;
|
|
|
|
scalarproduct(in, endin, out);
|
|
|
|
|
|
|
|
// save part of input for next round
|
|
|
|
memcpy(taps, endin, NUMTAPS * sizeof(*taps));
|
|
|
|
} else
|
|
|
|
memmove(taps, taps + 2*insamples->nb_samples, NUMTAPS * sizeof(*taps));
|
2011-03-26 22:02:50 +02:00
|
|
|
|
2013-03-10 03:30:30 +03:00
|
|
|
av_frame_free(&insamples);
|
2013-04-17 00:27:49 +03:00
|
|
|
return ff_filter_frame(outlink, outsamples);
|
2011-03-26 22:02:50 +02:00
|
|
|
}
|
|
|
|
|
2012-11-28 22:01:59 +03:00
|
|
|
static const AVFilterPad earwax_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad earwax_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-10-29 13:50:56 +03:00
|
|
|
AVFilter ff_af_earwax = {
|
2011-03-26 22:02:50 +02:00
|
|
|
.name = "earwax",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Widen the stereo image."),
|
|
|
|
.query_formats = query_formats,
|
|
|
|
.priv_size = sizeof(EarwaxContext),
|
2012-11-28 22:01:59 +03:00
|
|
|
.inputs = earwax_inputs,
|
|
|
|
.outputs = earwax_outputs,
|
2011-03-26 22:02:50 +02:00
|
|
|
};
|