Files
unshackle/unshackle/core/utils/hdr10plus.py
T

395 lines
17 KiB
Python

from __future__ import annotations
import logging
import struct
from collections.abc import Callable, Iterator
from concurrent.futures import ThreadPoolExecutor
from typing import TYPE_CHECKING, TypeVar
from urllib.parse import urljoin
from requests import Session
from unshackle.core.manifests.ism_init import find_box, iter_boxes, remove_emulation_prevention
from unshackle.core.session import RnetSession, utf8_text
from unshackle.core.utils.bitrate import PROBE_TIMEOUT, ranged_get
if TYPE_CHECKING:
from unshackle.core.title_source import TitleSource
from unshackle.core.titles import Title_T
from unshackle.core.tracks import Audio, Track, Tracks, Video
PROBE_BYTES = 64 * 1024
HDR10PLUS_T35 = b"\xb5\x00\x3c\x00\x01\x04"
CUVA_T35 = b"\x26\x00\x04\x00\x05"
TS_PACKET = 188
TS_PROBE_BYTES = 16 * TS_PACKET
T = TypeVar("T")
R = TypeVar("R")
def map_probes(probe: Callable[[T], R], items: list[T], workers: int) -> list[R]:
"""``probe`` over ``items`` in order, with no more than ``workers`` in flight."""
if not items:
return []
with ThreadPoolExecutor(max_workers=min(workers, len(items))) as executor:
return list(executor.map(probe, items))
def is_ts(data: bytes) -> bool:
"""Whether the bytes start as MPEG-TS packets."""
return data[:1] == b"\x47" and (len(data) <= TS_PACKET or data[TS_PACKET : TS_PACKET + 1] == b"\x47")
def ts_streams(data: bytes) -> list[tuple[int, bytes]] | None:
"""The ``stream_type`` and registration format identifier of each PMT entry, or None without a PMT."""
entries = ts_pmt(data)
return None if entries is None else [(stream_type, registration) for _, stream_type, registration in entries]
def ts_pid(b: bytes, i: int = 1) -> int:
"""The 13-bit PID at ``b[i]``: a packet header at offset 1, or a PAT or PMT entry."""
return ((b[i] & 0x1F) << 8) | b[i + 1]
def ts_payload(packet: bytes) -> bytes:
"""The payload of a TS packet after any adaptation field, or b"" when it carries none."""
adaptation = (packet[3] >> 4) & 3
return packet[5 + packet[4] if adaptation & 2 else 4 :] if adaptation & 1 else b""
def ts_packets(data: bytes) -> Iterator[bytes]:
"""The 188-byte packets of ``data`` that start with the sync byte."""
for pos in range(0, len(data) - TS_PACKET + 1, TS_PACKET):
if data[pos] == 0x47:
yield data[pos : pos + TS_PACKET]
def ts_pmt(data: bytes) -> list[tuple[int, int, bytes]] | None:
"""The PID, ``stream_type`` and registration format identifier of each PMT entry, or None without a PMT.
PAT program 0 names the network information table, so it is skipped. Lengths that run past the packet
are cut to it, so bad bytes give fewer entries, never an exception.
"""
# reads only a PAT and PMT that fit in one packet, as packagers write them; reassemble
# sections across packets if a source with a longer PMT appears
def length(b: bytes, i: int) -> int:
return ((b[i] & 0x0F) << 8) | b[i + 1]
def section(packet: bytes, table_id: int) -> bytes:
"""The packet's ``table_id`` section up to its CRC, or b"" when the packet holds no such section."""
payload = ts_payload(packet)
body = payload[1 + payload[0] :] if payload else b""
body = body[: max(0, 3 + length(body, 1) - 4)] if len(body) >= 3 else b""
return body if len(body) >= 12 and body[0] == table_id else b""
pmt_pid = None
for packet in ts_packets(data):
if not packet[1] & 0x40:
continue
if ts_pid(packet) == 0 and pmt_pid is None:
pat = section(packet, 0x00)
pmt_pid = next((ts_pid(pat, k + 2) for k in range(8, len(pat) - 3, 4) if pat[k] or pat[k + 1]), None)
elif ts_pid(packet) == pmt_pid:
pmt = section(packet, 0x02)
if not pmt:
return None
i, streams = 12 + length(pmt, 10), []
while i + 5 <= len(pmt):
info_end = min(i + 5 + length(pmt, i + 3), len(pmt))
registration, j = b"", i + 5
while j + 2 <= info_end:
if pmt[j] == 0x05 and j + 6 <= info_end:
registration = pmt[j + 2 : j + 6]
j += 2 + pmt[j + 1]
streams.append((ts_pid(pmt, i + 1), pmt[i], registration))
i = info_end
return streams
return None
def sei_number(rbsp: bytes, i: int) -> tuple[int | None, int]:
"""Read the SEI payload type or size that starts at ``i``: 0xFF bytes add 255 each. Returns (value, next)."""
value = 0
while i < len(rbsp) and rbsp[i] == 0xFF:
value += 255
i += 1
if i >= len(rbsp):
return None, i
return value + rbsp[i], i + 1
def ts_elementary(data: bytes, pid: int) -> bytes:
"""The payload bytes of ``pid`` in the packets of ``data``, joined and without their PES headers."""
stream = bytearray()
for packet in ts_packets(data):
if ts_pid(packet) != pid:
continue
payload = ts_payload(packet)
if packet[1] & 0x40 and payload[:3] == b"\x00\x00\x01" and len(payload) >= 9:
payload = payload[9 + payload[8] :]
stream += payload
return bytes(stream)
def sei_has_t35(nal: bytes, t35: bytes) -> bool:
"""Whether an HEVC or VVC SEI NAL holds the T.35 message that ``t35`` starts."""
rbsp = remove_emulation_prevention(nal[2:])
end = len(rbsp.rstrip(b"\x00")) - 1
i = 0
while i < end:
payload_type, i = sei_number(rbsp, i)
payload_size, i = sei_number(rbsp, i)
if payload_type is None or payload_size is None:
return False
if payload_type == 4 and rbsp[i : i + len(t35)] == t35:
return True
i += payload_size
return False
def mdat_nals(data: bytes) -> Iterator[bytes]:
"""The length-prefixed NALs of an fMP4 mdat."""
# 4-byte NAL lengths: reading lengthSizeMinusOne from hvcC would cost an init-segment fetch
mdat = find_box(data, 0, len(data), b"mdat")
pos = mdat[0] if mdat else len(data)
while pos + 6 <= len(data):
size = struct.unpack(">I", data[pos : pos + 4])[0]
yield data[pos + 4 : pos + 4 + size]
pos += 4 + size
def ts_nals(data: bytes) -> Iterator[bytes]:
"""The Annex-B NALs of the HEVC or VVC stream that the PMT of an MPEG-TS segment lists."""
pid = next((pid for pid, stream_type, _ in ts_pmt(data) or [] if stream_type in (0x24, 0x33)), None)
stream = b"" if pid is None else ts_elementary(data, pid)
yield from stream.split(b"\x00\x00\x01")[1:]
def fragment_has_t35(data: bytes, t35: bytes, vvc: bool = False) -> bool | None:
"""Whether the first sample of an fMP4 fragment, or the first access unit of an MPEG-TS segment, carries the
T.35 SEI that ``t35`` starts, or None when the bytes cannot tell. ``HDR10PLUS_T35`` finds HDR10+,
``CUVA_T35`` HDR Vivid, and ``vvc`` reads H.266 NAL headers.
The walk ends at the first VCL NAL, because a prefix SEI comes before it. CENC and cbcs keep
non-VCL NALs and every NAL length in the clear, so this needs no content key. A sample that is
encrypted whole can read as False as well as None; both leave the label alone.
"""
sei, vcl_end = (23, 12) if vvc else (39, 32)
for nal in ts_nals(data) if is_ts(data) else mdat_nals(data):
if len(nal) < 2 or nal[0] & 0x80 or not nal[1] & 0x07:
return None
kind = nal[1] >> 3 if vvc else nal[0] >> 1
if kind < vcl_end:
return False
if kind == sei and sei_has_t35(nal, t35):
return True
return None
def first_segment(track: Track, session: Session | RnetSession) -> tuple[str, int] | None:
"""The URL and start offset of the track's first media segment, looked up without a full parse."""
from unshackle.core.manifests import DASH
from unshackle.core.tracks.track import Track
url = track.url if isinstance(track.url, str) else track.url[0]
dash = track.data.get("dash") or {}
# a service can collapse a DASH representation to a one-file URL track and keep the manifest
if dash.get("manifest") is not None and dash.get("period") is not None:
_, segments, *_ = DASH.get_period_segments(
period=dash["period"],
adaptation_set=dash["adaptation_set"],
representation=dash["representation"],
manifest=dash["manifest"],
track=track,
track_url=url,
session=session,
probe_kid=False,
)
if not segments:
return None
seg_url, byte_range = segments[0]
return seg_url, int((byte_range or "").partition("-")[0] or 0)
if track.descriptor == Track.Descriptor.HLS:
text = track.held_playlist()
if text is None:
res = session.get(url, timeout=PROBE_TIMEOUT)
if getattr(res, "status_code", 0) != 200:
return None
text = utf8_text(res)
offset = 0
for line in text.splitlines():
line = line.strip()
if line.startswith("#EXT-X-BYTERANGE:") and "@" in line:
offset = int(line.rpartition("@")[2])
elif line and not line.startswith("#"):
return urljoin(url, line), offset
return None
if track.descriptor == Track.Descriptor.URL:
return url, 0
return None
def first_fragment(track: Track, session: Session | RnetSession) -> bytes | None:
"""The first PROBE_BYTES of the track's first media fragment, or of its first segment when that is MPEG-TS."""
segment = first_segment(track, session)
if not segment:
return None
url, start = segment
# A one-file track opens with ftyp, moov and sidx, which can outgrow PROBE_BYTES. Fetch again past
# them only when the fragment does not start inside this window.
for _ in range(4):
data = ranged_get(url, session, f"bytes={start}-{start + PROBE_BYTES - 1}", accept_200=True)
if not data:
return None
if is_ts(data):
return data
skip = 0
for kind, _, _, end in iter_boxes(data, 0, len(data)):
if kind in (b"styp", b"moof", b"mdat"):
return data
skip = end
if not skip:
return data
start += skip
return None
def probe_dynamic_hdr(
videos: list[Video],
session: Session | RnetSession,
*,
log: logging.Logger,
workers: int,
on_start: Callable[[], None] | None = None,
) -> None:
"""Relabel HEVC and VVC tracks by the dynamic HDR metadata of their first sample, and flag DV tracks with HDR10+.
HDR10+ SEI makes an HDR10 track HDR10P. CUVA SEI, or the ``cuvv`` registration of an MPEG-TS stream,
makes an HDR10 or HLG track VIVID, and an HLG one gets ``hlg_base``; ``PROBED_RANGES`` holds which label
each range takes. A DV track keeps its DV range and gets ``hdr10plus`` set, so selection and hybrid pairing
are unchanged. HDR10, HLG and DV are separate ladders, and so are HEVC and VVC. A ladder with one track per
resolution is sampled at both ends first. Two tracks at one resolution can be an HDR10 and an HDR10+ encode side by side, so such a
ladder is probed whole. The rules are in the ``hdr10plus_probe`` section of docs/reference/configuration/download.md.
A failed probe never changes a label. ``workers`` caps the probes in flight, and ``on_start`` runs once
before the first.
"""
from unshackle.core.tracks import Video
from unshackle.core.tracks.video import PROBED_RANGES
ladders = [
sorted(
(v for v in videos if v.range == range_ and v.codec == codec),
key=lambda v: v.bitrate or 0,
reverse=True,
)
for range_ in (*PROBED_RANGES, Video.Range.DV)
for codec in (Video.Codec.HEVC, Video.Codec.VVC)
]
if any(ladders) and on_start:
on_start()
def proven(data: bytes | None, vvc: bool) -> Video.Range | None:
if not data:
return None
if is_ts(data) and any(reg == b"cuvv" for _, reg in ts_streams(data) or []):
return Video.Range.VIVID
if fragment_has_t35(data, CUVA_T35, vvc):
return Video.Range.VIVID
return Video.Range.HDR10P if fragment_has_t35(data, HDR10PLUS_T35, vvc) else None
def probe(video: Video) -> Video.Range | None:
"""The range the first segment proves (HDR10P or VIVID), else None."""
try:
found = proven(first_fragment(video, video.session or session), video.codec == Video.Codec.VVC)
except Exception as e:
log.debug(f"{video.id}: dynamic HDR probe failed ({e})")
return None
log.debug(f"{video.id}: dynamic HDR probe {found}")
return found
def probe_all(tracks: list[Video]) -> dict[int, Video.Range | None]:
return dict(zip(map(id, tracks), map_probes(probe, tracks, workers), strict=True))
plans: list[tuple[list[Video], list[Video], list[Video]]] = []
for candidates in ladders:
if len(candidates) <= 4 or len({(v.width, v.height) for v in candidates}) < len(candidates):
plans.append((candidates, candidates, []))
else:
plans.append((candidates, candidates[:2] + candidates[-2:], candidates[2:-2]))
# one round for the samples of every ladder, then one for the rest of each mixed ladder
found = probe_all([v for _, samples, _ in plans for v in samples])
labels = [{found[id(s)] for s in samples} for _, samples, _ in plans]
found.update(
probe_all(
[v for (_, _, rest), seen in zip(plans, labels, strict=True) if any(seen) and len(seen) > 1 for v in rest]
)
)
for (candidates, _, _), seen in zip(plans, labels, strict=True):
whole = next(iter(seen)) if len(seen) == 1 else None
for video in candidates:
label = found.get(id(video), whole)
if video.range == Video.Range.DV:
video.hdr10plus = video.hdr10plus or label == Video.Range.HDR10P
elif label in PROBED_RANGES.get(video.range, ()):
video.hlg_base = video.range == Video.Range.HLG
video.range = label
def probe_audio_codecs(
audio: list[Audio], session: Session | RnetSession, *, log: logging.Logger, workers: int
) -> None:
"""Name the codec of each audio track that has none from the PMT of its MPEG-TS first segment.
Only the first ``TS_PROBE_BYTES`` are read, because an HLS segment opens with its PAT and PMT.
"""
from unshackle.core.tracks import Audio
def probe(track: Audio) -> None:
try:
track_session = track.session or session
segment = first_segment(track, track_session)
data = segment and ranged_get(
segment[0], track_session, f"bytes={segment[1]}-{segment[1] + TS_PROBE_BYTES - 1}", accept_200=True
)
streams = (ts_streams(data) if data and is_ts(data) else None) or []
except Exception as e:
log.debug(f"{track.id}: audio codec probe failed ({e})")
return
codec = next(filter(None, (Audio.Codec.from_ts(*stream) for stream in streams)), None)
log.debug(f"{track.id}: audio codec probe {codec}")
if codec:
track.codec = codec
map_probes(probe, [a for a in audio if not a.codec], workers)
def probed_tracks(
service: TitleSource,
title: Title_T,
*,
workers: int | None = None,
on_probe: Callable[[], None] | None = None,
) -> Tracks:
"""The title's tracks from ``service.get_tracks``, with HDR10+ and HDR Vivid found in HDR10 and HLG tracks.
Audio with no codec gets one from the MPEG-TS ``stream_type``. ``workers`` defaults to ``dl.workers`` from
the config. A remote service's tracks come back from a server that already probed them.
"""
from unshackle.core.config import config
from unshackle.core.downloaders import default_max_workers
from unshackle.core.remote_service import RemoteService
tracks = service.get_tracks(title)
codecless_audio = any(not a.codec for a in tracks.audio)
if config.hdr10plus_probe and (tracks.videos or codecless_audio) and not isinstance(service, RemoteService):
# int() as click does for --workers, so a quoted dl.workers works on the REST path too
workers = workers or int(config.dl.get("workers") or 0) or default_max_workers()
if not isinstance(workers, int) or isinstance(workers, bool) or workers < 1:
raise ValueError(f"workers must be a whole number of 1 or more, not {workers!r}")
log = logging.getLogger("hdr10plus")
probe_dynamic_hdr(tracks.videos, service.session, log=log, workers=workers, on_start=on_probe)
probe_audio_codecs(tracks.audio, service.session, log=log, workers=workers)
return tracks