Advertisement
mawen1250

Contra-Sharpen mod 16 1.1

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