vgmstream/src/meta/ea_swvr.c

204 lines
7.8 KiB
C
Raw Normal View History

2019-12-16 20:31:03 +01:00
#include "meta.h"
#include "../layout/layout.h"
#include "../coding/coding.h"
#include "../util/endianness.h"
2019-12-16 20:31:03 +01:00
/* SWVR - from EA games, demuxed from .av/trk/mis/etc [Future Cop L.A.P.D. (PS/PC), Freekstyle (PS2/GC), EA Sports Supercross (PS)] */
VGMSTREAM* init_vgmstream_ea_swvr(STREAMFILE* sf) {
VGMSTREAM* vgmstream = NULL;
2019-12-16 20:31:03 +01:00
off_t start_offset;
int loop_flag = 0, channels, sample_rate, big_endian, loop_block = 0;
2019-12-16 20:31:03 +01:00
coding_t coding;
uint32_t block_id;
read_u32_t read_u32 = NULL;
read_u16_t read_u16 = NULL;
int total_subsongs, target_subsong = sf->stream_index;
2019-12-16 20:31:03 +01:00
/* checks */
/* Files have no actual audio headers, so we inspect the first block for known values.
* Freekstyle uses multiblocks/subsongs (though some subsongs may be clones?) */
2019-12-16 20:31:03 +01:00
/* blocks ids are in machine endianness */
if (read_u32be(0x00,sf) == get_id32be("RVWS")) { /* PS1/PS2/PC */
2019-12-16 20:31:03 +01:00
big_endian = 0;
read_u32 = read_u32le;
read_u16 = read_u16le;
start_offset = read_u32(0x04, sf);
/* 0x08: null */
loop_block = read_u32(0x0c, sf); /* uncommon [NASCAR Racing (PS1), Rumble Racing (PS2)] */
2019-12-16 20:31:03 +01:00
}
else if (read_u32be(0x00,sf) == get_id32be("SWVR")) { /* GC */
2019-12-16 20:31:03 +01:00
big_endian = 1;
read_u32 = read_u32be;
read_u16 = read_u16be;
start_offset = read_u32(0x04, sf);
2019-12-16 20:31:03 +01:00
}
else if (read_u32be(0x00,sf) == get_id32be("MGAV")) { /* Freekstyle (PS2) raw movies */
2019-12-16 20:31:03 +01:00
big_endian = 0;
read_u32 = read_u32le;
read_u16 = read_u16le;
2019-12-16 20:31:03 +01:00
start_offset = 0x00;
}
else if (read_u32be(0x00,sf) == get_id32be("DSPM")) { /* Freekstyle (GC) raw movies */
2019-12-16 20:31:03 +01:00
big_endian = 1;
read_u32 = read_u32be;
read_u16 = read_u16be;
2019-12-16 20:31:03 +01:00
start_offset = 0x00;
}
else {
goto fail;
}
/* .stream: common (found inside files)
* .str: shortened, probably unnecessary */
if (!check_extensions(sf,"stream,str"))
goto fail;
2019-12-16 20:31:03 +01:00
if (read_u32(start_offset+0x00, sf) == get_id32be("PADD")) /* Freekstyle */
start_offset += read_u32(start_offset+0x04, sf);
if (read_u32(start_offset+0x00, sf) == get_id32be("FILL")) /* Freekstyle */
start_offset += read_u32(start_offset+0x04, sf);
2019-12-16 20:31:03 +01:00
total_subsongs = 1;
block_id = read_u32(start_offset, sf);
/* value after block id (usually at 0x38) is number of blocks of 0x6000 (results in file size, including FILLs) */
2019-12-16 20:31:03 +01:00
/* intended sample rate for PSX music (verified in emus) should be 14260, but is found in ELF as pitch value
* (ex. Nascar Rumble 0x052C in SLUS_010.68 at 0x000143BC): 0x052C * 44100 / 4096 ~= 14254.98046875hz
* Future Cop PSX pitch looks similar (comparing vs recordings). */
2019-12-16 20:31:03 +01:00
switch(block_id) {
case 0x5641474D: /* "VAGM" (stereo music) */
2019-12-16 20:31:03 +01:00
coding = coding_PSX;
if (read_u16(start_offset+0x1a, sf) == 0x0024) {
total_subsongs = read_u32(start_offset+0x0c, sf)+1;
sample_rate = 22050; /* Freekstyle (PS2) */
2019-12-16 20:31:03 +01:00
}
else {
sample_rate = 1324 * 44100 / 4096; /* ~14254 [Future Cop (PS1), Nascar Rumble (PS1), EA Sports Motocross (PS1)] */
2019-12-16 20:31:03 +01:00
}
channels = 2;
2019-12-16 20:31:03 +01:00
break;
case 0x56414742: /* "VAGB" (mono sfx/voices)*/
2019-12-16 20:31:03 +01:00
coding = coding_PSX;
if (read_u16(start_offset+0x1a, sf) == 0x6400) {
sample_rate = 22050; /* Freekstyle (PS2) */
2019-12-16 20:31:03 +01:00
}
else {
/* approximate as pitches vary per file (ex. Nascar Rumble: engine=3779, heli=22050, commentary=11050) */
sample_rate = 1080 * 44100 / 4096; /* ~11627 [EA Sports Motocross (PS1)] */
2019-12-16 20:31:03 +01:00
}
channels = 1;
2019-12-16 20:31:03 +01:00
break;
case 0x4453504D: /* "DSPM" (stereo music) */
2019-12-16 20:31:03 +01:00
coding = coding_NGC_DSP;
total_subsongs = read_u32(start_offset+0x0c, sf)+1;
sample_rate = 22050; /* Freekstyle (GC) */
channels = 2;
2019-12-16 20:31:03 +01:00
break;
case 0x44535042: /* "DSPB" (mono voices/sfx) */
2019-12-16 20:31:03 +01:00
coding = coding_NGC_DSP;
channels = 1;
sample_rate = 22050; /* Freekstyle (GC) */
2019-12-16 20:31:03 +01:00
break;
case 0x4D534943: /* "MSIC" (stereo music) */
2019-12-16 20:31:03 +01:00
coding = coding_PCM8_U_int;
channels = 2;
sample_rate = 14291; /* assumed, by comparing vs PSX output [Future Cop (PC)] */
2019-12-16 20:31:03 +01:00
break;
case 0x53484F43: /* "SHOC" (a generic block but hopefully has PC sounds) */
if (read_u32(start_offset+0x10, sf) == get_id32be("SHDR")) { /* Future Cop (PC) */
/* there is a mini header? after SHDR
* 0x00: 5
* 0x04: "snds"
* 0x08: channels?
* 0x0c: data size without blocks/padding
* 0x10: null
* 0x14: null
* 0x18: null
* 0x1c: 1
* 0x20: 1
* 0x24: 0x4F430000 */
if (read_u32(start_offset+0x18, sf) != get_id32be("snds"))
goto fail;
coding = coding_PCM8_U_int;
channels = 1;
sample_rate = 22050; /* assumed */
2019-12-27 01:48:29 +01:00
}
else {
//todo
/* The Lord of the Rings - The Return of the King (PC) uses IMA for sfx (in non-RVWS chunks)
* and some unknown codec for streams (mixed with strings and cutscene(?) commands) */
2019-12-27 01:48:29 +01:00
goto fail;
}
2019-12-16 20:31:03 +01:00
break;
default:
VGM_LOG("EA SWVR: unknown block id\n");
goto fail;
}
if (target_subsong == 0) target_subsong = 1;
if (target_subsong < 0 || target_subsong > total_subsongs || total_subsongs < 1) goto fail;
loop_flag = (loop_block > 0);//(channels > 1); /* some Future Cop LAPD tracks repeat but other games have fadeouts */
2019-12-16 20:31:03 +01:00
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channels, loop_flag);
2019-12-16 20:31:03 +01:00
if (!vgmstream) goto fail;
vgmstream->meta_type = meta_EA_SWVR;
vgmstream->sample_rate = sample_rate;
vgmstream->codec_endian = big_endian;
vgmstream->num_streams = total_subsongs;
vgmstream->stream_size = get_streamfile_size(sf) / total_subsongs; /* approx... */
2019-12-16 20:31:03 +01:00
vgmstream->coding_type = coding;
vgmstream->layout_type = layout_blocked_ea_swvr;
/* DSP coefs are loaded per block */
/* some files (voices etc) decode with pops but seems a mastering problem */
if (!vgmstream_open_stream(vgmstream,sf,start_offset))
2019-12-16 20:31:03 +01:00
goto fail;
/* calc num_samples manually */
{
int block, num_samples;
2019-12-16 20:31:03 +01:00
vgmstream->stream_index = target_subsong; /* needed to skip other subsong-blocks */
vgmstream->next_block_offset = start_offset;
block = 0;
2019-12-16 20:31:03 +01:00
do {
block_update(vgmstream->next_block_offset,vgmstream);
switch(vgmstream->coding_type) {
case coding_PSX: num_samples = ps_bytes_to_samples(vgmstream->current_block_size,1); break;
case coding_NGC_DSP: num_samples = dsp_bytes_to_samples(vgmstream->current_block_size,1); break;
case coding_PCM8_U_int: num_samples = pcm_bytes_to_samples(vgmstream->current_block_size,1,8); break;
default: num_samples = 0; break;
}
vgmstream->num_samples += num_samples;
/* check loop on data blocks */
if (num_samples) {
block++;
if (loop_block == block) /* 1=first */
vgmstream->loop_start_sample = vgmstream->num_samples;
}
2019-12-16 20:31:03 +01:00
}
while (vgmstream->next_block_offset < get_streamfile_size(sf));
2019-12-16 20:31:03 +01:00
block_update(start_offset, vgmstream);
}
vgmstream->loop_end_sample = vgmstream->num_samples;
2019-12-16 20:31:03 +01:00
return vgmstream;
fail:
close_vgmstream(vgmstream);
return NULL;
}