diff --git a/CMakeLists.txt b/CMakeLists.txt
index c6e80a6..b8d9c35 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -14,6 +14,8 @@ add_subdirectory(lib/psflib)
add_subdirectory(lib/Highly_Theoretical)
set(SSF_SOURCES src/SSFCodec.cpp)
+set(SSF_HEADERS src/CircularBuffer.h
+ src/SSFCodec.h)
set(DEPLIBS highly_theoretical psflib ${ZLIB_LIBRARIES})
diff --git a/audiodecoder.ssf/addon.xml.in b/audiodecoder.ssf/addon.xml.in
index 50532fe..67ae882 100644
--- a/audiodecoder.ssf/addon.xml.in
+++ b/audiodecoder.ssf/addon.xml.in
@@ -1,19 +1,19 @@
@ADDON_DEPENDS@
- SSF/DSF Audio Decoder
- SSF/DSF Audio Decoder
+ Sega SSF/DSF Audio Decoder
+ Sega Saturn Sound Format (SSF) and Dreamcast Sound Format (DSF) is an audio format based on PSF. It stores audio ripped from the ROMs of Sega Saturn and Sega Dreamcast games.
@PLATFORM@
diff --git a/audiodecoder.ssf/icon.png b/audiodecoder.ssf/icon.png
new file mode 100644
index 0000000..9a36d8a
Binary files /dev/null and b/audiodecoder.ssf/icon.png differ
diff --git a/audiodecoder.ssf/resources/language/resource.language.en_gb/strings.po b/audiodecoder.ssf/resources/language/resource.language.en_gb/strings.po
new file mode 100644
index 0000000..4c0f9a9
--- /dev/null
+++ b/audiodecoder.ssf/resources/language/resource.language.en_gb/strings.po
@@ -0,0 +1,97 @@
+msgid ""
+msgstr ""
+"Project-Id-Version: XBMC Main Translation Project (Frodo)\n"
+"Report-Msgid-Bugs-To: http://trac.xbmc.org/\n"
+"POT-Creation-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: XBMC Translation Team\n"
+"Language-Team: English (http://www.transifex.com/projects/p/XBMC-Main-Frodo/language/en/)\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Language: en\n"
+"Plural-Forms: nplurals=2; plural=(n != 1);\n"
+
+msgctxt "#30000"
+msgid "Default length"
+msgstr ""
+
+msgctxt "#30001"
+msgid "Used if specified file does not provide the length"
+msgstr ""
+
+msgctxt "#30002"
+msgid "unused"
+msgstr ""
+
+msgctxt "#30003"
+msgid "{0:d} s"
+msgstr ""
+
+msgctxt "#30004"
+msgid "Default fade out time"
+msgstr ""
+
+msgctxt "#30005"
+msgid "Used if specified file does not provide the length and fade"
+msgstr ""
+
+msgctxt "#30006"
+msgid "unused"
+msgstr ""
+
+msgctxt "#30007"
+msgid "{0:d} ms"
+msgstr ""
+
+msgctxt "#30008"
+msgid "Suppress opening silence"
+msgstr ""
+
+msgctxt "#30009"
+msgid "Some files start with silent data, this prevents it and looks for the beginning"
+msgstr ""
+
+msgctxt "#30010"
+msgid "Suppress end silence"
+msgstr ""
+
+msgctxt "#30011"
+msgid "Some files end with silent data, this prevents it and looks for the end"
+msgstr ""
+
+msgctxt "#30012"
+msgid "Second of silence to check"
+msgstr ""
+
+msgctxt "#30013"
+msgid "How many silent seconds are allowed before playback is stopped"
+msgstr ""
+
+msgctxt "#30014"
+msgid "{0:d} s"
+msgstr ""
+
+msgctxt "#30015"
+msgid "Direct output"
+msgstr ""
+
+msgctxt "#30016"
+msgid "Enable direct (dry) output"
+msgstr ""
+
+msgctxt "#30017"
+msgid "DSP emulation"
+msgstr ""
+
+msgctxt "#30018"
+msgid "Enable DSP emulation (for reverb, etc.)"
+msgstr ""
+
+msgctxt "#30019"
+msgid "Dynamic recompiler"
+msgstr ""
+
+msgctxt "#30020"
+msgid "Emulate DSP using dynamic recompiler"
+msgstr ""
diff --git a/audiodecoder.ssf/resources/settings.xml b/audiodecoder.ssf/resources/settings.xml
new file mode 100644
index 0000000..4948234
--- /dev/null
+++ b/audiodecoder.ssf/resources/settings.xml
@@ -0,0 +1,69 @@
+
+
+
+
+
+ 0
+
+ 0
+ 5
+ 500
+
+
+ 30003
+
+
+
+ 10000
+
+ 0
+ 100
+ 50000
+
+
+ 30007
+
+
+
+ true
+
+
+
+ true
+
+
+
+ 5
+
+ 1
+ 1
+ 20
+
+
+
+
+ true
+ true
+
+
+
+
+ 30014
+
+
+
+ true
+
+
+
+ true
+
+
+
+ true
+
+
+
+
+
+
diff --git a/lib/kodi-Highly_Theoretical-note.txt b/lib/kodi-Highly_Theoretical-note.txt
new file mode 100644
index 0000000..4aaabd2
--- /dev/null
+++ b/lib/kodi-Highly_Theoretical-note.txt
@@ -0,0 +1,4 @@
+Highly_Theoretical source from https://github.com/kode54/Highly_Theoretical
+Sync to 2998a4b (24 Jul 2016)
+
+Include fix build for ios/osx/android, see https://github.com/xbmc/audiodecoder.ssf/commit/41ef56f7
diff --git a/lib/kodi-psflib-note.txt b/lib/kodi-psflib-note.txt
new file mode 100644
index 0000000..d736f34
--- /dev/null
+++ b/lib/kodi-psflib-note.txt
@@ -0,0 +1,2 @@
+psflib source from https://github.com/kode54/psflib
+Sync to 6cb3515 (2 May 2019)
diff --git a/lib/psflib/psf2fs.c b/lib/psflib/psf2fs.c
index b53a98b..3e18427 100644
--- a/lib/psflib/psf2fs.c
+++ b/lib/psflib/psf2fs.c
@@ -1,3 +1,27 @@
+/*
+PSFLIB - PSF2FS implementation
+
+Copyright (c) 2012-2015 Christopher Snowhill
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
#include "psf2fs.h"
#include
diff --git a/lib/psflib/psf2fs.h b/lib/psflib/psf2fs.h
index f76d01a..485b9c9 100644
--- a/lib/psflib/psf2fs.h
+++ b/lib/psflib/psf2fs.h
@@ -1,3 +1,27 @@
+/*
+PSFLIB - PSF2FS implementation
+
+Copyright (c) 2012-2015 Christopher Snowhill
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
#ifndef PSF2FS_H
#define PSF2FS_H
diff --git a/lib/psflib/psflib.c b/lib/psflib/psflib.c
index 1e7ccee..06a6f93 100644
--- a/lib/psflib/psflib.c
+++ b/lib/psflib/psflib.c
@@ -1,3 +1,27 @@
+/*
+PSFLIB - Main PSF parser implementation
+
+Copyright (c) 2012-2015 Christopher Snowhill
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
#include "psflib.h"
#include
@@ -12,528 +36,726 @@
#include
#endif
+#undef strdup
#define strdup(s) my_strdup(s)
static char * my_strdup(const char * s)
{
- size_t l;
- char * r;
- if (!s) return NULL;
- l = strlen(s) + 1;
- r = (char *) malloc(l);
- if (!r) return NULL;
- memcpy(r, s, l);
- return r;
+ size_t l;
+ char * r;
+ if (!s) return NULL;
+ l = strlen(s) + 1;
+ r = (char *)malloc(l);
+ if (!r) return NULL;
+ memcpy(r, s, l);
+ return r;
}
-const char * strrpbrk( const char * s, const char * accept)
+const char * strrpbrk(const char * s, const char * accept)
{
- const char * start;
+ const char * start;
- if ( !s || !*s || !accept || !*accept ) return NULL;
+ if (!s || !*s || !accept || !*accept) return NULL;
- start = s;
- s += strlen( s ) - 1;
+ start = s;
+ s += strlen(s) - 1;
- while (s >= start)
- {
- const char *a = accept;
- while (*a != '\0')
- if (*a++ == *s)
- return s;
- --s;
- }
+ while (s >= start)
+ {
+ const char *a = accept;
+ while (*a != '\0')
+ if (*a++ == *s)
+ return s;
+ --s;
+ }
- return NULL;
+ return NULL;
}
enum { max_recursion_depth = 10 };
typedef struct psf_load_state
{
- int depth;
+ int depth;
+
+ unsigned char allowed_version;
- unsigned char allowed_version;
+ char * base_path;
+ const psf_file_callbacks * file_callbacks;
- char * base_path;
- const psf_file_callbacks * file_callbacks;
+ psf_load_callback load_target;
+ void * load_context;
- psf_load_callback load_target;
- void * load_context;
+ psf_info_callback info_target;
+ void * info_context;
+ int info_want_nested_tags;
- psf_info_callback info_target;
- void * info_context;
- int info_want_nested_tags;
+ psf_status_callback status_target;
+ void * status_context;
- char lib_name_temp[32];
+ char lib_name_temp[32];
} psf_load_state;
-static int psf_load_internal( psf_load_state * state, const char * file_name );
+static int psf_load_internal(psf_load_state * state, const char * file_name);
+
-int psf_load( const char * uri, const psf_file_callbacks * file_callbacks, uint8_t allowed_version,
- psf_load_callback load_target, void * load_context, psf_info_callback info_target, void * info_context, int info_want_nested_tags )
+static int psf_want_status(psf_load_state * state);
+static void psf_status(psf_load_state * state, const char * message, int indent);
+
+int psf_load(const char * uri, const psf_file_callbacks * file_callbacks, uint8_t allowed_version,
+ psf_load_callback load_target, void * load_context, psf_info_callback info_target,
+ void * info_context, int info_want_nested_tags, psf_status_callback status_target,
+ void * status_context)
{
- int rval;
+ int rval;
+
+ psf_load_state state;
- psf_load_state state;
+ const char * file_name;
- const char * file_name;
+ if (!uri || !*uri || !file_callbacks || !file_callbacks->path_separators || !*file_callbacks->path_separators || !file_callbacks->fopen ||
+ !file_callbacks->fread || !file_callbacks->fseek || !file_callbacks->fclose || !file_callbacks->ftell) return -1;
- if ( !uri || !*uri || !file_callbacks || !file_callbacks->path_separators || !*file_callbacks->path_separators || !file_callbacks->fopen ||
- !file_callbacks->fread || !file_callbacks->fseek || !file_callbacks->fclose || !file_callbacks->ftell ) return -1;
+ state.depth = 0;
+ state.allowed_version = allowed_version;
+ state.file_callbacks = file_callbacks;
+ state.load_target = load_target;
+ state.load_context = load_context;
+ state.info_target = info_target;
+ state.info_context = info_context;
+ state.info_want_nested_tags = info_want_nested_tags;
+ state.status_target = status_target;
+ state.status_context = status_context;
- state.depth = 0;
- state.allowed_version = allowed_version;
- state.file_callbacks = file_callbacks;
- state.load_target = load_target;
- state.load_context = load_context;
- state.info_target = info_target;
- state.info_context = info_context;
- state.info_want_nested_tags = info_want_nested_tags;
+ state.base_path = strdup(uri);
+ if (!state.base_path)
+ {
+ psf_status(&state, "Out of memory allocating state.base_path\n", 1);
+ return -1;
+ }
- state.base_path = strdup( uri );
- if ( !state.base_path ) return -1;
+ file_name = strrpbrk(uri, file_callbacks->path_separators);
- file_name = strrpbrk( uri, file_callbacks->path_separators );
+ if (file_name)
+ {
+ ++file_name;
+ state.base_path[file_name - uri] = '\0';
+ }
+ else
+ {
+ state.base_path[0] = '\0';
+ file_name = uri;
+ }
- if ( file_name )
- {
- ++file_name;
- state.base_path[ file_name - uri ] = '\0';
- }
- else
- {
- state.base_path[ 0 ] = '\0';
- file_name = uri;
- }
+ rval = psf_load_internal(&state, file_name);
- rval = psf_load_internal( &state, file_name );
+ free(state.base_path);
- free( state.base_path );
+ psf_status(&state, "Done.", 0);
- return rval;
+ return rval;
}
typedef struct psf_tag psf_tag;
struct psf_tag {
- char * name;
- char * value;
- psf_tag * next, *prev;
+ char * name;
+ char * value;
+ psf_tag * next, *prev;
};
#define FIELDS_SPLIT 6
-static const char * fields_to_split[FIELDS_SPLIT] = {"ARTIST", "ALBUM ARTIST", "PRODUCER", "COMPOSER", "PERFORMER", "GENRE"};
+static const char * fields_to_split[FIELDS_SPLIT] = { "ARTIST", "ALBUM ARTIST", "PRODUCER", "COMPOSER", "PERFORMER", "GENRE" };
-static int check_split_value( const char * name )
+static int check_split_value(const char * name)
{
- unsigned i;
- for ( i = 0; i < FIELDS_SPLIT; i++ )
- {
- if ( !strcasecmp( name, fields_to_split[ i ] ) ) return 1;
- }
- return 0;
+ unsigned i;
+ for (i = 0; i < FIELDS_SPLIT; i++)
+ {
+ if (!strcasecmp(name, fields_to_split[i])) return 1;
+ }
+ return 0;
}
-static psf_tag * find_tag( psf_tag * tags, const char * name )
+static psf_tag * find_tag(psf_tag * tags, const char * name)
{
- if ( tags && name && *name )
- {
- while ( tags )
- {
- if ( !strcasecmp( tags->name, name ) ) return tags;
- tags = tags->next;
- }
- }
-
- return NULL;
+ if (tags && name && *name)
+ {
+ while (tags)
+ {
+ if (!strcasecmp(tags->name, name)) return tags;
+ tags = tags->next;
+ }
+ }
+
+ return NULL;
}
-static void free_tags( psf_tag * tags )
+static void free_tags(psf_tag * tags)
{
- psf_tag * tag = tags, * next;
- while ( tag )
- {
- next = tag->next;
- if ( tag->name ) free( tag->name );
- if ( tag->value ) free( tag->value );
- free( tag );
- tag = next;
- }
+ psf_tag * tag = tags, *next;
+ while (tag)
+ {
+ next = tag->next;
+ if (tag->name) free(tag->name);
+ if (tag->value) free(tag->value);
+ free(tag);
+ tag = next;
+ }
}
-static psf_tag * add_tag_multi( psf_tag * tags, const char * name, const char ** values, int values_count )
+static psf_tag * add_tag_multi(psf_tag * tags, const char * name, const char ** values, int values_count)
{
- psf_tag * footer;
- psf_tag * tag;
-
- int i;
-
- if ( !name || !*name || !values || !values_count || !*values ) return NULL;
-
- footer = tags;
-
- tag = find_tag( tags, name );
- if ( !tag )
- {
- tag = calloc(1, sizeof(psf_tag));
- if (!tag) return footer;
- tag->name = strdup( name );
- if ( !tag->name )
- {
- free( tag );
- return footer;
- }
- tag->next = tags;
- if ( tags ) tags->prev = tag;
- footer = tag;
- }
- if ( tag->value )
- {
- size_t old_length = strlen(tag->value);
- size_t new_length = strlen( values[ 0 ] );
- char * new_value = (char *) realloc( tag->value, old_length + new_length + 2 );
- if (!new_value) return footer;
- tag->value = new_value;
- new_value[ old_length ] = '\n';
+ psf_tag * footer;
+ psf_tag * tag;
+
+ int i;
+
+ if (!name || !*name || !values || !values_count || !*values) return NULL;
+
+ footer = tags;
+
+ tag = find_tag(tags, name);
+ if (!tag)
+ {
+ tag = calloc(1, sizeof(psf_tag));
+ if (!tag) return footer;
+ tag->name = strdup(name);
+ if (!tag->name)
+ {
+ free(tag);
+ return footer;
+ }
+ tag->next = tags;
+ if (tags) tags->prev = tag;
+ footer = tag;
+ }
+ if (tag->value)
+ {
+ size_t old_length = strlen(tag->value);
+ size_t new_length = strlen(values[0]);
+ char * new_value = (char *)realloc(tag->value, old_length + new_length + 2);
+ if (!new_value) return footer;
+ tag->value = new_value;
+ new_value[old_length] = '\n';
#if _MSC_VER >= 1300
- strcpy_s( new_value + old_length + 1, new_length + 1, values[ 0 ] );
+ strcpy_s(new_value + old_length + 1, new_length + 1, values[0]);
#else
- strcpy( new_value + old_length + 1, values[ 0 ] );
+ strcpy(new_value + old_length + 1, values[0]);
#endif
- }
- else
- {
- tag->value = strdup( values[ 0 ] );
- if ( !tag->value ) return footer;
- }
-
- for (i = 1; i < values_count; i++)
- {
- tag = calloc(1, sizeof(psf_tag));
- if ( !tag ) return footer;
- tag->name = strdup( name );
- if ( !tag->name )
- {
- free( tag );
- return footer;
- }
- tag->value = strdup( values[ i ] );
- if ( !tag->value )
- {
- free( tag->name );
- free( tag );
- return footer;
- }
- tag->next = footer;
- if ( footer ) footer->prev = tag;
- footer = tag;
- }
-
- return footer;
+ }
+ else
+ {
+ tag->value = strdup(values[0]);
+ if (!tag->value) return footer;
+ }
+
+ for (i = 1; i < values_count; i++)
+ {
+ tag = calloc(1, sizeof(psf_tag));
+ if (!tag) return footer;
+ tag->name = strdup(name);
+ if (!tag->name)
+ {
+ free(tag);
+ return footer;
+ }
+ tag->value = strdup(values[i]);
+ if (!tag->value)
+ {
+ free(tag->name);
+ free(tag);
+ return footer;
+ }
+ tag->next = footer;
+ if (footer) footer->prev = tag;
+ footer = tag;
+ }
+
+ return footer;
}
-static psf_tag * add_tag( psf_tag * tags, const char * name, const char * value )
+static psf_tag * add_tag(psf_tag * tags, const char * name, const char * value)
{
- int values_count;
- const char ** values;
- char * value_split;
-
- if ( !name || !*name || !value || !*value ) return tags;
-
- if ( check_split_value( name ) )
- {
- char * split_point, * remain;
- const char ** new_values;
- values_count = 0;
- values = NULL;
- value_split = strdup( value );
- if ( !value_split ) return tags;
- remain = value_split;
- split_point = strstr( value_split, "; " );
- while ( split_point )
- {
- values_count++;
- new_values = (const char **) realloc( (void *) values, sizeof(const char*) * ((values_count + 3) & ~3) );
- if ( !new_values )
- {
- if ( values ) free( (void *) values );
- free( value_split );
- return tags;
- }
- values = new_values;
- *split_point = '\0';
- values[ values_count - 1 ] = remain;
- remain = split_point + 2;
- split_point = strstr( remain, "; " );
- }
- if ( *remain )
- {
- values_count++;
- new_values = (const char **) realloc( (void *) values, sizeof(char*) * ((values_count + 3) & ~3) );
- if ( !new_values )
- {
- if ( values ) free( (void *) values );
- free( value_split );
- return tags;
- }
- values = new_values;
- values[ values_count - 1 ] = remain;
- }
- }
- else
- {
- values_count = 1;
- value_split = NULL;
- values = (const char **) malloc(sizeof(const char *));
- if ( !values ) return tags;
- values[ 0 ] = value;
- }
-
- tags = add_tag_multi( tags, name, values, values_count );
-
- if ( value_split ) free( value_split );
- free( (void *) values );
-
- return tags;
+ int values_count;
+ const char ** values;
+ char * value_split;
+
+ if (!name || !*name || !value || !*value) return tags;
+
+ if (check_split_value(name))
+ {
+ char * split_point, *remain;
+ const char ** new_values;
+ values_count = 0;
+ values = NULL;
+ value_split = strdup(value);
+ if (!value_split) return tags;
+ remain = value_split;
+ split_point = strstr(value_split, "; ");
+ while (split_point)
+ {
+ values_count++;
+ new_values = (const char **)realloc((void *)values, sizeof(const char*) * ((values_count + 3) & ~3));
+ if (!new_values)
+ {
+ if (values) free((void *)values);
+ free(value_split);
+ return tags;
+ }
+ values = new_values;
+ *split_point = '\0';
+ values[values_count - 1] = remain;
+ remain = split_point + 2;
+ split_point = strstr(remain, "; ");
+ }
+ if (*remain)
+ {
+ values_count++;
+ new_values = (const char **)realloc((void *)values, sizeof(char*) * ((values_count + 3) & ~3));
+ if (!new_values)
+ {
+ if (values) free((void *)values);
+ free(value_split);
+ return tags;
+ }
+ values = new_values;
+ values[values_count - 1] = remain;
+ }
+ }
+ else
+ {
+ values_count = 1;
+ value_split = NULL;
+ values = (const char **)malloc(sizeof(const char *));
+ if (!values) return tags;
+ values[0] = value;
+ }
+
+ tags = add_tag_multi(tags, name, values, values_count);
+
+ if (value_split) free(value_split);
+ free((void *)values);
+
+ return tags;
}
/* Split line on first equals sign, and remove any whitespace surrounding the name and value fields */
-static psf_tag * process_tag_line( psf_tag * tags, char * line )
+static psf_tag * process_tag_line(psf_tag * tags, char * line)
{
- char * name, * value, * end;
- char * equals = strchr( line, '=' );
- if ( !equals ) return tags;
+ char * name, *value, *end;
+ char * equals = strchr(line, '=');
+ if (!equals) return tags;
- name = line;
- value = equals + 1;
- end = line + strlen( line );
+ name = line;
+ value = equals + 1;
+ end = line + strlen(line);
- while ( name < equals && *name > 0 && *name <= ' ' ) name++;
- if ( name == equals ) return tags;
+ while (name < equals && *name > 0 && *name <= ' ') name++;
+ if (name == equals) return tags;
- --equals;
- while ( equals > name && *equals > 0 && *equals <= ' ' ) --equals;
- equals[1] = '\0';
+ --equals;
+ while (equals > name && *equals > 0 && *equals <= ' ') --equals;
+ equals[1] = '\0';
- while ( value < end && *value > 0 && *value <= ' ' ) value++;
- if ( value == end ) return tags;
+ while (value < end && *value > 0 && *value <= ' ') value++;
+ if (value == end) return tags;
- --end;
- while ( end > value && *value > 0 && *value <= ' ' ) --end;
- end[1] = '\0';
+ --end;
+ while (end > value && *end > 0 && *end <= ' ') --end;
+ end[1] = '\0';
- if ( *name == '_' )
- {
- psf_tag * tag = find_tag( tags, name );
- if ( tag ) return tags;
- }
+ if (*name == '_')
+ {
+ psf_tag * tag = find_tag(tags, name);
+ if (tag) return tags;
+ }
- return add_tag( tags, name, value );
+ return add_tag(tags, name, value);
}
-static psf_tag * process_tags( char * buffer )
+static psf_tag * process_tags(char * buffer)
{
- psf_tag * tags = NULL;
- char * line_end;
- if ( !buffer || !*buffer ) return NULL;
-
- line_end = strpbrk( buffer, "\n\r" );
- while ( line_end )
- {
- *line_end++ = '\0';
- tags = process_tag_line( tags, buffer );
- while ( *line_end && ( *line_end == '\n' || *line_end == '\r' ) ) line_end++;
- buffer = line_end;
- line_end = strpbrk( buffer, "\n\r" );
- }
- if ( *buffer ) tags = process_tag_line( tags, buffer );
-
- return tags;
+ psf_tag * tags = NULL;
+ char * line_end;
+ if (!buffer || !*buffer) return NULL;
+
+ line_end = strpbrk(buffer, "\n\r");
+ while (line_end)
+ {
+ *line_end++ = '\0';
+ tags = process_tag_line(tags, buffer);
+ while (*line_end && (*line_end == '\n' || *line_end == '\r')) line_end++;
+ buffer = line_end;
+ line_end = strpbrk(buffer, "\n\r");
+ }
+ if (*buffer) tags = process_tag_line(tags, buffer);
+
+ return tags;
}
-static int psf_load_internal( psf_load_state * state, const char * file_name )
+static int psf_load_internal(psf_load_state * state, const char * file_name)
{
- psf_tag * tags = NULL;
- psf_tag * tag;
+ psf_tag * tags = NULL;
+ psf_tag * tag;
- char * full_path;
+ char * full_path;
- void * file;
+ void * file;
- long file_size, tag_size;
+ long file_size, tag_size;
- int n;
+ int n;
- uint8_t header_buffer[16];
+ uint8_t header_buffer[16];
- uint8_t * exe_compressed_buffer = NULL;
- uint8_t * exe_decompressed_buffer = NULL;
- uint8_t * reserved_buffer = NULL;
- char * tag_buffer = NULL;
+ uint8_t * exe_compressed_buffer = NULL;
+ uint8_t * exe_decompressed_buffer = NULL;
+ uint8_t * reserved_buffer = NULL;
+ char * tag_buffer = NULL;
- uint32_t exe_compressed_size, exe_crc32, reserved_size;
- uLong exe_decompressed_size, try_exe_decompressed_size;
+ uint32_t exe_compressed_size, exe_crc32, reserved_size;
+ uLong exe_decompressed_size, try_exe_decompressed_size;
- int zerr;
+ int zerr;
- size_t full_path_size;
+ size_t full_path_size;
- if ( ++state->depth > max_recursion_depth ) return -1;
+ if (++state->depth > max_recursion_depth)
+ {
+ psf_status(state, "Exceeded maximum file nesting depth.\n", 1);
+ return -1;
+ }
- full_path_size = strlen(state->base_path) + strlen(file_name) + 1;
- full_path = (char *) malloc( full_path_size );
- if ( !full_path ) return -1;
+ full_path_size = strlen(state->base_path) + strlen(file_name) + 1;
+ full_path = (char *)malloc(full_path_size);
+ if (!full_path) return -1;
#if _MSC_VER >= 1300
- strcpy_s( full_path, full_path_size, state->base_path );
- strcat_s( full_path, full_path_size, file_name );
+ strcpy_s(full_path, full_path_size, state->base_path);
+ strcat_s(full_path, full_path_size, file_name);
#else
- strcpy( full_path, state->base_path );
- strcat( full_path, file_name );
+ strcpy(full_path, state->base_path);
+ strcat(full_path, file_name);
#endif
- file = state->file_callbacks->fopen( full_path );
-
- free( full_path );
-
- if ( !file ) return -1;
-
- if ( state->file_callbacks->fread( header_buffer, 1, 16, file ) < 16 ) goto error_close_file;
-
- if ( memcmp( header_buffer, "PSF", 3 ) ) goto error_close_file;
-
- if ( state->allowed_version && ( header_buffer[ 3 ] != state->allowed_version ) ) goto error_close_file;
-
- reserved_size = header_buffer[ 4 ] | ( header_buffer[ 5 ] << 8 ) | ( header_buffer[ 6 ] << 16 ) | ( header_buffer[ 7 ] << 24 );
- exe_compressed_size = header_buffer[ 8 ] | ( header_buffer[ 9 ] << 8 ) | ( header_buffer[ 10 ] << 16 ) | ( header_buffer[ 11 ] << 24 );
- exe_crc32 = header_buffer[ 12 ] | ( header_buffer[ 13 ] << 8 ) | ( header_buffer[ 14 ] << 16 ) | ( header_buffer[ 15 ] << 24 );
-
- if ( state->file_callbacks->fseek( file, 0, SEEK_END ) ) goto error_close_file;
-
- file_size = state->file_callbacks->ftell( file );
-
- if ( file_size <= 0 ) goto error_close_file;
-
- if ( (unsigned long)file_size >= 16 + reserved_size + exe_compressed_size + 5 )
- {
- tag_size = file_size - ( 16 + reserved_size + exe_compressed_size );
- if ( state->file_callbacks->fseek( file, -tag_size, SEEK_CUR ) ) goto error_close_file;
- tag_buffer = (char *) malloc( tag_size + 1 );
- if ( !tag_buffer ) goto error_close_file;
- if ( state->file_callbacks->fread( tag_buffer, 1, tag_size, file ) < (size_t)tag_size ) goto error_free_buffers;
- tag_buffer[ tag_size ] = 0;
- if ( !memcmp( tag_buffer, "[TAG]", 5 ) ) tags = process_tags( tag_buffer + 5 );
- free( tag_buffer );
- tag_buffer = NULL;
-
- if ( tags && state->info_target && ( state->depth == 1 || state->info_want_nested_tags ) )
- {
- tag = tags;
- while ( tag->next ) tag = tag->next;
- while ( tag )
- {
- state->info_target( state->info_context, tag->name, tag->value );
- tag = tag->prev;
- }
- }
- }
-
- if ( !state->load_target ) goto done;
-
- tag = find_tag( tags, "_lib" );
- if ( tag )
- {
- if ( psf_load_internal( state, tag->value ) < 0 ) goto error_free_tags;
- }
-
- reserved_buffer = (uint8_t *) malloc( reserved_size );
- if ( !reserved_buffer ) goto error_free_tags;
- exe_compressed_buffer = (uint8_t *) malloc( exe_compressed_size );
- if ( !exe_compressed_buffer ) goto error_free_tags;
-
- if ( state->file_callbacks->fseek( file, 16, SEEK_SET ) ) goto error_free_tags;
- if ( reserved_size && state->file_callbacks->fread( reserved_buffer, 1, reserved_size, file ) < reserved_size ) goto error_free_tags;
- if ( exe_compressed_size && state->file_callbacks->fread( exe_compressed_buffer, 1, exe_compressed_size, file ) < exe_compressed_size ) goto error_free_tags;
- state->file_callbacks->fclose( file );
- file = NULL;
-
- if ( exe_compressed_size )
- {
- if ( exe_crc32 != crc32(crc32(0L, Z_NULL, 0), exe_compressed_buffer, exe_compressed_size) ) goto error_free_tags;
-
- exe_decompressed_size = try_exe_decompressed_size = exe_compressed_size * 3;
- exe_decompressed_buffer = (uint8_t *) malloc( exe_decompressed_size );
- if ( !exe_decompressed_buffer ) goto error_free_tags;
-
- while ( Z_OK != ( zerr = uncompress( exe_decompressed_buffer, &exe_decompressed_size, exe_compressed_buffer, exe_compressed_size ) ) )
- {
- void * try_exe_decompressed_buffer;
-
- if ( Z_MEM_ERROR != zerr && Z_BUF_ERROR != zerr ) goto error_free_tags;
-
- if ( try_exe_decompressed_size < 1 * 1024 * 1024 )
- try_exe_decompressed_size += 1 * 1024 * 1024;
- else
- try_exe_decompressed_size += try_exe_decompressed_size;
-
- exe_decompressed_size = try_exe_decompressed_size;
-
- try_exe_decompressed_buffer = realloc( exe_decompressed_buffer, exe_decompressed_size );
- if ( !try_exe_decompressed_buffer ) goto error_free_tags;
-
- exe_decompressed_buffer = (uint8_t *) try_exe_decompressed_buffer;
- }
- }
- else
- {
- exe_decompressed_size = 0;
- exe_decompressed_buffer = (uint8_t *) malloc( exe_decompressed_size );
- if ( !exe_decompressed_buffer ) goto error_free_tags;
- }
-
- free( exe_compressed_buffer );
- exe_compressed_buffer = NULL;
-
- if ( state->load_target( state->load_context, exe_decompressed_buffer, exe_decompressed_size, reserved_buffer, reserved_size ) ) goto error_free_tags;
-
- free( reserved_buffer );
- reserved_buffer = NULL;
-
- free( exe_decompressed_buffer );
- exe_decompressed_buffer = NULL;
-
- n = 2;
- snprintf( state->lib_name_temp, 31, "_lib%u", n );
- state->lib_name_temp[ 31 ] = '\0';
- tag = find_tag( tags, state->lib_name_temp );
- while ( tag )
- {
- if ( psf_load_internal( state, tag->value ) < 0 ) goto error_free_tags;
- ++n;
- snprintf( state->lib_name_temp, 31, "_lib%u", n );
- state->lib_name_temp[ 31 ] = '\0';
- tag = find_tag( tags, state->lib_name_temp );
- }
+ file = state->file_callbacks->fopen(full_path);
+
+ free(full_path);
+
+ if (!file)
+ {
+ if (psf_want_status(state))
+ {
+ psf_status(state, "Error opening file: ", 1);
+ psf_status(state, file_name, 0);
+ psf_status(state, "\n", 0);
+ psf_status(state, "From base path: ", 1);
+ psf_status(state, state->base_path, 0);
+ psf_status(state, "\n", 0);
+ }
+ return -1;
+ }
+
+ if (psf_want_status(state))
+ {
+ psf_status(state, "Opened file: ", 1);
+ psf_status(state, file_name, 0);
+ psf_status(state, "\n", 0);
+ psf_status(state, "From base path: ", 1);
+ psf_status(state, state->base_path, 0);
+ psf_status(state, "\n", 0);
+ }
+
+ if (state->file_callbacks->fread(header_buffer, 1, 16, file) < 16)
+ {
+ psf_status(state, "File too small to contain a valid header.\n", 1);
+ goto error_close_file;
+ }
+
+ if (memcmp(header_buffer, "PSF", 3))
+ {
+ psf_status(state, "File does not contain a valid PSF signature.\n", 1);
+ goto error_close_file;
+ }
+
+ if (state->allowed_version && (header_buffer[3] != state->allowed_version))
+ {
+ if (psf_want_status(state))
+ {
+ char *end;
+ char temp[8];
+ psf_status(state, "Expected PSF version ", 1);
+ snprintf(temp, 7, "%d", (int)state->allowed_version);
+ temp[7] = '\0';
+ psf_status(state, temp, 0);
+ psf_status(state, ", got ", 0);
+ snprintf(temp, 7, "%d", (int)header_buffer[3]);
+ temp[7] = '\0';
+ psf_status(state, temp, 0);
+ psf_status(state, "\n", 0);
+ }
+ goto error_close_file;
+ }
+
+ reserved_size = header_buffer[4] | (header_buffer[5] << 8) | (header_buffer[6] << 16) | (header_buffer[7] << 24);
+ exe_compressed_size = header_buffer[8] | (header_buffer[9] << 8) | (header_buffer[10] << 16) | (header_buffer[11] << 24);
+ exe_crc32 = header_buffer[12] | (header_buffer[13] << 8) | (header_buffer[14] << 16) | (header_buffer[15] << 24);
+
+ if (state->file_callbacks->fseek(file, 0, SEEK_END))
+ {
+ psf_status(state, "Could not seek to end of file to determine file size.\n", 1);
+ goto error_close_file;
+ }
+
+ file_size = state->file_callbacks->ftell(file);
+
+ if (file_size <= 0)
+ {
+ psf_status(state, "Could not determine file size.\n", 1);
+ goto error_close_file;
+ }
+
+ if ((unsigned long)file_size >= 16 + reserved_size + exe_compressed_size + 5)
+ {
+ psf_status(state, "Tag detected, attempting to read it.\n", 1);
+
+ tag_size = file_size - (16 + reserved_size + exe_compressed_size);
+ if (state->file_callbacks->fseek(file, -tag_size, SEEK_CUR))
+ {
+ psf_status(state, "Could not seek back to read tag.\n", 1);
+ goto error_close_file;
+ }
+ tag_buffer = (char *)malloc(tag_size + 1);
+ if (!tag_buffer)
+ {
+ psf_status(state, "Out of memory allocating tag buffer.\n", 1);
+ goto error_close_file;
+ }
+ if (state->file_callbacks->fread(tag_buffer, 1, tag_size, file) < (size_t)tag_size)
+ {
+ psf_status(state, "Could not read tag.\n", 1);
+ goto error_free_buffers;
+ }
+ tag_buffer[tag_size] = 0;
+ if (!memcmp(tag_buffer, "[TAG]", 5)) tags = process_tags(tag_buffer + 5);
+ free(tag_buffer);
+ tag_buffer = NULL;
+
+ if (tags && state->info_target && (state->depth == 1 || state->info_want_nested_tags))
+ {
+ tag = tags;
+ while (tag->next) tag = tag->next;
+ while (tag)
+ {
+ if (state->info_target(state->info_context, tag->name, tag->value))
+ {
+ if (psf_want_status(state))
+ {
+ psf_status(state, "Caller rejected tag: ", 1);
+ psf_status(state, tag->name, 0);
+ psf_status(state, "=", 0);
+ psf_status(state, tag->value, 0);
+ psf_status(state, "\n", 0);
+ }
+ goto error_free_tags;
+ }
+ tag = tag->prev;
+ }
+ }
+ }
+
+ if (!state->load_target) goto done;
+
+ tag = find_tag(tags, "_lib");
+ if (tag)
+ {
+ if (psf_want_status(state))
+ {
+ psf_status(state, "Found _lib: ", 1);
+ psf_status(state, tag->value, 0);
+ psf_status(state, "\n", 0);
+ }
+ if (psf_load_internal(state, tag->value) < 0) goto error_free_tags;
+ }
+
+ reserved_buffer = (uint8_t *)malloc(reserved_size);
+ if (!reserved_buffer)
+ {
+ psf_status(state, "Out of memory allocating buffer for reserved section.\n", 1);
+ goto error_free_tags;
+ }
+ exe_compressed_buffer = (uint8_t *)malloc(exe_compressed_size);
+ if (!exe_compressed_buffer)
+ {
+ psf_status(state, "Out of memory allocating buffer for compressed exe section.\n", 1);
+ goto error_free_tags;
+ }
+
+ if (state->file_callbacks->fseek(file, 16, SEEK_SET))
+ {
+ psf_status(state, "Could not seek back to main data section of file.", 1);
+ goto error_free_tags;
+ }
+ if (reserved_size && state->file_callbacks->fread(reserved_buffer, 1, reserved_size, file) < reserved_size)
+ {
+ psf_status(state, "Could not read reserved section.\n", 1);
+ goto error_free_tags;
+ }
+ if (exe_compressed_size && state->file_callbacks->fread(exe_compressed_buffer, 1, exe_compressed_size, file) < exe_compressed_size)
+ {
+ psf_status(state, "Could not read compressed exe section.\n", 1);
+ goto error_free_tags;
+ }
+ state->file_callbacks->fclose(file);
+ file = NULL;
+
+ psf_status(state, "File closed.\n", 1);
+
+ if (exe_compressed_size)
+ {
+ uint32_t got_crc32 = crc32(crc32(0L, Z_NULL, 0), exe_compressed_buffer, exe_compressed_size);
+ if (exe_crc32 != got_crc32)
+ {
+ if (psf_want_status(state))
+ {
+ char temp[16];
+ psf_status(state, "CRC mismatch on compressed exe section.\nWanted: 0x", 1);
+ snprintf(temp, 15, "%X", exe_crc32);
+ temp[15] = '\0';
+ psf_status(state, temp, 0);
+ psf_status(state, ", got 0x", 0);
+ snprintf(temp, 15, "%X", got_crc32);
+ temp[15] = '\0';
+ psf_status(state, temp, 0);
+ psf_status(state, "\n", 0);
+ }
+ goto error_free_tags;
+ }
+
+ exe_decompressed_size = try_exe_decompressed_size = exe_compressed_size * 3;
+ exe_decompressed_buffer = (uint8_t *)malloc(exe_decompressed_size);
+ if (!exe_decompressed_buffer)
+ {
+ psf_status(state, "Out of memory allocating buffer for decompressed exe section.\n", 1);
+ goto error_free_tags;
+ }
+
+ while (Z_OK != (zerr = uncompress(exe_decompressed_buffer, &exe_decompressed_size, exe_compressed_buffer, exe_compressed_size)))
+ {
+ void * try_exe_decompressed_buffer;
+
+ if (Z_MEM_ERROR != zerr && Z_BUF_ERROR != zerr)
+ {
+ psf_status(state, "Could not decompress exe section.\n", 1);
+ goto error_free_tags;
+ }
+
+ if (try_exe_decompressed_size < 1 * 1024 * 1024)
+ try_exe_decompressed_size += 1 * 1024 * 1024;
+ else
+ try_exe_decompressed_size += try_exe_decompressed_size;
+
+ exe_decompressed_size = try_exe_decompressed_size;
+
+ try_exe_decompressed_buffer = realloc(exe_decompressed_buffer, exe_decompressed_size);
+ if (!try_exe_decompressed_buffer)
+ {
+ psf_status(state, "Out of memory reallocating buffer for decompressed exe section.\n", 1);
+ goto error_free_tags;
+ }
+
+ exe_decompressed_buffer = (uint8_t *)try_exe_decompressed_buffer;
+ }
+ }
+ else
+ {
+ exe_decompressed_size = 0;
+ exe_decompressed_buffer = (uint8_t *)malloc(exe_decompressed_size);
+ if (!exe_decompressed_buffer)
+ {
+ psf_status(state, "Out of memory allocating dummy buffer for exe section.\n", 1);
+ goto error_free_tags;
+ }
+ }
+
+ free(exe_compressed_buffer);
+ exe_compressed_buffer = NULL;
+
+ psf_status(state, "Passing exe and reserved back out.\n", 1);
+
+ if (state->load_target(state->load_context, exe_decompressed_buffer, exe_decompressed_size, reserved_buffer, reserved_size))
+ {
+ psf_status(state, "Data handler returned an error.\n", 1);
+ goto error_free_tags;
+ }
+
+ free(reserved_buffer);
+ reserved_buffer = NULL;
+
+ free(exe_decompressed_buffer);
+ exe_decompressed_buffer = NULL;
+
+ n = 2;
+ snprintf(state->lib_name_temp, 31, "_lib%u", n);
+ state->lib_name_temp[31] = '\0';
+ tag = find_tag(tags, state->lib_name_temp);
+ while (tag)
+ {
+ if (psf_want_status(state))
+ {
+ psf_status(state, "Found ", 1);
+ psf_status(state, tag->name, 0);
+ psf_status(state, ": ", 0);
+ psf_status(state, tag->value, 0);
+ psf_status(state, "\n", 0);
+ }
+ if (psf_load_internal(state, tag->value) < 0) goto error_free_tags;
+ ++n;
+ snprintf(state->lib_name_temp, 31, "_lib%u", n);
+ state->lib_name_temp[31] = '\0';
+ tag = find_tag(tags, state->lib_name_temp);
+ }
done:
- if ( file ) state->file_callbacks->fclose( file );
+ if (file) state->file_callbacks->fclose(file);
- free_tags( tags );
+ free_tags(tags);
- --state->depth;
+ --state->depth;
- return header_buffer[ 3 ];
+ return header_buffer[3];
error_free_tags:
- free_tags( tags );
+ free_tags(tags);
error_free_buffers:
- if ( exe_compressed_buffer ) free( exe_compressed_buffer );
- if ( exe_decompressed_buffer ) free( exe_decompressed_buffer );
- if ( reserved_buffer ) free( reserved_buffer );
- if ( tag_buffer ) free( tag_buffer );
+ if (exe_compressed_buffer) free(exe_compressed_buffer);
+ if (exe_decompressed_buffer) free(exe_decompressed_buffer);
+ if (reserved_buffer) free(reserved_buffer);
+ if (tag_buffer) free(tag_buffer);
error_close_file:
- if ( file ) state->file_callbacks->fclose( file );
- return -1;
+ if (file) state->file_callbacks->fclose(file);
+ return -1;
+}
+
+int psf_want_status(psf_load_state * state)
+{
+ return !!state->status_target;
+}
+
+void psf_status(psf_load_state * state, const char * message, int indent)
+{
+ if (state->status_target)
+ {
+ if (indent)
+ {
+ char indent[16];
+ int indent_level = state->depth > 1 ? state->depth - 1 : 0;
+ memset(indent, ' ', indent_level);
+ indent[indent_level] = '\0';
+ state->status_target(state->status_context, indent);
+ }
+ state->status_target(state->status_context, message);
+ }
}
diff --git a/lib/psflib/psflib.h b/lib/psflib/psflib.h
index 58e4def..107471c 100644
--- a/lib/psflib/psflib.h
+++ b/lib/psflib/psflib.h
@@ -1,3 +1,27 @@
+/*
+PSFLIB - Main PSF parser implementation
+
+Copyright (c) 2012-2015 Christopher Snowhill
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
#ifndef PSFLIB_H
#define PSFLIB_H
@@ -14,11 +38,12 @@ typedef struct psf_file_callbacks
/* list of characters which act as path separators, null terminated */
const char * path_separators;
- /* accepts UTF-8 encoding, returns file handle */
- void * (* fopen )(const char *);
+ /* opens the file pointed to by path read-only in binary mode,
+ * accepts UTF-8 encoding, returns file handle */
+ void * (* fopen )(const char * path);
/* reads to specified buffer, returns count of size bytes read */
- size_t (* fread )(void *, size_t size, size_t count, void * handle);
+ size_t (* fread )(void * buffer, size_t size, size_t count, void * handle);
/* returns zero on success, -1 on error */
int (* fseek )(void * handle, int64_t, int);
@@ -50,6 +75,9 @@ typedef int (* psf_load_callback)(void * context, const uint8_t * exe, size_t ex
*/
typedef int (* psf_info_callback)(void * context, const char * name, const char * value);
+/* Receives any status messages, which should be appended to one big message. */
+typedef void (* psf_status_callback)(void * context, const char * message);
+
/* Loads the PSF chain starting with uri, opened using file_callbacks, passes the tags,
* if any, to the optional info_target callback, then passes all loaded data to load_target
* with the highest priority file first.
@@ -62,7 +90,9 @@ typedef int (* psf_info_callback)(void * context, const char * name, const char
* Returns negative on error, PSF version on success.
*/
int psf_load( const char * uri, const psf_file_callbacks * file_callbacks, uint8_t allowed_version,
- psf_load_callback load_target, void * load_context, psf_info_callback info_target, void * info_context, int info_want_nested_tags );
+ psf_load_callback load_target, void * load_context, psf_info_callback info_target,
+ void * info_context, int info_want_nested_tags, psf_status_callback status_target,
+ void * status_context);
#ifdef __cplusplus
}
diff --git a/src/CircularBuffer.h b/src/CircularBuffer.h
new file mode 100644
index 0000000..ee8377a
--- /dev/null
+++ b/src/CircularBuffer.h
@@ -0,0 +1,95 @@
+/*
+ * Copyright (C) 2019 Team Kodi
+ *
+ * This Program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This Program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with XBMC; see the file COPYING. If not, see
+ * .
+ *
+ */
+
+#pragma once
+
+#include
+#include
+
+int const silence_threshold = 8;
+
+template
+class circular_buffer
+{
+public:
+ circular_buffer( unsigned p_size ) : readptr( 0 ), writeptr( 0 ), size( p_size ), used( 0 )
+ {
+ buffer.resize( p_size );
+ }
+ unsigned data_available() { return used; }
+ unsigned free_space() { return size - used; }
+ bool write( const T * src, unsigned count )
+ {
+ if ( count > free_space() ) return false;
+ while( count )
+ {
+ unsigned delta = size - writeptr;
+ if ( delta > count ) delta = count;
+ std::copy( src, src + delta, buffer.begin() + writeptr );
+ used += delta;
+ writeptr = ( writeptr + delta ) % size;
+ src += delta;
+ count -= delta;
+ }
+ return true;
+ }
+ unsigned read( T * dst, unsigned count )
+ {
+ unsigned done = 0;
+ for(;;)
+ {
+ unsigned delta = size - readptr;
+ if ( delta > used ) delta = used;
+ if ( delta > count ) delta = count;
+ if ( !delta ) break;
+
+ std::copy( buffer.begin() + readptr, buffer.begin() + readptr + delta, dst );
+ dst += delta;
+ done += delta;
+ readptr = ( readptr + delta ) % size;
+ count -= delta;
+ used -= delta;
+ }
+ return done;
+ }
+ void reset()
+ {
+ readptr = writeptr = used = 0;
+ }
+ void resize(unsigned p_size)
+ {
+ size = p_size;
+ buffer.resize( p_size );
+ reset();
+ }
+ bool test_silence() const
+ {
+ T* begin = (T*) &buffer[0];
+ T first = *begin;
+ *begin = silence_threshold * 2;
+ T* p = begin + size;
+ while ( (unsigned) ( *--p + silence_threshold ) <= (unsigned) silence_threshold * 2 ) { }
+ *begin = first;
+ return p == begin && ( (unsigned) ( first + silence_threshold ) <= (unsigned) silence_threshold * 2 );
+ }
+
+private:
+ std::vector buffer;
+ unsigned readptr, writeptr, used, size;
+};
diff --git a/src/SSFCodec.cpp b/src/SSFCodec.cpp
index 9cb6375..6e4cd98 100644
--- a/src/SSFCodec.cpp
+++ b/src/SSFCodec.cpp
@@ -17,56 +17,38 @@
*
*/
-#include
-#include
-#include "sega.h"
-#include "dcsound.h"
-#include "satsound.h"
-#include "yam.h"
-#include "psflib.h"
-
-struct sdsf_load_state
-{
- std::vector state;
-};
+// Code based upon https://bitbucket.org/losnoco/foo_input_ht
-struct SSFContext
-{
- sdsf_load_state state;
- int64_t len;
- int sample_rate;
- int64_t pos;
- std::string title;
- std::string artist;
- std::vector sega_state;
- int version;
-};
+#include "SSFCodec.h"
+
+bool CSSFCodec::m_gInitialized = false;
+std::mutex CSSFCodec::m_gSyncMutex;
extern "C"
{
-inline unsigned get_le32( void const* p )
+inline unsigned get_le32(void const* p)
{
- return (unsigned) ((unsigned char const*) p) [3] << 24 |
- (unsigned) ((unsigned char const*) p) [2] << 16 |
- (unsigned) ((unsigned char const*) p) [1] << 8 |
- (unsigned) ((unsigned char const*) p) [0];
+ return (unsigned) ((unsigned char const*) p) [3] << 24 |
+ (unsigned) ((unsigned char const*) p) [2] << 16 |
+ (unsigned) ((unsigned char const*) p) [1] << 8 |
+ (unsigned) ((unsigned char const*) p) [0];
}
-static int sdsf_load(void * context, const uint8_t * exe, size_t exe_size,
- const uint8_t * reserved, size_t reserved_size)
+static int sdsf_load(void* context, const uint8_t* exe, size_t exe_size,
+ const uint8_t* reserved, size_t reserved_size)
{
- if ( exe_size < 4 )
+ if (exe_size < 4)
return -1;
- sdsf_load_state * state = ( sdsf_load_state * ) context;
+ sdsf_load_state* state = static_cast(context);
- std::vector & dst = state->state;
+ std::vector& dst = state->state;
- if ( dst.size() < 4 )
+ if (dst.size() < 4)
{
- dst.resize( exe_size );
- memcpy( &dst[0], exe, exe_size );
+ dst.resize(exe_size);
+ memcpy(&dst[0], exe, exe_size);
return 0;
}
@@ -76,33 +58,35 @@ static int sdsf_load(void * context, const uint8_t * exe, size_t exe_size,
src_start &= 0x7FFFFF;
size_t dst_len = dst.size() - 4;
size_t src_len = exe_size - 4;
- if ( dst_len > 0x800000 ) dst_len = 0x800000;
- if ( src_len > 0x800000 ) src_len = 0x800000;
+ if (dst_len > 0x800000)
+ dst_len = 0x800000;
+ if (src_len > 0x800000)
+ src_len = 0x800000;
- if ( src_start < dst_start )
+ if (src_start < dst_start)
{
size_t diff = dst_start - src_start;
- dst.resize( dst_len + 4 + diff );
- memmove( &dst[0] + 4 + diff, &dst[0] + 4, dst_len );
- memset( &dst[0] + 4, 0, diff );
+ dst.resize(dst_len + 4 + diff);
+ memmove(&dst[0] + 4 + diff, &dst[0] + 4, dst_len);
+ memset(&dst[0] + 4, 0, diff );
dst_len += diff;
dst_start = src_start;
*(uint32_t*)(&dst[0]) = get_le32(&dst_start);
}
- if ( ( src_start + src_len ) > ( dst_start + dst_len ) )
+ if ((src_start + src_len) > (dst_start + dst_len))
{
- size_t diff = ( src_start + src_len ) - ( dst_start + dst_len );
- dst.resize( dst_len + 4 + diff );
- memset( &dst[0] + 4 + dst_len, 0, diff );
+ size_t diff = (src_start + src_len) - (dst_start + dst_len);
+ dst.resize(dst_len + 4 + diff);
+ memset(&dst[0] + 4 + dst_len, 0, diff);
dst_len += diff;
}
- memcpy( &dst[0] + 4 + ( src_start - dst_start ), exe + 4, src_len );
+ memcpy(&dst[0] + 4 + (src_start - dst_start), exe + 4, src_len);
return 0;
}
-static void * psf_file_fopen( const char * uri )
+static void* psf_file_fopen(const char* uri)
{
kodi::vfs::CFile* file = new kodi::vfs::CFile;
if (!file->OpenFile(uri, 0))
@@ -114,26 +98,26 @@ static void * psf_file_fopen( const char * uri )
return file;
}
-static size_t psf_file_fread( void * buffer, size_t size, size_t count, void * handle )
+static size_t psf_file_fread(void* buffer, size_t size, size_t count, void* handle)
{
kodi::vfs::CFile* file = static_cast(handle);
return file->Read(buffer, size*count);
}
-static int psf_file_fseek( void * handle, int64_t offset, int whence )
+static int psf_file_fseek(void* handle, int64_t offset, int whence)
{
kodi::vfs::CFile* file = static_cast(handle);
return file->Seek(offset, whence) > -1 ? 0 : -1;
}
-static int psf_file_fclose( void * handle )
+static int psf_file_fclose(void* handle )
{
delete static_cast(handle);
return 0;
}
-static long psf_file_ftell( void * handle )
+static long psf_file_ftell(void* handle)
{
kodi::vfs::CFile* file = static_cast(handle);
return file->GetPosition();
@@ -150,262 +134,564 @@ const psf_file_callbacks psf_file_system =
};
#define BORK_TIME 0xC0CAC01A
-static unsigned long parse_time_crap(const char *input)
+
+static unsigned long parse_time_crap(const char* input)
{
- if (!input) return BORK_TIME;
- int len = strlen(input);
- if (!len) return BORK_TIME;
- int value = 0;
+ unsigned long value = 0;
+ unsigned long multiplier = 1000;
+ const char* ptr = input;
+ unsigned long colon_count = 0;
+
+ while (*ptr && ((*ptr >= '0' && *ptr <= '9') || *ptr == ':'))
{
- int i;
- for (i = len - 1; i >= 0; i--)
+ colon_count += *ptr == ':';
+ ++ptr;
+ }
+ if (colon_count > 2)
+ return BORK_TIME;
+ if (*ptr && *ptr != '.' && *ptr != ',')
+ return BORK_TIME;
+ if (*ptr)
+ ++ptr;
+ while (*ptr && *ptr >= '0' && *ptr <= '9')
+ ++ptr;
+ if (*ptr)
+ return BORK_TIME;
+
+ ptr = strrchr(input, ':');
+ if (!ptr)
+ ptr = input;
+ for (;;)
+ {
+ char* end;
+ if (ptr != input)
+ ++ptr;
+ if (multiplier == 1000)
{
- if ((input[i] < '0' || input[i] > '9') && input[i] != ':' && input[i] != ',' && input[i] != '.')
- {
+ double temp = std::stod(ptr);
+ if (temp >= 60.0)
return BORK_TIME;
- }
+ value = (long)(temp * 1000.0f);
+ }
+ else
+ {
+ unsigned long temp = strtoul(ptr, &end, 10);
+ if (temp >= 60 && multiplier < 3600000)
+ return BORK_TIME;
+ value += temp * multiplier;
}
+ if (ptr == input)
+ break;
+ ptr -= 2;
+ while (ptr > input && *ptr != ':')
+ --ptr;
+ multiplier *= 60;
}
- std::string foo = input;
- char *bar = (char *) &foo[0];
- char *strs = bar + foo.size() - 1;
- while (strs > bar && (*strs >= '0' && *strs <= '9'))
+
+ return value;
+}
+
+static int psf_info_meta(void* context, const char* name, const char* value)
+{
+ psf_info_meta_state* state = static_cast(context);
+
+ if (!strcasecmp(name, "artist") && state->artist.empty())
{
- strs--;
+ state->artist = value;
}
- if (*strs == '.' || *strs == ',')
+ else if (!strcasecmp(name, "game"))
{
- // fraction of a second
- strs++;
- if (strlen(strs) > 3) strs[3] = 0;
- value = atoi(strs);
- switch (strlen(strs))
- {
- case 1:
- value *= 100;
- break;
- case 2:
- value *= 10;
- break;
- }
- strs--;
- *strs = 0;
- strs--;
+ state->artist = value;
+ }
+ else if (!strcasecmp(name, "title"))
+ {
+ state->title = value;
}
- while (strs > bar && (*strs >= '0' && *strs <= '9'))
+ else if (!strcasecmp(name, "year"))
{
- strs--;
+ state->year = value;
}
- // seconds
- if (*strs < '0' || *strs > '9') strs++;
- value += atoi(strs) * 1000;
- if (strs > bar)
+ else if (!strcasecmp(name, "replaygain_"))
{
- strs--;
- *strs = 0;
- strs--;
- while (strs > bar && (*strs >= '0' && *strs <= '9'))
+ state->replaygain = value;
+ }
+ else if (!strcasecmp(name, "length"))
+ {
+ int temp = parse_time_crap(value);
+ if (temp != BORK_TIME)
{
- strs--;
+ state->tagSongMs = temp;
}
- if (*strs < '0' || *strs > '9') strs++;
- value += atoi(strs) * 60000;
- if (strs > bar)
+ }
+ else if (!strcasecmp(name, "fade"))
+ {
+ int temp = parse_time_crap(value);
+ if (temp != BORK_TIME)
{
- strs--;
- *strs = 0;
- strs--;
- while (strs > bar && (*strs >= '0' && *strs <= '9'))
- {
- strs--;
- }
- value += atoi(strs) * 3600000;
+ state->tagFadeMs = temp;
}
}
- return value;
+ else if (!strcasecmp(name, "utf8"))
+ {
+ state->utf8 = true;
+ }
+ else if (!strcasecmp(name, "_lib"))
+ {
+ // Unused, checked to prevent on next error
+ }
+ else if (name[0] == '_')
+ {
+ kodi::Log(ADDON_LOG_WARNING, "Unsupported tag found: '%s', required to play file", name);
+ return -1;
+ }
+
+ return 0;
}
-static int psf_info_meta(void* context,
- const char* name, const char* value)
+} // extern "C"
+
+//------------------------------------------------------------------------------
+
+CSSFCodec::CSSFCodec(KODI_HANDLE instance) : CInstanceAudioDecoder(instance)
{
- SSFContext* ssf = (SSFContext*)context;
- if (!strcasecmp(name, "length"))
- ssf->len = parse_time_crap(value);
- if (!strcasecmp(name, "title"))
- ssf->title = value;
- if (!strcasecmp(name, "artist"))
- ssf->artist = value;
- return 0;
}
+CSSFCodec::~CSSFCodec()
+{
+ if (m_segaState.empty())
+ return;
+
+ void* yam = nullptr;
+ if (m_xsfVersion == 0x12)
+ {
+ void* dcsound = sega_get_dcsound_state(m_segaState.data());
+ yam = dcsound_get_yam_state(dcsound);
+ }
+ else
+ {
+ void* satsound = sega_get_satsound_state(m_segaState.data());
+ yam = satsound_get_yam_state(satsound);
+ }
+ if (yam)
+ yam_unprepare_dynacode(yam);
}
-class ATTRIBUTE_HIDDEN CSSFCodec : public kodi::addon::CInstanceAudioDecoder
+bool CSSFCodec::Init(const std::string& filename, unsigned int filecache,
+ int& channels, int& samplerate,
+ int& bitspersample, int64_t& totaltime,
+ int& bitrate, AEDataFormat& format,
+ std::vector& channellist)
{
-public:
- CSSFCodec(KODI_HANDLE instance) :
- CInstanceAudioDecoder(instance) {}
+ m_path = filename;
+ m_xsfVersion = psf_load(m_path.c_str(), &psf_file_system, 0,
+ nullptr, nullptr,
+ nullptr, nullptr, 0,
+ SSFPrintMessage, this);
+ if (m_xsfVersion <= 0 || (m_xsfVersion != 0x11 && m_xsfVersion != 0x12))
+ {
+ kodi::Log(ADDON_LOG_ERROR, "%s: Not a SSF or PSF file '%s'", __func__, m_path.c_str());
+ return false;
+ }
+
+ psf_info_meta_state info_state;
+ int ret = psf_load(m_path.c_str(), &psf_file_system, m_xsfVersion,
+ nullptr, nullptr,
+ psf_info_meta, &info_state, 0,
+ SSFPrintMessage, this);
+ if (ret <= 0)
+ {
+ kodi::Log(ADDON_LOG_ERROR, "%s: Failed to load tags from '%s'", __func__, m_path.c_str());
+ return false;
+ }
+
+ kodi::CheckSettingBoolean("suppressopeningsilence", m_cfgSuppressOpeningSilence);
+ kodi::CheckSettingBoolean("suppressendsilence", m_cfgSuppressEndSilence);
+ kodi::CheckSettingInt("endsilenceseconds", m_cfgEndSilenceSeconds);
+ kodi::CheckSettingBoolean("dry", m_cfgDry);
+ kodi::CheckSettingBoolean("dsp", m_cfgDSP);
+ kodi::CheckSettingBoolean("dspdynamicrec", m_cfgDSPDynamicRec);
+
+ m_tagSongMs = info_state.tagSongMs;
+ m_tagFadeMs = info_state.tagFadeMs;
+
+ if (!m_tagSongMs)
+ {
+ m_tagSongMs = kodi::GetSettingInt("defaultlength") * 1000;
+ m_tagFadeMs = kodi::GetSettingInt("defaultfade");
+ }
- virtual ~CSSFCodec()
+ if (!Load())
+ return false;
+
+ totaltime = m_songLength / m_cfgDefaultSampleRate * 1000 + m_tagFadeMs;
+ format = AE_FMT_S16NE;
+ channellist = { AE_CH_FL, AE_CH_FR };
+ channels = 2;
+ bitspersample = 16;
+ bitrate = 0.0;
+ samplerate = m_cfgDefaultSampleRate;
+
+ return true;
+}
+
+bool CSSFCodec::Load()
+{
{
- if (ctx.sega_state.empty())
- return;
+ std::lock_guard lock(m_gSyncMutex);
+ if (!m_gInitialized)
+ {
+ if (sega_init())
+ {
+ kodi::Log(ADDON_LOG_ERROR, "%s: Sega emulator static initialization failed", __func__);
+ return false;
+ }
+ m_gInitialized = true;
+ }
+ }
- void * yam = 0;
- if (ctx.version == 0x12)
+ if (!m_segaState.empty())
+ {
+ void* yam = nullptr;
+ if (m_xsfVersion == 0x12)
{
- void * dcsound = sega_get_dcsound_state(&ctx.sega_state[0]);
- yam = dcsound_get_yam_state( dcsound );
+ void* dcsound = sega_get_dcsound_state(m_segaState.data());
+ yam = dcsound_get_yam_state(dcsound);
}
else
{
- void * satsound = sega_get_satsound_state(&ctx.sega_state[0]);
- yam = satsound_get_yam_state( satsound );
+ void* satsound = sega_get_satsound_state(m_segaState.data());
+ yam = satsound_get_yam_state(satsound);
}
if (yam)
yam_unprepare_dynacode(yam);
}
- virtual bool Init(const std::string& filename, unsigned int filecache,
- int& channels, int& samplerate,
- int& bitspersample, int64_t& totaltime,
- int& bitrate, AEDataFormat& format,
- std::vector& channellist) override
- {
- ctx.pos = 0;
- if ((ctx.version=psf_load(filename.c_str(), &psf_file_system, 0, 0, 0, 0, 0, 0)) <= 0 ||
- !(ctx.version == 0x11 || ctx.version == 0x12))
- return false;
+ m_segaState.resize(sega_get_state_size(m_xsfVersion - 0x10));
- if (psf_load(filename.c_str(), &psf_file_system, ctx.version,
- 0, 0, psf_info_meta, &ctx, 0) <= 0)
- return false;
+ void* pEmu = m_segaState.data();
- if (psf_load(filename.c_str(), &psf_file_system, ctx.version,
- sdsf_load, &ctx.state, 0, 0, 0) < 0)
- return false;
+ sega_clear_state(pEmu, m_xsfVersion - 0x10);
- sega_init();
- ctx.sega_state.resize(sega_get_state_size(ctx.version-0x10));
- void* emu = &ctx.sega_state[0];
- sega_clear_state(emu, ctx.version-0x10);
- sega_enable_dry(emu, 0);
- sega_enable_dsp(emu, 1);
- sega_enable_dsp_dynarec(emu, 1);
+ sega_enable_dry(pEmu, m_cfgDry ? 1 : !m_cfgDSP);
+ sega_enable_dsp(pEmu, m_cfgDSP);
+ sega_enable_dsp_dynarec(pEmu, m_cfgDSPDynamicRec);
- void * yam = 0;
- if (ctx.version == 0x12)
+ if (m_cfgDSPDynamicRec)
+ {
+ void* yam = 0;
+ if (m_xsfVersion == 0x12)
{
- void * dcsound = sega_get_dcsound_state(emu);
- yam = dcsound_get_yam_state( dcsound );
+ void* dcsound = sega_get_dcsound_state(pEmu);
+ yam = dcsound_get_yam_state(dcsound);
}
else
{
- void * satsound = sega_get_satsound_state(emu);
- yam = satsound_get_yam_state( satsound );
+ void * satsound = sega_get_satsound_state(pEmu);
+ yam = satsound_get_yam_state(satsound);
}
if (yam)
yam_prepare_dynacode(yam);
+ }
+
+ sdsf_load_state state;
+ int ret = psf_load(m_path.c_str(), &psf_file_system, m_xsfVersion,
+ sdsf_load, &state,
+ nullptr, nullptr, 0,
+ SSFPrintMessage, this);
+ if (ret < 0)
+ {
+ kodi::Log(ADDON_LOG_ERROR, "%s: Invalid SSF/DSF from '%s'", __func__, m_path.c_str());
+ return false;
+ }
+
+ uint32_t start = get_le32(state.state.data());
+ size_t length = state.state.size();
+ size_t max_length = (m_xsfVersion == 0x12) ? 0x800000 : 0x80000;
+ if ((start + (length-4)) > max_length)
+ {
+ length = max_length - start + 4;
+ }
+ sega_upload_program(pEmu, state.state.data(), length);
- uint32_t start = get_le32(&ctx.state.state[0]);
- size_t length = ctx.state.state.size();
- size_t max_length = ( ctx.version == 0x12 ) ? 0x800000 : 0x80000;
- if ((start + (length-4)) > max_length)
+ m_xsfEmuPosition = 0.;
+
+ m_startSilence = 0;
+ m_silence = 0;
+
+ m_eof = false;
+ m_dataWritten = 0;
+ m_remainder = 0;
+ m_posDelta = 0;
+ m_xsfEmuPosition = 0;
+ m_noLoop = true;
+
+ calcfade();
+
+ unsigned int skip_max = m_cfgEndSilenceSeconds * m_cfgDefaultSampleRate;
+
+ if (m_cfgSuppressOpeningSilence) // ohcrap
+ {
+ for (;;)
{
- length = max_length - start + 4;
+ unsigned int skip_howmany = skip_max - m_silence;
+ if (skip_howmany > 8192)
+ skip_howmany = 8192;
+ m_sampleBuffer.resize(skip_howmany * 2);
+ int rtn = sega_execute(pEmu, 0x7FFFFFFF, m_sampleBuffer.data(), & skip_howmany);
+ if (rtn < 0)
+ {
+ kodi::Log(ADDON_LOG_ERROR, "%s: Failed to call 'sega_execute'", __func__);
+ return false;
+ }
+ int16_t* foo = m_sampleBuffer.data();
+ unsigned int i;
+ for (i = 0; i < skip_howmany; ++i)
+ {
+ if (foo[0] || foo[1])
+ break;
+ foo += 2;
+ }
+ m_silence += i;
+ if (i < skip_howmany)
+ {
+ m_remainder = skip_howmany - i;
+ memmove(m_sampleBuffer.data(), foo, m_remainder * sizeof(int16_t) * 2);
+ break;
+ }
+ if (m_silence >= skip_max)
+ {
+ m_eof = true;
+ break;
+ }
}
- sega_upload_program(emu, &ctx.state.state[0], length );
- totaltime = ctx.len;
- format = AE_FMT_S16NE;
- channellist = { AE_CH_FL, AE_CH_FR };
- channels = 2;
- bitspersample = 16;
- bitrate = 0.0;
- samplerate = ctx.sample_rate = 44100;
- ctx.len = ctx.sample_rate*4*totaltime/1000;
+ m_startSilence += m_silence;
+ m_silence = 0;
+ }
+
+ if (m_cfgSuppressEndSilence)
+ m_silenceTestBuffer.resize(skip_max * 2);
+
+ return true;
+}
+
+int CSSFCodec::ReadPCM(uint8_t* buffer, int size, int& actualsize)
+{
+ if (m_eof && !m_silenceTestBuffer.data_available())
+ return 1;
+
+ if (m_noLoop && m_tagSongMs && (m_posDelta + mul_div(m_dataWritten, 1000, m_cfgDefaultSampleRate)) >= m_tagSongMs + m_tagFadeMs )
+ return -1;
+
+ unsigned int written = 0;
+
+ int usedSize = size / 2 / sizeof(int16_t);
+
+ int samples;
- return true;
+ if (m_noLoop)
+ {
+ samples = (m_songLength + m_fadeLength) - m_dataWritten;
+ if (samples > usedSize)
+ samples = usedSize;
+ }
+ else
+ {
+ samples = usedSize;
}
- virtual int ReadPCM(uint8_t* buffer, int size, int& actualsize) override
+ if (m_cfgSuppressEndSilence)
{
- if (ctx.pos >= ctx.len)
- return 1;
+ m_sampleBuffer.resize(usedSize * 2);
- actualsize = size/4;
- int err = sega_execute(&ctx.sega_state[0], 0x7FFFFFFF,
- (int16_t*)buffer, (unsigned int*)&actualsize);
- if (err < 0)
- return 1;
- actualsize *= 4;
- ctx.pos += actualsize;
- return 0;
- }
+ if (!m_eof)
+ {
+ unsigned int free_space = m_silenceTestBuffer.free_space() / 2;
+ while (free_space)
+ {
+ unsigned int samples_to_render;
+ if (m_remainder)
+ {
+ samples_to_render = m_remainder;
+ m_remainder = 0;
+ }
+ else
+ {
+ samples_to_render = free_space;
+ if (samples_to_render > usedSize)
+ samples_to_render = usedSize;
+ int err = sega_execute(m_segaState.data(), 0x7FFFFFFF, m_sampleBuffer.data(), &samples_to_render);
+ if (err < 0 || !samples_to_render)
+ {
+ kodi::Log(ADDON_LOG_ERROR, "%s: Execution halted with an error", __func__);
+ return 1;
+ }
+ }
+ m_silenceTestBuffer.write(m_sampleBuffer.data(), samples_to_render * 2);
+ free_space -= samples_to_render;
+ }
+ }
+
+ if (m_silenceTestBuffer.test_silence())
+ {
+ m_eof = true;
+ return -1;
+ }
- virtual int64_t Seek(int64_t time) override
+ written = m_silenceTestBuffer.data_available() / 2;
+ if (written > samples)
+ written = samples;
+ m_silenceTestBuffer.read(m_sampleBuffer.data(), written * 2);
+ }
+ else
{
- if (time*ctx.sample_rate*4/1000 < ctx.pos)
+ m_sampleBuffer.resize(samples * 2);
+
+ if (m_remainder)
+ {
+ written = m_remainder;
+ m_remainder = 0;
+ }
+ else
{
- void* emu = &ctx.sega_state[0];
- uint32_t start = get_le32((uint32_t*)(&ctx.state.state[0]));
- size_t length = ctx.state.state.size();
- size_t max_length = ( ctx.version == 0x12 ) ? 0x800000 : 0x80000;
- if ((start + (length-4)) > max_length)
+ written = samples;
+ int err = sega_execute(m_segaState.data(), 0x7FFFFFFF, m_sampleBuffer.data(), & written );
+ if (err < 0 || !written)
{
- length = max_length - start + 4;
+ kodi::Log(ADDON_LOG_ERROR, "%s: Execution halted with an error", __func__);
+ return 1;
}
- sega_upload_program(emu, &ctx.state.state[0], length );
- ctx.pos = 0;
}
+ }
- int64_t left = time*ctx.sample_rate*4/1000-ctx.pos;
- while (left > 1024)
+ m_xsfEmuPosition += double(written) / double(m_cfgDefaultSampleRate);
+
+ int d_start, d_end;
+ d_start = m_dataWritten;
+ m_dataWritten += written;
+ d_end = m_dataWritten;
+
+ if (m_tagSongMs && d_end > m_songLength && m_noLoop)
+ {
+ int16_t* foo = m_sampleBuffer.data();
+ for (int n = d_start; n < d_end; ++n)
{
- unsigned int chunk=1024;
- int rtn = sega_execute(&ctx.sega_state[0], 0x7FFFFFFF, 0, &chunk);
- ctx.pos += chunk*2;
- left -= chunk*2;
+ if (n > m_songLength)
+ {
+ if (n > m_songLength + m_fadeLength)
+ {
+ *(uint32_t*)foo = 0;
+ }
+ else
+ {
+ int bleh = m_songLength + m_fadeLength - n;
+ foo[0] = mul_div(foo[0], bleh, m_fadeLength);
+ foo[1] = mul_div(foo[1], bleh, m_fadeLength);
+ }
+ }
+ foo += 2;
}
+ }
+
+ if (!written)
+ {
+ m_eof = true;
+ return -1;
+ }
+
+ actualsize = written * 2 * sizeof(int16_t);
+ memcpy(buffer, m_sampleBuffer.data(), actualsize);
+
+ return 0;
+}
+
+int64_t CSSFCodec::Seek(int64_t time)
+{
+ double p_seconds = double(time) / 1000.0;
+ m_eof = false;
+
+ double buffered_time = (double)(m_silenceTestBuffer.data_available() / 2) / double(m_cfgDefaultSampleRate);
+
+ m_xsfEmuPosition += buffered_time;
- return ctx.pos/(ctx.sample_rate*4)*1000;
+ m_silenceTestBuffer.reset();
+
+ if (p_seconds < m_xsfEmuPosition)
+ {
+ Load();
}
+ unsigned int howmany = (int)(time_to_samples(p_seconds - m_xsfEmuPosition, m_cfgDefaultSampleRate));
- virtual bool ReadTag(const std::string& file, std::string& title,
- std::string& artist, int& length) override
+ // more abortable, and emu doesn't like doing huge numbers of samples per call anyway
+ void* pEmu = m_segaState.data();
+ while (howmany)
{
- SSFContext ssf;
+ unsigned todo = howmany;
+ if (todo > 2048)
+ todo = 2048;
+ int rtn = sega_execute(pEmu, 0x7FFFFFFF, 0, &todo);
+ if (rtn < 0 || ! todo)
+ {
+ m_eof = true;
+ return -1;
+ }
+ howmany -= todo;
+ }
- if (psf_load(file.c_str(), &psf_file_system, 0x11, 0, 0, psf_info_meta, &ssf, 0) <= 0 &&
- psf_load(file.c_str(), &psf_file_system, 0x12, 0, 0, psf_info_meta, &ssf, 0) <= 0)
- return false;
+ m_dataWritten = 0;
+ m_posDelta = (int)(p_seconds * 1000.);
+ m_xsfEmuPosition = p_seconds;
- title = ssf.title;
- artist = ssf.artist;
- length = ssf.len/1000;
+ calcfade();
- return true;
+ return time;
+}
+
+bool CSSFCodec::ReadTag(const std::string& file, std::string& title,
+ std::string& artist, int& length)
+{
+ int xsfVersion = psf_load(file.c_str(), &psf_file_system, 0,
+ nullptr, nullptr,
+ nullptr, nullptr, 0,
+ SSFPrintMessage, this);
+ if (xsfVersion <= 0 || (xsfVersion != 0x11 && xsfVersion != 0x12))
+ {
+ kodi::Log(ADDON_LOG_ERROR, "%s: Not a SSF or PSF file '%s'", __func__, m_path.c_str());
+ return false;
}
-private:
- SSFContext ctx;
-};
+ psf_info_meta_state info_state;
+ if (psf_load(file.c_str(), &psf_file_system, xsfVersion, nullptr, nullptr, psf_info_meta, &info_state, 0, SSFPrintMessage, this) <= 0)
+ {
+ kodi::Log(ADDON_LOG_ERROR, "%s: Failed to load %s information from '%s'", __func__, xsfVersion == 0x11 ? "SSF" : "DSF", file.c_str());
+ return false;
+ }
+
+ title = info_state.title;
+ artist = info_state.artist;
+ length = (info_state.tagSongMs+info_state.tagFadeMs)/1000;
+
+ return true;
+}
+
+void CSSFCodec::SSFPrintMessage(void* context, const char* message)
+{
+ kodi::Log(ADDON_LOG_DEBUG, "NCFS codec message: '%s'", message);
+}
+//------------------------------------------------------------------------------
class ATTRIBUTE_HIDDEN CMyAddon : public kodi::addon::CAddonBase
{
public:
- CMyAddon() { }
- virtual ADDON_STATUS CreateInstance(int instanceType, std::string instanceID, KODI_HANDLE instance, KODI_HANDLE& addonInstance) override
+ CMyAddon() = default;
+ ADDON_STATUS CreateInstance(int instanceType, std::string instanceID, KODI_HANDLE instance, KODI_HANDLE& addonInstance) override
{
addonInstance = new CSSFCodec(instance);
return ADDON_STATUS_OK;
}
- virtual ~CMyAddon()
- {
- }
+ virtual ~CMyAddon() = default;
};
-
-ADDONCREATOR(CMyAddon);
+ADDONCREATOR(CMyAddon)
diff --git a/src/SSFCodec.h b/src/SSFCodec.h
new file mode 100644
index 0000000..3bb2bba
--- /dev/null
+++ b/src/SSFCodec.h
@@ -0,0 +1,133 @@
+/*
+ * Copyright (C) 2019 Team Kodi
+ * Copyright (C) 2014 Arne Morten Kvarving
+ *
+ * This Program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This Program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with XBMC; see the file COPYING. If not, see
+ * .
+ *
+ */
+
+#pragma once
+
+#include "CircularBuffer.h"
+
+#include
+#include
+
+#include
+#include
+#include
+#include
+
+#include "sega.h"
+#include "dcsound.h"
+#include "satsound.h"
+#include "yam.h"
+#include "psflib.h"
+
+struct sdsf_load_state
+{
+ std::vector state;
+};
+
+struct psf_info_meta_state
+{
+ std::string title;
+ std::string artist;
+ std::string year;
+ std::string replaygain;
+
+ bool utf8 = false;
+
+ int tagSongMs = 0;
+ int tagFadeMs = 0;
+};
+
+class ATTRIBUTE_HIDDEN CSSFCodec : public kodi::addon::CInstanceAudioDecoder
+{
+public:
+ CSSFCodec(KODI_HANDLE instance);
+ ~CSSFCodec() override;
+
+ bool Init(const std::string& filename, unsigned int filecache,
+ int& channels, int& samplerate,
+ int& bitspersample, int64_t& totaltime,
+ int& bitrate, AEDataFormat& format,
+ std::vector& channellist) override;
+ int ReadPCM(uint8_t* buffer, int size, int& actualsize) override;
+ int64_t Seek(int64_t time) override;
+ bool ReadTag(const std::string& file, std::string& title,
+ std::string& artist, int& length) override;
+
+private:
+ static void SSFPrintMessage(void* context, const char* message);
+
+ static bool m_gInitialized;
+ static std::mutex m_gSyncMutex;
+
+ bool Load();
+
+ inline uint64_t time_to_samples(double p_time, uint32_t p_sample_rate)
+ {
+ return (uint64_t)floor((double)p_sample_rate * p_time + 0.5);
+ }
+
+ inline void calcfade()
+ {
+ m_songLength = mul_div(m_tagSongMs-m_posDelta,44100,1000);
+ m_fadeLength = mul_div(m_tagFadeMs,44100,1000);
+ }
+
+ inline int mul_div(int number, int numerator, int denominator)
+ {
+ long long ret = number;
+ ret *= numerator;
+ ret /= denominator;
+ return (int) ret;
+ }
+
+ int m_cfgDefaultSampleRate = 44100;
+ bool m_cfgSuppressOpeningSilence = true;
+ bool m_cfgSuppressEndSilence = true;
+ int m_cfgEndSilenceSeconds = 5;
+ bool m_cfgDry = true;
+ bool m_cfgDSP = true;
+ bool m_cfgDSPDynamicRec = true;
+
+ bool m_noLoop = true;
+ bool m_eof;
+
+ std::vector m_segaState;
+ std::vector m_sampleBuffer;
+
+ circular_buffer m_silenceTestBuffer = 0;
+
+ std::string m_path;
+
+ int m_xsfVersion;
+
+ int m_dataWritten;
+ int m_remainder;
+ int m_posDelta;
+ int m_startSilence;
+ int m_silence;
+
+ double m_xsfEmuPosition;
+
+ int m_songLength;
+ int m_fadeLength;
+ int m_tagSongMs;
+ int m_tagFadeMs;
+};
+