Fix some EALayer3 skips [Need For Speed Rivals (PS4)]

Uses a custom streamfile to remove .sps blocks for EALayer3, as V2P
needs it since frames may be split between blocks.
This commit is contained in:
bnnm 2018-04-19 22:26:46 +02:00
parent 2268d9c5bf
commit 7d992af25e
2 changed files with 139 additions and 43 deletions

View File

@ -1,7 +1,7 @@
#include "meta.h" #include "meta.h"
#include "../layout/layout.h" #include "../layout/layout.h"
#include "../coding/coding.h" #include "../coding/coding.h"
#include "ea_eaac_eatrax_streamfile.h" #include "ea_eaac_streamfile.h"
/* EAAudioCore formats, EA's current audio middleware */ /* EAAudioCore formats, EA's current audio middleware */
@ -129,7 +129,7 @@ static VGMSTREAM * init_vgmstream_eaaudiocore_header(STREAMFILE * streamHead, ST
/* rest is optional, depends on flags header used (ex. SNU and SPS may have bigger headers): /* rest is optional, depends on flags header used (ex. SNU and SPS may have bigger headers):
* &0x20: 1 int (usually 0x00), &0x00/40: nothing, &0x60: 2 ints (usually 0x00 and 0x14) */ * &0x20: 1 int (usually 0x00), &0x00/40: nothing, &0x60: 2 ints (usually 0x00 and 0x14) */
/* V0: SNR+SNS, V1: SPR+SPS (not apparent differences) */ /* V0: SNR+SNS, V1: SPR+SPS (not apparent differences, other than the block flags used) */
if (version != 0 && version != 1) { if (version != 0 && version != 1) {
VGM_LOG("EA SNS/SPS: unknown version\n"); VGM_LOG("EA SNS/SPS: unknown version\n");
goto fail; goto fail;
@ -219,14 +219,20 @@ static VGMSTREAM * init_vgmstream_eaaudiocore_header(STREAMFILE * streamHead, ST
case 0x06: /* "L32P": EALayer3 v2 "PCM" [Battlefield 1943 (PS3)] */ case 0x06: /* "L32P": EALayer3 v2 "PCM" [Battlefield 1943 (PS3)] */
case 0x07: { /* "L32S": EALayer3 v2 "Spike" [Dante's Inferno (PS3)] */ case 0x07: { /* "L32S": EALayer3 v2 "Spike" [Dante's Inferno (PS3)] */
mpeg_custom_config cfg = {0}; mpeg_custom_config cfg = {0};
off_t mpeg_start_offset = start_offset + 0x08;
mpeg_custom_t type = (codec == 0x05 ? MPEG_EAL31b : (codec == 0x06) ? MPEG_EAL32P : MPEG_EAL32S); mpeg_custom_t type = (codec == 0x05 ? MPEG_EAL31b : (codec == 0x06) ? MPEG_EAL32P : MPEG_EAL32S);
/* layout is still blocks, but should work fine with the custom mpeg decoder */ /* remove blocks on reads for some edge cases in L32P and to properly apply discard modes
vgmstream->codec_data = init_mpeg_custom(streamData, mpeg_start_offset, &vgmstream->coding_type, vgmstream->channels, type, &cfg); * (otherwise, and removing discards, it'd work with layout_blocked_ea_sns) */
if (!vgmstream->codec_data) goto fail; temp_streamFile = setup_eaac_streamfile(streamData, version, codec, start_offset, 0);
if (!temp_streamFile) goto fail;
start_offset = 0x00; /* must point to the custom streamfile's beginning */
/* layout is still blocks, but should work fine with the custom mpeg decoder */
vgmstream->codec_data = init_mpeg_custom(temp_streamFile, start_offset, &vgmstream->coding_type, vgmstream->channels, type, &cfg);
if (!vgmstream->codec_data) goto fail;
vgmstream->layout_type = layout_none;
vgmstream->layout_type = layout_blocked_ea_sns;
break; break;
} }
#endif #endif
@ -253,10 +259,10 @@ static VGMSTREAM * init_vgmstream_eaaudiocore_header(STREAMFILE * streamHead, ST
vgmstream->layout_type = layout_none; vgmstream->layout_type = layout_none;
/* EATrax is "buffered" ATRAC9, uses custom IO since it's kind of complex to add to the decoder */ /* EATrax is "buffered" ATRAC9, uses custom IO since it's kind of complex to add to the decoder */
start_offset = 0x00; /* must point to header start */ temp_streamFile = setup_eaac_streamfile(streamData, version, codec, start_offset, total_size);
temp_streamFile = setup_eatrax_streamfile(streamData, total_size);
if (!temp_streamFile) goto fail; if (!temp_streamFile) goto fail;
start_offset = 0x00; /* must point to the custom streamfile's beginning */
break; break;
} }
#endif #endif
@ -278,11 +284,10 @@ static VGMSTREAM * init_vgmstream_eaaudiocore_header(STREAMFILE * streamHead, ST
if (!vgmstream_open_stream(vgmstream,temp_streamFile ? temp_streamFile : streamData,start_offset)) if (!vgmstream_open_stream(vgmstream,temp_streamFile ? temp_streamFile : streamData,start_offset))
goto fail; goto fail;
close_streamfile(temp_streamFile);
if (vgmstream->layout_type == layout_blocked_ea_sns) if (vgmstream->layout_type == layout_blocked_ea_sns)
block_update_ea_sns(start_offset, vgmstream); block_update_ea_sns(start_offset, vgmstream);
close_streamfile(temp_streamFile);
return vgmstream; return vgmstream;
fail: fail:

View File

@ -1,5 +1,5 @@
#ifndef _EA_EAAC_EATRAX_STREAMFILE_H_ #ifndef _EA_EAAC_STREAMFILE_H_
#define _EA_EAAC_EATRAX_STREAMFILE_H_ #define _EA_EAAC_STREAMFILE_H_
#include "../streamfile.h" #include "../streamfile.h"
@ -8,25 +8,28 @@ typedef struct {
off_t logical_offset; /* offset that corresponds to physical_offset */ off_t logical_offset; /* offset that corresponds to physical_offset */
off_t physical_offset; /* actual file offset */ off_t physical_offset; /* actual file offset */
/* config */
int version;
int codec;
off_t start_offset;
size_t total_size; /* size of the resulting substream */ size_t total_size; /* size of the resulting substream */
} eatrax_io_data; } eaac_io_data;
/* Reads skipping EA's block headers, so the resulting data is smaller than physical data, /* Reads skipping EA's block headers, so the resulting data is smaller or larger than physical data.
* and physical_offset is bigger than offset (ex. reads at offset = 0x00 could be at physical_offset = 0x10).
* physical/logical_offset should always be at the start of a block and only advance when a block is fully done */ * physical/logical_offset should always be at the start of a block and only advance when a block is fully done */
static size_t eatrax_io_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length, eatrax_io_data* data) { static size_t eaac_io_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length, eaac_io_data* data) {
size_t total_read = 0; size_t total_read = 0;
/* ignore bad reads */ /* ignore bad reads */
if (offset < 0 || offset > data->total_size) { if (offset < 0 || offset > data->total_size) {
return 0; return total_read;
} }
/* previous offset: re-start as we can't map logical<>physical offsets /* previous offset: re-start as we can't map logical<>physical offsets
* (kinda slow as it trashes buffers, but shouldn't happen often) */ * (kinda slow as it trashes buffers, but shouldn't happen often) */
if (offset < data->logical_offset) { if (offset < data->logical_offset) {
data->physical_offset = 0x00; data->physical_offset = data->start_offset;
data->logical_offset = 0x00; data->logical_offset = 0x00;
} }
@ -34,26 +37,43 @@ static size_t eatrax_io_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset
while (length > 0) { while (length > 0) {
size_t to_read, bytes_read; size_t to_read, bytes_read;
off_t intrablock_offset, intradata_offset; off_t intrablock_offset, intradata_offset;
uint32_t block_size, block_flag, data_size; uint32_t block_flag, block_size, data_size, skip_size;
block_flag = read_8bit(data->physical_offset+0x00,streamfile); block_flag = read_8bit(data->physical_offset+0x00,streamfile);
block_size = read_32bitBE(data->physical_offset+0x00,streamfile) & 0x00FFFFFF; block_size = read_32bitBE(data->physical_offset+0x00,streamfile) & 0x00FFFFFF;
data_size = read_32bitBE(data->physical_offset+0x04,streamfile); /* typically block_size - 0x08 */
if (data->version == 1 && block_flag == 0x48) {
data->physical_offset += block_size;
continue; /* skip header block */
}
if (data->version == 1 && block_flag == 0x45)
return total_read; /* stop on last block (always empty) */
switch(data->codec) {
#if 0
case 0x03:
data_size = block_size - ???;
extra_size = (data_size % 0x800); /* deflated padding */
/* skip header block */ skip_size = 0x08 + 0x04*data->stream_count;
if (block_flag == 0x48) { break;
data->physical_offset += block_size; #endif
continue;
} case 0x05: /* EALayer3 v1 */
/* stop on footer block */ case 0x06: /* EALayer3 v2 "PCM" */
if (block_flag == 0x45) { case 0x07: /* EALayer3 v2 "Spike" */
data->physical_offset += block_size; data_size = block_size - 0x08;
return total_read; skip_size = 0x08;
} break;
/* data block expected */
if (block_flag != 0x44) { case 0x0a: /* EATrax */
return total_read; data_size = read_32bitBE(data->physical_offset+0x04,streamfile); /* should be block_size - 0x08 */
skip_size = 0x08;
break;
default:
return total_read;
} }
/* requested offset is outside current block, try next */ /* requested offset is outside current block, try next */
@ -65,7 +85,7 @@ static size_t eatrax_io_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset
/* reads could fall in the middle of the block */ /* reads could fall in the middle of the block */
intradata_offset = offset - data->logical_offset; intradata_offset = offset - data->logical_offset;
intrablock_offset = 0x08 + intradata_offset; intrablock_offset = skip_size + intradata_offset;
/* clamp reads up to this block's end */ /* clamp reads up to this block's end */
to_read = (data_size - intradata_offset); to_read = (data_size - intradata_offset);
@ -89,31 +109,102 @@ static size_t eatrax_io_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset
data->physical_offset += block_size; data->physical_offset += block_size;
data->logical_offset += data_size; data->logical_offset += data_size;
} }
if (data->version == 0 && block_flag == 0x80)
break; /* stop on last block */
} }
return total_read; return total_read;
} }
static size_t eatrax_io_size(STREAMFILE *streamfile, eatrax_io_data* data) { static size_t eaac_io_size(STREAMFILE *streamfile, eaac_io_data* data) {
off_t physical_offset, max_physical_offset;
size_t total_size = 0;
if (data->total_size)
return data->total_size;
physical_offset = data->start_offset;
max_physical_offset = get_streamfile_size(streamfile) - data->start_offset;
/* get size of the underlying, non-blocked data */
while (physical_offset < max_physical_offset) {
uint32_t block_flag, block_size, data_size;
block_flag = read_8bit(physical_offset+0x00,streamfile);
block_size = read_32bitBE(physical_offset+0x00,streamfile) & 0x00FFFFFF;
if (data->version == 0 && block_flag != 0x00 && block_flag != 0x80)
break; /* data/end block expected */
if (data->version == 1 && block_flag == 0x48) {
physical_offset += block_size;
continue; /* skip header block */
}
if (data->version == 1 && block_flag == 0x45)
break; /* stop on last block (always empty) */
if (data->version == 1 && block_flag != 0x44)
break; /* data block expected */
switch(data->codec) {
#if 0
case 0x03:
data_size = block_size - ???;
data_size += (data_size % 0x800); /* deflated padding */
break;
#endif
case 0x05: /* EALayer3 v1 */
case 0x06: /* EALayer3 v2 "PCM" */
case 0x07: /* EALayer3 v2 "Spike" */
data_size = block_size - 0x08;
break;
case 0x0a: /* EATrax */
data_size = read_32bitBE(physical_offset+0x04,streamfile); /* should be block_size - 0x08 */
break;
default:
return 0;
}
physical_offset += block_size;
total_size += data_size;
if (data->version == 0 && block_flag == 0x80)
break; /* stop on last block */
}
data->total_size = total_size;
return data->total_size; return data->total_size;
} }
/* Prepares custom IO for EATrax, which is simply blocked ATRAC9 data, but blocks /* Prepares custom IO for some blocked EAAudioCore formats, that need clean reads without block headers:
* may end in the middle of an ATRAC9 frame, so reads remove their headers */ * - EA-XMA: deflated XMA in multistreams (separate 2ch frames)
static STREAMFILE* setup_eatrax_streamfile(STREAMFILE *streamFile, size_t total_size) { * - EALayer3: MPEG granule 1 can go in the next block (in V2"P" mainly, others could use layout blocked_sns)
* - EATrax: ATRAC9 frames can be split between blooks
*/
static STREAMFILE* setup_eaac_streamfile(STREAMFILE *streamFile, int version, int codec, off_t start_offset, size_t total_size) {
STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL; STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL;
eatrax_io_data io_data = {0}; eaac_io_data io_data = {0};
size_t io_data_size = sizeof(eatrax_io_data); size_t io_data_size = sizeof(eaac_io_data);
io_data.total_size = total_size; io_data.version = version;
io_data.codec = codec;
io_data.start_offset = start_offset;
io_data.total_size = total_size; /* optional */
io_data.physical_offset = start_offset;
/* setup subfile */ /* setup subfile */
new_streamFile = open_wrap_streamfile(streamFile); new_streamFile = open_wrap_streamfile(streamFile);
if (!new_streamFile) goto fail; if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile; temp_streamFile = new_streamFile;
new_streamFile = open_io_streamfile(temp_streamFile, &io_data,io_data_size, eatrax_io_read,eatrax_io_size); new_streamFile = open_io_streamfile(temp_streamFile, &io_data,io_data_size, eaac_io_read,eaac_io_size);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
new_streamFile = open_buffer_streamfile(new_streamFile,0);
if (!new_streamFile) goto fail; if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile; temp_streamFile = new_streamFile;