2009-11-11 07:50:40 +01:00
|
|
|
/*
|
2018-07-07 14:29:49 +02:00
|
|
|
* Copyright 2003-2018 The Music Player Daemon Project
|
2009-11-11 07:50:40 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2018-07-07 14:29:49 +02:00
|
|
|
#include "FlacStreamMetadata.hxx"
|
2016-05-03 12:54:47 +02:00
|
|
|
#include "lib/xiph/XiphTags.hxx"
|
2013-10-26 14:19:34 +02:00
|
|
|
#include "MixRampInfo.hxx"
|
2017-02-08 08:26:58 +01:00
|
|
|
#include "tag/Handler.hxx"
|
|
|
|
#include "tag/Table.hxx"
|
|
|
|
#include "tag/Builder.hxx"
|
2014-01-08 19:42:04 +01:00
|
|
|
#include "tag/Tag.hxx"
|
2014-09-24 20:56:25 +02:00
|
|
|
#include "tag/VorbisComment.hxx"
|
2014-09-24 20:54:20 +02:00
|
|
|
#include "tag/ReplayGain.hxx"
|
2014-09-24 22:44:58 +02:00
|
|
|
#include "tag/MixRamp.hxx"
|
2013-10-02 12:22:12 +02:00
|
|
|
#include "ReplayGainInfo.hxx"
|
2014-12-03 21:38:06 +01:00
|
|
|
#include "util/DivideString.hxx"
|
2009-11-11 07:50:40 +01:00
|
|
|
|
2010-02-14 20:36:31 +01:00
|
|
|
bool
|
2013-10-25 19:09:22 +02:00
|
|
|
flac_parse_replay_gain(ReplayGainInfo &rgi,
|
2014-09-24 22:34:08 +02:00
|
|
|
const FLAC__StreamMetadata_VorbisComment &vc)
|
2009-11-11 07:50:40 +01:00
|
|
|
{
|
2013-10-25 19:05:49 +02:00
|
|
|
rgi.Clear();
|
2009-11-11 07:50:40 +01:00
|
|
|
|
2013-10-25 19:05:49 +02:00
|
|
|
bool found = false;
|
2014-09-24 20:54:20 +02:00
|
|
|
|
|
|
|
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;
|
2010-01-28 20:54:04 +01:00
|
|
|
|
2010-02-14 20:36:31 +01:00
|
|
|
return found;
|
2009-11-11 07:50:40 +01:00
|
|
|
}
|
|
|
|
|
2013-10-26 14:19:34 +02:00
|
|
|
MixRampInfo
|
2014-09-24 22:50:28 +02:00
|
|
|
flac_parse_mixramp(const FLAC__StreamMetadata_VorbisComment &vc)
|
2010-03-21 18:21:47 +01:00
|
|
|
{
|
2013-10-26 14:19:34 +02:00
|
|
|
MixRampInfo mix_ramp;
|
2014-09-24 22:44:58 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2013-10-26 14:19:34 +02:00
|
|
|
return mix_ramp;
|
2010-03-21 18:21:47 +01:00
|
|
|
}
|
|
|
|
|
2009-11-11 07:50:40 +01:00
|
|
|
/**
|
|
|
|
* Checks if the specified name matches the entry's name, and if yes,
|
2013-12-14 13:26:02 +01:00
|
|
|
* returns the comment value;
|
2009-11-11 07:50:40 +01:00
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
flac_comment_value(const FLAC__StreamMetadata_VorbisComment_Entry *entry,
|
2017-05-08 14:44:49 +02:00
|
|
|
const char *name) noexcept
|
2009-11-11 07:50:40 +01:00
|
|
|
{
|
2014-09-24 20:56:25 +02:00
|
|
|
return vorbis_comment_value((const char *)entry->entry, name);
|
2009-11-11 07:50:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the comment's name equals the passed name, and if so, copy
|
|
|
|
* the comment value into the tag.
|
|
|
|
*/
|
|
|
|
static bool
|
2012-02-11 19:12:02 +01:00
|
|
|
flac_copy_comment(const FLAC__StreamMetadata_VorbisComment_Entry *entry,
|
2013-10-20 13:32:59 +02:00
|
|
|
const char *name, TagType tag_type,
|
2018-07-05 19:07:05 +02:00
|
|
|
TagHandler &handler) noexcept
|
2009-11-11 07:50:40 +01:00
|
|
|
{
|
2013-12-14 13:26:02 +01:00
|
|
|
const char *value = flac_comment_value(entry, name);
|
2012-10-02 10:56:44 +02:00
|
|
|
if (value != nullptr) {
|
2018-07-05 19:07:05 +02:00
|
|
|
handler.OnTag(tag_type, value);
|
2009-11-11 07:50:40 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-10-02 11:43:26 +02:00
|
|
|
flac_scan_comment(const FLAC__StreamMetadata_VorbisComment_Entry *entry,
|
2018-07-05 19:07:05 +02:00
|
|
|
TagHandler &handler) noexcept
|
2009-11-11 07:50:40 +01:00
|
|
|
{
|
2018-07-05 19:07:05 +02:00
|
|
|
if (handler.WantPair()) {
|
2013-12-14 12:58:26 +01:00
|
|
|
const char *comment = (const char *)entry->entry;
|
2014-12-03 21:38:06 +01:00
|
|
|
const DivideString split(comment, '=');
|
2017-11-10 19:24:33 +01:00
|
|
|
if (split.IsDefined() && !split.empty())
|
2018-07-05 19:07:05 +02:00
|
|
|
handler.OnPair(split.GetFirst(), split.GetSecond());
|
2012-02-11 19:24:51 +01:00
|
|
|
}
|
|
|
|
|
2012-10-02 10:56:44 +02:00
|
|
|
for (const struct tag_table *i = xiph_tags; i->name != nullptr; ++i)
|
2018-07-05 19:07:05 +02:00
|
|
|
if (flac_copy_comment(entry, i->name, i->type, handler))
|
2012-02-11 10:51:43 +01:00
|
|
|
return;
|
2009-11-11 07:50:40 +01:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
|
2012-02-11 19:12:02 +01:00
|
|
|
if (flac_copy_comment(entry,
|
2013-10-20 13:32:59 +02:00
|
|
|
tag_item_names[i], (TagType)i,
|
2018-07-05 19:07:05 +02:00
|
|
|
handler))
|
2009-11-11 07:50:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
static void
|
2012-10-02 11:43:26 +02:00
|
|
|
flac_scan_comments(const FLAC__StreamMetadata_VorbisComment *comment,
|
2018-07-05 19:07:05 +02:00
|
|
|
TagHandler &handler) noexcept
|
2009-11-11 07:50:40 +01:00
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < comment->num_comments; ++i)
|
2012-10-02 11:43:26 +02:00
|
|
|
flac_scan_comment(&comment->comments[i],
|
2018-07-05 19:07:05 +02:00
|
|
|
handler);
|
2009-11-11 07:50:40 +01:00
|
|
|
}
|
|
|
|
|
2014-08-29 22:43:36 +02:00
|
|
|
gcc_pure
|
|
|
|
static inline SongTime
|
2017-05-08 14:44:49 +02:00
|
|
|
flac_duration(const FLAC__StreamMetadata_StreamInfo *stream_info) noexcept
|
2014-08-29 22:43:36 +02:00
|
|
|
{
|
|
|
|
assert(stream_info->sample_rate > 0);
|
|
|
|
|
|
|
|
return SongTime::FromScale<uint64_t>(stream_info->total_samples,
|
|
|
|
stream_info->sample_rate);
|
|
|
|
}
|
|
|
|
|
2018-07-07 14:33:53 +02:00
|
|
|
static void
|
|
|
|
Scan(const FLAC__StreamMetadata_StreamInfo &stream_info,
|
|
|
|
TagHandler &handler) noexcept
|
|
|
|
{
|
|
|
|
if (stream_info.sample_rate > 0)
|
|
|
|
handler.OnDuration(flac_duration(&stream_info));
|
|
|
|
}
|
|
|
|
|
2009-11-11 07:50:40 +01:00
|
|
|
void
|
2012-10-02 11:43:26 +02:00
|
|
|
flac_scan_metadata(const FLAC__StreamMetadata *block,
|
2018-07-05 19:07:05 +02:00
|
|
|
TagHandler &handler) noexcept
|
2009-11-11 07:50:40 +01:00
|
|
|
{
|
|
|
|
switch (block->type) {
|
|
|
|
case FLAC__METADATA_TYPE_VORBIS_COMMENT:
|
2012-10-02 11:43:26 +02:00
|
|
|
flac_scan_comments(&block->data.vorbis_comment,
|
2018-07-05 19:07:05 +02:00
|
|
|
handler);
|
2009-11-11 07:50:40 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FLAC__METADATA_TYPE_STREAMINFO:
|
2018-07-07 14:33:53 +02:00
|
|
|
Scan(block->data.stream_info, handler);
|
2009-11-11 07:50:40 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-01-06 08:28:27 +01:00
|
|
|
|
2014-01-08 19:42:04 +01:00
|
|
|
Tag
|
|
|
|
flac_vorbis_comments_to_tag(const FLAC__StreamMetadata_VorbisComment *comment)
|
2012-02-11 19:12:02 +01:00
|
|
|
{
|
2013-09-05 19:11:50 +02:00
|
|
|
TagBuilder tag_builder;
|
2018-07-05 19:07:05 +02:00
|
|
|
AddTagHandler h(tag_builder);
|
|
|
|
flac_scan_comments(comment, h);
|
2014-01-08 19:42:04 +01:00
|
|
|
return tag_builder.Commit();
|
2012-02-11 19:12:02 +01:00
|
|
|
}
|