-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmovie.go
More file actions
199 lines (179 loc) · 4.65 KB
/
movie.go
File metadata and controls
199 lines (179 loc) · 4.65 KB
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
// movie.go
package sofia
import (
"bytes"
"errors"
)
// --- MOOV ---
type MoovBox struct {
Header *BoxHeader
Mvhd *MvhdBox
Trak []*TrakBox
Pssh []*PsshBox
RawChildren [][]byte
}
func DecodeMoovBox(data []byte) (*MoovBox, error) {
b := &MoovBox{}
var err error
b.Header, err = DecodeBoxHeader(data)
if err != nil {
return nil, err
}
payload := data[8:b.Header.Size]
offset := 0
for offset < len(payload) {
header, err := DecodeBoxHeader(payload[offset:])
if err != nil {
break
}
boxSize := int(header.Size)
if boxSize == 0 {
boxSize = len(payload) - offset
}
if boxSize < 8 || offset+boxSize > len(payload) {
return nil, errors.New("invalid child box size")
}
content := payload[offset : offset+boxSize]
switch string(header.Type[:]) {
case "mvhd":
mvhd, err := DecodeMvhdBox(content)
if err != nil {
return nil, err
}
b.Mvhd = mvhd
case "trak":
trak, err := DecodeTrakBox(content)
if err != nil {
return nil, err
}
b.Trak = append(b.Trak, trak)
case "pssh":
pssh, err := DecodePsshBox(content)
if err != nil {
return nil, err
}
b.Pssh = append(b.Pssh, pssh)
default:
b.RawChildren = append(b.RawChildren, content)
}
offset += boxSize
}
return b, nil
}
func (b *MoovBox) Encode() []byte {
buffer := make([]byte, 8)
if b.Mvhd != nil {
buffer = append(buffer, b.Mvhd.Encode()...)
}
for _, trak := range b.Trak {
buffer = append(buffer, trak.Encode()...)
}
// pssh is skipped on encode
for _, raw := range b.RawChildren {
buffer = append(buffer, raw...)
}
b.Header.Size = uint32(len(buffer))
b.Header.Put(buffer)
return buffer
}
func (b *MoovBox) RemovePssh() {
b.Pssh = nil
}
func (b *MoovBox) RemoveMvex() {
var kept [][]byte
for _, child := range b.RawChildren {
if len(child) >= 8 && string(child[4:8]) == "mvex" {
continue
}
kept = append(kept, child)
}
b.RawChildren = kept
}
func (b *MoovBox) FindPssh(systemID []byte) (*PsshBox, bool) {
for _, pssh := range b.Pssh {
if bytes.Equal(pssh.SystemID[:], systemID) {
return pssh, true
}
}
return nil, false
}
// --- MVHD ---
type MvhdBox struct {
Header *BoxHeader
Version byte
Flags [3]byte
CreationTime uint64
ModificationTime uint64
Timescale uint32
Duration uint64
RemainingData []byte
}
func DecodeMvhdBox(data []byte) (*MvhdBox, error) {
b := &MvhdBox{}
var err error
b.Header, err = DecodeBoxHeader(data)
if err != nil {
return nil, err
}
if len(data) < 12 {
return nil, errors.New("mvhd box too small")
}
p := parser{data: data, offset: 8}
versionAndFlags := p.Bytes(4)
b.Version = versionAndFlags[0]
copy(b.Flags[:], versionAndFlags[1:])
if b.Version == 1 {
if len(data) < 36 { // 8 header + 4 version/flags + 24 v1 body
return nil, errors.New("mvhd v1 too short")
}
b.CreationTime = p.Uint64()
b.ModificationTime = p.Uint64()
b.Timescale = p.Uint32()
b.Duration = p.Uint64()
} else { // Version 0
if len(data) < 24 { // 8 header + 4 version/flags + 12 v0 body
return nil, errors.New("mvhd v0 too short")
}
b.CreationTime = uint64(p.Uint32())
b.ModificationTime = uint64(p.Uint32())
b.Timescale = p.Uint32()
b.Duration = uint64(p.Uint32())
}
b.RemainingData = data[p.offset:b.Header.Size]
return b, nil
}
func (b *MvhdBox) SetDuration(duration uint64) {
b.Duration = duration
if b.Duration > 0xFFFFFFFF {
b.Version = 1
}
}
func (b *MvhdBox) Encode() []byte {
var bodySize int
if b.Version == 1 {
bodySize = 32 // 8+8+4+8 + 4 for ver/flags
} else {
bodySize = 20 // 4+4+4+4 + 4 for ver/flags
}
totalSize := uint32(8 + bodySize + len(b.RemainingData))
buffer := make([]byte, totalSize)
w := writer{buf: buffer}
w.PutUint32(totalSize)
w.PutBytes(b.Header.Type[:])
w.PutByte(b.Version)
w.PutBytes(b.Flags[:])
if b.Version == 1 {
w.PutUint64(b.CreationTime)
w.PutUint64(b.ModificationTime)
w.PutUint32(b.Timescale)
w.PutUint64(b.Duration)
} else {
w.PutUint32(uint32(b.CreationTime))
w.PutUint32(uint32(b.ModificationTime))
w.PutUint32(b.Timescale)
w.PutUint32(uint32(b.Duration))
}
w.PutBytes(b.RemainingData)
b.Header.Size = totalSize
return buffer
}