-
Notifications
You must be signed in to change notification settings - Fork 177
/
h265.go
233 lines (191 loc) · 5.03 KB
/
h265.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package formats
import (
"encoding/base64"
"fmt"
"strconv"
"sync"
"github.com/bluenviron/mediacommon/pkg/codecs/h265"
"github.com/pion/rtp"
"github.com/bluenviron/gortsplib/v3/pkg/formats/rtph265"
)
// H265 is a RTP format for the H265 codec.
// Specification: https://datatracker.ietf.org/doc/html/rfc7798
type H265 struct {
PayloadTyp uint8
VPS []byte
SPS []byte
PPS []byte
MaxDONDiff int
mutex sync.RWMutex
}
func (f *H265) unmarshal(ctx *unmarshalContext) error {
f.PayloadTyp = ctx.payloadType
for key, val := range ctx.fmtp {
switch key {
case "sprop-vps":
var err error
f.VPS, err = base64.StdEncoding.DecodeString(val)
if err != nil {
return fmt.Errorf("invalid sprop-vps (%v)", ctx.fmtp)
}
case "sprop-sps":
var err error
f.SPS, err = base64.StdEncoding.DecodeString(val)
if err != nil {
return fmt.Errorf("invalid sprop-sps (%v)", ctx.fmtp)
}
case "sprop-pps":
var err error
f.PPS, err = base64.StdEncoding.DecodeString(val)
if err != nil {
return fmt.Errorf("invalid sprop-pps (%v)", ctx.fmtp)
}
case "sprop-max-don-diff":
tmp, err := strconv.ParseUint(val, 10, 31)
if err != nil {
return fmt.Errorf("invalid sprop-max-don-diff (%v)", ctx.fmtp)
}
f.MaxDONDiff = int(tmp)
}
}
return nil
}
// Codec implements Format.
func (f *H265) Codec() string {
return "H265"
}
// String implements Format.
//
// Deprecated: replaced by Codec().
func (f *H265) String() string {
return f.Codec()
}
// ClockRate implements Format.
func (f *H265) ClockRate() int {
return 90000
}
// PayloadType implements Format.
func (f *H265) PayloadType() uint8 {
return f.PayloadTyp
}
// RTPMap implements Format.
func (f *H265) RTPMap() string {
return "H265/90000"
}
// FMTP implements Format.
func (f *H265) FMTP() map[string]string {
f.mutex.RLock()
defer f.mutex.RUnlock()
fmtp := make(map[string]string)
if f.VPS != nil {
fmtp["sprop-vps"] = base64.StdEncoding.EncodeToString(f.VPS)
}
if f.SPS != nil {
fmtp["sprop-sps"] = base64.StdEncoding.EncodeToString(f.SPS)
}
if f.PPS != nil {
fmtp["sprop-pps"] = base64.StdEncoding.EncodeToString(f.PPS)
}
if f.MaxDONDiff != 0 {
fmtp["sprop-max-don-diff"] = strconv.FormatInt(int64(f.MaxDONDiff), 10)
}
return fmtp
}
// PTSEqualsDTS implements Format.
func (f *H265) PTSEqualsDTS(pkt *rtp.Packet) bool {
if len(pkt.Payload) == 0 {
return false
}
typ := h265.NALUType((pkt.Payload[0] >> 1) & 0b111111)
switch typ {
case h265.NALUType_IDR_W_RADL, h265.NALUType_IDR_N_LP, h265.NALUType_CRA_NUT,
h265.NALUType_VPS_NUT, h265.NALUType_SPS_NUT, h265.NALUType_PPS_NUT:
return true
case h265.NALUType_AggregationUnit:
payload := pkt.Payload[2:]
for len(payload) > 0 {
if len(payload) < 2 {
return false
}
size := uint16(payload[0])<<8 | uint16(payload[1])
payload = payload[2:]
if size == 0 || int(size) > len(payload) {
return false
}
var nalu []byte
nalu, payload = payload[:size], payload[size:]
typ = h265.NALUType((nalu[0] >> 1) & 0b111111)
switch typ {
case h265.NALUType_IDR_W_RADL, h265.NALUType_IDR_N_LP, h265.NALUType_CRA_NUT,
h265.NALUType_VPS_NUT, h265.NALUType_SPS_NUT, h265.NALUType_PPS_NUT:
return true
}
}
case h265.NALUType_FragmentationUnit:
if len(pkt.Payload) < 3 {
return false
}
start := pkt.Payload[1] >> 7
if start != 1 {
return false
}
typ := h265.NALUType(pkt.Payload[2] & 0b111111)
switch typ {
case h265.NALUType_IDR_W_RADL, h265.NALUType_IDR_N_LP, h265.NALUType_CRA_NUT,
h265.NALUType_VPS_NUT, h265.NALUType_SPS_NUT, h265.NALUType_PPS_NUT:
return true
}
}
return false
}
// CreateDecoder creates a decoder able to decode the content of the format.
//
// Deprecated: this has been replaced by CreateDecoder2() that can also return an error.
func (f *H265) CreateDecoder() *rtph265.Decoder {
d, _ := f.CreateDecoder2()
return d
}
// CreateDecoder2 creates a decoder able to decode the content of the format.
func (f *H265) CreateDecoder2() (*rtph265.Decoder, error) {
d := &rtph265.Decoder{
MaxDONDiff: f.MaxDONDiff,
}
err := d.Init()
if err != nil {
return nil, err
}
return d, nil
}
// CreateEncoder creates an encoder able to encode the content of the format.
//
// Deprecated: this has been replaced by CreateEncoder2() that can also return an error.
func (f *H265) CreateEncoder() *rtph265.Encoder {
e, _ := f.CreateEncoder2()
return e
}
// CreateEncoder2 creates an encoder able to encode the content of the format.
func (f *H265) CreateEncoder2() (*rtph265.Encoder, error) {
e := &rtph265.Encoder{
PayloadType: f.PayloadTyp,
MaxDONDiff: f.MaxDONDiff,
}
err := e.Init()
if err != nil {
return nil, err
}
return e, nil
}
// SafeSetParams sets the codec parameters.
func (f *H265) SafeSetParams(vps []byte, sps []byte, pps []byte) {
f.mutex.Lock()
defer f.mutex.Unlock()
f.VPS = vps
f.SPS = sps
f.PPS = pps
}
// SafeParams returns the codec parameters.
func (f *H265) SafeParams() ([]byte, []byte, []byte) {
f.mutex.RLock()
defer f.mutex.RUnlock()
return f.VPS, f.SPS, f.PPS
}