Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import vapoursynth as vs
- import havsfunc as haf
- import mvsfunc as mvf
- import debandshit as db # https://github.com/LightArrowsEXE/debandshit/blob/master/debandshit.py
- import rgvs # https://github.com/Irrational-Encoding-Wizardry/RgToolsVS/blob/master/rgvs.py
- from vsutil import *
- from functools import partial
- from math import log2
- core = vs.core
- FSRCNNX = r'C:\Users\Zastin\AppData\Roaming\mpv\shaders\FSRCNNX_x2_56-16-4-1.glsl'
- BDMV = r'C:\Users\Zastin\Desktop\cartoons\[BDMV] Monster Girl Doctor Volume 1\MON_ISHA_I\BDMV\STREAM\00002.m2ts'
- both = dict(coordinates=[0,1,0,1,1,0,1,0])
- def show(*clips): return core.std.Interleave(clips, mismatch=1).resize.Spline36(format=vs.RGB24, dither_type='error_diffusion',matrix_in=1)
- def median(*clips): return core.std.Expr(clips, 'x y z min max y z max min')
- def csharp(flt, src, blur=lambda x: x.rgvs.RemoveGrain(20)):
- pre = rgvs.MinBlur(flt, 0)
- return core.std.Expr([flt,src,pre,blur(pre)], 'x z + a - x y min max x y max min')
- def unsharp(clip, radius=1, strength=log2(3), custom=None):
- if callable(custom):
- return clip.std.MergeDiff(clip.std.MakeDiff(custom(clip)))
- strength = max(1e-6, min(strength, log2(3)))
- weight = 0.5 ** strength / ((1 - 0.5 ** strength) / 2)
- # find matrix with least rounding error when using integer clips
- if clip.format.sample_type == 0:
- all_matrices = [[x] for x in range(1, 1024)]
- for x in range(1023):
- while len(all_matrices[x]) < radius * 2 + 1:
- all_matrices[x].append(all_matrices[x][-1] / weight)
- error = [sum([abs(x - round(x)) for x in matrix[1:]]) for matrix in all_matrices]
- matrix = [round(x) for x in all_matrices[error.index(min(error))]]
- else:
- matrix = [1]
- while len(matrix) < radius * 2 + 1:
- matrix.append(matrix[-1] / weight)
- matrix = [matrix[x] for x in [(2,1,2,1,0,1,2,1,2), (4,3,2,3,4,3,2,1,2,3,2,1,0,1,2,3,2,1,2,3,4,3,2,3,4)][radius-1]]
- return clip.std.MergeDiff(clip.std.MakeDiff(clip.std.Convolution(matrix)))
- def pre_aa(clip, radius=1, strength=log2(3), pp=None, **nnedi3_params):
- sharp = unsharp(clip, radius=radius, strength=strength)
- nedi = clip.znedi3.nnedi3(2, **nnedi3_params).std.SeparateFields(True).std.SelectEvery(4, [0, 3]).std.DoubleWeave()[::2]
- if callable(pp):
- nedi = pp(nedi)
- return core.std.Expr([sharp, clip, nedi], 'x y z max min y z min max')
- def placebo_yuv(clip, iterations, threshold, radius):
- planes = split(clip)
- return join([planes[x].placebo.Deband(1, iterations[x], threshold[x], radius[x], 0) for x in range(3)])
- def MDegrain(clip, clipint):
- dft = clip.dfttest.DFTTest(tbsize=1, slocation=[0.0,4.0, 0.2,9.0, 1.0,15.0], sbsize=16, sosize=12)#.std.MaskedMerge(clip, clip.std.Expr(f'x {59/219} >= 0 x {59/219} - {1/(0-(59/219))} * ?'))
- med1 = depth(clipint.std.Median(), 32)
- med2 = depth(clipint.ctmf.CTMF(2), 32)
- blur1 = rgvs.removegrain(clip, 20)
- blur2 = rgvs.blur(clip, 2)
- minblur1 = median(clip, med1, blur1)
- minblur2 = median(clip, med2, blur2)
- diff = clip.std.MakeDiff(blur2)
- sbr2 = rgvs.MedianDiff(clip, diff, rgvs.blur(diff, 2))
- pre3 = median(clip, dft, sbr2)
- pre2 = median(clip, pre3, minblur2)
- pre1 = median(clip, pre2, minblur1)
- asup = clip.mvsf.Super(32, 32, 1, rfilter=4)
- rsup1= pre1.mvsf.Super(32, 32, 1, 1)
- rsup2= pre2.mvsf.Super(32, 32, 1, 1)
- rsup3= pre3.mvsf.Super(32, 32, 1, 1)
- msup = clip.mvsf.Super(32, 32, 1, 1)
- vec = core.mvsf.Analyze(asup, 3, 32, 32, search=3, truemotion=False, overlap=16, overlapv=16, badrange=-24, trymany=True)
- vec = core.mvsf.Recalculate(rsup1, vec, thsad=100, blksize=16, blksizev=16, search=3, truemotion=False, overlap=8, overlapv=8)
- vec = core.mvsf.Recalculate(rsup2, vec, thsad= 50, blksize=8, blksizev=8, search=3, truemotion=False, overlap=4, overlapv=4)
- vec = core.mvsf.Recalculate(rsup3, vec, thsad= 25, blksize=4, blksizev=4, search=3, truemotion=False, overlap=2, overlapv=2)
- mdg = core.mvsf.Degrain(clip, msup, vec, thsad=200)
- return core.std.Expr([mdg, clip, dft, med1, med2, blur1, blur2, sbr2], 'x y z a b c d e min min min min min min max y z a b c d e max max max max max max min')
- def mergechroma(*clips): return core.std.ShufflePlanes(clips, [0, 1, 2], vs.YUV)
- o = core.lsmas.LWLibavSource(BDMV)
- clip = depth(o, 16)
- y = get_y(clip)
- y0=y
- mask = y.std.Prewitt().std.Maximum()
- lo, hi = 15<<8, 25<<8
- aamask1a= mask.std.Binarize(20<<8)
- aamask1b= mask.std.Binarize(30<<8)\
- .std.Maximum().std.Maximum().std.Maximum().std.Maximum()\
- .std.Minimum().std.Minimum().std.Minimum()
- aamask1 = core.std.Expr([aamask1a, aamask1b], 'x y min')
- aamask2 = mask.std.Binarize(50<<8)
- mclip = aamask1.std.Maximum().resize.Point(1920*2, 1080*2)
- nemask = aamask2.std.Maximum().resize.Point(1920*2, 1080*2)
- lo, hi = 4<<8, 6<<8
- masknr = mask.std.Expr(f'x {lo} <= 0 x {hi} >= 65535 x {lo} - {65535/(hi-lo)} * ? ?').std.Convolution([1]*9)
- masknrc = mask.std.Binarize(2500).resize.Bicubic(960, 540, src_left=-0.5, filter_param_a=1, filter_param_b=0)
- gmask = core.std.Expr([masknr, mask.std.Binarize(30<<8).std.Maximum().std.Maximum().std.BoxBlur(0,2,1,2,1)], 'x y max')
- pre = pre_aa(y,pscrn=1)
- pre = pre.std.Transpose()
- pre = pre_aa(pre,pscrn=1)
- fsr = get_y(join([pre]*3).placebo.Shader(shader=FSRCNNX, filter='box', height=1920*2, width=1080*2))
- thr = 85
- expr = f'x y - x z - * 0 < x x y - abs x z - abs > z z y {thr} + min y {thr} - max ? ?'
- aa = fsr
- nn = aa.znedi3.nnedi3(0,0,0,0,4,2,0,1)
- ee = aa.eedi3m.EEDI3(0,0,0, .2, .5, 69, sclip=nn, mclip=mclip.std.Transpose())
- ne = core.std.Expr([aa,nn,ee], expr)
- aa = ne.std.MaskedMerge(ee, nemask.std.Transpose())
- aa = aa.std.Transpose()
- nn = aa.znedi3.nnedi3(0,0,0,0,4,2,0,1)
- ee = aa.eedi3m.EEDI3(0,0,0, .2, .5, 69, sclip=nn, mclip=mclip)
- ne = core.std.Expr([aa,nn,ee], expr)
- aa = ne.std.MaskedMerge(ee, nemask)
- aa = aa.resize.Spline36(1920,1080)
- bil = aa.bilateral.Bilateral(sigmaS=2, sigmaR=4 / 255, algorithm=0)
- aa = core.std.Expr([bil, aa, pre.std.Transpose(), y], 'x y z a max max min y z a min min max')
- y = y.std.MaskedMerge(aa, aamask1.rgvs.RemoveGrain(20))
- pstat = y.std.PlaneStats()
- uv = clip.knlm.KNLMeansCL(3,2,4,.3,'UV').std.MaskedMerge(clip, clip.std.Prewitt(planes=[1,2]).std.Binarize(5<<8,planes=[1,2]).std.Inflate(planes=[1,2]), planes=[1,2])
- rgb = y.resize.Spline16(format=vs.RGB48, dither_type='error_diffusion', matrix_in=1)
- opp = rgb.bm3d.RGB2OPP(sample=0)
- opp_y = get_y(opp)
- basic = core.bm3d.VBasic(opp_y, sigma=[2,0,0], radius=2, matrix=100, profile="fast").bm3d.VAggregate(radius=2, sample=0)
- mdg = MDegrain(depth(y, 32), y)
- mdg = mdg.resize.Spline16(format=vs.RGB48, dither_type='error_diffusion', matrix_in=1).bm3d.RGB2OPP(sample=0).std.ShufflePlanes(0, vs.GRAY)
- ref = median(opp_y, mdg, basic)
- flt = core.bm3d.VFinal(opp_y, ref, sigma=[2,0,0], radius=2, matrix=100, profile="fast").bm3d.VAggregate(radius=2, sample=0)
- flt = csharp(flt, opp_y)
- flt = median(opp_y, flt, ref)
- flt = core.std.ShufflePlanes([flt,opp], [0,1,2], vs.YUV)
- flt = core.bm3d.OPP2RGB(flt, sample=1)
- flt = flt.resize.Point(format=vs.YUV444P16, dither_type='error_diffusion', matrix=1)
- flt = get_y(flt)
- flt0 = flt
- flt = flt.std.MaskedMerge(y, core.adg.Mask(pstat).std.Deflate().std.Expr('x 45000 min'))
- flt = median(y, y.deblock.Deblock(quant=21), flt)
- flt = flt.rgvs.Repair(y, 13)
- flt = mvf.LimitFilter(flt, y, elast=4)
- flt = flt.std.MaskedMerge(y, masknr)
- clip = mergechroma(flt, uv)
- mask = db.rangemask(mergechroma(flt0, uv), 2, 2).std.Binarize([420,384])
- maskpp = mask.std.Deflate()
- masku, maskv = [core.std.Expr([x, masknrc], 'x y max') for x in split(maskpp)[1:]]
- lo, hi = 65536 * 0.2, 65536 * 0.5
- maskb = get_y(mask).std.BoxBlur(0,8,1,8,1).std.Expr(f'x {lo} - {65535/(hi-lo)} *')
- maskb = core.std.Expr([maskb, maskb.std.BoxBlur(0,4,1,4,1)], 'x y min')
- mask1 = get_y(maskpp)
- mask2 = db.rangemask(flt0,15,2).std.Binarize(6 * 256)
- mask2 = haf.mt_inpand_multi(mask2, sw=15 * 3 // 4, sh=15 * 3 // 4, mode='ellipse').std.BoxBlur(0,15//4,1,15//4,1)
- mask = core.std.Expr([maskb,mask1,mask2], 'x y z max max')
- mask = join([mask, masku, maskv])
- debn = placebo_yuv(clip, [1,1,1], [2.5,2,2], [16,10,10]).std.MaskedMerge(db.f3kdb(clip, 15, 32, 0, 0, 0, 0), mask)
- final = debn.grain.Add(0.2, constant=True).std.MaskedMerge(debn, gmask, 0)
- depth(final, 10).set_output()
- #vspipe "C:/Users/Zastin/Desktop/WIP/monisha/final.vpy" -y - | "C:\Users\Zastin\Desktop\WIP\x264_x64.exe" --demuxer y4m -f -2 --psy-rd 0.92 --preset veryslow --subme 11 --stitchable --fade-compensate 0.5 --no-dct-decimate -b 16 --crf 15 --chroma-qp-offset -1 --threads 8 --aq-mode 3 --aq-strength 0.9 --qcomp 0.75 --output-depth 10 --colormatrix bt709 --colorprim bt709 --transfer bt709 --range tv -o "C:\Users\Zastin\Desktop\WIP\monisha\01.h264" -
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement