From 2784de289a16e08d7b7bae25f202c1dfbd419e1f Mon Sep 17 00:00:00 2001
From: louis <louis@poweris.moe>
Date: Sun, 25 Apr 2021 09:50:00 -0400
Subject: [PATCH] vivy: tv: harder debanding

---
 Vivy/04/04.vpy                  |  7 +++++--
 Vivy/vivy_common/__init__.py    |  2 +-
 Vivy/vivy_common/filter.py      | 28 +++++++++++++++++++++++-----
 Vivy/vivy_common/final-settings |  2 +-
 4 files changed, 30 insertions(+), 9 deletions(-)

diff --git a/Vivy/04/04.vpy b/Vivy/04/04.vpy
index f86ef19..f2020a9 100644
--- a/Vivy/04/04.vpy
+++ b/Vivy/04/04.vpy
@@ -1,7 +1,7 @@
 import vapoursynth as vs
 
 from vivy_common import (VivyConfig, VivySource, antialias, deband, denoise,
-                         finalize, fsrcnnx_rescale, letterbox_edgefix)
+                         finalize, fsrcnnx_rescale, letterbox_edgefix, regrain)
 
 from yt_common.automation import SelfRunner
 from yt_common.source import waka_replace
@@ -14,6 +14,7 @@ from typing import List
 import os
 
 core = vs.core
+core.num_threads = 16
 
 
 EPNUM: int = int(os.path.basename(os.path.splitext(__file__)[0]))
@@ -43,6 +44,7 @@ def filter_basic() -> vs.VideoNode:
     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
 
 
@@ -52,7 +54,8 @@ def filter() -> vs.VideoNode:
     den = denoise(rescale)
     deb = deband(den)
     aa = antialias(deb, NOAA)
-    edgefix = letterbox_edgefix(aa, LETTERBOX)
+    grain = regrain(aa)
+    edgefix = letterbox_edgefix(grain, LETTERBOX)
     final = finalize(edgefix)
     final.set_output()
     return final
diff --git a/Vivy/vivy_common/__init__.py b/Vivy/vivy_common/__init__.py
index 850131e..242811f 100644
--- a/Vivy/vivy_common/__init__.py
+++ b/Vivy/vivy_common/__init__.py
@@ -1,2 +1,2 @@
-from .filter import antialias, deband, denoise, finalize, fsrcnnx_rescale, letterbox_edgefix  # noqa: F401
+from .filter import antialias, deband, denoise, finalize, fsrcnnx_rescale, letterbox_edgefix, regrain  # noqa: F401
 from .config import VivyConfig, VivySource  # noqa: F401
diff --git a/Vivy/vivy_common/filter.py b/Vivy/vivy_common/filter.py
index aebaea2..5a64b87 100644
--- a/Vivy/vivy_common/filter.py
+++ b/Vivy/vivy_common/filter.py
@@ -4,11 +4,13 @@ import lvsfunc as lvf
 import vardefunc as vdf
 
 from awsmfunc import bbmod
+from debandshit import f3kbilateral
 from lvsfunc.types import Range
 from mvsfunc import BM3D
 from typing import List, Optional
 
 from yt_common import antialiasing
+from yt_common.deband import morpho_mask
 
 import os
 import vsutil
@@ -38,14 +40,24 @@ def letterbox_edgefix(clip: vs.VideoNode, ranges: List[Range]) -> vs.VideoNode:
     return lvf.misc.replace_ranges(clip, edgefix, ranges)
 
 
-def denoise(clip: vs.VideoNode, sigma: float = 0.75) -> vs.VideoNode:
+def denoise(clip: vs.VideoNode, sigma: float = 1.5) -> vs.VideoNode:
     bm3d: vs.VideoNode = BM3D(clip, sigma=sigma, depth=16)
     return bm3d
 
 
 def deband(clip: vs.VideoNode) -> vs.VideoNode:
-    deb: vs.VideoNode = vdf.dumb3kdb(clip, radius=18, threshold=36, grain=[24, 0])
-    return deb
+    grad_mask, yrangebig = morpho_mask(clip.dfttest.DFTTest(sigma=14, sigma2=10, sbsize=1, sosize=0)
+                                       .rgvs.RemoveGrain(3))
+    y = vsutil.get_y(clip)
+    mask = lvf.mask.detail_mask(clip)
+    deband_dumb: vs.VideoNode = vdf.dumb3kdb(clip)
+    deband_weak: vs.VideoNode = core.std.MaskedMerge(vsutil.get_y(deband_dumb), y, mask)
+    deband_norm: vs.VideoNode = f3kbilateral(y, y=36)
+    deband_strong: vs.VideoNode = f3kbilateral(y, y=65)
+    deband = core.std.MaskedMerge(deband_strong, deband_norm, grad_mask)
+    deband = core.std.MaskedMerge(deband, deband_weak, yrangebig)
+    deband = core.std.ShufflePlanes([deband, deband_dumb], planes=[0, 1, 2], colorfamily=vs.YUV)
+    return deband
 
 
 def antialias(clip: vs.VideoNode, noaa: Optional[List[Range]] = None) -> vs.VideoNode:
@@ -53,6 +65,12 @@ def antialias(clip: vs.VideoNode, noaa: Optional[List[Range]] = None) -> vs.Vide
     return lvf.misc.replace_ranges(clamp, clip, noaa) if noaa else clamp
 
 
+def regrain(clip: vs.VideoNode) -> vs.VideoNode:
+    sgrain: vs.VideoNode = kgf.adaptive_grain(clip, 0.15, luma_scaling=10)
+    dgrain: vs.VideoNode = kgf.adaptive_grain(clip, 0.1, luma_scaling=25, static=False)
+    grain = dgrain.std.MergeDiff(clip.std.MakeDiff(sgrain))
+    return grain
+
+
 def finalize(clip: vs.VideoNode) -> vs.VideoNode:
-    grain = kgf.adaptive_grain(clip, 0.1)
-    return vsutil.depth(grain, 10)
+    return vsutil.depth(clip, 10)
diff --git a/Vivy/vivy_common/final-settings b/Vivy/vivy_common/final-settings
index 80939b5..b05f935 100644
--- a/Vivy/vivy_common/final-settings
+++ b/Vivy/vivy_common/final-settings
@@ -1 +1 @@
-x265 --input - --y4m --input-depth 10 --output-depth 10 --input-csp i420 --profile main10 --colormatrix bt709 --colorprim bt709 --transfer bt709 --preset slower --rc-lookahead 72 --keyint 360 --min-keyint 23 --subme 5 --qcomp 0.7 --crf 15 --aq-mode 3 --aq-strength 0.9 --bframes 16 --psy-rd 0.95 --psy-rdoq 1.8 --rdoq-level 1 --deblock -2:-2 --no-sao --no-open-gop --frames {frames:d} --output {filename:s}.h265
+x265 --input - --y4m --input-depth 10 --output-depth 10 --input-csp i420 --profile main10 --colormatrix bt709 --colorprim bt709 --transfer bt709 --preset slower --rc-lookahead 72 --keyint 360 --min-keyint 23 --subme 5 --qcomp 0.7 --crf 15 --aq-mode 3 --aq-strength 0.9 --bframes 16 --psy-rd 2.0 --psy-rdoq 1.8 --rdoq-level 1 --deblock -2:-2 --no-sao --no-open-gop --no-wpp --frames {frames:d} --output {filename:s}.h265