2 * Copyright (c) 2011 Mina Nagy Zaki
3 * Copyright (c) 2000 Edward Beingessner And Sundry Contributors.
4 * This source code is freely redistributable and may be used for any purpose.
5 * This copyright notice must be maintained. Edward Beingessner And Sundry
6 * Contributors are not responsible for the consequences of using this
9 * This file is part of FFmpeg.
11 * FFmpeg is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * FFmpeg is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 * Stereo Widening Effect. Adds audio cues to move stereo image in
29 * front of the listener. Adapted from the libsox earwax effect.
32 #include "libavutil/audioconvert.h"
37 static const int8_t filt[NUMTAPS] = {
39 4, -6, /* 32 tap stereo FIR filter. */
40 4, -11, /* One side filters as if the */
41 -1, -5, /* signal was from 30 degrees */
42 3, 3, /* from the ear, the other as */
43 -2, 5, /* if 330 degrees. */
47 -4, -1, /* Left Right */
48 -5, -3, /* __________ __________ */
50 -7, 1, /* .---| Hh,0(f) | | Hh,0(f) |---. */
51 6, -7, /* / |__________| |__________| \ */
52 30, -29, /* / \ / \ */
55 -3, 7, /* ____V_____ __________V V__________ _____V____ */
56 -20, 23, /* | | | | | | | | */
57 2, 0, /* | Hh,30(f) | | Hh,330(f)| | Hh,330(f)| | Hh,30(f) | */
58 1, -6, /* |__________| |__________| |__________| |__________| */
59 -14, -5, /* \ ___ / \ ___ / */
60 15, -18, /* \ / \ / _____ \ / \ / */
61 6, 7, /* `->| + |<--' / \ `-->| + |<-' */
62 15, -10, /* \___/ _/ \_ \___/ */
63 -14, 22, /* \ / \ / \ / */
64 -7, -2, /* `--->| | | |<---' */
67 6, -6, /* Headphones */
73 int16_t taps[NUMTAPS * 2];
76 static int query_formats(AVFilterContext *ctx)
78 AVFilterFormats *formats = NULL;
79 avfilter_add_format(&formats, AV_SAMPLE_FMT_S16);
80 avfilter_set_common_sample_formats(ctx, formats);
82 avfilter_add_format(&formats, AV_CH_LAYOUT_STEREO);
83 avfilter_set_common_channel_layouts(ctx, formats);
85 avfilter_add_format(&formats, AVFILTER_PACKED);
86 avfilter_set_common_packing_formats(ctx, formats);
91 static int config_input(AVFilterLink *inlink)
93 if (inlink->sample_rate != 44100) {
94 av_log(inlink->dst, AV_LOG_ERROR,
95 "The earwax filter only works for 44.1kHz audio. Insert "
96 "a resample filter before this\n");
97 return AVERROR(EINVAL);
102 //FIXME: replace with DSPContext.scalarproduct_int16
103 static inline int16_t *scalarproduct(const int16_t *in, const int16_t *endin, int16_t *out)
110 for (j = 0; j < NUMTAPS; j++)
111 sample += in[j] * filt[j];
120 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
122 AVFilterLink *outlink = inlink->dst->outputs[0];
123 int16_t *taps, *endin, *in, *out;
124 AVFilterBufferRef *outsamples =
125 avfilter_get_audio_buffer(inlink, AV_PERM_WRITE,
126 insamples->audio->nb_samples);
127 avfilter_copy_buffer_ref_props(outsamples, insamples);
129 taps = ((EarwaxContext *)inlink->dst->priv)->taps;
130 out = (int16_t *)outsamples->data[0];
131 in = (int16_t *)insamples ->data[0];
133 // copy part of new input and process with saved input
134 memcpy(taps+NUMTAPS, in, NUMTAPS * sizeof(*taps));
135 out = scalarproduct(taps, taps + NUMTAPS, out);
137 // process current input
138 endin = in + insamples->audio->nb_samples * 2 - NUMTAPS;
139 out = scalarproduct(in, endin, out);
141 // save part of input for next round
142 memcpy(taps, endin, NUMTAPS * sizeof(*taps));
144 avfilter_filter_samples(outlink, outsamples);
145 avfilter_unref_buffer(insamples);
148 AVFilter avfilter_af_earwax = {
150 .description = NULL_IF_CONFIG_SMALL("Widen the stereo image."),
151 .query_formats = query_formats,
152 .priv_size = sizeof(EarwaxContext),
153 .inputs = (const AVFilterPad[]) {{ .name = "default",
154 .type = AVMEDIA_TYPE_AUDIO,
155 .filter_samples = filter_samples,
156 .config_props = config_input,
157 .min_perms = AV_PERM_READ, },
160 .outputs = (const AVFilterPad[]) {{ .name = "default",
161 .type = AVMEDIA_TYPE_AUDIO, },