2009-08-26 20:08:13 +02:00
|
|
|
/*
|
2018-10-31 17:54:59 +01:00
|
|
|
* Copyright 2003-2018 The Music Player Daemon Project
|
2009-08-26 20:08:13 +02: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.
|
|
|
|
*/
|
|
|
|
|
2013-07-28 12:45:48 +02:00
|
|
|
#include "Mpg123DecoderPlugin.hxx"
|
2014-01-24 00:02:24 +01:00
|
|
|
#include "../DecoderAPI.hxx"
|
2013-07-29 07:50:08 +02:00
|
|
|
#include "CheckAudioFormat.hxx"
|
2017-02-08 08:26:58 +01:00
|
|
|
#include "tag/Handler.hxx"
|
|
|
|
#include "tag/Builder.hxx"
|
2014-09-24 20:08:44 +02:00
|
|
|
#include "tag/ReplayGain.hxx"
|
|
|
|
#include "tag/MixRamp.hxx"
|
2014-02-07 18:52:19 +01:00
|
|
|
#include "fs/Path.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "util/Domain.hxx"
|
2018-08-07 22:29:51 +02:00
|
|
|
#include "util/ScopeExit.hxx"
|
2015-09-30 22:03:01 +02:00
|
|
|
#include "util/StringView.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2009-08-26 20:08:13 +02:00
|
|
|
|
|
|
|
#include <mpg123.h>
|
2013-11-28 11:50:54 +01:00
|
|
|
|
2011-01-21 14:25:52 +01:00
|
|
|
#include <stdio.h>
|
2009-08-26 20:08:13 +02:00
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
static constexpr Domain mpg123_domain("mpg123");
|
2009-08-26 20:08:13 +02:00
|
|
|
|
|
|
|
static bool
|
2015-01-21 22:13:44 +01:00
|
|
|
mpd_mpg123_init(gcc_unused const ConfigBlock &block)
|
2009-08-26 20:08:13 +02:00
|
|
|
{
|
|
|
|
mpg123_init();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-01-21 11:47:17 +01:00
|
|
|
mpd_mpg123_finish() noexcept
|
2009-08-26 20:08:13 +02:00
|
|
|
{
|
|
|
|
mpg123_exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a file with an existing #mpg123_handle.
|
|
|
|
*
|
|
|
|
* @param handle a handle which was created before; on error, this
|
|
|
|
* function will not free it
|
|
|
|
* @param audio_format this parameter is filled after successful
|
|
|
|
* return
|
|
|
|
* @return true on success
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
mpd_mpg123_open(mpg123_handle *handle, const char *path_fs,
|
2013-08-03 21:00:50 +02:00
|
|
|
AudioFormat &audio_format)
|
2009-08-26 20:08:13 +02:00
|
|
|
{
|
2018-08-07 22:52:38 +02:00
|
|
|
int error = mpg123_open(handle, path_fs);
|
2009-08-26 20:08:13 +02:00
|
|
|
if (error != MPG123_OK) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(mpg123_domain,
|
|
|
|
"libmpg123 failed to open %s: %s",
|
|
|
|
path_fs, mpg123_plain_strerror(error));
|
2009-08-26 20:08:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* obtain the audio format */
|
|
|
|
|
2014-09-24 19:13:20 +02:00
|
|
|
long rate;
|
|
|
|
int channels, encoding;
|
2009-08-26 20:08:13 +02:00
|
|
|
error = mpg123_getformat(handle, &rate, &channels, &encoding);
|
|
|
|
if (error != MPG123_OK) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(mpg123_domain,
|
|
|
|
"mpg123_getformat() failed: %s",
|
|
|
|
mpg123_plain_strerror(error));
|
2009-08-26 20:08:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encoding != MPG123_ENC_SIGNED_16) {
|
|
|
|
/* other formats not yet implemented */
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(mpg123_domain,
|
|
|
|
"expected MPG123_ENC_SIGNED_16, got %d",
|
|
|
|
encoding);
|
2009-08-26 20:08:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-10 11:45:17 +01:00
|
|
|
audio_format = CheckAudioFormat(rate, SampleFormat::S16, channels);
|
2009-08-26 20:08:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-24 20:08:44 +02:00
|
|
|
static void
|
|
|
|
AddTagItem(TagBuilder &tag, TagType type, const mpg123_string &s)
|
|
|
|
{
|
|
|
|
assert(s.p != nullptr);
|
|
|
|
assert(s.size >= s.fill);
|
|
|
|
assert(s.fill > 0);
|
|
|
|
|
2015-09-30 22:03:01 +02:00
|
|
|
tag.AddItem(type, {s.p, s.fill - 1});
|
2014-09-24 20:08:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
AddTagItem(TagBuilder &tag, TagType type, const mpg123_string *s)
|
|
|
|
{
|
|
|
|
if (s != nullptr)
|
|
|
|
AddTagItem(tag, type, *s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-11-18 07:13:35 +01:00
|
|
|
mpd_mpg123_id3v2_tag(DecoderClient &client, const mpg123_id3v2 &id3v2)
|
2014-09-24 20:08:44 +02:00
|
|
|
{
|
|
|
|
TagBuilder tag;
|
|
|
|
|
|
|
|
AddTagItem(tag, TAG_TITLE, id3v2.title);
|
|
|
|
AddTagItem(tag, TAG_ARTIST, id3v2.artist);
|
|
|
|
AddTagItem(tag, TAG_ALBUM, id3v2.album);
|
|
|
|
AddTagItem(tag, TAG_DATE, id3v2.year);
|
|
|
|
AddTagItem(tag, TAG_GENRE, id3v2.genre);
|
|
|
|
|
|
|
|
for (size_t i = 0, n = id3v2.comments; i < n; ++i)
|
|
|
|
AddTagItem(tag, TAG_COMMENT, id3v2.comment_list[i].text);
|
|
|
|
|
2016-11-18 08:27:30 +01:00
|
|
|
client.SubmitTag(nullptr, tag.Commit());
|
2014-09-24 20:08:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-11-18 07:13:35 +01:00
|
|
|
mpd_mpg123_id3v2_extras(DecoderClient &client, const mpg123_id3v2 &id3v2)
|
2014-09-24 20:08:44 +02:00
|
|
|
{
|
|
|
|
ReplayGainInfo replay_gain;
|
|
|
|
replay_gain.Clear();
|
|
|
|
|
|
|
|
MixRampInfo mix_ramp;
|
|
|
|
|
|
|
|
bool found_replay_gain = false, found_mixramp = false;
|
|
|
|
|
|
|
|
for (size_t i = 0, n = id3v2.extras; i < n; ++i) {
|
|
|
|
if (ParseReplayGainTag(replay_gain,
|
|
|
|
id3v2.extra[i].description.p,
|
|
|
|
id3v2.extra[i].text.p))
|
|
|
|
found_replay_gain = true;
|
|
|
|
else if (ParseMixRampTag(mix_ramp,
|
|
|
|
id3v2.extra[i].description.p,
|
|
|
|
id3v2.extra[i].text.p))
|
|
|
|
found_mixramp = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found_replay_gain)
|
2016-11-18 08:27:30 +01:00
|
|
|
client.SubmitReplayGain(&replay_gain);
|
2014-09-24 20:08:44 +02:00
|
|
|
|
|
|
|
if (found_mixramp)
|
2016-11-18 08:27:30 +01:00
|
|
|
client.SubmitMixRamp(std::move(mix_ramp));
|
2014-09-24 20:08:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-11-18 07:13:35 +01:00
|
|
|
mpd_mpg123_id3v2(DecoderClient &client, const mpg123_id3v2 &id3v2)
|
2014-09-24 20:08:44 +02:00
|
|
|
{
|
2016-11-18 07:13:35 +01:00
|
|
|
mpd_mpg123_id3v2_tag(client, id3v2);
|
|
|
|
mpd_mpg123_id3v2_extras(client, id3v2);
|
2014-09-24 20:08:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-11-18 07:13:35 +01:00
|
|
|
mpd_mpg123_meta(DecoderClient &client, mpg123_handle *const handle)
|
2014-09-24 20:08:44 +02:00
|
|
|
{
|
|
|
|
if ((mpg123_meta_check(handle) & MPG123_NEW_ID3) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mpg123_id3v1 *v1;
|
|
|
|
mpg123_id3v2 *v2;
|
|
|
|
if (mpg123_id3(handle, &v1, &v2) != MPG123_OK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (v2 != nullptr)
|
2016-11-18 07:13:35 +01:00
|
|
|
mpd_mpg123_id3v2(client, *v2);
|
2014-09-24 20:08:44 +02:00
|
|
|
}
|
|
|
|
|
2009-08-26 20:08:13 +02:00
|
|
|
static void
|
2016-11-18 07:13:35 +01:00
|
|
|
mpd_mpg123_file_decode(DecoderClient &client, Path path_fs)
|
2009-08-26 20:08:13 +02:00
|
|
|
{
|
|
|
|
/* open the file */
|
|
|
|
|
2014-09-24 19:13:20 +02:00
|
|
|
int error;
|
|
|
|
mpg123_handle *const handle = mpg123_new(nullptr, &error);
|
2013-07-28 12:45:48 +02:00
|
|
|
if (handle == nullptr) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatError(mpg123_domain,
|
|
|
|
"mpg123_new() failed: %s",
|
|
|
|
mpg123_plain_strerror(error));
|
2009-08-26 20:08:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-07 22:29:51 +02:00
|
|
|
AtScopeExit(handle) { mpg123_delete(handle); };
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
AudioFormat audio_format;
|
2018-08-07 22:29:51 +02:00
|
|
|
if (!mpd_mpg123_open(handle, path_fs.c_str(), audio_format))
|
2009-08-26 20:08:13 +02:00
|
|
|
return;
|
|
|
|
|
2014-09-24 19:13:20 +02:00
|
|
|
const off_t num_samples = mpg123_length(handle);
|
2009-08-26 20:08:13 +02:00
|
|
|
|
|
|
|
/* tell MPD core we're ready */
|
|
|
|
|
2014-08-29 20:52:39 +02:00
|
|
|
const auto duration =
|
|
|
|
SongTime::FromScale<uint64_t>(num_samples,
|
|
|
|
audio_format.sample_rate);
|
|
|
|
|
2016-11-18 07:59:01 +01:00
|
|
|
client.Ready(audio_format, true, duration);
|
2009-08-26 20:08:13 +02:00
|
|
|
|
2014-09-24 19:13:20 +02:00
|
|
|
struct mpg123_frameinfo info;
|
2011-01-21 14:25:46 +01:00
|
|
|
if (mpg123_info(handle, &info) != MPG123_OK) {
|
|
|
|
info.vbr = MPG123_CBR;
|
|
|
|
info.bitrate = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (info.vbr) {
|
|
|
|
case MPG123_ABR:
|
|
|
|
info.bitrate = info.abr_rate;
|
|
|
|
break;
|
|
|
|
case MPG123_CBR:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
info.bitrate = 0;
|
|
|
|
}
|
|
|
|
|
2009-08-26 20:08:13 +02:00
|
|
|
/* the decoder main loop */
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand cmd;
|
2009-08-26 20:08:13 +02:00
|
|
|
do {
|
2014-09-24 20:08:44 +02:00
|
|
|
/* read metadata */
|
2016-11-18 07:13:35 +01:00
|
|
|
mpd_mpg123_meta(client, handle);
|
2014-09-24 20:08:44 +02:00
|
|
|
|
2009-08-26 20:08:13 +02:00
|
|
|
/* decode */
|
|
|
|
|
2014-09-24 19:13:20 +02:00
|
|
|
unsigned char buffer[8192];
|
|
|
|
size_t nbytes;
|
2009-08-26 20:08:13 +02:00
|
|
|
error = mpg123_read(handle, buffer, sizeof(buffer), &nbytes);
|
|
|
|
if (error != MPG123_OK) {
|
|
|
|
if (error != MPG123_DONE)
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(mpg123_domain,
|
|
|
|
"mpg123_read() failed: %s",
|
|
|
|
mpg123_plain_strerror(error));
|
2009-08-26 20:08:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-01-21 14:25:46 +01:00
|
|
|
/* update bitrate for ABR/VBR */
|
|
|
|
if (info.vbr != MPG123_CBR) {
|
|
|
|
/* FIXME: maybe skip, as too expensive? */
|
|
|
|
/* FIXME: maybe, (info.vbr == MPG123_VBR) ? */
|
|
|
|
if (mpg123_info (handle, &info) != MPG123_OK)
|
|
|
|
info.bitrate = 0;
|
|
|
|
}
|
|
|
|
|
2009-08-26 20:08:13 +02:00
|
|
|
/* send to MPD */
|
|
|
|
|
2016-11-18 08:27:30 +01:00
|
|
|
cmd = client.SubmitData(nullptr, buffer, nbytes, info.bitrate);
|
2009-08-26 20:08:13 +02:00
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
if (cmd == DecoderCommand::SEEK) {
|
2016-11-18 08:15:07 +01:00
|
|
|
off_t c = client.GetSeekFrame();
|
2011-01-21 14:25:52 +01:00
|
|
|
c = mpg123_seek(handle, c, SEEK_SET);
|
|
|
|
if (c < 0)
|
2016-11-18 08:15:07 +01:00
|
|
|
client.SeekError();
|
2011-01-21 14:25:52 +01:00
|
|
|
else {
|
2016-11-18 08:15:07 +01:00
|
|
|
client.CommandFinished();
|
2018-09-21 19:32:35 +02:00
|
|
|
client.SubmitTimestamp(audio_format.FramesToTime<FloatDuration>(c));
|
2011-01-21 14:25:52 +01:00
|
|
|
}
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
cmd = DecoderCommand::NONE;
|
2011-01-21 14:25:52 +01:00
|
|
|
}
|
2013-09-27 12:11:37 +02:00
|
|
|
} while (cmd == DecoderCommand::NONE);
|
2009-08-26 20:08:13 +02:00
|
|
|
}
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
static bool
|
2018-07-05 19:07:05 +02:00
|
|
|
mpd_mpg123_scan_file(Path path_fs, TagHandler &handler) noexcept
|
2009-08-26 20:08:13 +02:00
|
|
|
{
|
|
|
|
int error;
|
2014-09-24 19:13:20 +02:00
|
|
|
mpg123_handle *const handle = mpg123_new(nullptr, &error);
|
2013-07-28 12:45:48 +02:00
|
|
|
if (handle == nullptr) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatError(mpg123_domain,
|
|
|
|
"mpg123_new() failed: %s",
|
|
|
|
mpg123_plain_strerror(error));
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2009-08-26 20:08:13 +02:00
|
|
|
}
|
|
|
|
|
2018-08-07 22:48:11 +02:00
|
|
|
AtScopeExit(handle) { mpg123_delete(handle); };
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
AudioFormat audio_format;
|
2018-01-25 13:01:39 +01:00
|
|
|
try {
|
|
|
|
if (!mpd_mpg123_open(handle, path_fs.c_str(), audio_format)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (...) {
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2009-08-26 20:08:13 +02:00
|
|
|
}
|
|
|
|
|
2014-09-24 19:13:20 +02:00
|
|
|
const off_t num_samples = mpg123_length(handle);
|
2009-08-26 20:08:13 +02:00
|
|
|
if (num_samples <= 0) {
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2009-08-26 20:08:13 +02:00
|
|
|
}
|
|
|
|
|
2018-07-07 13:08:06 +02:00
|
|
|
handler.OnAudioFormat(audio_format);
|
|
|
|
|
2009-08-26 20:08:13 +02:00
|
|
|
/* ID3 tag support not yet implemented */
|
|
|
|
|
2014-08-29 22:43:36 +02:00
|
|
|
const auto duration =
|
|
|
|
SongTime::FromScale<uint64_t>(num_samples,
|
|
|
|
audio_format.sample_rate);
|
|
|
|
|
2018-07-05 19:07:05 +02:00
|
|
|
handler.OnDuration(duration);
|
2012-02-11 19:12:02 +01:00
|
|
|
return true;
|
2009-08-26 20:08:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *const mpg123_suffixes[] = {
|
|
|
|
"mp3",
|
2013-07-28 12:45:48 +02:00
|
|
|
nullptr
|
2009-08-26 20:08:13 +02:00
|
|
|
};
|
|
|
|
|
2013-10-21 20:31:34 +02:00
|
|
|
const struct DecoderPlugin mpg123_decoder_plugin = {
|
2013-07-28 12:45:48 +02:00
|
|
|
"mpg123",
|
|
|
|
mpd_mpg123_init,
|
|
|
|
mpd_mpg123_finish,
|
2009-08-26 20:08:13 +02:00
|
|
|
/* streaming not yet implemented */
|
2013-07-28 12:45:48 +02:00
|
|
|
nullptr,
|
|
|
|
mpd_mpg123_file_decode,
|
|
|
|
mpd_mpg123_scan_file,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
mpg123_suffixes,
|
|
|
|
nullptr,
|
2009-08-26 20:08:13 +02:00
|
|
|
};
|