Skip to content

Commit 35e0007

Browse files
committed
High and low now correctly calculates for 7 bit test case
1 parent 4e9cef0 commit 35e0007

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed

arithmeticDecoder.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
120127
func (arithmeticDecoder *ArithmeticDecoder) intervalGeneration() {
128+
firstBits := arithmeticDecoder.currentInput
129+
fmt.Println("\nFirst 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
}

binaryConversions.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func byteToBitSlice(bytes uint32, length uint8) []bool {
3636
return bits
3737
}
3838

39-
//Takes a length*8 sized bool slice and turns it into a single byte, or 4 bytes if we're converting length
39+
//Takes a length*8 sized bool slice and turns it into a single byte, or 4 bytes if we're converting 32 byte length bitslice
4040
func bitSliceToByte(bitSlice *[]bool, length uint8) []uint8 {
4141
var i uint8 = 0
4242
var j uint8 = 0
@@ -51,6 +51,22 @@ func bitSliceToByte(bitSlice *[]bool, length uint8) []uint8 {
5151

5252
return resultingBytes
5353
}
54+
55+
//Only for testing purposes, takes 7 bits at a time to generate a byte out of it
56+
func arbitraryBitsToByte(bitSlice *[]bool) uint8 {
57+
var resultingByte uint8 = 0
58+
var i uint8 = 0
59+
length := len((*bitSlice))
60+
for i = 0; i < uint8(length); i++ {
61+
if (*bitSlice)[i] {
62+
resultingByte |= 1 << ((uint8(length - 1)) - i)
63+
}
64+
}
65+
return resultingByte
66+
}
67+
68+
//Turn 4 bytes(uint8) into an uint32 by doing bit shifting magic
69+
//Not done in a loop because it kept messing up for no reason.
5470
func bytesToUint32(data []uint8) uint32 {
5571
var returnValue uint32 = 0
5672
returnValue |= uint32(data[3])

binaryIO.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ func readBinaryFile(arithmeticCoder *ArithmeticCoder, filepath string, operation
1616
fmt.Print(fileSize, "\n")
1717
var bufferSize int64
1818
bufferSize = 4096
19-
if fileSize < bufferSize {
19+
//YOLO
20+
if fileSize < bufferSize || operation == "d" {
2021
bufferSize = fileSize
2122
}
2223
var bufferOverflow int64 = 0
@@ -51,7 +52,6 @@ func readBinaryFile(arithmeticCoder *ArithmeticCoder, filepath string, operation
5152
arithmeticCoder.intervalCalculation(data)
5253
}
5354
} else if operation == "d" {
54-
arithmeticDecoder.numberOfAllSymbols = uint32(fileSize)
5555
arithmeticDecoder.readFreqTable(data)
5656

5757
}

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ func main() {
5656
highTable := make([]uint32, 0)
5757
lowTable := make([]uint32, 0)
5858
var symbolInterval uint32 = 0
59-
var step uint32 = 0
6059
output := make([]byte, 0)
60+
currentInputBits := make([]bool, 0)
6161
arithmeticDecoder := &ArithmeticDecoder{inputBits, highTable, lowTable,
62-
symbolInterval, step, 0, upperLimit, output, 0}
62+
symbolInterval, 0, 0, upperLimit, output, 0, currentInputBits}
6363
readBinaryFile(nil, inputFile, operation, false, arithmeticDecoder)
6464
}
6565
}

0 commit comments

Comments
 (0)