1 /* plugin_common - Routines common to several plugins
2 * Copyright (C) 2002 Josh Coalson
4 * dithering routine derived from (other GPLed source):
5 * mad - MPEG audio decoder
6 * Copyright (C) 2000-2001 Robert Leslie
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "FLAC/assert.h"
29 #define max(a,b) ((a)>(b)?(a):(b))
32 #if defined _MSC_VER || defined __MINGW32__
33 #define FLAC__INLINE __inline
38 /* 32-bit pseudo-random number generator */
39 static FLAC__INLINE FLAC__uint32 prng(FLAC__uint32 state)
41 return (state * 0x0019660dL + 0x3c6ef35fL) & 0xffffffffL;
44 /* dither routine derived from MAD winamp plugin */
51 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)
54 FLAC__int32 output, mask, random;
56 FLAC__ASSERT(source_bps < 32);
57 FLAC__ASSERT(target_bps <= 24);
58 FLAC__ASSERT(target_bps <= source_bps);
61 sample += dither->error[0] - dither->error[1] + dither->error[2];
63 dither->error[2] = dither->error[1];
64 dither->error[1] = dither->error[0] / 2;
67 output = sample + (1L << (source_bps - target_bps - 1));
69 scalebits = source_bps - target_bps;
70 mask = (1L << scalebits) - 1;
73 random = (FLAC__int32)prng(dither->random);
74 output += (random & mask) - (dither->random & mask);
76 dither->random = random;
85 else if(output < MIN) {
96 dither->error[0] = sample - output;
99 return output >> scalebits;
102 unsigned FLAC__plugin_common__pack_pcm_signed_little_endian(FLAC__byte *data, FLAC__int32 *input, unsigned wide_samples, unsigned channels, unsigned source_bps, unsigned target_bps)
104 static dither_state dither[FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS];
105 FLAC__byte * const start = data;
107 unsigned samples = wide_samples * channels;
108 const unsigned bytes_per_sample = target_bps / 8;
110 FLAC__ASSERT(FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS == 2);
111 FLAC__ASSERT(channels > 0 && channels <= FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS);
112 FLAC__ASSERT(source_bps < 32);
113 FLAC__ASSERT(target_bps <= 24);
114 FLAC__ASSERT(target_bps <= source_bps);
115 FLAC__ASSERT((source_bps & 7) == 0);
116 FLAC__ASSERT((target_bps & 7) == 0);
118 if(source_bps != target_bps) {
119 const FLAC__int32 MIN = -(1L << source_bps);
120 const FLAC__int32 MAX = ~MIN; /*(1L << (source_bps-1)) - 1 */
121 const unsigned dither_twiggle = channels - 1;
122 unsigned dither_source = 0;
125 sample = linear_dither(source_bps, target_bps, *input++, &dither[dither_source], MIN, MAX);
126 dither_source ^= dither_twiggle;
130 data[0] = sample ^ 0x80;
133 data[2] = (FLAC__byte)(sample >> 16);
136 data[1] = (FLAC__byte)(sample >> 8);
137 data[0] = (FLAC__byte)sample;
140 data += bytes_per_sample;
149 data[0] = sample ^ 0x80;
152 data[2] = (FLAC__byte)(sample >> 16);
155 data[1] = (FLAC__byte)(sample >> 8);
156 data[0] = (FLAC__byte)sample;
159 data += bytes_per_sample;