Advertisement
mawen1250

Contra-Sharpen mod 2.2

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