2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2019-06-17 11:17:30 +02:00
|
|
|
* Copyright 2003-2019 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-10-23 16:58:07 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2008-10-23 16:58:07 +02:00
|
|
|
*/
|
|
|
|
|
2019-06-17 11:10:33 +02:00
|
|
|
#include "Dither.hxx"
|
|
|
|
#include "Prng.hxx"
|
2013-12-22 21:58:18 +01:00
|
|
|
#include "Traits.hxx"
|
2008-10-23 16:58:07 +02:00
|
|
|
|
2013-12-22 16:09:07 +01:00
|
|
|
template<typename T, T MIN, T MAX, unsigned scale_bits>
|
2013-12-22 21:58:18 +01:00
|
|
|
inline T
|
2018-01-01 19:07:33 +01:00
|
|
|
PcmDither::Dither(T sample) noexcept
|
2008-10-23 16:58:07 +02:00
|
|
|
{
|
2013-12-22 16:09:07 +01:00
|
|
|
constexpr T round = 1 << (scale_bits - 1);
|
|
|
|
constexpr T mask = (1 << scale_bits) - 1;
|
2008-10-23 16:58:07 +02:00
|
|
|
|
2013-01-31 22:43:28 +01:00
|
|
|
sample += error[0] - error[1] + error[2];
|
2008-10-23 16:58:07 +02:00
|
|
|
|
2013-01-31 22:43:28 +01:00
|
|
|
error[2] = error[1];
|
|
|
|
error[1] = error[0] / 2;
|
2008-10-23 16:58:07 +02:00
|
|
|
|
|
|
|
/* round */
|
2013-12-22 16:09:07 +01:00
|
|
|
T output = sample + round;
|
2008-10-23 16:58:07 +02:00
|
|
|
|
2013-12-22 16:09:07 +01:00
|
|
|
const T rnd = pcm_prng(random);
|
2013-01-31 22:43:28 +01:00
|
|
|
output += (rnd & mask) - (random & mask);
|
2008-10-23 16:58:07 +02:00
|
|
|
|
2013-01-31 22:43:28 +01:00
|
|
|
random = rnd;
|
2008-10-23 16:58:07 +02:00
|
|
|
|
|
|
|
/* clip */
|
|
|
|
if (output > MAX) {
|
|
|
|
output = MAX;
|
|
|
|
|
|
|
|
if (sample > MAX)
|
|
|
|
sample = MAX;
|
|
|
|
} else if (output < MIN) {
|
|
|
|
output = MIN;
|
|
|
|
|
|
|
|
if (sample < MIN)
|
|
|
|
sample = MIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
output &= ~mask;
|
|
|
|
|
2013-01-31 22:43:28 +01:00
|
|
|
error[0] = sample - output;
|
2008-10-23 16:58:07 +02:00
|
|
|
|
2013-12-24 23:39:29 +01:00
|
|
|
return output >> scale_bits;
|
2013-12-22 16:09:07 +01:00
|
|
|
}
|
|
|
|
|
2013-12-22 17:39:26 +01:00
|
|
|
template<typename ST, unsigned SBITS, unsigned DBITS>
|
|
|
|
inline ST
|
2018-01-01 19:07:33 +01:00
|
|
|
PcmDither::DitherShift(ST sample) noexcept
|
2013-12-22 17:39:26 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-12-22 21:58:18 +01:00
|
|
|
template<typename ST, typename DT>
|
|
|
|
inline typename DT::value_type
|
2018-01-01 19:07:33 +01:00
|
|
|
PcmDither::DitherConvert(typename ST::value_type sample) noexcept
|
2013-12-22 16:09:07 +01:00
|
|
|
{
|
2013-12-22 21:58:18 +01:00
|
|
|
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,
|
2013-12-24 23:39:29 +01:00
|
|
|
scale_bits>(sample);
|
2008-10-23 16:58:07 +02:00
|
|
|
}
|
|
|
|
|
2013-12-22 21:58:18 +01:00
|
|
|
template<typename ST, typename DT>
|
|
|
|
inline void
|
2020-01-03 15:59:50 +01:00
|
|
|
PcmDither::DitherConvert(typename DT::pointer dest,
|
|
|
|
typename ST::const_pointer src,
|
|
|
|
typename ST::const_pointer src_end) noexcept
|
2008-10-23 16:58:07 +02:00
|
|
|
{
|
2011-10-09 12:37:32 +02:00
|
|
|
while (src < src_end)
|
2013-12-24 23:38:11 +01:00
|
|
|
*dest++ = DitherConvert<ST, DT>(*src++);
|
2008-10-23 16:58:07 +02:00
|
|
|
}
|
2009-03-02 16:37:11 +01:00
|
|
|
|
2013-12-28 18:30:13 +01:00
|
|
|
inline void
|
2013-12-22 21:58:18 +01:00
|
|
|
PcmDither::Dither24To16(int16_t *dest, const int32_t *src,
|
2018-01-01 19:07:33 +01:00
|
|
|
const int32_t *src_end) noexcept
|
2009-03-02 16:37:11 +01:00
|
|
|
{
|
2013-12-22 21:58:18 +01:00
|
|
|
typedef SampleTraits<SampleFormat::S24_P32> ST;
|
|
|
|
typedef SampleTraits<SampleFormat::S16> DT;
|
2013-12-24 23:38:11 +01:00
|
|
|
DitherConvert<ST, DT>(dest, src, src_end);
|
2009-03-02 16:37:11 +01:00
|
|
|
}
|
|
|
|
|
2013-12-28 18:30:13 +01:00
|
|
|
inline void
|
2013-01-31 22:43:28 +01:00
|
|
|
PcmDither::Dither32To16(int16_t *dest, const int32_t *src,
|
2018-01-01 19:07:33 +01:00
|
|
|
const int32_t *src_end) noexcept
|
2009-03-02 16:37:11 +01:00
|
|
|
{
|
2013-12-22 21:58:18 +01:00
|
|
|
typedef SampleTraits<SampleFormat::S32> ST;
|
|
|
|
typedef SampleTraits<SampleFormat::S16> DT;
|
2013-12-24 23:38:11 +01:00
|
|
|
DitherConvert<ST, DT>(dest, src, src_end);
|
2009-03-02 16:37:11 +01:00
|
|
|
}
|