VGMStream: Rewrite file interface based on the VGMStream stdio interface, only using CogSource files

CQTexperiment
Christopher Snowhill 2022-01-06 22:12:34 -08:00
parent 71fd2eb0f8
commit 1eb2270049
3 changed files with 211 additions and 94 deletions

View File

@ -106,7 +106,7 @@ static NSString* get_description_tag(const char* description, const char *tag, c
codec = get_description_tag(description, "encoding: ", 0); codec = get_description_tag(description, "encoding: ", 0);
STREAMFILE *tagFile = cogsf_create_from_url(tagurl); STREAMFILE *tagFile = open_cog_streamfile_from_url(tagurl);
if (tagFile) { if (tagFile) {
VGMSTREAM_TAGS *tags; VGMSTREAM_TAGS *tags;
const char *tag_key, *tag_val; const char *tag_key, *tag_val;

View File

@ -9,14 +9,23 @@
#import <libvgmstream/vgmstream.h> #import <libvgmstream/vgmstream.h>
#import <libvgmstream/plugins.h> #import <libvgmstream/plugins.h>
/* a STREAMFILE that operates via standard IO using a buffer */
typedef struct _COGSTREAMFILE { typedef struct _COGSTREAMFILE {
STREAMFILE sf; STREAMFILE vt; /* callbacks */
void *file;
offv_t offset; void* infile; /* CogSource, retained */
char name[PATH_LIMIT]; char name[PATH_LIMIT]; /* FILE filename */
int name_len; /* cache */
offv_t offset; /* last read offset (info) */
offv_t buf_offset; /* current buffer data start */
uint8_t* buf; /* data buffer */
size_t buf_size; /* max buffer size */
size_t valid_size; /* current buffer size */
size_t file_size; /* buffered file size */
} COGSTREAMFILE; } COGSTREAMFILE;
STREAMFILE *cogsf_create_from_url(NSURL * url); STREAMFILE* open_cog_streamfile_from_url(NSURL* url);
STREAMFILE* open_cog_streamfile(const char* filename);
VGMSTREAM *init_vgmstream_from_cogfile(const char *path, int subsong); VGMSTREAM *init_vgmstream_from_cogfile(const char *path, int subsong);

View File

@ -20,90 +20,201 @@ void register_log_callback() {
vgmstream_set_log_callback(VGM_LOG_LEVEL_ALL, &log_callback); vgmstream_set_log_callback(VGM_LOG_LEVEL_ALL, &log_callback);
} }
static void cogsf_seek(COGSTREAMFILE *this, offv_t offset) { static STREAMFILE* open_cog_streamfile_buffer(const char* const filename, size_t buf_size);
if (!this) return; static STREAMFILE* open_cog_streamfile_buffer_by_file(id infile, const char* const filename, size_t buf_size);
NSObject* _file = (__bridge NSObject *)(this->file);
static size_t cogsf_read(COGSTREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) {
size_t read_total = 0;
if (!sf || !sf->infile || !dst || length <= 0 || offset < 0)
return 0;
//;VGM_LOG("cogsf: read %lx + %x (buf %lx + %x)\n", offset, length, sf->buf_offset, sf->valid_size);
/* is the part of the requested length in the buffer? */
if (offset >= sf->buf_offset && offset < sf->buf_offset + sf->valid_size) {
size_t buf_limit;
int buf_into = (int)(offset - sf->buf_offset);
buf_limit = sf->valid_size - buf_into;
if (buf_limit > length)
buf_limit = length;
//;VGM_LOG("cogsf: copy buf %lx + %x (+ %x) (buf %lx + %x)\n", offset, length_to_read, (length - length_to_read), sf->buf_offset, sf->valid_size);
memcpy(dst, sf->buf + buf_into, buf_limit);
read_total += buf_limit;
length -= buf_limit;
offset += buf_limit;
dst += buf_limit;
}
#ifdef VGM_DEBUG_OUTPUT
if (offset < sf->buf_offset && length > 0) {
//VGM_LOG("cogsf: rebuffer, requested %x vs %x (sf %x)\n", (uint32_t)offset, (uint32_t)sf->buf_offset, (uint32_t)sf);
//sf->rebuffer++;
//if (rebuffer > N) ...
}
#endif
NSObject* _file = (__bridge NSObject *)(sf->infile);
id<CogSource> __unsafe_unretained file = (id) _file; id<CogSource> __unsafe_unretained file = (id) _file;
if ([file seek:offset whence:SEEK_SET] != 0)
this->offset = offset; /* read the rest of the requested length */
while (length > 0) {
size_t length_to_read;
/* ignore requests at EOF */
if (offset >= sf->file_size) {
//offset = sf->file_size; /* seems fseek doesn't clamp offset */
VGM_ASSERT_ONCE(offset > sf->file_size, "COGSF: reading over file_size 0x%x @ 0x%x + 0x%x\n", sf->file_size, (uint32_t)offset, length);
break;
}
/* position to new offset */
if (![file seek:offset whence:SEEK_SET]) {
break; /* this shouldn't happen in our code */
}
/* fill the buffer (offset now is beyond buf_offset) */
sf->buf_offset = offset;
sf->valid_size = [file read:sf->buf amount:sf->buf_size];
//;VGM_LOG("cogsf: read buf %lx + %x\n", sf->buf_offset, sf->valid_size);
/* decide how much must be read this time */
if (length > sf->buf_size)
length_to_read = sf->buf_size;
else else
this->offset = [file tell]; length_to_read = length;
/* give up on partial reads (EOF) */
if (sf->valid_size < length_to_read) {
memcpy(dst, sf->buf, sf->valid_size);
offset += sf->valid_size;
read_total += sf->valid_size;
break;
} }
static offv_t cogsf_get_size(COGSTREAMFILE *this) { /* use the new buffer */
if (!this) return 0; memcpy(dst, sf->buf, length_to_read);
NSObject* _file = (__bridge NSObject *)(this->file); offset += length_to_read;
id<CogSource> __unsafe_unretained file = (id) _file; read_total += length_to_read;
offv_t offset = [file tell]; length -= length_to_read;
[file seek:0 whence:SEEK_END]; dst += length_to_read;
offv_t size = [file tell];
[file seek:offset whence:SEEK_SET];
return size;
} }
static offv_t cogsf_get_offset(COGSTREAMFILE *this) { sf->offset = offset; /* last fread offset */
if (!this) return 0; return read_total;
return this->offset; }
static size_t cogsf_get_size(COGSTREAMFILE* sf) {
return sf->file_size;
}
static offv_t cogsf_get_offset(COGSTREAMFILE* sf) {
return sf->offset;
}
static void cogsf_get_name(COGSTREAMFILE* sf, char* name, size_t name_size) {
int copy_size = sf->name_len + 1;
if (copy_size > name_size)
copy_size = name_size;
memcpy(name, sf->name, copy_size);
name[copy_size - 1] = '\0';
} }
static void cogsf_get_name(COGSTREAMFILE *this, char *buffer, size_t length) { static STREAMFILE* cogsf_open(COGSTREAMFILE* sf, const char* const filename, size_t buf_size) {
if (!this) { if (!filename)
memset(buffer, 0, length); return NULL;
return;
} // a normal open, open a new file
if (length > sizeof(this->name)) return open_cog_streamfile_buffer(filename, buf_size);
length = sizeof(this->name);
strncpy(buffer, this->name, length);
buffer[length-1]='\0';
} }
static size_t cogsf_read(COGSTREAMFILE *this, uint8_t *dest, offv_t offset, size_t length) { static void cogsf_close(COGSTREAMFILE* sf) {
if (!this) return 0; if (sf->infile)
NSObject* _file = (__bridge NSObject *)(this->file); CFBridgingRelease(sf->infile);
id<CogSource> __unsafe_unretained file = (id) _file; free(sf->buf);
size_t read; free(sf);
if (this->offset != offset)
cogsf_seek(this, offset);
read = [file read:dest amount:length];
if (read > 0)
this->offset += read;
return read;
} }
static void cogsf_close(COGSTREAMFILE *this) {
if (this) { static STREAMFILE* open_cog_streamfile_buffer_by_file(id<CogSource> infile, const char* const filename, size_t buf_size) {
CFBridgingRelease(this->file); uint8_t* buf = NULL;
free(this); COGSTREAMFILE* this_sf = NULL;
} int infile_retained = 0;
buf = calloc(buf_size, sizeof(uint8_t));
if (!buf) goto fail;
this_sf = calloc(1, sizeof(COGSTREAMFILE));
if (!this_sf) goto fail;
this_sf->vt.read = (void*)cogsf_read;
this_sf->vt.get_size = (void*)cogsf_get_size;
this_sf->vt.get_offset = (void*)cogsf_get_offset;
this_sf->vt.get_name = (void*)cogsf_get_name;
this_sf->vt.open = (void*)cogsf_open;
this_sf->vt.close = (void*)cogsf_close;
if (infile) {
this_sf->infile = (void*)CFBridgingRetain(infile);
infile_retained = 1;
} }
static STREAMFILE *cogsf_create_from_path(const char *path); this_sf->buf_size = buf_size;
static STREAMFILE *cogsf_open(COGSTREAMFILE *this, const char *const filename,size_t buffersize) { this_sf->buf = buf;
if (!filename) return NULL;
return cogsf_create_from_path(filename); this_sf->name_len = strlen(filename);
if (this_sf->name_len >= sizeof(this_sf->name))
goto fail;
memcpy(this_sf->name, filename, this_sf->name_len);
this_sf->name[this_sf->name_len] = '\0';
/* cache file_size */
if (infile) {
[infile seek:0 whence:SEEK_END];
this_sf->file_size = [infile tell];
[infile seek:0 whence:SEEK_SET];
}
else {
this_sf->file_size = 0; /* allow virtual, non-existing files */
} }
static STREAMFILE *cogsf_create(id file, const char *path) { /* Typically fseek(o)/ftell(o) may only handle up to ~2.14GB, signed 32b = 0x7FFFFFFF
COGSTREAMFILE *streamfile = malloc(sizeof(COGSTREAMFILE)); * (happens in banks like FSB, though rarely). Should work if configured properly, log otherwise. */
if (this_sf->file_size == 0xFFFFFFFF) { /* -1 on error */
if (!streamfile) return NULL; vgm_logi("STREAMFILE: file size too big (report)\n");
goto fail; /* can be ignored but may result in strange/unexpected behaviors */
memset(streamfile,0,sizeof(COGSTREAMFILE));
streamfile->sf.read = (void*)cogsf_read;
streamfile->sf.get_size = (void*)cogsf_get_size;
streamfile->sf.get_offset = (void*)cogsf_get_offset;
streamfile->sf.get_name = (void*)cogsf_get_name;
streamfile->sf.open = (void*)cogsf_open;
streamfile->sf.close = (void*)cogsf_close;
streamfile->file = (void*)CFBridgingRetain(file);
streamfile->offset = 0;
strncpy(streamfile->name, path, sizeof(streamfile->name));
return &streamfile->sf;
} }
STREAMFILE *cogsf_create_from_path(const char *path) { return &this_sf->vt;
NSString * urlString = [NSString stringWithUTF8String:path];
fail:
if (infile_retained) CFBridgingRelease(this_sf->infile);
free(buf);
free(this_sf);
return NULL;
}
static STREAMFILE* open_cog_streamfile_buffer_from_url(NSURL* url, const char* const filename, size_t bufsize) {
id<CogSource> infile;
STREAMFILE* sf = NULL;
id audioSourceClass = NSClassFromString(@"AudioSource");
infile = [audioSourceClass audioSourceForURL:url];
if (![infile open:url]) {
/* allow non-existing files in some cases */
if (!vgmstream_is_virtual_filename(filename))
return NULL;
}
if (![infile seekable])
return NULL;
return open_cog_streamfile_buffer_by_file(infile, filename, bufsize);
}
static STREAMFILE* open_cog_streamfile_buffer(const char* const filename, size_t bufsize) {
NSString * urlString = [NSString stringWithUTF8String:filename];
NSURL * url = [NSURL URLWithDataRepresentation:[urlString dataUsingEncoding:NSUTF8StringEncoding] relativeToURL:nil]; NSURL * url = [NSURL URLWithDataRepresentation:[urlString dataUsingEncoding:NSUTF8StringEncoding] relativeToURL:nil];
if ([url fragment]) { if ([url fragment]) {
@ -116,23 +227,21 @@ STREAMFILE *cogsf_create_from_path(const char *path) {
} }
} }
return cogsf_create_from_url(url); return open_cog_streamfile_buffer_from_url(url, filename, bufsize);
} }
STREAMFILE *cogsf_create_from_url(NSURL * url) { STREAMFILE* open_cog_streamfile_from_url(NSURL* url) {
id<CogSource> source; return open_cog_streamfile_buffer_from_url(url, [[[url absoluteString] stringByRemovingPercentEncoding] UTF8String], STREAMFILE_DEFAULT_BUFFER_SIZE);
id audioSourceClass = NSClassFromString(@"AudioSource");
source = [audioSourceClass audioSourceForURL:url];
if (![source open:url])
return 0;
if (![source seekable])
return 0;
return cogsf_create(source, [[[url absoluteString] stringByRemovingPercentEncoding] UTF8String]);
} }
STREAMFILE* open_cog_streamfile(const char* filename) {
return open_cog_streamfile_buffer(filename, STREAMFILE_DEFAULT_BUFFER_SIZE);
}
//STREAMFILE* open_cog_streamfile_by_file(id<CogSource> file, const char* filename) {
// return open_cog_streamfile_buffer_by_file(file, filename, STREAMFILE_DEFAULT_BUFFER_SIZE);
//}
VGMSTREAM *init_vgmstream_from_cogfile(const char *path, int subsong) { VGMSTREAM *init_vgmstream_from_cogfile(const char *path, int subsong) {
STREAMFILE *sf; STREAMFILE *sf;
VGMSTREAM *vgm = NULL; VGMSTREAM *vgm = NULL;
@ -140,7 +249,7 @@ VGMSTREAM *init_vgmstream_from_cogfile(const char *path, int subsong) {
if (!subsong) if (!subsong)
subsong = 1; subsong = 1;
sf = cogsf_create_from_path(path); sf = open_cog_streamfile(path);
if (sf) { if (sf) {
sf->stream_index = subsong; sf->stream_index = subsong;
@ -150,4 +259,3 @@ VGMSTREAM *init_vgmstream_from_cogfile(const char *path, int subsong) {
return vgm; return vgm;
} }