ec6bfaa2ea3428784fb948fccf9d737568079ff5
[ardour.git] / libs / ardour / gdither.cc
1 /*
2  *  Copyright (C) 2002 Steve Harris <steve@plugin.org.uk>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  *  $Id$
19  */
20
21 #include <ardour/gdither_types_internal.h>
22 #include <ardour/gdither.h>
23 #include <ardour/noise.h>
24
25 /* this monstrosity is necessary to get access to lrintf() and random().
26    whoever is writing the glibc headers <cmath> and <cstdlib> should be
27    hauled off to a programmer re-education camp. for the rest of
28    their natural lives. or longer. <paul@linuxaudiosystems.com>
29 */
30
31 #define _ISOC9X_SOURCE  1
32 #define _ISOC99_SOURCE  1
33 #ifdef __cplusplus
34 #include <cmath>
35 #else
36 #include <math.h>
37 #endif
38
39 #undef  __USE_SVID 
40 #define __USE_SVID 1
41 #ifdef __cplusplus
42 #include <cstdlib>
43 #else
44 #include <stdlib.h>
45 #endif
46
47 #include <sys/types.h>
48
49 /* Lipshitz's minimally audible FIR, only really works for 46kHz-ish signals */
50 static const float shaped_bs[] = { 2.033f, -2.165f, 1.959f, -1.590f, 0.6149f };
51
52 /* Some useful constants */
53 #define MAX_U8        255
54 #define MIN_U8          0
55 #define SCALE_U8      128.0f
56
57 #define MAX_S16     32767
58 #define MIN_S16    -32768
59 #define SCALE_S16   32768.0f
60
61 #define MAX_S24   8388607
62 #define MIN_S24  -8388608
63 #define SCALE_S24 8388608.0f
64
65 GDither gdither_new(GDitherType type, uint32_t channels,
66
67                     GDitherSize bit_depth, int dither_depth)
68 {
69     GDither s;
70
71     s = (GDither)calloc(1, sizeof(struct GDither_s));
72     s->type = type;
73     s->channels = channels;
74     s->bit_depth = (int)bit_depth;
75
76     if (dither_depth <= 0 || dither_depth > (int)bit_depth) {
77         dither_depth = (int)bit_depth;
78     }
79     s->dither_depth = dither_depth;
80
81     s->scale = (float)(1LL << (dither_depth - 1));
82     if (bit_depth == GDitherFloat || bit_depth == GDitherDouble) {
83         s->post_scale_fp = 1.0f / s->scale;
84         s->post_scale = 0;
85     } else {
86         s->post_scale_fp = 0.0f;
87         s->post_scale = 1 << ((int)bit_depth - dither_depth);
88     }
89
90     switch (bit_depth) {
91     case GDither8bit:
92         /* Unsigned 8 bit */
93         s->bias = 1.0f;
94         s->clamp_u = 255;
95         s->clamp_l = 0;
96         break;
97     case GDither16bit:
98         /* Signed 16 bit */
99         s->bias = 0.0f;
100         s->clamp_u = 32767;
101         s->clamp_l = -32768;
102         break;
103     case GDither32bit:
104         /* Signed 24 bit, in upper 24 bits of 32 bit word */
105         s->bias = 0.0f;
106         s->clamp_u = 8388607;
107         s->clamp_l = -8388608;
108         break;
109     case GDitherFloat:
110         /* normalised float */
111         s->bias = 0.0f;
112         s->clamp_u = lrintf(s->scale);
113         s->clamp_l = lrintf(-s->scale);
114         break;
115     case GDitherDouble:
116         /* normalised float */
117         s->bias = 0.0f;
118         s->clamp_u = lrintf(s->scale);
119         s->clamp_l = lrintf(-s->scale);
120         break;
121     case 23:
122         /* special performance test case */
123         s->scale = SCALE_S24;
124         s->post_scale = 256;
125         s->bias = 0.0f;
126         s->clamp_u = 8388607;
127         s->clamp_l = -8388608;
128         break;
129     default:
130         /* Not a bit depth we can handle */
131         free(s);
132
133         return NULL;
134         break;
135     }
136
137     switch (type) {
138     case GDitherNone:
139     case GDitherRect:
140         /* No state */
141         break;
142
143     case GDitherTri:
144         /* The last whitenoise sample */
145         s->tri_state = (float *) calloc(channels, sizeof(float));
146         break;
147
148     case GDitherShaped:
149         /* The error from the last few samples encoded */
150         s->shaped_state = (GDitherShapedState*)
151                            calloc(channels, sizeof(GDitherShapedState));
152         break;
153     }
154
155     return s;
156 }
157
158 void gdither_free(GDither s)
159 {
160     if (s) {
161         free(s->tri_state);
162         free(s->shaped_state);
163         free(s);
164     }
165 }
166
167 inline static void gdither_innner_loop(const GDitherType dt, 
168     const uint32_t stride, const float bias, const float scale, 
169
170     const uint32_t post_scale, const int bit_depth, 
171     const uint32_t channel, const uint32_t length, float *ts, 
172
173     GDitherShapedState *ss, float *x, void *y, const int clamp_u, 
174
175     const int clamp_l)
176 {
177     uint32_t pos, i;
178     uint8_t *o8 = (uint8_t*) y;
179     int16_t *o16 = (int16_t*) y;
180     int32_t *o32 = (int32_t*) y;
181     float tmp, r, ideal;
182     int64_t clamped;
183
184     i = channel;
185     for (pos = 0; pos < length; pos++, i += stride) {
186         tmp = x[i] * scale + bias;
187
188         switch (dt) {
189         case GDitherNone:
190             break;
191         case GDitherRect:
192             tmp -= GDITHER_NOISE;
193             break;
194         case GDitherTri:
195             r = GDITHER_NOISE - 0.5f;
196             tmp -= r - ts[channel];
197             ts[channel] = r;
198             break;
199         case GDitherShaped:
200             /* Save raw value for error calculations */
201             ideal = tmp;
202
203             /* Run FIR and add white noise */
204             ss->buffer[ss->phase] = GDITHER_NOISE * 0.5f;
205             tmp += ss->buffer[ss->phase] * shaped_bs[0]
206                    + ss->buffer[(ss->phase - 1) & GDITHER_SH_BUF_MASK]
207                      * shaped_bs[1]
208                    + ss->buffer[(ss->phase - 2) & GDITHER_SH_BUF_MASK]
209                      * shaped_bs[2]
210                    + ss->buffer[(ss->phase - 3) & GDITHER_SH_BUF_MASK]
211                      * shaped_bs[3]
212                    + ss->buffer[(ss->phase - 4) & GDITHER_SH_BUF_MASK]
213                      * shaped_bs[4];
214
215             /* Roll buffer and store last error */
216             ss->phase = (ss->phase + 1) & GDITHER_SH_BUF_MASK;
217             ss->buffer[ss->phase] = (float)lrintf(tmp) - ideal;
218             break;
219         }
220         
221         clamped = lrintf(tmp);
222         if (clamped > clamp_u) {
223                 clamped = clamp_u;
224         } else if (clamped < clamp_l) {
225                 clamped = clamp_l;
226         }
227
228         switch (bit_depth) {
229         case GDither8bit:
230             o8[i] = (u_int8_t) (clamped * post_scale);
231             break;
232         case GDither16bit:
233             o16[i] = (int16_t) (clamped * post_scale);
234             break;
235         case GDither32bit:
236             o32[i] = (int32_t) (clamped * post_scale);
237             break;
238         }
239     }
240 }
241
242 /* floating pint version of the inner loop function */
243 inline static void gdither_innner_loop_fp(const GDitherType dt, 
244     const uint32_t stride, const float bias, const float scale, 
245
246     const float post_scale, const int bit_depth, 
247     const uint32_t channel, const uint32_t length, float *ts, 
248
249     GDitherShapedState *ss, float *x, void *y, const int clamp_u, 
250
251     const int clamp_l)
252 {
253     uint32_t pos, i;
254     float *oflt = (float*) y;
255     double *odbl = (double*) y;
256     float tmp, r, ideal;
257     double clamped;
258
259     i = channel;
260     for (pos = 0; pos < length; pos++, i += stride) {
261         tmp = x[i] * scale + bias;
262
263         switch (dt) {
264         case GDitherNone:
265             break;
266         case GDitherRect:
267             tmp -= GDITHER_NOISE;
268             break;
269         case GDitherTri:
270             r = GDITHER_NOISE - 0.5f;
271             tmp -= r - ts[channel];
272             ts[channel] = r;
273             break;
274         case GDitherShaped:
275             /* Save raw value for error calculations */
276             ideal = tmp;
277
278             /* Run FIR and add white noise */
279             ss->buffer[ss->phase] = GDITHER_NOISE * 0.5f;
280             tmp += ss->buffer[ss->phase] * shaped_bs[0]
281                    + ss->buffer[(ss->phase - 1) & GDITHER_SH_BUF_MASK]
282                      * shaped_bs[1]
283                    + ss->buffer[(ss->phase - 2) & GDITHER_SH_BUF_MASK]
284                      * shaped_bs[2]
285                    + ss->buffer[(ss->phase - 3) & GDITHER_SH_BUF_MASK]
286                      * shaped_bs[3]
287                    + ss->buffer[(ss->phase - 4) & GDITHER_SH_BUF_MASK]
288                      * shaped_bs[4];
289
290             /* Roll buffer and store last error */
291             ss->phase = (ss->phase + 1) & GDITHER_SH_BUF_MASK;
292             ss->buffer[ss->phase] = (float)lrintf(tmp) - ideal;
293             break;
294         }
295         
296         clamped = rintf(tmp);
297         if (clamped > clamp_u) {
298                 clamped = clamp_u;
299         } else if (clamped < clamp_l) {
300                 clamped = clamp_l;
301         }
302
303         switch (bit_depth) {
304         case GDitherFloat:
305             oflt[i] = (float) (clamped * post_scale);
306             break;
307         case GDitherDouble:
308             odbl[i] = (double) (clamped * post_scale);
309             break;
310         }
311     }
312 }
313
314 #define GDITHER_CONV_BLOCK 512
315
316 void gdither_run(GDither s, uint32_t channel, uint32_t length,
317                  double *x, void *y)
318 {
319     float conv[GDITHER_CONV_BLOCK];
320     uint32_t i, pos;
321     char *ycast = (char *)y;
322
323     int step;
324
325     switch (s->bit_depth) {
326     case GDither8bit:
327         step = 1;
328         break;
329     case GDither16bit:
330         step = 2;
331         break;
332     case GDither32bit:
333     case GDitherFloat:
334         step = 4;
335         break;
336     case GDitherDouble:
337         step = 8;
338         break;
339     default:
340         step = 0;
341         break;
342     }
343
344     pos = 0;
345     while (pos < length) {
346         for (i=0; (i + pos) < length && i < GDITHER_CONV_BLOCK; i++) {
347             conv[i] = x[pos + i];
348         }
349         gdither_runf(s, channel, i, conv, ycast + s->channels * step);
350         pos += i;
351     }
352 }
353
354 void gdither_runf(GDither s, uint32_t channel, uint32_t length,
355                  float *x, void *y)
356 {
357     uint32_t pos, i;
358     float tmp;
359     int64_t clamped;
360     GDitherShapedState *ss = NULL;
361
362     if (!s || channel >= s->channels) {
363         return;
364     }
365
366     if (s->shaped_state) {
367         ss = s->shaped_state + channel;
368     }
369
370     if (s->type == GDitherNone && s->bit_depth == 23) {
371         int32_t *o32 = (int32_t*) y;
372
373         for (pos = 0; pos < length; pos++) {
374             i = channel + (pos * s->channels);
375             tmp = x[i] * 8388608.0f;
376
377             clamped = lrintf(tmp);
378             if (clamped > 8388607) {
379                     clamped = 8388607;
380             } else if (clamped < -8388608) {
381                     clamped = -8388608;
382             }
383
384             o32[i] = (int32_t) (clamped * 256);
385         }
386
387         return;
388     }
389
390     /* some common case handling code - looks a bit wierd, but it allows
391      * the compiler to optiomise out the branches in the inner loop */
392     if (s->bit_depth == 8 && s->dither_depth == 8) {
393         switch (s->type) {
394         case GDitherNone:
395             gdither_innner_loop(GDitherNone, s->channels, 128.0f, SCALE_U8,
396                                 1, 8, channel, length, NULL, NULL, x, y,
397                                 MAX_U8, MIN_U8);
398             break;
399         case GDitherRect:
400             gdither_innner_loop(GDitherRect, s->channels, 128.0f, SCALE_U8,
401                                 1, 8, channel, length, NULL, NULL, x, y,
402                                 MAX_U8, MIN_U8);
403             break;
404         case GDitherTri:
405             gdither_innner_loop(GDitherTri, s->channels, 128.0f, SCALE_U8,
406                                 1, 8, channel, length, s->tri_state,
407                                 NULL, x, y, MAX_U8, MIN_U8);
408             break;
409         case GDitherShaped:
410             gdither_innner_loop(GDitherShaped, s->channels, 128.0f, SCALE_U8,
411                                 1, 8, channel, length, NULL,
412                                 ss, x, y, MAX_U8, MIN_U8);
413             break;
414         }
415     } else if (s->bit_depth == s->dither_depth == 16) {
416         switch (s->type) {
417         case GDitherNone:
418             gdither_innner_loop(GDitherNone, s->channels, 0.0f, SCALE_S16,
419                                 1, 16, channel, length, NULL, NULL, x, y,
420                                 MAX_S16, MIN_S16);
421             break;
422         case GDitherRect:
423             gdither_innner_loop(GDitherRect, s->channels, 0.0f, SCALE_S16,
424                                 1, 16, channel, length, NULL, NULL, x, y,
425                                 MAX_S16, MIN_S16);
426             break;
427         case GDitherTri:
428             gdither_innner_loop(GDitherTri, s->channels, 0.0f, SCALE_S16,
429                                 1, 16, channel, length, s->tri_state,
430                                 NULL, x, y, MAX_S16, MIN_S16);
431             break;
432         case GDitherShaped:
433             gdither_innner_loop(GDitherShaped, s->channels, 0.0f,
434                                 SCALE_S16, 1, 16, channel, length, NULL,
435                                 ss, x, y, MAX_S16, MIN_S16);
436             break;
437         }
438     } else if (s->bit_depth == 32 && s->dither_depth == 24) {
439         switch (s->type) {
440         case GDitherNone:
441             gdither_innner_loop(GDitherNone, s->channels, 0.0f, SCALE_S24,
442                                 256, 32, channel, length, NULL, NULL, x,
443                                 y, MAX_S24, MIN_S24);
444             break;
445         case GDitherRect:
446             gdither_innner_loop(GDitherRect, s->channels, 0.0f, SCALE_S24,
447                                 256, 32, channel, length, NULL, NULL, x,
448                                 y, MAX_S24, MIN_S24);
449             break;
450         case GDitherTri:
451             gdither_innner_loop(GDitherTri, s->channels, 0.0f, SCALE_S24,
452                                 256, 32, channel, length, s->tri_state,
453                                 NULL, x, y, MAX_S24, MIN_S24);
454             break;
455         case GDitherShaped:
456             gdither_innner_loop(GDitherShaped, s->channels, 0.0f, SCALE_S24,
457                                 256, 32, channel, length,
458                                 NULL, ss, x, y, MAX_S24, MIN_S24);
459             break;
460         }
461     } else if (s->bit_depth == GDitherFloat || s->bit_depth == GDitherDouble) {
462         gdither_innner_loop_fp(s->type, s->channels, s->bias, s->scale,
463                             s->post_scale_fp, s->bit_depth, channel, length,
464                             s->tri_state, ss, x, y, s->clamp_u, s->clamp_l);
465     } else {
466         /* no special case handling, just process it from the struct */
467
468         gdither_innner_loop(s->type, s->channels, s->bias, s->scale,
469                             s->post_scale, s->bit_depth, channel,
470                             length, s->tri_state, ss, x, y, s->clamp_u,
471                             s->clamp_l);
472     }
473 }
474
475 /* vi:set ts=8 sts=4 sw=4: */