|
|
|
@ -69,16 +69,16 @@ typedef struct SilenceRemoveContext { |
|
|
|
|
int64_t stop_silence_opt; |
|
|
|
|
int stop_mode; |
|
|
|
|
|
|
|
|
|
double *start_holdoff; |
|
|
|
|
double *start_silence_hold; |
|
|
|
|
AVFrame *start_holdoff; |
|
|
|
|
AVFrame *start_silence_hold; |
|
|
|
|
size_t start_holdoff_offset; |
|
|
|
|
size_t start_holdoff_end; |
|
|
|
|
size_t start_silence_offset; |
|
|
|
|
size_t start_silence_end; |
|
|
|
|
int start_found_periods; |
|
|
|
|
|
|
|
|
|
double *stop_holdoff; |
|
|
|
|
double *stop_silence_hold; |
|
|
|
|
AVFrame *stop_holdoff; |
|
|
|
|
AVFrame *stop_silence_hold; |
|
|
|
|
size_t stop_holdoff_offset; |
|
|
|
|
size_t stop_holdoff_end; |
|
|
|
|
size_t stop_silence_offset; |
|
|
|
@ -96,8 +96,10 @@ typedef struct SilenceRemoveContext { |
|
|
|
|
int64_t next_pts; |
|
|
|
|
|
|
|
|
|
int detection; |
|
|
|
|
void (*update)(struct SilenceRemoveContext *s, double sample); |
|
|
|
|
double(*compute)(struct SilenceRemoveContext *s, double sample); |
|
|
|
|
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); |
|
|
|
|
} SilenceRemoveContext; |
|
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(SilenceRemoveContext, x) |
|
|
|
@ -125,8 +127,158 @@ static const AVOption silenceremove_options[] = { |
|
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(silenceremove); |
|
|
|
|
|
|
|
|
|
static double compute_peak(SilenceRemoveContext *s, double sample) |
|
|
|
|
static void copy_double(SilenceRemoveContext *s, AVFrame *out, AVFrame *in, |
|
|
|
|
int ch, int out_offset, int in_offset) |
|
|
|
|
{ |
|
|
|
|
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) |
|
|
|
|
{ |
|
|
|
|
const double *srcp = (const double *)in->data[ch]; |
|
|
|
|
const double src = srcp[in_offset]; |
|
|
|
|
double *dstp = (double *)out->data[ch]; |
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
{ |
|
|
|
|
const float *srcp = (const float *)in->data[ch]; |
|
|
|
|
const float src = srcp[in_offset]; |
|
|
|
|
float *dstp = (float *)out->data[ch]; |
|
|
|
|
|
|
|
|
|
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]; |
|
|
|
|
double sample = samples[frame->channels * offset + ch]; |
|
|
|
|
double new_sum; |
|
|
|
|
|
|
|
|
|
new_sum = s->sum; |
|
|
|
|
new_sum -= *s->window_current; |
|
|
|
|
new_sum += fabs(sample); |
|
|
|
|
|
|
|
|
|
return new_sum / s->window_size; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void update_peak_double(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset) |
|
|
|
|
{ |
|
|
|
|
const double *samples = (const double *)frame->data[0]; |
|
|
|
|
double sample = samples[frame->channels * offset + ch]; |
|
|
|
|
|
|
|
|
|
s->sum -= *s->window_current; |
|
|
|
|
*s->window_current = fabs(sample); |
|
|
|
|
s->sum += *s->window_current; |
|
|
|
|
|
|
|
|
|
s->window_current++; |
|
|
|
|
if (s->window_current >= s->window_end) |
|
|
|
|
s->window_current = s->window; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static double compute_peak_float(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset) |
|
|
|
|
{ |
|
|
|
|
const float *samples = (const float *)frame->data[0]; |
|
|
|
|
float sample = samples[frame->channels * offset + ch]; |
|
|
|
|
float new_sum; |
|
|
|
|
|
|
|
|
|
new_sum = s->sum; |
|
|
|
|
new_sum -= *s->window_current; |
|
|
|
|
new_sum += fabs(sample); |
|
|
|
|
|
|
|
|
|
return new_sum / s->window_size; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void update_peak_float(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset) |
|
|
|
|
{ |
|
|
|
|
const float *samples = (const float *)frame->data[0]; |
|
|
|
|
float sample = samples[frame->channels * offset + ch]; |
|
|
|
|
|
|
|
|
|
s->sum -= *s->window_current; |
|
|
|
|
*s->window_current = fabs(sample); |
|
|
|
|
s->sum += *s->window_current; |
|
|
|
|
|
|
|
|
|
s->window_current++; |
|
|
|
|
if (s->window_current >= s->window_end) |
|
|
|
|
s->window_current = s->window; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static double compute_rms_double(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset) |
|
|
|
|
{ |
|
|
|
|
const double *samples = (const double *)frame->data[0]; |
|
|
|
|
double sample = samples[frame->channels * offset + ch]; |
|
|
|
|
double new_sum; |
|
|
|
|
|
|
|
|
|
new_sum = s->sum; |
|
|
|
|
new_sum -= *s->window_current; |
|
|
|
|
new_sum += sample * sample; |
|
|
|
|
|
|
|
|
|
return sqrt(new_sum / s->window_size); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void update_rms_double(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset) |
|
|
|
|
{ |
|
|
|
|
const double *samples = (const double *)frame->data[0]; |
|
|
|
|
double sample = samples[frame->channels * offset + ch]; |
|
|
|
|
|
|
|
|
|
s->sum -= *s->window_current; |
|
|
|
|
*s->window_current = sample * sample; |
|
|
|
|
s->sum += *s->window_current; |
|
|
|
|
|
|
|
|
|
s->window_current++; |
|
|
|
|
if (s->window_current >= s->window_end) |
|
|
|
|
s->window_current = s->window; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static double compute_rms_float(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset) |
|
|
|
|
{ |
|
|
|
|
const float *samples = (const float *)frame->data[0]; |
|
|
|
|
float sample = samples[frame->channels * offset + ch]; |
|
|
|
|
float new_sum; |
|
|
|
|
|
|
|
|
|
new_sum = s->sum; |
|
|
|
|
new_sum -= *s->window_current; |
|
|
|
|
new_sum += sample * sample; |
|
|
|
|
|
|
|
|
|
return sqrtf(new_sum / s->window_size); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void update_rms_float(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset) |
|
|
|
|
{ |
|
|
|
|
const float *samples = (const float *)frame->data[0]; |
|
|
|
|
float sample = samples[frame->channels * offset + ch]; |
|
|
|
|
|
|
|
|
|
s->sum -= *s->window_current; |
|
|
|
|
*s->window_current = sample * sample; |
|
|
|
|
s->sum += *s->window_current; |
|
|
|
|
|
|
|
|
|
s->window_current++; |
|
|
|
|
if (s->window_current >= s->window_end) |
|
|
|
|
s->window_current = s->window; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static double compute_peak_doublep(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset) |
|
|
|
|
{ |
|
|
|
|
const double *samples = (const double *)frame->data[ch]; |
|
|
|
|
double sample = samples[offset]; |
|
|
|
|
double new_sum; |
|
|
|
|
|
|
|
|
|
new_sum = s->sum; |
|
|
|
@ -136,8 +288,38 @@ static double compute_peak(SilenceRemoveContext *s, double sample) |
|
|
|
|
return new_sum / s->window_size; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void update_peak(SilenceRemoveContext *s, double sample) |
|
|
|
|
static void update_peak_doublep(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset) |
|
|
|
|
{ |
|
|
|
|
const double *samples = (const double *)frame->data[ch]; |
|
|
|
|
double sample = samples[offset]; |
|
|
|
|
|
|
|
|
|
s->sum -= *s->window_current; |
|
|
|
|
*s->window_current = fabs(sample); |
|
|
|
|
s->sum += *s->window_current; |
|
|
|
|
|
|
|
|
|
s->window_current++; |
|
|
|
|
if (s->window_current >= s->window_end) |
|
|
|
|
s->window_current = s->window; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static double compute_peak_floatp(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset) |
|
|
|
|
{ |
|
|
|
|
const float *samples = (const float *)frame->data[ch]; |
|
|
|
|
float sample = samples[offset]; |
|
|
|
|
float new_sum; |
|
|
|
|
|
|
|
|
|
new_sum = s->sum; |
|
|
|
|
new_sum -= *s->window_current; |
|
|
|
|
new_sum += fabs(sample); |
|
|
|
|
|
|
|
|
|
return new_sum / s->window_size; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void update_peak_floatp(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset) |
|
|
|
|
{ |
|
|
|
|
const float *samples = (const float *)frame->data[ch]; |
|
|
|
|
float sample = samples[offset]; |
|
|
|
|
|
|
|
|
|
s->sum -= *s->window_current; |
|
|
|
|
*s->window_current = fabs(sample); |
|
|
|
|
s->sum += *s->window_current; |
|
|
|
@ -147,8 +329,10 @@ static void update_peak(SilenceRemoveContext *s, double sample) |
|
|
|
|
s->window_current = s->window; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static double compute_rms(SilenceRemoveContext *s, double sample) |
|
|
|
|
static double compute_rms_doublep(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset) |
|
|
|
|
{ |
|
|
|
|
const double *samples = (const double *)frame->data[ch]; |
|
|
|
|
double sample = samples[offset]; |
|
|
|
|
double new_sum; |
|
|
|
|
|
|
|
|
|
new_sum = s->sum; |
|
|
|
@ -158,8 +342,38 @@ static double compute_rms(SilenceRemoveContext *s, double sample) |
|
|
|
|
return sqrt(new_sum / s->window_size); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void update_rms(SilenceRemoveContext *s, double sample) |
|
|
|
|
static void update_rms_doublep(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset) |
|
|
|
|
{ |
|
|
|
|
const double *samples = (const double *)frame->data[ch]; |
|
|
|
|
double sample = samples[offset]; |
|
|
|
|
|
|
|
|
|
s->sum -= *s->window_current; |
|
|
|
|
*s->window_current = sample * sample; |
|
|
|
|
s->sum += *s->window_current; |
|
|
|
|
|
|
|
|
|
s->window_current++; |
|
|
|
|
if (s->window_current >= s->window_end) |
|
|
|
|
s->window_current = s->window; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static double compute_rms_floatp(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset) |
|
|
|
|
{ |
|
|
|
|
const float *samples = (const float *)frame->data[ch]; |
|
|
|
|
float sample = samples[offset]; |
|
|
|
|
float new_sum; |
|
|
|
|
|
|
|
|
|
new_sum = s->sum; |
|
|
|
|
new_sum -= *s->window_current; |
|
|
|
|
new_sum += sample * sample; |
|
|
|
|
|
|
|
|
|
return sqrtf(new_sum / s->window_size); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void update_rms_floatp(SilenceRemoveContext *s, AVFrame *frame, int ch, int offset) |
|
|
|
|
{ |
|
|
|
|
const float *samples = (const float *)frame->data[ch]; |
|
|
|
|
float sample = samples[offset]; |
|
|
|
|
|
|
|
|
|
s->sum -= *s->window_current; |
|
|
|
|
*s->window_current = sample * sample; |
|
|
|
|
s->sum += *s->window_current; |
|
|
|
@ -178,17 +392,6 @@ static av_cold int init(AVFilterContext *ctx) |
|
|
|
|
s->restart = 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
switch (s->detection) { |
|
|
|
|
case D_PEAK: |
|
|
|
|
s->update = update_peak; |
|
|
|
|
s->compute = compute_peak; |
|
|
|
|
break; |
|
|
|
|
case D_RMS: |
|
|
|
|
s->update = update_rms; |
|
|
|
|
s->compute = compute_rms; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -223,15 +426,13 @@ static int config_input(AVFilterLink *inlink) |
|
|
|
|
s->stop_silence = av_rescale(s->stop_silence_opt, inlink->sample_rate, |
|
|
|
|
AV_TIME_BASE); |
|
|
|
|
|
|
|
|
|
s->start_holdoff = av_malloc_array(FFMAX(s->start_duration, 1), |
|
|
|
|
sizeof(*s->start_holdoff) * |
|
|
|
|
inlink->channels); |
|
|
|
|
s->start_holdoff = ff_get_audio_buffer(ctx->outputs[0], |
|
|
|
|
FFMAX(s->start_duration, 1)); |
|
|
|
|
if (!s->start_holdoff) |
|
|
|
|
return AVERROR(ENOMEM); |
|
|
|
|
|
|
|
|
|
s->start_silence_hold = av_malloc_array(FFMAX(s->start_silence, 1), |
|
|
|
|
sizeof(*s->start_silence_hold) * |
|
|
|
|
inlink->channels); |
|
|
|
|
s->start_silence_hold = ff_get_audio_buffer(ctx->outputs[0], |
|
|
|
|
FFMAX(s->start_silence, 1)); |
|
|
|
|
if (!s->start_silence_hold) |
|
|
|
|
return AVERROR(ENOMEM); |
|
|
|
|
|
|
|
|
@ -239,15 +440,13 @@ static int config_input(AVFilterLink *inlink) |
|
|
|
|
s->start_holdoff_end = 0; |
|
|
|
|
s->start_found_periods = 0; |
|
|
|
|
|
|
|
|
|
s->stop_holdoff = av_malloc_array(FFMAX(s->stop_duration, 1), |
|
|
|
|
sizeof(*s->stop_holdoff) * |
|
|
|
|
inlink->channels); |
|
|
|
|
s->stop_holdoff = ff_get_audio_buffer(ctx->outputs[0], |
|
|
|
|
FFMAX(s->stop_duration, 1)); |
|
|
|
|
if (!s->stop_holdoff) |
|
|
|
|
return AVERROR(ENOMEM); |
|
|
|
|
|
|
|
|
|
s->stop_silence_hold = av_malloc_array(FFMAX(s->stop_silence, 1), |
|
|
|
|
sizeof(*s->stop_silence_hold) * |
|
|
|
|
inlink->channels); |
|
|
|
|
s->stop_silence_hold = ff_get_audio_buffer(ctx->outputs[0], |
|
|
|
|
FFMAX(s->stop_silence, 1)); |
|
|
|
|
if (!s->stop_silence_hold) |
|
|
|
|
return AVERROR(ENOMEM); |
|
|
|
|
|
|
|
|
@ -260,6 +459,61 @@ static int config_input(AVFilterLink *inlink) |
|
|
|
|
else |
|
|
|
|
s->mode = SILENCE_COPY; |
|
|
|
|
|
|
|
|
|
switch (inlink->format) { |
|
|
|
|
case AV_SAMPLE_FMT_DBL: |
|
|
|
|
s->copy = copy_double; |
|
|
|
|
switch (s->detection) { |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case AV_SAMPLE_FMT_FLT: |
|
|
|
|
s->copy = copy_float; |
|
|
|
|
switch (s->detection) { |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case AV_SAMPLE_FMT_DBLP: |
|
|
|
|
s->copy = copy_doublep; |
|
|
|
|
switch (s->detection) { |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case AV_SAMPLE_FMT_FLTP: |
|
|
|
|
s->copy = copy_floatp; |
|
|
|
|
switch (s->detection) { |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -270,7 +524,7 @@ static void flush(SilenceRemoveContext *s, |
|
|
|
|
AVFrame *silence; |
|
|
|
|
|
|
|
|
|
if (*nb_samples_written) { |
|
|
|
|
out->nb_samples = *nb_samples_written / outlink->channels; |
|
|
|
|
out->nb_samples = *nb_samples_written; |
|
|
|
|
|
|
|
|
|
out->pts = s->next_pts; |
|
|
|
|
s->next_pts += av_rescale_q(out->nb_samples, |
|
|
|
@ -288,22 +542,24 @@ static void flush(SilenceRemoveContext *s, |
|
|
|
|
if (s->stop_silence_end <= 0 || !flush_silence) |
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
silence = ff_get_audio_buffer(outlink, s->stop_silence_end / outlink->channels); |
|
|
|
|
silence = ff_get_audio_buffer(outlink, s->stop_silence_end); |
|
|
|
|
if (!silence) { |
|
|
|
|
*ret = AVERROR(ENOMEM); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (s->stop_silence_offset < s->stop_silence_end) { |
|
|
|
|
memcpy(silence->data[0], |
|
|
|
|
&s->stop_silence_hold[s->stop_silence_offset], |
|
|
|
|
(s->stop_silence_end - s->stop_silence_offset) * sizeof(double)); |
|
|
|
|
av_samples_copy(silence->data, s->stop_silence_hold->data, 0, |
|
|
|
|
s->stop_silence_offset, |
|
|
|
|
s->stop_silence_end - s->stop_silence_offset, |
|
|
|
|
outlink->channels, outlink->format); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (s->stop_silence_offset > 0) { |
|
|
|
|
memcpy(silence->data[0] + (s->stop_silence_end - s->stop_silence_offset) * sizeof(double), |
|
|
|
|
&s->stop_silence_hold[0], |
|
|
|
|
s->stop_silence_offset * sizeof(double)); |
|
|
|
|
av_samples_copy(silence->data, s->stop_silence_hold->data, |
|
|
|
|
s->stop_silence_end - s->stop_silence_offset, |
|
|
|
|
0, s->stop_silence_offset, |
|
|
|
|
outlink->channels, outlink->format); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
s->stop_silence_offset = 0; |
|
|
|
@ -324,7 +580,6 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
|
|
|
|
SilenceRemoveContext *s = ctx->priv; |
|
|
|
|
int i, j, threshold, ret = 0; |
|
|
|
|
int nbs, nb_samples_read, nb_samples_written; |
|
|
|
|
double *obuf, *ibuf = (double *)in->data[0]; |
|
|
|
|
AVFrame *out; |
|
|
|
|
|
|
|
|
|
nb_samples_read = nb_samples_written = 0; |
|
|
|
@ -335,7 +590,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
|
|
|
|
switch (s->mode) { |
|
|
|
|
case SILENCE_TRIM: |
|
|
|
|
silence_trim: |
|
|
|
|
nbs = in->nb_samples - nb_samples_read / outlink->channels; |
|
|
|
|
nbs = in->nb_samples - nb_samples_read; |
|
|
|
|
if (!nbs) |
|
|
|
|
break; |
|
|
|
|
|
|
|
|
@ -343,23 +598,24 @@ silence_trim: |
|
|
|
|
if (s->start_mode == T_ANY) { |
|
|
|
|
threshold = 0; |
|
|
|
|
for (j = 0; j < outlink->channels; j++) { |
|
|
|
|
threshold |= s->compute(s, ibuf[j]) > s->start_threshold; |
|
|
|
|
threshold |= s->compute(s, in, j, nb_samples_read) > s->start_threshold; |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
threshold = 1; |
|
|
|
|
for (j = 0; j < outlink->channels; j++) { |
|
|
|
|
threshold &= s->compute(s, ibuf[j]) > s->start_threshold; |
|
|
|
|
threshold &= s->compute(s, in, j, nb_samples_read) > s->start_threshold; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (threshold) { |
|
|
|
|
for (j = 0; j < outlink->channels; j++) { |
|
|
|
|
s->update(s, *ibuf); |
|
|
|
|
s->start_holdoff[s->start_holdoff_end++] = *ibuf++; |
|
|
|
|
s->update(s, in, j, nb_samples_read); |
|
|
|
|
s->copy(s, s->start_holdoff, in, j, s->start_holdoff_end, nb_samples_read); |
|
|
|
|
} |
|
|
|
|
nb_samples_read += outlink->channels; |
|
|
|
|
s->start_holdoff_end++; |
|
|
|
|
nb_samples_read++; |
|
|
|
|
|
|
|
|
|
if (s->start_holdoff_end >= s->start_duration * outlink->channels) { |
|
|
|
|
if (s->start_holdoff_end >= s->start_duration) { |
|
|
|
|
if (++s->start_found_periods >= s->start_periods) { |
|
|
|
|
s->mode = SILENCE_TRIM_FLUSH; |
|
|
|
|
goto silence_trim_flush; |
|
|
|
@ -374,30 +630,30 @@ silence_trim: |
|
|
|
|
s->start_holdoff_end = 0; |
|
|
|
|
|
|
|
|
|
for (j = 0; j < outlink->channels; j++) { |
|
|
|
|
s->update(s, ibuf[j]); |
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
nb_samples_read++; |
|
|
|
|
s->start_silence_offset++; |
|
|
|
|
|
|
|
|
|
if (s->start_silence) { |
|
|
|
|
s->start_silence_hold[s->start_silence_offset++] = ibuf[j]; |
|
|
|
|
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_end = FFMIN(s->start_silence_end + 1, s->start_silence); |
|
|
|
|
if (s->start_silence_offset >= s->start_silence) |
|
|
|
|
s->start_silence_offset = 0; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ibuf += outlink->channels; |
|
|
|
|
nb_samples_read += outlink->channels; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
case SILENCE_TRIM_FLUSH: |
|
|
|
|
silence_trim_flush: |
|
|
|
|
nbs = s->start_holdoff_end - s->start_holdoff_offset; |
|
|
|
|
nbs -= nbs % outlink->channels; |
|
|
|
|
if (!nbs) |
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
out = ff_get_audio_buffer(outlink, nbs / outlink->channels + s->start_silence_end / outlink->channels); |
|
|
|
|
out = ff_get_audio_buffer(outlink, nbs + s->start_silence_end); |
|
|
|
|
if (!out) { |
|
|
|
|
av_frame_free(&in); |
|
|
|
|
return AVERROR(ENOMEM); |
|
|
|
@ -405,21 +661,24 @@ silence_trim_flush: |
|
|
|
|
|
|
|
|
|
if (s->start_silence_end > 0) { |
|
|
|
|
if (s->start_silence_offset < s->start_silence_end) { |
|
|
|
|
memcpy(out->data[0], |
|
|
|
|
&s->start_silence_hold[s->start_silence_offset], |
|
|
|
|
(s->start_silence_end - s->start_silence_offset) * sizeof(double)); |
|
|
|
|
av_samples_copy(out->data, s->start_silence_hold->data, 0, |
|
|
|
|
s->start_silence_offset, |
|
|
|
|
s->start_silence_end - s->start_silence_offset, |
|
|
|
|
outlink->channels, outlink->format); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (s->start_silence_offset > 0) { |
|
|
|
|
memcpy(out->data[0] + (s->start_silence_end - s->start_silence_offset) * sizeof(double), |
|
|
|
|
&s->start_silence_hold[0], |
|
|
|
|
s->start_silence_offset * sizeof(double)); |
|
|
|
|
av_samples_copy(out->data, s->start_silence_hold->data, |
|
|
|
|
s->start_silence_end - s->start_silence_offset, |
|
|
|
|
0, s->start_silence_offset, |
|
|
|
|
outlink->channels, outlink->format); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
memcpy(out->data[0] + s->start_silence_end * sizeof(double), |
|
|
|
|
&s->start_holdoff[s->start_holdoff_offset], |
|
|
|
|
nbs * sizeof(double)); |
|
|
|
|
av_samples_copy(out->data, s->start_holdoff->data, |
|
|
|
|
s->start_silence_end, |
|
|
|
|
s->start_holdoff_offset, nbs, |
|
|
|
|
outlink->channels, outlink->format); |
|
|
|
|
|
|
|
|
|
out->pts = s->next_pts; |
|
|
|
|
s->next_pts += av_rescale_q(out->nb_samples, |
|
|
|
@ -442,7 +701,7 @@ silence_trim_flush: |
|
|
|
|
|
|
|
|
|
case SILENCE_COPY: |
|
|
|
|
silence_copy: |
|
|
|
|
nbs = in->nb_samples - nb_samples_read / outlink->channels; |
|
|
|
|
nbs = in->nb_samples - nb_samples_read; |
|
|
|
|
if (!nbs) |
|
|
|
|
break; |
|
|
|
|
|
|
|
|
@ -451,19 +710,18 @@ silence_copy: |
|
|
|
|
av_frame_free(&in); |
|
|
|
|
return AVERROR(ENOMEM); |
|
|
|
|
} |
|
|
|
|
obuf = (double *)out->data[0]; |
|
|
|
|
|
|
|
|
|
if (s->stop_periods) { |
|
|
|
|
for (i = 0; i < nbs; i++) { |
|
|
|
|
if (s->stop_mode == T_ANY) { |
|
|
|
|
threshold = 0; |
|
|
|
|
for (j = 0; j < outlink->channels; j++) { |
|
|
|
|
threshold |= s->compute(s, ibuf[j]) > s->stop_threshold; |
|
|
|
|
threshold |= s->compute(s, in, j, nb_samples_read) > s->stop_threshold; |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
threshold = 1; |
|
|
|
|
for (j = 0; j < outlink->channels; j++) { |
|
|
|
|
threshold &= s->compute(s, ibuf[j]) > s->stop_threshold; |
|
|
|
|
threshold &= s->compute(s, in, j, nb_samples_read) > s->stop_threshold; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -473,27 +731,29 @@ silence_copy: |
|
|
|
|
goto silence_copy_flush; |
|
|
|
|
} else if (threshold) { |
|
|
|
|
for (j = 0; j < outlink->channels; j++) { |
|
|
|
|
s->update(s, *ibuf); |
|
|
|
|
*obuf++ = *ibuf++; |
|
|
|
|
s->update(s, in, j, nb_samples_read); |
|
|
|
|
s->copy(s, out, in, j, nb_samples_written, nb_samples_read); |
|
|
|
|
} |
|
|
|
|
nb_samples_read += outlink->channels; |
|
|
|
|
nb_samples_written += outlink->channels; |
|
|
|
|
|
|
|
|
|
nb_samples_read++; |
|
|
|
|
nb_samples_written++; |
|
|
|
|
} else if (!threshold) { |
|
|
|
|
for (j = 0; j < outlink->channels; j++) { |
|
|
|
|
s->update(s, *ibuf); |
|
|
|
|
s->update(s, in, j, nb_samples_read); |
|
|
|
|
if (s->stop_silence) { |
|
|
|
|
s->stop_silence_hold[s->stop_silence_offset++] = *ibuf; |
|
|
|
|
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->copy(s, s->stop_silence_hold, in, j, s->stop_silence_offset, nb_samples_read); |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
s->stop_holdoff[s->stop_holdoff_end++] = *ibuf++; |
|
|
|
|
s->copy(s, s->stop_holdoff, in, j, s->stop_holdoff_end, nb_samples_read); |
|
|
|
|
} |
|
|
|
|
nb_samples_read += outlink->channels; |
|
|
|
|
nb_samples_read++; |
|
|
|
|
s->stop_holdoff_end++; |
|
|
|
|
|
|
|
|
|
if (s->stop_holdoff_end >= s->stop_duration * outlink->channels) { |
|
|
|
|
if (s->stop_holdoff_end >= s->stop_duration) { |
|
|
|
|
if (++s->stop_found_periods >= s->stop_periods) { |
|
|
|
|
s->stop_holdoff_offset = 0; |
|
|
|
|
s->stop_holdoff_end = 0; |
|
|
|
@ -523,7 +783,10 @@ silence_copy: |
|
|
|
|
} |
|
|
|
|
flush(s, out, outlink, &nb_samples_written, &ret, 0); |
|
|
|
|
} else { |
|
|
|
|
memcpy(obuf, ibuf, sizeof(double) * nbs * outlink->channels); |
|
|
|
|
av_samples_copy(out->data, in->data, |
|
|
|
|
nb_samples_written, |
|
|
|
|
nb_samples_read, nbs, |
|
|
|
|
outlink->channels, outlink->format); |
|
|
|
|
|
|
|
|
|
out->pts = s->next_pts; |
|
|
|
|
s->next_pts += av_rescale_q(out->nb_samples, |
|
|
|
@ -537,18 +800,19 @@ silence_copy: |
|
|
|
|
case SILENCE_COPY_FLUSH: |
|
|
|
|
silence_copy_flush: |
|
|
|
|
nbs = s->stop_holdoff_end - s->stop_holdoff_offset; |
|
|
|
|
nbs -= nbs % outlink->channels; |
|
|
|
|
if (!nbs) |
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
out = ff_get_audio_buffer(outlink, nbs / outlink->channels); |
|
|
|
|
out = ff_get_audio_buffer(outlink, nbs); |
|
|
|
|
if (!out) { |
|
|
|
|
av_frame_free(&in); |
|
|
|
|
return AVERROR(ENOMEM); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
memcpy(out->data[0], &s->stop_holdoff[s->stop_holdoff_offset], |
|
|
|
|
nbs * sizeof(double)); |
|
|
|
|
av_samples_copy(out->data, s->stop_holdoff->data, 0, |
|
|
|
|
s->stop_holdoff_offset, nbs, |
|
|
|
|
outlink->channels, outlink->format); |
|
|
|
|
|
|
|
|
|
s->stop_holdoff_offset += nbs; |
|
|
|
|
|
|
|
|
|
out->pts = s->next_pts; |
|
|
|
@ -590,12 +854,13 @@ static int request_frame(AVFilterLink *outlink) |
|
|
|
|
if (nbs) { |
|
|
|
|
AVFrame *frame; |
|
|
|
|
|
|
|
|
|
frame = ff_get_audio_buffer(outlink, nbs / outlink->channels); |
|
|
|
|
frame = ff_get_audio_buffer(outlink, nbs); |
|
|
|
|
if (!frame) |
|
|
|
|
return AVERROR(ENOMEM); |
|
|
|
|
|
|
|
|
|
memcpy(frame->data[0], &s->stop_holdoff[s->stop_holdoff_offset], |
|
|
|
|
nbs * sizeof(double)); |
|
|
|
|
av_samples_copy(frame->data, s->stop_holdoff->data, 0, |
|
|
|
|
s->stop_holdoff_offset, nbs, |
|
|
|
|
outlink->channels, outlink->format); |
|
|
|
|
|
|
|
|
|
frame->pts = s->next_pts; |
|
|
|
|
s->next_pts += av_rescale_q(frame->nb_samples, |
|
|
|
@ -614,7 +879,9 @@ static int query_formats(AVFilterContext *ctx) |
|
|
|
|
AVFilterFormats *formats = NULL; |
|
|
|
|
AVFilterChannelLayouts *layouts = NULL; |
|
|
|
|
static const enum AVSampleFormat sample_fmts[] = { |
|
|
|
|
AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_NONE |
|
|
|
|
AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP, |
|
|
|
|
AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP, |
|
|
|
|
AV_SAMPLE_FMT_NONE |
|
|
|
|
}; |
|
|
|
|
int ret; |
|
|
|
|
|
|
|
|
@ -642,10 +909,10 @@ static av_cold void uninit(AVFilterContext *ctx) |
|
|
|
|
{ |
|
|
|
|
SilenceRemoveContext *s = ctx->priv; |
|
|
|
|
|
|
|
|
|
av_freep(&s->start_holdoff); |
|
|
|
|
av_freep(&s->start_silence_hold); |
|
|
|
|
av_freep(&s->stop_holdoff); |
|
|
|
|
av_freep(&s->stop_silence_hold); |
|
|
|
|
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); |
|
|
|
|
av_freep(&s->window); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|