2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 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
|
|
|
*/
|
|
|
|
|
2009-01-08 21:37:02 +01:00
|
|
|
#include "config.h"
|
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"
|
2013-07-29 07:50:08 +02:00
|
|
|
#include "CheckAudioFormat.hxx"
|
2013-09-05 18:22:02 +02:00
|
|
|
#include "tag/TagHandler.hxx"
|
2014-01-06 21:38:25 +01:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#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>
|
|
|
|
|
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
|
|
|
#include <unistd.h>
|
|
|
|
|
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
|
2013-04-17 22:45:10 +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-01-06 21:38:25 +01:00
|
|
|
auto data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_read(buffer));
|
|
|
|
if (data.size < 8) {
|
2009-02-17 22:56:42 +01:00
|
|
|
/* not enough data yet */
|
2014-01-06 21:44:21 +01:00
|
|
|
if (!decoder_buffer_fill(buffer))
|
2009-02-17 22:56:42 +01:00
|
|
|
/* failed */
|
|
|
|
return 0;
|
2009-02-17 22:53:25 +01:00
|
|
|
|
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
|
|
|
/* 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-01-06 21:46:10 +01:00
|
|
|
decoder_buffer_clear(buffer);
|
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-01-06 21:38:25 +01:00
|
|
|
decoder_buffer_consume(buffer, 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 */
|
|
|
|
decoder_buffer_consume(buffer, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-01-06 21:38:25 +01:00
|
|
|
if (data.size < frame_length) {
|
2009-02-17 22:56:42 +01:00
|
|
|
/* available buffer size is smaller than the
|
|
|
|
frame will be - attempt to read more
|
|
|
|
data */
|
2014-01-06 21:44:21 +01:00
|
|
|
if (!decoder_buffer_fill(buffer)) {
|
2009-02-17 22:56:42 +01:00
|
|
|
/* not enough data; discard this frame
|
|
|
|
to prevent a possible buffer
|
|
|
|
overflow */
|
2014-01-06 21:46:10 +01:00
|
|
|
decoder_buffer_clear(buffer);
|
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
|
|
|
}
|
|
|
|
|
2009-02-17 22:54:01 +01:00
|
|
|
static float
|
2013-04-17 22:45:10 +02:00
|
|
|
adts_song_duration(DecoderBuffer *buffer)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2014-07-11 22:52:26 +02:00
|
|
|
const InputStream &is = decoder_buffer_get_stream(buffer);
|
|
|
|
const bool estimate = !is.CheapSeeking();
|
|
|
|
const auto file_size = is.GetSize();
|
|
|
|
if (estimate && file_size <= 0)
|
|
|
|
return -1;
|
|
|
|
|
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-01-06 21:38:25 +01:00
|
|
|
auto data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_read(buffer));
|
|
|
|
assert(!data.IsEmpty());
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
decoder_buffer_consume(buffer, 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()
|
|
|
|
- decoder_buffer_available(buffer);
|
|
|
|
if (offset <= 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
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)
|
2009-02-17 22:54:01 +01:00
|
|
|
return -1;
|
|
|
|
|
2014-07-11 23:12:08 +02:00
|
|
|
float frames_per_second = (float)sample_rate / 1024.0;
|
|
|
|
assert(frames_per_second > 0);
|
|
|
|
|
2009-02-17 22:54:01 +01:00
|
|
|
return (float)frames / frames_per_second;
|
2004-03-21 22:32:23 +01:00
|
|
|
}
|
|
|
|
|
2009-02-17 22:54:01 +01:00
|
|
|
static float
|
2013-10-23 22:08:59 +02:00
|
|
|
faad_song_duration(DecoderBuffer *buffer, InputStream &is)
|
2008-08-26 08:27:11 +02:00
|
|
|
{
|
2013-10-23 22:08:59 +02:00
|
|
|
const auto size = is.GetSize();
|
2014-01-06 21:48:10 +01:00
|
|
|
const size_t fileread = size >= 0 ? size : 0;
|
2004-03-21 22:32:23 +01:00
|
|
|
|
2009-02-17 22:56:42 +01:00
|
|
|
decoder_buffer_fill(buffer);
|
2014-01-06 21:38:25 +01:00
|
|
|
auto data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_read(buffer));
|
|
|
|
if (data.IsEmpty())
|
2009-02-17 22:56:42 +01:00
|
|
|
return -1;
|
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-07-11 22:52:31 +02:00
|
|
|
const bool success = decoder_buffer_skip(buffer, tagsize) &&
|
2009-09-30 15:22:47 +02:00
|
|
|
decoder_buffer_fill(buffer);
|
|
|
|
if (!success)
|
|
|
|
return -1;
|
|
|
|
|
2014-01-06 21:38:25 +01:00
|
|
|
data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_read(buffer));
|
|
|
|
if (data.IsEmpty())
|
2009-02-17 22:56:42 +01:00
|
|
|
return -1;
|
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())
|
|
|
|
return -1;
|
|
|
|
|
2009-02-17 22:56:42 +01:00
|
|
|
float song_length = adts_song_duration(buffer);
|
2004-03-21 22:32:23 +01:00
|
|
|
|
2014-05-22 10:10:16 +02:00
|
|
|
is.LockSeek(tagsize, IgnoreError());
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2014-01-06 21:46:10 +01:00
|
|
|
decoder_buffer_clear(buffer);
|
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-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 */
|
2009-02-17 22:54:01 +01:00
|
|
|
return -1;
|
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
|
|
|
|
|
|
|
if (fileread != 0 && bit_rate != 0)
|
2009-02-17 22:54:01 +01:00
|
|
|
return fileread * 8.0 / bit_rate;
|
2006-08-25 02:29:41 +02:00
|
|
|
else
|
2009-02-17 22:54:01 +01:00
|
|
|
return fileread;
|
|
|
|
} else
|
|
|
|
return -1;
|
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.
|
|
|
|
*/
|
|
|
|
static bool
|
2013-04-17 22:45:10 +02:00
|
|
|
faad_decoder_init(NeAACDecHandle decoder, DecoderBuffer *buffer,
|
2013-08-10 18:02:44 +02:00
|
|
|
AudioFormat &audio_format, Error &error)
|
2009-02-17 22:54:26 +01:00
|
|
|
{
|
2014-07-12 01:27:18 +02:00
|
|
|
auto data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_read(buffer));
|
|
|
|
if (data.IsEmpty()) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(faad_decoder_domain, "Empty file");
|
2009-02-17 22:56:42 +01:00
|
|
|
return false;
|
2009-11-10 19:01:38 +01:00
|
|
|
}
|
2009-02-17 22:56:42 +01:00
|
|
|
|
2014-07-11 22:52:31 +02:00
|
|
|
uint8_t channels;
|
2009-02-17 23:35:49 +01:00
|
|
|
uint32_t sample_rate;
|
2009-02-17 22:54:26 +01:00
|
|
|
#ifdef HAVE_FAAD_LONG
|
|
|
|
/* neaacdec.h declares all arguments as "unsigned long", but
|
|
|
|
internally expects uint32_t pointers. To avoid gcc
|
|
|
|
warnings, use this workaround. */
|
2009-11-10 19:01:38 +01:00
|
|
|
unsigned long *sample_rate_p = (unsigned long *)(void *)&sample_rate;
|
2009-02-17 22:54:26 +01:00
|
|
|
#else
|
2009-11-10 19:01:38 +01:00
|
|
|
uint32_t *sample_rate_p = &sample_rate;
|
2009-02-17 22:54:26 +01:00
|
|
|
#endif
|
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,
|
2014-07-11 22:52:31 +02:00
|
|
|
sample_rate_p, &channels);
|
2009-11-10 19:01:38 +01:00
|
|
|
if (nbytes < 0) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(faad_decoder_domain, "Not an AAC stream");
|
2009-02-17 22:54:26 +01:00
|
|
|
return false;
|
2009-11-10 19:01:38 +01:00
|
|
|
}
|
2009-02-17 22:54:26 +01:00
|
|
|
|
2009-02-17 22:56:42 +01:00
|
|
|
decoder_buffer_consume(buffer, nbytes);
|
2009-02-17 23:35:49 +01:00
|
|
|
|
2009-11-10 19:01:38 +01:00
|
|
|
return audio_format_init_checked(audio_format, sample_rate,
|
2013-08-10 18:02:44 +02:00
|
|
|
SampleFormat::S16, channels, error);
|
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 *
|
2013-04-17 22:45:10 +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-01-06 21:38:25 +01:00
|
|
|
auto data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_read(buffer));
|
|
|
|
if (data.IsEmpty())
|
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
|
|
|
/**
|
|
|
|
* Get a song file's total playing time in seconds, as a float.
|
|
|
|
* Returns 0 if the duration is unknown, and a negative value if the
|
|
|
|
* file is invalid.
|
|
|
|
*/
|
2009-02-16 19:31:11 +01:00
|
|
|
static float
|
2013-10-23 22:08:59 +02:00
|
|
|
faad_get_file_time_float(InputStream &is)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2014-01-06 21:48:10 +01:00
|
|
|
DecoderBuffer *buffer =
|
|
|
|
decoder_buffer_new(nullptr, is,
|
2014-07-11 22:52:31 +02:00
|
|
|
FAAD_MIN_STREAMSIZE * MAX_CHANNELS);
|
2014-01-06 21:48:10 +01:00
|
|
|
float length = faad_song_duration(buffer, is);
|
2004-03-22 05:20:19 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (length < 0) {
|
2014-07-11 23:30:14 +02:00
|
|
|
NeAACDecHandle decoder = faad_decoder_new();
|
2004-03-22 05:20:19 +01:00
|
|
|
|
2009-02-17 22:56:42 +01:00
|
|
|
decoder_buffer_fill(buffer);
|
2009-02-17 22:54:26 +01:00
|
|
|
|
2014-01-06 21:48:10 +01:00
|
|
|
AudioFormat audio_format;
|
|
|
|
if (faad_decoder_init(decoder, buffer, audio_format,
|
|
|
|
IgnoreError()))
|
2006-07-20 18:02:40 +02:00
|
|
|
length = 0;
|
2004-03-22 05:20:19 +01:00
|
|
|
|
2013-01-29 16:17:15 +01:00
|
|
|
NeAACDecClose(decoder);
|
2004-03-22 05:20:19 +01:00
|
|
|
}
|
2004-03-21 22:32:23 +01:00
|
|
|
|
2009-02-17 22:56:42 +01:00
|
|
|
decoder_buffer_free(buffer);
|
2004-03-21 22:32:23 +01:00
|
|
|
|
2004-03-22 05:20:19 +01:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2009-02-17 23:42:06 +01:00
|
|
|
/**
|
|
|
|
* Get a song file's total playing time in seconds, as an int.
|
|
|
|
* Returns 0 if the duration is unknown, and a negative value if the
|
|
|
|
* file is invalid.
|
|
|
|
*/
|
2009-02-16 19:31:11 +01:00
|
|
|
static int
|
2013-10-23 22:08:59 +02:00
|
|
|
faad_get_file_time(InputStream &is)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2014-07-11 22:52:31 +02:00
|
|
|
float length = faad_get_file_time_float(is);
|
|
|
|
if (length < 0)
|
|
|
|
return -1;
|
2004-03-22 05:20:19 +01:00
|
|
|
|
2014-07-11 22:52:31 +02:00
|
|
|
return int(length + 0.5);
|
2004-03-21 22:32:23 +01:00
|
|
|
}
|
|
|
|
|
2008-11-11 17:13:44 +01:00
|
|
|
static void
|
2013-10-23 22:08:59 +02:00
|
|
|
faad_stream_decode(Decoder &mpd_decoder, InputStream &is)
|
2008-08-26 08:27:11 +02:00
|
|
|
{
|
2014-01-06 21:48:10 +01:00
|
|
|
DecoderBuffer *buffer =
|
|
|
|
decoder_buffer_new(&mpd_decoder, is,
|
2014-07-11 22:52:31 +02:00
|
|
|
FAAD_MIN_STREAMSIZE * MAX_CHANNELS);
|
2014-01-06 21:48:10 +01:00
|
|
|
const float total_time = faad_song_duration(buffer, is);
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2009-02-17 23:42:06 +01:00
|
|
|
/* create the libfaad decoder */
|
|
|
|
|
2014-07-11 23:30:14 +02:00
|
|
|
const NeAACDecHandle decoder = faad_decoder_new();
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
while (!decoder_buffer_is_full(buffer) && !is.LockIsEOF() &&
|
2013-09-27 12:11:37 +02:00
|
|
|
decoder_get_command(mpd_decoder) == DecoderCommand::NONE) {
|
2009-02-17 22:56:42 +01:00
|
|
|
adts_find_frame(buffer);
|
|
|
|
decoder_buffer_fill(buffer);
|
2008-08-26 08:27:11 +02:00
|
|
|
}
|
|
|
|
|
2009-02-17 23:42:06 +01:00
|
|
|
/* initialize it */
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
Error error;
|
2014-01-06 21:48:10 +01:00
|
|
|
AudioFormat audio_format;
|
|
|
|
if (!faad_decoder_init(decoder, buffer, audio_format, error)) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(error);
|
2013-01-29 16:17:15 +01:00
|
|
|
NeAACDecClose(decoder);
|
2014-01-08 22:11:00 +01:00
|
|
|
decoder_buffer_free(buffer);
|
2009-02-17 23:26:51 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-02-17 23:42:06 +01:00
|
|
|
/* initialize the MPD core */
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
decoder_initialized(mpd_decoder, 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
|
|
|
}
|
|
|
|
|
2009-02-17 22:56:42 +01:00
|
|
|
decoder_buffer_consume(buffer, 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) {
|
2009-02-16 19:31:11 +01:00
|
|
|
bit_rate = frame_info.bytesconsumed * 8.0 *
|
2009-02-17 23:35:49 +01:00
|
|
|
frame_info.channels * audio_format.sample_rate /
|
2009-02-16 19:31:11 +01:00
|
|
|
frame_info.samples / 1000 + 0.5;
|
2008-08-26 08:27:11 +02:00
|
|
|
}
|
|
|
|
|
2009-02-17 23:42:06 +01:00
|
|
|
/* send PCM samples to MPD */
|
|
|
|
|
2009-02-16 19:31:11 +01:00
|
|
|
cmd = decoder_data(mpd_decoder, is, decoded,
|
2009-12-25 19:47:33 +01:00
|
|
|
(size_t)frame_info.samples * 2,
|
2010-01-03 22:44:23 +01:00
|
|
|
bit_rate);
|
2013-09-27 12:11:37 +02:00
|
|
|
} while (cmd != DecoderCommand::STOP);
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2009-02-17 23:42:06 +01:00
|
|
|
/* cleanup */
|
|
|
|
|
2013-01-29 16:17:15 +01:00
|
|
|
NeAACDecClose(decoder);
|
2014-01-08 22:11:00 +01:00
|
|
|
decoder_buffer_free(buffer);
|
2008-08-26 08:27:11 +02:00
|
|
|
}
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
faad_scan_stream(InputStream &is,
|
2012-02-11 19:12:02 +01:00
|
|
|
const struct tag_handler *handler, void *handler_ctx)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-12-31 18:20:08 +01:00
|
|
|
int file_time = faad_get_file_time(is);
|
|
|
|
if (file_time < 0)
|
2012-02-11 19:12:02 +01:00
|
|
|
return false;
|
2004-05-31 04:56:14 +02:00
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
tag_handler_invoke_duration(handler, handler_ctx, file_time);
|
|
|
|
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
|
|
|
|
2014-01-06 21:48:10 +01:00
|
|
|
const DecoderPlugin faad_decoder_plugin = {
|
2013-04-17 22:33:59 +02:00
|
|
|
"faad",
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
faad_stream_decode,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
faad_scan_stream,
|
|
|
|
nullptr,
|
|
|
|
faad_suffixes,
|
|
|
|
faad_mime_types,
|
2004-05-31 04:56:14 +02:00
|
|
|
};
|