-
Notifications
You must be signed in to change notification settings - Fork 3
/
codebook.go
121 lines (111 loc) · 2.88 KB
/
codebook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package vorbis
import (
"errors"
"math"
)
const codebookPattern = 0x564342 //"BCV"
type codebook struct {
dimensions uint32
entries huffmanCode
values []float32
}
func (c *codebook) ReadFrom(r *bitReader) error {
if r.Read32(24) != codebookPattern {
return errors.New("vorbis: decoding error")
}
c.dimensions = r.Read32(16)
numEntries := r.Read32(24)
entries := newHuffmanBuilder(numEntries*2 - 2)
ordered := r.ReadBool()
if !ordered {
sparse := r.ReadBool()
for i := uint32(0); i < numEntries; i++ {
if !sparse || r.ReadBool() {
entries.Put(i, r.Read8(5)+1)
}
}
} else {
currentEntry := uint32(0)
currentLength := r.Read8(5) + 1
for currentEntry < numEntries {
num := r.Read32(ilog(int(numEntries - currentEntry)))
for i := currentEntry; i < currentEntry+num; i++ {
entries.Put(i, currentLength)
}
currentEntry += num
currentLength++
}
}
c.entries = entries.code
lookupType := r.Read8(4)
if lookupType == 0 {
return nil
}
if lookupType > 2 {
return errors.New("vorbis: decoding error")
}
minimumValue := float32Unpack(r.Read32(32))
deltaValue := float32Unpack(r.Read32(32))
valueBits := r.Read8(4) + 1
sequenceP := r.ReadBool()
var multiplicands []uint32
if lookupType == 1 {
multiplicands = make([]uint32, lookup1Values(int(numEntries), c.dimensions))
} else {
multiplicands = make([]uint32, int(numEntries)*int(c.dimensions))
}
for i := range multiplicands {
multiplicands[i] = r.Read32(uint(valueBits))
}
c.values = make([]float32, numEntries*c.dimensions)
for entry := 0; entry < int(numEntries); entry++ {
index := entry * int(c.dimensions)
if lookupType == 1 {
last := float32(0)
indexDivisor := 1
for i := 0; i < int(c.dimensions); i++ {
multiplicandOffset := (entry / indexDivisor) % len(multiplicands)
c.values[index+i] = float32(multiplicands[multiplicandOffset])*deltaValue + minimumValue + last
if sequenceP {
last = c.values[index+i]
}
indexDivisor *= len(multiplicands)
}
} else if lookupType == 2 {
last := float32(0)
for i := 0; i < int(c.dimensions); i++ {
c.values[index+i] = float32(multiplicands[index+i])*deltaValue + minimumValue + last
if sequenceP {
last = c.values[index+i]
}
}
}
}
return nil
}
func (c *codebook) DecodeScalar(r *bitReader) uint32 {
return c.entries.Lookup(r)
}
func (c *codebook) DecodeVector(r *bitReader) []float32 {
index := c.entries.Lookup(r) * c.dimensions
return c.values[index : index+c.dimensions]
}
func ilog(x int) uint {
var r uint
for x > 0 {
r++
x >>= 1
}
return r
}
func lookup1Values(entries int, dim uint32) int {
return int(math.Floor(math.Pow(float64(entries), 1/float64(dim))))
}
func float32Unpack(x uint32) float32 {
mantissa := float64(x & 0x1fffff)
if x&0x80000000 != 0 {
mantissa = -mantissa
}
exponent := (x & 0x7fe00000) >> 21
return float32(math.Ldexp(mantissa, int(exponent)-788))
}