1 
2 #if defined(TEMPLATE_DITHER_DBL)
3 #    define RENAME(N) N ## _double
4 #    define DELEM  double
5 #    define CLIP(v)
6 
7 #elif defined(TEMPLATE_DITHER_FLT)
8 #    define RENAME(N) N ## _float
9 #    define DELEM  float
10 #    define CLIP(v)
11 
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)
16 
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)
21 
22 #else
23 ERROR
24 #endif
25 
RENAME(swri_noise_shaping)26 void RENAME(swri_noise_shaping)(SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count){
27     int pos = s->dither.ns_pos;
28     int i, j, ch;
29     int taps  = s->dither.ns_taps;
30     float S   = s->dither.ns_scale;
31     float S_1 = s->dither.ns_scale_1;
32 
33     av_assert2((taps&3) != 2);
34     av_assert2((taps&3) != 3 || s->dither.ns_coeffs[taps] == 0);
35 
36     for (ch=0; ch<srcs->ch_count; ch++) {
37         const float *noise = ((const float *)noises->ch[ch]) + s->dither.noise_pos;
38         const DELEM *src = (const DELEM*)srcs->ch[ch];
39         DELEM *dst = (DELEM*)dsts->ch[ch];
40         float *ns_errors = s->dither.ns_errors[ch];
41         const float *ns_coeffs = s->dither.ns_coeffs;
42         pos  = s->dither.ns_pos;
43         for (i=0; i<count; i++) {
44             double d1, d = src[i]*S_1;
45             for(j=0; j<taps-2; j+=4) {
46                 d -= ns_coeffs[j    ] * ns_errors[pos + j    ]
47                     +ns_coeffs[j + 1] * ns_errors[pos + j + 1]
48                     +ns_coeffs[j + 2] * ns_errors[pos + j + 2]
49                     +ns_coeffs[j + 3] * ns_errors[pos + j + 3];
50             }
51             if(j < taps)
52                 d -= ns_coeffs[j] * ns_errors[pos + j];
53             pos = pos ? pos - 1 : taps - 1;
54             d1 = rint(d + noise[i]);
55             ns_errors[pos + taps] = ns_errors[pos] = d1 - d;
56             d1 *= S;
57             CLIP(d1);
58             dst[i] = d1;
59         }
60     }
61 
62     s->dither.ns_pos = pos;
63 }
64 
65 #undef RENAME
66 #undef DELEM
67 #undef CLIP
68