From f506bb3e8c675e3f9cad498c754fd7947bbeab26 Mon Sep 17 00:00:00 2001
From: louis <louis@poweris.moe>
Date: Tue, 11 May 2021 09:38:36 -0400
Subject: [PATCH] vivy: tv: 07

---
 Vivy/07/07.vpy                    | 121 ++++++++++++++++++++++++++++++
 Vivy/vivy_common/config.py        |   7 +-
 yt_common/yt_common/automation.py |  12 ++-
 yt_common/yt_common/source.py     |   7 +-
 4 files changed, 138 insertions(+), 9 deletions(-)
 create mode 100644 Vivy/07/07.vpy

diff --git a/Vivy/07/07.vpy b/Vivy/07/07.vpy
new file mode 100644
index 0000000..1267645
--- /dev/null
+++ b/Vivy/07/07.vpy
@@ -0,0 +1,121 @@
+import vapoursynth as vs
+
+from vivy_common import (VivyConfig, VivySource, antialias, deband, denoise,
+                         finalize, fsrcnnx_rescale, letterbox_edgefix, regrain)
+
+from yt_common.automation import SelfRunner
+from yt_common.denoise import bm3d
+from yt_common.source import waka_replace
+
+from lvsfunc.types import Range
+from lvsfunc.dehardsub import HardsubLine, HardsubLineFade, HardsubSign, HardsubSignFade, HardsubMask, bounded_dehardsub
+from lvsfunc.misc import replace_ranges
+from vardefunc import dumb3kdb
+
+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]] = [
+    [(31528, 32857)],
+    [],
+]
+SIGNS_RU: List[HardsubMask] = [
+    HardsubSignFade([
+        (2834, 2941),
+        (16031, 16035),
+    ]),
+    HardsubSignFade([
+        (33949, 34045),
+    ], refframe=1, expand=9),
+    HardsubSignFade([
+        (21060, 21113),
+    ], refframe=0),
+    HardsubSignFade([
+        (21107, 21132),
+    ], refframe=1),
+    HardsubSign([
+        (7079, 7111),
+    ]),
+    HardsubLineFade([
+        (21508, 21572),
+    ]),
+    HardsubLine([
+        (961, 1106),
+        (1111, 1252),
+        (1265, 1408),
+        (1414, 1502),
+        (1509, 1578),
+        (1583, 1657),
+        (1663, 1815),
+        (1862, 2006),
+        (2011, 2079),
+        (2084, 2171),
+        (2179, 2309),
+        (2313, 2382),
+        (2387, 2486),
+        (2491, 2556),
+        (2570, 2686),
+        (12150, 12261),
+        (12279, 12403),
+        (12413, 12525),
+        (12530, 12633),
+        (12637, 12783),
+        (12789, 12891),
+        (12896, 13049),
+        (13054, 13116),
+        (13121, 13255),
+        (14930, 14990),
+    ], ((283, 844), (1349, 204))),
+]
+NOSCALE: List[Range] = []
+AA_NONE: List[Range] = [
+    (14413, 14422),
+    (15428, 15431),
+]
+AA_STRONGER: List[Range] = [
+    (11972, 12080),
+]
+DB_WEAK: List[Range] = [
+    (0, 579),
+]
+
+LETTERBOX: List[Range] = []
+
+
+def filter_basic() -> vs.VideoNode:
+    wakas, ref = SOURCE.source()
+    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()
+    rescale = fsrcnnx_rescale(src, NOSCALE)
+    den = denoise(rescale)
+    deb = deband(den)
+    den2 = bm3d(rescale, sigma=4, radius=1)
+    deb2 = dumb3kdb(den2, radius=16, threshold=24)
+    deb = replace_ranges(deb, deb2, DB_WEAK)
+    aa = antialias(deb, stronger=AA_STRONGER, noaa=AA_NONE)
+    grain = regrain(aa)
+    edgefix = letterbox_edgefix(grain, LETTERBOX)
+    final = finalize(edgefix)
+    final.set_output()
+    return final
+
+
+if __name__ == "__main__":
+    SelfRunner(CONFIG, filter, filter_basic)
+else:
+    filter()
diff --git a/Vivy/vivy_common/config.py b/Vivy/vivy_common/config.py
index 4576b97..4e1a896 100644
--- a/Vivy/vivy_common/config.py
+++ b/Vivy/vivy_common/config.py
@@ -1,8 +1,9 @@
 import vapoursynth as vs
 
 from yt_common import source
