FlacMetadata.cxx 4.79 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 "tag/VorbisComment.hxx"
29
#include "tag/ReplayGain.hxx"
30
#include "tag/MixRamp.hxx"
31
#include "ReplayGainInfo.hxx"
32
#include "util/ASCII.hxx"
33
#include "util/DivideString.hxx"
34

35
bool
36
flac_parse_replay_gain(ReplayGainInfo &rgi,
37
		       const FLAC__StreamMetadata_VorbisComment &vc)
38
{
39
	rgi.Clear();
40

41
	bool found = false;
42 43 44 45 46 47

	const auto *comments = vc.comments;
	for (FLAC__uint32 i = 0, n = vc.num_comments; i < n; ++i)
		if (ParseReplayGainVorbis(rgi,
					  (const char *)comments[i].entry))
			found = true;
48

49
	return found;
50 51
}

52
MixRampInfo
53
flac_parse_mixramp(const FLAC__StreamMetadata_VorbisComment &vc)
54
{
55
	MixRampInfo mix_ramp;
56 57 58 59 60 61

	const auto *comments = vc.comments;
	for (FLAC__uint32 i = 0, n = vc.num_comments; i < n; ++i)
		ParseMixRampVorbis(mix_ramp,
				   (const char *)comments[i].entry);

62
	return mix_ramp;
63 64
}

65 66
/**
 * Checks if the specified name matches the entry's name, and if yes,
67
 * returns the comment value;
68 69 70
 */
static const char *
flac_comment_value(const FLAC__StreamMetadata_VorbisComment_Entry *entry,
71
		   const char *name)
72
{
73
	return vorbis_comment_value((const char *)entry->entry, name);
74 75 76 77 78 79 80
}

/**
 * Check if the comment's name equals the passed name, and if so, copy
 * the comment value into the tag.
 */
static bool
81
flac_copy_comment(const FLAC__StreamMetadata_VorbisComment_Entry *entry,
82
		  const char *name, TagType tag_type,
83
		  const struct tag_handler *handler, void *handler_ctx)
84
{
85
	const char *value = flac_comment_value(entry, name);
86
	if (value != nullptr) {
87
		tag_handler_invoke_tag(handler, handler_ctx, tag_type, value);
88 89 90 91 92 93 94
		return true;
	}

	return false;
}

static void
95
flac_scan_comment(const FLAC__StreamMetadata_VorbisComment_Entry *entry,
96
		  const struct tag_handler *handler, void *handler_ctx)
97
{
98
	if (handler->pair != nullptr) {
99
		const char *comment = (const char *)entry->entry;
100
		const DivideString split(comment, '=');
101
		if (split.IsDefined() && !split.IsEmpty())
102
			tag_handler_invoke_pair(handler, handler_ctx,
103 104
						split.GetFirst(),
						split.GetSecond());
105 106
	}

107
	for (const struct tag_table *i = xiph_tags; i->name != nullptr; ++i)
108
		if (flac_copy_comment(entry, i->name, i->type,
109
				      handler, handler_ctx))
110
			return;
111 112

	for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
113
		if (flac_copy_comment(entry,
114
				      tag_item_names[i], (TagType)i,
115
				      handler, handler_ctx))
116 117 118
			return;
}

119
static void
120
flac_scan_comments(const FLAC__StreamMetadata_VorbisComment *comment,
121
		   const struct tag_handler *handler, void *handler_ctx)
122 123
{
	for (unsigned i = 0; i < comment->num_comments; ++i)
124
		flac_scan_comment(&comment->comments[i],
125
				  handler, handler_ctx);
126 127
}

128 129 130 131 132 133 134 135 136 137
gcc_pure
static inline SongTime
flac_duration(const FLAC__StreamMetadata_StreamInfo *stream_info)
{
	assert(stream_info->sample_rate > 0);

	return SongTime::FromScale<uint64_t>(stream_info->total_samples,
					     stream_info->sample_rate);
}

138
void
139
flac_scan_metadata(const FLAC__StreamMetadata *block,
140
		   const struct tag_handler *handler, void *handler_ctx)
141 142 143
{
	switch (block->type) {
	case FLAC__METADATA_TYPE_VORBIS_COMMENT:
144
		flac_scan_comments(&block->data.vorbis_comment,
145
				   handler, handler_ctx);
146 147 148
		break;

	case FLAC__METADATA_TYPE_STREAMINFO:
149
		if (block->data.stream_info.sample_rate > 0)
150 151
			tag_handler_invoke_duration(handler, handler_ctx,
						    flac_duration(&block->data.stream_info));
152 153 154 155 156 157
		break;

	default:
		break;
	}
}
158

159 160
Tag
flac_vorbis_comments_to_tag(const FLAC__StreamMetadata_VorbisComment *comment)
161
{
162 163
	TagBuilder tag_builder;
	flac_scan_comments(comment, &add_tag_handler, &tag_builder);
164
	return tag_builder.Commit();
165 166
}

167
void
168
FlacMetadataChain::Scan(const struct tag_handler *handler, void *handler_ctx)
169 170 171
{
	FLACMetadataIterator iterator(*this);

172
	do {
173 174
		FLAC__StreamMetadata *block = iterator.GetBlock();
		if (block == nullptr)
175 176
			break;

177
		flac_scan_metadata(block, handler, handler_ctx);
178
	} while (iterator.Next());
179
}