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.
20 #include <ardour/gdither_types_internal.h>
21 #include <ardour/gdither.h>
22 #include <ardour/noise.h>
24 /* this monstrosity is necessary to get access to lrintf() and random().
25 whoever is writing the glibc headers <cmath> and <cstdlib> should be
26 hauled off to a programmer re-education camp. for the rest of
27 their natural lives. or longer. <paul@linuxaudiosystems.com>
30 #define _ISOC9X_SOURCE 1
31 #define _ISOC99_SOURCE 1
46 #include <sys/types.h>
48 /* Lipshitz's minimally audible FIR, only really works for 46kHz-ish signals */
49 static const float shaped_bs[] = { 2.033f, -2.165f, 1.959f, -1.590f, 0.6149f };
51 /* Some useful constants */
54 #define SCALE_U8 128.0f
57 #define MIN_S16 -32768
58 #define SCALE_S16 32768.0f
60 #define MAX_S24 8388607
61 #define MIN_S24 -8388608
62 #define SCALE_S24 8388608.0f
64 GDither gdither_new(GDitherType type, uint32_t channels,
66 GDitherSize bit_depth, int dither_depth)
70 s = (GDither)calloc(1, sizeof(struct GDither_s));
72 s->channels = channels;
73 s->bit_depth = (int)bit_depth;
75 if (dither_depth <= 0 || dither_depth > (int)bit_depth) {
76 dither_depth = (int)bit_depth;
78 s->dither_depth = dither_depth;
80 s->scale = (float)(1LL << (dither_depth - 1));
81 if (bit_depth == GDitherFloat || bit_depth == GDitherDouble) {
82 s->post_scale_fp = 1.0f / s->scale;
85 s->post_scale_fp = 0.0f;
86 s->post_scale = 1 << ((int)bit_depth - dither_depth);
103 /* Signed 24 bit, in upper 24 bits of 32 bit word */
105 s->clamp_u = 8388607;
106 s->clamp_l = -8388608;
109 /* normalised float */
111 s->clamp_u = lrintf(s->scale);
112 s->clamp_l = lrintf(-s->scale);
115 /* normalised float */
117 s->clamp_u = lrintf(s->scale);
118 s->clamp_l = lrintf(-s->scale);
121 /* special performance test case */
122 s->scale = SCALE_S24;
125 s->clamp_u = 8388607;
126 s->clamp_l = -8388608;
129 /* Not a bit depth we can handle */
143 /* The last whitenoise sample */
144 s->tri_state = (float *) calloc(channels, sizeof(float));
148 /* The error from the last few samples encoded */
149 s->shaped_state = (GDitherShapedState*)
150 calloc(channels, sizeof(GDitherShapedState));
157 void gdither_free(GDither s)
161 free(s->shaped_state);
166 inline static void gdither_innner_loop(const GDitherType dt,
167 const uint32_t stride, const float bias, const float scale,
169 const uint32_t post_scale, const int bit_depth,
170 const uint32_t channel, const uint32_t length, float *ts,
172 GDitherShapedState *ss, float *x, void *y, const int clamp_u,
177 uint8_t *o8 = (uint8_t*) y;
178 int16_t *o16 = (int16_t*) y;
179 int32_t *o32 = (int32_t*) y;
184 for (pos = 0; pos < length; pos++, i += stride) {
185 tmp = x[i] * scale + bias;
191 tmp -= GDITHER_NOISE;
194 r = GDITHER_NOISE - 0.5f;
195 tmp -= r - ts[channel];
199 /* Save raw value for error calculations */
202 /* Run FIR and add white noise */
203 ss->buffer[ss->phase] = GDITHER_NOISE * 0.5f;
204 tmp += ss->buffer[ss->phase] * shaped_bs[0]
205 + ss->buffer[(ss->phase - 1) & GDITHER_SH_BUF_MASK]
207 + ss->buffer[(ss->phase - 2) & GDITHER_SH_BUF_MASK]
209 + ss->buffer[(ss->phase - 3) & GDITHER_SH_BUF_MASK]
211 + ss->buffer[(ss->phase - 4) & GDITHER_SH_BUF_MASK]
214 /* Roll buffer and store last error */
215 ss->phase = (ss->phase + 1) & GDITHER_SH_BUF_MASK;
216 ss->buffer[ss->phase] = (float)lrintf(tmp) - ideal;
220 clamped = lrintf(tmp);
221 if (clamped > clamp_u) {
223 } else if (clamped < clamp_l) {
229 o8[i] = (u_int8_t) (clamped * post_scale);
232 o16[i] = (int16_t) (clamped * post_scale);
235 o32[i] = (int32_t) (clamped * post_scale);
241 /* floating pint version of the inner loop function */
242 inline static void gdither_innner_loop_fp(const GDitherType dt,
243 const uint32_t stride, const float bias, const float scale,
245 const float post_scale, const int bit_depth,
246 const uint32_t channel, const uint32_t length, float *ts,
248 GDitherShapedState *ss, float *x, void *y, const int clamp_u,
253 float *oflt = (float*) y;
254 double *odbl = (double*) y;
259 for (pos = 0; pos < length; pos++, i += stride) {
260 tmp = x[i] * scale + bias;
266 tmp -= GDITHER_NOISE;
269 r = GDITHER_NOISE - 0.5f;
270 tmp -= r - ts[channel];
274 /* Save raw value for error calculations */
277 /* Run FIR and add white noise */
278 ss->buffer[ss->phase] = GDITHER_NOISE * 0.5f;
279 tmp += ss->buffer[ss->phase] * shaped_bs[0]
280 + ss->buffer[(ss->phase - 1) & GDITHER_SH_BUF_MASK]
282 + ss->buffer[(ss->phase - 2) & GDITHER_SH_BUF_MASK]
284 + ss->buffer[(ss->phase - 3) & GDITHER_SH_BUF_MASK]
286 + ss->buffer[(ss->phase - 4) & GDITHER_SH_BUF_MASK]
289 /* Roll buffer and store last error */
290 ss->phase = (ss->phase + 1) & GDITHER_SH_BUF_MASK;
291 ss->buffer[ss->phase] = (float)lrintf(tmp) - ideal;
295 clamped = rintf(tmp);
296 if (clamped > clamp_u) {
298 } else if (clamped < clamp_l) {
304 oflt[i] = (float) (clamped * post_scale);
307 odbl[i] = (double) (clamped * post_scale);
313 #define GDITHER_CONV_BLOCK 512
315 void gdither_run(GDither s, uint32_t channel, uint32_t length,
318 float conv[GDITHER_CONV_BLOCK];
320 char *ycast = (char *)y;
324 switch (s->bit_depth) {
344 while (pos < length) {
345 for (i=0; (i + pos) < length && i < GDITHER_CONV_BLOCK; i++) {
346 conv[i] = x[pos + i];
348 gdither_runf(s, channel, i, conv, ycast + s->channels * step);
353 void gdither_runf(GDither s, uint32_t channel, uint32_t length,
359 GDitherShapedState *ss = NULL;
361 if (!s || channel >= s->channels) {
365 if (s->shaped_state) {
366 ss = s->shaped_state + channel;
369 if (s->type == GDitherNone && s->bit_depth == 23) {
370 int32_t *o32 = (int32_t*) y;
372 for (pos = 0; pos < length; pos++) {
373 i = channel + (pos * s->channels);
374 tmp = x[i] * 8388608.0f;
376 clamped = lrintf(tmp);
377 if (clamped > 8388607) {
379 } else if (clamped < -8388608) {
383 o32[i] = (int32_t) (clamped * 256);
389 /* some common case handling code - looks a bit wierd, but it allows
390 * the compiler to optimise out the branches in the inner loop */
391 if (s->bit_depth == 8 && s->dither_depth == 8) {
394 gdither_innner_loop(GDitherNone, s->channels, 128.0f, SCALE_U8,
395 1, 8, channel, length, NULL, NULL, x, y,
399 gdither_innner_loop(GDitherRect, s->channels, 128.0f, SCALE_U8,
400 1, 8, channel, length, NULL, NULL, x, y,
404 gdither_innner_loop(GDitherTri, s->channels, 128.0f, SCALE_U8,
405 1, 8, channel, length, s->tri_state,
406 NULL, x, y, MAX_U8, MIN_U8);
409 gdither_innner_loop(GDitherShaped, s->channels, 128.0f, SCALE_U8,
410 1, 8, channel, length, NULL,
411 ss, x, y, MAX_U8, MIN_U8);
414 } else if (s->bit_depth == 16 && s->dither_depth == 16) {
417 gdither_innner_loop(GDitherNone, s->channels, 0.0f, SCALE_S16,
418 1, 16, channel, length, NULL, NULL, x, y,
422 gdither_innner_loop(GDitherRect, s->channels, 0.0f, SCALE_S16,
423 1, 16, channel, length, NULL, NULL, x, y,
427 gdither_innner_loop(GDitherTri, s->channels, 0.0f, SCALE_S16,
428 1, 16, channel, length, s->tri_state,
429 NULL, x, y, MAX_S16, MIN_S16);
432 gdither_innner_loop(GDitherShaped, s->channels, 0.0f,
433 SCALE_S16, 1, 16, channel, length, NULL,
434 ss, x, y, MAX_S16, MIN_S16);
437 } else if (s->bit_depth == 32 && s->dither_depth == 24) {
440 gdither_innner_loop(GDitherNone, s->channels, 0.0f, SCALE_S24,
441 256, 32, channel, length, NULL, NULL, x,
442 y, MAX_S24, MIN_S24);
445 gdither_innner_loop(GDitherRect, s->channels, 0.0f, SCALE_S24,
446 256, 32, channel, length, NULL, NULL, x,
447 y, MAX_S24, MIN_S24);
450 gdither_innner_loop(GDitherTri, s->channels, 0.0f, SCALE_S24,
451 256, 32, channel, length, s->tri_state,
452 NULL, x, y, MAX_S24, MIN_S24);
455 gdither_innner_loop(GDitherShaped, s->channels, 0.0f, SCALE_S24,
456 256, 32, channel, length,
457 NULL, ss, x, y, MAX_S24, MIN_S24);
460 } else if (s->bit_depth == GDitherFloat || s->bit_depth == GDitherDouble) {
461 gdither_innner_loop_fp(s->type, s->channels, s->bias, s->scale,
462 s->post_scale_fp, s->bit_depth, channel, length,
463 s->tri_state, ss, x, y, s->clamp_u, s->clamp_l);
465 /* no special case handling, just process it from the struct */
467 gdither_innner_loop(s->type, s->channels, s->bias, s->scale,
468 s->post_scale, s->bit_depth, channel,
469 length, s->tri_state, ss, x, y, s->clamp_u,
474 /* vi:set ts=8 sts=4 sw=4: */