-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample.go
57 lines (50 loc) · 1.48 KB
/
sample.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
package mp4
import "encoding/binary"
// Sample - sample as used in trun box (mdhd timescale)
type Sample struct {
Flags uint32 // interpreted as SampleFlags
Dur uint32 // Sample duration in mdhd timescale
Size uint32 // Size of sample data
Cto int32 // Signed composition time offset
}
// NewSample - create Sample with trun data
func NewSample(flags uint32, dur uint32, size uint32, cto int32) *Sample {
return &Sample{
Flags: flags,
Dur: dur,
Size: size,
Cto: cto,
}
}
// IsSync - check sync by masking flags including dependsOn
func (s *Sample) IsSync() bool {
decFlags := DecodeSampleFlags(s.Flags)
return !decFlags.SampleIsNonSync && (decFlags.SampleDependsOn == 2)
}
// FullSample - include accumulated time and data. Times in mdhd timescale
type FullSample struct {
Sample
DecodeTime uint64 // Absolute decode time (offset + accumulated sample Dur)
Data []byte // Sample data
}
// PresentationTime - DecodeTime displaced by composition time offset (possibly negative)
func (s *FullSample) PresentationTime() uint64 {
p := int64(s.DecodeTime) + int64(s.Cto)
if p < 0 {
p = 0 // Extraordinary case. Clip it to 0.
}
return uint64(p)
}
func toAnnexB(videoSample []byte) {
length := uint64(len(videoSample))
var pos uint64 = 0
for pos < length-4 {
lenSlice := videoSample[pos : pos+4]
nalLen := binary.BigEndian.Uint32(lenSlice)
videoSample[pos] = 0
videoSample[pos+1] = 0
videoSample[pos+2] = 0
videoSample[pos+3] = 1
pos += uint64(nalLen + 4)
}
}