-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathvlaextension.go
More file actions
436 lines (371 loc) · 11.6 KB
/
Copy pathvlaextension.go
File metadata and controls
436 lines (371 loc) · 11.6 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package rtp
import (
"encoding/binary"
"errors"
"fmt"
"io"
"strings"
"github.com/pion/rtp/codecs/av1/obu"
)
var (
// ErrVLATooShort is returned when payload is too short.
ErrVLATooShort = errors.New("VLA payload too short")
// ErrVLAInvalidStreamCount is returned when RTP stream count is invalid.
ErrVLAInvalidStreamCount = errors.New("invalid RTP stream count in VLA")
// ErrVLAInvalidStreamID is returned when RTP stream ID is invalid.
ErrVLAInvalidStreamID = errors.New("invalid RTP stream ID in VLA")
// ErrVLAInvalidSpatialID is returned when spatial ID is invalid.
ErrVLAInvalidSpatialID = errors.New("invalid spatial ID in VLA")
// ErrVLADuplicateSpatialID is returned when spatial ID is invalid.
ErrVLADuplicateSpatialID = errors.New("duplicate spatial ID in VLA")
// ErrVLAInvalidTemporalLayer is returned when temporal layer is invalid.
ErrVLAInvalidTemporalLayer = errors.New("invalid temporal layer in VLA")
)
// SpatialLayer is a spatial layer in VLA.
type SpatialLayer struct {
RTPStreamID int
SpatialID int
TargetBitrates []int // target bitrates per temporal layer
// Following members are valid only when HasResolutionAndFramerate is true
Width int
Height int
Framerate int
}
// VLA is a Video Layer Allocation (VLA) extension.
// See https://webrtc.googlesource.com/src/+/refs/heads/main/docs/native-code/rtp-hdrext/video-layers-allocation00
type VLA struct {
RTPStreamID int // 0-origin RTP stream ID (RID) this allocation is sent on (0..3)
RTPStreamCount int // Number of RTP streams (1..4)
ActiveSpatialLayer []SpatialLayer
HasResolutionAndFramerate bool
}
type vlaMarshalingContext struct {
slMBs [4]uint8
slIndices [4][4]int // index into ActiveSpatialLayer, -1 if not set
commonSLBM uint8
requiredLen int
}
func (v VLA) preprocessForMashaling(ctx *vlaMarshalingContext) error { //nolint:cyclop
// Initialize indices to -1 (not set)
for i := range ctx.slIndices {
for j := range ctx.slIndices[i] {
ctx.slIndices[i][j] = -1
}
}
for i := range v.ActiveSpatialLayer {
sl := &v.ActiveSpatialLayer[i]
if sl.RTPStreamID < 0 || sl.RTPStreamID >= v.RTPStreamCount {
return fmt.Errorf("invalid RTP streamID %d:%w", sl.RTPStreamID, ErrVLAInvalidStreamID)
}
if sl.SpatialID < 0 || sl.SpatialID >= 4 {
return fmt.Errorf("invalid spatial ID %d: %w", sl.SpatialID, ErrVLAInvalidSpatialID)
}
if len(sl.TargetBitrates) == 0 || len(sl.TargetBitrates) > 4 {
return fmt.Errorf("invalid temporal layer count %d: %w", len(sl.TargetBitrates), ErrVLAInvalidTemporalLayer)
}
ctx.slMBs[sl.RTPStreamID] |= 1 << sl.SpatialID
if ctx.slIndices[sl.RTPStreamID][sl.SpatialID] != -1 {
return fmt.Errorf("duplicate spatial layer: %w", ErrVLADuplicateSpatialID)
}
ctx.slIndices[sl.RTPStreamID][sl.SpatialID] = i
}
return nil
}
func (v VLA) calcTargetBitratesSize(ctx *vlaMarshalingContext) {
for rtpStreamID := 0; rtpStreamID < v.RTPStreamCount; rtpStreamID++ {
for spatialID := range 4 {
if idx := ctx.slIndices[rtpStreamID][spatialID]; idx >= 0 {
for _, kbps := range v.ActiveSpatialLayer[idx].TargetBitrates {
ctx.requiredLen += leb128Size(uint(kbps)) //nolint:gosec
}
}
}
}
}
func (v VLA) analyzeVLAForMarshaling(ctx *vlaMarshalingContext) error {
// Validate RTPStreamCount
if v.RTPStreamCount <= 0 || v.RTPStreamCount > 4 {
return ErrVLAInvalidStreamCount
}
// Validate RTPStreamID
if v.RTPStreamID < 0 || v.RTPStreamID >= v.RTPStreamCount {
return ErrVLAInvalidStreamID
}
err := v.preprocessForMashaling(ctx)
if err != nil {
return err
}
ctx.commonSLBM = commonSLBMValues(ctx.slMBs[:])
// RID, NS, sl_bm fields
if ctx.commonSLBM != 0 {
ctx.requiredLen = 1
} else {
ctx.requiredLen = 3
}
// #tl fields
ctx.requiredLen += (len(v.ActiveSpatialLayer)-1)/4 + 1
v.calcTargetBitratesSize(ctx)
if v.HasResolutionAndFramerate {
ctx.requiredLen += len(v.ActiveSpatialLayer) * 5
}
return nil
}
// MarshalSize returns the size needed to marshal the VLA.
func (v VLA) MarshalSize() (int, error) {
var ctx vlaMarshalingContext
if err := v.analyzeVLAForMarshaling(&ctx); err != nil {
return 0, err
}
return ctx.requiredLen, nil
}
// MarshalTo marshals the VLA to the given buffer.
// Returns io.ErrShortBuffer if buf is too small.
func (v VLA) MarshalTo(buf []byte) (int, error) { //nolint:cyclop,gocognit
var ctx vlaMarshalingContext
if err := v.analyzeVLAForMarshaling(&ctx); err != nil {
return 0, err
}
if len(buf) < ctx.requiredLen {
return 0, io.ErrShortBuffer
}
offset := 0
// RID, NS, sl_bm fields
buf[offset] = byte(v.RTPStreamID<<6) | byte(v.RTPStreamCount-1)<<4 | ctx.commonSLBM //nolint:gosec // values are small
if ctx.commonSLBM == 0 {
offset++
for streamID := 0; streamID < v.RTPStreamCount; streamID++ {
if streamID%2 == 0 {
buf[offset+streamID/2] |= ctx.slMBs[streamID] << 4
} else {
buf[offset+streamID/2] |= ctx.slMBs[streamID]
}
}
offset += (v.RTPStreamCount - 1) / 2
}
// #tl fields
offset++
var temporalLayerIndex int
for rtpStreamID := 0; rtpStreamID < v.RTPStreamCount; rtpStreamID++ {
for spatialID := range 4 {
if idx := ctx.slIndices[rtpStreamID][spatialID]; idx >= 0 {
if temporalLayerIndex >= 4 {
temporalLayerIndex = 0
offset++
}
//nolint:gosec // values are small
buf[offset] |= byte(len(v.ActiveSpatialLayer[idx].TargetBitrates)-1) << (2 * (3 - temporalLayerIndex))
temporalLayerIndex++
}
}
}
// Target bitrate fields
offset++
for rtpStreamID := 0; rtpStreamID < v.RTPStreamCount; rtpStreamID++ {
for spatialID := range 4 {
if idx := ctx.slIndices[rtpStreamID][spatialID]; idx >= 0 {
for _, kbps := range v.ActiveSpatialLayer[idx].TargetBitrates {
offset += writeLeb128To(buf[offset:], uint(kbps)) //nolint:gosec
}
}
}
}
// Resolution & framerate fields
if v.HasResolutionAndFramerate {
for _, sl := range v.ActiveSpatialLayer {
binary.BigEndian.PutUint16(buf[offset+0:], uint16(sl.Width-1)) //nolint:gosec
binary.BigEndian.PutUint16(buf[offset+2:], uint16(sl.Height-1)) //nolint:gosec
buf[offset+4] = byte(sl.Framerate) //nolint:gosec
offset += 5
}
}
return ctx.requiredLen, nil
}
// Marshal encodes VLA into a byte slice.
func (v VLA) Marshal() ([]byte, error) {
size, err := v.MarshalSize()
if err != nil {
return nil, err
}
buf := make([]byte, size)
_, err = v.MarshalTo(buf)
if err != nil {
return nil, err
}
return buf, nil
}
func commonSLBMValues(slMBs []uint8) uint8 {
var common uint8
for i := range slMBs {
if slMBs[i] == 0 {
continue
}
if common == 0 {
common = slMBs[i]
continue
}
if slMBs[i] != common {
return 0
}
}
return common
}
type vlaUnmarshalingContext struct {
payload []byte
offset int
slBMField uint8
slBMs [4]uint8
}
func (ctx *vlaUnmarshalingContext) checkRemainingLen(requiredLen int) bool {
return len(ctx.payload)-ctx.offset >= requiredLen
}
func (v *VLA) unmarshalSpatialLayers(ctx *vlaUnmarshalingContext) error {
if !ctx.checkRemainingLen(1) {
return fmt.Errorf("failed to unmarshal VLA (offset=%d): %w", ctx.offset, ErrVLATooShort)
}
v.RTPStreamID = int(ctx.payload[ctx.offset] >> 6 & 0b11)
v.RTPStreamCount = int(ctx.payload[ctx.offset]>>4&0b11) + 1
// sl_bm fields
ctx.slBMField = ctx.payload[ctx.offset] & 0b1111
ctx.offset++
if ctx.slBMField != 0 {
for streamID := 0; streamID < v.RTPStreamCount; streamID++ {
ctx.slBMs[streamID] = ctx.slBMField
}
} else {
if !ctx.checkRemainingLen((v.RTPStreamCount-1)/2 + 1) {
return fmt.Errorf("failed to unmarshal VLA (offset=%d): %w", ctx.offset, ErrVLATooShort)
}
// slX_bm fields
for streamID := 0; streamID < v.RTPStreamCount; streamID++ {
var bm uint8
if streamID%2 == 0 {
bm = ctx.payload[ctx.offset+streamID/2] >> 4 & 0b1111
} else {
bm = ctx.payload[ctx.offset+streamID/2] & 0b1111
}
ctx.slBMs[streamID] = bm
}
ctx.offset += 1 + (v.RTPStreamCount-1)/2
}
return nil
}
func (v *VLA) unmarshalTemporalLayers(ctx *vlaUnmarshalingContext) error { // nolint: cyclop
if !ctx.checkRemainingLen(1) {
return fmt.Errorf("failed to unmarshal VLA (offset=%d): %w", ctx.offset, ErrVLATooShort)
}
var temporalLayerIndex int
for streamID := 0; streamID < v.RTPStreamCount; streamID++ {
for spatialID := range 4 {
if ctx.slBMs[streamID]&(1<<spatialID) == 0 {
continue
}
if temporalLayerIndex >= 4 {
temporalLayerIndex = 0
ctx.offset++
if !ctx.checkRemainingLen(1) {
return fmt.Errorf("failed to unmarshal VLA (offset=%d): %w", ctx.offset, ErrVLATooShort)
}
}
tlCount := int(ctx.payload[ctx.offset]>>(2*(3-temporalLayerIndex))&0b11) + 1
temporalLayerIndex++
sl := SpatialLayer{
RTPStreamID: streamID,
SpatialID: spatialID,
TargetBitrates: make([]int, tlCount),
}
v.ActiveSpatialLayer = append(v.ActiveSpatialLayer, sl)
}
}
ctx.offset++
// target bitrates
for i, sl := range v.ActiveSpatialLayer {
for j := range sl.TargetBitrates {
kbps, n, err := obu.ReadLeb128(ctx.payload[ctx.offset:])
if err != nil {
return err
}
in := int(n) // nolint: gosec
if !ctx.checkRemainingLen(in) {
return fmt.Errorf("failed to unmarshal VLA (offset=%d): %w", ctx.offset, ErrVLATooShort)
}
v.ActiveSpatialLayer[i].TargetBitrates[j] = int(kbps) // nolint: gosec
ctx.offset += in
}
}
return nil
}
func (v *VLA) unmarshalResolutionAndFramerate(ctx *vlaUnmarshalingContext) error {
if !ctx.checkRemainingLen(len(v.ActiveSpatialLayer) * 5) {
return fmt.Errorf("failed to unmarshal VLA (offset=%d): %w", ctx.offset, ErrVLATooShort)
}
v.HasResolutionAndFramerate = true
for i := range v.ActiveSpatialLayer {
v.ActiveSpatialLayer[i].Width = int(binary.BigEndian.Uint16(ctx.payload[ctx.offset+0:])) + 1
v.ActiveSpatialLayer[i].Height = int(binary.BigEndian.Uint16(ctx.payload[ctx.offset+2:])) + 1
v.ActiveSpatialLayer[i].Framerate = int(ctx.payload[ctx.offset+4])
ctx.offset += 5
}
return nil
}
// Unmarshal decodes VLA from a byte slice.
func (v *VLA) Unmarshal(payload []byte) (int, error) {
ctx := &vlaUnmarshalingContext{
payload: payload,
}
err := v.unmarshalSpatialLayers(ctx)
if err != nil {
return ctx.offset, err
}
// #tl fields (build the list ActiveSpatialLayer at the same time)
err = v.unmarshalTemporalLayers(ctx)
if err != nil {
return ctx.offset, err
}
if len(ctx.payload) == ctx.offset {
return ctx.offset, nil
}
// resolution & framerate (optional)
err = v.unmarshalResolutionAndFramerate(ctx)
if err != nil {
return ctx.offset, err
}
return ctx.offset, nil
}
// String makes VLA printable.
func (v VLA) String() string {
out := fmt.Sprintf("RID:%d,RTPStreamCount:%d", v.RTPStreamID, v.RTPStreamCount)
var slOut []string
for _, sl := range v.ActiveSpatialLayer {
out2 := fmt.Sprintf("RTPStreamID:%d", sl.RTPStreamID)
out2 += fmt.Sprintf(",TargetBitrates:%v", sl.TargetBitrates)
if v.HasResolutionAndFramerate {
out2 += fmt.Sprintf(",Resolution:(%d,%d)", sl.Width, sl.Height)
out2 += fmt.Sprintf(",Framerate:%d", sl.Framerate)
}
slOut = append(slOut, out2)
}
out += fmt.Sprintf(",ActiveSpatialLayers:{%s}", strings.Join(slOut, ","))
return out
}
// leb128Size returns the number of bytes needed to encode a value as LEB128.
func leb128Size(in uint) int {
size := 1
for in >>= 7; in != 0; in >>= 7 {
size++
}
return size
}
// writeLeb128To writes a LEB128 encoded value to buf and returns bytes written.
func writeLeb128To(buf []byte, in uint) int {
for i := range buf {
buf[i] = byte(in & 0x7f)
in >>= 7
if in == 0 {
return i + 1
}
buf[i] |= 0x80
}
return 0
}