FlacMetadata.cxx 4.72 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 "lib/xiph/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/DivideString.hxx"
33

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

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

	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;
47

48
	return found;
49 50
}

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

	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);

61
	return mix_ramp;
62 63
}

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

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

	return false;
}

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

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

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

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

127 128 129 130 131 132 133 134 135 136
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);
}

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

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

	default:
		break;
	}
}
157

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

166
void
167
FlacMetadataChain::Scan(const TagHandler &handler, void *handler_ctx)
168 169 170
{
	FLACMetadataIterator iterator(*this);

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

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