Skip to content

Commit 14d69f4

Browse files
authored
Merge branch 'pion:master' into master
2 parents 76317a5 + 67d2b3e commit 14d69f4

File tree

9 files changed

+138
-91
lines changed

9 files changed

+138
-91
lines changed

.reuse/dep5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
22
Upstream-Name: Pion
33
Source: https://github.com/pion/
44

5-
Files: README.md DESIGN.md **/README.md AUTHORS.txt renovate.json go.mod go.sum .eslintrc.json package.json examples/examples.json
5+
Files: README.md DESIGN.md **/README.md AUTHORS.txt renovate.json go.mod go.sum **/go.mod **/go.sum .eslintrc.json package.json examples/examples.json
66
Copyright: 2023 The Pion community <https://pion.ly>
77
License: MIT
88

codecs/av1/frame/av1.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
2+
// SPDX-License-Identifier: MIT
3+
4+
// Package frame provides code to construct complete media frames from packetized media.
5+
package frame
6+
7+
import "github.com/pion/rtp/codecs"
8+
9+
// AV1 represents a collection of OBUs given a stream of AV1 Packets.
10+
// Each AV1 RTP Packet is a collection of OBU Elements. Each OBU Element may be a full OBU, or just a fragment of one.
11+
// AV1 provides the tools to construct a collection of OBUs from a collection of OBU Elements. This structure
12+
// contains an internal cache and should be used for the entire RTP Stream.
13+
type AV1 struct {
14+
// Buffer for fragmented OBU. If ReadFrames is called on a RTP Packet
15+
// that doesn't contain a fully formed OBU
16+
obuBuffer []byte
17+
}
18+
19+
func (f *AV1) pushOBUElement(isFirstOBUFragment *bool, obuElement []byte, obuList [][]byte) [][]byte {
20+
if *isFirstOBUFragment {
21+
*isFirstOBUFragment = false
22+
// Discard pushed because we don't have a fragment to combine it with
23+
if f.obuBuffer == nil {
24+
return obuList
25+
}
26+
obuElement = append(f.obuBuffer, obuElement...)
27+
f.obuBuffer = nil
28+
}
29+
return append(obuList, obuElement)
30+
}
31+
32+
// ReadFrames processes the codecs.AV1Packet and returns fully constructed frames
33+
func (f *AV1) ReadFrames(pkt *codecs.AV1Packet) ([][]byte, error) {
34+
OBUs := [][]byte{}
35+
isFirstOBUFragment := pkt.Z
36+
37+
for i := range pkt.OBUElements {
38+
OBUs = f.pushOBUElement(&isFirstOBUFragment, pkt.OBUElements[i], OBUs)
39+
}
40+
41+
if pkt.Y && len(OBUs) > 0 {
42+
// Take copy of OBUElement that is being cached
43+
f.obuBuffer = append(f.obuBuffer, append([]byte{}, OBUs[len(OBUs)-1]...)...)
44+
OBUs = OBUs[:len(OBUs)-1]
45+
}
46+
return OBUs, nil
47+
}
File renamed without changes.

codecs/av1/obu/leb128.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
2+
// SPDX-License-Identifier: MIT
3+
4+
// Package obu implements tools for working with the Open Bitstream Unit.
5+
package obu
6+
7+
import "errors"
8+
9+
const (
10+
sevenLsbBitmask = uint(0b01111111)
11+
msbBitmask = uint(0b10000000)
12+
)
13+
14+
// ErrFailedToReadLEB128 indicates that a buffer ended before a LEB128 value could be successfully read
15+
var ErrFailedToReadLEB128 = errors.New("payload ended before LEB128 was finished")
16+
17+
// EncodeLEB128 encodes a uint as LEB128
18+
func EncodeLEB128(in uint) (out uint) {
19+
for {
20+
// Copy seven bits from in and discard
21+
// what we have copied from in
22+
out |= (in & sevenLsbBitmask)
23+
in >>= 7
24+
25+
// If we have more bits to encode set MSB
26+
// otherwise we are done
27+
if in != 0 {
28+
out |= msbBitmask
29+
out <<= 8
30+
} else {
31+
return out
32+
}
33+
}
34+
}
35+
36+
func decodeLEB128(in uint) (out uint) {
37+
for {
38+
// Take 7 LSB from in
39+
out |= (in & sevenLsbBitmask)
40+
41+
// Discard the MSB
42+
in >>= 8
43+
if in == 0 {
44+
return out
45+
}
46+
47+
out <<= 7
48+
}
49+
}
50+
51+
// ReadLeb128 scans an buffer and decodes a Leb128 value.
52+
// If the end of the buffer is reached and all MSB are set
53+
// an error is returned
54+
func ReadLeb128(in []byte) (uint, uint, error) {
55+
var encodedLength uint
56+
57+
for i := range in {
58+
encodedLength |= uint(in[i])
59+
60+
if in[i]&byte(msbBitmask) == 0 {
61+
return decodeLEB128(encodedLength), uint(i + 1), nil
62+
}
63+
64+
// Make more room for next read
65+
encodedLength <<= 8
66+
}
67+
68+
return 0, 0, ErrFailedToReadLEB128
69+
}
File renamed without changes.

codecs/av1_packet.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
package codecs
55

66
import (
7-
"github.com/pion/rtp/pkg/obu"
7+
"github.com/pion/rtp/codecs/av1/obu"
88
)
99

