@@ -34,14 +34,20 @@ func (arithmeticDecoder *ArithmeticDecoder) readFreqTable(data []uint8) {
3434 readBytes := data [i - 4 : i ]
3535 convertedInt := bytesToUint32 (readBytes )
3636 highTable [(i - 4 )/ 4 ] = convertedInt
37+
3738 }
3839 fmt .Println ("Read high table" , highTable )
3940 arithmeticDecoder .highTable = highTable
41+ //A temporary variable to avoid ovewriting
4042 sortedHighTable := make ([]uint32 , 256 )
4143 copy (sortedHighTable , highTable )
4244 sort .Slice (sortedHighTable , func (i , j int ) bool { return sortedHighTable [i ] < sortedHighTable [j ] })
45+ //The largest high(last one in the sorted array) tells us how many non-unique symbols we have
46+ arithmeticDecoder .numberOfAllSymbols = sortedHighTable [255 ]
47+ arithmeticDecoder .step = (arithmeticDecoder .high + 1 ) / arithmeticDecoder .numberOfAllSymbols
4348 arithmeticDecoder .generateLowTable (sortedHighTable )
4449 arithmeticDecoder .initializeField (data )
50+ arithmeticDecoder .intervalGeneration ()
4551 fmt .Println ("High table " , arithmeticDecoder .highTable )
4652 fmt .Println ("Low table " , arithmeticDecoder .lowTable )
4753 fmt .Println ("Read bits " , arithmeticDecoder .inputBits )
@@ -85,12 +91,13 @@ func (arithmeticDecoder *ArithmeticDecoder) initializeField(data []uint8) {
8591 bitSlice := make ([]bool , 0 )
8692 fmt .Println (arithmeticDecoder .numberOfAllSymbols )
8793 //Load every bit, that is not in the model(first 256*4 bytes) onwards
88- for i = 256 * 4 ; i < arithmeticDecoder . numberOfAllSymbols ; i ++ {
94+ for i = 256 * 4 ; i < uint32 ( len ( data )) ; i ++ {
8995 currByte := data [i ]
90- fmt .Println ("currByte" , currByte )
9196 bitSlice = append (bitSlice , byteToBitSlice (uint32 (currByte ), 8 )... )
9297 }
9398 arithmeticDecoder .inputBits = bitSlice
99+ //First 7 bits
100+ arithmeticDecoder .currentInput = bitSlice [0 :7 ]
94101
95102}
96103
@@ -118,5 +125,34 @@ func (arithmeticDecoder *ArithmeticDecoder) initializeField(data []uint8) {
118125 - field = 2*(field - 1stQuarter) + next bit
119126*/
120127func (arithmeticDecoder * ArithmeticDecoder ) intervalGeneration () {
128+ firstBits := arithmeticDecoder .currentInput
129+ fmt .Println ("\n First bits\n " , firstBits )
130+ firstByte := arbitraryBitsToByte (& firstBits )
131+ numberOfAllSymbols := arithmeticDecoder .numberOfAllSymbols
132+ fmt .Println (firstByte )
133+ for i := 256 * 4 ; uint32 (i ) < 1024 + arithmeticDecoder .numberOfAllSymbols ; i ++ {
134+ low := arithmeticDecoder .low
135+ high := arithmeticDecoder .high
136+ step := arithmeticDecoder .step
137+ currentBits := arithmeticDecoder .currentInput
138+ currentByte := arbitraryBitsToByte (& currentBits )
139+ symbolInterval := (uint32 (currentByte ) - low ) / step
140+ //Calculate the symbol that relates to the symbolInterval
141+ symbol := arithmeticDecoder .intervalToSymbol (symbolInterval )
142+ step = (high - low + 1 ) / numberOfAllSymbols
143+ high = low + step * arithmeticDecoder .highTable [symbol ] - 1
144+ fmt .Println ("Step" , step , " v " , symbolInterval , " symbol: " , symbol , " high " , high , " low " , low )
145+
146+ }
147+ }
121148
149+ //Returns the symbol that is represented by an interval numbe
150+ func (arithmeticDecoder * ArithmeticDecoder ) intervalToSymbol (symbolInterval uint32 ) uint8 {
151+ for i := 0 ; i < 256 ; i ++ {
152+ //If the interval is found anywhere
153+ if arithmeticDecoder .highTable [i ] > symbolInterval && arithmeticDecoder .lowTable [i ] <= symbolInterval {
154+ return uint8 (i )
155+ }
156+ }
157+ return 0
122158}
0 commit comments