Clementine-audio-player-Mac.../3rdparty/taglib/flac/flacfile.cpp

576 lines
14 KiB
C++
Raw Normal View History

2012-10-28 02:12:18 +02:00
/***************************************************************************
copyright : (C) 2003-2004 by Allan Sandfeld Jensen
email : kde@carewolf.org
***************************************************************************/
/***************************************************************************
* 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 <tbytevector.h>
#include <tstring.h>
#include <tlist.h>
#include <tdebug.h>
#include <tagunion.h>
#include <tpropertymap.h>
2016-07-19 16:58:52 +02:00
#include <tagutils.h>
2012-10-28 02:12:18 +02:00
#include <id3v2header.h>
#include <id3v2tag.h>
#include <id3v1tag.h>
#include <xiphcomment.h>
#include "flacpicture.h"
#include "flacfile.h"
#include "flacmetadatablock.h"
#include "flacunknownmetadatablock.h"
using namespace TagLib;
namespace
{
2016-07-19 16:58:52 +02:00
typedef List<FLAC::MetadataBlock *> BlockList;
typedef BlockList::Iterator BlockIterator;
typedef BlockList::Iterator BlockConstIterator;
2012-10-28 02:12:18 +02:00
enum { FlacXiphIndex = 0, FlacID3v2Index = 1, FlacID3v1Index = 2 };
2016-07-19 16:58:52 +02:00
const long MinPaddingLength = 4096;
const long MaxPaddingLegnth = 1024 * 1024;
const char LastBlockFlag = '\x80';
2012-10-28 02:12:18 +02:00
}
class FLAC::File::FilePrivate
{
public:
2016-07-19 16:58:52 +02:00
FilePrivate(const ID3v2::FrameFactory *frameFactory = ID3v2::FrameFactory::instance()) :
ID3v2FrameFactory(frameFactory),
2012-10-28 02:12:18 +02:00
ID3v2Location(-1),
ID3v2OriginalSize(0),
ID3v1Location(-1),
properties(0),
flacStart(0),
streamStart(0),
2016-07-19 16:58:52 +02:00
scanned(false)
2012-10-28 02:12:18 +02:00
{
2016-07-19 16:58:52 +02:00
blocks.setAutoDelete(true);
2012-10-28 02:12:18 +02:00
}
~FilePrivate()
{
delete properties;
}
const ID3v2::FrameFactory *ID3v2FrameFactory;
long ID3v2Location;
2016-07-19 16:58:52 +02:00
long ID3v2OriginalSize;
2012-10-28 02:12:18 +02:00
long ID3v1Location;
TagUnion tag;
Properties *properties;
ByteVector xiphCommentData;
2016-07-19 16:58:52 +02:00
BlockList blocks;
2012-10-28 02:12:18 +02:00
long flacStart;
long streamStart;
bool scanned;
};
2018-06-06 22:47:08 +02:00
////////////////////////////////////////////////////////////////////////////////
// static members
////////////////////////////////////////////////////////////////////////////////
bool FLAC::File::isSupported(IOStream *stream)
{
// A FLAC file has an ID "fLaC" somewhere. An ID3v2 tag may precede.
const ByteVector buffer = Utils::readHeader(stream, bufferSize(), true);
return (buffer.find("fLaC") >= 0);
}
2012-10-28 02:12:18 +02:00
////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////
2015-11-24 19:36:24 +01:00
FLAC::File::File(FileName file, bool readProperties, Properties::ReadStyle) :
TagLib::File(file),
d(new FilePrivate())
2012-10-28 02:12:18 +02:00
{
if(isOpen())
2015-11-24 19:36:24 +01:00
read(readProperties);
2012-10-28 02:12:18 +02:00
}
FLAC::File::File(FileName file, ID3v2::FrameFactory *frameFactory,
2015-11-24 19:36:24 +01:00
bool readProperties, Properties::ReadStyle) :
TagLib::File(file),
2016-07-19 16:58:52 +02:00
d(new FilePrivate(frameFactory))
2012-10-28 02:12:18 +02:00
{
if(isOpen())
2015-11-24 19:36:24 +01:00
read(readProperties);
2012-10-28 02:12:18 +02:00
}
FLAC::File::File(IOStream *stream, ID3v2::FrameFactory *frameFactory,
2015-11-24 19:36:24 +01:00
bool readProperties, Properties::ReadStyle) :
TagLib::File(stream),
2016-07-19 16:58:52 +02:00
d(new FilePrivate(frameFactory))
2012-10-28 02:12:18 +02:00
{
if(isOpen())
2015-11-24 19:36:24 +01:00
read(readProperties);
2012-10-28 02:12:18 +02:00
}
FLAC::File::~File()
{
delete d;
}
TagLib::Tag *FLAC::File::tag() const
{
return &d->tag;
}
PropertyMap FLAC::File::properties() const
{
2016-07-19 16:58:52 +02:00
return d->tag.properties();
2012-10-28 02:12:18 +02:00
}
void FLAC::File::removeUnsupportedProperties(const StringList &unsupported)
{
2016-07-19 16:58:52 +02:00
d->tag.removeUnsupportedProperties(unsupported);
2012-10-28 02:12:18 +02:00
}
PropertyMap FLAC::File::setProperties(const PropertyMap &properties)
{
2016-07-19 16:58:52 +02:00
return xiphComment(true)->setProperties(properties);
2012-10-28 02:12:18 +02:00
}
FLAC::Properties *FLAC::File::audioProperties() const
{
return d->properties;
}
bool FLAC::File::save()
{
if(readOnly()) {
debug("FLAC::File::save() - Cannot save to a read only file.");
return false;
}
if(!isValid()) {
debug("FLAC::File::save() -- Trying to save invalid file.");
return false;
}
// Create new vorbis comments
2018-06-06 22:47:08 +02:00
if(!hasXiphComment())
Tag::duplicate(&d->tag, xiphComment(true), false);
2012-10-28 02:12:18 +02:00
d->xiphCommentData = xiphComment()->render(false);
// Replace metadata blocks
2016-07-19 16:58:52 +02:00
for(BlockIterator it = d->blocks.begin(); it != d->blocks.end(); ++it) {
if((*it)->code() == MetadataBlock::VorbisComment) {
2012-10-28 02:12:18 +02:00
// Set the new Vorbis Comment block
2016-07-19 16:58:52 +02:00
delete *it;
d->blocks.erase(it);
break;
2012-10-28 02:12:18 +02:00
}
}
2016-07-19 16:58:52 +02:00
d->blocks.append(new UnknownMetadataBlock(MetadataBlock::VorbisComment, d->xiphCommentData));
2012-10-28 02:12:18 +02:00
// Render data for the metadata blocks
ByteVector data;
2016-07-19 16:58:52 +02:00
for(BlockConstIterator it = d->blocks.begin(); it != d->blocks.end(); ++it) {
ByteVector blockData = (*it)->render();
2012-10-28 02:12:18 +02:00
ByteVector blockHeader = ByteVector::fromUInt(blockData.size());
2016-07-19 16:58:52 +02:00
blockHeader[0] = (*it)->code();
2012-10-28 02:12:18 +02:00
data.append(blockHeader);
data.append(blockData);
}
2016-07-19 16:58:52 +02:00
// Compute the amount of padding, and append that to data.
2012-10-28 02:12:18 +02:00
long originalLength = d->streamStart - d->flacStart;
2016-07-19 16:58:52 +02:00
long paddingLength = originalLength - data.size() - 4;
2015-11-24 19:36:24 +01:00
if(paddingLength <= 0) {
2012-10-28 02:12:18 +02:00
paddingLength = MinPaddingLength;
}
2016-07-19 16:58:52 +02:00
else {
// Padding won't increase beyond 1% of the file size or 1MB.
long threshold = length() / 100;
threshold = std::max(threshold, MinPaddingLength);
threshold = std::min(threshold, MaxPaddingLegnth);
if(paddingLength > threshold)
paddingLength = MinPaddingLength;
}
ByteVector paddingHeader = ByteVector::fromUInt(paddingLength);
paddingHeader[0] = static_cast<char>(MetadataBlock::Padding | LastBlockFlag);
data.append(paddingHeader);
data.resize(static_cast<unsigned int>(data.size() + paddingLength));
2012-10-28 02:12:18 +02:00
// Write the data to the file
insert(data, d->flacStart, originalLength);
2016-07-19 16:58:52 +02:00
d->streamStart += (static_cast<long>(data.size()) - originalLength);
if(d->ID3v1Location >= 0)
d->ID3v1Location += (static_cast<long>(data.size()) - originalLength);
2012-10-28 02:12:18 +02:00
// Update ID3 tags
2016-07-19 16:58:52 +02:00
if(ID3v2Tag() && !ID3v2Tag()->isEmpty()) {
// ID3v2 tag is not empty. Update the old one or create a new one.
if(d->ID3v2Location < 0)
d->ID3v2Location = 0;
data = ID3v2Tag()->render();
insert(data, d->ID3v2Location, d->ID3v2OriginalSize);
d->flacStart += (static_cast<long>(data.size()) - d->ID3v2OriginalSize);
d->streamStart += (static_cast<long>(data.size()) - d->ID3v2OriginalSize);
if(d->ID3v1Location >= 0)
d->ID3v1Location += (static_cast<long>(data.size()) - d->ID3v2OriginalSize);
d->ID3v2OriginalSize = data.size();
}
else {
// ID3v2 tag is empty. Remove the old one.
if(d->ID3v2Location >= 0) {
removeBlock(d->ID3v2Location, d->ID3v2OriginalSize);
d->flacStart -= d->ID3v2OriginalSize;
d->streamStart -= d->ID3v2OriginalSize;
if(d->ID3v1Location >= 0)
d->ID3v1Location -= d->ID3v2OriginalSize;
d->ID3v2Location = -1;
d->ID3v2OriginalSize = 0;
2012-10-28 02:12:18 +02:00
}
}
2016-07-19 16:58:52 +02:00
if(ID3v1Tag() && !ID3v1Tag()->isEmpty()) {
// ID3v1 tag is not empty. Update the old one or create a new one.
if(d->ID3v1Location >= 0) {
2015-11-24 19:36:24 +01:00
seek(d->ID3v1Location);
}
else {
seek(0, End);
d->ID3v1Location = tell();
}
2012-10-28 02:12:18 +02:00
writeBlock(ID3v1Tag()->render());
2016-07-19 16:58:52 +02:00
}
else {
// ID3v1 tag is empty. Remove the old one.
if(d->ID3v1Location >= 0) {
truncate(d->ID3v1Location);
d->ID3v1Location = -1;
}
2012-10-28 02:12:18 +02:00
}
return true;
}
ID3v2::Tag *FLAC::File::ID3v2Tag(bool create)
{
2018-06-06 22:47:08 +02:00
return d->tag.access<ID3v2::Tag>(FlacID3v2Index, create);
2012-10-28 02:12:18 +02:00
}
ID3v1::Tag *FLAC::File::ID3v1Tag(bool create)
{
return d->tag.access<ID3v1::Tag>(FlacID3v1Index, create);
}
Ogg::XiphComment *FLAC::File::xiphComment(bool create)
{
return d->tag.access<Ogg::XiphComment>(FlacXiphIndex, create);
}
void FLAC::File::setID3v2FrameFactory(const ID3v2::FrameFactory *factory)
{
d->ID3v2FrameFactory = factory;
}
2015-11-24 19:36:24 +01:00
ByteVector FLAC::File::streamInfoData()
{
debug("FLAC::File::streamInfoData() -- This function is obsolete. Returning an empty ByteVector.");
return ByteVector();
}
long FLAC::File::streamLength()
{
debug("FLAC::File::streamLength() -- This function is obsolete. Returning zero.");
return 0;
}
List<FLAC::Picture *> FLAC::File::pictureList()
{
List<Picture *> pictures;
2016-07-19 16:58:52 +02:00
for(BlockConstIterator it = d->blocks.begin(); it != d->blocks.end(); ++it) {
Picture *picture = dynamic_cast<Picture *>(*it);
2015-11-24 19:36:24 +01:00
if(picture) {
pictures.append(picture);
}
}
return pictures;
}
void FLAC::File::addPicture(Picture *picture)
{
d->blocks.append(picture);
}
void FLAC::File::removePicture(Picture *picture, bool del)
{
2016-07-19 16:58:52 +02:00
BlockIterator it = d->blocks.find(picture);
2015-11-24 19:36:24 +01:00
if(it != d->blocks.end())
d->blocks.erase(it);
if(del)
delete picture;
}
void FLAC::File::removePictures()
{
2016-07-19 16:58:52 +02:00
for(BlockIterator it = d->blocks.begin(); it != d->blocks.end(); ) {
if(dynamic_cast<Picture *>(*it)) {
delete *it;
it = d->blocks.erase(it);
2015-11-24 19:36:24 +01:00
}
else {
2016-07-19 16:58:52 +02:00
++it;
2015-11-24 19:36:24 +01:00
}
}
2016-07-19 16:58:52 +02:00
}
void FLAC::File::strip(int tags)
{
if(tags & ID3v1)
d->tag.set(FlacID3v1Index, 0);
if(tags & ID3v2)
d->tag.set(FlacID3v2Index, 0);
if(tags & XiphComment) {
xiphComment()->removeAllFields();
xiphComment()->removeAllPictures();
}
2015-11-24 19:36:24 +01:00
}
bool FLAC::File::hasXiphComment() const
{
2016-07-19 16:58:52 +02:00
return !d->xiphCommentData.isEmpty();
2015-11-24 19:36:24 +01:00
}
bool FLAC::File::hasID3v1Tag() const
{
2016-07-19 16:58:52 +02:00
return (d->ID3v1Location >= 0);
2015-11-24 19:36:24 +01:00
}
bool FLAC::File::hasID3v2Tag() const
{
2016-07-19 16:58:52 +02:00
return (d->ID3v2Location >= 0);
2015-11-24 19:36:24 +01:00
}
2012-10-28 02:12:18 +02:00
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////
2015-11-24 19:36:24 +01:00
void FLAC::File::read(bool readProperties)
2012-10-28 02:12:18 +02:00
{
// Look for an ID3v2 tag
2016-07-19 16:58:52 +02:00
d->ID3v2Location = Utils::findID3v2(this);
2012-10-28 02:12:18 +02:00
if(d->ID3v2Location >= 0) {
d->tag.set(FlacID3v2Index, new ID3v2::Tag(this, d->ID3v2Location, d->ID3v2FrameFactory));
d->ID3v2OriginalSize = ID3v2Tag()->header()->completeTagSize();
}
// Look for an ID3v1 tag
2016-07-19 16:58:52 +02:00
d->ID3v1Location = Utils::findID3v1(this);
2012-10-28 02:12:18 +02:00
2016-07-19 16:58:52 +02:00
if(d->ID3v1Location >= 0)
2012-10-28 02:12:18 +02:00
d->tag.set(FlacID3v1Index, new ID3v1::Tag(this, d->ID3v1Location));
// Look for FLAC metadata, including vorbis comments
scan();
if(!isValid())
return;
2016-07-19 16:58:52 +02:00
if(!d->xiphCommentData.isEmpty())
2015-11-24 19:36:24 +01:00
d->tag.set(FlacXiphIndex, new Ogg::XiphComment(d->xiphCommentData));
2012-10-28 02:12:18 +02:00
else
2016-07-19 16:58:52 +02:00
d->tag.set(FlacXiphIndex, new Ogg::XiphComment());
2012-10-28 02:12:18 +02:00
2015-11-24 19:36:24 +01:00
if(readProperties) {
2012-10-28 02:12:18 +02:00
2015-11-24 19:36:24 +01:00
// First block should be the stream_info metadata
2012-10-28 02:12:18 +02:00
2015-11-24 19:36:24 +01:00
const ByteVector infoData = d->blocks.front()->render();
2012-10-28 02:12:18 +02:00
2015-11-24 19:36:24 +01:00
long streamLength;
2016-07-19 16:58:52 +02:00
if(d->ID3v1Location >= 0)
2015-11-24 19:36:24 +01:00
streamLength = d->ID3v1Location - d->streamStart;
else
2016-07-19 16:58:52 +02:00
streamLength = length() - d->streamStart;
2015-11-24 19:36:24 +01:00
d->properties = new Properties(infoData, streamLength);
}
2012-10-28 02:12:18 +02:00
}
void FLAC::File::scan()
{
// Scan the metadata pages
if(d->scanned)
return;
if(!isValid())
return;
long nextBlockOffset;
2016-07-19 16:58:52 +02:00
if(d->ID3v2Location >= 0)
2012-10-28 02:12:18 +02:00
nextBlockOffset = find("fLaC", d->ID3v2Location + d->ID3v2OriginalSize);
else
nextBlockOffset = find("fLaC");
if(nextBlockOffset < 0) {
debug("FLAC::File::scan() -- FLAC stream not found");
setValid(false);
return;
}
nextBlockOffset += 4;
d->flacStart = nextBlockOffset;
2016-07-19 16:58:52 +02:00
while(true) {
2012-10-28 02:12:18 +02:00
2016-07-19 16:58:52 +02:00
seek(nextBlockOffset);
const ByteVector header = readBlock(4);
// Header format (from spec):
// <1> Last-metadata-block flag
// <7> BLOCK_TYPE
// 0 : STREAMINFO
// 1 : PADDING
// ..
// 4 : VORBIS_COMMENT
// ..
// 6 : PICTURE
// ..
// <24> Length of metadata to follow
const char blockType = header[0] & ~LastBlockFlag;
const bool isLastBlock = (header[0] & LastBlockFlag) != 0;
const unsigned int blockLength = header.toUInt(1U, 3U);
2012-10-28 02:12:18 +02:00
2016-07-19 16:58:52 +02:00
// First block should be the stream_info metadata
2012-10-28 02:12:18 +02:00
2016-07-19 16:58:52 +02:00
if(d->blocks.isEmpty() && blockType != MetadataBlock::StreamInfo) {
debug("FLAC::File::scan() -- First block should be the stream_info metadata");
setValid(false);
return;
}
2012-10-28 02:12:18 +02:00
2018-06-06 22:47:08 +02:00
if(blockLength == 0
&& blockType != MetadataBlock::Padding && blockType != MetadataBlock::SeekTable)
{
2015-11-24 19:36:24 +01:00
debug("FLAC::File::scan() -- Zero-sized metadata block found");
setValid(false);
return;
}
2016-07-19 16:58:52 +02:00
const ByteVector data = readBlock(blockLength);
if(data.size() != blockLength) {
2015-11-24 19:36:24 +01:00
debug("FLAC::File::scan() -- Failed to read a metadata block");
2012-10-28 02:12:18 +02:00
setValid(false);
return;
}
MetadataBlock *block = 0;
// Found the vorbis-comment
if(blockType == MetadataBlock::VorbisComment) {
2016-07-19 16:58:52 +02:00
if(d->xiphCommentData.isEmpty()) {
2012-10-28 02:12:18 +02:00
d->xiphCommentData = data;
2016-07-19 16:58:52 +02:00
block = new UnknownMetadataBlock(MetadataBlock::VorbisComment, data);
2012-10-28 02:12:18 +02:00
}
else {
2016-07-19 16:58:52 +02:00
debug("FLAC::File::scan() -- multiple Vorbis Comment blocks found, discarding");
2012-10-28 02:12:18 +02:00
}
}
else if(blockType == MetadataBlock::Picture) {
FLAC::Picture *picture = new FLAC::Picture();
if(picture->parse(data)) {
block = picture;
}
else {
2015-11-24 19:36:24 +01:00
debug("FLAC::File::scan() -- invalid picture found, discarding");
2012-10-28 02:12:18 +02:00
delete picture;
}
}
2016-07-19 16:58:52 +02:00
else if(blockType == MetadataBlock::Padding) {
// Skip all padding blocks.
2012-10-28 02:12:18 +02:00
}
else {
2016-07-19 16:58:52 +02:00
block = new UnknownMetadataBlock(blockType, data);
2012-10-28 02:12:18 +02:00
}
2016-07-19 16:58:52 +02:00
if(block)
d->blocks.append(block);
2012-10-28 02:12:18 +02:00
2016-07-19 16:58:52 +02:00
nextBlockOffset += blockLength + 4;
if(isLastBlock)
break;
2012-10-28 02:12:18 +02:00
}
// End of metadata, now comes the datastream
d->streamStart = nextBlockOffset;
d->scanned = true;
}