-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdecoder_celt_trace_libopus_test.go
More file actions
352 lines (330 loc) · 11.8 KB
/
Copy pathdecoder_celt_trace_libopus_test.go
File metadata and controls
352 lines (330 loc) · 11.8 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package gopus
import (
"math"
"testing"
"github.com/thesyncim/gopus/internal/libopustest"
)
var libopusCELTTraceHelper libopustest.HelperCache
type libopusCELTTrace struct {
decodedSamples int
internalSamples int
downsample int
overlap int
decodeBuffer int
channel int
start int
finalRange uint32
celtRng uint32
lossDuration int
plcDuration int
postfilter int
postfilterOld int
postfilterGain float32
postfilterGainOld float32
postfilterTapset int
postfilterTapsetOld int
lastPitchPeriod int
lastFrameType int
skipPLC bool
prefilterFold bool
oldBandE []float32
final []float32
preDeemphasis []float32
}
func buildLibopusCELTTraceHelper() (string, error) {
return libopustest.BuildCHelper(libopustest.CHelperConfig{
Label: "CELT decode trace",
OutputBase: "gopus_libopus_celt_trace",
SourceFile: "libopus_celt_trace_single.c",
CFlags: []string{"-DHAVE_CONFIG_H", "-O3", "-DNDEBUG"},
RefIncludes: []string{"src", "celt", "silk", "silk/float"},
Libs: []string{libopustest.RefPath(".libs", "libopus.a"), "-lm"},
DeadStrip: true,
})
}
func traceLibopusCELT(sampleRate, channels, frameSize, targetStep, targetChannel, start, count int, steps []libopusAPIRateDecodeStep) (*libopusCELTTrace, error) {
binPath, err := libopusCELTTraceHelper.Path(buildLibopusCELTTraceHelper)
if err != nil {
return nil, err
}
payload := libopustest.NewOraclePayload("GCTI",
uint32(sampleRate), uint32(channels), uint32(frameSize),
uint32(targetStep), uint32(targetChannel), uint32(start), uint32(count),
uint32(len(steps)))
for _, step := range steps {
if step.fec {
payload.U32(1)
} else {
payload.U32(0)
}
payload.U32(uint32(len(step.packet)))
payload.Raw(step.packet)
}
reader, err := libopustest.RunOracle(binPath, payload.Bytes(), "CELT decode trace", "GCTO")
if err != nil {
return nil, err
}
trace := &libopusCELTTrace{
decodedSamples: int(reader.U32()),
internalSamples: int(reader.U32()),
downsample: int(reader.U32()),
overlap: int(reader.U32()),
decodeBuffer: int(reader.U32()),
channel: int(reader.U32()),
start: int(reader.U32()),
}
outCount := int(reader.U32())
trace.finalRange = reader.U32()
trace.celtRng = reader.U32()
trace.lossDuration = int(reader.U32())
trace.plcDuration = int(reader.U32())
trace.postfilter = int(reader.U32())
trace.postfilterOld = int(reader.U32())
trace.postfilterGain = reader.Float32()
trace.postfilterGainOld = reader.Float32()
trace.postfilterTapset = int(reader.U32())
trace.postfilterTapsetOld = int(reader.U32())
trace.lastPitchPeriod = int(reader.U32())
trace.lastFrameType = int(reader.U32())
trace.skipPLC = reader.U32() != 0
trace.prefilterFold = reader.U32() != 0
oldBandECount := int(reader.U32())
trace.final = make([]float32, outCount)
trace.preDeemphasis = make([]float32, outCount)
trace.oldBandE = make([]float32, oldBandECount)
reader.ExpectRemaining((outCount*2 + oldBandECount) * 4)
for i := range trace.final {
trace.final[i] = reader.Float32()
}
for i := range trace.preDeemphasis {
trace.preDeemphasis[i] = reader.Float32()
}
for i := range trace.oldBandE {
trace.oldBandE[i] = reader.Float32()
}
if err := reader.ExpectConsumed(); err != nil {
return nil, err
}
return trace, nil
}
func TestLibopusCELTTraceMatchesReferenceDecodeWindow(t *testing.T) {
libopustest.RequireOracle(t)
packet := encodeAPIRateCELTPacket(t, 1)
const (
sampleRate = 48000
channels = 1
frameSize = 960
start = 384
count = 64
)
steps := []libopusAPIRateDecodeStep{{packet: packet}}
trace, err := traceLibopusCELT(sampleRate, channels, frameSize, 0, 0, start, count, steps)
if err != nil {
libopustest.HelperUnavailable(t, "CELT decode trace", err)
}
if trace.decodedSamples != frameSize || trace.internalSamples != frameSize {
t.Fatalf("trace samples decoded=%d internal=%d want %d", trace.decodedSamples, trace.internalSamples, frameSize)
}
if trace.downsample != 1 || trace.decodeBuffer != 2048 || trace.overlap <= 0 {
t.Fatalf("trace metadata downsample=%d decodeBuffer=%d overlap=%d", trace.downsample, trace.decodeBuffer, trace.overlap)
}
if trace.channel != 0 || trace.start != start || len(trace.final) != count || len(trace.preDeemphasis) != count {
t.Fatalf("trace window channel=%d start=%d final=%d pre=%d", trace.channel, trace.start, len(trace.final), len(trace.preDeemphasis))
}
want, err := decodeWithLibopusReferenceAPIRateFloat32(sampleRate, channels, frameSize, [][]byte{packet})
if err != nil {
libopustest.HelperUnavailable(t, "api-rate reference decode", err)
}
for i, got := range trace.final {
if got != want[start+i] {
t.Fatalf("trace final[%d]=%08x want %08x", start+i, math.Float32bits(got), math.Float32bits(want[start+i]))
}
}
nonZero := false
for _, sample := range trace.preDeemphasis {
if sample != 0 {
nonZero = true
break
}
}
if !nonZero {
t.Fatal("trace pre-deemphasis window is silent")
}
}
func TestLibopusCELTTraceFinalRangeMatchesDecoder(t *testing.T) {
libopustest.RequireOracle(t)
packet := encodeAPIRateCELTPacket(t, 1)
const (
sampleRate = 48000
channels = 1
frameSize = 960
)
trace, err := traceLibopusCELT(sampleRate, channels, frameSize, 0, 0, 0, 1, []libopusAPIRateDecodeStep{{packet: packet}})
if err != nil {
libopustest.HelperUnavailable(t, "CELT decode trace", err)
}
dec, err := NewDecoder(DefaultDecoderConfig(sampleRate, channels))
if err != nil {
t.Fatalf("NewDecoder: %v", err)
}
out := make([]float32, frameSize)
n, err := dec.Decode(packet, out)
if err != nil {
t.Fatalf("Decode: %v", err)
}
if n != frameSize {
t.Fatalf("Decode samples=%d want %d", n, frameSize)
}
if got := dec.FinalRange(); got != trace.finalRange {
t.Fatalf("FinalRange()=0x%08x want libopus 0x%08x", got, trace.finalRange)
}
if trace.lossDuration != 0 || trace.plcDuration != 0 {
t.Fatalf("unexpected good-frame loss state: loss=%d plc=%d", trace.lossDuration, trace.plcDuration)
}
}
func TestLibopusCELTTracePostfilterStateMatchesDecoder(t *testing.T) {
libopustest.RequireOracle(t)
packet := encodeAPIRateCELTPacket(t, 1)
const (
sampleRate = 48000
channels = 1
frameSize = 960
)
steps := []libopusAPIRateDecodeStep{{packet: packet}, {}}
goodTrace, err := traceLibopusCELT(sampleRate, channels, frameSize, 0, 0, 0, 1, steps)
if err != nil {
libopustest.HelperUnavailable(t, "CELT decode trace", err)
}
lossTrace, err := traceLibopusCELT(sampleRate, channels, frameSize, 1, 0, 0, 1, steps)
if err != nil {
libopustest.HelperUnavailable(t, "CELT decode trace", err)
}
dec, err := NewDecoder(DefaultDecoderConfig(sampleRate, channels))
if err != nil {
t.Fatalf("NewDecoder: %v", err)
}
out := make([]float32, frameSize)
if _, err := dec.Decode(packet, out); err != nil {
t.Fatalf("Decode good: %v", err)
}
goodState := dec.celtDecoder.PostfilterState()
if got, want := goodState.Period, goodTrace.postfilter; got != want {
t.Fatalf("good postfilterPeriod=%d want libopus %d", got, want)
}
if got, want := goodState.PreviousPeriod, goodTrace.postfilterOld; got != want {
t.Fatalf("good postfilterPeriodOld=%d want libopus %d", got, want)
}
if got, want := goodState.Gain, goodTrace.postfilterGain; math.Float32bits(got) != math.Float32bits(want) {
t.Fatalf("good postfilterGain=%08x want libopus %08x", math.Float32bits(got), math.Float32bits(want))
}
if got, want := goodState.PreviousGain, goodTrace.postfilterGainOld; math.Float32bits(got) != math.Float32bits(want) {
t.Fatalf("good postfilterGainOld=%08x want libopus %08x", math.Float32bits(got), math.Float32bits(want))
}
if got, want := goodState.Tapset, goodTrace.postfilterTapset; got != want {
t.Fatalf("good postfilterTapset=%d want libopus %d", got, want)
}
if got, want := goodState.PreviousTapset, goodTrace.postfilterTapsetOld; got != want {
t.Fatalf("good postfilterTapsetOld=%d want libopus %d", got, want)
}
if _, err := dec.Decode(nil, out); err != nil {
t.Fatalf("Decode loss: %v", err)
}
lossState := dec.celtDecoder.PostfilterState()
if got, want := lossState.Period, lossTrace.postfilter; got != want {
t.Fatalf("loss postfilterPeriod=%d want libopus %d", got, want)
}
if got, want := lossState.PreviousPeriod, lossTrace.postfilterOld; got != want {
t.Fatalf("loss postfilterPeriodOld=%d want libopus %d", got, want)
}
if got, want := lossState.Gain, lossTrace.postfilterGain; math.Float32bits(got) != math.Float32bits(want) {
t.Fatalf("loss postfilterGain=%08x want libopus %08x", math.Float32bits(got), math.Float32bits(want))
}
if got, want := lossState.PreviousGain, lossTrace.postfilterGainOld; math.Float32bits(got) != math.Float32bits(want) {
t.Fatalf("loss postfilterGainOld=%08x want libopus %08x", math.Float32bits(got), math.Float32bits(want))
}
if got, want := lossState.Tapset, lossTrace.postfilterTapset; got != want {
t.Fatalf("loss postfilterTapset=%d want libopus %d", got, want)
}
if got, want := lossState.PreviousTapset, lossTrace.postfilterTapsetOld; got != want {
t.Fatalf("loss postfilterTapsetOld=%d want libopus %d", got, want)
}
}
func TestLibopusCELTTracePLCStateMatchesDecoder(t *testing.T) {
libopustest.RequireOracle(t)
packet := encodeAPIRateCELTPacket(t, 2)
const (
sampleRate = 48000
channels = 2
frameSize = 960
)
steps := []libopusAPIRateDecodeStep{{packet: packet}, {}}
trace, err := traceLibopusCELT(sampleRate, channels, frameSize, 1, 1, 0, 1, steps)
if err != nil {
libopustest.HelperUnavailable(t, "CELT decode trace", err)
}
dec, err := NewDecoder(DefaultDecoderConfig(sampleRate, channels))
if err != nil {
t.Fatalf("NewDecoder: %v", err)
}
out := make([]float32, frameSize*channels)
if _, err := dec.Decode(packet, out); err != nil {
t.Fatalf("Decode good: %v", err)
}
if _, err := dec.Decode(nil, out); err != nil {
t.Fatalf("Decode loss: %v", err)
}
state := dec.celtDecoder.SnapshotPLCState()
if state.LossDuration != trace.lossDuration {
t.Fatalf("lossDuration=%d want libopus %d", state.LossDuration, trace.lossDuration)
}
if state.PLCDuration != trace.plcDuration {
t.Fatalf("plcDuration=%d want libopus %d", state.PLCDuration, trace.plcDuration)
}
if state.LastFrameType != trace.lastFrameType {
t.Fatalf("lastFrameType=%d want libopus %d", state.LastFrameType, trace.lastFrameType)
}
if state.LastPitchPeriod != trace.lastPitchPeriod {
t.Fatalf("lastPitchPeriod=%d want libopus %d", state.LastPitchPeriod, trace.lastPitchPeriod)
}
if state.SkipPLC != trace.skipPLC {
t.Fatalf("skipPLC=%v want libopus %v", state.SkipPLC, trace.skipPLC)
}
if state.PrefilterAndFold != trace.prefilterFold {
t.Fatalf("prefilterAndFold=%v want libopus %v", state.PrefilterAndFold, trace.prefilterFold)
}
}
func TestLibopusCELTTraceEnergyStateMatchesDecoder(t *testing.T) {
libopustest.RequireOracle(t)
packet := encodeAPIRateCELTPacket(t, 1)
const (
sampleRate = 48000
channels = 1
frameSize = 960
)
trace, err := traceLibopusCELT(sampleRate, channels, frameSize, 0, 0, 0, 1, []libopusAPIRateDecodeStep{{packet: packet}})
if err != nil {
libopustest.HelperUnavailable(t, "CELT decode trace", err)
}
dec, err := NewDecoder(DefaultDecoderConfig(sampleRate, channels))
if err != nil {
t.Fatalf("NewDecoder: %v", err)
}
out := make([]float32, frameSize)
if _, err := dec.Decode(packet, out); err != nil {
t.Fatalf("Decode: %v", err)
}
gotEnergy := dec.celtDecoder.PrevEnergy()
if len(trace.oldBandE) < len(gotEnergy) {
t.Fatalf("trace oldBandE len=%d want at least %d", len(trace.oldBandE), len(gotEnergy))
}
for i, got := range gotEnergy {
got32 := float32(got)
want := trace.oldBandE[i]
if math.Float32bits(got32) != math.Float32bits(want) {
t.Fatalf("oldBandE[%d]=%08x %.10g want %08x %.10g",
i,
math.Float32bits(got32), got32,
math.Float32bits(want), want)
}
}
}