FlacMetadata.cxx 5.85 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "config.h"
21
#include "FlacMetadata.hxx"
22
#include "XiphTags.hxx"
23
#include "MixRampInfo.hxx"
24
#include "tag/TagHandler.hxx"
25
#include "tag/TagTable.hxx"
26
#include "tag/TagBuilder.hxx"
27
#include "tag/Tag.hxx"
28
#include "ReplayGainInfo.hxx"
29
#include "util/ASCII.hxx"
30
#include "util/SplitString.hxx"
31

32
#include <string.h>
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
static const char *
vorbis_comment_value(const FLAC__StreamMetadata *block,
		     const char *name)
{
	int offset =
		FLAC__metadata_object_vorbiscomment_find_entry_from(block, 0,
								    name);
	if (offset < 0)
		return nullptr;

	size_t name_length = strlen(name);

	const FLAC__StreamMetadata_VorbisComment_Entry &vc =
		block->data.vorbis_comment.comments[offset];
	const char *comment = (const char *)vc.entry;

	/* 1 is for '=' */
	return comment + name_length + 1;
}

54 55 56 57
static bool
flac_find_float_comment(const FLAC__StreamMetadata *block,
			const char *cmnt, float *fl)
{
58 59
	const char *value = vorbis_comment_value(block, cmnt);
	if (value == nullptr)
60 61
		return false;

62
	*fl = (float)atof(value);
63 64 65
	return true;
}

66
bool
67
flac_parse_replay_gain(ReplayGainInfo &rgi,
68
		       const FLAC__StreamMetadata *block)
69
{
70
	rgi.Clear();
71

72
	bool found = false;
73
	if (flac_find_float_comment(block, "replaygain_album_gain",
74
				    &rgi.tuples[REPLAY_GAIN_ALBUM].gain))
75 76
		found = true;
	if (flac_find_float_comment(block, "replaygain_album_peak",
77
				    &rgi.tuples[REPLAY_GAIN_ALBUM].peak))
78 79
		found = true;
	if (flac_find_float_comment(block, "replaygain_track_gain",
80
				    &rgi.tuples[REPLAY_GAIN_TRACK].gain))
81 82
		found = true;
	if (flac_find_float_comment(block, "replaygain_track_peak",
83
				    &rgi.tuples[REPLAY_GAIN_TRACK].peak))
84 85
		found = true;

86
	return found;
87 88
}

89 90 91
gcc_pure
static std::string
flac_find_string_comment(const FLAC__StreamMetadata *block, const char *cmnt)
92
{
93 94
	const char *value = vorbis_comment_value(block, cmnt);
	if (value == nullptr)
95
		return std::string();
96

97
	return std::string(value);
98 99
}

100 101
MixRampInfo
flac_parse_mixramp(const FLAC__StreamMetadata *block)
102
{
103 104 105 106
	MixRampInfo mix_ramp;
	mix_ramp.SetStart(flac_find_string_comment(block, "mixramp_start"));
	mix_ramp.SetEnd(flac_find_string_comment(block, "mixramp_end"));
	return mix_ramp;
107 108
}

109 110
/**
 * Checks if the specified name matches the entry's name, and if yes,
111
 * returns the comment value;
112 113 114
 */
static const char *
flac_comment_value(const FLAC__StreamMetadata_VorbisComment_Entry *entry,
115
		   const char *name)
116 117 118 119
{
	size_t name_length = strlen(name);
	const char *comment = (const char*)entry->entry;

120
	if (!StringEqualsCaseASCII(comment, name, name_length))
121
		return nullptr;
122 123 124 125 126

	if (comment[name_length] == '=') {
		return comment + name_length + 1;
	}

127
	return nullptr;
128 129 130 131 132 133 134
}

/**
 * Check if the comment's name equals the passed name, and if so, copy
 * the comment value into the tag.
 */
static bool
135
flac_copy_comment(const FLAC__StreamMetadata_VorbisComment_Entry *entry,
136
		  const char *name, TagType tag_type,
137
		  const struct tag_handler *handler, void *handler_ctx)
138
{
139
	const char *value = flac_comment_value(entry, name);
140
	if (value != nullptr) {
141
		tag_handler_invoke_tag(handler, handler_ctx, tag_type, value);
142 143 144 145 146 147 148
		return true;
	}

	return false;
}

static void
149
flac_scan_comment(const FLAC__StreamMetadata_VorbisComment_Entry *entry,
150
		  const struct tag_handler *handler, void *handler_ctx)
151
{
152
	if (handler->pair != nullptr) {
153 154 155
		const char *comment = (const char *)entry->entry;
		const SplitString split(comment, '=');
		if (split.IsDefined() && !split.IsEmpty())
156
			tag_handler_invoke_pair(handler, handler_ctx,
157 158
						split.GetFirst(),
						split.GetSecond());
159 160
	}

161
	for (const struct tag_table *i = xiph_tags; i->name != nullptr; ++i)
162
		if (flac_copy_comment(entry, i->name, i->type,
163
				      handler, handler_ctx))
164
			return;
165 166

	for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
167
		if (flac_copy_comment(entry,
168
				      tag_item_names[i], (TagType)i,
169
				      handler, handler_ctx))
170 171 172
			return;
}

173
static void
174
flac_scan_comments(const FLAC__StreamMetadata_VorbisComment *comment,
175
		   const struct tag_handler *handler, void *handler_ctx)
176 177
{
	for (unsigned i = 0; i < comment->num_comments; ++i)
178
		flac_scan_comment(&comment->comments[i],
179
				  handler, handler_ctx);
180 181 182
}

void
183
flac_scan_metadata(const FLAC__StreamMetadata *block,
184
		   const struct tag_handler *handler, void *handler_ctx)
185 186 187
{
	switch (block->type) {
	case FLAC__METADATA_TYPE_VORBIS_COMMENT:
188
		flac_scan_comments(&block->data.vorbis_comment,
189
				   handler, handler_ctx);
190 191 192
		break;

	case FLAC__METADATA_TYPE_STREAMINFO:
193
		if (block->data.stream_info.sample_rate > 0)
194 195
			tag_handler_invoke_duration(handler, handler_ctx,
						    flac_duration(&block->data.stream_info));
196 197 198 199 200 201
		break;

	default:
		break;
	}
}
202

203 204
Tag
flac_vorbis_comments_to_tag(const FLAC__StreamMetadata_VorbisComment *comment)
205
{
206 207
	TagBuilder tag_builder;
	flac_scan_comments(comment, &add_tag_handler, &tag_builder);
208
	return tag_builder.Commit();
209 210
}

211
void
212
FlacMetadataChain::Scan(const struct tag_handler *handler, void *handler_ctx)
213 214 215
{
	FLACMetadataIterator iterator(*this);

216
	do {
217 218
		FLAC__StreamMetadata *block = iterator.GetBlock();
		if (block == nullptr)
219 220
			break;

221
		flac_scan_metadata(block, handler, handler_ctx);
222
	} while (iterator.Next());
223
}