Advertisement
mawen1250

Contra-Sharpen mod 2.0

Jun 25th, 2012
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function CSmod(clip filtered, clip "original", clip "pclip", int "strength", float "divisor", float "index", float "ss_w", float "ss_h",
  2. \ bool "chroma", bool "preblur", bool "prec", bool "secure", bool "limit", bool "limit2", bool "Slimit", bool "SlimitY", bool "Tlimit",
  3. \ bool "ssrep", int "preR", int "Smethod", int "Smode", int "Szrp", int "Spwr", int "SdmpLo", int "SdmpHi", int "RGmode", int "RGmodeU",
  4. \ int "Repmode", int "RepmodeU", int "Sovershoot", int "Sundershoot", int "Tovershoot", int "Tundershoot", int "Soft", int "Soothe",
  5. \ string "filter_ss")
  6. {
  7.  
  8. chroma   = Default(chroma,  false  )
  9. ss_w     = Default(ss_w,    1.25   )
  10. ss_h     = Default(ss_h,    1.25   )
  11. ssrep    = Default(ssrep,   Defined(filter_ss))
  12. preblur  = Default(preblur, true   )
  13. prec     = Default(prec,    preblur)
  14. preR     = Default(preR,    ss_w*ss_h>2.25?2:1)
  15. limit    = Default(limit,   true   )
  16. limit2   = Default(limit2,  limit  )
  17. RGmode   = Default(RGmode,  11     )
  18. RGmodeU  = Default(RGmodeU, RGmode )
  19. Repmode  = Default(Repmode, 1      )
  20. RepmodeU = Default(RepmodeU,Repmode)
  21. limit    = Defined(original) || Defined(filter_ss) ? limit    : false
  22. limit2   = Defined(original) || Defined(filter_ss) ? limit2   : false
  23.  
  24. Smethod  = Default(Smethod, 3      )
  25. secure   = Default(secure,  true   )
  26. Smode    = Default(Smode,   3      )
  27. divisor  = Default(divisor, 1.5    )
  28. index    = Default(index,   0.8    )
  29. strength = Default(strength,100    )
  30. Szrp     = Default(Szrp,    16     )
  31. Spwr     = Default(Spwr,    4      )
  32. SdmpLo   = Default(SdmpLo,  4      )
  33. SdmpHi   = Default(SdmpHi,  48     )
  34.  
  35. Slimit      = Default(Slimit,      !limit      )
  36. Sovershoot  = Default(Sovershoot,  strength/100)
  37. Sundershoot = Default(Sundershoot, Sovershoot  )
  38. Tlimit      = Default(Tlimit,      false       )
  39. Tovershoot  = Default(Tovershoot,  strength/25 )
  40. Tundershoot = Default(Tundershoot, Tovershoot  )
  41. SlimitY     = Default(SlimitY,     !Tlimit     )
  42. Soothe      = Default(Soothe,      24          )
  43. Soft        = Default(Soft,        -2          )
  44.  
  45.  
  46. sw       = filtered.width()
  47. sh       = filtered.height()
  48. wws      = Round(sw*ss_w/8)*8
  49. hhs      = Round(sh*ss_h/8)*8
  50. HD       = (sw > 1024 || sh > 576) ? true : false
  51. ch21     = chroma ? 2 : 1
  52. ch31     = chroma ? 3 : 1
  53. ch32     = chroma ? 3 : 2
  54. strength = Smode==1 ? strength/50.0 : Smode==2 ? strength/20.0 : strength/100.0
  55. Soft     = Soft==-2  ? int( (1.0+(2.0/(ss_w+ss_h))) * sqrt(strength*100) )
  56. \        : Soft==-1  ? sqrt( (((ss_w+ss_h)/2.0-1.0)*100.0) ) * 10
  57. \        : Soft<=100 ? Soft : 100
  58.  
  59.  
  60. #super sampling with filtering
  61. original    = Defined(original) ? original : filtered
  62. filtered_or = filtered
  63. original_or = original
  64. filtered    = (ss_w > 1.0 || ss_h > 1.0) ? filtered.Spline64Resize(wws, hhs).Repair(filtered.GaussResize(wws, hhs, p=100), 1, chroma ? 1 : -1) : filtered
  65. original    = (ss_w > 1.0 || ss_h > 1.0) ? original.Spline64Resize(wws, hhs).Repair(original.GaussResize(wws, hhs, p=100), 1, chroma ? 1 : -1) : original
  66. filtered    = (ss_w > 1.0 || ss_h > 1.0)&&Defined(filter_ss) ? Eval("filtered."+filter_ss) : filtered
  67. filtered_dr = (ss_w > 1.0 || ss_h > 1.0)&&Defined(filter_ss) ? chroma ? filtered.Spline36Resize(sw, sh) : filtered.Spline36Resize(sw, sh).MergeChroma(filtered_or) : filtered_or
  68.  
  69. #pre filter before sharpening
  70. pre    = preblur ? Defined(pclip) ? pclip
  71. \                                 : filtered.minblur(preR, chroma ? (prec ? 3 : 2) : 1)
  72. \                : filtered
  73.  
  74.  
  75. #unsharp
  76. dark_limit   = pre.mt_inpand(U=ch31, V=ch31)
  77. bright_limit = pre.mt_expand(U=ch31, V=ch31)
  78. minmaxavg    = Smethod==1 ? NOP() : mt_average(dark_limit, bright_limit, U=ch31, V=ch31)
  79.  
  80. method = Smethod==1  ? pre      .removegrain(RGmode, chroma ? RGmodeU : -1)
  81. \      : Smethod==2  ? minmaxavg
  82. \      :               minmaxavg.removegrain(RGmode, chroma ? RGmodeU : -1)
  83.  
  84. method = secure      ? method.mt_lutxy(pre,"x y < x 1 + x y > x 1 - x ? ?", U=ch31, V=ch31) : method
  85.  
  86.  
  87. #making difference clip of sharpening
  88. sharpdiff = mt_makediff(pre, method, U=ch31,V=ch31)
  89.  
  90.  
  91. #sharpening diff generate mode
  92. sharpdiff = Smode==1 ? sharpdiff.mt_lut(expr="x 128 - "+string(strength)+" * 128 +", U=ch31, V=ch31)
  93. \         : Smode==2 ? sharpdiff.mt_lut("x 128 == x x 128 - "+String(divisor)+" / Abs "+String(index)+" ^ "+String(strength)+" * x 128 > 1 -1 ? * 128 + ?", U=ch31, V=ch31)
  94. \         : Smode==3 ? sharpdiff.mt_lut("x 128 == x x 128 - abs "+string(Szrp)+" / 1 "+string(Spwr)+" / ^ "+string(Szrp)+" * "+string(strength)+" * x 128 - x 128 - abs / * x 128 - 2 ^ "+string(Szrp)+" 2 ^ "+string(SdmpLo)+" + * x 128 - 2 ^ "+string(SdmpLo)+" + "+string(Szrp)+" 2 ^ * / * 1 "+string(SdmpHi)+" 0 == 0 "+string(Szrp)+" "+string(SdmpHi)+" / 4 ^ ? + 1 "+string(SdmpHi)+" 0 == 0 x 128 - abs "+string(SdmpHi)+" / 4 ^ ? + / * 128 + ?", U=ch31, V=ch31)
  95. \         : sharpdiff
  96.  
  97.  
  98. #spatial limit
  99. sclp   = (Slimit || Tlimit) ? mt_adddiff(filtered, sharpdiff, U=ch31, V=ch31) : NOP()
  100. sclp   = Slimit ? sclp.mt_clamp(bright_limit, dark_limit, Sovershoot, Sundershoot, Y=SlimitY?3:2, U=ch31, V=ch31) : sclp
  101.  
  102.  
  103. #temporal limit
  104. srcMVS = Tlimit ? pre.MSuper(hpad=0,vpad=0,pel=2,sharp=2,chroma=false) : NOP()
  105. f1v    = Tlimit ? MAnalyse(srcMVS, isb=false, delta=1, truemotion=false, blksize=HD?16:8, overlap=HD?8:4, pelsearch=2, search=2, searchparam=2, DCT=0, global=true, chroma=false) : NOP()
  106. b1v    = Tlimit ? MAnalyse(srcMVS, isb=true,  delta=1, truemotion=false, blksize=HD?16:8, overlap=HD?8:4, pelsearch=2, search=2, searchparam=2, DCT=0, global=true, chroma=false) : NOP()
  107. f1c    = Tlimit ? MCompensate(original, srcMVS, f1v, thSAD=300, thSCD1=300, thSCD2=100) : NOP()
  108. b1c    = Tlimit ? MCompensate(original, srcMVS, b1v, thSAD=300, thSCD1=300, thSCD2=100) : NOP()
  109. Tmax   = Tlimit ? original.mt_logic(f1c, "max", U=1, V=1).mt_logic(b1c, "max", U=1, V=1) : NOP()
  110. Tmin   = Tlimit ? original.mt_logic(f1c, "min", U=1, V=1).mt_logic(b1c, "min", U=1, V=1) : NOP()
  111. sclp   = Tlimit ? sclp.mt_clamp(Tmax, Tmin, Tovershoot, Tundershoot, U=ch21, V=ch21) : sclp
  112.  
  113.  
  114. #Soft
  115. sharpdiff  = (Slimit || Tlimit) ? mt_makediff(sclp, filtered, U=ch31, V=ch31) : sharpdiff
  116. sharpdiff  = Soft==100 ? sharpdiff : mt_lutxy(sharpdiff, sharpdiff.removegrain(19, chroma ? 19 : -1), "x 128 - abs y 128 - abs > y "+string(Soft)+" * x "+string(100-Soft)+" * + 100 / x ?", U=ch31, V=ch31)
  117.  
  118.  
  119. #Soothe
  120. sharpdiff2 = (Soothe>=0&&Soothe<=100) ? sharpdiff.temporalsoften(1,255,chroma ? 255 : 0,32,2) : NOP()
  121. sharpdiff  = (Soothe>=0&&Soothe<=100) ? mt_lutxy(sharpdiff, sharpdiff2, "x 128 - y 128 - * 0 < x 128 - 100 / "+string(Soothe)+" * 128 + x 128 - abs y 128 - abs > x "+string(Soothe)+" * y 100 "+string(Soothe)+" - * + 100 / x ? ?", U=ch31, V=ch31) : sharpdiff
  122.  
  123.  
  124. #the difference achieved by filtering
  125. allD       = limit  ? ssrep ? mt_makediff(original, filtered, U=ch31, V=ch31) : mt_makediff(original_or, filtered_dr, U=ch31, V=ch31) : NOP()
  126. #limiting sharpening to source clip
  127. sharpdiff  = !ssrep&&(ss_w > 1.0 || ss_h > 1.0) ? sharpdiff.Spline36Resize(sw, sh) : sharpdiff
  128. ssDD       = limit  ? sharpdiff.repair(allD, mode=Repmode, modeU=chroma ? RepmodeU : -1) : sharpdiff
  129. ssDD       = limit2 ? ssDD.mt_lutxy(sharpdiff, "x 128 - abs y 128 - abs < x y ?", U=ch31, V=ch31) : ssDD
  130. ssDD       = ssrep&&(ss_w > 1.0 || ss_h > 1.0) ? ssDD.Spline36Resize(sw, sh) : ssDD
  131.  
  132.  
  133. #add difference clip to clip "filtered" of original resolution
  134. sclp       = mt_adddiff(filtered_dr, ssDD, U=ch32, V=ch32)
  135.  
  136.  
  137. return sclp
  138. }
  139.  
  140.  
  141.  
  142. Function MinBlur(clip clp, int r, int "uv")
  143. {
  144. uv   = default(uv,3)
  145. uv2  = (uv==2) ? 1 : uv
  146. rg4  = (uv==3) ? 4 : -1
  147. rg11 = (uv==3) ? 11 : -1
  148. rg20 = (uv==3) ? 20 : -1
  149. medf = (uv==3) ? 1 : -200
  150.  
  151. RG11D = (r==1) ? mt_makediff(clp,clp.removegrain(11,rg11),U=uv2,V=uv2)
  152.  \    : (r==2) ? mt_makediff(clp,clp.removegrain(11,rg11).removegrain(20,rg20),U=uv2,V=uv2)
  153.  \    :          mt_makediff(clp,clp.removegrain(11,rg11).removegrain(20,rg20).removegrain(20,rg20),U=uv2,V=uv2)
  154. RG4D  = (r==1) ? mt_makediff(clp,clp.removegrain(4,rg4),U=uv2,V=uv2)
  155.  \    : (r==2) ? mt_makediff(clp,clp.medianblur(2,2*medf,2*medf),U=uv2,V=uv2)
  156.  \    :          mt_makediff(clp,clp.medianblur(3,3*medf,3*medf),U=uv2,V=uv2)
  157. DD    = mt_lutxy(RG11D,RG4D,"x 128 - y 128 - * 0 < 128 x 128 - abs y 128 - abs < x y ? ?",U=uv2,V=uv2)
  158. clp.mt_makediff(DD,U=uv,V=uv)
  159. return(last)
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement