mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-05 15:35:06 +00:00
fix(drm): decrypt a track with mp4decrypt when its sample offsets are not relative to the moof
This commit is contained in:
@@ -195,6 +195,11 @@ unshackle decrypts a stereoscopic 3D (MV-HEVC) track with mp4decrypt whatever th
|
|||||||
shaka-packager refuses its Dolby Vision configuration (version 3), and before v3.5.0 it also drops
|
shaka-packager refuses its Dolby Vision configuration (version 3), and before v3.5.0 it also drops
|
||||||
the boxes of the second view.
|
the boxes of the second view.
|
||||||
|
|
||||||
|
unshackle also uses mp4decrypt, whatever this setting says, for a fragmented track whose first
|
||||||
|
`moof` gives a base data offset in its `tfhd` or has a `trun` with no data offset, as
|
||||||
|
`MP4Box -frag` writes. shaka-packager reads every sample offset from the start of the `moof`, so
|
||||||
|
it writes noise for such a track.
|
||||||
|
|
||||||
mp4decrypt finds a content key by the KID in the first `sinf` box of the track. When that box
|
mp4decrypt finds a content key by the KID in the first `sinf` box of the track. When that box
|
||||||
has no CENC `tenc` (for example, a PIFF `sinf` before the CENC one), unshackle also gives
|
has no CENC `tenc` (for example, a PIFF `sinf` before the CENC one), unshackle also gives
|
||||||
mp4decrypt the content key of that KID by track ID. When mp4decrypt copies a track through
|
mp4decrypt the content key of that KID by track ID. When mp4decrypt copies a track through
|
||||||
|
|||||||
@@ -0,0 +1,100 @@
|
|||||||
|
"""``decrypter_for`` sends a track whose sample offsets are not relative to the moof to mp4decrypt."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import struct
|
||||||
|
import subprocess
|
||||||
|
from pathlib import Path
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from tests.core.test_ffmpeg_decodes import GOP, video
|
||||||
|
from tests.core.test_kid_windows import box, full
|
||||||
|
from unshackle.core import binaries
|
||||||
|
from unshackle.core.config import config
|
||||||
|
from unshackle.core.drm import ClearKeyCENC
|
||||||
|
from unshackle.core.drm.verify import decrypter_for, offsets_not_moof_relative
|
||||||
|
from unshackle.core.utils.subprocess import ffmpeg_decodes
|
||||||
|
|
||||||
|
KID = UUID("11223344556677889900aabbccddeeff")
|
||||||
|
KEY = "00112233445566778899aabbccddeeff"
|
||||||
|
GPAC_DRM = f"""<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<GPACDRM type="CENC AES-CTR">
|
||||||
|
<CrypTrack trackID="1" IsEncrypted="1" IV_size="8" first_IV="0x0123456789abcdef" saiSavedBox="senc">
|
||||||
|
<key KID="0x{KID.hex}" value="0x{KEY}"/>
|
||||||
|
</CrypTrack>
|
||||||
|
</GPACDRM>
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def trun(flags: int) -> bytes:
|
||||||
|
return full(b"trun", struct.pack(">I", 0) + (struct.pack(">i", 100) if flags & 1 else b""), flags=flags)
|
||||||
|
|
||||||
|
|
||||||
|
ABSOLUTE = full(b"tfhd", struct.pack(">IQ", 1, 2322), flags=0x000001)
|
||||||
|
MOOF_BASE = full(b"tfhd", struct.pack(">I", 1), flags=0x020000)
|
||||||
|
|
||||||
|
|
||||||
|
def fragment(*truns: bytes, tfhd: bytes = MOOF_BASE) -> bytes:
|
||||||
|
return box(b"moof", full(b"mfhd", struct.pack(">I", 1)), box(b"traf", tfhd, *truns)) + box(b"mdat", bytes(16))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("data", "expected"),
|
||||||
|
[
|
||||||
|
pytest.param(box(b"moov") + fragment(trun(0xA04)), True, id="no-offset"),
|
||||||
|
pytest.param(box(b"moov") + fragment(trun(0xA05)), False, id="offset"),
|
||||||
|
pytest.param(box(b"moov") + fragment(trun(0xA04), tfhd=ABSOLUTE), True, id="gpac"),
|
||||||
|
pytest.param(box(b"moov") + fragment(trun(0xA05), tfhd=ABSOLUTE), True, id="base-and-offset"),
|
||||||
|
pytest.param(box(b"moov") + fragment(trun(0xA04), trun(0xA05)), True, id="first-of-two"),
|
||||||
|
pytest.param(box(b"moov") + fragment(trun(0xA05)) + fragment(trun(0xA04)), False, id="second-moof"),
|
||||||
|
pytest.param(box(b"moov") + box(b"mdat", bytes(16)), False, id="progressive"),
|
||||||
|
pytest.param(box(b"moov") + fragment(trun(0xA04))[:30], False, id="truncated"),
|
||||||
|
pytest.param(box(b"moov") + box(b"moof", box(b"traf", box(b"trun"))), False, id="empty-trun"),
|
||||||
|
pytest.param(box(b"moov") + box(b"moof", b"\x00\x00\x00\x01traf\x00"), False, id="cut-largesize"),
|
||||||
|
pytest.param(b"\xff" * 64, False, id="garbage"),
|
||||||
|
pytest.param(box(b"moov") + b"\x00\x00\x00\x01moof" + (2**62).to_bytes(8, "big"), False, id="huge-moof"),
|
||||||
|
pytest.param(box(b"moov") + b"\x00\x00\x00\x01free" + (2**64 - 1).to_bytes(8, "big"), False, id="huge-box"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_offsets_not_moof_relative(tmp_path: Path, data: bytes, expected: bool) -> None:
|
||||||
|
(path := tmp_path / "track.mp4").write_bytes(data)
|
||||||
|
assert offsets_not_moof_relative(path) is expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_missing_file_is_not_detected(tmp_path: Path) -> None:
|
||||||
|
assert offsets_not_moof_relative(tmp_path / "gone.mp4") is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_decrypter_for_routes_only_the_shaka_default(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||||
|
(flagged := tmp_path / "gpac.mp4").write_bytes(box(b"moov") + fragment(trun(0xA04), tfhd=ABSOLUTE))
|
||||||
|
(normal := tmp_path / "dash.mp4").write_bytes(box(b"moov") + fragment(trun(0xA05)))
|
||||||
|
monkeypatch.setattr(config, "decryption", "shaka")
|
||||||
|
assert decrypter_for(flagged) == "mp4decrypt"
|
||||||
|
assert decrypter_for(normal) == "shaka"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
not (binaries.FFMPEG and binaries.MP4Box and binaries.Mp4decrypt and binaries.ShakaPackager),
|
||||||
|
reason="needs FFmpeg, MP4Box, mp4decrypt and shaka-packager",
|
||||||
|
)
|
||||||
|
def test_a_gpac_fragmented_track_decrypts_under_the_shaka_default(
|
||||||
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||||
|
) -> None:
|
||||||
|
def run(*cmd: object) -> None:
|
||||||
|
subprocess.run([str(part) for part in cmd], cwd=tmp_path, check=True, capture_output=True)
|
||||||
|
|
||||||
|
video(tmp_path / "src.mp4", *GOP, size="640x360")
|
||||||
|
(tmp_path / "drm.xml").write_text(GPAC_DRM)
|
||||||
|
run(binaries.MP4Box, "-quiet", "-crypt", "drm.xml", "src.mp4", "-out", "enc.mp4")
|
||||||
|
run(binaries.MP4Box, "-quiet", "-frag", "2000", "enc.mp4", "-out", "track.mp4")
|
||||||
|
track = tmp_path / "track.mp4"
|
||||||
|
monkeypatch.setattr(config, "decryption", "shaka")
|
||||||
|
monkeypatch.setattr(config.directories, "temp", tmp_path / "temp")
|
||||||
|
|
||||||
|
picked = decrypter_for(track)
|
||||||
|
ClearKeyCENC(kids=[KID], content_keys={KID: KEY}).decrypt(track)
|
||||||
|
|
||||||
|
assert ffmpeg_decodes(track, video=True)
|
||||||
|
assert picked == "mp4decrypt"
|
||||||
@@ -153,8 +153,8 @@ def read_top_level_box(path: Path, box_type: bytes) -> bytes | None:
|
|||||||
"""Read a single top-level box from an MP4 file.
|
"""Read a single top-level box from an MP4 file.
|
||||||
|
|
||||||
This function walks only the box headers, so the wanted box is the only payload it reads into memory.
|
This function walks only the box headers, so the wanted box is the only payload it reads into memory.
|
||||||
Returns None if the file has no such top-level box, or if a box header is truncated or declares an
|
Returns None if the file has no such top-level box, or if a box header is truncated or declares a size
|
||||||
impossible size.
|
below 8 or past the end of the file.
|
||||||
"""
|
"""
|
||||||
file_size = path.stat().st_size
|
file_size = path.stat().st_size
|
||||||
with path.open("rb") as f:
|
with path.open("rb") as f:
|
||||||
@@ -171,7 +171,7 @@ def read_top_level_box(path: Path, box_type: bytes) -> bytes | None:
|
|||||||
size = int.from_bytes(large, "big")
|
size = int.from_bytes(large, "big")
|
||||||
elif size == 0:
|
elif size == 0:
|
||||||
size = file_size - start
|
size = file_size - start
|
||||||
if size < 8:
|
if size < 8 or start + size > file_size:
|
||||||
return None
|
return None
|
||||||
if header[4:8] == box_type:
|
if header[4:8] == box_type:
|
||||||
f.seek(start)
|
f.seek(start)
|
||||||
@@ -244,15 +244,31 @@ def has_second_view(path: Path) -> bool:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def offsets_not_moof_relative(path: Path) -> bool:
|
||||||
|
"""True if the MP4's first moof has a tfhd base data offset, or a trun with no data offset."""
|
||||||
|
try:
|
||||||
|
moof = read_top_level_box(path, b"moof") or b""
|
||||||
|
flags = {(kind, moof[body + 3] & 0x01) for kind, body, end in walk(moof) if end - body >= 4}
|
||||||
|
except (OSError, struct.error, RecursionError):
|
||||||
|
return False
|
||||||
|
return (b"tfhd", 1) in flags or (b"trun", 0) in flags
|
||||||
|
|
||||||
|
|
||||||
def decrypter_for(path: Path) -> str:
|
def decrypter_for(path: Path) -> str:
|
||||||
"""The configured decrypter, or mp4decrypt for a multi-layer (MV-HEVC) track.
|
"""The configured decrypter, or mp4decrypt for a track that shaka-packager misreads.
|
||||||
|
|
||||||
shaka-packager refuses a version 3 dvvC (Dolby Vision profile 20) and, before v3.5.0, drops lhvC and vexu.
|
shaka-packager refuses a version 3 dvvC (Dolby Vision profile 20) and, before v3.5.0, drops lhvC and vexu.
|
||||||
|
shaka-packager also ignores the tfhd base data offset and reads a trun with no data offset as offset 0 of the moof.
|
||||||
"""
|
"""
|
||||||
configured = str(getattr(config, "decryption", "")).lower()
|
configured = str(getattr(config, "decryption", "")).lower()
|
||||||
if configured != "mp4decrypt" and has_second_view(path):
|
if configured == "mp4decrypt":
|
||||||
|
return configured
|
||||||
|
if has_second_view(path):
|
||||||
log.debug(f"Decrypting {path.name} with mp4decrypt: it holds a second video layer (lhvC)")
|
log.debug(f"Decrypting {path.name} with mp4decrypt: it holds a second video layer (lhvC)")
|
||||||
return "mp4decrypt"
|
return "mp4decrypt"
|
||||||
|
if offsets_not_moof_relative(path):
|
||||||
|
log.debug(f"Decrypting {path.name} with mp4decrypt: its sample offsets are not relative to the moof")
|
||||||
|
return "mp4decrypt"
|
||||||
return configured
|
return configured
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user