86 double sigmae,
double *detection,
87 double *acoefficients, uint8_t *click,
int *
index,
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
124 s->ar_order =
FFMAX(
s->window_size *
s->ar / 100., 1);
125 s->nb_burst_samples =
s->window_size *
s->burst / 1000.;
126 s->hop_size =
FFMAX(1,
s->window_size * (1. - (
s->overlap / 100.)));
128 s->window_func_lut =
av_calloc(
s->window_size,
sizeof(*
s->window_func_lut));
129 if (!
s->window_func_lut)
133 double *tx_in[2] = {
NULL }, *tx_out[2] = {
NULL };
139 tx_size = 1 << (32 -
ff_clz(
s->window_size));
146 scale = 1.0 / tx_size;
151 tx_in[0] =
av_calloc(tx_size + 2,
sizeof(*tx_in[0]));
152 tx_in[1] =
av_calloc(tx_size + 2,
sizeof(*tx_in[1]));
153 tx_out[0] =
av_calloc(tx_size + 2,
sizeof(*tx_out[0]));
154 tx_out[1] =
av_calloc(tx_size + 2,
sizeof(*tx_out[1]));
155 if (!tx_in[0] || !tx_in[1] || !tx_out[0] || !tx_out[1]) {
160 for (
int n = 0; n <
s->window_size -
s->hop_size; n++)
163 for (
int n = 0; n <
s->hop_size; n++)
166 tx_fn(tx, tx_out[0], tx_in[0],
sizeof(
double));
167 tx_fn(tx, tx_out[1], tx_in[1],
sizeof(
double));
169 for (
int n = 0; n <= tx_size/2; n++) {
170 double re0 = tx_out[0][2*n];
171 double im0 = tx_out[0][2*n+1];
172 double re1 = tx_out[1][2*n];
173 double im1 = tx_out[1][2*n+1];
175 tx_in[0][2*n] = re0 * re1 - im0 * im1;
176 tx_in[0][2*n+1] = re0 * im1 + re1 * im0;
181 scale = 1.0 / (
s->window_size -
s->hop_size);
182 for (
int n = 0; n <
s->window_size; n++)
183 s->window_func_lut[n] = tx_out[0][n] *
scale;
207 if (!
s->in || !
s->out || !
s->buffer || !
s->is || !
s->enabled)
216 s->overlap_skip =
s->method ? (
s->window_size -
s->hop_size) / 2 : 0;
217 if (
s->overlap_skip > 0) {
230 c->detection =
av_calloc(
s->window_size,
sizeof(*
c->detection));
231 c->auxiliary =
av_calloc(
s->ar_order + 1,
sizeof(*
c->auxiliary));
232 c->acoefficients =
av_calloc(
s->ar_order + 1,
sizeof(*
c->acoefficients));
233 c->acorrelation =
av_calloc(
s->ar_order + 1,
sizeof(*
c->acorrelation));
235 c->click =
av_calloc(
s->window_size,
sizeof(*
c->click));
236 c->index =
av_calloc(
s->window_size,
sizeof(*
c->index));
237 c->interpolated =
av_calloc(
s->window_size,
sizeof(*
c->interpolated));
238 if (!
c->auxiliary || !
c->acoefficients || !
c->detection || !
c->click ||
239 !
c->index || !
c->interpolated || !
c->acorrelation || !
c->tmp)
247 double *output,
double scale)
251 for (
i = 0;
i <= order;
i++) {
254 for (j =
i; j <
size; j++)
255 value += input[j] * input[j -
i];
272 k[0] =
a[0] = -
r[1] /
r[0];
273 alpha =
r[0] * (1. - k[0] * k[0]);
277 for (j = 0; j <
i; j++)
278 epsilon +=
a[j] *
r[
i - j];
283 for (j =
i - 1; j >= 0; j--)
284 k[j] =
a[j] + k[
i] *
a[
i - j - 1];
285 for (j = 0; j <=
i; j++)
317 while (start <= end) {
318 i = (end + start) / 2;
334 for (
i = 0;
i < n;
i++) {
335 const int in =
i * n;
339 for (j = 0; j <
i; j++)
347 for (j =
i + 1; j < n; j++) {
348 const int jn = j * n;
352 for (k = 0; k <
i; k++)
362 double *vector,
int n,
double *
out)
376 for (
i = 0;
i < n;
i++) {
377 const int in =
i * n;
381 for (j = 0; j <
i; j++)
386 for (
i = n - 1;
i >= 0;
i--) {
388 for (j =
i + 1; j < n; j++)
396 double *acoefficients,
int *
index,
int nb_errors,
397 double *auxiliary,
double *interpolated)
402 av_fast_malloc(&
c->matrix, &
c->matrix_size, nb_errors * nb_errors *
sizeof(*
c->matrix));
414 for (
i = 0;
i < nb_errors;
i++) {
415 const int im =
i * nb_errors;
417 for (j =
i; j < nb_errors; j++) {
426 for (
i = 0;
i < nb_errors;
i++) {
441 double *unused1,
double *unused2,
443 const double *
src,
double *
dst)
446 double max_amplitude = 0;
450 av_fast_malloc(&
c->histogram, &
c->histogram_size,
s->nb_hbins *
sizeof(*
c->histogram));
453 histogram =
c->histogram;
454 memset(histogram, 0,
sizeof(*histogram) *
s->nb_hbins);
456 for (
i = 0;
i <
s->window_size;
i++) {
464 for (
i =
s->nb_hbins - 1;
i > 1;
i--) {
467 max_amplitude =
i / (
double)
s->nb_hbins;
473 if (max_amplitude > 0.) {
474 for (
i = 0;
i <
s->window_size;
i++) {
479 memset(
clip, 0,
s->ar_order *
sizeof(*
clip));
480 memset(
clip + (
s->window_size -
s->ar_order), 0,
s->ar_order *
sizeof(*
clip));
491 double *detection,
double *acoefficients,
492 uint8_t *click,
int *
index,
493 const double *
src,
double *
dst)
496 int i, j, nb_clicks = 0, prev = -1;
498 memset(detection, 0,
s->window_size *
sizeof(*detection));
501 for (j = 0; j <=
s->ar_order; j++) {
502 detection[
i] += acoefficients[j] *
src[
i - j];
506 for (
i = 0;
i <
s->window_size;
i++) {
511 for (
i = 0;
i <
s->window_size;
i++) {
516 for (j = prev + 1; j <
i; j++)
521 memset(click, 0,
s->ar_order *
sizeof(*click));
522 memset(click + (
s->window_size -
s->ar_order), 0,
s->ar_order *
sizeof(*click));
540 const double *
src = (
const double *)
s->in->extended_data[ch];
541 double *
is = (
double *)
s->is->extended_data[ch];
542 double *
dst = (
double *)
s->out->extended_data[ch];
543 double *ptr = (
double *)
out->extended_data[ch];
544 double *buf = (
double *)
s->buffer->extended_data[ch];
545 const double *
w =
s->window_func_lut;
553 double *interpolated =
c->interpolated;
557 nb_errors =
s->detector(
s,
c, sigmae,
c->detection,
c->acoefficients,
560 double *enabled = (
double *)
s->enabled->extended_data[0];
563 nb_errors,
c->auxiliary, interpolated);
569 for (j = 0; j < nb_errors; j++) {
570 if (enabled[
index[j]]) {
577 memcpy(
dst,
src,
s->window_size *
sizeof(*
dst));
580 if (
s->method == 0) {
581 for (j = 0; j <
s->window_size; j++)
582 buf[j] +=
dst[j] *
w[j];
584 const int skip =
s->overlap_skip;
586 for (j = 0; j <
s->hop_size; j++)
589 for (j = 0; j <
s->hop_size; j++)
592 memmove(buf, buf +
s->hop_size, (
s->window_size * 2 -
s->hop_size) *
sizeof(*buf));
593 memmove(
is,
is +
s->hop_size, (
s->window_size -
s->hop_size) *
sizeof(*
is));
594 memset(buf +
s->window_size * 2 -
s->hop_size, 0,
s->hop_size *
sizeof(*buf));
595 memset(
is +
s->window_size -
s->hop_size, 0,
s->hop_size *
sizeof(*
is));
606 int ret = 0, j, ch, detected_errors = 0;
623 for (ch = 0; ch <
s->in->ch_layout.nb_channels; ch++) {
624 double *
is = (
double *)
s->is->extended_data[ch];
626 for (j = 0; j <
s->hop_size; j++) {
635 if (
s->samples_left > 0)
636 out->nb_samples =
FFMIN(
s->hop_size,
s->samples_left);
641 s->detected_errors += detected_errors;
648 if (
s->samples_left > 0) {
649 s->samples_left -=
s->hop_size;
650 if (
s->samples_left <= 0)
675 double *e = (
double *)
s->enabled->extended_data[0];
683 e[
i] = !
ctx->is_disabled;
709 if (
s->eof &&
s->samples_left <= 0) {
724 s->is_declip = !strcmp(
ctx->filter->name,
"adeclip");
739 if (
s->nb_samples > 0)
741 s->is_declip ?
"clips" :
"clicks",
s->detected_errors,
742 s->nb_samples, 100. *
s->detected_errors /
s->nb_samples);
754 for (
i = 0;
i <
s->nb_channels;
i++) {
768 c->histogram_size = 0;
788 .p.name =
"adeclick",
790 .p.priv_class = &adeclick_class,
826 .p.priv_class = &adeclip_class,
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static const AVFilterPad inputs[]
static int config_input(AVFilterLink *inlink)
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)
const FFFilter ff_af_adeclick
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 int filter_frame(AVFilterLink *inlink)
static const AVOption adeclip_options[]
static int activate(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)
const FFFilter ff_af_adeclip
static const AVOption adeclick_options[]
static AVFormatContext * ctx
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
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_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
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.
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
#define is(width, name, range_min, range_max, subs,...)
#define i(width, name, range_min, range_max)
static __device__ float fabs(float a)
double fmin(double, double)
int(* init)(AVBSFContext *ctx)
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
#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(const AVAudioFifo *af, void *const *data, int nb_samples)
Peek data from an AVAudioFifo.
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
int av_audio_fifo_write(AVAudioFifo *af, void *const *data, int nb_samples)
Write data to an AVAudioFifo.
void av_audio_fifo_free(AVAudioFifo *af)
Free 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.
@ AV_SAMPLE_FMT_DBLP
double, planar
#define AV_NOPTS_VALUE
Undefined timestamp value.
static const int16_t alpha[]
static void scale(int *out, const int *in, const int w, const int h, const int shift)
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static void filter_channel(MLPDecodeContext *m, unsigned int substr, unsigned int channel)
Generate PCM samples using the prediction filters and residual values read from the data stream,...
#define FILTER_INPUTS(array)
#define FILTER_OUTPUTS(array)
#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 and internal structures.
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
#define AVFILTER_DEFINE_CLASS(fname)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
Context for an Audio FIFO Buffer.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
A link between two filters.
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
AVChannelLayout ch_layout
channel layout of current buffer (see libavutil/channel_layout.h)
AVFilterContext * dst
dest filter
int format
agreed upon media format
A filter pad used for either input or output.
This structure describes decoded (raw) audio or video data.
int nb_samples
number of audio samples (per channel) described by this frame
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
uint8_t ** extended_data
pointers to the data planes/channels.
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.
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.