Fix Ubi Lyn .wav MPEG last interleave

This commit is contained in:
bnnm 2021-08-22 12:23:50 +02:00
parent af2bb05fdb
commit e54d9ed20b
4 changed files with 103 additions and 54 deletions

View File

@ -444,8 +444,10 @@ typedef struct {
int channels; /* max channels */
int fsb_padding; /* fsb padding mode */
int chunk_size; /* size of a data portion */
int max_chunks;
int data_size; /* playable size */
int interleave; /* size of stream interleave */
int interleave_last;
int encryption; /* encryption mode */
int big_endian;
int skip_samples;

View File

@ -51,6 +51,7 @@ int mpeg_custom_setup_init_default(STREAMFILE* sf, off_t start_offset, mpeg_code
}
break;
//todo simplify/unify XVAG/P3D/SCD/LYN and just feed arbitrary chunks to the decoder
case MPEG_P3D:
case MPEG_SCD:
if (data->config.interleave <= 0)
@ -61,7 +62,8 @@ int mpeg_custom_setup_init_default(STREAMFILE* sf, off_t start_offset, mpeg_code
if (data->config.interleave <= 0)
goto fail; /* needs external fixed size */
data->default_buffer_size = data->config.interleave;
//todo simplify/unify XVAG/P3D/SCD/LYN and just feed arbitrary chunks to the decoder
if (data->default_buffer_size < data->config.interleave_last)
data->default_buffer_size = data->config.interleave_last;
break;
case MPEG_STANDARD:
@ -179,7 +181,6 @@ int mpeg_custom_parse_frame_default(VGMSTREAMCHANNEL* stream, mpeg_codec_data* d
case MPEG_P3D: /* fixed interleave, not frame-aligned (ie. blocks may end/start in part of a frame) */
case MPEG_SCD:
case MPEG_LYN:
current_interleave = data->config.interleave;
/* check if current interleave block is short */
@ -197,6 +198,23 @@ int mpeg_custom_parse_frame_default(VGMSTREAMCHANNEL* stream, mpeg_codec_data* d
current_data_size = current_interleave;
break;
case MPEG_LYN:
/* after N interleaves last block is bigger */
if (ms->current_size_count < data->config.max_chunks)
current_interleave = data->config.interleave;
else if (ms->current_size_count == data->config.max_chunks)
current_interleave = data->config.interleave_last;
else
goto fail;
current_interleave_pre = current_interleave*num_stream;
current_interleave_post = current_interleave*(data->streams_size-1) - current_interleave_pre;
//VGM_LOG("o=%lx, %i: %x, %x, %x, %x\n", stream->offset, num_stream, ms->current_size_count, current_interleave, current_interleave_pre, current_interleave_post );
current_data_size = current_interleave;
ms->current_size_count++;
break;
default: /* standard frames (CBR or VBR) */
if ( !mpeg_get_frame_info(stream->streamfile, stream->offset, &info) )
goto fail;

View File

@ -144,7 +144,11 @@ mpeg_codec_data* init_mpeg_custom(STREAMFILE* sf, off_t start_offset, coding_t*
if (channels <= 0 || channels > 16) goto fail; /* arbitrary max */
if (channels < data->channels_per_frame) goto fail;
//todo simplify/unify XVAG/P3D/SCD/LYN and just feed arbitrary chunks to the decoder
if (data->default_buffer_size > 0x10000) goto fail; /* max for some Ubi Lyn */
/* max for some Ubi Lyn */
if (data->default_buffer_size > 0x20000) {
VGM_LOG("MPEG: buffer size too big %x\n", data->default_buffer_size);
goto fail;
}
/* init streams */

View File

@ -5,38 +5,40 @@
/* LyN RIFF - from Ubisoft LyN engine games [Red Steel 2 (Wii), Adventures of Tintin (Multi), From Dust (Multi), Just Dance 3/4 (multi)] */
VGMSTREAM * init_vgmstream_ubi_lyn(STREAMFILE *streamFile) {
VGMSTREAM* init_vgmstream_ubi_lyn(STREAMFILE* sf) {
VGMSTREAM * vgmstream = NULL;
off_t start_offset, first_offset = 0xc;
off_t fmt_offset, data_offset, fact_offset;
size_t fmt_size, data_size, fact_size;
int loop_flag, channel_count, sample_rate, codec;
int loop_flag, channels, sample_rate, codec;
int num_samples;
/* checks */
/* .sns: Red Steel 2, .wav: Tintin, .son: From Dust */
if (!check_extensions(streamFile,"sns,wav,lwav,son"))
/* .sns: Red Steel 2
* .wav: Tintin, Just Dance
* .son: From Dust */
if (!check_extensions(sf,"sns,wav,lwav,son"))
goto fail;
/* a slightly eccentric RIFF with custom codecs */
if (read_32bitBE(0x00,streamFile) != 0x52494646 || /* "RIFF" */
read_32bitBE(0x08,streamFile) != 0x57415645) /* "WAVE" */
if (!is_id32be(0x00,sf, "RIFF") ||
!is_id32be(0x08,sf, "WAVE"))
goto fail;
if (read_32bitLE(0x04,streamFile)+0x04+0x04 != get_streamfile_size(streamFile))
if (read_32bitLE(0x04,sf) + 0x04 + 0x04 != get_streamfile_size(sf))
goto fail;
if (!find_chunk(streamFile, 0x666d7420,first_offset,0, &fmt_offset,&fmt_size, 0, 0)) /* "fmt " */
if (!find_chunk(sf, 0x666d7420,first_offset,0, &fmt_offset,&fmt_size, 0, 0)) /* "fmt " */
goto fail;
if (!find_chunk(streamFile, 0x64617461,first_offset,0, &data_offset,&data_size, 0, 0)) /* "data" */
if (!find_chunk(sf, 0x64617461,first_offset,0, &data_offset,&data_size, 0, 0)) /* "data" */
goto fail;
/* always found, even with PCM (LyN subchunk seems to contain the engine version, ex. 0x0d/10) */
if (!find_chunk(streamFile, 0x66616374,first_offset,0, &fact_offset,&fact_size, 0, 0)) /* "fact" */
if (!find_chunk(sf, 0x66616374,first_offset,0, &fact_offset,&fact_size, 0, 0)) /* "fact" */
goto fail;
if (fact_size != 0x10 || read_32bitBE(fact_offset+0x04, streamFile) != 0x4C794E20) /* "LyN " */
if (fact_size != 0x10 || read_32bitBE(fact_offset+0x04, sf) != 0x4C794E20) /* "LyN " */
goto fail;
num_samples = read_32bitLE(fact_offset+0x00, streamFile);
num_samples = read_32bitLE(fact_offset+0x00, sf);
/* sometimes there is a LySE chunk */
@ -44,9 +46,9 @@ VGMSTREAM * init_vgmstream_ubi_lyn(STREAMFILE *streamFile) {
{
if (fmt_size < 0x12)
goto fail;
codec = (uint16_t)read_16bitLE(fmt_offset+0x00,streamFile);
channel_count = read_16bitLE(fmt_offset+0x02,streamFile);
sample_rate = read_32bitLE(fmt_offset+0x04,streamFile);
codec = read_u16le(fmt_offset+0x00,sf);
channels = read_u16le(fmt_offset+0x02,sf);
sample_rate = read_s32le(fmt_offset+0x04,sf);
/* 0x08: average bytes, 0x0c: block align, 0x0e: bps, etc */
/* fake WAVEFORMATEX, used with > 2ch */
@ -54,10 +56,10 @@ VGMSTREAM * init_vgmstream_ubi_lyn(STREAMFILE *streamFile) {
if (fmt_size < 0x28)
goto fail;
/* fake GUID with first value doubling as codec */
codec = read_32bitLE(fmt_offset+0x18,streamFile);
if (read_32bitBE(fmt_offset+0x1c,streamFile) != 0x00001000 &&
read_32bitBE(fmt_offset+0x20,streamFile) != 0x800000AA &&
read_32bitBE(fmt_offset+0x24,streamFile) != 0x00389B71) {
codec = read_32bitLE(fmt_offset+0x18,sf);
if (read_32bitBE(fmt_offset+0x1c,sf) != 0x00001000 &&
read_32bitBE(fmt_offset+0x20,sf) != 0x800000AA &&
read_32bitBE(fmt_offset+0x24,sf) != 0x00389B71) {
goto fail;
}
}
@ -70,7 +72,7 @@ VGMSTREAM * init_vgmstream_ubi_lyn(STREAMFILE *streamFile) {
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count,loop_flag);
vgmstream = allocate_vgmstream(channels, loop_flag);
if (!vgmstream) goto fail;
vgmstream->sample_rate = sample_rate;
@ -99,7 +101,7 @@ VGMSTREAM * init_vgmstream_ubi_lyn(STREAMFILE *streamFile) {
};
int i, ch;
for (ch = 0; ch < channel_count; ch++) {
for (ch = 0; ch < channels; ch++) {
for (i = 0; i < 16; i++) {
vgmstream->ch[ch].adpcm_coef[i] = coef[i];
}
@ -114,27 +116,27 @@ VGMSTREAM * init_vgmstream_ubi_lyn(STREAMFILE *streamFile) {
layered_layout_data* data = NULL;
int i;
if (read_32bitLE(start_offset+0x00,streamFile) != 1) /* id? */
if (read_32bitLE(start_offset+0x00,sf) != 1) /* id? */
goto fail;
interleave_size = read_32bitLE(start_offset+0x04,streamFile);
interleave_size = read_32bitLE(start_offset+0x04,sf);
/* interleave is adjusted so there is no smaller last block, it seems */
vgmstream->coding_type = coding_OGG_VORBIS;
vgmstream->layout_type = layout_layered;
/* init layout */
data = init_layout_layered(channel_count);
data = init_layout_layered(channels);
if (!data) goto fail;
vgmstream->layout_data = data;
/* open each layer subfile */
for (i = 0; i < channel_count; i++) {
for (i = 0; i < channels; i++) {
STREAMFILE* temp_sf = NULL;
size_t logical_size = read_32bitLE(start_offset+0x08 + 0x04*i,streamFile);
off_t layer_offset = start_offset + 0x08 + 0x04*channel_count; //+ interleave_size*i;
size_t logical_size = read_32bitLE(start_offset+0x08 + 0x04*i,sf);
off_t layer_offset = start_offset + 0x08 + 0x04*channels; //+ interleave_size*i;
temp_sf = setup_ubi_lyn_streamfile(streamFile, layer_offset, interleave_size, i, channel_count, logical_size);
temp_sf = setup_ubi_lyn_streamfile(sf, layer_offset, interleave_size, i, channels, logical_size);
if (!temp_sf) goto fail;
data->layers[i] = init_vgmstream_ogg_vorbis(temp_sf);
@ -155,17 +157,23 @@ VGMSTREAM * init_vgmstream_ubi_lyn(STREAMFILE *streamFile) {
#ifdef VGM_USE_MPEG
case 0x5051: { /* MPEG (PS3/PC), interleaved 1ch */
mpeg_custom_config cfg = {0};
int i;
int i, frame_samples;
cfg.interleave = read_32bitLE(start_offset+0x04,streamFile);
cfg.chunk_size = read_32bitLE(start_offset+0x08,streamFile); /* frame size (not counting MPEG padding?) */
/* 0x00: id? (2=Tintin, 3=Michael Jackson), 0x0c: frame per interleave, 0x10: samples per frame */
/* 0x00: config? (related to chunk size? 2=Tintin, some JD4?, 3=Michael Jackson, JD4) */
cfg.interleave = read_u32le(start_offset+0x04,sf);
/* 0x08: CBR frame size (not counting MPEG padding) */
/* 0x0c: bps (32) */
frame_samples = read_s32le(start_offset+0x10,sf);
/* skip seek tables and find actual start */
/* table: entries per channel (1 per chunk, but not chunk-aligned?) */
/* - 0x00: offset but same for both channels? */
/* - 0x04: flag? (-1~-N) */
/* - 0x06: flag? (-1~-N) */
start_offset += 0x14;
data_size -= 0x14;
for (i = 0; i < channel_count; i++) {
int entries = read_32bitLE(start_offset,streamFile);
for (i = 0; i < channels; i++) {
int entries = read_s32le(start_offset,sf);
start_offset += 0x04 + entries*0x08;
data_size -= 0x04 + entries*0x08;
@ -173,8 +181,25 @@ VGMSTREAM * init_vgmstream_ubi_lyn(STREAMFILE *streamFile) {
cfg.data_size = data_size;
//todo data parsing looks correct but some files decode a bit wrong at the end (ex. Tintin: Music~Boss~Allan~Victory~02)
vgmstream->codec_data = init_mpeg_custom(streamFile, start_offset, &vgmstream->coding_type, vgmstream->channels, MPEG_LYN, &cfg);
/* last interleave is half remaining data */
cfg.max_chunks = (cfg.data_size / (cfg.interleave * channels));
/* somehow certain interleaves don't work:
* - 0xB400: 0x2D00 ko, 0xE100 ok (max-1)
* - 0x8000: 0x3ec7 ko, 0xbec7 ok (max-1)
* - 0x8000: 0x5306 ok, 0xd306 ko (max-1) !!!
* (doesn't seem related to anything like data size/frame samples/sample rate/etc) */
cfg.max_chunks--;
cfg.interleave_last = (cfg.data_size - cfg.max_chunks * cfg.interleave * channels) / channels;
if (cfg.interleave <= 0x8000 && cfg.interleave_last > 0xD000) {
cfg.max_chunks++;
cfg.interleave_last = (cfg.data_size - cfg.max_chunks * cfg.interleave * channels) / channels;
}
/* for some reason num_samples may be +1 or +2 of max possible samples (no apparent meaning) */
vgmstream->num_samples = vgmstream->num_samples / frame_samples * frame_samples;
vgmstream->loop_end_sample = vgmstream->num_samples;
vgmstream->codec_data = init_mpeg_custom(sf, start_offset, &vgmstream->coding_type, vgmstream->channels, MPEG_LYN, &cfg);
if (!vgmstream->codec_data) goto fail;
vgmstream->layout_type = layout_none;
@ -192,13 +217,13 @@ VGMSTREAM * init_vgmstream_ubi_lyn(STREAMFILE *streamFile) {
/* skip standard XMA header + seek table */
/* 0x00: version? no apparent differences (0x1=Just Dance 4, 0x3=others) */
chunk_offset = start_offset + 0x04 + 0x04;
chunk_size = read_32bitLE(start_offset + 0x04, streamFile);
seek_size = read_32bitLE(chunk_offset+chunk_size, streamFile);
chunk_size = read_32bitLE(start_offset + 0x04, sf);
seek_size = read_32bitLE(chunk_offset+chunk_size, sf);
start_offset += (0x04 + 0x04 + chunk_size + 0x04 + seek_size);
data_size -= (0x04 + 0x04 + chunk_size + 0x04 + seek_size);
bytes = ffmpeg_make_riff_xma_from_fmt_chunk(buf,0x100, chunk_offset,chunk_size, data_size, streamFile, 1);
vgmstream->codec_data = init_ffmpeg_header_offset(streamFile, buf,bytes, start_offset,data_size);
bytes = ffmpeg_make_riff_xma_from_fmt_chunk(buf,0x100, chunk_offset,chunk_size, data_size, sf, 1);
vgmstream->codec_data = init_ffmpeg_header_offset(sf, buf,bytes, start_offset,data_size);
if ( !vgmstream->codec_data ) goto fail;
vgmstream->coding_type = coding_FFmpeg;
vgmstream->layout_type = layout_none;
@ -212,7 +237,7 @@ VGMSTREAM * init_vgmstream_ubi_lyn(STREAMFILE *streamFile) {
}
if ( !vgmstream_open_stream(vgmstream,streamFile,start_offset) )
if ( !vgmstream_open_stream(vgmstream,sf,start_offset) )
goto fail;
return vgmstream;
@ -223,7 +248,7 @@ fail:
/* LyN RIFF in containers */
VGMSTREAM * init_vgmstream_ubi_lyn_container(STREAMFILE *streamFile) {
VGMSTREAM * init_vgmstream_ubi_lyn_container(STREAMFILE *sf) {
VGMSTREAM *vgmstream = NULL;
STREAMFILE *temp_sf = NULL;
off_t subfile_offset;
@ -233,29 +258,29 @@ VGMSTREAM * init_vgmstream_ubi_lyn_container(STREAMFILE *streamFile) {
* data before the RIFF. Might as well support them in case the RIFF wasn't extracted. */
/* checks */
if (!check_extensions(streamFile,"sns,wav,lwav,son"))
if (!check_extensions(sf,"sns,wav,lwav,son"))
goto fail;
/* find "RIFF" position */
if (read_32bitBE(0x00,streamFile) == 0x4C795345 && /* "LySE" */
read_32bitBE(0x14,streamFile) == 0x52494646) { /* "RIFF" */
if (read_32bitBE(0x00,sf) == 0x4C795345 && /* "LySE" */
read_32bitBE(0x14,sf) == 0x52494646) { /* "RIFF" */
subfile_offset = 0x14; /* Adventures of Tintin */
}
else if (read_32bitBE(0x20,streamFile) == 0x4C795345 && /* "LySE" */
read_32bitBE(0x34,streamFile) == 0x52494646) { /* "RIFF" */
else if (read_32bitBE(0x20,sf) == 0x4C795345 && /* "LySE" */
read_32bitBE(0x34,sf) == 0x52494646) { /* "RIFF" */
subfile_offset = 0x34; /* Michael Jackson The Experience (Wii) */
}
else if (read_32bitLE(0x00,streamFile)+0x20 == get_streamfile_size(streamFile) &&
read_32bitBE(0x20,streamFile) == 0x52494646) { /* "RIFF" */
else if (read_32bitLE(0x00,sf)+0x20 == get_streamfile_size(sf) &&
read_32bitBE(0x20,sf) == 0x52494646) { /* "RIFF" */
subfile_offset = 0x20; /* Red Steel 2, From Dust */
}
else {
goto fail;
}
subfile_size = read_32bitLE(subfile_offset+0x04,streamFile) + 0x04+0x04;
subfile_size = read_32bitLE(subfile_offset+0x04,sf) + 0x04+0x04;
temp_sf = setup_subfile_streamfile(streamFile, subfile_offset, subfile_size, NULL);
temp_sf = setup_subfile_streamfile(sf, subfile_offset, subfile_size, NULL);
if (!temp_sf) goto fail;
vgmstream = init_vgmstream_ubi_lyn(temp_sf);