SHARE
TWEET

Untitled

a guest Jun 25th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import vapoursynth as vs
  2. import sys
  3. import math
  4. import havsfunc as haf
  5. import mvsfunc as mvf
  6.  
  7. core = vs.get_core(threads = 12)
  8. core.max_cache_size = 8000
  9.  
  10. def scale(val, bits):
  11.         return val * ((1 << bits) - 1) // 255
  12.  
  13. def Levels(clip, input_low, gamma, input_high, output_low, output_high, planes=None, coring=True):
  14.     core = vs.get_core()
  15.     funcName = 'Levels'
  16.  
  17.     if not isinstance(clip, vs.VideoNode):
  18.         raise TypeError(funcName + ': \"clip\" must be a clip!')
  19.    
  20.     if planes is None:
  21.         planes = list(range(clip.format.num_planes))
  22.  
  23.     bits = clip.format.bits_per_sample
  24.     neutral = 1 << (bits - 1)
  25.     peak = (1 << bits) - 1
  26.  
  27.     gamma = 1 / gamma
  28.     divisor = input_high - input_low + (input_high == input_low)
  29.    
  30.     tvLow = haf.scale(16, bits)
  31.     tvHigh = [haf.scale(235, bits), haf.scale(240, bits)]
  32.     scaleUp = peak / haf.scale(235 - 16, bits)
  33.     scaleDown = haf.scale(235 - 16, bits) / peak
  34.  
  35.     isGray = clip.format.color_family == vs.GRAY
  36.     chroma_planes = []
  37.     if not isGray:
  38.         if 1 in planes:
  39.             chroma_planes.append(1)
  40.         if 2 in planes:
  41.             chroma_planes.append(2)
  42.        
  43.     def get_lut1(x):
  44.         p = ((x - tvLow) * scaleUp - input_low) / divisor if coring else (x - input_low) / divisor
  45.         p = min(max(p, 0), 1) ** gamma * (output_high - output_low) + output_low
  46.         return min(max(math.floor(p * scaleDown + tvLow + 0.5), tvLow), tvHigh[0]) if coring else min(max(math.floor(p + 0.5), 0), peak)
  47.  
  48.     def get_lut2(x):
  49.         q = math.floor((x - neutral) * (output_high - output_low) / divisor + neutral + 0.5)
  50.         return min(max(q, tvLow), tvHigh[1]) if coring else min(max(q, 0), peak)
  51.    
  52.     if 0 in planes:
  53.         clip = core.std.Lut(clip, planes=[0], function=get_lut1)
  54.     if chroma_planes:
  55.         clip = core.std.Lut(clip, planes=chroma_planes, function=get_lut2)
  56.    
  57.     return clip
  58.  
  59. def ringmask2(clip, rx=None, amp=None, thY1=0, thY2=255, mode=1):
  60.     w = clip.width
  61.     h = clip.height
  62.     bits = clip.format.bits_per_sample
  63.     peak = (1 << bits) - 1
  64.     ry = rx
  65.  
  66.     thY1 = scale(thY1, bits)
  67.     thY2 = scale(thY2, bits)
  68.  
  69.     smooth = core.fmtc.resample(clip, haf.m4(w / rx), haf.m4(h / ry), kernel='bicubic',fulls=False, fulld=False).fmtc.resample(w, h, kernel='bicubic', a1=1, a2=0,fulls=False, fulld=False)
  70.     smoother = core.fmtc.resample(clip, haf.m4(w / rx), haf.m4(h / ry), kernel='bicubic',fulls=False, fulld=False).fmtc.resample(w, h, kernel='bicubic', a1=1.5, a2=-0.25,fulls=False, fulld=False)
  71.  
  72.     expr = 'x y - {amp} *'.format(amp=amp) if mode == 1 else 'y x - {amp} *'.format(amp=amp)
  73.     mask = core.std.Expr([smooth, smoother], [expr]).fmtc.bitdepth(bits=bits, fulls=True, fulld=True, dmode=1)
  74.  
  75.     limitexpr = 'x {thY1} < 0 x {thY2} >= {peak} x ? ?'.format(thY1=thY1, thY2=thY2, peak=peak)
  76.     mask = core.std.Expr([mask], [limitexpr])
  77.  
  78.     return mask
  79.  
  80. def dr_only(clp,strength1,strength2,dering_line_mask2):
  81.     bits = clp.format.bits_per_sample
  82.     multiple = scale(1, bits)
  83.     x = clp.width
  84.     y = clp.height
  85.     x  = round((x / 4.0) * 4)
  86.     y  = round((y / 4.0) * 4)
  87.     x2 = x // 2
  88.     y2 = y // 2
  89.     xs = x * 3 // 2
  90.     ys = y * 3 // 2
  91.     upsampling_args = dict(kernel='lanczos', taps=4, noring=True)
  92.     downsampling_args = dict(kernel='spline36')
  93.  
  94.     ps1 = core.std.MaskedMerge(clp, core.rgvs.RemoveGrain(clp, [20]), Levels(dering_line_mask2, scale(60, bits), 3.0, scale(140, bits), 0, scale(strength1, bits)))
  95.     halo = core.fmtc.resample(ps1, x2, y2, kernel='bicubic',fulls=False, fulld=False).fmtc.resample(x, y, kernel='bicubic', a1=1, a2=0,fulls=False, fulld=False).fmtc.bitdepth(bits=bits,fulls=False, fulld=False)
  96.     halo_mask1 = core.std.Expr([core.std.Maximum(ps1), core.std.Minimum(clp)], ['x y -'])
  97.     halotomask = core.std.Expr([core.std.Maximum(halo), core.std.Minimum(halo)], ['x y -'])
  98.     halotomask2 = core.std.Expr([halotomask, halo_mask1], ['y {multiple} / x {multiple} / - y {multiple} / 0.001 + / 255 * 50 - y {multiple} / 256 + 512 / 0.5 + * {multiple} *'.format(multiple=multiple)])
  99.     pshalo = core.std.MaskedMerge(halo, ps1, halotomask2)
  100.     pshalo2 = haf.Resize(ps1, xs, ys, bits=bits, **upsampling_args)
  101.     pshalo2 = core.std.Expr([pshalo2, core.std.Maximum(pshalo).fmtc.resample(xs, ys, kernel='bicubic').fmtc.bitdepth(bits=bits)], ['x y min'])
  102.     pshalo2 = core.std.Expr([pshalo2, core.std.Minimum(pshalo).fmtc.resample(xs, ys, kernel='bicubic').fmtc.bitdepth(bits=bits)], ['x y max'])
  103.     pshalo2 = haf.Resize(pshalo2, x, y, bits=bits, **downsampling_args)
  104.     ps2 = core.std.Expr([ps1, pshalo2], ['x y < x x y - {} * - x x y - 1.0 * - ?'.format(strength2)])
  105.  
  106.     return ps2
  107.  
  108. #OKE:INPUTFILE
  109. a="00001.m2ts"
  110. src8 = core.lsmas.LWLibavSource(a, threads=1)
  111. src16 = core.fmtc.bitdepth(src8,bits=16)
  112.  
  113. Y = core.fmtc.resample(src16,960,540,sx=-0.5)
  114. down = core.std.ShufflePlanes([Y,src16], [0,1,2], vs.YUV)
  115. nr16y = core.knlm.KNLMeansCL(src16, d=1, a=3, s=2, wmode=3, h=2.0, device_type="GPU")
  116. nr16uv = core.knlm.KNLMeansCL(down, d=1, a=2, s=2, channels="YUV", wmode=3, h=1.2, device_type="GPU")
  117. nr16 = core.std.ShufflePlanes([nr16y,nr16uv], [0,1,2], vs.YUV)
  118. noise16 = core.std.MakeDiff(src16, nr16)
  119. pre16 = core.std.ShufflePlanes([nr16y,src16],[0,1,2],vs.YUV)
  120. pre8 = mvf.Depth(pre16, depth=8, dither=5)
  121.  
  122. eemask = core.tcanny.TCanny(src8,sigma=0.8,mode=1,op=2,gmmax=255, planes=[0,1,2])
  123. eemask_u = core.std.ShufflePlanes(eemask, [1,1,1], vs.YUV).fmtc.resample(1920,1080,sx=0.25,css="420")
  124. eemask_v = core.std.ShufflePlanes(eemask, [2,2,2], vs.YUV).fmtc.resample(1920,1080,sx=0.25,css="420")
  125. weighted = core.std.Expr([eemask,eemask_u,eemask_v],["x 128 * y + z +",""],vs.YUV420P16)
  126.  
  127. nrmasks = core.std.Binarize(weighted, 4800, 0)
  128. dmaskb = core.std.Binarize(weighted, 2400, 0)
  129. dmasks = core.std.Binarize(weighted, 3600, 0)
  130. dmask = core.misc.Hysteresis(dmasks, dmaskb, 0)
  131.  
  132. aamask = core.std.Binarize(weighted, 6400, 0)
  133. aamask = core.std.Maximum(aamask,0).std.Maximum(0).std.Minimum(0).std.Minimum(0)
  134. aamask = core.rgvs.RemoveGrain(aamask,[20,0])
  135.  
  136. Y1080 = core.std.ShufflePlanes(pre16, 0, vs.GRAY)
  137. U1080 = core.std.ShufflePlanes(pre8, 1, vs.GRAY).fmtc.resample(1920,1080,sx=0.25,kernel="lanczos")
  138. V1080 = core.std.ShufflePlanes(pre8, 2, vs.GRAY).fmtc.resample(1920,1080,sx=0.25,kernel="lanczos")
  139.  
  140. textmask = core.std.Expr([Y1080,U1080,V1080],"x 57600 >= y 32768 - abs 770 <= and z 32768 - abs 770 <= and 65535 0 ?")
  141. textmask_mod = core.std.Minimum(textmask).std.Minimum().std.Maximum().std.Maximum()
  142. textmask = core.std.Expr([textmask, textmask_mod], "x y -")
  143. textmask = core.std.ShufflePlanes([textmask,src16],[0,1,2],vs.YUV)
  144. textmask = core.std.Maximum(textmask,0).std.Maximum(0).std.Maximum(0).rgvs.RemoveGrain([20,0]).rgvs.RemoveGrain([20,0])
  145. aamask = core.std.Expr([aamask, textmask, weighted], ["z 10000 > x y - x ?",""], vs.YUV420P16)
  146.  
  147. luma = core.std.ShufflePlanes(pre8, 0, vs.YUV).resize.Bilinear(format=vs.YUV420P8)
  148. eemask = core.std.Expr([eemask,luma],["x x * 60 * 255 y - dup * 0.4 * + 60000 min","x x * 80 * 255 y - dup * 0.6 * + 60000 min"],vs.YUV420P16)
  149. eemask = core.std.Maximum(core.std.Maximum(eemask,planes=[0,1,2]),planes=[0,1,2])
  150. eemask = core.rgvs.RemoveGrain(eemask, [20,11]).rgvs.RemoveGrain([20,11])
  151.  
  152. nrmaskg = core.tcanny.TCanny(pre8,sigma=1.0,t_h=8.0,t_l=1.2,op=2,planes=0)
  153. nrmaskb = core.tcanny.TCanny(pre8,sigma=1.4,t_h=9.0,t_l=1.5,op=2,planes=0)
  154.  
  155. nrmask = core.std.Expr([nrmaskg,nrmaskb,nrmasks,pre8,dmask],["a 20 < 65535 a 64 < x 257 * b max a 128 < y 257 * z ? ? ?","",""],vs.YUV420P16)
  156. nrmask = core.std.Maximum(nrmask,0).std.Maximum(0).std.Minimum(0)
  157. nrmask = core.rgvs.RemoveGrain(core.rgvs.RemoveGrain(nrmask,[20,0]),[20,0])
  158.  
  159. w       = 1920
  160. h       = 1080
  161.  
  162. aa_clip = core.std.ShufflePlanes(src16, 0, vs.GRAY)
  163. aa_clip = core.eedi2.EEDI2(aa_clip, field=1, mthresh=10, lthresh=20, vthresh=20, maxd=24, nt=50)
  164. aa_clip = core.fmtc.resample(aa_clip, w, h, sy=[-0.5,-1]).std.Transpose()
  165. aa_clip = core.eedi2.EEDI2(aa_clip, field=1, mthresh=10, lthresh=20, vthresh=20, maxd=24, nt=50)
  166. aa_clip = core.fmtc.resample(aa_clip, h, w, sy=[-0.5,-1]).std.Transpose()
  167. edge = core.std.ShufflePlanes([aa_clip, src16], [0,1,2], vs.YUV)
  168. aaed = core.std.MaskedMerge(src16, edge, aamask, [0,1,2], True)
  169.  
  170. preblur = core.std.MakeDiff(aaed,noise16)
  171.  
  172. dbed = core.f3kdb.Deband(preblur,12,48,64,64,0,0,output_depth=16)
  173. dbed = core.f3kdb.Deband(dbed,24,32,48,48,0,0,output_depth=16)
  174. dbed = mvf.LimitFilter(dbed, preblur, thr=0.6, thrc=0.8, elast=1.5)
  175. dbed = core.std.MaskedMerge(dbed, preblur, nrmask, first_plane=1)
  176.  
  177. dif = core.std.MakeDiff(dbed, core.rgvs.RemoveGrain(dbed, 20))
  178. sharp = core.std.MergeDiff(dbed, dif)
  179. sharped = core.std.MaskedMerge(sharp, dbed, eemask, [0,1,2], False)
  180.  
  181. noise16 = core.std.Expr(noise16,["x 32768 - 1.05 * 32768 +",""])
  182. nullclip = core.std.Expr(src16,["32768",""])
  183. nrweight = core.std.Expr(pre8, ["x 64 - 0 max dup * 4 * ",""], vs.YUV420P16)
  184. noise16 = core.std.MaskedMerge(noise16,nullclip,nrweight,0,True)
  185. res = core.std.MergeDiff(sharped,noise16)
  186.  
  187. ringmask = ringmask2(src16, 1.5, 50)
  188. ringmask = core.std.Expr([ringmask, luma, textmask],["y 16 > y 235 < and x z - 0 ?",""])
  189. gamma=3.0
  190. pre = core.std.Levels(res, 4096, 60160, 1/gamma, 4096, 60160)
  191. dr = dr_only(pre,strength1=0,strength2=0,dering_line_mask2=ringmask)
  192. dr = core.std.Levels(dr, 4096, 60160, gamma, 4096, 60160)
  193. res = core.std.MaskedMerge(res, dr, ringmask, 0, True)
  194. res = core.std.SetFrameProp(res, prop="_ChromaLocation", intval = 0)
  195.  
  196. #OKE:DEBUG
  197. Debug = 1
  198. if Debug:
  199.     res=core.std.Interleave([src16, res])
  200.     res=mvf.ToRGB(res,full=False,depth=8)
  201. else: res = core.fmtc.bitdepth(res,bits=10)
  202.  
  203. res.set_output()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top