From 5ca5bf799d199928643ca87899f76dcaa89fbf4b Mon Sep 17 00:00:00 2001
From: louis <louis@poweris.moe>
Date: Tue, 1 Jun 2021 09:28:08 -0400
Subject: [PATCH] vivy: tv: 10

---
 Vivy/10/10.vpy             | 81 ++++++++++++++++++++++++++++++++++++++
 Vivy/vivy_common/filter.py | 42 +++++++++++++++-----
 2 files changed, 113 insertions(+), 10 deletions(-)
 create mode 100644 Vivy/10/10.vpy

diff --git a/Vivy/10/10.vpy b/Vivy/10/10.vpy
new file mode 100644
index 0000000..7ce9f96
--- /dev/null
+++ b/Vivy/10/10.vpy
@@ -0,0 +1,81 @@
+import vapoursynth as vs
+
+from vivy_common import (VivyConfig, VivySource, antialias, deband, denoise,
+                         finalize, fsrcnnx_rescale, letterbox_edgefix, letterbox_refix, regrain)
+
+from yt_common.automation import SelfRunner
+from yt_common.source import waka_replace
+
+from lvsfunc.types import Range
+from lvsfunc.dehardsub import HardsubLine, HardsubSignFade, HardsubMask, bounded_dehardsub
+
+from typing import List
+
+import os
+
+core = vs.core
+
+
+EPNUM: int = int(os.path.basename(os.path.splitext(__file__)[0]))
+CONFIG: VivyConfig = VivyConfig(EPNUM)
+SOURCE: VivySource = VivySource(CONFIG)
+
+WAKA_REPLACE: List[List[Range]] = [
+    [(30354, 31683)],
+    [],
+]
+SIGNS_RU: List[HardsubMask] = [
+    HardsubLine([
+        (98, 189),
+        (2931, 5045),
+    ], ((283, 844), (1349, 204))),
+    HardsubSignFade([
+        (30347, 30353),
+    ], refframe=1),
+    HardsubSignFade([
+        (5094, 5214),
+        (33950, 34046),
+    ]),
+]
+NOSCALE: List[Range] = [
+]
+AA_NONE: List[Range] = [
+]
+AA_STRONGER: List[Range] = [
+]
+LETTERBOX: List[Range] = [
+    (11509, 13149),
+    (25136, 25244),
+]
+LETTERBOX_FADES: List[Range] = [
+]
+
+
+def filter_basic() -> vs.VideoNode:
+    wakas, ref = SOURCE.dhs_source()
+    wakas = [w[:33687] + w[33696:] for w in wakas]
+    waka = wakas[0]
+    waka, wakas = waka_replace(waka, wakas[1:], WAKA_REPLACE)
+    src = bounded_dehardsub(waka, ref, SIGNS_RU, wakas)
+    src.set_output(1)
+    return src
+
+
+def filter() -> vs.VideoNode:
+    src = filter_basic()
+    den = denoise(src)
+    rescale = fsrcnnx_rescale(den, NOSCALE)
+    edgefix = letterbox_edgefix(rescale, crops=LETTERBOX, fades=LETTERBOX_FADES)
+    deb = deband(edgefix)
+    aa = antialias(deb, stronger=AA_STRONGER, noaa=AA_NONE)
+    refix = letterbox_refix(aa, deb, LETTERBOX)
+    grain = regrain(refix)
+    final = finalize(grain)
+    final.set_output()
+    return final
+
+
+if __name__ == "__main__":
+    SelfRunner(CONFIG, SOURCE, filter, filter_basic)
+else:
+    filter()
diff --git a/Vivy/vivy_common/filter.py b/Vivy/vivy_common/filter.py
index 1484887..efa1a03 100644
--- a/Vivy/vivy_common/filter.py
+++ b/Vivy/vivy_common/filter.py
@@ -5,10 +5,10 @@ import vardefunc as vdf
 
 from awsmfunc import bbmod
 from debandshit import f3kbilateral
+from functools import partial
 from lvsfunc.aa import upscaled_sraa
 from lvsfunc.denoise import bm3d
-from lvsfunc.kernels import Bicubic
-from lvsfunc.misc import replace_ranges
+from lvsfunc.misc import replace_ranges, scale_thresh
 from lvsfunc.types import Range
 from typing import List, Optional
 
