-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp4.go
293 lines (251 loc) · 6.09 KB
/
mp4.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
package main
// http://l.web.umkc.edu/lizhu/teaching/2016sp.video-communication/ref/mp4.pdf
import (
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"log"
)
var MP4Endian = binary.BigEndian
func Read(r io.Reader, tgt interface{}) error {
return binary.Read(r, MP4Endian, tgt)
}
type FourCC [4]byte
var (
FOURCC_CO64 = FourCC{'c', 'o', '6', '4'}
FOURCC_HDLR = FourCC{'h', 'd', 'l', 'r'}
FOURCC_MDIA = FourCC{'m', 'd', 'i', 'a'}
FOURCC_MINF = FourCC{'m', 'i', 'n', 'f'}
FOURCC_MOOV = FourCC{'m', 'o', 'o', 'v'}
FOURCC_STBL = FourCC{'s', 't', 'b', 'l'}
FOURCC_STCO = FourCC{'s', 't', 'c', 'o'}
FOURCC_STSC = FourCC{'s', 't', 's', 'c'}
FOURCC_STSD = FourCC{'s', 't', 's', 'd'}
FOURCC_STSZ = FourCC{'s', 't', 's', 'z'}
FOURCC_STTS = FourCC{'s', 't', 't', 's'}
FOURCC_STZ2 = FourCC{'s', 't', 'z', '2'}
FOURCC_TRAK = FourCC{'t', 'r', 'a', 'k'}
)
func (x FourCC) String() string {
return string(x[:])
}
type MP4BoxHeader struct {
Size uint64
FourCC FourCC
}
const MP4BoxHeaderBytes = 8
func (h MP4BoxHeader) PayloadSize() int64 {
return int64(h.Size - MP4BoxHeaderBytes)
}
func ParseBoxHeader(r io.Reader) (res MP4BoxHeader, err error) {
var simpleSize uint32
if err = Read(r, &simpleSize); err != nil {
return res, err
}
if err = Read(r, &res.FourCC); err != nil {
return res, err
}
if simpleSize == 1 {
err = Read(r, &res.Size)
} else {
res.Size = uint64(simpleSize)
}
return res, err
}
// ScanBoxes scans the inside of an MP4 Box, for inner boxes.
//
// Boxes are given to f(header, payload) for inspection, which can yield io.EOF to end
// iteration, or some other error which will simply propagate up
func ScanBoxes(r io.ReadSeeker, f func(header MP4BoxHeader, payload io.ReadSeeker) error) error {
for {
header, err := ParseBoxHeader(r)
if err == io.EOF {
return nil
}
if err != nil {
return fmt.Errorf("Header Parse Error: %s", err)
}
var payload io.ReadSeeker
payload, err = LimitReadSeeker(r, header.PayloadSize())
if err != nil {
return err
}
err = f(header, payload)
// Silently skip remaining inner box.
pos, err := payload.Seek(0, SEEK_END)
if err != nil {
fmt.Errorf("Seek To end of Box (%d) failed: %s", pos, err)
}
if err == io.EOF {
return nil
}
if err != nil {
return err
}
}
}
type FullBox struct {
Version byte
Flags [3]byte
}
type TrackType FourCC
var (
TrackTypeSound = TrackType{'s', 'o', 'u', 'n'}
TrackTypeVideo = TrackType{'v', 'i', 'd', 'e'}
TrackTypeHint = TrackType{'h', 'i', 'n', 't'}
)
func (t TrackType) String() string {
return string(t[:])
}
type HandlerBox struct {
FullBox
Predefined uint32
HandlerType TrackType
Reserved [3]uint32
}
func ParseHandler(r io.Reader) (res HandlerBox, err error) {
err = Read(r, &res)
return res, err
}
type SimpleTableHeader struct {
FullBox
EntryCount uint32
}
type SampleDescriptionBox struct {
SimpleTableHeader
Entries []MP4BoxHeader // Highly simplified
}
func (sd SampleDescriptionBox) MatchesTypes(t []FourCC) bool {
for _, entry := range sd.Entries {
for _, m := range t {
if entry.FourCC == m {
return true
}
}
log.Printf("Track of wrong type: %s", entry.FourCC)
}
return false
}
func ParseSampleDescriptionBox(r io.Reader) (res SampleDescriptionBox, err error) {
err = Read(r, &res.SimpleTableHeader)
if err != nil {
return res, err
}
for {
header, err := ParseBoxHeader(r)
if err == io.EOF {
return res, nil
}
if err != nil {
return res, err
}
res.Entries = append(res.Entries, header)
_, err = io.CopyN(ioutil.Discard, r, header.PayloadSize())
if err != nil {
return res, err
}
}
}
type SampleSizeTable struct {
SampleSizeTableHeader
Entries []uint32
}
type SampleSizeTableHeader struct {
FullBox
ConstantSize uint32
EntryCount uint32
}
func (t SampleSizeTable) SampleSize(n int) uint32 {
if t.ConstantSize != 0 {
return t.ConstantSize
}
return t.Entries[n]
}
func (t SampleSizeTable) SampleSizes(n int, count int) uint64 {
if t.ConstantSize != 0 {
return uint64(t.ConstantSize) * uint64(count)
}
res := uint64(0)
for i := n; i < (n + count); i++ {
res += uint64(t.SampleSize(i))
}
return res
}
func ParseSampleSizeBox(r io.Reader) (res SampleSizeTable, err error) {
err = Read(r, &res.SampleSizeTableHeader)
if err != nil {
return res, err
}
if res.ConstantSize == 0 {
res.Entries = make([]uint32, res.EntryCount)
}
return res, Read(r, res.Entries)
}
type SampleToChunkTable struct {
SimpleTableHeader
Entries []SampleToChunkEntry
}
type SampleToChunkEntry struct {
FirstChunk uint32
SamplesPerChunk uint32
SampleDescriptionIndex uint32
}
func ParseSampleToChunkBox(r io.Reader) (res SampleToChunkTable, err error) {
err = Read(r, &res.SimpleTableHeader)
if err != nil {
return res, err
}
res.Entries = make([]SampleToChunkEntry, res.EntryCount)
return res, Read(r, res.Entries)
}
func (t SampleToChunkTable) ChunkSamples(chunkIndex int) int {
tableIndex := len(t.Entries) - 1
limit := uint32(chunkIndex + 1)
for ; t.Entries[tableIndex].FirstChunk > limit; tableIndex-- {
}
return int(t.Entries[tableIndex].SamplesPerChunk)
}
// ChunkOffsetTable is a unifying interface for the two versions
type ChunkOffsetTable interface {
Count() int
Offset(n int) uint64
}
type ChunkOffset32Table struct {
Entries []uint32
}
func (t ChunkOffset32Table) Count() int {
return len(t.Entries)
}
func (t ChunkOffset32Table) Offset(n int) uint64 {
return uint64(t.Entries[n])
}
type ChunkOffset64Table struct {
Entries []uint64
}
func (t ChunkOffset64Table) Count() int {
return len(t.Entries)
}
func (t ChunkOffset64Table) Offset(n int) uint64 {
return t.Entries[n]
}
func ParseChunkOffsetBox(r io.Reader, fourCC FourCC) (res ChunkOffsetTable, err error) {
var h SimpleTableHeader
err = Read(r, &h)
if err != nil {
return nil, err
}
switch fourCC {
case FOURCC_STCO:
res := ChunkOffset32Table{
Entries: make([]uint32, h.EntryCount),
}
return res, Read(r, res.Entries)
case FOURCC_CO64:
res := ChunkOffset64Table{
Entries: make([]uint64, h.EntryCount),
}
return res, Read(r, res.Entries)
}
return nil, fmt.Errorf("Unsupported Four-CC for ChunkOffset: %s", fourCC)
}