2010-04-10 14:06:25 +02:00
|
|
|
#include "meta.h"
|
2017-06-25 02:10:13 +02:00
|
|
|
#include "../coding/coding.h"
|
2010-04-10 14:06:25 +02:00
|
|
|
|
2021-09-12 20:02:52 +02:00
|
|
|
/* P3D - from Radical's Prototype 1/2 (PC/PS3/X360), Spider-Man 4 Beta (X360) */
|
|
|
|
VGMSTREAM* init_vgmstream_p3d(STREAMFILE* sf) {
|
|
|
|
VGMSTREAM* vgmstream = NULL;
|
|
|
|
off_t start_offset, offset, name_offset = 0;
|
2017-06-25 02:10:13 +02:00
|
|
|
size_t header_size, file_size, data_size;
|
2021-09-12 20:02:52 +02:00
|
|
|
uint32_t xma2_offset = 0, xma2_size = 0;
|
|
|
|
int loop_flag, channels, sample_rate, codec;
|
|
|
|
int i, name_count, text_len, block_size = 0, num_samples;
|
|
|
|
uint32_t (*read_u32)(off_t,STREAMFILE*) = NULL;
|
2017-06-25 02:10:13 +02:00
|
|
|
|
2010-04-10 14:06:25 +02:00
|
|
|
|
Fix XMA gapless/looping/samples
fixes: standard, wem, xwc, xwb, xnb, xwx, rak, pk, txth, genh, seg, rsd, past, p3d, nub-xma, gtd, gsp, fsb, eaac, cxs, awc, aac
2018-11-18 17:01:31 +01:00
|
|
|
/* checks */
|
2021-09-12 20:02:52 +02:00
|
|
|
if (!is_id32be(0x00,sf, "P3D\xFF") && /* LE: PC */
|
|
|
|
!is_id32le(0x00,sf, "P3D\xFF")) /* BE: PS3, X360 */
|
2017-06-25 02:10:13 +02:00
|
|
|
goto fail;
|
2021-09-12 20:02:52 +02:00
|
|
|
if (!check_extensions(sf,"p3d"))
|
2017-06-25 02:10:13 +02:00
|
|
|
goto fail;
|
|
|
|
|
2021-09-12 20:02:52 +02:00
|
|
|
read_u32 = guess_endianness32bit(0x04,sf) ? read_u32be : read_u32le;
|
|
|
|
file_size = get_streamfile_size(sf);
|
2010-04-10 14:06:25 +02:00
|
|
|
|
2017-06-25 02:10:13 +02:00
|
|
|
/* base header */
|
2021-09-12 20:02:52 +02:00
|
|
|
header_size = read_u32(0x04,sf);
|
|
|
|
if (header_size != 0x0C) goto fail;
|
|
|
|
if (read_u32(0x08,sf) != file_size) goto fail;
|
|
|
|
if (read_u32(0x0C,sf) != 0xFE000000) goto fail; /* fixed */
|
|
|
|
if (read_u32(0x10,sf) + header_size != file_size) goto fail;
|
|
|
|
if (read_u32(0x14,sf) + header_size != file_size) goto fail; /* body size again */
|
|
|
|
if (read_u32(0x18,sf) != 0x0000000A) goto fail; /* fixed */
|
2010-04-10 14:06:25 +02:00
|
|
|
|
2017-06-25 02:10:13 +02:00
|
|
|
/* header text */
|
2021-09-12 20:02:52 +02:00
|
|
|
offset = 0x1C;
|
|
|
|
text_len = read_u32(offset,sf);
|
|
|
|
if (text_len != 9) goto fail;
|
|
|
|
offset += 0x04;
|
2017-06-25 02:10:13 +02:00
|
|
|
|
2017-06-25 02:21:25 +02:00
|
|
|
/* check the type as P3D is just a generic container used in Radical's games */
|
2021-09-12 20:02:52 +02:00
|
|
|
if (!is_id64be(offset+0x00,sf, "AudioFil") || read_u16be(offset+0x08,sf) != 0x6500) /* "AudioFile\0" */
|
|
|
|
goto fail;
|
|
|
|
offset += text_len + 0x01;
|
2017-06-25 02:10:13 +02:00
|
|
|
|
|
|
|
/* file names: always 2 (repeated); but if it's 3 there is an extra string later */
|
2021-09-12 20:02:52 +02:00
|
|
|
name_count = read_u32(offset,sf);
|
2017-06-25 02:10:13 +02:00
|
|
|
if (name_count != 2 && name_count != 3) goto fail; /* 2: Prototype1, 3: Prototype2 */
|
2021-09-12 20:02:52 +02:00
|
|
|
offset += 0x04;
|
2017-06-25 02:10:13 +02:00
|
|
|
|
2017-08-12 11:46:28 +02:00
|
|
|
/* skip names */
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
if (!name_offset)
|
2021-09-12 20:02:52 +02:00
|
|
|
name_offset = offset + 0x04;
|
|
|
|
text_len = read_u32(offset,sf) + 1; /* null-terminated */
|
|
|
|
offset += 0x04 + text_len;
|
2010-04-10 14:06:25 +02:00
|
|
|
}
|
2017-06-25 02:10:13 +02:00
|
|
|
|
2010-04-10 14:06:25 +02:00
|
|
|
/* info count? */
|
2021-09-12 20:02:52 +02:00
|
|
|
if (0x01 != read_u32(offset,sf)) goto fail;
|
|
|
|
offset += 0x04;
|
2017-06-25 02:10:13 +02:00
|
|
|
|
|
|
|
/* next string can be used as a codec id */
|
2021-09-12 20:02:52 +02:00
|
|
|
text_len = read_u32(offset,sf);
|
|
|
|
codec = read_u32be(offset+0x04,sf);
|
|
|
|
offset += 0x04 + text_len + 0x01;
|
2017-06-25 02:10:13 +02:00
|
|
|
|
|
|
|
/* extra "Music" string in Prototype 2 */
|
|
|
|
if (name_count == 3) {
|
2021-09-12 20:02:52 +02:00
|
|
|
text_len = read_u32(offset,sf) + 1; /* null-terminated */
|
|
|
|
offset += 0x04 + text_len;
|
2010-04-10 14:06:25 +02:00
|
|
|
}
|
|
|
|
|
2021-09-12 20:02:52 +02:00
|
|
|
loop_flag = 0;
|
2017-06-25 02:10:13 +02:00
|
|
|
|
|
|
|
/* sub-header per codec */
|
|
|
|
switch(codec) {
|
|
|
|
case 0x72616470: /* "radp" (PC) */
|
2021-09-12 20:02:52 +02:00
|
|
|
if (!is_id32be(offset,sf, "RADP"))
|
|
|
|
goto fail;
|
|
|
|
offset += 0x04;
|
|
|
|
|
|
|
|
channels = read_u32(offset+0x00,sf);
|
|
|
|
sample_rate = read_u32(offset+0x04,sf);
|
2017-06-25 02:10:13 +02:00
|
|
|
/* 0x08: ? (0x0F) */
|
2021-09-12 20:02:52 +02:00
|
|
|
data_size = read_u32(offset+0x0c,sf);
|
|
|
|
block_size = 0x14;
|
|
|
|
|
|
|
|
num_samples = data_size / block_size / channels * 32;
|
|
|
|
start_offset = offset + 0x10;
|
2017-06-25 02:10:13 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x6D703300: /* "mp3\0" (PS3) */
|
2021-09-12 20:02:52 +02:00
|
|
|
if ((read_u32be(offset,sf) & 0xFFFFFF00) != get_id32be("mp3\0"))
|
|
|
|
goto fail;
|
|
|
|
offset += 0x03;
|
|
|
|
|
2017-06-25 02:10:13 +02:00
|
|
|
/* all fields LE even though the prev parts were BE */
|
2021-09-12 20:02:52 +02:00
|
|
|
sample_rate = read_s32le(offset+0x00,sf);
|
2017-06-25 02:10:13 +02:00
|
|
|
/* 0x04: mp3 sample rate (ex. @0x00 is 47999 and @0x04 is 48000) */
|
2021-09-12 20:02:52 +02:00
|
|
|
num_samples = read_s32le(offset+0x08,sf);
|
|
|
|
data_size = read_u32le(offset+0x0c,sf);
|
|
|
|
channels = read_s32le(offset+0x10,sf);
|
|
|
|
block_size = read_u32le(offset+0x14,sf);
|
|
|
|
|
|
|
|
num_samples = num_samples / channels; /* total samples */
|
|
|
|
start_offset = offset + 0x18;
|
2017-06-25 02:10:13 +02:00
|
|
|
break;
|
|
|
|
|
2021-09-12 20:02:52 +02:00
|
|
|
case 0x786D6100: { /* "xma\0" (X360) */
|
|
|
|
uint32_t seek_size;
|
|
|
|
|
|
|
|
if (!is_id32be(offset,sf, "XMA2"))
|
|
|
|
goto fail;
|
|
|
|
offset += 0x04;
|
|
|
|
|
|
|
|
xma2_size = read_u32be(offset+0x00,sf);
|
|
|
|
seek_size = read_u32be(offset+0x04,sf);
|
|
|
|
data_size = read_u32be(offset+0x08,sf);
|
|
|
|
/* 0x0c: ? */
|
|
|
|
xma2_offset = offset+0x10;
|
|
|
|
if (!read_u8(xma2_offset+0x00, sf)) /* needs "xma2" chunk (Spider-Man 4 beta has multi-streams) */
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* loops never set */
|
|
|
|
xma2_parse_xma2_chunk(sf, xma2_offset, &channels, &sample_rate, &loop_flag, &num_samples, NULL, NULL);
|
|
|
|
|
|
|
|
start_offset = offset + 0x10 + xma2_size + seek_size;
|
2017-06-25 02:10:13 +02:00
|
|
|
break;
|
2021-09-12 20:02:52 +02:00
|
|
|
}
|
2017-06-25 02:10:13 +02:00
|
|
|
|
|
|
|
default:
|
2021-09-12 20:02:52 +02:00
|
|
|
vgm_logi("P3D: unknown codec 0x%04x\n", codec);
|
2017-06-25 02:10:13 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2021-09-12 20:02:52 +02:00
|
|
|
if (start_offset + data_size != file_size)
|
|
|
|
goto fail;
|
|
|
|
|
2017-06-25 02:10:13 +02:00
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
2021-09-12 20:02:52 +02:00
|
|
|
vgmstream = allocate_vgmstream(channels, loop_flag);
|
2010-04-10 14:06:25 +02:00
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
2021-09-12 20:02:52 +02:00
|
|
|
vgmstream->meta_type = meta_P3D;
|
2010-04-10 14:06:25 +02:00
|
|
|
vgmstream->sample_rate = sample_rate;
|
2017-06-25 02:10:13 +02:00
|
|
|
vgmstream->num_samples = num_samples;
|
2017-08-12 11:46:28 +02:00
|
|
|
if (name_offset)
|
2021-09-12 20:02:52 +02:00
|
|
|
read_string(vgmstream->stream_name,STREAM_NAME_SIZE, name_offset,sf);
|
2010-04-10 14:06:25 +02:00
|
|
|
|
2017-06-25 02:10:13 +02:00
|
|
|
switch(codec) {
|
|
|
|
case 0x72616470: /* "radp" (PC) */
|
|
|
|
vgmstream->coding_type = coding_RAD_IMA_mono;
|
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
vgmstream->interleave_block_size = block_size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef VGM_USE_MPEG
|
|
|
|
case 0x6D703300: { /* "mp3\0" (PS3) */
|
2017-12-17 19:25:10 +01:00
|
|
|
mpeg_custom_config cfg = {0};
|
2017-06-25 02:10:13 +02:00
|
|
|
|
2017-07-29 23:53:45 +02:00
|
|
|
cfg.interleave = 0x400;
|
2017-12-17 19:25:10 +01:00
|
|
|
cfg.data_size = data_size;
|
2017-07-29 23:53:45 +02:00
|
|
|
/* block_size * 3 = frame size (0x60*3=0x120 or 0x40*3=0xC0) but doesn't seem to have any significance) */
|
2017-06-25 02:10:13 +02:00
|
|
|
|
2021-09-12 20:02:52 +02:00
|
|
|
vgmstream->codec_data = init_mpeg_custom(sf, start_offset, &vgmstream->coding_type, vgmstream->channels, MPEG_P3D, &cfg);
|
2017-07-29 23:14:04 +02:00
|
|
|
if (!vgmstream->codec_data) goto fail;
|
2017-12-17 19:25:10 +01:00
|
|
|
vgmstream->layout_type = layout_none;
|
2017-07-29 23:14:04 +02:00
|
|
|
break;
|
2010-04-10 14:06:25 +02:00
|
|
|
}
|
2017-06-25 02:10:13 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef VGM_USE_FFMPEG
|
|
|
|
case 0x786D6100: { /* "xma\0" (X360) */
|
|
|
|
uint8_t buf[0x100];
|
|
|
|
size_t bytes;
|
|
|
|
|
2021-09-12 20:02:52 +02:00
|
|
|
//TODO: some in Spider-Man 4 beta use 18ch but ffmpeg supports max 16ch XMA2
|
|
|
|
bytes = ffmpeg_make_riff_xma2_from_xma2_chunk(buf, sizeof(buf), xma2_offset, xma2_size, data_size, sf);
|
|
|
|
vgmstream->codec_data = init_ffmpeg_header_offset(sf, buf,bytes, start_offset,data_size);
|
|
|
|
if (!vgmstream->codec_data) goto fail;
|
2017-06-25 02:10:13 +02:00
|
|
|
vgmstream->coding_type = coding_FFmpeg;
|
|
|
|
vgmstream->layout_type = layout_none;
|
Fix XMA gapless/looping/samples
fixes: standard, wem, xwc, xwb, xnb, xwx, rak, pk, txth, genh, seg, rsd, past, p3d, nub-xma, gtd, gsp, fsb, eaac, cxs, awc, aac
2018-11-18 17:01:31 +01:00
|
|
|
|
2021-09-12 20:02:52 +02:00
|
|
|
xma_fix_raw_samples(vgmstream, sf, start_offset, data_size, 0, 1,1); /* samples needs adjustment */
|
2017-06-25 02:10:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
2021-09-12 20:02:52 +02:00
|
|
|
vgm_logi("P3D: unknown codec 0x%04x\n", codec);
|
2017-06-25 02:10:13 +02:00
|
|
|
goto fail;
|
2010-04-10 14:06:25 +02:00
|
|
|
}
|
2017-06-25 02:10:13 +02:00
|
|
|
|
2021-09-12 20:02:52 +02:00
|
|
|
if (!vgmstream_open_stream(vgmstream, sf, start_offset))
|
2017-06-25 02:10:13 +02:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return vgmstream;
|
2010-04-10 14:06:25 +02:00
|
|
|
|
|
|
|
fail:
|
2017-06-25 02:10:13 +02:00
|
|
|
close_vgmstream(vgmstream);
|
2010-04-10 14:06:25 +02:00
|
|
|
return NULL;
|
|
|
|
}
|