Repository: pion/rtp
Pull request #163 (2022-02-03 03:09:12)

Add IsPartitionHead and IsPartitionTail to H265
This change makes the H265 packet satisfy the depacketizer
interface allowing H265 streams to be decoded with
SampleBuilder.

codecov[bot]: # [Codecov](https://codecov.io/gh/pion/rtp/pull/163?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) Report
> Merging [#163](https://codecov.io/gh/pion/rtp/pull/163?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) (103d211) into [master](https://codecov.io/gh/pion/rtp/commit/831d81e119a2ebdbeaf84dfc9a9ec3efc542b8b6?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) (831d81e) will **decrease** coverage by `0.42%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/pion/rtp/pull/163/graphs/tree.svg?width=650&height=150&src=pr&token=4KrbiBsspj&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion)](https://codecov.io/gh/pion/rtp/pull/163?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion)

```diff
@@            Coverage Diff             @@
##           master     #163      +/-   ##
==========================================
- Coverage   86.25%   85.83%   -0.43%     
==========================================
  Files          15       15              
  Lines        1630     1638       +8     
==========================================
  Hits         1406     1406              
- Misses        195      201       +6     
- Partials       29       31       +2     
```

| Flag | Coverage Δ | |
|---|---|---|
| go | `?` | |
| wasm | `85.83% <100.00%> (+0.06%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/pion/rtp/pull/163?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) | Coverage Δ | |
|---|---|---|
| [codecs/h265\_packet.go](https://codecov.io/gh/pion/rtp/pull/163/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-Y29kZWNzL2gyNjVfcGFja2V0Lmdv) | `100.00% <100.00%> (ø)` | |
| [packet.go](https://codecov.io/gh/pion/rtp/pull/163/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-cGFja2V0Lmdv) | `85.63% <0.00%> (-2.35%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pion/rtp/pull/163?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pion/rtp/pull/163?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion). Last update [831d81e...103d211](https://codecov.io/gh/pion/rtp/pull/163?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion).


Patch:
diff --git a/codecs/h265_packet.go b/codecs/h265_packet.go
index a4ae843..6f0490d 100644
--- a/codecs/h265_packet.go
+++ b/codecs/h265_packet.go
@@ -1,6 +1,7 @@
 package codecs
 
 import (
+	"encoding/binary"
 	"errors"
 	"fmt"
 )
@@ -727,6 +728,8 @@ var (
 type H265Packet struct {
 	packet        isH265Packet
 	mightNeedDONL bool
+
+	videoDepacketizer
 }
 
 // WithDONL can be called to specify whether or not DONL might be parsed.
@@ -801,3 +804,16 @@ func (p *H265Packet) Unmarshal(payload []byte) ([]byte, error) {
 func (p *H265Packet) Packet() isH265Packet {
 	return p.packet
 }
+
+// IsPartitionHead checks if this is the head of a packetized nalu stream.
+func (*H265Packet) IsPartitionHead(payload []byte) bool {
+	if len(payload) < 3 {
+		return false
+	}
+
+	if H265NALUHeader(binary.BigEndian.Uint16(payload[0:2])).Type() == h265NaluFragmentationUnitType {
+		return H265FragmentationUnitHeader(payload[2]).S()
+	}
+
+	return true
+}


Test patch:
diff --git a/codecs/h265_packet_test.go b/codecs/h265_packet_test.go
index 7c25a9c..2172abe 100644
--- a/codecs/h265_packet_test.go
+++ b/codecs/h265_packet_test.go
@@ -812,6 +812,39 @@ func TestH265_Packet(t *testing.T) {
 	}
 }
 
+func TestH265IsPartitionHead(t *testing.T) {
+	h265 := H265Packet{}
+
+	if h265.IsPartitionHead(nil) {
+		t.Fatal("nil must not be a partition head")
+	}
+
+	emptyNalu := []byte{}
+	if h265.IsPartitionHead(emptyNalu) {
+		t.Fatal("empty nalu must not be a partition head")
+	}
+
+	singleNalu := []byte{0x01, 0x01, 0xab, 0xcd, 0xef}
+	if h265.IsPartitionHead(singleNalu) == false {
+		t.Fatal("single nalu must be a partition head")
+	}
+
+	fbitNalu := []byte{0x80, 0x00, 0x00}
+	if h265.IsPartitionHead(fbitNalu) == false {
+		t.Fatal("fbit nalu must be a partition head")
+	}
+
+	fuStartNalu := []byte{0x62, 0x01, 0x93}
+	if h265.IsPartitionHead(fuStartNalu) == false {
+		t.Fatal("fu start nalu must be a partition head")
+	}
+
+	fuEndNalu := []byte{0x62, 0x01, 0x53}
+	if h265.IsPartitionHead(fuEndNalu) {
+		t.Fatal("fu end nalu must not be a partition head")
+	}
+}
+
 func TestH265_Packet_Real(t *testing.T) {
 	// Tests decoding of real H265 payloads extracted from a Wireshark dump.