Advertisement
mawen1250

Contra-Sharpen mod 2.4

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