History

avfilter/af_earwax: fix filter behavior

Previous filter output was incorrect. New one actually follows graph in comments described on side of filter taps.

Paul B Mahol committed 5 years ago

Commit f41de041 parent c369ceb3 files · +117 −49Browse files

Unified · Split

3 files
  1. libavfilter/af_earwax.c +96 −28
  2. tests/fate/filter-audio.mak +1 −1
  3. tests/ref/fate/filter-earwax +20 −20
libavfilter/af_earwax.c+96−28
@@ -34,9 +34,9 @@ · 33 unmodified lines
34#include "audio.h"34#include "audio.h"
35#include "formats.h"35#include "formats.h"
36 36
37#define NUMTAPS 6437#define NUMTAPS 32
38 38
39static const int8_t filt[NUMTAPS] = {39static const int8_t filt[NUMTAPS * 2] = {
40/* 30° 330° */40/* 30° 330° */
41 4, -6, /* 32 tap stereo FIR filter. */41 4, -6, /* 32 tap stereo FIR filter. */
42 4, -11, /* One side filters as if the */42 4, -11, /* One side filters as if the */
@@ -72,7 +72,10 @@ static const int8_t filt[NUMTAPS] = { · 29 unmodified lines
72 4, 0};72 4, 0};
73 73
74typedef struct EarwaxContext {74typedef struct EarwaxContext {
75 int16_t taps[NUMTAPS * 2];75 int16_t filter[2][NUMTAPS];
76 int16_t taps[4][NUMTAPS * 2];
77
78 AVFrame *frame[2];
76} EarwaxContext;79} EarwaxContext;
77 80
78static int query_formats(AVFilterContext *ctx)81static int query_formats(AVFilterContext *ctx)
@@ -83,7 +86,7 @@ static int query_formats(AVFilterContext *ctx) · 4 unmodified lines
83 AVFilterFormats *formats = NULL;86 AVFilterFormats *formats = NULL;
84 AVFilterChannelLayouts *layout = NULL;87 AVFilterChannelLayouts *layout = NULL;
85 88
86 if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_S16 )) < 0 ||89 if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_S16P )) < 0 ||
87 (ret = ff_set_common_formats (ctx , formats )) < 0 ||90 (ret = ff_set_common_formats (ctx , formats )) < 0 ||
88 (ret = ff_add_channel_layout (&layout , AV_CH_LAYOUT_STEREO )) < 0 ||91 (ret = ff_add_channel_layout (&layout , AV_CH_LAYOUT_STEREO )) < 0 ||
89 (ret = ff_set_common_channel_layouts (ctx , layout )) < 0 ||92 (ret = ff_set_common_channel_layouts (ctx , layout )) < 0 ||
@@ -94,7 +97,8 @@ static int query_formats(AVFilterContext *ctx) · 4 unmodified lines
94}97}
95 98
96//FIXME: replace with DSPContext.scalarproduct_int1699//FIXME: replace with DSPContext.scalarproduct_int16
97static inline int16_t *scalarproduct(const int16_t *in, const int16_t *endin, int16_t *out)100static inline int16_t *scalarproduct(const int16_t *in, const int16_t *endin,
101 const int16_t *filt, int16_t *out)
98{102{
99 int32_t sample;103 int32_t sample;
100 int16_t j;104 int16_t j;
@@ -103,7 +107,7 @@ static inline int16_t *scalarproduct(const int16_t *in, const int16_t *endin, in · 2 unmodified lines
103 sample = 0;107 sample = 0;
104 for (j = 0; j < NUMTAPS; j++)108 for (j = 0; j < NUMTAPS; j++)
105 sample += in[j] * filt[j];109 sample += in[j] * filt[j];
106 *out = av_clip_int16(sample >> 6);110 *out = av_clip_int16(sample >> 7);
107 out++;111 out++;
108 in++;112 in++;
109 }113 }
@@ -111,40 +115,102 @@ static inline int16_t *scalarproduct(const int16_t *in, const int16_t *endin, in · 1 unmodified line
111 return out;115 return out;
112}116}
113 117
114static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)118static int config_input(AVFilterLink *inlink)
115{119{
116 AVFilterLink *outlink = inlink->dst->outputs[0];120 EarwaxContext *s = inlink->dst->priv;
117 int16_t *taps, *endin, *in, *out;
118 AVFrame *outsamples = ff_get_audio_buffer(outlink, insamples->nb_samples);
119 int len;
120 121
121 if (!outsamples) {122 for (int i = 0; i < NUMTAPS; i++) {
122 av_frame_free(&insamples);123 s->filter[0][i] = filt[i * 2];
123 return AVERROR(ENOMEM);124 s->filter[1][i] = filt[i * 2 + 1];
124 }125 }
125 av_frame_copy_props(outsamples, insamples);
126 126
127 taps = ((EarwaxContext *)inlink->dst->priv)->taps;127 return 0;
128 out = (int16_t *)outsamples->data[0];128}
129 in = (int16_t *)insamples ->data[0];129
130static void convolve(AVFilterContext *ctx, AVFrame *in,
131 int input_ch, int output_ch,
132 int filter_ch, int tap_ch)
133{
134 EarwaxContext *s = ctx->priv;
135 int16_t *taps, *endin, *dst, *src;
136 int len;
137
138 taps = s->taps[tap_ch];
139 dst = (int16_t *)s->frame[input_ch]->data[output_ch];
140 src = (int16_t *)in->data[input_ch];
130 141
131 len = FFMIN(NUMTAPS, 2*insamples->nb_samples);142 len = FFMIN(NUMTAPS, in->nb_samples);
132 // copy part of new input and process with saved input143 // copy part of new input and process with saved input
133 memcpy(taps+NUMTAPS, in, len * sizeof(*taps));144 memcpy(taps+NUMTAPS, src, len * sizeof(*taps));
134 out = scalarproduct(taps, taps + len, out);145 dst = scalarproduct(taps, taps + len, s->filter[filter_ch], dst);
135 146
136 // process current input147 // process current input
137 if (2*insamples->nb_samples >= NUMTAPS ){148 if (2*in->nb_samples >= NUMTAPS ){
138 endin = in + insamples->nb_samples * 2 - NUMTAPS;149 endin = src + in->nb_samples - NUMTAPS;
139 scalarproduct(in, endin, out);150 scalarproduct(src, endin, s->filter[filter_ch], dst);
140 151
141 // save part of input for next round152 // save part of input for next round
142 memcpy(taps, endin, NUMTAPS * sizeof(*taps));153 memcpy(taps, endin, NUMTAPS * sizeof(*taps));
143 } else154 } else {
144 memmove(taps, taps + 2*insamples->nb_samples, NUMTAPS * sizeof(*taps));155 memmove(taps, taps + in->nb_samples, NUMTAPS * sizeof(*taps));
156 }
157}
158
159static void mix(AVFilterContext *ctx, AVFrame *out,
160 int output_ch, int f0, int f1, int i0, int i1)
161{
162 EarwaxContext *s = ctx->priv;
163 const int16_t *srcl = (const int16_t *)s->frame[f0]->data[i0];
164 const int16_t *srcr = (const int16_t *)s->frame[f1]->data[i1];
165 int16_t *dst = (int16_t *)out->data[output_ch];
166
167 for (int n = 0; n < out->nb_samples; n++)
168 dst[n] = av_clip_int16(srcl[n] + srcr[n]);
169}
170
171static int filter_frame(AVFilterLink *inlink, AVFrame *in)
172{
173 AVFilterContext *ctx = inlink->dst;
174 EarwaxContext *s = ctx->priv;
175 AVFilterLink *outlink = ctx->outputs[0];
176 AVFrame *out = ff_get_audio_buffer(outlink, in->nb_samples);
177
178 for (int ch = 0; ch < 2; ch++) {
179 if (!s->frame[ch] || s->frame[ch]->nb_samples < in->nb_samples) {
180 av_frame_free(&s->frame[ch]);
181 s->frame[ch] = ff_get_audio_buffer(outlink, in->nb_samples);
182 if (!s->frame[ch]) {
183 av_frame_free(&in);
184 av_frame_free(&out);
185 return AVERROR(ENOMEM);
186 }
187 }
188 }
189
190 if (!out) {
191 av_frame_free(&in);
192 return AVERROR(ENOMEM);
193 }
194 av_frame_copy_props(out, in);
195
196 convolve(ctx, in, 0, 0, 0, 0);
197 convolve(ctx, in, 0, 1, 1, 1);
198 convolve(ctx, in, 1, 0, 0, 2);
199 convolve(ctx, in, 1, 1, 1, 3);
200
201 mix(ctx, out, 0, 0, 1, 1, 0);
202 mix(ctx, out, 1, 0, 1, 0, 1);
203
204 av_frame_free(&in);
205 return ff_filter_frame(outlink, out);
206}
207
208static av_cold void uninit(AVFilterContext *ctx)
209{
210 EarwaxContext *s = ctx->priv;
145 211
146 av_frame_free(&insamples);212 av_frame_free(&s->frame[0]);
147 return ff_filter_frame(outlink, outsamples);213 av_frame_free(&s->frame[1]);
148}214}
149 215
150static const AVFilterPad earwax_inputs[] = {216static const AVFilterPad earwax_inputs[] = {
@@ -152,6 +218,7 @@ static const AVFilterPad earwax_inputs[] = { · 1 unmodified line
152 .name = "default",218 .name = "default",
153 .type = AVMEDIA_TYPE_AUDIO,219 .type = AVMEDIA_TYPE_AUDIO,
154 .filter_frame = filter_frame,220 .filter_frame = filter_frame,
221 .config_props = config_input,
155 },222 },
156 { NULL }223 { NULL }
157};224};
@@ -169,6 +236,7 @@ AVFilter ff_af_earwax = { · 11 unmodified lines
169 .description = NULL_IF_CONFIG_SMALL("Widen the stereo image."),236 .description = NULL_IF_CONFIG_SMALL("Widen the stereo image."),
170 .query_formats = query_formats,237 .query_formats = query_formats,
171 .priv_size = sizeof(EarwaxContext),238 .priv_size = sizeof(EarwaxContext),
239 .uninit = uninit,
172 .inputs = earwax_inputs,240 .inputs = earwax_inputs,
173 .outputs = earwax_outputs,241 .outputs = earwax_outputs,
174};242};
tests/fate/filter-audio.mak+1−1
@@ -112,7 +112,7 @@ fate-filter-dcshift: CMD = framecrc -i $(SRC) -frames:a 20 -af aresample,dcshift · 111 unmodified lines
112FATE_AFILTER-$(call FILTERDEMDECENCMUX, EARWAX, WAV, PCM_S16LE, PCM_S16LE, WAV) += fate-filter-earwax112FATE_AFILTER-$(call FILTERDEMDECENCMUX, EARWAX, WAV, PCM_S16LE, PCM_S16LE, WAV) += fate-filter-earwax
113fate-filter-earwax: tests/data/asynth-44100-2.wav113fate-filter-earwax: tests/data/asynth-44100-2.wav
114fate-filter-earwax: SRC = $(TARGET_PATH)/tests/data/asynth-44100-2.wav114fate-filter-earwax: SRC = $(TARGET_PATH)/tests/data/asynth-44100-2.wav
115fate-filter-earwax: CMD = framecrc -i $(SRC) -frames:a 20 -af earwax115fate-filter-earwax: CMD = framecrc -i $(SRC) -frames:a 20 -af aresample,earwax,aresample
116 116
117FATE_AFILTER-$(call FILTERDEMDECENCMUX, EXTRASTEREO, WAV, PCM_S16LE, PCM_S16LE, WAV) += fate-filter-extrastereo117FATE_AFILTER-$(call FILTERDEMDECENCMUX, EXTRASTEREO, WAV, PCM_S16LE, PCM_S16LE, WAV) += fate-filter-extrastereo
118fate-filter-extrastereo: tests/data/asynth-44100-2.wav118fate-filter-extrastereo: tests/data/asynth-44100-2.wav
tests/ref/fate/filter-earwax+20−20
@@ -4,23 +4,23 @@ · 3 unmodified lines
4#sample_rate 0: 441004#sample_rate 0: 44100
5#channel_layout 0: 35#channel_layout 0: 3
6#channel_layout_name 0: stereo6#channel_layout_name 0: stereo
70, 0, 0, 1024, 4096, 0x900af75170, 0, 0, 1024, 4096, 0xb7e1f437
80, 1024, 1024, 1024, 4096, 0xad57006580, 1024, 1024, 1024, 4096, 0xa031042a
90, 2048, 2048, 1024, 4096, 0x93d5f49490, 2048, 2048, 1024, 4096, 0x9b72ed0f
100, 3072, 3072, 1024, 4096, 0x2c65ef7d100, 3072, 3072, 1024, 4096, 0xff14ed33
110, 4096, 4096, 1024, 4096, 0xdc8af6d2110, 4096, 4096, 1024, 4096, 0x96eef519
120, 5120, 5120, 1024, 4096, 0x7ae00249120, 5120, 5120, 1024, 4096, 0x290d0ca0
130, 6144, 6144, 1024, 4096, 0xaab5fdd0130, 6144, 6144, 1024, 4096, 0x0393fbf5
140, 7168, 7168, 1024, 4096, 0x4373ef39140, 7168, 7168, 1024, 4096, 0xed89ef59
150, 8192, 8192, 1024, 4096, 0x0756eb43150, 8192, 8192, 1024, 4096, 0xf664e969
160, 9216, 9216, 1024, 4096, 0x494d06e0160, 9216, 9216, 1024, 4096, 0x261a05e4
170, 10240, 10240, 1024, 4096, 0x4393ffae170, 10240, 10240, 1024, 4096, 0xc334ff5b
180, 11264, 11264, 1024, 4096, 0x6972f97e180, 11264, 11264, 1024, 4096, 0x030ffa65
190, 12288, 12288, 1024, 4096, 0xb834ea05190, 12288, 12288, 1024, 4096, 0xcfb4e835
200, 13312, 13312, 1024, 4096, 0x39b8f871200, 13312, 13312, 1024, 4096, 0xd9adf7ff
210, 14336, 14336, 1024, 4096, 0xf032fccd210, 14336, 14336, 1024, 4096, 0x5e9001ae
220, 15360, 15360, 1024, 4096, 0xefcd0709220, 15360, 15360, 1024, 4096, 0xbfaf0174
230, 16384, 16384, 1024, 4096, 0x0590ebc0230, 16384, 16384, 1024, 4096, 0x8cf3f061
240, 17408, 17408, 1024, 4096, 0x2e75f264240, 17408, 17408, 1024, 4096, 0x35ffece5
250, 18432, 18432, 1024, 4096, 0xbea1fd03250, 18432, 18432, 1024, 4096, 0x1de801e2
260, 19456, 19456, 1024, 4096, 0x9bbe0434260, 19456, 19456, 1024, 4096, 0xa1a40372

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t