forked from Eyevinn/mp4ff
/
mvhd.go
124 lines (112 loc) · 3.13 KB
/
mvhd.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
package mp4
import (
"io"
"io/ioutil"
)
// MvhdBox - Movie Header Box (mvhd - mandatory)
//
// Contained in : Movie Box (‘moov’)
//
// Contains all media information (duration, ...).
//
// Duration is measured in "time units", and timescale defines the number of time units per second.
//
type MvhdBox struct {
Version byte
Flags uint32
CreationTime uint64
ModificationTime uint64
Timescale uint32
Duration uint64
NextTrackID uint32
Rate Fixed32
Volume Fixed16
}
// CreateMvhd - create mvhd box with reasonable values
func CreateMvhd() *MvhdBox {
return &MvhdBox{
Timescale: 90000, // Irrelevant since mdhd timescale is used
NextTrackID: 2, // There will typically only be one track
Rate: 0x00010000, // This is 1.0
Volume: 0x0100, // Full volume
}
}
// DecodeMvhd - box-specific decode
func DecodeMvhd(hdr *boxHeader, startPos uint64, r io.Reader) (Box, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
s := NewSliceReader(data)
versionAndFlags := s.ReadUint32()
version := byte(versionAndFlags >> 24)
m := &MvhdBox{
Version: version,
Flags: versionAndFlags & flagsMask,
}
if version == 1 {
m.CreationTime = s.ReadUint64()
m.ModificationTime = s.ReadUint64()
m.Timescale = s.ReadUint32()
m.Duration = s.ReadUint64()
} else {
m.CreationTime = uint64(s.ReadUint32())
m.ModificationTime = uint64(s.ReadUint32())
m.Timescale = s.ReadUint32()
m.Duration = uint64(s.ReadUint32())
}
m.Rate = Fixed32(s.ReadUint32())
m.Volume = Fixed16(s.ReadUint16())
s.SkipBytes(10) // Reserved bytes
s.SkipBytes(36) // Matrix patterndata
s.SkipBytes(24) // Predefined 0
m.NextTrackID = s.ReadUint32()
return m, nil
}
// Type - return box type
func (b *MvhdBox) Type() string {
return "mvhd"
}
// Size - return calculated size
func (b *MvhdBox) Size() uint64 {
if b.Version == 1 {
return 12 + 80 + 28 // Full header + variable part + fixed part
}
return 12 + 80 + 16 // Full header + variable part + fixed part
}
// Encode - write box to w
func (b *MvhdBox) Encode(w io.Writer) error {
err := EncodeHeader(b, w)
if err != nil {
return err
}
buf := makebuf(b)
sw := NewSliceWriter(buf)
versionAndFlags := (uint32(b.Version) << 24) + b.Flags
sw.WriteUint32(versionAndFlags)
if b.Version == 0 {
sw.WriteUint32(uint32(b.CreationTime))
sw.WriteUint32(uint32(b.ModificationTime))
sw.WriteUint32(b.Timescale)
sw.WriteUint32(uint32(b.Duration))
} else {
sw.WriteUint64(b.CreationTime)
sw.WriteUint64(b.ModificationTime)
sw.WriteUint32(b.Timescale)
sw.WriteUint64(b.Duration)
}
sw.WriteUint32(uint32(b.Rate))
sw.WriteUint16(uint16(b.Volume))
sw.WriteZeroBytes(10) // Reserved bytes
sw.WriteUnityMatrix() // unity matrix according to 8.2.2.2
sw.WriteZeroBytes(24) // Predefined 0
sw.WriteUint32(b.NextTrackID)
_, err = w.Write(buf)
return err
}
func (b *MvhdBox) Info(w io.Writer, specificBoxLevels, indent, indentStep string) error {
bd := newInfoDumper(w, indent, b, int(b.Version), b.Flags)
bd.write(" - timeScale: %d", b.Timescale)
bd.write(" - duration: %d", b.Duration)
return bd.err
}