FlacMetadata.cxx 6.27 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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 25
#include "tag/Tag.hxx"
#include "tag/TagHandler.hxx"
26
#include "tag/TagTable.hxx"
27
#include "tag/TagBuilder.hxx"
28
#include "ReplayGainInfo.hxx"
29
#include "util/ASCII.hxx"
30 31 32 33

#include <glib.h>

#include <assert.h>
34
#include <string.h>
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

static bool
flac_find_float_comment(const FLAC__StreamMetadata *block,
			const char *cmnt, float *fl)
{
	int offset;
	size_t pos;
	int len;
	unsigned char tmp, *p;

	offset = FLAC__metadata_object_vorbiscomment_find_entry_from(block, 0,
								     cmnt);
	if (offset < 0)
		return false;

	pos = strlen(cmnt) + 1; /* 1 is for '=' */
	len = block->data.vorbis_comment.comments[offset].length - pos;
	if (len <= 0)
		return false;

	p = &block->data.vorbis_comment.comments[offset].entry[pos];
	tmp = p[len];
	p[len] = '\0';
	*fl = (float)atof((char *)p);
	p[len] = tmp;

	return true;
}

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

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

84
	return found;
85 86
}

87 88 89
gcc_pure
static std::string
flac_find_string_comment(const FLAC__StreamMetadata *block, const char *cmnt)
90 91 92 93
{
	int offset;
	size_t pos;
	int len;
94
	const unsigned char *p;
95 96 97 98

	offset = FLAC__metadata_object_vorbiscomment_find_entry_from(block, 0,
								     cmnt);
	if (offset < 0)
99
		return std::string();
100 101 102 103

	pos = strlen(cmnt) + 1; /* 1 is for '=' */
	len = block->data.vorbis_comment.comments[offset].length - pos;
	if (len <= 0)
104
		return std::string();
105 106

	p = &block->data.vorbis_comment.comments[offset].entry[pos];
107
	return std::string((const char *)p, len);
108 109
}

110 111
MixRampInfo
flac_parse_mixramp(const FLAC__StreamMetadata *block)
112
{
113 114 115 116
	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;
117 118
}

119 120 121 122 123 124
/**
 * Checks if the specified name matches the entry's name, and if yes,
 * returns the comment value (not null-temrinated).
 */
static const char *
flac_comment_value(const FLAC__StreamMetadata_VorbisComment_Entry *entry,
125
		   const char *name, size_t *length_r)
126 127 128 129 130
{
	size_t name_length = strlen(name);
	const char *comment = (const char*)entry->entry;

	if (entry->length <= name_length ||
131
	    !StringEqualsCaseASCII(comment, name, name_length))
132
		return nullptr;
133 134 135 136 137 138

	if (comment[name_length] == '=') {
		*length_r = entry->length - name_length - 1;
		return comment + name_length + 1;
	}

139
	return nullptr;
140 141 142 143 144 145 146
}

/**
 * Check if the comment's name equals the passed name, and if so, copy
 * the comment value into the tag.
 */
static bool
147
flac_copy_comment(const FLAC__StreamMetadata_VorbisComment_Entry *entry,
148
		  const char *name, TagType tag_type,
149
		  const struct tag_handler *handler, void *handler_ctx)
150 151 152 153
{
	const char *value;
	size_t value_length;

154
	value = flac_comment_value(entry, name, &value_length);
155
	if (value != nullptr) {
156 157 158
		char *p = g_strndup(value, value_length);
		tag_handler_invoke_tag(handler, handler_ctx, tag_type, p);
		g_free(p);
159 160 161 162 163 164 165
		return true;
	}

	return false;
}

static void
166
flac_scan_comment(const FLAC__StreamMetadata_VorbisComment_Entry *entry,
167
		  const struct tag_handler *handler, void *handler_ctx)
168
{
169
	if (handler->pair != nullptr) {
170 171 172
		char *name = g_strdup((const char*)entry->entry);
		char *value = strchr(name, '=');

173
		if (value != nullptr && value > name) {
174 175 176 177 178 179 180 181
			*value++ = 0;
			tag_handler_invoke_pair(handler, handler_ctx,
						name, value);
		}

		g_free(name);
	}

182
	for (const struct tag_table *i = xiph_tags; i->name != nullptr; ++i)
183
		if (flac_copy_comment(entry, i->name, i->type,
184
				      handler, handler_ctx))
185
			return;
186 187

	for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
188
		if (flac_copy_comment(entry,
189
				      tag_item_names[i], (TagType)i,
190
				      handler, handler_ctx))
191 192 193
			return;
}

194
static void
195
flac_scan_comments(const FLAC__StreamMetadata_VorbisComment *comment,
196
		   const struct tag_handler *handler, void *handler_ctx)
197 198
{
	for (unsigned i = 0; i < comment->num_comments; ++i)
199
		flac_scan_comment(&comment->comments[i],
200
				  handler, handler_ctx);
201 202 203
}

void
204
flac_scan_metadata(const FLAC__StreamMetadata *block,
205
		   const struct tag_handler *handler, void *handler_ctx)
206 207 208
{
	switch (block->type) {
	case FLAC__METADATA_TYPE_VORBIS_COMMENT:
209
		flac_scan_comments(&block->data.vorbis_comment,
210
				   handler, handler_ctx);
211 212 213
		break;

	case FLAC__METADATA_TYPE_STREAMINFO:
214
		if (block->data.stream_info.sample_rate > 0)
215 216
			tag_handler_invoke_duration(handler, handler_ctx,
						    flac_duration(&block->data.stream_info));
217 218 219 220 221 222
		break;

	default:
		break;
	}
}
223

224
void
Max Kellermann's avatar
Max Kellermann committed
225
flac_vorbis_comments_to_tag(Tag &tag,
226 227
			    const FLAC__StreamMetadata_VorbisComment *comment)
{
228 229 230
	TagBuilder tag_builder;
	flac_scan_comments(comment, &add_tag_handler, &tag_builder);
	tag_builder.Commit(tag);
231 232
}

233
void
234
FlacMetadataChain::Scan(const struct tag_handler *handler, void *handler_ctx)
235 236 237
{
	FLACMetadataIterator iterator(*this);

238
	do {
239 240
		FLAC__StreamMetadata *block = iterator.GetBlock();
		if (block == nullptr)
241 242
			break;

243
		flac_scan_metadata(block, handler, handler_ctx);
244
	} while (iterator.Next());
245
}