2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-04-17 22:33:59 +02:00
|
|
|
* Copyright (C) 2003-2013 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"
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "decoder_api.h"
|
2013-04-17 22:33:59 +02:00
|
|
|
extern "C" {
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "decoder_buffer.h"
|
2013-04-17 22:33:59 +02:00
|
|
|
}
|
2009-11-10 19:01:38 +01:00
|
|
|
#include "audio_check.h"
|
2012-02-11 19:12:02 +01:00
|
|
|
#include "tag_handler.h"
|
2004-03-21 22:32:23 +01:00
|
|
|
|
2013-01-29 16:17:15 +01:00
|
|
|
#include <neaacdec.h>
|
|
|
|
|
|
|
|
#include <glib.h>
|
2004-03-21 22:32:23 +01:00
|
|
|
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <assert.h>
|
2008-11-21 20:13:26 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#undef G_LOG_DOMAIN
|
2009-02-17 08:51:34 +01:00
|
|
|
#define G_LOG_DOMAIN "faad"
|
2004-03-21 22:32:23 +01:00
|
|
|
|
2013-01-29 16:17:15 +01:00
|
|
|
#define AAC_MAX_CHANNELS 6
|
|
|
|
|
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
|
|
|
|
2009-11-10 19:01:38 +01:00
|
|
|
/**
|
|
|
|
* The GLib quark used for errors reported by this plugin.
|
|
|
|
*/
|
|
|
|
static inline GQuark
|
|
|
|
faad_decoder_quark(void)
|
|
|
|
{
|
|
|
|
return g_quark_from_static_string("faad");
|
|
|
|
}
|
|
|
|
|
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
|
2009-02-17 22:56:42 +01:00
|
|
|
adts_find_frame(struct decoder_buffer *buffer)
|
2008-08-26 08:27:11 +02:00
|
|
|
{
|
2009-02-17 22:56:42 +01:00
|
|
|
size_t length, frame_length;
|
|
|
|
bool ret;
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2009-02-17 22:56:42 +01:00
|
|
|
while (true) {
|
2013-04-17 22:33:59 +02:00
|
|
|
const uint8_t *data = (const uint8_t *)
|
|
|
|
decoder_buffer_read(buffer, &length);
|
|
|
|
if (data == nullptr || length < 8) {
|
2009-02-17 22:56:42 +01:00
|
|
|
/* not enough data yet */
|
|
|
|
ret = decoder_buffer_fill(buffer);
|
|
|
|
if (!ret)
|
|
|
|
/* 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 */
|
2013-04-17 22:33:59 +02:00
|
|
|
const uint8_t *p = (const uint8_t *)memchr(data, 0xff, length);
|
|
|
|
if (p == nullptr) {
|
2009-02-17 22:56:42 +01:00
|
|
|
/* no marker - discard the buffer */
|
|
|
|
decoder_buffer_consume(buffer, length);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p > data) {
|
|
|
|
/* discard data before 0xff */
|
|
|
|
decoder_buffer_consume(buffer, p - data);
|
|
|
|
continue;
|
|
|
|
}
|
2008-08-26 08:27:11 +02:00
|
|
|
|
|
|
|
/* is it a frame? */
|
2009-02-17 22:56:42 +01:00
|
|
|
frame_length = adts_check_frame(data);
|
|
|
|
if (frame_length == 0) {
|
|
|
|
/* it's just some random 0xff byte; discard it
|
|
|
|
and continue searching */
|
|
|
|
decoder_buffer_consume(buffer, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length < frame_length) {
|
|
|
|
/* available buffer size is smaller than the
|
|
|
|
frame will be - attempt to read more
|
|
|
|
data */
|
|
|
|
ret = decoder_buffer_fill(buffer);
|
|
|
|
if (!ret) {
|
|
|
|
/* not enough data; discard this frame
|
|
|
|
to prevent a possible buffer
|
|
|
|
overflow */
|
2013-04-17 22:33:59 +02:00
|
|
|
data = (const uint8_t *)
|
|
|
|
decoder_buffer_read(buffer, &length);
|
|
|
|
if (data != nullptr)
|
2009-02-17 22:56:42 +01:00
|
|
|
decoder_buffer_consume(buffer, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2009-02-17 22:56:42 +01:00
|
|
|
adts_song_duration(struct decoder_buffer *buffer)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-16 19:31:11 +01:00
|
|
|
unsigned int frames, frame_length;
|
2008-11-12 08:16:38 +01:00
|
|
|
unsigned sample_rate = 0;
|
2009-02-16 19:31:11 +01:00
|
|
|
float frames_per_second;
|
2004-03-21 22:32:23 +01:00
|
|
|
|
|
|
|
/* Read all frames to ensure correct time and bitrate */
|
2006-07-20 18:02:40 +02:00
|
|
|
for (frames = 0;; frames++) {
|
2009-02-17 22:56:42 +01:00
|
|
|
frame_length = adts_find_frame(buffer);
|
|
|
|
if (frame_length == 0)
|
|
|
|
break;
|
2004-03-21 22:32:23 +01:00
|
|
|
|
|
|
|
|
2009-02-17 22:56:42 +01:00
|
|
|
if (frames == 0) {
|
|
|
|
size_t buffer_length;
|
2013-04-17 22:33:59 +02:00
|
|
|
const uint8_t *data = (const uint8_t *)
|
|
|
|
decoder_buffer_read(buffer, &buffer_length);
|
|
|
|
assert(data != nullptr);
|
2009-02-17 22:56:42 +01:00
|
|
|
assert(frame_length <= buffer_length);
|
|
|
|
|
|
|
|
sample_rate = adts_sample_rates[(data[2] & 0x3c) >> 2];
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder_buffer_consume(buffer, frame_length);
|
2004-03-21 22:32:23 +01:00
|
|
|
}
|
|
|
|
|
2009-02-16 19:31:11 +01:00
|
|
|
frames_per_second = (float)sample_rate / 1024.0;
|
2009-02-17 22:54:01 +01:00
|
|
|
if (frames_per_second <= 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return (float)frames / frames_per_second;
|
2004-03-21 22:32:23 +01:00
|
|
|
}
|
|
|
|
|
2009-02-17 22:54:01 +01:00
|
|
|
static float
|
2009-02-17 22:56:42 +01:00
|
|
|
faad_song_duration(struct decoder_buffer *buffer, struct input_stream *is)
|
2008-08-26 08:27:11 +02:00
|
|
|
{
|
|
|
|
size_t fileread;
|
|
|
|
size_t tagsize;
|
2009-02-17 22:56:42 +01:00
|
|
|
size_t length;
|
2009-09-30 15:22:47 +02:00
|
|
|
bool success;
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2013-01-24 19:14:40 +01:00
|
|
|
const goffset size = input_stream_get_size(is);
|
|
|
|
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);
|
2013-04-17 22:33:59 +02:00
|
|
|
const uint8_t *data = (const uint8_t *)
|
|
|
|
decoder_buffer_read(buffer, &length);
|
|
|
|
if (data == nullptr)
|
2009-02-17 22:56:42 +01:00
|
|
|
return -1;
|
2004-03-21 22:32:23 +01:00
|
|
|
|
|
|
|
tagsize = 0;
|
2009-02-17 22:56:42 +01:00
|
|
|
if (length >= 10 && !memcmp(data, "ID3", 3)) {
|
2009-02-17 23:42:06 +01:00
|
|
|
/* skip the ID3 tag */
|
|
|
|
|
2009-02-17 22:56:42 +01:00
|
|
|
tagsize = (data[6] << 21) | (data[7] << 14) |
|
|
|
|
(data[8] << 7) | (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
|
|
|
|
2009-09-30 15:22:47 +02:00
|
|
|
success = decoder_buffer_skip(buffer, tagsize) &&
|
|
|
|
decoder_buffer_fill(buffer);
|
|
|
|
if (!success)
|
|
|
|
return -1;
|
|
|
|
|
2013-04-17 22:33:59 +02:00
|
|
|
data = (const uint8_t *)decoder_buffer_read(buffer, &length);
|
|
|
|
if (data == nullptr)
|
2009-02-17 22:56:42 +01:00
|
|
|
return -1;
|
2004-03-21 22:32:23 +01:00
|
|
|
}
|
|
|
|
|
2013-01-24 19:14:40 +01:00
|
|
|
if (input_stream_is_seekable(is) && length >= 2 &&
|
2009-02-17 22:56:42 +01:00
|
|
|
data[0] == 0xFF && ((data[1] & 0xF6) == 0xF0)) {
|
2009-02-17 23:42:06 +01:00
|
|
|
/* obtain the duration from the ADTS header */
|
2009-02-17 22:56:42 +01:00
|
|
|
float song_length = adts_song_duration(buffer);
|
2004-03-21 22:32:23 +01:00
|
|
|
|
2013-04-17 22:33:59 +02:00
|
|
|
input_stream_lock_seek(is, tagsize, SEEK_SET, nullptr);
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2013-04-17 22:33:59 +02:00
|
|
|
data = (const uint8_t *)decoder_buffer_read(buffer, &length);
|
|
|
|
if (data != nullptr)
|
2009-02-17 22:56:42 +01:00
|
|
|
decoder_buffer_consume(buffer, length);
|
|
|
|
decoder_buffer_fill(buffer);
|
2009-02-17 22:54:01 +01:00
|
|
|
|
2009-02-17 22:56:42 +01:00
|
|
|
return song_length;
|
|
|
|
} else if (length >= 5 && memcmp(data, "ADIF", 4) == 0) {
|
2009-02-17 23:42:06 +01:00
|
|
|
/* obtain the duration from the ADIF header */
|
2009-02-16 19:31:11 +01:00
|
|
|
unsigned bit_rate;
|
2009-02-17 22:56:42 +01:00
|
|
|
size_t skip_size = (data[4] & 0x80) ? 9 : 0;
|
2008-11-12 08:16:38 +01:00
|
|
|
|
2009-02-17 22:56:42 +01:00
|
|
|
if (8 + skip_size > length)
|
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
|
|
|
|
2009-02-17 22:56:42 +01:00
|
|
|
bit_rate = ((data[4 + skip_size] & 0x0F) << 19) |
|
|
|
|
(data[5 + skip_size] << 11) |
|
|
|
|
(data[6 + skip_size] << 3) |
|
|
|
|
(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
|
|
|
}
|
|
|
|
|
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-01-29 16:17:15 +01:00
|
|
|
faad_decoder_init(NeAACDecHandle decoder, struct decoder_buffer *buffer,
|
2009-11-10 19:01:38 +01:00
|
|
|
struct audio_format *audio_format, GError **error_r)
|
2009-02-17 22:54:26 +01:00
|
|
|
{
|
|
|
|
int32_t nbytes;
|
2009-02-17 23:35:49 +01:00
|
|
|
uint32_t sample_rate;
|
|
|
|
uint8_t channels;
|
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
|
|
|
|
|
2013-04-17 22:33:59 +02:00
|
|
|
size_t length;
|
|
|
|
const unsigned char *data = (const unsigned char *)
|
|
|
|
decoder_buffer_read(buffer, &length);
|
|
|
|
if (data == nullptr) {
|
2009-11-10 19:01:38 +01:00
|
|
|
g_set_error(error_r, faad_decoder_quark(), 0,
|
|
|
|
"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
|
|
|
|
2013-04-17 22:33:59 +02:00
|
|
|
nbytes = NeAACDecInit(decoder,
|
|
|
|
/* deconst hack, libfaad requires this */
|
|
|
|
const_cast<unsigned char *>(data),
|
2009-02-17 22:56:42 +01:00
|
|
|
length,
|
2009-11-10 19:01:38 +01:00
|
|
|
sample_rate_p, &channels);
|
|
|
|
if (nbytes < 0) {
|
|
|
|
g_set_error(error_r, faad_decoder_quark(), 0,
|
|
|
|
"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,
|
2009-11-10 17:11:34 +01:00
|
|
|
SAMPLE_FORMAT_S16, channels, error_r);
|
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-01-29 16:17:15 +01:00
|
|
|
faad_decoder_decode(NeAACDecHandle decoder, struct decoder_buffer *buffer,
|
|
|
|
NeAACDecFrameInfo *frame_info)
|
2009-02-17 22:54:26 +01:00
|
|
|
{
|
2009-02-17 22:56:42 +01:00
|
|
|
size_t length;
|
2013-04-17 22:33:59 +02:00
|
|
|
const unsigned char *data = (const unsigned char *)
|
|
|
|
decoder_buffer_read(buffer, &length);
|
|
|
|
if (data == nullptr)
|
|
|
|
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 */
|
|
|
|
const_cast<unsigned char *>(data),
|
|
|
|
length);
|
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
|
2009-12-31 18:20:08 +01:00
|
|
|
faad_get_file_time_float(struct input_stream *is)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-17 22:56:42 +01:00
|
|
|
struct decoder_buffer *buffer;
|
2004-03-21 22:32:23 +01:00
|
|
|
float length;
|
|
|
|
|
2013-04-17 22:33:59 +02:00
|
|
|
buffer = decoder_buffer_new(nullptr, is,
|
2009-02-17 22:56:42 +01:00
|
|
|
FAAD_MIN_STREAMSIZE * AAC_MAX_CHANNELS);
|
2009-12-31 18:20:08 +01:00
|
|
|
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) {
|
2009-02-17 22:54:26 +01:00
|
|
|
bool ret;
|
2009-02-17 23:35:49 +01:00
|
|
|
struct audio_format audio_format;
|
2009-02-17 22:54:26 +01:00
|
|
|
|
2013-01-29 16:17:15 +01:00
|
|
|
NeAACDecHandle decoder = NeAACDecOpen();
|
2004-03-22 05:20:19 +01:00
|
|
|
|
2013-01-29 16:17:15 +01:00
|
|
|
NeAACDecConfigurationPtr config =
|
|
|
|
NeAACDecGetCurrentConfiguration(decoder);
|
2004-03-22 05:20:19 +01:00
|
|
|
config->outputFormat = FAAD_FMT_16BIT;
|
2013-01-29 16:17:15 +01:00
|
|
|
NeAACDecSetConfiguration(decoder, config);
|
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
|
|
|
|
2013-04-17 22:33:59 +02:00
|
|
|
ret = faad_decoder_init(decoder, buffer, &audio_format, nullptr);
|
2009-11-10 19:01:38 +01:00
|
|
|
if (ret)
|
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
|
2009-12-31 18:20:08 +01:00
|
|
|
faad_get_file_time(struct input_stream *is)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-04-12 06:16:32 +02:00
|
|
|
int file_time = -1;
|
2004-03-22 05:20:19 +01:00
|
|
|
float length;
|
|
|
|
|
2009-12-31 18:20:08 +01:00
|
|
|
if ((length = faad_get_file_time_float(is)) >= 0)
|
2008-04-12 06:16:32 +02:00
|
|
|
file_time = length + 0.5;
|
2004-03-22 05:20:19 +01:00
|
|
|
|
2008-04-12 06:16:32 +02:00
|
|
|
return file_time;
|
2004-03-21 22:32:23 +01:00
|
|
|
}
|
|
|
|
|
2008-11-11 17:13:44 +01:00
|
|
|
static void
|
2009-02-17 19:27:01 +01:00
|
|
|
faad_stream_decode(struct decoder *mpd_decoder, struct input_stream *is)
|
2008-08-26 08:27:11 +02:00
|
|
|
{
|
2013-04-17 22:33:59 +02:00
|
|
|
GError *error = nullptr;
|
2009-02-16 19:31:11 +01:00
|
|
|
float total_time = 0;
|
2009-02-17 23:26:51 +01:00
|
|
|
struct audio_format audio_format;
|
2009-02-17 22:54:26 +01:00
|
|
|
bool ret;
|
2009-02-16 19:31:11 +01:00
|
|
|
uint16_t bit_rate = 0;
|
2009-02-17 22:56:42 +01:00
|
|
|
struct decoder_buffer *buffer;
|
2008-11-12 08:25:06 +01:00
|
|
|
enum decoder_command cmd;
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2009-02-17 22:56:42 +01:00
|
|
|
buffer = decoder_buffer_new(mpd_decoder, is,
|
|
|
|
FAAD_MIN_STREAMSIZE * AAC_MAX_CHANNELS);
|
|
|
|
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 */
|
|
|
|
|
2013-01-29 16:17:15 +01:00
|
|
|
NeAACDecHandle decoder = NeAACDecOpen();
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2013-01-29 16:17:15 +01:00
|
|
|
NeAACDecConfigurationPtr config =
|
|
|
|
NeAACDecGetCurrentConfiguration(decoder);
|
2008-08-26 08:27:11 +02:00
|
|
|
config->outputFormat = FAAD_FMT_16BIT;
|
|
|
|
config->downMatrix = 1;
|
|
|
|
config->dontUpSampleImplicitSBR = 0;
|
2013-01-29 16:17:15 +01:00
|
|
|
NeAACDecSetConfiguration(decoder, config);
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2009-02-17 22:56:42 +01:00
|
|
|
while (!decoder_buffer_is_full(buffer) &&
|
2011-09-14 21:46:41 +02:00
|
|
|
!input_stream_lock_eof(is) &&
|
2008-08-26 08:27:11 +02:00
|
|
|
decoder_get_command(mpd_decoder) == DECODE_COMMAND_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 */
|
|
|
|
|
2009-11-10 19:01:38 +01:00
|
|
|
ret = faad_decoder_init(decoder, buffer, &audio_format, &error);
|
2009-02-17 22:54:26 +01:00
|
|
|
if (!ret) {
|
2009-11-10 19:01:38 +01:00
|
|
|
g_warning("%s", error->message);
|
|
|
|
g_error_free(error);
|
2013-01-29 16:17:15 +01:00
|
|
|
NeAACDecClose(decoder);
|
2009-02-17 23:26:51 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-02-17 23:42:06 +01:00
|
|
|
/* initialize the MPD core */
|
|
|
|
|
2009-02-17 23:26:51 +01:00
|
|
|
decoder_initialized(mpd_decoder, &audio_format, false, total_time);
|
|
|
|
|
2009-02-17 23:42:06 +01:00
|
|
|
/* the decoder loop */
|
|
|
|
|
2008-11-02 17:02:23 +01:00
|
|
|
do {
|
2009-02-17 23:42:06 +01:00
|
|
|
size_t frame_size;
|
2009-02-17 23:44:29 +01:00
|
|
|
const void *decoded;
|
2013-01-29 16:17:15 +01:00
|
|
|
NeAACDecFrameInfo frame_info;
|
2009-02-17 23:42:06 +01:00
|
|
|
|
|
|
|
/* find the next frame */
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
2009-02-17 22:56:42 +01:00
|
|
|
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) {
|
2008-11-21 20:13:26 +01:00
|
|
|
g_warning("error decoding AAC stream: %s\n",
|
2013-01-29 16:17:15 +01:00
|
|
|
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) {
|
2009-02-17 23:26:51 +01:00
|
|
|
g_warning("channel count changed from %u to %u",
|
2009-02-17 23:35:49 +01:00
|
|
|
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) {
|
2009-02-17 23:26:51 +01:00
|
|
|
g_warning("sample rate changed from %u to %lu",
|
2009-02-17 23:35:49 +01:00
|
|
|
audio_format.sample_rate,
|
2009-02-17 23:26:51 +01:00
|
|
|
(unsigned long)frame_info.samplerate);
|
|
|
|
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);
|
2008-11-12 08:25:09 +01:00
|
|
|
} while (cmd != DECODE_COMMAND_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);
|
2008-08-26 08:27:11 +02:00
|
|
|
}
|
|
|
|
|
2012-02-11 19:12:02 +01:00
|
|
|
static bool
|
|
|
|
faad_scan_stream(struct input_stream *is,
|
|
|
|
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);
|
2004-05-31 04:56:14 +02:00
|
|
|
|
2009-12-31 18:20:08 +01:00
|
|
|
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
|
|
|
|
2009-02-16 19:30:54 +01:00
|
|
|
const struct decoder_plugin 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
|
|
|
};
|