/
TrackRun.go
185 lines (173 loc) · 4.59 KB
/
TrackRun.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
package sofia
import (
"encoding/binary"
"io"
)
type RunSample struct {
Duration uint32
Size uint32
Flags uint32
CompositionTimeOffset [4]byte
}
func (s *RunSample) read(r io.Reader, run *TrackRun) error {
if run.sample_duration_present() {
err := binary.Read(r, binary.BigEndian, &s.Duration)
if err != nil {
return err
}
}
if run.sample_size_present() {
err := binary.Read(r, binary.BigEndian, &s.Size)
if err != nil {
return err
}
}
if run.sample_flags_present() {
err := binary.Read(r, binary.BigEndian, &s.Flags)
if err != nil {
return err
}
}
if run.sample_composition_time_offsets_present() {
_, err := io.ReadFull(r, s.CompositionTimeOffset[:])
if err != nil {
return err
}
}
return nil
}
func (s RunSample) write(w io.Writer, run TrackRun) error {
if run.sample_duration_present() {
err := binary.Write(w, binary.BigEndian, s.Duration)
if err != nil {
return err
}
}
if run.sample_size_present() {
err := binary.Write(w, binary.BigEndian, s.Size)
if err != nil {
return err
}
}
if run.sample_flags_present() {
err := binary.Write(w, binary.BigEndian, s.Flags)
if err != nil {
return err
}
}
if run.sample_composition_time_offsets_present() {
_, err := w.Write(s.CompositionTimeOffset[:])
if err != nil {
return err
}
}
return nil
}
// ISO/IEC 14496-12
//
// If the data-offset is present, it is relative to the base-data-offset
// established in the track fragment header.
//
// aligned(8) class TrackRunBox extends FullBox('trun', version, tr_flags) {
// unsigned int(32) sample_count;
// signed int(32) data_offset; // 0x000001, assume present
// unsigned int(32) first_sample_flags; // 0x000004
// {
// unsigned int(32) sample_duration; // 0x000100
// unsigned int(32) sample_size; // 0x000200, assume present
// unsigned int(32) sample_flags // 0x000400
// if (version == 0) {
// unsigned int(32) sample_composition_time_offset; // 0x000800
// } else {
// signed int(32) sample_composition_time_offset; // 0x000800
// }
// }[ sample_count ]
// }
type TrackRun struct {
BoxHeader BoxHeader
FullBoxHeader FullBoxHeader
SampleCount uint32
DataOffset int32
FirstSampleFlags uint32
Sample []RunSample
}
// 0x000004 first-sample-flags-present
func (t TrackRun) first_sample_flags_present() bool {
return t.FullBoxHeader.get_flags()&4 >= 1
}
func (t *TrackRun) read(r io.Reader) error {
err := t.FullBoxHeader.read(r)
if err != nil {
return err
}
err = binary.Read(r, binary.BigEndian, &t.SampleCount)
if err != nil {
return err
}
err = binary.Read(r, binary.BigEndian, &t.DataOffset)
if err != nil {
return err
}
if t.first_sample_flags_present() {
err := binary.Read(r, binary.BigEndian, &t.FirstSampleFlags)
if err != nil {
return err
}
}
t.Sample = make([]RunSample, t.SampleCount)
for i, sample := range t.Sample {
err := sample.read(r, t)
if err != nil {
return err
}
t.Sample[i] = sample
}
return nil
}
// 0x000800 sample-composition-time-offsets-present
func (t TrackRun) sample_composition_time_offsets_present() bool {
return t.FullBoxHeader.get_flags()&0x800 >= 1
}
// 0x000100 sample-duration-present
func (t TrackRun) sample_duration_present() bool {
return t.FullBoxHeader.get_flags()&0x100 >= 1
}
// 0x000400 sample-flags-present
func (t TrackRun) sample_flags_present() bool {
return t.FullBoxHeader.get_flags()&0x400 >= 1
}
// 0x000200 sample-size-present
func (t TrackRun) sample_size_present() bool {
return t.FullBoxHeader.get_flags()&0x200 >= 1
}
func (t TrackRun) write(w io.Writer) error {
err := t.BoxHeader.write(w)
if err != nil {
return err
}
err = t.FullBoxHeader.write(w)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, t.SampleCount)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, t.DataOffset)
if err != nil {
return err
}
if t.first_sample_flags_present() {
err := binary.Write(w, binary.BigEndian, t.FirstSampleFlags)
if err != nil {
return err
}
}
for _, sample := range t.Sample {
err := sample.write(w, t)
if err != nil {
return err
}
}
return nil
}