Guest User

DeHaloHmod

a guest
Apr 1st, 2017
298
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # DeHaloHmod v.2.35
  2.  
  3. function GreyCenteredToMask_dhh(clip input) {
  4. # Levels will convert to lut in avs26 by this "x input_low - input_high input_low - / 1 gamma / ^ output_high output_low - * output_low +"
  5. VersionNumber() < 2.6 ? input.Levels(128, 1, 255, 0, 255, false) : input.mt_lut("x range_half - range_max range_half - / range_max 0 - * 0 +")
  6. one = last
  7. VersionNumber() < 2.6 ? input.Levels(0, 1, 128, 255, 0, false) : input.mt_lut("x 0 - range_half 0 - / 0 range_max - * range_max +")
  8. two = last
  9. Overlay(one, two, mode="lighten")
  10. }
  11.  
  12.  
  13. function FastLineDarkenMOD3_dhh( clip input, float "strength", float "prot", float "luma_cap", float "threshold", float "thinning")
  14. {
  15. ## parameters ##
  16. str        = string(default(strength, 48) /128.)
  17. lum        = string(default(luma_cap, 191))
  18. protection = default(prot,5)
  19. thr        = string(default(threshold, 4))
  20. thinning   = default(thinning,0)
  21. thn        = string(thinning /16.)
  22.  
  23. Assert(!(input.isrgb()), "FastLineDarkenMOD4: RGB Color formats is not supported" )
  24.  
  25. ssispmt   = Findstr(VersionString(), "AviSynth+") != 0 && Findstr(VersionString(), "r1576") == 0
  26.  
  27. sislumaonly = ssispmt ? input.isy() : VersionNumber() < 2.6 ? true : input.isy8()
  28.  
  29. c = sislumaonly ? input : ssispmt ? input.converttoy() : input.converttoy8()
  30.  
  31. ## filtering ##
  32. exin    = c.mt_expand(thy=255/(protection+1)).mt_inpand()
  33. diff    = VersionNumber() < 2.6 ? mt_lutxy(c,exin,yexpr="y "+lum+" < y "+lum+" ? x "+thr+" + > x y "+lum+" < y "+lum+" ? - 0 ? 127 +",uexpr="x",vexpr="x")
  34.           \                     : mt_lutxy(c,exin,yexpr="y "+lum+" scalef < y "+lum+" scalef ? x "+thr+" scalef + > x y "+lum+" scalef < y "+lum+" scalef ? - 0 ? 127 scalef +")
  35. linemask= thinning != 0 ? VersionNumber() < 2.6 ? mt_lut(diff.mt_inpand(),"x 127 - "+thn+" * 255 +").mt_convolution("1 1 1","1 1 1")
  36.             \           : mt_lut(diff.mt_inpand(),"clamp_f_i8 x 127 scalef - "+thn+" * range_max +").mt_convolution("1 1 1","1 1 1") : nop()
  37. thick   = VersionNumber() < 2.6 ? mt_lutxy(c, exin, yexpr="y "+lum+" < y "+lum+" ? x "+thr+" + > x y "+lum+" < y "+lum+" ? - 0 ? "+str+" * x +",uexpr="x",vexpr="x",u=2, v=2)
  38.           \                     : mt_lutxy(c, exin, yexpr="clamp_f_i8 y "+lum+" scalef < y "+lum+" scalef ? x "+thr+" scalef + > x y "+lum+" scalef < y "+lum+" scalef ? - 0 ? "+str+" * x +",u=2, v=2)
  39. thin    = thinning != 0 ? VersionNumber() < 2.6 ? mt_lutxy(c.mt_expand(U=2,V=2),diff,yexpr="x y 127 - "+str+" 1 + * +",u=2, v=2)
  40.             \           : mt_lutxy(c.mt_expand(U=2,V=2),diff,yexpr="clamp_f_i8 x y 127 scalef - "+str+" 1 + * +",u=2, v=2) : nop()
  41.  
  42. thinning == 0 ? thick : mt_merge(thin,thick,linemask,y=3,u=2,v=2)
  43.  
  44. return sislumaonly ? last : ssispmt ? CombinePlanes(last,input,planes="YUV",sample_clip=input) : ytouv(input.utoy8(),input.vtoy8(),last)
  45. }
  46.  
  47.  
  48. function Camembert_dhh(clip input, int "useMedianBlur") {
  49.  
  50. useMedianBlur = default(useMedianBlur, 0)
  51.  
  52. ssispmt   = Findstr(VersionString(), "AviSynth+") != 0 && Findstr(VersionString(), "r1576") == 0
  53. sislumaonly = ssispmt ? input.isy() : VersionNumber() < 2.6 ? true : input.isy8()
  54.  
  55. useMedianBlur = sislumaonly && !(VersionNumber() < 2.6) ? -1 : useMedianBlur
  56.  
  57. input
  58. useMedianBlur==0 ? SmoothUV(3,200,false) : useMedianBlur>0 ? MedianBlur(0,useMedianBlur,useMedianBlur) : last
  59. Overlay(last, Blur(1).Blur(1), mode="difference")
  60. grsc=sislumaonly ? nop() : GreyCenteredToMask_dhh().greyscale()
  61. sislumaonly ? GreyCenteredToMask_dhh() : ssispmt ? grsc.converttoy() : grsc.converttoy8()
  62. VersionNumber() < 2.6 ? greyscale() : last
  63. VersionNumber() < 2.6 ? Levels(0, 1, 115, 0, 255, false) : mt_lut("x 0 - 155 scalef 0 - / range_max 0 - * 0 +")
  64. Blur(1)
  65. VersionNumber() < 2.6 ? Levels(0, 0.4, 64, 0, 255, false) : mt_lut("x 0 - 64 scalef 0 - / 1 0.4 / ^ range_max 0 - * 0 +")
  66. Blur(1).Blur(1)
  67. VersionNumber() < 2.6 ? Levels(0, 1, 30, 0, 255, false).Blur(1) : mt_lut("x 0 - 30 scalef 0 - / range_max 0 - * 0 +").Blur(1)
  68. sislumaonly ? last : ssispmt ? CombinePlanes(last,grsc,planes="YUV",sample_clip=grsc) : YToUV(grsc.UToY8(),grsc.vToY8(),last)
  69. }
  70.  
  71. function Camembert_dhhMod(clip input, int "useMedianBlur")
  72. {
  73.  EM1=input.Camembert_dhh(useMedianBlur)
  74.  EM2=input.mt_edge(mode="min/max", thY1=1, thY2=255, thC1=10, thC2=10, chroma="process").mt_inflate().blur(1.5).blur(1.5).invert()
  75.  return mt_logic(EM1, EM2, "and")
  76. }
  77.  
  78. Function EMask_dhh(clip i, int "mode", float "trh", bool "analog", float "trh2", int "useMedianBlur")
  79. {
  80. analog        = Default(analog, false)
  81. trh2          = Default(trh2, 170)
  82. useMedianBlur = default(useMedianBlur, -1)
  83. mode3thr1     = mode == 3 && analog ? trh2/2.46 : nop()
  84. mode3lut      = mode == 3 && analog ? VersionNumber() < 2.6 ? "x "+String(mode3thr1)+" < 255 x "+String(trh2)+" > 0 255 x "+String(mode3thr1)+" - 255 "+String(trh2)+" "+String(mode3thr1)+" - / * - ? ?"
  85.                                       \                     : "x "+String(mode3thr1)+" scalef < range_max x "+String(trh2)+" scalef > 0 range_max x "+String(mode3thr1)+" scalef - range_max "+String(trh2)+" scalef "+String(mode3thr1)+" scalef - / * - ? ?" : nop()
  86. e_mask = mode == 1 ? mt_makediff(i.blur(1.58).blur(1.58),i).mt_binarize(threshold=trh).mt_inflate().mt_inflate() :
  87.      \   mode == 2 ? mt_logic(i.mt_binarize(105,upper=true).mt_expand(),i.mt_binarize(110,upper=false).mt_expand(),"and").mt_deflate() :
  88.      \   mode == 3 ? analog ? mt_merge(i.mt_edge(mode="min/max", thY1=255, thY2=255), i.Camembert_dhh(useMedianBlur), i.FastLineDarkenMOD3_dhh(trh).blur(0.5).FastLineDarkenMOD3_dhh(250,1,250,-2).mt_lut(mode3lut,u=1,v=1)) :
  89.                             \ mt_merge(i.mt_edge(mode="min/max", thY1=255, thY2=255), i.Camembert_dhh(useMedianBlur), i.FastLineDarkenMOD3_dhh(trh).blur(0.5).FastLineDarkenMOD3_dhh(250,1,250,-2).mt_binarize(70, mode="0 255")) : NOP()
  90. return e_mask
  91. }
  92.  
  93. Function DR_Radius_dhh(clip i, int dr_rad, int count)
  94. {
  95. return count > dr_rad ? i : DR_Radius_dhh(i.mt_expand(), dr_rad, count+1)
  96. }
  97.  
  98. Function DeHaloHmod(clip input, int "Radius", int "Str", bool "Maska", bool "strong", int "mode", float "thr", val "exdehalo", bool "analog", bool "dirty", bool "smooth", bool "anime", bool "pel2", bool "usedeen", bool "maskpel2", float "thr2", val "extmask")
  99. {
  100.  usedeen    = Default(usedeen, false)
  101.  pel2       = Default(pel2, false)
  102.  exdhcb     = defined(exdehalo) ? isclip(exdehalo) ? true : false : false
  103.  pel2       = exdhcb ? false : pel2
  104.  maskpel2   = Default(maskpel2, false)
  105.  exhmcb     = defined(extmask) ? isclip(extmask) ? true : false : false
  106.  maskpel2   = exhmcb ? false : pel2
  107.  smooth     = Default(smooth, false)
  108.  strong     = Default(strong, true)
  109.  Maska      = Default(Maska, false)
  110.  analog     = Default(analog, false)
  111.  dirty      = Default(dirty, false)
  112.  DR_Radius  = Default(Radius, dirty ? 6 : smooth || maskpel2 ? 4 : 2)
  113.  DR_Str     = Default(Str, usedeen ? pel2 ? 40 : 15 : pel2 ? 7 : smooth ? 5 : 3)
  114.  mode       = Default(mode, usedeen ? 0 : 3)
  115.  threshold  = Default(thr, smooth ? mode==1 ? 1 : 2250 : 140)
  116.  anime      = Default(anime, mode != 3 ? false : true)
  117.  thr2       = Default(thr2, smooth ? 170 : threshold/1.5)
  118.  
  119.  oclip = pel2 ? input.nnedi3_rpow2(rfactor=2) : input
  120.  hfl = defined(exdehalo) ? exdhcb ? exdehalo : eval("oclip." + exdehalo) : smooth ? oclip.DeHalo_alpha_mt(rx=pel2 ? 3 : 2,ry=pel2 ? 3 : 2,darkstr=0.2,brightstr=0.8).yahr2(dirty ? pel2 ? 12 : 8 : pel2 ? 24 : 16).TBilateral(DR_Str,5,pel2 ? 1.4 : 0.9,0.9,pel2 ? 7 : 5,5,0.7,chroma=false) : usedeen ? oclip.Deen("a2d", 2, DR_Str  , 0, 0, 0, 0, 0) : oclip.TBilateral(DR_Str,5,pel2 ? 1.4 : 0.9,0.9,pel2 ? 7 : 5,5,0.7,chroma=false)
  121.  hfl = pel2 && !(maskpel2 && dirty) ? hfl.PointResize(input.Width(),input.Height()) : hfl
  122.  
  123.  cm  = dirty ? hfl : input
  124.  
  125.  cm  = !(dirty && pel2) && maskpel2 ? cm.nnedi3_rpow2(rfactor=2) : cm
  126.  
  127.  extmask = defined(extmask) ? exhmcb ? extmask : Eval("cm." + extmask) : undefined()
  128.  
  129.  EM1 = defined(extmask) ? extmask : mode == 0 ? (strong ? Camembert_dhh(analog ? cm.ColorYUV(autogain=true) : cm, 3) : Camembert_dhhMod(analog ? cm.ColorYUV(autogain=true) : cm, 3)) : EMask_dhh(analog ? cm.ColorYUV(autogain=true) : cm, mode, threshold, analog, thr2, 3)
  130.  
  131.  EM1 = maskpel2 ? EM1.PointResize(input.Width(),input.Height()) : EM1
  132.  
  133.  hfl = pel2 && (maskpel2 && dirty) ? hfl.PointResize(input.Width(),input.Height()) : hfl
  134.  
  135.  RM2 = DR_Radius_dhh(EM1.mt_inflate(),DR_Radius,0).mt_inflate()
  136.  
  137.  DeRinging = anime ? mt_Merge(hfl, input, smooth ? EM1.mt_inflate(155,155) : EM1) : hfl
  138.  DeRinging = Maska ?  DeRinging.Invert() : DeRinging
  139.  
  140. return mt_Merge(input, DeRinging, RM2, u=2, v=2)
  141. }
  142.  
  143. Function DeHaloH(clip input, int "DR_Radius", int "DR_Str", bool "Maska", bool "strong", int "mode", int "threshold")
  144. {
  145.  strong     = Default(strong, true)
  146.  Maska      = Default(Maska, false)
  147.  DR_Radius  = Default(DR_Radius, 2)
  148.  DR_Str     = Default(DR_Str, 15)
  149.  mode       = Default(mode, 0)
  150.  threshold  = Default(threshold, 140)
  151.  
  152.  EM = mode == 0 ? (strong ? input.Camembert_dhh() : input.Camembert_dhhMod()) : EMask_dhh(input, mode, threshold)
  153.  
  154.  RM=DR_Radius_dhh(EM,DR_Radius,0)
  155.  DeRinging  = input.Deen("a2d", 2, DR_Str  , 0, 0, 0, 0, 0)
  156.  DeRinging = Maska ?  DeRinging.Invert() : DeRinging
  157. return mt_Merge(input, DeRinging, RM)
  158. }
RAW Paste Data