2005-06-07 18:53:01 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2005-10-26 12:35:58 +00:00
|
|
|
* Copyright (C) 2005 Dave Chapman
|
2005-06-07 18:53:01 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2005-10-13 11:32:52 +00:00
|
|
|
#include "codeclib.h"
|
2005-10-26 12:35:58 +00:00
|
|
|
#include <codecs/libffmpegFLAC/decoder.h>
|
2005-06-07 18:53:01 +00:00
|
|
|
|
2006-01-18 00:05:14 +00:00
|
|
|
CODEC_HEADER
|
|
|
|
|
2006-01-08 22:50:14 +00:00
|
|
|
#ifdef USE_IRAM
|
2005-06-07 18:53:01 +00:00
|
|
|
extern char iramcopy[];
|
|
|
|
extern char iramstart[];
|
|
|
|
extern char iramend[];
|
2006-01-08 22:50:14 +00:00
|
|
|
extern char iedata[];
|
|
|
|
extern char iend[];
|
2005-06-07 18:53:01 +00:00
|
|
|
#endif
|
|
|
|
|
2005-10-26 12:35:58 +00:00
|
|
|
struct codec_api* rb;
|
|
|
|
struct codec_api* ci;
|
|
|
|
|
|
|
|
/* The output buffers containing the decoded samples (channels 0 and 1) */
|
|
|
|
int32_t decoded0[MAX_BLOCKSIZE] IBSS_ATTR;
|
|
|
|
int32_t decoded1[MAX_BLOCKSIZE] IBSS_ATTR;
|
|
|
|
|
2005-10-29 01:39:07 +00:00
|
|
|
#define MAX_SUPPORTED_SEEKTABLE_SIZE 5000
|
|
|
|
|
|
|
|
/* Notes about seeking:
|
|
|
|
|
|
|
|
The full seek table consists of:
|
|
|
|
uint64_t sample (only 36 bits are used)
|
|
|
|
uint64_t offset
|
|
|
|
uint32_t blocksize
|
|
|
|
|
|
|
|
We don't store the blocksize (of the target frame) and we also
|
|
|
|
limit the sample and offset values to 32-bits - Rockbox doesn't support
|
|
|
|
files bigger than 2GB on FAT32 filesystems.
|
|
|
|
|
|
|
|
The reference FLAC encoder produces a seek table with points every
|
|
|
|
10 seconds, but this can be overridden by the user when encoding a file.
|
|
|
|
|
|
|
|
With the default settings, a typical 4 minute track will contain
|
|
|
|
24 seek points.
|
|
|
|
|
|
|
|
Taking the extreme case of a Rockbox supported file to be a 2GB (compressed)
|
|
|
|
16-bit/44.1KHz mono stream with a likely uncompressed size of 4GB:
|
|
|
|
Total duration is: 48694 seconds (about 810 minutes - 13.5 hours)
|
|
|
|
Total number of seek points: 4869
|
|
|
|
|
|
|
|
Therefore we limit the number of seek points to 5000. This is a
|
|
|
|
very extreme case, and requires 5000*8=40000 bytes of storage.
|
|
|
|
|
|
|
|
If we come across a FLAC file with more than this number of seekpoints, we
|
|
|
|
just use the first 5000.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct FLACseekpoints {
|
|
|
|
uint32_t sample;
|
|
|
|
uint32_t offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FLACseekpoints seekpoints[MAX_SUPPORTED_SEEKTABLE_SIZE];
|
|
|
|
int nseekpoints;
|
|
|
|
|
2005-10-30 15:07:40 +00:00
|
|
|
static bool flac_init(FLACContext* fc, int first_frame_offset)
|
2005-07-28 18:43:33 +00:00
|
|
|
{
|
2005-10-26 12:35:58 +00:00
|
|
|
unsigned char buf[255];
|
|
|
|
bool found_streaminfo=false;
|
2005-10-29 01:39:07 +00:00
|
|
|
uint32_t seekpoint_hi,seekpoint_lo;
|
|
|
|
uint32_t offset_hi,offset_lo;
|
2005-10-26 12:35:58 +00:00
|
|
|
int endofmetadata=0;
|
|
|
|
int blocklength;
|
2005-10-29 01:39:07 +00:00
|
|
|
int n;
|
|
|
|
uint32_t* p;
|
2005-07-28 18:43:33 +00:00
|
|
|
|
2005-10-27 13:18:05 +00:00
|
|
|
ci->memset(fc,0,sizeof(FLACContext));
|
2005-10-29 01:39:07 +00:00
|
|
|
nseekpoints=0;
|
2005-10-27 13:18:05 +00:00
|
|
|
|
2005-10-30 15:07:40 +00:00
|
|
|
/* Skip any foreign tags at start of file */
|
|
|
|
ci->seek_buffer(first_frame_offset);
|
|
|
|
|
|
|
|
fc->metadatalength = first_frame_offset;
|
|
|
|
|
2005-10-26 12:35:58 +00:00
|
|
|
if (ci->read_filebuf(buf, 4) < 4)
|
|
|
|
{
|
|
|
|
return false;
|
2005-07-28 18:43:33 +00:00
|
|
|
}
|
|
|
|
|
2005-10-26 12:35:58 +00:00
|
|
|
if (ci->memcmp(buf,"fLaC",4) != 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2005-10-30 15:07:40 +00:00
|
|
|
fc->metadatalength += 4;
|
2005-07-28 18:43:33 +00:00
|
|
|
|
2005-10-26 12:35:58 +00:00
|
|
|
while (!endofmetadata) {
|
|
|
|
if (ci->read_filebuf(buf, 4) < 4)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2005-07-28 18:43:33 +00:00
|
|
|
|
2005-10-26 12:35:58 +00:00
|
|
|
endofmetadata=(buf[0]&0x80);
|
|
|
|
blocklength = (buf[1] << 16) | (buf[2] << 8) | buf[3];
|
|
|
|
fc->metadatalength+=blocklength+4;
|
2005-07-28 18:43:33 +00:00
|
|
|
|
2005-10-26 12:35:58 +00:00
|
|
|
if ((buf[0] & 0x7f) == 0) /* 0 is the STREAMINFO block */
|
|
|
|
{
|
|
|
|
/* FIXME: Don't trust the value of blocklength */
|
|
|
|
if (ci->read_filebuf(buf, blocklength) < 0)
|
|
|
|
{
|
|
|
|
return false;
|
2005-07-28 18:43:33 +00:00
|
|
|
}
|
2005-10-26 12:35:58 +00:00
|
|
|
|
|
|
|
fc->filesize = ci->filesize;
|
|
|
|
fc->min_blocksize = (buf[0] << 8) | buf[1];
|
|
|
|
fc->max_blocksize = (buf[2] << 8) | buf[3];
|
|
|
|
fc->min_framesize = (buf[4] << 16) | (buf[5] << 8) | buf[6];
|
|
|
|
fc->max_framesize = (buf[7] << 16) | (buf[8] << 8) | buf[9];
|
|
|
|
fc->samplerate = (buf[10] << 12) | (buf[11] << 4)
|
|
|
|
| ((buf[12] & 0xf0) >> 4);
|
|
|
|
fc->channels = ((buf[12]&0x0e)>>1) + 1;
|
|
|
|
fc->bps = (((buf[12]&0x01) << 4) | ((buf[13]&0xf0)>>4) ) + 1;
|
|
|
|
|
|
|
|
/* totalsamples is a 36-bit field, but we assume <= 32 bits are
|
|
|
|
used */
|
|
|
|
fc->totalsamples = (buf[14] << 24) | (buf[15] << 16)
|
|
|
|
| (buf[16] << 8) | buf[17];
|
|
|
|
|
|
|
|
/* Calculate track length (in ms) and estimate the bitrate
|
|
|
|
(in kbit/s) */
|
|
|
|
fc->length = (fc->totalsamples / fc->samplerate) * 1000;
|
|
|
|
|
|
|
|
found_streaminfo=true;
|
|
|
|
} else if ((buf[0] & 0x7f) == 3) { /* 3 is the SEEKTABLE block */
|
2005-10-29 01:39:07 +00:00
|
|
|
while ((nseekpoints < MAX_SUPPORTED_SEEKTABLE_SIZE) &&
|
|
|
|
(blocklength >= 18)) {
|
|
|
|
n=ci->read_filebuf(buf,18);
|
|
|
|
if (n < 18) return false;
|
|
|
|
blocklength-=n;
|
|
|
|
|
|
|
|
p=(uint32_t*)buf;
|
|
|
|
seekpoint_hi=betoh32(*(p++));
|
|
|
|
seekpoint_lo=betoh32(*(p++));
|
|
|
|
offset_hi=betoh32(*(p++));
|
|
|
|
offset_lo=betoh32(*(p++));
|
|
|
|
|
|
|
|
if ((seekpoint_hi == 0) && (seekpoint_lo != 0xffffffff) &&
|
|
|
|
(offset_hi == 0)) {
|
|
|
|
seekpoints[nseekpoints].sample=seekpoint_lo;
|
|
|
|
seekpoints[nseekpoints].offset=offset_lo;
|
|
|
|
nseekpoints++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Skip any unread seekpoints */
|
|
|
|
if (blocklength > 0)
|
|
|
|
ci->advance_buffer(blocklength);
|
2005-07-28 18:43:33 +00:00
|
|
|
} else {
|
2005-10-26 12:35:58 +00:00
|
|
|
/* Skip to next metadata block */
|
|
|
|
ci->advance_buffer(blocklength);
|
2005-07-28 18:43:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-26 12:35:58 +00:00
|
|
|
if (found_streaminfo) {
|
|
|
|
fc->bitrate = ((fc->filesize-fc->metadatalength) * 8) / fc->length;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2005-07-28 18:43:33 +00:00
|
|
|
}
|
|
|
|
|
2005-10-29 01:39:07 +00:00
|
|
|
/* A very simple seek implementation - seek to the seekpoint before
|
|
|
|
the target sample.
|
|
|
|
|
|
|
|
This needs to be improved to seek with greater accuracy
|
|
|
|
*/
|
|
|
|
bool flac_seek(FLACContext* fc, uint32_t newsample) {
|
|
|
|
uint32_t offset;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (nseekpoints==0) {
|
|
|
|
offset=0;
|
|
|
|
} else {
|
|
|
|
i=nseekpoints-1;
|
|
|
|
while ((i > 0) && (seekpoints[i].sample > newsample)) {
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((i==0) && (seekpoints[i].sample > newsample)) {
|
|
|
|
offset=0;
|
|
|
|
} else {
|
|
|
|
offset=seekpoints[i].offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
offset+=fc->metadatalength;
|
|
|
|
|
2005-10-30 15:07:40 +00:00
|
|
|
if (ci->seek_buffer(offset)) {
|
2005-10-29 01:39:07 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-22 19:41:30 +00:00
|
|
|
/* this is the codec entry point */
|
2005-06-22 19:55:09 +00:00
|
|
|
enum codec_status codec_start(struct codec_api* api)
|
2005-06-07 18:53:01 +00:00
|
|
|
{
|
2005-10-30 07:38:52 +00:00
|
|
|
int8_t *buf;
|
2005-10-26 12:35:58 +00:00
|
|
|
FLACContext fc;
|
|
|
|
uint32_t samplesdone;
|
|
|
|
uint32_t elapsedtime;
|
2005-10-30 07:38:52 +00:00
|
|
|
long bytesleft;
|
2005-10-26 12:35:58 +00:00
|
|
|
int consumed;
|
2005-10-28 20:09:47 +00:00
|
|
|
int res;
|
|
|
|
int frame;
|
2005-06-07 18:53:01 +00:00
|
|
|
|
2005-10-26 12:35:58 +00:00
|
|
|
/* Generic codec initialisation */
|
|
|
|
rb = api;
|
2006-01-18 00:05:14 +00:00
|
|
|
ci = api;
|
2005-06-07 18:53:01 +00:00
|
|
|
|
2006-01-08 22:50:14 +00:00
|
|
|
#ifdef USE_IRAM
|
2005-09-07 02:22:26 +00:00
|
|
|
ci->memcpy(iramstart, iramcopy, iramend-iramstart);
|
2006-01-08 22:50:14 +00:00
|
|
|
ci->memset(iedata, 0, iend - iedata);
|
2005-06-07 18:53:01 +00:00
|
|
|
#endif
|
|
|
|
|
2005-07-28 18:43:33 +00:00
|
|
|
ci->configure(CODEC_SET_FILEBUF_WATERMARK, (int *)(1024*512));
|
2005-10-26 12:35:58 +00:00
|
|
|
ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*128));
|
2005-06-07 18:53:01 +00:00
|
|
|
|
2005-07-28 18:43:33 +00:00
|
|
|
ci->configure(CODEC_DSP_ENABLE, (bool *)true);
|
|
|
|
ci->configure(DSP_DITHER, (bool *)false);
|
2005-10-26 12:35:58 +00:00
|
|
|
ci->configure(DSP_SET_STEREO_MODE, (long *)STEREO_NONINTERLEAVED);
|
2005-10-30 22:06:42 +00:00
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, (int *)(FLAC_OUTPUT_DEPTH-1));
|
2005-10-26 12:35:58 +00:00
|
|
|
|
|
|
|
next_track:
|
2005-06-07 18:53:01 +00:00
|
|
|
|
2005-10-30 15:07:40 +00:00
|
|
|
if (codec_init(api)) {
|
|
|
|
LOGF("FLAC: Error initialising codec\n");
|
|
|
|
return CODEC_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!flac_init(&fc,ci->id3->first_frame_offset)) {
|
2005-10-26 12:35:58 +00:00
|
|
|
LOGF("FLAC: Error initialising codec\n");
|
2005-09-07 02:22:26 +00:00
|
|
|
return CODEC_ERROR;
|
2005-07-28 18:43:33 +00:00
|
|
|
}
|
|
|
|
|
2005-10-30 07:38:52 +00:00
|
|
|
while (!*ci->taginfo_ready)
|
2005-09-07 02:22:26 +00:00
|
|
|
ci->yield();
|
2005-06-26 19:41:29 +00:00
|
|
|
|
2005-09-07 02:22:26 +00:00
|
|
|
ci->configure(DSP_SET_FREQUENCY, (long *)(ci->id3->frequency));
|
2005-11-02 17:26:47 +00:00
|
|
|
codec_set_replaygain(ci->id3);
|
2005-07-28 18:43:33 +00:00
|
|
|
|
2005-10-26 12:35:58 +00:00
|
|
|
/* The main decoding loop */
|
2005-10-27 16:59:23 +00:00
|
|
|
samplesdone=0;
|
2005-10-28 20:09:47 +00:00
|
|
|
frame=0;
|
2005-10-30 07:38:52 +00:00
|
|
|
buf = ci->request_buffer(&bytesleft, MAX_FRAMESIZE);
|
2005-10-26 12:35:58 +00:00
|
|
|
while (bytesleft) {
|
2005-09-07 02:22:26 +00:00
|
|
|
ci->yield();
|
2005-07-28 18:43:33 +00:00
|
|
|
if (ci->stop_codec || ci->reload_codec) {
|
2005-09-07 02:22:26 +00:00
|
|
|
break;
|
2005-07-28 18:43:33 +00:00
|
|
|
}
|
|
|
|
|
2005-10-26 12:35:58 +00:00
|
|
|
/* Deal with any pending seek requests */
|
2005-07-28 18:43:33 +00:00
|
|
|
if (ci->seek_time) {
|
2005-11-06 19:18:04 +00:00
|
|
|
if (flac_seek(&fc,((ci->seek_time-1)/20)*(ci->id3->frequency/50))) {
|
2005-10-29 01:39:07 +00:00
|
|
|
/* Refill the input buffer */
|
2005-10-30 07:38:52 +00:00
|
|
|
buf = ci->request_buffer(&bytesleft, MAX_FRAMESIZE);
|
2005-10-29 01:39:07 +00:00
|
|
|
}
|
2005-11-02 00:09:42 +00:00
|
|
|
ci->seek_complete();
|
2005-10-26 12:35:58 +00:00
|
|
|
}
|
2005-07-28 18:43:33 +00:00
|
|
|
|
2005-10-28 20:09:47 +00:00
|
|
|
if((res=flac_decode_frame(&fc,decoded0,decoded1,buf,
|
|
|
|
bytesleft,ci->yield)) < 0) {
|
|
|
|
LOGF("FLAC: Frame %d, error %d\n",frame,res);
|
2005-10-26 12:35:58 +00:00
|
|
|
return CODEC_ERROR;
|
2005-07-28 18:43:33 +00:00
|
|
|
}
|
2005-10-26 12:35:58 +00:00
|
|
|
consumed=fc.gb.index/8;
|
2005-10-28 20:09:47 +00:00
|
|
|
frame++;
|
2005-07-28 18:43:33 +00:00
|
|
|
|
2005-10-26 12:35:58 +00:00
|
|
|
ci->yield();
|
|
|
|
while(!ci->pcmbuf_insert_split((char*)decoded0,(char*)decoded1,
|
|
|
|
fc.blocksize*4)) {
|
|
|
|
ci->yield();
|
|
|
|
}
|
2005-06-07 18:53:01 +00:00
|
|
|
|
2005-10-26 12:35:58 +00:00
|
|
|
/* Update the elapsed-time indicator */
|
|
|
|
samplesdone=fc.samplenumber+fc.blocksize;
|
|
|
|
elapsedtime=(samplesdone*10)/(ci->id3->frequency/100);
|
|
|
|
ci->set_elapsed(elapsedtime);
|
2005-06-07 20:09:53 +00:00
|
|
|
|
2005-10-30 07:38:52 +00:00
|
|
|
ci->advance_buffer(consumed);
|
2005-10-26 12:35:58 +00:00
|
|
|
|
2005-10-30 07:38:52 +00:00
|
|
|
buf = ci->request_buffer(&bytesleft, MAX_FRAMESIZE);
|
2005-07-28 18:43:33 +00:00
|
|
|
}
|
2005-10-26 12:35:58 +00:00
|
|
|
LOGF("FLAC: Decoded %d samples\n",samplesdone);
|
|
|
|
|
|
|
|
if (ci->request_next_track())
|
|
|
|
goto next_track;
|
2005-06-07 18:53:01 +00:00
|
|
|
|
2005-07-28 18:43:33 +00:00
|
|
|
return CODEC_OK;
|
2005-06-07 18:53:01 +00:00
|
|
|
}
|