Advertisement
mawen1250

Contra-Sharpen mod 2.5

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