2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-01-31 20:33:26 +01:00
|
|
|
* Copyright (C) 2003-2013 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
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-31 20:33:26 +01:00
|
|
|
#include "PcmDither.hxx"
|
|
|
|
#include "PcmPrng.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
|
2013-12-22 16:09:07 +01:00
|
|
|
PcmDither::Dither(T sample)
|
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-22 16:09:07 +01:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2013-12-22 21:58:18 +01:00
|
|
|
template<typename ST, typename DT>
|
|
|
|
inline typename DT::value_type
|
|
|
|
PcmDither::DitherShift(typename ST::value_type sample)
|
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,
|
|
|
|
scale_bits>(sample) >> scale_bits;
|
2008-10-23 16:58:07 +02:00
|
|
|
}
|
|
|
|
|
2013-12-22 21:58:18 +01:00
|
|
|
template<typename ST, typename DT>
|
|
|
|
inline void
|
|
|
|
PcmDither::DitherShift(typename DT::pointer_type dest,
|
|
|
|
typename ST::const_pointer_type src,
|
|
|
|
typename ST::const_pointer_type src_end)
|
2008-10-23 16:58:07 +02:00
|
|
|
{
|
2011-10-09 12:37:32 +02:00
|
|
|
while (src < src_end)
|
2013-12-22 21:58:18 +01:00
|
|
|
*dest++ = DitherShift<ST, DT>(*src++);
|
2008-10-23 16:58:07 +02:00
|
|
|
}
|
2009-03-02 16:37:11 +01:00
|
|
|
|
2013-12-22 21:58:18 +01:00
|
|
|
void
|
|
|
|
PcmDither::Dither24To16(int16_t *dest, const int32_t *src,
|
|
|
|
const int32_t *src_end)
|
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;
|
|
|
|
DitherShift<ST, DT>(dest, src, src_end);
|
2009-03-02 16:37:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-31 22:43:28 +01:00
|
|
|
PcmDither::Dither32To16(int16_t *dest, const int32_t *src,
|
|
|
|
const int32_t *src_end)
|
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;
|
|
|
|
DitherShift<ST, DT>(dest, src, src_end);
|
2009-03-02 16:37:11 +01:00
|
|
|
}
|