mirror of
https://github.com/claunia/flac.git
synced 2025-12-16 18:54:26 +00:00
move dithering into plugin_common library
This commit is contained in:
@@ -28,6 +28,7 @@
|
|||||||
|
|
||||||
#include "in2.h"
|
#include "in2.h"
|
||||||
#include "FLAC/all.h"
|
#include "FLAC/all.h"
|
||||||
|
#include "plugin_common/all.h"
|
||||||
|
|
||||||
#ifdef max
|
#ifdef max
|
||||||
#undef max
|
#undef max
|
||||||
@@ -93,134 +94,6 @@ HANDLE thread_handle = INVALID_HANDLE_VALUE; /* the handle to the decode thread
|
|||||||
DWORD WINAPI __stdcall DecodeThread(void *b); /* the decode thread procedure */
|
DWORD WINAPI __stdcall DecodeThread(void *b); /* the decode thread procedure */
|
||||||
|
|
||||||
|
|
||||||
/* 32-bit pseudo-random number generator */
|
|
||||||
static __inline FLAC__uint32 prng(FLAC__uint32 state)
|
|
||||||
{
|
|
||||||
return (state * 0x0019660dL + 0x3c6ef35fL) & 0xffffffffL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* dither routine derived from MAD winamp plugin */
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
FLAC__int32 error[3];
|
|
||||||
FLAC__int32 random;
|
|
||||||
} dither_state;
|
|
||||||
|
|
||||||
static __inline FLAC__int32 linear_dither(unsigned source_bps, unsigned target_bps, FLAC__int32 sample, dither_state *dither, const FLAC__int32 MIN, const FLAC__int32 MAX)
|
|
||||||
{
|
|
||||||
unsigned scalebits;
|
|
||||||
FLAC__int32 output, mask, random;
|
|
||||||
|
|
||||||
FLAC__ASSERT(source_bps < 32);
|
|
||||||
FLAC__ASSERT(target_bps <= 24);
|
|
||||||
FLAC__ASSERT(target_bps <= source_bps);
|
|
||||||
|
|
||||||
/* noise shape */
|
|
||||||
sample += dither->error[0] - dither->error[1] + dither->error[2];
|
|
||||||
|
|
||||||
dither->error[2] = dither->error[1];
|
|
||||||
dither->error[1] = dither->error[0] / 2;
|
|
||||||
|
|
||||||
/* bias */
|
|
||||||
output = sample + (1L << (source_bps - target_bps - 1));
|
|
||||||
|
|
||||||
scalebits = source_bps - target_bps;
|
|
||||||
mask = (1L << scalebits) - 1;
|
|
||||||
|
|
||||||
/* dither */
|
|
||||||
random = (FLAC__int32)prng(dither->random);
|
|
||||||
output += (random & mask) - (dither->random & mask);
|
|
||||||
|
|
||||||
dither->random = random;
|
|
||||||
|
|
||||||
/* clip */
|
|
||||||
if(output > MAX) {
|
|
||||||
output = MAX;
|
|
||||||
|
|
||||||
if(sample > MAX)
|
|
||||||
sample = MAX;
|
|
||||||
}
|
|
||||||
else if(output < MIN) {
|
|
||||||
output = MIN;
|
|
||||||
|
|
||||||
if(sample < MIN)
|
|
||||||
sample = MIN;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* quantize */
|
|
||||||
output &= ~mask;
|
|
||||||
|
|
||||||
/* error feedback */
|
|
||||||
dither->error[0] = sample - output;
|
|
||||||
|
|
||||||
/* scale */
|
|
||||||
return output >> scalebits;
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned pack_pcm(FLAC__byte *data, FLAC__int32 *input, unsigned wide_samples, unsigned channels, unsigned source_bps, unsigned target_bps)
|
|
||||||
{
|
|
||||||
static dither_state dither[MAX_SUPPORTED_CHANNELS];
|
|
||||||
FLAC__byte * const start = data;
|
|
||||||
FLAC__int32 sample;
|
|
||||||
unsigned samples = wide_samples * channels;
|
|
||||||
const unsigned bytes_per_sample = target_bps / 8;
|
|
||||||
|
|
||||||
FLAC__ASSERT(MAX_SUPPORTED_CHANNELS == 2);
|
|
||||||
FLAC__ASSERT(channels > 0 && channels <= MAX_SUPPORTED_CHANNELS);
|
|
||||||
FLAC__ASSERT(source_bps < 32);
|
|
||||||
FLAC__ASSERT(target_bps <= 24);
|
|
||||||
FLAC__ASSERT(target_bps <= source_bps);
|
|
||||||
FLAC__ASSERT(source_bps & 7 == 0);
|
|
||||||
FLAC__ASSERT(target_bps & 7 == 0);
|
|
||||||
|
|
||||||
if(source_bps != target_bps) {
|
|
||||||
const FLAC__int32 MIN = -(1L << source_bps);
|
|
||||||
const FLAC__int32 MAX = ~MIN; /*(1L << (source_bps-1)) - 1 */
|
|
||||||
const unsigned dither_twiggle = channels - 1;
|
|
||||||
unsigned dither_source = 0;
|
|
||||||
|
|
||||||
while(samples--) {
|
|
||||||
sample = linear_dither(source_bps, target_bps, *input++, &dither[dither_source], MIN, MAX);
|
|
||||||
dither_source ^= dither_twiggle;
|
|
||||||
|
|
||||||
switch(target_bps) {
|
|
||||||
case 8:
|
|
||||||
data[0] = sample ^ 0x80;
|
|
||||||
break;
|
|
||||||
case 24:
|
|
||||||
data[2] = (FLAC__byte)(sample >> 16);
|
|
||||||
/* fall through */
|
|
||||||
case 16:
|
|
||||||
data[1] = (FLAC__byte)(sample >> 8);
|
|
||||||
data[0] = (FLAC__byte)sample;
|
|
||||||
}
|
|
||||||
|
|
||||||
data += bytes_per_sample;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
while(samples--) {
|
|
||||||
sample = *input++;
|
|
||||||
|
|
||||||
switch(target_bps) {
|
|
||||||
case 8:
|
|
||||||
data[0] = sample ^ 0x80;
|
|
||||||
break;
|
|
||||||
case 24:
|
|
||||||
data[2] = (FLAC__byte)(sample >> 16);
|
|
||||||
/* fall through */
|
|
||||||
case 16:
|
|
||||||
data[1] = (FLAC__byte)(sample >> 8);
|
|
||||||
data[0] = (FLAC__byte)sample;
|
|
||||||
}
|
|
||||||
|
|
||||||
data += bytes_per_sample;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return data - start;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
@@@@ incorporate this
|
@@@@ incorporate this
|
||||||
static void do_vis(char *data, int nch, int resolution, int position)
|
static void do_vis(char *data, int nch, int resolution, int position)
|
||||||
@@ -480,7 +353,7 @@ DWORD WINAPI __stdcall DecodeThread(void *b)
|
|||||||
#endif
|
#endif
|
||||||
const unsigned n = min(wide_samples_in_reservoir_, 576);
|
const unsigned n = min(wide_samples_in_reservoir_, 576);
|
||||||
const unsigned delta = n * channels;
|
const unsigned delta = n * channels;
|
||||||
int bytes = (int)pack_pcm(sample_buffer_, reservoir_, n, channels, bits_per_sample, target_bps);
|
int bytes = (int)FLAC__plugin_common__pack_pcm(sample_buffer_, reservoir_, n, channels, bits_per_sample, target_bps);
|
||||||
unsigned i;
|
unsigned i;
|
||||||
|
|
||||||
for(i = delta; i < wide_samples_in_reservoir_ * channels; i++)
|
for(i = delta; i < wide_samples_in_reservoir_ * channels; i++)
|
||||||
|
|||||||
Reference in New Issue
Block a user