Skip to content

DV P5 colr override hardcodes limited range — make it fill-the-gap only #20

Closed

Description

@superuser404notfound

Follow-up from DrHurt's review in #4 (thought 4).

Context

DV Profile 5 MP4 sources sometimes omit the HEVC SPS VUI color fields and the container colr atom, so AVPlayer's DV decoder won't engage on the dvh1 sample entry. The fix in #4 forces the canonical P5 color tuple via ColorOverride, which makes the muxer write a colr nclx 9/16/9 atom AVPlayer reads as the PQ signal.

The override currently hardcodes limited range unconditionally for every P5 source:

// HLSVideoEngine.swift:618
let p5ColorOverride: MP4SegmentMuxer.ColorOverride? = (dvVariant == .profile5)
    ? MP4SegmentMuxer.ColorOverride(
        primaries: AVCOL_PRI_BT2020,
        trc: AVCOL_TRC_SMPTE2084,
        space: AVCOL_SPC_BT2020_NCL,
        range: AVCOL_RANGE_MPEG   // <-- hardcoded limited
    )
    : nil

Concern (DrHurt)

You've hardcoded it to limited range. Is it worth re-testing with the hvcC patch alone? I'm worried this might cause problems down the line. If playback fails without it, then perhaps make it dynamic.

The risk: a P5 source that is full range, or one that already carries a correct colr atom, gets its range stomped to limited and shifts colours. The override was written for the "everything unspecified" case but applies to all P5 unconditionally.

Proposed fix

Make the override defensive instead of absolute: only force a field when the source codecpar leaves it unspecified.

  • range: force AVCOL_RANGE_MPEG only when codecpar.color_range == AVCOL_RANGE_UNSPECIFIED; otherwise pass the source value through.
  • Same reasoning for primaries / trc / space: only fill the gap, don't overwrite a present signal.

That keeps the #19 repro working (all fields unspecified → canonical P5 tuple) while leaving a properly-signaled or full-range P5 untouched.

What I need to verify it

A full-range P5 MP4 sample (or any P5 MP4 that carries a non-limited / explicitly-signaled colr). The current test corpus only has the unspecified-VUI case, so I can confirm the defensive path doesn't regress #19 but can't confirm it actually fixes a full-range source without a repro.

@DrHurt if you have one in your library, even a short clip would let me close this properly. No rush.

