SHARE
TWEET

Contra-Sharpen mod 3.4

mawen1250 Sep 21st, 2012 757 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###### Contra-Sharpen mod 3.4 ###### by mawen1250 ###### 2012.09.21 ######
  2. ###### Requirements: masktools v2.0a48, mvtools v2.5.11.3/v2.6.0.5, RemoveGrain + Repair v1.0pre, RemoveGrainHD v0.5    ######
  3. ###### FluxSmooth v1.1a, nnedi3 v0.9.4, SangNom 2004.01.18(filter_ss="AA"), TEdgeMask v0.9, tcanny v1.0, MSharpen v1.10 ######
  4.  
  5. Function CSmod(clip filtered, clip "source", clip "pclip", bool "chroma",
  6. \ int "edgemode", val "edgemask", float "edgethr", float "tcannysigma", bool "mergesrc",
  7. \ float "ss_w", float "ss_h", bool "ss_hq", string "ssmethod", string "filter_ss", bool "ssrep", bool "ssout", bool "ssoutc",
  8. \ int "preblur", bool "prec", int "preR", bool "usepasf",
  9. \ int "Smethod", val "kernel", bool "secure", string "filter_nr",
  10. \ val "Smode", float "strength", float "divisor", float "index", float "Szrp", float "Spwr", float "SdmpLo", float "SdmpHi",
  11. \ bool "Slimit", bool "Tlimit", int "Sovershoot", int "Sundershoot", int "Tovershoot", int "Tundershoot",
  12. \ float "Soft", int "Soothe",
  13. \ bool "limit", int "Repmode", int "RepmodeU", int "thr", int "thrc",
  14. \ bool "chromamv", int "blksize", int "overlap", int "thSAD", int "thSCD1", int "thSCD2",
  15. \ bool "truemotion", bool "MVglobal", int "pel", int "pelsearch", int "search", int "searchparam", int "MVsharp", int "DCT")
  16. {
  17.  
  18. defsrc   = Defined(source   )
  19. defpclp  = Defined(pclip    )
  20. deffss   = Defined(filter_ss)
  21. deffnr   = Defined(filter_nr)
  22. AA       = deffss ? filter_ss=="AA" : false
  23. sw       = filtered.width()
  24. sh       = filtered.height()
  25. HD       = (sw > 1024 || sh > 576) ? true : false
  26.  
  27. chroma   = Default(chroma,  false  )
  28.  
  29. edgemode    = Default(edgemode,    AA ? 1 : 0  )
  30. # 0 = Sharpening all, 1 = Sharpening only edge, 2 = Sharpening only non-edge.
  31. # By default, edgemode=2 is tuned for enhancing noise/small detail.
  32. # It will automatically set [edgemask=-2, ss_w=1, ss_h=1, preblur=0, kernel=5, Slimit=false, Tlimit=true(when limit=false)].
  33. edgemask    = Default(edgemask,    edgemode==2?-2:AA?7:2)
  34. # -1: Similar to 1 and tweaking for edgemode=2,   -2: Similar to 2 and tweaking for edgemode=2,
  35. #  1: Same as edgemaskHQ=false in LSFmod(min/max), 2: Same as edgemaskHQ=true in LSFmod,
  36. #  3: Same as sharpening mask in MCTD(prewitt),    4: Same as mtype=1 in TAA(sobel),
  37. #  5: Same as mtype=2 in TAA(roberts),             6: Same as mtype=3 in TAA(prewitt),
  38. #  7: Same as mtype=4 in TAA(TEdgeMask),           8: Same as mtype=5 in TAA(tcanny),
  39. #  9: Same as mtype=6 in TAA(MSharpen),           10: (or any other numbers) My own method of tcanny usage.
  40. #     Otherwise define a custom edge mask clip, only luma is taken to merge all planes.
  41. edgethr     = Default(edgethr,     32.0        )   #Tweak edge mask thr in edgemask mode -2/-1/1/2/6/7/8/9/10.
  42. tcannysigma = Default(tcannysigma, 1.2         )   #Tweak tcanny's sigma in edgemask mode 8/10.
  43. mergesrc    = Default(mergesrc,    false       )   #Whether to merge clip "source" instead of clip "filtered" at the end of processing.
  44.  
  45. ss_w     = Default(ss_w,    edgemode>=2?1.00:1.50)   #Super sampling multiplier of width.
  46. ss_h     = Default(ss_h,    edgemode>=2?1.00:1.50)   #Super sampling multiplier of height.
  47. ss_w     = max    (ss_w,    1.00   )
  48. ss_h     = max    (ss_h,    1.00   )
  49. ss_hq    = Default(ss_hq,   ss_w*ss_h>=3.0625)   #True using nnedi3_rpow2 in super sampling, false using non-ringing Spline64Resize.
  50. ssout    = Default(ssout,   false  )   #Output in super sampling resolution.
  51. ssoutc   = Default(ssoutc,  true   )   #Whether to output correct chroma when (chroma==false && ss_hq==false && ssout==true).
  52. ssrep    = Default(ssrep,   false  )   #When limiting sharpening to source clip, whether to Repair in super sampling resolution.
  53. ssout    = (ss_w > 1.0 || ss_h > 1.0) ? ssout : false
  54. ssrep    = (ss_w > 1.0 || ss_h > 1.0) ? ssrep : false
  55. ssoutc   = (!chroma && ssout) ? ssoutc : false
  56. ssrep    = ssout ? true : ssrep
  57. filter_ss= deffss ? AA ? "SangNom(aa=48).TurnRight().SangNom(aa=48).TurnLeft()" : filter_ss : NOP()
  58. # When defining filter_ss="AA", it will automatically use SangNom for anti-aliasing in super sampling clip, and it will automatically set [Repmode=13, edgemode=1, edgemask=7].
  59.  
  60. preblur  = Default(preblur, AA?0:edgemode>=2?0:ss_w*ss_h>=9?-3:ss_w*ss_h>=4?-2:-1)
  61. # Pre-filtering, 0 = disable, -1 = Gaussian Blur radius=1(RG11), -2 = Gaussian Blur radius=2(RG11+RG20),
  62. # -3 = Gaussian Blur radius=3(RG11+RG20+RG20), -4 = Median Blur radius=1(RG4),
  63. # 1 = MinBlur, 2 = MinBlur(Uses SBR by default) mixed with MinBlur+FluxSmoothT,
  64. # 3 = MinBlur(Uses SBR by default)+FluxSmoothT.
  65. # "preblur" is ignored when pclip is defined.
  66. prec     = Default(prec,    true   )   #Whether to process chroma plane in preblur.
  67. preR     = Default(preR,    preblur>=2?0:ss_w*ss_h>6.25?3:ss_w*ss_h>2.25?2:1)
  68. # MinBlur setting, 1-3 sets radius of MinBlur(Gaussian|Median), 0 uses SBR instead of normal Gaussian Blur in MinBlur.
  69. usepasf  = Default(usepasf, false  )
  70. # Whether to use pre-filtered clip as input(filtered) clip, which will reduce noise/halo/ring from input clip.
  71. usepasf  = (preblur==0 && !defpclp) ? false : usepasf
  72.  
  73. Smethod  = Default(Smethod, ss_w*ss_h>1&&!AA?3:1)   #Sharpen Method - 1: 3x3 kernel, 2: Min/Max, 3: Min/Max + 3x3 kernel.
  74. kernel   = Default(kernel,  edgemode>=2 ? 5 : Smethod>=3 ? ss_w*ss_h>=9?4:ss_w*ss_h>=4?3:ss_w*ss_h>=2.25?2:1
  75. \                                                        : ss_w*ss_h>=4?4:ss_w*ss_h>=2.25?3:ss_w*ss_h> 1?2:1)
  76. # 1: Gaussian Blur radius=1(RG11), 2: Average Blur(RG20), 3: Gaussian Blur radius=2(RG11+RG20),
  77. # 4: Gaussian Blur radius=3(RG11+RG20+RG20), 5: for noise/detail enhance(RG19+RG4), default for edgemode==2,
  78. # Otherwise define a custom kernel in string such as kernel="RemoveGrain(20, 11)".
  79. secure   = Default(secure,  true   )   #Mode to avoid banding & oil painting (or face wax) effect of sharpening.(from LSFmod)
  80. Smode    = Default(Smode,   3      )
  81. # Sharpen Mode - 0: None, 1: Linear, 2: Non-linear 1, 3:Non-linear 2(from LSFmod Smode=5), Otherwise define a custom Smode in string.
  82. divisor  = Default(divisor, 1.5    )
  83. index    = Default(index,   0.8    )
  84. #strength = Default(strength,120.0  )
  85. Szrp     = Default(Szrp,    16     )
  86. Spwr     = Default(Spwr,    4      )
  87. SdmpLo   = Default(SdmpLo,  4      )
  88. SdmpHi   = Default(SdmpHi,  48     )
  89. miSpwr   = 1.0/Spwr
  90.  
  91. limit    = Default(limit,   true   )
  92. # Whether to limit sharpening to source clip, only takes effect when (Defined(source) || Defined(filter_ss) || usepasf) == true.
  93. thr      = Default(thr,     0      )
  94. thrc     = Default(thrc,    thr    )
  95. # Allow sharpening more than the source pixel in [thr/thrc](luma/chroma).
  96. Repmode  = Default(Repmode, AA?13:1)
  97. RepmodeU = Default(RepmodeU,Repmode)
  98. thrc     = chroma ? thrc : 0
  99. limit    = defsrc || deffss || usepasf || deffnr ? limit : false
  100.  
  101. Slimit      = Default(Slimit,      edgemode>=2?false:!limit)
  102. # Spatial limit with overshoot and undershoot. Disabled when (limit==true && thr==0 && thrc==0).
  103. Tlimit      = Default(Tlimit,      edgemode>=2&&!limit?true:false)
  104. # Use MC Temporal limit at the end of sharpening(only process luma).(from MCTD)
  105. strength    = Default(strength,    Tlimit?160:120)
  106. Sovershoot  = Default(Sovershoot,  Round(strength/160))
  107. Sundershoot = Default(Sundershoot, Sovershoot  )
  108. Tovershoot  = Default(Tovershoot,  edgemode>=2?Round(strength/80):Round(strength/40))
  109. Tundershoot = Default(Tundershoot, Tovershoot  )
  110. Soft        = Default(Soft,        limit?  0:-2)
  111. # Soft the sharpening effect (-1 = old autocalculate, -2 = new autocalculate, 0 = disable, (0, 100] = enable).
  112. # Disabled when (limit==true && thr==0 && thrc==0).(from LSFmod)
  113. Soothe      = Default(Soothe,      limit? -1:24)
  114. # Soothe temporal stabilization, 0-100 sets minimum percent of the original sharpening to keep, -1 disables Soothe. Disabled when (chroma==true && Tlimit=true).
  115. Sovershoot  = max(Sovershoot,  0)
  116. Sundershoot = max(Sundershoot, 0)
  117. Tovershoot  = max(Tovershoot,  0)
  118. Tundershoot = max(Tundershoot, 0)
  119.  
  120. chromamv    = Default(chromamv,    chroma      )
  121. blksize     = Default(blksize,     HD ? 16 : 8 )
  122. overlap     = Default(overlap,     blksize/2   )
  123. thSAD       = Default(thSAD,       300         )
  124. thSCD1      = Default(thSCD1,      300         )
  125. thSCD2      = Default(thSCD2,      100         )
  126. truemotion  = Default(truemotion,  false       )
  127. MVglobal    = Default(MVglobal,    true        )
  128. pel         = Default(pel,         2           )
  129. pelsearch   = Default(pelsearch,   2           )
  130. search      = Default(search,      2           )
  131. searchparam = Default(searchparam, 2           )
  132. MVsharp     = Default(MVsharp,     2           )
  133. DCT         = Default(DCT,         0           )
  134.  
  135.  
  136. wss      = Round(sw*ss_w/8)*8
  137. hss      = Round(sh*ss_h/8)*8
  138. rfactor  = max(Round(sqrt(ss_w*ss_h)/2)*2, 2)
  139. ssmethod = Defined(ssmethod) ? ssmethod
  140. \        : ss_hq ? """nnedi3_rpow2(rfactor=rfactor, qual=2, cshift="Spline64Resize", fwidth=wss, fheight=hss)"""
  141. \                : "CSmod_nrSpline64Resize(wss, hss, chroma=chroma||ssoutc, nr=true)"
  142. # You can define your own super sampling method with ssmethod.
  143.  
  144. ch21     = chroma ? 2 : 1
  145. ch31     = chroma ? 3 : 1
  146. ch41     = chroma ? 4 : 1
  147. ch32     = chroma ? 3 : 2
  148. ch34     = chroma ? 3 : 4
  149. Slimit   = limit && thr==0 && thrc==0 ? false : Slimit
  150. Soft     = limit && thr==0 && thrc==0 ? 0
  151. \        : Soft <= -2  ? (1.0+(2.0/(ss_w+ss_h))) * sqrt(strength)
  152. \        : Soft == -1  ? sqrt( (((ss_w+ss_h)/2.0-1.0)*100.0) ) * 10
  153. \        : Soft <= 100 ? Soft : 100
  154. Soothe   = Tlimit ? -1 : Soothe
  155.  
  156.  
  157. # super sampling, filtering in ss clip
  158. source      = defsrc ? source : filtered
  159. filtered_os = filtered
  160. filtered_ss = (ss_w > 1.0 || ss_h > 1.0) ? Eval("filtered."+ssmethod) : filtered
  161. source_ss   = (ss_w > 1.0 || ss_h > 1.0) ? defsrc ? Eval("source."+ssmethod) : filtered_ss : source
  162. filtered    = deffss ? Eval("filtered_ss."+filter_ss) : filtered_ss
  163. filtered_ds = (ss_w > 1.0 || ss_h > 1.0) ? deffss ? filtered.CSmod_nrSpline64Resize(sw, sh, chroma=chroma, nr=0) : filtered_os : filtered
  164.  
  165. Assert( (isFrameBased(filtered_os)),                            """CSmod: input clip "filtered" must be Frame Based!""" )
  166. Assert( (isFrameBased(source)),                                   """CSmod: input clip "source" must be Frame Based!""" )
  167. Assert( (source.width() == sw && source.height() == sh), """CSmod: resolution of "filtered" and "source" must match!""" )
  168.  
  169.  
  170. # pre-filtering before sharpening
  171. spatial     = filtered.MinBlur(preR, chroma ? prec ? 3 : 2 : 1)
  172. temporal    = spatial .FluxsmoothT(7).Repair(spatial, 1, prec ? 1 : -1)
  173. temporal    = chroma&&!prec ? temporal.MergeChroma(filtered) : temporal
  174. mixed       = temporal.merge(spatial,0.251)
  175. pre         = defpclp     ? pclip
  176. \           : preblur<=-4 ? filtered.RemoveGrain(4 , chroma?prec?4 :0:-1)
  177. \           : preblur==-3 ? filtered.RemoveGrain(11, chroma?prec?11:0:-1).RemoveGrain(20, chroma?prec?20:0:-1).RemoveGrain(20, chroma?prec?20:0:-1)
  178. \           : preblur==-2 ? filtered.RemoveGrain(11, chroma?prec?11:0:-1).RemoveGrain(20, chroma?prec?20:0:-1)
  179. \           : preblur==-1 ? filtered.RemoveGrain(11, chroma?prec?11:0:-1)
  180. \           : preblur== 1 ? spatial
  181. \           : preblur== 2 ? mixed
  182. \           : preblur>= 3 ? temporal
  183. \           : filtered
  184. # You can define your own pre-filter clip with pclip.
  185. pre_ds      = (pre.width() == sw && pre.height() == sh) ? pre : pre.CSmod_nrSpline64Resize(sw, sh, chroma=chroma, nr=0)
  186. pre         = (ss_w > 1.0 || ss_h > 1.0) ? (pre.width() == wss && pre.height() == hss) ? pre : Eval("pre."+ssmethod) : pre_ds
  187.  
  188. # whether to use pre-filtered clip as main clip
  189. filtered    = usepasf ? pre    : filtered
  190. filtered_ds = usepasf ? pre_ds : filtered_ds
  191.  
  192.  
  193. # generate edge mask
  194. prefinal  = ssout ? Tlimit&&!chroma&&chromamv ? pre.MergeChroma(filtered_ss) : pre : Tlimit&&!chroma&&chromamv ? pre_ds.MergeChroma(filtered_os) : pre_ds
  195. srcfinal  = ssout ? source_ss                                                      : source
  196. edgemask  = IsClip(edgemask) ? edgemask
  197. \         : IsInt (edgemask) ?
  198. \           edgemask <= -2   ? mt_logic(prefinal.mt_edge(thY1=0, thY2=255, "8 16 8 0 0 0 -8 -16 -8 4", U=1, V=1),
  199. \                                       prefinal.mt_edge(thY1=0, thY2=255, "8 0 -8 16 0 -16 8 0 -8 4", U=1, V=1),
  200. \                                       "max", U=1, V=1).mt_lut("x " + string(edgethr*4) + " / 0.86 ^ 255 *", U=1, V=1)
  201. \                             .Removegrain(11, -1)
  202. \         : edgemask == -1   ? prefinal.mt_edge(thY1=0, thY2=255, mode="min/max", U=1, V=1)
  203. \                             .mt_lut("x " + string(edgethr) + " / 0.86 ^ 255 *", U=1, V=1)
  204. \                             .Removegrain(11, -1)
  205. \         : edgemask == 1    ? prefinal.mt_edge(thY1=0, thY2=255, mode="min/max", U=1, V=1)
  206. \                             .mt_lut("x " + string(edgethr) + " / 0.86 ^ 255 *", U=1, V=1)
  207. \                             .mt_inflate(U=1, V=1).mt_inflate(U=1, V=1).Removegrain(11, -1)
  208. \         : edgemask == 2    ? mt_logic(prefinal.mt_edge(thY1=0, thY2=255, "8 16 8 0 0 0 -8 -16 -8 4", U=1, V=1),
  209. \                                       prefinal.mt_edge(thY1=0, thY2=255, "8 0 -8 16 0 -16 8 0 -8 4", U=1, V=1),
  210. \                                       "max", U=1, V=1).mt_lut("x " + string(edgethr*4) + " / 0.86 ^ 255 *", U=1, V=1)
  211. \                             .mt_inflate(U=1, V=1).mt_inflate(U=1, V=1).RemoveGrain(11, -1)
  212. \         : edgemask == 3    ? mt_edge(prefinal, "prewitt", 0, 255, 0, 0, V=1, U=1)
  213. \                             .mt_lut("x 1.8 ^", U=1, V=1).RemoveGrain(4, -1).mt_inflate(U=1, V=1).RemoveGrain(20,-1)
  214. \         : edgemask == 4    ? mt_edge(prefinal, "sobel", 7, 7, 5, 5, U=1, V=1).mt_inflate(U=1, V=1)
  215. \         : edgemask == 5    ? mt_edge(prefinal, "roberts", 0, 4, 0, 4, U=1, V=1).mt_inflate(U=1, V=1)
  216. \         : edgemask == 6    ? mt_edge(prefinal, "prewitt", 0, 255, 0, 0, U=1, V=1)
  217. \                             .mt_lut("x " + string(edgethr) + " <= x 1 >> x 1.4 ^ ?", U=1, V=1)
  218. \                             .RemoveGrain(4, -1).mt_inflate(U=1, V=1)
  219. \         : edgemask == 7    ? TEdgeMask(prefinal, link=2, preblur=false, valon=-1, U=0, V=0)
  220. \                             .mt_lut("x " + string(edgethr/5) + " <= x 1 >> x 4 << ?", U=1, V=1)
  221. \                             .mt_deflate(U=1, V=1).RemoveGrain(HD ? 20 : 11, -1)
  222. \         : edgemask == 8    ? tcanny(srcfinal, sigma=tcannysigma, mode=1, plane=1)
  223. \                             .mt_lut("x " + string(edgethr) + " <= x 1 >> x 1 << ?", U=1, V=1)
  224. \                             .RemoveGrain(HD ? 20 : 11, -1).mt_inflate(U=1, V=1)
  225. \         : edgemask == 9    ? MSharpen(prefinal, threshold=Round(edgethr/5), strength=0, mask=true, highq=false)
  226. \         :                    tcanny(srcfinal, sigma=tcannysigma, mode=1, plane=1)
  227. \                             .mt_lut("x " + string(edgethr) + " <= 0 x " + string(edgethr) + " - 6 << ?", U=1, V=1)
  228. \                             .RemoveGrain(HD ? 20 : 11, -1).mt_inflate(U=1, V=1)
  229. \         :                    Assert(false, """CSmod: "edgemask" should be int or clip!""")
  230.  
  231.  
  232. # unsharp
  233. dark_limit   = pre.mt_inpand(U=ch31, V=ch31)
  234. bright_limit = pre.mt_expand(U=ch31, V=ch31)
  235. minmaxavg    = mt_average(dark_limit, bright_limit, U=ch31, V=ch31)
  236.  
  237. method     = Smethod <= 1     ? pre : minmaxavg
  238.  
  239. method     = Smethod == 2     ? method
  240. \          : IsString(kernel) ? Eval("method."+kernel)
  241. \          : IsInt   (kernel) ?
  242. \            kernel <= 1      ? method.RemoveGrain(11, chroma?11:-1)
  243. \          : kernel == 2      ? method.RemoveGrain(20, chroma?20:-1)
  244. \          : kernel == 3      ? method.RemoveGrain(11, chroma?11:-1).RemoveGrain(20, chroma?20:-1)
  245. \          : kernel == 4      ? method.RemoveGrain(11, chroma?11:-1).RemoveGrain(20, chroma?20:-1).RemoveGrain(20, chroma?20:-1)
  246. \          :                    method.RemoveGrain(19, chroma?19:-1).RemoveGrain(4 , chroma?4 :-1)
  247. \          :                    Assert(false, """CSmod: "kernel" should be int or string!""")
  248.  
  249. method     = secure           ? method.mt_lutxy(pre, "x y < x 1 + x y > x 1 - x ? ?", U=ch31, V=ch31) : method
  250.  
  251.  
  252. # making difference clip for sharpening
  253. sharpdiff  = mt_makediff(pre, method, U=ch31,V=ch31)
  254.  
  255.  
  256. # filtering in nr clip
  257. filtered   = deffnr ? Eval("method." + filter_nr).mt_adddiff(sharpdiff, U=ch31, V=ch31) : filtered
  258. filtered_ds= (ss_w > 1.0 || ss_h > 1.0) ? deffnr      ? filtered.CSmod_nrSpline64Resize(sw, sh, chroma=chroma, nr=0)
  259. \                                       : filtered_ds : filtered
  260.  
  261.  
  262. # sharpening diff generate mode
  263. sharpdiff  = IsString(Smode)  ? sharpdiff.mt_lut(Smode, U=ch31, V=ch31)
  264. \          : IsInt   (Smode)  ?
  265. \            Smode <= 0       ? sharpdiff
  266. \          : Smode == 1       ? sharpdiff.mt_lut("x 128 == x x 128 - "+string(strength/50.0)+" * 128 + ?", U=ch31, V=ch31)
  267. \          : Smode == 2       ? sharpdiff.mt_lut("x 128 == x x 128 - "+String(divisor)+" / Abs "+String(index)+" ^ "+String(strength/20.0)+" * x 128 > 1 -1 ? * 128 + ?", U=ch31, V=ch31)
  268. \          :                    sharpdiff.mt_lut("x 128 == x x 128 - abs "+string(Szrp)+" / "+string(miSpwr)+" ^ "+string(Szrp)+" * "+string(strength/100.0)+" * x 128 > 1 -1 ? * 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)
  269. \          :                    Assert(false,  """CSmod: "Smode" should be int or string!""")
  270.  
  271. # x==128 ? x : (abs(x-128)/Szrp)^(miSpwr)*Szrp*(strength/100.0)*(x>128 ? 1 : -1) * ((x-128)^2*(Szrp^2+SdmpLo) / ((x-128)^2+SdmpLo)*Szrp^2) * ((1+ SdmpHi==0 ? 0 : (Szrp/SdmpHi)^4) / (1+ SdmpHi==0 ? 0 : (abs(x-128)/SdmpHi)^4)) + 128
  272.  
  273.  
  274. # spatial limit
  275. DlimitF    = usepasf || (preblur==0 && !defpclp) ?   dark_limit : filtered.mt_inpand(U=ch31, V=ch31)
  276. BlimitF    = usepasf || (preblur==0 && !defpclp) ? bright_limit : filtered.mt_expand(U=ch31, V=ch31)
  277. sclp       = Slimit ? mt_adddiff(filtered, sharpdiff, U=ch31, V=ch31) : NOP()
  278. sclp       = Slimit ? sclp.mt_clamp(BlimitF, DlimitF, Sovershoot, Sundershoot, U=ch31, V=ch31) : NOP()
  279.  
  280.  
  281. # Soft
  282. sharpdiff  = Slimit ? mt_makediff(sclp, filtered, U=ch31, V=ch31) : sharpdiff
  283. sharpdiff  = Soft == 0 ? 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)
  284.  
  285.  
  286. # Soothe
  287. sharpdiff2 = (Soothe >= 0 && Soothe <= 100) ? sharpdiff.temporalsoften(1,255,chroma ? 255 : 0,32,2) : NOP()
  288. 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
  289.  
  290.  
  291. # the difference achieved by filtering
  292. allD       = limit  ? ssrep && (ss_w > 1.0 || ss_h > 1.0) ? mt_makediff(source_ss, filtered, U=ch31, V=ch31)
  293. \                                                         : mt_makediff(source, filtered_ds, U=ch31, V=ch31) : NOP()
  294.  
  295. # limiting sharpening to source clip
  296. sharpdiff  = !ssrep && (ss_w > 1.0 || ss_h > 1.0) ? sharpdiff.CSmod_nrSpline64Resize(sw, sh, chroma=chroma, nr=0) : sharpdiff
  297. ssDD       = limit  ? sharpdiff.Repair(allD, mode=Repmode, modeU=chroma ? RepmodeU : -1) : sharpdiff
  298. ssDD       = limit && (thr >  0 || thrc >  0) ? sharpdiff.mt_lutxy(ssDD,
  299. \                                               yexpr="x 128 - abs y 128 - abs " + string(thr ) + " + <= x y 128 < y " + string(thr ) + " - y " + string(thr ) + " + ? ?",
  300. \                                               uexpr="x 128 - abs y 128 - abs " + string(thrc) + " + <= x y 128 < y " + string(thrc) + " - y " + string(thrc) + " + ? ?",
  301. \                                               vexpr="x 128 - abs y 128 - abs " + string(thrc) + " + <= x y 128 < y " + string(thrc) + " - y " + string(thrc) + " + ? ?",
  302. \                                               U=ch31, V=ch31) : ssDD
  303. ssDD      = !limit && (thr >  0 || thrc >  0) ? sharpdiff.mt_lut(
  304. \                                               yexpr="x 128 - abs " + string(thr ) + " <= x x 128 < " + string(128-thr ) + " " + string(128+thr ) + " ? ?",
  305. \                                               uexpr="x 128 - abs " + string(thrc) + " <= x x 128 < " + string(128-thrc) + " " + string(128+thrc) + " ? ?",
  306. \                                               vexpr="x 128 - abs " + string(thrc) + " <= x x 128 < " + string(128-thrc) + " " + string(128+thrc) + " ? ?",
  307. \                                               U=ch31, V=ch31) : ssDD
  308. ssDD       = limit && (thr == 0 && thrc == 0) ? ssDD.mt_lutxy(sharpdiff, "x 128 - abs y 128 - abs <= x y ?", U=ch31, V=ch31) : ssDD
  309. ssDD       = ssrep && (ss_w > 1.0 || ss_h > 1.0) && !ssout ? ssDD.CSmod_nrSpline64Resize(sw, sh, chroma=chroma, nr=0) : ssDD
  310.  
  311. # add difference clip to clip "filtered" of ss/original resolution
  312. sclp       = ssout ? mt_adddiff(filtered, ssDD, U=ch31, V=ch31) : mt_adddiff(filtered_ds, ssDD, U=ch31, V=ch31)
  313.  
  314.  
  315. # temporal limit
  316.  
  317. sMVS       = prefinal.MSuper(hpad=0, vpad=0, pel=pel, levels=0, sharp=MVsharp, chroma=chromamv)
  318. rMVS       = usepasf || (preblur==0 && !defpclp) ? sMVS
  319. \                                                : srcfinal.MSuper(hpad=0, vpad=0, pel=pel, levels=1, sharp=MVsharp, chroma=chroma)
  320. f1v        = MAnalyse(sMVS, isb=false, delta=1, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=chromamv)
  321. b1v        = MAnalyse(sMVS, isb=true,  delta=1, truemotion=truemotion, blksize=blksize, overlap=overlap, pelsearch=pelsearch, search=search, searchparam=searchparam, DCT=DCT, global=MVglobal, chroma=chromamv)
  322. f1c        = MCompensate(srcfinal, rMVS, f1v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  323. b1c        = MCompensate(srcfinal, rMVS, b1v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  324. Tmax       = srcfinal.mt_logic(f1c, "max", U=ch31, V=ch31).mt_logic(b1c, "max", U=ch31, V=ch31)
  325. Tmin       = srcfinal.mt_logic(f1c, "min", U=ch31, V=ch31).mt_logic(b1c, "min", U=ch31, V=ch31)
  326. sclp       = Tlimit ? sclp.mt_clamp(Tmax, Tmin, Tovershoot, Tundershoot, U=ch32, V=ch32) : sclp
  327.  
  328.  
  329. # merge with edge mask and output correct chroma
  330. merged_ss  = mergesrc ? source_ss : filtered_ss
  331. merged_os  = mergesrc ? source    : filtered_os
  332.  
  333. end        = edgemode <= 0 ? chroma ? sclp
  334. \                                   : ssout ? ssoutc ? sclp.MergeChroma(merged_ss)
  335. \                                                    : sclp
  336. \                                           : sclp.MergeChroma(merged_os)
  337. \          : edgemode == 1 ? ssout ? mt_merge(merged_ss, sclp, edgemask, luma=chroma, Y=3, U=ssoutc?ch32:ch31, V=ssoutc?ch32:ch31)
  338. \                                  : mt_merge(merged_os, sclp, edgemask, luma=chroma, Y=3, U=ch32,             V=ch32            )
  339. \                          : ssout ? mt_merge(sclp, merged_ss, edgemask, luma=chroma, Y=3, U=ssoutc?ch34:ch31, V=ssoutc?ch34:ch31)
  340. \                                  : mt_merge(sclp, merged_os, edgemask, luma=chroma, Y=3, U=ch34,             V=ch34            )
  341.  
  342. return end
  343. }
  344.  
  345.  
  346. Function MinBlur(clip clp, int "r", int "uv"){
  347.  
  348. r     = Default(r,  1)
  349. uv    = Default(uv, 3)
  350.  
  351. uv2   = (uv==2) ? 1  : uv
  352. rg4   = (uv==3) ? 4  : -1
  353. rg11  = (uv==3) ? 11 : -1
  354. rg20  = (uv==3) ? 20 : -1
  355. medf  = (uv==3) ? 1  : -200
  356. uvm2  = (r==2)  ? (uv==3?3:uv==2?0:-1) : nop()
  357. uvm3  = (r==3)  ? (uv==3?3:uv==2?0:-1) : nop()
  358.  
  359. RG11D = (r<=0) ? mt_makediff(clp, clp.sbr(uv=uv2), U=uv2, V=uv2)
  360. \     : (r==1) ? mt_makediff(clp, clp.RemoveGrain(11, rg11), U=uv2, V=uv2)
  361. \     : (r==2) ? mt_makediff(clp, clp.RemoveGrain(11, rg11).RemoveGrain(20, rg20), U=uv2, V=uv2)
  362. \     :          mt_makediff(clp, clp.RemoveGrain(11, rg11).RemoveGrain(20, rg20).RemoveGrain(20, rg20), U=uv2, V=uv2)
  363. RG4D  = (r<=1) ? mt_makediff(clp, clp.RemoveGrain(4,  rg4 ), U=uv2, V=uv2)
  364. \     : (r==2) ? mt_makediff(clp, clp.Quantile(radius_y=2, radius_u=uvm2, radius_v=uvm2), U=uv2, V=uv2)
  365. \     :          mt_makediff(clp, clp.Quantile(radius_y=3, radius_u=uvm3, radius_v=uvm3), U=uv2, V=uv2)
  366.  
  367. DD    = mt_lutxy(RG11D, RG4D, "x 128 - y 128 - * 0 < 128 x 128 - abs y 128 - abs < x y ? ?", U=uv2, V=uv2)
  368.  
  369. return clp.mt_makediff(DD, U=uv, V=uv)
  370. }
  371.  
  372.  
  373. Function sbr(clip c, int "uv") {
  374. uv     = Default(uv, 1)
  375. uv2    = (uv==2) ? 1  : uv
  376. rg11   = (uv==3) ? 11 : -1
  377. rg11D  = mt_makediff(c, c.RemoveGrain(11, rg11), U=uv2, V=uv2)
  378. 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)
  379.  
  380. return c.mt_makediff(rg11DD, U=uv, V=uv)
  381. }
  382.  
  383.  
  384. Function CSmod_nrSpline64Resize(clip input, int "target_width", int "target_height", float "src_left", float "src_top", float "src_width", float "src_height", bool "chroma", val "nr") {
  385.  
  386.   w             = input.Width
  387.   h             = input.Height
  388.  
  389.   target_width  = Default(target_width,      w)
  390.   target_height = Default(target_height,     h)
  391.   src_left      = Default(src_left,          0)
  392.   src_top       = Default(src_top,           0)
  393.   src_width     = Default(src_width,         w)
  394.   src_height    = Default(src_height,        h)
  395.   chroma        = Default(chroma,         true)
  396.   nr            = Default(nr,             true)
  397.  
  398.   nr        = IsInt(nr) ? Float(nr) : nr
  399.   Assert( IsFloat(nr) || IsBool(nr), """CSmod_nrSpline64Resize: "nr" should be either bool or float!""" )
  400.  
  401.   res_mul   = Float( target_width * target_height ) / Float( w * h )
  402.   res_mul   = min( max( res_mul, 1 ), 2.25 )
  403.   nr_weight = IsFloat(nr)       ? nr
  404.   \         : nr /* == True  */ ? Spline( res_mul, 1, 0, 2.25, 1, 3.5, 0, true )
  405.   \         :    /* == False */   0
  406.   nr_weight = min( max( nr_weight, 0 ), 1 )
  407.  
  408.   forceYV12 = input.IsYV12 == false && nr_weight != 0
  409.  
  410.   try {
  411.     inputp = chroma ? forceYV12 ? input .ConvertToYV12
  412.         \                           : input
  413.         \               : input.ConvertToY8
  414.   } catch ( error_msg ) {
  415.     inputp = forceYV12 ? input.ConvertToYV12
  416.     \                  : input
  417.   }
  418.  
  419.   resize = inputp.Spline64Resize(target_width, target_height, src_left, src_top, src_width, src_height)
  420.   nrres  = inputp.GaussResize(target_width, target_height, src_left, src_top, src_width, src_height, p=100)
  421.   resize = resize.CSmod_GetCSP == "Y8" ? resize.ConvertToYV12 : resize
  422.   nrres  = nrres .CSmod_GetCSP == "Y8" ? nrres .ConvertToYV12 : nrres
  423.  
  424.   resize = nr_weight == 0 ? resize
  425.   \      : nr_weight == 1 ? resize.Repair(nrres, 1, chroma ? 1 : -1)
  426.   \      : chroma         ? Merge    (resize, resize.Repair(nrres, 1,  1), nr_weight)
  427.   \                       : MergeLuma(resize, resize.Repair(nrres, 1, -1), nr_weight)
  428.  
  429.   return resize
  430. }
  431.  
  432.  
  433. Function CSmod_GetCSP(clip c) {
  434.   return c.IsPlanar ? c.IsYV12 ? "YV12" :
  435.   \                   c.IsYV16 ? "YV16" :
  436.   \                   c.IsYV24 ? "YV24" : c.CSmod_Y8_YV411 :
  437.   \      c.IsYUY2   ? "YUY2"   :
  438.   \      c.IsRGB32  ? "RGB32"  :
  439.   \      c.IsRGB24  ? "RGB24"  : "Unknown"
  440. }
  441.  
  442.  
  443. Function CSmod_Y8_YV411(clip c) {
  444.   try {
  445.     c.UtoY
  446.     csp = "YV411"
  447.   } catch ( error_msg ) {
  448.     csp = "Y8"
  449.   }
  450.   return csp
  451. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top