Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import vapoursynth as vs
- core=vs.core
- import kagefunc as kgf
- import zzfunc as zz
- import fvsfunc as fvf
- import mvsfunc as mvf
- import muvsfunc as muf
- import havsfunc as haf
- import debandshit as db
- import mvmulti as mv
- import vsTAAmbk as taa
- import dhh
- from vsutil import *
- from functools import partial
- import zzfunc_beta as zzt
- from G41Fun import EdgeDetect
- def autogain(clip): return core.avsw.Eval('clip.coloryuv(autogain=true)', [clip], ['clip'])
- def all_lines(clip, radius=3, **mode):
- refa = haf.mt_inpand_multi(haf.mt_expand_multi(clip, sw=radius, sh=radius, **mode), sw=radius, sh=radius, **mode)
- refb = haf.mt_expand_multi(haf.mt_inpand_multi(clip, sw=radius, sh=radius, **mode), sw=radius, sh=radius, **mode)
- return core.std.Expr([clip, refa, refb], 'x y - abs x z - abs max')
- def dark_lines(clip, radius=3, show=False, **mode):
- ref = haf.mt_inpand_multi(haf.mt_expand_multi(clip, sw=radius, sh=radius, **mode), sw=radius, sh=radius, **mode)
- if show:
- return ref
- return core.std.Expr([clip, ref], 'x y - abs')
- def MMFilter(clips, mode='min', planes=None):
- planes = list(range(clips[0].format.num_planes)) if planes is None else [planes] if isinstance(planes, int) else planes
- expr = 'x y - x z - * 0 < {} x y - abs x z - abs < {} ? ?'.format('z' if mode=='max' else 'x', 'z y' if mode=='max' else 'y z')
- expr = [expr if i in planes else '' for i in range(clips[0].format.num_planes)]
- return core.std.Expr(clips, expr)
- both = dict(coordinates=[0,1,0,1,1,0,1,0])
- def Dither_gf3_bilateral_multistage(src, ref=None, radius=None, thr=1/3, elast=3.0, wmin=1.0, subspl=0, planes=[0,1,2]):
- if radius is None:
- # 16 @ 1080p, 12 at 720p and 9 at 480p
- radius = max( (src.width - 1280)/160 + 12, (src.height - 720)/ 90 + 12 )
- y, u, v = [3 if x in planes else 1 for x in range(3)]
- thr_1 = round(max(thr * 4.5, 1.25), 1)
- thr_2 = round(max(thr * 9, 5), 1)
- subspl_2 = subspl if subspl in (0,1) else subspl / 2
- r4 = round(max(radius * 4 / 3, 4))
- r2 = round(max(radius * 2 / 3, 3))
- r1 = round(max(radius / 3, 2))
- clips = [src.fmtc.nativetostack16()]
- clip_names = ["c"]
- ref_t = "c"
- if ref is not None:
- ref_s = ref.fmtc.nativetostack16()
- clips += [ref_s]
- clip_names += ["ref"]
- ref_t = "ref"
- avs_stuff = "c.Dither_bilateral16(radius={r4}, thr={thr_1}, flat=0.75, wmin={wmin}, ref={ref_t}, subspl={subspl}, y={y}, u={u}, v={v})"
- avs_stuff += ".Dither_bilateral16(radius={r2}, thr={thr_2}, flat=0.25, wmin={wmin}, ref={ref_t}, subspl={subspl_2}, y={y}, u={u}, v={v})"
- avs_stuff += ".Dither_bilateral16(radius={r1}, thr={thr_2}, flat=0.50, wmin={wmin}, ref={ref_t}, subspl={subspl_2}, y={y}, u={u}, v={v})"
- avs_stuff = avs_stuff.format(ref_t=ref_t, r4=r4, r2=r2, r1=r1, thr_1=thr_1, thr_2=thr_2, wmin=wmin, subspl=subspl, subspl_2=subspl_2, y=y, u=u, v=v)
- return core.avsw.Eval(avs_stuff, clips=clips, clip_names=clip_names).fmtc.stack16tonative()
- def lumadouble(clip):
- clip = core.std.Transpose(clip)
- clip = core.nnedi3.nnedi3(clip, 0, 1, 0, 0, 4, 2).nnedi3.nnedi3(1, 0, 0, 0, 4, 2)
- clip = core.std.Transpose(clip)
- clip = core.nnedi3.nnedi3(clip, 0, 1, 0, 0, 4, 2).nnedi3.nnedi3(1, 0, 0, 0, 4, 2)
- return core.resize.Spline36(clip, src_left=.5, src_top=.5)
- def show(*clips): return core.std.Interleave(clips, mismatch=1).resize.Spline36(format=vs.RGB24, dither_type='error_diffusion',matrix_in=1)
- ssf = 2.
- def aa(base, clip, mask):
- aa = clip.std.Transpose().resize.Spline16(1980, 3520)
- mclip = mask.std.Maximum().resize.Bilinear(3520, 1980).std.Binarize(1111)
- aa = aa.eedi3m.EEDI3(0, 0, gamma=0, sclip=aa.znedi3.nnedi3(0,0,0,0,4,2), mclip=mclip.std.Transpose()).std.Transpose()
- aa = aa.eedi3m.EEDI3(0, 0, gamma=0, sclip=aa.znedi3.nnedi3(0,0,0,0,4,2), mclip=mclip).resize.Spline36(1920, 1080)
- return base.std.MaskedMerge(aa, mask)
- def RepairMod(clip, repairclip, **planes):
- rmax = repairclip.std.Maximum(**planes, coordinates=[0,1,0,1,1,0,1,0]).std.Maximum(**planes)
- rmin = repairclip.std.Minimum(**planes, coordinates=[0,1,0,1,1,0,1,0]).std.Minimum(**planes)
- return core.std.Expr([clip, rmax, rmin], 'x y min z max')
- def FSRCNNX(clip):
- return get_y(join([clip]*3).placebo.Shader(shader=r'C:\Users\Zastin\AppData\Roaming\mpv\shaders\FSRCNNX_x2_16-0-4-1.glsl', filter='box', width=clip.width*2, height=clip.height*2))
- def nnedi3_rpow2(clip):
- clip = clip.std.Transpose().znedi3.nnedi3(0, 1, 0, 0, 4, 2)
- return clip.std.Transpose().znedi3.nnedi3(0, 1, 0, 0, 4, 2).resize.Spline36(src_left=.5, src_top=.5)
- m_none = "[0 75][655 755][2026 2127][2314 2365][3167 3219][3889 3956][4935 5042][6114 6257][7865 8012][8194 8223][9452 9577][10218 10307][10724 10765][16359 16424][20252 20313][23254 23337][24545 24628][26926 26973][27032 27233][29498 29549][30618 30809][31953 31975][31976 32521]"
- m_strong = "[318 389][3220 3285][3957 4004][5527 5641][8488 8523][8974 9105][10640 10723][11298 11387][14552 14587][15297 15314][15513 15520][18556 18663][21111 21218]"
- m_strong_masked = "[6446 6506][7580 7645][7786 7864][8013 8101][9152 9259][9578 9677][10980 11111][11472 11531][11532 11627][11628 11709][13282 13413][14408 14491][14794 14885][15801 15926][16689 17565][17938 18009][20096 20203][22701 22772][24239 24310][28094 28164][29306 29401][29814 29869][30378 30497][30498 30617][32968 33039][33736 33855][34234 34283][34313 34336][34448 34486][34515 34542][34580 34643]"
- m_grain = "[318 389][7208 7315][8974 9105][9152 9259][9578 9843][10640 10723][10980 11111][11298 11387][18556 18663]"
- #op="[3957 6113]"
- waka = core.ffms2.Source(r'C:\Users\Zastin\Desktop\WIP\scissorgirls\Adachi to Shimamura - 01 (Wakanim FR 1080p).mkv')[23:]
- funi = core.ffms2.Source(r'C:\Users\Zastin\Desktop\cartoons\[Erai-raws] Adachi to Shimamura - 01 [1080p].mkv')[192:]
- hsm = kgf.hardsubmask(waka, funi)
- diff = core.std.Expr([waka, funi], 'x y - abs').resize.Point(format=vs.YUV444P8)
- diff = core.std.Expr(split(diff), 'x y z max max')
- main = diff.std.Binarize(10)
- upper = diff.std.Binarize(30)
- middle= diff.std.Binarize(20)
- lower = main.std.Minimum()
- rad = 3
- diffh = iterate(lower, partial(core.std.Minimum, coordinates=[0,0,0,1,1,0,0,0]), rad-1)
- diffv = iterate(lower, partial(core.std.Minimum, coordinates=[0,1,0,0,0,0,1,0]), rad-1)
- diffhv= core.std.Expr([diffh,diffv], 'x y max').misc.Hysteresis(main)
- diffb = middle.std.Minimum().misc.Hysteresis(main)
- diffc = upper.rgvs.RemoveGrain(2).misc.Hysteresis(main)
- hsm_ts = iterate(core.std.Expr([diffhv, diffb, diffc, hsm], 'x y z max max'), core.std.Maximum, 5).std.Convolution([1]*25)
- hsm = core.std.Expr([hsm, hsm_ts], 'x y max')
- clipyuv = waka.std.MaskedMerge(funi, hsm)
- y = zz.y(clipyuv)
- desl = depth(depth(y, 32).descale.Debilinear(1504, 846), 16)
- clip = depth(y, 16)
- bars = core.std.Expr(split(clipyuv.std.Expr(['x 17 <= 255 0 ?','x 128 - abs 1 <= 255 0 ?']).resize.Point(format=vs.YUV444P8)), 'x y z min min')
- bars = iterate(bars, partial(core.std.Maximum, coordinates=[0,0,0,1,1,0,0,0]), 10).std.Median().std.BoxBlur(hradius=100).std.Binarize(255).std.Maximum(coordinates=[0,1,0,0,0,0,1,0]).std.Maximum(coordinates=[0,1,0,0,0,0,1,0]).std.Maximum(coordinates=[0,1,0,0,0,0,1,0]).std.Convolution([1]*5,mode='v')
- borders = y.std.BlankClip(1920-8,1080-8)
- for x in range(1, 5):
- borders = borders.std.AddBorders(1,1,1,1,min(256 * (x/4), 255))
- creds = core.std.Expr([clip, desl.resize.Bilinear(1920, 1080)], 'x y - abs')
- creds = creds.std.Binarize(12<<8).misc.Hysteresis(creds.std.Maximum().std.Binarize(4<<8))
- creds = creds.std.Maximum(coordinates=[0,1,0,1,1,0,1,0]).std.Maximum().std.Maximum(coordinates=[0,1,0,1,1,0,1,0]).std.Convolution([1]*25)
- creds = core.std.Expr([creds,borders,bars], 'x y z max 257 * max')
- fsr = FSRCNNX(desl)
- fsrref = fsr.resize.Spline36(1920,1080)
- desl = core.std.Merge(nnedi3_rpow2(desl), fsr, 0.5).resize.Spline36(1920,1080)
- mask = clip.std.Prewitt()
- maskm = mask.std.Maximum()
- aamask = maskm.std.Binarize(40<<8).std.Convolution([1]*9)
- maska = mask.std.Binarize(30<<8)
- maskb = mask.std.Binarize(60<<8)
- mask = core.std.Expr([maska, maskb.std.Maximum().std.Maximum(), maskb], 'x y - z max')
- mask = mask.std.Maximum().std.Maximum().std.Minimum().std.Minimum()
- maskmin = mask.std.Minimum()
- mask = core.std.Expr([mask, maskmin.std.Maximum(), maskmin], 'x y 0 = and x x z + 2 / ?')
- imask = iterate(mask, core.std.Maximum, 4).std.Invert()
- mask = core.std.Expr([mask, imask], 'x y +')
- desl = aa(desl, fsr, aamask)
- desl = desl.rgvs.Repair(clip.std.MaskedMerge(desl, mask), 1)
- edges = EdgeDetect(fsrref, "FDOG").std.Maximum().std.Minimum().std.Expr(f'x {20<<8} < 0 x {50<<8} > 65535 x {20<<8} - {65535/(30<<8)} / ? ?')
- lines = all_lines(fsrref, 2).std.Binarize(2 * 256)
- lines = core.std.Expr([lines.rgvs.RemoveGrain(1), lines.rgvs.RemoveGrain(3).std.Maximum(threshold=1<<15)], 'x y max')
- det = zzt.combine([edges, lines]).std.Convolution([1]*9)
- desl = clip.std.MaskedMerge(desl, det)
- clip = desl.std.MaskedMerge(clip, creds)
- mask_normal = db.rangemask(clipyuv, 2, 2).std.Binarize(2)
- mask_big = db.rangemask(clipyuv, 12, 6).std.Binarize(3)
- rad = 16
- thr = 2
- mi0 = y.std.Minimum()
- ma0 = y.std.Maximum()
- mi1 = y.std.Minimum(**both)
- ma1 = y.std.Maximum(**both)
- mi = iterate(mi0, partial(core.std.Minimum, coordinates=[0,0,0,1,1,0,0,0]), rad-1)
- ma = iterate(ma0, partial(core.std.Maximum, coordinates=[0,0,0,1,1,0,0,0]), rad-1)
- maskx = core.std.Expr([ma, mi], 'x y - abs').std.Binarize(thr)
- mi = iterate(mi0, partial(core.std.Minimum, coordinates=[0,1,0,0,0,0,1,0]), rad-1)
- ma = iterate(ma0, partial(core.std.Maximum, coordinates=[0,1,0,0,0,0,1,0]), rad-1)
- masky = core.std.Expr([ma, mi], 'x y - abs').std.Binarize(thr)
- mi = iterate(mi1, partial(core.std.Minimum, coordinates=[0,0,1,0,0,1,0,0]), rad-2)
- ma = iterate(ma1, partial(core.std.Maximum, coordinates=[0,0,1,0,0,1,0,0]), rad-2)
- maskz = core.std.Expr([ma, mi], 'x y - abs').std.Binarize(thr)
- mi = iterate(mi1, partial(core.std.Minimum, coordinates=[1,0,0,0,0,0,0,1]), rad-2)
- ma = iterate(ma1, partial(core.std.Maximum, coordinates=[1,0,0,0,0,0,0,1]), rad-2)
- maska = core.std.Expr([ma, mi], 'x y - abs').std.Binarize(thr)
- mask_directional = zzt.std.CombineClips([maskx, masky, maskz, maska], 'min')
- mask_directional = join([mask_directional]*3).resize.Bilinear(format=vs.YUV420P8)
- rad = 15
- a = iterate(iterate(y, core.std.Minimum, rad), core.std.Maximum, rad)
- b = iterate(iterate(y, core.std.Maximum, rad), core.std.Minimum, rad)
- mask_lines = core.std.Expr([y,a,b], 'x y - abs x z - abs max 1 > 255 0 ?').std.Maximum().std.Maximum(threshold=128)
- mask_lines = join([mask_lines]*3).resize.Bilinear(format=vs.YUV420P8)
- mask_weak = zzt.combine([mask_normal, mask_big, mask_directional], 'min').std.Deflate()
- mask_weak = depth(mask_weak, 16, range=1, range_in=1)
- mask_strong = zzt.combine([mask_normal, mask_big, mask_directional]).std.BoxBlur(hradius=3, vradius=3)
- mask_strong = zzt.combine([mask_strong, mask_lines])
- mask_strong = depth(mask_strong, 16, range=1, range_in=1)
- gmask = db.rangemask(y, 6, 1).std.Binarize(4).std.BoxBlur(hradius=10, vradius=10)
- gmask = core.std.Expr([gmask,gmask.std.BoxBlur(hradius=10, vradius=10)], 'x y min 257 *', vs.GRAY16)
- clip = zz.mergechroma(clip, depth(clipyuv, 16))
- bil = mvf.LimitFilter(db.Dither_bilateral(clip, thr=0.35), clip, thr=0.35, thrc=0.25, elast=3)
- verystrong = clip.placebo.Deband(7,2,5,16,0)
- verystrong = MMFilter([clip,bil,verystrong],'max')
- strong = clip.placebo.Deband(7,1,4,16,0)
- strong = MMFilter([clip,bil,strong],'max')
- weak = MMFilter([clip,bil,clip.placebo.Deband(7,1,2,16,0)])
- deband = strong.std.MaskedMerge(weak, mask_strong).std.MaskedMerge(clip, mask_weak)
- deband = fvf.rfs(deband, verystrong, m_strong)
- deband = fvf.rfs(deband, verystrong.std.MaskedMerge(strong, mask_strong).std.MaskedMerge(db.f3kdb(clip, 15, 60, 40, 40), mask_weak), m_strong_masked)
- deband = fvf.rfs(kgf.adaptive_grain(deband, luma_scaling=3).std.MaskedMerge(deband, gmask, 0), core.grain.Add(deband, var=1, constant=True), m_grain)
- clip = fvf.rfs(deband, clip, m_none)
- depth(clip[:34644], 10).set_output()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement