OSDN Git Service

libavfilter/scale2ref: Add constants for the primary input
[android-x86/external-ffmpeg.git] / libavfilter / af_silenceremove.c
1 /*
2  * Copyright (c) 2001 Heikki Leinonen
3  * Copyright (c) 2001 Chris Bagwell
4  * Copyright (c) 2003 Donnie Smith
5  * Copyright (c) 2014 Paul B Mahol
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include <float.h> /* DBL_MAX */
25
26 #include "libavutil/opt.h"
27 #include "libavutil/timestamp.h"
28 #include "audio.h"
29 #include "formats.h"
30 #include "avfilter.h"
31 #include "internal.h"
32
33 enum SilenceMode {
34     SILENCE_TRIM,
35     SILENCE_TRIM_FLUSH,
36     SILENCE_COPY,
37     SILENCE_COPY_FLUSH,
38     SILENCE_STOP
39 };
40
41 typedef struct SilenceRemoveContext {
42     const AVClass *class;
43
44     enum SilenceMode mode;
45
46     int start_periods;
47     int64_t start_duration;
48     double start_threshold;
49
50     int stop_periods;
51     int64_t stop_duration;
52     double stop_threshold;
53
54     double *start_holdoff;
55     size_t start_holdoff_offset;
56     size_t start_holdoff_end;
57     int    start_found_periods;
58
59     double *stop_holdoff;
60     size_t stop_holdoff_offset;
61     size_t stop_holdoff_end;
62     int    stop_found_periods;
63
64     double window_ratio;
65     double *window;
66     double *window_current;
67     double *window_end;
68     int window_size;
69     double sum;
70
71     int leave_silence;
72     int restart;
73     int64_t next_pts;
74
75     int detection;
76     void (*update)(struct SilenceRemoveContext *s, double sample);
77     double(*compute)(struct SilenceRemoveContext *s, double sample);
78 } SilenceRemoveContext;
79
80 #define OFFSET(x) offsetof(SilenceRemoveContext, x)
81 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
82 static const AVOption silenceremove_options[] = {
83     { "start_periods",   NULL, OFFSET(start_periods),   AV_OPT_TYPE_INT,      {.i64=0},     0,    9000, FLAGS },
84     { "start_duration",  NULL, OFFSET(start_duration),  AV_OPT_TYPE_DURATION, {.i64=0},     0,    9000, FLAGS },
85     { "start_threshold", NULL, OFFSET(start_threshold), AV_OPT_TYPE_DOUBLE,   {.dbl=0},     0, DBL_MAX, FLAGS },
86     { "stop_periods",    NULL, OFFSET(stop_periods),    AV_OPT_TYPE_INT,      {.i64=0}, -9000,    9000, FLAGS },
87     { "stop_duration",   NULL, OFFSET(stop_duration),   AV_OPT_TYPE_DURATION, {.i64=0},     0,    9000, FLAGS },
88     { "stop_threshold",  NULL, OFFSET(stop_threshold),  AV_OPT_TYPE_DOUBLE,   {.dbl=0},     0, DBL_MAX, FLAGS },
89     { "leave_silence",   NULL, OFFSET(leave_silence),   AV_OPT_TYPE_BOOL,     {.i64=0},     0,       1, FLAGS },
90     { "detection",       NULL, OFFSET(detection),       AV_OPT_TYPE_INT,      {.i64=1},     0,       1, FLAGS, "detection" },
91     {   "peak",          0,    0,                       AV_OPT_TYPE_CONST,    {.i64=0},     0,       0, FLAGS, "detection" },
92     {   "rms",           0,    0,                       AV_OPT_TYPE_CONST,    {.i64=1},     0,       0, FLAGS, "detection" },
93     { "window",          NULL, OFFSET(window_ratio),    AV_OPT_TYPE_DOUBLE,   {.dbl=0.02},  0,      10, FLAGS },
94     { NULL }
95 };
96
97 AVFILTER_DEFINE_CLASS(silenceremove);
98
99 static double compute_peak(SilenceRemoveContext *s, double sample)
100 {
101     double new_sum;
102
103     new_sum  = s->sum;
104     new_sum -= *s->window_current;
105     new_sum += fabs(sample);
106
107     return new_sum / s->window_size;
108 }
109
110 static void update_peak(SilenceRemoveContext *s, double sample)
111 {
112     s->sum -= *s->window_current;
113     *s->window_current = fabs(sample);
114     s->sum += *s->window_current;
115
116     s->window_current++;
117     if (s->window_current >= s->window_end)
118         s->window_current = s->window;
119 }
120
121 static double compute_rms(SilenceRemoveContext *s, double sample)
122 {
123     double new_sum;
124
125     new_sum  = s->sum;
126     new_sum -= *s->window_current;
127     new_sum += sample * sample;
128
129     return sqrt(new_sum / s->window_size);
130 }
131
132 static void update_rms(SilenceRemoveContext *s, double sample)
133 {
134     s->sum -= *s->window_current;
135     *s->window_current = sample * sample;
136     s->sum += *s->window_current;
137
138     s->window_current++;
139     if (s->window_current >= s->window_end)
140         s->window_current = s->window;
141 }
142
143 static av_cold int init(AVFilterContext *ctx)
144 {
145     SilenceRemoveContext *s = ctx->priv;
146
147     if (s->stop_periods < 0) {
148         s->stop_periods = -s->stop_periods;
149         s->restart = 1;
150     }
151
152     switch (s->detection) {
153     case 0:
154         s->update = update_peak;
155         s->compute = compute_peak;
156         break;
157     case 1:
158         s->update = update_rms;
159         s->compute = compute_rms;
160         break;
161     };
162
163     return 0;
164 }
165
166 static void clear_window(SilenceRemoveContext *s)
167 {
168     memset(s->window, 0, s->window_size * sizeof(*s->window));
169
170     s->window_current = s->window;
171     s->window_end = s->window + s->window_size;
172     s->sum = 0;
173 }
174
175 static int config_input(AVFilterLink *inlink)
176 {
177     AVFilterContext *ctx = inlink->dst;
178     SilenceRemoveContext *s = ctx->priv;
179
180     s->window_size = FFMAX((inlink->sample_rate * s->window_ratio), 1) * inlink->channels;
181     s->window = av_malloc_array(s->window_size, sizeof(*s->window));
182     if (!s->window)
183         return AVERROR(ENOMEM);
184
185     clear_window(s);
186
187     s->start_duration = av_rescale(s->start_duration, inlink->sample_rate,
188                                    AV_TIME_BASE);
189     s->stop_duration  = av_rescale(s->stop_duration, inlink->sample_rate,
190                                    AV_TIME_BASE);
191
192     s->start_holdoff = av_malloc_array(FFMAX(s->start_duration, 1),
193                                        sizeof(*s->start_holdoff) *
194                                        inlink->channels);
195     if (!s->start_holdoff)
196         return AVERROR(ENOMEM);
197
198     s->start_holdoff_offset = 0;
199     s->start_holdoff_end    = 0;
200     s->start_found_periods  = 0;
201
202     s->stop_holdoff = av_malloc_array(FFMAX(s->stop_duration, 1),
203                                       sizeof(*s->stop_holdoff) *
204                                       inlink->channels);
205     if (!s->stop_holdoff)
206         return AVERROR(ENOMEM);
207
208     s->stop_holdoff_offset = 0;
209     s->stop_holdoff_end    = 0;
210     s->stop_found_periods  = 0;
211
212     if (s->start_periods)
213         s->mode = SILENCE_TRIM;
214     else
215         s->mode = SILENCE_COPY;
216
217     return 0;
218 }
219
220 static void flush(SilenceRemoveContext *s,
221                   AVFrame *out, AVFilterLink *outlink,
222                   int *nb_samples_written, int *ret)
223 {
224     if (*nb_samples_written) {
225         out->nb_samples = *nb_samples_written / outlink->channels;
226
227         out->pts = s->next_pts;
228         s->next_pts += av_rescale_q(out->nb_samples,
229                                     (AVRational){1, outlink->sample_rate},
230                                     outlink->time_base);
231
232         *ret = ff_filter_frame(outlink, out);
233         *nb_samples_written = 0;
234     } else {
235         av_frame_free(&out);
236     }
237 }
238
239 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
240 {
241     AVFilterContext *ctx = inlink->dst;
242     AVFilterLink *outlink = ctx->outputs[0];
243     SilenceRemoveContext *s = ctx->priv;
244     int i, j, threshold, ret = 0;
245     int nbs, nb_samples_read, nb_samples_written;
246     double *obuf, *ibuf = (double *)in->data[0];
247     AVFrame *out;
248
249     nb_samples_read = nb_samples_written = 0;
250
251     switch (s->mode) {
252     case SILENCE_TRIM:
253 silence_trim:
254         nbs = in->nb_samples - nb_samples_read / inlink->channels;
255         if (!nbs)
256             break;
257
258         for (i = 0; i < nbs; i++) {
259             threshold = 0;
260             for (j = 0; j < inlink->channels; j++) {
261                 threshold |= s->compute(s, ibuf[j]) > s->start_threshold;
262             }
263
264             if (threshold) {
265                 for (j = 0; j < inlink->channels; j++) {
266                     s->update(s, *ibuf);
267                     s->start_holdoff[s->start_holdoff_end++] = *ibuf++;
268                 }
269                 nb_samples_read += inlink->channels;
270
271                 if (s->start_holdoff_end >= s->start_duration * inlink->channels) {
272                     if (++s->start_found_periods >= s->start_periods) {
273                         s->mode = SILENCE_TRIM_FLUSH;
274                         goto silence_trim_flush;
275                     }
276
277                     s->start_holdoff_offset = 0;
278                     s->start_holdoff_end = 0;
279                 }
280             } else {
281                 s->start_holdoff_end = 0;
282
283                 for (j = 0; j < inlink->channels; j++)
284                     s->update(s, ibuf[j]);
285
286                 ibuf += inlink->channels;
287                 nb_samples_read += inlink->channels;
288             }
289         }
290         break;
291
292     case SILENCE_TRIM_FLUSH:
293 silence_trim_flush:
294         nbs  = s->start_holdoff_end - s->start_holdoff_offset;
295         nbs -= nbs % inlink->channels;
296         if (!nbs)
297             break;
298
299         out = ff_get_audio_buffer(inlink, nbs / inlink->channels);
300         if (!out) {
301             av_frame_free(&in);
302             return AVERROR(ENOMEM);
303         }
304
305         memcpy(out->data[0], &s->start_holdoff[s->start_holdoff_offset],
306                nbs * sizeof(double));
307
308         out->pts = s->next_pts;
309         s->next_pts += av_rescale_q(out->nb_samples,
310                                     (AVRational){1, outlink->sample_rate},
311                                     outlink->time_base);
312
313         s->start_holdoff_offset += nbs;
314
315         ret = ff_filter_frame(outlink, out);
316
317         if (s->start_holdoff_offset == s->start_holdoff_end) {
318             s->start_holdoff_offset = 0;
319             s->start_holdoff_end = 0;
320             s->mode = SILENCE_COPY;
321             goto silence_copy;
322         }
323         break;
324
325     case SILENCE_COPY:
326 silence_copy:
327         nbs = in->nb_samples - nb_samples_read / inlink->channels;
328         if (!nbs)
329             break;
330
331         out = ff_get_audio_buffer(inlink, nbs);
332         if (!out) {
333             av_frame_free(&in);
334             return AVERROR(ENOMEM);
335         }
336         obuf = (double *)out->data[0];
337
338         if (s->stop_periods) {
339             for (i = 0; i < nbs; i++) {
340                 threshold = 1;
341                 for (j = 0; j < inlink->channels; j++)
342                     threshold &= s->compute(s, ibuf[j]) > s->stop_threshold;
343
344                 if (threshold && s->stop_holdoff_end && !s->leave_silence) {
345                     s->mode = SILENCE_COPY_FLUSH;
346                     flush(s, out, outlink, &nb_samples_written, &ret);
347                     goto silence_copy_flush;
348                 } else if (threshold) {
349                     for (j = 0; j < inlink->channels; j++) {
350                         s->update(s, *ibuf);
351                         *obuf++ = *ibuf++;
352                     }
353                     nb_samples_read    += inlink->channels;
354                     nb_samples_written += inlink->channels;
355                 } else if (!threshold) {
356                     for (j = 0; j < inlink->channels; j++) {
357                         s->update(s, *ibuf);
358                         if (s->leave_silence) {
359                             *obuf++ = *ibuf;
360                             nb_samples_written++;
361                         }
362
363                         s->stop_holdoff[s->stop_holdoff_end++] = *ibuf++;
364                     }
365                     nb_samples_read += inlink->channels;
366
367                     if (s->stop_holdoff_end >= s->stop_duration * inlink->channels) {
368                         if (++s->stop_found_periods >= s->stop_periods) {
369                             s->stop_holdoff_offset = 0;
370                             s->stop_holdoff_end = 0;
371
372                             if (!s->restart) {
373                                 s->mode = SILENCE_STOP;
374                                 flush(s, out, outlink, &nb_samples_written, &ret);
375                                 goto silence_stop;
376                             } else {
377                                 s->stop_found_periods = 0;
378                                 s->start_found_periods = 0;
379                                 s->start_holdoff_offset = 0;
380                                 s->start_holdoff_end = 0;
381                                 clear_window(s);
382                                 s->mode = SILENCE_TRIM;
383                                 flush(s, out, outlink, &nb_samples_written, &ret);
384                                 goto silence_trim;
385                             }
386                         }
387                         s->mode = SILENCE_COPY_FLUSH;
388                         flush(s, out, outlink, &nb_samples_written, &ret);
389                         goto silence_copy_flush;
390                     }
391                 }
392             }
393             flush(s, out, outlink, &nb_samples_written, &ret);
394         } else {
395             memcpy(obuf, ibuf, sizeof(double) * nbs * inlink->channels);
396
397             out->pts = s->next_pts;
398             s->next_pts += av_rescale_q(out->nb_samples,
399                                         (AVRational){1, outlink->sample_rate},
400                                         outlink->time_base);
401
402             ret = ff_filter_frame(outlink, out);
403         }
404         break;
405
406     case SILENCE_COPY_FLUSH:
407 silence_copy_flush:
408         nbs  = s->stop_holdoff_end - s->stop_holdoff_offset;
409         nbs -= nbs % inlink->channels;
410         if (!nbs)
411             break;
412
413         out = ff_get_audio_buffer(inlink, nbs / inlink->channels);
414         if (!out) {
415             av_frame_free(&in);
416             return AVERROR(ENOMEM);
417         }
418
419         memcpy(out->data[0], &s->stop_holdoff[s->stop_holdoff_offset],
420                nbs * sizeof(double));
421         s->stop_holdoff_offset += nbs;
422
423         out->pts = s->next_pts;
424         s->next_pts += av_rescale_q(out->nb_samples,
425                                     (AVRational){1, outlink->sample_rate},
426                                     outlink->time_base);
427
428         ret = ff_filter_frame(outlink, out);
429
430         if (s->stop_holdoff_offset == s->stop_holdoff_end) {
431             s->stop_holdoff_offset = 0;
432             s->stop_holdoff_end = 0;
433             s->mode = SILENCE_COPY;
434             goto silence_copy;
435         }
436         break;
437     case SILENCE_STOP:
438 silence_stop:
439         break;
440     }
441
442     av_frame_free(&in);
443
444     return ret;
445 }
446
447 static int request_frame(AVFilterLink *outlink)
448 {
449     AVFilterContext *ctx = outlink->src;
450     SilenceRemoveContext *s = ctx->priv;
451     int ret;
452
453     ret = ff_request_frame(ctx->inputs[0]);
454     if (ret == AVERROR_EOF && (s->mode == SILENCE_COPY_FLUSH ||
455                                s->mode == SILENCE_COPY)) {
456         int nbs = s->stop_holdoff_end - s->stop_holdoff_offset;
457         if (nbs) {
458             AVFrame *frame;
459
460             frame = ff_get_audio_buffer(outlink, nbs / outlink->channels);
461             if (!frame)
462                 return AVERROR(ENOMEM);
463
464             memcpy(frame->data[0], &s->stop_holdoff[s->stop_holdoff_offset],
465                    nbs * sizeof(double));
466
467             frame->pts = s->next_pts;
468             s->next_pts += av_rescale_q(frame->nb_samples,
469                                         (AVRational){1, outlink->sample_rate},
470                                         outlink->time_base);
471
472             ret = ff_filter_frame(ctx->inputs[0], frame);
473         }
474         s->mode = SILENCE_STOP;
475     }
476     return ret;
477 }
478
479 static int query_formats(AVFilterContext *ctx)
480 {
481     AVFilterFormats *formats = NULL;
482     AVFilterChannelLayouts *layouts = NULL;
483     static const enum AVSampleFormat sample_fmts[] = {
484         AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_NONE
485     };
486     int ret;
487
488     layouts = ff_all_channel_counts();
489     if (!layouts)
490         return AVERROR(ENOMEM);
491     ret = ff_set_common_channel_layouts(ctx, layouts);
492     if (ret < 0)
493         return ret;
494
495     formats = ff_make_format_list(sample_fmts);
496     if (!formats)
497         return AVERROR(ENOMEM);
498     ret = ff_set_common_formats(ctx, formats);
499     if (ret < 0)
500         return ret;
501
502     formats = ff_all_samplerates();
503     if (!formats)
504         return AVERROR(ENOMEM);
505     return ff_set_common_samplerates(ctx, formats);
506 }
507
508 static av_cold void uninit(AVFilterContext *ctx)
509 {
510     SilenceRemoveContext *s = ctx->priv;
511
512     av_freep(&s->start_holdoff);
513     av_freep(&s->stop_holdoff);
514     av_freep(&s->window);
515 }
516
517 static const AVFilterPad silenceremove_inputs[] = {
518     {
519         .name         = "default",
520         .type         = AVMEDIA_TYPE_AUDIO,
521         .config_props = config_input,
522         .filter_frame = filter_frame,
523     },
524     { NULL }
525 };
526
527 static const AVFilterPad silenceremove_outputs[] = {
528     {
529         .name          = "default",
530         .type          = AVMEDIA_TYPE_AUDIO,
531         .request_frame = request_frame,
532     },
533     { NULL }
534 };
535
536 AVFilter ff_af_silenceremove = {
537     .name          = "silenceremove",
538     .description   = NULL_IF_CONFIG_SMALL("Remove silence."),
539     .priv_size     = sizeof(SilenceRemoveContext),
540     .priv_class    = &silenceremove_class,
541     .init          = init,
542     .uninit        = uninit,
543     .query_formats = query_formats,
544     .inputs        = silenceremove_inputs,
545     .outputs       = silenceremove_outputs,
546 };