2012-10-28 02:12:18 +02:00
|
|
|
/***************************************************************************
|
|
|
|
copyright : (C) 2010 by Alex Novichkov
|
|
|
|
email : novichko@atnet.ru
|
|
|
|
|
|
|
|
copyright : (C) 2006 by Lukáš Lalinský
|
|
|
|
email : lalinsky@gmail.com
|
|
|
|
(original WavPack implementation)
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* This library is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Lesser General Public License version *
|
|
|
|
* 2.1 as published by the Free Software Foundation. *
|
|
|
|
* *
|
|
|
|
* This library 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 *
|
|
|
|
* Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public *
|
|
|
|
* License along with this library; if not, write to the Free Software *
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
|
|
|
|
* 02110-1301 USA *
|
|
|
|
* *
|
|
|
|
* Alternatively, this file is available under the Mozilla Public *
|
|
|
|
* License Version 1.1. You may obtain a copy of the License at *
|
|
|
|
* http://www.mozilla.org/MPL/ *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include <tstring.h>
|
|
|
|
#include <tdebug.h>
|
|
|
|
#include <bitset>
|
|
|
|
#include "id3v2tag.h"
|
|
|
|
#include "apeproperties.h"
|
|
|
|
#include "apefile.h"
|
2015-11-24 19:36:24 +01:00
|
|
|
#include "apetag.h"
|
|
|
|
#include "apefooter.h"
|
2012-10-28 02:12:18 +02:00
|
|
|
|
|
|
|
using namespace TagLib;
|
|
|
|
|
|
|
|
class APE::Properties::PropertiesPrivate
|
|
|
|
{
|
|
|
|
public:
|
2015-11-24 19:36:24 +01:00
|
|
|
PropertiesPrivate() :
|
2012-10-28 02:12:18 +02:00
|
|
|
length(0),
|
|
|
|
bitrate(0),
|
|
|
|
sampleRate(0),
|
|
|
|
channels(0),
|
|
|
|
version(0),
|
|
|
|
bitsPerSample(0),
|
2015-11-24 19:36:24 +01:00
|
|
|
sampleFrames(0) {}
|
2012-10-28 02:12:18 +02:00
|
|
|
|
|
|
|
int length;
|
|
|
|
int bitrate;
|
|
|
|
int sampleRate;
|
|
|
|
int channels;
|
|
|
|
int version;
|
|
|
|
int bitsPerSample;
|
2016-07-19 16:58:52 +02:00
|
|
|
unsigned int sampleFrames;
|
2012-10-28 02:12:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// public members
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
APE::Properties::Properties(File *, ReadStyle style) :
|
|
|
|
AudioProperties(style),
|
|
|
|
d(new PropertiesPrivate())
|
2012-10-28 02:12:18 +02:00
|
|
|
{
|
2015-11-24 19:36:24 +01:00
|
|
|
debug("APE::Properties::Properties() -- This constructor is no longer used.");
|
|
|
|
}
|
|
|
|
|
|
|
|
APE::Properties::Properties(File *file, long streamLength, ReadStyle style) :
|
|
|
|
AudioProperties(style),
|
|
|
|
d(new PropertiesPrivate())
|
|
|
|
{
|
|
|
|
read(file, streamLength);
|
2012-10-28 02:12:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
APE::Properties::~Properties()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
int APE::Properties::length() const
|
2015-11-24 19:36:24 +01:00
|
|
|
{
|
|
|
|
return lengthInSeconds();
|
|
|
|
}
|
|
|
|
|
|
|
|
int APE::Properties::lengthInSeconds() const
|
|
|
|
{
|
|
|
|
return d->length / 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
int APE::Properties::lengthInMilliseconds() const
|
2012-10-28 02:12:18 +02:00
|
|
|
{
|
|
|
|
return d->length;
|
|
|
|
}
|
|
|
|
|
|
|
|
int APE::Properties::bitrate() const
|
|
|
|
{
|
|
|
|
return d->bitrate;
|
|
|
|
}
|
|
|
|
|
|
|
|
int APE::Properties::sampleRate() const
|
|
|
|
{
|
|
|
|
return d->sampleRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
int APE::Properties::channels() const
|
|
|
|
{
|
|
|
|
return d->channels;
|
|
|
|
}
|
|
|
|
|
|
|
|
int APE::Properties::version() const
|
|
|
|
{
|
|
|
|
return d->version;
|
|
|
|
}
|
|
|
|
|
|
|
|
int APE::Properties::bitsPerSample() const
|
|
|
|
{
|
|
|
|
return d->bitsPerSample;
|
|
|
|
}
|
|
|
|
|
2016-07-19 16:58:52 +02:00
|
|
|
unsigned int APE::Properties::sampleFrames() const
|
2012-10-28 02:12:18 +02:00
|
|
|
{
|
|
|
|
return d->sampleFrames;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// private members
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
namespace
|
2012-10-28 02:12:18 +02:00
|
|
|
{
|
2016-07-19 16:58:52 +02:00
|
|
|
int headerVersion(const ByteVector &header)
|
2015-11-24 19:36:24 +01:00
|
|
|
{
|
|
|
|
if(header.size() < 6 || !header.startsWith("MAC "))
|
|
|
|
return -1;
|
2012-10-28 02:12:18 +02:00
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
return header.toUShort(4, false);
|
2012-10-28 02:12:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
void APE::Properties::read(File *file, long streamLength)
|
2012-10-28 02:12:18 +02:00
|
|
|
{
|
2015-11-24 19:36:24 +01:00
|
|
|
// First, we assume that the file pointer is set at the first descriptor.
|
|
|
|
long offset = file->tell();
|
|
|
|
int version = headerVersion(file->readBlock(6));
|
|
|
|
|
|
|
|
// Next, we look for the descriptor.
|
|
|
|
if(version < 0) {
|
|
|
|
offset = file->find("MAC ", offset);
|
|
|
|
file->seek(offset);
|
|
|
|
version = headerVersion(file->readBlock(6));
|
2012-10-28 02:12:18 +02:00
|
|
|
}
|
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
if(version < 0) {
|
|
|
|
debug("APE::Properties::read() -- APE descriptor not found");
|
|
|
|
return;
|
2012-10-28 02:12:18 +02:00
|
|
|
}
|
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
d->version = version;
|
2012-10-28 02:12:18 +02:00
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
if(d->version >= 3980)
|
|
|
|
analyzeCurrent(file);
|
|
|
|
else
|
|
|
|
analyzeOld(file);
|
2012-10-28 02:12:18 +02:00
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
if(d->sampleFrames > 0 && d->sampleRate > 0) {
|
|
|
|
const double length = d->sampleFrames * 1000.0 / d->sampleRate;
|
|
|
|
d->length = static_cast<int>(length + 0.5);
|
|
|
|
d->bitrate = static_cast<int>(streamLength * 8.0 / length + 0.5);
|
|
|
|
}
|
2012-10-28 02:12:18 +02:00
|
|
|
}
|
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
void APE::Properties::analyzeCurrent(File *file)
|
2012-10-28 02:12:18 +02:00
|
|
|
{
|
|
|
|
// Read the descriptor
|
2015-11-24 19:36:24 +01:00
|
|
|
file->seek(2, File::Current);
|
|
|
|
const ByteVector descriptor = file->readBlock(44);
|
|
|
|
if(descriptor.size() < 44) {
|
|
|
|
debug("APE::Properties::analyzeCurrent() -- descriptor is too short.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-19 16:58:52 +02:00
|
|
|
const unsigned int descriptorBytes = descriptor.toUInt(0, false);
|
2012-10-28 02:12:18 +02:00
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
if((descriptorBytes - 52) > 0)
|
|
|
|
file->seek(descriptorBytes - 52, File::Current);
|
2012-10-28 02:12:18 +02:00
|
|
|
|
|
|
|
// Read the header
|
2015-11-24 19:36:24 +01:00
|
|
|
const ByteVector header = file->readBlock(24);
|
|
|
|
if(header.size() < 24) {
|
|
|
|
debug("APE::Properties::analyzeCurrent() -- MAC header is too short.");
|
|
|
|
return;
|
|
|
|
}
|
2012-10-28 02:12:18 +02:00
|
|
|
|
|
|
|
// Get the APE info
|
2013-08-18 04:39:33 +02:00
|
|
|
d->channels = header.toShort(18, false);
|
|
|
|
d->sampleRate = header.toUInt(20, false);
|
|
|
|
d->bitsPerSample = header.toShort(16, false);
|
2012-10-28 02:12:18 +02:00
|
|
|
|
2016-07-19 16:58:52 +02:00
|
|
|
const unsigned int totalFrames = header.toUInt(12, false);
|
2015-11-24 19:36:24 +01:00
|
|
|
if(totalFrames == 0)
|
|
|
|
return;
|
|
|
|
|
2016-07-19 16:58:52 +02:00
|
|
|
const unsigned int blocksPerFrame = header.toUInt(4, false);
|
|
|
|
const unsigned int finalFrameBlocks = header.toUInt(8, false);
|
2015-11-24 19:36:24 +01:00
|
|
|
d->sampleFrames = (totalFrames - 1) * blocksPerFrame + finalFrameBlocks;
|
2012-10-28 02:12:18 +02:00
|
|
|
}
|
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
void APE::Properties::analyzeOld(File *file)
|
2012-10-28 02:12:18 +02:00
|
|
|
{
|
2015-11-24 19:36:24 +01:00
|
|
|
const ByteVector header = file->readBlock(26);
|
|
|
|
if(header.size() < 26) {
|
|
|
|
debug("APE::Properties::analyzeOld() -- MAC header is too short.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-19 16:58:52 +02:00
|
|
|
const unsigned int totalFrames = header.toUInt(18, false);
|
2012-10-28 02:12:18 +02:00
|
|
|
|
|
|
|
// Fail on 0 length APE files (catches non-finalized APE files)
|
|
|
|
if(totalFrames == 0)
|
|
|
|
return;
|
|
|
|
|
2013-08-18 04:39:33 +02:00
|
|
|
const short compressionLevel = header.toShort(0, false);
|
2016-07-19 16:58:52 +02:00
|
|
|
unsigned int blocksPerFrame;
|
2012-10-28 02:12:18 +02:00
|
|
|
if(d->version >= 3950)
|
|
|
|
blocksPerFrame = 73728 * 4;
|
|
|
|
else if(d->version >= 3900 || (d->version >= 3800 && compressionLevel == 4000))
|
|
|
|
blocksPerFrame = 73728;
|
|
|
|
else
|
|
|
|
blocksPerFrame = 9216;
|
2015-11-24 19:36:24 +01:00
|
|
|
|
|
|
|
// Get the APE info
|
2013-08-18 04:39:33 +02:00
|
|
|
d->channels = header.toShort(4, false);
|
|
|
|
d->sampleRate = header.toUInt(6, false);
|
2015-11-24 19:36:24 +01:00
|
|
|
|
2016-07-19 16:58:52 +02:00
|
|
|
const unsigned int finalFrameBlocks = header.toUInt(22, false);
|
2015-11-24 19:36:24 +01:00
|
|
|
d->sampleFrames = (totalFrames - 1) * blocksPerFrame + finalFrameBlocks;
|
2012-10-28 02:12:18 +02:00
|
|
|
|
2015-11-24 19:36:24 +01:00
|
|
|
// Get the bit depth from the RIFF-fmt chunk.
|
|
|
|
file->seek(16, File::Current);
|
|
|
|
const ByteVector fmt = file->readBlock(28);
|
|
|
|
if(fmt.size() < 28 || !fmt.startsWith("WAVEfmt ")) {
|
|
|
|
debug("APE::Properties::analyzeOld() -- fmt header is too short.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->bitsPerSample = fmt.toShort(26, false);
|
|
|
|
}
|