From 37a1c77e87ae6289a1613b1b19bbc5c88a023f05 Mon Sep 17 00:00:00 2001
From: louis <louis@poweris.moe>
Date: Thu, 15 Apr 2021 11:38:22 -0400
Subject: [PATCH] vivy: tv: common: allow hardsubmask highpass configuration
 and change default

---
 Vivy/01/01.vpy                | 14 +++++------
 Vivy/02/02.vpy                | 44 +++++++++++++++++------------------
 Vivy/vivy_common/__init__.py  |  2 +-
 Vivy/vivy_common/dehardsub.py | 21 +++++++++++++----
 4 files changed, 47 insertions(+), 34 deletions(-)

diff --git a/Vivy/01/01.vpy b/Vivy/01/01.vpy
index 168c5fa..42f2d90 100644
--- a/Vivy/01/01.vpy
+++ b/Vivy/01/01.vpy
@@ -14,13 +14,13 @@ core = vs.core
 
 EPNUM: int = int(os.path.basename(os.path.splitext(__file__)[0]))
 SIGNS_RU: List[HardsubSign] = [
-    HardsubSign((5283, 5403), ((226, 786), (1214, 102))),
-    HardsubSign((6778, 6888), ((588, 454), (434, 63))),
-    HardsubSign((9017, 9070), ((975, 301), (157, 24))),
-    HardsubSign((22276, 22350), ((480, 75), (959, 66))),
-    HardsubSign((25715, 25904), ((623, 73), (681, 67))),
-    HardsubSign((35223, 36108), ((775, 866), (1138, 211))),
-    HardsubSign((37572, 37666), ((259, 856), (987, 103))),
+    HardsubSign((5283, 5403), ((226, 786), (1214, 102)), highpass=2000),
+    HardsubSign((6778, 6888), ((588, 454), (434, 63)), highpass=2000),
+    HardsubSign((9017, 9070), ((975, 301), (157, 24)), highpass=2000),
+    HardsubSign((22276, 22350), ((480, 75), (959, 66)), highpass=2000),
+    HardsubSign((25715, 25904), ((623, 73), (681, 67)), highpass=2000),
+    HardsubSign((35223, 36108), ((775, 866), (1138, 211)), highpass=2000),
+    HardsubSign((37572, 37666), ((259, 856), (987, 103)), highpass=2000),
 ]
 CREDITS: List[Range] = [(35151, 37306)]
 PIXELSHIT: List[Range] = [
diff --git a/Vivy/02/02.vpy b/Vivy/02/02.vpy
index 9ae84cf..3265251 100644
--- a/Vivy/02/02.vpy
+++ b/Vivy/02/02.vpy
@@ -15,28 +15,28 @@ core = vs.core
 EPNUM: int = int(os.path.basename(os.path.splitext(__file__)[0]))
 SIGNS_RU: List[HardsubSign] = [
     HardsubSign((125, 245), ((220, 865), (992, 98)), refframe=143),
-    HardsubSign((865, 896), ((1173, 539), (232, 40))),
-    HardsubSign((2274, 2318), ((431, 671), (1068, 142))),
-    HardsubSign((2391, 2426), ((116, 62), (1471, 311))),
-    HardsubSign((2427, 2452), ((317, 728), (1176, 80))),
-    HardsubSign((3776, 3871), ((782, 286), (748, 76))),
-    HardsubSign((3877, 3950), ((866, 524), (494, 53))),
-    HardsubSign((6498, 6542), ((696, 296), (493, 31))),
-    HardsubSign((7212, 7221), ((430, 666), (1066, 149))),
-    HardsubSign((7222, 7233), ((317, 728), (1179, 84))),
-    HardsubSign((7234, 7245), ((410, 303), (1169, 129))),
-    HardsubSign((7246, 7254), ((514, 687), (807, 90))),
-    HardsubSign((27488, 27630), ((778, 287), (758, 78))),
-    HardsubSign((27636, 27779), ((756, 449), (792, 87))),
-    HardsubSign((28907, 28934), ((758, 454), (787, 79))),
-    HardsubSign((28945, 28954), ((773, 481), (758, 57))),
-    HardsubSign((28986, 29019), ((621, 748), (657, 52))),
-    HardsubSign((29053, 29061), ((621, 748), (657, 52))),
-    HardsubSign((29062, 29077), ((649, 333), (742, 53))),
-    HardsubSign((29615, 29674), ((336, 74), (1244, 76))),
-    HardsubSign((29675, 29758), ((587, 68), (750, 85))),
-    HardsubSign((30259, 30977), ((293, 843), (1321, 227))),
-    HardsubSign((32608, 32703), ((281, 859), (890, 101))),
+    HardsubSign((865, 896), ((1173, 539), (232, 40)), highpass=2000),
+    HardsubSign((2274, 2318), ((431, 671), (1068, 142)), highpass=2000),
+    HardsubSign((2391, 2426), ((116, 62), (1471, 311)), highpass=2000),
+    HardsubSign((2427, 2452), ((317, 728), (1176, 80)), highpass=2000),
+    HardsubSign((3776, 3871), ((782, 286), (748, 76)), highpass=2000),
+    HardsubSign((3877, 3950), ((866, 524), (494, 53)), highpass=2000),
+    HardsubSign((6498, 6542), ((696, 296), (493, 31)), highpass=2000),
+    HardsubSign((7212, 7221), ((430, 666), (1066, 149)), highpass=2000),
+    HardsubSign((7222, 7233), ((317, 728), (1179, 84)), highpass=2000),
+    HardsubSign((7234, 7245), ((410, 303), (1169, 129)), highpass=2000),
+    HardsubSign((7246, 7254), ((514, 687), (807, 90)), highpass=2000),
+    HardsubSign((27488, 27630), ((778, 287), (758, 78)), highpass=2000),
+    HardsubSign((27636, 27779), ((756, 449), (792, 87)), highpass=2000),
+    HardsubSign((28907, 28934), ((758, 454), (787, 79)), highpass=2000),
+    HardsubSign((28945, 28954), ((773, 481), (758, 57)), highpass=2000),
+    HardsubSign((28986, 29019), ((621, 748), (657, 52)), highpass=2000),
+    HardsubSign((29053, 29061), ((621, 748), (657, 52)), highpass=2000),
+    HardsubSign((29062, 29077), ((649, 333), (742, 53)), highpass=2000),
+    HardsubSign((29615, 29674), ((336, 74), (1244, 76)), highpass=2000),
+    HardsubSign((29675, 29758), ((587, 68), (750, 85)), highpass=2000),
+    HardsubSign((30259, 30977), ((293, 843), (1321, 227)), highpass=2000),
+    HardsubSign((32608, 32703), ((281, 859), (890, 101)), highpass=2000),
 ]
 CREDITS: List[Range] = [(30152, 32343)]
 PIXELSHIT: List[Range] = [
diff --git a/Vivy/vivy_common/__init__.py b/Vivy/vivy_common/__init__.py
index 49d630f..3bc9238 100644
--- a/Vivy/vivy_common/__init__.py
+++ b/Vivy/vivy_common/__init__.py
@@ -1,3 +1,3 @@
-from .dehardsub import HardsubSign, bounded_dehardsub  # noqa: F401
+from .dehardsub import HardsubSign, bounded_dehardsub, get_all_masks  # noqa: F401
 from .filter import antialias, deband, denoise, finalize, fsrcnnx_rescale, letterbox_edgefix  # noqa: F401
 from .util import Range, glob_crc, source  # noqa: F401
diff --git a/Vivy/vivy_common/dehardsub.py b/Vivy/vivy_common/dehardsub.py
index f590698..26abf85 100644
--- a/Vivy/vivy_common/dehardsub.py
+++ b/Vivy/vivy_common/dehardsub.py
@@ -32,13 +32,15 @@ class HardsubSign():
     range: Range
     bound: Optional[BoundingBox]
     refframe: Optional[int]
+    highpass: int
 
     def __init__(self,
                  range: Range,
                  bound: Union[BoundingBox, Tuple[Tuple[int, int], Tuple[int, int]], None],
-                 refframe: Optional[int] = None):
+                 refframe: Optional[int] = None, highpass: int = 5000):
         self.range = range
         self.refframe = refframe
+        self.highpass = highpass
         if bound is None:
             self.bound = None
         elif isinstance(bound, BoundingBox):
@@ -48,9 +50,9 @@ class HardsubSign():
 
     def _hardsub_mask(self, hrdsb: vs.VideoNode, ref: vs.VideoNode) -> vs.VideoNode:
         if self.refframe is not None:
-            mask = kgf.hardsubmask_fades(hrdsb[self.refframe], ref[self.refframe], highpass=2000)
+            mask = kgf.hardsubmask_fades(hrdsb[self.refframe], ref[self.refframe], highpass=self.highpass)
         else:
-            mask = kgf.hardsubmask_fades(hrdsb, ref, highpass=2000)
+            mask = kgf.hardsubmask_fades(hrdsb, ref, highpass=self.highpass)
 
         assert isinstance(mask, vs.VideoNode)
         return mask
@@ -71,11 +73,22 @@ class HardsubSign():
         return core.std.MaskedMerge(core.std.BlankClip(hm), hm, bm)
 
 
+def get_all_masks(hrdsb: vs.VideoNode, ref: vs.VideoNode, signs: List[HardsubSign]) -> vs.VideoNode:
+    """
+    Scenefiltering helper, not used in encode
+    """
+    assert ref.format is not None
+    mask = core.std.BlankClip(ref, format=ref.format.replace(color_family=vs.GRAY, subsampling_w=0, subsampling_h=0))
+    for sign in signs:
+        mask = lvf.misc.replace_ranges(mask, core.std.Expr([mask, sign.get_mask(hrdsb, ref)], 'x y +'), [sign.range])
+    return mask
+
+
 def bounded_dehardsub(hrdsb: vs.VideoNode, ref: vs.VideoNode, signs: List[HardsubSign]) -> vs.VideoNode:
     bound = hrdsb
     for sign in signs:
         bound = lvf.misc.replace_ranges(bound,
-                                        core.std.MaskedMerge(hrdsb, ref, sign.get_mask(hrdsb, ref)),
+                                        core.std.MaskedMerge(bound, ref, sign.get_mask(hrdsb, ref)),
                                         [sign.range])
 
     return bound