Use KMA9 custom IO instead of custom ATRAC9

This commit is contained in:
bnnm 2018-08-25 17:26:49 +02:00
parent 9c8dae38dc
commit f86c90c5f9
5 changed files with 173 additions and 12 deletions

View File

@ -240,6 +240,10 @@
RelativePath=".\meta\fsb5_interleave_streamfile.h"
>
</File>
<File
RelativePath=".\meta\kma9_streamfile.h"
>
</File>
<File
RelativePath=".\meta\opus_interleave_streamfile.h"
>

View File

@ -102,6 +102,7 @@
<ClInclude Include="meta\ea_schl_streamfile.h" />
<ClInclude Include="meta\fsb_interleave_streamfile.h" />
<ClInclude Include="meta\fsb5_interleave_streamfile.h" />
<ClInclude Include="meta\kma9_streamfile.h" />
<ClInclude Include="meta\ppst_streamfile.h" />
<ClInclude Include="meta\ps2_psh_streamfile.h" />
<ClInclude Include="meta\opus_interleave_streamfile.h" />

View File

@ -86,6 +86,9 @@
<ClInclude Include="meta\fsb5_interleave_streamfile.h">
<Filter>meta\Header Files</Filter>
</ClInclude>
<ClInclude Include="meta\kma9_streamfile.h">
<Filter>meta\Header Files</Filter>
</ClInclude>
<ClInclude Include="meta\opus_interleave_streamfile.h">
<Filter>meta\Header Files</Filter>
</ClInclude>

View File

