Fix .MSF sample counts with VBR MP3 [Asura's Wrath (PS3)]

This commit is contained in:
bnnm 2020-11-06 17:43:29 +01:00
parent f93700ed3e
commit 8a1430e0b1
3 changed files with 74 additions and 33 deletions

View File

@ -606,6 +606,7 @@ size_t atrac3plus_bytes_to_samples(size_t bytes, int full_block_align);
size_t ac3_bytes_to_samples(size_t bytes, int full_block_align, int channels);
size_t aac_get_samples(STREAMFILE* sf, off_t start_offset, size_t bytes);
size_t mpeg_get_samples(STREAMFILE* sf, off_t start_offset, size_t bytes);
int32_t mpeg_get_samples_clean(STREAMFILE* sf, off_t start, size_t size, size_t* p_loop_start, size_t* p_loop_end, int is_vbr);
/* helper to pass a wrapped, clamped, fake extension-ed, SF to another meta */

View File

@ -3,12 +3,12 @@
#ifdef VGM_USE_MPEG
/* init config and validate per type */
int mpeg_custom_setup_init_default(STREAMFILE *streamFile, off_t start_offset, mpeg_codec_data *data, coding_t *coding_type) {
int mpeg_custom_setup_init_default(STREAMFILE* sf, off_t start_offset, mpeg_codec_data* data, coding_t* coding_type) {
mpeg_frame_info info;
/* get frame info at offset */
if ( !mpeg_get_frame_info(streamFile, start_offset, &info))
if ( !mpeg_get_frame_info(sf, start_offset, &info))
goto fail;
switch(info.layer) {
case 1: *coding_type = coding_MPEG_layer1; break;
@ -113,7 +113,7 @@ fail:
/* writes data to the buffer and moves offsets */
int mpeg_custom_parse_frame_default(VGMSTREAMCHANNEL *stream, mpeg_codec_data *data, int num_stream) {
int mpeg_custom_parse_frame_default(VGMSTREAMCHANNEL* stream, mpeg_codec_data* data, int num_stream) {
mpeg_custom_stream *ms = data->streams[num_stream];
mpeg_frame_info info;
size_t current_data_size = 0;
@ -242,7 +242,7 @@ fail:
* Gets info from a MPEG frame header at offset. Normally you would use mpg123_info but somehow
* it's wrong at times (maybe because we use an ancient version) so here we do our thing.
*/
static int mpeg_get_frame_info_h(uint32_t header, mpeg_frame_info *info) {
static int mpeg_get_frame_info_h(uint32_t header, mpeg_frame_info* info) {
/* index tables */
static const int versions[4] = { /* MPEG 2.5 */ 3, /* reserved */ -1, /* MPEG 2 */ 2, /* MPEG 1 */ 1 };
static const int layers[4] = { -1,3,2,1 };
@ -313,12 +313,12 @@ static int mpeg_get_frame_info_h(uint32_t header, mpeg_frame_info *info) {
fail:
return 0;
}
int mpeg_get_frame_info(STREAMFILE *sf, off_t offset, mpeg_frame_info *info) {
int mpeg_get_frame_info(STREAMFILE* sf, off_t offset, mpeg_frame_info* info) {
uint32_t header = read_u32be(offset, sf);
return mpeg_get_frame_info_h(header, info);
}
size_t mpeg_get_samples(STREAMFILE *sf, off_t start_offset, size_t bytes) {
size_t mpeg_get_samples(STREAMFILE* sf, off_t start_offset, size_t bytes) {
off_t offset = start_offset;
off_t max_offset = start_offset + bytes;
int frames = 0, samples = 0, encoder_delay = 0, encoder_padding = 0;
@ -428,4 +428,48 @@ size_t mpeg_get_samples(STREAMFILE *sf, off_t start_offset, size_t bytes) {
return samples;
}
/* variation of the above, for clean streams = no ID3/VBR headers
* (maybe should be fused in a single thing with config, API is kinda messy too) */
int32_t mpeg_get_samples_clean(STREAMFILE *sf, off_t start, size_t size, size_t* p_loop_start, size_t* p_loop_end, int is_vbr) {
mpeg_frame_info info;
off_t offset = start;
int32_t num_samples = 0, loop_start = 0, loop_end = 0;
if (!is_vbr) {
/* CBR = quick calcs */
if (!mpeg_get_frame_info(sf, offset, &info))
goto fail;
num_samples = size / info.frame_size * info.frame_samples;
loop_start = *p_loop_start / info.frame_size * info.frame_samples;
loop_end = *p_loop_end / info.frame_size * info.frame_samples;
}
else {
/* VBR (or unknown) = count frames */
while (offset < start + size) {
if (!mpeg_get_frame_info(sf, offset, &info))
goto fail;
if (*p_loop_start + start == offset)
loop_start = num_samples;
num_samples += info.frame_samples;
offset += info.frame_size;
if (*p_loop_end + start == offset)
loop_end = num_samples;
}
}
*p_loop_start = loop_start;
*p_loop_end = loop_end;
return num_samples;
fail:
VGM_LOG("MPEG: sample reader failed at %lx\n", offset);
return 0;
}
#endif

View File

@ -7,7 +7,7 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
off_t start_offset;
uint32_t data_size, loop_start = 0, loop_end = 0;
uint32_t codec, flags;
int loop_flag, channel_count, sample_rate;
int loop_flag, channels, sample_rate;
/* checks */
@ -19,14 +19,14 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
goto fail;
/* check header "MSF" + version-char, usually:
* 0x01, 0x02, 0x30 ("0"), 0x35 ("5"), 0x43 ("C") (last/most common version) */
* 0x01, 0x02, 0x30="0", 0x35="5", 0x43="C" (last/most common version) */
if ((read_u32be(0x00,sf) & 0xffffff00) != 0x4D534600) /* "MSF\0" */
goto fail;
start_offset = 0x40;
codec = read_u32be(0x04,sf);
channel_count = read_s32be(0x08,sf);
channels = read_s32be(0x08,sf);
data_size = read_u32be(0x0C,sf); /* without header */
if (data_size == 0xFFFFFFFF) /* unneeded? */
data_size = get_streamfile_size(sf) - start_offset;
@ -35,26 +35,26 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
/* byte flags, not in MSFv1 or v2
* 0x01/02/04/08: loop marker 0/1/2/3
* 0x10: resample options (force 44/48khz)
* 0x20: VBR MP3 source (changed into simplified 0x1a1 CBR)
* 0x20: VBR MP3 source (encoded with min/max quality options, may end up being CBR)
* 0x40: joint stereo MP3 (apparently interleaved stereo for other formats)
* 0x80+: (none/reserved) */
flags = read_u32be(0x14,sf);
/* 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));
loop_flag = (flags != 0xffffffff) && ((flags & 0x01) || (flags & 0x02));
/* loop markers (marker N @ 0x18 + N*(4+4), but in practice only marker 0 is used) */
if (loop_flag) {
loop_start = read_u32be(0x18,sf);
loop_end = read_u32be(0x1C,sf); /* loop duration */
loop_end = loop_start + loop_end; /* usually equals data_size but not always */
if (loop_end > data_size)/* not seen */
if (loop_end > data_size) /* not seen */
loop_end = data_size;
}
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count,loop_flag);
vgmstream = allocate_vgmstream(channels, loop_flag);
if (!vgmstream) goto fail;
vgmstream->meta_type = meta_MSF;
@ -63,16 +63,16 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
vgmstream->sample_rate = 48000;
switch (codec) {
case 0x00: /* PCM (Big Endian) */
case 0x00: /* PCM (Big Endian) [MSEnc tests] */
case 0x01: { /* PCM (Little Endian) [Smash Cars (PS3)] */
vgmstream->coding_type = codec==0 ? coding_PCM16BE : coding_PCM16LE;
vgmstream->layout_type = layout_interleave;
vgmstream->interleave_block_size = 0x02;
vgmstream->num_samples = pcm_bytes_to_samples(data_size, channel_count,16);
vgmstream->num_samples = pcm_bytes_to_samples(data_size, channels, 16);
if (loop_flag){
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);
vgmstream->loop_start_sample = pcm_bytes_to_samples(loop_start, channels, 16);
vgmstream->loop_end_sample = pcm_bytes_to_samples(loop_end, channels, 16);
}
break;
@ -87,10 +87,10 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
vgmstream->layout_type = layout_interleave;
vgmstream->interleave_block_size = 0x10;
vgmstream->num_samples = ps_bytes_to_samples(data_size, channel_count);
vgmstream->num_samples = ps_bytes_to_samples(data_size, channels);
if (loop_flag) {
vgmstream->loop_start_sample = ps_bytes_to_samples(loop_start,channel_count);
vgmstream->loop_end_sample = ps_bytes_to_samples(loop_end,channel_count);
vgmstream->loop_start_sample = ps_bytes_to_samples(loop_start,channels);
vgmstream->loop_end_sample = ps_bytes_to_samples(loop_end,channels);
}
break;
@ -127,25 +127,23 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
}
#endif
#if defined(VGM_USE_MPEG)
case 0x07: { /* MPEG (CBR LAME MP3) [Dengeki Bunko Fighting Climax (PS3)] */
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 */
vgmstream->codec_data = init_mpeg(sf, start_offset, &vgmstream->coding_type, vgmstream->channels);
if (!vgmstream->codec_data) goto fail;
vgmstream->layout_type = layout_none;
vgmstream->num_samples = mpeg_bytes_to_samples(data_size, vgmstream->codec_data);
if (loop_flag) {
vgmstream->loop_start_sample = mpeg_bytes_to_samples(loop_start, vgmstream->codec_data);
vgmstream->loop_end_sample = mpeg_bytes_to_samples(loop_end, vgmstream->codec_data);
/* loops are always aligned to CBR frame beginnings */
}
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 */
/* encoder delay varies between 1152 (1f), 528, 576, etc; probably not actually skipped */
break;
}
#elif defined(VGM_USE_FFMPEG)
case 0x07:
{ /* MPEG (CBR LAME MP3) [Dengeki Bunko Fighting Climax (PS3)] */
case 0x07: { /* MPEG (LAME MP3) [Dengeki Bunko Fighting Climax (PS3), Asura's Wrath (PS3)-vbr] */
ffmpeg_codec_data *ffmpeg_data = NULL;
ffmpeg_data = init_ffmpeg_offset(sf, start_offset, sf->get_size(sf));
@ -154,14 +152,12 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
vgmstream->coding_type = coding_FFmpeg;
vgmstream->layout_type = layout_none;
//todo use same calcs as above
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;
/* loops are always aligned to CBR frame beginnings */
}
/* encoder delay varies between 1152 (1f), 528, 576, etc; probably not actually skipped */
break;
}
#endif
@ -171,7 +167,7 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
}
if (!vgmstream_open_stream(vgmstream,sf,start_offset))
if (!vgmstream_open_stream(vgmstream, sf, start_offset))
goto fail;
return vgmstream;