forked from NectGmbH/metadata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp4.go
522 lines (459 loc) · 9.54 KB
/
mp4.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
package mp4
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
)
type File struct {
Box
Header *MVHD // movid header
}
// Parse parses an MP4 file from r.
// If r is a io.ReadSeeker then it is used
// to seek forward within r when necessary.
func Parse(r io.Reader) (*File, error) {
p := parser{
r: r,
f: &File{
Box: Box{Type: "MP4", Size: -1},
},
}
if err := p.Parse(); err != nil {
return nil, err
}
// parse moov box
f := p.f
moov := f.Box.Find("moov")
if moov == nil {
return nil, formatError("moov missing")
}
if err := moov.unpackChildren(); err != nil {
return nil, err
}
// decode moov.mvhd box
mvhd := moov.Find("mvhd")
if mvhd == nil {
return nil, formatError("mvhd missing")
}
h, err := DecodeMVHD(mvhd.Raw)
if err != nil {
return nil, err
}
f.Header = h
// calc file size
for _, b := range f.Child {
f.Size += b.Size
}
return f, nil
}
// AddUuid inserts the an uuid box into f.
// The first 16 bits of data is the 16-byte UUID.
func (f *File) AddUuid(data []byte) {
if len(data) < 16 {
panic("len(data) < 16")
}
newBox := Box{
Offset: -1,
Size: boxSize(len(data)),
Type: "uuid",
Raw: data,
}
existing := -1
for i, b := range f.Child {
if b.Type != "uuid" || len(b.Raw) < 16 {
continue
}
if bytes.Equal(data[:16], b.Raw[:16]) {
existing = i
break
}
}
if existing != -1 {
if f.replace(existing, newBox) {
// successfully replaced old box in file
return
}
// delete old box
f.Child[existing] = Box{
Offset: -1,
Size: f.Child[existing].Size,
Type: "free",
}
}
if room, _ := f.findFreeSpace(newBox.Size); room != -1 {
// place box in free space
ok := f.replace(room, newBox)
if !ok {
panic("impossible")
}
} else {
// no space, append box at end
f.Child = append(f.Child, newBox)
}
}
// FrameSize returns the frame size of f.
func (f *File) FrameSize() (width, height int, err error) {
moov := f.Find("moov")
if moov == nil {
return 0, 0, formatError("mp4 without moov")
}
var firstErr error
var best int64
for _, b := range moov.Child {
if b.Type != "trak" {
continue
}
p := b.Find("tkhd")
if p == nil {
continue
}
hd, err := DecodeTKHD(p.Raw)
if err != nil {
if firstErr == nil {
err = formatError("decode TKHD error %v", err)
}
}
dx, dy := hd.FrameSize()
s := int64(dx) * int64(dy)
if s > best {
best, width, height = s, dx, dy
}
}
if (width == 0 || height == 0) && firstErr != nil {
return 0, 0, firstErr
}
return width, height, nil
}
func (f *File) replace(idx int, newBox Box) bool {
oldBox := f.Child[idx]
// find free boxes after oldBox until there is enough room
space := oldBox.Size
next := idx + 1
for next < len(f.Child) &&
newBox.Size != space &&
!boxSizePossible(newBox.Size, space) {
n := f.Child[next]
if n.Type != "free" {
break
}
space += n.Size
next++
}
if !boxSizePossible(newBox.Size, space) {
// no space
return false
}
f.Child[idx] = newBox
idx++
// remove consumed free boxes
ndrop := next - idx
if newBox.Size < space {
// want free box after idx
ndrop--
}
switch {
case ndrop < 0:
// ndrop == -1: add one
f.Child = append(f.Child, Box{})
copy(f.Child[idx+1:], f.Child[next:])
case ndrop > 0:
copy(f.Child[idx:], f.Child[idx+ndrop:])
f.Child = f.Child[:len(f.Child)-next+idx]
}
if newBox.Size == space {
return true
}
f.Child[idx] = Box{
Offset: -1,
Size: space - newBox.Size,
Type: "free",
}
return true
}
func (f *File) findFreeSpace(boxSize int64) (idx, nbox int) {
var space int64
for i, b := range f.Child {
if b.Type == "free" {
space += b.Size
if boxSizePossible(boxSize, space) {
return idx, i - idx + 1
}
} else {
space, idx = 0, i+1
}
}
return -1, 0
}
type Box struct {
Offset, Size int64 // size and offset of the box in the original file
Ext bool // false: 8-byte header, true: 16-byte header
Type string // 4-byte cc4
Raw []byte // raw content, if loaded
Child []Box // child boxes
}
func (b *Box) HeaderSize() int64 {
if b.Ext {
return 16
} else {
return 8
}
}
// ContentSize returns the payload length, or -1 if the
// box goes to EOF (or the end of its parent)
func (b *Box) ContentSize() int64 {
if b.Size == 0 {
return -1
}
return b.Size - b.HeaderSize()
}
func (b *Box) Find(typ ...string) *Box {
if len(typ) == 0 {
return b
}
for i := range b.Child {
child := &b.Child[i]
if child.Type == typ[0] {
return child.Find(typ[1:]...)
}
}
return nil
}
var parentBoxes = setOf("moov", "trak", "mdia", "minf", "stbl")
func (b *Box) unpackChildren() error {
if _, ok := parentBoxes[b.Type]; !ok {
return nil
}
for off := 0; off < len(b.Raw); {
if len(b.Raw[off:]) < 8 {
return formatError("%s unpack", b.Type)
}
c := Box{
Offset: b.Offset + int64(off),
Size: int64(binary.BigEndian.Uint32(b.Raw[off:])),
Type: string(b.Raw[off+4 : off+8]),
}
if c.Size == 1 {
c.Ext = true
off += 8
if len(b.Raw[off:]) < 8 {
return formatError("%s/%s unpack EOF", b.Type, c.Type)
}
// 64-bit box header
c.Size = int64(binary.BigEndian.Uint64(b.Raw[off:]))
}
if c.Size == 0 {
c.Size = int64(len(b.Raw)-off) + b.HeaderSize()
}
if c.Size < c.HeaderSize() {
return fmt.Errorf("box size %d < %d invalid", c.Size, c.HeaderSize())
}
off += 8
datalen := c.ContentSize()
if int64(len(b.Raw)) < int64(off)+datalen {
return formatError("%s/%s unpack EOF", b.Type, c.Type, c.Size)
}
c.Raw = b.Raw[off : off+int(datalen)]
b.Child = append(b.Child, c)
off += int(datalen)
}
for i := range b.Child {
c := &b.Child[i]
if err := c.unpackChildren(); err != nil {
return err
}
}
return nil
}
func (b *Box) packChildren() {
b.Size = b.packedSize()
p := make([]byte, int(b.Size))
off := packBox(b, p, 0)
if int64(off) != b.Size {
panic("consistency")
}
b.Raw = p
}
func (b *Box) packedSize() int64 {
if b.Child == nil {
return boxSize(len(b.Raw))
}
var n int64
for _, c := range b.Child {
n += c.packedSize()
}
return boxSize(int(n))
}
func packBox(b *Box, p []byte, off int) (noff int) {
// write cc4
copy(p[off+4:off+8], b.Type)
// write size
size := b.packedSize()
if size < 1<<32 {
binary.BigEndian.PutUint32(p[off:], 1)
binary.BigEndian.PutUint64(p[off+8:], uint64(size))
off += 16
} else {
binary.BigEndian.PutUint32(p[off:], uint32(size))
off += 8
}
// write raw content if no children
if b.Child == nil {
copy(p[off:], b.Raw)
return off + len(b.Raw)
}
// write children
for i := range b.Child {
off = packBox(&b.Child[i], p, off)
}
return off
}
func setOf(v ...string) map[string]struct{} {
m := make(map[string]struct{})
for _, k := range v {
m[k] = struct{}{}
}
return m
}
type parser struct {
r io.Reader
f *File
off int64 // offset in r
tmp []byte // scratch buffer
}
const maxParseSize = 1 << 20
func (p *parser) Parse() error {
for {
b, err := p.readAtomHeader()
if err != nil {
if err == io.EOF {
return nil
}
return err
}
if p.f.Child == nil {
if b.Type != "ftyp" {
return formatError("ftyp missing")
}
if b.Size > 1<<10 {
return formatError("ftyp too long")
}
}
if b.Size == 0 {
return p.finish(b)
}
contentSize := b.ContentSize()
if wantBox(b.Type) {
if contentSize > maxParseSize {
return formatError("%s too long", b.Type)
}
b.Raw = make([]byte, int(contentSize))
if _, err := io.ReadFull(p.r, b.Raw); err != nil {
return err
}
p.off += int64(len(b.Raw))
} else {
if err := p.skip(contentSize); err != nil {
return err
}
}
p.f.Child = append(p.f.Child, b)
}
return nil
}
func (p *parser) finish(b Box) error {
if !wantBox(b.Type) {
// unneeded box goes till EOF
p.f.Child = append(p.f.Child, b)
return nil
}
var err error
b.Raw, err = ioutil.ReadAll(io.LimitReader(p.r, maxParseSize+1))
if len(b.Raw) > maxParseSize {
return formatError("%s too long", b.Type)
}
if err != nil {
return err
}
b.Size = int64(len(b.Raw)) + b.HeaderSize()
p.f.Child = append(p.f.Child, b)
return nil
}
func (p *parser) skip(n int64) error {
var err error
if s, ok := p.r.(io.Seeker); ok {
_, err = s.Seek(n, 1)
} else {
if p.tmp == nil {
p.tmp = make([]byte, 32*1024)
}
_, err = io.CopyBuffer(ioutil.Discard, io.LimitReader(p.r, n), p.tmp)
}
if err == nil {
p.off += n
}
return err
}
func wantBox(cc4 string) bool {
switch cc4 {
case "ftyp":
return true
case "moov":
return true
case "trak":
return true
case "tkhd":
return true
case "uuid":
return true
}
return false
}
// read next atom header
func (p *parser) readAtomHeader() (b Box, err error) {
x := make([]byte, 8)
var n int
b.Offset = p.off
n, err = io.ReadFull(p.r, x)
p.off += int64(n)
if err != nil {
return Box{}, err
}
b.Size = int64(binary.BigEndian.Uint32(x))
b.Type = string(x[4:])
if b.Size == 1 {
b.Ext = true
// 64-bit box header
n, err = io.ReadFull(p.r, x)
p.off += int64(n)
if err != nil {
return Box{}, err
}
b.Size = int64(binary.BigEndian.Uint64(x))
}
if b.Size != 0 && b.Size < b.HeaderSize() {
return Box{}, fmt.Errorf("box size %d < %d invalid", b.Size, b.HeaderSize())
}
return b, nil
}
// headerSize returns the header size needed for contentlen.
func headerSize(contentlen int) int64 {
if contentlen+8 >= 1<<32 {
return 16
} else {
return 8
}
}
func boxSize(contentlen int) int64 {
return headerSize(contentlen) + int64(contentlen)
}
func boxSizePossible(size, space int64) bool {
if size == space {
return true
}
return space-size >= 8
}
func formatError(f string, args ...interface{}) error {
return fmt.Errorf(f, args...)
}