1010
const (

codecs/av1_packet_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"reflect"
1010
"testing"
1111

12-
"github.com/pion/rtp/pkg/obu"
12+
"github.com/pion/rtp/codecs/av1/obu"
1313
)
1414

1515
func TestAV1_Marshal(t *testing.T) {

pkg/frame/av1.go

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,17 @@
11
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
22
// SPDX-License-Identifier: MIT
33

4-
// Package frame provides code to construct complete media frames from packetized media
4+
// Package frame is deprecated.
55
package frame
66

7-
import "github.com/pion/rtp/codecs"
7+
import (
8+
"github.com/pion/rtp/codecs/av1/frame"
9+
)
810

911
// AV1 represents a collection of OBUs given a stream of AV1 Packets.
1012
// Each AV1 RTP Packet is a collection of OBU Elements. Each OBU Element may be a full OBU, or just a fragment of one.
1113
// AV1 provides the tools to construct a collection of OBUs from a collection of OBU Elements. This structure
1214
// contains an internal cache and should be used for the entire RTP Stream.
13-
type AV1 struct {
14-
// Buffer for fragmented OBU. If ReadFrames is called on a RTP Packet
15-
// that doesn't contain a fully formed OBU
16-
obuBuffer []byte
17-
}
18-
19-
func (f *AV1) pushOBUElement(isFirstOBUFragment *bool, obuElement []byte, obuList [][]byte) [][]byte {
20-
if *isFirstOBUFragment {
21-
*isFirstOBUFragment = false
22-
// Discard pushed because we don't have a fragment to combine it with
23-
if f.obuBuffer == nil {
24-
return obuList
25-
}
26-
obuElement = append(f.obuBuffer, obuElement...)
27-
f.obuBuffer = nil
28-
}
29-
return append(obuList, obuElement)
30-
}
31-
32-
// ReadFrames processes the codecs.AV1Packet and returns fully constructed frames
33-
func (f *AV1) ReadFrames(pkt *codecs.AV1Packet) ([][]byte, error) {
34-
OBUs := [][]byte{}
35-
isFirstOBUFragment := pkt.Z
36-
37-
for i := range pkt.OBUElements {
38-
OBUs = f.pushOBUElement(&isFirstOBUFragment, pkt.OBUElements[i], OBUs)
39-
}
40-
41-
if pkt.Y && len(OBUs) > 0 {
42-
// Take copy of OBUElement that is being cached
43-
f.obuBuffer = append(f.obuBuffer, append([]byte{}, OBUs[len(OBUs)-1]...)...)
44-
OBUs = OBUs[:len(OBUs)-1]
45-
}
46-
return OBUs, nil
47-
}
15+
//
16+
// Deprecated: moved into codecs/av1/frame.
17+
type AV1 = frame.AV1

pkg/obu/leb128.go

Lines changed: 12 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,30 @@
11
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
22
// SPDX-License-Identifier: MIT
33

4-
// Package obu implements tools for working with the "Open Bitstream Unit"
4+
// Package obu is deprecated.
55
package obu
66

7-
import "errors"
8-
9-
const (
10-
sevenLsbBitmask = uint(0b01111111)
11-
msbBitmask = uint(0b10000000)
7+
import (
8+
"github.com/pion/rtp/codecs/av1/obu"
129
)
1310

1411
// ErrFailedToReadLEB128 indicates that a buffer ended before a LEB128 value could be successfully read
15-
var ErrFailedToReadLEB128 = errors.New("payload ended before LEB128 was finished")
12+
//
13+
// Deprecated: moved into codecs/av1/obu.
14+
var ErrFailedToReadLEB128 = obu.ErrFailedToReadLEB128
1615

1716
// EncodeLEB128 encodes a uint as LEB128
17+
//
18+
// Deprecated: moved into codecs/av1/obu.
1819
func EncodeLEB128(in uint) (out uint) {
19-
for {
20-
// Copy seven bits from in and discard
21-
// what we have copied from in
22-
out |= (in & sevenLsbBitmask)
23-
in >>= 7
24-
25-
// If we have more bits to encode set MSB
26-
// otherwise we are done
27-
if in != 0 {
28-
out |= msbBitmask
29-
out <<= 8
30-
} else {
31-
return out
32-
}
33-
}
34-
}
35-
36-
func decodeLEB128(in uint) (out uint) {
37-
for {
38-
// Take 7 LSB from in
39-
out |= (in & sevenLsbBitmask)
40-
41-
// Discard the MSB
42-
in >>= 8
43-
if in == 0 {
44-
return out
45-
}
46-
47-
out <<= 7
48-
}
20+
return obu.EncodeLEB128(in)
4921
}
5022

5123
// ReadLeb128 scans an buffer and decodes a Leb128 value.
5224
// If the end of the buffer is reached and all MSB are set
5325
// an error is returned
26+
//
27+
// Deprecated: moved into codecs/av1/obu.
5428
func ReadLeb128(in []byte) (uint, uint, error) {
55-
var encodedLength uint
56-
57-
for i := range in {
58-
encodedLength |= uint(in[i])
59-
60-
if in[i]&byte(msbBitmask) == 0 {
61-
return decodeLEB128(encodedLength), uint(i + 1), nil
62-
}
63-
64-
// Make more room for next read
65-
encodedLength <<= 8
66-
}
67-
68-
return 0, 0, ErrFailedToReadLEB128
29+
return obu.ReadLeb128(in)
6930
}

0 commit comments

Comments
 (0)