Skip to content

Commit 78da5a2

Browse files
committed
Add VLA extention header parser
Added VLA parser, builder and unit tests.
1 parent 314bd8e commit 78da5a2

File tree

4 files changed

+941
-0
lines changed

4 files changed

+941
-0
lines changed

codecs/av1/obu/leb128.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,19 @@ func ReadLeb128(in []byte) (uint, uint, error) {
6767

6868
return 0, 0, ErrFailedToReadLEB128
6969
}
70+
71+
// WriteToLeb128 writes a uint to a LEB128 encoded byte slice.
72+
func WriteToLeb128(in uint) []byte {
73+
b := make([]byte, 10)
74+
75+
for i := 0; i < len(b); i++ {
76+
b[i] = byte(in & 0x7f)
77+
in >>= 7
78+
if in == 0 {
79+
return b[:i+1]
80+
}
81+
b[i] |= 0x80
82+
}
83+
84+
return b // unreachable
85+
}

codecs/av1/obu/leb128_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
package obu
55

66
import (
7+
"encoding/hex"
78
"errors"
9+
"fmt"
10+
"math"
811
"testing"
912
)
1013

@@ -40,3 +43,33 @@ func TestReadLeb128(t *testing.T) {
4043
t.Fatal("ReadLeb128 on a buffer with all MSB set should fail")
4144
}
4245
}
46+
47+
func TestWriteToLeb128(t *testing.T) {
48+
type testVector struct {
49+
value uint
50+
leb128 string
51+
}
52+
testVectors := []testVector{
53+
{150, "9601"},
54+
{240, "f001"},
55+
{400, "9003"},
56+
{720, "d005"},
57+
{1200, "b009"},
58+
{999999, "bf843d"},
59+
{0, "00"},
60+
{math.MaxUint32, "ffffffff0f"},
61+
}
62+
63+
runTest := func(t *testing.T, v testVector) {
64+
b := WriteToLeb128(v.value)
65+
if v.leb128 != hex.EncodeToString(b) {
66+
t.Errorf("Expected %s, got %s", v.leb128, hex.EncodeToString(b))
67+
}
68+
}
69+
70+
for _, v := range testVectors {
71+
t.Run(fmt.Sprintf("encode %d", v.value), func(t *testing.T) {
72+
runTest(t, v)
73+
})
74+
}
75+
}

0 commit comments

Comments
 (0)