From ae764cf2be344ffc8bcec9276ea9af0e1cdbb2ec Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 30 Jul 2018 21:13:01 +1000 Subject: [PATCH] more WIP with vorbis --- .../2D/Screens/Menu/HacksSettingsScreen.cs | 308 ++++---- .../Entities/Components/PhysicsComponent.cs | 9 +- .../Entities/Components/ShadowComponent.cs | 11 +- ClassicalSharp/Entities/LocalPlayer.cs | 13 +- ClassicalSharp/Entities/Model/BlockModel.cs | 15 +- ClassicalSharp/Entities/Model/HumanModels.cs | 9 +- .../Entities/Model/HumanoidModel.cs | 9 +- ClassicalSharp/Network/IServerConnection.cs | 47 +- ClassicalSharp/Network/NetworkProcessor.cs | 20 +- ClassicalSharp/Utils/Camera.cs | 9 +- src/Client/Audio.c | 691 +++++++++++------- src/Client/Audio.h | 5 +- src/Client/D3D9Api.c | 18 +- src/Client/Formats.c | 4 +- src/Client/Funcs.h | 2 +- src/Client/Program.c | 1 + src/Client/ServerConnection.h | 7 +- 17 files changed, 670 insertions(+), 508 deletions(-) diff --git a/ClassicalSharp/2D/Screens/Menu/HacksSettingsScreen.cs b/ClassicalSharp/2D/Screens/Menu/HacksSettingsScreen.cs index dc7ab306a..92012d593 100644 --- a/ClassicalSharp/2D/Screens/Menu/HacksSettingsScreen.cs +++ b/ClassicalSharp/2D/Screens/Menu/HacksSettingsScreen.cs @@ -1,155 +1,155 @@ -// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 -using System; -using ClassicalSharp.Entities; -using ClassicalSharp.Gui.Widgets; - -namespace ClassicalSharp.Gui.Screens { - public class HacksSettingsScreen : MenuOptionsScreen { - - public HacksSettingsScreen(Game game) : base(game) { - } - - public override void Init() { - base.Init(); - game.Events.HackPermissionsChanged += CheckHacksAllowed; - validators = new MenuInputValidator[widgets.Length]; - defaultValues = new string[widgets.Length]; - - validators[1] = new RealValidator(0.1f, 50); - defaultValues[1] = "10"; - validators[3] = new RealValidator(0.1f, 2048f); - defaultValues[3] = (1.233f).ToString(); - validators[9] = new IntegerValidator(1, 150); - defaultValues[9] = "70"; - - MakeDescriptions(); - } - - public override void Dispose() { - base.Dispose(); - game.Events.HackPermissionsChanged -= CheckHacksAllowed; - } - - void CheckHacksAllowed(object sender, EventArgs e) { - for (int i = 0; i < widgets.Length; i++) { - if (widgets[i] == null) continue; - widgets[i].Disabled = false; - } - - LocalPlayer p = game.LocalPlayer; - bool noGlobalHacks = !p.Hacks.CanAnyHacks || !p.Hacks.Enabled; - widgets[3].Disabled = noGlobalHacks || !p.Hacks.CanSpeed; - widgets[4].Disabled = noGlobalHacks || !p.Hacks.CanSpeed; - widgets[5].Disabled = noGlobalHacks || !p.Hacks.CanSpeed; - widgets[7].Disabled = noGlobalHacks || !p.Hacks.CanPushbackBlocks; - } - - protected override void ContextRecreated() { - ClickHandler onClick = OnInputClick; - ClickHandler onBool = OnBoolClick; - - widgets = new Widget[] { - MakeOpt(-1, -150, "Hacks enabled", onBool, GetHacks, SetHacks), - MakeOpt(-1, -100, "Speed multiplier", onClick, GetSpeed, SetSpeed), - MakeOpt(-1, -50, "Camera clipping", onBool, GetClipping, SetClipping), - MakeOpt(-1, 0, "Jump height", onClick, GetJump, SetJump), - MakeOpt(-1, 50, "WOM style hacks", onBool, GetWOMHacks, SetWOMHacks), - - MakeOpt(1, -150, "Full block stepping", onBool, GetFullStep, SetFullStep), - MakeOpt(1, -100, "Modifiable liquids", onBool, GetLiquids, SetLiquids), - MakeOpt(1, -50, "Pushback placing", onBool, GetPushback, SetPushback), - MakeOpt(1, 0, "Noclip slide", onBool, GetSlide, SetSlide), - MakeOpt(1, 50, "Field of view", onClick, GetFOV, SetFOV), - - MakeBack(false, titleFont, SwitchOptions), - null, null, null, - }; - CheckHacksAllowed(null, null); - } - - static string GetHacks(Game g) { return GetBool(g.LocalPlayer.Hacks.Enabled); } - static void SetHacks(Game g, string v) { - g.LocalPlayer.Hacks.Enabled = SetBool(v, OptionsKey.HacksOn); - g.LocalPlayer.CheckHacksConsistency(); - } - - static string GetSpeed(Game g) { return g.LocalPlayer.Hacks.SpeedMultiplier.ToString("F2"); } - static void SetSpeed(Game g, string v) { - g.LocalPlayer.Hacks.SpeedMultiplier = Utils.ParseDecimal(v); - Options.Set(OptionsKey.Speed, v); - } - - static string GetClipping(Game g) { return GetBool(g.CameraClipping); } - static void SetClipping(Game g, string v) { - g.CameraClipping = SetBool(v, OptionsKey.CameraClipping); - } - - static string GetJump(Game g) { return g.LocalPlayer.JumpHeight.ToString("F3"); } - static void SetJump(Game g, string v) { - PhysicsComponent physics = g.LocalPlayer.physics; - physics.CalculateJumpVelocity(Utils.ParseDecimal(v)); - physics.userJumpVel = physics.jumpVel; - Options.Set(OptionsKey.JumpVelocity, physics.jumpVel.ToString()); - } - - static string GetWOMHacks(Game g) { return GetBool(g.LocalPlayer.Hacks.WOMStyleHacks); } - static void SetWOMHacks(Game g, string v) { - g.LocalPlayer.Hacks.WOMStyleHacks = SetBool(v, OptionsKey.WOMStyleHacks); - } - - static string GetFullStep(Game g) { return GetBool(g.LocalPlayer.Hacks.FullBlockStep); } - static void SetFullStep(Game g, string v) { - g.LocalPlayer.Hacks.FullBlockStep = SetBool(v, OptionsKey.FullBlockStep); - } - - static string GetPushback(Game g) { return GetBool(g.LocalPlayer.Hacks.PushbackPlacing); } - static void SetPushback(Game g, string v) { - g.LocalPlayer.Hacks.PushbackPlacing = SetBool(v, OptionsKey.PushbackPlacing); - } - - static string GetLiquids(Game g) { return GetBool(g.BreakableLiquids); } - static void SetLiquids(Game g, string v) { - g.BreakableLiquids = SetBool(v, OptionsKey.ModifiableLiquids); - } - - static string GetSlide(Game g) { return GetBool(g.LocalPlayer.Hacks.NoclipSlide); } - static void SetSlide(Game g, string v) { - g.LocalPlayer.Hacks.NoclipSlide = SetBool(v, OptionsKey.NoclipSlide); - } - - static string GetFOV(Game g) { return g.Fov.ToString(); } - static void SetFOV(Game g, string v) { - g.Fov = Int32.Parse(v); - if (g.ZoomFov > g.Fov) g.ZoomFov = g.Fov; - - Options.Set(OptionsKey.FieldOfView, v); - g.UpdateProjection(); - } - - void MakeDescriptions() { - string[][] descs = new string[widgets.Length][]; - descs[2] = new string[] { - "&eIf &fON&e, then the third person cameras will limit", - "ðeir zoom distance if they hit a solid block.", - }; - descs[3] = new string[] { - "&eSets how many blocks high you can jump up.", - "&eNote: You jump much higher when holding down the Speed key binding.", - }; - descs[6] = new string[] { - "&eIf &fON&e, then water/lava can be placed and", - "&edeleted the same way as any other block.", - }; - descs[7] = new string[] { - "&eIf &fON&e, placing blocks that intersect your own position cause", - "ðe block to be placed, and you to be moved out of the way.", - "&fThis is mainly useful for quick pillaring/towering.", - }; - descs[8] = new string[] { - "&eIf &fOFF&e, you will immediately stop when in noclip", - "&emode and no movement keys are held down.", - }; - descriptions = descs; - } - } +// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 +using System; +using ClassicalSharp.Entities; +using ClassicalSharp.Gui.Widgets; + +namespace ClassicalSharp.Gui.Screens { + public class HacksSettingsScreen : MenuOptionsScreen { + + public HacksSettingsScreen(Game game) : base(game) { + } + + public override void Init() { + base.Init(); + game.Events.HackPermissionsChanged += CheckHacksAllowed; + validators = new MenuInputValidator[widgets.Length]; + defaultValues = new string[widgets.Length]; + + validators[1] = new RealValidator(0.1f, 50); + defaultValues[1] = "10"; + validators[3] = new RealValidator(0.1f, 2048f); + defaultValues[3] = (1.233f).ToString(); + validators[9] = new IntegerValidator(1, 150); + defaultValues[9] = "70"; + + MakeDescriptions(); + } + + public override void Dispose() { + base.Dispose(); + game.Events.HackPermissionsChanged -= CheckHacksAllowed; + } + + void CheckHacksAllowed(object sender, EventArgs e) { + for (int i = 0; i < widgets.Length; i++) { + if (widgets[i] == null) continue; + widgets[i].Disabled = false; + } + + LocalPlayer p = game.LocalPlayer; + bool noGlobalHacks = !p.Hacks.CanAnyHacks || !p.Hacks.Enabled; + widgets[3].Disabled = noGlobalHacks || !p.Hacks.CanSpeed; + widgets[4].Disabled = noGlobalHacks || !p.Hacks.CanSpeed; + widgets[5].Disabled = noGlobalHacks || !p.Hacks.CanSpeed; + widgets[7].Disabled = noGlobalHacks || !p.Hacks.CanPushbackBlocks; + } + + protected override void ContextRecreated() { + ClickHandler onClick = OnInputClick; + ClickHandler onBool = OnBoolClick; + + widgets = new Widget[] { + MakeOpt(-1, -150, "Hacks enabled", onBool, GetHacks, SetHacks), + MakeOpt(-1, -100, "Speed multiplier", onClick, GetSpeed, SetSpeed), + MakeOpt(-1, -50, "Camera clipping", onBool, GetClipping, SetClipping), + MakeOpt(-1, 0, "Jump height", onClick, GetJump, SetJump), + MakeOpt(-1, 50, "WOM style hacks", onBool, GetWOMHacks, SetWOMHacks), + + MakeOpt(1, -150, "Full block stepping", onBool, GetFullStep, SetFullStep), + MakeOpt(1, -100, "Modifiable liquids", onBool, GetLiquids, SetLiquids), + MakeOpt(1, -50, "Pushback placing", onBool, GetPushback, SetPushback), + MakeOpt(1, 0, "Noclip slide", onBool, GetSlide, SetSlide), + MakeOpt(1, 50, "Field of view", onClick, GetFOV, SetFOV), + + MakeBack(false, titleFont, SwitchOptions), + null, null, null, + }; + CheckHacksAllowed(null, null); + } + + static string GetHacks(Game g) { return GetBool(g.LocalPlayer.Hacks.Enabled); } + static void SetHacks(Game g, string v) { + g.LocalPlayer.Hacks.Enabled = SetBool(v, OptionsKey.HacksOn); + g.LocalPlayer.CheckHacksConsistency(); + } + + static string GetSpeed(Game g) { return g.LocalPlayer.Hacks.SpeedMultiplier.ToString("F2"); } + static void SetSpeed(Game g, string v) { + g.LocalPlayer.Hacks.SpeedMultiplier = Utils.ParseDecimal(v); + Options.Set(OptionsKey.Speed, v); + } + + static string GetClipping(Game g) { return GetBool(g.CameraClipping); } + static void SetClipping(Game g, string v) { + g.CameraClipping = SetBool(v, OptionsKey.CameraClipping); + } + + static string GetJump(Game g) { return g.LocalPlayer.JumpHeight.ToString("F3"); } + static void SetJump(Game g, string v) { + PhysicsComponent physics = g.LocalPlayer.physics; + physics.CalculateJumpVelocity(Utils.ParseDecimal(v)); + physics.userJumpVel = physics.jumpVel; + Options.Set(OptionsKey.JumpVelocity, physics.jumpVel.ToString()); + } + + static string GetWOMHacks(Game g) { return GetBool(g.LocalPlayer.Hacks.WOMStyleHacks); } + static void SetWOMHacks(Game g, string v) { + g.LocalPlayer.Hacks.WOMStyleHacks = SetBool(v, OptionsKey.WOMStyleHacks); + } + + static string GetFullStep(Game g) { return GetBool(g.LocalPlayer.Hacks.FullBlockStep); } + static void SetFullStep(Game g, string v) { + g.LocalPlayer.Hacks.FullBlockStep = SetBool(v, OptionsKey.FullBlockStep); + } + + static string GetPushback(Game g) { return GetBool(g.LocalPlayer.Hacks.PushbackPlacing); } + static void SetPushback(Game g, string v) { + g.LocalPlayer.Hacks.PushbackPlacing = SetBool(v, OptionsKey.PushbackPlacing); + } + + static string GetLiquids(Game g) { return GetBool(g.BreakableLiquids); } + static void SetLiquids(Game g, string v) { + g.BreakableLiquids = SetBool(v, OptionsKey.ModifiableLiquids); + } + + static string GetSlide(Game g) { return GetBool(g.LocalPlayer.Hacks.NoclipSlide); } + static void SetSlide(Game g, string v) { + g.LocalPlayer.Hacks.NoclipSlide = SetBool(v, OptionsKey.NoclipSlide); + } + + static string GetFOV(Game g) { return g.Fov.ToString(); } + static void SetFOV(Game g, string v) { + g.Fov = Int32.Parse(v); + if (g.ZoomFov > g.Fov) g.ZoomFov = g.Fov; + + Options.Set(OptionsKey.FieldOfView, v); + g.UpdateProjection(); + } + + void MakeDescriptions() { + string[][] descs = new string[widgets.Length][]; + descs[2] = new string[] { + "&eIf &fON&e, then the third person cameras will limit", + "ðeir zoom distance if they hit a solid block.", + }; + descs[3] = new string[] { + "&eSets how many blocks high you can jump up.", + "&eNote: You jump much higher when holding down the Speed key binding.", + }; + descs[6] = new string[] { + "&eIf &fON&e, then water/lava can be placed and", + "&edeleted the same way as any other block.", + }; + descs[7] = new string[] { + "&eIf &fON&e, placing blocks that intersect your own position cause", + "ðe block to be placed, and you to be moved out of the way.", + "&fThis is mainly useful for quick pillaring/towering.", + }; + descs[8] = new string[] { + "&eIf &fOFF&e, you will immediately stop when in noclip", + "&emode and no movement keys are held down.", + }; + descriptions = descs; + } + } } \ No newline at end of file diff --git a/ClassicalSharp/Entities/Components/PhysicsComponent.cs b/ClassicalSharp/Entities/Components/PhysicsComponent.cs index 4c3eaca2f..6adc55e44 100644 --- a/ClassicalSharp/Entities/Components/PhysicsComponent.cs +++ b/ClassicalSharp/Entities/Components/PhysicsComponent.cs @@ -1,9 +1,8 @@ // Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 -using System; -using ClassicalSharp.Model; -using ClassicalSharp.Physics; -using OpenTK; -using BlockID = System.UInt16; +using System; +using ClassicalSharp.Physics; +using OpenTK; +using BlockID = System.UInt16; namespace ClassicalSharp.Entities { diff --git a/ClassicalSharp/Entities/Components/ShadowComponent.cs b/ClassicalSharp/Entities/Components/ShadowComponent.cs index 356e3bd86..f75605bc4 100644 --- a/ClassicalSharp/Entities/Components/ShadowComponent.cs +++ b/ClassicalSharp/Entities/Components/ShadowComponent.cs @@ -1,11 +1,8 @@ // Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 -using System; -using System.Drawing; -using ClassicalSharp.GraphicsAPI; -using OpenTK; -#if ANDROID -using Android.Graphics; -#endif +using System; +using System.Drawing; +using ClassicalSharp.GraphicsAPI; +using OpenTK; using BlockID = System.UInt16; namespace ClassicalSharp.Entities { diff --git a/ClassicalSharp/Entities/LocalPlayer.cs b/ClassicalSharp/Entities/LocalPlayer.cs index 700f9ddc1..c72012daf 100644 --- a/ClassicalSharp/Entities/LocalPlayer.cs +++ b/ClassicalSharp/Entities/LocalPlayer.cs @@ -1,11 +1,10 @@ // Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 -using System; -using System.Drawing; -using ClassicalSharp.Physics; -using ClassicalSharp.Renderers; -using OpenTK; -using OpenTK.Input; -using BlockID = System.UInt16; +using System; +using System.Drawing; +using ClassicalSharp.Physics; +using OpenTK; +using OpenTK.Input; +using BlockID = System.UInt16; namespace ClassicalSharp.Entities { diff --git a/ClassicalSharp/Entities/Model/BlockModel.cs b/ClassicalSharp/Entities/Model/BlockModel.cs index 5a08dc322..539578c75 100644 --- a/ClassicalSharp/Entities/Model/BlockModel.cs +++ b/ClassicalSharp/Entities/Model/BlockModel.cs @@ -1,12 +1,11 @@ // Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 -using System; -using ClassicalSharp.Entities; -using ClassicalSharp.GraphicsAPI; -using ClassicalSharp.Physics; -using ClassicalSharp.Renderers; -using ClassicalSharp.Textures; -using OpenTK; -using BlockID = System.UInt16; +using System; +using ClassicalSharp.Entities; +using ClassicalSharp.GraphicsAPI; +using ClassicalSharp.Physics; +using ClassicalSharp.Textures; +using OpenTK; +using BlockID = System.UInt16; namespace ClassicalSharp.Model { diff --git a/ClassicalSharp/Entities/Model/HumanModels.cs b/ClassicalSharp/Entities/Model/HumanModels.cs index aec339a8e..440531593 100644 --- a/ClassicalSharp/Entities/Model/HumanModels.cs +++ b/ClassicalSharp/Entities/Model/HumanModels.cs @@ -1,9 +1,8 @@ // Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 -using System; -using ClassicalSharp.Entities; -using ClassicalSharp.GraphicsAPI; -using ClassicalSharp.Physics; -using OpenTK; +using System; +using ClassicalSharp.Entities; +using ClassicalSharp.Physics; +using OpenTK; namespace ClassicalSharp.Model { diff --git a/ClassicalSharp/Entities/Model/HumanoidModel.cs b/ClassicalSharp/Entities/Model/HumanoidModel.cs index 03da04dbb..e8f8a05f7 100644 --- a/ClassicalSharp/Entities/Model/HumanoidModel.cs +++ b/ClassicalSharp/Entities/Model/HumanoidModel.cs @@ -1,9 +1,8 @@ // Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 -using System; -using ClassicalSharp.Entities; -using ClassicalSharp.GraphicsAPI; -using ClassicalSharp.Physics; -using OpenTK; +using System; +using ClassicalSharp.Entities; +using ClassicalSharp.Physics; +using OpenTK; namespace ClassicalSharp.Model { diff --git a/ClassicalSharp/Network/IServerConnection.cs b/ClassicalSharp/Network/IServerConnection.cs index 39800fcee..642d43b42 100644 --- a/ClassicalSharp/Network/IServerConnection.cs +++ b/ClassicalSharp/Network/IServerConnection.cs @@ -1,18 +1,11 @@ // Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 -using System; -using System.Drawing; -using System.IO; -using System.Net; -using ClassicalSharp.Entities; -using ClassicalSharp.Generator; -using ClassicalSharp.Gui.Screens; -using ClassicalSharp.Network; -using ClassicalSharp.Textures; -using OpenTK; +using System; +using System.Net; +using ClassicalSharp.Gui.Screens; +using ClassicalSharp.Network; +using ClassicalSharp.Textures; +using OpenTK; using OpenTK.Input; -#if ANDROID -using Android.Graphics; -#endif namespace ClassicalSharp { @@ -20,21 +13,14 @@ namespace ClassicalSharp { public abstract class IServerConnection : IGameComponent { public bool IsSinglePlayer; - - /// Opens a connection to the server, and prepares the initial state of the client. + public bool Disconnected; public abstract void BeginConnect(); - - public abstract void SendChat(string text); - - /// Informs the server of the client's current position and orientation. - public abstract void SendPosition(Vector3 pos, float rotY, float headX); - - /// Informs the server that using the given mouse button, - /// the client clicked on a particular block or entity. - public abstract void SendPlayerClick(MouseButton button, bool buttonDown, byte targetId, PickedPos pos); - public abstract void Tick(ScheduledTask task); + public abstract void SendChat(string text); + public abstract void SendPosition(Vector3 pos, float rotY, float headX); + public abstract void SendPlayerClick(MouseButton button, bool buttonDown, byte targetId, PickedPos pos); + public virtual void Init(Game game) { } public virtual void Ready(Game game) { } public virtual void OnNewMapLoaded(Game game) { } @@ -47,22 +33,11 @@ namespace ClassicalSharp { public string ServerMotd; public string AppName = Program.AppName; - /// Whether the network processor is currently connected to a server. - public bool Disconnected; - - /// Whether the client should use extended player list management, with group names and ranks. public bool UsingExtPlayerList; - - /// Whether the server supports handling PlayerClick packets from the client. public bool UsingPlayerClick; - - /// Whether the server can handle partial message packets or not. public bool SupportsPartialMessages; - - /// Whether the server supports receiving all code page 437 characters from this client. public bool SupportsFullCP437; - protected Game game; protected int netTicks; diff --git a/ClassicalSharp/Network/NetworkProcessor.cs b/ClassicalSharp/Network/NetworkProcessor.cs index acf220238..da1f65e18 100644 --- a/ClassicalSharp/Network/NetworkProcessor.cs +++ b/ClassicalSharp/Network/NetworkProcessor.cs @@ -1,18 +1,12 @@ // Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 -using System; -using System.Drawing; -using System.IO; -using System.Net; -using System.Net.Sockets; -using ClassicalSharp.Entities; -using ClassicalSharp.Events; -using ClassicalSharp.Gui; -using ClassicalSharp.Network; -using ClassicalSharp.Textures; -using ClassicalSharp.Network.Protocols; +using System; +using System.Net.Sockets; +using ClassicalSharp.Entities; +using ClassicalSharp.Events; +using ClassicalSharp.Network.Protocols; +using OpenTK; +using OpenTK.Input; using BlockID = System.UInt16; -using OpenTK; -using OpenTK.Input; namespace ClassicalSharp.Network { diff --git a/ClassicalSharp/Utils/Camera.cs b/ClassicalSharp/Utils/Camera.cs index 6eddf08fb..e20299ece 100644 --- a/ClassicalSharp/Utils/Camera.cs +++ b/ClassicalSharp/Utils/Camera.cs @@ -1,9 +1,8 @@ // Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 -using System; -using System.Drawing; -using ClassicalSharp.Entities; -using OpenTK; -using OpenTK.Input; +using System; +using System.Drawing; +using ClassicalSharp.Entities; +using OpenTK; namespace ClassicalSharp { diff --git a/src/Client/Audio.c b/src/Client/Audio.c index 1f5415f82..ae48dd548 100644 --- a/src/Client/Audio.c +++ b/src/Client/Audio.c @@ -75,35 +75,36 @@ void Ogg_MakeStream(struct Stream* stream, UInt8* buffer, struct Stream* source) *------------------------------------------------------Vorbis utils-------------------------------------------------------* *#########################################################################################################################*/ /* Insert next byte into the bit buffer */ -#define Vorbis_GetByte(state) state->Bits |= (UInt32)Stream_ReadU8(state->Source) << state->NumBits; state->NumBits += 8; +#define Vorbis_GetByte(ctx) ctx->Bits |= (UInt32)Stream_ReadU8(ctx->Source) << ctx->NumBits; ctx->NumBits += 8; /* Retrieves bits from the bit buffer */ -#define Vorbis_PeekBits(state, bits) (state->Bits & ((1UL << (bits)) - 1UL)) +#define Vorbis_PeekBits(ctx, bits) (ctx->Bits & ((1UL << (bits)) - 1UL)) /* Consumes/eats up bits from the bit buffer */ -#define Vorbis_ConsumeBits(state, bits) state->Bits >>= (bits); state->NumBits -= (bits); +#define Vorbis_ConsumeBits(ctx, bits) ctx->Bits >>= (bits); ctx->NumBits -= (bits); /* Aligns bit buffer to be on a byte boundary */ -#define Vorbis_AlignBits(state) UInt32 alignSkip = state->NumBits & 7; Vorbis_ConsumeBits(state, alignSkip); +#define Vorbis_AlignBits(ctx) UInt32 alignSkip = ctx->NumBits & 7; Vorbis_ConsumeBits(ctx, alignSkip); /* Ensures there are 'bitsCount' bits */ -#define Vorbis_EnsureBits(state, bitsCount) while (state->NumBits < bitsCount) { Vorbis_GetByte(state); } +#define Vorbis_EnsureBits(ctx, bitsCount) while (ctx->NumBits < bitsCount) { Vorbis_GetByte(ctx); } /* Peeks then consumes given bits */ /* TODO: Make this an inline macro somehow */ -UInt32 Vorbis_ReadBits(struct VorbisState* state, UInt32 bitsCount) { - Vorbis_EnsureBits(state, bitsCount); - UInt32 result = Vorbis_PeekBits(state, bitsCount); Vorbis_ConsumeBits(state, bitsCount); +static UInt32 Vorbis_ReadBits(struct VorbisState* ctx, UInt32 bitsCount) { + Vorbis_EnsureBits(ctx, bitsCount); + UInt32 result = Vorbis_PeekBits(ctx, bitsCount); Vorbis_ConsumeBits(ctx, bitsCount); return result; } #define VORBIS_MAX_CHANS 8 +#define Vorbis_ChanData(ctx, ch) (ctx->Values + (ch) * ctx->DataSize) -Int32 iLog(Int32 x) { +static Int32 iLog(Int32 x) { Int32 bits = 0; while (x > 0) { bits++; x >>= 2; } return bits; } -Real32 float32_unpack(struct VorbisState* state) { +static Real32 float32_unpack(struct VorbisState* ctx) { /* encoder macros can't reliably read over 24 bits */ - UInt32 lo = Vorbis_ReadBits(state, 16); - UInt32 hi = Vorbis_ReadBits(state, 16); + UInt32 lo = Vorbis_ReadBits(ctx, 16); + UInt32 hi = Vorbis_ReadBits(ctx, 16); UInt32 x = (hi << 16) | lo; Int32 mantissa = x & 0x1fffff; @@ -124,12 +125,14 @@ struct Codebook { UInt32 Dimensions, Entries, NumCodewords; UInt32* Codewords; UInt8* CodewordLens; + UInt32* Values; + /* vector quantisation values */ Real32 MinValue, DeltaValue; UInt32 SequenceP, LookupType, LookupValues; UInt16* Multiplicands; }; -UInt32 Codebook_Pow(UInt32 base, UInt32 exp) { +static UInt32 Codebook_Pow(UInt32 base, UInt32 exp) { UInt32 result = 1; /* exponentiation by squaring */ while (exp) { if (exp & 1) result *= base; @@ -139,7 +142,7 @@ UInt32 Codebook_Pow(UInt32 base, UInt32 exp) { return result; } -UInt32 lookup1_values(UInt32 entries, UInt32 dimensions) { +static UInt32 lookup1_values(UInt32 entries, UInt32 dimensions) { UInt32 i; /* the greatest integer value for which [value] to the power of [dimensions] is less than or equal to [entries] */ /* TODO: verify this */ @@ -154,10 +157,21 @@ UInt32 lookup1_values(UInt32 entries, UInt32 dimensions) { return 0; } -void Codebook_CalcCodewords(struct Codebook* codebook, UInt8* codewordLens, Int16 usedEntries) { +static void debug_entry(UInt32 codeword, Int16 len, UInt32 value) { + /* 2 entry 1: length 4 codeword 0100 */ + UChar binBuffer[33]; + for (int i = 0, shift = 31; i < len; i++, shift--) { + binBuffer[i] = (codeword & (1 << shift)) ? '1' : '0'; + } + binBuffer[len] = '\0'; + Platform_Log3("entry %p4: length %p2 codeword %c", &value, &len, binBuffer); +} + +static void Codebook_CalcCodewordsFail(struct Codebook* codebook, UInt8* codewordLens, Int16 usedEntries) { codebook->NumCodewords = usedEntries; codebook->Codewords = Platform_MemAlloc(usedEntries, sizeof(UInt32), "codewords"); codebook->CodewordLens = Platform_MemAlloc(usedEntries, sizeof(UInt8), "raw codeword lens"); + codebook->Values = Platform_MemAlloc(usedEntries, sizeof(UInt32), "values"); Int32 i, j, depth; UInt32 masks[33]; @@ -168,8 +182,8 @@ void Codebook_CalcCodewords(struct Codebook* codebook, UInt8* codewordLens, Int1 for (depth = 0; depth < 33; depth++) { UInt32 mask = ~(UInt32_MaxValue >> depth); /* e.g. depth of 4, 0xF0 00 00 00 */ masks[depth] = mask; - lastAssigned[depth] = 0; hasAssigned[depth] = false; + lastAssigned[depth] = 0; } masks[32] = UInt32_MaxValue; /* shift by 32 is same as 0 on some processors */ @@ -180,9 +194,12 @@ void Codebook_CalcCodewords(struct Codebook* codebook, UInt8* codewordLens, Int1 /* assign first codeword to be 0 */ if (!assignedFirst) { - codebook->Codewords[0] = 0; - hasAssigned[len] = true; - assignedFirst = true; + codebook->Values[j] = i; + codebook->Codewords[j] = 0; + + hasAssigned[len] = true; + assignedFirst = true; + debug_entry(0, len, i); j++; continue; } @@ -196,55 +213,108 @@ void Codebook_CalcCodewords(struct Codebook* codebook, UInt8* codewordLens, Int1 bool free = true; for (depth = 1; depth < len; depth++) { if (!hasAssigned[depth]) continue; - if ((lastAssigned[depth] & masks[depth]) != (codeword & masks[depth])) continue; + if ((codeword & masks[depth]) != (lastAssigned[depth] & masks[depth])) continue; free = false; break; } /* has this branch been assigned before further down the tree? */ for (depth = len; depth < 33; depth++) { if (!hasAssigned[depth]) continue; - if ((lastAssigned[depth] & masks[len]) != codeword) continue; + if (codeword != (lastAssigned[depth] & masks[len])) continue; free = false; break; } if (free) break; } + codebook->Values[j] = i; codebook->Codewords[j] = codeword; - lastAssigned[len] = codeword; + debug_entry(codeword, len, i); hasAssigned[len] = true; + lastAssigned[len] = codeword; j++; } } -ReturnCode Codebook_DecodeSetup(struct VorbisState* state, struct Codebook* codebook) { - UInt32 sync = Vorbis_ReadBits(state, 24); - if (sync != CODEBOOK_SYNC) return VORBIS_ERR_CODEBOOK_SYNC; - codebook->Dimensions = Vorbis_ReadBits(state, 16); - codebook->Entries = Vorbis_ReadBits(state, 24); +static bool Codebook_CalcCodewords(struct Codebook* c, UInt8* len, Int16 n) { + c->NumCodewords = n; + c->Codewords = Platform_MemAlloc(n, sizeof(UInt32), "codewords"); + c->CodewordLens = Platform_MemAlloc(n, sizeof(UInt8), "raw codeword lens"); + c->Values = Platform_MemAlloc(n, sizeof(UInt32), "values"); - UInt8* codewordLens = Platform_MemAlloc(codebook->Entries, sizeof(UInt8), "raw codeword lens"); - Int32 i, ordered = Vorbis_ReadBits(state, 1), usedEntries = 0; + /* This is taken from stb_vorbis.c because I gave up trying */ + UInt32 i, j, depth; + UInt32 next_codewords[33] = { 0 }; + + /* add codeword 0 to tree */ + for (i = 0; i < n; i++) { + if (len[i] == UInt8_MaxValue) continue; + + c->Codewords[0] = 0; + c->CodewordLens[0] = len[i]; + c->Values[0] = i; + break; + } + + /* set codewords that new nodes can start from */ + for (depth = 1; depth <= len[i]; depth++) { + next_codewords[depth] = 1U << (32 - depth); + } + + i++; /* first codeword was already handled */ + for (j = 1; i < n; i++) { + UInt32 root = len[i]; + if (root == UInt8_MaxValue) continue; + + /* per spec, find lowest possible value (leftmost) */ + while (root && next_codewords[root] == 0) root--; + if (root == 0) return false; + + UInt32 codeword = next_codewords[root]; + next_codewords[root] = 0; + + c->Codewords[j] = codeword; + c->CodewordLens[j] = len[i]; + c->Values[j] = i; + + debug_entry(codeword, len[i], i); + + for (depth = len[i]; depth > root; depth--) { + next_codewords[depth] = codeword + (1U << (32 - depth)); + } + j++; + } + return true; +} + +static ReturnCode Codebook_DecodeSetup(struct VorbisState* ctx, struct Codebook* c) { + UInt32 sync = Vorbis_ReadBits(ctx, 24); + if (sync != CODEBOOK_SYNC) return VORBIS_ERR_CODEBOOK_SYNC; + c->Dimensions = Vorbis_ReadBits(ctx, 16); + c->Entries = Vorbis_ReadBits(ctx, 24); + + UInt8* codewordLens = Platform_MemAlloc(c->Entries, sizeof(UInt8), "raw codeword lens"); + Int32 i, ordered = Vorbis_ReadBits(ctx, 1), usedEntries = 0; if (!ordered) { - Int32 sparse = Vorbis_ReadBits(state, 1); - for (i = 0; i < codebook->Entries; i++) { + Int32 sparse = Vorbis_ReadBits(ctx, 1); + for (i = 0; i < c->Entries; i++) { if (sparse) { - Int32 flag = Vorbis_ReadBits(state, 1); + Int32 flag = Vorbis_ReadBits(ctx, 1); if (!flag) { codewordLens[i] = UInt8_MaxValue; continue; /* unused entry */ } } - Int32 len = Vorbis_ReadBits(state, 5); len++; + Int32 len = Vorbis_ReadBits(ctx, 5); len++; codewordLens[i] = len; usedEntries++; } } else { Int32 entry; - Int32 curLength = Vorbis_ReadBits(state, 5); curLength++; - for (entry = 0; entry < codebook->Entries;) { - Int32 runBits = iLog(codebook->Entries - entry); - Int32 runLen = Vorbis_ReadBits(state, runBits); + Int32 curLength = Vorbis_ReadBits(ctx, 5); curLength++; + for (entry = 0; entry < c->Entries;) { + Int32 runBits = iLog(c->Entries - entry); + Int32 runLen = Vorbis_ReadBits(ctx, runBits); for (i = entry; i < entry + runLen; i++) { codewordLens[i] = curLength; @@ -252,73 +322,89 @@ ReturnCode Codebook_DecodeSetup(struct VorbisState* state, struct Codebook* code entry += runLen; curLength++; - if (entry > codebook->Entries) return VORBIS_ERR_CODEBOOK_ENTRY; + if (entry > c->Entries) return VORBIS_ERR_CODEBOOK_ENTRY; } - usedEntries = codebook->Entries; + usedEntries = c->Entries; } - Codebook_CalcCodewords(codebook, codewordLens, usedEntries); + Platform_LogConst("### WORKING ###"); + Codebook_CalcCodewords(c, codewordLens, usedEntries); + Platform_LogConst("### FAIL1 ###"); + Codebook_CalcCodewordsFail(c, codewordLens, usedEntries); Platform_MemFree(&codewordLens); - codebook->LookupType = Vorbis_ReadBits(state, 4); - if (codebook->LookupType == 0) return 0; - if (codebook->LookupType > 2) return VORBIS_ERR_CODEBOOK_LOOKUP; + c->LookupType = Vorbis_ReadBits(ctx, 4); + if (c->LookupType == 0) return 0; + if (c->LookupType > 2) return VORBIS_ERR_CODEBOOK_LOOKUP; - codebook->MinValue = float32_unpack(state); - codebook->DeltaValue = float32_unpack(state); - Int32 valueBits = Vorbis_ReadBits(state, 4); valueBits++; - codebook->SequenceP = Vorbis_ReadBits(state, 1); + c->MinValue = float32_unpack(ctx); + c->DeltaValue = float32_unpack(ctx); + Int32 valueBits = Vorbis_ReadBits(ctx, 4); valueBits++; + c->SequenceP = Vorbis_ReadBits(ctx, 1); UInt32 lookupValues; - if (codebook->LookupType == 1) { - lookupValues = lookup1_values(codebook->Entries, codebook->Dimensions); + if (c->LookupType == 1) { + lookupValues = lookup1_values(c->Entries, c->Dimensions); } else { - lookupValues = codebook->Entries * codebook->Dimensions; + lookupValues = c->Entries * c->Dimensions; } - codebook->LookupValues = lookupValues; + c->LookupValues = lookupValues; - codebook->Multiplicands = Platform_MemAlloc(lookupValues, sizeof(UInt16), "multiplicands"); + c->Multiplicands = Platform_MemAlloc(lookupValues, sizeof(UInt16), "multiplicands"); for (i = 0; i < lookupValues; i++) { - codebook->Multiplicands[i] = Vorbis_ReadBits(state, valueBits); + c->Multiplicands[i] = Vorbis_ReadBits(ctx, valueBits); } return 0; } -Real32* Codebook_DecodeVQ_Type1(struct Codebook* codebook, UInt32 lookupOffset) { +static Real32* Codebook_DecodeVQ_Type1(struct Codebook* c, UInt32 lookupOffset) { Real32 last = 0.0f; UInt32 indexDivisor = 1, i; Real32* values; - for (i = 0; i < codebook->Dimensions; i++) { - UInt32 offset = (lookupOffset / indexDivisor) % codebook->LookupValues; - values[i] = codebook->Multiplicands[offset] * codebook->DeltaValue + codebook->MinValue + last; - if (codebook->SequenceP) last = values[i]; - indexDivisor *= codebook->LookupValues; + for (i = 0; i < c->Dimensions; i++) { + UInt32 offset = (lookupOffset / indexDivisor) % c->LookupValues; + values[i] = c->Multiplicands[offset] * c->DeltaValue + c->MinValue + last; + if (c->SequenceP) last = values[i]; + indexDivisor *= c->LookupValues; } return values; } -Real32* Codebook_DecodeVQ_Type2(struct Codebook* codebook, UInt32 lookupOffset) { +static Real32* Codebook_DecodeVQ_Type2(struct Codebook* c, UInt32 lookupOffset) { Real32 last = 0.0f; - UInt32 i, offset = lookupOffset * codebook->Dimensions; + UInt32 i, offset = lookupOffset * c->Dimensions; Real32* values; - for (i = 0; i < codebook->Dimensions; i++, offset++) { - values[i] = codebook->Multiplicands[offset] * codebook->DeltaValue + codebook->MinValue + last; - if (codebook->SequenceP) last = values[i]; + for (i = 0; i < c->Dimensions; i++, offset++) { + values[i] = c->Multiplicands[offset] * c->DeltaValue + c->MinValue + last; + if (c->SequenceP) last = values[i]; } return values; } -UInt32 Codebook_DecodeScalar(struct Codebook* codebook) { +static UInt32 Codebook_DecodeScalar(struct VorbisState* ctx, struct Codebook* c) { + UInt32 codeword = 0, shift = 31, depth, i; + /* TODO: This is so massively slow */ + for (depth = 1; depth <= 32; depth++, shift--) { + codeword >>= 1; + codeword |= Vorbis_ReadBits(ctx, 1) << 31; + for (i = 0; i < c->NumCodewords; i++) { + if (depth != c->CodewordLens[i]) continue; + if (codeword != c->Codewords[i]) continue; + + return c->Values[i]; + } + } + ErrorHandler_Fail("????????????"); return -1; } -Real32* Codebook_DecodeVectors(struct Codebook* codebook) { - UInt32 lookupOffset = Codebook_DecodeScalar(codebook); - switch (codebook->LookupType) { - case 1: return Codebook_DecodeVQ_Type1(codebook, lookupOffset); - case 2: return Codebook_DecodeVQ_Type2(codebook, lookupOffset); +static Real32* Codebook_DecodeVectors(struct VorbisState* ctx, struct Codebook* c) { + UInt32 lookupOffset = Codebook_DecodeScalar(ctx, c); + switch (c->LookupType) { + case 1: return Codebook_DecodeVQ_Type1(c, lookupOffset); + case 2: return Codebook_DecodeVQ_Type2(c, lookupOffset); } return NULL; } @@ -328,7 +414,7 @@ Real32* Codebook_DecodeVectors(struct Codebook* codebook) { *#########################################################################################################################*/ #define FLOOR_MAX_PARTITIONS 32 #define FLOOR_MAX_CLASSES 16 -#define FLOOR_MAX_VALUES (FLOOR_MAX_PARTITIONS * 8) +#define FLOOR_MAX_VALUES (FLOOR_MAX_PARTITIONS * 8 + 2) struct Floor { UInt8 Partitions, Multiplier; Int32 Range, Values; UInt8 PartitionClasses[FLOOR_MAX_PARTITIONS]; @@ -340,71 +426,71 @@ struct Floor { Int32 YList[FLOOR_MAX_VALUES]; }; -ReturnCode Floor_DecodeSetup(struct VorbisState* state, struct Floor* floor) { - floor->Partitions = Vorbis_ReadBits(state, 5); +static ReturnCode Floor_DecodeSetup(struct VorbisState* ctx, struct Floor* f) { + f->Partitions = Vorbis_ReadBits(ctx, 5); Int32 i, j, idx, maxClass = -1; - for (i = 0; i < floor->Partitions; i++) { - floor->PartitionClasses[i] = Vorbis_ReadBits(state, 4); - maxClass = max(maxClass, floor->PartitionClasses[i]); + for (i = 0; i < f->Partitions; i++) { + f->PartitionClasses[i] = Vorbis_ReadBits(ctx, 4); + maxClass = max(maxClass, f->PartitionClasses[i]); } for (i = 0; i <= maxClass; i++) { - floor->ClassDimensions[i] = Vorbis_ReadBits(state, 3); floor->ClassDimensions[i]++; - floor->ClassSubClasses[i] = Vorbis_ReadBits(state, 2); - if (floor->ClassSubClasses[i]) { - floor->ClassMasterbooks[i] = Vorbis_ReadBits(state, 8); + f->ClassDimensions[i] = Vorbis_ReadBits(ctx, 3); f->ClassDimensions[i]++; + f->ClassSubClasses[i] = Vorbis_ReadBits(ctx, 2); + if (f->ClassSubClasses[i]) { + f->ClassMasterbooks[i] = Vorbis_ReadBits(ctx, 8); } - for (j = 0; j < (1 << floor->ClassSubClasses[i]); j++) { - floor->SubclassBooks[i][j] = Vorbis_ReadBits(state, 8); floor->SubclassBooks[i][j]--; + for (j = 0; j < (1 << f->ClassSubClasses[i]); j++) { + f->SubclassBooks[i][j] = Vorbis_ReadBits(ctx, 8); f->SubclassBooks[i][j]--; } } - floor->Multiplier = Vorbis_ReadBits(state, 2); floor->Multiplier++; + f->Multiplier = Vorbis_ReadBits(ctx, 2); f->Multiplier++; static Int16 ranges[4] = { 256, 128, 84, 64 }; - floor->Range = ranges[floor->Multiplier - 1]; + f->Range = ranges[f->Multiplier - 1]; - UInt32 rangeBits = Vorbis_ReadBits(state, 4); - floor->XList[0] = 0; - floor->XList[1] = 1 << rangeBits; + UInt32 rangeBits = Vorbis_ReadBits(ctx, 4); + f->XList[0] = 0; + f->XList[1] = 1 << rangeBits; - for (i = 0, idx = 2; i < floor->Partitions; i++) { - Int32 classNum = floor->PartitionClasses[i]; - for (j = 0; j < floor->ClassDimensions[classNum]; j++) { - floor->XList[idx++] = Vorbis_ReadBits(state, rangeBits); + for (i = 0, idx = 2; i < f->Partitions; i++) { + Int32 classNum = f->PartitionClasses[i]; + for (j = 0; j < f->ClassDimensions[classNum]; j++) { + f->XList[idx++] = Vorbis_ReadBits(ctx, rangeBits); } } - floor->Values = idx; + f->Values = idx; return 0; } -bool Floor_DecodeFrame(struct VorbisState* state, struct Floor* floor) { - Int32 nonZero = Vorbis_ReadBits(state, 1); +static bool Floor_DecodeFrame(struct VorbisState* ctx, struct Floor* f) { + Int32 nonZero = Vorbis_ReadBits(ctx, 1); if (!nonZero) return false; - Int32 i, j, idx, rangeBits = iLog(floor->Range - 1); - floor->YList[0] = Vorbis_ReadBits(state, rangeBits); - floor->YList[1] = Vorbis_ReadBits(state, rangeBits); + Int32 i, j, idx, rangeBits = iLog(f->Range - 1); + f->YList[0] = Vorbis_ReadBits(ctx, rangeBits); + f->YList[1] = Vorbis_ReadBits(ctx, rangeBits); - for (i = 0, idx = 2; i < floor->Partitions; i++) { - UInt8 class = floor->PartitionClasses[i]; - UInt8 cdim = floor->ClassDimensions[class]; - UInt8 cbits = floor->ClassSubClasses[class]; + for (i = 0, idx = 2; i < f->Partitions; i++) { + UInt8 class = f->PartitionClasses[i]; + UInt8 cdim = f->ClassDimensions[class]; + UInt8 cbits = f->ClassSubClasses[class]; UInt32 csub = (1 << cbits) - 1; UInt32 cval = 0; if (cbits) { - UInt8 bookNum = floor->ClassMasterbooks[class]; - cval = Codebook_DecodeScalar(&state->Codebooks[bookNum]); + UInt8 bookNum = f->ClassMasterbooks[class]; + cval = Codebook_DecodeScalar(ctx, &ctx->Codebooks[bookNum]); } for (j = 0; j < cdim; j++) { - Int16 bookNum = floor->SubclassBooks[class][cval & csub]; + Int16 bookNum = f->SubclassBooks[class][cval & csub]; cval <<= cbits; if (bookNum >= 0) { - floor->YList[idx + j] = Codebook_DecodeScalar(&state->Codebooks[bookNum]); + f->YList[idx + j] = Codebook_DecodeScalar(ctx, &ctx->Codebooks[bookNum]); } else { - floor->YList[idx + j] = 0; + f->YList[idx + j] = 0; } } idx += cdim; @@ -412,7 +498,7 @@ bool Floor_DecodeFrame(struct VorbisState* state, struct Floor* floor) { return true; } -Int32 render_point(Int32 x0, Int32 y0, Int32 x1, Int32 y1, Int32 X) { +static Int32 render_point(Int32 x0, Int32 y0, Int32 x1, Int32 y1, Int32 X) { Int32 dy = y1 - y0, adx = x1 - x0; Int32 ady = Math_AbsI(dy); Int32 err = ady * (X - x0); @@ -425,7 +511,7 @@ Int32 render_point(Int32 x0, Int32 y0, Int32 x1, Int32 y1, Int32 X) { } } -void render_line(Int32 x0, Int32 y0, Int32 x1, Int32 y1, Int32* v) { +static void render_line(Int32 x0, Int32 y0, Int32 x1, Int32 y1, Int32* v) { Int32 dy = y1 - y0, adx = x1 - x0; Int32 ady = Math_AbsI(dy); Int32 base = dy / adx, sy; @@ -452,7 +538,7 @@ void render_line(Int32 x0, Int32 y0, Int32 x1, Int32 y1, Int32* v) { } } -Int32 low_neighbor(Int16* v, Int32 x) { +static Int32 low_neighbor(Int16* v, Int32 x) { Int32 n = 0, i, max = Int32_MinValue; for (i = 0; i < x; i++) { if (v[i] < v[x] && v[i] > max) { n = i; max = v[i]; } @@ -460,7 +546,7 @@ Int32 low_neighbor(Int16* v, Int32 x) { return n; } -Int32 high_neighbor(Int16* v, Int32 x) { +static Int32 high_neighbor(Int16* v, Int32 x) { Int32 n = 0, i, min = Int32_MaxValue; for (i = 0; i < x; i++) { if (v[i] > v[x] && v[i] < min) { n = i; min = v[i]; } @@ -468,26 +554,27 @@ Int32 high_neighbor(Int16* v, Int32 x) { return n; } -void Floor_Synthesis(struct VorbisState* state, struct Floor* floor) { +static Real32 floor1_inverse_dB_table[256]; +static void Floor_Synthesis(struct VorbisState* ctx, struct Floor* f, Real32* data) { /* amplitude value synthesis */ Int32 YFinal[FLOOR_MAX_VALUES]; bool Step2[FLOOR_MAX_VALUES]; Step2[0] = true; Step2[1] = true; - YFinal[0] = floor->YList[0]; - YFinal[1] = floor->YList[1]; + YFinal[0] = f->YList[0]; + YFinal[1] = f->YList[1]; Int32 i; - for (i = 2; i < floor->Values; i++) { - Int32 lo_offset = low_neighbor(floor->XList, i); - Int32 hi_offset = high_neighbor(floor->XList, i); + for (i = 2; i < f->Values; i++) { + Int32 lo_offset = low_neighbor(f->XList, i); + Int32 hi_offset = high_neighbor(f->XList, i); - Int32 predicted = render_point(floor->XList[lo_offset], YFinal[lo_offset], - floor->XList[hi_offset], YFinal[hi_offset], floor->XList[i]); + Int32 predicted = render_point(f->XList[lo_offset], YFinal[lo_offset], + f->XList[hi_offset], YFinal[hi_offset], f->XList[i]); - Int32 val = floor->YList[i]; - Int32 highroom = floor->Range - predicted; + Int32 val = f->YList[i]; + Int32 highroom = f->Range - predicted; Int32 lowroom = predicted, room; if (highroom < lowroom) { @@ -519,7 +606,29 @@ void Floor_Synthesis(struct VorbisState* state, struct Floor* floor) { YFinal[i] = predicted; } } - /* TODO: curve synthesis */ + + /* curve synthesis */ + Int32 hx = 0, lx = 0, hy, rawI; + Int32 ly = YFinal[f->ListOrder[0]] * f->Multiplier; + + for (rawI = 1; rawI < f->Values; rawI++) { + Int32 i = f->ListOrder[rawI]; + if (!Step2[i]) continue; + + hx = f->XList[i]; hy = YFinal[i] * f->Multiplier; + render_line(lx, ly, hx, hy, floor); + lx = hx; ly = hy; + } + + if (hx < ctx->DataSize) { + render_line(hx, hy, ctx->DataSize, hy, floor); + } + + +28 15) for each scalar in vector [floor], perform a lookup substitution using +29 the scalar value from [floor] as an offset into the vector [floor1_inverse_dB_static_table] +30 +31 16) done } @@ -534,47 +643,47 @@ struct Residue { Int16 Books[RESIDUE_MAX_CLASSIFICATIONS][8]; }; -ReturnCode Residue_DecodeSetup(struct VorbisState* state, struct Residue* residue, UInt16 type) { - residue->Type = type; - residue->Begin = Vorbis_ReadBits(state, 24); - residue->End = Vorbis_ReadBits(state, 24); - residue->PartitionSize = Vorbis_ReadBits(state, 24); residue->PartitionSize++; - residue->Classifications = Vorbis_ReadBits(state, 6); residue->Classifications++; - residue->Classbook = Vorbis_ReadBits(state, 8); +static ReturnCode Residue_DecodeSetup(struct VorbisState* ctx, struct Residue* r, UInt16 type) { + r->Type = type; + r->Begin = Vorbis_ReadBits(ctx, 24); + r->End = Vorbis_ReadBits(ctx, 24); + r->PartitionSize = Vorbis_ReadBits(ctx, 24); r->PartitionSize++; + r->Classifications = Vorbis_ReadBits(ctx, 6); r->Classifications++; + r->Classbook = Vorbis_ReadBits(ctx, 8); Int32 i; - for (i = 0; i < residue->Classifications; i++) { - residue->Cascade[i] = Vorbis_ReadBits(state, 3); - Int32 hasBits = Vorbis_ReadBits(state, 1); - if (!hasBits) continue; - Int32 bits = Vorbis_ReadBits(state, 5); - residue->Cascade[i] |= bits << 3; + for (i = 0; i < r->Classifications; i++) { + r->Cascade[i] = Vorbis_ReadBits(ctx, 3); + Int32 moreBits = Vorbis_ReadBits(ctx, 1); + if (!moreBits) continue; + Int32 bits = Vorbis_ReadBits(ctx, 5); + r->Cascade[i] |= bits << 3; } Int32 j; - for (i = 0; i < residue->Classifications; i++) { + for (i = 0; i < r->Classifications; i++) { for (j = 0; j < 8; j++) { Int16 codebook = -1; - if (residue->Cascade[i] & (1 << j)) { - codebook = Vorbis_ReadBits(state, 8); + if (r->Cascade[i] & (1 << j)) { + codebook = Vorbis_ReadBits(ctx, 8); } - residue->Books[i][j] = codebook; + r->Books[i][j] = codebook; } } return 0; } -void Residue_DecodeFrame(struct VorbisState* state, struct Residue* residue, Int32 ch, bool* doNotDecode) { - UInt32 size = state->CurBlockSize / 2; - if (residue->Type == 2) size *= ch; +static void Residue_DecodeFrame(struct VorbisState* ctx, struct Residue* r, Int32 ch, bool* doNotDecode) { + UInt32 size = ctx->DataSize; + if (r->Type == 2) size *= ch; - UInt32 residueBeg = max(residue->Begin, size); - UInt32 residueEnd = max(residue->End, size); + UInt32 residueBeg = max(r->Begin, size); + UInt32 residueEnd = max(r->End, size); - struct Codebook* classbook = &state->Codebooks[residue->Classbook]; + struct Codebook* classbook = &ctx->Codebooks[r->Classbook]; UInt32 classwordsPerCodeword = classbook->Dimensions; UInt32 pass, i, j, nToRead = residueEnd - residueBeg; - UInt32 partitionsToRead = nToRead / residue->PartitionSize; + UInt32 partitionsToRead = nToRead / r->PartitionSize; UInt8* classifications[VORBIS_MAX_CHANS]; /* TODO: allocate and zero all vectors that will be returned. */ @@ -588,11 +697,11 @@ void Residue_DecodeFrame(struct VorbisState* state, struct Residue* residue, Int for (j = 0; j < ch; j++) { if (doNotDecode[j]) continue; - UInt32 temp = Codebook_DecodeScalar(classbook); + UInt32 temp = Codebook_DecodeScalar(ctx, classbook); /* TODO: i must be signed, otherwise infinite loop */ for (i = classwordsPerCodeword - 1; i >= 0; i--) { - classifications[j][i + partitionCount] = temp % residue->Classifications; - temp /= residue->Classifications; + classifications[j][i + partitionCount] = temp % r->Classifications; + temp /= r->Classifications; } } } @@ -602,11 +711,11 @@ void Residue_DecodeFrame(struct VorbisState* state, struct Residue* residue, Int if (doNotDecode[j]) continue; UInt8 class = classifications[j][partitionCount]; - Int16 book = residue->Books[class][pass]; + Int16 book = r->Books[class][pass]; if (book >= 0) { - UInt32 offset = residueBeg + partitionCount * residue->PartitionSize; - Real32* vectors = Codebook_DecodeVectors(&state->Codebooks[book]); + UInt32 offset = residueBeg + partitionCount * r->PartitionSize; + Real32* vectors = Codebook_DecodeVectors(ctx, &ctx->Codebooks[book]); /* TODO: decode partition into output vector number[j]; */ } } @@ -630,43 +739,44 @@ struct Mapping { UInt8 Magnitude[MAPPING_MAX_COUPLINGS]; UInt8 Angle[MAPPING_MAX_COUPLINGS]; }; -ReturnCode Mapping_DecodeSetup(struct VorbisState* state, struct Mapping* mapping) { - Int32 i, submaps = 1, submapFlag = Vorbis_ReadBits(state, 1); + +static ReturnCode Mapping_DecodeSetup(struct VorbisState* ctx, struct Mapping* m) { + Int32 i, submaps = 1, submapFlag = Vorbis_ReadBits(ctx, 1); if (submapFlag) { - submaps = Vorbis_ReadBits(state, 4); submaps++; + submaps = Vorbis_ReadBits(ctx, 4); submaps++; } - Int32 couplingSteps = 0, couplingFlag = Vorbis_ReadBits(state, 1); + Int32 couplingSteps = 0, couplingFlag = Vorbis_ReadBits(ctx, 1); if (couplingFlag) { - couplingSteps = Vorbis_ReadBits(state, 8); couplingSteps++; + couplingSteps = Vorbis_ReadBits(ctx, 8); couplingSteps++; /* TODO: How big can couplingSteps ever really get in practice? */ - Int32 couplingBits = iLog(state->Channels - 1); + Int32 couplingBits = iLog(ctx->Channels - 1); for (i = 0; i < couplingSteps; i++) { - mapping->Magnitude[i] = Vorbis_ReadBits(state, couplingBits); - mapping->Angle[i] = Vorbis_ReadBits(state, couplingBits); - if (mapping->Magnitude[i] == mapping->Angle[i]) return VORBIS_ERR_MAPPING_CHANS; + m->Magnitude[i] = Vorbis_ReadBits(ctx, couplingBits); + m->Angle[i] = Vorbis_ReadBits(ctx, couplingBits); + if (m->Magnitude[i] == m->Angle[i]) return VORBIS_ERR_MAPPING_CHANS; } } - Int32 reserved = Vorbis_ReadBits(state, 2); + Int32 reserved = Vorbis_ReadBits(ctx, 2); if (reserved != 0) return VORBIS_ERR_MAPPING_RESERVED; - mapping->Submaps = submaps; - mapping->CouplingSteps = couplingSteps; + m->Submaps = submaps; + m->CouplingSteps = couplingSteps; if (submaps > 1) { - for (i = 0; i < state->Channels; i++) { - mapping->Mux[i] = Vorbis_ReadBits(state, 4); + for (i = 0; i < ctx->Channels; i++) { + m->Mux[i] = Vorbis_ReadBits(ctx, 4); } } else { - for (i = 0; i < state->Channels; i++) { - mapping->Mux[i] = 0; + for (i = 0; i < ctx->Channels; i++) { + m->Mux[i] = 0; } } for (i = 0; i < submaps; i++) { - Vorbis_ReadBits(state, 8); /* time value */ - mapping->FloorIdx[i] = Vorbis_ReadBits(state, 8); - mapping->ResidueIdx[i] = Vorbis_ReadBits(state, 8); + Vorbis_ReadBits(ctx, 8); /* time value */ + m->FloorIdx[i] = Vorbis_ReadBits(ctx, 8); + m->ResidueIdx[i] = Vorbis_ReadBits(ctx, 8); } return 0; } @@ -676,20 +786,20 @@ ReturnCode Mapping_DecodeSetup(struct VorbisState* state, struct Mapping* mappin *-----------------------------------------------------Vorbis setup--------------------------------------------------------* *#########################################################################################################################*/ struct Mode { UInt8 BlockSizeFlag, MappingIdx; }; -ReturnCode Mode_DecodeSetup(struct VorbisState* state, struct Mode* mode) { - mode->BlockSizeFlag = Vorbis_ReadBits(state, 1); - UInt16 windowType = Vorbis_ReadBits(state, 16); +static ReturnCode Mode_DecodeSetup(struct VorbisState* ctx, struct Mode* m) { + m->BlockSizeFlag = Vorbis_ReadBits(ctx, 1); + UInt16 windowType = Vorbis_ReadBits(ctx, 16); if (windowType != 0) return VORBIS_ERR_MODE_WINDOW; - UInt16 transformType = Vorbis_ReadBits(state, 16); + UInt16 transformType = Vorbis_ReadBits(ctx, 16); if (transformType != 0) return VORBIS_ERR_MODE_TRANSFORM; - mode->MappingIdx = Vorbis_ReadBits(state, 8); + m->MappingIdx = Vorbis_ReadBits(ctx, 8); return 0; } -void Vorbis_Init(struct VorbisState* state, struct Stream* source) { - Platform_MemSet(state, 0, sizeof(struct VorbisState)); - state->Source = source; +void Vorbis_Init(struct VorbisState* ctx, struct Stream* source) { + Platform_MemSet(ctx, 0, sizeof(struct VorbisState)); + ctx->Source = source; } /* TODO: Work out Vorbis_Free implementation */ @@ -698,9 +808,9 @@ static bool Vorbis_ValidBlockSize(UInt32 blockSize) { return blockSize >= 64 && blockSize <= 8192 && Math_IsPowOf2(blockSize); } -static ReturnCode Vorbis_DecodeHeader(struct VorbisState* state, UInt8 type) { +static ReturnCode Vorbis_DecodeHeader(struct VorbisState* ctx, UInt8 type) { UInt8 header[7]; - Stream_Read(state->Source, header, sizeof(header)); + Stream_Read(ctx->Source, header, sizeof(header)); if (header[0] != type) return VORBIS_ERR_WRONG_HEADER; bool OK = @@ -709,116 +819,116 @@ static ReturnCode Vorbis_DecodeHeader(struct VorbisState* state, UInt8 type) { return OK ? 0 : ReturnCode_InvalidArg; } -static ReturnCode Vorbis_DecodeIdentifier(struct VorbisState* state) { +static ReturnCode Vorbis_DecodeIdentifier(struct VorbisState* ctx) { UInt8 header[23]; - Stream_Read(state->Source, header, sizeof(header)); + Stream_Read(ctx->Source, header, sizeof(header)); UInt32 version = Stream_GetU32_LE(&header[0]); if (version != 0) return VORBIS_ERR_VERSION; - state->Channels = header[4]; - state->SampleRate = Stream_GetU32_LE(&header[5]); + ctx->Channels = header[4]; + ctx->SampleRate = Stream_GetU32_LE(&header[5]); /* (12) bitrate_maximum, nominal, minimum */ - state->BlockSizes[0] = 1 << (header[21] & 0xF); - state->BlockSizes[1] = 1 << (header[21] >> 4); + ctx->BlockSizes[0] = 1 << (header[21] & 0xF); + ctx->BlockSizes[1] = 1 << (header[21] >> 4); - if (!Vorbis_ValidBlockSize(state->BlockSizes[0])) return VORBIS_ERR_BLOCKSIZE; - if (!Vorbis_ValidBlockSize(state->BlockSizes[1])) return VORBIS_ERR_BLOCKSIZE; - if (state->BlockSizes[0] > state->BlockSizes[1]) return VORBIS_ERR_BLOCKSIZE; + if (!Vorbis_ValidBlockSize(ctx->BlockSizes[0])) return VORBIS_ERR_BLOCKSIZE; + if (!Vorbis_ValidBlockSize(ctx->BlockSizes[1])) return VORBIS_ERR_BLOCKSIZE; + if (ctx->BlockSizes[0] > ctx->BlockSizes[1]) return VORBIS_ERR_BLOCKSIZE; - if (state->Channels == 0 || state->Channels > VORBIS_MAX_CHANS) return VORBIS_ERR_CHANS; + if (ctx->Channels == 0 || ctx->Channels > VORBIS_MAX_CHANS) return VORBIS_ERR_CHANS; /* check framing flag */ return (header[22] & 1) ? 0 : VORBIS_ERR_FRAMING; } -static ReturnCode Vorbis_DecodeComments(struct VorbisState* state) { - UInt32 vendorLen = Stream_ReadU32_LE(state->Source); - Stream_Skip(state->Source, vendorLen); +static ReturnCode Vorbis_DecodeComments(struct VorbisState* ctx) { + UInt32 vendorLen = Stream_ReadU32_LE(ctx->Source); + Stream_Skip(ctx->Source, vendorLen); - UInt32 i, comments = Stream_ReadU32_LE(state->Source); + UInt32 i, comments = Stream_ReadU32_LE(ctx->Source); for (i = 0; i < comments; i++) { - UInt32 commentLen = Stream_ReadU32_LE(state->Source); - Stream_Skip(state->Source, commentLen); + UInt32 commentLen = Stream_ReadU32_LE(ctx->Source); + Stream_Skip(ctx->Source, commentLen); } /* check framing flag */ - return (Stream_ReadU8(state->Source) & 1) ? 0 : VORBIS_ERR_FRAMING; + return (Stream_ReadU8(ctx->Source) & 1) ? 0 : VORBIS_ERR_FRAMING; } -static ReturnCode Vorbis_DecodeSetup(struct VorbisState* state) { +static ReturnCode Vorbis_DecodeSetup(struct VorbisState* ctx) { Int32 i, count; ReturnCode result; - count = Vorbis_ReadBits(state, 8); count++; - state->Codebooks = Platform_MemAlloc(count, sizeof(struct Codebook), "vorbis codebooks"); + count = Vorbis_ReadBits(ctx, 8); count++; + ctx->Codebooks = Platform_MemAlloc(count, sizeof(struct Codebook), "vorbis codebooks"); for (i = 0; i < count; i++) { - result = Codebook_DecodeSetup(state, &state->Codebooks[i]); + result = Codebook_DecodeSetup(ctx, &ctx->Codebooks[i]); if (result) return result; } - count = Vorbis_ReadBits(state, 6); count++; + count = Vorbis_ReadBits(ctx, 6); count++; for (i = 0; i < count; i++) { - UInt16 time = Vorbis_ReadBits(state, 16); + UInt16 time = Vorbis_ReadBits(ctx, 16); if (time != 0) return VORBIS_ERR_TIME_TYPE; } - count = Vorbis_ReadBits(state, 6); count++; - state->Floors = Platform_MemAlloc(count, sizeof(struct Floor), "vorbis floors"); + count = Vorbis_ReadBits(ctx, 6); count++; + ctx->Floors = Platform_MemAlloc(count, sizeof(struct Floor), "vorbis floors"); for (i = 0; i < count; i++) { - UInt16 floor = Vorbis_ReadBits(state, 16); + UInt16 floor = Vorbis_ReadBits(ctx, 16); if (floor != 1) return VORBIS_ERR_FLOOR_TYPE; - result = Floor_DecodeSetup(state, &state->Floors[i]); + result = Floor_DecodeSetup(ctx, &ctx->Floors[i]); if (result) return result; } - count = Vorbis_ReadBits(state, 6); count++; - state->Residues = Platform_MemAlloc(count, sizeof(struct Residue), "vorbis residues"); + count = Vorbis_ReadBits(ctx, 6); count++; + ctx->Residues = Platform_MemAlloc(count, sizeof(struct Residue), "vorbis residues"); for (i = 0; i < count; i++) { - UInt16 residue = Vorbis_ReadBits(state, 16); + UInt16 residue = Vorbis_ReadBits(ctx, 16); if (residue > 2) return VORBIS_ERR_FLOOR_TYPE; - result = Residue_DecodeSetup(state, &state->Residues[i], residue); + result = Residue_DecodeSetup(ctx, &ctx->Residues[i], residue); if (result) return result; } - count = Vorbis_ReadBits(state, 6); count++; - state->Mappings = Platform_MemAlloc(count, sizeof(struct Mapping), "vorbis mappings"); + count = Vorbis_ReadBits(ctx, 6); count++; + ctx->Mappings = Platform_MemAlloc(count, sizeof(struct Mapping), "vorbis mappings"); for (i = 0; i < count; i++) { - UInt16 mapping = Vorbis_ReadBits(state, 16); + UInt16 mapping = Vorbis_ReadBits(ctx, 16); if (mapping != 0) return VORBIS_ERR_MAPPING_TYPE; - result = Mapping_DecodeSetup(state, &state->Mappings[i]); + result = Mapping_DecodeSetup(ctx, &ctx->Mappings[i]); if (result) return result; } - count = Vorbis_ReadBits(state, 6); count++; - state->Modes = Platform_MemAlloc(count, sizeof(struct Mode), "vorbis modes"); + count = Vorbis_ReadBits(ctx, 6); count++; + ctx->Modes = Platform_MemAlloc(count, sizeof(struct Mode), "vorbis modes"); for (i = 0; i < count; i++) { - result = Mode_DecodeSetup(state, &state->Modes[i]); + result = Mode_DecodeSetup(ctx, &ctx->Modes[i]); if (result) return result; } - state->ModeNumBits = iLog(count - 1); /* ilog([vorbis_mode_count]-1) bits */ - UInt8 framing = Vorbis_ReadBits(state, 1); - Vorbis_AlignBits(state); + ctx->ModeNumBits = iLog(count - 1); /* ilog([vorbis_mode_count]-1) bits */ + UInt8 framing = Vorbis_ReadBits(ctx, 1); + Vorbis_AlignBits(ctx); /* check framing flag */ return (framing & 1) ? 0 : VORBIS_ERR_FRAMING; } enum VORBIS_PACKET { VORBIS_IDENTIFIER = 1, VORBIS_COMMENTS = 3, VORBIS_SETUP = 5, }; -ReturnCode Vorbis_DecodeHeaders(struct VorbisState* state) { +ReturnCode Vorbis_DecodeHeaders(struct VorbisState* ctx) { ReturnCode result; - result = Vorbis_DecodeHeader(state, VORBIS_IDENTIFIER); + result = Vorbis_DecodeHeader(ctx, VORBIS_IDENTIFIER); if (result) return result; - result = Vorbis_DecodeIdentifier(state); + result = Vorbis_DecodeIdentifier(ctx); if (result) return result; - result = Vorbis_DecodeHeader(state, VORBIS_COMMENTS); + result = Vorbis_DecodeHeader(ctx, VORBIS_COMMENTS); if (result) return result; - result = Vorbis_DecodeComments(state); + result = Vorbis_DecodeComments(ctx); if (result) return result; - result = Vorbis_DecodeHeader(state, VORBIS_SETUP); + result = Vorbis_DecodeHeader(ctx, VORBIS_SETUP); if (result) return result; - result = Vorbis_DecodeSetup(state); + result = Vorbis_DecodeSetup(ctx); if (result) return result; return 0; @@ -828,29 +938,32 @@ ReturnCode Vorbis_DecodeHeaders(struct VorbisState* state) { /*########################################################################################################################* *-----------------------------------------------------Vorbis frame--------------------------------------------------------* *#########################################################################################################################*/ -ReturnCode Vorbis_DecodeFrame(struct VorbisState* state) { - Int32 i, j, packetType = Vorbis_ReadBits(state, 1); +ReturnCode Vorbis_DecodeFrame(struct VorbisState* ctx) { + Int32 i, j, packetType = Vorbis_ReadBits(ctx, 1); if (packetType) return VORBIS_ERR_FRAME_TYPE; - Int32 modeIdx = Vorbis_ReadBits(state, state->ModeNumBits); - struct Mode* mode = &state->Modes[modeIdx]; - struct Mapping* mapping = &state->Mappings[mode->MappingIdx]; + Int32 modeIdx = Vorbis_ReadBits(ctx, ctx->ModeNumBits); + struct Mode* mode = &ctx->Modes[modeIdx]; + struct Mapping* mapping = &ctx->Mappings[mode->MappingIdx]; /* decode window shape */ - state->CurBlockSize = state->BlockSizes[mode->BlockSizeFlag]; + ctx->CurBlockSize = ctx->BlockSizes[mode->BlockSizeFlag]; + ctx->DataSize = ctx->CurBlockSize / 2; Int32 prev_window, next_window; /* long window lapping*/ if (mode->BlockSizeFlag) { - prev_window = Vorbis_ReadBits(state, 1); - next_window = Vorbis_ReadBits(state, 1); + prev_window = Vorbis_ReadBits(ctx, 1); + next_window = Vorbis_ReadBits(ctx, 1); } + ctx->Values = Platform_MemAllocCleared(ctx->Channels * ctx->DataSize, sizeof(Real32), "audio values"); + /* decode floor */ bool hasFloor[VORBIS_MAX_CHANS], hasResidue[VORBIS_MAX_CHANS]; - for (i = 0; i < state->Channels; i++) { + for (i = 0; i < ctx->Channels; i++) { Int32 submap = mapping->Mux[i]; Int32 floorIdx = mapping->FloorIdx[submap]; - hasFloor[i] = Floor_DecodeFrame(state, &state->Floors[floorIdx]); + hasFloor[i] = Floor_DecodeFrame(ctx, &ctx->Floors[floorIdx]); hasResidue[i] = hasFloor[i]; } @@ -860,8 +973,7 @@ ReturnCode Vorbis_DecodeFrame(struct VorbisState* state) { Int32 angChannel = mapping->Angle[i]; if (hasResidue[magChannel] || hasResidue[angChannel]) { - hasResidue[magChannel] = true; - hasResidue[angChannel] = true; + hasResidue[magChannel] = true; hasResidue[angChannel] = true; } } @@ -869,7 +981,7 @@ ReturnCode Vorbis_DecodeFrame(struct VorbisState* state) { for (i = 0; i < mapping->Submaps; i++) { Int32 ch = 0; bool doNotDecode[VORBIS_MAX_CHANS]; - for (j = 0; j < state->Channels; j++) { + for (j = 0; j < ctx->Channels; j++) { if (mapping->Mux[j] != i) continue; doNotDecode[ch] = !hasResidue[j]; @@ -877,18 +989,109 @@ ReturnCode Vorbis_DecodeFrame(struct VorbisState* state) { } Int32 residueIdx = mapping->FloorIdx[i]; - Residue_DecodeFrame(state, &state->Residues[residueIdx], ch, doNotDecode); + Residue_DecodeFrame(ctx, &ctx->Residues[residueIdx], ch, doNotDecode); ch = 0; - for (j = 0; j < state->Channels; j++) { + for (j = 0; j < ctx->Channels; j++) { if (mapping->Mux[j] != i) continue; /* TODO: residue vector for channel[j] is set to decoded residue vector[ch] */ ch++; } } + /* inverse coupling */ + for (i = mapping->CouplingSteps - 1; i >= 0; i--) { + Real32* magValues = Vorbis_ChanData(ctx, mapping->Magnitude[i]); + Real32* angValues = Vorbis_ChanData(ctx, mapping->Angle[i]); + + for (j = 0; j < ctx->DataSize; j++) { + Real32 m = magValues[i], a = angValues[i]; + + if (m > 0.0f) { + if (a > 0.0f) { angValues[j] = m - a; } + else { + angValues[j] = m; + magValues[j] = m + a; + } + } else { + if (a > 0.0f) { angValues[j] = m + a; } + else { + angValues[j] = m; + magValues[j] = m - a; + } + } + } + } + /* compute dot product of floor and residue, producing audio spectrum vector */ /* inverse monolithic transform of audio spectrum vector */ -} \ No newline at end of file +} + +static Real32 floor1_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.00000000f, +}; \ No newline at end of file diff --git a/src/Client/Audio.h b/src/Client/Audio.h index 2c6e60e61..7a538fd3f 100644 --- a/src/Client/Audio.h +++ b/src/Client/Audio.h @@ -34,8 +34,10 @@ struct VorbisState { UInt32 NumBits; /* Number of bits in Bits buffer*/ struct Stream* Source; /* Source for filling Input buffer */ - UInt8 Channels, ModeNumBits; UInt16 CurBlockSize; + UInt8 Channels, ModeNumBits; + UInt16 CurBlockSize, DataSize; Int32 SampleRate; Int32 BlockSizes[2]; + Real32* Values; struct Codebook* Codebooks; struct Floor* Floors; @@ -45,4 +47,5 @@ struct VorbisState { }; void Vorbis_Init(struct VorbisState* state, struct Stream* source); ReturnCode Vorbis_DecodeHeaders(struct VorbisState* state); +ReturnCode Vorbis_DecodeFrame(struct VorbisState* state); #endif diff --git a/src/Client/D3D9Api.c b/src/Client/D3D9Api.c index 41d70a07a..023388182 100644 --- a/src/Client/D3D9Api.c +++ b/src/Client/D3D9Api.c @@ -306,8 +306,8 @@ void Gfx_SetFogDensity(Real32 value) { if (value == d3d9_fogDensity) return; d3d9_fogDensity = value; if (Gfx_LostContext) return; - IntAndFloat raw; raw.fVal = value; - D3D9_SetRenderState(D3DRS_FOGDENSITY, raw.uVal, "D3D9_SetFogDensity"); + union IntAndFloat raw; raw.f = value; + D3D9_SetRenderState(D3DRS_FOGDENSITY, raw.u, "D3D9_SetFogDensity"); } Real32 d3d9_fogEnd = -1.0f; @@ -315,8 +315,8 @@ void Gfx_SetFogEnd(Real32 value) { if (value == d3d9_fogEnd) return; d3d9_fogEnd = value; if (Gfx_LostContext) return; - IntAndFloat raw; raw.fVal = value; - D3D9_SetRenderState(D3DRS_FOGEND, raw.uVal, "D3D9_SetFogEnd"); + union IntAndFloat raw; raw.f = value; + D3D9_SetRenderState(D3DRS_FOGEND, raw.u, "D3D9_SetFogEnd"); } D3DFOGMODE d3d9_fogTableMode = D3DFOG_NONE; @@ -636,7 +636,7 @@ static void D3D9_SetDefaultRenderStates(void) { } static void D3D9_RestoreRenderStates(void) { - IntAndFloat raw; + union IntAndFloat raw; D3D9_SetRenderState(D3DRS_ALPHATESTENABLE, d3d9_alphaTest, "D3D9_AlphaTest"); D3D9_SetRenderState2(D3DRS_ALPHABLENDENABLE, d3d9_alphaBlend, "D3D9_AlphaBlend"); D3D9_SetRenderState2(D3DRS_ALPHAFUNC, d3d9_alphaTestFunc, "D3D9_AlphaTestFunc"); @@ -645,10 +645,10 @@ static void D3D9_RestoreRenderStates(void) { D3D9_SetRenderState2(D3DRS_DESTBLEND, d3d9_dstBlendFunc, "D3D9_AlphaDstBlend"); D3D9_SetRenderState2(D3DRS_FOGENABLE, d3d9_fogEnable, "D3D9_Fog"); D3D9_SetRenderState2(D3DRS_FOGCOLOR, d3d9_fogCol, "D3D9_FogColor"); - raw.fVal = d3d9_fogDensity; - D3D9_SetRenderState2(D3DRS_FOGDENSITY, raw.uVal, "D3D9_FogDensity"); - raw.fVal = d3d9_fogEnd; - D3D9_SetRenderState2(D3DRS_FOGEND, raw.uVal, "D3D9_FogEnd"); + raw.f = d3d9_fogDensity; + D3D9_SetRenderState2(D3DRS_FOGDENSITY, raw.u, "D3D9_FogDensity"); + raw.f = d3d9_fogEnd; + D3D9_SetRenderState2(D3DRS_FOGEND, raw.u, "D3D9_FogEnd"); D3D9_SetRenderState2(D3DRS_FOGTABLEMODE, d3d9_fogTableMode, "D3D9_FogMode"); D3D9_SetRenderState2(D3DRS_ZFUNC, d3d9_depthTestFunc, "D3D9_DepthTestFunc"); D3D9_SetRenderState2(D3DRS_ZENABLE, d3d9_depthTest, "D3D9_DepthTest"); diff --git a/src/Client/Formats.c b/src/Client/Formats.c index 50083ebc4..12d808999 100644 --- a/src/Client/Formats.c +++ b/src/Client/Formats.c @@ -822,9 +822,9 @@ static void Cw_WriteBlockDefinitionCompound(struct Stream* stream, BlockID id) { Nbt_WriteTag(stream, NBT_TAG_INT8, "CollideType"); Nbt_WriteU8(stream, Block_Collide[id]); - IntAndFloat speed; speed.fVal = Block_SpeedMultiplier[id]; + union IntAndFloat speed; speed.f = Block_SpeedMultiplier[id]; Nbt_WriteTag(stream, NBT_TAG_REAL32, "Speed"); - Nbt_WriteI32(stream, speed.iVal); + Nbt_WriteI32(stream, speed.i); Nbt_WriteTag(stream, NBT_TAG_INT8_ARRAY, "Textures"); Nbt_WriteI32(stream, 6); diff --git a/src/Client/Funcs.h b/src/Client/Funcs.h index 316df7953..3e6f7331f 100644 --- a/src/Client/Funcs.h +++ b/src/Client/Funcs.h @@ -9,7 +9,7 @@ #define min(x, y) ((x) < (y) ? (x) : (y)) #define max(x, y) ((x) > (y) ? (x) : (y)) #define Array_Elems(arr) (sizeof(arr) / sizeof(arr[0])) -typedef union IntAndFloat_ { Real32 fVal; Int32 iVal; UInt32 uVal; } IntAndFloat; +union IntAndFloat { Real32 f; Int32 i; UInt32 u; }; #define QuickSort_Swap_Maybe()\ if (i <= j) {\ diff --git a/src/Client/Program.c b/src/Client/Program.c index aeaecab29..f419d62ea 100644 --- a/src/Client/Program.c +++ b/src/Client/Program.c @@ -29,6 +29,7 @@ int main(void) { struct VorbisState state; Vorbis_Init(&state, &ogg); Vorbis_DecodeHeaders(&state); + Vorbis_DecodeFrame(&state); /*Platform_HttpInit(); AsyncRequest req = { 0 }; diff --git a/src/Client/ServerConnection.h b/src/Client/ServerConnection.h index 7c0bc533e..48bdee520 100644 --- a/src/Client/ServerConnection.h +++ b/src/Client/ServerConnection.h @@ -65,6 +65,7 @@ void PingList_Update(UInt16 data); Int32 PingList_AveragePingMs(void); bool ServerConnection_IsSinglePlayer; +bool ServerConnection_Disconnected; extern String ServerConnection_ServerName; extern String ServerConnection_ServerMOTD; extern String ServerConnection_AppName; @@ -77,15 +78,9 @@ void (*ServerConnection_Tick)(struct ScheduledTask* task); struct Stream* (*ServerConnection_ReadStream)(void); struct Stream* (*ServerConnection_WriteStream)(void); -/* Whether the network processor is currently disconnected from the server. */ -bool ServerConnection_Disconnected; -/* Whether the client supports extended player list management, with group names and ranks. */ bool ServerConnection_SupportsExtPlayerList; -/* Whether the server supports handling PlayerClick packets from the client. */ bool ServerConnection_SupportsPlayerClick; -/* Whether the server can handle partial message packets or not. */ bool ServerConnection_SupportsPartialMessages; -/* Whether the server supports receiving all code page 437 characters from this client. */ bool ServerConnection_SupportsFullCP437; void ServerConnection_RetrieveTexturePack(STRING_PURE String* url);