summaryrefslogtreecommitdiff
path: root/src/tag
diff options
context:
space:
mode:
authorMax Kellermann <max@musicpd.org>2019-08-13 12:22:11 +0200
committerMax Kellermann <max@musicpd.org>2019-08-14 12:39:03 +0200
commit433e18b247450b1adfacd22c8964e9483934b8d5 (patch)
treeb98942905de0b69635ea65a20a41dd380a22f5de /src/tag
parent2b837277c1da5c3c9ed00610b126d0f4facf291e (diff)
decoder/{opus,vorbis}: support embedded pictures (METADATA_BLOCK_PICTURE)
More for https://github.com/MusicPlayerDaemon/MPD/issues/42
Diffstat (limited to 'src/tag')
-rw-r--r--src/tag/Id3Picture.cxx80
-rw-r--r--src/tag/Id3Picture.hxx32
-rw-r--r--src/tag/meson.build1
3 files changed, 113 insertions, 0 deletions
diff --git a/src/tag/Id3Picture.cxx b/src/tag/Id3Picture.cxx
new file mode 100644
index 000000000..7c4aa666f
--- /dev/null
+++ b/src/tag/Id3Picture.cxx
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2003-2019 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * 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.
+ *
+ * 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.
+ */
+
+#include "Id3Picture.hxx"
+#include "Handler.hxx"
+#include "util/ByteOrder.hxx"
+#include "util/ConstBuffer.hxx"
+#include "util/StringView.hxx"
+
+#include <string>
+
+#include <stdint.h>
+
+static StringView
+ReadString(ConstBuffer<uint8_t> &src) noexcept
+{
+ if (src.size < 4)
+ return nullptr;
+
+ const size_t length = FromBE32(*(const uint32_t *)src.data);
+ src.skip_front(4);
+
+ if (src.size < length)
+ return nullptr;
+
+ StringView result((const char *)src.data, length);
+ src.skip_front(length);
+ return result;
+}
+
+void
+ScanId3Apic(ConstBuffer<void> _buffer, TagHandler &handler) noexcept
+{
+ auto buffer = ConstBuffer<uint8_t>::FromVoid(_buffer);
+ if (buffer.size < 4)
+ return;
+
+ buffer.skip_front(4); /* picture type */
+
+ const auto mime_type = ReadString(buffer);
+ if (mime_type.IsNull())
+ return;
+
+ /* description */
+ if (ReadString(buffer).IsNull())
+ return;
+
+ if (buffer.size < 20)
+ return;
+
+ buffer.skip_front(16);
+
+ const size_t image_size = FromBE32(*(const uint32_t *)buffer.data);
+ buffer.skip_front(4);
+
+ if (buffer.size < image_size)
+ return;
+
+ ConstBuffer<void> image(buffer.data, image_size);
+
+ // TODO: don't copy MIME type, pass StringView to TagHandler::OnPicture()
+ handler.OnPicture(std::string(mime_type.data, mime_type.size).c_str(),
+ image);
+}
diff --git a/src/tag/Id3Picture.hxx b/src/tag/Id3Picture.hxx
new file mode 100644
index 000000000..950441672
--- /dev/null
+++ b/src/tag/Id3Picture.hxx
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2003-2019 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * 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.
+ *
+ * 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.
+ */
+
+#ifndef MPD_TAG_ID3_PICTURE_HXX
+#define MPD_TAG_ID3_PICTURE_HXX
+
+template<typename T> struct ConstBuffer;
+class TagHandler;
+
+/**
+ * Scan an "APIC" value and call TagHandler::OnPicture().
+ */
+void
+ScanId3Apic(ConstBuffer<void> buffer, TagHandler &handler) noexcept;
+
+#endif
diff --git a/src/tag/meson.build b/src/tag/meson.build
index 9767ba6ed..13e722f0b 100644
--- a/src/tag/meson.build
+++ b/src/tag/meson.build
@@ -15,6 +15,7 @@ tag_sources = [
'MixRamp.cxx',
'Generic.cxx',
'Id3MusicBrainz.cxx',
+ 'Id3Picture.cxx',
'ApeLoader.cxx',
'ApeReplayGain.cxx',
'ApeTag.cxx',