diff --git a/codecs/av1_packet.go b/codecs/av1_packet.go index 3a78b90..fa843de 100644 --- a/codecs/av1_packet.go +++ b/codecs/av1_packet.go @@ -134,6 +134,8 @@ type AV1Packet struct { // Each AV1 RTP Packet is a collection of OBU Elements. Each OBU Element may be a full OBU, or just a fragment of one. // AV1Frame provides the tools to construct a collection of OBUs from a collection of OBU Elements OBUElements [][]byte + + videoDepacketizer } // Unmarshal parses the passed byte slice and stores the result in the AV1Packet this method is called upon @@ -186,3 +188,48 @@ func (p *AV1Packet) Unmarshal(payload []byte) ([]byte, error) { return payload[1:], nil } + +func (p *AV1Packet) IsPartitionHead(payload []byte) bool { + // at this point, p.Unmarshal may not have been called yet. Check + // manually for Z == 0 & N == 1. + return len(payload) > 0 && (payload[0] & 0x88) != 0x08 +} + +// AppendToSample appends the contents of the packet to a sample. It +// reconstructs the size of the last OBU if required. +func (p *AV1Packet) AppendToSample(sample, payload []byte) []byte { + if len(payload) < 1 { + return sample + } + + switch p.W { + case 0: + sample = append(sample, payload...) + case 1: + sample = append(sample, obu.WriteToLeb128(uint(len(payload)))...) + sample = append(sample, payload...) + case 2: + l1, ll1, err := obu.ReadLeb128(payload) + if err != nil { + return sample + } + sample = append(sample, payload[:l1+ll1]...) + l2 := uint(len(payload)) - (l1 + ll1) + sample = append(sample, obu.WriteToLeb128(l2)...) + sample = append(sample, payload[l1+ll1:]...) + case 3: + l1, ll1, err := obu.ReadLeb128(payload) + if err != nil { + return sample + } + l2, ll2, err := obu.ReadLeb128(payload[l1+ll1:]) + if err != nil { + return sample + } + l3 := uint(len(payload)) - (l1 + ll1 + l2 + ll2) + sample = append(sample, payload[:l1+ll1+l2+ll2]...) + sample = append(sample, obu.WriteToLeb128(l3)...) + sample = append(sample, payload[l1+ll1+l2+ll2:]...) + } + return sample +}