From e144906b11ffe0193cfad09b864fc7f151b50641 Mon Sep 17 00:00:00 2001
From: louis <louis@poweris.moe>
Date: Sat, 17 Apr 2021 20:52:22 -0400
Subject: [PATCH] vivy: tv: common: upstream dehardsubbing utility to lvsfunc

---
 Vivy/01/01.vpy                | 38 ++++++++++----
 Vivy/02/02.vpy                | 38 ++++++++++----
 Vivy/03/03.vpy                |  5 +-
 Vivy/vivy_common/__init__.py  |  3 +-
 Vivy/vivy_common/dehardsub.py | 97 -----------------------------------
 Vivy/vivy_common/filter.py    |  3 +-
 Vivy/vivy_common/util.py      |  2 -
 7 files changed, 60 insertions(+), 126 deletions(-)
 delete mode 100644 Vivy/vivy_common/dehardsub.py

diff --git a/Vivy/01/01.vpy b/Vivy/01/01.vpy
index 42f2d90..ec5986a 100644
--- a/Vivy/01/01.vpy
+++ b/Vivy/01/01.vpy
@@ -1,12 +1,15 @@
 import vapoursynth as vs
 
+from lvsfunc.types import Range
+from lvsfunc.dehardsub import HardsubSign, bounded_dehardsub
+
 from typing import List
 
 import os
 import sys
 sys.path.append("..")
 
