-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsofia.go
185 lines (161 loc) · 3.97 KB
/
sofia.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
package sofia
import (
"encoding/binary"
"encoding/hex"
"strconv"
)
// ISO/IEC 14496-12
//
// aligned(8) class BoxHeader(
// unsigned int(32) boxtype,
// optional unsigned int(8)[16] extended_type
// ) {
// unsigned int(32) size;
// unsigned int(32) type = boxtype;
// if (size==1) {
// unsigned int(64) largesize;
// } else if (size==0) {
// // box extends to end of file
// }
// if (boxtype=='uuid') {
// unsigned int(8)[16] usertype = extended_type;
// }
// }
type BoxHeader struct {
Size uint32
Type Type
UserType *Uuid
}
func (b *BoxHeader) Decode(data []byte) (int, error) {
n, err := binary.Decode(data, binary.BigEndian, &b.Size)
if err != nil {
return 0, err
}
n += copy(b.Type[:], data[n:])
if b.Type.String() == "uuid" {
b.UserType = &Uuid{}
n += copy(b.UserType[:], data[n:])
}
return n, nil
}
func (b *Box) Read(data []byte) error {
n, err := b.BoxHeader.Decode(data)
if err != nil {
return err
}
b.Payload = data[n:b.BoxHeader.Size]
return nil
}
func (u Uuid) String() string {
return hex.EncodeToString(u[:])
}
type Uuid [16]uint8
type Appender interface {
Append([]byte) ([]byte, error)
}
func (b *Box) Append(data []byte) ([]byte, error) {
data, err := b.BoxHeader.Append(data)
if err != nil {
return nil, err
}
return append(data, b.Payload...), nil
}
func (b *BoxHeader) GetSize() int {
size := binary.Size(b.Size)
size += binary.Size(b.Type)
if b.UserType != nil {
size += binary.Size(b.UserType)
}
return size
}
func (b *BoxHeader) Append(data []byte) ([]byte, error) {
data = binary.BigEndian.AppendUint32(data, b.Size)
data = append(data, b.Type[:]...)
if b.UserType != nil {
data = append(data, (*b.UserType)[:]...)
}
return data, nil
}
type Decoder interface {
Decode([]byte) (int, error)
}
type Reader interface {
Read([]byte) error
}
type SizeGetter interface {
GetSize() int
}
func (t Type) String() string {
return string(t[:])
}
type Type [4]uint8
type BoxError struct {
Container BoxHeader
Box BoxHeader
}
func (b *BoxError) Error() string {
data := []byte("container:")
data = strconv.AppendQuote(data, b.Container.Type.String())
data = append(data, " box type:"...)
data = strconv.AppendQuote(data, b.Box.Type.String())
return string(data)
}
// ISO/IEC 14496-12
//
// aligned(8) class Box(
// unsigned int(32) boxtype,
// optional unsigned int(8)[16] extended_type
// ) {
// BoxHeader(boxtype, extended_type);
// // the remaining bytes are the BoxPayload
// }
type Box struct {
BoxHeader BoxHeader
Payload []byte
}
const PiffExtendedType = "a2394f525a9b4f14a2446c427c648df4"
func (f *FullBoxHeader) GetFlags() uint32 {
var flag [4]byte
copy(flag[1:], f.Flags[:])
return binary.BigEndian.Uint32(flag[:])
}
// ISO/IEC 14496-12
//
// aligned(8) class FullBoxHeader(unsigned int(8) v, bit(24) f) {
// unsigned int(8) version = v;
// bit(24) flags = f;
// }
type FullBoxHeader struct {
Version uint8
Flags [3]byte
}
func (s *SampleEntry) Decode(data []byte) (int, error) {
n := 6 // reserved
data = data[n:]
n1, err := binary.Decode(data, binary.BigEndian, &s.DataReferenceIndex)
if err != nil {
return 0, err
}
return n + n1, nil
}
// ISO/IEC 14496-12
//
// aligned(8) abstract class SampleEntry(
// unsigned int(32) format
// ) extends Box(format) {
// const unsigned int(8)[6] reserved = 0;
// unsigned int(16) data_reference_index;
// }
type SampleEntry struct {
BoxHeader BoxHeader
_ [6]uint8
DataReferenceIndex uint16
}
func (s *SampleEntry) Append(data []byte) ([]byte, error) {
data, err := s.BoxHeader.Append(data)
if err != nil {
return nil, err
}
data = append(data, 0, 0, 0, 0, 0, 0) // reserved
return binary.BigEndian.AppendUint16(data, s.DataReferenceIndex), nil
}