-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtables.go
More file actions
366 lines (326 loc) · 8.55 KB
/
tables.go
File metadata and controls
366 lines (326 loc) · 8.55 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// tables.go
package sofia
import "errors"
// --- STBL ---
type StblBox struct {
Header *BoxHeader
Stsd *StsdBox
RawChildren [][]byte
}
func DecodeStblBox(data []byte) (*StblBox, error) {
b := &StblBox{}
var err error
b.Header, err = DecodeBoxHeader(data)
if err != nil {
return nil, err
}
payload := data[8:b.Header.Size]
offset := 0
for offset < len(payload) {
header, err := DecodeBoxHeader(payload[offset:])
if err != nil {
break
}
boxSize := int(header.Size)
if boxSize == 0 {
boxSize = len(payload) - offset
}
if boxSize < 8 || offset+boxSize > len(payload) {
return nil, errors.New("invalid child box size")
}
content := payload[offset : offset+boxSize]
switch string(header.Type[:]) {
case "stsd":
stsd, err := DecodeStsdBox(content)
if err != nil {
return nil, err
}
b.Stsd = stsd
default:
b.RawChildren = append(b.RawChildren, content)
}
offset += boxSize
}
return b, nil
}
func (b *StblBox) Encode() []byte {
buffer := make([]byte, 8)
if b.Stsd != nil {
buffer = append(buffer, b.Stsd.Encode()...)
}
for _, child := range b.RawChildren {
buffer = append(buffer, child...)
}
b.Header.Size = uint32(len(buffer))
b.Header.Put(buffer)
return buffer
}
// --- STTS ---
type SttsEntry struct {
SampleCount uint32
SampleDuration uint32
}
type SttsBox struct {
Header *BoxHeader
Entries []SttsEntry
}
func (b *SttsBox) Encode() []byte {
size := 16 + len(b.Entries)*8
buffer := make([]byte, size)
w := writer{buf: buffer}
w.offset = 8 // Skip header
w.PutBytes([]byte{0, 0, 0, 0})
w.PutUint32(uint32(len(b.Entries)))
for _, entry := range b.Entries {
w.PutUint32(entry.SampleCount)
w.PutUint32(entry.SampleDuration)
}
b.Header.Size = uint32(size)
b.Header.Type = [4]byte{'s', 't', 't', 's'}
b.Header.Put(buffer)
return buffer
}
func buildStts(samples []RemuxSample) []byte {
if len(samples) == 0 {
return nil
}
var entries []SttsEntry
currentDuration := samples[0].Duration
currentCount := uint32(0)
for _, sample := range samples {
if sample.Duration == currentDuration {
currentCount++
} else {
entries = append(entries, SttsEntry{currentCount, currentDuration})
currentDuration = sample.Duration
currentCount = 1
}
}
entries = append(entries, SttsEntry{currentCount, currentDuration})
box := SttsBox{Header: &BoxHeader{}, Entries: entries}
return box.Encode()
}
// --- CTTS ---
type CttsEntry struct {
SampleCount uint32
SampleOffset int32
}
type CttsBox struct {
Header *BoxHeader
Entries []CttsEntry
}
func (b *CttsBox) Encode() []byte {
size := 16 + len(b.Entries)*8
buffer := make([]byte, size)
w := writer{buf: buffer}
w.offset = 8 // Skip header
w.PutUint32(0) // Version 0, Flags 0
w.PutUint32(uint32(len(b.Entries)))
for _, entry := range b.Entries {
w.PutUint32(entry.SampleCount)
w.PutUint32(uint32(entry.SampleOffset))
}
b.Header.Size = uint32(size)
b.Header.Type = [4]byte{'c', 't', 't', 's'}
b.Header.Put(buffer)
return buffer
}
func buildCtts(samples []RemuxSample) []byte {
hasCTO := false
for _, sample := range samples {
if sample.CompositionTimeOffset != 0 {
hasCTO = true
break
}
}
if !hasCTO {
return nil // No ctts box needed if all offsets are 0
}
var entries []CttsEntry
if len(samples) > 0 {
currentOffset := samples[0].CompositionTimeOffset
currentCount := uint32(0)
for _, sample := range samples {
if sample.CompositionTimeOffset == currentOffset {
currentCount++
} else {
entries = append(entries, CttsEntry{currentCount, currentOffset})
currentOffset = sample.CompositionTimeOffset
currentCount = 1
}
}
entries = append(entries, CttsEntry{currentCount, currentOffset})
}
box := CttsBox{Header: &BoxHeader{}, Entries: entries}
return box.Encode()
}
// --- STSZ ---
type StszBox struct {
Header *BoxHeader
SampleSize uint32
SampleCount uint32
EntrySizes []uint32
}
func (b *StszBox) Encode() []byte {
size := 20 + len(b.EntrySizes)*4
buffer := make([]byte, size)
w := writer{buf: buffer}
w.offset = 8 // Skip header
w.PutUint32(0)
w.PutUint32(b.SampleSize)
w.PutUint32(b.SampleCount)
for _, entrySize := range b.EntrySizes {
w.PutUint32(entrySize)
}
b.Header.Size = uint32(size)
b.Header.Type = [4]byte{'s', 't', 's', 'z'}
b.Header.Put(buffer)
return buffer
}
func buildStsz(samples []RemuxSample) []byte {
entries := make([]uint32, len(samples))
for i, sample := range samples {
entries[i] = sample.Size
}
box := StszBox{Header: &BoxHeader{}, SampleSize: 0, SampleCount: uint32(len(samples)), EntrySizes: entries}
return box.Encode()
}
// --- STSC ---
type StscEntry struct {
FirstChunk uint32
SamplesPerChunk uint32
SampleDescriptionIndex uint32
}
type StscBox struct {
Header *BoxHeader
Entries []StscEntry
}
func (b *StscBox) Encode() []byte {
size := 16 + len(b.Entries)*12
buffer := make([]byte, size)
w := writer{buf: buffer}
w.offset = 8 // Skip header
w.PutUint32(0)
w.PutUint32(uint32(len(b.Entries)))
for _, entry := range b.Entries {
w.PutUint32(entry.FirstChunk)
w.PutUint32(entry.SamplesPerChunk)
w.PutUint32(entry.SampleDescriptionIndex)
}
b.Header.Size = uint32(size)
b.Header.Type = [4]byte{'s', 't', 's', 'c'}
b.Header.Put(buffer)
return buffer
}
func buildStsc(counts []uint32) []byte {
var entries []StscEntry
chunkIdx := uint32(1)
for _, count := range counts {
if len(entries) > 0 {
last := &entries[len(entries)-1]
if last.SamplesPerChunk == count {
chunkIdx++
continue
}
}
entries = append(entries, StscEntry{chunkIdx, count, 1})
chunkIdx++
}
box := StscBox{Header: &BoxHeader{}, Entries: entries}
return box.Encode()
}
// --- STCO ---
type StcoBox struct {
Header *BoxHeader
Offsets []uint32
}
func (b *StcoBox) Encode() []byte {
size := 16 + len(b.Offsets)*4
buffer := make([]byte, size)
w := writer{buf: buffer}
w.offset = 8 // Skip header
w.PutUint32(0)
w.PutUint32(uint32(len(b.Offsets)))
for _, offset := range b.Offsets {
w.PutUint32(offset)
}
b.Header.Size = uint32(size)
b.Header.Type = [4]byte{'s', 't', 'c', 'o'}
b.Header.Put(buffer)
return buffer
}
// --- CO64 ---
type Co64Box struct {
Header *BoxHeader
Offsets []uint64
}
func (b *Co64Box) Encode() []byte {
size := 16 + len(b.Offsets)*8
buffer := make([]byte, size)
w := writer{buf: buffer}
w.offset = 8 // Skip header
w.PutUint32(0)
w.PutUint32(uint32(len(b.Offsets)))
for _, offset := range b.Offsets {
w.PutUint64(offset)
}
b.Header.Size = uint32(size)
b.Header.Type = [4]byte{'c', 'o', '6', '4'}
b.Header.Put(buffer)
return buffer
}
// buildChunkOffsetBox decides whether to use stco or co64.
func buildChunkOffsetBox(offsets []uint64) []byte {
use64bit := false
for _, offset := range offsets {
if offset > 0xFFFFFFFF {
use64bit = true
break
}
}
if use64bit {
// Build a 'co64' box
box := Co64Box{Header: &BoxHeader{}, Offsets: offsets}
return box.Encode()
}
// Build an 'stco' box
entries32 := make([]uint32, len(offsets))
for i, offset := range offsets {
entries32[i] = uint32(offset)
}
box := StcoBox{Header: &BoxHeader{}, Offsets: entries32}
return box.Encode()
}
// --- STSS ---
type StssBox struct {
Header *BoxHeader
Indices []uint32
}
func (b *StssBox) Encode() []byte {
size := 16 + len(b.Indices)*4
buffer := make([]byte, size)
w := writer{buf: buffer}
w.offset = 8 // Skip Header
w.PutUint32(0)
w.PutUint32(uint32(len(b.Indices)))
for _, index := range b.Indices {
w.PutUint32(index)
}
b.Header.Size = uint32(size)
b.Header.Type = [4]byte{'s', 't', 's', 's'}
b.Header.Put(buffer)
return buffer
}
func buildStss(samples []RemuxSample) []byte {
var indices []uint32
for i, sample := range samples {
if sample.IsSync {
indices = append(indices, uint32(i+1))
}
}
if len(indices) == len(samples) {
return nil
}
box := StssBox{Header: &BoxHeader{}, Indices: indices}
return box.Encode()
}