Skip to content

Commit 45fa85a

Browse files
committed
avfilter/silenceremove_template: refactor detectors using common peaks code
1 parent f02964a commit 45fa85a

File tree

1 file changed

+72
-145
lines changed

1 file changed

+72
-145
lines changed

libavfilter/silenceremove_template.c

Lines changed: 72 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -105,188 +105,115 @@ static void fn(queue_sample)(AVFilterContext *ctx,
105105
*window_pos = 0;
106106
}
107107

108-
static ftype fn(compute_avg)(ftype *cache, ftype sample, ftype wsample,
108+
static ftype fn(compute_avg)(ftype *cache, ftype x, ftype px,
109109
int window_size, int *unused, int *unused2)
110110
{
111111
ftype r;
112112

113-
cache[0] += FABS(sample);
114-
cache[0] -= FABS(wsample);
113+
cache[0] += FABS(x);
114+
cache[0] -= FABS(px);
115115
cache[0] = r = FMAX(cache[0], ZERO);
116116

117117
return r / window_size;
118118
}
119119

120-
static ftype fn(compute_median)(ftype *peak, ftype sample, ftype wsample,
121-
int size, int *ffront, int *bback)
120+
#define PEAKS(empty_value,op,sample, psample)\
121+
if (!empty && psample == ss[front]) { \
122+
ss[front] = empty_value; \
123+
if (back != front) { \
124+
front--; \
125+
if (front < 0) \
126+
front = n - 1; \
127+
} \
128+
empty = front == back; \
129+
} \
130+
\
131+
if (!empty && sample op ss[front]) { \
132+
while (1) { \
133+
ss[front] = empty_value; \
134+
if (back == front) { \
135+
empty = 1; \
136+
break; \
137+
} \
138+
front--; \
139+
if (front < 0) \
140+
front = n - 1; \
141+
} \
142+
} \
143+
\
144+
while (!empty && sample op ss[back]) { \
145+
ss[back] = empty_value; \
146+
if (back == front) { \
147+
empty = 1; \
148+
break; \
149+
} \
150+
back++; \
151+
if (back >= n) \
152+
back = 0; \
153+
} \
154+
\
155+
if (!empty) { \
156+
back--; \
157+
if (back < 0) \
158+
back = n - 1; \
159+
}
160+
161+
static ftype fn(compute_median)(ftype *ss, ftype x, ftype px,
162+
int n, int *ffront, int *bback)
122163
{
123-
ftype r, abs_sample = FABS(sample);
164+
ftype r, ax = FABS(x);
124165
int front = *ffront;
125166
int back = *bback;
126-
int empty = front == back && peak[front] == -ONE;
167+
int empty = front == back && ss[front] == -ONE;
127168
int idx;
128169

129-
if (!empty && FABS(wsample) == peak[front]) {
130-
peak[front] = -ONE;
131-
if (back != front) {
132-
front--;
133-
if (front < 0)
134-
front = size - 1;
135-
}
136-
empty = front == back;
137-
}
138-
139-
if (!empty && abs_sample > peak[front]) {
140-
while (1) {
141-
peak[front] = -ONE;
142-
if (back == front) {
143-
empty = 1;
144-
break;
145-
}
146-
front--;
147-
if (front < 0)
148-
front = size - 1;
149-
}
150-
}
151-
152-
while (!empty && abs_sample > peak[back]) {
153-
peak[back] = -ONE;
154-
if (back == front) {
155-
empty = 1;
156-
break;
157-
}
158-
back++;
159-
if (back >= size)
160-
back = 0;
161-
}
162-
163-
if (!empty) {
164-
back--;
165-
if (back < 0)
166-
back = size - 1;
167-
}
170+
PEAKS(-ONE, >, ax, FABS(px))
168171

169-
peak[back] = abs_sample;
170-
idx = (back <= front) ? back + (front - back + 1) / 2 : back + (size + front - back + 1) / 2;
171-
if (idx >= size)
172-
idx -= size;
173-
av_assert2(idx >= 0 && idx < size);
174-
r = peak[idx];
172+
ss[back] = ax;
173+
idx = (back <= front) ? back + (front - back + 1) / 2 : back + (n + front - back + 1) / 2;
174+
if (idx >= n)
175+
idx -= n;
176+
av_assert2(idx >= 0 && idx < n);
177+
r = ss[idx];
175178

176179
*ffront = front;
177180
*bback = back;
178181

179182
return r;
180183
}
181184

182-
static ftype fn(compute_peak)(ftype *peak, ftype sample, ftype wsample,
183-
int size, int *ffront, int *bback)
185+
static ftype fn(compute_peak)(ftype *ss, ftype x, ftype px,
186+
int n, int *ffront, int *bback)
184187
{
185-
ftype r, abs_sample = FABS(sample);
188+
ftype r, ax = FABS(x);
186189
int front = *ffront;
187190
int back = *bback;
188-
int empty = front == back && peak[front] == ZERO;
189-
190-
if (!empty && FABS(wsample) == peak[front]) {
191-
peak[front] = ZERO;
192-
if (back != front) {
193-
front--;
194-
if (front < 0)
195-
front = size - 1;
196-
}
197-
empty = front == back;
198-
}
191+
int empty = front == back && ss[front] == ZERO;
199192

200-
if (!empty && abs_sample >= peak[front]) {
201-
while (1) {
202-
peak[front] = ZERO;
203-
if (back == front) {
204-
empty = 1;
205-
break;
206-
}
207-
front--;
208-
if (front < 0)
209-
front = size - 1;
210-
}
211-
}
193+
PEAKS(ZERO, >=, ax, FABS(px))
212194

213-
while (!empty && abs_sample >= peak[back]) {
214-
peak[back] = ZERO;
215-
if (back == front) {
216-
empty = 1;
217-
break;
218-
}
219-
back++;
220-
if (back >= size)
221-
back = 0;
222-
}
223-
224-
if (!empty) {
225-
back--;
226-
if (back < 0)
227-
back = size - 1;
228-
}
229-
230-
peak[back] = abs_sample;
231-
r = peak[front];
195+
ss[back] = ax;
196+
r = ss[front];
232197

233198
*ffront = front;
234199
*bback = back;
235200

236201
return r;
237202
}
238203

239-
static ftype fn(compute_ptp)(ftype *peak, ftype sample, ftype wsample,
240-
int size, int *ffront, int *bback)
204+
static ftype fn(compute_ptp)(ftype *ss, ftype x, ftype px,
205+
int n, int *ffront, int *bback)
241206
{
242207
int front = *ffront;
243208
int back = *bback;
244-
int empty = front == back && peak[front] == TMIN;
209+
int empty = front == back && ss[front] == TMIN;
245210
ftype r, max, min;
246211

247-
if (!empty && wsample == peak[front]) {
248-
peak[front] = TMIN;
249-
if (back != front) {
250-
front--;
251-
if (front < 0)
252-
front = size - 1;
253-
}
254-
empty = front == back;
255-
}
256-
257-
if (!empty && sample >= peak[front]) {
258-
while (1) {
259-
peak[front] = TMIN;
260-
if (back == front) {
261-
empty = 1;
262-
break;
263-
}
264-
front--;
265-
if (front < 0)
266-
front = size - 1;
267-
}
268-
}
269-
270-
while (!empty && sample >= peak[back]) {
271-
peak[back] = TMIN;
272-
if (back == front) {
273-
empty = 1;
274-
break;
275-
}
276-
back++;
277-
if (back >= size)
278-
back = 0;
279-
}
280-
281-
if (!empty) {
282-
back--;
283-
if (back < 0)
284-
back = size - 1;
285-
}
212+
PEAKS(TMIN, >=, x, px)
286213

287-
peak[back] = sample;
288-
max = peak[front];
289-
min = sample;
214+
ss[back] = x;
215+
max = ss[front];
216+
min = x;
290217
r = FABS(min) + FABS(max - min);
291218

292219
*ffront = front;
@@ -295,13 +222,13 @@ static ftype fn(compute_ptp)(ftype *peak, ftype sample, ftype wsample,
295222
return r;
296223
}
297224

298-
static ftype fn(compute_rms)(ftype *cache, ftype sample, ftype wsample,
225+
static ftype fn(compute_rms)(ftype *cache, ftype x, ftype px,
299226
int window_size, int *unused, int *unused2)
300227
{
301228
ftype r;
302229

303-
cache[0] += sample * sample;
304-
cache[0] -= wsample * wsample;
230+
cache[0] += x * x;
231+
cache[0] -= px * px;
305232
cache[0] = r = FMAX(cache[0], ZERO);
306233

307234
return SQRT(r / window_size);

0 commit comments

Comments
 (0)