2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "pcm_dither.h"
25 pcm_dither_sample_24_to_16(int32_t sample
, struct pcm_dither
*dither
)
32 scale_bits
= from_bits
- to_bits
,
33 round
= 1 << (scale_bits
- 1),
34 mask
= (1 << scale_bits
) - 1,
35 ONE
= 1 << (from_bits
- 1),
40 sample
+= dither
->error
[0] - dither
->error
[1] + dither
->error
[2];
42 dither
->error
[2] = dither
->error
[1];
43 dither
->error
[1] = dither
->error
[0] / 2;
46 output
= sample
+ round
;
48 rnd
= pcm_prng(dither
->random
);
49 output
+= (rnd
& mask
) - (dither
->random
& mask
);
59 } else if (output
< MIN
) {
68 dither
->error
[0] = sample
- output
;
70 return (int16_t)(output
>> scale_bits
);
74 pcm_dither_24_to_16(struct pcm_dither
*dither
,
75 int16_t *dest
, const int32_t *src
,
78 while (num_samples
-- > 0)
79 *dest
++ = pcm_dither_sample_24_to_16(*src
++, dither
);
83 pcm_dither_sample_32_to_16(int32_t sample
, struct pcm_dither
*dither
)
85 return pcm_dither_sample_24_to_16(sample
>> 8, dither
);
89 pcm_dither_32_to_16(struct pcm_dither
*dither
,
90 int16_t *dest
, const int32_t *src
,
93 while (num_samples
-- > 0)
94 *dest
++ = pcm_dither_sample_32_to_16(*src
++, dither
);