mirror of
https://github.com/vgmstream/vgmstream.git
synced 2024-11-24 15:00:11 +01:00
Added Killzone VAGp (.vag); code cleanup
This commit is contained in:
parent
973c4bff1d
commit
46d7fcfe80
@ -2,11 +2,10 @@
|
|||||||
#include "../util.h"
|
#include "../util.h"
|
||||||
|
|
||||||
|
|
||||||
static int vag_find_loop_points(STREAMFILE *streamFile, off_t * loop_start, off_t * loop_end, off_t offset);
|
static int vag_find_loop_offsets(STREAMFILE *streamFile, off_t start_offset, off_t * loop_start, off_t * loop_end);
|
||||||
|
|
||||||
/* PS2 VAG format, found in many Sony games
|
/**
|
||||||
|
* VAGp - SDK format, created by Sony's tools (like AIFF2VAG)
|
||||||
2008-05-17 - Fastelbja : First version ...
|
|
||||||
*/
|
*/
|
||||||
VGMSTREAM * init_vgmstream_ps2_vag(STREAMFILE *streamFile) {
|
VGMSTREAM * init_vgmstream_ps2_vag(STREAMFILE *streamFile) {
|
||||||
VGMSTREAM * vgmstream = NULL;
|
VGMSTREAM * vgmstream = NULL;
|
||||||
@ -16,14 +15,16 @@ VGMSTREAM * init_vgmstream_ps2_vag(STREAMFILE *streamFile) {
|
|||||||
off_t loopEnd = 0;
|
off_t loopEnd = 0;
|
||||||
|
|
||||||
uint8_t vagID;
|
uint8_t vagID;
|
||||||
|
uint32_t version = 0;
|
||||||
|
|
||||||
|
size_t filesize = 0, datasize = 0;
|
||||||
|
size_t interleave;
|
||||||
off_t start_offset;
|
off_t start_offset;
|
||||||
|
|
||||||
size_t interleave;
|
|
||||||
|
|
||||||
int loop_flag=0;
|
int loop_flag=0;
|
||||||
|
int loop_samples_found = 0;
|
||||||
int channel_count=0;
|
int channel_count=0;
|
||||||
int i;
|
int i;
|
||||||
int loop_samples_found = 0;
|
|
||||||
|
|
||||||
/* check extension, case insensitive */
|
/* check extension, case insensitive */
|
||||||
streamFile->get_name(streamFile,filename,sizeof(filename));
|
streamFile->get_name(streamFile,filename,sizeof(filename));
|
||||||
@ -34,36 +35,49 @@ VGMSTREAM * init_vgmstream_ps2_vag(STREAMFILE *streamFile) {
|
|||||||
((read_32bitLE(0x00,streamFile) & 0xFFFFFF00) != 0x56414700))
|
((read_32bitLE(0x00,streamFile) & 0xFFFFFF00) != 0x56414700))
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
|
filesize = get_streamfile_size(streamFile);
|
||||||
|
|
||||||
|
/* version used to create the file
|
||||||
|
* ex: 00000000 = v1.8 PC, 00000002 = v1.3 Mac, 00000003 = v1.6+ Mac, 00000020 = v2.0+ PC */
|
||||||
|
version = read_32bitBE(0x04,streamFile);
|
||||||
|
/* 0x08-0c: reserved */
|
||||||
|
datasize = read_32bitBE(0x0c,streamFile);
|
||||||
|
/* 0x14-20 reserved */
|
||||||
|
/* 0x20-30: name (optional) */
|
||||||
|
/* 0x30: data start (first 0x10 usually 0s to init SPU) */
|
||||||
|
|
||||||
/* Check for correct channel count and loop flag */
|
/* Check for correct channel count and loop flag */
|
||||||
vagID=read_8bit(0x03,streamFile);
|
vagID=read_8bit(0x03,streamFile);
|
||||||
|
|
||||||
switch(vagID) {
|
switch(vagID) {
|
||||||
case '1': /* "VAG1" (1 channel) */
|
case '1': /* "VAG1" (1 channel) [Metal Gear Solid 3] */
|
||||||
channel_count=1;
|
channel_count=1;
|
||||||
break;
|
break;
|
||||||
case '2': /* "VAG2" (2 channels) */
|
case '2': /* "VAG2" (2 channels) [Metal Gear Solid 3] */
|
||||||
channel_count=2;
|
channel_count=2;
|
||||||
break;
|
break;
|
||||||
case 'i': /* "VAGi" (interleaved) */
|
case 'i': /* "VAGi" (interleaved) */
|
||||||
channel_count=2;
|
channel_count=2;
|
||||||
break;
|
break;
|
||||||
case 'V': /* pGAV (little endian header) */
|
case 'V': /* pGAV (little endian / stereo) [Jak 3, Jak X] */
|
||||||
if(read_32bitBE(0x20,streamFile)==0x53746572) /* "Ster" */
|
if (read_32bitBE(0x20,streamFile)==0x53746572) /* "Ster" */
|
||||||
channel_count=2;
|
channel_count=2;
|
||||||
else
|
else
|
||||||
channel_count=1;
|
channel_count=1;
|
||||||
break;
|
break;
|
||||||
case 'p': /* "VAGp" (extended) */
|
case 'p': /* "VAGp" (extended) [most common, ex Ratchet & Clank] */
|
||||||
if((read_32bitBE(0x04,streamFile)<=0x00000004) && (read_32bitBE(0x0c,streamFile)<(get_streamfile_size(streamFile)/2))) {
|
|
||||||
|
if ((version <= 0x00000004) && (datasize < filesize / 2)) {
|
||||||
loop_flag=(read_32bitBE(0x14,streamFile)!=0);
|
loop_flag=(read_32bitBE(0x14,streamFile)!=0);
|
||||||
channel_count=2;
|
channel_count=2;
|
||||||
}
|
}
|
||||||
else if (read_32bitBE(0x04,streamFile) == 0x00020001) { /* HEVAG */
|
else if (version == 0x00020001) { /* HEVAG */
|
||||||
loop_flag = vag_find_loop_points(streamFile, &loopStart, &loopEnd, 0x30);
|
loop_flag = vag_find_loop_offsets(streamFile, 0x30, &loopStart, &loopEnd);
|
||||||
channel_count = read_8bit(0x1e,streamFile);
|
channel_count = read_8bit(0x1e,streamFile);
|
||||||
|
if (channel_count == 0)
|
||||||
|
channel_count = 1; /* ex. Lumines */
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
loop_flag = vag_find_loop_points(streamFile, &loopStart, &loopEnd, 0x30);
|
loop_flag = vag_find_loop_offsets(streamFile, 0x30, &loopStart, &loopEnd);
|
||||||
channel_count = 1;
|
channel_count = 1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -76,42 +90,38 @@ VGMSTREAM * init_vgmstream_ps2_vag(STREAMFILE *streamFile) {
|
|||||||
if (!vgmstream) goto fail;
|
if (!vgmstream) goto fail;
|
||||||
|
|
||||||
/* fill in the vital statistics */
|
/* fill in the vital statistics */
|
||||||
vgmstream->channels = channel_count;
|
|
||||||
vgmstream->coding_type = coding_PSX;
|
vgmstream->coding_type = coding_PSX;
|
||||||
|
vgmstream->sample_rate = read_32bitBE(0x10,streamFile);
|
||||||
|
|
||||||
switch(vagID) {
|
switch(vagID) {
|
||||||
case '1': // VAG1
|
case '1': // VAG1
|
||||||
vgmstream->layout_type=layout_none;
|
vgmstream->layout_type=layout_none;
|
||||||
vgmstream->sample_rate = read_32bitBE(0x10,streamFile);
|
vgmstream->num_samples = datasize / 16 * 28;
|
||||||
vgmstream->num_samples = read_32bitBE(0x0C,streamFile)/16*28;
|
|
||||||
interleave = read_32bitLE(0x08,streamFile);
|
interleave = read_32bitLE(0x08,streamFile);
|
||||||
if (interleave != 0) goto fail;
|
if (interleave != 0) goto fail;
|
||||||
vgmstream->meta_type=meta_PS2_VAG1;
|
vgmstream->meta_type=meta_PS2_VAG1;
|
||||||
start_offset=0x40;
|
start_offset=0x40; /* 0x30 is extra data in VAG1 */
|
||||||
break;
|
break;
|
||||||
case '2': // VAG2
|
case '2': // VAG2
|
||||||
vgmstream->layout_type=layout_interleave;
|
vgmstream->layout_type=layout_interleave;
|
||||||
vgmstream->sample_rate = read_32bitBE(0x10,streamFile);
|
vgmstream->num_samples = datasize / 16 * 28; /* datasize is for 1 channel only in VAG2 */
|
||||||
vgmstream->num_samples = read_32bitBE(0x0C,streamFile)/16*28;
|
|
||||||
interleave = 0x800;
|
interleave = 0x800;
|
||||||
vgmstream->meta_type=meta_PS2_VAG2;
|
vgmstream->meta_type=meta_PS2_VAG2;
|
||||||
start_offset=0x40;
|
start_offset=0x40; /* 0x30 is extra data in VAG2 */
|
||||||
break;
|
break;
|
||||||
case 'i': // VAGi
|
case 'i': // VAGi
|
||||||
vgmstream->layout_type=layout_interleave;
|
vgmstream->layout_type=layout_interleave;
|
||||||
vgmstream->sample_rate = read_32bitBE(0x10,streamFile);
|
vgmstream->num_samples = datasize / 16 * 28;
|
||||||
vgmstream->num_samples = read_32bitBE(0x0C,streamFile)/16*28;
|
|
||||||
interleave = read_32bitLE(0x08,streamFile);
|
interleave = read_32bitLE(0x08,streamFile);
|
||||||
vgmstream->meta_type=meta_PS2_VAGi;
|
vgmstream->meta_type=meta_PS2_VAGi;
|
||||||
start_offset=0x800;
|
start_offset=0x800;
|
||||||
break;
|
break;
|
||||||
case 'p': // VAGp
|
case 'p': // VAGp
|
||||||
vgmstream->sample_rate = read_32bitBE(0x10,streamFile);
|
|
||||||
interleave=0x10;
|
interleave=0x10;
|
||||||
|
|
||||||
if((read_32bitBE(0x04,streamFile)==0x00000004) && (read_32bitBE(0x0c,streamFile)<(get_streamfile_size(streamFile)/2))) {
|
if ((version == 0x00000004) && (datasize < filesize / 2)) {
|
||||||
vgmstream->channels=2;
|
vgmstream->channels=2;
|
||||||
vgmstream->num_samples = read_32bitBE(0x0C,streamFile);
|
vgmstream->num_samples = datasize; /* todo test if datasize/16*28? */
|
||||||
|
|
||||||
if(loop_flag) {
|
if(loop_flag) {
|
||||||
vgmstream->loop_start_sample=read_32bitBE(0x14,streamFile);
|
vgmstream->loop_start_sample=read_32bitBE(0x14,streamFile);
|
||||||
@ -125,33 +135,42 @@ VGMSTREAM * init_vgmstream_ps2_vag(STREAMFILE *streamFile) {
|
|||||||
|
|
||||||
// Double VAG Header @ 0x0000 & 0x1000
|
// Double VAG Header @ 0x0000 & 0x1000
|
||||||
if(read_32bitBE(0,streamFile)==read_32bitBE(0x1000,streamFile)) {
|
if(read_32bitBE(0,streamFile)==read_32bitBE(0x1000,streamFile)) {
|
||||||
vgmstream->num_samples = read_32bitBE(0x0C,streamFile)/16*28;
|
vgmstream->num_samples = datasize / 16 * 28;
|
||||||
interleave=0x1000;
|
interleave=0x1000;
|
||||||
start_offset=0;
|
start_offset=0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (read_32bitBE(0x04,streamFile) == 0x00020001) { /* HEVAG */
|
else if (version == 0x40000000) { /* Guerilla VAG (little endian) */
|
||||||
vgmstream->meta_type = meta_PS2_VAGs;
|
datasize = read_32bitLE(0x0c,streamFile);
|
||||||
|
vgmstream->sample_rate = read_32bitLE(0x10,streamFile);
|
||||||
|
vgmstream->layout_type=layout_none;
|
||||||
|
vgmstream->meta_type=meta_PS2_VAGp;
|
||||||
|
|
||||||
|
vgmstream->num_samples = datasize / channel_count / 16 * 28;
|
||||||
|
start_offset = 0x30;
|
||||||
|
}
|
||||||
|
else if (version == 0x00020001) { /* HEVAG */
|
||||||
vgmstream->coding_type = coding_HEVAG_ADPCM;
|
vgmstream->coding_type = coding_HEVAG_ADPCM;
|
||||||
vgmstream->layout_type = layout_interleave;
|
vgmstream->layout_type = layout_interleave;
|
||||||
|
vgmstream->meta_type = meta_PS2_VAGs;
|
||||||
|
|
||||||
vgmstream->num_samples = read_32bitBE(0x0C,streamFile) / channel_count / 16 * 28;
|
vgmstream->num_samples = datasize / channel_count / 16 * 28;
|
||||||
start_offset=0x30;
|
start_offset = 0x30;
|
||||||
}
|
}
|
||||||
else {
|
else { /* VAGp, usually separate L/R files */
|
||||||
vgmstream->layout_type=layout_none;
|
vgmstream->layout_type=layout_none;
|
||||||
vgmstream->num_samples = read_32bitBE(0x0C,streamFile)/16*28;
|
|
||||||
vgmstream->meta_type=meta_PS2_VAGp;
|
vgmstream->meta_type=meta_PS2_VAGp;
|
||||||
|
|
||||||
|
vgmstream->num_samples = datasize / channel_count / 16 * 28;
|
||||||
start_offset=0x30;
|
start_offset=0x30;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'V': // pGAV
|
case 'V': // pGAV
|
||||||
vgmstream->layout_type=layout_interleave;
|
vgmstream->layout_type=layout_interleave;
|
||||||
interleave=0x2000;
|
interleave=0x2000; /* Jak 3 interleave, includes header */
|
||||||
|
|
||||||
// Jak X hack ...
|
if(read_32bitLE(0x1000,streamFile)==0x56414770) /* "pGAV" */
|
||||||
if(read_32bitLE(0x1000,streamFile)==0x56414770)
|
interleave=0x1000; /* Jak X interleave, includes header */
|
||||||
interleave=0x1000;
|
|
||||||
|
|
||||||
vgmstream->sample_rate = read_32bitLE(0x10,streamFile);
|
vgmstream->sample_rate = read_32bitLE(0x10,streamFile);
|
||||||
vgmstream->num_samples = read_32bitLE(0x0C,streamFile)/16*14;
|
vgmstream->num_samples = read_32bitLE(0x0C,streamFile)/16*14;
|
||||||
@ -203,15 +222,16 @@ fail:
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds loop points in VAG data using flag markers and updates loop_start and loop_end with the offsets.
|
* Finds loop points in VAG data using flag markers and updates loop_start and loop_end with the global offsets.
|
||||||
*
|
*
|
||||||
* returns 0 if not found
|
* returns 0 if not found
|
||||||
*/
|
*/
|
||||||
static int vag_find_loop_points(STREAMFILE *streamFile, off_t * loop_start, off_t * loop_end, off_t offset) {
|
static int vag_find_loop_offsets(STREAMFILE *streamFile, off_t start_offset, off_t * loop_start, off_t * loop_end) {
|
||||||
off_t loopStart = 0;
|
off_t loopStart = 0;
|
||||||
off_t loopEnd = 0;
|
off_t loopEnd = 0;
|
||||||
|
|
||||||
/* used for loop points ... */
|
/* used for loop points (todo: variations: 0x0c0700..00, 0x070077..77 ) */
|
||||||
|
/* 'used to prevent unnecessary SPU interrupts' (optional if no IRQ or no looping) */
|
||||||
uint8_t eofVAG[16]={0x00,0x07,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77,0x77};
|
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 eofVAG2[16]={0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
|
||||||
uint8_t readbuf[16];
|
uint8_t readbuf[16];
|
||||||
@ -221,7 +241,7 @@ static int vag_find_loop_points(STREAMFILE *streamFile, off_t * loop_start, off_
|
|||||||
size_t fileLength = get_streamfile_size(streamFile);
|
size_t fileLength = get_streamfile_size(streamFile);
|
||||||
|
|
||||||
|
|
||||||
off_t readOffset = offset - 0x10;
|
off_t readOffset = start_offset - 0x10;
|
||||||
do {
|
do {
|
||||||
readOffset+=0x10;
|
readOffset+=0x10;
|
||||||
|
|
||||||
@ -246,8 +266,8 @@ static int vag_find_loop_points(STREAMFILE *streamFile, off_t * loop_start, off_
|
|||||||
// if so we don't loop, if not present, we loop from end to start ...
|
// if so we don't loop, if not present, we loop from end to start ...
|
||||||
int read = read_streamfile(readbuf,readOffset+0x10,0x10,streamFile);
|
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(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 */
|
if(memcmp(readbuf,eofVAG,0x10) && (memcmp(readbuf,eofVAG2,0x10))) { /* probably could just check flag 0x7*/
|
||||||
loopStart = offset + 0x10;
|
loopStart = start_offset + 0x10; /* todo proper start */
|
||||||
loopEnd = readOffset;
|
loopEnd = readOffset;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -257,7 +277,7 @@ static int vag_find_loop_points(STREAMFILE *streamFile, off_t * loop_start, off_
|
|||||||
} while (streamFile->get_offset(streamFile)<(off_t)fileLength);
|
} while (streamFile->get_offset(streamFile)<(off_t)fileLength);
|
||||||
|
|
||||||
|
|
||||||
if (loopEnd) {
|
if (loopStart && loopEnd) {
|
||||||
*loop_start = loopStart;
|
*loop_start = loopStart;
|
||||||
*loop_end = loopEnd;
|
*loop_end = loopEnd;
|
||||||
return 1;
|
return 1;
|
||||||
|
Loading…
Reference in New Issue
Block a user