Skip to content

Commit

Permalink
Convert H264Packet.doPackaging to append style
Browse files Browse the repository at this point in the history
Not a big deal, avoids a couple of allocations for every
STAP-A packet.
  • Loading branch information
jech authored and Sean-Der committed Apr 23, 2024
1 parent 74a9dc7 commit 32ee92e
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions codecs/h264_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,16 @@ type H264Packet struct {
videoDepacketizer
}

func (p *H264Packet) doPackaging(nalu []byte) []byte {
func (p *H264Packet) doPackaging(buf, nalu []byte) []byte {
if p.IsAVC {
naluLength := make([]byte, 4)
binary.BigEndian.PutUint32(naluLength, uint32(len(nalu)))
return append(naluLength, nalu...)
buf = binary.BigEndian.AppendUint32(buf, uint32(len(nalu)))
buf = append(buf, nalu...)
return buf
}

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

// IsDetectedFinalPacketInSequence returns true of the packet passed in has the
Expand All @@ -215,7 +217,7 @@ func (p *H264Packet) Unmarshal(payload []byte) ([]byte, error) {
naluType := payload[0] & naluTypeBitmask
switch {
case naluType > 0 && naluType < 24:
return p.doPackaging(payload), nil
return p.doPackaging(nil, payload), nil

case naluType == stapaNALUType:
currOffset := int(stapaHeaderSize)
Expand All @@ -228,7 +230,7 @@ func (p *H264Packet) Unmarshal(payload []byte) ([]byte, error) {
return nil, fmt.Errorf("%w STAP-A declared size(%d) is larger than buffer(%d)", errShortPacket, naluSize, len(payload)-currOffset)
}

result = append(result, p.doPackaging(payload[currOffset:currOffset+naluSize])...)
result = p.doPackaging(result, payload[currOffset:currOffset+naluSize])
currOffset += naluSize
}
return result, nil
Expand All @@ -251,7 +253,7 @@ func (p *H264Packet) Unmarshal(payload []byte) ([]byte, error) {
nalu := append([]byte{}, naluRefIdc|fragmentedNaluType)
nalu = append(nalu, p.fuaBuffer...)
p.fuaBuffer = nil
return p.doPackaging(nalu), nil
return p.doPackaging(nil, nalu), nil
}

return []byte{}, nil
Expand Down

0 comments on commit 32ee92e

Please sign in to comment.