Audacity  2.2.2
Dither.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  Dither.cpp
6 
7  Steve Harris
8  Markus Meyer
9 
10 *******************************************************************//*******************************************************************/
36 
37 
38 // Erik de Castro Lopo's header file that
39 // makes sure that we have lrint and lrintf
40 // (Note: this file should be included first)
41 #include "float_cast.h"
42 
43 #include <stdlib.h>
44 #include <math.h>
45 #include <string.h>
46 //#include <sys/types.h>
47 //#include <memory.h>
48 //#include <assert.h>
49 
50 #include <wx/defs.h>
51 
52 #include "Dither.h"
53 
55 
56 // Constants for the noise shaping buffer
57 const int Dither::BUF_MASK = 7;
58 const int Dither::BUF_SIZE = 8;
59 
60 // Lipshitz's minimally audible FIR
61 const float Dither::SHAPED_BS[] = { 2.033f, -2.165f, 1.959f, -1.590f, 0.6149f };
62 
63 // This is supposed to produce white noise and no dc
64 #define DITHER_NOISE (rand() / (float)RAND_MAX - 0.5f)
65 
66 // The following is a rather ugly, but fast implementation
67 // of a dither loop. The macro "DITHER" is expanded to an implementation
68 // of a dithering algorithm, which contains no branches in the inner loop
69 // except the branches for clipping the sample, and therefore should
70 // be quite fast.
71 
72 #if 0
73 // To assist in understanding what the macros are doing, here's an example of what
74 // the result would be for Shaped dither:
75 //
76 // DITHER(ShapedDither, dest, destFormat, destStride, source, sourceFormat, sourceStride, len);
77 
78  do {
79  if (sourceFormat == int24Sample && destFormat == int16Sample)
80  do {
81  char *d, *s;
82  unsigned int i;
83  int x;
84  for (d = (char*)dest, s = (char*)source, i = 0; i < len; i++, d += (int16Sample >> 16) * destStride, s += (int24Sample >> 16) * sourceStride)
85  do {
86  x = lrintf((ShapedDither((((*(( int*)(s)) / float(1<<23))) * float(1<<15)))));
87  if (x>(32767))
88  *((short*)((((d)))))=(32767);
89  else if (x<(-32768))
90  *((short*)((((d)))))=(-32768);
91  else *((short*)((((d)))))=(short)x;
92  } while (0);
93  } while (0);
94  else if (sourceFormat == floatSample && destFormat == int16Sample)
95  do {
96  char *d, *s;
97  unsigned int i;
98  int x;
99  for (d = (char*)dest, s = (char*)source, i = 0; i < len; i++, d += (int16Sample >> 16) * destStride, s += (floatSample >> 16) * sourceStride)
100  do {
101  x = lrintf((ShapedDither((((*((float*)(s)) > 1.0 ? 1.0 : *((float*)(s)) < -1.0 ? -1.0 : *((float*)(s)))) * float(1<<15)))));
102  if (x>(32767))
103  *((short*)((((d)))))=(32767);
104  else if (x<(-32768))
105  *((short*)((((d)))))=(-32768);
106  else *((short*)((((d)))))=(short)x;
107  } while (0);
108  } while (0);
109  else if (sourceFormat == floatSample && destFormat == int24Sample)
110  do {
111  char *d, *s;
112  unsigned int i;
113  int x;
114  for (d = (char*)dest, s = (char*)source, i = 0; i < len; i++, d += (int24Sample >> 16) * destStride, s += (floatSample >> 16) * sourceStride)
115  do {
116  x = lrintf((ShapedDither((((*((float*)(s)) > 1.0 ? 1.0 : *((float*)(s)) < -1.0 ? -1.0 : *((float*)(s)))) * float(1<<23)))));
117  if (x>(8388607))
118  *((int*)((((d)))))=(8388607);
119  else if (x<(-8388608))
120  *((int*)((((d)))))=(-8388608);
121  else *((int*)((((d)))))=(int)x;
122  } while (0);
123  } while (0);
124  else {
125  if ( false )
126  ;
127  else wxOnAssert(L"c:\\users\\yam\\documents\\audacity\\mixer\\n\\audacity\\src\\dither.cpp", 348, __FUNCTION__ , L"false", 0);
128  }
129  } while (0);
130 #endif
131 
132 // Defines for sample conversion
133 #define CONVERT_DIV16 float(1<<15)
134 #define CONVERT_DIV24 float(1<<23)
135 
136 // Dereference sample pointer and convert to float sample
137 #define FROM_INT16(ptr) (*((short*)(ptr)) / CONVERT_DIV16)
138 #define FROM_INT24(ptr) (*(( int*)(ptr)) / CONVERT_DIV24)
139 
140 // For float, we internally allow values greater than 1.0, which
141 // would blow up the dithering to int values. FROM_FLOAT is
142 // only used to dither to int, so clip here.
143 #define FROM_FLOAT(ptr) (*((float*)(ptr)) > 1.0 ? 1.0 : \
144  *((float*)(ptr)) < -1.0 ? -1.0 : \
145  *((float*)(ptr)))
146 
147 // Promote sample to range of specified type, keep it float, though
148 #define PROMOTE_TO_INT16(sample) ((sample) * CONVERT_DIV16)
149 #define PROMOTE_TO_INT24(sample) ((sample) * CONVERT_DIV24)
150 
151 // Store float sample 'sample' into pointer 'ptr', clip it, if necessary
152 // Note: This assumes, a variable 'x' of type int is valid which is
153 // used by this macro.
154 #define IMPLEMENT_STORE(ptr, sample, ptr_type, min_bound, max_bound) \
155  do { \
156  x = lrintf(sample); \
157  if (x>(max_bound)) *((ptr_type*)(ptr))=(max_bound); \
158  else if (x<(min_bound)) *((ptr_type*)(ptr))=(min_bound); \
159  else *((ptr_type*)(ptr))=(ptr_type)x; } while (0)
160 
161 #define STORE_INT16(ptr, sample) IMPLEMENT_STORE((ptr), (sample), short, -32768, 32767)
162 #define STORE_INT24(ptr, sample) IMPLEMENT_STORE((ptr), (sample), int, -8388608, 8388607)
163 
164 // Dither single float 'sample' and store it in pointer 'dst', using 'dither' as algorithm
165 #define DITHER_TO_INT16(dither, dst, sample) STORE_INT16((dst), dither(PROMOTE_TO_INT16(sample)))
166 #define DITHER_TO_INT24(dither, dst, sample) STORE_INT24((dst), dither(PROMOTE_TO_INT24(sample)))
167 
168 // Implement one single dither step
169 #define DITHER_STEP(dither, store, load, dst, src) \
170  store(dither, (dst), load(src))
171 
172 // Implement a dithering loop
173 // Note: The variable 'x' is needed for the STORE_... macros
174 #define DITHER_LOOP(dither, store, load, dst, dstFormat, dstStride, src, srcFormat, srcStride, len) \
175  do { \
176  char *d, *s; \
177  unsigned int i; \
178  int x; \
179  for (d = (char*)dst, s = (char*)src, i = 0; \
180  i < len; \
181  i++, d += SAMPLE_SIZE(dstFormat) * dstStride, \
182  s += SAMPLE_SIZE(srcFormat) * srcStride) \
183  DITHER_STEP(dither, store, load, d, s); \
184  } while (0)
185 
186 // Shortcuts to dithering loops
187 #define DITHER_INT24_TO_INT16(dither, dst, dstStride, src, srcStride, len) \
188  DITHER_LOOP(dither, DITHER_TO_INT16, FROM_INT24, dst, int16Sample, dstStride, src, int24Sample, srcStride, len)
189 #define DITHER_FLOAT_TO_INT16(dither, dst, dstStride, src, srcStride, len) \
190  DITHER_LOOP(dither, DITHER_TO_INT16, FROM_FLOAT, dst, int16Sample, dstStride, src, floatSample, srcStride, len)
191 #define DITHER_FLOAT_TO_INT24(dither, dst, dstStride, src, srcStride, len) \
192  DITHER_LOOP(dither, DITHER_TO_INT24, FROM_FLOAT, dst, int24Sample, dstStride, src, floatSample, srcStride, len)
193 
194 // Implement a dither. There are only 3 cases where we must dither,
195 // in all other cases, no dithering is necessary.
196 #define DITHER(dither, dst, dstFormat, dstStride, src, srcFormat, srcStride, len) \
197  do { if (srcFormat == int24Sample && dstFormat == int16Sample) \
198  DITHER_INT24_TO_INT16(dither, dst, dstStride, src, srcStride, len); \
199  else if (srcFormat == floatSample && dstFormat == int16Sample) \
200  DITHER_FLOAT_TO_INT16(dither, dst, dstStride, src, srcStride, len); \
201  else if (srcFormat == floatSample && dstFormat == int24Sample) \
202  DITHER_FLOAT_TO_INT24(dither, dst, dstStride, src, srcStride, len); \
203  else { wxASSERT(false); } \
204  } while (0)
205 
206 
208 {
209  // On startup, initialize dither by resetting values
210  Reset();
211 }
212 
214 {
215  mTriangleState = 0;
216  mPhase = 0;
217  memset(mBuffer, 0, sizeof(float) * BUF_SIZE);
218 }
219 
220 // This only decides if we must dither at all, the dithers
221 // are all implemented using macros.
222 //
223 // "source" and "dest" can contain either interleaved or non-interleaved
224 // samples. They do not have to be the same...one can be interleaved while
225 // the otner non-interleaved.
226 //
227 // The "len" argument specifies the number of samples to process.
228 //
229 // If either stride value equals 1 then the corresponding buffer contains
230 // non-interleaved samples.
231 //
232 // If either stride value is greater than 1 then the corresponding buffer
233 // contains interleaved samples and they will be processed by skipping every
234 // stride number of samples.
235 
236 void Dither::Apply(enum DitherType ditherType,
237  const samplePtr source, sampleFormat sourceFormat,
238  samplePtr dest, sampleFormat destFormat,
239  unsigned int len,
240  unsigned int sourceStride /* = 1 */,
241  unsigned int destStride /* = 1 */)
242 {
243  unsigned int i;
244 
245  // This code is not designed for 16-bit or 64-bit machine
246  wxASSERT(sizeof(int) == 4);
247  wxASSERT(sizeof(short) == 2);
248 
249  // Check parameters
250  wxASSERT(source);
251  wxASSERT(dest);
252  wxASSERT(len >= 0);
253  wxASSERT(sourceStride > 0);
254  wxASSERT(destStride > 0);
255 
256  if (len == 0)
257  return; // nothing to do
258 
259  if (destFormat == sourceFormat)
260  {
261  // No need to dither, because source and destination
262  // format are the same. Just copy samples.
263  if (destStride == 1 && sourceStride == 1)
264  memcpy(dest, source, len * SAMPLE_SIZE(destFormat));
265  else
266  {
267  if (sourceFormat == floatSample)
268  {
269  float* d = (float*)dest;
270  float* s = (float*)source;
271 
272  for (i = 0; i < len; i++, d += destStride, s += sourceStride)
273  *d = *s;
274  } else
275  if (sourceFormat == int24Sample)
276  {
277  int* d = (int*)dest;
278  int* s = (int*)source;
279 
280  for (i = 0; i < len; i++, d += destStride, s += sourceStride)
281  *d = *s;
282  } else
283  if (sourceFormat == int16Sample)
284  {
285  short* d = (short*)dest;
286  short* s = (short*)source;
287 
288  for (i = 0; i < len; i++, d += destStride, s += sourceStride)
289  *d = *s;
290  } else {
291  wxASSERT(false); // source format unknown
292  }
293  }
294  } else
295  if (destFormat == floatSample)
296  {
297  // No need to dither, just convert samples to float.
298  // No clipping should be necessary.
299  float* d = (float*)dest;
300 
301  if (sourceFormat == int16Sample)
302  {
303  short* s = (short*)source;
304  for (i = 0; i < len; i++, d += destStride, s += sourceStride)
305  *d = FROM_INT16(s);
306  } else
307  if (sourceFormat == int24Sample)
308  {
309  int* s = (int*)source;
310  for (i = 0; i < len; i++, d += destStride, s += sourceStride)
311  *d = FROM_INT24(s);
312  } else {
313  wxASSERT(false); // source format unknown
314  }
315  } else
316  if (destFormat == int24Sample && sourceFormat == int16Sample)
317  {
318  // Special case when promoting 16 bit to 24 bit
319  int* d = (int*)dest;
320  short* s = (short*)source;
321  for (i = 0; i < len; i++, d += destStride, s += sourceStride)
322  *d = ((int)*s) << 8;
323  } else
324  {
325  // We must do dithering
326  switch (ditherType)
327  {
328  case DitherType::none:
329  DITHER(NoDither, dest, destFormat, destStride, source, sourceFormat, sourceStride, len);
330  break;
332  DITHER(RectangleDither, dest, destFormat, destStride, source, sourceFormat, sourceStride, len);
333  break;
335  Reset(); // reset dither filter for this NEW conversion
336  DITHER(TriangleDither, dest, destFormat, destStride, source, sourceFormat, sourceStride, len);
337  break;
338  case DitherType::shaped:
339  Reset(); // reset dither filter for this NEW conversion
340  DITHER(ShapedDither, dest, destFormat, destStride, source, sourceFormat, sourceStride, len);
341  break;
342  default:
343  wxASSERT(false); // unknown dither algorithm
344  }
345  }
346 }
347 
348 // Dither implementations
349 
350 // No dither, just return sample
351 inline float Dither::NoDither(float sample)
352 {
353  return sample;
354 }
355 
356 // Rectangle dithering, apply one-step noise
357 inline float Dither::RectangleDither(float sample)
358 {
359  return sample - DITHER_NOISE;
360 }
361 
362 // Triangle dither - high pass filtered
363 inline float Dither::TriangleDither(float sample)
364 {
365  float r = DITHER_NOISE;
366  float result = sample + r - mTriangleState;
367  mTriangleState = r;
368 
369  return result;
370 }
371 
372 // Shaped dither
373 inline float Dither::ShapedDither(float sample)
374 {
375  // Generate triangular dither, +-1 LSB, flat psd
376  float r = DITHER_NOISE + DITHER_NOISE;
377  if(sample != sample) // test for NaN
378  sample = 0; // and do the best we can with it
379 
380  // Run FIR
381  float xe = sample + mBuffer[mPhase] * SHAPED_BS[0]
382  + mBuffer[(mPhase - 1) & BUF_MASK] * SHAPED_BS[1]
383  + mBuffer[(mPhase - 2) & BUF_MASK] * SHAPED_BS[2]
384  + mBuffer[(mPhase - 3) & BUF_MASK] * SHAPED_BS[3]
385  + mBuffer[(mPhase - 4) & BUF_MASK] * SHAPED_BS[4];
386 
387  // Accumulate FIR and triangular noise
388  float result = xe + r;
389 
390  // Roll buffer and store last error
391  mPhase = (mPhase + 1) & BUF_MASK;
392  mBuffer[mPhase] = xe - lrintf(result);
393 
394  return result;
395 }
#define DITHER_NOISE
Definition: Dither.cpp:64
Definition: Dither.h:18
void Apply(DitherType ditherType, const samplePtr source, sampleFormat sourceFormat, samplePtr dest, sampleFormat destFormat, unsigned int len, unsigned int sourceStride=1, unsigned int destStride=1)
Definition: Dither.cpp:236
static const int BUF_SIZE
Definition: Dither.h:48
#define SAMPLE_SIZE(SampleFormat)
Definition: Types.h:198
#define FROM_INT24(ptr)
Definition: Dither.cpp:138
float TriangleDither(float sample)
Definition: Dither.cpp:363
float NoDither(float sample)
Definition: Dither.cpp:351
#define DITHER(dither, dst, dstFormat, dstStride, src, srcFormat, srcStride, len)
Definition: Dither.cpp:196
static const int BUF_MASK
Definition: Dither.h:49
float mBuffer[8]
Definition: Dither.h:55
Definition: Dither.h:18
float RectangleDither(float sample)
Definition: Dither.cpp:357
int mPhase
Definition: Dither.h:53
#define FROM_INT16(ptr)
Definition: Dither.cpp:137
sampleFormat
Definition: Types.h:188
char * samplePtr
Definition: Types.h:203
float ShapedDither(float sample)
Definition: Dither.cpp:373
float mTriangleState
Definition: Dither.h:54
Dither()
Default constructor.
Definition: Dither.cpp:207
DitherType
These ditherers are currently available:
Definition: Dither.h:17
#define lrintf(flt)
Definition: float_cast.h:137
void Reset()
Reset state of the dither.
Definition: Dither.cpp:213
static const float SHAPED_BS[]
Definition: Dither.h:50