-
Notifications
You must be signed in to change notification settings - Fork 2
/
isobmff.go
230 lines (184 loc) · 5.62 KB
/
isobmff.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
package quicktime
import (
"bytes"
"encoding/binary"
"errors"
"io"
)
var ErrFinalSegment = errors.New("MFRA atom read")
type IsoBmffInitSegment struct {
FTYP *Atom
MOOV *Atom
}
type IsoBmffMediaSegment struct {
MOOF *Atom
MDAT *Atom
BaseVideoMediaDecodeTime uint64
BaseAudioMediaDecodeTime uint64
}
type IsoBmffMergedSegment struct {
*IsoBmffInitSegment
*IsoBmffMediaSegment
Buffer []byte
}
func ReadIsoBmffInitSegment(r io.Reader) (*IsoBmffInitSegment, error) {
ftyp, err := ReadAtom(r)
if err != nil {
return nil, err
}
if ftyp.Header.Type != "ftyp" {
return nil, errors.New("FTYP atom expected but got: " + ftyp.Header.Type)
}
moov, err := ReadAtom(r)
if err != nil {
return nil, err
}
if moov.Header.Type != "moov" {
return nil, errors.New("MOOV atom expected but got: " + moov.Header.Type)
}
return &IsoBmffInitSegment{ftyp, moov}, nil
}
func ReadIsoBmffMediaSegment(r io.Reader) (*IsoBmffMediaSegment, error) {
moof, err := ReadAtom(r)
if err != nil {
return nil, err
}
if moof.Header.Type == "mfra" {
return nil, ErrFinalSegment
}
if moof.Header.Type != "moof" {
return nil, errors.New("MOOF atom expected but got: " + moof.Header.Type)
}
////////////////////////////////////////////////////////////////////////
//////////////////////////// Base media decode time ////////////////////
////////////////////////////////////////////////////////////////////////
// MFHD [skip]
mfhd, err := ParseAtomHeader(moof.Buffer[AtomHeaderLength:])
if err != nil {
return nil, err
}
if mfhd.Type != "mfhd" {
return nil, errors.New("MFHD atom expected but got: " + mfhd.Type)
}
// TRAF
videoTraf, err := ParseAtomHeader(moof.Buffer[AtomHeaderLength+mfhd.Size:])
if err != nil {
return nil, err
}
if videoTraf.Type != "traf" {
return nil, errors.New("TRAF atom expected but got: " + videoTraf.Type)
}
// TRAF->TFHD [skip]
videoTfhd, err := ParseAtomHeader(moof.Buffer[AtomHeaderLength+mfhd.Size+AtomHeaderLength:])
if err != nil {
return nil, err
}
if videoTfhd.Type != "tfhd" {
return nil, errors.New("TFHD atom expected but got: " + videoTfhd.Type)
}
// TRAF->TFDT [Base media decode time]
videoTfdt, err := ParseAtomHeader(moof.Buffer[AtomHeaderLength+mfhd.Size+AtomHeaderLength+videoTfhd.Size:])
if err != nil {
return nil, err
}
if videoTfdt.Type != "tfdt" {
return nil, errors.New("TFDT atom expected but got: " + videoTfdt.Type)
}
var baseVideoMediaDecodeTime uint64
binary.Read(
bytes.NewReader(moof.Buffer[AtomHeaderLength+mfhd.Size+AtomHeaderLength+videoTfhd.Size+AtomHeaderLength+4:]),
binary.BigEndian,
&baseVideoMediaDecodeTime)
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
// MFHD [skip]
mfhd, err = ParseAtomHeader(moof.Buffer[AtomHeaderLength:])
if err != nil {
return nil, err
}
if mfhd.Type != "mfhd" {
return nil, errors.New("MFHD atom expected but got: " + mfhd.Type)
}
// TRAF [skip]
videoTraf, err = ParseAtomHeader(moof.Buffer[AtomHeaderLength+mfhd.Size:])
if err != nil {
return nil, err
}
if videoTraf.Type != "traf" {
return nil, errors.New("TRAF atom expected but got: " + videoTraf.Type)
}
////////////////////////////////////////////////////////////////////////
// TRAF
audioTraf, err := ParseAtomHeader(moof.Buffer[AtomHeaderLength+mfhd.Size+videoTraf.Size:])
if err != nil {
return nil, err
}
if audioTraf.Type != "traf" {
return nil, errors.New("TRAF atom expected but got: " + audioTraf.Type)
}
// TRAF->TFHD [skip]
audioTfhd, err := ParseAtomHeader(moof.Buffer[AtomHeaderLength+mfhd.Size+videoTraf.Size+AtomHeaderLength:])
if err != nil {
return nil, err
}
if audioTfhd.Type != "tfhd" {
return nil, errors.New("TFHD atom expected but got: " + audioTfhd.Type)
}
// TRAF->TFDT [Base media decode time]
audioTfdt, err := ParseAtomHeader(moof.Buffer[AtomHeaderLength+mfhd.Size+videoTraf.Size+AtomHeaderLength+audioTfhd.Size:])
if err != nil {
return nil, err
}
if audioTfdt.Type != "tfdt" {
return nil, errors.New("TFDT atom expected but got: " + audioTfdt.Type)
}
var baseAudioMediaDecodeTime uint64
binary.Read(
bytes.NewReader(moof.Buffer[AtomHeaderLength+mfhd.Size+videoTraf.Size+AtomHeaderLength+audioTfhd.Size+AtomHeaderLength+4:]),
binary.BigEndian,
&baseAudioMediaDecodeTime)
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
mdat, err := ReadAtom(r)
if err != nil {
return nil, err
}
if mdat.Header.Type != "mdat" {
return nil, errors.New("MDAT atom expected but got: " + mdat.Header.Type)
}
return &IsoBmffMediaSegment{moof, mdat, baseVideoMediaDecodeTime, baseAudioMediaDecodeTime}, nil
}
func ReadIsoBmffMergedSegment(r io.Reader, prev *IsoBmffMergedSegment) (*IsoBmffMergedSegment, error) {
var (
init *IsoBmffInitSegment
media *IsoBmffMediaSegment
err error
)
// Read init segment
if prev == nil {
init, err = ReadIsoBmffInitSegment(r)
if err != nil {
return nil, err
}
} else {
init = prev.IsoBmffInitSegment
}
// Read media segment
media, err = ReadIsoBmffMediaSegment(r)
if err != nil {
return nil, err
}
// Prepare for creating a buffer
ftyp := init.FTYP.Buffer
moov := init.MOOV.Buffer
moof := media.MOOF.Buffer
mdat := media.MDAT.Buffer
size := len(ftyp) + len(moov) + len(moof) + len(mdat)
buffer := bytes.NewBuffer(make([]byte, 0, size))
// Write all segment one after the other
buffer.Write(ftyp)
buffer.Write(moov)
buffer.Write(moof)
buffer.Write(mdat)
return &IsoBmffMergedSegment{init, media, buffer.Bytes()}, nil
}