Advertisement
mawen1250

Contra-Sharpen mod 16 1.3

Sep 21st, 2012
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Avisynth 31.42 KB | None | 0 0
  1. ###### Contra-Sharpen mod 16 1.3 ###### by mawen1250 ###### 2012.09.21 ######
  2. ###### Requirements: masktools v2.0a48, mvtools v2.5.11.3/v2.6.0.5, dither v1.21.0     ######
  3. ###### RemoveGrain v1.0pre, nnedi3 v0.9.4, TEdgeMask v0.9, tcanny v1.0, MSharpen v1.10 ######
  4.  
  5. Function CSmod16(clip filtered, clip "source", clip "pclip", bool "lsb_in", bool "lsb", int "dither", 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", bool "usepasf",
  9. \ int "Smethod", val "kernel", float "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", float "Sovershoot", float "Sundershoot", float "Tovershoot", float "Tundershoot",
  12. \ float "Soft",
  13. \ bool "limit", int "Repmode", int "RepmodeU", float "thr", float "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. lsb_in   = Default(lsb_in,  true   )
  19. lsb      = Default(lsb,     true   )
  20. dither   = Default(dither,  6      )
  21.  
  22. defsrc   = Defined(source   )
  23. defpclp  = Defined(pclip    )
  24. deffss   = Defined(filter_ss)
  25. deffnr   = Defined(filter_nr)
  26. filtered8=           !lsb_in ? filtered : filtered.DitherPost(mode=dither)
  27. source8  = defsrc  ? !lsb_in ? source   : source  .DitherPost(mode=dither)  : NOP()
  28. pclip8   = defpclp ? !lsb_in ? pclip    : pclip   .DitherPost(mode=dither)  : NOP()
  29. filtered =            lsb_in ? filtered : filtered.Dither_convert_8_to_16()
  30. source   = defsrc  ?  lsb_in ? source   : source  .Dither_convert_8_to_16() : NOP()
  31. pclip    = defpclp ?  lsb_in ? pclip    : pclip   .Dither_convert_8_to_16() : NOP()
  32. sw       = filtered.width()
  33. sh       = filtered.height()/2
  34. HD       = (sw > 1024 || sh > 576) ? true : false
  35.  
  36. chroma   = Default(chroma,  false  )
  37.  
  38. edgemode    = Default(edgemode,    0           )
  39. # 0 = Sharpening all, 1 = Sharpening only edge, 2 = Sharpening only non-edge.
  40. # By default, edgemode=2 is tuned for enhancing noise/small detail.
  41. # It will automatically set [edgemask=-2, ss_w=1, ss_h=1, preblur=0, kernel=5, Slimit=false, Tlimit=true(when limit=false)].
  42. edgemask    = Default(edgemask,    edgemode==2?-2:2)
  43. # -1: Similar to 1 and tweaking for edgemode=2,   -2: Similar to 2 and tweaking for edgemode=2,
  44. #  1: Same as edgemaskHQ=false in LSFmod(min/max), 2: Same as edgemaskHQ=true in LSFmod,
  45. #  3: Same as sharpening mask in MCTD(prewitt),    4: Same as mtype=1 in TAA(sobel),
  46. #  5: Same as mtype=2 in TAA(roberts),             6: Same as mtype=3 in TAA(prewitt),
  47. #  7: Same as mtype=4 in TAA(TEdgeMask),           8: Same as mtype=5 in TAA(tcanny),
  48. #  9: Same as mtype=6 in TAA(MSharpen),           10: (or any other numbers) My own method of tcanny usage.
  49. #     Otherwise define a custom edge mask clip, only luma is taken to merge all planes.
  50. edgethr     = Default(edgethr,     32.0        )   #Tweak edge mask thr in edgemask mode -2/-1/1/2/6/7/8/9/10.
  51. tcannysigma = Default(tcannysigma, 1.2         )   #Tweak tcanny's sigma in edgemask mode 8/10.
  52. mergesrc    = Default(mergesrc,    false       )   #Whether to merge clip "source" instead of clip "filtered" at the end of processing.
  53.  
  54. ss_w     = Default(ss_w,    edgemode>=2?1.00:1.50)   #Super sampling multiplier of width.
  55. ss_h     = Default(ss_h,    edgemode>=2?1.00:1.50)   #Super sampling multiplier of height.
  56. ss_w     = max    (ss_w,    1.00   )
  57. ss_h     = max    (ss_h,    1.00   )
  58. ss_hq    = Default(ss_hq,   false  )   #True using nnedi3_rpow2 in super sampling, false using non-ringing Spline64Resize.
  59. ssout    = Default(ssout,   false  )   #Output in super sampling resolution.
  60. ssoutc   = Default(ssoutc,  true   )   #Whether to output correct chroma when (chroma==false && ssout==true).
  61. ssrep    = Default(ssrep,   false  )   #When limiting sharpening to source clip, whether to repair in super sampling resolution.
  62. ssout    = (ss_w > 1.0 || ss_h > 1.0) ? ssout : false
  63. ssrep    = (ss_w > 1.0 || ss_h > 1.0) ? ssrep : false
  64. ssoutc   = (!chroma && ssout) ? ssoutc : false
  65. ssrep    = ssout ? true : ssrep
  66.  
  67. preblur  = Default(preblur, edgemode>=2?0:ss_w*ss_h>=9?-3:ss_w*ss_h>=4?-2:-1)
  68. # Pre-filtering, 0 = disable, -1 = Gaussian Blur radius=1(RG11), -2 = Gaussian Blur radius=2(RG11+RG20),
  69. # -3 = Gaussian Blur radius=3(RG11+RG20+RG20), -4 = Median Blur radius=1(RG4),
  70. # 1 = MinBlur16(mode=1)(RG11|RG4), 2 = MinBlur16(mode=2)(RG20|RG4), 3 = MinBlur16(mode=3)(SBR|RG4),
  71. # 4 = MinBlur16(mode=4)(Gaussian|Median radius=2), 5 = MinBlur16(mode=5)(Gaussian|Median radius=3).
  72. # Now Dither_median16 is very slow without optimization, if you want stronger pre-filter you can use other method such as MDegrain.
  73. # "preblur" is ignored when pclip is defined.
  74. prec     = Default(prec,    true   )   #Whether to process chroma plane in preblur.
  75. usepasf  = Default(usepasf, false  )
  76. # Whether to use pre-filtered clip as input(filtered) clip, which will reduce noise/halo/ring from input clip.
  77. usepasf  = (preblur==0 && !defpclp) ? false : usepasf
  78.  
  79. Smethod  = Default(Smethod, ss_w*ss_h>=4?3:1)   #Sharpen Method - 1: 3x3 kernel, 2: Min/Max, 3: Min/Max + 3x3 kernel.
  80. 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
  81. \                                                        : ss_w*ss_h>=4?4:ss_w*ss_h>=2.25?3:ss_w*ss_h> 1?2:1)
  82. # 1: Gaussian Blur radius=1(RG11), 2: Average Blur(RG20), 3: Gaussian Blur radius=2(RG11+RG20),
  83. # 4: Gaussian Blur radius=3(RG11+RG20+RG20), 5: for noise/detail enhance(RG19+RG4), default for edgemode==2,
  84. # Otherwise define a custom kernel in string such as kernel="Dither_removegrain16(20, 11)".
  85. secure   = Default(secure,  0.50   )
  86. # Threshold(on an 8-bit scale) to avoid banding & oil painting (or face wax) effect of sharpening, set 0 to disable it.(from LSFmod)
  87. Smode    = Default(Smode,   3      )
  88. # 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.
  89. divisor  = Default(divisor, 1.5    )
  90. index    = Default(index,   0.8    )
  91. #strength = Default(strength,80.0   )
  92. Szrp     = Default(Szrp,    16.0   )
  93. Spwr     = Default(Spwr,    4.0    )
  94. SdmpLo   = Default(SdmpLo,  4.0    )
  95. SdmpHi   = Default(SdmpHi,  48.0   )
  96. miSpwr   = 1.0/Spwr
  97. Szrp16   = Szrp*256
  98. divisor16= divisor*256
  99.  
  100. limit    = Default(limit,   true   )
  101. # Whether to limit sharpening to source clip, only takes effect when (Defined(source) || Defined(filter_ss) || usepasf) == true.
  102. thr      = Default(thr ,    0      )
  103. thrc     = Default(thrc,    thr    )
  104. # Allow sharpening more than the source pixel in [thr/thrc](luma/chroma)(on an 8-bit scale).
  105. Repmode  = Default(Repmode, 1      )
  106. RepmodeU = Default(RepmodeU,Repmode)
  107. thrc     = chroma ? thrc : 0
  108. limit    = defsrc || deffss || usepasf || deffnr ? limit : false
  109.  
  110. Slimit      = Default(Slimit,      edgemode>=2?false:!limit)
  111. # Spatial limit with overshoot and undershoot. Disabled when (limit==true && thr==0 && thrc==0).
  112. Tlimit      = Default(Tlimit,      edgemode>=2&&!limit?true:false)
  113. # Use MC Temporal limit at the end of sharpening(only process luma).(from MCTD)
  114. strength    = Default(strength,    Tlimit?120:80)
  115. Sovershoot  = Default(Sovershoot,  strength/120)   #(on an 8-bit scale)
  116. Sundershoot = Default(Sundershoot, Sovershoot  )   #(on an 8-bit scale)
  117. Tovershoot  = Default(Tovershoot,  edgemode>=2?strength/48:strength/24)   #(on an 8-bit scale)
  118. Tundershoot = Default(Tundershoot, Tovershoot  )   #(on an 8-bit scale)
  119. Soft        = Default(Soft,        limit?  0:-2)
  120. # Soft the sharpening effect (-1 = old autocalculate, -2 = new autocalculate, 0 = disable, (0, 100] = enable).
  121. # Disabled when (limit==true && thr==0 && thrc==0).(from LSFmod)
  122. Sovershoot  = max(Sovershoot,  0)
  123. Sundershoot = max(Sundershoot, 0)
  124. Tovershoot  = max(Tovershoot,  0)
  125. Tundershoot = max(Tundershoot, 0)
  126.  
  127. chromamv    = Default(chromamv,    chroma      )
  128. blksize     = Default(blksize,     HD ? 16 : 8 )
  129. overlap     = Default(overlap,     blksize/2   )
  130. thSAD       = Default(thSAD,       300         )
  131. thSCD1      = Default(thSCD1,      300         )
  132. thSCD2      = Default(thSCD2,      100         )
  133. truemotion  = Default(truemotion,  false       )
  134. MVglobal    = Default(MVglobal,    true        )
  135. pel         = Default(pel,         2           )
  136. pelsearch   = Default(pelsearch,   2           )
  137. search      = Default(search,      2           )
  138. searchparam = Default(searchparam, 2           )
  139. MVsharp     = Default(MVsharp,     2           )
  140. DCT         = Default(DCT,         0           )
  141.  
  142.  
  143. wss      = Round(sw*ss_w/8)*8
  144. hss      = Round(sh*ss_h/8)*8
  145. ssmethod = Defined(ssmethod) ? ssmethod
  146. \        :             ss_hq ? "nnedi3_rpow2(rfactor=2, qual=2).Dither_convert_8_to_16().CSmod16_nrSpline64Resize16(wss, hss, -0.5, -0.5, chroma=chroma||ssoutc, nr=true, hpgauss=false)"
  147. \        :                     "CSmod16_nrSpline64Resize16(wss, hss, chroma=chroma||ssoutc, nr=true, hpgauss=false)"
  148. # You can define your own super sampling method with ssmethod.
  149.  
  150. ch21     = chroma ? 2 : 1
  151. ch31     = chroma ? 3 : 1
  152. ch41     = chroma ? 4 : 1
  153. ch32     = chroma ? 3 : 2
  154. ch34     = chroma ? 3 : 4
  155. Slimit   = limit && thr==0 && thrc==0 ? false : Slimit
  156. Soft     = limit && thr==0 && thrc==0 ? 0
  157. \        : Soft <= -2  ? (1.0+(2.0/(ss_w+ss_h))) * sqrt(strength)
  158. \        : Soft == -1  ? sqrt( (((ss_w+ss_h)/2.0-1.0)*100.0) ) * 10
  159. \        : Soft <= 100 ? Soft : 100
  160.  
  161.  
  162. # super sampling, filtering in ss clip
  163. source      = defsrc ? source  : filtered
  164. source8     = defsrc ? source8 : source.DitherPost(mode=dither)
  165. filtered_os = filtered
  166. filtered_ss = (ss_w > 1.0 || ss_h > 1.0) ? Eval((ss_hq ? "filtered8." : "filtered.")+ssmethod) : filtered
  167. source_ss   = (ss_w > 1.0 || ss_h > 1.0) ? defsrc ? Eval((ss_hq ? "source8." : "source.")+ssmethod) : filtered_ss : source
  168. filtered    = deffss ? Eval("filtered_ss."+filter_ss) : filtered_ss
  169. filtered_ds = (ss_w > 1.0 || ss_h > 1.0) ? deffss ? filtered.CSmod16_nrSpline64Resize16(sw, sh, chroma=chroma, nr=0) : filtered_os : filtered
  170. filtered_ss8= filtered_ss.DitherPost(mode=0)
  171.  
  172. Assert( (isFrameBased(filtered_os)),                              """CSmod16: input clip "filtered" must be Frame Based!""" )
  173. Assert( (isFrameBased(source)),                                     """CSmod16: input clip "source" must be Frame Based!""" )
  174. Assert( (source.width() == sw && source.height()/2 == sh), """CSmod16: resolution of "filtered" and "source" must match!""" )
  175.  
  176.  
  177. # pre-filtering before sharpening
  178. pre         = defpclp     ? pclip
  179. \           : preblur<=-4 ? filtered.Dither_removegrain16(4 , chroma?prec?4 :0:-1)
  180. \           : preblur==-3 ? filtered.Dither_removegrain16(11, chroma?prec?11:0:-1).Dither_removegrain16(20, chroma?prec?20:0:-1)
  181. \                                   .Dither_removegrain16(20, chroma?prec?20:0:-1)
  182. \           : preblur==-2 ? filtered.Dither_removegrain16(11, chroma?prec?11:0:-1).Dither_removegrain16(20, chroma?prec?20:0:-1)
  183. \           : preblur==-1 ? filtered.Dither_removegrain16(11, chroma?prec?11:0:-1)
  184. \           : preblur>= 1 ? filtered.MinBlur16(preblur, chroma ? (prec ? 3 : 2) : 1)
  185. \           :               filtered
  186. # You can define your own pre-filter clip with pclip.
  187. pre_ds      = (pre.width() == sw && pre.height()/2 == sh) ? pre : pre.CSmod16_nrSpline64Resize16(sw, sh, chroma=chroma, nr=0)
  188. pre         = (ss_w > 1.0 || ss_h > 1.0) ? (pre.width() == wss && pre.height()/2 == hss) ? pre : Eval((ss_hq ? "pclip8." : "pclip.")+ssmethod) : pre_ds
  189. pre_ds8     = defpclp ? pclip8 : pre_ds.DitherPost(mode=-1)
  190. pre8        = pre.DitherPost(mode=-1)
  191.  
  192. # whether to use pre-filtered clip as main clip
  193. filtered    = usepasf ? pre    : filtered
  194. filtered_ds = usepasf ? pre_ds : filtered_ds
  195.  
  196.  
  197. # generate edge mask
  198. prefinal  = ssout ? Tlimit&&!chroma&&chromamv ? pre8.MergeChroma(filtered_ss8) : pre8 : Tlimit&&!chroma&&chromamv ? pre_ds8.MergeChroma(filtered8) : pre_ds8
  199. srcfinal  = ssout ? source_ss.DitherPost(mode=dither)                                 : source8
  200. edgemask  = IsClip(edgemask) ? edgemask
  201. \         : IsInt (edgemask) ?
  202. \           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),
  203. \                                       prefinal.mt_edge(thY1=0, thY2=255, "8 0 -8 16 0 -16 8 0 -8 4", U=1, V=1),
  204. \                                       "max", U=1, V=1).mt_lut("x " + string(edgethr*4) + " / 0.86 ^ 255 *", U=1, V=1)
  205. \                             .Removegrain(11, -1)
  206. \         : edgemask == -1   ? prefinal.mt_edge(thY1=0, thY2=255, mode="min/max", U=1, V=1)
  207. \                             .mt_lut("x " + string(edgethr) + " / 0.86 ^ 255 *", U=1, V=1)
  208. \                             .Removegrain(11, -1)
  209. \         : edgemask == 1    ? prefinal.mt_edge(thY1=0, thY2=255, mode="min/max", U=1, V=1)
  210. \                             .mt_lut("x " + string(edgethr) + " / 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 == 2    ? mt_logic(prefinal.mt_edge(thY1=0, thY2=255, "8 16 8 0 0 0 -8 -16 -8 4", U=1, V=1),
  213. \                                       prefinal.mt_edge(thY1=0, thY2=255, "8 0 -8 16 0 -16 8 0 -8 4", U=1, V=1),
  214. \                                       "max", U=1, V=1).mt_lut("x " + string(edgethr*4) + " / 0.86 ^ 255 *", U=1, V=1)
  215. \                             .mt_inflate(U=1, V=1).mt_inflate(U=1, V=1).Removegrain(11, -1)
  216. \         : edgemask == 3    ? mt_edge(prefinal, "prewitt", 0, 255, 0, 0, V=1, U=1)
  217. \                             .mt_lut("x 1.8 ^", U=1, V=1).Removegrain(4, -1).mt_inflate(U=1, V=1).Removegrain(20,-1)
  218. \         : edgemask == 4    ? mt_edge(prefinal, "sobel", 7, 7, 5, 5, U=1, V=1).mt_inflate(U=1, V=1)
  219. \         : edgemask == 5    ? mt_edge(prefinal, "roberts", 0, 4, 0, 4, U=1, V=1).mt_inflate(U=1, V=1)
  220. \         : edgemask == 6    ? mt_edge(prefinal, "prewitt", 0, 255, 0, 0, U=1, V=1)
  221. \                             .mt_lut("x " + string(edgethr) + " <= x 1 >> x 1.4 ^ ?", U=1, V=1)
  222. \                             .RemoveGrain(4, -1).mt_inflate(U=1, V=1)
  223. \         : edgemask == 7    ? TEdgeMask(prefinal, link=2, preblur=false, valon=-1, U=0, V=0)
  224. \                             .mt_lut("x " + string(edgethr/5) + " <= x 1 >> x 4 << ?", U=1, V=1)
  225. \                             .mt_deflate(U=1, V=1).RemoveGrain(HD ? 20 : 11, -1)
  226. \         : edgemask == 8    ? tcanny(srcfinal, sigma=tcannysigma, mode=1, plane=1)
  227. \                             .mt_lut("x " + string(edgethr) + " <= x 1 >> x 1 << ?", U=1, V=1)
  228. \                             .RemoveGrain(HD ? 20 : 11, -1).mt_inflate(U=1, V=1)
  229. \         : edgemask == 9    ? MSharpen(prefinal, threshold=Round(edgethr/5), strength=0, mask=true, highq=false)
  230. \         :                    tcanny(srcfinal, sigma=tcannysigma, mode=1, plane=1)
  231. \                             .mt_lut("x " + string(edgethr) + " <= 0 x " + string(edgethr) + " - 6 << ?", U=1, V=1)
  232. \                             .RemoveGrain(HD ? 20 : 11, -1).mt_inflate(U=1, V=1)
  233. \         :                    Assert(false, """CSmod16: "edgemask" should be int or clip!""")
  234.  
  235.  
  236. # unsharp
  237. dark_limit   = pre.CSmod16_inpand16(U=ch31, V=ch31)
  238. bright_limit = pre.CSmod16_expand16(U=ch31, V=ch31)
  239. minmaxavg    = CSmod16_average16(dark_limit, bright_limit, U=ch31, V=ch31)
  240.  
  241. method     = Smethod <= 1     ? pre : minmaxavg
  242.  
  243. method     = Smethod == 2     ? method
  244. \          : IsString(kernel) ? Eval("method."+kernel)
  245. \          : IsInt   (kernel) ?
  246. \            kernel <= 1      ? method.Dither_removegrain16(11, chroma?11:-1)
  247. \          : kernel == 2      ? method.Dither_removegrain16(20, chroma?20:-1)
  248. \          : kernel == 3      ? method.Dither_removegrain16(11, chroma?11:-1).Dither_removegrain16(20, chroma?20:-1)
  249. \          : kernel == 4      ? method.Dither_removegrain16(11, chroma?11:-1).Dither_removegrain16(20, chroma?20:-1)
  250. \                                     .Dither_removegrain16(20, chroma?20:-1)
  251. \          :                    method.Dither_removegrain16(19, chroma?19:-1).Dither_removegrain16(4 , chroma?4 :-1)
  252. \          :                    Assert(false, """CSmod16: "kernel" should be int or string!""")
  253.  
  254. method     = secure>0         ? pre.Dither_limit_dif16(method, thr=secure, elast=3.0, U=ch31, V=ch31) : method
  255.  
  256.  
  257. # making difference clip for sharpening
  258. sharpdiff  = Dither_sub16(pre, method, U=ch31, V=ch31, dif=true)
  259.  
  260.  
  261. # filtering in nr clip
  262. filtered   = deffnr ? Eval("method." + filter_nr).Dither_add16(sharpdiff, U=ch31, V=ch31, dif=true) : filtered
  263. filtered_ds= (ss_w > 1.0 || ss_h > 1.0) ? deffnr      ? filtered.CSmod16_nrSpline64Resize16(sw, sh, chroma=chroma, nr=0)
  264. \                                       : filtered_ds : filtered
  265.  
  266.  
  267. # sharpening diff generate mode
  268. sharpdiff  = IsString(Smode)  ? sharpdiff.Dither_lut16(Smode, U=ch31, V=ch31)
  269. \          : IsInt   (Smode)  ?
  270. \            Smode <= 0       ? sharpdiff
  271. \          : Smode == 1       ? sharpdiff.Dither_lut16("x 32768 == x x 32768 - "+string(strength/50.0)+" * 32768 + ?", U=ch31, V=ch31)
  272. \          : Smode == 2       ? sharpdiff.Dither_lut16("x 32768 == x x 32768 - "+String(divisor16)+" / Abs "+String(index)+" ^ "+String(strength*12.8)+" * x 32768 > 1 -1 ? * 32768 + ?", U=ch31, V=ch31)
  273. \          :                    sharpdiff.Dither_lut16("x 32768 == x x 32768 - abs "+string(Szrp16)+" / "+string(miSpwr)+" ^ "+string(Szrp)+" * "+string(strength*2.56)+" * x 32768 > 1 -1 ? * x 32768 - 256 / 2 ^ "+string(Szrp)+" 2 ^ "+string(SdmpLo)+" + * x 32768 - 256 / 2 ^ "+string(SdmpLo)+" + "+string(Szrp)+" 2 ^ * / * 1 "+string(SdmpHi)+" 0 == 0 "+string(Szrp)+" "+string(SdmpHi)+" / 4 ^ ? + 1 "+string(SdmpHi)+" 0 == 0 x 32768 - 256 / abs "+string(SdmpHi)+" / 4 ^ ? + / * 32768 + ?", U=ch31, V=ch31)
  274. \          :                    Assert(false,  """CSmod16: "Smode" should be int or string!""")
  275.  
  276. # x==32768 ? x : (abs(x-32768)/Szrp16)^(miSpwr)*Szrp*(strength*2.56)*(x>32768 ? 1 : -1) * (((x-32768)/256)^2*(Szrp^2+SdmpLo) / (((x-32768)/256)^2+SdmpLo)*Szrp^2) * ((1+ SdmpHi==0 ? 0 : (Szrp/SdmpHi)^4) / (1+ SdmpHi==0 ? 0 : (abs((x-32768)/256)/SdmpHi)^4)) + 32768
  277.  
  278.  
  279. # spatial limit
  280. DlimitF    = usepasf || (preblur==0 && !defpclp) ?   dark_limit : filtered.CSmod16_inpand16(U=ch31, V=ch31)
  281. BlimitF    = usepasf || (preblur==0 && !defpclp) ? bright_limit : filtered.CSmod16_expand16(U=ch31, V=ch31)
  282. sclp       = Slimit ? Dither_add16(filtered, sharpdiff, U=ch31, V=ch31, dif=true) : NOP()
  283. sclp       = Slimit ? sclp.CSmod16_clamp16(BlimitF, DlimitF, Sovershoot, Sundershoot, U=ch31, V=ch31) : NOP()
  284.  
  285.  
  286. # Soft
  287. sharpdiff  = Slimit ? Dither_sub16(sclp, filtered, U=ch31, V=ch31, dif=true) : sharpdiff
  288. sharpdiffS = sharpdiff.Dither_removegrain16(19, chroma ? 19 : -1)
  289. wmask      = sharpdiff.Dither_lut16(Y=Round(-655.35*Soft), U=chroma?Round(-655.35*Soft):1, V=chroma?Round(-655.35*Soft):1)
  290. sharpdiffW = Soft == 100 ? sharpdiffS : Dither_merge16(sharpdiff, sharpdiffS, wmask, luma=false, U=ch31, V=ch31)
  291. sharpdiff  = Soft ==   0 ? sharpdiff  : CSmod16_limitdiff16(sharpdiff, sharpdiffW, U=ch31, V=ch31)
  292.  
  293.  
  294. # the difference achieved by filtering
  295. allD       = limit  ? ssrep && (ss_w > 1.0 || ss_h > 1.0) ? Dither_sub16(source_ss, filtered, U=ch31, V=ch31, dif=true)
  296. \                                                         : Dither_sub16(source, filtered_ds, U=ch31, V=ch31, dif=true) : NOP()
  297.  
  298. # limiting sharpening to source clip
  299. sharpdiff  = !ssrep && (ss_w > 1.0 || ss_h > 1.0) ? sharpdiff.CSmod16_nrSpline64Resize16(sw, sh, chroma=chroma, nr=0) : sharpdiff
  300. ssDD       = limit ? sharpdiff.Dither_repair16(allD, mode=Repmode, modeU=chroma ? RepmodeU : -1) : sharpdiff
  301. ssDD       = limit ? ssDD.CSmod16_limitdiff16(sharpdiff, U=ch31, V=ch31) : ssDD
  302. ssDDl      = thr  > 0 ? sharpdiff.Dither_limit_dif16(limit ? ssDD : sharpdiff.CSmod16_gen_null_diff(), thr=thr , elast=3.0, Y=3, U=1, V=1) : ssDD
  303. ssDDc      = thrc > 0 ? sharpdiff.Dither_limit_dif16(limit ? ssDD : sharpdiff.CSmod16_gen_null_diff(), thr=thrc, elast=3.0, Y=1, U=3, V=3) : ssDD
  304. ssDD       = thr  > 0 || thrc > 0 ? thr == thrc ?
  305. \                       sharpdiff.Dither_limit_dif16(limit ? ssDD : sharpdiff.CSmod16_gen_null_diff(), thr=thr , elast=3.0, Y=3, U=3, V=3) : ssDDl.MergeChroma(ssDDc) : ssDD
  306. ssDD       = ssrep && (ss_w > 1.0 || ss_h > 1.0) && !ssout ? ssDD.CSmod16_nrSpline64Resize16(sw, sh, chroma=chroma, nr=0) : ssDD
  307.  
  308. # add difference clip to clip "filtered" of ss/original resolution
  309. sclp       = ssout ? Dither_add16(filtered, ssDD, U=ch31, V=ch31, dif=true) : Dither_add16(filtered_ds, ssDD, U=ch31, V=ch31, dif=true)
  310.  
  311.  
  312. # temporal limit
  313. srcfinal16 = ssout ? source_ss : source
  314.  
  315. sMVS       = prefinal.MSuper(hpad=0, vpad=0, pel=pel, levels=0, sharp=MVsharp, chroma=chromamv)
  316. rMVS       = usepasf || (preblur==0 && !defpclp) ? sMVS
  317. \                                                : srcfinal.MSuper(hpad=0, vpad=0, pel=pel, levels=1, sharp=MVsharp, chroma=chroma)
  318. 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)
  319. 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)
  320. f1c        = MCompensate(srcfinal, rMVS, f1v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  321. b1c        = MCompensate(srcfinal, rMVS, b1v, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  322. Tmax       = srcfinal.mt_logic(f1c, "max", U=ch31, V=ch31).mt_logic(b1c, "max", U=ch31, V=ch31).Dither_convert_8_to_16()
  323. Tmin       = srcfinal.mt_logic(f1c, "min", U=ch31, V=ch31).mt_logic(b1c, "min", U=ch31, V=ch31).Dither_convert_8_to_16()
  324. Tmax       = lsb_in ? srcfinal16.Dither_limit_dif16(Tmax, thr=0.50, elast=3.0, U=ch31, V=ch31) : Tmax
  325. Tmin       = lsb_in ? srcfinal16.Dither_limit_dif16(Tmin, thr=0.50, elast=3.0, U=ch31, V=ch31) : Tmin
  326. sclp       = Tlimit ? sclp.CSmod16_clamp16(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 ? Dither_merge16_8(merged_ss, sclp, edgemask,
  338. \                                    luma=chroma, Y=3, U=ssoutc?ch32:ch31, V=ssoutc?ch32:ch31)
  339. \                                  : Dither_merge16_8(merged_os, sclp, edgemask,
  340. \                                    luma=chroma, Y=3, U=ch32,             V=ch32            )
  341. \                          : ssout ? Dither_merge16_8(sclp, merged_ss, edgemask,
  342. \                                    luma=chroma, Y=3, U=ssoutc?ch34:ch31, V=ssoutc?ch34:ch31)
  343. \                                  : Dither_merge16_8(sclp, merged_os, edgemask,
  344. \                                    luma=chroma, Y=3, U=ch34,             V=ch34            )
  345.  
  346. return lsb ? end : end.DitherPost(mode=dither)
  347. }
  348.  
  349.  
  350. Function MinBlur16(clip clp, int "mode", int "uv"){
  351.  
  352. mode  = Default(mode, 1)
  353. uv    = Default(uv,   3)
  354.  
  355. uv2   = (uv==2) ? 1  : uv
  356. rg4   = (uv==3) ? 4  : -1
  357. rg11  = (uv==3) ? 11 : -1
  358. rg20  = (uv==3) ? 20 : -1
  359.  
  360. RG11D = (mode<=1) ? Dither_sub16(clp, clp.Dither_removegrain16(11, rg11), U=uv2, V=uv2, dif=true)
  361. \     : (mode==2) ? Dither_sub16(clp, clp.Dither_removegrain16(20, rg20), U=uv2, V=uv2, dif=true)
  362. \     : (mode==3) ? Dither_sub16(clp, clp.sbr16(uv=uv2), U=uv2, V=uv2, dif=true)
  363. \     : (mode==4) ? Dither_sub16(clp, clp.Dither_removegrain16(11, rg11).Dither_removegrain16(20, rg20), U=uv2, V=uv2, dif=true)
  364. \     :             Dither_sub16(clp, clp.Dither_removegrain16(11, rg11).Dither_removegrain16(20, rg20)
  365. \                                        .Dither_removegrain16(20, rg20), U=uv2, V=uv2, dif=true)
  366. RG4D  = (mode<=3) ? Dither_sub16(clp, clp.Dither_removegrain16(4,  rg4 ), U=uv2, V=uv2, dif=true)
  367. \     : (mode==4) ? Dither_sub16(clp, clp.Dither_median16(2, 2, 0, U=uv2, V=uv2), U=uv2, V=uv2, dif=true)
  368. \     :             Dither_sub16(clp, clp.Dither_median16(3, 3, 0, U=uv2, V=uv2), U=uv2, V=uv2, dif=true)
  369.  
  370. DD    = CSmod16_limitdiff16(RG11D, RG4D, U=uv2, V=uv2)
  371.  
  372. return clp.Dither_sub16(DD, U=uv, V=uv, dif=true)
  373. }
  374.  
  375.  
  376. Function sbr16(clip clp, int "uv") {
  377. uv       = Default(uv, 1)
  378. uv2      = (uv==2) ? 1  : uv
  379. rg11     = (uv==3) ? 11 : -1
  380.  
  381. rg11D    = Dither_sub16(clp, clp.Dither_removegrain16(11, rg11), U=uv2, V=uv2, dif=true)
  382. rg11Dr   = rg11D.Dither_removegrain16(11, rg11)
  383.  
  384. abrg11D  = rg11D.Dither_lut16("x 32768 - abs", U=uv2, V=uv2)
  385. Ddiff    = Dither_sub16(rg11D, rg11Dr, U=uv2, V=uv2, dif=true)
  386. abDdiff  = Ddiff.Dither_lut16("x 32768 - abs", U=uv2, V=uv2)
  387. abDDD    = Dither_sub16(abDdiff, abrg11D, U=uv2, V=uv2, dif=true)
  388.  
  389. Dmask1   = abDDD.Dither_lut16("x 32768 < 65535 0 ?", U=uv2, V=uv2)
  390. Ddiffg   = Ddiff.Dither_lut16("x 32768 == x x 32768 < 0 65535 ? ?", U=uv2, V=uv2).Crop(0, 0, 0, -Ddiff.height()/2)
  391. rg11Dg   = rg11D.Dither_lut16("x 32768 == x x 32768 < 0 65535 ? ?", U=uv2, V=uv2).Crop(0, 0, 0, -rg11D.height()/2)
  392. Dmask2   = mt_lutxy(Ddiffg, rg11Dg, "x 128 - y 128 - * 0 < 0 255 ?", U=uv2, V=uv2)
  393.  
  394. DD1      = Dither_merge16(rg11D, Ddiff, Dmask1, luma=false, U=uv2, V=uv2)
  395. DD2      = Dither_merge16_8(DD1.CSmod16_gen_null_diff(), DD1, Dmask2, luma=false, U=uv2, V=uv2)
  396.  
  397. return clp.Dither_sub16(DD2, U=uv, V=uv, dif=true)
  398. }
  399.  
  400.  
  401. Function CSmod16_nrSpline64Resize16(clip input, int "target_width", int "target_height", float "src_left", float "src_top", float "src_width", float "src_height", bool "chroma", val "nr", bool "hpgauss") {
  402.  
  403.   w             = input.width()
  404.   h             = input.height()/2
  405.  
  406.   target_width  = Default(target_width,      w)
  407.   target_height = Default(target_height,     h)
  408.   src_left      = Default(src_left,          0)
  409.   src_top       = Default(src_top,           0)
  410.   src_width     = Default(src_width,         w)
  411.   src_height    = Default(src_height,        h)
  412.   chroma        = Default(chroma,         true)
  413.   nr            = Default(nr,             true)
  414.   hpgauss       = Default(hpgauss,       false)
  415.  
  416.   nr        = IsInt(nr) ? Float(nr) : nr
  417.   Assert( IsFloat(nr) || IsBool(nr), """CSmod16_nrSpline64Resize16: "nr" should be either bool or float!""" )
  418.  
  419.   res_mul   = Float( target_width * target_height ) / Float( w * h )
  420.   res_mul   = min( max( res_mul, 1 ), 2.25 )
  421.   nr_weight = IsFloat(nr)       ? nr
  422.   \         : nr /* == True  */ ? Spline( res_mul, 1, 0, 2.25, 1, 3.5, 0, true )
  423.   \         :    /* == False */   0
  424.   nr_weight = min( max( nr_weight, 0 ), 1 )
  425.   wmaskv    = Round(-65535*nr_weight)
  426.  
  427.     try {
  428.     inputp = chroma ? input
  429.     \               : input.ConvertToY8
  430.     conv   = true
  431.   } catch ( error_msg ) {
  432.     inputp = input
  433.     conv   = false
  434.   }
  435.  
  436.   resize = input.Dither_resize16(target_width, target_height, src_left, src_top, src_width, src_height, kernel="Spline64", Y=3, U=chroma?3:1, V=chroma?3:1)
  437.   nrres  = hpgauss ? input.Dither_resize16(target_width, target_height, src_left, src_top, src_width, src_height, kernel="Gaussian", a1=100, Y=3, U=chroma?3:1, V=chroma?3:1) : inputp.DitherPost(mode=0, Y=3, U=chroma?3:1, V=chroma?3:1).GaussResize(target_width, target_height, src_left, src_top, src_width, src_height, p=100).Dither_convert_8_to_16()
  438.   nrres  = hpgauss||chroma||!conv ? nrres : Eval("nrres.ConvertTo"+input.CSmod16_GetCSP())
  439.   wmask  = resize.Dither_lut16(Y=wmaskv, U=chroma?wmaskv:1, V=chroma?wmaskv:1)
  440.  
  441.   resize = nr_weight == 0 ? resize
  442.   \      : nr_weight == 1 ? resize.Dither_repair16(nrres, 1, chroma ? 1 : -1)
  443.   \      :                  Dither_merge16(resize, resize.Dither_repair16(nrres, 1, chroma ? 1 : -1), wmask, luma=false, Y=3, U=chroma?3:1, V=chroma?3:1)
  444.  
  445.   return resize
  446. }
  447.  
  448.  
  449. Function CSmod16_GetCSP(clip c) {
  450.   return c.IsPlanar ? c.IsYV12 ? "YV12" :
  451.   \                   c.IsYV16 ? "YV16" :
  452.   \                   c.IsYV24 ? "YV24" : c.CSmod16_Y8_YV411 :
  453.   \      c.IsYUY2   ? "YUY2"   :
  454.   \      c.IsRGB32  ? "RGB32"  :
  455.   \      c.IsRGB24  ? "RGB24"  : "Unknown"
  456. }
  457.  
  458.  
  459. Function CSmod16_Y8_YV411(clip c) {
  460.   try {
  461.     c.UtoY
  462.     csp = "YV411"
  463.   } catch ( error_msg ) {
  464.     csp = "Y8"
  465.   }
  466.   return csp
  467. }
  468.  
  469.  
  470. Function CSmod16_inpand16(clip input, int "Y", int "U", int "V")
  471. {
  472. Y          = Default(Y,        3)
  473. U          = Default(U,        1)
  474. V          = Default(V,        1)
  475.  
  476. input_msb  = input.Crop(0, 0, 0, -input.height()/2)
  477. inpand_msb = input_msb.mt_inpand(Y=Y, U=U, V=V)
  478. inpand_16  = inpand_msb.Dither_convert_8_to_16()
  479.  
  480. return inpand_16.Dither_repair16(input, Y==3?1:Y==2?0:-1, U==3?1:U==2?0:-1, V==3?1:V==2?0:-1)
  481. }
  482.  
  483.  
  484. Function CSmod16_expand16(clip input, int "Y", int "U", int "V")
  485. {
  486. Y          = Default(Y,        3)
  487. U          = Default(U,        1)
  488. V          = Default(V,        1)
  489.  
  490. input_msb  = input.Crop(0, 0, 0, -input.height()/2)
  491. expand_msb = input_msb.mt_lut("x 1 +", Y=Y, U=U, V=V).mt_expand(Y=Y, U=U, V=V)
  492. expand_16  = expand_msb.Dither_convert_8_to_16()
  493.  
  494. return expand_16.Dither_repair16(input, Y==3?1:Y==2?0:-1, U==3?1:U==2?0:-1, V==3?1:V==2?0:-1)
  495. }
  496.  
  497.  
  498. Function CSmod16_average16(clip input1, clip input2, int "Y", int "U", int "V")
  499. {
  500. Y          = Default(Y,        3)
  501. U          = Default(U,        3)
  502. V          = Default(V,        3)
  503.  
  504. hmask = input1.CSmod16_gen_null_diff()
  505.  
  506. return Dither_merge16(input1, input2, hmask, luma=false, Y=Y, U=U, V=V)
  507. }
  508.  
  509. Function CSmod16_limitdiff16(clip diff1, clip diff2, int "Y", int "U", int "V")
  510. {
  511. Y          = Default(Y,        3)
  512. U          = Default(U,        3)
  513. V          = Default(V,        3)
  514.  
  515. abdiff1    = Dither_lut16(diff1, "x 32768 - abs", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  516. abdiff2    = Dither_lut16(diff2, "x 32768 - abs", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  517. abdiffdiff = Dither_sub16(abdiff1, abdiff2, Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1, dif=true)
  518. bin        = Dither_lut16(abdiffdiff, "x 32768 <= 0 65535 ?", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  519.  
  520. return Dither_merge16(diff1, diff2, bin, luma=false, Y=Y, U=U, V=V)
  521. }
  522.  
  523.  
  524. Function CSmod16_clamp16(clip c, clip bright_limit, clip dark_limit, float "overshoot", float "undershoot", int "Y", int "U", int "V")
  525. {
  526. Y          = Default(Y,          3)
  527. U          = Default(U,          1)
  528. V          = Default(V,          1)
  529. overshoot  = Default(overshoot,  0)
  530. undershoot = Default(undershoot, 0)
  531. os16       = string(overshoot *256)
  532. us16       = string(undershoot*256)
  533.  
  534. brightdiff = Dither_sub16(c, bright_limit, Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1, dif=true)
  535. darkdiff   = Dither_sub16(c,   dark_limit, Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1, dif=true)
  536.  
  537. brightDdec = brightdiff.Dither_lut16("x " + os16 + " - 32768 > x " + os16 + " - 32768 ?", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  538. darkDdec   = darkdiff  .Dither_lut16("x " + us16 + " + 32768 < x " + us16 + " + 32768 ?", Y=Y==3?3:1, U=U==3?3:1, V=V==3?3:1)
  539.  
  540. return c.Dither_sub16(brightDdec, Y=Y, U=U, V=V, dif=true).Dither_sub16(darkDdec, Y=Y, U=U, V=V, dif=true)
  541. }
  542.  
  543.  
  544. Function CSmod16_gen_null_diff(clip input, bool "lsb_in")
  545. {
  546.     lsb_in = Default(lsb_in, true)
  547.    
  548.     vers   = VersionNumber ()
  549.     p_t    = (vers < 2.60) ? "YV12" : Dither_undef ()
  550.    
  551.     input    = lsb_in ? input.Crop(0, 0, 0, -input.height/2) : input
  552.    
  553.     StackVertical(BlankClip(input, pixel_type=p_t, color_yuv=8421504), BlankClip(input, pixel_type=p_t, color_yuv=0))
  554. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement