Add .idwav/idmsf/idxma [Doom 3 BFG Edition (PC/PS3/X360)]

This commit is contained in:
bnnm 2019-08-02 21:11:55 +02:00
parent 52dd56bc1a
commit 4f5d622edc
12 changed files with 304 additions and 4 deletions

View File

@ -361,6 +361,7 @@ void xma2_parse_fmt_chunk_extra(STREAMFILE *streamFile, off_t chunk_offset, int
void xma2_parse_xma2_chunk(STREAMFILE *streamFile, off_t chunk_offset, int * channels, int * sample_rate, int * loop_flag, int32_t * num_samples, int32_t * loop_start_sample, int32_t * loop_end_sample);
void xma_fix_raw_samples(VGMSTREAM *vgmstream, STREAMFILE*streamFile, off_t stream_offset, size_t stream_size, off_t chunk_offset, int fix_num_samples, int fix_loop_samples);
void xma_fix_raw_samples_hb(VGMSTREAM *vgmstream, STREAMFILE *headerFile, STREAMFILE *bodyFile, off_t stream_offset, size_t stream_size, off_t chunk_offset, int fix_num_samples, int fix_loop_samples);
void xma_fix_raw_samples_ch(VGMSTREAM *vgmstream, STREAMFILE*streamFile, off_t stream_offset, size_t stream_size, int channel_per_stream, int fix_num_samples, int fix_loop_samples);
int riff_get_fact_skip_samples(STREAMFILE * streamFile, off_t start_offset);

View File

@ -871,6 +871,11 @@ void xma_fix_raw_samples_ch(VGMSTREAM *vgmstream, STREAMFILE*streamFile, off_t s
#endif
}
void xma_fix_raw_samples_hb(VGMSTREAM *vgmstream, STREAMFILE *headerFile, STREAMFILE *bodyFile, off_t stream_offset, size_t stream_size, off_t chunk_offset, int fix_num_samples, int fix_loop_samples) {
int channels_per_stream = xma_get_channels_per_stream(headerFile, chunk_offset, vgmstream->channels);
xma_fix_raw_samples_ch(vgmstream, bodyFile, stream_offset, stream_size, channels_per_stream, fix_num_samples, fix_loop_samples);
}
void xma_fix_raw_samples(VGMSTREAM *vgmstream, STREAMFILE*streamFile, off_t stream_offset, size_t stream_size, off_t chunk_offset, int fix_num_samples, int fix_loop_samples) {
int channels_per_stream = xma_get_channels_per_stream(streamFile, chunk_offset, vgmstream->channels);
xma_fix_raw_samples_ch(vgmstream, streamFile, stream_offset, stream_size, channels_per_stream, fix_num_samples, fix_loop_samples);

View File

@ -90,6 +90,8 @@ int mpeg_custom_setup_init_default(STREAMFILE *streamFile, off_t start_offset, m
case MPEG_XVAG: /* set in header and needed for gapless looping */
data->skip_samples = data->config.skip_samples; break;
case MPEG_STANDARD:
data->skip_samples = data->config.skip_samples; break;
default:
break;
}

View File

@ -189,9 +189,12 @@ static const char* extension_list[] = {
"iab",
"iadp",
"idmsf",
"idsp",
"idvi", //fake extension/header id for .pcm (renamed, to be removed)
"idwav",
"idx",
"idxma",
"ikm",
"ild",
"ilv", //txth/reserved [Star Wars Episode III (PS2)]
@ -1202,6 +1205,7 @@ static const meta_info meta_info_list[] = {
{meta_BWAV, "Nintendo BWAV header"},
{meta_RAD, "Traveller's Tales .RAD header"},
{meta_SMACKER, "RAD Game Tools SMACKER header"},
{meta_MZRT, "id Software MZRT header"},
};

View File

@ -280,6 +280,10 @@
RelativePath=".\meta\mta2_streamfile.h"
>
</File>
<File
RelativePath=".\meta\mzrt_streamfile.h"
>
</File>
<File
RelativePath=".\meta\ogg_vorbis_streamfile.h"
>
@ -802,10 +806,14 @@
RelativePath=".\meta\musx.c"
>
</File>
<File
RelativePath=".\meta\myspd.c"
>
</File>
<File
RelativePath=".\meta\myspd.c"
>
</File>
<File
RelativePath=".\meta\mzrt.c"
>
</File>
<File
RelativePath=".\meta\naac.c"
>

View File

@ -115,6 +115,7 @@
<ClInclude Include="meta\ppst_streamfile.h" />
<ClInclude Include="meta\vsv_streamfile.h" />
<ClInclude Include="meta\mta2_streamfile.h" />
<ClInclude Include="meta\mzrt_streamfile.h" />
<ClInclude Include="meta\ogg_vorbis_streamfile.h" />
<ClInclude Include="meta\opus_interleave_streamfile.h" />
<ClInclude Include="meta\sfh_streamfile.h" />
@ -317,6 +318,7 @@
<ClCompile Include="meta\musc.c" />
<ClCompile Include="meta\musx.c" />
<ClCompile Include="meta\myspd.c" />
<ClCompile Include="meta\mzrt.c" />
<ClCompile Include="meta\naac.c" />
<ClCompile Include="meta\naomi_adpcm.c" />
<ClCompile Include="meta\naomi_spsd.c" />

View File

@ -107,6 +107,9 @@
<ClInclude Include="meta\mta2_streamfile.h">
<Filter>meta\Header Files</Filter>
</ClInclude>
<ClInclude Include="meta\mzrt_streamfile.h">
<Filter>meta\Header Files</Filter>
</ClInclude>
<ClInclude Include="meta\ogg_vorbis_streamfile.h">
<Filter>meta\Header Files</Filter>
</ClInclude>
@ -493,6 +496,9 @@
<ClCompile Include="meta\myspd.c">
<Filter>meta\Source Files</Filter>
</ClCompile>
<ClCompile Include="meta\mzrt.c">
<Filter>meta\Source Files</Filter>
</ClCompile>
<ClCompile Include="meta\naac.c">
<Filter>meta\Source Files</Filter>
</ClCompile>

View File

@ -869,4 +869,6 @@ VGMSTREAM * init_vgmstream_rad(STREAMFILE * streamFile);
VGMSTREAM * init_vgmstream_smk(STREAMFILE * streamFile);
VGMSTREAM * init_vgmstream_mzrt(STREAMFILE * streamFile);
#endif /*_META_H*/

144
src/meta/mzrt.c Normal file
View File

@ -0,0 +1,144 @@
#include "meta.h"
#include "../coding/coding.h"
#include "mzrt_streamfile.h"
/* mzrt - idTech "4.5" audio found in .resource bigfiles (w/ internal filenames) [Doom 3 BFG edition (PC/PS3/X360)] */
VGMSTREAM * init_vgmstream_mzrt(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL;
off_t start_offset;
int loop_flag = 0, channel_count, codec, sample_rate, block_size = 0, bps = 0, num_samples;
STREAMFILE *temp_streamFile = NULL;
/* checks */
if (!check_extensions(streamFile, "idwav,idmsf,idxma"))
goto fail;
if (read_32bitBE(0x00,streamFile) != 0x6D7A7274) /* "mzrt" */
goto fail;
/* this format is bizarrely mis-aligned (and mis-designed too) */
num_samples = read_32bitBE(0x11,streamFile);
codec = read_16bitLE(0x15,streamFile);
switch(codec) {
case 0x0001:
case 0x0002:
case 0x0166:
channel_count = read_16bitLE(0x17,streamFile);
sample_rate = read_32bitLE(0x19, streamFile);
block_size = read_16bitLE(0x21, streamFile);
bps = read_16bitLE(0x23,streamFile);
start_offset = 0x25;
break;
case 0x0000:
sample_rate = read_32bitBE(0x1D, streamFile);
channel_count = read_32bitBE(0x21, streamFile);
start_offset = 0x29;
break;
default:
goto fail;
}
/* skip fmt-extra data */
if (codec == 0x0002 || codec == 0x0166) {
start_offset += 0x02 + read_16bitLE(start_offset, streamFile);
}
/* skip unknown table */
if (codec == 0x0000) {
start_offset += 0x04 + read_32bitBE(start_offset, streamFile) * 0x04;
}
/* skip unknown table */
start_offset += 0x04 + read_32bitBE(start_offset, streamFile);
/* skip block info */
if (codec != 0x0000) {
/* 0x00: de-blocked size
* 0x04: block count*/
start_offset += 0x08;
/* idwav only uses 1 super-block though */
temp_streamFile = setup_mzrt_streamfile(streamFile, start_offset);
if (!temp_streamFile) goto fail;
}
else {
/* 0x00: de-blocked size */
start_offset += 0x04;
}
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count, loop_flag);
if (!vgmstream) goto fail;
vgmstream->meta_type = meta_MZRT;
vgmstream->sample_rate = sample_rate;
vgmstream->num_samples = num_samples;
switch(codec) {
case 0x0001:
if (bps != 16) goto fail;
vgmstream->coding_type = coding_PCM16LE;
vgmstream->layout_type = layout_interleave;
vgmstream->interleave_block_size = block_size / channel_count;
break;
case 0x0002:
if (bps != 4) goto fail;
vgmstream->coding_type = coding_MSADPCM;
vgmstream->layout_type = layout_none;
vgmstream->interleave_block_size = block_size;
break;
#ifdef VGM_USE_FFMPEG
case 0x0166: {
uint8_t buf[0x100];
int bytes;
size_t stream_size = get_streamfile_size(temp_streamFile);
bytes = ffmpeg_make_riff_xma_from_fmt_chunk(buf,sizeof(buf), 0x15,0x34, stream_size, streamFile, 0);
vgmstream->codec_data = init_ffmpeg_header_offset(temp_streamFile, buf,bytes, 0x00,stream_size);
if (!vgmstream->codec_data) goto fail;
vgmstream->coding_type = coding_FFmpeg;
vgmstream->layout_type = layout_none;
xma_fix_raw_samples_hb(vgmstream, streamFile, temp_streamFile, 0x00,stream_size, 0x15, 0,0);
break;
}
#endif
#ifdef VGM_USE_MPEG
case 0x0000: {
mpeg_custom_config cfg = {0};
cfg.skip_samples = 576; /* assumed */
vgmstream->codec_data = init_mpeg_custom(streamFile, start_offset, &vgmstream->coding_type, vgmstream->channels, MPEG_STANDARD, &cfg);
if (!vgmstream->codec_data) goto fail;
vgmstream->layout_type = layout_none;
break;
}
#endif
default:
goto fail;
}
if (!vgmstream_open_stream(vgmstream,temp_streamFile == NULL ? streamFile : temp_streamFile,temp_streamFile == NULL ? start_offset : 0x00))
goto fail;
close_streamfile(temp_streamFile);
return vgmstream;
fail:
close_streamfile(temp_streamFile);
close_vgmstream(vgmstream);
return NULL;
}

124
src/meta/mzrt_streamfile.h Normal file
View File

@ -0,0 +1,124 @@
#ifndef _MZRT_STREAMFILE_H_
#define _MZRT_STREAMFILE_H_
#include "../streamfile.h"
typedef struct {
/* config */
off_t stream_offset;
size_t stream_size;
/* state */
off_t logical_offset; /* fake offset */
off_t physical_offset; /* actual offset */
size_t block_size; /* current size */
size_t skip_size; /* size from block start to reach data */
size_t data_size; /* usable size in a block */
size_t logical_size;
} mzrt_io_data;
static size_t mzrt_io_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length, mzrt_io_data* data) {
size_t total_read = 0;
/* re-start when previous offset (can't map logical<>physical offsets) */
if (data->logical_offset < 0 || offset < data->logical_offset) {
data->physical_offset = data->stream_offset;
data->logical_offset = 0x00;
data->data_size = 0;
}
/* read blocks */
while (length > 0) {
/* ignore EOF */
if (offset < 0 || data->physical_offset >= data->stream_offset + data->stream_size) {
break;
}
/* process new block */
if (data->data_size == 0) {
/* 0x00: samples in this block */
data->data_size = read_32bitBE(data->stream_offset + 0x04, streamfile);
data->skip_size = 0x08;
data->block_size = data->skip_size + data->data_size;
}
/* move to next block */
if (data->data_size == 0 || offset >= data->logical_offset + data->data_size) {
data->physical_offset += data->block_size;
data->logical_offset += data->data_size;
data->data_size = 0;
continue;
}
/* read data */
{
size_t bytes_consumed, bytes_done, to_read;
bytes_consumed = offset - data->logical_offset;
to_read = data->data_size - bytes_consumed;
if (to_read > length)
to_read = length;
bytes_done = read_streamfile(dest, data->physical_offset + data->skip_size + bytes_consumed, to_read, streamfile);
total_read += bytes_done;
dest += bytes_done;
offset += bytes_done;
length -= bytes_done;
if (bytes_done != to_read || bytes_done == 0) {
break; /* error/EOF */
}
}
}
return total_read;
}
static size_t mzrt_io_size(STREAMFILE *streamfile, mzrt_io_data* data) {
uint8_t buf[1];
if (data->logical_size)
return data->logical_size;
/* force a fake read at max offset, to get max logical_offset (will be reset next read) */
mzrt_io_read(streamfile, buf, 0x7FFFFFFF, 1, data);
data->logical_size = data->logical_offset;
return data->logical_size;
}
/* Handles deinterleaving of MZRT blocked streams */
static STREAMFILE* setup_mzrt_streamfile(STREAMFILE *streamFile, off_t stream_offset) {
STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL;
mzrt_io_data io_data = {0};
size_t io_data_size = sizeof(mzrt_io_data);
io_data.stream_offset = stream_offset;
io_data.stream_size = get_streamfile_size(streamFile) - stream_offset;
io_data.logical_offset = -1; /* force phys offset reset */
/* setup subfile */
new_streamFile = open_wrap_streamfile(streamFile);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
new_streamFile = open_io_streamfile(new_streamFile, &io_data,io_data_size, mzrt_io_read,mzrt_io_size);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
new_streamFile = open_buffer_streamfile(new_streamFile,0);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
return temp_streamFile;
fail:
close_streamfile(temp_streamFile);
return NULL;
}
#endif /* _MZRT_STREAMFILE_H_ */

View File

@ -486,6 +486,7 @@ VGMSTREAM * (*init_vgmstream_functions[])(STREAMFILE *streamFile) = {
init_vgmstream_acb,
init_vgmstream_rad,
init_vgmstream_smk,
init_vgmstream_mzrt,
/* lowest priority metas (should go after all metas, and TXTH should go before raw formats) */
init_vgmstream_txth, /* proper parsers should supersede TXTH, once added */

View File

@ -733,6 +733,7 @@ typedef enum {
meta_BWAV,
meta_RAD,
meta_SMACKER,
meta_MZRT,
} meta_t;