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

Create av1_sample_buffer_support.go #238

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
97 changes: 97 additions & 0 deletions codecs/av1/frame/av1_sample_buffer_support.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package frame

import (
"github.com/pion/rtp/codecs"
"github.com/pion/rtp/codecs/av1/obu"
)

type AV1PacketSampleBufferSupport struct {

Check warning on line 8 in codecs/av1/frame/av1_sample_buffer_support.go

View workflow job for this annotation

GitHub Actions / lint / Go

exported: exported type AV1PacketSampleBufferSupport should have comment or be unexported (revive)
popFrame bool
avFrame *AV1
}

func (d *AV1PacketSampleBufferSupport) IsPartitionTail(marker bool, _ []byte) bool {

Check warning on line 13 in codecs/av1/frame/av1_sample_buffer_support.go

View workflow job for this annotation

GitHub Actions / lint / Go

exported: exported method AV1PacketSampleBufferSupport.IsPartitionTail should have comment or be unexported (revive)
d.popFrame = true
return marker
}

// IsPartitionHead checks whether if this is a head of the AV1 partition
func (d *AV1PacketSampleBufferSupport) IsPartitionHead(payload []byte) bool {
d.popFrame = true
if len(payload) == 0 {
return false

Check warning on line 22 in codecs/av1/frame/av1_sample_buffer_support.go

View check run for this annotation

Codecov / codecov/patch

codecs/av1/frame/av1_sample_buffer_support.go#L22

Added line #L22 was not covered by tests
}
return (payload[0] & byte(0b10000000)) == 0
}

func (d *AV1PacketSampleBufferSupport) Unmarshal(payload []byte) ([]byte, error) {

Check warning on line 27 in codecs/av1/frame/av1_sample_buffer_support.go

View workflow job for this annotation

GitHub Actions / lint / Go

exported: exported method AV1PacketSampleBufferSupport.Unmarshal should have comment or be unexported (revive)

Check failure on line 28 in codecs/av1/frame/av1_sample_buffer_support.go

View workflow job for this annotation

GitHub Actions / lint / Go

File is not `gofumpt`-ed (gofumpt)
if d.popFrame {
d.avFrame = &AV1{}
d.popFrame = false // start frame assembling
}

packet := codecs.AV1Packet{}
_, err := packet.Unmarshal(payload)

Check failure on line 36 in codecs/av1/frame/av1_sample_buffer_support.go

View workflow job for this annotation

GitHub Actions / lint / Go

File is not `gofumpt`-ed (gofumpt)
if err != nil {
return nil, err

Check warning on line 38 in codecs/av1/frame/av1_sample_buffer_support.go

View check run for this annotation

Codecov / codecov/patch

codecs/av1/frame/av1_sample_buffer_support.go#L38

Added line #L38 was not covered by tests
}

OBUs, _ := d.avFrame.ReadFrames(&packet)

if len(OBUs) == 0 {
return nil, nil

Check warning on line 44 in codecs/av1/frame/av1_sample_buffer_support.go

View check run for this annotation

Codecov / codecov/patch

codecs/av1/frame/av1_sample_buffer_support.go#L44

Added line #L44 was not covered by tests
}

var payloadSize uint = 0

Check warning on line 47 in codecs/av1/frame/av1_sample_buffer_support.go

View workflow job for this annotation

GitHub Actions / lint / Go

var-declaration: should drop = 0 from declaration of var payloadSize; it is the zero value (revive)

for i := range OBUs {
obuLength := uint(len(OBUs[i]))
if obuLength == 0 {
continue
}
payloadSize += obuLength
payloadSize += obu.SizeLeb128(obu.EncodeLEB128(obuLength - 1))
}

result := make([]byte, payloadSize)

offset := 0
for i := range OBUs {
obuLength := len(OBUs[i])

if obuLength == 0 {
continue
}

lenMinus := obuLength - 1

result[offset] = OBUs[i][0] ^ 2 // mark size header exists
offset++
payloadSize := obu.EncodeLEB128(uint(lenMinus))

switch obu.SizeLeb128(payloadSize) {
case 4:
result[offset] = byte(payloadSize >> 24)
offset++
fallthrough

Check warning on line 78 in codecs/av1/frame/av1_sample_buffer_support.go

View check run for this annotation

Codecov / codecov/patch

codecs/av1/frame/av1_sample_buffer_support.go#L75-L78

Added lines #L75 - L78 were not covered by tests
case 3:
result[offset] = byte(payloadSize >> 16)
offset++
fallthrough
case 2:
result[offset] = byte(payloadSize >> 8)
offset++
fallthrough
case 1:
result[offset] = byte(payloadSize)
offset++
}

copy(result[offset:], OBUs[i][1:])
offset += lenMinus
}

return result, nil
}
149 changes: 149 additions & 0 deletions codecs/av1/frame/av1_sample_buffer_support_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package frame