@ -1,20 +1,21 @@
#include "meta.h"
#include "../coding/coding.h"
#include "kma9_streamfile.h"
/* KMA9 - Koei Tecmo's custom ATRAC9 [Nobunaga no Yabou - Souzou (Vita)] */
/* KMA9 - Koei Tecmo's interleaved ATRAC9 [Nobunaga no Yabou - Souzou (Vita)] */
VGMSTREAM * init_vgmstream_kma9(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL;
STREAMFILE* temp_streamFile = NULL;
off_t start_offset;
size_t stream_size;
size_t stream_size, interleave;
int loop_flag, channel_count;
int total_subsongs = 0, target_subsong = streamFile->stream_index;
/* check extension */
/* checks */
if ( !check_extensions(streamFile,"km9") )
goto fail;
/* check header */
if (read_32bitBE(0x00,streamFile) != 0x4B4D4139) /* "KMA9" */
goto fail;
@ -26,6 +27,7 @@ VGMSTREAM * init_vgmstream_kma9(STREAMFILE *streamFile) {
if (target_subsong == 0) target_subsong = 1;
if (target_subsong < 0 || target_subsong > total_subsongs || total_subsongs < 1) goto fail;
/* 0x0c: unknown */
interleave = read_32bitLE(0x10,streamFile); /* 1 superframe */
stream_size = read_32bitLE(0x14,streamFile); /* per subsong */
@ -45,14 +47,10 @@ VGMSTREAM * init_vgmstream_kma9(STREAMFILE *streamFile) {
{
atrac9_config cfg = {0};
cfg.type = ATRAC9_KMA9;
cfg.type = ATRAC9_DEFAULT;
cfg.channels = vgmstream->channels;
cfg.config_data = read_32bitBE(0x5c,streamFile);
cfg.encoder_delay = read_32bitLE(0x20,streamFile);
cfg.interleave_skip = read_32bitLE(0x10,streamFile); /* 1 superframe */
cfg.subsong_skip = total_subsongs;
start_offset += (target_subsong-1) * cfg.interleave_skip * (cfg.subsong_skip-1);
cfg.config_data = read_32bitBE(0x5c,streamFile);
vgmstream->codec_data = init_atrac9(&cfg);
if (!vgmstream->codec_data) goto fail;
@ -63,17 +61,24 @@ VGMSTREAM * init_vgmstream_kma9(STREAMFILE *streamFile) {
vgmstream->loop_start_sample -= cfg.encoder_delay;
//vgmstream->loop_end_sample -= cfg.encoder_delay;
}
/* KMA9 interleaves one ATRAC9 frame per subsong */
temp_streamFile = setup_kma9_streamfile(streamFile, start_offset, stream_size, interleave, (target_subsong-1), total_subsongs);
if (!temp_streamFile) goto fail;
start_offset = 0;
}
#else
goto fail;
#endif
/* open the file for reading */
if ( !vgmstream_open_stream(vgmstream, streamFile, start_offset) )
if ( !vgmstream_open_stream(vgmstream, temp_streamFile, start_offset) )
goto fail;
close_streamfile(temp_streamFile);
return vgmstream;
fail:
close_streamfile(temp_streamFile);
close_vgmstream(vgmstream);
return NULL;
}

148
src/meta/kma9_streamfile.h Normal file
View File

@ -0,0 +1,148 @@
#ifndef _KM9_STREAMFILE_H_
#define _KM9_STREAMFILE_H_
#include "../streamfile.h"
typedef struct {
/* config */
int stream_number;
int stream_count;
size_t interleave_size;
off_t stream_offset;
size_t stream_size;
/* state */
off_t logical_offset; /* offset that corresponds to physical_offset */
off_t physical_offset; /* actual file offset */
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 logical_size;
} kma9_io_data;
static size_t kma9_io_read(STREAMFILE *streamfile, uint8_t *dest, off_t offset, size_t length, kma9_io_data* data) {
size_t total_read = 0;
/* ignore bad reads */
if (offset < 0 || offset > data->logical_size) {
return 0;
}
/* 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) {
data->logical_offset = 0x00;
data->physical_offset = data->stream_offset;
data->data_size = 0;
}
/* read blocks, one at a time */
while (length > 0) {
/* ignore EOF */
if (data->logical_offset >= data->logical_size) {
break;
}
/* process new block */
if (data->data_size == 0) {
data->skip_size = data->interleave_size * data->stream_number;
data->data_size = data->interleave_size;
}
/* move to next block */
if (offset >= data->logical_offset + data->data_size) {
data->physical_offset += data->interleave_size*data->stream_count;
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);
offset += bytes_done;
total_read += bytes_done;
length -= bytes_done;
dest += bytes_done;
if (bytes_done != to_read || bytes_done == 0) {
break; /* error/EOF */
}
}
}
return total_read;
}
static size_t kma9_io_size(STREAMFILE *streamfile, kma9_io_data* data) {
off_t physical_offset = data->stream_offset;
off_t max_physical_offset = get_streamfile_size(streamfile);
size_t logical_size = 0;
if (data->logical_size)
return data->logical_size;
/* get size of the logical stream */
while (physical_offset < max_physical_offset) {
logical_size += data->interleave_size;
physical_offset += data->interleave_size*data->stream_count;
}
if (logical_size > max_physical_offset)
return 0;
if (logical_size != data->stream_size)
return 0;
data->logical_size = logical_size;
return data->logical_size;
}
/* Prepares custom IO for KMA9, which interleaves ATRAC9 frames */
static STREAMFILE* setup_kma9_streamfile(STREAMFILE *streamFile, off_t stream_offset, size_t stream_size, size_t interleave_size, int stream_number, int stream_count) {
STREAMFILE *temp_streamFile = NULL, *new_streamFile = NULL;
kma9_io_data io_data = {0};
size_t io_data_size = sizeof(kma9_io_data);
io_data.stream_number = stream_number;
io_data.stream_count = stream_count;
io_data.stream_offset = stream_offset;
io_data.stream_size = stream_size;
io_data.interleave_size = interleave_size;
io_data.physical_offset = stream_offset;
io_data.logical_size = kma9_io_size(streamFile, &io_data); /* force init */
if (io_data.logical_size == 0) {
VGM_LOG("KMA9: wrong logical size\n");
goto fail;
}
/* setup subfile */
new_streamFile = open_wrap_streamfile(streamFile);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
new_streamFile = open_io_streamfile(temp_streamFile, &io_data,io_data_size, kma9_io_read,kma9_io_size);
if (!new_streamFile) goto fail;
temp_streamFile = new_streamFile;
return temp_streamFile;
fail:
close_streamfile(temp_streamFile);
return NULL;
}
#endif /* _KM9_STREAMFILE_H_ */