2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2020-01-18 19:22:19 +01:00
|
|
|
* Copyright 2003-2020 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
2004-03-21 22:32:23 +01:00
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2004-03-21 22:32:23 +01:00
|
|
|
*/
|
|
|
|
|
2013-04-17 22:33:59 +02:00
|
|
|
#include "FaadDecoderPlugin.hxx"
|
2014-01-24 00:02:24 +01:00
|
|
|
#include "../DecoderAPI.hxx"
|
|
|
|
#include "../DecoderBuffer.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "input/InputStream.hxx"
|
2020-01-18 20:07:09 +01:00
|
|
|
#include "pcm/CheckAudioFormat.hxx"
|
2017-02-08 08:26:58 +01:00
|
|
|
#include "tag/Handler.hxx"
|
2016-11-10 12:17:05 +01:00
|
|
|
#include "util/ScopeExit.hxx"
|
2014-01-06 21:38:25 +01:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Domain.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2004-03-21 22:32:23 +01:00
|
|
|
|
2013-01-29 16:17:15 +01:00
|
|
|
#include <neaacdec.h>
|
|
|
|
|
2018-09-22 19:08:03 +02:00
|
|
|
#include <cmath>
|
2016-09-09 18:47:42 +02:00
|
|
|
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <assert.h>
|
2013-07-30 20:11:57 +02:00
|
|
|
#include <string.h>
|
2008-11-21 20:13:26 +01:00
|
|
|
|
2009-02-16 19:31:11 +01:00
|
|
|
static const unsigned adts_sample_rates[] =
|
2006-07-20 18:02:40 +02:00
|
|
|
{ 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
|
|
|
|
16000, 12000, 11025, 8000, 7350, 0, 0, 0
|
|
|
|
};
|
2004-03-21 22:32:23 +01:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain faad_decoder_domain("faad_decoder");
|
2009-11-10 19:01:38 +01:00
|
|
|
|
2008-08-26 08:27:11 +02:00
|
|
|
/**
|
|
|
|
* Check whether the buffer head is an AAC frame, and return the frame
|
|
|
|
* length. Returns 0 if it is not a frame.
|
|
|
|
*/
|
2009-02-16 19:31:11 +01:00
|
|
|
static size_t
|
2009-02-17 22:56:42 +01:00
|
|
|
adts_check_frame(const unsigned char *data)
|
2008-08-26 08:27:11 +02:00
|
|
|
{
|
|
|
|
/* check syncword */
|
2009-02-17 22:56:42 +01:00
|
|
|
if (!((data[0] == 0xFF) && ((data[1] & 0xF6) == 0xF0)))
|
2008-08-26 08:27:11 +02:00
|
|
|
return 0;
|
|
|
|
|
2009-02-17 22:56:42 +01:00
|
|
|
return (((unsigned int)data[3] & 0x3) << 11) |
|
|
|
|
(((unsigned int)data[4]) << 3) |
|
|
|
|
(data[5] >> 5);
|
2008-08-26 08:27:11 +02:00
|
|
|
}
|
|
|
|
|
2008-08-26 08:27:11 +02:00
|
|
|
/**
|
|
|
|
* Find the next AAC frame in the buffer. Returns 0 if no frame is
|
|
|
|
* found or if not enough data is available.
|
|
|
|
*/
|
2009-02-16 19:31:11 +01:00
|
|
|
static size_t
|
2014-09-22 08:30:25 +02:00
|
|
|
adts_find_frame(DecoderBuffer &buffer)
|
2008-08-26 08:27:11 +02:00
|
|
|
{
|
2009-02-17 22:56:42 +01:00
|
|
|
while (true) {
|
2014-09-22 08:26:04 +02:00
|
|
|
auto data = ConstBuffer<uint8_t>::FromVoid(buffer.Need(8));
|
2014-07-12 01:33:12 +02:00
|
|
|
if (data.IsNull())
|
|
|
|
/* failed */
|
|
|
|
return 0;
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2009-02-17 22:56:42 +01:00
|
|
|
/* find the 0xff marker */
|
2014-01-06 21:38:25 +01:00
|
|
|
const uint8_t *p = (const uint8_t *)
|
|
|
|
memchr(data.data, 0xff, data.size);
|
2013-04-17 22:33:59 +02:00
|
|
|
if (p == nullptr) {
|
2009-02-17 22:56:42 +01:00
|
|
|
/* no marker - discard the buffer */
|
2014-09-22 08:26:04 +02:00
|
|
|
buffer.Clear();
|
2009-02-17 22:56:42 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-01-06 21:38:25 +01:00
|
|
|
if (p > data.data) {
|
2009-02-17 22:56:42 +01:00
|
|
|
/* discard data before 0xff */
|
2014-09-22 08:26:04 +02:00
|
|
|
buffer.Consume(p - data.data);
|
2009-02-17 22:56:42 +01:00
|
|
|
continue;
|
|
|
|
}
|
2008-08-26 08:27:11 +02:00
|
|
|
|
|
|
|
/* is it a frame? */
|
2014-07-12 01:27:18 +02:00
|
|
|
const size_t frame_length = adts_check_frame(data.data);
|
2009-02-17 22:56:42 +01:00
|
|
|
if (frame_length == 0) {
|
|
|
|
/* it's just some random 0xff byte; discard it
|
|
|
|
and continue searching */
|
2014-09-22 08:26:04 +02:00
|
|
|
buffer.Consume(1);
|
2009-02-17 22:56:42 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-09-22 08:26:04 +02:00
|
|
|
if (buffer.Need(frame_length).IsNull()) {
|
2014-07-12 01:33:12 +02:00
|
|
|
/* not enough data; discard this frame to
|
|
|
|
prevent a possible buffer overflow */
|
2014-09-22 08:26:04 +02:00
|
|
|
buffer.Clear();
|
2009-02-17 22:56:42 +01:00
|
|
|
continue;
|
|
|
|
}
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2009-02-17 22:56:42 +01:00
|
|
|
/* found a full frame! */
|
|
|
|
return frame_length;
|
|
|
|
}
|
2008-08-26 08:27:11 +02:00
|
|
|
}
|
|
|
|
|
2014-08-29 20:52:39 +02:00
|
|
|
static SignedSongTime
|
2014-09-22 08:30:25 +02:00
|
|
|
adts_song_duration(DecoderBuffer &buffer)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2014-09-22 08:26:04 +02:00
|
|
|
const InputStream &is = buffer.GetStream();
|
2014-07-11 22:52:26 +02:00
|
|
|
const bool estimate = !is.CheapSeeking();
|
2014-08-19 20:26:12 +02:00
|
|
|
if (estimate && !is.KnownSize())
|
2014-08-29 20:52:39 +02:00
|
|
|
return SignedSongTime::Negative();
|
2014-07-11 22:52:26 +02:00
|
|
|
|
2008-11-12 08:16:38 +01:00
|
|
|
unsigned sample_rate = 0;
|
2004-03-21 22:32:23 +01:00
|
|
|
|
|
|
|
/* Read all frames to ensure correct time and bitrate */
|
2014-01-06 21:48:10 +01:00
|
|
|
unsigned frames = 0;
|
|
|
|
for (;; frames++) {
|
2014-07-11 22:52:31 +02:00
|
|
|
const unsigned frame_length = adts_find_frame(buffer);
|
2009-02-17 22:56:42 +01:00
|
|
|
if (frame_length == 0)
|
|
|
|
break;
|
2004-03-21 22:32:23 +01:00
|
|
|
|
2009-02-17 22:56:42 +01:00
|
|
|
if (frames == 0) {
|
2014-09-22 08:26:04 +02:00
|
|
|
auto data = ConstBuffer<uint8_t>::FromVoid(buffer.Read());
|
2017-11-10 19:24:33 +01:00
|
|
|
assert(!data.empty());
|
2014-01-06 21:38:25 +01:00
|
|
|
assert(frame_length <= data.size);
|
2009-02-17 22:56:42 +01:00
|
|
|
|
2014-01-06 21:38:25 +01:00
|
|
|
sample_rate = adts_sample_rates[(data.data[2] & 0x3c) >> 2];
|
2014-07-12 00:21:52 +02:00
|
|
|
if (sample_rate == 0)
|
|
|
|
break;
|
2009-02-17 22:56:42 +01:00
|
|
|
}
|
|
|
|
|
2014-09-22 08:26:04 +02:00
|
|
|
buffer.Consume(frame_length);
|
2014-07-11 22:52:26 +02:00
|
|
|
|
|
|
|
if (estimate && frames == 128) {
|
|
|
|
/* if this is a remote file, don't slurp the
|
|
|
|
whole file just for checking the song
|
|
|
|
duration; instead, stop after some time and
|
|
|
|
extrapolate the song duration from what we
|
|
|
|
have until now */
|
|
|
|
|
|
|
|
const auto offset = is.GetOffset()
|
2014-09-22 08:26:04 +02:00
|
|
|
- buffer.GetAvailable();
|
2014-07-11 22:52:26 +02:00
|
|
|
if (offset <= 0)
|
2014-08-29 20:52:39 +02:00
|
|
|
return SignedSongTime::Negative();
|
2014-07-11 22:52:26 +02:00
|
|
|
|
2014-08-19 20:26:12 +02:00
|
|
|
const auto file_size = is.GetSize();
|
2014-07-11 22:52:26 +02:00
|
|
|
frames = (frames * file_size) / offset;
|
|
|
|
break;
|
|
|
|
}
|
2004-03-21 22:32:23 +01:00
|
|
|
}
|
|
|
|
|
2014-07-11 23:12:08 +02:00
|
|
|
if (sample_rate == 0)
|
2014-08-29 20:52:39 +02:00
|
|
|
return SignedSongTime::Negative();
|
2009-02-17 22:54:01 +01:00
|
|
|
|
2014-08-29 20:52:39 +02:00
|
|
|
return SignedSongTime::FromScale<uint64_t>(frames * uint64_t(1024),
|
|
|
|
sample_rate);
|
2004-03-21 22:32:23 +01:00
|
|
|
}
|
|
|
|
|
2014-08-29 20:52:39 +02:00
|
|
|
static SignedSongTime
|
2014-09-22 08:30:25 +02:00
|
|
|
faad_song_duration(DecoderBuffer &buffer, InputStream &is)
|
2008-08-26 08:27:11 +02:00
|
|
|
{
|
2014-09-22 08:26:04 +02:00
|
|
|
auto data = ConstBuffer<uint8_t>::FromVoid(buffer.Need(5));
|
2014-07-12 01:33:12 +02:00
|
|
|
if (data.IsNull())
|
2014-08-29 20:52:39 +02:00
|
|
|
return SignedSongTime::Negative();
|
2004-03-21 22:32:23 +01:00
|
|
|
|
2014-01-06 21:48:10 +01:00
|
|
|
size_t tagsize = 0;
|
2014-01-06 21:38:25 +01:00
|
|
|
if (data.size >= 10 && !memcmp(data.data, "ID3", 3)) {
|
2009-02-17 23:42:06 +01:00
|
|
|
/* skip the ID3 tag */
|
|
|
|
|
2014-01-06 21:38:25 +01:00
|
|
|
tagsize = (data.data[6] << 21) | (data.data[7] << 14) |
|
|
|
|
(data.data[8] << 7) | (data.data[9] << 0);
|
2004-03-21 22:32:23 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
tagsize += 10;
|
2009-02-17 22:56:42 +01:00
|
|
|
|
2014-09-22 08:26:04 +02:00
|
|
|
if (!buffer.Skip(tagsize))
|
2014-08-29 20:52:39 +02:00
|
|
|
return SignedSongTime::Negative();
|
2009-09-30 15:22:47 +02:00
|
|
|
|
2014-09-22 08:26:04 +02:00
|
|
|
data = ConstBuffer<uint8_t>::FromVoid(buffer.Need(5));
|
2014-07-12 01:33:12 +02:00
|
|
|
if (data.IsNull())
|
2014-08-29 20:52:39 +02:00
|
|
|
return SignedSongTime::Negative();
|
2004-03-21 22:32:23 +01:00
|
|
|
}
|
|
|
|
|
2014-07-12 01:27:18 +02:00
|
|
|
if (data.size >= 8 && adts_check_frame(data.data) > 0) {
|
2009-02-17 23:42:06 +01:00
|
|
|
/* obtain the duration from the ADTS header */
|
2014-07-11 23:58:51 +02:00
|
|
|
|
|
|
|
if (!is.IsSeekable())
|
2014-08-29 20:52:39 +02:00
|
|
|
return SignedSongTime::Negative();
|
2014-07-11 23:58:51 +02:00
|
|
|
|
2014-08-29 20:52:39 +02:00
|
|
|
auto song_length = adts_song_duration(buffer);
|
2004-03-21 22:32:23 +01:00
|
|
|
|
2016-09-09 18:47:42 +02:00
|
|
|
try {
|
|
|
|
is.LockSeek(tagsize);
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
2016-09-09 18:47:42 +02:00
|
|
|
}
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2014-09-22 08:26:04 +02:00
|
|
|
buffer.Clear();
|
2009-02-17 22:54:01 +01:00
|
|
|
|
2009-02-17 22:56:42 +01:00
|
|
|
return song_length;
|
2014-01-06 21:38:25 +01:00
|
|
|
} else if (data.size >= 5 && memcmp(data.data, "ADIF", 4) == 0) {
|
2009-02-17 23:42:06 +01:00
|
|
|
/* obtain the duration from the ADIF header */
|
2014-08-19 20:26:12 +02:00
|
|
|
|
|
|
|
if (!is.KnownSize())
|
2014-08-29 20:52:39 +02:00
|
|
|
return SignedSongTime::Negative();
|
2014-08-19 20:26:12 +02:00
|
|
|
|
2014-01-06 21:38:25 +01:00
|
|
|
size_t skip_size = (data.data[4] & 0x80) ? 9 : 0;
|
2008-11-12 08:16:38 +01:00
|
|
|
|
2014-01-06 21:38:25 +01:00
|
|
|
if (8 + skip_size > data.size)
|
2008-11-12 08:16:54 +01:00
|
|
|
/* not enough data yet; skip parsing this
|
|
|
|
header */
|
2014-08-29 20:52:39 +02:00
|
|
|
return SignedSongTime::Negative();
|
2008-11-12 08:16:54 +01:00
|
|
|
|
2014-07-12 01:27:18 +02:00
|
|
|
unsigned bit_rate = ((data.data[4 + skip_size] & 0x0F) << 19) |
|
2014-01-06 21:38:25 +01:00
|
|
|
(data.data[5 + skip_size] << 11) |
|
|
|
|
(data.data[6 + skip_size] << 3) |
|
|
|
|
(data.data[7 + skip_size] & 0xE0);
|
2009-02-16 19:31:11 +01:00
|
|
|
|
2014-08-19 20:28:50 +02:00
|
|
|
const auto size = is.GetSize();
|
2014-08-29 21:20:16 +02:00
|
|
|
if (bit_rate == 0)
|
2014-08-29 20:52:39 +02:00
|
|
|
return SignedSongTime::Negative();
|
2014-08-29 21:20:16 +02:00
|
|
|
|
2014-08-29 20:52:39 +02:00
|
|
|
return SongTime::FromScale(size, bit_rate / 8);
|
2009-02-17 22:54:01 +01:00
|
|
|
} else
|
2014-08-29 20:52:39 +02:00
|
|
|
return SignedSongTime::Negative();
|
2004-03-21 22:32:23 +01:00
|
|
|
}
|
|
|
|
|
2014-07-11 23:30:14 +02:00
|
|
|
static NeAACDecHandle
|
|
|
|
faad_decoder_new()
|
|
|
|
{
|
|
|
|
const NeAACDecHandle decoder = NeAACDecOpen();
|
|
|
|
|
|
|
|
NeAACDecConfigurationPtr config =
|
|
|
|
NeAACDecGetCurrentConfiguration(decoder);
|
|
|
|
config->outputFormat = FAAD_FMT_16BIT;
|
|
|
|
config->downMatrix = 1;
|
|
|
|
config->dontUpSampleImplicitSBR = 0;
|
|
|
|
NeAACDecSetConfiguration(decoder, config);
|
|
|
|
|
|
|
|
return decoder;
|
|
|
|
}
|
|
|
|
|
2009-02-17 22:54:26 +01:00
|
|
|
/**
|
2013-01-29 16:17:15 +01:00
|
|
|
* Wrapper for NeAACDecInit() which works around some API
|
2009-02-17 22:54:26 +01:00
|
|
|
* inconsistencies in libfaad.
|
2016-11-10 12:15:49 +01:00
|
|
|
*
|
|
|
|
* Throws #std::runtime_error on error.
|
2009-02-17 22:54:26 +01:00
|
|
|
*/
|
2016-11-10 12:15:49 +01:00
|
|
|
static void
|
2014-09-22 08:30:25 +02:00
|
|
|
faad_decoder_init(NeAACDecHandle decoder, DecoderBuffer &buffer,
|
2016-11-10 12:15:49 +01:00
|
|
|
AudioFormat &audio_format)
|
2009-02-17 22:54:26 +01:00
|
|
|
{
|
2014-09-22 08:26:04 +02:00
|
|
|
auto data = ConstBuffer<uint8_t>::FromVoid(buffer.Read());
|
2017-11-10 19:24:33 +01:00
|
|
|
if (data.empty())
|
2016-11-10 12:15:49 +01:00
|
|
|
throw std::runtime_error("Empty file");
|
2009-02-17 22:56:42 +01:00
|
|
|
|
2014-07-11 22:52:31 +02:00
|
|
|
uint8_t channels;
|
decoder/faad: remove workaround for ancient libfaad2 ABI bug
Many years ago, FAAD had a serious ABI bug: the NeAACDecInit()
prototype in its header declared the "samplerate" parameter to be
"unsigned long *", but internally, the function assumed it was
"uint32_t *" instead. On 32 bit machines, that was no difference, but
on 64 bit, this left one portion of the return value uninitialized;
and worse, on big-endian, the wrong word was filled. This bug had to
be worked around in MPD (commit 9c4e97a6).
A few months later, the bug was fixed in the FAAD CVS in commit 1.117
on file libfaad/decoder.c; the commit message was:
"Use public headers internally to prevent duplicate declarations"
The commit message was too brief at best; the problem was not
duplicate declarations, but a prototype mismatch. No mention of the
bug fix in the ChangeLog.
The MPD project never learned about this bug fix, and so MPD would
always pass a "uin32_t *" dressed up as a "unsigned long *". Nearly 6
years later, it's about time to fix this second ABI problem. Let's
kill the workaround!
2014-10-25 20:42:50 +02:00
|
|
|
unsigned long sample_rate;
|
2014-07-11 22:52:31 +02:00
|
|
|
long nbytes = NeAACDecInit(decoder,
|
|
|
|
/* deconst hack, libfaad requires this */
|
2014-07-12 01:27:18 +02:00
|
|
|
const_cast<unsigned char *>(data.data),
|
|
|
|
data.size,
|
decoder/faad: remove workaround for ancient libfaad2 ABI bug
Many years ago, FAAD had a serious ABI bug: the NeAACDecInit()
prototype in its header declared the "samplerate" parameter to be
"unsigned long *", but internally, the function assumed it was
"uint32_t *" instead. On 32 bit machines, that was no difference, but
on 64 bit, this left one portion of the return value uninitialized;
and worse, on big-endian, the wrong word was filled. This bug had to
be worked around in MPD (commit 9c4e97a6).
A few months later, the bug was fixed in the FAAD CVS in commit 1.117
on file libfaad/decoder.c; the commit message was:
"Use public headers internally to prevent duplicate declarations"
The commit message was too brief at best; the problem was not
duplicate declarations, but a prototype mismatch. No mention of the
bug fix in the ChangeLog.
The MPD project never learned about this bug fix, and so MPD would
always pass a "uin32_t *" dressed up as a "unsigned long *". Nearly 6
years later, it's about time to fix this second ABI problem. Let's
kill the workaround!
2014-10-25 20:42:50 +02:00
|
|
|
&sample_rate, &channels);
|
2016-11-10 12:15:49 +01:00
|
|
|
if (nbytes < 0)
|
|
|
|
throw std::runtime_error("Not an AAC stream");
|
2009-02-17 22:54:26 +01:00
|
|
|
|
2014-09-22 08:26:04 +02:00
|
|
|
buffer.Consume(nbytes);
|
2009-02-17 23:35:49 +01:00
|
|
|
|
2016-11-10 11:45:17 +01:00
|
|
|
audio_format = CheckAudioFormat(sample_rate, SampleFormat::S16,
|
|
|
|
channels);
|
2009-02-17 22:54:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-29 16:17:15 +01:00
|
|
|
* Wrapper for NeAACDecDecode() which works around some API
|
2009-02-17 22:54:26 +01:00
|
|
|
* inconsistencies in libfaad.
|
|
|
|
*/
|
|
|
|
static const void *
|
2014-09-22 08:30:25 +02:00
|
|
|
faad_decoder_decode(NeAACDecHandle decoder, DecoderBuffer &buffer,
|
2013-01-29 16:17:15 +01:00
|
|
|
NeAACDecFrameInfo *frame_info)
|
2009-02-17 22:54:26 +01:00
|
|
|
{
|
2014-09-22 08:26:04 +02:00
|
|
|
auto data = ConstBuffer<uint8_t>::FromVoid(buffer.Read());
|
2017-11-10 19:24:33 +01:00
|
|
|
if (data.empty())
|
2013-04-17 22:33:59 +02:00
|
|
|
return nullptr;
|
2009-02-17 22:56:42 +01:00
|
|
|
|
2013-01-29 16:17:15 +01:00
|
|
|
return NeAACDecDecode(decoder, frame_info,
|
2013-04-17 22:33:59 +02:00
|
|
|
/* deconst hack, libfaad requires this */
|
2014-01-06 21:38:25 +01:00
|
|
|
const_cast<uint8_t *>(data.data),
|
|
|
|
data.size);
|
2009-02-17 22:54:26 +01:00
|
|
|
}
|
|
|
|
|
2009-02-17 23:42:06 +01:00
|
|
|
/**
|
2014-08-29 20:52:39 +02:00
|
|
|
* Determine a song file's total playing time.
|
|
|
|
*
|
|
|
|
* The first return value specifies whether the file was recognized.
|
|
|
|
* The second return value is the duration.
|
2009-02-17 23:42:06 +01:00
|
|
|
*/
|
2014-08-29 20:52:39 +02:00
|
|
|
static std::pair<bool, SignedSongTime>
|
|
|
|
faad_get_file_time(InputStream &is)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2014-09-22 08:29:07 +02:00
|
|
|
DecoderBuffer buffer(nullptr, is,
|
|
|
|
FAAD_MIN_STREAMSIZE * MAX_CHANNELS);
|
2014-09-22 08:30:25 +02:00
|
|
|
auto duration = faad_song_duration(buffer, is);
|
2014-08-29 20:52:39 +02:00
|
|
|
bool recognized = !duration.IsNegative();
|
2004-03-22 05:20:19 +01:00
|
|
|
|
2014-08-29 20:52:39 +02:00
|
|
|
if (!recognized) {
|
2014-07-11 23:30:14 +02:00
|
|
|
NeAACDecHandle decoder = faad_decoder_new();
|
2016-11-10 12:17:05 +01:00
|
|
|
AtScopeExit(decoder) { NeAACDecClose(decoder); };
|
2004-03-22 05:20:19 +01:00
|
|
|
|
2014-09-22 08:26:04 +02:00
|
|
|
buffer.Fill();
|
2009-02-17 22:54:26 +01:00
|
|
|
|
2014-01-06 21:48:10 +01:00
|
|
|
AudioFormat audio_format;
|
2016-11-10 12:15:49 +01:00
|
|
|
try {
|
|
|
|
faad_decoder_init(decoder, buffer, audio_format);
|
2014-08-29 20:52:39 +02:00
|
|
|
recognized = true;
|
2017-12-19 10:56:23 +01:00
|
|
|
} catch (...) {
|
2016-11-10 12:15:49 +01:00
|
|
|
}
|
2004-03-22 05:20:19 +01:00
|
|
|
}
|
2004-03-21 22:32:23 +01:00
|
|
|
|
2014-08-29 20:52:39 +02:00
|
|
|
return std::make_pair(recognized, duration);
|
2004-03-21 22:32:23 +01:00
|
|
|
}
|
|
|
|
|
2008-11-11 17:13:44 +01:00
|
|
|
static void
|
2016-11-18 07:13:35 +01:00
|
|
|
faad_stream_decode(DecoderClient &client, InputStream &is,
|
2014-09-22 08:30:25 +02:00
|
|
|
DecoderBuffer &buffer, const NeAACDecHandle decoder)
|
2008-08-26 08:27:11 +02:00
|
|
|
{
|
2014-08-29 20:52:39 +02:00
|
|
|
const auto total_time = faad_song_duration(buffer, is);
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2014-07-12 01:51:39 +02:00
|
|
|
if (adts_find_frame(buffer) == 0)
|
|
|
|
return;
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2009-02-17 23:42:06 +01:00
|
|
|
/* initialize it */
|
|
|
|
|
2014-01-06 21:48:10 +01:00
|
|
|
AudioFormat audio_format;
|
2016-11-10 12:15:49 +01:00
|
|
|
faad_decoder_init(decoder, buffer, audio_format);
|
2009-02-17 23:26:51 +01:00
|
|
|
|
2009-02-17 23:42:06 +01:00
|
|
|
/* initialize the MPD core */
|
|
|
|
|
2016-11-18 07:59:01 +01:00
|
|
|
client.Ready(audio_format, false, total_time);
|
2009-02-17 23:26:51 +01:00
|
|
|
|
2009-02-17 23:42:06 +01:00
|
|
|
/* the decoder loop */
|
|
|
|
|
2013-09-27 12:11:37 +02:00
|
|
|
DecoderCommand cmd;
|
2014-01-06 21:48:10 +01:00
|
|
|
unsigned bit_rate = 0;
|
2008-11-02 17:02:23 +01:00
|
|
|
do {
|
2009-02-17 23:42:06 +01:00
|
|
|
/* find the next frame */
|
|
|
|
|
2014-07-11 22:52:31 +02:00
|
|
|
const size_t frame_size = adts_find_frame(buffer);
|
2009-02-17 22:56:07 +01:00
|
|
|
if (frame_size == 0)
|
2009-02-17 23:42:06 +01:00
|
|
|
/* end of file */
|
2008-08-26 08:27:11 +02:00
|
|
|
break;
|
|
|
|
|
2009-02-17 23:42:06 +01:00
|
|
|
/* decode it */
|
|
|
|
|
2014-07-11 22:52:31 +02:00
|
|
|
NeAACDecFrameInfo frame_info;
|
|
|
|
const void *const decoded =
|
|
|
|
faad_decoder_decode(decoder, buffer, &frame_info);
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2009-02-16 19:31:11 +01:00
|
|
|
if (frame_info.error > 0) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(faad_decoder_domain,
|
|
|
|
"error decoding AAC stream: %s",
|
|
|
|
NeAACDecGetErrorMessage(frame_info.error));
|
2008-08-26 08:27:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-02-17 23:35:49 +01:00
|
|
|
if (frame_info.channels != audio_format.channels) {
|
2013-11-04 22:20:11 +01:00
|
|
|
FormatDefault(faad_decoder_domain,
|
|
|
|
"channel count changed from %u to %u",
|
|
|
|
audio_format.channels, frame_info.channels);
|
2009-02-17 23:26:51 +01:00
|
|
|
break;
|
|
|
|
}
|
2008-11-21 20:27:30 +01:00
|
|
|
|
2009-02-17 23:35:49 +01:00
|
|
|
if (frame_info.samplerate != audio_format.sample_rate) {
|
2013-11-04 22:20:11 +01:00
|
|
|
FormatDefault(faad_decoder_domain,
|
|
|
|
"sample rate changed from %u to %lu",
|
|
|
|
audio_format.sample_rate,
|
|
|
|
(unsigned long)frame_info.samplerate);
|
2009-02-17 23:26:51 +01:00
|
|
|
break;
|
2008-08-26 08:27:11 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 08:26:04 +02:00
|
|
|
buffer.Consume(frame_info.bytesconsumed);
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2009-02-17 23:42:06 +01:00
|
|
|
/* update bit rate and position */
|
|
|
|
|
2009-02-17 23:44:29 +01:00
|
|
|
if (frame_info.samples > 0) {
|
2018-09-22 19:08:03 +02:00
|
|
|
bit_rate = lround(frame_info.bytesconsumed * 8.0 *
|
|
|
|
frame_info.channels * audio_format.sample_rate /
|
|
|
|
frame_info.samples / 1000);
|
2008-08-26 08:27:11 +02:00
|
|
|
}
|
|
|
|
|
2009-02-17 23:42:06 +01:00
|
|
|
/* send PCM samples to MPD */
|
|
|
|
|
2016-11-18 08:27:30 +01:00
|
|
|
cmd = client.SubmitData(is, decoded,
|
|
|
|
(size_t)frame_info.samples * 2,
|
|
|
|
bit_rate);
|
2013-09-27 12:11:37 +02:00
|
|
|
} while (cmd != DecoderCommand::STOP);
|
2014-07-12 01:47:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-11-18 07:13:35 +01:00
|
|
|
faad_stream_decode(DecoderClient &client, InputStream &is)
|
2014-07-12 01:47:07 +02:00
|
|
|
{
|
2016-11-18 07:13:35 +01:00
|
|
|
DecoderBuffer buffer(&client, is,
|
2014-09-22 08:29:07 +02:00
|
|
|
FAAD_MIN_STREAMSIZE * MAX_CHANNELS);
|
2014-07-12 01:47:07 +02:00
|
|
|
|
|
|
|
/* create the libfaad decoder */
|
|
|
|
|
|
|
|
const NeAACDecHandle decoder = faad_decoder_new();
|
2016-11-10 12:17:05 +01:00
|
|
|
AtScopeExit(decoder) { NeAACDecClose(decoder); };
|
2014-07-12 01:47:07 +02:00
|
|
|
|
2016-11-18 07:13:35 +01:00
|
|
|
faad_stream_decode(client, is, buffer, decoder);
|
2008-08-26 08:27:11 +02:00
|
|
|
}
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
static bool
|
2018-07-05 19:07:05 +02:00
|
|
|
faad_scan_stream(InputStream &is, TagHandler &handler) noexcept
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2014-08-29 20:52:39 +02:00
|
|
|
auto result = faad_get_file_time(is);
|
|
|
|
if (!result.first)
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2004-05-31 04:56:14 +02:00
|
|
|
|
2014-08-29 22:43:36 +02:00
|
|
|
if (!result.second.IsNegative())
|
2018-07-05 19:07:05 +02:00
|
|
|
handler.OnDuration(SongTime(result.second));
|
2012-02-11 19:12:02 +01:00
|
|
|
return true;
|
2004-05-31 04:56:14 +02:00
|
|
|
}
|
|
|
|
|
2013-04-17 22:33:59 +02:00
|
|
|
static const char *const faad_suffixes[] = { "aac", nullptr };
|
2009-02-16 19:31:11 +01:00
|
|
|
static const char *const faad_mime_types[] = {
|
2013-04-17 22:33:59 +02:00
|
|
|
"audio/aac", "audio/aacp", nullptr
|
2009-02-16 19:31:11 +01:00
|
|
|
};
|
2004-05-31 04:56:14 +02:00
|
|
|
|
2019-06-15 14:44:37 +02:00
|
|
|
constexpr DecoderPlugin faad_decoder_plugin =
|
|
|
|
DecoderPlugin("faad", faad_stream_decode, faad_scan_stream)
|
|
|
|
.WithSuffixes(faad_suffixes)
|
|
|
|
.WithMimeTypes(faad_mime_types);
|