-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode_public_test.go
More file actions
12253 lines (11674 loc) · 466 KB
/
Copy pathencode_public_test.go
File metadata and controls
12253 lines (11674 loc) · 466 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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package goav1_test
import (
"bytes"
"errors"
"fmt"
"math/rand"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
goav1 "github.com/thesyncim/goav1"
"github.com/thesyncim/goav1/internal/av1/obu"
)
var publicRTCEncoderHotPathSink int
func TestPublicEncoderWebRTCScalabilityModeCatalogue(t *testing.T) {
want := []string{
"L1T1", "L1T2", "L1T3",
"L2T1", "L2T1h", "L2T1_KEY",
"L2T2", "L2T2h", "L2T2_KEY", "L2T2_KEY_SHIFT",
"L2T3", "L2T3h", "L2T3_KEY",
"L3T1", "L3T1h", "L3T1_KEY",
"L3T2", "L3T2h", "L3T2_KEY",
"L3T3", "L3T3h", "L3T3_KEY",
"S2T1", "S2T1h", "S2T2", "S2T2h", "S2T3", "S2T3h",
"S3T1", "S3T1h", "S3T2", "S3T2h", "S3T3", "S3T3h",
}
modes := goav1.EncoderWebRTCScalabilityModes()
if len(modes) != len(want) {
t.Fatalf("EncoderWebRTCScalabilityModes len=%d want %d", len(modes), len(want))
}
prefixed := goav1.AppendEncoderWebRTCScalabilityModes([]goav1.EncoderScalabilityMode{goav1.EncoderScalabilityModeL3T3})
if len(prefixed) != len(want)+1 || prefixed[0] != goav1.EncoderScalabilityModeL3T3 {
t.Fatalf("AppendEncoderWebRTCScalabilityModes prefix len=%d first=%s", len(prefixed), prefixed[0])
}
for i, name := range want {
mode := modes[i]
if prefixed[i+1] != mode {
t.Fatalf("prefixed mode %d=%s want %s", i+1, prefixed[i+1], mode)
}
parsed, ok := goav1.ParseEncoderScalabilityMode(name)
if !ok || parsed != mode {
t.Fatalf("ParseEncoderScalabilityMode(%q)=%s,%v want %s,true", name, parsed, ok, mode)
}
if got := mode.String(); got != name {
t.Fatalf("mode %d String()=%q want %q", i, got, name)
}
tIndex := strings.IndexByte(name, 'T')
if len(name) < 4 || tIndex < 2 || tIndex+1 >= len(name) {
t.Fatalf("bad expected scalability name %q", name)
}
wantSpatial := uint8(name[1] - '0')
wantTemporal := uint8(name[tIndex+1] - '0')
spatial, temporal, key, ok := mode.Layers()
if !ok || spatial != wantSpatial || temporal != wantTemporal ||
key != strings.Contains(name, "_KEY") {
t.Fatalf("%q Layers()=%d,%d,%v,%v want %d,%d,%v,true",
name, spatial, temporal, key, ok,
wantSpatial, wantTemporal, strings.Contains(name, "_KEY"))
}
if mode.IsSimulcast() != strings.HasPrefix(name, "S") ||
mode.UsesSmallResolutionStep() != strings.Contains(name, "h") ||
mode.UsesSharedReferenceSlots() != (wantSpatial > 1 && !strings.HasPrefix(name, "S")) ||
mode.UsesKeyFrameInterLayerDependency() != strings.Contains(name, "_KEY") ||
mode.UsesKeyFrameInterLayerDependencyShift() != strings.Contains(name, "_KEY_SHIFT") {
t.Fatalf("%q flags simulcast=%v small=%v shared=%v key=%v shift=%v",
name,
mode.IsSimulcast(),
mode.UsesSmallResolutionStep(),
mode.UsesSharedReferenceSlots(),
mode.UsesKeyFrameInterLayerDependency(),
mode.UsesKeyFrameInterLayerDependencyShift())
}
}
if _, ok := goav1.ParseEncoderScalabilityMode("L4T4"); ok {
t.Fatal("ParseEncoderScalabilityMode accepted invalid L4T4")
}
}
func TestPublicEncoderScalabilityModeCatalogueIncludesExplicitModes(t *testing.T) {
want := []string{
"L1T1", "L1T2", "L1T3",
"L2T1", "L2T1h", "L2T1_KEY",
"L2T2", "L2T2h", "L2T2_KEY", "L2T2_KEY_SHIFT",
"L2T3", "L2T3h", "L2T3_KEY", "L2T3_KEY_SHIFT",
"L3T1", "L3T1h", "L3T1_KEY",
"L3T2", "L3T2h", "L3T2_KEY", "L3T2_KEY_SHIFT",
"L3T3", "L3T3h", "L3T3_KEY", "L3T3_KEY_SHIFT",
"S2T1", "S2T1h", "S2T2", "S2T2h", "S2T3", "S2T3h",
"S3T1", "S3T1h", "S3T2", "S3T2h", "S3T3", "S3T3h",
}
modes := goav1.EncoderScalabilityModes()
if len(modes) != len(want) {
t.Fatalf("EncoderScalabilityModes len=%d want %d", len(modes), len(want))
}
prefixed := goav1.AppendEncoderScalabilityModes([]goav1.EncoderScalabilityMode{goav1.EncoderScalabilityModeL3T3})
if len(prefixed) != len(want)+1 || prefixed[0] != goav1.EncoderScalabilityModeL3T3 {
t.Fatalf("AppendEncoderScalabilityModes prefix len=%d first=%s", len(prefixed), prefixed[0])
}
for i, name := range want {
mode := modes[i]
if prefixed[i+1] != mode {
t.Fatalf("prefixed mode %d=%s want %s", i+1, prefixed[i+1], mode)
}
parsed, ok := goav1.ParseEncoderScalabilityMode(name)
if !ok || parsed != mode {
t.Fatalf("ParseEncoderScalabilityMode(%q)=%s,%v want %s,true", name, parsed, ok, mode)
}
if got := mode.String(); got != name {
t.Fatalf("mode %d String()=%q want %q", i, got, name)
}
}
pinnedLibWebRTCOnly := make(map[goav1.EncoderScalabilityMode]bool)
for _, mode := range goav1.EncoderWebRTCScalabilityModes() {
pinnedLibWebRTCOnly[mode] = true
}
webRTCSVC := make(map[goav1.EncoderScalabilityMode]bool)
for _, mode := range goav1.EncoderWebRTCSVCScalabilityModes() {
webRTCSVC[mode] = true
}
for _, mode := range []goav1.EncoderScalabilityMode{
goav1.EncoderScalabilityModeL2T3_KEY_SHIFT,
goav1.EncoderScalabilityModeL3T2_KEY_SHIFT,
goav1.EncoderScalabilityModeL3T3_KEY_SHIFT,
} {
if !containsEncoderScalabilityMode(modes, mode) {
t.Fatalf("EncoderScalabilityModes omitted explicit mode %s", mode)
}
if pinnedLibWebRTCOnly[mode] {
t.Fatalf("%s unexpectedly exported by EncoderWebRTCScalabilityModes", mode)
}
if !webRTCSVC[mode] {
t.Fatalf("EncoderWebRTCSVCScalabilityModes omitted W3C mode %s", mode)
}
if err := goav1.ValidateEncoderWebRTCActiveScalabilityModes(mode); err != nil {
t.Fatalf("ValidateEncoderWebRTCActiveScalabilityModes(%s): %v", mode, err)
}
}
}
func TestPublicEncoderWebRTCSVCScalabilityModeCatalogueIncludesCompleteW3CList(t *testing.T) {
want := []string{
"L1T1", "L1T2", "L1T3",
"L2T1", "L2T1h", "L2T1_KEY",
"L2T2", "L2T2h", "L2T2_KEY", "L2T2_KEY_SHIFT",
"L2T3", "L2T3h", "L2T3_KEY", "L2T3_KEY_SHIFT",
"L3T1", "L3T1h", "L3T1_KEY",
"L3T2", "L3T2h", "L3T2_KEY", "L3T2_KEY_SHIFT",
"L3T3", "L3T3h", "L3T3_KEY", "L3T3_KEY_SHIFT",
"S2T1", "S2T1h", "S2T2", "S2T2h", "S2T3", "S2T3h",
"S3T1", "S3T1h", "S3T2", "S3T2h", "S3T3", "S3T3h",
}
modes := goav1.EncoderWebRTCSVCScalabilityModes()
if len(modes) != len(want) {
t.Fatalf("EncoderWebRTCSVCScalabilityModes len=%d want %d", len(modes), len(want))
}
prefixed := goav1.AppendEncoderWebRTCSVCScalabilityModes([]goav1.EncoderScalabilityMode{goav1.EncoderScalabilityModeL3T3})
if len(prefixed) != len(want)+1 || prefixed[0] != goav1.EncoderScalabilityModeL3T3 {
t.Fatalf("AppendEncoderWebRTCSVCScalabilityModes prefix len=%d first=%s", len(prefixed), prefixed[0])
}
seen := make(map[goav1.EncoderScalabilityMode]bool, len(modes))
for i, name := range want {
mode := modes[i]
if prefixed[i+1] != mode {
t.Fatalf("prefixed mode %d=%s want %s", i+1, prefixed[i+1], mode)
}
parsed, ok := goav1.ParseEncoderScalabilityMode(name)
if !ok || parsed != mode {
t.Fatalf("ParseEncoderScalabilityMode(%q)=%s,%v want %s,true", name, parsed, ok, mode)
}
if got := mode.String(); got != name {
t.Fatalf("mode %d String()=%q want %q", i, got, name)
}
if seen[mode] {
t.Fatalf("duplicate mode %s", mode)
}
seen[mode] = true
}
modes[0] = goav1.EncoderScalabilityModeS3T3h
if got := goav1.EncoderWebRTCSVCScalabilityModes()[0]; got != goav1.EncoderScalabilityModeL1T1 {
t.Fatalf("EncoderWebRTCSVCScalabilityModes aliased caller mutation: %s", got)
}
}
func containsEncoderScalabilityMode(modes []goav1.EncoderScalabilityMode, want goav1.EncoderScalabilityMode) bool {
for _, mode := range modes {
if mode == want {
return true
}
}
return false
}
// TestPublicVideoEncoderRoundTrip drives the public encoding surface end to
// end: CBR with two temporal layers, a mid-stream forced keyframe, and decode
// through the public Decoder with every frame bit-exact against the encoder
// reconstruction.
func TestPublicVideoEncoderRoundTrip(t *testing.T) {
const w, h = 320, 192
cw, ch := w/2, h/2
rng := rand.New(rand.NewSource(5))
bg := make([]byte, w*h)
for i := range bg {
bg[i] = uint8(60 + rng.Intn(80))
}
makeFrame := func(n int) goav1.I420Frame {
f := goav1.I420Frame{
Y: append([]byte(nil), bg...), U: make([]byte, cw*ch), V: make([]byte, cw*ch),
YStride: w, ChromaStride: cw, Width: w, Height: h,
}
for i := range f.U {
f.U[i] = 120
f.V[i] = 130
}
for y := 20 + n*3; y < 52+n*3 && y < h; y++ {
for x := 16 + n*5; x < 48+n*5 && x < w; x++ {
f.Y[y*w+x] = 220
}
}
return f
}
enc, err := goav1.NewVideoEncoder(goav1.VideoEncoderConfig{
Width: w, Height: h,
TargetBitrate: 400_000, Framerate: 30,
TemporalLayers: 2,
})
if err != nil {
t.Fatal(err)
}
const frames = 12
var tus [][]byte
type snap struct {
y, u, v []byte
}
var recons []snap
keyAt := map[int]bool{}
for i := range frames {
out, err := enc.Encode(makeFrame(i), i == 6) // force a mid-stream keyframe
if err != nil {
t.Fatalf("encode %d: %v", i, err)
}
if out.Keyframe {
keyAt[i] = true
}
if i >= 1 && i < 6 && i%2 == 1 && out.TemporalID != 1 {
t.Fatalf("frame %d temporal id %d, want 1", i, out.TemporalID)
}
tus = append(tus, append([]byte(nil), out.Data...))
r := enc.Reconstruction()
recons = append(recons, snap{
y: append([]byte(nil), r.Y...),
u: append([]byte(nil), r.U...),
v: append([]byte(nil), r.V...),
})
}
if !keyAt[0] || !keyAt[6] {
t.Fatalf("keyframes at %v, want frames 0 and 6", keyAt)
}
if enc.QIndex() == 0 {
t.Fatal("rate controller reported qindex 0")
}
dec, err := goav1.NewDecoder(tus)
if err != nil {
t.Fatal(err)
}
defer dec.Close()
i := 0
for {
batch, ok, err := dec.DecodeNext()
if err != nil {
t.Fatalf("decode %d: %v", i, err)
}
if !ok {
break
}
for _, f := range batch {
for y := range h {
for x := range w {
if f.Y.Pix[y*f.Y.Stride+x] != recons[i].y[y*w+x] {
t.Fatalf("frame %d Y mismatch at (%d,%d)", i, x, y)
}
}
}
for y := range ch {
for x := range cw {
if f.U.Pix[y*f.U.Stride+x] != recons[i].u[y*cw+x] || f.V.Pix[y*f.V.Stride+x] != recons[i].v[y*cw+x] {
t.Fatalf("frame %d chroma mismatch at (%d,%d)", i, x, y)
}
}
}
i++
}
}
if i != frames {
t.Fatalf("decoded %d frames, want %d", i, frames)
}
}
func TestPublicVideoEncoderDisableSceneCutKeyframes(t *testing.T) {
const w, h = 320, 192
sceneA := make([]byte, (w+64)*h)
sceneB := make([]byte, w*h)
rngA := rand.New(rand.NewSource(21))
rngB := rand.New(rand.NewSource(22))
for y := range h {
for x := 0; x < w+64; x++ {
sceneA[y*(w+64)+x] = uint8(60 + (x/5+y/7)%60 + rngA.Intn(40))
}
}
for i := range sceneB {
sceneB[i] = uint8(140 + (i/9)%50 + rngB.Intn(50))
}
makeFrame := func(idx int) goav1.I420Frame {
f := goav1.I420Frame{
Y: make([]byte, w*h),
U: make([]byte, w*h/4),
V: make([]byte, w*h/4),
YStride: w,
ChromaStride: w / 2,
Width: w,
Height: h,
}
if idx < 6 {
off := idx * 6
for y := range h {
copy(f.Y[y*w:(y+1)*w], sceneA[y*(w+64)+off:])
}
} else {
copy(f.Y, sceneB)
}
for i := range f.U {
f.U[i], f.V[i] = 120, 128
}
return f
}
encodeKeys := func(disable bool) []bool {
t.Helper()
enc, err := goav1.NewVideoEncoder(goav1.VideoEncoderConfig{
Width: w,
Height: h,
QIndex: 110,
DisableSceneCutKeyframes: disable,
})
if err != nil {
t.Fatal(err)
}
defer enc.Close()
keys := make([]bool, 9)
for i := range keys {
out, err := enc.Encode(makeFrame(i), false)
if err != nil {
t.Fatalf("encode frame %d disable=%v: %v", i, disable, err)
}
keys[i] = out.Keyframe
}
return keys
}
defaultKeys := encodeKeys(false)
if !defaultKeys[0] || !defaultKeys[6] {
t.Fatalf("default scene-cut keys=%v, want frames 0 and 6", defaultKeys)
}
disabledKeys := encodeKeys(true)
if !disabledKeys[0] {
t.Fatalf("disabled scene-cut keys=%v, want first frame key", disabledKeys)
}
for i := 1; i < len(disabledKeys); i++ {
if disabledKeys[i] {
t.Fatalf("disabled scene-cut emitted keyframe at %d: %v", i, disabledKeys)
}
}
}
func TestPublicKeyframeEncoderRoundTripAnd1080pAllocs(t *testing.T) {
const w, h = 1920, 1080
enc, err := goav1.NewKeyframeEncoder(w, h, 80)
if err != nil {
t.Fatalf("NewKeyframeEncoder: %v", err)
}
defer enc.Close()
frames := [...]goav1.I420Frame{
publicRTCMatrixFrame(w, h, 0),
publicRTCMatrixFrame(w, h, 1),
publicRTCMatrixFrame(w, h, 2),
publicRTCMatrixFrame(w, h, 3),
}
out, err := enc.Encode(frames[0])
if err != nil {
t.Fatalf("Encode: %v", err)
}
if !out.Keyframe || out.TemporalID != 0 || len(out.Data) == 0 {
t.Fatalf("encoded frame key=%v tid=%d len=%d", out.Keyframe, out.TemporalID, len(out.Data))
}
publicAssertI420TUDecodesToRecon(t, out.Data, enc.Reconstruction())
if err := enc.SetQIndex(72); err != nil {
t.Fatalf("SetQIndex: %v", err)
}
if out, err = enc.Encode(frames[1]); err != nil {
t.Fatalf("Encode after SetQIndex: %v", err)
} else if !out.Keyframe || len(out.Data) == 0 {
t.Fatalf("encoded frame after SetQIndex key=%v len=%d", out.Keyframe, len(out.Data))
}
var allocErr error
frameIndex := 0
sum := 0
allocs := testing.AllocsPerRun(5, func() {
out, err := enc.Encode(frames[frameIndex&3])
frameIndex++
if err != nil {
allocErr = err
return
}
recon := enc.Reconstruction()
if !out.Keyframe || len(out.Data) == 0 || len(recon.Y) == 0 {
allocErr = fmt.Errorf("empty keyframe output")
return
}
sum += len(out.Data) + len(recon.Y)
})
publicRTCEncoderHotPathSink = sum
if allocErr != nil {
t.Fatalf("Encode allocation run: %v", allocErr)
}
if allocs != 0 {
t.Fatalf("KeyframeEncoder Encode 1080p allocations=%f want 0", allocs)
}
if err := enc.Close(); err != nil {
t.Fatalf("Close: %v", err)
}
if err := enc.Close(); err != nil {
t.Fatalf("second Close: %v", err)
}
if _, err := enc.Encode(frames[2]); err == nil {
t.Fatal("Encode after Close succeeded")
}
}
func publicAssertI420TUDecodesToRecon(t *testing.T, tu []byte, recon goav1.I420Frame) {
t.Helper()
dec, err := goav1.NewDecoder([][]byte{append([]byte(nil), tu...)})
if err != nil {
t.Fatalf("NewDecoder: %v", err)
}
defer dec.Close()
frames, err := dec.DecodeAll()
if err != nil {
t.Fatalf("DecodeAll: %v", err)
}
if len(frames) != 1 {
t.Fatalf("decoded %d frames, want 1", len(frames))
}
got := frames[0]
for y := 0; y < recon.Height; y++ {
gotRow := got.Y.Pix[y*got.Y.Stride : y*got.Y.Stride+recon.Width]
wantRow := recon.Y[y*recon.YStride : y*recon.YStride+recon.Width]
if !bytes.Equal(gotRow, wantRow) {
t.Fatalf("decoded Y row %d mismatch", y)
}
}
chromaW, chromaH := recon.Width/2, recon.Height/2
for y := 0; y < chromaH; y++ {
gotU := got.U.Pix[y*got.U.Stride : y*got.U.Stride+chromaW]
gotV := got.V.Pix[y*got.V.Stride : y*got.V.Stride+chromaW]
wantU := recon.U[y*recon.ChromaStride : y*recon.ChromaStride+chromaW]
wantV := recon.V[y*recon.ChromaStride : y*recon.ChromaStride+chromaW]
if !bytes.Equal(gotU, wantU) || !bytes.Equal(gotV, wantV) {
t.Fatalf("decoded chroma row %d mismatch", y)
}
}
}
func TestPublicEncodeI400KeyframeNativeMonochrome(t *testing.T) {
const w, h, stride = 96, 64, 112
src := goav1.I400Frame{
Y: make([]byte, stride*h),
YStride: stride,
Width: w,
Height: h,
}
for y := range h {
for x := range w {
src.Y[y*stride+x] = uint8((64 + x*3 + y*5 + (x*y)%17) & 0xff)
}
}
for _, tc := range []struct {
name string
qIndex uint8
lossless bool
}{
{name: "lossless", qIndex: 0, lossless: true},
{name: "lossy", qIndex: 96},
} {
t.Run(tc.name, func(t *testing.T) {
tu, recon, err := goav1.EncodeI400Keyframe(src, tc.qIndex)
if err != nil {
t.Fatalf("EncodeI400Keyframe: %v", err)
}
if len(tu) == 0 {
t.Fatal("empty temporal unit")
}
seq := publicFirstSequenceHeader(t, tu)
if !seq.ColorConfig.MonoChrome || seq.ColorConfig.BitDepth != 8 {
t.Fatalf("sequence color=%+v, want native 8-bit monochrome", seq.ColorConfig)
}
if recon.Width != w || recon.Height != h || recon.YStride < w {
t.Fatalf("recon geometry=%dx%d stride=%d", recon.Width, recon.Height, recon.YStride)
}
if tc.lossless {
for y := range h {
got := recon.Y[y*recon.YStride : y*recon.YStride+w]
want := src.Y[y*src.YStride : y*src.YStride+w]
if !bytes.Equal(got, want) {
t.Fatalf("lossless recon row %d differs", y)
}
}
}
dec, err := goav1.NewDecoder([][]byte{tu})
if err != nil {
t.Fatalf("NewDecoder: %v", err)
}
defer dec.Close()
frames, err := dec.DecodeAll()
if err != nil {
t.Fatalf("DecodeAll: %v", err)
}
if len(frames) != 1 {
t.Fatalf("decoded %d frames, want 1", len(frames))
}
frame := frames[0]
if !frame.Format.MonoChrome || frame.Format.BitDepth != 8 {
t.Fatalf("decoded format=%+v, want native 8-bit monochrome", frame.Format)
}
if frame.U.Width != 0 || frame.V.Width != 0 || len(frame.U.Pix) != 0 || len(frame.V.Pix) != 0 {
t.Fatalf("decoded chroma U=%+v V=%+v, want absent", frame.U, frame.V)
}
for y := range h {
got := frame.Y.Pix[y*frame.Y.Stride : y*frame.Y.Stride+w]
want := recon.Y[y*recon.YStride : y*recon.YStride+w]
if !bytes.Equal(got, want) {
t.Fatalf("decoded luma row %d differs from reconstruction", y)
}
}
})
}
}
func TestPublicEncodeI400PFrameNativeMonochrome(t *testing.T) {
const w, h, stride = 128, 96, 144
src1 := goav1.I400Frame{
Y: make([]byte, stride*h),
YStride: stride,
Width: w,
Height: h,
}
for y := range h {
for x := range w {
src1.Y[y*stride+x] = uint8((72 + x*2 + y*3 + (x*y)%19) & 0xff)
}
}
src2 := goav1.I400Frame{
Y: make([]byte, stride*h),
YStride: stride,
Width: w,
Height: h,
}
const shiftX, shiftY = 4, 2
for y := range h {
for x := range w {
sx, sy := x-shiftX, y-shiftY
if sx < 0 {
sx = 0
}
if sy < 0 {
sy = 0
}
src2.Y[y*stride+x] = uint8(min(255, int(src1.Y[sy*stride+sx])+1))
}
}
keyTU, keyRecon, err := goav1.EncodeI400Keyframe(src1, 80)
if err != nil {
t.Fatalf("EncodeI400Keyframe: %v", err)
}
seq := publicFirstSequenceHeader(t, keyTU)
if !seq.ColorConfig.MonoChrome || seq.ColorConfig.BitDepth != 8 {
t.Fatalf("sequence color=%+v, want native 8-bit monochrome", seq.ColorConfig)
}
for _, tc := range []struct {
name string
qIndex uint8
}{
{name: "lossless", qIndex: 0},
{name: "lossy", qIndex: 80},
} {
t.Run(tc.name, func(t *testing.T) {
pTU, pRecon, err := goav1.EncodeI400PFrame(src2, keyRecon, tc.qIndex)
if err != nil {
t.Fatalf("EncodeI400PFrame: %v", err)
}
if len(pTU) == 0 {
t.Fatal("empty P temporal unit")
}
if tc.qIndex == 0 && !bytes.Equal(pRecon.Y, src2.Y) {
t.Fatal("lossless P-frame reconstruction differs from source")
}
dec, err := goav1.NewDecoder([][]byte{keyTU, pTU})
if err != nil {
t.Fatalf("NewDecoder: %v", err)
}
defer dec.Close()
frames, err := dec.DecodeAll()
if err != nil {
t.Fatalf("DecodeAll: %v", err)
}
if len(frames) != 2 {
t.Fatalf("decoded %d frames, want 2", len(frames))
}
for i, frame := range frames {
if !frame.Format.MonoChrome || frame.Format.BitDepth != 8 {
t.Fatalf("decoded frame %d format=%+v, want native 8-bit monochrome", i, frame.Format)
}
if frame.U.Width != 0 || frame.V.Width != 0 || len(frame.U.Pix) != 0 || len(frame.V.Pix) != 0 {
t.Fatalf("decoded frame %d chroma U=%+v V=%+v, want absent", i, frame.U, frame.V)
}
}
for y := range h {
got := frames[1].Y.Pix[y*frames[1].Y.Stride : y*frames[1].Y.Stride+w]
want := pRecon.Y[y*pRecon.YStride : y*pRecon.YStride+w]
if !bytes.Equal(got, want) {
t.Fatalf("decoded P-frame luma row %d differs from reconstruction", y)
}
}
})
}
}
func TestPublicEncodeI400KeyframeReferenceDecoders(t *testing.T) {
decoders := publicReferenceAV1Decoders(t)
const w, h = 128, 72
src := goav1.I400Frame{
Y: make([]byte, w*h),
YStride: w,
Width: w,
Height: h,
}
for y := range h {
for x := range w {
src.Y[y*w+x] = uint8((80 + x*2 + y*3 + (x^y)%23) & 0xff)
}
}
for _, qIndex := range []uint8{0, 88} {
t.Run(fmt.Sprintf("q%d", qIndex), func(t *testing.T) {
tu, _, err := goav1.EncodeI400Keyframe(src, qIndex)
if err != nil {
t.Fatalf("EncodeI400Keyframe: %v", err)
}
ivf := appendPublicIVF(nil, w, h, 30, 1, []publicIVFFrame{{payload: tu}})
assertPublicIVFMatchesReferenceDecodersRawYUV(t, decoders, fmt.Sprintf("public-i400-keyframe-q%d", qIndex), ivf)
})
}
}
func TestPublicEncodeI400HighBitDepthLosslessKeyframeReferenceDecoders(t *testing.T) {
decoders := publicReferenceAV1Decoders(t)
const w, h = 64, 64
for _, bitDepth := range []uint8{10, 12} {
t.Run(fmt.Sprintf("%dbit", bitDepth), func(t *testing.T) {
maxSample := uint16((1 << bitDepth) - 1)
src := goav1.I400HighBitDepthFrame{
Y: make([]uint16, w*h),
YStride: w,
Width: w,
Height: h,
BitDepth: bitDepth,
}
for y := range h {
for x := range w {
src.Y[y*w+x] = uint16((37 + x*19 + y*23 + (x*y)%97) & int(maxSample))
}
}
tu, recon, err := goav1.EncodeI400HighBitDepthLosslessKeyframe(src)
if err != nil {
t.Fatalf("EncodeI400HighBitDepthLosslessKeyframe: %v", err)
}
if recon.BitDepth != src.BitDepth || recon.Width != src.Width || recon.Height != src.Height ||
recon.YStride != src.YStride || !publicUint16Equal(recon.Y, src.Y) {
t.Fatalf("reconstruction mismatch: got=%+v src=%+v", recon, src)
}
seq := publicFirstSequenceHeader(t, tu)
wantProfile := uint8(0)
if bitDepth == 12 {
wantProfile = 2
}
if seq.SeqProfile != wantProfile ||
!seq.ColorConfig.MonoChrome ||
seq.ColorConfig.BitDepth != bitDepth ||
!seq.ColorConfig.HighBitdepth ||
(bitDepth == 12 && !seq.ColorConfig.TwelveBit) {
t.Fatalf("sequence profile=%d color=%+v want profile=%d native %d-bit monochrome", seq.SeqProfile, seq.ColorConfig, wantProfile, bitDepth)
}
ivf := appendPublicIVF(nil, w, h, 30, 1, []publicIVFFrame{{payload: tu}})
wantY := appendPublicI400HighBitDepthRaw(nil, src)
assertPublicIVFHighBitDepthMonochromeMatchesReferenceDecoders(t, decoders, fmt.Sprintf("public-i400-%dbit-lossless", bitDepth), ivf, wantY, w, h, 2)
})
}
}
func TestPublicEncodeI400HighBitDepthKeyframeReferenceDecoders(t *testing.T) {
decoders := publicReferenceAV1Decoders(t)
const w, h = 64, 64
cases := []struct {
bitDepth uint8
qIndex uint8
}{
{bitDepth: 10, qIndex: 0},
{bitDepth: 10, qIndex: 72},
{bitDepth: 12, qIndex: 104},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("%dbit-q%d", tc.bitDepth, tc.qIndex), func(t *testing.T) {
maxSample := uint16((1 << tc.bitDepth) - 1)
src := goav1.I400HighBitDepthFrame{
Y: make([]uint16, w*h),
YStride: w,
Width: w,
Height: h,
BitDepth: tc.bitDepth,
}
for y := range h {
for x := range w {
src.Y[y*w+x] = uint16((73 + x*29 + y*17 + (x*y)%211) & int(maxSample))
}
}
tu, recon, err := goav1.EncodeI400HighBitDepthKeyframe(src, tc.qIndex)
if err != nil {
t.Fatalf("EncodeI400HighBitDepthKeyframe: %v", err)
}
if recon.BitDepth != src.BitDepth || recon.Width != src.Width || recon.Height != src.Height ||
recon.YStride != src.YStride {
t.Fatalf("unexpected reconstruction: got=%+v src=%+v", recon, src)
}
if tc.qIndex == 0 && !publicUint16Equal(recon.Y, src.Y) {
t.Fatalf("lossless reconstruction mismatch: got=%+v src=%+v", recon, src)
}
if tc.qIndex != 0 && publicUint16Equal(recon.Y, src.Y) {
t.Fatalf("lossy reconstruction unexpectedly equals source: got=%+v src=%+v", recon, src)
}
seq := publicFirstSequenceHeader(t, tu)
wantProfile := uint8(0)
if tc.bitDepth == 12 {
wantProfile = 2
}
if seq.SeqProfile != wantProfile ||
!seq.ColorConfig.MonoChrome ||
seq.ColorConfig.BitDepth != tc.bitDepth ||
!seq.ColorConfig.HighBitdepth ||
(tc.bitDepth == 12 && !seq.ColorConfig.TwelveBit) {
t.Fatalf("sequence profile=%d color=%+v want profile=%d native %d-bit monochrome", seq.SeqProfile, seq.ColorConfig, wantProfile, tc.bitDepth)
}
ivf := appendPublicIVF(nil, w, h, 30, 1, []publicIVFFrame{{payload: tu}})
wantY := appendPublicI400HighBitDepthRaw(nil, recon)
assertPublicIVFHighBitDepthMonochromeMatchesReferenceDecoders(t, decoders, fmt.Sprintf("public-i400-%dbit-q%d", tc.bitDepth, tc.qIndex), ivf, wantY, w, h, 2)
})
}
}
func TestPublicEncodeI420HighBitDepthKeyframeReferenceDecoders(t *testing.T) {
decoders := publicReferenceAV1Decoders(t)
const w, h = 64, 64
cases := []struct {
bitDepth uint8
qIndex uint8
profile uint8
}{
{bitDepth: 10, qIndex: 0, profile: uint8(goav1.EncoderProfile0)},
{bitDepth: 10, qIndex: 72, profile: uint8(goav1.EncoderProfile0)},
{bitDepth: 12, qIndex: 0, profile: uint8(goav1.EncoderProfile2)},
{bitDepth: 12, qIndex: 104, profile: uint8(goav1.EncoderProfile2)},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("%dbit-q%d", tc.bitDepth, tc.qIndex), func(t *testing.T) {
src := publicI420HighBitDepthFrame(w, h, tc.bitDepth, int(tc.qIndex))
tu, recon, err := goav1.EncodeI420HighBitDepthKeyframe(src, tc.qIndex)
if err != nil {
t.Fatalf("EncodeI420HighBitDepthKeyframe: %v", err)
}
if recon.BitDepth != src.BitDepth || recon.Width != src.Width || recon.Height != src.Height ||
recon.YStride != src.YStride || recon.ChromaStride != src.ChromaStride {
t.Fatalf("unexpected reconstruction: got=%+v src=%+v", recon, src)
}
if tc.qIndex == 0 && !publicI420HighBitDepthFrameEqual(recon, src) {
t.Fatal("lossless high-bit-depth 4:2:0 reconstruction differs from source")
}
seq := publicFirstSequenceHeader(t, tu)
if seq.SeqProfile != tc.profile ||
seq.ColorConfig.MonoChrome ||
seq.ColorConfig.BitDepth != tc.bitDepth ||
!seq.ColorConfig.HighBitdepth ||
!seq.ColorConfig.SubsamplingX ||
!seq.ColorConfig.SubsamplingY ||
(tc.bitDepth == 12 && !seq.ColorConfig.TwelveBit) {
t.Fatalf("sequence profile=%d color=%+v want profile=%d native %d-bit 4:2:0", seq.SeqProfile, seq.ColorConfig, tc.profile, tc.bitDepth)
}
ivf := appendPublicIVF(nil, w, h, 30, 1, []publicIVFFrame{{payload: tu}})
decoded, err := goav1.DecodeIVF(ivf)
if err != nil {
t.Fatalf("DecodeIVF: %v", err)
}
if len(decoded) != 1 ||
decoded[0].Width != w ||
decoded[0].Height != h ||
decoded[0].ChromaWidth != w/2 ||
decoded[0].ChromaHeight != h/2 ||
decoded[0].BytesPerSample != 2 {
t.Fatalf("decoded frame=%+v want one %dx%d 2-byte 4:2:0 frame", decoded, w, h)
}
wantYUV := appendPublicI420HighBitDepthRaw(nil, recon)
if got := publicDecodedFramesRawYUV(decoded); !bytes.Equal(got, wantYUV) {
offset := firstPublicByteDiff(got, wantYUV)
t.Fatalf("local decoded high-bit-depth 4:2:0 differs first at byte %d", offset)
}
assertPublicIVFMatchesReferenceDecodersRawYUVBytes(t, decoders, fmt.Sprintf("public-i420-%dbit-q%d", tc.bitDepth, tc.qIndex), ivf, wantYUV, 1)
})
}
}
func TestPublicEncodeI400HighBitDepthPFrameReferenceDecoders(t *testing.T) {
decoders := publicReferenceAV1Decoders(t)
const w, h = 64, 64
cases := []struct {
bitDepth uint8
qIndex uint8
}{
{bitDepth: 10, qIndex: 0},
{bitDepth: 10, qIndex: 32},
{bitDepth: 12, qIndex: 0},
{bitDepth: 12, qIndex: 48},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("%dbit-q%d", tc.bitDepth, tc.qIndex), func(t *testing.T) {
maxSample := uint16((1 << tc.bitDepth) - 1)
src1 := goav1.I400HighBitDepthFrame{
Y: make([]uint16, w*h),
YStride: w,
Width: w,
Height: h,
BitDepth: tc.bitDepth,
}
for y := range h {
for x := range w {
src1.Y[y*w+x] = uint16((91 + x*17 + y*29 + (x*y)%181) & int(maxSample))
}
}
keyTU, keyRecon, err := goav1.EncodeI400HighBitDepthKeyframe(src1, tc.qIndex)
if err != nil {
t.Fatalf("EncodeI400HighBitDepthKeyframe: %v", err)
}
src2 := goav1.I400HighBitDepthFrame{
Y: make([]uint16, w*h),
YStride: w,
Width: w,
Height: h,
BitDepth: tc.bitDepth,
}
for y := range h {
for x := range w {
base := int(keyRecon.Y[y*keyRecon.YStride+x])
delta := ((x*3 + 2*y) % 65) - 32
v := base + delta
if v < 0 {
v = 0
} else if v > int(maxSample) {
v = int(maxSample)
}
src2.Y[y*w+x] = uint16(v)
}
}
pTU, pRecon, err := goav1.EncodeI400HighBitDepthPFrame(src2, keyRecon, tc.qIndex)
if err != nil {
t.Fatalf("EncodeI400HighBitDepthPFrame: %v", err)
}
if publicI400HighBitDepthFrameEqual(pRecon, keyRecon) {
t.Fatal("P-frame reconstruction unexpectedly equals reference")
}
if tc.qIndex == 0 && !publicI400HighBitDepthFrameEqual(pRecon, src2) {
t.Fatal("lossless high-bit-depth monochrome P-frame reconstruction differs from source")
}
ivf := appendPublicIVF(nil, w, h, 30, 1, []publicIVFFrame{
{payload: keyTU},
{timestamp: 1, payload: pTU},
})
wantY := appendPublicI400HighBitDepthRaw(nil, keyRecon)
wantY = appendPublicI400HighBitDepthRaw(wantY, pRecon)
assertPublicIVFHighBitDepthMonochromeMatchesReferenceDecoders(t, decoders, fmt.Sprintf("public-i400-%dbit-pframe-q%d", tc.bitDepth, tc.qIndex), ivf, wantY, w, h, 2)
})
}
}
func TestPublicEncodeI420HighBitDepthPFrameReferenceDecoders(t *testing.T) {
decoders := publicReferenceAV1Decoders(t)
const w, h = 64, 64
cases := []struct {
bitDepth uint8
qIndex uint8
}{
{bitDepth: 10, qIndex: 0},
{bitDepth: 10, qIndex: 32},
{bitDepth: 12, qIndex: 0},
{bitDepth: 12, qIndex: 48},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("%dbit-q%d", tc.bitDepth, tc.qIndex), func(t *testing.T) {
maxSample := uint16((1 << tc.bitDepth) - 1)
src1 := publicI420HighBitDepthFrame(w, h, tc.bitDepth, int(tc.qIndex))
keyTU, keyRecon, err := goav1.EncodeI420HighBitDepthKeyframe(src1, tc.qIndex)
if err != nil {
t.Fatalf("EncodeI420HighBitDepthKeyframe: %v", err)
}
src2 := goav1.I420HighBitDepthFrame{
Y: make([]uint16, w*h),
U: make([]uint16, (w/2)*(h/2)),
V: make([]uint16, (w/2)*(h/2)),
YStride: w,
ChromaStride: w / 2,
Width: w,
Height: h,
BitDepth: tc.bitDepth,
}
for y := range h {
for x := range w {
base := int(keyRecon.Y[y*keyRecon.YStride+x])
src2.Y[y*w+x] = publicClampUint16(base+((x*5+y*3)%81-40), maxSample)
}
}
for y := range h / 2 {
for x := range w / 2 {
off := y*src2.ChromaStride + x
src2.U[off] = publicClampUint16(int(keyRecon.U[y*keyRecon.ChromaStride+x])+((x*7+y*5)%53-26), maxSample)
src2.V[off] = publicClampUint16(int(keyRecon.V[y*keyRecon.ChromaStride+x])+((x*3+y*11)%59-29), maxSample)
}
}
pTU, pRecon, err := goav1.EncodeI420HighBitDepthPFrame(src2, keyRecon, tc.qIndex)
if err != nil {
t.Fatalf("EncodeI420HighBitDepthPFrame: %v", err)
}
if publicI420HighBitDepthFrameEqual(pRecon, keyRecon) {
t.Fatal("P-frame reconstruction unexpectedly equals reference")
}
if tc.qIndex == 0 && !publicI420HighBitDepthFrameEqual(pRecon, src2) {
t.Fatal("lossless high-bit-depth 4:2:0 P-frame reconstruction differs from source")
}
ivf := appendPublicIVF(nil, w, h, 30, 1, []publicIVFFrame{
{payload: keyTU},
{timestamp: 1, payload: pTU},
})
dec, err := goav1.NewDecoder([][]byte{keyTU, pTU})
if err != nil {
t.Fatalf("NewDecoder: %v", err)
}
defer dec.Close()
decoded, err := dec.DecodeAll()
if err != nil {
t.Fatalf("DecodeAll: %v", err)
}
if len(decoded) != 2 {
t.Fatalf("decoded %d frames, want 2", len(decoded))
}
wantYUV := appendPublicI420HighBitDepthRaw(nil, keyRecon)
wantYUV = appendPublicI420HighBitDepthRaw(wantYUV, pRecon)
var gotYUV []byte
for _, frame := range decoded {
gotYUV = appendPublicFrameRawYUV(gotYUV, frame)
}
if !bytes.Equal(gotYUV, wantYUV) {
offset := firstPublicByteDiff(gotYUV, wantYUV)
t.Fatalf("local decoded high-bit-depth 4:2:0 key+P differs first at byte %d", offset)
}
assertPublicIVFMatchesReferenceDecodersRawYUVBytes(t, decoders, fmt.Sprintf("public-i420-%dbit-pframe-q%d", tc.bitDepth, tc.qIndex), ivf, wantYUV, 2)
})
}
}
func TestPublicEncodeI400HighBitDepthLosslessKeyframeRejectsInvalid(t *testing.T) {
valid := goav1.I400HighBitDepthFrame{