-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecoder.go
321 lines (305 loc) · 7.28 KB
/
decoder.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package tta
import (
"io"
"os"
"github.com/zyxar/tta/filter"
"github.com/zyxar/tta/wave"
)
type Decoder struct {
codecs [maxNCH]codec // 1 per channel
channels int // number of channels/codecs
data [8]byte // codec initialization data
fifo fifo
passwordSet bool // password protection flag
seekAllowed bool // seek table flag
seekTable []uint64 // the playing position table
format uint32 // tta data format
rate uint32 // bitrate (kbps)
offset uint64 // data start position (header size, bytes)
frames uint32 // total count of frames
depth uint32 // bytes per sample
flenStd uint32 // default frame length in samples
flenLast uint32 // last frame length in samples
flen uint32 // current frame length in samples
fnum uint32 // currently playing frame index
fpos uint32 // the current position in frame
}
func Decompress(infile io.ReadWriteSeeker, outfile io.WriteSeeker, passwd string, cb Callback) (err error) {
decoder := NewDecoder(infile)
if len(passwd) > 0 {
decoder.SetPassword(passwd)
}
info := Info{}
if err = decoder.GetInfo(&info, 0); err != nil {
return
}
smpSize := info.nch * ((info.bps + 7) / 8)
dataSize := info.samples * smpSize
waveHdr := wave.NewHeader(dataSize, uint16(info.nch), info.sps, uint16(info.bps), uint16(smpSize))
if _, err = waveHdr.WriteTo(outfile); err != nil {
return
}
bufSize := pcmBufferLength * smpSize
buffer := make([]byte, bufSize)
var writeLen int
for {
if writeLen = int(uint32(decoder.ProcessStream(buffer, cb)) * smpSize); writeLen == 0 {
break
}
buf := buffer[:writeLen]
if writeLen, err = outfile.Write(buf); err != nil {
return
} else if writeLen != len(buf) {
err = errPartialWritten
return
}
}
return
}
func NewDecoder(iocb io.ReadWriteSeeker) *Decoder {
dec := Decoder{}
dec.fifo.io = iocb
return &dec
}
func (d *Decoder) ProcessStream(out []byte, cb Callback) int32 {
var cache [maxNCH]int32
var value int32
var ret int32
i := 0
outClone := out[:]
for d.fpos < d.flen && len(outClone) > 0 {
value = d.fifo.getValue(&d.codecs[i].adapter)
// decompress stage 1: adaptive hybrid filter
d.codecs[i].filter.Decode(&value)
// decompress stage 2: fixed order 1 prediction
value += ((d.codecs[i].prev * ((1 << 5) - 1)) >> 5)
d.codecs[i].prev = value
cache[i] = value
if i < d.channels-1 {
i++
} else {
if d.channels == 1 {
writeBuffer(value, outClone, d.depth)
outClone = outClone[d.depth:]
} else {
k := i - 1
cache[i] += cache[k] / 2
for k > 0 {
cache[k] = cache[k+1] - cache[k]
k--
}
cache[k] = cache[k+1] - cache[k]
for k <= i {
writeBuffer(cache[k], outClone, d.depth)
outClone = outClone[d.depth:]
k++
}
}
i = 0
d.fpos++
ret++
}
if d.fpos == d.flen {
// check frame crc
crcFlag := !d.fifo.readCrc32()
if crcFlag {
for i := 0; i < len(out); i++ {
out[i] = 0
}
if !d.seekAllowed {
break
}
}
d.fnum++
// update dynamic info
d.rate = (d.fifo.count << 3) / 1070
if cb != nil {
cb(d.rate, d.fnum, d.frames)
}
if d.fnum == d.frames {
break
}
d.frameInit(d.fnum, crcFlag)
}
}
return ret
}
func (d *Decoder) ProcessFrame(inSize uint32, out []byte) int32 {
i := 0
var cache [maxNCH]int32
var value int32
var ret int32
outClone := out[:]
for d.fifo.count < inSize && len(outClone) > 0 {
value = d.fifo.getValue(&d.codecs[i].adapter)
// decompress stage 1: adaptive hybrid filter
d.codecs[i].filter.Decode(&value)
// decompress stage 2: fixed order 1 prediction
value += ((d.codecs[i].prev * ((1 << 5) - 1)) >> 5)
d.codecs[i].prev = value
cache[i] = value
if i < d.channels-1 {
i++
} else {
if d.channels == 1 {
writeBuffer(value, outClone, d.depth)
outClone = outClone[d.depth:]
} else {
j := i
k := i - 1
cache[i] += cache[k] / 2
for k > 0 {
cache[k] = cache[j] - cache[k]
j--
k--
}
cache[k] = cache[j] - cache[k]
for k <= i {
writeBuffer(cache[k], outClone, d.depth)
outClone = outClone[d.depth:]
k++
}
}
i = 0
d.fpos++
ret++
}
if d.fpos == d.flen || d.fifo.count == inSize-4 {
// check frame crc
if !d.fifo.readCrc32() {
for i := 0; i < len(out); i++ {
out[i] = 0
}
}
// update dynamic info
d.rate = (d.fifo.count << 3) / 1070
break
}
}
return ret
}
func (d *Decoder) readSeekTable() bool {
if d.seekTable == nil {
return false
}
d.fifo.reset()
tmp := d.offset + uint64(d.frames+1)*4
for i := uint32(0); i < d.frames; i++ {
d.seekTable[i] = tmp
tmp += uint64(d.fifo.readUint32())
}
return d.fifo.readCrc32()
}
func (d *Decoder) SetPassword(pass string) {
d.data = computeKeyDigits(convertPassword(pass))
d.passwordSet = true
}
func (d *Decoder) frameInit(frame uint32, seekNeeded bool) (err error) {
if frame >= d.frames {
return
}
shift := shifts[d.depth-1]
d.fnum = frame
if seekNeeded && d.seekAllowed {
pos := d.seekTable[d.fnum]
if pos != 0 {
if _, err = d.fifo.io.Seek(int64(pos), os.SEEK_SET); err != nil {
return errSeek
}
}
d.fifo.readStart()
}
if d.fnum == d.frames-1 {
d.flen = d.flenLast
} else {
d.flen = d.flenStd
}
for i := 0; i < d.channels; i++ {
d.codecs[i].filter = filter.New(d.data, shift)
d.codecs[i].adapter.init(10, 10)
d.codecs[i].prev = 0
}
d.fpos = 0
d.fifo.reset()
return
}
func (d *Decoder) frameReset(frame uint32, iocb io.ReadWriteSeeker) {
d.fifo.io = iocb
d.fifo.readStart()
d.frameInit(frame, false)
}
func (d *Decoder) setPosition(seconds uint32) (newPos uint32, err error) {
var frame = (245 * (seconds) / 256)
newPos = (256 * (frame) / 245)
if !d.seekAllowed || frame >= d.frames {
err = errSeek
return
}
d.frameInit(frame, true)
return
}
func (d *Decoder) ReadHeader(info *Info) (uint32, error) {
size := d.fifo.skipId3v2()
d.fifo.reset()
if 'T' != d.fifo.readByte() ||
'T' != d.fifo.readByte() ||
'A' != d.fifo.readByte() ||
'1' != d.fifo.readByte() {
return 0, errFormat
}
info.format = uint32(d.fifo.readUint16())
info.nch = uint32(d.fifo.readUint16())
info.bps = uint32(d.fifo.readUint16())
info.sps = d.fifo.readUint32()
info.samples = d.fifo.readUint32()
if !d.fifo.readCrc32() {
return 0, errFile
}
size += 22
return size, nil
}
func (d *Decoder) GetInfo(info *Info, pos int64) (err error) {
if pos != 0 {
if _, err = d.fifo.io.Seek(pos, os.SEEK_SET); err != nil {
err = errSeek
return
}
}
d.fifo.readStart()
var p uint32
if p, err = d.ReadHeader(info); err != nil {
return
}
if info.format > 2 ||
info.bps < minBPS ||
info.bps > maxBPS ||
info.nch > maxNCH {
return errFormat
}
if info.format == formatEncrypted {
if !d.passwordSet {
return errPassword
}
} else {
// disregard password if file is not encrypted
d.passwordSet = false
d.data = [8]byte{}
}
d.offset = uint64(pos) + uint64(p)
d.format = info.format
d.depth = (info.bps + 7) / 8
d.flenStd = (256 * (info.sps) / 245)
d.flenLast = info.samples % d.flenStd
d.frames = info.samples / d.flenStd
if d.flenLast != 0 {
d.frames++
} else {
d.flenLast = d.flenStd
}
d.rate = 0
d.seekTable = make([]uint64, d.frames)
d.seekAllowed = d.readSeekTable()
d.channels = int(info.nch)
d.frameInit(0, false)
return
}