-
Notifications
You must be signed in to change notification settings - Fork 50
/
data_pes_test.go
506 lines (459 loc) · 14.7 KB
/
data_pes_test.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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
package astits
import (
"bytes"
"testing"
"github.com/asticode/go-astikit"
"github.com/stretchr/testify/assert"
)
func TestHasPESOptionalHeader(t *testing.T) {
var a []int
for i := 0; i <= 255; i++ {
if !hasPESOptionalHeader(uint8(i)) {
a = append(a, i)
}
}
assert.Equal(t, []int{StreamIDPaddingStream, StreamIDPrivateStream2}, a)
}
var dsmTrickModeSlow = &DSMTrickMode{
RepeatControl: 21,
TrickModeControl: TrickModeControlSlowMotion,
}
func dsmTrickModeSlowBytes() []byte {
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
w.Write("001") // Control
w.Write("10101") // Repeat control
return buf.Bytes()
}
type dsmTrickModeTestCase struct {
name string
bytesFunc func(w *astikit.BitsWriter)
trickMode *DSMTrickMode
}
var dsmTrickModeTestCases = []dsmTrickModeTestCase{
{
"fast_forward",
func(w *astikit.BitsWriter) {
w.Write("000") // Control
w.Write("10") // Field ID
w.Write("1") // Intra slice refresh
w.Write("11") // Frequency truncation
},
&DSMTrickMode{
FieldID: 2,
FrequencyTruncation: 3,
IntraSliceRefresh: 1,
TrickModeControl: TrickModeControlFastForward,
},
},
{
"slow_motion",
func(w *astikit.BitsWriter) {
w.Write("001")
w.Write("10101")
},
&DSMTrickMode{
RepeatControl: 0b10101,
TrickModeControl: TrickModeControlSlowMotion,
},
},
{
"freeze_frame",
func(w *astikit.BitsWriter) {
w.Write("010") // Control
w.Write("10") // Field ID
w.Write("111") // Reserved
},
&DSMTrickMode{
FieldID: 2,
TrickModeControl: TrickModeControlFreezeFrame,
},
},
{
"fast_reverse",
func(w *astikit.BitsWriter) {
w.Write("011") // Control
w.Write("10") // Field ID
w.Write("1") // Intra slice refresh
w.Write("11") // Frequency truncation
},
&DSMTrickMode{
FieldID: 2,
FrequencyTruncation: 3,
IntraSliceRefresh: 1,
TrickModeControl: TrickModeControlFastReverse,
},
},
{
"slow_reverse",
func(w *astikit.BitsWriter) {
w.Write("100")
w.Write("01010")
},
&DSMTrickMode{
RepeatControl: 0b01010,
TrickModeControl: TrickModeControlSlowReverse,
},
},
{
"reserved",
func(w *astikit.BitsWriter) {
w.Write("101")
w.Write("11111")
},
&DSMTrickMode{
TrickModeControl: 5, // reserved
},
},
}
func TestParseDSMTrickMode(t *testing.T) {
for _, tc := range dsmTrickModeTestCases {
t.Run(tc.name, func(t *testing.T) {
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
tc.bytesFunc(w)
assert.Equal(t, parseDSMTrickMode(buf.Bytes()[0]), tc.trickMode)
})
}
}
func TestWriteDSMTrickMode(t *testing.T) {
for _, tc := range dsmTrickModeTestCases {
t.Run(tc.name, func(t *testing.T) {
bufExpected := &bytes.Buffer{}
wExpected := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: bufExpected})
tc.bytesFunc(wExpected)
bufActual := &bytes.Buffer{}
wActual := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: bufActual})
n, err := writeDSMTrickMode(wActual, tc.trickMode)
assert.NoError(t, err)
assert.Equal(t, 1, n)
assert.Equal(t, n, bufActual.Len())
assert.Equal(t, bufExpected.Bytes(), bufActual.Bytes())
})
}
}
var ptsClockReference = &ClockReference{Base: 5726623061}
func ptsBytes(flag string) []byte {
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
w.Write(flag) // Flag
w.Write("101") // 32...30
w.Write("1") // Dummy
w.Write("010101010101010") // 29...15
w.Write("1") // Dummy
w.Write("101010101010101") // 14...0
w.Write("1") // Dummy
return buf.Bytes()
}
var dtsClockReference = &ClockReference{Base: 5726623060}
func dtsBytes(flag string) []byte {
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
w.Write(flag) // Flag
w.Write("101") // 32...30
w.Write("1") // Dummy
w.Write("010101010101010") // 29...15
w.Write("1") // Dummy
w.Write("101010101010100") // 14...0
w.Write("1") // Dummy
return buf.Bytes()
}
func TestParsePTSOrDTS(t *testing.T) {
v, err := parsePTSOrDTS(astikit.NewBytesIterator(ptsBytes("0010")))
assert.Equal(t, v, ptsClockReference)
assert.NoError(t, err)
}
func TestWritePTSOrDTS(t *testing.T) {
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
n, err := writePTSOrDTS(w, uint8(0b0010), dtsClockReference)
assert.NoError(t, err)
assert.Equal(t, n, 5)
assert.Equal(t, n, buf.Len())
assert.Equal(t, dtsBytes("0010"), buf.Bytes())
}
func escrBytes() []byte {
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
w.Write("11") // Dummy
w.Write("011") // 32...30
w.Write("1") // Dummy
w.Write("000010111110000") // 29...15
w.Write("1") // Dummy
w.Write("000010111001111") // 14...0
w.Write("1") // Dummy
w.Write("000111010") // Ext
w.Write("1") // Dummy
return buf.Bytes()
}
func TestParseESCR(t *testing.T) {
v, err := parseESCR(astikit.NewBytesIterator(escrBytes()))
assert.Equal(t, v, clockReference)
assert.NoError(t, err)
}
func TestWriteESCR(t *testing.T) {
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
n, err := writeESCR(w, clockReference)
assert.NoError(t, err)
assert.Equal(t, n, 6)
assert.Equal(t, n, buf.Len())
assert.Equal(t, escrBytes(), buf.Bytes())
}
type pesTestCase struct {
name string
headerBytesFunc func(w *astikit.BitsWriter, withStuffing bool, withCRC bool)
optionalHeaderBytesFunc func(w *astikit.BitsWriter, withStuffing bool, withCRC bool)
bytesFunc func(w *astikit.BitsWriter, withStuffing bool, withCRC bool)
pesData *PESData
}
var pesTestCases = []pesTestCase{
{
"without_header",
func(w *astikit.BitsWriter, withStuffing bool, withCRC bool) {
w.Write("000000000000000000000001") // Prefix
w.Write(uint8(StreamIDPaddingStream)) // Stream ID
w.Write(uint16(4)) // Packet length
},
func(w *astikit.BitsWriter, withStuffing bool, withCRC bool) {
// do nothing here
},
func(w *astikit.BitsWriter, withStuffing bool, withCRC bool) {
w.Write([]byte("data")) // Data
},
&PESData{
Data: []byte("data"),
Header: &PESHeader{
PacketLength: 4,
StreamID: StreamIDPaddingStream,
},
},
},
{
"with_header",
func(w *astikit.BitsWriter, withStuffing bool, withCRC bool) {
packetLength := 67
stuffing := []byte("stuff")
if !withStuffing {
packetLength -= len(stuffing)
}
if !withCRC {
packetLength -= 2
}
w.Write("000000000000000000000001") // Prefix
w.Write(uint8(1)) // Stream ID
w.Write(uint16(packetLength)) // Packet length
},
func(w *astikit.BitsWriter, withStuffing bool, withCRC bool) {
optionalHeaderLength := 60
stuffing := []byte("stuff")
if !withStuffing {
optionalHeaderLength -= len(stuffing)
}
if !withCRC {
optionalHeaderLength -= 2
}
w.Write("10") // Marker bits
w.Write("01") // Scrambling control
w.Write("1") // Priority
w.Write("1") // Data alignment indicator
w.Write("1") // Copyright
w.Write("1") // Original or copy
w.Write("11") // PTS/DTS indicator
w.Write("1") // ESCR flag
w.Write("1") // ES rate flag
w.Write("1") // DSM trick mode flag
w.Write("1") // Additional copy flag
w.Write(withCRC) // CRC flag
w.Write("1") // Extension flag
w.Write(uint8(optionalHeaderLength)) // Header length
w.Write(ptsBytes("0011")) // PTS
w.Write(dtsBytes("0001")) // DTS
w.Write(escrBytes()) // ESCR
w.Write("101010101010101010101011") // ES rate
w.Write(dsmTrickModeSlowBytes()) // DSM trick mode
w.Write("11111111") // Additional copy info
if withCRC {
w.Write(uint16(4)) // CRC
}
// Extension starts here
w.Write("1") // Private data flag
w.Write("0") // Pack header field flag
w.Write("1") // Program packet sequence counter flag
w.Write("1") // PSTD buffer flag
w.Write("111") // Dummy
w.Write("1") // Extension 2 flag
w.Write([]byte("1234567890123456")) // Private data
//w.Write(uint8(5)) // Pack field
w.Write("1101010111010101") // Packet sequence counter
w.Write("0111010101010101") // PSTD buffer
w.Write("10001010") // Extension 2 header
w.Write([]byte("extension2")) // Extension 2 data
if withStuffing {
w.Write(stuffing) // Optional header stuffing bytes
}
},
func(w *astikit.BitsWriter, withStuffing bool, withCRC bool) {
stuffing := []byte("stuff")
w.Write([]byte("data")) // Data
if withStuffing {
w.Write(stuffing) // Stuffing
}
},
&PESData{
Data: []byte("data"),
Header: &PESHeader{
OptionalHeader: &PESOptionalHeader{
AdditionalCopyInfo: 127,
CRC: 4,
DataAlignmentIndicator: true,
DSMTrickMode: dsmTrickModeSlow,
DTS: dtsClockReference,
ESCR: clockReference,
ESRate: 1398101,
Extension2Data: []byte("extension2"),
Extension2Length: 10,
HasAdditionalCopyInfo: true,
HasCRC: true,
HasDSMTrickMode: true,
HasESCR: true,
HasESRate: true,
HasExtension: true,
HasExtension2: true,
HasPackHeaderField: false,
HasPrivateData: true,
HasProgramPacketSequenceCounter: true,
HasPSTDBuffer: true,
HeaderLength: 60,
IsCopyrighted: true,
IsOriginal: true,
MarkerBits: 2,
MPEG1OrMPEG2ID: 1,
OriginalStuffingLength: 21,
PacketSequenceCounter: 85,
//PackField: 5,
Priority: true,
PrivateData: []byte("1234567890123456"),
PSTDBufferScale: 1,
PSTDBufferSize: 5461,
PTSDTSIndicator: 3,
PTS: ptsClockReference,
ScramblingControl: 1,
},
PacketLength: 67,
StreamID: 1,
},
},
},
}
// used by TestParseData
func pesWithHeaderBytes() []byte {
buf := bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: &buf})
pesTestCases[1].headerBytesFunc(w, true, true)
pesTestCases[1].optionalHeaderBytesFunc(w, true, true)
pesTestCases[1].bytesFunc(w, true, true)
return buf.Bytes()
}
// used by TestParseData
func pesWithHeader() *PESData {
return pesTestCases[1].pesData
}
func TestParsePESData(t *testing.T) {
for _, tc := range pesTestCases {
t.Run(tc.name, func(t *testing.T) {
buf := bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: &buf})
tc.headerBytesFunc(w, true, true)
tc.optionalHeaderBytesFunc(w, true, true)
tc.bytesFunc(w, true, true)
d, err := parsePESData(astikit.NewBytesIterator(buf.Bytes()))
assert.NoError(t, err)
assert.Equal(t, tc.pesData, d)
})
}
}
func TestWritePESData(t *testing.T) {
for _, tc := range pesTestCases {
t.Run(tc.name, func(t *testing.T) {
bufExpected := bytes.Buffer{}
wExpected := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: &bufExpected})
tc.headerBytesFunc(wExpected, false, false)
tc.optionalHeaderBytesFunc(wExpected, false, false)
tc.bytesFunc(wExpected, false, false)
bufActual := bytes.Buffer{}
wActual := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: &bufActual})
start := true
totalBytes := 0
payloadPos := 0
for payloadPos+1 < len(tc.pesData.Data) {
n, payloadN, err := writePESData(
wActual,
tc.pesData.Header,
tc.pesData.Data[payloadPos:],
start,
MpegTsPacketSize-mpegTsPacketHeaderSize,
)
assert.NoError(t, err)
start = false
totalBytes += n
payloadPos += payloadN
}
assert.Equal(t, totalBytes, bufActual.Len())
assert.Equal(t, bufExpected.Len(), bufActual.Len())
assert.Equal(t, bufExpected.Bytes(), bufActual.Bytes())
})
}
}
func TestWritePESHeader(t *testing.T) {
for _, tc := range pesTestCases {
t.Run(tc.name, func(t *testing.T) {
bufExpected := bytes.Buffer{}
wExpected := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: &bufExpected})
tc.headerBytesFunc(wExpected, false, false)
tc.optionalHeaderBytesFunc(wExpected, false, false)
bufActual := bytes.Buffer{}
wActual := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: &bufActual})
n, err := writePESHeader(wActual, tc.pesData.Header, len(tc.pesData.Data))
assert.NoError(t, err)
assert.Equal(t, n, bufActual.Len())
assert.Equal(t, bufExpected.Len(), bufActual.Len())
assert.Equal(t, bufExpected.Bytes(), bufActual.Bytes())
})
}
}
func TestWritePESOptionalHeader(t *testing.T) {
for _, tc := range pesTestCases {
t.Run(tc.name, func(t *testing.T) {
bufExpected := bytes.Buffer{}
wExpected := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: &bufExpected})
tc.optionalHeaderBytesFunc(wExpected, false, false)
bufActual := bytes.Buffer{}
wActual := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: &bufActual})
n, err := writePESOptionalHeader(wActual, tc.pesData.Header.OptionalHeader)
assert.NoError(t, err)
assert.Equal(t, n, bufActual.Len())
assert.Equal(t, bufExpected.Len(), bufActual.Len())
assert.Equal(t, bufExpected.Bytes(), bufActual.Bytes())
})
}
}
func BenchmarkParsePESData(b *testing.B) {
bss := make([][]byte, len(pesTestCases))
for ti, tc := range pesTestCases {
buf := bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: &buf})
tc.headerBytesFunc(w, true, true)
tc.optionalHeaderBytesFunc(w, true, true)
tc.bytesFunc(w, true, true)
bss[ti] = buf.Bytes()
}
for ti, tc := range pesTestCases {
b.Run(tc.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
parsePESData(astikit.NewBytesIterator(bss[ti]))
}
})
}
}