Skip to content

Commit

Permalink
Optimize the performance of H264 packaging
Browse files Browse the repository at this point in the history
NALU parsing was generating slices as it went. This updates
emitNalus to instead use bytes.Index which is more performant
  • Loading branch information
ziminghua authored and Sean-Der committed Sep 24, 2023
1 parent 67d2b3e commit bfe92b9
Showing 1 changed file with 24 additions and 31 deletions.
55 changes: 24 additions & 31 deletions codecs/h264_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package codecs

import (
"bytes"
"encoding/binary"
"fmt"
)
Expand Down Expand Up @@ -34,40 +35,32 @@ const (
outputStapAHeader = 0x78
)

func annexbNALUStartCode() []byte { return []byte{0x00, 0x00, 0x00, 0x01} }
// nolint:gochecknoglobals
var (
naluStartCode = []byte{0x00, 0x00, 0x01}
annexbNALUStartCode = []byte{0x00, 0x00, 0x00, 0x01}
)

func emitNalus(nals []byte, emit func([]byte)) {
nextInd := func(nalu []byte, start int) (indStart int, indLen int) {
zeroCount := 0

for i, b := range nalu[start:] {
if b == 0 {
zeroCount++
continue
} else if b == 1 {
if zeroCount >= 2 {
return start + i - zeroCount, zeroCount + 1
}
}
zeroCount = 0
start := 0
length := len(nals)

for start < length {
end := bytes.Index(nals[start:], annexbNALUStartCode)
offset := 4
if end == -1 {
end = bytes.Index(nals[start:], naluStartCode)
offset = 3
}
return -1, -1
}

nextIndStart, nextIndLen := nextInd(nals, 0)
if nextIndStart == -1 {
emit(nals)
} else {
for nextIndStart != -1 {
prevStart := nextIndStart + nextIndLen
nextIndStart, nextIndLen = nextInd(nals, prevStart)
if nextIndStart != -1 {
emit(nals[prevStart:nextIndStart])
} else {
// Emit until end of stream, no end indicator found
emit(nals[prevStart:])
}
if end == -1 {
emit(nals[start:])
break
}

emit(nals[start : start+end])

// next NAL start position
start += end + offset
}
}

Expand Down Expand Up @@ -203,7 +196,7 @@ func (p *H264Packet) doPackaging(nalu []byte) []byte {
return append(naluLength, nalu...)
}

return append(annexbNALUStartCode(), nalu...)
return append(annexbNALUStartCode, nalu...)
}

// IsDetectedFinalPacketInSequence returns true of the packet passed in has the
Expand Down

0 comments on commit bfe92b9

Please sign in to comment.