33 #define C (M_LN10 * 0.1)
35 #define RRATIO (1.0 - RATIO)
143 #define OFFSET(x) offsetof(AudioFFTDeNoiseContext, x)
144 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
174 d1 = 10.0 * log(1.0 + d1 * d1) /
M_LN10;
176 d2 = 10.0 * log(1.0 + d2 * d2) /
M_LN10;
178 d3 = 10.0 * log(1.0 + d3 * d3) /
M_LN10;
180 return lrint(-d1 + d2 - d3);
185 for (
int i = 0; i < size - 1; i++) {
186 for (
int j = i + 1; j <
size; j++) {
187 double d = array[j + i *
size] / array[i + i *
size];
189 array[j + i *
size] = d;
190 for (
int k = i + 1; k <
size; k++) {
191 array[j + k *
size] -= d * array[i + k *
size];
197 static void solve(
double *matrix,
double *vector,
int size)
199 for (
int i = 0; i < size - 1; i++) {
200 for (
int j = i + 1; j <
size; j++) {
201 double d = matrix[j + i *
size];
202 vector[j] -= d * vector[i];
206 vector[size - 1] /= matrix[size * size - 1];
208 for (
int i = size - 2; i >= 0; i--) {
209 double d = vector[i];
210 for (
int j = i + 1; j <
size; j++)
211 d -= matrix[i + j * size] * vector[j];
212 vector[i] = d / matrix[i + i *
size];
220 double product, sum,
f;
226 for (
int j = 0; j < 5; j++) {
228 for (
int k = 0; k < 15; k++)
235 f = 15.0 + log(f / 1.5) / log(1.5);
238 for (
int j = 0; j < 5; j++) {
250 double d1 = 0.0, d2 = 1.0;
253 for (
int k = start; k <
end; k++) {
261 }
else if (d2 < 1.0
E-100) {
270 d2 = log(d2) + 230.2585 * i;
285 return (b * a - 1.0) / (b + a - 2.0);
287 return (b * a - 2.0 * a + 1.0) / (b -
a);
293 double *prior,
double *prior_band_excit,
int track_noise)
295 double d1, d2, d3, gain;
298 d1 = fft_data[0].
re * fft_data[0].
re;
301 d3 =
RATIO * prior[0] +
RRATIO * fmax(d2 - 1.0, 0.0);
302 gain = d3 / (1.0 + d3);
303 gain *= (gain + M_PI_4 / fmax(d2, 1.0
E-6));
304 prior[0] = (d2 * gain);
307 dnch->
gain[0] = gain;
310 d1 = fft_data[i].
re * fft_data[i].
re + fft_data[i].
im * fft_data[i].
im;
316 d3 =
RATIO * prior[i] +
RRATIO * fmax(d2 - 1.0, 0.0);
317 gain = d3 / (1.0 + d3);
318 gain *= (gain + M_PI_4 / fmax(d2, 1.0
E-6));
319 prior[i] = d2 * gain;
322 dnch->
gain[i] = gain;
324 d1 = fft_data[0].
im * fft_data[0].
im;
331 gain = d3 / (1.0 + d3);
332 gain *= gain + M_PI_4 / fmax(d2, 1.0
E-6);
353 for (k = i1 - 1; k >= 0; k--) {
365 double sum = 0.0,
min, max;
368 for (i = i1 - 1; i > k; i--) {
375 min = 3.0E-4 * i * i;
377 min = 3.0E-4 * (8 * i - 16);
380 max = 2.0E-4 * i * i;
382 max = 2.0E-4 * (4 * i - 4);
407 sum = av_clipd(sum, -
min, max);
409 for (
int i = 0; i < 15; i++)
445 double limit = sqrt(dnch->
abs_var[0] / dnch->
amt[0]);
463 double limit = sqrt(dnch->
abs_var[i] / dnch->
amt[i]);
470 gain = dnch->
gain[0];
472 fft_data[0].
re *= gain;
475 fft_data[0].
im *= gain;
477 gain = dnch->
gain[i];
479 fft_data[i].
re *= gain;
480 fft_data[i].
im *= gain;
486 double d = x / 7500.0;
488 return 13.0 * atan(7.6
E-4 * x) + 3.5 * atan(d * d);
515 double band_noise, d2, d3, d4, d5;
516 int i = 0, j = 0, k = 0;
535 dnch->
rel_var[m] =
exp((d5 * d3 + band_noise * d4) *
C);
539 for (i = 0; i < 15; i++)
551 char *p, *
arg, *saveptr =
NULL;
552 int i, ret, band_noise[15] = { 0 };
561 for (i = 0; i < 15; i++) {
562 if (!(arg =
av_strtok(p,
"| ", &saveptr)))
567 ret = sscanf(arg,
"%d", &band_noise[i]);
573 band_noise[i] = av_clip(band_noise[i], -24, 24);
577 memcpy(dnch->
band_noise, band_noise,
sizeof(band_noise));
613 double wscale, sar, sum, sdiv;
631 for (i = 1; i < 15; i++) {
644 for (j = 0; j < 5; j++) {
645 for (k = 0; k < 5; k++) {
647 for (m = 0; m < 15; m++)
648 s->
matrix_a[j + k * 5] += pow(m, j + k);
655 for (j = 0; j < 5; j++)
656 for (k = 0; k < 15; k++)
660 for (j = 0; j < 15; j++)
661 for (k = 0; k < 5; k++)
685 for (i = 0; i < 15; i++)
689 for (i = 0; i < 15; i++)
693 for (i = 0; i < 15; i++)
706 for (i = 0; i < 512; i++)
712 for (i = 0; i < 512; i += j) {
757 double *prior = dnch->
prior;
761 p1 = pow(0.1, 2.5 / sdiv);
762 p2 = pow(0.1, 1.0 / sdiv);
778 prior_band_excit[m] = 0.0;
793 if (i <
lrint(12.0 * sdiv)) {
794 dnch->
band_excit[i] = pow(0.1, 1.45 + 0.1 * i / sdiv);
796 dnch->
band_excit[i] = pow(0.1, 2.5 - 0.2 * (i / sdiv - 14.0));
817 double d7 = fmin(0.008 + 2.2 / d6, 0.03);
842 for (
int j = 1; j < 16; j++) {
859 double d1, d2, d3, d4, d5, d6, d7, d8, d9, d10;
870 for (i = 1; i < len / 4; i++) {
872 d2 = 0.5 * (in[i].
re + in[k].
re);
873 d1 = 0.5 * (in[i].
im - in[k].
im);
874 d4 = 0.5 * (in[i].
im + in[k].
im);
875 d3 = 0.5 * (in[k].
re - in[i].
re);
876 in[i].
re = d2 + d9 * d4 + d6 * d3;
877 in[i].
im = d1 + d9 * d3 - d6 * d4;
878 in[k].
re = d2 - d9 * d4 - d6 * d3;
879 in[k].
im = -d1 + d9 * d3 - d6 * d4;
881 d9 += d9 * d8 - d6 * d7;
882 d6 += d6 * d8 + d10 * d7;
886 in[0].
re = d2 + in[0].
im;
887 in[0].
im = d2 - in[0].
im;
892 double d1, d2, d3, d4, d5, d6, d7, d8, d9, d10;
902 for (i = 1; i < len / 4; i++) {
904 d2 = 0.5 * (in[i].
re + in[k].
re);
905 d1 = 0.5 * (in[i].
im - in[k].
im);
906 d4 = 0.5 * (in[i].
re - in[k].
re);
907 d3 = 0.5 * (in[i].
im + in[k].
im);
908 in[i].
re = d2 - d9 * d3 - d6 * d4;
909 in[i].
im = d1 + d9 * d4 - d6 * d3;
910 in[k].
re = d2 + d9 * d3 + d6 * d4;
911 in[k].
im = -d1 + d9 * d4 - d6 * d3;
913 d9 += d9 * d8 - d6 * d7;
914 d6 += d6 * d8 + d10 * d7;
917 in[0].
re = 0.5 * (d2 + in[0].
im);
918 in[0].
im = 0.5 * (d2 - in[0].
im);
923 for (
int i = 0; i < 15; i++) {
936 double mag2, var = 0.0, avr = 0.0, avi = 0.0;
937 int edge, j, k,
n, edgemax;
963 for (
int i = j; i <= edgemax; i++) {
964 if ((i == j) && (i < edgemax)) {
1001 double *sample_noise)
1014 sample_noise[i] = sample_noise[i - 1];
1020 double *sample_noise,
1023 int new_band_noise[15];
1025 double sum = 0.0, d1;
1026 float new_noise_floor;
1029 for (
int m = 0; m < 15; m++)
1030 temp[m] = sample_noise[m];
1034 for (
int m = 0; m < 5; m++) {
1036 for (n = 0; n < 15; n++)
1042 for (
int m = 0; m < 15; m++) {
1044 for (n = 0; n < 5; n++)
1051 for (
int m = 0; m < 15; m++)
1054 d1 = (
int)(sum / 15.0 - 0.5);
1056 i =
lrint(temp[7] - d1);
1058 for (d1 -= dnch->
band_noise[7] - i; d1 > -20.0; d1 -= 1.0)
1061 for (
int m = 0; m < 15; m++)
1064 new_noise_floor = d1 + 2.5;
1068 for (
int m = 0; m < 15; m++) {
1069 new_band_noise[m] =
lrint(temp[m]);
1070 new_band_noise[m] = av_clip(new_band_noise[m], -24, 24);
1074 memcpy(dnch->
band_noise, new_band_noise,
sizeof(new_band_noise));
1091 const int end = (in->
channels * (jobnr+1)) / nb_jobs;
1148 levels[i] = levels[i - 1];
1151 for (
int i = 0; i < 15; i++) {
1208 double sample_noise[15];
1232 float *orig = (
float *)in->extended_data[
ch];
1246 dst[m] = orig[m] - src[m];
1374 char *res,
int res_len,
int flags)
1379 if (!strcmp(cmd,
"sample_noise") ||
1380 !strcmp(cmd,
"sn")) {
1381 if (!strcmp(args,
"start")) {
1384 }
else if (!strcmp(args,
"end") ||
1385 !strcmp(args,
"stop")) {
1389 }
else if (!strcmp(cmd,
"nr") ||
1390 !strcmp(cmd,
"noise_reduction")) {
1393 if (sscanf(args,
"%f", &nr) == 1) {
1397 }
else if (!strcmp(cmd,
"nf") ||
1398 !strcmp(cmd,
"noise_floor")) {
1401 if (sscanf(args,
"%f", &nf) == 1) {
1405 }
else if (!strcmp(cmd,
"output_mode") ||
1406 !strcmp(cmd,
"om")) {
1407 if (!strcmp(args,
"i")) {
1409 }
else if (!strcmp(args,
"o")) {
1411 }
else if (!strcmp(args,
"n")) {
1444 .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)
A link between two filters.
#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. ...
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...
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)
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)
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[]
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;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);returnNULL;}returnac;}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;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->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);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Describe the class of an AVClass context structure.
double noise_band_avi[15]
static double limit_gain(double a, double b)
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)
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)
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch