2005-10-31 20:56:29 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Dave Chapman
|
|
|
|
*
|
2008-06-28 18:10:04 +00: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.
|
2005-10-31 20:56:29 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "codeclib.h"
|
|
|
|
#include "libm4a/m4a.h"
|
|
|
|
#include "libfaad/common.h"
|
|
|
|
#include "libfaad/structs.h"
|
|
|
|
#include "libfaad/decoder.h"
|
|
|
|
|
2006-01-18 00:05:14 +00:00
|
|
|
CODEC_HEADER
|
|
|
|
|
2011-04-18 19:12:51 +00:00
|
|
|
/* The maximum buffer size handled by faad. 12 bytes are required by libfaad
|
|
|
|
* as headroom (see libfaad/bits.c). FAAD_BYTE_BUFFER_SIZE bytes are buffered
|
|
|
|
* for each frame. */
|
|
|
|
#define FAAD_BYTE_BUFFER_SIZE (2048-12)
|
|
|
|
|
2005-10-31 20:56:29 +00:00
|
|
|
/* this is the codec entry point */
|
2011-04-27 03:08:23 +00:00
|
|
|
enum codec_status codec_main(enum codec_entry_call_reason reason)
|
|
|
|
{
|
|
|
|
if (reason == CODEC_LOAD) {
|
|
|
|
/* Generic codec initialisation */
|
|
|
|
ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
|
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, 29);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CODEC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is called for each file to process */
|
|
|
|
enum codec_status codec_run(void)
|
2005-10-31 20:56:29 +00:00
|
|
|
{
|
2006-10-11 17:02:23 +00:00
|
|
|
/* Note that when dealing with QuickTime/MPEG4 files, terminology is
|
|
|
|
* a bit confusing. Files with sound are split up in chunks, where
|
|
|
|
* each chunk contains one or more samples. Each sample in turn
|
|
|
|
* contains a number of "sound samples" (the kind you refer to with
|
|
|
|
* the sampling frequency).
|
|
|
|
*/
|
2005-10-31 23:34:14 +00:00
|
|
|
size_t n;
|
2010-12-21 13:35:02 +00:00
|
|
|
demux_res_t demux_res;
|
2005-10-31 23:34:14 +00:00
|
|
|
stream_t input_stream;
|
2006-10-11 17:02:23 +00:00
|
|
|
uint32_t sound_samples_done;
|
|
|
|
uint32_t elapsed_time;
|
|
|
|
int file_offset;
|
2007-06-16 13:00:52 +00:00
|
|
|
int framelength;
|
|
|
|
int lead_trim = 0;
|
2011-07-17 13:00:53 +00:00
|
|
|
unsigned int frame_samples;
|
2005-10-31 23:34:14 +00:00
|
|
|
unsigned int i;
|
|
|
|
unsigned char* buffer;
|
2010-12-21 13:35:02 +00:00
|
|
|
NeAACDecFrameInfo frame_info;
|
2006-10-11 17:02:23 +00:00
|
|
|
NeAACDecHandle decoder;
|
2005-10-31 23:34:14 +00:00
|
|
|
int err;
|
2011-04-19 05:55:54 +00:00
|
|
|
uint32_t seek_idx = 0;
|
2006-10-11 17:02:23 +00:00
|
|
|
uint32_t s = 0;
|
2011-02-02 09:38:24 +00:00
|
|
|
uint32_t sbr_fac = 1;
|
2006-10-11 17:02:23 +00:00
|
|
|
unsigned char c = 0;
|
2009-12-14 01:09:01 +00:00
|
|
|
void *ret;
|
2011-04-27 03:08:23 +00:00
|
|
|
intptr_t param;
|
2011-06-18 15:11:30 +00:00
|
|
|
bool empty_first_frame = false;
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2010-08-29 16:43:11 +00:00
|
|
|
/* Clean and initialize decoder structures */
|
|
|
|
memset(&demux_res , 0, sizeof(demux_res));
|
2006-11-26 18:31:41 +00:00
|
|
|
if (codec_init()) {
|
2010-08-29 16:46:03 +00:00
|
|
|
LOGF("FAAD: Codec init error\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2005-10-31 20:56:29 +00:00
|
|
|
}
|
|
|
|
|
2010-03-14 13:37:41 +00:00
|
|
|
file_offset = ci->id3->offset;
|
2006-08-23 13:10:48 +00:00
|
|
|
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
|
2006-11-26 18:31:41 +00:00
|
|
|
codec_set_replaygain(ci->id3);
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
stream_create(&input_stream,ci);
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
ci->seek_buffer(ci->id3->first_frame_offset);
|
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
/* if qtmovie_read returns successfully, the stream is up to
|
|
|
|
* the movie data, which can be used directly by the decoder */
|
|
|
|
if (!qtmovie_read(&input_stream, &demux_res)) {
|
2010-08-29 16:46:03 +00:00
|
|
|
LOGF("FAAD: File init error\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2005-10-31 23:34:14 +00:00
|
|
|
}
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
/* initialise the sound converter */
|
2006-10-11 17:02:23 +00:00
|
|
|
decoder = NeAACDecOpen();
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2006-10-11 17:02:23 +00:00
|
|
|
if (!decoder) {
|
2010-08-29 16:46:03 +00:00
|
|
|
LOGF("FAAD: Decode open error\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2005-10-31 20:56:29 +00:00
|
|
|
}
|
|
|
|
|
2006-10-11 17:02:23 +00:00
|
|
|
NeAACDecConfigurationPtr conf = NeAACDecGetCurrentConfiguration(decoder);
|
2005-11-02 19:43:52 +00:00
|
|
|
conf->outputFormat = FAAD_FMT_24BIT; /* irrelevant, we don't convert */
|
2006-10-11 17:02:23 +00:00
|
|
|
NeAACDecSetConfiguration(decoder, conf);
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2006-10-11 17:02:23 +00:00
|
|
|
err = NeAACDecInit2(decoder, demux_res.codecdata, demux_res.codecdata_len, &s, &c);
|
2005-10-31 23:34:14 +00:00
|
|
|
if (err) {
|
2010-08-29 16:46:03 +00:00
|
|
|
LOGF("FAAD: DecInit: %d, %d\n", err, decoder->object_type);
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2005-10-31 23:34:14 +00:00
|
|
|
}
|
2010-07-02 04:35:37 +00:00
|
|
|
|
2011-02-02 09:38:24 +00:00
|
|
|
#ifdef SBR_DEC
|
2011-02-02 15:12:55 +00:00
|
|
|
/* Check for need of special handling for seek/resume and elapsed time. */
|
|
|
|
if (ci->id3->needs_upsampling_correction) {
|
2011-02-02 09:38:24 +00:00
|
|
|
sbr_fac = 2;
|
|
|
|
} else {
|
|
|
|
sbr_fac = 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-10-11 17:02:23 +00:00
|
|
|
i = 0;
|
2006-08-23 13:10:48 +00:00
|
|
|
|
2010-03-14 13:37:41 +00:00
|
|
|
if (file_offset > 0) {
|
2011-02-02 09:38:24 +00:00
|
|
|
/* Resume the desired (byte) position. Important: When resuming SBR
|
|
|
|
* upsampling files the resulting sound_samples_done must be expanded
|
|
|
|
* by a factor of 2. This is done via using sbr_fac. */
|
2011-04-18 19:12:51 +00:00
|
|
|
if (m4a_seek_raw(&demux_res, &input_stream, file_offset,
|
2006-10-11 17:02:23 +00:00
|
|
|
&sound_samples_done, (int*) &i)) {
|
2011-02-02 09:38:24 +00:00
|
|
|
sound_samples_done *= sbr_fac;
|
2006-10-11 17:02:23 +00:00
|
|
|
elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
|
|
|
|
ci->set_elapsed(elapsed_time);
|
2006-08-23 13:10:48 +00:00
|
|
|
} else {
|
2006-10-11 17:02:23 +00:00
|
|
|
sound_samples_done = 0;
|
2006-08-23 13:10:48 +00:00
|
|
|
}
|
2011-04-24 18:56:23 +00:00
|
|
|
NeAACDecPostSeekReset(decoder, i);
|
2010-03-19 15:27:10 +00:00
|
|
|
} else {
|
|
|
|
sound_samples_done = 0;
|
2006-08-23 13:10:48 +00:00
|
|
|
}
|
2007-06-16 13:00:52 +00:00
|
|
|
|
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
lead_trim = ci->id3->lead_trim;
|
|
|
|
}
|
2006-08-23 13:10:48 +00:00
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
/* The main decoding loop */
|
|
|
|
while (i < demux_res.num_sample_byte_sizes) {
|
2011-04-27 03:08:23 +00:00
|
|
|
enum codec_command_action action = ci->get_command(¶m);
|
2006-10-11 17:02:23 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if (action == CODEC_ACTION_HALT)
|
2005-10-31 23:34:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Deal with any pending seek requests */
|
2011-04-27 03:08:23 +00:00
|
|
|
if (action == CODEC_ACTION_SEEK_TIME) {
|
2011-02-02 09:38:24 +00:00
|
|
|
/* Seek to the desired time position. Important: When seeking in SBR
|
|
|
|
* upsampling files the seek_time must be divided by 2 when calling
|
2011-04-18 19:12:51 +00:00
|
|
|
* m4a_seek and the resulting sound_samples_done must be expanded
|
2011-02-02 09:38:24 +00:00
|
|
|
* by a factor 2. This is done via using sbr_fac. */
|
2011-04-18 19:12:51 +00:00
|
|
|
if (m4a_seek(&demux_res, &input_stream,
|
2011-04-27 03:08:23 +00:00
|
|
|
(param/10/sbr_fac)*(ci->id3->frequency/100),
|
2006-10-11 17:02:23 +00:00
|
|
|
&sound_samples_done, (int*) &i)) {
|
2011-02-02 09:38:24 +00:00
|
|
|
sound_samples_done *= sbr_fac;
|
2006-10-11 17:02:23 +00:00
|
|
|
elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
|
|
|
|
ci->set_elapsed(elapsed_time);
|
2011-04-19 05:55:54 +00:00
|
|
|
seek_idx = 0;
|
|
|
|
|
2007-06-16 13:00:52 +00:00
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
lead_trim = ci->id3->lead_trim;
|
|
|
|
}
|
2005-10-31 23:34:14 +00:00
|
|
|
}
|
2011-04-24 18:56:23 +00:00
|
|
|
NeAACDecPostSeekReset(decoder, i);
|
2005-11-02 00:09:42 +00:00
|
|
|
ci->seek_complete();
|
2005-10-31 23:34:14 +00:00
|
|
|
}
|
|
|
|
|
2006-10-11 17:02:23 +00:00
|
|
|
/* There can be gaps between chunks, so skip ahead if needed. It
|
|
|
|
* doesn't seem to happen much, but it probably means that a
|
|
|
|
* "proper" file can have chunks out of order. Why one would want
|
|
|
|
* that an good question (but files with gaps do exist, so who
|
|
|
|
* knows?), so we don't support that - for now, at least.
|
2011-04-19 05:55:54 +00:00
|
|
|
*/
|
|
|
|
file_offset = m4a_check_sample_offset(&demux_res, i, &seek_idx);
|
2011-04-16 19:39:01 +00:00
|
|
|
|
2006-10-11 17:02:23 +00:00
|
|
|
if (file_offset > ci->curpos)
|
|
|
|
{
|
|
|
|
ci->advance_buffer(file_offset - ci->curpos);
|
|
|
|
}
|
2007-01-30 21:42:36 +00:00
|
|
|
else if (file_offset == 0)
|
|
|
|
{
|
2010-08-29 16:46:03 +00:00
|
|
|
LOGF("AAC: get_sample_offset error\n");
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2007-01-30 21:42:36 +00:00
|
|
|
}
|
2006-10-11 17:02:23 +00:00
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
/* Request the required number of bytes from the input buffer */
|
2011-04-18 19:12:51 +00:00
|
|
|
buffer=ci->request_buffer(&n, FAAD_BYTE_BUFFER_SIZE);
|
2005-10-31 23:34:14 +00:00
|
|
|
|
|
|
|
/* Decode one block - returned samples will be host-endian */
|
2009-12-14 01:09:01 +00:00
|
|
|
ret = NeAACDecDecode(decoder, &frame_info, buffer, n);
|
|
|
|
|
|
|
|
/* NeAACDecDecode may sometimes return NULL without setting error. */
|
|
|
|
if (ret == NULL || frame_info.error > 0) {
|
2010-08-29 16:46:03 +00:00
|
|
|
LOGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info.error));
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_ERROR;
|
2005-10-31 23:34:14 +00:00
|
|
|
}
|
|
|
|
|
2010-03-14 13:37:41 +00:00
|
|
|
/* Advance codec buffer (no need to call set_offset because of this) */
|
2011-04-16 19:39:01 +00:00
|
|
|
ci->advance_buffer(frame_info.bytesconsumed);
|
2005-10-31 23:34:14 +00:00
|
|
|
|
|
|
|
/* Output the audio */
|
2006-11-26 18:31:41 +00:00
|
|
|
ci->yield();
|
2007-06-16 13:00:52 +00:00
|
|
|
|
2011-07-17 13:00:53 +00:00
|
|
|
frame_samples = frame_info.samples >> 1;
|
|
|
|
|
2011-06-18 15:11:30 +00:00
|
|
|
if (empty_first_frame)
|
|
|
|
{
|
|
|
|
/* Remove the first frame from lead_trim, under the assumption
|
|
|
|
* that it had the same size as this frame
|
|
|
|
*/
|
|
|
|
empty_first_frame = false;
|
2011-07-17 13:00:53 +00:00
|
|
|
lead_trim -= frame_samples;
|
2011-06-18 15:11:30 +00:00
|
|
|
|
|
|
|
if (lead_trim < 0)
|
|
|
|
{
|
|
|
|
lead_trim = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-16 19:39:01 +00:00
|
|
|
/* Gather number of samples for the decoded frame. */
|
2011-07-17 13:00:53 +00:00
|
|
|
framelength = frame_samples - lead_trim;
|
2007-06-16 13:00:52 +00:00
|
|
|
|
2011-06-18 15:11:30 +00:00
|
|
|
if (i == demux_res.num_sample_byte_sizes - 1)
|
2007-06-16 13:00:52 +00:00
|
|
|
{
|
2011-07-17 13:00:53 +00:00
|
|
|
// Size of the last frame
|
|
|
|
const uint32_t sample_duration = (demux_res.num_time_to_samples > 0) ?
|
|
|
|
demux_res.time_to_sample[demux_res.num_time_to_samples - 1].sample_duration :
|
|
|
|
frame_samples;
|
|
|
|
|
|
|
|
/* Currently limited to at most one frame of tail_trim.
|
|
|
|
* Seems to be enough.
|
|
|
|
*/
|
|
|
|
if (ci->id3->tail_trim == 0 && sample_duration < frame_samples)
|
|
|
|
{
|
|
|
|
/* Subtract lead_trim just in case we decode a file with only
|
|
|
|
* one audio frame with actual data (lead_trim is usually zero
|
|
|
|
* here).
|
|
|
|
*/
|
|
|
|
framelength = sample_duration - lead_trim;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
framelength -= ci->id3->tail_trim;
|
|
|
|
}
|
2007-06-16 13:00:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (framelength > 0)
|
|
|
|
{
|
|
|
|
ci->pcmbuf_insert(&decoder->time_out[0][lead_trim],
|
|
|
|
&decoder->time_out[1][lead_trim],
|
|
|
|
framelength);
|
2011-07-17 13:00:53 +00:00
|
|
|
sound_samples_done += framelength;
|
|
|
|
/* Update the elapsed-time indicator */
|
|
|
|
elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
|
|
|
|
ci->set_elapsed(elapsed_time);
|
2011-06-18 15:11:30 +00:00
|
|
|
}
|
|
|
|
|
2007-06-16 13:00:52 +00:00
|
|
|
if (lead_trim > 0)
|
|
|
|
{
|
2011-06-18 15:11:30 +00:00
|
|
|
/* frame_info.samples can be 0 for frame 0. We still want to
|
|
|
|
* remove it from lead_trim, so do that during frame 1.
|
|
|
|
*/
|
|
|
|
if (0 == i && 0 == frame_info.samples)
|
|
|
|
{
|
|
|
|
empty_first_frame = true;
|
|
|
|
}
|
|
|
|
|
2011-07-17 13:00:53 +00:00
|
|
|
lead_trim -= frame_samples;
|
2007-06-16 13:00:52 +00:00
|
|
|
|
2011-06-18 15:11:30 +00:00
|
|
|
if (lead_trim < 0)
|
2007-06-16 13:00:52 +00:00
|
|
|
{
|
|
|
|
lead_trim = 0;
|
|
|
|
}
|
|
|
|
}
|
2005-10-31 23:34:14 +00:00
|
|
|
|
2011-07-17 13:00:53 +00:00
|
|
|
++i;
|
2005-10-31 23:34:14 +00:00
|
|
|
}
|
2006-10-11 17:02:23 +00:00
|
|
|
|
2010-08-29 16:46:03 +00:00
|
|
|
LOGF("AAC: Decoded %lu samples\n", (unsigned long)sound_samples_done);
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_OK;
|
2005-10-31 20:56:29 +00:00
|
|
|
}
|