History

avfilter/silenceremove_template: refactor detectors using common peaks code

Paul B Mahol committed 3 years ago

Commit 45fa85a1 parent f02964a1 file · +72 −145Browse files

Showing the changes to libavfilter (1 of 1 file). Show all

Unified · Split

libavfilter/silenceremove_template.c+72−145
@@ -105,73 +105,76 @@ static void fn(queue_sample)(AVFilterContext *ctx, · 104 unmodified lines
105 *window_pos = 0;105 *window_pos = 0;
106}106}
107 107
108static ftype fn(compute_avg)(ftype *cache, ftype sample, ftype wsample,108static ftype fn(compute_avg)(ftype *cache, ftype x, ftype px,
109 int window_size, int *unused, int *unused2)109 int window_size, int *unused, int *unused2)
110{110{
111 ftype r;111 ftype r;
112 112
113 cache[0] += FABS(sample);113 cache[0] += FABS(x);
114 cache[0] -= FABS(wsample);114 cache[0] -= FABS(px);
115 cache[0] = r = FMAX(cache[0], ZERO);115 cache[0] = r = FMAX(cache[0], ZERO);
116 116
117 return r / window_size;117 return r / window_size;
118}118}
119 119
120static ftype fn(compute_median)(ftype *peak, ftype sample, ftype wsample,120#define PEAKS(empty_value,op,sample, psample)\
121 int size, int *ffront, int *bback)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
161static ftype fn(compute_median)(ftype *ss, ftype x, ftype px,
162 int n, int *ffront, int *bback)
122{163{
123 ftype r, abs_sample = FABS(sample);164 ftype r, ax = FABS(x);
124 int front = *ffront;165 int front = *ffront;
125 int back = *bback;166 int back = *bback;
126 int empty = front == back && peak[front] == -ONE;167 int empty = front == back && ss[front] == -ONE;
127 int idx;168 int idx;
128 169
129 if (!empty && FABS(wsample) == peak[front]) {170 PEAKS(-ONE, >, ax, FABS(px))
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 }
168 171
169 peak[back] = abs_sample;172 ss[back] = ax;
170 idx = (back <= front) ? back + (front - back + 1) / 2 : back + (size + front - back + 1) / 2;173 idx = (back <= front) ? back + (front - back + 1) / 2 : back + (n + front - back + 1) / 2;
171 if (idx >= size)174 if (idx >= n)
172 idx -= size;175 idx -= n;
173 av_assert2(idx >= 0 && idx < size);176 av_assert2(idx >= 0 && idx < n);
174 r = peak[idx];177 r = ss[idx];
175 178
176 *ffront = front;179 *ffront = front;
177 *bback = back;180 *bback = back;
@@ -179,56 +182,18 @@ static ftype fn(compute_median)(ftype *peak, ftype sample, ftype wsample, · 1 unmodified line
179 return r;182 return r;
180}183}
181 184
182static ftype fn(compute_peak)(ftype *peak, ftype sample, ftype wsample,185static ftype fn(compute_peak)(ftype *ss, ftype x, ftype px,
183 int size, int *ffront, int *bback)186 int n, int *ffront, int *bback)
184{187{
185 ftype r, abs_sample = FABS(sample);188 ftype r, ax = FABS(x);
186 int front = *ffront;189 int front = *ffront;
187 int back = *bback;190 int back = *bback;
188 int empty = front == back && peak[front] == ZERO;191 int empty = front == back && ss[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 }
199 192
200 if (!empty && abs_sample >= peak[front]) {193 PEAKS(ZERO, >=, ax, FABS(px))
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 }
212 194
213 while (!empty && abs_sample >= peak[back]) {195 ss[back] = ax;
214 peak[back] = ZERO;196 r = ss[front];
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];
232 197
233 *ffront = front;198 *ffront = front;
234 *bback = back;199 *bback = back;
@@ -236,57 +201,19 @@ static ftype fn(compute_peak)(ftype *peak, ftype sample, ftype wsample, · 1 unmodified line
236 return r;201 return r;
237}202}
238 203
239static ftype fn(compute_ptp)(ftype *peak, ftype sample, ftype wsample,204static ftype fn(compute_ptp)(ftype *ss, ftype x, ftype px,
240 int size, int *ffront, int *bback)205 int n, int *ffront, int *bback)
241{206{
242 int front = *ffront;207 int front = *ffront;
243 int back = *bback;208 int back = *bback;
244 int empty = front == back && peak[front] == TMIN;209 int empty = front == back && ss[front] == TMIN;
245 ftype r, max, min;210 ftype r, max, min;
246 211
247 if (!empty && wsample == peak[front]) {212 PEAKS(TMIN, >=, x, px)
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 }
286 213
287 peak[back] = sample;214 ss[back] = x;
288 max = peak[front];215 max = ss[front];
289 min = sample;216 min = x;
290 r = FABS(min) + FABS(max - min);217 r = FABS(min) + FABS(max - min);
291 218
292 *ffront = front;219 *ffront = front;
@@ -295,13 +222,13 @@ static ftype fn(compute_ptp)(ftype *peak, ftype sample, ftype wsample, · 2 unmodified lines
295 return r;222 return r;
296}223}
297 224
298static ftype fn(compute_rms)(ftype *cache, ftype sample, ftype wsample,225static ftype fn(compute_rms)(ftype *cache, ftype x, ftype px,
299 int window_size, int *unused, int *unused2)226 int window_size, int *unused, int *unused2)
300{227{
301 ftype r;228 ftype r;
302 229
303 cache[0] += sample * sample;230 cache[0] += x * x;
304 cache[0] -= wsample * wsample;231 cache[0] -= px * px;
305 cache[0] = r = FMAX(cache[0], ZERO);232 cache[0] = r = FMAX(cache[0], ZERO);
306 233
307 return SQRT(r / window_size);234 return SQRT(r / window_size);

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