2008-05-17 23:52:40 +02:00
|
|
|
#include "meta.h"
|
|
|
|
#include "../util.h"
|
|
|
|
|
|
|
|
|
2016-12-21 00:48:47 +01:00
|
|
|
static int vag_find_loop_points(STREAMFILE *streamFile, off_t * loop_start, off_t * loop_end, off_t offset);
|
2008-05-17 23:52:40 +02:00
|
|
|
|
2016-12-20 23:29:36 +01:00
|
|
|
/* PS2 VAG format, found in many Sony games
|
2008-05-17 23:52:40 +02:00
|
|
|
|
|
|
|
2008-05-17 - Fastelbja : First version ...
|
|
|
|
*/
|
2008-05-20 17:18:38 +02:00
|
|
|
VGMSTREAM * init_vgmstream_ps2_vag(STREAMFILE *streamFile) {
|
2008-05-17 23:52:40 +02:00
|
|
|
VGMSTREAM * vgmstream = NULL;
|
2013-05-27 05:55:50 +02:00
|
|
|
char filename[PATH_LIMIT];
|
2008-05-17 23:52:40 +02:00
|
|
|
|
|
|
|
off_t loopStart = 0;
|
|
|
|
off_t loopEnd = 0;
|
|
|
|
|
|
|
|
uint8_t vagID;
|
|
|
|
off_t start_offset;
|
|
|
|
|
|
|
|
size_t interleave;
|
|
|
|
|
|
|
|
int loop_flag=0;
|
2016-12-20 23:29:36 +01:00
|
|
|
int channel_count=0;
|
2008-05-17 23:52:40 +02:00
|
|
|
int i;
|
2016-12-20 23:29:36 +01:00
|
|
|
int loop_samples_found = 0;
|
2008-05-17 23:52:40 +02:00
|
|
|
|
|
|
|
/* check extension, case insensitive */
|
2008-05-20 17:18:38 +02:00
|
|
|
streamFile->get_name(streamFile,filename,sizeof(filename));
|
2008-08-19 16:31:22 +02:00
|
|
|
if (strcasecmp("vag",filename_extension(filename))) goto fail;
|
2008-05-17 23:52:40 +02:00
|
|
|
|
|
|
|
/* check VAG Header */
|
2016-12-20 23:29:36 +01:00
|
|
|
if (((read_32bitBE(0x00,streamFile) & 0xFFFFFF00) != 0x56414700) && /* "VAG\0" */
|
2008-05-20 17:18:38 +02:00
|
|
|
((read_32bitLE(0x00,streamFile) & 0xFFFFFF00) != 0x56414700))
|
2008-05-17 23:52:40 +02:00
|
|
|
goto fail;
|
|
|
|
|
2016-12-20 23:29:36 +01:00
|
|
|
/* Check for correct channel count and loop flag */
|
2008-05-20 17:18:38 +02:00
|
|
|
vagID=read_8bit(0x03,streamFile);
|
2008-05-17 23:52:40 +02:00
|
|
|
|
|
|
|
switch(vagID) {
|
2016-12-20 23:29:36 +01:00
|
|
|
case '1': /* "VAG1" (1 channel) */
|
2011-05-08 05:47:20 +02:00
|
|
|
channel_count=1;
|
2011-05-08 06:09:34 +02:00
|
|
|
break;
|
2016-12-20 23:29:36 +01:00
|
|
|
case '2': /* "VAG2" (2 channels) */
|
2011-05-08 06:09:34 +02:00
|
|
|
channel_count=2;
|
2011-05-08 05:47:20 +02:00
|
|
|
break;
|
2016-12-20 23:29:36 +01:00
|
|
|
case 'i': /* "VAGi" (interleaved) */
|
2008-05-17 23:52:40 +02:00
|
|
|
channel_count=2;
|
|
|
|
break;
|
2016-12-20 23:29:36 +01:00
|
|
|
case 'V': /* pGAV (little endian header) */
|
|
|
|
if(read_32bitBE(0x20,streamFile)==0x53746572) /* "Ster" */
|
2008-05-17 23:52:40 +02:00
|
|
|
channel_count=2;
|
2016-12-20 23:29:36 +01:00
|
|
|
else
|
|
|
|
channel_count=1;
|
|
|
|
break;
|
|
|
|
case 'p': /* "VAGp" (extended) */
|
2008-07-18 23:42:35 +02:00
|
|
|
if((read_32bitBE(0x04,streamFile)<=0x00000004) && (read_32bitBE(0x0c,streamFile)<(get_streamfile_size(streamFile)/2))) {
|
2008-05-28 13:36:17 +02:00
|
|
|
loop_flag=(read_32bitBE(0x14,streamFile)!=0);
|
|
|
|
channel_count=2;
|
2016-12-20 23:29:36 +01:00
|
|
|
}
|
|
|
|
else {
|
2016-12-21 00:48:47 +01:00
|
|
|
loop_flag = vag_find_loop_points(streamFile, &loopStart, &loopEnd, 0x30);
|
2016-12-20 23:29:36 +01:00
|
|
|
channel_count = 1;
|
2008-05-28 13:36:17 +02:00
|
|
|
}
|
2008-05-17 23:52:40 +02:00
|
|
|
break;
|
2008-05-18 07:05:49 +02:00
|
|
|
default:
|
|
|
|
goto fail;
|
2008-05-17 23:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
|
|
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
|
|
|
/* fill in the vital statistics */
|
|
|
|
vgmstream->channels = channel_count;
|
2016-12-20 23:29:36 +01:00
|
|
|
vgmstream->coding_type = coding_PSX;
|
2008-05-17 23:52:40 +02:00
|
|
|
|
2016-12-20 23:29:36 +01:00
|
|
|
switch(vagID) {
|
2011-05-08 05:47:20 +02:00
|
|
|
case '1': // VAG1
|
|
|
|
vgmstream->layout_type=layout_none;
|
|
|
|
vgmstream->sample_rate = read_32bitBE(0x10,streamFile);
|
|
|
|
vgmstream->num_samples = read_32bitBE(0x0C,streamFile)/16*28;
|
|
|
|
interleave = read_32bitLE(0x08,streamFile);
|
|
|
|
if (interleave != 0) goto fail;
|
|
|
|
vgmstream->meta_type=meta_PS2_VAG1;
|
|
|
|
start_offset=0x40;
|
|
|
|
break;
|
2011-05-08 06:09:34 +02:00
|
|
|
case '2': // VAG2
|
|
|
|
vgmstream->layout_type=layout_interleave;
|
|
|
|
vgmstream->sample_rate = read_32bitBE(0x10,streamFile);
|
|
|
|
vgmstream->num_samples = read_32bitBE(0x0C,streamFile)/16*28;
|
|
|
|
interleave = 0x800;
|
|
|
|
vgmstream->meta_type=meta_PS2_VAG2;
|
|
|
|
start_offset=0x40;
|
|
|
|
break;
|
2008-05-17 23:52:40 +02:00
|
|
|
case 'i': // VAGi
|
|
|
|
vgmstream->layout_type=layout_interleave;
|
2008-05-20 17:18:38 +02:00
|
|
|
vgmstream->sample_rate = read_32bitBE(0x10,streamFile);
|
|
|
|
vgmstream->num_samples = read_32bitBE(0x0C,streamFile)/16*28;
|
|
|
|
interleave = read_32bitLE(0x08,streamFile);
|
2008-05-17 23:52:40 +02:00
|
|
|
vgmstream->meta_type=meta_PS2_VAGi;
|
|
|
|
start_offset=0x800;
|
|
|
|
break;
|
|
|
|
case 'p': // VAGp
|
2008-05-20 17:18:38 +02:00
|
|
|
vgmstream->sample_rate = read_32bitBE(0x10,streamFile);
|
2016-12-20 23:29:36 +01:00
|
|
|
interleave=0x10;
|
2008-05-28 13:36:17 +02:00
|
|
|
|
2008-07-18 23:42:35 +02:00
|
|
|
if((read_32bitBE(0x04,streamFile)==0x00000004) && (read_32bitBE(0x0c,streamFile)<(get_streamfile_size(streamFile)/2))) {
|
2008-05-28 13:36:17 +02:00
|
|
|
vgmstream->channels=2;
|
|
|
|
vgmstream->num_samples = read_32bitBE(0x0C,streamFile);
|
|
|
|
|
|
|
|
if(loop_flag) {
|
|
|
|
vgmstream->loop_start_sample=read_32bitBE(0x14,streamFile);
|
|
|
|
vgmstream->loop_end_sample =read_32bitBE(0x18,streamFile);
|
2016-12-20 23:29:36 +01:00
|
|
|
loop_samples_found = 1;
|
2008-05-28 13:36:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
start_offset=0x80;
|
|
|
|
vgmstream->layout_type=layout_interleave;
|
|
|
|
vgmstream->meta_type=meta_PS2_VAGs;
|
2008-06-07 23:11:33 +02:00
|
|
|
|
|
|
|
// Double VAG Header @ 0x0000 & 0x1000
|
|
|
|
if(read_32bitBE(0,streamFile)==read_32bitBE(0x1000,streamFile)) {
|
|
|
|
vgmstream->num_samples = read_32bitBE(0x0C,streamFile)/16*28;
|
|
|
|
interleave=0x1000;
|
|
|
|
start_offset=0;
|
|
|
|
}
|
2016-12-20 23:29:36 +01:00
|
|
|
}
|
|
|
|
else {
|
2008-05-28 13:36:17 +02:00
|
|
|
vgmstream->layout_type=layout_none;
|
|
|
|
vgmstream->num_samples = read_32bitBE(0x0C,streamFile)/16*28;
|
|
|
|
vgmstream->meta_type=meta_PS2_VAGp;
|
|
|
|
start_offset=0x30;
|
|
|
|
}
|
2008-05-17 23:52:40 +02:00
|
|
|
break;
|
|
|
|
case 'V': // pGAV
|
|
|
|
vgmstream->layout_type=layout_interleave;
|
|
|
|
interleave=0x2000;
|
|
|
|
|
|
|
|
// Jak X hack ...
|
2008-05-20 17:18:38 +02:00
|
|
|
if(read_32bitLE(0x1000,streamFile)==0x56414770)
|
2008-05-17 23:52:40 +02:00
|
|
|
interleave=0x1000;
|
|
|
|
|
2008-05-20 17:18:38 +02:00
|
|
|
vgmstream->sample_rate = read_32bitLE(0x10,streamFile);
|
|
|
|
vgmstream->num_samples = read_32bitLE(0x0C,streamFile)/16*14;
|
2008-05-17 23:52:40 +02:00
|
|
|
vgmstream->meta_type=meta_PS2_pGAV;
|
|
|
|
start_offset=0;
|
|
|
|
break;
|
2008-05-18 07:05:49 +02:00
|
|
|
default:
|
|
|
|
goto fail;
|
2008-05-17 23:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vgmstream->interleave_block_size=interleave;
|
|
|
|
|
|
|
|
/* Don't add the header size to loop calc points */
|
2016-12-20 23:29:36 +01:00
|
|
|
if(loop_flag && !loop_samples_found) {
|
2008-05-28 13:36:17 +02:00
|
|
|
loopStart-=start_offset;
|
|
|
|
loopEnd-=start_offset;
|
|
|
|
|
2016-12-20 23:29:36 +01:00
|
|
|
vgmstream->loop_start_sample = (int32_t)((loopStart/(interleave*channel_count))*interleave)/16*28;
|
|
|
|
vgmstream->loop_start_sample += (int32_t)(loopStart%(interleave*channel_count))/16*28;
|
|
|
|
vgmstream->loop_end_sample = (int32_t)((loopEnd/(interleave*channel_count))*interleave)/16*28;
|
|
|
|
vgmstream->loop_end_sample += (int32_t)(loopEnd%(interleave*channel_count))/16*28;
|
2008-05-17 23:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* open the file for reading by each channel */
|
|
|
|
{
|
|
|
|
for (i=0;i<channel_count;i++) {
|
2011-05-08 05:47:20 +02:00
|
|
|
if (vgmstream->interleave_block_size > 0) {
|
|
|
|
vgmstream->ch[i].streamfile = streamFile->open(streamFile,filename,vgmstream->interleave_block_size);
|
|
|
|
} else {
|
|
|
|
vgmstream->ch[i].streamfile = streamFile->open(streamFile,filename,STREAMFILE_DEFAULT_BUFFER_SIZE);
|
|
|
|
}
|
2008-05-17 23:52:40 +02:00
|
|
|
|
|
|
|
if (!vgmstream->ch[i].streamfile) goto fail;
|
|
|
|
|
|
|
|
vgmstream->ch[i].channel_start_offset=
|
|
|
|
vgmstream->ch[i].offset=
|
|
|
|
(off_t)(start_offset+vgmstream->interleave_block_size*i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
/* clean up anything we may have opened */
|
|
|
|
fail:
|
|
|
|
if (vgmstream) close_vgmstream(vgmstream);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-12-20 23:29:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds loop points in VAG data using flag markers and updates loop_start and loop_end with the offsets.
|
|
|
|
*
|
|
|
|
* returns 0 if not found
|
|
|
|
*/
|
2016-12-21 00:48:47 +01:00
|
|
|
static int vag_find_loop_points(STREAMFILE *streamFile, off_t * loop_start, off_t * loop_end, off_t offset) {
|
2016-12-20 23:29:36 +01:00
|
|
|
off_t loopStart = 0;
|
|
|
|
off_t loopEnd = 0;
|
|
|
|
|
|
|
|
/* used for loop points ... */
|
|
|
|
uint8_t eofVAG[16]={0x00,0x07,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77};
|
|
|
|
uint8_t eofVAG2[16]={0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
|
|
|
|
uint8_t readbuf[16];
|
2016-12-21 00:48:47 +01:00
|
|
|
uint8_t flag;
|
2016-12-20 23:29:36 +01:00
|
|
|
|
|
|
|
/* Search for loop in VAG */
|
|
|
|
size_t fileLength = get_streamfile_size(streamFile);
|
|
|
|
|
|
|
|
|
2016-12-21 00:48:47 +01:00
|
|
|
off_t readOffset = offset - 0x10;
|
2016-12-20 23:29:36 +01:00
|
|
|
do {
|
|
|
|
readOffset+=0x10;
|
|
|
|
|
2016-12-21 00:48:47 +01:00
|
|
|
flag = read_8bit(readOffset+0x01,streamFile) & 0x0F; /* lower nibble (for HEVAG) */
|
|
|
|
|
2016-12-20 23:29:36 +01:00
|
|
|
// Loop Start ...
|
2016-12-21 00:48:47 +01:00
|
|
|
if (flag == 0x06 && !loopStart) {
|
|
|
|
loopStart = readOffset;
|
2016-12-20 23:29:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop End ...
|
2016-12-21 00:48:47 +01:00
|
|
|
if (flag == 0x03 && !loopEnd) {
|
|
|
|
loopEnd = readOffset;
|
|
|
|
|
|
|
|
if (loopStart && loopEnd)
|
|
|
|
break;
|
2016-12-20 23:29:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop from end to beginning ...
|
2016-12-21 00:48:47 +01:00
|
|
|
if (flag == 0x01) {
|
2016-12-20 23:29:36 +01:00
|
|
|
// Check if we have the eof tag after the loop point ...
|
|
|
|
// if so we don't loop, if not present, we loop from end to start ...
|
2016-12-21 00:48:47 +01:00
|
|
|
int read = read_streamfile(readbuf,readOffset+0x10,0x10,streamFile);
|
|
|
|
if(read > 0 && readbuf[0]!=0x00 && readbuf[0]!=0x0c) { /* is there valid data after flag 0x1? */
|
|
|
|
if(memcmp(readbuf,eofVAG,0x10) && (memcmp(readbuf,eofVAG2,0x10))) { /* probably could just check flag 0x7 */
|
|
|
|
loopStart = offset + 0x10;
|
2016-12-20 23:29:36 +01:00
|
|
|
loopEnd = readOffset;
|
2016-12-21 00:48:47 +01:00
|
|
|
break;
|
2016-12-20 23:29:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (streamFile->get_offset(streamFile)<(off_t)fileLength);
|
|
|
|
|
|
|
|
|
|
|
|
if (loopEnd) {
|
|
|
|
*loop_start = loopStart;
|
|
|
|
*loop_end = loopEnd;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|