implement musepack decoding, closing out issue #9

adpcm
Dimitri Diakopoulos 11 years ago
parent c84116e048
commit dd97514f82

@ -31,6 +31,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "VorbisDecoder.h"
#include "OpusDecoder.h"
#include "CafDecoder.h"
#include "MusepackDecoder.h"
using namespace nqr;
@ -136,4 +137,5 @@ void NyquistIO::BuildDecoderTable()
AddDecoderToTable(std::make_shared<VorbisDecoder>());
AddDecoderToTable(std::make_shared<OpusDecoder>());
AddDecoderToTable(std::make_shared<CAFDecoder>());
AddDecoderToTable(std::make_shared<MusepackDecoder>());
}

@ -27,21 +27,170 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using namespace nqr;
#include "mpc/mpcdec.h"
#include "mpc/reader.h"
#include "musepack/libmpcdec/decoder.h"
#include "musepack/libmpcdec/internal.h"
class MusepackInternal
{
static const uint32_t STDIO_MAGIC = 0xF36D656D;
struct mpc_reader_state
{
unsigned char *p_file;
unsigned char *p_begin, *p_end;
mpc_bool_t is_seekable;
mpc_int32_t magic;
};
static mpc_int32_t read_mem(mpc_reader *p_reader, void *ptr, mpc_int32_t size)
{
mpc_reader_state *p_mem = (mpc_reader_state*) p_reader->data;
if (p_mem->magic != STDIO_MAGIC) return MPC_STATUS_FAIL;
mpc_int32_t max = (p_mem->p_end - p_mem->p_file);
if (size >= max) size = max;
memcpy((unsigned char *)ptr, p_mem->p_file, size);
p_mem->p_file += size;
return size;
}
static mpc_bool_t seek_mem(mpc_reader *p_reader, mpc_int32_t offset)
{
mpc_reader_state *p_mem = (mpc_reader_state*) p_reader->data;
if (p_mem->magic != STDIO_MAGIC) return MPC_FALSE;
if (!p_mem->is_seekable) return MPC_FALSE;
p_mem->p_file = p_mem->p_begin + offset;
if(p_mem->p_file < p_mem->p_begin) return MPC_FALSE;
if(p_mem->p_file >= p_mem->p_end ) return MPC_FALSE;
return MPC_TRUE;
}
static mpc_int32_t tell_mem(mpc_reader *p_reader)
{
mpc_reader_state *p_mem = (mpc_reader_state*) p_reader->data;
if(p_mem->magic != STDIO_MAGIC) return MPC_STATUS_FAIL;
return p_mem->p_file - p_mem->p_begin;
}
static mpc_int32_t get_size_mem(mpc_reader *p_reader)
{
mpc_reader_state *p_mem = (mpc_reader_state*) p_reader->data;
if (p_mem->magic != STDIO_MAGIC) return MPC_STATUS_FAIL;
return p_mem->p_end - p_mem->p_begin;
}
static mpc_bool_t canseek_mem(mpc_reader *p_reader)
{
mpc_reader_state *p_mem = (mpc_reader_state*) p_reader->data;
if (p_mem->magic != STDIO_MAGIC) return MPC_FALSE;
return p_mem->is_seekable;
}
public:
// Musepack is a purely variable bitrate format and does not work at a constant bitrate.
MusepackInternal(AudioData * d, const std::vector<uint8_t> & fileData) : d(d)
{
decoderMemory.reset(new mpc_reader_state());
decoderMemory->magic = STDIO_MAGIC;
decoderMemory->p_file = (unsigned char *) fileData.data();
decoderMemory->p_begin = (unsigned char *) fileData.data();
decoderMemory->p_end = (unsigned char *) fileData.data() + fileData.size();
decoderMemory->is_seekable = MPC_TRUE;
reader.data = decoderMemory.get();
reader.canseek = canseek_mem;
reader.get_size = get_size_mem;
reader.read = read_mem;
reader.seek = seek_mem;
reader.tell = tell_mem;
mpcDemux = mpc_demux_init(&reader);
if (!mpcDemux)
throw std::runtime_error("could not initialize mpc demuxer");
mpc_demux_get_info(mpcDemux, &streamInfo);
d->sampleRate = (float) streamInfo.sample_freq;
d->channelCount = streamInfo.channels;
d->sourceFormat = MakeFormatForBits(32, true, false);
d->lengthSeconds = (double) mpc_streaminfo_get_length(&streamInfo);
auto totalSamples = size_t(mpc_streaminfo_get_length_samples(&streamInfo));
d->samples.resize(totalSamples * d->channelCount);
if (!readInternal())
throw std::runtime_error("could not read any data");
}
size_t readInternal()
{
mpc_status err;
size_t totalSamplesRead = 0;
while(MPC_TRUE)
{
mpc_frame_info frame;
frame.buffer = d->samples.data() + totalSamplesRead;
err = mpc_demux_decode(mpcDemux, &frame);
if (frame.bits == -1) break;
totalSamplesRead += (frame.samples * streamInfo.channels);
}
if (err != MPC_STATUS_OK)
std::cerr << "Something went wrong... " << err << std::endl;
return totalSamplesRead;
}
~MusepackInternal()
{
mpc_demux_exit(mpcDemux);
}
private:
mpc_streaminfo streamInfo;
mpc_reader_t reader;
mpc_decoder decoder;
mpc_demux * mpcDemux;
std::unique_ptr<mpc_reader_state> decoderMemory;
NO_MOVE(MusepackInternal);
AudioData * d;
};
//////////////////////
// Public Interface //
//////////////////////
int MusepackDecoder::LoadFromPath(AudioData * data, const std::string & path)
{
return IOError::LoadPathNotImplemented;
auto fileBuffer = nqr::ReadFile(path);
MusepackInternal decoder(data, fileBuffer.buffer);
return IOError::NoError;
}
int MusepackDecoder::LoadFromBuffer(AudioData * data, const std::vector<uint8_t> & memory)
{
return IOError::LoadBufferNotImplemented;
MusepackInternal decoder(data, memory);
return IOError::NoError;
}
std::vector<std::string> MusepackDecoder::GetSupportedFileExtensions()
{
return {"mpc"};
return {"mpc", "mpp"};
}
Loading…
Cancel
Save