box.go (4482B)
1 package mp4 2 3 import ( 4 "errors" 5 "io" 6 "math" 7 8 "github.com/abema/go-mp4/bitio" 9 ) 10 11 const LengthUnlimited = math.MaxUint32 12 13 type ICustomFieldObject interface { 14 // GetFieldSize returns size of dynamic field 15 GetFieldSize(name string, ctx Context) uint 16 17 // GetFieldLength returns length of dynamic field 18 GetFieldLength(name string, ctx Context) uint 19 20 // IsOptFieldEnabled check whether if the optional field is enabled 21 IsOptFieldEnabled(name string, ctx Context) bool 22 23 // StringifyField returns field value as string 24 StringifyField(name string, indent string, depth int, ctx Context) (string, bool) 25 26 IsPString(name string, bytes []byte, remainingSize uint64, ctx Context) bool 27 28 BeforeUnmarshal(r io.ReadSeeker, size uint64, ctx Context) (n uint64, override bool, err error) 29 30 OnReadField(name string, r bitio.ReadSeeker, leftBits uint64, ctx Context) (rbits uint64, override bool, err error) 31 32 OnWriteField(name string, w bitio.Writer, ctx Context) (wbits uint64, override bool, err error) 33 } 34 35 type BaseCustomFieldObject struct { 36 } 37 38 // GetFieldSize returns size of dynamic field 39 func (box *BaseCustomFieldObject) GetFieldSize(string, Context) uint { 40 panic(errors.New("GetFieldSize not implemented")) 41 } 42 43 // GetFieldLength returns length of dynamic field 44 func (box *BaseCustomFieldObject) GetFieldLength(string, Context) uint { 45 panic(errors.New("GetFieldLength not implemented")) 46 } 47 48 // IsOptFieldEnabled check whether if the optional field is enabled 49 func (box *BaseCustomFieldObject) IsOptFieldEnabled(string, Context) bool { 50 return false 51 } 52 53 // StringifyField returns field value as string 54 func (box *BaseCustomFieldObject) StringifyField(string, string, int, Context) (string, bool) { 55 return "", false 56 } 57 58 func (*BaseCustomFieldObject) IsPString(name string, bytes []byte, remainingSize uint64, ctx Context) bool { 59 return true 60 } 61 62 func (*BaseCustomFieldObject) BeforeUnmarshal(io.ReadSeeker, uint64, Context) (uint64, bool, error) { 63 return 0, false, nil 64 } 65 66 func (*BaseCustomFieldObject) OnReadField(string, bitio.ReadSeeker, uint64, Context) (uint64, bool, error) { 67 return 0, false, nil 68 } 69 70 func (*BaseCustomFieldObject) OnWriteField(string, bitio.Writer, Context) (uint64, bool, error) { 71 return 0, false, nil 72 } 73 74 // IImmutableBox is common interface of box 75 type IImmutableBox interface { 76 ICustomFieldObject 77 78 // GetVersion returns the box version 79 GetVersion() uint8 80 81 // GetFlags returns the flags 82 GetFlags() uint32 83 84 // CheckFlag checks the flag status 85 CheckFlag(uint32) bool 86 87 // GetType returns the BoxType 88 GetType() BoxType 89 } 90 91 // IBox is common interface of box 92 type IBox interface { 93 IImmutableBox 94 95 // SetVersion sets the box version 96 SetVersion(uint8) 97 98 // SetFlags sets the flags 99 SetFlags(uint32) 100 101 // AddFlag adds the flag 102 AddFlag(uint32) 103 104 // RemoveFlag removes the flag 105 RemoveFlag(uint32) 106 } 107 108 type Box struct { 109 BaseCustomFieldObject 110 } 111 112 // GetVersion returns the box version 113 func (box *Box) GetVersion() uint8 { 114 return 0 115 } 116 117 // SetVersion sets the box version 118 func (box *Box) SetVersion(uint8) { 119 } 120 121 // GetFlags returns the flags 122 func (box *Box) GetFlags() uint32 { 123 return 0x000000 124 } 125 126 // CheckFlag checks the flag status 127 func (box *Box) CheckFlag(flag uint32) bool { 128 return true 129 } 130 131 // SetFlags sets the flags 132 func (box *Box) SetFlags(uint32) { 133 } 134 135 // AddFlag adds the flag 136 func (box *Box) AddFlag(flag uint32) { 137 } 138 139 // RemoveFlag removes the flag 140 func (box *Box) RemoveFlag(flag uint32) { 141 } 142 143 // FullBox is ISOBMFF FullBox 144 type FullBox struct { 145 BaseCustomFieldObject 146 Version uint8 `mp4:"0,size=8"` 147 Flags [3]byte `mp4:"1,size=8"` 148 } 149 150 // GetVersion returns the box version 151 func (box *FullBox) GetVersion() uint8 { 152 return box.Version 153 } 154 155 // SetVersion sets the box version 156 func (box *FullBox) SetVersion(version uint8) { 157 box.Version = version 158 } 159 160 // GetFlags returns the flags 161 func (box *FullBox) GetFlags() uint32 { 162 flag := uint32(box.Flags[0]) << 16 163 flag ^= uint32(box.Flags[1]) << 8 164 flag ^= uint32(box.Flags[2]) 165 return flag 166 } 167 168 // CheckFlag checks the flag status 169 func (box *FullBox) CheckFlag(flag uint32) bool { 170 return box.GetFlags()&flag != 0 171 } 172 173 // SetFlags sets the flags 174 func (box *FullBox) SetFlags(flags uint32) { 175 box.Flags[0] = byte(flags >> 16) 176 box.Flags[1] = byte(flags >> 8) 177 box.Flags[2] = byte(flags) 178 } 179 180 // AddFlag adds the flag 181 func (box *FullBox) AddFlag(flag uint32) { 182 box.SetFlags(box.GetFlags() | flag) 183 } 184 185 // RemoveFlag removes the flag 186 func (box *FullBox) RemoveFlag(flag uint32) { 187 box.SetFlags(box.GetFlags() & (^flag)) 188 }