2009-11-11 07:50:40 +01:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 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"
|
2013-05-05 14:19:04 +02:00
|
|
|
#include "FlacMetadata.hxx"
|
2013-07-29 07:42:50 +02:00
|
|
|
#include "XiphTags.hxx"
|
2013-10-26 14:19:34 +02:00
|
|
|
#include "MixRampInfo.hxx"
|
2013-09-05 18:22:02 +02:00
|
|
|
#include "tag/TagHandler.hxx"
|
2013-09-04 23:54:37 +02:00
|
|
|
#include "tag/TagTable.hxx"
|
2013-09-05 19:11:50 +02:00
|
|
|
#include "tag/TagBuilder.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"
|
2013-10-02 12:22:12 +02:00
|
|
|
#include "ReplayGainInfo.hxx"
|
2013-10-21 08:42:55 +02:00
|
|
|
#include "util/ASCII.hxx"
|
2013-12-14 12:58:26 +01:00
|
|
|
#include "util/SplitString.hxx"
|
2009-11-11 07:50:40 +01:00
|
|
|
|
2013-09-04 23:53:50 +02:00
|
|
|
#include <string.h>
|
2009-11-11 07:50:40 +01:00
|
|
|
|
2013-12-14 13:38:08 +01:00
|
|
|
static const char *
|
|
|
|
vorbis_comment_value(const FLAC__StreamMetadata *block,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
int offset =
|
|
|
|
FLAC__metadata_object_vorbiscomment_find_entry_from(block, 0,
|
|
|
|
name);
|
|
|
|
if (offset < 0)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
size_t name_length = strlen(name);
|
|
|
|
|
|
|
|
const FLAC__StreamMetadata_VorbisComment_Entry &vc =
|
|
|
|
block->data.vorbis_comment.comments[offset];
|
|
|
|
const char *comment = (const char *)vc.entry;
|
|
|
|
|
|
|
|
/* 1 is for '=' */
|
|
|
|
return comment + name_length + 1;
|
|
|
|
}
|
|
|
|
|
2009-11-11 07:50:40 +01:00
|
|
|
static bool
|
|
|
|
flac_find_float_comment(const FLAC__StreamMetadata *block,
|
|
|
|
const char *cmnt, float *fl)
|
|
|
|
{
|
2013-12-14 13:38:08 +01:00
|
|
|
const char *value = vorbis_comment_value(block, cmnt);
|
|
|
|
if (value == nullptr)
|
2009-11-11 07:50:40 +01:00
|
|
|
return false;
|
|
|
|
|
2013-12-14 13:38:08 +01:00
|
|
|
*fl = (float)atof(value);
|
2009-11-11 07:50:40 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-02-14 20:36:31 +01:00
|
|
|
bool
|
2013-10-25 19:09:22 +02:00
|
|
|
flac_parse_replay_gain(ReplayGainInfo &rgi,
|
2010-02-14 20:36:31 +01:00
|
|
|
const FLAC__StreamMetadata *block)
|
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;
|
2010-01-28 20:54:04 +01:00
|
|
|
if (flac_find_float_comment(block, "replaygain_album_gain",
|
2013-10-25 19:09:22 +02:00
|
|
|
&rgi.tuples[REPLAY_GAIN_ALBUM].gain))
|
2010-01-28 20:54:04 +01:00
|
|
|
found = true;
|
|
|
|
if (flac_find_float_comment(block, "replaygain_album_peak",
|
2013-10-25 19:09:22 +02:00
|
|
|
&rgi.tuples[REPLAY_GAIN_ALBUM].peak))
|
2010-01-28 20:54:04 +01:00
|
|
|
found = true;
|
|
|
|
if (flac_find_float_comment(block, "replaygain_track_gain",
|
2013-10-25 19:09:22 +02:00
|
|
|
&rgi.tuples[REPLAY_GAIN_TRACK].gain))
|
2010-01-28 20:54:04 +01:00
|
|
|
found = true;
|
|
|
|
if (flac_find_float_comment(block, "replaygain_track_peak",
|
2013-10-25 19:09:22 +02:00
|
|
|
&rgi.tuples[REPLAY_GAIN_TRACK].peak))
|
2010-01-28 20:54:04 +01:00
|
|
|
found = true;
|
|
|
|
|
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
|
|
|
gcc_pure
|
|
|
|
static std::string
|
|
|
|
flac_find_string_comment(const FLAC__StreamMetadata *block, const char *cmnt)
|
2010-03-21 18:21:47 +01:00
|
|
|
{
|
2013-12-14 13:38:08 +01:00
|
|
|
const char *value = vorbis_comment_value(block, cmnt);
|
|
|
|
if (value == nullptr)
|
2013-10-26 14:19:34 +02:00
|
|
|
return std::string();
|
2010-03-21 18:21:47 +01:00
|
|
|
|
2013-12-14 13:38:08 +01:00
|
|
|
return std::string(value);
|
2010-03-21 18:21:47 +01:00
|
|
|
}
|
|
|
|
|
2013-10-26 14:19:34 +02:00
|
|
|
MixRampInfo
|
|
|
|
flac_parse_mixramp(const FLAC__StreamMetadata *block)
|
2010-03-21 18:21:47 +01:00
|
|
|
{
|
2013-10-26 14:19:34 +02:00
|
|
|
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;
|
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,
|
2013-12-14 13:26:02 +01:00
|
|
|
const char *name)
|
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,
|
2012-02-11 19:12:02 +01:00
|
|
|
const struct tag_handler *handler, void *handler_ctx)
|
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) {
|
2013-12-14 13:26:02 +01:00
|
|
|
tag_handler_invoke_tag(handler, handler_ctx, 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,
|
2012-02-11 19:12:02 +01:00
|
|
|
const struct tag_handler *handler, void *handler_ctx)
|
2009-11-11 07:50:40 +01:00
|
|
|
{
|
2012-10-02 10:56:44 +02:00
|
|
|
if (handler->pair != nullptr) {
|
2013-12-14 12:58:26 +01:00
|
|
|
const char *comment = (const char *)entry->entry;
|
|
|
|
const SplitString split(comment, '=');
|
|
|
|
if (split.IsDefined() && !split.IsEmpty())
|
2012-02-11 19:24:51 +01:00
|
|
|
tag_handler_invoke_pair(handler, handler_ctx,
|
2013-12-14 12:58:26 +01:00
|
|
|
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)
|
2012-10-02 11:43:26 +02:00
|
|
|
if (flac_copy_comment(entry, i->name, i->type,
|
2012-02-11 19:12:02 +01:00
|
|
|
handler, handler_ctx))
|
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,
|
2012-02-11 19:12:02 +01:00
|
|
|
handler, handler_ctx))
|
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,
|
2012-02-11 19:12:02 +01:00
|
|
|
const struct tag_handler *handler, void *handler_ctx)
|
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],
|
2012-02-11 19:12:02 +01:00
|
|
|
handler, handler_ctx);
|
2009-11-11 07:50:40 +01:00
|
|
|
}
|
|
|
|
|
2014-08-29 22:43:36 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-11-11 07:50:40 +01:00
|
|
|
void
|
2012-10-02 11:43:26 +02:00
|
|
|
flac_scan_metadata(const FLAC__StreamMetadata *block,
|
2012-02-11 19:12:02 +01:00
|
|
|
const struct tag_handler *handler, void *handler_ctx)
|
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,
|
2012-02-11 19:12:02 +01:00
|
|
|
handler, handler_ctx);
|
2009-11-11 07:50:40 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FLAC__METADATA_TYPE_STREAMINFO:
|
2011-07-03 14:57:56 +02:00
|
|
|
if (block->data.stream_info.sample_rate > 0)
|
2012-02-11 19:12:02 +01:00
|
|
|
tag_handler_invoke_duration(handler, handler_ctx,
|
|
|
|
flac_duration(&block->data.stream_info));
|
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;
|
|
|
|
flac_scan_comments(comment, &add_tag_handler, &tag_builder);
|
2014-01-08 19:42:04 +01:00
|
|
|
return tag_builder.Commit();
|
2012-02-11 19:12:02 +01:00
|
|
|
}
|
|
|
|
|
2012-10-02 18:24:15 +02:00
|
|
|
void
|
2013-05-05 14:19:04 +02:00
|
|
|
FlacMetadataChain::Scan(const struct tag_handler *handler, void *handler_ctx)
|
2012-10-02 18:24:15 +02:00
|
|
|
{
|
|
|
|
FLACMetadataIterator iterator(*this);
|
|
|
|
|
2010-01-06 08:28:27 +01:00
|
|
|
do {
|
2012-10-02 17:38:21 +02:00
|
|
|
FLAC__StreamMetadata *block = iterator.GetBlock();
|
|
|
|
if (block == nullptr)
|
2010-01-06 08:28:27 +01:00
|
|
|
break;
|
|
|
|
|
2012-10-02 11:43:26 +02:00
|
|
|
flac_scan_metadata(block, handler, handler_ctx);
|
2012-10-02 17:38:21 +02:00
|
|
|
} while (iterator.Next());
|
2012-02-11 19:12:02 +01:00
|
|
|
}
|