2010-09-05 00:06:39 +02:00
|
|
|
#include "meta.h"
|
2010-09-10 23:49:56 +02:00
|
|
|
#include "../coding/coding.h"
|
2010-09-05 00:06:39 +02:00
|
|
|
#include "../util.h"
|
|
|
|
|
|
|
|
/* XVAG (from Ratchet & Clank Future: Quest for Booty) */
|
2010-09-09 00:33:06 +02:00
|
|
|
/* v0.1 : bxaimc : Initial release */
|
|
|
|
/* v0.2 : Fastelbja : add support for loops points */
|
|
|
|
/* + little endian header values */
|
|
|
|
|
2010-09-05 00:06:39 +02:00
|
|
|
VGMSTREAM * init_vgmstream_ps3_xvag(STREAMFILE *streamFile) {
|
|
|
|
VGMSTREAM * vgmstream = NULL;
|
|
|
|
char filename[260];
|
2010-09-09 00:33:06 +02:00
|
|
|
uint8_t testBuffer[0x10];
|
|
|
|
|
2010-09-05 00:06:39 +02:00
|
|
|
off_t start_offset;
|
2010-09-09 00:33:06 +02:00
|
|
|
off_t fileLength;
|
|
|
|
|
|
|
|
int loop_flag = 0;
|
|
|
|
int channel_count;
|
|
|
|
off_t readOffset = 0;
|
|
|
|
int little_endian = 0;
|
2010-09-10 23:49:56 +02:00
|
|
|
long sample_rate = 0;
|
|
|
|
long num_samples = 0;
|
|
|
|
|
2010-09-11 00:06:50 +02:00
|
|
|
#ifdef VGM_USE_MPEG
|
2010-09-10 23:49:56 +02:00
|
|
|
mpeg_codec_data *mpeg_data = NULL;
|
|
|
|
coding_t mpeg_coding_type = coding_MPEG1_L3;
|
2010-09-11 00:06:50 +02:00
|
|
|
#endif
|
2010-09-05 00:06:39 +02:00
|
|
|
|
2010-09-09 00:33:06 +02:00
|
|
|
int loopStartPointsCount=0;
|
|
|
|
int loopEndPointsCount=0;
|
2010-09-10 23:49:56 +02:00
|
|
|
uint16_t mp3ID;
|
2010-09-09 00:33:06 +02:00
|
|
|
off_t loopStartPoints[0x10];
|
|
|
|
off_t loopEndPoints[0x10];
|
|
|
|
|
|
|
|
off_t loopStart = 0;
|
|
|
|
off_t loopEnd = 0;
|
2010-09-05 00:06:39 +02:00
|
|
|
|
|
|
|
/* check extension, case insensitive */
|
|
|
|
streamFile->get_name(streamFile,filename,sizeof(filename));
|
|
|
|
if (strcasecmp("xvag",filename_extension(filename))) goto fail;
|
|
|
|
|
|
|
|
/* check header */
|
|
|
|
if (read_32bitBE(0x00,streamFile) != 0x58564147) /* "XVAG" */
|
|
|
|
goto fail;
|
|
|
|
|
2010-09-09 00:33:06 +02:00
|
|
|
fileLength = get_streamfile_size(streamFile);
|
|
|
|
|
|
|
|
if (read_8bit(0x07,streamFile)==0)
|
|
|
|
little_endian=1;
|
|
|
|
|
|
|
|
if(little_endian)
|
|
|
|
{
|
|
|
|
channel_count = read_32bitLE(0x28,streamFile);
|
|
|
|
start_offset = read_32bitLE(0x4,streamFile);
|
2010-09-10 23:49:56 +02:00
|
|
|
sample_rate = read_32bitLE(0x3c,streamFile);
|
|
|
|
num_samples = read_32bitLE(0x30,streamFile);
|
2010-09-09 00:33:06 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
channel_count = read_32bitBE(0x28,streamFile);
|
|
|
|
start_offset = read_32bitBE(0x4,streamFile);
|
2010-09-10 23:49:56 +02:00
|
|
|
sample_rate = read_32bitBE(0x3c,streamFile);
|
|
|
|
num_samples = read_32bitBE(0x30,streamFile);
|
2010-09-09 00:33:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
readOffset=start_offset;
|
|
|
|
|
|
|
|
// MP3s ?
|
2011-11-21 16:46:29 +01:00
|
|
|
mp3ID=(uint16_t)read_16bitBE(start_offset,streamFile);
|
2010-09-10 23:49:56 +02:00
|
|
|
if(mp3ID==0xFFFB) {
|
|
|
|
#ifdef VGM_USE_MPEG
|
2012-08-24 18:43:33 +02:00
|
|
|
int rate;
|
|
|
|
int channels;
|
2010-09-10 23:49:56 +02:00
|
|
|
|
2011-01-19 14:54:59 +01:00
|
|
|
mpeg_data = init_mpeg_codec_data(streamFile, start_offset, -1, -1, &mpeg_coding_type, &rate, &channels); // -1 to not check sample rate or channels
|
2010-09-10 23:49:56 +02:00
|
|
|
if (!mpeg_data) goto fail;
|
|
|
|
|
|
|
|
channel_count = channels;
|
|
|
|
sample_rate = rate;
|
|
|
|
|
|
|
|
#else
|
|
|
|
// reject if no MPEG support
|
|
|
|
goto fail;
|
|
|
|
#endif
|
|
|
|
} else {
|
2010-09-09 00:33:06 +02:00
|
|
|
// get the loops the same way we get on .MIB
|
|
|
|
do {
|
|
|
|
readOffset+=(off_t)read_streamfile(testBuffer,readOffset,0x10,streamFile);
|
|
|
|
|
|
|
|
// Loop Start ...
|
|
|
|
if(testBuffer[0x01]==0x06)
|
|
|
|
{
|
|
|
|
if(loopStartPointsCount<0x10)
|
|
|
|
{
|
|
|
|
loopStartPoints[loopStartPointsCount] = readOffset-0x10;
|
|
|
|
loopStartPointsCount++;
|
|
|
|
}
|
|
|
|
}
|
2010-09-05 00:06:39 +02:00
|
|
|
|
2010-09-09 00:33:06 +02:00
|
|
|
// Loop End ...
|
|
|
|
if(((testBuffer[0x01]==0x03) && (testBuffer[0x03]!=0x77)) ||
|
|
|
|
(testBuffer[0x01]==0x01))
|
|
|
|
{
|
|
|
|
if(loopEndPointsCount<0x10)
|
|
|
|
{
|
|
|
|
loopEndPoints[loopEndPointsCount] = readOffset; //-0x10;
|
|
|
|
loopEndPointsCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (readOffset<((int32_t)fileLength));
|
|
|
|
|
|
|
|
// Calc Loop Points & Interleave ...
|
|
|
|
if(loopStartPointsCount>=channel_count)
|
|
|
|
{
|
|
|
|
// can't get more then 0x10 loop point !
|
|
|
|
if((loopStartPointsCount<=0x0F) && (loopStartPointsCount>=2))
|
|
|
|
{
|
|
|
|
// Always took the first 2 loop points
|
2010-09-11 21:12:07 +02:00
|
|
|
loopStart=loopStartPoints[1]-start_offset;
|
2010-09-09 00:33:06 +02:00
|
|
|
loop_flag=1;
|
|
|
|
} else
|
|
|
|
loopStart=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(loopEndPointsCount>=channel_count)
|
|
|
|
{
|
|
|
|
// can't get more then 0x10 loop point !
|
|
|
|
if((loopEndPointsCount<=0x0F) && (loopEndPointsCount>=2)) {
|
|
|
|
loop_flag=1;
|
2010-09-11 21:12:07 +02:00
|
|
|
loopEnd=loopEndPoints[loopEndPointsCount-1]-start_offset;
|
2010-09-09 00:33:06 +02:00
|
|
|
} else {
|
|
|
|
loopEnd=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// as i can get on the header if a song is looped or not
|
|
|
|
// if try to take the loop marker on the file
|
|
|
|
// if the last byte of the file is = 00 is assume that the song is not looped
|
|
|
|
// i know that i can cover 95% of the file, but can't work on some of them
|
|
|
|
if(read_8bit((fileLength-1),streamFile)==0)
|
|
|
|
loop_flag=0;
|
|
|
|
}
|
2010-09-05 00:06:39 +02:00
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
|
|
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
2010-09-10 23:49:56 +02:00
|
|
|
vgmstream->sample_rate = sample_rate;
|
|
|
|
vgmstream->num_samples = num_samples;
|
2010-09-09 00:33:06 +02:00
|
|
|
vgmstream->channels = channel_count;
|
2010-09-05 00:06:39 +02:00
|
|
|
vgmstream->meta_type = meta_PS3_XVAG;
|
2010-09-09 00:33:06 +02:00
|
|
|
|
2010-09-10 23:49:56 +02:00
|
|
|
if(mp3ID==0xFFFB) {
|
|
|
|
#ifdef VGM_USE_MPEG
|
|
|
|
/* NOTE: num_samples seems to be quite wrong for MPEG */
|
|
|
|
vgmstream->codec_data = mpeg_data;
|
2010-09-09 00:33:06 +02:00
|
|
|
vgmstream->layout_type = layout_mpeg;
|
2010-09-10 23:49:56 +02:00
|
|
|
vgmstream->coding_type = mpeg_coding_type;
|
|
|
|
#else
|
|
|
|
// reject if no MPEG support
|
|
|
|
goto fail;
|
|
|
|
#endif
|
2010-09-09 00:33:06 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
vgmstream->interleave_block_size = 0x10;
|
|
|
|
vgmstream->coding_type = coding_PSX;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loop_flag) {
|
|
|
|
if(loopStart!=0) {
|
2010-09-11 21:12:07 +02:00
|
|
|
vgmstream->loop_start_sample = ((((loopStart/vgmstream->interleave_block_size)-1)*vgmstream->interleave_block_size)/16*28)/channel_count;
|
2010-09-09 00:33:06 +02:00
|
|
|
if(loopStart%vgmstream->interleave_block_size) {
|
|
|
|
vgmstream->loop_start_sample += (((loopStart%vgmstream->interleave_block_size)-1)/16*14*channel_count);
|
|
|
|
}
|
|
|
|
}
|
2010-09-11 21:12:07 +02:00
|
|
|
vgmstream->loop_end_sample = ((((loopEnd/vgmstream->interleave_block_size)-1)*vgmstream->interleave_block_size)/16*28)/channel_count;
|
2010-09-09 00:33:06 +02:00
|
|
|
if(loopEnd%vgmstream->interleave_block_size) {
|
|
|
|
vgmstream->loop_end_sample += (((loopEnd%vgmstream->interleave_block_size)-1)/16*14*channel_count);
|
|
|
|
}
|
|
|
|
}
|
2010-09-05 00:06:39 +02:00
|
|
|
|
|
|
|
/* open the file for reading */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
STREAMFILE * file;
|
2010-09-10 23:49:56 +02:00
|
|
|
if(vgmstream->layout_type == layout_interleave) {
|
2010-09-09 00:33:06 +02:00
|
|
|
file = streamFile->open(streamFile,filename,STREAMFILE_DEFAULT_BUFFER_SIZE);
|
|
|
|
if (!file) goto fail;
|
|
|
|
for (i=0;i<channel_count;i++) {
|
|
|
|
vgmstream->ch[i].streamfile = file;
|
2010-09-05 00:06:39 +02:00
|
|
|
|
2010-09-09 00:33:06 +02:00
|
|
|
vgmstream->ch[i].channel_start_offset=
|
|
|
|
vgmstream->ch[i].offset=start_offset+
|
|
|
|
vgmstream->interleave_block_size*i;
|
2010-09-05 00:06:39 +02:00
|
|
|
|
2010-09-09 00:33:06 +02:00
|
|
|
}
|
|
|
|
}
|
2010-09-10 23:49:56 +02:00
|
|
|
#ifdef VGM_USE_MPEG
|
|
|
|
else if(vgmstream->layout_type == layout_mpeg) {
|
|
|
|
for (i=0;i<channel_count;i++) {
|
|
|
|
vgmstream->ch[i].streamfile = streamFile->open(streamFile,filename,MPEG_BUFFER_SIZE);
|
|
|
|
vgmstream->ch[i].channel_start_offset= vgmstream->ch[i].offset=start_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else { goto fail; }
|
2010-09-05 00:06:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
/* clean up anything we may have opened */
|
|
|
|
fail:
|
2010-09-11 00:06:50 +02:00
|
|
|
#ifdef VGM_USE_MPEG
|
2010-09-10 23:57:55 +02:00
|
|
|
if (mpeg_data) {
|
|
|
|
mpg123_delete(mpeg_data->m);
|
|
|
|
free(mpeg_data);
|
|
|
|
|
|
|
|
if (vgmstream) {
|
|
|
|
vgmstream->codec_data = NULL;
|
|
|
|
}
|
|
|
|
}
|
2010-09-11 00:06:50 +02:00
|
|
|
#endif
|
2010-09-05 00:06:39 +02:00
|
|
|
if (vgmstream) close_vgmstream(vgmstream);
|
|
|
|
return NULL;
|
|
|
|
}
|