Permalink
Browse files

Work-in-progress support for writing FLAC picture

This will enable the possibility to add support for accessing the Seektable
and Cuesheet blocks, but I'm not planning on implementing that right now.

CCBUG:218696


git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@1201717 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
  • Loading branch information...
1 parent d21d8f9 commit 5c930a6a8c99a9eebeefb9fb34c856fe241c1f46 @lalinsky lalinsky committed Nov 28, 2010
View
@@ -95,6 +95,9 @@ SET(flacs_SRCS
flac/flacfile.cpp
flac/flacpicture.cpp
flac/flacproperties.cpp
+flac/flacmetadatablock.cpp
+flac/flacmetadatablocks.cpp
+flac/flacunknownmetadatablock.cpp
)
SET(oggflacs_SRCS
View
@@ -36,6 +36,7 @@
#include "flacpicture.h"
#include "flacfile.h"
+#include "flacmetadatablock.h"
using namespace TagLib;
@@ -0,0 +1,51 @@
+/**************************************************************************
+ copyright : (C) 2010 by Lukáš Lalinský
+ email : lalinsky@gmail.com
+ **************************************************************************/
+
+/***************************************************************************
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
+ * 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/ *
+ ***************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <taglib.h>
+#include <tdebug.h>
+#include "flacmetadatablock.h"
+
+using namespace TagLib;
+
+class FLAC::MetadataBlock::MetadataBlockPrivate
+{
+public:
+ MetadataBlockPrivate() {}
+
+};
+
+FLAC::MetadataBlock::MetadataBlock()
+{
+ d = 0;
+}
+
+FLAC::MetadataBlock::~MetadataBlock()
+{
+}
+
@@ -0,0 +1,75 @@
+/**************************************************************************
+ copyright : (C) 2010 by Lukáš Lalinský
+ email : lalinsky@gmail.com
+ **************************************************************************/
+
+/***************************************************************************
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
+ * 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/ *
+ ***************************************************************************/
+
+#ifndef TAGLIB_FLACMETADATABLOCK_H
+#define TAGLIB_FLACMETADATABLOCK_H
+
+#include "tlist.h"
+#include "tbytevector.h"
+#include "taglib_export.h"
+
+namespace TagLib {
+
+ namespace FLAC {
+
+ class TAGLIB_EXPORT MetadataBlock
+ {
+ public:
+ MetadataBlock();
+ virtual ~MetadataBlock();
+
+ enum BlockType {
+ StreamInfo = 0,
+ Padding,
+ Application,
+ SeekTable,
+ VorbisComment,
+ CueSheet,
+ Picture
+ };
+
+ /*!
+ * Returns the FLAC metadata block type.
+ */
+ virtual int code() const = 0;
+
+ /*!
+ * Render the content of the block.
+ */
+ virtual ByteVector render() const = 0;
+
+ private:
+ MetadataBlock(const MetadataBlock &item);
+ MetadataBlock &operator=(const MetadataBlock &item);
+
+ class MetadataBlockPrivate;
+ MetadataBlockPrivate *d;
+ };
+
+ }
+
+}
+
+#endif
@@ -0,0 +1,111 @@
+/**************************************************************************
+ copyright : (C) 2010 by Lukáš Lalinský
+ email : lalinsky@gmail.com
+ **************************************************************************/
+
+/***************************************************************************
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
+ * 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/ *
+ ***************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <taglib.h>
+#include <tdebug.h>
+#include "flacfile.h"
+#include "flacunknownmetadatablock.h"
+#include "flacmetadatablock.h"
+#include "flacmetadatablocks.h"
+
+using namespace TagLib;
+
+class FLAC::MetadataBlocks::MetadataBlocksPrivate
+{
+public:
+ MetadataBlocksPrivate() {}
+
+ List<MetadataBlock *> blocks;
+};
+
+FLAC::MetadataBlocks::MetadataBlocks()
+{
+ d = new MetadataBlocksPrivate();
+}
+
+FLAC::MetadataBlocks::~MetadataBlocks()
+{
+ delete d;
+}
+
+const List<FLAC::MetadataBlock *> &FLAC::MetadataBlocks::metadataBlockList() const
+{
+ return d->blocks;
+}
+
+bool FLAC::MetadataBlocks::read(FLAC::File *file)
+{
+ bool isLastBlock = false;
+ while(!isLastBlock) {
+ ByteVector header = file->readBlock(4);
+ if(header.size() != 4) {
+ debug("FLAC::MetadataBlocks::read -- Unable to read 4 bytes long header");
+ return false;
+ }
+ char blockType = header[0] & 0x7f;
+ isLastBlock = (header[0] & 0x80) != 0;
+ uint length = header.mid(1, 3).toUInt();
+ ByteVector data = file->readBlock(length);
+ if(data.size() != length) {
+ debug("FLAC::MetadataBlocks::read -- Unable to read " + String::number(length) + " bytes long block body");
+ return false;
+ }
+ if(blockType != FLAC::MetadataBlock::Padding) {
+ FLAC::MetadataBlock *block = new FLAC::UnknownMetadataBlock(blockType, data);
+ d->blocks.append(block);
+ }
+ }
+ return true;
+}
+
+ByteVector FLAC::MetadataBlocks::render(int originalLength) const
+{
+ ByteVector result;
+ for(uint i = 0; i < d->blocks.size(); i++) {
+ FLAC::MetadataBlock *block = d->blocks[i];
+ if(block->code() == FLAC::MetadataBlock::Padding)
+ continue;
+ ByteVector data = block->render();
+ ByteVector header = ByteVector::fromUInt(data.size());
+ header[0] = block->code();
+ result.append(header);
+ result.append(data);
+ }
+ int paddingLength = originalLength - result.size() - 4;
+ // We have to resize the file, add some padding
+ if (paddingLength < 0) {
+ paddingLength = 4096;
+ }
+ ByteVector padding = ByteVector::fromUInt(paddingLength);
+ padding.resize(paddingLength + 4);
+ padding[0] = FLAC::MetadataBlock::Padding | 0x80;
+ result.append(padding);
+ return result;
+}
+
@@ -0,0 +1,69 @@
+/**************************************************************************
+ copyright : (C) 2010 by Lukáš Lalinský
+ email : lalinsky@gmail.com
+ **************************************************************************/
+
+/***************************************************************************
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
+ * 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/ *
+ ***************************************************************************/
+
+#ifndef TAGLIB_FLACMETADATABLOCKS_H
+#define TAGLIB_FLACMETADATABLOCKS_H
+
+#include "tlist.h"
+#include "tbytevector.h"
+#include "taglib_export.h"
+
+namespace TagLib {
+
+ namespace FLAC {
+
+ class File;
+
+ class TAGLIB_EXPORT MetadataBlocks
+ {
+ public:
+ MetadataBlocks();
+ virtual ~MetadataBlocks();
+
+ /*!
+ * Read the blocks from a file.
+ */
+ bool read(File *file);
+
+ /*!
+ * Render the blocks into a byte vector.
+ */
+ ByteVector render(int originalLength) const;
+
+ const List<MetadataBlock *> &metadataBlockList() const;
+
+ private:
+ MetadataBlocks(const MetadataBlocks &item);
+ MetadataBlocks &operator=(const MetadataBlocks &item);
+
+ class MetadataBlocksPrivate;
+ MetadataBlocksPrivate *d;
+ };
+
+ }
+
+}
+
+#endif
@@ -70,6 +70,11 @@ FLAC::Picture::~Picture()
delete d;
}
+int FLAC::Picture::code() const
+{
+ return FLAC::MetadataBlock::Picture;
+}
+
bool FLAC::Picture::parse(const ByteVector &data)
{
if(data.size() < 32) {
@@ -115,6 +120,25 @@ bool FLAC::Picture::parse(const ByteVector &data)
return true;
}
+ByteVector FLAC::Picture::render() const
+{
+ ByteVector result;
+ result.append(ByteVector::fromUInt(d->type));
+ ByteVector mimeTypeData = d->mimeType.data(String::UTF8);
+ result.append(ByteVector::fromUInt(mimeTypeData.size()));
+ result.append(mimeTypeData);
+ ByteVector descriptionData = d->description.data(String::UTF8);
+ result.append(ByteVector::fromUInt(descriptionData.size()));
+ result.append(descriptionData);
+ result.append(ByteVector::fromUInt(d->width));
+ result.append(ByteVector::fromUInt(d->height));
+ result.append(ByteVector::fromUInt(d->colorDepth));
+ result.append(ByteVector::fromUInt(d->numColors));
+ result.append(ByteVector::fromUInt(d->data.size()));
+ result.append(d->data);
+ return result;
+}
+
FLAC::Picture::Type FLAC::Picture::type() const
{
return d->type;
View
@@ -30,12 +30,13 @@
#include "tbytevector.h"
#include "taglib_export.h"
#include "attachedpictureframe.h"
+#include "flacmetadatablock.h"
namespace TagLib {
namespace FLAC {
- class TAGLIB_EXPORT Picture
+ class TAGLIB_EXPORT Picture : public MetadataBlock
{
public:
typedef ID3v2::AttachedPictureFrame::Type Type;
@@ -128,6 +129,16 @@ namespace TagLib {
*/
void setData(const ByteVector &data);
+ /*!
+ * Returns the FLAC metadata block type.
+ */
+ int code() const;
+
+ /*!
+ * Render the content of the block.
+ */
+ ByteVector render() const;
+
bool parse(const ByteVector &rawData);
private:
Oops, something went wrong.

0 comments on commit 5c930a6

Please sign in to comment.