Activity

  1. added
    bug
    help wanted
    on May 30, 2026
  2. superuser404notfound commented on May 30, 2026

    @superuser404notfound
    OwnerAuthor

    Fixed in 654da7f (Sodalite pin bumped in 76c5ecbb).

    The override no longer forces limited range unconditionally. I split the four fields by whether P5 actually allows an alternate:

    • Primaries / transfer / matrix stay forced. P5 is spec-defined as IPT-PQ-c2 (BT.2020 / PQ / BT.2020-NCL). There is no legal alternate, so a source claiming anything else is malformed and forcing the canonical values is a repair, not an overwrite. Passing those through would risk letting a bad colr atom break a well-formed P5.
    • Range is now fill-the-gap. This is the only field with a legitimate alternate (P5 is usually limited, but full-range P5 is legal). The override now reads codecpar.color_range: if the source signals a range it passes through untouched, and AVCOL_RANGE_MPEG is only filled in when the source leaves it AVCOL_RANGE_UNSPECIFIED.
    range: sourceRange == AVCOL_RANGE_UNSPECIFIED
        ? AVCOL_RANGE_MPEG
        : sourceRange

    So the #19 repro (Wandering Earth 2, all color fields unspecified) still resolves to the full canonical P5 tuple, limited range included, exactly as before. A P5 source that already carries range signaling, including a full-range one, keeps it instead of being stomped to limited.

    On your two sub-questions:

    1. "Re-test with the hvcC patch alone" — this build is the test. A properly-signaled P5 now keeps its own range, so if any of your existing P5 files were quietly being forced to limited, you should see correct range on this build. If they all still look right, that confirms the limited default was harmless for your corpus and the gap-fill changes nothing for them.
    2. Full-range correctness — this is the one I still can't verify without a full-range P5 MP4 sample. The code path is correct by construction (source range passes through), but a real source would confirm AVPlayer's DV decoder engages with a full-range colr nclx as expected. If you have one, even a short clip would let me close this with evidence rather than reasoning.

    New build going up shortly.

  3. DrHurt commented on May 30, 2026

    @DrHurt
    Contributor

    I believe the reference file I sent you is full range. If not, I’ll have a look when I get home.

    Unfortunately, I won’t be able to test today though.

  4. superuser404notfound commented on May 30, 2026

    @superuser404notfound
    OwnerAuthor

    Verified end-to-end against your DV5.mkv from #4 — and it turns out this was the right file to ask about. It is full range.

    ffprobe on the source:

    codec_name=hevc / profile=Main 10 / dv_profile=5
    color_range=pc          <- full range
    color_space=unknown
    color_transfer=unknown
    color_primaries=unknown
    

    Your own hand-built working remux (the init.mp4 + media0.m4s you bundled) is color_range=pc too, so the intent was always full range.

    I ran DV5.mkv through aetherctl serve on this build and pulled the engine's init.mp4 back. The colr nclx atom it writes now:

    colr nclx  0009 0010 0980
               prim=9 (BT.2020)  trc=16 (PQ)  matrix=9 (BT.2020-NCL)  + 0x80
    

    That trailing 0x80 is full_range_flag = 1. ffprobe on the engine output confirms color_range=pc, matching your source and your reference byte-for-intent.

    For contrast, the old unconditional AVCOL_RANGE_MPEG would have written 0x00 there (limited) and stomped the full-range signal. So this was a real defect, not just a theoretical one, and it affected MKV as well as MP4 since the override keyed on dvVariant == .profile5 regardless of container.

    Summary:

    color_range colr full_range_flag
    DV5.mkv (source) pc (full) n/a (mkv)
    your working remux pc (full) n/a
    engine, old hardcode forced limited 0x00 (limited)
    engine, this build pc (full) 0x80 (full)

    Leaving this open for your visual confirmation on a DV panel when you get a chance — the colr is byte-correct and matches your known-good remux, so it should engage the DV decode cleanly, but a real on-TV check closes it with eyes rather than hex. No rush, whenever you next test.

  5. DrHurt commented on Jun 1, 2026

    @DrHurt
    Contributor

    Everything looks fine and your fix looks sensible:
    If range is present copy it and if not force limited (being the default for movies & series).

    The question is whether it’s needed to play Wandering Earth 2 and other broken files.

    I have no strong feelings about this either way. This is a solid sensible approach.

    On a slightly different note, I think you misunderstood my comment about DV stripping.

    I meant stripping with DV 8.x & 10.x on HDR10-only panels.

    DV7 will always need stripping (or maybe conversion in the future)

    I’m curious to know if DV 8.1 will just tone map using media playlist after the recent fixes instead of removing the dvcC for HDR10-only panels.

    Your current approach is functional, but I’m just trying to re-use the same pipeline on all panels, just vary playlist.

  6. superuser404notfound commented on Jun 1, 2026

    @superuser404notfound
    OwnerAuthor

    Thanks, glad the P5 fix holds up.

    On whether the range is needed for Wandering Earth 2: it isn't, strictly. What engages AVPlayer's DV decoder on those broken files is the dvh1 sample entry plus the presence of a colr atom, not the range value inside it. So range is about color correctness, not about getting a picture. And since WE2 has range unspecified, the new fill-the-gap logic still resolves it to limited exactly as before, so nothing about that repro changes. The range field only starts mattering the day a properly signaled full-range P5 shows up, which is what your DV5.mkv finally confirmed.

    On the DV stripping point, thanks for the correction, that reframes it usefully.

    First, to set expectations: the recent fixes (P5 colr range here, the hvcC rebuild in #4/#19) were all P5 specific. They did not touch the 8.x routing at all. So today, 8.1 on an HDR10-only panel still strips the DV side data and plays the HDR10-compatible base layer as plain hvc1.2.4 PQ. Nothing about that changed under you.

    Now, your actual question: could 8.1 keep its dvcC and route through the media playlist like P5 does, letting the system DV decoder engage and tone-map, so the muxing is identical on every panel and only the playlist varies?

    It is not ruled out the way you might expect. The failure that forced the strip was tvOS 26's master-level codec filter rejecting a dvvC in the sample entry with -11868, even when the master CODECS attribute was plain hvc1.2.4. That is a master-playlist problem. The media playlist has no master-level CODECS attribute, which is exactly why the P5 media-playlist path sidesteps it. So a media-playlist 8.1 with dvcC retained is at least plausible on paper.

    The reason it is split today is a quality argument rather than inertia. P8.1 has a genuine HDR10-compatible base layer, so stripping and playing that base is native HDR10: lossless, no tone-mapping in the path. The P5 media trick exists out of necessity, because P5 has no base layer and the DV decoder is the only way to get any picture at all. Pushing 8.1 through the DV decoder to tone-map would be strictly more work for, at best, no gain, and possibly a regression if the decoder tone-maps the full DV grade down instead of just presenting the already-correct HDR10 base.

    8.4 is a separate case again: the base is HLG, and a bare dvh1 plus HLG transfer was rejected by AVPlayer outright back in #4 Build 160, so it cannot follow the P5 shape regardless. AV1 DV (10.x) currently skips DV routing entirely on non-DV panels.

    So I would frame it as: the unified pipeline is buildable for 8.1 specifically, but it trades a proven lossless native-HDR10 path for an unproven tone-map path. If you want to settle it with eyes rather than reasoning, the clean test is an A/B on your HDR10 panel: the current strip-to-base build versus a media-playlist build that keeps dvcC, same 8.1 file. If they are visually identical, unification is a free simplification and I will take it. If the tone-map path crushes highlights or shifts anything, the split stays justified. I can put up that experimental build whenever you have time to look.

  7. DrHurt commented on Jun 1, 2026

    @DrHurt
    Contributor

    The recent change I was referring to was the change of the bitstream codec tag (ffmpeg -vtag) from dvh1 to hvc1 for DV 8.1 like we do with DV 8.4.

    The bitstream tag is now hvc1 and the master codecs is now hvc1 for both profiles (which is exactly what Apples HLS authoring docs recommend)

    Both P8 profiles currently signal DV exclusively via dvcC + supplemental codecs. The rest remains hvc1.

    I don’t know but I would like to think AVPlayer tone mapping pipeline is smart enough to just use the HDR/HLG layer without touching the DV stuff if we use the media playlist path.

    Also if you haven’t tried with master + hvc1 bitstream tag, might be worth a re-test. The whole point of HLS codecs + supplemental codecs is to allow fallback (would be an Apple bug if it doesn’t fall back).

    I wonder if your previous testing used dvh1 tagged mp4 segments with profile 8.1

    Again, current approach is solid but I’m thinking out loud & learning as we go.

    Also feel free to close this now.

  8. superuser404notfound commented on Jun 1, 2026

    @superuser404notfound
    OwnerAuthor

    Good thread, and you're right to push on it. One correction first that actually helps your case:

    We already tag P8.1 segments hvc1, not dvh1. The master CODECS is already hvc1.2.4.L<level>, and DV rides only on the dvvC box plus SUPPLEMENTAL-CODECS="dvh1.08.<lvl>/db1p", gated on a DV-capable panel. So the bitstream-tag change you're describing is already in place, and it matches the Apple authoring shape you cited. That also answers your last question: the -11868 test was never dvh1-tagged P8.1. It was hvc1-tagged with the dvvC box retained.

    What actually failed, both on the master playlist on an HDR10-capable-but-non-DV panel (match dynamic range ON, panel went to HDR10 mode):

    1. CODECS hvc1.2.4 + SUPPLEMENTAL-CODECS=dvh1.08/db1p + dvvC in init.mp4: the DV path engaged and then silently failed instead of falling back. That's the regression we later fixed by gating SUPPLEMENTAL on DV-capable panels only.
    2. SUPPLEMENTAL dropped, plain hvc1.2.4 master, but dvvC still in init.mp4: still -11868.

    So the HLS fallback you'd expect from codecs + supplemental-codecs did not happen on tvOS 26.5 here. Whether that's an Apple bug or our packaging not being byte-exact, the empirical result is that the master-level filter rejects a retained dvvC on a non-DV panel, which is why we strip.

    The part that lines up with your instinct: the one configuration we have NOT tested for P8.1 is the P5 shape. A media playlist has no master-level CODECS attribute, so there's no master-level filter to trip. That's exactly why the P5 media path works on every panel. A media-playlist P8.1 that keeps its dvvC (no SUPPLEMENTAL, since media playlists have no such attribute) is therefore plausible on paper, and it's the only path that could give you the unified pipeline you want.

    That said, the current split works and ships correctly on every panel, and the strip path has a real upside: P8.1 has a genuine HDR10-compatible base, so stripping and playing that base is native, lossless HDR10 with no tonemap in the path. Routing through the DV decoder to tonemap is strictly more work and a possible regression if it grades the full DV down instead of presenting the already-correct base. So I'm going to leave it as is rather than chase a unification that, at best, breaks even.

    If you do want to settle the media-playlist P8.1 path with eyes on your HDR10 panel, open a fresh issue and I'll put up an A/B build (current strip-to-base vs P5-shape media playlist, same file) so we can compare them side by side.

    Closing this one out, since the P5 range fix is settled and verified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions