]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/af_silenceremove.c
avfilter: Constify all AVFilters
[ffmpeg] / libavfilter / af_silenceremove.c
index bc390ab536bba0a95b1daabf405b6a5eb796dffa..e4b4cd71d23ee65bb949214c24e651bb17c4b925 100644 (file)
 #include "avfilter.h"
 #include "internal.h"
 
+enum SilenceDetect {
+    D_PEAK,
+    D_RMS,
+};
+
+enum ThresholdMode {
+    T_ANY,
+    T_ALL,
+};
+
 enum SilenceMode {
     SILENCE_TRIM,
     SILENCE_TRIM_FLUSH,
@@ -49,6 +59,7 @@ typedef struct SilenceRemoveContext {
     double start_threshold;
     int64_t start_silence;
     int64_t start_silence_opt;
+    int start_mode;
 
     int stop_periods;
     int64_t stop_duration;
@@ -56,6 +67,7 @@ typedef struct SilenceRemoveContext {
     double stop_threshold;
     int64_t stop_silence;
     int64_t stop_silence_opt;
+    int stop_mode;
 
     double *start_holdoff;
     double *start_silence_hold;
@@ -92,18 +104,22 @@ typedef struct SilenceRemoveContext {
 #define AF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
 
 static const AVOption silenceremove_options[] = {
-    { "start_periods",   NULL, OFFSET(start_periods),       AV_OPT_TYPE_INT,      {.i64=0},     0,      9000, AF },
-    { "start_duration",  NULL, OFFSET(start_duration_opt),  AV_OPT_TYPE_DURATION, {.i64=0},     0, INT32_MAX, AF },
-    { "start_threshold", NULL, OFFSET(start_threshold),     AV_OPT_TYPE_DOUBLE,   {.dbl=0},     0,   DBL_MAX, AF },
-    { "start_silence",   NULL, OFFSET(start_silence_opt),   AV_OPT_TYPE_DURATION, {.i64=0},     0, INT32_MAX, AF },
-    { "stop_periods",    NULL, OFFSET(stop_periods),        AV_OPT_TYPE_INT,      {.i64=0}, -9000,      9000, AF },
-    { "stop_duration",   NULL, OFFSET(stop_duration_opt),   AV_OPT_TYPE_DURATION, {.i64=0},     0, INT32_MAX, AF },
-    { "stop_threshold",  NULL, OFFSET(stop_threshold),      AV_OPT_TYPE_DOUBLE,   {.dbl=0},     0,   DBL_MAX, AF },
-    { "stop_silence",    NULL, OFFSET(stop_silence_opt),    AV_OPT_TYPE_DURATION, {.i64=0},     0, INT32_MAX, AF },
-    { "detection",       NULL, OFFSET(detection),           AV_OPT_TYPE_INT,      {.i64=1},     0,         1, AF, "detection" },
-    {   "peak",          0,    0,                           AV_OPT_TYPE_CONST,    {.i64=0},     0,         0, AF, "detection" },
-    {   "rms",           0,    0,                           AV_OPT_TYPE_CONST,    {.i64=1},     0,         0, AF, "detection" },
-    { "window",          NULL, OFFSET(window_ratio),        AV_OPT_TYPE_DOUBLE,   {.dbl=0.02},  0,        10, AF },
+    { "start_periods",   NULL,                                                 OFFSET(start_periods),       AV_OPT_TYPE_INT,      {.i64=0},     0,      9000, AF },
+    { "start_duration",  "set start duration of non-silence part",             OFFSET(start_duration_opt),  AV_OPT_TYPE_DURATION, {.i64=0},     0, INT32_MAX, AF },
+    { "start_threshold", "set threshold for start silence detection",          OFFSET(start_threshold),     AV_OPT_TYPE_DOUBLE,   {.dbl=0},     0,   DBL_MAX, AF },
+    { "start_silence",   "set start duration of silence part to keep",         OFFSET(start_silence_opt),   AV_OPT_TYPE_DURATION, {.i64=0},     0, INT32_MAX, AF },
+    { "start_mode",      "set which channel will trigger trimming from start", OFFSET(start_mode),          AV_OPT_TYPE_INT,      {.i64=T_ANY}, T_ANY, T_ALL, AF, "mode" },
+    {   "any",           0,                                                    0,                           AV_OPT_TYPE_CONST,    {.i64=T_ANY}, 0,         0, AF, "mode" },
+    {   "all",           0,                                                    0,                           AV_OPT_TYPE_CONST,    {.i64=T_ALL}, 0,         0, AF, "mode" },
+    { "stop_periods",    NULL,                                                 OFFSET(stop_periods),        AV_OPT_TYPE_INT,      {.i64=0}, -9000,      9000, AF },
+    { "stop_duration",   "set stop duration of non-silence part",              OFFSET(stop_duration_opt),   AV_OPT_TYPE_DURATION, {.i64=0},     0, INT32_MAX, AF },
+    { "stop_threshold",  "set threshold for stop silence detection",           OFFSET(stop_threshold),      AV_OPT_TYPE_DOUBLE,   {.dbl=0},     0,   DBL_MAX, AF },
+    { "stop_silence",    "set stop duration of silence part to keep",          OFFSET(stop_silence_opt),    AV_OPT_TYPE_DURATION, {.i64=0},     0, INT32_MAX, AF },
+    { "stop_mode",       "set which channel will trigger trimming from end",   OFFSET(stop_mode),           AV_OPT_TYPE_INT,      {.i64=T_ANY}, T_ANY, T_ALL, AF, "mode" },
+    { "detection",       "set how silence is detected",                        OFFSET(detection),           AV_OPT_TYPE_INT,      {.i64=D_RMS}, D_PEAK,D_RMS, AF, "detection" },
+    {   "peak",          "use absolute values of samples",                     0,                           AV_OPT_TYPE_CONST,    {.i64=D_PEAK},0,         0, AF, "detection" },
+    {   "rms",           "use squared values of samples",                      0,                           AV_OPT_TYPE_CONST,    {.i64=D_RMS}, 0,         0, AF, "detection" },
+    { "window",          "set duration of window in seconds",                  OFFSET(window_ratio),        AV_OPT_TYPE_DOUBLE,   {.dbl=0.02},  0,        10, AF },
     { NULL }
 };
 
@@ -163,11 +179,11 @@ static av_cold int init(AVFilterContext *ctx)
     }
 
     switch (s->detection) {
-    case 0:
+    case D_PEAK:
         s->update = update_peak;
         s->compute = compute_peak;
         break;
-    case 1:
+    case D_RMS:
         s->update = update_rms;
         s->compute = compute_rms;
         break;
@@ -190,6 +206,7 @@ static int config_input(AVFilterLink *inlink)
     AVFilterContext *ctx = inlink->dst;
     SilenceRemoveContext *s = ctx->priv;
 
+    s->next_pts = AV_NOPTS_VALUE;
     s->window_size = FFMAX((inlink->sample_rate * s->window_ratio), 1) * inlink->channels;
     s->window = av_malloc_array(s->window_size, sizeof(*s->window));
     if (!s->window)
@@ -312,27 +329,37 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 
     nb_samples_read = nb_samples_written = 0;
 
+    if (s->next_pts == AV_NOPTS_VALUE)
+        s->next_pts = in->pts;
+
     switch (s->mode) {
     case SILENCE_TRIM:
 silence_trim:
-        nbs = in->nb_samples - nb_samples_read / inlink->channels;
+        nbs = in->nb_samples - nb_samples_read / outlink->channels;
         if (!nbs)
             break;
 
         for (i = 0; i < nbs; i++) {
-            threshold = 0;
-            for (j = 0; j < inlink->channels; j++) {
-                threshold |= s->compute(s, ibuf[j]) > s->start_threshold;
+            if (s->start_mode == T_ANY) {
+                threshold = 0;
+                for (j = 0; j < outlink->channels; j++) {
+                    threshold |= s->compute(s, ibuf[j]) > s->start_threshold;
+                }
+            } else {
+                threshold = 1;
+                for (j = 0; j < outlink->channels; j++) {
+                    threshold &= s->compute(s, ibuf[j]) > s->start_threshold;
+                }
             }
 
             if (threshold) {
-                for (j = 0; j < inlink->channels; j++) {
+                for (j = 0; j < outlink->channels; j++) {
                     s->update(s, *ibuf);
                     s->start_holdoff[s->start_holdoff_end++] = *ibuf++;
                 }
-                nb_samples_read += inlink->channels;
+                nb_samples_read += outlink->channels;
 
-                if (s->start_holdoff_end >= s->start_duration * inlink->channels) {
+                if (s->start_holdoff_end >= s->start_duration * outlink->channels) {
                     if (++s->start_found_periods >= s->start_periods) {
                         s->mode = SILENCE_TRIM_FLUSH;
                         goto silence_trim_flush;
@@ -346,19 +373,19 @@ silence_trim:
             } else {
                 s->start_holdoff_end = 0;
 
-                for (j = 0; j < inlink->channels; j++) {
+                for (j = 0; j < outlink->channels; j++) {
                     s->update(s, ibuf[j]);
                     if (s->start_silence) {
                         s->start_silence_hold[s->start_silence_offset++] = ibuf[j];
-                        s->start_silence_end = FFMIN(s->start_silence_end + 1, inlink->channels * s->start_silence);
-                        if (s->start_silence_offset >= inlink->channels * s->start_silence) {
+                        s->start_silence_end = FFMIN(s->start_silence_end + 1, outlink->channels * s->start_silence);
+                        if (s->start_silence_offset >= outlink->channels * s->start_silence) {
                             s->start_silence_offset = 0;
                         }
                     }
                 }
 
-                ibuf += inlink->channels;
-                nb_samples_read += inlink->channels;
+                ibuf += outlink->channels;
+                nb_samples_read += outlink->channels;
             }
         }
         break;
@@ -366,11 +393,11 @@ silence_trim:
     case SILENCE_TRIM_FLUSH:
 silence_trim_flush:
         nbs  = s->start_holdoff_end - s->start_holdoff_offset;
-        nbs -= nbs % inlink->channels;
+        nbs -= nbs % outlink->channels;
         if (!nbs)
             break;
 
-        out = ff_get_audio_buffer(inlink, nbs / inlink->channels + s->start_silence_end / inlink->channels);
+        out = ff_get_audio_buffer(outlink, nbs / outlink->channels + s->start_silence_end / outlink->channels);
         if (!out) {
             av_frame_free(&in);
             return AVERROR(ENOMEM);
@@ -415,11 +442,11 @@ silence_trim_flush:
 
     case SILENCE_COPY:
 silence_copy:
-        nbs = in->nb_samples - nb_samples_read / inlink->channels;
+        nbs = in->nb_samples - nb_samples_read / outlink->channels;
         if (!nbs)
             break;
 
-        out = ff_get_audio_buffer(inlink, nbs);
+        out = ff_get_audio_buffer(outlink, nbs);
         if (!out) {
             av_frame_free(&in);
             return AVERROR(ENOMEM);
@@ -428,37 +455,45 @@ silence_copy:
 
         if (s->stop_periods) {
             for (i = 0; i < nbs; i++) {
-                threshold = 1;
-                for (j = 0; j < inlink->channels; j++)
-                    threshold &= s->compute(s, ibuf[j]) > s->stop_threshold;
+                if (s->stop_mode == T_ANY) {
+                    threshold = 0;
+                    for (j = 0; j < outlink->channels; j++) {
+                        threshold |= s->compute(s, ibuf[j]) > s->stop_threshold;
+                    }
+                } else {
+                    threshold = 1;
+                    for (j = 0; j < outlink->channels; j++) {
+                        threshold &= s->compute(s, ibuf[j]) > s->stop_threshold;
+                    }
+                }
 
                 if (threshold && s->stop_holdoff_end && !s->stop_silence) {
                     s->mode = SILENCE_COPY_FLUSH;
                     flush(s, out, outlink, &nb_samples_written, &ret, 0);
                     goto silence_copy_flush;
                 } else if (threshold) {
-                    for (j = 0; j < inlink->channels; j++) {
+                    for (j = 0; j < outlink->channels; j++) {
                         s->update(s, *ibuf);
                         *obuf++ = *ibuf++;
                     }
-                    nb_samples_read    += inlink->channels;
-                    nb_samples_written += inlink->channels;
+                    nb_samples_read    += outlink->channels;
+                    nb_samples_written += outlink->channels;
                 } else if (!threshold) {
-                    for (j = 0; j < inlink->channels; j++) {
+                    for (j = 0; j < outlink->channels; j++) {
                         s->update(s, *ibuf);
                         if (s->stop_silence) {
                             s->stop_silence_hold[s->stop_silence_offset++] = *ibuf;
-                            s->stop_silence_end = FFMIN(s->stop_silence_end + 1, inlink->channels * s->stop_silence);
-                            if (s->stop_silence_offset >= inlink->channels * s->stop_silence) {
+                            s->stop_silence_end = FFMIN(s->stop_silence_end + 1, outlink->channels * s->stop_silence);
+                            if (s->stop_silence_offset >= outlink->channels * s->stop_silence) {
                                 s->stop_silence_offset = 0;
                             }
                         }
 
                         s->stop_holdoff[s->stop_holdoff_end++] = *ibuf++;
                     }
-                    nb_samples_read += inlink->channels;
+                    nb_samples_read += outlink->channels;
 
-                    if (s->stop_holdoff_end >= s->stop_duration * inlink->channels) {
+                    if (s->stop_holdoff_end >= s->stop_duration * outlink->channels) {
                         if (++s->stop_found_periods >= s->stop_periods) {
                             s->stop_holdoff_offset = 0;
                             s->stop_holdoff_end = 0;
@@ -488,7 +523,7 @@ silence_copy:
             }
             flush(s, out, outlink, &nb_samples_written, &ret, 0);
         } else {
-            memcpy(obuf, ibuf, sizeof(double) * nbs * inlink->channels);
+            memcpy(obuf, ibuf, sizeof(double) * nbs * outlink->channels);
 
             out->pts = s->next_pts;
             s->next_pts += av_rescale_q(out->nb_samples,
@@ -502,11 +537,11 @@ silence_copy:
     case SILENCE_COPY_FLUSH:
 silence_copy_flush:
         nbs  = s->stop_holdoff_end - s->stop_holdoff_offset;
-        nbs -= nbs % inlink->channels;
+        nbs -= nbs % outlink->channels;
         if (!nbs)
             break;
 
-        out = ff_get_audio_buffer(inlink, nbs / inlink->channels);
+        out = ff_get_audio_buffer(outlink, nbs / outlink->channels);
         if (!out) {
             av_frame_free(&in);
             return AVERROR(ENOMEM);
@@ -633,7 +668,7 @@ static const AVFilterPad silenceremove_outputs[] = {
     { NULL }
 };
 
-AVFilter ff_af_silenceremove = {
+const AVFilter ff_af_silenceremove = {
     .name          = "silenceremove",
     .description   = NULL_IF_CONFIG_SMALL("Remove silence."),
     .priv_size     = sizeof(SilenceRemoveContext),