From eeab6462728044819e39bffe973fc6fba7d9d7b2 Mon Sep 17 00:00:00 2001 From: imSp4rky Date: Wed, 2 Dec 2026 15:43:22 -0700 Subject: [PATCH] fix(tracks): mark AC-4 Atmos from the HLS ATMOS channels identifier and carry atmos without JOC through signature, export and remote --- docs/dev/creating-a-service.md | 2 +- docs/guide/downloading.md | 4 ++ tests/core/test_ac4_atmos.py | 113 +++++++++++++++++++++++++++++++ tests/core/test_export.py | 14 ++++ tests/core/test_wire.py | 1 + unshackle/core/export_name.py | 4 +- unshackle/core/export_writer.py | 2 +- unshackle/core/manifests/dash.py | 8 ++- unshackle/core/manifests/hls.py | 7 +- unshackle/core/tracks/tracks.py | 1 + unshackle/core/wire.py | 7 +- 11 files changed, 154 insertions(+), 9 deletions(-) create mode 100644 tests/core/test_ac4_atmos.py diff --git a/docs/dev/creating-a-service.md b/docs/dev/creating-a-service.md index 1b04081f..f4b2cf9f 100644 --- a/docs/dev/creating-a-service.md +++ b/docs/dev/creating-a-service.md @@ -548,7 +548,7 @@ def get_tracks(self, title: Title_T) -> Tracks: - `signature` holds the attributes that the track list and selection read: codec, range, resolution, frame rate, bitrate and name for video; - language, codec, channels, JOC, descriptive, bitrate and name for audio; + language, codec, channels, JOC, Atmos, descriptive, bitrate and name for audio; language, codec, flags and name for subtitles. - The helper does not remove copies inside one manifest. To remove them, call `tracks.dedupe()`. Two tracks that differ only in a value outside diff --git a/docs/guide/downloading.md b/docs/guide/downloading.md index 9e61f8b6..50c0ad37 100644 --- a/docs/guide/downloading.md +++ b/docs/guide/downloading.md @@ -354,6 +354,10 @@ unshackle dl -a EC3,AAC EXAMPLE 81234567 `5.1.4` matches on its total number of channels, so `-c 10` selects it. - `-naa` / `--noatmos`: exclude Atmos audio tracks from selection. +A track is Atmos when its manifest says so: a DD+ JOC descriptor, an HLS `CHANNELS` value with +`JOC` or `ATMOS` (`16/JOC`, `2/IMSA,ATMOS`), or the Smooth Streaming `HasAtmos` attribute. A DASH +manifest has no Atmos signal for AC-4, so a DASH AC-4 track is Atmos only when the service marks it. + ```shell title="5.1 audio, no Atmos" unshackle dl -c 5.1 --noatmos EXAMPLE 0ABC123 ``` diff --git a/tests/core/test_ac4_atmos.py b/tests/core/test_ac4_atmos.py new file mode 100644 index 00000000..347c5ae4 --- /dev/null +++ b/tests/core/test_ac4_atmos.py @@ -0,0 +1,113 @@ +"""Atmos on an AC-4 track comes only from a manifest signal that a Dolby or ETSI document defines as Atmos.""" + +from __future__ import annotations + +import pytest + +from unshackle.core.export_name import audio_token +from unshackle.core.manifests.dash import DASH +from unshackle.core.manifests.hls import HLS +from unshackle.core.tracks import Audio, Tracks +from unshackle.core.wire import deserialize_audio, serialize_audio_track, track_from_dict, track_to_dict + +VIRTUALIZED = '' + + +def hls_audio(channels: str, codecs: str = "ac-4") -> Audio: + text = ( + "#EXTM3U\n" + f'#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="a",NAME="English",LANGUAGE="en",CHANNELS="{channels}",URI="ac4.m3u8"\n' + f'#EXT-X-STREAM-INF:BANDWIDTH=3000000,RESOLUTION=1920x1080,CODECS="avc1.640028,{codecs}",AUDIO="a"\n' + "video.m3u8\n" + ) + (audio,) = HLS.from_text(text, "https://cdn.example/master.m3u8").to_tracks(language="en").audio + return audio + + +def dash_audio(codecs: str, channels: str, extra: str = "") -> Audio: + text = f""" + + + + + + {extra} + audio.mp4 + + + + +""" + (audio,) = DASH.from_text(text, "https://cdn.example/manifest.mpd").to_tracks(language="en").audio + return audio + + +def test_an_hls_ac4_rendition_with_the_atmos_identifier_is_atmos_without_a_joc_count() -> None: + audio = hls_audio("2/IMSA,ATMOS") + + assert audio.codec == Audio.Codec.AC4 + assert audio.atmos + assert audio.channels == 2.0 + assert not audio.joc + assert "2.0, Atmos |" in str(audio) + assert "JOC" not in str(audio) + + +def test_an_hls_immersive_stereo_rendition_without_the_atmos_identifier_is_not_atmos() -> None: + audio = hls_audio("2/IMSA") + + assert not audio.atmos + assert audio.channels == 2.0 + + +@pytest.mark.parametrize("channels", ["2", "6"]) +def test_a_plain_hls_ac4_rendition_is_not_atmos(channels: str) -> None: + assert not hls_audio(channels, codecs="ac-4.02.01.00").atmos + + +def test_an_hls_joc_rendition_keeps_its_joc_count() -> None: + audio = hls_audio("16/JOC", codecs="ec-3") + + assert audio.atmos + assert audio.joc == 16 + + +@pytest.mark.parametrize( + ("codecs", "channels", "extra"), + [ + ("ac-4.02.01.00", "6", ""), + ("ac-4.02.01.00", "2", ""), + ("ac-4.02.02.00", "2", VIRTUALIZED), + ], + ids=["plain-5.1", "plain-2.0", "immersive-stereo"], +) +def test_a_dash_ac4_representation_without_a_cited_atmos_signal_is_not_atmos( + codecs: str, channels: str, extra: str +) -> None: + """Neither presentation_version 2 nor virtualized_content proves Atmos: an IMS presentation may carry any content.""" + audio = dash_audio(codecs, channels, extra) + + assert audio.codec == Audio.Codec.AC4 + assert not audio.atmos + + +def test_the_export_name_reads_atmos_without_a_joc_count() -> None: + assert audio_token(hls_audio("2/IMSA,ATMOS")).endswith("2.0atmos") + + +def test_atmos_without_a_joc_count_is_part_of_the_track_signature() -> None: + assert Tracks.signature(hls_audio("2/IMSA,ATMOS")) != Tracks.signature(hls_audio("2/IMSA")) + + +def test_atmos_without_a_joc_count_survives_the_export_track_dict() -> None: + audio = track_from_dict(track_to_dict(hls_audio("2/IMSA,ATMOS"))) + + assert audio.atmos + assert not audio.joc + + +def test_hls_atmos_without_a_joc_count_survives_the_remote_round_trip() -> None: + audio = deserialize_audio(serialize_audio_track(hls_audio("2/IMSA,ATMOS"))) + + assert audio.atmos + assert not audio.joc diff --git a/tests/core/test_export.py b/tests/core/test_export.py index 3de94073..bf2c55ae 100644 --- a/tests/core/test_export.py +++ b/tests/core/test_export.py @@ -504,6 +504,20 @@ def test_a_3d_video_stays_3d_through_export_and_import(tmp_path: Path) -> None: assert svc.get_tracks(movie).videos[0].stereo +def test_atmos_without_a_joc_count_stays_atmos_through_export_and_import(tmp_path: Path) -> None: + writer = make_writer(tmp_path) + title = make_title() + title.tracks.audio[0].joc = 0 + title.tracks.audio[0].extra = {"atmos": True} + for track in [*title.tracks.videos, *title.tracks.audio]: + writer.record(title, track) + export = written(writer) + assert [row.get("atmos") for row in entry(export)["tracks"] if row["type"] == "audio"] == [True] + svc = ImportService(import_ctx(), "EXAMPLE", "movie-1", str(export)) + movie = next(iter(svc.get_titles())) + assert svc.get_tracks(movie).audio[0].atmos + + class ProviderService: ANIME = True METADATA_PROVIDER = "tmdb" diff --git a/tests/core/test_wire.py b/tests/core/test_wire.py index 7fd26916..15a245d0 100644 --- a/tests/core/test_wire.py +++ b/tests/core/test_wire.py @@ -567,6 +567,7 @@ EXPORT_AUDIO = { "bitrate": 640000, "channels": 5.1, "joc": 16, + "atmos": True, "descriptive": True, "bit_depth": 24, "sample_rate": 48000, diff --git a/unshackle/core/export_name.py b/unshackle/core/export_name.py index f518df1a..1a716213 100644 --- a/unshackle/core/export_name.py +++ b/unshackle/core/export_name.py @@ -116,7 +116,7 @@ def audio_token(track: Audio) -> str: channels = track.channels if isinstance(channels, float): channels = f"{channels:.1f}" - return f"{codec}{channels or ''}{'atmos' if track.joc else ''}" + return f"{codec}{channels or ''}{'atmos' if track.atmos else ''}" def quality_parts(tracks: Iterable[AnyTrack]) -> list[str]: @@ -137,7 +137,7 @@ def quality_parts(tracks: Iterable[AnyTrack]) -> list[str]: key=lambda a: ( audio_order.index(a.codec) if a.codec else len(audio_order), -Audio.channel_total(a.channels) if a.channels else 0, - not a.joc, + not a.atmos, ), ) diff --git a/unshackle/core/export_writer.py b/unshackle/core/export_writer.py index 794b8003..e62a76dd 100644 --- a/unshackle/core/export_writer.py +++ b/unshackle/core/export_writer.py @@ -240,7 +240,7 @@ class ExportWriter: if t.stereo: row["stereo"] = True elif isinstance(t, Audio): - row.update(channels=str(t.channels) if t.channels else "", atmos=bool(t.joc), descriptive=t.descriptive) + row.update(channels=str(t.channels) if t.channels else "", atmos=t.atmos, descriptive=t.descriptive) elif isinstance(t, Subtitle): row.update(sdh=t.sdh, forced=t.forced, cc=t.cc) if t.descriptor == Video.Descriptor.URL: diff --git a/unshackle/core/manifests/dash.py b/unshackle/core/manifests/dash.py index 44d0d81b..76311679 100644 --- a/unshackle/core/manifests/dash.py +++ b/unshackle/core/manifests/dash.py @@ -1537,7 +1537,13 @@ class DASH: @staticmethod def get_ddp_complexity_index(adaptation_set: Element, representation: Element | None) -> int | None: - """Get the DD+ Complexity Index (if any) from the AdaptationSet or Representation.""" + """ + Get the DD+ Complexity Index (if any) from the AdaptationSet or Representation. + + An MPD has no Atmos signal for AC-4: ETSI TS 103 190-2 E.13 and G.3.1 give no Atmos meaning to the codecs + string or virtualized_content, and the Dolby AC-4 DASH spec (Online Delivery Kit 1.5) allows immersive + stereo for any audio. + """ return next( ( int(x.get("value")) diff --git a/unshackle/core/manifests/hls.py b/unshackle/core/manifests/hls.py index 61cee1d0..0b060f65 100644 --- a/unshackle/core/manifests/hls.py +++ b/unshackle/core/manifests/hls.py @@ -207,6 +207,7 @@ class HLS: All Track objects' URL will be to another M3U(8) document. However, these documents will be Invariant Playlists and contain the list of segments URIs among other metadata. A variant or a media entry that the master lists twice with the same attributes gets one track. + A CHANNELS coding identifier ATMOS marks Atmos (Dolby AC-4 HLS spec, Online Delivery Kit 1.5: "2/IMSA,ATMOS"). """ session_keys = list(self.manifest.session_keys or []) if not session_keys: @@ -332,6 +333,7 @@ class HLS: entries.add(entry) joc = 0 + atmos = False track_type: type[Audio] | type[Subtitle] if media.type == "AUDIO": track_type = Audio @@ -343,7 +345,9 @@ class HLS: if media.channels: # the channel count, then /-separated coding (JOC) and usage identifiers (RFC 8216bis) count, *identifiers = media.channels.split("/") - if identifiers and "JOC" in identifiers[0].split(","): + coding = identifiers[0].split(",") if identifiers else [] + atmos = "ATMOS" in coding + if "JOC" in coding: joc = int(count) media.channels = "5.1" else: @@ -379,6 +383,7 @@ class HLS: bitrate=0, # TODO: M3U doesn't seem to state bitrate? channels=media.channels, joc=joc, + extra={"atmos": True} if atmos else None, descriptive="public.accessibility.describes-video" in (media.characteristics or ""), **resolution, ) diff --git a/unshackle/core/tracks/tracks.py b/unshackle/core/tracks/tracks.py index dc21f528..86c05660 100644 --- a/unshackle/core/tracks/tracks.py +++ b/unshackle/core/tracks/tracks.py @@ -416,6 +416,7 @@ class Tracks: track.codec, track.channels, track.joc, + track.atmos, track.descriptive, track.bitrate, track.bit_depth, diff --git a/unshackle/core/wire.py b/unshackle/core/wire.py index a7351ce9..c4332f08 100644 --- a/unshackle/core/wire.py +++ b/unshackle/core/wire.py @@ -824,14 +824,13 @@ def deserialize_video(data: dict[str, Any]) -> Video: def deserialize_audio(data: dict[str, Any]) -> Audio: """An audio track from its REST form.""" - joc = data.get("joc") return Audio( **rest_track_kwargs(data), codec=enum_get(Audio.Codec, data.get("codec")), bitrate=deserialize_bitrate(data), channels=data.get("channels"), - joc=joc, - extra={"atmos": True} if joc is None and data.get("atmos") else None, + joc=data.get("joc"), + extra={"atmos": True} if data.get("atmos") else None, descriptive=data.get("descriptive", False), bit_depth=data.get("bit_depth"), sample_rate=data.get("sample_rate"), @@ -919,6 +918,7 @@ def track_to_dict(track: Track) -> dict[str, Any]: "bitrate": track.bitrate, "channels": track.channels, "joc": track.joc, + "atmos": track.atmos, "descriptive": track.descriptive, "bit_depth": track.bit_depth, "sample_rate": track.sample_rate, @@ -969,6 +969,7 @@ def track_from_dict(data: dict[str, Any]) -> Video | Audio | Subtitle: bitrate=data.get("bitrate"), channels=data.get("channels"), joc=data.get("joc"), + extra={"atmos": True} if data.get("atmos") else None, descriptive=data.get("descriptive", False), bit_depth=data.get("bit_depth"), sample_rate=data.get("sample_rate"),