33 #define C (M_LN10 * 0.1) 35 #define RRATIO (1.0 - RATIO) 133 int noise_band_edge[17];
143 #define OFFSET(x) offsetof(AudioFFTDeNoiseContext, x) 144 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM 145 #define AFR AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM 175 d1 = 10.0 * log(1.0 + d1 * d1) /
M_LN10;
177 d2 = 10.0 * log(1.0 + d2 * d2) /
M_LN10;
179 d3 = 10.0 * log(1.0 + d3 * d3) /
M_LN10;
181 return lrint(-d1 + d2 - d3);
186 for (
int i = 0;
i < size - 1;
i++) {
187 for (
int j =
i + 1; j <
size; j++) {
188 double d = array[j +
i *
size] / array[
i +
i *
size];
190 array[j +
i *
size] = d;
191 for (
int k =
i + 1; k <
size; k++) {
192 array[j + k *
size] -= d * array[
i + k *
size];
198 static void solve(
double *matrix,
double *vector,
int size)
200 for (
int i = 0;
i < size - 1;
i++) {
201 for (
int j =
i + 1; j <
size; j++) {
202 double d = matrix[j +
i *
size];
203 vector[j] -= d * vector[
i];
207 vector[size - 1] /= matrix[size * size - 1];
209 for (
int i = size - 2;
i >= 0;
i--) {
210 double d = vector[
i];
211 for (
int j =
i + 1; j <
size; j++)
212 d -= matrix[
i + j * size] * vector[j];
213 vector[
i] = d / matrix[
i +
i *
size];
221 double product, sum,
f;
227 for (
int j = 0; j < 5; j++) {
229 for (
int k = 0; k < 15; k++)
236 f = 15.0 + log(f / 1.5) / log(1.5);
239 for (
int j = 0; j < 5; j++) {
251 double d1 = 0.0, d2 = 1.0;
254 for (
int k = start; k <
end; k++) {
262 }
else if (d2 < 1.0
E-100) {
271 d2 = log(d2) + 230.2585 *
i;
286 return (b * a - 1.0) / (b + a - 2.0);
288 return (b * a - 2.0 * a + 1.0) / (b -
a);
296 double d1, d2, d3,
gain;
299 d1 = fft_data[0].
re * fft_data[0].
re;
303 gain = d3 / (1.0 + d3);
304 gain *= (gain + M_PI_4 /
fmax(d2, 1.0
E-6));
305 prior[0] = (d2 *
gain);
311 d1 = fft_data[
i].
re * fft_data[
i].
re + fft_data[
i].
im * fft_data[
i].
im;
318 gain = d3 / (1.0 + d3);
319 gain *= (gain + M_PI_4 /
fmax(d2, 1.0
E-6));
320 prior[
i] = d2 *
gain;
325 d1 = fft_data[0].
im * fft_data[0].
im;
332 gain = d3 / (1.0 + d3);
333 gain *= gain + M_PI_4 /
fmax(d2, 1.0
E-6);
354 for (k = i1 - 1; k >= 0; k--) {
366 double sum = 0.0,
min,
max;
369 for (i = i1 - 1; i > k; i--) {
376 min = 3.0E-4 * i *
i;
378 min = 3.0E-4 * (8 * i - 16);
381 max = 2.0E-4 * i *
i;
383 max = 2.0E-4 * (4 * i - 4);
408 sum = av_clipd(sum, -
min, max);
410 for (
int i = 0; i < 15; i++)
446 double limit = sqrt(dnch->
abs_var[0] / dnch->
amt[0]);
471 gain = dnch->
gain[0];
478 gain = dnch->
gain[
i];
487 double d = x / 7500.0;
489 return 13.0 * atan(7.6
E-4 * x) + 3.5 * atan(d * d);
517 int i = 0, j = 0, k = 0;
536 dnch->
rel_var[m] =
exp((d5 * d3 + band_noise * d4) *
C);
540 for (i = 0; i < 15; i++)
552 char *p, *
arg, *saveptr =
NULL;
562 for (i = 0; i < 15; i++) {
563 if (!(arg =
av_strtok(p,
"| ", &saveptr)))
568 ret =
av_sscanf(arg,
"%d", &band_noise[i]);
574 band_noise[
i] = av_clip(band_noise[i], -24, 24);
578 memcpy(dnch->
band_noise, band_noise,
sizeof(band_noise));
603 for (
int ch = 0; ch < s->
channels; ch++) {
614 double wscale, sar, sum, sdiv;
632 for (i = 1; i < 15; i++) {
645 for (j = 0; j < 5; j++) {
646 for (k = 0; k < 5; k++) {
648 for (m = 0; m < 15; m++)
649 s->
matrix_a[j + k * 5] += pow(m, j + k);
656 for (j = 0; j < 5; j++)
657 for (k = 0; k < 15; k++)
661 for (j = 0; j < 15; j++)
662 for (k = 0; k < 5; k++)
681 for (
int ch = 0; ch < inlink->
channels; ch++) {
686 for (i = 0; i < 15; i++)
690 for (i = 0; i < 15; i++)
694 for (i = 0; i < 15; i++)
707 for (i = 0; i < 512; i++)
713 for (i = 0; i < 512; i += j) {
755 for (
int ch = 0; ch < inlink->
channels; ch++) {
762 p1 = pow(0.1, 2.5 / sdiv);
763 p2 = pow(0.1, 1.0 / sdiv);
779 prior_band_excit[m] = 0.0;
794 if (i <
lrint(12.0 * sdiv)) {
795 dnch->
band_excit[
i] = pow(0.1, 1.45 + 0.1 * i / sdiv);
797 dnch->
band_excit[
i] = pow(0.1, 2.5 - 0.2 * (i / sdiv - 14.0));
818 double d7 =
fmin(0.008 + 2.2 / d6, 0.03);
843 for (
int j = 1; j < 16; j++) {
860 double d1, d2, d3, d4, d5, d6, d7, d8, d9, d10;
871 for (i = 1; i < len / 4; i++) {
873 d2 = 0.5 * (in[
i].
re + in[k].
re);
874 d1 = 0.5 * (in[
i].
im - in[k].
im);
875 d4 = 0.5 * (in[
i].
im + in[k].
im);
876 d3 = 0.5 * (in[k].
re - in[
i].
re);
877 in[
i].
re = d2 + d9 * d4 + d6 * d3;
878 in[
i].
im = d1 + d9 * d3 - d6 * d4;
879 in[k].
re = d2 - d9 * d4 - d6 * d3;
880 in[k].
im = -d1 + d9 * d3 - d6 * d4;
882 d9 += d9 * d8 - d6 * d7;
883 d6 += d6 * d8 + d10 * d7;
887 in[0].
re = d2 + in[0].
im;
888 in[0].
im = d2 - in[0].
im;
893 double d1, d2, d3, d4, d5, d6, d7, d8, d9, d10;
903 for (i = 1; i < len / 4; i++) {
905 d2 = 0.5 * (in[
i].
re + in[k].
re);
906 d1 = 0.5 * (in[
i].
im - in[k].
im);
907 d4 = 0.5 * (in[
i].
re - in[k].
re);
908 d3 = 0.5 * (in[
i].
im + in[k].
im);
909 in[
i].
re = d2 - d9 * d3 - d6 * d4;
910 in[
i].
im = d1 + d9 * d4 - d6 * d3;
911 in[k].
re = d2 + d9 * d3 + d6 * d4;
912 in[k].
im = -d1 + d9 * d4 - d6 * d3;
914 d9 += d9 * d8 - d6 * d7;
915 d6 += d6 * d8 + d10 * d7;
918 in[0].
re = 0.5 * (d2 + in[0].
im);
919 in[0].
im = 0.5 * (d2 - in[0].
im);
924 for (
int i = 0;
i < 15;
i++) {
937 double mag2, var = 0.0, avr = 0.0, avi = 0.0;
938 int edge, j, k, n, edgemax;
964 for (
int i = j;
i <= edgemax;
i++) {
965 if ((
i == j) && (
i < edgemax)) {
1002 double *sample_noise)
1015 sample_noise[
i] = sample_noise[
i - 1];
1021 double *sample_noise,
1024 int new_band_noise[15];
1026 double sum = 0.0, d1;
1027 float new_noise_floor;
1030 for (
int m = 0; m < 15; m++)
1031 temp[m] = sample_noise[m];
1035 for (
int m = 0; m < 5; m++) {
1037 for (n = 0; n < 15; n++)
1043 for (
int m = 0; m < 15; m++) {
1045 for (n = 0; n < 5; n++)
1052 for (
int m = 0; m < 15; m++)
1055 d1 = (
int)(sum / 15.0 - 0.5);
1057 i =
lrint(temp[7] - d1);
1059 for (d1 -= dnch->
band_noise[7] - i; d1 > -20.0; d1 -= 1.0)
1062 for (
int m = 0; m < 15; m++)
1065 new_noise_floor = d1 + 2.5;
1069 for (
int m = 0; m < 15; m++) {
1070 new_band_noise[m] =
lrint(temp[m]);
1071 new_band_noise[m] = av_clip(new_band_noise[m], -24, 24);
1075 memcpy(dnch->
band_noise, new_band_noise,
sizeof(new_band_noise));
1091 const int start = (in->
channels * jobnr) / nb_jobs;
1092 const int end = (in->
channels * (jobnr+1)) / nb_jobs;
1094 for (
int ch = start; ch <
end; ch++) {
1149 levels[
i] = levels[
i - 1];
1152 for (
int i = 0;
i < 15;
i++) {
1176 for (
int ch = 0; ch < inlink->
channels; ch++) {
1189 for (
int ch = 0; ch < inlink->
channels; ch++) {
1199 for (
int ch = 0; ch < inlink->
channels; ch++) {
1207 for (
int ch = 0; ch < inlink->
channels; ch++) {
1209 double sample_noise[15];
1230 for (
int ch = 0; ch < inlink->
channels; ch++) {
1233 float *orig = (
float *)in->extended_data[ch];
1247 dst[m] = orig[m] - src[m];
1318 for (
int ch = 0; ch < s->
channels; ch++) {
1375 char *res,
int res_len,
int flags)
1381 if (!strcmp(cmd,
"sample_noise") ||
1382 !strcmp(cmd,
"sn")) {
1383 if (!strcmp(args,
"start")) {
1386 }
else if (!strcmp(args,
"end") ||
1387 !strcmp(args,
"stop")) {
1426 .priv_class = &afftdn_class,
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
double noise_band_var[15]
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
double noise_band_norm[15]
This structure describes decoded (raw) audio or video data.
av_cold void av_fft_end(FFTContext *s)
Main libavfilter public API header.
static void set_parameters(AudioFFTDeNoiseContext *s)
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
static const AVOption afftdn_options[]
static void set_noise_profile(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, double *sample_noise, int new_profile)
#define FFERROR_NOT_READY
Filters implementation helper functions.
void av_fft_permute(FFTContext *s, FFTComplex *z)
Do the permutation needed BEFORE calling ff_fft_calc().
static int process_get_band_noise(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, int band)
static int config_input(AVFilterLink *inlink)
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
static int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
static void factor(double *array, int size)
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
static double freq2bark(double x)
static av_cold int end(AVCodecContext *avctx)
double noise_band_sample[15]
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
A filter pad used for either input or output.
static int query_formats(AVFilterContext *ctx)
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
A link between two filters.
#define i(width, name, range_min, range_max)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int sample_rate
samples per second
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options...
void * priv
private data for use by the filter
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link...
double * prior_band_excit
FFTContext * av_fft_init(int nbits, int inverse)
Set up a complex FFT.
static int get_band_edge(AudioFFTDeNoiseContext *s, int band)
static void get_auto_noise_levels(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, double *levels)
int av_sscanf(const char *string, const char *format,...)
See libc sscanf manual for more information.
double noise_band_auto_var[15]
Context for an Audio FIFO Buffer.
float last_noise_reduction
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
int channels
number of audio channels, only used for audio.
audio channel layout utility functions
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
static av_cold void uninit(AVFilterContext *ctx)
static int get_band_noise(AudioFFTDeNoiseContext *s, int band, double a, double b, double c)
float fmaxf(float, float)
static void read_custom_noise(AudioFFTDeNoiseContext *s, int ch)
static void set_band_parameters(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch)
static void preprocess(FFTComplex *in, int len)
static int output_frame(AVFilterLink *inlink)
static void postprocess(FFTComplex *in, int len)
double noise_band_avr[15]
int format
agreed upon media format
A list of supported channel layouts.
#define AV_LOG_INFO
Standard information.
char * av_strdup(const char *s)
Duplicate a string.
AVSampleFormat
Audio sample formats.
Used for passing data between threads.
static const AVFilterPad outputs[]
double fmax(double, double)
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
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)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Describe the class of an AVClass context structure.
double noise_band_avi[15]
static double limit_gain(double a, double b)
Rational number (pair of numerator and denominator).
AVFILTER_DEFINE_CLASS(afftdn)
const char * name
Filter name.
static void finish_sample_noise(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, double *sample_noise)
AVFilterLink ** outputs
array of pointers to output links
enum MovChannelLayoutTag * layouts
static void solve(double *matrix, double *vector, int size)
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
#define flags(name, subs,...)
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok()...
float last_residual_floor
static void process_frame(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, FFTComplex *fft_data, double *prior, double *prior_band_excit, int track_noise)
static int activate(AVFilterContext *ctx)
double fmin(double, double)
int channels
Number of channels.
avfilter_execute_func * execute
AVFilterContext * dst
dest filter
static void init_sample_noise(DeNoiseChannel *dnch)
int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
Peek data from an AVAudioFifo.
static enum AVSampleFormat sample_fmts[]
static const AVFilterPad inputs[]
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
static int array[MAX_W *MAX_W]
uint8_t ** extended_data
pointers to the data planes/channels.
static void sample_noise_block(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, AVFrame *in, int ch)
void av_fft_calc(FFTContext *s, FFTComplex *z)
Do a complex FFT with the parameters defined in av_fft_init().
static int get_band_centre(AudioFFTDeNoiseContext *s, int band)
int nb_samples
number of audio samples (per channel) described by this frame
#define AV_NOPTS_VALUE
Undefined timestamp value.
static void calculate_sfm(AudioFFTDeNoiseContext *s, DeNoiseChannel *dnch, int start, int end)