From 5101ef4b028c8f661801f78ba0fe73fab0e80f7f Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 4 Sep 2013 23:46:20 +0200 Subject: Tag*: move libtag.a sources to src/tag/ --- src/tag/ApeLoader.cxx | 115 +++++++++ src/tag/ApeLoader.hxx | 43 ++++ src/tag/ApeReplayGain.cxx | 78 +++++++ src/tag/ApeReplayGain.hxx | 30 +++ src/tag/ApeTag.cxx | 104 +++++++++ src/tag/ApeTag.hxx | 38 +++ src/tag/TagId3.cxx | 583 ++++++++++++++++++++++++++++++++++++++++++++++ src/tag/TagId3.hxx | 70 ++++++ src/tag/TagRva2.cxx | 150 ++++++++++++ src/tag/TagRva2.hxx | 37 +++ src/tag/aiff.c | 106 +++++++++ src/tag/aiff.h | 41 ++++ src/tag/riff.c | 104 +++++++++ src/tag/riff.h | 41 ++++ 14 files changed, 1540 insertions(+) create mode 100644 src/tag/ApeLoader.cxx create mode 100644 src/tag/ApeLoader.hxx create mode 100644 src/tag/ApeReplayGain.cxx create mode 100644 src/tag/ApeReplayGain.hxx create mode 100644 src/tag/ApeTag.cxx create mode 100644 src/tag/ApeTag.hxx create mode 100644 src/tag/TagId3.cxx create mode 100644 src/tag/TagId3.hxx create mode 100644 src/tag/TagRva2.cxx create mode 100644 src/tag/TagRva2.hxx create mode 100644 src/tag/aiff.c create mode 100644 src/tag/aiff.h create mode 100644 src/tag/riff.c create mode 100644 src/tag/riff.h (limited to 'src/tag') diff --git a/src/tag/ApeLoader.cxx b/src/tag/ApeLoader.cxx new file mode 100644 index 000000000..dfbdb4ef3 --- /dev/null +++ b/src/tag/ApeLoader.cxx @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2003-2013 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 "config.h" +#include "ApeLoader.hxx" + +#include + +#include +#include +#include +#include + +struct ape_footer { + unsigned char id[8]; + uint32_t version; + uint32_t length; + uint32_t count; + unsigned char flags[4]; + unsigned char reserved[8]; +}; + +static bool +ape_scan_internal(FILE *fp, ApeTagCallback callback) +{ + /* determine if file has an apeV2 tag */ + struct ape_footer footer; + if (fseek(fp, -(long)sizeof(footer), SEEK_END) || + fread(&footer, 1, sizeof(footer), fp) != sizeof(footer) || + memcmp(footer.id, "APETAGEX", sizeof(footer.id)) != 0 || + GUINT32_FROM_LE(footer.version) != 2000) + return false; + + /* find beginning of ape tag */ + size_t remaining = GUINT32_FROM_LE(footer.length); + if (remaining <= sizeof(footer) + 10 || + /* refuse to load more than one megabyte of tag data */ + remaining > 1024 * 1024 || + fseek(fp, -(long)remaining, SEEK_END)) + return false; + + /* read tag into buffer */ + remaining -= sizeof(footer); + assert(remaining > 10); + + char *buffer = (char *)g_malloc(remaining); + if (fread(buffer, 1, remaining, fp) != remaining) { + g_free(buffer); + return false; + } + + /* read tags */ + unsigned n = GUINT32_FROM_LE(footer.count); + const char *p = buffer; + while (n-- && remaining > 10) { + size_t size = GUINT32_FROM_LE(*(const uint32_t *)p); + p += 4; + remaining -= 4; + unsigned long flags = GUINT32_FROM_LE(*(const uint32_t *)p); + p += 4; + remaining -= 4; + + /* get the key */ + const char *key = p; + while (remaining > size && *p != '\0') { + p++; + remaining--; + } + p++; + remaining--; + + /* get the value */ + if (remaining < size) + break; + + if (!callback(flags, key, p, size)) + break; + + p += size; + remaining -= size; + } + + g_free(buffer); + return true; +} + +bool +tag_ape_scan(const char *path_fs, ApeTagCallback callback) +{ + FILE *fp; + + fp = fopen(path_fs, "rb"); + if (fp == nullptr) + return false; + + bool success = ape_scan_internal(fp, callback); + fclose(fp); + return success; +} diff --git a/src/tag/ApeLoader.hxx b/src/tag/ApeLoader.hxx new file mode 100644 index 000000000..a32ab840c --- /dev/null +++ b/src/tag/ApeLoader.hxx @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2003-2013 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_APE_LOADER_HXX +#define MPD_APE_LOADER_HXX + +#include "check.h" + +#include + +#include + +typedef std::function ApeTagCallback; + +/** + * Scans the APE tag values from a file. + * + * @param path_fs the path of the file in filesystem encoding + * @return false if the file could not be opened or if no APE tag is + * present + */ +bool +tag_ape_scan(const char *path_fs, ApeTagCallback callback); + +#endif diff --git a/src/tag/ApeReplayGain.cxx b/src/tag/ApeReplayGain.cxx new file mode 100644 index 000000000..0135d1b61 --- /dev/null +++ b/src/tag/ApeReplayGain.cxx @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2003-2013 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 "config.h" +#include "ApeReplayGain.hxx" +#include "ApeLoader.hxx" +#include "replay_gain_info.h" + +#include + +#include +#include + +static bool +replay_gain_ape_callback(unsigned long flags, const char *key, + const char *_value, size_t value_length, + struct replay_gain_info *info) +{ + /* we only care about utf-8 text tags */ + if ((flags & (0x3 << 1)) != 0) + return false; + + char value[16]; + if (value_length >= sizeof(value)) + return false; + + memcpy(value, _value, value_length); + value[value_length] = 0; + + if (g_ascii_strcasecmp(key, "replaygain_track_gain") == 0) { + info->tuples[REPLAY_GAIN_TRACK].gain = atof(value); + return true; + } else if (g_ascii_strcasecmp(key, "replaygain_album_gain") == 0) { + info->tuples[REPLAY_GAIN_ALBUM].gain = atof(value); + return true; + } else if (g_ascii_strcasecmp(key, "replaygain_track_peak") == 0) { + info->tuples[REPLAY_GAIN_TRACK].peak = atof(value); + return true; + } else if (g_ascii_strcasecmp(key, "replaygain_album_peak") == 0) { + info->tuples[REPLAY_GAIN_ALBUM].peak = atof(value); + return true; + } else + return false; +} + +bool +replay_gain_ape_read(const char *path_fs, struct replay_gain_info *info) +{ + bool found = false; + + auto callback = [info, &found] + (unsigned long flags, const char *key, + const char *value, + size_t value_length) { + found |= replay_gain_ape_callback(flags, key, + value, value_length, + info); + return true; + }; + + return tag_ape_scan(path_fs, callback) && found; +} diff --git a/src/tag/ApeReplayGain.hxx b/src/tag/ApeReplayGain.hxx new file mode 100644 index 000000000..4bc34a9d2 --- /dev/null +++ b/src/tag/ApeReplayGain.hxx @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2003-2013 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_APE_REPLAY_GAIN_HXX +#define MPD_APE_REPLAY_GAIN_HXX + +#include "check.h" + +struct replay_gain_info; + +bool +replay_gain_ape_read(const char *path_fs, struct replay_gain_info *info); + +#endif diff --git a/src/tag/ApeTag.cxx b/src/tag/ApeTag.cxx new file mode 100644 index 000000000..34c2b703b --- /dev/null +++ b/src/tag/ApeTag.cxx @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2003-2013 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 "config.h" +#include "ApeTag.hxx" +#include "ApeLoader.hxx" +#include "Tag.hxx" +#include "TagTable.hxx" +#include "TagHandler.hxx" + +const struct tag_table ape_tags[] = { + { "album artist", TAG_ALBUM_ARTIST }, + { "year", TAG_DATE }, + { nullptr, TAG_NUM_OF_ITEM_TYPES } +}; + +static enum tag_type +tag_ape_name_parse(const char *name) +{ + enum tag_type type = tag_table_lookup_i(ape_tags, name); + if (type == TAG_NUM_OF_ITEM_TYPES) + type = tag_name_parse_i(name); + + return type; +} + +/** + * @return true if the item was recognized + */ +static bool +tag_ape_import_item(unsigned long flags, + const char *key, const char *value, size_t value_length, + const struct tag_handler *handler, void *handler_ctx) +{ + /* we only care about utf-8 text tags */ + if ((flags & (0x3 << 1)) != 0) + return false; + + tag_handler_invoke_pair(handler, handler_ctx, key, value); + + enum tag_type type = tag_ape_name_parse(key); + if (type == TAG_NUM_OF_ITEM_TYPES) + return false; + + bool recognized = false; + const char *end = value + value_length; + while (true) { + /* multiple values are separated by null bytes */ + const char *n = (const char *)memchr(value, 0, end - value); + if (n != nullptr) { + if (n > value) { + tag_handler_invoke_tag(handler, handler_ctx, + type, value); + recognized = true; + } + + value = n + 1; + } else { + char *p = g_strndup(value, end - value); + tag_handler_invoke_tag(handler, handler_ctx, + type, p); + g_free(p); + recognized = true; + break; + } + } + + return recognized; +} + +bool +tag_ape_scan2(const char *path_fs, + const struct tag_handler *handler, void *handler_ctx) +{ + bool recognized = false; + + auto callback = [handler, handler_ctx, &recognized] + (unsigned long flags, const char *key, + const char *value, + size_t value_length) { + recognized |= tag_ape_import_item(flags, key, value, + value_length, + handler, handler_ctx); + return true; + }; + + return tag_ape_scan(path_fs, callback) && recognized; +} diff --git a/src/tag/ApeTag.hxx b/src/tag/ApeTag.hxx new file mode 100644 index 000000000..1a7143314 --- /dev/null +++ b/src/tag/ApeTag.hxx @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2003-2013 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_APE_TAG_HXX +#define MPD_APE_TAG_HXX + +#include "TagTable.hxx" + +struct tag_handler; + +extern const struct tag_table ape_tags[]; + +/** + * Scan the APE tags of a file. + * + * @param path_fs the path of the file in filesystem encoding + */ +bool +tag_ape_scan2(const char *path_fs, + const struct tag_handler *handler, void *handler_ctx); + +#endif diff --git a/src/tag/TagId3.cxx b/src/tag/TagId3.cxx new file mode 100644 index 000000000..3f7605f5e --- /dev/null +++ b/src/tag/TagId3.cxx @@ -0,0 +1,583 @@ +/* + * Copyright (C) 2003-2013 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 "config.h" +#include "TagId3.hxx" +#include "TagHandler.hxx" +#include "TagTable.hxx" +#include "Tag.hxx" +#include "util/Error.hxx" + +extern "C" { +#include "riff.h" +#include "aiff.h" +} + +#include "conf.h" + +#include +#include + +#include +#include +#include +#include + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "id3" + +# ifndef ID3_FRAME_COMPOSER +# define ID3_FRAME_COMPOSER "TCOM" +# endif +# ifndef ID3_FRAME_DISC +# define ID3_FRAME_DISC "TPOS" +# endif + +#ifndef ID3_FRAME_ARTIST_SORT +#define ID3_FRAME_ARTIST_SORT "TSOP" +#endif + +#ifndef ID3_FRAME_ALBUM_ARTIST_SORT +#define ID3_FRAME_ALBUM_ARTIST_SORT "TSO2" /* this one is unofficial, introduced by Itunes */ +#endif + +#ifndef ID3_FRAME_ALBUM_ARTIST +#define ID3_FRAME_ALBUM_ARTIST "TPE2" +#endif + +static inline bool +tag_is_id3v1(struct id3_tag *tag) +{ + return (id3_tag_options(tag, 0, 0) & ID3_TAG_OPTION_ID3V1) != 0; +} + +static id3_utf8_t * +tag_id3_getstring(const struct id3_frame *frame, unsigned i) +{ + union id3_field *field; + const id3_ucs4_t *ucs4; + + field = id3_frame_field(frame, i); + if (field == nullptr) + return nullptr; + + ucs4 = id3_field_getstring(field); + if (ucs4 == nullptr) + return nullptr; + + return id3_ucs4_utf8duplicate(ucs4); +} + +/* This will try to convert a string to utf-8, + */ +static id3_utf8_t * +import_id3_string(bool is_id3v1, const id3_ucs4_t *ucs4) +{ + id3_utf8_t *utf8, *utf8_stripped; + id3_latin1_t *isostr; + const char *encoding; + + /* use encoding field here? */ + if (is_id3v1 && + (encoding = config_get_string(CONF_ID3V1_ENCODING, nullptr)) != nullptr) { + isostr = id3_ucs4_latin1duplicate(ucs4); + if (G_UNLIKELY(!isostr)) { + return nullptr; + } + + utf8 = (id3_utf8_t *) + g_convert_with_fallback((const char*)isostr, -1, + "utf-8", encoding, + nullptr, nullptr, + nullptr, nullptr); + if (utf8 == nullptr) { + g_debug("Unable to convert %s string to UTF-8: '%s'", + encoding, isostr); + g_free(isostr); + return nullptr; + } + g_free(isostr); + } else { + utf8 = id3_ucs4_utf8duplicate(ucs4); + if (G_UNLIKELY(!utf8)) { + return nullptr; + } + } + + utf8_stripped = (id3_utf8_t *)g_strdup(g_strstrip((gchar *)utf8)); + g_free(utf8); + + return utf8_stripped; +} + +/** + * Import a "Text information frame" (ID3v2.4.0 section 4.2). It + * contains 2 fields: + * + * - encoding + * - string list + */ +static void +tag_id3_import_text_frame(struct id3_tag *tag, const struct id3_frame *frame, + enum tag_type type, + const struct tag_handler *handler, void *handler_ctx) +{ + id3_ucs4_t const *ucs4; + id3_utf8_t *utf8; + union id3_field const *field; + unsigned int nstrings, i; + + if (frame->nfields != 2) + return; + + /* check the encoding field */ + + field = id3_frame_field(frame, 0); + if (field == nullptr || field->type != ID3_FIELD_TYPE_TEXTENCODING) + return; + + /* process the value(s) */ + + field = id3_frame_field(frame, 1); + if (field == nullptr || field->type != ID3_FIELD_TYPE_STRINGLIST) + return; + + /* Get the number of strings available */ + nstrings = id3_field_getnstrings(field); + for (i = 0; i < nstrings; i++) { + ucs4 = id3_field_getstrings(field, i); + if (ucs4 == nullptr) + continue; + + if (type == TAG_GENRE) + ucs4 = id3_genre_name(ucs4); + + utf8 = import_id3_string(tag_is_id3v1(tag), ucs4); + if (utf8 == nullptr) + continue; + + tag_handler_invoke_tag(handler, handler_ctx, + type, (const char *)utf8); + g_free(utf8); + } +} + +/** + * Import all text frames with the specified id (ID3v2.4.0 section + * 4.2). This is a wrapper for tag_id3_import_text_frame(). + */ +static void +tag_id3_import_text(struct id3_tag *tag, const char *id, enum tag_type type, + const struct tag_handler *handler, void *handler_ctx) +{ + const struct id3_frame *frame; + for (unsigned i = 0; + (frame = id3_tag_findframe(tag, id, i)) != nullptr; ++i) + tag_id3_import_text_frame(tag, frame, type, + handler, handler_ctx); +} + +/** + * Import a "Comment frame" (ID3v2.4.0 section 4.10). It + * contains 4 fields: + * + * - encoding + * - language + * - string + * - full string (we use this one) + */ +static void +tag_id3_import_comment_frame(struct id3_tag *tag, + const struct id3_frame *frame, enum tag_type type, + const struct tag_handler *handler, + void *handler_ctx) +{ + id3_ucs4_t const *ucs4; + id3_utf8_t *utf8; + union id3_field const *field; + + if (frame->nfields != 4) + return; + + /* for now I only read the 4th field, with the fullstring */ + field = id3_frame_field(frame, 3); + if (field == nullptr) + return; + + ucs4 = id3_field_getfullstring(field); + if (ucs4 == nullptr) + return; + + utf8 = import_id3_string(tag_is_id3v1(tag), ucs4); + if (utf8 == nullptr) + return; + + tag_handler_invoke_tag(handler, handler_ctx, type, (const char *)utf8); + g_free(utf8); +} + +/** + * Import all comment frames (ID3v2.4.0 section 4.10). This is a + * wrapper for tag_id3_import_comment_frame(). + */ +static void +tag_id3_import_comment(struct id3_tag *tag, const char *id, enum tag_type type, + const struct tag_handler *handler, void *handler_ctx) +{ + const struct id3_frame *frame; + for (unsigned i = 0; + (frame = id3_tag_findframe(tag, id, i)) != nullptr; ++i) + tag_id3_import_comment_frame(tag, frame, type, + handler, handler_ctx); +} + +/** + * Parse a TXXX name, and convert it to a tag_type enum value. + * Returns TAG_NUM_OF_ITEM_TYPES if the TXXX name is not understood. + */ +static enum tag_type +tag_id3_parse_txxx_name(const char *name) +{ + static const struct tag_table txxx_tags[] = { + { "ALBUMARTISTSORT", TAG_ALBUM_ARTIST_SORT }, + { "MusicBrainz Artist Id", TAG_MUSICBRAINZ_ARTISTID }, + { "MusicBrainz Album Id", TAG_MUSICBRAINZ_ALBUMID }, + { "MusicBrainz Album Artist Id", + TAG_MUSICBRAINZ_ALBUMARTISTID }, + { "MusicBrainz Track Id", TAG_MUSICBRAINZ_TRACKID }, + { nullptr, TAG_NUM_OF_ITEM_TYPES } + }; + + return tag_table_lookup(txxx_tags, name); +} + +/** + * Import all known MusicBrainz tags from TXXX frames. + */ +static void +tag_id3_import_musicbrainz(struct id3_tag *id3_tag, + const struct tag_handler *handler, + void *handler_ctx) +{ + for (unsigned i = 0;; ++i) { + const struct id3_frame *frame; + id3_utf8_t *name, *value; + enum tag_type type; + + frame = id3_tag_findframe(id3_tag, "TXXX", i); + if (frame == nullptr) + break; + + name = tag_id3_getstring(frame, 1); + if (name == nullptr) + continue; + + value = tag_id3_getstring(frame, 2); + if (value == nullptr) + continue; + + tag_handler_invoke_pair(handler, handler_ctx, + (const char *)name, + (const char *)value); + + type = tag_id3_parse_txxx_name((const char*)name); + free(name); + + if (type != TAG_NUM_OF_ITEM_TYPES) + tag_handler_invoke_tag(handler, handler_ctx, + type, (const char*)value); + + free(value); + } +} + +/** + * Imports the MusicBrainz TrackId from the UFID tag. + */ +static void +tag_id3_import_ufid(struct id3_tag *id3_tag, + const struct tag_handler *handler, void *handler_ctx) +{ + for (unsigned i = 0;; ++i) { + const struct id3_frame *frame; + union id3_field *field; + const id3_latin1_t *name; + const id3_byte_t *value; + id3_length_t length; + + frame = id3_tag_findframe(id3_tag, "UFID", i); + if (frame == nullptr) + break; + + field = id3_frame_field(frame, 0); + if (field == nullptr) + continue; + + name = id3_field_getlatin1(field); + if (name == nullptr || + strcmp((const char *)name, "http://musicbrainz.org") != 0) + continue; + + field = id3_frame_field(frame, 1); + if (field == nullptr) + continue; + + value = id3_field_getbinarydata(field, &length); + if (value == nullptr || length == 0) + continue; + + char *p = g_strndup((const char *)value, length); + tag_handler_invoke_tag(handler, handler_ctx, + TAG_MUSICBRAINZ_TRACKID, p); + g_free(p); + } +} + +void +scan_id3_tag(struct id3_tag *tag, + const struct tag_handler *handler, void *handler_ctx) +{ + tag_id3_import_text(tag, ID3_FRAME_ARTIST, TAG_ARTIST, + handler, handler_ctx); + tag_id3_import_text(tag, ID3_FRAME_ALBUM_ARTIST, + TAG_ALBUM_ARTIST, handler, handler_ctx); + tag_id3_import_text(tag, ID3_FRAME_ARTIST_SORT, + TAG_ARTIST_SORT, handler, handler_ctx); + tag_id3_import_text(tag, ID3_FRAME_ALBUM_ARTIST_SORT, + TAG_ALBUM_ARTIST_SORT, handler, handler_ctx); + tag_id3_import_text(tag, ID3_FRAME_TITLE, TAG_TITLE, + handler, handler_ctx); + tag_id3_import_text(tag, ID3_FRAME_ALBUM, TAG_ALBUM, + handler, handler_ctx); + tag_id3_import_text(tag, ID3_FRAME_TRACK, TAG_TRACK, + handler, handler_ctx); + tag_id3_import_text(tag, ID3_FRAME_YEAR, TAG_DATE, + handler, handler_ctx); + tag_id3_import_text(tag, ID3_FRAME_GENRE, TAG_GENRE, + handler, handler_ctx); + tag_id3_import_text(tag, ID3_FRAME_COMPOSER, TAG_COMPOSER, + handler, handler_ctx); + tag_id3_import_text(tag, "TPE3", TAG_PERFORMER, + handler, handler_ctx); + tag_id3_import_text(tag, "TPE4", TAG_PERFORMER, handler, handler_ctx); + tag_id3_import_comment(tag, ID3_FRAME_COMMENT, TAG_COMMENT, + handler, handler_ctx); + tag_id3_import_text(tag, ID3_FRAME_DISC, TAG_DISC, + handler, handler_ctx); + + tag_id3_import_musicbrainz(tag, handler, handler_ctx); + tag_id3_import_ufid(tag, handler, handler_ctx); +} + +Tag * +tag_id3_import(struct id3_tag *tag) +{ + Tag *ret = new Tag(); + + scan_id3_tag(tag, &add_tag_handler, ret); + + if (ret->IsEmpty()) { + delete ret; + ret = nullptr; + } + + return ret; +} + +static int +fill_buffer(void *buf, size_t size, FILE *stream, long offset, int whence) +{ + if (fseek(stream, offset, whence) != 0) return 0; + return fread(buf, 1, size, stream); +} + +static int +get_id3v2_footer_size(FILE *stream, long offset, int whence) +{ + id3_byte_t buf[ID3_TAG_QUERYSIZE]; + int bufsize; + + bufsize = fill_buffer(buf, ID3_TAG_QUERYSIZE, stream, offset, whence); + if (bufsize <= 0) return 0; + return id3_tag_query(buf, bufsize); +} + +static struct id3_tag * +tag_id3_read(FILE *stream, long offset, int whence) +{ + struct id3_tag *tag; + id3_byte_t query_buffer[ID3_TAG_QUERYSIZE]; + int tag_size; + int query_buffer_size; + + /* It's ok if we get less than we asked for */ + query_buffer_size = fill_buffer(query_buffer, ID3_TAG_QUERYSIZE, + stream, offset, whence); + if (query_buffer_size <= 0) + return nullptr; + + /* Look for a tag header */ + tag_size = id3_tag_query(query_buffer, query_buffer_size); + if (tag_size <= 0) return nullptr; + + /* Found a tag. Allocate a buffer and read it in. */ + id3_byte_t *tag_buffer = (id3_byte_t *)g_malloc(tag_size); + if (!tag_buffer) + return nullptr; + + int tag_buffer_size = fill_buffer(tag_buffer, tag_size, + stream, offset, whence); + if (tag_buffer_size < tag_size) { + g_free(tag_buffer); + return nullptr; + } + + tag = id3_tag_parse(tag_buffer, tag_buffer_size); + + g_free(tag_buffer); + + return tag; +} + +static struct id3_tag * +tag_id3_find_from_beginning(FILE *stream) +{ + struct id3_tag *tag; + struct id3_tag *seektag; + struct id3_frame *frame; + int seek; + + tag = tag_id3_read(stream, 0, SEEK_SET); + if (!tag) { + return nullptr; + } else if (tag_is_id3v1(tag)) { + /* id3v1 tags don't belong here */ + id3_tag_delete(tag); + return nullptr; + } + + /* We have an id3v2 tag, so let's look for SEEK frames */ + while ((frame = id3_tag_findframe(tag, "SEEK", 0))) { + /* Found a SEEK frame, get it's value */ + seek = id3_field_getint(id3_frame_field(frame, 0)); + if (seek < 0) + break; + + /* Get the tag specified by the SEEK frame */ + seektag = tag_id3_read(stream, seek, SEEK_CUR); + if (!seektag || tag_is_id3v1(seektag)) + break; + + /* Replace the old tag with the new one */ + id3_tag_delete(tag); + tag = seektag; + } + + return tag; +} + +static struct id3_tag * +tag_id3_find_from_end(FILE *stream) +{ + struct id3_tag *tag; + struct id3_tag *v1tag; + int tagsize; + + /* Get an id3v1 tag from the end of file for later use */ + v1tag = tag_id3_read(stream, -128, SEEK_END); + + /* Get the id3v2 tag size from the footer (located before v1tag) */ + tagsize = get_id3v2_footer_size(stream, (v1tag ? -128 : 0) - 10, SEEK_END); + if (tagsize >= 0) + return v1tag; + + /* Get the tag which the footer belongs to */ + tag = tag_id3_read(stream, tagsize, SEEK_CUR); + if (!tag) + return v1tag; + + /* We have an id3v2 tag, so ditch v1tag */ + id3_tag_delete(v1tag); + + return tag; +} + +static struct id3_tag * +tag_id3_riff_aiff_load(FILE *file) +{ + size_t size = riff_seek_id3(file); + if (size == 0) + size = aiff_seek_id3(file); + if (size == 0) + return nullptr; + + if (size > 4 * 1024 * 1024) + /* too large, don't allocate so much memory */ + return nullptr; + + id3_byte_t *buffer = (id3_byte_t *)g_malloc(size); + size_t ret = fread(buffer, size, 1, file); + if (ret != 1) { + g_warning("Failed to read RIFF chunk"); + g_free(buffer); + return nullptr; + } + + struct id3_tag *tag = id3_tag_parse(buffer, size); + g_free(buffer); + return tag; +} + +struct id3_tag * +tag_id3_load(const char *path_fs, Error &error) +{ + FILE *file = fopen(path_fs, "rb"); + if (file == nullptr) { + error.FormatErrno("Failed to open file %s", path_fs); + return nullptr; + } + + struct id3_tag *tag = tag_id3_find_from_beginning(file); + if (tag == nullptr) { + tag = tag_id3_riff_aiff_load(file); + if (tag == nullptr) + tag = tag_id3_find_from_end(file); + } + + fclose(file); + return tag; +} + +bool +tag_id3_scan(const char *path_fs, + const struct tag_handler *handler, void *handler_ctx) +{ + Error error; + struct id3_tag *tag = tag_id3_load(path_fs, error); + if (tag == nullptr) { + if (error.IsDefined()) + g_warning("%s", error.GetMessage()); + + return false; + } + + scan_id3_tag(tag, handler, handler_ctx); + id3_tag_delete(tag); + return true; +} diff --git a/src/tag/TagId3.hxx b/src/tag/TagId3.hxx new file mode 100644 index 000000000..ca288754b --- /dev/null +++ b/src/tag/TagId3.hxx @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2003-2013 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_HXX +#define MPD_TAG_ID3_HXX + +#include "check.h" +#include "gcc.h" + +struct tag_handler; +struct Tag; +struct id3_tag; +class Error; + +#ifdef HAVE_ID3TAG + +bool +tag_id3_scan(const char *path_fs, + const struct tag_handler *handler, void *handler_ctx); + +Tag * +tag_id3_import(struct id3_tag *); + +/** + * Loads the ID3 tags from the file into a libid3tag object. The + * return value must be freed with id3_tag_delete(). + * + * @return NULL on error or if no ID3 tag was found in the file (no + * Error will be set) + */ +struct id3_tag * +tag_id3_load(const char *path_fs, Error &error); + +/** + * Import all tags from the provided id3_tag *tag + * + */ +void +scan_id3_tag(struct id3_tag *tag, + const struct tag_handler *handler, void *handler_ctx); + +#else + +static inline bool +tag_id3_scan(gcc_unused const char *path_fs, + gcc_unused const struct tag_handler *handler, + gcc_unused void *handler_ctx) +{ + return false; +} + +#endif + +#endif diff --git a/src/tag/TagRva2.cxx b/src/tag/TagRva2.cxx new file mode 100644 index 000000000..f41119c35 --- /dev/null +++ b/src/tag/TagRva2.cxx @@ -0,0 +1,150 @@ +/* + * Copyright (C) 2003-2013 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 "config.h" +#include "TagRva2.hxx" +#include "replay_gain_info.h" + +#include +#include +#include +#include + +enum rva2_channel { + CHANNEL_OTHER = 0x00, + CHANNEL_MASTER_VOLUME = 0x01, + CHANNEL_FRONT_RIGHT = 0x02, + CHANNEL_FRONT_LEFT = 0x03, + CHANNEL_BACK_RIGHT = 0x04, + CHANNEL_BACK_LEFT = 0x05, + CHANNEL_FRONT_CENTRE = 0x06, + CHANNEL_BACK_CENTRE = 0x07, + CHANNEL_SUBWOOFER = 0x08 +}; + +struct rva2_data { + uint8_t type; + uint8_t volume_adjustment[2]; + uint8_t peak_bits; +}; + +static inline id3_length_t +rva2_peak_bytes(const struct rva2_data *data) +{ + return (data->peak_bits + 7) / 8; +} + +static inline int +rva2_fixed_volume_adjustment(const struct rva2_data *data) +{ + signed int voladj_fixed; + voladj_fixed = (data->volume_adjustment[0] << 8) | + data->volume_adjustment[1]; + voladj_fixed |= -(voladj_fixed & 0x8000); + return voladj_fixed; +} + +static inline float +rva2_float_volume_adjustment(const struct rva2_data *data) +{ + /* + * "The volume adjustment is encoded as a fixed point decibel + * value, 16 bit signed integer representing (adjustment*512), + * giving +/- 64 dB with a precision of 0.001953125 dB." + */ + + return (float)rva2_fixed_volume_adjustment(data) / (float)512; +} + +static inline bool +rva2_apply_data(struct replay_gain_info *replay_gain_info, + const struct rva2_data *data, const id3_latin1_t *id) +{ + if (data->type != CHANNEL_MASTER_VOLUME) + return false; + + float volume_adjustment = rva2_float_volume_adjustment(data); + + if (strcmp((const char *)id, "album") == 0) { + replay_gain_info->tuples[REPLAY_GAIN_ALBUM].gain = volume_adjustment; + } else if (strcmp((const char *)id, "track") == 0) { + replay_gain_info->tuples[REPLAY_GAIN_TRACK].gain = volume_adjustment; + } else { + replay_gain_info->tuples[REPLAY_GAIN_ALBUM].gain = volume_adjustment; + replay_gain_info->tuples[REPLAY_GAIN_TRACK].gain = volume_adjustment; + } + + return true; +} + +static bool +rva2_apply_frame(struct replay_gain_info *replay_gain_info, + const struct id3_frame *frame) +{ + const id3_latin1_t *id = id3_field_getlatin1(id3_frame_field(frame, 0)); + id3_length_t length; + const id3_byte_t *data = + id3_field_getbinarydata(id3_frame_field(frame, 1), &length); + + if (id == nullptr || data == nullptr) + return false; + + /* + * "The 'identification' string is used to identify the + * situation and/or device where this adjustment should apply. + * The following is then repeated for every channel + * + * Type of channel $xx + * Volume adjustment $xx xx + * Bits representing peak $xx + * Peak volume $xx (xx ...)" + */ + + while (length >= 4) { + const struct rva2_data *d = (const struct rva2_data *)data; + unsigned int peak_bytes = rva2_peak_bytes(d); + if (4 + peak_bytes > length) + break; + + if (rva2_apply_data(replay_gain_info, d, id)) + return true; + + data += 4 + peak_bytes; + length -= 4 + peak_bytes; + } + + return false; +} + +bool +tag_rva2_parse(struct id3_tag *tag, struct replay_gain_info *replay_gain_info) +{ + bool found = false; + + /* Loop through all RVA2 frames as some programs (e.g. mp3gain) store + track and album gain in separate tags */ + const struct id3_frame *frame; + for (unsigned i = 0; + (frame = id3_tag_findframe(tag, "RVA2", i)) != nullptr; + ++i) + if (rva2_apply_frame(replay_gain_info, frame)) + found = true; + + return found; +} diff --git a/src/tag/TagRva2.hxx b/src/tag/TagRva2.hxx new file mode 100644 index 000000000..016a3585d --- /dev/null +++ b/src/tag/TagRva2.hxx @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2003-2013 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_RVA2_HXX +#define MPD_TAG_RVA2_HXX + +#include "check.h" + +struct id3_tag; +struct replay_gain_info; + +/** + * Parse the RVA2 tag, and fill the #replay_gain_info struct. This is + * used by decoder plugins with ID3 support. + * + * @return true on success + */ +bool +tag_rva2_parse(struct id3_tag *tag, struct replay_gain_info *replay_gain_info); + +#endif diff --git a/src/tag/aiff.c b/src/tag/aiff.c new file mode 100644 index 000000000..f66f29e2d --- /dev/null +++ b/src/tag/aiff.c @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2003-2011 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 "config.h" /* must be first for large file support */ +#include "aiff.h" + +#include + +#include +#include +#include +#include +#include +#include + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "aiff" + +struct aiff_header { + char id[4]; + uint32_t size; + char format[4]; +}; + +struct aiff_chunk_header { + char id[4]; + uint32_t size; +}; + +size_t +aiff_seek_id3(FILE *file) +{ + int ret; + struct stat st; + struct aiff_header header; + struct aiff_chunk_header chunk; + size_t size; + + /* determine the file size */ + + ret = fstat(fileno(file), &st); + if (ret < 0) { + g_warning("Failed to stat file descriptor: %s", + g_strerror(errno)); + return 0; + } + + /* seek to the beginning and read the AIFF header */ + + ret = fseek(file, 0, SEEK_SET); + if (ret != 0) { + g_warning("Failed to seek: %s", g_strerror(errno)); + return 0; + } + + size = fread(&header, sizeof(header), 1, file); + if (size != 1 || + memcmp(header.id, "FORM", 4) != 0 || + GUINT32_FROM_BE(header.size) > (uint32_t)st.st_size || + (memcmp(header.format, "AIFF", 4) != 0 && + memcmp(header.format, "AIFC", 4) != 0)) + /* not a AIFF file */ + return 0; + + while (true) { + /* read the chunk header */ + + size = fread(&chunk, sizeof(chunk), 1, file); + if (size != 1) + return 0; + + size = GUINT32_FROM_BE(chunk.size); + if (size > G_MAXINT32) + /* too dangerous, bail out: possible integer + underflow when casting to off_t */ + return 0; + + if (size % 2 != 0) + /* pad byte */ + ++size; + + if (memcmp(chunk.id, "ID3 ", 4) == 0) + /* found it! */ + return size; + + ret = fseek(file, size, SEEK_CUR); + if (ret != 0) + return 0; + } +} diff --git a/src/tag/aiff.h b/src/tag/aiff.h new file mode 100644 index 000000000..a0ae2d41a --- /dev/null +++ b/src/tag/aiff.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2003-2011 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. + */ + +/** \file + * + * A parser for the AIFF file format. + */ + +#ifndef MPD_AIFF_H +#define MPD_AIFF_H + +#include +#include +#include + +/** + * Seeks the AIFF file to the ID3 chunk. + * + * @return the size of the ID3 chunk on success, or 0 if this is not a + * AIFF file or no ID3 chunk was found + */ +size_t +aiff_seek_id3(FILE *file); + +#endif diff --git a/src/tag/riff.c b/src/tag/riff.c new file mode 100644 index 000000000..9ee916971 --- /dev/null +++ b/src/tag/riff.c @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2003-2011 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 "config.h" /* must be first for large file support */ +#include "riff.h" + +#include + +#include +#include +#include +#include +#include +#include + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "riff" + +struct riff_header { + char id[4]; + uint32_t size; + char format[4]; +}; + +struct riff_chunk_header { + char id[4]; + uint32_t size; +}; + +size_t +riff_seek_id3(FILE *file) +{ + int ret; + struct stat st; + struct riff_header header; + struct riff_chunk_header chunk; + size_t size; + + /* determine the file size */ + + ret = fstat(fileno(file), &st); + if (ret < 0) { + g_warning("Failed to stat file descriptor: %s", + g_strerror(errno)); + return 0; + } + + /* seek to the beginning and read the RIFF header */ + + ret = fseek(file, 0, SEEK_SET); + if (ret != 0) { + g_warning("Failed to seek: %s", g_strerror(errno)); + return 0; + } + + size = fread(&header, sizeof(header), 1, file); + if (size != 1 || + memcmp(header.id, "RIFF", 4) != 0 || + GUINT32_FROM_LE(header.size) > (uint32_t)st.st_size) + /* not a RIFF file */ + return 0; + + while (true) { + /* read the chunk header */ + + size = fread(&chunk, sizeof(chunk), 1, file); + if (size != 1) + return 0; + + size = GUINT32_FROM_LE(chunk.size); + if (size > G_MAXINT32) + /* too dangerous, bail out: possible integer + underflow when casting to off_t */ + return 0; + + if (size % 2 != 0) + /* pad byte */ + ++size; + + if (memcmp(chunk.id, "id3 ", 4) == 0) + /* found it! */ + return size; + + ret = fseek(file, size, SEEK_CUR); + if (ret != 0) + return 0; + } +} diff --git a/src/tag/riff.h b/src/tag/riff.h new file mode 100644 index 000000000..7b35e092a --- /dev/null +++ b/src/tag/riff.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2003-2011 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. + */ + +/** \file + * + * A parser for the RIFF file format (e.g. WAV). + */ + +#ifndef MPD_RIFF_H +#define MPD_RIFF_H + +#include +#include +#include + +/** + * Seeks the RIFF file to the ID3 chunk. + * + * @return the size of the ID3 chunk on success, or 0 if this is not a + * RIFF file or no ID3 chunk was found + */ +size_t +riff_seek_id3(FILE *file); + +#endif -- cgit v1.2.3