Advertisement
mawen1250

Contra-Sharpen mod 2.6

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