]> git.sesse.net Git - ffmpeg/blob - libswresample/dither_template.c
swr: move buffer used to discard sample to context
[ffmpeg] / libswresample / dither_template.c
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
26 void RENAME(swri_noise_shaping)(SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count){
27     int i, j, pos, ch;
28     int taps  = s->dither.ns_taps;
29     float S   = s->dither.ns_scale;
30     float S_1 = s->dither.ns_scale_1;
31
32     av_assert2((taps&3) != 2);
33     av_assert2((taps&3) != 3 || s->dither.ns_coeffs[taps] == 0);
34
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];
49             }
50             if(j < taps)
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;
55             d1 *= S;
56             CLIP(d1);
57             dst[i] = d1;
58         }
59     }
60
61     s->dither.ns_pos = pos;
62 }
63
64 #undef RENAME
65 #undef DELEM
66 #undef CLIP