Skip to content

WIP: add SCC (608) writer #441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 23 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/python/ttconv/scc/codes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ def get_values(self) -> Tuple[int, int]:
"""Returns SCC Code values"""
return self._channel_1, self._channel_2

def get_ch1_value(self) -> int:
"""Returns Channel 1 Code Value"""
return self._channel_1

def get_channel(self, value: int) -> Optional[SccChannel]:
"""Returns caption channel corresponding to the specified code value"""
if value == self._channel_1:
Expand Down
55 changes: 55 additions & 0 deletions src/main/python/ttconv/scc/codes/characters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# Copyright (c) 2025, Sandflow Consulting LLC
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""SCC characters utility functions"""

from __future__ import annotations

from ttconv.scc.codes.special_characters import SccSpecialCharacter
from ttconv.scc.codes.standard_characters import TO_SCC_BYTES

def unicode_to_scc(s: str) -> bytes:
"""Convert a Unicode string to an SCC character string"""

b = bytearray()

for c in s:

scc_c = TO_SCC_BYTES.get(c, None)

if scc_c is not None:
b.append(scc_c)
continue

scc_c = SccSpecialCharacter.from_unicode(c)

if scc_c is not None:
b.append(scc_c.get_ch1_value() // 256)
b.append(scc_c.get_ch1_value() % 256)
continue

b.append(0x20) # Standard

return b
103 changes: 83 additions & 20 deletions src/main/python/ttconv/scc/codes/preambles_address_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,33 @@
(0x04, 0x60): 15
}

_ROW_PAC_MAPPING_CH1 = [
(0x01, 0x40),
(0x01, 0x60),
(0x02, 0x40),
(0x02, 0x60),
(0x05, 0x40),
(0x05, 0x60),
(0x06, 0x40),
(0x06, 0x60),
(0x07, 0x40),
(0x07, 0x60),
(0x00, 0x40),
(0x03, 0x40),
(0x03, 0x60),
(0x04, 0x40),
(0x04, 0x60)
]

_PAC_COLOR_MAPPING = {
NamedColors.white.value: 0,
NamedColors.green.value: 2,
NamedColors.blue.value: 4,
NamedColors.cyan.value: 6,
NamedColors.red.value: 8,
NamedColors.yellow.value: 10,
NamedColors.magenta.value: 12,
}

class _SccPacDescriptionBits:
"""Helper class for SCC PAC description bits handling"""
Expand Down Expand Up @@ -88,22 +115,18 @@ def get_indent(self) -> Optional[int]:
class SccPreambleAddressCode:
"""SCC PAC definition"""

def __init__(self, byte_1: int, byte_2: int):
row = SccPreambleAddressCode._get_row(byte_1, byte_2)
if row is None:
raise ValueError("Failed to extract PAC row from specified bytes:", hex(byte_1), hex(byte_2))

desc_bits = SccPreambleAddressCode._get_description_bits(byte_2)
if desc_bits is None:
raise ValueError("Failed to extract PAC description from specified bytes:", hex(byte_1), hex(byte_2))

def __init__(self, channel: SccChannel,
row: int,
color: ColorType = NamedColors.white,
indent: Optional[int] = None,
is_italic: bool = False,
is_underline: bool = False):
self._channel = channel
self._row = row
self._color: Optional[ColorType] = desc_bits.get_color()
self._indent: Optional[int] = desc_bits.get_indent()
self._font_style: Optional[bool] = FontStyleType.italic if desc_bits.get_italic() else None
self._text_decoration: Optional[TextDecorationType] = \
TextDecorationType(underline=True) if desc_bits.get_underline() else None
self._channel = SccChannel.CHANNEL_2 if byte_1 & 0x08 else SccChannel.CHANNEL_1
self._color = color
self._indent = indent
self._is_italic = is_italic
self._is_underline = is_underline

def get_row(self) -> int:
"""Returns the PAC row"""
Expand All @@ -117,13 +140,21 @@ def get_color(self) -> Optional[ColorType]:
"""Returns PAC color"""
return self._color

def is_underline(self) -> bool:
"""Returns PAC underline flag"""
return self._is_underline

def is_italic(self) -> bool:
"""Returns PAC italic flag"""
return self._is_italic

def get_font_style(self) -> Optional[FontStyleType]:
"""Returns PAC font style"""
return self._font_style
return FontStyleType.italic if self._is_italic else None

def get_text_decoration(self) -> Optional[TextDecorationType]:
"""Returns PAC text decoration"""
return self._text_decoration
return TextDecorationType(underline=True) if self._is_underline else None

def get_channel(self) -> SccChannel:
"""Returns PAC channel"""
Expand All @@ -138,14 +169,46 @@ def __eq__(self, other) -> bool:
and self.get_font_style() == other.get_font_style() \
and self.get_text_decoration() == other.get_text_decoration()

def get_ch1_packet(self):
hi, lo = _ROW_PAC_MAPPING_CH1[self.get_row() - 1]

hi = hi + 0x10 # channel 1

if self.is_italic():
lo = lo + 0x0E
elif self.get_indent() is not None:
lo = lo + 0x10 + self.get_indent() // 2
else:
lo = lo + _PAC_COLOR_MAPPING.get(self.get_color(), 0)

if self.is_underline():
lo = lo + 1

return hi * 256 + lo

@staticmethod
def find(byte_1: int, byte_2: int) -> Optional[SccPreambleAddressCode]:
"""Find the SCC PAC corresponding to the specified bytes"""
try:
return SccPreambleAddressCode(byte_1, byte_2)
except ValueError as _e:

row = SccPreambleAddressCode._get_row(byte_1, byte_2)
if row is None:
# Failed to extract PAC row from specified bytes
return None

desc_bits = SccPreambleAddressCode._get_description_bits(byte_2)
if desc_bits is None:
# Failed to extract PAC description from specified bytes
return None

return SccPreambleAddressCode(
SccChannel.CHANNEL_2 if byte_1 & 0x08 else SccChannel.CHANNEL_1,
row,
desc_bits.get_color(),
desc_bits.get_indent(),
desc_bits.get_italic(),
desc_bits.get_underline()
)

@staticmethod
def _get_row(byte_1: int, byte_2: int) -> Optional[int]:
"""Decodes SCC PAC row number from specified bytes"""
Expand Down
8 changes: 8 additions & 0 deletions src/main/python/ttconv/scc/codes/special_characters.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ def get_unicode_value(self) -> chr:
"""Returns the special or extended character unicode value"""
return self._unicode

@staticmethod
def from_unicode(c: str) -> typing.Optional[SccSpecialCharacter]:
"""Find the special character corresponding to the Unicode character"""
for spec_char in list(SccSpecialCharacter):
if spec_char.get_unicode_value() == c:
return spec_char
return None

@staticmethod
def find(value: int) -> typing.Optional[SccSpecialCharacter]:
"""Find the special character corresponding to the specified value"""
Expand Down
2 changes: 2 additions & 0 deletions src/main/python/ttconv/scc/codes/standard_characters.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,5 @@
(0x7E, "\u00F1"), # ñ Lower-case n with tilde
(0x7F, "\u2588"), # █ Solid block
])

TO_SCC_BYTES = {unicode_char: scc_byte for scc_byte, unicode_char in SCC_STANDARD_CHARACTERS_MAPPING.items()}
32 changes: 32 additions & 0 deletions src/main/python/ttconv/scc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

from dataclasses import dataclass, field
from enum import Enum
import typing

from ttconv.config import ModuleConfiguration
from ttconv.style_properties import TextAlignType
Expand Down Expand Up @@ -71,3 +72,34 @@ class SccReaderConfiguration(ModuleConfiguration):
@classmethod
def name(cls):
return "scc_reader"

def _decode_rollup_lines(value: str) -> int:
decoded_value = int(value)

if decoded_value < 2 or decoded_value > 4:
raise ValueError(f"Invalid rollup_lines '{value}' value. Expect: 2-4.")

return decoded_value

@dataclass
class SccWriterConfiguration(ModuleConfiguration):
"""SCC writer configuration"""

allow_reflow: bool = field(
default=True,
metadata={"decoder": bool}
)

force_popon: bool = field(
default=False,
metadata={"decoder": bool}
)

rollup_lines: int = field(
default=4,
metadata={"decoder": _decode_rollup_lines}
)

@classmethod
def name(cls):
return "scc_writer"
Loading