-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoeff_roundtrip_test.go
More file actions
94 lines (85 loc) · 2.16 KB
/
Copy pathcoeff_roundtrip_test.go
File metadata and controls
94 lines (85 loc) · 2.16 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
package vp8
import (
"bytes"
"testing"
)
func TestCoeffRoundTrip(t *testing.T) {
// Test that encoding and decoding coefficients produces the same values.
tests := []struct {
name string
coeffs [16]int16
plane int
first int
}{
{
"dc_only",
[16]int16{-4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
1, 0,
},
{
"gradient_ac",
[16]int16{-4, -3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
0, 1, // Y with DC via WHT (skip first)
},
{
"mixed_ac",
[16]int16{10, -5, 3, -1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
0, 1,
},
{
"all_ones",
[16]int16{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
0, 0,
},
{
"large_values",
[16]int16{100, -50, 30, -20, 10, -5, 3, -2, 1, 0, 0, 0, 0, 0, 0, 0},
2, 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Encode
var buf bytes.Buffer
enc := newBoolEncoder(&buf)
encodeCoefficientsFrom(enc, &tt.coeffs, tt.plane, 0, tt.first)
enc.flush()
encoded := buf.Bytes()
t.Logf("Encoded %d bytes", len(encoded))
// Decode
bd, err := newBoolDecoder(encoded, 0)
if err != nil {
t.Fatalf("newBoolDecoder: %v", err)
}
var decoded [16]int16
probs := &defaultCoeffProbs[tt.plane]
skipFirst := tt.first > 0
decodeResiduals4(bd, probs, 0, 1, 1, skipFirst, &decoded)
// The encoder uses zigzag[i] to index coefficients.
// The decoder uses zigzagDecode to place them.
// We need to compare the decoded raster-order output with the input.
//
// The encoder reads coeffs[zigzag[i]] at scan position i.
// The decoder writes coeffs[zigzagDecode[n-1]] at scan position n-1.
// Since zigzag == zigzagDecode, this should be a round-trip.
//
// But wait - the decoder uses dequantization with dcQ=1, acQ=1,
// so the values should pass through unchanged.
t.Logf("Input: %v", tt.coeffs)
t.Logf("Decoded: %v", decoded)
match := true
for i := 0; i < 16; i++ {
if i < tt.first {
continue
}
if tt.coeffs[i] != decoded[i] {
t.Errorf("coeffs[%d]: input=%d decoded=%d", i, tt.coeffs[i], decoded[i])
match = false
}
}
if match {
t.Log("MATCH!")
}
})
}
}