1
0
Fork 0
forked from len0rd/rockbox

Code policed. Converted to generate internal s3.28 format directly (next

task is removing use of interleaved audio). Could not test ADPCM due to
difficulty in finding files it could play, so tell me if it is broken.


git-svn-id: svn://svn.rockbox.org/rockbox/trunk@9136 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Thom Johansen 2006-03-20 20:32:19 +00:00
parent d8eaefe29a
commit 0968536c4b

View file

@ -22,12 +22,15 @@
CODEC_HEADER CODEC_HEADER
/* Macro that sign extends an unsigned byte */
#define SE(x) ((int32_t)((int8_t)(x)))
struct codec_api *rb; struct codec_api *rb;
/* This codec support WAVE files with the following formats: /* This codec support WAVE files with the following formats:
* - PCM, up to 32 bits, supporting 32 bits playback when useful. * - PCM, up to 32 bits, supporting 32 bits playback when useful.
* - ALAW and MULAW (16 bits compressed on 8 bits). * - ALAW and MULAW (16 bits compressed on 8 bits).
* - DVI_ADPCM (16 bits compressed on 4 bits). * - DVI_ADPCM (16 bits compressed on 3 or 4 bits).
* *
* For a good documentation on WAVE files, see: * For a good documentation on WAVE files, see:
* http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/WAVE/WAVE.html * http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/WAVE/WAVE.html
@ -100,8 +103,6 @@ extern char iedata[];
extern char iend[]; extern char iend[];
#endif #endif
/* Those are lookup tables, so they should be in the idata section
* (fast but small RAM on the coldfire processor) */
static const int16_t alaw2linear16[256] ICONST_ATTR = { static const int16_t alaw2linear16[256] ICONST_ATTR = {
-5504, -5248, -6016, -5760, -4480, -4224, -4992, -5504, -5248, -6016, -5760, -4480, -4224, -4992,
-4736, -7552, -7296, -8064, -7808, -6528, -6272, -4736, -7552, -7296, -8064, -7808, -6528, -6272,
@ -195,17 +196,20 @@ static const uint16_t dvi_adpcm_steptab[ 89 ] ICONST_ATTR = {
7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794,
32767 }; 32767 };
static const int dvi_adpcm_indextab4[ 8 ] ICONST_ATTR = { -1, -1, -1, -1, 2, 4, 6, 8 };
static const int dvi_adpcm_indextab4[8] ICONST_ATTR = {
-1, -1, -1, -1, 2, 4, 6, 8 };
static const int dvi_adpcm_indextab3[4] ICONST_ATTR = { -1, -1, 1, 2 }; static const int dvi_adpcm_indextab3[4] ICONST_ATTR = { -1, -1, 1, 2 };
static int16_t int16_samples[WAV_CHUNK_SIZE] IBSS_ATTR; static int32_t samples[WAV_CHUNK_SIZE] IBSS_ATTR;
static enum codec_status static enum codec_status
decode_dvi_adpcm(struct codec_api *ci, decode_dvi_adpcm(struct codec_api *ci,
const uint8_t *buf, const uint8_t *buf,
int n, int n,
uint16_t channels, uint16_t bitspersample, uint16_t channels, uint16_t bitspersample,
int16_t *pcmout, int32_t *pcmout,
size_t *pcmoutsize); size_t *pcmoutsize);
/* this is the codec entry point */ /* this is the codec entry point */
@ -219,17 +223,15 @@ enum codec_status codec_start(struct codec_api* api)
int bytespersample = 0; int bytespersample = 0;
uint16_t bitspersample; uint16_t bitspersample;
uint32_t i; uint32_t i;
size_t n, wavbufsize; size_t n, bufsize;
int endofstream; int endofstream;
unsigned char *buf; unsigned char *buf;
uint16_t* wavbuf; uint8_t *wavbuf;
long chunksize; long chunksize;
uint16_t formattag = 0; uint16_t formattag = 0;
uint16_t blockalign = 0; uint16_t blockalign = 0;
uint32_t avgbytespersec = 0; uint32_t avgbytespersec = 0;
off_t firstblockposn; /* position of the first block in file */ off_t firstblockposn; /* position of the first block in file */
int shortorlong = 1; /* do we output shorts (1) or longs (2)? */
int32_t * const int32_samples = (int32_t*)int16_samples;
/* Generic codec initialisation */ /* Generic codec initialisation */
rb = api; rb = api;
@ -240,13 +242,13 @@ enum codec_status codec_start(struct codec_api* api)
ci->memset(iedata, 0, iend - iedata); ci->memset(iedata, 0, iend - iedata);
#endif #endif
ci->configure(CODEC_DSP_ENABLE, (bool *)true);
ci->configure(DSP_SET_SAMPLE_DEPTH, (long *)28);
ci->configure(CODEC_SET_FILEBUF_WATERMARK, (int *)(1024*512)); ci->configure(CODEC_SET_FILEBUF_WATERMARK, (int *)(1024*512));
ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*256)); ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*256));
ci->configure(DSP_DITHER, (bool *)false); ci->configure(DSP_DITHER, (bool *)false);
next_track: next_track:
if (codec_init(api)) { if (codec_init(api)) {
i = CODEC_ERROR; i = CODEC_ERROR;
goto exit; goto exit;
@ -310,8 +312,7 @@ enum codec_status codec_start(struct codec_api* api)
goto exit; goto exit;
} }
samplesperblock = buf[26]|(buf[27]<<8); samplesperblock = buf[26]|(buf[27]<<8);
} } else if (formattag == WAVE_FORMAT_EXTENSIBLE) {
else if (formattag == WAVE_FORMAT_EXTENSIBLE) {
if (size < 22) { if (size < 22) {
DEBUGF("CODEC_ERROR: WAVE_FORMAT_EXTENSIBLE is " DEBUGF("CODEC_ERROR: WAVE_FORMAT_EXTENSIBLE is "
"missing extension\n"); "missing extension\n");
@ -325,18 +326,14 @@ enum codec_status codec_start(struct codec_api* api)
formattag = buf[32]|(buf[33]<<8); formattag = buf[32]|(buf[33]<<8);
} }
} }
} } else if (memcmp(buf, "data", 4) == 0) {
else if (memcmp(buf,"data",4)==0) {
numbytes = i; numbytes = i;
i = 0; /* advance to the beginning of data */ i = 0; /* advance to the beginning of data */
} } else if (memcmp(buf, "fact", 4) == 0) {
else if (memcmp(buf,"fact",4)==0) {
/* dwSampleLength */ /* dwSampleLength */
if (i>=4) { if (i >= 4)
totalsamples = (buf[8]|(buf[9]<<8)|(buf[10]<<16)|(buf[11]<<24)); totalsamples = (buf[8]|(buf[9]<<8)|(buf[10]<<16)|(buf[11]<<24));
} } else {
}
else {
DEBUGF("unknown WAVE chunk: '%c%c%c%c', size=%lu\n", DEBUGF("unknown WAVE chunk: '%c%c%c%c', size=%lu\n",
buf[0], buf[1], buf[2], buf[3], i); buf[0], buf[1], buf[2], buf[3], i);
} }
@ -388,23 +385,11 @@ enum codec_status codec_start(struct codec_api* api)
goto exit; goto exit;
} }
ci->configure(CODEC_DSP_ENABLE, (bool *)true);
ci->configure(DSP_SET_FREQUENCY, (long *)(ci->id3->frequency)); ci->configure(DSP_SET_FREQUENCY, (long *)(ci->id3->frequency));
if (bitspersample <= 16) {
ci->configure(DSP_SET_SAMPLE_DEPTH, (int *)(16));
} else {
shortorlong = 2;
ci->configure(DSP_DITHER, (bool *)false);
ci->configure(DSP_SET_SAMPLE_DEPTH, (long *) (32));
ci->configure(DSP_SET_CLIP_MAX, (long *) (2147483647));
ci->configure(DSP_SET_CLIP_MIN, (long *) (-2147483647-1));
}
if (channels == 2) { if (channels == 2) {
ci->configure(DSP_SET_STEREO_MODE, (int *)STEREO_INTERLEAVED); ci->configure(DSP_SET_STEREO_MODE, (long *)STEREO_INTERLEAVED);
} else if (channels == 1) { } else if (channels == 1) {
ci->configure(DSP_SET_STEREO_MODE, (int *)STEREO_MONO); ci->configure(DSP_SET_STEREO_MODE, (long *)STEREO_MONO);
} else { } else {
DEBUGF("CODEC_ERROR: more than 2 channels\n"); DEBUGF("CODEC_ERROR: more than 2 channels\n");
i = CODEC_ERROR; i = CODEC_ERROR;
@ -418,19 +403,17 @@ enum codec_status codec_start(struct codec_api* api)
/* for PCM and derived formats only */ /* for PCM and derived formats only */
bytespersample = (((bitspersample - 1)/8 + 1)*channels); bytespersample = (((bitspersample - 1)/8 + 1)*channels);
totalsamples = numbytes/bytespersample; totalsamples = numbytes/bytespersample;
} } else {
else {
DEBUGF("CODEC_ERROR: cannot compute totalsamples\n"); DEBUGF("CODEC_ERROR: cannot compute totalsamples\n");
i = CODEC_ERROR; i = CODEC_ERROR;
goto exit; goto exit;
} }
} }
firstblockposn = (1024-n); firstblockposn = 1024 - n;
ci->advance_buffer(firstblockposn); ci->advance_buffer(firstblockposn);
/* The main decoder loop */ /* The main decoder loop */
bytesdone = 0; bytesdone = 0;
ci->set_elapsed(0); ci->set_elapsed(0);
endofstream = 0; endofstream = 0;
@ -440,16 +423,14 @@ enum codec_status codec_start(struct codec_api* api)
chunksize = (1 + avgbytespersec / (50*blockalign))*blockalign; chunksize = (1 + avgbytespersec / (50*blockalign))*blockalign;
/* check that the output buffer is big enough (convert to samplespersec, /* check that the output buffer is big enough (convert to samplespersec,
then round to the blockalign multiple below) */ then round to the blockalign multiple below) */
if (((uint64_t)chunksize*ci->id3->frequency*channels*shortorlong) if (((uint64_t)chunksize*ci->id3->frequency*channels*sizeof(long))
/(uint64_t)avgbytespersec >= WAV_CHUNK_SIZE) { /(uint64_t)avgbytespersec >= WAV_CHUNK_SIZE) {
chunksize = ((uint64_t)WAV_CHUNK_SIZE*avgbytespersec chunksize = ((uint64_t)WAV_CHUNK_SIZE*avgbytespersec
/ ((uint64_t)ci->id3->frequency * channels * shortorlong /((uint64_t)ci->id3->frequency*channels*sizeof(long)
*blockalign))*blockalign; *blockalign))*blockalign;
} }
while (!endofstream) { while (!endofstream) {
uint8_t *wavbuf8;
ci->yield(); ci->yield();
if (ci->stop_codec || ci->reload_codec) { if (ci->stop_codec || ci->reload_codec) {
break; break;
@ -464,13 +445,11 @@ enum codec_status codec_start(struct codec_api* api)
/ (1000LL*blockalign))*blockalign; / (1000LL*blockalign))*blockalign;
if (newpos > numbytes) if (newpos > numbytes)
break; break;
if (ci->seek_buffer(firstblockposn + newpos)) { if (ci->seek_buffer(firstblockposn + newpos))
bytesdone = newpos; bytesdone = newpos;
}
ci->seek_complete(); ci->seek_complete();
} }
wavbuf=ci->request_buffer((long *)&n,chunksize); wavbuf = (uint8_t *)ci->request_buffer((long *)&n, chunksize);
wavbuf8 = (uint8_t*)wavbuf;
if (n == 0) if (n == 0)
break; /* End of stream */ break; /* End of stream */
@ -480,48 +459,41 @@ enum codec_status codec_start(struct codec_api* api)
endofstream = 1; endofstream = 1;
} }
wavbufsize = sizeof(int16_samples);
if (formattag == WAVE_FORMAT_PCM) { if (formattag == WAVE_FORMAT_PCM) {
if (bitspersample > 24) { if (bitspersample > 24) {
for (i = 0; i < n; i += 4) { for (i = 0; i < n; i += 4) {
int32_samples[i/4]=(int32_t)(wavbuf8[i]|(wavbuf8[i+1]<<8)| samples[i/4] = (wavbuf[i] >> 3)|
(wavbuf8[i+2]<<16)|(wavbuf8[i+3]<<24)); (wavbuf[i + 1]<<5)|(wavbuf[i + 2]<<13)|
(SE(wavbuf[i + 3])<<21);
} }
wavbufsize = n; bufsize = n;
} } else if (bitspersample > 16) {
else if (bitspersample > 16) {
for (i = 0; i < n; i += 3) { for (i = 0; i < n; i += 3) {
int32_samples[i/3]=(int32_t)((wavbuf8[i]<<8)| samples[i/3] = (wavbuf[i]<<5)|
(wavbuf8[i+1]<<16)|(wavbuf8[i+2]<<24)); (wavbuf[i + 1]<<13)|(SE(wavbuf[i + 2])<<21);
} }
wavbufsize = n*4/3; bufsize = n*4/3;
} else if (bitspersample > 8) {
for (i = 0; i < n; i += 2) {
samples[i/2] = (wavbuf[i]<<13)|(SE(wavbuf[i + 1])<<21);
} }
else if (bitspersample > 8) { bufsize = n*2;
/* Byte-swap data. */ } else {
for (i=0;i<n/2;i++) {
int16_samples[i]=(int16_t)letoh16(wavbuf[i]);
}
wavbufsize = n;
}
else {
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
int16_samples[i] = (wavbuf8[i]<<8) - 0x8000; samples[i] = (wavbuf[i] - 0x80)<<21;
} }
wavbufsize = n*2; bufsize = n*4;
} }
} } else if (formattag == WAVE_FORMAT_ALAW
else if (formattag == WAVE_FORMAT_ALAW || formattag == IBM_FORMAT_ALAW) { || formattag == IBM_FORMAT_ALAW) {
for (i=0;i<n;i++) { for (i = 0; i < n; i++)
int16_samples[i] = alaw2linear16[wavbuf8[i]]; samples[i] = alaw2linear16[wavbuf[i]] << 13;
} bufsize = n*4;
wavbufsize = n*2; } else if (formattag == WAVE_FORMAT_MULAW
} || formattag == IBM_FORMAT_MULAW) {
else if (formattag == WAVE_FORMAT_MULAW || formattag == IBM_FORMAT_MULAW) { for (i = 0; i < n; i++)
for (i=0;i<n;i++) { samples[i] = ulaw2linear16[wavbuf[i]] << 13;
int16_samples[i] = ulaw2linear16[wavbuf8[i]]; bufsize = n*4;
}
wavbufsize = n*2;
} }
else if (formattag == WAVE_FORMAT_DVI_ADPCM) { else if (formattag == WAVE_FORMAT_DVI_ADPCM) {
unsigned int nblocks = chunksize/blockalign; unsigned int nblocks = chunksize/blockalign;
@ -530,35 +502,25 @@ enum codec_status codec_start(struct codec_api* api)
size_t decodedsize = samplesperblock*channels; size_t decodedsize = samplesperblock*channels;
if (decode_dvi_adpcm(ci, ((uint8_t *)wavbuf) + i*blockalign, if (decode_dvi_adpcm(ci, ((uint8_t *)wavbuf) + i*blockalign,
blockalign, channels, bitspersample, blockalign, channels, bitspersample,
int16_samples+i*samplesperblock*channels, samples + i*samplesperblock*channels,
&decodedsize) &decodedsize) != CODEC_OK)
!= CODEC_OK) {
i = CODEC_ERROR; i = CODEC_ERROR;
goto exit; goto exit;
} }
if (decodedsize != samplesperblock) { bufsize = nblocks*samplesperblock*channels*2;
i = CODEC_ERROR; } else {
goto exit;
}
}
wavbufsize = nblocks*samplesperblock*channels*2;
}
else {
DEBUGF("CODEC_ERROR: unsupported format %x\n", formattag); DEBUGF("CODEC_ERROR: unsupported format %x\n", formattag);
i = CODEC_ERROR; i = CODEC_ERROR;
goto exit; goto exit;
} }
while (!ci->pcmbuf_insert((char*)int16_samples, wavbufsize)) { while (!ci->pcmbuf_insert((char *)samples, bufsize))
ci->yield(); ci->yield();
}
ci->advance_buffer(n); ci->advance_buffer(n);
bytesdone += n; bytesdone += n;
if (bytesdone >= numbytes) { if (bytesdone >= numbytes)
endofstream = 1; endofstream = 1;
}
ci->set_elapsed(bytesdone*1000LL/avgbytespersec); ci->set_elapsed(bytesdone*1000LL/avgbytespersec);
} }
@ -575,7 +537,7 @@ decode_dvi_adpcm(struct codec_api* ci,
const uint8_t *buf, const uint8_t *buf,
int n, int n,
uint16_t channels, uint16_t bitspersample, uint16_t channels, uint16_t bitspersample,
int16_t *pcmout, int32_t *pcmout,
size_t *pcmoutsize) size_t *pcmoutsize)
{ {
size_t nsamples = 0; size_t nsamples = 0;
@ -625,7 +587,6 @@ decode_dvi_adpcm(struct codec_api* ci,
buf += 4; buf += 4;
n -= 4; n -= 4;
} }
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
for (c = 0; c < channels; c++) { for (c = 0; c < channels; c++) {
step = dvi_adpcm_steptab[stepindex[c]]; step = dvi_adpcm_steptab[stepindex[c]];
@ -661,20 +622,18 @@ decode_dvi_adpcm(struct codec_api* ci,
sample[c] -= diff; sample[c] -= diff;
if (sample[c] < -32768) if (sample[c] < -32768)
sample[c] = -32768; sample[c] = -32768;
} } else {
else {
sample[c] += diff; sample[c] += diff;
if (sample[c] > 32767) if (sample[c] > 32767)
sample[c] = 32767; sample[c] = 32767;
} }
/* output the new sample */ /* output the new sample */
pcmout[nsamples] = sample[c]; pcmout[nsamples] = sample[c] << 13;
nsamples++; nsamples++;
} }
} }
} }
} } else { /* bitspersample == 3 */
else { /* bitspersample == 3 */
while (n >= channels*12 && (nsamples + 32*channels) <= *pcmoutsize) { while (n >= channels*12 && (nsamples + 32*channels) <= *pcmoutsize) {
for (c = 0; c < channels; c++) { for (c = 0; c < channels; c++) {
uint16_t bitstream = 0; uint16_t bitstream = 0;
@ -737,7 +696,7 @@ decode_dvi_adpcm(struct codec_api* ci,
sample[c] = 32767; sample[c] = 32767;
} }
/* output the new sample */ /* output the new sample */
pcmout[nsamples] = sample[c]; pcmout[nsamples] = sample[c] << 13;
nsamples++; nsamples++;
} }
} }