forked from kenzuya/unshackle
feat(dash): flag dv profile 8 tracks from supplementalCodecs and use hdr10+dv as the hybrid
This commit is contained in:
@@ -1752,7 +1752,7 @@ def serialize_video_track(track: Video, include_url: bool = False) -> Dict[str,
|
||||
"resolution": f"{track.width}x{track.height}" if track.width and track.height else None,
|
||||
"fps": track.fps if track.fps else None,
|
||||
"range": range_name,
|
||||
"range_display": DYNAMIC_RANGE_MAP.get(range_name, range_name) + ("+HDR10P" if track.hdr10plus else ""),
|
||||
"range_display": DYNAMIC_RANGE_MAP.get(track.range_label, track.range_label),
|
||||
"scan_type": enum_name(track.scan_type) if track.scan_type else None,
|
||||
"closed_captions": list(getattr(track, "closed_captions", None) or []),
|
||||
"dv_compatible_bitstream": bool(getattr(track, "dv_compatible_bitstream", False)),
|
||||
|
||||
@@ -230,6 +230,7 @@ class DASH:
|
||||
# mimeType likely specifies the subtitle codec better than `codecs`
|
||||
codecs = mime_type.split("/")[1]
|
||||
|
||||
video_args: dict[str, Any] = {}
|
||||
if content_type == "video":
|
||||
track_type = Video
|
||||
track_codec = Video.Codec.from_codecs(codecs)
|
||||
@@ -252,6 +253,9 @@ class DASH:
|
||||
fps=track_fps or None,
|
||||
scan_type=scan_type,
|
||||
)
|
||||
video_args["dv_compatible_bitstream"] = Video.is_dv_composite(
|
||||
track_codec, codecs, get("supplementalCodecs")
|
||||
)
|
||||
elif content_type == "audio":
|
||||
track_type = Audio
|
||||
track_codec = Audio.Codec.from_codecs(codecs)
|
||||
@@ -326,6 +330,7 @@ class DASH:
|
||||
}
|
||||
},
|
||||
**track_args,
|
||||
**video_args,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -1161,11 +1166,7 @@ class DASH:
|
||||
def get_video_range(
|
||||
codecs: str, all_supplemental_props: list[Element], all_essential_props: list[Element]
|
||||
) -> Video.Range:
|
||||
# TODO: Detect Dolby Vision composite streams in DASH manifests (DV RPU embedded but
|
||||
# primary codec is plain hvc1, signaled via a separate AdaptationSet/Representation
|
||||
# with DV codec strings or DolbyVisionConfigurationBox). When found, mark the track
|
||||
# with dv_compatible_bitstream=True so DVFixup runs pre-mux. No DASH samples seen yet.
|
||||
if codecs.startswith(("dva1", "dvav", "dvhe", "dvh1")):
|
||||
if Video.has_dv_codec(codecs):
|
||||
return Video.Range.DV
|
||||
|
||||
return Video.Range.from_cicp(
|
||||
|
||||
@@ -222,28 +222,19 @@ class HLS:
|
||||
tracks = Tracks()
|
||||
|
||||
supplemental_codecs = self.supplemental_codecs_by_uri()
|
||||
dv_supp_prefixes = ("dva1", "dvav", "dvhe", "dvh1")
|
||||
|
||||
for playlist in self.manifest.playlists:
|
||||
video_codec: Optional[Video.Codec] = None
|
||||
try:
|
||||
# TODO: Any better way to figure out the primary track type?
|
||||
if playlist.stream_info.codecs:
|
||||
Video.Codec.from_codecs(playlist.stream_info.codecs)
|
||||
video_codec = Video.Codec.from_codecs(playlist.stream_info.codecs)
|
||||
except ValueError:
|
||||
primary_track_type = Audio
|
||||
else:
|
||||
primary_track_type = Video
|
||||
|
||||
primary_codecs = (playlist.stream_info.codecs or "").lower()
|
||||
primary_has_dv = any(codec.split(".")[0] in dv_supp_prefixes for codec in primary_codecs.split(","))
|
||||
|
||||
supp_codecs_str = supplemental_codecs.get(playlist.uri, "")
|
||||
supp_dv_codec: Optional[str] = None
|
||||
for codec in supp_codecs_str.lower().split(","):
|
||||
token = codec.strip().split("/")[0]
|
||||
if token.split(".")[0] in dv_supp_prefixes:
|
||||
supp_dv_codec = token
|
||||
break
|
||||
primary_has_dv = Video.has_dv_codec(playlist.stream_info.codecs)
|
||||
|
||||
video_range = (
|
||||
Video.Range.DV if primary_has_dv else Video.Range.from_m3u_range_tag(playlist.stream_info.video_range)
|
||||
@@ -252,7 +243,9 @@ class HLS:
|
||||
# a DV codec. Range stays whatever VIDEO-RANGE signaled (HDR10/HLG/SDR); DVFixup will
|
||||
# restore DV signaling post-download. HDR10+ SEI in an HDR10 track is found later by the
|
||||
# core HDR10+ probe.
|
||||
dv_compatible_bitstream = primary_track_type is Video and not primary_has_dv and supp_dv_codec is not None
|
||||
dv_compatible_bitstream = primary_track_type is Video and Video.is_dv_composite(
|
||||
video_codec, playlist.stream_info.codecs, supplemental_codecs.get(playlist.uri)
|
||||
)
|
||||
|
||||
tracks.add(
|
||||
primary_track_type(
|
||||
@@ -293,7 +286,9 @@ class HLS:
|
||||
joc = 0
|
||||
if media.type == "AUDIO":
|
||||
track_type = Audio
|
||||
codec = HLS.select_audio_codec(audio_codecs_by_group_id.get(media.group_id), media.uri)
|
||||
codec: Union[Audio.Codec, Subtitle.Codec, None] = HLS.select_audio_codec(
|
||||
audio_codecs_by_group_id.get(media.group_id), media.uri
|
||||
)
|
||||
if media.channels and media.channels.endswith("/JOC"):
|
||||
joc = int(media.channels.split("/JOC")[0])
|
||||
media.channels = "5.1"
|
||||
@@ -354,11 +349,11 @@ class HLS:
|
||||
try:
|
||||
probe = HLS.probe_ts_info(video.url, self.session)
|
||||
if probe:
|
||||
width, height, codec = probe
|
||||
width, height, probed_codec = probe
|
||||
if not has_resolution:
|
||||
video.width, video.height = width, height
|
||||
if not has_codec:
|
||||
video.codec = codec
|
||||
video.codec = probed_codec
|
||||
# best-effort probe over network + ffprobe; the track stays usable without it
|
||||
except Exception as e:
|
||||
logging.getLogger("HLS").debug(f"TS probe failed for {video.url}: {e!r}")
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
"""
|
||||
DV exposure for HLS composite HEVC streams.
|
||||
DV exposure for HLS and DASH composite HEVC streams.
|
||||
|
||||
Some services deliver DV Profile 8.1 in a track whose primary CODECS is plain
|
||||
hvc1, with DV advertised only in SUPPLEMENTAL-CODECS. The fMP4 carries valid DV RPU NALs
|
||||
Some services deliver DV Profile 8.1 in a track whose primary codec is plain
|
||||
hvc1, with DV advertised only in HLS SUPPLEMENTAL-CODECS or DASH @supplementalCodecs. The fMP4 carries valid DV RPU NALs
|
||||
but the container does not signal DV, so muxing the MP4 directly produces an MKV that
|
||||
MediaInfo and DV-capable TVs see as plain HDR10/HDR10+.
|
||||
|
||||
|
||||
@@ -445,20 +445,27 @@ class Tracks:
|
||||
if video not in non_hybrid_selected and not video.native_hybrid:
|
||||
video.hybrid_base_only = True
|
||||
|
||||
def select_hybrid(self, tracks, quality, worst: bool = False):
|
||||
def select_hybrid(self, tracks, quality, worst: bool = False, generate_dv: bool = False):
|
||||
"""A filter for the hybrid ingredients of each resolution. It sets ``native_hybrid`` on every track.
|
||||
|
||||
A DV track that carries HDR10+ is already a hybrid, so its resolution takes that track and no base.
|
||||
A track that already carries DV and an HDR10 or HDR10+ base (DV+HDR10P, HDR10+DV, HDR10P+DV) is a finished
|
||||
hybrid, so its resolution takes that track and no base. One that carries HDR10+ comes first. The other
|
||||
resolutions get a base only when a DV track can give it an RPU, or with ``generate_dv``.
|
||||
"""
|
||||
pick = min if worst else max
|
||||
natives = {}
|
||||
for res in quality:
|
||||
candidates = [v for v in tracks if v.range == Video.Range.DV and v.hdr10plus and at_resolution(v, res)]
|
||||
candidates = [v for v in tracks if v.finished_hybrid and at_resolution(v, res)]
|
||||
if candidates:
|
||||
natives[res] = pick(candidates, key=lambda v: v.bitrate or 0)
|
||||
with_hdr10plus = [v for v in candidates if v.carries_hdr10plus]
|
||||
natives[res] = pick(with_hdr10plus or candidates, key=lambda v: v.bitrate or 0)
|
||||
for video in tracks:
|
||||
video.native_hybrid = any(video is v for v in natives.values())
|
||||
quality = [res for res in quality if res not in natives]
|
||||
dv_tracks = [v for v in tracks if v.range == Video.Range.DV]
|
||||
if natives and not dv_tracks and not generate_dv:
|
||||
quality = [] # no DV track to take an RPU from
|
||||
else:
|
||||
quality = [res for res in quality if res not in natives]
|
||||
|
||||
# Prefer HDR10+ over HDR10 as the base layer (preserves dynamic metadata).
|
||||
# Only an HEVC base qualifies, because dovi_tool injects the RPU into an HEVC stream.
|
||||
@@ -482,7 +489,6 @@ class Tracks:
|
||||
chosen = pick(candidates, key=lambda v: v.bitrate)
|
||||
base_selected.append(chosen)
|
||||
|
||||
dv_tracks = [v for v in tracks if v.range == Video.Range.DV]
|
||||
lowest_dv = min(dv_tracks, key=lambda v: v.height) if dv_tracks and (base_selected or not natives) else None
|
||||
|
||||
def select(x):
|
||||
|
||||
@@ -338,10 +338,45 @@ class Video(Track):
|
||||
hdr10plus=data.get("hdr10plus", False),
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def dv_fourccs(codecs: Optional[str]) -> set[str]:
|
||||
"""The Dolby Vision fourccs in a codecs list (comma or space separated, ``/brand`` suffixes allowed)."""
|
||||
fourccs = {token.split("/")[0].split(".")[0] for token in re.split(r"[\s,]+", (codecs or "").lower())}
|
||||
return fourccs & {"dva1", "dvav", "dvhe", "dvh1"}
|
||||
|
||||
@staticmethod
|
||||
def has_dv_codec(codecs: Optional[str]) -> bool:
|
||||
"""Whether a codecs list names a Dolby Vision codec."""
|
||||
return bool(Video.dv_fourccs(codecs))
|
||||
|
||||
@staticmethod
|
||||
def is_dv_composite(
|
||||
codec: Optional[Video.Codec], codecs: Optional[str], supplemental_codecs: Optional[str]
|
||||
) -> bool:
|
||||
"""Whether an HEVC track names DV only in its supplemental codecs: DV profile 8 on an HDR10, SDR or HLG base."""
|
||||
return (
|
||||
codec == Video.Codec.HEVC
|
||||
and not Video.has_dv_codec(codecs)
|
||||
and bool(Video.dv_fourccs(supplemental_codecs) & {"dvh1", "dvhe"})
|
||||
)
|
||||
|
||||
@property
|
||||
def carries_hdr10plus(self) -> bool:
|
||||
"""Whether the track carries HDR10+, as its range or, on a DV track, found by the HDR10+ probe."""
|
||||
return self.hdr10plus or self.range == Video.Range.HDR10P
|
||||
|
||||
@property
|
||||
def finished_hybrid(self) -> bool:
|
||||
"""Whether the track already carries DV and an HDR10 or HDR10+ base, so ``-r HYBRID`` needs no build."""
|
||||
if self.range == Video.Range.DV:
|
||||
return self.hdr10plus
|
||||
return self.dv_compatible_bitstream and self.range in (Video.Range.HDR10, Video.Range.HDR10P)
|
||||
|
||||
@property
|
||||
def range_label(self) -> str:
|
||||
"""The range name, with ``+HDR10P`` on a DV track that also carries HDR10+."""
|
||||
return self.range.name + ("+HDR10P" if self.hdr10plus else "")
|
||||
"""The range that ``-r`` selects the track by, then any second format it carries: HDR10+DV, DV+HDR10P."""
|
||||
label = self.range.name + ("+HDR10P" if self.hdr10plus else "")
|
||||
return f"{label}+DV" if self.dv_compatible_bitstream and self.range != Video.Range.DV else label
|
||||
|
||||
def __str__(self) -> str:
|
||||
return " | ".join(
|
||||
|
||||
Reference in New Issue
Block a user