Add Ubi LyN (.sns/son/wav) [Adventures of Tintin, From Dust (multi)]

This commit is contained in:
bnnm 2018-04-07 11:31:43 +02:00
parent 8fab88be8f
commit 41e60f29da
11 changed files with 422 additions and 5 deletions

View File

@ -58,7 +58,11 @@ int mpeg_custom_setup_init_default(STREAMFILE *streamFile, off_t start_offset, m
break;
case MPEG_LYN:
goto fail; /* not fully implemented */
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
break;
case MPEG_STANDARD:
case MPEG_AHX:
@ -137,9 +141,9 @@ 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;
#if 1
/* check if current interleave block is short */
{
off_t block_offset = stream->offset - stream->channel_start_offset;
@ -148,7 +152,7 @@ int mpeg_custom_parse_frame_default(VGMSTREAMCHANNEL *stream, mpeg_codec_data *d
if (data->config.data_size && block_offset + next_block >= data->config.data_size)
current_interleave = (data->config.data_size % next_block) / data->streams_size; /* short_interleave*/
}
#endif
current_interleave_pre = current_interleave*num_stream;
current_interleave_post = current_interleave*(data->streams_size-1) - current_interleave_pre;
@ -162,7 +166,7 @@ int mpeg_custom_parse_frame_default(VGMSTREAMCHANNEL *stream, mpeg_codec_data *d
break;
}
if (!current_data_size || current_data_size > ms->buffer_size) {
VGM_LOG("MPEG: incorrect data_size 0x%x\n", current_data_size);
VGM_LOG("MPEG: incorrect data_size 0x%x vs buffer 0x%x\n", current_data_size, ms->buffer_size);
goto fail;
}

View File

@ -131,6 +131,8 @@ mpeg_codec_data *init_mpeg_custom(STREAMFILE *streamFile, off_t start_offset, co
memcpy(&data->config, config, sizeof(mpeg_custom_config));
data->config.channels = channels;
data->default_buffer_size = MPEG_DATA_BUFFER_SIZE;
/* init per subtype */
switch(data->type) {
case MPEG_EAL31:
@ -145,6 +147,7 @@ mpeg_codec_data *init_mpeg_custom(STREAMFILE *streamFile, off_t start_offset, co
if (channels <= 0 || channels > 16) goto fail; /* arbitrary max */
if (channels < data->channels_per_frame) goto fail;
if (data->default_buffer_size > 0x8000) goto fail;
/* init streams */
@ -161,7 +164,7 @@ mpeg_codec_data *init_mpeg_custom(STREAMFILE *streamFile, off_t start_offset, co
if (!data->streams[i]->output_buffer) goto fail;
/* one per stream as sometimes mpg123 can't read the whole buffer in one pass */
data->streams[i]->buffer_size = MPEG_DATA_BUFFER_SIZE;
data->streams[i]->buffer_size = data->default_buffer_size;
data->streams[i]->buffer = calloc(sizeof(uint8_t), data->streams[i]->buffer_size);
if (!data->streams[i]->buffer) goto fail;
}

View File

@ -303,6 +303,7 @@ static const char* extension_list[] = {
"snr",
"sns",
"snu",
"son",
"spd",
"spm",
"sps",
@ -983,6 +984,7 @@ static const meta_info meta_info_list[] = {
{meta_MP4, "MP4/AAC header"},
{meta_PCM_SRE, "Capcom .PCM+SRE header"},
{meta_DSP_MCADPCM, "Bethesda .mcadpcm header"},
{meta_UBI_LYN, "Ubisoft LyN RIFF header"},
#ifdef VGM_USE_FFMPEG
{meta_FFmpeg, "FFmpeg supported file format"},

View File

@ -231,6 +231,10 @@
<File
RelativePath=".\meta\sqex_scd_streamfile.h"
>
</File>
<File
RelativePath=".\meta\ubi_lyn_ogg_streamfile.h"
>
</File>
</Filter>
<Filter
@ -1274,6 +1278,10 @@
RelativePath=".\meta\ubi_ckd.c"
>
</File>
<File
RelativePath=".\meta\ubi_lyn.c"
>
</File>
<File
RelativePath=".\meta\ubi_raki.c"
>

View File

@ -100,6 +100,7 @@
<ClInclude Include="meta\bar_streamfile.h" />
<ClInclude Include="meta\ea_eaac_eatrax_streamfile.h" />
<ClInclude Include="meta\sqex_scd_streamfile.h" />
<ClInclude Include="meta\ubi_lyn_ogg_streamfile.h" />
<ClInclude Include="meta\meta.h" />
<ClInclude Include="meta\hca_keys.h" />
<ClInclude Include="meta\fsb_keys.h" />
@ -399,6 +400,7 @@
<ClCompile Include="meta\thp.c" />
<ClCompile Include="meta\vgs.c" />
<ClCompile Include="meta\ubi_ckd.c" />
<ClCompile Include="meta\ubi_lyn.c" />
<ClCompile Include="meta\ubi_raki.c" />
<ClCompile Include="meta\ubi_sb.c" />
<ClCompile Include="meta\vs.c" />

View File

@ -80,6 +80,9 @@
<ClInclude Include="meta\sqex_scd_streamfile.h">
<Filter>meta\Header Files</Filter>
</ClInclude>
<ClInclude Include="meta\ubi_lyn_ogg_streamfile.h">
<Filter>meta\Header Files</Filter>
</ClInclude>
<ClInclude Include="meta\meta.h">
<Filter>meta\Header Files</Filter>
</ClInclude>
@ -778,6 +781,9 @@
<ClCompile Include="meta\ubi_ckd.c">
<Filter>meta\Source Files</Filter>
</ClCompile>
<ClCompile Include="meta\ubi_lyn.c">
<Filter>meta\Source Files</Filter>
</ClCompile>
<ClCompile Include="meta\ubi_raki.c">
<Filter>meta\Source Files</Filter>
</ClCompile>

View File

@ -729,4 +729,7 @@ VGMSTREAM * init_vgmstream_pcm_sre(STREAMFILE *streamFile);
VGMSTREAM * init_vgmstream_dsp_mcadpcm(STREAMFILE *streamFile);
VGMSTREAM * init_vgmstream_ubi_lyn(STREAMFILE * streamFile);
VGMSTREAM * init_vgmstream_ubi_lyn_container(STREAMFILE * streamFile);
#endif /*_META_H*/

293
src/meta/ubi_lyn.c Normal file
View File

@ -0,0 +1,293 @@
#include "meta.h"
#include "../layout/layout.h"
#include "../coding/coding.h"
#include "ubi_lyn_ogg_streamfile.h"
static STREAMFILE* setup_lyn_streamfile(STREAMFILE *streamFile, off_t subfile_offset, size_t subfile_size);
/* 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 * 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 num_samples;
/* checks */
/* .sns: Red Steel 2, .wav: Tintin, .son: From Dust */
if (!check_extensions(streamFile,"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" */
goto fail;
if (read_32bitLE(0x04,streamFile)+0x04+0x04 != get_streamfile_size(streamFile))
goto fail;
if (!find_chunk(streamFile, 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" */
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" */
goto fail;
if (fact_size != 0x10 || read_32bitBE(fact_offset+0x04, streamFile) != 0x4C794E20) /* "LyN " */
goto fail;
num_samples = read_32bitLE(fact_offset+0x00, streamFile);
/* sometimes there is a LySE chunk */
/* parse format */
{
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);
/* 0x08: average bytes, 0x0c: block align, 0x0e: bps, etc */
/* fake WAVEFORMATEX, used with > 2ch */
if (codec == 0xFFFE) {
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) {
goto fail;
}
}
}
/* most songs simply repeat, loop if it looks long enough */
loop_flag = (num_samples > 20*sample_rate); /* in seconds */
start_offset = data_offset;
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count,loop_flag);
if (!vgmstream) goto fail;
vgmstream->sample_rate = sample_rate;
vgmstream->meta_type = meta_UBI_LYN;
vgmstream->num_samples = num_samples;
vgmstream->loop_start_sample = 0;
vgmstream->loop_end_sample = vgmstream->num_samples;
switch(codec) {
case 0x0001: /* PCM */
vgmstream->coding_type = coding_PCM16LE; /* LE even in X360 */
vgmstream->layout_type = layout_interleave;
vgmstream->interleave_block_size = 0x02;
break;
case 0x5050: /* DSP (Wii) */
vgmstream->coding_type = coding_NGC_DSP;
vgmstream->layout_type = layout_interleave;
vgmstream->interleave_block_size = 0x08;
/* setup default Ubisoft coefs */
{
static const int16_t coef[16] = {
0x04ab,0xfced,0x0789,0xfedf,0x09a2,0xfae5,0x0c90,0xfac1,
0x084d,0xfaa4,0x0982,0xfdf7,0x0af6,0xfafa,0x0be6,0xfbf5
};
int i, ch;
for (ch = 0; ch < channel_count; ch++) {
for (i = 0; i < 16; i++) {
vgmstream->ch[ch].adpcm_coef[i] = coef[i];
}
}
}
break;
#ifdef VGM_USE_VORBIS
case 0x3157: { /* Ogg (PC), interleaved 1ch */
size_t interleave_size, stride_size;
layered_layout_data* data = NULL;
int i;
if (read_32bitLE(start_offset+0x00,streamFile) != 1) /* id? */
goto fail;
interleave_size = read_32bitLE(start_offset+0x04,streamFile);
stride_size = interleave_size * channel_count;
/* 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);
if (!data) goto fail;
vgmstream->layout_data = data;
/* open each layer subfile */
for (i = 0; i < channel_count; i++) {
STREAMFILE* temp_streamFile = NULL;
size_t total_size = read_32bitLE(start_offset+0x08 + 0x04*i,streamFile);
off_t layer_offset = start_offset+0x08 + 0x04*channel_count + interleave_size*i;
temp_streamFile = setup_lyn_ogg_streamfile(streamFile, layer_offset, interleave_size, stride_size, total_size);
if (!temp_streamFile) goto fail;
data->layers[i] = init_vgmstream_ogg_vorbis(temp_streamFile);
close_streamfile(temp_streamFile);
if (!data->layers[i]) goto fail;
/* could validate between layers, meh */
}
/* setup layered VGMSTREAMs */
if (!setup_layout_layered(data))
goto fail;
break;
}
#endif
#ifdef VGM_USE_MPEG
case 0x5051: { /* MPEG (PS3/PC), interleaved 1ch */
mpeg_codec_data *mpeg_data = NULL;
mpeg_custom_config cfg = {0};
int i;
if (read_32bitLE(start_offset+0x00,streamFile) != 2) /* id? */
goto fail;
cfg.interleave = read_32bitLE(start_offset+0x04,streamFile);
cfg.chunk_size = read_32bitLE(start_offset+0x08,streamFile);
/* 0x08: frame size, 0x0c: frame per interleave, 0x10: samples per frame */
/* skip seek tables and find actual start */
start_offset += 0x14;
data_size -= 0x14;
for (i = 0; i < channel_count; i++) {
int entries = read_32bitLE(start_offset,streamFile);
start_offset += 0x04 + entries*0x08;
data_size -= 0x04 + entries*0x08;
}
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)
mpeg_data = init_mpeg_custom(streamFile, start_offset, &vgmstream->coding_type, vgmstream->channels, MPEG_LYN, &cfg);
if (!mpeg_data) goto fail;
vgmstream->codec_data = mpeg_data;
vgmstream->layout_type = layout_none;
break;
}
#endif
#ifdef VGM_USE_FFMPEG
case 0x0166: { /* XMA (X360), standard */
uint8_t buf[0x100];
int bytes;
off_t chunk_offset;
size_t chunk_size, seek_size;
if (read_32bitLE(start_offset+0x00,streamFile) != 3) /* id? */
goto fail;
/* skip standard XMA header + seek table */
chunk_offset = start_offset + 0x04 + 0x04;
chunk_size = read_32bitLE(start_offset + 0x04, streamFile);
seek_size = read_32bitLE(chunk_offset+chunk_size, streamFile);
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);
if ( !vgmstream->codec_data ) goto fail;
vgmstream->coding_type = coding_FFmpeg;
vgmstream->layout_type = layout_none;
break;
}
#endif
default:
goto fail;
}
if ( !vgmstream_open_stream(vgmstream,streamFile,start_offset) )
goto fail;
return vgmstream;
fail:
close_vgmstream(vgmstream);
return NULL;
}
/* LyN RIFF in containers */
VGMSTREAM * init_vgmstream_ubi_lyn_container(STREAMFILE *streamFile) {
VGMSTREAM *vgmstream = NULL;
STREAMFILE *temp_streamFile = NULL;
off_t subfile_offset;
size_t subfile_size;
/* LyN packs files in bigfiles, and once extracted the sound files have extra engine
* 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"))
goto fail;
/* find "RIFF" position */
if (read_32bitBE(0x00,streamFile) == 0x4C795345 && /* "LySE" */
read_32bitBE(0x14,streamFile) == 0x52494646) { /* "RIFF" */
subfile_offset = 0x14; /* Adventures of Tintin */
}
else if (read_32bitLE(0x00,streamFile)+0x20 == get_streamfile_size(streamFile) &&
read_32bitBE(0x20,streamFile) == 0x52494646) { /* "RIFF" */
subfile_offset = 0x20; /* Red Steel 2, From Dust */
}
else {
goto fail;
}
subfile_size = read_32bitLE(subfile_offset+0x04,streamFile) + 0x04+0x04;
temp_streamFile = setup_lyn_streamfile(streamFile, subfile_offset,subfile_size);
if (!temp_streamFile) goto fail;
vgmstream = init_vgmstream_ubi_lyn(temp_streamFile);
close_streamfile(temp_streamFile);
return vgmstream;
fail:
close_streamfile(temp_streamFile);
close_vgmstream(vgmstream);
return NULL;
}
static STREAMFILE* setup_lyn_streamfile(STREAMFILE *streamFile, off_t subfile_offset, size_t subfile_size) {
STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL;
/* setup subfile */
new_streamFile = open_wrap_streamfile(streamFile);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
new_streamFile = open_clamp_streamfile(temp_streamFile, subfile_offset,subfile_size);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
return temp_streamFile;
fail:
close_streamfile(temp_streamFile);
return NULL;
}

View File

@ -0,0 +1,92 @@
#ifndef _LYN_OGG_STREAMFILE_H_
#define _LYN_OGG_STREAMFILE_H_
#include "../streamfile.h"
typedef struct {
off_t start_physical_offset; /* interleaved data start, for this substream */
size_t interleave_block_size; /* max size that can be read before encountering other substreams */
size_t stride_size; /* step size between interleave blocks (interleave*channels) */
size_t total_size; /* final size of the deinterleaved substream */
} lyn_ogg_io_data;
/* Handles deinterleaving of complete files, skipping portions or other substreams. */
static size_t scd_dsp_io_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length, lyn_ogg_io_data* data) {
size_t total_read = 0;
while (length > 0) {
size_t to_read;
size_t length_available;
off_t block_num;
off_t intrablock_offset;
off_t physical_offset;
block_num = offset / data->interleave_block_size;
intrablock_offset = offset % data->interleave_block_size;
physical_offset = data->start_physical_offset + block_num*data->stride_size + intrablock_offset;
length_available = data->interleave_block_size - intrablock_offset;
if (length < length_available) {
to_read = length;
}
else {
to_read = length_available;
}
if (to_read > 0) {
size_t bytes_read;
bytes_read = read_streamfile(dest, physical_offset, to_read, streamfile);
total_read += bytes_read;
if (bytes_read != to_read) {
return total_read;
}
dest += bytes_read;
offset += bytes_read;
length -= bytes_read;
}
}
return total_read;
}
static size_t scd_dsp_io_size(STREAMFILE *streamfile, lyn_ogg_io_data* data) {
return data->total_size;
}
static STREAMFILE* setup_lyn_ogg_streamfile(STREAMFILE *streamFile, off_t start_offset, size_t interleave_block_size, size_t stride_size, size_t total_size) {
STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL;
lyn_ogg_io_data io_data = {0};
size_t io_data_size = sizeof(lyn_ogg_io_data);
io_data.start_physical_offset = start_offset;
io_data.interleave_block_size = interleave_block_size;
io_data.stride_size = stride_size;
io_data.total_size = total_size;
/* setup subfile */
new_streamFile = open_wrap_streamfile(streamFile);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
new_streamFile = open_io_streamfile(temp_streamFile, &io_data,io_data_size, scd_dsp_io_read,scd_dsp_io_size);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
new_streamFile = open_fakename_streamfile(temp_streamFile, NULL,"ogg");
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
return temp_streamFile;
fail:
close_streamfile(temp_streamFile);
return NULL;
}
#endif /* _LYN_OGG_STREAMFILE_H_ */

View File

@ -394,6 +394,8 @@ VGMSTREAM * (*init_vgmstream_functions[])(STREAMFILE *streamFile) = {
init_vgmstream_sthd,
init_vgmstream_pcm_sre,
init_vgmstream_dsp_mcadpcm,
init_vgmstream_ubi_lyn,
init_vgmstream_ubi_lyn_container,
init_vgmstream_txth, /* should go at the end (lower priority) */
#ifdef VGM_USE_FFMPEG

View File

@ -665,6 +665,7 @@ typedef enum {
meta_MP4, /* MP4/AAC */
meta_PCM_SRE, /* .PCM+SRE [Viewtiful Joe (PS2)] */
meta_DSP_MCADPCM, /* Skyrim (Switch) */
meta_UBI_LYN, /* Ubisoft LyN engine [The Adventures of Tintin (multi)] */
#ifdef VGM_USE_FFMPEG
meta_FFmpeg,
@ -964,6 +965,7 @@ typedef struct {
mpeg_custom_t type; /* mpeg subtype */
mpeg_custom_config config; /* config depending on the mode */
size_t default_buffer_size;
mpeg_custom_stream **streams; /* array of MPEG streams (ex. 2ch+2ch) */
size_t streams_size;