2 #if defined(TEMPLATE_DITHER_DBL)
3 # define RENAME(N) N ## _double
7 #elif defined(TEMPLATE_DITHER_FLT)
8 # define RENAME(N) N ## _float
12 #elif defined(TEMPLATE_DITHER_S32)
13 # define RENAME(N) N ## _int32
14 # define DELEM int32_t
15 # define CLIP(v) v = FFMAX(FFMIN(v, INT32_MAX), INT32_MIN)
17 #elif defined(TEMPLATE_DITHER_S16)
18 # define RENAME(N) N ## _int16
19 # define DELEM int16_t
20 # define CLIP(v) v = FFMAX(FFMIN(v, INT16_MAX), INT16_MIN)
26 void RENAME(swri_noise_shaping)(SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count){
28 int taps = s->dither.ns_taps;
29 float S = s->dither.ns_scale;
30 float S_1 = s->dither.ns_scale_1;
32 av_assert2((taps&3) != 2);
33 av_assert2((taps&3) != 3 || s->dither.ns_coeffs[taps] == 0);
35 for (ch=0; ch<srcs->ch_count; ch++) {
36 const float *noise = ((const float *)noises->ch[ch]) + s->dither.noise_pos;
37 const DELEM *src = (const DELEM*)srcs->ch[ch];
38 DELEM *dst = (DELEM*)dsts->ch[ch];
39 float *ns_errors = s->dither.ns_errors[ch];
40 const float *ns_coeffs = s->dither.ns_coeffs;
41 pos = s->dither.ns_pos;
42 for (i=0; i<count; i++) {
43 double d1, d = src[i]*S_1;
44 for(j=0; j<taps-2; j+=4) {
45 d -= ns_coeffs[j ] * ns_errors[pos + j ]
46 +ns_coeffs[j + 1] * ns_errors[pos + j + 1]
47 +ns_coeffs[j + 2] * ns_errors[pos + j + 2]
48 +ns_coeffs[j + 3] * ns_errors[pos + j + 3];
51 d -= ns_coeffs[j] * ns_errors[pos + j];
52 pos = pos ? pos - 1 : taps - 1;
53 d1 = rint(d + noise[i]);
54 ns_errors[pos + taps] = ns_errors[pos] = d1 - d;
61 s->dither.ns_pos = pos;