Skip to content

Commit af8cafd

Browse files
Vincent Herbstclaude
andcommitted
fix(dv5): force canonical color signaling on Profile 5 mp4 output
Some DV Profile 5 MP4 encoders write a `dvh1` sample entry and a well-formed `dvcC` record but omit the HEVC SPS VUI transfer fields and the container `colr` atom (issue #19 Wandering Earth 2 WEB-DL: codec_tag=dvh1, dv_profile=5 L6 compat_id=0, color_transfer=unknown, color_primaries=unknown, color_space=unknown, no nclx). AVPlayer refuses to engage the DV decoder on a `dvh1` sample entry without an explicit PQ signal, so the engine's output fMP4 (which preserved the gap) failed at item.status. The same content as MKV plays cleanly because matroska's Colour element gives FFmpeg explicit codecpar color fields that the mp4 muxer writes as a colr nclx atom; the mp4 demuxer has no equivalent fallback. Force the canonical P5 color tuple (BT.2020 / PQ / BT.2020-NCL / limited range) on the muxer's stream codecpar before write_header. P5 is defined as IPT-PQ-c2; the dvcC record alone implies that, so the override has no risk of misclassifying a non-PQ source as PQ. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 7aedad7 commit af8cafd

3 files changed

Lines changed: 72 additions & 5 deletions

File tree

‎Sources/AetherEngine/Video/HLSSegmentProducer.swift‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,22 @@ final class HLSSegmentProducer: @unchecked Sendable {
5959
/// HEVC HDR10 (P7 today). See
6060
/// `MP4SegmentMuxer.VideoConfig.stripDolbyVisionMetadata`.
6161
let stripDolbyVisionMetadata: Bool
62+
/// Optional color-signaling override forwarded to the muxer.
63+
/// See `MP4SegmentMuxer.ColorOverride`.
64+
let colorOverride: MP4SegmentMuxer.ColorOverride?
6265

6366
init(
6467
codecpar: UnsafePointer<AVCodecParameters>,
6568
timeBase: AVRational,
6669
codecTagOverride: String?,
67-
stripDolbyVisionMetadata: Bool = false
70+
stripDolbyVisionMetadata: Bool = false,
71+
colorOverride: MP4SegmentMuxer.ColorOverride? = nil
6872
) {
6973
self.codecpar = codecpar
7074
self.timeBase = timeBase
7175
self.codecTagOverride = codecTagOverride
7276
self.stripDolbyVisionMetadata = stripDolbyVisionMetadata
77+
self.colorOverride = colorOverride
7378
}
7479
}
7580

@@ -560,7 +565,8 @@ final class HLSSegmentProducer: @unchecked Sendable {
560565
codecpar: videoConfig.codecpar,
561566
timeBase: videoConfig.timeBase,
562567
codecTagOverride: videoConfig.codecTagOverride,
563-
stripDolbyVisionMetadata: videoConfig.stripDolbyVisionMetadata
568+
stripDolbyVisionMetadata: videoConfig.stripDolbyVisionMetadata,
569+
colorOverride: videoConfig.colorOverride
564570
)
565571
let muxerAudio: MP4SegmentMuxer.AudioConfig? = audioConfig.map { a in
566572
MP4SegmentMuxer.AudioConfig(codecpar: a.codecpar, timeBase: a.inputTimeBase)

‎Sources/AetherEngine/Video/HLSVideoEngine.swift‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,11 +575,37 @@ public final class HLSVideoEngine: @unchecked Sendable {
575575
let segmentCache = SegmentCache()
576576
self.cache = segmentCache
577577

578+
// DV Profile 5 is defined as IPT-PQ-c2 (BT.2020 primaries, PQ
579+
// transfer, BT.2020-NCL matrix, limited range). The `dvcC`
580+
// record implies that signaling, but some P5 MP4 encoders
581+
// omit the HEVC SPS VUI fields and the container `colr` atom
582+
// (Wandering Earth 2 WEB-DL 2026-05-28 issue #19: dvh1
583+
// sample entry + dvcC P5 L6 present, but color_trc /
584+
// color_primaries / color_space all unspecified, no nclx).
585+
// Without an explicit transfer signal on the output fMP4,
586+
// AVPlayer's DV decoder won't engage on the dvh1 sample
587+
// entry (item.status .failed) even though the elementary
588+
// stream is well-formed P5. The matroska demuxer reads the
589+
// Colour element directly into codecpar.color_* so the same
590+
// content as MKV plays cleanly; the mp4 demuxer has no
591+
// equivalent fallback. Forcing the canonical P5 color tuple
592+
// here makes the muxer write a `colr nclx 9/16/9` atom that
593+
// AVPlayer reads as the missing PQ signal. Safe to overwrite:
594+
// P5 has no legal alternate color signaling.
595+
let p5ColorOverride: MP4SegmentMuxer.ColorOverride? = (dvVariant == .profile5)
596+
? MP4SegmentMuxer.ColorOverride(
597+
primaries: AVCOL_PRI_BT2020,
598+
trc: AVCOL_TRC_SMPTE2084,
599+
space: AVCOL_SPC_BT2020_NCL,
600+
range: AVCOL_RANGE_MPEG
601+
)
602+
: nil
578603
let videoConfig = HLSSegmentProducer.StreamConfig(
579604
codecpar: codecpar,
580605
timeBase: videoTimeBase,
581606
codecTagOverride: codecTagOverride,
582-
stripDolbyVisionMetadata: stripDolbyVisionMetadata
607+
stripDolbyVisionMetadata: stripDolbyVisionMetadata,
608+
colorOverride: p5ColorOverride
583609
)
584610
self.videoStreamIndex = videoIndex
585611
self.savedVideoConfig = videoConfig
@@ -1324,7 +1350,8 @@ public final class HLSVideoEngine: @unchecked Sendable {
13241350
codecpar: vcfg.codecpar,
13251351
timeBase: vcfg.timeBase,
13261352
codecTagOverride: vcfg.codecTagOverride,
1327-
stripDolbyVisionMetadata: vcfg.stripDolbyVisionMetadata
1353+
stripDolbyVisionMetadata: vcfg.stripDolbyVisionMetadata,
1354+
colorOverride: vcfg.colorOverride
13281355
)
13291356
let probeAudio = MP4SegmentMuxer.AudioConfig(
13301357
codecpar: cfg.codecpar,

‎Sources/AetherEngine/Video/MP4SegmentMuxer.swift‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,24 @@ final class MP4SegmentMuxer {
107107

108108
// MARK: - Types
109109

110+
/// Force color signaling fields on the output stream's codecpar
111+
/// after `avcodec_parameters_copy` and before `avformat_write_header`.
112+
/// Used for Dolby Vision Profile 5 sources whose SPS VUI omits the
113+
/// transfer characteristic and whose MP4 container has no `colr`
114+
/// atom: P5 is defined as IPT-PQ-c2 (BT.2020 / PQ / BT.2020-NCL,
115+
/// limited range), and the `dvcC` record alone implies that, but
116+
/// AVPlayer's DV decoder won't engage on a `dvh1` sample entry
117+
/// without an explicit `colr nclx` atom or PQ VUI. Setting these
118+
/// on the muxer's stream codecpar causes the mp4 muxer to write a
119+
/// `colr nclx 9/16/9` atom that AVPlayer reads as the canonical
120+
/// PQ signal.
121+
struct ColorOverride {
122+
let primaries: AVColorPrimaries
123+
let trc: AVColorTransferCharacteristic
124+
let space: AVColorSpace
125+
let range: AVColorRange
126+
}
127+
110128
struct VideoConfig {
111129
let codecpar: UnsafePointer<AVCodecParameters>
112130
let timeBase: AVRational
@@ -124,17 +142,21 @@ final class MP4SegmentMuxer {
124142
/// because the dvcC advertises a DV profile the dvh1-less
125143
/// sample entry contradicts.
126144
let stripDolbyVisionMetadata: Bool
145+
/// Optional color-signaling override. See `ColorOverride`.
146+
let colorOverride: ColorOverride?
127147

128148
init(
129149
codecpar: UnsafePointer<AVCodecParameters>,
130150
timeBase: AVRational,
131151
codecTagOverride: String?,
132-
stripDolbyVisionMetadata: Bool = false
152+
stripDolbyVisionMetadata: Bool = false,
153+
colorOverride: ColorOverride? = nil
133154
) {
134155
self.codecpar = codecpar
135156
self.timeBase = timeBase
136157
self.codecTagOverride = codecTagOverride
137158
self.stripDolbyVisionMetadata = stripDolbyVisionMetadata
159+
self.colorOverride = colorOverride
138160
}
139161
}
140162

@@ -349,6 +371,12 @@ final class MP4SegmentMuxer {
349371
if video.stripDolbyVisionMetadata {
350372
Self.stripDolbyVisionSideData(videoStream.pointee.codecpar)
351373
}
374+
if let co = video.colorOverride {
375+
videoStream.pointee.codecpar.pointee.color_primaries = co.primaries
376+
videoStream.pointee.codecpar.pointee.color_trc = co.trc
377+
videoStream.pointee.codecpar.pointee.color_space = co.space
378+
videoStream.pointee.codecpar.pointee.color_range = co.range
379+
}
352380

353381
// Audio stream (optional).
354382
if let audio = audio {
@@ -487,6 +515,12 @@ final class MP4SegmentMuxer {
487515
if video.stripDolbyVisionMetadata {
488516
Self.stripDolbyVisionSideData(videoStream.pointee.codecpar)
489517
}
518+
if let co = video.colorOverride {
519+
videoStream.pointee.codecpar.pointee.color_primaries = co.primaries
520+
videoStream.pointee.codecpar.pointee.color_trc = co.trc
521+
videoStream.pointee.codecpar.pointee.color_space = co.space
522+
videoStream.pointee.codecpar.pointee.color_range = co.range
523+
}
490524

491525
// Audio stream (optional).
492526
if let audio = audio {

0 commit comments

Comments
 (0)