Skip to main content

Module mp4_sanitize

Module mp4_sanitize 

Source
Expand description

Lenient pre-pass for the strict mp4 crate.

ISOBMFF box headers carry a size field that COULD be wrong — malformed encoders (older Apple QuickTime, some prosumer cameras, anything that round-trips through a buggy mux) can emit child boxes whose advertised size exceeds the parent’s remaining payload. The mp4 0.14 crate (and most strict ISOBMFF parsers) bail with "<parent> box contains a box with a larger size than it" and the whole demux fails.

sanitize_isobmff_box_sizes walks the box tree from the root, and any time a child’s advertised size would exceed the parent’s remaining payload, rewrites the child’s size field to fit. The output bytes are byte-compatible with strict parsers and (in the common case where the child’s size was a benign over-report by 1-N bytes) preserve everything that the parser actually reads.

The function is a no-op on every well-formed file — every box header is left untouched, so a clean MP4 hashes identically through this function. Only malformed files mutate.

What this DOES handle:

  • Top-level container boxes: ftyp, moov, mdat, etc.
  • Recursive containers: moov > trak > mdia > minf > stbl > stsd > {mp4a, av01, hvc1, …}.
  • 64-bit largesize extended-size form.

What this does NOT handle:

  • size = 0 “extends to end of file” — left untouched (strict parsers handle this correctly).
  • Box trees with byte-level corruption inside a leaf box’s payload (e.g. a malformed esds descriptor). The sanitizer only touches the box header bytes; leaf payload is opaque.

Functions§

sanitize_isobmff_box_sizes