Signed-off-by: Paul B Mahol <onemda@gmail.com>
Paul B Mahol authored on 2014/07/02 18:39:07... | ... |
@@ -13,6 +13,7 @@ version <next>: |
13 | 13 |
- added codecview filter to visualize information exported by some codecs |
14 | 14 |
- Matroska 3D support thorugh side data |
15 | 15 |
- HTML generation using texi2html is deprecated in favor of makeinfo/texi2any |
16 |
+- silenceremove filter |
|
16 | 17 |
|
17 | 18 |
|
18 | 19 |
version 2.3: |
... | ... |
@@ -42,6 +42,7 @@ |
42 | 42 |
• ported lenscorrection filter from frei0r filter |
43 | 43 |
• large optimizations in dctdnoiz to make it usable |
44 | 44 |
• added codecview filter to visualize information exported by some codecs |
45 |
+ • added silenceremove filter |
|
45 | 46 |
|
46 | 47 |
┌────────────────────────────┐ |
47 | 48 |
│ libavutil │ |
... | ... |
@@ -1875,6 +1875,75 @@ ffmpeg -i silence.mp3 -af silencedetect=noise=0.0001 -f null - |
1875 | 1875 |
@end example |
1876 | 1876 |
@end itemize |
1877 | 1877 |
|
1878 |
+@section silenceremove |
|
1879 |
+ |
|
1880 |
+Remove silence from the beginning, middle or end of the audio. |
|
1881 |
+ |
|
1882 |
+The filter accepts the following options: |
|
1883 |
+ |
|
1884 |
+@table @option |
|
1885 |
+@item start_periods |
|
1886 |
+This value is used to indicate if audio should be trimmed at beginning of |
|
1887 |
+the audio. A value of zero indicates no silence should be trimmed from the |
|
1888 |
+beginning. When specifying a non-zero value, it trims audio up until it |
|
1889 |
+finds non-silence. Normally, when trimming silence from beginning of audio |
|
1890 |
+the @var{start_periods} will be @code{1} but it can be increased to higher |
|
1891 |
+values to trim all audio up to specific count of non-silence periods. |
|
1892 |
+Default value is @code{0}. |
|
1893 |
+ |
|
1894 |
+@item start_duration |
|
1895 |
+Specify the amount of time that non-silence must be detected before it stops |
|
1896 |
+trimming audio. By increasing the duration, bursts of noises can be treated |
|
1897 |
+as silence and trimmed off. Default value is @code{0}. |
|
1898 |
+ |
|
1899 |
+@item start_threshold |
|
1900 |
+This indicates what sample value should be treated as silence. For digital |
|
1901 |
+audio, a value of @code{0} may be fine but for audio recorded from analog, |
|
1902 |
+you may wish to increase the value to account for background noise. |
|
1903 |
+Can be specified in dB (in case "dB" is appended to the specified value) |
|
1904 |
+or amplitude ratio. Default value is @code{0}. |
|
1905 |
+ |
|
1906 |
+@item stop_periods |
|
1907 |
+Set the count for trimming silence from the end of audio. |
|
1908 |
+To remove silence from the middle of a file, specify a @var{stop_periods} |
|
1909 |
+that is negative. This value is then threated as a positive value and is |
|
1910 |
+used to indicate the effect should restart processing as specified by |
|
1911 |
+@var{start_periods}, making it suitable for removing periods of silence |
|
1912 |
+in the middle of the audio. |
|
1913 |
+Default value is @code{0}. |
|
1914 |
+ |
|
1915 |
+@item stop_duration |
|
1916 |
+Specify a duration of silence that must exist before audio is not copied any |
|
1917 |
+more. By specifying a higher duration, silence that is wanted can be left in |
|
1918 |
+the audio. |
|
1919 |
+Default value is @code{0}. |
|
1920 |
+ |
|
1921 |
+@item stop_threshold |
|
1922 |
+This is the same as @option{start_threshold} but for trimming silence from |
|
1923 |
+the end of audio. |
|
1924 |
+Can be specified in dB (in case "dB" is appended to the specified value) |
|
1925 |
+or amplitude ratio. Default value is @code{0}. |
|
1926 |
+ |
|
1927 |
+@item leave_silence |
|
1928 |
+This indicate that @var{stop_duration} length of audio should be left intact |
|
1929 |
+at the beginning of each period of silence. |
|
1930 |
+For example, if you want to remove long pauses between words but do not want |
|
1931 |
+to remove the pauses completely. Default value is @code{0}. |
|
1932 |
+ |
|
1933 |
+@end table |
|
1934 |
+ |
|
1935 |
+@subsection Examples |
|
1936 |
+ |
|
1937 |
+@itemize |
|
1938 |
+@item |
|
1939 |
+The following example shows how this filter can be used to start a recording |
|
1940 |
+that does not contain the delay at the start which usually occurs between |
|
1941 |
+pressing the record button and the start of the performance: |
|
1942 |
+@example |
|
1943 |
+silenceremove=1:5:0.02 |
|
1944 |
+@end example |
|
1945 |
+@end itemize |
|
1946 |
+ |
|
1878 | 1947 |
@section treble |
1879 | 1948 |
|
1880 | 1949 |
Boost or cut treble (upper) frequencies of the audio using a two-pole |
... | ... |
@@ -78,6 +78,7 @@ OBJS-$(CONFIG_PAN_FILTER) += af_pan.o |
78 | 78 |
OBJS-$(CONFIG_REPLAYGAIN_FILTER) += af_replaygain.o |
79 | 79 |
OBJS-$(CONFIG_RESAMPLE_FILTER) += af_resample.o |
80 | 80 |
OBJS-$(CONFIG_SILENCEDETECT_FILTER) += af_silencedetect.o |
81 |
+OBJS-$(CONFIG_SILENCEREMOVE_FILTER) += af_silenceremove.o |
|
81 | 82 |
OBJS-$(CONFIG_TREBLE_FILTER) += af_biquads.o |
82 | 83 |
OBJS-$(CONFIG_VOLUME_FILTER) += af_volume.o |
83 | 84 |
OBJS-$(CONFIG_VOLUMEDETECT_FILTER) += af_volumedetect.o |
84 | 85 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,482 @@ |
0 |
+/* |
|
1 |
+ * Copyright (c) 2001 Heikki Leinonen |
|
2 |
+ * Copyright (c) 2001 Chris Bagwell |
|
3 |
+ * Copyright (c) 2003 Donnie Smith |
|
4 |
+ * Copyright (c) 2014 Paul B Mahol |
|
5 |
+ * |
|
6 |
+ * This file is part of FFmpeg. |
|
7 |
+ * |
|
8 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
9 |
+ * modify it under the terms of the GNU Lesser General Public |
|
10 |
+ * License as published by the Free Software Foundation; either |
|
11 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
12 |
+ * |
|
13 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
14 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 |
+ * Lesser General Public License for more details. |
|
17 |
+ * |
|
18 |
+ * You should have received a copy of the GNU Lesser General Public |
|
19 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
20 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
21 |
+ */ |
|
22 |
+ |
|
23 |
+#include <float.h> /* DBL_MAX */ |
|
24 |
+ |
|
25 |
+#include "libavutil/opt.h" |
|
26 |
+#include "libavutil/timestamp.h" |
|
27 |
+#include "audio.h" |
|
28 |
+#include "formats.h" |
|
29 |
+#include "avfilter.h" |
|
30 |
+#include "internal.h" |
|
31 |
+ |
|
32 |
+enum SilenceMode { |
|
33 |
+ SILENCE_TRIM, |
|
34 |
+ SILENCE_TRIM_FLUSH, |
|
35 |
+ SILENCE_COPY, |
|
36 |
+ SILENCE_COPY_FLUSH, |
|
37 |
+ SILENCE_STOP |
|
38 |
+}; |
|
39 |
+ |
|
40 |
+typedef struct SilenceRemoveContext { |
|
41 |
+ const AVClass *class; |
|
42 |
+ |
|
43 |
+ enum SilenceMode mode; |
|
44 |
+ |
|
45 |
+ int start_periods; |
|
46 |
+ int64_t start_duration; |
|
47 |
+ double start_threshold; |
|
48 |
+ |
|
49 |
+ int stop_periods; |
|
50 |
+ int64_t stop_duration; |
|
51 |
+ double stop_threshold; |
|
52 |
+ |
|
53 |
+ double *start_holdoff; |
|
54 |
+ size_t start_holdoff_offset; |
|
55 |
+ size_t start_holdoff_end; |
|
56 |
+ int start_found_periods; |
|
57 |
+ |
|
58 |
+ double *stop_holdoff; |
|
59 |
+ size_t stop_holdoff_offset; |
|
60 |
+ size_t stop_holdoff_end; |
|
61 |
+ int stop_found_periods; |
|
62 |
+ |
|
63 |
+ double *window; |
|
64 |
+ double *window_current; |
|
65 |
+ double *window_end; |
|
66 |
+ int window_size; |
|
67 |
+ double rms_sum; |
|
68 |
+ |
|
69 |
+ int leave_silence; |
|
70 |
+ int restart; |
|
71 |
+ int64_t next_pts; |
|
72 |
+} SilenceRemoveContext; |
|
73 |
+ |
|
74 |
+#define OFFSET(x) offsetof(SilenceRemoveContext, x) |
|
75 |
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM |
|
76 |
+static const AVOption silenceremove_options[] = { |
|
77 |
+ { "start_periods", NULL, OFFSET(start_periods), AV_OPT_TYPE_INT, {.i64=0}, 0, 9000, FLAGS }, |
|
78 |
+ { "start_duration", NULL, OFFSET(start_duration), AV_OPT_TYPE_DURATION, {.i64=0}, 0, 9000, FLAGS }, |
|
79 |
+ { "start_threshold", NULL, OFFSET(start_threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, DBL_MAX, FLAGS }, |
|
80 |
+ { "stop_periods", NULL, OFFSET(stop_periods), AV_OPT_TYPE_INT, {.i64=0}, -9000, 9000, FLAGS }, |
|
81 |
+ { "stop_duration", NULL, OFFSET(stop_duration), AV_OPT_TYPE_DURATION, {.i64=0}, 0, 9000, FLAGS }, |
|
82 |
+ { "stop_threshold", NULL, OFFSET(stop_threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, DBL_MAX, FLAGS }, |
|
83 |
+ { "leave_silence", NULL, OFFSET(leave_silence), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS }, |
|
84 |
+ { NULL } |
|
85 |
+}; |
|
86 |
+ |
|
87 |
+AVFILTER_DEFINE_CLASS(silenceremove); |
|
88 |
+ |
|
89 |
+static av_cold int init(AVFilterContext *ctx) |
|
90 |
+{ |
|
91 |
+ SilenceRemoveContext *s = ctx->priv; |
|
92 |
+ |
|
93 |
+ if (s->stop_periods < 0) { |
|
94 |
+ s->stop_periods = -s->stop_periods; |
|
95 |
+ s->restart = 1; |
|
96 |
+ } |
|
97 |
+ |
|
98 |
+ return 0; |
|
99 |
+} |
|
100 |
+ |
|
101 |
+static void clear_rms(SilenceRemoveContext *s) |
|
102 |
+{ |
|
103 |
+ memset(s->window, 0, s->window_size * sizeof(*s->window)); |
|
104 |
+ |
|
105 |
+ s->window_current = s->window; |
|
106 |
+ s->window_end = s->window + s->window_size; |
|
107 |
+ s->rms_sum = 0; |
|
108 |
+} |
|
109 |
+ |
|
110 |
+static int config_input(AVFilterLink *inlink) |
|
111 |
+{ |
|
112 |
+ AVFilterContext *ctx = inlink->dst; |
|
113 |
+ SilenceRemoveContext *s = ctx->priv; |
|
114 |
+ |
|
115 |
+ s->window_size = (inlink->sample_rate / 50) * inlink->channels; |
|
116 |
+ s->window = av_malloc_array(s->window_size, sizeof(*s->window)); |
|
117 |
+ if (!s->window) |
|
118 |
+ return AVERROR(ENOMEM); |
|
119 |
+ |
|
120 |
+ clear_rms(s); |
|
121 |
+ |
|
122 |
+ s->start_duration = av_rescale(s->start_duration, inlink->sample_rate, |
|
123 |
+ AV_TIME_BASE); |
|
124 |
+ s->stop_duration = av_rescale(s->stop_duration, inlink->sample_rate, |
|
125 |
+ AV_TIME_BASE); |
|
126 |
+ |
|
127 |
+ s->start_holdoff = av_malloc_array(FFMAX(s->start_duration, 1), |
|
128 |
+ sizeof(*s->start_holdoff) * |
|
129 |
+ inlink->channels); |
|
130 |
+ if (!s->start_holdoff) |
|
131 |
+ return AVERROR(ENOMEM); |
|
132 |
+ |
|
133 |
+ s->start_holdoff_offset = 0; |
|
134 |
+ s->start_holdoff_end = 0; |
|
135 |
+ s->start_found_periods = 0; |
|
136 |
+ |
|
137 |
+ s->stop_holdoff = av_malloc_array(FFMAX(s->stop_duration, 1), |
|
138 |
+ sizeof(*s->stop_holdoff) * |
|
139 |
+ inlink->channels); |
|
140 |
+ if (!s->stop_holdoff) |
|
141 |
+ return AVERROR(ENOMEM); |
|
142 |
+ |
|
143 |
+ s->stop_holdoff_offset = 0; |
|
144 |
+ s->stop_holdoff_end = 0; |
|
145 |
+ s->stop_found_periods = 0; |
|
146 |
+ |
|
147 |
+ if (s->start_periods) |
|
148 |
+ s->mode = SILENCE_TRIM; |
|
149 |
+ else |
|
150 |
+ s->mode = SILENCE_COPY; |
|
151 |
+ |
|
152 |
+ return 0; |
|
153 |
+} |
|
154 |
+ |
|
155 |
+static int config_output(AVFilterLink *outlink) |
|
156 |
+{ |
|
157 |
+ outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP; |
|
158 |
+ |
|
159 |
+ return 0; |
|
160 |
+} |
|
161 |
+ |
|
162 |
+static double compute_rms(SilenceRemoveContext *s, double sample) |
|
163 |
+{ |
|
164 |
+ double new_sum; |
|
165 |
+ |
|
166 |
+ new_sum = s->rms_sum; |
|
167 |
+ new_sum -= *s->window_current; |
|
168 |
+ new_sum += sample * sample; |
|
169 |
+ |
|
170 |
+ return sqrt(new_sum / s->window_size); |
|
171 |
+} |
|
172 |
+ |
|
173 |
+static void update_rms(SilenceRemoveContext *s, double sample) |
|
174 |
+{ |
|
175 |
+ s->rms_sum -= *s->window_current; |
|
176 |
+ *s->window_current = sample * sample; |
|
177 |
+ s->rms_sum += *s->window_current; |
|
178 |
+ |
|
179 |
+ s->window_current++; |
|
180 |
+ if (s->window_current >= s->window_end) |
|
181 |
+ s->window_current = s->window; |
|
182 |
+} |
|
183 |
+ |
|
184 |
+static void flush(AVFrame *out, AVFilterLink *outlink, |
|
185 |
+ int *nb_samples_written, int *ret) |
|
186 |
+{ |
|
187 |
+ if (*nb_samples_written) { |
|
188 |
+ out->nb_samples = *nb_samples_written / outlink->channels; |
|
189 |
+ *ret = ff_filter_frame(outlink, out); |
|
190 |
+ *nb_samples_written = 0; |
|
191 |
+ } else { |
|
192 |
+ av_frame_free(&out); |
|
193 |
+ } |
|
194 |
+} |
|
195 |
+ |
|
196 |
+static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
|
197 |
+{ |
|
198 |
+ AVFilterContext *ctx = inlink->dst; |
|
199 |
+ AVFilterLink *outlink = ctx->outputs[0]; |
|
200 |
+ SilenceRemoveContext *s = ctx->priv; |
|
201 |
+ int i, j, threshold, ret = 0; |
|
202 |
+ int nbs, nb_samples_read, nb_samples_written; |
|
203 |
+ double *obuf, *ibuf = (double *)in->data[0]; |
|
204 |
+ AVFrame *out; |
|
205 |
+ |
|
206 |
+ nb_samples_read = nb_samples_written = 0; |
|
207 |
+ |
|
208 |
+ switch (s->mode) { |
|
209 |
+ case SILENCE_TRIM: |
|
210 |
+silence_trim: |
|
211 |
+ nbs = in->nb_samples - nb_samples_read / inlink->channels; |
|
212 |
+ if (!nbs) |
|
213 |
+ break; |
|
214 |
+ |
|
215 |
+ for (i = 0; i < nbs; i++) { |
|
216 |
+ threshold = 0; |
|
217 |
+ for (j = 0; j < inlink->channels; j++) { |
|
218 |
+ threshold |= compute_rms(s, ibuf[j]) > s->start_threshold; |
|
219 |
+ } |
|
220 |
+ |
|
221 |
+ if (threshold) { |
|
222 |
+ for (j = 0; j < inlink->channels; j++) { |
|
223 |
+ update_rms(s, *ibuf); |
|
224 |
+ s->start_holdoff[s->start_holdoff_end++] = *ibuf++; |
|
225 |
+ nb_samples_read++; |
|
226 |
+ } |
|
227 |
+ |
|
228 |
+ if (s->start_holdoff_end >= s->start_duration * inlink->channels) { |
|
229 |
+ if (++s->start_found_periods >= s->start_periods) { |
|
230 |
+ s->mode = SILENCE_TRIM_FLUSH; |
|
231 |
+ goto silence_trim_flush; |
|
232 |
+ } |
|
233 |
+ |
|
234 |
+ s->start_holdoff_offset = 0; |
|
235 |
+ s->start_holdoff_end = 0; |
|
236 |
+ } |
|
237 |
+ } else { |
|
238 |
+ s->start_holdoff_end = 0; |
|
239 |
+ |
|
240 |
+ for (j = 0; j < inlink->channels; j++) |
|
241 |
+ update_rms(s, ibuf[j]); |
|
242 |
+ |
|
243 |
+ ibuf += inlink->channels; |
|
244 |
+ nb_samples_read += inlink->channels; |
|
245 |
+ } |
|
246 |
+ } |
|
247 |
+ break; |
|
248 |
+ |
|
249 |
+ case SILENCE_TRIM_FLUSH: |
|
250 |
+silence_trim_flush: |
|
251 |
+ nbs = s->start_holdoff_end - s->start_holdoff_offset; |
|
252 |
+ nbs -= nbs % inlink->channels; |
|
253 |
+ if (!nbs) |
|
254 |
+ break; |
|
255 |
+ |
|
256 |
+ out = ff_get_audio_buffer(inlink, nbs / inlink->channels); |
|
257 |
+ if (!out) { |
|
258 |
+ av_frame_free(&in); |
|
259 |
+ return AVERROR(ENOMEM); |
|
260 |
+ } |
|
261 |
+ |
|
262 |
+ memcpy(out->data[0], &s->start_holdoff[s->start_holdoff_offset], |
|
263 |
+ nbs * sizeof(double)); |
|
264 |
+ s->start_holdoff_offset += nbs; |
|
265 |
+ |
|
266 |
+ ret = ff_filter_frame(outlink, out); |
|
267 |
+ |
|
268 |
+ if (s->start_holdoff_offset == s->start_holdoff_end) { |
|
269 |
+ s->start_holdoff_offset = 0; |
|
270 |
+ s->start_holdoff_end = 0; |
|
271 |
+ s->mode = SILENCE_COPY; |
|
272 |
+ goto silence_copy; |
|
273 |
+ } |
|
274 |
+ break; |
|
275 |
+ |
|
276 |
+ case SILENCE_COPY: |
|
277 |
+silence_copy: |
|
278 |
+ nbs = in->nb_samples - nb_samples_read / inlink->channels; |
|
279 |
+ if (!nbs) |
|
280 |
+ break; |
|
281 |
+ |
|
282 |
+ out = ff_get_audio_buffer(inlink, nbs); |
|
283 |
+ if (!out) { |
|
284 |
+ av_frame_free(&in); |
|
285 |
+ return AVERROR(ENOMEM); |
|
286 |
+ } |
|
287 |
+ obuf = (double *)out->data[0]; |
|
288 |
+ |
|
289 |
+ if (s->stop_periods) { |
|
290 |
+ for (i = 0; i < nbs; i++) { |
|
291 |
+ threshold = 1; |
|
292 |
+ for (j = 0; j < inlink->channels; j++) |
|
293 |
+ threshold &= compute_rms(s, ibuf[j]) > s->stop_threshold; |
|
294 |
+ |
|
295 |
+ if (threshold && s->stop_holdoff_end && !s->leave_silence) { |
|
296 |
+ s->mode = SILENCE_COPY_FLUSH; |
|
297 |
+ flush(out, outlink, &nb_samples_written, &ret); |
|
298 |
+ goto silence_copy_flush; |
|
299 |
+ } else if (threshold) { |
|
300 |
+ for (j = 0; j < inlink->channels; j++) { |
|
301 |
+ update_rms(s, *ibuf); |
|
302 |
+ *obuf++ = *ibuf++; |
|
303 |
+ nb_samples_read++; |
|
304 |
+ nb_samples_written++; |
|
305 |
+ } |
|
306 |
+ } else if (!threshold) { |
|
307 |
+ for (j = 0; j < inlink->channels; j++) { |
|
308 |
+ update_rms(s, *ibuf); |
|
309 |
+ if (s->leave_silence) { |
|
310 |
+ *obuf++ = *ibuf; |
|
311 |
+ nb_samples_written++; |
|
312 |
+ } |
|
313 |
+ |
|
314 |
+ s->stop_holdoff[s->stop_holdoff_end++] = *ibuf++; |
|
315 |
+ nb_samples_read++; |
|
316 |
+ } |
|
317 |
+ |
|
318 |
+ if (s->stop_holdoff_end >= s->stop_duration * inlink->channels) { |
|
319 |
+ if (++s->stop_found_periods >= s->stop_periods) { |
|
320 |
+ s->stop_holdoff_offset = 0; |
|
321 |
+ s->stop_holdoff_end = 0; |
|
322 |
+ |
|
323 |
+ if (!s->restart) { |
|
324 |
+ s->mode = SILENCE_STOP; |
|
325 |
+ flush(out, outlink, &nb_samples_written, &ret); |
|
326 |
+ goto silence_stop; |
|
327 |
+ } else { |
|
328 |
+ s->stop_found_periods = 0; |
|
329 |
+ s->start_found_periods = 0; |
|
330 |
+ s->start_holdoff_offset = 0; |
|
331 |
+ s->start_holdoff_end = 0; |
|
332 |
+ clear_rms(s); |
|
333 |
+ s->mode = SILENCE_TRIM; |
|
334 |
+ flush(out, outlink, &nb_samples_written, &ret); |
|
335 |
+ goto silence_trim; |
|
336 |
+ } |
|
337 |
+ } else { |
|
338 |
+ s->mode = SILENCE_COPY_FLUSH; |
|
339 |
+ flush(out, outlink, &nb_samples_written, &ret); |
|
340 |
+ goto silence_copy_flush; |
|
341 |
+ } |
|
342 |
+ flush(out, outlink, &nb_samples_written, &ret); |
|
343 |
+ break; |
|
344 |
+ } |
|
345 |
+ } |
|
346 |
+ } |
|
347 |
+ flush(out, outlink, &nb_samples_written, &ret); |
|
348 |
+ } else { |
|
349 |
+ memcpy(obuf, ibuf, sizeof(double) * nbs * inlink->channels); |
|
350 |
+ ret = ff_filter_frame(outlink, out); |
|
351 |
+ } |
|
352 |
+ break; |
|
353 |
+ |
|
354 |
+ case SILENCE_COPY_FLUSH: |
|
355 |
+silence_copy_flush: |
|
356 |
+ nbs = s->stop_holdoff_end - s->stop_holdoff_offset; |
|
357 |
+ nbs -= nbs % inlink->channels; |
|
358 |
+ if (!nbs) |
|
359 |
+ break; |
|
360 |
+ |
|
361 |
+ out = ff_get_audio_buffer(inlink, nbs / inlink->channels); |
|
362 |
+ if (!out) { |
|
363 |
+ av_frame_free(&in); |
|
364 |
+ return AVERROR(ENOMEM); |
|
365 |
+ } |
|
366 |
+ |
|
367 |
+ memcpy(out->data[0], &s->stop_holdoff[s->stop_holdoff_offset], |
|
368 |
+ nbs * sizeof(double)); |
|
369 |
+ s->stop_holdoff_offset += nbs; |
|
370 |
+ |
|
371 |
+ ret = ff_filter_frame(outlink, out); |
|
372 |
+ |
|
373 |
+ if (s->stop_holdoff_offset == s->stop_holdoff_end) { |
|
374 |
+ s->stop_holdoff_offset = 0; |
|
375 |
+ s->stop_holdoff_end = 0; |
|
376 |
+ s->mode = SILENCE_COPY; |
|
377 |
+ goto silence_copy; |
|
378 |
+ } |
|
379 |
+ break; |
|
380 |
+ case SILENCE_STOP: |
|
381 |
+silence_stop: |
|
382 |
+ break; |
|
383 |
+ } |
|
384 |
+ |
|
385 |
+ av_frame_free(&in); |
|
386 |
+ |
|
387 |
+ return ret; |
|
388 |
+} |
|
389 |
+ |
|
390 |
+static int request_frame(AVFilterLink *outlink) |
|
391 |
+{ |
|
392 |
+ AVFilterContext *ctx = outlink->src; |
|
393 |
+ SilenceRemoveContext *s = ctx->priv; |
|
394 |
+ int ret; |
|
395 |
+ |
|
396 |
+ ret = ff_request_frame(ctx->inputs[0]); |
|
397 |
+ if (ret == AVERROR_EOF && (s->mode == SILENCE_COPY_FLUSH || |
|
398 |
+ s->mode == SILENCE_COPY)) { |
|
399 |
+ int nbs = s->stop_holdoff_end - s->stop_holdoff_offset; |
|
400 |
+ if (nbs) { |
|
401 |
+ AVFrame *frame; |
|
402 |
+ |
|
403 |
+ frame = ff_get_audio_buffer(outlink, nbs / outlink->channels); |
|
404 |
+ if (!frame) |
|
405 |
+ return AVERROR(ENOMEM); |
|
406 |
+ |
|
407 |
+ memcpy(frame->data[0], &s->stop_holdoff[s->stop_holdoff_offset], |
|
408 |
+ nbs * sizeof(double)); |
|
409 |
+ ret = ff_filter_frame(ctx->inputs[0], frame); |
|
410 |
+ } |
|
411 |
+ s->mode = SILENCE_STOP; |
|
412 |
+ } |
|
413 |
+ return ret; |
|
414 |
+} |
|
415 |
+ |
|
416 |
+static int query_formats(AVFilterContext *ctx) |
|
417 |
+{ |
|
418 |
+ AVFilterFormats *formats = NULL; |
|
419 |
+ AVFilterChannelLayouts *layouts = NULL; |
|
420 |
+ static const enum AVSampleFormat sample_fmts[] = { |
|
421 |
+ AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_NONE |
|
422 |
+ }; |
|
423 |
+ |
|
424 |
+ layouts = ff_all_channel_layouts(); |
|
425 |
+ if (!layouts) |
|
426 |
+ return AVERROR(ENOMEM); |
|
427 |
+ ff_set_common_channel_layouts(ctx, layouts); |
|
428 |
+ |
|
429 |
+ formats = ff_make_format_list(sample_fmts); |
|
430 |
+ if (!formats) |
|
431 |
+ return AVERROR(ENOMEM); |
|
432 |
+ ff_set_common_formats(ctx, formats); |
|
433 |
+ |
|
434 |
+ formats = ff_all_samplerates(); |
|
435 |
+ if (!formats) |
|
436 |
+ return AVERROR(ENOMEM); |
|
437 |
+ ff_set_common_samplerates(ctx, formats); |
|
438 |
+ |
|
439 |
+ return 0; |
|
440 |
+} |
|
441 |
+ |
|
442 |
+static av_cold void uninit(AVFilterContext *ctx) |
|
443 |
+{ |
|
444 |
+ SilenceRemoveContext *s = ctx->priv; |
|
445 |
+ |
|
446 |
+ av_freep(&s->start_holdoff); |
|
447 |
+ av_freep(&s->stop_holdoff); |
|
448 |
+ av_freep(&s->window); |
|
449 |
+} |
|
450 |
+ |
|
451 |
+static const AVFilterPad silenceremove_inputs[] = { |
|
452 |
+ { |
|
453 |
+ .name = "default", |
|
454 |
+ .type = AVMEDIA_TYPE_AUDIO, |
|
455 |
+ .config_props = config_input, |
|
456 |
+ .filter_frame = filter_frame, |
|
457 |
+ }, |
|
458 |
+ { NULL } |
|
459 |
+}; |
|
460 |
+ |
|
461 |
+static const AVFilterPad silenceremove_outputs[] = { |
|
462 |
+ { |
|
463 |
+ .name = "default", |
|
464 |
+ .type = AVMEDIA_TYPE_AUDIO, |
|
465 |
+ .config_props = config_output, |
|
466 |
+ .request_frame = request_frame, |
|
467 |
+ }, |
|
468 |
+ { NULL } |
|
469 |
+}; |
|
470 |
+ |
|
471 |
+AVFilter ff_af_silenceremove = { |
|
472 |
+ .name = "silenceremove", |
|
473 |
+ .description = NULL_IF_CONFIG_SMALL("Remove silence."), |
|
474 |
+ .priv_size = sizeof(SilenceRemoveContext), |
|
475 |
+ .priv_class = &silenceremove_class, |
|
476 |
+ .init = init, |
|
477 |
+ .uninit = uninit, |
|
478 |
+ .query_formats = query_formats, |
|
479 |
+ .inputs = silenceremove_inputs, |
|
480 |
+ .outputs = silenceremove_outputs, |
|
481 |
+}; |
... | ... |
@@ -96,6 +96,7 @@ void avfilter_register_all(void) |
96 | 96 |
REGISTER_FILTER(REPLAYGAIN, replaygain, af); |
97 | 97 |
REGISTER_FILTER(RESAMPLE, resample, af); |
98 | 98 |
REGISTER_FILTER(SILENCEDETECT, silencedetect, af); |
99 |
+ REGISTER_FILTER(SILENCEREMOVE, silenceremove, af); |
|
99 | 100 |
REGISTER_FILTER(TREBLE, treble, af); |
100 | 101 |
REGISTER_FILTER(VOLUME, volume, af); |
101 | 102 |
REGISTER_FILTER(VOLUMEDETECT, volumedetect, af); |
... | ... |
@@ -30,8 +30,8 @@ |
30 | 30 |
#include "libavutil/version.h" |
31 | 31 |
|
32 | 32 |
#define LIBAVFILTER_VERSION_MAJOR 5 |
33 |
-#define LIBAVFILTER_VERSION_MINOR 0 |
|
34 |
-#define LIBAVFILTER_VERSION_MICRO 103 |
|
33 |
+#define LIBAVFILTER_VERSION_MINOR 1 |
|
34 |
+#define LIBAVFILTER_VERSION_MICRO 100 |
|
35 | 35 |
|
36 | 36 |
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ |
37 | 37 |
LIBAVFILTER_VERSION_MINOR, \ |