2005-09-22 21:55:37 +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-09-22 21:55:37 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2005-10-13 11:32:52 +00:00
|
|
|
#include "codeclib.h"
|
2005-10-29 17:12:52 +00:00
|
|
|
#include "libm4a/m4a.h"
|
|
|
|
#include "libalac/decomp.h"
|
2005-09-22 21:55:37 +00:00
|
|
|
|
2006-01-18 00:05:14 +00:00
|
|
|
CODEC_HEADER
|
|
|
|
|
2011-04-18 19:12:51 +00:00
|
|
|
/* The maximum buffer size handled. This amount of bytes is buffered for each
|
|
|
|
* frame. */
|
|
|
|
#define ALAC_BYTE_BUFFER_SIZE 32768
|
|
|
|
|
2010-07-25 22:24:53 +00:00
|
|
|
static int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE] IBSS_ATTR;
|
2005-10-29 17:12:52 +00:00
|
|
|
|
2005-09-22 21:55:37 +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)
|
2005-09-22 21:55:37 +00:00
|
|
|
{
|
2011-04-27 03:08:23 +00:00
|
|
|
if (reason == CODEC_LOAD) {
|
|
|
|
/* Generic codec initialisation */
|
|
|
|
ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED);
|
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, ALAC_OUTPUT_DEPTH-1);
|
|
|
|
}
|
2005-09-22 21:55:37 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_OK;
|
|
|
|
}
|
2005-09-22 21:55:37 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* this is called for each file to process */
|
|
|
|
enum codec_status codec_run(void)
|
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
demux_res_t demux_res;
|
|
|
|
stream_t input_stream;
|
|
|
|
uint32_t samplesdone;
|
|
|
|
uint32_t elapsedtime = 0;
|
|
|
|
int samplesdecoded;
|
|
|
|
unsigned int i;
|
|
|
|
unsigned char* buffer;
|
|
|
|
alac_file alac;
|
|
|
|
intptr_t param;
|
|
|
|
|
|
|
|
/* Clean and initialize decoder structures */
|
|
|
|
memset(&demux_res , 0, sizeof(demux_res));
|
|
|
|
if (codec_init()) {
|
|
|
|
LOGF("ALAC: Error initialising codec\n");
|
|
|
|
return CODEC_ERROR;
|
2010-03-14 16:05:12 +00:00
|
|
|
}
|
|
|
|
|
2013-05-05 04:48:40 +00:00
|
|
|
ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
|
2011-04-27 03:08:23 +00:00
|
|
|
codec_set_replaygain(ci->id3);
|
|
|
|
|
|
|
|
ci->seek_buffer(0);
|
|
|
|
|
|
|
|
stream_create(&input_stream,ci);
|
|
|
|
|
|
|
|
/* Read from ci->id3->offset before calling qtmovie_read. */
|
|
|
|
samplesdone = (uint32_t)(((uint64_t)(ci->id3->offset) * ci->id3->frequency) /
|
|
|
|
(ci->id3->bitrate*128));
|
|
|
|
|
|
|
|
/* 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)) {
|
|
|
|
LOGF("ALAC: Error initialising file\n");
|
|
|
|
return CODEC_ERROR;
|
2005-09-22 21:55:37 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* initialise the sound converter */
|
|
|
|
alac_set_info(&alac, demux_res.codecdata);
|
|
|
|
|
|
|
|
/* Set i for first frame, seek to desired sample position for resuming. */
|
|
|
|
i=0;
|
|
|
|
if (samplesdone > 0) {
|
|
|
|
if (m4a_seek(&demux_res, &input_stream, samplesdone,
|
|
|
|
&samplesdone, (int*) &i)) {
|
|
|
|
elapsedtime = (samplesdone * 10) / (ci->id3->frequency / 100);
|
|
|
|
ci->set_elapsed(elapsedtime);
|
|
|
|
} else {
|
|
|
|
samplesdone = 0;
|
|
|
|
}
|
2005-09-22 21:55:37 +00:00
|
|
|
}
|
|
|
|
|
2011-08-28 07:45:35 +00:00
|
|
|
ci->set_elapsed(elapsedtime);
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* The main decoding loop */
|
|
|
|
while (i < demux_res.num_sample_byte_sizes) {
|
|
|
|
enum codec_command_action action = ci->get_command(¶m);
|
|
|
|
|
|
|
|
if (action == CODEC_ACTION_HALT)
|
|
|
|
break;
|
2005-09-22 21:55:37 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* Request the required number of bytes from the input buffer */
|
|
|
|
buffer=ci->request_buffer(&n, ALAC_BYTE_BUFFER_SIZE);
|
2005-09-22 21:55:37 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* Deal with any pending seek requests */
|
|
|
|
if (action == CODEC_ACTION_SEEK_TIME) {
|
|
|
|
if (m4a_seek(&demux_res, &input_stream,
|
|
|
|
(param/10) * (ci->id3->frequency/100),
|
|
|
|
&samplesdone, (int *)&i)) {
|
|
|
|
elapsedtime=(samplesdone*10)/(ci->id3->frequency/100);
|
|
|
|
}
|
|
|
|
ci->set_elapsed(elapsedtime);
|
|
|
|
ci->seek_complete();
|
|
|
|
}
|
2005-09-22 21:55:37 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* Request the required number of bytes from the input buffer */
|
|
|
|
buffer=ci->request_buffer(&n, ALAC_BYTE_BUFFER_SIZE);
|
2005-09-22 21:55:37 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* Decode one block - returned samples will be host-endian */
|
|
|
|
samplesdecoded=alac_decode_frame(&alac, buffer, outputbuffer, ci->yield);
|
|
|
|
ci->yield();
|
2005-09-22 21:55:37 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* Advance codec buffer by amount of consumed bytes */
|
|
|
|
ci->advance_buffer(alac.bytes_consumed);
|
2005-09-22 21:55:37 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* Output the audio */
|
|
|
|
ci->pcmbuf_insert(outputbuffer[0], outputbuffer[1], samplesdecoded);
|
2005-09-22 21:55:37 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* Update the elapsed-time indicator */
|
|
|
|
samplesdone+=samplesdecoded;
|
|
|
|
elapsedtime=(samplesdone*10)/(ci->id3->frequency/100);
|
|
|
|
ci->set_elapsed(elapsedtime);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
2005-09-22 21:55:37 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
LOGF("ALAC: Decoded %lu samples\n",(unsigned long)samplesdone);
|
|
|
|
return CODEC_OK;
|
2005-09-22 21:55:37 +00:00
|
|
|
}
|