2005-10-31 20:56:29 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Dave Chapman
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
2005-10-31 20:56:29 +00:00
|
|
|
/* this is the codec entry point */
|
2006-11-26 18:31:41 +00:00
|
|
|
enum codec_status codec_main(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;
|
|
|
|
static demux_res_t demux_res;
|
|
|
|
stream_t input_stream;
|
2006-10-11 17:02:23 +00:00
|
|
|
uint32_t sound_samples_done;
|
|
|
|
uint32_t elapsed_time;
|
2005-10-31 23:34:14 +00:00
|
|
|
uint32_t sample_duration;
|
|
|
|
uint32_t sample_byte_size;
|
2006-10-11 17:02:23 +00:00
|
|
|
int file_offset;
|
2005-10-31 23:34:14 +00:00
|
|
|
unsigned int i;
|
|
|
|
unsigned char* buffer;
|
2006-10-11 17:02:23 +00:00
|
|
|
static NeAACDecFrameInfo frame_info;
|
|
|
|
NeAACDecHandle decoder;
|
2005-10-31 23:34:14 +00:00
|
|
|
int err;
|
2006-10-11 17:02:23 +00:00
|
|
|
uint32_t s = 0;
|
|
|
|
unsigned char c = 0;
|
2005-10-31 23:34:14 +00:00
|
|
|
|
|
|
|
/* Generic codec initialisation */
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, 1024*16);
|
|
|
|
ci->configure(CODEC_SET_FILEBUF_WATERMARK, 1024*512);
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2007-02-10 16:34:16 +00:00
|
|
|
ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
|
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, 29);
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2006-04-15 02:03:11 +00:00
|
|
|
next_track:
|
2006-10-11 17:02:23 +00:00
|
|
|
err = CODEC_OK;
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2006-11-26 18:31:41 +00:00
|
|
|
if (codec_init()) {
|
2006-10-11 17:02:23 +00:00
|
|
|
LOGF("FAAD: Codec init error\n");
|
2006-01-18 20:22:03 +00:00
|
|
|
err = CODEC_ERROR;
|
|
|
|
goto exit;
|
2005-10-31 20:56:29 +00:00
|
|
|
}
|
|
|
|
|
2006-08-23 13:10:48 +00:00
|
|
|
while (!*ci->taginfo_ready && !ci->stop_codec)
|
|
|
|
ci->sleep(1);
|
2005-10-31 23:34:14 +00:00
|
|
|
|
2006-10-11 17:02:23 +00:00
|
|
|
sound_samples_done = 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
|
|
|
|
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)) {
|
2006-10-11 17:02:23 +00:00
|
|
|
LOGF("FAAD: File init error\n");
|
2006-01-18 20:22:03 +00:00
|
|
|
err = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
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) {
|
|
|
|
LOGF("FAAD: Decode open error\n");
|
2006-01-18 20:22:03 +00:00
|
|
|
err = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
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) {
|
2006-10-11 17:02:23 +00:00
|
|
|
LOGF("FAAD: DecInit: %d, %d\n", err, decoder->object_type);
|
2006-01-18 20:22:03 +00:00
|
|
|
err = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
2005-10-31 23:34:14 +00:00
|
|
|
}
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2006-10-11 17:02:23 +00:00
|
|
|
ci->id3->frequency = s;
|
2005-10-31 23:34:14 +00:00
|
|
|
|
2006-10-11 17:02:23 +00:00
|
|
|
i = 0;
|
2006-08-23 13:10:48 +00:00
|
|
|
|
2006-10-11 17:02:23 +00:00
|
|
|
if (sound_samples_done > 0) {
|
|
|
|
if (alac_seek_raw(&demux_res, &input_stream, sound_samples_done,
|
|
|
|
&sound_samples_done, (int*) &i)) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
/* The main decoding loop */
|
|
|
|
while (i < demux_res.num_sample_byte_sizes) {
|
2006-11-26 18:31:41 +00:00
|
|
|
ci->yield();
|
2006-10-11 17:02:23 +00:00
|
|
|
|
2006-04-15 02:03:11 +00:00
|
|
|
if (ci->stop_codec || ci->new_track) {
|
2005-10-31 23:34:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deal with any pending seek requests */
|
|
|
|
if (ci->seek_time) {
|
2005-12-02 08:42:48 +00:00
|
|
|
if (alac_seek(&demux_res, &input_stream,
|
2006-10-11 17:02:23 +00:00
|
|
|
((ci->seek_time-1)/10)*(ci->id3->frequency/100),
|
|
|
|
&sound_samples_done, (int*) &i)) {
|
|
|
|
elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
|
|
|
|
ci->set_elapsed(elapsed_time);
|
2005-10-31 23:34:14 +00:00
|
|
|
}
|
2005-11-02 00:09:42 +00:00
|
|
|
ci->seek_complete();
|
2005-10-31 23:34:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup the length (in samples and bytes) of block i */
|
|
|
|
if (!get_sample_info(&demux_res, i, &sample_duration,
|
|
|
|
&sample_byte_size)) {
|
2006-10-11 17:02:23 +00:00
|
|
|
LOGF("AAC: get_sample_info error\n");
|
2006-01-18 20:22:03 +00:00
|
|
|
err = CODEC_ERROR;
|
2006-04-22 14:40:13 +00:00
|
|
|
goto done;
|
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.
|
|
|
|
*/
|
|
|
|
file_offset = get_sample_offset(&demux_res, i);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
LOGF("AAC: get_sample_offset error\n");
|
|
|
|
err = CODEC_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
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 */
|
2006-03-24 14:02:27 +00:00
|
|
|
buffer=ci->request_buffer(&n,sample_byte_size);
|
2005-10-31 23:34:14 +00:00
|
|
|
|
|
|
|
/* Decode one block - returned samples will be host-endian */
|
2006-10-11 17:02:23 +00:00
|
|
|
NeAACDecDecode(decoder, &frame_info, buffer, n);
|
|
|
|
/* Ignore return value, we access samples in the decoder struct
|
|
|
|
* directly.
|
|
|
|
*/
|
|
|
|
if (frame_info.error > 0) {
|
|
|
|
LOGF("FAAD: decode error '%s'\n", NeAACDecGetErrorMessage(frame_info.error));
|
|
|
|
err = CODEC_ERROR;
|
|
|
|
goto done;
|
2005-10-31 23:34:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance codec buffer */
|
|
|
|
ci->advance_buffer(n);
|
|
|
|
|
|
|
|
/* Output the audio */
|
2006-11-26 18:31:41 +00:00
|
|
|
ci->yield();
|
2007-02-07 00:51:50 +00:00
|
|
|
ci->pcmbuf_insert(decoder->time_out[0], decoder->time_out[1],
|
|
|
|
frame_info.samples >> 1);
|
2005-10-31 23:34:14 +00:00
|
|
|
|
|
|
|
/* Update the elapsed-time indicator */
|
2006-10-11 17:02:23 +00:00
|
|
|
sound_samples_done += sample_duration;
|
|
|
|
elapsed_time = (sound_samples_done * 10) / (ci->id3->frequency / 100);
|
|
|
|
ci->set_elapsed(elapsed_time);
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
/* Keep track of current position - for resuming */
|
2006-10-11 17:02:23 +00:00
|
|
|
ci->set_offset(elapsed_time);
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
i++;
|
|
|
|
}
|
2006-10-11 17:02:23 +00:00
|
|
|
|
2006-04-22 14:40:13 +00:00
|
|
|
err = CODEC_OK;
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2006-04-22 14:40:13 +00:00
|
|
|
done:
|
2007-03-20 13:53:23 +00:00
|
|
|
LOGF("AAC: Decoded %lu samples\n", sound_samples_done);
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2005-10-31 23:34:14 +00:00
|
|
|
if (ci->request_next_track())
|
|
|
|
goto next_track;
|
2005-10-31 20:56:29 +00:00
|
|
|
|
2006-01-18 20:22:03 +00:00
|
|
|
exit:
|
|
|
|
return err;
|
2005-10-31 20:56:29 +00:00
|
|
|
}
|