2010-09-17 20:32:18 +02:00
|
|
|
#include "meta.h"
|
|
|
|
#include "../coding/coding.h"
|
|
|
|
|
2017-02-04 23:26:13 +01:00
|
|
|
/* MSF - Sony's PS3 SDK format (MultiStream File) */
|
2020-10-12 23:31:15 +02:00
|
|
|
VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
|
|
|
|
VGMSTREAM* vgmstream = NULL;
|
2018-10-27 23:10:03 +02:00
|
|
|
off_t start_offset;
|
2017-02-04 23:26:13 +01:00
|
|
|
uint32_t data_size, loop_start = 0, loop_end = 0;
|
2019-03-12 00:38:48 +01:00
|
|
|
uint32_t codec, flags;
|
2020-11-06 17:43:29 +01:00
|
|
|
int loop_flag, channels, sample_rate;
|
2016-11-25 22:29:07 +01:00
|
|
|
|
2010-09-17 20:32:18 +02:00
|
|
|
|
2018-03-29 20:25:54 +02:00
|
|
|
/* checks */
|
2018-10-27 23:10:03 +02:00
|
|
|
/* .msf: standard
|
2019-12-22 20:03:01 +01:00
|
|
|
* .msa: Sonic & Sega All-Stars Racing (PS3)
|
2020-10-12 23:31:15 +02:00
|
|
|
* .at3: Silent Hill HD Collection (PS3), Z/X Zekkai no Crusade (PS3)
|
2021-12-06 13:30:42 +01:00
|
|
|
* .mp3: Darkstalkers Resurrection (PS3)
|
|
|
|
* .str: Pac-Man and the Ghostly Adventures (PS3) */
|
|
|
|
if (!check_extensions(sf,"msf,msa,at3,mp3,str"))
|
2018-03-29 20:25:54 +02:00
|
|
|
goto fail;
|
2010-09-26 10:25:43 +02:00
|
|
|
|
2018-10-27 23:10:03 +02:00
|
|
|
/* check header "MSF" + version-char, usually:
|
2020-11-06 17:43:29 +01:00
|
|
|
* 0x01, 0x02, 0x30="0", 0x35="5", 0x43="C" (last/most common version) */
|
2020-10-12 23:31:15 +02:00
|
|
|
if ((read_u32be(0x00,sf) & 0xffffff00) != 0x4D534600) /* "MSF\0" */
|
2019-03-12 00:38:48 +01:00
|
|
|
goto fail;
|
2017-02-04 23:26:13 +01:00
|
|
|
|
2019-03-12 00:38:48 +01:00
|
|
|
start_offset = 0x40;
|
2017-02-04 23:26:13 +01:00
|
|
|
|
2020-10-12 23:31:15 +02:00
|
|
|
codec = read_u32be(0x04,sf);
|
2020-11-06 17:43:29 +01:00
|
|
|
channels = read_s32be(0x08,sf);
|
2020-10-12 23:31:15 +02:00
|
|
|
data_size = read_u32be(0x0C,sf); /* without header */
|
2017-02-04 23:26:13 +01:00
|
|
|
if (data_size == 0xFFFFFFFF) /* unneeded? */
|
2020-10-12 23:31:15 +02:00
|
|
|
data_size = get_streamfile_size(sf) - start_offset;
|
|
|
|
sample_rate = read_s32be(0x10,sf);
|
2017-02-04 23:26:13 +01:00
|
|
|
|
|
|
|
/* byte flags, not in MSFv1 or v2
|
2017-03-24 16:37:14 +01:00
|
|
|
* 0x01/02/04/08: loop marker 0/1/2/3
|
2017-05-20 18:00:27 +02:00
|
|
|
* 0x10: resample options (force 44/48khz)
|
2020-11-06 17:43:29 +01:00
|
|
|
* 0x20: VBR MP3 source (encoded with min/max quality options, may end up being CBR)
|
2017-02-04 23:26:13 +01:00
|
|
|
* 0x40: joint stereo MP3 (apparently interleaved stereo for other formats)
|
|
|
|
* 0x80+: (none/reserved) */
|
2020-10-12 23:31:15 +02:00
|
|
|
flags = read_u32be(0x14,sf);
|
2017-03-24 16:37:14 +01:00
|
|
|
/* sometimes loop_start/end is set with flag 0x10, but from tests it only loops if 0x01/02 is set
|
|
|
|
* 0x10 often goes with 0x01 but not always (Castlevania HoD); Malicious PS3 uses flag 0x2 instead */
|
2020-11-06 17:43:29 +01:00
|
|
|
loop_flag = (flags != 0xffffffff) && ((flags & 0x01) || (flags & 0x02));
|
2017-02-04 23:26:13 +01:00
|
|
|
|
|
|
|
/* loop markers (marker N @ 0x18 + N*(4+4), but in practice only marker 0 is used) */
|
2016-11-25 22:29:07 +01:00
|
|
|
if (loop_flag) {
|
2020-10-12 23:31:15 +02:00
|
|
|
loop_start = read_u32be(0x18,sf);
|
|
|
|
loop_end = read_u32be(0x1C,sf); /* loop duration */
|
2017-02-04 23:26:13 +01:00
|
|
|
loop_end = loop_start + loop_end; /* usually equals data_size but not always */
|
2020-11-06 17:43:29 +01:00
|
|
|
if (loop_end > data_size) /* not seen */
|
2016-11-25 22:29:07 +01:00
|
|
|
loop_end = data_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-17 20:32:18 +02:00
|
|
|
/* build the VGMSTREAM */
|
2020-11-06 17:43:29 +01:00
|
|
|
vgmstream = allocate_vgmstream(channels, loop_flag);
|
2010-09-17 20:32:18 +02:00
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
2019-03-11 14:49:29 +01:00
|
|
|
vgmstream->meta_type = meta_MSF;
|
2019-03-12 00:38:48 +01:00
|
|
|
vgmstream->sample_rate = sample_rate;
|
|
|
|
if (vgmstream->sample_rate == 0) /* some MSFv1 (PS-ADPCM only?) [Megazone 23 - Aoi Garland (PS3)] */
|
|
|
|
vgmstream->sample_rate = 48000;
|
2010-09-17 20:32:18 +02:00
|
|
|
|
2019-03-12 00:38:48 +01:00
|
|
|
switch (codec) {
|
2020-11-06 17:43:29 +01:00
|
|
|
case 0x00: /* PCM (Big Endian) [MSEnc tests] */
|
2018-03-29 20:25:54 +02:00
|
|
|
case 0x01: { /* PCM (Little Endian) [Smash Cars (PS3)] */
|
2019-03-12 00:38:48 +01:00
|
|
|
vgmstream->coding_type = codec==0 ? coding_PCM16BE : coding_PCM16LE;
|
2019-12-22 20:03:01 +01:00
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
vgmstream->interleave_block_size = 0x02;
|
2017-02-04 23:26:13 +01:00
|
|
|
|
2020-11-06 17:43:29 +01:00
|
|
|
vgmstream->num_samples = pcm_bytes_to_samples(data_size, channels, 16);
|
2017-02-04 23:26:13 +01:00
|
|
|
if (loop_flag){
|
2020-11-06 17:43:29 +01:00
|
|
|
vgmstream->loop_start_sample = pcm_bytes_to_samples(loop_start, channels, 16);
|
|
|
|
vgmstream->loop_end_sample = pcm_bytes_to_samples(loop_end, channels, 16);
|
2010-09-26 10:25:43 +02:00
|
|
|
}
|
2017-02-04 23:26:13 +01:00
|
|
|
|
2010-09-26 10:25:43 +02:00
|
|
|
break;
|
2017-02-04 23:26:13 +01:00
|
|
|
}
|
|
|
|
|
2018-03-29 20:25:54 +02:00
|
|
|
case 0x02: { /* PCM 32 (Float) */
|
|
|
|
goto fail; /* probably unused/spec only */
|
2017-02-04 23:26:13 +01:00
|
|
|
}
|
|
|
|
|
2018-03-29 20:25:54 +02:00
|
|
|
case 0x03: { /* PS ADPCM [Smash Cars (PS3)] */
|
2017-02-04 23:26:13 +01:00
|
|
|
vgmstream->coding_type = coding_PSX;
|
2019-03-12 00:38:48 +01:00
|
|
|
vgmstream->layout_type = layout_interleave;
|
2017-02-04 23:26:13 +01:00
|
|
|
vgmstream->interleave_block_size = 0x10;
|
|
|
|
|
2020-11-06 17:43:29 +01:00
|
|
|
vgmstream->num_samples = ps_bytes_to_samples(data_size, channels);
|
2017-02-04 23:26:13 +01:00
|
|
|
if (loop_flag) {
|
2020-11-06 17:43:29 +01:00
|
|
|
vgmstream->loop_start_sample = ps_bytes_to_samples(loop_start,channels);
|
|
|
|
vgmstream->loop_end_sample = ps_bytes_to_samples(loop_end,channels);
|
2010-09-17 21:00:21 +02:00
|
|
|
}
|
2017-02-04 23:26:13 +01:00
|
|
|
|
2010-09-17 21:00:21 +02:00
|
|
|
break;
|
2017-02-04 23:26:13 +01:00
|
|
|
}
|
2016-11-25 22:29:07 +01:00
|
|
|
|
2017-02-04 23:26:13 +01:00
|
|
|
#ifdef VGM_USE_FFMPEG
|
2018-03-29 20:25:54 +02:00
|
|
|
case 0x04: /* ATRAC3 low (66 kbps, frame size 96, Joint Stereo) [Silent Hill HD (PS3)] */
|
|
|
|
case 0x05: /* ATRAC3 mid (105 kbps, frame size 152) [Atelier Rorona (PS3)] */
|
|
|
|
case 0x06: { /* ATRAC3 high (132 kbps, frame size 192) [Tekken Tag Tournament HD (PS3)] */
|
2019-08-26 22:58:43 +02:00
|
|
|
int block_align, encoder_delay;
|
2017-02-04 23:26:13 +01:00
|
|
|
|
2017-05-20 18:00:27 +02:00
|
|
|
/* MSF skip samples: from tests with MSEnc and real files (ex. TTT2 eddy.msf v43, v01 demos) seems like 1162 is consistent.
|
|
|
|
* Atelier Rorona bt_normal01 needs it to properly skip the beginning garbage but usually doesn't matter.
|
|
|
|
* (note that encoder may add a fade-in with looping/resampling enabled but should be skipped) */
|
2019-08-26 22:58:43 +02:00
|
|
|
encoder_delay = 1024 + 69*2;
|
|
|
|
block_align = (codec==4 ? 0x60 : (codec==5 ? 0x98 : 0xC0)) * vgmstream->channels;
|
|
|
|
vgmstream->num_samples = atrac3_bytes_to_samples(data_size, block_align) - encoder_delay;
|
2021-07-29 17:08:30 +02:00
|
|
|
if (vgmstream->sample_rate == -1) /* some MSFv1 (Digi World SP) */
|
2018-10-27 23:10:03 +02:00
|
|
|
vgmstream->sample_rate = 44100; /* voice tracks seems to use 44khz, not sure about other tracks */
|
2017-02-04 23:26:13 +01:00
|
|
|
|
2020-10-12 23:31:15 +02:00
|
|
|
vgmstream->codec_data = init_ffmpeg_atrac3_raw(sf, start_offset,data_size, vgmstream->num_samples,vgmstream->channels,vgmstream->sample_rate, block_align, encoder_delay);
|
2019-08-26 22:58:43 +02:00
|
|
|
if (!vgmstream->codec_data) goto fail;
|
2016-11-25 22:29:07 +01:00
|
|
|
vgmstream->coding_type = coding_FFmpeg;
|
|
|
|
vgmstream->layout_type = layout_none;
|
2016-12-10 15:53:29 +01:00
|
|
|
|
2019-08-26 22:58:43 +02:00
|
|
|
/* MSF loop/sample values are offsets so trickier to adjust but this seems correct */
|
2017-02-04 23:26:13 +01:00
|
|
|
if (loop_flag) {
|
2019-08-26 22:58:43 +02:00
|
|
|
/* set offset samples (offset 0 jumps to sample 0 > pre-applied delay, and offset end loops after sample end > adjusted delay) */
|
|
|
|
vgmstream->loop_start_sample = atrac3_bytes_to_samples(loop_start, block_align); //- encoder_delay
|
|
|
|
vgmstream->loop_end_sample = atrac3_bytes_to_samples(loop_end, block_align) - encoder_delay;
|
2016-11-25 22:29:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2017-02-04 23:26:13 +01:00
|
|
|
}
|
2016-11-25 22:29:07 +01:00
|
|
|
#endif
|
2019-12-22 20:03:01 +01:00
|
|
|
#if defined(VGM_USE_MPEG)
|
2020-11-06 17:43:29 +01:00
|
|
|
case 0x07: { /* MPEG (LAME MP3) [Dengeki Bunko Fighting Climax (PS3), Asura's Wrath (PS3)-vbr] */
|
|
|
|
int is_vbr = (flags & 0x20); /* must calc samples/loop offsets manually */
|
2018-03-29 20:25:54 +02:00
|
|
|
|
2020-10-12 23:31:15 +02:00
|
|
|
vgmstream->codec_data = init_mpeg(sf, start_offset, &vgmstream->coding_type, vgmstream->channels);
|
2020-07-17 19:42:05 +02:00
|
|
|
if (!vgmstream->codec_data) goto fail;
|
2016-11-25 22:29:07 +01:00
|
|
|
vgmstream->layout_type = layout_none;
|
|
|
|
|
2020-11-06 17:43:29 +01:00
|
|
|
vgmstream->num_samples = mpeg_get_samples_clean(sf, start_offset, data_size, &loop_start, &loop_end, is_vbr);
|
|
|
|
vgmstream->loop_start_sample = loop_start;
|
|
|
|
vgmstream->loop_end_sample = loop_end;
|
|
|
|
/* MPEG here seems stripped from ID3/Xing headers, loops are frame offsets */
|
2016-11-25 22:29:07 +01:00
|
|
|
|
2018-03-29 20:25:54 +02:00
|
|
|
/* encoder delay varies between 1152 (1f), 528, 576, etc; probably not actually skipped */
|
2016-11-25 22:29:07 +01:00
|
|
|
break;
|
2017-02-04 23:26:13 +01:00
|
|
|
}
|
2019-12-22 20:03:01 +01:00
|
|
|
#elif defined(VGM_USE_FFMPEG)
|
2020-11-06 17:43:29 +01:00
|
|
|
case 0x07: { /* MPEG (LAME MP3) [Dengeki Bunko Fighting Climax (PS3), Asura's Wrath (PS3)-vbr] */
|
2021-07-23 18:55:03 +02:00
|
|
|
int is_vbr = (flags & 0x20); /* must calc samples/loop offsets manually */
|
2017-02-04 23:26:13 +01:00
|
|
|
|
2021-07-23 18:55:03 +02:00
|
|
|
vgmstream->codec_data = init_ffmpeg_offset(sf, start_offset, 0);;
|
|
|
|
if (!vgmstream->codec_data) goto fail;
|
2019-12-22 20:03:01 +01:00
|
|
|
vgmstream->coding_type = coding_FFmpeg;
|
2017-07-29 13:05:23 +02:00
|
|
|
vgmstream->layout_type = layout_none;
|
2018-03-29 20:25:54 +02:00
|
|
|
|
2021-07-23 18:55:03 +02:00
|
|
|
vgmstream->num_samples = mpeg_get_samples_clean(sf, start_offset, data_size, &loop_start, &loop_end, is_vbr);
|
|
|
|
vgmstream->loop_start_sample = loop_start;
|
|
|
|
vgmstream->loop_end_sample = loop_end;
|
2010-09-17 20:32:18 +02:00
|
|
|
break;
|
2017-02-04 23:26:13 +01:00
|
|
|
}
|
2010-09-17 20:32:18 +02:00
|
|
|
#endif
|
2017-02-04 23:26:13 +01:00
|
|
|
|
2018-03-29 20:25:54 +02:00
|
|
|
default: /* 0x08+: not defined */
|
2010-09-17 20:32:18 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-06 17:43:29 +01:00
|
|
|
if (!vgmstream_open_stream(vgmstream, sf, start_offset))
|
2017-02-04 23:26:13 +01:00
|
|
|
goto fail;
|
2010-09-17 20:32:18 +02:00
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
2017-02-04 23:26:13 +01:00
|
|
|
close_vgmstream(vgmstream);
|
2010-09-17 20:32:18 +02:00
|
|
|
return NULL;
|
|
|
|
}
|