summaryrefslogtreecommitdiff
path: root/src/SFML
diff options
context:
space:
mode:
authorChristoph Egger <Christoph.Egger@gmx.de>2010-04-07 22:00:13 +0200
committerChristoph Egger <Christoph.Egger@gmx.de>2010-04-07 22:00:13 +0200
commit46bf1cf82d855660c212ee3421dff3050ec8ffea (patch)
treef0fadfc004a86242ad096dee015ed7c20cc7f554 /src/SFML
parent25177f23f35afa0e6ebdd63d581003c298d3a7df (diff)
Imported Upstream version 1.6+repack1
Diffstat (limited to 'src/SFML')
-rwxr-xr-xsrc/SFML/Audio/Makefile2
-rwxr-xr-xsrc/SFML/Audio/Sound.cpp77
-rwxr-xr-xsrc/SFML/Audio/SoundBuffer.cpp30
-rwxr-xr-xsrc/SFML/Audio/SoundRecorder.cpp3
-rwxr-xr-xsrc/SFML/Audio/SoundStream.cpp63
-rwxr-xr-xsrc/SFML/Audio/stb_vorbis/stb_vorbis.c10076
-rwxr-xr-xsrc/SFML/Graphics/Font.cpp18
-rwxr-xr-xsrc/SFML/Graphics/FontLoader.cpp6
-rwxr-xr-xsrc/SFML/Graphics/Image.cpp20
-rwxr-xr-xsrc/SFML/Graphics/Makefile4
-rwxr-xr-xsrc/SFML/Graphics/RenderTarget.cpp4
-rwxr-xr-xsrc/SFML/Graphics/Sprite.cpp4
-rwxr-xr-xsrc/SFML/Makefile9
-rwxr-xr-xsrc/SFML/Network/Ftp.cpp9
-rwxr-xr-xsrc/SFML/Network/Http.cpp27
-rwxr-xr-xsrc/SFML/Network/Makefile2
-rwxr-xr-xsrc/SFML/Network/SocketTCP.cpp35
-rwxr-xr-xsrc/SFML/Network/SocketUDP.cpp25
-rwxr-xr-xsrc/SFML/System/Makefile2
-rwxr-xr-xsrc/SFML/System/Randomizer.cpp6
-rwxr-xr-xsrc/SFML/System/Unix/Initializer.cpp (renamed from src/SFML/System/Initializer.cpp)0
-rwxr-xr-xsrc/SFML/System/Unix/Thread.cpp6
-rwxr-xr-xsrc/SFML/System/Win32/Thread.cpp6
-rwxr-xr-xsrc/SFML/Window/Cocoa/AppController.h22
-rwxr-xr-xsrc/SFML/Window/Cocoa/AppController.mm177
-rwxr-xr-xsrc/SFML/Window/Cocoa/GLKit.h152
-rwxr-xr-xsrc/SFML/Window/Cocoa/GLKit.mm866
-rwxr-xr-xsrc/SFML/Window/Cocoa/Joystick.cpp9
-rwxr-xr-xsrc/SFML/Window/Cocoa/Joystick.hpp10
-rwxr-xr-xsrc/SFML/Window/Cocoa/WindowImplCocoa.hpp18
-rwxr-xr-xsrc/SFML/Window/Cocoa/WindowImplCocoa.mm486
-rwxr-xr-xsrc/SFML/Window/Input.cpp60
-rwxr-xr-xsrc/SFML/Window/Joystick.hpp18
-rwxr-xr-xsrc/SFML/Window/Linux/Joystick.cpp98
-rwxr-xr-xsrc/SFML/Window/Linux/Joystick.hpp26
-rwxr-xr-xsrc/SFML/Window/Linux/WindowImplX11.cpp13
-rwxr-xr-xsrc/SFML/Window/Makefile2
-rwxr-xr-xsrc/SFML/Window/Win32/Joystick.cpp83
-rwxr-xr-xsrc/SFML/Window/Win32/Joystick.hpp16
-rwxr-xr-xsrc/SFML/Window/Win32/WindowImplWin32.cpp226
-rwxr-xr-xsrc/SFML/Window/Win32/WindowImplWin32.hpp11
-rwxr-xr-xsrc/SFML/Window/Window.cpp4
-rwxr-xr-xsrc/SFML/Window/WindowImpl.cpp28
-rwxr-xr-xsrc/SFML/Window/WindowImpl.hpp13
44 files changed, 6650 insertions, 6122 deletions
diff --git a/src/SFML/Audio/Makefile b/src/SFML/Audio/Makefile
index 109a00a..0215e80 100755
--- a/src/SFML/Audio/Makefile
+++ b/src/SFML/Audio/Makefile
@@ -10,7 +10,7 @@ ifeq ($(STATIC), yes)
else
LIB = libsfml-audio.so
LIBNAME = $(LIBPATH)/$(LIB).$(VERSION)
- INSTALL = && $(LN) $(LNFLAGS) $(DESTLIBDIR)/$(LIB).$(VERSION) $(DESTLIBDIR)/$(LIB)
+ INSTALL = && $(LN) $(LNFLAGS) $(LIB).$(VERSION) $(DESTLIBDIR)/$(LIB)
endif
all: $(LIB)
diff --git a/src/SFML/Audio/Sound.cpp b/src/SFML/Audio/Sound.cpp
index 51a3bd9..84136db 100755
--- a/src/SFML/Audio/Sound.cpp
+++ b/src/SFML/Audio/Sound.cpp
@@ -46,15 +46,15 @@ Sound::Sound()
/// Construct the sound from its parameters
////////////////////////////////////////////////////////////
Sound::Sound(const SoundBuffer& Buffer, bool Loop, float Pitch, float Volume, const Vector3f& Position) :
-myBuffer(&Buffer)
+myBuffer(NULL)
{
ALCheck(alGenSources(1, &mySource));
- ALCheck(alSourcei (mySource, AL_BUFFER, Buffer.myBuffer));
- ALCheck(alSourcei (mySource, AL_LOOPING, Loop));
- ALCheck(alSourcef (mySource, AL_PITCH, Pitch));
- ALCheck(alSourcef (mySource, AL_GAIN, Volume * 0.01f));
- ALCheck(alSource3f(mySource, AL_POSITION, Position.x, Position.y, Position.z));
+ SetBuffer(Buffer);
+ SetLoop(Loop);
+ SetPitch(Pitch);
+ SetVolume(Volume);
+ SetPosition(Position);
}
@@ -63,15 +63,19 @@ myBuffer(&Buffer)
////////////////////////////////////////////////////////////
Sound::Sound(const Sound& Copy) :
AudioResource(Copy),
-myBuffer (Copy.myBuffer)
+myBuffer(NULL)
{
ALCheck(alGenSources(1, &mySource));
- ALCheck(alSourcei (mySource, AL_BUFFER, myBuffer ? myBuffer->myBuffer : 0));
- ALCheck(alSourcei (mySource, AL_LOOPING, Copy.GetLoop()));
- ALCheck(alSourcef (mySource, AL_PITCH, Copy.GetPitch()));
- ALCheck(alSourcef (mySource, AL_GAIN, Copy.GetVolume() * 0.01f));
- ALCheck(alSource3f(mySource, AL_POSITION, Copy.GetPosition().x, Copy.GetPosition().y, Copy.GetPosition().z));
+ if (Copy.myBuffer)
+ SetBuffer(*Copy.myBuffer);
+ SetLoop(Copy.GetLoop());
+ SetPitch(Copy.GetPitch());
+ SetVolume(Copy.GetVolume());
+ SetPosition(Copy.GetPosition());
+ SetRelativeToListener(Copy.IsRelativeToListener());
+ SetMinDistance(Copy.GetMinDistance());
+ SetAttenuation(Copy.GetAttenuation());
}
@@ -86,6 +90,7 @@ Sound::~Sound()
{
Stop();
ALCheck(alSourcei(mySource, AL_BUFFER, 0));
+ myBuffer->DetachSound(this);
}
ALCheck(alDeleteSources(1, &mySource));
}
@@ -124,8 +129,17 @@ void Sound::Stop()
////////////////////////////////////////////////////////////
void Sound::SetBuffer(const SoundBuffer& Buffer)
{
+ // First detach from the previous buffer
+ if (myBuffer)
+ {
+ Stop();
+ myBuffer->DetachSound(this);
+ }
+
+ // Assign and use the new buffer
myBuffer = &Buffer;
- ALCheck(alSourcei(mySource, AL_BUFFER, myBuffer ? myBuffer->myBuffer : 0));
+ myBuffer->AttachSound(this);
+ ALCheck(alSourcei(mySource, AL_BUFFER, myBuffer->myBuffer));
}
@@ -348,12 +362,43 @@ Sound::Status Sound::GetStatus() const
////////////////////////////////////////////////////////////
Sound& Sound::operator =(const Sound& Other)
{
- Sound Temp(Other);
+ // Here we don't use the copy-and-swap idiom, because it would mess up
+ // the list of sound instances contained in the buffers
- std::swap(mySource, Temp.mySource);
- std::swap(myBuffer, Temp.myBuffer);
+ // Detach the sound instance from the previous buffer (if any)
+ if (myBuffer)
+ {
+ Stop();
+ myBuffer->DetachSound(this);
+ myBuffer = NULL;
+ }
+
+ // Copy the sound attributes
+ if (Other.myBuffer)
+ SetBuffer(*Other.myBuffer);
+ SetLoop(Other.GetLoop());
+ SetPitch(Other.GetPitch());
+ SetVolume(Other.GetVolume());
+ SetPosition(Other.GetPosition());
+ SetRelativeToListener(Other.IsRelativeToListener());
+ SetMinDistance(Other.GetMinDistance());
+ SetAttenuation(Other.GetAttenuation());
return *this;
}
+
+////////////////////////////////////////////////////////////
+/// Reset the internal buffer
+////////////////////////////////////////////////////////////
+void Sound::ResetBuffer()
+{
+ // First stop the sound in case it is playing
+ Stop();
+
+ // Detach the buffer
+ ALCheck(alSourcei(mySource, AL_BUFFER, 0));
+ myBuffer = NULL;
+}
+
} // namespace sf
diff --git a/src/SFML/Audio/SoundBuffer.cpp b/src/SFML/Audio/SoundBuffer.cpp
index 7636a01..e726b13 100755
--- a/src/SFML/Audio/SoundBuffer.cpp
+++ b/src/SFML/Audio/SoundBuffer.cpp
@@ -27,6 +27,7 @@
////////////////////////////////////////////////////////////
#include <SFML/Audio/SoundBuffer.hpp>
#include <SFML/Audio/SoundFile.hpp>
+#include <SFML/Audio/Sound.hpp>
#include <SFML/Audio/AudioDevice.hpp>
#include <SFML/Audio/OpenAL.hpp>
#include <iostream>
@@ -55,7 +56,8 @@ AudioResource (Copy),
Resource<SoundBuffer>(Copy),
myBuffer (0),
mySamples (Copy.mySamples),
-myDuration (Copy.myDuration)
+myDuration (Copy.myDuration),
+mySounds () // don't copy the attached sounds
{
// Create the buffer
ALCheck(alGenBuffers(1, &myBuffer));
@@ -70,6 +72,11 @@ myDuration (Copy.myDuration)
////////////////////////////////////////////////////////////
SoundBuffer::~SoundBuffer()
{
+ // First detach the buffer from the sounds that use it (to avoid OpenAL errors)
+ for (SoundList::const_iterator it = mySounds.begin(); it != mySounds.end(); ++it)
+ (*it)->ResetBuffer();
+
+ // Destroy the buffer
if (myBuffer)
ALCheck(alDeleteBuffers(1, &myBuffer));
}
@@ -268,9 +275,10 @@ SoundBuffer& SoundBuffer::operator =(const SoundBuffer& Other)
{
SoundBuffer Temp(Other);
- mySamples.swap(Temp.mySamples);
+ std::swap(mySamples, Temp.mySamples);
std::swap(myBuffer, Temp.myBuffer);
std::swap(myDuration, Temp.myDuration);
+ std::swap(mySounds, Temp.mySounds); // swap sounds too, so that they are detached when Temp is destroyed
return *this;
}
@@ -305,4 +313,22 @@ bool SoundBuffer::Update(unsigned int ChannelsCount, unsigned int SampleRate)
return true;
}
+
+////////////////////////////////////////////////////////////
+/// Add a sound to the list of sounds that use this buffer
+////////////////////////////////////////////////////////////
+void SoundBuffer::AttachSound(Sound* Instance) const
+{
+ mySounds.insert(Instance);
+}
+
+
+////////////////////////////////////////////////////////////
+/// Remove a sound from the list of sounds that use this buffer
+////////////////////////////////////////////////////////////
+void SoundBuffer::DetachSound(Sound* Instance) const
+{
+ mySounds.erase(Instance);
+}
+
} // namespace sf
diff --git a/src/SFML/Audio/SoundRecorder.cpp b/src/SFML/Audio/SoundRecorder.cpp
index ac787b2..c12a49d 100755
--- a/src/SFML/Audio/SoundRecorder.cpp
+++ b/src/SFML/Audio/SoundRecorder.cpp
@@ -137,7 +137,8 @@ bool SoundRecorder::CanCapture()
{
ALCdevice* Device = priv::AudioDevice::GetInstance().GetDevice();
- return alcIsExtensionPresent(Device, "ALC_EXT_CAPTURE") != AL_FALSE;
+ return (alcIsExtensionPresent(Device, "ALC_EXT_CAPTURE") != AL_FALSE) ||
+ (alcIsExtensionPresent(Device, "ALC_EXT_capture") != AL_FALSE); // "bug" in Mac OS X 10.5 and 10.6
}
diff --git a/src/SFML/Audio/SoundStream.cpp b/src/SFML/Audio/SoundStream.cpp
index 4fb2ff2..9530d78 100755
--- a/src/SFML/Audio/SoundStream.cpp
+++ b/src/SFML/Audio/SoundStream.cpp
@@ -188,9 +188,10 @@ bool SoundStream::GetLoop() const
////////////////////////////////////////////////////////////
void SoundStream::Run()
{
- // Create buffers
+ // Create the buffers
ALCheck(alGenBuffers(BuffersCount, myBuffers));
- unsigned int EndBuffer = 0xFFFF;
+ for (int i = 0; i < BuffersCount; ++i)
+ myEndBuffers[i] = false;
// Fill the queue
bool RequestStop = FillQueue();
@@ -225,12 +226,21 @@ void SoundStream::Run()
ALuint Buffer;
ALCheck(alSourceUnqueueBuffers(Sound::mySource, 1, &Buffer));
+ // Find its number
+ unsigned int BufferNum = 0;
+ for (int i = 0; i < BuffersCount; ++i)
+ if (myBuffers[i] == Buffer)
+ {
+ BufferNum = i;
+ break;
+ }
+
// Retrieve its size and add it to the samples count
- if (Buffer == EndBuffer)
+ if (myEndBuffers[BufferNum])
{
// This was the last buffer: reset the sample count
mySamplesProcessed = 0;
- EndBuffer = 0xFFFF;
+ myEndBuffers[BufferNum] = false;
}
else
{
@@ -242,21 +252,8 @@ void SoundStream::Run()
// Fill it and push it back into the playing queue
if (!RequestStop)
{
- if (FillAndPushBuffer(Buffer))
- {
- // User requested to stop: check if we must loop or really stop
- if (myLoop && OnStart())
- {
- // Looping: mark the current buffer as the last one
- // (to know when to reset the sample count)
- EndBuffer = Buffer;
- }
- else
- {
- // Not looping or restart failed: request stop
- RequestStop = true;
- }
- }
+ if (FillAndPushBuffer(BufferNum))
+ RequestStop = true;
}
}
@@ -281,18 +278,38 @@ void SoundStream::Run()
/// Fill a new buffer with audio data, and push it to the
/// playing queue
////////////////////////////////////////////////////////////
-bool SoundStream::FillAndPushBuffer(unsigned int Buffer)
+bool SoundStream::FillAndPushBuffer(unsigned int BufferNum)
{
bool RequestStop = false;
// Acquire audio data
Chunk Data = {NULL, 0};
if (!OnGetData(Data))
- RequestStop = true;
+ {
+ // Mark the buffer as the last one (so that we know when to reset the playing position)
+ myEndBuffers[BufferNum] = true;
+
+ // Check if the stream must loop or stop
+ if (myLoop && OnStart())
+ {
+ // If we succeeded to restart and we previously had no data, try to fill the buffer once again
+ if (!Data.Samples || (Data.NbSamples == 0))
+ {
+ return FillAndPushBuffer(BufferNum);
+ }
+ }
+ else
+ {
+ // Not looping or restart failed: request stop
+ RequestStop = true;
+ }
+ }
- // Create and fill the buffer, and push it to the queue
+ // Fill the buffer if some data was returned
if (Data.Samples && Data.NbSamples)
{
+ unsigned int Buffer = myBuffers[BufferNum];
+
// Fill the buffer
ALsizei Size = static_cast<ALsizei>(Data.NbSamples) * sizeof(Int16);
ALCheck(alBufferData(Buffer, myFormat, Data.Samples, Size, mySampleRate));
@@ -314,7 +331,7 @@ bool SoundStream::FillQueue()
bool RequestStop = false;
for (int i = 0; (i < BuffersCount) && !RequestStop; ++i)
{
- if (FillAndPushBuffer(myBuffers[i]))
+ if (FillAndPushBuffer(i))
RequestStop = true;
}
diff --git a/src/SFML/Audio/stb_vorbis/stb_vorbis.c b/src/SFML/Audio/stb_vorbis/stb_vorbis.c
index e34405f..902761a 100755
--- a/src/SFML/Audio/stb_vorbis/stb_vorbis.c
+++ b/src/SFML/Audio/stb_vorbis/stb_vorbis.c
@@ -1,5039 +1,5039 @@
-// Ogg Vorbis I audio decoder -- version 0.99994
-//
-// Written in April 2007 by Sean Barrett, sponsored by RAD Game Tools.
-//
-// Placed in the public domain April 2007 by the author: no copyright is
-// claimed, and you may use it for any purpose you like.
-//
-// 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.
-//
-// Get the latest version and other information at:
-// http://nothings.org/stb_vorbis/
-
-
-// Todo:
-//
-// - seeking (note you can seek yourself using the pushdata API)
-//
-// Limitations:
-//
-// - floor 0 not supported (used in old ogg vorbis files)
-// - 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)
-//
-// All of these limitations may be removed in future versions.
-
-#include "stb_vorbis.h"
-
-#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
-
-// STB_VORBIS_CODEBOOK_SHORTS
-// The vorbis file format encodes VQ codebook floats as ax+b where a and
-// b are floating point per-codebook constants, and x is a 16-bit int.
-// Normally, stb_vorbis decodes them to floats rather than leaving them
-// as 16-bit ints and computing ax+b while decoding. This is a speed/space
-// tradeoff; you can save space by defining this flag.
-#ifndef STB_VORBIS_CODEBOOK_SHORTS
-#define STB_VORBIS_CODEBOOK_FLOATS
-#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) &&!defined(__FreeBSD__)
-#include <malloc.h>
-#endif
-
-#else
-#define NULL 0
-#endif
-
-#ifndef _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
-
-
-#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
-
-#ifdef STB_VORBIS_CODEBOOK_FLOATS
-typedef float codetype;
-#else
-typedef uint16 codetype;
-#endif
-
-// @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 after_previous_page_start;
- uint32 first_decoded_sample;
- 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;
-};
-
-extern int my_prof(int slot);
-//#define stb_prof my_prof
-
-#ifndef stb_prof
-#define stb_prof(x) 0
-#endif
-
-#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 not 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, size_t 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=i<<24, j=0; j < 8; ++j)
- s = (s << 1) ^ (s >= (1<<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 < (1U << 14))
- if (n < (1U << 4)) return 0 + log2_4[n ];
- else if (n < (1U << 9)) return 5 + log2_4[n >> 5];
- else return 10 + log2_4[n >> 10];
- else if (n < (1U << 24))
- if (n < (1U << 19)) return 15 + log2_4[n >> 15];
- else return 20 + log2_4[n >> 20];
- else if (n < (1U << 29)) return 25 + log2_4[n >> 25];
- else if (n < (1U << 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] = 1 << (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) { assert(0); return FALSE; }
- res = available[z];
- available[z] = 0;
- add_entry(c, bit_reverse(res), i, m++, len[i], values);
- // propogate availability up the tree
- if (z != len[i]) {
- 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];
- }
- }
- }
-}
-
-static int 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;
-
-int 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 += 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,i;
- // 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 != ~0 || loc1 != ~0) {
- // 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.after_previous_page_start = p.page_start;
- p.first_decoded_sample = 0;
- 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)
- 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;
-}
-
-static int32 get_bits_signed(vorb *f, int n)
-{
- uint32 z = get_bits(f, n);
- if (z & (1 << (n-1)))
- z += ~((1 << n) - 1);
- return (int32) 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 += 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);
-
- assert(c->sorted_codewords || c->codewords);
- // 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;
-}
-
-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);
-}
-
-#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
-
-#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
-#ifndef STB_VORBIS_CODEBOOK_FLOATS
- #define CODEBOOK_ELEMENT(c,off) (c->multiplicands[off] * c->delta_value + c->minimum_value)
- #define CODEBOOK_ELEMENT_FAST(c,off) (c->multiplicands[off] * c->delta_value)
- #define CODEBOOK_ELEMENT_BASE(c) (c->minimum_value)
-#else
- #define CODEBOOK_ELEMENT(c,off) (c->multiplicands[off])
- #define CODEBOOK_ELEMENT_FAST(c,off) (c->multiplicands[off])
- #define CODEBOOK_ELEMENT_BASE(c) (0)
-#endif
-
-static int codebook_decode_start(vorb *f, Codebook *c, int len)
-{
- 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,len);
- 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,len);
- 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;
- 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;
- 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;
- 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;
-}
-
-#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
-static int codebook_decode_deinterleave_repeat_2(vorb *f, Codebook *c, float **outputs, 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);
-
- 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*2 + effective > len * 2) {
- effective = len*2 - (p_inter*2 - c_inter);
- }
-
- {
- z *= c->dimensions;
- stb_prof(11);
- if (c->sequence_p) {
- // haven't optimized this case because I don't have any examples
- for (i=0; i < effective; ++i) {
- float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;
- outputs[c_inter][p_inter] += val;
- if (++c_inter == 2) { c_inter = 0; ++p_inter; }
- last = val;
- }
- } else {
- i=0;
- if (c_inter == 1) {
- float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;
- outputs[c_inter][p_inter] += val;
- c_inter = 0; ++p_inter;
- ++i;
- }
- {
- float *z0 = outputs[0];
- float *z1 = outputs[1];
- for (; i+1 < effective;) {
- z0[p_inter] += CODEBOOK_ELEMENT_FAST(c,z+i) + last;
- z1[p_inter] += CODEBOOK_ELEMENT_FAST(c,z+i+1) + last;
- ++p_inter;
- i += 2;
- }
- }
- if (i < effective) {
- float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;
- outputs[c_inter][p_inter] += val;
- if (++c_inter == 2) { c_inter = 0; ++p_inter; }
- }
- }
- }
-
- total_decode -= effective;
- }
- *c_inter_p = c_inter;
- *p_inter_p = p_inter;
- return TRUE;
-}
-#endif
-
-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;
- 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
-
- stb_prof(2);
- for (i=0; i < ch; ++i)
- if (!do_not_decode[i])
- memset(residue_buffers[i], 0, sizeof(float) * n);
-
- if (rtype == 2 && ch != 1) {
- int len = ch * n;
- for (j=0; j < ch; ++j)
- if (!do_not_decode[j])
- break;
- if (j == ch)
- goto done;
-
- stb_prof(3);
- for (pass=0; pass < 8; ++pass) {
- int pcount = 0, class_set = 0;
- if (ch == 2) {
- stb_prof(13);
- 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
- }
- stb_prof(5);
- 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;
- stb_prof(20); // accounts for X time
- #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_2(f, book, residue_buffers, &c_inter, &p_inter, n, r->part_size))
- goto done;
- #endif
- stb_prof(7);
- } else {
- z += r->part_size;
- c_inter = z & 1;
- p_inter = z >> 1;
- }
- }
- stb_prof(8);
- #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;
- stb_prof(22);
- if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
- goto done;
- stb_prof(3);
- } 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;
- stb_prof(22);
- if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
- goto done;
- stb_prof(3);
- } else {
- z += r->part_size;
- c_inter = z % ch;
- p_inter = z / ch;
- }
- }
- #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
- ++class_set;
- #endif
- }
- }
- }
- goto done;
- }
- stb_prof(9);
-
- 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:
- stb_prof(0);
- 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);
-}
-#else
-// 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];
- //acc += x[j] * cos(M_PI / n * (i + 0.5) * (j + 0.5));
- 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 k_off = -8;
- 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 n3_4 = n - n4, 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_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];
- draw_line(target, lx,ly, hx,hy, n2);
- 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]);
- }
- return TRUE;
-}
-
-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];
- int window_center;
-
-// WINDOWING
-
- n = f->blocksize[m->blockflag];
- window_center = n >> 1;
-
- map = &f->mapping[m->mapping];
-
-// FLOORS
- n2 = n >> 1;
-
- stb_prof(1);
- 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
- }
- }
- stb_prof(0);
- // 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;
- }
-
-// RESIDUE DECODE
- for (i=0; i < map->submaps; ++i) {
- float *residue_buffers[STB_VORBIS_MAX_CHANNELS];
- int r,t;
- 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];
- t = f->residue_types[r];
- 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);
-
-// INVERSE COUPLING
- stb_prof(14);
- 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;
- }
- }
-
- // finish decoding the floors
-#ifndef STB_VORBIS_NO_DEFER_FLOOR
- stb_prof(15);
- 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
- stb_prof(16);
- for (i=0; i < f->channels; ++i)
- inverse_mdct(f->channel_buffers[i], n, f, m->blockflag);
- stb_prof(0);
-
- // 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) {
- 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) {
- if (current_end < f->current_loc) {
- // negative truncation, that's impossible!
- *len = 0;
- } else {
- *len = current_end - f->current_loc;
- }
- *len += left_start;
- 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
- 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 == f->segment_count)
- 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;
- 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->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;
- } 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);
- 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;
- //assert(total != 0);
- } 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;
-
- 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));
- // 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) { ++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);
-
- 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;
- }
- 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;
- // 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) {
- int z = sparse ? c->sorted_values[j] : j, div=1;
- for (k=0; k < c->dimensions; ++k) {
- int off = (z / div) % c->lookup_values;
- c->multiplicands[j*c->dimensions + k] =
- #ifndef STB_VORBIS_CODEBOOK_FLOATS
- mults[off];
- #else
- mults[off]*c->delta_value + c->minimum_value;
- // in this case (and this case only) we could pre-expand c->sequence_p,
- // and throw away the decode logic for it; have to ALSO do
- // it in the case below, but it can only be done if
- // STB_VORBIS_CODEBOOK_FLOATS
- // !STB_VORBIS_DIVIDES_IN_CODEBOOK
- #endif
- div *= c->lookup_values;
- }
- }
- setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values);
- c->lookup_type = 2;
- }
- else
-#endif
- {
- c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->lookup_values);
- #ifndef STB_VORBIS_CODEBOOK_FLOATS
- memcpy(c->multiplicands, mults, sizeof(c->multiplicands[0]) * c->lookup_values);
- #else
- for (j=0; j < (int) c->lookup_values; ++j)
- c->multiplicands[j] = mults[j] * c->delta_value + c->minimum_value;
- #endif
- setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values);
- }
- skip:;
-
- #ifdef STB_VORBIS_CODEBOOK_FLOATS
- if (c->lookup_type == 2 && c->sequence_p) {
- for (j=1; j < (int) c->lookup_values; ++j)
- c->multiplicands[j] = c->multiplicands[j-1];
- c->sequence_p = 0;
- }
- #endif
- }
- }
-
- // 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));
- 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));
- 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);
- r->part_size = get_bits(f,24)+1;
- r->classifications = get_bits(f,6)+1;
- r->classbook = get_bits(f,8);
- 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);
- 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);
- 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));
- 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 (get_bits(f,1))
- m->submaps = get_bits(f,4);
- 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);
- #ifdef STB_VORBIS_NO_DEFER_FLOOR
- f->floor_buffers[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1/2);
- #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;
- 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) {
- 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);
- for (i=0; i < p->mapping_count; ++i)
- setup_free(p, p->mapping[i].chan);
- setup_free(p, p->mapping);
- for (i=0; i < p->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]);
+// Ogg Vorbis I audio decoder -- version 0.99994
+//
+// Written in April 2007 by Sean Barrett, sponsored by RAD Game Tools.
+//
+// Placed in the public domain April 2007 by the author: no copyright is
+// claimed, and you may use it for any purpose you like.
+//
+// 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.
+//
+// Get the latest version and other information at:
+// http://nothings.org/stb_vorbis/
+
+
+// Todo:
+//
+// - seeking (note you can seek yourself using the pushdata API)
+//
+// Limitations:
+//
+// - floor 0 not supported (used in old ogg vorbis files)
+// - 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)
+//
+// All of these limitations may be removed in future versions.
+
+#include "stb_vorbis.h"
+
+#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
+
+// STB_VORBIS_CODEBOOK_SHORTS
+// The vorbis file format encodes VQ codebook floats as ax+b where a and
+// b are floating point per-codebook constants, and x is a 16-bit int.
+// Normally, stb_vorbis decodes them to floats rather than leaving them
+// as 16-bit ints and computing ax+b while decoding. This is a speed/space
+// tradeoff; you can save space by defining this flag.
+#ifndef STB_VORBIS_CODEBOOK_SHORTS
+#define STB_VORBIS_CODEBOOK_FLOATS
+#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) &&!defined(__FreeBSD__)
+#include <malloc.h>
+#endif
+
+#else
+#define NULL 0
+#endif
+
+#ifndef _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
+
+
+#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
+
+#ifdef STB_VORBIS_CODEBOOK_FLOATS
+typedef float codetype;
+#else
+typedef uint16 codetype;
+#endif
+
+// @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 after_previous_page_start;
+ uint32 first_decoded_sample;
+ 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;
+};
+
+extern int my_prof(int slot);
+//#define stb_prof my_prof
+
+#ifndef stb_prof
+#define stb_prof(x) 0
+#endif
+
+#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 not 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, size_t 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=i<<24, j=0; j < 8; ++j)
+ s = (s << 1) ^ (s >= (1<<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 < (1U << 14))
+ if (n < (1U << 4)) return 0 + log2_4[n ];
+ else if (n < (1U << 9)) return 5 + log2_4[n >> 5];
+ else return 10 + log2_4[n >> 10];
+ else if (n < (1U << 24))
+ if (n < (1U << 19)) return 15 + log2_4[n >> 15];
+ else return 20 + log2_4[n >> 20];
+ else if (n < (1U << 29)) return 25 + log2_4[n >> 25];
+ else if (n < (1U << 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] = 1 << (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) { assert(0); return FALSE; }
+ res = available[z];
+ available[z] = 0;
+ add_entry(c, bit_reverse(res), i, m++, len[i], values);
+ // propogate availability up the tree
+ if (z != len[i]) {
+ 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];
+ }
+ }
+ }
+}
+
+static int 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;
+
+int 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 += 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,i;
+ // 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 != ~0 || loc1 != ~0) {
+ // 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.after_previous_page_start = p.page_start;
+ p.first_decoded_sample = 0;
+ 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)
+ 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;
+}
+
+static int32 get_bits_signed(vorb *f, int n)
+{
+ uint32 z = get_bits(f, n);
+ if (z & (1 << (n-1)))
+ z += ~((1 << n) - 1);
+ return (int32) 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 += 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);
+
+ assert(c->sorted_codewords || c->codewords);
+ // 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;
+}
+
+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);
+}
+
+#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
+
+#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
+#ifndef STB_VORBIS_CODEBOOK_FLOATS
+ #define CODEBOOK_ELEMENT(c,off) (c->multiplicands[off] * c->delta_value + c->minimum_value)
+ #define CODEBOOK_ELEMENT_FAST(c,off) (c->multiplicands[off] * c->delta_value)
+ #define CODEBOOK_ELEMENT_BASE(c) (c->minimum_value)
+#else
+ #define CODEBOOK_ELEMENT(c,off) (c->multiplicands[off])
+ #define CODEBOOK_ELEMENT_FAST(c,off) (c->multiplicands[off])
+ #define CODEBOOK_ELEMENT_BASE(c) (0)
+#endif
+
+static int codebook_decode_start(vorb *f, Codebook *c, int len)
+{
+ 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,len);
+ 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,len);
+ 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;
+ 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;
+ 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;
+ 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;
+}
+
+#ifndef STB_VORBIS_DIVIDES_IN_CODEBOOK
+static int codebook_decode_deinterleave_repeat_2(vorb *f, Codebook *c, float **outputs, 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);
+
+ 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*2 + effective > len * 2) {
+ effective = len*2 - (p_inter*2 - c_inter);
+ }
+
+ {
+ z *= c->dimensions;
+ stb_prof(11);
+ if (c->sequence_p) {
+ // haven't optimized this case because I don't have any examples
+ for (i=0; i < effective; ++i) {
+ float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;
+ outputs[c_inter][p_inter] += val;
+ if (++c_inter == 2) { c_inter = 0; ++p_inter; }
+ last = val;
+ }
+ } else {
+ i=0;
+ if (c_inter == 1) {
+ float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;
+ outputs[c_inter][p_inter] += val;
+ c_inter = 0; ++p_inter;
+ ++i;
+ }
+ {
+ float *z0 = outputs[0];
+ float *z1 = outputs[1];
+ for (; i+1 < effective;) {
+ z0[p_inter] += CODEBOOK_ELEMENT_FAST(c,z+i) + last;
+ z1[p_inter] += CODEBOOK_ELEMENT_FAST(c,z+i+1) + last;
+ ++p_inter;
+ i += 2;
+ }
+ }
+ if (i < effective) {
+ float val = CODEBOOK_ELEMENT_FAST(c,z+i) + last;
+ outputs[c_inter][p_inter] += val;
+ if (++c_inter == 2) { c_inter = 0; ++p_inter; }
+ }
+ }
+ }
+
+ total_decode -= effective;
+ }
+ *c_inter_p = c_inter;
+ *p_inter_p = p_inter;
+ return TRUE;
+}
+#endif
+
+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;
+ 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
+
+ stb_prof(2);
+ for (i=0; i < ch; ++i)
+ if (!do_not_decode[i])
+ memset(residue_buffers[i], 0, sizeof(float) * n);
+
+ if (rtype == 2 && ch != 1) {
+ int len = ch * n;
+ for (j=0; j < ch; ++j)
+ if (!do_not_decode[j])
+ break;
+ if (j == ch)
+ goto done;
+
+ stb_prof(3);
+ for (pass=0; pass < 8; ++pass) {
+ int pcount = 0, class_set = 0;
+ if (ch == 2) {
+ stb_prof(13);
+ 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
+ }
+ stb_prof(5);
+ 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;
+ stb_prof(20); // accounts for X time
+ #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_2(f, book, residue_buffers, &c_inter, &p_inter, n, r->part_size))
+ goto done;
+ #endif
+ stb_prof(7);
+ } else {
+ z += r->part_size;
+ c_inter = z & 1;
+ p_inter = z >> 1;
+ }
+ }
+ stb_prof(8);
+ #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;
+ stb_prof(22);
+ if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
+ goto done;
+ stb_prof(3);
+ } 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;
+ stb_prof(22);
+ if (!codebook_decode_deinterleave_repeat(f, book, residue_buffers, ch, &c_inter, &p_inter, n, r->part_size))
+ goto done;
+ stb_prof(3);
+ } else {
+ z += r->part_size;
+ c_inter = z % ch;
+ p_inter = z / ch;
+ }
+ }
+ #ifndef STB_VORBIS_DIVIDES_IN_RESIDUE
+ ++class_set;
+ #endif
+ }
+ }
+ }
+ goto done;
+ }
+ stb_prof(9);
+
+ 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:
+ stb_prof(0);
+ 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);
+}
+#else
+// 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];
+ //acc += x[j] * cos(M_PI / n * (i + 0.5) * (j + 0.5));
+ 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 k_off = -8;
+ 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 n3_4 = n - n4, 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_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];
+ draw_line(target, lx,ly, hx,hy, n2);
+ 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]);
+ }
+ return TRUE;
+}
+
+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];
+ int window_center;
+
+// WINDOWING
+
+ n = f->blocksize[m->blockflag];
+ window_center = n >> 1;
+
+ map = &f->mapping[m->mapping];
+
+// FLOORS
+ n2 = n >> 1;
+
+ stb_prof(1);
+ 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
+ }
+ }
+ stb_prof(0);
+ // 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;
+ }
+
+// RESIDUE DECODE
+ for (i=0; i < map->submaps; ++i) {
+ float *residue_buffers[STB_VORBIS_MAX_CHANNELS];
+ int r,t;
+ 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];
+ t = f->residue_types[r];
+ 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);
+
+// INVERSE COUPLING
+ stb_prof(14);
+ 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;
+ }
+ }
+
+ // finish decoding the floors
+#ifndef STB_VORBIS_NO_DEFER_FLOOR
+ stb_prof(15);
+ 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
+ stb_prof(16);
+ for (i=0; i < f->channels; ++i)
+ inverse_mdct(f->channel_buffers[i], n, f, m->blockflag);
+ stb_prof(0);
+
+ // 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) {
+ 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) {
+ if (current_end < f->current_loc) {
+ // negative truncation, that's impossible!
+ *len = 0;
+ } else {
+ *len = current_end - f->current_loc;
+ }
+ *len += left_start;
+ 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
+ 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 == f->segment_count)
+ 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;
+ 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->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;
+ } 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);
+ 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;
+ //assert(total != 0);
+ } 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;
+
+ 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));
+ // 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) { ++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);
+
+ 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;
+ }
+ 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;
+ // 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) {
+ int z = sparse ? c->sorted_values[j] : j, div=1;
+ for (k=0; k < c->dimensions; ++k) {
+ int off = (z / div) % c->lookup_values;
+ c->multiplicands[j*c->dimensions + k] =
+ #ifndef STB_VORBIS_CODEBOOK_FLOATS
+ mults[off];
+ #else
+ mults[off]*c->delta_value + c->minimum_value;
+ // in this case (and this case only) we could pre-expand c->sequence_p,
+ // and throw away the decode logic for it; have to ALSO do
+ // it in the case below, but it can only be done if
+ // STB_VORBIS_CODEBOOK_FLOATS
+ // !STB_VORBIS_DIVIDES_IN_CODEBOOK
+ #endif
+ div *= c->lookup_values;
+ }
+ }
+ setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values);
+ c->lookup_type = 2;
+ }
+ else
+#endif
+ {
+ c->multiplicands = (codetype *) setup_malloc(f, sizeof(c->multiplicands[0]) * c->lookup_values);
+ #ifndef STB_VORBIS_CODEBOOK_FLOATS
+ memcpy(c->multiplicands, mults, sizeof(c->multiplicands[0]) * c->lookup_values);
+ #else
+ for (j=0; j < (int) c->lookup_values; ++j)
+ c->multiplicands[j] = mults[j] * c->delta_value + c->minimum_value;
+ #endif
+ setup_temp_free(f, mults,sizeof(mults[0])*c->lookup_values);
+ }
+ skip:;
+
+ #ifdef STB_VORBIS_CODEBOOK_FLOATS
+ if (c->lookup_type == 2 && c->sequence_p) {
+ for (j=1; j < (int) c->lookup_values; ++j)
+ c->multiplicands[j] = c->multiplicands[j-1];
+ c->sequence_p = 0;
+ }
+ #endif
+ }
+ }
+
+ // 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));
+ 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));
+ 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);
+ r->part_size = get_bits(f,24)+1;
+ r->classifications = get_bits(f,6)+1;
+ r->classbook = get_bits(f,8);
+ 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);
+ 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);
+ 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));
+ 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 (get_bits(f,1))
+ m->submaps = get_bits(f,4);
+ 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);
+ #ifdef STB_VORBIS_NO_DEFER_FLOOR
+ f->floor_buffers[i] = (float *) setup_malloc(f, sizeof(float) * f->blocksize_1/2);
+ #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;
+ 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) {
+ 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);
+ for (i=0; i < p->mapping_count; ++i)
+ setup_free(p, p->mapping[i].chan);
+ setup_free(p, p->mapping);
+ for (i=0; i < p->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, 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 != ~0;
- 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
- 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, data, data_len);
- }
-
- f->stream = data;
- f->stream_end = 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 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 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 f->stream - data;
-}
-
-stb_vorbis *stb_vorbis_open_pushdata(
- unsigned char *data, int data_len, // the memory available for decoding
- int *data_used, // only defined if result is not NULL
- int *error, stb_vorbis_alloc *alloc)
-{
- stb_vorbis *f, p;
- vorbis_init(&p, alloc);
- p.stream = data;
- p.stream_end = 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 = 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 f->stream - f->stream_start;
- #ifndef STB_VORBIS_NO_STDIO
- return 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
- 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);
- }
- }
-}
-
-// seek is implemented with 'interpolation search'--this is like
-// binary search, but we use the data values to estimate the likely
-// location of the data item (plus a bit of a bias so when the
-// estimation is wrong we don't waste overly much time)
-
-#define SAMPLE_unknown 0xffffffff
-
-
-// ogg vorbis, in its insane infinite wisdom, only provides
-// information about the sample at the END of the page.
-// therefore we COULD have the data we need in the current
-// page, and not know it. we could just use the end location
-// as our only knowledge for bounds, seek back, and eventually
-// the binary search finds it. or we can try to be smart and
-// not waste time trying to locate more pages. we try to be
-// smart, since this data is already in memory anyway, so
-// doing needless I/O would be crazy!
-static int vorbis_analyze_page(stb_vorbis *f, ProbedPage *z)
-{
- uint8 header[27], lacing[255];
- uint8 packet_type[255];
- int num_packet, packet_start, previous =0;
- int i,len;
- uint32 samples;
-
- // record where the page starts
- z->page_start = stb_vorbis_get_file_offset(f);
-
- // parse the header
- getn(f, header, 27);
- assert(header[0] == 'O' && header[1] == 'g' && header[2] == 'g' && header[3] == 'S');
- 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] << 16);
-
- if (header[5] & 4) {
- // if this is the last page, it's not possible to work
- // backwards to figure out the first sample! whoops! fuck.
- z->first_decoded_sample = SAMPLE_unknown;
- set_file_offset(f, z->page_start);
- return 1;
- }
-
- // scan through the frames to determine the sample-count of each one...
- // our goal is the sample # of the first fully-decoded sample on the
- // page, which is the first decoded sample of the 2nd page
-
- num_packet=0;
-
- packet_start = ((header[5] & 1) == 0);
-
- for (i=0; i < header[26]; ++i) {
- if (packet_start) {
- uint8 n,b,m;
- if (lacing[i] == 0) goto bail; // trying to read from zero-length packet
- n = get8(f);
- // if bottom bit is non-zero, we've got corruption
- if (n & 1) goto bail;
- n >>= 1;
- b = ilog(f->mode_count-1);
- m = n >> b;
- n &= (1 << b)-1;
- if (n >= f->mode_count) goto bail;
- if (num_packet == 0 && f->mode_config[n].blockflag)
- previous = (m & 1);
- packet_type[num_packet++] = f->mode_config[n].blockflag;
- skip(f, lacing[i]-1);
- } else
- skip(f, lacing[i]);
- packet_start = (lacing[i] < 255);
- }
-
- // now that we know the sizes of all the pages, we can start determining
- // how much sample data there is.
-
- samples = 0;
-
- // for the last packet, we step by its whole length, because the definition
- // is that we encoded the end sample loc of the 'last packet completed',
- // where 'completed' refers to packets being split, and we are left to guess
- // what 'end sample loc' means. we assume it means ignoring the fact that
- // the last half of the data is useless without windowing against the next
- // packet... (so it's not REALLY complete in that sense)
- if (num_packet > 1)
- samples += f->blocksize[packet_type[num_packet-1]];
-
- for (i=num_packet-2; i >= 1; --i) {
- // now, for this packet, how many samples do we have that
- // do not overlap the following packet?
- if (packet_type[i] == 1)
- if (packet_type[i+1] == 1)
- samples += f->blocksize_1 >> 1;
- else
- samples += ((f->blocksize_1 - f->blocksize_0) >> 2) + (f->blocksize_0 >> 1);
- else
- samples += f->blocksize_0 >> 1;
- }
- // now, at this point, we've rewound to the very beginning of the
- // _second_ packet. if we entirely discard the first packet after
- // a seek, this will be exactly the right sample number. HOWEVER!
- // we can't as easily compute this number for the LAST page. The
- // only way to get the sample offset of the LAST page is to use
- // the end loc from the previous page. But what that returns us
- // is _exactly_ the place where we get our first non-overlapped
- // sample. (I think. Stupid spec for being ambiguous.) So for
- // consistency it's better to do that here, too. However, that
- // will then require us to NOT discard all of the first frame we
- // decode, in some cases, which means an even weirder frame size
- // and extra code. what a fucking pain.
-
- // we're going to discard the first packet if we
- // start the seek here, so we don't care about it. (we could actually
- // do better; if the first packet is long, and the previous packet
- // is short, there's actually data in the first half of the first
- // packet that doesn't need discarding... but not worth paying the
- // effort of tracking that of that here and in the seeking logic)
- // except crap, if we infer it from the _previous_ packet's end
- // location, we DO need to use that definition... and we HAVE to
- // infer the start loc of the LAST packet from the previous packet's
- // end location. fuck you, ogg vorbis.
-
- z->first_decoded_sample = z->last_decoded_sample - samples;
-
- // restore file state to where we were
- set_file_offset(f, z->page_start);
- return 1;
-
- // restore file state to where we were
- bail:
- set_file_offset(f, z->page_start);
- return 0;
-}
-
-static int vorbis_seek_frame_from_page(stb_vorbis *f, uint32 page_start, uint32 first_sample, uint32 target_sample, int fine)
-{
- int left_start, left_end, right_start, right_end, mode,i;
- int frame=0;
- uint32 frame_start;
- int frames_to_skip, data_to_skip;
-
- // first_sample is the sample # of the first sample that doesn't
- // overlap the previous page... note that this requires us to
- // _partially_ discard the first packet! bleh.
- set_file_offset(f, page_start);
-
- f->next_seg = -1; // force page resync
-
- frame_start = first_sample;
- // frame start is where the previous packet's last decoded sample
- // was, which corresponds to left_end... EXCEPT if the previous
- // packet was long and this packet is short? Probably a bug here.
-
-
- // now, we can start decoding frames... we'll only FAKE decode them,
- // until we find the frame that contains our sample; then we'll rewind,
- // and try again
- for (;;) {
- int start;
-
- if (!vorbis_decode_initial(f, &left_start, &left_end, &right_start, &right_end, &mode))
- return error(f, VORBIS_seek_failed);
-
- if (frame == 0)
- start = left_end;
- else
- start = left_start;
-
- // the window starts at left_start; the last valid sample we generate
- // before the next frame's window start is right_start-1
- if (target_sample < frame_start + right_start-start)
- break;
-
- flush_packet(f);
- if (f->eof)
- return error(f, VORBIS_seek_failed);
-
- frame_start += right_start - start;
-
- ++frame;
- }
-
- // ok, at this point, the sample we want is contained in frame #'frame'
-
- // to decode frame #'frame' normally, we have to decode the
- // previous frame first... but if it's the FIRST frame of the page
- // we can't. if it's the first frame, it means it falls in the part
- // of the first frame that doesn't overlap either of the other frames.
- // so, if we have to handle that case for the first frame, we might
- // as well handle it for all of them, so:
- if (target_sample > frame_start + (left_end - left_start)) {
- // so what we want to do is go ahead and just immediately decode
- // this frame, but then make it so the next get_frame_float() uses
- // this already-decoded data? or do we want to go ahead and rewind,
- // and leave a flag saying to skip the first N data? let's do that
- frames_to_skip = frame; // if this is frame #1, skip 1 frame (#0)
- data_to_skip = left_end - left_start;
- } else {
- // otherwise, we want to skip frames 0, 1, 2, ... frame-2
- // (which means frame-2+1 total frames) then decode frame-1,
- // then leave frame pending
- frames_to_skip = frame - 1;
- assert(frames_to_skip >= 0);
- data_to_skip = -1;
- }
-
- set_file_offset(f, page_start);
- f->next_seg = - 1; // force page resync
-
- for (i=0; i < frames_to_skip; ++i) {
- maybe_start_packet(f);
- flush_packet(f);
- }
-
- if (data_to_skip >= 0) {
- int i,j,n = f->blocksize_0 >> 1;
- f->discard_samples_deferred = data_to_skip;
- for (i=0; i < f->channels; ++i)
- for (j=0; j < n; ++j)
- f->previous_window[i][j] = 0;
- f->previous_length = n;
- frame_start += data_to_skip;
- } else {
- f->previous_length = 0;
- vorbis_pump_first_frame(f);
- }
-
- // at this point, the NEXT decoded frame will generate the desired sample
- if (fine) {
- // so if we're doing sample accurate streaming, we want to go ahead and decode it!
- if (target_sample != frame_start) {
- int n;
- stb_vorbis_get_frame_float(f, &n, NULL);
- assert(target_sample > frame_start);
- assert(f->channel_buffer_start + (int) (target_sample-frame_start) < f->channel_buffer_end);
- f->channel_buffer_start += (target_sample - frame_start);
- }
- }
-
- return 0;
-}
-
-static int vorbis_seek_base(stb_vorbis *f, unsigned int sample_number, int fine)
-{
- ProbedPage p[2],q;
- if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing);
-
- // do we know the location of the last page?
- if (f->p_last.page_start == 0) {
- uint32 z = stb_vorbis_stream_length_in_samples(f);
- if (z == 0) return error(f, VORBIS_cant_find_last_page);
- }
-
- p[0] = f->p_first;
- p[1] = f->p_last;
-
- if (sample_number >= f->p_last.last_decoded_sample)
- sample_number = f->p_last.last_decoded_sample-1;
-
- if (sample_number < f->p_first.last_decoded_sample) {
- vorbis_seek_frame_from_page(f, p[0].page_start, 0, sample_number, fine);
- return 0;
- } else {
- int attempts=0;
- while (p[0].page_end < p[1].page_start) {
- uint32 probe;
- uint32 start_offset, end_offset;
- uint32 start_sample, end_sample;
-
- // copy these into local variables so we can tweak them
- // if any are unknown
- start_offset = p[0].page_end;
- end_offset = p[1].after_previous_page_start; // an address known to seek to page p[1]
- start_sample = p[0].last_decoded_sample;
- end_sample = p[1].last_decoded_sample;
-
- // currently there is no such tweaking logic needed/possible?
- if (start_sample == SAMPLE_unknown || end_sample == SAMPLE_unknown)
- return error(f, VORBIS_seek_failed);
-
- // now we want to lerp between these for the target samples...
-
- // step 1: we need to bias towards the page start...
- if (start_offset + 4000 < end_offset)
- end_offset -= 4000;
-
- // now compute an interpolated search loc
- probe = start_offset + (int) floor((float) (end_offset - start_offset) / (end_sample - start_sample) * (sample_number - start_sample));
-
- // next we need to bias towards binary search...
- // code is a little wonky to allow for full 32-bit unsigned values
- if (attempts >= 4) {
- uint32 probe2 = start_offset + ((end_offset - start_offset) >> 1);
- if (attempts >= 8)
- probe = probe2;
- else if (probe < probe2)
- probe = probe + ((probe2 - probe) >> 1);
- else
- probe = probe2 + ((probe - probe2) >> 1);
- }
- ++attempts;
-
- set_file_offset(f, probe);
- if (!vorbis_find_page(f, NULL, NULL)) return error(f, VORBIS_seek_failed);
- if (!vorbis_analyze_page(f, &q)) return error(f, VORBIS_seek_failed);
- q.after_previous_page_start = probe;
-
- // it's possible we've just found the last page again
- if (q.page_start == p[1].page_start) {
- p[1] = q;
- continue;
- }
-
- if (sample_number < q.last_decoded_sample)
- p[1] = q;
- else
- p[0] = q;
- }
-
- if (p[0].last_decoded_sample <= sample_number && sample_number < p[1].last_decoded_sample) {
- vorbis_seek_frame_from_page(f, p[1].page_start, p[0].last_decoded_sample, sample_number, fine);
- return 0;
- }
- return error(f, VORBIS_seek_failed);
- }
-}
-
-int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number)
-{
- return vorbis_seek_base(f, sample_number, FALSE);
-}
-
-int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number)
-{
- return vorbis_seek_base(f, sample_number, TRUE);
-}
-
-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) {
- 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, (int unsigned *)&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, (int unsigned *)&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;
- f->p_last.first_decoded_sample = SAMPLE_unknown;
- f->p_last.after_previous_page_start = previous_safe;
-
- 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, stb_vorbis_alloc *alloc, unsigned int length)
-{
- stb_vorbis *f, p;
- vorbis_init(&p, alloc);
- p.f = file;
- p.f_start = 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, stb_vorbis_alloc *alloc)
-{
- unsigned int len, start;
- start = ftell(file);
- fseek(file, 0, SEEK_END);
- len = 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(char *filename, int *error, 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(unsigned char *data, int len, int *error, stb_vorbis_alloc *alloc)
-{
- stb_vorbis *f, p;
- if (data == NULL) return NULL;
- vorbis_init(&p, alloc);
- p.stream = data;
- p.stream_end = data + len;
- p.stream_start = 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
- // 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(x,s) ((temp = (x) + MAGIC(s)), (*(int *)&temp) - ADDEND(s))
- #define check_endianness()
- typedef char stb_vorbis_float_size_test[sizeof(float)==4 && sizeof(int) == 4];
- #define FASTDEF(x) x
-#else
- #define FAST_SCALED_FLOAT_TO_INT(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;
- FASTDEF(float temp);
- check_endianness();
- for (i=0; i < len; ++i) {
- int v = FAST_SCALED_FLOAT_TO_INT(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;
- FASTDEF(float temp);
- 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) {
- int v = FAST_SCALED_FLOAT_TO_INT(buffer[i],15);
- if ((unsigned int) (v + 32768) > 65535)
- v = v < 0 ? -32768 : 32767;
- output[o+i] = v;
- }
- }
-}
-
-static int channel_selector[3][2] = { {0}, {PLAYBACK_MONO}, {PLAYBACK_LEFT, PLAYBACK_RIGHT} };
-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;
- FASTDEF(float temp);
- // 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) {
- int v = FAST_SCALED_FLOAT_TO_INT(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], 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;
- FASTDEF(float temp);
- for (j=0; j < len; ++j) {
- for (i=0; i < limit; ++i) {
- int v = FAST_SCALED_FLOAT_TO_INT(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(char *filename, int *channels, 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;
- 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;
- return data_len;
-}
-#endif // NO_STDIO
-
-int stb_vorbis_decode_memory(uint8 *mem, int len, int *channels, 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;
- 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;
- return data_len;
-}
-#endif
-
-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+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
-
-#endif // STB_VORBIS_HEADER_ONLY
+ }
+ #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, 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 != ~0;
+ 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
+ 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, data, data_len);
+ }
+
+ f->stream = data;
+ f->stream_end = 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 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 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 f->stream - data;
+}
+
+stb_vorbis *stb_vorbis_open_pushdata(
+ unsigned char *data, int data_len, // the memory available for decoding
+ int *data_used, // only defined if result is not NULL
+ int *error, stb_vorbis_alloc *alloc)
+{
+ stb_vorbis *f, p;
+ vorbis_init(&p, alloc);
+ p.stream = data;
+ p.stream_end = 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 = 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 f->stream - f->stream_start;
+ #ifndef STB_VORBIS_NO_STDIO
+ return 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
+ 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);
+ }
+ }
+}
+
+// seek is implemented with 'interpolation search'--this is like
+// binary search, but we use the data values to estimate the likely
+// location of the data item (plus a bit of a bias so when the
+// estimation is wrong we don't waste overly much time)
+
+#define SAMPLE_unknown 0xffffffff
+
+
+// ogg vorbis, in its insane infinite wisdom, only provides
+// information about the sample at the END of the page.
+// therefore we COULD have the data we need in the current
+// page, and not know it. we could just use the end location
+// as our only knowledge for bounds, seek back, and eventually
+// the binary search finds it. or we can try to be smart and
+// not waste time trying to locate more pages. we try to be
+// smart, since this data is already in memory anyway, so
+// doing needless I/O would be crazy!
+static int vorbis_analyze_page(stb_vorbis *f, ProbedPage *z)
+{
+ uint8 header[27], lacing[255];
+ uint8 packet_type[255];
+ int num_packet, packet_start, previous =0;
+ int i,len;
+ uint32 samples;
+
+ // record where the page starts
+ z->page_start = stb_vorbis_get_file_offset(f);
+
+ // parse the header
+ getn(f, header, 27);
+ assert(header[0] == 'O' && header[1] == 'g' && header[2] == 'g' && header[3] == 'S');
+ 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] << 16);
+
+ if (header[5] & 4) {
+ // if this is the last page, it's not possible to work
+ // backwards to figure out the first sample! whoops! fuck.
+ z->first_decoded_sample = SAMPLE_unknown;
+ set_file_offset(f, z->page_start);
+ return 1;
+ }
+
+ // scan through the frames to determine the sample-count of each one...
+ // our goal is the sample # of the first fully-decoded sample on the
+ // page, which is the first decoded sample of the 2nd page
+
+ num_packet=0;
+
+ packet_start = ((header[5] & 1) == 0);
+
+ for (i=0; i < header[26]; ++i) {
+ if (packet_start) {
+ uint8 n,b,m;
+ if (lacing[i] == 0) goto bail; // trying to read from zero-length packet
+ n = get8(f);
+ // if bottom bit is non-zero, we've got corruption
+ if (n & 1) goto bail;
+ n >>= 1;
+ b = ilog(f->mode_count-1);
+ m = n >> b;
+ n &= (1 << b)-1;
+ if (n >= f->mode_count) goto bail;
+ if (num_packet == 0 && f->mode_config[n].blockflag)
+ previous = (m & 1);
+ packet_type[num_packet++] = f->mode_config[n].blockflag;
+ skip(f, lacing[i]-1);
+ } else
+ skip(f, lacing[i]);
+ packet_start = (lacing[i] < 255);
+ }
+
+ // now that we know the sizes of all the pages, we can start determining
+ // how much sample data there is.
+
+ samples = 0;
+
+ // for the last packet, we step by its whole length, because the definition
+ // is that we encoded the end sample loc of the 'last packet completed',
+ // where 'completed' refers to packets being split, and we are left to guess
+ // what 'end sample loc' means. we assume it means ignoring the fact that
+ // the last half of the data is useless without windowing against the next
+ // packet... (so it's not REALLY complete in that sense)
+ if (num_packet > 1)
+ samples += f->blocksize[packet_type[num_packet-1]];
+
+ for (i=num_packet-2; i >= 1; --i) {
+ // now, for this packet, how many samples do we have that
+ // do not overlap the following packet?
+ if (packet_type[i] == 1)
+ if (packet_type[i+1] == 1)
+ samples += f->blocksize_1 >> 1;
+ else
+ samples += ((f->blocksize_1 - f->blocksize_0) >> 2) + (f->blocksize_0 >> 1);
+ else
+ samples += f->blocksize_0 >> 1;
+ }
+ // now, at this point, we've rewound to the very beginning of the
+ // _second_ packet. if we entirely discard the first packet after
+ // a seek, this will be exactly the right sample number. HOWEVER!
+ // we can't as easily compute this number for the LAST page. The
+ // only way to get the sample offset of the LAST page is to use
+ // the end loc from the previous page. But what that returns us
+ // is _exactly_ the place where we get our first non-overlapped
+ // sample. (I think. Stupid spec for being ambiguous.) So for
+ // consistency it's better to do that here, too. However, that
+ // will then require us to NOT discard all of the first frame we
+ // decode, in some cases, which means an even weirder frame size
+ // and extra code. what a fucking pain.
+
+ // we're going to discard the first packet if we
+ // start the seek here, so we don't care about it. (we could actually
+ // do better; if the first packet is long, and the previous packet
+ // is short, there's actually data in the first half of the first
+ // packet that doesn't need discarding... but not worth paying the
+ // effort of tracking that of that here and in the seeking logic)
+ // except crap, if we infer it from the _previous_ packet's end
+ // location, we DO need to use that definition... and we HAVE to
+ // infer the start loc of the LAST packet from the previous packet's
+ // end location. fuck you, ogg vorbis.
+
+ z->first_decoded_sample = z->last_decoded_sample - samples;
+
+ // restore file state to where we were
+ set_file_offset(f, z->page_start);
+ return 1;
+
+ // restore file state to where we were
+ bail:
+ set_file_offset(f, z->page_start);
+ return 0;
+}
+
+static int vorbis_seek_frame_from_page(stb_vorbis *f, uint32 page_start, uint32 first_sample, uint32 target_sample, int fine)
+{
+ int left_start, left_end, right_start, right_end, mode,i;
+ int frame=0;
+ uint32 frame_start;
+ int frames_to_skip, data_to_skip;
+
+ // first_sample is the sample # of the first sample that doesn't
+ // overlap the previous page... note that this requires us to
+ // _partially_ discard the first packet! bleh.
+ set_file_offset(f, page_start);
+
+ f->next_seg = -1; // force page resync
+
+ frame_start = first_sample;
+ // frame start is where the previous packet's last decoded sample
+ // was, which corresponds to left_end... EXCEPT if the previous
+ // packet was long and this packet is short? Probably a bug here.
+
+
+ // now, we can start decoding frames... we'll only FAKE decode them,
+ // until we find the frame that contains our sample; then we'll rewind,
+ // and try again
+ for (;;) {
+ int start;
+
+ if (!vorbis_decode_initial(f, &left_start, &left_end, &right_start, &right_end, &mode))
+ return error(f, VORBIS_seek_failed);
+
+ if (frame == 0)
+ start = left_end;
+ else
+ start = left_start;
+
+ // the window starts at left_start; the last valid sample we generate
+ // before the next frame's window start is right_start-1
+ if (target_sample < frame_start + right_start-start)
+ break;
+
+ flush_packet(f);
+ if (f->eof)
+ return error(f, VORBIS_seek_failed);
+
+ frame_start += right_start - start;
+
+ ++frame;
+ }
+
+ // ok, at this point, the sample we want is contained in frame #'frame'
+
+ // to decode frame #'frame' normally, we have to decode the
+ // previous frame first... but if it's the FIRST frame of the page
+ // we can't. if it's the first frame, it means it falls in the part
+ // of the first frame that doesn't overlap either of the other frames.
+ // so, if we have to handle that case for the first frame, we might
+ // as well handle it for all of them, so:
+ if (target_sample > frame_start + (left_end - left_start)) {
+ // so what we want to do is go ahead and just immediately decode
+ // this frame, but then make it so the next get_frame_float() uses
+ // this already-decoded data? or do we want to go ahead and rewind,
+ // and leave a flag saying to skip the first N data? let's do that
+ frames_to_skip = frame; // if this is frame #1, skip 1 frame (#0)
+ data_to_skip = left_end - left_start;
+ } else {
+ // otherwise, we want to skip frames 0, 1, 2, ... frame-2
+ // (which means frame-2+1 total frames) then decode frame-1,
+ // then leave frame pending
+ frames_to_skip = frame - 1;
+ assert(frames_to_skip >= 0);
+ data_to_skip = -1;
+ }
+
+ set_file_offset(f, page_start);
+ f->next_seg = - 1; // force page resync
+
+ for (i=0; i < frames_to_skip; ++i) {
+ maybe_start_packet(f);
+ flush_packet(f);
+ }
+
+ if (data_to_skip >= 0) {
+ int i,j,n = f->blocksize_0 >> 1;
+ f->discard_samples_deferred = data_to_skip;
+ for (i=0; i < f->channels; ++i)
+ for (j=0; j < n; ++j)
+ f->previous_window[i][j] = 0;
+ f->previous_length = n;
+ frame_start += data_to_skip;
+ } else {
+ f->previous_length = 0;
+ vorbis_pump_first_frame(f);
+ }
+
+ // at this point, the NEXT decoded frame will generate the desired sample
+ if (fine) {
+ // so if we're doing sample accurate streaming, we want to go ahead and decode it!
+ if (target_sample != frame_start) {
+ int n;
+ stb_vorbis_get_frame_float(f, &n, NULL);
+ assert(target_sample > frame_start);
+ assert(f->channel_buffer_start + (int) (target_sample-frame_start) < f->channel_buffer_end);
+ f->channel_buffer_start += (target_sample - frame_start);
+ }
+ }
+
+ return 0;
+}
+
+static int vorbis_seek_base(stb_vorbis *f, unsigned int sample_number, int fine)
+{
+ ProbedPage p[2],q;
+ if (IS_PUSH_MODE(f)) return error(f, VORBIS_invalid_api_mixing);
+
+ // do we know the location of the last page?
+ if (f->p_last.page_start == 0) {
+ uint32 z = stb_vorbis_stream_length_in_samples(f);
+ if (z == 0) return error(f, VORBIS_cant_find_last_page);
+ }
+
+ p[0] = f->p_first;
+ p[1] = f->p_last;
+
+ if (sample_number >= f->p_last.last_decoded_sample)
+ sample_number = f->p_last.last_decoded_sample-1;
+
+ if (sample_number < f->p_first.last_decoded_sample) {
+ vorbis_seek_frame_from_page(f, p[0].page_start, 0, sample_number, fine);
+ return 0;
+ } else {
+ int attempts=0;
+ while (p[0].page_end < p[1].page_start) {
+ uint32 probe;
+ uint32 start_offset, end_offset;
+ uint32 start_sample, end_sample;
+
+ // copy these into local variables so we can tweak them
+ // if any are unknown
+ start_offset = p[0].page_end;
+ end_offset = p[1].after_previous_page_start; // an address known to seek to page p[1]
+ start_sample = p[0].last_decoded_sample;
+ end_sample = p[1].last_decoded_sample;
+
+ // currently there is no such tweaking logic needed/possible?
+ if (start_sample == SAMPLE_unknown || end_sample == SAMPLE_unknown)
+ return error(f, VORBIS_seek_failed);
+
+ // now we want to lerp between these for the target samples...
+
+ // step 1: we need to bias towards the page start...
+ if (start_offset + 4000 < end_offset)
+ end_offset -= 4000;
+
+ // now compute an interpolated search loc
+ probe = start_offset + (int) floor((float) (end_offset - start_offset) / (end_sample - start_sample) * (sample_number - start_sample));
+
+ // next we need to bias towards binary search...
+ // code is a little wonky to allow for full 32-bit unsigned values
+ if (attempts >= 4) {
+ uint32 probe2 = start_offset + ((end_offset - start_offset) >> 1);
+ if (attempts >= 8)
+ probe = probe2;
+ else if (probe < probe2)
+ probe = probe + ((probe2 - probe) >> 1);
+ else
+ probe = probe2 + ((probe - probe2) >> 1);
+ }
+ ++attempts;
+
+ set_file_offset(f, probe);
+ if (!vorbis_find_page(f, NULL, NULL)) return error(f, VORBIS_seek_failed);
+ if (!vorbis_analyze_page(f, &q)) return error(f, VORBIS_seek_failed);
+ q.after_previous_page_start = probe;
+
+ // it's possible we've just found the last page again
+ if (q.page_start == p[1].page_start) {
+ p[1] = q;
+ continue;
+ }
+
+ if (sample_number < q.last_decoded_sample)
+ p[1] = q;
+ else
+ p[0] = q;
+ }
+
+ if (p[0].last_decoded_sample <= sample_number && sample_number < p[1].last_decoded_sample) {
+ vorbis_seek_frame_from_page(f, p[1].page_start, p[0].last_decoded_sample, sample_number, fine);
+ return 0;
+ }
+ return error(f, VORBIS_seek_failed);
+ }
+}
+
+int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number)
+{
+ return vorbis_seek_base(f, sample_number, FALSE);
+}
+
+int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number)
+{
+ return vorbis_seek_base(f, sample_number, TRUE);
+}
+
+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) {
+ 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, (int unsigned *)&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, (int unsigned *)&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;
+ f->p_last.first_decoded_sample = SAMPLE_unknown;
+ f->p_last.after_previous_page_start = previous_safe;
+
+ 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, stb_vorbis_alloc *alloc, unsigned int length)
+{
+ stb_vorbis *f, p;
+ vorbis_init(&p, alloc);
+ p.f = file;
+ p.f_start = 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, stb_vorbis_alloc *alloc)
+{
+ unsigned int len, start;
+ start = ftell(file);
+ fseek(file, 0, SEEK_END);
+ len = 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(char *filename, int *error, 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(unsigned char *data, int len, int *error, stb_vorbis_alloc *alloc)
+{
+ stb_vorbis *f, p;
+ if (data == NULL) return NULL;
+ vorbis_init(&p, alloc);
+ p.stream = data;
+ p.stream_end = data + len;
+ p.stream_start = 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
+ // 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(x,s) ((temp = (x) + MAGIC(s)), (*(int *)&temp) - ADDEND(s))
+ #define check_endianness()
+ typedef char stb_vorbis_float_size_test[sizeof(float)==4 && sizeof(int) == 4];
+ #define FASTDEF(x) x
+#else
+ #define FAST_SCALED_FLOAT_TO_INT(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;
+ FASTDEF(float temp);
+ check_endianness();
+ for (i=0; i < len; ++i) {
+ int v = FAST_SCALED_FLOAT_TO_INT(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;
+ FASTDEF(float temp);
+ 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) {
+ int v = FAST_SCALED_FLOAT_TO_INT(buffer[i],15);
+ if ((unsigned int) (v + 32768) > 65535)
+ v = v < 0 ? -32768 : 32767;
+ output[o+i] = v;
+ }
+ }
+}
+
+static int channel_selector[3][2] = { {0}, {PLAYBACK_MONO}, {PLAYBACK_LEFT, PLAYBACK_RIGHT} };
+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;
+ FASTDEF(float temp);
+ // 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) {
+ int v = FAST_SCALED_FLOAT_TO_INT(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], 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;
+ FASTDEF(float temp);
+ for (j=0; j < len; ++j) {
+ for (i=0; i < limit; ++i) {
+ int v = FAST_SCALED_FLOAT_TO_INT(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(char *filename, int *channels, 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;
+ 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;
+ return data_len;
+}
+#endif // NO_STDIO
+
+int stb_vorbis_decode_memory(uint8 *mem, int len, int *channels, 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;
+ 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;
+ return data_len;
+}
+#endif
+
+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+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
+
+#endif // STB_VORBIS_HEADER_ONLY
diff --git a/src/SFML/Graphics/Font.cpp b/src/SFML/Graphics/Font.cpp
index df2e563..36c4c28 100755
--- a/src/SFML/Graphics/Font.cpp
+++ b/src/SFML/Graphics/Font.cpp
@@ -158,14 +158,28 @@ const Image& Font::GetImage() const
////////////////////////////////////////////////////////////
const Font& Font::GetDefaultFont()
{
- static Font DefaultFont;
- static bool DefaultFontLoaded = false;
+#if defined(SFML_SYSTEM_WINDOWS) && defined(SFML_DYNAMIC)
+
+ // On Windows dynamic build, the default font causes a crash at global exit.
+ // This is a temporary workaround that turns the crash into a memory leak.
+ // Note that this bug doesn't exist anymore in SFML 2.
+ static Font* DefaultFontPtr = new Font;
+ Font& DefaultFont = *DefaultFontPtr;
+
+#else
+
+ static Font DefaultFont;
+
+#endif
+
+ // Get the raw data of the Arial font file into an array, so that we can load it into the font
static const char DefaultFontData[] =
{
#include <SFML/Graphics/Arial.hpp>
};
// Load the default font on first call
+ static bool DefaultFontLoaded = false;
if (!DefaultFontLoaded)
{
DefaultFont.LoadFromMemory(DefaultFontData, sizeof(DefaultFontData), 30);
diff --git a/src/SFML/Graphics/FontLoader.cpp b/src/SFML/Graphics/FontLoader.cpp
index 922cac9..0f6e67b 100755
--- a/src/SFML/Graphics/FontLoader.cpp
+++ b/src/SFML/Graphics/FontLoader.cpp
@@ -213,10 +213,6 @@ FT_Error FontLoader::CreateBitmapFont(FT_Face FontFace, unsigned int CharSize, c
FT_Glyph_To_Bitmap(&Glyph, FT_RENDER_MODE_NORMAL, 0, 1);
FT_BitmapGlyph BitmapGlyph = (FT_BitmapGlyph)Glyph;
- // Should we handle other pixel modes ?
- if (BitmapGlyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
- return FT_Err_Cannot_Render_Glyph;
-
// Add it to the sorted table of glyphs
Glyphs.insert(std::make_pair(BitmapGlyph, Charset[i]));
}
@@ -293,7 +289,7 @@ FT_Error FontLoader::CreateBitmapFont(FT_Face FontFace, unsigned int CharSize, c
for (std::size_t i = 0; i < Charset.size(); ++i)
{
Uint32 CurChar = Charset[i];
- LoadedFont.myGlyphs[CurChar].TexCoords = LoadedFont.myTexture.GetTexCoords(Coords[CurChar], false);
+ LoadedFont.myGlyphs[CurChar].TexCoords = LoadedFont.myTexture.GetTexCoords(Coords[CurChar]);
}
// Update the character size (it may have been changed by the function)
diff --git a/src/SFML/Graphics/Image.cpp b/src/SFML/Graphics/Image.cpp
index f90673c..14346fb 100755
--- a/src/SFML/Graphics/Image.cpp
+++ b/src/SFML/Graphics/Image.cpp
@@ -542,25 +542,15 @@ bool Image::IsSmooth() const
/// Convert a subrect expressed in pixels, into float
/// texture coordinates
////////////////////////////////////////////////////////////
-FloatRect Image::GetTexCoords(const IntRect& Rect, bool Adjust) const
+FloatRect Image::GetTexCoords(const IntRect& Rect) const
{
float Width = static_cast<float>(myTextureWidth);
float Height = static_cast<float>(myTextureHeight);
- if (Adjust && myIsSmooth)
- {
- return FloatRect((Rect.Left + 0.5f) / Width,
- (Rect.Top + 0.5f) / Height,
- (Rect.Right - 0.5f) / Width,
- (Rect.Bottom - 0.5f) / Height);
- }
- else
- {
- return FloatRect(Rect.Left / Width,
- Rect.Top / Height,
- Rect.Right / Width,
- Rect.Bottom / Height);
- }
+ return FloatRect(Rect.Left / Width,
+ Rect.Top / Height,
+ Rect.Right / Width,
+ Rect.Bottom / Height);
}
diff --git a/src/SFML/Graphics/Makefile b/src/SFML/Graphics/Makefile
index f1cc6ff..4de224d 100755
--- a/src/SFML/Graphics/Makefile
+++ b/src/SFML/Graphics/Makefile
@@ -18,7 +18,7 @@ ifeq ($(STATIC), yes)
else
LIB = libsfml-graphics.so
LIBNAME = $(LIBPATH)/$(LIB).$(VERSION)
- INSTALL = && $(LN) $(LNFLAGS) $(DESTLIBDIR)/$(LIB).$(VERSION) $(DESTLIBDIR)/$(LIB)
+ INSTALL = && $(LN) $(LNFLAGS) $(LIB).$(VERSION) $(DESTLIBDIR)/$(LIB)
endif
all: $(LIB)
@@ -27,7 +27,7 @@ libsfml-graphics-s.a: $(OBJ) $(OBJGLEW) $(OBJJPEG) $(OBJPNG) $(OBJSOIL) $(OBJZLI
$(AR) $(ARFLAGS) $(LIBNAME) $(OBJ) $(OBJGLEW) $(OBJJPEG) $(OBJPNG) $(OBJSOIL) $(OBJZLIB)
libsfml-graphics.so: $(OBJ) $(OBJGLEW) $(OBJJPEG) $(OBJPNG) $(OBJSOIL) $(OBJZLIB)
- $(CPP) $(LDFLAGS) -Wl,-soname,$(LIB).$(VERSION) -o $(LIBNAME) $(OBJ) $(OBJGLEW) $(OBJJPEG) $(OBJPNG) $(OBJSOIL) $(OBJZLIB) -lfreetype
+ $(CPP) $(LDFLAGS) -Wl,-soname,$(LIB).$(VERSION) -o $(LIBNAME) $(OBJ) $(OBJGLEW) $(OBJJPEG) $(OBJPNG) $(OBJSOIL) $(OBJZLIB) -lfreetype -lX11 -lGL
$(OBJ): %.o: %.cpp
$(CPP) -o $@ -c $< $(CFLAGS) -I/usr/include/freetype2
diff --git a/src/SFML/Graphics/RenderTarget.cpp b/src/SFML/Graphics/RenderTarget.cpp
index f9d9096..2dca866 100755
--- a/src/SFML/Graphics/RenderTarget.cpp
+++ b/src/SFML/Graphics/RenderTarget.cpp
@@ -86,10 +86,10 @@ void RenderTarget::Draw(const Drawable& Object)
// Save the current render states and set the SFML ones
if (myPreserveStates)
{
- GLCheck(glMatrixMode(GL_MODELVIEW)); GLCheck(glPushMatrix());
- GLCheck(glMatrixMode(GL_PROJECTION)); GLCheck(glPushMatrix());
GLCheck(glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT | GL_ENABLE_BIT |
GL_TEXTURE_BIT | GL_TRANSFORM_BIT | GL_VIEWPORT_BIT));
+ GLCheck(glMatrixMode(GL_MODELVIEW)); GLCheck(glPushMatrix());
+ GLCheck(glMatrixMode(GL_PROJECTION)); GLCheck(glPushMatrix());
SetRenderStates();
}
diff --git a/src/SFML/Graphics/Sprite.cpp b/src/SFML/Graphics/Sprite.cpp
index a728d4b..375bd5e 100755
--- a/src/SFML/Graphics/Sprite.cpp
+++ b/src/SFML/Graphics/Sprite.cpp
@@ -186,6 +186,10 @@ void Sprite::Render(RenderTarget&) const
// Check if the image is valid
if (myImage && (myImage->GetWidth() > 0) && (myImage->GetHeight() > 0))
{
+ // Use the "offset trick" to get pixel-perfect rendering
+ // see http://www.opengl.org/resources/faq/technical/transformations.htm#tran0030
+ GLCheck(glTranslatef(0.375f, 0.375f, 0.f));
+
// Bind the texture
myImage->Bind();
diff --git a/src/SFML/Makefile b/src/SFML/Makefile
index dbc9d45..d8f633e 100755
--- a/src/SFML/Makefile
+++ b/src/SFML/Makefile
@@ -21,31 +21,36 @@ export CFLAGS = -W -Wall -pedantic -I../../../include -I../../ $(DEBUGFLAGS)
export CFLAGSEXT = -I../../../include -I../.. $(DEBUGFLAGS) $(BUILDFLAGS)
export LDFLAGS = $(LINKFLAGS)
export LIBPATH = ../../../lib
-export VERSION = 1.5
+export VERSION = 1.6
export CP = cp
export LN = ln
export LNFLAGS = -s -f
export AR = ar
export ARFLAGS = rcs
-export DESTDIR = /usr
+export DESTDIR = /usr/local
export DESTLIBDIR = $(DESTDIR)/lib
export DESTINCDIR = $(DESTDIR)/include
all: sfml-system sfml-window sfml-network sfml-graphics sfml-audio
sfml-system:
+ @(mkdir -p ../../lib)
@(cd ./System && $(MAKE))
sfml-window:
+ @(mkdir -p ../../lib)
@(cd ./Window && $(MAKE))
sfml-network:
+ @(mkdir -p ../../lib)
@(cd ./Network && $(MAKE))
sfml-graphics:
+ @(mkdir -p ../../lib)
@(cd ./Graphics && $(MAKE))
sfml-audio:
+ @(mkdir -p ../../lib)
@(cd ./Audio && $(MAKE))
.PHONY: clean mrproper
diff --git a/src/SFML/Network/Ftp.cpp b/src/SFML/Network/Ftp.cpp
index 85146d4..c53322c 100755
--- a/src/SFML/Network/Ftp.cpp
+++ b/src/SFML/Network/Ftp.cpp
@@ -380,7 +380,8 @@ Ftp::Response Ftp::Download(const std::string& DistantFile, const std::string& D
std::ofstream File((Path + Filename).c_str(), std::ios_base::binary);
if (!File)
return Response(Response::InvalidFile);
- File.write(&FileData[0], static_cast<std::streamsize>(FileData.size()));
+ if (!FileData.empty())
+ File.write(&FileData[0], static_cast<std::streamsize>(FileData.size()));
}
}
}
@@ -402,7 +403,8 @@ Ftp::Response Ftp::Upload(const std::string& LocalFile, const std::string& DestP
std::size_t Length = File.tellg();
File.seekg(0, std::ios::beg);
std::vector<char> FileData(Length);
- File.read(&FileData[0], static_cast<std::streamsize>(Length));
+ if (Length > 0)
+ File.read(&FileData[0], static_cast<std::streamsize>(Length));
// Extract the filename from the file path
std::string Filename = LocalFile;
@@ -700,7 +702,8 @@ void Ftp::DataChannel::Receive(std::vector<char>& Data)
void Ftp::DataChannel::Send(const std::vector<char>& Data)
{
// Send data
- myDataSocket.Send(&Data[0], Data.size());
+ if (!Data.empty())
+ myDataSocket.Send(&Data[0], Data.size());
// Close the data socket
myDataSocket.Close();
diff --git a/src/SFML/Network/Http.cpp b/src/SFML/Network/Http.cpp
index af34a34..4a9502f 100755
--- a/src/SFML/Network/Http.cpp
+++ b/src/SFML/Network/Http.cpp
@@ -27,6 +27,8 @@
////////////////////////////////////////////////////////////
#include <SFML/Network/Http.hpp>
#include <ctype.h>
+#include <algorithm>
+#include <iterator>
#include <sstream>
@@ -51,14 +53,12 @@ namespace sf
////////////////////////////////////////////////////////////
/// Default constructor
////////////////////////////////////////////////////////////
-Http::Request::Request(Method RequestMethod, const std::string& URI, const std::string& Body) :
-myMethod (RequestMethod),
-myURI (URI),
-myMajorVersion(1),
-myMinorVersion(0),
-myBody (Body)
+Http::Request::Request(Method RequestMethod, const std::string& URI, const std::string& Body)
{
-
+ SetMethod(RequestMethod);
+ SetURI(URI);
+ SetHttpVersion(1, 0);
+ SetBody(Body);
}
@@ -180,7 +180,7 @@ myMinorVersion(0)
////////////////////////////////////////////////////////////
const std::string& Http::Response::GetField(const std::string& Field) const
{
- FieldTable::const_iterator It = myFields.find(Field);
+ FieldTable::const_iterator It = myFields.find(ToLower(Field));
if (It != myFields.end())
{
return It->second;
@@ -297,8 +297,7 @@ void Http::Response::FromString(const std::string& Data)
// Finally extract the body
myBody.clear();
- while (std::getline(In, Line))
- myBody += Line + "\n";
+ std::copy(std::istreambuf_iterator<char>(In), std::istreambuf_iterator<char>(), std::back_inserter(myBody));
}
@@ -386,6 +385,14 @@ Http::Response Http::SendRequest(const Http::Request& Req, float Timeout)
Out << ToSend.myBody.size();
ToSend.SetField("Content-Length", Out.str());
}
+ if ((ToSend.myMethod == Request::Post) && !ToSend.HasField("Content-Type"))
+ {
+ ToSend.SetField("Content-Type", "application/x-www-form-urlencoded");
+ }
+ if ((ToSend.myMajorVersion * 10 + ToSend.myMinorVersion >= 11) && !ToSend.HasField("Connection"))
+ {
+ ToSend.SetField("Connection", "close");
+ }
// Prepare the response
Response Received;
diff --git a/src/SFML/Network/Makefile b/src/SFML/Network/Makefile
index d30da62..62fb673 100755
--- a/src/SFML/Network/Makefile
+++ b/src/SFML/Network/Makefile
@@ -8,7 +8,7 @@ ifeq ($(STATIC), yes)
else
LIB = libsfml-network.so
LIBNAME = $(LIBPATH)/$(LIB).$(VERSION)
- INSTALL = && $(LN) $(LNFLAGS) $(DESTLIBDIR)/$(LIB).$(VERSION) $(DESTLIBDIR)/$(LIB)
+ INSTALL = && $(LN) $(LNFLAGS) $(LIB).$(VERSION) $(DESTLIBDIR)/$(LIB)
endif
all: $(LIB)
diff --git a/src/SFML/Network/SocketTCP.cpp b/src/SFML/Network/SocketTCP.cpp
index 03d96d3..6a200fc 100755
--- a/src/SFML/Network/SocketTCP.cpp
+++ b/src/SFML/Network/SocketTCP.cpp
@@ -131,11 +131,22 @@ Socket::Status SocketTCP::Connect(unsigned short Port, const IPAddress& HostAddr
Time.tv_sec = static_cast<long>(Timeout);
Time.tv_usec = (static_cast<long>(Timeout * 1000) % 1000) * 1000;
- // Wait for something to write on our socket (would mean the connection has been accepted)
+ // Wait for something to write on our socket (which means that the connection request has returned)
if (select(static_cast<int>(mySocket + 1), NULL, &Selector, NULL, &Time) > 0)
{
- // Connection succeeded
- Status = Socket::Done;
+ // At this point the connection may have been either accepted or refused.
+ // To know whether it's a success or a failure, we try to retrieve the name of the connected peer
+ SocketHelper::LengthType Size = sizeof(SockAddr);
+ if (getpeername(mySocket, reinterpret_cast<sockaddr*>(&SockAddr), &Size) != -1)
+ {
+ // Connection accepted
+ Status = Socket::Done;
+ }
+ else
+ {
+ // Connection failed
+ Status = SocketHelper::GetErrorStatus();
+ }
}
else
{
@@ -333,11 +344,20 @@ Socket::Status SocketTCP::Receive(Packet& PacketToReceive)
std::size_t Received = 0;
if (myPendingPacketSize < 0)
{
- Socket::Status Status = Receive(reinterpret_cast<char*>(&PacketSize), sizeof(PacketSize), Received);
- if (Status != Socket::Done)
- return Status;
+ // Loop until we've received the entire size of the packet
+ // (even a 4 bytes variable may be received in more than one call)
+ while (myPendingHeaderSize < sizeof(myPendingHeader))
+ {
+ char* Data = reinterpret_cast<char*>(&myPendingHeader) + myPendingHeaderSize;
+ Socket::Status Status = Receive(Data, sizeof(myPendingHeader) - myPendingHeaderSize, Received);
+ myPendingHeaderSize += Received;
+
+ if (Status != Socket::Done)
+ return Status;
+ }
- PacketSize = ntohl(PacketSize);
+ PacketSize = ntohl(myPendingHeader);
+ myPendingHeaderSize = 0;
}
else
{
@@ -461,6 +481,7 @@ void SocketTCP::Create(SocketHelper::SocketType Descriptor)
myIsBlocking = true;
// Reset the pending packet
+ myPendingHeaderSize = 0;
myPendingPacket.clear();
myPendingPacketSize = -1;
diff --git a/src/SFML/Network/SocketUDP.cpp b/src/SFML/Network/SocketUDP.cpp
index 0c977cd..c1fbc7b 100755
--- a/src/SFML/Network/SocketUDP.cpp
+++ b/src/SFML/Network/SocketUDP.cpp
@@ -244,20 +244,25 @@ Socket::Status SocketUDP::Send(Packet& PacketToSend, const IPAddress& Address, u
////////////////////////////////////////////////////////////
Socket::Status SocketUDP::Receive(Packet& PacketToReceive, IPAddress& Address, unsigned short& Port)
{
- // This is not safe at all, as data can be lost, duplicated, or arrive in a different order.
- // So if a packet is split into more than one chunk, nobody knows what could happen...
- // Conclusion : we shouldn't use packets with UDP, unless we build a more complex protocol on top of it.
-
// We start by getting the size of the incoming packet
Uint32 PacketSize = 0;
std::size_t Received = 0;
if (myPendingPacketSize < 0)
{
- Socket::Status Status = Receive(reinterpret_cast<char*>(&PacketSize), sizeof(PacketSize), Received, Address, Port);
- if (Status != Socket::Done)
- return Status;
+ // Loop until we've received the entire size of the packet
+ // (even a 4 bytes variable may be received in more than one call)
+ while (myPendingHeaderSize < sizeof(myPendingHeader))
+ {
+ char* Data = reinterpret_cast<char*>(&myPendingHeader) + myPendingHeaderSize;
+ Socket::Status Status = Receive(Data, sizeof(myPendingHeader) - myPendingHeaderSize, Received, Address, Port);
+ myPendingHeaderSize += Received;
- PacketSize = ntohl(PacketSize);
+ if (Status != Socket::Done)
+ return Status;
+ }
+
+ PacketSize = ntohl(myPendingHeader);
+ myPendingHeaderSize = 0;
}
else
{
@@ -265,9 +270,6 @@ Socket::Status SocketUDP::Receive(Packet& PacketToReceive, IPAddress& Address, u
PacketSize = myPendingPacketSize;
}
- // Clear the user packet
- PacketToReceive.Clear();
-
// Use another address instance for receiving the packet data ;
// chunks of data coming from a different sender will be discarded (and lost...)
IPAddress Sender;
@@ -402,6 +404,7 @@ void SocketUDP::Create(SocketHelper::SocketType Descriptor)
myPort = 0;
// Reset the pending packet
+ myPendingHeaderSize = 0;
myPendingPacket.clear();
myPendingPacketSize = -1;
diff --git a/src/SFML/System/Makefile b/src/SFML/System/Makefile
index 04514f5..1cf6211 100755
--- a/src/SFML/System/Makefile
+++ b/src/SFML/System/Makefile
@@ -8,7 +8,7 @@ ifeq ($(STATIC), yes)
else
LIB = libsfml-system.so
LIBNAME = $(LIBPATH)/$(LIB).$(VERSION)
- INSTALL = && $(LN) $(LNFLAGS) $(DESTLIBDIR)/$(LIB).$(VERSION) $(DESTLIBDIR)/$(LIB)
+ INSTALL = && $(LN) $(LNFLAGS) $(LIB).$(VERSION) $(DESTLIBDIR)/$(LIB)
endif
all: $(LIB)
diff --git a/src/SFML/System/Randomizer.cpp b/src/SFML/System/Randomizer.cpp
index 93500bc..18bbe9c 100755
--- a/src/SFML/System/Randomizer.cpp
+++ b/src/SFML/System/Randomizer.cpp
@@ -26,8 +26,8 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/Randomizer.hpp>
-#include <SFML/System/Platform.hpp>
-#include <cstdlib>
+#include <stdlib.h>
+#include <time.h>
namespace
@@ -35,7 +35,7 @@ namespace
// Set the random numbers sequence seed with the current system time, so that it is always different
unsigned int SetRandomSeed()
{
- unsigned int Seed = static_cast<unsigned int>(sf::priv::Platform::GetSystemTime() * 1000);
+ unsigned int Seed = static_cast<unsigned int>(time(NULL));
srand(Seed);
return Seed;
}
diff --git a/src/SFML/System/Initializer.cpp b/src/SFML/System/Unix/Initializer.cpp
index 0d3dbee..0d3dbee 100755
--- a/src/SFML/System/Initializer.cpp
+++ b/src/SFML/System/Unix/Initializer.cpp
diff --git a/src/SFML/System/Unix/Thread.cpp b/src/SFML/System/Unix/Thread.cpp
index 3268f61..da02399 100755
--- a/src/SFML/System/Unix/Thread.cpp
+++ b/src/SFML/System/Unix/Thread.cpp
@@ -61,8 +61,7 @@ myUserData(UserData)
Thread::~Thread()
{
// Wait for the thread to finish before destroying the instance
- if (myIsActive)
- Wait();
+ Wait();
}
@@ -71,6 +70,9 @@ Thread::~Thread()
////////////////////////////////////////////////////////////
void Thread::Launch()
{
+ // Wait for the thread to finish, in case it was already running
+ Wait();
+
// Create the thread
myIsActive = true;
int Error = pthread_create(&myThread, NULL, &Thread::ThreadFunc, this);
diff --git a/src/SFML/System/Win32/Thread.cpp b/src/SFML/System/Win32/Thread.cpp
index d2d1d82..3252100 100755
--- a/src/SFML/System/Win32/Thread.cpp
+++ b/src/SFML/System/Win32/Thread.cpp
@@ -62,8 +62,7 @@ myUserData(UserData)
Thread::~Thread()
{
// Wait for the thread to finish before destroying the instance
- if (myHandle)
- Wait();
+ Wait();
}
@@ -72,6 +71,9 @@ Thread::~Thread()
////////////////////////////////////////////////////////////
void Thread::Launch()
{
+ // Wait for the thread to finish, in case it was already running
+ Wait();
+
// Create the thread
myHandle = reinterpret_cast<HANDLE>(_beginthreadex(NULL, 0, &Thread::ThreadFunc, this, 0, NULL));
diff --git a/src/SFML/Window/Cocoa/AppController.h b/src/SFML/Window/Cocoa/AppController.h
index f56db4e..de2d055 100755
--- a/src/SFML/Window/Cocoa/AppController.h
+++ b/src/SFML/Window/Cocoa/AppController.h
@@ -30,18 +30,16 @@
#import <Cocoa/Cocoa.h>
-#define SharedAppController [AppController sharedController]
-
// Fade operations
enum {
FillScreen,
CleanScreen
};
-@class WindowWrapper;
-@interface AppController : NSObject {
+@class sfPrivWindow;
+@interface sfPrivAppController : NSObject {
BOOL myOwningEventLoop;
- WindowWrapper *myFullscreenWrapper;
+ sfPrivWindow *myFullscreenWrapper;
NSAutoreleasePool *myMainPool;
sf::VideoMode myDesktopMode;
sf::VideoMode myPrevMode;
@@ -50,7 +48,17 @@ enum {
////////////////////////////////////////////////////////////
/// Return the shared AppController instance. Make one if needed.
////////////////////////////////////////////////////////////
-+ (AppController *)sharedController;
++ (sfPrivAppController *)sharedController;
+
+////////////////////////////////////////////////////////////
+/// Returns the primay computer's screen
+////////////////////////////////////////////////////////////
++ (CGDirectDisplayID)primaryScreen;
+
+////////////////////////////////////////////////////////////
+/// Reset notifictions about application focus
+////////////////////////////////////////////////////////////
+- (void)setNotifications;
////////////////////////////////////////////////////////////
/// Make the menu bar
@@ -68,7 +76,7 @@ enum {
/// Set @window as the current fullscreen window
/// Change the screen resolution if needed according to @window and @fullscreenMode
////////////////////////////////////////////////////////////
-- (void)setFullscreenWindow:(WindowWrapper *)window mode:(sf::VideoMode *)fullscreenMode;
+- (void)setFullscreenWindow:(sfPrivWindow *)window mode:(sf::VideoMode *)fullscreenMode;
////////////////////////////////////////////////////////////
/// Perform fade operation where 'operation' is one of { FillScreen, CleanScreen}
diff --git a/src/SFML/Window/Cocoa/AppController.mm b/src/SFML/Window/Cocoa/AppController.mm
index 8d07eaf..9356090 100755
--- a/src/SFML/Window/Cocoa/AppController.mm
+++ b/src/SFML/Window/Cocoa/AppController.mm
@@ -33,9 +33,6 @@
#import <iostream>
-// AppController singleton object
-static AppController *shared = nil;
-
/* setAppleMenu disappeared from the headers in 10.4 */
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
@@ -60,7 +57,7 @@ static AppController *shared = nil;
@end
-@implementation AppController
+@implementation sfPrivAppController
////////////////////////////////////////////////////////////
@@ -83,6 +80,11 @@ static AppController *shared = nil;
// Make the app autorelease pool
myMainPool = [[NSAutoreleasePool alloc] init];
+ if (!myMainPool) {
+ [self release];
+ throw std::bad_alloc();
+ }
+
// Don't go on if the user handles the app
if (![NSApp isRunning])
{
@@ -96,27 +98,12 @@ static AppController *shared = nil;
}
// Make the app
- [NSApplication sharedApplication];
-
- NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
- // I want to go back to the desktop mode
- // if we've a fullscreen window when hiding
- [nc addObserver:self
- selector:@selector(applicationWillHide:)
- name:NSApplicationWillHideNotification
- object:NSApp];
-
- // And restore de fullscreen mode when unhiding
- [nc addObserver:self
- selector:@selector(applicationWillUnhide:)
- name:NSApplicationWillUnhideNotification
- object:NSApp];
+ if (![NSApplication sharedApplication]) {
+ [self release];
+ throw std::bad_alloc();
+ }
- // Go back to desktop mode before exit
- [nc addObserver:self
- selector:@selector(applicationWillTerminate:)
- name:NSApplicationWillTerminateNotification
- object:NSApp];
+ [self setNotifications];
if ([NSApp mainMenu] == nil) {
[self makeMenuBar];
@@ -134,7 +121,7 @@ static AppController *shared = nil;
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
- [myFullscreenWrapper release];
+ [myMainPool release];
[super dealloc];
}
@@ -142,40 +129,80 @@ static AppController *shared = nil;
////////////////////////////////////////////////////////////
/// Return the shared AppController instance. Make one if needed.
////////////////////////////////////////////////////////////
-+ (AppController *)sharedController
++ (sfPrivAppController *)sharedController
{
- if (nil == shared)
- shared = [[AppController alloc] init];
-
+ // AppController singleton object
+ static sfPrivAppController *shared = [[sfPrivAppController alloc] init];
return shared;
}
////////////////////////////////////////////////////////////
+/// Returns the primay computer's screen
+////////////////////////////////////////////////////////////
++ (CGDirectDisplayID)primaryScreen
+{
+ static BOOL firstTime = YES;
+ static CGDirectDisplayID screen = kCGNullDirectDisplay;
+
+ if (firstTime) {
+ CGDisplayCount numScr;
+ CGDisplayErr err = CGGetDisplaysWithPoint(CGPointMake(0, 0), 1, &screen, &numScr);
+
+ if (err != kCGErrorSuccess || numScr < 1) {
+ std::cerr << "Unable to get primary screen (error code " << err
+ << " ). Using the main screen.";
+ screen = CGMainDisplayID();
+ }
+ firstTime = NO;
+ }
+
+ return screen;
+}
+
+
+////////////////////////////////////////////////////////////
+/// Reset notifictions about application focus
+////////////////////////////////////////////////////////////
+- (void)setNotifications
+{
+ NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
+ [nc addObserver:self
+ selector:@selector(applicationWillDeactivate:)
+ name:NSApplicationWillResignActiveNotification
+ object:NSApp];
+ [nc addObserver:self
+ selector:@selector(applicationWillActivate:)
+ name:NSApplicationWillBecomeActiveNotification
+ object:NSApp];
+
+ [nc addObserver:self
+ selector:@selector(applicationWillTerminate:)
+ name:NSApplicationWillTerminateNotification
+ object:NSApp];
+}
+
+
+////////////////////////////////////////////////////////////
/// Hide all the fullscreen windows and switch back to the desktop display mode
////////////////////////////////////////////////////////////
-- (void)applicationWillHide:(NSNotification *)aNotification
+- (void)applicationWillDeactivate:(NSNotification *)aNotification
{
+ // Note: not using fading because it produces reactivation issues
if (myFullscreenWrapper) {
myPrevMode = sf::VideoMode::GetDesktopMode();
- CFDictionaryRef displayMode = CGDisplayBestModeForParameters (kCGDirectMainDisplay,
+ CFDictionaryRef displayMode = CGDisplayBestModeForParameters ([sfPrivAppController primaryScreen],
myDesktopMode.BitsPerPixel,
myDesktopMode.Width,
myDesktopMode.Height,
NULL);
- // Fade to black screen
- [self doFadeOperation:FillScreen time:0.2f sync:true];
-
// Make the full screen window unvisible
[[myFullscreenWrapper window] setAlphaValue:0.0f];
// Switch to the wished display mode
- CGDisplaySwitchToMode(kCGDirectMainDisplay, displayMode);
-
- // Fade to normal screen
- [self doFadeOperation:CleanScreen time:0.5f sync:false];
+ CGDisplaySwitchToMode([sfPrivAppController primaryScreen], displayMode);
}
}
@@ -183,31 +210,26 @@ static AppController *shared = nil;
////////////////////////////////////////////////////////////
/// Unhide all the fullscreen windows and switch to full screen display mode
////////////////////////////////////////////////////////////
-- (void)applicationWillUnhide:(NSNotification *)aNotification
+- (void)applicationWillActivate:(NSNotification *)aNotification
{
if (myFullscreenWrapper) {
- CFDictionaryRef displayMode = CGDisplayBestModeForParameters (kCGDirectMainDisplay,
+ CFDictionaryRef displayMode = CGDisplayBestModeForParameters ([sfPrivAppController primaryScreen],
myPrevMode.BitsPerPixel,
myPrevMode.Width,
myPrevMode.Height,
NULL);
-
- // Fade to a black screen
- [self doFadeOperation:FillScreen time:0.5f sync:true];
[NSMenu setMenuBarVisible:NO];
// Switch to the wished display mode
- CGDisplaySwitchToMode(kCGDirectMainDisplay, displayMode);
+ CGDisplaySwitchToMode([sfPrivAppController primaryScreen], displayMode);
// Show the fullscreen window if existing
if (myFullscreenWrapper)
{
- [[myFullscreenWrapper window] setAlphaValue:1.0f];
- [[myFullscreenWrapper window] center];
+ [[myFullscreenWrapper window] setAlphaValue:1.0f];
+ [[myFullscreenWrapper window] center];
+ [[myFullscreenWrapper window] makeKeyAndOrderFront:self];
}
-
- // Fade to normal screen
- [self doFadeOperation:CleanScreen time:0.5f sync:false];
}
}
@@ -216,10 +238,6 @@ static AppController *shared = nil;
{
if (myFullscreenWrapper)
[self setFullscreenWindow:nil mode:NULL];
-
- // FIXME: should I really do this ? what about the user owned windows ?
- // And is this really useful as the application is about to exit ?
- [NSApp makeWindowsPerform:@selector(close) inOrder:NO];
}
////////////////////////////////////////////////////////////
@@ -228,7 +246,6 @@ static AppController *shared = nil;
- (void)makeMenuBar
{
// Source taken from SDL 1.3
-
NSString *appName = nil;
NSString *title = nil;
NSMenu *appleMenu = nil;
@@ -266,10 +283,10 @@ static AppController *shared = nil;
keyEquivalent:@"h"];
// + 'Hide other' menu item
- menuItem = reinterpret_cast <NSMenuItem *> ([appleMenu addItemWithTitle:@"Hide Others"
- action:@selector(hideOtherApplications:)
- keyEquivalent:@"h"]);
- [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
+ menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others"
+ action:@selector(hideOtherApplications:)
+ keyEquivalent:@"h"];
+ [menuItem setKeyEquivalentModifierMask:NSAlternateKeyMask | NSCommandKeyMask];
// + 'Show all' menu item
[appleMenu addItemWithTitle:@"Show All"
@@ -363,14 +380,14 @@ static AppController *shared = nil;
myOwningEventLoop = YES;
}
- // Clean the autorelease pool
- [myMainPool release];
- myMainPool = [[NSAutoreleasePool alloc] init];
-
NSEvent *event = nil;
if (myOwningEventLoop)
{
+ // Clean the autorelease pool
+ [myMainPool release];
+ myMainPool = [[NSAutoreleasePool alloc] init];
+
// Minimal event loop
while (nil != (event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:nil
@@ -387,13 +404,13 @@ static AppController *shared = nil;
/// Set @window as the current fullscreen window
/// Change the screen resolution if needed according to @window and @fullscreenMode
////////////////////////////////////////////////////////////
-- (void)setFullscreenWindow:(WindowWrapper *)aWrapper mode:(sf::VideoMode *)fullscreenMode
+- (void)setFullscreenWindow:(sfPrivWindow *)aWindow mode:(sf::VideoMode *)fullscreenMode
{
// If we have a fullscreen window and want to remove it
- if (aWrapper == nil && myFullscreenWrapper)
+ if (aWindow == nil && myFullscreenWrapper)
{
// Get the CoreGraphics display mode according to the desktop mode
- CFDictionaryRef displayMode = CGDisplayBestModeForParameters (kCGDirectMainDisplay,
+ CFDictionaryRef displayMode = CGDisplayBestModeForParameters ([sfPrivAppController primaryScreen],
myDesktopMode.BitsPerPixel,
myDesktopMode.Width,
myDesktopMode.Height,
@@ -405,7 +422,7 @@ static AppController *shared = nil;
#endif
// Switch to the desktop display mode
- CGDisplaySwitchToMode(kCGDirectMainDisplay, displayMode);
+ CGDisplaySwitchToMode([sfPrivAppController primaryScreen], displayMode);
// Close the window
[[myFullscreenWrapper window] close];
@@ -421,12 +438,13 @@ static AppController *shared = nil;
// Release the saved window wrapper
myFullscreenWrapper = nil;
}
- else if (aWrapper)
+ else if (aWindow)
{
+ // else if we want to SET fullscreen
assert(fullscreenMode != NULL);
// Get the CoreGraphics display mode according to the given sf mode
- CFDictionaryRef displayMode = CGDisplayBestModeForParameters (kCGDirectMainDisplay,
+ CFDictionaryRef displayMode = CGDisplayBestModeForParameters ([sfPrivAppController primaryScreen],
fullscreenMode->BitsPerPixel,
fullscreenMode->Width,
fullscreenMode->Height,
@@ -446,7 +464,8 @@ static AppController *shared = nil;
if (myPrevMode != *fullscreenMode)
{
// Switch to the wished display mode
- CGDisplaySwitchToMode(kCGDirectMainDisplay, displayMode);
+ myPrevMode = *fullscreenMode;
+ CGDisplaySwitchToMode([sfPrivAppController primaryScreen], displayMode);
}
if (myFullscreenWrapper)
@@ -455,8 +474,8 @@ static AppController *shared = nil;
}
// Open and center the window
- [[aWrapper window] makeKeyAndOrderFront:nil];
- [[aWrapper window] center];
+ [[aWindow window] makeKeyAndOrderFront:nil];
+ [[aWindow window] center];
#if ENABLE_FADE_OPERATIONS
// Fade to normal screen
@@ -464,11 +483,11 @@ static AppController *shared = nil;
#endif
// Save the fullscreen wrapper
- myFullscreenWrapper = aWrapper;
+ myFullscreenWrapper = aWindow;
}
else
{
- std::cerr << "Inconcistency error for arguments given to -[AppController setFullscreenWindow:mode:]" << std::endl;
+ std::cerr << "Inconcistency error for arguments given to -[sfPrivAppController setFullscreenWindow:mode:]" << std::endl;
}
}
@@ -495,7 +514,7 @@ static AppController *shared = nil;
if (!result) {
// Capture display but do not fill the screen with black
// so that we can see the fade operation
- capture = CGDisplayCaptureWithOptions(kCGDirectMainDisplay, kCGCaptureNoFill);
+ capture = CGDisplayCaptureWithOptions([sfPrivAppController primaryScreen], kCGCaptureNoFill);
if (!capture) {
// Do the increasing fade operation
@@ -505,11 +524,11 @@ static AppController *shared = nil;
0.0f, 0.0f, 0.0f, sync);
// Now, release the non black-filling capture
- CGDisplayRelease(kCGDirectMainDisplay);
+ CGDisplayRelease([sfPrivAppController primaryScreen]);
// And capture with filling
// so that we don't see the switching in the meantime
- CGDisplayCaptureWithOptions(kCGDirectMainDisplay, kCGCaptureNoOptions);
+ CGDisplayCaptureWithOptions([sfPrivAppController primaryScreen], kCGCaptureNoOptions);
}
prevToken = token;
@@ -522,10 +541,10 @@ static AppController *shared = nil;
if (!result) {
if (!capture) {
// Release the black-filling capture
- CGDisplayRelease(kCGDirectMainDisplay);
+ CGDisplayRelease([sfPrivAppController primaryScreen]);
// Capture the display but do not fill with black (still for the fade operation)
- CGDisplayCaptureWithOptions(kCGDirectMainDisplay, kCGCaptureNoFill);
+ CGDisplayCaptureWithOptions([sfPrivAppController primaryScreen], kCGCaptureNoFill);
// Do the decreasing fading
CGDisplayFade(token, time,
@@ -541,7 +560,7 @@ static AppController *shared = nil;
}
// Release the captured display
- CGDisplayRelease(kCGDirectMainDisplay);
+ CGDisplayRelease([sfPrivAppController primaryScreen]);
}
}
}
diff --git a/src/SFML/Window/Cocoa/GLKit.h b/src/SFML/Window/Cocoa/GLKit.h
index ac12252..f4cff4a 100755
--- a/src/SFML/Window/Cocoa/GLKit.h
+++ b/src/SFML/Window/Cocoa/GLKit.h
@@ -32,10 +32,7 @@
////////////////////////////////////////////////////////////
/// Window independant OpenGL context class
////////////////////////////////////////////////////////////
-@interface GLContext : NSOpenGLContext
-{
- GLContext *mySharedContext;
-}
+@interface sfPrivGLContext : NSOpenGLContext
////////////////////////////////////////////////////////////
/// Return the shared OpenGL context instance (making one if needed)
@@ -47,7 +44,8 @@
/// and the shared context @context
////////////////////////////////////////////////////////////
- (id)initWithAttributes:(sf::WindowSettings&)attribs
- sharedContext:(GLContext *)context;
+ mode:(const sf::VideoMode&)mode
+ sharedContext:(sfPrivGLContext *)context;
@end
@@ -55,22 +53,31 @@
////////////////////////////////////////////////////////////
/// Customized Cocoa OpenGL view
////////////////////////////////////////////////////////////
-@interface GLView : NSOpenGLView
+@interface sfPrivGLView : NSOpenGLView
{
sf::priv::WindowImplCocoa *myDelegate;
- GLContext *myGLContext;
+ sfPrivGLContext *myGLContext;
}
////////////////////////////////////////////////////////////
/// Make a new view according the the rect @frame,
/// the video mode @mode, the window settings @settings
-/// and the sf window delegate @delegate
/// @delegate must not be null
////////////////////////////////////////////////////////////
- (id)initWithFrame:(NSRect)frame
mode:(const sf::VideoMode&)mode
- settings:(sf::WindowSettings&)settings
- delegate:(sf::priv::WindowImplCocoa *)delegate;
+ settings:(sf::WindowSettings&)settings;
+
+
+////////////////////////////////////////////////////////////
+/// Sets @aDelegate as the view delegate
+////////////////////////////////////////////////////////////
+- (void)setDelegate:(sf::priv::WindowImplCocoa *)aDelegate;
+
+////////////////////////////////////////////////////////////
+/// Returns the view delegate
+////////////////////////////////////////////////////////////
+- (sf::priv::WindowImplCocoa *)delegate;
////////////////////////////////////////////////////////////
/// Finish view setting (after having added it to the window)
@@ -95,75 +102,34 @@
@end
////////////////////////////////////////////////////////////
-/// Cocoa window implementation to let fullscreen windows
-/// catch key events
-////////////////////////////////////////////////////////////
-@interface GLWindow : NSWindow
-
-////////////////////////////////////////////////////////////
-/// Technical note: this class must neither contain new members
-/// nor methods. It is used transparently as a NSWindow object
-/// by WindowWrapper. Not following this rule could result
-/// in a segmentation fault or data corruption.
-////////////////////////////////////////////////////////////
-
-@end
-
+/// Parent class for handling general SFML window stuff
////////////////////////////////////////////////////////////
-/// WindowWrapper class : handles both imported and self-built windows
-////////////////////////////////////////////////////////////
-@interface WindowWrapper : NSObject
+@interface sfPrivWindow : NSObject
{
- GLWindow *myWindow;
- GLView *myView;
- sf::VideoMode myFullscreenMode;
- bool myIsFullscreen;
+@private
+ NSWindow *myWindow;
+ sfPrivGLView *myView;
}
////////////////////////////////////////////////////////////
-/// Make a new window wrapper according to the window settings @attribs,
-/// the video mode @mode, the window style @style, the window title @title
-/// and the sf window implementation delegate @delegate
-////////////////////////////////////////////////////////////
-- (id)initWithSettings:(sf::WindowSettings&)attribs
- videoMode:(sf::VideoMode&)mode
- style:(unsigned long)style
- title:(NSString *)title
- delegate:(sf::priv::WindowImplCocoa *)delegate;
-
-////////////////////////////////////////////////////////////
-/// Make a new window wrapper by importing @window and according to
-/// the window settings @params and the sf window implementation delegate
-/// @delegate
-/// @window and @delegate must not be null
+/// Return a reference to the internal Cocoa window
////////////////////////////////////////////////////////////
-- (id)initWithWindow:(NSWindow *)window
- settings:(sf::WindowSettings&)params
- delegate:(sf::priv::WindowImplCocoa *)delegate;
+- (NSWindow *)window;
////////////////////////////////////////////////////////////
-/// Make a new window wrapper by importing @window if it's not null and according to
-/// the window settings @params and the sf window implementation delegate
-/// @delegate; or by creating a new window if @window is null. In this case @title
-/// must therefore not be null and @params must be valid.
-/// @delegate must never be null
+/// Return a reference to the internal Cocoa OpenGL view
////////////////////////////////////////////////////////////
-- (id)initWithWindow:(NSWindow *)window
- settings:(sf::WindowSettings&)params
- videoMode:(sf::VideoMode&)mode
- style:(unsigned long)style
- title:(NSString *)title
- delegate:(sf::priv::WindowImplCocoa *)delegate;
+- (sfPrivGLView *)view;
////////////////////////////////////////////////////////////
-/// Return a reference to the internal Cocoa window
+/// Sets @aDelegate as the window delegate
////////////////////////////////////////////////////////////
-- (NSWindow *)window;
+- (void)setDelegate:(sf::priv::WindowImplCocoa *)aDelegate;
////////////////////////////////////////////////////////////
-/// Return a reference to the internal Cocoa OpenGL view
+/// Returns the window delegate
////////////////////////////////////////////////////////////
-- (GLView *)glView;
+- (sf::priv::WindowImplCocoa *)delegate;
////////////////////////////////////////////////////////////
/// Forward call to set the window position on screen
@@ -207,3 +173,61 @@
@end
+////////////////////////////////////////////////////////////
+/// Class for creating new SFML windows from informations
+////////////////////////////////////////////////////////////
+@interface sfPrivOwnedWindow : sfPrivWindow
+{
+@private
+ sf::VideoMode myFullscreenMode;
+ bool myIsFullscreen;
+}
+
+////////////////////////////////////////////////////////////
+/// Creates and returns a new SFML window handler with
+/// the given parameters
+////////////////////////////////////////////////////////////
+- (id)initWithVideoMode:(sf::VideoMode&)aMode
+ settings:(sf::WindowSettings&)someSettings
+ style:(unsigned long)aStyle
+ title:(NSString *)aTitle;
+
+////////////////////////////////////////////////////////////
+/// Returns the window's fullscreen state
+////////////////////////////////////////////////////////////
+- (BOOL)isFullscreen;
+
+@end
+
+
+////////////////////////////////////////////////////////////
+/// Class for creating SFML windows from Cocoa windows
+////////////////////////////////////////////////////////////
+@interface sfPrivImportedWindow : sfPrivWindow
+
+////////////////////////////////////////////////////////////
+/// Returns a new SFML window handler with the given window
+/// and parameters
+////////////////////////////////////////////////////////////
+- (id)initWithWindow:(NSWindow *)aWindow
+ settings:(sf::WindowSettings&)someSettings;
+
+@end
+
+
+////////////////////////////////////////////////////////////
+/// Class for creating SFML windows from Cocoa views
+////////////////////////////////////////////////////////////
+@interface sfPrivImportedView : sfPrivWindow
+{
+ NSView *parentView;
+}
+
+////////////////////////////////////////////////////////////
+/// Returns a new SFML window handler with the given view
+/// and parameters
+////////////////////////////////////////////////////////////
+- (id)initWithView:(NSView *)aView
+ settings:(sf::WindowSettings&)someSettings;
+
+@end
diff --git a/src/SFML/Window/Cocoa/GLKit.mm b/src/SFML/Window/Cocoa/GLKit.mm
index a570f44..f19fa77 100755
--- a/src/SFML/Window/Cocoa/GLKit.mm
+++ b/src/SFML/Window/Cocoa/GLKit.mm
@@ -29,36 +29,62 @@
#import <SFML/Window/Cocoa/AppController.h>
#import <SFML/Window/VideoMode.hpp>
#import <SFML/Window/WindowStyle.hpp>
-#import <SFML/System/Sleep.hpp>
#import <OpenGL/gl.h>
#import <iostream>
+@interface sfPrivWindow (Protected)
+
+////////////////////////////////////////////////////////////
+/// Registers a reference to the internal Cocoa window
+////////////////////////////////////////////////////////////
+- (void)setWindow:(NSWindow *)aWindow;
+
+////////////////////////////////////////////////////////////
+/// Registers the the OpenGL view and adds it to its parent container
+////////////////////////////////////////////////////////////
+- (void)putOpenGLView:(sfPrivGLView *)aView;
+
+////////////////////////////////////////////////////////////
+/// Registers a reference to the internal Cocoa OpenGL view
+////////////////////////////////////////////////////////////
+- (void)setView:(sfPrivGLView *)aView;
+
+@end
+
+@interface sfPrivOwnedWindow (Private)
+
+////////////////////////////////////////////////////////////
+/// Sets the window's fullscreen state
+////////////////////////////////////////////////////////////
+- (void)setFullscreen:(BOOL)aFlag;
+
+@end
+
+
////////////////////////////////////////////////////////////
/// Window independant OpenGL context class
////////////////////////////////////////////////////////////
-@implementation GLContext
+@implementation sfPrivGLContext
-static GLContext *sharedCtx = nil;
////////////////////////////////////////////////////////////
/// Return the shared OpenGL context instance (making one if needed)
////////////////////////////////////////////////////////////
+ (id)sharedContext
{
- if (sharedCtx == nil)
- {
- // Make a new context with the default parameters
- sf::WindowSettings params(0, 0, 0);
- sharedCtx = [[GLContext alloc] initWithAttributes:params sharedContext:nil];
- }
-
+ // Make a new context with the default parameters
+ sf::WindowSettings params;
+ sf::VideoMode m;
+ static sfPrivGLContext *sharedCtx = [[sfPrivGLContext alloc] initWithAttributes:params
+ mode:m
+ sharedContext:nil];
return sharedCtx;
}
- (void)dealloc
{
- [mySharedContext release];
+ [[sfPrivGLContext sharedContext] release];
[super dealloc];
}
@@ -66,7 +92,7 @@ static GLContext *sharedCtx = nil;
/// Make a new OpenGL context according to the @attribs settings
/// and the shared context @context
////////////////////////////////////////////////////////////
-- (id)initWithAttributes:(sf::WindowSettings&)attribs sharedContext:(GLContext *)context
+- (id)initWithAttributes:(sf::WindowSettings&)attribs mode:(const sf::VideoMode&)mode sharedContext:(sfPrivGLContext *)sharedContext
{
// Note about antialiasing and other context attributes :
// OpenGL context sharing does not allow the shared contexts to use different attributes.
@@ -77,8 +103,13 @@ static GLContext *sharedCtx = nil;
//
// I've no way to fix this for now.
- if (attribs.AntialiasingLevel)
- std::cerr << "Warning: antialiasing settings are inhibited under Mac OS X for technical reasons" << std::endl;
+ if (attribs.AntialiasingLevel) {
+ NSLog(@"Warning: antialiasing settings are inhibited on Mac OS X for technical reasons");
+ }
+
+ if (attribs.StencilBits != 8) {
+ NSLog(@"Warning: stencil bits settings are inhibited on Mac OS X for technical reasons");
+ }
NSOpenGLPixelFormat *myPixelFormat = nil;
unsigned idx = 0;
@@ -91,28 +122,44 @@ static GLContext *sharedCtx = nil;
ctxtAttribs[idx++] = NSOpenGLPFADoubleBuffer;
ctxtAttribs[idx++] = NSOpenGLPFAAccelerated;
+ // Force use of first screen
+ //ctxtAttribs[idx++] = NSOpenGLPFAScreenMask;
+ //ctxtAttribs[idx++] = CGDisplayIDToOpenGLDisplayMask([sfPrivAppController primaryScreen]);
+
// windowed context (even fullscreen mode uses a window)
ctxtAttribs[idx++] = NSOpenGLPFAWindow;
- // Color size ; usually 32 bits per pixel
+ // Color buffer bits ; usually 32 bits per pixel
ctxtAttribs[idx++] = NSOpenGLPFAColorSize;
- ctxtAttribs[idx++] = (NSOpenGLPixelFormatAttribute) sf::VideoMode::GetDesktopMode().BitsPerPixel;
- // Z-buffer size
+ // well.. whatever I put here, the color size is always 24 bits on my computer..
+ ctxtAttribs[idx++] = (NSOpenGLPixelFormatAttribute) mode.BitsPerPixel;
+
+ // Alpha buffer size
+ // FIXME: I don't really remember whether I can safely use the user video mode settings
+ // to set this attribute (because of the shared context attributes conflicts)
+ if (mode.BitsPerPixel > 24) {
+ ctxtAttribs[idx++] = NSOpenGLPFAAlphaSize;
+ ctxtAttribs[idx++] = (NSOpenGLPixelFormatAttribute) 8;
+ }
+
+ // Depth buffer size
ctxtAttribs[idx++] = NSOpenGLPFADepthSize;
ctxtAttribs[idx++] = (NSOpenGLPixelFormatAttribute) attribs.DepthBits;
- // Stencil bits (I don't really know what's that...)
+ // Stencil buffer bits
+ // Note: even with "NSOpenGLPFAClosestPolicy" set, the NSOpenGLPFAStencilSize value must be an valid and exact one
+ // I've been looking for possible diffferent default stencil bits values among different Apple computers
+ // but I can't again get the website I found oneaday
+ // (describing all the graphic cards used by Apple, the supported extensions, etc).
ctxtAttribs[idx++] = NSOpenGLPFAStencilSize;
- ctxtAttribs[idx++] = (NSOpenGLPixelFormatAttribute) attribs.StencilBits;
+ ctxtAttribs[idx++] = (NSOpenGLPixelFormatAttribute) /* attribs.StencilBits */ 8;
myPixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:ctxtAttribs];
if (myPixelFormat) {
self = [super initWithFormat:myPixelFormat
- shareContext:context];
-
- mySharedContext = [context retain];
+ shareContext:[sharedContext retain]];
// Get the effective properties from our OpenGL context
GLint tmpDepthSize = 0, tmpStencilBits = 0, tmpAntialiasingLevel = 0;
@@ -137,6 +184,8 @@ static GLContext *sharedCtx = nil;
attribs.AntialiasingLevel = (unsigned) tmpAntialiasingLevel;
[myPixelFormat release];
+ } else {
+ NSLog(@"NSOpenGLPixelFormat creation failed! (invalid video settings ?)");
}
return self;
@@ -148,7 +197,7 @@ static GLContext *sharedCtx = nil;
////////////////////////////////////////////////////////////
/// Customized Cocoa OpenGL view
////////////////////////////////////////////////////////////
-@implementation GLView
+@implementation sfPrivGLView
////////////////////////////////////////////////////////////
/// Make a new view according the the rect @frame,
@@ -159,10 +208,7 @@ static GLContext *sharedCtx = nil;
- (id)initWithFrame:(NSRect)frame
mode:(const sf::VideoMode&)mode
settings:(sf::WindowSettings&)settings
- delegate:(sf::priv::WindowImplCocoa *)delegate
{
- assert(delegate != NULL);
-
// make the view
self = [super initWithFrame:frame pixelFormat:nil];
@@ -172,7 +218,14 @@ static GLContext *sharedCtx = nil;
[self setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
// make the OpenGL context
- myGLContext = [[GLContext alloc] initWithAttributes:settings sharedContext:sharedCtx];
+ myGLContext = [[sfPrivGLContext alloc] initWithAttributes:settings
+ mode:mode
+ sharedContext:[sfPrivGLContext sharedContext]];
+
+ if (!myGLContext) {
+ [self release];
+ return nil;
+ }
// We need to update the OpenGL view when it's resized
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
@@ -180,9 +233,6 @@ static GLContext *sharedCtx = nil;
selector:@selector(viewFrameDidChange:)
name:NSViewFrameDidChangeNotification
object:self];
-
- // Save the delegate
- myDelegate = delegate;
}
return self;
@@ -201,6 +251,22 @@ static GLContext *sharedCtx = nil;
[super dealloc];
}
+////////////////////////////////////////////////////////////
+/// Sets @aDelegate as the view delegate
+////////////////////////////////////////////////////////////
+- (void)setDelegate:(sf::priv::WindowImplCocoa *)aDelegate
+{
+ myDelegate = aDelegate;
+}
+
+////////////////////////////////////////////////////////////
+/// Returns the view delegate
+////////////////////////////////////////////////////////////
+- (sf::priv::WindowImplCocoa *)delegate
+{
+ return myDelegate;
+}
+
////////////////////////////////////////////////////////////
/// Finish view setting (after having added it to the window)
@@ -287,9 +353,12 @@ static GLContext *sharedCtx = nil;
{
assert(myDelegate != NULL);
- NSText *field = [[self window] fieldEditor:YES forObject:nil];
- [field interpretKeyEvents:[NSArray arrayWithObject:theEvent]];
- [field setString:@""];
+ if (sf::priv::WindowImplCocoa::IsTextEvent((void *)theEvent))
+ {
+ NSText *field = [[self window] fieldEditor:YES forObject:nil];
+ [field interpretKeyEvents:[NSArray arrayWithObject:theEvent]];
+ [field setString:@""];
+ }
myDelegate->HandleKeyDown(theEvent);
}
@@ -414,10 +483,10 @@ static GLContext *sharedCtx = nil;
////////////////////////////////////////////////////////////
-/// Cocoa window implementation to let fullscreen windows
+/// Cocoa window category to let fullscreen windows
/// catch key events
////////////////////////////////////////////////////////////
-@implementation GLWindow
+@implementation NSWindow (GLWindow)
- (BOOL)canBecomeKeyWindow
{
@@ -432,309 +501,188 @@ static GLContext *sharedCtx = nil;
@end
+
+@implementation sfPrivWindow
+
+- (void)dealloc
+{
+ NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];
+ // Remove the notification observer
+ [[NSNotificationCenter defaultCenter] removeObserver:self];
+
+ // Close the window
+ [self show:false];
+
+ // Release the window and view
+ [[self view] removeFromSuperviewWithoutNeedingDisplay];
+
+ [self setWindow:nil];
+ [self setView:nil];
+ [self setDelegate:nil];
+
+ [super dealloc];
+ [localPool release];
+}
+
////////////////////////////////////////////////////////////
-/// WindowWrapper class : handles both imported and self-built windows
+/// Registers a reference to the internal Cocoa window
////////////////////////////////////////////////////////////
-@implementation WindowWrapper
+- (void)setWindow:(NSWindow *)aWindow
+{
+ if (myWindow != aWindow)
+ {
+ NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
+
+ // Drop the observers on the previously set window
+ if ([self window]) {
+ [nc removeObserver:self
+ name:NSWindowDidBecomeMainNotification
+ object:[self window]];
+ [nc removeObserver:self
+ name:NSWindowDidResignMainNotification
+ object:[self window]];
+ [nc removeObserver:self
+ name:NSWindowWillCloseNotification
+ object:[self window]];
+ }
+
+ [myWindow release];
+ myWindow = [aWindow retain];
+
+ // Set the new observers
+ // We want to know when our window got the focus
+ [nc addObserver:self
+ selector:@selector(windowDidBecomeMain:)
+ name:NSWindowDidBecomeMainNotification
+ object:[self window]];
+
+ // We want to know when our window lost the focus
+ [nc addObserver:self
+ selector:@selector(windowDidResignMain:)
+ name:NSWindowDidResignMainNotification
+ object:[self window]];
+
+ // We want to know when the user closes the window
+ [nc addObserver:self
+ selector:@selector(windowWillClose:)
+ name:NSWindowWillCloseNotification
+ object:[self window]];
+
+ }
+}
////////////////////////////////////////////////////////////
-/// Make a new window wrapper according to the window settings @attribs,
-/// the video mode @mode, the window style @style, the window title @title
-/// and the sf window implementation delegate @delegate
+/// Return a reference to the internal Cocoa window
////////////////////////////////////////////////////////////
-- (id)initWithSettings:(sf::WindowSettings&)params
- videoMode:(sf::VideoMode&)mode
- style:(unsigned long)style
- title:(NSString *)title
- delegate:(sf::priv::WindowImplCocoa *)delegate
+- (NSWindow *)window
{
- return [self initWithWindow:nil
- settings:params
- videoMode:mode
- style:style
- title:title
- delegate:delegate];
+ return [[myWindow retain] autorelease];
}
////////////////////////////////////////////////////////////
-/// Make a new window wrapper by importing @window and according to
-/// the window settings @params and the sf window implementation delegate
-/// @delegate
-/// @window and @delegate must not be null
+/// Registers the the OpenGL view and adds it to its parent container
////////////////////////////////////////////////////////////
-- (id)initWithWindow:(NSWindow *)window
- settings:(sf::WindowSettings&)params
- delegate:(sf::priv::WindowImplCocoa *)delegate
+- (void)putOpenGLView:(sfPrivGLView *)aView
{
- sf::VideoMode mode([[myWindow contentView] frame].size.width, [[myWindow contentView] frame].size.height);
- return [self initWithWindow:window
- settings:params
- videoMode:mode
- style:0
- title:nil
- delegate:delegate];
+ [self setView:aView];
+
+ // Finish setting up the view and window
+ // Add the view to our window and tell it to the view
+ [[[self window] contentView] addSubview:[self view]];
+ [[self view] finishInitialization];
}
-
////////////////////////////////////////////////////////////
-/// Make a new window wrapper by importing @window if it's not null and according to
-/// the window settings @params and the sf window implementation delegate
-/// @delegate; or by creating a new window if @window is null. In this case @title
-/// must therefore not be null and @params must be valid.
-/// @delegate must never be null
+/// Registers a reference to the internal Cocoa OpenGL view
////////////////////////////////////////////////////////////
-- (id)initWithWindow:(NSWindow *)window
- settings:(sf::WindowSettings&)params
- videoMode:(sf::VideoMode&)mode
- style:(unsigned long)style
- title:(NSString *)title
- delegate:(sf::priv::WindowImplCocoa *)delegate
+- (void)setView:(sfPrivGLView *)aView
{
- assert(delegate != NULL);
-
- self = [super init];
-
- if (self)
+ if (myView != aView)
{
- if (window) {
- myWindow = (GLWindow *)[window retain];
- } else {
- assert(title != nil);
-
- NSRect frame = NSMakeRect (0.0f, 0.0f, (float) mode.Width, (float) mode.Height);
- unsigned int mask = 0;
-
- if (style & sf::Style::Fullscreen) {
- myIsFullscreen = true;
-
- // Check display mode and put new values in 'mode' if needed
- boolean_t exact = true;
-
- CFDictionaryRef properties = CGDisplayBestModeForParameters(kCGDirectMainDisplay, mode.BitsPerPixel,
- mode.Width, mode.Height, &exact);
-
- if (!properties) {
- std::cerr << "Unable to get a display mode with the given parameters" << std::endl;
- [self autorelease];
- return nil;
- }
-
- if (exact == false) {
- CFNumberGetValue((CFNumberRef) CFDictionaryGetValue(properties, kCGDisplayWidth),
- kCFNumberIntType, &mode.Width);
-
- CFNumberGetValue((CFNumberRef) CFDictionaryGetValue(properties, kCGDisplayHeight),
- kCFNumberIntType, &mode.Height);
-
- CFNumberGetValue((CFNumberRef) CFDictionaryGetValue(properties, kCGDisplayBitsPerPixel),
- kCFNumberIntType, &mode.BitsPerPixel);
-
- }
- }
-
- // We grab options from WindowStyle and add them to our window mask
- if (style & sf::Style::None || style & sf::Style::Fullscreen) {
- mask |= NSBorderlessWindowMask;
-
-
-
- } else {
- if (style & sf::Style::Titlebar) {
- mask |= NSTitledWindowMask;
- mask |= NSMiniaturizableWindowMask;
- }
-
- if (style & sf::Style::Resize) {
- mask |= NSTitledWindowMask;
- mask |= NSMiniaturizableWindowMask;
- mask |= NSResizableWindowMask;
- }
-
- if (style & sf::Style::Close) {
- mask |= NSTitledWindowMask;
- mask |= NSClosableWindowMask;
- mask |= NSMiniaturizableWindowMask;
- }
- }
-
- // Now we make the window with the values we got
- // Note: defer flag set to NO to be able to use OpenGL in our window
- myWindow = [[GLWindow alloc] initWithContentRect:frame
- styleMask:mask
- backing:NSBackingStoreBuffered
- defer:NO];
-
- if (myWindow) {
- // We set title and window position
- [myWindow setTitle:title];
- [myWindow center];
- } else {
- std::cerr << "Unable to create the Cocoa window" << std::endl;
- [self autorelease];
- return nil;
- }
- }
-
- // Make the OpenGL view
- myView = [[GLView alloc] initWithFrame:[[myWindow contentView] frame]
- mode:mode
- settings:params
- delegate:delegate];
-
- if (myView) {
- // Finish setting up the view and window
- // Add the view to our window and tell it to the view
- [[myWindow contentView] addSubview:myView];
- [myView finishInitialization];
-
- NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
-
- // We want to know when our window got the focus
- [nc addObserver:self
- selector:@selector(windowDidBecomeMain:)
- name:NSWindowDidBecomeMainNotification
- object:myWindow];
-
- // We want to know when our window lost the focus
- [nc addObserver:self
- selector:@selector(windowDidResignMain:)
- name:NSWindowDidResignMainNotification
- object:myWindow];
-
- // We want to know when the user closes the window
- [nc addObserver:self
- selector:@selector(windowWillClose:)
- name:NSWindowWillCloseNotification
- object:myWindow];
-
- // I want to re-center the window if it's a full screen one and moved by Spaces
- [nc addObserver:self
- selector:@selector(windowDidMove:)
- name:NSWindowDidMoveNotification
- object:myWindow];
-
- // Needed not to make application crash when releasing the window in our destructor
- // (I prefer to take control of everything :P)
- [myWindow setReleasedWhenClosed:NO];
- [myWindow setAcceptsMouseMovedEvents:YES];
-
- } else {
- std::cerr << "Unable to create the OpenGL view" << std::endl;
- [self autorelease];
- return nil;
- }
-
- if (myIsFullscreen) {
- myFullscreenMode = mode;
-
- // Using this because full screen window was not always
- // in front of the other application windows when unhiding app
- [myWindow setLevel:NSFloatingWindowLevel];
- }
+ [myView release];
+ myView = [aView retain];
}
-
- return self;
}
-
////////////////////////////////////////////////////////////
-/// Clean the window wrapper
+/// Return a reference to the internal Cocoa OpenGL view
////////////////////////////////////////////////////////////
-- (void)dealloc
+- (sfPrivGLView *)view
{
-
- // Remove the notification observer
- [[NSNotificationCenter defaultCenter] removeObserver:self];
-
- // Close the window
- [self show:false];
-
- // Release the window and view
- [myView release];
- [myWindow release];
-
- [super dealloc];
+ return [[myView retain] autorelease];
}
-
////////////////////////////////////////////////////////////
-/// Return a reference to the internal Cocoa window
+/// Sets @aDelegate as the window delegate
////////////////////////////////////////////////////////////
-- (NSWindow *)window
+- (void)setDelegate:(sf::priv::WindowImplCocoa *)aDelegate
{
- return myWindow;
+ [[self view] setDelegate:aDelegate];
}
-
////////////////////////////////////////////////////////////
-/// Return a reference to the internal Cocoa OpenGL view
+/// Returns the window delegate
////////////////////////////////////////////////////////////
-- (GLView *)glView
+- (sf::priv::WindowImplCocoa *)delegate
{
- return myView;
+ return [[self view] delegate];
}
-
////////////////////////////////////////////////////////////
/// Forward call to set the window position on screen
////////////////////////////////////////////////////////////
- (void)setPosition:(NSPoint)pos
{
- assert(myWindow != nil);
+ NSAssert([self window] != nil, @"expected valid window");
- if (!myIsFullscreen) {
- // Flip Y and set window position
- pos.y = [[myWindow screen] frame].size.height - pos.y;
- [myWindow setFrameTopLeftPoint:pos];
- }
+ // Flip Y and set window position
+ pos.y = [[[self window] screen] frame].size.height - pos.y;
+ [[self window] setFrameTopLeftPoint:pos];
}
-
////////////////////////////////////////////////////////////
/// Forward call to set the window size
////////////////////////////////////////////////////////////
- (void)setSize:(NSSize)size
{
- assert(myWindow != nil);
+ NSAssert([self window] != nil, @"expected valid window");
- if (!myIsFullscreen) {
- [myWindow setFrame:NSMakeRect([myWindow frame].origin.x,
- [myWindow frame].origin.y,
- size.width, size.height)
- display:YES];
- }
+ [[self window] setFrame:NSMakeRect([[self window] frame].origin.x,
+ [[self window] frame].origin.y,
+ size.width, size.height)
+ display:YES];
}
-
////////////////////////////////////////////////////////////
/// Return the mouse location relative to the internal window
////////////////////////////////////////////////////////////
- (NSPoint)mouseLocation
{
- assert(myWindow != nil);
+ NSAssert([self view] != nil, @"expected valid window");
+
+ NSPoint windowPoint = [[self window] convertScreenToBase:[NSEvent mouseLocation]];
+ NSPoint viewPoint = [[self view] convertPoint:windowPoint fromView:[[[self window] contentView] superview]];
- NSPoint relativeLocation = [myWindow convertScreenToBase:[NSEvent mouseLocation]];
- relativeLocation.y = [[self glView] frame].size.height - relativeLocation.y;
- return relativeLocation;
+ viewPoint.y = [[self view] frame].size.height - viewPoint.y;
+ return viewPoint;
}
-
////////////////////////////////////////////////////////////
/// Return whether the mouse is on our window
////////////////////////////////////////////////////////////
- (BOOL)mouseInside
{
- assert(myWindow != nil);
- assert(myView != nil);
+ NSAssert([self window] != nil, @"expected valid window");
+ NSAssert([self view] != nil, @"expected valid OpenGL view");
BOOL flag = NO;
- if ([myWindow isVisible]) {
- NSPoint relativeToWindow = [myWindow mouseLocationOutsideOfEventStream];
- NSPoint relativeToView = [myView convertPoint:relativeToWindow fromView:nil];
+ if ([[self window] isVisible]) {
+ NSPoint relativeToWindow = [[self window] mouseLocationOutsideOfEventStream];
+ NSPoint relativeToView = [[self view] convertPoint:relativeToWindow fromView:nil];
- if (NSPointInRect (relativeToView, [myView bounds]))
+ if (NSPointInRect (relativeToView, [[self view] bounds]))
{
flag = YES;
}
@@ -743,32 +691,23 @@ static GLContext *sharedCtx = nil;
return flag;
}
-
////////////////////////////////////////////////////////////
/// Close or open the window
////////////////////////////////////////////////////////////
- (void)show:(bool)flag
{
- assert(myWindow != nil);
+ NSAssert([self window] != nil, @"expected valid window");
- if (flag && ![myWindow isVisible]) {
+ if (flag && ![[self window] isVisible]) {
// Wanna open the closed window
- if (myIsFullscreen) {
- [SharedAppController setFullscreenWindow:self mode:&myFullscreenMode];
- } else {
- // Show the window
- [myWindow makeKeyAndOrderFront:nil];
- }
- } else if (!flag && [myWindow isVisible]) {
+ // Show the window
+ [[self window] makeKeyAndOrderFront:nil];
+ } else if (!flag && [[self window] isVisible]) {
// Wanna close the opened window
- if (myIsFullscreen) {
- [SharedAppController setFullscreenWindow:nil mode:NULL];
- } else {
- // Close the window
- [myWindow close];
- }
+ // Close the window
+ [[self window] close];
}
}
@@ -778,31 +717,28 @@ static GLContext *sharedCtx = nil;
////////////////////////////////////////////////////////////
- (void)enableVerticalSync:(bool)flag
{
- assert(myView != nil);
- [myView enableVerticalSync:flag];
+ NSAssert([self view] != nil, @"expected valid OpenGL view");
+ [[self view] enableVerticalSync:flag];
}
-
////////////////////////////////////////////////////////////
/// Forward 'setActive' call the the OpenGL view
////////////////////////////////////////////////////////////
- (void)setActive:(bool)flag
{
- assert(myView != nil);
- [myView setActive:flag];
+ NSAssert([self view] != nil, @"expected valid OpenGL view");
+ [[self view] setActive:flag];
}
-
////////////////////////////////////////////////////////////
/// Forward call to flush the OpenGL view
////////////////////////////////////////////////////////////
- (void)flushBuffer
{
- assert(myView != nil);
- [myView flushBuffer];
+ NSAssert([self view] != nil, @"expected valid OpenGL view");
+ [[self view] flushBuffer];
}
-
////////////////////////////////////////////////////////////
/// Notification method receiver when the window gains focus
////////////////////////////////////////////////////////////
@@ -811,7 +747,7 @@ static GLContext *sharedCtx = nil;
sf::Event ev;
ev.Type = sf::Event::GainedFocus;
- [myView pushEvent:ev];
+ [[self view] pushEvent:ev];
}
@@ -823,7 +759,7 @@ static GLContext *sharedCtx = nil;
sf::Event ev;
ev.Type = sf::Event::LostFocus;
- [myView pushEvent:ev];
+ [[self view] pushEvent:ev];
}
@@ -835,9 +771,170 @@ static GLContext *sharedCtx = nil;
sf::Event ev;
ev.Type = sf::Event::Closed;
- [myView pushEvent:ev];
+ [[self view] pushEvent:ev];
+}
+
+
+@end
+
+
+@implementation sfPrivOwnedWindow
+
+- (id)initWithVideoMode:(sf::VideoMode&)aMode
+ settings:(sf::WindowSettings&)someSettings
+ style:(unsigned long)aStyle
+ title:(NSString *)aTitle
+{
+ self = [super init];
+ if (self)
+ {
+ if (aStyle & sf::Style::Fullscreen) {
+ [self setFullscreen:YES];
+ }
+
+ NSRect frame = NSMakeRect (0.0f, 0.0f, (float) aMode.Width, (float) aMode.Height);
+ unsigned int mask = 0;
+
+ if (aStyle & sf::Style::Fullscreen) {
+ // Check display mode and put new values in 'mode' if needed
+ boolean_t exact = true;
+
+ CFDictionaryRef properties =
+ CGDisplayBestModeForParameters([sfPrivAppController primaryScreen],
+ aMode.BitsPerPixel,
+ aMode.Width,
+ aMode.Height,
+ &exact);
+
+ if (!properties) {
+ std::cerr
+ << "Unable to get a display mode with the given parameters"
+ << std::endl;
+
+ [self autorelease];
+ return nil;
+ }
+
+ if (exact == false) {
+ CFNumberGetValue((CFNumberRef) CFDictionaryGetValue(properties,
+ kCGDisplayWidth),
+ kCFNumberIntType, &aMode.Width);
+
+ CFNumberGetValue((CFNumberRef) CFDictionaryGetValue(properties,
+ kCGDisplayHeight),
+ kCFNumberIntType, &aMode.Height);
+
+ CFNumberGetValue((CFNumberRef) CFDictionaryGetValue(properties,
+ kCGDisplayBitsPerPixel),
+ kCFNumberIntType, &aMode.BitsPerPixel);
+
+ }
+ }
+
+ // We grab options from WindowStyle and add them to our window mask
+ if (aStyle & sf::Style::None || aStyle & sf::Style::Fullscreen) {
+ mask |= NSBorderlessWindowMask;
+ } else {
+ if (aStyle & sf::Style::Titlebar) {
+ mask |= NSTitledWindowMask;
+ mask |= NSMiniaturizableWindowMask;
+ }
+
+ if (aStyle & sf::Style::Resize) {
+ mask |= NSTitledWindowMask;
+ mask |= NSMiniaturizableWindowMask;
+ mask |= NSResizableWindowMask;
+ }
+
+ if (aStyle & sf::Style::Close) {
+ mask |= NSTitledWindowMask;
+ mask |= NSClosableWindowMask;
+ mask |= NSMiniaturizableWindowMask;
+ }
+ }
+
+ // Now we make the window with the values we got
+ // Note: defer flag set to NO to be able to use OpenGL in our window
+ NSWindow *newWindow = [[NSWindow alloc]
+ initWithContentRect:frame
+ styleMask:mask
+ backing:NSBackingStoreBuffered
+ defer:NO];
+
+ if (newWindow) {
+ [self setWindow:[newWindow autorelease]];
+ } else {
+ std::cerr << "Unable to create the Cocoa window" << std::endl;
+ [self autorelease];
+ return nil;
+ }
+
+ // We set title and window position
+ [[self window] setTitle:aTitle != nil ? aTitle : @""];
+ [[self window] center];
+
+ // Make the OpenGL view
+ sfPrivGLView *newView = [[sfPrivGLView alloc]
+ initWithFrame:[[[self window] contentView] frame]
+ mode:aMode
+ settings:someSettings];
+
+ if (!newView) {
+ std::cerr << "Unable to create the OpenGL view" << std::endl;
+ [self autorelease];
+ return nil;
+ }
+
+ // Put our view in the window
+ [self putOpenGLView:[newView autorelease]];
+
+ // I want to re-center the window if it's a full screen one and moved by Spaces
+ [[NSNotificationCenter defaultCenter]
+ addObserver:self
+ selector:@selector(windowDidMove:)
+ name:NSWindowDidMoveNotification
+ object:[self window]];
+
+ // Needed not to make application crash when releasing the window in our destructor
+ // (I prefer to take control of everything :P)
+ [[self window] setReleasedWhenClosed:NO];
+ [[self window] setAcceptsMouseMovedEvents:YES];
+
+ if ([self isFullscreen]) {
+ myFullscreenMode = aMode;
+
+ // Using this because full screen window was not always
+ // in front of the other application windows when unhiding app
+ [[self window] setLevel:NSFloatingWindowLevel];
+ }
+ }
+ return self;
}
+- (void)dealloc
+{
+ [[NSNotificationCenter defaultCenter]
+ removeObserver:self
+ name:NSWindowDidMoveNotification
+ object:[self window]];
+ [super dealloc];
+}
+
+////////////////////////////////////////////////////////////
+/// Sets the window's fullscreen state
+////////////////////////////////////////////////////////////
+- (void)setFullscreen:(BOOL)aFlag
+{
+ myIsFullscreen = aFlag;
+}
+
+////////////////////////////////////////////////////////////
+/// Returns the window's fullscreen state
+////////////////////////////////////////////////////////////
+- (BOOL)isFullscreen
+{
+ return myIsFullscreen;
+}
////////////////////////////////////////////////////////////
/// Notification method receiver when the window finish moving
@@ -846,9 +943,182 @@ static GLContext *sharedCtx = nil;
{
NSWindow *sender = [notification object];
- if (!([sender styleMask] & NSTitledWindowMask))
+ if (myIsFullscreen)
[sender center];
}
+////////////////////////////////////////////////////////////
+/// Close or open the window
+////////////////////////////////////////////////////////////
+- (void)show:(bool)flag
+{
+ NSAssert([self window] != nil, @"expected valid window");
+
+ if (flag && ![[self window] isVisible]) {
+ // Wanna open the closed window
+
+ if ([self isFullscreen]) {
+ [[sfPrivAppController sharedController]
+ setFullscreenWindow:self
+ mode:&myFullscreenMode];
+ } else {
+ // Show the window
+ [[self window] makeKeyAndOrderFront:nil];
+ }
+ } else if (!flag && [[self window] isVisible]) {
+ // Wanna close the opened window
+
+ if ([self isFullscreen]) {
+ [[sfPrivAppController sharedController]
+ setFullscreenWindow:nil
+ mode:NULL];
+ } else {
+ // Close the window
+ [[self window] close];
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////
+/// Forward call to set the window position on screen
+////////////////////////////////////////////////////////////
+- (void)setPosition:(NSPoint)aPosition
+{
+ if (![self isFullscreen]) {
+ [super setPosition:aPosition];
+ }
+}
+
+////////////////////////////////////////////////////////////
+/// Forward call to set the window size
+////////////////////////////////////////////////////////////
+- (void)setSize:(NSSize)size
+{
+ if (![self isFullscreen]) {
+ [super setSize:size];
+ }
+}
+
+@end
+
+
+@implementation sfPrivImportedWindow
+
+- (id)initWithWindow:(NSWindow *)aWindow
+ settings:(sf::WindowSettings&)someSettings
+{
+ self = [super init];
+
+ if (self) {
+ [self setWindow:aWindow];
+
+ // Make the OpenGL view
+ sf::VideoMode mode([[[self window] contentView] frame].size.width,
+ [[[self window] contentView] frame].size.height);
+ sfPrivGLView *newView = [[sfPrivGLView alloc]
+ initWithFrame:[[[self window] contentView] frame]
+ mode:mode
+ settings:someSettings];
+
+ if (!newView) {
+ std::cerr << "Unable to create the OpenGL view" << std::endl;
+ [self autorelease];
+ return nil;
+ }
+
+ [self putOpenGLView:[newView autorelease]];
+
+ [[self window] setAcceptsMouseMovedEvents:YES];
+ }
+
+ return self;
+}
+
+@end
+
+
+@implementation sfPrivImportedView
+
+
+- (id)initWithView:(NSView *)aView
+ settings:(sf::WindowSettings&)someSettings
+{
+ self = [super init];
+ if (self)
+ {
+ parentView = [aView retain];
+ [self setWindow:[parentView window]];
+
+ // Make the OpenGL view
+ sf::VideoMode mode([parentView bounds].size.width,
+ [parentView bounds].size.height);
+ sfPrivGLView *newView = [[sfPrivGLView alloc]
+ initWithFrame:[parentView bounds]
+ mode:mode
+ settings:someSettings];
+
+ if (!newView) {
+ std::cerr << "Unable to create the OpenGL view" << std::endl;
+ [self autorelease];
+ return nil;
+ }
+
+ [self putOpenGLView:[newView autorelease]];
+ [[self window] setAcceptsMouseMovedEvents:YES];
+ }
+ return self;
+}
+
+- (void)dealloc
+{
+ [parentView release];
+ [super dealloc];
+}
+
+////////////////////////////////////////////////////////////
+/// Registers the the OpenGL view and adds it to its parent container
+////////////////////////////////////////////////////////////
+- (void)putOpenGLView:(sfPrivGLView *)aView
+{
+ [self setView:aView];
+
+ // Finish setting up the view and window
+ NSRect originalFrame = [[self window] frame];
+ NSRect tmpFrame = originalFrame;
+ originalFrame.origin.x++;
+
+ [[self window] setFrame:tmpFrame display:YES];
+ [[self window] setFrame:originalFrame display:YES];
+
+
+ // Add the view to our *parent view* and tell it to the view
+ [parentView addSubview:[self view]];
+ [[self view] finishInitialization];
+}
+
+////////////////////////////////////////////////////////////
+/// Forward call to set the window position on screen
+////////////////////////////////////////////////////////////
+- (void)setPosition:(NSPoint)aPosition
+{
+ std::cerr
+ << "Warning: called Window::SetPosition() on a window imported from a widget. "
+ << "This method has been disabled in this case and has no effect. "
+ << "Directly use the widget if you want to move it."
+ << std::endl;
+}
+
+////////////////////////////////////////////////////////////
+/// Forward call to set the window size
+////////////////////////////////////////////////////////////
+- (void)setSize:(NSSize)size
+{
+ std::cerr
+ << "Warning: called Window::SetSize() on a window imported from a widget. "
+ << "This method has been disabled in this case and has no effect. "
+ << "Directly use the widget if you want to resize it."
+ << std::endl;
+}
+
@end
diff --git a/src/SFML/Window/Cocoa/Joystick.cpp b/src/SFML/Window/Cocoa/Joystick.cpp
index 4ade09c..f65ac73 100755
--- a/src/SFML/Window/Cocoa/Joystick.cpp
+++ b/src/SFML/Window/Cocoa/Joystick.cpp
@@ -49,18 +49,17 @@ void Joystick::Initialize(unsigned int Index)
JoystickState Joystick::UpdateState()
{
// Fill a JoystickState instance with the current joystick state
- JoystickState s = {0};
+ JoystickState s;
return s;
}
////////////////////////////////////////////////////////////
-/// Get the number of axes supported by the joystick
+/// Check if the joystick supports the given axis
////////////////////////////////////////////////////////////
-unsigned int Joystick::GetAxesCount() const
+bool Joystick::HasAxis(Joy::Axis Axis) const
{
- // Return number of supported axes
- return 0;
+ return false;
}
diff --git a/src/SFML/Window/Cocoa/Joystick.hpp b/src/SFML/Window/Cocoa/Joystick.hpp
index 4259075..60c5db2 100755
--- a/src/SFML/Window/Cocoa/Joystick.hpp
+++ b/src/SFML/Window/Cocoa/Joystick.hpp
@@ -58,13 +58,15 @@ public :
JoystickState UpdateState();
////////////////////////////////////////////////////////////
- /// Get the number of axes supported by the joystick
+ /// Check if the joystick supports the given axis
///
- /// \return Number of axis
+ /// \param Axis : Axis to check
+ ///
+ /// \return True of the axis is supported, false otherwise
///
////////////////////////////////////////////////////////////
- unsigned int GetAxesCount() const;
-
+ bool HasAxis(Joy::Axis Axis) const;
+
////////////////////////////////////////////////////////////
/// Get the number of buttons supported by the joystick
///
diff --git a/src/SFML/Window/Cocoa/WindowImplCocoa.hpp b/src/SFML/Window/Cocoa/WindowImplCocoa.hpp
index bc93d54..62158f0 100755
--- a/src/SFML/Window/Cocoa/WindowImplCocoa.hpp
+++ b/src/SFML/Window/Cocoa/WindowImplCocoa.hpp
@@ -33,8 +33,10 @@
#include <string>
#ifdef __OBJC__
-#import <Cocoa/Cocoa.h>
-@class WindowWrapper;
+@class sfPrivWindow;
+typedef sfPrivWindow* sfPrivWindowRef;
+#else
+typedef void* sfPrivWindowRef;
#endif
namespace sf
@@ -108,6 +110,10 @@ public :
void HandleMouseMove(void *eventRef);
void HandleMouseWheel(void *eventRef);
+ ////////////////////////////////////////////////////////////
+ /// Return whether 'ev' must be considered as a TextEntered event
+ ////////////////////////////////////////////////////////////
+ static bool IsTextEvent(void *event);
private :
////////////////////////////////////////////////////////////
@@ -186,13 +192,7 @@ private :
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
-
-#ifdef __OBJC__
- WindowWrapper *myWrapper;
-#else
- void *myWrapper;
-#endif
-
+ sfPrivWindowRef myWrapper;
bool myUseKeyRepeat;
bool myMouseIn;
float myWheelStatus;
diff --git a/src/SFML/Window/Cocoa/WindowImplCocoa.mm b/src/SFML/Window/Cocoa/WindowImplCocoa.mm
index 3b66bbc..13dec83 100755
--- a/src/SFML/Window/Cocoa/WindowImplCocoa.mm
+++ b/src/SFML/Window/Cocoa/WindowImplCocoa.mm
@@ -31,9 +31,6 @@
#import <SFML/Window/Cocoa/GLKit.h>
#import <SFML/Window/WindowStyle.hpp>
#import <SFML/System.hpp>
-#import <OpenGL/OpenGL.h>
-#import <OpenGL/gl.h>
-#import <CoreFoundation/CoreFoundation.h>
#import <iostream>
@@ -54,26 +51,26 @@ __done = 1;\
////////////////////////////////////////////////////////////
/// Private function declarations
////////////////////////////////////////////////////////////
-static Key::Code KeyForVirtualCode(unsigned short vCode);
-static Key::Code KeyForUnicode(unsigned short uniCode);
-static bool IsTextEvent(NSEvent *event);
+namespace {
+ Key::Code KeyForVirtualCode(unsigned short vCode);
+ Key::Code KeyForUnicode(unsigned short uniCode);
+} // anonymous namespace
////////////////////////////////////////////////////////////
/// Default constructor
/// (creates a dummy window to provide a valid OpenGL context)
////////////////////////////////////////////////////////////
- static WindowImplCocoa *globalWin = NULL;
WindowImplCocoa::WindowImplCocoa() :
myWrapper(nil),
myUseKeyRepeat(false),
myMouseIn(false),
myWheelStatus(0.0f)
{
- [AppController sharedController];
+ [sfPrivAppController sharedController];
// Create the shared OpenGL context
- if ([GLContext sharedContext]) {
+ if ([sfPrivGLContext sharedContext]) {
// Then we make it the current active OpenGL context
SetActive();
} else {
@@ -91,33 +88,46 @@ myUseKeyRepeat(false),
myMouseIn(false),
myWheelStatus(0.0f)
{
- if (Handle)
- {
- if (![(NSWindow *)Handle isKindOfClass:[NSWindow class]])
- std::cerr << "Cannot import this Window Handle because it is not a <NSWindow *> object"
- << "(or one of its subclasses). You gave a <"
- << [[(NSWindow *)Handle className] UTF8String]
- << "> object." << std::endl;
- else
- {
+ if (Handle) {
+ // Classical window import
+ if ([(id)Handle isKindOfClass:[NSWindow class]]) {
+ myWrapper = [[sfPrivImportedWindow alloc]
+ initWithWindow:(NSWindow *)Handle
+ settings:params];
+ }
+ // Qt "window" import
+ else if ([(id)Handle isKindOfClass:[NSView class]]) {
+ myWrapper = [[sfPrivImportedView alloc]
+ initWithView:(NSView *)Handle
+ settings:params];
+ } else {
+ std::cerr
+ << "Cannot import this Window Handle because it is neither"
+ << "a <NSWindow *> nor <NSView *> object"
+ << "(or any of its subclasses). You gave a <"
+ << [[(id)Handle className] UTF8String]
+ << "> object."
+ << std::endl;
- // We create the window according to the given handle
- myWrapper = [[WindowWrapper alloc] initWithWindow:(NSWindow *)Handle
- settings:params
- delegate:this];
+ }
+
+ if (myWrapper) {
+ [myWrapper setDelegate:this];
- if (myWrapper)
- {
- // initial mouse state
- myMouseIn = [myWrapper mouseInside];
-
- // We set the myWidth and myHeight members to the correct values
- myWidth = (int) [[myWrapper glView] frame].size.width;
- myHeight = (int) [[myWrapper glView] frame].size.height;
- } else {
- std::cerr << "Failed to make the public window" << std::endl;
- }
+ // initial mouse state
+ myMouseIn = [myWrapper mouseInside];
+
+ // We set the myWidth and myHeight members to the correct values
+ myWidth = (int) [[myWrapper view] frame].size.width;
+ myHeight = (int) [[myWrapper view] frame].size.height;
+ } else {
+ std::cerr << "Failed to make the public window" << std::endl;
}
+ } else {
+ std::cerr
+ << "Invalid null handle given to "
+ << "Window::Window(WindowHandle Handle, const WindowSettings& Params)"
+ << std::endl;
}
}
@@ -133,17 +143,22 @@ myWheelStatus(0.0f)
{
// Create a new window with given size, title and style
// First we define some objects used for our window
- NSString *title = [NSString stringWithUTF8String:(Title.c_str()) ? (Title.c_str()) : ""];
+ NSString *title = [NSString stringWithCString:(Title.c_str()) ? (Title.c_str()) : ""
+ encoding:NSASCIIStringEncoding];
// We create the window
- myWrapper = [[WindowWrapper alloc] initWithSettings:params
- videoMode:Mode
- style:WindowStyle
- title:title
- delegate:this];
+ myWrapper = [[sfPrivOwnedWindow alloc]
+ initWithVideoMode:Mode
+ settings:params
+ style:WindowStyle
+ title:title];
+
+
if (myWrapper)
{
+ [myWrapper setDelegate:this];
+
// initial mouse state
myMouseIn = [myWrapper mouseInside];
@@ -228,18 +243,20 @@ void WindowImplCocoa::HandleKeyDown(void *eventRef)
// convert the characters
// note: using CFString in order to keep compatibility with Mac OS X 10.4
- // (NSUTF32StringEncoding only defined since Mac OS X 10.5)
+ // (as NSUTF32StringEncoding is only being defined in Mac OS X 10.5 and later)
if (!CFStringGetCString ((CFStringRef)[event characters],
(char *)utf32Characters,
sizeof(utf32Characters),
kCFStringEncodingUTF32))
{
- const char *utf8Char = NULL;
- if ([[event characters] lengthOfBytesUsingEncoding:NSUTF8StringEncoding])
- utf8Char = [[event characters] UTF8String];
+ char asciiChar[3] = {0};
+ if ([[event characters] lengthOfBytesUsingEncoding:NSASCIIStringEncoding])
+ [[event characters] getCString:asciiChar
+ maxLength:3
+ encoding:NSASCIIStringEncoding];
- std::cerr << "Error while converting character to UTF32 : "
- << ((utf8Char) ? utf8Char : "(undefined)") << std::endl;
+ std::cerr << "Error while converting character to UTF32 : \""
+ << asciiChar << "\"" << std::endl;
}
else
{
@@ -359,7 +376,9 @@ void WindowImplCocoa::HandleMouseDown(void *eventRef)
{
NSEvent *event = reinterpret_cast <NSEvent *> (eventRef);
Event sfEvent;
- NSPoint loc = {0, 0};
+
+ // Get mouse position relative to the window
+ NSPoint loc = [myWrapper mouseLocation];
unsigned mods = [event modifierFlags];
switch ([event type]) {
@@ -373,9 +392,6 @@ void WindowImplCocoa::HandleMouseDown(void *eventRef)
sfEvent.MouseButton.Button = Mouse::Left;
}
- // Get mouse position relative to the window
- loc = [myWrapper mouseLocation];
-
sfEvent.MouseButton.X = (int) loc.x;
sfEvent.MouseButton.Y = (int) loc.y;
@@ -387,9 +403,6 @@ void WindowImplCocoa::HandleMouseDown(void *eventRef)
sfEvent.Type = Event::MouseButtonPressed;
sfEvent.MouseButton.Button = Mouse::Right;
- // Get mouse position relative to the window
- loc = [myWrapper mouseLocation];
-
sfEvent.MouseButton.X = (int) loc.x;
sfEvent.MouseButton.Y = (int) loc.y;
@@ -410,7 +423,9 @@ void WindowImplCocoa::HandleMouseUp(void *eventRef)
{
NSEvent *event = reinterpret_cast <NSEvent *> (eventRef);
Event sfEvent;
- NSPoint loc = {0, 0};
+
+ // Get mouse position relative to the window
+ NSPoint loc = [myWrapper mouseLocation];
unsigned mods = [event modifierFlags];
switch ([event type]) {
@@ -424,9 +439,6 @@ void WindowImplCocoa::HandleMouseUp(void *eventRef)
sfEvent.MouseButton.Button = Mouse::Left;
}
- // Get mouse position relative to the window
- loc = [myWrapper mouseLocation];
-
sfEvent.MouseButton.X = (int) loc.x;
sfEvent.MouseButton.Y = (int) loc.y;
@@ -438,9 +450,6 @@ void WindowImplCocoa::HandleMouseUp(void *eventRef)
sfEvent.Type = Event::MouseButtonReleased;
sfEvent.MouseButton.Button = Mouse::Right;
- // Get mouse position relative to the window
- loc = [myWrapper mouseLocation];
-
sfEvent.MouseButton.X = (int) loc.x;
sfEvent.MouseButton.Y = (int) loc.y;
@@ -460,9 +469,7 @@ void WindowImplCocoa::HandleMouseUp(void *eventRef)
void WindowImplCocoa::HandleMouseMove(void *eventRef)
{
Event sfEvent;
- NSPoint loc = {0, 0};
-
- loc = [myWrapper mouseLocation];
+ NSPoint loc = [myWrapper mouseLocation];
sfEvent.Type = Event::MouseMoved;
sfEvent.MouseMove.X = (int) loc.x;
@@ -511,6 +518,25 @@ void WindowImplCocoa::HandleMouseWheel(void *eventRef)
}
}
+////////////////////////////////////////////////////////////
+/// Return whether 'ev' must be considered as a TextEntered event
+////////////////////////////////////////////////////////////
+bool WindowImplCocoa::IsTextEvent(void *eventRef)
+{
+ NSEvent *event = (NSEvent *)eventRef;
+ bool res = false;
+
+ if (event && [event type] == NSKeyDown && [[event characters] length]) {
+ unichar code = [[event characters] characterAtIndex:0];
+
+ // Codes from 0xF700 to 0xF8FF are non text keys (see NSEvent.h)
+ // 0x35 is the Escape key
+ if ([event keyCode] != 0x35 && (code < 0xF700 || code > 0xF8FF))
+ res = true;
+ }
+
+ return res;
+}
////////////////////////////////////////////////////////////
/// /see sfWindowImpl::Display
@@ -528,7 +554,7 @@ void WindowImplCocoa::Display()
void WindowImplCocoa::ProcessEvents()
{
// Forward event handling call to the application controller
- [SharedAppController processEvents];
+ [[sfPrivAppController sharedController] processEvents];
}
@@ -538,7 +564,18 @@ void WindowImplCocoa::ProcessEvents()
void WindowImplCocoa::SetActive(bool Active) const
{
// Forward the call to the window
- [myWrapper setActive:Active];
+ if (myWrapper)
+ [myWrapper setActive:Active];
+ else {
+ // Or directly activate the shared OpenGL context if we're not using a window
+ if (Active) {
+ if ([NSOpenGLContext currentContext] != [sfPrivGLContext sharedContext])
+ [[sfPrivGLContext sharedContext] makeCurrentContext];
+ } else {
+ if ([NSOpenGLContext currentContext] == [sfPrivGLContext sharedContext])
+ [NSOpenGLContext clearCurrentContext];
+ }
+ }
}
@@ -577,7 +614,7 @@ void WindowImplCocoa::SetCursorPosition(unsigned int Left, unsigned int Top)
pos.y = [[myWrapper window] frame].size.height - pos.y;
// Adjust for view reference instead of window
- pos.y -= [[myWrapper window] frame].size.height - [[myWrapper glView] frame].size.height;
+ pos.y -= [[myWrapper window] frame].size.height - [[myWrapper view] frame].size.height;
// Convert to screen coordinates
NSPoint absolute = [[myWrapper window] convertBaseToScreen:pos];
@@ -586,7 +623,8 @@ void WindowImplCocoa::SetCursorPosition(unsigned int Left, unsigned int Top)
absolute.y = [[NSScreen mainScreen] frame].size.height - absolute.y;
// Move cursor
- CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, CGPointMake(absolute.x, absolute.y));
+ CGDisplayMoveCursorToPoint([sfPrivAppController primaryScreen],
+ CGPointMake(absolute.x, absolute.y));
}
}
@@ -637,183 +675,167 @@ void WindowImplCocoa::SetIcon(unsigned int Width, unsigned int Height, const Uin
}
-////////////////////////////////////////////////////////////
-/// Return the SFML key corresponding to a key code
-////////////////////////////////////////////////////////////
-static Key::Code KeyForVirtualCode(unsigned short vCode)
-{
- static struct {
- unsigned short code;
- Key::Code sfKey;
- } virtualTable[] =
+namespace {
+ ////////////////////////////////////////////////////////////
+ /// Return the SFML key corresponding to a key code
+ ////////////////////////////////////////////////////////////
+ Key::Code KeyForVirtualCode(unsigned short vCode)
{
- {0x35, Key::Escape},
- {0x31, Key::Space},
- {0x24, Key::Return}, // main Return key
- {0x4C, Key::Return}, // pav Return key
- {0x33, Key::Back},
- {0x30, Key::Tab},
- {0x74, Key::PageUp},
- {0x79, Key::PageDown},
- {0x77, Key::End},
- {0x73, Key::Home},
- {0x72, Key::Insert},
- {0x75, Key::Delete},
- {0x45, Key::Add},
- {0x4E, Key::Subtract},
- {0x43, Key::Multiply},
- {0x4B, Key::Divide},
-
- {0x7A, Key::F1}, {0x78, Key::F2}, {0x63, Key::F3},
- {0x76, Key::F4}, {0x60, Key::F5}, {0x61, Key::F6},
- {0x62, Key::F7}, {0x64, Key::F8}, {0x65, Key::F9},
- {0x6D, Key::F10}, {0x67, Key::F11}, {0x6F, Key::F12},
- {0x69, Key::F13}, {0x6B, Key::F14}, {0x71, Key::F15},
-
- {0x7B, Key::Left},
- {0x7C, Key::Right},
- {0x7E, Key::Up},
- {0x7D, Key::Down},
-
- {0x52, Key::Numpad0}, {0x53, Key::Numpad1}, {0x54, Key::Numpad2},
- {0x55, Key::Numpad3}, {0x56, Key::Numpad4}, {0x57, Key::Numpad5},
- {0x58, Key::Numpad6}, {0x59, Key::Numpad7}, {0x5B, Key::Numpad8},
- {0x5C, Key::Numpad9},
-
- {0x1D, Key::Num0}, {0x12, Key::Num1}, {0x13, Key::Num2},
- {0x14, Key::Num3}, {0x15, Key::Num4}, {0x17, Key::Num5},
- {0x16, Key::Num6}, {0x1A, Key::Num7}, {0x1C, Key::Num8},
- {0x19, Key::Num9},
+ static struct {
+ unsigned short code;
+ Key::Code sfKey;
+ } virtualTable[] =
+ {
+ {0x35, Key::Escape},
+ {0x31, Key::Space},
+ {0x24, Key::Return}, // main Return key
+ {0x4C, Key::Return}, // pav Return key
+ {0x33, Key::Back},
+ {0x30, Key::Tab},
+ {0x74, Key::PageUp},
+ {0x79, Key::PageDown},
+ {0x77, Key::End},
+ {0x73, Key::Home},
+ {0x72, Key::Insert},
+ {0x75, Key::Delete},
+ {0x45, Key::Add},
+ {0x4E, Key::Subtract},
+ {0x43, Key::Multiply},
+ {0x4B, Key::Divide},
+
+ {0x7A, Key::F1}, {0x78, Key::F2}, {0x63, Key::F3},
+ {0x76, Key::F4}, {0x60, Key::F5}, {0x61, Key::F6},
+ {0x62, Key::F7}, {0x64, Key::F8}, {0x65, Key::F9},
+ {0x6D, Key::F10}, {0x67, Key::F11}, {0x6F, Key::F12},
+ {0x69, Key::F13}, {0x6B, Key::F14}, {0x71, Key::F15},
+
+ {0x7B, Key::Left},
+ {0x7C, Key::Right},
+ {0x7E, Key::Up},
+ {0x7D, Key::Down},
+
+ {0x52, Key::Numpad0}, {0x53, Key::Numpad1}, {0x54, Key::Numpad2},
+ {0x55, Key::Numpad3}, {0x56, Key::Numpad4}, {0x57, Key::Numpad5},
+ {0x58, Key::Numpad6}, {0x59, Key::Numpad7}, {0x5B, Key::Numpad8},
+ {0x5C, Key::Numpad9},
+
+ {0x1D, Key::Num0}, {0x12, Key::Num1}, {0x13, Key::Num2},
+ {0x14, Key::Num3}, {0x15, Key::Num4}, {0x17, Key::Num5},
+ {0x16, Key::Num6}, {0x1A, Key::Num7}, {0x1C, Key::Num8},
+ {0x19, Key::Num9},
+
+ {0x3B, Key::LControl}, //< Left Ctrl
+ {0x3A, Key::LAlt}, //< Left Option/Alt
+ {0x37, Key::LSystem}, //< Left Command
+ {0x38, Key::LShift}, //< Left Shift
+ {0x3E, Key::RControl}, //< Right Ctrl
+ {0x3D, Key::RAlt}, //< Right Option/Alt
+ {0x36, Key::RSystem}, //< Right Command
+ {0x3C, Key::RShift}, //< Right Shift
+
+ {0x39, Key::Code(0)} //< Caps Lock (not handled by SFML for now)
+ };
- {0x3B, Key::LControl}, //< Left Ctrl
- {0x3A, Key::LAlt}, //< Left Option/Alt
- {0x37, Key::LSystem}, //< Left Command
- {0x38, Key::LShift}, //< Left Shift
- {0x3E, Key::RControl}, //< Right Ctrl
- {0x3D, Key::RAlt}, //< Right Option/Alt
- {0x36, Key::RSystem}, //< Right Command
- {0x3C, Key::RShift}, //< Right Shift
+ Key::Code result = Key::Code(0);
- {0x39, Key::Code(0)} //< Caps Lock (not handled by SFML for now)
- };
-
- Key::Code result = Key::Code(0);
-
- for (unsigned i = 0;virtualTable[i].code;i++) {
- if (virtualTable[i].code == vCode) {
- result = virtualTable[i].sfKey;
- break;
+ for (unsigned i = 0;virtualTable[i].code;i++) {
+ if (virtualTable[i].code == vCode) {
+ result = virtualTable[i].sfKey;
+ break;
+ }
}
+
+ return result;
}
- return result;
-}
-
-
-////////////////////////////////////////////////////////////
-/// Return the SFML key corresponding to a unicode code
-////////////////////////////////////////////////////////////
-static Key::Code KeyForUnicode(unsigned short uniCode)
-{
- // TODO: find a better way to get the language independant key
- static struct {
- unsigned short character;
- Key::Code sfKey;
- } unicodeTable[] =
+
+ ////////////////////////////////////////////////////////////
+ /// Return the SFML key corresponding to a unicode code
+ ////////////////////////////////////////////////////////////
+ Key::Code KeyForUnicode(unsigned short uniCode)
{
- {'!', Key::Code(0)}, //< No Key for this code
- {'"', Key::Code(0)}, //< No Key for this code
- {'#', Key::Code(0)}, //< No Key for this code
- {'$', Key::Code(0)}, //< No Key for this code
- {'%', Key::Code(0)}, //< No Key for this code
- {'&', Key::Code(0)}, //< No Key for this code
- {'\'', Key::Quote},
- {'(', Key::Code(0)}, //< No Key for this code
- {')', Key::Code(0)}, //< No Key for this code
- {'*', Key::Multiply},
- {'+', Key::Add},
- {',', Key::Comma},
- {'-', Key::Code(0)}, //< Handled by KeyForVirtualCode()
- {'.', Key::Period},
- {'/', Key::Code(0)}, //< Handled by KeyForVirtualCode()
- {'0', Key::Code(0)}, //< Handled by KeyForVirtualCode()
- {'1', Key::Code(0)}, //< Handled by KeyForVirtualCode()
- {'2', Key::Code(0)}, //< Handled by KeyForVirtualCode()
- {'3', Key::Code(0)}, //< Handled by KeyForVirtualCode()
- {'4', Key::Code(0)}, //< Handled by KeyForVirtualCode()
- {'5', Key::Code(0)}, //< Handled by KeyForVirtualCode()
- {'6', Key::Code(0)}, //< Handled by KeyForVirtualCode()
- {'7', Key::Code(0)}, //< Handled by KeyForVirtualCode()
- {'8', Key::Code(0)}, //< Handled by KeyForVirtualCode()
- {'9', Key::Code(0)}, //< Handled by KeyForVirtualCode()
- {':', Key::Code(0)}, //< No Key for this code
- {';', Key::SemiColon},
- {'<', Key::Code(0)}, //< No Key for this code
- {'=', Key::Equal},
- {'>', Key::Code(0)}, //< No Key for this code
- {'?', Key::Code(0)}, //< No Key for this code
- {'@', Key::Code(0)}, //< No Key for this code
- {'A', Key::A}, {'B', Key::B}, {'C', Key::C},
- {'D', Key::D}, {'E', Key::E}, {'F', Key::F},
- {'G', Key::G}, {'H', Key::H}, {'I', Key::I},
- {'J', Key::J}, {'K', Key::K}, {'L', Key::L},
- {'M', Key::M}, {'N', Key::N}, {'O', Key::O},
- {'P', Key::P}, {'Q', Key::Q}, {'R', Key::R},
- {'S', Key::S}, {'T', Key::T}, {'U', Key::U},
- {'V', Key::V}, {'W', Key::W}, {'X', Key::X},
- {'Y', Key::Y}, {'Z', Key::Z},
- {'[', Key::LBracket},
- {'\\', Key::BackSlash},
- {']', Key::RBracket},
- {'^', Key::Code(0)}, //< No Key for this code
- {'_', Key::Code(0)}, //< No Key for this code
- {'`', Key::Code(0)}, //< No Key for this code
- {'a', Key::A}, {'b', Key::B}, {'c', Key::C},
- {'d', Key::D}, {'e', Key::E}, {'f', Key::F},
- {'g', Key::G}, {'h', Key::H}, {'i', Key::I},
- {'j', Key::J}, {'k', Key::K}, {'l', Key::L},
- {'m', Key::M}, {'n', Key::N}, {'o', Key::O},
- {'p', Key::P}, {'q', Key::Q}, {'r', Key::R},
- {'s', Key::S}, {'t', Key::T}, {'u', Key::U},
- {'v', Key::V}, {'w', Key::W}, {'x', Key::X},
- {'y', Key::Y}, {'z', Key::Z},
- {'{', Key::Code(0)}, //< No Key for this code
- {'|', Key::Code(0)}, //< No Key for this code
- {'}', Key::Code(0)}, //< No Key for this code
- {'~', Key::Tilde},
- {0, Key::Code(0)}
- };
-
- Key::Code result = Key::Code(0);
-
- for (unsigned i = 0;unicodeTable[i].character;i++) {
- if (unicodeTable[i].character == uniCode) {
- result = unicodeTable[i].sfKey;
- break;
+ // TODO: find a better way to get the language independant key
+ static struct {
+ unsigned short character;
+ Key::Code sfKey;
+ } unicodeTable[] =
+ {
+ {'!', Key::Code(0)}, //< No Key for this code
+ {'"', Key::Code(0)}, //< No Key for this code
+ {'#', Key::Code(0)}, //< No Key for this code
+ {'$', Key::Code(0)}, //< No Key for this code
+ {'%', Key::Code(0)}, //< No Key for this code
+ {'&', Key::Code(0)}, //< No Key for this code
+ {'\'', Key::Quote},
+ {'(', Key::Code(0)}, //< No Key for this code
+ {')', Key::Code(0)}, //< No Key for this code
+ {'*', Key::Multiply},
+ {'+', Key::Add},
+ {',', Key::Comma},
+ {'-', Key::Code(0)}, //< Handled by KeyForVirtualCode()
+ {'.', Key::Period},
+ {'/', Key::Code(0)}, //< Handled by KeyForVirtualCode()
+ {'0', Key::Code(0)}, //< Handled by KeyForVirtualCode()
+ {'1', Key::Code(0)}, //< Handled by KeyForVirtualCode()
+ {'2', Key::Code(0)}, //< Handled by KeyForVirtualCode()
+ {'3', Key::Code(0)}, //< Handled by KeyForVirtualCode()
+ {'4', Key::Code(0)}, //< Handled by KeyForVirtualCode()
+ {'5', Key::Code(0)}, //< Handled by KeyForVirtualCode()
+ {'6', Key::Code(0)}, //< Handled by KeyForVirtualCode()
+ {'7', Key::Code(0)}, //< Handled by KeyForVirtualCode()
+ {'8', Key::Code(0)}, //< Handled by KeyForVirtualCode()
+ {'9', Key::Code(0)}, //< Handled by KeyForVirtualCode()
+ {':', Key::Code(0)}, //< No Key for this code
+ {';', Key::SemiColon},
+ {'<', Key::Code(0)}, //< No Key for this code
+ {'=', Key::Equal},
+ {'>', Key::Code(0)}, //< No Key for this code
+ {'?', Key::Code(0)}, //< No Key for this code
+ {'@', Key::Code(0)}, //< No Key for this code
+ {'A', Key::A}, {'B', Key::B}, {'C', Key::C},
+ {'D', Key::D}, {'E', Key::E}, {'F', Key::F},
+ {'G', Key::G}, {'H', Key::H}, {'I', Key::I},
+ {'J', Key::J}, {'K', Key::K}, {'L', Key::L},
+ {'M', Key::M}, {'N', Key::N}, {'O', Key::O},
+ {'P', Key::P}, {'Q', Key::Q}, {'R', Key::R},
+ {'S', Key::S}, {'T', Key::T}, {'U', Key::U},
+ {'V', Key::V}, {'W', Key::W}, {'X', Key::X},
+ {'Y', Key::Y}, {'Z', Key::Z},
+ {'[', Key::LBracket},
+ {'\\', Key::BackSlash},
+ {']', Key::RBracket},
+ {'^', Key::Code(0)}, //< No Key for this code
+ {'_', Key::Code(0)}, //< No Key for this code
+ {'`', Key::Code(0)}, //< No Key for this code
+ {'a', Key::A}, {'b', Key::B}, {'c', Key::C},
+ {'d', Key::D}, {'e', Key::E}, {'f', Key::F},
+ {'g', Key::G}, {'h', Key::H}, {'i', Key::I},
+ {'j', Key::J}, {'k', Key::K}, {'l', Key::L},
+ {'m', Key::M}, {'n', Key::N}, {'o', Key::O},
+ {'p', Key::P}, {'q', Key::Q}, {'r', Key::R},
+ {'s', Key::S}, {'t', Key::T}, {'u', Key::U},
+ {'v', Key::V}, {'w', Key::W}, {'x', Key::X},
+ {'y', Key::Y}, {'z', Key::Z},
+ {'{', Key::Code(0)}, //< No Key for this code
+ {'|', Key::Code(0)}, //< No Key for this code
+ {'}', Key::Code(0)}, //< No Key for this code
+ {'~', Key::Tilde},
+ {0, Key::Code(0)}
+ };
+
+ Key::Code result = Key::Code(0);
+
+ for (unsigned i = 0;unicodeTable[i].character;i++) {
+ if (unicodeTable[i].character == uniCode) {
+ result = unicodeTable[i].sfKey;
+ break;
+ }
}
- }
-
- return result;
-}
-
-
-////////////////////////////////////////////////////////////
-/// Return whether 'ev' must be considered as a TextEntered event
-////////////////////////////////////////////////////////////
-static bool IsTextEvent(NSEvent *event)
-{
- bool res = false;
-
- if (event && [event type] == NSKeyDown && [[event characters] length]) {
- unichar code = [[event characters] characterAtIndex:0];
- // Codes from 0xF700 to 0xF8FF are non text keys (see NSEvent.h)
- if (code < 0xF700 || code > 0xF8FF)
- res = true;
+ return result;
}
- return res;
-}
+} // anonymous namespace
} // namespace priv
diff --git a/src/SFML/Window/Input.cpp b/src/SFML/Window/Input.cpp
index 7b10270..01dd54f 100755
--- a/src/SFML/Window/Input.cpp
+++ b/src/SFML/Window/Input.cpp
@@ -37,23 +37,7 @@ Input::Input() :
myMouseX(0),
myMouseY(0)
{
- for (int i = 0; i < Key::Count; ++i)
- myKeys[i] = false;
-
- for (int i = 0; i < Mouse::Count; ++i)
- myMouseButtons[i] = false;
-
- for (int i = 0; i < 16; ++i)
- {
- myJoystickButtons[0][i] = false;
- myJoystickButtons[1][i] = false;
- }
-
- for (int i = 0; i < Joy::Count; ++i)
- {
- myJoystickAxis[0][i] = 0.f;
- myJoystickAxis[1][i] = 0.f;
- }
+ ResetStates();
}
@@ -80,7 +64,7 @@ bool Input::IsMouseButtonDown(Mouse::Button Button) const
////////////////////////////////////////////////////////////
bool Input::IsJoystickButtonDown(unsigned int JoyId, unsigned int Button) const
{
- if ((JoyId < 2) && (Button < 16))
+ if ((JoyId < Joy::Count) && (Button < Joy::ButtonCount))
return myJoystickButtons[JoyId][Button];
else
return false;
@@ -110,7 +94,10 @@ int Input::GetMouseY() const
////////////////////////////////////////////////////////////
float Input::GetJoystickAxis(unsigned int JoyId, Joy::Axis Axis) const
{
- return myJoystickAxis[JoyId][Axis];
+ if (JoyId < Joy::Count)
+ return myJoystickAxis[JoyId][Axis];
+ else
+ return 0.f;
}
@@ -147,17 +134,7 @@ void Input::OnEvent(const Event& EventReceived)
// Lost focus event : we must reset all persistent states
case Event::LostFocus :
{
- for (int i = 0; i < Key::Count; ++i)
- myKeys[i] = false;
-
- for (int i = 0; i < Mouse::Count; ++i)
- myMouseButtons[i] = false;
-
- for (int i = 0; i < 16; ++i)
- {
- myJoystickButtons[0][i] = false;
- myJoystickButtons[1][i] = false;
- }
+ ResetStates();
break;
}
@@ -166,4 +143,27 @@ void Input::OnEvent(const Event& EventReceived)
}
}
+
+////////////////////////////////////////////////////////////
+/// Reset all the states
+////////////////////////////////////////////////////////////
+void Input::ResetStates()
+{
+ for (int i = 0; i < Key::Count; ++i)
+ myKeys[i] = false;
+
+ for (int i = 0; i < Mouse::ButtonCount; ++i)
+ myMouseButtons[i] = false;
+
+ for (int i = 0; i < Joy::Count; ++i)
+ {
+ for (int j = 0; j < Joy::ButtonCount; ++j)
+ myJoystickButtons[i][j] = false;
+
+ for (int j = 0; j < Joy::AxisCount; ++j)
+ myJoystickAxis[i][j] = 0.f;
+ myJoystickAxis[i][Joy::AxisPOV] = -1.f;
+ }
+}
+
} // namespace sf
diff --git a/src/SFML/Window/Joystick.hpp b/src/SFML/Window/Joystick.hpp
index 4a17f4e..6dd1b6e 100755
--- a/src/SFML/Window/Joystick.hpp
+++ b/src/SFML/Window/Joystick.hpp
@@ -41,10 +41,20 @@ namespace priv
////////////////////////////////////////////////////////////
struct JoystickState
{
- enum {MaxButtons = 32};
-
- float Axis[Joy::Count]; ///< Position on each axis in range [-100, 100] (except POV which is [0, 360])
- bool Buttons[MaxButtons]; ///< Status of each button (true = pressed)
+ JoystickState()
+ {
+ // Default value for axes
+ for (int i = 0; i < Joy::AxisCount; ++i)
+ Axis[i] = 0.f;
+ Axis[Joy::AxisPOV] = -1.f;
+
+ // Default value for buttons
+ for (int i = 0; i < Joy::ButtonCount; ++i)
+ Buttons[i] = false;
+ }
+
+ float Axis[Joy::AxisCount]; ///< Position on each axis in range [-100, 100] (except POV which is [0, 360])
+ bool Buttons[Joy::ButtonCount]; ///< Status of each button (true = pressed)
};
} // namespace priv
diff --git a/src/SFML/Window/Linux/Joystick.cpp b/src/SFML/Window/Linux/Joystick.cpp
index 509bc53..829a741 100755
--- a/src/SFML/Window/Linux/Joystick.cpp
+++ b/src/SFML/Window/Linux/Joystick.cpp
@@ -28,13 +28,6 @@
#include <SFML/Window/Joystick.hpp>
#include <sstream>
-#if defined(SFML_SYSTEM_LINUX)
- #include <linux/joystick.h>
- #include <fcntl.h>
-#elif defined(SFML_SYSTEM_FREEBSD)
- // #include <sys/joystick.h> ?
-#endif
-
namespace sf
{
@@ -48,12 +41,18 @@ namespace priv
void Joystick::Initialize(unsigned int Index)
{
// Initial state
- myNbAxes = 0;
myNbButtons = 0;
- for (int i = 0; i < Joy::Count; ++i)
- myState.Axis[i] = 0.f;
- for (int i = 0; i < JoystickState::MaxButtons; ++i)
+ myPovX = 0;
+ myPovY = 0;
+ for (int i = 0; i < Joy::ButtonCount; ++i)
+ {
myState.Buttons[i] = false;
+ }
+ for (int i = 0; i < Joy::AxisCount; ++i)
+ {
+ myState.Axis[i] = 0.f;
+ myAxes[i] = false;
+ }
// Open the joystick handle
std::ostringstream oss;
@@ -63,13 +62,32 @@ void Joystick::Initialize(unsigned int Index)
{
// Use non-blocking mode
fcntl(myDescriptor, F_SETFL, O_NONBLOCK);
-
- // Get number of axes and buttons
- char NbAxes, NbButtons;
- ioctl(myDescriptor, JSIOCGAXES, &NbAxes);
+
+ // Get number of buttons
+ char NbButtons;
ioctl(myDescriptor, JSIOCGBUTTONS, &NbButtons);
- myNbAxes = NbAxes;
myNbButtons = NbButtons;
+ if (myNbButtons > Joy::ButtonCount)
+ myNbButtons = Joy::ButtonCount;
+
+ // Get the supported axes
+ char NbAxes;
+ ioctl(myDescriptor, JSIOCGAXES, &NbAxes);
+ ioctl(myDescriptor, JSIOCGAXMAP, myAxesMapping);
+ for (int i = 0; i < NbAxes; ++i)
+ {
+ switch (myAxesMapping[i])
+ {
+ case ABS_X : myAxes[Joy::AxisX] = true; break;
+ case ABS_Y : myAxes[Joy::AxisY] = true; break;
+ case ABS_Z : case ABS_THROTTLE : myAxes[Joy::AxisZ] = true; break;
+ case ABS_RZ: case ABS_RUDDER: myAxes[Joy::AxisR] = true; break;
+ case ABS_RX : myAxes[Joy::AxisU] = true; break;
+ case ABS_RY : myAxes[Joy::AxisV] = true; break;
+ case ABS_HAT0X : case ABS_HAT0Y : myAxes[Joy::AxisPOV] = true; break;
+ default : break;
+ }
+ }
}
}
@@ -89,11 +107,41 @@ JoystickState Joystick::UpdateState()
// An axis has been moved
case JS_EVENT_AXIS :
{
- if (JoyState.number < Joy::Count)
- myState.Axis[JoyState.number] = JoyState.value * 100.f / 32767.f;
+ switch (myAxesMapping[JoyState.number])
+ {
+ case ABS_X : myState.Axis[Joy::AxisX] = JoyState.value * 100.f / 32767.f; break;
+ case ABS_Y : myState.Axis[Joy::AxisY] = JoyState.value * 100.f / 32767.f; break;
+ case ABS_Z : case ABS_THROTTLE : myState.Axis[Joy::AxisZ] = JoyState.value * 100.f / 32767.f; break;
+ case ABS_RZ: case ABS_RUDDER: myState.Axis[Joy::AxisR] = JoyState.value * 100.f / 32767.f; break;
+ case ABS_RX : myState.Axis[Joy::AxisU] = JoyState.value * 100.f / 32767.f; break;
+ case ABS_RY : myState.Axis[Joy::AxisV] = JoyState.value * 100.f / 32767.f; break;
+ case ABS_HAT0X : myPovX = JoyState.value; break;
+ case ABS_HAT0Y : myPovY = JoyState.value; break;
+ default : break;
+ }
+
+ // Compute the new POV angle
+ if (myPovX > 0)
+ {
+ if (myPovY > 0) myState.Axis[Joy::AxisPOV] = 135.f;
+ else if (myPovY < 0) myState.Axis[Joy::AxisPOV] = 45.f;
+ else myState.Axis[Joy::AxisPOV] = 90.f;
+ }
+ else if (myPovX < 0)
+ {
+ if (myPovY > 0) myState.Axis[Joy::AxisPOV] = 225.f;
+ else if (myPovY < 0) myState.Axis[Joy::AxisPOV] = 315.f;
+ else myState.Axis[Joy::AxisPOV] = 270.f;
+ }
+ else
+ {
+ if (myPovY > 0) myState.Axis[Joy::AxisPOV] = 180.f;
+ else if (myPovY < 0) myState.Axis[Joy::AxisPOV] = 0.f;
+ else myState.Axis[Joy::AxisPOV] = -1.f;
+ }
break;
}
-
+
// A button has been pressed
case JS_EVENT_BUTTON :
{
@@ -110,11 +158,11 @@ JoystickState Joystick::UpdateState()
////////////////////////////////////////////////////////////
-/// Get the number of axes supported by the joystick
+/// Check if the joystick supports the given axis
////////////////////////////////////////////////////////////
-unsigned int Joystick::GetAxesCount() const
+bool Joystick::HasAxis(Joy::Axis Axis) const
{
- return myNbAxes;
+ return myAxes[Axis];
}
@@ -148,11 +196,11 @@ JoystickState Joystick::UpdateState()
////////////////////////////////////////////////////////////
-/// Get the number of axes supported by the joystick
+/// Check if the joystick supports the given axis
////////////////////////////////////////////////////////////
-unsigned int Joystick::GetAxesCount() const
+bool Joystick::HasAxis(Joy::Axis Axis) const
{
- return 0;
+ return false;
}
diff --git a/src/SFML/Window/Linux/Joystick.hpp b/src/SFML/Window/Linux/Joystick.hpp
index a035f61..3122264 100755
--- a/src/SFML/Window/Linux/Joystick.hpp
+++ b/src/SFML/Window/Linux/Joystick.hpp
@@ -28,6 +28,13 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
+#if defined(SFML_SYSTEM_LINUX)
+ #include <linux/joystick.h>
+ #include <fcntl.h>
+#elif defined(SFML_SYSTEM_FREEBSD)
+ // #include <sys/joystick.h> ?
+ #define ABS_MAX 1
+#endif
namespace sf
@@ -58,12 +65,14 @@ public :
JoystickState UpdateState();
////////////////////////////////////////////////////////////
- /// Get the number of axes supported by the joystick
+ /// Check if the joystick supports the given axis
///
- /// \return Number of axis
+ /// \param Axis : Axis to check
+ ///
+ /// \return True of the axis is supported, false otherwise
///
////////////////////////////////////////////////////////////
- unsigned int GetAxesCount() const;
+ bool HasAxis(Joy::Axis Axis) const;
////////////////////////////////////////////////////////////
/// Get the number of buttons supported by the joystick
@@ -78,10 +87,13 @@ private :
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
- int myDescriptor; ///< Linux descriptor of the joystick
- unsigned int myNbAxes; ///< Number of axis supported by the joystick
- unsigned int myNbButtons; ///< Number of buttons supported by the joystick
- JoystickState myState; ///< Current state of the joystick
+ int myDescriptor; ///< Linux descriptor of the joystick
+ unsigned int myNbButtons; ///< Number of buttons supported by the joystick
+ bool myAxes[Joy::AxisCount]; ///< Supported axes
+ JoystickState myState; ///< Current state of the joystick
+ int myPovX; ///< Last X position of the POV
+ int myPovY; ///< Last Y position of the POV
+ char myAxesMapping[ABS_MAX + 1]; ///< Axes mapping (index --> axis id)
};
} // namespace priv
diff --git a/src/SFML/Window/Linux/WindowImplX11.cpp b/src/SFML/Window/Linux/WindowImplX11.cpp
index fa0bd8e..edec19b 100755
--- a/src/SFML/Window/Linux/WindowImplX11.cpp
+++ b/src/SFML/Window/Linux/WindowImplX11.cpp
@@ -349,20 +349,11 @@ WindowImplX11::~WindowImplX11()
// Close the display
CloseDisplay();
}
-
-
+
+
////////////////////////////////////////////////////////////
/// Check if there's an active context on the current thread
-////////////////////
////////////////////////////////////////////////////////////
-/// Filter the received events
-/// (only allow those matching a specific window)
-////////////////////////////////////////////////////////////
-Bool CheckEvent(::Display*, XEvent* Event, XPointer UserData)
-{
- // Just check if the event matches our window
- return Event->xany.window == reinterpret_cast< ::Window >(UserData);
-}////////////////////////////////////////
bool WindowImplX11::IsContextActive()
{
return glXGetCurrentContext() != NULL;
diff --git a/src/SFML/Window/Makefile b/src/SFML/Window/Makefile
index 956b34b..0dd689c 100755
--- a/src/SFML/Window/Makefile
+++ b/src/SFML/Window/Makefile
@@ -9,7 +9,7 @@ ifeq ($(STATIC), yes)
else
LIB = libsfml-window.so
LIBNAME = $(LIBPATH)/$(LIB).$(VERSION)
- INSTALL = && $(LN) $(LNFLAGS) $(DESTLIBDIR)/$(LIB).$(VERSION) $(DESTLIBDIR)/$(LIB)
+ INSTALL = && $(LN) $(LNFLAGS) $(LIB).$(VERSION) $(DESTLIBDIR)/$(LIB)
endif
all: $(LIB)
diff --git a/src/SFML/Window/Win32/Joystick.cpp b/src/SFML/Window/Win32/Joystick.cpp
index 10bb0d6..e5e16a9 100755
--- a/src/SFML/Window/Win32/Joystick.cpp
+++ b/src/SFML/Window/Win32/Joystick.cpp
@@ -42,9 +42,12 @@ namespace priv
void Joystick::Initialize(unsigned int Index)
{
// Reset state
- myIndex = JOYSTICKID1;
- myNbAxes = 0;
- myNbButtons = 0;
+ myIndex = JOYSTICKID1;
+ myNbButtons = 0;
+ myIsConnected = false;
+ myHasContinuousPOV = false;
+ for (int i = 0; i < Joy::AxisCount; ++i)
+ myAxes[i] = false;
// Get the Index-th connected joystick
MMRESULT Error;
@@ -60,12 +63,21 @@ void Joystick::Initialize(unsigned int Index)
if (NbFound == Index)
{
// Ok : store its parameters and return
+ myIsConnected = true;
JOYCAPS Caps;
joyGetDevCaps(myIndex, &Caps, sizeof(Caps));
- myNbAxes = Caps.wNumAxes;
myNbButtons = Caps.wNumButtons;
- if (myNbButtons > JoystickState::MaxButtons)
- myNbButtons = JoystickState::MaxButtons;
+ if (myNbButtons > Joy::ButtonCount)
+ myNbButtons = Joy::ButtonCount;
+
+ myAxes[Joy::AxisX] = true;
+ myAxes[Joy::AxisY] = true;
+ myAxes[Joy::AxisZ] = (Caps.wCaps & JOYCAPS_HASZ) != 0;
+ myAxes[Joy::AxisR] = (Caps.wCaps & JOYCAPS_HASR) != 0;
+ myAxes[Joy::AxisU] = (Caps.wCaps & JOYCAPS_HASU) != 0;
+ myAxes[Joy::AxisV] = (Caps.wCaps & JOYCAPS_HASV) != 0;
+ myAxes[Joy::AxisPOV] = (Caps.wCaps & JOYCAPS_HASPOV) != 0;
+ myHasContinuousPOV = (Caps.wCaps & JOYCAPS_POVCTS) != 0;
return;
}
@@ -82,32 +94,39 @@ void Joystick::Initialize(unsigned int Index)
////////////////////////////////////////////////////////////
JoystickState Joystick::UpdateState()
{
- JoystickState State = {0};
+ JoystickState State;
- // Get the joystick caps (for range conversions)
- JOYCAPS Caps;
- if (joyGetDevCaps(myIndex, &Caps, sizeof(Caps)) == JOYERR_NOERROR)
+ if (myIsConnected)
{
- // Get the current joystick state
- JOYINFOEX Pos;
- Pos.dwFlags = JOY_RETURNALL;
- Pos.dwSize = sizeof(JOYINFOEX);
- if (joyGetPosEx(myIndex, &Pos) == JOYERR_NOERROR)
+ // Get the joystick caps (for range conversions)
+ JOYCAPS Caps;
+ if (joyGetDevCaps(myIndex, &Caps, sizeof(Caps)) == JOYERR_NOERROR)
{
- // Axes
- State.Axis[Joy::AxisX] = (Pos.dwXpos - (Caps.wXmax + Caps.wXmin) / 2.f) * 200.f / (Caps.wXmax - Caps.wXmin);
- State.Axis[Joy::AxisY] = (Pos.dwYpos - (Caps.wYmax + Caps.wYmin) / 2.f) * 200.f / (Caps.wYmax - Caps.wYmin);
- State.Axis[Joy::AxisZ] = (Pos.dwZpos - (Caps.wZmax + Caps.wZmin) / 2.f) * 200.f / (Caps.wZmax - Caps.wZmin);
- State.Axis[Joy::AxisR] = (Pos.dwRpos - (Caps.wRmax + Caps.wRmin) / 2.f) * 200.f / (Caps.wRmax - Caps.wRmin);
- State.Axis[Joy::AxisU] = (Pos.dwUpos - (Caps.wUmax + Caps.wUmin) / 2.f) * 200.f / (Caps.wUmax - Caps.wUmin);
- State.Axis[Joy::AxisV] = (Pos.dwVpos - (Caps.wVmax + Caps.wVmin) / 2.f) * 200.f / (Caps.wVmax - Caps.wVmin);
-
- // POV
- State.Axis[Joy::AxisPOV] = Pos.dwPOV / 100.f;
-
- // Buttons
- for (unsigned int i = 0; i < GetButtonsCount(); ++i)
- State.Buttons[i] = (Pos.dwButtons & (1 << i)) != 0;
+ // Get the current joystick state
+ JOYINFOEX Pos;
+ Pos.dwFlags = JOY_RETURNX | JOY_RETURNY | JOY_RETURNZ | JOY_RETURNR | JOY_RETURNU | JOY_RETURNV | JOY_RETURNBUTTONS;
+ Pos.dwFlags |= myHasContinuousPOV ? JOY_RETURNPOVCTS : JOY_RETURNPOV;
+ Pos.dwSize = sizeof(JOYINFOEX);
+ if (joyGetPosEx(myIndex, &Pos) == JOYERR_NOERROR)
+ {
+ // Axes
+ State.Axis[Joy::AxisX] = (Pos.dwXpos - (Caps.wXmax + Caps.wXmin) / 2.f) * 200.f / (Caps.wXmax - Caps.wXmin);
+ State.Axis[Joy::AxisY] = (Pos.dwYpos - (Caps.wYmax + Caps.wYmin) / 2.f) * 200.f / (Caps.wYmax - Caps.wYmin);
+ State.Axis[Joy::AxisZ] = (Pos.dwZpos - (Caps.wZmax + Caps.wZmin) / 2.f) * 200.f / (Caps.wZmax - Caps.wZmin);
+ State.Axis[Joy::AxisR] = (Pos.dwRpos - (Caps.wRmax + Caps.wRmin) / 2.f) * 200.f / (Caps.wRmax - Caps.wRmin);
+ State.Axis[Joy::AxisU] = (Pos.dwUpos - (Caps.wUmax + Caps.wUmin) / 2.f) * 200.f / (Caps.wUmax - Caps.wUmin);
+ State.Axis[Joy::AxisV] = (Pos.dwVpos - (Caps.wVmax + Caps.wVmin) / 2.f) * 200.f / (Caps.wVmax - Caps.wVmin);
+
+ // POV
+ if (Pos.dwPOV != 0xFFFF)
+ State.Axis[Joy::AxisPOV] = Pos.dwPOV / 100.f;
+ else
+ State.Axis[Joy::AxisPOV] = -1.f;
+
+ // Buttons
+ for (unsigned int i = 0; i < GetButtonsCount(); ++i)
+ State.Buttons[i] = (Pos.dwButtons & (1 << i)) != 0;
+ }
}
}
@@ -116,11 +135,11 @@ JoystickState Joystick::UpdateState()
////////////////////////////////////////////////////////////
-/// Get the number of axes supported by the joystick
+/// Check if the joystick supports the given axis
////////////////////////////////////////////////////////////
-unsigned int Joystick::GetAxesCount() const
+bool Joystick::HasAxis(Joy::Axis Axis) const
{
- return myNbAxes;
+ return myAxes[Axis];
}
diff --git a/src/SFML/Window/Win32/Joystick.hpp b/src/SFML/Window/Win32/Joystick.hpp
index 8303a7e..1538069 100755
--- a/src/SFML/Window/Win32/Joystick.hpp
+++ b/src/SFML/Window/Win32/Joystick.hpp
@@ -58,12 +58,14 @@ public :
JoystickState UpdateState();
////////////////////////////////////////////////////////////
- /// Get the number of axes supported by the joystick
+ /// Check if the joystick supports the given axis
///
- /// \return Number of axis
+ /// \param Axis : Axis to check
+ ///
+ /// \return True of the axis is supported, false otherwise
///
////////////////////////////////////////////////////////////
- unsigned int GetAxesCount() const;
+ bool HasAxis(Joy::Axis Axis) const;
////////////////////////////////////////////////////////////
/// Get the number of buttons supported by the joystick
@@ -78,9 +80,11 @@ private :
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
- unsigned int myIndex; ///< Windows ID of the joystick
- unsigned int myNbAxes; ///< Number of axis supported by the joystick
- unsigned int myNbButtons; ///< Number of buttons supported by the joystick
+ bool myIsConnected; ///< Is there a joystick connected?
+ unsigned int myIndex; ///< Windows ID of the joystick
+ unsigned int myNbButtons; ///< Number of buttons supported by the joystick
+ bool myAxes[Joy::AxisCount]; ///< Supported axes
+ bool myHasContinuousPOV; ///< True if the driver supports continuous values for the POV
};
} // namespace priv
diff --git a/src/SFML/Window/Win32/WindowImplWin32.cpp b/src/SFML/Window/Win32/WindowImplWin32.cpp
index 9148774..506cb73 100755
--- a/src/SFML/Window/Win32/WindowImplWin32.cpp
+++ b/src/SFML/Window/Win32/WindowImplWin32.cpp
@@ -36,13 +36,16 @@
#include <iostream>
#include <vector>
-// Old versions of MinGW lack the definition of XBUTTON1 and XBUTTON2
+// MinGW lacks the definition of some Win32 constants
#ifndef XBUTTON1
#define XBUTTON1 0x0001
#endif
#ifndef XBUTTON2
#define XBUTTON2 0x0002
#endif
+#ifndef MAPVK_VK_TO_VSC
+ #define MAPVK_VK_TO_VSC (0)
+#endif
namespace sf
@@ -150,10 +153,12 @@ myIsCursorIn (false)
RegisterWindowClass();
// Compute position and size
- int Left = (GetDeviceCaps(GetDC(NULL), HORZRES) - Mode.Width) / 2;
- int Top = (GetDeviceCaps(GetDC(NULL), VERTRES) - Mode.Height) / 2;
+ HDC ScreenDC = GetDC(NULL);
+ int Left = (GetDeviceCaps(ScreenDC, HORZRES) - Mode.Width) / 2;
+ int Top = (GetDeviceCaps(ScreenDC, VERTRES) - Mode.Height) / 2;
int Width = myWidth = Mode.Width;
int Height = myHeight = Mode.Height;
+ ReleaseDC(NULL, ScreenDC);
// Choose the window style according to the Style parameter
DWORD Win32Style = WS_VISIBLE;
@@ -268,7 +273,7 @@ void WindowImplWin32::ProcessEvents()
if (!myCallback)
{
MSG Message;
- while (PeekMessage(&Message, myHandle, 0, 0, PM_REMOVE))
+ while (PeekMessage(&Message, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&Message);
DispatchMessage(&Message);
@@ -476,20 +481,16 @@ void WindowImplWin32::SwitchToFullscreen(const VideoMode& Mode)
return;
}
- // Change window style (no border, no titlebar, ...)
- SetWindowLong(myHandle, GWL_STYLE, WS_POPUP);
+ // Make the window flags compatible with fullscreen mode
+ SetWindowLong(myHandle, GWL_STYLE, WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
SetWindowLong(myHandle, GWL_EXSTYLE, WS_EX_APPWINDOW);
- // And resize it so that it fits the entire screen
+ // Resize the window so that it fits the entire screen
SetWindowPos(myHandle, HWND_TOP, 0, 0, Mode.Width, Mode.Height, SWP_FRAMECHANGED);
ShowWindow(myHandle, SW_SHOW);
// Set "this" as the current fullscreen window
ourFullscreenWindow = this;
-
- // SetPixelFormat can fail (really ?) if window style doesn't contain these flags
- long Style = GetWindowLong(myHandle, GWL_STYLE);
- SetWindowLong(myHandle, GWL_STYLE, Style | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
}
@@ -512,68 +513,76 @@ void WindowImplWin32::CreateContext(const VideoMode& Mode, WindowSettings& Param
{
// Get the wglChoosePixelFormatARB function (it is an extension)
PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = reinterpret_cast<PFNWGLCHOOSEPIXELFORMATARBPROC>(wglGetProcAddress("wglChoosePixelFormatARB"));
-
- // Define the basic attributes we want for our window
- int IntAttributes[] =
- {
- WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
- WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
- WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
- WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
- WGL_SAMPLE_BUFFERS_ARB, (Params.AntialiasingLevel ? GL_TRUE : GL_FALSE),
- WGL_SAMPLES_ARB, Params.AntialiasingLevel,
- 0, 0
- };
-
- // Let's check how many formats are supporting our requirements
- int Formats[128];
- UINT NbFormats;
- float FloatAttributes[] = {0, 0};
- bool IsValid = wglChoosePixelFormatARB(myDeviceContext, IntAttributes, FloatAttributes, sizeof(Formats) / sizeof(*Formats), Formats, &NbFormats) != 0;
- if (!IsValid || (NbFormats == 0))
+ if (wglChoosePixelFormatARB)
{
- if (Params.AntialiasingLevel > 2)
+ // Define the basic attributes we want for our window
+ int IntAttributes[] =
{
- // No format matching our needs : reduce the multisampling level
- std::cerr << "Failed to find a pixel format supporting "
- << Params.AntialiasingLevel << " antialiasing levels ; trying with 2 levels" << std::endl;
-
- Params.AntialiasingLevel = IntAttributes[1] = 2;
- IsValid = wglChoosePixelFormatARB(myDeviceContext, IntAttributes, FloatAttributes, sizeof(Formats) / sizeof(*Formats), Formats, &NbFormats) != 0;
- }
-
+ WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
+ WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
+ WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
+ WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
+ WGL_SAMPLE_BUFFERS_ARB, (Params.AntialiasingLevel ? GL_TRUE : GL_FALSE),
+ WGL_SAMPLES_ARB, Params.AntialiasingLevel,
+ 0, 0
+ };
+
+ // Let's check how many formats are supporting our requirements
+ int Formats[128];
+ UINT NbFormats;
+ float FloatAttributes[] = {0, 0};
+ bool IsValid = wglChoosePixelFormatARB(myDeviceContext, IntAttributes, FloatAttributes, sizeof(Formats) / sizeof(*Formats), Formats, &NbFormats) != 0;
if (!IsValid || (NbFormats == 0))
{
- // Cannot find any pixel format supporting multisampling ; disabling antialiasing
- std::cerr << "Failed to find a pixel format supporting antialiasing ; antialiasing will be disabled" << std::endl;
- Params.AntialiasingLevel = 0;
+ if (Params.AntialiasingLevel > 2)
+ {
+ // No format matching our needs : reduce the multisampling level
+ std::cerr << "Failed to find a pixel format supporting "
+ << Params.AntialiasingLevel << " antialiasing levels ; trying with 2 levels" << std::endl;
+
+ Params.AntialiasingLevel = IntAttributes[11] = 2;
+ IsValid = wglChoosePixelFormatARB(myDeviceContext, IntAttributes, FloatAttributes, sizeof(Formats) / sizeof(*Formats), Formats, &NbFormats) != 0;
+ }
+
+ if (!IsValid || (NbFormats == 0))
+ {
+ // Cannot find any pixel format supporting multisampling ; disabling antialiasing
+ std::cerr << "Failed to find a pixel format supporting antialiasing ; antialiasing will be disabled" << std::endl;
+ Params.AntialiasingLevel = 0;
+ }
}
- }
- // Get the best format among the returned ones
- if (IsValid && (NbFormats > 0))
- {
- int BestScore = 0xFFFF;
- for (UINT i = 0; i < NbFormats; ++i)
+ // Get the best format among the returned ones
+ if (IsValid && (NbFormats > 0))
{
- // Get the current format's attributes
- PIXELFORMATDESCRIPTOR Attribs;
- Attribs.nSize = sizeof(PIXELFORMATDESCRIPTOR);
- Attribs.nVersion = 1;
- DescribePixelFormat(myDeviceContext, Formats[i], sizeof(PIXELFORMATDESCRIPTOR), &Attribs);
-
- // Evaluate the current configuration
- int Color = Attribs.cRedBits + Attribs.cGreenBits + Attribs.cBlueBits + Attribs.cAlphaBits;
- int Score = EvaluateConfig(Mode, Params, Color, Attribs.cDepthBits, Attribs.cStencilBits, Params.AntialiasingLevel);
-
- // Keep it if it's better than the current best
- if (Score < BestScore)
+ int BestScore = 0xFFFF;
+ for (UINT i = 0; i < NbFormats; ++i)
{
- BestScore = Score;
- BestFormat = Formats[i];
+ // Get the current format's attributes
+ PIXELFORMATDESCRIPTOR Attribs;
+ Attribs.nSize = sizeof(PIXELFORMATDESCRIPTOR);
+ Attribs.nVersion = 1;
+ DescribePixelFormat(myDeviceContext, Formats[i], sizeof(PIXELFORMATDESCRIPTOR), &Attribs);
+
+ // Evaluate the current configuration
+ int Color = Attribs.cRedBits + Attribs.cGreenBits + Attribs.cBlueBits + Attribs.cAlphaBits;
+ int Score = EvaluateConfig(Mode, Params, Color, Attribs.cDepthBits, Attribs.cStencilBits, Params.AntialiasingLevel);
+
+ // Keep it if it's better than the current best
+ if (Score < BestScore)
+ {
+ BestScore = Score;
+ BestFormat = Formats[i];
+ }
}
}
}
+ else
+ {
+ // wglChoosePixelFormatARB not supported ; disabling antialiasing
+ std::cerr << "Antialiasing is not supported ; it will be disabled" << std::endl;
+ Params.AntialiasingLevel = 0;
+ }
}
// Find a pixel format with no antialiasing, if not needed or not supported
@@ -590,6 +599,7 @@ void WindowImplWin32::CreateContext(const VideoMode& Mode, WindowSettings& Param
PixelDescriptor.cColorBits = static_cast<BYTE>(Mode.BitsPerPixel);
PixelDescriptor.cDepthBits = static_cast<BYTE>(Params.DepthBits);
PixelDescriptor.cStencilBits = static_cast<BYTE>(Params.StencilBits);
+ PixelDescriptor.cAlphaBits = Mode.BitsPerPixel == 32 ? 8 : 0;
// Get the pixel format that best matches our requirements
BestFormat = ChoosePixelFormat(myDeviceContext, &PixelDescriptor);
@@ -745,10 +755,13 @@ void WindowImplWin32::ProcessEvent(UINT Message, WPARAM WParam, LPARAM LParam)
// Text event
case WM_CHAR :
{
- Event Evt;
- Evt.Type = Event::TextEntered;
- Evt.Text.Unicode = static_cast<Uint32>(WParam);
- SendEvent(Evt);
+ if (myKeyRepeatEnabled || ((LParam & (1 << 30)) == 0))
+ {
+ Event Evt;
+ Evt.Type = Event::TextEntered;
+ Evt.Text.Unicode = static_cast<Uint32>(WParam);
+ SendEvent(Evt);
+ }
break;
}
@@ -756,26 +769,15 @@ void WindowImplWin32::ProcessEvent(UINT Message, WPARAM WParam, LPARAM LParam)
case WM_KEYDOWN :
case WM_SYSKEYDOWN :
{
- if (myKeyRepeatEnabled || ((LParam & (1 << 30)) == 0))
+ if (myKeyRepeatEnabled || ((HIWORD(LParam) & KF_REPEAT) == 0))
{
Event Evt;
Evt.Type = Event::KeyPressed;
Evt.Key.Alt = HIWORD(GetAsyncKeyState(VK_MENU)) != 0;
Evt.Key.Control = HIWORD(GetAsyncKeyState(VK_CONTROL)) != 0;
Evt.Key.Shift = HIWORD(GetAsyncKeyState(VK_SHIFT)) != 0;
-
- if (WParam != VK_SHIFT)
- {
- Evt.Key.Code = VirtualKeyCodeToSF(WParam, LParam);
- SendEvent(Evt);
- }
- else
- {
- // Special case for shift, its state can't be retrieved directly
- Evt.Key.Code = GetShiftState(true);
- if (Evt.Key.Code != 0)
- SendEvent(Evt);
- }
+ Evt.Key.Code = VirtualKeyCodeToSF(WParam, LParam);
+ SendEvent(Evt);
}
break;
}
@@ -789,19 +791,8 @@ void WindowImplWin32::ProcessEvent(UINT Message, WPARAM WParam, LPARAM LParam)
Evt.Key.Alt = HIWORD(GetAsyncKeyState(VK_MENU)) != 0;
Evt.Key.Control = HIWORD(GetAsyncKeyState(VK_CONTROL)) != 0;
Evt.Key.Shift = HIWORD(GetAsyncKeyState(VK_SHIFT)) != 0;
-
- if (WParam != VK_SHIFT)
- {
- Evt.Key.Code = VirtualKeyCodeToSF(WParam, LParam);
- SendEvent(Evt);
- }
- else
- {
- // Special case for shift, its state can't be retrieved directly
- Evt.Key.Code = GetShiftState(false);
- if (Evt.Key.Code != 0)
- SendEvent(Evt);
- }
+ Evt.Key.Code = VirtualKeyCodeToSF(WParam, LParam);
+ SendEvent(Evt);
break;
}
@@ -954,46 +945,27 @@ void WindowImplWin32::ProcessEvent(UINT Message, WPARAM WParam, LPARAM LParam)
////////////////////////////////////////////////////////////
-/// Check the state of the shift keys on a key event,
-/// and return the corresponding SF key code
-////////////////////////////////////////////////////////////
-Key::Code WindowImplWin32::GetShiftState(bool KeyDown)
-{
- static bool LShiftPrevDown = false;
- static bool RShiftPrevDown = false;
-
- bool LShiftDown = (HIWORD(GetAsyncKeyState(VK_LSHIFT)) != 0);
- bool RShiftDown = (HIWORD(GetAsyncKeyState(VK_RSHIFT)) != 0);
-
- Key::Code Code = Key::Code(0);
- if (KeyDown)
- {
- if (!LShiftPrevDown && LShiftDown) Code = Key::LShift;
- else if (!RShiftPrevDown && RShiftDown) Code = Key::RShift;
- }
- else
- {
- if (LShiftPrevDown && !LShiftDown) Code = Key::LShift;
- else if (RShiftPrevDown && !RShiftDown) Code = Key::RShift;
- }
-
- LShiftPrevDown = LShiftDown;
- RShiftPrevDown = RShiftDown;
-
- return Code;
-}
-
-
-////////////////////////////////////////////////////////////
/// Convert a Win32 virtual key code to a SFML key code
////////////////////////////////////////////////////////////
Key::Code WindowImplWin32::VirtualKeyCodeToSF(WPARAM VirtualKey, LPARAM Flags)
{
switch (VirtualKey)
{
- // VK_SHIFT is handled by the GetShiftState function
- case VK_MENU : return (Flags & (1 << 24)) ? Key::RAlt : Key::LAlt;
- case VK_CONTROL : return (Flags & (1 << 24)) ? Key::RControl : Key::LControl;
+ // Check the scancode to distinguish between left and right shift
+ case VK_SHIFT :
+ {
+ static UINT LShift = MapVirtualKey(VK_LSHIFT, MAPVK_VK_TO_VSC);
+ UINT scancode = (Flags & (0xFF << 16)) >> 16;
+ return scancode == LShift ? Key::LShift : Key::RShift;
+ }
+
+ // Check the "extended" flag to distinguish between left and right alt
+ case VK_MENU : return (HIWORD(Flags) & KF_EXTENDED) ? Key::RAlt : Key::LAlt;
+
+ // Check the "extended" flag to distinguish between left and right control
+ case VK_CONTROL : return (HIWORD(Flags) & KF_EXTENDED) ? Key::RControl : Key::LControl;
+
+ // Other keys are reported properly
case VK_LWIN : return Key::LSystem;
case VK_RWIN : return Key::RSystem;
case VK_APPS : return Key::Menu;
diff --git a/src/SFML/Window/Win32/WindowImplWin32.hpp b/src/SFML/Window/Win32/WindowImplWin32.hpp
index 75c1c59..20557dc 100755
--- a/src/SFML/Window/Win32/WindowImplWin32.hpp
+++ b/src/SFML/Window/Win32/WindowImplWin32.hpp
@@ -194,17 +194,6 @@ private :
void ProcessEvent(UINT Message, WPARAM WParam, LPARAM LParam);
////////////////////////////////////////////////////////////
- /// Check the state of the shift keys on a key event,
- /// and return the corresponding SF key code
- ///
- /// \param KeyDown : True for a keydown event, false for a keyup event
- ///
- /// \return SFML key code corresponding to the shift key
- ///
- ////////////////////////////////////////////////////////////
- static Key::Code GetShiftState(bool KeyDown);
-
- ////////////////////////////////////////////////////////////
/// Convert a Win32 virtual key code to a SFML key code
///
/// \param VirtualKey : Virtual key code to convert
diff --git a/src/SFML/Window/Window.cpp b/src/SFML/Window/Window.cpp
index cfaae84..7ea1963 100755
--- a/src/SFML/Window/Window.cpp
+++ b/src/SFML/Window/Window.cpp
@@ -445,6 +445,10 @@ void Window::Initialize(priv::WindowImpl* Window)
myWindow = Window;
myWindow->Initialize();
+ // Clear the event queue
+ while (!myEvents.empty())
+ myEvents.pop();
+
// Listen to events from the new window
myWindow->AddListener(this);
myWindow->AddListener(&myInput);
diff --git a/src/SFML/Window/WindowImpl.cpp b/src/SFML/Window/WindowImpl.cpp
index bc60543..dd0584f 100755
--- a/src/SFML/Window/WindowImpl.cpp
+++ b/src/SFML/Window/WindowImpl.cpp
@@ -125,7 +125,7 @@ void WindowImpl::RemoveListener(WindowListener* Listener)
void WindowImpl::Initialize()
{
// Initialize the joysticks
- for (unsigned int i = 0; i < JoysticksCount; ++i)
+ for (unsigned int i = 0; i < Joy::Count; ++i)
{
myJoysticks[i].Initialize(i);
myJoyStates[i] = myJoysticks[i].UpdateState();
@@ -214,25 +214,29 @@ int WindowImpl::EvaluateConfig(const VideoMode& Mode, const WindowSettings& Sett
////////////////////////////////////////////////////////////
void WindowImpl::ProcessJoystickEvents()
{
- for (unsigned int i = 0; i < JoysticksCount; ++i)
+ for (unsigned int i = 0; i < Joy::Count; ++i)
{
// Copy the previous state of the joystick and get the new one
JoystickState PreviousState = myJoyStates[i];
myJoyStates[i] = myJoysticks[i].UpdateState();
// Axis
- for (unsigned int j = 0; j < myJoysticks[i].GetAxesCount(); ++j)
+ for (unsigned int j = 0; j < Joy::AxisCount; ++j)
{
- float PrevPos = PreviousState.Axis[j];
- float CurrPos = myJoyStates[i].Axis[j];
- if (fabs(CurrPos - PrevPos) >= myJoyThreshold)
+ Joy::Axis Axis = static_cast<Joy::Axis>(j);
+ if (myJoysticks[i].HasAxis(Axis))
{
- Event Event;
- Event.Type = Event::JoyMoved;
- Event.JoyMove.JoystickId = i;
- Event.JoyMove.Axis = static_cast<Joy::Axis>(j);
- Event.JoyMove.Position = CurrPos;
- SendEvent(Event);
+ float PrevPos = PreviousState.Axis[j];
+ float CurrPos = myJoyStates[i].Axis[j];
+ if (fabs(CurrPos - PrevPos) >= myJoyThreshold)
+ {
+ Event Event;
+ Event.Type = Event::JoyMoved;
+ Event.JoyMove.JoystickId = i;
+ Event.JoyMove.Axis = Axis;
+ Event.JoyMove.Position = CurrPos;
+ SendEvent(Event);
+ }
}
}
diff --git a/src/SFML/Window/WindowImpl.hpp b/src/SFML/Window/WindowImpl.hpp
index 15d29ec..09f8b54 100755
--- a/src/SFML/Window/WindowImpl.hpp
+++ b/src/SFML/Window/WindowImpl.hpp
@@ -291,17 +291,12 @@ private :
virtual void ProcessEvents() = 0;
////////////////////////////////////////////////////////////
- // Total number of joysticks supported
- ////////////////////////////////////////////////////////////
- enum {JoysticksCount = 2};
-
- ////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
- std::set<WindowListener*> myListeners; ///< Array of listeners connected to the window
- Joystick myJoysticks[JoysticksCount]; ///< Joysticks to observe
- JoystickState myJoyStates[JoysticksCount]; ///< Current states of the joysticks
- float myJoyThreshold; ///< Joystick threshold (minimum motion for MOVE event to be generated)
+ std::set<WindowListener*> myListeners; ///< Array of listeners connected to the window
+ Joystick myJoysticks[Joy::Count]; ///< Joysticks to observe
+ JoystickState myJoyStates[Joy::Count]; ///< Current states of the joysticks
+ float myJoyThreshold; ///< Joystick threshold (minimum motion for MOVE event to be generated)
};
} // namespace priv