Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add VLA support - an RTP header extension parser/builder #245

Merged
merged 1 commit into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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
}
33 changes: 33 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,33 @@ 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"},
}

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
Loading