]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_earwax.c
overlay: do not leak x/y expressions.
[ffmpeg] / libavfilter / af_earwax.c
1 /*
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
7  * software.
8  *
9  * This file is part of FFmpeg.
10  *
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.
15  *
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.
20  *
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
24  */
25
26 /**
27  * @file
28  * Stereo Widening Effect. Adds audio cues to move stereo image in
29  * front of the listener. Adapted from the libsox earwax effect.
30  */
31
32 #include "libavutil/audioconvert.h"
33 #include "avfilter.h"
34
35 #define NUMTAPS 64
36
37 static const int8_t filt[NUMTAPS] = {
38 /* 30°  330° */
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. */
44    -5,    0,
45     9,    1,
46     6,    3,     /*                         Input                         */
47    -4,   -1,     /*                   Left         Right                  */
48    -5,   -3,     /*                __________   __________                */
49    -2,   -5,     /*               |          | |          |               */
50    -7,    1,     /*           .---|  Hh,0(f) | |  Hh,0(f) |---.           */
51     6,   -7,     /*          /    |__________| |__________|    \          */
52    30,  -29,     /*         /                \ /                \         */
53    12,   -3,     /*        /                  X                  \        */
54   -11,    4,     /*       /                  / \                  \       */
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,     /*                `--->| |       | |<---'                */
65    -4,    9,     /*                     \_/       \_/                     */
66     6,  -12,     /*                                                       */
67     6,   -6,     /*                       Headphones                      */
68     0,  -11,
69     0,   -5,
70     4,    0};
71
72 typedef struct {
73     int16_t taps[NUMTAPS * 2];
74 } EarwaxContext;
75
76 static int query_formats(AVFilterContext *ctx)
77 {
78     AVFilterFormats *formats = NULL;
79     avfilter_add_format(&formats, AV_SAMPLE_FMT_S16);
80     avfilter_set_common_sample_formats(ctx, formats);
81     formats = NULL;
82     avfilter_add_format(&formats, AV_CH_LAYOUT_STEREO);
83     avfilter_set_common_channel_layouts(ctx, formats);
84     formats = NULL;
85     avfilter_add_format(&formats, AVFILTER_PACKED);
86     avfilter_set_common_packing_formats(ctx, formats);
87
88     return 0;
89 }
90
91 static int config_input(AVFilterLink *inlink)
92 {
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);
98     }
99     return 0;
100 }
101
102 //FIXME: replace with DSPContext.scalarproduct_int16
103 static inline int16_t *scalarproduct(const int16_t *in, const int16_t *endin, int16_t *out)
104 {
105     int32_t sample;
106     int16_t j;
107
108     while (in < endin) {
109         sample = 32;
110         for (j = 0; j < NUMTAPS; j++)
111             sample += in[j] * filt[j];
112         *out = sample >> 6;
113         out++;
114         in++;
115     }
116
117     return out;
118 }
119
120 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
121 {
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);
128
129     taps  = ((EarwaxContext *)inlink->dst->priv)->taps;
130     out   = (int16_t *)outsamples->data[0];
131     in    = (int16_t *)insamples ->data[0];
132
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);
136
137     // process current input
138     endin = in + insamples->audio->nb_samples * 2 - NUMTAPS;
139     out   = scalarproduct(in, endin, out);
140
141     // save part of input for next round
142     memcpy(taps, endin, NUMTAPS * sizeof(*taps));
143
144     avfilter_filter_samples(outlink, outsamples);
145     avfilter_unref_buffer(insamples);
146 }
147
148 AVFilter avfilter_af_earwax = {
149     .name           = "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, },
158                                  {  .name = NULL}},
159
160     .outputs = (const AVFilterPad[])  {{  .name     = "default",
161                                     .type           = AVMEDIA_TYPE_AUDIO, },
162                                  {  .name = NULL}},
163 };