JOE: Remove padding at the end

This commit is contained in:
NicknineTheEagle 2019-08-06 22:13:25 +03:00
parent 47d7b5a0ed
commit c66a832301

View File

@ -1,13 +1,15 @@
#include "meta.h"
#include "../coding/coding.h"
static size_t joe_find_padding(STREAMFILE *streamFile, off_t start_offset, size_t data_size, int channels, size_t interleave);
/* .JOE - from Asobo Studio games [Up (PS2), Wall-E (PS2)] */
VGMSTREAM * init_vgmstream_ps2_joe(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL;
off_t start_offset;
int channel_count, loop_flag, sample_rate;
int32_t num_samples, loop_start = 0, loop_end = 0;
size_t file_size, data_size, unknown1, unknown2, interleave;
size_t file_size, data_size, unknown1, unknown2, interleave, padding_size;
/* checks */
@ -20,44 +22,43 @@ VGMSTREAM * init_vgmstream_ps2_joe(STREAMFILE *streamFile) {
unknown2 = read_32bitLE(0x0c,streamFile);
/* detect version */
if (data_size/2 == file_size - 0x10
&& unknown1 == 0x0045039A && unknown2 == 0x00108920) { /* Super Farm (PS2) */
if (data_size / 2 == file_size - 0x10
&& unknown1 == 0x0045039A && unknown2 == 0x00108920) { /* Super Farm (PS2) */
data_size = data_size / 2;
interleave = 0x4000;
start_offset = 0x10;
}
else if (data_size/2 == file_size - 0x10
&& unknown1 == 0xCCCCCCCC && unknown2 == 0xCCCCCCCC) { /* Sitting Ducks (PS2) */
} else if (data_size / 2 == file_size - 0x10
&& unknown1 == 0xCCCCCCCC && unknown2 == 0xCCCCCCCC) { /* Sitting Ducks (PS2) */
data_size = data_size / 2;
interleave = 0x8000;
start_offset = 0x10;
}
else if (data_size == file_size - 0x10
&& unknown1 == 0xCCCCCCCC && unknown2 == 0xCCCCCCCC) { /* The Mummy: The Animated Series (PS2) */
} else if (data_size == file_size - 0x10
&& unknown1 == 0xCCCCCCCC && unknown2 == 0xCCCCCCCC) { /* The Mummy: The Animated Series (PS2) */
interleave = 0x8000;
start_offset = 0x10;
}
else if (data_size == file_size - 0x4020) { /* CT Special Forces (PS2), and all games beyond */
} else if (data_size == file_size - 0x4020) { /* CT Special Forces (PS2), and all games beyond */
interleave = unknown1; /* always 0? */
if (!interleave)
interleave = 0x10;
start_offset = 0x4020; /* header padding contains garbage */
}
else {
} else {
goto fail;
}
//start_offset = file_size - data_size; /* also ok */
channel_count = 2;
loop_flag = 0;
sample_rate = read_32bitLE(0x00,streamFile);
/* the file's end is padded with either 0xcdcdcdcd or zeroes */
padding_size = joe_find_padding(streamFile, start_offset, data_size, channel_count, interleave);
if (padding_size == 0xFFFFFFFF)
goto fail;
data_size -= padding_size;
num_samples = ps_bytes_to_samples(data_size, channel_count);
loop_flag = ps_find_loop_offsets(streamFile, start_offset, data_size, channel_count, interleave,&loop_start, &loop_end);
/* most songs simply repeat except a few jingles (PS-ADPCM flags are always set) */
loop_flag = loop_flag && (num_samples > 20*sample_rate); /* in seconds */
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count,loop_flag);
if (!vgmstream) goto fail;
@ -66,6 +67,7 @@ VGMSTREAM * init_vgmstream_ps2_joe(STREAMFILE *streamFile) {
vgmstream->num_samples = num_samples;
vgmstream->loop_start_sample = loop_start;
vgmstream->loop_end_sample = loop_end;
vgmstream->stream_size = data_size;
vgmstream->coding_type = coding_PSX;
vgmstream->layout_type = layout_interleave;
@ -80,3 +82,38 @@ fail:
close_vgmstream(vgmstream);
return NULL;
}
static size_t joe_find_padding(STREAMFILE *streamFile, off_t start_offset, size_t data_size, int channels, size_t interleave) {
uint8_t flag;
off_t min_offset, offset;
size_t frame_size = 0x10;
size_t padding_size = 0;
size_t interleave_consumed = 0;
if (data_size == 0 || channels == 0 || (channels > 0 && interleave == 0))
return 0;
offset = start_offset + data_size - interleave * (channels - 1);
min_offset = start_offset;
while (offset > min_offset) {
offset -= frame_size;
flag = (read_32bitBE(offset + 0x00, streamFile) >> 16) & 0xFF;
if (flag == 0x03)
break;
padding_size += frame_size * channels;
/* skip other channels */
interleave_consumed += 0x10;
if (interleave_consumed == interleave) {
interleave_consumed = 0;
offset -= interleave * (channels - 1);
}
}
if (padding_size >= data_size)
return 0xFFFFFFFF;
return padding_size;
}