Skip to main content

Module cmaf

Module cmaf 

Source
Expand description

Fragmented MP4 / CMAF box writers.

Produces ISO/IEC 14496-12 §8.8 movie-fragment boxes (moof / mfhd / traf / tfhd / tfdt / trun) and the corresponding mvex / mehd / trex declarations that go inside a CMAF init segment’s moov. CMAF (ISO/IEC 23000-19) constrains the general 14496-12 model: exactly one track per fragment (one traf per moof), exactly one track per init segment, and a small set of mandatory boxes.

This module is the box-level primitive layer. Higher-level callers (init_segment_video, media_segment_video, etc. in subsequent commits) compose these into init + media segments. The split lets us unit-test each box’s byte layout against the spec without having to drive a full encode + segment pipeline.

Spec citations are given by section number in the relevant box’s doc comment so future readers can cross-check against the standard.

§CMAF brand

Init segments for video tracks declare the cmfc brand (CMAF constraints, per CMAF §7.3.4). Audio tracks use cmfa. Both brands coexist in compatible_brands alongside the existing iso6 / mp42 / av01 brands so non-CMAF-aware tools that consume the same boxes (e.g. an old ffprobe) can still demux them.

§Sample-flags packing

default_sample_flags (in trex / tfhd) and first_sample_flags / per-sample flags (in trun) are packed per ISO/IEC 14496-12 §8.8.3.1. The 32 bits are laid out:

  reserved[6]      = 0
  is_leading[2]    = 0
  sample_depends_on[2]
  sample_is_depended_on[2]
  sample_has_redundancy[2]
  sample_padding_value[3] = 0
  sample_is_non_sync_sample[1]
  sample_degradation_priority[16] = 0

For AV1 / AAC the meaningful values are sample_depends_on = 1 (this sample depends on others — i.e. P / B / non-IDR) or 2 (independent — i.e. IDR / sync), and sample_is_non_sync_sample = 1 for non-key frames, 0 for keyframes. The helper SampleFlags::pack handles this; callers shouldn’t compose the u32 by hand.

Modules§

brand
CMAF brand identifiers used in ftyp.compatible_brands.

Structs§

CmafAudioMuxer
Stateful CMAF audio segmenter. Same model as the video muxer but simpler — every audio sample is independently decodable, so there’s no first-sample-flags / sync-boundary requirement.
CmafSample
Per-sample fields written into trun. Each entry produces one row of (duration, size, flags) in the fragment’s sample table.
CmafTrackManifest
Output of a finalized track muxer: where the init segment lives, the ordered list of media segments, and the timescale needed to convert duration_ticks to seconds.
CmafVideoMuxer
Stateful CMAF video segmenter for one AV1 rendition.
CmafVideoMuxerOptions
Optional construction parameters for CmafVideoMuxer. Defaults match the original 5-arg new() behaviour: write init.mp4, start segment numbering at 1, decode-time at 0.
MoofData
Full moof blob with the inner trun.data_offset patched up.
SampleFlags
Sample flags as packed in default_sample_flags / first_sample_flags / per-sample sample_flags in trun. ISO/IEC 14496-12 §8.8.3.1.
SegmentInfo
Per-segment metadata returned by CmafVideoMuxer::flush_segment / CmafAudioMuxer::flush_segment. These records form the input to the HLS playlist writer (Phase 3) and the segment-alignment validator (Phase 5).

Enums§

CmafTrackKind
Track type discriminator. CMAF places one track per init / fragment; this enum is what higher-level orchestration uses to pick which codec dispatch to take. The init / segment writers themselves don’t take this enum (they have type-specific entry points), so it stays #[allow(dead_code)] until the pipeline orchestrator (Phase 4) wires it through.

Functions§

build_init_segment_audio
Build a CMAF audio init segment.
build_init_segment_video
Build a CMAF video init segment for an AV1 track.
build_mehd
mehd — Movie Extends Header (14496-12 §8.8.2).
build_mfhd
mfhd — Movie Fragment Header (14496-12 §8.8.5).
build_moof_audio
Build an audio moof. Same composition as video but without first-sample-flags differentiation in trun (every audio sample is independently decodable).
build_moof_video
Build a video moof for one CMAF fragment.
build_mvex
mvex — Movie Extends container (14496-12 §8.8.1).
build_tfdt
tfdt — Track Fragment Decode Time (14496-12 §8.8.12).
build_tfhd
tfhd — Track Fragment Header (14496-12 §8.8.7).
build_trex
trex — Track Extends (14496-12 §8.8.3).