2008-05-10 21:59:29 +02:00
|
|
|
#include "meta.h"
|
2008-05-11 03:55:13 +02:00
|
|
|
#include "../layout/layout.h"
|
2017-11-16 19:47:42 +01:00
|
|
|
#include "../coding/coding.h"
|
2008-05-10 21:59:29 +02:00
|
|
|
|
2017-11-16 19:47:42 +01:00
|
|
|
/* CD-XA - from Sony PS1 CDs */
|
|
|
|
VGMSTREAM * init_vgmstream_cdxa(STREAMFILE *streamFile) {
|
|
|
|
VGMSTREAM * vgmstream = NULL;
|
|
|
|
off_t start_offset;
|
|
|
|
int loop_flag = 0, channel_count, sample_rate;
|
|
|
|
int is_blocked;
|
|
|
|
size_t file_size = get_streamfile_size(streamFile);
|
|
|
|
|
2018-07-22 23:08:09 +02:00
|
|
|
/* checks
|
2018-07-23 19:36:08 +02:00
|
|
|
* .xa: common, .str: sometimes (mainly videos)
|
|
|
|
* .adp: Phantasy Star Collection (SAT) raw XA */
|
|
|
|
if ( !check_extensions(streamFile,"xa,str,adp") )
|
2017-11-16 19:47:42 +01:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* Proper XA comes in raw (BIN 2352 mode2/form2) CD sectors, that contain XA subheaders.
|
|
|
|
* This also has minimal support for headerless (ISO 2048 mode1/data) mode. */
|
|
|
|
|
|
|
|
/* check RIFF header = raw (optional, added when ripping and not part of the CD data) */
|
|
|
|
if (read_32bitBE(0x00,streamFile) == 0x52494646 && /* "RIFF" */
|
|
|
|
read_32bitBE(0x08,streamFile) == 0x43445841 && /* "CDXA" */
|
|
|
|
read_32bitBE(0x0C,streamFile) == 0x666D7420) { /* "fmt " */
|
|
|
|
is_blocked = 1;
|
|
|
|
start_offset = 0x2c; /* after "data" (chunk size tends to be a bit off) */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* sector sync word = raw */
|
|
|
|
if (read_32bitBE(0x00,streamFile) == 0x00FFFFFF &&
|
|
|
|
read_32bitBE(0x04,streamFile) == 0xFFFFFFFF &&
|
|
|
|
read_32bitBE(0x08,streamFile) == 0xFFFFFF00) {
|
|
|
|
is_blocked = 1;
|
|
|
|
start_offset = 0x00;
|
|
|
|
}
|
2018-07-23 19:36:08 +02:00
|
|
|
else { /* headerless and possibly incorrectly ripped */
|
2017-11-16 19:47:42 +01:00
|
|
|
is_blocked = 0;
|
|
|
|
start_offset = 0x00;
|
|
|
|
}
|
|
|
|
}
|
2008-05-10 21:59:29 +02:00
|
|
|
|
2018-01-27 17:08:04 +01:00
|
|
|
/* test some blocks (except when RIFF) since other .XA/STR may start blank */
|
2017-11-16 19:47:42 +01:00
|
|
|
if (start_offset == 0) {
|
2018-01-27 17:08:04 +01:00
|
|
|
int i, j, block;
|
|
|
|
off_t test_offset = start_offset;
|
|
|
|
size_t sector_size = (is_blocked ? 0x900 : 0x800);
|
|
|
|
size_t block_size = 0x80;
|
2008-05-11 03:55:13 +02:00
|
|
|
|
2018-01-27 17:08:04 +01:00
|
|
|
for (block = 0; block < 3; block++) {
|
|
|
|
test_offset += (is_blocked ? 0x18 : 0x00); /* header */
|
2008-05-10 21:59:29 +02:00
|
|
|
|
2018-01-27 17:08:04 +01:00
|
|
|
for (i = 0; i < (sector_size/block_size); i++) {
|
2018-07-23 19:36:08 +02:00
|
|
|
/* XA headers checks: filter indexes should be 0..3, and shifts 0..C */
|
2018-01-27 17:08:04 +01:00
|
|
|
for (j = 0; j < 16; j++) {
|
2018-07-22 23:08:09 +02:00
|
|
|
uint8_t header = (uint8_t)read_8bit(test_offset + i, streamFile);
|
2018-07-23 19:36:08 +02:00
|
|
|
if (((header >> 4) & 0xF) > 0x03)
|
|
|
|
goto fail;
|
|
|
|
if (((header >> 0) & 0xF) > 0x0c)
|
2018-01-27 17:08:04 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2018-07-23 19:36:08 +02:00
|
|
|
/* XA headers pairs are repeated */
|
|
|
|
if (read_32bitBE(test_offset+0x00,streamFile) != read_32bitBE(test_offset+0x04,streamFile) ||
|
|
|
|
read_32bitBE(test_offset+0x08,streamFile) != read_32bitBE(test_offset+0x0c,streamFile))
|
|
|
|
goto fail;
|
|
|
|
|
2018-01-27 17:08:04 +01:00
|
|
|
test_offset += 0x80;
|
2017-11-16 19:47:42 +01:00
|
|
|
}
|
2018-01-27 17:08:04 +01:00
|
|
|
|
|
|
|
test_offset += (is_blocked ? 0x18 : 0x00); /* footer */
|
2017-11-16 19:47:42 +01:00
|
|
|
}
|
2018-07-23 19:36:08 +02:00
|
|
|
/* (the above could get fooled by files with many 0s at the beginning;
|
|
|
|
* this could be detected as blank XA frames should have have 0c0c0c0c... headers */
|
2017-11-16 19:47:42 +01:00
|
|
|
}
|
2008-05-10 21:59:29 +02:00
|
|
|
|
|
|
|
|
2017-11-16 19:47:42 +01:00
|
|
|
/* data is ok: parse header */
|
|
|
|
if (is_blocked) {
|
|
|
|
/* parse 0x18 sector header (also see xa_blocked.c) */
|
2018-07-22 23:08:09 +02:00
|
|
|
uint8_t xa_header = (uint8_t)read_8bit(start_offset + 0x13,streamFile);
|
2008-05-10 21:59:29 +02:00
|
|
|
|
2018-07-22 23:08:09 +02:00
|
|
|
switch((xa_header >> 0) & 3) { /* 0..1: mono/stereo */
|
2017-11-16 19:47:42 +01:00
|
|
|
case 0: channel_count = 1; break;
|
|
|
|
case 1: channel_count = 2; break;
|
|
|
|
default: goto fail;
|
2008-05-10 21:59:29 +02:00
|
|
|
}
|
2017-11-16 19:47:42 +01:00
|
|
|
switch((xa_header >> 2) & 3) { /* 2..3: sample rate */
|
|
|
|
case 0: sample_rate = 37800; break;
|
|
|
|
case 1: sample_rate = 18900; break;
|
|
|
|
default: goto fail;
|
|
|
|
}
|
2018-07-22 23:08:09 +02:00
|
|
|
switch((xa_header >> 4) & 3) { /* 4..5: bits per sample (0=4, 1=8) */
|
|
|
|
case 0: break;
|
|
|
|
default: /* PS1 games only do 4b */
|
|
|
|
VGM_LOG("XA: unknown bits per sample found\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
switch((xa_header >> 6) & 1) { /* 6: emphasis (applies a filter) */
|
|
|
|
case 0: break;
|
|
|
|
default: /* shouldn't be used by games */
|
|
|
|
VGM_LOG("XA: unknown emphasis found\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch((xa_header >> 7) & 1) { /* 7: reserved */
|
|
|
|
case 0: break;
|
|
|
|
default:
|
|
|
|
VGM_LOG("XA: unknown reserved bit found\n");
|
|
|
|
break;
|
|
|
|
}
|
2017-11-16 19:47:42 +01:00
|
|
|
}
|
|
|
|
else {
|
2018-07-23 19:36:08 +02:00
|
|
|
/* headerless */
|
|
|
|
if (check_extensions(streamFile,"adp")) {
|
|
|
|
/* Phantasy Star Collection (SAT) raw files */
|
|
|
|
/* most are stereo, though a few (mainly sfx banks, sometimes using .bin) are mono */
|
|
|
|
|
|
|
|
char filename[PATH_LIMIT] = {0};
|
|
|
|
get_streamfile_filename(streamFile, filename,PATH_LIMIT);
|
|
|
|
|
|
|
|
/* detect PS1 mono files, very lame but whatevs, no way to detect XA mono/stereo */
|
|
|
|
if (filename[0]=='P' && filename[1]=='S' && filename[2]=='1' && filename[3]=='S') {
|
|
|
|
channel_count = 1;
|
|
|
|
sample_rate = 22050;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
channel_count = 2;
|
|
|
|
sample_rate = 44100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* incorrectly ripped standard XA */
|
|
|
|
channel_count = 2;
|
|
|
|
sample_rate = 37800;
|
|
|
|
}
|
2008-05-10 21:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-16 19:47:42 +01:00
|
|
|
/* build the VGMSTREAM */
|
|
|
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
|
|
|
vgmstream->sample_rate = sample_rate;
|
|
|
|
|
|
|
|
vgmstream->meta_type = meta_PSX_XA;
|
2018-07-22 23:08:09 +02:00
|
|
|
vgmstream->coding_type = coding_XA;
|
|
|
|
vgmstream->layout_type = is_blocked ? layout_blocked_xa : layout_none;
|
2017-11-16 19:47:42 +01:00
|
|
|
|
|
|
|
/* open the file for reading */
|
|
|
|
if ( !vgmstream_open_stream(vgmstream, streamFile, start_offset) )
|
|
|
|
goto fail;
|
|
|
|
|
2018-08-02 17:21:09 +02:00
|
|
|
|
|
|
|
if (is_blocked) {
|
|
|
|
/* calc num_samples as blocks may be empty or smaller than usual depending on flags */
|
|
|
|
vgmstream->next_block_offset = start_offset;
|
|
|
|
do {
|
|
|
|
block_update_xa(vgmstream->next_block_offset,vgmstream);
|
|
|
|
vgmstream->num_samples += vgmstream->current_block_samples;
|
|
|
|
}
|
|
|
|
while (vgmstream->next_block_offset < get_streamfile_size(streamFile));
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vgmstream->num_samples = xa_bytes_to_samples(file_size - start_offset, channel_count, is_blocked);
|
|
|
|
}
|
|
|
|
|
2018-07-22 23:08:09 +02:00
|
|
|
if (vgmstream->layout_type == layout_blocked_xa)
|
|
|
|
block_update_xa(start_offset,vgmstream);
|
2017-11-16 19:47:42 +01:00
|
|
|
return vgmstream;
|
|
|
|
|
2008-05-10 21:59:29 +02:00
|
|
|
fail:
|
|
|
|
if (vgmstream) close_vgmstream(vgmstream);
|
|
|
|
return NULL;
|
|
|
|
}
|