PcmDither.cxx 3 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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
 */

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

24
template<typename T, T MIN, T MAX, unsigned scale_bits>
25
inline T
Max Kellermann's avatar
Max Kellermann committed
26
PcmDither::Dither(T sample) noexcept
27
{
28 29
	constexpr T round = 1 << (scale_bits - 1);
	constexpr T mask = (1 << scale_bits) - 1;
30

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

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

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

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

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

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

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

		if (sample < MIN)
			sample = MIN;
	}

	output &= ~mask;

59
	error[0] = sample - output;
60

61
	return output >> scale_bits;
62 63
}

64 65
template<typename ST, unsigned SBITS, unsigned DBITS>
inline ST
Max Kellermann's avatar
Max Kellermann committed
66
PcmDither::DitherShift(ST sample) noexcept
67 68 69 70 71 72 73 74 75 76
{
	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);
}

77 78
template<typename ST, typename DT>
inline typename DT::value_type
Max Kellermann's avatar
Max Kellermann committed
79
PcmDither::DitherConvert(typename ST::value_type sample) noexcept
80
{
81 82 83 84 85 86
	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,
87
		      scale_bits>(sample);
88 89
}

90 91
template<typename ST, typename DT>
inline void
92 93
PcmDither::DitherConvert(typename DT::pointer_type dest,
			 typename ST::const_pointer_type src,
Max Kellermann's avatar
Max Kellermann committed
94
			 typename ST::const_pointer_type src_end) noexcept
95
{
96
	while (src < src_end)
97
		*dest++ = DitherConvert<ST, DT>(*src++);
98
}
99

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

109
inline void
110
PcmDither::Dither32To16(int16_t *dest, const int32_t *src,
Max Kellermann's avatar
Max Kellermann committed
111
			const int32_t *src_end) noexcept
112
{
113 114
	typedef SampleTraits<SampleFormat::S32> ST;
	typedef SampleTraits<SampleFormat::S16> DT;
115
	DitherConvert<ST, DT>(dest, src, src_end);
116
}