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 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 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); 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 */ /* helper to pass a wrapped, clamped, fake extension-ed, SF to another meta */

View File

@ -3,12 +3,12 @@
#ifdef VGM_USE_MPEG #ifdef VGM_USE_MPEG
/* init config and validate per type */ /* 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; mpeg_frame_info info;
/* get frame info at offset */ /* 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; goto fail;
switch(info.layer) { switch(info.layer) {
case 1: *coding_type = coding_MPEG_layer1; break; case 1: *coding_type = coding_MPEG_layer1; break;
@ -428,4 +428,48 @@ size_t mpeg_get_samples(STREAMFILE *sf, off_t start_offset, size_t bytes) {
return samples; 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 #endif

View File

@ -7,7 +7,7 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
off_t start_offset; off_t start_offset;
uint32_t data_size, loop_start = 0, loop_end = 0; uint32_t data_size, loop_start = 0, loop_end = 0;
uint32_t codec, flags; uint32_t codec, flags;
int loop_flag, channel_count, sample_rate; int loop_flag, channels, sample_rate;
/* checks */ /* checks */
@ -19,14 +19,14 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
goto fail; goto fail;
/* check header "MSF" + version-char, usually: /* 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" */ if ((read_u32be(0x00,sf) & 0xffffff00) != 0x4D534600) /* "MSF\0" */
goto fail; goto fail;
start_offset = 0x40; start_offset = 0x40;
codec = read_u32be(0x04,sf); codec = read_u32be(0x04,sf);
channel_count = read_s32be(0x08,sf); channels = read_s32be(0x08,sf);
data_size = read_u32be(0x0C,sf); /* without header */ data_size = read_u32be(0x0C,sf); /* without header */
if (data_size == 0xFFFFFFFF) /* unneeded? */ if (data_size == 0xFFFFFFFF) /* unneeded? */
data_size = get_streamfile_size(sf) - start_offset; data_size = get_streamfile_size(sf) - start_offset;
@ -35,13 +35,13 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
/* byte flags, not in MSFv1 or v2 /* byte flags, not in MSFv1 or v2
* 0x01/02/04/08: loop marker 0/1/2/3 * 0x01/02/04/08: loop marker 0/1/2/3
* 0x10: resample options (force 44/48khz) * 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) * 0x40: joint stereo MP3 (apparently interleaved stereo for other formats)
* 0x80+: (none/reserved) */ * 0x80+: (none/reserved) */
flags = read_u32be(0x14,sf); 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 /* 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 */ * 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) */ /* loop markers (marker N @ 0x18 + N*(4+4), but in practice only marker 0 is used) */
if (loop_flag) { if (loop_flag) {
@ -54,7 +54,7 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
/* build the VGMSTREAM */ /* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count,loop_flag); vgmstream = allocate_vgmstream(channels, loop_flag);
if (!vgmstream) goto fail; if (!vgmstream) goto fail;
vgmstream->meta_type = meta_MSF; vgmstream->meta_type = meta_MSF;
@ -63,16 +63,16 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
vgmstream->sample_rate = 48000; vgmstream->sample_rate = 48000;
switch (codec) { switch (codec) {
case 0x00: /* PCM (Big Endian) */ case 0x00: /* PCM (Big Endian) [MSEnc tests] */
case 0x01: { /* PCM (Little Endian) [Smash Cars (PS3)] */ case 0x01: { /* PCM (Little Endian) [Smash Cars (PS3)] */
vgmstream->coding_type = codec==0 ? coding_PCM16BE : coding_PCM16LE; vgmstream->coding_type = codec==0 ? coding_PCM16BE : coding_PCM16LE;
vgmstream->layout_type = layout_interleave; vgmstream->layout_type = layout_interleave;
vgmstream->interleave_block_size = 0x02; 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){ if (loop_flag){
vgmstream->loop_start_sample = pcm_bytes_to_samples(loop_start, 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, channel_count,16); vgmstream->loop_end_sample = pcm_bytes_to_samples(loop_end, channels, 16);
} }
break; break;
@ -87,10 +87,10 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
vgmstream->layout_type = layout_interleave; vgmstream->layout_type = layout_interleave;
vgmstream->interleave_block_size = 0x10; 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) { if (loop_flag) {
vgmstream->loop_start_sample = ps_bytes_to_samples(loop_start,channel_count); vgmstream->loop_start_sample = ps_bytes_to_samples(loop_start,channels);
vgmstream->loop_end_sample = ps_bytes_to_samples(loop_end,channel_count); vgmstream->loop_end_sample = ps_bytes_to_samples(loop_end,channels);
} }
break; break;
@ -127,25 +127,23 @@ VGMSTREAM* init_vgmstream_msf(STREAMFILE* sf) {
} }
#endif #endif
#if defined(VGM_USE_MPEG) #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); vgmstream->codec_data = init_mpeg(sf, start_offset, &vgmstream->coding_type, vgmstream->channels);
if (!vgmstream->codec_data) goto fail; if (!vgmstream->codec_data) goto fail;
vgmstream->layout_type = layout_none; vgmstream->layout_type = layout_none;
vgmstream->num_samples = mpeg_bytes_to_samples(data_size, vgmstream->codec_data); vgmstream->num_samples = mpeg_get_samples_clean(sf, start_offset, data_size, &loop_start, &loop_end, is_vbr);
if (loop_flag) { vgmstream->loop_start_sample = loop_start;
vgmstream->loop_start_sample = mpeg_bytes_to_samples(loop_start, vgmstream->codec_data); vgmstream->loop_end_sample = loop_end;
vgmstream->loop_end_sample = mpeg_bytes_to_samples(loop_end, vgmstream->codec_data); /* MPEG here seems stripped from ID3/Xing headers, loops are frame offsets */
/* loops are always aligned to CBR frame beginnings */
}
/* encoder delay varies between 1152 (1f), 528, 576, etc; probably not actually skipped */ /* encoder delay varies between 1152 (1f), 528, 576, etc; probably not actually skipped */
break; break;
} }
#elif defined(VGM_USE_FFMPEG) #elif defined(VGM_USE_FFMPEG)
case 0x07: case 0x07: { /* MPEG (LAME MP3) [Dengeki Bunko Fighting Climax (PS3), Asura's Wrath (PS3)-vbr] */
{ /* MPEG (CBR LAME MP3) [Dengeki Bunko Fighting Climax (PS3)] */
ffmpeg_codec_data *ffmpeg_data = NULL; ffmpeg_codec_data *ffmpeg_data = NULL;
ffmpeg_data = init_ffmpeg_offset(sf, start_offset, sf->get_size(sf)); 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->coding_type = coding_FFmpeg;
vgmstream->layout_type = layout_none; 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; vgmstream->num_samples = (int64_t)data_size * ffmpeg_data->sampleRate * 8 / ffmpeg_data->bitrate;
if (loop_flag) { if (loop_flag) {
vgmstream->loop_start_sample = (int64_t)loop_start * ffmpeg_data->sampleRate * 8 / ffmpeg_data->bitrate; 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; 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; break;
} }
#endif #endif