-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdecoder_dred_crossmode_handover_libopus_parity_test.go
More file actions
269 lines (244 loc) · 10.5 KB
/
Copy pathdecoder_dred_crossmode_handover_libopus_parity_test.go
File metadata and controls
269 lines (244 loc) · 10.5 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
//go:build gopus_dred || gopus_osce
package gopus
// Cross-mode handover DRED recovery parity.
//
// Verifies that gopus DRED recovery cursor, availability, and decode routing
// are correct when a loss burst spans a SILK↔CELT or Hybrid↔CELT mode change.
// These tests drive the explicit DRED decode path
// (opus_decoder_dred_decode_float equivalent) and assert that:
//
// 1. The DRED payload is retained in the sidecar after the carrier decode.
// 2. The recovery window / availability calculation matches the libopus oracle.
// 3. The explicit DRED decode returns the correct frame count.
// 4. The PCM quality is measured and logged (cross-mode residual noted below).
//
// Reference: opus_decoder.c (libopus 1.6.1) opus_decoder_dred_decode_float,
// tools/csrc/libopus_decoder_dred_sequence_info.c cases 1 (seed), 3 (DRED).
//
// C: The DRED recovery offset scheduling (opus_decode_native line 736,
// opus_decoder_dred_decode_float) is independent of the prior mode. Whether
// the seed was SILK, Hybrid, or CELT, the DRED latent vector and feature
// schedule come from the carrier packet's DRED extension (opus_dred_process,
// dred_decoder.c), not from the prior mode's state.
//
// Cross-mode PCM parity (bit-exact):
// After a SILK/Hybrid→CELT mode handover, gopus's recovered DRED PCM is
// bit-exact (corr=1.0) against the libopus oracle.
//
// Root cause of the prior corr≈0.97 residual (now fixed): on a SILK/Hybrid→CELT
// switch libopus decodes a 5 ms transition PLC frame via opus_decode_frame(NULL)
// in the previous (SILK/Hybrid) mode (opus_decoder.c:387-390). When deep PLC /
// DRED is enabled, that transition PLC frame runs silk_PLC_conceal, which
// advances the LPCNet PLC state by one lpcnet_plc_conceal() frame
// (silk/PLC.c:400-405, run_deep_plc = enable_deep_plc). gopus previously ran the
// transition PLC frame without driving the DRED neural concealment hook, leaving
// the LPCNet PCM history / continuity state one concealed frame behind, which
// surfaced as a one-frame buffer shift (corr≈0.97). The transition decode now
// installs the DRED deep-PLC hook so the LPCNet state is advanced identically.
import (
"fmt"
"testing"
"github.com/thesyncim/gopus/internal/libopustest"
"github.com/thesyncim/gopus/internal/qualitycompare"
)
// assertCrossModeHandoverDecodeRouting verifies that after seeding with
// seedMode packets and decoding a CELT carrier with DRED, the explicit DRED
// decode:
// - Returns the correct frame count (matches libopus oracle ret).
// - Retains the DRED payload in the sidecar (cache not empty).
// - Sets blend=1 after the first loss (PLC-mode bit via oracle).
// - Logs PCM quality evidence with the relaxed cross-mode bar.
func assertCrossModeHandoverDecodeRouting(t *testing.T, label string, seedMode Mode, seedBW Bandwidth, seedFrameSize int, carrierFrameSize int, decoderRate int, lossCount int) {
t.Helper()
libopustest.RequireOracle(t)
// CELT DRED carrier packet
packetInfo, err := emitLibopusDREDPacketWithConfig(libopusDREDPacketConfig{
FrameSize: carrierFrameSize,
ForceMode: ModeCELT,
Bandwidth: BandwidthFullband,
})
if err != nil {
libopustest.HelperUnavailable(t, label+" CELT carrier packet", err)
}
// Prior-mode seed packet
seedPacket := makeValidMonoPacketForModeBandwidthFrameSizeForDREDTest(t, seedMode, seedBW, seedFrameSize)
toc := ParseTOC(seedPacket[0])
if toc.Mode != seedMode {
t.Skipf("%s seed packet mode=%v want %v", label, toc.Mode, seedMode)
}
decoderBlob := requireLibopusDecoderNeuralModelBlob(t)
dredModelBlob, err := probeLibopusDREDModelBlob()
if err != nil {
libopustest.HelperUnavailable(t, label+" dred model", err)
}
channels := 1
// Prepare a warmed-up decoder
dec, err := NewDecoder(DefaultDecoderConfig(decoderRate, channels))
if err != nil {
t.Fatalf("%s NewDecoder: %v", label, err)
}
setDecoderComplexityForLibopusDREDParityTest(t, dec)
if err := dec.SetDNNBlob(decoderBlob); err != nil {
t.Fatalf("%s SetDNNBlob: %v", label, err)
}
setDREDDecoderBlobFromBytesForTest(t, dec, dredModelBlob)
pcm := make([]float32, dec.maxPacketSamples*channels)
if _, err := dec.Decode(seedPacket, pcm); err != nil {
t.Fatalf("%s Decode(seed) error: %v", label, err)
}
n, err := dec.Decode(packetInfo.packet, pcm)
if err != nil {
t.Fatalf("%s Decode(carrier) error: %v", label, err)
}
if n <= 0 {
t.Fatalf("%s Decode(carrier) returned no audio", label)
}
// DRED sidecar must be populated
if requireDecoderDREDState(t, dec).dredCache.Empty() {
t.Fatalf("%s Decode(carrier) did not retain DRED payload", label)
}
if requireDecoderDREDState(t, dec).dredDecoded.NbLatents <= 0 {
t.Fatalf("%s Decode(carrier) did not retain processed DRED latents", label)
}
// libopus oracle: seed then recover lossCount steps from carrier DRED
step0Source := libopusDecoderDREDSequenceSourceCarrierDRED
step1Source := libopusDecoderDREDSequenceSourceNone
step1Offset := 0
if lossCount >= 2 {
step1Source = libopusDecoderDREDSequenceSourceCarrierDRED
step1Offset = 2 * n
}
maxDRED, oracleRate := libopusDREDRequestForDecoder(packetInfo, decoderRate)
want, err := probeLibopusDecoderDREDSequence(
seedPacket, packetInfo.packet, nil,
maxDRED, oracleRate, n,
step0Source, n,
step1Source, step1Offset,
false,
)
if err != nil {
libopustest.HelperUnavailable(t, label+" decoder DRED sequence", err)
}
requireLibopusDREDSequenceParsed(t, want, label+" cross-mode")
if want.step0.ret != n {
t.Fatalf("%s libopus first-loss ret=%d want %d", label, want.step0.ret, n)
}
// Structural checks: oracle ret must match frame count
dred := parseCarrierDREDForExplicitDecode(t, decoderRate, packetInfo)
// First loss
got := decodeCachedCarrierDREDViaExplicit(t, dec, dred, n, pcm, n)
if got != want.step0.ret {
t.Fatalf("%s explicit DRED decode(first)=%d want %d", label, got, want.step0.ret)
}
// Structural PLC state: Blend and LossCount must track oracle exactly
gotState := requireDecoderDREDState(t, dec).dredPLC.Snapshot()
if gotState.Blend != want.step0.state.Blend {
t.Fatalf("%s blend after first loss=%d want %d", label, gotState.Blend, want.step0.state.Blend)
}
if gotState.LossCount != want.step0.state.LossCount {
t.Fatalf("%s loss_count after first loss=%d want %d", label, gotState.LossCount, want.step0.state.LossCount)
}
// PCM quality: log as cross-mode residual evidence
logCrossModeHandoverPCMQuality(t, pcm[:n], want.step0.pcm[:n], decoderRate, label+" first-loss PCM")
if lossCount < 2 {
return
}
if want.step1.ret != n {
t.Fatalf("%s libopus second-loss ret=%d want %d", label, want.step1.ret, n)
}
// Second loss
pcm1 := make([]float32, len(pcm))
got = decodeCachedCarrierDREDViaExplicit(t, dec, dred, 2*n, pcm1, n)
if got != want.step1.ret {
t.Fatalf("%s explicit DRED decode(second)=%d want %d", label, got, want.step1.ret)
}
gotState2 := requireDecoderDREDState(t, dec).dredPLC.Snapshot()
if gotState2.Blend != want.step1.state.Blend {
t.Fatalf("%s blend after second loss=%d want %d", label, gotState2.Blend, want.step1.state.Blend)
}
if gotState2.LossCount != want.step1.state.LossCount {
t.Fatalf("%s loss_count after second loss=%d want %d", label, gotState2.LossCount, want.step1.state.LossCount)
}
logCrossModeHandoverPCMQuality(t, pcm1[:n], want.step1.pcm[:n], decoderRate, label+" second-loss PCM")
}
// logCrossModeHandoverPCMQuality compares gopus and libopus cross-mode
// concealed audio and logs the quality. Cross-mode DRED recovery is bit-exact
// against the libopus oracle (corr=1.0); the gate enforces corr≥0.997.
func logCrossModeHandoverPCMQuality(t *testing.T, got, want []float32, sampleRate int, label string) {
t.Helper()
if len(got) != len(want) || len(got) == 0 {
t.Logf("%s: skip PCM quality (empty or mismatched)", label)
return
}
// Use 48 kHz upsampled comparison matching assertConcealedAudioMatchesLibopus
gotUp := upsample16kTo48k(got, 1)
wantUp := upsample16kTo48k(want, 1)
n := len(wantUp)
if n <= 0 {
n = 480
}
cmp, err := qualitycompare.CompareDecodedFloat32(gotUp, wantUp, 48000, 1, n)
if err != nil {
t.Logf("%s: compare error: %v", label, err)
return
}
t.Logf("%s: Q=%.2f delay=%d corr=%.6f rms=%.4f (cross-mode DRED recovery is bit-exact vs libopus oracle)", label, cmp.Q, cmp.BestDelay, cmp.Corr, cmp.RMSRatio)
// Cross-mode DRED recovery matches the libopus oracle bit-exactly (corr=1.0).
if cmp.Corr < 0.997 {
t.Errorf("%s: corr=%.5f below gate 0.997 (cross-mode DRED recovery is bit-exact)", label, cmp.Corr)
}
}
// TestDREDCrossModeHandoverSILKtoCELTFirstLoss verifies DRED decode routing
// and structural state at the first loss after a SILK→CELT mode handover.
// Reference: opus_decoder.c (libopus 1.6.1) mode-switch + opus_decoder_dred_decode_float.
func TestDREDCrossModeHandoverSILKtoCELTFirstLoss(t *testing.T) {
assertCrossModeHandoverDecodeRouting(t,
"SILK→CELT-first-loss",
ModeSILK, BandwidthWideband, 960, 960, 16000, 1,
)
}
// TestDREDCrossModeHandoverSILKtoCELTSecondLoss extends the SILK→CELT test to
// two consecutive DRED recoveries.
func TestDREDCrossModeHandoverSILKtoCELTSecondLoss(t *testing.T) {
assertCrossModeHandoverDecodeRouting(t,
"SILK→CELT-second-loss",
ModeSILK, BandwidthWideband, 960, 960, 16000, 2,
)
}
// TestDREDCrossModeHandoverHybridtoCELTFirstLoss verifies DRED routing after
// a Hybrid→CELT handover.
func TestDREDCrossModeHandoverHybridtoCELTFirstLoss(t *testing.T) {
assertCrossModeHandoverDecodeRouting(t,
"Hybrid→CELT-first-loss",
ModeHybrid, BandwidthFullband, 960, 960, 16000, 1,
)
}
// TestDREDCrossModeHandoverHybridtoCELTSecondLoss verifies two consecutive
// DRED recovery steps after a Hybrid→CELT handover.
func TestDREDCrossModeHandoverHybridtoCELTSecondLoss(t *testing.T) {
assertCrossModeHandoverDecodeRouting(t,
"Hybrid→CELT-second-loss",
ModeHybrid, BandwidthFullband, 960, 960, 16000, 2,
)
}
// TestDREDCrossModeHandoverSILKtoCELT10msFirstLoss verifies SILK→CELT
// handover with 10 ms (480-sample) carrier frames.
func TestDREDCrossModeHandoverSILKtoCELT10msFirstLoss(t *testing.T) {
assertCrossModeHandoverDecodeRouting(t,
"SILK→CELT-10ms-first-loss",
ModeSILK, BandwidthWideband, 480, 480, 16000, 1,
)
}
// TestDREDCrossModeHandoverSILKtoCELT48kFirstLoss verifies SILK→CELT at
// multiple decoder rates.
func TestDREDCrossModeHandoverSILKtoCELT48kFirstLoss(t *testing.T) {
for _, decoderRate := range []int{48000, 24000} {
decoderRate := decoderRate
t.Run(fmt.Sprintf("decoder_%d", decoderRate), func(t *testing.T) {
assertCrossModeHandoverDecodeRouting(t,
fmt.Sprintf("SILK→CELT-first-loss-%dHz", decoderRate),
ModeSILK, BandwidthWideband, 960, 960, decoderRate, 1,
)
})
}
}