2008-07-22 04:18:32 +02:00
|
|
|
#include "meta.h"
|
|
|
|
|
|
|
|
|
2018-08-26 02:26:38 +02:00
|
|
|
/* .sfl - odd RIFF-formatted files that go along with .ogg [Hanachirasu (PC), Touhou 10.5 (PC)] */
|
|
|
|
VGMSTREAM * init_vgmstream_sfl_ogg(STREAMFILE *streamFile) {
|
2008-07-22 04:18:32 +02:00
|
|
|
VGMSTREAM * vgmstream = NULL;
|
2018-08-26 02:26:38 +02:00
|
|
|
STREAMFILE * streamData = NULL;
|
2008-07-22 04:18:32 +02:00
|
|
|
int loop_flag = 0;
|
2019-02-09 23:43:28 +01:00
|
|
|
int loop_start = 0, loop_end = 0;
|
2008-07-22 04:18:32 +02:00
|
|
|
|
|
|
|
|
2018-08-26 02:26:38 +02:00
|
|
|
/* checks */
|
|
|
|
if (!check_extensions(streamFile, "sfl"))
|
2008-07-22 04:18:32 +02:00
|
|
|
goto fail;
|
2018-08-26 02:26:38 +02:00
|
|
|
if (read_32bitBE(0x00,streamFile) != 0x52494646) /* "RIFF" */
|
2008-07-22 04:18:32 +02:00
|
|
|
goto fail;
|
2018-08-26 02:26:38 +02:00
|
|
|
if (read_32bitBE(0x08,streamFile) != 0x5346504C) /* "SFPL" */
|
2008-07-22 04:18:32 +02:00
|
|
|
goto fail;
|
2018-08-26 02:26:38 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
/* try with file.ogg.sfl=header and file.ogg=data [Hanachirasu (PC)] */
|
|
|
|
char basename[PATH_LIMIT];
|
|
|
|
get_streamfile_basename(streamFile,basename,PATH_LIMIT);
|
|
|
|
streamData = open_streamfile_by_filename(streamFile, basename);
|
|
|
|
if (!streamData) {
|
2019-02-09 23:43:28 +01:00
|
|
|
/* try again with file.sfl=header + file.ogg=data */
|
2018-08-26 02:26:38 +02:00
|
|
|
streamData = open_streamfile_by_ext(streamFile,"ogg");
|
|
|
|
if (!streamData) goto fail;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!check_extensions(streamData, "ogg"))
|
|
|
|
goto fail;
|
|
|
|
}
|
2008-07-22 04:18:32 +02:00
|
|
|
}
|
|
|
|
|
2018-08-26 02:26:38 +02:00
|
|
|
#ifdef VGM_USE_VORBIS
|
2008-07-22 04:18:32 +02:00
|
|
|
/* let the real initer do the parsing */
|
2018-08-26 02:26:38 +02:00
|
|
|
vgmstream = init_vgmstream_ogg_vorbis(streamData);
|
2008-07-22 04:18:32 +02:00
|
|
|
if (!vgmstream) goto fail;
|
2018-08-26 02:26:38 +02:00
|
|
|
vgmstream->meta_type = meta_OGG_SFL;
|
|
|
|
#else
|
|
|
|
goto fail;
|
|
|
|
#endif
|
2008-07-22 04:18:32 +02:00
|
|
|
|
|
|
|
/* read through chunks to verify format and find metadata */
|
|
|
|
{
|
2019-02-09 23:43:28 +01:00
|
|
|
off_t current_chunk = 0x0c;
|
|
|
|
size_t riff_size = read_32bitLE(0x04,streamFile);
|
|
|
|
size_t file_size = get_streamfile_size(streamFile);
|
2018-08-26 02:26:38 +02:00
|
|
|
|
|
|
|
if (file_size < riff_size+0x08)
|
|
|
|
goto fail;
|
2008-07-22 04:18:32 +02:00
|
|
|
|
2019-02-09 23:43:28 +01:00
|
|
|
/* sfl loops come in two varieties:
|
|
|
|
* - "cue " (start) + "LIST" with "rgn" (length) [Touhou]
|
|
|
|
* - "cue " (start+end) [Hanachirasu]
|
|
|
|
* Both may have "LIST" with optional "labl" markers (start+end or start+length in seconds),
|
|
|
|
* that can be parsed to get loops, but aren't sample-accurate nor always exist.
|
|
|
|
*/
|
2008-07-22 04:18:32 +02:00
|
|
|
while (current_chunk < file_size) {
|
2018-08-26 02:26:38 +02:00
|
|
|
uint32_t chunk_type = read_32bitBE(current_chunk+0x00,streamFile);
|
2019-02-09 23:43:28 +01:00
|
|
|
size_t chunk_size = read_32bitLE(current_chunk+0x04,streamFile);
|
2008-07-22 04:18:32 +02:00
|
|
|
|
2019-02-09 23:43:28 +01:00
|
|
|
if (current_chunk + 0x08 + chunk_size > file_size)
|
2018-08-26 02:26:38 +02:00
|
|
|
goto fail;
|
2008-07-22 04:18:32 +02:00
|
|
|
|
|
|
|
switch(chunk_type) {
|
2019-02-09 23:43:28 +01:00
|
|
|
case 0x63756520: /* "cue " */
|
|
|
|
switch (read_32bitLE(current_chunk+0x08+0x00, streamFile)) { /* cue count */
|
|
|
|
case 1:
|
|
|
|
loop_start = read_32bitLE(current_chunk+0x08+0x04+0x04, streamFile);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
loop_start = read_32bitLE(current_chunk+0x08+0x04+0x04, streamFile);
|
|
|
|
loop_end = read_32bitLE(current_chunk+0x08+0x1c+0x04, streamFile);
|
|
|
|
/* cues can be unordered */
|
|
|
|
if (loop_start > loop_end) {
|
|
|
|
long temp = loop_start;
|
|
|
|
loop_start = loop_end;
|
|
|
|
loop_end = temp;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-08-26 02:26:38 +02:00
|
|
|
case 0x4C495354: /* "LIST" */
|
2019-02-09 23:43:28 +01:00
|
|
|
/* "LIST" is chunk-based too but in practice sfl always follows the same order */
|
|
|
|
switch (read_32bitBE(current_chunk+0x08+0x00, streamFile)) {
|
2018-08-26 02:26:38 +02:00
|
|
|
case 0x6164746C: /* "adtl" */
|
2019-02-09 23:43:28 +01:00
|
|
|
if (read_32bitBE(current_chunk+0x08+0x04, streamFile) == 0x6C747874 && /* "ltxt" */
|
|
|
|
read_32bitBE(current_chunk+0x08+0x14, streamFile) == 0x72676E20) { /* "rgn " */
|
|
|
|
loop_end = read_32bitLE(current_chunk+0x08+0x10, streamFile) + loop_start;
|
|
|
|
}
|
2008-07-22 04:18:32 +02:00
|
|
|
break;
|
2019-02-09 23:43:28 +01:00
|
|
|
|
|
|
|
case 0x6c61626c: /* "labl" */
|
2008-07-22 04:18:32 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2019-02-09 23:43:28 +01:00
|
|
|
|
|
|
|
case 0x53465049: /* "SFPI": filename info */
|
2008-07-22 04:18:32 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-08-26 02:26:38 +02:00
|
|
|
current_chunk += 0x08+chunk_size;
|
2019-02-09 23:43:28 +01:00
|
|
|
|
|
|
|
/* there may be padding bytes, included in riff_size but not enough to make a new chunk */
|
|
|
|
if (current_chunk + 0x08 > file_size)
|
|
|
|
break;
|
2008-07-22 04:18:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-09 23:43:28 +01:00
|
|
|
loop_flag = (loop_end > 0);
|
|
|
|
|
|
|
|
/* install loops (sfl .ogg often has song endings too, use the base .ogg for those) */
|
2008-07-22 04:18:32 +02:00
|
|
|
if (loop_flag) {
|
2018-08-26 02:26:38 +02:00
|
|
|
vgmstream_force_loop(vgmstream,loop_flag,loop_start, loop_end);
|
2008-07-22 04:18:32 +02:00
|
|
|
}
|
|
|
|
|
2018-08-26 02:26:38 +02:00
|
|
|
close_streamfile(streamData);
|
2008-07-22 04:18:32 +02:00
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
2018-08-26 02:26:38 +02:00
|
|
|
close_streamfile(streamData);
|
|
|
|
close_vgmstream(vgmstream);
|
2008-07-22 04:18:32 +02:00
|
|
|
return NULL;
|
|
|
|
}
|