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

Additional testing and associated bug fixes related to H.265 payloader #287

Open
wants to merge 4 commits into
base: h265
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions codecs/h265_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ type H265Payloader struct {
// Payload fragments a H265 packet across one or more byte arrays
func (p *H265Payloader) Payload(mtu uint16, payload []byte) [][]byte { //nolint: gocognit
var payloads [][]byte
if len(payload) == 0 {
if len(payload) == 0 || mtu == 0 {
return payloads
}

Expand Down Expand Up @@ -998,7 +998,7 @@ func (p *H265Payloader) Payload(mtu uint16, payload []byte) [][]byte { //nolint:
}
} else {
// construct an aggregation packet
aggregationPacketSize := aggregationBufferSize + 2
aggregationPacketSize := aggregationBufferSize
buf := make([]byte, aggregationPacketSize)

layerID := uint8(math.MaxUint8)
Expand Down Expand Up @@ -1039,22 +1039,39 @@ func (p *H265Payloader) Payload(mtu uint16, payload []byte) [][]byte { //nolint:
aggregationBufferSize = 0
}

calcMarginalAggregationSize := func(nalu []byte) int {
marginalAggregationSize := len(nalu) + 2 // +2 is NALU size Field size
if len(bufferedNALUs) == 1 {
marginalAggregationSize = len(nalu) + 4 // +4 are Aggregation header + NALU size Field size
}
if p.AddDONL {
if len(bufferedNALUs) == 0 {
marginalAggregationSize += 2
} else {
marginalAggregationSize++
}
}
return marginalAggregationSize
}

emitNalus(payload, func(nalu []byte) {
if len(nalu) == 0 {
if len(nalu) < 2 {
// NALU header is 2 bytes
return
}

if len(nalu) <= int(mtu) {
naluLen := len(nalu) + 2
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent the MTU size from being exceeded during aggregation packet, the size is compared by adding 2 bytes for the aggregation header.

if p.AddDONL {
naluLen += 2
}
if naluLen <= int(mtu) {
// this nalu fits into a single packet, either it can be emitted as
// a single nalu or appended to the previous aggregation packet

marginalAggregationSize := len(nalu) + 2
if p.AddDONL {
marginalAggregationSize++
}
marginalAggregationSize := calcMarginalAggregationSize(nalu)

if aggregationBufferSize+marginalAggregationSize > int(mtu) {
flushBufferedNals()
marginalAggregationSize = calcMarginalAggregationSize(nalu)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After flushing, recalculate because the margin conditions change.

}
bufferedNALUs = append(bufferedNALUs, nalu)
aggregationBufferSize += marginalAggregationSize
Expand All @@ -1077,7 +1094,7 @@ func (p *H265Payloader) Payload(mtu uint16, payload []byte) [][]byte { //nolint:
// the nalu header is omitted from the fragmentation packet payload
nalu = nalu[h265NaluHeaderSize:]

if maxFUPayloadSize == 0 || len(nalu) == 0 {
if maxFUPayloadSize <= 0 || len(nalu) == 0 {
return
}

Expand Down
Loading