import (
"bytes"

Check failure on line 4 in codecs/av1/frame/av1_sample_buffer_support_test.go

View workflow job for this annotation

GitHub Actions / lint / Go

File is not `gci`-ed with --skip-generated -s standard -s default (gci)
"github.com/pion/rtp"
"github.com/pion/rtp/codecs/av1/obu"
"github.com/pion/webrtc/v3/pkg/media/samplebuilder"
"testing"

Check failure on line 8 in codecs/av1/frame/av1_sample_buffer_support_test.go

View workflow job for this annotation

GitHub Actions / lint / Go

File is not `gci`-ed with --skip-generated -s standard -s default (gci)
"time"
)

func buildAv1Payload(data byte, padding int) []byte {
dataSize := 0

if data > 0 {
dataSize = 1
}

payloadSize := obu.EncodeLEB128(uint(1 + padding + dataSize))
result := make([]byte, 3+obu.SizeLeb128(payloadSize))

result[0] = 0 // AV1 RTP header

offset := 1

switch obu.SizeLeb128(payloadSize) {
case 4:
result[offset] = byte(payloadSize >> 24)
offset++
fallthrough
case 3:
result[offset] = byte(payloadSize >> 16)
offset++
fallthrough
case 2:
result[offset] = byte(payloadSize >> 8)
offset++
fallthrough
case 1:
result[offset] = byte(payloadSize)
offset++
}

result[offset] = 0 // OBU HEADER

if dataSize > 0 {
offset++
result[offset] = data
}
return append(result, make([]byte, padding)...)
}

Check failure on line 51 in codecs/av1/frame/av1_sample_buffer_support_test.go

View workflow job for this annotation

GitHub Actions / lint / Go

File is not `gofumpt`-ed (gofumpt)
func buildAv1Packages(seqNo *uint16, timestamp *uint32, padding int) []*rtp.Packet {
s := *seqNo
t := *timestamp

*timestamp += 1800
*seqNo += 5

return []*rtp.Packet{
{Header: rtp.Header{SequenceNumber: s, Timestamp: t}, Payload: buildAv1Payload(1, 0)},
{Header: rtp.Header{SequenceNumber: s + 1, Timestamp: t}, Payload: buildAv1Payload(2, 0)},
{Header: rtp.Header{SequenceNumber: s + 2, Timestamp: t}, Payload: buildAv1Payload(3, 0)},
{Header: rtp.Header{SequenceNumber: s + 3, Timestamp: t}, Payload: buildAv1Payload(4, 0)},
{Header: rtp.Header{SequenceNumber: s + 4, Timestamp: t, Marker: true}, Payload: buildAv1Payload(5, padding)},
}
}
func TestAV1SampleBufferSupport(t *testing.T) {

Check failure on line 67 in codecs/av1/frame/av1_sample_buffer_support_test.go

View workflow job for this annotation

GitHub Actions / lint / Go

unnecessary leading newline (whitespace)

assembledAv1Frame := []byte{2, 1, 1, 2, 1, 2, 2, 1, 3, 2, 1, 4, 2, 1, 5}
t.Run("AV1 Sample Buffer returning OBU stream", func(t *testing.T) {
videoStreamBuilder := samplebuilder.New(100, &AV1PacketSampleBufferSupport{}, 90000,
samplebuilder.WithMaxTimeDelay(time.Millisecond*100))
var seqNo uint16 = 0

Check warning on line 73 in codecs/av1/frame/av1_sample_buffer_support_test.go

View workflow job for this annotation

GitHub Actions / lint / Go

var-declaration: should drop = 0 from declaration of var seqNo; it is the zero value (revive)
var timestamp uint32 = 0

Check warning on line 74 in codecs/av1/frame/av1_sample_buffer_support_test.go

View workflow job for this annotation

GitHub Actions / lint / Go

var-declaration: should drop = 0 from declaration of var timestamp; it is the zero value (revive)

for i := 0; i < 4; i++ {
for _, pkt := range buildAv1Packages(&seqNo, &timestamp, 0) {
sample := videoStreamBuilder.Pop()
if nil != sample {
if !bytes.Equal(sample.Data, assembledAv1Frame) {
t.Fatal("issue in unmarshalling")
}
}
videoStreamBuilder.Push(pkt)
}
}

for i := 1; i < 16400; i++ { // check OBU len up to 3 bytes
for _, pkt := range buildAv1Packages(&seqNo, &timestamp, i) {
sample := videoStreamBuilder.Pop()
if nil != sample {

Check failure on line 91 in codecs/av1/frame/av1_sample_buffer_support_test.go

View workflow job for this annotation

GitHub Actions / lint / Go

unnecessary leading newline (whitespace)

if !bytes.Equal(sample.Data[0:12], assembledAv1Frame[0:12]) {
t.Fatal("issue in unmarshalling")
}

if sample.Data[len(sample.Data)-i] != 5 {
t.Fatal("issue in unmarshalling")
}

if i > 0 {
padding := make([]byte, i-1)
if !bytes.Equal(sample.Data[len(sample.Data)-i+1:], padding) {
t.Fatal("issue in unmarshalling")
}
}
}
videoStreamBuilder.Push(pkt)
}
}
})
}

