-
Notifications
You must be signed in to change notification settings - Fork 3
/
residue.go
140 lines (133 loc) · 3.21 KB
/
residue.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package vorbis
import "errors"
type residue struct {
residueType uint16
begin, end uint32
partitionSize uint32
classifications uint8
classbook uint8
cascade []uint8
books [][8]int16
}
func (x *residue) ReadFrom(r *bitReader) error {
x.residueType = r.Read16(16)
if x.residueType > 2 {
return errors.New("vorbis: decoding error")
}
x.begin = r.Read32(24)
x.end = r.Read32(24)
x.partitionSize = r.Read32(24) + 1
x.classifications = r.Read8(6) + 1
x.classbook = r.Read8(8)
x.cascade = make([]uint8, x.classifications)
for i := range x.cascade {
highBits := uint8(0)
lowBits := r.Read8(3)
if r.ReadBool() {
highBits = r.Read8(5)
}
x.cascade[i] = highBits*8 + lowBits
}
x.books = make([][8]int16, x.classifications)
for i := range x.books {
for j := 0; j < 8; j++ {
if x.cascade[i]&(1<<uint(j)) != 0 {
x.books[i][j] = int16(r.Read8(8))
} else {
x.books[i][j] = -1
}
}
}
return nil
}
func (x *residue) Decode(r *bitReader, doNotDecode []bool, n uint32, books []codebook, out [][]float32) {
ch := uint32(len(doNotDecode))
if x.residueType == 2 {
decode := false
for _, not := range doNotDecode {
if !not {
decode = true
break
}
}
if !decode {
return
}
n *= ch
ch = 1
}
begin, end := x.begin, x.end
if begin > n {
begin = n
}
if end > n {
end = n
}
classbook := books[x.classbook]
classWordsPerCodeword := classbook.dimensions
nToRead := end - begin
partitionsToRead := nToRead / x.partitionSize
if nToRead == 0 {
return
}
cs := (partitionsToRead + classWordsPerCodeword)
classifications := make([]uint32, ch*cs)
for pass := 0; pass < 8; pass++ {
partitionCount := uint32(0)
for partitionCount < partitionsToRead {
if pass == 0 {
for j := uint32(0); j < ch; j++ {
if !doNotDecode[j] {
temp := classbook.DecodeScalar(r)
for i := classWordsPerCodeword; i > 0; i-- {
classifications[j*cs+(i-1)+partitionCount] = temp % uint32(x.classifications)
temp /= uint32(x.classifications)
}
}
}
}
for classword := uint32(0); classword < classWordsPerCodeword && partitionCount < partitionsToRead; classword++ {
for j := uint32(0); j < ch; j++ {
if !doNotDecode[j] {
vqclass := classifications[j*cs+partitionCount]
vqbook := x.books[vqclass][pass]
if vqbook != -1 {
book := books[vqbook]
offset := begin + partitionCount*x.partitionSize
switch x.residueType {
case 0:
step := x.partitionSize / book.dimensions
for i := uint32(0); i < step; i++ {
tmp := book.DecodeVector(r)
for k := range tmp {
out[j][offset+i+uint32(k)*step] += tmp[k]
}
}
case 1:
var i uint32
for i < x.partitionSize {
tmp := book.DecodeVector(r)
for k := range tmp {
out[j][offset+i] += tmp[k]
i++
}
}
case 2:
var i uint32
ch := uint32(len(out))
for i < x.partitionSize {
tmp := book.DecodeVector(r)
for k := range tmp {
out[(offset+i)%ch][(offset+i)/ch] += tmp[k]
i++
}
}
}
}
}
}
partitionCount++
}
}
}
}