Skip to content

Commit

Permalink
Add VLA extention header parser
Browse files Browse the repository at this point in the history
Added VLA parser, builder and unit tests.
  • Loading branch information
enobufs committed Jan 7, 2024
1 parent 314bd8e commit a45c336
Show file tree
Hide file tree
Showing 4 changed files with 938 additions and 0 deletions.
16 changes: 16 additions & 0 deletions codecs/av1/obu/leb128.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,19 @@ func ReadLeb128(in []byte) (uint, uint, error) {

return 0, 0, ErrFailedToReadLEB128
}

// WriteToLeb128 writes a uint to a LEB128 encoded byte slice.
func WriteToLeb128(in uint) []byte {
b := make([]byte, 10)

for i := 0; i < len(b); i++ {
b[i] = byte(in & 0x7f)
in >>= 7
if in == 0 {
return b[:i+1]
}
b[i] |= 0x80
}

return b // unreachable

Check warning on line 84 in codecs/av1/obu/leb128.go

View check run for this annotation

Codecov / codecov/patch

codecs/av1/obu/leb128.go#L84

Added line #L84 was not covered by tests
}
36 changes: 36 additions & 0 deletions codecs/av1/obu/leb128_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
package obu

import (
"encoding/hex"
"errors"
"fmt"
"math"
"testing"
)

Expand Down Expand Up @@ -40,3 +43,36 @@ func TestReadLeb128(t *testing.T) {
t.Fatal("ReadLeb128 on a buffer with all MSB set should fail")
}
}

func TestWriteToLeb128(t *testing.T) {
type testVector struct {
value uint
leb128 string
}
testVectors := []testVector{
{150, "9601"},
{240, "f001"},
{400, "9003"},
{720, "d005"},
{1200, "b009"},
{999999, "bf843d"},
{0, "00"},
{math.MaxUint32, "ffffffff0f"},
}
if math.MaxInt64 == int(^uint(0)>>1) {
testVectors = append(testVectors, testVector{math.MaxUint64, "ffffffffffffffffff01"})
}

runTest := func(t *testing.T, v testVector) {
b := WriteToLeb128(v.value)
if v.leb128 != hex.EncodeToString(b) {
t.Errorf("Expected %s, got %s", v.leb128, hex.EncodeToString(b))
}
}

for _, v := range testVectors {
t.Run(fmt.Sprintf("encode %d", v.value), func(t *testing.T) {
runTest(t, v)
})
}
}
Loading

0 comments on commit a45c336

Please sign in to comment.