2 * Copyright (C) 2002 Steve Harris <steve@plugin.org.uk>
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.
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.
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.
21 #include <ardour/gdither_types_internal.h>
22 #include <ardour/gdither.h>
23 #include <ardour/noise.h>
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>
31 #define _ISOC9X_SOURCE 1
32 #define _ISOC99_SOURCE 1
47 #include <sys/types.h>
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 };
52 /* Some useful constants */
55 #define SCALE_U8 128.0f
58 #define MIN_S16 -32768
59 #define SCALE_S16 32768.0f
61 #define MAX_S24 8388607
62 #define MIN_S24 -8388608
63 #define SCALE_S24 8388608.0f
65 GDither gdither_new(GDitherType type, uint32_t channels,
67 GDitherSize bit_depth, int dither_depth)
71 s = (GDither)calloc(1, sizeof(struct GDither_s));
73 s->channels = channels;
74 s->bit_depth = (int)bit_depth;
76 if (dither_depth <= 0 || dither_depth > (int)bit_depth) {
77 dither_depth = (int)bit_depth;
79 s->dither_depth = dither_depth;
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;
86 s->post_scale_fp = 0.0f;
87 s->post_scale = 1 << ((int)bit_depth - dither_depth);
104 /* Signed 24 bit, in upper 24 bits of 32 bit word */
106 s->clamp_u = 8388607;
107 s->clamp_l = -8388608;
110 /* normalised float */
112 s->clamp_u = lrintf(s->scale);
113 s->clamp_l = lrintf(-s->scale);
116 /* normalised float */
118 s->clamp_u = lrintf(s->scale);
119 s->clamp_l = lrintf(-s->scale);
122 /* special performance test case */
123 s->scale = SCALE_S24;
126 s->clamp_u = 8388607;
127 s->clamp_l = -8388608;
130 /* Not a bit depth we can handle */
144 /* The last whitenoise sample */
145 s->tri_state = (float *) calloc(channels, sizeof(float));
149 /* The error from the last few samples encoded */
150 s->shaped_state = (GDitherShapedState*)
151 calloc(channels, sizeof(GDitherShapedState));
158 void gdither_free(GDither s)
162 free(s->shaped_state);
167 inline static void gdither_innner_loop(const GDitherType dt,
168 const uint32_t stride, const float bias, const float scale,
170 const uint32_t post_scale, const int bit_depth,
171 const uint32_t channel, const uint32_t length, float *ts,
173 GDitherShapedState *ss, float *x, void *y, const int clamp_u,
178 uint8_t *o8 = (uint8_t*) y;
179 int16_t *o16 = (int16_t*) y;
180 int32_t *o32 = (int32_t*) y;
185 for (pos = 0; pos < length; pos++, i += stride) {
186 tmp = x[i] * scale + bias;
192 tmp -= GDITHER_NOISE;
195 r = GDITHER_NOISE - 0.5f;
196 tmp -= r - ts[channel];
200 /* Save raw value for error calculations */
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]
208 + ss->buffer[(ss->phase - 2) & GDITHER_SH_BUF_MASK]
210 + ss->buffer[(ss->phase - 3) & GDITHER_SH_BUF_MASK]
212 + ss->buffer[(ss->phase - 4) & GDITHER_SH_BUF_MASK]
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;
221 clamped = lrintf(tmp);
222 if (clamped > clamp_u) {
224 } else if (clamped < clamp_l) {
230 o8[i] = (u_int8_t) (clamped * post_scale);
233 o16[i] = (int16_t) (clamped * post_scale);
236 o32[i] = (int32_t) (clamped * post_scale);
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,
246 const float post_scale, const int bit_depth,
247 const uint32_t channel, const uint32_t length, float *ts,
249 GDitherShapedState *ss, float *x, void *y, const int clamp_u,
254 float *oflt = (float*) y;
255 double *odbl = (double*) y;
260 for (pos = 0; pos < length; pos++, i += stride) {
261 tmp = x[i] * scale + bias;
267 tmp -= GDITHER_NOISE;
270 r = GDITHER_NOISE - 0.5f;
271 tmp -= r - ts[channel];
275 /* Save raw value for error calculations */
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]
283 + ss->buffer[(ss->phase - 2) & GDITHER_SH_BUF_MASK]
285 + ss->buffer[(ss->phase - 3) & GDITHER_SH_BUF_MASK]
287 + ss->buffer[(ss->phase - 4) & GDITHER_SH_BUF_MASK]
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;
296 clamped = rintf(tmp);
297 if (clamped > clamp_u) {
299 } else if (clamped < clamp_l) {
305 oflt[i] = (float) (clamped * post_scale);
308 odbl[i] = (double) (clamped * post_scale);
314 #define GDITHER_CONV_BLOCK 512
316 void gdither_run(GDither s, uint32_t channel, uint32_t length,
319 float conv[GDITHER_CONV_BLOCK];
321 char *ycast = (char *)y;
325 switch (s->bit_depth) {
345 while (pos < length) {
346 for (i=0; (i + pos) < length && i < GDITHER_CONV_BLOCK; i++) {
347 conv[i] = x[pos + i];
349 gdither_runf(s, channel, i, conv, ycast + s->channels * step);
354 void gdither_runf(GDither s, uint32_t channel, uint32_t length,
360 GDitherShapedState *ss = NULL;
362 if (!s || channel >= s->channels) {
366 if (s->shaped_state) {
367 ss = s->shaped_state + channel;
370 if (s->type == GDitherNone && s->bit_depth == 23) {
371 int32_t *o32 = (int32_t*) y;
373 for (pos = 0; pos < length; pos++) {
374 i = channel + (pos * s->channels);
375 tmp = x[i] * 8388608.0f;
377 clamped = lrintf(tmp);
378 if (clamped > 8388607) {
380 } else if (clamped < -8388608) {
384 o32[i] = (int32_t) (clamped * 256);
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) {
395 gdither_innner_loop(GDitherNone, s->channels, 128.0f, SCALE_U8,
396 1, 8, channel, length, NULL, NULL, x, y,
400 gdither_innner_loop(GDitherRect, s->channels, 128.0f, SCALE_U8,
401 1, 8, channel, length, NULL, NULL, x, y,
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);
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);
415 } else if (s->bit_depth == s->dither_depth == 16) {
418 gdither_innner_loop(GDitherNone, s->channels, 0.0f, SCALE_S16,
419 1, 16, channel, length, NULL, NULL, x, y,
423 gdither_innner_loop(GDitherRect, s->channels, 0.0f, SCALE_S16,
424 1, 16, channel, length, NULL, NULL, x, y,
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);
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);
438 } else if (s->bit_depth == 32 && s->dither_depth == 24) {
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);
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);
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);
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);
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);
466 /* no special case handling, just process it from the struct */
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,
475 /* vi:set ts=8 sts=4 sw=4: */