From bcf97a480115aa653af9e0eb5c012fc3673bc00f Mon Sep 17 00:00:00 2001 From: Dave Bryant Date: Sun, 12 Aug 2007 06:36:06 +0000 Subject: Move WavPack metadata parsing to its own file and add handling of non-standard sampling rates git-svn-id: svn://svn.rockbox.org/rockbox/trunk@14289 a1c6a512-1295-4272-9138-f99709370657 --- apps/SOURCES | 1 + apps/codecs/libwavpack/metadata.c | 3 + apps/codecs/libwavpack/unpack.c | 16 +++++ apps/codecs/libwavpack/wavpack.h | 2 + apps/codecs/wavpack.c | 5 +- apps/metadata.c | 59 +----------------- apps/metadata/metadata_parsers.h | 1 + apps/metadata/wavpack.c | 122 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 148 insertions(+), 61 deletions(-) create mode 100644 apps/metadata/wavpack.c diff --git a/apps/SOURCES b/apps/SOURCES index bfa4490153..a8a4d0c91e 100644 --- a/apps/SOURCES +++ b/apps/SOURCES @@ -121,6 +121,7 @@ metadata/sid.c metadata/spc.c metadata/vorbis.c metadata/wave.c +metadata/wavpack.c #endif #ifdef HAVE_TAGCACHE tagcache.c diff --git a/apps/codecs/libwavpack/metadata.c b/apps/codecs/libwavpack/metadata.c index c7f2d61841..c944093b19 100644 --- a/apps/codecs/libwavpack/metadata.c +++ b/apps/codecs/libwavpack/metadata.c @@ -102,6 +102,9 @@ int process_metadata (WavpackContext *wpc, WavpackMetadata *wpmd) case ID_CHANNEL_INFO: return read_channel_info (wpc, wpmd); + case ID_SAMPLE_RATE: + return read_sample_rate (wpc, wpmd); + case ID_CONFIG_BLOCK: return read_config_info (wpc, wpmd); diff --git a/apps/codecs/libwavpack/unpack.c b/apps/codecs/libwavpack/unpack.c index f2eca7619f..69252f24ad 100644 --- a/apps/codecs/libwavpack/unpack.c +++ b/apps/codecs/libwavpack/unpack.c @@ -270,6 +270,22 @@ int read_config_info (WavpackContext *wpc, WavpackMetadata *wpmd) return TRUE; } +// Read non-standard sampling rate from metadata. + +int read_sample_rate (WavpackContext *wpc, WavpackMetadata *wpmd) +{ + int bytecnt = wpmd->byte_length; + uchar *byteptr = wpmd->data; + + if (bytecnt == 3) { + wpc->config.sample_rate = (int32_t) *byteptr++; + wpc->config.sample_rate |= (int32_t) *byteptr++ << 8; + wpc->config.sample_rate |= (int32_t) *byteptr++ << 16; + } + + return TRUE; +} + // This monster actually unpacks the WavPack bitstream(s) into the specified // buffer as 32-bit integers or floats (depending on orignal data). Lossy // samples will be clipped to their original limits (i.e. 8-bit samples are diff --git a/apps/codecs/libwavpack/wavpack.h b/apps/codecs/libwavpack/wavpack.h index a000438081..e2952115e1 100644 --- a/apps/codecs/libwavpack/wavpack.h +++ b/apps/codecs/libwavpack/wavpack.h @@ -114,6 +114,7 @@ typedef struct { #define ID_CUESHEET (ID_OPTIONAL_DATA | 0x4) #define ID_CONFIG_BLOCK (ID_OPTIONAL_DATA | 0x5) #define ID_MD5_CHECKSUM (ID_OPTIONAL_DATA | 0x6) +#define ID_SAMPLE_RATE (ID_OPTIONAL_DATA | 0x7) ///////////////////////// WavPack Configuration /////////////////////////////// @@ -364,6 +365,7 @@ int read_float_info (WavpackStream *wps, WavpackMetadata *wpmd); int read_int32_info (WavpackStream *wps, WavpackMetadata *wpmd); int read_channel_info (WavpackContext *wpc, WavpackMetadata *wpmd); int read_config_info (WavpackContext *wpc, WavpackMetadata *wpmd); +int read_sample_rate (WavpackContext *wpc, WavpackMetadata *wpmd); int32_t unpack_samples (WavpackContext *wpc, int32_t *buffer, uint32_t sample_count); int check_crc_error (WavpackContext *wpc); diff --git a/apps/codecs/wavpack.c b/apps/codecs/wavpack.c index 680673fec7..1485eedf8b 100644 --- a/apps/codecs/wavpack.c +++ b/apps/codecs/wavpack.c @@ -57,9 +57,6 @@ enum codec_status codec_main(void) while (!*ci->taginfo_ready && !ci->stop_codec) ci->sleep(1); - ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency); - codec_set_replaygain(ci->id3); - /* Create a decoder instance */ wpc = WavpackOpenFileInput (read_callback, error); @@ -68,6 +65,8 @@ enum codec_status codec_main(void) goto done; } + ci->configure(DSP_SWITCH_FREQUENCY, WavpackGetSampleRate (wpc)); + codec_set_replaygain(ci->id3); bps = WavpackGetBytesPerSample (wpc); nchans = WavpackGetReducedChannels (wpc); ci->configure(DSP_SET_STEREO_MODE, nchans == 2 ? STEREO_INTERLEAVED : STEREO_MONO); diff --git a/apps/metadata.c b/apps/metadata.c index 15813c679d..5796a5669f 100644 --- a/apps/metadata.c +++ b/apps/metadata.c @@ -51,12 +51,6 @@ static const unsigned short a52_441framesizes[] = 1254 * 2, 1393 * 2, 1394 * 2 }; -static const long wavpack_sample_rates [] = -{ - 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000, - 32000, 44100, 48000, 64000, 88200, 96000, 192000 -}; - #endif /* CONFIG_CODEC == SWCODEC */ @@ -179,59 +173,8 @@ bool get_metadata(struct track_info* track, int fd, const char* trackname, break; case AFMT_WAVPACK: - /* A simple parser to read basic information from a WavPack file. This - * now works with self-extrating WavPack files. This no longer fails on - * WavPack files containing floating-point audio data because these are - * now converted to standard Rockbox format in the decoder. - */ - - /* Use the trackname part of the id3 structure as a temporary buffer */ - buf = (unsigned char *)track->id3.path; - - for (i = 0; i < 256; ++i) { - - /* at every 256 bytes into file, try to read a WavPack header */ - - if ((lseek(fd, i * 256, SEEK_SET) < 0) || (read(fd, buf, 32) < 32)) - { - return false; - } - - /* if valid WavPack 4 header version & not floating data, break */ - - if (memcmp (buf, "wvpk", 4) == 0 && buf [9] == 4 && - (buf [8] >= 2 && buf [8] <= 0x10)) - { - break; - } - } - - if (i == 256) { - logf ("%s is not a WavPack file\n", trackname); + if (!get_wavpack_metadata(fd, &(track->id3))) return false; - } - - track->id3.vbr = true; /* All WavPack files are VBR */ - track->id3.filesize = filesize (fd); - - if ((buf [20] | buf [21] | buf [22] | buf [23]) && - (buf [12] & buf [13] & buf [14] & buf [15]) != 0xff) - { - int srindx = ((buf [26] >> 7) & 1) + ((buf [27] << 1) & 14); - - if (srindx == 15) - { - track->id3.frequency = 44100; - } - else - { - track->id3.frequency = wavpack_sample_rates[srindx]; - } - - totalsamples = get_long_le(&buf[12]); - track->id3.length = totalsamples / (track->id3.frequency / 100) * 10; - track->id3.bitrate = filesize (fd) / (track->id3.length / 8); - } read_ape_tags(fd, &track->id3); /* use any apetag info we find */ break; diff --git a/apps/metadata/metadata_parsers.h b/apps/metadata/metadata_parsers.h index 5fc674d31b..315305b0df 100644 --- a/apps/metadata/metadata_parsers.h +++ b/apps/metadata/metadata_parsers.h @@ -29,4 +29,5 @@ bool get_spc_metadata(int fd, struct mp3entry* id3); bool get_speex_metadata(int fd, struct mp3entry* id3); bool get_vorbis_metadata(int fd, struct mp3entry* id3); bool get_wave_metadata(int fd, struct mp3entry* id3); +bool get_wavpack_metadata(int fd, struct mp3entry* id3); bool get_asf_metadata(int fd, struct mp3entry* id3); diff --git a/apps/metadata/wavpack.c b/apps/metadata/wavpack.c new file mode 100644 index 0000000000..5d33c0dffd --- /dev/null +++ b/apps/metadata/wavpack.c @@ -0,0 +1,122 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id:$ + * + * Copyright (C) 2007 David Bryant + * + * 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 +#include +#include +#include +#include + +#include "system.h" +#include "id3.h" +#include "metadata_common.h" +#include "logf.h" + +#define ID_UNIQUE 0x3f +#define ID_LARGE 0x80 +#define ID_SAMPLE_RATE 0x27 + +static const long wavpack_sample_rates [] = +{ + 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000, + 32000, 44100, 48000, 64000, 88200, 96000, 192000 +}; + +/* A simple parser to read basic information from a WavPack file. This + * now works with self-extrating WavPack files and also will scan the + * metadata for non-standard sampling rates. This no longer fails on + * WavPack files containing floating-point audio data because these are + * now converted to standard Rockbox format in the decoder. + */ + +bool get_wavpack_metadata(int fd, struct mp3entry* id3) +{ + /* Use the trackname part of the id3 structure as a temporary buffer */ + unsigned char* buf = (unsigned char *)id3->path; + uint32_t totalsamples, blocksamples; + int i; + + for (i = 0; i < 256; ++i) { + + /* at every 256 bytes into file, try to read a WavPack header */ + + if ((lseek(fd, i * 256, SEEK_SET) < 0) || (read(fd, buf, 32) < 32)) + return false; + + /* if valid WavPack 4 header version, break */ + + if (memcmp (buf, "wvpk", 4) == 0 && buf [9] == 4 && + (buf [8] >= 2 && buf [8] <= 0x10)) + break; + } + + if (i == 256) { + logf ("Not a WavPack file"); + return false; + } + + id3->vbr = true; /* All WavPack files are VBR */ + id3->filesize = filesize (fd); + totalsamples = get_long_le(&buf[12]); + blocksamples = get_long_le(&buf[20]); + + if (blocksamples && totalsamples != (uint32_t) -1) { + int srindx = ((buf [26] >> 7) & 1) + ((buf [27] << 1) & 14); + + if (srindx == 15) { + uint32_t meta_bytes = buf [4] + (buf [5] << 8) + (buf [6] << 16) - 24; + uint32_t meta_size; + + id3->frequency = 44100; + + while (meta_bytes >= 6) { + if (read(fd, buf, 2) < 2) + break; + + if (buf [0] & ID_LARGE) { + if (read(fd, buf + 2, 2) < 2) + break; + + meta_size = (buf [1] << 1) + (buf [2] << 9) + (buf [3] << 17); + meta_bytes -= meta_size + 4; + } + else { + meta_size = buf [1] << 1; + meta_bytes -= meta_size + 2; + + if ((buf [0] & ID_UNIQUE) == ID_SAMPLE_RATE) { + if (meta_size == 4 && read(fd, buf + 2, 4) == 4) + id3->frequency = buf [2] + (buf [3] << 8) + (buf [4] << 16); + + break; + } + } + + if (meta_size > 0 && lseek(fd, meta_size, SEEK_CUR) < 0) + break; + } + } + else + id3->frequency = wavpack_sample_rates[srindx]; + + id3->length = ((int64_t) totalsamples * 1000) / id3->frequency; + id3->bitrate = filesize (fd) / (id3->length / 8); + } + + return true; +} -- cgit v1.2.3