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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! Muxer configuration for the MP4 / ISOBMFF writer.
//!
//! The default [`Mp4MuxerOptions`] matches what `muxer::open` has always done:
//! major brand `mp42`, no faststart, no fragmentation. Three convenience
//! presets are provided via [`BrandPreset`] for the common `mp4`, `mov`, and
//! `ismv` registry entries; a `Custom` variant lets callers supply any
//! major + compatible brand list directly.
//!
//! Setting [`Mp4MuxerOptions::fragmented`] to `Some(...)` switches the muxer
//! into fragmented-MP4 mode (DASH / HLS / Smooth-Streaming / CMAF output).
/// Brand preset controlling the `ftyp` box written at the start of the file.
///
/// The four-byte codes follow ISO/IEC 14496-12 and the de-facto QuickTime /
/// Smooth Streaming conventions:
///
/// * [`Mp4`](BrandPreset::Mp4): `mp42` / `isom mp42 mp41 iso2`
/// * [`Mov`](BrandPreset::Mov): `qt ` / `qt `
/// * [`Ismv`](BrandPreset::Ismv): `iso4` / `iso4 piff iso6 isml`
/// * [`Custom`](BrandPreset::Custom): caller-supplied major + compatible list
/// Cadence policy controlling when the fragmented muxer emits a `moof+mdat`
/// pair (one segment / fragment per flush).
///
/// In a true CMAF / DASH `init+seg*` workflow each `moof+mdat` becomes one
/// addressable HTTP range; the cadence picks how big each one is.
/// Fragmented-MP4 muxer options.
///
/// When [`Mp4MuxerOptions::fragmented`] is `Some(FragmentedOptions { .. })`,
/// the muxer writes the file as
///
/// ```text
/// ftyp
/// moov (mvex+trex; no media samples in moov)
/// sidx? (one per fragment, references the next moof+mdat)
/// styp? + moof + mdat (per fragment, repeated)
/// sidx? + styp? + moof + mdat
/// ...
/// mfra? (at end: per-track tfra + mfro size trailer)
/// ```
///
/// matching ISO/IEC 14496-12 §8.8 (Movie Fragments) + §8.16 (sidx) + §8.8.10
/// (mfra) + DASH-IF Interop guidelines for `styp` brands.
/// Per-track sample-group emission request.
///
/// Each entry attaches an `sbgp` (SampleToGroupBox), `csgp`
/// (CompactSampleToGroupBox) and / or `sgpd` (SampleGroupDescriptionBox)
/// box into one track's `stbl`. The halves share `grouping_type`; the
/// writer simply serialises whatever the caller supplies — content
/// interpretation belongs to a layer that knows the grouping-type
/// semantics (per ISO/IEC 14496-12 §8.9).
///
/// Multiple `TrackSampleGroups` entries may target the same
/// `stream_index`; they accumulate in encounter order. The muxer
/// emits all `sgpd` boxes first, then all `sbgp` boxes, then all
/// `csgp` boxes, after the chunk-offset table inside each track's
/// `stbl`. `sbgp` and `csgp` are *alternative* encodings of the same
/// per-sample → group mapping (§8.9.5: "at most one `csgp` *or* `sbgp`
/// with a given `grouping_type` may exist per track"); a caller picks
/// one form per `grouping_type` and never both.
/// An explicit per-track edit list for the muxer (ISO/IEC 14496-12
/// §8.6.5–6).
///
/// When a [`Mp4MuxerOptions::track_edit_lists`] entry targets a
/// stream, the muxer emits that track's `edts/elst` from the given
/// entries verbatim (serialised through `demux::build_elst_box`, so
/// the §8.6.6.3 round-trip rules apply — `media_rate_integer` must be
/// 0 or 1, the final entry may not be an empty edit, `media_time`
/// may not sit below the `-1` empty-edit sentinel, and the entry list
/// may not be empty; violations fail at `open` time). An explicit
/// list *overrides* the automatic start-delay emission for that track
/// and is written even when [`Mp4MuxerOptions::write_edit_list`] is
/// `false` (the flag governs only the automatic behaviour).
///
/// This is the write-side dual of `Mp4Demuxer::edit_list`: a remuxer
/// carries a source's elst across by feeding the demuxer's slice
/// straight back in. Note the §8.6.6.3 unit split — each entry's
/// `segment_duration` is in the *movie* timescale (this muxer writes
/// movie timescale 1000) while `media_time` is in the track's media
/// timescale.
/// Per-track CENC protection signalling for the muxer (ISO/IEC
/// 14496-12 §8.12 envelope + ISO/IEC 23001-7 §4.1 carriage).
///
/// When a [`Mp4MuxerOptions::track_protection`] entry targets a
/// stream, the muxer wraps that track's sample entry into its
/// protected form: the FourCC becomes `encv` / `enca` / `enct` /
/// `encs` (per the stream's media type) and a `sinf` box —
/// `frma(original_format)` + `schm(scheme_type, scheme_version)` +
/// `schi(tenc)` — is appended to the entry body, exactly the shape
/// this crate's demuxer unwraps back to the original codec id plus
/// `protection_scheme` / `cenc_default_*` options.
///
/// The muxer signals protection only — packet payloads are written
/// as handed in. The caller encrypts each sample first (e.g. via
/// `cenc_cipher::encrypt_sample_in_place` with a
/// `CencSchemeDecision` built from this same `(scheme_type, tenc)`
/// pair) and carries the per-sample IVs / subsample maps through its
/// own `senc` / `saiz` / `saio` channel.
/// Runtime options controlling how the MP4 muxer shapes its output.
///
/// Call [`Mp4MuxerOptions::default`] for the historical behavior of the
/// plain `"mp4"` registry entry (major=`mp42`, no faststart, no fragmentation).