mirror of
https://github.com/vgmstream/vgmstream.git
synced 2025-01-19 00:04:04 +01:00
Simplify XVAG/code cleanup preparations
This commit is contained in:
parent
4673050fcb
commit
d3a1fba917
@ -2,131 +2,176 @@
|
|||||||
#include "../coding/coding.h"
|
#include "../coding/coding.h"
|
||||||
#include "../util.h"
|
#include "../util.h"
|
||||||
|
|
||||||
/* XVAG (from Ratchet & Clank Future: Quest for Booty) */
|
static int ps_adpcm_find_loop_offsets(STREAMFILE *streamFile, int channel_count, off_t start_offset, off_t * loop_start, off_t * loop_end);
|
||||||
/* v0.1 : bxaimc : Initial release */
|
|
||||||
/* v0.2 : Fastelbja : add support for loops points */
|
|
||||||
/* + little endian header values */
|
|
||||||
|
|
||||||
|
/* XVAG - Sony's (second party?) format (God of War III, Ratchet & Clank Future, The Last of Us, Uncharted) */
|
||||||
VGMSTREAM * init_vgmstream_ps3_xvag(STREAMFILE *streamFile) {
|
VGMSTREAM * init_vgmstream_ps3_xvag(STREAMFILE *streamFile) {
|
||||||
VGMSTREAM * vgmstream = NULL;
|
VGMSTREAM * vgmstream = NULL;
|
||||||
char filename[PATH_LIMIT];
|
int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL;
|
||||||
uint8_t testBuffer[0x10];
|
int loop_flag = 0, channel_count, codec;
|
||||||
|
|
||||||
off_t start_offset;
|
off_t start_offset, loop_start, loop_end, chunk_offset;
|
||||||
off_t fileLength;
|
off_t first_offset = 0x20;
|
||||||
|
int little_endian;
|
||||||
int loop_flag = 0;
|
int sample_rate;
|
||||||
int channel_count;
|
int num_samples;
|
||||||
off_t readOffset = 0;
|
|
||||||
int little_endian = 0;
|
|
||||||
long sample_rate = 0;
|
|
||||||
long num_samples = 0;
|
|
||||||
|
|
||||||
#ifdef VGM_USE_MPEG
|
|
||||||
mpeg_codec_data *mpeg_data = NULL;
|
|
||||||
coding_t mpeg_coding_type = coding_MPEG1_L3;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int loopStartPointsCount=0;
|
|
||||||
int loopEndPointsCount=0;
|
|
||||||
uint16_t mp3ID;
|
|
||||||
off_t loopStartPoints[0x10];
|
|
||||||
off_t loopEndPoints[0x10];
|
|
||||||
|
|
||||||
off_t loopStart = 0;
|
|
||||||
off_t loopEnd = 0;
|
|
||||||
|
|
||||||
/* check extension, case insensitive */
|
/* check extension, case insensitive */
|
||||||
streamFile->get_name(streamFile,filename,sizeof(filename));
|
if (!check_extensions(streamFile,"xvag")) goto fail;
|
||||||
if (strcasecmp("xvag",filename_extension(filename))) goto fail;
|
|
||||||
|
|
||||||
/* check header */
|
/* check header */
|
||||||
if (read_32bitBE(0x00,streamFile) != 0x58564147) /* "XVAG" */
|
if (read_32bitBE(0x00,streamFile) != 0x58564147) /* "XVAG" */
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
fileLength = get_streamfile_size(streamFile);
|
little_endian = read_8bit(0x07,streamFile)==0; /* empty start_offset > little endian */
|
||||||
|
if (little_endian) {
|
||||||
if (read_8bit(0x07,streamFile)==0)
|
read_32bit = read_32bitLE;
|
||||||
little_endian=1;
|
} else {
|
||||||
|
read_32bit = read_32bitBE;
|
||||||
if(little_endian)
|
|
||||||
{
|
|
||||||
channel_count = read_32bitLE(0x28,streamFile);
|
|
||||||
start_offset = read_32bitLE(0x4,streamFile);
|
|
||||||
sample_rate = read_32bitLE(0x3c,streamFile);
|
|
||||||
num_samples = read_32bitLE(0x30,streamFile);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
channel_count = read_32bitBE(0x28,streamFile);
|
|
||||||
start_offset = read_32bitBE(0x4,streamFile);
|
|
||||||
sample_rate = read_32bitBE(0x3c,streamFile);
|
|
||||||
num_samples = read_32bitBE(0x30,streamFile);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
readOffset=start_offset;
|
start_offset = read_32bit(0x04,streamFile);
|
||||||
|
/* 0x08: flags? (&0x01=big endian?) 0x0a: version (chunk sizes vary) */
|
||||||
|
|
||||||
|
/* "fmat": base format */
|
||||||
|
if (!find_chunk(streamFile, 0x666D6174,first_offset,0, &chunk_offset,NULL, !little_endian)) goto fail; /*"fmat"*/
|
||||||
|
channel_count = read_32bit(chunk_offset+0x00,streamFile);
|
||||||
|
codec = read_32bit(chunk_offset+0x04,streamFile);
|
||||||
|
num_samples = read_32bit(chunk_offset+0x08,streamFile);
|
||||||
|
/* 0x0c: samples again? 0x10: 1? */
|
||||||
|
sample_rate = read_32bit(chunk_offset+0x14,streamFile);
|
||||||
|
/* 0x18: datasize */
|
||||||
|
|
||||||
|
/* other chunks: */
|
||||||
|
/* "cpan": pan/volume per channel */
|
||||||
|
/* "0000": last before start_offset */
|
||||||
|
|
||||||
|
//if ((uint16_t)read_16bitBE(start_offset,streamFile)==0xFFFB) codec = 0x08;
|
||||||
|
if (codec == 0x06) { /* todo not sure if there are any looping XVAGs */
|
||||||
|
loop_flag = ps_adpcm_find_loop_offsets(streamFile, channel_count, start_offset, &loop_start, &loop_end);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* build the VGMSTREAM */
|
||||||
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
||||||
|
if (!vgmstream) goto fail;
|
||||||
|
|
||||||
|
vgmstream->sample_rate = sample_rate;
|
||||||
|
vgmstream->num_samples = num_samples;
|
||||||
|
vgmstream->meta_type = meta_PS3_XVAG;
|
||||||
|
|
||||||
|
switch (codec) {
|
||||||
|
case 0x06: { /* PS ADPCM: God of War III, Uncharted 1/2, Ratchet and Clank Future */
|
||||||
|
vgmstream->layout_type = layout_interleave;
|
||||||
|
vgmstream->interleave_block_size = 0x10;
|
||||||
|
vgmstream->coding_type = coding_PSX;
|
||||||
|
|
||||||
|
if (loop_flag) {
|
||||||
|
if (loop_start!=0) {
|
||||||
|
vgmstream->loop_start_sample = ((((loop_start/vgmstream->interleave_block_size)-1)*vgmstream->interleave_block_size)/16*28)/channel_count;
|
||||||
|
if(loop_start%vgmstream->interleave_block_size)
|
||||||
|
vgmstream->loop_start_sample += (((loop_start%vgmstream->interleave_block_size)-1)/16*14*channel_count);
|
||||||
|
}
|
||||||
|
vgmstream->loop_end_sample = ((((loop_end/vgmstream->interleave_block_size)-1)*vgmstream->interleave_block_size)/16*28)/channel_count;
|
||||||
|
if (loop_end%vgmstream->interleave_block_size)
|
||||||
|
vgmstream->loop_end_sample += (((loop_end%vgmstream->interleave_block_size)-1)/16*14*channel_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// MP3s ?
|
|
||||||
mp3ID=(uint16_t)read_16bitBE(start_offset,streamFile);
|
|
||||||
if(mp3ID==0xFFFB) {
|
|
||||||
#ifdef VGM_USE_MPEG
|
#ifdef VGM_USE_MPEG
|
||||||
int rate;
|
case 0x08: { /* MPEG: The Last of Us, Uncharted 3, Medieval Moves */
|
||||||
int channels;
|
mpeg_codec_data *mpeg_data = NULL;
|
||||||
|
coding_t coding_type;
|
||||||
|
|
||||||
mpeg_data = init_mpeg_codec_data(streamFile, start_offset, -1, -1, &mpeg_coding_type, &rate, &channels); // -1 to not check sample rate or channels
|
/* "mpin": mpeg info */
|
||||||
|
/* 0x00/04: mpeg version/layer? 0x1c: frame size; other: unknown or repeats of "fmat" */
|
||||||
|
|
||||||
|
mpeg_data = init_mpeg_codec_data(streamFile, start_offset, sample_rate, channel_count, &coding_type, NULL, NULL);
|
||||||
if (!mpeg_data) goto fail;
|
if (!mpeg_data) goto fail;
|
||||||
|
|
||||||
channel_count = channels;
|
/* todo num_samples seems to be quite wrong for MPEG */
|
||||||
sample_rate = rate;
|
vgmstream->codec_data = mpeg_data;
|
||||||
|
vgmstream->layout_type = layout_mpeg;
|
||||||
|
vgmstream->coding_type = coding_type;
|
||||||
|
|
||||||
#else
|
break;
|
||||||
// reject if no MPEG support
|
}
|
||||||
goto fail;
|
|
||||||
#endif
|
#endif
|
||||||
} else {
|
|
||||||
|
case 0x09: { /* ATRAC9: Sly Cooper and the Thievius Raccoonus */
|
||||||
|
/* "a9in" chunk: ATRAC9 info */
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* open the file for reading */
|
||||||
|
if (!vgmstream_open_stream(vgmstream,streamFile,start_offset))
|
||||||
|
goto fail;
|
||||||
|
|
||||||
|
return vgmstream;
|
||||||
|
|
||||||
|
fail:
|
||||||
|
if (vgmstream) close_vgmstream(vgmstream);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int ps_adpcm_find_loop_offsets(STREAMFILE *streamFile, int channel_count, off_t start_offset, off_t * loop_start, off_t * loop_end) {
|
||||||
|
uint8_t testBuffer[0x10];
|
||||||
|
int loopStartPointsCount=0;
|
||||||
|
int loopEndPointsCount=0;
|
||||||
|
off_t readOffset = 0;
|
||||||
|
off_t loopStartPoints[0x10];
|
||||||
|
off_t loopEndPoints[0x10];
|
||||||
|
|
||||||
|
off_t loopStart = 0;
|
||||||
|
off_t loopEnd = 0;
|
||||||
|
off_t fileLength;
|
||||||
|
int loop_flag = 0;
|
||||||
|
|
||||||
|
readOffset=start_offset;
|
||||||
|
fileLength = get_streamfile_size(streamFile);
|
||||||
|
|
||||||
// get the loops the same way we get on .MIB
|
// get the loops the same way we get on .MIB
|
||||||
do {
|
do {
|
||||||
readOffset+=(off_t)read_streamfile(testBuffer,readOffset,0x10,streamFile);
|
readOffset+=(off_t)read_streamfile(testBuffer,readOffset,0x10,streamFile);
|
||||||
|
|
||||||
// Loop Start ...
|
// Loop Start ...
|
||||||
if(testBuffer[0x01]==0x06)
|
if(testBuffer[0x01]==0x06) {
|
||||||
{
|
if(loopStartPointsCount<0x10) {
|
||||||
if(loopStartPointsCount<0x10)
|
|
||||||
{
|
|
||||||
loopStartPoints[loopStartPointsCount] = readOffset-0x10;
|
loopStartPoints[loopStartPointsCount] = readOffset-0x10;
|
||||||
loopStartPointsCount++;
|
loopStartPointsCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loop End ...
|
// Loop End ...
|
||||||
if(((testBuffer[0x01]==0x03) && (testBuffer[0x03]!=0x77)) ||
|
if(((testBuffer[0x01]==0x03) && (testBuffer[0x03]!=0x77)) || (testBuffer[0x01]==0x01)) {
|
||||||
(testBuffer[0x01]==0x01))
|
if(loopEndPointsCount<0x10) {
|
||||||
{
|
|
||||||
if(loopEndPointsCount<0x10)
|
|
||||||
{
|
|
||||||
loopEndPoints[loopEndPointsCount] = readOffset; //-0x10;
|
loopEndPoints[loopEndPointsCount] = readOffset; //-0x10;
|
||||||
loopEndPointsCount++;
|
loopEndPointsCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} while (readOffset<((int32_t)fileLength));
|
} while (readOffset<((int32_t)fileLength));
|
||||||
|
|
||||||
// Calc Loop Points & Interleave ...
|
// Calc Loop Points & Interleave ...
|
||||||
if(loopStartPointsCount>=channel_count)
|
if(loopStartPointsCount>=channel_count) {
|
||||||
{
|
|
||||||
// can't get more then 0x10 loop point !
|
// can't get more then 0x10 loop point !
|
||||||
if((loopStartPointsCount<=0x0F) && (loopStartPointsCount>=2))
|
if((loopStartPointsCount<=0x0F) && (loopStartPointsCount>=2)) {
|
||||||
{
|
|
||||||
// Always took the first 2 loop points
|
// Always took the first 2 loop points
|
||||||
loopStart=loopStartPoints[1]-start_offset;
|
loopStart=loopStartPoints[1]-start_offset;
|
||||||
loop_flag=1;
|
loop_flag=1;
|
||||||
} else
|
} else {
|
||||||
loopStart=0;
|
loopStart=0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(loopEndPointsCount>=channel_count)
|
if(loopEndPointsCount>=channel_count) {
|
||||||
{
|
|
||||||
// can't get more then 0x10 loop point !
|
// can't get more then 0x10 loop point !
|
||||||
if((loopEndPointsCount<=0x0F) && (loopEndPointsCount>=2)) {
|
if((loopEndPointsCount<=0x0F) && (loopEndPointsCount>=2)) {
|
||||||
loop_flag=1;
|
loop_flag=1;
|
||||||
@ -142,83 +187,11 @@ VGMSTREAM * init_vgmstream_ps3_xvag(STREAMFILE *streamFile) {
|
|||||||
// i know that i can cover 95% of the file, but can't work on some of them
|
// 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)
|
if(read_8bit((fileLength-1),streamFile)==0)
|
||||||
loop_flag=0;
|
loop_flag=0;
|
||||||
}
|
|
||||||
|
|
||||||
/* build the VGMSTREAM */
|
|
||||||
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
|
||||||
if (!vgmstream) goto fail;
|
|
||||||
|
|
||||||
vgmstream->sample_rate = sample_rate;
|
|
||||||
vgmstream->num_samples = num_samples;
|
|
||||||
vgmstream->channels = channel_count;
|
|
||||||
vgmstream->meta_type = meta_PS3_XVAG;
|
|
||||||
|
|
||||||
if(mp3ID==0xFFFB) {
|
|
||||||
#ifdef VGM_USE_MPEG
|
|
||||||
/* NOTE: num_samples seems to be quite wrong for MPEG */
|
|
||||||
vgmstream->codec_data = mpeg_data;
|
|
||||||
vgmstream->layout_type = layout_mpeg;
|
|
||||||
vgmstream->coding_type = mpeg_coding_type;
|
|
||||||
#else
|
|
||||||
// reject if no MPEG support
|
|
||||||
goto fail;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
vgmstream->layout_type = layout_interleave;
|
|
||||||
vgmstream->interleave_block_size = 0x10;
|
|
||||||
vgmstream->coding_type = coding_PSX;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (loop_flag) {
|
if (loop_flag) {
|
||||||
if(loopStart!=0) {
|
*loop_start = loopStart;
|
||||||
vgmstream->loop_start_sample = ((((loopStart/vgmstream->interleave_block_size)-1)*vgmstream->interleave_block_size)/16*28)/channel_count;
|
*loop_end = loopEnd;
|
||||||
if(loopStart%vgmstream->interleave_block_size) {
|
|
||||||
vgmstream->loop_start_sample += (((loopStart%vgmstream->interleave_block_size)-1)/16*14*channel_count);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
vgmstream->loop_end_sample = ((((loopEnd/vgmstream->interleave_block_size)-1)*vgmstream->interleave_block_size)/16*28)/channel_count;
|
|
||||||
if(loopEnd%vgmstream->interleave_block_size) {
|
|
||||||
vgmstream->loop_end_sample += (((loopEnd%vgmstream->interleave_block_size)-1)/16*14*channel_count);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* open the file for reading */
|
return loop_flag;
|
||||||
{
|
|
||||||
int i;
|
|
||||||
STREAMFILE * file;
|
|
||||||
if(vgmstream->layout_type == layout_interleave) {
|
|
||||||
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;
|
|
||||||
|
|
||||||
vgmstream->ch[i].channel_start_offset=
|
|
||||||
vgmstream->ch[i].offset=start_offset+
|
|
||||||
vgmstream->interleave_block_size*i;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#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,STREAMFILE_DEFAULT_BUFFER_SIZE);
|
|
||||||
vgmstream->ch[i].channel_start_offset= vgmstream->ch[i].offset=start_offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
else { goto fail; }
|
|
||||||
}
|
|
||||||
|
|
||||||
return vgmstream;
|
|
||||||
|
|
||||||
/* clean up anything we may have opened */
|
|
||||||
fail:
|
|
||||||
#ifdef VGM_USE_MPEG
|
|
||||||
free_mpeg(mpeg_data);
|
|
||||||
if (vgmstream) vgmstream->codec_data = NULL;
|
|
||||||
#endif
|
|
||||||
if (vgmstream) close_vgmstream(vgmstream);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user