2018-04-19 22:26:46 +02:00
|
|
|
#ifndef _EA_EAAC_STREAMFILE_H_
|
|
|
|
#define _EA_EAAC_STREAMFILE_H_
|
2018-03-31 20:10:57 +02:00
|
|
|
#include "../streamfile.h"
|
|
|
|
|
2018-08-25 09:57:46 +02:00
|
|
|
#define XMA_FRAME_SIZE 0x800
|
2018-03-31 20:10:57 +02:00
|
|
|
|
|
|
|
typedef struct {
|
2018-04-19 22:26:46 +02:00
|
|
|
/* config */
|
|
|
|
int version;
|
|
|
|
int codec;
|
2018-07-21 22:09:44 +02:00
|
|
|
int streamed;
|
2018-08-25 09:57:46 +02:00
|
|
|
int stream_number;
|
|
|
|
int stream_count;
|
|
|
|
off_t stream_offset;
|
|
|
|
|
|
|
|
/* state */
|
|
|
|
off_t logical_offset; /* offset that corresponds to physical_offset */
|
|
|
|
off_t physical_offset; /* actual file offset */
|
|
|
|
|
|
|
|
uint32_t block_flag; /* current block flags */
|
|
|
|
size_t block_size; /* current block size */
|
|
|
|
size_t skip_size; /* size to skip from a block start to reach data start */
|
|
|
|
size_t data_size; /* logical size of the block */
|
|
|
|
size_t extra_size; /* extra padding/etc size of the block */
|
|
|
|
|
|
|
|
size_t logical_size;
|
2018-04-19 22:26:46 +02:00
|
|
|
} eaac_io_data;
|
2018-03-31 20:10:57 +02:00
|
|
|
|
|
|
|
|
2018-04-19 22:26:46 +02:00
|
|
|
/* Reads skipping EA's block headers, so the resulting data is smaller or larger than physical data.
|
2018-08-25 09:57:46 +02:00
|
|
|
* physical/logical_offset will be at the start of a block and only advance when a block is done */
|
2018-04-19 22:26:46 +02:00
|
|
|
static size_t eaac_io_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length, eaac_io_data* data) {
|
2018-03-31 20:10:57 +02:00
|
|
|
size_t total_read = 0;
|
|
|
|
|
|
|
|
/* ignore bad reads */
|
2018-08-25 09:57:46 +02:00
|
|
|
if (offset < 0 || offset > data->logical_size) {
|
2018-04-19 22:26:46 +02:00
|
|
|
return total_read;
|
2018-03-31 20:10:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* previous offset: re-start as we can't map logical<>physical offsets
|
|
|
|
* (kinda slow as it trashes buffers, but shouldn't happen often) */
|
|
|
|
if (offset < data->logical_offset) {
|
2018-08-25 09:57:46 +02:00
|
|
|
data->physical_offset = data->stream_offset;
|
2018-03-31 20:10:57 +02:00
|
|
|
data->logical_offset = 0x00;
|
2018-08-25 09:57:46 +02:00
|
|
|
data->data_size = 0;
|
|
|
|
data->extra_size = 0;
|
2018-03-31 20:10:57 +02:00
|
|
|
}
|
|
|
|
|
2018-08-25 09:57:46 +02:00
|
|
|
/* read blocks, one at a time */
|
2018-03-31 20:10:57 +02:00
|
|
|
while (length > 0) {
|
|
|
|
|
2018-08-25 09:57:46 +02:00
|
|
|
/* ignore EOF (implicitly handles block end flags) */
|
|
|
|
if (data->logical_offset >= data->logical_size) {
|
|
|
|
break;
|
2018-03-31 20:10:57 +02:00
|
|
|
}
|
2018-04-19 22:26:46 +02:00
|
|
|
|
2018-08-25 09:57:46 +02:00
|
|
|
/* process new block */
|
|
|
|
if (data->data_size == 0) {
|
|
|
|
data->block_flag = (uint8_t)read_8bit(data->physical_offset+0x00,streamfile);
|
|
|
|
data->block_size = read_32bitBE(data->physical_offset+0x00,streamfile) & 0x00FFFFFF;
|
2018-04-19 22:26:46 +02:00
|
|
|
|
2018-08-25 09:57:46 +02:00
|
|
|
/* ignore header block */
|
|
|
|
if (data->version == 1 && data->block_flag == 0x48) {
|
|
|
|
data->physical_offset += data->block_size;
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-19 22:26:46 +02:00
|
|
|
|
2018-08-25 09:57:46 +02:00
|
|
|
switch(data->codec) {
|
|
|
|
#if 0
|
|
|
|
case 0x03: { /* EA-XMA */
|
|
|
|
/* block format: 0x04=num-samples, (size*4 + N XMA packets) per stream (with 1/2ch XMA headers) */
|
|
|
|
int i;
|
|
|
|
|
|
|
|
data->skip_size = 0x04 + 0x04;
|
|
|
|
for (i = 0; i < data->stream_number - 1; i++) {
|
|
|
|
data->skip_size += read_32bitBE(data->physical_offset+data->skip_size, streamfile) / 4;
|
|
|
|
}
|
|
|
|
data->data_size = read_32bitBE(data->physical_offset+data->skip_size, streamfile) / 4; /* why size*4...? */
|
|
|
|
data->skip_size += 0x04; /* skip mini header */
|
|
|
|
data->data_size -= 0x04; /* remove mini header */
|
|
|
|
if (data->data_size % XMA_FRAME_SIZE)
|
|
|
|
data->extra_size = XMA_FRAME_SIZE - (data->data_size % XMA_FRAME_SIZE);
|
|
|
|
break;
|
|
|
|
}
|
2018-04-19 22:26:46 +02:00
|
|
|
#endif
|
2018-08-25 09:57:46 +02:00
|
|
|
case 0x05: /* EALayer3 v1 */
|
|
|
|
case 0x06: /* EALayer3 v2 "PCM" */
|
|
|
|
case 0x07: /* EALayer3 v2 "Spike" */
|
|
|
|
data->skip_size = 0x08;
|
|
|
|
data->data_size = data->block_size - data->skip_size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x0a: /* EATrax */
|
|
|
|
data->skip_size = 0x08;
|
|
|
|
data->data_size = read_32bitBE(data->physical_offset+0x04,streamfile); /* also block_size - 0x08 */
|
|
|
|
break;
|
|
|
|
#if 0
|
|
|
|
case 0x0c: /* EA Opus */
|
|
|
|
data->skip_size = 0x08;
|
|
|
|
data->data_size = data->block_size - data->skip_size;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return total_read;
|
|
|
|
}
|
2018-03-31 20:10:57 +02:00
|
|
|
}
|
|
|
|
|
2018-08-25 09:57:46 +02:00
|
|
|
/* move to next block */
|
|
|
|
if (offset >= data->logical_offset + data->data_size + data->extra_size) {
|
|
|
|
data->physical_offset += data->block_size;
|
|
|
|
data->logical_offset += data->data_size + data->extra_size;
|
|
|
|
data->data_size = 0;
|
|
|
|
data->extra_size = 0;
|
2018-03-31 20:10:57 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-25 09:57:46 +02:00
|
|
|
/* read data */
|
|
|
|
{
|
|
|
|
size_t bytes_consumed, bytes_done, to_read;
|
2018-04-19 22:26:46 +02:00
|
|
|
|
2018-08-25 09:57:46 +02:00
|
|
|
bytes_consumed = offset - data->logical_offset;
|
|
|
|
|
|
|
|
switch(data->codec) {
|
|
|
|
#if 0
|
|
|
|
case 0x03: { /* EA-XMA */
|
|
|
|
if (bytes_consumed < data->data_size) { /* offset falls within actual data */
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
else { /* offset falls within logical padded data */
|
|
|
|
to_read = data->data_size + data->extra_size - bytes_consumed;
|
|
|
|
if (to_read > length)
|
|
|
|
to_read = length;
|
|
|
|
memset(dest, 0xFF, to_read); /* no real need though, padding is ignored */
|
|
|
|
bytes_done = to_read;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
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);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
total_read += bytes_done;
|
|
|
|
dest += bytes_done;
|
|
|
|
offset += bytes_done;
|
|
|
|
length -= bytes_done;
|
|
|
|
|
|
|
|
if (bytes_done != to_read || bytes_done == 0) {
|
|
|
|
break; /* error/EOF */
|
|
|
|
}
|
|
|
|
}
|
2018-03-31 20:10:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return total_read;
|
|
|
|
}
|
|
|
|
|
2018-08-25 09:57:46 +02:00
|
|
|
|
2018-04-19 22:26:46 +02:00
|
|
|
static size_t eaac_io_size(STREAMFILE *streamfile, eaac_io_data* data) {
|
|
|
|
off_t physical_offset, max_physical_offset;
|
2018-08-25 09:57:46 +02:00
|
|
|
size_t logical_size = 0;
|
2018-04-19 22:26:46 +02:00
|
|
|
|
2018-08-25 09:57:46 +02:00
|
|
|
if (data->logical_size)
|
|
|
|
return data->logical_size;
|
2018-04-19 22:26:46 +02:00
|
|
|
|
2018-08-25 09:57:46 +02:00
|
|
|
physical_offset = data->stream_offset;
|
2018-07-21 20:58:01 +02:00
|
|
|
max_physical_offset = get_streamfile_size(streamfile);
|
2018-04-19 22:26:46 +02:00
|
|
|
|
2018-08-25 09:57:46 +02:00
|
|
|
/* get size of the logical stream */
|
2018-04-19 22:26:46 +02:00
|
|
|
while (physical_offset < max_physical_offset) {
|
2018-08-25 09:57:46 +02:00
|
|
|
uint32_t block_flag, block_size, data_size, skip_size;
|
|
|
|
//int i;
|
2018-04-19 22:26:46 +02:00
|
|
|
|
2018-07-14 23:02:40 +02:00
|
|
|
block_flag = (uint8_t)read_8bit(physical_offset+0x00,streamfile);
|
2018-04-19 22:26:46 +02:00
|
|
|
block_size = read_32bitBE(physical_offset+0x00,streamfile) & 0x00FFFFFF;
|
|
|
|
|
|
|
|
if (data->version == 0 && block_flag != 0x00 && block_flag != 0x80)
|
2018-08-25 09:57:46 +02:00
|
|
|
break; /* unknown block */
|
2018-04-19 22:26:46 +02:00
|
|
|
|
|
|
|
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)
|
2018-08-25 09:57:46 +02:00
|
|
|
break; /* unknown block */
|
2018-04-19 22:26:46 +02:00
|
|
|
|
|
|
|
switch(data->codec) {
|
|
|
|
#if 0
|
2018-08-25 09:57:46 +02:00
|
|
|
case 0x03: /* EA-XMA */
|
|
|
|
skip_size = 0x04 + 0x04;
|
|
|
|
for (i = 0; i < data->stream_number - 1; i++) {
|
|
|
|
skip_size += read_32bitBE(physical_offset + skip_size, streamfile) / 4; /* why size*4...? */
|
|
|
|
}
|
|
|
|
data_size = read_32bitBE(physical_offset + skip_size, streamfile) / 4;
|
|
|
|
skip_size += 0x04; /* skip mini header */
|
|
|
|
data_size -= 0x04; /* remove mini header */
|
|
|
|
if (data_size % XMA_FRAME_SIZE)
|
|
|
|
data_size += XMA_FRAME_SIZE - (data_size % XMA_FRAME_SIZE); /* extra padding */
|
2018-04-19 22:26:46 +02:00
|
|
|
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 */
|
2018-08-25 09:57:46 +02:00
|
|
|
data_size = read_32bitBE(physical_offset+0x04,streamfile); /* also block_size - 0x08 */
|
2018-04-19 22:26:46 +02:00
|
|
|
break;
|
2018-08-25 09:57:46 +02:00
|
|
|
#if 0
|
|
|
|
case 0x0c: { /* EAOpus */
|
|
|
|
size_t done;
|
|
|
|
data_size = 0;
|
|
|
|
while (done < block_size - 0x08) {
|
|
|
|
size_t packet_size = read_16bitBE(physical_offset+0x08+done,streamfile);
|
|
|
|
done += 0x02 + packet_size;
|
|
|
|
data_size = 0x1a + packet_size; /* OggS page per Opus packet */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2018-04-19 22:26:46 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
physical_offset += block_size;
|
2018-08-25 09:57:46 +02:00
|
|
|
logical_size += data_size;
|
2018-04-19 22:26:46 +02:00
|
|
|
|
2018-07-21 22:09:44 +02:00
|
|
|
if (data->version == 0 && (!data->streamed || block_flag == 0x80))
|
2018-04-19 22:26:46 +02:00
|
|
|
break; /* stop on last block */
|
|
|
|
}
|
|
|
|
|
2018-08-25 09:57:46 +02:00
|
|
|
data->logical_size = logical_size;
|
|
|
|
return data->logical_size;
|
2018-03-31 20:10:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-19 22:26:46 +02:00
|
|
|
/* Prepares custom IO for some blocked EAAudioCore formats, that need clean reads without block headers:
|
2018-08-25 09:57:46 +02:00
|
|
|
* - EA-XMA: deflated XMA in multistreams (separate 1/2ch packets)
|
2018-04-19 22:26:46 +02:00
|
|
|
* - 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
|
2018-08-25 09:57:46 +02:00
|
|
|
* - EAOpus:
|
2018-04-19 22:26:46 +02:00
|
|
|
*/
|
2018-08-25 09:57:46 +02:00
|
|
|
static STREAMFILE* setup_eaac_streamfile(STREAMFILE *streamFile, int version, int codec, int streamed, int stream_number, int stream_count, off_t stream_offset) {
|
2018-03-31 20:10:57 +02:00
|
|
|
STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL;
|
2018-04-19 22:26:46 +02:00
|
|
|
eaac_io_data io_data = {0};
|
|
|
|
size_t io_data_size = sizeof(eaac_io_data);
|
2018-03-31 20:10:57 +02:00
|
|
|
|
2018-04-19 22:26:46 +02:00
|
|
|
io_data.version = version;
|
|
|
|
io_data.codec = codec;
|
2018-07-21 22:09:44 +02:00
|
|
|
io_data.streamed = streamed;
|
2018-08-25 09:57:46 +02:00
|
|
|
io_data.stream_number = stream_number;
|
|
|
|
io_data.stream_count = stream_count;
|
|
|
|
io_data.stream_offset = stream_offset;
|
|
|
|
io_data.physical_offset = stream_offset;
|
|
|
|
io_data.logical_size = eaac_io_size(streamFile, &io_data); /* force init */
|
|
|
|
|
|
|
|
if (io_data.logical_size > get_streamfile_size(streamFile)) {
|
|
|
|
VGM_LOG("EA EAAC: wrong logical size\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
2018-03-31 20:10:57 +02:00
|
|
|
|
|
|
|
/* setup subfile */
|
|
|
|
new_streamFile = open_wrap_streamfile(streamFile);
|
|
|
|
if (!new_streamFile) goto fail;
|
|
|
|
temp_streamFile = new_streamFile;
|
|
|
|
|
2018-04-19 22:26:46 +02:00
|
|
|
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);
|
2018-03-31 20:10:57 +02:00
|
|
|
if (!new_streamFile) goto fail;
|
|
|
|
temp_streamFile = new_streamFile;
|
|
|
|
|
|
|
|
return temp_streamFile;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
close_streamfile(temp_streamFile);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _EA_EAAC_STREAMFILE_H_ */
|