Advertisement
mawen1250

Contra-Sharpen mod 16 1.2

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