Zastin

healer 03.vpy

Feb 1st, 2021 (edited)
3,007
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 19.44 KB | None | 0 0
  1. import vapoursynth as vs
  2. core=vs.core
  3. import kagefunc as kgf
  4. import zzfunc as zz
  5. import fvsfunc as fvf
  6. import muvsfunc as muf
  7. import mvsfunc as mvf
  8. import havsfunc as haf
  9. import debandshit as db
  10. import mvmulti as mv
  11. import vsTAAmbk as taa
  12. import dhh
  13. import descale
  14. import rgvs
  15. import zzfunc_beta as zzt
  16. import mt_lutspa
  17. import math
  18. import adjust
  19.  
  20. def AutoDeblock(src, edgevalue=24, db1=0.2, db2=1, db3=6, deblocky=True, deblockuv=True, debug=False, redfix=False,
  21.                 fastdeblock=False, adb1=3, adb2=4, adb3=8, adb1d=2, adb2d=7, adb3d=11, planes=None):
  22.  
  23.     try:
  24.         import havsfunc as haf
  25.     except ImportError:
  26.         raise ImportError('AutoDeblock: havsfunc not found. Download it here: https://github.com/HomeOfVapourSynthEvolution/havsfunc')
  27.  
  28.     def to8bit(f):
  29.         return f * 0xFF
  30.  
  31.     def eval_deblock_strength(n, f, fastdeblock, unfiltered, fast, weakdeblock,
  32.                               mediumdeblock, strongdeblock):
  33.         out = unfiltered
  34.         if fastdeblock:
  35.             if to8bit(f[0].props.OrigDiff) > adb1 and to8bit(f[1].props.YNextDiff) > adb1d:
  36.                 return fast
  37.             else:
  38.                 return unfiltered
  39.         if to8bit(f[0].props.OrigDiff) > adb1 and to8bit(f[1].props.YNextDiff) > adb1d:
  40.             out = weakdeblock
  41.         if to8bit(f[0].props.OrigDiff) > adb2 and to8bit(f[1].props.YNextDiff) > adb2d:
  42.             out = mediumdeblock
  43.         if to8bit(f[0].props.OrigDiff) > adb3 and to8bit(f[1].props.YNextDiff) > adb3d:
  44.             out = strongdeblock
  45.         return out
  46.  
  47.     def fix_red(n, f, unfiltered, autodeblock):
  48.         if (to8bit(f[0].props.YAverage) > 50 and to8bit(f[0].props.YAverage) < 130
  49.                 and to8bit(f[1].props.UAverage) > 95 and to8bit(f[1].props.UAverage) < 130
  50.                 and to8bit(f[2].props.VAverage) > 130 and to8bit(f[2].props.YAverage) < 155):
  51.             return unfiltered
  52.         return autodeblock
  53.  
  54.     if redfix and fastdeblock:
  55.         raise ValueError('AutoDeblock: You cannot set both "redfix" and "fastdeblock" to True!')
  56.  
  57.     if planes is None:
  58.         planes = []
  59.         if deblocky: planes.append(0)
  60.         if deblockuv: planes.extend([1,2])
  61.  
  62.     maxvalue = (1 << src.format.bits_per_sample) - 1
  63.     orig = core.std.Prewitt(src)
  64.     orig = core.std.Expr(orig, "x {edgevalue} >= {maxvalue} x ?".format(edgevalue=edgevalue, maxvalue=maxvalue))
  65.     orig_d = orig.rgvs.RemoveGrain(4).rgvs.RemoveGrain(4)
  66.  
  67.     predeblock = haf.Deblock_QED(src, uv=2).deblock.Deblock(planes=[1,2])
  68.     fast = core.dfttest.DFTTest(predeblock, tbsize=1)
  69.     fast = core.text.Text(fast, 'deblock') if debug else fast
  70.  
  71.     unfiltered    = predeblock
  72.     unfiltered    = core.text.Text(unfiltered, 'unfiltered') if debug else unfiltered
  73.     weakdeblock   = csharp(mvf.BM3D(predeblock, sigma=1, psample=0, profile1='fast'), predeblock)
  74.     weakdeblock   = core.text.Text(weakdeblock, 'weakdeblock') if debug else weakdeblock
  75.     mediumdeblock = csharp(mvf.BM3D(predeblock, sigma=2, psample=0, profile1='fast'), predeblock)
  76.     mediumdeblock = core.text.Text(mediumdeblock, 'mediumdeblock') if debug else mediumdeblock
  77.     strongdeblock = csharp(mvf.BM3D(predeblock, sigma=6, psample=0, profile1='fast'), predeblock)
  78.     strongdeblock = core.text.Text(strongdeblock, 'strongdeblock') if debug else strongdeblock
  79.  
  80.     difforig = core.std.PlaneStats(orig, orig_d, prop='Orig')
  81.     diffnext = core.std.PlaneStats(src, src.std.DeleteFrames([0]), prop='YNext')
  82.     autodeblock = core.std.FrameEval(unfiltered, partial(eval_deblock_strength, fastdeblock=fastdeblock,
  83.                                      unfiltered=unfiltered, fast=fast, weakdeblock=weakdeblock,
  84.                                      mediumdeblock=mediumdeblock, strongdeblock=strongdeblock),
  85.                                      prop_src=[difforig,diffnext])
  86.  
  87.     if redfix:
  88.         src = core.std.PlaneStats(src, prop='Y')
  89.         src_u = core.std.PlaneStats(src, plane=1, prop='U')
  90.         src_v = core.std.PlaneStats(src, plane=2, prop='V')
  91.         autodeblock = core.std.FrameEval(unfiltered, partial(fix_red, unfiltered=unfiltered,
  92.                                          autodeblock=autodeblock), prop_src=[src,src_u,src_v])
  93.  
  94.     return autodeblock
  95.  
  96. from vsutil import *
  97. from functools import partial
  98.  
  99. both = dict(coordinates=[0,1,0,1,1,0,1,0])
  100.  
  101. def fuckMPEG2(clip):
  102.     atx = AutoDeblock(clip).resize.Spline16(1920)
  103.     clip = atx
  104.     mask = atx.resize.Spline36(960,540)
  105.     mask = haf.FineDehalo(mask, showmask=1).std.Transpose().znedi3.nnedi3(0,1,0,0,1).std.Transpose().znedi3.nnedi3(0,1,0,0,1).resize.Spline36(src_left=.5,src_top=.5)
  106.     dha = atx.std.MaskedMerge(haf.DeHalo_alpha(atx, 3, 3, darkstr=0.1, brightstr=0.6), mask, 0)
  107.     y = zz.y(dha)
  108.    
  109.     rmask = db.rangemask(y,2,1).std.Binarize(1000).std.Maximum().std.Minimum().std.Minimum().std.Minimum().std.Minimum()
  110.     emask = y.std.Prewitt().std.Binarize(6000).std.Maximum()
  111.     strongmask = core.std.Expr([rmask, emask], 'y x -').std.Inflate()
  112.    
  113.     p1 = core.bilateral.Bilateral(dha, sigmaS=5 / 3, sigmaR=8 / 255, planes=0, algorithm=0)
  114.     p2 = mvf.BM3D(dha, sigma=24, psample=0, profile1='fast')
  115.     p0 = core.std.Expr([dha,p1,p2], ['x y z min max y z max min','z'])
  116.     p = csharp(p0,dha,11).rgvs.Repair(dha, 23)
  117.     p = mvf.LimitFilter(p, dha, thr=12, brighten_thr=4, elast=2, planes=0)
  118.     p = mvf.LimitFilter(p, dha, thr=6, planes=[1,2])
  119.     rimask = haf.HQDeringmod(p, mrad=4, msmooth=3, mthr=30, show=1)
  120.    
  121.     atx = dha.std.MaskedMerge(p, rimask, [0,1,2], True)
  122.     atx = atx.std.MaskedMerge(csharp(p2,dha), strongmask, [0,1,2], True)
  123.    
  124.     y = zz.y(atx)
  125.     mask = zzt.combine([y.std.Prewitt(), get_y(clip).std.Prewitt()]).std.Binarize(35<<8).std.Maximum().std.Minimum(threshold=1<<15,**both)
  126.     mclip = mask.resize.Point(1920*2, 1080*2).fmtc.resample(3520, 1980, kernel='box', fulls=1, fulld=1)
  127.     s1 = haf.LSFmod(y.bilateral.Bilateral(sigmaS=2, sigmaR=12 / 255, algorithm=0), strength=150)
  128.     s2 = haf.LSFmod(y, strength=150)
  129.     y = y.std.MaskedMerge(median(y, s1, s2), mask)
  130.     aa = y
  131.     aa = aa.znedi3.nnedi3(0,1,0,0,4,2,0,1).resize.Spline36(height=1980, src_top=0.5)
  132.     aa = aa.std.Transpose()
  133.     aa = aa.znedi3.nnedi3(0,1,0,0,4,2,0,1).resize.Spline36(height=3520, src_top=0.5)
  134.     aa = aa.eedi3m.EEDI3(0,0,0,0.2, 0.5, 69, mclip=mclip.std.Transpose(), sclip=aa.znedi3.nnedi3(0,0,0,0,4,2,0,1)).std.Transpose()
  135.     aa = aa.eedi3m.EEDI3(0,0,0,0.2, 0.5, 69, mclip=mclip                , sclip=aa.znedi3.nnedi3(0,0,0,0,4,2,0,1)).resize.Spline36(1920,1080)
  136.     y = y.std.MaskedMerge(aa, mask)
  137.     atx = zz.mergechroma(y, atx)
  138.    
  139.     return atx
  140.  
  141. def decensor(cr, atx, radius=5, min_length=3, smooth=6, thr=None, bordfix=10, alt=None, unfuck_mpeg2=fuckMPEG2, debug=False):
  142.     from vsutil import split, fallback, iterate
  143.     core = vs.core
  144.    
  145.     fmt = cr.format
  146.     peak = (1 << fmt.bits_per_sample) - 1
  147.     thr = fallback(thr, 24 << (fmt.bits_per_sample - 8))
  148.     alt = fallback(alt, cr)
  149.    
  150.     clip = core.std.Expr([cr, atxref], 'x y - abs')
  151.  
  152.     mask = core.std.Expr(split(clip.resize.Point(format=vrv.format.replace(subsampling_w=0, subsampling_h=0).id)), 'x y z max max')
  153.     mask1_ = mask.std.Binarize(3.5 * 256).std.Minimum().std.Minimum()
  154.     mask1 = iterate(mask1_, core.std.Maximum, 10)
  155.     mask2 = iterate(iterate(mask.std.Binarize(25<<8), core.std.Minimum, 5), core.std.Maximum, 25)
  156.     mask = zzt.combine([mask1, mask2])
  157.     mask = core.std.Expr([mask, zzt.combine([mask1_, mask2]).fmtc.bitdepth(bits=32,fulls=1).fmtc.resample(1,1,kernel='box').std.Expr(f'x 0.5 > 1 0 ?',vs.GRAY8).resize.Point(1920, 1080)], 'y 65535 x ?')
  158.     mask = maxm(mask, sw=50)[-1]
  159.     mask = minm(mask, sw=30)[-1]
  160.     mask = minm(mask, sw=20, threshold=65536//21)[-1]
  161.    
  162.     clip = core.resize.Point(clip, format=fmt.replace(subsampling_w=0, subsampling_h=0).id).std.Binarize(thr)
  163.     clip = core.std.Expr(split(clip), 'x y z max max')
  164.     clip = iterate(clip, core.std.Minimum, radius)
  165.    
  166.     def binarize_frame(n, f, clip=clip): return core.std.BlankClip(clip, 1, 1, color=peak if f.props.PlaneStatsMax else 0)
  167.    
  168.     prop_src = clip.std.Crop(bordfix,bordfix,bordfix,bordfix).std.PlaneStats()
  169.     clip = core.std.FrameEval(core.std.BlankClip(clip, 1, 1), binarize_frame, prop_src=prop_src)
  170.    
  171.     if min_length == 2:
  172.         med = clip.tmedian.TemporalMedian(1) # faster than Clense
  173.         clip = core.std.Expr([clip, med], 'x y min')
  174.     if min_length > 2:
  175.         avg = core.misc.AverageFrames(clip, [1] * ((min_length * 2) + 1)).std.Binarize(peak//2)
  176.         clip = core.std.Expr([clip, avg], 'x y min')
  177.     if smooth > 0:
  178.         clip = zzt.shiftframes(clip, [-smooth, smooth])
  179.         clip = zzt.combine(clip)
  180.         clip = core.misc.AverageFrames(clip, [1] * ((smooth * 2) + 1))
  181.         smooth //= 2
  182.         mask = zzt.shiftframes(mask, [-smooth, smooth])
  183.         mask = zzt.combine(mask)
  184.         mask = core.misc.AverageFrames(mask, [1] * ((smooth * 2) + 1))
  185.    
  186.     if debug:
  187.         alt = alt.text.Text('Family Friendly')
  188.         atx = atxref.text.Text('SEND BOBS AND VAGENE')
  189.     elif callable(unfuck_mpeg2):
  190.         atx = unfuck_mpeg2(atx)
  191.    
  192.     def _merge_tits(n, f):
  193.         weight = f.props.PlaneStatsMax
  194.         if weight == peak:
  195.             return alt.std.MaskedMerge(atx, mask, [0,1,2], True)
  196.         if weight == 0:
  197.             return alt
  198.         return core.std.Merge(alt, alt.std.MaskedMerge(atx, mask, [0,1,2], True), weight/peak)
  199.        
  200.     return [core.std.FrameEval(alt, _merge_tits, prop_src=clip.std.PlaneStats()), clip, mask]
  201.  
  202. def median(*clips): return core.std.Expr(clips, 'x y z min max y z max min')
  203.  
  204. def error_mask(clip, desc, thr1=2500, thr2=None, expand1=2, expand2=3, blur=3, bwbias=1, min_length=1):
  205.     y,u,v = split(clip)
  206.     start = 0
  207.     error = desc.resize.Bicubic(1920, 1080, filter_param_a=1/3, filter_param_b=1/3)
  208.     error = core.std.Expr([y, error], 'x y - abs')
  209.     if bwbias > 1:
  210.         chroma = core.std.Expr([u,v], 'x 32768 - abs y 32768 - abs max').resize.Point(1920, 1080)
  211.         bias = core.std.Expr([y, chroma], f'x {235 << 8} >= x {16 << 8} <= or y 0 = and {bwbias} 1 ?').std.Maximum().std.Maximum()
  212.  
  213.         error = core.std.Expr([error, bias], 'x y *')
  214.  
  215.     error = iterate(error, core.std.Maximum, expand1)
  216.     for x in range(expand1):
  217.         error = error.std.Maximum(coordinates=[[1]*8, [0,1,0,1,1,0,1,0]][min(start%3,1)])
  218.         start += 1
  219.  
  220.     if thr2 is not None:
  221.         error = error.std.Binarize(thr2).misc.Hysteresis(error.std.Binarize(thr1))
  222.     else:
  223.         error = error.std.Binarize(thr1)
  224.  
  225.     for x in range(expand2):
  226.         error = error.std.Maximum(coordinates=[[1]*8, [0,1,0,1,1,0,1,0]][min(start%3,1)])
  227.         start += 1
  228.    
  229.     if min_length > 1:
  230.         avg = core.misc.AverageFrames(error, [1] * ((min_length * 2) + 1)).std.Binarize(65535//2)
  231.         _error = core.std.Expr([error, avg], 'x y min')
  232.         _error = zzt.combine(zzt.shiftframes(_error, [-min_length, min_length]))
  233.         error = zzt.combine([error, _error], 'min')
  234.  
  235.     if blur:
  236.         return error.std.BoxBlur(hradius=blur, vradius=blur)
  237.     return error
  238.  
  239. def ringing_mask(clip, rad=4, ethr1=30<<8, ethr2=50<<8, minthr=0.5):
  240.     prew = clip.std.Prewitt()
  241.     mask_lo = prew.std.Binarize(ethr1)
  242.     mask_hi = prew.std.Binarize(ethr2)
  243.     main = core.std.Expr([mask_lo, mask_hi.std.Maximum().std.Maximum(), mask_hi], 'x y - z max')
  244.     main = main.std.Maximum().std.Maximum().std.Minimum().std.Minimum()
  245.     shrink = main.std.Minimum()
  246.     lines = core.std.Expr([clip.std.Maximum(**both).std.Maximum(**both).std.Minimum(**both).std.Minimum(**both), clip], 'x y -').std.Binarize(10<<8)
  247.     mask = core.std.Expr([main, shrink.std.Maximum(), main.std.Merge(shrink, minthr), lines], f'x y 0 = and x z ? a max')
  248.     imask = iterate(mask, core.std.Maximum, rad).std.Invert()
  249.     return [core.std.Expr([mask, imask], 'x y +'), prew.std.Maximum()]
  250.  
  251. def repair1(clip, repairclip, rad=1, alt=None, mode='ellipse'):
  252.     if rad == 1:
  253.         return clip.rgvs.Repair(repairclip, 1)
  254.     rmax = repairclip if alt is None else zzt.combine([repairclip, alt])
  255.     rmin = repairclip if alt is None else zzt.combine([repairclip, alt], 'min')
  256.     rmax = haf.mt_expand_multi(rmax, sw=rad, sh=rad, mode=mode)
  257.     rmin = haf.mt_inpand_multi(rmin, sw=rad, sh=rad, mode=mode)
  258.     return core.std.Expr([clip, rmax, rmin], 'x y min z max')
  259.  
  260. def show(*clips): return core.std.Interleave(clips, mismatch=1).resize.Spline36(format=vs.RGB24, dither_type='error_diffusion',matrix_in=1)
  261.  
  262. def post_adjust_aa(clip): return taa.TAAmbk(clip, aatype='Eedi3', alpha=0.2, beta=0.5, nrad=2, mdis=20)
  263.  
  264. def mt_xxpand_multi(clip, sw=1, sh=None, mode='square', planes=None, start=0, M__imum=core.std.Maximum, **params):
  265.     sh = fallback(sh, sw)
  266.     planes = list(range(clip.format.num_planes)) if planes is None else [planes] if isinstance(planes, int) else planes
  267.    
  268.     if mode == 'ellipse':
  269.         coordinates = [[1]*8, [0,1,0,1,1,0,1,0], [0,1,0,1,1,0,1,0]]
  270.     elif mode == 'losange':
  271.         coordinates = [[0,1,0,1,1,0,1,0]] * 3
  272.     else:
  273.         coordinates = [[1]*8] * 3
  274.    
  275.     clips = [clip]
  276.    
  277.     end = min(sw, sh) + start
  278.    
  279.     for x in range(start, end):
  280.         clips += [M__imum(clips[-1], coordinates=coordinates[x % 3], planes=planes, **params)]
  281.    
  282.     for x in range(end, end + sw - sh):
  283.         clips += [M__imum(clips[-1], coordinates=[0,0,0,1,1,0,0,0], planes=planes, **params)]
  284.    
  285.     for x in range(end, end + sh - sw):
  286.         clips += [M__imum(clips[-1], coordinates=[0,1,0,0,0,0,1,0], planes=planes, **params)]
  287.    
  288.     return clips
  289. maxm = partial(mt_xxpand_multi, M__imum=core.std.Maximum)
  290. minm = partial(mt_xxpand_multi, M__imum=core.std.Minimum)
  291. def csharp(flt, src, str=20):
  292.     np = flt.format.num_planes
  293.     blur = flt.rgvs.RemoveGrain(str)
  294.     return core.std.Expr([flt,src,blur], ['x dup + z - x y min max x y max min','',''][:np])
  295.  
  296.  
  297.  
  298.  
  299. atx = core.d2v.Source(r'C:\Users\Zastin\Downloads\Kaifuku Jutsushi no Yarinaoshi - 03 (AT-X HD MPEG2).ts.d2v')
  300. ivtc1 = fvf.JIVTC(atx, 1, thr=15, draft=False, tff=True)
  301. ivtc3 = fvf.JIVTC(atx, 3, thr=15, draft=False, tff=True)
  302. ivtc4 = fvf.JIVTC(atx, 4, thr=15, draft=False, tff=True)
  303. vrv = core.ffms2.Source(r'C:\Users\Zastin\Desktop\cartoons\[SubsPlease] Kaifuku Jutsushi no Yarinaoshi - 03 (1080p) [300B9475].mkv')[192:]
  304. atx = core.std.Splice([ivtc4[786:4887], ivtc1[5126:16658], vrv.resize.Spline16(1440)[15633:15705], ivtc3[16658:35072]])[:34105]
  305. atx = join([zzt.padding(x.std.Crop(left=1, right=1).edgefixer.ContinuityFixer(2,0,2,0), left=1, right=1) for x in split(atx)])
  306.  
  307. vrv = vrv[:34105]
  308. vrv = depth(vrv, 16)
  309.  
  310. atx = depth(atx, 16)
  311. atxref = atx.resize.Spline16(1920)
  312. atxref = fvf.rfs(atxref, vrv, '[0 5802][15320 16114][31076 33716][33999 34104]')
  313.  
  314. #core.std.StackVertical([vrv, atxref]).set_output()
  315.  
  316. o = vrv
  317. clip = vrv
  318.  
  319. ### DESCALE ###
  320.  
  321. y = get_y(clip)
  322. des = depth(depth(y, 32).descale.Debicubic(1488, 837, b=1/3, c=1/3),16)
  323. err = error_mask(clip, des, thr1=1500, thr2=3000, expand1=1, expand2=4, blur=3, bwbias=2, min_length=2)
  324.  
  325. rm, pw = ringing_mask(y)
  326.  
  327. nn = des.std.Transpose().znedi3.nnedi3(0,1,0,0,4,2,0,1).std.Transpose().znedi3.nnedi3(0,1,0,0,4,2,0,1).resize.Spline36(1920,1080,src_left=.5, src_top=.5)
  328.  
  329. rep = y.std.MaskedMerge(nn, rm)
  330. ar0 = nn.rgvs.Repair(rep,1)
  331. ar = ar0
  332.  
  333. luma = y.std.MaskedMerge(ar, pw.std.Binarize(25<<8).rgvs.RemoveGrain(20)).std.MaskedMerge(y, err)
  334.  
  335. clip = zz.mergechroma(luma, clip)
  336.  
  337. ### DECENSOR ###
  338.  
  339. clip, cen, cenmask = decensor(vrv, atx, radius=5, min_length=3, smooth=6, thr=20<<8, bordfix=10, alt=clip, unfuck_mpeg2=fuckMPEG2, debug=False)
  340. censor = core.std.Expr([cen.resize.Point(1920,1080), cenmask], 'x y 0 ?')
  341.  
  342. #show(clip).set_output()
  343.  
  344. ### DENOISE ###
  345.  
  346. mask = clip.std.Prewitt().std.Binarize([1500,1000]).rgvs.RemoveGrain(11)
  347. nr0 = zz.smd(clip, 2, 100).knlm.KNLMeansCL(3,2,4,0.5,'UV')
  348. nr = mvf.LimitFilter(nr0, clip, thr=3/4, thrc=2/3, elast=4)
  349. clip = median(clip, clip.deblock.Deblock(quant=21), nr).std.MaskedMerge(clip, mask)
  350.  
  351. ### DEBAND ###
  352. y,u,v = split(clip)
  353.  
  354.  
  355.  
  356. stats = y.std.PlaneStats()
  357. agm3 = core.adg.Mask(stats, 3)
  358.  
  359. ymax = maxm(y, sw=30, mode='ellipse')
  360. ymin = minm(y, sw=30, mode='ellipse')
  361. umax = maxm(u, sw= 2, mode='ellipse')[-1]
  362. umin = minm(u, sw= 2, mode='ellipse')[-1]
  363. vmax = maxm(v, sw= 2, mode='ellipse')[-1]
  364. vmin = minm(v, sw= 2, mode='ellipse')[-1]
  365.  
  366.  
  367.  
  368. ### edge detection
  369. thr = 3.2 * 256
  370. ypw0 = y.std.Prewitt()
  371. ypw = ypw0.std.Binarize(thr).std.Inflate()
  372.  
  373.  
  374.  
  375. ### range masks (neighborhood max - min)
  376.  
  377. rad = 3
  378. thr = 2.5 * 256
  379. yrangesml0 = core.std.Expr([ymax[3], ymin[3]], 'x y - abs')
  380. yrangesml = yrangesml0.std.Binarize(thr).std.BoxBlur(0,2,1,2,1)
  381.  
  382. rad = 16
  383. thr = 4 * 256
  384. yrangebig0 = core.std.Expr([ymax[rad], ymin[rad]], 'x y - abs')
  385. yrangebig = yrangebig0.std.Binarize(thr)
  386. yrangebig = minm(yrangebig, sw=4, threshold=65535, mode='ellipse')[-1]
  387. yrangebig = minm(yrangebig, sw=12, threshold=65536 // 13, mode='ellipse')[-1]
  388.  
  389.  
  390. ### morphological masks (shapes)
  391.  
  392. rad = 30
  393. thr = 1 * 256
  394. ymph = core.std.Expr([y, maxm(ymin[rad], sw=rad, mode='ellipse')[rad],
  395.                          minm(ymax[rad], sw=rad, mode='ellipse')[rad]], 'x y - z x - max')
  396. ymph = ymph.std.Binarize(256)
  397. ymph = ymph.std.Minimum().std.Maximum()
  398. ymph = maxm(ymph, sw=5, threshold=65536//6)[-1]
  399.  
  400.  
  401. ymaskmain = zzt.combine([yrangebig, yrangesml0.std.Binarize(384).std.BoxBlur(0,2,1,2,1)])
  402.  
  403.  
  404. grad_mask = zzt.combine([ymph, yrangesml, ypw])
  405. grain_excl = core.std.Expr([ymax[6], ymin[6]], 'x y - abs').std.Binarize(6<<8).std.Minimum().std.Minimum(**both).std.BoxBlur(0,4,1,4,1)
  406. grain_mask = core.std.Expr([yrangebig, grad_mask, grain_excl], '65535 y - x min z -')
  407. grain_mask = core.std.Expr([grain_mask.std.Binarize(65535).misc.Hysteresis(grain_mask), grain_mask], 'x y 0 ?')
  408. grain_mask = grain_mask.std.BoxBlur(0,16,1,16,1)
  409.  
  410.  
  411. ydebn_src = y.dfttest.DFTTest(sigma=1, tbsize=1, sbsize=48, sosize=36)
  412. ydebn_src = csharp(ydebn_src, y).std.MaskedMerge(y, zzt.combine([ymph, ymaskmain]))#.std.MaskedMerge(ydebn_src, censor)
  413.  
  414. ydebn_nomask = ydebn_src.f3kdb.Deband(16, 41, 0, 0, 0, 0, output_depth=16)
  415. ydebn_weak = y.f3kdb.Deband(16, 25, 0, 0, 0, 0, output_depth=16)
  416. ydebn = ydebn_nomask.std.MaskedMerge(csharp(ydebn_nomask, y).std.Merge(y, 2/3), ymph)
  417. ydebn = ydebn.std.MaskedMerge(ydebn_weak, ymaskmain)
  418. ydebn = ydebn.std.MaskedMerge(ydebn_nomask, censor)
  419.  
  420. strong_grain = ydebn_nomask.std.Merge(ydebn).grain.Add(0.6, constant=True, seed=69)
  421. normal_grain = ydebn.std.MaskedMerge(ydebn.grain.Add(0.3, constant=True, seed=69), agm3)
  422. y_final = normal_grain.std.MaskedMerge(strong_grain, grain_mask).std.MaskedMerge(ydebn, grain_excl)
  423.  
  424.  
  425. usml = core.std.Expr([umax, umin], 'x y - abs').std.Binarize(384).std.BoxBlur(0,2,1,2,1)
  426. vsml = core.std.Expr([vmax, vmin], 'x y - abs').std.Binarize(384).std.BoxBlur(0,2,1,2,1)
  427. cmask = yrangesml.resize.Bilinear(960, 540, src_left=-0.5)
  428. cmask = cmask.std.Expr('x 2 /')
  429. cmask = zz.yuv(yrangesml, zzt.combine([usml, cmask]), zzt.combine([vsml, cmask]))
  430.  
  431.  
  432. clip = zz.mergechroma(y_final, clip)
  433. clip = clip.f3kdb.Deband(16, 0, 41, 41, 0, 0, 2, output_depth=16).std.MaskedMerge(clip, cmask, [1,2])
  434.  
  435. depth(clip, 10).set_output()
  436.  
  437.  
  438.  
  439. #vspipe "C:/Users/Zastin/Desktop/WIP/healer/03.vpy" -y - | "C:\Users\Zastin\Desktop\WIP\x264_x64.exe" --demuxer y4m -f -1 --fade-compensate 0.5 --threads 8 --preset veryslow --psy-rd 0.85 --merange 32 -b 16 --crf 14.69 --aq-mode 3 --qcomp 0.75 --output-depth 10 --colormatrix bt709 --colorprim bt709 --transfer bt709 --range tv --stitchable -o "C:\Users\Zastin\Desktop\wip\healer\03.h264" -
  440.  
Add Comment
Please, Sign In to add comment