-
Notifications
You must be signed in to change notification settings - Fork 3
/
bmff.go
366 lines (330 loc) · 10.4 KB
/
bmff.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package main
import (
"bytes"
"io"
)
// References:
// ISO/IEC 14496 Part 12
// ISO/IEC 14496 Part 15
// ISO/IEC 14496 Part 14 is not used.
func writeInt(w io.Writer, v int, n int) {
b := make([]byte, n)
for i := 0; i < n; i++ {
b[n-i-1] = byte(v & 0xff)
v >>= 8
}
w.Write(b)
}
func writeString(w io.Writer, s string) {
w.Write([]byte(s))
}
func writeTag(w io.Writer, tag string, cb func(w io.Writer)) {
var b bytes.Buffer
cb(&b) // callback
writeInt(w, b.Len()+8, 4) // box size
writeString(w, tag) // box type
w.Write(b.Bytes()) // box content
}
func writeFTYP(w io.Writer) {
writeTag(w, "ftyp", func(w io.Writer) {
writeString(w, "isom") // major brand
writeInt(w, 0x200, 4) // minor version
writeString(w, "isomiso2iso5avc1mp41") // compatible brands
})
}
func writeMOOV(w io.Writer, width, height uint16) {
writeTag(w, "moov", func(w io.Writer) {
writeMVHD(w)
writeTRAK(w, width, height)
writeMVEX(w)
})
}
func writeMVHD(w io.Writer) {
writeTag(w, "mvhd", func(w io.Writer) {
writeInt(w, 0, 4) // version and flags
writeInt(w, 0, 4) // creation time
writeInt(w, 0, 4) // modification time
writeInt(w, 1000, 4) // timescale
writeInt(w, 0, 4) // duration (all 1s == unknown)
writeInt(w, 0x00010000, 4) // rate (1.0 == normal)
writeInt(w, 0x0100, 2) // volume (1.0 == normal)
writeInt(w, 0, 2) // reserved
writeInt(w, 0, 4) // reserved
writeInt(w, 0, 4) // reserved
writeInt(w, 0x00010000, 4) // matrix
writeInt(w, 0x0, 4) // matrix
writeInt(w, 0x0, 4) // matrix
writeInt(w, 0x0, 4) // matrix
writeInt(w, 0x00010000, 4) // matrix
writeInt(w, 0x0, 4) // matrix
writeInt(w, 0x0, 4) // matrix
writeInt(w, 0x0, 4) // matrix
writeInt(w, 0x40000000, 4) // matrix
writeInt(w, 0, 4) // pre-defined
writeInt(w, 0, 4) // pre-defined
writeInt(w, 0, 4) // pre-defined
writeInt(w, 0, 4) // pre-defined
writeInt(w, 0, 4) // pre-defined
writeInt(w, 0, 4) // pre-defined
writeInt(w, -1, 4) // next track id
})
}
func writeTRAK(w io.Writer, width, height uint16) {
writeTag(w, "trak", func(w io.Writer) {
writeTKHD(w, width, height)
writeMDIA(w, width, height)
})
}
func writeTKHD(w io.Writer, width, height uint16) {
writeTag(w, "tkhd", func(w io.Writer) {
writeInt(w, 7, 4) // version and flags (track enabled)
writeInt(w, 0, 4) // creation time
writeInt(w, 0, 4) // modification time
writeInt(w, 1, 4) // track id
writeInt(w, 0, 4) // reserved
writeInt(w, 0, 4) // duration
writeInt(w, 0, 4) // reserved
writeInt(w, 0, 4) // reserved
writeInt(w, 0, 2) // layer
writeInt(w, 0, 2) // alternate group
writeInt(w, 0, 2) // volume (ignored for video tracks)
writeInt(w, 0, 2) // reserved
writeInt(w, 0x00010000, 4) // matrix
writeInt(w, 0x0, 4) // matrix
writeInt(w, 0x0, 4) // matrix
writeInt(w, 0x0, 4) // matrix
writeInt(w, 0x00010000, 4) // matrix
writeInt(w, 0x0, 4) // matrix
writeInt(w, 0x0, 4) // matrix
writeInt(w, 0x0, 4) // matrix
writeInt(w, 0x40000000, 4) // matrix
writeInt(w, int(width)<<16, 4) // width (fixed-point 16.16 format)
writeInt(w, int(height)<<16, 4) // height (fixed-point 16.16 format)
})
}
func writeMDIA(w io.Writer, width, height uint16) {
writeTag(w, "mdia", func(w io.Writer) {
writeMDHD(w)
writeHDLR(w)
writeMINF(w, width, height)
})
}
func writeMDHD(w io.Writer) {
writeTag(w, "mdhd", func(w io.Writer) {
writeInt(w, 0, 4) // version and flags
writeInt(w, 0, 4) // creation time
writeInt(w, 0, 4) // modification time
writeInt(w, 10000, 4) // timescale
writeInt(w, 0, 4) // duration
writeInt(w, 0x55c4, 2) // language ('und' == undefined)
writeInt(w, 0, 2) // pre-defined
})
}
func writeHDLR(w io.Writer) {
writeTag(w, "hdlr", func(w io.Writer) {
writeInt(w, 0, 4) // version and flags
writeInt(w, 0, 4) // pre-defined
writeString(w, "vide") // handler type
writeInt(w, 0, 4) // reserved
writeInt(w, 0, 4) // reserved
writeInt(w, 0, 4) // reserved
writeString(w, "MicroMSE Video Handler") // name
writeInt(w, 0, 1) // null-terminator
})
}
func writeMINF(w io.Writer, width, height uint16) {
writeTag(w, "minf", func(w io.Writer) {
writeVMHD(w)
writeDINF(w)
writeSTBL(w, width, height)
})
}
func writeVMHD(w io.Writer) {
writeTag(w, "vmhd", func(w io.Writer) {
writeInt(w, 1, 4) // version and flags
writeInt(w, 0, 2) // graphics mode
writeInt(w, 0, 2) // opcolor
writeInt(w, 0, 2) // opcolor
writeInt(w, 0, 2) // opcolor
})
}
func writeDINF(w io.Writer) {
writeTag(w, "dinf", func(w io.Writer) {
writeDREF(w)
})
}
func writeDREF(w io.Writer) {
writeTag(w, "dref", func(w io.Writer) {
writeInt(w, 0, 4) // version and flags
writeInt(w, 1, 4) // entry count
writeTag(w, "url ", func(w io.Writer) {
writeInt(w, 1, 4) // version and flags
})
})
}
func writeSTBL(w io.Writer, width, height uint16) {
writeTag(w, "stbl", func(w io.Writer) {
writeSTSD(w, width, height)
writeSTSZ(w)
writeSTSC(w)
writeSTTS(w)
writeSTCO(w)
})
}
// Sample Table Box
func writeSTSD(w io.Writer, width, height uint16) {
writeTag(w, "stsd", func(w io.Writer) {
writeInt(w, 0, 6) // reserved
writeInt(w, 1, 2) // deta reference index
writeTag(w, "avc1", func(w io.Writer) {
writeInt(w, 0, 6) // reserved
writeInt(w, 1, 2) // data reference index
writeInt(w, 0, 2) // pre-defined
writeInt(w, 0, 2) // reserved
writeInt(w, 0, 4) // pre-defined
writeInt(w, 0, 4) // pre-defined
writeInt(w, 0, 4) // pre-defined
writeInt(w, int(width), 2) // width
writeInt(w, int(height), 2) // height
writeInt(w, 0x00480000, 4) // horizontal resolution: 72 dpi
writeInt(w, 0x00480000, 4) // vertical resolution: 72 dpi
writeInt(w, 0, 4) // data size: 0
writeInt(w, 1, 2) // frame count: 1
w.Write(make([]byte, 32)) // compressor name
writeInt(w, 0x18, 2) // depth
writeInt(w, 0xffff, 2) // pre-defined
// Raspberry Pi 3B+ SPS/PPS for H.264 Main 4.0
sps := []byte{
0x27, 0x64, 0x00, 0x28, 0xac, 0x2b, 0x40, 0x28,
0x02, 0xdd, 0x00, 0xf1, 0x22, 0x6a,
}
pps := []byte{
0x28, 0xee, 0x02, 0x5c, 0xb0, 0x00,
}
// MPEG-4 Part 15 extension
// See ISO/IEC 14496-15:2004 5.3.4.1.2
writeTag(w, "avcC", func(w io.Writer) {
writeInt(w, 1, 1) // configuration version
writeInt(w, 0x64, 1) // H.264 profile (0x64 == high)
writeInt(w, 0x00, 1) // H.264 profile compatibility
writeInt(w, 0x28, 1) // H.264 level (0x28 == 4.0)
writeInt(w, 0xff, 1) // nal unit length - 1 (upper 6 bits == 1)
writeInt(w, 0xe1, 1) // number of sps (upper 3 bits == 1)
writeInt(w, len(sps), 2)
w.Write(sps)
writeInt(w, 1, 1) // number of pps
writeInt(w, len(pps), 2)
w.Write(pps)
})
})
})
}
func writeSTTS(w io.Writer) {
writeTag(w, "stts", func(w io.Writer) {
writeInt(w, 0, 4) // version and flags
writeInt(w, 0, 4) // entry count
})
}
func writeSTSC(w io.Writer) {
writeTag(w, "stsc", func(w io.Writer) {
writeInt(w, 0, 4) // version and flags
writeInt(w, 0, 4) // entry count
})
}
func writeSTSZ(w io.Writer) {
writeTag(w, "stsz", func(w io.Writer) {
writeInt(w, 0, 4) // version and flags
writeInt(w, 0, 4) // sample size
writeInt(w, 0, 4) // sample count
})
}
func writeSTCO(w io.Writer) {
writeTag(w, "stco", func(w io.Writer) {
writeInt(w, 0, 4) // version and flags
writeInt(w, 0, 4) // entry count
})
}
// Movie Extends Box
func writeMVEX(w io.Writer) {
writeTag(w, "mvex", func(w io.Writer) {
writeMEHD(w)
writeTREX(w)
})
}
// Movie Extends Header Box
func writeMEHD(w io.Writer) {
writeTag(w, "mehd", func(w io.Writer) {
writeInt(w, 0, 4) // version and flags
writeInt(w, 0, 4) // fragment duration
})
}
// Track Extends Box
func writeTREX(w io.Writer) {
writeTag(w, "trex", func(w io.Writer) {
writeInt(w, 0, 4) // version and flags
writeInt(w, 1, 4) // track id
writeInt(w, 1, 4) // default sample description index
writeInt(w, 0, 4) // default sample duration
writeInt(w, 0, 4) // default sample size
writeInt(w, 0x00010000, 4) // default sample flags
})
}
// Movie Fragment Box
func writeMOOF(w io.Writer, seq int, data []byte) {
writeTag(w, "moof", func(w io.Writer) {
writeMFHD(w, seq)
writeTRAF(w, seq, data)
})
}
// Movie Fragment Header Box
func writeMFHD(w io.Writer, seq int) {
writeTag(w, "mfhd", func(w io.Writer) {
writeInt(w, 0, 4) // version and flags
writeInt(w, seq, 4) // sequence number
})
}
// Track Fragment Box
func writeTRAF(w io.Writer, seq int, data []byte) {
writeTag(w, "traf", func(w io.Writer) {
writeTFHD(w)
writeTFDT(w, seq)
writeTRUN(w, data)
})
}
// Track Fragment Header Box
func writeTFHD(w io.Writer) {
writeTag(w, "tfhd", func(w io.Writer) {
writeInt(w, 0x020020, 4) // version and flags
writeInt(w, 1, 4) // track ID
writeInt(w, 0x01010000, 4) // default sample flags
})
}
// Track Fragment Base Media Decode Time Box
func writeTFDT(w io.Writer, seq int) {
writeTag(w, "tfdt", func(w io.Writer) {
writeInt(w, 0x01000000, 4) // version and flags
writeInt(w, 330*seq, 8) // base media decode time
})
}
// Track Run Box
func writeTRUN(w io.Writer, data []byte) {
writeTag(w, "trun", func(w io.Writer) {
writeInt(w, 0x00000305, 4) // version and flags
writeInt(w, 1, 4) // sample count
writeInt(w, 0x70, 4) // data offset
if (len(data) > 0) && (data[0]&0x1f == 0x5) {
writeInt(w, 0x02000000, 4) // first sample flags (i-frame)
} else {
writeInt(w, 0x01010000, 4) // first sample flags (not i-frame)
}
writeInt(w, 330, 4) // sample duration
writeInt(w, 4+len(data), 4) // sample size
})
}
// Media Data Box
func writeMDAT(w io.Writer, data []byte) {
writeTag(w, "mdat", func(w io.Writer) {
writeInt(w, len(data), 4)
w.Write(data)
})
}