FlacStreamMetadata.cxx 4.57 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "FlacStreamMetadata.hxx"
22 23 24
#include "FlacAudioFormat.hxx"
#include "XiphTags.hxx"
#include "CheckAudioFormat.hxx"
25
#include "MixRampInfo.hxx"
26 27 28
#include "tag/Handler.hxx"
#include "tag/Table.hxx"
#include "tag/Builder.hxx"
29
#include "tag/Tag.hxx"
30
#include "tag/VorbisComment.hxx"
31
#include "tag/ReplayGain.hxx"
32
#include "tag/MixRamp.hxx"
33
#include "ReplayGainInfo.hxx"
34
#include "util/DivideString.hxx"
35

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

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

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

50
	return found;
51 52
}

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

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

63
	return mix_ramp;
64 65
}

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

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

	return false;
}

static void
96
flac_scan_comment(const FLAC__StreamMetadata_VorbisComment_Entry *entry,
97
		  TagHandler &handler) noexcept
98
{
99
	if (handler.WantPair()) {
100
		const char *comment = (const char *)entry->entry;
101
		const DivideString split(comment, '=');
102
		if (split.IsDefined() && !split.empty())
103
			handler.OnPair(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, handler))
108
			return;
109 110

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

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

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

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

136 137 138 139 140 141
static void
Scan(const FLAC__StreamMetadata_StreamInfo &stream_info,
     TagHandler &handler) noexcept
{
	if (stream_info.sample_rate > 0)
		handler.OnDuration(flac_duration(&stream_info));
142 143 144 145 146 147 148

	try {
		handler.OnAudioFormat(CheckAudioFormat(stream_info.sample_rate,
						       FlacSampleFormat(stream_info.bits_per_sample),
						       stream_info.channels));
	} catch (...) {
	}
149 150
}

151
void
152
flac_scan_metadata(const FLAC__StreamMetadata *block,
153
		   TagHandler &handler) noexcept
154 155 156
{
	switch (block->type) {
	case FLAC__METADATA_TYPE_VORBIS_COMMENT:
157
		flac_scan_comments(&block->data.vorbis_comment,
158
				   handler);
159 160 161
		break;

	case FLAC__METADATA_TYPE_STREAMINFO:
162
		Scan(block->data.stream_info, handler);
163 164 165 166 167 168
		break;

	default:
		break;
	}
}
169

170 171
Tag
flac_vorbis_comments_to_tag(const FLAC__StreamMetadata_VorbisComment *comment)
172
{
173
	TagBuilder tag_builder;
174 175
	AddTagHandler h(tag_builder);
	flac_scan_comments(comment, h);
176
	return tag_builder.Commit();
177
}