-
Notifications
You must be signed in to change notification settings - Fork 31
/
box.go
188 lines (142 loc) · 4.39 KB
/
box.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
package mp4
import (
"errors"
"io"
"math"
"github.com/abema/go-mp4/internal/bitio"
)
const LengthUnlimited = math.MaxUint32
type ICustomFieldObject interface {
// GetFieldSize returns size of dynamic field
GetFieldSize(name string, ctx Context) uint
// GetFieldLength returns length of dynamic field
GetFieldLength(name string, ctx Context) uint
// IsOptFieldEnabled check whether if the optional field is enabled
IsOptFieldEnabled(name string, ctx Context) bool
// StringifyField returns field value as string
StringifyField(name string, indent string, depth int, ctx Context) (string, bool)
IsPString(name string, bytes []byte, remainingSize uint64, ctx Context) bool
BeforeUnmarshal(r io.ReadSeeker, size uint64, ctx Context) (n uint64, override bool, err error)
OnReadField(name string, r bitio.ReadSeeker, leftBits uint64, ctx Context) (rbits uint64, override bool, err error)
OnWriteField(name string, w bitio.Writer, ctx Context) (wbits uint64, override bool, err error)
}
type BaseCustomFieldObject struct {
}
// GetFieldSize returns size of dynamic field
func (box *BaseCustomFieldObject) GetFieldSize(string, Context) uint {
panic(errors.New("GetFieldSize not implemented"))
}
// GetFieldLength returns length of dynamic field
func (box *BaseCustomFieldObject) GetFieldLength(string, Context) uint {
panic(errors.New("GetFieldLength not implemented"))
}
// IsOptFieldEnabled check whether if the optional field is enabled
func (box *BaseCustomFieldObject) IsOptFieldEnabled(string, Context) bool {
return false
}
// StringifyField returns field value as string
func (box *BaseCustomFieldObject) StringifyField(string, string, int, Context) (string, bool) {
return "", false
}
func (*BaseCustomFieldObject) IsPString(name string, bytes []byte, remainingSize uint64, ctx Context) bool {
return true
}
func (*BaseCustomFieldObject) BeforeUnmarshal(io.ReadSeeker, uint64, Context) (uint64, bool, error) {
return 0, false, nil
}
func (*BaseCustomFieldObject) OnReadField(string, bitio.ReadSeeker, uint64, Context) (uint64, bool, error) {
return 0, false, nil
}
func (*BaseCustomFieldObject) OnWriteField(string, bitio.Writer, Context) (uint64, bool, error) {
return 0, false, nil
}
// IImmutableBox is common interface of box
type IImmutableBox interface {
ICustomFieldObject
// GetVersion returns the box version
GetVersion() uint8
// GetFlags returns the flags
GetFlags() uint32
// CheckFlag checks the flag status
CheckFlag(uint32) bool
// GetType returns the BoxType
GetType() BoxType
}
// IBox is common interface of box
type IBox interface {
IImmutableBox
// SetVersion sets the box version
SetVersion(uint8)
// SetFlags sets the flags
SetFlags(uint32)
// AddFlag adds the flag
AddFlag(uint32)
// RemoveFlag removes the flag
RemoveFlag(uint32)
}
type Box struct {
BaseCustomFieldObject
}
// GetVersion returns the box version
func (box *Box) GetVersion() uint8 {
return 0
}
// SetVersion sets the box version
func (box *Box) SetVersion(uint8) {
}
// GetFlags returns the flags
func (box *Box) GetFlags() uint32 {
return 0x000000
}
// CheckFlag checks the flag status
func (box *Box) CheckFlag(flag uint32) bool {
return true
}
// SetFlags sets the flags
func (box *Box) SetFlags(uint32) {
}
// AddFlag adds the flag
func (box *Box) AddFlag(flag uint32) {
}
// RemoveFlag removes the flag
func (box *Box) RemoveFlag(flag uint32) {
}
// FullBox is ISOBMFF FullBox
type FullBox struct {
BaseCustomFieldObject
Version uint8 `mp4:"0,size=8"`
Flags [3]byte `mp4:"1,size=8"`
}
// GetVersion returns the box version
func (box *FullBox) GetVersion() uint8 {
return box.Version
}
// SetVersion sets the box version
func (box *FullBox) SetVersion(version uint8) {
box.Version = version
}
// GetFlags returns the flags
func (box *FullBox) GetFlags() uint32 {
flag := uint32(box.Flags[0]) << 16
flag ^= uint32(box.Flags[1]) << 8
flag ^= uint32(box.Flags[2])
return flag
}
// CheckFlag checks the flag status
func (box *FullBox) CheckFlag(flag uint32) bool {
return box.GetFlags()&flag != 0
}
// SetFlags sets the flags
func (box *FullBox) SetFlags(flags uint32) {
box.Flags[0] = byte(flags >> 16)
box.Flags[1] = byte(flags >> 8)
box.Flags[2] = byte(flags)
}
// AddFlag adds the flag
func (box *FullBox) AddFlag(flag uint32) {
box.SetFlags(box.GetFlags() | flag)
}
// RemoveFlag removes the flag
func (box *FullBox) RemoveFlag(flag uint32) {
box.SetFlags(box.GetFlags() & (^flag))
}