2015-01-22 01:28:43 +01:00
|
|
|
#include "meta.h"
|
|
|
|
#include "../util.h"
|
2017-03-04 02:07:48 +01:00
|
|
|
#include "../coding/coding.h"
|
2015-01-22 01:28:43 +01:00
|
|
|
|
2017-03-04 02:07:48 +01:00
|
|
|
/* Capcom MADP - found in Capcom 3DS games */
|
2015-01-22 01:28:43 +01:00
|
|
|
VGMSTREAM * init_vgmstream_mca(STREAMFILE *streamFile) {
|
|
|
|
VGMSTREAM * vgmstream = NULL;
|
2017-03-04 02:07:48 +01:00
|
|
|
int channel_count, loop_flag, version;
|
2017-01-09 18:24:44 +01:00
|
|
|
size_t head_size, data_size, file_size;
|
2016-12-16 20:34:44 +01:00
|
|
|
off_t start_offset, coef_offset, coef_start, coef_shift;
|
2015-01-22 02:31:44 +01:00
|
|
|
int coef_spacing;
|
2015-01-22 01:28:43 +01:00
|
|
|
|
|
|
|
/* check extension, case insensitive */
|
2017-03-04 02:07:48 +01:00
|
|
|
if (!check_extensions(streamFile,"mca"))
|
2015-01-22 01:28:43 +01:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* check header */
|
|
|
|
if ((uint32_t)read_32bitBE(0, streamFile) != 0x4D414450) /* "MADP" */
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
channel_count = read_8bit(0x8, streamFile);
|
2016-12-16 20:34:44 +01:00
|
|
|
if (channel_count < 1) goto fail;
|
|
|
|
loop_flag = read_32bitLE(0x18, streamFile) > 0;
|
2015-01-22 01:28:43 +01:00
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
|
|
|
vgmstream = allocate_vgmstream(channel_count, loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
2016-12-16 20:34:44 +01:00
|
|
|
vgmstream->interleave_block_size = read_16bitLE(0xa, streamFile); /* guessed, only seen 0x100 */
|
2015-01-22 01:28:43 +01:00
|
|
|
vgmstream->num_samples = read_32bitLE(0xc, streamFile);
|
|
|
|
vgmstream->sample_rate = (uint16_t)read_16bitLE(0x10, streamFile);
|
|
|
|
vgmstream->loop_start_sample = read_32bitLE(0x14, streamFile);
|
|
|
|
vgmstream->loop_end_sample = read_32bitLE(0x18, streamFile);
|
|
|
|
|
2017-03-31 22:38:45 +02:00
|
|
|
if (vgmstream->loop_end_sample > vgmstream->num_samples) /* some MH3U songs, somehow */
|
|
|
|
vgmstream->loop_end_sample = vgmstream->num_samples;
|
|
|
|
|
2016-12-16 20:34:44 +01:00
|
|
|
vgmstream->coding_type = coding_NGC_DSP;
|
2017-03-04 02:07:48 +01:00
|
|
|
vgmstream->layout_type = channel_count == 1 ? layout_none : layout_interleave;
|
2015-01-22 01:28:43 +01:00
|
|
|
vgmstream->meta_type = meta_MCA;
|
|
|
|
|
2016-12-16 20:34:44 +01:00
|
|
|
|
|
|
|
/* find data/coef offsets (guessed, formula may change with version) */
|
|
|
|
version = read_16bitLE(0x04, streamFile);
|
|
|
|
coef_spacing = 0x30;
|
|
|
|
data_size = read_32bitLE(0x20, streamFile);
|
|
|
|
|
|
|
|
if (version <= 0x3) { /* v3: Resident Evil Mercenaries 3D, Super Street Fighter IV 3D */
|
|
|
|
head_size = get_streamfile_size(streamFile) - data_size; /* probably 0x2c + 0x30*ch */
|
|
|
|
coef_shift = 0x0;
|
|
|
|
coef_start = head_size - coef_spacing * channel_count;
|
|
|
|
|
|
|
|
start_offset = head_size;
|
|
|
|
coef_offset = coef_start + coef_shift * 0x14;
|
|
|
|
|
|
|
|
} else if (version == 0x4) { /* v4: EX Troopers, Ace Attourney 5 */
|
|
|
|
head_size = read_16bitLE(0x1c, streamFile);
|
|
|
|
coef_shift = read_16bitLE(0x28, streamFile);
|
|
|
|
coef_start = head_size - coef_spacing * channel_count;
|
|
|
|
|
2017-03-31 22:38:45 +02:00
|
|
|
start_offset = get_streamfile_size(streamFile) - data_size; /* usually head_size but not for some MH3U songs */
|
2016-12-16 20:34:44 +01:00
|
|
|
coef_offset = coef_start + coef_shift * 0x14;
|
|
|
|
|
|
|
|
} else { /* v5: Ace Attourney 6, Monster Hunter Generations, v6+? */
|
|
|
|
head_size = read_16bitLE(0x1c, streamFile); /* partial size */
|
|
|
|
coef_shift = read_16bitLE(0x28, streamFile);
|
|
|
|
coef_start = head_size - coef_spacing * channel_count;
|
|
|
|
|
|
|
|
start_offset = read_32bitLE(coef_start - 0x4, streamFile);
|
|
|
|
coef_offset = coef_start + coef_shift * 0x14;
|
|
|
|
}
|
|
|
|
|
2017-03-04 02:07:48 +01:00
|
|
|
/* sanity check (for bad rips with the header manually truncated to in attempt to "fix" v5 headers) */
|
2017-01-09 18:24:44 +01:00
|
|
|
file_size = get_streamfile_size(streamFile);
|
|
|
|
|
|
|
|
if (start_offset + data_size > file_size) {
|
|
|
|
if (head_size + data_size > file_size)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
start_offset = file_size - data_size;
|
|
|
|
}
|
|
|
|
|
2017-03-04 02:07:48 +01:00
|
|
|
/* set up ADPCM coefs */
|
|
|
|
dsp_read_coefs_le(vgmstream, streamFile, coef_offset, coef_spacing);
|
|
|
|
|
|
|
|
/* open the file for reading */
|
|
|
|
if ( !vgmstream_open_stream(vgmstream,streamFile, start_offset) )
|
|
|
|
goto fail;
|
2015-01-22 01:28:43 +01:00
|
|
|
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
2017-03-04 02:07:48 +01:00
|
|
|
close_vgmstream(vgmstream);
|
2015-01-22 01:28:43 +01:00
|
|
|
return NULL;
|
|
|
|
}
|