-from vivy_common import (HardsubSign, Range, bounded_dehardsub, antialias, deband, denoise,  # noqa: E402
+from vivy_common import (SelfRunner, antialias, deband, denoise,  # noqa: E402
                          finalize, fsrcnnx_rescale, letterbox_edgefix, source)
 
 core = vs.core
@@ -37,14 +40,27 @@ LETTERBOX: List[Range] = [
     (659, 14071)
 ]
 
-waka, ref = source(EPNUM)
-waka = waka[:37301] + core.std.BlankClip(waka, length=6) + waka[37301:]
 
-src = bounded_dehardsub(waka, ref, SIGNS_RU)
-rescale = fsrcnnx_rescale(src, NOSCALE)
-den = denoise(rescale)
-deb = deband(den)
-aa = antialias(deb, NOAA)
-edgefix = letterbox_edgefix(aa, LETTERBOX)
-final = finalize(edgefix)
-final.set_output()
+def filter_basic() -> vs.VideoNode:
+    waka, ref = source(EPNUM)
+    waka = waka[:37301] + core.std.BlankClip(waka, length=6) + waka[37301:]
+    src = bounded_dehardsub(waka, ref, SIGNS_RU)
+    return src
+
+
+def filter() -> vs.VideoNode:
+    src = filter_basic()
+    rescale = fsrcnnx_rescale(src, NOSCALE)
+    den = denoise(rescale)
+    deb = deband(den)
+    aa = antialias(deb, NOAA)
+    edgefix = letterbox_edgefix(aa, LETTERBOX)
+    final = finalize(edgefix)
+    final.set_output()
+    return final
+
+
+if __name__ == "__main__":
+    SelfRunner(EPNUM, filter, filter_basic)
+else:
+    filter()
diff --git a/Vivy/02/02.vpy b/Vivy/02/02.vpy
index 3265251..df5abe6 100644
--- a/Vivy/02/02.vpy
+++ b/Vivy/02/02.vpy
@@ -1,13 +1,16 @@
 import vapoursynth as vs
 
+from lvsfunc.types import Range
+from lvsfunc.dehardsub import HardsubSign, bounded_dehardsub
+
 from typing import List
 
 import os
 import sys
 sys.path.append("..")
 
-from vivy_common import (HardsubSign, Range, bounded_dehardsub, antialias, deband, denoise,  # noqa: E402
-                         finalize, fsrcnnx_rescale, source)
+from vivy_common import (SelfRunner, antialias, deband, denoise, finalize,  # noqa: E402
+                         fsrcnnx_rescale, source)
 
 core = vs.core
 
@@ -45,13 +48,26 @@ PIXELSHIT: List[Range] = [
 NOSCALE: List[Range] = CREDITS + PIXELSHIT
 NOAA: List[Range] = PIXELSHIT
 
-waka, ref = source(EPNUM)
-waka = waka[:32344] + waka[32349:]
 
-src = bounded_dehardsub(waka, ref, SIGNS_RU)
-rescale = fsrcnnx_rescale(src, NOSCALE)
-den = denoise(rescale)
-deb = deband(den)
-aa = antialias(deb, NOAA)
-final = finalize(aa)
-final.set_output()
+def filter_basic() -> vs.VideoNode:
+    waka, ref = source(EPNUM)
+    waka = waka[:32344] + waka[32349:]
+    src = bounded_dehardsub(waka, ref, SIGNS_RU)
+    return src
+
+
+def filter() -> vs.VideoNode:
+    src = filter_basic()
+    rescale = fsrcnnx_rescale(src, NOSCALE)
+    den = denoise(rescale)
+    deb = deband(den)
+    aa = antialias(deb, NOAA)
+    final = finalize(aa)
+    final.set_output()
+    return final
+
+
+if __name__ == "__main__":
+    SelfRunner(EPNUM, filter, filter_basic)
+else:
+    filter()
diff --git a/Vivy/03/03.vpy b/Vivy/03/03.vpy
index 30b31d3..2b542ea 100644
--- a/Vivy/03/03.vpy
+++ b/Vivy/03/03.vpy
@@ -1,12 +1,15 @@
 import vapoursynth as vs
 
+from lvsfunc.types import Range
+from lvsfunc.dehardsub import HardsubSign, bounded_dehardsub
+
 from typing import List
 
 import os
 import sys
 sys.path.append("..")
 
-from vivy_common import (HardsubSign, Range, SelfRunner, bounded_dehardsub, antialias, deband, denoise,  # noqa: E402
+from vivy_common import (SelfRunner, antialias, deband, denoise,  # noqa: E402
                          finalize, fsrcnnx_rescale, letterbox_edgefix, source)
 
 core = vs.core
diff --git a/Vivy/vivy_common/__init__.py b/Vivy/vivy_common/__init__.py
index 6789f14..475408f 100644
--- a/Vivy/vivy_common/__init__.py
+++ b/Vivy/vivy_common/__init__.py
@@ -1,3 +1,2 @@
-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, SelfRunner, glob_crc, source  # noqa: F401
+from .util import SelfRunner, glob_crc, source  # noqa: F401
diff --git a/Vivy/vivy_common/dehardsub.py b/Vivy/vivy_common/dehardsub.py
deleted file mode 100644
index d03787c..0000000
--- a/Vivy/vivy_common/dehardsub.py
+++ /dev/null
@@ -1,97 +0,0 @@
-import vapoursynth as vs
-import kagefunc as kgf
-import lvsfunc as lvf
-
-from typing import List, NamedTuple, Optional, Tuple, Union
-
-from .util import Range
-
-core = vs.core
-
-
-class Position(NamedTuple):
-    x: int
-    y: int
-
-
-class Size(NamedTuple):
-    x: int
-    y: int
-
-
-class BoundingBox():
-    pos: Position
-    size: Size
-
-    def __init__(self, pos: Position, size: Size):
-        self.pos = pos
-        self.size = size
-
-
-class HardsubSign():
-    range: Range
-    bound: Optional[BoundingBox]
-    refframe: Optional[int]
-    highpass: int
-    expand: int
-
-    def __init__(self,
-                 range: Range,
-                 bound: Union[BoundingBox, Tuple[Tuple[int, int], Tuple[int, int]], None],
-                 refframe: Optional[int] = None, highpass: int = 5000, expand: int = 8):
-        self.range = range
-        self.refframe = refframe
-        self.highpass = highpass
-        self.expand = expand
-        if bound is None:
-            self.bound = None
-        elif isinstance(bound, BoundingBox):
-            self.bound = bound
-        else:
-            self.bound = BoundingBox(Position(bound[0][0], bound[0][1]), Size(bound[1][0], bound[1][1]))
-
-    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=self.highpass, expand_n=self.expand)
-        else:
-            mask = kgf.hardsubmask_fades(hrdsb, ref, highpass=self.highpass, expand_n=self.expand)
-
-        assert isinstance(mask, vs.VideoNode)
-        return mask
-
-    def _bound_mask(self, ref: vs.VideoNode) -> vs.VideoNode:
-        if self.bound is not None:
-            mask = kgf.squaremask(ref, self.bound.size.x, self.bound.size.y,
-                                  self.bound.pos.x, self.bound.pos.y)
-        else:
-            mask = kgf.squaremask(ref, ref.width, ref.height, 0, 0)
-
-        assert isinstance(mask, vs.VideoNode)
-        return mask
-
-    def get_mask(self, hrdsb: vs.VideoNode, ref: vs.VideoNode) -> vs.VideoNode:
-        bm = self._bound_mask(ref)
-        hm = self._hardsub_mask(hrdsb, ref)
-        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).id)
-    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(bound, ref, sign.get_mask(hrdsb, ref)),
-                                        [sign.range])
-
-    return bound
diff --git a/Vivy/vivy_common/filter.py b/Vivy/vivy_common/filter.py
index 70b32d5..6d9b508 100644
--- a/Vivy/vivy_common/filter.py
+++ b/Vivy/vivy_common/filter.py
@@ -4,11 +4,10 @@ import lvsfunc as lvf
 import vardefunc as vdf
 
 from awsmfunc import bbmod
+from lvsfunc.types import Range
 from mvsfunc import BM3D
 from typing import List, Optional
 
-from .util import Range
-
 import os
 import vsutil
 
diff --git a/Vivy/vivy_common/util.py b/Vivy/vivy_common/util.py
index 7a3aba7..8f8032c 100644
--- a/Vivy/vivy_common/util.py
+++ b/Vivy/vivy_common/util.py
@@ -14,8 +14,6 @@ from typing import Any, BinaryIO, Callable, List, Optional, Sequence, Tuple, Uni
 
 core = vs.core
 
-Range = Union[int, Tuple[int, int]]
-
 TITLE: str = "Vivy"
 TITLE_LONG: str = f"{TITLE} - Fluorite Eye's Song"
 RESOLUTION: int = 1080