/* * Copyright (C) 2003-2014 The Music Player Daemon Project * http://www.musicpd.org * * 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. * * 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. */ #include "config.h" #include "PcmFormat.hxx" #include "PcmBuffer.hxx" #include "PcmUtils.hxx" #include "Traits.hxx" #include "util/ConstBuffer.hxx" #include "util/WritableBuffer.hxx" #include "PcmDither.cxx" // including the .cxx file to get inlined templates template static inline ConstBuffer ToConst(WritableBuffer b) { return { b.data, b.size }; } static void pcm_convert_8_to_16(int16_t *out, const int8_t *in, size_t n) { for (size_t i = 0; i != n; ++i) out[i] = in[i] << 8; } static void pcm_convert_24_to_16(PcmDither &dither, int16_t *out, const int32_t *in, const int32_t *in_end) { dither.Dither24To16(out, in, in_end); } static void pcm_convert_32_to_16(PcmDither &dither, int16_t *out, const int32_t *in, const int32_t *in_end) { dither.Dither32To16(out, in, in_end); } template> static void ConvertFromFloat(typename Traits::pointer_type dest, const float *src, size_t n) { const float factor = 1 << (Traits::BITS - 1); for (size_t i = 0; i != n; ++i) { typename Traits::long_type sample(src[i] * factor); dest[i] = PcmClamp(sample); } } template> static WritableBuffer AllocateFromFloat(PcmBuffer &buffer, ConstBuffer src) { auto dest = buffer.GetT(src.size); ConvertFromFloat(dest, src.data, src.size); return { dest, src.size }; } static ConstBuffer pcm_allocate_8_to_16(PcmBuffer &buffer, ConstBuffer src) { auto dest = buffer.GetT(src.size); pcm_convert_8_to_16(dest, src.data, src.size); return { dest, src.size }; } static ConstBuffer pcm_allocate_24p32_to_16(PcmBuffer &buffer, PcmDither &dither, ConstBuffer src) { auto dest = buffer.GetT(src.size); pcm_convert_24_to_16(dither, dest, src.data, src.end()); return { dest, src.size }; } static ConstBuffer pcm_allocate_32_to_16(PcmBuffer &buffer, PcmDither &dither, ConstBuffer src) { auto dest = buffer.GetT(src.size); pcm_convert_32_to_16(dither, dest, src.data, src.end()); return { dest, src.size }; } static ConstBuffer pcm_allocate_float_to_16(PcmBuffer &buffer, ConstBuffer src) { return ToConst(AllocateFromFloat(buffer, src)); } ConstBuffer pcm_convert_to_16(PcmBuffer &buffer, PcmDither &dither, SampleFormat src_format, ConstBuffer src) { switch (src_format) { case SampleFormat::UNDEFINED: case SampleFormat::DSD: break; case SampleFormat::S8: return pcm_allocate_8_to_16(buffer, ConstBuffer::FromVoid(src)); case SampleFormat::S16: return ConstBuffer::FromVoid(src); case SampleFormat::S24_P32: return pcm_allocate_24p32_to_16(buffer, dither, ConstBuffer::FromVoid(src)); case SampleFormat::S32: return pcm_allocate_32_to_16(buffer, dither, ConstBuffer::FromVoid(src)); case SampleFormat::FLOAT: return pcm_allocate_float_to_16(buffer, ConstBuffer::FromVoid(src)); } return nullptr; } static void pcm_convert_8_to_24(int32_t *out, const int8_t *in, size_t n) { for (size_t i = 0; i != n; ++i) out[i] = in[i] << 16; } static void pcm_convert_16_to_24(int32_t *out, const int16_t *in, size_t n) { for (size_t i = 0; i != n; ++i) out[i] = in[i] << 8; } static void pcm_convert_32_to_24(int32_t *gcc_restrict out, const int32_t *gcc_restrict in, size_t n) { for (size_t i = 0; i != n; ++i) out[i] = in[i] >> 8; } static ConstBuffer pcm_allocate_8_to_24(PcmBuffer &buffer, ConstBuffer src) { auto dest = buffer.GetT(src.size); pcm_convert_8_to_24(dest, src.data, src.size); return { dest, src.size }; } static ConstBuffer pcm_allocate_16_to_24(PcmBuffer &buffer, ConstBuffer src) { auto dest = buffer.GetT(src.size); pcm_convert_16_to_24(dest, src.data, src.size); return { dest, src.size }; } static ConstBuffer pcm_allocate_32_to_24(PcmBuffer &buffer, ConstBuffer src) { auto dest = buffer.GetT(src.size); pcm_convert_32_to_24(dest, src.data, src.size); return { dest, src.size }; } static WritableBuffer pcm_allocate_float_to_24(PcmBuffer &buffer, ConstBuffer src) { return AllocateFromFloat(buffer, src); } ConstBuffer pcm_convert_to_24(PcmBuffer &buffer, SampleFormat src_format, ConstBuffer src) { switch (src_format) { case SampleFormat::UNDEFINED: case SampleFormat::DSD: break; case SampleFormat::S8: return pcm_allocate_8_to_24(buffer, ConstBuffer::FromVoid(src)); case SampleFormat::S16: return pcm_allocate_16_to_24(buffer, ConstBuffer::FromVoid(src)); case SampleFormat::S24_P32: return ConstBuffer::FromVoid(src); case SampleFormat::S32: return pcm_allocate_32_to_24(buffer, ConstBuffer::FromVoid(src)); case SampleFormat::FLOAT: return ToConst(pcm_allocate_float_to_24(buffer, ConstBuffer::FromVoid(src))); } return nullptr; } static void pcm_convert_8_to_32(int32_t *out, const int8_t *in, size_t n) { for (size_t i = 0; i != n; ++i) out[i] = in[i] << 24; } static void pcm_convert_16_to_32(int32_t *out, const int16_t *in, size_t n) { for (size_t i = 0; i != n; ++i) out[i] = in[i] << 16; } static void pcm_convert_24_to_32(int32_t *gcc_restrict out, const int32_t *gcc_restrict in, size_t n) { for (size_t i = 0; i != n; ++i) out[i] = in[i] << 8; } static ConstBuffer pcm_allocate_8_to_32(PcmBuffer &buffer, ConstBuffer src) { auto dest = buffer.GetT(src.size); pcm_convert_8_to_32(dest, src.data, src.size); return { dest, src.size }; } static ConstBuffer pcm_allocate_16_to_32(PcmBuffer &buffer, ConstBuffer src) { auto dest = buffer.GetT(src.size); pcm_convert_16_to_32(dest, src.data, src.size); return { dest, src.size }; } static ConstBuffer pcm_allocate_24p32_to_32(PcmBuffer &buffer, ConstBuffer src) { auto dest = buffer.GetT(src.size); pcm_convert_24_to_32(dest, src.data, src.size); return { dest, src.size }; } static ConstBuffer pcm_allocate_float_to_32(PcmBuffer &buffer, ConstBuffer src) { /* convert to S24_P32 first */ auto dest = pcm_allocate_float_to_24(buffer, src); /* convert to 32 bit in-place */ pcm_convert_24_to_32(dest.data, dest.data, src.size); return ToConst(dest); } ConstBuffer pcm_convert_to_32(PcmBuffer &buffer, SampleFormat src_format, ConstBuffer src) { switch (src_format) { case SampleFormat::UNDEFINED: case SampleFormat::DSD: break; case SampleFormat::S8: return pcm_allocate_8_to_32(buffer, ConstBuffer::FromVoid(src)); case SampleFormat::S16: return pcm_allocate_16_to_32(buffer, ConstBuffer::FromVoid(src)); case SampleFormat::S24_P32: return pcm_allocate_24p32_to_32(buffer, ConstBuffer::FromVoid(src)); case SampleFormat::S32: return ConstBuffer::FromVoid(src); case SampleFormat::FLOAT: return pcm_allocate_float_to_32(buffer, ConstBuffer::FromVoid(src)); } return nullptr; } template> static void ConvertToFloat(float *dest, typename Traits::const_pointer_type src, size_t n) { constexpr float factor = 0.5 / (1 << (Traits::BITS - 2)); for (size_t i = 0; i != n; ++i) dest[i] = float(src[i]) * factor; } template> static ConstBuffer AllocateToFloat(PcmBuffer &buffer, ConstBuffer src) { float *dest = buffer.GetT(src.size); ConvertToFloat(dest, src.data, src.size); return { dest, src.size }; } static ConstBuffer pcm_allocate_8_to_float(PcmBuffer &buffer, ConstBuffer src) { return AllocateToFloat(buffer, src); } static ConstBuffer pcm_allocate_16_to_float(PcmBuffer &buffer, ConstBuffer src) { return AllocateToFloat(buffer, src); } static ConstBuffer pcm_allocate_24p32_to_float(PcmBuffer &buffer, ConstBuffer src) { return AllocateToFloat(buffer, src); } static ConstBuffer pcm_allocate_32_to_float(PcmBuffer &buffer, ConstBuffer src) { return AllocateToFloat(buffer, src); } ConstBuffer pcm_convert_to_float(PcmBuffer &buffer, SampleFormat src_format, ConstBuffer src) { switch (src_format) { case SampleFormat::UNDEFINED: case SampleFormat::DSD: break; case SampleFormat::S8: return pcm_allocate_8_to_float(buffer, ConstBuffer::FromVoid(src)); case SampleFormat::S16: return pcm_allocate_16_to_float(buffer, ConstBuffer::FromVoid(src)); case SampleFormat::S32: return pcm_allocate_32_to_float(buffer, ConstBuffer::FromVoid(src)); case SampleFormat::S24_P32: return pcm_allocate_24p32_to_float(buffer, ConstBuffer::FromVoid(src)); case SampleFormat::FLOAT: return ConstBuffer::FromVoid(src); } return nullptr; }