/
file.go
429 lines (391 loc) · 10.5 KB
/
file.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
package mp4
import (
"fmt"
"io"
"os"
"strings"
)
// File - an MPEG-4 file asset
//
// A progressive MPEG-4 file contains three main boxes:
//
// ftyp : the file type box
// moov : the movie box (meta-data)
// mdat : the media data (chunks and samples). Only used for pror
//
// where mdat may come before moov.
// If fragmented, there are many more boxes and they are collected
// in the InitSegment, Segment and Segments structures.
// The sample metadata in thefragments in the Segments will be
// optimized unless EncodeVerbatim is set.
// To Encode the same data as Decoded, this flag must therefore be set.
// In all cases, Children contain all top-level boxes
type File struct {
Ftyp *FtypBox
Moov *MoovBox
Mdat *MdatBox // Only used for non-fragmented files
Init *InitSegment // Init data (ftyp + moov for fragmented file)
Sidx *SidxBox // SidxBox for a DASH OnDemand file
Segments []*MediaSegment // Media segments
Children []Box // All top-level boxes in order
FragEncMode EncFragFileMode // Determine how fragmented files are encoded
EncOptimize EncOptimize // Bit field with optimizations being done at encoding
isFragmented bool
fileDecMode DecFileMode
}
type EncFragFileMode byte
const (
EncModeSegment = EncFragFileMode(0) // Only encode boxes that are part of Init and MediaSegments
EncModeBoxTree = EncFragFileMode(1) // Encode all boxes in file tree
)
type DecFileMode byte
const (
// DecModeNormal reads Mdat data into memory during file decoding.
DecModeNormal DecFileMode = iota
// DecModeLazyMdat doesn't not read Mdat data into memory,
// which means the decoding process requires less memory and faster.
DecModeLazyMdat
)
type EncOptimize uint32
const (
OptimizeNone = EncOptimize(0)
OptimizeTrun = EncOptimize(1 << 0)
)
func (eo EncOptimize) String() string {
var optList []string
msg := "OptimizeNone"
if eo&OptimizeTrun != 0 {
optList = append(optList, "OptimizeTrun")
}
if len(optList) > 0 {
msg = strings.Join(optList, " | ")
}
return msg
}
// NewFile - create MP4 file
func NewFile() *File {
return &File{
FragEncMode: EncModeSegment,
EncOptimize: OptimizeNone,
fileDecMode: DecModeNormal,
}
}
// ReadMP4File - read an mp4 file from path
func ReadMP4File(path string) (*File, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
mp4Root, err := DecodeFile(f)
if err != nil {
return nil, err
}
return mp4Root, nil
}
// BoxStructure represent a box or similar entity such as a Segment
type BoxStructure interface {
Encode(w io.Writer) error
}
// WriteToFile - write a box structure to a file at filePath
func WriteToFile(boxStructure BoxStructure, filePath string) error {
ofd, err := os.Create(filePath)
if err != nil {
return err
}
defer ofd.Close()
err = boxStructure.Encode(ofd)
return err
}
// AddMediaSegment - add a mediasegment to file f
func (f *File) AddMediaSegment(m *MediaSegment) {
f.Segments = append(f.Segments, m)
}
// DecodeFile - parse and decode a file from reader r with optional file options.
// For example, the file options overwrite the default decode or encode mode.
func DecodeFile(r io.Reader, options ...Option) (*File, error) {
f := NewFile()
// apply options to change the default decode or encode mode
f.ApplyOptions(options...)
var boxStartPos uint64 = 0
lastBoxType := ""
var rs io.ReadSeeker
if f.fileDecMode == DecModeLazyMdat {
ok := false
rs, ok = r.(io.ReadSeeker)
if !ok {
return nil, fmt.Errorf("expecting readseeker when decoding file lazily, but got %T", r)
}
}
LoopBoxes:
for {
var box Box
var err error
if f.fileDecMode == DecModeLazyMdat {
box, err = DecodeBoxLazyMdat(boxStartPos, rs)
} else {
box, err = DecodeBox(boxStartPos, r)
}
if err == io.EOF {
break LoopBoxes
}
if err != nil {
return nil, err
}
boxType, boxSize := box.Type(), box.Size()
if err != nil {
return nil, err
}
if boxType == "mdat" {
if f.isFragmented {
if lastBoxType != "moof" {
return nil, fmt.Errorf("Does not support %v between moof and mdat", lastBoxType)
}
}
}
f.AddChild(box, boxStartPos)
lastBoxType = boxType
boxStartPos += boxSize
}
return f, nil
}
// AddChild - add child with start position
func (f *File) AddChild(box Box, boxStartPos uint64) {
switch box.Type() {
case "ftyp":
f.Ftyp = box.(*FtypBox)
case "moov":
f.Moov = box.(*MoovBox)
if len(f.Moov.Trak.Mdia.Minf.Stbl.Stts.SampleCount) == 0 {
f.isFragmented = true
f.Init = NewMP4Init()
f.Init.AddChild(f.Ftyp)
f.Init.AddChild(f.Moov)
}
case "sidx":
if len(f.Segments) == 0 { // sidx before first styp
f.Sidx = box.(*SidxBox)
} else {
currSeg := f.Segments[len(f.Segments)-1]
currSeg.Sidx = box.(*SidxBox)
}
case "styp":
f.isFragmented = true
newSeg := NewMediaSegment()
newSeg.Styp = box.(*StypBox)
f.AddMediaSegment(newSeg)
case "moof":
f.isFragmented = true
moof := box.(*MoofBox)
moof.StartPos = boxStartPos
var currentSegment *MediaSegment
if len(f.Segments) == 0 || f.Segments[0].Styp == nil {
// No styp present, so one fragment per segment
currentSegment = NewMediaSegment()
f.AddMediaSegment(currentSegment)
} else {
currentSegment = f.LastSegment()
}
newFragment := NewFragment()
currentSegment.AddFragment(newFragment)
newFragment.AddChild(moof)
case "mdat":
mdat := box.(*MdatBox)
if !f.isFragmented {
f.Mdat = mdat
} else {
currentFragment := f.LastSegment().LastFragment()
currentFragment.AddChild(mdat)
}
}
f.Children = append(f.Children, box)
}
// DumpWithSampleData - print information about file and its children boxes
func (f *File) DumpWithSampleData(w io.Writer, specificBoxLevels string) error {
if f.isFragmented {
fmt.Printf("Init segment\n")
err := f.Init.Info(w, specificBoxLevels, "", " ")
if err != nil {
return err
}
for i, seg := range f.Segments {
fmt.Printf(" mediaSegment %d\n", i)
for j, frag := range seg.Fragments {
fmt.Printf(" fragment %d\n ", j)
w, err := os.OpenFile("tmp.264", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
err = frag.DumpSampleData(w, f.Init.Moov.Mvex.Trex)
if err != nil {
w.Close()
return err
}
w.Close()
}
}
} else {
err := f.Ftyp.Info(w, specificBoxLevels, "", " ")
if err != nil {
return err
}
err = f.Moov.Info(w, specificBoxLevels, "", " ")
if err != nil {
return err
}
}
return nil
}
// Encode - encode a file to a Writer
// Fragmented files are encoded based on InitSegment and MediaSegments, unless EncodeVerbatim is set.
func (f *File) Encode(w io.Writer) error {
if f.isFragmented {
switch f.FragEncMode {
case EncModeSegment:
if f.Init != nil {
err := f.Init.Encode(w)
if err != nil {
return err
}
}
if f.Sidx != nil {
err := f.Sidx.Encode(w)
if err != nil {
return err
}
}
for _, seg := range f.Segments {
if f.EncOptimize&OptimizeTrun != 0 {
seg.EncOptimize = f.EncOptimize
}
err := seg.Encode(w)
if err != nil {
return err
}
}
case EncModeBoxTree:
for _, b := range f.Children {
err := b.Encode(w)
if err != nil {
return err
}
}
default:
return fmt.Errorf("Unknown FragEncMode=%d", f.FragEncMode)
}
return nil
}
// Progressive file
for _, b := range f.Children {
err := b.Encode(w)
if err != nil {
return err
}
}
return nil
}
// Info - write box tree with indent for each level
func (f *File) Info(w io.Writer, specificBoxLevels, indent, indentStep string) error {
for _, box := range f.Children {
err := box.Info(w, specificBoxLevels, indent, indentStep)
if err != nil {
return err
}
}
return nil
}
// LastSegment - Currently last segment
func (f *File) LastSegment() *MediaSegment {
return f.Segments[len(f.Segments)-1]
}
// IsFragmented - is file made of multiple segments (Mp4 fragments)
func (f *File) IsFragmented() bool {
return f.isFragmented
}
// ApplyOptions - applies options for decoding or encoding a file
func (f *File) ApplyOptions(opts ...Option) {
for _, opt := range opts {
opt(f)
}
}
// Option is function signature of file options.
// The design follows functional options pattern.
type Option func(f *File)
// WithEncodeMode sets up EncFragFileMode
func WithEncodeMode(mode EncFragFileMode) Option {
return func(f *File) { f.FragEncMode = mode }
}
// WithDecodeMode sets up DecFileMode
func WithDecodeMode(mode DecFileMode) Option {
return func(f *File) { f.fileDecMode = mode }
}
// CopySampleData - copy sample data from a track in a progressive mp4 file to w. Use rs if lazy read.
func (f *File) CopySampleData(w io.Writer, rs io.ReadSeeker, trak *TrakBox, startSampleNr, endSampleNr uint32) error {
if f.isFragmented {
return fmt.Errorf("only available for progressive files")
}
mdat := f.Mdat
if mdat.IsLazy() && rs == nil {
return fmt.Errorf("no ReadSeeker for lazy mdat")
}
mdatPayloadStart := mdat.PayloadAbsoluteOffset()
stbl := trak.Mdia.Minf.Stbl
chunks, err := stbl.Stsc.GetContainingChunks(startSampleNr, endSampleNr)
if err != nil {
return err
}
var chunkOffsets []uint64
if stbl.Stco != nil {
chunkOffsets = make([]uint64, len(stbl.Stco.ChunkOffset))
for i := range stbl.Stco.ChunkOffset {
chunkOffsets[i] = uint64(stbl.Stco.ChunkOffset[i])
}
} else if stbl.Co64 != nil {
chunkOffsets = stbl.Co64.ChunkOffset
} else {
return fmt.Errorf("neither stco nor co64 available")
}
var startNr, endNr uint32
var offset uint64
for i, chunk := range chunks {
startNr = chunk.StartSampleNr
endNr = startNr + chunk.NrSamples - 1
offset = chunkOffsets[chunk.ChunkNr-1]
if i == 0 {
for sNr := chunk.StartSampleNr; sNr < startSampleNr; sNr++ {
offset += uint64(stbl.Stsz.SampleSize[sNr-1])
}
startNr = startSampleNr
}
if i == len(chunks)-1 {
endNr = endSampleNr
}
var size int64
for sNr := startNr; sNr <= endNr; sNr++ {
size += int64(stbl.Stsz.GetSampleSize(int(sNr)))
}
if mdat.IsLazy() {
_, err := rs.Seek(int64(offset), io.SeekStart)
if err != nil {
return err
}
n, err := io.CopyN(w, rs, size)
if err != nil {
return err
}
if n != size {
return fmt.Errorf("copied %d bytes instead of %d", n, size)
}
} else {
offsetInMdatData := offset - mdatPayloadStart
n, err := w.Write(mdat.Data[offsetInMdatData : offsetInMdatData+uint64(size)])
if err != nil {
return err
}
if int64(n) != size {
return fmt.Errorf("copied %d bytes instead of %d", n, size)
}
}
}
return nil
}