-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecode_differential_fuzz_test.go
More file actions
564 lines (533 loc) · 18.3 KB
/
Copy pathdecode_differential_fuzz_test.go
File metadata and controls
564 lines (533 loc) · 18.3 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
// decode_differential_fuzz_test.go — differential fuzz harness comparing gopus
// decode against the libopus C decoder oracle over a large seeded space of
// generated and structured-malformed Opus packets.
//
// Two complementary strategies (both seeded + CI-budgeted):
//
// (a) ENCODE-then-DECODE: gopus encodes deterministic + random PCM across the
// config space (mode/bandwidth/frame duration/bitrate/channels/FEC/DTX),
// producing VALID packets, then decodes each through BOTH gopus and the
// libopus oracle and asserts identical PCM. amd64 is bit-exact; the
// documented darwin/arm64 ≤1-ULP CELT/Hybrid float drift is absorbed by a
// tiny tolerance on those modes only (see project_arm64_celt_1ulp_drift).
//
// (b) STRUCTURED-MALFORMED: seeded mutations of valid packets (truncate, TOC
// flip, frame-length corruption, padding edge cases, code-0/1/2/3
// boundaries) are decoded through both. The invariant is AGREEMENT: either
// both reject (gopus error class ↔ libopus negative code) or both accept
// with identical PCM. A gopus accept-where-libopus-rejects (or vice versa)
// or a panic is a divergence to root-cause.
//
// Both decode each packet through a FRESH decoder (oracle isolates per case; the
// Go side makes a new Decoder per case) so any failure minimises to one packet.
//
// Run with GOPUS_TEST_TIER=parity GOPUS_STRICT_LIBOPUS_REF=1 for the full sweep.
//
// Scope: this harness hardens the DECODE path. Strategy (a) drives gopus's own
// encoder to produce inputs; an encoder error or panic means no valid packet can
// be generated for that config, so the spec is logged as an encoder-side finding
// and skipped (it is not a decode divergence).
package gopus
import (
"fmt"
"math"
"math/rand"
"runtime"
"testing"
"github.com/thesyncim/gopus/internal/libopustest"
)
// diffFuzzBudget returns the per-stage iteration budget, shrunk under -short so
// the harness stays CI-friendly while remaining a substantial sweep otherwise.
func diffFuzzBudget(full int) int {
if testing.Short() {
b := max(full/8, 16)
return b
}
return full
}
// ---- gopus-side decode dispatch -------------------------------------------
// gopusDecodeProbe decodes one packet through a fresh gopus decoder, mirroring
// the oracle's fresh-decoder-per-case isolation. It returns the decoded PCM as
// float32 (converting int16/int24 to the same 1/32768 / 1/8388608 scale the
// oracle PCM is compared at), the per-channel sample count, and any error.
func gopusDecodeProbe(sampleRate, channels int, c libopustest.DecodeDiffCase) (pcm []float32, samples int, err error) {
dec, derr := NewDecoder(DefaultDecoderConfig(sampleRate, channels))
if derr != nil {
return nil, 0, derr
}
bufSamples := int(c.FrameSize)
if bufSamples == 0 {
bufSamples = 5760
}
switch c.Format {
case libopustest.DecodeDiffFormatInt16:
buf := make([]int16, bufSamples*channels)
n, e := dec.DecodeInt16(c.Packet, buf)
if e != nil {
return nil, 0, e
}
out := make([]float32, n*channels)
for i := range out {
out[i] = float32(buf[i]) / 32768.0
}
return out, n, nil
case libopustest.DecodeDiffFormatInt24:
buf := make([]int32, bufSamples*channels)
n, e := dec.DecodeInt24(c.Packet, buf)
if e != nil {
return nil, 0, e
}
out := make([]float32, n*channels)
for i := range out {
out[i] = float32(buf[i]) / 8388608.0
}
return out, n, nil
default:
buf := make([]float32, bufSamples*channels)
n, e := dec.Decode(c.Packet, buf)
if e != nil {
return nil, 0, e
}
return buf[:n*channels], n, nil
}
}
// oracleResultToFloat32 normalises the oracle's PCM to the same float32 scale as
// gopusDecodeProbe.
func oracleResultToFloat32(format uint32, r libopustest.DecodeDiffResult) []float32 {
switch format {
case libopustest.DecodeDiffFormatInt16:
in := r.Int16()
out := make([]float32, len(in))
for i, v := range in {
out[i] = float32(v) / 32768.0
}
return out
case libopustest.DecodeDiffFormatInt24:
in := r.Int24()
out := make([]float32, len(in))
for i, v := range in {
out[i] = float32(v) / 8388608.0
}
return out
default:
return r.Float32()
}
}
func absF32(v float32) float32 {
if v < 0 {
return -v
}
return v
}
// diffMode classifies a TOC byte's coding mode for tolerance selection.
type diffMode int
const (
diffModeSILK diffMode = iota
diffModeHybrid
diffModeCELT
)
func tocMode(toc byte) diffMode {
config := int(toc >> 3)
switch {
case config < 12:
return diffModeSILK
case config < 16:
return diffModeHybrid
default:
return diffModeCELT
}
}
// pcmExactTolerance returns the max absolute per-sample float32 difference
// tolerated for a packet, in the shared float32 comparison scale.
//
// On the amd64 asm/SIMD build the requirement is bit-exact for every mode and
// format (tolerance 0): gopus's SSE/AVX kernels are tuned to match the SIMD
// libopus the default oracle build links on amd64, so exactness is the gate.
//
// On every pure-Go (-tags purego) build and on darwin/arm64 the documented
// ≤1-ULP float drift (project_arm64_celt_1ulp_drift) applies. The pure-Go float
// path does not reproduce the SIMD libopus reference bit-for-bit (CELT/hybrid
// IMDCT, synthesis, and deemphasis round a few LSB at the ~1/32768 quantum;
// the SILK stereo MS->LR multiply drifts a few ULP), so the same per-arch budget
// is applied uniformly there: a few /32768 for float32/int16 and a matching
// int24 band. This is the documented per-arch budget, not a mask -- the amd64
// asm build stays exact, and the bound (~1.2e-4) is three orders of magnitude
// below any real divergence (the fixed SILK LBRR desync produced ~1.0-2.0).
func pcmExactTolerance(toc byte, format uint32) float32 {
if runtime.GOARCH == "amd64" && !testPuregoBuild {
return 0
}
switch format {
case libopustest.DecodeDiffFormatInt24:
// int24 quantum is 1/8388608; the float drift maps to the same ~4/32768
// band. Conversion-overflow samples (|x|>=256) are skipped in pcmDiffWorst.
return 4.0 / 32768.0
default: // float32, int16
return 4.0 / 32768.0
}
}
// pcmDiffWorst returns the worst tolerated-scale per-sample |Δ| between gopus and
// oracle PCM, the index, the tolerance, and whether they are within tolerance. It
// does not touch *testing.T so callers can decide how to report (hard fail vs
// allow-listed residual).
func pcmDiffWorst(toc byte, format uint32, got, want []float32) (worst float32, worstIdx int, tol float32, ok bool) {
worstIdx = -1
if len(got) != len(want) {
return 0, -1, 0, false
}
tol = pcmExactTolerance(toc, format)
// int24 conversion (RES2INT24 = float2int(32768*256*x)) overflows int32 once
// |x| >= 256, where both libopus' lrintf and Go's int32() cast are
// implementation-defined. Real audio never reaches this; it only arises from
// pathological random-encoded content that decodes to hundreds× full scale.
// float32/int16 stay exact there, so skip int24 samples in the overflow band
// rather than compare two undefined-behaviour saturations.
int24Overflow := format == libopustest.DecodeDiffFormatInt24
const int24OverflowMag = 250.0 // safe margin below the 256.0 int32-overflow point
for i := range got {
if int24Overflow && (absF32(got[i]) >= int24OverflowMag || absF32(want[i]) >= int24OverflowMag) {
continue
}
d := absF32(got[i] - want[i])
if d > worst {
worst = d
worstIdx = i
}
}
return worst, worstIdx, tol, worst <= tol
}
// assertDiffPCM compares gopus vs oracle PCM for a single accepted packet and
// reports a divergence via t.Errorf. Used by the encode-then-decode sweep, where
// every packet is valid and PCM must match within the per-arch tolerance.
func assertDiffPCM(t *testing.T, label string, toc byte, format uint32, got, want []float32) bool {
t.Helper()
if len(got) != len(want) {
t.Errorf("%s: PCM length gopus=%d libopus=%d", label, len(got), len(want))
return false
}
worst, worstIdx, tol, ok := pcmDiffWorst(toc, format, got, want)
if !ok {
t.Errorf("%s: PCM diverges (worst |Δ|=%g at sample %d, tol=%g, toc=0x%02x mode=%d)",
label, worst, worstIdx, tol, toc, tocMode(toc))
return false
}
return true
}
// ---- (a) encode-then-decode sweep -----------------------------------------
// encodeSweepSpec is one point in the encoder configuration space.
type encodeSweepSpec struct {
name string
application Application
mode EncoderMode
bandwidth Bandwidth
autoBW bool
frameMs ExpertFrameDuration
bitrate int
channels int
vbr BitrateMode
fec bool
dtx bool
}
func (s encodeSweepSpec) frameSamples48k() int {
switch s.frameMs {
case ExpertFrameDuration2_5Ms:
return 120
case ExpertFrameDuration5Ms:
return 240
case ExpertFrameDuration10Ms:
return 480
case ExpertFrameDuration20Ms:
return 960
case ExpertFrameDuration40Ms:
return 1920
case ExpertFrameDuration60Ms:
return 2880
default:
return 960
}
}
// buildEncodeSweep enumerates the structured encoder config matrix.
func buildEncodeSweep() []encodeSweepSpec {
var specs []encodeSweepSpec
type modeDef struct {
name string
app Application
mode EncoderMode
bw Bandwidth
mins int // min frame ms index
}
modes := []modeDef{
{"silk_nb", ApplicationRestrictedSilk, EncoderModeSILK, BandwidthNarrowband, 0},
{"silk_mb", ApplicationRestrictedSilk, EncoderModeSILK, BandwidthMediumband, 0},
{"silk_wb", ApplicationRestrictedSilk, EncoderModeSILK, BandwidthWideband, 0},
{"hybrid_swb", ApplicationVoIP, EncoderModeHybrid, BandwidthSuperwideband, 0},
{"hybrid_fb", ApplicationVoIP, EncoderModeHybrid, BandwidthFullband, 0},
{"celt_nb", ApplicationRestrictedCelt, EncoderModeCELT, BandwidthNarrowband, 0},
{"celt_wb", ApplicationRestrictedCelt, EncoderModeCELT, BandwidthWideband, 0},
{"celt_swb", ApplicationRestrictedCelt, EncoderModeCELT, BandwidthSuperwideband, 0},
{"celt_fb", ApplicationRestrictedCelt, EncoderModeCELT, BandwidthFullband, 0},
}
// SILK supports 10/20/40/60 ms; CELT supports 2.5/5/10/20 ms; Hybrid 10/20 ms.
silkFrames := []ExpertFrameDuration{ExpertFrameDuration10Ms, ExpertFrameDuration20Ms, ExpertFrameDuration40Ms, ExpertFrameDuration60Ms}
hybridFrames := []ExpertFrameDuration{ExpertFrameDuration10Ms, ExpertFrameDuration20Ms}
celtFrames := []ExpertFrameDuration{ExpertFrameDuration2_5Ms, ExpertFrameDuration5Ms, ExpertFrameDuration10Ms, ExpertFrameDuration20Ms}
bitratesByMode := map[EncoderMode][]int{
EncoderModeSILK: {8000, 16000, 32000},
EncoderModeHybrid: {24000, 48000, 96000},
EncoderModeCELT: {32000, 96000, 256000},
}
vbrModes := []BitrateMode{BitrateModeVBR, BitrateModeCVBR, BitrateModeCBR}
for _, m := range modes {
var frames []ExpertFrameDuration
switch m.mode {
case EncoderModeSILK:
frames = silkFrames
case EncoderModeHybrid:
frames = hybridFrames
default:
frames = celtFrames
}
for _, ch := range []int{1, 2} {
for _, fr := range frames {
for _, br := range bitratesByMode[m.mode] {
for _, vbr := range vbrModes {
// FEC only meaningful for SILK/Hybrid; DTX for SILK/Hybrid.
fecOpts := []bool{false}
dtxOpts := []bool{false}
if m.mode == EncoderModeSILK || m.mode == EncoderModeHybrid {
fecOpts = []bool{false, true}
dtxOpts = []bool{false, true}
}
for _, fec := range fecOpts {
for _, dtx := range dtxOpts {
specs = append(specs, encodeSweepSpec{
name: fmt.Sprintf("%s_ch%d_%dms_%dbps_vbr%d_fec%t_dtx%t", m.name, ch, frameMsOf(fr), br, vbr, fec, dtx),
application: m.app,
mode: m.mode,
bandwidth: m.bw,
frameMs: fr,
bitrate: br,
channels: ch,
vbr: vbr,
fec: fec,
dtx: dtx,
})
}
}
}
}
}
}
}
return specs
}
func frameMsOf(fr ExpertFrameDuration) int {
switch fr {
case ExpertFrameDuration2_5Ms:
return 2
case ExpertFrameDuration5Ms:
return 5
case ExpertFrameDuration10Ms:
return 10
case ExpertFrameDuration20Ms:
return 20
case ExpertFrameDuration40Ms:
return 40
case ExpertFrameDuration60Ms:
return 60
default:
return 20
}
}
// genPCM fills an interleaved PCM buffer with a seeded mix of tones + noise.
func genPCM(rng *rand.Rand, frameSamples, channels int, sampleRate float64) []float32 {
pcm := make([]float32, frameSamples*channels)
// Random tone parameters per channel.
type tone struct{ f, a, ph float64 }
tones := make([][]tone, channels)
for c := range channels {
nt := 1 + rng.Intn(3)
ts := make([]tone, nt)
for k := range ts {
ts[k] = tone{
f: 120 + rng.Float64()*6000,
a: 0.05 + rng.Float64()*0.3,
ph: rng.Float64() * 2 * math.Pi,
}
}
tones[c] = ts
}
noise := rng.Float64() * 0.05
for i := range frameSamples {
tm := float64(i) / sampleRate
for c := range channels {
var v float64
for _, ts := range tones[c] {
v += ts.a * math.Sin(2*math.Pi*ts.f*tm+ts.ph)
}
v += (rng.Float64()*2 - 1) * noise
if v > 0.99 {
v = 0.99
} else if v < -0.99 {
v = -0.99
}
pcm[i*channels+c] = float32(v)
}
}
return pcm
}
// encodePackets encodes nFrames consecutive frames for one spec, returning the
// produced packets. Packets of length 0/1 (DTX/CELT silence) are kept: they are
// valid and exercise the PLC/empty-frame decode path identically in both codecs.
func encodePackets(t *testing.T, spec encodeSweepSpec, rng *rand.Rand, nFrames int) ([][]byte, bool) {
t.Helper()
const sampleRate = 48000
enc, err := NewEncoder(EncoderConfig{
SampleRate: sampleRate,
Channels: spec.channels,
Application: spec.application,
})
if err != nil {
t.Fatalf("NewEncoder(%s): %v", spec.name, err)
}
if err := enc.SetMode(spec.mode); err != nil {
return nil, false
}
frameSamples := spec.frameSamples48k()
if err := enc.SetFrameSize(frameSamples); err != nil {
return nil, false
}
if err := enc.SetExpertFrameDuration(spec.frameMs); err != nil {
return nil, false
}
if spec.autoBW {
if err := enc.SetBandwidthAuto(); err != nil {
return nil, false
}
} else if err := enc.SetBandwidth(spec.bandwidth); err != nil {
return nil, false
}
if err := enc.SetBitrate(spec.bitrate); err != nil {
return nil, false
}
if err := enc.SetBitrateMode(spec.vbr); err != nil {
return nil, false
}
enc.SetFEC(spec.fec)
if spec.fec {
if err := enc.SetPacketLoss(20); err != nil {
return nil, false
}
}
enc.SetDTX(spec.dtx)
if spec.channels == 2 {
if err := enc.SetForceChannels(2); err != nil {
return nil, false
}
}
packets := make([][]byte, 0, nFrames)
for f := range nFrames {
pcm := genPCM(rng, frameSamples, spec.channels, sampleRate)
pkt, err := encodeOneFrame(enc, pcm)
if err != nil {
// This harness validates the DECODE path. An encoder error/panic is an
// encoder-side finding (it cannot generate a valid packet to decode), so
// it is reported and the spec is skipped rather than failing the decode
// sweep.
t.Logf("encoder finding (%s frame %d): %v — skipping spec for decode sweep", spec.name, f, err)
return nil, false
}
// Copy: EncodeFloat32 may reuse an internal buffer across calls.
packets = append(packets, append([]byte(nil), pkt...))
}
return packets, true
}
// encodeOneFrame encodes one frame and converts any encoder panic into an error
// so an encoder-side crash does not abort the decode-focused sweep.
func encodeOneFrame(enc *Encoder, pcm []float32) (pkt []byte, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("PANIC in gopus encode: %v", r)
}
}()
return enc.EncodeFloat32(pcm)
}
// TestDecodeDifferentialEncodeThenDecode encodes across the full config space
// and asserts gopus and libopus decode the resulting valid packets to identical
// PCM (bit-exact on amd64; ≤1-ULP CELT/Hybrid tolerance on arm64).
func TestDecodeDifferentialEncodeThenDecode(t *testing.T) {
libopustest.RequireOracle(t)
if _, err := libopustest.DecodeDiffHelperPath(); err != nil {
libopustest.HelperUnavailable(t, "decode diff probe", err)
}
specs := buildEncodeSweep()
// Decode formats to exercise; float32 is the primary, int16/int24 share the
// same decode core and differ only in final conversion.
formats := []uint32{
libopustest.DecodeDiffFormatFloat32,
libopustest.DecodeDiffFormatInt16,
libopustest.DecodeDiffFormatInt24,
}
const framesPerSpec = 3
budget := min(diffFuzzBudget(len(specs)), len(specs))
// Deterministically stride through the spec list so a shrunk budget still
// covers the whole space rather than a prefix.
stride := 1
if budget < len(specs) {
stride = len(specs) / budget
}
tested := 0
for idx := 0; idx < len(specs) && tested < budget; idx += stride {
spec := specs[idx]
tested++
t.Run(spec.name, func(t *testing.T) {
specRng := rand.New(rand.NewSource(int64(idx)*1000003 + 1))
packets, ok := encodePackets(t, spec, specRng, framesPerSpec)
if !ok {
t.Skipf("encoder rejected config %s", spec.name)
}
for _, format := range formats {
cases := make([]libopustest.DecodeDiffCase, len(packets))
for i, p := range packets {
cases[i] = libopustest.DecodeDiffCase{Packet: p, Format: format, FrameSize: 5760}
}
oracle, err := libopustest.ProbeDecodeDiff(48000, spec.channels, cases)
if err != nil {
libopustest.HelperUnavailable(t, "decode diff probe", err)
return
}
for i, p := range packets {
or := oracle[i]
gpcm, gn, gerr := gopusDecodeProbe(48000, spec.channels, cases[i])
label := fmt.Sprintf("%s/fmt%d/frame%d", spec.name, format, i)
if or.Code < 0 {
// libopus rejected a packet gopus produced — both should reject.
if gerr == nil {
t.Errorf("%s: libopus rejected (code=%d) a valid gopus packet but gopus accepted (n=%d) — packet=% x",
label, or.Code, gn, p)
}
continue
}
if gerr != nil {
t.Errorf("%s: libopus accepted (n=%d) but gopus rejected: %v — packet=% x",
label, or.Code, gerr, p)
continue
}
if gn != int(or.Code) {
t.Errorf("%s: sample count gopus=%d libopus=%d — packet=% x", label, gn, or.Code, p)
continue
}
want := oracleResultToFloat32(format, or)
toc := byte(0)
if len(p) > 0 {
toc = p[0]
}
if !assertDiffPCM(t, label, toc, format, gpcm, want) {
t.Logf("%s: diverging packet=% x", label, p)
}
}
}
})
}
t.Logf("encode-then-decode sweep: %d/%d specs × %d frames × %d formats", tested, len(specs), framesPerSpec, len(formats))
}