Ver Fonte

Merge pull request #691 from fodinabor/goodbye_cmake

goodbye_cmake improvements.
Ivan Safrin há 9 anos atrás
pai
commit
712dcf0ec4
73 ficheiros alterados com 12048 adições e 6101 exclusões
  1. BIN
      assets/icons/sdl_icon.bmp
  2. 1 5
      build/android/Polycode2DPhysics/AndroidManifest.xml
  3. 1 1
      build/android/Polycode2DPhysics/build.gradle
  4. 1 1
      build/android/Polycode2DPhysics/jni/Application.mk
  5. 1 1
      build/android/Polycode3DPhysics/build.gradle
  6. 1 1
      build/android/Polycode3DPhysics/jni/Application.mk
  7. 1 1
      build/android/PolycodeUI/build.gradle
  8. 1 1
      build/android/PolycodeUI/jni/Application.mk
  9. 1 1
      build/android/Polycore/build.gradle
  10. 0 1
      build/android/Polycore/jni/Android.mk
  11. 1 1
      build/android/Polycore/jni/Application.mk
  12. 1 1
      build/android/TemplateApp/build.gradle
  13. 1 12
      build/android/TemplateApp/jni/Android.mk
  14. 1 1
      build/android/TemplateApp/jni/Application.mk
  15. 1 1
      build/android/build.gradle
  16. 8 16
      build/ios/PolycodeCore/PolycodeCore.xcodeproj/project.pbxproj
  17. 0 12
      build/ios/TemplateApp/TemplateApp.xcodeproj/project.pbxproj
  18. 4 3
      build/linux/Makefile
  19. 4 0
      build/osx/PolycodeCore/PolycodeCore.xcodeproj/project.pbxproj
  20. 0 6
      build/osx/PolycodeStudio/PolycodeStudio.xcodeproj/project.pbxproj
  21. 0 6
      build/osx/TemplateApp/TemplateApp.xcodeproj/project.pbxproj
  22. 1 0
      build/windows/win32/PolycodeCore/PolycodeCore.vcxproj
  23. 4 1
      build/windows/win32/PolycodeCore/PolycodeCore.vcxproj.filters
  24. 4 4
      build/windows/win32/PolycodeStudio/PolycodeStudio.vcxproj
  25. 4 4
      build/windows/win32/TemplateApp/TemplateApp.vcxproj
  26. 1 1
      include/Polycode.h
  27. 0 2666
      include/libpng15/png.h
  28. 0 596
      include/libpng15/pngconf.h
  29. 0 186
      include/libpng15/pnglibconf.h
  30. 0 25
      include/ogg/config_types.h
  31. 0 209
      include/ogg/ogg.h
  32. 0 147
      include/ogg/os_types.h
  33. 7 3
      include/polycode/core/PolyAndroidCore.h
  34. 1 1
      include/polycode/core/PolyCocoaCore.h
  35. 4 2
      include/polycode/core/PolyCoreInput.h
  36. 3 1
      include/polycode/core/PolyImage.h
  37. 8 9
      include/polycode/core/PolyInputEvent.h
  38. 1 1
      include/polycode/core/PolyInputKeys.h
  39. 17 5
      include/polycode/core/PolySDLCore.h
  40. 57 44
      include/polycode/core/PolyString.h
  41. 3 2
      include/polycode/core/PolyWinCore.h
  42. 4 1
      include/polycode/ide/PolycodeTextEditor.h
  43. 4 2
      include/polycode/modules/ui/PolyUITextInput.h
  44. 1 1
      include/polycode/modules/ui/PolyUITreeContainer.h
  45. 1 1
      include/polycode/modules/ui/PolyUIWindow.h
  46. 3 0
      include/polycode/view/android/PolycodeView.h
  47. 5267 0
      include/stb_vorbis.h
  48. 0 243
      include/vorbis/codec.h
  49. 0 436
      include/vorbis/vorbisenc.h
  50. 0 206
      include/vorbis/vorbisfile.h
  51. 1 1
      lib
  52. 2 2
      src/core/PolyAAssetFileProvider.cpp
  53. 126 9
      src/core/PolyAndroidCore.cpp
  54. 5 2
      src/core/PolyCocoaCore.mm
  55. 11 8
      src/core/PolyCoreInput.cpp
  56. 7 1
      src/core/PolyImage.cpp
  57. 1 13
      src/core/PolyInputEvent.cpp
  58. 10 4
      src/core/PolyOpenGLGraphicsInterface.cpp
  59. 1 1
      src/core/PolyOpenSLAudioInterface.cpp
  60. 282 510
      src/core/PolySDLCore.cpp
  61. 542 582
      src/core/PolySound.cpp
  62. 11 6
      src/core/PolyString.cpp
  63. 14 8
      src/core/PolyTexture.cpp
  64. 27 7
      src/core/PolyWinCore.cpp
  65. 5269 0
      src/core/stb_vorbis.cpp
  66. 174 4
      src/ide/PolycodeTextEditor.cpp
  67. 7 5
      src/ide/PolycodeWinIDEView.cpp
  68. 39 24
      src/modules/ui/PolyUITextInput.cpp
  69. 2 2
      src/modules/ui/PolyUITreeContainer.cpp
  70. 2 2
      src/modules/ui/PolyUIWindow.cpp
  71. 63 7
      src/view/android/PolycodeView.cpp
  72. 13 21
      src/view/osx/PolycodeView.mm
  73. 15 13
      src/view/win32/PolycodeView.cpp

BIN
assets/icons/sdl_icon.bmp


+ 1 - 5
build/android/Polycode2DPhysics/AndroidManifest.xml

@@ -1,11 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!-- BEGIN_INCLUDE(manifest) -->
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-        package="com.example.native_activity"
-        android:versionCode="1"
-        android:versionName="1.0">
-
-    <uses-sdk android:minSdkVersion="21" />
+        package="com.example.native_activity">
 
     <!-- This .apk has no Java code itself, so set hasCode to false. -->
     <application android:label="Polycore" android:hasCode="false">

+ 1 - 1
build/android/Polycode2DPhysics/build.gradle

@@ -8,7 +8,7 @@ android {
 
     defaultConfig {
     applicationId "org.polycode.polycode2dphysics"
-        minSdkVersion 21
+        minSdkVersion 19
         targetSdkVersion 23
         versionCode 1
         versionName "1.0"

+ 1 - 1
build/android/Polycode2DPhysics/jni/Application.mk

@@ -1,4 +1,4 @@
-APP_PLATFORM := android-21
+APP_PLATFORM := android-19
 APP_ABI := armeabi x86
 APP_STL := gnustl_static
 APP_CPPFLAGS += -std=c++11

+ 1 - 1
build/android/Polycode3DPhysics/build.gradle

@@ -8,7 +8,7 @@ android {
 
     defaultConfig {
     applicationId "org.polycode.polycode3dphysics"
-        minSdkVersion 21
+        minSdkVersion 19
         targetSdkVersion 23
         versionCode 1
         versionName "1.0"

+ 1 - 1
build/android/Polycode3DPhysics/jni/Application.mk

@@ -1,4 +1,4 @@
-APP_PLATFORM := android-21
+APP_PLATFORM := android-19
 APP_ABI := armeabi x86
 APP_STL := gnustl_static
 APP_CPPFLAGS += -std=c++11

+ 1 - 1
build/android/PolycodeUI/build.gradle

@@ -8,7 +8,7 @@ android {
 
     defaultConfig {
     applicationId "org.polycode.polycodeui"
-        minSdkVersion 21
+        minSdkVersion 19
         targetSdkVersion 23
         versionCode 1
         versionName "1.0"

+ 1 - 1
build/android/PolycodeUI/jni/Application.mk

@@ -1,4 +1,4 @@
-APP_PLATFORM := android-21
+APP_PLATFORM := android-19
 APP_ABI := armeabi x86
 APP_STL := gnustl_static
 APP_CPPFLAGS += -std=c++11

+ 1 - 1
build/android/Polycore/build.gradle

@@ -6,7 +6,7 @@ android {
     buildToolsVersion "23.0.2"
     defaultConfig {
         applicationId "org.polycode.polycore"
-        minSdkVersion 21
+        minSdkVersion 19
         targetSdkVersion 23
         versionCode 1
         versionName "1.0"

Diff do ficheiro suprimidas por serem muito extensas
+ 0 - 1
build/android/Polycore/jni/Android.mk


+ 1 - 1
build/android/Polycore/jni/Application.mk

@@ -1,4 +1,4 @@
-APP_PLATFORM := android-21
+APP_PLATFORM := android-19
 APP_ABI := armeabi x86
 APP_STL := gnustl_static
 APP_CPPFLAGS += -std=c++11

+ 1 - 1
build/android/TemplateApp/build.gradle

@@ -7,7 +7,7 @@ android {
     buildToolsVersion '23.0.2'
     defaultConfig {
         applicationId "org.polycode.templateapp"
-        minSdkVersion 21
+        minSdkVersion 19
         targetSdkVersion 23
         versionCode 1
         versionName "1.0"

+ 1 - 12
build/android/TemplateApp/jni/Android.mk

@@ -62,21 +62,10 @@ LOCAL_MODULE := box2d
 LOCAL_SRC_FILES := $(LIBDIR)/libbox2d.a
 include $(PREBUILT_STATIC_LIBRARY)
 
-include $(CLEAR_VARS)
-LOCAL_MODULE := ogg
-LOCAL_SRC_FILES := $(LIBDIR)/libogg.so
-include $(PREBUILT_SHARED_LIBRARY)
-
-include $(CLEAR_VARS)
-LOCAL_MODULE := vorbis
-LOCAL_SRC_FILES := $(LIBDIR)/libvorbis.so
-include $(PREBUILT_SHARED_LIBRARY)
-
 include $(CLEAR_VARS)
 LOCAL_MODULE := TemplateApp
 LOCAL_LDLIBS := -landroid -lEGL -lGLESv2 -lOpenSLES -lz -llog
 LOCAL_STATIC_LIBRARIES := Polycore PolycodeUI Polycode3DPhysics freetype lua physfs box2d BulletDynamics BulletCollision BulletSoftBody LinearMath
-LOCAL_SHARED_LIBRARIES := ogg vorbis
-LOCAL_CFLAGS += -I$(LOCAL_PATH)/../../../../include -DUSE_EGL -DSTRICT_OPENGLES2
+LOCAL_CFLAGS += -I$(LOCAL_PATH)/../../../../include -DUSE_EGL -DSTRICT_OPENGLES2 -DNO_FP16
 LOCAL_SRC_FILES := PolycodeTemplate.cpp PolycodeTemplateApp.cpp
 include $(BUILD_SHARED_LIBRARY)

+ 1 - 1
build/android/TemplateApp/jni/Application.mk

@@ -1,4 +1,4 @@
-APP_PLATFORM := android-21
+APP_PLATFORM := android-19
 APP_ABI := armeabi x86
 APP_STL := gnustl_static
 APP_CPPFLAGS += -std=c++11

+ 1 - 1
build/android/build.gradle

@@ -5,7 +5,7 @@ buildscript {
         jcenter()
     }
     dependencies {
-        classpath 'com.android.tools.build:gradle:2.1.0'
+        classpath 'com.android.tools.build:gradle:2.1.2'
 
         // NOTE: Do not place your application dependencies here; they belong
         // in the individual module build.gradle files

+ 8 - 16
build/ios/PolycodeCore/PolycodeCore.xcodeproj/project.pbxproj

@@ -8,7 +8,6 @@
 
 /* Begin PBXBuildFile section */
 		6D904FDD1CCBF95700D0E80A /* PolyScript.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6D904FDC1CCBF95700D0E80A /* PolyScript.cpp */; };
-		6D904FE01CCBF9F900D0E80A /* duktape.c in Sources */ = {isa = PBXBuildFile; fileRef = 6D904FDF1CCBF9F900D0E80A /* duktape.c */; };
 		6D9D8C221CD975B1001E636B /* PolycodeLua.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6D9D8C211CD975B1001E636B /* PolycodeLua.cpp */; };
 		8A0F81471BF541F900E24F9B /* lodepng.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F80F91BF541F800E24F9B /* lodepng.cpp */; };
 		8A0F81481BF541F900E24F9B /* PolyBasicFileProvider.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F80FA1BF541F800E24F9B /* PolyBasicFileProvider.cpp */; };
@@ -29,7 +28,6 @@
 		8A0F81571BF541F900E24F9B /* PolyEventDispatcher.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F81091BF541F800E24F9B /* PolyEventDispatcher.cpp */; };
 		8A0F81581BF541F900E24F9B /* PolyEventHandler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F810A1BF541F800E24F9B /* PolyEventHandler.cpp */; };
 		8A0F81591BF541F900E24F9B /* PolyFont.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F810B1BF541F800E24F9B /* PolyFont.cpp */; };
-		8A0F815A1BF541F900E24F9B /* PolyFontGlyphSheet.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F810C1BF541F800E24F9B /* PolyFontGlyphSheet.cpp */; };
 		8A0F815C1BF541F900E24F9B /* PolyGPUDrawBuffer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F810E1BF541F800E24F9B /* PolyGPUDrawBuffer.cpp */; };
 		8A0F815D1BF541F900E24F9B /* PolyImage.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F810F1BF541F800E24F9B /* PolyImage.cpp */; };
 		8A0F815E1BF541F900E24F9B /* PolyInputEvent.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F81101BF541F800E24F9B /* PolyInputEvent.cpp */; };
@@ -37,7 +35,6 @@
 		8A0F81601BF541F900E24F9B /* PolyLabel.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F81121BF541F800E24F9B /* PolyLabel.cpp */; };
 		8A0F81611BF541F900E24F9B /* PolyLogger.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F81131BF541F800E24F9B /* PolyLogger.cpp */; };
 		8A0F81621BF541F900E24F9B /* PolyMaterial.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F81141BF541F800E24F9B /* PolyMaterial.cpp */; };
-		8A0F81631BF541F900E24F9B /* PolyMaterialManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F81151BF541F800E24F9B /* PolyMaterialManager.cpp */; };
 		8A0F81641BF541F900E24F9B /* PolyMatrix4.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F81161BF541F800E24F9B /* PolyMatrix4.cpp */; };
 		8A0F81651BF541F900E24F9B /* PolyMesh.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F81171BF541F800E24F9B /* PolyMesh.cpp */; };
 		8A0F81661BF541F900E24F9B /* PolyObject.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F81181BF541F800E24F9B /* PolyObject.cpp */; };
@@ -55,7 +52,6 @@
 		8A0F81731BF541F900E24F9B /* PolyResourceManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F81251BF541F800E24F9B /* PolyResourceManager.cpp */; };
 		8A0F81741BF541F900E24F9B /* PolyScene.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F81261BF541F800E24F9B /* PolyScene.cpp */; };
 		8A0F81751BF541F900E24F9B /* PolySceneEntityInstance.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F81271BF541F800E24F9B /* PolySceneEntityInstance.cpp */; };
-		8A0F81761BF541F900E24F9B /* PolySceneImage.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F81281BF541F800E24F9B /* PolySceneImage.cpp */; };
 		8A0F81771BF541F900E24F9B /* PolySceneLabel.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F81291BF541F800E24F9B /* PolySceneLabel.cpp */; };
 		8A0F81781BF541F900E24F9B /* PolySceneLight.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F812A1BF541F800E24F9B /* PolySceneLight.cpp */; };
 		8A0F81791BF541F900E24F9B /* PolySceneLine.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F812B1BF541F800E24F9B /* PolySceneLine.cpp */; };
@@ -87,6 +83,8 @@
 		8A0F81931BF541F900E24F9B /* tinyxmlerror.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F81451BF541F900E24F9B /* tinyxmlerror.cpp */; };
 		8A0F81941BF541F900E24F9B /* tinyxmlparser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A0F81461BF541F900E24F9B /* tinyxmlparser.cpp */; };
 		8A35E1A11C87B25900BF9EEA /* PolyPhysFSFileProvider.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A35E1A01C87B25900BF9EEA /* PolyPhysFSFileProvider.cpp */; };
+		AA2456D01D24434B00E34036 /* duktape.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AA2456CF1D24434B00E34036 /* duktape.cpp */; };
+		AA2456D21D24437400E34036 /* stb_vorbis.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AA2456D11D24437400E34036 /* stb_vorbis.cpp */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXCopyFilesBuildPhase section */
@@ -104,7 +102,6 @@
 /* Begin PBXFileReference section */
 		6D904FDC1CCBF95700D0E80A /* PolyScript.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolyScript.cpp; path = ../../../src/core/PolyScript.cpp; sourceTree = "<group>"; };
 		6D904FDE1CCBF96D00D0E80A /* PolyScript.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PolyScript.h; path = ../../../include/polycode/core/PolyScript.h; sourceTree = "<group>"; };
-		6D904FDF1CCBF9F900D0E80A /* duktape.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = duktape.c; path = ../../../src/duktape/duktape.c; sourceTree = "<group>"; };
 		6D9D8C1F1CD9759E001E636B /* PolycodeLua.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PolycodeLua.h; path = ../../../include/polycode/bindings/lua/PolycodeLua.h; sourceTree = "<group>"; };
 		6D9D8C201CD9759E001E636B /* PolycodeLuaWrappers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PolycodeLuaWrappers.h; path = ../../../include/polycode/bindings/lua/PolycodeLuaWrappers.h; sourceTree = "<group>"; };
 		6D9D8C211CD975B1001E636B /* PolycodeLua.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.objcpp.preprocessed; fileEncoding = 4; name = PolycodeLua.cpp; path = ../../../src/bindings/lua/PolycodeLua.cpp; sourceTree = "<group>"; };
@@ -201,7 +198,6 @@
 		8A0F81091BF541F800E24F9B /* PolyEventDispatcher.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolyEventDispatcher.cpp; path = ../../../src/core/PolyEventDispatcher.cpp; sourceTree = "<group>"; };
 		8A0F810A1BF541F800E24F9B /* PolyEventHandler.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolyEventHandler.cpp; path = ../../../src/core/PolyEventHandler.cpp; sourceTree = "<group>"; };
 		8A0F810B1BF541F800E24F9B /* PolyFont.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolyFont.cpp; path = ../../../src/core/PolyFont.cpp; sourceTree = "<group>"; };
-		8A0F810C1BF541F800E24F9B /* PolyFontGlyphSheet.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolyFontGlyphSheet.cpp; path = ../../../src/core/PolyFontGlyphSheet.cpp; sourceTree = "<group>"; };
 		8A0F810E1BF541F800E24F9B /* PolyGPUDrawBuffer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolyGPUDrawBuffer.cpp; path = ../../../src/core/PolyGPUDrawBuffer.cpp; sourceTree = "<group>"; };
 		8A0F810F1BF541F800E24F9B /* PolyImage.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolyImage.cpp; path = ../../../src/core/PolyImage.cpp; sourceTree = "<group>"; };
 		8A0F81101BF541F800E24F9B /* PolyInputEvent.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolyInputEvent.cpp; path = ../../../src/core/PolyInputEvent.cpp; sourceTree = "<group>"; };
@@ -209,7 +205,6 @@
 		8A0F81121BF541F800E24F9B /* PolyLabel.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolyLabel.cpp; path = ../../../src/core/PolyLabel.cpp; sourceTree = "<group>"; };
 		8A0F81131BF541F800E24F9B /* PolyLogger.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolyLogger.cpp; path = ../../../src/core/PolyLogger.cpp; sourceTree = "<group>"; };
 		8A0F81141BF541F800E24F9B /* PolyMaterial.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolyMaterial.cpp; path = ../../../src/core/PolyMaterial.cpp; sourceTree = "<group>"; };
-		8A0F81151BF541F800E24F9B /* PolyMaterialManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolyMaterialManager.cpp; path = ../../../src/core/PolyMaterialManager.cpp; sourceTree = "<group>"; };
 		8A0F81161BF541F800E24F9B /* PolyMatrix4.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolyMatrix4.cpp; path = ../../../src/core/PolyMatrix4.cpp; sourceTree = "<group>"; };
 		8A0F81171BF541F800E24F9B /* PolyMesh.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolyMesh.cpp; path = ../../../src/core/PolyMesh.cpp; sourceTree = "<group>"; };
 		8A0F81181BF541F800E24F9B /* PolyObject.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolyObject.cpp; path = ../../../src/core/PolyObject.cpp; sourceTree = "<group>"; };
@@ -227,7 +222,6 @@
 		8A0F81251BF541F800E24F9B /* PolyResourceManager.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.objcpp; fileEncoding = 4; name = PolyResourceManager.cpp; path = ../../../src/core/PolyResourceManager.cpp; sourceTree = "<group>"; };
 		8A0F81261BF541F800E24F9B /* PolyScene.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolyScene.cpp; path = ../../../src/core/PolyScene.cpp; sourceTree = "<group>"; };
 		8A0F81271BF541F800E24F9B /* PolySceneEntityInstance.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolySceneEntityInstance.cpp; path = ../../../src/core/PolySceneEntityInstance.cpp; sourceTree = "<group>"; };
-		8A0F81281BF541F800E24F9B /* PolySceneImage.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolySceneImage.cpp; path = ../../../src/core/PolySceneImage.cpp; sourceTree = "<group>"; };
 		8A0F81291BF541F800E24F9B /* PolySceneLabel.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolySceneLabel.cpp; path = ../../../src/core/PolySceneLabel.cpp; sourceTree = "<group>"; };
 		8A0F812A1BF541F800E24F9B /* PolySceneLight.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolySceneLight.cpp; path = ../../../src/core/PolySceneLight.cpp; sourceTree = "<group>"; };
 		8A0F812B1BF541F800E24F9B /* PolySceneLine.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolySceneLine.cpp; path = ../../../src/core/PolySceneLine.cpp; sourceTree = "<group>"; };
@@ -260,6 +254,8 @@
 		8A0F81461BF541F900E24F9B /* tinyxmlparser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = tinyxmlparser.cpp; path = ../../../src/core/tinyxmlparser.cpp; sourceTree = "<group>"; };
 		8A35E19F1C87B20400BF9EEA /* PolyPhysFSFileProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PolyPhysFSFileProvider.h; path = ../../../include/polycode/core/PolyPhysFSFileProvider.h; sourceTree = "<group>"; };
 		8A35E1A01C87B25900BF9EEA /* PolyPhysFSFileProvider.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolyPhysFSFileProvider.cpp; path = ../../../src/core/PolyPhysFSFileProvider.cpp; sourceTree = "<group>"; };
+		AA2456CF1D24434B00E34036 /* duktape.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = duktape.cpp; path = ../../../src/duktape/duktape.cpp; sourceTree = "<group>"; };
+		AA2456D11D24437400E34036 /* stb_vorbis.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = stb_vorbis.cpp; path = ../../../src/core/stb_vorbis.cpp; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -378,7 +374,7 @@
 			isa = PBXGroup;
 			children = (
 				6D9D8C211CD975B1001E636B /* PolycodeLua.cpp */,
-				6D904FDF1CCBF9F900D0E80A /* duktape.c */,
+				AA2456CF1D24434B00E34036 /* duktape.cpp */,
 				8A0F80F91BF541F800E24F9B /* lodepng.cpp */,
 				8A0F80FA1BF541F800E24F9B /* PolyBasicFileProvider.cpp */,
 				8A0F80FB1BF541F800E24F9B /* PolyBezierCurve.cpp */,
@@ -399,7 +395,6 @@
 				8A0F81091BF541F800E24F9B /* PolyEventDispatcher.cpp */,
 				8A0F810A1BF541F800E24F9B /* PolyEventHandler.cpp */,
 				8A0F810B1BF541F800E24F9B /* PolyFont.cpp */,
-				8A0F810C1BF541F800E24F9B /* PolyFontGlyphSheet.cpp */,
 				8A0F810E1BF541F800E24F9B /* PolyGPUDrawBuffer.cpp */,
 				8A0F810F1BF541F800E24F9B /* PolyImage.cpp */,
 				8A0F81101BF541F800E24F9B /* PolyInputEvent.cpp */,
@@ -407,7 +402,6 @@
 				8A0F81121BF541F800E24F9B /* PolyLabel.cpp */,
 				8A0F81131BF541F800E24F9B /* PolyLogger.cpp */,
 				8A0F81141BF541F800E24F9B /* PolyMaterial.cpp */,
-				8A0F81151BF541F800E24F9B /* PolyMaterialManager.cpp */,
 				8A0F81161BF541F800E24F9B /* PolyMatrix4.cpp */,
 				8A0F81171BF541F800E24F9B /* PolyMesh.cpp */,
 				8A0F81181BF541F800E24F9B /* PolyObject.cpp */,
@@ -425,7 +419,6 @@
 				8A0F81251BF541F800E24F9B /* PolyResourceManager.cpp */,
 				8A0F81261BF541F800E24F9B /* PolyScene.cpp */,
 				8A0F81271BF541F800E24F9B /* PolySceneEntityInstance.cpp */,
-				8A0F81281BF541F800E24F9B /* PolySceneImage.cpp */,
 				8A0F81291BF541F800E24F9B /* PolySceneLabel.cpp */,
 				8A0F812A1BF541F800E24F9B /* PolySceneLight.cpp */,
 				8A0F812B1BF541F800E24F9B /* PolySceneLine.cpp */,
@@ -453,6 +446,7 @@
 				8A0F81401BF541F800E24F9B /* PolyVector3.cpp */,
 				8A0F81411BF541F800E24F9B /* PolyVector4.cpp */,
 				8A0F81421BF541F800E24F9B /* rgbe.cpp */,
+				AA2456D11D24437400E34036 /* stb_vorbis.cpp */,
 				8A0F81431BF541F800E24F9B /* tinystr.cpp */,
 				8A0F81441BF541F900E24F9B /* tinyxml.cpp */,
 				8A0F81451BF541F900E24F9B /* tinyxmlerror.cpp */,
@@ -534,6 +528,7 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				AA2456D01D24434B00E34036 /* duktape.cpp in Sources */,
 				8A0F815F1BF541F900E24F9B /* PolyIOSCore.mm in Sources */,
 				6D9D8C221CD975B1001E636B /* PolycodeLua.cpp in Sources */,
 				8A0F81911BF541F900E24F9B /* tinystr.cpp in Sources */,
@@ -548,9 +543,7 @@
 				8A0F818C1BF541F900E24F9B /* PolyTweenManager.cpp in Sources */,
 				8A0F81591BF541F900E24F9B /* PolyFont.cpp in Sources */,
 				8A0F81881BF541F900E24F9B /* PolyThreaded.cpp in Sources */,
-				8A0F81761BF541F900E24F9B /* PolySceneImage.cpp in Sources */,
 				8A0F818D1BF541F900E24F9B /* PolyVector2.cpp in Sources */,
-				8A0F81631BF541F900E24F9B /* PolyMaterialManager.cpp in Sources */,
 				8A0F816C1BF541F900E24F9B /* PolyQuaternion.cpp in Sources */,
 				8A0F81651BF541F900E24F9B /* PolyMesh.cpp in Sources */,
 				8A0F81781BF541F900E24F9B /* PolySceneLight.cpp in Sources */,
@@ -573,6 +566,7 @@
 				8A0F81801BF541F900E24F9B /* PolyServer.cpp in Sources */,
 				8A0F81851BF541F900E24F9B /* PolySoundManager.cpp in Sources */,
 				8A0F818A1BF541F900E24F9B /* PolyTimerManager.cpp in Sources */,
+				AA2456D21D24437400E34036 /* stb_vorbis.cpp in Sources */,
 				8A0F81751BF541F900E24F9B /* PolySceneEntityInstance.cpp in Sources */,
 				8A0F81721BF541F900E24F9B /* PolyResource.cpp in Sources */,
 				8A0F817F1BF541F900E24F9B /* PolySceneSprite.cpp in Sources */,
@@ -586,7 +580,6 @@
 				8A0F81511BF541F900E24F9B /* PolyCoreInput.cpp in Sources */,
 				8A0F815C1BF541F900E24F9B /* PolyGPUDrawBuffer.cpp in Sources */,
 				8A0F81811BF541F900E24F9B /* PolyShader.cpp in Sources */,
-				8A0F815A1BF541F900E24F9B /* PolyFontGlyphSheet.cpp in Sources */,
 				8A0F81901BF541F900E24F9B /* rgbe.cpp in Sources */,
 				8A0F81571BF541F900E24F9B /* PolyEventDispatcher.cpp in Sources */,
 				8A0F81891BF541F900E24F9B /* PolyTimer.cpp in Sources */,
@@ -603,7 +596,6 @@
 				8A0F818E1BF541F900E24F9B /* PolyVector3.cpp in Sources */,
 				8A0F81691BF541F900E24F9B /* PolyPeer.cpp in Sources */,
 				8A0F81531BF541F900E24F9B /* PolyCubemap.cpp in Sources */,
-				6D904FE01CCBF9F900D0E80A /* duktape.c in Sources */,
 				8A0F817E1BF541F900E24F9B /* PolySceneSound.cpp in Sources */,
 				8A0F81791BF541F900E24F9B /* PolySceneLine.cpp in Sources */,
 				8A0F81661BF541F900E24F9B /* PolyObject.cpp in Sources */,

+ 0 - 12
build/ios/TemplateApp/TemplateApp.xcodeproj/project.pbxproj

@@ -8,9 +8,6 @@
 
 /* Begin PBXBuildFile section */
 		6D086CF21CD9B3770057F210 /* AudioToolbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6D086CF11CD9B3770057F210 /* AudioToolbox.framework */; };
-		6D086D661CD9BC330057F210 /* libogg.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 6D086D631CD9BC330057F210 /* libogg.a */; };
-		6D086D671CD9BC330057F210 /* libvorbis.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 6D086D641CD9BC330057F210 /* libvorbis.a */; };
-		6D086D681CD9BC330057F210 /* libvorbisfile.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 6D086D651CD9BC330057F210 /* libvorbisfile.a */; };
 		6D086D6A1CD9BC820057F210 /* test.ogg in Resources */ = {isa = PBXBuildFile; fileRef = 6D086D691CD9BC820057F210 /* test.ogg */; };
 		6D1992381CD692ED00DE01FA /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6D1992341CD692ED00DE01FA /* QuartzCore.framework */; };
 		6D1992391CD692ED00DE01FA /* CoreMotion.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6D1992351CD692ED00DE01FA /* CoreMotion.framework */; };
@@ -34,9 +31,6 @@
 
 /* Begin PBXFileReference section */
 		6D086CF11CD9B3770057F210 /* AudioToolbox.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AudioToolbox.framework; path = System/Library/Frameworks/AudioToolbox.framework; sourceTree = SDKROOT; };
-		6D086D631CD9BC330057F210 /* libogg.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libogg.a; path = ../../../../lib/ios/iphonesimulator/libogg.a; sourceTree = "<group>"; };
-		6D086D641CD9BC330057F210 /* libvorbis.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libvorbis.a; path = ../../../../lib/ios/iphonesimulator/libvorbis.a; sourceTree = "<group>"; };
-		6D086D651CD9BC330057F210 /* libvorbisfile.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libvorbisfile.a; path = ../../../../lib/ios/iphonesimulator/libvorbisfile.a; sourceTree = "<group>"; };
 		6D086D691CD9BC820057F210 /* test.ogg */ = {isa = PBXFileReference; lastKnownFileType = file; name = test.ogg; path = ../../../../../../../Downloads/test.ogg; sourceTree = "<group>"; };
 		6D1992341CD692ED00DE01FA /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
 		6D1992351CD692ED00DE01FA /* CoreMotion.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreMotion.framework; path = System/Library/Frameworks/CoreMotion.framework; sourceTree = SDKROOT; };
@@ -72,14 +66,11 @@
 				6D086CF21CD9B3770057F210 /* AudioToolbox.framework in Frameworks */,
 				6D1992381CD692ED00DE01FA /* QuartzCore.framework in Frameworks */,
 				6D1992391CD692ED00DE01FA /* CoreMotion.framework in Frameworks */,
-				6D086D661CD9BC330057F210 /* libogg.a in Frameworks */,
 				6D90505D1CCBFFD400D0E80A /* liblua.a in Frameworks */,
 				6D19923B1CD692ED00DE01FA /* OpenGLES.framework in Frameworks */,
-				6D086D681CD9BC330057F210 /* libvorbisfile.a in Frameworks */,
 				8A35E14F1C877DDB00BF9EEA /* libFreetype2.a in Frameworks */,
 				8A35E1C61C87B3BC00BF9EEA /* libphysfs.a in Frameworks */,
 				6D19923A1CD692ED00DE01FA /* UIKit.framework in Frameworks */,
-				6D086D671CD9BC330057F210 /* libvorbis.a in Frameworks */,
 				8AD300AB1BF68290004D243B /* libPolycodeCore.a in Frameworks */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
@@ -106,9 +97,6 @@
 		8A0F805E1BF536C800E24F9B /* TemplateApp */ = {
 			isa = PBXGroup;
 			children = (
-				6D086D631CD9BC330057F210 /* libogg.a */,
-				6D086D641CD9BC330057F210 /* libvorbis.a */,
-				6D086D651CD9BC330057F210 /* libvorbisfile.a */,
 				6D086CF11CD9B3770057F210 /* AudioToolbox.framework */,
 				6D1992341CD692ED00DE01FA /* QuartzCore.framework */,
 				6D1992351CD692ED00DE01FA /* CoreMotion.framework */,

Diff do ficheiro suprimidas por serem muito extensas
+ 4 - 3
build/linux/Makefile


+ 4 - 0
build/osx/PolycodeCore/PolycodeCore.xcodeproj/project.pbxproj

@@ -169,6 +169,7 @@
 		8A9D122F1CDD503C00ACD484 /* PolycodeJS.h in Headers */ = {isa = PBXBuildFile; fileRef = 8A9D122D1CDD503C00ACD484 /* PolycodeJS.h */; };
 		8A9D12301CDD503C00ACD484 /* PolycodeJSWrappers.h in Headers */ = {isa = PBXBuildFile; fileRef = 8A9D122E1CDD503C00ACD484 /* PolycodeJSWrappers.h */; };
 		8A9D12321CDD504400ACD484 /* PolycodeJS.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A9D12311CDD504400ACD484 /* PolycodeJS.cpp */; };
+		AA5DCB931D2433BE0021D0F8 /* stb_vorbis.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AA5DCB921D2433BE0021D0F8 /* stb_vorbis.cpp */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXFileReference section */
@@ -335,6 +336,7 @@
 		8A9D122D1CDD503C00ACD484 /* PolycodeJS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PolycodeJS.h; path = ../../../include/polycode/bindings/javascript/PolycodeJS.h; sourceTree = "<group>"; };
 		8A9D122E1CDD503C00ACD484 /* PolycodeJSWrappers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PolycodeJSWrappers.h; path = ../../../include/polycode/bindings/javascript/PolycodeJSWrappers.h; sourceTree = "<group>"; };
 		8A9D12311CDD504400ACD484 /* PolycodeJS.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PolycodeJS.cpp; path = ../../../src/bindings/javascript/PolycodeJS.cpp; sourceTree = "<group>"; };
+		AA5DCB921D2433BE0021D0F8 /* stb_vorbis.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = stb_vorbis.cpp; path = ../../../src/core/stb_vorbis.cpp; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -435,6 +437,7 @@
 				8A8652CF1B72867F009F94DD /* PolyVector4.cpp */,
 				8A7349091B86A54800F660C0 /* lodepng.cpp */,
 				8A8652D01B72867F009F94DD /* rgbe.cpp */,
+				AA5DCB921D2433BE0021D0F8 /* stb_vorbis.cpp */,
 				8A8652D11B72867F009F94DD /* tinystr.cpp */,
 				8A8652D21B72867F009F94DD /* tinyxml.cpp */,
 				8A8652D31B72867F009F94DD /* tinyxmlerror.cpp */,
@@ -711,6 +714,7 @@
 				8A8652DF1B72867F009F94DD /* PolyCoreServices.cpp in Sources */,
 				8A8652FC1B72867F009F94DD /* PolyRenderer.cpp in Sources */,
 				8A8653131B72867F009F94DD /* PolyThreaded.cpp in Sources */,
+				AA5DCB931D2433BE0021D0F8 /* stb_vorbis.cpp in Sources */,
 				8A8652F11B72867F009F94DD /* PolyMesh.cpp in Sources */,
 				8A8652E21B72867F009F94DD /* PolyEntity.cpp in Sources */,
 				8A8652F21B72867F009F94DD /* PolyObject.cpp in Sources */,

+ 0 - 6
build/osx/PolycodeStudio/PolycodeStudio.xcodeproj/project.pbxproj

@@ -52,10 +52,7 @@
 		8A36D2731B8E5BA9009897D0 /* TrackballCamera.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A36D2511B8E5BA9009897D0 /* TrackballCamera.cpp */; };
 		8A36D2741B8E5BA9009897D0 /* TransformGizmo.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8A36D2521B8E5BA9009897D0 /* TransformGizmo.cpp */; };
 		8A36D31B1B8E6FD5009897D0 /* libfreetype.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8A36D3111B8E6FD5009897D0 /* libfreetype.a */; };
-		8A36D31C1B8E6FD5009897D0 /* liblibogg.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8A36D3121B8E6FD5009897D0 /* liblibogg.a */; };
-		8A36D31D1B8E6FD5009897D0 /* liblibvorbis.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8A36D3131B8E6FD5009897D0 /* liblibvorbis.a */; };
 		8A36D31E1B8E6FD5009897D0 /* libportaudio.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8A36D3141B8E6FD5009897D0 /* libportaudio.a */; };
-		8A36D31F1B8E6FD5009897D0 /* liblibvorbisfile.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8A36D3151B8E6FD5009897D0 /* liblibvorbisfile.a */; };
 		8A36D3211B8E6FD5009897D0 /* libphysfs.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8A36D3171B8E6FD5009897D0 /* libphysfs.a */; };
 		8A36D3231B8E6FD5009897D0 /* libPolycore.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8A36D3191B8E6FD5009897D0 /* libPolycore.a */; };
 		8A36D3241B8E6FD5009897D0 /* libz.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8A36D31A1B8E6FD5009897D0 /* libz.a */; };
@@ -168,16 +165,13 @@
 				8A22D92F1BCC6809009EF0A6 /* CoreAudio.framework in Frameworks */,
 				8A22D92D1BCC67E6009EF0A6 /* AudioUnit.framework in Frameworks */,
 				8A22D92B1BCC67DE009EF0A6 /* OpenGL.framework in Frameworks */,
-				8A36D31D1B8E6FD5009897D0 /* liblibvorbis.a in Frameworks */,
 				8A36D3231B8E6FD5009897D0 /* libPolycore.a in Frameworks */,
-				8A36D31F1B8E6FD5009897D0 /* liblibvorbisfile.a in Frameworks */,
 				8A36D31E1B8E6FD5009897D0 /* libportaudio.a in Frameworks */,
 				8A36D3261B8E6FE7009897D0 /* libPolycodeUI.a in Frameworks */,
 				8A36D31B1B8E6FD5009897D0 /* libfreetype.a in Frameworks */,
 				6D90506D1CCC058F00D0E80A /* liblua.a in Frameworks */,
 				8A36D3211B8E6FD5009897D0 /* libphysfs.a in Frameworks */,
 				8A36D3241B8E6FD5009897D0 /* libz.a in Frameworks */,
-				8A36D31C1B8E6FD5009897D0 /* liblibogg.a in Frameworks */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 0 - 6
build/osx/TemplateApp/TemplateApp.xcodeproj/project.pbxproj

@@ -24,9 +24,6 @@
 		8A86533B1B72931C009F94DD /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8A86533A1B72931C009F94DD /* Images.xcassets */; };
 		8A86533E1B72931C009F94DD /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 8A86533C1B72931C009F94DD /* MainMenu.xib */; };
 		8A86535C1B72949F009F94DD /* libfreetype.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8A8653531B72949F009F94DD /* libfreetype.a */; };
-		8A86535D1B72949F009F94DD /* liblibogg.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8A8653541B72949F009F94DD /* liblibogg.a */; };
-		8A86535E1B72949F009F94DD /* liblibvorbis.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8A8653551B72949F009F94DD /* liblibvorbis.a */; };
-		8A86535F1B72949F009F94DD /* liblibvorbisfile.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8A8653561B72949F009F94DD /* liblibvorbisfile.a */; };
 		8A8653611B72949F009F94DD /* libphysfs.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8A8653581B72949F009F94DD /* libphysfs.a */; };
 		8A8653631B72949F009F94DD /* libPolycore.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8A86535A1B72949F009F94DD /* libPolycore.a */; };
 		8A8653641B72949F009F94DD /* libz.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8A86535B1B72949F009F94DD /* libz.a */; };
@@ -89,13 +86,10 @@
 				8A8653641B72949F009F94DD /* libz.a in Frameworks */,
 				8A825F271B82A2680039E823 /* libportaudio.a in Frameworks */,
 				8A8653631B72949F009F94DD /* libPolycore.a in Frameworks */,
-				8A86535F1B72949F009F94DD /* liblibvorbisfile.a in Frameworks */,
-				8A86535E1B72949F009F94DD /* liblibvorbis.a in Frameworks */,
 				8A86535C1B72949F009F94DD /* libfreetype.a in Frameworks */,
 				6D90506B1CCC057C00D0E80A /* liblua.a in Frameworks */,
 				6DD2D0B61BEEDC150026D85C /* libportaudio.a in Frameworks */,
 				8A8653611B72949F009F94DD /* libphysfs.a in Frameworks */,
-				8A86535D1B72949F009F94DD /* liblibogg.a in Frameworks */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 1 - 0
build/windows/win32/PolycodeCore/PolycodeCore.vcxproj

@@ -229,6 +229,7 @@
     <ClCompile Include="..\..\..\..\src\core\PolyVector4.cpp" />
     <ClCompile Include="..\..\..\..\src\core\PolyWinCore.cpp" />
     <ClCompile Include="..\..\..\..\src\core\rgbe.cpp" />
+    <ClCompile Include="..\..\..\..\src\core\stb_vorbis.cpp" />
     <ClCompile Include="..\..\..\..\src\core\tinystr.cpp" />
     <ClCompile Include="..\..\..\..\src\core\tinyxml.cpp" />
     <ClCompile Include="..\..\..\..\src\core\tinyxmlerror.cpp" />

+ 4 - 1
build/windows/win32/PolycodeCore/PolycodeCore.vcxproj.filters

@@ -258,6 +258,9 @@
     <ClCompile Include="..\..\..\..\src\view\win32\PolycodeView.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="..\..\..\..\src\core\stb_vorbis.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\..\..\..\include\polycode\core\PolyFont.h">
@@ -480,7 +483,7 @@
       <Filter>Header Files</Filter>
     </ClInclude>
     <ClInclude Include="..\..\..\..\include\polycode\view\win32\PolycodeView.h">
-      <Filter>Source Files</Filter>
+      <Filter>Header Files</Filter>
     </ClInclude>
   </ItemGroup>
 </Project>

+ 4 - 4
build/windows/win32/PolycodeStudio/PolycodeStudio.vcxproj

@@ -96,7 +96,7 @@
     <Link>
       <SubSystem>Windows</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
-      <AdditionalDependencies>liblua.lib;ws2_32.lib;winmm.lib;pathcch.lib;portaudio.lib;libvorbisfile.lib;libvorbis.lib;libogg.lib;opengl32.lib;glew32s.lib;glew32.lib;freetype.lib;physfs.lib;zlib.lib;PolycodeUI_d.lib;Polycore_d.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>liblua.lib;ws2_32.lib;winmm.lib;pathcch.lib;portaudio.lib;opengl32.lib;glew32s.lib;glew32.lib;freetype.lib;physfs.lib;zlib.lib;PolycodeUI_d.lib;Polycore_d.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
     <PostBuildEvent>
       <Command>if not exist "$(TargetDir)default.pak" copy "..\..\..\..\assets\default\default.pak" "$(TargetDir)"
@@ -122,7 +122,7 @@ if not exist "$(TargetDir)Standalone" (
     <Link>
       <SubSystem>Windows</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
-      <AdditionalDependencies>liblua.lib;ws2_32.lib;winmm.lib;pathcch.lib;portaudio.lib;libvorbisfile.lib;libvorbis.lib;libogg.lib;opengl32.lib;glew32s.lib;glew32.lib;freetype.lib;physfs.lib;zlib.lib;PolycodeUI_d.lib;Polycore_d.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>liblua.lib;ws2_32.lib;winmm.lib;pathcch.lib;portaudio.lib;opengl32.lib;glew32s.lib;glew32.lib;freetype.lib;physfs.lib;zlib.lib;PolycodeUI_d.lib;Polycore_d.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
     <PostBuildEvent>
       <Command>if not exist "$(TargetDir)default.pak" copy "..\..\..\..\assets\default\default.pak" "$(TargetDir)"
@@ -152,7 +152,7 @@ if not exist "$(TargetDir)Standalone" (
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
-      <AdditionalDependencies>liblua.lib;ws2_32.lib;winmm.lib;pathcch.lib;portaudio.lib;libvorbisfile.lib;libvorbis.lib;libogg.lib;opengl32.lib;glew32s.lib;glew32.lib;freetype.lib;physfs.lib;zlib.lib;PolycodeUI.lib;Polycore.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>liblua.lib;ws2_32.lib;winmm.lib;pathcch.lib;portaudio.lib;opengl32.lib;glew32s.lib;glew32.lib;freetype.lib;physfs.lib;zlib.lib;PolycodeUI.lib;Polycore.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
     <PostBuildEvent>
       <Command>if not exist "$(TargetDir)default.pak" copy "..\..\..\..\assets\default\default.pak" "$(TargetDir)"
@@ -182,7 +182,7 @@ if not exist "$(TargetDir)Standalone" (
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
-      <AdditionalDependencies>liblua.lib;ws2_32.lib;winmm.lib;pathcch.lib;portaudio.lib;libvorbisfile.lib;libvorbis.lib;libogg.lib;opengl32.lib;glew32s.lib;glew32.lib;freetype.lib;physfs.lib;zlib.lib;PolycodeUI.lib;Polycore.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>liblua.lib;ws2_32.lib;winmm.lib;pathcch.lib;portaudio.lib;opengl32.lib;glew32s.lib;glew32.lib;freetype.lib;physfs.lib;zlib.lib;PolycodeUI.lib;Polycore.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
     <PostBuildEvent>
       <Command>if not exist "$(TargetDir)default.pak" copy "..\..\..\..\assets\default\default.pak" "$(TargetDir)"

+ 4 - 4
build/windows/win32/TemplateApp/TemplateApp.vcxproj

@@ -100,7 +100,7 @@
     <Link>
       <SubSystem>Windows</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
-      <AdditionalDependencies>ws2_32.lib;winmm.lib;portaudio.lib;libvorbisfile.lib;libvorbis.lib;libogg.lib;opengl32.lib;glew32s.lib;glew32.lib;freetype.lib;physfs.lib;zlib.lib;liblua.lib;Polycore_d.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>ws2_32.lib;winmm.lib;portaudio.lib;opengl32.lib;glew32s.lib;glew32.lib;freetype.lib;physfs.lib;zlib.lib;liblua.lib;Polycore_d.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
     <PostBuildEvent>
       <Command>if not exist "$(TargetDir)default.pak" copy "..\..\..\..\assets\default\default.pak" "$(TargetDir)"
@@ -119,7 +119,7 @@ if not exist "$(TargetDir)glew32.dll" copy "..\..\..\..\lib\win32\$(PlatformTarg
     <Link>
       <SubSystem>Windows</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
-      <AdditionalDependencies>ws2_32.lib;winmm.lib;portaudio.lib;libvorbisfile.lib;libvorbis.lib;libogg.lib;opengl32.lib;glew32s.lib;glew32.lib;freetype.lib;physfs.lib;zlib.lib;liblua.lib;Polycore_d.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>ws2_32.lib;winmm.lib;portaudio.lib;opengl32.lib;glew32s.lib;glew32.lib;freetype.lib;physfs.lib;zlib.lib;liblua.lib;Polycore_d.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
     <PostBuildEvent>
       <Command>if not exist "$(TargetDir)default.pak" copy "..\..\..\..\assets\default\default.pak" "$(TargetDir)"
@@ -142,7 +142,7 @@ if not exist "$(TargetDir)glew32.dll" copy "..\..\..\..\lib\win32\$(PlatformTarg
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
-      <AdditionalDependencies>ws2_32.lib;winmm.lib;portaudio.lib;libvorbisfile.lib;libvorbis.lib;libogg.lib;opengl32.lib;glew32s.lib;glew32.lib;freetype.lib;physfs.lib;zlib.lib;liblua.lib;Polycore.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>ws2_32.lib;winmm.lib;portaudio.lib;opengl32.lib;glew32s.lib;glew32.lib;freetype.lib;physfs.lib;zlib.lib;liblua.lib;Polycore.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
     <PostBuildEvent>
       <Command>if not exist "$(TargetDir)default.pak" copy "..\..\..\..\assets\default\default.pak" "$(TargetDir)"
@@ -165,7 +165,7 @@ if not exist "$(TargetDir)glew32.dll" copy "..\..\..\..\lib\win32\$(PlatformTarg
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
-      <AdditionalDependencies>ws2_32.lib;winmm.lib;portaudio.lib;libvorbisfile.lib;libvorbis.lib;libogg.lib;opengl32.lib;glew32s.lib;glew32.lib;freetype.lib;physfs.lib;zlib.lib;liblua.lib;Polycore.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>ws2_32.lib;winmm.lib;portaudio.lib;opengl32.lib;glew32s.lib;glew32.lib;freetype.lib;physfs.lib;zlib.lib;liblua.lib;Polycore.lib;%(AdditionalDependencies)</AdditionalDependencies>
     </Link>
     <PostBuildEvent>
       <Command>if not exist "$(TargetDir)default.pak" copy "..\..\..\..\assets\default\default.pak" "$(TargetDir)"

+ 1 - 1
include/Polycode.h

@@ -94,7 +94,7 @@
  	#endif
 #else
     #if defined(_WINDOWS) && !defined(_MINGW)
-		#if defined(WINAPI_FAMILY)
+		#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PC_APP || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
 			#include "polycode/core/PolyUWPCore.h"
 		#else
 			#include "polycode/core/PolyWinCore.h"

+ 0 - 2666
include/libpng15/png.h

@@ -1,2666 +0,0 @@
-
-/* png.h - header file for PNG reference library
- *
- * libpng version 1.5.10 - March 29, 2012
- * Copyright (c) 1998-2012 Glenn Randers-Pehrson
- * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
- * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- *
- * This code is released under the libpng license (See LICENSE, below)
- *
- * Authors and maintainers:
- *   libpng versions 0.71, May 1995, through 0.88, January 1996: Guy Schalnat
- *   libpng versions 0.89c, June 1996, through 0.96, May 1997: Andreas Dilger
- *   libpng versions 0.97, January 1998, through 1.5.10 - March 29, 2012: Glenn
- *   See also "Contributing Authors", below.
- *
- * Note about libpng version numbers:
- *
- *   Due to various miscommunications, unforeseen code incompatibilities
- *   and occasional factors outside the authors' control, version numbering
- *   on the library has not always been consistent and straightforward.
- *   The following table summarizes matters since version 0.89c, which was
- *   the first widely used release:
- *
- *    source                 png.h  png.h  shared-lib
- *    version                string   int  version
- *    -------                ------ -----  ----------
- *    0.89c "1.0 beta 3"     0.89      89  1.0.89
- *    0.90  "1.0 beta 4"     0.90      90  0.90  [should have been 2.0.90]
- *    0.95  "1.0 beta 5"     0.95      95  0.95  [should have been 2.0.95]
- *    0.96  "1.0 beta 6"     0.96      96  0.96  [should have been 2.0.96]
- *    0.97b "1.00.97 beta 7" 1.00.97   97  1.0.1 [should have been 2.0.97]
- *    0.97c                  0.97      97  2.0.97
- *    0.98                   0.98      98  2.0.98
- *    0.99                   0.99      98  2.0.99
- *    0.99a-m                0.99      99  2.0.99
- *    1.00                   1.00     100  2.1.0 [100 should be 10000]
- *    1.0.0      (from here on, the   100  2.1.0 [100 should be 10000]
- *    1.0.1       png.h string is   10001  2.1.0
- *    1.0.1a-e    identical to the  10002  from here on, the shared library
- *    1.0.2       source version)   10002  is 2.V where V is the source code
- *    1.0.2a-b                      10003  version, except as noted.
- *    1.0.3                         10003
- *    1.0.3a-d                      10004
- *    1.0.4                         10004
- *    1.0.4a-f                      10005
- *    1.0.5 (+ 2 patches)           10005
- *    1.0.5a-d                      10006
- *    1.0.5e-r                      10100 (not source compatible)
- *    1.0.5s-v                      10006 (not binary compatible)
- *    1.0.6 (+ 3 patches)           10006 (still binary incompatible)
- *    1.0.6d-f                      10007 (still binary incompatible)
- *    1.0.6g                        10007
- *    1.0.6h                        10007  10.6h (testing xy.z so-numbering)
- *    1.0.6i                        10007  10.6i
- *    1.0.6j                        10007  2.1.0.6j (incompatible with 1.0.0)
- *    1.0.7beta11-14        DLLNUM  10007  2.1.0.7beta11-14 (binary compatible)
- *    1.0.7beta15-18           1    10007  2.1.0.7beta15-18 (binary compatible)
- *    1.0.7rc1-2               1    10007  2.1.0.7rc1-2 (binary compatible)
- *    1.0.7                    1    10007  (still compatible)
- *    1.0.8beta1-4             1    10008  2.1.0.8beta1-4
- *    1.0.8rc1                 1    10008  2.1.0.8rc1
- *    1.0.8                    1    10008  2.1.0.8
- *    1.0.9beta1-6             1    10009  2.1.0.9beta1-6
- *    1.0.9rc1                 1    10009  2.1.0.9rc1
- *    1.0.9beta7-10            1    10009  2.1.0.9beta7-10
- *    1.0.9rc2                 1    10009  2.1.0.9rc2
- *    1.0.9                    1    10009  2.1.0.9
- *    1.0.10beta1              1    10010  2.1.0.10beta1
- *    1.0.10rc1                1    10010  2.1.0.10rc1
- *    1.0.10                   1    10010  2.1.0.10
- *    1.0.11beta1-3            1    10011  2.1.0.11beta1-3
- *    1.0.11rc1                1    10011  2.1.0.11rc1
- *    1.0.11                   1    10011  2.1.0.11
- *    1.0.12beta1-2            2    10012  2.1.0.12beta1-2
- *    1.0.12rc1                2    10012  2.1.0.12rc1
- *    1.0.12                   2    10012  2.1.0.12
- *    1.1.0a-f                 -    10100  2.1.1.0a-f (branch abandoned)
- *    1.2.0beta1-2             2    10200  2.1.2.0beta1-2
- *    1.2.0beta3-5             3    10200  3.1.2.0beta3-5
- *    1.2.0rc1                 3    10200  3.1.2.0rc1
- *    1.2.0                    3    10200  3.1.2.0
- *    1.2.1beta1-4             3    10201  3.1.2.1beta1-4
- *    1.2.1rc1-2               3    10201  3.1.2.1rc1-2
- *    1.2.1                    3    10201  3.1.2.1
- *    1.2.2beta1-6            12    10202  12.so.0.1.2.2beta1-6
- *    1.0.13beta1             10    10013  10.so.0.1.0.13beta1
- *    1.0.13rc1               10    10013  10.so.0.1.0.13rc1
- *    1.2.2rc1                12    10202  12.so.0.1.2.2rc1
- *    1.0.13                  10    10013  10.so.0.1.0.13
- *    1.2.2                   12    10202  12.so.0.1.2.2
- *    1.2.3rc1-6              12    10203  12.so.0.1.2.3rc1-6
- *    1.2.3                   12    10203  12.so.0.1.2.3
- *    1.2.4beta1-3            13    10204  12.so.0.1.2.4beta1-3
- *    1.0.14rc1               13    10014  10.so.0.1.0.14rc1
- *    1.2.4rc1                13    10204  12.so.0.1.2.4rc1
- *    1.0.14                  10    10014  10.so.0.1.0.14
- *    1.2.4                   13    10204  12.so.0.1.2.4
- *    1.2.5beta1-2            13    10205  12.so.0.1.2.5beta1-2
- *    1.0.15rc1-3             10    10015  10.so.0.1.0.15rc1-3
- *    1.2.5rc1-3              13    10205  12.so.0.1.2.5rc1-3
- *    1.0.15                  10    10015  10.so.0.1.0.15
- *    1.2.5                   13    10205  12.so.0.1.2.5
- *    1.2.6beta1-4            13    10206  12.so.0.1.2.6beta1-4
- *    1.0.16                  10    10016  10.so.0.1.0.16
- *    1.2.6                   13    10206  12.so.0.1.2.6
- *    1.2.7beta1-2            13    10207  12.so.0.1.2.7beta1-2
- *    1.0.17rc1               10    10017  12.so.0.1.0.17rc1
- *    1.2.7rc1                13    10207  12.so.0.1.2.7rc1
- *    1.0.17                  10    10017  12.so.0.1.0.17
- *    1.2.7                   13    10207  12.so.0.1.2.7
- *    1.2.8beta1-5            13    10208  12.so.0.1.2.8beta1-5
- *    1.0.18rc1-5             10    10018  12.so.0.1.0.18rc1-5
- *    1.2.8rc1-5              13    10208  12.so.0.1.2.8rc1-5
- *    1.0.18                  10    10018  12.so.0.1.0.18
- *    1.2.8                   13    10208  12.so.0.1.2.8
- *    1.2.9beta1-3            13    10209  12.so.0.1.2.9beta1-3
- *    1.2.9beta4-11           13    10209  12.so.0.9[.0]
- *    1.2.9rc1                13    10209  12.so.0.9[.0]
- *    1.2.9                   13    10209  12.so.0.9[.0]
- *    1.2.10beta1-7           13    10210  12.so.0.10[.0]
- *    1.2.10rc1-2             13    10210  12.so.0.10[.0]
- *    1.2.10                  13    10210  12.so.0.10[.0]
- *    1.4.0beta1-5            14    10400  14.so.0.0[.0]
- *    1.2.11beta1-4           13    10211  12.so.0.11[.0]
- *    1.4.0beta7-8            14    10400  14.so.0.0[.0]
- *    1.2.11                  13    10211  12.so.0.11[.0]
- *    1.2.12                  13    10212  12.so.0.12[.0]
- *    1.4.0beta9-14           14    10400  14.so.0.0[.0]
- *    1.2.13                  13    10213  12.so.0.13[.0]
- *    1.4.0beta15-36          14    10400  14.so.0.0[.0]
- *    1.4.0beta37-87          14    10400  14.so.14.0[.0]
- *    1.4.0rc01               14    10400  14.so.14.0[.0]
- *    1.4.0beta88-109         14    10400  14.so.14.0[.0]
- *    1.4.0rc02-08            14    10400  14.so.14.0[.0]
- *    1.4.0                   14    10400  14.so.14.0[.0]
- *    1.4.1beta01-03          14    10401  14.so.14.1[.0]
- *    1.4.1rc01               14    10401  14.so.14.1[.0]
- *    1.4.1beta04-12          14    10401  14.so.14.1[.0]
- *    1.4.1                   14    10401  14.so.14.1[.0]
- *    1.4.2                   14    10402  14.so.14.2[.0]
- *    1.4.3                   14    10403  14.so.14.3[.0]
- *    1.4.4                   14    10404  14.so.14.4[.0]
- *    1.5.0beta01-58          15    10500  15.so.15.0[.0]
- *    1.5.0rc01-07            15    10500  15.so.15.0[.0]
- *    1.5.0                   15    10500  15.so.15.0[.0]
- *    1.5.1beta01-11          15    10501  15.so.15.1[.0]
- *    1.5.1rc01-02            15    10501  15.so.15.1[.0]
- *    1.5.1                   15    10501  15.so.15.1[.0]
- *    1.5.2beta01-03          15    10502  15.so.15.2[.0]
- *    1.5.2rc01-03            15    10502  15.so.15.2[.0]
- *    1.5.2                   15    10502  15.so.15.2[.0]
- *    1.5.3beta01-10          15    10503  15.so.15.3[.0]
- *    1.5.3rc01-02            15    10503  15.so.15.3[.0]
- *    1.5.3beta11             15    10503  15.so.15.3[.0]
- *    1.5.3 [omitted]
- *    1.5.4beta01-08          15    10504  15.so.15.4[.0]
- *    1.5.4rc01               15    10504  15.so.15.4[.0]
- *    1.5.4                   15    10504  15.so.15.4[.0]
- *    1.5.5beta01-08          15    10505  15.so.15.5[.0]
- *    1.5.5rc01               15    10505  15.so.15.5[.0]
- *    1.5.5                   15    10505  15.so.15.5[.0]
- *    1.5.6beta01-07          15    10506  15.so.15.6[.0]
- *    1.5.6rc01-03            15    10506  15.so.15.6[.0]
- *    1.5.6                   15    10506  15.so.15.6[.0]
- *    1.5.7beta01-05          15    10507  15.so.15.7[.0]
- *    1.5.7rc01-03            15    10507  15.so.15.7[.0]
- *    1.5.7                   15    10507  15.so.15.7[.0]
- *    1.5.8beta01             15    10508  15.so.15.8[.0]
- *    1.5.8rc01               15    10508  15.so.15.8[.0]
- *    1.5.8                   15    10508  15.so.15.8[.0]
- *    1.5.9beta01-02          15    10509  15.so.15.9[.0]
- *    1.5.9rc01               15    10509  15.so.15.9[.0]
- *    1.5.9                   15    10509  15.so.15.9[.0]
- *    1.5.10beta01-05         15    10510  15.so.15.10[.0]
- *    1.5.10                  15    10510  15.so.15.10[.0]
- *
- *   Henceforth the source version will match the shared-library major
- *   and minor numbers; the shared-library major version number will be
- *   used for changes in backward compatibility, as it is intended.  The
- *   PNG_LIBPNG_VER macro, which is not used within libpng but is available
- *   for applications, is an unsigned integer of the form xyyzz corresponding
- *   to the source version x.y.z (leading zeros in y and z).  Beta versions
- *   were given the previous public release number plus a letter, until
- *   version 1.0.6j; from then on they were given the upcoming public
- *   release number plus "betaNN" or "rcNN".
- *
- *   Binary incompatibility exists only when applications make direct access
- *   to the info_ptr or png_ptr members through png.h, and the compiled
- *   application is loaded with a different version of the library.
- *
- *   DLLNUM will change each time there are forward or backward changes
- *   in binary compatibility (e.g., when a new feature is added).
- *
- * See libpng-manual.txt or libpng.3 for more information.  The PNG
- * specification is available as a W3C Recommendation and as an ISO
- * Specification, <http://www.w3.org/TR/2003/REC-PNG-20031110/
- */
-
-/*
- * COPYRIGHT NOTICE, DISCLAIMER, and LICENSE:
- *
- * If you modify libpng you may insert additional notices immediately following
- * this sentence.
- *
- * This code is released under the libpng license.
- *
- * libpng versions 1.2.6, August 15, 2004, through 1.5.10, March 29, 2012, are
- * Copyright (c) 2004, 2006-2012 Glenn Randers-Pehrson, and are
- * distributed according to the same disclaimer and license as libpng-1.2.5
- * with the following individual added to the list of Contributing Authors:
- *
- *    Cosmin Truta
- *
- * libpng versions 1.0.7, July 1, 2000, through 1.2.5, October 3, 2002, are
- * Copyright (c) 2000-2002 Glenn Randers-Pehrson, and are
- * distributed according to the same disclaimer and license as libpng-1.0.6
- * with the following individuals added to the list of Contributing Authors:
- *
- *    Simon-Pierre Cadieux
- *    Eric S. Raymond
- *    Gilles Vollant
- *
- * and with the following additions to the disclaimer:
- *
- *    There is no warranty against interference with your enjoyment of the
- *    library or against infringement.  There is no warranty that our
- *    efforts or the library will fulfill any of your particular purposes
- *    or needs.  This library is provided with all faults, and the entire
- *    risk of satisfactory quality, performance, accuracy, and effort is with
- *    the user.
- *
- * libpng versions 0.97, January 1998, through 1.0.6, March 20, 2000, are
- * Copyright (c) 1998, 1999, 2000 Glenn Randers-Pehrson, and are
- * distributed according to the same disclaimer and license as libpng-0.96,
- * with the following individuals added to the list of Contributing Authors:
- *
- *    Tom Lane
- *    Glenn Randers-Pehrson
- *    Willem van Schaik
- *
- * libpng versions 0.89, June 1996, through 0.96, May 1997, are
- * Copyright (c) 1996, 1997 Andreas Dilger
- * Distributed according to the same disclaimer and license as libpng-0.88,
- * with the following individuals added to the list of Contributing Authors:
- *
- *    John Bowler
- *    Kevin Bracey
- *    Sam Bushell
- *    Magnus Holmgren
- *    Greg Roelofs
- *    Tom Tanner
- *
- * libpng versions 0.5, May 1995, through 0.88, January 1996, are
- * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
- *
- * For the purposes of this copyright and license, "Contributing Authors"
- * is defined as the following set of individuals:
- *
- *    Andreas Dilger
- *    Dave Martindale
- *    Guy Eric Schalnat
- *    Paul Schmidt
- *    Tim Wegner
- *
- * The PNG Reference Library is supplied "AS IS".  The Contributing Authors
- * and Group 42, Inc. disclaim all warranties, expressed or implied,
- * including, without limitation, the warranties of merchantability and of
- * fitness for any purpose.  The Contributing Authors and Group 42, Inc.
- * assume no liability for direct, indirect, incidental, special, exemplary,
- * or consequential damages, which may result from the use of the PNG
- * Reference Library, even if advised of the possibility of such damage.
- *
- * Permission is hereby granted to use, copy, modify, and distribute this
- * source code, or portions hereof, for any purpose, without fee, subject
- * to the following restrictions:
- *
- *   1. The origin of this source code must not be misrepresented.
- *
- *   2. Altered versions must be plainly marked as such and must not
- *      be misrepresented as being the original source.
- *
- *   3. This Copyright notice may not be removed or altered from
- *      any source or altered source distribution.
- *
- * The Contributing Authors and Group 42, Inc. specifically permit, without
- * fee, and encourage the use of this source code as a component to
- * supporting the PNG file format in commercial products.  If you use this
- * source code in a product, acknowledgment is not required but would be
- * appreciated.
- */
-
-/*
- * A "png_get_copyright" function is available, for convenient use in "about"
- * boxes and the like:
- *
- *     printf("%s", png_get_copyright(NULL));
- *
- * Also, the PNG logo (in PNG format, of course) is supplied in the
- * files "pngbar.png" and "pngbar.jpg (88x31) and "pngnow.png" (98x31).
- */
-
-/*
- * Libpng is OSI Certified Open Source Software.  OSI Certified is a
- * certification mark of the Open Source Initiative.
- */
-
-/*
- * The contributing authors would like to thank all those who helped
- * with testing, bug fixes, and patience.  This wouldn't have been
- * possible without all of you.
- *
- * Thanks to Frank J. T. Wojcik for helping with the documentation.
- */
-
-/*
- * Y2K compliance in libpng:
- * =========================
- *
- *    March 29, 2012
- *
- *    Since the PNG Development group is an ad-hoc body, we can't make
- *    an official declaration.
- *
- *    This is your unofficial assurance that libpng from version 0.71 and
- *    upward through 1.5.10 are Y2K compliant.  It is my belief that
- *    earlier versions were also Y2K compliant.
- *
- *    Libpng only has two year fields.  One is a 2-byte unsigned integer
- *    that will hold years up to 65535.  The other holds the date in text
- *    format, and will hold years up to 9999.
- *
- *    The integer is
- *        "png_uint_16 year" in png_time_struct.
- *
- *    The string is
- *        "png_char time_buffer" in png_struct
- *
- *    There are seven time-related functions:
- *        png.c: png_convert_to_rfc_1123() in png.c
- *          (formerly png_convert_to_rfc_1152() in error)
- *        png_convert_from_struct_tm() in pngwrite.c, called in pngwrite.c
- *        png_convert_from_time_t() in pngwrite.c
- *        png_get_tIME() in pngget.c
- *        png_handle_tIME() in pngrutil.c, called in pngread.c
- *        png_set_tIME() in pngset.c
- *        png_write_tIME() in pngwutil.c, called in pngwrite.c
- *
- *    All handle dates properly in a Y2K environment.  The
- *    png_convert_from_time_t() function calls gmtime() to convert from system
- *    clock time, which returns (year - 1900), which we properly convert to
- *    the full 4-digit year.  There is a possibility that applications using
- *    libpng are not passing 4-digit years into the png_convert_to_rfc_1123()
- *    function, or that they are incorrectly passing only a 2-digit year
- *    instead of "year - 1900" into the png_convert_from_struct_tm() function,
- *    but this is not under our control.  The libpng documentation has always
- *    stated that it works with 4-digit years, and the APIs have been
- *    documented as such.
- *
- *    The tIME chunk itself is also Y2K compliant.  It uses a 2-byte unsigned
- *    integer to hold the year, and can hold years as large as 65535.
- *
- *    zlib, upon which libpng depends, is also Y2K compliant.  It contains
- *    no date-related code.
- *
- *       Glenn Randers-Pehrson
- *       libpng maintainer
- *       PNG Development Group
- */
-
-#ifndef PNG_H
-#define PNG_H
-
-/* This is not the place to learn how to use libpng. The file libpng-manual.txt
- * describes how to use libpng, and the file example.c summarizes it
- * with some code on which to build.  This file is useful for looking
- * at the actual function definitions and structure components.
- *
- * If you just need to read a PNG file and don't want to read the documentation
- * skip to the end of this file and read the section entitled 'simplified API'.
- */
-
-/* Version information for png.h - this should match the version in png.c */
-#define PNG_LIBPNG_VER_STRING "1.5.10"
-#define PNG_HEADER_VERSION_STRING \
-     " libpng version 1.5.10 - March 29, 2012\n"
-
-#define PNG_LIBPNG_VER_SONUM   15
-#define PNG_LIBPNG_VER_DLLNUM  15
-
-/* These should match the first 3 components of PNG_LIBPNG_VER_STRING: */
-#define PNG_LIBPNG_VER_MAJOR   1
-#define PNG_LIBPNG_VER_MINOR   5
-#define PNG_LIBPNG_VER_RELEASE 10
-
-/* This should match the numeric part of the final component of
- * PNG_LIBPNG_VER_STRING, omitting any leading zero:
- */
-
-#define PNG_LIBPNG_VER_BUILD  0
-
-/* Release Status */
-#define PNG_LIBPNG_BUILD_ALPHA    1
-#define PNG_LIBPNG_BUILD_BETA     2
-#define PNG_LIBPNG_BUILD_RC       3
-#define PNG_LIBPNG_BUILD_STABLE   4
-#define PNG_LIBPNG_BUILD_RELEASE_STATUS_MASK 7
-
-/* Release-Specific Flags */
-#define PNG_LIBPNG_BUILD_PATCH    8 /* Can be OR'ed with
-                                       PNG_LIBPNG_BUILD_STABLE only */
-#define PNG_LIBPNG_BUILD_PRIVATE 16 /* Cannot be OR'ed with
-                                       PNG_LIBPNG_BUILD_SPECIAL */
-#define PNG_LIBPNG_BUILD_SPECIAL 32 /* Cannot be OR'ed with
-                                       PNG_LIBPNG_BUILD_PRIVATE */
-
-#define PNG_LIBPNG_BUILD_BASE_TYPE PNG_LIBPNG_BUILD_STABLE
-
-/* Careful here.  At one time, Guy wanted to use 082, but that would be octal.
- * We must not include leading zeros.
- * Versions 0.7 through 1.0.0 were in the range 0 to 100 here (only
- * version 1.0.0 was mis-numbered 100 instead of 10000).  From
- * version 1.0.1 it's    xxyyzz, where x=major, y=minor, z=release
- */
-#define PNG_LIBPNG_VER 10510 /* 1.5.10 */
-
-/* Library configuration: these options cannot be changed after
- * the library has been built.
- */
-#ifndef PNGLCONF_H
-    /* If pnglibconf.h is missing, you can
-     * copy scripts/pnglibconf.h.prebuilt to pnglibconf.h
-     */
-#   include "pnglibconf.h"
-#endif
-
-#ifndef PNG_VERSION_INFO_ONLY
-#  ifndef PNG_BUILDING_SYMBOL_TABLE
-  /*
-   *   Standard header files (not needed for the version info or while
-   *   building symbol table -- see scripts/pnglibconf.dfa)
-   */
-#    ifdef PNG_SETJMP_SUPPORTED
-#      include <setjmp.h>
-#    endif
-
-    /* Need the time information for converting tIME chunks, it
-     * defines struct tm:
-     */
-#    ifdef PNG_CONVERT_tIME_SUPPORTED
-       /* "time.h" functions are not supported on all operating systems */
-#      include <time.h>
-#    endif
-#  endif
-
-/* Machine specific configuration. */
-#  include "pngconf.h"
-#endif
-
-/*
- * Added at libpng-1.2.8
- *
- * Ref MSDN: Private as priority over Special
- * VS_FF_PRIVATEBUILD File *was not* built using standard release
- * procedures. If this value is given, the StringFileInfo block must
- * contain a PrivateBuild string.
- *
- * VS_FF_SPECIALBUILD File *was* built by the original company using
- * standard release procedures but is a variation of the standard
- * file of the same version number. If this value is given, the
- * StringFileInfo block must contain a SpecialBuild string.
- */
-
-#ifdef PNG_USER_PRIVATEBUILD /* From pnglibconf.h */
-#  define PNG_LIBPNG_BUILD_TYPE \
-       (PNG_LIBPNG_BUILD_BASE_TYPE | PNG_LIBPNG_BUILD_PRIVATE)
-#else
-#  ifdef PNG_LIBPNG_SPECIALBUILD
-#    define PNG_LIBPNG_BUILD_TYPE \
-         (PNG_LIBPNG_BUILD_BASE_TYPE | PNG_LIBPNG_BUILD_SPECIAL)
-#  else
-#    define PNG_LIBPNG_BUILD_TYPE (PNG_LIBPNG_BUILD_BASE_TYPE)
-#  endif
-#endif
-
-#ifndef PNG_VERSION_INFO_ONLY
-
-/* Inhibit C++ name-mangling for libpng functions but not for system calls. */
-#ifdef __cplusplus
-extern "C" {
-#endif /* __cplusplus */
-
-/* Version information for C files, stored in png.c.  This had better match
- * the version above.
- */
-#define png_libpng_ver png_get_header_ver(NULL)
-
-/* This file is arranged in several sections:
- *
- * 1. Any configuration options that can be specified by for the application
- *    code when it is built.  (Build time configuration is in pnglibconf.h)
- * 2. Type definitions (base types are defined in pngconf.h), structure
- *    definitions.
- * 3. Exported library functions.
- *
- * The library source code has additional files (principally pngpriv.h) that
- * allow configuration of the library.
- */
-/* Section 1: run time configuration
- * See pnglibconf.h for build time configuration
- *
- * Run time configuration allows the application to choose between
- * implementations of certain arithmetic APIs.  The default is set
- * at build time and recorded in pnglibconf.h, but it is safe to
- * override these (and only these) settings.  Note that this won't
- * change what the library does, only application code, and the
- * settings can (and probably should) be made on a per-file basis
- * by setting the #defines before including png.h
- *
- * Use macros to read integers from PNG data or use the exported
- * functions?
- *   PNG_USE_READ_MACROS: use the macros (see below)  Note that
- *     the macros evaluate their argument multiple times.
- *   PNG_NO_USE_READ_MACROS: call the relevant library function.
- *
- * Use the alternative algorithm for compositing alpha samples that
- * does not use division?
- *   PNG_READ_COMPOSITE_NODIV_SUPPORTED: use the 'no division'
- *      algorithm.
- *   PNG_NO_READ_COMPOSITE_NODIV: use the 'division' algorithm.
- *
- * How to handle benign errors if PNG_ALLOW_BENIGN_ERRORS is
- * false?
- *   PNG_ALLOW_BENIGN_ERRORS: map calls to the benign error
- *      APIs to png_warning.
- * Otherwise the calls are mapped to png_error.
- */
-
-/* Section 2: type definitions, including structures and compile time
- * constants.
- * See pngconf.h for base types that vary by machine/system
- */
-
-/* This triggers a compiler error in png.c, if png.c and png.h
- * do not agree upon the version number.
- */
-typedef char* png_libpng_version_1_5_10;
-
-/* Three color definitions.  The order of the red, green, and blue, (and the
- * exact size) is not important, although the size of the fields need to
- * be png_byte or png_uint_16 (as defined below).
- */
-typedef struct png_color_struct
-{
-   png_byte red;
-   png_byte green;
-   png_byte blue;
-} png_color;
-typedef png_color FAR * png_colorp;
-typedef PNG_CONST png_color FAR * png_const_colorp;
-typedef png_color FAR * FAR * png_colorpp;
-
-typedef struct png_color_16_struct
-{
-   png_byte index;    /* used for palette files */
-   png_uint_16 red;   /* for use in red green blue files */
-   png_uint_16 green;
-   png_uint_16 blue;
-   png_uint_16 gray;  /* for use in grayscale files */
-} png_color_16;
-typedef png_color_16 FAR * png_color_16p;
-typedef PNG_CONST png_color_16 FAR * png_const_color_16p;
-typedef png_color_16 FAR * FAR * png_color_16pp;
-
-typedef struct png_color_8_struct
-{
-   png_byte red;   /* for use in red green blue files */
-   png_byte green;
-   png_byte blue;
-   png_byte gray;  /* for use in grayscale files */
-   png_byte alpha; /* for alpha channel files */
-} png_color_8;
-typedef png_color_8 FAR * png_color_8p;
-typedef PNG_CONST png_color_8 FAR * png_const_color_8p;
-typedef png_color_8 FAR * FAR * png_color_8pp;
-
-/*
- * The following two structures are used for the in-core representation
- * of sPLT chunks.
- */
-typedef struct png_sPLT_entry_struct
-{
-   png_uint_16 red;
-   png_uint_16 green;
-   png_uint_16 blue;
-   png_uint_16 alpha;
-   png_uint_16 frequency;
-} png_sPLT_entry;
-typedef png_sPLT_entry FAR * png_sPLT_entryp;
-typedef PNG_CONST png_sPLT_entry FAR * png_const_sPLT_entryp;
-typedef png_sPLT_entry FAR * FAR * png_sPLT_entrypp;
-
-/*  When the depth of the sPLT palette is 8 bits, the color and alpha samples
- *  occupy the LSB of their respective members, and the MSB of each member
- *  is zero-filled.  The frequency member always occupies the full 16 bits.
- */
-
-typedef struct png_sPLT_struct
-{
-   png_charp name;           /* palette name */
-   png_byte depth;           /* depth of palette samples */
-   png_sPLT_entryp entries;  /* palette entries */
-   png_int_32 nentries;      /* number of palette entries */
-} png_sPLT_t;
-typedef png_sPLT_t FAR * png_sPLT_tp;
-typedef PNG_CONST png_sPLT_t FAR * png_const_sPLT_tp;
-typedef png_sPLT_t FAR * FAR * png_sPLT_tpp;
-
-#ifdef PNG_TEXT_SUPPORTED
-/* png_text holds the contents of a text/ztxt/itxt chunk in a PNG file,
- * and whether that contents is compressed or not.  The "key" field
- * points to a regular zero-terminated C string.  The "text" fields can be a
- * regular C string, an empty string, or a NULL pointer.
- * However, the structure returned by png_get_text() will always contain
- * the "text" field as a regular zero-terminated C string (possibly
- * empty), never a NULL pointer, so it can be safely used in printf() and
- * other string-handling functions.  Note that the "itxt_length", "lang", and
- * "lang_key" members of the structure only exist when the library is built
- * with iTXt chunk support.  Prior to libpng-1.4.0 the library was built by
- * default without iTXt support. Also note that when iTXt *is* supported,
- * the "lang" and "lang_key" fields contain NULL pointers when the
- * "compression" field contains * PNG_TEXT_COMPRESSION_NONE or
- * PNG_TEXT_COMPRESSION_zTXt. Note that the "compression value" is not the
- * same as what appears in the PNG tEXt/zTXt/iTXt chunk's "compression flag"
- * which is always 0 or 1, or its "compression method" which is always 0.
- */
-typedef struct png_text_struct
-{
-   int  compression;       /* compression value:
-                             -1: tEXt, none
-                              0: zTXt, deflate
-                              1: iTXt, none
-                              2: iTXt, deflate  */
-   png_charp key;          /* keyword, 1-79 character description of "text" */
-   png_charp text;         /* comment, may be an empty string (ie "")
-                              or a NULL pointer */
-   png_size_t text_length; /* length of the text string */
-   png_size_t itxt_length; /* length of the itxt string */
-   png_charp lang;         /* language code, 0-79 characters
-                              or a NULL pointer */
-   png_charp lang_key;     /* keyword translated UTF-8 string, 0 or more
-                              chars or a NULL pointer */
-} png_text;
-typedef png_text FAR * png_textp;
-typedef PNG_CONST png_text FAR * png_const_textp;
-typedef png_text FAR * FAR * png_textpp;
-#endif
-
-/* Supported compression types for text in PNG files (tEXt, and zTXt).
- * The values of the PNG_TEXT_COMPRESSION_ defines should NOT be changed. */
-#define PNG_TEXT_COMPRESSION_NONE_WR -3
-#define PNG_TEXT_COMPRESSION_zTXt_WR -2
-#define PNG_TEXT_COMPRESSION_NONE    -1
-#define PNG_TEXT_COMPRESSION_zTXt     0
-#define PNG_ITXT_COMPRESSION_NONE     1
-#define PNG_ITXT_COMPRESSION_zTXt     2
-#define PNG_TEXT_COMPRESSION_LAST     3  /* Not a valid value */
-
-/* png_time is a way to hold the time in an machine independent way.
- * Two conversions are provided, both from time_t and struct tm.  There
- * is no portable way to convert to either of these structures, as far
- * as I know.  If you know of a portable way, send it to me.  As a side
- * note - PNG has always been Year 2000 compliant!
- */
-typedef struct png_time_struct
-{
-   png_uint_16 year; /* full year, as in, 1995 */
-   png_byte month;   /* month of year, 1 - 12 */
-   png_byte day;     /* day of month, 1 - 31 */
-   png_byte hour;    /* hour of day, 0 - 23 */
-   png_byte minute;  /* minute of hour, 0 - 59 */
-   png_byte second;  /* second of minute, 0 - 60 (for leap seconds) */
-} png_time;
-typedef png_time FAR * png_timep;
-typedef PNG_CONST png_time FAR * png_const_timep;
-typedef png_time FAR * FAR * png_timepp;
-
-#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) || \
-    defined(PNG_HANDLE_AS_UNKNOWN_SUPPORTED)
-/* png_unknown_chunk is a structure to hold queued chunks for which there is
- * no specific support.  The idea is that we can use this to queue
- * up private chunks for output even though the library doesn't actually
- * know about their semantics.
- */
-typedef struct png_unknown_chunk_t
-{
-    png_byte name[5];
-    png_byte *data;
-    png_size_t size;
-
-    /* libpng-using applications should NOT directly modify this byte. */
-    png_byte location; /* mode of operation at read time */
-}
-
-
-png_unknown_chunk;
-typedef png_unknown_chunk FAR * png_unknown_chunkp;
-typedef PNG_CONST png_unknown_chunk FAR * png_const_unknown_chunkp;
-typedef png_unknown_chunk FAR * FAR * png_unknown_chunkpp;
-#endif
-
-/* Values for the unknown chunk location byte */
-
-#define PNG_HAVE_IHDR  0x01
-#define PNG_HAVE_PLTE  0x02
-#define PNG_AFTER_IDAT 0x08
-
-/* The complete definition of png_info has, as of libpng-1.5.0,
- * been moved into a separate header file that is not accessible to
- * applications.  Read libpng-manual.txt or libpng.3 for more info.
- */
-typedef struct png_info_def png_info;
-typedef png_info FAR * png_infop;
-typedef PNG_CONST png_info FAR * png_const_infop;
-typedef png_info FAR * FAR * png_infopp;
-
-/* Maximum positive integer used in PNG is (2^31)-1 */
-#define PNG_UINT_31_MAX ((png_uint_32)0x7fffffffL)
-#define PNG_UINT_32_MAX ((png_uint_32)(-1))
-#define PNG_SIZE_MAX ((png_size_t)(-1))
-
-/* These are constants for fixed point values encoded in the
- * PNG specification manner (x100000)
- */
-#define PNG_FP_1    100000
-#define PNG_FP_HALF  50000
-#define PNG_FP_MAX  ((png_fixed_point)0x7fffffffL)
-#define PNG_FP_MIN  (-PNG_FP_MAX)
-
-/* These describe the color_type field in png_info. */
-/* color type masks */
-#define PNG_COLOR_MASK_PALETTE    1
-#define PNG_COLOR_MASK_COLOR      2
-#define PNG_COLOR_MASK_ALPHA      4
-
-/* color types.  Note that not all combinations are legal */
-#define PNG_COLOR_TYPE_GRAY 0
-#define PNG_COLOR_TYPE_PALETTE  (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
-#define PNG_COLOR_TYPE_RGB        (PNG_COLOR_MASK_COLOR)
-#define PNG_COLOR_TYPE_RGB_ALPHA  (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
-#define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
-/* aliases */
-#define PNG_COLOR_TYPE_RGBA  PNG_COLOR_TYPE_RGB_ALPHA
-#define PNG_COLOR_TYPE_GA  PNG_COLOR_TYPE_GRAY_ALPHA
-
-/* This is for compression type. PNG 1.0-1.2 only define the single type. */
-#define PNG_COMPRESSION_TYPE_BASE 0 /* Deflate method 8, 32K window */
-#define PNG_COMPRESSION_TYPE_DEFAULT PNG_COMPRESSION_TYPE_BASE
-
-/* This is for filter type. PNG 1.0-1.2 only define the single type. */
-#define PNG_FILTER_TYPE_BASE      0 /* Single row per-byte filtering */
-#define PNG_INTRAPIXEL_DIFFERENCING 64 /* Used only in MNG datastreams */
-#define PNG_FILTER_TYPE_DEFAULT   PNG_FILTER_TYPE_BASE
-
-/* These are for the interlacing type.  These values should NOT be changed. */
-#define PNG_INTERLACE_NONE        0 /* Non-interlaced image */
-#define PNG_INTERLACE_ADAM7       1 /* Adam7 interlacing */
-#define PNG_INTERLACE_LAST        2 /* Not a valid value */
-
-/* These are for the oFFs chunk.  These values should NOT be changed. */
-#define PNG_OFFSET_PIXEL          0 /* Offset in pixels */
-#define PNG_OFFSET_MICROMETER     1 /* Offset in micrometers (1/10^6 meter) */
-#define PNG_OFFSET_LAST           2 /* Not a valid value */
-
-/* These are for the pCAL chunk.  These values should NOT be changed. */
-#define PNG_EQUATION_LINEAR       0 /* Linear transformation */
-#define PNG_EQUATION_BASE_E       1 /* Exponential base e transform */
-#define PNG_EQUATION_ARBITRARY    2 /* Arbitrary base exponential transform */
-#define PNG_EQUATION_HYPERBOLIC   3 /* Hyperbolic sine transformation */
-#define PNG_EQUATION_LAST         4 /* Not a valid value */
-
-/* These are for the sCAL chunk.  These values should NOT be changed. */
-#define PNG_SCALE_UNKNOWN         0 /* unknown unit (image scale) */
-#define PNG_SCALE_METER           1 /* meters per pixel */
-#define PNG_SCALE_RADIAN          2 /* radians per pixel */
-#define PNG_SCALE_LAST            3 /* Not a valid value */
-
-/* These are for the pHYs chunk.  These values should NOT be changed. */
-#define PNG_RESOLUTION_UNKNOWN    0 /* pixels/unknown unit (aspect ratio) */
-#define PNG_RESOLUTION_METER      1 /* pixels/meter */
-#define PNG_RESOLUTION_LAST       2 /* Not a valid value */
-
-/* These are for the sRGB chunk.  These values should NOT be changed. */
-#define PNG_sRGB_INTENT_PERCEPTUAL 0
-#define PNG_sRGB_INTENT_RELATIVE   1
-#define PNG_sRGB_INTENT_SATURATION 2
-#define PNG_sRGB_INTENT_ABSOLUTE   3
-#define PNG_sRGB_INTENT_LAST       4 /* Not a valid value */
-
-/* This is for text chunks */
-#define PNG_KEYWORD_MAX_LENGTH     79
-
-/* Maximum number of entries in PLTE/sPLT/tRNS arrays */
-#define PNG_MAX_PALETTE_LENGTH    256
-
-/* These determine if an ancillary chunk's data has been successfully read
- * from the PNG header, or if the application has filled in the corresponding
- * data in the info_struct to be written into the output file.  The values
- * of the PNG_INFO_<chunk> defines should NOT be changed.
- */
-#define PNG_INFO_gAMA 0x0001
-#define PNG_INFO_sBIT 0x0002
-#define PNG_INFO_cHRM 0x0004
-#define PNG_INFO_PLTE 0x0008
-#define PNG_INFO_tRNS 0x0010
-#define PNG_INFO_bKGD 0x0020
-#define PNG_INFO_hIST 0x0040
-#define PNG_INFO_pHYs 0x0080
-#define PNG_INFO_oFFs 0x0100
-#define PNG_INFO_tIME 0x0200
-#define PNG_INFO_pCAL 0x0400
-#define PNG_INFO_sRGB 0x0800   /* GR-P, 0.96a */
-#define PNG_INFO_iCCP 0x1000   /* ESR, 1.0.6 */
-#define PNG_INFO_sPLT 0x2000   /* ESR, 1.0.6 */
-#define PNG_INFO_sCAL 0x4000   /* ESR, 1.0.6 */
-#define PNG_INFO_IDAT 0x8000   /* ESR, 1.0.6 */
-
-/* This is used for the transformation routines, as some of them
- * change these values for the row.  It also should enable using
- * the routines for other purposes.
- */
-typedef struct png_row_info_struct
-{
-   png_uint_32 width;    /* width of row */
-   png_size_t rowbytes;  /* number of bytes in row */
-   png_byte color_type;  /* color type of row */
-   png_byte bit_depth;   /* bit depth of row */
-   png_byte channels;    /* number of channels (1, 2, 3, or 4) */
-   png_byte pixel_depth; /* bits per pixel (depth * channels) */
-} png_row_info;
-
-typedef png_row_info FAR * png_row_infop;
-typedef png_row_info FAR * FAR * png_row_infopp;
-
-/* The complete definition of png_struct has, as of libpng-1.5.0,
- * been moved into a separate header file that is not accessible to
- * applications.  Read libpng-manual.txt or libpng.3 for more info.
- */
-typedef struct png_struct_def png_struct;
-typedef PNG_CONST png_struct FAR * png_const_structp;
-typedef png_struct FAR * png_structp;
-
-/* These are the function types for the I/O functions and for the functions
- * that allow the user to override the default I/O functions with his or her
- * own.  The png_error_ptr type should match that of user-supplied warning
- * and error functions, while the png_rw_ptr type should match that of the
- * user read/write data functions.  Note that the 'write' function must not
- * modify the buffer it is passed. The 'read' function, on the other hand, is
- * expected to return the read data in the buffer.
- */
-typedef PNG_CALLBACK(void, *png_error_ptr, (png_structp, png_const_charp));
-typedef PNG_CALLBACK(void, *png_rw_ptr, (png_structp, png_bytep, png_size_t));
-typedef PNG_CALLBACK(void, *png_flush_ptr, (png_structp));
-typedef PNG_CALLBACK(void, *png_read_status_ptr, (png_structp, png_uint_32,
-    int));
-typedef PNG_CALLBACK(void, *png_write_status_ptr, (png_structp, png_uint_32,
-    int));
-
-#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
-typedef PNG_CALLBACK(void, *png_progressive_info_ptr, (png_structp, png_infop));
-typedef PNG_CALLBACK(void, *png_progressive_end_ptr, (png_structp, png_infop));
-
-/* The following callback receives png_uint_32 row_number, int pass for the
- * png_bytep data of the row.  When transforming an interlaced image the
- * row number is the row number within the sub-image of the interlace pass, so
- * the value will increase to the height of the sub-image (not the full image)
- * then reset to 0 for the next pass.
- *
- * Use PNG_ROW_FROM_PASS_ROW(row, pass) and PNG_COL_FROM_PASS_COL(col, pass) to
- * find the output pixel (x,y) given an interlaced sub-image pixel
- * (row,col,pass).  (See below for these macros.)
- */
-typedef PNG_CALLBACK(void, *png_progressive_row_ptr, (png_structp, png_bytep,
-    png_uint_32, int));
-#endif
-
-#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
-    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
-typedef PNG_CALLBACK(void, *png_user_transform_ptr, (png_structp, png_row_infop,
-    png_bytep));
-#endif
-
-#ifdef PNG_USER_CHUNKS_SUPPORTED
-typedef PNG_CALLBACK(int, *png_user_chunk_ptr, (png_structp,
-    png_unknown_chunkp));
-#endif
-#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
-typedef PNG_CALLBACK(void, *png_unknown_chunk_ptr, (png_structp));
-#endif
-
-#ifdef PNG_SETJMP_SUPPORTED
-/* This must match the function definition in <setjmp.h>, and the application
- * must include this before png.h to obtain the definition of jmp_buf.  The
- * function is required to be PNG_NORETURN, but this is not checked.  If the
- * function does return the application will crash via an abort() or similar
- * system level call.
- *
- * If you get a warning here while building the library you may need to make
- * changes to ensure that pnglibconf.h records the calling convention used by
- * your compiler.  This may be very difficult - try using a different compiler
- * to build the library!
- */
-PNG_FUNCTION(void, (PNGCAPI *png_longjmp_ptr), PNGARG((jmp_buf, int)), typedef);
-#endif
-
-/* Transform masks for the high-level interface */
-#define PNG_TRANSFORM_IDENTITY       0x0000    /* read and write */
-#define PNG_TRANSFORM_STRIP_16       0x0001    /* read only */
-#define PNG_TRANSFORM_STRIP_ALPHA    0x0002    /* read only */
-#define PNG_TRANSFORM_PACKING        0x0004    /* read and write */
-#define PNG_TRANSFORM_PACKSWAP       0x0008    /* read and write */
-#define PNG_TRANSFORM_EXPAND         0x0010    /* read only */
-#define PNG_TRANSFORM_INVERT_MONO    0x0020    /* read and write */
-#define PNG_TRANSFORM_SHIFT          0x0040    /* read and write */
-#define PNG_TRANSFORM_BGR            0x0080    /* read and write */
-#define PNG_TRANSFORM_SWAP_ALPHA     0x0100    /* read and write */
-#define PNG_TRANSFORM_SWAP_ENDIAN    0x0200    /* read and write */
-#define PNG_TRANSFORM_INVERT_ALPHA   0x0400    /* read and write */
-#define PNG_TRANSFORM_STRIP_FILLER   0x0800    /* write only */
-/* Added to libpng-1.2.34 */
-#define PNG_TRANSFORM_STRIP_FILLER_BEFORE PNG_TRANSFORM_STRIP_FILLER
-#define PNG_TRANSFORM_STRIP_FILLER_AFTER 0x1000 /* write only */
-/* Added to libpng-1.4.0 */
-#define PNG_TRANSFORM_GRAY_TO_RGB   0x2000      /* read only */
-/* Added to libpng-1.5.4 */
-#define PNG_TRANSFORM_EXPAND_16     0x4000      /* read only */
-#define PNG_TRANSFORM_SCALE_16      0x8000      /* read only */
-
-/* Flags for MNG supported features */
-#define PNG_FLAG_MNG_EMPTY_PLTE     0x01
-#define PNG_FLAG_MNG_FILTER_64      0x04
-#define PNG_ALL_MNG_FEATURES        0x05
-
-/* NOTE: prior to 1.5 these functions had no 'API' style declaration,
- * this allowed the zlib default functions to be used on Windows
- * platforms.  In 1.5 the zlib default malloc (which just calls malloc and
- * ignores the first argument) should be completely compatible with the
- * following.
- */
-typedef PNG_CALLBACK(png_voidp, *png_malloc_ptr, (png_structp,
-    png_alloc_size_t));
-typedef PNG_CALLBACK(void, *png_free_ptr, (png_structp, png_voidp));
-
-typedef png_struct FAR * FAR * png_structpp;
-
-/* Section 3: exported functions
- * Here are the function definitions most commonly used.  This is not
- * the place to find out how to use libpng.  See libpng-manual.txt for the
- * full explanation, see example.c for the summary.  This just provides
- * a simple one line description of the use of each function.
- *
- * The PNG_EXPORT() and PNG_EXPORTA() macros used below are defined in
- * pngconf.h and in the *.dfn files in the scripts directory.
- *
- *   PNG_EXPORT(ordinal, type, name, (args));
- *
- *       ordinal:    ordinal that is used while building
- *                   *.def files. The ordinal value is only
- *                   relevant when preprocessing png.h with
- *                   the *.dfn files for building symbol table
- *                   entries, and are removed by pngconf.h.
- *       type:       return type of the function
- *       name:       function name
- *       args:       function arguments, with types
- *
- * When we wish to append attributes to a function prototype we use
- * the PNG_EXPORTA() macro instead.
- *
- *   PNG_EXPORTA(ordinal, type, name, (args), attributes);
- *
- *       ordinal, type, name, and args: same as in PNG_EXPORT().
- *       attributes: function attributes
- */
-
-/* Returns the version number of the library */
-PNG_EXPORT(1, png_uint_32, png_access_version_number, (void));
-
-/* Tell lib we have already handled the first <num_bytes> magic bytes.
- * Handling more than 8 bytes from the beginning of the file is an error.
- */
-PNG_EXPORT(2, void, png_set_sig_bytes, (png_structp png_ptr, int num_bytes));
-
-/* Check sig[start] through sig[start + num_to_check - 1] to see if it's a
- * PNG file.  Returns zero if the supplied bytes match the 8-byte PNG
- * signature, and non-zero otherwise.  Having num_to_check == 0 or
- * start > 7 will always fail (ie return non-zero).
- */
-PNG_EXPORT(3, int, png_sig_cmp, (png_const_bytep sig, png_size_t start,
-    png_size_t num_to_check));
-
-/* Simple signature checking function.  This is the same as calling
- * png_check_sig(sig, n) := !png_sig_cmp(sig, 0, n).
- */
-#define png_check_sig(sig, n) !png_sig_cmp((sig), 0, (n))
-
-/* Allocate and initialize png_ptr struct for reading, and any other memory. */
-PNG_EXPORTA(4, png_structp, png_create_read_struct,
-    (png_const_charp user_png_ver, png_voidp error_ptr,
-    png_error_ptr error_fn, png_error_ptr warn_fn),
-    PNG_ALLOCATED);
-
-/* Allocate and initialize png_ptr struct for writing, and any other memory */
-PNG_EXPORTA(5, png_structp, png_create_write_struct,
-    (png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn,
-    png_error_ptr warn_fn),
-    PNG_ALLOCATED);
-
-PNG_EXPORT(6, png_size_t, png_get_compression_buffer_size,
-    (png_const_structp png_ptr));
-
-PNG_EXPORT(7, void, png_set_compression_buffer_size, (png_structp png_ptr,
-    png_size_t size));
-
-/* Moved from pngconf.h in 1.4.0 and modified to ensure setjmp/longjmp
- * match up.
- */
-#ifdef PNG_SETJMP_SUPPORTED
-/* This function returns the jmp_buf built in to *png_ptr.  It must be
- * supplied with an appropriate 'longjmp' function to use on that jmp_buf
- * unless the default error function is overridden in which case NULL is
- * acceptable.  The size of the jmp_buf is checked against the actual size
- * allocated by the library - the call will return NULL on a mismatch
- * indicating an ABI mismatch.
- */
-PNG_EXPORT(8, jmp_buf*, png_set_longjmp_fn, (png_structp png_ptr,
-    png_longjmp_ptr longjmp_fn, size_t jmp_buf_size));
-#  define png_jmpbuf(png_ptr) \
-      (*png_set_longjmp_fn((png_ptr), longjmp, sizeof (jmp_buf)))
-#else
-#  define png_jmpbuf(png_ptr) \
-      (LIBPNG_WAS_COMPILED_WITH__PNG_NO_SETJMP)
-#endif
-/* This function should be used by libpng applications in place of
- * longjmp(png_ptr->jmpbuf, val).  If longjmp_fn() has been set, it
- * will use it; otherwise it will call PNG_ABORT().  This function was
- * added in libpng-1.5.0.
- */
-PNG_EXPORTA(9, void, png_longjmp, (png_structp png_ptr, int val),
-    PNG_NORETURN);
-
-#ifdef PNG_READ_SUPPORTED
-/* Reset the compression stream */
-PNG_EXPORT(10, int, png_reset_zstream, (png_structp png_ptr));
-#endif
-
-/* New functions added in libpng-1.0.2 (not enabled by default until 1.2.0) */
-#ifdef PNG_USER_MEM_SUPPORTED
-PNG_EXPORTA(11, png_structp, png_create_read_struct_2,
-    (png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn,
-    png_error_ptr warn_fn,
-    png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn),
-    PNG_ALLOCATED);
-PNG_EXPORTA(12, png_structp, png_create_write_struct_2,
-    (png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn,
-    png_error_ptr warn_fn,
-    png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn),
-    PNG_ALLOCATED);
-#endif
-
-/* Write the PNG file signature. */
-PNG_EXPORT(13, void, png_write_sig, (png_structp png_ptr));
-
-/* Write a PNG chunk - size, type, (optional) data, CRC. */
-PNG_EXPORT(14, void, png_write_chunk, (png_structp png_ptr, png_const_bytep
-    chunk_name, png_const_bytep data, png_size_t length));
-
-/* Write the start of a PNG chunk - length and chunk name. */
-PNG_EXPORT(15, void, png_write_chunk_start, (png_structp png_ptr,
-    png_const_bytep chunk_name, png_uint_32 length));
-
-/* Write the data of a PNG chunk started with png_write_chunk_start(). */
-PNG_EXPORT(16, void, png_write_chunk_data, (png_structp png_ptr,
-    png_const_bytep data, png_size_t length));
-
-/* Finish a chunk started with png_write_chunk_start() (includes CRC). */
-PNG_EXPORT(17, void, png_write_chunk_end, (png_structp png_ptr));
-
-/* Allocate and initialize the info structure */
-PNG_EXPORTA(18, png_infop, png_create_info_struct, (png_structp png_ptr),
-    PNG_ALLOCATED);
-
-PNG_EXPORT(19, void, png_info_init_3, (png_infopp info_ptr,
-    png_size_t png_info_struct_size));
-
-/* Writes all the PNG information before the image. */
-PNG_EXPORT(20, void, png_write_info_before_PLTE,
-    (png_structp png_ptr, png_infop info_ptr));
-PNG_EXPORT(21, void, png_write_info,
-    (png_structp png_ptr, png_infop info_ptr));
-
-#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
-/* Read the information before the actual image data. */
-PNG_EXPORT(22, void, png_read_info,
-    (png_structp png_ptr, png_infop info_ptr));
-#endif
-
-#ifdef PNG_TIME_RFC1123_SUPPORTED
-PNG_EXPORT(23, png_const_charp, png_convert_to_rfc1123,
-    (png_structp png_ptr,
-    png_const_timep ptime));
-#endif
-
-#ifdef PNG_CONVERT_tIME_SUPPORTED
-/* Convert from a struct tm to png_time */
-PNG_EXPORT(24, void, png_convert_from_struct_tm, (png_timep ptime,
-    PNG_CONST struct tm FAR * ttime));
-
-/* Convert from time_t to png_time.  Uses gmtime() */
-PNG_EXPORT(25, void, png_convert_from_time_t,
-    (png_timep ptime, time_t ttime));
-#endif /* PNG_CONVERT_tIME_SUPPORTED */
-
-#ifdef PNG_READ_EXPAND_SUPPORTED
-/* Expand data to 24-bit RGB, or 8-bit grayscale, with alpha if available. */
-PNG_EXPORT(26, void, png_set_expand, (png_structp png_ptr));
-PNG_EXPORT(27, void, png_set_expand_gray_1_2_4_to_8, (png_structp png_ptr));
-PNG_EXPORT(28, void, png_set_palette_to_rgb, (png_structp png_ptr));
-PNG_EXPORT(29, void, png_set_tRNS_to_alpha, (png_structp png_ptr));
-#endif
-
-#ifdef PNG_READ_EXPAND_16_SUPPORTED
-/* Expand to 16-bit channels, forces conversion of palette to RGB and expansion
- * of a tRNS chunk if present.
- */
-PNG_EXPORT(221, void, png_set_expand_16, (png_structp png_ptr));
-#endif
-
-#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
-/* Use blue, green, red order for pixels. */
-PNG_EXPORT(30, void, png_set_bgr, (png_structp png_ptr));
-#endif
-
-#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
-/* Expand the grayscale to 24-bit RGB if necessary. */
-PNG_EXPORT(31, void, png_set_gray_to_rgb, (png_structp png_ptr));
-#endif
-
-#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
-/* Reduce RGB to grayscale. */
-#define PNG_ERROR_ACTION_NONE  1
-#define PNG_ERROR_ACTION_WARN  2
-#define PNG_ERROR_ACTION_ERROR 3
-#define PNG_RGB_TO_GRAY_DEFAULT (-1)/*for red/green coefficients*/
-
-PNG_FP_EXPORT(32, void, png_set_rgb_to_gray, (png_structp png_ptr,
-    int error_action, double red, double green));
-PNG_FIXED_EXPORT(33, void, png_set_rgb_to_gray_fixed, (png_structp png_ptr,
-    int error_action, png_fixed_point red, png_fixed_point green));
-
-PNG_EXPORT(34, png_byte, png_get_rgb_to_gray_status, (png_const_structp
-    png_ptr));
-#endif
-
-#ifdef PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED
-PNG_EXPORT(35, void, png_build_grayscale_palette, (int bit_depth,
-    png_colorp palette));
-#endif
-
-#ifdef PNG_READ_ALPHA_MODE_SUPPORTED
-/* How the alpha channel is interpreted - this affects how the color channels of
- * a PNG file are returned when an alpha channel, or tRNS chunk in a palette
- * file, is present.
- *
- * This has no effect on the way pixels are written into a PNG output
- * datastream. The color samples in a PNG datastream are never premultiplied
- * with the alpha samples.
- *
- * The default is to return data according to the PNG specification: the alpha
- * channel is a linear measure of the contribution of the pixel to the
- * corresponding composited pixel.  The gamma encoded color channels must be
- * scaled according to the contribution and to do this it is necessary to undo
- * the encoding, scale the color values, perform the composition and reencode
- * the values.  This is the 'PNG' mode.
- *
- * The alternative is to 'associate' the alpha with the color information by
- * storing color channel values that have been scaled by the alpha.  The
- * advantage is that the color channels can be resampled (the image can be
- * scaled) in this form.  The disadvantage is that normal practice is to store
- * linear, not (gamma) encoded, values and this requires 16-bit channels for
- * still images rather than the 8-bit channels that are just about sufficient if
- * gamma encoding is used.  In addition all non-transparent pixel values,
- * including completely opaque ones, must be gamma encoded to produce the final
- * image.  This is the 'STANDARD', 'ASSOCIATED' or 'PREMULTIPLIED' mode (the
- * latter being the two common names for associated alpha color channels.)
- *
- * Since it is not necessary to perform arithmetic on opaque color values so
- * long as they are not to be resampled and are in the final color space it is
- * possible to optimize the handling of alpha by storing the opaque pixels in
- * the PNG format (adjusted for the output color space) while storing partially
- * opaque pixels in the standard, linear, format.  The accuracy required for
- * standard alpha composition is relatively low, because the pixels are
- * isolated, therefore typically the accuracy loss in storing 8-bit linear
- * values is acceptable.  (This is not true if the alpha channel is used to
- * simulate transparency over large areas - use 16 bits or the PNG mode in
- * this case!)  This is the 'OPTIMIZED' mode.  For this mode a pixel is
- * treated as opaque only if the alpha value is equal to the maximum value.
- *
- * The final choice is to gamma encode the alpha channel as well.  This is
- * broken because, in practice, no implementation that uses this choice
- * correctly undoes the encoding before handling alpha composition.  Use this
- * choice only if other serious errors in the software or hardware you use
- * mandate it; the typical serious error is for dark halos to appear around
- * opaque areas of the composited PNG image because of arithmetic overflow.
- *
- * The API function png_set_alpha_mode specifies which of these choices to use
- * with an enumerated 'mode' value and the gamma of the required output:
- */
-#define PNG_ALPHA_PNG           0 /* according to the PNG standard */
-#define PNG_ALPHA_STANDARD      1 /* according to Porter/Duff */
-#define PNG_ALPHA_ASSOCIATED    1 /* as above; this is the normal practice */
-#define PNG_ALPHA_PREMULTIPLIED 1 /* as above */
-#define PNG_ALPHA_OPTIMIZED     2 /* 'PNG' for opaque pixels, else 'STANDARD' */
-#define PNG_ALPHA_BROKEN        3 /* the alpha channel is gamma encoded */
-
-PNG_FP_EXPORT(227, void, png_set_alpha_mode, (png_structp png_ptr, int mode,
-    double output_gamma));
-PNG_FIXED_EXPORT(228, void, png_set_alpha_mode_fixed, (png_structp png_ptr,
-    int mode, png_fixed_point output_gamma));
-#endif
-
-#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_ALPHA_MODE_SUPPORTED)
-/* The output_gamma value is a screen gamma in libpng terminology: it expresses
- * how to decode the output values, not how they are encoded.  The values used
- * correspond to the normal numbers used to describe the overall gamma of a
- * computer display system; for example 2.2 for an sRGB conformant system.  The
- * values are scaled by 100000 in the _fixed version of the API (so 220000 for
- * sRGB.)
- *
- * The inverse of the value is always used to provide a default for the PNG file
- * encoding if it has no gAMA chunk and if png_set_gamma() has not been called
- * to override the PNG gamma information.
- *
- * When the ALPHA_OPTIMIZED mode is selected the output gamma is used to encode
- * opaque pixels however pixels with lower alpha values are not encoded,
- * regardless of the output gamma setting.
- *
- * When the standard Porter Duff handling is requested with mode 1 the output
- * encoding is set to be linear and the output_gamma value is only relevant
- * as a default for input data that has no gamma information.  The linear output
- * encoding will be overridden if png_set_gamma() is called - the results may be
- * highly unexpected!
- *
- * The following numbers are derived from the sRGB standard and the research
- * behind it.  sRGB is defined to be approximated by a PNG gAMA chunk value of
- * 0.45455 (1/2.2) for PNG.  The value implicitly includes any viewing
- * correction required to take account of any differences in the color
- * environment of the original scene and the intended display environment; the
- * value expresses how to *decode* the image for display, not how the original
- * data was *encoded*.
- *
- * sRGB provides a peg for the PNG standard by defining a viewing environment.
- * sRGB itself, and earlier TV standards, actually use a more complex transform
- * (a linear portion then a gamma 2.4 power law) than PNG can express.  (PNG is
- * limited to simple power laws.)  By saying that an image for direct display on
- * an sRGB conformant system should be stored with a gAMA chunk value of 45455
- * (11.3.3.2 and 11.3.3.5 of the ISO PNG specification) the PNG specification
- * makes it possible to derive values for other display systems and
- * environments.
- *
- * The Mac value is deduced from the sRGB based on an assumption that the actual
- * extra viewing correction used in early Mac display systems was implemented as
- * a power 1.45 lookup table.
- *
- * Any system where a programmable lookup table is used or where the behavior of
- * the final display device characteristics can be changed requires system
- * specific code to obtain the current characteristic.  However this can be
- * difficult and most PNG gamma correction only requires an approximate value.
- *
- * By default, if png_set_alpha_mode() is not called, libpng assumes that all
- * values are unencoded, linear, values and that the output device also has a
- * linear characteristic.  This is only very rarely correct - it is invariably
- * better to call png_set_alpha_mode() with PNG_DEFAULT_sRGB than rely on the
- * default if you don't know what the right answer is!
- *
- * The special value PNG_GAMMA_MAC_18 indicates an older Mac system (pre Mac OS
- * 10.6) which used a correction table to implement a somewhat lower gamma on an
- * otherwise sRGB system.
- *
- * Both these values are reserved (not simple gamma values) in order to allow
- * more precise correction internally in the future.
- *
- * NOTE: the following values can be passed to either the fixed or floating
- * point APIs, but the floating point API will also accept floating point
- * values.
- */
-#define PNG_DEFAULT_sRGB -1       /* sRGB gamma and color space */
-#define PNG_GAMMA_MAC_18 -2       /* Old Mac '1.8' gamma and color space */
-#define PNG_GAMMA_sRGB   220000   /* Television standards--matches sRGB gamma */
-#define PNG_GAMMA_LINEAR PNG_FP_1 /* Linear */
-#endif
-
-/* The following are examples of calls to png_set_alpha_mode to achieve the
- * required overall gamma correction and, where necessary, alpha
- * premultiplication.
- *
- * png_set_alpha_mode(pp, PNG_ALPHA_PNG, PNG_DEFAULT_sRGB);
- *    This is the default libpng handling of the alpha channel - it is not
- *    pre-multiplied into the color components.  In addition the call states
- *    that the output is for a sRGB system and causes all PNG files without gAMA
- *    chunks to be assumed to be encoded using sRGB.
- *
- * png_set_alpha_mode(pp, PNG_ALPHA_PNG, PNG_GAMMA_MAC);
- *    In this case the output is assumed to be something like an sRGB conformant
- *    display preceeded by a power-law lookup table of power 1.45.  This is how
- *    early Mac systems behaved.
- *
- * png_set_alpha_mode(pp, PNG_ALPHA_STANDARD, PNG_GAMMA_LINEAR);
- *    This is the classic Jim Blinn approach and will work in academic
- *    environments where everything is done by the book.  It has the shortcoming
- *    of assuming that input PNG data with no gamma information is linear - this
- *    is unlikely to be correct unless the PNG files where generated locally.
- *    Most of the time the output precision will be so low as to show
- *    significant banding in dark areas of the image.
- *
- * png_set_expand_16(pp);
- * png_set_alpha_mode(pp, PNG_ALPHA_STANDARD, PNG_DEFAULT_sRGB);
- *    This is a somewhat more realistic Jim Blinn inspired approach.  PNG files
- *    are assumed to have the sRGB encoding if not marked with a gamma value and
- *    the output is always 16 bits per component.  This permits accurate scaling
- *    and processing of the data.  If you know that your input PNG files were
- *    generated locally you might need to replace PNG_DEFAULT_sRGB with the
- *    correct value for your system.
- *
- * png_set_alpha_mode(pp, PNG_ALPHA_OPTIMIZED, PNG_DEFAULT_sRGB);
- *    If you just need to composite the PNG image onto an existing background
- *    and if you control the code that does this you can use the optimization
- *    setting.  In this case you just copy completely opaque pixels to the
- *    output.  For pixels that are not completely transparent (you just skip
- *    those) you do the composition math using png_composite or png_composite_16
- *    below then encode the resultant 8-bit or 16-bit values to match the output
- *    encoding.
- *
- * Other cases
- *    If neither the PNG nor the standard linear encoding work for you because
- *    of the software or hardware you use then you have a big problem.  The PNG
- *    case will probably result in halos around the image.  The linear encoding
- *    will probably result in a washed out, too bright, image (it's actually too
- *    contrasty.)  Try the ALPHA_OPTIMIZED mode above - this will probably
- *    substantially reduce the halos.  Alternatively try:
- *
- * png_set_alpha_mode(pp, PNG_ALPHA_BROKEN, PNG_DEFAULT_sRGB);
- *    This option will also reduce the halos, but there will be slight dark
- *    halos round the opaque parts of the image where the background is light.
- *    In the OPTIMIZED mode the halos will be light halos where the background
- *    is dark.  Take your pick - the halos are unavoidable unless you can get
- *    your hardware/software fixed!  (The OPTIMIZED approach is slightly
- *    faster.)
- *
- * When the default gamma of PNG files doesn't match the output gamma.
- *    If you have PNG files with no gamma information png_set_alpha_mode allows
- *    you to provide a default gamma, but it also sets the ouput gamma to the
- *    matching value.  If you know your PNG files have a gamma that doesn't
- *    match the output you can take advantage of the fact that
- *    png_set_alpha_mode always sets the output gamma but only sets the PNG
- *    default if it is not already set:
- *
- * png_set_alpha_mode(pp, PNG_ALPHA_PNG, PNG_DEFAULT_sRGB);
- * png_set_alpha_mode(pp, PNG_ALPHA_PNG, PNG_GAMMA_MAC);
- *    The first call sets both the default and the output gamma values, the
- *    second call overrides the output gamma without changing the default.  This
- *    is easier than achieving the same effect with png_set_gamma.  You must use
- *    PNG_ALPHA_PNG for the first call - internal checking in png_set_alpha will
- *    fire if more than one call to png_set_alpha_mode and png_set_background is
- *    made in the same read operation, however multiple calls with PNG_ALPHA_PNG
- *    are ignored.
- */
-
-#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
-PNG_EXPORT(36, void, png_set_strip_alpha, (png_structp png_ptr));
-#endif
-
-#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \
-    defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
-PNG_EXPORT(37, void, png_set_swap_alpha, (png_structp png_ptr));
-#endif
-
-#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \
-    defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
-PNG_EXPORT(38, void, png_set_invert_alpha, (png_structp png_ptr));
-#endif
-
-#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
-/* Add a filler byte to 8-bit Gray or 24-bit RGB images. */
-PNG_EXPORT(39, void, png_set_filler, (png_structp png_ptr, png_uint_32 filler,
-    int flags));
-/* The values of the PNG_FILLER_ defines should NOT be changed */
-#  define PNG_FILLER_BEFORE 0
-#  define PNG_FILLER_AFTER 1
-/* Add an alpha byte to 8-bit Gray or 24-bit RGB images. */
-PNG_EXPORT(40, void, png_set_add_alpha,
-    (png_structp png_ptr, png_uint_32 filler,
-    int flags));
-#endif /* PNG_READ_FILLER_SUPPORTED || PNG_WRITE_FILLER_SUPPORTED */
-
-#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
-/* Swap bytes in 16-bit depth files. */
-PNG_EXPORT(41, void, png_set_swap, (png_structp png_ptr));
-#endif
-
-#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
-/* Use 1 byte per pixel in 1, 2, or 4-bit depth files. */
-PNG_EXPORT(42, void, png_set_packing, (png_structp png_ptr));
-#endif
-
-#if defined(PNG_READ_PACKSWAP_SUPPORTED) || \
-    defined(PNG_WRITE_PACKSWAP_SUPPORTED)
-/* Swap packing order of pixels in bytes. */
-PNG_EXPORT(43, void, png_set_packswap, (png_structp png_ptr));
-#endif
-
-#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
-/* Converts files to legal bit depths. */
-PNG_EXPORT(44, void, png_set_shift, (png_structp png_ptr, png_const_color_8p
-    true_bits));
-#endif
-
-#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
-    defined(PNG_WRITE_INTERLACING_SUPPORTED)
-/* Have the code handle the interlacing.  Returns the number of passes.
- * MUST be called before png_read_update_info or png_start_read_image,
- * otherwise it will not have the desired effect.  Note that it is still
- * necessary to call png_read_row or png_read_rows png_get_image_height
- * times for each pass.
-*/
-PNG_EXPORT(45, int, png_set_interlace_handling, (png_structp png_ptr));
-#endif
-
-#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
-/* Invert monochrome files */
-PNG_EXPORT(46, void, png_set_invert_mono, (png_structp png_ptr));
-#endif
-
-#ifdef PNG_READ_BACKGROUND_SUPPORTED
-/* Handle alpha and tRNS by replacing with a background color.  Prior to
- * libpng-1.5.4 this API must not be called before the PNG file header has been
- * read.  Doing so will result in unexpected behavior and possible warnings or
- * errors if the PNG file contains a bKGD chunk.
- */
-PNG_FP_EXPORT(47, void, png_set_background, (png_structp png_ptr,
-    png_const_color_16p background_color, int background_gamma_code,
-    int need_expand, double background_gamma));
-PNG_FIXED_EXPORT(215, void, png_set_background_fixed, (png_structp png_ptr,
-    png_const_color_16p background_color, int background_gamma_code,
-    int need_expand, png_fixed_point background_gamma));
-#endif
-#ifdef PNG_READ_BACKGROUND_SUPPORTED
-#  define PNG_BACKGROUND_GAMMA_UNKNOWN 0
-#  define PNG_BACKGROUND_GAMMA_SCREEN  1
-#  define PNG_BACKGROUND_GAMMA_FILE    2
-#  define PNG_BACKGROUND_GAMMA_UNIQUE  3
-#endif
-
-#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
-/* Scale a 16-bit depth file down to 8-bit, accurately. */
-PNG_EXPORT(229, void, png_set_scale_16, (png_structp png_ptr));
-#endif
-
-#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
-#define PNG_READ_16_TO_8 SUPPORTED /* Name prior to 1.5.4 */
-/* Strip the second byte of information from a 16-bit depth file. */
-PNG_EXPORT(48, void, png_set_strip_16, (png_structp png_ptr));
-#endif
-
-#ifdef PNG_READ_QUANTIZE_SUPPORTED
-/* Turn on quantizing, and reduce the palette to the number of colors
- * available.
- */
-PNG_EXPORT(49, void, png_set_quantize,
-    (png_structp png_ptr, png_colorp palette,
-    int num_palette, int maximum_colors, png_const_uint_16p histogram,
-    int full_quantize));
-#endif
-
-#ifdef PNG_READ_GAMMA_SUPPORTED
-/* The threshold on gamma processing is configurable but hard-wired into the
- * library.  The following is the floating point variant.
- */
-#define PNG_GAMMA_THRESHOLD (PNG_GAMMA_THRESHOLD_FIXED*.00001)
-
-/* Handle gamma correction. Screen_gamma=(display_exponent).
- * NOTE: this API simply sets the screen and file gamma values. It will
- * therefore override the value for gamma in a PNG file if it is called after
- * the file header has been read - use with care  - call before reading the PNG
- * file for best results!
- *
- * These routines accept the same gamma values as png_set_alpha_mode (described
- * above).  The PNG_GAMMA_ defines and PNG_DEFAULT_sRGB can be passed to either
- * API (floating point or fixed.)  Notice, however, that the 'file_gamma' value
- * is the inverse of a 'screen gamma' value.
- */
-PNG_FP_EXPORT(50, void, png_set_gamma,
-    (png_structp png_ptr, double screen_gamma,
-    double override_file_gamma));
-PNG_FIXED_EXPORT(208, void, png_set_gamma_fixed, (png_structp png_ptr,
-    png_fixed_point screen_gamma, png_fixed_point override_file_gamma));
-#endif
-
-#ifdef PNG_WRITE_FLUSH_SUPPORTED
-/* Set how many lines between output flushes - 0 for no flushing */
-PNG_EXPORT(51, void, png_set_flush, (png_structp png_ptr, int nrows));
-/* Flush the current PNG output buffer */
-PNG_EXPORT(52, void, png_write_flush, (png_structp png_ptr));
-#endif
-
-/* Optional update palette with requested transformations */
-PNG_EXPORT(53, void, png_start_read_image, (png_structp png_ptr));
-
-/* Optional call to update the users info structure */
-PNG_EXPORT(54, void, png_read_update_info,
-    (png_structp png_ptr, png_infop info_ptr));
-
-#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
-/* Read one or more rows of image data. */
-PNG_EXPORT(55, void, png_read_rows, (png_structp png_ptr, png_bytepp row,
-    png_bytepp display_row, png_uint_32 num_rows));
-#endif
-
-#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
-/* Read a row of data. */
-PNG_EXPORT(56, void, png_read_row, (png_structp png_ptr, png_bytep row,
-    png_bytep display_row));
-#endif
-
-#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
-/* Read the whole image into memory at once. */
-PNG_EXPORT(57, void, png_read_image, (png_structp png_ptr, png_bytepp image));
-#endif
-
-/* Write a row of image data */
-PNG_EXPORT(58, void, png_write_row,
-    (png_structp png_ptr, png_const_bytep row));
-
-/* Write a few rows of image data: (*row) is not written; however, the type
- * is declared as writeable to maintain compatibility with previous versions
- * of libpng and to allow the 'display_row' array from read_rows to be passed
- * unchanged to write_rows.
- */
-PNG_EXPORT(59, void, png_write_rows, (png_structp png_ptr, png_bytepp row,
-    png_uint_32 num_rows));
-
-/* Write the image data */
-PNG_EXPORT(60, void, png_write_image,
-    (png_structp png_ptr, png_bytepp image));
-
-/* Write the end of the PNG file. */
-PNG_EXPORT(61, void, png_write_end,
-    (png_structp png_ptr, png_infop info_ptr));
-
-#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
-/* Read the end of the PNG file. */
-PNG_EXPORT(62, void, png_read_end, (png_structp png_ptr, png_infop info_ptr));
-#endif
-
-/* Free any memory associated with the png_info_struct */
-PNG_EXPORT(63, void, png_destroy_info_struct, (png_structp png_ptr,
-    png_infopp info_ptr_ptr));
-
-/* Free any memory associated with the png_struct and the png_info_structs */
-PNG_EXPORT(64, void, png_destroy_read_struct, (png_structpp png_ptr_ptr,
-    png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr));
-
-/* Free any memory associated with the png_struct and the png_info_structs */
-PNG_EXPORT(65, void, png_destroy_write_struct, (png_structpp png_ptr_ptr,
-    png_infopp info_ptr_ptr));
-
-/* Set the libpng method of handling chunk CRC errors */
-PNG_EXPORT(66, void, png_set_crc_action,
-    (png_structp png_ptr, int crit_action, int ancil_action));
-
-/* Values for png_set_crc_action() say how to handle CRC errors in
- * ancillary and critical chunks, and whether to use the data contained
- * therein.  Note that it is impossible to "discard" data in a critical
- * chunk.  For versions prior to 0.90, the action was always error/quit,
- * whereas in version 0.90 and later, the action for CRC errors in ancillary
- * chunks is warn/discard.  These values should NOT be changed.
- *
- *      value                       action:critical     action:ancillary
- */
-#define PNG_CRC_DEFAULT       0  /* error/quit          warn/discard data */
-#define PNG_CRC_ERROR_QUIT    1  /* error/quit          error/quit        */
-#define PNG_CRC_WARN_DISCARD  2  /* (INVALID)           warn/discard data */
-#define PNG_CRC_WARN_USE      3  /* warn/use data       warn/use data     */
-#define PNG_CRC_QUIET_USE     4  /* quiet/use data      quiet/use data    */
-#define PNG_CRC_NO_CHANGE     5  /* use current value   use current value */
-
-/* These functions give the user control over the scan-line filtering in
- * libpng and the compression methods used by zlib.  These functions are
- * mainly useful for testing, as the defaults should work with most users.
- * Those users who are tight on memory or want faster performance at the
- * expense of compression can modify them.  See the compression library
- * header file (zlib.h) for an explination of the compression functions.
- */
-
-/* Set the filtering method(s) used by libpng.  Currently, the only valid
- * value for "method" is 0.
- */
-PNG_EXPORT(67, void, png_set_filter,
-    (png_structp png_ptr, int method, int filters));
-
-/* Flags for png_set_filter() to say which filters to use.  The flags
- * are chosen so that they don't conflict with real filter types
- * below, in case they are supplied instead of the #defined constants.
- * These values should NOT be changed.
- */
-#define PNG_NO_FILTERS     0x00
-#define PNG_FILTER_NONE    0x08
-#define PNG_FILTER_SUB     0x10
-#define PNG_FILTER_UP      0x20
-#define PNG_FILTER_AVG     0x40
-#define PNG_FILTER_PAETH   0x80
-#define PNG_ALL_FILTERS (PNG_FILTER_NONE | PNG_FILTER_SUB | PNG_FILTER_UP | \
-                         PNG_FILTER_AVG | PNG_FILTER_PAETH)
-
-/* Filter values (not flags) - used in pngwrite.c, pngwutil.c for now.
- * These defines should NOT be changed.
- */
-#define PNG_FILTER_VALUE_NONE  0
-#define PNG_FILTER_VALUE_SUB   1
-#define PNG_FILTER_VALUE_UP    2
-#define PNG_FILTER_VALUE_AVG   3
-#define PNG_FILTER_VALUE_PAETH 4
-#define PNG_FILTER_VALUE_LAST  5
-
-#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* EXPERIMENTAL */
-/* The "heuristic_method" is given by one of the PNG_FILTER_HEURISTIC_
- * defines, either the default (minimum-sum-of-absolute-differences), or
- * the experimental method (weighted-minimum-sum-of-absolute-differences).
- *
- * Weights are factors >= 1.0, indicating how important it is to keep the
- * filter type consistent between rows.  Larger numbers mean the current
- * filter is that many times as likely to be the same as the "num_weights"
- * previous filters.  This is cumulative for each previous row with a weight.
- * There needs to be "num_weights" values in "filter_weights", or it can be
- * NULL if the weights aren't being specified.  Weights have no influence on
- * the selection of the first row filter.  Well chosen weights can (in theory)
- * improve the compression for a given image.
- *
- * Costs are factors >= 1.0 indicating the relative decoding costs of a
- * filter type.  Higher costs indicate more decoding expense, and are
- * therefore less likely to be selected over a filter with lower computational
- * costs.  There needs to be a value in "filter_costs" for each valid filter
- * type (given by PNG_FILTER_VALUE_LAST), or it can be NULL if you aren't
- * setting the costs.  Costs try to improve the speed of decompression without
- * unduly increasing the compressed image size.
- *
- * A negative weight or cost indicates the default value is to be used, and
- * values in the range [0.0, 1.0) indicate the value is to remain unchanged.
- * The default values for both weights and costs are currently 1.0, but may
- * change if good general weighting/cost heuristics can be found.  If both
- * the weights and costs are set to 1.0, this degenerates the WEIGHTED method
- * to the UNWEIGHTED method, but with added encoding time/computation.
- */
-PNG_FP_EXPORT(68, void, png_set_filter_heuristics, (png_structp png_ptr,
-    int heuristic_method, int num_weights, png_const_doublep filter_weights,
-    png_const_doublep filter_costs));
-PNG_FIXED_EXPORT(209, void, png_set_filter_heuristics_fixed,
-    (png_structp png_ptr,
-    int heuristic_method, int num_weights, png_const_fixed_point_p
-    filter_weights, png_const_fixed_point_p filter_costs));
-#endif /*  PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
-
-/* Heuristic used for row filter selection.  These defines should NOT be
- * changed.
- */
-#define PNG_FILTER_HEURISTIC_DEFAULT    0  /* Currently "UNWEIGHTED" */
-#define PNG_FILTER_HEURISTIC_UNWEIGHTED 1  /* Used by libpng < 0.95 */
-#define PNG_FILTER_HEURISTIC_WEIGHTED   2  /* Experimental feature */
-#define PNG_FILTER_HEURISTIC_LAST       3  /* Not a valid value */
-
-#ifdef PNG_WRITE_SUPPORTED
-/* Set the library compression level.  Currently, valid values range from
- * 0 - 9, corresponding directly to the zlib compression levels 0 - 9
- * (0 - no compression, 9 - "maximal" compression).  Note that tests have
- * shown that zlib compression levels 3-6 usually perform as well as level 9
- * for PNG images, and do considerably fewer caclulations.  In the future,
- * these values may not correspond directly to the zlib compression levels.
- */
-PNG_EXPORT(69, void, png_set_compression_level,
-    (png_structp png_ptr, int level));
-
-PNG_EXPORT(70, void, png_set_compression_mem_level, (png_structp png_ptr,
-    int mem_level));
-
-PNG_EXPORT(71, void, png_set_compression_strategy, (png_structp png_ptr,
-    int strategy));
-
-/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
- * smaller value of window_bits if it can do so safely.
- */
-PNG_EXPORT(72, void, png_set_compression_window_bits, (png_structp png_ptr,
-    int window_bits));
-
-PNG_EXPORT(73, void, png_set_compression_method, (png_structp png_ptr,
-    int method));
-#endif
-
-#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
-/* Also set zlib parameters for compressing non-IDAT chunks */
-PNG_EXPORT(222, void, png_set_text_compression_level,
-    (png_structp png_ptr, int level));
-
-PNG_EXPORT(223, void, png_set_text_compression_mem_level, (png_structp png_ptr,
-    int mem_level));
-
-PNG_EXPORT(224, void, png_set_text_compression_strategy, (png_structp png_ptr,
-    int strategy));
-
-/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
- * smaller value of window_bits if it can do so safely.
- */
-PNG_EXPORT(225, void, png_set_text_compression_window_bits, (png_structp
-    png_ptr, int window_bits));
-
-PNG_EXPORT(226, void, png_set_text_compression_method, (png_structp png_ptr,
-    int method));
-#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */
-
-/* These next functions are called for input/output, memory, and error
- * handling.  They are in the file pngrio.c, pngwio.c, and pngerror.c,
- * and call standard C I/O routines such as fread(), fwrite(), and
- * fprintf().  These functions can be made to use other I/O routines
- * at run time for those applications that need to handle I/O in a
- * different manner by calling png_set_???_fn().  See libpng-manual.txt for
- * more information.
- */
-
-#ifdef PNG_STDIO_SUPPORTED
-/* Initialize the input/output for the PNG file to the default functions. */
-PNG_EXPORT(74, void, png_init_io, (png_structp png_ptr, png_FILE_p fp));
-#endif
-
-/* Replace the (error and abort), and warning functions with user
- * supplied functions.  If no messages are to be printed you must still
- * write and use replacement functions. The replacement error_fn should
- * still do a longjmp to the last setjmp location if you are using this
- * method of error handling.  If error_fn or warning_fn is NULL, the
- * default function will be used.
- */
-
-PNG_EXPORT(75, void, png_set_error_fn,
-    (png_structp png_ptr, png_voidp error_ptr,
-    png_error_ptr error_fn, png_error_ptr warning_fn));
-
-/* Return the user pointer associated with the error functions */
-PNG_EXPORT(76, png_voidp, png_get_error_ptr, (png_const_structp png_ptr));
-
-/* Replace the default data output functions with a user supplied one(s).
- * If buffered output is not used, then output_flush_fn can be set to NULL.
- * If PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile time
- * output_flush_fn will be ignored (and thus can be NULL).
- * It is probably a mistake to use NULL for output_flush_fn if
- * write_data_fn is not also NULL unless you have built libpng with
- * PNG_WRITE_FLUSH_SUPPORTED undefined, because in this case libpng's
- * default flush function, which uses the standard *FILE structure, will
- * be used.
- */
-PNG_EXPORT(77, void, png_set_write_fn, (png_structp png_ptr, png_voidp io_ptr,
-    png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn));
-
-/* Replace the default data input function with a user supplied one. */
-PNG_EXPORT(78, void, png_set_read_fn, (png_structp png_ptr, png_voidp io_ptr,
-    png_rw_ptr read_data_fn));
-
-/* Return the user pointer associated with the I/O functions */
-PNG_EXPORT(79, png_voidp, png_get_io_ptr, (png_structp png_ptr));
-
-PNG_EXPORT(80, void, png_set_read_status_fn, (png_structp png_ptr,
-    png_read_status_ptr read_row_fn));
-
-PNG_EXPORT(81, void, png_set_write_status_fn, (png_structp png_ptr,
-    png_write_status_ptr write_row_fn));
-
-#ifdef PNG_USER_MEM_SUPPORTED
-/* Replace the default memory allocation functions with user supplied one(s). */
-PNG_EXPORT(82, void, png_set_mem_fn, (png_structp png_ptr, png_voidp mem_ptr,
-    png_malloc_ptr malloc_fn, png_free_ptr free_fn));
-/* Return the user pointer associated with the memory functions */
-PNG_EXPORT(83, png_voidp, png_get_mem_ptr, (png_const_structp png_ptr));
-#endif
-
-#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
-PNG_EXPORT(84, void, png_set_read_user_transform_fn, (png_structp png_ptr,
-    png_user_transform_ptr read_user_transform_fn));
-#endif
-
-#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
-PNG_EXPORT(85, void, png_set_write_user_transform_fn, (png_structp png_ptr,
-    png_user_transform_ptr write_user_transform_fn));
-#endif
-
-#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
-PNG_EXPORT(86, void, png_set_user_transform_info, (png_structp png_ptr,
-    png_voidp user_transform_ptr, int user_transform_depth,
-    int user_transform_channels));
-/* Return the user pointer associated with the user transform functions */
-PNG_EXPORT(87, png_voidp, png_get_user_transform_ptr,
-    (png_const_structp png_ptr));
-#endif
-
-#ifdef PNG_USER_TRANSFORM_INFO_SUPPORTED
-/* Return information about the row currently being processed.  Note that these
- * APIs do not fail but will return unexpected results if called outside a user
- * transform callback.  Also note that when transforming an interlaced image the
- * row number is the row number within the sub-image of the interlace pass, so
- * the value will increase to the height of the sub-image (not the full image)
- * then reset to 0 for the next pass.
- *
- * Use PNG_ROW_FROM_PASS_ROW(row, pass) and PNG_COL_FROM_PASS_COL(col, pass) to
- * find the output pixel (x,y) given an interlaced sub-image pixel
- * (row,col,pass).  (See below for these macros.)
- */
-PNG_EXPORT(217, png_uint_32, png_get_current_row_number, (png_const_structp));
-PNG_EXPORT(218, png_byte, png_get_current_pass_number, (png_const_structp));
-#endif
-
-#ifdef PNG_USER_CHUNKS_SUPPORTED
-PNG_EXPORT(88, void, png_set_read_user_chunk_fn, (png_structp png_ptr,
-    png_voidp user_chunk_ptr, png_user_chunk_ptr read_user_chunk_fn));
-PNG_EXPORT(89, png_voidp, png_get_user_chunk_ptr, (png_const_structp png_ptr));
-#endif
-
-#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
-/* Sets the function callbacks for the push reader, and a pointer to a
- * user-defined structure available to the callback functions.
- */
-PNG_EXPORT(90, void, png_set_progressive_read_fn, (png_structp png_ptr,
-    png_voidp progressive_ptr, png_progressive_info_ptr info_fn,
-    png_progressive_row_ptr row_fn, png_progressive_end_ptr end_fn));
-
-/* Returns the user pointer associated with the push read functions */
-PNG_EXPORT(91, png_voidp, png_get_progressive_ptr, (png_const_structp png_ptr));
-
-/* Function to be called when data becomes available */
-PNG_EXPORT(92, void, png_process_data,
-    (png_structp png_ptr, png_infop info_ptr,
-    png_bytep buffer, png_size_t buffer_size));
-
-/* A function which may be called *only* within png_process_data to stop the
- * processing of any more data.  The function returns the number of bytes
- * remaining, excluding any that libpng has cached internally.  A subsequent
- * call to png_process_data must supply these bytes again.  If the argument
- * 'save' is set to true the routine will first save all the pending data and
- * will always return 0.
- */
-PNG_EXPORT(219, png_size_t, png_process_data_pause, (png_structp, int save));
-
-/* A function which may be called *only* outside (after) a call to
- * png_process_data.  It returns the number of bytes of data to skip in the
- * input.  Normally it will return 0, but if it returns a non-zero value the
- * application must skip than number of bytes of input data and pass the
- * following data to the next call to png_process_data.
- */
-PNG_EXPORT(220, png_uint_32, png_process_data_skip, (png_structp));
-
-#ifdef PNG_READ_INTERLACING_SUPPORTED
-/* Function that combines rows.  'new_row' is a flag that should come from
- * the callback and be non-NULL if anything needs to be done; the library
- * stores its own version of the new data internally and ignores the passed
- * in value.
- */
-PNG_EXPORT(93, void, png_progressive_combine_row, (png_structp png_ptr,
-    png_bytep old_row, png_const_bytep new_row));
-#endif /* PNG_READ_INTERLACING_SUPPORTED */
-#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
-
-PNG_EXPORTA(94, png_voidp, png_malloc,
-    (png_structp png_ptr, png_alloc_size_t size),
-    PNG_ALLOCATED);
-/* Added at libpng version 1.4.0 */
-PNG_EXPORTA(95, png_voidp, png_calloc,
-    (png_structp png_ptr, png_alloc_size_t size),
-    PNG_ALLOCATED);
-
-/* Added at libpng version 1.2.4 */
-PNG_EXPORTA(96, png_voidp, png_malloc_warn, (png_structp png_ptr,
-    png_alloc_size_t size), PNG_ALLOCATED);
-
-/* Frees a pointer allocated by png_malloc() */
-PNG_EXPORT(97, void, png_free, (png_structp png_ptr, png_voidp ptr));
-
-/* Free data that was allocated internally */
-PNG_EXPORT(98, void, png_free_data,
-    (png_structp png_ptr, png_infop info_ptr, png_uint_32 free_me, int num));
-
-/* Reassign responsibility for freeing existing data, whether allocated
- * by libpng or by the application */
-PNG_EXPORT(99, void, png_data_freer,
-    (png_structp png_ptr, png_infop info_ptr, int freer, png_uint_32 mask));
-
-/* Assignments for png_data_freer */
-#define PNG_DESTROY_WILL_FREE_DATA 1
-#define PNG_SET_WILL_FREE_DATA 1
-#define PNG_USER_WILL_FREE_DATA 2
-/* Flags for png_ptr->free_me and info_ptr->free_me */
-#define PNG_FREE_HIST 0x0008
-#define PNG_FREE_ICCP 0x0010
-#define PNG_FREE_SPLT 0x0020
-#define PNG_FREE_ROWS 0x0040
-#define PNG_FREE_PCAL 0x0080
-#define PNG_FREE_SCAL 0x0100
-#define PNG_FREE_UNKN 0x0200
-#define PNG_FREE_LIST 0x0400
-#define PNG_FREE_PLTE 0x1000
-#define PNG_FREE_TRNS 0x2000
-#define PNG_FREE_TEXT 0x4000
-#define PNG_FREE_ALL  0x7fff
-#define PNG_FREE_MUL  0x4220 /* PNG_FREE_SPLT|PNG_FREE_TEXT|PNG_FREE_UNKN */
-
-#ifdef PNG_USER_MEM_SUPPORTED
-PNG_EXPORTA(100, png_voidp, png_malloc_default, (png_structp png_ptr,
-    png_alloc_size_t size), PNG_ALLOCATED);
-PNG_EXPORT(101, void, png_free_default, (png_structp png_ptr, png_voidp ptr));
-#endif
-
-#ifdef PNG_ERROR_TEXT_SUPPORTED
-/* Fatal error in PNG image of libpng - can't continue */
-PNG_EXPORTA(102, void, png_error,
-    (png_structp png_ptr, png_const_charp error_message),
-    PNG_NORETURN);
-
-/* The same, but the chunk name is prepended to the error string. */
-PNG_EXPORTA(103, void, png_chunk_error, (png_structp png_ptr,
-    png_const_charp error_message), PNG_NORETURN);
-
-#else
-/* Fatal error in PNG image of libpng - can't continue */
-PNG_EXPORTA(104, void, png_err, (png_structp png_ptr), PNG_NORETURN);
-#endif
-
-#ifdef PNG_WARNINGS_SUPPORTED
-/* Non-fatal error in libpng.  Can continue, but may have a problem. */
-PNG_EXPORT(105, void, png_warning, (png_structp png_ptr,
-    png_const_charp warning_message));
-
-/* Non-fatal error in libpng, chunk name is prepended to message. */
-PNG_EXPORT(106, void, png_chunk_warning, (png_structp png_ptr,
-    png_const_charp warning_message));
-#endif
-
-#ifdef PNG_BENIGN_ERRORS_SUPPORTED
-/* Benign error in libpng.  Can continue, but may have a problem.
- * User can choose whether to handle as a fatal error or as a warning. */
-#  undef png_benign_error
-PNG_EXPORT(107, void, png_benign_error, (png_structp png_ptr,
-    png_const_charp warning_message));
-
-/* Same, chunk name is prepended to message. */
-#  undef png_chunk_benign_error
-PNG_EXPORT(108, void, png_chunk_benign_error, (png_structp png_ptr,
-    png_const_charp warning_message));
-
-PNG_EXPORT(109, void, png_set_benign_errors,
-    (png_structp png_ptr, int allowed));
-#else
-#  ifdef PNG_ALLOW_BENIGN_ERRORS
-#    define png_benign_error png_warning
-#    define png_chunk_benign_error png_chunk_warning
-#  else
-#    define png_benign_error png_error
-#    define png_chunk_benign_error png_chunk_error
-#  endif
-#endif
-
-/* The png_set_<chunk> functions are for storing values in the png_info_struct.
- * Similarly, the png_get_<chunk> calls are used to read values from the
- * png_info_struct, either storing the parameters in the passed variables, or
- * setting pointers into the png_info_struct where the data is stored.  The
- * png_get_<chunk> functions return a non-zero value if the data was available
- * in info_ptr, or return zero and do not change any of the parameters if the
- * data was not available.
- *
- * These functions should be used instead of directly accessing png_info
- * to avoid problems with future changes in the size and internal layout of
- * png_info_struct.
- */
-/* Returns "flag" if chunk data is valid in info_ptr. */
-PNG_EXPORT(110, png_uint_32, png_get_valid,
-    (png_const_structp png_ptr, png_const_infop info_ptr,
-    png_uint_32 flag));
-
-/* Returns number of bytes needed to hold a transformed row. */
-PNG_EXPORT(111, png_size_t, png_get_rowbytes, (png_const_structp png_ptr,
-    png_const_infop info_ptr));
-
-#ifdef PNG_INFO_IMAGE_SUPPORTED
-/* Returns row_pointers, which is an array of pointers to scanlines that was
- * returned from png_read_png().
- */
-PNG_EXPORT(112, png_bytepp, png_get_rows,
-    (png_const_structp png_ptr, png_const_infop info_ptr));
-/* Set row_pointers, which is an array of pointers to scanlines for use
- * by png_write_png().
- */
-PNG_EXPORT(113, void, png_set_rows, (png_structp png_ptr,
-    png_infop info_ptr, png_bytepp row_pointers));
-#endif
-
-/* Returns number of color channels in image. */
-PNG_EXPORT(114, png_byte, png_get_channels,
-    (png_const_structp png_ptr, png_const_infop info_ptr));
-
-#ifdef PNG_EASY_ACCESS_SUPPORTED
-/* Returns image width in pixels. */
-PNG_EXPORT(115, png_uint_32, png_get_image_width, (png_const_structp png_ptr,
-    png_const_infop info_ptr));
-
-/* Returns image height in pixels. */
-PNG_EXPORT(116, png_uint_32, png_get_image_height, (png_const_structp png_ptr,
-    png_const_infop info_ptr));
-
-/* Returns image bit_depth. */
-PNG_EXPORT(117, png_byte, png_get_bit_depth,
-    (png_const_structp png_ptr, png_const_infop info_ptr));
-
-/* Returns image color_type. */
-PNG_EXPORT(118, png_byte, png_get_color_type, (png_const_structp png_ptr,
-    png_const_infop info_ptr));
-
-/* Returns image filter_type. */
-PNG_EXPORT(119, png_byte, png_get_filter_type, (png_const_structp png_ptr,
-    png_const_infop info_ptr));
-
-/* Returns image interlace_type. */
-PNG_EXPORT(120, png_byte, png_get_interlace_type, (png_const_structp png_ptr,
-    png_const_infop info_ptr));
-
-/* Returns image compression_type. */
-PNG_EXPORT(121, png_byte, png_get_compression_type, (png_const_structp png_ptr,
-    png_const_infop info_ptr));
-
-/* Returns image resolution in pixels per meter, from pHYs chunk data. */
-PNG_EXPORT(122, png_uint_32, png_get_pixels_per_meter,
-    (png_const_structp png_ptr, png_const_infop info_ptr));
-PNG_EXPORT(123, png_uint_32, png_get_x_pixels_per_meter,
-    (png_const_structp png_ptr, png_const_infop info_ptr));
-PNG_EXPORT(124, png_uint_32, png_get_y_pixels_per_meter,
-    (png_const_structp png_ptr, png_const_infop info_ptr));
-
-/* Returns pixel aspect ratio, computed from pHYs chunk data.  */
-PNG_FP_EXPORT(125, float, png_get_pixel_aspect_ratio,
-    (png_const_structp png_ptr, png_const_infop info_ptr));
-PNG_FIXED_EXPORT(210, png_fixed_point, png_get_pixel_aspect_ratio_fixed,
-    (png_const_structp png_ptr, png_const_infop info_ptr));
-
-/* Returns image x, y offset in pixels or microns, from oFFs chunk data. */
-PNG_EXPORT(126, png_int_32, png_get_x_offset_pixels,
-    (png_const_structp png_ptr, png_const_infop info_ptr));
-PNG_EXPORT(127, png_int_32, png_get_y_offset_pixels,
-    (png_const_structp png_ptr, png_const_infop info_ptr));
-PNG_EXPORT(128, png_int_32, png_get_x_offset_microns,
-    (png_const_structp png_ptr, png_const_infop info_ptr));
-PNG_EXPORT(129, png_int_32, png_get_y_offset_microns,
-    (png_const_structp png_ptr, png_const_infop info_ptr));
-
-#endif /* PNG_EASY_ACCESS_SUPPORTED */
-
-/* Returns pointer to signature string read from PNG header */
-PNG_EXPORT(130, png_const_bytep, png_get_signature,
-    (png_const_structp png_ptr, png_infop info_ptr));
-
-#ifdef PNG_bKGD_SUPPORTED
-PNG_EXPORT(131, png_uint_32, png_get_bKGD,
-    (png_const_structp png_ptr, png_infop info_ptr,
-    png_color_16p *background));
-#endif
-
-#ifdef PNG_bKGD_SUPPORTED
-PNG_EXPORT(132, void, png_set_bKGD, (png_structp png_ptr, png_infop info_ptr,
-    png_const_color_16p background));
-#endif
-
-#ifdef PNG_cHRM_SUPPORTED
-PNG_FP_EXPORT(133, png_uint_32, png_get_cHRM, (png_const_structp png_ptr,
-   png_const_infop info_ptr, double *white_x, double *white_y, double *red_x,
-    double *red_y, double *green_x, double *green_y, double *blue_x,
-    double *blue_y));
-PNG_FP_EXPORT(230, png_uint_32, png_get_cHRM_XYZ, (png_structp png_ptr,
-    png_const_infop info_ptr, double *red_X, double *red_Y, double *red_Z,
-    double *green_X, double *green_Y, double *green_Z, double *blue_X,
-    double *blue_Y, double *blue_Z));
-#ifdef PNG_FIXED_POINT_SUPPORTED /* Otherwise not implemented */
-PNG_FIXED_EXPORT(134, png_uint_32, png_get_cHRM_fixed,
-    (png_const_structp png_ptr,
-    png_const_infop info_ptr, png_fixed_point *int_white_x,
-    png_fixed_point *int_white_y, png_fixed_point *int_red_x,
-    png_fixed_point *int_red_y, png_fixed_point *int_green_x,
-    png_fixed_point *int_green_y, png_fixed_point *int_blue_x,
-    png_fixed_point *int_blue_y));
-#endif
-PNG_FIXED_EXPORT(231, png_uint_32, png_get_cHRM_XYZ_fixed,
-    (png_structp png_ptr, png_const_infop info_ptr,
-    png_fixed_point *int_red_X, png_fixed_point *int_red_Y,
-    png_fixed_point *int_red_Z, png_fixed_point *int_green_X,
-    png_fixed_point *int_green_Y, png_fixed_point *int_green_Z,
-    png_fixed_point *int_blue_X, png_fixed_point *int_blue_Y,
-    png_fixed_point *int_blue_Z));
-#endif
-
-#ifdef PNG_cHRM_SUPPORTED
-PNG_FP_EXPORT(135, void, png_set_cHRM,
-    (png_structp png_ptr, png_infop info_ptr,
-    double white_x, double white_y, double red_x, double red_y, double green_x,
-    double green_y, double blue_x, double blue_y));
-PNG_FP_EXPORT(232, void, png_set_cHRM_XYZ, (png_structp png_ptr,
-    png_infop info_ptr, double red_X, double red_Y, double red_Z,
-    double green_X, double green_Y, double green_Z, double blue_X,
-    double blue_Y, double blue_Z));
-PNG_FIXED_EXPORT(136, void, png_set_cHRM_fixed, (png_structp png_ptr,
-    png_infop info_ptr, png_fixed_point int_white_x,
-    png_fixed_point int_white_y, png_fixed_point int_red_x,
-    png_fixed_point int_red_y, png_fixed_point int_green_x,
-    png_fixed_point int_green_y, png_fixed_point int_blue_x,
-    png_fixed_point int_blue_y));
-PNG_FIXED_EXPORT(233, void, png_set_cHRM_XYZ_fixed, (png_structp png_ptr,
-    png_infop info_ptr, png_fixed_point int_red_X, png_fixed_point int_red_Y,
-    png_fixed_point int_red_Z, png_fixed_point int_green_X,
-    png_fixed_point int_green_Y, png_fixed_point int_green_Z,
-    png_fixed_point int_blue_X, png_fixed_point int_blue_Y,
-    png_fixed_point int_blue_Z));
-#endif
-
-#ifdef PNG_gAMA_SUPPORTED
-PNG_FP_EXPORT(137, png_uint_32, png_get_gAMA,
-    (png_const_structp png_ptr, png_const_infop info_ptr,
-    double *file_gamma));
-PNG_FIXED_EXPORT(138, png_uint_32, png_get_gAMA_fixed,
-    (png_const_structp png_ptr, png_const_infop info_ptr,
-    png_fixed_point *int_file_gamma));
-#endif
-
-#ifdef PNG_gAMA_SUPPORTED
-PNG_FP_EXPORT(139, void, png_set_gAMA, (png_structp png_ptr,
-    png_infop info_ptr, double file_gamma));
-PNG_FIXED_EXPORT(140, void, png_set_gAMA_fixed, (png_structp png_ptr,
-    png_infop info_ptr, png_fixed_point int_file_gamma));
-#endif
-
-#ifdef PNG_hIST_SUPPORTED
-PNG_EXPORT(141, png_uint_32, png_get_hIST,
-    (png_const_structp png_ptr, png_const_infop info_ptr,
-    png_uint_16p *hist));
-#endif
-
-#ifdef PNG_hIST_SUPPORTED
-PNG_EXPORT(142, void, png_set_hIST, (png_structp png_ptr,
-    png_infop info_ptr, png_const_uint_16p hist));
-#endif
-
-PNG_EXPORT(143, png_uint_32, png_get_IHDR,
-    (png_structp png_ptr, png_infop info_ptr,
-    png_uint_32 *width, png_uint_32 *height, int *bit_depth, int *color_type,
-    int *interlace_method, int *compression_method, int *filter_method));
-
-PNG_EXPORT(144, void, png_set_IHDR,
-    (png_structp png_ptr, png_infop info_ptr,
-    png_uint_32 width, png_uint_32 height, int bit_depth, int color_type,
-    int interlace_method, int compression_method, int filter_method));
-
-#ifdef PNG_oFFs_SUPPORTED
-PNG_EXPORT(145, png_uint_32, png_get_oFFs,
-    (png_const_structp png_ptr, png_const_infop info_ptr,
-    png_int_32 *offset_x, png_int_32 *offset_y, int *unit_type));
-#endif
-
-#ifdef PNG_oFFs_SUPPORTED
-PNG_EXPORT(146, void, png_set_oFFs,
-    (png_structp png_ptr, png_infop info_ptr,
-    png_int_32 offset_x, png_int_32 offset_y, int unit_type));
-#endif
-
-#ifdef PNG_pCAL_SUPPORTED
-PNG_EXPORT(147, png_uint_32, png_get_pCAL,
-    (png_const_structp png_ptr, png_const_infop info_ptr,
-    png_charp *purpose, png_int_32 *X0, png_int_32 *X1, int *type,
-    int *nparams,
-    png_charp *units, png_charpp *params));
-#endif
-
-#ifdef PNG_pCAL_SUPPORTED
-PNG_EXPORT(148, void, png_set_pCAL, (png_structp png_ptr,
-    png_infop info_ptr,
-    png_const_charp purpose, png_int_32 X0, png_int_32 X1, int type,
-    int nparams, png_const_charp units, png_charpp params));
-#endif
-
-#ifdef PNG_pHYs_SUPPORTED
-PNG_EXPORT(149, png_uint_32, png_get_pHYs,
-    (png_const_structp png_ptr, png_const_infop info_ptr,
-    png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type));
-#endif
-
-#ifdef PNG_pHYs_SUPPORTED
-PNG_EXPORT(150, void, png_set_pHYs,
-    (png_structp png_ptr, png_infop info_ptr,
-    png_uint_32 res_x, png_uint_32 res_y, int unit_type));
-#endif
-
-PNG_EXPORT(151, png_uint_32, png_get_PLTE,
-    (png_const_structp png_ptr, png_const_infop info_ptr,
-    png_colorp *palette, int *num_palette));
-
-PNG_EXPORT(152, void, png_set_PLTE,
-    (png_structp png_ptr, png_infop info_ptr,
-    png_const_colorp palette, int num_palette));
-
-#ifdef PNG_sBIT_SUPPORTED
-PNG_EXPORT(153, png_uint_32, png_get_sBIT,
-    (png_const_structp png_ptr, png_infop info_ptr,
-    png_color_8p *sig_bit));
-#endif
-
-#ifdef PNG_sBIT_SUPPORTED
-PNG_EXPORT(154, void, png_set_sBIT,
-    (png_structp png_ptr, png_infop info_ptr, png_const_color_8p sig_bit));
-#endif
-
-#ifdef PNG_sRGB_SUPPORTED
-PNG_EXPORT(155, png_uint_32, png_get_sRGB, (png_const_structp png_ptr,
-    png_const_infop info_ptr, int *file_srgb_intent));
-#endif
-
-#ifdef PNG_sRGB_SUPPORTED
-PNG_EXPORT(156, void, png_set_sRGB,
-    (png_structp png_ptr, png_infop info_ptr, int srgb_intent));
-PNG_EXPORT(157, void, png_set_sRGB_gAMA_and_cHRM, (png_structp png_ptr,
-    png_infop info_ptr, int srgb_intent));
-#endif
-
-#ifdef PNG_iCCP_SUPPORTED
-PNG_EXPORT(158, png_uint_32, png_get_iCCP,
-    (png_const_structp png_ptr, png_const_infop info_ptr,
-    png_charpp name, int *compression_type, png_bytepp profile,
-    png_uint_32 *proflen));
-#endif
-
-#ifdef PNG_iCCP_SUPPORTED
-PNG_EXPORT(159, void, png_set_iCCP,
-    (png_structp png_ptr, png_infop info_ptr,
-    png_const_charp name, int compression_type, png_const_bytep profile,
-    png_uint_32 proflen));
-#endif
-
-#ifdef PNG_sPLT_SUPPORTED
-PNG_EXPORT(160, png_uint_32, png_get_sPLT,
-    (png_const_structp png_ptr, png_const_infop info_ptr,
-    png_sPLT_tpp entries));
-#endif
-
-#ifdef PNG_sPLT_SUPPORTED
-PNG_EXPORT(161, void, png_set_sPLT,
-    (png_structp png_ptr, png_infop info_ptr,
-    png_const_sPLT_tp entries, int nentries));
-#endif
-
-#ifdef PNG_TEXT_SUPPORTED
-/* png_get_text also returns the number of text chunks in *num_text */
-PNG_EXPORT(162, png_uint_32, png_get_text,
-    (png_const_structp png_ptr, png_const_infop info_ptr,
-    png_textp *text_ptr, int *num_text));
-#endif
-
-/* Note while png_set_text() will accept a structure whose text,
- * language, and  translated keywords are NULL pointers, the structure
- * returned by png_get_text will always contain regular
- * zero-terminated C strings.  They might be empty strings but
- * they will never be NULL pointers.
- */
-
-#ifdef PNG_TEXT_SUPPORTED
-PNG_EXPORT(163, void, png_set_text,
-    (png_structp png_ptr, png_infop info_ptr,
-    png_const_textp text_ptr, int num_text));
-#endif
-
-#ifdef PNG_tIME_SUPPORTED
-PNG_EXPORT(164, png_uint_32, png_get_tIME,
-    (png_const_structp png_ptr, png_infop info_ptr, png_timep *mod_time));
-#endif
-
-#ifdef PNG_tIME_SUPPORTED
-PNG_EXPORT(165, void, png_set_tIME,
-    (png_structp png_ptr, png_infop info_ptr, png_const_timep mod_time));
-#endif
-
-#ifdef PNG_tRNS_SUPPORTED
-PNG_EXPORT(166, png_uint_32, png_get_tRNS,
-    (png_const_structp png_ptr, png_infop info_ptr,
-    png_bytep *trans_alpha, int *num_trans, png_color_16p *trans_color));
-#endif
-
-#ifdef PNG_tRNS_SUPPORTED
-PNG_EXPORT(167, void, png_set_tRNS,
-    (png_structp png_ptr, png_infop info_ptr,
-    png_const_bytep trans_alpha, int num_trans,
-    png_const_color_16p trans_color));
-#endif
-
-#ifdef PNG_sCAL_SUPPORTED
-PNG_FP_EXPORT(168, png_uint_32, png_get_sCAL,
-    (png_const_structp png_ptr, png_const_infop info_ptr,
-    int *unit, double *width, double *height));
-#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
-/* NOTE: this API is currently implemented using floating point arithmetic,
- * consequently it can only be used on systems with floating point support.
- * In any case the range of values supported by png_fixed_point is small and it
- * is highly recommended that png_get_sCAL_s be used instead.
- */
-PNG_FIXED_EXPORT(214, png_uint_32, png_get_sCAL_fixed,
-    (png_structp png_ptr, png_const_infop info_ptr, int *unit,
-    png_fixed_point *width,
-    png_fixed_point *height));
-#endif
-PNG_EXPORT(169, png_uint_32, png_get_sCAL_s,
-    (png_const_structp png_ptr, png_const_infop info_ptr,
-    int *unit, png_charpp swidth, png_charpp sheight));
-
-PNG_FP_EXPORT(170, void, png_set_sCAL,
-    (png_structp png_ptr, png_infop info_ptr,
-    int unit, double width, double height));
-PNG_FIXED_EXPORT(213, void, png_set_sCAL_fixed, (png_structp png_ptr,
-   png_infop info_ptr, int unit, png_fixed_point width,
-   png_fixed_point height));
-PNG_EXPORT(171, void, png_set_sCAL_s,
-    (png_structp png_ptr, png_infop info_ptr,
-    int unit, png_const_charp swidth, png_const_charp sheight));
-#endif /* PNG_sCAL_SUPPORTED */
-
-#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
-/* Provide a list of chunks and how they are to be handled, if the built-in
-   handling or default unknown chunk handling is not desired.  Any chunks not
-   listed will be handled in the default manner.  The IHDR and IEND chunks
-   must not be listed.  Because this turns off the default handling for chunks
-   that would otherwise be recognized the behavior of libpng transformations may
-   well become incorrect!
-      keep = 0: PNG_HANDLE_CHUNK_AS_DEFAULT: follow default behavior
-           = 1: PNG_HANDLE_CHUNK_NEVER:      do not keep
-           = 2: PNG_HANDLE_CHUNK_IF_SAFE:    keep only if safe-to-copy
-           = 3: PNG_HANDLE_CHUNK_ALWAYS:     keep even if unsafe-to-copy
-*/
-PNG_EXPORT(172, void, png_set_keep_unknown_chunks,
-    (png_structp png_ptr, int keep,
-    png_const_bytep chunk_list, int num_chunks));
-
-/* The handling code is returned; the result is therefore true (non-zero) if
- * special handling is required, false for the default handling.
- */
-PNG_EXPORT(173, int, png_handle_as_unknown, (png_structp png_ptr,
-    png_const_bytep chunk_name));
-#endif
-#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
-PNG_EXPORT(174, void, png_set_unknown_chunks, (png_structp png_ptr,
-    png_infop info_ptr, png_const_unknown_chunkp unknowns,
-    int num_unknowns));
-PNG_EXPORT(175, void, png_set_unknown_chunk_location,
-    (png_structp png_ptr, png_infop info_ptr, int chunk, int location));
-PNG_EXPORT(176, int, png_get_unknown_chunks, (png_const_structp png_ptr,
-    png_const_infop info_ptr, png_unknown_chunkpp entries));
-#endif
-
-/* Png_free_data() will turn off the "valid" flag for anything it frees.
- * If you need to turn it off for a chunk that your application has freed,
- * you can use png_set_invalid(png_ptr, info_ptr, PNG_INFO_CHNK);
- */
-PNG_EXPORT(177, void, png_set_invalid,
-    (png_structp png_ptr, png_infop info_ptr, int mask));
-
-#ifdef PNG_INFO_IMAGE_SUPPORTED
-/* The "params" pointer is currently not used and is for future expansion. */
-PNG_EXPORT(178, void, png_read_png, (png_structp png_ptr, png_infop info_ptr,
-    int transforms, png_voidp params));
-PNG_EXPORT(179, void, png_write_png, (png_structp png_ptr, png_infop info_ptr,
-    int transforms, png_voidp params));
-#endif
-
-PNG_EXPORT(180, png_const_charp, png_get_copyright,
-    (png_const_structp png_ptr));
-PNG_EXPORT(181, png_const_charp, png_get_header_ver,
-    (png_const_structp png_ptr));
-PNG_EXPORT(182, png_const_charp, png_get_header_version,
-    (png_const_structp png_ptr));
-PNG_EXPORT(183, png_const_charp, png_get_libpng_ver,
-    (png_const_structp png_ptr));
-
-#ifdef PNG_MNG_FEATURES_SUPPORTED
-PNG_EXPORT(184, png_uint_32, png_permit_mng_features, (png_structp png_ptr,
-    png_uint_32 mng_features_permitted));
-#endif
-
-/* For use in png_set_keep_unknown, added to version 1.2.6 */
-#define PNG_HANDLE_CHUNK_AS_DEFAULT   0
-#define PNG_HANDLE_CHUNK_NEVER        1
-#define PNG_HANDLE_CHUNK_IF_SAFE      2
-#define PNG_HANDLE_CHUNK_ALWAYS       3
-
-/* Strip the prepended error numbers ("#nnn ") from error and warning
- * messages before passing them to the error or warning handler.
- */
-#ifdef PNG_ERROR_NUMBERS_SUPPORTED
-PNG_EXPORT(185, void, png_set_strip_error_numbers,
-    (png_structp png_ptr,
-    png_uint_32 strip_mode));
-#endif
-
-/* Added in libpng-1.2.6 */
-#ifdef PNG_SET_USER_LIMITS_SUPPORTED
-PNG_EXPORT(186, void, png_set_user_limits, (png_structp png_ptr,
-    png_uint_32 user_width_max, png_uint_32 user_height_max));
-PNG_EXPORT(187, png_uint_32, png_get_user_width_max,
-    (png_const_structp png_ptr));
-PNG_EXPORT(188, png_uint_32, png_get_user_height_max,
-    (png_const_structp png_ptr));
-/* Added in libpng-1.4.0 */
-PNG_EXPORT(189, void, png_set_chunk_cache_max, (png_structp png_ptr,
-    png_uint_32 user_chunk_cache_max));
-PNG_EXPORT(190, png_uint_32, png_get_chunk_cache_max,
-    (png_const_structp png_ptr));
-/* Added in libpng-1.4.1 */
-PNG_EXPORT(191, void, png_set_chunk_malloc_max, (png_structp png_ptr,
-    png_alloc_size_t user_chunk_cache_max));
-PNG_EXPORT(192, png_alloc_size_t, png_get_chunk_malloc_max,
-    (png_const_structp png_ptr));
-#endif
-
-#if defined(PNG_INCH_CONVERSIONS_SUPPORTED)
-PNG_EXPORT(193, png_uint_32, png_get_pixels_per_inch,
-    (png_const_structp png_ptr, png_const_infop info_ptr));
-
-PNG_EXPORT(194, png_uint_32, png_get_x_pixels_per_inch,
-    (png_const_structp png_ptr, png_const_infop info_ptr));
-
-PNG_EXPORT(195, png_uint_32, png_get_y_pixels_per_inch,
-    (png_const_structp png_ptr, png_const_infop info_ptr));
-
-PNG_FP_EXPORT(196, float, png_get_x_offset_inches,
-    (png_const_structp png_ptr, png_const_infop info_ptr));
-#ifdef PNG_FIXED_POINT_SUPPORTED /* otherwise not implemented. */
-PNG_FIXED_EXPORT(211, png_fixed_point, png_get_x_offset_inches_fixed,
-    (png_structp png_ptr, png_const_infop info_ptr));
-#endif
-
-PNG_FP_EXPORT(197, float, png_get_y_offset_inches, (png_const_structp png_ptr,
-    png_const_infop info_ptr));
-#ifdef PNG_FIXED_POINT_SUPPORTED /* otherwise not implemented. */
-PNG_FIXED_EXPORT(212, png_fixed_point, png_get_y_offset_inches_fixed,
-    (png_structp png_ptr, png_const_infop info_ptr));
-#endif
-
-#  ifdef PNG_pHYs_SUPPORTED
-PNG_EXPORT(198, png_uint_32, png_get_pHYs_dpi, (png_const_structp png_ptr,
-    png_const_infop info_ptr, png_uint_32 *res_x, png_uint_32 *res_y,
-    int *unit_type));
-#  endif /* PNG_pHYs_SUPPORTED */
-#endif  /* PNG_INCH_CONVERSIONS_SUPPORTED */
-
-/* Added in libpng-1.4.0 */
-#ifdef PNG_IO_STATE_SUPPORTED
-PNG_EXPORT(199, png_uint_32, png_get_io_state, (png_structp png_ptr));
-
-PNG_EXPORTA(200, png_const_bytep, png_get_io_chunk_name,
-    (png_structp png_ptr), PNG_DEPRECATED);
-PNG_EXPORT(216, png_uint_32, png_get_io_chunk_type,
-    (png_const_structp png_ptr));
-
-/* The flags returned by png_get_io_state() are the following: */
-#  define PNG_IO_NONE        0x0000   /* no I/O at this moment */
-#  define PNG_IO_READING     0x0001   /* currently reading */
-#  define PNG_IO_WRITING     0x0002   /* currently writing */
-#  define PNG_IO_SIGNATURE   0x0010   /* currently at the file signature */
-#  define PNG_IO_CHUNK_HDR   0x0020   /* currently at the chunk header */
-#  define PNG_IO_CHUNK_DATA  0x0040   /* currently at the chunk data */
-#  define PNG_IO_CHUNK_CRC   0x0080   /* currently at the chunk crc */
-#  define PNG_IO_MASK_OP     0x000f   /* current operation: reading/writing */
-#  define PNG_IO_MASK_LOC    0x00f0   /* current location: sig/hdr/data/crc */
-#endif /* ?PNG_IO_STATE_SUPPORTED */
-
-/* Interlace support.  The following macros are always defined so that if
- * libpng interlace handling is turned off the macros may be used to handle
- * interlaced images within the application.
- */
-#define PNG_INTERLACE_ADAM7_PASSES 7
-
-/* Two macros to return the first row and first column of the original,
- * full, image which appears in a given pass.  'pass' is in the range 0
- * to 6 and the result is in the range 0 to 7.
- */
-#define PNG_PASS_START_ROW(pass) (((1&~(pass))<<(3-((pass)>>1)))&7)
-#define PNG_PASS_START_COL(pass) (((1& (pass))<<(3-(((pass)+1)>>1)))&7)
-
-/* A macro to return the offset between pixels in the output row for a pair of
- * pixels in the input - effectively the inverse of the 'COL_SHIFT' macro that
- * follows.  Note that ROW_OFFSET is the offset from one row to the next whereas
- * COL_OFFSET is from one column to the next, within a row.
- */
-#define PNG_PASS_ROW_OFFSET(pass) ((pass)>2?(8>>(((pass)-1)>>1)):8)
-#define PNG_PASS_COL_OFFSET(pass) (1<<((7-(pass))>>1))
-
-/* Two macros to help evaluate the number of rows or columns in each
- * pass.  This is expressed as a shift - effectively log2 of the number or
- * rows or columns in each 8x8 tile of the original image.
- */
-#define PNG_PASS_ROW_SHIFT(pass) ((pass)>2?(8-(pass))>>1:3)
-#define PNG_PASS_COL_SHIFT(pass) ((pass)>1?(7-(pass))>>1:3)
-
-/* Hence two macros to determine the number of rows or columns in a given
- * pass of an image given its height or width.  In fact these macros may
- * return non-zero even though the sub-image is empty, because the other
- * dimension may be empty for a small image.
- */
-#define PNG_PASS_ROWS(height, pass) (((height)+(((1<<PNG_PASS_ROW_SHIFT(pass))\
-   -1)-PNG_PASS_START_ROW(pass)))>>PNG_PASS_ROW_SHIFT(pass))
-#define PNG_PASS_COLS(width, pass) (((width)+(((1<<PNG_PASS_COL_SHIFT(pass))\
-   -1)-PNG_PASS_START_COL(pass)))>>PNG_PASS_COL_SHIFT(pass))
-
-/* For the reader row callbacks (both progressive and sequential) it is
- * necessary to find the row in the output image given a row in an interlaced
- * image, so two more macros:
- */
-#define PNG_ROW_FROM_PASS_ROW(yIn, pass) \
-   (((yIn)<<PNG_PASS_ROW_SHIFT(pass))+PNG_PASS_START_ROW(pass))
-#define PNG_COL_FROM_PASS_COL(xIn, pass) \
-   (((xIn)<<PNG_PASS_COL_SHIFT(pass))+PNG_PASS_START_COL(pass))
-
-/* Two macros which return a boolean (0 or 1) saying whether the given row
- * or column is in a particular pass.  These use a common utility macro that
- * returns a mask for a given pass - the offset 'off' selects the row or
- * column version.  The mask has the appropriate bit set for each column in
- * the tile.
- */
-#define PNG_PASS_MASK(pass,off) ( \
-   ((0x110145AF>>(((7-(off))-(pass))<<2)) & 0xF) | \
-   ((0x01145AF0>>(((7-(off))-(pass))<<2)) & 0xF0))
-
-#define PNG_ROW_IN_INTERLACE_PASS(y, pass) \
-   ((PNG_PASS_MASK(pass,0) >> ((y)&7)) & 1)
-#define PNG_COL_IN_INTERLACE_PASS(x, pass) \
-   ((PNG_PASS_MASK(pass,1) >> ((x)&7)) & 1)
-
-#ifdef PNG_READ_COMPOSITE_NODIV_SUPPORTED
-/* With these routines we avoid an integer divide, which will be slower on
- * most machines.  However, it does take more operations than the corresponding
- * divide method, so it may be slower on a few RISC systems.  There are two
- * shifts (by 8 or 16 bits) and an addition, versus a single integer divide.
- *
- * Note that the rounding factors are NOT supposed to be the same!  128 and
- * 32768 are correct for the NODIV code; 127 and 32767 are correct for the
- * standard method.
- *
- * [Optimized code by Greg Roelofs and Mark Adler...blame us for bugs. :-) ]
- */
-
- /* fg and bg should be in `gamma 1.0' space; alpha is the opacity */
-
-#  define png_composite(composite, fg, alpha, bg)         \
-     { png_uint_16 temp = (png_uint_16)((png_uint_16)(fg) \
-           * (png_uint_16)(alpha)                         \
-           + (png_uint_16)(bg)*(png_uint_16)(255          \
-           - (png_uint_16)(alpha)) + 128);                \
-       (composite) = (png_byte)((temp + (temp >> 8)) >> 8); }
-
-#  define png_composite_16(composite, fg, alpha, bg)       \
-     { png_uint_32 temp = (png_uint_32)((png_uint_32)(fg)  \
-           * (png_uint_32)(alpha)                          \
-           + (png_uint_32)(bg)*(65535                      \
-           - (png_uint_32)(alpha)) + 32768);               \
-       (composite) = (png_uint_16)((temp + (temp >> 16)) >> 16); }
-
-#else  /* Standard method using integer division */
-
-#  define png_composite(composite, fg, alpha, bg)                          \
-     (composite) = (png_byte)(((png_uint_16)(fg) * (png_uint_16)(alpha) +  \
-     (png_uint_16)(bg) * (png_uint_16)(255 - (png_uint_16)(alpha)) +       \
-     127) / 255)
-
-#  define png_composite_16(composite, fg, alpha, bg)                         \
-     (composite) = (png_uint_16)(((png_uint_32)(fg) * (png_uint_32)(alpha) + \
-     (png_uint_32)(bg)*(png_uint_32)(65535 - (png_uint_32)(alpha)) +         \
-     32767) / 65535)
-#endif /* PNG_READ_COMPOSITE_NODIV_SUPPORTED */
-
-#ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED
-PNG_EXPORT(201, png_uint_32, png_get_uint_32, (png_const_bytep buf));
-PNG_EXPORT(202, png_uint_16, png_get_uint_16, (png_const_bytep buf));
-PNG_EXPORT(203, png_int_32, png_get_int_32, (png_const_bytep buf));
-#endif
-
-PNG_EXPORT(204, png_uint_32, png_get_uint_31, (png_structp png_ptr,
-    png_const_bytep buf));
-/* No png_get_int_16 -- may be added if there's a real need for it. */
-
-/* Place a 32-bit number into a buffer in PNG byte order (big-endian). */
-#ifdef PNG_WRITE_INT_FUNCTIONS_SUPPORTED
-PNG_EXPORT(205, void, png_save_uint_32, (png_bytep buf, png_uint_32 i));
-#endif
-#ifdef PNG_SAVE_INT_32_SUPPORTED
-PNG_EXPORT(206, void, png_save_int_32, (png_bytep buf, png_int_32 i));
-#endif
-
-/* Place a 16-bit number into a buffer in PNG byte order.
- * The parameter is declared unsigned int, not png_uint_16,
- * just to avoid potential problems on pre-ANSI C compilers.
- */
-#ifdef PNG_WRITE_INT_FUNCTIONS_SUPPORTED
-PNG_EXPORT(207, void, png_save_uint_16, (png_bytep buf, unsigned int i));
-/* No png_save_int_16 -- may be added if there's a real need for it. */
-#endif
-
-#ifdef PNG_USE_READ_MACROS
-/* Inline macros to do direct reads of bytes from the input buffer.
- * The png_get_int_32() routine assumes we are using two's complement
- * format for negative values, which is almost certainly true.
- */
-#  define png_get_uint_32(buf) \
-     (((png_uint_32)(*(buf)) << 24) + \
-      ((png_uint_32)(*((buf) + 1)) << 16) + \
-      ((png_uint_32)(*((buf) + 2)) << 8) + \
-      ((png_uint_32)(*((buf) + 3))))
-
-   /* From libpng-1.4.0 until 1.4.4, the png_get_uint_16 macro (but not the
-    * function) incorrectly returned a value of type png_uint_32.
-    */
-#  define png_get_uint_16(buf) \
-     ((png_uint_16) \
-      (((unsigned int)(*(buf)) << 8) + \
-       ((unsigned int)(*((buf) + 1)))))
-
-#  define png_get_int_32(buf) \
-     ((png_int_32)((*(buf) & 0x80) \
-      ? -((png_int_32)((png_get_uint_32(buf) ^ 0xffffffffL) + 1)) \
-      : (png_int_32)png_get_uint_32(buf)))
-#endif
-
-#if defined(PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED) || \
-    defined(PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED)
-PNG_EXPORT(234, void, png_set_check_for_invalid_index, (png_structp png_ptr,
-    int allowed));
-#endif
-
-/* Maintainer: Put new public prototypes here ^, in libpng.3, and project
- * defs
- */
-
-/* The last ordinal number (this is the *last* one already used; the next
- * one to use is one more than this.)  Maintainer, remember to add an entry to
- * scripts/symbols.def as well.
- */
-#ifdef PNG_EXPORT_LAST_ORDINAL
-  PNG_EXPORT_LAST_ORDINAL(234);
-#endif
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* PNG_VERSION_INFO_ONLY */
-/* Do not put anything past this line */
-#endif /* PNG_H */

+ 0 - 596
include/libpng15/pngconf.h

@@ -1,596 +0,0 @@
-
-/* pngconf.h - machine configurable file for libpng
- *
- * libpng version 1.5.10 - March 29, 2012
- *
- * Copyright (c) 1998-2012 Glenn Randers-Pehrson
- * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
- * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
- *
- * This code is released under the libpng license.
- * For conditions of distribution and use, see the disclaimer
- * and license in png.h
- *
- */
-
-/* Any machine specific code is near the front of this file, so if you
- * are configuring libpng for a machine, you may want to read the section
- * starting here down to where it starts to typedef png_color, png_text,
- * and png_info.
- */
-
-#ifndef PNGCONF_H
-#define PNGCONF_H
-
-#ifndef PNG_BUILDING_SYMBOL_TABLE
-/* PNG_NO_LIMITS_H may be used to turn off the use of the standard C
- * definition file for  machine specific limits, this may impact the
- * correctness of the definitons below (see uses of INT_MAX).
- */
-#  ifndef PNG_NO_LIMITS_H
-#    include <limits.h>
-#  endif
-
-/* For the memory copy APIs (i.e. the standard definitions of these),
- * because this file defines png_memcpy and so on the base APIs must
- * be defined here.
- */
-#  ifdef BSD
-#    include <strings.h>
-#  else
-#    include <string.h>
-#  endif
-
-/* For png_FILE_p - this provides the standard definition of a
- * FILE
- */
-#  ifdef PNG_STDIO_SUPPORTED
-#    include <stdio.h>
-#  endif
-#endif
-
-/* This controls optimization of the reading of 16 and 32 bit values
- * from PNG files.  It can be set on a per-app-file basis - it
- * just changes whether a macro is used when the function is called.
- * The library builder sets the default; if read functions are not
- * built into the library the macro implementation is forced on.
- */
-#ifndef PNG_READ_INT_FUNCTIONS_SUPPORTED
-#  define PNG_USE_READ_MACROS
-#endif
-#if !defined(PNG_NO_USE_READ_MACROS) && !defined(PNG_USE_READ_MACROS)
-#  if PNG_DEFAULT_READ_MACROS
-#    define PNG_USE_READ_MACROS
-#  endif
-#endif
-
-/* COMPILER SPECIFIC OPTIONS.
- *
- * These options are provided so that a variety of difficult compilers
- * can be used.  Some are fixed at build time (e.g. PNG_API_RULE
- * below) but still have compiler specific implementations, others
- * may be changed on a per-file basis when compiling against libpng.
- */
-
-/* The PNGARG macro protects us against machines that don't have function
- * prototypes (ie K&R style headers).  If your compiler does not handle
- * function prototypes, define this macro and use the included ansi2knr.
- * I've always been able to use _NO_PROTO as the indicator, but you may
- * need to drag the empty declaration out in front of here, or change the
- * ifdef to suit your own needs.
- */
-#ifndef PNGARG
-
-#  ifdef OF /* zlib prototype munger */
-#    define PNGARG(arglist) OF(arglist)
-#  else
-
-#    ifdef _NO_PROTO
-#      define PNGARG(arglist) ()
-#    else
-#      define PNGARG(arglist) arglist
-#    endif /* _NO_PROTO */
-
-#  endif /* OF */
-
-#endif /* PNGARG */
-
-/* Function calling conventions.
- * =============================
- * Normally it is not necessary to specify to the compiler how to call
- * a function - it just does it - however on x86 systems derived from
- * Microsoft and Borland C compilers ('IBM PC', 'DOS', 'Windows' systems
- * and some others) there are multiple ways to call a function and the
- * default can be changed on the compiler command line.  For this reason
- * libpng specifies the calling convention of every exported function and
- * every function called via a user supplied function pointer.  This is
- * done in this file by defining the following macros:
- *
- * PNGAPI    Calling convention for exported functions.
- * PNGCBAPI  Calling convention for user provided (callback) functions.
- * PNGCAPI   Calling convention used by the ANSI-C library (required
- *           for longjmp callbacks and sometimes used internally to
- *           specify the calling convention for zlib).
- *
- * These macros should never be overridden.  If it is necessary to
- * change calling convention in a private build this can be done
- * by setting PNG_API_RULE (which defaults to 0) to one of the values
- * below to select the correct 'API' variants.
- *
- * PNG_API_RULE=0 Use PNGCAPI - the 'C' calling convention - throughout.
- *                This is correct in every known environment.
- * PNG_API_RULE=1 Use the operating system convention for PNGAPI and
- *                the 'C' calling convention (from PNGCAPI) for
- *                callbacks (PNGCBAPI).  This is no longer required
- *                in any known environment - if it has to be used
- *                please post an explanation of the problem to the
- *                libpng mailing list.
- *
- * These cases only differ if the operating system does not use the C
- * calling convention, at present this just means the above cases
- * (x86 DOS/Windows sytems) and, even then, this does not apply to
- * Cygwin running on those systems.
- *
- * Note that the value must be defined in pnglibconf.h so that what
- * the application uses to call the library matches the conventions
- * set when building the library.
- */
-
-/* Symbol export
- * =============
- * When building a shared library it is almost always necessary to tell
- * the compiler which symbols to export.  The png.h macro 'PNG_EXPORT'
- * is used to mark the symbols.  On some systems these symbols can be
- * extracted at link time and need no special processing by the compiler,
- * on other systems the symbols are flagged by the compiler and just
- * the declaration requires a special tag applied (unfortunately) in a
- * compiler dependent way.  Some systems can do either.
- *
- * A small number of older systems also require a symbol from a DLL to
- * be flagged to the program that calls it.  This is a problem because
- * we do not know in the header file included by application code that
- * the symbol will come from a shared library, as opposed to a statically
- * linked one.  For this reason the application must tell us by setting
- * the magic flag PNG_USE_DLL to turn on the special processing before
- * it includes png.h.
- *
- * Four additional macros are used to make this happen:
- *
- * PNG_IMPEXP The magic (if any) to cause a symbol to be exported from
- *            the build or imported if PNG_USE_DLL is set - compiler
- *            and system specific.
- *
- * PNG_EXPORT_TYPE(type) A macro that pre or appends PNG_IMPEXP to
- *                       'type', compiler specific.
- *
- * PNG_DLL_EXPORT Set to the magic to use during a libpng build to
- *                make a symbol exported from the DLL.  Not used in the
- *                public header files; see pngpriv.h for how it is used
- *                in the libpng build.
- *
- * PNG_DLL_IMPORT Set to the magic to force the libpng symbols to come
- *                from a DLL - used to define PNG_IMPEXP when
- *                PNG_USE_DLL is set.
- */
-
-/* System specific discovery.
- * ==========================
- * This code is used at build time to find PNG_IMPEXP, the API settings
- * and PNG_EXPORT_TYPE(), it may also set a macro to indicate the DLL
- * import processing is possible.  On Windows/x86 systems it also sets
- * compiler-specific macros to the values required to change the calling
- * conventions of the various functions.
- */
-#if ( defined(_Windows) || defined(_WINDOWS) || defined(WIN32) ||\
-      defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) ) &&\
-    ( defined(_X86_) || defined(_X64_) || defined(_M_IX86) ||\
-      defined(_M_X64) || defined(_M_IA64) )
-  /* Windows system (DOS doesn't support DLLs) running on x86/x64.  Includes
-   * builds under Cygwin or MinGW.  Also includes Watcom builds but these need
-   * special treatment because they are not compatible with GCC or Visual C
-   * because of different calling conventions.
-   */
-#  if PNG_API_RULE == 2
-    /* If this line results in an error, either because __watcall is not
-     * understood or because of a redefine just below you cannot use *this*
-     * build of the library with the compiler you are using.  *This* build was
-     * build using Watcom and applications must also be built using Watcom!
-     */
-#    define PNGCAPI __watcall
-#  endif
-
-#  if defined(__GNUC__) || (defined (_MSC_VER) && (_MSC_VER >= 800))
-#    define PNGCAPI __cdecl
-#    if PNG_API_RULE == 1
-#      define PNGAPI __stdcall
-#    endif
-#  else
-    /* An older compiler, or one not detected (erroneously) above,
-     * if necessary override on the command line to get the correct
-     * variants for the compiler.
-     */
-#    ifndef PNGCAPI
-#      define PNGCAPI _cdecl
-#    endif
-#    if PNG_API_RULE == 1 && !defined(PNGAPI)
-#      define PNGAPI _stdcall
-#    endif
-#  endif /* compiler/api */
-  /* NOTE: PNGCBAPI always defaults to PNGCAPI. */
-
-#  if defined(PNGAPI) && !defined(PNG_USER_PRIVATEBUILD)
-   ERROR: PNG_USER_PRIVATEBUILD must be defined if PNGAPI is changed
-#  endif
-
-#  if (defined(_MSC_VER) && _MSC_VER < 800) ||\
-      (defined(__BORLANDC__) && __BORLANDC__ < 0x500)
-    /* older Borland and MSC
-     * compilers used '__export' and required this to be after
-     * the type.
-     */
-#    ifndef PNG_EXPORT_TYPE
-#      define PNG_EXPORT_TYPE(type) type PNG_IMPEXP
-#    endif
-#    define PNG_DLL_EXPORT __export
-#  else /* newer compiler */
-#    define PNG_DLL_EXPORT __declspec(dllexport)
-#    ifndef PNG_DLL_IMPORT
-#      define PNG_DLL_IMPORT __declspec(dllimport)
-#    endif
-#  endif /* compiler */
-
-#else /* !Windows/x86 */
-#  if (defined(__IBMC__) || defined(__IBMCPP__)) && defined(__OS2__)
-#    define PNGAPI _System
-#  else /* !Windows/x86 && !OS/2 */
-    /* Use the defaults, or define PNG*API on the command line (but
-     * this will have to be done for every compile!)
-     */
-#  endif /* other system, !OS/2 */
-#endif /* !Windows/x86 */
-
-/* Now do all the defaulting . */
-#ifndef PNGCAPI
-#  define PNGCAPI
-#endif
-#ifndef PNGCBAPI
-#  define PNGCBAPI PNGCAPI
-#endif
-#ifndef PNGAPI
-#  define PNGAPI PNGCAPI
-#endif
-
-/* PNG_IMPEXP may be set on the compilation system command line or (if not set)
- * then in an internal header file when building the library, otherwise (when
- * using the library) it is set here.
- */
-#ifndef PNG_IMPEXP
-#  if defined(PNG_USE_DLL) && defined(PNG_DLL_IMPORT)
-     /* This forces use of a DLL, disallowing static linking */
-#    define PNG_IMPEXP PNG_DLL_IMPORT
-#  endif
-
-#  ifndef PNG_IMPEXP
-#    define PNG_IMPEXP
-#  endif
-#endif
-
-/* In 1.5.2 the definition of PNG_FUNCTION has been changed to always treat
- * 'attributes' as a storage class - the attributes go at the start of the
- * function definition, and attributes are always appended regardless of the
- * compiler.  This considerably simplifies these macros but may cause problems
- * if any compilers both need function attributes and fail to handle them as
- * a storage class (this is unlikely.)
- */
-#ifndef PNG_FUNCTION
-#  define PNG_FUNCTION(type, name, args, attributes) attributes type name args
-#endif
-
-#ifndef PNG_EXPORT_TYPE
-#  define PNG_EXPORT_TYPE(type) PNG_IMPEXP type
-#endif
-
-   /* The ordinal value is only relevant when preprocessing png.h for symbol
-    * table entries, so we discard it here.  See the .dfn files in the
-    * scripts directory.
-    */
-#ifndef PNG_EXPORTA
-
-#  define PNG_EXPORTA(ordinal, type, name, args, attributes)\
-      PNG_FUNCTION(PNG_EXPORT_TYPE(type),(PNGAPI name),PNGARG(args), \
-        extern attributes)
-#endif
-
-/* ANSI-C (C90) does not permit a macro to be invoked with an empty argument,
- * so make something non-empty to satisfy the requirement:
- */
-#define PNG_EMPTY /*empty list*/
-
-#define PNG_EXPORT(ordinal, type, name, args)\
-   PNG_EXPORTA(ordinal, type, name, args, PNG_EMPTY)
-
-/* Use PNG_REMOVED to comment out a removed interface. */
-#ifndef PNG_REMOVED
-#  define PNG_REMOVED(ordinal, type, name, args, attributes)
-#endif
-
-#ifndef PNG_CALLBACK
-#  define PNG_CALLBACK(type, name, args) type (PNGCBAPI name) PNGARG(args)
-#endif
-
-/* Support for compiler specific function attributes.  These are used
- * so that where compiler support is available incorrect use of API
- * functions in png.h will generate compiler warnings.
- *
- * Added at libpng-1.2.41.
- */
-
-#ifndef PNG_NO_PEDANTIC_WARNINGS
-#  ifndef PNG_PEDANTIC_WARNINGS_SUPPORTED
-#    define PNG_PEDANTIC_WARNINGS_SUPPORTED
-#  endif
-#endif
-
-#ifdef PNG_PEDANTIC_WARNINGS_SUPPORTED
-  /* Support for compiler specific function attributes.  These are used
-   * so that where compiler support is available incorrect use of API
-   * functions in png.h will generate compiler warnings.  Added at libpng
-   * version 1.2.41.
-   */
-#  if defined(__GNUC__)
-#    ifndef PNG_USE_RESULT
-#      define PNG_USE_RESULT __attribute__((__warn_unused_result__))
-#    endif
-#    ifndef PNG_NORETURN
-#      define PNG_NORETURN   __attribute__((__noreturn__))
-#    endif
-#    ifndef PNG_ALLOCATED
-#      define PNG_ALLOCATED  __attribute__((__malloc__))
-#    endif
-#    ifndef PNG_DEPRECATED
-#      define PNG_DEPRECATED __attribute__((__deprecated__))
-#    endif
-#    ifndef PNG_PRIVATE
-#      if 0 /* Doesn't work so we use deprecated instead*/
-#        define PNG_PRIVATE \
-          __attribute__((warning("This function is not exported by libpng.")))
-#      else
-#        define PNG_PRIVATE \
-          __attribute__((__deprecated__))
-#      endif
-#    endif
-#  endif /* __GNUC__ */
-
-#  if defined(_MSC_VER)  && (_MSC_VER >= 1300)
-#    ifndef PNG_USE_RESULT
-#      define PNG_USE_RESULT /* not supported */
-#    endif
-#    ifndef PNG_NORETURN
-#      define PNG_NORETURN __declspec(noreturn)
-#    endif
-#    ifndef PNG_ALLOCATED
-#      if (_MSC_VER >= 1400)
-#        define PNG_ALLOCATED __declspec(restrict)
-#      endif
-#    endif
-#    ifndef PNG_DEPRECATED
-#      define PNG_DEPRECATED __declspec(deprecated)
-#    endif
-#    ifndef PNG_PRIVATE
-#      define PNG_PRIVATE __declspec(deprecated)
-#    endif
-#  endif /* _MSC_VER */
-#endif /* PNG_PEDANTIC_WARNINGS */
-
-#ifndef PNG_DEPRECATED
-#  define PNG_DEPRECATED  /* Use of this function is deprecated */
-#endif
-#ifndef PNG_USE_RESULT
-#  define PNG_USE_RESULT  /* The result of this function must be checked */
-#endif
-#ifndef PNG_NORETURN
-#  define PNG_NORETURN    /* This function does not return */
-#endif
-#ifndef PNG_ALLOCATED
-#  define PNG_ALLOCATED   /* The result of the function is new memory */
-#endif
-#ifndef PNG_PRIVATE
-#  define PNG_PRIVATE     /* This is a private libpng function */
-#endif
-#ifndef PNG_FP_EXPORT     /* A floating point API. */
-#  ifdef PNG_FLOATING_POINT_SUPPORTED
-#     define PNG_FP_EXPORT(ordinal, type, name, args)\
-         PNG_EXPORT(ordinal, type, name, args)
-#  else                   /* No floating point APIs */
-#     define PNG_FP_EXPORT(ordinal, type, name, args)
-#  endif
-#endif
-#ifndef PNG_FIXED_EXPORT  /* A fixed point API. */
-#  ifdef PNG_FIXED_POINT_SUPPORTED
-#     define PNG_FIXED_EXPORT(ordinal, type, name, args)\
-         PNG_EXPORT(ordinal, type, name, args)
-#  else                   /* No fixed point APIs */
-#     define PNG_FIXED_EXPORT(ordinal, type, name, args)
-#  endif
-#endif
-
-/* The following uses const char * instead of char * for error
- * and warning message functions, so some compilers won't complain.
- * If you do not want to use const, define PNG_NO_CONST here.
- *
- * This should not change how the APIs are called, so it can be done
- * on a per-file basis in the application.
- */
-#ifndef PNG_CONST
-#  ifndef PNG_NO_CONST
-#    define PNG_CONST const
-#  else
-#    define PNG_CONST
-#  endif
-#endif
-
-/* Some typedefs to get us started.  These should be safe on most of the
- * common platforms.  The typedefs should be at least as large as the
- * numbers suggest (a png_uint_32 must be at least 32 bits long), but they
- * don't have to be exactly that size.  Some compilers dislike passing
- * unsigned shorts as function parameters, so you may be better off using
- * unsigned int for png_uint_16.
- */
-
-#if defined(INT_MAX) && (INT_MAX > 0x7ffffffeL)
-typedef unsigned int png_uint_32;
-typedef int png_int_32;
-#else
-typedef unsigned long png_uint_32;
-typedef long png_int_32;
-#endif
-typedef unsigned short png_uint_16;
-typedef short png_int_16;
-typedef unsigned char png_byte;
-
-#ifdef PNG_NO_SIZE_T
-typedef unsigned int png_size_t;
-#else
-typedef size_t png_size_t;
-#endif
-#define png_sizeof(x) (sizeof (x))
-
-/* The following is needed for medium model support.  It cannot be in the
- * pngpriv.h header.  Needs modification for other compilers besides
- * MSC.  Model independent support declares all arrays and pointers to be
- * large using the far keyword.  The zlib version used must also support
- * model independent data.  As of version zlib 1.0.4, the necessary changes
- * have been made in zlib.  The USE_FAR_KEYWORD define triggers other
- * changes that are needed. (Tim Wegner)
- */
-
-/* Separate compiler dependencies (problem here is that zlib.h always
- * defines FAR. (SJT)
- */
-#ifdef __BORLANDC__
-#  if defined(__LARGE__) || defined(__HUGE__) || defined(__COMPACT__)
-#    define LDATA 1
-#  else
-#    define LDATA 0
-#  endif
-  /* GRR:  why is Cygwin in here?  Cygwin is not Borland C... */
-#  if !defined(__WIN32__) && !defined(__FLAT__) && !defined(__CYGWIN__)
-#    define PNG_MAX_MALLOC_64K /* only used in build */
-#    if (LDATA != 1)
-#      ifndef FAR
-#        define FAR __far
-#      endif
-#      define USE_FAR_KEYWORD
-#    endif   /* LDATA != 1 */
-         /* Possibly useful for moving data out of default segment.
-          * Uncomment it if you want. Could also define FARDATA as
-          * const if your compiler supports it. (SJT)
-#        define FARDATA FAR
-          */
-#  endif  /* __WIN32__, __FLAT__, __CYGWIN__ */
-#endif   /* __BORLANDC__ */
-
-
-/* Suggest testing for specific compiler first before testing for
- * FAR.  The Watcom compiler defines both __MEDIUM__ and M_I86MM,
- * making reliance oncertain keywords suspect. (SJT)
- */
-
-/* MSC Medium model */
-#ifdef FAR
-#  ifdef M_I86MM
-#    define USE_FAR_KEYWORD
-#    define FARDATA FAR
-#    include <dos.h>
-#  endif
-#endif
-
-/* SJT: default case */
-#ifndef FAR
-#  define FAR
-#endif
-
-/* At this point FAR is always defined */
-#ifndef FARDATA
-#  define FARDATA
-#endif
-
-/* Typedef for floating-point numbers that are converted
- * to fixed-point with a multiple of 100,000, e.g., gamma
- */
-typedef png_int_32 png_fixed_point;
-
-/* Add typedefs for pointers */
-typedef void                      FAR * png_voidp;
-typedef PNG_CONST void            FAR * png_const_voidp;
-typedef png_byte                  FAR * png_bytep;
-typedef PNG_CONST png_byte        FAR * png_const_bytep;
-typedef png_uint_32               FAR * png_uint_32p;
-typedef PNG_CONST png_uint_32     FAR * png_const_uint_32p;
-typedef png_int_32                FAR * png_int_32p;
-typedef PNG_CONST png_int_32      FAR * png_const_int_32p;
-typedef png_uint_16               FAR * png_uint_16p;
-typedef PNG_CONST png_uint_16     FAR * png_const_uint_16p;
-typedef png_int_16                FAR * png_int_16p;
-typedef PNG_CONST png_int_16      FAR * png_const_int_16p;
-typedef char                      FAR * png_charp;
-typedef PNG_CONST char            FAR * png_const_charp;
-typedef png_fixed_point           FAR * png_fixed_point_p;
-typedef PNG_CONST png_fixed_point FAR * png_const_fixed_point_p;
-typedef png_size_t                FAR * png_size_tp;
-typedef PNG_CONST png_size_t      FAR * png_const_size_tp;
-
-#ifdef PNG_STDIO_SUPPORTED
-typedef FILE            * png_FILE_p;
-#endif
-
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-typedef double           FAR * png_doublep;
-typedef PNG_CONST double FAR * png_const_doublep;
-#endif
-
-/* Pointers to pointers; i.e. arrays */
-typedef png_byte        FAR * FAR * png_bytepp;
-typedef png_uint_32     FAR * FAR * png_uint_32pp;
-typedef png_int_32      FAR * FAR * png_int_32pp;
-typedef png_uint_16     FAR * FAR * png_uint_16pp;
-typedef png_int_16      FAR * FAR * png_int_16pp;
-typedef PNG_CONST char  FAR * FAR * png_const_charpp;
-typedef char            FAR * FAR * png_charpp;
-typedef png_fixed_point FAR * FAR * png_fixed_point_pp;
-#ifdef PNG_FLOATING_POINT_SUPPORTED
-typedef double          FAR * FAR * png_doublepp;
-#endif
-
-/* Pointers to pointers to pointers; i.e., pointer to array */
-typedef char            FAR * FAR * FAR * png_charppp;
-
-/* png_alloc_size_t is guaranteed to be no smaller than png_size_t,
- * and no smaller than png_uint_32.  Casts from png_size_t or png_uint_32
- * to png_alloc_size_t are not necessary; in fact, it is recommended
- * not to use them at all so that the compiler can complain when something
- * turns out to be problematic.
- * Casts in the other direction (from png_alloc_size_t to png_size_t or
- * png_uint_32) should be explicitly applied; however, we do not expect
- * to encounter practical situations that require such conversions.
- */
-#if defined(__TURBOC__) && !defined(__FLAT__)
-   typedef unsigned long png_alloc_size_t;
-#else
-#  if defined(_MSC_VER) && defined(MAXSEG_64K)
-     typedef unsigned long    png_alloc_size_t;
-#  else
-     /* This is an attempt to detect an old Windows system where (int) is
-      * actually 16 bits, in that case png_malloc must have an argument with a
-      * bigger size to accomodate the requirements of the library.
-      */
-#    if (defined(_Windows) || defined(_WINDOWS) || defined(_WINDOWS_)) && \
-        (!defined(INT_MAX) || INT_MAX <= 0x7ffffffeL)
-       typedef DWORD         png_alloc_size_t;
-#    else
-       typedef png_size_t    png_alloc_size_t;
-#    endif
-#  endif
-#endif
-
-#endif /* PNGCONF_H */

+ 0 - 186
include/libpng15/pnglibconf.h

@@ -1,186 +0,0 @@
-
-/* libpng STANDARD API DEFINITION */
-
-/* pnglibconf.h - library build configuration */
-
-/* Libpng 1.5.10 - March 29, 2012 */
-
-/* Copyright (c) 1998-2012 Glenn Randers-Pehrson */
-
-/* This code is released under the libpng license. */
-/* For conditions of distribution and use, see the disclaimer */
-/* and license in png.h */
-
-/* pnglibconf.h */
-/* Derived from: scripts/pnglibconf.dfa */
-/* If you edit this file by hand you must obey the rules expressed in */
-/* pnglibconf.dfa with respect to the dependencies between the following */
-/* symbols.  It is much better to generate a new file using */
-/* scripts/libpngconf.mak */
-
-#ifndef PNGLCONF_H
-#define PNGLCONF_H
-/* settings */
-#define PNG_API_RULE 0
-#define PNG_CALLOC_SUPPORTED
-#define PNG_COST_SHIFT 3
-#define PNG_DEFAULT_READ_MACROS 1
-#define PNG_GAMMA_THRESHOLD_FIXED 5000
-#define PNG_MAX_GAMMA_8 11
-#define PNG_QUANTIZE_BLUE_BITS 5
-#define PNG_QUANTIZE_GREEN_BITS 5
-#define PNG_QUANTIZE_RED_BITS 5
-#define PNG_sCAL_PRECISION 5
-#define PNG_WEIGHT_SHIFT 8
-#define PNG_ZBUF_SIZE 8192
-/* end of settings */
-/* options */
-#define PNG_16BIT_SUPPORTED
-#define PNG_ALIGN_MEMORY_SUPPORTED
-#define PNG_BENIGN_ERRORS_SUPPORTED
-#define PNG_bKGD_SUPPORTED
-#define PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED
-#define PNG_CHECK_cHRM_SUPPORTED
-#define PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
-#define PNG_cHRM_SUPPORTED
-#define PNG_CONSOLE_IO_SUPPORTED
-#define PNG_CONVERT_tIME_SUPPORTED
-#define PNG_EASY_ACCESS_SUPPORTED
-/*#undef PNG_ERROR_NUMBERS_SUPPORTED*/
-#define PNG_ERROR_TEXT_SUPPORTED
-#define PNG_FIXED_POINT_SUPPORTED
-#define PNG_FLOATING_ARITHMETIC_SUPPORTED
-#define PNG_FLOATING_POINT_SUPPORTED
-#define PNG_gAMA_SUPPORTED
-#define PNG_HANDLE_AS_UNKNOWN_SUPPORTED
-#define PNG_hIST_SUPPORTED
-#define PNG_iCCP_SUPPORTED
-#define PNG_INCH_CONVERSIONS_SUPPORTED
-#define PNG_INFO_IMAGE_SUPPORTED
-#define PNG_IO_STATE_SUPPORTED
-#define PNG_iTXt_SUPPORTED
-#define PNG_MNG_FEATURES_SUPPORTED
-#define PNG_oFFs_SUPPORTED
-#define PNG_pCAL_SUPPORTED
-#define PNG_pHYs_SUPPORTED
-#define PNG_POINTER_INDEXING_SUPPORTED
-#define PNG_PROGRESSIVE_READ_SUPPORTED
-#define PNG_READ_16BIT_SUPPORTED
-#define PNG_READ_ALPHA_MODE_SUPPORTED
-#define PNG_READ_ANCILLARY_CHUNKS_SUPPORTED
-#define PNG_READ_BACKGROUND_SUPPORTED
-#define PNG_READ_BGR_SUPPORTED
-#define PNG_READ_bKGD_SUPPORTED
-#define PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
-#define PNG_READ_cHRM_SUPPORTED
-#define PNG_READ_COMPOSITE_NODIV_SUPPORTED
-#define PNG_READ_COMPRESSED_TEXT_SUPPORTED
-#define PNG_READ_EXPAND_16_SUPPORTED
-#define PNG_READ_EXPAND_SUPPORTED
-#define PNG_READ_FILLER_SUPPORTED
-#define PNG_READ_gAMA_SUPPORTED
-#define PNG_READ_GAMMA_SUPPORTED
-#define PNG_READ_GRAY_TO_RGB_SUPPORTED
-#define PNG_READ_hIST_SUPPORTED
-#define PNG_READ_iCCP_SUPPORTED
-#define PNG_READ_INTERLACING_SUPPORTED
-#define PNG_READ_INT_FUNCTIONS_SUPPORTED
-#define PNG_READ_INVERT_ALPHA_SUPPORTED
-#define PNG_READ_INVERT_SUPPORTED
-#define PNG_READ_iTXt_SUPPORTED
-#define PNG_READ_oFFs_SUPPORTED
-#define PNG_READ_OPT_PLTE_SUPPORTED
-#define PNG_READ_PACK_SUPPORTED
-#define PNG_READ_PACKSWAP_SUPPORTED
-#define PNG_READ_pCAL_SUPPORTED
-#define PNG_READ_pHYs_SUPPORTED
-#define PNG_READ_QUANTIZE_SUPPORTED
-#define PNG_READ_RGB_TO_GRAY_SUPPORTED
-#define PNG_READ_sBIT_SUPPORTED
-#define PNG_READ_SCALE_16_TO_8_SUPPORTED
-#define PNG_READ_sCAL_SUPPORTED
-#define PNG_READ_SHIFT_SUPPORTED
-#define PNG_READ_sPLT_SUPPORTED
-#define PNG_READ_sRGB_SUPPORTED
-#define PNG_READ_STRIP_16_TO_8_SUPPORTED
-#define PNG_READ_STRIP_ALPHA_SUPPORTED
-#define PNG_READ_SUPPORTED
-#define PNG_READ_SWAP_ALPHA_SUPPORTED
-#define PNG_READ_SWAP_SUPPORTED
-#define PNG_READ_tEXt_SUPPORTED
-#define PNG_READ_TEXT_SUPPORTED
-#define PNG_READ_tIME_SUPPORTED
-#define PNG_READ_TRANSFORMS_SUPPORTED
-#define PNG_READ_tRNS_SUPPORTED
-#define PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
-#define PNG_READ_USER_CHUNKS_SUPPORTED
-#define PNG_READ_USER_TRANSFORM_SUPPORTED
-#define PNG_READ_zTXt_SUPPORTED
-#define PNG_SAVE_INT_32_SUPPORTED
-#define PNG_sBIT_SUPPORTED
-#define PNG_sCAL_SUPPORTED
-#define PNG_SEQUENTIAL_READ_SUPPORTED
-#define PNG_SET_CHUNK_CACHE_LIMIT_SUPPORTED
-#define PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
-#define PNG_SETJMP_SUPPORTED
-#define PNG_SET_USER_LIMITS_SUPPORTED
-#define PNG_sPLT_SUPPORTED
-#define PNG_sRGB_SUPPORTED
-#define PNG_STDIO_SUPPORTED
-#define PNG_tEXt_SUPPORTED
-#define PNG_TEXT_SUPPORTED
-#define PNG_TIME_RFC1123_SUPPORTED
-#define PNG_tIME_SUPPORTED
-#define PNG_tRNS_SUPPORTED
-#define PNG_UNKNOWN_CHUNKS_SUPPORTED
-#define PNG_USER_CHUNKS_SUPPORTED
-#define PNG_USER_LIMITS_SUPPORTED
-#define PNG_USER_MEM_SUPPORTED
-#define PNG_USER_TRANSFORM_INFO_SUPPORTED
-#define PNG_USER_TRANSFORM_PTR_SUPPORTED
-#define PNG_WARNINGS_SUPPORTED
-#define PNG_WRITE_16BIT_SUPPORTED
-#define PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED
-#define PNG_WRITE_BGR_SUPPORTED
-#define PNG_WRITE_bKGD_SUPPORTED
-#define PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
-#define PNG_WRITE_cHRM_SUPPORTED
-#define PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
-#define PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
-#define PNG_WRITE_FILLER_SUPPORTED
-#define PNG_WRITE_FILTER_SUPPORTED
-#define PNG_WRITE_FLUSH_SUPPORTED
-#define PNG_WRITE_gAMA_SUPPORTED
-#define PNG_WRITE_hIST_SUPPORTED
-#define PNG_WRITE_iCCP_SUPPORTED
-#define PNG_WRITE_INTERLACING_SUPPORTED
-#define PNG_WRITE_INT_FUNCTIONS_SUPPORTED
-#define PNG_WRITE_INVERT_ALPHA_SUPPORTED
-#define PNG_WRITE_INVERT_SUPPORTED
-#define PNG_WRITE_iTXt_SUPPORTED
-#define PNG_WRITE_oFFs_SUPPORTED
-#define PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
-#define PNG_WRITE_PACK_SUPPORTED
-#define PNG_WRITE_PACKSWAP_SUPPORTED
-#define PNG_WRITE_pCAL_SUPPORTED
-#define PNG_WRITE_pHYs_SUPPORTED
-#define PNG_WRITE_sBIT_SUPPORTED
-#define PNG_WRITE_sCAL_SUPPORTED
-#define PNG_WRITE_SHIFT_SUPPORTED
-#define PNG_WRITE_sPLT_SUPPORTED
-#define PNG_WRITE_sRGB_SUPPORTED
-#define PNG_WRITE_SUPPORTED
-#define PNG_WRITE_SWAP_ALPHA_SUPPORTED
-#define PNG_WRITE_SWAP_SUPPORTED
-#define PNG_WRITE_tEXt_SUPPORTED
-#define PNG_WRITE_TEXT_SUPPORTED
-#define PNG_WRITE_tIME_SUPPORTED
-#define PNG_WRITE_TRANSFORMS_SUPPORTED
-#define PNG_WRITE_tRNS_SUPPORTED
-#define PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
-#define PNG_WRITE_USER_TRANSFORM_SUPPORTED
-#define PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
-#define PNG_WRITE_zTXt_SUPPORTED
-#define PNG_zTXt_SUPPORTED
-/* end of options */
-#endif /* PNGLCONF_H */

+ 0 - 25
include/ogg/config_types.h

@@ -1,25 +0,0 @@
-#ifndef __CONFIG_TYPES_H__
-#define __CONFIG_TYPES_H__
-
-/* these are filled in by configure */
-#define INCLUDE_INTTYPES_H 1
-#define INCLUDE_STDINT_H 1
-#define INCLUDE_SYS_TYPES_H 1
-
-#if INCLUDE_INTTYPES_H
-#  include <inttypes.h>
-#endif
-#if INCLUDE_STDINT_H
-#  include <stdint.h>
-#endif
-#if INCLUDE_SYS_TYPES_H
-#  include <sys/types.h>
-#endif
-
-typedef short ogg_int16_t;
-typedef unsigned short ogg_uint16_t;
-typedef int ogg_int32_t;
-typedef unsigned int ogg_uint32_t;
-typedef long ogg_int64_t;
-
-#endif

+ 0 - 209
include/ogg/ogg.h

@@ -1,209 +0,0 @@
-/********************************************************************
- *                                                                  *
- * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
- * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
- * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
- * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
- *                                                                  *
- * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007             *
- * by the Xiph.Org Foundation http://www.xiph.org/                  *
- *                                                                  *
- ********************************************************************
-
- function: toplevel libogg include
- last mod: $Id: ogg.h 17571 2010-10-27 13:28:20Z xiphmont $
-
- ********************************************************************/
-#ifndef _OGG_H
-#define _OGG_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include <stddef.h>
-#include <ogg/os_types.h>
-
-typedef struct {
-  void *iov_base;
-  size_t iov_len;
-} ogg_iovec_t;
-
-typedef struct {
-  long endbyte;
-  int  endbit;
-
-  unsigned char *buffer;
-  unsigned char *ptr;
-  long storage;
-} oggpack_buffer;
-
-/* ogg_page is used to encapsulate the data in one Ogg bitstream page *****/
-
-typedef struct {
-  unsigned char *header;
-  long header_len;
-  unsigned char *body;
-  long body_len;
-} ogg_page;
-
-/* ogg_stream_state contains the current encode/decode state of a logical
-   Ogg bitstream **********************************************************/
-
-typedef struct {
-  unsigned char   *body_data;    /* bytes from packet bodies */
-  long    body_storage;          /* storage elements allocated */
-  long    body_fill;             /* elements stored; fill mark */
-  long    body_returned;         /* elements of fill returned */
-
-
-  int     *lacing_vals;      /* The values that will go to the segment table */
-  ogg_int64_t *granule_vals; /* granulepos values for headers. Not compact
-                                this way, but it is simple coupled to the
-                                lacing fifo */
-  long    lacing_storage;
-  long    lacing_fill;
-  long    lacing_packet;
-  long    lacing_returned;
-
-  unsigned char    header[282];      /* working space for header encode */
-  int              header_fill;
-
-  int     e_o_s;          /* set when we have buffered the last packet in the
-                             logical bitstream */
-  int     b_o_s;          /* set after we've written the initial page
-                             of a logical bitstream */
-  long    serialno;
-  long    pageno;
-  ogg_int64_t  packetno;  /* sequence number for decode; the framing
-                             knows where there's a hole in the data,
-                             but we need coupling so that the codec
-                             (which is in a separate abstraction
-                             layer) also knows about the gap */
-  ogg_int64_t   granulepos;
-
-} ogg_stream_state;
-
-/* ogg_packet is used to encapsulate the data and metadata belonging
-   to a single raw Ogg/Vorbis packet *************************************/
-
-typedef struct {
-  unsigned char *packet;
-  long  bytes;
-  long  b_o_s;
-  long  e_o_s;
-
-  ogg_int64_t  granulepos;
-
-  ogg_int64_t  packetno;     /* sequence number for decode; the framing
-                                knows where there's a hole in the data,
-                                but we need coupling so that the codec
-                                (which is in a separate abstraction
-                                layer) also knows about the gap */
-} ogg_packet;
-
-typedef struct {
-  unsigned char *data;
-  int storage;
-  int fill;
-  int returned;
-
-  int unsynced;
-  int headerbytes;
-  int bodybytes;
-} ogg_sync_state;
-
-/* Ogg BITSTREAM PRIMITIVES: bitstream ************************/
-
-extern void  oggpack_writeinit(oggpack_buffer *b);
-extern int   oggpack_writecheck(oggpack_buffer *b);
-extern void  oggpack_writetrunc(oggpack_buffer *b,long bits);
-extern void  oggpack_writealign(oggpack_buffer *b);
-extern void  oggpack_writecopy(oggpack_buffer *b,void *source,long bits);
-extern void  oggpack_reset(oggpack_buffer *b);
-extern void  oggpack_writeclear(oggpack_buffer *b);
-extern void  oggpack_readinit(oggpack_buffer *b,unsigned char *buf,int bytes);
-extern void  oggpack_write(oggpack_buffer *b,unsigned long value,int bits);
-extern long  oggpack_look(oggpack_buffer *b,int bits);
-extern long  oggpack_look1(oggpack_buffer *b);
-extern void  oggpack_adv(oggpack_buffer *b,int bits);
-extern void  oggpack_adv1(oggpack_buffer *b);
-extern long  oggpack_read(oggpack_buffer *b,int bits);
-extern long  oggpack_read1(oggpack_buffer *b);
-extern long  oggpack_bytes(oggpack_buffer *b);
-extern long  oggpack_bits(oggpack_buffer *b);
-extern unsigned char *oggpack_get_buffer(oggpack_buffer *b);
-
-extern void  oggpackB_writeinit(oggpack_buffer *b);
-extern int   oggpackB_writecheck(oggpack_buffer *b);
-extern void  oggpackB_writetrunc(oggpack_buffer *b,long bits);
-extern void  oggpackB_writealign(oggpack_buffer *b);
-extern void  oggpackB_writecopy(oggpack_buffer *b,void *source,long bits);
-extern void  oggpackB_reset(oggpack_buffer *b);
-extern void  oggpackB_writeclear(oggpack_buffer *b);
-extern void  oggpackB_readinit(oggpack_buffer *b,unsigned char *buf,int bytes);
-extern void  oggpackB_write(oggpack_buffer *b,unsigned long value,int bits);
-extern long  oggpackB_look(oggpack_buffer *b,int bits);
-extern long  oggpackB_look1(oggpack_buffer *b);
-extern void  oggpackB_adv(oggpack_buffer *b,int bits);
-extern void  oggpackB_adv1(oggpack_buffer *b);
-extern long  oggpackB_read(oggpack_buffer *b,int bits);
-extern long  oggpackB_read1(oggpack_buffer *b);
-extern long  oggpackB_bytes(oggpack_buffer *b);
-extern long  oggpackB_bits(oggpack_buffer *b);
-extern unsigned char *oggpackB_get_buffer(oggpack_buffer *b);
-
-/* Ogg BITSTREAM PRIMITIVES: encoding **************************/
-
-extern int      ogg_stream_packetin(ogg_stream_state *os, ogg_packet *op);
-extern int      ogg_stream_iovecin(ogg_stream_state *os, ogg_iovec_t *iov,
-                                   int count, long e_o_s, ogg_int64_t granulepos);
-extern int      ogg_stream_pageout(ogg_stream_state *os, ogg_page *og);
-extern int      ogg_stream_pageout_fill(ogg_stream_state *os, ogg_page *og, int nfill);
-extern int      ogg_stream_flush(ogg_stream_state *os, ogg_page *og);
-
-/* Ogg BITSTREAM PRIMITIVES: decoding **************************/
-
-extern int      ogg_sync_init(ogg_sync_state *oy);
-extern int      ogg_sync_clear(ogg_sync_state *oy);
-extern int      ogg_sync_reset(ogg_sync_state *oy);
-extern int      ogg_sync_destroy(ogg_sync_state *oy);
-extern int      ogg_sync_check(ogg_sync_state *oy);
-
-extern char    *ogg_sync_buffer(ogg_sync_state *oy, long size);
-extern int      ogg_sync_wrote(ogg_sync_state *oy, long bytes);
-extern long     ogg_sync_pageseek(ogg_sync_state *oy,ogg_page *og);
-extern int      ogg_sync_pageout(ogg_sync_state *oy, ogg_page *og);
-extern int      ogg_stream_pagein(ogg_stream_state *os, ogg_page *og);
-extern int      ogg_stream_packetout(ogg_stream_state *os,ogg_packet *op);
-extern int      ogg_stream_packetpeek(ogg_stream_state *os,ogg_packet *op);
-
-/* Ogg BITSTREAM PRIMITIVES: general ***************************/
-
-extern int      ogg_stream_init(ogg_stream_state *os,int serialno);
-extern int      ogg_stream_clear(ogg_stream_state *os);
-extern int      ogg_stream_reset(ogg_stream_state *os);
-extern int      ogg_stream_reset_serialno(ogg_stream_state *os,int serialno);
-extern int      ogg_stream_destroy(ogg_stream_state *os);
-extern int      ogg_stream_check(ogg_stream_state *os);
-extern int      ogg_stream_eos(ogg_stream_state *os);
-
-extern void     ogg_page_checksum_set(ogg_page *og);
-
-extern int      ogg_page_version(const ogg_page *og);
-extern int      ogg_page_continued(const ogg_page *og);
-extern int      ogg_page_bos(const ogg_page *og);
-extern int      ogg_page_eos(const ogg_page *og);
-extern ogg_int64_t  ogg_page_granulepos(const ogg_page *og);
-extern int      ogg_page_serialno(const ogg_page *og);
-extern long     ogg_page_pageno(const ogg_page *og);
-extern int      ogg_page_packets(const ogg_page *og);
-
-extern void     ogg_packet_clear(ogg_packet *op);
-
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif  /* _OGG_H */

+ 0 - 147
include/ogg/os_types.h

@@ -1,147 +0,0 @@
-/********************************************************************
- *                                                                  *
- * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
- * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
- * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
- * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
- *                                                                  *
- * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002             *
- * by the Xiph.Org Foundation http://www.xiph.org/                  *
- *                                                                  *
- ********************************************************************
-
- function: #ifdef jail to whip a few platforms into the UNIX ideal.
- last mod: $Id: os_types.h 17712 2010-12-03 17:10:02Z xiphmont $
-
- ********************************************************************/
-#ifndef _OS_TYPES_H
-#define _OS_TYPES_H
-
-/* make it easy on the folks that want to compile the libs with a
-   different malloc than stdlib */
-#define _ogg_malloc  malloc
-#define _ogg_calloc  calloc
-#define _ogg_realloc realloc
-#define _ogg_free    free
-
-#if defined(_WIN32) 
-
-#  if defined(__CYGWIN__)
-#    include <stdint.h>
-     typedef int16_t ogg_int16_t;
-     typedef uint16_t ogg_uint16_t;
-     typedef int32_t ogg_int32_t;
-     typedef uint32_t ogg_uint32_t;
-     typedef int64_t ogg_int64_t;
-     typedef uint64_t ogg_uint64_t;
-#  elif defined(__MINGW32__)
-#    include <sys/types.h>
-     typedef short ogg_int16_t;
-     typedef unsigned short ogg_uint16_t;
-     typedef int ogg_int32_t;
-     typedef unsigned int ogg_uint32_t;
-     typedef long long ogg_int64_t;
-     typedef unsigned long long ogg_uint64_t;
-#  elif defined(__MWERKS__)
-     typedef long long ogg_int64_t;
-     typedef int ogg_int32_t;
-     typedef unsigned int ogg_uint32_t;
-     typedef short ogg_int16_t;
-     typedef unsigned short ogg_uint16_t;
-#  else
-     /* MSVC/Borland */
-     typedef __int64 ogg_int64_t;
-     typedef __int32 ogg_int32_t;
-     typedef unsigned __int32 ogg_uint32_t;
-     typedef __int16 ogg_int16_t;
-     typedef unsigned __int16 ogg_uint16_t;
-#  endif
-
-#elif defined(__MACOS__)
-
-#  include <sys/types.h>
-   typedef SInt16 ogg_int16_t;
-   typedef UInt16 ogg_uint16_t;
-   typedef SInt32 ogg_int32_t;
-   typedef UInt32 ogg_uint32_t;
-   typedef SInt64 ogg_int64_t;
-
-#elif (defined(__APPLE__) && defined(__MACH__)) /* MacOS X Framework build */
-
-#  include <inttypes.h>
-   typedef int16_t ogg_int16_t;
-   typedef uint16_t ogg_uint16_t;
-   typedef int32_t ogg_int32_t;
-   typedef uint32_t ogg_uint32_t;
-   typedef int64_t ogg_int64_t;
-
-#elif defined(__HAIKU__)
-
-  /* Haiku */
-#  include <sys/types.h>
-   typedef short ogg_int16_t;
-   typedef unsigned short ogg_uint16_t;
-   typedef int ogg_int32_t;
-   typedef unsigned int ogg_uint32_t;
-   typedef long long ogg_int64_t;
-
-#elif defined(__BEOS__)
-
-   /* Be */
-#  include <inttypes.h>
-   typedef int16_t ogg_int16_t;
-   typedef uint16_t ogg_uint16_t;
-   typedef int32_t ogg_int32_t;
-   typedef uint32_t ogg_uint32_t;
-   typedef int64_t ogg_int64_t;
-
-#elif defined (__EMX__)
-
-   /* OS/2 GCC */
-   typedef short ogg_int16_t;
-   typedef unsigned short ogg_uint16_t;
-   typedef int ogg_int32_t;
-   typedef unsigned int ogg_uint32_t;
-   typedef long long ogg_int64_t;
-
-#elif defined (DJGPP)
-
-   /* DJGPP */
-   typedef short ogg_int16_t;
-   typedef int ogg_int32_t;
-   typedef unsigned int ogg_uint32_t;
-   typedef long long ogg_int64_t;
-
-#elif defined(R5900)
-
-   /* PS2 EE */
-   typedef long ogg_int64_t;
-   typedef int ogg_int32_t;
-   typedef unsigned ogg_uint32_t;
-   typedef short ogg_int16_t;
-
-#elif defined(__SYMBIAN32__)
-
-   /* Symbian GCC */
-   typedef signed short ogg_int16_t;
-   typedef unsigned short ogg_uint16_t;
-   typedef signed int ogg_int32_t;
-   typedef unsigned int ogg_uint32_t;
-   typedef long long int ogg_int64_t;
-
-#elif defined(__TMS320C6X__)
-
-   /* TI C64x compiler */
-   typedef signed short ogg_int16_t;
-   typedef unsigned short ogg_uint16_t;
-   typedef signed int ogg_int32_t;
-   typedef unsigned int ogg_uint32_t;
-   typedef long long int ogg_int64_t;
-
-#else
-
-#  include <ogg/config_types.h>
-
-#endif
-
-#endif  /* _OS_TYPES_H */

+ 7 - 3
include/polycode/core/PolyAndroidCore.h

@@ -41,6 +41,7 @@
 
 namespace Polycode {
 
+	class OpenSLAudioInterface;
 	class PolycodeView;
 // 	class ANativeActivity;
 	
@@ -56,7 +57,7 @@ namespace Polycode {
 		TouchInfo touch;
 
 		PolyKEY keyCode;
-		wchar_t unicodeChar;
+		String text;
 
 		char mouseButton;
 		long eventTime;
@@ -133,6 +134,8 @@ namespace Polycode {
 		
 		PolyKEY mapKey(int keyCode);
 		void extractResources();
+		
+		OpenSLAudioInterface* getAudioInterface();
 	private:
 		PolycodeView* view;
 		
@@ -150,10 +153,11 @@ namespace Polycode {
 		
 		std::vector<AndroidEvent> systemInputEvents;
 
-		double pcFreq;
+// 		double pcFreq;
 
 		OpenGLGraphicsInterface *graphicsInterface;
-
+		OpenSLAudioInterface* audioInterface;
+		
 		void initKeyMap();
 		PolyKEY keyMap[1024];
 	};

+ 1 - 1
include/polycode/core/PolyCocoaCore.h

@@ -65,7 +65,7 @@ namespace Polycode {
 		TouchInfo touch;
 		
 		PolyKEY keyCode;
-		wchar_t unicodeChar;
+        String text;
 		
 		char mouseButton;
 		

+ 4 - 2
include/polycode/core/PolyCoreInput.h

@@ -157,13 +157,15 @@ namespace Polycode {
 		void mouseWheelDown(int ticks);
 		void setMouseButtonState(int mouseButton, bool state, int ticks);
 		void setMousePosition(int x, int y, int ticks);
-		void setKeyState(PolyKEY keyCode, wchar_t code, bool newState, int ticks);
+		void setKeyState(PolyKEY keyCode, bool newState, int ticks);
 		void setDeltaPosition(int x, int y);
 		
 		void touchesBegan(TouchInfo touch, std::vector<TouchInfo> touches, int ticks);
 		void touchesMoved(TouchInfo touch, std::vector<TouchInfo> touches, int ticks);
 		void touchesEnded(TouchInfo touch, std::vector<TouchInfo> touches, int ticks);
-				
+		
+		void textInput(String text);
+		
 		static InputEvent *createEvent(Event *event){ return (InputEvent*)event; }
 		
 		/**

+ 3 - 1
include/polycode/core/PolyImage.h

@@ -275,8 +275,10 @@ namespace Polycode {
 		
 			static const int IMAGE_RGB = 0;
 			static const int IMAGE_RGBA = 1;
+#ifndef NO_FP16
 			static const int IMAGE_FP16 = 2;
-		
+#endif
+			
 		protected:
 		
 			bool loadHDR(const String &fileName);

+ 8 - 9
include/polycode/core/PolyInputEvent.h

@@ -57,8 +57,7 @@ namespace Polycode {
 		public:
 			InputEvent();
 			InputEvent(Vector2 mousePosition,int timestamp);
-//			InputEvent(PolyKEY key, int timestamp);
-			InputEvent(PolyKEY key, wchar_t charCode, int timestamp);			
+			InputEvent(PolyKEY key, int timestamp);			
 			virtual ~InputEvent();
 		
 			// ----------------------------------------------------------------------------------------------------------------
@@ -89,6 +88,8 @@ namespace Polycode {
 		static const int EVENT_TOUCHES_BEGAN = EVENTBASE_INPUTEVENT+20;
 		static const int EVENT_TOUCHES_MOVED = EVENTBASE_INPUTEVENT+21;
 		static const int EVENT_TOUCHES_ENDED = EVENTBASE_INPUTEVENT+22;
+		
+		static const int EVENT_TEXTINPUT = EVENTBASE_INPUTEVENT+23;
 
 		//@}
 		// ----------------------------------------------------------------------------------------------------------------
@@ -112,17 +113,15 @@ namespace Polycode {
 		*/		
 		PolyKEY key;
 		
-		
-		wchar_t getCharCode();
-		
 		int keyCode() { return key; }
 		
-		/**
-		* If this is a key press event, this will contain the unicode character that's being typed.
-		*/				
-		wchar_t charCode;
 		int timestamp;
 		
+		/**
+		 * If this is a text input event, this will contain the text with all modifiers applied. (will usually be only 1 character)
+		 */
+		String text;
+		
 		std::vector<TouchInfo> touches;
 		TouchInfo touch;
 		int touchType;

+ 1 - 1
include/polycode/core/PolyInputKeys.h

@@ -24,7 +24,7 @@ THE SOFTWARE.
 
 namespace Polycode {
 
-	// copied from SDL for conveniece :)
+	// copied from SDL 1 for conveniece :)
 
 	/**
 	* Keys values.

+ 17 - 5
include/polycode/core/PolySDLCore.h

@@ -25,7 +25,7 @@
 #include "PolyGlobals.h"
 #include "PolyCore.h"
 #include <vector>
-// #include <SDL2/SDL.h>
+#include <SDL2/SDL.h>
 
 #include "polycode/core/PolyPAAudioInterface.h"
 
@@ -59,7 +59,7 @@ namespace Polycode {
 		void flushRenderContext();
 		
 		void handleVideoModeChange(VideoModeChangeInfo *modeInfo);
-//		void setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, bool retinaSupport = true);
+
 		void createThread(Threaded *target);
 		std::vector<Rectangle> getVideoModes();
 		
@@ -67,8 +67,7 @@ namespace Polycode {
 				
 		void setCursor(int cursorType);
 		void warpCursor(int x, int y);
-		//void lockMutex(CoreMutex *mutex);
-		//void unlockMutex(CoreMutex *mutex);
+
 		CoreMutex *createMutex();
 		void copyStringToClipboard(const String& str);
 		String getClipboardString();
@@ -78,15 +77,25 @@ namespace Polycode {
 		void removeDiskItem(const String& itemPath);
 		String openFolderPicker();
 		std::vector<String> openFilePicker(std::vector<CoreFileExtension> extensions, bool allowMultiple);
-				String saveFilePicker(std::vector<CoreFileExtension> extensions);
+		String saveFilePicker(std::vector<CoreFileExtension> extensions);
 		void resizeTo(int xRes, int yRes);
 
 		String executeExternalCommand(String command, String args, String inDirectory="");
 		void openURL(String url);
+		
+		Number getBackingXRes();
+		Number getBackingYRes();
 
 	private:
+		void initKeymap();
+		PolyKEY mapKey(SDL_Scancode key);
 		bool checkSpecialKeyEvents(PolyKEY key);
 		
+		PolyKEY keyMap[512];
+		
+		int backingX;
+		int backingY;
+		
 		uint32_t flags;
 		bool resizableWindow;
 		
@@ -94,5 +103,8 @@ namespace Polycode {
 		
 		int lastMouseX;
 		int lastMouseY;
+		
+		SDL_GLContext sdlContext;
+		SDL_Window* sdlWindow;
 	};
 }

+ 57 - 44
include/polycode/core/PolyString.h

@@ -44,32 +44,32 @@ namespace Polycode {
 		
 			/**
 			* Default constructor
-			*/			
+			*/
 			String();
 			
 			/**
 			* Initializes the string from a pointer to wide character buffer.
-			*/			
+			*/
 			String(const wchar_t *str);
 			
 			/**
 			* Initializes the string from a pointer to regular character buffer of a certain size.
-			*/						
+			*/
 			String(const char *str, size_t n);
 			
 			/**
 			* Initializes the string from a regular character buffer.
-			*/									
+			*/
 			String(const char *str);
 			
 			/**
 			* Initializes the string from an STL string.
-			*/												
+			*/
 			String(const std::string& str);
 			
 			/**
 			* Initializes the string from an STL wstring.
-			*/															
+			*/
 			String(const std::wstring& str);
 			
 			String(const wchar_t wchar);
@@ -78,25 +78,25 @@ namespace Polycode {
 		
 			/**
 			* Return the length of the string.
-			*/														
+			*/
 			size_t size() const { return contents.size(); }
 			
 			/**
 			* Return the length of the string.
-			*/			
+			*/
 			size_t length() const { return contents.size(); }
 		
 			/**
 			* Return the string and an STL string.
-			*/		
+			*/
 			const std::string& getSTLString() const;
-					
+			
 			/**
 			* Returns the substring of the string.
 			* @param pos Position of a character in the current string object to be used as starting character for the substring.
 			* @param n Length of the substring.
 			* @return A string object containing a substring of the current object.
-			*/					
+			*/
 			String substr(size_t pos = 0, size_t n = std::wstring::npos) const { return String(contents.substr(pos,n)); }
 
 			/**
@@ -104,7 +104,7 @@ namespace Polycode {
 			* @param str String to be searched for in the object.
 			* @param pos Position of the last character in the string to be taken into consideration for possible matches. The default value indicates that the entire string is searched.
 			* @return The position of the last occurrence in the string of the searched content or -1 if not found
-			*/							
+			*/
 			size_t rfind ( const String &str, size_t pos = std::wstring::npos ) const { return contents.rfind(str.contents, pos); }
 			
 			/**
@@ -112,7 +112,7 @@ namespace Polycode {
 			* @param str String to be searched for in the object.
 			* @param pos Position of the first character in the string to be taken into consideration for possible matches. The default value indicates that the entire string is searched.
 			* @return The position of the first occurrence in the string of the searched content or -1 if not found.
-			*/										
+			*/
 			size_t find ( const String &str, size_t pos = 0 ) const { return contents.find(str.contents, pos); }
 			
 			/**
@@ -120,7 +120,7 @@ namespace Polycode {
 			* @param str String containing the characters to search for.
 			* @param pos Position of the last character in the string to be taken into consideration for possible matches. The default value indicates that the entire string is searched.
 			* @return The position of the last occurrence in the string of any of the characters searched for.
-			*/													
+			*/
 			size_t find_last_of(const String& str, size_t pos = std::wstring::npos ) { return contents.find_last_of(str.contents, pos); }
 		
 			
@@ -128,37 +128,47 @@ namespace Polycode {
 			* Find character in string from the beginning. Searches the string from the beginnign for any of the characters that are part of the passed string.
 			* @param str String containing the characters to search for.
 			* @param pos Position of the first character in the string to be taken into consideration for possible matches. The default value indicates that the entire string is searched.
-			* @return The position of the last occurrence in the string of any of the characters searched for.
-			*/			
+			* @return The position of the first occurrence in the string of any of the characters searched for.
+			*/
 			size_t find_first_of(const String &str, size_t pos = 0) {
-				return contents.find_first_of(str.contents, pos); 
+				return contents.find_first_of(str.contents, pos);
+			}
+			
+			/**
+			* Find character in string from the beginning. Searches the string from the beginning for any other than the characters that are part of the passed string.
+			* @param str String containing the characters to not search for.
+			* @param pos Position of the first character in the string to be taken into consideration for possible matches. The default value indicates that the entire string is searched.
+			* @return The position of the first occurrence in the string of any of the characters searched for.
+			*/
+			size_t find_first_not_of(const String &str, size_t pos = 0) {
+				return contents.find_first_not_of(str.contents, pos);
 			}
 		
-			inline String operator + (const char *str) const { return String(contents + String(str).contents); }		
-			inline String operator + (const String &str) const { return String(contents + str.contents); }		
-			String operator += (const String &str) { contents = contents + str.contents; return *this; }		
+			inline String operator + (const char *str) const { return String(contents + String(str).contents); }
+			inline String operator + (const String &str) const { return String(contents + str.contents); }
+			String operator += (const String &str) { contents = contents + str.contents; return *this; }
 			String operator = (const String &str) {	 contents = str.contents; return *this;}
-			inline bool operator == (const String &str) const {	 return (str.contents == contents); }		
-			inline bool operator != (const String &str) const {	 return (str.contents != contents); }		
+			inline bool operator == (const String &str) const {	 return (str.contents == contents); }
+			inline bool operator != (const String &str) const {	 return (str.contents != contents); }
 			inline wchar_t operator [] ( const size_t i ) const { return contents[i]; }
 
 			/**
 			* Returns the lowercase version of the string.
 			* @return Lowercase version of the stirng.
-			*/															
+			*/
 			String toLowerCase() const;
 			
 			/**
 			* Returns the uppercase version of the string.
 			* @return Uppercase version of the stirng.
-			*/																		
+			*/
 			String toUpperCase() const;
-					
+			
 			/**
 			* Splits the string by the specified delimeter
 			* @param delim The delimeter to split by.
 			* @return An STL vector of the split parts of the string. 
-			*/																				
+			*/
 			std::vector<String> split(const String &delim) const;
 
 			/**
@@ -166,14 +176,14 @@ namespace Polycode {
 			* @param what Ocurrences of which string to replace in this string.
 			* @param withWhat What to replace them with.
 			* @return A new string with the specified matches replaced with the specified string.
-			*/																							
+			*/
 			String replace(const String &what, const String &withWhat) const;
 			
 			/**
 			* Convert a Number to a String.
 			* @param value Number to convert.
 			* @return A string converted from the Number.
-			*/																										
+			*/
 			static String NumberToString(Number value, int precision = 2);
 		
 		
@@ -184,13 +194,13 @@ namespace Polycode {
 			* Convert an integer to a String.
 			* @param value Integer to convert.
 			* @return A string converted from the integer.
-			*/																												
+			*/
 			static String IntToString(int value);
 		
 			/**
 			* Pointer to char data.
 			* @return A pointer to char data.
-			*/																												
+			*/
 			const char *c_str() const;
 			
 			/**
@@ -198,7 +208,7 @@ namespace Polycode {
 			* @param encoding The encoding to use.
 			* @return A pointer to the data using specified encoding.
 			* @see getDataSizeWithEncoding()
-			*/																															
+			*/
 			const char *getDataWithEncoding(int encoding) const;
 			
 			/**
@@ -206,48 +216,51 @@ namespace Polycode {
 			* @param encoding The encoding to use.
 			* @return A pointer to the data using specified encoding.
 			* @see getDataSizeWithEncoding()
-			*/																															
+			*/
 			wchar_t *getWDataWithEncoding(int encoding);
 
+			void append(const char c);
 
 			/**
 			* Returns the size of the data with the specified encoding. Currently the only supported encoding is String::ENCODING_UTF8
 			* @param encoding The encoding to use.
 			* @return The size the data would take up if returned with this encoding.
-			* @see getDataWithEncoding()
-			*/																		
-			
-
-			void append(const char c);
-
-			size_t getDataSizeWithEncoding(int encoding) const;					
+			* @see getDataSizeWithEncoding()
+			*/
+			size_t getDataSizeWithEncoding(int encoding) const;
 			
 			/**
 			* Sets the data for the string using specified encoding.
 			* @param data Data to set the string with.
 			* @param encoding The encoding to use.
-			*/																																		
+			*/
 			void setDataWithEncoding(char *data, int encoding);
 			
 			/**
 			* Checks if the string is a number
 			* @return true if the string is a number
-			*/			
+			*/
 			bool isNumber();
 
+			/**
+			* Checks if the string is an integer
+			* @return true if the string is an integer
+			*/
+			bool isInteger();
+ 
 			/**
 			* STL string version of the string.
-			*/																																					
+			*/
 			std::string contents;
 
 			/**
 			* STL string version of the string.
-			*/																																					
+			*/
 			std::wstring w_contents;
 
 			/**
 			* UTF-8 encoding.
-			*/																																							
+			*/
 			static const int ENCODING_UTF8 = 0; 
 			
 		

+ 3 - 2
include/polycode/core/PolyWinCore.h

@@ -128,7 +128,7 @@ namespace Polycode {
 		std::vector<TouchInfo> touches;
 		int touchType;
 		PolyKEY keyCode;
-		wchar_t unicodeChar;		
+		String text;
 		char mouseButton;	
 		static const int EVENTBASE_PLATFORMEVENT = 0x300;
 		static const int INPUT_EVENT = EVENTBASE_PLATFORMEVENT+0;
@@ -197,7 +197,7 @@ public:
 		void Render();
 		void setVSync(bool vSyncVal);
 
-		void handleKeyDown(LPARAM lParam, WPARAM wParam, wchar_t unicodeChar);
+		void handleKeyDown(LPARAM lParam, WPARAM wParam);
 		void handleKeyUp(LPARAM lParam, WPARAM wParam);
 		void handleMouseMove(LPARAM lParam, WPARAM wParam);
 		void handleMouseWheel(LPARAM lParam, WPARAM wParam);
@@ -205,6 +205,7 @@ public:
 		void handleMouseUp(int mouseCode,LPARAM lParam, WPARAM wParam);
 		void handleTouchEvent(LPARAM lParam, WPARAM wParam);
 		void handlePointerUpdate(LPARAM lParam, WPARAM wParam);
+		void handleTextInput(LPARAM lParam, WPARAM wParam);
 
 		void handleVideoModeChange(VideoModeChangeInfo *modeInfo);
 

+ 4 - 1
include/polycode/ide/PolycodeTextEditor.h

@@ -69,11 +69,13 @@ class PolycodeSyntaxHighlighter : public UITextInputSyntaxHighlighter {
 		bool contains_char(char part, std::vector<char> *list);
 			
 		std::vector<SyntaxHighlightToken> parseText(String text, SyntaxHighlightToken overrideToken);
-		std::vector<SyntaxHighlightToken> parseLua(String text, SyntaxHighlightToken overrideToken);	
+		std::vector<SyntaxHighlightToken> parseLua(String text, SyntaxHighlightToken overrideToken);
+		std::vector<SyntaxHighlightToken> parseJS(String text, SyntaxHighlightToken overrideToken);
 		std::vector<SyntaxHighlightToken> parseGLSL(String text, SyntaxHighlightToken overrideToken);
 			
 		static const int MODE_LUA = 0;
 		static const int MODE_GLSL = 1;
+		static const int MODE_JS = 2;
 						
 	protected:
 
@@ -118,6 +120,7 @@ class PolycodeTextEditorFactory : public PolycodeEditorFactory {
 public:
 	PolycodeTextEditorFactory() : PolycodeEditorFactory() {
 		extensions.push_back("lua");
+		extensions.push_back("js");
 		extensions.push_back("txt");
 		extensions.push_back("xml");
 		extensions.push_back("vert");

+ 4 - 2
include/polycode/modules/ui/PolyUITextInput.h

@@ -183,8 +183,10 @@ namespace Polycode {
 			 */
 			int insertLine(String lineText = "");
 
-			void onKeyDown(PolyKEY key, wchar_t charCode);
-		
+			void onKeyDown(PolyKEY key);
+			
+			void onTextInput(String newText);
+			
 			/**
 			 * Clear the current selection.
 			 */

+ 1 - 1
include/polycode/modules/ui/PolyUITreeContainer.h

@@ -41,7 +41,7 @@ namespace Polycode {
 		UITree *getRootNode();
 		Entity *scrollChild;
 		
-		void onKeyDown(PolyKEY key, wchar_t charCode);
+		void onKeyDown(PolyKEY key);
 		void onGainFocus();
 		
 		/**

+ 1 - 1
include/polycode/modules/ui/PolyUIWindow.h

@@ -42,7 +42,7 @@ namespace Polycode {
 			
 			void showWindow();
 			void hideWindow();
-			void onKeyDown(PolyKEY key, wchar_t charCode);
+			void onKeyDown(PolyKEY key);
 			virtual void onClose();
 			void onLoseFocus();
 			

+ 3 - 0
include/polycode/view/android/PolycodeView.h

@@ -44,6 +44,8 @@
 #define LOGI(text) ((void)__android_log_write(ANDROID_LOG_INFO, "TemplateApp", text))
 #define LOGE(text) ((void)__android_log_write(ANDROID_LOG_ERROR, "TemplateApp", text))
 
+#define ACONFIGURATION_DENSITY_XXXHIGH 640
+
 namespace Polycode {
 	
 	enum{
@@ -115,3 +117,4 @@ void* startApp(void* data);
 int JNIGetUnicodeChar(ANativeActivity* native_activity, int eventType, int keyCode, int metaState);
 void JNIAutoHideNavBar(ANativeActivity* native_activity);
 void JNIWakeLock(ANativeActivity* native_activity, bool acquire);
+void JNIVolumeControl(ANativeActivity* native_activity, bool up);

+ 5267 - 0
include/stb_vorbis.h

@@ -0,0 +1,5267 @@
+// Ogg Vorbis audio decoder - v1.09 - public domain
+// http://nothings.org/stb_vorbis/
+//
+// Original version written by Sean Barrett in 2007.
+//
+// Originally sponsored by RAD Game Tools. Seeking sponsored
+// by Phillip Bennefall, Marc Andersen, Aaron Baker, Elias Software,
+// Aras Pranckevicius, and Sean Barrett.
+//
+// LICENSE
+//
+//   This software is dual-licensed to the public domain and under the following
+//   license: you are granted a perpetual, irrevocable license to copy, modify,
+//   publish, and distribute this file as you see fit.
+//
+// No warranty for any purpose is expressed or implied by the author (nor
+// by RAD Game Tools). Report bugs and send enhancements to the author.
+//
+// Limitations:
+//
+//   - floor 0 not supported (used in old ogg vorbis files pre-2004)
+//   - lossless sample-truncation at beginning ignored
+//   - cannot concatenate multiple vorbis streams
+//   - sample positions are 32-bit, limiting seekable 192Khz
+//       files to around 6 hours (Ogg supports 64-bit)
+//
+// Feature contributors:
+//    Dougall Johnson (sample-exact seeking)
+//
+// Bugfix/warning contributors:
+//    Terje Mathisen     Niklas Frykholm     Andy Hill
+//    Casey Muratori     John Bolton         Gargaj
+//    Laurent Gomila     Marc LeBlanc        Ronny Chevalier
+//    Bernhard Wodo      Evan Balster        alxprd@github
+//    Tom Beaumont       Ingo Leitgeb        Nicolas Guillemot
+//    Phillip Bennefall  Rohit               Thiago Goulart
+//    manxorist@github   saga musix
+//
+// Partial history:
+//    1.09    - 2016/04/04 - back out 'truncation of last frame' fix from previous version
+//    1.08    - 2016/04/02 - warnings; setup memory leaks; truncation of last frame
+//    1.07    - 2015/01/16 - fixes for crashes on invalid files; warning fixes; const
+//    1.06    - 2015/08/31 - full, correct support for seeking API (Dougall Johnson)
+//                           some crash fixes when out of memory or with corrupt files
+//                           fix some inappropriately signed shifts
+//    1.05    - 2015/04/19 - don't define __forceinline if it's redundant
+//    1.04    - 2014/08/27 - fix missing const-correct case in API
+//    1.03    - 2014/08/07 - warning fixes
+//    1.02    - 2014/07/09 - declare qsort comparison as explicitly _cdecl in Windows
+//    1.01    - 2014/06/18 - fix stb_vorbis_get_samples_float (interleaved was correct)
+//    1.0     - 2014/05/26 - fix memory leaks; fix warnings; fix bugs in >2-channel;
+//                           (API change) report sample rate for decode-full-file funcs
+//
+// See end of file for full version history.
+
+
+//////////////////////////////////////////////////////////////////////////////
+//
+//  HEADER BEGINS HERE
+//
+
+#ifndef STB_VORBIS_INCLUDE_STB_VORBIS_H
+#define STB_VORBIS_INCLUDE_STB_VORBIS_H
+
+#if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)
+#define STB_VORBIS_NO_STDIO 1
+#endif
+
+#ifndef STB_VORBIS_NO_STDIO
+#include <stdio.h>
+#include "polycode/core/PolyCoreServices.h"
+#include "polycode/core/PolyCore.h"
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+	///////////   THREAD SAFETY
+
+	// Individual stb_vorbis* handles are not thread-safe; you cannot decode from
+	// them from multiple threads at the same time. However, you can have multiple
+	// stb_vorbis* handles and decode from them independently in multiple thrads.
+
+
+	///////////   MEMORY ALLOCATION
+
+	// normally stb_vorbis uses malloc() to allocate memory at startup,
+	// and alloca() to allocate temporary memory during a frame on the
+	// stack. (Memory consumption will depend on the amount of setup
+	// data in the file and how you set the compile flags for speed
+	// vs. size. In my test files the maximal-size usage is ~150KB.)
+	//
+	// You can modify the wrapper functions in the source (setup_malloc,
+	// setup_temp_malloc, temp_malloc) to change this behavior, or you
+	// can use a simpler allocation model: you pass in a buffer from
+	// which stb_vorbis will allocate _all_ its memory (including the
+	// temp memory). "open" may fail with a VORBIS_outofmem if you
+	// do not pass in enough data; there is no way to determine how
+	// much you do need except to succeed (at which point you can
+	// query get_info to find the exact amount required. yes I know
+	// this is lame).
+	//
+	// If you pass in a non-NULL buffer of the type below, allocation
+	// will occur from it as described above. Otherwise just pass NULL
+	// to use malloc()/alloca()
+
+	typedef struct {
+		char *alloc_buffer;
+		int   alloc_buffer_length_in_bytes;
+	} stb_vorbis_alloc;
+
+
+	///////////   FUNCTIONS USEABLE WITH ALL INPUT MODES
+
+	typedef struct stb_vorbis stb_vorbis;
+
+	typedef struct {
+		unsigned int sample_rate;
+		int channels;
+
+		unsigned int setup_memory_required;
+		unsigned int setup_temp_memory_required;
+		unsigned int temp_memory_required;
+
+		int max_frame_size;
+	} stb_vorbis_info;
+
+	// get general information about the file
+	extern stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f);
+
+	// get the last error detected (clears it, too)
+	extern int stb_vorbis_get_error(stb_vorbis *f);
+
+	// close an ogg vorbis file and free all memory in use
+	extern void stb_vorbis_close(stb_vorbis *f);
+
+	// this function returns the offset (in samples) from the beginning of the
+	// file that will be returned by the next decode, if it is known, or -1
+	// otherwise. after a flush_pushdata() call, this may take a while before
+	// it becomes valid again.
+	// NOT WORKING YET after a seek with PULLDATA API
+	extern int stb_vorbis_get_sample_offset(stb_vorbis *f);
+
+	// returns the current seek point within the file, or offset from the beginning
+	// of the memory buffer. In pushdata mode it returns 0.
+	extern unsigned int stb_vorbis_get_file_offset(stb_vorbis *f);
+
+	///////////   PUSHDATA API
+
+#ifndef STB_VORBIS_NO_PUSHDATA_API
+
+	// this API allows you to get blocks of data from any source and hand
+	// them to stb_vorbis. you have to buffer them; stb_vorbis will tell
+	// you how much it used, and you have to give it the rest next time;
+	// and stb_vorbis may not have enough data to work with and you will
+	// need to give it the same data again PLUS more. Note that the Vorbis
+	// specification does not bound the size of an individual frame.
+
+	extern stb_vorbis *stb_vorbis_open_pushdata(
+		const unsigned char * datablock, int datablock_length_in_bytes,
+		int *datablock_memory_consumed_in_bytes,
+		int *error,
+		const stb_vorbis_alloc *alloc_buffer);
+	// create a vorbis decoder by passing in the initial data block containing
+	//    the ogg&vorbis headers (you don't need to do parse them, just provide
+	//    the first N bytes of the file--you're told if it's not enough, see below)
+	// on success, returns an stb_vorbis *, does not set error, returns the amount of
+	//    data parsed/consumed on this call in *datablock_memory_consumed_in_bytes;
+	// on failure, returns NULL on error and sets *error, does not change *datablock_memory_consumed
+	// if returns NULL and *error is VORBIS_need_more_data, then the input block was
+	//       incomplete and you need to pass in a larger block from the start of the file
+
+	extern int stb_vorbis_decode_frame_pushdata(
+		stb_vorbis *f,
+		const unsigned char *datablock, int datablock_length_in_bytes,
+		int *channels,             // place to write number of float * buffers
+		float ***output,           // place to write float ** array of float * buffers
+		int *samples               // place to write number of output samples
+	);
+	// decode a frame of audio sample data if possible from the passed-in data block
+	//
+	// return value: number of bytes we used from datablock
+	//
+	// possible cases:
+	//     0 bytes used, 0 samples output (need more data)
+	//     N bytes used, 0 samples output (resynching the stream, keep going)
+	//     N bytes used, M samples output (one frame of data)
+	// note that after opening a file, you will ALWAYS get one N-bytes,0-sample
+	// frame, because Vorbis always "discards" the first frame.
+	//
+	// Note that on resynch, stb_vorbis will rarely consume all of the buffer,
+	// instead only datablock_length_in_bytes-3 or less. This is because it wants
+	// to avoid missing parts of a page header if they cross a datablock boundary,
+	// without writing state-machiney code to record a partial detection.
+	//
+	// The number of channels returned are stored in *channels (which can be
+	// NULL--it is always the same as the number of channels reported by
+	// get_info). *output will contain an array of float* buffers, one per
+	// channel. In other words, (*output)[0][0] contains the first sample from
+	// the first channel, and (*output)[1][0] contains the first sample from
+	// the second channel.
+
+	extern void stb_vorbis_flush_pushdata(stb_vorbis *f);
+	// inform stb_vorbis that your next datablock will not be contiguous with
+	// previous ones (e.g. you've seeked in the data); future attempts to decode
+	// frames will cause stb_vorbis to resynchronize (as noted above), and
+	// once it sees a valid Ogg page (typically 4-8KB, as large as 64KB), it
+	// will begin decoding the _next_ frame.
+	//
+	// if you want to seek using pushdata, you need to seek in your file, then
+	// call stb_vorbis_flush_pushdata(), then start calling decoding, then once
+	// decoding is returning you data, call stb_vorbis_get_sample_offset, and
+	// if you don't like the result, seek your file again and repeat.
+#endif
+
+
+	//////////   PULLING INPUT API
+
+#ifndef STB_VORBIS_NO_PULLDATA_API
+	// This API assumes stb_vorbis is allowed to pull data from a source--
+	// either a block of memory containing the _entire_ vorbis stream, or a
+	// FILE * that you or it create, or possibly some other reading mechanism
+	// if you go modify the source to replace the FILE * case with some kind
+	// of callback to your code. (But if you don't support seeking, you may
+	// just want to go ahead and use pushdata.)
+
+#if !defined(STB_VORBIS_NO_STDIO) && !defined(STB_VORBIS_NO_INTEGER_CONVERSION)
+	extern int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output);
+#endif
+#if !defined(STB_VORBIS_NO_INTEGER_CONVERSION)
+	extern int stb_vorbis_decode_memory(const unsigned char *mem, int len, int *channels, int *sample_rate, short **output);
+#endif
+	// decode an entire file and output the data interleaved into a malloc()ed
+	// buffer stored in *output. The return value is the number of samples
+	// decoded, or -1 if the file could not be opened or was not an ogg vorbis file.
+	// When you're done with it, just free() the pointer returned in *output.
+
+	extern stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len,
+		int *error, const stb_vorbis_alloc *alloc_buffer);
+	// create an ogg vorbis decoder from an ogg vorbis stream in memory (note
+	// this must be the entire stream!). on failure, returns NULL and sets *error
+
+#ifndef STB_VORBIS_NO_STDIO
+	extern stb_vorbis * stb_vorbis_open_filename(const char *filename,
+		int *error, const stb_vorbis_alloc *alloc_buffer);
+	// create an ogg vorbis decoder from a filename via fopen(). on failure,
+	// returns NULL and sets *error (possibly to VORBIS_file_open_failure).
+
+	extern stb_vorbis * stb_vorbis_open_file(Polycode::CoreFile *f, int close_handle_on_close,
+		int *error, const stb_vorbis_alloc *alloc_buffer);
+	// create an ogg vorbis decoder from an open FILE *, looking for a stream at
+	// the _current_ seek point (ftell). on failure, returns NULL and sets *error.
+	// note that stb_vorbis must "own" this stream; if you seek it in between
+	// calls to stb_vorbis, it will become confused. Morever, if you attempt to
+	// perform stb_vorbis_seek_*() operations on this file, it will assume it
+	// owns the _entire_ rest of the file after the start point. Use the next
+	// function, stb_vorbis_open_file_section(), to limit it.
+
+	extern stb_vorbis * stb_vorbis_open_file_section(Polycode::CoreFile *f, int close_handle_on_close,
+		int *error, const stb_vorbis_alloc *alloc_buffer, unsigned int len);
+	// create an ogg vorbis decoder from an open FILE *, looking for a stream at
+	// the _current_ seek point (ftell); the stream will be of length 'len' bytes.
+	// on failure, returns NULL and sets *error. note that stb_vorbis must "own"
+	// this stream; if you seek it in between calls to stb_vorbis, it will become
+	// confused.
+#endif
+
+	extern int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number);
+	extern int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number);
+	// these functions seek in the Vorbis file to (approximately) 'sample_number'.
+	// after calling seek_frame(), the next call to get_frame_*() will include
+	// the specified sample. after calling stb_vorbis_seek(), the next call to
+	// stb_vorbis_get_samples_* will start with the specified sample. If you
+	// do not need to seek to EXACTLY the target sample when using get_samples_*,
+	// you can also use seek_frame().
+
+	extern void stb_vorbis_seek_start(stb_vorbis *f);
+	// this function is equivalent to stb_vorbis_seek(f,0)
+
+	extern unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f);
+	extern float        stb_vorbis_stream_length_in_seconds(stb_vorbis *f);
+	// these functions return the total length of the vorbis stream
+
+	extern int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output);
+	// decode the next frame and return the number of samples. the number of
+	// channels returned are stored in *channels (which can be NULL--it is always
+	// the same as the number of channels reported by get_info). *output will
+	// contain an array of float* buffers, one per channel. These outputs will
+	// be overwritten on the next call to stb_vorbis_get_frame_*.
+	//
+	// You generally should not intermix calls to stb_vorbis_get_frame_*()
+	// and stb_vorbis_get_samples_*(), since the latter calls the former.
+
+#ifndef STB_VORBIS_NO_INTEGER_CONVERSION
+	extern int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts);
+	extern int stb_vorbis_get_frame_short(stb_vorbis *f, int num_c, short **buffer, int num_samples);
+#endif
+	// decode the next frame and return the number of *samples* per channel.
+	// Note that for interleaved data, you pass in the number of shorts (the
+	// size of your array), but the return value is the number of samples per
+	// channel, not the total number of samples.
+	//
+	// The data is coerced to the number of channels you request according to the
+	// channel coercion rules (see below). You must pass in the size of your
+	// buffer(s) so that stb_vorbis will not overwrite the end of the buffer.
+	// The maximum buffer size needed can be gotten from get_info(); however,
+	// the Vorbis I specification implies an absolute maximum of 4096 samples
+	// per channel.
+
+	// Channel coercion rules:
+	//    Let M be the number of channels requested, and N the number of channels present,
+	//    and Cn be the nth channel; let stereo L be the sum of all L and center channels,
+	//    and stereo R be the sum of all R and center channels (channel assignment from the
+	//    vorbis spec).
+	//        M    N       output
+	//        1    k      sum(Ck) for all k
+	//        2    *      stereo L, stereo R
+	//        k    l      k > l, the first l channels, then 0s
+	//        k    l      k <= l, the first k channels
+	//    Note that this is not _good_ surround etc. mixing at all! It's just so
+	//    you get something useful.
+
+	extern int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats);
+	extern int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples);
+	// gets num_samples samples, not necessarily on a frame boundary--this requires
+	// buffering so you have to supply the buffers. DOES NOT APPLY THE COERCION RULES.
+	// Returns the number of samples stored per channel; it may be less than requested
+	// at the end of the file. If there are no more samples in the file, returns 0.
+
+#ifndef STB_VORBIS_NO_INTEGER_CONVERSION
+	extern int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts);
+	extern int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int num_samples);
+#endif
+	// gets num_samples samples, not necessarily on a frame boundary--this requires
+	// buffering so you have to supply the buffers. Applies the coercion rules above
+	// to produce 'channels' channels. Returns the number of samples stored per channel;
+	// it may be less than requested at the end of the file. If there are no more
+	// samples in the file, returns 0.
+
+#endif
+
+	////////   ERROR CODES
+
+	enum STBVorbisError {
+		VORBIS__no_error,
+
+		VORBIS_need_more_data = 1,             // not a real error
+
+		VORBIS_invalid_api_mixing,           // can't mix API modes
+		VORBIS_outofmem,                     // not enough memory
+		VORBIS_feature_not_supported,        // uses floor 0
+		VORBIS_too_many_channels,            // STB_VORBIS_MAX_CHANNELS is too small
+		VORBIS_file_open_failure,            // fopen() failed
+		VORBIS_seek_without_length,          // can't seek in unknown-length file
+
+		VORBIS_unexpected_eof = 10,            // file is truncated?
+		VORBIS_seek_invalid,                 // seek past EOF
+
+											 // decoding errors (corrupt/invalid stream) -- you probably
+											 // don't care about the exact details of these
+
+											 // vorbis errors:
+											 VORBIS_invalid_setup = 20,
+											 VORBIS_invalid_stream,
+
+											 // ogg errors:
+											 VORBIS_missing_capture_pattern = 30,
+											 VORBIS_invalid_stream_structure_version,
+											 VORBIS_continued_packet_flag_invalid,
+											 VORBIS_incorrect_stream_serial_number,
+											 VORBIS_invalid_first_page,
+											 VORBIS_bad_packet_type,
+											 VORBIS_cant_find_last_page,
+											 VORBIS_seek_failed
+	};
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // STB_VORBIS_INCLUDE_STB_VORBIS_H
+//
+//  HEADER ENDS HERE
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#ifndef STB_VORBIS_HEADER_ONLY
+
+// global configuration settings (e.g. set these in the project/makefile),
+// or just set them in this file at the top (although ideally the first few
+// should be visible when the header file is compiled too, although it's not
+// crucial)
+
+// STB_VORBIS_NO_PUSHDATA_API
+//     does not compile the code for the various stb_vorbis_*_pushdata()
+//     functions
+// #define STB_VORBIS_NO_PUSHDATA_API
+
+// STB_VORBIS_NO_PULLDATA_API
+//     does not compile the code for the non-pushdata APIs
+// #define STB_VORBIS_NO_PULLDATA_API
+
+// STB_VORBIS_NO_STDIO
+//     does not compile the code for the APIs that use FILE *s internally
+//     or externally (implied by STB_VORBIS_NO_PULLDATA_API)
+// #define STB_VORBIS_NO_STDIO
+
+// STB_VORBIS_NO_INTEGER_CONVERSION
+//     does not compile the code for converting audio sample data from
+//     float to integer (implied by STB_VORBIS_NO_PULLDATA_API)
+// #define STB_VORBIS_NO_INTEGER_CONVERSION
+
+// STB_VORBIS_NO_FAST_SCALED_FLOAT
+//      does not use a fast float-to-int trick to accelerate float-to-int on
+//      most platforms which requires endianness be defined correctly.
+//#define STB_VORBIS_NO_FAST_SCALED_FLOAT
+
+
+// STB_VORBIS_MAX_CHANNELS [number]
+//     globally define this to the maximum number of channels you need.
+//     The spec does not put a restriction on channels except that
+//     the count is stored in a byte, so 255 is the hard limit.
+//     Reducing this saves about 16 bytes per value, so using 16 saves
+//     (255-16)*16 or around 4KB. Plus anything other memory usage
+//     I forgot to account for. Can probably go as low as 8 (7.1 audio),
+//     6 (5.1 audio), or 2 (stereo only).
+#ifndef STB_VORBIS_MAX_CHANNELS
+#define STB_VORBIS_MAX_CHANNELS    16  // enough for anyone?
+#endif
+
+// STB_VORBIS_PUSHDATA_CRC_COUNT [number]
+//     after a flush_pushdata(), stb_vorbis begins scanning for the
+//     next valid page, without backtracking. when it finds something
+//     that looks like a page, it streams through it and verifies its
+//     CRC32. Should that validation fail, it keeps scanning. But it's
+//     possible that _while_ streaming through to check the CRC32 of
+//     one candidate page, it sees another candidate page. This #define
+//     determines how many "overlapping" candidate pages it can search
+//     at once. Note that "real" pages are typically ~4KB to ~8KB, whereas
+//     garbage pages could be as big as 64KB, but probably average ~16KB.
+//     So don't hose ourselves by scanning an apparent 64KB page and
+//     missing a ton of real ones in the interim; so minimum of 2
+#ifndef STB_VORBIS_PUSHDATA_CRC_COUNT
+#define STB_VORBIS_PUSHDATA_CRC_COUNT  4
+#endif
+
+// STB_VORBIS_FAST_HUFFMAN_LENGTH [number]
+//     sets the log size of the huffman-acceleration table.  Maximum
+//     supported value is 24. with larger numbers, more decodings are O(1),
+//     but the table size is larger so worse cache missing, so you'll have
+//     to probe (and try multiple ogg vorbis files) to find the sweet spot.
+#ifndef STB_VORBIS_FAST_HUFFMAN_LENGTH
+#define STB_VORBIS_FAST_HUFFMAN_LENGTH   10
+#endif
+
+// STB_VORBIS_FAST_BINARY_LENGTH [number]
+//     sets the log size of the binary-search acceleration table. this
+//     is used in similar fashion to the fast-huffman size to set initial
+//     parameters for the binary search
+
+// STB_VORBIS_FAST_HUFFMAN_INT
+//     The fast huffman tables are much more efficient if they can be
+//     stored as 16-bit results instead of 32-bit results. This restricts
+//     the codebooks to having only 65535 possible outcomes, though.
+//     (At least, accelerated by the huffman table.)
+#ifndef STB_VORBIS_FAST_HUFFMAN_INT
+#define STB_VORBIS_FAST_HUFFMAN_SHORT
+#endif
+
+// STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH
+//     If the 'fast huffman' search doesn't succeed, then stb_vorbis falls
+//     back on binary searching for the correct one. This requires storing
+//     extra tables with the huffman codes in sorted order. Defining this
+//     symbol trades off space for speed by forcing a linear search in the
+//     non-fast case, except for "sparse" codebooks.
+// #define STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH
+
+// STB_VORBIS_DIVIDES_IN_RESIDUE
+//     stb_vorbis precomputes the result of the scalar residue decoding
+//     that would otherwise require a divide per chunk. you can trade off
+//     space for time by defining this symbol.
+// #define STB_VORBIS_DIVIDES_IN_RESIDUE
+
+// STB_VORBIS_DIVIDES_IN_CODEBOOK
+//     vorbis VQ codebooks can be encoded two ways: with every case explicitly
+//     stored, or with all elements being chosen from a small range of values,
+//     and all values possible in all elements. By default, stb_vorbis expands
+//     this latter kind out to look like the former kind for ease of decoding,
+//     because otherwise an integer divide-per-vector-element is required to
+//     unpack the index. If you define STB_VORBIS_DIVIDES_IN_CODEBOOK, you can
+//     trade off storage for speed.
+//#define STB_VORBIS_DIVIDES_IN_CODEBOOK
+
+#ifdef STB_VORBIS_CODEBOOK_SHORTS
+#error "STB_VORBIS_CODEBOOK_SHORTS is no longer supported as it produced incorrect results for some input formats"
+#endif
+
+// STB_VORBIS_DIVIDE_TABLE
+//     this replaces small integer divides in the floor decode loop with
+//     table lookups. made less than 1% difference, so disabled by default.
+
+// STB_VORBIS_NO_INLINE_DECODE
+//     disables the inlining of the scalar codebook fast-huffman decode.
+//     might save a little codespace; useful for debugging
+// #define STB_VORBIS_NO_INLINE_DECODE
+
+// STB_VORBIS_NO_DEFER_FLOOR
+//     Normally we only decode the floor without synthesizing the actual
+//     full curve. We can instead synthesize the curve immediately. This
+//     requires more memory and is very likely slower, so I don't think
+//     you'd ever want to do it except for debugging.
+// #define STB_VORBIS_NO_DEFER_FLOOR
+
+
+
+
+//////////////////////////////////////////////////////////////////////////////
+
+#ifdef STB_VORBIS_NO_PULLDATA_API
+#define STB_VORBIS_NO_INTEGER_CONVERSION
+#define STB_VORBIS_NO_STDIO
+#endif
+
+#if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)
+#define STB_VORBIS_NO_STDIO 1
+#endif
+
+#ifndef STB_VORBIS_NO_INTEGER_CONVERSION
+#ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT
+
+// only need endianness for fast-float-to-int, which we don't
+// use for pushdata
+
+#ifndef STB_VORBIS_BIG_ENDIAN
+#define STB_VORBIS_ENDIAN  0
+#else
+#define STB_VORBIS_ENDIAN  1
+#endif
+
+#endif
+#endif
+
+
+#ifndef STB_VORBIS_NO_STDIO
+#include <stdio.h>
+#endif
+
+#ifndef STB_VORBIS_NO_CRT
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <math.h>
+#if !(defined(__APPLE__) || defined(MACOSX) || defined(macintosh) || defined(Macintosh))
+#include <malloc.h>
+#if defined(__linux__) || defined(__linux) || defined(__EMSCRIPTEN__)
+#include <alloca.h>
+#endif
+#endif
+#else // STB_VORBIS_NO_CRT
+#define NULL 0
+#define malloc(s)   0
+#define free(s)     ((void) 0)
+#define realloc(s)  0
+#endif // STB_VORBIS_NO_CRT
+
+#include <limits.h>
+
+#ifdef __MINGW32__
+// eff you mingw:
+//     "fixed":
+//         http://sourceforge.net/p/mingw-w64/mailman/message/32882927/
+//     "no that broke the build, reverted, who cares about C":
+//         http://sourceforge.net/p/mingw-w64/mailman/message/32890381/
+#ifdef __forceinline
+#undef __forceinline
+#endif
+#define __forceinline
+#elif !defined(_MSC_VER)
+#if __GNUC__
+#define __forceinline inline
+#else
+#define __forceinline
+#endif
+#endif
+
+#if STB_VORBIS_MAX_CHANNELS > 256
+#error "Value of STB_VORBIS_MAX_CHANNELS outside of allowed range"
+#endif
+
+#if STB_VORBIS_FAST_HUFFMAN_LENGTH > 24
+#error "Value of STB_VORBIS_FAST_HUFFMAN_LENGTH outside of allowed range"
+#endif
+
+
+#if 0
+#include <crtdbg.h>
+#define CHECK(f)   _CrtIsValidHeapPointer(f->channel_buffers[1])
+#else
+#define CHECK(f)   ((void) 0)
+#endif
+
+#define MAX_BLOCKSIZE_LOG  13   // from specification
+#define MAX_BLOCKSIZE      (1 << MAX_BLOCKSIZE_LOG)
+
+
+typedef unsigned char  uint8;
+typedef   signed char   int8;
+typedef unsigned short uint16;
+typedef   signed short  int16;
+typedef unsigned int   uint32;
+typedef   signed int    int32;
+
+#ifndef TRUE
+#define TRUE 1
+#define FALSE 0
+#endif
+
+typedef float codetype;
+
+// @NOTE
+//
+// Some arrays below are tagged "//varies", which means it's actually
+// a variable-sized piece of data, but rather than malloc I assume it's
+// small enough it's better to just allocate it all together with the
+// main thing
+//
+// Most of the variables are specified with the smallest size I could pack
+// them into. It might give better performance to make them all full-sized
+// integers. It should be safe to freely rearrange the structures or change
+// the sizes larger--nothing relies on silently truncating etc., nor the
+// order of variables.
+
+#define FAST_HUFFMAN_TABLE_SIZE   (1 << STB_VORBIS_FAST_HUFFMAN_LENGTH)
+#define FAST_HUFFMAN_TABLE_MASK   (FAST_HUFFMAN_TABLE_SIZE - 1)
+
+typedef struct {
+	int dimensions, entries;
+	uint8 *codeword_lengths;
+	float  minimum_value;
+	float  delta_value;
+	uint8  value_bits;
+	uint8  lookup_type;
+	uint8  sequence_p;
+	uint8  sparse;
+	uint32 lookup_values;
+	codetype *multiplicands;
+	uint32 *codewords;
+#ifdef STB_VORBIS_FAST_HUFFMAN_SHORT
+	int16  fast_huffman[FAST_HUFFMAN_TABLE_SIZE];
+#else
+	int32  fast_huffman[FAST_HUFFMAN_TABLE_SIZE];
+#endif
+	uint32 *sorted_codewords;
+	int    *sorted_values;
+	int     sorted_entries;
+} Codebook;
+
+typedef struct {
+	uint8 order;
+	uint16 rate;
+	uint16 bark_map_size;
+	uint8 amplitude_bits;
+	uint8 amplitude_offset;
+	uint8 number_of_books;
+	uint8 book_list[16]; // varies
+} Floor0;
+
+typedef struct {
+	uint8 partitions;
+	uint8 partition_class_list[32]; // varies
+	uint8 class_dimensions[16]; // varies
+	uint8 class_subclasses[16]; // varies
+	uint8 class_masterbooks[16]; // varies
+	int16 subclass_books[16][8]; // varies
+	uint16 Xlist[31 * 8 + 2]; // varies
+	uint8 sorted_order[31 * 8 + 2];
+	uint8 neighbors[31 * 8 + 2][2];
+	uint8 floor1_multiplier;
+	uint8 rangebits;
+	int values;
+} Floor1;
+
+typedef union {
+	Floor0 floor0;
+	Floor1 floor1;
+} Floor;
+
+typedef struct {
+	uint32 begin, end;
+	uint32 part_size;
+	uint8 classifications;
+	uint8 classbook;
+	uint8 **classdata;
+	int16(*residue_books)[8];
+} Residue;
+
+typedef struct {
+	uint8 magnitude;
+	uint8 angle;
+	uint8 mux;
+} MappingChannel;
+
+typedef struct {
+	uint16 coupling_steps;
+	MappingChannel *chan;
+	uint8  submaps;
+	uint8  submap_floor[15]; // varies
+	uint8  submap_residue[15]; // varies
+} Mapping;
+
+typedef struct {
+	uint8 blockflag;
+	uint8 mapping;
+	uint16 windowtype;
+	uint16 transformtype;
+} Mode;
+
+typedef struct {
+	uint32  goal_crc;    // expected crc if match
+	int     bytes_left;  // bytes left in packet
+	uint32  crc_so_far;  // running crc
+	int     bytes_done;  // bytes processed in _current_ chunk
+	uint32  sample_loc;  // granule pos encoded in page
+} CRCscan;
+
+typedef struct {
+	uint32 page_start, page_end;
+	uint32 last_decoded_sample;
+} ProbedPage;
+
+struct stb_vorbis {
+	// user-accessible info
+	unsigned int sample_rate;
+	int channels;
+
+	unsigned int setup_memory_required;
+	unsigned int temp_memory_required;
+	unsigned int setup_temp_memory_required;
+
+	// input config
+#ifndef STB_VORBIS_NO_STDIO
+	FILE *f;
+	uint32 f_start;
+	int close_on_free;
+#endif
+
+	uint8 *stream;
+	uint8 *stream_start;
+	uint8 *stream_end;
+
+	uint32 stream_len;
+
+	uint8  push_mode;
+
+	uint32 first_audio_page_offset;
+
+	ProbedPage p_first, p_last;
+
+	// memory management
+	stb_vorbis_alloc alloc;
+	int setup_offset;
+	int temp_offset;
+
+	// run-time results
+	int eof;
+	enum STBVorbisError error;
+
+	// user-useful data
+
+	// header info
+	int blocksize[2];
+	int blocksize_0, blocksize_1;
+	int codebook_count;
+	Codebook *codebooks;
+	int floor_count;
+	uint16 floor_types[64]; // varies
+	Floor *floor_config;
+	int residue_count;
+	uint16 residue_types[64]; // varies
+	Residue *residue_config;
+	int mapping_count;
+	Mapping *mapping;
+	int mode_count;
+	Mode mode_config[64];  // varies
+
+	uint32 total_samples;
+
+	// decode buffer
+	float *channel_buffers[STB_VORBIS_MAX_CHANNELS];
+	float *outputs[STB_VORBIS_MAX_CHANNELS];
+
+	float *previous_window[STB_VORBIS_MAX_CHANNELS];
+	int previous_length;
+
+#ifndef STB_VORBIS_NO_DEFER_FLOOR
+	int16 *finalY[STB_VORBIS_MAX_CHANNELS];
+#else
+	float *floor_buffers[STB_VORBIS_MAX_CHANNELS];
+#endif
+
+	uint32 current_loc; // sample location of next frame to decode
+	int    current_loc_valid;
+
+	// per-blocksize precomputed data
+
+	// twiddle factors
+	float *A[2], *B[2], *C[2];
+	float *window[2];
+	uint16 *bit_reverse[2];
+
+	// current page/packet/segment streaming info
+	uint32 serial; // stream serial number for verification
+	int last_page;
+	int segment_count;
+	uint8 segments[255];
+	uint8 page_flag;
+	uint8 bytes_in_seg;
+	uint8 first_decode;
+	int next_seg;
+	int last_seg;  // flag that we're on the last segment
+	int last_seg_which; // what was the segment number of the last seg?
+	uint32 acc;
+	int valid_bits;
+	int packet_bytes;
+	int end_seg_with_known_loc;
+	uint32 known_loc_for_packet;
+	int discard_samples_deferred;
+	uint32 samples_output;
+
+	// push mode scanning
+	int page_crc_tests; // only in push_mode: number of tests active; -1 if not searching
+#ifndef STB_VORBIS_NO_PUSHDATA_API
+	CRCscan scan[STB_VORBIS_PUSHDATA_CRC_COUNT];
+#endif
+
+	// sample-access
+	int channel_buffer_start;
+	int channel_buffer_end;
+};
+
+#if defined(STB_VORBIS_NO_PUSHDATA_API)
+#define IS_PUSH_MODE(f)   FALSE
+#elif defined(STB_VORBIS_NO_PULLDATA_API)
+#define IS_PUSH_MODE(f)   TRUE
+#else
+#define IS_PUSH_MODE(f)   ((f)->push_mode)
+#endif
+
+typedef struct stb_vorbis vorb;
+
+static int error(vorb *f, enum STBVorbisError e) {
+	f->error = e;
+	if (!f->eof && e != VORBIS_need_more_data) {
+		f->error = e; // breakpoint for debugging
+	}
+	return 0;
+}
+
+
+// these functions are used for allocating temporary memory
+// while decoding. if you can afford the stack space, use
+// alloca(); otherwise, provide a temp buffer and it will
+// allocate out of those.
+
+#define array_size_required(count,size)  (count*(sizeof(void *)+(size)))
+
+#define temp_alloc(f,size)              (f->alloc.alloc_buffer ? setup_temp_malloc(f,size) : alloca(size))
+#ifdef dealloca
+#define temp_free(f,p)                  (f->alloc.alloc_buffer ? 0 : dealloca(size))
+#else
+#define temp_free(f,p)                  0
+#endif
+#define temp_alloc_save(f)              ((f)->temp_offset)
+#define temp_alloc_restore(f,p)         ((f)->temp_offset = (p))
+
+#define temp_block_array(f,count,size)  make_block_array(temp_alloc(f,array_size_required(count,size)), count, size)
+
+// given a sufficiently large block of memory, make an array of pointers to subblocks of it
+static void *make_block_array(void *mem, int count, int size) {
+	int i;
+	void ** p = (void **) mem;
+	char *q = (char *) (p + count);
+	for (i = 0; i < count; ++i) {
+		p[i] = q;
+		q += size;
+	}
+	return p;
+}
+
+static void *setup_malloc(vorb *f, int sz) {
+	sz = (sz + 3) & ~3;
+	f->setup_memory_required += sz;
+	if (f->alloc.alloc_buffer) {
+		void *p = (char *) f->alloc.alloc_buffer + f->setup_offset;
+		if (f->setup_offset + sz > f->temp_offset) return NULL;
+		f->setup_offset += sz;
+		return p;
+	}
+	return sz ? malloc(sz) : NULL;
+}
+
+static void setup_free(vorb *f, void *p) {
+	if (f->alloc.alloc_buffer) return; // do nothing; setup mem is a stack
+	free(p);
+}
+
+static void *setup_temp_malloc(vorb *f, int sz) {
+	sz = (sz + 3) & ~3;
+	if (f->alloc.alloc_buffer) {
+		if (f->temp_offset - sz < f->setup_offset) return NULL;
+		f->temp_offset -= sz;
+		return (char *) f->alloc.alloc_buffer + f->temp_offset;
+	}
+	return malloc(sz);
+}
+
+static void setup_temp_free(vorb *f, void *p, int sz) {
+	if (f->alloc.alloc_buffer) {
+		f->temp_offset += (sz + 3)&~3;
+		return;
+	}
+	free(p);
+}
+
+#define CRC32_POLY    0x04c11db7   // from spec
+
+static uint32 crc_table[256];
+static void crc32_init(void) {
+	int i, j;
+	uint32 s;
+	for (i = 0; i < 256; i++) {
+		for (s = (uint32) i << 24, j = 0; j < 8; ++j)
+			s = (s << 1) ^ (s >= (1U << 31) ? CRC32_POLY : 0);
+		crc_table[i] = s;
+	}
+}
+
+static __forceinline uint32 crc32_update(uint32 crc, uint8 byte) {
+	return (crc << 8) ^ crc_table[byte ^ (crc >> 24)];
+}
+
+
+// used in setup, and for huffman that doesn't go fast path
+static unsigned int bit_reverse(unsigned int n) {
+	n = ((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1);
+	n = ((n & 0xCCCCCCCC) >> 2) | ((n & 0x33333333) << 2);
+	n = ((n & 0xF0F0F0F0) >> 4) | ((n & 0x0F0F0F0F) << 4);
+	n = ((n & 0xFF00FF00) >> 8) | ((n & 0x00FF00FF) << 8);
+	return (n >> 16) | (n << 16);
+}
+
+static float square(float x) {
+	return x*x;
+}
+
+// this is a weird definition of log2() for which log2(1) = 1, log2(2) = 2, log2(4) = 3
+// as required by the specification. fast(?) implementation from stb.h
+// @OPTIMIZE: called multiple times per-packet with "constants"; move to setup
+static int ilog(int32 n) {
+	static signed char log2_4[16] = {0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4};
+
+	// 2 compares if n < 16, 3 compares otherwise (4 if signed or n > 1<<29)
+	if (n < (1 << 14))
+		if (n < (1 << 4))        return     0 + log2_4[n];
+		else if (n < (1 << 9))      return  5 + log2_4[n >> 5];
+		else                     return 10 + log2_4[n >> 10];
+	else if (n < (1 << 24))
+		if (n < (1 << 19))      return 15 + log2_4[n >> 15];
+		else                     return 20 + log2_4[n >> 20];
+	else if (n < (1 << 29))      return 25 + log2_4[n >> 25];
+	else if (n < (1 << 31)) return 30 + log2_4[n >> 30];
+	else                return 0; // signed n returns 0
+}
+
+#ifndef M_PI
+#define M_PI  3.14159265358979323846264f  // from CRC
+#endif
+
+// code length assigned to a value with no huffman encoding
+#define NO_CODE   255
+
+/////////////////////// LEAF SETUP FUNCTIONS //////////////////////////
+//
+// these functions are only called at setup, and only a few times
+// per file
+
+static float float32_unpack(uint32 x) {
+	// from the specification
+	uint32 mantissa = x & 0x1fffff;
+	uint32 sign = x & 0x80000000;
+	uint32 exp = (x & 0x7fe00000) >> 21;
+	double res = sign ? -(double) mantissa : (double) mantissa;
+	return (float) ldexp((float) res, exp - 788);
+}
+
+
+// zlib & jpeg huffman tables assume that the output symbols
+// can either be arbitrarily arranged, or have monotonically
+// increasing frequencies--they rely on the lengths being sorted;
+// this makes for a very simple generation algorithm.
+// vorbis allows a huffman table with non-sorted lengths. This
+// requires a more sophisticated construction, since symbols in
+// order do not map to huffman codes "in order".
+static void add_entry(Codebook *c, uint32 huff_code, int symbol, int count, int len, uint32 *values) {
+	if (!c->sparse) {
+		c->codewords[symbol] = huff_code;
+	} else {
+		c->codewords[count] = huff_code;
+		c->codeword_lengths[count] = len;
+		values[count] = symbol;
+	}
+}
+
+static int compute_codewords(Codebook *c, uint8 *len, int n, uint32 *values) {
+	int i, k, m = 0;
+	uint32 available[32];
+
+	memset(available, 0, sizeof(available));
+	// find the first entry
+	for (k = 0; k < n; ++k) if (len[k] < NO_CODE) break;
+	if (k == n) { assert(c->sorted_entries == 0); return TRUE; }
+	// add to the list
+	add_entry(c, 0, k, m++, len[k], values);
+	// add all available leaves
+	for (i = 1; i <= len[k]; ++i)
+		available[i] = 1U << (32 - i);
+	// note that the above code treats the first case specially,
+	// but it's really the same as the following code, so they
+	// could probably be combined (except the initial code is 0,
+	// and I use 0 in available[] to mean 'empty')
+	for (i = k + 1; i < n; ++i) {
+		uint32 res;
+		int z = len[i], y;
+		if (z == NO_CODE) continue;
+		// find lowest available leaf (should always be earliest,
+		// which is what the specification calls for)
+		// note that this property, and the fact we can never have
+		// more than one free leaf at a given level, isn't totally
+		// trivial to prove, but it seems true and the assert never
+		// fires, so!
+		while (z > 0 && !available[z]) --z;
+		if (z == 0) { return FALSE; }
+		res = available[z];
+		assert(z >= 0 && z < 32);
+		available[z] = 0;
+		add_entry(c, bit_reverse(res), i, m++, len[i], values);
+		// propogate availability up the tree
+		if (z != len[i]) {
+			assert(len[i] >= 0 && len[i] < 32);
+			for (y = len[i]; y > z; --y) {
+				assert(available[y] == 0);
+				available[y] = res + (1 << (32 - y));
+			}
+		}
+	}
+	return TRUE;
+}
+
+// accelerated huffman table allows fast O(1) match of all symbols
+// of length <= STB_VORBIS_FAST_HUFFMAN_LENGTH
+static void compute_accelerated_huffman(Codebook *c) {
+	int i, len;
+	for (i = 0; i < FAST_HUFFMAN_TABLE_SIZE; ++i)
+		c->fast_huffman[i] = -1;
+
+	len = c->sparse ? c->sorted_entries : c->entries;
+#ifdef STB_VORBIS_FAST_HUFFMAN_SHORT
+	if (len > 32767) len = 32767; // largest possible value we can encode!
+#endif
+	for (i = 0; i < len; ++i) {
+		if (c->codeword_lengths[i] <= STB_VORBIS_FAST_HUFFMAN_LENGTH) {
+			uint32 z = c->sparse ? bit_reverse(c->sorted_codewords[i]) : c->codewords[i];
+			// set table entries for all bit combinations in the higher bits
+			while (z < FAST_HUFFMAN_TABLE_SIZE) {
+				c->fast_huffman[z] = i;
+				z += 1 << c->codeword_lengths[i];
+			}
+		}
+	}
+}
+
+#ifdef _MSC_VER
+#define STBV_CDECL __cdecl
+#else
+#define STBV_CDECL
+#endif
+
+static int STBV_CDECL uint32_compare(const void *p, const void *q) {
+	uint32 x = *(uint32 *) p;
+	uint32 y = *(uint32 *) q;
+	return x < y ? -1 : x > y;
+}
+
+static int include_in_sort(Codebook *c, uint8 len) {
+	if (c->sparse) { assert(len != NO_CODE); return TRUE; }
+	if (len == NO_CODE) return FALSE;
+	if (len > STB_VORBIS_FAST_HUFFMAN_LENGTH) return TRUE;
+	return FALSE;
+}
+
+// if the fast table above doesn't work, we want to binary
+// search them... need to reverse the bits
+static void compute_sorted_huffman(Codebook *c, uint8 *lengths, uint32 *values) {
+	int i, len;
+	// build a list of all the entries
+	// OPTIMIZATION: don't include the short ones, since they'll be caught by FAST_HUFFMAN.
+	// this is kind of a frivolous optimization--I don't see any performance improvement,
+	// but it's like 4 extra lines of code, so.
+	if (!c->sparse) {
+		int k = 0;
+		for (i = 0; i < c->entries; ++i)
+			if (include_in_sort(c, lengths[i]))
+				c->sorted_codewords[k++] = bit_reverse(c->codewords[i]);
+		assert(k == c->sorted_entries);
+	} else {
+		for (i = 0; i < c->sorted_entries; ++i)
+			c->sorted_codewords[i] = bit_reverse(c->codewords[i]);
+	}
+
+	qsort(c->sorted_codewords, c->sorted_entries, sizeof(c->sorted_codewords[0]), uint32_compare);
+	c->sorted_codewords[c->sorted_entries] = 0xffffffff;
+
+	len = c->sparse ? c->sorted_entries : c->entries;
+	// now we need to indicate how they correspond; we could either
+	//   #1: sort a different data structure that says who they correspond to
+	//   #2: for each sorted entry, search the original list to find who corresponds
+	//   #3: for each original entry, find the sorted entry
+	// #1 requires extra storage, #2 is slow, #3 can use binary search!
+	for (i = 0; i < len; ++i) {
+		int huff_len = c->sparse ? lengths[values[i]] : lengths[i];
+		if (include_in_sort(c, huff_len)) {
+			uint32 code = bit_reverse(c->codewords[i]);
+			int x = 0, n = c->sorted_entries;
+			while (n > 1) {
+				// invariant: sc[x] <= code < sc[x+n]
+				int m = x + (n >> 1);
+				if (c->sorted_codewords[m] <= code) {
+					x = m;
+					n -= (n >> 1);
+				} else {
+					n >>= 1;
+				}
+			}
+			assert(c->sorted_codewords[x] == code);
+			if (c->sparse) {
+				c->sorted_values[x] = values[i];
+				c->codeword_lengths[x] = huff_len;
+			} else {
+				c->sorted_values[x] = i;
+			}
+		}
+	}
+}
+
+// only run while parsing the header (3 times)
+static int vorbis_validate(uint8 *data) {
+	static uint8 vorbis[6] = {'v', 'o', 'r', 'b', 'i', 's'};
+	return memcmp(data, vorbis, 6) == 0;
+}
+
+// called from setup only, once per code book
+// (formula implied by specification)
+static int lookup1_values(int entries, int dim) {
+	int r = (int) floor(exp((float) log((float) entries) / dim));
+	if ((int) floor(pow((float) r + 1, dim)) <= entries)   // (int) cast for MinGW warning;
+		++r;                                              // floor() to avoid _ftol() when non-CRT
+	assert(pow((float) r + 1, dim) > entries);
+	assert((int) floor(pow((float) r, dim)) <= entries); // (int),floor() as above
+	return r;
+}
+
+// called twice per file
+static void compute_twiddle_factors(int n, float *A, float *B, float *C) {
+	int n4 = n >> 2, n8 = n >> 3;
+	int k, k2;
+
+	for (k = k2 = 0; k < n4; ++k, k2 += 2) {
+		A[k2] = (float) cos(4 * k*M_PI / n);
+		A[k2 + 1] = (float) -sin(4 * k*M_PI / n);
+		B[k2] = (float) cos((k2 + 1)*M_PI / n / 2) * 0.5f;
+		B[k2 + 1] = (float) sin((k2 + 1)*M_PI / n / 2) * 0.5f;
+	}
+	for (k = k2 = 0; k < n8; ++k, k2 += 2) {
+		C[k2] = (float) cos(2 * (k2 + 1)*M_PI / n);
+		C[k2 + 1] = (float) -sin(2 * (k2 + 1)*M_PI / n);
+	}
+}
+
+static void compute_window(int n, float *window) {
+	int n2 = n >> 1, i;
+	for (i = 0; i < n2; ++i)
+		window[i] = (float) sin(0.5 * M_PI * square((float) sin((i - 0 + 0.5) / n2 * 0.5 * M_PI)));
+}
+
+static void compute_bitreverse(int n, uint16 *rev) {
+	int ld = ilog(n) - 1; // ilog is off-by-one from normal definitions
+	int i, n8 = n >> 3;
+	for (i = 0; i < n8; ++i)
+		rev[i] = (bit_reverse(i) >> (32 - ld + 3)) << 2;
+}
+
+static int init_blocksize(vorb *f, int b, int n) {
+	int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3;
+	f->A[b] = (float *) setup_malloc(f, sizeof(float) * n2);
+	f->B[b] = (float *) setup_malloc(f, sizeof(float) * n2);
+	f->C[b] = (float *) setup_malloc(f, sizeof(float) * n4);
+	if (!f->A[b] || !f->B[b] || !f->C[b]) return error(f, VORBIS_outofmem);
+	compute_twiddle_factors(n, f->A[b], f->B[b], f->C[b]);
+	f->window[b] = (float *) setup_malloc(f, sizeof(float) * n2);
+	if (!f->window[b]) return error(f, VORBIS_outofmem);
+	compute_window(n, f->window[b]);
+	f->bit_reverse[b] = (uint16 *) setup_malloc(f, sizeof(uint16) * n8);
+	if (!f->bit_reverse[b]) return error(f, VORBIS_outofmem);
+	compute_bitreverse(n, f->bit_reverse[b]);
+	return TRUE;
+}
+
+static void neighbors(uint16 *x, int n, int *plow, int *phigh) {
+	int low = -1;
+	int high = 65536;
+	int i;
+	for (i = 0; i < n; ++i) {
+		if (x[i] > low  && x[i] < x[n]) { *plow = i; low = x[i]; }
+		if (x[i] < high && x[i] > x[n]) { *phigh = i; high = x[i]; }
+	}
+}
+
+// this has been repurposed so y is now the original index instead of y
+typedef struct {
+	uint16 x, y;
+} Point;
+
+static int STBV_CDECL point_compare(const void *p, const void *q) {
+	Point *a = (Point *) p;
+	Point *b = (Point *) q;
+	return a->x < b->x ? -1 : a->x > b->x;
+}
+
+//
+/////////////////////// END LEAF SETUP FUNCTIONS //////////////////////////
+
+
+#if defined(STB_VORBIS_NO_STDIO)
+#define USE_MEMORY(z)    TRUE
+#else
+#define USE_MEMORY(z)    ((z)->stream)
+#endif
+
+static uint8 get8(vorb *z) {
+	if (USE_MEMORY(z)) {
+		if (z->stream >= z->stream_end) { z->eof = TRUE; return 0; }
+		return *z->stream++;
+	}
+
+#ifndef STB_VORBIS_NO_STDIO
+	{
+		int c = fgetc(z->f);
+		if (c == EOF) { z->eof = TRUE; return 0; }
+		return c;
+	}
+#endif
+}
+
+static uint32 get32(vorb *f) {
+	uint32 x;
+	x = get8(f);
+	x += get8(f) << 8;
+	x += get8(f) << 16;
+	x += (uint32) get8(f) << 24;
+	return x;
+}
+
+static int getn(vorb *z, uint8 *data, int n) {
+	if (USE_MEMORY(z)) {
+		if (z->stream + n > z->stream_end) { z->eof = 1; return 0; }
+		memcpy(data, z->stream, n);
+		z->stream += n;
+		return 1;
+	}
+
+#ifndef STB_VORBIS_NO_STDIO   
+	if (fread(data, n, 1, z->f) == 1)
+		return 1;
+	else {
+		z->eof = 1;
+		return 0;
+	}
+#endif
+}
+
+static void skip(vorb *z, int n) {
+	if (USE_MEMORY(z)) {
+		z->stream += n;
+		if (z->stream >= z->stream_end) z->eof = 1;
+		return;
+	}
+#ifndef STB_VORBIS_NO_STDIO
+	{
+		long x = ftell(z->f);
+		fseek(z->f, x + n, SEEK_SET);
+	}
+#endif
+}
+
+static int set_file_offset(stb_vorbis *f, unsigned int loc) {
+#ifndef STB_VORBIS_NO_PUSHDATA_API
+	if (f->push_mode) return 0;
+#endif
+	f->eof = 0;
+	if (USE_MEMORY(f)) {
+		if (f->stream_start + loc >= f->stream_end || f->stream_start + loc < f->stream_start) {
+			f->stream = f->stream_end;
+			f->eof = 1;
+			return 0;
+		} else {
+			f->stream = f->stream_start + loc;
+			return 1;
+		}
+	}
+#ifndef STB_VORBIS_NO_STDIO
+	if (loc + f->f_start < loc || loc >= 0x80000000) {
+		loc = 0x7fffffff;
+		f->eof = 1;
+	} else {
+		loc += f->f_start;
+	}
+	if (!fseek(f->f, loc, SEEK_SET))
+		return 1;
+	f->eof = 1;
+	fseek(f->f, f->f_start, SEEK_END);
+	return 0;
+#endif
+}
+
+
+static uint8 ogg_page_header[4] = {0x4f, 0x67, 0x67, 0x53};
+
+static int capture_pattern(vorb *f) {
+	if (0x4f != get8(f)) return FALSE;
+	if (0x67 != get8(f)) return FALSE;
+	if (0x67 != get8(f)) return FALSE;
+	if (0x53 != get8(f)) return FALSE;
+	return TRUE;
+}
+
+#define PAGEFLAG_continued_packet   1
+#define PAGEFLAG_first_page         2
+#define PAGEFLAG_last_page          4
+
+static int start_page_no_capturepattern(vorb *f) {
+	uint32 loc0, loc1, n;
+	// stream structure version
+	if (0 != get8(f)) return error(f, VORBIS_invalid_stream_structure_version);
+	// header flag
+	f->page_flag = get8(f);
+	// absolute granule position
+	loc0 = get32(f);
+	loc1 = get32(f);
+	// @TODO: validate loc0,loc1 as valid positions?
+	// stream serial number -- vorbis doesn't interleave, so discard
+	get32(f);
+	//if (f->serial != get32(f)) return error(f, VORBIS_incorrect_stream_serial_number);
+	// page sequence number
+	n = get32(f);
+	f->last_page = n;
+	// CRC32
+	get32(f);
+	// page_segments
+	f->segment_count = get8(f);
+	if (!getn(f, f->segments, f->segment_count))
+		return error(f, VORBIS_unexpected_eof);
+	// assume we _don't_ know any the sample position of any segments
+	f->end_seg_with_known_loc = -2;
+	if (loc0 != ~0U || loc1 != ~0U) {
+		int i;
+		// determine which packet is the last one that will complete
+		for (i = f->segment_count - 1; i >= 0; --i)
+			if (f->segments[i] < 255)
+				break;
+		// 'i' is now the index of the _last_ segment of a packet that ends
+		if (i >= 0) {
+			f->end_seg_with_known_loc = i;
+			f->known_loc_for_packet = loc0;
+		}
+	}
+	if (f->first_decode) {
+		int i, len;
+		ProbedPage p;
+		len = 0;
+		for (i = 0; i < f->segment_count; ++i)
+			len += f->segments[i];
+		len += 27 + f->segment_count;
+		p.page_start = f->first_audio_page_offset;
+		p.page_end = p.page_start + len;
+		p.last_decoded_sample = loc0;
+		f->p_first = p;
+	}
+	f->next_seg = 0;
+	return TRUE;
+}
+
+static int start_page(vorb *f) {
+	if (!capture_pattern(f)) return error(f, VORBIS_missing_capture_pattern);
+	return start_page_no_capturepattern(f);
+}
+
+static int start_packet(vorb *f) {
+	while (f->next_seg == -1) {
+		if (!start_page(f)) return FALSE;
+		if (f->page_flag & PAGEFLAG_continued_packet)
+			return error(f, VORBIS_continued_packet_flag_invalid);
+	}
+	f->last_seg = FALSE;
+	f->valid_bits = 0;
+	f->packet_bytes = 0;
+	f->bytes_in_seg = 0;
+	// f->next_seg is now valid
+	return TRUE;
+}
+
+static int maybe_start_packet(vorb *f) {
+	if (f->next_seg == -1) {
+		int x = get8(f);
+		if (f->eof) return FALSE; // EOF at page boundary is not an error!
+		if (0x4f != x) return error(f, VORBIS_missing_capture_pattern);
+		if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern);
+		if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern);
+		if (0x53 != get8(f)) return error(f, VORBIS_missing_capture_pattern);
+		if (!start_page_no_capturepattern(f)) return FALSE;
+		if (f->page_flag & PAGEFLAG_continued_packet) {
+			// set up enough state that we can read this packet if we want,
+			// e.g. during recovery
+			f->last_seg = FALSE;
+			f->bytes_in_seg = 0;
+			return error(f, VORBIS_continued_packet_flag_invalid);
+		}
+	}
+	return start_packet(f);
+}
+
+static int next_segment(vorb *f) {
+	int len;
+	if (f->last_seg) return 0;
+	if (f->next_seg == -1) {
+		f->last_seg_which = f->segment_count - 1; // in case start_page fails
+		if (!start_page(f)) { f->last_seg = 1; return 0; }
+		if (!(f->page_flag & PAGEFLAG_continued_packet)) return error(f, VORBIS_continued_packet_flag_invalid);
+	}
+	len = f->segments[f->next_seg++];
+	if (len < 255) {
+		f->last_seg = TRUE;
+		f->last_seg_which = f->next_seg - 1;
+	}
+	if (f->next_seg >= f->segment_count)
+		f->next_seg = -1;
+	assert(f->bytes_in_seg == 0);
+	f->bytes_in_seg = len;
+	return len;
+}
+
+#define EOP    (-1)
+#define INVALID_BITS  (-1)
+
+static int get8_packet_raw(vorb *f) {
+	if (!f->bytes_in_seg) {  // CLANG!
+		if (f->last_seg) return EOP;
+		else if (!next_segment(f)) return EOP;
+	}
+	assert(f->bytes_in_seg > 0);
+	--f->bytes_in_seg;
+	++f->packet_bytes;
+	return get8(f);
+}
+
+static int get8_packet(vorb *f) {
+	int x = get8_packet_raw(f);
+	f->valid_bits = 0;
+	return x;
+}
+
+static void flush_packet(vorb *f) {
+	while (get8_packet_raw(f) != EOP);
+}
+
+// @OPTIMIZE: this is the secondary bit decoder, so it's probably not as important
+// as the huffman decoder?
+static uint32 get_bits(vorb *f, int n) {
+	uint32 z;
+
+	if (f->valid_bits < 0) return 0;
+	if (f->valid_bits < n) {
+		if (n > 24) {
+			// the accumulator technique below would not work correctly in this case
+			z = get_bits(f, 24);
+			z += get_bits(f, n - 24) << 24;
+			return z;
+		}
+		if (f->valid_bits == 0) f->acc = 0;
+		while (f->valid_bits < n) {
+			int z = get8_packet_raw(f);
+			if (z == EOP) {
+				f->valid_bits = INVALID_BITS;
+				return 0;
+			}
+			f->acc += z << f->valid_bits;
+			f->valid_bits += 8;
+		}
+	}
+	if (f->valid_bits < 0) return 0;
+	z = f->acc & ((1 << n) - 1);
+	f->acc >>= n;
+	f->valid_bits -= n;
+	return z;
+}
+
+// @OPTIMIZE: primary accumulator for huffman
+// expand the buffer to as many bits as possible without reading off end of packet
+// it might be nice to allow f->valid_bits and f->acc to be stored in registers,
+// e.g. cache them locally and decode locally
+static __forceinline void prep_huffman(vorb *f) {
+	if (f->valid_bits <= 24) {
+		if (f->valid_bits == 0) f->acc = 0;
+		do {
+			int z;
+			if (f->last_seg && !f->bytes_in_seg) return;
+			z = get8_packet_raw(f);
+			if (z == EOP) return;
+			f->acc += (unsigned) z << f->valid_bits;
+			f->valid_bits += 8;
+		} while (f->valid_bits <= 24);
+	}
+}
+
+enum {
+	VORBIS_packet_id = 1,
+	VORBIS_packet_comment = 3,
+	VORBIS_packet_setup = 5
+};
+
+static int codebook_decode_scalar_raw(vorb *f, Codebook *c) {
+	int i;
+	prep_huffman(f);
+
+	if (c->codewords == NULL && c->sorted_codewords == NULL)
+		return -1;
+
+	// cases to use binary search: sorted_codewords && !c->codewords
+	//                             sorted_codewords && c->entries > 8
+	if (c->entries > 8 ? c->sorted_codewords != NULL : !c->codewords) {
+		// binary search
+		uint32 code = bit_reverse(f->acc);
+		int x = 0, n = c->sorted_entries, len;
+
+		while (n > 1) {
+			// invariant: sc[x] <= code < sc[x+n]
+			int m = x + (n >> 1);
+			if (c->sorted_codewords[m] <= code) {
+				x = m;
+				n -= (n >> 1);
+			} else {
+				n >>= 1;
+			}
+		}
+		// x is now the sorted index
+		if (!c->sparse) x = c->sorted_values[x];
+		// x is now sorted index if sparse, or symbol otherwise
+		len = c->codeword_lengths[x];
+		if (f->valid_bits >= len) {
+			f->acc >>= len;
+			f->valid_bits -= len;
+			return x;
+		}
+
+		f->valid_bits = 0;
+		return -1;
+	}
+
+	// if small, linear search
+	assert(!c->sparse);
+	for (i = 0; i < c->entries; ++i) {
+		if (c->codeword_lengths[i] == NO_CODE) continue;
+		if (c->codewords[i] == (f->acc & ((1 << c->codeword_lengths[i]) - 1))) {
+			if (f->valid_bits >= c->codeword_lengths[i]) {
+				f->acc >>= c->codeword_lengths[i];
+				f->valid_bits -= c->codeword_lengths[i];
+				return i;
+			}
+			f->valid_bits = 0;
+			return -1;
+		}
+	}
+
+	error(f, VORBIS_invalid_stream);
+	f->valid_bits = 0;
+	return -1;
+}
+
+#ifndef STB_VORBIS_NO_INLINE_DECODE
+
+#define DECODE_RAW(var, f,c)                                  \
+   if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH)        \
+      prep_huffman(f);                                        \
+   var = f->acc & FAST_HUFFMAN_TABLE_MASK;                    \
+   var = c->fast_huffman[var];                                \
+   if (var >= 0) {                                            \
+      int n = c->codeword_lengths[var];                       \
+      f->acc >>= n;                                           \
+      f->valid_bits -= n;                                     \
+      if (f->valid_bits < 0) { f->valid_bits = 0; var = -1; } \
+   } else {                                                   \
+      var = codebook_decode_scalar_raw(f,c);                  \
+   }
+
+#else
+
+static int codebook_decode_scalar(vorb *f, Codebook *c) {
+	int i;
+	if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH)
+		prep_huffman(f);
+	// fast huffman table lookup
+	i = f->acc & FAST_HUFFMAN_TABLE_MASK;
+	i = c->fast_huffman[i];
+	if (i >= 0) {
+		f->acc >>= c->codeword_lengths[i];
+		f->valid_bits -= c->codeword_lengths[i];
+		if (f->valid_bits < 0) { f->valid_bits = 0; return -1; }
+		return i;
+	}
+	return codebook_decode_scalar_raw(f, c);
+}
+
+#define DECODE_RAW(var,f,c)    var = codebook_decode_scalar(f,c);
+
+#endif
+
+#define DECODE(var,f,c)                                       \
+   DECODE_RAW(var,f,c)                                        \
+   if (c->sparse) var = c->sorted_values[var];
+
+#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
+#define DECODE_VQ(var,f,c)   DECODE_RAW(var,f,c)
+#else
+#define DECODE_VQ(var,f,c)   DECODE(var,f,c)
+#endif
+
+
+
+
+
+
+// CODEBOOK_ELEMENT_FAST is an optimization for the CODEBOOK_FLOATS case
+// where we avoid one addition
+#define CODEBOOK_ELEMENT(c,off)          (c->multiplicands[off])
+#define CODEBOOK_ELEMENT_FAST(c,off)     (c->multiplicands[off])
+#define CODEBOOK_ELEMENT_BASE(c)         (0)
+
+static int codebook_decode_start(vorb *f, Codebook *c) {
+	int z = -1;
+
+	// type 0 is only legal in a scalar context
+	if (c->lookup_type == 0)
+		error(f, VORBIS_invalid_stream);
+	else {
+		DECODE_VQ(z, f, c);
+		if (c->sparse) assert(z < c->sorted_entries);
+		if (z < 0) {  // check for EOP
+			if (!f->bytes_in_seg)
+				if (f->last_seg)
+					return z;
+			error(f, VORBIS_invalid_stream);
+		}
+	}
+	return z;
+}
+
+static int codebook_decode(vorb *f, Codebook *c, float *output, int len) {
+	int i, z = codebook_decode_start(f, c);
+	if (z < 0) return FALSE;
+	if (len > c->dimensions) len = c->dimensions;
+
+#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
+	if (c->lookup_type == 1) {
+		float last = CODEBOOK_ELEMENT_BASE(c);
+		int div = 1;
+		for (i = 0; i < len; ++i) {
+			int off = (z / div) % c->lookup_values;
+			float val = CODEBOOK_ELEMENT_FAST(c, off) + last;
+			output[i] += val;
+			if (c->sequence_p) last = val + c->minimum_value;
+			div *= c->lookup_values;
+		}
+		return TRUE;
+	}
+#endif
+
+	z *= c->dimensions;
+	if (c->sequence_p) {
+		float last = CODEBOOK_ELEMENT_BASE(c);
+		for (i = 0; i < len; ++i) {
+			float val = CODEBOOK_ELEMENT_FAST(c, z + i) + last;
+			output[i] += val;
+			last = val + c->minimum_value;
+		}
+	} else {
+		float last = CODEBOOK_ELEMENT_BASE(c);
+		for (i = 0; i < len; ++i) {
+			output[i] += CODEBOOK_ELEMENT_FAST(c, z + i) + last;
+		}
+	}
+
+	return TRUE;
+}
+
+static int codebook_decode_step(vorb *f, Codebook *c, float *output, int len, int step) {
+	int i, z = codebook_decode_start(f, c);
+	float last = CODEBOOK_ELEMENT_BASE(c);
+	if (z < 0) return FALSE;
+	if (len > c->dimensions) len = c->dimensions;
+
+#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
+	if (c->lookup_type == 1) {
+		int div = 1;
+		for (i = 0; i < len; ++i) {
+			int off = (z / div) % c->lookup_values;
+			float val = CODEBOOK_ELEMENT_FAST(c, off) + last;
+			output[i*step] += val;
+			if (c->sequence_p) last = val;
+			div *= c->lookup_values;
+		}
+		return TRUE;
+	}
+#endif
+
+	z *= c->dimensions;
+	for (i = 0; i < len; ++i) {
+		float val = CODEBOOK_ELEMENT_FAST(c, z + i) + last;
+		output[i*step] += val;
+		if (c->sequence_p) last = val;
+	}
+
+	return TRUE;
+}
+
+static int codebook_decode_deinterleave_repeat(vorb *f, Codebook *c, float **outputs, int ch, int *c_inter_p, int *p_inter_p, int len, int total_decode) {
+	int c_inter = *c_inter_p;
+	int p_inter = *p_inter_p;
+	int i, z, effective = c->dimensions;
+
+	// type 0 is only legal in a scalar context
+	if (c->lookup_type == 0)   return error(f, VORBIS_invalid_stream);
+
+	while (total_decode > 0) {
+		float last = CODEBOOK_ELEMENT_BASE(c);
+		DECODE_VQ(z, f, c);
+#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
+		assert(!c->sparse || z < c->sorted_entries);
+#endif
+		if (z < 0) {
+			if (!f->bytes_in_seg)
+				if (f->last_seg) return FALSE;
+			return error(f, VORBIS_invalid_stream);
+		}
+
+		// if this will take us off the end of the buffers, stop short!
+		// we check by computing the length of the virtual interleaved
+		// buffer (len*ch), our current offset within it (p_inter*ch)+(c_inter),
+		// and the length we'll be using (effective)
+		if (c_inter + p_inter*ch + effective > len * ch) {
+			effective = len*ch - (p_inter*ch - c_inter);
+		}
+
+#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
+		if (c->lookup_type == 1) {
+			int div = 1;
+			for (i = 0; i < effective; ++i) {
+				int off = (z / div) % c->lookup_values;
+				float val = CODEBOOK_ELEMENT_FAST(c, off) + last;
+				if (outputs[c_inter])
+					outputs[c_inter][p_inter] += val;
+				if (++c_inter == ch) { c_inter = 0; ++p_inter; }
+				if (c->sequence_p) last = val;
+				div *= c->lookup_values;
+			}
+		} else
+#endif
+		{
+			z *= c->dimensions;
+			if (c->sequence_p) {
+				for (i = 0; i < effective; ++i) {
+					float val = CODEBOOK_ELEMENT_FAST(c, z + i) + last;
+					if (outputs[c_inter])
+						outputs[c_inter][p_inter] += val;
+					if (++c_inter == ch) { c_inter = 0; ++p_inter; }
+					last = val;
+				}
+			} else {
+				for (i = 0; i < effective; ++i) {
+					float val = CODEBOOK_ELEMENT_FAST(c, z + i) + last;
+					if (outputs[c_inter])
+						outputs[c_inter][p_inter] += val;
+					if (++c_inter == ch) { c_inter = 0; ++p_inter; }
+				}
+			}
+		}
+
+		total_decode -= effective;
+	}
+	*c_inter_p = c_inter;
+	*p_inter_p = p_inter;
+	return TRUE;
+}
+
+static int predict_point(int x, int x0, int x1, int y0, int y1) {
+	int dy = y1 - y0;
+	int adx = x1 - x0;
+	// @OPTIMIZE: force int division to round in the right direction... is this necessary on x86?
+	int err = abs(dy) * (x - x0);
+	int off = err / adx;
+	return dy < 0 ? y0 - off : y0 + off;
+}
+
+// the following table is block-copied from the specification
+static float inverse_db_table[256] =
+{
+	1.0649863e-07f, 1.1341951e-07f, 1.2079015e-07f, 1.2863978e-07f,
+	1.3699951e-07f, 1.4590251e-07f, 1.5538408e-07f, 1.6548181e-07f,
+	1.7623575e-07f, 1.8768855e-07f, 1.9988561e-07f, 2.1287530e-07f,
+	2.2670913e-07f, 2.4144197e-07f, 2.5713223e-07f, 2.7384213e-07f,
+	2.9163793e-07f, 3.1059021e-07f, 3.3077411e-07f, 3.5226968e-07f,
+	3.7516214e-07f, 3.9954229e-07f, 4.2550680e-07f, 4.5315863e-07f,
+	4.8260743e-07f, 5.1396998e-07f, 5.4737065e-07f, 5.8294187e-07f,
+	6.2082472e-07f, 6.6116941e-07f, 7.0413592e-07f, 7.4989464e-07f,
+	7.9862701e-07f, 8.5052630e-07f, 9.0579828e-07f, 9.6466216e-07f,
+	1.0273513e-06f, 1.0941144e-06f, 1.1652161e-06f, 1.2409384e-06f,
+	1.3215816e-06f, 1.4074654e-06f, 1.4989305e-06f, 1.5963394e-06f,
+	1.7000785e-06f, 1.8105592e-06f, 1.9282195e-06f, 2.0535261e-06f,
+	2.1869758e-06f, 2.3290978e-06f, 2.4804557e-06f, 2.6416497e-06f,
+	2.8133190e-06f, 2.9961443e-06f, 3.1908506e-06f, 3.3982101e-06f,
+	3.6190449e-06f, 3.8542308e-06f, 4.1047004e-06f, 4.3714470e-06f,
+	4.6555282e-06f, 4.9580707e-06f, 5.2802740e-06f, 5.6234160e-06f,
+	5.9888572e-06f, 6.3780469e-06f, 6.7925283e-06f, 7.2339451e-06f,
+	7.7040476e-06f, 8.2047000e-06f, 8.7378876e-06f, 9.3057248e-06f,
+	9.9104632e-06f, 1.0554501e-05f, 1.1240392e-05f, 1.1970856e-05f,
+	1.2748789e-05f, 1.3577278e-05f, 1.4459606e-05f, 1.5399272e-05f,
+	1.6400004e-05f, 1.7465768e-05f, 1.8600792e-05f, 1.9809576e-05f,
+	2.1096914e-05f, 2.2467911e-05f, 2.3928002e-05f, 2.5482978e-05f,
+	2.7139006e-05f, 2.8902651e-05f, 3.0780908e-05f, 3.2781225e-05f,
+	3.4911534e-05f, 3.7180282e-05f, 3.9596466e-05f, 4.2169667e-05f,
+	4.4910090e-05f, 4.7828601e-05f, 5.0936773e-05f, 5.4246931e-05f,
+	5.7772202e-05f, 6.1526565e-05f, 6.5524908e-05f, 6.9783085e-05f,
+	7.4317983e-05f, 7.9147585e-05f, 8.4291040e-05f, 8.9768747e-05f,
+	9.5602426e-05f, 0.00010181521f, 0.00010843174f, 0.00011547824f,
+	0.00012298267f, 0.00013097477f, 0.00013948625f, 0.00014855085f,
+	0.00015820453f, 0.00016848555f, 0.00017943469f, 0.00019109536f,
+	0.00020351382f, 0.00021673929f, 0.00023082423f, 0.00024582449f,
+	0.00026179955f, 0.00027881276f, 0.00029693158f, 0.00031622787f,
+	0.00033677814f, 0.00035866388f, 0.00038197188f, 0.00040679456f,
+	0.00043323036f, 0.00046138411f, 0.00049136745f, 0.00052329927f,
+	0.00055730621f, 0.00059352311f, 0.00063209358f, 0.00067317058f,
+	0.00071691700f, 0.00076350630f, 0.00081312324f, 0.00086596457f,
+	0.00092223983f, 0.00098217216f, 0.0010459992f,  0.0011139742f,
+	0.0011863665f,  0.0012634633f,  0.0013455702f,  0.0014330129f,
+	0.0015261382f,  0.0016253153f,  0.0017309374f,  0.0018434235f,
+	0.0019632195f,  0.0020908006f,  0.0022266726f,  0.0023713743f,
+	0.0025254795f,  0.0026895994f,  0.0028643847f,  0.0030505286f,
+	0.0032487691f,  0.0034598925f,  0.0036847358f,  0.0039241906f,
+	0.0041792066f,  0.0044507950f,  0.0047400328f,  0.0050480668f,
+	0.0053761186f,  0.0057254891f,  0.0060975636f,  0.0064938176f,
+	0.0069158225f,  0.0073652516f,  0.0078438871f,  0.0083536271f,
+	0.0088964928f,  0.009474637f,   0.010090352f,   0.010746080f,
+	0.011444421f,   0.012188144f,   0.012980198f,   0.013823725f,
+	0.014722068f,   0.015678791f,   0.016697687f,   0.017782797f,
+	0.018938423f,   0.020169149f,   0.021479854f,   0.022875735f,
+	0.024362330f,   0.025945531f,   0.027631618f,   0.029427276f,
+	0.031339626f,   0.033376252f,   0.035545228f,   0.037855157f,
+	0.040315199f,   0.042935108f,   0.045725273f,   0.048696758f,
+	0.051861348f,   0.055231591f,   0.058820850f,   0.062643361f,
+	0.066714279f,   0.071049749f,   0.075666962f,   0.080584227f,
+	0.085821044f,   0.091398179f,   0.097337747f,   0.10366330f,
+	0.11039993f,    0.11757434f,    0.12521498f,    0.13335215f,
+	0.14201813f,    0.15124727f,    0.16107617f,    0.17154380f,
+	0.18269168f,    0.19456402f,    0.20720788f,    0.22067342f,
+	0.23501402f,    0.25028656f,    0.26655159f,    0.28387361f,
+	0.30232132f,    0.32196786f,    0.34289114f,    0.36517414f,
+	0.38890521f,    0.41417847f,    0.44109412f,    0.46975890f,
+	0.50028648f,    0.53279791f,    0.56742212f,    0.60429640f,
+	0.64356699f,    0.68538959f,    0.72993007f,    0.77736504f,
+	0.82788260f,    0.88168307f,    0.9389798f,     1.0f
+};
+
+
+// @OPTIMIZE: if you want to replace this bresenham line-drawing routine,
+// note that you must produce bit-identical output to decode correctly;
+// this specific sequence of operations is specified in the spec (it's
+// drawing integer-quantized frequency-space lines that the encoder
+// expects to be exactly the same)
+//     ... also, isn't the whole point of Bresenham's algorithm to NOT
+// have to divide in the setup? sigh.
+#ifndef STB_VORBIS_NO_DEFER_FLOOR
+#define LINE_OP(a,b)   a *= b
+#else
+#define LINE_OP(a,b)   a = b
+#endif
+
+#ifdef STB_VORBIS_DIVIDE_TABLE
+#define DIVTAB_NUMER   32
+#define DIVTAB_DENOM   64
+int8 integer_divide_table[DIVTAB_NUMER][DIVTAB_DENOM]; // 2KB
+#endif
+
+static __forceinline void draw_line(float *output, int x0, int y0, int x1, int y1, int n) {
+	int dy = y1 - y0;
+	int adx = x1 - x0;
+	int ady = abs(dy);
+	int base;
+	int x = x0, y = y0;
+	int err = 0;
+	int sy;
+
+#ifdef STB_VORBIS_DIVIDE_TABLE
+	if (adx < DIVTAB_DENOM && ady < DIVTAB_NUMER) {
+		if (dy < 0) {
+			base = -integer_divide_table[ady][adx];
+			sy = base - 1;
+		} else {
+			base = integer_divide_table[ady][adx];
+			sy = base + 1;
+		}
+	} else {
+		base = dy / adx;
+		if (dy < 0)
+			sy = base - 1;
+		else
+			sy = base + 1;
+	}
+#else
+	base = dy / adx;
+	if (dy < 0)
+		sy = base - 1;
+	else
+		sy = base + 1;
+#endif
+	ady -= abs(base) * adx;
+	if (x1 > n) x1 = n;
+	if (x < x1) {
+		LINE_OP(output[x], inverse_db_table[y]);
+		for (++x; x < x1; ++x) {
+			err += ady;
+			if (err >= adx) {
+				err -= adx;
+				y += sy;
+			} else
+				y += base;
+			LINE_OP(output[x], inverse_db_table[y]);
+		}
+	}
+}
+
+static int residue_decode(vorb *f, Codebook *book, float *target, int offset, int n, int rtype) {
+	int k;
+	if (rtype == 0) {
+		int step = n / book->dimensions;
+		for (k = 0; k < step; ++k)
+			if (!codebook_decode_step(f, book, target + offset + k, n - offset - k, step))
+				return FALSE;
+	} else {
+		for (k = 0; k < n; ) {
+			if (!codebook_decode(f, book, target + offset, n - k))
+				return FALSE;
+			k += book->dimensions;
+			offset += book->dimensions;
+		}
+	}
+	return TRUE;
+}
+
+static void decode_residue(vorb *f, float *residue_buffers[], int ch, int n, int rn, uint8 *do_not_decode) {
+	int i, j, pass;
+	Residue *r = f->residue_config + rn;
+	int rtype = f->residue_types[rn];
+	int c = r->classbook;
+	int classwords = f->codebooks[c].dimensions;
+	int n_read = r->end - r->begin;
+	int part_read = n_read / r->part_size;
+	int temp_alloc_point = temp_alloc_save(f);
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+	uint8 ***part_classdata = (uint8 ***) temp_block_array(f, f->channels, part_read * sizeof(**part_classdata));
+#else
+	int **classifications = (int **) temp_block_array(f, f->channels, part_read * sizeof(**classifications));
+#endif
+
+	CHECK(f);
+
+	for (i = 0; i < ch; ++i)
+		if (!do_not_decode[i])
+			memset(residue_buffers[i], 0, sizeof(float) * n);
+
+	if (rtype == 2 && ch != 1) {
+		for (j = 0; j < ch; ++j)
+			if (!do_not_decode[j])
+				break;
+		if (j == ch)
+			goto done;
+
+		for (pass = 0; pass < 8; ++pass) {
+			int pcount = 0, class_set = 0;
+			if (ch == 2) {
+				while (pcount < part_read) {
+					int z = r->begin + pcount*r->part_size;
+					int c_inter = (z & 1), p_inter = z >> 1;
+					if (pass == 0) {
+						Codebook *c = f->codebooks + r->classbook;
+						int q;
+						DECODE(q, f, c);
+						if (q == EOP) goto done;
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+						part_classdata[0][class_set] = r->classdata[q];
+#else
+						for (i = classwords - 1; i >= 0; --i) {
+							classifications[0][i + pcount] = q % r->classifications;
+							q /= r->classifications;
+						}
+#endif
+					}
+					for (i = 0; i < classwords && pcount < part_read; ++i, ++pcount) {
+						int z = r->begin + pcount*r->part_size;
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+						int c = part_classdata[0][class_set][i];
+#else
+						int c = classifications[0][pcount];
+#endif
+						int b = r->residue_books[c][pass];
+						if (b >= 0) {
+							Codebook *book = f->codebooks + b;
+#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
+							if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
+								goto done;
+#else
+							// saves 1%
+							if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
+								goto done;
+#endif
+						} else {
+							z += r->part_size;
+							c_inter = z & 1;
+							p_inter = z >> 1;
+						}
+					}
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+					++class_set;
+#endif
+				}
+			} else if (ch == 1) {
+				while (pcount < part_read) {
+					int z = r->begin + pcount*r->part_size;
+					int c_inter = 0, p_inter = z;
+					if (pass == 0) {
+						Codebook *c = f->codebooks + r->classbook;
+						int q;
+						DECODE(q, f, c);
+						if (q == EOP) goto done;
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+						part_classdata[0][class_set] = r->classdata[q];
+#else
+						for (i = classwords - 1; i >= 0; --i) {
+							classifications[0][i + pcount] = q % r->classifications;
+							q /= r->classifications;
+						}
+#endif
+					}
+					for (i = 0; i < classwords && pcount < part_read; ++i, ++pcount) {
+						int z = r->begin + pcount*r->part_size;
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+						int c = part_classdata[0][class_set][i];
+#else
+						int c = classifications[0][pcount];
+#endif
+						int b = r->residue_books[c][pass];
+						if (b >= 0) {
+							Codebook *book = f->codebooks + b;
+							if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
+								goto done;
+						} else {
+							z += r->part_size;
+							c_inter = 0;
+							p_inter = z;
+						}
+					}
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+					++class_set;
+#endif
+				}
+			} else {
+				while (pcount < part_read) {
+					int z = r->begin + pcount*r->part_size;
+					int c_inter = z % ch, p_inter = z / ch;
+					if (pass == 0) {
+						Codebook *c = f->codebooks + r->classbook;
+						int q;
+						DECODE(q, f, c);
+						if (q == EOP) goto done;
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+						part_classdata[0][class_set] = r->classdata[q];
+#else
+						for (i = classwords - 1; i >= 0; --i) {
+							classifications[0][i + pcount] = q % r->classifications;
+							q /= r->classifications;
+						}
+#endif
+					}
+					for (i = 0; i < classwords && pcount < part_read; ++i, ++pcount) {
+						int z = r->begin + pcount*r->part_size;
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+						int c = part_classdata[0][class_set][i];
+#else
+						int c = classifications[0][pcount];
+#endif
+						int b = r->residue_books[c][pass];
+						if (b >= 0) {
+							Codebook *book = f->codebooks + b;
+							if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
+								goto done;
+						} else {
+							z += r->part_size;
+							c_inter = z % ch;
+							p_inter = z / ch;
+						}
+					}
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+					++class_set;
+#endif
+				}
+			}
+		}
+		goto done;
+	}
+	CHECK(f);
+
+	for (pass = 0; pass < 8; ++pass) {
+		int pcount = 0, class_set = 0;
+		while (pcount < part_read) {
+			if (pass == 0) {
+				for (j = 0; j < ch; ++j) {
+					if (!do_not_decode[j]) {
+						Codebook *c = f->codebooks + r->classbook;
+						int temp;
+						DECODE(temp, f, c);
+						if (temp == EOP) goto done;
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+						part_classdata[j][class_set] = r->classdata[temp];
+#else
+						for (i = classwords - 1; i >= 0; --i) {
+							classifications[j][i + pcount] = temp % r->classifications;
+							temp /= r->classifications;
+						}
+#endif
+					}
+				}
+			}
+			for (i = 0; i < classwords && pcount < part_read; ++i, ++pcount) {
+				for (j = 0; j < ch; ++j) {
+					if (!do_not_decode[j]) {
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+						int c = part_classdata[j][class_set][i];
+#else
+						int c = classifications[j][pcount];
+#endif
+						int b = r->residue_books[c][pass];
+						if (b >= 0) {
+							float *target = residue_buffers[j];
+							int offset = r->begin + pcount * r->part_size;
+							int n = r->part_size;
+							Codebook *book = f->codebooks + b;
+							if (!residue_decode(f, book, target, offset, n, rtype))
+								goto done;
+						}
+					}
+				}
+			}
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+			++class_set;
+#endif
+		}
+	}
+done:
+	CHECK(f);
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+	temp_free(f, part_classdata);
+#else
+	temp_free(f, classifications);
+#endif
+	temp_alloc_restore(f, temp_alloc_point);
+}
+
+
+#if 0
+// slow way for debugging
+void inverse_mdct_slow(float *buffer, int n) {
+	int i, j;
+	int n2 = n >> 1;
+	float *x = (float *) malloc(sizeof(*x) * n2);
+	memcpy(x, buffer, sizeof(*x) * n2);
+	for (i = 0; i < n; ++i) {
+		float acc = 0;
+		for (j = 0; j < n2; ++j)
+			// formula from paper:
+			//acc += n/4.0f * x[j] * (float) cos(M_PI / 2 / n * (2 * i + 1 + n/2.0)*(2*j+1));
+			// formula from wikipedia
+			//acc += 2.0f / n2 * x[j] * (float) cos(M_PI/n2 * (i + 0.5 + n2/2)*(j + 0.5));
+			// these are equivalent, except the formula from the paper inverts the multiplier!
+			// however, what actually works is NO MULTIPLIER!?!
+			//acc += 64 * 2.0f / n2 * x[j] * (float) cos(M_PI/n2 * (i + 0.5 + n2/2)*(j + 0.5));
+			acc += x[j] * (float) cos(M_PI / 2 / n * (2 * i + 1 + n / 2.0)*(2 * j + 1));
+		buffer[i] = acc;
+	}
+	free(x);
+}
+#elif 0
+// same as above, but just barely able to run in real time on modern machines
+void inverse_mdct_slow(float *buffer, int n, vorb *f, int blocktype) {
+	float mcos[16384];
+	int i, j;
+	int n2 = n >> 1, nmask = (n << 2) - 1;
+	float *x = (float *) malloc(sizeof(*x) * n2);
+	memcpy(x, buffer, sizeof(*x) * n2);
+	for (i = 0; i < 4 * n; ++i)
+		mcos[i] = (float) cos(M_PI / 2 * i / n);
+
+	for (i = 0; i < n; ++i) {
+		float acc = 0;
+		for (j = 0; j < n2; ++j)
+			acc += x[j] * mcos[(2 * i + 1 + n2)*(2 * j + 1) & nmask];
+		buffer[i] = acc;
+	}
+	free(x);
+}
+#elif 0
+// transform to use a slow dct-iv; this is STILL basically trivial,
+// but only requires half as many ops
+void dct_iv_slow(float *buffer, int n) {
+	float mcos[16384];
+	float x[2048];
+	int i, j;
+	int n2 = n >> 1, nmask = (n << 3) - 1;
+	memcpy(x, buffer, sizeof(*x) * n);
+	for (i = 0; i < 8 * n; ++i)
+		mcos[i] = (float) cos(M_PI / 4 * i / n);
+	for (i = 0; i < n; ++i) {
+		float acc = 0;
+		for (j = 0; j < n; ++j)
+			acc += x[j] * mcos[((2 * i + 1)*(2 * j + 1)) & nmask];
+		buffer[i] = acc;
+	}
+}
+
+void inverse_mdct_slow(float *buffer, int n, vorb *f, int blocktype) {
+	int i, n4 = n >> 2, n2 = n >> 1, n3_4 = n - n4;
+	float temp[4096];
+
+	memcpy(temp, buffer, n2 * sizeof(float));
+	dct_iv_slow(temp, n2);  // returns -c'-d, a-b'
+
+	for (i = 0; i < n4; ++i) buffer[i] = temp[i + n4];            // a-b'
+	for (; i < n3_4; ++i) buffer[i] = -temp[n3_4 - i - 1];   // b-a', c+d'
+	for (; i < n; ++i) buffer[i] = -temp[i - n3_4];       // c'+d
+}
+#endif
+
+#ifndef LIBVORBIS_MDCT
+#define LIBVORBIS_MDCT 0
+#endif
+
+#if LIBVORBIS_MDCT
+// directly call the vorbis MDCT using an interface documented
+// by Jeff Roberts... useful for performance comparison
+typedef struct {
+	int n;
+	int log2n;
+
+	float *trig;
+	int   *bitrev;
+
+	float scale;
+} mdct_lookup;
+
+extern void mdct_init(mdct_lookup *lookup, int n);
+extern void mdct_clear(mdct_lookup *l);
+extern void mdct_backward(mdct_lookup *init, float *in, float *out);
+
+mdct_lookup M1, M2;
+
+void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) {
+	mdct_lookup *M;
+	if (M1.n == n) M = &M1;
+	else if (M2.n == n) M = &M2;
+	else if (M1.n == 0) { mdct_init(&M1, n); M = &M1; } else {
+		if (M2.n) __asm int 3;
+		mdct_init(&M2, n);
+		M = &M2;
+	}
+
+	mdct_backward(M, buffer, buffer);
+}
+#endif
+
+
+// the following were split out into separate functions while optimizing;
+// they could be pushed back up but eh. __forceinline showed no change;
+// they're probably already being inlined.
+static void imdct_step3_iter0_loop(int n, float *e, int i_off, int k_off, float *A) {
+	float *ee0 = e + i_off;
+	float *ee2 = ee0 + k_off;
+	int i;
+
+	assert((n & 3) == 0);
+	for (i = (n >> 2); i > 0; --i) {
+		float k00_20, k01_21;
+		k00_20 = ee0[0] - ee2[0];
+		k01_21 = ee0[-1] - ee2[-1];
+		ee0[0] += ee2[0];//ee0[ 0] = ee0[ 0] + ee2[ 0];
+		ee0[-1] += ee2[-1];//ee0[-1] = ee0[-1] + ee2[-1];
+		ee2[0] = k00_20 * A[0] - k01_21 * A[1];
+		ee2[-1] = k01_21 * A[0] + k00_20 * A[1];
+		A += 8;
+
+		k00_20 = ee0[-2] - ee2[-2];
+		k01_21 = ee0[-3] - ee2[-3];
+		ee0[-2] += ee2[-2];//ee0[-2] = ee0[-2] + ee2[-2];
+		ee0[-3] += ee2[-3];//ee0[-3] = ee0[-3] + ee2[-3];
+		ee2[-2] = k00_20 * A[0] - k01_21 * A[1];
+		ee2[-3] = k01_21 * A[0] + k00_20 * A[1];
+		A += 8;
+
+		k00_20 = ee0[-4] - ee2[-4];
+		k01_21 = ee0[-5] - ee2[-5];
+		ee0[-4] += ee2[-4];//ee0[-4] = ee0[-4] + ee2[-4];
+		ee0[-5] += ee2[-5];//ee0[-5] = ee0[-5] + ee2[-5];
+		ee2[-4] = k00_20 * A[0] - k01_21 * A[1];
+		ee2[-5] = k01_21 * A[0] + k00_20 * A[1];
+		A += 8;
+
+		k00_20 = ee0[-6] - ee2[-6];
+		k01_21 = ee0[-7] - ee2[-7];
+		ee0[-6] += ee2[-6];//ee0[-6] = ee0[-6] + ee2[-6];
+		ee0[-7] += ee2[-7];//ee0[-7] = ee0[-7] + ee2[-7];
+		ee2[-6] = k00_20 * A[0] - k01_21 * A[1];
+		ee2[-7] = k01_21 * A[0] + k00_20 * A[1];
+		A += 8;
+		ee0 -= 8;
+		ee2 -= 8;
+	}
+}
+
+static void imdct_step3_inner_r_loop(int lim, float *e, int d0, int k_off, float *A, int k1) {
+	int i;
+	float k00_20, k01_21;
+
+	float *e0 = e + d0;
+	float *e2 = e0 + k_off;
+
+	for (i = lim >> 2; i > 0; --i) {
+		k00_20 = e0[-0] - e2[-0];
+		k01_21 = e0[-1] - e2[-1];
+		e0[-0] += e2[-0];//e0[-0] = e0[-0] + e2[-0];
+		e0[-1] += e2[-1];//e0[-1] = e0[-1] + e2[-1];
+		e2[-0] = (k00_20) *A[0] - (k01_21) * A[1];
+		e2[-1] = (k01_21) *A[0] + (k00_20) * A[1];
+
+		A += k1;
+
+		k00_20 = e0[-2] - e2[-2];
+		k01_21 = e0[-3] - e2[-3];
+		e0[-2] += e2[-2];//e0[-2] = e0[-2] + e2[-2];
+		e0[-3] += e2[-3];//e0[-3] = e0[-3] + e2[-3];
+		e2[-2] = (k00_20) *A[0] - (k01_21) * A[1];
+		e2[-3] = (k01_21) *A[0] + (k00_20) * A[1];
+
+		A += k1;
+
+		k00_20 = e0[-4] - e2[-4];
+		k01_21 = e0[-5] - e2[-5];
+		e0[-4] += e2[-4];//e0[-4] = e0[-4] + e2[-4];
+		e0[-5] += e2[-5];//e0[-5] = e0[-5] + e2[-5];
+		e2[-4] = (k00_20) *A[0] - (k01_21) * A[1];
+		e2[-5] = (k01_21) *A[0] + (k00_20) * A[1];
+
+		A += k1;
+
+		k00_20 = e0[-6] - e2[-6];
+		k01_21 = e0[-7] - e2[-7];
+		e0[-6] += e2[-6];//e0[-6] = e0[-6] + e2[-6];
+		e0[-7] += e2[-7];//e0[-7] = e0[-7] + e2[-7];
+		e2[-6] = (k00_20) *A[0] - (k01_21) * A[1];
+		e2[-7] = (k01_21) *A[0] + (k00_20) * A[1];
+
+		e0 -= 8;
+		e2 -= 8;
+
+		A += k1;
+	}
+}
+
+static void imdct_step3_inner_s_loop(int n, float *e, int i_off, int k_off, float *A, int a_off, int k0) {
+	int i;
+	float A0 = A[0];
+	float A1 = A[0 + 1];
+	float A2 = A[0 + a_off];
+	float A3 = A[0 + a_off + 1];
+	float A4 = A[0 + a_off * 2 + 0];
+	float A5 = A[0 + a_off * 2 + 1];
+	float A6 = A[0 + a_off * 3 + 0];
+	float A7 = A[0 + a_off * 3 + 1];
+
+	float k00, k11;
+
+	float *ee0 = e + i_off;
+	float *ee2 = ee0 + k_off;
+
+	for (i = n; i > 0; --i) {
+		k00 = ee0[0] - ee2[0];
+		k11 = ee0[-1] - ee2[-1];
+		ee0[0] = ee0[0] + ee2[0];
+		ee0[-1] = ee0[-1] + ee2[-1];
+		ee2[0] = (k00) * A0 - (k11) * A1;
+		ee2[-1] = (k11) * A0 + (k00) * A1;
+
+		k00 = ee0[-2] - ee2[-2];
+		k11 = ee0[-3] - ee2[-3];
+		ee0[-2] = ee0[-2] + ee2[-2];
+		ee0[-3] = ee0[-3] + ee2[-3];
+		ee2[-2] = (k00) * A2 - (k11) * A3;
+		ee2[-3] = (k11) * A2 + (k00) * A3;
+
+		k00 = ee0[-4] - ee2[-4];
+		k11 = ee0[-5] - ee2[-5];
+		ee0[-4] = ee0[-4] + ee2[-4];
+		ee0[-5] = ee0[-5] + ee2[-5];
+		ee2[-4] = (k00) * A4 - (k11) * A5;
+		ee2[-5] = (k11) * A4 + (k00) * A5;
+
+		k00 = ee0[-6] - ee2[-6];
+		k11 = ee0[-7] - ee2[-7];
+		ee0[-6] = ee0[-6] + ee2[-6];
+		ee0[-7] = ee0[-7] + ee2[-7];
+		ee2[-6] = (k00) * A6 - (k11) * A7;
+		ee2[-7] = (k11) * A6 + (k00) * A7;
+
+		ee0 -= k0;
+		ee2 -= k0;
+	}
+}
+
+static __forceinline void iter_54(float *z) {
+	float k00, k11, k22, k33;
+	float y0, y1, y2, y3;
+
+	k00 = z[0] - z[-4];
+	y0 = z[0] + z[-4];
+	y2 = z[-2] + z[-6];
+	k22 = z[-2] - z[-6];
+
+	z[-0] = y0 + y2;      // z0 + z4 + z2 + z6
+	z[-2] = y0 - y2;      // z0 + z4 - z2 - z6
+
+						  // done with y0,y2
+
+	k33 = z[-3] - z[-7];
+
+	z[-4] = k00 + k33;    // z0 - z4 + z3 - z7
+	z[-6] = k00 - k33;    // z0 - z4 - z3 + z7
+
+						  // done with k33
+
+	k11 = z[-1] - z[-5];
+	y1 = z[-1] + z[-5];
+	y3 = z[-3] + z[-7];
+
+	z[-1] = y1 + y3;      // z1 + z5 + z3 + z7
+	z[-3] = y1 - y3;      // z1 + z5 - z3 - z7
+	z[-5] = k11 - k22;    // z1 - z5 + z2 - z6
+	z[-7] = k11 + k22;    // z1 - z5 - z2 + z6
+}
+
+static void imdct_step3_inner_s_loop_ld654(int n, float *e, int i_off, float *A, int base_n) {
+	int a_off = base_n >> 3;
+	float A2 = A[0 + a_off];
+	float *z = e + i_off;
+	float *base = z - 16 * n;
+
+	while (z > base) {
+		float k00, k11;
+
+		k00 = z[-0] - z[-8];
+		k11 = z[-1] - z[-9];
+		z[-0] = z[-0] + z[-8];
+		z[-1] = z[-1] + z[-9];
+		z[-8] = k00;
+		z[-9] = k11;
+
+		k00 = z[-2] - z[-10];
+		k11 = z[-3] - z[-11];
+		z[-2] = z[-2] + z[-10];
+		z[-3] = z[-3] + z[-11];
+		z[-10] = (k00 + k11) * A2;
+		z[-11] = (k11 - k00) * A2;
+
+		k00 = z[-12] - z[-4];  // reverse to avoid a unary negation
+		k11 = z[-5] - z[-13];
+		z[-4] = z[-4] + z[-12];
+		z[-5] = z[-5] + z[-13];
+		z[-12] = k11;
+		z[-13] = k00;
+
+		k00 = z[-14] - z[-6];  // reverse to avoid a unary negation
+		k11 = z[-7] - z[-15];
+		z[-6] = z[-6] + z[-14];
+		z[-7] = z[-7] + z[-15];
+		z[-14] = (k00 + k11) * A2;
+		z[-15] = (k00 - k11) * A2;
+
+		iter_54(z);
+		iter_54(z - 8);
+		z -= 16;
+	}
+}
+
+static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) {
+	int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, l;
+	int ld;
+	// @OPTIMIZE: reduce register pressure by using fewer variables?
+	int save_point = temp_alloc_save(f);
+	float *buf2 = (float *) temp_alloc(f, n2 * sizeof(*buf2));
+	float *u = NULL, *v = NULL;
+	// twiddle factors
+	float *A = f->A[blocktype];
+
+	// IMDCT algorithm from "The use of multirate filter banks for coding of high quality digital audio"
+	// See notes about bugs in that paper in less-optimal implementation 'inverse_mdct_old' after this function.
+
+	// kernel from paper
+
+
+	// merged:
+	//   copy and reflect spectral data
+	//   step 0
+
+	// note that it turns out that the items added together during
+	// this step are, in fact, being added to themselves (as reflected
+	// by step 0). inexplicable inefficiency! this became obvious
+	// once I combined the passes.
+
+	// so there's a missing 'times 2' here (for adding X to itself).
+	// this propogates through linearly to the end, where the numbers
+	// are 1/2 too small, and need to be compensated for.
+
+	{
+		float *d, *e, *AA, *e_stop;
+		d = &buf2[n2 - 2];
+		AA = A;
+		e = &buffer[0];
+		e_stop = &buffer[n2];
+		while (e != e_stop) {
+			d[1] = (e[0] * AA[0] - e[2] * AA[1]);
+			d[0] = (e[0] * AA[1] + e[2] * AA[0]);
+			d -= 2;
+			AA += 2;
+			e += 4;
+		}
+
+		e = &buffer[n2 - 3];
+		while (d >= buf2) {
+			d[1] = (-e[2] * AA[0] - -e[0] * AA[1]);
+			d[0] = (-e[2] * AA[1] + -e[0] * AA[0]);
+			d -= 2;
+			AA += 2;
+			e -= 4;
+		}
+	}
+
+	// now we use symbolic names for these, so that we can
+	// possibly swap their meaning as we change which operations
+	// are in place
+
+	u = buffer;
+	v = buf2;
+
+	// step 2    (paper output is w, now u)
+	// this could be in place, but the data ends up in the wrong
+	// place... _somebody_'s got to swap it, so this is nominated
+	{
+		float *AA = &A[n2 - 8];
+		float *d0, *d1, *e0, *e1;
+
+		e0 = &v[n4];
+		e1 = &v[0];
+
+		d0 = &u[n4];
+		d1 = &u[0];
+
+		while (AA >= A) {
+			float v40_20, v41_21;
+
+			v41_21 = e0[1] - e1[1];
+			v40_20 = e0[0] - e1[0];
+			d0[1] = e0[1] + e1[1];
+			d0[0] = e0[0] + e1[0];
+			d1[1] = v41_21*AA[4] - v40_20*AA[5];
+			d1[0] = v40_20*AA[4] + v41_21*AA[5];
+
+			v41_21 = e0[3] - e1[3];
+			v40_20 = e0[2] - e1[2];
+			d0[3] = e0[3] + e1[3];
+			d0[2] = e0[2] + e1[2];
+			d1[3] = v41_21*AA[0] - v40_20*AA[1];
+			d1[2] = v40_20*AA[0] + v41_21*AA[1];
+
+			AA -= 8;
+
+			d0 += 4;
+			d1 += 4;
+			e0 += 4;
+			e1 += 4;
+		}
+	}
+
+	// step 3
+	ld = ilog(n) - 1; // ilog is off-by-one from normal definitions
+
+					  // optimized step 3:
+
+					  // the original step3 loop can be nested r inside s or s inside r;
+					  // it's written originally as s inside r, but this is dumb when r
+					  // iterates many times, and s few. So I have two copies of it and
+					  // switch between them halfway.
+
+					  // this is iteration 0 of step 3
+	imdct_step3_iter0_loop(n >> 4, u, n2 - 1 - n4 * 0, -(n >> 3), A);
+	imdct_step3_iter0_loop(n >> 4, u, n2 - 1 - n4 * 1, -(n >> 3), A);
+
+	// this is iteration 1 of step 3
+	imdct_step3_inner_r_loop(n >> 5, u, n2 - 1 - n8 * 0, -(n >> 4), A, 16);
+	imdct_step3_inner_r_loop(n >> 5, u, n2 - 1 - n8 * 1, -(n >> 4), A, 16);
+	imdct_step3_inner_r_loop(n >> 5, u, n2 - 1 - n8 * 2, -(n >> 4), A, 16);
+	imdct_step3_inner_r_loop(n >> 5, u, n2 - 1 - n8 * 3, -(n >> 4), A, 16);
+
+	l = 2;
+	for (; l < (ld - 3) >> 1; ++l) {
+		int k0 = n >> (l + 2), k0_2 = k0 >> 1;
+		int lim = 1 << (l + 1);
+		int i;
+		for (i = 0; i < lim; ++i)
+			imdct_step3_inner_r_loop(n >> (l + 4), u, n2 - 1 - k0*i, -k0_2, A, 1 << (l + 3));
+	}
+
+	for (; l < ld - 6; ++l) {
+		int k0 = n >> (l + 2), k1 = 1 << (l + 3), k0_2 = k0 >> 1;
+		int rlim = n >> (l + 6), r;
+		int lim = 1 << (l + 1);
+		int i_off;
+		float *A0 = A;
+		i_off = n2 - 1;
+		for (r = rlim; r > 0; --r) {
+			imdct_step3_inner_s_loop(lim, u, i_off, -k0_2, A0, k1, k0);
+			A0 += k1 * 4;
+			i_off -= 8;
+		}
+	}
+
+	// iterations with count:
+	//   ld-6,-5,-4 all interleaved together
+	//       the big win comes from getting rid of needless flops
+	//         due to the constants on pass 5 & 4 being all 1 and 0;
+	//       combining them to be simultaneous to improve cache made little difference
+	imdct_step3_inner_s_loop_ld654(n >> 5, u, n2 - 1, A, n);
+
+	// output is u
+
+	// step 4, 5, and 6
+	// cannot be in-place because of step 5
+	{
+		uint16 *bitrev = f->bit_reverse[blocktype];
+		// weirdly, I'd have thought reading sequentially and writing
+		// erratically would have been better than vice-versa, but in
+		// fact that's not what my testing showed. (That is, with
+		// j = bitreverse(i), do you read i and write j, or read j and write i.)
+
+		float *d0 = &v[n4 - 4];
+		float *d1 = &v[n2 - 4];
+		while (d0 >= v) {
+			int k4;
+
+			k4 = bitrev[0];
+			d1[3] = u[k4 + 0];
+			d1[2] = u[k4 + 1];
+			d0[3] = u[k4 + 2];
+			d0[2] = u[k4 + 3];
+
+			k4 = bitrev[1];
+			d1[1] = u[k4 + 0];
+			d1[0] = u[k4 + 1];
+			d0[1] = u[k4 + 2];
+			d0[0] = u[k4 + 3];
+
+			d0 -= 4;
+			d1 -= 4;
+			bitrev += 2;
+		}
+	}
+	// (paper output is u, now v)
+
+
+	// data must be in buf2
+	assert(v == buf2);
+
+	// step 7   (paper output is v, now v)
+	// this is now in place
+	{
+		float *C = f->C[blocktype];
+		float *d, *e;
+
+		d = v;
+		e = v + n2 - 4;
+
+		while (d < e) {
+			float a02, a11, b0, b1, b2, b3;
+
+			a02 = d[0] - e[2];
+			a11 = d[1] + e[3];
+
+			b0 = C[1] * a02 + C[0] * a11;
+			b1 = C[1] * a11 - C[0] * a02;
+
+			b2 = d[0] + e[2];
+			b3 = d[1] - e[3];
+
+			d[0] = b2 + b0;
+			d[1] = b3 + b1;
+			e[2] = b2 - b0;
+			e[3] = b1 - b3;
+
+			a02 = d[2] - e[0];
+			a11 = d[3] + e[1];
+
+			b0 = C[3] * a02 + C[2] * a11;
+			b1 = C[3] * a11 - C[2] * a02;
+
+			b2 = d[2] + e[0];
+			b3 = d[3] - e[1];
+
+			d[2] = b2 + b0;
+			d[3] = b3 + b1;
+			e[0] = b2 - b0;
+			e[1] = b1 - b3;
+
+			C += 4;
+			d += 4;
+			e -= 4;
+		}
+	}
+
+	// data must be in buf2
+
+
+	// step 8+decode   (paper output is X, now buffer)
+	// this generates pairs of data a la 8 and pushes them directly through
+	// the decode kernel (pushing rather than pulling) to avoid having
+	// to make another pass later
+
+	// this cannot POSSIBLY be in place, so we refer to the buffers directly
+
+	{
+		float *d0, *d1, *d2, *d3;
+
+		float *B = f->B[blocktype] + n2 - 8;
+		float *e = buf2 + n2 - 8;
+		d0 = &buffer[0];
+		d1 = &buffer[n2 - 4];
+		d2 = &buffer[n2];
+		d3 = &buffer[n - 4];
+		while (e >= v) {
+			float p0, p1, p2, p3;
+
+			p3 = e[6] * B[7] - e[7] * B[6];
+			p2 = -e[6] * B[6] - e[7] * B[7];
+
+			d0[0] = p3;
+			d1[3] = -p3;
+			d2[0] = p2;
+			d3[3] = p2;
+
+			p1 = e[4] * B[5] - e[5] * B[4];
+			p0 = -e[4] * B[4] - e[5] * B[5];
+
+			d0[1] = p1;
+			d1[2] = -p1;
+			d2[1] = p0;
+			d3[2] = p0;
+
+			p3 = e[2] * B[3] - e[3] * B[2];
+			p2 = -e[2] * B[2] - e[3] * B[3];
+
+			d0[2] = p3;
+			d1[1] = -p3;
+			d2[2] = p2;
+			d3[1] = p2;
+
+			p1 = e[0] * B[1] - e[1] * B[0];
+			p0 = -e[0] * B[0] - e[1] * B[1];
+
+			d0[3] = p1;
+			d1[0] = -p1;
+			d2[3] = p0;
+			d3[0] = p0;
+
+			B -= 8;
+			e -= 8;
+			d0 += 4;
+			d2 += 4;
+			d1 -= 4;
+			d3 -= 4;
+		}
+	}
+
+	temp_free(f, buf2);
+	temp_alloc_restore(f, save_point);
+}
+
+#if 0
+// this is the original version of the above code, if you want to optimize it from scratch
+void inverse_mdct_naive(float *buffer, int n) {
+	float s;
+	float A[1 << 12], B[1 << 12], C[1 << 11];
+	int i, k, k2, k4, n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, l;
+	int n3_4 = n - n4, ld;
+	// how can they claim this only uses N words?!
+	// oh, because they're only used sparsely, whoops
+	float u[1 << 13], X[1 << 13], v[1 << 13], w[1 << 13];
+	// set up twiddle factors
+
+	for (k = k2 = 0; k < n4; ++k, k2 += 2) {
+		A[k2] = (float) cos(4 * k*M_PI / n);
+		A[k2 + 1] = (float) -sin(4 * k*M_PI / n);
+		B[k2] = (float) cos((k2 + 1)*M_PI / n / 2);
+		B[k2 + 1] = (float) sin((k2 + 1)*M_PI / n / 2);
+	}
+	for (k = k2 = 0; k < n8; ++k, k2 += 2) {
+		C[k2] = (float) cos(2 * (k2 + 1)*M_PI / n);
+		C[k2 + 1] = (float) -sin(2 * (k2 + 1)*M_PI / n);
+	}
+
+	// IMDCT algorithm from "The use of multirate filter banks for coding of high quality digital audio"
+	// Note there are bugs in that pseudocode, presumably due to them attempting
+	// to rename the arrays nicely rather than representing the way their actual
+	// implementation bounces buffers back and forth. As a result, even in the
+	// "some formulars corrected" version, a direct implementation fails. These
+	// are noted below as "paper bug".
+
+	// copy and reflect spectral data
+	for (k = 0; k < n2; ++k) u[k] = buffer[k];
+	for (; k < n; ++k) u[k] = -buffer[n - k - 1];
+	// kernel from paper
+	// step 1
+	for (k = k2 = k4 = 0; k < n4; k += 1, k2 += 2, k4 += 4) {
+		v[n - k4 - 1] = (u[k4] - u[n - k4 - 1]) * A[k2] - (u[k4 + 2] - u[n - k4 - 3])*A[k2 + 1];
+		v[n - k4 - 3] = (u[k4] - u[n - k4 - 1]) * A[k2 + 1] + (u[k4 + 2] - u[n - k4 - 3])*A[k2];
+	}
+	// step 2
+	for (k = k4 = 0; k < n8; k += 1, k4 += 4) {
+		w[n2 + 3 + k4] = v[n2 + 3 + k4] + v[k4 + 3];
+		w[n2 + 1 + k4] = v[n2 + 1 + k4] + v[k4 + 1];
+		w[k4 + 3] = (v[n2 + 3 + k4] - v[k4 + 3])*A[n2 - 4 - k4] - (v[n2 + 1 + k4] - v[k4 + 1])*A[n2 - 3 - k4];
+		w[k4 + 1] = (v[n2 + 1 + k4] - v[k4 + 1])*A[n2 - 4 - k4] + (v[n2 + 3 + k4] - v[k4 + 3])*A[n2 - 3 - k4];
+	}
+	// step 3
+	ld = ilog(n) - 1; // ilog is off-by-one from normal definitions
+	for (l = 0; l < ld - 3; ++l) {
+		int k0 = n >> (l + 2), k1 = 1 << (l + 3);
+		int rlim = n >> (l + 4), r4, r;
+		int s2lim = 1 << (l + 2), s2;
+		for (r = r4 = 0; r < rlim; r4 += 4, ++r) {
+			for (s2 = 0; s2 < s2lim; s2 += 2) {
+				u[n - 1 - k0*s2 - r4] = w[n - 1 - k0*s2 - r4] + w[n - 1 - k0*(s2 + 1) - r4];
+				u[n - 3 - k0*s2 - r4] = w[n - 3 - k0*s2 - r4] + w[n - 3 - k0*(s2 + 1) - r4];
+				u[n - 1 - k0*(s2 + 1) - r4] = (w[n - 1 - k0*s2 - r4] - w[n - 1 - k0*(s2 + 1) - r4]) * A[r*k1]
+					- (w[n - 3 - k0*s2 - r4] - w[n - 3 - k0*(s2 + 1) - r4]) * A[r*k1 + 1];
+				u[n - 3 - k0*(s2 + 1) - r4] = (w[n - 3 - k0*s2 - r4] - w[n - 3 - k0*(s2 + 1) - r4]) * A[r*k1]
+					+ (w[n - 1 - k0*s2 - r4] - w[n - 1 - k0*(s2 + 1) - r4]) * A[r*k1 + 1];
+			}
+		}
+		if (l + 1 < ld - 3) {
+			// paper bug: ping-ponging of u&w here is omitted
+			memcpy(w, u, sizeof(u));
+		}
+	}
+
+	// step 4
+	for (i = 0; i < n8; ++i) {
+		int j = bit_reverse(i) >> (32 - ld + 3);
+		assert(j < n8);
+		if (i == j) {
+			// paper bug: original code probably swapped in place; if copying,
+			//            need to directly copy in this case
+			int i8 = i << 3;
+			v[i8 + 1] = u[i8 + 1];
+			v[i8 + 3] = u[i8 + 3];
+			v[i8 + 5] = u[i8 + 5];
+			v[i8 + 7] = u[i8 + 7];
+		} else if (i < j) {
+			int i8 = i << 3, j8 = j << 3;
+			v[j8 + 1] = u[i8 + 1], v[i8 + 1] = u[j8 + 1];
+			v[j8 + 3] = u[i8 + 3], v[i8 + 3] = u[j8 + 3];
+			v[j8 + 5] = u[i8 + 5], v[i8 + 5] = u[j8 + 5];
+			v[j8 + 7] = u[i8 + 7], v[i8 + 7] = u[j8 + 7];
+		}
+	}
+	// step 5
+	for (k = 0; k < n2; ++k) {
+		w[k] = v[k * 2 + 1];
+	}
+	// step 6
+	for (k = k2 = k4 = 0; k < n8; ++k, k2 += 2, k4 += 4) {
+		u[n - 1 - k2] = w[k4];
+		u[n - 2 - k2] = w[k4 + 1];
+		u[n3_4 - 1 - k2] = w[k4 + 2];
+		u[n3_4 - 2 - k2] = w[k4 + 3];
+	}
+	// step 7
+	for (k = k2 = 0; k < n8; ++k, k2 += 2) {
+		v[n2 + k2] = (u[n2 + k2] + u[n - 2 - k2] + C[k2 + 1] * (u[n2 + k2] - u[n - 2 - k2]) + C[k2] * (u[n2 + k2 + 1] + u[n - 2 - k2 + 1])) / 2;
+		v[n - 2 - k2] = (u[n2 + k2] + u[n - 2 - k2] - C[k2 + 1] * (u[n2 + k2] - u[n - 2 - k2]) - C[k2] * (u[n2 + k2 + 1] + u[n - 2 - k2 + 1])) / 2;
+		v[n2 + 1 + k2] = (u[n2 + 1 + k2] - u[n - 1 - k2] + C[k2 + 1] * (u[n2 + 1 + k2] + u[n - 1 - k2]) - C[k2] * (u[n2 + k2] - u[n - 2 - k2])) / 2;
+		v[n - 1 - k2] = (-u[n2 + 1 + k2] + u[n - 1 - k2] + C[k2 + 1] * (u[n2 + 1 + k2] + u[n - 1 - k2]) - C[k2] * (u[n2 + k2] - u[n - 2 - k2])) / 2;
+	}
+	// step 8
+	for (k = k2 = 0; k < n4; ++k, k2 += 2) {
+		X[k] = v[k2 + n2] * B[k2] + v[k2 + 1 + n2] * B[k2 + 1];
+		X[n2 - 1 - k] = v[k2 + n2] * B[k2 + 1] - v[k2 + 1 + n2] * B[k2];
+	}
+
+	// decode kernel to output
+	// determined the following value experimentally
+	// (by first figuring out what made inverse_mdct_slow work); then matching that here
+	// (probably vorbis encoder premultiplies by n or n/2, to save it on the decoder?)
+	s = 0.5; // theoretically would be n4
+
+			 // [[[ note! the s value of 0.5 is compensated for by the B[] in the current code,
+			 //     so it needs to use the "old" B values to behave correctly, or else
+			 //     set s to 1.0 ]]]
+	for (i = 0; i < n4; ++i) buffer[i] = s * X[i + n4];
+	for (; i < n3_4; ++i) buffer[i] = -s * X[n3_4 - i - 1];
+	for (; i < n; ++i) buffer[i] = -s * X[i - n3_4];
+}
+#endif
+
+static float *get_window(vorb *f, int len) {
+	len <<= 1;
+	if (len == f->blocksize_0) return f->window[0];
+	if (len == f->blocksize_1) return f->window[1];
+	assert(0);
+	return NULL;
+}
+
+#ifndef STB_VORBIS_NO_DEFER_FLOOR
+typedef int16 YTYPE;
+#else
+typedef int YTYPE;
+#endif
+static int do_floor(vorb *f, Mapping *map, int i, int n, float *target, YTYPE *finalY, uint8 *step2_flag) {
+	int n2 = n >> 1;
+	int s = map->chan[i].mux, floor;
+	floor = map->submap_floor[s];
+	if (f->floor_types[floor] == 0) {
+		return error(f, VORBIS_invalid_stream);
+	} else {
+		Floor1 *g = &f->floor_config[floor].floor1;
+		int j, q;
+		int lx = 0, ly = finalY[0] * g->floor1_multiplier;
+		for (q = 1; q < g->values; ++q) {
+			j = g->sorted_order[q];
+#ifndef STB_VORBIS_NO_DEFER_FLOOR
+			if (finalY[j] >= 0)
+#else
+			if (step2_flag[j])
+#endif
+			{
+				int hy = finalY[j] * g->floor1_multiplier;
+				int hx = g->Xlist[j];
+				if (lx != hx)
+					draw_line(target, lx, ly, hx, hy, n2);
+				CHECK(f);
+				lx = hx, ly = hy;
+			}
+		}
+		if (lx < n2) {
+			// optimization of: draw_line(target, lx,ly, n,ly, n2);
+			for (j = lx; j < n2; ++j)
+				LINE_OP(target[j], inverse_db_table[ly]);
+			CHECK(f);
+		}
+	}
+	return TRUE;
+}
+
+// The meaning of "left" and "right"
+//
+// For a given frame:
+//     we compute samples from 0..n
+//     window_center is n/2
+//     we'll window and mix the samples from left_start to left_end with data from the previous frame
+//     all of the samples from left_end to right_start can be output without mixing; however,
+//        this interval is 0-length except when transitioning between short and long frames
+//     all of the samples from right_start to right_end need to be mixed with the next frame,
+//        which we don't have, so those get saved in a buffer
+//     frame N's right_end-right_start, the number of samples to mix with the next frame,
+//        has to be the same as frame N+1's left_end-left_start (which they are by
+//        construction)
+
+static int vorbis_decode_initial(vorb *f, int *p_left_start, int *p_left_end, int *p_right_start, int *p_right_end, int *mode) {
+	Mode *m;
+	int i, n, prev, next, window_center;
+	f->channel_buffer_start = f->channel_buffer_end = 0;
+
+retry:
+	if (f->eof) return FALSE;
+	if (!maybe_start_packet(f))
+		return FALSE;
+	// check packet type
+	if (get_bits(f, 1) != 0) {
+		if (IS_PUSH_MODE(f))
+			return error(f, VORBIS_bad_packet_type);
+		while (EOP != get8_packet(f));
+		goto retry;
+	}
+
+	if (f->alloc.alloc_buffer)
+		assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
+
+	i = get_bits(f, ilog(f->mode_count - 1));
+	if (i == EOP) return FALSE;
+	if (i >= f->mode_count) return FALSE;
+	*mode = i;
+	m = f->mode_config + i;
+	if (m->blockflag) {
+		n = f->blocksize_1;
+		prev = get_bits(f, 1);
+		next = get_bits(f, 1);
+	} else {
+		prev = next = 0;
+		n = f->blocksize_0;
+	}
+
+	// WINDOWING
+
+	window_center = n >> 1;
+	if (m->blockflag && !prev) {
+		*p_left_start = (n - f->blocksize_0) >> 2;
+		*p_left_end = (n + f->blocksize_0) >> 2;
+	} else {
+		*p_left_start = 0;
+		*p_left_end = window_center;
+	}
+	if (m->blockflag && !next) {
+		*p_right_start = (n * 3 - f->blocksize_0) >> 2;
+		*p_right_end = (n * 3 + f->blocksize_0) >> 2;
+	} else {
+		*p_right_start = window_center;
+		*p_right_end = n;
+	}
+
+	return TRUE;
+}
+
+static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start, int left_end, int right_start, int right_end, int *p_left) {
+	Mapping *map;
+	int i, j, k, n, n2;
+	int zero_channel[256];
+	int really_zero_channel[256];
+
+	// WINDOWING
+
+	n = f->blocksize[m->blockflag];
+	map = &f->mapping[m->mapping];
+
+	// FLOORS
+	n2 = n >> 1;
+
+	CHECK(f);
+
+	for (i = 0; i < f->channels; ++i) {
+		int s = map->chan[i].mux, floor;
+		zero_channel[i] = FALSE;
+		floor = map->submap_floor[s];
+		if (f->floor_types[floor] == 0) {
+			return error(f, VORBIS_invalid_stream);
+		} else {
+			Floor1 *g = &f->floor_config[floor].floor1;
+			if (get_bits(f, 1)) {
+				short *finalY;
+				uint8 step2_flag[256];
+				static int range_list[4] = {256, 128, 86, 64};
+				int range = range_list[g->floor1_multiplier - 1];
+				int offset = 2;
+				finalY = f->finalY[i];
+				finalY[0] = get_bits(f, ilog(range) - 1);
+				finalY[1] = get_bits(f, ilog(range) - 1);
+				for (j = 0; j < g->partitions; ++j) {
+					int pclass = g->partition_class_list[j];
+					int cdim = g->class_dimensions[pclass];
+					int cbits = g->class_subclasses[pclass];
+					int csub = (1 << cbits) - 1;
+					int cval = 0;
+					if (cbits) {
+						Codebook *c = f->codebooks + g->class_masterbooks[pclass];
+						DECODE(cval, f, c);
+					}
+					for (k = 0; k < cdim; ++k) {
+						int book = g->subclass_books[pclass][cval & csub];
+						cval = cval >> cbits;
+						if (book >= 0) {
+							int temp;
+							Codebook *c = f->codebooks + book;
+							DECODE(temp, f, c);
+							finalY[offset++] = temp;
+						} else
+							finalY[offset++] = 0;
+					}
+				}
+				if (f->valid_bits == INVALID_BITS) goto error; // behavior according to spec
+				step2_flag[0] = step2_flag[1] = 1;
+				for (j = 2; j < g->values; ++j) {
+					int low, high, pred, highroom, lowroom, room, val;
+					low = g->neighbors[j][0];
+					high = g->neighbors[j][1];
+					//neighbors(g->Xlist, j, &low, &high);
+					pred = predict_point(g->Xlist[j], g->Xlist[low], g->Xlist[high], finalY[low], finalY[high]);
+					val = finalY[j];
+					highroom = range - pred;
+					lowroom = pred;
+					if (highroom < lowroom)
+						room = highroom * 2;
+					else
+						room = lowroom * 2;
+					if (val) {
+						step2_flag[low] = step2_flag[high] = 1;
+						step2_flag[j] = 1;
+						if (val >= room)
+							if (highroom > lowroom)
+								finalY[j] = val - lowroom + pred;
+							else
+								finalY[j] = pred - val + highroom - 1;
+						else
+							if (val & 1)
+								finalY[j] = pred - ((val + 1) >> 1);
+							else
+								finalY[j] = pred + (val >> 1);
+					} else {
+						step2_flag[j] = 0;
+						finalY[j] = pred;
+					}
+				}
+
+#ifdef STB_VORBIS_NO_DEFER_FLOOR
+				do_floor(f, map, i, n, f->floor_buffers[i], finalY, step2_flag);
+#else
+				// defer final floor computation until _after_ residue
+				for (j = 0; j < g->values; ++j) {
+					if (!step2_flag[j])
+						finalY[j] = -1;
+				}
+#endif
+			} else {
+error:
+				zero_channel[i] = TRUE;
+			}
+			// So we just defer everything else to later
+
+			// at this point we've decoded the floor into buffer
+		}
+	}
+	CHECK(f);
+	// at this point we've decoded all floors
+
+	if (f->alloc.alloc_buffer)
+		assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
+
+	// re-enable coupled channels if necessary
+	memcpy(really_zero_channel, zero_channel, sizeof(really_zero_channel[0]) * f->channels);
+	for (i = 0; i < map->coupling_steps; ++i)
+		if (!zero_channel[map->chan[i].magnitude] || !zero_channel[map->chan[i].angle]) {
+			zero_channel[map->chan[i].magnitude] = zero_channel[map->chan[i].angle] = FALSE;
+		}
+
+	CHECK(f);
+	// RESIDUE DECODE
+	for (i = 0; i < map->submaps; ++i) {
+		float *residue_buffers[STB_VORBIS_MAX_CHANNELS];
+		int r;
+		uint8 do_not_decode[256];
+		int ch = 0;
+		for (j = 0; j < f->channels; ++j) {
+			if (map->chan[j].mux == i) {
+				if (zero_channel[j]) {
+					do_not_decode[ch] = TRUE;
+					residue_buffers[ch] = NULL;
+				} else {
+					do_not_decode[ch] = FALSE;
+					residue_buffers[ch] = f->channel_buffers[j];
+				}
+				++ch;
+			}
+		}
+		r = map->submap_residue[i];
+		decode_residue(f, residue_buffers, ch, n2, r, do_not_decode);
+	}
+
+	if (f->alloc.alloc_buffer)
+		assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
+	CHECK(f);
+
+	// INVERSE COUPLING
+	for (i = map->coupling_steps - 1; i >= 0; --i) {
+		int n2 = n >> 1;
+		float *m = f->channel_buffers[map->chan[i].magnitude];
+		float *a = f->channel_buffers[map->chan[i].angle];
+		for (j = 0; j < n2; ++j) {
+			float a2, m2;
+			if (m[j] > 0)
+				if (a[j] > 0)
+					m2 = m[j], a2 = m[j] - a[j];
+				else
+					a2 = m[j], m2 = m[j] + a[j];
+			else
+				if (a[j] > 0)
+					m2 = m[j], a2 = m[j] + a[j];
+				else
+					a2 = m[j], m2 = m[j] - a[j];
+			m[j] = m2;
+			a[j] = a2;
+		}
+	}
+	CHECK(f);
+
+	// finish decoding the floors
+#ifndef STB_VORBIS_NO_DEFER_FLOOR
+	for (i = 0; i < f->channels; ++i) {
+		if (really_zero_channel[i]) {
+			memset(f->channel_buffers[i], 0, sizeof(*f->channel_buffers[i]) * n2);
+		} else {
+			do_floor(f, map, i, n, f->channel_buffers[i], f->finalY[i], NULL);
+		}
+	}
+#else
+	for (i = 0; i < f->channels; ++i) {
+		if (really_zero_channel[i]) {
+			memset(f->channel_buffers[i], 0, sizeof(*f->channel_buffers[i]) * n2);
+		} else {
+			for (j = 0; j < n2; ++j)
+				f->channel_buffers[i][j] *= f->floor_buffers[i][j];
+		}
+	}
+#endif
+
+	// INVERSE MDCT
+	CHECK(f);
+	for (i = 0; i < f->channels; ++i)
+		inverse_mdct(f->channel_buffers[i], n, f, m->blockflag);
+	CHECK(f);
+
+	// this shouldn't be necessary, unless we exited on an error
+	// and want to flush to get to the next packet
+	flush_packet(f);
+
+	if (f->first_decode) {
+		// assume we start so first non-discarded sample is sample 0
+		// this isn't to spec, but spec would require us to read ahead
+		// and decode the size of all current frames--could be done,
+		// but presumably it's not a commonly used feature
+		f->current_loc = -n2; // start of first frame is positioned for discard
+							  // we might have to discard samples "from" the next frame too,
+							  // if we're lapping a large block then a small at the start?
+		f->discard_samples_deferred = n - right_end;
+		f->current_loc_valid = TRUE;
+		f->first_decode = FALSE;
+	} else if (f->discard_samples_deferred) {
+		if (f->discard_samples_deferred >= right_start - left_start) {
+			f->discard_samples_deferred -= (right_start - left_start);
+			left_start = right_start;
+			*p_left = left_start;
+		} else {
+			left_start += f->discard_samples_deferred;
+			*p_left = left_start;
+			f->discard_samples_deferred = 0;
+		}
+	} else if (f->previous_length == 0 && f->current_loc_valid) {
+		// we're recovering from a seek... that means we're going to discard
+		// the samples from this packet even though we know our position from
+		// the last page header, so we need to update the position based on
+		// the discarded samples here
+		// but wait, the code below is going to add this in itself even
+		// on a discard, so we don't need to do it here...
+	}
+
+	// check if we have ogg information about the sample # for this packet
+	if (f->last_seg_which == f->end_seg_with_known_loc) {
+		// if we have a valid current loc, and this is final:
+		if (f->current_loc_valid && (f->page_flag & PAGEFLAG_last_page)) {
+			uint32 current_end = f->known_loc_for_packet - (n - right_end);
+			// then let's infer the size of the (probably) short final frame
+			if (current_end < f->current_loc + (right_end - left_start)) {
+				if (current_end < f->current_loc) {
+					// negative truncation, that's impossible!
+					*len = 0;
+				} else {
+					*len = current_end - f->current_loc;
+				}
+				*len += left_start;
+				if (*len > right_end) *len = right_end; // this should never happen
+				f->current_loc += *len;
+				return TRUE;
+			}
+		}
+		// otherwise, just set our sample loc
+		// guess that the ogg granule pos refers to the _middle_ of the
+		// last frame?
+		// set f->current_loc to the position of left_start
+		f->current_loc = f->known_loc_for_packet - (n2 - left_start);
+		f->current_loc_valid = TRUE;
+	}
+	if (f->current_loc_valid)
+		f->current_loc += (right_start - left_start);
+
+	if (f->alloc.alloc_buffer)
+		assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
+	*len = right_end;  // ignore samples after the window goes to 0
+	CHECK(f);
+
+	return TRUE;
+}
+
+static int vorbis_decode_packet(vorb *f, int *len, int *p_left, int *p_right) {
+	int mode, left_end, right_end;
+	if (!vorbis_decode_initial(f, p_left, &left_end, p_right, &right_end, &mode)) return 0;
+	return vorbis_decode_packet_rest(f, len, f->mode_config + mode, *p_left, left_end, *p_right, right_end, p_left);
+}
+
+static int vorbis_finish_frame(stb_vorbis *f, int len, int left, int right) {
+	int prev, i, j;
+	// we use right&left (the start of the right- and left-window sin()-regions)
+	// to determine how much to return, rather than inferring from the rules
+	// (same result, clearer code); 'left' indicates where our sin() window
+	// starts, therefore where the previous window's right edge starts, and
+	// therefore where to start mixing from the previous buffer. 'right'
+	// indicates where our sin() ending-window starts, therefore that's where
+	// we start saving, and where our returned-data ends.
+
+	// mixin from previous window
+	if (f->previous_length) {
+		int i, j, n = f->previous_length;
+		float *w = get_window(f, n);
+		for (i = 0; i < f->channels; ++i) {
+			for (j = 0; j < n; ++j)
+				f->channel_buffers[i][left + j] =
+				f->channel_buffers[i][left + j] * w[j] +
+				f->previous_window[i][j] * w[n - 1 - j];
+		}
+	}
+
+	prev = f->previous_length;
+
+	// last half of this data becomes previous window
+	f->previous_length = len - right;
+
+	// @OPTIMIZE: could avoid this copy by double-buffering the
+	// output (flipping previous_window with channel_buffers), but
+	// then previous_window would have to be 2x as large, and
+	// channel_buffers couldn't be temp mem (although they're NOT
+	// currently temp mem, they could be (unless we want to level
+	// performance by spreading out the computation))
+	for (i = 0; i < f->channels; ++i)
+		for (j = 0; right + j < len; ++j)
+			f->previous_window[i][j] = f->channel_buffers[i][right + j];
+
+	if (!prev)
+		// there was no previous packet, so this data isn't valid...
+		// this isn't entirely true, only the would-have-overlapped data
+		// isn't valid, but this seems to be what the spec requires
+		return 0;
+
+	// truncate a short frame
+	if (len < right) right = len;
+
+	f->samples_output += right - left;
+
+	return right - left;
+}
+
+static void vorbis_pump_first_frame(stb_vorbis *f) {
+	int len, right, left;
+	if (vorbis_decode_packet(f, &len, &left, &right))
+		vorbis_finish_frame(f, len, left, right);
+}
+
+#ifndef STB_VORBIS_NO_PUSHDATA_API
+static int is_whole_packet_present(stb_vorbis *f, int end_page) {
+	// make sure that we have the packet available before continuing...
+	// this requires a full ogg parse, but we know we can fetch from f->stream
+
+	// instead of coding this out explicitly, we could save the current read state,
+	// read the next packet with get8() until end-of-packet, check f->eof, then
+	// reset the state? but that would be slower, esp. since we'd have over 256 bytes
+	// of state to restore (primarily the page segment table)
+
+	int s = f->next_seg, first = TRUE;
+	uint8 *p = f->stream;
+
+	if (s != -1) { // if we're not starting the packet with a 'continue on next page' flag
+		for (; s < f->segment_count; ++s) {
+			p += f->segments[s];
+			if (f->segments[s] < 255)               // stop at first short segment
+				break;
+		}
+		// either this continues, or it ends it...
+		if (end_page)
+			if (s < f->segment_count - 1)             return error(f, VORBIS_invalid_stream);
+		if (s == f->segment_count)
+			s = -1; // set 'crosses page' flag
+		if (p > f->stream_end)                     return error(f, VORBIS_need_more_data);
+		first = FALSE;
+	}
+	for (; s == -1;) {
+		uint8 *q;
+		int n;
+
+		// check that we have the page header ready
+		if (p + 26 >= f->stream_end)               return error(f, VORBIS_need_more_data);
+		// validate the page
+		if (memcmp(p, ogg_page_header, 4))         return error(f, VORBIS_invalid_stream);
+		if (p[4] != 0)                             return error(f, VORBIS_invalid_stream);
+		if (first) { // the first segment must NOT have 'continued_packet', later ones MUST
+			if (f->previous_length)
+				if ((p[5] & PAGEFLAG_continued_packet))  return error(f, VORBIS_invalid_stream);
+			// if no previous length, we're resynching, so we can come in on a continued-packet,
+			// which we'll just drop
+		} else {
+			if (!(p[5] & PAGEFLAG_continued_packet)) return error(f, VORBIS_invalid_stream);
+		}
+		n = p[26]; // segment counts
+		q = p + 27;  // q points to segment table
+		p = q + n; // advance past header
+				   // make sure we've read the segment table
+		if (p > f->stream_end)                     return error(f, VORBIS_need_more_data);
+		for (s = 0; s < n; ++s) {
+			p += q[s];
+			if (q[s] < 255)
+				break;
+		}
+		if (end_page)
+			if (s < n - 1)                            return error(f, VORBIS_invalid_stream);
+		if (s == n)
+			s = -1; // set 'crosses page' flag
+		if (p > f->stream_end)                     return error(f, VORBIS_need_more_data);
+		first = FALSE;
+	}
+	return TRUE;
+}
+#endif // !STB_VORBIS_NO_PUSHDATA_API
+
+static int start_decoder(vorb *f) {
+	uint8 header[6], x, y;
+	int len, i, j, k, max_submaps = 0;
+	int longest_floorlist = 0;
+
+	// first page, first packet
+
+	if (!start_page(f))                              return FALSE;
+	// validate page flag
+	if (!(f->page_flag & PAGEFLAG_first_page))       return error(f, VORBIS_invalid_first_page);
+	if (f->page_flag & PAGEFLAG_last_page)           return error(f, VORBIS_invalid_first_page);
+	if (f->page_flag & PAGEFLAG_continued_packet)    return error(f, VORBIS_invalid_first_page);
+	// check for expected packet length
+	if (f->segment_count != 1)                       return error(f, VORBIS_invalid_first_page);
+	if (f->segments[0] != 30)                        return error(f, VORBIS_invalid_first_page);
+	// read packet
+	// check packet header
+	if (get8(f) != VORBIS_packet_id)                 return error(f, VORBIS_invalid_first_page);
+	if (!getn(f, header, 6))                         return error(f, VORBIS_unexpected_eof);
+	if (!vorbis_validate(header))                    return error(f, VORBIS_invalid_first_page);
+	// vorbis_version
+	if (get32(f) != 0)                               return error(f, VORBIS_invalid_first_page);
+	f->channels = get8(f); if (!f->channels)         return error(f, VORBIS_invalid_first_page);
+	if (f->channels > STB_VORBIS_MAX_CHANNELS)       return error(f, VORBIS_too_many_channels);
+	f->sample_rate = get32(f); if (!f->sample_rate)  return error(f, VORBIS_invalid_first_page);
+	get32(f); // bitrate_maximum
+	get32(f); // bitrate_nominal
+	get32(f); // bitrate_minimum
+	x = get8(f);
+	{
+		int log0, log1;
+		log0 = x & 15;
+		log1 = x >> 4;
+		f->blocksize_0 = 1 << log0;
+		f->blocksize_1 = 1 << log1;
+		if (log0 < 6 || log0 > 13)                       return error(f, VORBIS_invalid_setup);
+		if (log1 < 6 || log1 > 13)                       return error(f, VORBIS_invalid_setup);
+		if (log0 > log1)                                 return error(f, VORBIS_invalid_setup);
+	}
+
+	// framing_flag
+	x = get8(f);
+	if (!(x & 1))                                    return error(f, VORBIS_invalid_first_page);
+
+	// second packet!
+	if (!start_page(f))                              return FALSE;
+
+	if (!start_packet(f))                            return FALSE;
+	do {
+		len = next_segment(f);
+		skip(f, len);
+		f->bytes_in_seg = 0;
+	} while (len);
+
+	// third packet!
+	if (!start_packet(f))                            return FALSE;
+
+#ifndef STB_VORBIS_NO_PUSHDATA_API
+	if (IS_PUSH_MODE(f)) {
+		if (!is_whole_packet_present(f, TRUE)) {
+			// convert error in ogg header to write type
+			if (f->error == VORBIS_invalid_stream)
+				f->error = VORBIS_invalid_setup;
+			return FALSE;
+		}
+	}
+#endif
+
+	crc32_init(); // always init it, to avoid multithread race conditions
+
+	if (get8_packet(f) != VORBIS_packet_setup)       return error(f, VORBIS_invalid_setup);
+	for (i = 0; i < 6; ++i) header[i] = get8_packet(f);
+	if (!vorbis_validate(header))                    return error(f, VORBIS_invalid_setup);
+
+	// codebooks
+
+	f->codebook_count = get_bits(f, 8) + 1;
+	f->codebooks = (Codebook *) setup_malloc(f, sizeof(*f->codebooks) * f->codebook_count);
+	if (f->codebooks == NULL)                        return error(f, VORBIS_outofmem);
+	memset(f->codebooks, 0, sizeof(*f->codebooks) * f->codebook_count);
+	for (i = 0; i < f->codebook_count; ++i) {
+		uint32 *values;
+		int ordered, sorted_count;
+		int total = 0;
+		uint8 *lengths;
+		Codebook *c = f->codebooks + i;
+		CHECK(f);
+		x = get_bits(f, 8); if (x != 0x42)            return error(f, VORBIS_invalid_setup);
+		x = get_bits(f, 8); if (x != 0x43)            return error(f, VORBIS_invalid_setup);
+		x = get_bits(f, 8); if (x != 0x56)            return error(f, VORBIS_invalid_setup);
+		x = get_bits(f, 8);
+		c->dimensions = (get_bits(f, 8) << 8) + x;
+		x = get_bits(f, 8);
+		y = get_bits(f, 8);
+		c->entries = (get_bits(f, 8) << 16) + (y << 8) + x;
+		ordered = get_bits(f, 1);
+		c->sparse = ordered ? 0 : get_bits(f, 1);
+
+		if (c->dimensions == 0 && c->entries != 0)    return error(f, VORBIS_invalid_setup);
+
+		if (c->sparse)
+			lengths = (uint8 *) setup_temp_malloc(f, c->entries);
+		else
+			lengths = c->codeword_lengths = (uint8 *) setup_malloc(f, c->entries);
+
+		if (!lengths) return error(f, VORBIS_outofmem);
+
+		if (ordered) {
+			int current_entry = 0;
+			int current_length = get_bits(f, 5) + 1;
+			while (current_entry < c->entries) {
+				int limit = c->entries - current_entry;
+				int n = get_bits(f, ilog(limit));
+				if (current_entry + n >(int) c->entries) { return error(f, VORBIS_invalid_setup); }
+				memset(lengths + current_entry, current_length, n);
+				current_entry += n;
+				++current_length;
+			}
+		} else {
+			for (j = 0; j < c->entries; ++j) {
+				int present = c->sparse ? get_bits(f, 1) : 1;
+				if (present) {
+					lengths[j] = get_bits(f, 5) + 1;
+					++total;
+					if (lengths[j] == 32)
+						return error(f, VORBIS_invalid_setup);
+				} else {
+					lengths[j] = NO_CODE;
+				}
+			}
+		}
+
+		if (c->sparse && total >= c->entries >> 2) {
+			// convert sparse items to non-sparse!
+			if (c->entries > (int) f->setup_temp_memory_required)
+				f->setup_temp_memory_required = c->entries;
+
+			c->codeword_lengths = (uint8 *) setup_malloc(f, c->entries);
+			if (c->codeword_lengths == NULL) return error(f, VORBIS_outofmem);
+			memcpy(c->codeword_lengths, lengths, c->entries);
+			setup_temp_free(f, lengths, c->entries); // note this is only safe if there have been no intervening temp mallocs!
+			lengths = c->codeword_lengths;
+			c->sparse = 0;
+		}
+
+		// compute the size of the sorted tables
+		if (c->sparse) {
+			sorted_count = total;
+		} else {
+			sorted_count = 0;
+#ifndef STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH
+			for (j = 0; j < c->entries; ++j)
+				if (lengths[j] > STB_VORBIS_FAST_HUFFMAN_LENGTH && lengths[j] != NO_CODE)
+					++sorted_count;
+#endif
+		}
+
+		c->sorted_entries = sorted_count;
+		values = NULL;
+
+		CHECK(f);
+		if (!c->sparse) {
+			c->codewords = (uint32 *) setup_malloc(f, sizeof(c->codewords[0]) * c->entries);
+			if (!c->codewords)                  return error(f, VORBIS_outofmem);
+		} else {
+			unsigned int size;
+			if (c->sorted_entries) {
+				c->codeword_lengths = (uint8 *) setup_malloc(f, c->sorted_entries);
+				if (!c->codeword_lengths)           return error(f, VORBIS_outofmem);
+				c->codewords = (uint32 *) setup_temp_malloc(f, sizeof(*c->codewords) * c->sorted_entries);
+				if (!c->codewords)                  return error(f, VORBIS_outofmem);
+				values = (uint32 *) setup_temp_malloc(f, sizeof(*values) * c->sorted_entries);
+				if (!values)                        return error(f, VORBIS_outofmem);
+			}
+			size = c->entries + (sizeof(*c->codewords) + sizeof(*values)) * c->sorted_entries;
+			if (size > f->setup_temp_memory_required)
+				f->setup_temp_memory_required = size;
+		}
+
+		if (!compute_codewords(c, lengths, c->entries, values)) {
+			if (c->sparse) setup_temp_free(f, values, 0);
+			return error(f, VORBIS_invalid_setup);
+		}
+
+		if (c->sorted_entries) {
+			// allocate an extra slot for sentinels
+			c->sorted_codewords = (uint32 *) setup_malloc(f, sizeof(*c->sorted_codewords) * (c->sorted_entries + 1));
+			if (c->sorted_codewords == NULL) return error(f, VORBIS_outofmem);
+			// allocate an extra slot at the front so that c->sorted_values[-1] is defined
+			// so that we can catch that case without an extra if
+			c->sorted_values = (int   *) setup_malloc(f, sizeof(*c->sorted_values) * (c->sorted_entries + 1));
+			if (c->sorted_values == NULL) return error(f, VORBIS_outofmem);
+			++c->sorted_values;
+			c->sorted_values[-1] = -1;
+			compute_sorted_huffman(c, lengths, values);
+		}
+
+		if (c->sparse) {
+			setup_temp_free(f, values, sizeof(*values)*c->sorted_entries);
+			setup_temp_free(f, c->codewords, sizeof(*c->codewords)*c->sorted_entries);
+			setup_temp_free(f, lengths, c->entries);
+			c->codewords = NULL;
+		}
+
+		compute_accelerated_huffman(c);
+
+		CHECK(f);
+		c->lookup_type = get_bits(f, 4);
+		if (c->lookup_type > 2) return error(f, VORBIS_invalid_setup);
+		if (c->lookup_type > 0) {
+			uint16 *mults;
+			c->minimum_value = float32_unpack(get_bits(f, 32));
+			c->delta_value = float32_unpack(get_bits(f, 32));
+			c->value_bits = get_bits(f, 4) + 1;
+			c->sequence_p = get_bits(f, 1);
+			if (c->lookup_type == 1) {
+				c->lookup_values = lookup1_values(c->entries, c->dimensions);
+			} else {
+				c->lookup_values = c->entries * c->dimensions;
+			}
+			if (c->lookup_values == 0) return error(f, VORBIS_invalid_setup);
+			mults = (uint16 *) setup_temp_malloc(f, sizeof(mults[0]) * c->lookup_values);
+			if (mults == NULL) return error(f, VORBIS_outofmem);
+			for (j = 0; j < (int) c->lookup_values; ++j) {
+				int q = get_bits(f, c->value_bits);
+				if (q == EOP) { setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_invalid_setup); }
+				mults[j] = q;
+			}
+
+#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
+			if (c->lookup_type == 1) {
+				int len, sparse = c->sparse;
+				float last = 0;
+				// pre-expand the lookup1-style multiplicands, to avoid a divide in the inner loop
+				if (sparse) {
+					if (c->sorted_entries == 0) goto skip;
+					c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->sorted_entries * c->dimensions);
+				} else
+					c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->entries        * c->dimensions);
+				if (c->multiplicands == NULL) { setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_outofmem); }
+				len = sparse ? c->sorted_entries : c->entries;
+				for (j = 0; j < len; ++j) {
+					unsigned int z = sparse ? c->sorted_values[j] : j;
+					unsigned int div = 1;
+					for (k = 0; k < c->dimensions; ++k) {
+						int off = (z / div) % c->lookup_values;
+						float val = mults[off];
+						val = mults[off] * c->delta_value + c->minimum_value + last;
+						c->multiplicands[j*c->dimensions + k] = val;
+						if (c->sequence_p)
+							last = val;
+						if (k + 1 < c->dimensions) {
+							if (div > UINT_MAX / (unsigned int) c->lookup_values) {
+								setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values);
+								return error(f, VORBIS_invalid_setup);
+							}
+							div *= c->lookup_values;
+						}
+					}
+				}
+				c->lookup_type = 2;
+			} else
+#endif
+			{
+				float last = 0;
+				CHECK(f);
+				c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->lookup_values);
+				if (c->multiplicands == NULL) { setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_outofmem); }
+				for (j = 0; j < (int) c->lookup_values; ++j) {
+					float val = mults[j] * c->delta_value + c->minimum_value + last;
+					c->multiplicands[j] = val;
+					if (c->sequence_p)
+						last = val;
+				}
+			}
+#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
+			skip : ;
+#endif
+				   setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values);
+
+				   CHECK(f);
+		}
+		CHECK(f);
+	}
+
+	// time domain transfers (notused)
+
+	x = get_bits(f, 6) + 1;
+	for (i = 0; i < x; ++i) {
+		uint32 z = get_bits(f, 16);
+		if (z != 0) return error(f, VORBIS_invalid_setup);
+	}
+
+	// Floors
+	f->floor_count = get_bits(f, 6) + 1;
+	f->floor_config = (Floor *) setup_malloc(f, f->floor_count * sizeof(*f->floor_config));
+	if (f->floor_config == NULL) return error(f, VORBIS_outofmem);
+	for (i = 0; i < f->floor_count; ++i) {
+		f->floor_types[i] = get_bits(f, 16);
+		if (f->floor_types[i] > 1) return error(f, VORBIS_invalid_setup);
+		if (f->floor_types[i] == 0) {
+			Floor0 *g = &f->floor_config[i].floor0;
+			g->order = get_bits(f, 8);
+			g->rate = get_bits(f, 16);
+			g->bark_map_size = get_bits(f, 16);
+			g->amplitude_bits = get_bits(f, 6);
+			g->amplitude_offset = get_bits(f, 8);
+			g->number_of_books = get_bits(f, 4) + 1;
+			for (j = 0; j < g->number_of_books; ++j)
+				g->book_list[j] = get_bits(f, 8);
+			return error(f, VORBIS_feature_not_supported);
+		} else {
+			Point p[31 * 8 + 2];
+			Floor1 *g = &f->floor_config[i].floor1;
+			int max_class = -1;
+			g->partitions = get_bits(f, 5);
+			for (j = 0; j < g->partitions; ++j) {
+				g->partition_class_list[j] = get_bits(f, 4);
+				if (g->partition_class_list[j] > max_class)
+					max_class = g->partition_class_list[j];
+			}
+			for (j = 0; j <= max_class; ++j) {
+				g->class_dimensions[j] = get_bits(f, 3) + 1;
+				g->class_subclasses[j] = get_bits(f, 2);
+				if (g->class_subclasses[j]) {
+					g->class_masterbooks[j] = get_bits(f, 8);
+					if (g->class_masterbooks[j] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
+				}
+				for (k = 0; k < 1 << g->class_subclasses[j]; ++k) {
+					g->subclass_books[j][k] = get_bits(f, 8) - 1;
+					if (g->subclass_books[j][k] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
+				}
+			}
+			g->floor1_multiplier = get_bits(f, 2) + 1;
+			g->rangebits = get_bits(f, 4);
+			g->Xlist[0] = 0;
+			g->Xlist[1] = 1 << g->rangebits;
+			g->values = 2;
+			for (j = 0; j < g->partitions; ++j) {
+				int c = g->partition_class_list[j];
+				for (k = 0; k < g->class_dimensions[c]; ++k) {
+					g->Xlist[g->values] = get_bits(f, g->rangebits);
+					++g->values;
+				}
+			}
+			// precompute the sorting
+			for (j = 0; j < g->values; ++j) {
+				p[j].x = g->Xlist[j];
+				p[j].y = j;
+			}
+			qsort(p, g->values, sizeof(p[0]), point_compare);
+			for (j = 0; j < g->values; ++j)
+				g->sorted_order[j] = (uint8) p[j].y;
+			// precompute the neighbors
+			for (j = 2; j < g->values; ++j) {
+				int low, hi;
+				neighbors(g->Xlist, j, &low, &hi);
+				g->neighbors[j][0] = low;
+				g->neighbors[j][1] = hi;
+			}
+
+			if (g->values > longest_floorlist)
+				longest_floorlist = g->values;
+		}
+	}
+
+	// Residue
+	f->residue_count = get_bits(f, 6) + 1;
+	f->residue_config = (Residue *) setup_malloc(f, f->residue_count * sizeof(f->residue_config[0]));
+	if (f->residue_config == NULL) return error(f, VORBIS_outofmem);
+	memset(f->residue_config, 0, f->residue_count * sizeof(f->residue_config[0]));
+	for (i = 0; i < f->residue_count; ++i) {
+		uint8 residue_cascade[64];
+		Residue *r = f->residue_config + i;
+		f->residue_types[i] = get_bits(f, 16);
+		if (f->residue_types[i] > 2) return error(f, VORBIS_invalid_setup);
+		r->begin = get_bits(f, 24);
+		r->end = get_bits(f, 24);
+		if (r->end < r->begin) return error(f, VORBIS_invalid_setup);
+		r->part_size = get_bits(f, 24) + 1;
+		r->classifications = get_bits(f, 6) + 1;
+		r->classbook = get_bits(f, 8);
+		if (r->classbook >= f->codebook_count) return error(f, VORBIS_invalid_setup);
+		for (j = 0; j < r->classifications; ++j) {
+			uint8 high_bits = 0;
+			uint8 low_bits = get_bits(f, 3);
+			if (get_bits(f, 1))
+				high_bits = get_bits(f, 5);
+			residue_cascade[j] = high_bits * 8 + low_bits;
+		}
+		r->residue_books = (short(*)[8]) setup_malloc(f, sizeof(r->residue_books[0]) * r->classifications);
+		if (r->residue_books == NULL) return error(f, VORBIS_outofmem);
+		for (j = 0; j < r->classifications; ++j) {
+			for (k = 0; k < 8; ++k) {
+				if (residue_cascade[j] & (1 << k)) {
+					r->residue_books[j][k] = get_bits(f, 8);
+					if (r->residue_books[j][k] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
+				} else {
+					r->residue_books[j][k] = -1;
+				}
+			}
+		}
+		// precompute the classifications[] array to avoid inner-loop mod/divide
+		// call it 'classdata' since we already have r->classifications
+		r->classdata = (uint8 **) setup_malloc(f, sizeof(*r->classdata) * f->codebooks[r->classbook].entries);
+		if (!r->classdata) return error(f, VORBIS_outofmem);
+		memset(r->classdata, 0, sizeof(*r->classdata) * f->codebooks[r->classbook].entries);
+		for (j = 0; j < f->codebooks[r->classbook].entries; ++j) {
+			int classwords = f->codebooks[r->classbook].dimensions;
+			int temp = j;
+			r->classdata[j] = (uint8 *) setup_malloc(f, sizeof(r->classdata[j][0]) * classwords);
+			if (r->classdata[j] == NULL) return error(f, VORBIS_outofmem);
+			for (k = classwords - 1; k >= 0; --k) {
+				r->classdata[j][k] = temp % r->classifications;
+				temp /= r->classifications;
+			}
+		}
+	}
+
+	f->mapping_count = get_bits(f, 6) + 1;
+	f->mapping = (Mapping *) setup_malloc(f, f->mapping_count * sizeof(*f->mapping));
+	if (f->mapping == NULL) return error(f, VORBIS_outofmem);
+	memset(f->mapping, 0, f->mapping_count * sizeof(*f->mapping));
+	for (i = 0; i < f->mapping_count; ++i) {
+		Mapping *m = f->mapping + i;
+		int mapping_type = get_bits(f, 16);
+		if (mapping_type != 0) return error(f, VORBIS_invalid_setup);
+		m->chan = (MappingChannel *) setup_malloc(f, f->channels * sizeof(*m->chan));
+		if (m->chan == NULL) return error(f, VORBIS_outofmem);
+		if (get_bits(f, 1))
+			m->submaps = get_bits(f, 4) + 1;
+		else
+			m->submaps = 1;
+		if (m->submaps > max_submaps)
+			max_submaps = m->submaps;
+		if (get_bits(f, 1)) {
+			m->coupling_steps = get_bits(f, 8) + 1;
+			for (k = 0; k < m->coupling_steps; ++k) {
+				m->chan[k].magnitude = get_bits(f, ilog(f->channels - 1));
+				m->chan[k].angle = get_bits(f, ilog(f->channels - 1));
+				if (m->chan[k].magnitude >= f->channels)        return error(f, VORBIS_invalid_setup);
+				if (m->chan[k].angle >= f->channels)        return error(f, VORBIS_invalid_setup);
+				if (m->chan[k].magnitude == m->chan[k].angle)   return error(f, VORBIS_invalid_setup);
+			}
+		} else
+			m->coupling_steps = 0;
+
+		// reserved field
+		if (get_bits(f, 2)) return error(f, VORBIS_invalid_setup);
+		if (m->submaps > 1) {
+			for (j = 0; j < f->channels; ++j) {
+				m->chan[j].mux = get_bits(f, 4);
+				if (m->chan[j].mux >= m->submaps)                return error(f, VORBIS_invalid_setup);
+			}
+		} else
+			// @SPECIFICATION: this case is missing from the spec
+			for (j = 0; j < f->channels; ++j)
+				m->chan[j].mux = 0;
+
+		for (j = 0; j < m->submaps; ++j) {
+			get_bits(f, 8); // discard
+			m->submap_floor[j] = get_bits(f, 8);
+			m->submap_residue[j] = get_bits(f, 8);
+			if (m->submap_floor[j] >= f->floor_count)      return error(f, VORBIS_invalid_setup);
+			if (m->submap_residue[j] >= f->residue_count)  return error(f, VORBIS_invalid_setup);
+		}
+	}
+
+	// Modes
+	f->mode_count = get_bits(f, 6) + 1;
+	for (i = 0; i < f->mode_count; ++i) {
+		Mode *m = f->mode_config + i;
+		m->blockflag = get_bits(f, 1);
+		m->windowtype = get_bits(f, 16);
+		m->transformtype = get_bits(f, 16);
+		m->mapping = get_bits(f, 8);
+		if (m->windowtype != 0)                 return error(f, VORBIS_invalid_setup);
+		if (m->transformtype != 0)              return error(f, VORBIS_invalid_setup);
+		if (m->mapping >= f->mapping_count)     return error(f, VORBIS_invalid_setup);
+	}
+
+	flush_packet(f);
+
+	f->previous_length = 0;
+
+	for (i = 0; i < f->channels; ++i) {
+		f->channel_buffers[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1);
+		f->previous_window[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1 / 2);
+		f->finalY[i] = (int16 *) setup_malloc(f, sizeof(int16) * longest_floorlist);
+		if (f->channel_buffers[i] == NULL || f->previous_window[i] == NULL || f->finalY[i] == NULL) return error(f, VORBIS_outofmem);
+#ifdef STB_VORBIS_NO_DEFER_FLOOR
+		f->floor_buffers[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1 / 2);
+		if (f->floor_buffers[i] == NULL) return error(f, VORBIS_outofmem);
+#endif
+	}
+
+	if (!init_blocksize(f, 0, f->blocksize_0)) return FALSE;
+	if (!init_blocksize(f, 1, f->blocksize_1)) return FALSE;
+	f->blocksize[0] = f->blocksize_0;
+	f->blocksize[1] = f->blocksize_1;
+
+#ifdef STB_VORBIS_DIVIDE_TABLE
+	if (integer_divide_table[1][1] == 0)
+		for (i = 0; i < DIVTAB_NUMER; ++i)
+			for (j = 1; j < DIVTAB_DENOM; ++j)
+				integer_divide_table[i][j] = i / j;
+#endif
+
+	// compute how much temporary memory is needed
+
+	// 1.
+	{
+		uint32 imdct_mem = (f->blocksize_1 * sizeof(float) >> 1);
+		uint32 classify_mem;
+		int i, max_part_read = 0;
+		for (i = 0; i < f->residue_count; ++i) {
+			Residue *r = f->residue_config + i;
+			int n_read = r->end - r->begin;
+			int part_read = n_read / r->part_size;
+			if (part_read > max_part_read)
+				max_part_read = part_read;
+		}
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+		classify_mem = f->channels * (sizeof(void*) + max_part_read * sizeof(uint8 *));
+#else
+		classify_mem = f->channels * (sizeof(void*) + max_part_read * sizeof(int *));
+#endif
+
+		f->temp_memory_required = classify_mem;
+		if (imdct_mem > f->temp_memory_required)
+			f->temp_memory_required = imdct_mem;
+	}
+
+	f->first_decode = TRUE;
+
+	if (f->alloc.alloc_buffer) {
+		assert(f->temp_offset == f->alloc.alloc_buffer_length_in_bytes);
+		// check if there's enough temp memory so we don't error later
+		if (f->setup_offset + sizeof(*f) + f->temp_memory_required > (unsigned) f->temp_offset)
+			return error(f, VORBIS_outofmem);
+	}
+
+	f->first_audio_page_offset = stb_vorbis_get_file_offset(f);
+
+	return TRUE;
+}
+
+static void vorbis_deinit(stb_vorbis *p) {
+	int i, j;
+	if (p->residue_config) {
+		for (i = 0; i < p->residue_count; ++i) {
+			Residue *r = p->residue_config + i;
+			if (r->classdata) {
+				for (j = 0; j < p->codebooks[r->classbook].entries; ++j)
+					setup_free(p, r->classdata[j]);
+				setup_free(p, r->classdata);
+			}
+			setup_free(p, r->residue_books);
+		}
+	}
+
+	if (p->codebooks) {
+		CHECK(p);
+		for (i = 0; i < p->codebook_count; ++i) {
+			Codebook *c = p->codebooks + i;
+			setup_free(p, c->codeword_lengths);
+			setup_free(p, c->multiplicands);
+			setup_free(p, c->codewords);
+			setup_free(p, c->sorted_codewords);
+			// c->sorted_values[-1] is the first entry in the array
+			setup_free(p, c->sorted_values ? c->sorted_values - 1 : NULL);
+		}
+		setup_free(p, p->codebooks);
+	}
+	setup_free(p, p->floor_config);
+	setup_free(p, p->residue_config);
+	if (p->mapping) {
+		for (i = 0; i < p->mapping_count; ++i)
+			setup_free(p, p->mapping[i].chan);
+		setup_free(p, p->mapping);
+	}
+	CHECK(p);
+	for (i = 0; i < p->channels && i < STB_VORBIS_MAX_CHANNELS; ++i) {
+		setup_free(p, p->channel_buffers[i]);
+		setup_free(p, p->previous_window[i]);
+#ifdef STB_VORBIS_NO_DEFER_FLOOR
+		setup_free(p, p->floor_buffers[i]);
+#endif
+		setup_free(p, p->finalY[i]);
+	}
+	for (i = 0; i < 2; ++i) {
+		setup_free(p, p->A[i]);
+		setup_free(p, p->B[i]);
+		setup_free(p, p->C[i]);
+		setup_free(p, p->window[i]);
+		setup_free(p, p->bit_reverse[i]);
+	}
+#ifndef STB_VORBIS_NO_STDIO
+	if (p->close_on_free) fclose(p->f);
+#endif
+}
+
+void stb_vorbis_close(stb_vorbis *p) {
+	if (p == NULL) return;
+	vorbis_deinit(p);
+	setup_free(p, p);
+}
+
+static void vorbis_init(stb_vorbis *p, const stb_vorbis_alloc *z) {
+	memset(p, 0, sizeof(*p)); // NULL out all malloc'd pointers to start
+	if (z) {
+		p->alloc = *z;
+		p->alloc.alloc_buffer_length_in_bytes = (p->alloc.alloc_buffer_length_in_bytes + 3) & ~3;
+		p->temp_offset = p->alloc.alloc_buffer_length_in_bytes;
+	}
+	p->eof = 0;
+	p->error = VORBIS__no_error;
+	p->stream = NULL;
+	p->codebooks = NULL;
+	p->page_crc_tests = -1;
+#ifndef STB_VORBIS_NO_STDIO
+	p->close_on_free = FALSE;
+	p->f = NULL;
+#endif
+}
+
+int stb_vorbis_get_sample_offset(stb_vorbis *f) {
+	if (f->current_loc_valid)
+		return f->current_loc;
+	else
+		return -1;
+}
+
+stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f) {
+	stb_vorbis_info d;
+	d.channels = f->channels;
+	d.sample_rate = f->sample_rate;
+	d.setup_memory_required = f->setup_memory_required;
+	d.setup_temp_memory_required = f->setup_temp_memory_required;
+	d.temp_memory_required = f->temp_memory_required;
+	d.max_frame_size = f->blocksize_1 >> 1;
+	return d;
+}
+
+int stb_vorbis_get_error(stb_vorbis *f) {
+	int e = f->error;
+	f->error = VORBIS__no_error;
+	return e;
+}
+
+static stb_vorbis * vorbis_alloc(stb_vorbis *f) {
+	stb_vorbis *p = (stb_vorbis *) setup_malloc(f, sizeof(*p));
+	return p;
+}
+
+#ifndef STB_VORBIS_NO_PUSHDATA_API
+
+void stb_vorbis_flush_pushdata(stb_vorbis *f) {
+	f->previous_length = 0;
+	f->page_crc_tests = 0;
+	f->discard_samples_deferred = 0;
+	f->current_loc_valid = FALSE;
+	f->first_decode = FALSE;
+	f->samples_output = 0;
+	f->channel_buffer_start = 0;
+	f->channel_buffer_end = 0;
+}
+
+static int vorbis_search_for_page_pushdata(vorb *f, uint8 *data, int data_len) {
+	int i, n;
+	for (i = 0; i < f->page_crc_tests; ++i)
+		f->scan[i].bytes_done = 0;
+
+	// if we have room for more scans, search for them first, because
+	// they may cause us to stop early if their header is incomplete
+	if (f->page_crc_tests < STB_VORBIS_PUSHDATA_CRC_COUNT) {
+		if (data_len < 4) return 0;
+		data_len -= 3; // need to look for 4-byte sequence, so don't miss
+					   // one that straddles a boundary
+		for (i = 0; i < data_len; ++i) {
+			if (data[i] == 0x4f) {
+				if (0 == memcmp(data + i, ogg_page_header, 4)) {
+					int j, len;
+					uint32 crc;
+					// make sure we have the whole page header
+					if (i + 26 >= data_len || i + 27 + data[i + 26] >= data_len) {
+						// only read up to this page start, so hopefully we'll
+						// have the whole page header start next time
+						data_len = i;
+						break;
+					}
+					// ok, we have it all; compute the length of the page
+					len = 27 + data[i + 26];
+					for (j = 0; j < data[i + 26]; ++j)
+						len += data[i + 27 + j];
+					// scan everything up to the embedded crc (which we must 0)
+					crc = 0;
+					for (j = 0; j < 22; ++j)
+						crc = crc32_update(crc, data[i + j]);
+					// now process 4 0-bytes
+					for (; j < 26; ++j)
+						crc = crc32_update(crc, 0);
+					// len is the total number of bytes we need to scan
+					n = f->page_crc_tests++;
+					f->scan[n].bytes_left = len - j;
+					f->scan[n].crc_so_far = crc;
+					f->scan[n].goal_crc = data[i + 22] + (data[i + 23] << 8) + (data[i + 24] << 16) + (data[i + 25] << 24);
+					// if the last frame on a page is continued to the next, then
+					// we can't recover the sample_loc immediately
+					if (data[i + 27 + data[i + 26] - 1] == 255)
+						f->scan[n].sample_loc = ~0;
+					else
+						f->scan[n].sample_loc = data[i + 6] + (data[i + 7] << 8) + (data[i + 8] << 16) + (data[i + 9] << 24);
+					f->scan[n].bytes_done = i + j;
+					if (f->page_crc_tests == STB_VORBIS_PUSHDATA_CRC_COUNT)
+						break;
+					// keep going if we still have room for more
+				}
+			}
+		}
+	}
+
+	for (i = 0; i < f->page_crc_tests;) {
+		uint32 crc;
+		int j;
+		int n = f->scan[i].bytes_done;
+		int m = f->scan[i].bytes_left;
+		if (m > data_len - n) m = data_len - n;
+		// m is the bytes to scan in the current chunk
+		crc = f->scan[i].crc_so_far;
+		for (j = 0; j < m; ++j)
+			crc = crc32_update(crc, data[n + j]);
+		f->scan[i].bytes_left -= m;
+		f->scan[i].crc_so_far = crc;
+		if (f->scan[i].bytes_left == 0) {
+			// does it match?
+			if (f->scan[i].crc_so_far == f->scan[i].goal_crc) {
+				// Houston, we have page
+				data_len = n + m; // consumption amount is wherever that scan ended
+				f->page_crc_tests = -1; // drop out of page scan mode
+				f->previous_length = 0; // decode-but-don't-output one frame
+				f->next_seg = -1;       // start a new page
+				f->current_loc = f->scan[i].sample_loc; // set the current sample location
+														// to the amount we'd have decoded had we decoded this page
+				f->current_loc_valid = f->current_loc != ~0U;
+				return data_len;
+			}
+			// delete entry
+			f->scan[i] = f->scan[--f->page_crc_tests];
+		} else {
+			++i;
+		}
+	}
+
+	return data_len;
+}
+
+// return value: number of bytes we used
+int stb_vorbis_decode_frame_pushdata(
+	stb_vorbis *f,                   // the file we're decoding
+	const uint8 *data, int data_len, // the memory available for decoding
+	int *channels,                   // place to write number of float * buffers
+	float ***output,                 // place to write float ** array of float * buffers
+	int *samples                     // place to write number of output samples
+) {
+	int i;
+	int len, right, left;
+
+	if (!IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing);
+
+	if (f->page_crc_tests >= 0) {
+		*samples = 0;
+		return vorbis_search_for_page_pushdata(f, (uint8 *) data, data_len);
+	}
+
+	f->stream = (uint8 *) data;
+	f->stream_end = (uint8 *) data + data_len;
+	f->error = VORBIS__no_error;
+
+	// check that we have the entire packet in memory
+	if (!is_whole_packet_present(f, FALSE)) {
+		*samples = 0;
+		return 0;
+	}
+
+	if (!vorbis_decode_packet(f, &len, &left, &right)) {
+		// save the actual error we encountered
+		enum STBVorbisError error = f->error;
+		if (error == VORBIS_bad_packet_type) {
+			// flush and resynch
+			f->error = VORBIS__no_error;
+			while (get8_packet(f) != EOP)
+				if (f->eof) break;
+			*samples = 0;
+			return (int) (f->stream - data);
+		}
+		if (error == VORBIS_continued_packet_flag_invalid) {
+			if (f->previous_length == 0) {
+				// we may be resynching, in which case it's ok to hit one
+				// of these; just discard the packet
+				f->error = VORBIS__no_error;
+				while (get8_packet(f) != EOP)
+					if (f->eof) break;
+				*samples = 0;
+				return (int) (f->stream - data);
+			}
+		}
+		// if we get an error while parsing, what to do?
+		// well, it DEFINITELY won't work to continue from where we are!
+		stb_vorbis_flush_pushdata(f);
+		// restore the error that actually made us bail
+		f->error = error;
+		*samples = 0;
+		return 1;
+	}
+
+	// success!
+	len = vorbis_finish_frame(f, len, left, right);
+	for (i = 0; i < f->channels; ++i)
+		f->outputs[i] = f->channel_buffers[i] + left;
+
+	if (channels) *channels = f->channels;
+	*samples = len;
+	*output = f->outputs;
+	return (int) (f->stream - data);
+}
+
+stb_vorbis *stb_vorbis_open_pushdata(
+	const unsigned char *data, int data_len, // the memory available for decoding
+	int *data_used,              // only defined if result is not NULL
+	int *error, const stb_vorbis_alloc *alloc) {
+	stb_vorbis *f, p;
+	vorbis_init(&p, alloc);
+	p.stream = (uint8 *) data;
+	p.stream_end = (uint8 *) data + data_len;
+	p.push_mode = TRUE;
+	if (!start_decoder(&p)) {
+		if (p.eof)
+			*error = VORBIS_need_more_data;
+		else
+			*error = p.error;
+		return NULL;
+	}
+	f = vorbis_alloc(&p);
+	if (f) {
+		*f = p;
+		*data_used = (int) (f->stream - data);
+		*error = 0;
+		return f;
+	} else {
+		vorbis_deinit(&p);
+		return NULL;
+	}
+}
+#endif // STB_VORBIS_NO_PUSHDATA_API
+
+unsigned int stb_vorbis_get_file_offset(stb_vorbis *f) {
+#ifndef STB_VORBIS_NO_PUSHDATA_API
+	if (f->push_mode) return 0;
+#endif
+	if (USE_MEMORY(f)) return (unsigned int) (f->stream - f->stream_start);
+#ifndef STB_VORBIS_NO_STDIO
+	return (unsigned int) (ftell(f->f) - f->f_start);
+#endif
+}
+
+#ifndef STB_VORBIS_NO_PULLDATA_API
+//
+// DATA-PULLING API
+//
+
+static uint32 vorbis_find_page(stb_vorbis *f, uint32 *end, uint32 *last) {
+	for (;;) {
+		int n;
+		if (f->eof) return 0;
+		n = get8(f);
+		if (n == 0x4f) { // page header candidate
+			unsigned int retry_loc = stb_vorbis_get_file_offset(f);
+			int i;
+			// check if we're off the end of a file_section stream
+			if (retry_loc - 25 > f->stream_len)
+				return 0;
+			// check the rest of the header
+			for (i = 1; i < 4; ++i)
+				if (get8(f) != ogg_page_header[i])
+					break;
+			if (f->eof) return 0;
+			if (i == 4) {
+				uint8 header[27];
+				uint32 i, crc, goal, len;
+				for (i = 0; i < 4; ++i)
+					header[i] = ogg_page_header[i];
+				for (; i < 27; ++i)
+					header[i] = get8(f);
+				if (f->eof) return 0;
+				if (header[4] != 0) goto invalid;
+				goal = header[22] + (header[23] << 8) + (header[24] << 16) + (header[25] << 24);
+				for (i = 22; i < 26; ++i)
+					header[i] = 0;
+				crc = 0;
+				for (i = 0; i < 27; ++i)
+					crc = crc32_update(crc, header[i]);
+				len = 0;
+				for (i = 0; i < header[26]; ++i) {
+					int s = get8(f);
+					crc = crc32_update(crc, s);
+					len += s;
+				}
+				if (len && f->eof) return 0;
+				for (i = 0; i < len; ++i)
+					crc = crc32_update(crc, get8(f));
+				// finished parsing probable page
+				if (crc == goal) {
+					// we could now check that it's either got the last
+					// page flag set, OR it's followed by the capture
+					// pattern, but I guess TECHNICALLY you could have
+					// a file with garbage between each ogg page and recover
+					// from it automatically? So even though that paranoia
+					// might decrease the chance of an invalid decode by
+					// another 2^32, not worth it since it would hose those
+					// invalid-but-useful files?
+					if (end)
+						*end = stb_vorbis_get_file_offset(f);
+					if (last) {
+						if (header[5] & 0x04)
+							*last = 1;
+						else
+							*last = 0;
+					}
+					set_file_offset(f, retry_loc - 1);
+					return 1;
+				}
+			}
+invalid:
+			// not a valid page, so rewind and look for next one
+			set_file_offset(f, retry_loc);
+		}
+	}
+}
+
+
+#define SAMPLE_unknown  0xffffffff
+
+// seeking is implemented with a binary search, which narrows down the range to
+// 64K, before using a linear search (because finding the synchronization
+// pattern can be expensive, and the chance we'd find the end page again is
+// relatively high for small ranges)
+//
+// two initial interpolation-style probes are used at the start of the search
+// to try to bound either side of the binary search sensibly, while still
+// working in O(log n) time if they fail.
+
+static int get_seek_page_info(stb_vorbis *f, ProbedPage *z) {
+	uint8 header[27], lacing[255];
+	int i, len;
+
+	// record where the page starts
+	z->page_start = stb_vorbis_get_file_offset(f);
+
+	// parse the header
+	getn(f, header, 27);
+	if (header[0] != 'O' || header[1] != 'g' || header[2] != 'g' || header[3] != 'S')
+		return 0;
+	getn(f, lacing, header[26]);
+
+	// determine the length of the payload
+	len = 0;
+	for (i = 0; i < header[26]; ++i)
+		len += lacing[i];
+
+	// this implies where the page ends
+	z->page_end = z->page_start + 27 + header[26] + len;
+
+	// read the last-decoded sample out of the data
+	z->last_decoded_sample = header[6] + (header[7] << 8) + (header[8] << 16) + (header[9] << 24);
+
+	// restore file state to where we were
+	set_file_offset(f, z->page_start);
+	return 1;
+}
+
+// rarely used function to seek back to the preceeding page while finding the
+// start of a packet
+static int go_to_page_before(stb_vorbis *f, unsigned int limit_offset) {
+	unsigned int previous_safe, end;
+
+	// now we want to seek back 64K from the limit
+	if (limit_offset >= 65536 && limit_offset - 65536 >= f->first_audio_page_offset)
+		previous_safe = limit_offset - 65536;
+	else
+		previous_safe = f->first_audio_page_offset;
+
+	set_file_offset(f, previous_safe);
+
+	while (vorbis_find_page(f, &end, NULL)) {
+		if (end >= limit_offset && stb_vorbis_get_file_offset(f) < limit_offset)
+			return 1;
+		set_file_offset(f, end);
+	}
+
+	return 0;
+}
+
+// implements the search logic for finding a page and starting decoding. if
+// the function succeeds, current_loc_valid will be true and current_loc will
+// be less than or equal to the provided sample number (the closer the
+// better).
+static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number) {
+	ProbedPage left, right, mid;
+	int i, start_seg_with_known_loc, end_pos, page_start;
+	uint32 delta, stream_length, padding;
+	double offset, bytes_per_sample;
+	int probe = 0;
+
+	// find the last page and validate the target sample
+	stream_length = stb_vorbis_stream_length_in_samples(f);
+	if (stream_length == 0)            return error(f, VORBIS_seek_without_length);
+	if (sample_number > stream_length) return error(f, VORBIS_seek_invalid);
+
+	// this is the maximum difference between the window-center (which is the
+	// actual granule position value), and the right-start (which the spec
+	// indicates should be the granule position (give or take one)).
+	padding = ((f->blocksize_1 - f->blocksize_0) >> 2);
+	if (sample_number < padding)
+		sample_number = 0;
+	else
+		sample_number -= padding;
+
+	left = f->p_first;
+	while (left.last_decoded_sample == ~0U) {
+		// (untested) the first page does not have a 'last_decoded_sample'
+		set_file_offset(f, left.page_end);
+		if (!get_seek_page_info(f, &left)) goto error;
+	}
+
+	right = f->p_last;
+	assert(right.last_decoded_sample != ~0U);
+
+	// starting from the start is handled differently
+	if (sample_number <= left.last_decoded_sample) {
+		stb_vorbis_seek_start(f);
+		return 1;
+	}
+
+	while (left.page_end != right.page_start) {
+		assert(left.page_end < right.page_start);
+		// search range in bytes
+		delta = right.page_start - left.page_end;
+		if (delta <= 65536) {
+			// there's only 64K left to search - handle it linearly
+			set_file_offset(f, left.page_end);
+		} else {
+			if (probe < 2) {
+				if (probe == 0) {
+					// first probe (interpolate)
+					double data_bytes = right.page_end - left.page_start;
+					bytes_per_sample = data_bytes / right.last_decoded_sample;
+					offset = left.page_start + bytes_per_sample * (sample_number - left.last_decoded_sample);
+				} else {
+					// second probe (try to bound the other side)
+					double error = ((double) sample_number - mid.last_decoded_sample) * bytes_per_sample;
+					if (error >= 0 && error <  8000) error = 8000;
+					if (error <  0 && error > -8000) error = -8000;
+					offset += error * 2;
+				}
+
+				// ensure the offset is valid
+				if (offset < left.page_end)
+					offset = left.page_end;
+				if (offset > right.page_start - 65536)
+					offset = right.page_start - 65536;
+
+				set_file_offset(f, (unsigned int) offset);
+			} else {
+				// binary search for large ranges (offset by 32K to ensure
+				// we don't hit the right page)
+				set_file_offset(f, left.page_end + (delta / 2) - 32768);
+			}
+
+			if (!vorbis_find_page(f, NULL, NULL)) goto error;
+		}
+
+		for (;;) {
+			if (!get_seek_page_info(f, &mid)) goto error;
+			if (mid.last_decoded_sample != ~0U) break;
+			// (untested) no frames end on this page
+			set_file_offset(f, mid.page_end);
+			assert(mid.page_start < right.page_start);
+		}
+
+		// if we've just found the last page again then we're in a tricky file,
+		// and we're close enough.
+		if (mid.page_start == right.page_start)
+			break;
+
+		if (sample_number < mid.last_decoded_sample)
+			right = mid;
+		else
+			left = mid;
+
+		++probe;
+	}
+
+	// seek back to start of the last packet
+	page_start = left.page_start;
+	set_file_offset(f, page_start);
+	if (!start_page(f)) return error(f, VORBIS_seek_failed);
+	end_pos = f->end_seg_with_known_loc;
+	assert(end_pos >= 0);
+
+	for (;;) {
+		for (i = end_pos; i > 0; --i)
+			if (f->segments[i - 1] != 255)
+				break;
+
+		start_seg_with_known_loc = i;
+
+		if (start_seg_with_known_loc > 0 || !(f->page_flag & PAGEFLAG_continued_packet))
+			break;
+
+		// (untested) the final packet begins on an earlier page
+		if (!go_to_page_before(f, page_start))
+			goto error;
+
+		page_start = stb_vorbis_get_file_offset(f);
+		if (!start_page(f)) goto error;
+		end_pos = f->segment_count - 1;
+	}
+
+	// prepare to start decoding
+	f->current_loc_valid = FALSE;
+	f->last_seg = FALSE;
+	f->valid_bits = 0;
+	f->packet_bytes = 0;
+	f->bytes_in_seg = 0;
+	f->previous_length = 0;
+	f->next_seg = start_seg_with_known_loc;
+
+	for (i = 0; i < start_seg_with_known_loc; i++)
+		skip(f, f->segments[i]);
+
+	// start decoding (optimizable - this frame is generally discarded)
+	vorbis_pump_first_frame(f);
+	return 1;
+
+error:
+	// try to restore the file to a valid state
+	stb_vorbis_seek_start(f);
+	return error(f, VORBIS_seek_failed);
+}
+
+// the same as vorbis_decode_initial, but without advancing
+static int peek_decode_initial(vorb *f, int *p_left_start, int *p_left_end, int *p_right_start, int *p_right_end, int *mode) {
+	int bits_read, bytes_read;
+
+	if (!vorbis_decode_initial(f, p_left_start, p_left_end, p_right_start, p_right_end, mode))
+		return 0;
+
+	// either 1 or 2 bytes were read, figure out which so we can rewind
+	bits_read = 1 + ilog(f->mode_count - 1);
+	if (f->mode_config[*mode].blockflag)
+		bits_read += 2;
+	bytes_read = (bits_read + 7) / 8;
+
+	f->bytes_in_seg += bytes_read;
+	f->packet_bytes -= bytes_read;
+	skip(f, -bytes_read);
+	if (f->next_seg == -1)
+		f->next_seg = f->segment_count - 1;
+	else
+		f->next_seg--;
+	f->valid_bits = 0;
+
+	return 1;
+}
+
+int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number) {
+	uint32 max_frame_samples;
+
+	if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing);
+
+	// fast page-level search
+	if (!seek_to_sample_coarse(f, sample_number))
+		return 0;
+
+	assert(f->current_loc_valid);
+	assert(f->current_loc <= sample_number);
+
+	// linear search for the relevant packet
+	max_frame_samples = (f->blocksize_1 * 3 - f->blocksize_0) >> 2;
+	while (f->current_loc < sample_number) {
+		int left_start, left_end, right_start, right_end, mode, frame_samples;
+		if (!peek_decode_initial(f, &left_start, &left_end, &right_start, &right_end, &mode))
+			return error(f, VORBIS_seek_failed);
+		// calculate the number of samples returned by the next frame
+		frame_samples = right_start - left_start;
+		if (f->current_loc + frame_samples > sample_number) {
+			return 1; // the next frame will contain the sample
+		} else if (f->current_loc + frame_samples + max_frame_samples > sample_number) {
+			// there's a chance the frame after this could contain the sample
+			vorbis_pump_first_frame(f);
+		} else {
+			// this frame is too early to be relevant
+			f->current_loc += frame_samples;
+			f->previous_length = 0;
+			maybe_start_packet(f);
+			flush_packet(f);
+		}
+	}
+	// the next frame will start with the sample
+	assert(f->current_loc == sample_number);
+	return 1;
+}
+
+int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number) {
+	if (!stb_vorbis_seek_frame(f, sample_number))
+		return 0;
+
+	if (sample_number != f->current_loc) {
+		int n;
+		uint32 frame_start = f->current_loc;
+		stb_vorbis_get_frame_float(f, &n, NULL);
+		assert(sample_number > frame_start);
+		assert(f->channel_buffer_start + (int) (sample_number - frame_start) <= f->channel_buffer_end);
+		f->channel_buffer_start += (sample_number - frame_start);
+	}
+
+	return 1;
+}
+
+void stb_vorbis_seek_start(stb_vorbis *f) {
+	if (IS_PUSH_MODE(f)) { error(f, VORBIS_invalid_api_mixing); return; }
+	set_file_offset(f, f->first_audio_page_offset);
+	f->previous_length = 0;
+	f->first_decode = TRUE;
+	f->next_seg = -1;
+	vorbis_pump_first_frame(f);
+}
+
+unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f) {
+	unsigned int restore_offset, previous_safe;
+	unsigned int end, last_page_loc;
+
+	if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing);
+	if (!f->total_samples) {
+		unsigned int last;
+		uint32 lo, hi;
+		char header[6];
+
+		// first, store the current decode position so we can restore it
+		restore_offset = stb_vorbis_get_file_offset(f);
+
+		// now we want to seek back 64K from the end (the last page must
+		// be at most a little less than 64K, but let's allow a little slop)
+		if (f->stream_len >= 65536 && f->stream_len - 65536 >= f->first_audio_page_offset)
+			previous_safe = f->stream_len - 65536;
+		else
+			previous_safe = f->first_audio_page_offset;
+
+		set_file_offset(f, previous_safe);
+		// previous_safe is now our candidate 'earliest known place that seeking
+		// to will lead to the final page'
+
+		if (!vorbis_find_page(f, &end, &last)) {
+			// if we can't find a page, we're hosed!
+			f->error = VORBIS_cant_find_last_page;
+			f->total_samples = 0xffffffff;
+			goto done;
+		}
+
+		// check if there are more pages
+		last_page_loc = stb_vorbis_get_file_offset(f);
+
+		// stop when the last_page flag is set, not when we reach eof;
+		// this allows us to stop short of a 'file_section' end without
+		// explicitly checking the length of the section
+		while (!last) {
+			set_file_offset(f, end);
+			if (!vorbis_find_page(f, &end, &last)) {
+				// the last page we found didn't have the 'last page' flag
+				// set. whoops!
+				break;
+			}
+			previous_safe = last_page_loc + 1;
+			last_page_loc = stb_vorbis_get_file_offset(f);
+		}
+
+		set_file_offset(f, last_page_loc);
+
+		// parse the header
+		getn(f, (unsigned char *) header, 6);
+		// extract the absolute granule position
+		lo = get32(f);
+		hi = get32(f);
+		if (lo == 0xffffffff && hi == 0xffffffff) {
+			f->error = VORBIS_cant_find_last_page;
+			f->total_samples = SAMPLE_unknown;
+			goto done;
+		}
+		if (hi)
+			lo = 0xfffffffe; // saturate
+		f->total_samples = lo;
+
+		f->p_last.page_start = last_page_loc;
+		f->p_last.page_end = end;
+		f->p_last.last_decoded_sample = lo;
+
+done:
+		set_file_offset(f, restore_offset);
+	}
+	return f->total_samples == SAMPLE_unknown ? 0 : f->total_samples;
+}
+
+float stb_vorbis_stream_length_in_seconds(stb_vorbis *f) {
+	return stb_vorbis_stream_length_in_samples(f) / (float) f->sample_rate;
+}
+
+
+
+int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output) {
+	int len, right, left, i;
+	if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing);
+
+	if (!vorbis_decode_packet(f, &len, &left, &right)) {
+		f->channel_buffer_start = f->channel_buffer_end = 0;
+		return 0;
+	}
+
+	len = vorbis_finish_frame(f, len, left, right);
+	for (i = 0; i < f->channels; ++i)
+		f->outputs[i] = f->channel_buffers[i] + left;
+
+	f->channel_buffer_start = left;
+	f->channel_buffer_end = left + len;
+
+	if (channels) *channels = f->channels;
+	if (output)   *output = f->outputs;
+	return len;
+}
+
+#ifndef STB_VORBIS_NO_STDIO
+
+stb_vorbis * stb_vorbis_open_file_section(FILE *file, int close_on_free, int *error, const stb_vorbis_alloc *alloc, unsigned int length) {
+	stb_vorbis *f, p;
+	vorbis_init(&p, alloc);
+	p.f = file;
+	p.f_start = (uint32) ftell(file);
+	p.stream_len = length;
+	p.close_on_free = close_on_free;
+	if (start_decoder(&p)) {
+		f = vorbis_alloc(&p);
+		if (f) {
+			*f = p;
+			vorbis_pump_first_frame(f);
+			return f;
+		}
+	}
+	if (error) *error = p.error;
+	vorbis_deinit(&p);
+	return NULL;
+}
+
+stb_vorbis * stb_vorbis_open_file(FILE *file, int close_on_free, int *error, const stb_vorbis_alloc *alloc) {
+	unsigned int len, start;
+	start = (unsigned int) ftell(file);
+	fseek(file, 0, SEEK_END);
+	len = (unsigned int) (ftell(file) - start);
+	fseek(file, start, SEEK_SET);
+	return stb_vorbis_open_file_section(file, close_on_free, error, alloc, len);
+}
+
+stb_vorbis * stb_vorbis_open_filename(const char *filename, int *error, const stb_vorbis_alloc *alloc) {
+	FILE *f = fopen(filename, "rb");
+	if (f)
+		return stb_vorbis_open_file(f, TRUE, error, alloc);
+	if (error) *error = VORBIS_file_open_failure;
+	return NULL;
+}
+#endif // STB_VORBIS_NO_STDIO
+
+stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len, int *error, const stb_vorbis_alloc *alloc) {
+	stb_vorbis *f, p;
+	if (data == NULL) return NULL;
+	vorbis_init(&p, alloc);
+	p.stream = (uint8 *) data;
+	p.stream_end = (uint8 *) data + len;
+	p.stream_start = (uint8 *) p.stream;
+	p.stream_len = len;
+	p.push_mode = FALSE;
+	if (start_decoder(&p)) {
+		f = vorbis_alloc(&p);
+		if (f) {
+			*f = p;
+			vorbis_pump_first_frame(f);
+			return f;
+		}
+	}
+	if (error) *error = p.error;
+	vorbis_deinit(&p);
+	return NULL;
+}
+
+#ifndef STB_VORBIS_NO_INTEGER_CONVERSION
+#define PLAYBACK_MONO     1
+#define PLAYBACK_LEFT     2
+#define PLAYBACK_RIGHT    4
+
+#define L  (PLAYBACK_LEFT  | PLAYBACK_MONO)
+#define C  (PLAYBACK_LEFT  | PLAYBACK_RIGHT | PLAYBACK_MONO)
+#define R  (PLAYBACK_RIGHT | PLAYBACK_MONO)
+
+static int8 channel_position[7][6] =
+{
+	{0},
+	{C},
+	{L, R},
+	{L, C, R},
+	{L, R, L, R},
+	{L, C, R, L, R},
+	{L, C, R, L, R, C},
+};
+
+
+#ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT
+typedef union {
+	float f;
+	int i;
+} float_conv;
+typedef char stb_vorbis_float_size_test[sizeof(float) == 4 && sizeof(int) == 4];
+#define FASTDEF(x) float_conv x
+// add (1<<23) to convert to int, then divide by 2^SHIFT, then add 0.5/2^SHIFT to round
+#define MAGIC(SHIFT) (1.5f * (1 << (23-SHIFT)) + 0.5f/(1 << SHIFT))
+#define ADDEND(SHIFT) (((150-SHIFT) << 23) + (1 << 22))
+#define FAST_SCALED_FLOAT_TO_INT(temp,x,s) (temp.f = (x) + MAGIC(s), temp.i - ADDEND(s))
+#define check_endianness()  
+#else
+#define FAST_SCALED_FLOAT_TO_INT(temp,x,s) ((int) ((x) * (1 << (s))))
+#define check_endianness()
+#define FASTDEF(x)
+#endif
+
+static void copy_samples(short *dest, float *src, int len) {
+	int i;
+	check_endianness();
+	for (i = 0; i < len; ++i) {
+		FASTDEF(temp);
+		int v = FAST_SCALED_FLOAT_TO_INT(temp, src[i], 15);
+		if ((unsigned int) (v + 32768) > 65535)
+			v = v < 0 ? -32768 : 32767;
+		dest[i] = v;
+	}
+}
+
+static void compute_samples(int mask, short *output, int num_c, float **data, int d_offset, int len) {
+#define BUFFER_SIZE  32
+	float buffer[BUFFER_SIZE];
+	int i, j, o, n = BUFFER_SIZE;
+	check_endianness();
+	for (o = 0; o < len; o += BUFFER_SIZE) {
+		memset(buffer, 0, sizeof(buffer));
+		if (o + n > len) n = len - o;
+		for (j = 0; j < num_c; ++j) {
+			if (channel_position[num_c][j] & mask) {
+				for (i = 0; i < n; ++i)
+					buffer[i] += data[j][d_offset + o + i];
+			}
+		}
+		for (i = 0; i < n; ++i) {
+			FASTDEF(temp);
+			int v = FAST_SCALED_FLOAT_TO_INT(temp, buffer[i], 15);
+			if ((unsigned int) (v + 32768) > 65535)
+				v = v < 0 ? -32768 : 32767;
+			output[o + i] = v;
+		}
+	}
+}
+
+static void compute_stereo_samples(short *output, int num_c, float **data, int d_offset, int len) {
+#define BUFFER_SIZE  32
+	float buffer[BUFFER_SIZE];
+	int i, j, o, n = BUFFER_SIZE >> 1;
+	// o is the offset in the source data
+	check_endianness();
+	for (o = 0; o < len; o += BUFFER_SIZE >> 1) {
+		// o2 is the offset in the output data
+		int o2 = o << 1;
+		memset(buffer, 0, sizeof(buffer));
+		if (o + n > len) n = len - o;
+		for (j = 0; j < num_c; ++j) {
+			int m = channel_position[num_c][j] & (PLAYBACK_LEFT | PLAYBACK_RIGHT);
+			if (m == (PLAYBACK_LEFT | PLAYBACK_RIGHT)) {
+				for (i = 0; i < n; ++i) {
+					buffer[i * 2 + 0] += data[j][d_offset + o + i];
+					buffer[i * 2 + 1] += data[j][d_offset + o + i];
+				}
+			} else if (m == PLAYBACK_LEFT) {
+				for (i = 0; i < n; ++i) {
+					buffer[i * 2 + 0] += data[j][d_offset + o + i];
+				}
+			} else if (m == PLAYBACK_RIGHT) {
+				for (i = 0; i < n; ++i) {
+					buffer[i * 2 + 1] += data[j][d_offset + o + i];
+				}
+			}
+		}
+		for (i = 0; i < (n << 1); ++i) {
+			FASTDEF(temp);
+			int v = FAST_SCALED_FLOAT_TO_INT(temp, buffer[i], 15);
+			if ((unsigned int) (v + 32768) > 65535)
+				v = v < 0 ? -32768 : 32767;
+			output[o2 + i] = v;
+		}
+	}
+}
+
+static void convert_samples_short(int buf_c, short **buffer, int b_offset, int data_c, float **data, int d_offset, int samples) {
+	int i;
+	if (buf_c != data_c && buf_c <= 2 && data_c <= 6) {
+		static int channel_selector[3][2] = {{0},{PLAYBACK_MONO},{PLAYBACK_LEFT, PLAYBACK_RIGHT}};
+		for (i = 0; i < buf_c; ++i)
+			compute_samples(channel_selector[buf_c][i], buffer[i] + b_offset, data_c, data, d_offset, samples);
+	} else {
+		int limit = buf_c < data_c ? buf_c : data_c;
+		for (i = 0; i < limit; ++i)
+			copy_samples(buffer[i] + b_offset, data[i] + d_offset, samples);
+		for (; i < buf_c; ++i)
+			memset(buffer[i] + b_offset, 0, sizeof(short) * samples);
+	}
+}
+
+int stb_vorbis_get_frame_short(stb_vorbis *f, int num_c, short **buffer, int num_samples) {
+	float **output;
+	int len = stb_vorbis_get_frame_float(f, NULL, &output);
+	if (len > num_samples) len = num_samples;
+	if (len)
+		convert_samples_short(num_c, buffer, 0, f->channels, output, 0, len);
+	return len;
+}
+
+static void convert_channels_short_interleaved(int buf_c, short *buffer, int data_c, float **data, int d_offset, int len) {
+	int i;
+	check_endianness();
+	if (buf_c != data_c && buf_c <= 2 && data_c <= 6) {
+		assert(buf_c == 2);
+		for (i = 0; i < buf_c; ++i)
+			compute_stereo_samples(buffer, data_c, data, d_offset, len);
+	} else {
+		int limit = buf_c < data_c ? buf_c : data_c;
+		int j;
+		for (j = 0; j < len; ++j) {
+			for (i = 0; i < limit; ++i) {
+				FASTDEF(temp);
+				float f = data[i][d_offset + j];
+				int v = FAST_SCALED_FLOAT_TO_INT(temp, f, 15);//data[i][d_offset+j],15);
+				if ((unsigned int) (v + 32768) > 65535)
+					v = v < 0 ? -32768 : 32767;
+				*buffer++ = v;
+			}
+			for (; i < buf_c; ++i)
+				*buffer++ = 0;
+		}
+	}
+}
+
+int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts) {
+	float **output;
+	int len;
+	if (num_c == 1) return stb_vorbis_get_frame_short(f, num_c, &buffer, num_shorts);
+	len = stb_vorbis_get_frame_float(f, NULL, &output);
+	if (len) {
+		if (len*num_c > num_shorts) len = num_shorts / num_c;
+		convert_channels_short_interleaved(num_c, buffer, f->channels, output, 0, len);
+	}
+	return len;
+}
+
+int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts) {
+	float **outputs;
+	int len = num_shorts / channels;
+	int n = 0;
+	int z = f->channels;
+	if (z > channels) z = channels;
+	while (n < len) {
+		int k = f->channel_buffer_end - f->channel_buffer_start;
+		if (n + k >= len) k = len - n;
+		if (k)
+			convert_channels_short_interleaved(channels, buffer, f->channels, f->channel_buffers, f->channel_buffer_start, k);
+		buffer += k*channels;
+		n += k;
+		f->channel_buffer_start += k;
+		if (n == len) break;
+		if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break;
+	}
+	return n;
+}
+
+int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int len) {
+	float **outputs;
+	int n = 0;
+	int z = f->channels;
+	if (z > channels) z = channels;
+	while (n < len) {
+		int k = f->channel_buffer_end - f->channel_buffer_start;
+		if (n + k >= len) k = len - n;
+		if (k)
+			convert_samples_short(channels, buffer, n, f->channels, f->channel_buffers, f->channel_buffer_start, k);
+		n += k;
+		f->channel_buffer_start += k;
+		if (n == len) break;
+		if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break;
+	}
+	return n;
+}
+
+#ifndef STB_VORBIS_NO_STDIO
+int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output) {
+	int data_len, offset, total, limit, error;
+	short *data;
+	stb_vorbis *v = stb_vorbis_open_filename(filename, &error, NULL);
+	if (v == NULL) return -1;
+	limit = v->channels * 4096;
+	*channels = v->channels;
+	if (sample_rate)
+		*sample_rate = v->sample_rate;
+	offset = data_len = 0;
+	total = limit;
+	data = (short *) malloc(total * sizeof(*data));
+	if (data == NULL) {
+		stb_vorbis_close(v);
+		return -2;
+	}
+	for (;;) {
+		int n = stb_vorbis_get_frame_short_interleaved(v, v->channels, data + offset, total - offset);
+		if (n == 0) break;
+		data_len += n;
+		offset += n * v->channels;
+		if (offset + limit > total) {
+			short *data2;
+			total *= 2;
+			data2 = (short *) realloc(data, total * sizeof(*data));
+			if (data2 == NULL) {
+				free(data);
+				stb_vorbis_close(v);
+				return -2;
+			}
+			data = data2;
+		}
+	}
+	*output = data;
+	stb_vorbis_close(v);
+	return data_len;
+}
+#endif // NO_STDIO
+
+int stb_vorbis_decode_memory(const uint8 *mem, int len, int *channels, int *sample_rate, short **output) {
+	int data_len, offset, total, limit, error;
+	short *data;
+	stb_vorbis *v = stb_vorbis_open_memory(mem, len, &error, NULL);
+	if (v == NULL) return -1;
+	limit = v->channels * 4096;
+	*channels = v->channels;
+	if (sample_rate)
+		*sample_rate = v->sample_rate;
+	offset = data_len = 0;
+	total = limit;
+	data = (short *) malloc(total * sizeof(*data));
+	if (data == NULL) {
+		stb_vorbis_close(v);
+		return -2;
+	}
+	for (;;) {
+		int n = stb_vorbis_get_frame_short_interleaved(v, v->channels, data + offset, total - offset);
+		if (n == 0) break;
+		data_len += n;
+		offset += n * v->channels;
+		if (offset + limit > total) {
+			short *data2;
+			total *= 2;
+			data2 = (short *) realloc(data, total * sizeof(*data));
+			if (data2 == NULL) {
+				free(data);
+				stb_vorbis_close(v);
+				return -2;
+			}
+			data = data2;
+		}
+	}
+	*output = data;
+	stb_vorbis_close(v);
+	return data_len;
+}
+#endif // STB_VORBIS_NO_INTEGER_CONVERSION
+
+int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats) {
+	float **outputs;
+	int len = num_floats / channels;
+	int n = 0;
+	int z = f->channels;
+	if (z > channels) z = channels;
+	while (n < len) {
+		int i, j;
+		int k = f->channel_buffer_end - f->channel_buffer_start;
+		if (n + k >= len) k = len - n;
+		for (j = 0; j < k; ++j) {
+			for (i = 0; i < z; ++i)
+				*buffer++ = f->channel_buffers[i][f->channel_buffer_start + j];
+			for (; i < channels; ++i)
+				*buffer++ = 0;
+		}
+		n += k;
+		f->channel_buffer_start += k;
+		if (n == len)
+			break;
+		if (!stb_vorbis_get_frame_float(f, NULL, &outputs))
+			break;
+	}
+	return n;
+}
+
+int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples) {
+	float **outputs;
+	int n = 0;
+	int z = f->channels;
+	if (z > channels) z = channels;
+	while (n < num_samples) {
+		int i;
+		int k = f->channel_buffer_end - f->channel_buffer_start;
+		if (n + k >= num_samples) k = num_samples - n;
+		if (k) {
+			for (i = 0; i < z; ++i)
+				memcpy(buffer[i] + n, f->channel_buffers[i] + f->channel_buffer_start, sizeof(float)*k);
+			for (; i < channels; ++i)
+				memset(buffer[i] + n, 0, sizeof(float) * k);
+		}
+		n += k;
+		f->channel_buffer_start += k;
+		if (n == num_samples)
+			break;
+		if (!stb_vorbis_get_frame_float(f, NULL, &outputs))
+			break;
+	}
+	return n;
+}
+#endif // STB_VORBIS_NO_PULLDATA_API
+
+/* Version history
+1.09    - 2016/04/04 - back out 'avoid discarding last frame' fix from previous version
+1.08    - 2016/04/02 - fixed multiple warnings; fix setup memory leaks;
+avoid discarding last frame of audio data
+1.07    - 2015/01/16 - fixed some warnings, fix mingw, const-correct API
+some more crash fixes when out of memory or with corrupt files
+1.06    - 2015/08/31 - full, correct support for seeking API (Dougall Johnson)
+some crash fixes when out of memory or with corrupt files
+1.05    - 2015/04/19 - don't define __forceinline if it's redundant
+1.04    - 2014/08/27 - fix missing const-correct case in API
+1.03    - 2014/08/07 - Warning fixes
+1.02    - 2014/07/09 - Declare qsort compare function _cdecl on windows
+1.01    - 2014/06/18 - fix stb_vorbis_get_samples_float
+1.0     - 2014/05/26 - fix memory leaks; fix warnings; fix bugs in multichannel
+(API change) report sample rate for decode-full-file funcs
+0.99996 - bracket #include <malloc.h> for macintosh compilation by Laurent Gomila
+0.99995 - use union instead of pointer-cast for fast-float-to-int to avoid alias-optimization problem
+0.99994 - change fast-float-to-int to work in single-precision FPU mode, remove endian-dependence
+0.99993 - remove assert that fired on legal files with empty tables
+0.99992 - rewind-to-start
+0.99991 - bugfix to stb_vorbis_get_samples_short by Bernhard Wodo
+0.9999 - (should have been 0.99990) fix no-CRT support, compiling as C++
+0.9998 - add a full-decode function with a memory source
+0.9997 - fix a bug in the read-from-FILE case in 0.9996 addition
+0.9996 - query length of vorbis stream in samples/seconds
+0.9995 - bugfix to another optimization that only happened in certain files
+0.9994 - bugfix to one of the optimizations that caused significant (but inaudible?) errors
+0.9993 - performance improvements; runs in 99% to 104% of time of reference implementation
+0.9992 - performance improvement of IMDCT; now performs close to reference implementation
+0.9991 - performance improvement of IMDCT
+0.999 - (should have been 0.9990) performance improvement of IMDCT
+0.998 - no-CRT support from Casey Muratori
+0.997 - bugfixes for bugs found by Terje Mathisen
+0.996 - bugfix: fast-huffman decode initialized incorrectly for sparse codebooks; fixing gives 10% speedup - found by Terje Mathisen
+0.995 - bugfix: fix to 'effective' overrun detection - found by Terje Mathisen
+0.994 - bugfix: garbage decode on final VQ symbol of a non-multiple - found by Terje Mathisen
+0.993 - bugfix: pushdata API required 1 extra byte for empty page (failed to consume final page if empty) - found by Terje Mathisen
+0.992 - fixes for MinGW warning
+0.991 - turn fast-float-conversion on by default
+0.990 - fix push-mode seek recovery if you seek into the headers
+0.98b - fix to bad release of 0.98
+0.98 - fix push-mode seek recovery; robustify float-to-int and support non-fast mode
+0.97 - builds under c++ (typecasting, don't use 'class' keyword)
+0.96 - somehow MY 0.95 was right, but the web one was wrong, so here's my 0.95 rereleased as 0.96, fixes a typo in the clamping code
+0.95 - clamping code for 16-bit functions
+0.94 - not publically released
+0.93 - fixed all-zero-floor case (was decoding garbage)
+0.92 - fixed a memory leak
+0.91 - conditional compiles to omit parts of the API and the infrastructure to support them: STB_VORBIS_NO_PULLDATA_API, STB_VORBIS_NO_PUSHDATA_API, STB_VORBIS_NO_STDIO, STB_VORBIS_NO_INTEGER_CONVERSION
+0.90 - first public release
+*/
+
+#endif // STB_VORBIS_HEADER_ONLY

+ 0 - 243
include/vorbis/codec.h

@@ -1,243 +0,0 @@
-/********************************************************************
- *                                                                  *
- * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
- * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
- * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
- * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
- *                                                                  *
- * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
- * by the Xiph.Org Foundation http://www.xiph.org/                  *
-
- ********************************************************************
-
- function: libvorbis codec headers
- last mod: $Id: codec.h 17021 2010-03-24 09:29:41Z xiphmont $
-
- ********************************************************************/
-
-#ifndef _vorbis_codec_h_
-#define _vorbis_codec_h_
-
-#ifdef __cplusplus
-extern "C"
-{
-#endif /* __cplusplus */
-
-#include <ogg/ogg.h>
-
-typedef struct vorbis_info{
-  int version;
-  int channels;
-  long rate;
-
-  /* The below bitrate declarations are *hints*.
-     Combinations of the three values carry the following implications:
-
-     all three set to the same value:
-       implies a fixed rate bitstream
-     only nominal set:
-       implies a VBR stream that averages the nominal bitrate.  No hard
-       upper/lower limit
-     upper and or lower set:
-       implies a VBR bitstream that obeys the bitrate limits. nominal
-       may also be set to give a nominal rate.
-     none set:
-       the coder does not care to speculate.
-  */
-
-  long bitrate_upper;
-  long bitrate_nominal;
-  long bitrate_lower;
-  long bitrate_window;
-
-  void *codec_setup;
-} vorbis_info;
-
-/* vorbis_dsp_state buffers the current vorbis audio
-   analysis/synthesis state.  The DSP state belongs to a specific
-   logical bitstream ****************************************************/
-typedef struct vorbis_dsp_state{
-  int analysisp;
-  vorbis_info *vi;
-
-  float **pcm;
-  float **pcmret;
-  int      pcm_storage;
-  int      pcm_current;
-  int      pcm_returned;
-
-  int  preextrapolate;
-  int  eofflag;
-
-  long lW;
-  long W;
-  long nW;
-  long centerW;
-
-  ogg_int64_t granulepos;
-  ogg_int64_t sequence;
-
-  ogg_int64_t glue_bits;
-  ogg_int64_t time_bits;
-  ogg_int64_t floor_bits;
-  ogg_int64_t res_bits;
-
-  void       *backend_state;
-} vorbis_dsp_state;
-
-typedef struct vorbis_block{
-  /* necessary stream state for linking to the framing abstraction */
-  float  **pcm;       /* this is a pointer into local storage */
-  oggpack_buffer opb;
-
-  long  lW;
-  long  W;
-  long  nW;
-  int   pcmend;
-  int   mode;
-
-  int         eofflag;
-  ogg_int64_t granulepos;
-  ogg_int64_t sequence;
-  vorbis_dsp_state *vd; /* For read-only access of configuration */
-
-  /* local storage to avoid remallocing; it's up to the mapping to
-     structure it */
-  void               *localstore;
-  long                localtop;
-  long                localalloc;
-  long                totaluse;
-  struct alloc_chain *reap;
-
-  /* bitmetrics for the frame */
-  long glue_bits;
-  long time_bits;
-  long floor_bits;
-  long res_bits;
-
-  void *internal;
-
-} vorbis_block;
-
-/* vorbis_block is a single block of data to be processed as part of
-the analysis/synthesis stream; it belongs to a specific logical
-bitstream, but is independent from other vorbis_blocks belonging to
-that logical bitstream. *************************************************/
-
-struct alloc_chain{
-  void *ptr;
-  struct alloc_chain *next;
-};
-
-/* vorbis_info contains all the setup information specific to the
-   specific compression/decompression mode in progress (eg,
-   psychoacoustic settings, channel setup, options, codebook
-   etc). vorbis_info and substructures are in backends.h.
-*********************************************************************/
-
-/* the comments are not part of vorbis_info so that vorbis_info can be
-   static storage */
-typedef struct vorbis_comment{
-  /* unlimited user comment fields.  libvorbis writes 'libvorbis'
-     whatever vendor is set to in encode */
-  char **user_comments;
-  int   *comment_lengths;
-  int    comments;
-  char  *vendor;
-
-} vorbis_comment;
-
-
-/* libvorbis encodes in two abstraction layers; first we perform DSP
-   and produce a packet (see docs/analysis.txt).  The packet is then
-   coded into a framed OggSquish bitstream by the second layer (see
-   docs/framing.txt).  Decode is the reverse process; we sync/frame
-   the bitstream and extract individual packets, then decode the
-   packet back into PCM audio.
-
-   The extra framing/packetizing is used in streaming formats, such as
-   files.  Over the net (such as with UDP), the framing and
-   packetization aren't necessary as they're provided by the transport
-   and the streaming layer is not used */
-
-/* Vorbis PRIMITIVES: general ***************************************/
-
-extern void     vorbis_info_init(vorbis_info *vi);
-extern void     vorbis_info_clear(vorbis_info *vi);
-extern int      vorbis_info_blocksize(vorbis_info *vi,int zo);
-extern void     vorbis_comment_init(vorbis_comment *vc);
-extern void     vorbis_comment_add(vorbis_comment *vc, const char *comment);
-extern void     vorbis_comment_add_tag(vorbis_comment *vc,
-                                       const char *tag, const char *contents);
-extern char    *vorbis_comment_query(vorbis_comment *vc, const char *tag, int count);
-extern int      vorbis_comment_query_count(vorbis_comment *vc, const char *tag);
-extern void     vorbis_comment_clear(vorbis_comment *vc);
-
-extern int      vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb);
-extern int      vorbis_block_clear(vorbis_block *vb);
-extern void     vorbis_dsp_clear(vorbis_dsp_state *v);
-extern double   vorbis_granule_time(vorbis_dsp_state *v,
-                                    ogg_int64_t granulepos);
-
-extern const char *vorbis_version_string(void);
-
-/* Vorbis PRIMITIVES: analysis/DSP layer ****************************/
-
-extern int      vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi);
-extern int      vorbis_commentheader_out(vorbis_comment *vc, ogg_packet *op);
-extern int      vorbis_analysis_headerout(vorbis_dsp_state *v,
-                                          vorbis_comment *vc,
-                                          ogg_packet *op,
-                                          ogg_packet *op_comm,
-                                          ogg_packet *op_code);
-extern float  **vorbis_analysis_buffer(vorbis_dsp_state *v,int vals);
-extern int      vorbis_analysis_wrote(vorbis_dsp_state *v,int vals);
-extern int      vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb);
-extern int      vorbis_analysis(vorbis_block *vb,ogg_packet *op);
-
-extern int      vorbis_bitrate_addblock(vorbis_block *vb);
-extern int      vorbis_bitrate_flushpacket(vorbis_dsp_state *vd,
-                                           ogg_packet *op);
-
-/* Vorbis PRIMITIVES: synthesis layer *******************************/
-extern int      vorbis_synthesis_idheader(ogg_packet *op);
-extern int      vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc,
-                                          ogg_packet *op);
-
-extern int      vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi);
-extern int      vorbis_synthesis_restart(vorbis_dsp_state *v);
-extern int      vorbis_synthesis(vorbis_block *vb,ogg_packet *op);
-extern int      vorbis_synthesis_trackonly(vorbis_block *vb,ogg_packet *op);
-extern int      vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb);
-extern int      vorbis_synthesis_pcmout(vorbis_dsp_state *v,float ***pcm);
-extern int      vorbis_synthesis_lapout(vorbis_dsp_state *v,float ***pcm);
-extern int      vorbis_synthesis_read(vorbis_dsp_state *v,int samples);
-extern long     vorbis_packet_blocksize(vorbis_info *vi,ogg_packet *op);
-
-extern int      vorbis_synthesis_halfrate(vorbis_info *v,int flag);
-extern int      vorbis_synthesis_halfrate_p(vorbis_info *v);
-
-/* Vorbis ERRORS and return codes ***********************************/
-
-#define OV_FALSE      -1
-#define OV_EOF        -2
-#define OV_HOLE       -3
-
-#define OV_EREAD      -128
-#define OV_EFAULT     -129
-#define OV_EIMPL      -130
-#define OV_EINVAL     -131
-#define OV_ENOTVORBIS -132
-#define OV_EBADHEADER -133
-#define OV_EVERSION   -134
-#define OV_ENOTAUDIO  -135
-#define OV_EBADPACKET -136
-#define OV_EBADLINK   -137
-#define OV_ENOSEEK    -138
-
-#ifdef __cplusplus
-}
-#endif /* __cplusplus */
-
-#endif
-

+ 0 - 436
include/vorbis/vorbisenc.h

@@ -1,436 +0,0 @@
-/********************************************************************
- *                                                                  *
- * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
- * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
- * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
- * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
- *                                                                  *
- * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
- * by the Xiph.Org Foundation http://www.xiph.org/                  *
- *                                                                  *
- ********************************************************************
-
- function: vorbis encode-engine setup
- last mod: $Id: vorbisenc.h 17021 2010-03-24 09:29:41Z xiphmont $
-
- ********************************************************************/
-
-/** \file
- * Libvorbisenc is a convenient API for setting up an encoding
- * environment using libvorbis. Libvorbisenc encapsulates the
- * actions needed to set up the encoder properly.
- */
-
-#ifndef _OV_ENC_H_
-#define _OV_ENC_H_
-
-#ifdef __cplusplus
-extern "C"
-{
-#endif /* __cplusplus */
-
-#include "codec.h"
-
-/**
- * This is the primary function within libvorbisenc for setting up managed
- * bitrate modes.
- *
- * Before this function is called, the \ref vorbis_info
- * struct should be initialized by using vorbis_info_init() from the libvorbis
- * API.  After encoding, vorbis_info_clear() should be called.
- *
- * The max_bitrate, nominal_bitrate, and min_bitrate settings are used to set
- * constraints for the encoded file.  This function uses these settings to
- * select the appropriate encoding mode and set it up.
- *
- * \param vi               Pointer to an initialized \ref vorbis_info struct.
- * \param channels         The number of channels to be encoded.
- * \param rate             The sampling rate of the source audio.
- * \param max_bitrate      Desired maximum bitrate (limit). -1 indicates unset.
- * \param nominal_bitrate  Desired average, or central, bitrate. -1 indicates unset.
- * \param min_bitrate      Desired minimum bitrate. -1 indicates unset.
- *
- * \return Zero for success, and negative values for failure.
- *
- * \retval 0          Success.
- * \retval OV_EFAULT  Internal logic fault; indicates a bug or heap/stack corruption.
- * \retval OV_EINVAL  Invalid setup request, eg, out of range argument.
- * \retval OV_EIMPL   Unimplemented mode; unable to comply with bitrate request.
- */
-extern int vorbis_encode_init(vorbis_info *vi,
-                              long channels,
-                              long rate,
-
-                              long max_bitrate,
-                              long nominal_bitrate,
-                              long min_bitrate);
-
-/**
- * This function performs step-one of a three-step bitrate-managed encode
- * setup.  It functions similarly to the one-step setup performed by \ref
- * vorbis_encode_init but allows an application to make further encode setup
- * tweaks using \ref vorbis_encode_ctl before finally calling \ref
- * vorbis_encode_setup_init to complete the setup process.
- *
- * Before this function is called, the \ref vorbis_info struct should be
- * initialized by using vorbis_info_init() from the libvorbis API.  After
- * encoding, vorbis_info_clear() should be called.
- *
- * The max_bitrate, nominal_bitrate, and min_bitrate settings are used to set
- * constraints for the encoded file.  This function uses these settings to
- * select the appropriate encoding mode and set it up.
- *
- * \param vi                Pointer to an initialized vorbis_info struct.
- * \param channels          The number of channels to be encoded.
- * \param rate              The sampling rate of the source audio.
- * \param max_bitrate       Desired maximum bitrate (limit). -1 indicates unset.
- * \param nominal_bitrate   Desired average, or central, bitrate. -1 indicates unset.
- * \param min_bitrate       Desired minimum bitrate. -1 indicates unset.
- *
- * \return Zero for success, and negative for failure.
- *
- * \retval 0           Success
- * \retval OV_EFAULT   Internal logic fault; indicates a bug or heap/stack corruption.
- * \retval OV_EINVAL   Invalid setup request, eg, out of range argument.
- * \retval OV_EIMPL    Unimplemented mode; unable to comply with bitrate request.
- */
-extern int vorbis_encode_setup_managed(vorbis_info *vi,
-                                       long channels,
-                                       long rate,
-
-                                       long max_bitrate,
-                                       long nominal_bitrate,
-                                       long min_bitrate);
-
-/**
- * This function performs step-one of a three-step variable bitrate
- * (quality-based) encode setup.  It functions similarly to the one-step setup
- * performed by \ref vorbis_encode_init_vbr() but allows an application to
- * make further encode setup tweaks using \ref vorbis_encode_ctl() before
- * finally calling \ref vorbis_encode_setup_init to complete the setup
- * process.
- *
- * Before this function is called, the \ref vorbis_info struct should be
- * initialized by using \ref vorbis_info_init() from the libvorbis API.  After
- * encoding, vorbis_info_clear() should be called.
- *
- * \param vi        Pointer to an initialized vorbis_info struct.
- * \param channels  The number of channels to be encoded.
- * \param rate      The sampling rate of the source audio.
- * \param quality   Desired quality level, currently from -0.1 to 1.0 (lo to hi).
- *
- * \return Zero for success, and negative values for failure.
- *
- * \retval  0          Success
- * \retval  OV_EFAULT  Internal logic fault; indicates a bug or heap/stack corruption.
- * \retval  OV_EINVAL  Invalid setup request, eg, out of range argument.
- * \retval  OV_EIMPL   Unimplemented mode; unable to comply with quality level request.
- */
-extern int vorbis_encode_setup_vbr(vorbis_info *vi,
-                                  long channels,
-                                  long rate,
-
-                                  float quality
-                                  );
-
-/**
- * This is the primary function within libvorbisenc for setting up variable
- * bitrate ("quality" based) modes.
- *
- *
- * Before this function is called, the vorbis_info struct should be
- * initialized by using vorbis_info_init() from the libvorbis API. After
- * encoding, vorbis_info_clear() should be called.
- *
- * \param vi           Pointer to an initialized vorbis_info struct.
- * \param channels     The number of channels to be encoded.
- * \param rate         The sampling rate of the source audio.
- * \param base_quality Desired quality level, currently from -0.1 to 1.0 (lo to hi).
- *
- *
- * \return Zero for success, or a negative number for failure.
- *
- * \retval 0           Success
- * \retval OV_EFAULT   Internal logic fault; indicates a bug or heap/stack corruption.
- * \retval OV_EINVAL   Invalid setup request, eg, out of range argument.
- * \retval OV_EIMPL    Unimplemented mode; unable to comply with quality level request.
- */
-extern int vorbis_encode_init_vbr(vorbis_info *vi,
-                                  long channels,
-                                  long rate,
-
-                                  float base_quality
-                                  );
-
-/**
- * This function performs the last stage of three-step encoding setup, as
- * described in the API overview under managed bitrate modes.
- *
- * Before this function is called, the \ref vorbis_info struct should be
- * initialized by using vorbis_info_init() from the libvorbis API, one of
- * \ref vorbis_encode_setup_managed() or \ref vorbis_encode_setup_vbr() called to
- * initialize the high-level encoding setup, and \ref vorbis_encode_ctl()
- * called if necessary to make encoding setup changes.
- * vorbis_encode_setup_init() finalizes the highlevel encoding structure into
- * a complete encoding setup after which the application may make no further
- * setup changes.
- *
- * After encoding, vorbis_info_clear() should be called.
- *
- * \param vi Pointer to an initialized \ref vorbis_info struct.
- *
- * \return Zero for success, and negative values for failure.
- *
- * \retval  0           Success.
- * \retval  OV_EFAULT  Internal logic fault; indicates a bug or heap/stack corruption.
- *
- * \retval OV_EINVAL   Attempt to use vorbis_encode_setup_init() without first
- * calling one of vorbis_encode_setup_managed() or vorbis_encode_setup_vbr() to
- * initialize the high-level encoding setup
- *
- */
-extern int vorbis_encode_setup_init(vorbis_info *vi);
-
-/**
- * This function implements a generic interface to miscellaneous encoder
- * settings similar to the classic UNIX 'ioctl()' system call.  Applications
- * may use vorbis_encode_ctl() to query or set bitrate management or quality
- * mode details by using one of several \e request arguments detailed below.
- * vorbis_encode_ctl() must be called after one of
- * vorbis_encode_setup_managed() or vorbis_encode_setup_vbr().  When used
- * to modify settings, \ref vorbis_encode_ctl() must be called before \ref
- * vorbis_encode_setup_init().
- *
- * \param vi      Pointer to an initialized vorbis_info struct.
- *
- * \param number Specifies the desired action; See \ref encctlcodes "the list
- * of available requests".
- *
- * \param arg void * pointing to a data structure matching the request
- * argument.
- *
- * \retval 0          Success. Any further return information (such as the result of a
- * query) is placed into the storage pointed to by *arg.
- *
- * \retval OV_EINVAL  Invalid argument, or an attempt to modify a setting after
- * calling vorbis_encode_setup_init().
- *
- * \retval OV_EIMPL   Unimplemented or unknown request
- */
-extern int vorbis_encode_ctl(vorbis_info *vi,int number,void *arg);
-
-/**
- * \deprecated This is a deprecated interface. Please use vorbis_encode_ctl()
- * with the \ref ovectl_ratemanage2_arg struct and \ref
- * OV_ECTL_RATEMANAGE2_GET and \ref OV_ECTL_RATEMANAGE2_SET calls in new code.
- *
- * The \ref ovectl_ratemanage_arg structure is used with vorbis_encode_ctl()
- * and the \ref OV_ECTL_RATEMANAGE_GET, \ref OV_ECTL_RATEMANAGE_SET, \ref
- * OV_ECTL_RATEMANAGE_AVG, \ref OV_ECTL_RATEMANAGE_HARD calls in order to
- * query and modify specifics of the encoder's bitrate management
- * configuration.
-*/
-struct ovectl_ratemanage_arg {
-  int    management_active; /**< nonzero if bitrate management is active*/
-/** hard lower limit (in kilobits per second) below which the stream bitrate
-    will never be allowed for any given bitrate_hard_window seconds of time.*/
-  long   bitrate_hard_min;
-/** hard upper limit (in kilobits per second) above which the stream bitrate
-    will never be allowed for any given bitrate_hard_window seconds of time.*/
-  long   bitrate_hard_max;
-/** the window period (in seconds) used to regulate the hard bitrate minimum
-    and maximum*/
-  double bitrate_hard_window;
-/** soft lower limit (in kilobits per second) below which the average bitrate
-    tracker will start nudging the bitrate higher.*/
-  long   bitrate_av_lo;
-/** soft upper limit (in kilobits per second) above which the average bitrate
-    tracker will start nudging the bitrate lower.*/
-  long   bitrate_av_hi;
-/** the window period (in seconds) used to regulate the average bitrate
-    minimum and maximum.*/
-  double bitrate_av_window;
-/** Regulates the relative centering of the average and hard windows; in
-    libvorbis 1.0 and 1.0.1, the hard window regulation overlapped but
-    followed the average window regulation. In libvorbis 1.1 a bit-reservoir
-    interface replaces the old windowing interface; the older windowing
-    interface is simulated and this field has no effect.*/
-  double bitrate_av_window_center;
-};
-
-/**
- * \name struct ovectl_ratemanage2_arg
- *
- * The ovectl_ratemanage2_arg structure is used with vorbis_encode_ctl() and
- * the OV_ECTL_RATEMANAGE2_GET and OV_ECTL_RATEMANAGE2_SET calls in order to
- * query and modify specifics of the encoder's bitrate management
- * configuration.
- *
-*/
-struct ovectl_ratemanage2_arg {
-  int    management_active; /**< nonzero if bitrate management is active */
-/** Lower allowed bitrate limit in kilobits per second */
-  long   bitrate_limit_min_kbps;
-/** Upper allowed bitrate limit in kilobits per second */
-  long   bitrate_limit_max_kbps;
-  long   bitrate_limit_reservoir_bits; /**<Size of the bitrate reservoir in bits */
-/** Regulates the bitrate reservoir's preferred fill level in a range from 0.0
- * to 1.0; 0.0 tries to bank bits to buffer against future bitrate spikes, 1.0
- * buffers against future sudden drops in instantaneous bitrate. Default is
- * 0.1
- */
-  double bitrate_limit_reservoir_bias;
-/** Average bitrate setting in kilobits per second */
-  long   bitrate_average_kbps;
-/** Slew rate limit setting for average bitrate adjustment; sets the minimum
- *  time in seconds the bitrate tracker may swing from one extreme to the
- *  other when boosting or damping average bitrate.
- */
-  double bitrate_average_damping;
-};
-
-
-/**
- * \name vorbis_encode_ctl() codes
- *
- * \anchor encctlcodes
- *
- * These values are passed as the \c number parameter of vorbis_encode_ctl().
- * The type of the referent of that function's \c arg pointer depends on these
- * codes.
- */
-/*@{*/
-
-/**
- * Query the current encoder bitrate management setting.
- *
- *Argument: <tt>struct ovectl_ratemanage2_arg *</tt>
- *
- * Used to query the current encoder bitrate management setting. Also used to
- * initialize fields of an ovectl_ratemanage2_arg structure for use with
- * \ref OV_ECTL_RATEMANAGE2_SET.
- */
-#define OV_ECTL_RATEMANAGE2_GET      0x14
-
-/**
- * Set the current encoder bitrate management settings.
- *
- * Argument: <tt>struct ovectl_ratemanage2_arg *</tt>
- *
- * Used to set the current encoder bitrate management settings to the values
- * listed in the ovectl_ratemanage2_arg. Passing a NULL pointer will disable
- * bitrate management.
-*/
-#define OV_ECTL_RATEMANAGE2_SET      0x15
-
-/**
- * Returns the current encoder hard-lowpass setting (kHz) in the double
- * pointed to by arg.
- *
- * Argument: <tt>double *</tt>
-*/
-#define OV_ECTL_LOWPASS_GET          0x20
-
-/**
- *  Sets the encoder hard-lowpass to the value (kHz) pointed to by arg. Valid
- *  lowpass settings range from 2 to 99.
- *
- * Argument: <tt>double *</tt>
-*/
-#define OV_ECTL_LOWPASS_SET          0x21
-
-/**
- *  Returns the current encoder impulse block setting in the double pointed
- *  to by arg.
- *
- * Argument: <tt>double *</tt>
-*/
-#define OV_ECTL_IBLOCK_GET           0x30
-
-/**
- *  Sets the impulse block bias to the the value pointed to by arg.
- *
- * Argument: <tt>double *</tt>
- *
- *  Valid range is -15.0 to 0.0 [default]. A negative impulse block bias will
- *  direct to encoder to use more bits when incoding short blocks that contain
- *  strong impulses, thus improving the accuracy of impulse encoding.
- */
-#define OV_ECTL_IBLOCK_SET           0x31
-
-/**
- *  Returns the current encoder coupling setting in the int pointed
- *  to by arg.
- *
- * Argument: <tt>int *</tt>
-*/
-#define OV_ECTL_COUPLING_GET         0x40
-
-/**
- *  Enables/disables channel coupling in multichannel encoding according to arg.
- *
- * Argument: <tt>int *</tt>
- *
- *  Zero disables channel coupling for multichannel inputs, nonzer enables
- *  channel coupling.  Setting has no effect on monophonic encoding or
- *  multichannel counts that do not offer coupling.  At present, coupling is
- *  available for stereo and 5.1 encoding.
- */
-#define OV_ECTL_COUPLING_SET         0x41
-
-  /* deprecated rate management supported only for compatibility */
-
-/**
- * Old interface to querying bitrate management settings.
- *
- * Deprecated after move to bit-reservoir style management in 1.1 rendered
- * this interface partially obsolete.
-
- * \deprecated Please use \ref OV_ECTL_RATEMANAGE2_GET instead.
- *
- * Argument: <tt>struct ovectl_ratemanage_arg *</tt>
- */
-#define OV_ECTL_RATEMANAGE_GET       0x10
-/**
- * Old interface to modifying bitrate management settings.
- *
- *  deprecated after move to bit-reservoir style management in 1.1 rendered
- *  this interface partially obsolete.
- *
- * \deprecated Please use \ref OV_ECTL_RATEMANAGE2_SET instead.
- *
- * Argument: <tt>struct ovectl_ratemanage_arg *</tt>
- */
-#define OV_ECTL_RATEMANAGE_SET       0x11
-/**
- * Old interface to setting average-bitrate encoding mode.
- *
- * Deprecated after move to bit-reservoir style management in 1.1 rendered
- * this interface partially obsolete.
- *
- *  \deprecated Please use \ref OV_ECTL_RATEMANAGE2_SET instead.
- *
- * Argument: <tt>struct ovectl_ratemanage_arg *</tt>
- */
-#define OV_ECTL_RATEMANAGE_AVG       0x12
-/**
- * Old interface to setting bounded-bitrate encoding modes.
- *
- * deprecated after move to bit-reservoir style management in 1.1 rendered
- * this interface partially obsolete.
- *
- *  \deprecated Please use \ref OV_ECTL_RATEMANAGE2_SET instead.
- *
- * Argument: <tt>struct ovectl_ratemanage_arg *</tt>
- */
-#define OV_ECTL_RATEMANAGE_HARD      0x13
-
-/*@}*/
-
-
-
-#ifdef __cplusplus
-}
-#endif /* __cplusplus */
-
-#endif

+ 0 - 206
include/vorbis/vorbisfile.h

@@ -1,206 +0,0 @@
-/********************************************************************
- *                                                                  *
- * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
- * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
- * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
- * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
- *                                                                  *
- * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007             *
- * by the Xiph.Org Foundation http://www.xiph.org/                  *
- *                                                                  *
- ********************************************************************
-
- function: stdio-based convenience library for opening/seeking/decoding
- last mod: $Id: vorbisfile.h 17182 2010-04-29 03:48:32Z xiphmont $
-
- ********************************************************************/
-
-#ifndef _OV_FILE_H_
-#define _OV_FILE_H_
-
-#ifdef __cplusplus
-extern "C"
-{
-#endif /* __cplusplus */
-
-#include <stdio.h>
-#include "codec.h"
-
-/* The function prototypes for the callbacks are basically the same as for
- * the stdio functions fread, fseek, fclose, ftell.
- * The one difference is that the FILE * arguments have been replaced with
- * a void * - this is to be used as a pointer to whatever internal data these
- * functions might need. In the stdio case, it's just a FILE * cast to a void *
- *
- * If you use other functions, check the docs for these functions and return
- * the right values. For seek_func(), you *MUST* return -1 if the stream is
- * unseekable
- */
-typedef struct {
-  size_t (*read_func)  (void *ptr, size_t size, size_t nmemb, void *datasource);
-  int    (*seek_func)  (void *datasource, ogg_int64_t offset, int whence);
-  int    (*close_func) (void *datasource);
-  long   (*tell_func)  (void *datasource);
-} ov_callbacks;
-
-#ifndef OV_EXCLUDE_STATIC_CALLBACKS
-
-/* a few sets of convenient callbacks, especially for use under
- * Windows where ov_open_callbacks() should always be used instead of
- * ov_open() to avoid problems with incompatible crt.o version linking
- * issues. */
-
-static int _ov_header_fseek_wrap(FILE *f,ogg_int64_t off,int whence){
-  if(f==NULL)return(-1);
-
-#ifdef __MINGW32__
-  return fseeko64(f,off,whence);
-#elif defined (_WIN32)
-  return _fseeki64(f,off,whence);
-#else
-  return fseek(f,off,whence);
-#endif
-}
-
-/* These structs below (OV_CALLBACKS_DEFAULT etc) are defined here as
- * static data. That means that every file which includes this header
- * will get its own copy of these structs whether it uses them or
- * not unless it #defines OV_EXCLUDE_STATIC_CALLBACKS.
- * These static symbols are essential on platforms such as Windows on
- * which several different versions of stdio support may be linked to
- * by different DLLs, and we need to be certain we know which one
- * we're using (the same one as the main application).
- */
-
-static ov_callbacks OV_CALLBACKS_DEFAULT = {
-  (size_t (*)(void *, size_t, size_t, void *))  fread,
-  (int (*)(void *, ogg_int64_t, int))           _ov_header_fseek_wrap,
-  (int (*)(void *))                             fclose,
-  (long (*)(void *))                            ftell
-};
-
-static ov_callbacks OV_CALLBACKS_NOCLOSE = {
-  (size_t (*)(void *, size_t, size_t, void *))  fread,
-  (int (*)(void *, ogg_int64_t, int))           _ov_header_fseek_wrap,
-  (int (*)(void *))                             NULL,
-  (long (*)(void *))                            ftell
-};
-
-static ov_callbacks OV_CALLBACKS_STREAMONLY = {
-  (size_t (*)(void *, size_t, size_t, void *))  fread,
-  (int (*)(void *, ogg_int64_t, int))           NULL,
-  (int (*)(void *))                             fclose,
-  (long (*)(void *))                            NULL
-};
-
-static ov_callbacks OV_CALLBACKS_STREAMONLY_NOCLOSE = {
-  (size_t (*)(void *, size_t, size_t, void *))  fread,
-  (int (*)(void *, ogg_int64_t, int))           NULL,
-  (int (*)(void *))                             NULL,
-  (long (*)(void *))                            NULL
-};
-
-#endif
-
-#define  NOTOPEN   0
-#define  PARTOPEN  1
-#define  OPENED    2
-#define  STREAMSET 3
-#define  INITSET   4
-
-typedef struct OggVorbis_File {
-  void            *datasource; /* Pointer to a FILE *, etc. */
-  int              seekable;
-  ogg_int64_t      offset;
-  ogg_int64_t      end;
-  ogg_sync_state   oy;
-
-  /* If the FILE handle isn't seekable (eg, a pipe), only the current
-     stream appears */
-  int              links;
-  ogg_int64_t     *offsets;
-  ogg_int64_t     *dataoffsets;
-  long            *serialnos;
-  ogg_int64_t     *pcmlengths; /* overloaded to maintain binary
-                                  compatibility; x2 size, stores both
-                                  beginning and end values */
-  vorbis_info     *vi;
-  vorbis_comment  *vc;
-
-  /* Decoding working state local storage */
-  ogg_int64_t      pcm_offset;
-  int              ready_state;
-  long             current_serialno;
-  int              current_link;
-
-  double           bittrack;
-  double           samptrack;
-
-  ogg_stream_state os; /* take physical pages, weld into a logical
-                          stream of packets */
-  vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
-  vorbis_block     vb; /* local working space for packet->PCM decode */
-
-  ov_callbacks callbacks;
-
-} OggVorbis_File;
-
-
-extern int ov_clear(OggVorbis_File *vf);
-extern int ov_fopen(const char *path,OggVorbis_File *vf);
-extern int ov_open(FILE *f,OggVorbis_File *vf,const char *initial,long ibytes);
-extern int ov_open_callbacks(void *datasource, OggVorbis_File *vf,
-                const char *initial, long ibytes, ov_callbacks callbacks);
-
-extern int ov_test(FILE *f,OggVorbis_File *vf,const char *initial,long ibytes);
-extern int ov_test_callbacks(void *datasource, OggVorbis_File *vf,
-                const char *initial, long ibytes, ov_callbacks callbacks);
-extern int ov_test_open(OggVorbis_File *vf);
-
-extern long ov_bitrate(OggVorbis_File *vf,int i);
-extern long ov_bitrate_instant(OggVorbis_File *vf);
-extern long ov_streams(OggVorbis_File *vf);
-extern long ov_seekable(OggVorbis_File *vf);
-extern long ov_serialnumber(OggVorbis_File *vf,int i);
-
-extern ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i);
-extern ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i);
-extern double ov_time_total(OggVorbis_File *vf,int i);
-
-extern int ov_raw_seek(OggVorbis_File *vf,ogg_int64_t pos);
-extern int ov_pcm_seek(OggVorbis_File *vf,ogg_int64_t pos);
-extern int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos);
-extern int ov_time_seek(OggVorbis_File *vf,double pos);
-extern int ov_time_seek_page(OggVorbis_File *vf,double pos);
-
-extern int ov_raw_seek_lap(OggVorbis_File *vf,ogg_int64_t pos);
-extern int ov_pcm_seek_lap(OggVorbis_File *vf,ogg_int64_t pos);
-extern int ov_pcm_seek_page_lap(OggVorbis_File *vf,ogg_int64_t pos);
-extern int ov_time_seek_lap(OggVorbis_File *vf,double pos);
-extern int ov_time_seek_page_lap(OggVorbis_File *vf,double pos);
-
-extern ogg_int64_t ov_raw_tell(OggVorbis_File *vf);
-extern ogg_int64_t ov_pcm_tell(OggVorbis_File *vf);
-extern double ov_time_tell(OggVorbis_File *vf);
-
-extern vorbis_info *ov_info(OggVorbis_File *vf,int link);
-extern vorbis_comment *ov_comment(OggVorbis_File *vf,int link);
-
-extern long ov_read_float(OggVorbis_File *vf,float ***pcm_channels,int samples,
-                          int *bitstream);
-extern long ov_read_filter(OggVorbis_File *vf,char *buffer,int length,
-                          int bigendianp,int word,int sgned,int *bitstream,
-                          void (*filter)(float **pcm,long channels,long samples,void *filter_param),void *filter_param);
-extern long ov_read(OggVorbis_File *vf,char *buffer,int length,
-                    int bigendianp,int word,int sgned,int *bitstream);
-extern int ov_crosslap(OggVorbis_File *vf1,OggVorbis_File *vf2);
-
-extern int ov_halfrate(OggVorbis_File *vf,int flag);
-extern int ov_halfrate_p(OggVorbis_File *vf);
-
-#ifdef __cplusplus
-}
-#endif /* __cplusplus */
-
-#endif
-

+ 1 - 1
lib

@@ -1 +1 @@
-Subproject commit cbd0b400c695ee6626d562365990846bc74cfc99
+Subproject commit 5fe8534491efb52b6fe0d7e47274916eeceffc58

+ 2 - 2
src/core/PolyAAssetFileProvider.cpp

@@ -107,9 +107,9 @@ long AAssetFile::write( const void * ptr, size_t size, size_t count) {
 }
 
 int AAssetFile::seek(long int offset, int origin) {
-	return AAsset_seek(file, offset, origin);
+	return AAsset_seek64(file, offset, origin);
 }
 
 long AAssetFile::tell() {
-	return AAsset_getLength64(file);
+	return AAsset_getLength64(file) - AAsset_getRemainingLength64(file);
 }

+ 126 - 9
src/core/PolyAndroidCore.cpp

@@ -84,8 +84,6 @@ AndroidCore::AndroidCore(PolycodeView *view, int xRes, int yRes, bool fullScreen
 	defaultWorkingDirectory = view->native_activity->internalDataPath;
 	userHomeDirectory = view->native_activity->externalDataPath;
 	
-	services->getSoundManager()->setAudioInterface(new AudioInterface());
-	
 	paused = true;
 	
 	initKeyMap();
@@ -93,7 +91,8 @@ AndroidCore::AndroidCore(PolycodeView *view, int xRes, int yRes, bool fullScreen
 	this->view = view;
 	core = this;
 	
-	services->getSoundManager()->setAudioInterface(new OpenSLAudioInterface());
+	audioInterface = new OpenSLAudioInterface();
+	services->getSoundManager()->setAudioInterface(audioInterface);
 	extractResources();
 }
 
@@ -137,12 +136,15 @@ void AndroidCore::checkEvents() {
 			case InputEvent::EVENT_MOUSEUP:
 				input->setMouseButtonState(event.mouseButton, false, getTicks());
 				break;
+			case InputEvent::EVENT_TEXTINPUT:
+				input->textInput(event.text);
+				break;
 			case InputEvent::EVENT_KEYDOWN:
 				if (!checkSpecialKeyEvents(event.keyCode))
-					input->setKeyState(event.keyCode, event.unicodeChar, true, getTicks());
+					input->setKeyState(event.keyCode, true, getTicks());
 				break;
 			case InputEvent::EVENT_KEYUP:
-				input->setKeyState(event.keyCode, event.unicodeChar, false, getTicks());
+				input->setKeyState(event.keyCode, false, getTicks());
 				break;
 			case InputEvent::EVENT_TOUCHES_BEGAN:
 				input->touchesBegan(event.touch, event.touches, getTicks());
@@ -249,8 +251,9 @@ void AndroidCore::openOnScreenKeyboard(bool open){
 		jboolean lRes = jniEnv->CallBooleanMethod(lInputMethodManager, MethodHideSoftInput, lBinder, lFlags); 
 	} 
 
-	// Finished with the JVM. 
-	javaVM->DetachCurrentThread(); 
+	// Finished with the JVM.
+	if (attached)
+	javaVM->DetachCurrentThread();
 }
 
 void launchThread(Threaded *target) {
@@ -279,11 +282,121 @@ CoreMutex *AndroidCore::createMutex() {
 }
 
 void AndroidCore::copyStringToClipboard(const String& str) {
+	// Attaches the current thread to the JVM. 
+	JavaVM* javaVM = view->native_activity->vm;
+	JNIEnv* jniEnv;
+	bool attached = false;
 
+	if(javaVM->GetEnv((void**)&jniEnv, JNI_VERSION_1_6) ==JNI_EDETACHED){
+		JavaVMAttachArgs attachArgs;
+		attachArgs.version = JNI_VERSION_1_6;
+		attachArgs.name = "NativeThread";
+		attachArgs.group = NULL;
+		
+		jint result = javaVM->AttachCurrentThread(&jniEnv, &attachArgs);
+		if(result == JNI_ERR){
+			return;
+		}
+		attached = true;
+	} 
+
+	
+	jclass looperClass = jniEnv->FindClass("android/os/Looper");
+	jmethodID prepareMethodID = jniEnv->GetStaticMethodID(looperClass, "prepare", "()V");
+	jniEnv->CallStaticVoidMethod(looperClass, prepareMethodID);
+	
+	// Retrieves Context.CLIPBOARD_SERVICE. 
+	jclass ClassContext = jniEnv->FindClass("android/content/Context");
+	jfieldID FieldCLIPBOARD_SERVICE = jniEnv->GetStaticFieldID(ClassContext, "CLIPBOARD_SERVICE", "Ljava/lang/String;");
+	jobject CLIPBOARD_SERVICE = jniEnv->GetStaticObjectField(ClassContext, FieldCLIPBOARD_SERVICE);
+// 	jstring CLIPBOARD_SERVICE = jniEnv->NewStringUTF("clipboard");
+
+	jclass ClassNativeActivity = jniEnv->FindClass("android/app/NativeActivity");
+	jobject lNativeActivity = view->native_activity->clazz;
+	
+	// Runs getSystemService(Context.CLIPBOARD_SERVICE).
+	jclass ClassClipboardManager = jniEnv->FindClass("android/content/ClipboardManager");
+	jmethodID MethodGetSystemService = jniEnv->GetMethodID(ClassNativeActivity, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
+	jobject lCliboardManager = jniEnv->CallObjectMethod(lNativeActivity, MethodGetSystemService, CLIPBOARD_SERVICE);
+	
+	// Runs clipData.newPlainText()
+	jclass ClassClipData = jniEnv->FindClass("android/content/ClipData");
+	jmethodID MethodnewPlainText = jniEnv->GetStaticMethodID(ClassClipData, "newPlainText", "(Ljava/lang/CharSequence;Ljava/lang/CharSequence;)Landroid/content/ClipData;");
+	jstring text = jniEnv->NewStringUTF(str.c_str());
+	jstring lbl = jniEnv->NewStringUTF("PolycodeText");
+	jobject lClipData = jniEnv->CallStaticObjectMethod(ClassClipData, MethodnewPlainText, lbl, text);
+	
+	jmethodID MethodSetPrimaryClip = jniEnv->GetMethodID(ClassClipboardManager, "setPrimaryClip", "(Landroid/content/ClipData;)V");
+	jniEnv->CallVoidMethod(lCliboardManager, MethodSetPrimaryClip, lClipData);
+	
+	// Finished with the JVM.
+	if (attached)
+		javaVM->DetachCurrentThread();
 }
 
 String AndroidCore::getClipboardString() {
-	return "";
+	// Attaches the current thread to the JVM. 
+	JavaVM* javaVM = view->native_activity->vm;
+	JNIEnv* jniEnv;
+	bool attached = false;
+
+	if(javaVM->GetEnv((void**)&jniEnv, JNI_VERSION_1_6) ==JNI_EDETACHED){
+		JavaVMAttachArgs attachArgs;
+		attachArgs.version = JNI_VERSION_1_6;
+		attachArgs.name = "NativeThread";
+		attachArgs.group = NULL;
+		
+		jint result = javaVM->AttachCurrentThread(&jniEnv, &attachArgs);
+		if(result == JNI_ERR){
+			return "";
+		}
+		attached = true;
+	} 
+	
+	jclass looperClass = jniEnv->FindClass("android/os/Looper");
+	jmethodID prepareMethodID = jniEnv->GetStaticMethodID(looperClass, "prepare", "()V");
+	jniEnv->CallStaticVoidMethod(looperClass, prepareMethodID);
+
+	// Retrieves Context.CLIPBOARD_SERVICE. 
+	jclass ClassContext = jniEnv->FindClass("android/content/Context");
+	jfieldID FieldCLIPBOARD_SERVICE = jniEnv->GetStaticFieldID(ClassContext, "CLIPBOARD_SERVICE", "Ljava/lang/String;");
+	jobject CLIPBOARD_SERVICE = jniEnv->GetStaticObjectField(ClassContext, FieldCLIPBOARD_SERVICE);
+
+	jclass ClassNativeActivity = jniEnv->FindClass("android/app/NativeActivity");
+	jobject lNativeActivity = view->native_activity->clazz;
+	
+	// Runs getSystemService(Context.CLIPBOARD_SERVICE).
+	jclass ClassClipboardManager = jniEnv->FindClass("android/content/ClipboardManager");
+	jmethodID MethodGetSystemService = jniEnv->GetMethodID(ClassNativeActivity, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
+	jobject lCliboardManager = jniEnv->CallObjectMethod(lNativeActivity, MethodGetSystemService, CLIPBOARD_SERVICE);
+
+	// Runs clipboard.getPrimaryClip()
+	jmethodID MethodGetPrimaryClip = jniEnv->GetMethodID(ClassClipboardManager, "getPrimaryClip", "()Landroid/content/ClipData;");
+	jobject lClipData = jniEnv->CallObjectMethod(lCliboardManager, MethodGetPrimaryClip);
+	
+	//Runs clipdata.getItemAt(0)
+	jclass ClassClipDataItem = jniEnv->FindClass("android/content/ClipData$Item");
+	jclass ClassClipData = jniEnv->FindClass("android/content/ClipData");
+	jmethodID MethodGetItemAt = jniEnv->GetMethodID(ClassClipData, "getItemAt", "(I)Landroid/content/ClipData$Item;");
+	jobject lClipDataItem = jniEnv->CallObjectMethod(lClipData, MethodGetItemAt, 0);
+	
+	//Runs clipdescription.getText()
+	jmethodID MethodGetText = jniEnv->GetMethodID(ClassClipDataItem, "getText", "()Ljava/lang/CharSequence;");
+	jobject lCharSequence = jniEnv->CallObjectMethod(lClipDataItem, MethodGetText);
+	
+	//Runs charseuquence.toString()
+	jclass ClassCharSequence = jniEnv->FindClass("java/lang/CharSequence");
+	jmethodID MethodtoString = jniEnv->GetMethodID(ClassCharSequence, "toString", "()Ljava/lang/String;");
+	jobject lText = jniEnv->CallObjectMethod(lCharSequence, MethodtoString);
+	
+	const char *nativeString = jniEnv->GetStringUTFChars((jstring)lText, (jboolean*)0);
+	String returnStr = String(nativeString);
+	jniEnv->ReleaseStringUTFChars((jstring)lText, nativeString);
+	
+	// Finished with the JVM.
+	if (attached)
+		javaVM->DetachCurrentThread();
+	return returnStr;
 }
 
 void AndroidCore::extractResources(){
@@ -328,6 +441,7 @@ void AndroidCore::extractResources(){
 			while(source->read(buffer, sizeof(char), 1) > 0){
 				dest->write(buffer,sizeof(char), 1);
 			}
+			bfileProvider->closeFile(dest);
 		}
 		
 		afileProvider->closeFile(source);
@@ -664,7 +778,7 @@ void AndroidCore::initKeyMap() {
 	keyMap[AKEYCODE_META_RIGHT] = KEY_RSUPER;
 	keyMap[AKEYCODE_META_LEFT] = KEY_LSUPER;
 
-	keyMap[AKEYCODE_HELP] = KEY_HELP;
+	keyMap[259] = KEY_HELP;
  	keyMap[AKEYCODE_SYSRQ] = KEY_PRINT;
 	keyMap[AKEYCODE_BREAK] = KEY_BREAK;
 	keyMap[AKEYCODE_MENU] = KEY_MENU;
@@ -675,3 +789,6 @@ PolyKEY AndroidCore::mapKey(int keyCode){
 	return keyMap[(unsigned int) keyCode];
 }
 
+OpenSLAudioInterface* AndroidCore::getAudioInterface(){
+	return audioInterface;
+}

+ 5 - 2
src/core/PolyCocoaCore.mm

@@ -494,10 +494,10 @@ void CocoaCore::checkEvents() {
 						break;
 					case InputEvent::EVENT_KEYDOWN:
 						if(!checkSpecialKeyEvents(event.keyCode))
-							input->setKeyState(event.keyCode, event.unicodeChar, true, getTicks());
+							input->setKeyState(event.keyCode, true, getTicks());
 						break;
 					case InputEvent::EVENT_KEYUP:
-						input->setKeyState(event.keyCode, event.unicodeChar, false, getTicks());
+						input->setKeyState(event.keyCode, false, getTicks());
                     break;
                     case InputEvent::EVENT_TOUCHES_BEGAN:
                         input->touchesBegan(event.touch, event.touches, getTicks());
@@ -508,6 +508,9 @@ void CocoaCore::checkEvents() {
                     case InputEvent::EVENT_TOUCHES_MOVED:
                         input->touchesMoved(event.touch, event.touches, getTicks());
                     break;
+					case InputEvent::EVENT_TEXTINPUT:
+						input->textInput(event.text);
+					break;
 				}
 				break;
 				case CocoaEvent::FOCUS_EVENT:

+ 11 - 8
src/core/PolyCoreInput.cpp

@@ -242,7 +242,7 @@ namespace Polycode {
 			return false;
 	}
 	
-	void CoreInput::setKeyState(PolyKEY keyCode, wchar_t code, bool newState, int ticks) {
+	void CoreInput::setKeyState(PolyKEY keyCode, bool newState, int ticks) {
 		
 		if(newState && !keyRepeat) {
 			if(keyboardState[keyCode]) {
@@ -250,7 +250,7 @@ namespace Polycode {
 			}
 		}
 		
-		InputEvent *evt = new InputEvent(keyCode, code, ticks);
+		InputEvent *evt = new InputEvent(keyCode, ticks);
 		if(keyCode < 512)
 			keyboardState[keyCode] = newState;
 		if(newState) {
@@ -260,6 +260,15 @@ namespace Polycode {
 		}
 	}
 	
+	void CoreInput::textInput(String text){
+		InputEvent* iev = new InputEvent();
+		iev->text = "";
+		iev->text = text;
+		
+		dispatchEvent(iev, InputEvent::EVENT_TEXTINPUT);
+	}
+
+	
 	void CoreInput::touchesBegan(TouchInfo touch, std::vector<TouchInfo> touches, int ticks) {
 		if(ignoreOffScreenTouch) {
 			Core *core = CoreServices::getInstance()->getCore();
@@ -295,12 +304,6 @@ namespace Polycode {
 		}
 	}
 
-
-
-
-
-
-	
 	void CoreInput::touchesEnded(TouchInfo touch, std::vector<TouchInfo> touches, int ticks) {
 		if(ignoreOffScreenTouch) {
 			Core *core = CoreServices::getInstance()->getCore();

+ 7 - 1
src/core/PolyImage.cpp

@@ -61,9 +61,11 @@ void Image::setPixelType(int type) {
 		case IMAGE_RGBA:
 			pixelSize = 4;						
 		break;
+#ifndef NO_FP16
 		case IMAGE_FP16:		
 			pixelSize = 6;
 		break;
+#endif
 		default:
 			pixelSize = 4;								
 			break;
@@ -624,7 +626,7 @@ bool Image::loadSTB(const String &fileName) {
 }
 
 bool Image::loadHDR(const String &fileName) {
-	
+#ifndef NO_FP16
 	imageType = Image::IMAGE_FP16;
 	
 	CoreFile *infile = Services()->getCore()->openFile(fileName.c_str(), "rb");
@@ -660,6 +662,10 @@ bool Image::loadHDR(const String &fileName) {
 	
 	
 	return true;
+#else
+	Logger::log("HDR not supported!");
+	return false;
+#endif
 }
 
 void Image::transformCoordinates(int *x, int *y) {

+ 1 - 13
src/core/PolyInputEvent.cpp

@@ -37,23 +37,11 @@ InputEvent::InputEvent(Vector2 mousePosition, int timestamp) : Event() {
 	eventType = "InputEvent";
 }
 
-InputEvent::InputEvent(PolyKEY key, wchar_t charCode, int timestamp) : Event() {
+InputEvent::InputEvent(PolyKEY key, int timestamp) : Event() {
 	this->key = key;
-	this->charCode = charCode;
 	this->timestamp = timestamp;
 	eventType = "InputEvent";	
 }
-	
-wchar_t InputEvent::getCharCode() {
-	return charCode;
-}
-
-/*
-InputEvent::InputEvent(PolyKEY key, int timestamp)	: Event() {
-	this->key = key;
-	this->timestamp = timestamp;	
-}
-*/
 
 InputEvent::~InputEvent() {
 

+ 10 - 4
src/core/PolyOpenGLGraphicsInterface.cpp

@@ -485,13 +485,15 @@ void OpenGLGraphicsInterface::createRenderBuffer(RenderBuffer *renderBuffer) {
 		renderBuffer->depthTexture->type = renderBuffer->colorTexture->type;
 		renderBuffer->depthTexture->filteringMode = Texture::FILTERING_LINEAR;
 		createTexture(&*renderBuffer->depthTexture);
-		
+#ifndef NO_FP16
 		if(renderBuffer->colorTexture->type == Image::IMAGE_FP16) {
 			glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, renderBuffer->getWidth(), renderBuffer->getHeight());
-			
 		} else {
+#endif
 			glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, renderBuffer->getWidth(), renderBuffer->getHeight());
+#ifndef NO_FP16
 		}
+#endif
 		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, *((GLuint*)renderBuffer->depthTexture->platformData), 0);
 	}
 	
@@ -553,6 +555,7 @@ void OpenGLGraphicsInterface::createTexture(Texture *texture) {
 			glTextureFormat = GL_RGB;
 			pixelType = GL_UNSIGNED_BYTE;
 			break;
+#ifndef NO_FP16
 		case Image::IMAGE_FP16:
 #ifdef BGRA_TEXTURE_FORMAT
 			glTextureType = GL_BGR;
@@ -568,6 +571,7 @@ void OpenGLGraphicsInterface::createTexture(Texture *texture) {
 #endif
 			pixelType = GL_FLOAT;
 			break;
+#endif
 		default:
 #ifdef BGRA_TEXTURE_FORMAT
 			glTextureType = GL_BGRA;
@@ -581,15 +585,17 @@ void OpenGLGraphicsInterface::createTexture(Texture *texture) {
 	
 	if(texture->depthTexture) {
 		glTextureType = GL_DEPTH_COMPONENT;
-		
+#ifndef NO_FP16
 		if(texture->type == Image::IMAGE_FP16) {
 			pixelType = GL_FLOAT;
 			glTextureFormat = GL_DEPTH_COMPONENT16;
 		} else {
+#endif
 			pixelType = GL_UNSIGNED_BYTE;
 			glTextureFormat = GL_DEPTH_COMPONENT;
+#ifndef NO_FP16
 		}
-		
+#endif		
 	  //  glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
 	}
 	

+ 1 - 1
src/core/PolyOpenSLAudioInterface.cpp

@@ -121,7 +121,7 @@ void OpenSLAudioInterface::terminateOpenSL(){
 
 void OpenSLAudioInterface::queueCallback(SLAndroidSimpleBufferQueueItf caller, void* pContext){
 	OpenSLAudioInterface *audioInterface = (OpenSLAudioInterface*) pContext;
-	if(audioInterface->buffer && audioInterface->getMixer()) {
+	if(audioInterface->buffer && audioInterface->getMixer() && !Services()->getCore()->paused) {
 		int16_t *out = (int16_t*)audioInterface->buffer;
 		audioInterface->getMixer()->mixIntoBuffer(out, POLY_FRAMES_PER_BUFFER);
 		(*(audioInterface->mPlayerQueue))->Enqueue(audioInterface->mPlayerQueue, out, sizeof(int16_t)*POLY_FRAMES_PER_BUFFER*POLY_NUM_CHANNELS);

Diff do ficheiro suprimidas por serem muito extensas
+ 282 - 510
src/core/PolySDLCore.cpp


+ 542 - 582
src/core/PolySound.cpp

@@ -1,582 +1,542 @@
-/*
- Copyright (C) 2011 by Ivan Safrin
- 
- 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 "polycode/core/PolySound.h"
-
-#ifndef NO_OGG
-	#define OV_EXCLUDE_STATIC_CALLBACKS
-	#include <vorbis/vorbisfile.h>
-#endif
-
-#undef OV_EXCLUDE_STATIC_CALLBACKS
-#include "polycode/core/PolyString.h"
-#include "polycode/core/PolyLogger.h"
-#include "polycode/core/PolySoundManager.h"
-#include "polycode/core/PolyCore.h"
-#include "polycode/core/PolyCoreServices.h"
-#include <stdlib.h>
-#include <string>
-#include <vector>
-#include <stdint.h>
-#include <limits>
-
-#ifndef MAX_FLOAT
-	#define MAX_FLOAT (std::numeric_limits<double>::infinity())
-#endif
-
-#ifndef INT32_MAX
-	#define INT32_MAX (std::numeric_limits<int32_t>::max())
-#endif
-
-#ifndef INT16_MAX
-	#define INT16_MAX (std::numeric_limits<int16_t>::max())
-#endif
-
-using namespace std;
-using namespace Polycode;
-
-AudioStreamingSource::AudioStreamingSource(unsigned int channels, unsigned int freq) : channels(channels), freq(freq) {
-}
-
-unsigned int AudioStreamingSource::getNumChannels() {
-	return channels;
-}
-
-unsigned int AudioStreamingSource::getFrequency() {
-	return freq;
-}
-
-unsigned int AudioStreamingSource::streamData(int16_t *buffer, unsigned int size) {
-	return 0;
-}
-
-#ifndef NO_OGG
-size_t custom_readfunc(void *ptr, size_t size, size_t nmemb, void *datasource) {
-	Polycode::CoreFile *file = (Polycode::CoreFile*) datasource;
-	return file->read(ptr, size, nmemb);
-}
-
-int custom_seekfunc(void *datasource, ogg_int64_t offset, int whence){
-	Polycode::CoreFile *file = (Polycode::CoreFile*) datasource;
-	return file->seek(offset, whence);
-}
-
-int custom_closefunc(void *datasource) {
-	Polycode::CoreFile *file = (Polycode::CoreFile*) datasource;
-	Services()->getCore()->closeFile(file);
-	return 0;
-}
-
-long custom_tellfunc(void *datasource) {
-	CoreFile *file = (CoreFile*) datasource;
-	return file->tell();
-}
-#endif
-
-Sound::Sound(const String& fileName) :	referenceDistance(1), maxDistance(MAX_FLOAT), pitch(1), volume(1), numSamples(-1), streamingSound(false), playing(false), playbackOffset(0), streamingSource(NULL), frequencyAdjust(1.0) {
-	soundLoaded = false;
-	setIsPositional(false);
-	loadFile(fileName);
-
-    if(soundLoaded) {
-        Services()->getSoundManager()->registerSound(this);
-    }
-}
-
-Sound::Sound(int size, const char *data, int channels, unsigned int freq, SoundFormat format) : referenceDistance(1), maxDistance(MAX_FLOAT), pitch(1), volume(1), numSamples(-1), streamingSound(false), playing(false) , playbackOffset(0), streamingSource(NULL), frequencyAdjust(1.0) {
-	setIsPositional(false);
-	soundLoaded = loadBytes(data, size, channels, freq, format);
-	if(soundLoaded) {
-		Services()->getSoundManager()->registerSound(this);
-	}
-}
-
-Sound::Sound(AudioStreamingSource *streamingSource) : referenceDistance(1), maxDistance(MAX_FLOAT), pitch(1), volume(1),  numSamples(-1), streamingSound(true), streamingSource(streamingSource), playing(false), playbackOffset(0), frequencyAdjust(1.0) {
-
-	soundBuffer = (int16_t*) malloc(sizeof(int16_t) * streamingSource->getNumChannels() * POLY_MIX_BUFFER_SIZE);
-	Services()->getSoundManager()->registerSound(this);
-	numChannels = streamingSource->getNumChannels();
-}
-
-void Sound::updateStream(unsigned int streamCount) {
-	if(streamingSource) {
-		playbackOffset = 0;
-		numSamples = streamCount;
-		streamingSource->streamData(soundBuffer, streamCount);
-	}
-}
-
-void Sound::loadFile(String fileName) {
-
-	if(soundLoaded) {
-		free(soundBuffer);
-	}
-
-	String actualFilename = fileName;
-	CoreFile *test = Services()->getCore()->openFile(fileName, "rb");
-	if(!test) {
-		actualFilename = "default/default.wav";
-	} else {
-		Services()->getCore()->closeFile(test);
-	}
-	
-	String extension;
-	size_t found;
-	found=actualFilename.rfind(".");
-	if (found!=string::npos) {
-		extension = actualFilename.substr(found+1);
-	} else {
-		extension = "";
-	}
-	
-	if(extension == "wav" || extension == "WAV") {
-		soundLoaded = loadWAV(actualFilename);
-	} else if(extension == "ogg" || extension == "OGG") {
-		soundLoaded = loadOGG(actualFilename);
-	}
-	
-	this->fileName = actualFilename;
-}
-
-String Sound::getFileName() {
-	return fileName;
-}
-
-Number Sound::getVolume() {
-	return volume;
-}
-
-Number Sound::getPitch() {
-	return pitch;
-}
-
-Sound::~Sound() {
-	free(soundBuffer);
-	Services()->getSoundManager()->unregisterSound(this);
-}
-
-void Sound::soundCheck(bool result, const String& err) {
-	if(!result)
-		Logger::log(err);
-}
-
-unsigned long Sound::readByte32(const unsigned char data[4]) {
-#if TAU_BIG_ENDIAN
-	return (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
-#else
-	return (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
-#endif
-}
-
-unsigned short Sound::readByte16(const unsigned char data[2]) {
-#if TAU_BIG_ENDIAN
-	return (data[0] << 8) + data[1];
-#else
-	return (data[1] << 8) + data[0];
-#endif	
-}
-
-void Sound::Play(bool loop, bool restartSound) {
-	if(restartSound) {
-		playbackOffset = 0;
-	}
-	playing = true;
-	looped = loop;
-}
-
-bool Sound::isPlaying() {
-	return playing;
-}
-
-bool Sound::isLooped() {
-	return looped;
-}
-
-
-void Sound::setVolume(Number newVolume) {
-	this->volume = newVolume;
-}
-
-void Sound::setPitch(Number newPitch) {
-	this->pitch = newPitch;
-}
-
-void Sound::setSoundPosition(const Vector3 &position) {
-	this->position = position;
-}
-
-void Sound::setSoundVelocity(const Vector3 &velocity) {
-	this->velocity = velocity;
-}
-
-void Sound::setSoundDirection(const Vector3 &direction) {
-	this->direction = direction;
-}
-
-
-Number Sound::getPlaybackTime() {
-	/*
-	float result = 0.0;
-	alGetSourcef(soundSource, AL_SEC_OFFSET, &result);
-	return result;
-	 */
-		//NOAL_TODO
-	return 0.0;
-}
-
-Number Sound::getPlaybackDuration() {
-	/*
-	ALint sizeInBytes;
-	ALint channels;
-	ALint bits;
-	ALint bufferID;
-	alGetSourcei(soundSource, AL_BUFFER, &bufferID);
-	
-	alGetBufferi(bufferID, AL_SIZE, &sizeInBytes);
-	alGetBufferi(bufferID, AL_CHANNELS, &channels);
-	alGetBufferi(bufferID, AL_BITS, &bits);
-
-	int lengthInSamples = sizeInBytes * 8 / (channels * bits);
-
-	ALint frequency;
-	alGetBufferi(bufferID, AL_FREQUENCY, &frequency);
-	Number durationInSeconds = (float)lengthInSamples / (float)frequency;
-	
-	return durationInSeconds;
-	 */
-		//NOAL_TODO
-	return 0.0;
-}
-		
-int Sound::getOffset() {
-	return playbackOffset;
-}
-
-void Sound::setOffset(unsigned int offset) {
-	playbackOffset = (offset);
-	
-	Number adjustedOffset = ((Number)playbackOffset) * pitch * frequencyAdjust;
-	
-	if((unsigned int)adjustedOffset >= numSamples) {
-		playbackOffset = 0;
-		if(!looped && !streamingSource) {
-			playing = false;
-		}
-	}
-}
-
-void Sound::seekTo(Number time) {
-	/*
-	if(time > getPlaybackDuration())
-		return;
-	alSourcef(soundSource, AL_SEC_OFFSET, time);
-	checkALError("Seek");
-	 */
-			//NOAL_TODO
-}
-
-int Sound::getSampleLength() {
-	return numSamples;
-}
-
-void Sound::setPositionalProperties(Number referenceDistance, Number maxDistance) { 
-	setReferenceDistance(referenceDistance);
-	setMaxDistance(maxDistance);
-}
-
-void Sound::setReferenceDistance(Number referenceDistance) {
-	this->referenceDistance = referenceDistance;
-}
-
-void Sound::setMaxDistance(Number maxDistance) {
-	this->maxDistance = maxDistance;
-}
-		
-Number Sound::getReferenceDistance() {
-	return referenceDistance;
-}
-
-Number Sound::getMaxDistance() {
-	return maxDistance;
-}
-
-void Sound::setIsPositional(bool isPositional) {
-	this->isPositional = isPositional;
-}
-
-void Sound::Stop() {
-	playing = false;
-}
-
-
-Number Sound::getSampleAsNumber(unsigned int offset, unsigned int channel, const Vector3 &position, const Quaternion &orientation) {
-	Number adjustedOffset = ((Number)offset) * pitch * frequencyAdjust;
-	Number ret;
-	if(isPositional) {
-		ret = (((Number)(soundBuffer[((((unsigned int )adjustedOffset)%numSamples)*numChannels)])/((Number)INT16_MAX))) * volume;
-		ret = modulateSampleForListener(ret, channel, position, orientation);
-	} else {
-		ret = (((Number)(soundBuffer[((((unsigned int )adjustedOffset)%numSamples)*numChannels)+(channel % numChannels)])/((Number)INT16_MAX))) * volume;
-	}
-	return ret;
-}
-
-Number Sound::modulateSampleForListener(Number sample, unsigned int channel, const Vector3 &position, const Quaternion &orientation) {
-	
-	// setup different channel configurations here
-	// if(STEREO) {
-	Vector3 earDirection;
-	if(channel) {
-		earDirection = Vector3(-1.0, 0.0, 0.0);
-	} else {
-		earDirection = Vector3(1.0, 0.0, 0.0);
-	}
-	earDirection = orientation.applyTo(earDirection);
-	
-	Vector3 dir = position - this->position;
-	dir.Normalize();
-	Number muliplier = earDirection.dot(dir);
-	if(muliplier < 0.0) {
-		muliplier = 0.0;
-	}
-	
-	Number ret = sample * (0.1 + (muliplier * 0.9)); // bleed 0.1 into the other ear
-	Number distance = position.distance(this->position);
-	Number attenuate = 0.5 * pow(referenceDistance/distance, 2.0);
-	
-	attenuate = MIN(attenuate, 1.0);
-	attenuate = MAX(attenuate, 0.0);
-	ret *= attenuate;
-	return ret;
-}
-
-bool Sound::loadBytes(const char *data, int size, int channels, unsigned int freq, SoundFormat format) {
-	
-	if(format == SoundFormatUnsupported) {
-		Logger::log("[%s] Error: sound format unsupported!\n", fileName.c_str());
-		return false;
-	}
-	
-	soundBuffer = (int16_t*) malloc(sizeof(int16_t) * channels * size);
-	
-	int16_t *soundBufferPtr = soundBuffer;
-	
-	unsigned int dataOffset = 0;
-	
-	switch(format) {
-		case SoundFormat8:
-			numSamples = size / channels;
-			break;
-		case SoundFormat16:
-			numSamples = size / channels / 2;
-			break;
-		case SoundFormat32:
-			numSamples = size / channels / 4;
-			break;
-		default:
-		break;
-	}
-	
-	for(int i=0; i < numSamples; i++){
-		for(int c=0; c < channels; c++) {
-			switch(format) {
-				case SoundFormat8:
-					*soundBufferPtr = ((int8_t*)data)[dataOffset];
-				break;
-				case SoundFormat16:
-					*soundBufferPtr = ((int16_t*)data)[dataOffset];
-				break;
-				case SoundFormat32:
-					*soundBufferPtr = ((int32_t*)data)[dataOffset];
-				break;
-				default:
-				break;
-			}
-			soundBufferPtr++;
-			dataOffset++;
-		}
-	}
-	
-	numChannels = channels;
-	frequency = freq;
-	
-	// adjust for different frequency
-	frequencyAdjust = (Number)freq/(Number)POLY_AUDIO_FREQ;
-	
-	return true;
-}
-
-unsigned int Sound::getFrequency() {
-	return frequency;
-}
-
-
-bool Sound::loadOGG(const String& fileName) {
-#ifndef NO_OGG
-
-	vector<char> data;
-	int bitStream;
-	long bytes;
-	char array[BUFFER_SIZE];
-	
-	CoreFile *f = Services()->getCore()->openFile(fileName.c_str(), "rb");
-	if(!f) {
-		Logger::log("Error loading OGG file!\n");
-		return false;
-	}
-	vorbis_info *pInfo;
-	OggVorbis_File oggFile; 
-	
-	ov_callbacks callbacks;
-	callbacks.read_func = custom_readfunc;
-	callbacks.seek_func = custom_seekfunc;
-	callbacks.close_func = custom_closefunc;
-	callbacks.tell_func = custom_tellfunc;
-	
-	ov_open_callbacks( (void*)f, &oggFile, NULL, 0, callbacks);
-	pInfo = ov_info(&oggFile, -1);
-
-	do {
-		bytes = ov_read(&oggFile, array, BUFFER_SIZE, 0, 2, 1, &bitStream);
-		data.insert(data.end(), array, array + bytes);
-	} while (bytes > 0);
-	
-	bool retVal = loadBytes(data.data(), data.size(), pInfo->channels, pInfo->rate, SoundFormat16);
-	ov_clear(&oggFile);
-	return retVal;
-#else
-	return false;
-#endif
-}
-
-bool Sound::loadWAV(const String& fileName) {
-	
-	long bytes;
-	vector <char> data;
-	
-	// Local resources
-	CoreFile *f = NULL;
-	char *array = NULL;
-	
-	// Open for binary reading
-	f = Services()->getCore()->openFile(fileName.c_str(), "rb");
-	if (!f) {
-		Logger::log("LoadWav: Could not load wav from " + fileName);
-		return false;
-	}
-	
-	// buffers
-	char magic[5];
-	magic[4] = '\0';
-	unsigned char data32[4];
-	unsigned char data16[2];
-	
-	// check magic
-	soundCheck(f->read(magic,4,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
-	soundCheck(String(magic) == "RIFF", "LoadWav: Wrong wav file format. This file is not a .wav file (no RIFF magic): "+ fileName );
-	
-	// skip 4 bytes (file size)
-	f->seek(4,SEEK_CUR);
-	
-	// check file format
-	soundCheck(f->read(magic,4,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
-	soundCheck(String(magic) == "WAVE", "LoadWav: Wrong wav file format. This file is not a .wav file (no WAVE format): "+ fileName );
-	
-	// check 'fmt ' sub chunk (1)
-	soundCheck(f->read(magic,4,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
-	soundCheck(String(magic) == "fmt ", "LoadWav: Wrong wav file format. This file is not a .wav file (no 'fmt ' subchunk): "+ fileName );
-	
-	// read (1)'s size
-	soundCheck(f->read(data32,4,1)	 == 1, "LoadWav: Cannot read wav file "+ fileName );
-	unsigned long subChunk1Size = readByte32(data32);
-	soundCheck(subChunk1Size >= 16, "Wrong wav file format. This file is not a .wav file ('fmt ' chunk too small, truncated file?): "+ fileName );
-	
-	// check PCM audio format
-	soundCheck(f->read(data16,2,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
-	unsigned short audioFormat = readByte16(data16);
-	soundCheck(audioFormat == 1, "LoadWav: Wrong wav file format. This file is not a .wav file (audio format is not PCM): "+ fileName );
-	
-	// read number of channels
-	soundCheck(f->read(data16,2,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
-	unsigned short channels = readByte16(data16);
-	
-	// read frequency (sample rate)
-	soundCheck(f->read(data32,4,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
-	unsigned long frequency = readByte32(data32);
-	
-	// skip 6 bytes (Byte rate (4), Block align (2))
-	f->seek(6,SEEK_CUR);
-	
-	// read bits per sample
-	soundCheck(f->read(data16,2,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
-	unsigned short bps = readByte16(data16);
-	
-	SoundFormat format = SoundFormatUnsupported;
-	
-	switch(bps) {
-		case 8:
-			format = SoundFormat8;
-		break;
-		case 16:
-			format = SoundFormat16;
-		break;
-		case 32:
-			format = SoundFormat32;
-		break;
-			
-	}
-	
-	// check 'data' sub chunk (2)
-	soundCheck(f->read(magic,4,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
-	soundCheck(String(magic) == "data", "LoadWav: Wrong wav file format. This file is not a .wav file (no data subchunk): "+ fileName );
-	
-	soundCheck(f->read(data32,4,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
-	unsigned long subChunk2Size = readByte32(data32);
-	
-	array = new char[BUFFER_SIZE];
-	
-	while (data.size() != subChunk2Size) {
-		// Read up to a buffer's worth of decoded sound data
-		bytes = f->read(array, 1, BUFFER_SIZE);
-		
-		if (bytes <= 0)
-			break;
-		
-		if (data.size() + bytes > subChunk2Size)
-			bytes = subChunk2Size - data.size();
-		
-		// Append to end of buffer
-		data.insert(data.end(), array, array + bytes);
-	};
-	
-	delete []array;
-	array = NULL;
-	
-	Services()->getCore()->closeFile(f);
-	f = NULL;
-
-	return loadBytes(&data[0], data.size(), channels, frequency, format);
-
-}
- 
-
-//NOAL_TODO
+/*
+ Copyright (C) 2011 by Ivan Safrin
+ 
+ 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 "polycode/core/PolySound.h"
+
+#define STB_VORBIS_HEADER_ONLY
+#include "stb_vorbis.h"
+
+#include "polycode/core/PolyString.h"
+#include "polycode/core/PolyLogger.h"
+#include "polycode/core/PolySoundManager.h"
+#include "polycode/core/PolyCore.h"
+#include "polycode/core/PolyCoreServices.h"
+#include <stdlib.h>
+#include <string>
+#include <vector>
+#include <stdint.h>
+#include <limits>
+
+#ifndef MAX_FLOAT
+	#define MAX_FLOAT (std::numeric_limits<double>::infinity())
+#endif
+
+#ifndef INT32_MAX
+	#define INT32_MAX (std::numeric_limits<int32_t>::max())
+#endif
+
+#ifndef INT16_MAX
+	#define INT16_MAX (std::numeric_limits<int16_t>::max())
+#endif
+
+using namespace std;
+using namespace Polycode;
+
+AudioStreamingSource::AudioStreamingSource(unsigned int channels, unsigned int freq) : channels(channels), freq(freq) {
+}
+
+unsigned int AudioStreamingSource::getNumChannels() {
+	return channels;
+}
+
+unsigned int AudioStreamingSource::getFrequency() {
+	return freq;
+}
+
+unsigned int AudioStreamingSource::streamData(int16_t *buffer, unsigned int size) {
+	return 0;
+}
+
+Sound::Sound(const String& fileName) :	referenceDistance(1), maxDistance(MAX_FLOAT), pitch(1), volume(1), numSamples(-1), streamingSound(false), playing(false), playbackOffset(0), streamingSource(NULL), frequencyAdjust(1.0) {
+	soundLoaded = false;
+	setIsPositional(false);
+	loadFile(fileName);
+
+    if(soundLoaded) {
+        Services()->getSoundManager()->registerSound(this);
+    }
+}
+
+Sound::Sound(int size, const char *data, int channels, unsigned int freq, SoundFormat format) : referenceDistance(1), maxDistance(MAX_FLOAT), pitch(1), volume(1), numSamples(-1), streamingSound(false), playing(false) , playbackOffset(0), streamingSource(NULL), frequencyAdjust(1.0) {
+	setIsPositional(false);
+	soundLoaded = loadBytes(data, size, channels, freq, format);
+	if(soundLoaded) {
+		Services()->getSoundManager()->registerSound(this);
+	}
+}
+
+Sound::Sound(AudioStreamingSource *streamingSource) : referenceDistance(1), maxDistance(MAX_FLOAT), pitch(1), volume(1),  numSamples(-1), streamingSound(true), streamingSource(streamingSource), playing(false), playbackOffset(0), frequencyAdjust(1.0) {
+
+	soundBuffer = (int16_t*) malloc(sizeof(int16_t) * streamingSource->getNumChannels() * POLY_MIX_BUFFER_SIZE);
+	Services()->getSoundManager()->registerSound(this);
+	numChannels = streamingSource->getNumChannels();
+}
+
+void Sound::updateStream(unsigned int streamCount) {
+	if(streamingSource) {
+		playbackOffset = 0;
+		numSamples = streamCount;
+		streamingSource->streamData(soundBuffer, streamCount);
+	}
+}
+
+void Sound::loadFile(String fileName) {
+
+	if(soundLoaded) {
+		free(soundBuffer);
+	}
+
+	String actualFilename = fileName;
+	CoreFile *test = Services()->getCore()->openFile(fileName, "rb");
+	if(!test) {
+		actualFilename = "default/default.wav";
+	} else {
+		Services()->getCore()->closeFile(test);
+	}
+	
+	String extension;
+	size_t found;
+	found=actualFilename.rfind(".");
+	if (found!=string::npos) {
+		extension = actualFilename.substr(found+1);
+	} else {
+		extension = "";
+	}
+	
+	if(extension == "wav" || extension == "WAV") {
+		soundLoaded = loadWAV(actualFilename);
+	} else if(extension == "ogg" || extension == "OGG") {
+		soundLoaded = loadOGG(actualFilename);
+	}
+	
+	this->fileName = actualFilename;
+}
+
+String Sound::getFileName() {
+	return fileName;
+}
+
+Number Sound::getVolume() {
+	return volume;
+}
+
+Number Sound::getPitch() {
+	return pitch;
+}
+
+Sound::~Sound() {
+	free(soundBuffer);
+	Services()->getSoundManager()->unregisterSound(this);
+}
+
+void Sound::soundCheck(bool result, const String& err) {
+	if(!result)
+		Logger::log(err);
+}
+
+unsigned long Sound::readByte32(const unsigned char data[4]) {
+#if TAU_BIG_ENDIAN
+	return (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
+#else
+	return (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
+#endif
+}
+
+unsigned short Sound::readByte16(const unsigned char data[2]) {
+#if TAU_BIG_ENDIAN
+	return (data[0] << 8) + data[1];
+#else
+	return (data[1] << 8) + data[0];
+#endif	
+}
+
+void Sound::Play(bool loop, bool restartSound) {
+	if(restartSound) {
+		playbackOffset = 0;
+	}
+	playing = true;
+	looped = loop;
+}
+
+bool Sound::isPlaying() {
+	return playing;
+}
+
+bool Sound::isLooped() {
+	return looped;
+}
+
+
+void Sound::setVolume(Number newVolume) {
+	this->volume = newVolume;
+}
+
+void Sound::setPitch(Number newPitch) {
+	this->pitch = newPitch;
+}
+
+void Sound::setSoundPosition(const Vector3 &position) {
+	this->position = position;
+}
+
+void Sound::setSoundVelocity(const Vector3 &velocity) {
+	this->velocity = velocity;
+}
+
+void Sound::setSoundDirection(const Vector3 &direction) {
+	this->direction = direction;
+}
+
+
+Number Sound::getPlaybackTime() {
+	/*
+	float result = 0.0;
+	alGetSourcef(soundSource, AL_SEC_OFFSET, &result);
+	return result;
+	 */
+		//NOAL_TODO
+	return 0.0;
+}
+
+Number Sound::getPlaybackDuration() {
+	/*
+	ALint sizeInBytes;
+	ALint channels;
+	ALint bits;
+	ALint bufferID;
+	alGetSourcei(soundSource, AL_BUFFER, &bufferID);
+	
+	alGetBufferi(bufferID, AL_SIZE, &sizeInBytes);
+	alGetBufferi(bufferID, AL_CHANNELS, &channels);
+	alGetBufferi(bufferID, AL_BITS, &bits);
+
+	int lengthInSamples = sizeInBytes * 8 / (channels * bits);
+
+	ALint frequency;
+	alGetBufferi(bufferID, AL_FREQUENCY, &frequency);
+	Number durationInSeconds = (float)lengthInSamples / (float)frequency;
+	
+	return durationInSeconds;
+	 */
+		//NOAL_TODO
+	return 0.0;
+}
+		
+int Sound::getOffset() {
+	return playbackOffset;
+}
+
+void Sound::setOffset(unsigned int offset) {
+	playbackOffset = (offset);
+	
+	Number adjustedOffset = ((Number)playbackOffset) * pitch * frequencyAdjust;
+	
+	if((unsigned int)adjustedOffset >= numSamples) {
+		playbackOffset = 0;
+		if(!looped && !streamingSource) {
+			playing = false;
+		}
+	}
+}
+
+void Sound::seekTo(Number time) {
+	/*
+	if(time > getPlaybackDuration())
+		return;
+	alSourcef(soundSource, AL_SEC_OFFSET, time);
+	checkALError("Seek");
+	 */
+			//NOAL_TODO
+}
+
+int Sound::getSampleLength() {
+	return numSamples;
+}
+
+void Sound::setPositionalProperties(Number referenceDistance, Number maxDistance) { 
+	setReferenceDistance(referenceDistance);
+	setMaxDistance(maxDistance);
+}
+
+void Sound::setReferenceDistance(Number referenceDistance) {
+	this->referenceDistance = referenceDistance;
+}
+
+void Sound::setMaxDistance(Number maxDistance) {
+	this->maxDistance = maxDistance;
+}
+		
+Number Sound::getReferenceDistance() {
+	return referenceDistance;
+}
+
+Number Sound::getMaxDistance() {
+	return maxDistance;
+}
+
+void Sound::setIsPositional(bool isPositional) {
+	this->isPositional = isPositional;
+}
+
+void Sound::Stop() {
+	playing = false;
+}
+
+
+Number Sound::getSampleAsNumber(unsigned int offset, unsigned int channel, const Vector3 &position, const Quaternion &orientation) {
+	Number adjustedOffset = ((Number)offset) * pitch * frequencyAdjust;
+	Number ret;
+	if(isPositional) {
+		ret = (((Number)(soundBuffer[((((unsigned int )adjustedOffset)%numSamples)*numChannels)])/((Number)INT16_MAX))) * volume;
+		ret = modulateSampleForListener(ret, channel, position, orientation);
+	} else {
+		ret = (((Number)(soundBuffer[((((unsigned int )adjustedOffset)%numSamples)*numChannels)+(channel % numChannels)])/((Number)INT16_MAX))) * volume;
+	}
+	return ret;
+}
+
+Number Sound::modulateSampleForListener(Number sample, unsigned int channel, const Vector3 &position, const Quaternion &orientation) {
+	
+	// setup different channel configurations here
+	// if(STEREO) {
+	Vector3 earDirection;
+	if(channel) {
+		earDirection = Vector3(-1.0, 0.0, 0.0);
+	} else {
+		earDirection = Vector3(1.0, 0.0, 0.0);
+	}
+	earDirection = orientation.applyTo(earDirection);
+	
+	Vector3 dir = position - this->position;
+	dir.Normalize();
+	Number muliplier = earDirection.dot(dir);
+	if(muliplier < 0.0) {
+		muliplier = 0.0;
+	}
+	
+	Number ret = sample * (0.1 + (muliplier * 0.9)); // bleed 0.1 into the other ear
+	Number distance = position.distance(this->position);
+	Number attenuate = 0.5 * pow(referenceDistance/distance, 2.0);
+	
+	attenuate = MIN(attenuate, 1.0);
+	attenuate = MAX(attenuate, 0.0);
+	ret *= attenuate;
+	return ret;
+}
+
+bool Sound::loadBytes(const char *data, int size, int channels, unsigned int freq, SoundFormat format) {
+	
+	if(format == SoundFormatUnsupported) {
+		Logger::log("[%s] Error: sound format unsupported!\n", fileName.c_str());
+		return false;
+	}
+	
+	soundBuffer = (int16_t*) malloc(sizeof(int16_t) * channels * size);
+	
+	int16_t *soundBufferPtr = soundBuffer;
+	
+	unsigned int dataOffset = 0;
+	
+	switch(format) {
+		case SoundFormat8:
+			numSamples = size / channels;
+			break;
+		case SoundFormat16:
+			numSamples = size / channels / 2;
+			break;
+		case SoundFormat32:
+			numSamples = size / channels / 4;
+			break;
+		default:
+		break;
+	}
+	
+	for(int i=0; i < numSamples; i++){
+		for(int c=0; c < channels; c++) {
+			switch(format) {
+				case SoundFormat8:
+					*soundBufferPtr = ((int8_t*)data)[dataOffset];
+				break;
+				case SoundFormat16:
+					*soundBufferPtr = ((int16_t*)data)[dataOffset];
+				break;
+				case SoundFormat32:
+					*soundBufferPtr = ((int32_t*)data)[dataOffset];
+				break;
+				default:
+				break;
+			}
+			soundBufferPtr++;
+			dataOffset++;
+		}
+	}
+	
+	numChannels = channels;
+	frequency = freq;
+	
+	// adjust for different frequency
+	frequencyAdjust = (Number)freq/(Number)POLY_AUDIO_FREQ;
+	
+	return true;
+}
+
+unsigned int Sound::getFrequency() {
+	return frequency;
+}
+
+
+bool Sound::loadOGG(const String& fileName) {
+	CoreFile *f = Services()->getCore()->openFile(fileName.c_str(), "rb");
+	if (!f) {
+		Logger::log("Error loading OGG file!\n");
+		return false;
+	}
+	Services()->getCore()->closeFile(f);
+
+	short *decoded;
+	int channels, len, sample_rate;
+	len = stb_vorbis_decode_filename(fileName.c_str(), &channels, &sample_rate, &decoded);
+	if (len <= 0) {
+		return false;
+	}
+
+	numChannels = channels;
+	numSamples = len;
+	soundBuffer = decoded;
+	frequency = sample_rate;
+	frequencyAdjust = sample_rate / POLY_AUDIO_FREQ;
+
+	return true;
+}
+
+bool Sound::loadWAV(const String& fileName) {
+	
+	long bytes;
+	vector <char> data;
+	
+	// Local resources
+	CoreFile *f = NULL;
+	char *array = NULL;
+	
+	// Open for binary reading
+	f = Services()->getCore()->openFile(fileName.c_str(), "rb");
+	if (!f) {
+		Logger::log("LoadWav: Could not load wav from " + fileName);
+		return false;
+	}
+	
+	// buffers
+	char magic[5];
+	magic[4] = '\0';
+	unsigned char data32[4];
+	unsigned char data16[2];
+	
+	// check magic
+	soundCheck(f->read(magic,4,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
+	soundCheck(String(magic) == "RIFF", "LoadWav: Wrong wav file format. This file is not a .wav file (no RIFF magic): "+ fileName );
+	
+	// skip 4 bytes (file size)
+	f->seek(4,SEEK_CUR);
+	
+	// check file format
+	soundCheck(f->read(magic,4,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
+	soundCheck(String(magic) == "WAVE", "LoadWav: Wrong wav file format. This file is not a .wav file (no WAVE format): "+ fileName );
+	
+	// check 'fmt ' sub chunk (1)
+	soundCheck(f->read(magic,4,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
+	soundCheck(String(magic) == "fmt ", "LoadWav: Wrong wav file format. This file is not a .wav file (no 'fmt ' subchunk): "+ fileName );
+	
+	// read (1)'s size
+	soundCheck(f->read(data32,4,1)	 == 1, "LoadWav: Cannot read wav file "+ fileName );
+	unsigned long subChunk1Size = readByte32(data32);
+	soundCheck(subChunk1Size >= 16, "Wrong wav file format. This file is not a .wav file ('fmt ' chunk too small, truncated file?): "+ fileName );
+	
+	// check PCM audio format
+	soundCheck(f->read(data16,2,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
+	unsigned short audioFormat = readByte16(data16);
+	soundCheck(audioFormat == 1, "LoadWav: Wrong wav file format. This file is not a .wav file (audio format is not PCM): "+ fileName );
+	
+	// read number of channels
+	soundCheck(f->read(data16,2,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
+	unsigned short channels = readByte16(data16);
+	
+	// read frequency (sample rate)
+	soundCheck(f->read(data32,4,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
+	unsigned long frequency = readByte32(data32);
+	
+	// skip 6 bytes (Byte rate (4), Block align (2))
+	f->seek(6,SEEK_CUR);
+	
+	// read bits per sample
+	soundCheck(f->read(data16,2,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
+	unsigned short bps = readByte16(data16);
+	
+	SoundFormat format = SoundFormatUnsupported;
+	
+	switch(bps) {
+		case 8:
+			format = SoundFormat8;
+		break;
+		case 16:
+			format = SoundFormat16;
+		break;
+		case 32:
+			format = SoundFormat32;
+		break;
+			
+	}
+	
+	// check 'data' sub chunk (2)
+	soundCheck(f->read(magic,4,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
+	soundCheck(String(magic) == "data", "LoadWav: Wrong wav file format. This file is not a .wav file (no data subchunk): "+ fileName );
+	
+	soundCheck(f->read(data32,4,1) == 1, "LoadWav: Cannot read wav file "+ fileName );
+	unsigned long subChunk2Size = readByte32(data32);
+	
+	array = new char[BUFFER_SIZE];
+	
+	while (data.size() != subChunk2Size) {
+		// Read up to a buffer's worth of decoded sound data
+		bytes = f->read(array, 1, BUFFER_SIZE);
+		
+		if (bytes <= 0)
+			break;
+		
+		if (data.size() + bytes > subChunk2Size)
+			bytes = subChunk2Size - data.size();
+		
+		// Append to end of buffer
+		data.insert(data.end(), array, array + bytes);
+	};
+	
+	delete []array;
+	array = NULL;
+	
+	Services()->getCore()->closeFile(f);
+	f = NULL;
+
+	return loadBytes(&data[0], data.size(), channels, frequency, format);
+
+}
+ 
+
+//NOAL_TODO

+ 11 - 6
src/core/PolyString.cpp

@@ -24,6 +24,9 @@
 #include "polycode/core/PolyGlobals.h"
 #include <iomanip>
 #include <sstream>
+#ifdef _WINDOWS
+	#include <ctype.h>
+#endif
 
 using namespace Polycode;
 using namespace std;
@@ -113,15 +116,17 @@ void String::setDataWithEncoding(char *data, int encoding) {
 	}
 }
 
-bool String::isNumber() {
-#if PLATFORM == PLATFORM_WINDOWS
+bool String::isNumber(){
+	if (contents.find_first_not_of("0123456789.+-") == string::npos)
+		return true;
 	return false;
-#else
+}
+
+bool String::isInteger() {
 	std::string::const_iterator it = contents.begin();
-	while (it != contents.end() && std::isdigit(*it)) ++it;
+	while (it != contents.end() && (isdigit(*it) || *it == '+' || *it == '-')) ++it;
 	return !contents.empty() && it == contents.end();
-#endif
-}				
+}
 
 
 

+ 14 - 8
src/core/PolyTexture.cpp

@@ -43,16 +43,18 @@ Texture::Texture(unsigned int width, unsigned int height, char *textureData,bool
 	
 	switch(type) {
 		case Image::IMAGE_RGB:
-			pixelSize = 3;			
+			pixelSize = 3;
 			break;
 		case Image::IMAGE_RGBA:
-			pixelSize = 4;						
+			pixelSize = 4;
 		break;
-		case Image::IMAGE_FP16:		
+#ifndef NO_FP16
+		case Image::IMAGE_FP16:
 			pixelSize = 6;
 		break;
+#endif
 		default:
-			pixelSize = 4;								
+			pixelSize = 4;
 		break;
 	}
 	
@@ -94,16 +96,18 @@ void Texture::setImageData(Image *data) {
 
 	switch (data->getType()) {
 		case Image::IMAGE_RGB:
-			pixelSize = 3;			
+			pixelSize = 3;
 		break;
 		case Image::IMAGE_RGBA:
-			pixelSize = 4;						
+			pixelSize = 4;
 		break;
-		case Image::IMAGE_FP16:		
+#ifndef NO_FP16
+		case Image::IMAGE_FP16:
 			pixelSize = 6;
 		break;
+#endif
 		default:
-			pixelSize = 4;								
+			pixelSize = 4;
 		break;
 	}
 
@@ -159,9 +163,11 @@ Texture::Texture(Image *image, bool clamp, bool createMipmaps) : Resource(Resour
 RenderBuffer::RenderBuffer(unsigned int width, unsigned int height, bool attachDepthBuffer, bool floatingPoint) : platformData(NULL), width(width), height(height), depthBufferPlatformData(NULL), floatingPoint(floatingPoint) {
 	
 	int imageType = Image::IMAGE_RGBA;
+#ifndef NO_FP16
 	if(floatingPoint) {
 		imageType = Image::IMAGE_FP16;
 	}
+#endif
 	colorTexture = std::make_shared<Texture>(width, height, nullptr, false, false, imageType, true);
 	if(attachDepthBuffer) {
 		depthTexture = std::make_shared<Texture>(width, height, nullptr, false, false, 1, true);

+ 27 - 7
src/core/PolyWinCore.cpp

@@ -334,6 +334,12 @@ void Win32Core::setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, in
 	}
 	else {
 
+		bool menu;
+		if (GetMenu(hWnd))
+			menu = true;
+		else
+			menu = false;
+
 		RECT rect;
 		rect.left = 0;
 		rect.top = 0;
@@ -341,11 +347,11 @@ void Win32Core::setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, in
 		rect.bottom = yRes;
 		if (resizable){
 			SetWindowLongPtr(hWnd, GWL_STYLE, WS_OVERLAPPEDWINDOW | WS_SYSMENU | WS_VISIBLE);
-			AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW | WS_SYSMENU, FALSE);
+			AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW | WS_SYSMENU, menu);
 		}
 		else {
 			SetWindowLongPtr(hWnd, GWL_STYLE, WS_CAPTION | WS_POPUP | WS_SYSMENU | WS_VISIBLE);
-			AdjustWindowRect(&rect, WS_CAPTION | WS_POPUP | WS_SYSMENU, FALSE);
+			AdjustWindowRect(&rect, WS_CAPTION | WS_POPUP | WS_SYSMENU, menu);
 		}
 		MoveWindow(hWnd, 0, 0, rect.right-rect.left, rect.bottom-rect.top, TRUE);
 
@@ -653,13 +659,12 @@ PolyKEY Win32Core::mapKey(LPARAM lParam, WPARAM wParam) {
 	return keyMap[(unsigned int)wParam];
 }
 
-void Win32Core::handleKeyDown(LPARAM lParam, WPARAM wParam, wchar_t unicodeChar) {
+void Win32Core::handleKeyDown(LPARAM lParam, WPARAM wParam) {
 	lockMutex(eventMutex);
 	Win32Event newEvent;
 	newEvent.eventGroup = Win32Event::INPUT_EVENT;
 	newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
 	newEvent.keyCode = mapKey(lParam, wParam);
-	newEvent.unicodeChar = unicodeChar;
 	win32Events.push_back(newEvent);
 	unlockMutex(eventMutex);
 }
@@ -670,7 +675,6 @@ void Win32Core::handleKeyUp(LPARAM lParam, WPARAM wParam) {
 	newEvent.eventGroup = Win32Event::INPUT_EVENT;
 	newEvent.eventCode = InputEvent::EVENT_KEYUP;
 	newEvent.keyCode = mapKey(lParam, wParam);
-	newEvent.unicodeChar = 0;
 	win32Events.push_back(newEvent);
 	unlockMutex(eventMutex);
 }
@@ -860,6 +864,19 @@ void Win32Core::handleMouseUp(int mouseCode,LPARAM lParam, WPARAM wParam) {
 	unlockMutex(eventMutex);
 }
 
+void Win32Core::handleTextInput(LPARAM lParam, WPARAM wParam) {
+	Win32Event newEvent;
+	newEvent.eventGroup = Win32Event::INPUT_EVENT;
+	newEvent.text = String(wParam);
+	newEvent.eventCode = InputEvent::EVENT_TEXTINPUT;
+	if ((unsigned char) newEvent.text[0] < ' ' || newEvent.text[0] == 127) {
+		return;
+	}
+	lockMutex(eventMutex);
+	win32Events.push_back(newEvent);
+	unlockMutex(eventMutex);
+}
+
 bool Win32Core::checkSpecialKeyEvents(PolyKEY key) {
 	
 	if(key == KEY_a && (input->getKeyState(KEY_LCTRL) || input->getKeyState(KEY_RCTRL))) {
@@ -932,11 +949,14 @@ void Win32Core::checkEvents() {
 					break;	
 					case InputEvent::EVENT_KEYDOWN:
 						if(!checkSpecialKeyEvents((event.keyCode))) {
-							input->setKeyState(event.keyCode, (char)event.unicodeChar, true, getTicks());
+							input->setKeyState(event.keyCode, true, getTicks());
 						}
 					break;
 					case InputEvent::EVENT_KEYUP:
-						input->setKeyState(event.keyCode, (char)event.unicodeChar, false, getTicks());
+						input->setKeyState(event.keyCode, false, getTicks());
+					break;
+					case InputEvent::EVENT_TEXTINPUT:
+						input->textInput(event.text);
 					break;
 				}
 			break;

+ 5269 - 0
src/core/stb_vorbis.cpp

@@ -0,0 +1,5269 @@
+// Ogg Vorbis audio decoder - v1.09 - public domain
+// http://nothings.org/stb_vorbis/
+//
+// Original version written by Sean Barrett in 2007.
+//
+// Originally sponsored by RAD Game Tools. Seeking sponsored
+// by Phillip Bennefall, Marc Andersen, Aaron Baker, Elias Software,
+// Aras Pranckevicius, and Sean Barrett.
+//
+// LICENSE
+//
+//   This software is dual-licensed to the public domain and under the following
+//   license: you are granted a perpetual, irrevocable license to copy, modify,
+//   publish, and distribute this file as you see fit.
+//
+// No warranty for any purpose is expressed or implied by the author (nor
+// by RAD Game Tools). Report bugs and send enhancements to the author.
+//
+// Limitations:
+//
+//   - floor 0 not supported (used in old ogg vorbis files pre-2004)
+//   - lossless sample-truncation at beginning ignored
+//   - cannot concatenate multiple vorbis streams
+//   - sample positions are 32-bit, limiting seekable 192Khz
+//       files to around 6 hours (Ogg supports 64-bit)
+//
+// Feature contributors:
+//    Dougall Johnson (sample-exact seeking)
+//
+// Bugfix/warning contributors:
+//    Terje Mathisen     Niklas Frykholm     Andy Hill
+//    Casey Muratori     John Bolton         Gargaj
+//    Laurent Gomila     Marc LeBlanc        Ronny Chevalier
+//    Bernhard Wodo      Evan Balster        alxprd@github
+//    Tom Beaumont       Ingo Leitgeb        Nicolas Guillemot
+//    Phillip Bennefall  Rohit               Thiago Goulart
+//    manxorist@github   saga musix
+//
+// Partial history:
+//    1.09    - 2016/04/04 - back out 'truncation of last frame' fix from previous version
+//    1.08    - 2016/04/02 - warnings; setup memory leaks; truncation of last frame
+//    1.07    - 2015/01/16 - fixes for crashes on invalid files; warning fixes; const
+//    1.06    - 2015/08/31 - full, correct support for seeking API (Dougall Johnson)
+//                           some crash fixes when out of memory or with corrupt files
+//                           fix some inappropriately signed shifts
+//    1.05    - 2015/04/19 - don't define __forceinline if it's redundant
+//    1.04    - 2014/08/27 - fix missing const-correct case in API
+//    1.03    - 2014/08/07 - warning fixes
+//    1.02    - 2014/07/09 - declare qsort comparison as explicitly _cdecl in Windows
+//    1.01    - 2014/06/18 - fix stb_vorbis_get_samples_float (interleaved was correct)
+//    1.0     - 2014/05/26 - fix memory leaks; fix warnings; fix bugs in >2-channel;
+//                           (API change) report sample rate for decode-full-file funcs
+//
+// See end of file for full version history.
+#define STB_VORBIS_HEADER_ONLY
+#include "stb_vorbis.h"
+#undef STB_VORBIS_HEADER_ONLY
+//////////////////////////////////////////////////////////////////////////////
+//
+//  HEADER BEGINS HERE
+//
+
+#ifndef STB_VORBIS_INCLUDE_STB_VORBIS_H
+#define STB_VORBIS_INCLUDE_STB_VORBIS_H
+
+#if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)
+#define STB_VORBIS_NO_STDIO 1
+#endif
+
+#ifndef STB_VORBIS_NO_STDIO
+#include <stdio.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+	///////////   THREAD SAFETY
+
+	// Individual stb_vorbis* handles are not thread-safe; you cannot decode from
+	// them from multiple threads at the same time. However, you can have multiple
+	// stb_vorbis* handles and decode from them independently in multiple thrads.
+
+
+	///////////   MEMORY ALLOCATION
+
+	// normally stb_vorbis uses malloc() to allocate memory at startup,
+	// and alloca() to allocate temporary memory during a frame on the
+	// stack. (Memory consumption will depend on the amount of setup
+	// data in the file and how you set the compile flags for speed
+	// vs. size. In my test files the maximal-size usage is ~150KB.)
+	//
+	// You can modify the wrapper functions in the source (setup_malloc,
+	// setup_temp_malloc, temp_malloc) to change this behavior, or you
+	// can use a simpler allocation model: you pass in a buffer from
+	// which stb_vorbis will allocate _all_ its memory (including the
+	// temp memory). "open" may fail with a VORBIS_outofmem if you
+	// do not pass in enough data; there is no way to determine how
+	// much you do need except to succeed (at which point you can
+	// query get_info to find the exact amount required. yes I know
+	// this is lame).
+	//
+	// If you pass in a non-NULL buffer of the type below, allocation
+	// will occur from it as described above. Otherwise just pass NULL
+	// to use malloc()/alloca()
+
+	typedef struct {
+		char *alloc_buffer;
+		int   alloc_buffer_length_in_bytes;
+	} stb_vorbis_alloc;
+
+
+	///////////   FUNCTIONS USEABLE WITH ALL INPUT MODES
+
+	typedef struct stb_vorbis stb_vorbis;
+
+	typedef struct {
+		unsigned int sample_rate;
+		int channels;
+
+		unsigned int setup_memory_required;
+		unsigned int setup_temp_memory_required;
+		unsigned int temp_memory_required;
+
+		int max_frame_size;
+	} stb_vorbis_info;
+
+	// get general information about the file
+	extern stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f);
+
+	// get the last error detected (clears it, too)
+	extern int stb_vorbis_get_error(stb_vorbis *f);
+
+	// close an ogg vorbis file and free all memory in use
+	extern void stb_vorbis_close(stb_vorbis *f);
+
+	// this function returns the offset (in samples) from the beginning of the
+	// file that will be returned by the next decode, if it is known, or -1
+	// otherwise. after a flush_pushdata() call, this may take a while before
+	// it becomes valid again.
+	// NOT WORKING YET after a seek with PULLDATA API
+	extern int stb_vorbis_get_sample_offset(stb_vorbis *f);
+
+	// returns the current seek point within the file, or offset from the beginning
+	// of the memory buffer. In pushdata mode it returns 0.
+	extern unsigned int stb_vorbis_get_file_offset(stb_vorbis *f);
+
+	///////////   PUSHDATA API
+
+#ifndef STB_VORBIS_NO_PUSHDATA_API
+
+	// this API allows you to get blocks of data from any source and hand
+	// them to stb_vorbis. you have to buffer them; stb_vorbis will tell
+	// you how much it used, and you have to give it the rest next time;
+	// and stb_vorbis may not have enough data to work with and you will
+	// need to give it the same data again PLUS more. Note that the Vorbis
+	// specification does not bound the size of an individual frame.
+
+	extern stb_vorbis *stb_vorbis_open_pushdata(
+		const unsigned char * datablock, int datablock_length_in_bytes,
+		int *datablock_memory_consumed_in_bytes,
+		int *error,
+		const stb_vorbis_alloc *alloc_buffer);
+	// create a vorbis decoder by passing in the initial data block containing
+	//    the ogg&vorbis headers (you don't need to do parse them, just provide
+	//    the first N bytes of the file--you're told if it's not enough, see below)
+	// on success, returns an stb_vorbis *, does not set error, returns the amount of
+	//    data parsed/consumed on this call in *datablock_memory_consumed_in_bytes;
+	// on failure, returns NULL on error and sets *error, does not change *datablock_memory_consumed
+	// if returns NULL and *error is VORBIS_need_more_data, then the input block was
+	//       incomplete and you need to pass in a larger block from the start of the file
+
+	extern int stb_vorbis_decode_frame_pushdata(
+		stb_vorbis *f,
+		const unsigned char *datablock, int datablock_length_in_bytes,
+		int *channels,             // place to write number of float * buffers
+		float ***output,           // place to write float ** array of float * buffers
+		int *samples               // place to write number of output samples
+	);
+	// decode a frame of audio sample data if possible from the passed-in data block
+	//
+	// return value: number of bytes we used from datablock
+	//
+	// possible cases:
+	//     0 bytes used, 0 samples output (need more data)
+	//     N bytes used, 0 samples output (resynching the stream, keep going)
+	//     N bytes used, M samples output (one frame of data)
+	// note that after opening a file, you will ALWAYS get one N-bytes,0-sample
+	// frame, because Vorbis always "discards" the first frame.
+	//
+	// Note that on resynch, stb_vorbis will rarely consume all of the buffer,
+	// instead only datablock_length_in_bytes-3 or less. This is because it wants
+	// to avoid missing parts of a page header if they cross a datablock boundary,
+	// without writing state-machiney code to record a partial detection.
+	//
+	// The number of channels returned are stored in *channels (which can be
+	// NULL--it is always the same as the number of channels reported by
+	// get_info). *output will contain an array of float* buffers, one per
+	// channel. In other words, (*output)[0][0] contains the first sample from
+	// the first channel, and (*output)[1][0] contains the first sample from
+	// the second channel.
+
+	extern void stb_vorbis_flush_pushdata(stb_vorbis *f);
+	// inform stb_vorbis that your next datablock will not be contiguous with
+	// previous ones (e.g. you've seeked in the data); future attempts to decode
+	// frames will cause stb_vorbis to resynchronize (as noted above), and
+	// once it sees a valid Ogg page (typically 4-8KB, as large as 64KB), it
+	// will begin decoding the _next_ frame.
+	//
+	// if you want to seek using pushdata, you need to seek in your file, then
+	// call stb_vorbis_flush_pushdata(), then start calling decoding, then once
+	// decoding is returning you data, call stb_vorbis_get_sample_offset, and
+	// if you don't like the result, seek your file again and repeat.
+#endif
+
+
+	//////////   PULLING INPUT API
+
+#ifndef STB_VORBIS_NO_PULLDATA_API
+	// This API assumes stb_vorbis is allowed to pull data from a source--
+	// either a block of memory containing the _entire_ vorbis stream, or a
+	// FILE * that you or it create, or possibly some other reading mechanism
+	// if you go modify the source to replace the FILE * case with some kind
+	// of callback to your code. (But if you don't support seeking, you may
+	// just want to go ahead and use pushdata.)
+
+#if !defined(STB_VORBIS_NO_STDIO) && !defined(STB_VORBIS_NO_INTEGER_CONVERSION)
+	extern int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output);
+#endif
+#if !defined(STB_VORBIS_NO_INTEGER_CONVERSION)
+	extern int stb_vorbis_decode_memory(const unsigned char *mem, int len, int *channels, int *sample_rate, short **output);
+#endif
+	// decode an entire file and output the data interleaved into a malloc()ed
+	// buffer stored in *output. The return value is the number of samples
+	// decoded, or -1 if the file could not be opened or was not an ogg vorbis file.
+	// When you're done with it, just free() the pointer returned in *output.
+
+	extern stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len,
+		int *error, const stb_vorbis_alloc *alloc_buffer);
+	// create an ogg vorbis decoder from an ogg vorbis stream in memory (note
+	// this must be the entire stream!). on failure, returns NULL and sets *error
+
+#ifndef STB_VORBIS_NO_STDIO
+	extern stb_vorbis * stb_vorbis_open_filename(const char *filename,
+		int *error, const stb_vorbis_alloc *alloc_buffer);
+	// create an ogg vorbis decoder from a filename via fopen(). on failure,
+	// returns NULL and sets *error (possibly to VORBIS_file_open_failure).
+
+	extern stb_vorbis * stb_vorbis_open_file(FILE *f, int close_handle_on_close,
+		int *error, const stb_vorbis_alloc *alloc_buffer);
+	// create an ogg vorbis decoder from an open FILE *, looking for a stream at
+	// the _current_ seek point (ftell). on failure, returns NULL and sets *error.
+	// note that stb_vorbis must "own" this stream; if you seek it in between
+	// calls to stb_vorbis, it will become confused. Morever, if you attempt to
+	// perform stb_vorbis_seek_*() operations on this file, it will assume it
+	// owns the _entire_ rest of the file after the start point. Use the next
+	// function, stb_vorbis_open_file_section(), to limit it.
+
+	extern stb_vorbis * stb_vorbis_open_file_section(FILE *f, int close_handle_on_close,
+		int *error, const stb_vorbis_alloc *alloc_buffer, unsigned int len);
+	// create an ogg vorbis decoder from an open FILE *, looking for a stream at
+	// the _current_ seek point (ftell); the stream will be of length 'len' bytes.
+	// on failure, returns NULL and sets *error. note that stb_vorbis must "own"
+	// this stream; if you seek it in between calls to stb_vorbis, it will become
+	// confused.
+#endif
+
+	extern int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number);
+	extern int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number);
+	// these functions seek in the Vorbis file to (approximately) 'sample_number'.
+	// after calling seek_frame(), the next call to get_frame_*() will include
+	// the specified sample. after calling stb_vorbis_seek(), the next call to
+	// stb_vorbis_get_samples_* will start with the specified sample. If you
+	// do not need to seek to EXACTLY the target sample when using get_samples_*,
+	// you can also use seek_frame().
+
+	extern void stb_vorbis_seek_start(stb_vorbis *f);
+	// this function is equivalent to stb_vorbis_seek(f,0)
+
+	extern unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f);
+	extern float        stb_vorbis_stream_length_in_seconds(stb_vorbis *f);
+	// these functions return the total length of the vorbis stream
+
+	extern int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output);
+	// decode the next frame and return the number of samples. the number of
+	// channels returned are stored in *channels (which can be NULL--it is always
+	// the same as the number of channels reported by get_info). *output will
+	// contain an array of float* buffers, one per channel. These outputs will
+	// be overwritten on the next call to stb_vorbis_get_frame_*.
+	//
+	// You generally should not intermix calls to stb_vorbis_get_frame_*()
+	// and stb_vorbis_get_samples_*(), since the latter calls the former.
+
+#ifndef STB_VORBIS_NO_INTEGER_CONVERSION
+	extern int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts);
+	extern int stb_vorbis_get_frame_short(stb_vorbis *f, int num_c, short **buffer, int num_samples);
+#endif
+	// decode the next frame and return the number of *samples* per channel.
+	// Note that for interleaved data, you pass in the number of shorts (the
+	// size of your array), but the return value is the number of samples per
+	// channel, not the total number of samples.
+	//
+	// The data is coerced to the number of channels you request according to the
+	// channel coercion rules (see below). You must pass in the size of your
+	// buffer(s) so that stb_vorbis will not overwrite the end of the buffer.
+	// The maximum buffer size needed can be gotten from get_info(); however,
+	// the Vorbis I specification implies an absolute maximum of 4096 samples
+	// per channel.
+
+	// Channel coercion rules:
+	//    Let M be the number of channels requested, and N the number of channels present,
+	//    and Cn be the nth channel; let stereo L be the sum of all L and center channels,
+	//    and stereo R be the sum of all R and center channels (channel assignment from the
+	//    vorbis spec).
+	//        M    N       output
+	//        1    k      sum(Ck) for all k
+	//        2    *      stereo L, stereo R
+	//        k    l      k > l, the first l channels, then 0s
+	//        k    l      k <= l, the first k channels
+	//    Note that this is not _good_ surround etc. mixing at all! It's just so
+	//    you get something useful.
+
+	extern int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats);
+	extern int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples);
+	// gets num_samples samples, not necessarily on a frame boundary--this requires
+	// buffering so you have to supply the buffers. DOES NOT APPLY THE COERCION RULES.
+	// Returns the number of samples stored per channel; it may be less than requested
+	// at the end of the file. If there are no more samples in the file, returns 0.
+
+#ifndef STB_VORBIS_NO_INTEGER_CONVERSION
+	extern int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts);
+	extern int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int num_samples);
+#endif
+	// gets num_samples samples, not necessarily on a frame boundary--this requires
+	// buffering so you have to supply the buffers. Applies the coercion rules above
+	// to produce 'channels' channels. Returns the number of samples stored per channel;
+	// it may be less than requested at the end of the file. If there are no more
+	// samples in the file, returns 0.
+
+#endif
+
+	////////   ERROR CODES
+
+	enum STBVorbisError {
+		VORBIS__no_error,
+
+		VORBIS_need_more_data = 1,             // not a real error
+
+		VORBIS_invalid_api_mixing,           // can't mix API modes
+		VORBIS_outofmem,                     // not enough memory
+		VORBIS_feature_not_supported,        // uses floor 0
+		VORBIS_too_many_channels,            // STB_VORBIS_MAX_CHANNELS is too small
+		VORBIS_file_open_failure,            // fopen() failed
+		VORBIS_seek_without_length,          // can't seek in unknown-length file
+
+		VORBIS_unexpected_eof = 10,            // file is truncated?
+		VORBIS_seek_invalid,                 // seek past EOF
+
+											 // decoding errors (corrupt/invalid stream) -- you probably
+											 // don't care about the exact details of these
+
+											 // vorbis errors:
+											 VORBIS_invalid_setup = 20,
+											 VORBIS_invalid_stream,
+
+											 // ogg errors:
+											 VORBIS_missing_capture_pattern = 30,
+											 VORBIS_invalid_stream_structure_version,
+											 VORBIS_continued_packet_flag_invalid,
+											 VORBIS_incorrect_stream_serial_number,
+											 VORBIS_invalid_first_page,
+											 VORBIS_bad_packet_type,
+											 VORBIS_cant_find_last_page,
+											 VORBIS_seek_failed
+	};
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // STB_VORBIS_INCLUDE_STB_VORBIS_H
+//
+//  HEADER ENDS HERE
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#ifndef STB_VORBIS_HEADER_ONLY
+
+// global configuration settings (e.g. set these in the project/makefile),
+// or just set them in this file at the top (although ideally the first few
+// should be visible when the header file is compiled too, although it's not
+// crucial)
+
+// STB_VORBIS_NO_PUSHDATA_API
+//     does not compile the code for the various stb_vorbis_*_pushdata()
+//     functions
+// #define STB_VORBIS_NO_PUSHDATA_API
+
+// STB_VORBIS_NO_PULLDATA_API
+//     does not compile the code for the non-pushdata APIs
+// #define STB_VORBIS_NO_PULLDATA_API
+
+// STB_VORBIS_NO_STDIO
+//     does not compile the code for the APIs that use FILE *s internally
+//     or externally (implied by STB_VORBIS_NO_PULLDATA_API)
+// #define STB_VORBIS_NO_STDIO
+
+// STB_VORBIS_NO_INTEGER_CONVERSION
+//     does not compile the code for converting audio sample data from
+//     float to integer (implied by STB_VORBIS_NO_PULLDATA_API)
+// #define STB_VORBIS_NO_INTEGER_CONVERSION
+
+// STB_VORBIS_NO_FAST_SCALED_FLOAT
+//      does not use a fast float-to-int trick to accelerate float-to-int on
+//      most platforms which requires endianness be defined correctly.
+//#define STB_VORBIS_NO_FAST_SCALED_FLOAT
+
+
+// STB_VORBIS_MAX_CHANNELS [number]
+//     globally define this to the maximum number of channels you need.
+//     The spec does not put a restriction on channels except that
+//     the count is stored in a byte, so 255 is the hard limit.
+//     Reducing this saves about 16 bytes per value, so using 16 saves
+//     (255-16)*16 or around 4KB. Plus anything other memory usage
+//     I forgot to account for. Can probably go as low as 8 (7.1 audio),
+//     6 (5.1 audio), or 2 (stereo only).
+#ifndef STB_VORBIS_MAX_CHANNELS
+#define STB_VORBIS_MAX_CHANNELS    16  // enough for anyone?
+#endif
+
+// STB_VORBIS_PUSHDATA_CRC_COUNT [number]
+//     after a flush_pushdata(), stb_vorbis begins scanning for the
+//     next valid page, without backtracking. when it finds something
+//     that looks like a page, it streams through it and verifies its
+//     CRC32. Should that validation fail, it keeps scanning. But it's
+//     possible that _while_ streaming through to check the CRC32 of
+//     one candidate page, it sees another candidate page. This #define
+//     determines how many "overlapping" candidate pages it can search
+//     at once. Note that "real" pages are typically ~4KB to ~8KB, whereas
+//     garbage pages could be as big as 64KB, but probably average ~16KB.
+//     So don't hose ourselves by scanning an apparent 64KB page and
+//     missing a ton of real ones in the interim; so minimum of 2
+#ifndef STB_VORBIS_PUSHDATA_CRC_COUNT
+#define STB_VORBIS_PUSHDATA_CRC_COUNT  4
+#endif
+
+// STB_VORBIS_FAST_HUFFMAN_LENGTH [number]
+//     sets the log size of the huffman-acceleration table.  Maximum
+//     supported value is 24. with larger numbers, more decodings are O(1),
+//     but the table size is larger so worse cache missing, so you'll have
+//     to probe (and try multiple ogg vorbis files) to find the sweet spot.
+#ifndef STB_VORBIS_FAST_HUFFMAN_LENGTH
+#define STB_VORBIS_FAST_HUFFMAN_LENGTH   10
+#endif
+
+// STB_VORBIS_FAST_BINARY_LENGTH [number]
+//     sets the log size of the binary-search acceleration table. this
+//     is used in similar fashion to the fast-huffman size to set initial
+//     parameters for the binary search
+
+// STB_VORBIS_FAST_HUFFMAN_INT
+//     The fast huffman tables are much more efficient if they can be
+//     stored as 16-bit results instead of 32-bit results. This restricts
+//     the codebooks to having only 65535 possible outcomes, though.
+//     (At least, accelerated by the huffman table.)
+#ifndef STB_VORBIS_FAST_HUFFMAN_INT
+#define STB_VORBIS_FAST_HUFFMAN_SHORT
+#endif
+
+// STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH
+//     If the 'fast huffman' search doesn't succeed, then stb_vorbis falls
+//     back on binary searching for the correct one. This requires storing
+//     extra tables with the huffman codes in sorted order. Defining this
+//     symbol trades off space for speed by forcing a linear search in the
+//     non-fast case, except for "sparse" codebooks.
+// #define STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH
+
+// STB_VORBIS_DIVIDES_IN_RESIDUE
+//     stb_vorbis precomputes the result of the scalar residue decoding
+//     that would otherwise require a divide per chunk. you can trade off
+//     space for time by defining this symbol.
+// #define STB_VORBIS_DIVIDES_IN_RESIDUE
+
+// STB_VORBIS_DIVIDES_IN_CODEBOOK
+//     vorbis VQ codebooks can be encoded two ways: with every case explicitly
+//     stored, or with all elements being chosen from a small range of values,
+//     and all values possible in all elements. By default, stb_vorbis expands
+//     this latter kind out to look like the former kind for ease of decoding,
+//     because otherwise an integer divide-per-vector-element is required to
+//     unpack the index. If you define STB_VORBIS_DIVIDES_IN_CODEBOOK, you can
+//     trade off storage for speed.
+//#define STB_VORBIS_DIVIDES_IN_CODEBOOK
+
+#ifdef STB_VORBIS_CODEBOOK_SHORTS
+#error "STB_VORBIS_CODEBOOK_SHORTS is no longer supported as it produced incorrect results for some input formats"
+#endif
+
+// STB_VORBIS_DIVIDE_TABLE
+//     this replaces small integer divides in the floor decode loop with
+//     table lookups. made less than 1% difference, so disabled by default.
+
+// STB_VORBIS_NO_INLINE_DECODE
+//     disables the inlining of the scalar codebook fast-huffman decode.
+//     might save a little codespace; useful for debugging
+// #define STB_VORBIS_NO_INLINE_DECODE
+
+// STB_VORBIS_NO_DEFER_FLOOR
+//     Normally we only decode the floor without synthesizing the actual
+//     full curve. We can instead synthesize the curve immediately. This
+//     requires more memory and is very likely slower, so I don't think
+//     you'd ever want to do it except for debugging.
+// #define STB_VORBIS_NO_DEFER_FLOOR
+
+
+
+
+//////////////////////////////////////////////////////////////////////////////
+
+#ifdef STB_VORBIS_NO_PULLDATA_API
+#define STB_VORBIS_NO_INTEGER_CONVERSION
+#define STB_VORBIS_NO_STDIO
+#endif
+
+#if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO)
+#define STB_VORBIS_NO_STDIO 1
+#endif
+
+#ifndef STB_VORBIS_NO_INTEGER_CONVERSION
+#ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT
+
+// only need endianness for fast-float-to-int, which we don't
+// use for pushdata
+
+#ifndef STB_VORBIS_BIG_ENDIAN
+#define STB_VORBIS_ENDIAN  0
+#else
+#define STB_VORBIS_ENDIAN  1
+#endif
+
+#endif
+#endif
+
+
+#ifndef STB_VORBIS_NO_STDIO
+#include <stdio.h>
+#endif
+
+#ifndef STB_VORBIS_NO_CRT
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <math.h>
+#if !(defined(__APPLE__) || defined(MACOSX) || defined(macintosh) || defined(Macintosh))
+#include <malloc.h>
+#if defined(__linux__) || defined(__linux) || defined(__EMSCRIPTEN__)
+#include <alloca.h>
+#endif
+#endif
+#else // STB_VORBIS_NO_CRT
+#define NULL 0
+#define malloc(s)   0
+#define free(s)     ((void) 0)
+#define realloc(s)  0
+#endif // STB_VORBIS_NO_CRT
+
+#include <limits.h>
+
+#ifdef __MINGW32__
+// eff you mingw:
+//     "fixed":
+//         http://sourceforge.net/p/mingw-w64/mailman/message/32882927/
+//     "no that broke the build, reverted, who cares about C":
+//         http://sourceforge.net/p/mingw-w64/mailman/message/32890381/
+#ifdef __forceinline
+#undef __forceinline
+#endif
+#define __forceinline
+#elif !defined(_MSC_VER)
+#if __GNUC__
+#define __forceinline inline
+#else
+#define __forceinline
+#endif
+#endif
+
+#if STB_VORBIS_MAX_CHANNELS > 256
+#error "Value of STB_VORBIS_MAX_CHANNELS outside of allowed range"
+#endif
+
+#if STB_VORBIS_FAST_HUFFMAN_LENGTH > 24
+#error "Value of STB_VORBIS_FAST_HUFFMAN_LENGTH outside of allowed range"
+#endif
+
+
+#if 0
+#include <crtdbg.h>
+#define CHECK(f)   _CrtIsValidHeapPointer(f->channel_buffers[1])
+#else
+#define CHECK(f)   ((void) 0)
+#endif
+
+#define MAX_BLOCKSIZE_LOG  13   // from specification
+#define MAX_BLOCKSIZE      (1 << MAX_BLOCKSIZE_LOG)
+
+
+typedef unsigned char  uint8;
+typedef   signed char   int8;
+typedef unsigned short uint16;
+typedef   signed short  int16;
+typedef unsigned int   uint32;
+typedef   signed int    int32;
+
+#ifndef TRUE
+#define TRUE 1
+#define FALSE 0
+#endif
+
+typedef float codetype;
+
+// @NOTE
+//
+// Some arrays below are tagged "//varies", which means it's actually
+// a variable-sized piece of data, but rather than malloc I assume it's
+// small enough it's better to just allocate it all together with the
+// main thing
+//
+// Most of the variables are specified with the smallest size I could pack
+// them into. It might give better performance to make them all full-sized
+// integers. It should be safe to freely rearrange the structures or change
+// the sizes larger--nothing relies on silently truncating etc., nor the
+// order of variables.
+
+#define FAST_HUFFMAN_TABLE_SIZE   (1 << STB_VORBIS_FAST_HUFFMAN_LENGTH)
+#define FAST_HUFFMAN_TABLE_MASK   (FAST_HUFFMAN_TABLE_SIZE - 1)
+
+typedef struct {
+	int dimensions, entries;
+	uint8 *codeword_lengths;
+	float  minimum_value;
+	float  delta_value;
+	uint8  value_bits;
+	uint8  lookup_type;
+	uint8  sequence_p;
+	uint8  sparse;
+	uint32 lookup_values;
+	codetype *multiplicands;
+	uint32 *codewords;
+#ifdef STB_VORBIS_FAST_HUFFMAN_SHORT
+	int16  fast_huffman[FAST_HUFFMAN_TABLE_SIZE];
+#else
+	int32  fast_huffman[FAST_HUFFMAN_TABLE_SIZE];
+#endif
+	uint32 *sorted_codewords;
+	int    *sorted_values;
+	int     sorted_entries;
+} Codebook;
+
+typedef struct {
+	uint8 order;
+	uint16 rate;
+	uint16 bark_map_size;
+	uint8 amplitude_bits;
+	uint8 amplitude_offset;
+	uint8 number_of_books;
+	uint8 book_list[16]; // varies
+} Floor0;
+
+typedef struct {
+	uint8 partitions;
+	uint8 partition_class_list[32]; // varies
+	uint8 class_dimensions[16]; // varies
+	uint8 class_subclasses[16]; // varies
+	uint8 class_masterbooks[16]; // varies
+	int16 subclass_books[16][8]; // varies
+	uint16 Xlist[31 * 8 + 2]; // varies
+	uint8 sorted_order[31 * 8 + 2];
+	uint8 neighbors[31 * 8 + 2][2];
+	uint8 floor1_multiplier;
+	uint8 rangebits;
+	int values;
+} Floor1;
+
+typedef union {
+	Floor0 floor0;
+	Floor1 floor1;
+} Floor;
+
+typedef struct {
+	uint32 begin, end;
+	uint32 part_size;
+	uint8 classifications;
+	uint8 classbook;
+	uint8 **classdata;
+	int16(*residue_books)[8];
+} Residue;
+
+typedef struct {
+	uint8 magnitude;
+	uint8 angle;
+	uint8 mux;
+} MappingChannel;
+
+typedef struct {
+	uint16 coupling_steps;
+	MappingChannel *chan;
+	uint8  submaps;
+	uint8  submap_floor[15]; // varies
+	uint8  submap_residue[15]; // varies
+} Mapping;
+
+typedef struct {
+	uint8 blockflag;
+	uint8 mapping;
+	uint16 windowtype;
+	uint16 transformtype;
+} Mode;
+
+typedef struct {
+	uint32  goal_crc;    // expected crc if match
+	int     bytes_left;  // bytes left in packet
+	uint32  crc_so_far;  // running crc
+	int     bytes_done;  // bytes processed in _current_ chunk
+	uint32  sample_loc;  // granule pos encoded in page
+} CRCscan;
+
+typedef struct {
+	uint32 page_start, page_end;
+	uint32 last_decoded_sample;
+} ProbedPage;
+
+struct stb_vorbis {
+	// user-accessible info
+	unsigned int sample_rate;
+	int channels;
+
+	unsigned int setup_memory_required;
+	unsigned int temp_memory_required;
+	unsigned int setup_temp_memory_required;
+
+	// input config
+#ifndef STB_VORBIS_NO_STDIO
+	Polycode::CoreFile *f;
+	uint32 f_start;
+	int close_on_free;
+#endif
+
+	uint8 *stream;
+	uint8 *stream_start;
+	uint8 *stream_end;
+
+	uint32 stream_len;
+
+	uint8  push_mode;
+
+	uint32 first_audio_page_offset;
+
+	ProbedPage p_first, p_last;
+
+	// memory management
+	stb_vorbis_alloc alloc;
+	int setup_offset;
+	int temp_offset;
+
+	// run-time results
+	int eof;
+	enum STBVorbisError error;
+
+	// user-useful data
+
+	// header info
+	int blocksize[2];
+	int blocksize_0, blocksize_1;
+	int codebook_count;
+	Codebook *codebooks;
+	int floor_count;
+	uint16 floor_types[64]; // varies
+	Floor *floor_config;
+	int residue_count;
+	uint16 residue_types[64]; // varies
+	Residue *residue_config;
+	int mapping_count;
+	Mapping *mapping;
+	int mode_count;
+	Mode mode_config[64];  // varies
+
+	uint32 total_samples;
+
+	// decode buffer
+	float *channel_buffers[STB_VORBIS_MAX_CHANNELS];
+	float *outputs[STB_VORBIS_MAX_CHANNELS];
+
+	float *previous_window[STB_VORBIS_MAX_CHANNELS];
+	int previous_length;
+
+#ifndef STB_VORBIS_NO_DEFER_FLOOR
+	int16 *finalY[STB_VORBIS_MAX_CHANNELS];
+#else
+	float *floor_buffers[STB_VORBIS_MAX_CHANNELS];
+#endif
+
+	uint32 current_loc; // sample location of next frame to decode
+	int    current_loc_valid;
+
+	// per-blocksize precomputed data
+
+	// twiddle factors
+	float *A[2], *B[2], *C[2];
+	float *window[2];
+	uint16 *bit_reverse[2];
+
+	// current page/packet/segment streaming info
+	uint32 serial; // stream serial number for verification
+	int last_page;
+	int segment_count;
+	uint8 segments[255];
+	uint8 page_flag;
+	uint8 bytes_in_seg;
+	uint8 first_decode;
+	int next_seg;
+	int last_seg;  // flag that we're on the last segment
+	int last_seg_which; // what was the segment number of the last seg?
+	uint32 acc;
+	int valid_bits;
+	int packet_bytes;
+	int end_seg_with_known_loc;
+	uint32 known_loc_for_packet;
+	int discard_samples_deferred;
+	uint32 samples_output;
+
+	// push mode scanning
+	int page_crc_tests; // only in push_mode: number of tests active; -1 if not searching
+#ifndef STB_VORBIS_NO_PUSHDATA_API
+	CRCscan scan[STB_VORBIS_PUSHDATA_CRC_COUNT];
+#endif
+
+	// sample-access
+	int channel_buffer_start;
+	int channel_buffer_end;
+};
+
+#if defined(STB_VORBIS_NO_PUSHDATA_API)
+#define IS_PUSH_MODE(f)   FALSE
+#elif defined(STB_VORBIS_NO_PULLDATA_API)
+#define IS_PUSH_MODE(f)   TRUE
+#else
+#define IS_PUSH_MODE(f)   ((f)->push_mode)
+#endif
+
+typedef struct stb_vorbis vorb;
+
+static int error(vorb *f, enum STBVorbisError e) {
+	f->error = e;
+	if (!f->eof && e != VORBIS_need_more_data) {
+		f->error = e; // breakpoint for debugging
+	}
+	return 0;
+}
+
+
+// these functions are used for allocating temporary memory
+// while decoding. if you can afford the stack space, use
+// alloca(); otherwise, provide a temp buffer and it will
+// allocate out of those.
+
+#define array_size_required(count,size)  (count*(sizeof(void *)+(size)))
+
+#define temp_alloc(f,size)              (f->alloc.alloc_buffer ? setup_temp_malloc(f,size) : alloca(size))
+#ifdef dealloca
+#define temp_free(f,p)                  (f->alloc.alloc_buffer ? 0 : dealloca(size))
+#else
+#define temp_free(f,p)                  0
+#endif
+#define temp_alloc_save(f)              ((f)->temp_offset)
+#define temp_alloc_restore(f,p)         ((f)->temp_offset = (p))
+
+#define temp_block_array(f,count,size)  make_block_array(temp_alloc(f,array_size_required(count,size)), count, size)
+
+// given a sufficiently large block of memory, make an array of pointers to subblocks of it
+static void *make_block_array(void *mem, int count, int size) {
+	int i;
+	void ** p = (void **) mem;
+	char *q = (char *) (p + count);
+	for (i = 0; i < count; ++i) {
+		p[i] = q;
+		q += size;
+	}
+	return p;
+}
+
+static void *setup_malloc(vorb *f, int sz) {
+	sz = (sz + 3) & ~3;
+	f->setup_memory_required += sz;
+	if (f->alloc.alloc_buffer) {
+		void *p = (char *) f->alloc.alloc_buffer + f->setup_offset;
+		if (f->setup_offset + sz > f->temp_offset) return NULL;
+		f->setup_offset += sz;
+		return p;
+	}
+	return sz ? malloc(sz) : NULL;
+}
+
+static void setup_free(vorb *f, void *p) {
+	if (f->alloc.alloc_buffer) return; // do nothing; setup mem is a stack
+	free(p);
+}
+
+static void *setup_temp_malloc(vorb *f, int sz) {
+	sz = (sz + 3) & ~3;
+	if (f->alloc.alloc_buffer) {
+		if (f->temp_offset - sz < f->setup_offset) return NULL;
+		f->temp_offset -= sz;
+		return (char *) f->alloc.alloc_buffer + f->temp_offset;
+	}
+	return malloc(sz);
+}
+
+static void setup_temp_free(vorb *f, void *p, int sz) {
+	if (f->alloc.alloc_buffer) {
+		f->temp_offset += (sz + 3)&~3;
+		return;
+	}
+	free(p);
+}
+
+#define CRC32_POLY    0x04c11db7   // from spec
+
+static uint32 crc_table[256];
+static void crc32_init(void) {
+	int i, j;
+	uint32 s;
+	for (i = 0; i < 256; i++) {
+		for (s = (uint32) i << 24, j = 0; j < 8; ++j)
+			s = (s << 1) ^ (s >= (1U << 31) ? CRC32_POLY : 0);
+		crc_table[i] = s;
+	}
+}
+
+static __forceinline uint32 crc32_update(uint32 crc, uint8 byte) {
+	return (crc << 8) ^ crc_table[byte ^ (crc >> 24)];
+}
+
+
+// used in setup, and for huffman that doesn't go fast path
+static unsigned int bit_reverse(unsigned int n) {
+	n = ((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1);
+	n = ((n & 0xCCCCCCCC) >> 2) | ((n & 0x33333333) << 2);
+	n = ((n & 0xF0F0F0F0) >> 4) | ((n & 0x0F0F0F0F) << 4);
+	n = ((n & 0xFF00FF00) >> 8) | ((n & 0x00FF00FF) << 8);
+	return (n >> 16) | (n << 16);
+}
+
+static float square(float x) {
+	return x*x;
+}
+
+// this is a weird definition of log2() for which log2(1) = 1, log2(2) = 2, log2(4) = 3
+// as required by the specification. fast(?) implementation from stb.h
+// @OPTIMIZE: called multiple times per-packet with "constants"; move to setup
+static int ilog(int32 n) {
+	static signed char log2_4[16] = {0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4};
+
+	// 2 compares if n < 16, 3 compares otherwise (4 if signed or n > 1<<29)
+	if (n < (1 << 14))
+		if (n < (1 << 4))        return     0 + log2_4[n];
+		else if (n < (1 << 9))      return  5 + log2_4[n >> 5];
+		else                     return 10 + log2_4[n >> 10];
+	else if (n < (1 << 24))
+		if (n < (1 << 19))      return 15 + log2_4[n >> 15];
+		else                     return 20 + log2_4[n >> 20];
+	else if (n < (1 << 29))      return 25 + log2_4[n >> 25];
+	else if (n < (1 << 31)) return 30 + log2_4[n >> 30];
+	else                return 0; // signed n returns 0
+}
+
+#ifndef M_PI
+#define M_PI  3.14159265358979323846264f  // from CRC
+#endif
+
+// code length assigned to a value with no huffman encoding
+#define NO_CODE   255
+
+/////////////////////// LEAF SETUP FUNCTIONS //////////////////////////
+//
+// these functions are only called at setup, and only a few times
+// per file
+
+static float float32_unpack(uint32 x) {
+	// from the specification
+	uint32 mantissa = x & 0x1fffff;
+	uint32 sign = x & 0x80000000;
+	uint32 exp = (x & 0x7fe00000) >> 21;
+	double res = sign ? -(double) mantissa : (double) mantissa;
+	return (float) ldexp((float) res, exp - 788);
+}
+
+
+// zlib & jpeg huffman tables assume that the output symbols
+// can either be arbitrarily arranged, or have monotonically
+// increasing frequencies--they rely on the lengths being sorted;
+// this makes for a very simple generation algorithm.
+// vorbis allows a huffman table with non-sorted lengths. This
+// requires a more sophisticated construction, since symbols in
+// order do not map to huffman codes "in order".
+static void add_entry(Codebook *c, uint32 huff_code, int symbol, int count, int len, uint32 *values) {
+	if (!c->sparse) {
+		c->codewords[symbol] = huff_code;
+	} else {
+		c->codewords[count] = huff_code;
+		c->codeword_lengths[count] = len;
+		values[count] = symbol;
+	}
+}
+
+static int compute_codewords(Codebook *c, uint8 *len, int n, uint32 *values) {
+	int i, k, m = 0;
+	uint32 available[32];
+
+	memset(available, 0, sizeof(available));
+	// find the first entry
+	for (k = 0; k < n; ++k) if (len[k] < NO_CODE) break;
+	if (k == n) { assert(c->sorted_entries == 0); return TRUE; }
+	// add to the list
+	add_entry(c, 0, k, m++, len[k], values);
+	// add all available leaves
+	for (i = 1; i <= len[k]; ++i)
+		available[i] = 1U << (32 - i);
+	// note that the above code treats the first case specially,
+	// but it's really the same as the following code, so they
+	// could probably be combined (except the initial code is 0,
+	// and I use 0 in available[] to mean 'empty')
+	for (i = k + 1; i < n; ++i) {
+		uint32 res;
+		int z = len[i], y;
+		if (z == NO_CODE) continue;
+		// find lowest available leaf (should always be earliest,
+		// which is what the specification calls for)
+		// note that this property, and the fact we can never have
+		// more than one free leaf at a given level, isn't totally
+		// trivial to prove, but it seems true and the assert never
+		// fires, so!
+		while (z > 0 && !available[z]) --z;
+		if (z == 0) { return FALSE; }
+		res = available[z];
+		assert(z >= 0 && z < 32);
+		available[z] = 0;
+		add_entry(c, bit_reverse(res), i, m++, len[i], values);
+		// propogate availability up the tree
+		if (z != len[i]) {
+			assert(len[i] >= 0 && len[i] < 32);
+			for (y = len[i]; y > z; --y) {
+				assert(available[y] == 0);
+				available[y] = res + (1 << (32 - y));
+			}
+		}
+	}
+	return TRUE;
+}
+
+// accelerated huffman table allows fast O(1) match of all symbols
+// of length <= STB_VORBIS_FAST_HUFFMAN_LENGTH
+static void compute_accelerated_huffman(Codebook *c) {
+	int i, len;
+	for (i = 0; i < FAST_HUFFMAN_TABLE_SIZE; ++i)
+		c->fast_huffman[i] = -1;
+
+	len = c->sparse ? c->sorted_entries : c->entries;
+#ifdef STB_VORBIS_FAST_HUFFMAN_SHORT
+	if (len > 32767) len = 32767; // largest possible value we can encode!
+#endif
+	for (i = 0; i < len; ++i) {
+		if (c->codeword_lengths[i] <= STB_VORBIS_FAST_HUFFMAN_LENGTH) {
+			uint32 z = c->sparse ? bit_reverse(c->sorted_codewords[i]) : c->codewords[i];
+			// set table entries for all bit combinations in the higher bits
+			while (z < FAST_HUFFMAN_TABLE_SIZE) {
+				c->fast_huffman[z] = i;
+				z += 1 << c->codeword_lengths[i];
+			}
+		}
+	}
+}
+
+#ifdef _MSC_VER
+#define STBV_CDECL __cdecl
+#else
+#define STBV_CDECL
+#endif
+
+static int STBV_CDECL uint32_compare(const void *p, const void *q) {
+	uint32 x = *(uint32 *) p;
+	uint32 y = *(uint32 *) q;
+	return x < y ? -1 : x > y;
+}
+
+static int include_in_sort(Codebook *c, uint8 len) {
+	if (c->sparse) { assert(len != NO_CODE); return TRUE; }
+	if (len == NO_CODE) return FALSE;
+	if (len > STB_VORBIS_FAST_HUFFMAN_LENGTH) return TRUE;
+	return FALSE;
+}
+
+// if the fast table above doesn't work, we want to binary
+// search them... need to reverse the bits
+static void compute_sorted_huffman(Codebook *c, uint8 *lengths, uint32 *values) {
+	int i, len;
+	// build a list of all the entries
+	// OPTIMIZATION: don't include the short ones, since they'll be caught by FAST_HUFFMAN.
+	// this is kind of a frivolous optimization--I don't see any performance improvement,
+	// but it's like 4 extra lines of code, so.
+	if (!c->sparse) {
+		int k = 0;
+		for (i = 0; i < c->entries; ++i)
+			if (include_in_sort(c, lengths[i]))
+				c->sorted_codewords[k++] = bit_reverse(c->codewords[i]);
+		assert(k == c->sorted_entries);
+	} else {
+		for (i = 0; i < c->sorted_entries; ++i)
+			c->sorted_codewords[i] = bit_reverse(c->codewords[i]);
+	}
+
+	qsort(c->sorted_codewords, c->sorted_entries, sizeof(c->sorted_codewords[0]), uint32_compare);
+	c->sorted_codewords[c->sorted_entries] = 0xffffffff;
+
+	len = c->sparse ? c->sorted_entries : c->entries;
+	// now we need to indicate how they correspond; we could either
+	//   #1: sort a different data structure that says who they correspond to
+	//   #2: for each sorted entry, search the original list to find who corresponds
+	//   #3: for each original entry, find the sorted entry
+	// #1 requires extra storage, #2 is slow, #3 can use binary search!
+	for (i = 0; i < len; ++i) {
+		int huff_len = c->sparse ? lengths[values[i]] : lengths[i];
+		if (include_in_sort(c, huff_len)) {
+			uint32 code = bit_reverse(c->codewords[i]);
+			int x = 0, n = c->sorted_entries;
+			while (n > 1) {
+				// invariant: sc[x] <= code < sc[x+n]
+				int m = x + (n >> 1);
+				if (c->sorted_codewords[m] <= code) {
+					x = m;
+					n -= (n >> 1);
+				} else {
+					n >>= 1;
+				}
+			}
+			assert(c->sorted_codewords[x] == code);
+			if (c->sparse) {
+				c->sorted_values[x] = values[i];
+				c->codeword_lengths[x] = huff_len;
+			} else {
+				c->sorted_values[x] = i;
+			}
+		}
+	}
+}
+
+// only run while parsing the header (3 times)
+static int vorbis_validate(uint8 *data) {
+	static uint8 vorbis[6] = {'v', 'o', 'r', 'b', 'i', 's'};
+	return memcmp(data, vorbis, 6) == 0;
+}
+
+// called from setup only, once per code book
+// (formula implied by specification)
+static int lookup1_values(int entries, int dim) {
+	int r = (int) floor(exp((float) log((float) entries) / dim));
+	if ((int) floor(pow((float) r + 1, dim)) <= entries)   // (int) cast for MinGW warning;
+		++r;                                              // floor() to avoid _ftol() when non-CRT
+	assert(pow((float) r + 1, dim) > entries);
+	assert((int) floor(pow((float) r, dim)) <= entries); // (int),floor() as above
+	return r;
+}
+
+// called twice per file
+static void compute_twiddle_factors(int n, float *A, float *B, float *C) {
+	int n4 = n >> 2, n8 = n >> 3;
+	int k, k2;
+
+	for (k = k2 = 0; k < n4; ++k, k2 += 2) {
+		A[k2] = (float) cos(4 * k*M_PI / n);
+		A[k2 + 1] = (float) -sin(4 * k*M_PI / n);
+		B[k2] = (float) cos((k2 + 1)*M_PI / n / 2) * 0.5f;
+		B[k2 + 1] = (float) sin((k2 + 1)*M_PI / n / 2) * 0.5f;
+	}
+	for (k = k2 = 0; k < n8; ++k, k2 += 2) {
+		C[k2] = (float) cos(2 * (k2 + 1)*M_PI / n);
+		C[k2 + 1] = (float) -sin(2 * (k2 + 1)*M_PI / n);
+	}
+}
+
+static void compute_window(int n, float *window) {
+	int n2 = n >> 1, i;
+	for (i = 0; i < n2; ++i)
+		window[i] = (float) sin(0.5 * M_PI * square((float) sin((i - 0 + 0.5) / n2 * 0.5 * M_PI)));
+}
+
+static void compute_bitreverse(int n, uint16 *rev) {
+	int ld = ilog(n) - 1; // ilog is off-by-one from normal definitions
+	int i, n8 = n >> 3;
+	for (i = 0; i < n8; ++i)
+		rev[i] = (bit_reverse(i) >> (32 - ld + 3)) << 2;
+}
+
+static int init_blocksize(vorb *f, int b, int n) {
+	int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3;
+	f->A[b] = (float *) setup_malloc(f, sizeof(float) * n2);
+	f->B[b] = (float *) setup_malloc(f, sizeof(float) * n2);
+	f->C[b] = (float *) setup_malloc(f, sizeof(float) * n4);
+	if (!f->A[b] || !f->B[b] || !f->C[b]) return error(f, VORBIS_outofmem);
+	compute_twiddle_factors(n, f->A[b], f->B[b], f->C[b]);
+	f->window[b] = (float *) setup_malloc(f, sizeof(float) * n2);
+	if (!f->window[b]) return error(f, VORBIS_outofmem);
+	compute_window(n, f->window[b]);
+	f->bit_reverse[b] = (uint16 *) setup_malloc(f, sizeof(uint16) * n8);
+	if (!f->bit_reverse[b]) return error(f, VORBIS_outofmem);
+	compute_bitreverse(n, f->bit_reverse[b]);
+	return TRUE;
+}
+
+static void neighbors(uint16 *x, int n, int *plow, int *phigh) {
+	int low = -1;
+	int high = 65536;
+	int i;
+	for (i = 0; i < n; ++i) {
+		if (x[i] > low  && x[i] < x[n]) { *plow = i; low = x[i]; }
+		if (x[i] < high && x[i] > x[n]) { *phigh = i; high = x[i]; }
+	}
+}
+
+// this has been repurposed so y is now the original index instead of y
+typedef struct {
+	uint16 x, y;
+} Point;
+
+static int STBV_CDECL point_compare(const void *p, const void *q) {
+	Point *a = (Point *) p;
+	Point *b = (Point *) q;
+	return a->x < b->x ? -1 : a->x > b->x;
+}
+
+//
+/////////////////////// END LEAF SETUP FUNCTIONS //////////////////////////
+
+
+#if defined(STB_VORBIS_NO_STDIO)
+#define USE_MEMORY(z)    TRUE
+#else
+#define USE_MEMORY(z)    ((z)->stream)
+#endif
+
+static uint8 get8(vorb *z) {
+	if (USE_MEMORY(z)) {
+		if (z->stream >= z->stream_end) { z->eof = TRUE; return 0; }
+		return *z->stream++;
+	}
+
+#ifndef STB_VORBIS_NO_STDIO
+	{
+		int* ptr = new int[1];
+		z->f->read(ptr, sizeof(uint8), 1);
+		int c = ptr[0];
+		//int c = fgetc(z->f);
+		if (c == EOF) { z->eof = TRUE; return 0; }
+		return c;
+	}
+#endif
+}
+
+static uint32 get32(vorb *f) {
+	uint32 x;
+	x = get8(f);
+	x += get8(f) << 8;
+	x += get8(f) << 16;
+	x += (uint32) get8(f) << 24;
+	return x;
+}
+
+static int getn(vorb *z, uint8 *data, int n) {
+	if (USE_MEMORY(z)) {
+		if (z->stream + n > z->stream_end) { z->eof = 1; return 0; }
+		memcpy(data, z->stream, n);
+		z->stream += n;
+		return 1;
+	}
+
+#ifndef STB_VORBIS_NO_STDIO   
+	if (z->f->read(data, n, 1) == 1)
+		return 1;
+	else {
+		z->eof = 1;
+		return 0;
+	}
+#endif
+}
+
+static void skip(vorb *z, int n) {
+	if (USE_MEMORY(z)) {
+		z->stream += n;
+		if (z->stream >= z->stream_end) z->eof = 1;
+		return;
+	}
+#ifndef STB_VORBIS_NO_STDIO
+	{
+		long x = z->f->tell();
+		z->f->seek(x + n, SEEK_SET);
+	}
+#endif
+}
+
+static int set_file_offset(stb_vorbis *f, unsigned int loc) {
+#ifndef STB_VORBIS_NO_PUSHDATA_API
+	if (f->push_mode) return 0;
+#endif
+	f->eof = 0;
+	if (USE_MEMORY(f)) {
+		if (f->stream_start + loc >= f->stream_end || f->stream_start + loc < f->stream_start) {
+			f->stream = f->stream_end;
+			f->eof = 1;
+			return 0;
+		} else {
+			f->stream = f->stream_start + loc;
+			return 1;
+		}
+	}
+#ifndef STB_VORBIS_NO_STDIO
+	if (loc + f->f_start < loc || loc >= 0x80000000) {
+		loc = 0x7fffffff;
+		f->eof = 1;
+	} else {
+		loc += f->f_start;
+	}
+	if (!f->f->seek(loc, SEEK_SET))
+		return 1;
+	f->eof = 1;
+	f->f->seek(f->f_start, SEEK_END);
+	return 0;
+#endif
+}
+
+
+static uint8 ogg_page_header[4] = {0x4f, 0x67, 0x67, 0x53};
+
+static int capture_pattern(vorb *f) {
+	if (0x4f != get8(f)) return FALSE;
+	if (0x67 != get8(f)) return FALSE;
+	if (0x67 != get8(f)) return FALSE;
+	if (0x53 != get8(f)) return FALSE;
+	return TRUE;
+}
+
+#define PAGEFLAG_continued_packet   1
+#define PAGEFLAG_first_page         2
+#define PAGEFLAG_last_page          4
+
+static int start_page_no_capturepattern(vorb *f) {
+	uint32 loc0, loc1, n;
+	// stream structure version
+	if (0 != get8(f)) return error(f, VORBIS_invalid_stream_structure_version);
+	// header flag
+	f->page_flag = get8(f);
+	// absolute granule position
+	loc0 = get32(f);
+	loc1 = get32(f);
+	// @TODO: validate loc0,loc1 as valid positions?
+	// stream serial number -- vorbis doesn't interleave, so discard
+	get32(f);
+	//if (f->serial != get32(f)) return error(f, VORBIS_incorrect_stream_serial_number);
+	// page sequence number
+	n = get32(f);
+	f->last_page = n;
+	// CRC32
+	get32(f);
+	// page_segments
+	f->segment_count = get8(f);
+	if (!getn(f, f->segments, f->segment_count))
+		return error(f, VORBIS_unexpected_eof);
+	// assume we _don't_ know any the sample position of any segments
+	f->end_seg_with_known_loc = -2;
+	if (loc0 != ~0U || loc1 != ~0U) {
+		int i;
+		// determine which packet is the last one that will complete
+		for (i = f->segment_count - 1; i >= 0; --i)
+			if (f->segments[i] < 255)
+				break;
+		// 'i' is now the index of the _last_ segment of a packet that ends
+		if (i >= 0) {
+			f->end_seg_with_known_loc = i;
+			f->known_loc_for_packet = loc0;
+		}
+	}
+	if (f->first_decode) {
+		int i, len;
+		ProbedPage p;
+		len = 0;
+		for (i = 0; i < f->segment_count; ++i)
+			len += f->segments[i];
+		len += 27 + f->segment_count;
+		p.page_start = f->first_audio_page_offset;
+		p.page_end = p.page_start + len;
+		p.last_decoded_sample = loc0;
+		f->p_first = p;
+	}
+	f->next_seg = 0;
+	return TRUE;
+}
+
+static int start_page(vorb *f) {
+	if (!capture_pattern(f)) return error(f, VORBIS_missing_capture_pattern);
+	return start_page_no_capturepattern(f);
+}
+
+static int start_packet(vorb *f) {
+	while (f->next_seg == -1) {
+		if (!start_page(f)) return FALSE;
+		if (f->page_flag & PAGEFLAG_continued_packet)
+			return error(f, VORBIS_continued_packet_flag_invalid);
+	}
+	f->last_seg = FALSE;
+	f->valid_bits = 0;
+	f->packet_bytes = 0;
+	f->bytes_in_seg = 0;
+	// f->next_seg is now valid
+	return TRUE;
+}
+
+static int maybe_start_packet(vorb *f) {
+	if (f->next_seg == -1) {
+		int x = get8(f);
+		if (f->eof) return FALSE; // EOF at page boundary is not an error!
+		if (0x4f != x) return error(f, VORBIS_missing_capture_pattern);
+		if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern);
+		if (0x67 != get8(f)) return error(f, VORBIS_missing_capture_pattern);
+		if (0x53 != get8(f)) return error(f, VORBIS_missing_capture_pattern);
+		if (!start_page_no_capturepattern(f)) return FALSE;
+		if (f->page_flag & PAGEFLAG_continued_packet) {
+			// set up enough state that we can read this packet if we want,
+			// e.g. during recovery
+			f->last_seg = FALSE;
+			f->bytes_in_seg = 0;
+			return error(f, VORBIS_continued_packet_flag_invalid);
+		}
+	}
+	return start_packet(f);
+}
+
+static int next_segment(vorb *f) {
+	int len;
+	if (f->last_seg) return 0;
+	if (f->next_seg == -1) {
+		f->last_seg_which = f->segment_count - 1; // in case start_page fails
+		if (!start_page(f)) { f->last_seg = 1; return 0; }
+		if (!(f->page_flag & PAGEFLAG_continued_packet)) return error(f, VORBIS_continued_packet_flag_invalid);
+	}
+	len = f->segments[f->next_seg++];
+	if (len < 255) {
+		f->last_seg = TRUE;
+		f->last_seg_which = f->next_seg - 1;
+	}
+	if (f->next_seg >= f->segment_count)
+		f->next_seg = -1;
+	assert(f->bytes_in_seg == 0);
+	f->bytes_in_seg = len;
+	return len;
+}
+
+#define EOP    (-1)
+#define INVALID_BITS  (-1)
+
+static int get8_packet_raw(vorb *f) {
+	if (!f->bytes_in_seg) {  // CLANG!
+		if (f->last_seg) return EOP;
+		else if (!next_segment(f)) return EOP;
+	}
+	assert(f->bytes_in_seg > 0);
+	--f->bytes_in_seg;
+	++f->packet_bytes;
+	return get8(f);
+}
+
+static int get8_packet(vorb *f) {
+	int x = get8_packet_raw(f);
+	f->valid_bits = 0;
+	return x;
+}
+
+static void flush_packet(vorb *f) {
+	while (get8_packet_raw(f) != EOP);
+}
+
+// @OPTIMIZE: this is the secondary bit decoder, so it's probably not as important
+// as the huffman decoder?
+static uint32 get_bits(vorb *f, int n) {
+	uint32 z;
+
+	if (f->valid_bits < 0) return 0;
+	if (f->valid_bits < n) {
+		if (n > 24) {
+			// the accumulator technique below would not work correctly in this case
+			z = get_bits(f, 24);
+			z += get_bits(f, n - 24) << 24;
+			return z;
+		}
+		if (f->valid_bits == 0) f->acc = 0;
+		while (f->valid_bits < n) {
+			int z = get8_packet_raw(f);
+			if (z == EOP) {
+				f->valid_bits = INVALID_BITS;
+				return 0;
+			}
+			f->acc += z << f->valid_bits;
+			f->valid_bits += 8;
+		}
+	}
+	if (f->valid_bits < 0) return 0;
+	z = f->acc & ((1 << n) - 1);
+	f->acc >>= n;
+	f->valid_bits -= n;
+	return z;
+}
+
+// @OPTIMIZE: primary accumulator for huffman
+// expand the buffer to as many bits as possible without reading off end of packet
+// it might be nice to allow f->valid_bits and f->acc to be stored in registers,
+// e.g. cache them locally and decode locally
+static __forceinline void prep_huffman(vorb *f) {
+	if (f->valid_bits <= 24) {
+		if (f->valid_bits == 0) f->acc = 0;
+		do {
+			int z;
+			if (f->last_seg && !f->bytes_in_seg) return;
+			z = get8_packet_raw(f);
+			if (z == EOP) return;
+			f->acc += (unsigned) z << f->valid_bits;
+			f->valid_bits += 8;
+		} while (f->valid_bits <= 24);
+	}
+}
+
+enum {
+	VORBIS_packet_id = 1,
+	VORBIS_packet_comment = 3,
+	VORBIS_packet_setup = 5
+};
+
+static int codebook_decode_scalar_raw(vorb *f, Codebook *c) {
+	int i;
+	prep_huffman(f);
+
+	if (c->codewords == NULL && c->sorted_codewords == NULL)
+		return -1;
+
+	// cases to use binary search: sorted_codewords && !c->codewords
+	//                             sorted_codewords && c->entries > 8
+	if (c->entries > 8 ? c->sorted_codewords != NULL : !c->codewords) {
+		// binary search
+		uint32 code = bit_reverse(f->acc);
+		int x = 0, n = c->sorted_entries, len;
+
+		while (n > 1) {
+			// invariant: sc[x] <= code < sc[x+n]
+			int m = x + (n >> 1);
+			if (c->sorted_codewords[m] <= code) {
+				x = m;
+				n -= (n >> 1);
+			} else {
+				n >>= 1;
+			}
+		}
+		// x is now the sorted index
+		if (!c->sparse) x = c->sorted_values[x];
+		// x is now sorted index if sparse, or symbol otherwise
+		len = c->codeword_lengths[x];
+		if (f->valid_bits >= len) {
+			f->acc >>= len;
+			f->valid_bits -= len;
+			return x;
+		}
+
+		f->valid_bits = 0;
+		return -1;
+	}
+
+	// if small, linear search
+	assert(!c->sparse);
+	for (i = 0; i < c->entries; ++i) {
+		if (c->codeword_lengths[i] == NO_CODE) continue;
+		if (c->codewords[i] == (f->acc & ((1 << c->codeword_lengths[i]) - 1))) {
+			if (f->valid_bits >= c->codeword_lengths[i]) {
+				f->acc >>= c->codeword_lengths[i];
+				f->valid_bits -= c->codeword_lengths[i];
+				return i;
+			}
+			f->valid_bits = 0;
+			return -1;
+		}
+	}
+
+	error(f, VORBIS_invalid_stream);
+	f->valid_bits = 0;
+	return -1;
+}
+
+#ifndef STB_VORBIS_NO_INLINE_DECODE
+
+#define DECODE_RAW(var, f,c)                                  \
+   if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH)        \
+      prep_huffman(f);                                        \
+   var = f->acc & FAST_HUFFMAN_TABLE_MASK;                    \
+   var = c->fast_huffman[var];                                \
+   if (var >= 0) {                                            \
+      int n = c->codeword_lengths[var];                       \
+      f->acc >>= n;                                           \
+      f->valid_bits -= n;                                     \
+      if (f->valid_bits < 0) { f->valid_bits = 0; var = -1; } \
+   } else {                                                   \
+      var = codebook_decode_scalar_raw(f,c);                  \
+   }
+
+#else
+
+static int codebook_decode_scalar(vorb *f, Codebook *c) {
+	int i;
+	if (f->valid_bits < STB_VORBIS_FAST_HUFFMAN_LENGTH)
+		prep_huffman(f);
+	// fast huffman table lookup
+	i = f->acc & FAST_HUFFMAN_TABLE_MASK;
+	i = c->fast_huffman[i];
+	if (i >= 0) {
+		f->acc >>= c->codeword_lengths[i];
+		f->valid_bits -= c->codeword_lengths[i];
+		if (f->valid_bits < 0) { f->valid_bits = 0; return -1; }
+		return i;
+	}
+	return codebook_decode_scalar_raw(f, c);
+}
+
+#define DECODE_RAW(var,f,c)    var = codebook_decode_scalar(f,c);
+
+#endif
+
+#define DECODE(var,f,c)                                       \
+   DECODE_RAW(var,f,c)                                        \
+   if (c->sparse) var = c->sorted_values[var];
+
+#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
+#define DECODE_VQ(var,f,c)   DECODE_RAW(var,f,c)
+#else
+#define DECODE_VQ(var,f,c)   DECODE(var,f,c)
+#endif
+
+
+
+
+
+
+// CODEBOOK_ELEMENT_FAST is an optimization for the CODEBOOK_FLOATS case
+// where we avoid one addition
+#define CODEBOOK_ELEMENT(c,off)          (c->multiplicands[off])
+#define CODEBOOK_ELEMENT_FAST(c,off)     (c->multiplicands[off])
+#define CODEBOOK_ELEMENT_BASE(c)         (0)
+
+static int codebook_decode_start(vorb *f, Codebook *c) {
+	int z = -1;
+
+	// type 0 is only legal in a scalar context
+	if (c->lookup_type == 0)
+		error(f, VORBIS_invalid_stream);
+	else {
+		DECODE_VQ(z, f, c);
+		if (c->sparse) assert(z < c->sorted_entries);
+		if (z < 0) {  // check for EOP
+			if (!f->bytes_in_seg)
+				if (f->last_seg)
+					return z;
+			error(f, VORBIS_invalid_stream);
+		}
+	}
+	return z;
+}
+
+static int codebook_decode(vorb *f, Codebook *c, float *output, int len) {
+	int i, z = codebook_decode_start(f, c);
+	if (z < 0) return FALSE;
+	if (len > c->dimensions) len = c->dimensions;
+
+#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
+	if (c->lookup_type == 1) {
+		float last = CODEBOOK_ELEMENT_BASE(c);
+		int div = 1;
+		for (i = 0; i < len; ++i) {
+			int off = (z / div) % c->lookup_values;
+			float val = CODEBOOK_ELEMENT_FAST(c, off) + last;
+			output[i] += val;
+			if (c->sequence_p) last = val + c->minimum_value;
+			div *= c->lookup_values;
+		}
+		return TRUE;
+	}
+#endif
+
+	z *= c->dimensions;
+	if (c->sequence_p) {
+		float last = CODEBOOK_ELEMENT_BASE(c);
+		for (i = 0; i < len; ++i) {
+			float val = CODEBOOK_ELEMENT_FAST(c, z + i) + last;
+			output[i] += val;
+			last = val + c->minimum_value;
+		}
+	} else {
+		float last = CODEBOOK_ELEMENT_BASE(c);
+		for (i = 0; i < len; ++i) {
+			output[i] += CODEBOOK_ELEMENT_FAST(c, z + i) + last;
+		}
+	}
+
+	return TRUE;
+}
+
+static int codebook_decode_step(vorb *f, Codebook *c, float *output, int len, int step) {
+	int i, z = codebook_decode_start(f, c);
+	float last = CODEBOOK_ELEMENT_BASE(c);
+	if (z < 0) return FALSE;
+	if (len > c->dimensions) len = c->dimensions;
+
+#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
+	if (c->lookup_type == 1) {
+		int div = 1;
+		for (i = 0; i < len; ++i) {
+			int off = (z / div) % c->lookup_values;
+			float val = CODEBOOK_ELEMENT_FAST(c, off) + last;
+			output[i*step] += val;
+			if (c->sequence_p) last = val;
+			div *= c->lookup_values;
+		}
+		return TRUE;
+	}
+#endif
+
+	z *= c->dimensions;
+	for (i = 0; i < len; ++i) {
+		float val = CODEBOOK_ELEMENT_FAST(c, z + i) + last;
+		output[i*step] += val;
+		if (c->sequence_p) last = val;
+	}
+
+	return TRUE;
+}
+
+static int codebook_decode_deinterleave_repeat(vorb *f, Codebook *c, float **outputs, int ch, int *c_inter_p, int *p_inter_p, int len, int total_decode) {
+	int c_inter = *c_inter_p;
+	int p_inter = *p_inter_p;
+	int i, z, effective = c->dimensions;
+
+	// type 0 is only legal in a scalar context
+	if (c->lookup_type == 0)   return error(f, VORBIS_invalid_stream);
+
+	while (total_decode > 0) {
+		float last = CODEBOOK_ELEMENT_BASE(c);
+		DECODE_VQ(z, f, c);
+#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
+		assert(!c->sparse || z < c->sorted_entries);
+#endif
+		if (z < 0) {
+			if (!f->bytes_in_seg)
+				if (f->last_seg) return FALSE;
+			return error(f, VORBIS_invalid_stream);
+		}
+
+		// if this will take us off the end of the buffers, stop short!
+		// we check by computing the length of the virtual interleaved
+		// buffer (len*ch), our current offset within it (p_inter*ch)+(c_inter),
+		// and the length we'll be using (effective)
+		if (c_inter + p_inter*ch + effective > len * ch) {
+			effective = len*ch - (p_inter*ch - c_inter);
+		}
+
+#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
+		if (c->lookup_type == 1) {
+			int div = 1;
+			for (i = 0; i < effective; ++i) {
+				int off = (z / div) % c->lookup_values;
+				float val = CODEBOOK_ELEMENT_FAST(c, off) + last;
+				if (outputs[c_inter])
+					outputs[c_inter][p_inter] += val;
+				if (++c_inter == ch) { c_inter = 0; ++p_inter; }
+				if (c->sequence_p) last = val;
+				div *= c->lookup_values;
+			}
+		} else
+#endif
+		{
+			z *= c->dimensions;
+			if (c->sequence_p) {
+				for (i = 0; i < effective; ++i) {
+					float val = CODEBOOK_ELEMENT_FAST(c, z + i) + last;
+					if (outputs[c_inter])
+						outputs[c_inter][p_inter] += val;
+					if (++c_inter == ch) { c_inter = 0; ++p_inter; }
+					last = val;
+				}
+			} else {
+				for (i = 0; i < effective; ++i) {
+					float val = CODEBOOK_ELEMENT_FAST(c, z + i) + last;
+					if (outputs[c_inter])
+						outputs[c_inter][p_inter] += val;
+					if (++c_inter == ch) { c_inter = 0; ++p_inter; }
+				}
+			}
+		}
+
+		total_decode -= effective;
+	}
+	*c_inter_p = c_inter;
+	*p_inter_p = p_inter;
+	return TRUE;
+}
+
+static int predict_point(int x, int x0, int x1, int y0, int y1) {
+	int dy = y1 - y0;
+	int adx = x1 - x0;
+	// @OPTIMIZE: force int division to round in the right direction... is this necessary on x86?
+	int err = abs(dy) * (x - x0);
+	int off = err / adx;
+	return dy < 0 ? y0 - off : y0 + off;
+}
+
+// the following table is block-copied from the specification
+static float inverse_db_table[256] =
+{
+	1.0649863e-07f, 1.1341951e-07f, 1.2079015e-07f, 1.2863978e-07f,
+	1.3699951e-07f, 1.4590251e-07f, 1.5538408e-07f, 1.6548181e-07f,
+	1.7623575e-07f, 1.8768855e-07f, 1.9988561e-07f, 2.1287530e-07f,
+	2.2670913e-07f, 2.4144197e-07f, 2.5713223e-07f, 2.7384213e-07f,
+	2.9163793e-07f, 3.1059021e-07f, 3.3077411e-07f, 3.5226968e-07f,
+	3.7516214e-07f, 3.9954229e-07f, 4.2550680e-07f, 4.5315863e-07f,
+	4.8260743e-07f, 5.1396998e-07f, 5.4737065e-07f, 5.8294187e-07f,
+	6.2082472e-07f, 6.6116941e-07f, 7.0413592e-07f, 7.4989464e-07f,
+	7.9862701e-07f, 8.5052630e-07f, 9.0579828e-07f, 9.6466216e-07f,
+	1.0273513e-06f, 1.0941144e-06f, 1.1652161e-06f, 1.2409384e-06f,
+	1.3215816e-06f, 1.4074654e-06f, 1.4989305e-06f, 1.5963394e-06f,
+	1.7000785e-06f, 1.8105592e-06f, 1.9282195e-06f, 2.0535261e-06f,
+	2.1869758e-06f, 2.3290978e-06f, 2.4804557e-06f, 2.6416497e-06f,
+	2.8133190e-06f, 2.9961443e-06f, 3.1908506e-06f, 3.3982101e-06f,
+	3.6190449e-06f, 3.8542308e-06f, 4.1047004e-06f, 4.3714470e-06f,
+	4.6555282e-06f, 4.9580707e-06f, 5.2802740e-06f, 5.6234160e-06f,
+	5.9888572e-06f, 6.3780469e-06f, 6.7925283e-06f, 7.2339451e-06f,
+	7.7040476e-06f, 8.2047000e-06f, 8.7378876e-06f, 9.3057248e-06f,
+	9.9104632e-06f, 1.0554501e-05f, 1.1240392e-05f, 1.1970856e-05f,
+	1.2748789e-05f, 1.3577278e-05f, 1.4459606e-05f, 1.5399272e-05f,
+	1.6400004e-05f, 1.7465768e-05f, 1.8600792e-05f, 1.9809576e-05f,
+	2.1096914e-05f, 2.2467911e-05f, 2.3928002e-05f, 2.5482978e-05f,
+	2.7139006e-05f, 2.8902651e-05f, 3.0780908e-05f, 3.2781225e-05f,
+	3.4911534e-05f, 3.7180282e-05f, 3.9596466e-05f, 4.2169667e-05f,
+	4.4910090e-05f, 4.7828601e-05f, 5.0936773e-05f, 5.4246931e-05f,
+	5.7772202e-05f, 6.1526565e-05f, 6.5524908e-05f, 6.9783085e-05f,
+	7.4317983e-05f, 7.9147585e-05f, 8.4291040e-05f, 8.9768747e-05f,
+	9.5602426e-05f, 0.00010181521f, 0.00010843174f, 0.00011547824f,
+	0.00012298267f, 0.00013097477f, 0.00013948625f, 0.00014855085f,
+	0.00015820453f, 0.00016848555f, 0.00017943469f, 0.00019109536f,
+	0.00020351382f, 0.00021673929f, 0.00023082423f, 0.00024582449f,
+	0.00026179955f, 0.00027881276f, 0.00029693158f, 0.00031622787f,
+	0.00033677814f, 0.00035866388f, 0.00038197188f, 0.00040679456f,
+	0.00043323036f, 0.00046138411f, 0.00049136745f, 0.00052329927f,
+	0.00055730621f, 0.00059352311f, 0.00063209358f, 0.00067317058f,
+	0.00071691700f, 0.00076350630f, 0.00081312324f, 0.00086596457f,
+	0.00092223983f, 0.00098217216f, 0.0010459992f,  0.0011139742f,
+	0.0011863665f,  0.0012634633f,  0.0013455702f,  0.0014330129f,
+	0.0015261382f,  0.0016253153f,  0.0017309374f,  0.0018434235f,
+	0.0019632195f,  0.0020908006f,  0.0022266726f,  0.0023713743f,
+	0.0025254795f,  0.0026895994f,  0.0028643847f,  0.0030505286f,
+	0.0032487691f,  0.0034598925f,  0.0036847358f,  0.0039241906f,
+	0.0041792066f,  0.0044507950f,  0.0047400328f,  0.0050480668f,
+	0.0053761186f,  0.0057254891f,  0.0060975636f,  0.0064938176f,
+	0.0069158225f,  0.0073652516f,  0.0078438871f,  0.0083536271f,
+	0.0088964928f,  0.009474637f,   0.010090352f,   0.010746080f,
+	0.011444421f,   0.012188144f,   0.012980198f,   0.013823725f,
+	0.014722068f,   0.015678791f,   0.016697687f,   0.017782797f,
+	0.018938423f,   0.020169149f,   0.021479854f,   0.022875735f,
+	0.024362330f,   0.025945531f,   0.027631618f,   0.029427276f,
+	0.031339626f,   0.033376252f,   0.035545228f,   0.037855157f,
+	0.040315199f,   0.042935108f,   0.045725273f,   0.048696758f,
+	0.051861348f,   0.055231591f,   0.058820850f,   0.062643361f,
+	0.066714279f,   0.071049749f,   0.075666962f,   0.080584227f,
+	0.085821044f,   0.091398179f,   0.097337747f,   0.10366330f,
+	0.11039993f,    0.11757434f,    0.12521498f,    0.13335215f,
+	0.14201813f,    0.15124727f,    0.16107617f,    0.17154380f,
+	0.18269168f,    0.19456402f,    0.20720788f,    0.22067342f,
+	0.23501402f,    0.25028656f,    0.26655159f,    0.28387361f,
+	0.30232132f,    0.32196786f,    0.34289114f,    0.36517414f,
+	0.38890521f,    0.41417847f,    0.44109412f,    0.46975890f,
+	0.50028648f,    0.53279791f,    0.56742212f,    0.60429640f,
+	0.64356699f,    0.68538959f,    0.72993007f,    0.77736504f,
+	0.82788260f,    0.88168307f,    0.9389798f,     1.0f
+};
+
+
+// @OPTIMIZE: if you want to replace this bresenham line-drawing routine,
+// note that you must produce bit-identical output to decode correctly;
+// this specific sequence of operations is specified in the spec (it's
+// drawing integer-quantized frequency-space lines that the encoder
+// expects to be exactly the same)
+//     ... also, isn't the whole point of Bresenham's algorithm to NOT
+// have to divide in the setup? sigh.
+#ifndef STB_VORBIS_NO_DEFER_FLOOR
+#define LINE_OP(a,b)   a *= b
+#else
+#define LINE_OP(a,b)   a = b
+#endif
+
+#ifdef STB_VORBIS_DIVIDE_TABLE
+#define DIVTAB_NUMER   32
+#define DIVTAB_DENOM   64
+int8 integer_divide_table[DIVTAB_NUMER][DIVTAB_DENOM]; // 2KB
+#endif
+
+static __forceinline void draw_line(float *output, int x0, int y0, int x1, int y1, int n) {
+	int dy = y1 - y0;
+	int adx = x1 - x0;
+	int ady = abs(dy);
+	int base;
+	int x = x0, y = y0;
+	int err = 0;
+	int sy;
+
+#ifdef STB_VORBIS_DIVIDE_TABLE
+	if (adx < DIVTAB_DENOM && ady < DIVTAB_NUMER) {
+		if (dy < 0) {
+			base = -integer_divide_table[ady][adx];
+			sy = base - 1;
+		} else {
+			base = integer_divide_table[ady][adx];
+			sy = base + 1;
+		}
+	} else {
+		base = dy / adx;
+		if (dy < 0)
+			sy = base - 1;
+		else
+			sy = base + 1;
+	}
+#else
+	base = dy / adx;
+	if (dy < 0)
+		sy = base - 1;
+	else
+		sy = base + 1;
+#endif
+	ady -= abs(base) * adx;
+	if (x1 > n) x1 = n;
+	if (x < x1) {
+		LINE_OP(output[x], inverse_db_table[y]);
+		for (++x; x < x1; ++x) {
+			err += ady;
+			if (err >= adx) {
+				err -= adx;
+				y += sy;
+			} else
+				y += base;
+			LINE_OP(output[x], inverse_db_table[y]);
+		}
+	}
+}
+
+static int residue_decode(vorb *f, Codebook *book, float *target, int offset, int n, int rtype) {
+	int k;
+	if (rtype == 0) {
+		int step = n / book->dimensions;
+		for (k = 0; k < step; ++k)
+			if (!codebook_decode_step(f, book, target + offset + k, n - offset - k, step))
+				return FALSE;
+	} else {
+		for (k = 0; k < n; ) {
+			if (!codebook_decode(f, book, target + offset, n - k))
+				return FALSE;
+			k += book->dimensions;
+			offset += book->dimensions;
+		}
+	}
+	return TRUE;
+}
+
+static void decode_residue(vorb *f, float *residue_buffers[], int ch, int n, int rn, uint8 *do_not_decode) {
+	int i, j, pass;
+	Residue *r = f->residue_config + rn;
+	int rtype = f->residue_types[rn];
+	int c = r->classbook;
+	int classwords = f->codebooks[c].dimensions;
+	int n_read = r->end - r->begin;
+	int part_read = n_read / r->part_size;
+	int temp_alloc_point = temp_alloc_save(f);
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+	uint8 ***part_classdata = (uint8 ***) temp_block_array(f, f->channels, part_read * sizeof(**part_classdata));
+#else
+	int **classifications = (int **) temp_block_array(f, f->channels, part_read * sizeof(**classifications));
+#endif
+
+	CHECK(f);
+
+	for (i = 0; i < ch; ++i)
+		if (!do_not_decode[i])
+			memset(residue_buffers[i], 0, sizeof(float) * n);
+
+	if (rtype == 2 && ch != 1) {
+		for (j = 0; j < ch; ++j)
+			if (!do_not_decode[j])
+				break;
+		if (j == ch)
+			goto done;
+
+		for (pass = 0; pass < 8; ++pass) {
+			int pcount = 0, class_set = 0;
+			if (ch == 2) {
+				while (pcount < part_read) {
+					int z = r->begin + pcount*r->part_size;
+					int c_inter = (z & 1), p_inter = z >> 1;
+					if (pass == 0) {
+						Codebook *c = f->codebooks + r->classbook;
+						int q;
+						DECODE(q, f, c);
+						if (q == EOP) goto done;
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+						part_classdata[0][class_set] = r->classdata[q];
+#else
+						for (i = classwords - 1; i >= 0; --i) {
+							classifications[0][i + pcount] = q % r->classifications;
+							q /= r->classifications;
+						}
+#endif
+					}
+					for (i = 0; i < classwords && pcount < part_read; ++i, ++pcount) {
+						int z = r->begin + pcount*r->part_size;
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+						int c = part_classdata[0][class_set][i];
+#else
+						int c = classifications[0][pcount];
+#endif
+						int b = r->residue_books[c][pass];
+						if (b >= 0) {
+							Codebook *book = f->codebooks + b;
+#ifdef STB_VORBIS_DIVIDES_IN_CODEBOOK
+							if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
+								goto done;
+#else
+							// saves 1%
+							if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
+								goto done;
+#endif
+						} else {
+							z += r->part_size;
+							c_inter = z & 1;
+							p_inter = z >> 1;
+						}
+					}
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+					++class_set;
+#endif
+				}
+			} else if (ch == 1) {
+				while (pcount < part_read) {
+					int z = r->begin + pcount*r->part_size;
+					int c_inter = 0, p_inter = z;
+					if (pass == 0) {
+						Codebook *c = f->codebooks + r->classbook;
+						int q;
+						DECODE(q, f, c);
+						if (q == EOP) goto done;
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+						part_classdata[0][class_set] = r->classdata[q];
+#else
+						for (i = classwords - 1; i >= 0; --i) {
+							classifications[0][i + pcount] = q % r->classifications;
+							q /= r->classifications;
+						}
+#endif
+					}
+					for (i = 0; i < classwords && pcount < part_read; ++i, ++pcount) {
+						int z = r->begin + pcount*r->part_size;
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+						int c = part_classdata[0][class_set][i];
+#else
+						int c = classifications[0][pcount];
+#endif
+						int b = r->residue_books[c][pass];
+						if (b >= 0) {
+							Codebook *book = f->codebooks + b;
+							if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
+								goto done;
+						} else {
+							z += r->part_size;
+							c_inter = 0;
+							p_inter = z;
+						}
+					}
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+					++class_set;
+#endif
+				}
+			} else {
+				while (pcount < part_read) {
+					int z = r->begin + pcount*r->part_size;
+					int c_inter = z % ch, p_inter = z / ch;
+					if (pass == 0) {
+						Codebook *c = f->codebooks + r->classbook;
+						int q;
+						DECODE(q, f, c);
+						if (q == EOP) goto done;
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+						part_classdata[0][class_set] = r->classdata[q];
+#else
+						for (i = classwords - 1; i >= 0; --i) {
+							classifications[0][i + pcount] = q % r->classifications;
+							q /= r->classifications;
+						}
+#endif
+					}
+					for (i = 0; i < classwords && pcount < part_read; ++i, ++pcount) {
+						int z = r->begin + pcount*r->part_size;
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+						int c = part_classdata[0][class_set][i];
+#else
+						int c = classifications[0][pcount];
+#endif
+						int b = r->residue_books[c][pass];
+						if (b >= 0) {
+							Codebook *book = f->codebooks + b;
+							if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
+								goto done;
+						} else {
+							z += r->part_size;
+							c_inter = z % ch;
+							p_inter = z / ch;
+						}
+					}
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+					++class_set;
+#endif
+				}
+			}
+		}
+		goto done;
+	}
+	CHECK(f);
+
+	for (pass = 0; pass < 8; ++pass) {
+		int pcount = 0, class_set = 0;
+		while (pcount < part_read) {
+			if (pass == 0) {
+				for (j = 0; j < ch; ++j) {
+					if (!do_not_decode[j]) {
+						Codebook *c = f->codebooks + r->classbook;
+						int temp;
+						DECODE(temp, f, c);
+						if (temp == EOP) goto done;
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+						part_classdata[j][class_set] = r->classdata[temp];
+#else
+						for (i = classwords - 1; i >= 0; --i) {
+							classifications[j][i + pcount] = temp % r->classifications;
+							temp /= r->classifications;
+						}
+#endif
+					}
+				}
+			}
+			for (i = 0; i < classwords && pcount < part_read; ++i, ++pcount) {
+				for (j = 0; j < ch; ++j) {
+					if (!do_not_decode[j]) {
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+						int c = part_classdata[j][class_set][i];
+#else
+						int c = classifications[j][pcount];
+#endif
+						int b = r->residue_books[c][pass];
+						if (b >= 0) {
+							float *target = residue_buffers[j];
+							int offset = r->begin + pcount * r->part_size;
+							int n = r->part_size;
+							Codebook *book = f->codebooks + b;
+							if (!residue_decode(f, book, target, offset, n, rtype))
+								goto done;
+						}
+					}
+				}
+			}
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+			++class_set;
+#endif
+		}
+	}
+done:
+	CHECK(f);
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+	temp_free(f, part_classdata);
+#else
+	temp_free(f, classifications);
+#endif
+	temp_alloc_restore(f, temp_alloc_point);
+}
+
+
+#if 0
+// slow way for debugging
+void inverse_mdct_slow(float *buffer, int n) {
+	int i, j;
+	int n2 = n >> 1;
+	float *x = (float *) malloc(sizeof(*x) * n2);
+	memcpy(x, buffer, sizeof(*x) * n2);
+	for (i = 0; i < n; ++i) {
+		float acc = 0;
+		for (j = 0; j < n2; ++j)
+			// formula from paper:
+			//acc += n/4.0f * x[j] * (float) cos(M_PI / 2 / n * (2 * i + 1 + n/2.0)*(2*j+1));
+			// formula from wikipedia
+			//acc += 2.0f / n2 * x[j] * (float) cos(M_PI/n2 * (i + 0.5 + n2/2)*(j + 0.5));
+			// these are equivalent, except the formula from the paper inverts the multiplier!
+			// however, what actually works is NO MULTIPLIER!?!
+			//acc += 64 * 2.0f / n2 * x[j] * (float) cos(M_PI/n2 * (i + 0.5 + n2/2)*(j + 0.5));
+			acc += x[j] * (float) cos(M_PI / 2 / n * (2 * i + 1 + n / 2.0)*(2 * j + 1));
+		buffer[i] = acc;
+	}
+	free(x);
+}
+#elif 0
+// same as above, but just barely able to run in real time on modern machines
+void inverse_mdct_slow(float *buffer, int n, vorb *f, int blocktype) {
+	float mcos[16384];
+	int i, j;
+	int n2 = n >> 1, nmask = (n << 2) - 1;
+	float *x = (float *) malloc(sizeof(*x) * n2);
+	memcpy(x, buffer, sizeof(*x) * n2);
+	for (i = 0; i < 4 * n; ++i)
+		mcos[i] = (float) cos(M_PI / 2 * i / n);
+
+	for (i = 0; i < n; ++i) {
+		float acc = 0;
+		for (j = 0; j < n2; ++j)
+			acc += x[j] * mcos[(2 * i + 1 + n2)*(2 * j + 1) & nmask];
+		buffer[i] = acc;
+	}
+	free(x);
+}
+#elif 0
+// transform to use a slow dct-iv; this is STILL basically trivial,
+// but only requires half as many ops
+void dct_iv_slow(float *buffer, int n) {
+	float mcos[16384];
+	float x[2048];
+	int i, j;
+	int n2 = n >> 1, nmask = (n << 3) - 1;
+	memcpy(x, buffer, sizeof(*x) * n);
+	for (i = 0; i < 8 * n; ++i)
+		mcos[i] = (float) cos(M_PI / 4 * i / n);
+	for (i = 0; i < n; ++i) {
+		float acc = 0;
+		for (j = 0; j < n; ++j)
+			acc += x[j] * mcos[((2 * i + 1)*(2 * j + 1)) & nmask];
+		buffer[i] = acc;
+	}
+}
+
+void inverse_mdct_slow(float *buffer, int n, vorb *f, int blocktype) {
+	int i, n4 = n >> 2, n2 = n >> 1, n3_4 = n - n4;
+	float temp[4096];
+
+	memcpy(temp, buffer, n2 * sizeof(float));
+	dct_iv_slow(temp, n2);  // returns -c'-d, a-b'
+
+	for (i = 0; i < n4; ++i) buffer[i] = temp[i + n4];            // a-b'
+	for (; i < n3_4; ++i) buffer[i] = -temp[n3_4 - i - 1];   // b-a', c+d'
+	for (; i < n; ++i) buffer[i] = -temp[i - n3_4];       // c'+d
+}
+#endif
+
+#ifndef LIBVORBIS_MDCT
+#define LIBVORBIS_MDCT 0
+#endif
+
+#if LIBVORBIS_MDCT
+// directly call the vorbis MDCT using an interface documented
+// by Jeff Roberts... useful for performance comparison
+typedef struct {
+	int n;
+	int log2n;
+
+	float *trig;
+	int   *bitrev;
+
+	float scale;
+} mdct_lookup;
+
+extern void mdct_init(mdct_lookup *lookup, int n);
+extern void mdct_clear(mdct_lookup *l);
+extern void mdct_backward(mdct_lookup *init, float *in, float *out);
+
+mdct_lookup M1, M2;
+
+void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) {
+	mdct_lookup *M;
+	if (M1.n == n) M = &M1;
+	else if (M2.n == n) M = &M2;
+	else if (M1.n == 0) { mdct_init(&M1, n); M = &M1; } else {
+		if (M2.n) __asm int 3;
+		mdct_init(&M2, n);
+		M = &M2;
+	}
+
+	mdct_backward(M, buffer, buffer);
+}
+#endif
+
+
+// the following were split out into separate functions while optimizing;
+// they could be pushed back up but eh. __forceinline showed no change;
+// they're probably already being inlined.
+static void imdct_step3_iter0_loop(int n, float *e, int i_off, int k_off, float *A) {
+	float *ee0 = e + i_off;
+	float *ee2 = ee0 + k_off;
+	int i;
+
+	assert((n & 3) == 0);
+	for (i = (n >> 2); i > 0; --i) {
+		float k00_20, k01_21;
+		k00_20 = ee0[0] - ee2[0];
+		k01_21 = ee0[-1] - ee2[-1];
+		ee0[0] += ee2[0];//ee0[ 0] = ee0[ 0] + ee2[ 0];
+		ee0[-1] += ee2[-1];//ee0[-1] = ee0[-1] + ee2[-1];
+		ee2[0] = k00_20 * A[0] - k01_21 * A[1];
+		ee2[-1] = k01_21 * A[0] + k00_20 * A[1];
+		A += 8;
+
+		k00_20 = ee0[-2] - ee2[-2];
+		k01_21 = ee0[-3] - ee2[-3];
+		ee0[-2] += ee2[-2];//ee0[-2] = ee0[-2] + ee2[-2];
+		ee0[-3] += ee2[-3];//ee0[-3] = ee0[-3] + ee2[-3];
+		ee2[-2] = k00_20 * A[0] - k01_21 * A[1];
+		ee2[-3] = k01_21 * A[0] + k00_20 * A[1];
+		A += 8;
+
+		k00_20 = ee0[-4] - ee2[-4];
+		k01_21 = ee0[-5] - ee2[-5];
+		ee0[-4] += ee2[-4];//ee0[-4] = ee0[-4] + ee2[-4];
+		ee0[-5] += ee2[-5];//ee0[-5] = ee0[-5] + ee2[-5];
+		ee2[-4] = k00_20 * A[0] - k01_21 * A[1];
+		ee2[-5] = k01_21 * A[0] + k00_20 * A[1];
+		A += 8;
+
+		k00_20 = ee0[-6] - ee2[-6];
+		k01_21 = ee0[-7] - ee2[-7];
+		ee0[-6] += ee2[-6];//ee0[-6] = ee0[-6] + ee2[-6];
+		ee0[-7] += ee2[-7];//ee0[-7] = ee0[-7] + ee2[-7];
+		ee2[-6] = k00_20 * A[0] - k01_21 * A[1];
+		ee2[-7] = k01_21 * A[0] + k00_20 * A[1];
+		A += 8;
+		ee0 -= 8;
+		ee2 -= 8;
+	}
+}
+
+static void imdct_step3_inner_r_loop(int lim, float *e, int d0, int k_off, float *A, int k1) {
+	int i;
+	float k00_20, k01_21;
+
+	float *e0 = e + d0;
+	float *e2 = e0 + k_off;
+
+	for (i = lim >> 2; i > 0; --i) {
+		k00_20 = e0[-0] - e2[-0];
+		k01_21 = e0[-1] - e2[-1];
+		e0[-0] += e2[-0];//e0[-0] = e0[-0] + e2[-0];
+		e0[-1] += e2[-1];//e0[-1] = e0[-1] + e2[-1];
+		e2[-0] = (k00_20) *A[0] - (k01_21) * A[1];
+		e2[-1] = (k01_21) *A[0] + (k00_20) * A[1];
+
+		A += k1;
+
+		k00_20 = e0[-2] - e2[-2];
+		k01_21 = e0[-3] - e2[-3];
+		e0[-2] += e2[-2];//e0[-2] = e0[-2] + e2[-2];
+		e0[-3] += e2[-3];//e0[-3] = e0[-3] + e2[-3];
+		e2[-2] = (k00_20) *A[0] - (k01_21) * A[1];
+		e2[-3] = (k01_21) *A[0] + (k00_20) * A[1];
+
+		A += k1;
+
+		k00_20 = e0[-4] - e2[-4];
+		k01_21 = e0[-5] - e2[-5];
+		e0[-4] += e2[-4];//e0[-4] = e0[-4] + e2[-4];
+		e0[-5] += e2[-5];//e0[-5] = e0[-5] + e2[-5];
+		e2[-4] = (k00_20) *A[0] - (k01_21) * A[1];
+		e2[-5] = (k01_21) *A[0] + (k00_20) * A[1];
+
+		A += k1;
+
+		k00_20 = e0[-6] - e2[-6];
+		k01_21 = e0[-7] - e2[-7];
+		e0[-6] += e2[-6];//e0[-6] = e0[-6] + e2[-6];
+		e0[-7] += e2[-7];//e0[-7] = e0[-7] + e2[-7];
+		e2[-6] = (k00_20) *A[0] - (k01_21) * A[1];
+		e2[-7] = (k01_21) *A[0] + (k00_20) * A[1];
+
+		e0 -= 8;
+		e2 -= 8;
+
+		A += k1;
+	}
+}
+
+static void imdct_step3_inner_s_loop(int n, float *e, int i_off, int k_off, float *A, int a_off, int k0) {
+	int i;
+	float A0 = A[0];
+	float A1 = A[0 + 1];
+	float A2 = A[0 + a_off];
+	float A3 = A[0 + a_off + 1];
+	float A4 = A[0 + a_off * 2 + 0];
+	float A5 = A[0 + a_off * 2 + 1];
+	float A6 = A[0 + a_off * 3 + 0];
+	float A7 = A[0 + a_off * 3 + 1];
+
+	float k00, k11;
+
+	float *ee0 = e + i_off;
+	float *ee2 = ee0 + k_off;
+
+	for (i = n; i > 0; --i) {
+		k00 = ee0[0] - ee2[0];
+		k11 = ee0[-1] - ee2[-1];
+		ee0[0] = ee0[0] + ee2[0];
+		ee0[-1] = ee0[-1] + ee2[-1];
+		ee2[0] = (k00) * A0 - (k11) * A1;
+		ee2[-1] = (k11) * A0 + (k00) * A1;
+
+		k00 = ee0[-2] - ee2[-2];
+		k11 = ee0[-3] - ee2[-3];
+		ee0[-2] = ee0[-2] + ee2[-2];
+		ee0[-3] = ee0[-3] + ee2[-3];
+		ee2[-2] = (k00) * A2 - (k11) * A3;
+		ee2[-3] = (k11) * A2 + (k00) * A3;
+
+		k00 = ee0[-4] - ee2[-4];
+		k11 = ee0[-5] - ee2[-5];
+		ee0[-4] = ee0[-4] + ee2[-4];
+		ee0[-5] = ee0[-5] + ee2[-5];
+		ee2[-4] = (k00) * A4 - (k11) * A5;
+		ee2[-5] = (k11) * A4 + (k00) * A5;
+
+		k00 = ee0[-6] - ee2[-6];
+		k11 = ee0[-7] - ee2[-7];
+		ee0[-6] = ee0[-6] + ee2[-6];
+		ee0[-7] = ee0[-7] + ee2[-7];
+		ee2[-6] = (k00) * A6 - (k11) * A7;
+		ee2[-7] = (k11) * A6 + (k00) * A7;
+
+		ee0 -= k0;
+		ee2 -= k0;
+	}
+}
+
+static __forceinline void iter_54(float *z) {
+	float k00, k11, k22, k33;
+	float y0, y1, y2, y3;
+
+	k00 = z[0] - z[-4];
+	y0 = z[0] + z[-4];
+	y2 = z[-2] + z[-6];
+	k22 = z[-2] - z[-6];
+
+	z[-0] = y0 + y2;      // z0 + z4 + z2 + z6
+	z[-2] = y0 - y2;      // z0 + z4 - z2 - z6
+
+						  // done with y0,y2
+
+	k33 = z[-3] - z[-7];
+
+	z[-4] = k00 + k33;    // z0 - z4 + z3 - z7
+	z[-6] = k00 - k33;    // z0 - z4 - z3 + z7
+
+						  // done with k33
+
+	k11 = z[-1] - z[-5];
+	y1 = z[-1] + z[-5];
+	y3 = z[-3] + z[-7];
+
+	z[-1] = y1 + y3;      // z1 + z5 + z3 + z7
+	z[-3] = y1 - y3;      // z1 + z5 - z3 - z7
+	z[-5] = k11 - k22;    // z1 - z5 + z2 - z6
+	z[-7] = k11 + k22;    // z1 - z5 - z2 + z6
+}
+
+static void imdct_step3_inner_s_loop_ld654(int n, float *e, int i_off, float *A, int base_n) {
+	int a_off = base_n >> 3;
+	float A2 = A[0 + a_off];
+	float *z = e + i_off;
+	float *base = z - 16 * n;
+
+	while (z > base) {
+		float k00, k11;
+
+		k00 = z[-0] - z[-8];
+		k11 = z[-1] - z[-9];
+		z[-0] = z[-0] + z[-8];
+		z[-1] = z[-1] + z[-9];
+		z[-8] = k00;
+		z[-9] = k11;
+
+		k00 = z[-2] - z[-10];
+		k11 = z[-3] - z[-11];
+		z[-2] = z[-2] + z[-10];
+		z[-3] = z[-3] + z[-11];
+		z[-10] = (k00 + k11) * A2;
+		z[-11] = (k11 - k00) * A2;
+
+		k00 = z[-12] - z[-4];  // reverse to avoid a unary negation
+		k11 = z[-5] - z[-13];
+		z[-4] = z[-4] + z[-12];
+		z[-5] = z[-5] + z[-13];
+		z[-12] = k11;
+		z[-13] = k00;
+
+		k00 = z[-14] - z[-6];  // reverse to avoid a unary negation
+		k11 = z[-7] - z[-15];
+		z[-6] = z[-6] + z[-14];
+		z[-7] = z[-7] + z[-15];
+		z[-14] = (k00 + k11) * A2;
+		z[-15] = (k00 - k11) * A2;
+
+		iter_54(z);
+		iter_54(z - 8);
+		z -= 16;
+	}
+}
+
+static void inverse_mdct(float *buffer, int n, vorb *f, int blocktype) {
+	int n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, l;
+	int ld;
+	// @OPTIMIZE: reduce register pressure by using fewer variables?
+	int save_point = temp_alloc_save(f);
+	float *buf2 = (float *) temp_alloc(f, n2 * sizeof(*buf2));
+	float *u = NULL, *v = NULL;
+	// twiddle factors
+	float *A = f->A[blocktype];
+
+	// IMDCT algorithm from "The use of multirate filter banks for coding of high quality digital audio"
+	// See notes about bugs in that paper in less-optimal implementation 'inverse_mdct_old' after this function.
+
+	// kernel from paper
+
+
+	// merged:
+	//   copy and reflect spectral data
+	//   step 0
+
+	// note that it turns out that the items added together during
+	// this step are, in fact, being added to themselves (as reflected
+	// by step 0). inexplicable inefficiency! this became obvious
+	// once I combined the passes.
+
+	// so there's a missing 'times 2' here (for adding X to itself).
+	// this propogates through linearly to the end, where the numbers
+	// are 1/2 too small, and need to be compensated for.
+
+	{
+		float *d, *e, *AA, *e_stop;
+		d = &buf2[n2 - 2];
+		AA = A;
+		e = &buffer[0];
+		e_stop = &buffer[n2];
+		while (e != e_stop) {
+			d[1] = (e[0] * AA[0] - e[2] * AA[1]);
+			d[0] = (e[0] * AA[1] + e[2] * AA[0]);
+			d -= 2;
+			AA += 2;
+			e += 4;
+		}
+
+		e = &buffer[n2 - 3];
+		while (d >= buf2) {
+			d[1] = (-e[2] * AA[0] - -e[0] * AA[1]);
+			d[0] = (-e[2] * AA[1] + -e[0] * AA[0]);
+			d -= 2;
+			AA += 2;
+			e -= 4;
+		}
+	}
+
+	// now we use symbolic names for these, so that we can
+	// possibly swap their meaning as we change which operations
+	// are in place
+
+	u = buffer;
+	v = buf2;
+
+	// step 2    (paper output is w, now u)
+	// this could be in place, but the data ends up in the wrong
+	// place... _somebody_'s got to swap it, so this is nominated
+	{
+		float *AA = &A[n2 - 8];
+		float *d0, *d1, *e0, *e1;
+
+		e0 = &v[n4];
+		e1 = &v[0];
+
+		d0 = &u[n4];
+		d1 = &u[0];
+
+		while (AA >= A) {
+			float v40_20, v41_21;
+
+			v41_21 = e0[1] - e1[1];
+			v40_20 = e0[0] - e1[0];
+			d0[1] = e0[1] + e1[1];
+			d0[0] = e0[0] + e1[0];
+			d1[1] = v41_21*AA[4] - v40_20*AA[5];
+			d1[0] = v40_20*AA[4] + v41_21*AA[5];
+
+			v41_21 = e0[3] - e1[3];
+			v40_20 = e0[2] - e1[2];
+			d0[3] = e0[3] + e1[3];
+			d0[2] = e0[2] + e1[2];
+			d1[3] = v41_21*AA[0] - v40_20*AA[1];
+			d1[2] = v40_20*AA[0] + v41_21*AA[1];
+
+			AA -= 8;
+
+			d0 += 4;
+			d1 += 4;
+			e0 += 4;
+			e1 += 4;
+		}
+	}
+
+	// step 3
+	ld = ilog(n) - 1; // ilog is off-by-one from normal definitions
+
+					  // optimized step 3:
+
+					  // the original step3 loop can be nested r inside s or s inside r;
+					  // it's written originally as s inside r, but this is dumb when r
+					  // iterates many times, and s few. So I have two copies of it and
+					  // switch between them halfway.
+
+					  // this is iteration 0 of step 3
+	imdct_step3_iter0_loop(n >> 4, u, n2 - 1 - n4 * 0, -(n >> 3), A);
+	imdct_step3_iter0_loop(n >> 4, u, n2 - 1 - n4 * 1, -(n >> 3), A);
+
+	// this is iteration 1 of step 3
+	imdct_step3_inner_r_loop(n >> 5, u, n2 - 1 - n8 * 0, -(n >> 4), A, 16);
+	imdct_step3_inner_r_loop(n >> 5, u, n2 - 1 - n8 * 1, -(n >> 4), A, 16);
+	imdct_step3_inner_r_loop(n >> 5, u, n2 - 1 - n8 * 2, -(n >> 4), A, 16);
+	imdct_step3_inner_r_loop(n >> 5, u, n2 - 1 - n8 * 3, -(n >> 4), A, 16);
+
+	l = 2;
+	for (; l < (ld - 3) >> 1; ++l) {
+		int k0 = n >> (l + 2), k0_2 = k0 >> 1;
+		int lim = 1 << (l + 1);
+		int i;
+		for (i = 0; i < lim; ++i)
+			imdct_step3_inner_r_loop(n >> (l + 4), u, n2 - 1 - k0*i, -k0_2, A, 1 << (l + 3));
+	}
+
+	for (; l < ld - 6; ++l) {
+		int k0 = n >> (l + 2), k1 = 1 << (l + 3), k0_2 = k0 >> 1;
+		int rlim = n >> (l + 6), r;
+		int lim = 1 << (l + 1);
+		int i_off;
+		float *A0 = A;
+		i_off = n2 - 1;
+		for (r = rlim; r > 0; --r) {
+			imdct_step3_inner_s_loop(lim, u, i_off, -k0_2, A0, k1, k0);
+			A0 += k1 * 4;
+			i_off -= 8;
+		}
+	}
+
+	// iterations with count:
+	//   ld-6,-5,-4 all interleaved together
+	//       the big win comes from getting rid of needless flops
+	//         due to the constants on pass 5 & 4 being all 1 and 0;
+	//       combining them to be simultaneous to improve cache made little difference
+	imdct_step3_inner_s_loop_ld654(n >> 5, u, n2 - 1, A, n);
+
+	// output is u
+
+	// step 4, 5, and 6
+	// cannot be in-place because of step 5
+	{
+		uint16 *bitrev = f->bit_reverse[blocktype];
+		// weirdly, I'd have thought reading sequentially and writing
+		// erratically would have been better than vice-versa, but in
+		// fact that's not what my testing showed. (That is, with
+		// j = bitreverse(i), do you read i and write j, or read j and write i.)
+
+		float *d0 = &v[n4 - 4];
+		float *d1 = &v[n2 - 4];
+		while (d0 >= v) {
+			int k4;
+
+			k4 = bitrev[0];
+			d1[3] = u[k4 + 0];
+			d1[2] = u[k4 + 1];
+			d0[3] = u[k4 + 2];
+			d0[2] = u[k4 + 3];
+
+			k4 = bitrev[1];
+			d1[1] = u[k4 + 0];
+			d1[0] = u[k4 + 1];
+			d0[1] = u[k4 + 2];
+			d0[0] = u[k4 + 3];
+
+			d0 -= 4;
+			d1 -= 4;
+			bitrev += 2;
+		}
+	}
+	// (paper output is u, now v)
+
+
+	// data must be in buf2
+	assert(v == buf2);
+
+	// step 7   (paper output is v, now v)
+	// this is now in place
+	{
+		float *C = f->C[blocktype];
+		float *d, *e;
+
+		d = v;
+		e = v + n2 - 4;
+
+		while (d < e) {
+			float a02, a11, b0, b1, b2, b3;
+
+			a02 = d[0] - e[2];
+			a11 = d[1] + e[3];
+
+			b0 = C[1] * a02 + C[0] * a11;
+			b1 = C[1] * a11 - C[0] * a02;
+
+			b2 = d[0] + e[2];
+			b3 = d[1] - e[3];
+
+			d[0] = b2 + b0;
+			d[1] = b3 + b1;
+			e[2] = b2 - b0;
+			e[3] = b1 - b3;
+
+			a02 = d[2] - e[0];
+			a11 = d[3] + e[1];
+
+			b0 = C[3] * a02 + C[2] * a11;
+			b1 = C[3] * a11 - C[2] * a02;
+
+			b2 = d[2] + e[0];
+			b3 = d[3] - e[1];
+
+			d[2] = b2 + b0;
+			d[3] = b3 + b1;
+			e[0] = b2 - b0;
+			e[1] = b1 - b3;
+
+			C += 4;
+			d += 4;
+			e -= 4;
+		}
+	}
+
+	// data must be in buf2
+
+
+	// step 8+decode   (paper output is X, now buffer)
+	// this generates pairs of data a la 8 and pushes them directly through
+	// the decode kernel (pushing rather than pulling) to avoid having
+	// to make another pass later
+
+	// this cannot POSSIBLY be in place, so we refer to the buffers directly
+
+	{
+		float *d0, *d1, *d2, *d3;
+
+		float *B = f->B[blocktype] + n2 - 8;
+		float *e = buf2 + n2 - 8;
+		d0 = &buffer[0];
+		d1 = &buffer[n2 - 4];
+		d2 = &buffer[n2];
+		d3 = &buffer[n - 4];
+		while (e >= v) {
+			float p0, p1, p2, p3;
+
+			p3 = e[6] * B[7] - e[7] * B[6];
+			p2 = -e[6] * B[6] - e[7] * B[7];
+
+			d0[0] = p3;
+			d1[3] = -p3;
+			d2[0] = p2;
+			d3[3] = p2;
+
+			p1 = e[4] * B[5] - e[5] * B[4];
+			p0 = -e[4] * B[4] - e[5] * B[5];
+
+			d0[1] = p1;
+			d1[2] = -p1;
+			d2[1] = p0;
+			d3[2] = p0;
+
+			p3 = e[2] * B[3] - e[3] * B[2];
+			p2 = -e[2] * B[2] - e[3] * B[3];
+
+			d0[2] = p3;
+			d1[1] = -p3;
+			d2[2] = p2;
+			d3[1] = p2;
+
+			p1 = e[0] * B[1] - e[1] * B[0];
+			p0 = -e[0] * B[0] - e[1] * B[1];
+
+			d0[3] = p1;
+			d1[0] = -p1;
+			d2[3] = p0;
+			d3[0] = p0;
+
+			B -= 8;
+			e -= 8;
+			d0 += 4;
+			d2 += 4;
+			d1 -= 4;
+			d3 -= 4;
+		}
+	}
+
+	temp_free(f, buf2);
+	temp_alloc_restore(f, save_point);
+}
+
+#if 0
+// this is the original version of the above code, if you want to optimize it from scratch
+void inverse_mdct_naive(float *buffer, int n) {
+	float s;
+	float A[1 << 12], B[1 << 12], C[1 << 11];
+	int i, k, k2, k4, n2 = n >> 1, n4 = n >> 2, n8 = n >> 3, l;
+	int n3_4 = n - n4, ld;
+	// how can they claim this only uses N words?!
+	// oh, because they're only used sparsely, whoops
+	float u[1 << 13], X[1 << 13], v[1 << 13], w[1 << 13];
+	// set up twiddle factors
+
+	for (k = k2 = 0; k < n4; ++k, k2 += 2) {
+		A[k2] = (float) cos(4 * k*M_PI / n);
+		A[k2 + 1] = (float) -sin(4 * k*M_PI / n);
+		B[k2] = (float) cos((k2 + 1)*M_PI / n / 2);
+		B[k2 + 1] = (float) sin((k2 + 1)*M_PI / n / 2);
+	}
+	for (k = k2 = 0; k < n8; ++k, k2 += 2) {
+		C[k2] = (float) cos(2 * (k2 + 1)*M_PI / n);
+		C[k2 + 1] = (float) -sin(2 * (k2 + 1)*M_PI / n);
+	}
+
+	// IMDCT algorithm from "The use of multirate filter banks for coding of high quality digital audio"
+	// Note there are bugs in that pseudocode, presumably due to them attempting
+	// to rename the arrays nicely rather than representing the way their actual
+	// implementation bounces buffers back and forth. As a result, even in the
+	// "some formulars corrected" version, a direct implementation fails. These
+	// are noted below as "paper bug".
+
+	// copy and reflect spectral data
+	for (k = 0; k < n2; ++k) u[k] = buffer[k];
+	for (; k < n; ++k) u[k] = -buffer[n - k - 1];
+	// kernel from paper
+	// step 1
+	for (k = k2 = k4 = 0; k < n4; k += 1, k2 += 2, k4 += 4) {
+		v[n - k4 - 1] = (u[k4] - u[n - k4 - 1]) * A[k2] - (u[k4 + 2] - u[n - k4 - 3])*A[k2 + 1];
+		v[n - k4 - 3] = (u[k4] - u[n - k4 - 1]) * A[k2 + 1] + (u[k4 + 2] - u[n - k4 - 3])*A[k2];
+	}
+	// step 2
+	for (k = k4 = 0; k < n8; k += 1, k4 += 4) {
+		w[n2 + 3 + k4] = v[n2 + 3 + k4] + v[k4 + 3];
+		w[n2 + 1 + k4] = v[n2 + 1 + k4] + v[k4 + 1];
+		w[k4 + 3] = (v[n2 + 3 + k4] - v[k4 + 3])*A[n2 - 4 - k4] - (v[n2 + 1 + k4] - v[k4 + 1])*A[n2 - 3 - k4];
+		w[k4 + 1] = (v[n2 + 1 + k4] - v[k4 + 1])*A[n2 - 4 - k4] + (v[n2 + 3 + k4] - v[k4 + 3])*A[n2 - 3 - k4];
+	}
+	// step 3
+	ld = ilog(n) - 1; // ilog is off-by-one from normal definitions
+	for (l = 0; l < ld - 3; ++l) {
+		int k0 = n >> (l + 2), k1 = 1 << (l + 3);
+		int rlim = n >> (l + 4), r4, r;
+		int s2lim = 1 << (l + 2), s2;
+		for (r = r4 = 0; r < rlim; r4 += 4, ++r) {
+			for (s2 = 0; s2 < s2lim; s2 += 2) {
+				u[n - 1 - k0*s2 - r4] = w[n - 1 - k0*s2 - r4] + w[n - 1 - k0*(s2 + 1) - r4];
+				u[n - 3 - k0*s2 - r4] = w[n - 3 - k0*s2 - r4] + w[n - 3 - k0*(s2 + 1) - r4];
+				u[n - 1 - k0*(s2 + 1) - r4] = (w[n - 1 - k0*s2 - r4] - w[n - 1 - k0*(s2 + 1) - r4]) * A[r*k1]
+					- (w[n - 3 - k0*s2 - r4] - w[n - 3 - k0*(s2 + 1) - r4]) * A[r*k1 + 1];
+				u[n - 3 - k0*(s2 + 1) - r4] = (w[n - 3 - k0*s2 - r4] - w[n - 3 - k0*(s2 + 1) - r4]) * A[r*k1]
+					+ (w[n - 1 - k0*s2 - r4] - w[n - 1 - k0*(s2 + 1) - r4]) * A[r*k1 + 1];
+			}
+		}
+		if (l + 1 < ld - 3) {
+			// paper bug: ping-ponging of u&w here is omitted
+			memcpy(w, u, sizeof(u));
+		}
+	}
+
+	// step 4
+	for (i = 0; i < n8; ++i) {
+		int j = bit_reverse(i) >> (32 - ld + 3);
+		assert(j < n8);
+		if (i == j) {
+			// paper bug: original code probably swapped in place; if copying,
+			//            need to directly copy in this case
+			int i8 = i << 3;
+			v[i8 + 1] = u[i8 + 1];
+			v[i8 + 3] = u[i8 + 3];
+			v[i8 + 5] = u[i8 + 5];
+			v[i8 + 7] = u[i8 + 7];
+		} else if (i < j) {
+			int i8 = i << 3, j8 = j << 3;
+			v[j8 + 1] = u[i8 + 1], v[i8 + 1] = u[j8 + 1];
+			v[j8 + 3] = u[i8 + 3], v[i8 + 3] = u[j8 + 3];
+			v[j8 + 5] = u[i8 + 5], v[i8 + 5] = u[j8 + 5];
+			v[j8 + 7] = u[i8 + 7], v[i8 + 7] = u[j8 + 7];
+		}
+	}
+	// step 5
+	for (k = 0; k < n2; ++k) {
+		w[k] = v[k * 2 + 1];
+	}
+	// step 6
+	for (k = k2 = k4 = 0; k < n8; ++k, k2 += 2, k4 += 4) {
+		u[n - 1 - k2] = w[k4];
+		u[n - 2 - k2] = w[k4 + 1];
+		u[n3_4 - 1 - k2] = w[k4 + 2];
+		u[n3_4 - 2 - k2] = w[k4 + 3];
+	}
+	// step 7
+	for (k = k2 = 0; k < n8; ++k, k2 += 2) {
+		v[n2 + k2] = (u[n2 + k2] + u[n - 2 - k2] + C[k2 + 1] * (u[n2 + k2] - u[n - 2 - k2]) + C[k2] * (u[n2 + k2 + 1] + u[n - 2 - k2 + 1])) / 2;
+		v[n - 2 - k2] = (u[n2 + k2] + u[n - 2 - k2] - C[k2 + 1] * (u[n2 + k2] - u[n - 2 - k2]) - C[k2] * (u[n2 + k2 + 1] + u[n - 2 - k2 + 1])) / 2;
+		v[n2 + 1 + k2] = (u[n2 + 1 + k2] - u[n - 1 - k2] + C[k2 + 1] * (u[n2 + 1 + k2] + u[n - 1 - k2]) - C[k2] * (u[n2 + k2] - u[n - 2 - k2])) / 2;
+		v[n - 1 - k2] = (-u[n2 + 1 + k2] + u[n - 1 - k2] + C[k2 + 1] * (u[n2 + 1 + k2] + u[n - 1 - k2]) - C[k2] * (u[n2 + k2] - u[n - 2 - k2])) / 2;
+	}
+	// step 8
+	for (k = k2 = 0; k < n4; ++k, k2 += 2) {
+		X[k] = v[k2 + n2] * B[k2] + v[k2 + 1 + n2] * B[k2 + 1];
+		X[n2 - 1 - k] = v[k2 + n2] * B[k2 + 1] - v[k2 + 1 + n2] * B[k2];
+	}
+
+	// decode kernel to output
+	// determined the following value experimentally
+	// (by first figuring out what made inverse_mdct_slow work); then matching that here
+	// (probably vorbis encoder premultiplies by n or n/2, to save it on the decoder?)
+	s = 0.5; // theoretically would be n4
+
+			 // [[[ note! the s value of 0.5 is compensated for by the B[] in the current code,
+			 //     so it needs to use the "old" B values to behave correctly, or else
+			 //     set s to 1.0 ]]]
+	for (i = 0; i < n4; ++i) buffer[i] = s * X[i + n4];
+	for (; i < n3_4; ++i) buffer[i] = -s * X[n3_4 - i - 1];
+	for (; i < n; ++i) buffer[i] = -s * X[i - n3_4];
+}
+#endif
+
+static float *get_window(vorb *f, int len) {
+	len <<= 1;
+	if (len == f->blocksize_0) return f->window[0];
+	if (len == f->blocksize_1) return f->window[1];
+	assert(0);
+	return NULL;
+}
+
+#ifndef STB_VORBIS_NO_DEFER_FLOOR
+typedef int16 YTYPE;
+#else
+typedef int YTYPE;
+#endif
+static int do_floor(vorb *f, Mapping *map, int i, int n, float *target, YTYPE *finalY, uint8 *step2_flag) {
+	int n2 = n >> 1;
+	int s = map->chan[i].mux, floor;
+	floor = map->submap_floor[s];
+	if (f->floor_types[floor] == 0) {
+		return error(f, VORBIS_invalid_stream);
+	} else {
+		Floor1 *g = &f->floor_config[floor].floor1;
+		int j, q;
+		int lx = 0, ly = finalY[0] * g->floor1_multiplier;
+		for (q = 1; q < g->values; ++q) {
+			j = g->sorted_order[q];
+#ifndef STB_VORBIS_NO_DEFER_FLOOR
+			if (finalY[j] >= 0)
+#else
+			if (step2_flag[j])
+#endif
+			{
+				int hy = finalY[j] * g->floor1_multiplier;
+				int hx = g->Xlist[j];
+				if (lx != hx)
+					draw_line(target, lx, ly, hx, hy, n2);
+				CHECK(f);
+				lx = hx, ly = hy;
+			}
+		}
+		if (lx < n2) {
+			// optimization of: draw_line(target, lx,ly, n,ly, n2);
+			for (j = lx; j < n2; ++j)
+				LINE_OP(target[j], inverse_db_table[ly]);
+			CHECK(f);
+		}
+	}
+	return TRUE;
+}
+
+// The meaning of "left" and "right"
+//
+// For a given frame:
+//     we compute samples from 0..n
+//     window_center is n/2
+//     we'll window and mix the samples from left_start to left_end with data from the previous frame
+//     all of the samples from left_end to right_start can be output without mixing; however,
+//        this interval is 0-length except when transitioning between short and long frames
+//     all of the samples from right_start to right_end need to be mixed with the next frame,
+//        which we don't have, so those get saved in a buffer
+//     frame N's right_end-right_start, the number of samples to mix with the next frame,
+//        has to be the same as frame N+1's left_end-left_start (which they are by
+//        construction)
+
+static int vorbis_decode_initial(vorb *f, int *p_left_start, int *p_left_end, int *p_right_start, int *p_right_end, int *mode) {
+	Mode *m;
+	int i, n, prev, next, window_center;
+	f->channel_buffer_start = f->channel_buffer_end = 0;
+
+retry:
+	if (f->eof) return FALSE;
+	if (!maybe_start_packet(f))
+		return FALSE;
+	// check packet type
+	if (get_bits(f, 1) != 0) {
+		if (IS_PUSH_MODE(f))
+			return error(f, VORBIS_bad_packet_type);
+		while (EOP != get8_packet(f));
+		goto retry;
+	}
+
+	if (f->alloc.alloc_buffer)
+		assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
+
+	i = get_bits(f, ilog(f->mode_count - 1));
+	if (i == EOP) return FALSE;
+	if (i >= f->mode_count) return FALSE;
+	*mode = i;
+	m = f->mode_config + i;
+	if (m->blockflag) {
+		n = f->blocksize_1;
+		prev = get_bits(f, 1);
+		next = get_bits(f, 1);
+	} else {
+		prev = next = 0;
+		n = f->blocksize_0;
+	}
+
+	// WINDOWING
+
+	window_center = n >> 1;
+	if (m->blockflag && !prev) {
+		*p_left_start = (n - f->blocksize_0) >> 2;
+		*p_left_end = (n + f->blocksize_0) >> 2;
+	} else {
+		*p_left_start = 0;
+		*p_left_end = window_center;
+	}
+	if (m->blockflag && !next) {
+		*p_right_start = (n * 3 - f->blocksize_0) >> 2;
+		*p_right_end = (n * 3 + f->blocksize_0) >> 2;
+	} else {
+		*p_right_start = window_center;
+		*p_right_end = n;
+	}
+
+	return TRUE;
+}
+
+static int vorbis_decode_packet_rest(vorb *f, int *len, Mode *m, int left_start, int left_end, int right_start, int right_end, int *p_left) {
+	Mapping *map;
+	int i, j, k, n, n2;
+	int zero_channel[256];
+	int really_zero_channel[256];
+
+	// WINDOWING
+
+	n = f->blocksize[m->blockflag];
+	map = &f->mapping[m->mapping];
+
+	// FLOORS
+	n2 = n >> 1;
+
+	CHECK(f);
+
+	for (i = 0; i < f->channels; ++i) {
+		int s = map->chan[i].mux, floor;
+		zero_channel[i] = FALSE;
+		floor = map->submap_floor[s];
+		if (f->floor_types[floor] == 0) {
+			return error(f, VORBIS_invalid_stream);
+		} else {
+			Floor1 *g = &f->floor_config[floor].floor1;
+			if (get_bits(f, 1)) {
+				short *finalY;
+				uint8 step2_flag[256];
+				static int range_list[4] = {256, 128, 86, 64};
+				int range = range_list[g->floor1_multiplier - 1];
+				int offset = 2;
+				finalY = f->finalY[i];
+				finalY[0] = get_bits(f, ilog(range) - 1);
+				finalY[1] = get_bits(f, ilog(range) - 1);
+				for (j = 0; j < g->partitions; ++j) {
+					int pclass = g->partition_class_list[j];
+					int cdim = g->class_dimensions[pclass];
+					int cbits = g->class_subclasses[pclass];
+					int csub = (1 << cbits) - 1;
+					int cval = 0;
+					if (cbits) {
+						Codebook *c = f->codebooks + g->class_masterbooks[pclass];
+						DECODE(cval, f, c);
+					}
+					for (k = 0; k < cdim; ++k) {
+						int book = g->subclass_books[pclass][cval & csub];
+						cval = cval >> cbits;
+						if (book >= 0) {
+							int temp;
+							Codebook *c = f->codebooks + book;
+							DECODE(temp, f, c);
+							finalY[offset++] = temp;
+						} else
+							finalY[offset++] = 0;
+					}
+				}
+				if (f->valid_bits == INVALID_BITS) goto error; // behavior according to spec
+				step2_flag[0] = step2_flag[1] = 1;
+				for (j = 2; j < g->values; ++j) {
+					int low, high, pred, highroom, lowroom, room, val;
+					low = g->neighbors[j][0];
+					high = g->neighbors[j][1];
+					//neighbors(g->Xlist, j, &low, &high);
+					pred = predict_point(g->Xlist[j], g->Xlist[low], g->Xlist[high], finalY[low], finalY[high]);
+					val = finalY[j];
+					highroom = range - pred;
+					lowroom = pred;
+					if (highroom < lowroom)
+						room = highroom * 2;
+					else
+						room = lowroom * 2;
+					if (val) {
+						step2_flag[low] = step2_flag[high] = 1;
+						step2_flag[j] = 1;
+						if (val >= room)
+							if (highroom > lowroom)
+								finalY[j] = val - lowroom + pred;
+							else
+								finalY[j] = pred - val + highroom - 1;
+						else
+							if (val & 1)
+								finalY[j] = pred - ((val + 1) >> 1);
+							else
+								finalY[j] = pred + (val >> 1);
+					} else {
+						step2_flag[j] = 0;
+						finalY[j] = pred;
+					}
+				}
+
+#ifdef STB_VORBIS_NO_DEFER_FLOOR
+				do_floor(f, map, i, n, f->floor_buffers[i], finalY, step2_flag);
+#else
+				// defer final floor computation until _after_ residue
+				for (j = 0; j < g->values; ++j) {
+					if (!step2_flag[j])
+						finalY[j] = -1;
+				}
+#endif
+			} else {
+error:
+				zero_channel[i] = TRUE;
+			}
+			// So we just defer everything else to later
+
+			// at this point we've decoded the floor into buffer
+		}
+	}
+	CHECK(f);
+	// at this point we've decoded all floors
+
+	if (f->alloc.alloc_buffer)
+		assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
+
+	// re-enable coupled channels if necessary
+	memcpy(really_zero_channel, zero_channel, sizeof(really_zero_channel[0]) * f->channels);
+	for (i = 0; i < map->coupling_steps; ++i)
+		if (!zero_channel[map->chan[i].magnitude] || !zero_channel[map->chan[i].angle]) {
+			zero_channel[map->chan[i].magnitude] = zero_channel[map->chan[i].angle] = FALSE;
+		}
+
+	CHECK(f);
+	// RESIDUE DECODE
+	for (i = 0; i < map->submaps; ++i) {
+		float *residue_buffers[STB_VORBIS_MAX_CHANNELS];
+		int r;
+		uint8 do_not_decode[256];
+		int ch = 0;
+		for (j = 0; j < f->channels; ++j) {
+			if (map->chan[j].mux == i) {
+				if (zero_channel[j]) {
+					do_not_decode[ch] = TRUE;
+					residue_buffers[ch] = NULL;
+				} else {
+					do_not_decode[ch] = FALSE;
+					residue_buffers[ch] = f->channel_buffers[j];
+				}
+				++ch;
+			}
+		}
+		r = map->submap_residue[i];
+		decode_residue(f, residue_buffers, ch, n2, r, do_not_decode);
+	}
+
+	if (f->alloc.alloc_buffer)
+		assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
+	CHECK(f);
+
+	// INVERSE COUPLING
+	for (i = map->coupling_steps - 1; i >= 0; --i) {
+		int n2 = n >> 1;
+		float *m = f->channel_buffers[map->chan[i].magnitude];
+		float *a = f->channel_buffers[map->chan[i].angle];
+		for (j = 0; j < n2; ++j) {
+			float a2, m2;
+			if (m[j] > 0)
+				if (a[j] > 0)
+					m2 = m[j], a2 = m[j] - a[j];
+				else
+					a2 = m[j], m2 = m[j] + a[j];
+			else
+				if (a[j] > 0)
+					m2 = m[j], a2 = m[j] + a[j];
+				else
+					a2 = m[j], m2 = m[j] - a[j];
+			m[j] = m2;
+			a[j] = a2;
+		}
+	}
+	CHECK(f);
+
+	// finish decoding the floors
+#ifndef STB_VORBIS_NO_DEFER_FLOOR
+	for (i = 0; i < f->channels; ++i) {
+		if (really_zero_channel[i]) {
+			memset(f->channel_buffers[i], 0, sizeof(*f->channel_buffers[i]) * n2);
+		} else {
+			do_floor(f, map, i, n, f->channel_buffers[i], f->finalY[i], NULL);
+		}
+	}
+#else
+	for (i = 0; i < f->channels; ++i) {
+		if (really_zero_channel[i]) {
+			memset(f->channel_buffers[i], 0, sizeof(*f->channel_buffers[i]) * n2);
+		} else {
+			for (j = 0; j < n2; ++j)
+				f->channel_buffers[i][j] *= f->floor_buffers[i][j];
+		}
+	}
+#endif
+
+	// INVERSE MDCT
+	CHECK(f);
+	for (i = 0; i < f->channels; ++i)
+		inverse_mdct(f->channel_buffers[i], n, f, m->blockflag);
+	CHECK(f);
+
+	// this shouldn't be necessary, unless we exited on an error
+	// and want to flush to get to the next packet
+	flush_packet(f);
+
+	if (f->first_decode) {
+		// assume we start so first non-discarded sample is sample 0
+		// this isn't to spec, but spec would require us to read ahead
+		// and decode the size of all current frames--could be done,
+		// but presumably it's not a commonly used feature
+		f->current_loc = -n2; // start of first frame is positioned for discard
+							  // we might have to discard samples "from" the next frame too,
+							  // if we're lapping a large block then a small at the start?
+		f->discard_samples_deferred = n - right_end;
+		f->current_loc_valid = TRUE;
+		f->first_decode = FALSE;
+	} else if (f->discard_samples_deferred) {
+		if (f->discard_samples_deferred >= right_start - left_start) {
+			f->discard_samples_deferred -= (right_start - left_start);
+			left_start = right_start;
+			*p_left = left_start;
+		} else {
+			left_start += f->discard_samples_deferred;
+			*p_left = left_start;
+			f->discard_samples_deferred = 0;
+		}
+	} else if (f->previous_length == 0 && f->current_loc_valid) {
+		// we're recovering from a seek... that means we're going to discard
+		// the samples from this packet even though we know our position from
+		// the last page header, so we need to update the position based on
+		// the discarded samples here
+		// but wait, the code below is going to add this in itself even
+		// on a discard, so we don't need to do it here...
+	}
+
+	// check if we have ogg information about the sample # for this packet
+	if (f->last_seg_which == f->end_seg_with_known_loc) {
+		// if we have a valid current loc, and this is final:
+		if (f->current_loc_valid && (f->page_flag & PAGEFLAG_last_page)) {
+			uint32 current_end = f->known_loc_for_packet - (n - right_end);
+			// then let's infer the size of the (probably) short final frame
+			if (current_end < f->current_loc + (right_end - left_start)) {
+				if (current_end < f->current_loc) {
+					// negative truncation, that's impossible!
+					*len = 0;
+				} else {
+					*len = current_end - f->current_loc;
+				}
+				*len += left_start;
+				if (*len > right_end) *len = right_end; // this should never happen
+				f->current_loc += *len;
+				return TRUE;
+			}
+		}
+		// otherwise, just set our sample loc
+		// guess that the ogg granule pos refers to the _middle_ of the
+		// last frame?
+		// set f->current_loc to the position of left_start
+		f->current_loc = f->known_loc_for_packet - (n2 - left_start);
+		f->current_loc_valid = TRUE;
+	}
+	if (f->current_loc_valid)
+		f->current_loc += (right_start - left_start);
+
+	if (f->alloc.alloc_buffer)
+		assert(f->alloc.alloc_buffer_length_in_bytes == f->temp_offset);
+	*len = right_end;  // ignore samples after the window goes to 0
+	CHECK(f);
+
+	return TRUE;
+}
+
+static int vorbis_decode_packet(vorb *f, int *len, int *p_left, int *p_right) {
+	int mode, left_end, right_end;
+	if (!vorbis_decode_initial(f, p_left, &left_end, p_right, &right_end, &mode)) return 0;
+	return vorbis_decode_packet_rest(f, len, f->mode_config + mode, *p_left, left_end, *p_right, right_end, p_left);
+}
+
+static int vorbis_finish_frame(stb_vorbis *f, int len, int left, int right) {
+	int prev, i, j;
+	// we use right&left (the start of the right- and left-window sin()-regions)
+	// to determine how much to return, rather than inferring from the rules
+	// (same result, clearer code); 'left' indicates where our sin() window
+	// starts, therefore where the previous window's right edge starts, and
+	// therefore where to start mixing from the previous buffer. 'right'
+	// indicates where our sin() ending-window starts, therefore that's where
+	// we start saving, and where our returned-data ends.
+
+	// mixin from previous window
+	if (f->previous_length) {
+		int i, j, n = f->previous_length;
+		float *w = get_window(f, n);
+		for (i = 0; i < f->channels; ++i) {
+			for (j = 0; j < n; ++j)
+				f->channel_buffers[i][left + j] =
+				f->channel_buffers[i][left + j] * w[j] +
+				f->previous_window[i][j] * w[n - 1 - j];
+		}
+	}
+
+	prev = f->previous_length;
+
+	// last half of this data becomes previous window
+	f->previous_length = len - right;
+
+	// @OPTIMIZE: could avoid this copy by double-buffering the
+	// output (flipping previous_window with channel_buffers), but
+	// then previous_window would have to be 2x as large, and
+	// channel_buffers couldn't be temp mem (although they're NOT
+	// currently temp mem, they could be (unless we want to level
+	// performance by spreading out the computation))
+	for (i = 0; i < f->channels; ++i)
+		for (j = 0; right + j < len; ++j)
+			f->previous_window[i][j] = f->channel_buffers[i][right + j];
+
+	if (!prev)
+		// there was no previous packet, so this data isn't valid...
+		// this isn't entirely true, only the would-have-overlapped data
+		// isn't valid, but this seems to be what the spec requires
+		return 0;
+
+	// truncate a short frame
+	if (len < right) right = len;
+
+	f->samples_output += right - left;
+
+	return right - left;
+}
+
+static void vorbis_pump_first_frame(stb_vorbis *f) {
+	int len, right, left;
+	if (vorbis_decode_packet(f, &len, &left, &right))
+		vorbis_finish_frame(f, len, left, right);
+}
+
+#ifndef STB_VORBIS_NO_PUSHDATA_API
+static int is_whole_packet_present(stb_vorbis *f, int end_page) {
+	// make sure that we have the packet available before continuing...
+	// this requires a full ogg parse, but we know we can fetch from f->stream
+
+	// instead of coding this out explicitly, we could save the current read state,
+	// read the next packet with get8() until end-of-packet, check f->eof, then
+	// reset the state? but that would be slower, esp. since we'd have over 256 bytes
+	// of state to restore (primarily the page segment table)
+
+	int s = f->next_seg, first = TRUE;
+	uint8 *p = f->stream;
+
+	if (s != -1) { // if we're not starting the packet with a 'continue on next page' flag
+		for (; s < f->segment_count; ++s) {
+			p += f->segments[s];
+			if (f->segments[s] < 255)               // stop at first short segment
+				break;
+		}
+		// either this continues, or it ends it...
+		if (end_page)
+			if (s < f->segment_count - 1)             return error(f, VORBIS_invalid_stream);
+		if (s == f->segment_count)
+			s = -1; // set 'crosses page' flag
+		if (p > f->stream_end)                     return error(f, VORBIS_need_more_data);
+		first = FALSE;
+	}
+	for (; s == -1;) {
+		uint8 *q;
+		int n;
+
+		// check that we have the page header ready
+		if (p + 26 >= f->stream_end)               return error(f, VORBIS_need_more_data);
+		// validate the page
+		if (memcmp(p, ogg_page_header, 4))         return error(f, VORBIS_invalid_stream);
+		if (p[4] != 0)                             return error(f, VORBIS_invalid_stream);
+		if (first) { // the first segment must NOT have 'continued_packet', later ones MUST
+			if (f->previous_length)
+				if ((p[5] & PAGEFLAG_continued_packet))  return error(f, VORBIS_invalid_stream);
+			// if no previous length, we're resynching, so we can come in on a continued-packet,
+			// which we'll just drop
+		} else {
+			if (!(p[5] & PAGEFLAG_continued_packet)) return error(f, VORBIS_invalid_stream);
+		}
+		n = p[26]; // segment counts
+		q = p + 27;  // q points to segment table
+		p = q + n; // advance past header
+				   // make sure we've read the segment table
+		if (p > f->stream_end)                     return error(f, VORBIS_need_more_data);
+		for (s = 0; s < n; ++s) {
+			p += q[s];
+			if (q[s] < 255)
+				break;
+		}
+		if (end_page)
+			if (s < n - 1)                            return error(f, VORBIS_invalid_stream);
+		if (s == n)
+			s = -1; // set 'crosses page' flag
+		if (p > f->stream_end)                     return error(f, VORBIS_need_more_data);
+		first = FALSE;
+	}
+	return TRUE;
+}
+#endif // !STB_VORBIS_NO_PUSHDATA_API
+
+static int start_decoder(vorb *f) {
+	uint8 header[6], x, y;
+	int len, i, j, k, max_submaps = 0;
+	int longest_floorlist = 0;
+
+	// first page, first packet
+
+	if (!start_page(f))                              return FALSE;
+	// validate page flag
+	if (!(f->page_flag & PAGEFLAG_first_page))       return error(f, VORBIS_invalid_first_page);
+	if (f->page_flag & PAGEFLAG_last_page)           return error(f, VORBIS_invalid_first_page);
+	if (f->page_flag & PAGEFLAG_continued_packet)    return error(f, VORBIS_invalid_first_page);
+	// check for expected packet length
+	if (f->segment_count != 1)                       return error(f, VORBIS_invalid_first_page);
+	if (f->segments[0] != 30)                        return error(f, VORBIS_invalid_first_page);
+	// read packet
+	// check packet header
+	if (get8(f) != VORBIS_packet_id)                 return error(f, VORBIS_invalid_first_page);
+	if (!getn(f, header, 6))                         return error(f, VORBIS_unexpected_eof);
+	if (!vorbis_validate(header))                    return error(f, VORBIS_invalid_first_page);
+	// vorbis_version
+	if (get32(f) != 0)                               return error(f, VORBIS_invalid_first_page);
+	f->channels = get8(f); if (!f->channels)         return error(f, VORBIS_invalid_first_page);
+	if (f->channels > STB_VORBIS_MAX_CHANNELS)       return error(f, VORBIS_too_many_channels);
+	f->sample_rate = get32(f); if (!f->sample_rate)  return error(f, VORBIS_invalid_first_page);
+	get32(f); // bitrate_maximum
+	get32(f); // bitrate_nominal
+	get32(f); // bitrate_minimum
+	x = get8(f);
+	{
+		int log0, log1;
+		log0 = x & 15;
+		log1 = x >> 4;
+		f->blocksize_0 = 1 << log0;
+		f->blocksize_1 = 1 << log1;
+		if (log0 < 6 || log0 > 13)                       return error(f, VORBIS_invalid_setup);
+		if (log1 < 6 || log1 > 13)                       return error(f, VORBIS_invalid_setup);
+		if (log0 > log1)                                 return error(f, VORBIS_invalid_setup);
+	}
+
+	// framing_flag
+	x = get8(f);
+	if (!(x & 1))                                    return error(f, VORBIS_invalid_first_page);
+
+	// second packet!
+	if (!start_page(f))                              return FALSE;
+
+	if (!start_packet(f))                            return FALSE;
+	do {
+		len = next_segment(f);
+		skip(f, len);
+		f->bytes_in_seg = 0;
+	} while (len);
+
+	// third packet!
+	if (!start_packet(f))                            return FALSE;
+
+#ifndef STB_VORBIS_NO_PUSHDATA_API
+	if (IS_PUSH_MODE(f)) {
+		if (!is_whole_packet_present(f, TRUE)) {
+			// convert error in ogg header to write type
+			if (f->error == VORBIS_invalid_stream)
+				f->error = VORBIS_invalid_setup;
+			return FALSE;
+		}
+	}
+#endif
+
+	crc32_init(); // always init it, to avoid multithread race conditions
+
+	if (get8_packet(f) != VORBIS_packet_setup)       return error(f, VORBIS_invalid_setup);
+	for (i = 0; i < 6; ++i) header[i] = get8_packet(f);
+	if (!vorbis_validate(header))                    return error(f, VORBIS_invalid_setup);
+
+	// codebooks
+
+	f->codebook_count = get_bits(f, 8) + 1;
+	f->codebooks = (Codebook *) setup_malloc(f, sizeof(*f->codebooks) * f->codebook_count);
+	if (f->codebooks == NULL)                        return error(f, VORBIS_outofmem);
+	memset(f->codebooks, 0, sizeof(*f->codebooks) * f->codebook_count);
+	for (i = 0; i < f->codebook_count; ++i) {
+		uint32 *values;
+		int ordered, sorted_count;
+		int total = 0;
+		uint8 *lengths;
+		Codebook *c = f->codebooks + i;
+		CHECK(f);
+		x = get_bits(f, 8); if (x != 0x42)            return error(f, VORBIS_invalid_setup);
+		x = get_bits(f, 8); if (x != 0x43)            return error(f, VORBIS_invalid_setup);
+		x = get_bits(f, 8); if (x != 0x56)            return error(f, VORBIS_invalid_setup);
+		x = get_bits(f, 8);
+		c->dimensions = (get_bits(f, 8) << 8) + x;
+		x = get_bits(f, 8);
+		y = get_bits(f, 8);
+		c->entries = (get_bits(f, 8) << 16) + (y << 8) + x;
+		ordered = get_bits(f, 1);
+		c->sparse = ordered ? 0 : get_bits(f, 1);
+
+		if (c->dimensions == 0 && c->entries != 0)    return error(f, VORBIS_invalid_setup);
+
+		if (c->sparse)
+			lengths = (uint8 *) setup_temp_malloc(f, c->entries);
+		else
+			lengths = c->codeword_lengths = (uint8 *) setup_malloc(f, c->entries);
+
+		if (!lengths) return error(f, VORBIS_outofmem);
+
+		if (ordered) {
+			int current_entry = 0;
+			int current_length = get_bits(f, 5) + 1;
+			while (current_entry < c->entries) {
+				int limit = c->entries - current_entry;
+				int n = get_bits(f, ilog(limit));
+				if (current_entry + n >(int) c->entries) { return error(f, VORBIS_invalid_setup); }
+				memset(lengths + current_entry, current_length, n);
+				current_entry += n;
+				++current_length;
+			}
+		} else {
+			for (j = 0; j < c->entries; ++j) {
+				int present = c->sparse ? get_bits(f, 1) : 1;
+				if (present) {
+					lengths[j] = get_bits(f, 5) + 1;
+					++total;
+					if (lengths[j] == 32)
+						return error(f, VORBIS_invalid_setup);
+				} else {
+					lengths[j] = NO_CODE;
+				}
+			}
+		}
+
+		if (c->sparse && total >= c->entries >> 2) {
+			// convert sparse items to non-sparse!
+			if (c->entries > (int) f->setup_temp_memory_required)
+				f->setup_temp_memory_required = c->entries;
+
+			c->codeword_lengths = (uint8 *) setup_malloc(f, c->entries);
+			if (c->codeword_lengths == NULL) return error(f, VORBIS_outofmem);
+			memcpy(c->codeword_lengths, lengths, c->entries);
+			setup_temp_free(f, lengths, c->entries); // note this is only safe if there have been no intervening temp mallocs!
+			lengths = c->codeword_lengths;
+			c->sparse = 0;
+		}
+
+		// compute the size of the sorted tables
+		if (c->sparse) {
+			sorted_count = total;
+		} else {
+			sorted_count = 0;
+#ifndef STB_VORBIS_NO_HUFFMAN_BINARY_SEARCH
+			for (j = 0; j < c->entries; ++j)
+				if (lengths[j] > STB_VORBIS_FAST_HUFFMAN_LENGTH && lengths[j] != NO_CODE)
+					++sorted_count;
+#endif
+		}
+
+		c->sorted_entries = sorted_count;
+		values = NULL;
+
+		CHECK(f);
+		if (!c->sparse) {
+			c->codewords = (uint32 *) setup_malloc(f, sizeof(c->codewords[0]) * c->entries);
+			if (!c->codewords)                  return error(f, VORBIS_outofmem);
+		} else {
+			unsigned int size;
+			if (c->sorted_entries) {
+				c->codeword_lengths = (uint8 *) setup_malloc(f, c->sorted_entries);
+				if (!c->codeword_lengths)           return error(f, VORBIS_outofmem);
+				c->codewords = (uint32 *) setup_temp_malloc(f, sizeof(*c->codewords) * c->sorted_entries);
+				if (!c->codewords)                  return error(f, VORBIS_outofmem);
+				values = (uint32 *) setup_temp_malloc(f, sizeof(*values) * c->sorted_entries);
+				if (!values)                        return error(f, VORBIS_outofmem);
+			}
+			size = c->entries + (sizeof(*c->codewords) + sizeof(*values)) * c->sorted_entries;
+			if (size > f->setup_temp_memory_required)
+				f->setup_temp_memory_required = size;
+		}
+
+		if (!compute_codewords(c, lengths, c->entries, values)) {
+			if (c->sparse) setup_temp_free(f, values, 0);
+			return error(f, VORBIS_invalid_setup);
+		}
+
+		if (c->sorted_entries) {
+			// allocate an extra slot for sentinels
+			c->sorted_codewords = (uint32 *) setup_malloc(f, sizeof(*c->sorted_codewords) * (c->sorted_entries + 1));
+			if (c->sorted_codewords == NULL) return error(f, VORBIS_outofmem);
+			// allocate an extra slot at the front so that c->sorted_values[-1] is defined
+			// so that we can catch that case without an extra if
+			c->sorted_values = (int   *) setup_malloc(f, sizeof(*c->sorted_values) * (c->sorted_entries + 1));
+			if (c->sorted_values == NULL) return error(f, VORBIS_outofmem);
+			++c->sorted_values;
+			c->sorted_values[-1] = -1;
+			compute_sorted_huffman(c, lengths, values);
+		}
+
+		if (c->sparse) {
+			setup_temp_free(f, values, sizeof(*values)*c->sorted_entries);
+			setup_temp_free(f, c->codewords, sizeof(*c->codewords)*c->sorted_entries);
+			setup_temp_free(f, lengths, c->entries);
+			c->codewords = NULL;
+		}
+
+		compute_accelerated_huffman(c);
+
+		CHECK(f);
+		c->lookup_type = get_bits(f, 4);
+		if (c->lookup_type > 2) return error(f, VORBIS_invalid_setup);
+		if (c->lookup_type > 0) {
+			uint16 *mults;
+			c->minimum_value = float32_unpack(get_bits(f, 32));
+			c->delta_value = float32_unpack(get_bits(f, 32));
+			c->value_bits = get_bits(f, 4) + 1;
+			c->sequence_p = get_bits(f, 1);
+			if (c->lookup_type == 1) {
+				c->lookup_values = lookup1_values(c->entries, c->dimensions);
+			} else {
+				c->lookup_values = c->entries * c->dimensions;
+			}
+			if (c->lookup_values == 0) return error(f, VORBIS_invalid_setup);
+			mults = (uint16 *) setup_temp_malloc(f, sizeof(mults[0]) * c->lookup_values);
+			if (mults == NULL) return error(f, VORBIS_outofmem);
+			for (j = 0; j < (int) c->lookup_values; ++j) {
+				int q = get_bits(f, c->value_bits);
+				if (q == EOP) { setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_invalid_setup); }
+				mults[j] = q;
+			}
+
+#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
+			if (c->lookup_type == 1) {
+				int len, sparse = c->sparse;
+				float last = 0;
+				// pre-expand the lookup1-style multiplicands, to avoid a divide in the inner loop
+				if (sparse) {
+					if (c->sorted_entries == 0) goto skip;
+					c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->sorted_entries * c->dimensions);
+				} else
+					c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->entries        * c->dimensions);
+				if (c->multiplicands == NULL) { setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_outofmem); }
+				len = sparse ? c->sorted_entries : c->entries;
+				for (j = 0; j < len; ++j) {
+					unsigned int z = sparse ? c->sorted_values[j] : j;
+					unsigned int div = 1;
+					for (k = 0; k < c->dimensions; ++k) {
+						int off = (z / div) % c->lookup_values;
+						float val = mults[off];
+						val = mults[off] * c->delta_value + c->minimum_value + last;
+						c->multiplicands[j*c->dimensions + k] = val;
+						if (c->sequence_p)
+							last = val;
+						if (k + 1 < c->dimensions) {
+							if (div > UINT_MAX / (unsigned int) c->lookup_values) {
+								setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values);
+								return error(f, VORBIS_invalid_setup);
+							}
+							div *= c->lookup_values;
+						}
+					}
+				}
+				c->lookup_type = 2;
+			} else
+#endif
+			{
+				float last = 0;
+				CHECK(f);
+				c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->lookup_values);
+				if (c->multiplicands == NULL) { setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values); return error(f, VORBIS_outofmem); }
+				for (j = 0; j < (int) c->lookup_values; ++j) {
+					float val = mults[j] * c->delta_value + c->minimum_value + last;
+					c->multiplicands[j] = val;
+					if (c->sequence_p)
+						last = val;
+				}
+			}
+#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
+			skip : ;
+#endif
+				   setup_temp_free(f, mults, sizeof(mults[0])*c->lookup_values);
+
+				   CHECK(f);
+		}
+		CHECK(f);
+	}
+
+	// time domain transfers (notused)
+
+	x = get_bits(f, 6) + 1;
+	for (i = 0; i < x; ++i) {
+		uint32 z = get_bits(f, 16);
+		if (z != 0) return error(f, VORBIS_invalid_setup);
+	}
+
+	// Floors
+	f->floor_count = get_bits(f, 6) + 1;
+	f->floor_config = (Floor *) setup_malloc(f, f->floor_count * sizeof(*f->floor_config));
+	if (f->floor_config == NULL) return error(f, VORBIS_outofmem);
+	for (i = 0; i < f->floor_count; ++i) {
+		f->floor_types[i] = get_bits(f, 16);
+		if (f->floor_types[i] > 1) return error(f, VORBIS_invalid_setup);
+		if (f->floor_types[i] == 0) {
+			Floor0 *g = &f->floor_config[i].floor0;
+			g->order = get_bits(f, 8);
+			g->rate = get_bits(f, 16);
+			g->bark_map_size = get_bits(f, 16);
+			g->amplitude_bits = get_bits(f, 6);
+			g->amplitude_offset = get_bits(f, 8);
+			g->number_of_books = get_bits(f, 4) + 1;
+			for (j = 0; j < g->number_of_books; ++j)
+				g->book_list[j] = get_bits(f, 8);
+			return error(f, VORBIS_feature_not_supported);
+		} else {
+			Point p[31 * 8 + 2];
+			Floor1 *g = &f->floor_config[i].floor1;
+			int max_class = -1;
+			g->partitions = get_bits(f, 5);
+			for (j = 0; j < g->partitions; ++j) {
+				g->partition_class_list[j] = get_bits(f, 4);
+				if (g->partition_class_list[j] > max_class)
+					max_class = g->partition_class_list[j];
+			}
+			for (j = 0; j <= max_class; ++j) {
+				g->class_dimensions[j] = get_bits(f, 3) + 1;
+				g->class_subclasses[j] = get_bits(f, 2);
+				if (g->class_subclasses[j]) {
+					g->class_masterbooks[j] = get_bits(f, 8);
+					if (g->class_masterbooks[j] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
+				}
+				for (k = 0; k < 1 << g->class_subclasses[j]; ++k) {
+					g->subclass_books[j][k] = get_bits(f, 8) - 1;
+					if (g->subclass_books[j][k] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
+				}
+			}
+			g->floor1_multiplier = get_bits(f, 2) + 1;
+			g->rangebits = get_bits(f, 4);
+			g->Xlist[0] = 0;
+			g->Xlist[1] = 1 << g->rangebits;
+			g->values = 2;
+			for (j = 0; j < g->partitions; ++j) {
+				int c = g->partition_class_list[j];
+				for (k = 0; k < g->class_dimensions[c]; ++k) {
+					g->Xlist[g->values] = get_bits(f, g->rangebits);
+					++g->values;
+				}
+			}
+			// precompute the sorting
+			for (j = 0; j < g->values; ++j) {
+				p[j].x = g->Xlist[j];
+				p[j].y = j;
+			}
+			qsort(p, g->values, sizeof(p[0]), point_compare);
+			for (j = 0; j < g->values; ++j)
+				g->sorted_order[j] = (uint8) p[j].y;
+			// precompute the neighbors
+			for (j = 2; j < g->values; ++j) {
+				int low, hi;
+				neighbors(g->Xlist, j, &low, &hi);
+				g->neighbors[j][0] = low;
+				g->neighbors[j][1] = hi;
+			}
+
+			if (g->values > longest_floorlist)
+				longest_floorlist = g->values;
+		}
+	}
+
+	// Residue
+	f->residue_count = get_bits(f, 6) + 1;
+	f->residue_config = (Residue *) setup_malloc(f, f->residue_count * sizeof(f->residue_config[0]));
+	if (f->residue_config == NULL) return error(f, VORBIS_outofmem);
+	memset(f->residue_config, 0, f->residue_count * sizeof(f->residue_config[0]));
+	for (i = 0; i < f->residue_count; ++i) {
+		uint8 residue_cascade[64];
+		Residue *r = f->residue_config + i;
+		f->residue_types[i] = get_bits(f, 16);
+		if (f->residue_types[i] > 2) return error(f, VORBIS_invalid_setup);
+		r->begin = get_bits(f, 24);
+		r->end = get_bits(f, 24);
+		if (r->end < r->begin) return error(f, VORBIS_invalid_setup);
+		r->part_size = get_bits(f, 24) + 1;
+		r->classifications = get_bits(f, 6) + 1;
+		r->classbook = get_bits(f, 8);
+		if (r->classbook >= f->codebook_count) return error(f, VORBIS_invalid_setup);
+		for (j = 0; j < r->classifications; ++j) {
+			uint8 high_bits = 0;
+			uint8 low_bits = get_bits(f, 3);
+			if (get_bits(f, 1))
+				high_bits = get_bits(f, 5);
+			residue_cascade[j] = high_bits * 8 + low_bits;
+		}
+		r->residue_books = (short(*)[8]) setup_malloc(f, sizeof(r->residue_books[0]) * r->classifications);
+		if (r->residue_books == NULL) return error(f, VORBIS_outofmem);
+		for (j = 0; j < r->classifications; ++j) {
+			for (k = 0; k < 8; ++k) {
+				if (residue_cascade[j] & (1 << k)) {
+					r->residue_books[j][k] = get_bits(f, 8);
+					if (r->residue_books[j][k] >= f->codebook_count) return error(f, VORBIS_invalid_setup);
+				} else {
+					r->residue_books[j][k] = -1;
+				}
+			}
+		}
+		// precompute the classifications[] array to avoid inner-loop mod/divide
+		// call it 'classdata' since we already have r->classifications
+		r->classdata = (uint8 **) setup_malloc(f, sizeof(*r->classdata) * f->codebooks[r->classbook].entries);
+		if (!r->classdata) return error(f, VORBIS_outofmem);
+		memset(r->classdata, 0, sizeof(*r->classdata) * f->codebooks[r->classbook].entries);
+		for (j = 0; j < f->codebooks[r->classbook].entries; ++j) {
+			int classwords = f->codebooks[r->classbook].dimensions;
+			int temp = j;
+			r->classdata[j] = (uint8 *) setup_malloc(f, sizeof(r->classdata[j][0]) * classwords);
+			if (r->classdata[j] == NULL) return error(f, VORBIS_outofmem);
+			for (k = classwords - 1; k >= 0; --k) {
+				r->classdata[j][k] = temp % r->classifications;
+				temp /= r->classifications;
+			}
+		}
+	}
+
+	f->mapping_count = get_bits(f, 6) + 1;
+	f->mapping = (Mapping *) setup_malloc(f, f->mapping_count * sizeof(*f->mapping));
+	if (f->mapping == NULL) return error(f, VORBIS_outofmem);
+	memset(f->mapping, 0, f->mapping_count * sizeof(*f->mapping));
+	for (i = 0; i < f->mapping_count; ++i) {
+		Mapping *m = f->mapping + i;
+		int mapping_type = get_bits(f, 16);
+		if (mapping_type != 0) return error(f, VORBIS_invalid_setup);
+		m->chan = (MappingChannel *) setup_malloc(f, f->channels * sizeof(*m->chan));
+		if (m->chan == NULL) return error(f, VORBIS_outofmem);
+		if (get_bits(f, 1))
+			m->submaps = get_bits(f, 4) + 1;
+		else
+			m->submaps = 1;
+		if (m->submaps > max_submaps)
+			max_submaps = m->submaps;
+		if (get_bits(f, 1)) {
+			m->coupling_steps = get_bits(f, 8) + 1;
+			for (k = 0; k < m->coupling_steps; ++k) {
+				m->chan[k].magnitude = get_bits(f, ilog(f->channels - 1));
+				m->chan[k].angle = get_bits(f, ilog(f->channels - 1));
+				if (m->chan[k].magnitude >= f->channels)        return error(f, VORBIS_invalid_setup);
+				if (m->chan[k].angle >= f->channels)        return error(f, VORBIS_invalid_setup);
+				if (m->chan[k].magnitude == m->chan[k].angle)   return error(f, VORBIS_invalid_setup);
+			}
+		} else
+			m->coupling_steps = 0;
+
+		// reserved field
+		if (get_bits(f, 2)) return error(f, VORBIS_invalid_setup);
+		if (m->submaps > 1) {
+			for (j = 0; j < f->channels; ++j) {
+				m->chan[j].mux = get_bits(f, 4);
+				if (m->chan[j].mux >= m->submaps)                return error(f, VORBIS_invalid_setup);
+			}
+		} else
+			// @SPECIFICATION: this case is missing from the spec
+			for (j = 0; j < f->channels; ++j)
+				m->chan[j].mux = 0;
+
+		for (j = 0; j < m->submaps; ++j) {
+			get_bits(f, 8); // discard
+			m->submap_floor[j] = get_bits(f, 8);
+			m->submap_residue[j] = get_bits(f, 8);
+			if (m->submap_floor[j] >= f->floor_count)      return error(f, VORBIS_invalid_setup);
+			if (m->submap_residue[j] >= f->residue_count)  return error(f, VORBIS_invalid_setup);
+		}
+	}
+
+	// Modes
+	f->mode_count = get_bits(f, 6) + 1;
+	for (i = 0; i < f->mode_count; ++i) {
+		Mode *m = f->mode_config + i;
+		m->blockflag = get_bits(f, 1);
+		m->windowtype = get_bits(f, 16);
+		m->transformtype = get_bits(f, 16);
+		m->mapping = get_bits(f, 8);
+		if (m->windowtype != 0)                 return error(f, VORBIS_invalid_setup);
+		if (m->transformtype != 0)              return error(f, VORBIS_invalid_setup);
+		if (m->mapping >= f->mapping_count)     return error(f, VORBIS_invalid_setup);
+	}
+
+	flush_packet(f);
+
+	f->previous_length = 0;
+
+	for (i = 0; i < f->channels; ++i) {
+		f->channel_buffers[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1);
+		f->previous_window[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1 / 2);
+		f->finalY[i] = (int16 *) setup_malloc(f, sizeof(int16) * longest_floorlist);
+		if (f->channel_buffers[i] == NULL || f->previous_window[i] == NULL || f->finalY[i] == NULL) return error(f, VORBIS_outofmem);
+#ifdef STB_VORBIS_NO_DEFER_FLOOR
+		f->floor_buffers[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1 / 2);
+		if (f->floor_buffers[i] == NULL) return error(f, VORBIS_outofmem);
+#endif
+	}
+
+	if (!init_blocksize(f, 0, f->blocksize_0)) return FALSE;
+	if (!init_blocksize(f, 1, f->blocksize_1)) return FALSE;
+	f->blocksize[0] = f->blocksize_0;
+	f->blocksize[1] = f->blocksize_1;
+
+#ifdef STB_VORBIS_DIVIDE_TABLE
+	if (integer_divide_table[1][1] == 0)
+		for (i = 0; i < DIVTAB_NUMER; ++i)
+			for (j = 1; j < DIVTAB_DENOM; ++j)
+				integer_divide_table[i][j] = i / j;
+#endif
+
+	// compute how much temporary memory is needed
+
+	// 1.
+	{
+		uint32 imdct_mem = (f->blocksize_1 * sizeof(float) >> 1);
+		uint32 classify_mem;
+		int i, max_part_read = 0;
+		for (i = 0; i < f->residue_count; ++i) {
+			Residue *r = f->residue_config + i;
+			int n_read = r->end - r->begin;
+			int part_read = n_read / r->part_size;
+			if (part_read > max_part_read)
+				max_part_read = part_read;
+		}
+#ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+		classify_mem = f->channels * (sizeof(void*) + max_part_read * sizeof(uint8 *));
+#else
+		classify_mem = f->channels * (sizeof(void*) + max_part_read * sizeof(int *));
+#endif
+
+		f->temp_memory_required = classify_mem;
+		if (imdct_mem > f->temp_memory_required)
+			f->temp_memory_required = imdct_mem;
+	}
+
+	f->first_decode = TRUE;
+
+	if (f->alloc.alloc_buffer) {
+		assert(f->temp_offset == f->alloc.alloc_buffer_length_in_bytes);
+		// check if there's enough temp memory so we don't error later
+		if (f->setup_offset + sizeof(*f) + f->temp_memory_required > (unsigned) f->temp_offset)
+			return error(f, VORBIS_outofmem);
+	}
+
+	f->first_audio_page_offset = stb_vorbis_get_file_offset(f);
+
+	return TRUE;
+}
+
+static void vorbis_deinit(stb_vorbis *p) {
+	int i, j;
+	if (p->residue_config) {
+		for (i = 0; i < p->residue_count; ++i) {
+			Residue *r = p->residue_config + i;
+			if (r->classdata) {
+				for (j = 0; j < p->codebooks[r->classbook].entries; ++j)
+					setup_free(p, r->classdata[j]);
+				setup_free(p, r->classdata);
+			}
+			setup_free(p, r->residue_books);
+		}
+	}
+
+	if (p->codebooks) {
+		CHECK(p);
+		for (i = 0; i < p->codebook_count; ++i) {
+			Codebook *c = p->codebooks + i;
+			setup_free(p, c->codeword_lengths);
+			setup_free(p, c->multiplicands);
+			setup_free(p, c->codewords);
+			setup_free(p, c->sorted_codewords);
+			// c->sorted_values[-1] is the first entry in the array
+			setup_free(p, c->sorted_values ? c->sorted_values - 1 : NULL);
+		}
+		setup_free(p, p->codebooks);
+	}
+	setup_free(p, p->floor_config);
+	setup_free(p, p->residue_config);
+	if (p->mapping) {
+		for (i = 0; i < p->mapping_count; ++i)
+			setup_free(p, p->mapping[i].chan);
+		setup_free(p, p->mapping);
+	}
+	CHECK(p);
+	for (i = 0; i < p->channels && i < STB_VORBIS_MAX_CHANNELS; ++i) {
+		setup_free(p, p->channel_buffers[i]);
+		setup_free(p, p->previous_window[i]);
+#ifdef STB_VORBIS_NO_DEFER_FLOOR
+		setup_free(p, p->floor_buffers[i]);
+#endif
+		setup_free(p, p->finalY[i]);
+	}
+	for (i = 0; i < 2; ++i) {
+		setup_free(p, p->A[i]);
+		setup_free(p, p->B[i]);
+		setup_free(p, p->C[i]);
+		setup_free(p, p->window[i]);
+		setup_free(p, p->bit_reverse[i]);
+	}
+#ifndef STB_VORBIS_NO_STDIO
+	if (p->close_on_free) p->f->provider->closeFile(p->f);
+#endif
+}
+
+void stb_vorbis_close(stb_vorbis *p) {
+	if (p == NULL) return;
+	vorbis_deinit(p);
+	setup_free(p, p);
+}
+
+static void vorbis_init(stb_vorbis *p, const stb_vorbis_alloc *z) {
+	memset(p, 0, sizeof(*p)); // NULL out all malloc'd pointers to start
+	if (z) {
+		p->alloc = *z;
+		p->alloc.alloc_buffer_length_in_bytes = (p->alloc.alloc_buffer_length_in_bytes + 3) & ~3;
+		p->temp_offset = p->alloc.alloc_buffer_length_in_bytes;
+	}
+	p->eof = 0;
+	p->error = VORBIS__no_error;
+	p->stream = NULL;
+	p->codebooks = NULL;
+	p->page_crc_tests = -1;
+#ifndef STB_VORBIS_NO_STDIO
+	p->close_on_free = FALSE;
+	p->f = NULL;
+#endif
+}
+
+int stb_vorbis_get_sample_offset(stb_vorbis *f) {
+	if (f->current_loc_valid)
+		return f->current_loc;
+	else
+		return -1;
+}
+
+stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f) {
+	stb_vorbis_info d;
+	d.channels = f->channels;
+	d.sample_rate = f->sample_rate;
+	d.setup_memory_required = f->setup_memory_required;
+	d.setup_temp_memory_required = f->setup_temp_memory_required;
+	d.temp_memory_required = f->temp_memory_required;
+	d.max_frame_size = f->blocksize_1 >> 1;
+	return d;
+}
+
+int stb_vorbis_get_error(stb_vorbis *f) {
+	int e = f->error;
+	f->error = VORBIS__no_error;
+	return e;
+}
+
+static stb_vorbis * vorbis_alloc(stb_vorbis *f) {
+	stb_vorbis *p = (stb_vorbis *) setup_malloc(f, sizeof(*p));
+	return p;
+}
+
+#ifndef STB_VORBIS_NO_PUSHDATA_API
+
+void stb_vorbis_flush_pushdata(stb_vorbis *f) {
+	f->previous_length = 0;
+	f->page_crc_tests = 0;
+	f->discard_samples_deferred = 0;
+	f->current_loc_valid = FALSE;
+	f->first_decode = FALSE;
+	f->samples_output = 0;
+	f->channel_buffer_start = 0;
+	f->channel_buffer_end = 0;
+}
+
+static int vorbis_search_for_page_pushdata(vorb *f, uint8 *data, int data_len) {
+	int i, n;
+	for (i = 0; i < f->page_crc_tests; ++i)
+		f->scan[i].bytes_done = 0;
+
+	// if we have room for more scans, search for them first, because
+	// they may cause us to stop early if their header is incomplete
+	if (f->page_crc_tests < STB_VORBIS_PUSHDATA_CRC_COUNT) {
+		if (data_len < 4) return 0;
+		data_len -= 3; // need to look for 4-byte sequence, so don't miss
+					   // one that straddles a boundary
+		for (i = 0; i < data_len; ++i) {
+			if (data[i] == 0x4f) {
+				if (0 == memcmp(data + i, ogg_page_header, 4)) {
+					int j, len;
+					uint32 crc;
+					// make sure we have the whole page header
+					if (i + 26 >= data_len || i + 27 + data[i + 26] >= data_len) {
+						// only read up to this page start, so hopefully we'll
+						// have the whole page header start next time
+						data_len = i;
+						break;
+					}
+					// ok, we have it all; compute the length of the page
+					len = 27 + data[i + 26];
+					for (j = 0; j < data[i + 26]; ++j)
+						len += data[i + 27 + j];
+					// scan everything up to the embedded crc (which we must 0)
+					crc = 0;
+					for (j = 0; j < 22; ++j)
+						crc = crc32_update(crc, data[i + j]);
+					// now process 4 0-bytes
+					for (; j < 26; ++j)
+						crc = crc32_update(crc, 0);
+					// len is the total number of bytes we need to scan
+					n = f->page_crc_tests++;
+					f->scan[n].bytes_left = len - j;
+					f->scan[n].crc_so_far = crc;
+					f->scan[n].goal_crc = data[i + 22] + (data[i + 23] << 8) + (data[i + 24] << 16) + (data[i + 25] << 24);
+					// if the last frame on a page is continued to the next, then
+					// we can't recover the sample_loc immediately
+					if (data[i + 27 + data[i + 26] - 1] == 255)
+						f->scan[n].sample_loc = ~0;
+					else
+						f->scan[n].sample_loc = data[i + 6] + (data[i + 7] << 8) + (data[i + 8] << 16) + (data[i + 9] << 24);
+					f->scan[n].bytes_done = i + j;
+					if (f->page_crc_tests == STB_VORBIS_PUSHDATA_CRC_COUNT)
+						break;
+					// keep going if we still have room for more
+				}
+			}
+		}
+	}
+
+	for (i = 0; i < f->page_crc_tests;) {
+		uint32 crc;
+		int j;
+		int n = f->scan[i].bytes_done;
+		int m = f->scan[i].bytes_left;
+		if (m > data_len - n) m = data_len - n;
+		// m is the bytes to scan in the current chunk
+		crc = f->scan[i].crc_so_far;
+		for (j = 0; j < m; ++j)
+			crc = crc32_update(crc, data[n + j]);
+		f->scan[i].bytes_left -= m;
+		f->scan[i].crc_so_far = crc;
+		if (f->scan[i].bytes_left == 0) {
+			// does it match?
+			if (f->scan[i].crc_so_far == f->scan[i].goal_crc) {
+				// Houston, we have page
+				data_len = n + m; // consumption amount is wherever that scan ended
+				f->page_crc_tests = -1; // drop out of page scan mode
+				f->previous_length = 0; // decode-but-don't-output one frame
+				f->next_seg = -1;       // start a new page
+				f->current_loc = f->scan[i].sample_loc; // set the current sample location
+														// to the amount we'd have decoded had we decoded this page
+				f->current_loc_valid = f->current_loc != ~0U;
+				return data_len;
+			}
+			// delete entry
+			f->scan[i] = f->scan[--f->page_crc_tests];
+		} else {
+			++i;
+		}
+	}
+
+	return data_len;
+}
+
+// return value: number of bytes we used
+int stb_vorbis_decode_frame_pushdata(
+	stb_vorbis *f,                   // the file we're decoding
+	const uint8 *data, int data_len, // the memory available for decoding
+	int *channels,                   // place to write number of float * buffers
+	float ***output,                 // place to write float ** array of float * buffers
+	int *samples                     // place to write number of output samples
+) {
+	int i;
+	int len, right, left;
+
+	if (!IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing);
+
+	if (f->page_crc_tests >= 0) {
+		*samples = 0;
+		return vorbis_search_for_page_pushdata(f, (uint8 *) data, data_len);
+	}
+
+	f->stream = (uint8 *) data;
+	f->stream_end = (uint8 *) data + data_len;
+	f->error = VORBIS__no_error;
+
+	// check that we have the entire packet in memory
+	if (!is_whole_packet_present(f, FALSE)) {
+		*samples = 0;
+		return 0;
+	}
+
+	if (!vorbis_decode_packet(f, &len, &left, &right)) {
+		// save the actual error we encountered
+		enum STBVorbisError error = f->error;
+		if (error == VORBIS_bad_packet_type) {
+			// flush and resynch
+			f->error = VORBIS__no_error;
+			while (get8_packet(f) != EOP)
+				if (f->eof) break;
+			*samples = 0;
+			return (int) (f->stream - data);
+		}
+		if (error == VORBIS_continued_packet_flag_invalid) {
+			if (f->previous_length == 0) {
+				// we may be resynching, in which case it's ok to hit one
+				// of these; just discard the packet
+				f->error = VORBIS__no_error;
+				while (get8_packet(f) != EOP)
+					if (f->eof) break;
+				*samples = 0;
+				return (int) (f->stream - data);
+			}
+		}
+		// if we get an error while parsing, what to do?
+		// well, it DEFINITELY won't work to continue from where we are!
+		stb_vorbis_flush_pushdata(f);
+		// restore the error that actually made us bail
+		f->error = error;
+		*samples = 0;
+		return 1;
+	}
+
+	// success!
+	len = vorbis_finish_frame(f, len, left, right);
+	for (i = 0; i < f->channels; ++i)
+		f->outputs[i] = f->channel_buffers[i] + left;
+
+	if (channels) *channels = f->channels;
+	*samples = len;
+	*output = f->outputs;
+	return (int) (f->stream - data);
+}
+
+stb_vorbis *stb_vorbis_open_pushdata(
+	const unsigned char *data, int data_len, // the memory available for decoding
+	int *data_used,              // only defined if result is not NULL
+	int *error, const stb_vorbis_alloc *alloc) {
+	stb_vorbis *f, p;
+	vorbis_init(&p, alloc);
+	p.stream = (uint8 *) data;
+	p.stream_end = (uint8 *) data + data_len;
+	p.push_mode = TRUE;
+	if (!start_decoder(&p)) {
+		if (p.eof)
+			*error = VORBIS_need_more_data;
+		else
+			*error = p.error;
+		return NULL;
+	}
+	f = vorbis_alloc(&p);
+	if (f) {
+		*f = p;
+		*data_used = (int) (f->stream - data);
+		*error = 0;
+		return f;
+	} else {
+		vorbis_deinit(&p);
+		return NULL;
+	}
+}
+#endif // STB_VORBIS_NO_PUSHDATA_API
+
+unsigned int stb_vorbis_get_file_offset(stb_vorbis *f) {
+#ifndef STB_VORBIS_NO_PUSHDATA_API
+	if (f->push_mode) return 0;
+#endif
+	if (USE_MEMORY(f)) return (unsigned int) (f->stream - f->stream_start);
+#ifndef STB_VORBIS_NO_STDIO
+	return (unsigned int) (f->f->tell() - f->f_start);
+#endif
+}
+
+#ifndef STB_VORBIS_NO_PULLDATA_API
+//
+// DATA-PULLING API
+//
+
+static uint32 vorbis_find_page(stb_vorbis *f, uint32 *end, uint32 *last) {
+	for (;;) {
+		int n;
+		if (f->eof) return 0;
+		n = get8(f);
+		if (n == 0x4f) { // page header candidate
+			unsigned int retry_loc = stb_vorbis_get_file_offset(f);
+			int i;
+			// check if we're off the end of a file_section stream
+			if (retry_loc - 25 > f->stream_len)
+				return 0;
+			// check the rest of the header
+			for (i = 1; i < 4; ++i)
+				if (get8(f) != ogg_page_header[i])
+					break;
+			if (f->eof) return 0;
+			if (i == 4) {
+				uint8 header[27];
+				uint32 i, crc, goal, len;
+				for (i = 0; i < 4; ++i)
+					header[i] = ogg_page_header[i];
+				for (; i < 27; ++i)
+					header[i] = get8(f);
+				if (f->eof) return 0;
+				if (header[4] != 0) goto invalid;
+				goal = header[22] + (header[23] << 8) + (header[24] << 16) + (header[25] << 24);
+				for (i = 22; i < 26; ++i)
+					header[i] = 0;
+				crc = 0;
+				for (i = 0; i < 27; ++i)
+					crc = crc32_update(crc, header[i]);
+				len = 0;
+				for (i = 0; i < header[26]; ++i) {
+					int s = get8(f);
+					crc = crc32_update(crc, s);
+					len += s;
+				}
+				if (len && f->eof) return 0;
+				for (i = 0; i < len; ++i)
+					crc = crc32_update(crc, get8(f));
+				// finished parsing probable page
+				if (crc == goal) {
+					// we could now check that it's either got the last
+					// page flag set, OR it's followed by the capture
+					// pattern, but I guess TECHNICALLY you could have
+					// a file with garbage between each ogg page and recover
+					// from it automatically? So even though that paranoia
+					// might decrease the chance of an invalid decode by
+					// another 2^32, not worth it since it would hose those
+					// invalid-but-useful files?
+					if (end)
+						*end = stb_vorbis_get_file_offset(f);
+					if (last) {
+						if (header[5] & 0x04)
+							*last = 1;
+						else
+							*last = 0;
+					}
+					set_file_offset(f, retry_loc - 1);
+					return 1;
+				}
+			}
+invalid:
+			// not a valid page, so rewind and look for next one
+			set_file_offset(f, retry_loc);
+		}
+	}
+}
+
+
+#define SAMPLE_unknown  0xffffffff
+
+// seeking is implemented with a binary search, which narrows down the range to
+// 64K, before using a linear search (because finding the synchronization
+// pattern can be expensive, and the chance we'd find the end page again is
+// relatively high for small ranges)
+//
+// two initial interpolation-style probes are used at the start of the search
+// to try to bound either side of the binary search sensibly, while still
+// working in O(log n) time if they fail.
+
+static int get_seek_page_info(stb_vorbis *f, ProbedPage *z) {
+	uint8 header[27], lacing[255];
+	int i, len;
+
+	// record where the page starts
+	z->page_start = stb_vorbis_get_file_offset(f);
+
+	// parse the header
+	getn(f, header, 27);
+	if (header[0] != 'O' || header[1] != 'g' || header[2] != 'g' || header[3] != 'S')
+		return 0;
+	getn(f, lacing, header[26]);
+
+	// determine the length of the payload
+	len = 0;
+	for (i = 0; i < header[26]; ++i)
+		len += lacing[i];
+
+	// this implies where the page ends
+	z->page_end = z->page_start + 27 + header[26] + len;
+
+	// read the last-decoded sample out of the data
+	z->last_decoded_sample = header[6] + (header[7] << 8) + (header[8] << 16) + (header[9] << 24);
+
+	// restore file state to where we were
+	set_file_offset(f, z->page_start);
+	return 1;
+}
+
+// rarely used function to seek back to the preceeding page while finding the
+// start of a packet
+static int go_to_page_before(stb_vorbis *f, unsigned int limit_offset) {
+	unsigned int previous_safe, end;
+
+	// now we want to seek back 64K from the limit
+	if (limit_offset >= 65536 && limit_offset - 65536 >= f->first_audio_page_offset)
+		previous_safe = limit_offset - 65536;
+	else
+		previous_safe = f->first_audio_page_offset;
+
+	set_file_offset(f, previous_safe);
+
+	while (vorbis_find_page(f, &end, NULL)) {
+		if (end >= limit_offset && stb_vorbis_get_file_offset(f) < limit_offset)
+			return 1;
+		set_file_offset(f, end);
+	}
+
+	return 0;
+}
+
+// implements the search logic for finding a page and starting decoding. if
+// the function succeeds, current_loc_valid will be true and current_loc will
+// be less than or equal to the provided sample number (the closer the
+// better).
+static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number) {
+	ProbedPage left, right, mid;
+	int i, start_seg_with_known_loc, end_pos, page_start;
+	uint32 delta, stream_length, padding;
+	double offset, bytes_per_sample;
+	int probe = 0;
+
+	// find the last page and validate the target sample
+	stream_length = stb_vorbis_stream_length_in_samples(f);
+	if (stream_length == 0)            return error(f, VORBIS_seek_without_length);
+	if (sample_number > stream_length) return error(f, VORBIS_seek_invalid);
+
+	// this is the maximum difference between the window-center (which is the
+	// actual granule position value), and the right-start (which the spec
+	// indicates should be the granule position (give or take one)).
+	padding = ((f->blocksize_1 - f->blocksize_0) >> 2);
+	if (sample_number < padding)
+		sample_number = 0;
+	else
+		sample_number -= padding;
+
+	left = f->p_first;
+	while (left.last_decoded_sample == ~0U) {
+		// (untested) the first page does not have a 'last_decoded_sample'
+		set_file_offset(f, left.page_end);
+		if (!get_seek_page_info(f, &left)) goto error;
+	}
+
+	right = f->p_last;
+	assert(right.last_decoded_sample != ~0U);
+
+	// starting from the start is handled differently
+	if (sample_number <= left.last_decoded_sample) {
+		stb_vorbis_seek_start(f);
+		return 1;
+	}
+
+	while (left.page_end != right.page_start) {
+		assert(left.page_end < right.page_start);
+		// search range in bytes
+		delta = right.page_start - left.page_end;
+		if (delta <= 65536) {
+			// there's only 64K left to search - handle it linearly
+			set_file_offset(f, left.page_end);
+		} else {
+			if (probe < 2) {
+				if (probe == 0) {
+					// first probe (interpolate)
+					double data_bytes = right.page_end - left.page_start;
+					bytes_per_sample = data_bytes / right.last_decoded_sample;
+					offset = left.page_start + bytes_per_sample * (sample_number - left.last_decoded_sample);
+				} else {
+					// second probe (try to bound the other side)
+					double error = ((double) sample_number - mid.last_decoded_sample) * bytes_per_sample;
+					if (error >= 0 && error <  8000) error = 8000;
+					if (error <  0 && error > -8000) error = -8000;
+					offset += error * 2;
+				}
+
+				// ensure the offset is valid
+				if (offset < left.page_end)
+					offset = left.page_end;
+				if (offset > right.page_start - 65536)
+					offset = right.page_start - 65536;
+
+				set_file_offset(f, (unsigned int) offset);
+			} else {
+				// binary search for large ranges (offset by 32K to ensure
+				// we don't hit the right page)
+				set_file_offset(f, left.page_end + (delta / 2) - 32768);
+			}
+
+			if (!vorbis_find_page(f, NULL, NULL)) goto error;
+		}
+
+		for (;;) {
+			if (!get_seek_page_info(f, &mid)) goto error;
+			if (mid.last_decoded_sample != ~0U) break;
+			// (untested) no frames end on this page
+			set_file_offset(f, mid.page_end);
+			assert(mid.page_start < right.page_start);
+		}
+
+		// if we've just found the last page again then we're in a tricky file,
+		// and we're close enough.
+		if (mid.page_start == right.page_start)
+			break;
+
+		if (sample_number < mid.last_decoded_sample)
+			right = mid;
+		else
+			left = mid;
+
+		++probe;
+	}
+
+	// seek back to start of the last packet
+	page_start = left.page_start;
+	set_file_offset(f, page_start);
+	if (!start_page(f)) return error(f, VORBIS_seek_failed);
+	end_pos = f->end_seg_with_known_loc;
+	assert(end_pos >= 0);
+
+	for (;;) {
+		for (i = end_pos; i > 0; --i)
+			if (f->segments[i - 1] != 255)
+				break;
+
+		start_seg_with_known_loc = i;
+
+		if (start_seg_with_known_loc > 0 || !(f->page_flag & PAGEFLAG_continued_packet))
+			break;
+
+		// (untested) the final packet begins on an earlier page
+		if (!go_to_page_before(f, page_start))
+			goto error;
+
+		page_start = stb_vorbis_get_file_offset(f);
+		if (!start_page(f)) goto error;
+		end_pos = f->segment_count - 1;
+	}
+
+	// prepare to start decoding
+	f->current_loc_valid = FALSE;
+	f->last_seg = FALSE;
+	f->valid_bits = 0;
+	f->packet_bytes = 0;
+	f->bytes_in_seg = 0;
+	f->previous_length = 0;
+	f->next_seg = start_seg_with_known_loc;
+
+	for (i = 0; i < start_seg_with_known_loc; i++)
+		skip(f, f->segments[i]);
+
+	// start decoding (optimizable - this frame is generally discarded)
+	vorbis_pump_first_frame(f);
+	return 1;
+
+error:
+	// try to restore the file to a valid state
+	stb_vorbis_seek_start(f);
+	return error(f, VORBIS_seek_failed);
+}
+
+// the same as vorbis_decode_initial, but without advancing
+static int peek_decode_initial(vorb *f, int *p_left_start, int *p_left_end, int *p_right_start, int *p_right_end, int *mode) {
+	int bits_read, bytes_read;
+
+	if (!vorbis_decode_initial(f, p_left_start, p_left_end, p_right_start, p_right_end, mode))
+		return 0;
+
+	// either 1 or 2 bytes were read, figure out which so we can rewind
+	bits_read = 1 + ilog(f->mode_count - 1);
+	if (f->mode_config[*mode].blockflag)
+		bits_read += 2;
+	bytes_read = (bits_read + 7) / 8;
+
+	f->bytes_in_seg += bytes_read;
+	f->packet_bytes -= bytes_read;
+	skip(f, -bytes_read);
+	if (f->next_seg == -1)
+		f->next_seg = f->segment_count - 1;
+	else
+		f->next_seg--;
+	f->valid_bits = 0;
+
+	return 1;
+}
+
+int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number) {
+	uint32 max_frame_samples;
+
+	if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing);
+
+	// fast page-level search
+	if (!seek_to_sample_coarse(f, sample_number))
+		return 0;
+
+	assert(f->current_loc_valid);
+	assert(f->current_loc <= sample_number);
+
+	// linear search for the relevant packet
+	max_frame_samples = (f->blocksize_1 * 3 - f->blocksize_0) >> 2;
+	while (f->current_loc < sample_number) {
+		int left_start, left_end, right_start, right_end, mode, frame_samples;
+		if (!peek_decode_initial(f, &left_start, &left_end, &right_start, &right_end, &mode))
+			return error(f, VORBIS_seek_failed);
+		// calculate the number of samples returned by the next frame
+		frame_samples = right_start - left_start;
+		if (f->current_loc + frame_samples > sample_number) {
+			return 1; // the next frame will contain the sample
+		} else if (f->current_loc + frame_samples + max_frame_samples > sample_number) {
+			// there's a chance the frame after this could contain the sample
+			vorbis_pump_first_frame(f);
+		} else {
+			// this frame is too early to be relevant
+			f->current_loc += frame_samples;
+			f->previous_length = 0;
+			maybe_start_packet(f);
+			flush_packet(f);
+		}
+	}
+	// the next frame will start with the sample
+	assert(f->current_loc == sample_number);
+	return 1;
+}
+
+int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number) {
+	if (!stb_vorbis_seek_frame(f, sample_number))
+		return 0;
+
+	if (sample_number != f->current_loc) {
+		int n;
+		uint32 frame_start = f->current_loc;
+		stb_vorbis_get_frame_float(f, &n, NULL);
+		assert(sample_number > frame_start);
+		assert(f->channel_buffer_start + (int) (sample_number - frame_start) <= f->channel_buffer_end);
+		f->channel_buffer_start += (sample_number - frame_start);
+	}
+
+	return 1;
+}
+
+void stb_vorbis_seek_start(stb_vorbis *f) {
+	if (IS_PUSH_MODE(f)) { error(f, VORBIS_invalid_api_mixing); return; }
+	set_file_offset(f, f->first_audio_page_offset);
+	f->previous_length = 0;
+	f->first_decode = TRUE;
+	f->next_seg = -1;
+	vorbis_pump_first_frame(f);
+}
+
+unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f) {
+	unsigned int restore_offset, previous_safe;
+	unsigned int end, last_page_loc;
+
+	if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing);
+	if (!f->total_samples) {
+		unsigned int last;
+		uint32 lo, hi;
+		char header[6];
+
+		// first, store the current decode position so we can restore it
+		restore_offset = stb_vorbis_get_file_offset(f);
+
+		// now we want to seek back 64K from the end (the last page must
+		// be at most a little less than 64K, but let's allow a little slop)
+		if (f->stream_len >= 65536 && f->stream_len - 65536 >= f->first_audio_page_offset)
+			previous_safe = f->stream_len - 65536;
+		else
+			previous_safe = f->first_audio_page_offset;
+
+		set_file_offset(f, previous_safe);
+		// previous_safe is now our candidate 'earliest known place that seeking
+		// to will lead to the final page'
+
+		if (!vorbis_find_page(f, &end, &last)) {
+			// if we can't find a page, we're hosed!
+			f->error = VORBIS_cant_find_last_page;
+			f->total_samples = 0xffffffff;
+			goto done;
+		}
+
+		// check if there are more pages
+		last_page_loc = stb_vorbis_get_file_offset(f);
+
+		// stop when the last_page flag is set, not when we reach eof;
+		// this allows us to stop short of a 'file_section' end without
+		// explicitly checking the length of the section
+		while (!last) {
+			set_file_offset(f, end);
+			if (!vorbis_find_page(f, &end, &last)) {
+				// the last page we found didn't have the 'last page' flag
+				// set. whoops!
+				break;
+			}
+			previous_safe = last_page_loc + 1;
+			last_page_loc = stb_vorbis_get_file_offset(f);
+		}
+
+		set_file_offset(f, last_page_loc);
+
+		// parse the header
+		getn(f, (unsigned char *) header, 6);
+		// extract the absolute granule position
+		lo = get32(f);
+		hi = get32(f);
+		if (lo == 0xffffffff && hi == 0xffffffff) {
+			f->error = VORBIS_cant_find_last_page;
+			f->total_samples = SAMPLE_unknown;
+			goto done;
+		}
+		if (hi)
+			lo = 0xfffffffe; // saturate
+		f->total_samples = lo;
+
+		f->p_last.page_start = last_page_loc;
+		f->p_last.page_end = end;
+		f->p_last.last_decoded_sample = lo;
+
+done:
+		set_file_offset(f, restore_offset);
+	}
+	return f->total_samples == SAMPLE_unknown ? 0 : f->total_samples;
+}
+
+float stb_vorbis_stream_length_in_seconds(stb_vorbis *f) {
+	return stb_vorbis_stream_length_in_samples(f) / (float) f->sample_rate;
+}
+
+
+
+int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output) {
+	int len, right, left, i;
+	if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing);
+
+	if (!vorbis_decode_packet(f, &len, &left, &right)) {
+		f->channel_buffer_start = f->channel_buffer_end = 0;
+		return 0;
+	}
+
+	len = vorbis_finish_frame(f, len, left, right);
+	for (i = 0; i < f->channels; ++i)
+		f->outputs[i] = f->channel_buffers[i] + left;
+
+	f->channel_buffer_start = left;
+	f->channel_buffer_end = left + len;
+
+	if (channels) *channels = f->channels;
+	if (output)   *output = f->outputs;
+	return len;
+}
+
+#ifndef STB_VORBIS_NO_STDIO
+
+stb_vorbis * stb_vorbis_open_file_section(Polycode::CoreFile *file, int close_on_free, int *error, const stb_vorbis_alloc *alloc, unsigned int length) {
+	stb_vorbis *f, p;
+	vorbis_init(&p, alloc);
+	p.f = file;
+	p.f_start = (uint32) file->tell();
+	p.stream_len = length;
+	p.close_on_free = close_on_free;
+	if (start_decoder(&p)) {
+		f = vorbis_alloc(&p);
+		if (f) {
+			*f = p;
+			vorbis_pump_first_frame(f);
+			return f;
+		}
+	}
+	if (error) *error = p.error;
+	vorbis_deinit(&p);
+	return NULL;
+}
+
+stb_vorbis * stb_vorbis_open_file(Polycode::CoreFile *file, int close_on_free, int *error, const stb_vorbis_alloc *alloc) {
+	unsigned int len, start;
+	start = (unsigned int) file->tell();
+	file->seek(0, SEEK_END);
+	len = (unsigned int) (file->tell() - start);
+	file->seek(start, SEEK_SET);
+	return stb_vorbis_open_file_section(file, close_on_free, error, alloc, len);
+}
+
+stb_vorbis * stb_vorbis_open_filename(const char *filename, int *error, const stb_vorbis_alloc *alloc) {
+	Polycode::CoreFile *f = Polycode::Services()->getCore()->openFile(filename, "rb");
+	if (f)
+		return stb_vorbis_open_file(f, TRUE, error, alloc);
+	if (error) *error = VORBIS_file_open_failure;
+	return NULL;
+}
+#endif // STB_VORBIS_NO_STDIO
+
+stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len, int *error, const stb_vorbis_alloc *alloc) {
+	stb_vorbis *f, p;
+	if (data == NULL) return NULL;
+	vorbis_init(&p, alloc);
+	p.stream = (uint8 *) data;
+	p.stream_end = (uint8 *) data + len;
+	p.stream_start = (uint8 *) p.stream;
+	p.stream_len = len;
+	p.push_mode = FALSE;
+	if (start_decoder(&p)) {
+		f = vorbis_alloc(&p);
+		if (f) {
+			*f = p;
+			vorbis_pump_first_frame(f);
+			return f;
+		}
+	}
+	if (error) *error = p.error;
+	vorbis_deinit(&p);
+	return NULL;
+}
+
+#ifndef STB_VORBIS_NO_INTEGER_CONVERSION
+#define PLAYBACK_MONO     1
+#define PLAYBACK_LEFT     2
+#define PLAYBACK_RIGHT    4
+
+#define L  (PLAYBACK_LEFT  | PLAYBACK_MONO)
+#define C  (PLAYBACK_LEFT  | PLAYBACK_RIGHT | PLAYBACK_MONO)
+#define R  (PLAYBACK_RIGHT | PLAYBACK_MONO)
+
+static int8 channel_position[7][6] =
+{
+	{0},
+	{C},
+	{L, R},
+	{L, C, R},
+	{L, R, L, R},
+	{L, C, R, L, R},
+	{L, C, R, L, R, C},
+};
+
+
+#ifndef STB_VORBIS_NO_FAST_SCALED_FLOAT
+typedef union {
+	float f;
+	int i;
+} float_conv;
+typedef char stb_vorbis_float_size_test[sizeof(float) == 4 && sizeof(int) == 4];
+#define FASTDEF(x) float_conv x
+// add (1<<23) to convert to int, then divide by 2^SHIFT, then add 0.5/2^SHIFT to round
+#define MAGIC(SHIFT) (1.5f * (1 << (23-SHIFT)) + 0.5f/(1 << SHIFT))
+#define ADDEND(SHIFT) (((150-SHIFT) << 23) + (1 << 22))
+#define FAST_SCALED_FLOAT_TO_INT(temp,x,s) (temp.f = (x) + MAGIC(s), temp.i - ADDEND(s))
+#define check_endianness()  
+#else
+#define FAST_SCALED_FLOAT_TO_INT(temp,x,s) ((int) ((x) * (1 << (s))))
+#define check_endianness()
+#define FASTDEF(x)
+#endif
+
+static void copy_samples(short *dest, float *src, int len) {
+	int i;
+	check_endianness();
+	for (i = 0; i < len; ++i) {
+		FASTDEF(temp);
+		int v = FAST_SCALED_FLOAT_TO_INT(temp, src[i], 15);
+		if ((unsigned int) (v + 32768) > 65535)
+			v = v < 0 ? -32768 : 32767;
+		dest[i] = v;
+	}
+}
+
+static void compute_samples(int mask, short *output, int num_c, float **data, int d_offset, int len) {
+#define BUFFER_SIZE  32
+	float buffer[BUFFER_SIZE];
+	int i, j, o, n = BUFFER_SIZE;
+	check_endianness();
+	for (o = 0; o < len; o += BUFFER_SIZE) {
+		memset(buffer, 0, sizeof(buffer));
+		if (o + n > len) n = len - o;
+		for (j = 0; j < num_c; ++j) {
+			if (channel_position[num_c][j] & mask) {
+				for (i = 0; i < n; ++i)
+					buffer[i] += data[j][d_offset + o + i];
+			}
+		}
+		for (i = 0; i < n; ++i) {
+			FASTDEF(temp);
+			int v = FAST_SCALED_FLOAT_TO_INT(temp, buffer[i], 15);
+			if ((unsigned int) (v + 32768) > 65535)
+				v = v < 0 ? -32768 : 32767;
+			output[o + i] = v;
+		}
+	}
+}
+
+static void compute_stereo_samples(short *output, int num_c, float **data, int d_offset, int len) {
+#define BUFFER_SIZE  32
+	float buffer[BUFFER_SIZE];
+	int i, j, o, n = BUFFER_SIZE >> 1;
+	// o is the offset in the source data
+	check_endianness();
+	for (o = 0; o < len; o += BUFFER_SIZE >> 1) {
+		// o2 is the offset in the output data
+		int o2 = o << 1;
+		memset(buffer, 0, sizeof(buffer));
+		if (o + n > len) n = len - o;
+		for (j = 0; j < num_c; ++j) {
+			int m = channel_position[num_c][j] & (PLAYBACK_LEFT | PLAYBACK_RIGHT);
+			if (m == (PLAYBACK_LEFT | PLAYBACK_RIGHT)) {
+				for (i = 0; i < n; ++i) {
+					buffer[i * 2 + 0] += data[j][d_offset + o + i];
+					buffer[i * 2 + 1] += data[j][d_offset + o + i];
+				}
+			} else if (m == PLAYBACK_LEFT) {
+				for (i = 0; i < n; ++i) {
+					buffer[i * 2 + 0] += data[j][d_offset + o + i];
+				}
+			} else if (m == PLAYBACK_RIGHT) {
+				for (i = 0; i < n; ++i) {
+					buffer[i * 2 + 1] += data[j][d_offset + o + i];
+				}
+			}
+		}
+		for (i = 0; i < (n << 1); ++i) {
+			FASTDEF(temp);
+			int v = FAST_SCALED_FLOAT_TO_INT(temp, buffer[i], 15);
+			if ((unsigned int) (v + 32768) > 65535)
+				v = v < 0 ? -32768 : 32767;
+			output[o2 + i] = v;
+		}
+	}
+}
+
+static void convert_samples_short(int buf_c, short **buffer, int b_offset, int data_c, float **data, int d_offset, int samples) {
+	int i;
+	if (buf_c != data_c && buf_c <= 2 && data_c <= 6) {
+		static int channel_selector[3][2] = {{0},{PLAYBACK_MONO},{PLAYBACK_LEFT, PLAYBACK_RIGHT}};
+		for (i = 0; i < buf_c; ++i)
+			compute_samples(channel_selector[buf_c][i], buffer[i] + b_offset, data_c, data, d_offset, samples);
+	} else {
+		int limit = buf_c < data_c ? buf_c : data_c;
+		for (i = 0; i < limit; ++i)
+			copy_samples(buffer[i] + b_offset, data[i] + d_offset, samples);
+		for (; i < buf_c; ++i)
+			memset(buffer[i] + b_offset, 0, sizeof(short) * samples);
+	}
+}
+
+int stb_vorbis_get_frame_short(stb_vorbis *f, int num_c, short **buffer, int num_samples) {
+	float **output;
+	int len = stb_vorbis_get_frame_float(f, NULL, &output);
+	if (len > num_samples) len = num_samples;
+	if (len)
+		convert_samples_short(num_c, buffer, 0, f->channels, output, 0, len);
+	return len;
+}
+
+static void convert_channels_short_interleaved(int buf_c, short *buffer, int data_c, float **data, int d_offset, int len) {
+	int i;
+	check_endianness();
+	if (buf_c != data_c && buf_c <= 2 && data_c <= 6) {
+		assert(buf_c == 2);
+		for (i = 0; i < buf_c; ++i)
+			compute_stereo_samples(buffer, data_c, data, d_offset, len);
+	} else {
+		int limit = buf_c < data_c ? buf_c : data_c;
+		int j;
+		for (j = 0; j < len; ++j) {
+			for (i = 0; i < limit; ++i) {
+				FASTDEF(temp);
+				float f = data[i][d_offset + j];
+				int v = FAST_SCALED_FLOAT_TO_INT(temp, f, 15);//data[i][d_offset+j],15);
+				if ((unsigned int) (v + 32768) > 65535)
+					v = v < 0 ? -32768 : 32767;
+				*buffer++ = v;
+			}
+			for (; i < buf_c; ++i)
+				*buffer++ = 0;
+		}
+	}
+}
+
+int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts) {
+	float **output;
+	int len;
+	if (num_c == 1) return stb_vorbis_get_frame_short(f, num_c, &buffer, num_shorts);
+	len = stb_vorbis_get_frame_float(f, NULL, &output);
+	if (len) {
+		if (len*num_c > num_shorts) len = num_shorts / num_c;
+		convert_channels_short_interleaved(num_c, buffer, f->channels, output, 0, len);
+	}
+	return len;
+}
+
+int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts) {
+	float **outputs;
+	int len = num_shorts / channels;
+	int n = 0;
+	int z = f->channels;
+	if (z > channels) z = channels;
+	while (n < len) {
+		int k = f->channel_buffer_end - f->channel_buffer_start;
+		if (n + k >= len) k = len - n;
+		if (k)
+			convert_channels_short_interleaved(channels, buffer, f->channels, f->channel_buffers, f->channel_buffer_start, k);
+		buffer += k*channels;
+		n += k;
+		f->channel_buffer_start += k;
+		if (n == len) break;
+		if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break;
+	}
+	return n;
+}
+
+int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int len) {
+	float **outputs;
+	int n = 0;
+	int z = f->channels;
+	if (z > channels) z = channels;
+	while (n < len) {
+		int k = f->channel_buffer_end - f->channel_buffer_start;
+		if (n + k >= len) k = len - n;
+		if (k)
+			convert_samples_short(channels, buffer, n, f->channels, f->channel_buffers, f->channel_buffer_start, k);
+		n += k;
+		f->channel_buffer_start += k;
+		if (n == len) break;
+		if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break;
+	}
+	return n;
+}
+
+#ifndef STB_VORBIS_NO_STDIO
+int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output) {
+	int data_len, offset, total, limit, error;
+	short *data;
+	stb_vorbis *v = stb_vorbis_open_filename(filename, &error, NULL);
+	if (v == NULL) return -1;
+	limit = v->channels * 4096;
+	*channels = v->channels;
+	if (sample_rate)
+		*sample_rate = v->sample_rate;
+	offset = data_len = 0;
+	total = limit;
+	data = (short *) malloc(total * sizeof(*data));
+	if (data == NULL) {
+		stb_vorbis_close(v);
+		return -2;
+	}
+	for (;;) {
+		int n = stb_vorbis_get_frame_short_interleaved(v, v->channels, data + offset, total - offset);
+		if (n == 0) break;
+		data_len += n;
+		offset += n * v->channels;
+		if (offset + limit > total) {
+			short *data2;
+			total *= 2;
+			data2 = (short *) realloc(data, total * sizeof(*data));
+			if (data2 == NULL) {
+				free(data);
+				stb_vorbis_close(v);
+				return -2;
+			}
+			data = data2;
+		}
+	}
+	*output = data;
+	stb_vorbis_close(v);
+	return data_len;
+}
+#endif // NO_STDIO
+
+int stb_vorbis_decode_memory(const uint8 *mem, int len, int *channels, int *sample_rate, short **output) {
+	int data_len, offset, total, limit, error;
+	short *data;
+	stb_vorbis *v = stb_vorbis_open_memory(mem, len, &error, NULL);
+	if (v == NULL) return -1;
+	limit = v->channels * 4096;
+	*channels = v->channels;
+	if (sample_rate)
+		*sample_rate = v->sample_rate;
+	offset = data_len = 0;
+	total = limit;
+	data = (short *) malloc(total * sizeof(*data));
+	if (data == NULL) {
+		stb_vorbis_close(v);
+		return -2;
+	}
+	for (;;) {
+		int n = stb_vorbis_get_frame_short_interleaved(v, v->channels, data + offset, total - offset);
+		if (n == 0) break;
+		data_len += n;
+		offset += n * v->channels;
+		if (offset + limit > total) {
+			short *data2;
+			total *= 2;
+			data2 = (short *) realloc(data, total * sizeof(*data));
+			if (data2 == NULL) {
+				free(data);
+				stb_vorbis_close(v);
+				return -2;
+			}
+			data = data2;
+		}
+	}
+	*output = data;
+	stb_vorbis_close(v);
+	return data_len;
+}
+#endif // STB_VORBIS_NO_INTEGER_CONVERSION
+
+int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats) {
+	float **outputs;
+	int len = num_floats / channels;
+	int n = 0;
+	int z = f->channels;
+	if (z > channels) z = channels;
+	while (n < len) {
+		int i, j;
+		int k = f->channel_buffer_end - f->channel_buffer_start;
+		if (n + k >= len) k = len - n;
+		for (j = 0; j < k; ++j) {
+			for (i = 0; i < z; ++i)
+				*buffer++ = f->channel_buffers[i][f->channel_buffer_start + j];
+			for (; i < channels; ++i)
+				*buffer++ = 0;
+		}
+		n += k;
+		f->channel_buffer_start += k;
+		if (n == len)
+			break;
+		if (!stb_vorbis_get_frame_float(f, NULL, &outputs))
+			break;
+	}
+	return n;
+}
+
+int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples) {
+	float **outputs;
+	int n = 0;
+	int z = f->channels;
+	if (z > channels) z = channels;
+	while (n < num_samples) {
+		int i;
+		int k = f->channel_buffer_end - f->channel_buffer_start;
+		if (n + k >= num_samples) k = num_samples - n;
+		if (k) {
+			for (i = 0; i < z; ++i)
+				memcpy(buffer[i] + n, f->channel_buffers[i] + f->channel_buffer_start, sizeof(float)*k);
+			for (; i < channels; ++i)
+				memset(buffer[i] + n, 0, sizeof(float) * k);
+		}
+		n += k;
+		f->channel_buffer_start += k;
+		if (n == num_samples)
+			break;
+		if (!stb_vorbis_get_frame_float(f, NULL, &outputs))
+			break;
+	}
+	return n;
+}
+#endif // STB_VORBIS_NO_PULLDATA_API
+
+/* Version history
+1.09    - 2016/04/04 - back out 'avoid discarding last frame' fix from previous version
+1.08    - 2016/04/02 - fixed multiple warnings; fix setup memory leaks;
+avoid discarding last frame of audio data
+1.07    - 2015/01/16 - fixed some warnings, fix mingw, const-correct API
+some more crash fixes when out of memory or with corrupt files
+1.06    - 2015/08/31 - full, correct support for seeking API (Dougall Johnson)
+some crash fixes when out of memory or with corrupt files
+1.05    - 2015/04/19 - don't define __forceinline if it's redundant
+1.04    - 2014/08/27 - fix missing const-correct case in API
+1.03    - 2014/08/07 - Warning fixes
+1.02    - 2014/07/09 - Declare qsort compare function _cdecl on windows
+1.01    - 2014/06/18 - fix stb_vorbis_get_samples_float
+1.0     - 2014/05/26 - fix memory leaks; fix warnings; fix bugs in multichannel
+(API change) report sample rate for decode-full-file funcs
+0.99996 - bracket #include <malloc.h> for macintosh compilation by Laurent Gomila
+0.99995 - use union instead of pointer-cast for fast-float-to-int to avoid alias-optimization problem
+0.99994 - change fast-float-to-int to work in single-precision FPU mode, remove endian-dependence
+0.99993 - remove assert that fired on legal files with empty tables
+0.99992 - rewind-to-start
+0.99991 - bugfix to stb_vorbis_get_samples_short by Bernhard Wodo
+0.9999 - (should have been 0.99990) fix no-CRT support, compiling as C++
+0.9998 - add a full-decode function with a memory source
+0.9997 - fix a bug in the read-from-FILE case in 0.9996 addition
+0.9996 - query length of vorbis stream in samples/seconds
+0.9995 - bugfix to another optimization that only happened in certain files
+0.9994 - bugfix to one of the optimizations that caused significant (but inaudible?) errors
+0.9993 - performance improvements; runs in 99% to 104% of time of reference implementation
+0.9992 - performance improvement of IMDCT; now performs close to reference implementation
+0.9991 - performance improvement of IMDCT
+0.999 - (should have been 0.9990) performance improvement of IMDCT
+0.998 - no-CRT support from Casey Muratori
+0.997 - bugfixes for bugs found by Terje Mathisen
+0.996 - bugfix: fast-huffman decode initialized incorrectly for sparse codebooks; fixing gives 10% speedup - found by Terje Mathisen
+0.995 - bugfix: fix to 'effective' overrun detection - found by Terje Mathisen
+0.994 - bugfix: garbage decode on final VQ symbol of a non-multiple - found by Terje Mathisen
+0.993 - bugfix: pushdata API required 1 extra byte for empty page (failed to consume final page if empty) - found by Terje Mathisen
+0.992 - fixes for MinGW warning
+0.991 - turn fast-float-conversion on by default
+0.990 - fix push-mode seek recovery if you seek into the headers
+0.98b - fix to bad release of 0.98
+0.98 - fix push-mode seek recovery; robustify float-to-int and support non-fast mode
+0.97 - builds under c++ (typecasting, don't use 'class' keyword)
+0.96 - somehow MY 0.95 was right, but the web one was wrong, so here's my 0.95 rereleased as 0.96, fixes a typo in the clamping code
+0.95 - clamping code for 16-bit functions
+0.94 - not publically released
+0.93 - fixed all-zero-floor case (was decoding garbage)
+0.92 - fixed a memory leak
+0.91 - conditional compiles to omit parts of the API and the infrastructure to support them: STB_VORBIS_NO_PULLDATA_API, STB_VORBIS_NO_PUSHDATA_API, STB_VORBIS_NO_STDIO, STB_VORBIS_NO_INTEGER_CONVERSION
+0.90 - first public release
+*/
+
+#endif // STB_VORBIS_HEADER_ONLY

+ 174 - 4
src/ide/PolycodeTextEditor.cpp

@@ -109,7 +109,17 @@ PolycodeSyntaxHighlighter::PolycodeSyntaxHighlighter(String extension) {
 		}
 	
 		keywords = String("for cast safe_cast and require true false class self break do end else elseif function if local nil not or repeat return then until while").split(" ");
-		
+	} else if (extension == "js") {
+		mode = MODE_JS;
+
+		std::vector<String>separators_s = String("[ * [ ] { } ; . , : ( ) \t \n = + - / \\ ' \"").split(" ");
+		separators_s.push_back(" ");
+
+		for (int i = 0; i < separators_s.size(); i++) {
+			separators.push_back(separators_s[i][0]);
+		}
+
+		keywords = String("abstract arguments boolean break byte case catch char class const continue debugger default delete do double else enum eval export extends false final finally float for function goto if implements import in instanceof int interface let long native new null package private protected public return short static super switch synchronized this throw throws transient true try typeof var void volatile while with yield").split(" ");
 	} else if(extension == "vert" || extension == "frag") {
 		mode = MODE_GLSL;
 		
@@ -120,8 +130,7 @@ PolycodeSyntaxHighlighter::PolycodeSyntaxHighlighter(String extension) {
 			separators.push_back(separators_s[i][0]);
 		}
 	
-		keywords = String("for cast and true falsebreak do end else if notreturn then until while uniform varying sampler2D sampler3D vec2 vec3 vec4 float in inout void mat2 mat3 mat4 bool int #define #ifdef #elif #else #endif").split(" ");
-		
+		keywords = String("for cast and true false break do end else if not return then until while uniform varying sampler2D sampler3D vec2 vec3 vec4 float in inout void mat2 mat3 mat4 bool int #define #ifdef #elif #else #endif").split(" ");
 	}
 	
 }
@@ -149,6 +158,8 @@ bool PolycodeSyntaxHighlighter::contains_char(char part, std::vector<char> *list
 std::vector<SyntaxHighlightToken> PolycodeSyntaxHighlighter::parseText(String text, SyntaxHighlightToken overrideToken) {
 	if(mode == MODE_LUA) {	
 		return parseLua(text, overrideToken);
+	} else if (mode == MODE_JS) {
+		return parseJS(text, overrideToken);
 	} else {
 		return parseGLSL(text, overrideToken);	
 	}
@@ -448,6 +459,165 @@ std::vector<SyntaxHighlightToken> PolycodeSyntaxHighlighter::parseLua(String tex
 	return tokens;
 }
 
+std::vector<SyntaxHighlightToken> PolycodeSyntaxHighlighter::parseJS(String text, SyntaxHighlightToken overrideToken) {
+	std::vector<SyntaxHighlightToken> tokens;
+
+	text = text + "\n";
+
+	const int MODE_GENERAL = 0;
+	const int MODE_COMMENT = 1;
+	const int MODE_STRING = 2;
+	const int MODE_METHOD = 3;
+	const int MODE_KEYWORD = 4;
+	const int MODE_NUMBER = 5;
+	const int MODE_MEMBER = 6;
+	const int MODE_SINGLEQUOTE_STRING = 7;
+
+	int mode = MODE_GENERAL;
+	bool isComment = false;
+
+	if (text.find_first_of("*/") != -1) {
+		if (overrideToken.overrideType == SyntaxHighlightToken::TOKEN_TYPE_OVERRIDE_LINE || overrideToken.overrideType == SyntaxHighlightToken::TOKEN_TYPE_OVERRIDE_START) {
+			mode = MODE_COMMENT;
+		}
+	}
+
+
+	String line = "";
+
+	char lastSeparator = ' ';
+
+
+	for (int i = 0; i < text.length(); i++) {
+		char ch = text[i];
+		if (contains_char(ch, &separators)) {
+
+			unsigned int type = mode;
+			unsigned int ch_type = mode;
+
+
+			if (ch == '\"' && mode != MODE_COMMENT && mode != MODE_SINGLEQUOTE_STRING)
+				ch_type = MODE_STRING;
+
+			if (ch == '\'' && mode != MODE_COMMENT && mode != MODE_STRING)
+				ch_type = MODE_SINGLEQUOTE_STRING;
+
+			if ((mode != MODE_STRING && mode != MODE_SINGLEQUOTE_STRING) && ch == '(' && mode != MODE_COMMENT) {
+				type = MODE_METHOD;
+			}
+
+			if ((mode != MODE_STRING && mode != MODE_SINGLEQUOTE_STRING)	&& mode != MODE_COMMENT) {
+				if (contains(line, &keywords)) {
+					type = MODE_KEYWORD;
+				}
+			}
+
+			if ((mode != MODE_STRING && mode != MODE_SINGLEQUOTE_STRING) && !isComment && mode != MODE_COMMENT) {
+
+				if (line.isNumber()) {
+					type = MODE_NUMBER;
+				} else {
+					if (lastSeparator == '.' && ch != '.' && ch != ':') {
+						type = MODE_MEMBER;
+					}
+				}
+			}
+
+			if (isComment) {
+				type = MODE_COMMENT;
+				ch_type = MODE_COMMENT;
+			}
+
+			if (mode == MODE_COMMENT) {
+				type = MODE_COMMENT;
+				ch_type = MODE_COMMENT;
+			}
+
+
+			if (line != "")
+				tokens.push_back(SyntaxHighlightToken(line, type));
+			tokens.push_back(SyntaxHighlightToken(ch, ch_type));
+
+			if (ch == '/' && lastSeparator == '/' && (mode != MODE_STRING && mode != MODE_SINGLEQUOTE_STRING)) {
+				isComment = true;
+				tokens[tokens.size() - 1].type = MODE_COMMENT;
+				tokens[tokens.size() - 2].type = MODE_COMMENT;
+			}
+
+			if (ch == '*' && lastSeparator == '/' && (mode != MODE_STRING && mode != MODE_SINGLEQUOTE_STRING)) {
+				tokens[tokens.size() - 1].type = MODE_COMMENT;
+				tokens[tokens.size() - 2].type = MODE_COMMENT;
+				mode = MODE_COMMENT;
+				tokens[tokens.size() - 1].overrideType = SyntaxHighlightToken::TOKEN_TYPE_OVERRIDE_START;
+			}
+
+			if (ch == '/' && lastSeparator == '*' && mode == MODE_COMMENT) {
+				if (mode == MODE_COMMENT)
+					mode = MODE_GENERAL;
+				if ((mode != MODE_STRING && mode != MODE_SINGLEQUOTE_STRING))
+					tokens[tokens.size() - 1].overrideType = SyntaxHighlightToken::TOKEN_TYPE_OVERRIDE_END;
+
+			}
+
+			if (ch == '\n')
+				isComment = false;
+
+
+			if (ch == '\"'  && mode != MODE_COMMENT) {
+				if (mode == MODE_STRING) {
+					mode = MODE_GENERAL;
+				} else if (mode != MODE_SINGLEQUOTE_STRING){
+					mode = MODE_STRING;
+				}
+			}
+
+			if (ch == '\''  && mode != MODE_COMMENT) {
+				if (mode == MODE_SINGLEQUOTE_STRING) {
+					mode = MODE_GENERAL;
+				} else if (mode != MODE_STRING){
+					mode = MODE_SINGLEQUOTE_STRING;
+				}
+			}
+
+			line = "";
+			lastSeparator = ch;
+		} else {
+			line.append(ch);
+		}
+	}
+
+	for (int i = 0; i < tokens.size(); i++) {
+		switch (tokens[i].type) {
+		case MODE_STRING:
+			tokens[i].color = globalSyntaxTheme->colors[4];
+			break;
+		case MODE_SINGLEQUOTE_STRING:
+			tokens[i].color = globalSyntaxTheme->colors[4];
+			break;
+		case MODE_COMMENT:
+			tokens[i].color = globalSyntaxTheme->colors[1];
+			break;
+		case MODE_METHOD:
+			tokens[i].color = globalSyntaxTheme->colors[3];
+			break;
+		case MODE_KEYWORD:
+			tokens[i].color = globalSyntaxTheme->colors[2];
+			break;
+		case MODE_NUMBER:
+			tokens[i].color = globalSyntaxTheme->colors[6];
+			break;
+		case MODE_MEMBER:
+			tokens[i].color = globalSyntaxTheme->colors[5];
+			break;
+		default:
+			tokens[i].color = globalSyntaxTheme->colors[0];
+			break;
+		}
+	}
+
+	return tokens;
+}
+
 PolycodeTextEditor::PolycodeTextEditor() : PolycodeEditor(true){
 	firstTimeResize = true;
 	editorType = "PolycodeTextEditor";
@@ -493,7 +663,7 @@ bool PolycodeTextEditor::openFile(OSFileEntry filePath) {
 			
 	syntaxHighligher = NULL;
 	
-	if(filePath.extension == "lua" || filePath.extension == "vert" || filePath.extension == "frag") {
+	if(filePath.extension == "lua" || filePath.extension == "js" || filePath.extension == "vert" || filePath.extension == "frag") {
 		syntaxHighligher = new PolycodeSyntaxHighlighter(filePath.extension);
 		textInput->setSyntaxHighlighter(syntaxHighligher);
 	} else {

+ 7 - 5
src/ide/PolycodeWinIDEView.cpp

@@ -228,12 +228,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
 				m.wParam = wParam;
 				m.lParam = lParam;
 				m.time = 0;
-				if ( PeekMessage(&m, hWnd, 0, WM_USER, PM_NOREMOVE) && (m.message == WM_CHAR) ) {
-					GetMessage(&m, hWnd, 0, WM_USER);
-						unicodeChar = (wchar_t)m.wParam;
-				}
 
-			core->handleKeyDown(lParam,wParam, unicodeChar);
+			core->handleKeyDown(lParam,wParam);
 		}
 	break;
 	case WM_KEYUP:
@@ -241,6 +237,12 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
 		if(core)
 			core->handleKeyUp(lParam,wParam);
 	break;
+	//case WM_UNICHAR:
+	//	if (wParam == UNICODE_NOCHAR)
+	//		break;
+	case WM_CHAR:
+		core->handleTextInput(lParam, wParam);
+	break;
 	case WM_CLOSE:
 	{
 		globalApp->saveConfigFile();

+ 39 - 24
src/modules/ui/PolyUITextInput.cpp

@@ -237,6 +237,7 @@ UITextInput::UITextInput(bool multiLine, Number width, Number height, int custom
 	indentSpacing = 4;
 	indentType = INDENT_TAB;
 	
+	core->getInput()->addEventListenerUnique(this, InputEvent::EVENT_TEXTINPUT);
 	core->getInput()->addEventListenerUnique(this, InputEvent::EVENT_KEYDOWN);
 	core->getInput()->addEventListenerUnique(this, InputEvent::EVENT_MOUSEUP);
 }
@@ -1717,7 +1718,7 @@ bool UITextInput::isNumberOrCharacter(wchar_t charCode) {
 	return false;
 }
 
-void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) {
+void UITextInput::onKeyDown(PolyKEY key) {
 	
 	if(!hasFocus)
 		return;
@@ -2059,28 +2060,6 @@ void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) {
 	String ctext = lines[actualLineOffset].text;
 	
 	bool _changedText = false;
-		
-	if(((charCode > 31 && charCode < 127) || charCode > 127) && key != KEY_DELETE && key != KEY_HOME && key != KEY_END) {
-		
-		if(!isNumberOnly || (isNumberOnly && ((charCode > 47 && charCode < 58) || (charCode == '.' || charCode == '-')))) {
-			if(!isNumberOrCharacter(charCode)) { 
-				saveUndoState();
-			} else if (!isTypingWord) {
-				saveUndoState();
-				isTypingWord = 1;
-			}
-			if(hasSelection)
-				deleteSelection();
-			ctext = lines[actualLineOffset].text;
-			String text2 = ctext.substr(actualCaretPosition, ctext.length()-actualCaretPosition);
-			ctext = ctext.substr(0,actualCaretPosition);
-			ctext += charCode + text2;
-			actualCaretPosition++;
-			_changedText = true;
-			
-			showCurrentLineIfOffscreen();
-		}
-	}
 	
 	if(key == KEY_TAB && multiLine) {
 		saveUndoState();
@@ -2176,6 +2155,38 @@ void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) {
 	updateCaretPosition();
 }
 
+void UITextInput::onTextInput(String newText){
+	if(!hasFocus)
+		return;
+	
+	if(!isNumberOnly || (isNumberOnly && newText.isNumber())) {
+		String ctext = lines[actualLineOffset].text;
+		//if(!isNumberOrCharacter(charCode)) { 
+			/*saveUndoState();
+		} else */
+		if (!isTypingWord) {
+			saveUndoState();
+			isTypingWord = 1;
+		}
+		
+		if(hasSelection)
+			deleteSelection();
+		
+		ctext = lines[actualLineOffset].text;
+		String text2 = ctext.substr(actualCaretPosition, ctext.length()-actualCaretPosition);
+		ctext = ctext.substr(0,actualCaretPosition);
+		ctext += newText + text2;
+		actualCaretPosition+=newText.length();
+		
+		showCurrentLineIfOffscreen();
+		
+		lines[actualLineOffset].text = ctext;
+		changedText(actualLineOffset, actualLineOffset);
+		updateCaretPosition();
+	}
+	
+}
+
 void UITextInput::Update() {
 	resizeTimer += core->getElapsed();
 	
@@ -2518,9 +2529,13 @@ void UITextInput::readjustBuffer(int lineStart, int lineEnd) {
 void UITextInput::handleEvent(Event *event) {
 
 	if(event->getDispatcher() == core->getInput()) {
+		if(event->getEventCode() == InputEvent::EVENT_TEXTINPUT){
+			InputEvent *inputEvent = (InputEvent*) event;
+			onTextInput(inputEvent->text);
+		}
 		if(event->getEventCode() == InputEvent::EVENT_KEYDOWN) {
 			InputEvent *inputEvent = (InputEvent*) event;
-			onKeyDown(inputEvent->key, inputEvent->charCode);
+			onKeyDown(inputEvent->key);
 		}
 		if (event->getEventCode() == InputEvent::EVENT_MOUSEUP) {
 			draggingSelection = false;

+ 2 - 2
src/modules/ui/PolyUITreeContainer.cpp

@@ -91,7 +91,7 @@ void UITreeContainer::handleEvent(Event *event) {
 	if(event->getDispatcher() == CoreServices::getInstance()->getCore()->getInput()) {
 		InputEvent *inputEvent = (InputEvent*) event;
 		if(event->getEventCode() == InputEvent::EVENT_KEYDOWN) {
-			onKeyDown(inputEvent->key, inputEvent->charCode);
+			onKeyDown(inputEvent->key);
 		}
 	}
 
@@ -164,7 +164,7 @@ UITree *UITreeContainer::findNextParentSibling(UITree *parent) {
 //
 // END RECURSIVE HELPER FUNCTIONS
 
-void UITreeContainer::onKeyDown(PolyKEY key, wchar_t charCode) {
+void UITreeContainer::onKeyDown(PolyKEY key) {
 	if (hasFocus) {
 		
 		// KEYBOARD NAV STUFF

+ 2 - 2
src/modules/ui/PolyUIWindow.cpp

@@ -131,7 +131,7 @@ UIWindow::~UIWindow() {
 	CoreServices::getInstance()->getCore()->getInput()->removeAllHandlersForListener(this);
 }
 
-void UIWindow::onKeyDown(PolyKEY key, wchar_t charCode) {	
+void UIWindow::onKeyDown(PolyKEY key) {	
 	if(key == KEY_ESCAPE && closeOnEscape) {
 		onClose();
 		dispatchEvent(new UIEvent(), UIEvent::CLOSE_EVENT);		
@@ -177,7 +177,7 @@ void UIWindow::handleEvent(Event *event) {
 	if(event->getDispatcher() == CoreServices::getInstance()->getCore()->getInput()) {
 		InputEvent *inputEvent = (InputEvent*)event;
 		if(event->getEventCode() == InputEvent::EVENT_KEYDOWN) {
-			onKeyDown(inputEvent->key, inputEvent->charCode);
+			onKeyDown(inputEvent->key);
 		}
 	}
 

+ 63 - 7
src/view/android/PolycodeView.cpp

@@ -104,14 +104,18 @@ void onStart(ANativeActivity* activity){
 void onResume(ANativeActivity* activity){
 	Logger::log("onResume");
 	JNIWakeLock(activity, true);
-	if(core)
+	if(core){
 		core->paused = false;
+		OpenSLAudioInterface::queueCallback(NULL, core->getAudioInterface());
+	}
 	((PolycodeView*)activity->instance)->lifecycleFlags |= APP_STATUS_ACTIVE;
 }
 
 void onPause(ANativeActivity* activity){
 	Logger::log("onPause");
 	JNIWakeLock(activity, false);
+	if(core)
+		core->paused = true;
 	((PolycodeView*)activity->instance)->lifecycleFlags &= ~APP_STATUS_ACTIVE;
 }
 
@@ -129,7 +133,7 @@ void onDestroy(ANativeActivity* activity){
 	JNIEnv* jniEnv;
 	bool attached = false;
 	
-	if(javaVM->GetEnv((void**)&jniEnv, JNI_VERSION_1_6) ==JNI_EDETACHED){
+	if(javaVM->GetEnv((void**)&jniEnv, JNI_VERSION_1_6) == JNI_EDETACHED){
 		JavaVMAttachArgs attachArgs;
 		attachArgs.version = JNI_VERSION_1_6;
 		attachArgs.name = "NativeThread";
@@ -181,7 +185,7 @@ void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow *window){
 		((PolycodeView*)activity->instance)->lifecycleFlags|=APP_STATUS_HAS_REAL_SURFACE;
 		if(core){
 			if(((PolycodeView*)activity->instance)->firstWindowCreate){
-				Services()->getSoundManager()->setAudioInterface(new OpenSLAudioInterface());
+				
 				((PolycodeView*)activity->instance)->firstWindowCreate = false;
 			}
 			core->recreateContext = true;
@@ -242,7 +246,6 @@ void onLowMemory(ANativeActivity* activity){
 }
 
 static int inputLoop(int fd, int events, void* data){
-// 	Logger::log("inputLoop");
 	AInputQueue* native_input = ((PolycodeView*)data)->native_input;
 	AndroidEvent event;
 	AInputEvent* aev;
@@ -257,16 +260,27 @@ static int inputLoop(int fd, int events, void* data){
 			if(type == AINPUT_EVENT_TYPE_KEY){
 				event.eventTime = AKeyEvent_getEventTime(aev);
 				int kC = AKeyEvent_getKeyCode(aev);
-				if(core)
+				if(core){
 					event.keyCode = core->mapKey(kC);
+					if(kC == AKEYCODE_VOLUME_DOWN){
+						JNIVolumeControl(((PolycodeView*)data)->native_activity, false);
+					} else if (kC == AKEYCODE_VOLUME_UP){
+						JNIVolumeControl(((PolycodeView*)data)->native_activity, true);
+					}
+				}
 				
 				action = AKeyEvent_getAction(aev);
 				if(action == AKEY_EVENT_ACTION_DOWN){
 					event.eventCode = InputEvent::EVENT_KEYDOWN;
-					event.unicodeChar = JNIGetUnicodeChar(((PolycodeView*)data)->native_activity, AKEY_EVENT_ACTION_DOWN, kC, AKeyEvent_getMetaState(aev));
+					
+					AndroidEvent textEvent; 
+					textEvent.text = JNIGetUnicodeChar(((PolycodeView*)data)->native_activity, AKEY_EVENT_ACTION_DOWN, kC, AKeyEvent_getMetaState(aev)); 
+					textEvent.eventCode = InputEvent::EVENT_TEXTINPUT; 
+					textEvent.eventGroup = AndroidEvent::INPUT_EVENT; 
+					if(textEvent.text.length() > 0 && !(textEvent.text[0] < ' ' || textEvent.text[0] == 127) && textEvent.text[0] > 0) 
+						core->handleSystemEvent(textEvent); 
 				} else if (action == AKEY_EVENT_ACTION_UP){
 					event.eventCode = InputEvent::EVENT_KEYUP;
-					event.unicodeChar = JNIGetUnicodeChar(((PolycodeView*)data)->native_activity, AKEY_EVENT_ACTION_UP, kC, AKeyEvent_getMetaState(aev));
 				}
 				core->handleSystemEvent(event);
 			} else if(type == AINPUT_EVENT_TYPE_MOTION){
@@ -558,6 +572,48 @@ void JNIWakeLock(ANativeActivity* native_activity, bool acquire){
 		}
 	}
 	
+	if(attached)
+		javaVM->DetachCurrentThread();
+}
+
+void JNIVolumeControl(ANativeActivity* native_activity, bool up){
+	JavaVM* javaVM = native_activity->vm;
+	JNIEnv* jniEnv;
+	bool attached = false;
+	
+	if(javaVM->GetEnv((void**)&jniEnv, JNI_VERSION_1_6) ==JNI_EDETACHED){
+		JavaVMAttachArgs attachArgs;
+		attachArgs.version = JNI_VERSION_1_6;
+		attachArgs.name = "NativeThread";
+		attachArgs.group = NULL;
+		
+		jint result = javaVM->AttachCurrentThread(&jniEnv, &attachArgs);
+		if(result == JNI_ERR){
+			return;
+		}
+		attached = true;
+	}
+
+	jclass classNativeActivity = jniEnv->FindClass("android/app/NativeActivity");
+	jclass classAudioManager = jniEnv->FindClass("android/media/AudioManager");
+	
+	jclass ClassContext = jniEnv->FindClass("android/content/Context");
+	jfieldID FieldAUDIO_SERVICE = jniEnv->GetStaticFieldID(ClassContext, "AUDIO_SERVICE", "Ljava/lang/String;");
+	jobject AUDIO_SERVICE = jniEnv->GetStaticObjectField(ClassContext, FieldAUDIO_SERVICE);
+	
+	jmethodID getSystemServiceID = jniEnv->GetMethodID(classNativeActivity, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
+	jobject AudioManager = jniEnv->CallObjectMethod(native_activity->clazz, getSystemServiceID, AUDIO_SERVICE);
+	
+	int ADJUST_RAISE = 1;
+	int ADJUST_LOWER = -1;
+	jmethodID adjustVolumeID = jniEnv->GetMethodID(classAudioManager, "adjustVolume", "(II)V");
+	
+	if (up){
+		jniEnv->CallVoidMethod(AudioManager, adjustVolumeID, ADJUST_RAISE, 0);
+	} else {
+		jniEnv->CallVoidMethod(AudioManager, adjustVolumeID, ADJUST_LOWER, 0);
+	}
+	
 	if(attached)
 		javaVM->DetachCurrentThread();
 }

+ 13 - 21
src/view/osx/PolycodeView.mm

@@ -682,7 +682,6 @@
 			// Don't care otherwise
 			break;
 	}
-	newEvent.unicodeChar = 0;
 	
 	
 	core->cocoaEvents.push_back(newEvent);	
@@ -700,19 +699,20 @@
 	newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
 	newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
 	newEvent.keyCode = keymap[[theEvent keyCode]];	
-	
-	NSString *chars = [theEvent characters];
-	NSUInteger numChars = [chars length];
-	
-//	NSLog(@"CHARS: %@", [chars characterAtIndex:0]);
-	if(numChars > 0) {
-		newEvent.unicodeChar = [chars characterAtIndex:0];
-	} else {
-		newEvent.unicodeChar = 0;
+    core->cocoaEvents.push_back(newEvent);
+    
+    CocoaEvent textEvent;
+    textEvent.eventGroup = CocoaEvent::INPUT_EVENT;
+    textEvent.eventCode = InputEvent::EVENT_TEXTINPUT;
+    
+    NSString *chars = [theEvent characters];
+    NSUInteger numChars = [chars length];
+	textEvent.text = [chars UTF8String];
+	if(numChars > 0 && !((unsigned char) textEvent.text[0] < ' ' || textEvent.text[0] == 127) && textEvent.text[0] > 0) {
+		core->cocoaEvents.push_back(textEvent);
 	}
 	
-	core->cocoaEvents.push_back(newEvent);	
-	core->unlockMutex(core->eventMutex);	
+	core->unlockMutex(core->eventMutex);
 	
 }
 
@@ -726,15 +726,7 @@
 	newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
 	newEvent.eventCode = InputEvent::EVENT_KEYUP;
 	newEvent.keyCode = keymap[[theEvent keyCode]];
-	
-	NSString *chars = [theEvent characters];
-	NSUInteger numChars = [chars length];
-	
-	if(numChars > 0)
-		newEvent.unicodeChar = [chars characterAtIndex:0];
-	else
-		newEvent.unicodeChar = 0;
-	
+    
 	core->cocoaEvents.push_back(newEvent);	
 	core->unlockMutex(core->eventMutex);		
 }

+ 15 - 13
src/view/win32/PolycodeView.cpp

@@ -127,19 +127,15 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
 	case WM_KEYDOWN:
 	case WM_SYSKEYDOWN:
 		if(core) {
-				wchar_t unicodeChar = 0;
-				MSG m;
-				m.hwnd = hWnd;
-				m.message = message;
-				m.wParam = wParam;
-				m.lParam = lParam;
-				m.time = 0;
-				if ( PeekMessage(&m, hWnd, 0, WM_USER, PM_NOREMOVE) && (m.message == WM_CHAR) ) {
-					GetMessage(&m, hWnd, 0, WM_USER);
-						unicodeChar = (wchar_t)m.wParam;
-				}
-
-			core->handleKeyDown(lParam,wParam, unicodeChar);
+			wchar_t unicodeChar = 0;
+			MSG m;
+			m.hwnd = hWnd;
+			m.message = message;
+			m.wParam = wParam;
+			m.lParam = lParam;
+			m.time = 0;
+
+			core->handleKeyDown(lParam,wParam);
 		}
 	break;
 	case WM_KEYUP:
@@ -147,6 +143,12 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
 		if(core)
 			core->handleKeyUp(lParam,wParam);
 	break;
+	//case WM_UNICHAR:
+	//	if (wParam == UNICODE_NOCHAR)
+	//		break;
+	case WM_CHAR:
+		core->handleTextInput(lParam, wParam);
+		break;
 	case WM_CLOSE:
 		if(core)
 			core->Shutdown();

Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff