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 "gdither_types_internal.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
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);
121 case GDitherPerformanceTest:
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 const *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 */
204 /* Run FIR and add white noise */
205 ss->buffer[ss->phase] = GDITHER_NOISE * 0.5f;
206 tmp += ss->buffer[ss->phase] * shaped_bs[0]
207 + ss->buffer[(ss->phase - 1) & GDITHER_SH_BUF_MASK]
209 + ss->buffer[(ss->phase - 2) & GDITHER_SH_BUF_MASK]
211 + ss->buffer[(ss->phase - 3) & GDITHER_SH_BUF_MASK]
213 + ss->buffer[(ss->phase - 4) & GDITHER_SH_BUF_MASK]
216 /* Roll buffer and store last error */
217 ss->phase = (ss->phase + 1) & GDITHER_SH_BUF_MASK;
218 ss->buffer[ss->phase] = (float)lrintf(tmp) - ideal;
222 clamped = lrintf(tmp);
223 if (clamped > clamp_u) {
225 } else if (clamped < clamp_l) {
231 o8[i] = (uint8_t) (clamped * post_scale);
234 o16[i] = (int16_t) (clamped * post_scale);
237 o32[i] = (int32_t) (clamped * post_scale);
243 /* floating pint version of the inner loop function */
244 inline static void gdither_innner_loop_fp(const GDitherType dt,
245 const uint32_t stride, const float bias, const float scale,
247 const float post_scale, const int bit_depth,
248 const uint32_t channel, const uint32_t length, float *ts,
250 GDitherShapedState *ss, float const *x, void *y, const int clamp_u,
255 float *oflt = (float*) y;
256 double *odbl = (double*) y;
261 for (pos = 0; pos < length; pos++, i += stride) {
262 tmp = x[i] * scale + bias;
268 tmp -= GDITHER_NOISE;
271 r = GDITHER_NOISE - 0.5f;
272 tmp -= r - ts[channel];
277 /* Save raw value for error calculations */
280 /* Run FIR and add white noise */
281 ss->buffer[ss->phase] = GDITHER_NOISE * 0.5f;
282 tmp += ss->buffer[ss->phase] * shaped_bs[0]
283 + ss->buffer[(ss->phase - 1) & GDITHER_SH_BUF_MASK]
285 + ss->buffer[(ss->phase - 2) & GDITHER_SH_BUF_MASK]
287 + ss->buffer[(ss->phase - 3) & GDITHER_SH_BUF_MASK]
289 + ss->buffer[(ss->phase - 4) & GDITHER_SH_BUF_MASK]
292 /* Roll buffer and store last error */
293 ss->phase = (ss->phase + 1) & GDITHER_SH_BUF_MASK;
294 ss->buffer[ss->phase] = (float)lrintf(tmp) - ideal;
298 clamped = (double)lrintf(tmp);
299 if (clamped > clamp_u) {
301 } else if (clamped < clamp_l) {
307 oflt[i] = (float) (clamped * post_scale);
310 odbl[i] = (double) (clamped * post_scale);
316 #define GDITHER_CONV_BLOCK 512
318 void gdither_run(GDither s, uint32_t channel, uint32_t length,
319 double const *x, void *y)
321 float conv[GDITHER_CONV_BLOCK];
323 char *ycast = (char *)y;
327 switch (s->bit_depth) {
347 while (pos < length) {
348 for (i=0; (i + pos) < length && i < GDITHER_CONV_BLOCK; i++) {
349 conv[i] = x[pos + i];
351 gdither_runf(s, channel, i, conv, ycast + s->channels * step);
356 void gdither_runf(GDither s, uint32_t channel, uint32_t length,
357 float const *x, void *y)
362 GDitherShapedState *ss = NULL;
364 if (!s || channel >= s->channels) {
368 if (s->shaped_state) {
369 ss = s->shaped_state + channel;
372 if (s->type == GDitherNone && s->bit_depth == 23) {
373 int32_t *o32 = (int32_t*) y;
375 for (pos = 0; pos < length; pos++) {
376 i = channel + (pos * s->channels);
377 tmp = x[i] * 8388608.0f;
379 clamped = lrintf(tmp);
380 if (clamped > 8388607) {
382 } else if (clamped < -8388608) {
386 o32[i] = (int32_t) (clamped * 256);
392 /* some common case handling code - looks a bit wierd, but it allows
393 * the compiler to optimise out the branches in the inner loop */
394 if (s->bit_depth == 8 && s->dither_depth == 8) {
397 gdither_innner_loop(GDitherNone, s->channels, 128.0f, SCALE_U8,
398 1, 8, channel, length, NULL, NULL, x, y,
402 gdither_innner_loop(GDitherRect, s->channels, 128.0f, SCALE_U8,
403 1, 8, channel, length, NULL, NULL, x, y,
407 gdither_innner_loop(GDitherTri, s->channels, 128.0f, SCALE_U8,
408 1, 8, channel, length, s->tri_state,
409 NULL, x, y, MAX_U8, MIN_U8);
412 gdither_innner_loop(GDitherShaped, s->channels, 128.0f, SCALE_U8,
413 1, 8, channel, length, NULL,
414 ss, x, y, MAX_U8, MIN_U8);
417 } else if (s->bit_depth == 16 && s->dither_depth == 16) {
420 gdither_innner_loop(GDitherNone, s->channels, 0.0f, SCALE_S16,
421 1, 16, channel, length, NULL, NULL, x, y,
425 gdither_innner_loop(GDitherRect, s->channels, 0.0f, SCALE_S16,
426 1, 16, channel, length, NULL, NULL, x, y,
430 gdither_innner_loop(GDitherTri, s->channels, 0.0f, SCALE_S16,
431 1, 16, channel, length, s->tri_state,
432 NULL, x, y, MAX_S16, MIN_S16);
435 gdither_innner_loop(GDitherShaped, s->channels, 0.0f,
436 SCALE_S16, 1, 16, channel, length, NULL,
437 ss, x, y, MAX_S16, MIN_S16);
440 } else if (s->bit_depth == 32 && s->dither_depth == 24) {
443 gdither_innner_loop(GDitherNone, s->channels, 0.0f, SCALE_S24,
444 256, 32, channel, length, NULL, NULL, x,
445 y, MAX_S24, MIN_S24);
448 gdither_innner_loop(GDitherRect, s->channels, 0.0f, SCALE_S24,
449 256, 32, channel, length, NULL, NULL, x,
450 y, MAX_S24, MIN_S24);
453 gdither_innner_loop(GDitherTri, s->channels, 0.0f, SCALE_S24,
454 256, 32, channel, length, s->tri_state,
455 NULL, x, y, MAX_S24, MIN_S24);
458 gdither_innner_loop(GDitherShaped, s->channels, 0.0f, SCALE_S24,
459 256, 32, channel, length,
460 NULL, ss, x, y, MAX_S24, MIN_S24);
463 } else if (s->bit_depth == GDitherFloat || s->bit_depth == GDitherDouble) {
464 gdither_innner_loop_fp(s->type, s->channels, s->bias, s->scale,
465 s->post_scale_fp, s->bit_depth, channel, length,
466 s->tri_state, ss, x, y, s->clamp_u, s->clamp_l);
468 /* no special case handling, just process it from the struct */
470 gdither_innner_loop(s->type, s->channels, s->bias, s->scale,
471 s->post_scale, s->bit_depth, channel,
472 length, s->tri_state, ss, x, y, s->clamp_u,
477 /* vi:set ts=8 sts=4 sw=4: */