FFmpeg: libavfilter/af_adeclick.c Source File
Go to the documentation of this file. 1 /*
2 * Copyright (c) 2018 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
28
47
50
58
65
71
73
80
84
86 double sigmae, double *detection,
87 double *acoefficients, uint8_t *click,
int *
index,
88 const double *
src,
double *dst);
90
91 #define OFFSET(x) offsetof(AudioDeclickContext, x)
92 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
93
112 };
113
115
117 {
121
123 s->window_size =
inlink->sample_rate *
s->w / 1000.;
124 if (
s->window_size < 100)
126 s->ar_order =
FFMAX(
s->window_size *
s->ar / 100., 1);
127 s->nb_burst_samples =
s->window_size *
s->burst / 1000.;
128 s->hop_size =
s->window_size * (1. - (
s->overlap / 100.));
131
132 s->window_func_lut =
av_calloc(
s->window_size,
sizeof(*
s->window_func_lut));
133 if (!
s->window_func_lut)
135 for (
i = 0;
i <
s->window_size;
i++)
136 s->window_func_lut[
i] = sin(
M_PI *
i /
s->window_size) *
137 (1. - (
s->overlap / 100.)) *
M_PI_2;
138
148 if (!
s->in || !
s->out || !
s->buffer || !
s->is || !
s->enabled)
150
157 s->overlap_skip =
s->method ? (
s->window_size -
s->hop_size) / 2 : 0;
158 if (
s->overlap_skip > 0) {
161 }
162
163 s->nb_channels =
inlink->channels;
167
170
171 c->detection =
av_calloc(
s->window_size,
sizeof(*
c->detection));
172 c->auxiliary =
av_calloc(
s->ar_order + 1,
sizeof(*
c->auxiliary));
173 c->acoefficients =
av_calloc(
s->ar_order + 1,
sizeof(*
c->acoefficients));
174 c->acorrelation =
av_calloc(
s->ar_order + 1,
sizeof(*
c->acorrelation));
176 c->click =
av_calloc(
s->window_size,
sizeof(*
c->click));
177 c->index =
av_calloc(
s->window_size,
sizeof(*
c->index));
178 c->interpolated =
av_calloc(
s->window_size,
sizeof(*
c->interpolated));
179 if (!
c->auxiliary || !
c->acoefficients || !
c->detection || !
c->click ||
180 !
c->index || !
c->interpolated || !
c->acorrelation || !
c->tmp)
182 }
183
184 return 0;
185 }
186
189 {
191
192 for (
i = 0;
i <= order;
i++) {
194
195 for (j =
i; j <
size; j++)
197
199 }
200 }
201
204 {
207
209
211
212 /* Levinson-Durbin algorithm */
213 k[0] =
a[0] = -
r[1] /
r[0];
214 alpha =
r[0] * (1. - k[0] * k[0]);
216 double epsilon = 0.;
217
218 for (j = 0; j <
i; j++)
219 epsilon +=
a[j] *
r[
i - j];
221
224 for (j =
i - 1; j >= 0; j--)
225 k[j] =
a[j] + k[
i] *
a[
i - j - 1];
226 for (j = 0; j <=
i; j++)
228 }
229
230 k[0] = 1.;
233
235 }
236
238 {
240
243 return 0;
244
245 return 1;
246 }
247
249 {
251
253 return 1;
254
257
258 while (start <= end) {
259 i = (end + start) / 2;
261 return 0;
266 }
267
268 return 1;
269 }
270
272 {
274
275 for (
i = 0;
i < n;
i++) {
276 const int in =
i * n;
278
280 for (j = 0; j <
i; j++)
281 value -= matrix[j * n + j] * matrix[
in + j] * matrix[
in + j];
282
284 return -1;
285 }
286
288 for (j =
i + 1; j < n; j++) {
289 const int jn = j * n;
290 double x;
291
293 for (k = 0; k <
i; k++)
294 x -= matrix[k * n + k] * matrix[
in + k] * matrix[jn + k];
295 matrix[jn +
i] = x / matrix[
in +
i];
296 }
297 }
298
299 return 0;
300 }
301
303 double *vector,
int n,
double *
out)
304 {
306 double *y;
307
311
314 if (!y)
316
317 for (
i = 0;
i < n;
i++) {
318 const int in =
i * n;
320
322 for (j = 0; j <
i; j++)
323 value -= matrix[
in + j] * y[j];
325 }
326
327 for (
i = n - 1;
i >= 0;
i--) {
328 out[
i] = y[
i] / matrix[
i * n +
i];
329 for (j =
i + 1; j < n; j++)
330 out[
i] -= matrix[j * n +
i] *
out[j];
331 }
332
333 return 0;
334 }
335
337 double *acoefficients,
int *
index,
int nb_errors,
338 double *auxiliary, double *interpolated)
339 {
340 double *vector, *matrix;
342
343 av_fast_malloc(&
c->matrix, &
c->matrix_size, nb_errors * nb_errors *
sizeof(*
c->matrix));
345 if (!matrix)
347
350 if (!vector)
352
354
355 for (
i = 0;
i < nb_errors;
i++) {
356 const int im =
i * nb_errors;
357
358 for (j =
i; j < nb_errors; j++) {
360 matrix[j * nb_errors +
i] = matrix[
im + j] = auxiliary[
abs(
index[j] -
index[
i])];
361 } else {
362 matrix[j * nb_errors +
i] = matrix[
im + j] = 0;
363 }
364 }
365 }
366
367 for (
i = 0;
i < nb_errors;
i++) {
369
373
375 }
376
378 }
379
381 double unused0,
382 double *unused1, double *unused2,
384 const double *
src,
double *dst)
385 {
387 double max_amplitude = 0;
388 unsigned *histogram;
390
391 av_fast_malloc(&
c->histogram, &
c->histogram_size,
s->nb_hbins *
sizeof(*
c->histogram));
394 histogram =
c->histogram;
395 memset(histogram, 0,
sizeof(*histogram) *
s->nb_hbins);
396
397 for (
i = 0;
i <
s->window_size;
i++) {
399
403 }
404
405 for (
i =
s->nb_hbins - 1;
i > 1;
i--) {
408 max_amplitude =
i / (double)
s->nb_hbins;
409 }
410 break;
411 }
412 }
413
414 if (max_amplitude > 0.) {
415 for (
i = 0;
i <
s->window_size;
i++) {
417 }
418 }
419
420 memset(
clip, 0,
s->ar_order *
sizeof(*
clip));
421 memset(
clip + (
s->window_size -
s->ar_order), 0,
s->ar_order *
sizeof(*
clip));
422
426
427 return nb_clips;
428 }
429
431 double sigmae,
432 double *detection, double *acoefficients,
433 uint8_t *click,
int *
index,
434 const double *
src,
double *dst)
435 {
437 int i, j, nb_clicks = 0, prev = -1;
438
439 memset(detection, 0,
s->window_size *
sizeof(*detection));
440
442 for (j = 0; j <=
s->ar_order; j++) {
443 detection[
i] += acoefficients[j] *
src[
i - j];
444 }
445 }
446
447 for (
i = 0;
i <
s->window_size;
i++) {
450 }
451
452 for (
i = 0;
i <
s->window_size;
i++) {
454 continue;
455
457 for (j = prev + 1; j <
i; j++)
458 click[j] = 1;
460 }
461
462 memset(click, 0,
s->ar_order *
sizeof(*click));
463 memset(click + (
s->window_size -
s->ar_order), 0,
s->ar_order *
sizeof(*click));
464
468
469 return nb_clicks;
470 }
471
475
477 {
481 const double *
src = (
const double *)
s->in->extended_data[ch];
482 double *
is = (
double *)
s->is->extended_data[ch];
483 double *dst = (
double *)
s->out->extended_data[ch];
484 double *ptr = (
double *)
out->extended_data[ch];
485 double *buf = (
double *)
s->buffer->extended_data[ch];
486 const double *
w =
s->window_func_lut;
488 double sigmae;
490
492
494 double *interpolated =
c->interpolated;
496 int nb_errors;
497
498 nb_errors =
s->detector(
s,
c, sigmae,
c->detection,
c->acoefficients,
500 if (nb_errors > 0) {
501 double *enabled = (
double *)
s->enabled->extended_data[0];
502
504 nb_errors,
c->auxiliary, interpolated);
507
509
510 for (j = 0; j < nb_errors; j++) {
511 if (enabled[
index[j]]) {
512 dst[
index[j]] = interpolated[j];
514 }
515 }
516 }
517 } else {
518 memcpy(dst,
src,
s->window_size *
sizeof(*dst));
519 }
520
521 if (
s->method == 0) {
522 for (j = 0; j <
s->window_size; j++)
523 buf[j] += dst[j] *
w[j];
524 } else {
525 const int skip =
s->overlap_skip;
526
527 for (j = 0; j <
s->hop_size; j++)
528 buf[j] = dst[skip + j];
529 }
530 for (j = 0; j <
s->hop_size; j++)
531 ptr[j] = buf[j];
532
533 memmove(buf, buf +
s->hop_size, (
s->window_size * 2 -
s->hop_size) *
sizeof(*buf));
534 memmove(
is,
is +
s->hop_size, (
s->window_size -
s->hop_size) *
sizeof(*
is));
535 memset(buf +
s->window_size * 2 -
s->hop_size, 0,
s->hop_size *
sizeof(*buf));
536 memset(
is +
s->window_size -
s->hop_size, 0,
s->hop_size *
sizeof(*
is));
537
538 return 0;
539 }
540
542 {
547 int ret = 0, j, ch, detected_errors = 0;
549
553
558
563
564 for (ch = 0; ch <
s->in->channels; ch++) {
565 double *
is = (
double *)
s->is->extended_data[ch];
566
567 for (j = 0; j <
s->hop_size; j++) {
569 detected_errors++;
570 }
571 }
572
575
576 if (
s->samples_left > 0)
577 out->nb_samples =
FFMIN(
s->hop_size,
s->samples_left);
578
581
582 s->detected_errors += detected_errors;
583 s->nb_samples +=
out->nb_samples *
inlink->channels;
584
588
589 if (
s->samples_left > 0) {
590 s->samples_left -=
s->hop_size;
591 if (
s->samples_left <= 0)
593 }
594
599 }
600
602 {
609
611
616 double *e = (
double *)
s->enabled->extended_data[0];
617
620
624 e[
i] = !
ctx->is_disabled;
625
630 }
631
635
638 return 0;
639 }
640
646 return 0;
647 }
648 }
649
650 if (
s->eof &&
s->samples_left <= 0) {
652 return 0;
653 }
654
657
659 }
660
662 {
664
665 s->is_declip = !strcmp(
ctx->filter->name,
"adeclip");
668 } else {
670 }
671
672 return 0;
673 }
674
676 {
679
681 s->is_declip ?
"clips" :
"clicks",
s->detected_errors,
682 s->nb_samples, 100. *
s->detected_errors /
s->nb_samples);
683
692
694 for (
i = 0;
i <
s->nb_channels;
i++) {
696
708 c->histogram_size = 0;
713 }
714 }
717 }
718
720 {
724 },
725 };
726
728 {
731 },
732 };
733
738 .priv_class = &adeclick_class,
746 };
747
766 };
767
769
774 .priv_class = &adeclip_class,
782 };
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
they must not be accessed directly The fifo field contains the frames that are queued in the input for processing by the filter The status_in and status_out fields contains the queued status(EOF or error) of the link
static const AVFilterPad outputs[]
Filter the word "frame" indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
The official guide to swscale for confused that is
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
#define AVERROR_EOF
End of file.
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
This structure describes decoded (raw) audio or video data.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
static av_cold int init(AVFilterContext *ctx)
const char * name
Filter name.
A link between two filters.
static int activate(AVFilterContext *ctx)
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
const AVFilter ff_af_adeclip
Context for an Audio FIFO Buffer.
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
static av_always_inline float scale(float x, float s)
A filter pad used for either input or output.
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
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 av_cold void uninit(AVFilterContext *ctx)
#define FILTER_INPUTS(array)
static int config_input(AVFilterLink *inlink)
static void autocorrelation(const double *input, int order, int size, double *output, double scale)
Describe the class of an AVClass context structure.
static __device__ float fabs(float a)
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.
Rational number (pair of numerator and denominator).
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
int(* detector)(struct AudioDeclickContext *s, DeclickChannel *c, double sigmae, double *detection, double *acoefficients, uint8_t *click, int *index, const double *src, double *dst)
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
static const AVOption adeclick_options[]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
double fmin(double, double)
#define AV_NOPTS_VALUE
Undefined timestamp value.
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
FF_FILTER_FORWARD_WANTED(outlink, inlink)
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
static double autoregression(const double *samples, int ar_order, int nb_samples, double *k, double *r, double *a)
static int interpolation(DeclickChannel *c, const double *src, int ar_order, double *acoefficients, int *index, int nb_errors, double *auxiliary, double *interpolated)
#define AV_LOG_INFO
Standard information.
static int detect_clips(AudioDeclickContext *s, DeclickChannel *c, double unused0, double *unused1, double *unused2, uint8_t *clip, int *index, const double *src, double *dst)
int nb_samples
number of audio samples (per channel) described by this frame
static int filter_frame(AVFilterLink *inlink)
#define i(width, name, range_min, range_max)
static int find_index(int *index, int value, int size)
static const AVOption adeclip_options[]
uint8_t ** extended_data
pointers to the data planes/channels.
Used for passing data between threads.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
const char * name
Pad name.
void * av_calloc(size_t nmemb, size_t size)
static int factorization(double *matrix, int n)
AVFILTER_DEFINE_CLASS(adeclick)
static const AVFilterPad inputs[]
@ AV_SAMPLE_FMT_DBLP
double, planar
Filter the word "frame" indicates either a video frame or a group of audio samples
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
static const int16_t alpha[]
static int do_interpolation(DeclickChannel *c, double *matrix, double *vector, int n, double *out)
#define FILTER_OUTPUTS(array)
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
const AVFilter ff_af_adeclick
static int isfinite_array(double *samples, int nb_samples)
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
Peek data from an AVAudioFifo.
static double clip(void *opaque, double val)
Clip value val in the minval - maxval range.
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Generated on Wed Aug 24 2022 21:38:04 for FFmpeg by
doxygen
1.8.17