dither.c 6.7 KB
Newer Older
Josh Coalson's avatar
Josh Coalson committed
1
/* plugin_common - Routines common to several plugins
Josh Coalson's avatar
Josh Coalson committed
2
 * Copyright (C) 2002,2003,2004,2005  Josh Coalson
Josh Coalson's avatar
Josh Coalson committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 *
 * dithering routine derived from (other GPLed source):
 * mad - MPEG audio decoder
 * Copyright (C) 2000-2001 Robert Leslie
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include "dither.h"
#include "FLAC/assert.h"

#ifdef max
#undef max
#endif
#define max(a,b) ((a)>(b)?(a):(b))


Josh Coalson's avatar
Josh Coalson committed
32
#if defined _MSC_VER
Josh Coalson's avatar
Josh Coalson committed
33
34
35
36
37
#define FLAC__INLINE __inline
#else
#define FLAC__INLINE
#endif

Josh Coalson's avatar
Josh Coalson committed
38
39
40
41
42
/* 32-bit pseudo-random number generator
 *
 * @@@ According to Miroslav, this one is poor quality, the one from the
 * @@@ original replaygain code is much better
 */
Josh Coalson's avatar
Josh Coalson committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
static FLAC__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 FLAC__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;
}

Josh Coalson's avatar
Josh Coalson committed
106
size_t FLAC__plugin_common__pack_pcm_signed_big_endian(FLAC__byte *data, const FLAC__int32 * const input[], unsigned wide_samples, unsigned channels, unsigned source_bps, unsigned target_bps)
107
108
109
110
111
112
113
{
	static dither_state dither[FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS];
	FLAC__byte * const start = data;
	FLAC__int32 sample;
	const FLAC__int32 *input_;
	unsigned samples, channel;
	const unsigned bytes_per_sample = target_bps / 8;
Josh Coalson's avatar
Josh Coalson committed
114
	const unsigned incr = bytes_per_sample * channels;
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

	FLAC__ASSERT(channels > 0 && channels <= FLAC_PLUGIN__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 - 1));
		const FLAC__int32 MAX = ~MIN; /*(1L << (source_bps-1)) - 1 */

		for(channel = 0; channel < channels; channel++) {
			
			samples = wide_samples;
			data = start + bytes_per_sample * channel;
			input_ = input[channel];

			while(samples--) {
				sample = linear_dither(source_bps, target_bps, *input_++, &dither[channel], MIN, MAX);

				switch(target_bps) {
					case 8:
						data[0] = sample ^ 0x80;
						break;
					case 16:
						data[0] = (FLAC__byte)(sample >> 8);
						data[1] = (FLAC__byte)sample;
						break;
					case 24:
						data[0] = (FLAC__byte)(sample >> 16);
						data[1] = (FLAC__byte)(sample >> 8);
						data[2] = (FLAC__byte)sample;
						break;
				}

Josh Coalson's avatar
Josh Coalson committed
151
				data += incr;
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
			}
		}
	}
	else {
		for(channel = 0; channel < channels; channel++) {
			samples = wide_samples;
			data = start + bytes_per_sample * channel;
			input_ = input[channel];

			while(samples--) {
				sample = *input_++;

				switch(target_bps) {
					case 8:
						data[0] = sample ^ 0x80;
						break;
					case 16:
						data[0] = (FLAC__byte)(sample >> 8);
						data[1] = (FLAC__byte)sample;
						break;
					case 24:
						data[0] = (FLAC__byte)(sample >> 16);
						data[1] = (FLAC__byte)(sample >> 8);
						data[2] = (FLAC__byte)sample;
						break;
				}

Josh Coalson's avatar
Josh Coalson committed
179
				data += incr;
180
181
182
183
			}
		}
	}

Josh Coalson's avatar
Josh Coalson committed
184
	return wide_samples * channels * (target_bps/8);
185
186
}

Josh Coalson's avatar
Josh Coalson committed
187
size_t FLAC__plugin_common__pack_pcm_signed_little_endian(FLAC__byte *data, const FLAC__int32 * const input[], unsigned wide_samples, unsigned channels, unsigned source_bps, unsigned target_bps)
Josh Coalson's avatar
Josh Coalson committed
188
{
Josh Coalson's avatar
Josh Coalson committed
189
	static dither_state dither[FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS];
Josh Coalson's avatar
Josh Coalson committed
190
191
	FLAC__byte * const start = data;
	FLAC__int32 sample;
192
193
	const FLAC__int32 *input_;
	unsigned samples, channel;
Josh Coalson's avatar
Josh Coalson committed
194
	const unsigned bytes_per_sample = target_bps / 8;
Josh Coalson's avatar
Josh Coalson committed
195
	const unsigned incr = bytes_per_sample * channels;
Josh Coalson's avatar
Josh Coalson committed
196

Josh Coalson's avatar
Josh Coalson committed
197
	FLAC__ASSERT(channels > 0 && channels <= FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS);
Josh Coalson's avatar
Josh Coalson committed
198
199
200
201
202
203
204
	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) {
205
		const FLAC__int32 MIN = -(1L << (source_bps - 1));
Josh Coalson's avatar
Josh Coalson committed
206
207
		const FLAC__int32 MAX = ~MIN; /*(1L << (source_bps-1)) - 1 */

208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
		for(channel = 0; channel < channels; channel++) {
			
			samples = wide_samples;
			data = start + bytes_per_sample * channel;
			input_ = input[channel];

			while(samples--) {
				sample = linear_dither(source_bps, target_bps, *input_++, &dither[channel], MIN, MAX);

				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;
				}

Josh Coalson's avatar
Josh Coalson committed
229
				data += incr;
230
			}
Josh Coalson's avatar
Josh Coalson committed
231
232
233
		}
	}
	else {
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
		for(channel = 0; channel < channels; channel++) {
			samples = wide_samples;
			data = start + bytes_per_sample * channel;
			input_ = input[channel];

			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;
				}

Josh Coalson's avatar
Josh Coalson committed
254
				data += incr;
Josh Coalson's avatar
Josh Coalson committed
255
256
257
258
			}
		}
	}

Josh Coalson's avatar
Josh Coalson committed
259
	return wide_samples * channels * (target_bps/8);
Josh Coalson's avatar
Josh Coalson committed
260
}