forked from len0rd/rockbox
Streamlined WavPack decoder by utilizing dsp functionality where it was
applicable (like mono conversion and clipping) and eliminating the conversion to 16-bit samples (everything is now returned as 28-bit). This reduced boost ratio (on iRiver) by about 7% on those tracks that require it. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@8598 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
parent
f17e35d27b
commit
ff40af42ba
2 changed files with 22 additions and 110 deletions
|
|
@ -675,11 +675,17 @@ static void decorr_mono_pass (struct decorr_pass *dpp, long *buffer, long sample
|
||||||
// it is clipped and shifted in a single operation. Otherwise, if it's
|
// it is clipped and shifted in a single operation. Otherwise, if it's
|
||||||
// lossless then the last step is to apply the final shift (if any).
|
// lossless then the last step is to apply the final shift (if any).
|
||||||
|
|
||||||
|
// This function has been modified for RockBox to return all integer samples
|
||||||
|
// as 28-bits, and clipping (for lossy mode) has been eliminated because this
|
||||||
|
// now happens in the dsp module.
|
||||||
|
|
||||||
static void fixup_samples (WavpackStream *wps, long *buffer, ulong sample_count)
|
static void fixup_samples (WavpackStream *wps, long *buffer, ulong sample_count)
|
||||||
{
|
{
|
||||||
ulong flags = wps->wphdr.flags;
|
ulong flags = wps->wphdr.flags;
|
||||||
int shift = (flags & SHIFT_MASK) >> SHIFT_LSB;
|
int shift = (flags & SHIFT_MASK) >> SHIFT_LSB;
|
||||||
|
|
||||||
|
shift += 20 - (flags & BYTES_STORED) * 8; // this provides RockBox with 28-bit data
|
||||||
|
|
||||||
if (flags & FLOAT_DATA) {
|
if (flags & FLOAT_DATA) {
|
||||||
float_values (wps, buffer, (flags & MONO_FLAG) ? sample_count : sample_count * 2);
|
float_values (wps, buffer, (flags & MONO_FLAG) ? sample_count : sample_count * 2);
|
||||||
return;
|
return;
|
||||||
|
|
@ -689,7 +695,6 @@ static void fixup_samples (WavpackStream *wps, long *buffer, ulong sample_count)
|
||||||
ulong count = (flags & MONO_FLAG) ? sample_count : sample_count * 2;
|
ulong count = (flags & MONO_FLAG) ? sample_count : sample_count * 2;
|
||||||
int sent_bits = wps->int32_sent_bits, zeros = wps->int32_zeros;
|
int sent_bits = wps->int32_sent_bits, zeros = wps->int32_zeros;
|
||||||
int ones = wps->int32_ones, dups = wps->int32_dups;
|
int ones = wps->int32_ones, dups = wps->int32_dups;
|
||||||
// ulong mask = (1 << sent_bits) - 1;
|
|
||||||
long *dptr = buffer;
|
long *dptr = buffer;
|
||||||
|
|
||||||
if (!(flags & HYBRID_FLAG) && !sent_bits && (zeros + ones + dups))
|
if (!(flags & HYBRID_FLAG) && !sent_bits && (zeros + ones + dups))
|
||||||
|
|
@ -707,51 +712,22 @@ static void fixup_samples (WavpackStream *wps, long *buffer, ulong sample_count)
|
||||||
shift += zeros + sent_bits + ones + dups;
|
shift += zeros + sent_bits + ones + dups;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (flags & HYBRID_FLAG) {
|
if (shift > 0) {
|
||||||
long min_value, max_value, min_shifted, max_shifted;
|
|
||||||
|
|
||||||
switch (flags & BYTES_STORED) {
|
|
||||||
case 0:
|
|
||||||
min_shifted = (min_value = -128 >> shift) << shift;
|
|
||||||
max_shifted = (max_value = 127 >> shift) << shift;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 1:
|
|
||||||
min_shifted = (min_value = -32768 >> shift) << shift;
|
|
||||||
max_shifted = (max_value = 32767 >> shift) << shift;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 2:
|
|
||||||
min_shifted = (min_value = -8388608 >> shift) << shift;
|
|
||||||
max_shifted = (max_value = 8388607 >> shift) << shift;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 3:
|
|
||||||
default:
|
|
||||||
min_shifted = (min_value = (long) 0x80000000 >> shift) << shift;
|
|
||||||
max_shifted = (max_value = (long) 0x7FFFFFFF >> shift) << shift;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(flags & MONO_FLAG))
|
|
||||||
sample_count *= 2;
|
|
||||||
|
|
||||||
while (sample_count--) {
|
|
||||||
if (*buffer < min_value)
|
|
||||||
*buffer++ = min_shifted;
|
|
||||||
else if (*buffer > max_value)
|
|
||||||
*buffer++ = max_shifted;
|
|
||||||
else
|
|
||||||
*buffer++ <<= shift;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (shift) {
|
|
||||||
if (!(flags & MONO_FLAG))
|
if (!(flags & MONO_FLAG))
|
||||||
sample_count *= 2;
|
sample_count *= 2;
|
||||||
|
|
||||||
while (sample_count--)
|
while (sample_count--)
|
||||||
*buffer++ <<= shift;
|
*buffer++ <<= shift;
|
||||||
}
|
}
|
||||||
|
else if (shift < 0) {
|
||||||
|
shift = -shift;
|
||||||
|
|
||||||
|
if (!(flags & MONO_FLAG))
|
||||||
|
sample_count *= 2;
|
||||||
|
|
||||||
|
while (sample_count--)
|
||||||
|
*buffer++ >>= shift;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function checks the crc value(s) for an unpacked block, returning the
|
// This function checks the crc value(s) for an unpacked block, returning the
|
||||||
|
|
|
||||||
|
|
@ -24,9 +24,6 @@ CODEC_HEADER
|
||||||
|
|
||||||
static struct codec_api *ci;
|
static struct codec_api *ci;
|
||||||
|
|
||||||
#define FORCE_DSP_USE /* fixes some WavPack bugs; adds about 12% to boost ratio
|
|
||||||
(when DSP would not have been used) */
|
|
||||||
|
|
||||||
#define BUFFER_SIZE 4096
|
#define BUFFER_SIZE 4096
|
||||||
|
|
||||||
static long temp_buffer [BUFFER_SIZE] IBSS_ATTR;
|
static long temp_buffer [BUFFER_SIZE] IBSS_ATTR;
|
||||||
|
|
@ -66,8 +63,7 @@ enum codec_status codec_start(struct codec_api* api)
|
||||||
ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*128));
|
ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*128));
|
||||||
|
|
||||||
ci->configure(DSP_DITHER, (bool *)false);
|
ci->configure(DSP_DITHER, (bool *)false);
|
||||||
ci->configure(DSP_SET_STEREO_MODE, (int *)STEREO_INTERLEAVED);
|
ci->configure(DSP_SET_SAMPLE_DEPTH, (int *)(27)); // should be 28...
|
||||||
ci->configure(DSP_SET_SAMPLE_DEPTH, (int *)(16));
|
|
||||||
|
|
||||||
next_track:
|
next_track:
|
||||||
|
|
||||||
|
|
@ -79,20 +75,9 @@ enum codec_status codec_start(struct codec_api* api)
|
||||||
while (!*ci->taginfo_ready && !ci->stop_codec)
|
while (!*ci->taginfo_ready && !ci->stop_codec)
|
||||||
ci->sleep(1);
|
ci->sleep(1);
|
||||||
|
|
||||||
#ifdef FORCE_DSP_USE
|
|
||||||
ci->configure(CODEC_DSP_ENABLE, (bool *)true);
|
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));
|
||||||
codec_set_replaygain(ci->id3);
|
codec_set_replaygain(ci->id3);
|
||||||
#else
|
|
||||||
if (ci->id3->frequency != NATIVE_FREQUENCY ||
|
|
||||||
ci->global_settings->replaygain) {
|
|
||||||
ci->configure(CODEC_DSP_ENABLE, (bool *)true);
|
|
||||||
ci->configure(DSP_SET_FREQUENCY, (long *)(ci->id3->frequency));
|
|
||||||
codec_set_replaygain(ci->id3);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
ci->configure(CODEC_DSP_ENABLE, (bool *)false);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Create a decoder instance */
|
/* Create a decoder instance */
|
||||||
wpc = WavpackOpenFileInput (read_callback, error);
|
wpc = WavpackOpenFileInput (read_callback, error);
|
||||||
|
|
@ -104,6 +89,7 @@ enum codec_status codec_start(struct codec_api* api)
|
||||||
|
|
||||||
bps = WavpackGetBytesPerSample (wpc);
|
bps = WavpackGetBytesPerSample (wpc);
|
||||||
nchans = WavpackGetReducedChannels (wpc);
|
nchans = WavpackGetReducedChannels (wpc);
|
||||||
|
ci->configure(DSP_SET_STEREO_MODE, nchans == 2 ? (int *)STEREO_INTERLEAVED : (int *)STEREO_MONO);
|
||||||
sr_100 = ci->id3->frequency / 100;
|
sr_100 = ci->id3->frequency / 100;
|
||||||
|
|
||||||
ci->set_elapsed (0);
|
ci->set_elapsed (0);
|
||||||
|
|
@ -141,71 +127,21 @@ enum codec_status codec_start(struct codec_api* api)
|
||||||
ci->yield ();
|
ci->yield ();
|
||||||
}
|
}
|
||||||
|
|
||||||
nsamples = WavpackUnpackSamples (wpc, temp_buffer, BUFFER_SIZE / 2);
|
nsamples = WavpackUnpackSamples (wpc, temp_buffer, BUFFER_SIZE / nchans);
|
||||||
|
|
||||||
if (!nsamples || ci->stop_codec || ci->reload_codec)
|
if (!nsamples || ci->stop_codec || ci->reload_codec)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* convert mono to stereo here, in place */
|
|
||||||
|
|
||||||
if (nchans == 1) {
|
|
||||||
long *dst = temp_buffer + (nsamples * 2);
|
|
||||||
long *src = temp_buffer + nsamples;
|
|
||||||
long count = nsamples;
|
|
||||||
|
|
||||||
while (count--) {
|
|
||||||
*--dst = *--src;
|
|
||||||
*--dst = *src;
|
|
||||||
if (!(count & 0x7f))
|
|
||||||
ci->yield ();
|
ci->yield ();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bps == 1) {
|
|
||||||
short *dst = (short *) temp_buffer;
|
|
||||||
long *src = temp_buffer;
|
|
||||||
long count = nsamples;
|
|
||||||
|
|
||||||
while (count--) {
|
|
||||||
*dst++ = *src++ << 8;
|
|
||||||
*dst++ = *src++ << 8;
|
|
||||||
if (!(count & 0x7f))
|
|
||||||
ci->yield ();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (bps == 2) {
|
|
||||||
short *dst = (short *) temp_buffer;
|
|
||||||
long *src = temp_buffer;
|
|
||||||
long count = nsamples;
|
|
||||||
|
|
||||||
while (count--) {
|
|
||||||
*dst++ = *src++;
|
|
||||||
*dst++ = *src++;
|
|
||||||
if (!(count & 0x7f))
|
|
||||||
ci->yield ();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
short *dst = (short *) temp_buffer;
|
|
||||||
int shift = (bps - 2) * 8;
|
|
||||||
long *src = temp_buffer;
|
|
||||||
long count = nsamples;
|
|
||||||
|
|
||||||
while (count--) {
|
|
||||||
*dst++ = *src++ >> shift;
|
|
||||||
*dst++ = *src++ >> shift;
|
|
||||||
if (!(count & 0x7f))
|
|
||||||
ci->yield ();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ci->stop_codec || ci->reload_codec)
|
if (ci->stop_codec || ci->reload_codec)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
while (!ci->pcmbuf_insert ((char *) temp_buffer, nsamples * 4))
|
while (!ci->pcmbuf_insert ((char *) temp_buffer, nsamples * nchans * 4))
|
||||||
ci->sleep (1);
|
ci->sleep (1);
|
||||||
|
|
||||||
ci->set_elapsed (WavpackGetSampleIndex (wpc) / sr_100 * 10);
|
ci->set_elapsed (WavpackGetSampleIndex (wpc) / sr_100 * 10);
|
||||||
|
ci->yield ();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ci->request_next_track())
|
if (ci->request_next_track())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue