2009-01-07 18:53:36 +01:00
|
|
|
/*
|
2013-10-30 23:37:06 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-01-07 18:53:36 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 18:06:14 +01: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.
|
2004-02-24 18:06:14 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-30 23:40:56 +01:00
|
|
|
#include "PcmConvert.hxx"
|
2013-01-31 20:33:26 +01:00
|
|
|
#include "PcmChannels.hxx"
|
|
|
|
#include "PcmFormat.hxx"
|
2013-08-03 21:00:50 +02:00
|
|
|
#include "AudioFormat.hxx"
|
2013-11-29 22:06:14 +01:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
2008-10-08 10:49:29 +02:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <math.h>
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
const Domain pcm_convert_domain("pcm_convert");
|
|
|
|
|
2013-11-29 10:06:20 +01:00
|
|
|
bool
|
|
|
|
pcm_convert_global_init(Error &error)
|
|
|
|
{
|
|
|
|
return pcm_resample_global_init(error);
|
|
|
|
}
|
|
|
|
|
2013-01-30 23:40:56 +01:00
|
|
|
PcmConvert::PcmConvert()
|
2008-10-21 22:53:16 +02:00
|
|
|
{
|
2013-11-11 16:15:38 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
src_format.Clear();
|
|
|
|
dest_format.Clear();
|
|
|
|
#endif
|
2008-10-21 22:53:16 +02:00
|
|
|
}
|
|
|
|
|
2013-01-30 23:40:56 +01:00
|
|
|
PcmConvert::~PcmConvert()
|
2009-01-07 22:20:30 +01:00
|
|
|
{
|
2013-11-11 16:15:38 +01:00
|
|
|
assert(!src_format.IsValid());
|
|
|
|
assert(!dest_format.IsValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PcmConvert::Open(AudioFormat _src_format, AudioFormat _dest_format,
|
|
|
|
gcc_unused Error &error)
|
|
|
|
{
|
|
|
|
assert(!src_format.IsValid());
|
|
|
|
assert(!dest_format.IsValid());
|
|
|
|
assert(_src_format.IsValid());
|
|
|
|
assert(_dest_format.IsValid());
|
|
|
|
|
|
|
|
src_format = _src_format;
|
|
|
|
dest_format = _dest_format;
|
|
|
|
|
2013-11-28 18:43:33 +01:00
|
|
|
is_dsd = src_format.format == SampleFormat::DSD;
|
|
|
|
if (is_dsd)
|
|
|
|
src_format.format = SampleFormat::FLOAT;
|
|
|
|
|
2013-11-11 16:15:38 +01:00
|
|
|
return true;
|
2009-01-07 22:20:30 +01:00
|
|
|
}
|
|
|
|
|
2012-03-01 00:59:53 +01:00
|
|
|
void
|
2013-11-11 16:15:38 +01:00
|
|
|
PcmConvert::Close()
|
2012-03-01 00:59:53 +01:00
|
|
|
{
|
2013-07-29 07:56:40 +02:00
|
|
|
dsd.Reset();
|
2013-07-29 23:18:55 +02:00
|
|
|
resampler.Reset();
|
2013-11-11 16:15:38 +01:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
src_format.Clear();
|
|
|
|
dest_format.Clear();
|
|
|
|
#endif
|
2012-03-01 00:59:53 +01:00
|
|
|
}
|
|
|
|
|
2013-01-30 23:40:56 +01:00
|
|
|
inline const int16_t *
|
2013-11-11 16:15:38 +01:00
|
|
|
PcmConvert::Convert16(const void *src_buffer, size_t src_size,
|
|
|
|
size_t *dest_size_r,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2007-05-23 01:11:36 +02:00
|
|
|
{
|
2008-10-12 11:28:37 +02:00
|
|
|
const int16_t *buf;
|
2008-10-23 20:11:24 +02:00
|
|
|
size_t len;
|
2007-05-23 01:11:36 +02:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
assert(dest_format.format == SampleFormat::S16);
|
2007-05-23 01:11:36 +02:00
|
|
|
|
2013-07-29 08:10:10 +02:00
|
|
|
buf = pcm_convert_to_16(format_buffer, dither,
|
2013-08-03 21:00:50 +02:00
|
|
|
src_format.format,
|
2013-01-30 23:40:56 +01:00
|
|
|
src_buffer, src_size,
|
2009-01-07 23:56:34 +01:00
|
|
|
&len);
|
2013-10-28 23:58:17 +01:00
|
|
|
if (buf == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(pcm_convert_domain,
|
|
|
|
"Conversion from %s to 16 bit is not implemented",
|
|
|
|
sample_format_to_string(src_format.format));
|
2013-10-28 23:58:17 +01:00
|
|
|
return nullptr;
|
2009-07-23 12:01:03 +02:00
|
|
|
}
|
2007-05-23 01:11:36 +02:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
if (src_format.channels != dest_format.channels) {
|
2013-07-29 08:10:10 +02:00
|
|
|
buf = pcm_convert_channels_16(channels_buffer,
|
2013-08-03 21:00:50 +02:00
|
|
|
dest_format.channels,
|
|
|
|
src_format.channels,
|
2008-10-23 20:03:14 +02:00
|
|
|
buf, len, &len);
|
2013-10-28 23:58:17 +01:00
|
|
|
if (buf == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(pcm_convert_domain,
|
|
|
|
"Conversion from %u to %u channels "
|
|
|
|
"is not implemented",
|
|
|
|
src_format.channels,
|
|
|
|
dest_format.channels);
|
2013-10-28 23:58:17 +01:00
|
|
|
return nullptr;
|
2009-07-23 12:01:03 +02:00
|
|
|
}
|
2007-05-23 01:11:36 +02:00
|
|
|
}
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
if (src_format.sample_rate != dest_format.sample_rate) {
|
|
|
|
buf = resampler.Resample16(dest_format.channels,
|
|
|
|
src_format.sample_rate, buf, len,
|
|
|
|
dest_format.sample_rate, &len,
|
2013-08-10 18:02:44 +02:00
|
|
|
error);
|
2013-10-28 23:58:17 +01:00
|
|
|
if (buf == nullptr)
|
|
|
|
return nullptr;
|
2009-07-23 12:01:03 +02:00
|
|
|
}
|
2009-01-08 00:46:38 +01:00
|
|
|
|
2009-01-17 13:11:16 +01:00
|
|
|
*dest_size_r = len;
|
|
|
|
return buf;
|
2004-05-10 16:06:23 +02:00
|
|
|
}
|
|
|
|
|
2013-01-30 23:40:56 +01:00
|
|
|
inline const int32_t *
|
2013-11-11 16:15:38 +01:00
|
|
|
PcmConvert::Convert24(const void *src_buffer, size_t src_size,
|
|
|
|
size_t *dest_size_r,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2008-10-23 20:11:37 +02:00
|
|
|
{
|
|
|
|
const int32_t *buf;
|
|
|
|
size_t len;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
assert(dest_format.format == SampleFormat::S24_P32);
|
2008-10-23 20:11:37 +02:00
|
|
|
|
2013-07-29 08:10:10 +02:00
|
|
|
buf = pcm_convert_to_24(format_buffer,
|
2013-08-03 21:00:50 +02:00
|
|
|
src_format.format,
|
2008-10-23 20:11:37 +02:00
|
|
|
src_buffer, src_size, &len);
|
2013-10-28 23:58:17 +01:00
|
|
|
if (buf == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(pcm_convert_domain,
|
|
|
|
"Conversion from %s to 24 bit is not implemented",
|
|
|
|
sample_format_to_string(src_format.format));
|
2013-10-28 23:58:17 +01:00
|
|
|
return nullptr;
|
2009-07-23 12:01:03 +02:00
|
|
|
}
|
2008-10-23 20:11:37 +02:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
if (src_format.channels != dest_format.channels) {
|
2013-07-29 08:10:10 +02:00
|
|
|
buf = pcm_convert_channels_24(channels_buffer,
|
2013-08-03 21:00:50 +02:00
|
|
|
dest_format.channels,
|
|
|
|
src_format.channels,
|
2008-10-23 20:11:37 +02:00
|
|
|
buf, len, &len);
|
2013-10-28 23:58:17 +01:00
|
|
|
if (buf == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(pcm_convert_domain,
|
|
|
|
"Conversion from %u to %u channels "
|
|
|
|
"is not implemented",
|
|
|
|
src_format.channels,
|
|
|
|
dest_format.channels);
|
2013-10-28 23:58:17 +01:00
|
|
|
return nullptr;
|
2009-07-23 12:01:03 +02:00
|
|
|
}
|
2008-10-23 20:11:37 +02:00
|
|
|
}
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
if (src_format.sample_rate != dest_format.sample_rate) {
|
|
|
|
buf = resampler.Resample24(dest_format.channels,
|
|
|
|
src_format.sample_rate, buf, len,
|
|
|
|
dest_format.sample_rate, &len,
|
2013-08-10 18:02:44 +02:00
|
|
|
error);
|
2013-10-28 23:58:17 +01:00
|
|
|
if (buf == nullptr)
|
|
|
|
return nullptr;
|
2009-07-23 12:01:03 +02:00
|
|
|
}
|
2009-01-08 00:46:38 +01:00
|
|
|
|
2009-01-17 13:11:16 +01:00
|
|
|
*dest_size_r = len;
|
|
|
|
return buf;
|
2008-10-23 20:11:37 +02:00
|
|
|
}
|
|
|
|
|
2013-01-30 23:40:56 +01:00
|
|
|
inline const int32_t *
|
2013-11-11 16:15:38 +01:00
|
|
|
PcmConvert::Convert32(const void *src_buffer, size_t src_size,
|
|
|
|
size_t *dest_size_r,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2009-03-02 16:41:10 +01:00
|
|
|
{
|
|
|
|
const int32_t *buf;
|
|
|
|
size_t len;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
assert(dest_format.format == SampleFormat::S32);
|
2009-03-02 16:41:10 +01:00
|
|
|
|
2013-07-29 08:10:10 +02:00
|
|
|
buf = pcm_convert_to_32(format_buffer,
|
2013-08-03 21:00:50 +02:00
|
|
|
src_format.format,
|
2009-03-02 16:41:10 +01:00
|
|
|
src_buffer, src_size, &len);
|
2013-10-28 23:58:17 +01:00
|
|
|
if (buf == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(pcm_convert_domain,
|
|
|
|
"Conversion from %s to 32 bit is not implemented",
|
|
|
|
sample_format_to_string(src_format.format));
|
2013-10-28 23:58:17 +01:00
|
|
|
return nullptr;
|
2009-07-23 12:01:03 +02:00
|
|
|
}
|
2009-03-02 16:41:10 +01:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
if (src_format.channels != dest_format.channels) {
|
2013-07-29 08:10:10 +02:00
|
|
|
buf = pcm_convert_channels_32(channels_buffer,
|
2013-08-03 21:00:50 +02:00
|
|
|
dest_format.channels,
|
|
|
|
src_format.channels,
|
2009-03-02 16:41:10 +01:00
|
|
|
buf, len, &len);
|
2013-10-28 23:58:17 +01:00
|
|
|
if (buf == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(pcm_convert_domain,
|
|
|
|
"Conversion from %u to %u channels "
|
|
|
|
"is not implemented",
|
|
|
|
src_format.channels,
|
|
|
|
dest_format.channels);
|
2013-10-28 23:58:17 +01:00
|
|
|
return nullptr;
|
2009-07-23 12:01:03 +02:00
|
|
|
}
|
2009-03-02 16:41:10 +01:00
|
|
|
}
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
if (src_format.sample_rate != dest_format.sample_rate) {
|
|
|
|
buf = resampler.Resample32(dest_format.channels,
|
|
|
|
src_format.sample_rate, buf, len,
|
|
|
|
dest_format.sample_rate, &len,
|
2013-08-10 18:02:44 +02:00
|
|
|
error);
|
2013-10-28 23:58:17 +01:00
|
|
|
if (buf == nullptr)
|
2009-07-23 12:01:03 +02:00
|
|
|
return buf;
|
|
|
|
}
|
2009-03-02 16:41:10 +01:00
|
|
|
|
|
|
|
*dest_size_r = len;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2013-01-30 23:40:56 +01:00
|
|
|
inline const float *
|
2013-11-11 16:15:38 +01:00
|
|
|
PcmConvert::ConvertFloat(const void *src_buffer, size_t src_size,
|
|
|
|
size_t *dest_size_r,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2011-10-08 15:36:29 +02:00
|
|
|
{
|
2013-01-30 23:40:56 +01:00
|
|
|
const float *buffer = (const float *)src_buffer;
|
2011-10-08 15:36:29 +02:00
|
|
|
size_t size = src_size;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
assert(dest_format.format == SampleFormat::FLOAT);
|
2011-10-08 15:36:29 +02:00
|
|
|
|
|
|
|
/* convert to float now */
|
|
|
|
|
2013-07-29 08:10:10 +02:00
|
|
|
buffer = pcm_convert_to_float(format_buffer,
|
2013-08-03 21:00:50 +02:00
|
|
|
src_format.format,
|
2011-10-08 15:36:29 +02:00
|
|
|
buffer, size, &size);
|
2013-10-28 23:58:17 +01:00
|
|
|
if (buffer == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(pcm_convert_domain,
|
|
|
|
"Conversion from %s to float is not implemented",
|
|
|
|
sample_format_to_string(src_format.format));
|
2013-10-28 23:58:17 +01:00
|
|
|
return nullptr;
|
2011-10-08 15:36:29 +02:00
|
|
|
}
|
|
|
|
|
2012-10-02 08:29:52 +02:00
|
|
|
/* convert channels */
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
if (src_format.channels != dest_format.channels) {
|
2013-07-29 08:10:10 +02:00
|
|
|
buffer = pcm_convert_channels_float(channels_buffer,
|
2013-08-03 21:00:50 +02:00
|
|
|
dest_format.channels,
|
|
|
|
src_format.channels,
|
2012-10-02 08:29:52 +02:00
|
|
|
buffer, size, &size);
|
2013-10-28 23:58:17 +01:00
|
|
|
if (buffer == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(pcm_convert_domain,
|
|
|
|
"Conversion from %u to %u channels "
|
|
|
|
"is not implemented",
|
|
|
|
src_format.channels,
|
|
|
|
dest_format.channels);
|
2013-10-28 23:58:17 +01:00
|
|
|
return nullptr;
|
2012-10-02 08:29:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-08 15:36:29 +02:00
|
|
|
/* resample with float, because this is the best format for
|
|
|
|
libsamplerate */
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
if (src_format.sample_rate != dest_format.sample_rate) {
|
|
|
|
buffer = resampler.ResampleFloat(dest_format.channels,
|
|
|
|
src_format.sample_rate,
|
2013-07-29 23:18:55 +02:00
|
|
|
buffer, size,
|
2013-08-03 21:00:50 +02:00
|
|
|
dest_format.sample_rate,
|
2013-08-10 18:02:44 +02:00
|
|
|
&size, error);
|
2013-10-28 23:58:17 +01:00
|
|
|
if (buffer == nullptr)
|
|
|
|
return nullptr;
|
2011-10-08 15:36:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*dest_size_r = size;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2009-01-17 13:11:16 +01:00
|
|
|
const void *
|
2013-11-11 16:15:38 +01:00
|
|
|
PcmConvert::Convert(const void *src, size_t src_size,
|
2013-01-30 23:40:56 +01:00
|
|
|
size_t *dest_size_r,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2008-10-23 20:11:24 +02:00
|
|
|
{
|
2013-11-29 22:06:14 +01:00
|
|
|
ConstBuffer<void> buffer(src, src_size);
|
|
|
|
|
2013-11-28 18:43:33 +01:00
|
|
|
if (is_dsd) {
|
2013-11-29 22:06:14 +01:00
|
|
|
auto s = ConstBuffer<uint8_t>::FromVoid(buffer);
|
|
|
|
auto d = dsd.ToFloat(src_format.channels,
|
|
|
|
false, s);
|
|
|
|
if (d.IsNull()) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(pcm_convert_domain,
|
|
|
|
"DSD to PCM conversion failed");
|
2013-10-28 23:58:17 +01:00
|
|
|
return nullptr;
|
2012-03-01 00:57:33 +01:00
|
|
|
}
|
|
|
|
|
2013-11-29 22:06:14 +01:00
|
|
|
buffer = d.ToVoid();
|
2012-03-01 00:57:33 +01:00
|
|
|
}
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
switch (dest_format.format) {
|
|
|
|
case SampleFormat::S16:
|
2013-11-29 22:06:14 +01:00
|
|
|
return Convert16(buffer.data, buffer.size,
|
2013-11-11 16:15:38 +01:00
|
|
|
dest_size_r,
|
2013-08-10 18:02:44 +02:00
|
|
|
error);
|
2009-01-17 13:11:16 +01:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S24_P32:
|
2013-11-29 22:06:14 +01:00
|
|
|
return Convert24(buffer.data, buffer.size,
|
2013-11-11 16:15:38 +01:00
|
|
|
dest_size_r,
|
2013-08-10 18:02:44 +02:00
|
|
|
error);
|
2008-10-23 20:11:24 +02:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S32:
|
2013-11-29 22:06:14 +01:00
|
|
|
return Convert32(buffer.data, buffer.size,
|
2013-11-11 16:15:38 +01:00
|
|
|
dest_size_r,
|
2013-08-10 18:02:44 +02:00
|
|
|
error);
|
2009-03-02 16:41:10 +01:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::FLOAT:
|
2013-11-29 22:06:14 +01:00
|
|
|
return ConvertFloat(buffer.data, buffer.size,
|
2013-11-11 16:15:38 +01:00
|
|
|
dest_size_r,
|
2013-08-10 18:02:44 +02:00
|
|
|
error);
|
2011-10-08 15:36:29 +02:00
|
|
|
|
2008-10-23 20:11:24 +02:00
|
|
|
default:
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(pcm_convert_domain,
|
|
|
|
"PCM conversion to %s is not implemented",
|
|
|
|
sample_format_to_string(dest_format.format));
|
2013-10-28 23:58:17 +01:00
|
|
|
return nullptr;
|
2008-10-23 20:11:24 +02:00
|
|
|
}
|
|
|
|
}
|