fix(tracks): mark AC-4 Atmos from the HLS ATMOS channels identifier and carry atmos without JOC through signature, export and remote

This commit is contained in:
imSp4rky
2026-12-02 15:43:23 -07:00
parent 3b6bd02e46
commit eeab646272
11 changed files with 154 additions and 9 deletions
+1 -1
View File
@@ -548,7 +548,7 @@ def get_tracks(self, title: Title_T) -> Tracks:
- `signature` holds the attributes that the track list and selection - `signature` holds the attributes that the track list and selection
read: codec, range, resolution, frame rate, bitrate and name for video; 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. language, codec, flags and name for subtitles.
- The helper does not remove copies inside one manifest. To remove them, - The helper does not remove copies inside one manifest. To remove them,
call `tracks.dedupe()`. Two tracks that differ only in a value outside call `tracks.dedupe()`. Two tracks that differ only in a value outside
+4
View File
@@ -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. `5.1.4` matches on its total number of channels, so `-c 10` selects it.
- `-naa` / `--noatmos`: exclude Atmos audio tracks from selection. - `-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" ```shell title="5.1 audio, no Atmos"
unshackle dl -c 5.1 --noatmos EXAMPLE 0ABC123 unshackle dl -c 5.1 --noatmos EXAMPLE 0ABC123
``` ```
+113
View File
@@ -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 = '<SupplementalProperty schemeIdUri="tag:dolby.com,2016:dash:virtualized_content:2016" value="1"/>'
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"""<?xml version="1.0" encoding="UTF-8"?>
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" type="static" mediaPresentationDuration="PT3M">
<Period id="0">
<AdaptationSet id="1" contentType="audio" lang="en">
<Representation id="1" codecs="{codecs}" audioSamplingRate="48000" bandwidth="259908" mimeType="audio/mp4">
<AudioChannelConfiguration schemeIdUri="urn:mpeg:mpegB:cicp:ChannelConfiguration" value="{channels}"/>
{extra}
<BaseURL>audio.mp4</BaseURL>
<SegmentBase indexRange="0-100"/>
</Representation>
</AdaptationSet>
</Period>
</MPD>"""
(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
+14
View File
@@ -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 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: class ProviderService:
ANIME = True ANIME = True
METADATA_PROVIDER = "tmdb" METADATA_PROVIDER = "tmdb"
+1
View File
@@ -567,6 +567,7 @@ EXPORT_AUDIO = {
"bitrate": 640000, "bitrate": 640000,
"channels": 5.1, "channels": 5.1,
"joc": 16, "joc": 16,
"atmos": True,
"descriptive": True, "descriptive": True,
"bit_depth": 24, "bit_depth": 24,
"sample_rate": 48000, "sample_rate": 48000,
+2 -2
View File
@@ -116,7 +116,7 @@ def audio_token(track: Audio) -> str:
channels = track.channels channels = track.channels
if isinstance(channels, float): if isinstance(channels, float):
channels = f"{channels:.1f}" 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]: def quality_parts(tracks: Iterable[AnyTrack]) -> list[str]:
@@ -137,7 +137,7 @@ def quality_parts(tracks: Iterable[AnyTrack]) -> list[str]:
key=lambda a: ( key=lambda a: (
audio_order.index(a.codec) if a.codec else len(audio_order), audio_order.index(a.codec) if a.codec else len(audio_order),
-Audio.channel_total(a.channels) if a.channels else 0, -Audio.channel_total(a.channels) if a.channels else 0,
not a.joc, not a.atmos,
), ),
) )
+1 -1
View File
@@ -240,7 +240,7 @@ class ExportWriter:
if t.stereo: if t.stereo:
row["stereo"] = True row["stereo"] = True
elif isinstance(t, Audio): 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): elif isinstance(t, Subtitle):
row.update(sdh=t.sdh, forced=t.forced, cc=t.cc) row.update(sdh=t.sdh, forced=t.forced, cc=t.cc)
if t.descriptor == Video.Descriptor.URL: if t.descriptor == Video.Descriptor.URL:
+7 -1
View File
@@ -1537,7 +1537,13 @@ class DASH:
@staticmethod @staticmethod
def get_ddp_complexity_index(adaptation_set: Element, representation: Element | None) -> int | None: 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( return next(
( (
int(x.get("value")) int(x.get("value"))
+6 -1
View File
@@ -207,6 +207,7 @@ class HLS:
All Track objects' URL will be to another M3U(8) document. However, these documents 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. 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 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 []) session_keys = list(self.manifest.session_keys or [])
if not session_keys: if not session_keys:
@@ -332,6 +333,7 @@ class HLS:
entries.add(entry) entries.add(entry)
joc = 0 joc = 0
atmos = False
track_type: type[Audio] | type[Subtitle] track_type: type[Audio] | type[Subtitle]
if media.type == "AUDIO": if media.type == "AUDIO":
track_type = Audio track_type = Audio
@@ -343,7 +345,9 @@ class HLS:
if media.channels: if media.channels:
# the channel count, then /-separated coding (JOC) and usage identifiers (RFC 8216bis) # the channel count, then /-separated coding (JOC) and usage identifiers (RFC 8216bis)
count, *identifiers = media.channels.split("/") 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) joc = int(count)
media.channels = "5.1" media.channels = "5.1"
else: else:
@@ -379,6 +383,7 @@ class HLS:
bitrate=0, # TODO: M3U doesn't seem to state bitrate? bitrate=0, # TODO: M3U doesn't seem to state bitrate?
channels=media.channels, channels=media.channels,
joc=joc, joc=joc,
extra={"atmos": True} if atmos else None,
descriptive="public.accessibility.describes-video" in (media.characteristics or ""), descriptive="public.accessibility.describes-video" in (media.characteristics or ""),
**resolution, **resolution,
) )
+1
View File
@@ -416,6 +416,7 @@ class Tracks:
track.codec, track.codec,
track.channels, track.channels,
track.joc, track.joc,
track.atmos,
track.descriptive, track.descriptive,
track.bitrate, track.bitrate,
track.bit_depth, track.bit_depth,
+4 -3
View File
@@ -824,14 +824,13 @@ def deserialize_video(data: dict[str, Any]) -> Video:
def deserialize_audio(data: dict[str, Any]) -> Audio: def deserialize_audio(data: dict[str, Any]) -> Audio:
"""An audio track from its REST form.""" """An audio track from its REST form."""
joc = data.get("joc")
return Audio( return Audio(
**rest_track_kwargs(data), **rest_track_kwargs(data),
codec=enum_get(Audio.Codec, data.get("codec")), codec=enum_get(Audio.Codec, data.get("codec")),
bitrate=deserialize_bitrate(data), bitrate=deserialize_bitrate(data),
channels=data.get("channels"), channels=data.get("channels"),
joc=joc, joc=data.get("joc"),
extra={"atmos": True} if joc is None and data.get("atmos") else None, extra={"atmos": True} if data.get("atmos") else None,
descriptive=data.get("descriptive", False), descriptive=data.get("descriptive", False),
bit_depth=data.get("bit_depth"), bit_depth=data.get("bit_depth"),
sample_rate=data.get("sample_rate"), sample_rate=data.get("sample_rate"),
@@ -919,6 +918,7 @@ def track_to_dict(track: Track) -> dict[str, Any]:
"bitrate": track.bitrate, "bitrate": track.bitrate,
"channels": track.channels, "channels": track.channels,
"joc": track.joc, "joc": track.joc,
"atmos": track.atmos,
"descriptive": track.descriptive, "descriptive": track.descriptive,
"bit_depth": track.bit_depth, "bit_depth": track.bit_depth,
"sample_rate": track.sample_rate, "sample_rate": track.sample_rate,
@@ -969,6 +969,7 @@ def track_from_dict(data: dict[str, Any]) -> Video | Audio | Subtitle:
bitrate=data.get("bitrate"), bitrate=data.get("bitrate"),
channels=data.get("channels"), channels=data.get("channels"),
joc=data.get("joc"), joc=data.get("joc"),
extra={"atmos": True} if data.get("atmos") else None,
descriptive=data.get("descriptive", False), descriptive=data.get("descriptive", False),
bit_depth=data.get("bit_depth"), bit_depth=data.get("bit_depth"),
sample_rate=data.get("sample_rate"), sample_rate=data.get("sample_rate"),