forked from Eyevinn/mp4ff
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tfhd.go
200 lines (176 loc) · 4.85 KB
/
tfhd.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
package mp4
import (
"io"
"io/ioutil"
)
const baseDataOffsetPresent uint32 = 0x000001
const sampleDescriptionIndexPresent uint32 = 0x000002
const defaultSampleDurationPresent uint32 = 0x000008
const defaultSampleSizePresent uint32 = 0x000010
const defaultSampleFlagsPresent uint32 = 0x000020
const durationIsEmpty uint32 = 0x010000
const defaultBaseIsMoof uint32 = 0x020000
// TfhdBox - Track Fragment Header Box (tfhd)
//
// Contained in : Track Fragment box (traf))
type TfhdBox struct {
Version byte
Flags uint32
TrackID uint32
BaseDataOffset uint64
SampleDescriptionIndex uint32
DefaultSampleDuration uint32
DefaultSampleSize uint32
DefaultSampleFlags uint32
}
// DecodeTfhd - box-specific decode
func DecodeTfhd(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)
flags := versionAndFlags & flagsMask
t := &TfhdBox{
Version: version,
Flags: flags,
TrackID: s.ReadUint32(),
}
if t.HasBaseDataOffset() {
t.BaseDataOffset = s.ReadUint64()
}
if t.HasSampleDescriptionIndex() {
t.SampleDescriptionIndex = s.ReadUint32()
}
if t.HasDefaultSampleDuration() {
t.DefaultSampleDuration = s.ReadUint32()
}
if t.HasDefaultSampleSize() {
t.DefaultSampleSize = s.ReadUint32()
}
if t.HasDefaultSampleFlags() {
t.DefaultSampleFlags = s.ReadUint32()
}
return t, nil
}
// CreateTfhd - Create a new TfdtBox with baseMediaDecodeTime
func CreateTfhd(trackID uint32) *TfhdBox {
// The only flag set is defaultBaseIsMoof
tfhd := &TfhdBox{
Version: 0,
Flags: defaultBaseIsMoof,
TrackID: trackID,
BaseDataOffset: 0,
SampleDescriptionIndex: 1,
DefaultSampleDuration: 0,
DefaultSampleSize: 0,
DefaultSampleFlags: 0,
}
return tfhd
}
// HasBaseDataOffset - interpreted flags value
func (t *TfhdBox) HasBaseDataOffset() bool {
return t.Flags&baseDataOffsetPresent != 0
}
// HasSampleDescriptionIndex - interpreted flags value
func (t *TfhdBox) HasSampleDescriptionIndex() bool {
return t.Flags&sampleDescriptionIndexPresent != 0
}
// HasDefaultSampleDuration - interpreted flags value
func (t *TfhdBox) HasDefaultSampleDuration() bool {
return t.Flags&defaultSampleDurationPresent != 0
}
// HasDefaultSampleSize - interpreted flags value
func (t *TfhdBox) HasDefaultSampleSize() bool {
return t.Flags&defaultSampleSizePresent != 0
}
// HasDefaultSampleFlags - interpreted flags value
func (t *TfhdBox) HasDefaultSampleFlags() bool {
return t.Flags&defaultSampleFlagsPresent != 0
}
// DurationIsEmpty - interpreted flags value
func (t *TfhdBox) DurationIsEmpty() bool {
return t.Flags&durationIsEmpty != 0
}
// DefaultBaseIfMoof - interpreted flags value
func (t *TfhdBox) DefaultBaseIfMoof() bool {
return t.Flags&defaultBaseIsMoof != 0
}
// Type - returns box type
func (t *TfhdBox) Type() string {
return "tfhd"
}
// Size - returns calculated size
func (t *TfhdBox) Size() uint64 {
sz := boxHeaderSize + 8
if t.HasBaseDataOffset() {
sz += 8
}
if t.HasSampleDescriptionIndex() {
sz += 4
}
if t.HasDefaultSampleDuration() {
sz += 4
}
if t.HasDefaultSampleSize() {
sz += 4
}
if t.HasDefaultSampleFlags() {
sz += 4
}
return uint64(sz)
}
// Encode - write box to w
func (t *TfhdBox) Encode(w io.Writer) error {
err := EncodeHeader(t, w)
if err != nil {
return err
}
buf := makebuf(t)
sw := NewSliceWriter(buf)
versionAndFlags := (uint32(t.Version) << 24) + t.Flags
sw.WriteUint32(versionAndFlags)
sw.WriteUint32(t.TrackID)
if t.HasBaseDataOffset() {
sw.WriteUint64(t.BaseDataOffset)
}
if t.HasSampleDescriptionIndex() {
sw.WriteUint32(t.SampleDescriptionIndex)
}
if t.HasDefaultSampleDuration() {
sw.WriteUint32(t.DefaultSampleDuration)
}
if t.HasDefaultSampleSize() {
sw.WriteUint32(t.DefaultSampleSize)
}
if t.HasDefaultSampleFlags() {
sw.WriteUint32(t.DefaultSampleFlags)
}
_, err = w.Write(buf)
return err
}
func (t *TfhdBox) Info(w io.Writer, specificBoxLevels, indent, indentStep string) error {
bd := newInfoDumper(w, indent, t, int(t.Version), t.Flags)
bd.write(" - trackID: %d", t.TrackID)
if t.Flags&defaultBaseIsMoof != 0 {
bd.write(" - defaultBaseIsMoof: true")
}
if t.HasBaseDataOffset() {
bd.write(" - baseDataOffset=%d", t.BaseDataOffset)
}
if t.HasSampleDescriptionIndex() {
bd.write(" - sampleDescriptionIndex: %d", t.SampleDescriptionIndex)
}
if t.HasDefaultSampleDuration() {
bd.write(" - defaultSampleDuration: %d", t.DefaultSampleDuration)
}
if t.HasDefaultSampleSize() {
bd.write(" - defaultSampleSize: %d", t.DefaultSampleSize)
}
if t.HasDefaultSampleFlags() {
bd.write(" - defaultSampleFlags: %08x (%s)", t.DefaultSampleFlags, DecodeSampleFlags(t.DefaultSampleFlags))
}
return bd.err
}