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
|
package fragment
import (
"encoding/binary"
"io"
"github.com/tetsuo/mp4"
)
// Writer writes fragmented MP4 segments.
type Writer struct {
w io.Writer
buf []byte // moof build buffer
copyBuf [131072]byte // sample data copy buffer
mdatHdr [8]byte // mdat box header (size + 'mdat'), reused per fragment
// State carried from Prepare to the body writers. moof points into buf and
// holds the finished moof bytes; mdatPayload is the sum of the sample sizes,
// so the body size is known before any media byte is read.
moof []byte
mdatPayload int64
// Per-track scratch buffers.
sampleIdx [maxTracks][]int
trunBuf [maxTracks][]mp4.TrunEntry
ranges []byteRange
}
type byteRange struct {
offset int64
size int64
}
// NewWriter creates a fragment writer which writes to the provided [io.Writer].
func NewWriter(w io.Writer) *Writer {
wr := &Writer{
w: w,
buf: make([]byte, 65536),
ranges: make([]byteRange, 0, 1024),
}
for i := range wr.sampleIdx {
wr.sampleIdx[i] = make([]int, 0, 512)
}
for i := range wr.trunBuf {
wr.trunBuf[i] = make([]mp4.TrunEntry, 0, 512)
}
return wr
}
// Reset prepares the [Writer] for reuse with a new [io.Writer].
func (w *Writer) Reset(dst io.Writer) {
w.w = dst
}
// WriteInit writes the init segment (ftyp+moov).
func (w *Writer) WriteInit(s *InitSegment) error {
_, err := w.w.Write(s.buf)
return err
}
// WriteFragment writes a single moof+mdat fragment.
// src is used to read sample data at the offsets specified in fragment samples.
func (w *Writer) WriteFragment(frag *Fragment, src io.ReaderAt) error {
if err := w.Prepare(frag); err != nil {
return err
}
return w.WriteBodyRange(w.w, src, 0, w.BodySize())
}
// BodySize returns the byte size of the body (moof + mdat) of the fragment last
// passed to [Writer.Prepare]. It is moofSize + 8 + sum(sample sizes), all known
// from sample metadata, so the size is available before any media is read.
func (w *Writer) BodySize() int64 {
return int64(len(w.moof)) + 8 + w.mdatPayload
}
// Prepare builds the moof for frag from sample metadata alone, reading no media.
// It records the moof bytes and the mdat payload size so [Writer.BodySize] and
// [Writer.WriteBodyRange] can write the body, or a byte window of it, afterward.
// The recorded state is valid until the next call to Prepare on this writer.
func (w *Writer) Prepare(frag *Fragment) error {
// Group samples by track
var trackIDs [maxTracks]uint32
groupCount := 0
for i := range w.sampleIdx {
w.sampleIdx[i] = w.sampleIdx[i][:0]
}
for i := range frag.Samples {
s := &frag.Samples[i]
found := false
for g := 0; g < groupCount; g++ {
if trackIDs[g] == s.TrackID {
w.sampleIdx[g] = append(w.sampleIdx[g], i)
found = true
break
}
}
if !found {
if groupCount >= maxTracks {
continue
}
trackIDs[groupCount] = s.TrackID
w.sampleIdx[groupCount] = append(w.sampleIdx[groupCount], i)
groupCount++
}
}
// Build byte ranges for mdat (contiguous runs of sample data)
w.ranges = w.ranges[:0]
for g := 0; g < groupCount; g++ {
for _, idx := range w.sampleIdx[g] {
s := &frag.Samples[idx]
sz := int64(s.Size())
if len(w.ranges) > 0 {
last := &w.ranges[len(w.ranges)-1]
if last.offset+last.size == s.Offset {
last.size += sz
continue
}
}
w.ranges = append(w.ranges, byteRange{offset: s.Offset, size: sz})
}
}
// Total mdat payload size
var mdatPayload int64
for i := range w.ranges {
mdatPayload += w.ranges[i].size
}
mdatHeaderSize := int32(8)
// Build trun entries per track group
type trafInfo struct {
trackID uint32
baseDTS int64
hasCtts bool
}
var trafs [maxTracks]trafInfo
for g := 0; g < groupCount; g++ {
ti := &trafs[g]
ti.trackID = trackIDs[g]
w.trunBuf[g] = w.trunBuf[g][:0]
indices := w.sampleIdx[g]
if len(indices) > 0 {
ti.baseDTS = frag.Samples[indices[0]].DTS
}
for _, idx := range indices {
s := &frag.Samples[idx]
entry := mp4.TrunEntry{
Duration: s.Duration,
Size: s.Size(),
Flags: sampleFlags(s.IsSync()),
}
if s.PresentationOffset != 0 {
entry.CompositionTimeOffset = s.PresentationOffset
ti.hasCtts = true
}
w.trunBuf[g] = append(w.trunBuf[g], entry)
}
}
// Track data layout in mdat
var trackDataOffsets [maxTracks]int32
var accum int32
for g := 0; g < groupCount; g++ {
trackDataOffsets[g] = accum
for _, idx := range w.sampleIdx[g] {
accum += int32(frag.Samples[idx].Size())
}
}
// Build moof, recording trun data_offset positions for backpatching
type trunPatch struct {
pos int
dataStart int32
}
var patches [maxTracks]trunPatch
patchCount := 0
mw := mp4.NewWriter(w.buf)
mw.Reset()
mw.StartBox(mp4.TypeMoof)
mw.WriteMfhd(frag.SequenceNum)
for g := 0; g < groupCount; g++ {
ti := &trafs[g]
entries := w.trunBuf[g]
n := len(entries)
// Move constant fields into tfhd defaults so the trun only carries what
// varies. Sample sizes always differ, so they stay per-sample. Sample
// flags differ only at the first sample (a keyframe) unless the segment
// holds interior keyframes, in which case they stay per-sample.
var defaultDuration, defaultFlags uint32
sameDuration, sameFlags, firstDiffers := true, true, false
if n > 0 {
defaultDuration = entries[0].Duration
ref := 0
if n > 1 {
ref = 1
}
defaultFlags = entries[ref].Flags
for i := 1; i < n; i++ {
if entries[i].Duration != defaultDuration {
sameDuration = false
}
if entries[i].Flags != defaultFlags {
sameFlags = false
}
}
firstDiffers = entries[0].Flags != defaultFlags
}
tfhdFlags := uint32(mp4.TfhdDefaultBaseIsMoof | mp4.TfhdDefaultSampleFlagsPresent)
if sameDuration {
tfhdFlags |= mp4.TfhdDefaultSampleDurationPresent
}
trunFlags := uint32(mp4.TrunDataOffsetPresent | mp4.TrunSampleSizePresent)
if !sameDuration {
trunFlags |= mp4.TrunSampleDurationPresent
}
if !sameFlags {
trunFlags |= mp4.TrunSampleFlagsPresent
} else if firstDiffers {
trunFlags |= mp4.TrunFirstSampleFlagsPresent
}
if ti.hasCtts {
trunFlags |= mp4.TrunSampleCompositionTimeOffsetPresent
}
var firstSampleFlags uint32
if n > 0 {
firstSampleFlags = entries[0].Flags
}
mw.StartBox(mp4.TypeTraf)
mw.WriteTfhd(tfhdFlags, ti.trackID, defaultDuration, 0, defaultFlags)
mw.WriteTfdt(uint64(ti.baseDTS))
trunStart := mw.Len()
if patchCount < maxTracks {
patches[patchCount] = trunPatch{
pos: trunStart + 16, // offset to data_offset field
dataStart: trackDataOffsets[g],
}
patchCount++
}
mw.WriteTrun(trunFlags, 0, firstSampleFlags, entries)
mw.EndBox() // traf
}
mw.EndBox() // moof
moofSize := int32(mw.Len())
// Backpatch data_offset fields
moofBytes := mw.Bytes()
for i := 0; i < patchCount; i++ {
p := patches[i]
dataOffset := moofSize + mdatHeaderSize + p.dataStart
binary.BigEndian.PutUint32(moofBytes[p.pos:], uint32(dataOffset))
}
w.moof = moofBytes
w.mdatPayload = mdatPayload
return nil
}
// WriteBodyRange writes the body bytes in [start, end) to dst, reading sample
// bytes from src only for the portion of the window that overlaps the sample
// data. Offsets are relative to the start of the body: the moof occupies
// [0, moofSize), the 8-byte mdat header [moofSize, moofSize+8), and the sample
// data the remainder up to [Writer.BodySize]. It must be called after
// [Writer.Prepare]. start and end must satisfy 0 <= start <= end <= BodySize.
func (w *Writer) WriteBodyRange(dst io.Writer, src io.ReaderAt, start, end int64) error {
moofSize := int64(len(w.moof))
// moof region [0, moofSize)
if start < moofSize {
e := min(end, moofSize)
if _, err := dst.Write(w.moof[start:e]); err != nil {
return err
}
}
// mdat header region [moofSize, moofSize+8)
const mdatHdrSize = 8
dataStart := moofSize + mdatHdrSize
if start < dataStart && end > moofSize {
hdr := w.mdatHdr[:]
binary.BigEndian.PutUint32(hdr[:4], uint32(w.mdatPayload+mdatHdrSize))
copy(hdr[4:], "mdat")
s := max(start, moofSize) - moofSize
e := min(end, dataStart) - moofSize
if _, err := dst.Write(hdr[s:e]); err != nil {
return err
}
}
// sample data region [dataStart, dataStart+mdatPayload)
if end <= dataStart {
return nil
}
return w.writeDataRange(dst, src, max(start, dataStart)-dataStart, end-dataStart)
}
// writeDataRange writes the sample-data bytes in [lo, hi) to dst, where the
// offsets are relative to the start of the concatenated sample data. It maps the
// window onto the source byte ranges recorded by Prepare and reads only the
// overlapping source bytes, in fixed-size chunks through the reused copy buffer.
func (w *Writer) writeDataRange(dst io.Writer, src io.ReaderAt, lo, hi int64) error {
var pos int64 // running start of the current range within the data region
for i := range w.ranges {
rng := &w.ranges[i]
rEnd := pos + rng.size
if rEnd <= lo {
pos = rEnd
continue
}
if pos >= hi {
break
}
from := max(lo, pos)
to := min(hi, rEnd)
off := rng.offset + (from - pos)
remaining := to - from
for remaining > 0 {
n := min(int64(len(w.copyBuf)), remaining)
nr, err := src.ReadAt(w.copyBuf[:n], off)
if err != nil && err != io.EOF {
return err
}
if nr == 0 {
break
}
if _, err := dst.Write(w.copyBuf[:nr]); err != nil {
return err
}
off += int64(nr)
remaining -= int64(nr)
}
pos = rEnd
}
return nil
}
func sampleFlags(isSync bool) uint32 {
if isSync {
return 0x02000000
}
return 0x01010000
}
|