forked from deepch/vdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filetype.go
103 lines (88 loc) · 2.03 KB
/
filetype.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
package fmp4io
import "github.com/galindocode/vdk/utils/bits/pio"
const FTYP = Tag(0x66747970)
type FileType struct {
MajorBrand uint32
MinorVersion uint32
CompatibleBrands []uint32
AtomPos
}
func (t FileType) Tag() Tag {
return FTYP
}
func (f FileType) Marshal(b []byte) (n int) {
l := 16 + 4*len(f.CompatibleBrands)
pio.PutU32BE(b, uint32(l))
pio.PutU32BE(b[4:], uint32(FTYP))
pio.PutU32BE(b[8:], f.MajorBrand)
pio.PutU32BE(b[12:], f.MinorVersion)
for i, v := range f.CompatibleBrands {
pio.PutU32BE(b[16+4*i:], v)
}
return l
}
func (f FileType) Len() int {
return 16 + 4*len(f.CompatibleBrands)
}
func (f *FileType) Unmarshal(b []byte, offset int) (n int, err error) {
f.AtomPos.setPos(offset, len(b))
n = 8
if len(b) < n+8 {
return 0, parseErr("MajorBrand", offset+n, nil)
}
f.MajorBrand = pio.U32BE(b[n:])
n += 4
f.MinorVersion = pio.U32BE(b[n:])
n += 4
for n < len(b)-3 {
f.CompatibleBrands = append(f.CompatibleBrands, pio.U32BE(b[n:]))
n += 4
}
return
}
func (f FileType) Children() []Atom {
return nil
}
const STYP = Tag(0x73747970)
type SegmentType struct {
MajorBrand uint32
MinorVersion uint32
CompatibleBrands []uint32
AtomPos
}
func (t SegmentType) Tag() Tag {
return STYP
}
func (f SegmentType) Marshal(b []byte) (n int) {
l := 16 + 4*len(f.CompatibleBrands)
pio.PutU32BE(b, uint32(l))
pio.PutU32BE(b[4:], uint32(STYP))
pio.PutU32BE(b[8:], f.MajorBrand)
pio.PutU32BE(b[12:], f.MinorVersion)
for i, v := range f.CompatibleBrands {
pio.PutU32BE(b[16+4*i:], v)
}
return l
}
func (f SegmentType) Len() int {
return 16 + 4*len(f.CompatibleBrands)
}
func (f *SegmentType) Unmarshal(b []byte, offset int) (n int, err error) {
f.AtomPos.setPos(offset, len(b))
n = 8
if len(b) < n+8 {
return 0, parseErr("MajorBrand", offset+n, nil)
}
f.MajorBrand = pio.U32BE(b[n:])
n += 4
f.MinorVersion = pio.U32BE(b[n:])
n += 4
for n < len(b)-3 {
f.CompatibleBrands = append(f.CompatibleBrands, pio.U32BE(b[n:]))
n += 4
}
return
}
func (f SegmentType) Children() []Atom {
return nil
}