Fix some .SFL looping and SLI cleanup [Hanachirasu (PC)]

This commit is contained in:
bnnm 2018-08-26 02:26:38 +02:00
parent 88f50985de
commit b02eeeff04
4 changed files with 243 additions and 253 deletions

View File

@ -109,8 +109,6 @@ typedef struct {
} ogg_vorbis_meta_info_t;
VGMSTREAM * init_vgmstream_ogg_vorbis_callbacks(STREAMFILE *streamFile, ov_callbacks *callbacks, off_t other_header_bytes, const ogg_vorbis_meta_info_t *ovmi);
VGMSTREAM * init_vgmstream_sli_ogg(STREAMFILE * streamFile);
#endif
VGMSTREAM * init_vgmstream_hca(STREAMFILE *streamFile);
@ -129,7 +127,9 @@ VGMSTREAM * init_vgmstream_mp4_aac_offset(STREAMFILE *streamFile, uint64_t start
VGMSTREAM * init_vgmstream_akb_mp4(STREAMFILE *streamFile);
#endif
VGMSTREAM * init_vgmstream_sfl(STREAMFILE * streamFile);
VGMSTREAM * init_vgmstream_sli_ogg(STREAMFILE * streamFile);
VGMSTREAM * init_vgmstream_sfl_ogg(STREAMFILE * streamFile);
VGMSTREAM * init_vgmstream_sadb(STREAMFILE *streamFile);

View File

@ -1,12 +1,118 @@
#include "../vgmstream.h"
#include "meta.h"
static void parse_adtl(off_t adtl_offset, off_t adtl_length, STREAMFILE *streamFile, long *loop_start, long *loop_end, int *loop_flag);
/* .sfl - odd RIFF-formatted files that go along with .ogg [Hanachirasu (PC), Touhou 10.5 (PC)] */
VGMSTREAM * init_vgmstream_sfl_ogg(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL;
STREAMFILE * streamData = NULL;
int loop_flag = 0;
long loop_start_ms = -1;
long loop_end_ms = -1;
/* checks */
if (!check_extensions(streamFile, "sfl"))
goto fail;
if (read_32bitBE(0x00,streamFile) != 0x52494646) /* "RIFF" */
goto fail;
if (read_32bitBE(0x08,streamFile) != 0x5346504C) /* "SFPL" */
goto fail;
{
/* 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) {
/* try again with file.sfl=header + file.ogg=daba */
streamData = open_streamfile_by_ext(streamFile,"ogg");
if (!streamData) goto fail;
}
else {
if (!check_extensions(streamData, "ogg"))
goto fail;
}
}
#ifdef VGM_USE_VORBIS
/* let the real initer do the parsing */
vgmstream = init_vgmstream_ogg_vorbis(streamData);
if (!vgmstream) goto fail;
vgmstream->meta_type = meta_OGG_SFL;
#else
goto fail;
#endif
#include "meta.h"
#include "../layout/layout.h"
#include "../util.h"
/* read through chunks to verify format and find metadata */
{
size_t riff_size, file_size;
off_t current_chunk = 0x0c; /* start with first chunk */
/* .sfl, odd RIFF-formatted files that go along with oggs */
riff_size = read_32bitLE(0x04,streamFile);
file_size = get_streamfile_size(streamFile);
if (file_size < riff_size+0x08)
goto fail;
while (current_chunk < file_size) {
uint32_t chunk_type = read_32bitBE(current_chunk+0x00,streamFile);
off_t chunk_size = read_32bitLE(current_chunk+0x04,streamFile);
/* There seem to be a few bytes left over, included in the
* RIFF but not enough to make a new chunk. */
if (current_chunk+0x08 > file_size) break;
if (current_chunk+0x08+chunk_size > file_size)
goto fail;
switch(chunk_type) {
case 0x4C495354: /* "LIST" */
switch (read_32bitBE(current_chunk+0x08, streamFile)) {
case 0x6164746C: /* "adtl" */
/* yay, atdl is its own little world */
parse_adtl(current_chunk + 0x08, chunk_size, streamFile,
&loop_start_ms,&loop_end_ms,&loop_flag);
break;
default:
break;
}
break;
default:
break;
}
current_chunk += 0x08+chunk_size;
}
}
/* install loops */
if (loop_flag) {
int loop_start = (long long)loop_start_ms * vgmstream->sample_rate / 1000;
int loop_end = (long long)loop_end_ms * vgmstream->sample_rate / 1000;
vgmstream_force_loop(vgmstream,loop_flag,loop_start, loop_end);
}
/* sfl .ogg often has song endings (use the base .ogg for those) */
close_streamfile(streamData);
return vgmstream;
fail:
close_streamfile(streamData);
close_vgmstream(vgmstream);
return NULL;
}
/* return milliseconds */
static long parse_adtl_marker(unsigned char * marker) {
long hh,mm,ss,ms;
if (memcmp("Marker ",marker,7)) return -1;
if (4 != sscanf((char*)marker+7,"%ld:%ld:%ld.%ld",&hh,&mm,&ss,&ms))
return -1;
return ((hh*60+mm)*60+ss)*1000+ms;
}
/* return milliseconds */
static int parse_region(unsigned char * region, long *start, long *end) {
@ -22,161 +128,67 @@ static int parse_region(unsigned char * region, long *start, long *end) {
*start = ((start_hh*60+start_mm)*60+start_ss)*1000+start_ms;
*end = ((end_hh*60+end_mm)*60+end_ss)*1000+end_ms;
return 0;
}
/* loop points have been found hiding here */
static void parse_adtl(off_t adtl_offset, off_t adtl_length, STREAMFILE *streamFile,
long *loop_start, long *loop_end, int *loop_flag) {
int loop_found = 0;
static void parse_adtl(off_t adtl_offset, off_t adtl_length, STREAMFILE *streamFile, long *loop_start, long *loop_end, int *loop_flag) {
int loop_start_found = 0;
int loop_end_found = 0;
off_t current_chunk = adtl_offset+0x04;
off_t current_chunk = adtl_offset+4;
while (current_chunk < adtl_offset + adtl_length) {
uint32_t chunk_type = read_32bitBE(current_chunk+0x00,streamFile);
off_t chunk_size = read_32bitLE(current_chunk+0x04,streamFile);
while (current_chunk < adtl_offset+adtl_length) {
uint32_t chunk_type = read_32bitBE(current_chunk,streamFile);
off_t chunk_size = read_32bitLE(current_chunk+4,streamFile);
if (current_chunk+8+chunk_size > adtl_offset+adtl_length) return;
if (current_chunk+0x08+chunk_size > adtl_offset+adtl_length)
return;
switch(chunk_type) {
case 0x6c61626c: /* labl */
{
unsigned char *labelcontent;
labelcontent = malloc(chunk_size-4);
if (!labelcontent) return;
if (read_streamfile(labelcontent,current_chunk+0xc,
chunk_size-4,streamFile)!=chunk_size-4) {
free(labelcontent);
return;
}
if (!loop_found &&
parse_region(labelcontent,loop_start,loop_end)>=0)
{
loop_found = 1;
}
case 0x6c61626c: { /* "labl" */
unsigned char *labelcontent = malloc(chunk_size-0x04);
if (!labelcontent) return;
if (read_streamfile(labelcontent,current_chunk+0x0c, chunk_size-0x04,streamFile) != chunk_size-0x04) {
free(labelcontent);
return;
}
switch (read_32bitLE(current_chunk+8,streamFile)) {
case 1:
if (!loop_start_found && (*loop_start = parse_adtl_marker(labelcontent)) >= 0)
loop_start_found = 1;
if (!loop_start_found && !loop_end_found && parse_region(labelcontent,loop_start,loop_end) >= 0) {
loop_start_found = 1;
loop_end_found = 1;
}
break;
case 2:
if (!loop_end_found && (*loop_end = parse_adtl_marker(labelcontent)) >= 0)
loop_end_found = 1;
break;
default:
break;
}
free(labelcontent);
break;
}
default:
break;
}
current_chunk += 8 + chunk_size;
current_chunk += 0x08 + chunk_size;
}
if (loop_found) *loop_flag = 1;
if (loop_start_found && loop_end_found)
*loop_flag = 1;
/* labels don't seem to be consistently ordered */
if (*loop_start > *loop_end) {
long temp = *loop_start;
*loop_start = *loop_end;
*loop_end = temp;
}
}
VGMSTREAM * init_vgmstream_sfl(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL;
STREAMFILE * streamFileOGG = NULL;
char filenameOGG[PATH_LIMIT];
char filename[PATH_LIMIT];
off_t file_size = -1;
int loop_flag = 0;
long loop_start_ms = -1;
long loop_end_ms = -1;
uint32_t riff_size;
/* check extension, case insensitive */
streamFile->get_name(streamFile,filename,sizeof(filename));
if (strcasecmp("sfl",filename_extension(filename))) goto fail;
/* check header */
if ((uint32_t)read_32bitBE(0,streamFile)!=0x52494646) /* "RIFF" */
goto fail;
/* check for SFPL form */
if ((uint32_t)read_32bitBE(8,streamFile)!=0x5346504C) /* "SFPL" */
goto fail;
/* check for .OGG file */
strcpy(filenameOGG,filename);
strcpy(filenameOGG+strlen(filenameOGG)-3,"ogg");
streamFileOGG = streamFile->open(streamFile,filenameOGG,STREAMFILE_DEFAULT_BUFFER_SIZE);
if (!streamFileOGG) {
goto fail;
}
/* let the real initer do the parsing */
vgmstream = init_vgmstream_ogg_vorbis(streamFileOGG);
if (!vgmstream) goto fail;
close_streamfile(streamFileOGG);
streamFileOGG = NULL;
/* now that we have an ogg, proceed with parsing the .sfl */
riff_size = read_32bitLE(4,streamFile);
file_size = get_streamfile_size(streamFile);
/* check for tructated RIFF */
if (file_size < riff_size+8) goto fail;
/* read through chunks to verify format and find metadata */
{
off_t current_chunk = 0xc; /* start with first chunk */
while (current_chunk < file_size) {
uint32_t chunk_type = read_32bitBE(current_chunk,streamFile);
off_t chunk_size = read_32bitLE(current_chunk+4,streamFile);
/* There seem to be a few bytes left over, included in the
* RIFF but not enough to make a new chunk.
*/
if (current_chunk+8 > file_size) break;
if (current_chunk+8+chunk_size > file_size) goto fail;
switch(chunk_type) {
case 0x4C495354: /* LIST */
/* what lurks within?? */
switch (read_32bitBE(current_chunk + 8, streamFile)) {
case 0x6164746C: /* adtl */
/* yay, atdl is its own little world */
parse_adtl(current_chunk + 8, chunk_size,
streamFile,
&loop_start_ms,&loop_end_ms,&loop_flag);
break;
default:
break;
}
break;
default:
/* ignorance is bliss */
break;
}
current_chunk += 8+chunk_size;
}
}
if (loop_flag) {
/* install loops */
if (!vgmstream->loop_flag) {
vgmstream->loop_flag = 1;
vgmstream->loop_ch = calloc(vgmstream->channels,
sizeof(VGMSTREAMCHANNEL));
if (!vgmstream->loop_ch) goto fail;
}
vgmstream->loop_start_sample = (long long)loop_start_ms*vgmstream->sample_rate/1000;
vgmstream->loop_end_sample = (long long)loop_end_ms*vgmstream->sample_rate/1000;
}
vgmstream->meta_type = meta_OGG_SFL;
return vgmstream;
/* clean up anything we may have opened */
fail:
if (streamFileOGG) close_streamfile(streamFileOGG);
if (vgmstream) close_vgmstream(vgmstream);
return NULL;
}
#endif

View File

@ -1,121 +1,99 @@
#include "../vgmstream.h"
#include "meta.h"
#include <ctype.h>
/* .sli - loop points associated with a similarly named .ogg [Fate/Stay Night (PC), World End Economica (PC)]*/
VGMSTREAM * init_vgmstream_sli_ogg(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL;
STREAMFILE * streamData = NULL;
int32_t loop_start = -1, loop_length = -1;
int32_t loop_from = -1, loop_to = -1;
/* checks */
if (!check_extensions(streamFile, "sli"))
goto fail;
{
/* try with file.ogg.sli=header and file.ogg=data */
char basename[PATH_LIMIT];
get_streamfile_basename(streamFile,basename,PATH_LIMIT);
streamData = open_streamfile_by_filename(streamFile, basename);
if (!streamData) goto fail;
if (!check_extensions(streamData, "ogg"))
goto fail;
}
#ifdef VGM_USE_VORBIS
#include <ctype.h>
#include "meta.h"
#include "../util.h"
#ifdef WIN32
#define DIRSEP '\\'
/* let the real initer do the parsing */
vgmstream = init_vgmstream_ogg_vorbis(streamData);
if (!vgmstream) goto fail;
#else
#define DIRSEP '/'
goto fail;
#endif
/* .sli is a file with loop points, associated with a similarly named .ogg */
/* find loop text */
{
char linebuffer[PATH_LIMIT];
size_t bytes_read;
off_t sli_offset;
int done;
VGMSTREAM * init_vgmstream_sli_ogg(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL;
STREAMFILE * streamFileOGG = NULL;
char filename[PATH_LIMIT];
char filenameOGG[PATH_LIMIT];
char linebuffer[PATH_LIMIT];
off_t bytes_read;
off_t sli_offset;
int done;
int32_t loop_start = -1;
int32_t loop_length = -1;
int32_t loop_from = -1;
int32_t loop_to = -1;
sli_offset = 0;
while ((loop_start == -1 || loop_length == -1) && sli_offset < get_streamfile_size(streamFile)) {
char *endptr, *foundptr;
/* check extension, case insensitive */
streamFile->get_name(streamFile,filename,sizeof(filename));
if (strcasecmp("sli",filename_extension(filename))) goto fail;
bytes_read = get_streamfile_text_line(sizeof(linebuffer),linebuffer,sli_offset,streamFile,&done);
if (!done) goto fail;
/* check for .OGG file */
strcpy(filenameOGG,filename);
/* strip off .sli */
filenameOGG[strlen(filenameOGG)-4]='\0';
if (memcmp("LoopStart=",linebuffer,10)==0 && linebuffer[10] != '\0') {
loop_start = strtol(linebuffer+10,&endptr,10);
if (*endptr != '\0') {
loop_start = -1; /* if it didn't parse cleanly */
}
}
else if (memcmp("LoopLength=",linebuffer,11)==0 && linebuffer[11] != '\0') {
loop_length = strtol(linebuffer+11,&endptr,10);
if (*endptr != '\0') {
loop_length = -1; /* if it didn't parse cleanly */
}
}
streamFileOGG = streamFile->open(streamFile,filenameOGG,STREAMFILE_DEFAULT_BUFFER_SIZE);
if (!streamFileOGG) {
goto fail;
/* a completely different format (2.0?), also with .sli extension and can be handled similarly */
if ((foundptr = strstr(linebuffer,"To=")) != NULL && isdigit(foundptr[3])) {
loop_to = strtol(foundptr+3,&endptr,10);
if (*endptr != ';') {
loop_to = -1;
}
}
if ((foundptr = strstr(linebuffer,"From=")) != NULL && isdigit(foundptr[5])) {
loop_from = strtol(foundptr+5,&endptr,10);
if (*endptr != ';') {
loop_from = -1;
}
}
sli_offset += bytes_read;
}
}
/* let the real initer do the parsing */
vgmstream = init_vgmstream_ogg_vorbis(streamFileOGG);
if (!vgmstream) goto fail;
close_streamfile(streamFileOGG);
streamFileOGG = NULL;
sli_offset = 0;
while ((loop_start == -1 || loop_length == -1) && sli_offset < get_streamfile_size(streamFile)) {
char *endptr;
char *foundptr;
bytes_read=get_streamfile_text_line(sizeof(linebuffer),linebuffer,sli_offset,streamFile,&done);
if (!done) goto fail;
if (!memcmp("LoopStart=",linebuffer,10) && linebuffer[10]!='\0') {
loop_start = strtol(linebuffer+10,&endptr,10);
if (*endptr != '\0') {
/* if it didn't parse cleanly */
loop_start = -1;
}
}
else if (!memcmp("LoopLength=",linebuffer,11) && linebuffer[11]!='\0') {
loop_length = strtol(linebuffer+11,&endptr,10);
if (*endptr != '\0') {
/* if it didn't parse cleanly */
loop_length = -1;
}
}
/* a completely different format, also with .sli extension and can be handled similarly */
if ((foundptr=strstr(linebuffer,"To="))!=NULL && isdigit(foundptr[3])) {
loop_to = strtol(foundptr+3,&endptr,10);
if (*endptr != ';') {
loop_to = -1;
}
}
if ((foundptr=strstr(linebuffer,"From="))!=NULL && isdigit(foundptr[5])) {
loop_from = strtol(foundptr+5,&endptr,10);
if (*endptr != ';') {
loop_from = -1;
}
}
sli_offset += bytes_read;
if (loop_start != -1 && loop_length != -1) {
vgmstream_force_loop(vgmstream,1,loop_start, loop_start+loop_length);
vgmstream->meta_type = meta_OGG_SLI;
}
else if (loop_from != -1 && loop_to != -1) {
vgmstream_force_loop(vgmstream,1,loop_to, loop_from);
vgmstream->meta_type = meta_OGG_SLI2;
}
else {
goto fail; /* if there's no loop points the .sli wasn't valid */
}
if ((loop_start != -1 && loop_length != -1) ||
(loop_to != -1 && loop_from != -1)) {
/* install loops */
if (!vgmstream->loop_flag) {
vgmstream->loop_flag = 1;
vgmstream->loop_ch = calloc(vgmstream->channels,
sizeof(VGMSTREAMCHANNEL));
if (!vgmstream->loop_ch) goto fail;
}
if (loop_to != -1 && loop_from != -1) {
vgmstream->loop_start_sample = loop_to;
vgmstream->loop_end_sample = loop_from;
vgmstream->meta_type = meta_OGG_SLI2;
} else {
vgmstream->loop_start_sample = loop_start;
vgmstream->loop_end_sample = loop_start+loop_length;
vgmstream->meta_type = meta_OGG_SLI;
}
} else goto fail; /* if there's no loop points the .sli wasn't valid */
close_streamfile(streamData);
return vgmstream;
/* clean up anything we may have opened */
fail:
if (streamFileOGG) close_streamfile(streamFileOGG);
if (vgmstream) close_vgmstream(vgmstream);
close_streamfile(streamData);
close_vgmstream(vgmstream);
return NULL;
}
#endif

View File

@ -60,9 +60,9 @@ VGMSTREAM * (*init_vgmstream_functions[])(STREAMFILE *streamFile) = {
init_vgmstream_genh,
#ifdef VGM_USE_VORBIS
init_vgmstream_ogg_vorbis,
init_vgmstream_sli_ogg,
init_vgmstream_sfl,
#endif
init_vgmstream_sli_ogg,
init_vgmstream_sfl_ogg,
#if 0
init_vgmstream_mp4_aac,
#endif