2017-12-26 18:17:59 +01:00
|
|
|
#include "meta.h"
|
|
|
|
#include "../coding/coding.h"
|
|
|
|
|
|
|
|
/* XNB - Microsoft XNA Game Studio 4.0 format */
|
|
|
|
VGMSTREAM * init_vgmstream_xnb(STREAMFILE *streamFile) {
|
|
|
|
VGMSTREAM * vgmstream = NULL;
|
Fix XMA gapless/looping/samples
fixes: standard, wem, xwc, xwb, xnb, xwx, rak, pk, txth, genh, seg, rsd, past, p3d, nub-xma, gtd, gsp, fsb, eaac, cxs, awc, aac
2018-11-18 17:01:31 +01:00
|
|
|
off_t start_offset, xma_chunk_offset = 0;
|
2018-02-10 17:45:33 +01:00
|
|
|
int loop_flag = 0, channel_count, num_samples = 0, loop_start = 0, loop_end = 0;
|
2019-02-23 03:15:49 +01:00
|
|
|
int big_endian, flags, codec, sample_rate, block_align, bps;
|
2018-02-10 17:45:33 +01:00
|
|
|
size_t data_size;
|
|
|
|
char platform;
|
2017-12-26 18:17:59 +01:00
|
|
|
|
|
|
|
|
Fix XMA gapless/looping/samples
fixes: standard, wem, xwc, xwb, xnb, xwx, rak, pk, txth, genh, seg, rsd, past, p3d, nub-xma, gtd, gsp, fsb, eaac, cxs, awc, aac
2018-11-18 17:01:31 +01:00
|
|
|
/* checks */
|
|
|
|
if (!check_extensions(streamFile,"xnb"))
|
2017-12-26 18:17:59 +01:00
|
|
|
goto fail;
|
|
|
|
if ((read_32bitBE(0,streamFile) & 0xFFFFFF00) != 0x584E4200) /* "XNB" */
|
|
|
|
goto fail;
|
2018-02-10 17:45:33 +01:00
|
|
|
|
2018-02-17 02:09:48 +01:00
|
|
|
/* XNA Studio platforms: 'w' = Windows, 'm' = Windows Phone 7, 'x' = X360
|
2019-07-28 23:34:55 +02:00
|
|
|
* MonoGame extensions: 'i' = iOS, 'a' = Android, 'X' = MacOSX, 'P' = PS4, 'S' = Switch, etc */
|
2018-02-10 17:45:33 +01:00
|
|
|
platform = read_8bit(0x03,streamFile);
|
|
|
|
big_endian = (platform == 'x');
|
2017-12-26 18:17:59 +01:00
|
|
|
|
2019-01-20 00:42:05 +01:00
|
|
|
if (read_8bit(0x04,streamFile) != 0x04 && /* XNA 3.0? found on Scare Me (XBLIG), no notable diffs */
|
|
|
|
read_8bit(0x04,streamFile) != 0x05) /* XNA 4.0 version */
|
2017-12-26 18:17:59 +01:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
flags = read_8bit(0x05,streamFile);
|
2018-02-10 17:45:33 +01:00
|
|
|
//if (flags & 0x01) goto fail; /* "HiDef profile" content (no actual difference) */
|
2018-02-17 02:09:48 +01:00
|
|
|
if (flags & 0x80) goto fail; /* compressed with LZX/XMemCompress (at 0x0a is decompressed size) */
|
|
|
|
if (flags & 0x40) goto fail; /* compressed with LZ4, MonoGame extension [ex. Square Heroes (PS4)] */
|
2018-02-10 17:45:33 +01:00
|
|
|
|
|
|
|
/* full size */
|
|
|
|
if (read_32bitLE(0x06,streamFile) != get_streamfile_size(streamFile))
|
|
|
|
goto fail;
|
2017-12-26 18:17:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* XNB contains "type reader" class references to parse "shared resource" data (can be any implemented filetype) */
|
|
|
|
{
|
|
|
|
char reader_name[255+1];
|
2018-02-10 17:45:33 +01:00
|
|
|
off_t current_offset = 0x0a;
|
2018-03-29 20:42:52 +02:00
|
|
|
size_t reader_string_len;
|
2017-12-26 18:17:59 +01:00
|
|
|
uint32_t fmt_chunk_size;
|
|
|
|
const char * type_sound = "Microsoft.Xna.Framework.Content.SoundEffectReader"; /* partial "fmt" chunk or XMA */
|
|
|
|
//const char * type_song = "Microsoft.Xna.Framework.Content.SongReader"; /* just references a companion .wma */
|
|
|
|
|
|
|
|
/* type reader count, accept only one for now */
|
2018-02-10 17:45:33 +01:00
|
|
|
if (read_8bit(current_offset++, streamFile) != 1)
|
2017-12-26 18:17:59 +01:00
|
|
|
goto fail;
|
|
|
|
|
2018-02-10 17:45:33 +01:00
|
|
|
reader_string_len = read_8bit(current_offset++, streamFile); /* doesn't count null */
|
2017-12-26 18:17:59 +01:00
|
|
|
if (reader_string_len > 255) goto fail;
|
|
|
|
|
|
|
|
/* check SoundEffect type string */
|
2018-02-10 17:45:33 +01:00
|
|
|
if (read_string(reader_name,reader_string_len+1,current_offset,streamFile) != reader_string_len)
|
2017-12-26 18:17:59 +01:00
|
|
|
goto fail;
|
|
|
|
if ( strcmp(reader_name, type_sound) != 0 )
|
|
|
|
goto fail;
|
2018-02-10 17:45:33 +01:00
|
|
|
current_offset += reader_string_len + 1;
|
2019-07-28 23:34:55 +02:00
|
|
|
current_offset += 0x04; /* reader version */
|
2017-12-26 18:17:59 +01:00
|
|
|
|
|
|
|
/* shared resource count */
|
2018-02-10 17:45:33 +01:00
|
|
|
if (read_8bit(current_offset++, streamFile) != 1)
|
2017-12-26 18:17:59 +01:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* shared resource: partial "fmt" chunk */
|
2018-02-10 17:45:33 +01:00
|
|
|
fmt_chunk_size = read_32bitLE(current_offset, streamFile);
|
2019-07-28 23:34:55 +02:00
|
|
|
current_offset += 0x04;
|
2017-12-26 18:17:59 +01:00
|
|
|
|
|
|
|
{
|
2018-02-10 17:45:33 +01:00
|
|
|
int32_t (*read_32bit)(off_t,STREAMFILE*) = big_endian ? read_32bitBE : read_32bitLE;
|
|
|
|
int16_t (*read_16bit)(off_t,STREAMFILE*) = big_endian ? read_16bitBE : read_16bitLE;
|
|
|
|
|
2019-07-28 23:34:55 +02:00
|
|
|
codec = (uint16_t)read_16bit(current_offset+0x00, streamFile);
|
2018-02-10 17:45:33 +01:00
|
|
|
channel_count = read_16bit(current_offset+0x02, streamFile);
|
|
|
|
sample_rate = read_32bit(current_offset+0x04, streamFile);
|
2018-03-31 13:05:17 +02:00
|
|
|
/* 0x08: byte rate */
|
2019-02-23 03:15:49 +01:00
|
|
|
block_align = read_16bit(current_offset+0x0c, streamFile);
|
2018-02-10 17:45:33 +01:00
|
|
|
bps = read_16bit(current_offset+0x0e, streamFile);
|
|
|
|
|
2019-10-20 16:35:52 +02:00
|
|
|
if (codec == 0x0002) {
|
|
|
|
if (!msadpcm_check_coefs(streamFile, current_offset + 0x14))
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (codec == 0x0166) {
|
2018-02-10 17:45:33 +01:00
|
|
|
xma2_parse_fmt_chunk_extra(streamFile, current_offset, &loop_flag, &num_samples, &loop_start, &loop_end, big_endian);
|
Fix XMA gapless/looping/samples
fixes: standard, wem, xwc, xwb, xnb, xwx, rak, pk, txth, genh, seg, rsd, past, p3d, nub-xma, gtd, gsp, fsb, eaac, cxs, awc, aac
2018-11-18 17:01:31 +01:00
|
|
|
xma_chunk_offset = current_offset;
|
2018-02-10 17:45:33 +01:00
|
|
|
}
|
2019-07-28 23:34:55 +02:00
|
|
|
|
|
|
|
if (codec == 0xFFFF) {
|
|
|
|
if (platform != 'S') goto fail;
|
|
|
|
sample_rate = read_32bit(current_offset+fmt_chunk_size+0x04+0x08, streamFile);
|
|
|
|
}
|
2017-12-26 18:17:59 +01:00
|
|
|
}
|
|
|
|
|
2018-02-10 17:45:33 +01:00
|
|
|
current_offset += fmt_chunk_size;
|
2017-12-26 18:17:59 +01:00
|
|
|
|
2018-02-10 17:45:33 +01:00
|
|
|
data_size = read_32bitLE(current_offset, streamFile);
|
2019-07-28 23:34:55 +02:00
|
|
|
current_offset += 0x04;
|
2017-12-26 18:17:59 +01:00
|
|
|
|
2018-02-10 17:45:33 +01:00
|
|
|
start_offset = current_offset;
|
2017-12-26 18:17:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
|
|
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
|
|
|
vgmstream->sample_rate = sample_rate;
|
|
|
|
vgmstream->meta_type = meta_XNB;
|
|
|
|
|
|
|
|
switch (codec) {
|
2018-02-10 17:45:33 +01:00
|
|
|
case 0x01: /* Dragon's Blade (Android) */
|
2018-03-31 13:05:17 +02:00
|
|
|
/* null in Metagalactic Blitz (PC) */
|
2019-02-23 03:15:49 +01:00
|
|
|
if (!block_align)
|
|
|
|
block_align = (bps == 8 ? 0x01 : 0x02) * channel_count;
|
2018-03-31 13:05:17 +02:00
|
|
|
|
2019-09-14 12:42:08 +02:00
|
|
|
vgmstream->coding_type = bps == 8 ? coding_PCM8_U : coding_PCM16LE;
|
2017-12-26 18:17:59 +01:00
|
|
|
vgmstream->layout_type = layout_interleave;
|
2019-02-23 03:15:49 +01:00
|
|
|
vgmstream->interleave_block_size = block_align / channel_count;
|
2017-12-26 18:17:59 +01:00
|
|
|
vgmstream->num_samples = pcm_bytes_to_samples(data_size, channel_count, bps);
|
|
|
|
break;
|
|
|
|
|
2018-02-10 17:45:33 +01:00
|
|
|
case 0x02: /* White Noise Online (PC) */
|
2019-02-23 03:15:49 +01:00
|
|
|
if (!block_align) goto fail;
|
2017-12-26 18:17:59 +01:00
|
|
|
vgmstream->coding_type = coding_MSADPCM;
|
|
|
|
vgmstream->layout_type = layout_none;
|
2019-10-20 16:35:52 +02:00
|
|
|
vgmstream->frame_size = block_align;
|
2019-02-23 03:15:49 +01:00
|
|
|
vgmstream->num_samples = msadpcm_bytes_to_samples(data_size, block_align, channel_count);
|
2017-12-26 18:17:59 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x11:
|
2019-02-23 03:15:49 +01:00
|
|
|
if (!block_align) goto fail;
|
2017-12-26 18:17:59 +01:00
|
|
|
vgmstream->coding_type = coding_MS_IMA;
|
|
|
|
vgmstream->layout_type = layout_none;
|
2019-02-23 03:15:49 +01:00
|
|
|
vgmstream->interleave_block_size = block_align;
|
|
|
|
vgmstream->num_samples = ms_ima_bytes_to_samples(data_size, block_align, channel_count);
|
2017-12-26 18:17:59 +01:00
|
|
|
break;
|
|
|
|
|
2018-02-10 17:45:33 +01:00
|
|
|
#ifdef VGM_USE_FFMPEG
|
|
|
|
case 0x166: { /* Terraria (X360) */
|
|
|
|
uint8_t buf[0x100];
|
|
|
|
int32_t bytes, block_size, block_count;
|
|
|
|
|
|
|
|
block_size = 0x10000; /* guessed */
|
|
|
|
block_count = data_size / block_size + (data_size % block_size ? 1 : 0);
|
|
|
|
|
|
|
|
bytes = ffmpeg_make_riff_xma2(buf,0x100, num_samples, data_size, vgmstream->channels, vgmstream->sample_rate, block_count, block_size);
|
|
|
|
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;
|
|
|
|
|
|
|
|
vgmstream->num_samples = num_samples;
|
|
|
|
vgmstream->loop_start_sample = loop_start;
|
|
|
|
vgmstream->loop_end_sample = loop_end;
|
Fix XMA gapless/looping/samples
fixes: standard, wem, xwc, xwb, xnb, xwx, rak, pk, txth, genh, seg, rsd, past, p3d, nub-xma, gtd, gsp, fsb, eaac, cxs, awc, aac
2018-11-18 17:01:31 +01:00
|
|
|
|
|
|
|
xma_fix_raw_samples(vgmstream, streamFile, start_offset,data_size, xma_chunk_offset, 1,1);
|
2018-02-10 17:45:33 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2019-07-28 23:34:55 +02:00
|
|
|
case 0xFFFF: /* Eagle Island (Switch) */
|
|
|
|
vgmstream->coding_type = coding_NGC_DSP;
|
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
vgmstream->interleave_block_size = data_size / channel_count;
|
|
|
|
vgmstream->num_samples = read_32bitLE(start_offset + 0x00, streamFile);
|
|
|
|
//vgmstream->num_samples = dsp_bytes_to_samples(data_size - 0x60*channel_count, channel_count);
|
|
|
|
|
|
|
|
dsp_read_coefs(vgmstream,streamFile,start_offset + 0x1c,vgmstream->interleave_block_size,big_endian);
|
|
|
|
dsp_read_hist (vgmstream,streamFile,start_offset + 0x3c,vgmstream->interleave_block_size,big_endian);
|
|
|
|
break;
|
2018-02-10 17:45:33 +01:00
|
|
|
|
2017-12-26 18:17:59 +01:00
|
|
|
default:
|
|
|
|
VGM_LOG("XNB: unknown codec 0x%x\n", codec);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !vgmstream_open_stream(vgmstream,streamFile,start_offset) )
|
|
|
|
goto fail;
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
close_vgmstream(vgmstream);
|
|
|
|
return NULL;
|
|
|
|
}
|