2014-07-02 09:39:07 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2001 Heikki Leinonen
|
|
|
|
* Copyright (c) 2001 Chris Bagwell
|
|
|
|
* Copyright (c) 2003 Donnie Smith
|
|
|
|
* Copyright (c) 2014 Paul B Mahol
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <float.h> /* DBL_MAX */
|
|
|
|
|
2021-09-08 13:15:19 +00:00
|
|
|
#include "libavutil/audio_fifo.h"
|
2021-09-08 07:42:27 +00:00
|
|
|
#include "libavutil/avassert.h"
|
2014-07-02 09:39:07 +00:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/timestamp.h"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "formats.h"
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
2018-10-15 15:44:56 +00:00
|
|
|
enum SilenceDetect {
|
|
|
|
D_PEAK,
|
|
|
|
D_RMS,
|
|
|
|
};
|
|
|
|
|
2018-10-15 15:52:56 +00:00
|
|
|
enum ThresholdMode {
|
|
|
|
T_ANY,
|
|
|
|
T_ALL,
|
|
|
|
};
|
|
|
|
|
2014-07-02 09:39:07 +00:00
|
|
|
enum SilenceMode {
|
|
|
|
SILENCE_TRIM,
|
|
|
|
SILENCE_TRIM_FLUSH,
|
|
|
|
SILENCE_COPY,
|
|
|
|
SILENCE_COPY_FLUSH,
|
|
|
|
SILENCE_STOP
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct SilenceRemoveContext {
|
|
|
|
const AVClass *class;
|
|
|
|
|
|
|
|
enum SilenceMode mode;
|
|
|
|
|
|
|
|
int start_periods;
|
|
|
|
int64_t start_duration;
|
2018-10-08 18:43:14 +00:00
|
|
|
int64_t start_duration_opt;
|
2014-07-02 09:39:07 +00:00
|
|
|
double start_threshold;
|
2018-10-08 18:43:14 +00:00
|
|
|
int64_t start_silence;
|
|
|
|
int64_t start_silence_opt;
|
2018-10-15 14:05:04 +00:00
|
|
|
int start_mode;
|
2014-07-02 09:39:07 +00:00
|
|
|
|
|
|
|
int stop_periods;
|
|
|
|
int64_t stop_duration;
|
2018-10-08 18:43:14 +00:00
|
|
|
int64_t stop_duration_opt;
|
2014-07-02 09:39:07 +00:00
|
|
|
double stop_threshold;
|
2018-10-08 18:43:14 +00:00
|
|
|
int64_t stop_silence;
|
|
|
|
int64_t stop_silence_opt;
|
2018-10-15 14:05:04 +00:00
|
|
|
int stop_mode;
|
2014-07-02 09:39:07 +00:00
|
|
|
|
2021-09-07 17:20:38 +00:00
|
|
|
int64_t window_duration_opt;
|
|
|
|
|
2021-07-25 13:38:33 +00:00
|
|
|
AVFrame *start_holdoff;
|
|
|
|
AVFrame *start_silence_hold;
|
2014-07-02 09:39:07 +00:00
|
|
|
size_t start_holdoff_offset;
|
|
|
|
size_t start_holdoff_end;
|
2018-10-08 18:43:14 +00:00
|
|
|
size_t start_silence_offset;
|
|
|
|
size_t start_silence_end;
|
2014-07-02 09:39:07 +00:00
|
|
|
int start_found_periods;
|
|
|
|
|
2021-07-25 13:38:33 +00:00
|
|
|
AVFrame *stop_holdoff;
|
|
|
|
AVFrame *stop_silence_hold;
|
2014-07-02 09:39:07 +00:00
|
|
|
size_t stop_holdoff_offset;
|
|
|
|
size_t stop_holdoff_end;
|
2018-10-08 18:43:14 +00:00
|
|
|
size_t stop_silence_offset;
|
|
|
|
size_t stop_silence_end;
|
2014-07-02 09:39:07 +00:00
|
|
|
int stop_found_periods;
|
|
|
|
|
2021-07-25 19:50:31 +00:00
|
|
|
AVFrame *window;
|
|
|
|
int window_offset;
|
2021-09-07 17:20:38 +00:00
|
|
|
int64_t window_duration;
|
2015-12-27 19:45:46 +00:00
|
|
|
double sum;
|
2014-07-02 09:39:07 +00:00
|
|
|
|
2021-09-08 11:06:43 +00:00
|
|
|
int one_period;
|
2014-07-02 09:39:07 +00:00
|
|
|
int restart;
|
|
|
|
int64_t next_pts;
|
2015-12-27 19:45:46 +00:00
|
|
|
|
|
|
|
int detection;
|
2021-07-25 13:38:33 +00:00
|
|
|
void (*update)(struct SilenceRemoveContext *s, AVFrame *frame, int ch, int offset);
|
|
|
|
double (*compute)(struct SilenceRemoveContext *s, AVFrame *frame, int ch, int offset);
|
|
|
|
void (*copy)(struct SilenceRemoveContext *s, AVFrame *out, AVFrame *in,
|
|
|
|
int ch, int out_offset, int in_offset);
|
2021-09-08 13:15:19 +00:00
|
|
|
|
|
|
|
AVAudioFifo *fifo;
|
2014-07-02 09:39:07 +00:00
|
|
|
} SilenceRemoveContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(SilenceRemoveContext, x)
|
2018-10-08 18:43:14 +00:00
|
|
|
#define AF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
|
|
|
|
|
2014-07-02 09:39:07 +00:00
|
|
|
static const AVOption silenceremove_options[] = {
|
2021-09-08 11:06:43 +00:00
|
|
|
{ "start_periods", "set periods of silence parts to skip from start", OFFSET(start_periods), AV_OPT_TYPE_INT, {.i64=0}, 0, 9000, AF },
|
2018-10-15 14:26:16 +00:00
|
|
|
{ "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 },
|
2018-10-15 15:52:56 +00:00
|
|
|
{ "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" },
|
2021-09-08 11:06:43 +00:00
|
|
|
{ "stop_periods", "set periods of silence parts to skip from end", OFFSET(stop_periods), AV_OPT_TYPE_INT, {.i64=0}, -9000, 9000, AF },
|
2018-10-15 14:26:16 +00:00
|
|
|
{ "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 },
|
2018-10-15 15:52:56 +00:00
|
|
|
{ "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" },
|
2018-10-15 15:44:56 +00:00
|
|
|
{ "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" },
|
2021-09-07 17:20:38 +00:00
|
|
|
{ "window", "set duration of window for silence detection", OFFSET(window_duration_opt), AV_OPT_TYPE_DURATION, {.i64=20000}, 0, 100000000, AF },
|
2014-07-02 09:39:07 +00:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(silenceremove);
|
|
|
|
|
2021-07-25 13:38:33 +00:00
|
|
|
static void copy_double(SilenceRemoveContext *s, AVFrame *out, AVFrame *in,
|
|
|
|
int ch, int out_offset, int in_offset)
|
2015-12-27 19:45:46 +00:00
|
|
|
{
|
2021-07-25 13:38:33 +00:00
|
|
|
const double *srcp = (const double *)in->data[0];
|
|
|
|
const double src = srcp[in->channels * in_offset + ch];
|
|
|
|
double *dstp = (double *)out->data[0];
|
|
|
|
|
|
|
|
dstp[out->channels * out_offset + ch] = src;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void copy_doublep(SilenceRemoveContext *s, AVFrame *out, AVFrame *in,
|
|
|
|
int ch, int out_offset, int in_offset)
|
|
|
|
{
|
2021-07-25 19:50:31 +00:00
|
|
|
const double *srcp = (const double *)in->extended_data[ch];
|
2021-07-25 13:38:33 +00:00
|
|
|
const double src = srcp[in_offset];
|
2021-07-25 19:50:31 +00:00
|
|
|
double *dstp = (double *)out->extended_data[ch];
|
2021-07-25 13:38:33 +00:00
|
|
|
|
|
|
|
dstp[out_offset] = src;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void copy_float(SilenceRemoveContext *s, AVFrame *out, AVFrame *in,
|
|
|
|
int ch, int out_offset, int in_offset)
|
|
|
|
{
|
|
|
|
const float *srcp = (const float *)in->data[0];
|
|
|
|
const float src = srcp[in->channels * in_offset + ch];
|
|
|
|
float *dstp = (float *)out->data[0];
|
|
|
|
|
|
|
|
dstp[out->channels * out_offset + ch] = src;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void copy_floatp(SilenceRemoveContext *s, AVFrame *out, AVFrame *in,
|
|
|
|
int ch, int out_offset, int in_offset)
|
|
|
|
{
|
2021-07-25 19:50:31 +00:00
|
|
|
const float *srcp = (const float *)in->extended_data[ch];
|
2021-07-25 13:38:33 +00:00
|
|
|
const float src = srcp[in_offset];
|
2021-07-25 19:50:31 +00:00
|
|
|
float *dstp = (float *)out->extended_data[ch];
|
2021-07-25 13:38:33 +00:00
|
|
|
|
|
|
|
dstp[out_offset] = src;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double compute_peak_double(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
|
|
|
|
{
|
|
|
|
const double *samples = (const double *)frame->data[0];
|
2021-07-25 19:50:31 +00:00
|
|
|
const double *wsamples = (const double *)s->window->data[0];
|
2021-07-25 13:38:33 +00:00
|
|
|
double sample = samples[frame->channels * offset + ch];
|
2021-07-25 19:50:31 +00:00
|
|
|
double wsample = wsamples[frame->channels * s->window_offset + ch];
|
2015-12-27 19:45:46 +00:00
|
|
|
double new_sum;
|
|
|
|
|
|
|
|
new_sum = s->sum;
|
2021-07-25 19:50:31 +00:00
|
|
|
new_sum -= wsample;
|
2021-09-08 08:05:53 +00:00
|
|
|
new_sum = fmax(new_sum, 0.);
|
2015-12-27 19:45:46 +00:00
|
|
|
new_sum += fabs(sample);
|
|
|
|
|
2021-09-07 17:20:38 +00:00
|
|
|
return new_sum / s->window_duration;
|
2015-12-27 19:45:46 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 13:38:33 +00:00
|
|
|
static void update_peak_double(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
|
|
|
|
{
|
|
|
|
const double *samples = (const double *)frame->data[0];
|
2021-07-25 19:50:31 +00:00
|
|
|
double *wsamples = (double *)s->window->data[0];
|
2021-07-25 13:38:33 +00:00
|
|
|
double sample = samples[frame->channels * offset + ch];
|
2021-07-25 19:50:31 +00:00
|
|
|
double *wsample = &wsamples[frame->channels * s->window_offset + ch];
|
2021-07-25 13:38:33 +00:00
|
|
|
|
2021-07-25 19:50:31 +00:00
|
|
|
s->sum -= *wsample;
|
2021-09-08 08:05:53 +00:00
|
|
|
s->sum = fmax(s->sum, 0.);
|
2021-07-25 19:50:31 +00:00
|
|
|
*wsample = fabs(sample);
|
|
|
|
s->sum += *wsample;
|
2021-07-25 13:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static double compute_peak_float(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
|
|
|
|
{
|
|
|
|
const float *samples = (const float *)frame->data[0];
|
2021-07-25 19:50:31 +00:00
|
|
|
const float *wsamples = (const float *)s->window->data[0];
|
2021-07-25 13:38:33 +00:00
|
|
|
float sample = samples[frame->channels * offset + ch];
|
2021-07-25 19:50:31 +00:00
|
|
|
float wsample = wsamples[frame->channels * s->window_offset + ch];
|
2021-07-25 13:38:33 +00:00
|
|
|
float new_sum;
|
|
|
|
|
|
|
|
new_sum = s->sum;
|
2021-07-25 19:50:31 +00:00
|
|
|
new_sum -= wsample;
|
2021-09-08 08:05:53 +00:00
|
|
|
new_sum = fmaxf(new_sum, 0.f);
|
2021-07-25 19:50:31 +00:00
|
|
|
new_sum += fabsf(sample);
|
2021-07-25 13:38:33 +00:00
|
|
|
|
2021-09-07 17:20:38 +00:00
|
|
|
return new_sum / s->window_duration;
|
2021-07-25 13:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void update_peak_float(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
|
2015-12-27 19:45:46 +00:00
|
|
|
{
|
2021-07-25 13:38:33 +00:00
|
|
|
const float *samples = (const float *)frame->data[0];
|
2021-07-25 19:50:31 +00:00
|
|
|
float *wsamples = (float *)s->window->data[0];
|
2021-07-25 13:38:33 +00:00
|
|
|
float sample = samples[frame->channels * offset + ch];
|
2021-07-25 19:50:31 +00:00
|
|
|
float *wsample = &wsamples[frame->channels * s->window_offset + ch];
|
2021-07-25 13:38:33 +00:00
|
|
|
|
2021-07-25 19:50:31 +00:00
|
|
|
s->sum -= *wsample;
|
2021-09-08 08:05:53 +00:00
|
|
|
s->sum = fmaxf(s->sum, 0.f);
|
2021-07-25 19:50:31 +00:00
|
|
|
*wsample = fabsf(sample);
|
|
|
|
s->sum += *wsample;
|
2015-12-27 19:45:46 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 13:38:33 +00:00
|
|
|
static double compute_rms_double(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
|
2015-12-27 19:45:46 +00:00
|
|
|
{
|
2021-07-25 13:38:33 +00:00
|
|
|
const double *samples = (const double *)frame->data[0];
|
2021-07-25 19:50:31 +00:00
|
|
|
const double *wsamples = (const double *)s->window->data[0];
|
2021-07-25 13:38:33 +00:00
|
|
|
double sample = samples[frame->channels * offset + ch];
|
2021-07-25 19:50:31 +00:00
|
|
|
double wsample = wsamples[frame->channels * s->window_offset + ch];
|
2015-12-27 19:45:46 +00:00
|
|
|
double new_sum;
|
|
|
|
|
|
|
|
new_sum = s->sum;
|
2021-07-25 19:50:31 +00:00
|
|
|
new_sum -= wsample;
|
2021-09-08 08:05:53 +00:00
|
|
|
new_sum = fmax(new_sum, 0.);
|
2015-12-27 19:45:46 +00:00
|
|
|
new_sum += sample * sample;
|
|
|
|
|
2021-09-08 07:42:27 +00:00
|
|
|
av_assert2(new_sum >= 0.);
|
2021-09-07 17:20:38 +00:00
|
|
|
return sqrt(new_sum / s->window_duration);
|
2015-12-27 19:45:46 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 13:38:33 +00:00
|
|
|
static void update_rms_double(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
|
|
|
|
{
|
|
|
|
const double *samples = (const double *)frame->data[0];
|
2021-07-25 19:50:31 +00:00
|
|
|
double *wsamples = (double *)s->window->data[0];
|
2021-07-25 13:38:33 +00:00
|
|
|
double sample = samples[frame->channels * offset + ch];
|
2021-07-25 19:50:31 +00:00
|
|
|
double *wsample = &wsamples[frame->channels * s->window_offset + ch];
|
2021-07-25 13:38:33 +00:00
|
|
|
|
2021-07-25 19:50:31 +00:00
|
|
|
s->sum -= *wsample;
|
2021-09-08 08:05:53 +00:00
|
|
|
s->sum = fmax(s->sum, 0.);
|
2021-07-25 19:50:31 +00:00
|
|
|
*wsample = sample * sample;
|
|
|
|
s->sum += *wsample;
|
2021-07-25 13:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static double compute_rms_float(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
|
|
|
|
{
|
|
|
|
const float *samples = (const float *)frame->data[0];
|
2021-07-25 19:50:31 +00:00
|
|
|
const float *wsamples = (const float *)s->window->data[0];
|
2021-07-25 13:38:33 +00:00
|
|
|
float sample = samples[frame->channels * offset + ch];
|
2021-07-25 19:50:31 +00:00
|
|
|
float wsample = wsamples[frame->channels * s->window_offset + ch];
|
2021-07-25 13:38:33 +00:00
|
|
|
float new_sum;
|
|
|
|
|
|
|
|
new_sum = s->sum;
|
2021-07-25 19:50:31 +00:00
|
|
|
new_sum -= wsample;
|
2021-09-08 08:05:53 +00:00
|
|
|
new_sum = fmaxf(new_sum, 0.f);
|
2021-07-25 13:38:33 +00:00
|
|
|
new_sum += sample * sample;
|
|
|
|
|
2021-09-08 07:42:27 +00:00
|
|
|
av_assert2(new_sum >= 0.f);
|
2021-09-07 17:20:38 +00:00
|
|
|
return sqrtf(new_sum / s->window_duration);
|
2021-07-25 13:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void update_rms_float(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
|
2015-12-27 19:45:46 +00:00
|
|
|
{
|
2021-07-25 13:38:33 +00:00
|
|
|
const float *samples = (const float *)frame->data[0];
|
|
|
|
float sample = samples[frame->channels * offset + ch];
|
2021-07-25 19:50:31 +00:00
|
|
|
float *wsamples = (float *)s->window->data[0];
|
|
|
|
float *wsample = &wsamples[frame->channels * s->window_offset + ch];
|
2021-07-25 13:38:33 +00:00
|
|
|
|
2021-07-25 19:50:31 +00:00
|
|
|
s->sum -= *wsample;
|
2021-09-08 08:05:53 +00:00
|
|
|
s->sum = fmaxf(s->sum, 0.f);
|
2021-07-25 19:50:31 +00:00
|
|
|
*wsample = sample * sample;
|
|
|
|
s->sum += *wsample;
|
2021-07-25 13:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static double compute_peak_doublep(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
|
|
|
|
{
|
2021-07-25 19:50:31 +00:00
|
|
|
const double *samples = (const double *)frame->extended_data[ch];
|
|
|
|
const double *wsamples = (const double *)s->window->extended_data[ch];
|
2021-07-25 13:38:33 +00:00
|
|
|
double sample = samples[offset];
|
2021-07-25 19:50:31 +00:00
|
|
|
double wsample = wsamples[s->window_offset];
|
2021-07-25 13:38:33 +00:00
|
|
|
double new_sum;
|
|
|
|
|
|
|
|
new_sum = s->sum;
|
2021-07-25 19:50:31 +00:00
|
|
|
new_sum -= wsample;
|
2021-09-08 08:05:53 +00:00
|
|
|
new_sum = fmax(new_sum, 0.);
|
2021-07-25 13:38:33 +00:00
|
|
|
new_sum += fabs(sample);
|
|
|
|
|
2021-09-07 17:20:38 +00:00
|
|
|
return new_sum / s->window_duration;
|
2021-07-25 13:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void update_peak_doublep(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
|
|
|
|
{
|
2021-07-25 19:50:31 +00:00
|
|
|
const double *samples = (const double *)frame->extended_data[ch];
|
|
|
|
double *wsamples = (double *)s->window->extended_data[ch];
|
2021-07-25 13:38:33 +00:00
|
|
|
double sample = samples[offset];
|
2021-07-25 19:50:31 +00:00
|
|
|
double *wsample = &wsamples[s->window_offset];
|
2021-07-25 13:38:33 +00:00
|
|
|
|
2021-07-25 19:50:31 +00:00
|
|
|
s->sum -= *wsample;
|
2021-09-08 08:05:53 +00:00
|
|
|
s->sum = fmax(s->sum, 0.);
|
2021-07-25 19:50:31 +00:00
|
|
|
*wsample = fabs(sample);
|
|
|
|
s->sum += *wsample;
|
2021-07-25 13:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static double compute_peak_floatp(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
|
|
|
|
{
|
2021-07-25 19:50:31 +00:00
|
|
|
const float *samples = (const float *)frame->extended_data[ch];
|
|
|
|
const float *wsamples = (const float *)s->window->extended_data[ch];
|
2021-07-25 13:38:33 +00:00
|
|
|
float sample = samples[offset];
|
2021-07-25 19:50:31 +00:00
|
|
|
float wsample = wsamples[s->window_offset];
|
2021-07-25 13:38:33 +00:00
|
|
|
float new_sum;
|
|
|
|
|
|
|
|
new_sum = s->sum;
|
2021-07-25 19:50:31 +00:00
|
|
|
new_sum -= wsample;
|
2021-09-08 08:05:53 +00:00
|
|
|
new_sum = fmaxf(new_sum, 0.f);
|
2021-07-25 19:50:31 +00:00
|
|
|
new_sum += fabsf(sample);
|
2021-07-25 13:38:33 +00:00
|
|
|
|
2021-09-07 17:20:38 +00:00
|
|
|
return new_sum / s->window_duration;
|
2021-07-25 13:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void update_peak_floatp(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
|
|
|
|
{
|
2021-07-25 19:50:31 +00:00
|
|
|
const float *samples = (const float *)frame->extended_data[ch];
|
|
|
|
float *wsamples = (float *)s->window->extended_data[ch];
|
2021-07-25 13:38:33 +00:00
|
|
|
float sample = samples[offset];
|
2021-07-25 19:50:31 +00:00
|
|
|
float *wsample = &wsamples[s->window_offset];
|
2021-07-25 13:38:33 +00:00
|
|
|
|
2021-07-25 19:50:31 +00:00
|
|
|
s->sum -= *wsample;
|
2021-09-08 08:05:53 +00:00
|
|
|
s->sum = fmaxf(s->sum, 0.f);
|
2021-07-25 19:50:31 +00:00
|
|
|
*wsample = fabsf(sample);
|
|
|
|
s->sum += *wsample;
|
2021-07-25 13:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static double compute_rms_doublep(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
|
|
|
|
{
|
2021-07-25 19:50:31 +00:00
|
|
|
const double *samples = (const double *)frame->extended_data[ch];
|
|
|
|
const double *wsamples = (const double *)s->window->extended_data[ch];
|
2021-07-25 13:38:33 +00:00
|
|
|
double sample = samples[offset];
|
2021-07-25 19:50:31 +00:00
|
|
|
double wsample = wsamples[s->window_offset];
|
2021-07-25 13:38:33 +00:00
|
|
|
double new_sum;
|
|
|
|
|
|
|
|
new_sum = s->sum;
|
2021-07-25 19:50:31 +00:00
|
|
|
new_sum -= wsample;
|
2021-09-08 08:05:53 +00:00
|
|
|
new_sum = fmax(new_sum, 0.);
|
2021-07-25 13:38:33 +00:00
|
|
|
new_sum += sample * sample;
|
|
|
|
|
2021-09-08 07:42:27 +00:00
|
|
|
av_assert2(new_sum >= 0.);
|
2021-09-07 17:20:38 +00:00
|
|
|
return sqrt(new_sum / s->window_duration);
|
2021-07-25 13:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void update_rms_doublep(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
|
|
|
|
{
|
2021-07-25 19:50:31 +00:00
|
|
|
const double *samples = (const double *)frame->extended_data[ch];
|
|
|
|
double *wsamples = (double *)s->window->extended_data[ch];
|
2021-07-25 13:38:33 +00:00
|
|
|
double sample = samples[offset];
|
2021-07-25 19:50:31 +00:00
|
|
|
double *wsample = &wsamples[s->window_offset];
|
2021-07-25 13:38:33 +00:00
|
|
|
|
2021-07-25 19:50:31 +00:00
|
|
|
s->sum -= *wsample;
|
2021-09-08 08:05:53 +00:00
|
|
|
s->sum = fmax(s->sum, 0.);
|
2021-07-25 19:50:31 +00:00
|
|
|
*wsample = sample * sample;
|
|
|
|
s->sum += *wsample;
|
2021-07-25 13:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static double compute_rms_floatp(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
|
|
|
|
{
|
2021-07-25 19:50:31 +00:00
|
|
|
const float *samples = (const float *)frame->extended_data[ch];
|
|
|
|
const float *wsamples = (const float *)s->window->extended_data[ch];
|
2021-07-25 13:38:33 +00:00
|
|
|
float sample = samples[offset];
|
2021-07-25 19:50:31 +00:00
|
|
|
float wsample = wsamples[s->window_offset];
|
2021-07-25 13:38:33 +00:00
|
|
|
float new_sum;
|
|
|
|
|
|
|
|
new_sum = s->sum;
|
2021-07-25 19:50:31 +00:00
|
|
|
new_sum -= wsample;
|
2021-09-08 08:05:53 +00:00
|
|
|
new_sum = fmaxf(new_sum, 0.f);
|
2021-07-25 13:38:33 +00:00
|
|
|
new_sum += sample * sample;
|
|
|
|
|
2021-09-08 07:42:27 +00:00
|
|
|
av_assert2(new_sum >= 0.f);
|
2021-09-07 17:20:38 +00:00
|
|
|
return sqrtf(new_sum / s->window_duration);
|
2021-07-25 13:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void update_rms_floatp(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset)
|
|
|
|
{
|
2021-07-25 19:50:31 +00:00
|
|
|
const float *samples = (const float *)frame->extended_data[ch];
|
|
|
|
float *wsamples = (float *)s->window->extended_data[ch];
|
2021-07-25 13:38:33 +00:00
|
|
|
float sample = samples[offset];
|
2021-07-25 19:50:31 +00:00
|
|
|
float *wsample = &wsamples[s->window_offset];
|
2021-07-25 13:38:33 +00:00
|
|
|
|
2021-07-25 19:50:31 +00:00
|
|
|
s->sum -= *wsample;
|
2021-09-08 08:05:53 +00:00
|
|
|
s->sum = fmaxf(s->sum, 0.f);
|
2021-07-25 19:50:31 +00:00
|
|
|
*wsample = sample * sample;
|
|
|
|
s->sum += *wsample;
|
2015-12-27 19:45:46 +00:00
|
|
|
}
|
|
|
|
|
2014-07-02 09:39:07 +00:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
SilenceRemoveContext *s = ctx->priv;
|
|
|
|
|
|
|
|
if (s->stop_periods < 0) {
|
|
|
|
s->stop_periods = -s->stop_periods;
|
|
|
|
s->restart = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-27 19:45:46 +00:00
|
|
|
static void clear_window(SilenceRemoveContext *s)
|
2014-07-02 09:39:07 +00:00
|
|
|
{
|
2021-09-07 17:20:38 +00:00
|
|
|
av_samples_set_silence(s->window->extended_data, 0, s->window_duration,
|
2021-07-25 19:50:31 +00:00
|
|
|
s->window->channels, s->window->format);
|
2014-07-02 09:39:07 +00:00
|
|
|
|
2021-07-25 19:50:31 +00:00
|
|
|
s->window_offset = 0;
|
2015-12-27 19:45:46 +00:00
|
|
|
s->sum = 0;
|
2014-07-02 09:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int config_input(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
SilenceRemoveContext *s = ctx->priv;
|
|
|
|
|
2019-07-02 16:06:28 +00:00
|
|
|
s->next_pts = AV_NOPTS_VALUE;
|
2021-09-07 17:20:38 +00:00
|
|
|
s->window_duration = av_rescale(s->window_duration_opt, inlink->sample_rate,
|
|
|
|
AV_TIME_BASE);
|
|
|
|
s->window_duration = FFMAX(1, s->window_duration);
|
|
|
|
s->window = ff_get_audio_buffer(ctx->outputs[0], s->window_duration);
|
2014-07-02 09:39:07 +00:00
|
|
|
if (!s->window)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2015-12-27 19:45:46 +00:00
|
|
|
clear_window(s);
|
2014-07-02 09:39:07 +00:00
|
|
|
|
2018-10-08 18:43:14 +00:00
|
|
|
s->start_duration = av_rescale(s->start_duration_opt, inlink->sample_rate,
|
2014-07-02 09:39:07 +00:00
|
|
|
AV_TIME_BASE);
|
2018-10-08 18:43:14 +00:00
|
|
|
s->start_silence = av_rescale(s->start_silence_opt, inlink->sample_rate,
|
|
|
|
AV_TIME_BASE);
|
|
|
|
s->stop_duration = av_rescale(s->stop_duration_opt, inlink->sample_rate,
|
|
|
|
AV_TIME_BASE);
|
|
|
|
s->stop_silence = av_rescale(s->stop_silence_opt, inlink->sample_rate,
|
2014-07-02 09:39:07 +00:00
|
|
|
AV_TIME_BASE);
|
|
|
|
|
2021-07-25 13:38:33 +00:00
|
|
|
s->start_holdoff = ff_get_audio_buffer(ctx->outputs[0],
|
|
|
|
FFMAX(s->start_duration, 1));
|
2014-07-02 09:39:07 +00:00
|
|
|
if (!s->start_holdoff)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2021-07-25 13:38:33 +00:00
|
|
|
s->start_silence_hold = ff_get_audio_buffer(ctx->outputs[0],
|
|
|
|
FFMAX(s->start_silence, 1));
|
2018-10-08 18:43:14 +00:00
|
|
|
if (!s->start_silence_hold)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2014-07-02 09:39:07 +00:00
|
|
|
s->start_holdoff_offset = 0;
|
|
|
|
s->start_holdoff_end = 0;
|
|
|
|
s->start_found_periods = 0;
|
|
|
|
|
2021-07-25 13:38:33 +00:00
|
|
|
s->stop_holdoff = ff_get_audio_buffer(ctx->outputs[0],
|
|
|
|
FFMAX(s->stop_duration, 1));
|
2014-07-02 09:39:07 +00:00
|
|
|
if (!s->stop_holdoff)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2021-07-25 13:38:33 +00:00
|
|
|
s->stop_silence_hold = ff_get_audio_buffer(ctx->outputs[0],
|
|
|
|
FFMAX(s->stop_silence, 1));
|
2018-10-08 18:43:14 +00:00
|
|
|
if (!s->stop_silence_hold)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2014-07-02 09:39:07 +00:00
|
|
|
s->stop_holdoff_offset = 0;
|
|
|
|
s->stop_holdoff_end = 0;
|
|
|
|
s->stop_found_periods = 0;
|
|
|
|
|
|
|
|
if (s->start_periods)
|
|
|
|
s->mode = SILENCE_TRIM;
|
|
|
|
else
|
|
|
|
s->mode = SILENCE_COPY;
|
|
|
|
|
2021-07-25 13:38:33 +00:00
|
|
|
switch (inlink->format) {
|
|
|
|
case AV_SAMPLE_FMT_DBL:
|
|
|
|
s->copy = copy_double;
|
|
|
|
switch (s->detection) {
|
2021-09-07 16:58:36 +00:00
|
|
|
case D_PEAK:
|
|
|
|
s->update = update_peak_double;
|
|
|
|
s->compute = compute_peak_double;
|
|
|
|
break;
|
|
|
|
case D_RMS:
|
|
|
|
s->update = update_rms_double;
|
|
|
|
s->compute = compute_rms_double;
|
|
|
|
break;
|
2021-07-25 13:38:33 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_FLT:
|
|
|
|
s->copy = copy_float;
|
|
|
|
switch (s->detection) {
|
2021-09-07 16:58:36 +00:00
|
|
|
case D_PEAK:
|
|
|
|
s->update = update_peak_float;
|
|
|
|
s->compute = compute_peak_float;
|
|
|
|
break;
|
|
|
|
case D_RMS:
|
|
|
|
s->update = update_rms_float;
|
|
|
|
s->compute = compute_rms_float;
|
|
|
|
break;
|
2021-07-25 13:38:33 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_DBLP:
|
|
|
|
s->copy = copy_doublep;
|
|
|
|
switch (s->detection) {
|
2021-09-07 16:58:36 +00:00
|
|
|
case D_PEAK:
|
|
|
|
s->update = update_peak_doublep;
|
|
|
|
s->compute = compute_peak_doublep;
|
|
|
|
break;
|
|
|
|
case D_RMS:
|
|
|
|
s->update = update_rms_doublep;
|
|
|
|
s->compute = compute_rms_doublep;
|
|
|
|
break;
|
2021-07-25 13:38:33 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_FLTP:
|
|
|
|
s->copy = copy_floatp;
|
|
|
|
switch (s->detection) {
|
2021-09-07 16:58:36 +00:00
|
|
|
case D_PEAK:
|
|
|
|
s->update = update_peak_floatp;
|
|
|
|
s->compute = compute_peak_floatp;
|
|
|
|
break;
|
|
|
|
case D_RMS:
|
|
|
|
s->update = update_rms_floatp;
|
|
|
|
s->compute = compute_rms_floatp;
|
|
|
|
break;
|
2021-07-25 13:38:33 +00:00
|
|
|
}
|
|
|
|
break;
|
2021-09-07 16:56:41 +00:00
|
|
|
default:
|
|
|
|
return AVERROR_BUG;
|
2021-07-25 13:38:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 13:15:19 +00:00
|
|
|
s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, 1024);
|
|
|
|
if (!s->fifo)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2014-07-02 09:39:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-15 12:05:17 +00:00
|
|
|
static void flush(SilenceRemoveContext *s,
|
|
|
|
AVFrame *out, AVFilterLink *outlink,
|
2021-09-08 13:15:19 +00:00
|
|
|
int *nb_samples_written, int flush_silence)
|
2014-07-02 09:39:07 +00:00
|
|
|
{
|
2018-10-08 18:43:14 +00:00
|
|
|
AVFrame *silence;
|
|
|
|
|
2014-07-02 09:39:07 +00:00
|
|
|
if (*nb_samples_written) {
|
2021-07-25 13:38:33 +00:00
|
|
|
out->nb_samples = *nb_samples_written;
|
2017-05-15 12:05:17 +00:00
|
|
|
|
2021-09-08 13:15:19 +00:00
|
|
|
av_audio_fifo_write(s->fifo, (void **)out->extended_data, out->nb_samples);
|
2014-07-02 09:39:07 +00:00
|
|
|
*nb_samples_written = 0;
|
|
|
|
}
|
2018-10-08 18:43:14 +00:00
|
|
|
|
2021-09-08 13:15:19 +00:00
|
|
|
av_frame_free(&out);
|
|
|
|
|
2018-10-08 18:43:14 +00:00
|
|
|
if (s->stop_silence_end <= 0 || !flush_silence)
|
|
|
|
return;
|
|
|
|
|
2021-07-25 13:38:33 +00:00
|
|
|
silence = ff_get_audio_buffer(outlink, s->stop_silence_end);
|
2021-09-08 13:15:19 +00:00
|
|
|
if (!silence)
|
2018-10-08 18:43:14 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (s->stop_silence_offset < s->stop_silence_end) {
|
2021-07-25 19:50:31 +00:00
|
|
|
av_samples_copy(silence->extended_data, s->stop_silence_hold->extended_data, 0,
|
2021-07-25 13:38:33 +00:00
|
|
|
s->stop_silence_offset,
|
|
|
|
s->stop_silence_end - s->stop_silence_offset,
|
|
|
|
outlink->channels, outlink->format);
|
2018-10-08 18:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s->stop_silence_offset > 0) {
|
2021-07-25 19:50:31 +00:00
|
|
|
av_samples_copy(silence->extended_data, s->stop_silence_hold->extended_data,
|
2021-07-25 13:38:33 +00:00
|
|
|
s->stop_silence_end - s->stop_silence_offset,
|
|
|
|
0, s->stop_silence_offset,
|
|
|
|
outlink->channels, outlink->format);
|
2018-10-08 18:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s->stop_silence_offset = 0;
|
|
|
|
s->stop_silence_end = 0;
|
|
|
|
|
2021-09-08 13:15:19 +00:00
|
|
|
av_audio_fifo_write(s->fifo, (void **)silence->extended_data, silence->nb_samples);
|
|
|
|
av_frame_free(&silence);
|
2014-07-02 09:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
SilenceRemoveContext *s = ctx->priv;
|
|
|
|
int nbs, nb_samples_read, nb_samples_written;
|
2021-09-08 11:06:43 +00:00
|
|
|
int i, j, threshold, ret = 0;
|
2014-07-02 09:39:07 +00:00
|
|
|
AVFrame *out;
|
|
|
|
|
|
|
|
nb_samples_read = nb_samples_written = 0;
|
|
|
|
|
2019-07-02 16:06:28 +00:00
|
|
|
if (s->next_pts == AV_NOPTS_VALUE)
|
|
|
|
s->next_pts = in->pts;
|
|
|
|
|
2014-07-02 09:39:07 +00:00
|
|
|
switch (s->mode) {
|
|
|
|
case SILENCE_TRIM:
|
|
|
|
silence_trim:
|
2021-07-25 13:38:33 +00:00
|
|
|
nbs = in->nb_samples - nb_samples_read;
|
2014-07-02 09:39:07 +00:00
|
|
|
if (!nbs)
|
|
|
|
break;
|
|
|
|
|
|
|
|
for (i = 0; i < nbs; i++) {
|
2018-10-15 15:52:56 +00:00
|
|
|
if (s->start_mode == T_ANY) {
|
2018-10-15 14:05:04 +00:00
|
|
|
threshold = 0;
|
2018-10-15 14:09:24 +00:00
|
|
|
for (j = 0; j < outlink->channels; j++) {
|
2021-07-25 13:38:33 +00:00
|
|
|
threshold |= s->compute(s, in, j, nb_samples_read) > s->start_threshold;
|
2018-10-15 14:05:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
threshold = 1;
|
2018-10-15 14:09:24 +00:00
|
|
|
for (j = 0; j < outlink->channels; j++) {
|
2021-07-25 13:38:33 +00:00
|
|
|
threshold &= s->compute(s, in, j, nb_samples_read) > s->start_threshold;
|
2018-10-15 14:05:04 +00:00
|
|
|
}
|
2014-07-02 09:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (threshold) {
|
2018-10-15 14:09:24 +00:00
|
|
|
for (j = 0; j < outlink->channels; j++) {
|
2021-07-25 13:38:33 +00:00
|
|
|
s->update(s, in, j, nb_samples_read);
|
|
|
|
s->copy(s, s->start_holdoff, in, j, s->start_holdoff_end, nb_samples_read);
|
2014-07-02 09:39:07 +00:00
|
|
|
}
|
2021-07-25 19:50:31 +00:00
|
|
|
|
|
|
|
s->window_offset++;
|
2021-09-07 17:20:38 +00:00
|
|
|
if (s->window_offset >= s->window_duration)
|
2021-07-25 19:50:31 +00:00
|
|
|
s->window_offset = 0;
|
2021-07-25 13:38:33 +00:00
|
|
|
s->start_holdoff_end++;
|
|
|
|
nb_samples_read++;
|
2014-07-02 09:39:07 +00:00
|
|
|
|
2021-07-25 13:38:33 +00:00
|
|
|
if (s->start_holdoff_end >= s->start_duration) {
|
2021-09-23 20:17:48 +00:00
|
|
|
s->start_found_periods += s->one_period >= 1;
|
|
|
|
s->one_period = 0;
|
2021-09-08 11:06:43 +00:00
|
|
|
if (s->start_found_periods >= s->start_periods) {
|
2014-07-02 09:39:07 +00:00
|
|
|
s->mode = SILENCE_TRIM_FLUSH;
|
|
|
|
goto silence_trim_flush;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->start_holdoff_offset = 0;
|
|
|
|
s->start_holdoff_end = 0;
|
2018-10-08 18:43:14 +00:00
|
|
|
s->start_silence_offset = 0;
|
|
|
|
s->start_silence_end = 0;
|
2014-07-02 09:39:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s->start_holdoff_end = 0;
|
2021-09-23 20:17:48 +00:00
|
|
|
s->one_period++;
|
2014-07-02 09:39:07 +00:00
|
|
|
|
2018-10-15 14:09:24 +00:00
|
|
|
for (j = 0; j < outlink->channels; j++) {
|
2021-07-25 13:38:33 +00:00
|
|
|
s->update(s, in, j, nb_samples_read);
|
|
|
|
if (s->start_silence)
|
|
|
|
s->copy(s, s->start_silence_hold, in, j, s->start_silence_offset, nb_samples_read);
|
2018-10-08 18:43:14 +00:00
|
|
|
}
|
2014-07-02 09:39:07 +00:00
|
|
|
|
2021-07-25 19:50:31 +00:00
|
|
|
s->window_offset++;
|
2021-09-07 17:20:38 +00:00
|
|
|
if (s->window_offset >= s->window_duration)
|
2021-07-25 19:50:31 +00:00
|
|
|
s->window_offset = 0;
|
2021-07-25 13:38:33 +00:00
|
|
|
nb_samples_read++;
|
|
|
|
s->start_silence_offset++;
|
|
|
|
|
|
|
|
if (s->start_silence) {
|
|
|
|
s->start_silence_end = FFMIN(s->start_silence_end + 1, s->start_silence);
|
|
|
|
if (s->start_silence_offset >= s->start_silence)
|
|
|
|
s->start_silence_offset = 0;
|
|
|
|
}
|
2014-07-02 09:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SILENCE_TRIM_FLUSH:
|
|
|
|
silence_trim_flush:
|
|
|
|
nbs = s->start_holdoff_end - s->start_holdoff_offset;
|
|
|
|
if (!nbs)
|
|
|
|
break;
|
|
|
|
|
2021-07-25 13:38:33 +00:00
|
|
|
out = ff_get_audio_buffer(outlink, nbs + s->start_silence_end);
|
2014-07-02 09:39:07 +00:00
|
|
|
if (!out) {
|
|
|
|
av_frame_free(&in);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2018-10-08 18:43:14 +00:00
|
|
|
if (s->start_silence_end > 0) {
|
|
|
|
if (s->start_silence_offset < s->start_silence_end) {
|
2021-07-25 19:50:31 +00:00
|
|
|
av_samples_copy(out->extended_data, s->start_silence_hold->extended_data, 0,
|
2021-07-25 13:38:33 +00:00
|
|
|
s->start_silence_offset,
|
|
|
|
s->start_silence_end - s->start_silence_offset,
|
|
|
|
outlink->channels, outlink->format);
|
2018-10-08 18:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s->start_silence_offset > 0) {
|
2021-07-25 19:50:31 +00:00
|
|
|
av_samples_copy(out->extended_data, s->start_silence_hold->extended_data,
|
2021-07-25 13:38:33 +00:00
|
|
|
s->start_silence_end - s->start_silence_offset,
|
|
|
|
0, s->start_silence_offset,
|
|
|
|
outlink->channels, outlink->format);
|
2018-10-08 18:43:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-25 19:50:31 +00:00
|
|
|
av_samples_copy(out->extended_data, s->start_holdoff->extended_data,
|
2021-07-25 13:38:33 +00:00
|
|
|
s->start_silence_end,
|
|
|
|
s->start_holdoff_offset, nbs,
|
|
|
|
outlink->channels, outlink->format);
|
2017-05-15 12:05:17 +00:00
|
|
|
|
2014-07-02 09:39:07 +00:00
|
|
|
s->start_holdoff_offset += nbs;
|
|
|
|
|
2021-09-08 13:15:19 +00:00
|
|
|
av_audio_fifo_write(s->fifo, (void **)out->extended_data, out->nb_samples);
|
|
|
|
av_frame_free(&out);
|
2014-07-02 09:39:07 +00:00
|
|
|
|
|
|
|
if (s->start_holdoff_offset == s->start_holdoff_end) {
|
|
|
|
s->start_holdoff_offset = 0;
|
|
|
|
s->start_holdoff_end = 0;
|
2018-10-08 18:43:14 +00:00
|
|
|
s->start_silence_offset = 0;
|
|
|
|
s->start_silence_end = 0;
|
2014-07-02 09:39:07 +00:00
|
|
|
s->mode = SILENCE_COPY;
|
|
|
|
goto silence_copy;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SILENCE_COPY:
|
|
|
|
silence_copy:
|
2021-07-25 13:38:33 +00:00
|
|
|
nbs = in->nb_samples - nb_samples_read;
|
2014-07-02 09:39:07 +00:00
|
|
|
if (!nbs)
|
|
|
|
break;
|
|
|
|
|
2018-10-15 14:09:24 +00:00
|
|
|
out = ff_get_audio_buffer(outlink, nbs);
|
2014-07-02 09:39:07 +00:00
|
|
|
if (!out) {
|
|
|
|
av_frame_free(&in);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->stop_periods) {
|
|
|
|
for (i = 0; i < nbs; i++) {
|
2018-10-15 15:52:56 +00:00
|
|
|
if (s->stop_mode == T_ANY) {
|
2018-10-15 14:05:04 +00:00
|
|
|
threshold = 0;
|
2018-10-15 14:09:24 +00:00
|
|
|
for (j = 0; j < outlink->channels; j++) {
|
2021-07-25 13:38:33 +00:00
|
|
|
threshold |= s->compute(s, in, j, nb_samples_read) > s->stop_threshold;
|
2018-10-15 14:05:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
threshold = 1;
|
2018-10-15 14:09:24 +00:00
|
|
|
for (j = 0; j < outlink->channels; j++) {
|
2021-07-25 13:38:33 +00:00
|
|
|
threshold &= s->compute(s, in, j, nb_samples_read) > s->stop_threshold;
|
2018-10-15 14:05:04 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-02 09:39:07 +00:00
|
|
|
|
2018-10-08 18:43:14 +00:00
|
|
|
if (threshold && s->stop_holdoff_end && !s->stop_silence) {
|
2014-07-02 09:39:07 +00:00
|
|
|
s->mode = SILENCE_COPY_FLUSH;
|
2021-09-08 13:15:19 +00:00
|
|
|
flush(s, out, outlink, &nb_samples_written, 0);
|
2021-09-23 20:17:48 +00:00
|
|
|
s->one_period++;
|
2014-07-02 09:39:07 +00:00
|
|
|
goto silence_copy_flush;
|
|
|
|
} else if (threshold) {
|
2018-10-15 14:09:24 +00:00
|
|
|
for (j = 0; j < outlink->channels; j++) {
|
2021-07-25 13:38:33 +00:00
|
|
|
s->update(s, in, j, nb_samples_read);
|
|
|
|
s->copy(s, out, in, j, nb_samples_written, nb_samples_read);
|
2014-07-02 09:39:07 +00:00
|
|
|
}
|
2021-07-25 13:38:33 +00:00
|
|
|
|
2021-07-25 19:50:31 +00:00
|
|
|
s->window_offset++;
|
2021-09-07 17:20:38 +00:00
|
|
|
if (s->window_offset >= s->window_duration)
|
2021-07-25 19:50:31 +00:00
|
|
|
s->window_offset = 0;
|
2021-07-25 13:38:33 +00:00
|
|
|
nb_samples_read++;
|
|
|
|
nb_samples_written++;
|
2021-09-23 20:17:48 +00:00
|
|
|
s->one_period++;
|
2014-07-02 09:39:07 +00:00
|
|
|
} else if (!threshold) {
|
2018-10-15 14:09:24 +00:00
|
|
|
for (j = 0; j < outlink->channels; j++) {
|
2021-07-25 13:38:33 +00:00
|
|
|
s->update(s, in, j, nb_samples_read);
|
2021-09-08 13:37:36 +00:00
|
|
|
if (s->stop_silence)
|
2021-07-25 13:38:33 +00:00
|
|
|
s->copy(s, s->stop_silence_hold, in, j, s->stop_silence_offset, nb_samples_read);
|
2014-07-02 09:39:07 +00:00
|
|
|
|
2021-07-25 13:38:33 +00:00
|
|
|
s->copy(s, s->stop_holdoff, in, j, s->stop_holdoff_end, nb_samples_read);
|
2014-07-02 09:39:07 +00:00
|
|
|
}
|
2021-07-25 19:50:31 +00:00
|
|
|
|
2021-09-08 13:37:36 +00:00
|
|
|
if (s->stop_silence) {
|
|
|
|
s->stop_silence_offset++;
|
|
|
|
s->stop_silence_end = FFMIN(s->stop_silence_end + 1, s->stop_silence);
|
|
|
|
if (s->stop_silence_offset >= s->stop_silence) {
|
|
|
|
s->stop_silence_offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-25 19:50:31 +00:00
|
|
|
s->window_offset++;
|
2021-09-07 17:20:38 +00:00
|
|
|
if (s->window_offset >= s->window_duration)
|
2021-07-25 19:50:31 +00:00
|
|
|
s->window_offset = 0;
|
2021-07-25 13:38:33 +00:00
|
|
|
nb_samples_read++;
|
|
|
|
s->stop_holdoff_end++;
|
2014-07-02 09:39:07 +00:00
|
|
|
|
2021-07-25 13:38:33 +00:00
|
|
|
if (s->stop_holdoff_end >= s->stop_duration) {
|
2021-09-23 20:17:48 +00:00
|
|
|
s->stop_found_periods += s->one_period >= 1;
|
|
|
|
s->one_period = 0;
|
2021-09-08 11:06:43 +00:00
|
|
|
if (s->stop_found_periods >= s->stop_periods) {
|
2014-07-02 09:39:07 +00:00
|
|
|
s->stop_holdoff_offset = 0;
|
|
|
|
s->stop_holdoff_end = 0;
|
|
|
|
|
|
|
|
if (!s->restart) {
|
|
|
|
s->mode = SILENCE_STOP;
|
2021-09-08 13:15:19 +00:00
|
|
|
flush(s, out, outlink, &nb_samples_written, 1);
|
2014-07-02 09:39:07 +00:00
|
|
|
goto silence_stop;
|
|
|
|
} else {
|
|
|
|
s->stop_found_periods = 0;
|
|
|
|
s->start_found_periods = 0;
|
|
|
|
s->start_holdoff_offset = 0;
|
|
|
|
s->start_holdoff_end = 0;
|
2018-10-08 18:43:14 +00:00
|
|
|
s->start_silence_offset = 0;
|
|
|
|
s->start_silence_end = 0;
|
2015-12-27 19:45:46 +00:00
|
|
|
clear_window(s);
|
2014-07-02 09:39:07 +00:00
|
|
|
s->mode = SILENCE_TRIM;
|
2021-09-08 13:15:19 +00:00
|
|
|
flush(s, out, outlink, &nb_samples_written, 1);
|
2014-07-02 09:39:07 +00:00
|
|
|
goto silence_trim;
|
|
|
|
}
|
|
|
|
}
|
2014-09-06 10:56:03 +00:00
|
|
|
s->mode = SILENCE_COPY_FLUSH;
|
2021-09-08 13:15:19 +00:00
|
|
|
flush(s, out, outlink, &nb_samples_written, 0);
|
2014-09-06 10:56:03 +00:00
|
|
|
goto silence_copy_flush;
|
2014-07-02 09:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-23 20:17:48 +00:00
|
|
|
s->one_period++;
|
2021-09-08 13:15:19 +00:00
|
|
|
flush(s, out, outlink, &nb_samples_written, 0);
|
2014-07-02 09:39:07 +00:00
|
|
|
} else {
|
2021-07-25 19:50:31 +00:00
|
|
|
av_samples_copy(out->extended_data, in->extended_data,
|
2021-07-25 13:38:33 +00:00
|
|
|
nb_samples_written,
|
|
|
|
nb_samples_read, nbs,
|
|
|
|
outlink->channels, outlink->format);
|
2017-05-15 12:05:17 +00:00
|
|
|
|
2021-09-08 13:15:19 +00:00
|
|
|
av_audio_fifo_write(s->fifo, (void **)out->extended_data, out->nb_samples);
|
|
|
|
av_frame_free(&out);
|
2014-07-02 09:39:07 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SILENCE_COPY_FLUSH:
|
|
|
|
silence_copy_flush:
|
|
|
|
nbs = s->stop_holdoff_end - s->stop_holdoff_offset;
|
|
|
|
if (!nbs)
|
|
|
|
break;
|
|
|
|
|
2021-07-25 13:38:33 +00:00
|
|
|
out = ff_get_audio_buffer(outlink, nbs);
|
2014-07-02 09:39:07 +00:00
|
|
|
if (!out) {
|
|
|
|
av_frame_free(&in);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2021-07-25 19:50:31 +00:00
|
|
|
av_samples_copy(out->extended_data, s->stop_holdoff->extended_data, 0,
|
2021-07-25 13:38:33 +00:00
|
|
|
s->stop_holdoff_offset, nbs,
|
|
|
|
outlink->channels, outlink->format);
|
|
|
|
|
2014-07-02 09:39:07 +00:00
|
|
|
s->stop_holdoff_offset += nbs;
|
|
|
|
|
2021-09-08 13:15:19 +00:00
|
|
|
av_audio_fifo_write(s->fifo, (void **)out->extended_data, out->nb_samples);
|
|
|
|
av_frame_free(&out);
|
2014-07-02 09:39:07 +00:00
|
|
|
|
|
|
|
if (s->stop_holdoff_offset == s->stop_holdoff_end) {
|
|
|
|
s->stop_holdoff_offset = 0;
|
|
|
|
s->stop_holdoff_end = 0;
|
2018-10-08 18:43:14 +00:00
|
|
|
s->stop_silence_offset = 0;
|
|
|
|
s->stop_silence_end = 0;
|
2014-07-02 09:39:07 +00:00
|
|
|
s->mode = SILENCE_COPY;
|
|
|
|
goto silence_copy;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SILENCE_STOP:
|
|
|
|
silence_stop:
|
|
|
|
break;
|
2021-09-07 16:56:41 +00:00
|
|
|
default:
|
|
|
|
ret = AVERROR_BUG;
|
2014-07-02 09:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
av_frame_free(&in);
|
|
|
|
|
2021-09-08 13:15:19 +00:00
|
|
|
if (av_audio_fifo_size(s->fifo) > 0) {
|
|
|
|
out = ff_get_audio_buffer(outlink, av_audio_fifo_size(s->fifo));
|
|
|
|
if (!out)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
av_audio_fifo_read(s->fifo, (void **)out->extended_data, out->nb_samples);
|
|
|
|
out->pts = s->next_pts;
|
2021-09-09 18:20:13 +00:00
|
|
|
s->next_pts += av_rescale_q(out->nb_samples,
|
|
|
|
(AVRational){1, outlink->sample_rate},
|
|
|
|
outlink->time_base);
|
|
|
|
|
2021-09-08 13:15:19 +00:00
|
|
|
ret = ff_filter_frame(outlink, out);
|
|
|
|
}
|
|
|
|
|
2014-07-02 09:39:07 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int request_frame(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
SilenceRemoveContext *s = ctx->priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ff_request_frame(ctx->inputs[0]);
|
|
|
|
if (ret == AVERROR_EOF && (s->mode == SILENCE_COPY_FLUSH ||
|
|
|
|
s->mode == SILENCE_COPY)) {
|
|
|
|
int nbs = s->stop_holdoff_end - s->stop_holdoff_offset;
|
|
|
|
if (nbs) {
|
|
|
|
AVFrame *frame;
|
|
|
|
|
2021-07-25 13:38:33 +00:00
|
|
|
frame = ff_get_audio_buffer(outlink, nbs);
|
2014-07-02 09:39:07 +00:00
|
|
|
if (!frame)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2021-07-25 19:50:31 +00:00
|
|
|
av_samples_copy(frame->extended_data, s->stop_holdoff->extended_data, 0,
|
2021-07-25 13:38:33 +00:00
|
|
|
s->stop_holdoff_offset, nbs,
|
|
|
|
outlink->channels, outlink->format);
|
2017-05-15 12:05:17 +00:00
|
|
|
|
|
|
|
frame->pts = s->next_pts;
|
|
|
|
s->next_pts += av_rescale_q(frame->nb_samples,
|
|
|
|
(AVRational){1, outlink->sample_rate},
|
|
|
|
outlink->time_base);
|
|
|
|
|
2017-11-27 12:38:33 +00:00
|
|
|
ret = ff_filter_frame(outlink, frame);
|
2014-07-02 09:39:07 +00:00
|
|
|
}
|
|
|
|
s->mode = SILENCE_STOP;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
SilenceRemoveContext *s = ctx->priv;
|
|
|
|
|
2021-07-25 13:38:33 +00:00
|
|
|
av_frame_free(&s->start_holdoff);
|
|
|
|
av_frame_free(&s->start_silence_hold);
|
|
|
|
av_frame_free(&s->stop_holdoff);
|
|
|
|
av_frame_free(&s->stop_silence_hold);
|
2021-07-25 19:50:31 +00:00
|
|
|
av_frame_free(&s->window);
|
2021-09-08 13:15:19 +00:00
|
|
|
|
|
|
|
av_audio_fifo_free(s->fifo);
|
|
|
|
s->fifo = NULL;
|
2014-07-02 09:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const AVFilterPad silenceremove_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = config_input,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad silenceremove_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.request_frame = request_frame,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 16:33:56 +00:00
|
|
|
const AVFilter ff_af_silenceremove = {
|
2014-07-02 09:39:07 +00:00
|
|
|
.name = "silenceremove",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Remove silence."),
|
|
|
|
.priv_size = sizeof(SilenceRemoveContext),
|
|
|
|
.priv_class = &silenceremove_class,
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
2021-08-12 11:05:31 +00:00
|
|
|
FILTER_INPUTS(silenceremove_inputs),
|
|
|
|
FILTER_OUTPUTS(silenceremove_outputs),
|
2021-09-27 11:10:11 +00:00
|
|
|
FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
|
|
|
|
AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP),
|
2014-07-02 09:39:07 +00:00
|
|
|
};
|