forked from deepch/vdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.go
204 lines (174 loc) · 3.45 KB
/
atom.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
package fmp4io
import (
"fmt"
"io"
"os"
"strings"
"github.com/blackbeans/vdk/utils/bits/pio"
)
type Tag uint32
func (a Tag) String() string {
var b [4]byte
pio.PutU32BE(b[:], uint32(a))
for i := 0; i < 4; i++ {
if b[i] == 0 {
b[i] = ' '
}
}
return string(b[:])
}
type Atom interface {
Pos() (int, int)
Tag() Tag
Marshal([]byte) int
Unmarshal([]byte, int) (int, error)
Len() int
Children() []Atom
}
type AtomPos struct {
Offset int
Size int
}
func (a AtomPos) Pos() (int, int) {
return a.Offset, a.Size
}
func (a *AtomPos) setPos(offset int, size int) {
a.Offset, a.Size = offset, size
}
type Dummy struct {
Data []byte
Tag_ Tag
AtomPos
}
func (a Dummy) Children() []Atom {
return nil
}
func (a Dummy) Tag() Tag {
return a.Tag_
}
func (a Dummy) Len() int {
return len(a.Data)
}
func (a Dummy) Marshal(b []byte) int {
copy(b, a.Data)
return len(a.Data)
}
func (a *Dummy) Unmarshal(b []byte, offset int) (n int, err error) {
(&a.AtomPos).setPos(offset, len(b))
a.Data = b
n = len(b)
return
}
type FullAtom struct {
Version uint8
Flags uint32
AtomPos
}
func (f FullAtom) marshalAtom(b []byte, tag Tag) (n int) {
pio.PutU32BE(b[4:], uint32(tag))
pio.PutU8(b[8:], f.Version)
pio.PutU24BE(b[9:], f.Flags)
return 12
}
func (f FullAtom) atomLen() int {
return 12
}
func (f *FullAtom) unmarshalAtom(b []byte, offset int) (n int, err error) {
f.AtomPos.setPos(offset, len(b))
n = 8
if len(b) < n+4 {
return 0, parseErr("fullAtom", offset, nil)
}
f.Version = pio.U8(b[n:])
f.Flags = pio.U24BE(b[n+1:])
n += 4
return
}
func StringToTag(tag string) Tag {
var b [4]byte
copy(b[:], []byte(tag))
return Tag(pio.U32BE(b[:]))
}
func FindChildrenByName(root Atom, tag string) Atom {
return FindChildren(root, StringToTag(tag))
}
func FindChildren(root Atom, tag Tag) Atom {
if root.Tag() == tag {
return root
}
for _, child := range root.Children() {
if r := FindChildren(child, tag); r != nil {
return r
}
}
return nil
}
func ReadFileAtoms(r io.ReadSeeker) (atoms []Atom, err error) {
for {
offset, _ := r.Seek(0, 1)
taghdr := make([]byte, 8)
if _, err = io.ReadFull(r, taghdr); err != nil {
if err == io.EOF {
err = nil
}
return
}
size := pio.U32BE(taghdr[0:])
tag := Tag(pio.U32BE(taghdr[4:]))
var atom Atom
switch tag {
case FTYP:
atom = &FileType{}
case STYP:
atom = &SegmentType{}
case MOOV:
atom = &Movie{}
case MOOF:
atom = &MovieFrag{}
case SIDX:
atom = &SegmentIndex{}
}
if atom != nil {
b := make([]byte, int(size))
if _, err = io.ReadFull(r, b[8:]); err != nil {
return
}
copy(b, taghdr)
if _, err = atom.Unmarshal(b, int(offset)); err != nil {
return
}
atoms = append(atoms, atom)
} else {
dummy := &Dummy{Tag_: tag}
dummy.setPos(int(offset), int(size))
if _, err = r.Seek(int64(size)-8, 1); err != nil {
return
}
atoms = append(atoms, dummy)
}
}
}
func printatom(out io.Writer, root Atom, depth int) {
offset, size := root.Pos()
type stringintf interface {
String() string
}
fmt.Fprintf(out,
"%s%s offset=%d size=%d",
strings.Repeat(" ", depth*2), root.Tag(), offset, size,
)
if str, ok := root.(stringintf); ok {
fmt.Fprint(out, " ", str.String())
}
fmt.Fprintln(out)
children := root.Children()
for _, child := range children {
printatom(out, child, depth+1)
}
}
func FprintAtom(out io.Writer, root Atom) {
printatom(out, root, 0)
}
func PrintAtom(root Atom) {
FprintAtom(os.Stdout, root)
}