def line_darkening(clip: vs.VideoNode, strength: float = 0.2, **kwargs) -> vs.VideoNode: """Darken lineart through Toon Args: clip (vs.VideoNode): Source clip. strength (float, optional): Strength in Toon. Defaults to 0.2. Returns: vs.VideoNode: Darked clip. """ darken = hvf.Toon(clip, strength, **kwargs) darken_mask = core.std.Expr([ core.std.Convolution(clip, [5, 10, 5, 0, 0, 0, -5, -10, -5], divisor=4, saturate=False), core.std.Convolution(clip, [5, 0, -5, 10, 0, -10, 5, 0, -5], divisor=4, saturate=False) ], [ 'x y max {neutral} / 0.86 pow {peak} *'.format( neutral=1 << (clip.format.bits_per_sample - 1), peak=(1 << clip.format.bits_per_sample) - 1) ]) return core.std.MaskedMerge(clip, darken, darken_mask)
def line_darkening(clip: vs.VideoNode, strength: float = 0.2, **kwargs: Any) -> vs.VideoNode: """ Darken lineart through Toon. Taken from varde's repository. """ import havsfunc as haf darken = haf.Toon(clip, strength, **kwargs) darken_mask = core.std.Expr( [ core.std.Convolution(clip, [5, 10, 5, 0, 0, 0, -5, -10, -5], divisor=4, saturate=False), core.std.Convolution(clip, [5, 0, -5, 10, 0, -10, 5, 0, -5], divisor=4, saturate=False) ], [ 'x y max {neutral} / 0.86 pow {peak} *'.format( neutral=1 << (clip.format.bits_per_sample - 1), # type: ignore[union-attr] peak=(1 << clip.format.bits_per_sample) - 1) ]) # type: ignore[union-attr] return core.std.MaskedMerge(clip, darken, darken_mask)
def warping(clip: vs.VideoNode, strength: float, awarp_depth: float)-> vs.VideoNode: darken = hvf.Toon(clip, strength) darken_mask = core.std.Expr( [core.std.Convolution(clip, [5, 10, 5, 0, 0, 0, -5, -10, -5], divisor=4, saturate=False), core.std.Convolution(clip, [5, 0, -5, 10, 0, -10, 5, 0, -5], divisor=4, saturate=False)], ['x y max {neutral} / 0.86 pow {peak} *'.format(neutral=1 << (clip.format.bits_per_sample-1), peak=(1 << clip.format.bits_per_sample)-1)]) unwarp = core.warp.AWarpSharp2(darken, depth=awarp_depth) return core.std.MaskedMerge(clip, unwarp, darken_mask)
def TAAmbk(clip, aatype=1, aatypeu=None, aatypev=None, preaa=0, strength=0.0, cycle=0, mtype=None, mclip=None, mthr=None, mlthresh=None, mpand=(0, 0), txtmask=0, txtfade=0, thin=0, dark=0.0, sharp=0, aarepair=0, postaa=None, src=None, stabilize=0, down8=True, showmask=0, opencl=False, opencl_device=-1, **kwargs): core = vs.get_core() aatypeu = aatype if aatypeu is None else aatypeu aatypev = aatype if aatypev is None else aatypev if mtype is None: mtype = 0 if preaa == 0 and True not in (aatype, aatypeu, aatypev) else 1 if postaa is None: postaa = True if abs(sharp) > 70 or (0.4 < abs(sharp) < 1) else False if src is None: src = clip else: if clip.format.id != src.format.id: raise ValueError(MODULE_NAME + ': clip format and src format mismatch.') elif clip.width != src.width or clip.height != src.height: raise ValueError(MODULE_NAME + ': clip resolution and src resolution mismatch.') preaa_clip = clip if preaa == 0 else daa(clip, preaa, opencl, opencl_device) edge_enhanced_clip = ( thin != 0 and core.warp.AWarpSharp2(preaa_clip, depth=int(thin)) or preaa_clip) edge_enhanced_clip = (dark != 0 and haf.Toon(edge_enhanced_clip, str=float(dark)) or edge_enhanced_clip) aa_kernel = { 0: lambda clip, *args, **kwargs: type('', (), {'out': lambda: clip}), 1: AAEedi2, 2: AAEedi3, 3: AANnedi3, 4: AANnedi3UpscaleSangNom, 5: AASpline64NRSangNom, 6: AASpline64SangNom, -1: AAEedi2SangNom, -2: AAEedi3SangNom, -3: AANnedi3SangNom, 'Eedi2': AAEedi2, 'Eedi3': AAEedi3, 'Nnedi3': AANnedi3, 'Nnedi3UpscaleSangNom': AANnedi3UpscaleSangNom, 'Spline64NrSangNom': AASpline64NRSangNom, 'Spline64SangNom': AASpline64SangNom, 'Eedi2SangNom': AAEedi2SangNom, 'Eedi3SangNom': AAEedi3SangNom, 'Nnedi3SangNom': AANnedi3SangNom, 'PointSangNom': AAPointSangNom, 'Unknown': lambda clip, *args, **kwargs: type( '', (), { 'out': lambda: exec( 'raise ValueError(MODULE_NAME + ": unknown aatype, aatypeu or aatypev")' ) }), 'Custom': kwargs.get( 'aakernel', lambda clip, *args, **kwargs: type( '', (), { 'out': lambda: exec( 'raise RuntimeError(MODULE_NAME + ": custom aatype: aakernel must be set.")' ) })), } if clip.format.color_family is vs.YUV: yuv = [ core.std.ShufflePlanes(edge_enhanced_clip, i, vs.GRAY) for i in range(clip.format.num_planes) ] aatypes = [aatype, aatypeu, aatypev] aa_classes = [ aa_kernel.get(aatype, aa_kernel['Unknown']) for aatype in aatypes ] aa_clips = [ aa_cycle(plane, aa_class, cycle, strength if yuv.index(plane) == 0 else 0, down8, opencl=opencl, opencl_device=opencl_device, **kwargs) for plane, aa_class in zip(yuv, aa_classes) ] aaed_clip = core.std.ShufflePlanes(aa_clips, [0, 0, 0], vs.YUV) elif clip.format.color_family is vs.GRAY: gray = edge_enhanced_clip aa_class = aa_kernel.get(aatype, aa_kernel['Unknown']) aaed_clip = aa_cycle(gray, aa_class, cycle, strength, down8, **kwargs) else: raise ValueError(MODULE_NAME + ': Unsupported color family.') abs_sharp = abs(sharp) if sharp >= 1: sharped_clip = haf.LSFmod(aaed_clip, strength=int(abs_sharp), defaults='old', source=src) elif sharp > 0: per = int(40 * abs_sharp) matrix = [-1, -2, -1, -2, 52 - per, -2, -1, -2, -1] sharped_clip = core.std.Convolution(aaed_clip, matrix) elif sharp == 0: sharped_clip = aaed_clip elif sharp > -1: sharped_clip = haf.LSFmod(aaed_clip, strength=round(abs_sharp * 100), defaults='fast', source=src) elif sharp == -1: blured = core.rgvs.RemoveGrain( aaed_clip, mode=20 if aaed_clip.width > 1100 else 11) diff = core.std.MakeDiff(aaed_clip, blured) diff = core.rgvs.Repair(diff, core.std.MakeDiff(src, aaed_clip), mode=13) sharped_clip = core.std.MergeDiff(aaed_clip, diff) else: sharped_clip = aaed_clip postaa_clip = sharped_clip if postaa is False else soothe( sharped_clip, src, 24) repaired_clip = ( (aarepair > 0 and core.rgvs.Repair(src, postaa_clip, aarepair)) or (aarepair < 0 and core.rgvs.Repair(postaa_clip, src, -aarepair)) or postaa_clip) stabilized_clip = repaired_clip if stabilize == 0 else temporal_stabilize( repaired_clip, src, stabilize) if mclip is not None: try: masked_clip = core.std.MaskedMerge(src, stabilized_clip, mclip, first_plane=True) masker = type('', (), { '__call__': lambda *args, **kwargs: mclip })() except vs.Error: raise RuntimeError( MODULE_NAME + ': Something wrong with your mclip. Maybe format, resolution or bit_depth mismatch.' ) else: # Use lambda for lazy evaluation mask_kernel = { 0: lambda: lambda a, b, *args, **kwargs: b, 1: lambda: mask_lthresh(clip, mthr, mlthresh, mask_sobel, mpand, opencl=opencl, opencl_device=opencl_device, **kwargs), 2: lambda: mask_lthresh(clip, mthr, mlthresh, mask_robert, mpand, ** kwargs), 3: lambda: mask_lthresh(clip, mthr, mlthresh, mask_prewitt, mpand, ** kwargs), 4: lambda: mask_lthresh(clip, mthr, mlthresh, mask_tedge, mpand, ** kwargs), 5: lambda: mask_lthresh(clip, mthr, mlthresh, mask_canny_continuous, mpand, opencl=opencl, opencl_device=opencl_device, **kwargs), 6: lambda: mask_lthresh(clip, mthr, mlthresh, mask_msharpen, mpand, ** kwargs), 'Sobel': lambda: mask_lthresh(clip, mthr, mlthresh, mask_sobel, mpand, opencl=opencl, opencl_device=opencl_device, **kwargs), 'Canny': lambda: mask_lthresh(clip, mthr, mlthresh, mask_canny_binarized, mpand, opencl=opencl, opencl_device=opencl_device, **kwargs), 'Prewitt': lambda: mask_lthresh(clip, mthr, mlthresh, mask_prewitt, mpand, ** kwargs), 'Robert': lambda: mask_lthresh(clip, mthr, mlthresh, mask_robert, mpand, ** kwargs), 'TEdge': lambda: mask_lthresh(clip, mthr, mlthresh, mask_tedge, mpand, ** kwargs), 'Canny_Old': lambda: mask_lthresh(clip, mthr, mlthresh, mask_canny_continuous, mpand, opencl=opencl, opencl_device=opencl_device, **kwargs), 'MSharpen': lambda: mask_lthresh(clip, mthr, mlthresh, mask_msharpen, mpand, ** kwargs), 'Unknown': lambda: exec('raise ValueError(MODULE_NAME + ": unknown mtype")') } mtype = 5 if mtype is None else mtype mthr = (24, ) if mthr is None else mthr masker = mask_kernel.get(mtype, mask_kernel['Unknown'])() masked_clip = masker(src, stabilized_clip) if txtmask > 0 and clip.format.color_family is not vs.GRAY: text_mask = mask_fadetxt(clip, lthr=txtmask, fade_num=txtfade) txt_protected_clip = core.std.MaskedMerge(masked_clip, src, text_mask, first_plane=True) else: text_mask = src txt_protected_clip = masked_clip final_output = ( (showmask == -1 and text_mask) or (showmask == 1 and masker(None, src, show=True)) or (showmask == 2 and core.std.StackVertical([ core.std.ShufflePlanes( [masker(None, src, show=True), core.std.BlankClip(src)], [0, 1, 2], vs.YUV), src ])) or (showmask == 3 and core.std.Interleave([ core.std.ShufflePlanes( [masker(None, src, show=True), core.std.BlankClip(src)], [0, 1, 2], vs.YUV), src ])) or txt_protected_clip) return final_output
def TAAmbk(clip, aatype=1, aatypeu=None, aatypev=None, preaa=0, strength=0.0, cycle=0, mtype=None, mclip=None, mthr=None, mthr2=None, mlthresh=None, mpand=(1, 0), txtmask=0, txtfade=0, thin=0, dark=0.0, sharp=0, aarepair=0, postaa=None, src=None, stabilize=0, down8=True, showmask=0, opencl=False, opencl_device=0, **args): core = vs.get_core() aatypeu = aatype if aatypeu is None else aatypeu aatypev = aatype if aatypev is None else aatypev if mtype is None: mtype = 0 if preaa == 0 and True not in (aatype, aatypeu, aatypev) else 1 if postaa is None: postaa = True if abs(sharp) > 70 or (0.4 < abs(sharp) < 1) else False if src is None: src = clip else: if clip.format.id != src.format.id: raise ValueError(MODULE_NAME + ': clip format and src format mismatch.') elif clip.width != src.width or clip.height != src.height: raise ValueError(MODULE_NAME + ': clip resolution and src resolution mismatch.') preaa_clip = clip if preaa == 0 else daa(clip, preaa) if thin == 0 and dark == 0: edge_enhanced_clip = preaa_clip elif thin != 0 and dark != 0: edge_enhanced_clip = haf.Toon(core.warp.AWarpSharp2(preaa_clip, depth=int(thin)), str=float(dark)) elif thin == 0: edge_enhanced_clip = haf.Toon(preaa_clip, str=float(dark)) else: edge_enhanced_clip = core.warp.AWarpSharp2(preaa_clip, depth=int(thin)) aa_kernel = { 1: AAEedi2, 2: AAEedi3, 3: AANnedi3, 4: AANnedi3UpscaleSangNom, 5: AASpline64NRSangNom, 6: AASpline64SangNom, -1: AAEedi2SangNom, -2: AAEedi3SangNom, -3: AANnedi3SangNom, 'Eedi2': AAEedi2, 'Eedi3': AAEedi3, 'Nnedi3': AANnedi3, 'Nnedi3UpscaleSangNom': AANnedi3UpscaleSangNom, 'Spline64NrSangNom': AASpline64NRSangNom, 'Spline64SangNom': AASpline64SangNom, 'Eedi2SangNom': AAEedi2SangNom, 'Eedi3SangNom': AAEedi3SangNom, 'Nnedi3SangNom': AANnedi3SangNom, 'PointSangNom': AAPointSangNom } aaed_clip = None if clip.format.color_family is vs.YUV: y = core.std.ShufflePlanes(edge_enhanced_clip, 0, vs.GRAY) u = core.std.ShufflePlanes(edge_enhanced_clip, 1, vs.GRAY) v = core.std.ShufflePlanes(edge_enhanced_clip, 2, vs.GRAY) if aatype != 0: try: y = aa_kernel[aatype](y, strength, down8, opencl=opencl, opencl_device=opencl_device, **args).out() cycle_y = cycle while cycle_y > 0: y = aa_kernel[aatype](y, strength, down8, opencl=opencl, opencl_device=opencl_device, **args).out() cycle_y -= 1 y = mvf.Depth( y, clip.format.bits_per_sample) if down8 is True else y except KeyError: raise ValueError(MODULE_NAME + ': unknown aatype.') if aatypeu != 0: try: u = aa_kernel[aatypeu]( u, 0, down8, opencl=opencl, opencl_device=opencl_device, **args).out() # Won't do predown for u plane cycle_u = cycle while cycle_u > 0: u = aa_kernel[aatypeu](u, 0, down8, opencl=opencl, opencl_device=opencl_device, **args).out() cycle_u -= 1 u = mvf.Depth( u, clip.format.bits_per_sample) if down8 is True else u except KeyError: raise ValueError(MODULE_NAME + ': unknown aatypeu.') if aatypev != 0: try: v = aa_kernel[aatypev]( v, 0, down8, opencl=opencl, opencl_device=opencl_device, **args).out() # Won't do predown for v plane cycle_v = cycle while cycle_v > 0: v = aa_kernel[aatypev](v, 0, down8, opencl=opencl, opencl_device=opencl_device, **args).out() cycle_v -= 1 v = mvf.Depth( v, clip.format.bits_per_sample) if down8 is True else v except KeyError: raise ValueError(MODULE_NAME + ': unknown aatypev.') aaed_clip = core.std.ShufflePlanes([y, u, v], [0, 0, 0], vs.YUV) elif clip.format.color_family is vs.GRAY: y = edge_enhanced_clip if aatype != 0: try: y = aa_kernel[aatype](y, strength, down8, **args).out() cycle_y = cycle while cycle_y > 0: y = aa_kernel[aatype](y, strength, down8, **args).out() cycle_y -= 1 aaed_clip = mvf.Depth( y, clip.format.bits_per_sample) if down8 is True else y except KeyError: raise ValueError(MODULE_NAME + ': unknown aatype.') else: raise ValueError(MODULE_NAME + ': Unsupported color family.') abs_sharp = abs(sharp) if sharp >= 1: sharped_clip = haf.LSFmod(aaed_clip, strength=int(abs_sharp), defaults='old', source=src) elif sharp > 0: per = int(40 * abs_sharp) matrix = [-1, -2, -1, -2, 52 - per, -2, -1, -2, -1] sharped_clip = core.std.Convolution(aaed_clip, matrix) elif sharp == 0: sharped_clip = aaed_clip elif sharp > -1: sharped_clip = haf.LSFmod(aaed_clip, strength=round(abs_sharp * 100), defaults='fast', source=src) elif sharp == -1: blured = core.rgvs.RemoveGrain( aaed_clip, mode=20 if aaed_clip.width > 1100 else 11) diff = core.std.MakeDiff(aaed_clip, blured) diff = core.rgvs.Repair(diff, core.std.MakeDiff(src, aaed_clip), mode=13) sharped_clip = core.std.MergeDiff(aaed_clip, diff) else: sharped_clip = aaed_clip postaa_clip = sharped_clip if postaa is False else soothe( sharped_clip, src, 24) repaired_clip = postaa_clip if aarepair == 0 else core.rgvs.Repair( src, postaa_clip, aarepair) stabilized_clip = repaired_clip if stabilize == 0 else temporal_stabilize( repaired_clip, src, stabilize) if mclip is not None: mask = mclip try: masked_clip = core.std.MaskedMerge(src, stabilized_clip, mask, first_plane=True) except vs.Error: raise RuntimeError(MODULE_NAME + ': Something wrong with your mclip. ' 'Maybe resolution or bit_depth mismatch.') elif mtype != 0: if mtype == 1 or mtype is 'Canny': opencl_device = args.get('opencl_device', 0) mthr = 1.2 if mthr is None else mthr mthr2 = 8.0 if mthr2 is None else mthr2 mask = MaskCanny(clip, sigma=mthr, t_h=mthr2, lthresh=mlthresh, mpand=mpand, opencl=opencl, opencl_devices=opencl_device).out() elif mtype == 2 or mtype is 'Sobel': mthr = 1.2 if mthr is None else mthr mthr2 = 48 if mthr2 is None else mthr2 mask = MaskSobel(clip, sigma=mthr, binarize=mthr2, lthresh=mlthresh, mpand=mpand).out() elif mtype == 3 or mtype is 'prewitt': mthr = 62 if mthr is None else mthr mask = MaskPrewitt(clip, factor=mthr, lthresh=mlthresh, mpand=mpand).out() else: raise ValueError(MODULE_NAME + ': unknown mtype.') masked_clip = core.std.MaskedMerge(src, stabilized_clip, mask) else: masked_clip = stabilized_clip if txtmask > 0 and clip.format.color_family is not vs.GRAY: text_mask = FadeTextMask(clip, lthr=txtmask, fade_nums=txtfade).out() txt_protected_clip = core.std.MaskedMerge(masked_clip, src, text_mask, first_plane=True) else: txt_protected_clip = masked_clip if clip.format.bits_per_sample > 8 and down8 is True: clamped_clip = mvf.LimitFilter(src, txt_protected_clip, thr=1.0, elast=2.0) else: clamped_clip = txt_protected_clip try: if showmask == -1: return text_mask elif showmask == 1: return mask elif showmask == 2: return core.std.StackVertical([ core.std.ShufflePlanes([mask, core.std.BlankClip(src)], [0, 1, 2], vs.YUV), src ]) elif showmask == 3: return core.std.Interleave([ core.std.ShufflePlanes([mask, core.std.BlankClip(src)], [0, 1, 2], vs.YUV), src ]) else: return clamped_clip except UnboundLocalError: raise RuntimeError(MODULE_NAME + ': No mask to show if you don\'t have one.')
def do_filter(): """Vapoursynth filtering""" scene_change = [] with open('k.log') as file: lines = file.readlines() for line in lines: line = line.split() scene_change.append(int(line[0])) src = CLIP_SRC # Lol? border = awf.bbmod(src, 4, thresh=128, blur=15, y=True, u=True, v=True) border = lvf.ef(border, [6, 3, 3], radius=[12, 6, 6]) border = lvf.rfs(src, border, scene_change) out = depth(border, 16) # joletb has stronk eyes planes = split(out) planes[1], planes[2] = [ core.resize.Spline16(plane, src_top=-0.25) for plane in planes[1:] ] out = join(planes) # qual=2 produces weird artefacts and a stronger alpha/beta/gamma smudges details. new_fields = core.eedi3m.EEDI3(out, 1, alpha=0.25, beta=0.3, gamma=400, nrad=3, mdis=20, vcheck=3, sclip=core.nnedi3.nnedi3(out, 1, nsize=3, nns=3, qual=1, pscrn=4)) out = new_fields # omegartifacted frames def freeze_frame_after(clip, frame): return core.std.FreezeFrames(clip, frame, frame, frame + 1) def freeze_frame_before(clip, frame): return core.std.FreezeFrames(clip, frame, frame, frame - 1) cursed_frames = [ 2326, 8907, 12211, 12551, 13990, 14403, 15462, 17673, 19382, 23099, 23738, 24031, 24802, 25083 ] for cursed_frame in cursed_frames: out = freeze_frame_after(out, cursed_frame) cursed_frames = [5695, 9115, 9116, 17671, 18432] for cursed_frame in cursed_frames: out = freeze_frame_before(out, cursed_frame) # omegartifacted frames ² denoise_li = hvf.SMDegrain(out, thSAD=200) denoise_hi = hvf.SMDegrain(out, thSAD=350) denoise = lvf.rfs(denoise_li, denoise_hi, scene_change) def hard_denoise(clip): clip = hvf.SMDegrain(clip, thSAD=500) clip = knlm_denoise(clip, (2, 2), dict(a=8)) return clip cursed_frames = [ 595, 1191, 2643, 2663, 2664, 2665, 2666, 2667, 2671, 2672, 2674, 2675, 2679, 3999, 4419, 6351, 6355, 6547, 8906, 11731, 14176, 14177, 14178, 14179, 18430, 18435, 18437, 18438, 18439, 27766 ] cursed_frames += range(10767, 10776) cursed_frames += range(25013, 25018) cursed_frames += range(27663, 27668) cursed_frames += range(29642, 29646) cursed_frames += range(31384, 31388) uncursed = hard_denoise(out) denoise = lvf.rfs(denoise, uncursed, cursed_frames) out = denoise # It helps to fix the the aliasing left antialias = lvf.sraa(out, rep=13) out = antialias # Compensative line darkening and sharpening luma = get_y(out) darken = hvf.Toon(luma, 0.20) darken_mask = core.std.Expr([ core.std.Convolution( luma, [5, 10, 5, 0, 0, 0, -5, -10, -5], divisor=4, saturate=False), core.std.Convolution( luma, [5, 0, -5, 10, 0, -10, 5, 0, -5], divisor=4, saturate=False) ], [ 'x y max {neutral} / 0.86 pow {peak} *'.format( neutral=1 << (luma.format.bits_per_sample - 1), peak=(1 << luma.format.bits_per_sample) - 1) ]) darken = core.std.MaskedMerge(luma, darken, darken_mask) # Slight sharp through CAS sharp = hvf.LSFmod(darken, strength=65, Smode=3, Lmode=1, edgemode=1, edgemaskHQ=True) out = vdf.merge_chroma(sharp, out) # Chroma planes are pure crap chromableed = xvs.WarpFixChromaBlend(out, 96, depth=8) out = chromableed detail_mask = lvf.denoise.detail_mask(out, brz_a=2700, brz_b=1500) deband = placebo.deband(out, 17, 5.75, grain=4) deband_b = placebo.deband(out, 24, 8, 2, grain=4) deband_c = placebo.deband(out, 17, 8, grain=4) deband_d = placebo.deband(out, 20, 12, 3, grain=4) deband = lvf.rfs(deband, deband_b, [(4596, 4669), (23036, 23098)]) deband = lvf.rfs(deband, deband_c, [(1646, 1711), (29768, 29840), (29932, 30037), (30163, 30243)]) deband = lvf.rfs(deband, deband_d, [(1712, 1830)]) deband = core.std.MaskedMerge(deband, out, detail_mask) out = deband return depth(out, 10)