func buildHeaderOnlyAv1Packets(seqNo *uint16, timestamp *uint32, padding int) []*rtp.Packet {
s := *seqNo
t := *timestamp

*timestamp += 1800
*seqNo += 3

return []*rtp.Packet{
// Two header-only OBUs
{Header: rtp.Header{SequenceNumber: s, Timestamp: t}, Payload: buildAv1Payload(0, 0)},
{Header: rtp.Header{SequenceNumber: s + 1, Timestamp: t}, Payload: buildAv1Payload(0, 0)},
{Header: rtp.Header{SequenceNumber: s + 2, Timestamp: t, Marker: true}, Payload: buildAv1Payload(5, padding)},
}
}

func TestAV1SampleBufferSupport_OBUWIthoutPayload(t *testing.T) {
assembledAv1Frame := []byte{2, 0, 2, 0, 2, 1, 5}
t.Run("AV1 Sample Buffer with header only OBU elements", func(t *testing.T) {
videoStreamBuilder := samplebuilder.New(100, &AV1PacketSampleBufferSupport{}, 90000,
samplebuilder.WithMaxTimeDelay(time.Millisecond*100))
var seqNo uint16 = 0

Check warning on line 134 in codecs/av1/frame/av1_sample_buffer_support_test.go

View workflow job for this annotation

GitHub Actions / lint / Go

var-declaration: should drop = 0 from declaration of var seqNo; it is the zero value (revive)
var timestamp uint32 = 0

Check warning on line 135 in codecs/av1/frame/av1_sample_buffer_support_test.go

View workflow job for this annotation

GitHub Actions / lint / Go

var-declaration: should drop = 0 from declaration of var timestamp; it is the zero value (revive)

for i := 0; i < 4; i++ {
for _, pkt := range buildHeaderOnlyAv1Packets(&seqNo, &timestamp, 0) {
sample := videoStreamBuilder.Pop()
if nil != sample {
if !bytes.Equal(sample.Data, assembledAv1Frame) {
t.Fatal("issue in unmarshalling")
}
}
videoStreamBuilder.Push(pkt)
}
}
})
}
11 changes: 11 additions & 0 deletions codecs/av1/obu/leb128.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@
return 0, 0, ErrFailedToReadLEB128
}

func SizeLeb128(leb128 uint) uint {

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

View workflow job for this annotation

GitHub Actions / lint / Go

exported: exported function SizeLeb128 should have comment or be unexported (revive)
if (leb128 >> 24) > 0 {

Check failure on line 72 in codecs/av1/obu/leb128.go

View workflow job for this annotation

GitHub Actions / lint / Go

ifElseChain: rewrite if-else to switch statement (gocritic)
return 4

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

View check run for this annotation

Codecov / codecov/patch

codecs/av1/obu/leb128.go#L73

Added line #L73 was not covered by tests
} else if (leb128 >> 16) > 0 {
return 3
} else if (leb128 >> 8) > 0 {
return 2
}
return 1
}

// WriteToLeb128 writes a uint to a LEB128 encoded byte slice.
func WriteToLeb128(in uint) []byte {
b := make([]byte, 10)
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/pion/rtp

go 1.19

require github.com/pion/randutil v0.1.0
require (
github.com/pion/randutil v0.1.0
github.com/pion/webrtc/v3 v3.2.21
)