Advertisement
mawen1250

Contra-Sharpen mod 16 1.4

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