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) */
|
2010-09-17 20:32:18 +02:00
|
|
|
VGMSTREAM * init_vgmstream_ps3_msf(STREAMFILE *streamFile) {
|
|
|
|
VGMSTREAM * vgmstream = NULL;
|
2016-11-25 22:29:07 +01:00
|
|
|
off_t start_offset, header_offset = 0;
|
2017-02-04 23:26:13 +01:00
|
|
|
uint32_t data_size, loop_start = 0, loop_end = 0;
|
|
|
|
uint32_t id, codec_id, flags;
|
|
|
|
int loop_flag = 0, channel_count;
|
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 */
|
|
|
|
/* .msf: standard, .at3: Silent Hill HD Collection, .mp3: Darkstalkers Resurrection */
|
|
|
|
if (!check_extensions(streamFile,"msf,at3,mp3"))
|
|
|
|
goto fail;
|
2010-09-26 10:25:43 +02:00
|
|
|
|
2018-03-29 20:25:54 +02:00
|
|
|
/* "WMSF" variation with a mini header over the MSFC header [Dai-2-Ji Super Robot Generations (PS3)] */
|
2016-11-25 22:29:07 +01:00
|
|
|
if (read_32bitBE(0x00,streamFile) == 0x574D5346) {
|
|
|
|
header_offset = 0x10;
|
|
|
|
}
|
2017-02-04 23:26:13 +01:00
|
|
|
start_offset = header_offset+0x40; /* MSF header is always 0x40 */
|
|
|
|
|
|
|
|
/* check header "MSF" + version-char
|
2017-03-24 16:37:14 +01:00
|
|
|
* usually "MSF\0\1", "MSF\0\2", "MSF0"(\3\0), "MSF5"(\3\5), "MSFC"(\4\3) (last/common version) */
|
2017-02-04 23:26:13 +01:00
|
|
|
id = read_32bitBE(header_offset+0x00,streamFile);
|
|
|
|
if ((id & 0xffffff00) != 0x4D534600) goto fail;
|
|
|
|
|
|
|
|
|
|
|
|
codec_id = read_32bitBE(header_offset+0x04,streamFile);
|
|
|
|
channel_count = read_32bitBE(header_offset+0x08,streamFile);
|
|
|
|
data_size = read_32bitBE(header_offset+0x0C,streamFile); /* without header */
|
|
|
|
if (data_size == 0xFFFFFFFF) /* unneeded? */
|
|
|
|
data_size = get_streamfile_size(streamFile) - start_offset;
|
|
|
|
|
|
|
|
/* 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)
|
2018-03-29 20:25:54 +02:00
|
|
|
* 0x20: VBR MP3 source (changed into simplified 0x1a1 CBR)
|
2017-02-04 23:26:13 +01:00
|
|
|
* 0x40: joint stereo MP3 (apparently interleaved stereo for other formats)
|
|
|
|
* 0x80+: (none/reserved) */
|
|
|
|
flags = read_32bitBE(header_offset+0x14,streamFile);
|
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 */
|
|
|
|
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) {
|
2017-02-04 23:26:13 +01:00
|
|
|
loop_start = read_32bitBE(header_offset+0x18,streamFile);
|
|
|
|
loop_end = read_32bitBE(header_offset+0x1C,streamFile); /* loop duration */
|
|
|
|
loop_end = loop_start + loop_end; /* usually equals data_size but not always */
|
|
|
|
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 */
|
|
|
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
2018-03-29 20:25:54 +02:00
|
|
|
/* sample rate hack for strange MSFv1 files that don't have it */
|
2017-02-04 23:26:13 +01:00
|
|
|
vgmstream->sample_rate = read_32bitBE(header_offset+0x10,streamFile);
|
|
|
|
if (vgmstream->sample_rate == 0x00000000) /* PS ADPCM only? */
|
2010-11-05 03:14:53 +01:00
|
|
|
vgmstream->sample_rate = 48000;
|
2016-11-25 22:29:07 +01:00
|
|
|
|
|
|
|
vgmstream->meta_type = meta_PS3_MSF;
|
2010-09-17 20:32:18 +02:00
|
|
|
|
|
|
|
switch (codec_id) {
|
2018-03-29 20:25:54 +02:00
|
|
|
case 0x00: /* PCM (Big Endian) */
|
|
|
|
case 0x01: { /* PCM (Little Endian) [Smash Cars (PS3)] */
|
2017-02-04 23:26:13 +01:00
|
|
|
vgmstream->coding_type = codec_id==0 ? coding_PCM16BE : coding_PCM16LE;
|
|
|
|
vgmstream->layout_type = channel_count == 1 ? layout_none : layout_interleave;
|
|
|
|
vgmstream->interleave_block_size = 2;
|
|
|
|
|
2018-03-29 20:25:54 +02:00
|
|
|
vgmstream->num_samples = pcm_bytes_to_samples(data_size, channel_count,16);
|
2017-02-04 23:26:13 +01:00
|
|
|
if (loop_flag){
|
2018-03-29 20:25:54 +02:00
|
|
|
vgmstream->loop_start_sample = pcm_bytes_to_samples(loop_start, channel_count,16);
|
|
|
|
vgmstream->loop_end_sample = pcm_bytes_to_samples(loop_end, channel_count,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;
|
|
|
|
vgmstream->layout_type = channel_count == 1 ? layout_none : layout_interleave;
|
|
|
|
vgmstream->interleave_block_size = 0x10;
|
|
|
|
|
2018-03-29 20:25:54 +02:00
|
|
|
vgmstream->num_samples = ps_bytes_to_samples(data_size, channel_count);
|
2017-02-04 23:26:13 +01:00
|
|
|
if (loop_flag) {
|
2018-03-29 20:25:54 +02:00
|
|
|
vgmstream->loop_start_sample = ps_bytes_to_samples(loop_start,channel_count);
|
|
|
|
vgmstream->loop_end_sample = ps_bytes_to_samples(loop_end,channel_count);
|
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)] */
|
2017-02-04 23:26:13 +01:00
|
|
|
ffmpeg_codec_data *ffmpeg_data = NULL;
|
|
|
|
uint8_t buf[100];
|
2017-05-20 18:00:27 +02:00
|
|
|
int32_t bytes, block_size, encoder_delay, joint_stereo;
|
2017-02-04 23:26:13 +01:00
|
|
|
|
2017-04-07 21:18:07 +02:00
|
|
|
block_size = (codec_id==4 ? 0x60 : (codec_id==5 ? 0x98 : 0xC0)) * vgmstream->channels;
|
2017-05-20 18:00:27 +02:00
|
|
|
joint_stereo = (codec_id==4); /* interleaved joint stereo (ch must be even) */
|
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) */
|
|
|
|
encoder_delay = 1162;
|
|
|
|
vgmstream->num_samples = atrac3_bytes_to_samples(data_size, block_size) - encoder_delay;
|
2017-02-04 23:26:13 +01:00
|
|
|
if (vgmstream->sample_rate==0xFFFFFFFF) /* some MSFv1 (Digi World SP) */
|
|
|
|
vgmstream->sample_rate = 44100;//voice tracks seems to use 44khz, not sure about other tracks
|
|
|
|
|
|
|
|
bytes = ffmpeg_make_riff_atrac3(buf, 100, vgmstream->num_samples, data_size, vgmstream->channels, vgmstream->sample_rate, block_size, joint_stereo, encoder_delay);
|
|
|
|
if (bytes <= 0) goto fail;
|
|
|
|
|
|
|
|
ffmpeg_data = init_ffmpeg_header_offset(streamFile, buf,bytes, start_offset,data_size);
|
|
|
|
if (!ffmpeg_data) goto fail;
|
|
|
|
vgmstream->codec_data = ffmpeg_data;
|
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
|
|
|
|
2017-05-20 18:00:27 +02:00
|
|
|
|
|
|
|
/* manually set skip_samples if FFmpeg didn't do it */
|
|
|
|
if (ffmpeg_data->skipSamples <= 0) {
|
|
|
|
ffmpeg_set_skip_samples(ffmpeg_data, encoder_delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* MSF loop/sample values are offsets so trickier to adjust the skip_samples but this seems correct */
|
2017-02-04 23:26:13 +01:00
|
|
|
if (loop_flag) {
|
2017-05-20 18:00:27 +02:00
|
|
|
vgmstream->loop_start_sample = atrac3_bytes_to_samples(loop_start, block_size) /* - encoder_delay*/;
|
|
|
|
vgmstream->loop_end_sample = atrac3_bytes_to_samples(loop_end, block_size) - 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
|
2018-03-29 20:25:54 +02:00
|
|
|
#if defined(VGM_USE_FFMPEG) && !defined(VGM_USE_MPEG)
|
|
|
|
case 0x07: { /* MPEG (CBR LAME MP3) [Dengeki Bunko Fighting Climax (PS3)] */
|
|
|
|
ffmpeg_codec_data *ffmpeg_data = NULL;
|
|
|
|
|
|
|
|
ffmpeg_data = init_ffmpeg_offset(streamFile, start_offset, streamFile->get_size(streamFile) );
|
2016-11-25 22:29:07 +01:00
|
|
|
if ( !ffmpeg_data ) goto fail;
|
2017-02-04 23:26:13 +01:00
|
|
|
vgmstream->codec_data = ffmpeg_data;
|
2016-11-25 22:29:07 +01:00
|
|
|
vgmstream->coding_type = coding_FFmpeg;
|
|
|
|
vgmstream->layout_type = layout_none;
|
|
|
|
|
|
|
|
vgmstream->num_samples = (int64_t)data_size * ffmpeg_data->sampleRate * 8 / ffmpeg_data->bitrate;
|
|
|
|
if (loop_flag) {
|
|
|
|
vgmstream->loop_start_sample = (int64_t)loop_start * ffmpeg_data->sampleRate * 8 / ffmpeg_data->bitrate;
|
|
|
|
vgmstream->loop_end_sample = (int64_t)loop_end * ffmpeg_data->sampleRate * 8 / ffmpeg_data->bitrate;
|
2018-03-29 20:25:54 +02:00
|
|
|
/* loops are always CBR frame beginnings */
|
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
|
|
|
}
|
2016-11-25 22:29:07 +01:00
|
|
|
#endif
|
2018-03-29 20:25:54 +02:00
|
|
|
#ifdef VGM_USE_MPEG
|
|
|
|
case 0x07: { /* MPEG (CBR LAME MP3) []Dengeki Bunko Fighting Climax (PS3) */
|
2017-02-04 23:26:13 +01:00
|
|
|
mpeg_codec_data *mpeg_data = NULL;
|
|
|
|
|
2018-03-29 20:25:54 +02:00
|
|
|
mpeg_data = init_mpeg(streamFile, start_offset, &vgmstream->coding_type, vgmstream->channels);
|
2017-02-04 23:26:13 +01:00
|
|
|
if (!mpeg_data) goto fail;
|
|
|
|
vgmstream->codec_data = mpeg_data;
|
2017-07-29 13:05:23 +02:00
|
|
|
vgmstream->layout_type = layout_none;
|
2018-03-29 20:25:54 +02:00
|
|
|
|
2017-02-18 00:17:38 +01:00
|
|
|
vgmstream->num_samples = mpeg_bytes_to_samples(data_size, mpeg_data);
|
2017-02-04 23:26:13 +01:00
|
|
|
if (loop_flag) {
|
2017-02-18 00:17:38 +01:00
|
|
|
vgmstream->loop_start_sample = mpeg_bytes_to_samples(loop_start, mpeg_data);
|
|
|
|
vgmstream->loop_end_sample = mpeg_bytes_to_samples(loop_end, mpeg_data);
|
2018-03-29 20:25:54 +02:00
|
|
|
/* loops are always CBR frame beginnings */
|
2010-09-17 20:32:18 +02:00
|
|
|
}
|
2017-02-04 23:26:13 +01:00
|
|
|
|
2018-03-29 20:25:54 +02:00
|
|
|
/* encoder delay varies between 1152 (1f), 528, 576, etc; probably not actually skipped */
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-04 23:26:13 +01:00
|
|
|
if (!vgmstream_open_stream(vgmstream,streamFile,start_offset))
|
|
|
|
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;
|
|
|
|
}
|