2007-06-16 18:19:51 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Dave Chapman
|
2010-02-28 08:48:07 +00:00
|
|
|
* Copyright (C) 2010 Yoshihisa Uchida
|
2007-06-16 18:19:51 +00:00
|
|
|
*
|
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-06-16 18:19:51 +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 <inttypes.h>
|
|
|
|
|
|
|
|
#include "system.h"
|
2008-10-15 06:38:51 +00:00
|
|
|
#include "metadata.h"
|
2007-06-16 18:19:51 +00:00
|
|
|
#include "metadata_common.h"
|
2008-05-03 07:03:59 +00:00
|
|
|
#include "metadata_parsers.h"
|
2010-05-08 12:15:15 +00:00
|
|
|
#include "rbunicode.h"
|
2010-02-20 02:04:56 +00:00
|
|
|
#include "logf.h"
|
2007-06-16 18:19:51 +00:00
|
|
|
|
2011-03-15 20:27:26 +00:00
|
|
|
#ifdef DEBUGF
|
|
|
|
#undef DEBUGF
|
|
|
|
#define DEBUGF(...)
|
|
|
|
#endif
|
|
|
|
|
2010-03-10 10:55:01 +00:00
|
|
|
/* Wave(RIFF)/Wave64 format */
|
|
|
|
|
|
|
|
|
2010-02-16 03:34:39 +00:00
|
|
|
# define AV_WL32(p, d) do { \
|
|
|
|
((uint8_t*)(p))[0] = (d); \
|
|
|
|
((uint8_t*)(p))[1] = (d)>>8; \
|
|
|
|
((uint8_t*)(p))[2] = (d)>>16; \
|
|
|
|
((uint8_t*)(p))[3] = (d)>>24; \
|
|
|
|
} while(0)
|
|
|
|
# define AV_WL16(p, d) do { \
|
|
|
|
((uint8_t*)(p))[0] = (d); \
|
|
|
|
((uint8_t*)(p))[1] = (d)>>8; \
|
|
|
|
} while(0)
|
|
|
|
|
2010-03-10 10:55:01 +00:00
|
|
|
enum {
|
|
|
|
RIFF_CHUNK = 0,
|
|
|
|
WAVE_CHUNK,
|
|
|
|
FMT_CHUNK,
|
|
|
|
FACT_CHUNK,
|
|
|
|
DATA_CHUNK,
|
2010-05-08 12:15:15 +00:00
|
|
|
LIST_CHUNK,
|
2010-03-10 10:55:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Wave chunk names */
|
2010-03-14 05:41:02 +00:00
|
|
|
#define WAVE_CHUNKNAME_LENGTH 4
|
|
|
|
#define WAVE_CHUNKSIZE_LENGTH 4
|
|
|
|
|
2011-12-17 18:31:55 +00:00
|
|
|
static const unsigned char * const wave_chunklist
|
|
|
|
= "RIFF"
|
2010-03-14 05:41:02 +00:00
|
|
|
"WAVE"
|
|
|
|
"fmt "
|
|
|
|
"fact"
|
2010-05-08 12:15:15 +00:00
|
|
|
"data"
|
|
|
|
"LIST";
|
2010-02-28 08:48:07 +00:00
|
|
|
|
|
|
|
/* Wave64 GUIDs */
|
2010-03-14 05:41:02 +00:00
|
|
|
#define WAVE64_CHUNKNAME_LENGTH 16
|
|
|
|
#define WAVE64_CHUNKSIZE_LENGTH 8
|
|
|
|
|
2011-12-17 18:31:55 +00:00
|
|
|
static const unsigned char * const wave64_chunklist
|
2010-03-14 05:41:02 +00:00
|
|
|
= "riff\x2e\x91\xcf\x11\xa5\xd6\x28\xdb\x04\xc1\x00\x00"
|
|
|
|
"wave\xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
|
|
|
|
"fmt \xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
|
|
|
|
"fact\xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
|
2010-05-08 12:15:15 +00:00
|
|
|
"data\xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
|
|
|
|
"\xbc\x94\x5f\x92\x5a\x52\xd2\x11\x86\xdc\x00\xc0\x4f\x8e\xdb\x8a";
|
2010-03-14 05:41:02 +00:00
|
|
|
|
2010-05-09 07:10:07 +00:00
|
|
|
/* list/info chunk */
|
|
|
|
|
|
|
|
struct info_chunk {
|
|
|
|
const unsigned char* tag;
|
|
|
|
size_t offset;
|
2010-05-08 12:15:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* info chunk names are common wave/wave64 */
|
2010-05-09 07:10:07 +00:00
|
|
|
static const struct info_chunk info_chunks[] = {
|
|
|
|
{ "INAM", offsetof(struct mp3entry, title), }, /* title */
|
|
|
|
{ "IART", offsetof(struct mp3entry, artist), }, /* artist */
|
|
|
|
{ "ISBJ", offsetof(struct mp3entry, albumartist), }, /* albumartist */
|
|
|
|
{ "IPRD", offsetof(struct mp3entry, album), }, /* album */
|
|
|
|
{ "IWRI", offsetof(struct mp3entry, composer), }, /* composer */
|
|
|
|
{ "ICMT", offsetof(struct mp3entry, comment), }, /* comment */
|
|
|
|
{ "ISRF", offsetof(struct mp3entry, grouping), }, /* grouping */
|
|
|
|
{ "IGNR", offsetof(struct mp3entry, genre_string), }, /* genre */
|
|
|
|
{ "ICRD", offsetof(struct mp3entry, year_string), }, /* date */
|
|
|
|
{ "IPRT", offsetof(struct mp3entry, track_string), }, /* track/trackcount */
|
|
|
|
{ "IFRM", offsetof(struct mp3entry, disc_string), }, /* disc/disccount */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define INFO_CHUNK_COUNT ((int)ARRAYLEN(info_chunks))
|
2010-02-28 08:48:07 +00:00
|
|
|
|
2010-03-10 10:55:01 +00:00
|
|
|
/* support formats */
|
2010-02-20 02:04:56 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
WAVE_FORMAT_PCM = 0x0001, /* Microsoft PCM Format */
|
|
|
|
WAVE_FORMAT_ADPCM = 0x0002, /* Microsoft ADPCM Format */
|
|
|
|
WAVE_FORMAT_IEEE_FLOAT = 0x0003, /* IEEE Float */
|
|
|
|
WAVE_FORMAT_ALAW = 0x0006, /* Microsoft ALAW */
|
|
|
|
WAVE_FORMAT_MULAW = 0x0007, /* Microsoft MULAW */
|
|
|
|
WAVE_FORMAT_DVI_ADPCM = 0x0011, /* Intel's DVI ADPCM */
|
|
|
|
WAVE_FORMAT_DIALOGIC_OKI_ADPCM = 0x0017, /* Dialogic OKI ADPCM */
|
|
|
|
WAVE_FORMAT_YAMAHA_ADPCM = 0x0020, /* Yamaha ADPCM */
|
|
|
|
WAVE_FORMAT_XBOX_ADPCM = 0x0069, /* XBOX ADPCM */
|
|
|
|
IBM_FORMAT_MULAW = 0x0101, /* same as WAVE_FORMAT_MULAW */
|
|
|
|
IBM_FORMAT_ALAW = 0x0102, /* same as WAVE_FORMAT_ALAW */
|
|
|
|
WAVE_FORMAT_ATRAC3 = 0x0270, /* Atrac3 stream */
|
|
|
|
WAVE_FORMAT_SWF_ADPCM = 0x5346, /* Adobe SWF ADPCM */
|
2010-03-10 10:55:01 +00:00
|
|
|
WAVE_FORMAT_EXTENSIBLE = 0xFFFE,
|
2010-02-20 02:04:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct wave_fmt {
|
|
|
|
unsigned int formattag;
|
2010-03-10 10:55:01 +00:00
|
|
|
unsigned int channels;
|
2010-02-20 02:04:56 +00:00
|
|
|
unsigned int blockalign;
|
2010-03-10 10:55:01 +00:00
|
|
|
unsigned int bitspersample;
|
2010-02-20 02:04:56 +00:00
|
|
|
unsigned int samplesperblock;
|
2010-03-10 10:55:01 +00:00
|
|
|
uint32_t totalsamples;
|
2010-02-28 08:48:07 +00:00
|
|
|
uint64_t numbytes;
|
2010-02-20 02:04:56 +00:00
|
|
|
};
|
|
|
|
|
2010-05-09 07:10:07 +00:00
|
|
|
static unsigned char *convert_utf8(const unsigned char *src, unsigned char *dst,
|
|
|
|
int size, bool is_64)
|
2010-05-08 12:15:15 +00:00
|
|
|
{
|
|
|
|
if (is_64)
|
|
|
|
{
|
|
|
|
/* Note: wave64: metadata codepage is UTF-16 only */
|
|
|
|
return utf16LEdecode(src, dst, size);
|
|
|
|
}
|
|
|
|
return iso_decode(src, dst, -1, size);
|
|
|
|
}
|
|
|
|
|
2010-03-10 10:55:01 +00:00
|
|
|
static void set_totalsamples(struct wave_fmt *fmt, struct mp3entry* id3)
|
2010-02-20 02:04:56 +00:00
|
|
|
{
|
|
|
|
switch (fmt->formattag)
|
|
|
|
{
|
|
|
|
case WAVE_FORMAT_PCM:
|
|
|
|
case WAVE_FORMAT_IEEE_FLOAT:
|
|
|
|
case WAVE_FORMAT_ALAW:
|
|
|
|
case WAVE_FORMAT_MULAW:
|
|
|
|
case IBM_FORMAT_ALAW:
|
|
|
|
case IBM_FORMAT_MULAW:
|
2010-03-14 05:41:02 +00:00
|
|
|
fmt->blockalign = fmt->bitspersample * fmt->channels >> 3;
|
|
|
|
fmt->samplesperblock = 1;
|
2010-02-20 02:04:56 +00:00
|
|
|
break;
|
|
|
|
case WAVE_FORMAT_YAMAHA_ADPCM:
|
2010-03-14 05:41:02 +00:00
|
|
|
if (id3->channels != 0)
|
2010-02-20 02:04:56 +00:00
|
|
|
{
|
2010-03-14 05:41:02 +00:00
|
|
|
fmt->samplesperblock =
|
|
|
|
(fmt->blockalign == ((id3->frequency / 60) + 4) * fmt->channels)?
|
|
|
|
id3->frequency / 30 : (fmt->blockalign << 1) / fmt->channels;
|
2010-02-20 02:04:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WAVE_FORMAT_DIALOGIC_OKI_ADPCM:
|
2010-03-14 05:41:02 +00:00
|
|
|
fmt->blockalign = 1;
|
|
|
|
fmt->samplesperblock = 2;
|
2010-02-20 02:04:56 +00:00
|
|
|
break;
|
|
|
|
case WAVE_FORMAT_SWF_ADPCM:
|
2010-03-14 05:41:02 +00:00
|
|
|
if (fmt->bitspersample != 0 && id3->channels != 0)
|
2010-03-10 10:55:01 +00:00
|
|
|
{
|
2010-03-14 05:41:02 +00:00
|
|
|
fmt->samplesperblock
|
|
|
|
= (((fmt->blockalign << 3) - 2) / fmt->channels - 22)
|
|
|
|
/ fmt->bitspersample + 1;
|
2010-03-10 10:55:01 +00:00
|
|
|
}
|
2010-02-20 02:04:56 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-03-14 05:41:02 +00:00
|
|
|
|
|
|
|
if (fmt->blockalign != 0)
|
|
|
|
fmt->totalsamples = (fmt->numbytes / fmt->blockalign) * fmt->samplesperblock;
|
2010-02-20 02:04:56 +00:00
|
|
|
}
|
|
|
|
|
2010-02-28 08:48:07 +00:00
|
|
|
static void parse_riff_format(unsigned char* buf, int fmtsize, struct wave_fmt *fmt,
|
|
|
|
struct mp3entry* id3)
|
|
|
|
{
|
|
|
|
/* wFormatTag */
|
|
|
|
fmt->formattag = buf[0] | (buf[1] << 8);
|
|
|
|
/* wChannels */
|
|
|
|
fmt->channels = buf[2] | (buf[3] << 8);
|
|
|
|
/* dwSamplesPerSec */
|
|
|
|
id3->frequency = get_long_le(&buf[4]);
|
|
|
|
/* dwAvgBytesPerSec */
|
|
|
|
id3->bitrate = (get_long_le(&buf[8]) * 8) / 1000;
|
|
|
|
/* wBlockAlign */
|
|
|
|
fmt->blockalign = buf[12] | (buf[13] << 8);
|
|
|
|
/* wBitsPerSample */
|
|
|
|
fmt->bitspersample = buf[14] | (buf[15] << 8);
|
2010-03-10 10:55:01 +00:00
|
|
|
|
|
|
|
if (fmt->formattag != WAVE_FORMAT_EXTENSIBLE)
|
2010-02-28 08:48:07 +00:00
|
|
|
{
|
2010-03-10 10:55:01 +00:00
|
|
|
if (fmtsize > 19)
|
|
|
|
{
|
|
|
|
/* wSamplesPerBlock */
|
|
|
|
fmt->samplesperblock = buf[18] | (buf[19] << 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fmtsize > 25)
|
|
|
|
{
|
|
|
|
/* wValidBitsPerSample */
|
|
|
|
fmt->bitspersample = buf[18] | (buf[19] << 8);
|
|
|
|
/* SubFormat */
|
|
|
|
fmt->formattag = buf[24] | (buf[25] << 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for ATRAC3 stream */
|
|
|
|
if (fmt->formattag == WAVE_FORMAT_ATRAC3)
|
|
|
|
{
|
|
|
|
int jsflag = 0;
|
|
|
|
if(id3->bitrate == 66 || id3->bitrate == 94)
|
|
|
|
jsflag = 1;
|
|
|
|
|
|
|
|
id3->extradata_size = 14;
|
|
|
|
id3->channels = 2;
|
|
|
|
id3->codectype = AFMT_OMA_ATRAC3;
|
2010-03-12 18:47:13 +00:00
|
|
|
id3->bytesperframe = fmt->blockalign;
|
|
|
|
|
2010-03-10 10:55:01 +00:00
|
|
|
/* Store the extradata for the codec */
|
|
|
|
AV_WL16(&id3->id3v2buf[0], 1); // always 1
|
|
|
|
AV_WL32(&id3->id3v2buf[2], id3->frequency);// samples rate
|
|
|
|
AV_WL16(&id3->id3v2buf[6], jsflag); // coding mode
|
|
|
|
AV_WL16(&id3->id3v2buf[8], jsflag); // coding mode
|
|
|
|
AV_WL16(&id3->id3v2buf[10], 1); // always 1
|
|
|
|
AV_WL16(&id3->id3v2buf[12], 0); // always 0
|
2010-02-28 08:48:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-09 07:10:07 +00:00
|
|
|
static void parse_list_chunk(int fd, struct mp3entry* id3, int chunksize, bool is_64)
|
2010-05-08 12:15:15 +00:00
|
|
|
{
|
|
|
|
unsigned char tmpbuf[ID3V2_BUF_SIZE];
|
|
|
|
unsigned char *bp = tmpbuf;
|
|
|
|
unsigned char *endp;
|
|
|
|
unsigned char *data_pos;
|
2010-05-09 07:10:07 +00:00
|
|
|
unsigned char *tag_pos = id3->id3v2buf;
|
2010-05-08 12:15:15 +00:00
|
|
|
int datasize;
|
|
|
|
int infosize;
|
2010-05-09 07:10:07 +00:00
|
|
|
int remain;
|
|
|
|
int i;
|
2010-05-08 12:15:15 +00:00
|
|
|
|
|
|
|
if (is_64)
|
|
|
|
lseek(fd, 4, SEEK_CUR);
|
|
|
|
else if (read(fd, bp, 4) < 4 || memcmp(bp, "INFO", 4))
|
2010-05-09 07:10:07 +00:00
|
|
|
return;
|
2010-05-10 11:13:17 +00:00
|
|
|
|
|
|
|
/* decrease skip bytes */
|
|
|
|
chunksize -= 4;
|
2010-05-08 12:15:15 +00:00
|
|
|
|
|
|
|
infosize = read(fd, bp, (ID3V2_BUF_SIZE > chunksize)? chunksize : ID3V2_BUF_SIZE);
|
|
|
|
if (infosize <= 8)
|
2010-05-09 07:10:07 +00:00
|
|
|
return;
|
2010-05-08 12:15:15 +00:00
|
|
|
|
|
|
|
endp = bp + infosize;
|
|
|
|
while (bp < endp)
|
|
|
|
{
|
|
|
|
datasize = get_long_le(bp + 4);
|
|
|
|
data_pos = bp + 8;
|
2010-05-09 07:10:07 +00:00
|
|
|
remain = ID3V2_BUF_SIZE - (tag_pos - (unsigned char*)id3->id3v2buf);
|
|
|
|
if (remain < 1)
|
2010-05-08 12:15:15 +00:00
|
|
|
break;
|
|
|
|
|
2010-05-09 07:10:07 +00:00
|
|
|
for (i = 0; i < INFO_CHUNK_COUNT; i++)
|
2010-05-08 12:15:15 +00:00
|
|
|
{
|
2010-05-09 07:10:07 +00:00
|
|
|
if (memcmp(bp, info_chunks[i].tag, 4) == 0)
|
|
|
|
{
|
|
|
|
*((char **)(((char*)id3) + info_chunks[i].offset)) = tag_pos;
|
|
|
|
tag_pos = convert_utf8(data_pos, tag_pos,
|
|
|
|
(datasize + 1 >= remain )? remain - 1 : datasize,
|
|
|
|
is_64);
|
|
|
|
*tag_pos++ = 0;
|
|
|
|
break;
|
|
|
|
}
|
2010-05-08 13:21:27 +00:00
|
|
|
}
|
2010-05-08 12:15:15 +00:00
|
|
|
bp = data_pos + datasize + (datasize & 1);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-03-14 05:41:02 +00:00
|
|
|
static bool read_header(int fd, struct mp3entry* id3, const unsigned char *chunknames,
|
|
|
|
bool is_64)
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
2010-03-10 10:55:01 +00:00
|
|
|
/* Use the temporary buffer */
|
2007-06-16 18:19:51 +00:00
|
|
|
unsigned char* buf = (unsigned char *)id3->path;
|
2010-03-10 10:55:01 +00:00
|
|
|
|
2010-02-20 02:04:56 +00:00
|
|
|
struct wave_fmt fmt;
|
2010-03-10 10:55:01 +00:00
|
|
|
|
2010-05-09 19:58:20 +00:00
|
|
|
const unsigned int namelen = (is_64)? WAVE64_CHUNKNAME_LENGTH : WAVE_CHUNKNAME_LENGTH;
|
|
|
|
const unsigned int sizelen = (is_64)? WAVE64_CHUNKSIZE_LENGTH : WAVE_CHUNKSIZE_LENGTH;
|
|
|
|
const unsigned int len = namelen + sizelen;
|
2010-03-10 10:55:01 +00:00
|
|
|
uint64_t chunksize;
|
|
|
|
uint64_t offset = len + namelen;
|
|
|
|
int read_data;
|
2007-06-16 18:19:51 +00:00
|
|
|
|
2010-02-20 02:04:56 +00:00
|
|
|
memset(&fmt, 0, sizeof(struct wave_fmt));
|
2010-05-08 12:15:15 +00:00
|
|
|
|
|
|
|
id3->vbr = false; /* All Wave/Wave64 files are CBR */
|
|
|
|
id3->filesize = filesize(fd);
|
2010-02-20 02:04:56 +00:00
|
|
|
|
2007-06-16 18:19:51 +00:00
|
|
|
/* get RIFF chunk header */
|
2010-03-10 10:55:01 +00:00
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
read(fd, buf, offset);
|
2007-06-16 18:19:51 +00:00
|
|
|
|
2010-03-14 05:41:02 +00:00
|
|
|
if ((memcmp(buf, chunknames + RIFF_CHUNK * namelen, namelen) != 0) ||
|
|
|
|
(memcmp(buf + len, chunknames + WAVE_CHUNK * namelen, namelen) != 0))
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
2010-02-20 02:04:56 +00:00
|
|
|
DEBUGF("metadata error: missing riff header.\n");
|
2007-06-16 18:19:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* iterate over WAVE chunks until 'data' chunk */
|
2010-05-08 12:15:15 +00:00
|
|
|
while (read(fd, buf, len) > 0)
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
2010-03-10 10:55:01 +00:00
|
|
|
offset += len;
|
2007-06-16 18:19:51 +00:00
|
|
|
|
2010-03-10 10:55:01 +00:00
|
|
|
/* get chunk size (when the header is wave64, chunksize includes GUID and data length) */
|
|
|
|
chunksize = (is_64) ? get_uint64_le(buf + namelen) - len :
|
|
|
|
get_long_le(buf + namelen);
|
2010-02-16 03:34:39 +00:00
|
|
|
|
2010-03-10 10:55:01 +00:00
|
|
|
read_data = 0;
|
2010-03-14 05:41:02 +00:00
|
|
|
if (memcmp(buf, chunknames + FMT_CHUNK * namelen, namelen) == 0)
|
2010-03-10 10:55:01 +00:00
|
|
|
{
|
|
|
|
DEBUGF("find 'fmt ' chunk\n");
|
2010-02-20 02:04:56 +00:00
|
|
|
|
2010-03-10 10:55:01 +00:00
|
|
|
if (chunksize < 16)
|
2010-02-20 02:04:56 +00:00
|
|
|
{
|
2010-03-10 10:55:01 +00:00
|
|
|
DEBUGF("metadata error: 'fmt ' chunk is too small: %d\n", (int)chunksize);
|
|
|
|
return false;
|
2010-02-16 03:34:39 +00:00
|
|
|
}
|
2010-03-10 10:55:01 +00:00
|
|
|
|
|
|
|
/* get and parse format */
|
|
|
|
read_data = (chunksize > 25)? 26 : chunksize;
|
|
|
|
|
|
|
|
read(fd, buf, read_data);
|
|
|
|
parse_riff_format(buf, read_data, &fmt, id3);
|
2007-06-16 18:19:51 +00:00
|
|
|
}
|
2010-03-14 05:41:02 +00:00
|
|
|
else if (memcmp(buf, chunknames + FACT_CHUNK * namelen, namelen) == 0)
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
2010-03-10 10:55:01 +00:00
|
|
|
DEBUGF("find 'fact' chunk\n");
|
|
|
|
|
2007-06-16 18:19:51 +00:00
|
|
|
/* dwSampleLength */
|
2010-03-10 10:55:01 +00:00
|
|
|
if (chunksize >= sizelen)
|
2007-06-16 18:19:51 +00:00
|
|
|
{
|
2010-03-10 10:55:01 +00:00
|
|
|
/* get totalsamples */
|
|
|
|
read_data = sizelen;
|
|
|
|
read(fd, buf, read_data);
|
|
|
|
fmt.totalsamples = (is_64)? get_uint64_le(buf) : get_long_le(buf);
|
2007-06-16 18:19:51 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-08 12:15:15 +00:00
|
|
|
else if (memcmp(buf, chunknames + DATA_CHUNK * namelen, namelen) == 0)
|
|
|
|
{
|
|
|
|
DEBUGF("find 'data' chunk\n");
|
|
|
|
fmt.numbytes = chunksize;
|
|
|
|
if (fmt.formattag == WAVE_FORMAT_ATRAC3)
|
|
|
|
id3->first_frame_offset = offset;
|
|
|
|
}
|
|
|
|
else if (memcmp(buf, chunknames + LIST_CHUNK * namelen, namelen) == 0)
|
|
|
|
{
|
|
|
|
DEBUGF("find 'LIST' chunk\n");
|
|
|
|
parse_list_chunk(fd, id3, chunksize, is_64);
|
|
|
|
lseek(fd, offset, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* padded to next chunk */
|
|
|
|
chunksize += ((is_64)? ((1 + ~chunksize) & 0x07) : (chunksize & 1));
|
|
|
|
|
|
|
|
offset += chunksize;
|
|
|
|
if (offset >= id3->filesize)
|
|
|
|
break;
|
2007-06-16 18:19:51 +00:00
|
|
|
|
2010-03-10 10:55:01 +00:00
|
|
|
lseek(fd, chunksize - read_data, SEEK_CUR);
|
2007-06-16 18:19:51 +00:00
|
|
|
}
|
|
|
|
|
2010-05-08 12:15:15 +00:00
|
|
|
if (fmt.numbytes == 0)
|
|
|
|
{
|
|
|
|
DEBUGF("metadata error: read error or missing 'data' chunk.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-10 10:55:01 +00:00
|
|
|
if (fmt.totalsamples == 0)
|
|
|
|
set_totalsamples(&fmt, id3);
|
2007-06-16 18:19:51 +00:00
|
|
|
|
2010-03-14 05:41:02 +00:00
|
|
|
if (id3->frequency == 0 || id3->bitrate == 0)
|
|
|
|
{
|
|
|
|
DEBUGF("metadata error: frequency or bitrate is 0\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-06-16 18:19:51 +00:00
|
|
|
/* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
|
2010-03-14 05:41:02 +00:00
|
|
|
id3->length = (fmt.formattag != WAVE_FORMAT_ATRAC3)?
|
|
|
|
(uint64_t)fmt.totalsamples * 1000 / id3->frequency :
|
|
|
|
((id3->filesize - id3->first_frame_offset) * 8) / id3->bitrate;
|
2010-02-28 08:48:07 +00:00
|
|
|
|
2010-03-10 10:55:01 +00:00
|
|
|
/* output header/id3 info (for debug) */
|
|
|
|
DEBUGF("%s header info ----\n", (is_64)? "wave64" : "wave");
|
|
|
|
DEBUGF(" format: %04x\n", (int)fmt.formattag);
|
|
|
|
DEBUGF(" channels: %u\n", fmt.channels);
|
|
|
|
DEBUGF(" blockalign: %u\n", fmt.blockalign);
|
|
|
|
DEBUGF(" bitspersample: %u\n", fmt.bitspersample);
|
|
|
|
DEBUGF(" samplesperblock: %u\n", fmt.samplesperblock);
|
|
|
|
DEBUGF(" totalsamples: %u\n", (unsigned int)fmt.totalsamples);
|
2010-05-09 19:58:20 +00:00
|
|
|
DEBUGF(" numbytes: %u\n", (unsigned int)fmt.numbytes);
|
2010-03-10 10:55:01 +00:00
|
|
|
DEBUGF("id3 info ----\n");
|
2010-05-09 19:58:20 +00:00
|
|
|
DEBUGF(" frequency: %u\n", (unsigned int)id3->frequency);
|
2010-03-10 10:55:01 +00:00
|
|
|
DEBUGF(" bitrate: %d\n", id3->bitrate);
|
|
|
|
DEBUGF(" length: %u\n", (unsigned int)id3->length);
|
2010-02-28 08:48:07 +00:00
|
|
|
|
2010-03-10 10:55:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-02-28 08:48:07 +00:00
|
|
|
|
2010-03-10 10:55:01 +00:00
|
|
|
bool get_wave_metadata(int fd, struct mp3entry* id3)
|
|
|
|
{
|
2010-03-14 05:41:02 +00:00
|
|
|
return read_header(fd, id3, wave_chunklist, false);
|
2010-03-10 10:55:01 +00:00
|
|
|
}
|
2010-02-28 08:48:07 +00:00
|
|
|
|
2010-03-10 10:55:01 +00:00
|
|
|
bool get_wave64_metadata(int fd, struct mp3entry* id3)
|
|
|
|
{
|
2010-03-14 05:41:02 +00:00
|
|
|
return read_header(fd, id3, wave64_chunklist, true);
|
2010-02-28 08:48:07 +00:00
|
|
|
}
|