def nneedi3_clamp(clip: vs.VideoNode, strength: int = 1, mask: Optional[vs.VideoNode] = None, ret_mask: bool = False, show_mask: bool = False, opencl: bool = False) -> vs.VideoNode: """ A function that clamps eedi3 to nnedi3 for the purpose of reducing eedi3 artifacts. This should fix every issue created by eedi3. For example: https://i.imgur.com/hYVhetS.jpg Original function written by Zastin, modified by LightArrowsEXE. Dependencies: * kagefunc (optional: retinex edgemask) * vapoursynth-retinex (optional: retinex edgemask) * vapoursynth-tcanny (optional: retinex edgemask) * vapoursynth-eedi3 * vapoursynth-nnedi3 or znedi3 * vapoursynth-nnedi3cl (optional: opencl) * vsTAAmbk :param clip: Input clip :param strength: Set threshold strength (Default: 1) :param mask: Clip to use for custom mask (Default: None) :param ret_mask: Replace default mask with a retinex edgemask (Default: False) :param show_mask: Return mask instead of clip (Default: False) :param opencl: OpenCL acceleration (Default: False) :return: Antialiased clip """ try: from vsTAAmbk import TAAmbk except ModuleNotFoundError: raise ModuleNotFoundError( "nnedi3_clamp: missing dependency 'vsTAAmbk'") bits = clip.format.bits_per_sample - 8 thr = strength * (1 >> bits) strong = TAAmbk(clip, aatype='Eedi3', alpha=0.25, beta=0.5, gamma=40, nrad=2, mdis=20, mtype=0, opencl=opencl) weak = TAAmbk(clip, aatype='Nnedi3', nsize=3, nns=3, qual=1, mtype=0, opencl=opencl) expr = 'x z - y z - * 0 < y x y {0} + min y {0} - max ?'.format(thr) if clip.format.num_planes > 1: expr = [expr, ''] aa = core.std.Expr([strong, weak, clip], expr) if mask: merged = clip.std.MaskedMerge(aa, mask, planes=0) elif ret_mask: try: import kagefunc as kgf except ModuleNotFoundError: raise ModuleNotFoundError( "nnedi3_clamp: missing dependency 'kagefunc'") mask = kgf.retinex_edgemask(clip, 1).std.Binarize() merged = clip.std.MaskedMerge(aa, mask, planes=0) else: mask = clip.std.Prewitt(planes=0).std.Binarize(planes=0).std.Maximum( planes=0).std.Convolution([1] * 9, planes=0) mask = get_y(mask) merged = clip.std.MaskedMerge(aa, mask, planes=0) if show_mask: return mask return merged if clip.format.color_family == vs.GRAY else core.std.ShufflePlanes( [merged, clip], [0, 1, 2], vs.YUV)
def do_filter(): """Vapoursynth filtering""" def _sraa(clip: vs.VideoNode, nnargs: dict, eeargs: dict) -> vs.VideoNode: def _nnedi3(clip): return clip.nnedi3.nnedi3(0, False, **nnargs) def _eedi3(clip, sclip): return clip.eedi3m.EEDI3(0, False, **eeargs, sclip=sclip) clip = _eedi3(clip, _nnedi3(clip)).std.Transpose() clip = _eedi3(clip, _nnedi3(clip)).std.Transpose() return clip def _nnedi3(clip: vs.VideoNode, factor: float, args: dict) -> vs.VideoNode: upscale = clip.std.Transpose().nnedi3.nnedi3(0, True, **args) \ .std.Transpose().nnedi3.nnedi3(0, True, **args) return core.resize.Spline36(upscale, clip.width * factor, clip.height * factor, src_top=.5, src_left=.5) def area_resize(clip: vs.VideoNode, width: int, height: int) -> vs.VideoNode: blank = core.std.BlankClip(clip, format=vs.GRAY16, color=128 << 8) clip = join([clip, blank, blank]) clip = core.area.AreaResize(clip, width, height) return get_y(clip) src = JPBD.src_cut src = depth(src, 16) flashback = [(31665, 31987), (68524, 68569)] denoise = CoolDegrain(src, tr=1, thsad=48, blksize=8, overlap=4, plane=4) denoise = lvf.rfs(denoise, src, flashback) dering = hvf.EdgeCleaner(denoise, 10, rmode=3, smode=1, hot=True) luma = get_y(dering) line_mask = TAAmbk(luma, mtype=2, showmask=1).std.Inflate() upscale = _nnedi3(luma, 2, dict(nsize=4, nns=4, qual=2, pscrn=2)) sraa = _sraa(upscale, dict(nsize=0, nns=3, qual=1, pscrn=1), dict(alpha=0.2, beta=0.5, gamma=40, nrad=3, mdis=20)) sraa = core.rgvs.Repair(sraa, upscale, 13) antialias = area_resize(sraa, src.width, src.height) antialias = lvf.rfs(core.std.MaskedMerge(luma, antialias, line_mask), antialias, [(22503, 22789)]) antialias_merged = vdf.merge_chroma(antialias, denoise) deband_mask_a = lvf.denoise.detail_mask(antialias_merged, brz_a=3000, brz_b=1200) deband_mask_b = kgf.retinex_edgemask(antialias_merged) deband_mask = core.std.Expr([deband_mask_a, deband_mask_b], 'x y +') deband = dbs.f3kpf(antialias_merged, 18, 30, 30) deband = core.std.MaskedMerge(deband, antialias_merged, deband_mask) deband = hvf.ContraSharpening(deband, antialias_merged) deband = lvf.rfs(deband, antialias_merged, flashback) grain = core.neo_f3kdb.Deband(deband, preset='depth', grainy=24) grain_a = kgf.adaptive_grain(grain) grain_b = core.grain.Add(grain, 1, 0, constant=True) grain = lvf.rfs(grain_a, grain_b, flashback) mask_borders = core.std.BlankClip(grain, format=vs.GRAY16, color=(256 << 8) - 1) mask_borders = vdf.region_mask(mask_borders, 0, 0, 132, 132).std.Invert() final = lvf.rfs(grain, core.std.MaskedMerge(grain, src, mask_borders), flashback + [(65388, 65413), (113621, 113763)]) smooth = xvs.mvfrc(src[146178:src.num_frames], preset='slow') vfr = muvf.VFRSplice([final[:146178], smooth], 'sora_timecode.txt') return depth(vfr, 10)
def do_filter(): """Vapoursynth filtering""" def _nneedi3_clamp(clip: vs.VideoNode, strength: int = 1)-> vs.VideoNode: bits = clip.format.bits_per_sample - 8 thr = strength * (1 >> bits) luma = get_y(clip) def _strong(clip: vs.VideoNode)-> vs.VideoNode: args = dict(alpha=0.25, beta=0.5, gamma=40, nrad=2, mdis=20, vcheck=3) clip = core.eedi3m.EEDI3(clip, 1, True, **args).std.Transpose() clip = core.eedi3m.EEDI3(clip, 1, True, **args).std.Transpose() return core.resize.Spline36(clip, luma.width, luma.height, src_left=-.5, src_top=-.5) def _weak(clip: vs.VideoNode)-> vs.VideoNode: args = dict(nsize=3, nns=2, qual=2) clip = core.znedi3.nnedi3(clip, 1, True, **args).std.Transpose() clip = core.znedi3.nnedi3(clip, 1, True, **args).std.Transpose() return core.resize.Spline36(clip, luma.width, luma.height, src_left=-.5, src_top=-.5) clip_aa = core.std.Expr([_strong(luma), _weak(luma), luma], 'x z - y z - * 0 < y x y {0} + min y {0} - max ?'.format(thr)) return vdf.merge_chroma(clip_aa, clip) def _perform_endcard(path: str, ref: vs.VideoNode)-> vs.VideoNode: endcard = lvf.src(path).std.AssumeFPS(ref) endcard = core.std.CropRel(endcard, left=7, top=0, right=11, bottom=22) endcard = core.resize.Bicubic(endcard, ref.width, ref.height, vs.RGBS, dither_type='error_diffusion') endcard = core.w2xc.Waifu2x(endcard, noise=3, scale=1, photo=True) endcard = core.resize.Bicubic(endcard, format=vs.YUV444PS, matrix_s='709', dither_type='error_diffusion') endcard = lvf.util.quick_resample(endcard, lambda c: core.neo_f3kdb.Deband(c, 15, 36, 36, 36, 24, 24, 4)) return Tweak(endcard, sat=1.4, cont=1.2) def _perform_motion_mask(clip: vs.VideoNode, brz: int)-> vs.VideoNode: clip = depth(clip, 8) sup = core.hqdn3d.Hqdn3d(clip).neo_fft3d.FFT3D().mv.Super(sharp=1) fv1 = core.mv.Analyse(sup, isb=False, delta=1, truemotion=False, dct=2) fv2 = core.mv.Analyse(sup, isb=True, delta=1, truemotion=True, dct=2) momask1 = core.mv.Mask(clip, fv1, ml=2, kind=1) momask2 = core.mv.Mask(clip, fv2, ml=3, kind=1) momask = core.std.Merge(momask1, momask2).rgvs.RemoveGrain(3).std.Binarize(brz) momask = momask.std.Minimum().std.Minimum() return depth(get_y(momask), 16) src = JPBD.src_cut src = depth(src, 16) denoise_a = CoolDegrain(src, tr=2, thsad=48, blksize=8, overlap=4, plane=4) denoise = denoise_a antialias = _nneedi3_clamp(denoise) antialias_a = TAAmbk(antialias, aatype='Eedi3', cycle=3) antialias_b = lvf.sraa(denoise, 2, 13) motion_mask = _perform_motion_mask(denoise[16914:17001], 140).std.FreezeFrames([0, 18, 57], [2, 38, 86], [2, 38, 57]) motion_mask = insert_clip(src.std.BlankClip(format=vs.GRAY16), motion_mask, 16914) antialias = lvf.rfs(antialias, core.std.MaskedMerge(antialias_a, antialias, motion_mask), [(16914, 17000)]) antialias = lvf.rfs(antialias, antialias_b, [(18084, 18179)]) predenoise = mClean(antialias, thSAD=200, chroma=False) detail_mask = lvf.denoise.detail_mask(predenoise, rad=2, radc=2, brz_a=3250, brz_b=1250) ret_mask = kgf.retinex_edgemask(predenoise).std.Binarize(9250).std.Median().std.Inflate() line_mask = core.std.Expr([detail_mask, ret_mask], 'x y max') deband_a = core.neo_f3kdb.Deband(antialias, 17, 42, 42, 42, 12, 0, sample_mode=4, keep_tv_range=True) deband_a = core.std.MaskedMerge(deband_a, antialias, line_mask) deband = deband_a grain = kgf.adaptive_grain(deband, 0.25, luma_scaling=10) ending = lvf.rfs(grain, src, [(32079, 33098)]) endcard = _perform_endcard(r'[BDMV][200902][Magia Record][Vol.5]\Scans\endcard13_front_descreen.png', src) endcard_length = 119 final = core.std.Splice([ending, endcard * endcard_length], True) final = core.resize.Bicubic(final, format=vs.YUV420P10, dither_type='error_diffusion') final = core.std.Limiter(final, 16, [235 << 2, 240 << 2]) return depth(final, 10), endcard_length
def do_filter(): """Vapoursynth filtering""" def _nneedi3_clamp(clip: vs.VideoNode, strength: int = 1) -> vs.VideoNode: bits = clip.format.bits_per_sample - 8 thr = strength * (1 >> bits) luma = get_y(clip) def _strong(clip: vs.VideoNode) -> vs.VideoNode: args = dict(alpha=0.25, beta=0.5, gamma=40, nrad=2, mdis=20, vcheck=3) clip = core.eedi3m.EEDI3(clip, 1, True, **args).std.Transpose() clip = core.eedi3m.EEDI3(clip, 1, True, **args).std.Transpose() return core.resize.Spline36(clip, luma.width, luma.height, src_left=-.5, src_top=-.5) def _weak(clip: vs.VideoNode) -> vs.VideoNode: args = dict(nsize=3, nns=2, qual=2) clip = core.znedi3.nnedi3(clip, 1, True, **args).std.Transpose() clip = core.znedi3.nnedi3(clip, 1, True, **args).std.Transpose() return core.resize.Spline36(clip, luma.width, luma.height, src_left=-.5, src_top=-.5) clip_aa = core.std.Expr( [_strong(luma), _weak(luma), luma], 'x z - y z - * 0 < y x y {0} + min y {0} - max ?'.format(thr)) return vdf.merge_chroma(clip_aa, clip) def _perform_endcard(path: str, ref: vs.VideoNode) -> vs.VideoNode: endcard = lvf.src(path).std.AssumeFPS(ref) endcard = core.std.CropRel(endcard, left=4, top=12, right=0, bottom=21) endcard = core.resize.Bicubic(endcard, ref.width, ref.height, vs.RGBS, dither_type='error_diffusion') endcard = iterate( endcard, partial(core.w2xc.Waifu2x, noise=3, scale=1, photo=True), 2) endcard = core.resize.Bicubic(endcard, format=vs.YUV444PS, matrix_s='709', dither_type='error_diffusion') endcard = lvf.util.quick_resample( endcard, lambda c: core.neo_f3kdb.Deband(c, 15, 36, 36, 36, 24, 24, 4)) return Tweak(endcard, sat=1.2, bright=-0.05, cont=1.2) def to_gray(clip: vs.VideoNode, ref: vs.VideoNode) -> vs.VideoNode: clip = core.std.AssumeFPS(clip, ref) return core.resize.Point(clip, format=vs.GRAY16, matrix_s=mvf.GetMatrix(ref)) src = JPBD.src_cut src = depth(src, 16) edstart = 30618 denoise_a = CoolDegrain(src, tr=2, thsad=48, blksize=8, overlap=4, plane=4) denoise_b = CoolDegrain(src, tr=3, thsad=96, blksize=8, overlap=4, plane=4) denoise = lvf.rfs(denoise_a, denoise_b, [(edstart + 1870, edstart + 1900)]) antialias_a = _nneedi3_clamp(denoise) antialias_b = TAAmbk(denoise, aatype='Eedi3', mtype=1) antialias = lvf.rfs(antialias_a, antialias_b, [(9478, 9501)]) predenoise = mClean(antialias, thSAD=200, chroma=False) detail_mask = lvf.denoise.detail_mask(predenoise, rad=2, radc=2, brz_a=3250, brz_b=1250) ret_mask = kgf.retinex_edgemask(predenoise).std.Binarize( 9250).std.Median().std.Inflate() line_mask = core.std.Expr([detail_mask, ret_mask], 'x y max') deband_a = core.neo_f3kdb.Deband(antialias, 17, 42, 42, 42, 12, 0, sample_mode=4, keep_tv_range=True) deband_a = core.std.MaskedMerge(deband_a, antialias, line_mask) deband_b = core.neo_f3kdb.Deband(deband_a, 18, 48, 48, 48, 0, 0, sample_mode=2, keep_tv_range=True) deband_c = core.neo_f3kdb.Deband(deband_a, 20, 64, 64, 64, 24, 0, sample_mode=2, keep_tv_range=True) deband = lvf.rfs(deband_a, deband_b, [(edstart + 1870, edstart + 1900)]) deband = lvf.rfs( deband, core.std.MaskedMerge( deband_c, deband_a, to_gray(lvf.src('masks/magia_07_mask_01.png'), src)), [(6729, 6865)]) grain = kgf.adaptive_grain(deband, 0.25, luma_scaling=10) borders = vdf.region_mask(grain, 240, 240, 0, 0) borders = lvf.rfs(grain, borders, [(25623, 25670)]) endcard = _perform_endcard( '[BDMV][200610][Magia Record][Vol.3]/Scans/endcard7_front_descreen.png', src) endcard_length = 119 final = core.std.Splice([borders, endcard * endcard_length], True) final = core.resize.Bicubic(final, format=vs.YUV420P10, dither_type='error_diffusion') final = core.std.Limiter(final, 16, [235 << 2, 240 << 2]) return depth(final, 10), endcard_length