2008-07-20 18:34:01 +02:00
|
|
|
#include "meta.h"
|
|
|
|
#include "../util.h"
|
|
|
|
|
2018-06-10 21:08:06 +02:00
|
|
|
/* .ADS - Sony's "Audio Stream" format [Edit Racing (PS2), Evergrace II (PS2), Pri-Saga! Portable (PSP)] */
|
2008-07-20 18:34:01 +02:00
|
|
|
VGMSTREAM * init_vgmstream_ps2_ads(STREAMFILE *streamFile) {
|
|
|
|
VGMSTREAM * vgmstream = NULL;
|
2018-06-10 21:08:06 +02:00
|
|
|
int loop_flag, channel_count;
|
2017-11-10 22:22:04 +01:00
|
|
|
off_t start_offset;
|
2018-06-10 21:08:06 +02:00
|
|
|
size_t stream_size;
|
|
|
|
uint32_t loop_start, loop_end;
|
2009-09-08 08:03:14 +02:00
|
|
|
|
2008-12-29 22:03:07 +01:00
|
|
|
|
2018-06-10 21:08:06 +02:00
|
|
|
/* checks */
|
|
|
|
/* .ads: actual extension
|
|
|
|
* .ss2: demuxed videos (fake?)
|
|
|
|
* .pcm: Taisho Mononoke Ibunroku (PS2)
|
|
|
|
* .adx: Armored Core 3 (PS2) */
|
|
|
|
if (!check_extensions(streamFile, "ads,ss2,pcm,adx"))
|
2008-07-20 18:34:01 +02:00
|
|
|
goto fail;
|
|
|
|
|
2018-06-10 21:08:06 +02:00
|
|
|
if (read_32bitBE(0x00,streamFile) != 0x53536864 && /* "SShd" */
|
|
|
|
read_32bitBE(0x20,streamFile) != 0x53536264) /* "SSbd" */
|
2008-07-20 18:34:01 +02:00
|
|
|
goto fail;
|
2018-06-10 21:08:06 +02:00
|
|
|
/* 0x04: header size, always 0x20 */
|
|
|
|
|
2008-07-20 18:34:01 +02:00
|
|
|
|
2018-06-10 21:08:06 +02:00
|
|
|
/* check if file is not corrupt */ //todo ???
|
2017-11-10 22:22:04 +01:00
|
|
|
/* seems the Gran Turismo 4 ADS files are considered corrupt,*/
|
|
|
|
/* so I changed it to adapt the stream size if that's the case */
|
|
|
|
/* instead of failing playing them at all*/
|
2018-06-10 21:08:06 +02:00
|
|
|
stream_size = read_32bitLE(0x24,streamFile); /* body size */
|
|
|
|
if (stream_size + 0x28 >= get_streamfile_size(streamFile)) {
|
|
|
|
stream_size = get_streamfile_size(streamFile) - 0x28;
|
2017-11-10 22:22:04 +01:00
|
|
|
}
|
2008-07-20 18:34:01 +02:00
|
|
|
|
2018-06-10 21:08:06 +02:00
|
|
|
/* check loop */
|
|
|
|
loop_start = read_32bitLE(0x18,streamFile);
|
|
|
|
loop_end = read_32bitLE(0x1C,streamFile);
|
|
|
|
|
|
|
|
//todo should loop if loop_start > 0 and loop_end == -1
|
|
|
|
if ((loop_end == 0xFFFFFFFF) || (loop_start == 0 && loop_end == 0)) {
|
2017-11-10 22:22:04 +01:00
|
|
|
loop_flag = 0;
|
|
|
|
}
|
2018-06-10 21:08:06 +02:00
|
|
|
else {
|
2017-11-10 22:22:04 +01:00
|
|
|
loop_flag = 1;
|
|
|
|
}
|
|
|
|
|
2018-06-10 21:08:06 +02:00
|
|
|
channel_count = read_32bitLE(0x10,streamFile);
|
|
|
|
|
2008-07-20 18:34:01 +02:00
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
|
|
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
|
|
|
vgmstream->sample_rate = read_32bitLE(0x0C,streamFile);
|
|
|
|
|
2018-06-10 21:08:06 +02:00
|
|
|
//todo use proper flags
|
|
|
|
if (read_32bitLE(0x08,streamFile)!=0x10) {
|
|
|
|
vgmstream->coding_type = coding_PCM16LE;
|
|
|
|
vgmstream->num_samples = stream_size/2/vgmstream->channels;
|
|
|
|
} else {
|
|
|
|
vgmstream->coding_type = coding_PSX;
|
|
|
|
vgmstream->num_samples = ((stream_size-0x40)/16*28)/vgmstream->channels; //todo don't - 0x40?
|
2008-07-20 18:34:01 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 21:08:06 +02:00
|
|
|
vgmstream->interleave_block_size = read_32bitLE(0x14,streamFile); /* set even in mono */
|
2008-07-20 18:34:01 +02:00
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
vgmstream->meta_type = meta_PS2_SShd;
|
|
|
|
|
2018-06-10 21:08:06 +02:00
|
|
|
/* loops */
|
|
|
|
if (vgmstream->loop_flag) {
|
|
|
|
if ((loop_end*0x10*vgmstream->channels+0x800) == get_streamfile_size(streamFile)) {
|
|
|
|
/* full loop? */ //todo needed???
|
|
|
|
uint8_t testBuffer[0x10];
|
|
|
|
off_t readOffset = 0, loopEndOffset = 0;
|
2017-11-10 22:22:04 +01:00
|
|
|
|
2018-06-10 21:08:06 +02:00
|
|
|
readOffset = (off_t)get_streamfile_size(streamFile)-(4*vgmstream->interleave_block_size);
|
2017-11-10 22:22:04 +01:00
|
|
|
do {
|
2018-06-10 21:08:06 +02:00
|
|
|
readOffset += (off_t)read_streamfile(testBuffer,readOffset,0x10,streamFile);
|
2017-11-10 22:22:04 +01:00
|
|
|
|
|
|
|
if(testBuffer[0x01]==0x01) {
|
2018-06-10 21:08:06 +02:00
|
|
|
if(loopEndOffset==0)
|
|
|
|
loopEndOffset = readOffset-0x10;
|
2017-11-10 22:22:04 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (streamFile->get_offset(streamFile)<(int32_t)get_streamfile_size(streamFile));
|
|
|
|
|
|
|
|
vgmstream->loop_start_sample = 0;
|
2018-06-10 21:08:06 +02:00
|
|
|
vgmstream->loop_end_sample = (loopEndOffset/(vgmstream->interleave_block_size)*vgmstream->interleave_block_size)/16*28;
|
|
|
|
vgmstream->loop_end_sample += (loopEndOffset%vgmstream->interleave_block_size)/16*28;
|
2017-11-10 22:22:04 +01:00
|
|
|
vgmstream->loop_end_sample /=vgmstream->channels;
|
2018-06-10 21:08:06 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (loop_end <= vgmstream->num_samples) {
|
|
|
|
/* assume loops are samples */
|
|
|
|
vgmstream->loop_start_sample = loop_start;
|
|
|
|
vgmstream->loop_end_sample = loop_end;
|
2017-11-10 22:22:04 +01:00
|
|
|
} else {
|
2018-06-10 21:08:06 +02:00
|
|
|
/* assume loops are addresses (official definition) */ //todo use interleave instead of 0x10?
|
|
|
|
vgmstream->loop_start_sample = (loop_start*0x10)/16*28/vgmstream->channels;
|
|
|
|
vgmstream->loop_end_sample = (loop_end*0x10)/16*28/vgmstream->channels;
|
2017-11-10 22:22:04 +01:00
|
|
|
}
|
|
|
|
}
|
2008-07-20 18:34:01 +02:00
|
|
|
|
2018-06-10 21:08:06 +02:00
|
|
|
/* don't know why, but it does happen, in ps2 too :( */ //todo what
|
|
|
|
if (vgmstream->loop_end_sample > vgmstream->num_samples)
|
|
|
|
vgmstream->loop_end_sample = vgmstream->num_samples;
|
2017-11-10 22:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-10 21:08:06 +02:00
|
|
|
/* adjust */
|
|
|
|
start_offset = 0x28;
|
|
|
|
|
|
|
|
if ((stream_size * 2) == (get_streamfile_size(streamFile) - 0x18)) {
|
|
|
|
/* True Fortune (PS2) with weird stream size */ //todo try to move
|
|
|
|
stream_size = (read_32bitLE(0x24,streamFile) * 2) - 0x10;
|
|
|
|
vgmstream->num_samples = stream_size / 16 * 28 / vgmstream->channels;
|
2017-11-10 22:22:04 +01:00
|
|
|
}
|
2018-06-10 21:08:06 +02:00
|
|
|
else if(get_streamfile_size(streamFile) - read_32bitLE(0x24,streamFile) >= 0x800) {
|
|
|
|
/* Hack for files with start_offset = 0x800 (ex. Taisho Mononoke Ibunroku) */
|
|
|
|
start_offset = 0x800;
|
2017-11-10 22:22:04 +01:00
|
|
|
}
|
|
|
|
|
2018-06-10 21:08:06 +02:00
|
|
|
if (vgmstream->coding_type == coding_PSX && start_offset == 0x28) {
|
|
|
|
int i;
|
|
|
|
start_offset = 0x800;
|
2017-11-10 22:22:04 +01:00
|
|
|
|
2018-06-10 21:08:06 +02:00
|
|
|
for (i=0; i < 0x1f6; i += 4) {
|
|
|
|
if (read_32bitLE(0x28+(i*4),streamFile)!=0) {
|
|
|
|
start_offset = 0x28;
|
2017-11-10 22:22:04 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-10 21:08:06 +02:00
|
|
|
//todo should adjust num samples after changing start_offset and stream_size?
|
|
|
|
|
|
|
|
|
|
|
|
/* check if we got a real pcm by checking PS-ADPCM flags (ex: Clock Tower 3) */
|
|
|
|
//todo check format 0x02 instead
|
|
|
|
if (vgmstream->coding_type==coding_PCM16LE) {
|
|
|
|
uint8_t isPCM = 0;
|
|
|
|
off_t check_offset;
|
|
|
|
|
|
|
|
check_offset = start_offset;
|
|
|
|
do {
|
|
|
|
if (read_8bit(check_offset+1,streamFile)>7) {
|
2017-11-10 22:22:04 +01:00
|
|
|
isPCM=1;
|
|
|
|
break;
|
|
|
|
}
|
2018-06-10 21:08:06 +02:00
|
|
|
else {
|
2017-11-10 22:22:04 +01:00
|
|
|
check_offset+=0x10;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (check_offset<get_streamfile_size(streamFile));
|
|
|
|
|
2018-06-10 21:08:06 +02:00
|
|
|
if (!isPCM) {
|
2017-11-10 22:22:04 +01:00
|
|
|
vgmstream->num_samples=(get_streamfile_size(streamFile)-start_offset)/16*28/vgmstream->channels;
|
|
|
|
vgmstream->coding_type=coding_PSX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-20 18:34:01 +02:00
|
|
|
|
2018-06-10 21:08:06 +02:00
|
|
|
if (!vgmstream_open_stream(vgmstream,streamFile,start_offset))
|
|
|
|
goto fail;
|
2008-07-20 18:34:01 +02:00
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
2018-06-10 21:08:06 +02:00
|
|
|
close_vgmstream(vgmstream);
|
2008-07-20 18:34:01 +02:00
|
|
|
return NULL;
|
|
|
|
}
|