2007-08-12 06:36:06 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2009-11-18 16:59:34 +00:00
|
|
|
* $Id$
|
2007-08-12 06:36:06 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2007 David Bryant
|
|
|
|
*
|
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.
|
2007-08-12 06:36:06 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <inttypes.h>
|
2011-10-30 16:05:04 +00:00
|
|
|
#include "platform.h"
|
2007-08-12 06:36:06 +00:00
|
|
|
|
2008-10-15 06:38:51 +00:00
|
|
|
#include "metadata.h"
|
2007-08-12 06:36:06 +00:00
|
|
|
#include "metadata_common.h"
|
2008-05-03 07:03:59 +00:00
|
|
|
#include "metadata_parsers.h"
|
2007-08-12 06:36:06 +00:00
|
|
|
#include "logf.h"
|
|
|
|
|
|
|
|
#define ID_UNIQUE 0x3f
|
|
|
|
#define ID_LARGE 0x80
|
|
|
|
#define ID_SAMPLE_RATE 0x27
|
|
|
|
|
2008-03-30 19:28:31 +00:00
|
|
|
#define MONO_FLAG 4
|
|
|
|
#define HYBRID_FLAG 8
|
|
|
|
|
2007-08-12 06:36:06 +00:00
|
|
|
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
|
2010-12-05 19:25:32 +00:00
|
|
|
* now converted to standard Rockbox format in the decoder, and also
|
|
|
|
* handles the case where up to 15 non-audio blocks might occur at the
|
|
|
|
* beginning of the file.
|
2007-08-12 06:36:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
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;
|
2010-12-05 19:25:32 +00:00
|
|
|
uint32_t totalsamples = (uint32_t) -1;
|
2007-08-12 06:36:06 +00:00
|
|
|
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);
|
|
|
|
|
2010-12-05 19:25:32 +00:00
|
|
|
/* check up to 16 headers before we give up finding one with audio */
|
2007-08-12 06:36:06 +00:00
|
|
|
|
2010-12-05 19:25:32 +00:00
|
|
|
for (i = 0; i < 16; ++i) {
|
2010-12-05 22:01:28 +00:00
|
|
|
uint32_t meta_bytes = get_long_le(&buf [4]) - 24;
|
2010-12-05 19:25:32 +00:00
|
|
|
uint32_t trial_totalsamples = get_long_le(&buf[12]);
|
|
|
|
uint32_t blockindex = get_long_le(&buf[16]);
|
|
|
|
uint32_t blocksamples = get_long_le(&buf[20]);
|
|
|
|
uint32_t flags = get_long_le(&buf[24]);
|
2007-08-12 06:36:06 +00:00
|
|
|
|
2010-12-05 22:01:28 +00:00
|
|
|
if (totalsamples == (uint32_t) -1 && blockindex == 0)
|
2010-12-05 19:25:32 +00:00
|
|
|
totalsamples = trial_totalsamples;
|
2007-08-12 06:36:06 +00:00
|
|
|
|
2010-12-05 19:25:32 +00:00
|
|
|
if (blocksamples) {
|
|
|
|
int srindx = ((buf [26] >> 7) & 1) + ((buf [27] << 1) & 14);
|
|
|
|
|
|
|
|
if (srindx == 15) {
|
|
|
|
uint32_t meta_size;
|
2007-08-12 06:36:06 +00:00
|
|
|
|
2010-12-05 19:25:32 +00:00
|
|
|
id3->frequency = 44100;
|
|
|
|
|
|
|
|
while (meta_bytes >= 6) {
|
|
|
|
if (read(fd, buf, 2) < 2)
|
2007-08-12 06:36:06 +00:00
|
|
|
break;
|
|
|
|
|
2010-12-05 19:25:32 +00:00
|
|
|
if (buf [0] & ID_LARGE) {
|
|
|
|
if (read(fd, buf + 2, 2) < 2)
|
|
|
|
break;
|
2007-08-12 06:36:06 +00:00
|
|
|
|
2010-12-05 19:25:32 +00:00
|
|
|
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;
|
2007-08-12 06:36:06 +00:00
|
|
|
|
2010-12-05 19:25:32 +00:00
|
|
|
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;
|
|
|
|
}
|
2007-08-12 06:36:06 +00:00
|
|
|
}
|
|
|
|
|
2010-12-05 19:25:32 +00:00
|
|
|
if (meta_size > 0 && lseek(fd, meta_size, SEEK_CUR) < 0)
|
|
|
|
break;
|
|
|
|
}
|
2007-08-12 06:36:06 +00:00
|
|
|
}
|
2010-12-05 19:25:32 +00:00
|
|
|
else
|
|
|
|
id3->frequency = wavpack_sample_rates[srindx];
|
|
|
|
|
|
|
|
/* if the total number of samples is still unknown, make a guess on the high side (for now) */
|
|
|
|
|
|
|
|
if (totalsamples == (uint32_t) -1) {
|
2010-12-05 22:01:28 +00:00
|
|
|
totalsamples = id3->filesize * 3;
|
2007-08-12 06:36:06 +00:00
|
|
|
|
2010-12-05 19:25:32 +00:00
|
|
|
if (!(flags & HYBRID_FLAG))
|
|
|
|
totalsamples /= 2;
|
2008-03-30 19:28:31 +00:00
|
|
|
|
2010-12-05 19:25:32 +00:00
|
|
|
if (!(flags & MONO_FLAG))
|
|
|
|
totalsamples /= 2;
|
|
|
|
}
|
2008-03-30 19:28:31 +00:00
|
|
|
|
2010-12-05 19:25:32 +00:00
|
|
|
id3->length = ((int64_t) totalsamples * 1000) / id3->frequency;
|
2010-12-05 22:01:28 +00:00
|
|
|
id3->bitrate = id3->filesize / (id3->length / 8);
|
2008-03-30 19:28:31 +00:00
|
|
|
|
2010-12-05 19:25:32 +00:00
|
|
|
read_ape_tags(fd, id3);
|
|
|
|
return true;
|
2008-03-30 19:28:31 +00:00
|
|
|
}
|
2010-12-05 19:25:32 +00:00
|
|
|
else { /* block did not contain audio, so seek to the end and see if there's another */
|
|
|
|
if ((meta_bytes > 0 && lseek(fd, meta_bytes, SEEK_CUR) < 0) ||
|
2010-12-05 22:01:28 +00:00
|
|
|
read(fd, buf, 32) < 32 || memcmp (buf, "wvpk", 4) != 0)
|
2010-12-05 19:25:32 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-08-12 06:36:06 +00:00
|
|
|
}
|
|
|
|
|
2008-03-30 19:28:31 +00:00
|
|
|
return false;
|
2007-08-12 06:36:06 +00:00
|
|
|
}
|