Skip to content

Instantly share code, notes, and snippets.

@RyougiKukoc
Last active November 26, 2022 19:39
Show Gist options
  • Save RyougiKukoc/47fa155a8b8ccce52f17c2ed81626dcb to your computer and use it in GitHub Desktop.
Save RyougiKukoc/47fa155a8b8ccce52f17c2ed81626dcb to your computer and use it in GitHub Desktop.
A function to reduce film grain, included in rksfunc, public here.
from vapoursynth import core, VideoNode
def defilmgrain(clip: VideoNode, s1=16, s2=3, s3=3, g=1.5, dark=10000) -> VideoNode:
from vapoursynth import YUV, GRAY, Error
from fvsfunc import Depth
from havsfunc import QTGMC
from muvsfunc import MergeChroma
from vsutil import get_y, iterate
from dfttest2 import DFTTest
def gamma_curve(clip_y, gamma) -> VideoNode:
def lut_y(x):
floa = min(max(int(x / 56283 * 65536), 0), 65535) / 65535
gammaed = floa ** gamma
return min(max(int(gammaed * 56283 + 4112), 4112), 60395)
return core.std.Lut(clip_y, planes=[0], function=lut_y)
if clip.format.color_family != YUV and clip.format.color_family != GRAY:
raise Error("rksfunc.defilmgrain: only YUV and GRAY format are supported.")
origdep = clip.format.bits_per_sample
if origdep != 16:
clip = Depth(clip, 16)
if clip.format.color_family == YUV:
clip_y = get_y(clip)
else:
clip_y = clip
clip_yf = Depth(clip_y, 32)
basic_f = clip_yf.bm3dcuda_rtc.BM3Dv2(clip_yf, s1, 3, 12, 0, 2, 8)
final_f = clip_yf.bm3dcuda_rtc.BM3Dv2(basic_f, s1, 3, 12, 0, 2, 8)
line_f = clip_yf.bm3dcuda_rtc.BM3Dv2(final_f, s2, 3, 15, 0, 2, 8)
line = Depth(line_f, 16)
clearplane = DFTTest(Depth(final_f, 16), tbsize=1)
emask = gamma_curve(clearplane, g).tcanny.TCanny(1, mode=0, op=3)
emask = iterate(emask, core.std.Maximum, 2)
emask = iterate(emask, core.std.Inflate, 2)
emask = core.std.Expr([emask, clearplane], f"y {dark} > 0 x ?")
tref = QTGMC(clip_y, InputType=1, Sharpness=0, SourceMatch=3)
ttp = clip_y.ttmpsm.TTempSmooth(3, 5, 3, 3, pfclip=tref)
ttp = core.std.MaskedMerge(ttp, line, emask)
ttp_f = Depth(ttp, 32)
vfinal_f = ttp_f.bm3dcuda_rtc.BM3Dv2(clip_yf, s3, 3, 12, 1, 2, 8)
vfinal = Depth(vfinal_f, 16)
if clip.format.color_family == YUV:
vfinal = MergeChroma(vfinal, clip)
if origdep != 16:
vfinal = Depth(vfinal, origdep)
return vfinal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment