PcmDither.cxx 2.98 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 19
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21 22
#include "PcmDither.hxx"
#include "PcmPrng.hxx"
23
#include "Traits.hxx"
24

25
template<typename T, T MIN, T MAX, unsigned scale_bits>
26
inline T
27
PcmDither::Dither(T sample)
28
{
29 30
	constexpr T round = 1 << (scale_bits - 1);
	constexpr T mask = (1 << scale_bits) - 1;
31

32
	sample += error[0] - error[1] + error[2];
33

34 35
	error[2] = error[1];
	error[1] = error[0] / 2;
36 37

	/* round */
38
	T output = sample + round;
39

40
	const T rnd = pcm_prng(random);
41
	output += (rnd & mask) - (random & mask);
42

43
	random = rnd;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

	/* clip */
	if (output > MAX) {
		output = MAX;

		if (sample > MAX)
			sample = MAX;
	} else if (output < MIN) {
		output = MIN;

		if (sample < MIN)
			sample = MIN;
	}

	output &= ~mask;

60
	error[0] = sample - output;
61

62
	return output >> scale_bits;
63 64
}

65 66 67 68 69 70 71 72 73 74 75 76 77
template<typename ST, unsigned SBITS, unsigned DBITS>
inline ST
PcmDither::DitherShift(ST sample)
{
	static_assert(sizeof(ST) * 8 > SBITS, "Source type too small");
	static_assert(SBITS > DBITS, "Non-positive scale_bits");

	static constexpr ST MIN = -(ST(1) << (SBITS - 1));
	static constexpr ST MAX = (ST(1) << (SBITS - 1)) - 1;

	return Dither<ST, MIN, MAX, SBITS - DBITS>(sample);
}

78 79
template<typename ST, typename DT>
inline typename DT::value_type
80
PcmDither::DitherConvert(typename ST::value_type sample)
81
{
82 83 84 85 86 87
	static_assert(ST::BITS > DT::BITS,
		      "Sample formats cannot be dithered");

	constexpr unsigned scale_bits = ST::BITS - DT::BITS;

	return Dither<typename ST::sum_type, ST::MIN, ST::MAX,
88
		      scale_bits>(sample);
89 90
}

91 92
template<typename ST, typename DT>
inline void
93 94 95
PcmDither::DitherConvert(typename DT::pointer_type dest,
			 typename ST::const_pointer_type src,
			 typename ST::const_pointer_type src_end)
96
{
97
	while (src < src_end)
98
		*dest++ = DitherConvert<ST, DT>(*src++);
99
}
100

101
inline void
102 103
PcmDither::Dither24To16(int16_t *dest, const int32_t *src,
			const int32_t *src_end)
104
{
105 106
	typedef SampleTraits<SampleFormat::S24_P32> ST;
	typedef SampleTraits<SampleFormat::S16> DT;
107
	DitherConvert<ST, DT>(dest, src, src_end);
108 109
}

110
inline void
111 112
PcmDither::Dither32To16(int16_t *dest, const int32_t *src,
			const int32_t *src_end)
113
{
114 115
	typedef SampleTraits<SampleFormat::S32> ST;
	typedef SampleTraits<SampleFormat::S16> DT;
116
	DitherConvert<ST, DT>(dest, src, src_end);
117
}