-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoptimize.go
221 lines (199 loc) · 4.25 KB
/
optimize.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package mp4
import (
"encoding/binary"
"sort"
)
func (f *File) Optimize() error {
sort.Stable(topLevelBoxSort(f.Child))
moov := f.Box.Find("moov")
if moov == nil {
// no offsets to adjust
return nil
}
// collect old mdat offsets
var oo []int64
for _, b := range f.Child {
if b.Type == "mdat" {
oo = append(oo, b.Offset)
}
}
// calc size excluding moov
var lenxmoov int64
for _, b := range f.Child {
if b.Type != "moov" {
lenxmoov += b.Size
}
}
// calc final moov size
baselen, noffs := analyseMoov(moov)
use64bit := false
if s32 := lenxmoov + baselen + 4*int64(noffs); s32 >= 1<<32 {
use64bit = true
moov.Size = lenxmoov + baselen + 8*int64(noffs)
} else {
moov.Size = s32
}
// calc new mdat offsets
var no []int64
off := int64(0)
for _, b := range f.Child {
if b.Type == "mdat" {
no = append(no, off)
}
off += b.Size
}
shiftMoovOffsets(moov, oo, no, use64bit)
ms := moov.Size
moov.packChildren()
if moov.Size != ms {
panic("consistency")
}
return nil
}
type topLevelBoxSort []Box
func (s topLevelBoxSort) Len() int { return len(s) }
func (s topLevelBoxSort) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s topLevelBoxSort) Less(i, j int) bool {
return boxIdx(s[i].Type) < boxIdx(s[j].Type)
}
func boxIdx(cc4 string) int {
switch cc4 {
case "ftyp":
return 0
case "moov":
return 1
case "uuid":
return 2
default:
return 3
case "mdat":
return 4
}
}
func analyseMoov(moov *Box) (baselen int64, noffsets int) {
for _, c := range moov.Child {
baselen += c.Size
if c.Type == "trak" {
stbl := c.Find("mdia", "minf", "stbl")
if stbl == nil {
continue
}
stco, co64 := stbl.Find("stco"), stbl.Find("co64")
if n, _ := checkOffsetBlock(stco); n != 0 {
noffsets += n
if co64 != nil {
// has both co64
baselen -= co64.Size
}
} else {
if n, _ := checkOffsetBlock(co64); n != 0 {
noffsets += n
}
}
}
}
return baselen, noffsets
}
func shiftMoovOffsets(moov *Box, oo, no []int64, use64bit bool) {
if len(oo) == 0 {
return
}
var offsets []int64
for _, c := range moov.Child {
if c.Type == "trak" {
stbl := c.Find("mdia", "minf", "stbl")
if stbl == nil {
continue
}
// read old offsets
stco, co64 := stbl.Find("stco"), stbl.Find("co64")
n, xf := checkOffsetBlock(stco)
if n == 0 {
n, xf = checkOffsetBlock(co64)
}
if n == 0 {
continue
}
offsets = offsets[:0]
for i := 0; i < n; i++ {
offsets = append(offsets, xf(i))
}
// shift offsets
for i, off := range offsets {
idx := sort.Search(len(oo), func(i int) bool {
return oo[i] > off
})
if idx == 0 {
// offset before first mdat
continue
}
idx--
offsets[i] = off - oo[idx] + no[idx]
}
// create box with new offsets
var cc4 string
var p []byte
if use64bit {
cc4 = "co64"
p = make([]byte, 8+8*len(offsets))
binary.BigEndian.PutUint32(p[4:], uint32(len(offsets)))
for i, off := range offsets {
binary.BigEndian.PutUint64(p[8+i*8:], uint64(off))
}
} else {
cc4 = "stco"
p = make([]byte, 4+4*len(offsets))
binary.BigEndian.PutUint32(p, uint32(len(offsets)))
for i, off := range offsets {
binary.BigEndian.PutUint32(p[4+i*4:], uint32(off))
}
}
newBox := Box{
Offset: -1,
Size: boxSize(len(p)),
Type: cc4,
Raw: p,
}
// delete old stco and co64 boxes, add new box
xc := stbl.Child
stbl.Child = make([]Box, 0, len(xc))
for _, c := range xc {
if c.Type == "stco" || c.Type == "co64" {
continue
}
stbl.Child = append(stbl.Child, c)
}
stbl.Child = append(stbl.Child, newBox)
}
}
}
func checkOffsetBlock(b *Box) (noffsets int, extract func(i int) int64) {
if b == nil {
return 0, nil
}
switch b.Type {
case "stco":
if len(b.Raw) < 4 {
return 0, nil
}
n := int(binary.BigEndian.Uint32(b.Raw))
if 4+n*4 <= len(b.Raw) {
return 0, nil
}
return n, func(i int) int64 {
return int64(binary.BigEndian.Uint32(b.Raw[4+i*4:]))
}
case "co64":
if len(b.Raw) < 8 {
return 0, nil
}
n := int(binary.BigEndian.Uint32(b.Raw[4:]))
if 8+n*8 <= len(b.Raw) {
return 0, nil
}
return n, func(i int) int64 {
return int64(binary.BigEndian.Uint64(b.Raw[8+i*8:]))
}
}
return 0, nil
}