From f4f7eddc07b4476e4ba84a0bd6d56ae184d33963 Mon Sep 17 00:00:00 2001 From: alcexhim Date: Thu, 17 Apr 2014 16:10:19 -0400 Subject: [PATCH] Moved OSL/IMA/BGM compression out of BGMDataFormat into its own compression module --- .../Multimedia/Audio/BGM/BGMDataFormat.cs | 77 +-------------- .../Audio/BGM/OSLCompressionModule.cs | 97 +++++++++++++++++++ 2 files changed, 101 insertions(+), 73 deletions(-) create mode 100644 CSharp/Plugins/UniversalEditor.Plugins.OSLib/DataFormats/Multimedia/Audio/BGM/OSLCompressionModule.cs diff --git a/CSharp/Plugins/UniversalEditor.Plugins.OSLib/DataFormats/Multimedia/Audio/BGM/BGMDataFormat.cs b/CSharp/Plugins/UniversalEditor.Plugins.OSLib/DataFormats/Multimedia/Audio/BGM/BGMDataFormat.cs index 1ddbc109..2fe182e6 100644 --- a/CSharp/Plugins/UniversalEditor.Plugins.OSLib/DataFormats/Multimedia/Audio/BGM/BGMDataFormat.cs +++ b/CSharp/Plugins/UniversalEditor.Plugins.OSLib/DataFormats/Multimedia/Audio/BGM/BGMDataFormat.cs @@ -37,10 +37,12 @@ namespace UniversalEditor.DataFormats.Multimedia.Audio.BGM byte unknown2 = reader.ReadByte(); byte[] sampleData = reader.ReadToEnd(); - short[] usamples = OSLDecode(sampleData.Length / 2, sampleData); + + OSLCompressionModule ocm = new OSLCompressionModule(); + sampleData = ocm.Decompress(sampleData); WaveformAudioObjectModel wave = (objectModel as WaveformAudioObjectModel); - wave.RawSamples = usamples; + wave.RawData = sampleData; wave.Header.BitsPerSample = 16; wave.Header.ChannelCount = 1; wave.Header.SampleRate = sampleRate; @@ -48,77 +50,6 @@ namespace UniversalEditor.DataFormats.Multimedia.Audio.BGM wave.Header.DataRate = sampleRate * 2; } - private readonly sbyte[] ima_index_table /*[16]*/ = - { - -1, -1, -1, -1, 2, 4, 7, 12, - -1, -1, -1, -1, 2, 4, 7, 12 - }; - private readonly short[] ima_step_table /*[89]*/ = - { - 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, - 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, - 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, - 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, - 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, - 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, - 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, - 5894, 6484, 7132, 7845, 8630, 9493,10442,11487,12635,13899, - 15289,16818,18500,20350,22385,24623,27086,29794,32767 - }; - - private static int ima_get_diff(int step, byte nibble, ref int predictor) - { - int diff = step >> 3; - if ((nibble & 1) != 0) diff += step >> 2; - if ((nibble & 2) != 0) diff += step >> 1; - if ((nibble & 4) != 0) diff += step; - if ((nibble & 7) == 7) diff += step >> 1; - if ((nibble & 8) != 0) diff = -diff; - return diff; - } - - private short[] OSLDecode(int sampleCount, byte[] data) - { - // each byte represents two IMA nibbles, which each represent a signed 16-bit PCM sample - // so therefore each byte represents 2 Int16 samples - short[] samples = new short[data.Length * 2]; - - int len = data.Length; - - int predictor = 0; - int step_index = 0; - byte nibble = 0; - - for (int i = 0; i < data.Length; i++) - { - int step = 0; - - nibble = (byte)(data[i] & 0x0F); - ima_process_nibble(nibble, ref step_index, ref predictor, ref step); - samples[(int)((double)i * 2)] = (short)predictor; - - nibble = (byte)(data[i] >> 4); - ima_process_nibble(nibble, ref step_index, ref predictor, ref step); - samples[(int)((double)i * 2) + 1] = (short)predictor; - } - return samples; - } - - private void ima_process_nibble(byte nibble, ref int step_index, ref int predictor, ref int step) - { - if (step_index < 0) step_index = 0; - if (step_index > ima_step_table.Length - 1) step_index = ima_step_table.Length - 1; - - step = ima_step_table[step_index]; - int diff = ima_get_diff(step, nibble, ref predictor); - - step_index += ima_index_table[nibble & 0x07]; - - predictor = predictor + diff; - if (predictor < Int16.MinValue) predictor = Int16.MinValue; - if (predictor > Int16.MaxValue) predictor = Int16.MaxValue; - } - protected override void SaveInternal(ObjectModel objectModel) { throw new NotImplementedException(); diff --git a/CSharp/Plugins/UniversalEditor.Plugins.OSLib/DataFormats/Multimedia/Audio/BGM/OSLCompressionModule.cs b/CSharp/Plugins/UniversalEditor.Plugins.OSLib/DataFormats/Multimedia/Audio/BGM/OSLCompressionModule.cs new file mode 100644 index 00000000..b0c9b7da --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Plugins.OSLib/DataFormats/Multimedia/Audio/BGM/OSLCompressionModule.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using UniversalEditor.Accessors; +using UniversalEditor.Compression; +using UniversalEditor.IO; + +namespace UniversalEditor.DataFormats.Multimedia.Audio.BGM +{ + public class OSLCompressionModule : CompressionModule + { + public override string Name + { + get { return "oslbgm"; } + } + + private readonly sbyte[] ima_index_table /*[16]*/ = + { + -1, -1, -1, -1, 2, 4, 7, 12, + -1, -1, -1, -1, 2, 4, 7, 12 + }; + private readonly short[] ima_step_table /*[89]*/ = + { + 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, + 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, + 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, + 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, + 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, + 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, + 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, + 5894, 6484, 7132, 7845, 8630, 9493,10442,11487,12635,13899, + 15289,16818,18500,20350,22385,24623,27086,29794,32767 + }; + + private static int ima_get_diff(int step, byte nibble, ref int predictor) + { + int diff = step >> 3; + if ((nibble & 1) != 0) diff += step >> 2; + if ((nibble & 2) != 0) diff += step >> 1; + if ((nibble & 4) != 0) diff += step; + if ((nibble & 7) == 7) diff += step >> 1; + if ((nibble & 8) != 0) diff = -diff; + return diff; + } + + private void ima_process_nibble(byte nibble, ref int step_index, ref int predictor, ref int step) + { + if (step_index < 0) step_index = 0; + if (step_index > ima_step_table.Length - 1) step_index = ima_step_table.Length - 1; + + step = ima_step_table[step_index]; + int diff = ima_get_diff(step, nibble, ref predictor); + + step_index += ima_index_table[nibble & 0x07]; + + predictor = predictor + diff; + if (predictor < Int16.MinValue) predictor = Int16.MinValue; + if (predictor > Int16.MaxValue) predictor = Int16.MaxValue; + } + + protected override void CompressInternal(System.IO.Stream inputStream, System.IO.Stream outputStream) + { + } + + protected override void DecompressInternal(System.IO.Stream inputStream, System.IO.Stream outputStream, int inputLength, int outputLength) + { + StreamAccessor saInput = new StreamAccessor(inputStream); + StreamAccessor saOutput = new StreamAccessor(outputStream); + + Reader br = new Reader(saInput); + Writer bw = new Writer(saOutput); + + // each byte represents two IMA nibbles, which each represent a signed 16-bit PCM sample + // so therefore each byte represents 2 Int16 samples + int predictor = 0; + int step_index = 0; + byte nibble = 0; + + while (!br.EndOfStream) + { + byte next = br.ReadByte(); + int step = 0; + + nibble = (byte)(next & 0x0F); + ima_process_nibble(nibble, ref step_index, ref predictor, ref step); + bw.WriteInt16((short)predictor); + + + nibble = (byte)(next >> 4); + ima_process_nibble(nibble, ref step_index, ref predictor, ref step); + bw.WriteInt16((short)predictor); + } + } + } +}