@@ -43,11 +43,31 @@ def _fixplane(clip: vs.VideoNode, top: int, bottom: int,
                                    bbmod(clip.std.Crop(top=clip.height-bottom), top=1 if not chroma else 0)])
 
 
-def letterbox_edgefix(clip: vs.VideoNode, ranges: List[Range]) -> vs.VideoNode:
-    fy = _fixplane(clip.std.ShufflePlanes(planes=0, colorfamily=vs.GRAY), top=132, bottom=131, bbt=3, bbb=3)
-    fu = _fixplane(clip.std.ShufflePlanes(planes=1, colorfamily=vs.GRAY), top=66, bottom=65, bbt=2, bbb=2, chroma=True)
-    fv = _fixplane(clip.std.ShufflePlanes(planes=2, colorfamily=vs.GRAY), top=66, bottom=65, bbt=2, bbb=2, chroma=True)
-    return replace_ranges(clip, core.std.ShufflePlanes([fy, fu, fv], planes=[0, 0, 0], colorfamily=vs.YUV), ranges)
+def letterbox_edgefix(clip: vs.VideoNode, crops: Optional[List[Range]] = None,
+                      fades: Optional[List[Range]] = None) -> vs.VideoNode:
+    assert clip.format is not None
+    fixed = clip
+    if fades:
+        fy = _fixplane(clip.std.ShufflePlanes(planes=0, colorfamily=vs.GRAY),
+                       top=132, bottom=131, bbt=2, bbb=2)
+        fu = _fixplane(clip.std.ShufflePlanes(planes=1, colorfamily=vs.GRAY),
+                       top=66, bottom=65, bbt=1, bbb=2, chroma=True)
+        fv = _fixplane(clip.std.ShufflePlanes(planes=2, colorfamily=vs.GRAY),
+                       top=66, bottom=66, bbt=1, bbb=2, chroma=True)
+        f = core.std.ShufflePlanes([fy, fu, fv], planes=[0, 0, 0], colorfamily=vs.YUV)
+        fixed = replace_ranges(fixed, f, fades)
+    if crops:
+        black = [
+            vsutil.scale_value(0, 8, clip.format.bits_per_sample, range_in=vsutil.Range.FULL,
+                               range=vsutil.Range.LIMITED, scale_offsets=True),
+            scale_thresh(0.5, clip),
+            scale_thresh(0.5, clip),
+        ]
+        crop = clip.std.Crop(top=132, bottom=132)
+        bb = bbmod(crop, top=2, bottom=2, blur=500)
+        f = bb.std.AddBorders(top=132, bottom=132, color=black)
+        fixed = replace_ranges(fixed, f, crops)
+    return fixed
 
 
 def letterbox_refix(aa: vs.VideoNode, noaa: vs.VideoNode, ranges: List[Range]) -> vs.VideoNode:
@@ -84,10 +104,12 @@ def deband(clip: vs.VideoNode) -> vs.VideoNode:
 
 def antialias(clip: vs.VideoNode, weak: Optional[List[Range]] = None, strong: Optional[List[Range]] = None,
               stronger: Optional[List[Range]] = None, noaa: Optional[List[Range]] = None) -> vs.VideoNode:
-    mask = antialiasing.combine_mask(clip, weak or [])
+    mask = partial(antialiasing.combine_mask, weak=weak or [])
     clamp = antialiasing.sraa_clamp(clip, mask=mask)
-    sraa = core.std.MaskedMerge(clip, upscaled_sraa(clip, rfactor=2, downscaler=Bicubic(b=0, c=1/2).scale), mask)
-    sraa_13 = core.std.MaskedMerge(clip, upscaled_sraa(clip, rfactor=1.3, downscaler=Bicubic(b=0, c=1/2).scale), mask)
+    sraa = upscaled_sraa(clip, rfactor=2)
+    sraa = core.std.MaskedMerge(clip, sraa, mask(sraa))
+    sraa_13 = upscaled_sraa(clip, rfactor=1.3)
+    sraa_13 = core.std.MaskedMerge(clip, sraa_13, mask(sraa_13))
     return replace_ranges(replace_ranges(replace_ranges(clamp, clip, noaa or []), sraa, strong or []), sraa_13,
                           stronger or [])