86 double sigmae,
double *detection,
88 const double *
src,
double *dst);
91 #define OFFSET(x) offsetof(AudioDeclickContext, x)
92 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
153 if (
s->window_size < 100)
155 s->ar_order =
FFMAX(
s->window_size *
s->ar / 100., 1);
156 s->nb_burst_samples =
s->window_size *
s->burst / 1000.;
157 s->hop_size =
s->window_size * (1. - (
s->overlap / 100.));
161 s->window_func_lut =
av_calloc(
s->window_size,
sizeof(*
s->window_func_lut));
162 if (!
s->window_func_lut)
164 for (
i = 0;
i <
s->window_size;
i++)
165 s->window_func_lut[
i] = sin(
M_PI *
i /
s->window_size) *
166 (1. - (
s->overlap / 100.)) *
M_PI_2;
177 if (!
s->in || !
s->out || !
s->buffer || !
s->is || !
s->enabled)
186 s->overlap_skip =
s->method ? (
s->window_size -
s->hop_size) / 2 : 0;
187 if (
s->overlap_skip > 0) {
200 c->detection =
av_calloc(
s->window_size,
sizeof(*
c->detection));
201 c->auxiliary =
av_calloc(
s->ar_order + 1,
sizeof(*
c->auxiliary));
202 c->acoefficients =
av_calloc(
s->ar_order + 1,
sizeof(*
c->acoefficients));
203 c->acorrelation =
av_calloc(
s->ar_order + 1,
sizeof(*
c->acorrelation));
205 c->click =
av_calloc(
s->window_size,
sizeof(*
c->click));
206 c->index =
av_calloc(
s->window_size,
sizeof(*
c->index));
207 c->interpolated =
av_calloc(
s->window_size,
sizeof(*
c->interpolated));
208 if (!
c->auxiliary || !
c->acoefficients || !
c->detection || !
c->click ||
209 !
c->index || !
c->interpolated || !
c->acorrelation || !
c->tmp)
217 double *output,
double scale)
221 for (
i = 0;
i <= order;
i++) {
224 for (j =
i; j <
size; j++)
225 value += input[j] * input[j -
i];
227 output[
i] =
value * scale;
242 k[0] =
a[0] = -
r[1] /
r[0];
243 alpha =
r[0] * (1. - k[0] * k[0]);
247 for (j = 0; j <
i; j++)
248 epsilon +=
a[j] *
r[
i - j];
253 for (j =
i - 1; j >= 0; j--)
254 k[j] =
a[j] + k[
i] *
a[
i - j - 1];
255 for (j = 0; j <=
i; j++)
287 while (start <= end) {
288 i = (end + start) / 2;
304 for (
i = 0;
i < n;
i++) {
305 const int in =
i * n;
309 for (j = 0; j <
i; j++)
310 value -= matrix[j * n + j] * matrix[
in + j] * matrix[
in + j];
317 for (j =
i + 1; j < n; j++) {
318 const int jn = j * n;
322 for (k = 0; k <
i; k++)
323 x -= matrix[k * n + k] * matrix[
in + k] * matrix[jn + k];
324 matrix[jn +
i] = x / matrix[
in +
i];
332 double *vector,
int n,
double *
out)
346 for (
i = 0;
i < n;
i++) {
347 const int in =
i * n;
351 for (j = 0; j <
i; j++)
352 value -= matrix[
in + j] * y[j];
356 for (
i = n - 1;
i >= 0;
i--) {
357 out[
i] = y[
i] / matrix[
i * n +
i];
358 for (j =
i + 1; j < n; j++)
359 out[
i] -= matrix[j * n +
i] *
out[j];
366 double *acoefficients,
int *
index,
int nb_errors,
367 double *auxiliary,
double *interpolated)
369 double *vector, *matrix;
372 av_fast_malloc(&
c->matrix, &
c->matrix_size, nb_errors * nb_errors *
sizeof(*
c->matrix));
384 for (
i = 0;
i < nb_errors;
i++) {
385 const int im =
i * nb_errors;
387 for (j =
i; j < nb_errors; j++) {
389 matrix[j * nb_errors +
i] = matrix[
im + j] = auxiliary[
abs(
index[j] -
index[
i])];
391 matrix[j * nb_errors +
i] = matrix[
im + j] = 0;
396 for (
i = 0;
i < nb_errors;
i++) {
411 double *unused1,
double *unused2,
413 const double *
src,
double *dst)
416 double max_amplitude = 0;
420 av_fast_malloc(&
c->histogram, &
c->histogram_size,
s->nb_hbins *
sizeof(*
c->histogram));
423 histogram =
c->histogram;
424 memset(histogram, 0,
sizeof(*histogram) *
s->nb_hbins);
426 for (
i = 0;
i <
s->window_size;
i++) {
434 for (
i =
s->nb_hbins - 1;
i > 1;
i--) {
437 max_amplitude =
i / (double)
s->nb_hbins;
443 if (max_amplitude > 0.) {
444 for (
i = 0;
i <
s->window_size;
i++) {
449 memset(
clip, 0,
s->ar_order *
sizeof(*
clip));
450 memset(
clip + (
s->window_size -
s->ar_order), 0,
s->ar_order *
sizeof(*
clip));
461 double *detection,
double *acoefficients,
463 const double *
src,
double *dst)
466 int i, j, nb_clicks = 0, prev = -1;
468 memset(detection, 0,
s->window_size *
sizeof(*detection));
471 for (j = 0; j <=
s->ar_order; j++) {
472 detection[
i] += acoefficients[j] *
src[
i - j];
476 for (
i = 0;
i <
s->window_size;
i++) {
481 for (
i = 0;
i <
s->window_size;
i++) {
486 for (j = prev + 1; j <
i; j++)
491 memset(click, 0,
s->ar_order *
sizeof(*click));
492 memset(click + (
s->window_size -
s->ar_order), 0,
s->ar_order *
sizeof(*click));
510 const double *
src = (
const double *)
s->in->extended_data[ch];
511 double *
is = (
double *)
s->is->extended_data[ch];
512 double *dst = (
double *)
s->out->extended_data[ch];
513 double *ptr = (
double *)
out->extended_data[ch];
514 double *buf = (
double *)
s->buffer->extended_data[ch];
515 const double *
w =
s->window_func_lut;
523 double *interpolated =
c->interpolated;
527 nb_errors =
s->detector(
s,
c, sigmae,
c->detection,
c->acoefficients,
530 double *enabled = (
double *)
s->enabled->extended_data[0];
533 nb_errors,
c->auxiliary, interpolated);
539 for (j = 0; j < nb_errors; j++) {
540 if (enabled[
index[j]]) {
541 dst[
index[j]] = interpolated[j];
547 memcpy(dst,
src,
s->window_size *
sizeof(*dst));
550 if (
s->method == 0) {
551 for (j = 0; j <
s->window_size; j++)
552 buf[j] += dst[j] *
w[j];
554 const int skip =
s->overlap_skip;
556 for (j = 0; j <
s->hop_size; j++)
557 buf[j] = dst[skip + j];
559 for (j = 0; j <
s->hop_size; j++)
562 memmove(buf, buf +
s->hop_size, (
s->window_size * 2 -
s->hop_size) *
sizeof(*buf));
563 memmove(
is,
is +
s->hop_size, (
s->window_size -
s->hop_size) *
sizeof(*
is));
564 memset(buf +
s->window_size * 2 -
s->hop_size, 0,
s->hop_size *
sizeof(*buf));
565 memset(
is +
s->window_size -
s->hop_size, 0,
s->hop_size *
sizeof(*
is));
576 int ret = 0, j, ch, detected_errors = 0;
593 for (ch = 0; ch <
s->in->channels; ch++) {
594 double *
is = (
double *)
s->is->extended_data[ch];
596 for (j = 0; j <
s->hop_size; j++) {
605 if (
s->samples_left > 0)
606 out->nb_samples =
FFMIN(
s->hop_size,
s->samples_left);
611 s->detected_errors += detected_errors;
618 if (
s->samples_left > 0) {
619 s->samples_left -=
s->hop_size;
620 if (
s->samples_left <= 0)
645 double *e = (
double *)
s->enabled->extended_data[0];
652 for (
int i = 0;
i <
in->nb_samples;
i++)
653 e[
i] = !
ctx->is_disabled;
679 if (
s->eof &&
s->samples_left <= 0) {
694 s->is_declip = !strcmp(
ctx->filter->name,
"adeclip");
710 s->is_declip ?
"clips" :
"clicks",
s->detected_errors,
711 s->nb_samples, 100. *
s->detected_errors /
s->nb_samples);
723 for (
i = 0;
i <
s->nb_channels;
i++) {
737 c->histogram_size = 0;
770 .priv_class = &adeclick_class,
806 .priv_class = &adeclip_class,
static enum AVSampleFormat sample_fmts[]
static void autocorrelation(const double *input, int order, int size, double *output, double scale)
static int detect_clips(AudioDeclickContext *s, DeclickChannel *c, double unused0, double *unused1, double *unused2, uint8_t *clip, int *index, const double *src, double *dst)
static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
static int find_index(int *index, int value, int size)
static int isfinite_array(double *samples, int nb_samples)
static int factorization(double *matrix, int n)
static int query_formats(AVFilterContext *ctx)
static int config_input(AVFilterLink *inlink)
static double autoregression(const double *samples, int ar_order, int nb_samples, double *k, double *r, double *a)
static const AVFilterPad inputs[]
static int filter_frame(AVFilterLink *inlink)
static const AVFilterPad outputs[]
static const AVOption adeclip_options[]
static int activate(AVFilterContext *ctx)
static av_cold int init(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
static int detect_clicks(AudioDeclickContext *s, DeclickChannel *c, double sigmae, double *detection, double *acoefficients, uint8_t *click, int *index, const double *src, double *dst)
static int do_interpolation(DeclickChannel *c, double *matrix, double *vector, int n, double *out)
static int interpolation(DeclickChannel *c, const double *src, int ar_order, double *acoefficients, int *index, int nb_errors, double *auxiliary, double *interpolated)
AVFILTER_DEFINE_CLASS(adeclick)
static const AVOption adeclick_options[]
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Main libavfilter public API header.
#define flags(name, subs,...)
#define is(width, name, range_min, range_max, subs,...)
static __device__ float fabs(float a)
double fmin(double, double)
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
#define FFERROR_NOT_READY
Filters implementation helper functions.
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
Peek data from an AVAudioFifo.
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
#define AVERROR_EOF
End of file.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define AV_LOG_INFO
Standard information.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
AVSampleFormat
Audio sample formats.
@ AV_SAMPLE_FMT_DBLP
double, planar
#define AV_NOPTS_VALUE
Undefined timestamp value.
static const int16_t alpha[]
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
enum MovChannelLayoutTag * layouts
Context for an Audio FIFO Buffer.
Describe the class of an AVClass context structure.
A list of supported channel layouts.
A link between two filters.
int channels
Number of channels.
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
int sample_rate
samples per second
AVFilterContext * dst
dest filter
int format
agreed upon media format
A filter pad used for either input or output.
const char * name
Pad name.
const char * name
Filter name.
AVFormatInternal * internal
An opaque field for libavformat internal usage.
This structure describes decoded (raw) audio or video data.
Rational number (pair of numerator and denominator).
int(* detector)(struct AudioDeclickContext *s, DeclickChannel *c, double sigmae, double *detection, double *acoefficients, uint8_t *click, int *index, const double *src, double *dst)
Used for passing data between threads.
static double clip(void *opaque, double val)
Clip value val in the minval - maxval range.