-from yt_common.logging import log
 from yt_common.config import Config
+from yt_common.logging import log
+from yt_common.source import AMAZON_FILENAME_VBR
 
 import os
 
@@ -38,11 +39,11 @@ class VivySource(source.FunimationSource):
         # ep1-3 have good funi video, let's just use that
         if self.config.epnum < 4:
             raise FileNotFoundError()
-        if not os.path.isfile(self.config.format_filename(AMAZON_FILENAME)):
+        if not os.path.isfile(self.config.format_filename(AMAZON_FILENAME_VBR)):
             log.warn("Amazon not found, falling back to Funimation")
             raise FileNotFoundError()
         log.success("Found Amazon video")
-        return core.ffms2.Source(self.config.format_filename(AMAZON_FILENAME))
+        return core.ffms2.Source(self.config.format_filename(AMAZON_FILENAME_VBR))
 
     def get_waka_filenames(self) -> List[str]:
         return [self.config.format_filename(f) for f in [
diff --git a/yt_common/yt_common/automation.py b/yt_common/yt_common/automation.py
index 748da57..86c79a7 100644
--- a/yt_common/yt_common/automation.py
+++ b/yt_common/yt_common/automation.py
@@ -12,7 +12,7 @@ from typing import Any, BinaryIO, Callable, List, Optional, Sequence, Union, cas
 
 from .config import Config
 from .logging import log
-from .source import AMAZON_FILENAME, ER_FILENAME, SUBSPLS_FILENAME, FUNI_INTRO, glob_filename
+from .source import AMAZON_FILENAME_CBR, AMAZON_FILENAME_VBR, ER_FILENAME, SUBSPLS_FILENAME, FUNI_INTRO, glob_filename
 
 core = vs.core
 
@@ -135,8 +135,14 @@ class AudioGetter():
             return
 
         # look for amazon first
-        if os.path.isfile(self.config.format_filename(AMAZON_FILENAME)):
-            self.audio_file = self.config.format_filename(AMAZON_FILENAME)
+        if os.path.isfile(self.config.format_filename(AMAZON_FILENAME_CBR)):
+            self.audio_file = self.config.format_filename(AMAZON_FILENAME_CBR)
+            self.video_src = core.ffms2.Source(self.audio_file)
+            log.success("Found Amazon audio")
+            return
+
+        if os.path.isfile(self.config.format_filename(AMAZON_FILENAME_VBR)):
+            self.audio_file = self.config.format_filename(AMAZON_FILENAME_VBR)
             self.video_src = core.ffms2.Source(self.audio_file)
             log.success("Found Amazon audio")
             return
diff --git a/yt_common/yt_common/source.py b/yt_common/yt_common/source.py
index b0318b6..e4a7c1c 100644
--- a/yt_common/yt_common/source.py
+++ b/yt_common/yt_common/source.py
@@ -19,7 +19,8 @@ core = vs.core
 SUBSPLS_FILENAME: str = "[SubsPlease] {title_long} - {epnum:02d} ({resolution}p) [$GLOB].mkv"
 ER_FILENAME: str = "[Erai-raws] {title_long} - {epnum:02d} [v0][{resolution}p]$GLOB.mkv"
 FUNI_INTRO: int = 289
-AMAZON_FILENAME: str = "{title_long} - {epnum:02d} (Amazon Prime CBR {resolution}p).mkv"
+AMAZON_FILENAME_CBR: str = "{title_long} - {epnum:02d} (Amazon Prime CBR {resolution}p).mkv"
+AMAZON_FILENAME_VBR: str = "{title_long} - {epnum:02d} (Amazon Prime VBR {resolution}p).mkv"
 
 
 def waka_replace(src: vs.VideoNode, wakas: List[vs.VideoNode], ranges: List[List[Range]]
@@ -75,11 +76,11 @@ class FunimationSource(DehardsubFileFinder):
         super().__init__(*args, **kwargs)
 
     def get_amazon(self) -> vs.VideoNode:
-        if not os.path.isfile(self.config.format_filename(AMAZON_FILENAME)):
+        if not os.path.isfile(self.config.format_filename(AMAZON_FILENAME_CBR)):
             log.warn("Amazon not found, falling back to Funimation")
             raise FileNotFoundError()
         log.success("Found Amazon video")
-        return core.ffms2.Source(self.config.format_filename(AMAZON_FILENAME))
+        return core.ffms2.Source(self.config.format_filename(AMAZON_FILENAME_CBR))
 
     def get_funi_filename(self) -> str:
         try: