-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathenable_dolby_vision_hdmi.py
64 lines (52 loc) · 2.16 KB
/
enable_dolby_vision_hdmi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import sys
def hex_to_int(hex: str) -> int:
"""Convert a hexadecimal string to an integer."""
return int(hex, 16)
def int_to_hex(num: int) -> str:
"""Convert an integer to a 2-character hexadecimal string."""
return f'{num:02x}'
def enable_dolby_vision_hdmi(hex: str) -> str:
"""
Enable Dolby Vision HDMI by modifying the appropriate byte in a 14-character
hexadecimal string.
"""
if len(hex) != 14 or not all(c in '0123456789abcdefABCDEF' for c in hex):
raise ValueError("Input must be a 14-character hexadecimal string.")
hex_chunks_index = 2 # Index of the Dolby Vision value in the chunks
# Split into chunks of 2 characters
hex_chunks = [hex[i : i + 2] for i in range(0, len(hex), 2)]
# Set the last bit to enable 'LLDV-HDMI'
dolby_bits = hex_to_int(hex_chunks[hex_chunks_index])
dolby_bits |= 1 # Enable the last bit
hex_chunks[hex_chunks_index] = int_to_hex(dolby_bits)
return ''.join(hex_chunks)
def run_tests():
"""Run test cases to validate the enable_dolby_vision_hdmi function."""
samples = (
('480376825e6d95', '480377825e6d95'),
('4403609248458f', '4403619248458f'),
('4d4e4a725a7776', '4d4e4b725a7776'),
('480a7e86607694', '480a7f86607694'),
('48039e5898aa5c', '48039f5898aa5c'),
)
for hex_input, expected_hex_output in samples:
hex_output = enable_dolby_vision_hdmi(hex_input)
assert hex_output == expected_hex_output, (
f"Test failed: {hex_input} -> {hex_output} (expected {expected_hex_output})"
)
print("All tests passed.")
def main():
"""Main function to handle command-line input."""
try:
video_hex = sys.argv[1]
except IndexError:
raise ValueError('No value provided for argument `video_hex`')
video_hex = video_hex.strip()
new_video_hex = enable_dolby_vision_hdmi(video_hex)
if new_video_hex == video_hex:
print(f"Warning: `video_hex` of '{video_hex}' is already enabled with LLDV-HDMI")
else:
print(f"Update `video_hex` from '{video_hex}' to '{new_video_hex}' to enable LLDV-HDMI")
if __name__ == '__main__':
run_tests()
main()