Guest User

SeeSaw

a guest
Jul 31st, 2019
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Avisynth 16.50 KB | None | 0 0
  1. #  SeeSaw v0.3e (02 Jan 2006)
  2. #  ( modded 07 Sep 2018 )
  3. #    Mod due to this change in v2.6: Force int call arguments to user script function float params to be explicit floats.
  4. #    yet another one in 2019-07-30 for avs+
  5. #
  6. #  (Full Name: "Denoiser-and-Sharpener-are-riding-the-SeeSaw" )
  7. #
  8. #  This function provides a (simple) implementation of the "crystality sharpen" principle.
  9. #  In conjunction with a user-specified denoised clip, the aim is to enhance
  10. #  weak detail, hopefully without oversharpening or creating jaggies on strong
  11. #  detail, and produce a result that is temporally stable without detail shimmering,
  12. #  while keeping everything within reasonable bitrate requirements.
  13. #  This is done by intermixing source, denoised source and a modified sharpening process,
  14. #  in a seesaw-like manner.
  15. #
  16. #
  17. #  Usage:
  18. #
  19. #  a = TheNoisySource
  20. #  b = a.YourPreferredDenoising()
  21. #  SeeSaw( a, b, [parameters] )
  22. #
  23. #  You're very much encouraged to feed your own custom denoised clip into SeeSaw.
  24. #  If the "denoised" clip parameter is omitted, a simple "spatial pressdown" filter is used.
  25. #
  26. #
  27. #  Fiddled together by Didée, for your pleasure.
  28. #
  29.  
  30.  
  31.  
  32. # =======  Main function  =======
  33.  
  34. function SeeSaw( clip  input, clip "denoised",
  35.  \               int   "NRlimit",int "NRlimit2",
  36.  \               float "Sstr",   int "Slimit", float "Spower", float "SdampLo", float "SdampHi", float "Szp",
  37.  \               float "bias",   int "Smode",    int "sootheT",  int "sootheS", float "ssx",     float "ssy")
  38. {
  39. avs26   = !(VersionNumber() < 2.60)
  40. sisphbd = AvsPlusVersionNumber > 2294
  41. sislumaonly = sisphbd ? input.isy() : VersionNumber() < 2.6 ? true : input.isy8()
  42. clp = sislumaonly ? input : sisphbd ? input.converttoy() : input.converttoy8()
  43. denoised = defined(denoised) ? sislumaonly ? denoised : sisphbd ? denoised.converttoy() : denoised.converttoy8() : denoised
  44. ssx      = Float(default( ssx,1.0 ))         # supersampling factor x  /  SeeSaw doesn't require supersampling urgently.
  45.  
  46.  
  47. ssy      = Float(default( ssy,ssx ))         # supersampling factor y  /  if at all, small values ~1.25 seem to be enough.
  48. NRlimit  = default( NRlimit,    2 )          # absolute limit for pixel change by denoising
  49. NRlimit2 = default( NRlimit2, NRlimit+1)     # limit for intermediate denoising
  50. Sstr     = Float(default( Sstr,1.5 ))        # Sharpening strength (don't touch this too much)
  51. Slimit   = default( Slimit, NRlimit+2 )      # positive: absolute limit for pixel change by sharpening
  52.                                              # negative: pixel's sharpening difference is reduced to diff=pow(diff,1/abs(limit))
  53. Spower   = Int(default( Spower,4 ))          # exponent for modified sharpener
  54. Szp      = Float(default(Szp,16+2 ))         # zero point - below: overdrive sharpening - above: reduced sharpening
  55. SdampLo  = Float(default(SdampLo,Spower+1))  # reduces overdrive sharpening for very small changes
  56. SdampHi  = Float(default(SdampHi,24 ))       # further reduces sharpening for big sharpening changes. Try 15~30. "0" disables.
  57. bias     = Float(default( bias,49 ))         # bias towards detail ( >= 50 ) ,  or towards calm result ( < 50 )
  58. Smode    = default(Smode,ssx<1.35 ? 11 : ssx<1.51 ? 20 : 19 )
  59. sootheT  = default( sootheT,  49 )           # 0=minimum, 100=maximum soothing of sharpener's temporal instableness.
  60.                                              # (-100 .. -1 : will chain 2 instances of temporal soothing.)
  61. sootheS  = default( sootheS,   0 )           # 0=minimum, 100=maximum smoothing of sharpener's spatial effect.
  62.  
  63. Szp   = Szp / pow(Sstr, 1.0/4.0) / pow( (ssx+ssy)/2.0, 1.0/2.0 )
  64. SdampLo = SdampLo / pow(Sstr, 1.0/4.0) / pow( (ssx+ssy)/2.0, 1.0/2.0 )
  65.  
  66. ox=clp.width
  67. oy=clp.height
  68. xss = m4_seesaw(ox*ssx)
  69. yss = m4_seesaw(oy*ssy)
  70. NRL   = string( NRlimit  )
  71. NRL2  = string( NRlimit2 )
  72. NRLL  = string( int(round( NRlimit2 * 100.0/bias - 1.0 )) )
  73. SLIM  = string( abs(Slimit) )
  74. BIAS1 = string( bias )
  75. BIAS2 = string( 100-bias )
  76. #ZRP   = string( abs(Szp) )
  77. #PWR   = string( abs(Spower) )
  78. #DMP   = string( SdampLo )
  79.  
  80. denoised = defined(denoised) ? denoised : avs26 ? mt_lutxy(clp,clp.removegrain(4,-1),"x "+NRL+" scalef + y < x "+NRL+" scalef + x "+NRL+" scalef - y > x "+NRL+" scalef - y ? ?",chroma="copy first",use_expr=1,clamp_float=true) : mt_lutxy(clp,clp.removegrain(4,-1),"x "+NRL+" + y < x "+NRL+" + x "+NRL+" - y > x "+NRL+" - y ? ?",chroma="copy first")
  81.  
  82. NRdiff = mt_makediff(clp,denoised,chroma="process")
  83. tame   = avs26 ? mt_lutxy(clp,denoised,"x "+NRLL+" scalef + y < x "+NRL2+" scalef + x "+NRLL+" scalef - y > x "+NRL2+" scalef - x "+BIAS1+" * y "+BIAS2+" * + 100 / ? ?",use_expr=2,clamp_float=true) : mt_lutxy(clp,denoised,"x "+NRLL+" + y < x "+NRL2+" + x "+NRLL+" - y > x "+NRL2+" - x "+BIAS1+" * y "+BIAS2+" * + 100 / ? ?")
  84. head   = tame.sharpen2(Sstr,Spower,Szp,SdampLo,SdampHi,4)
  85. head = head.mt_merge(tame,tame.mt_edge("prewitt",0,255,0,0,U=1,V=1).mt_expand().removegrain(20,-1))
  86.  
  87. (ssx==1.0 && ssy==1.0) ? repair(tame.sharpen2(Sstr,Spower,Szp,SdampLo,SdampHi,Smode),head,1,-1,-1)
  88.  \                     : repair(tame.lanczosresize(xss,yss).sharpen2(Sstr,Spower,Szp,SdampLo,SdampHi,Smode),head.bicubicresize(xss,yss,-.2,.6),1,-1,-1).lanczosresize(ox,oy)
  89.  
  90. SootheSS(last,tame,sootheT,sootheS)
  91. sharpdiff= mt_makediff(tame,last)
  92.  
  93. (NRlimit==0) ? clp : \
  94. avs26 ? mt_lutxy(clp,NRdiff, "y range_half "+NRL+" scalef + > x "+NRL+" scalef - y range_half "+NRL+" scalef - < x "+NRL+" scalef + x y range_half - - ? ?",chroma="process",use_expr=1,clamp_float=true) : mt_lutxy(clp,NRdiff, "y 128 "+NRL+" + > x "+NRL+" - y 128 "+NRL+" - < x "+NRL+" + x y 128 - - ? ?",chroma="process")
  95.  
  96. Slimit>=0 ? avs26 ? mt_lutxy(last,sharpdiff,"y range_half "+SLIM+" scalef + > x "+SLIM+" scalef - y range_half "+SLIM+" scalef - < x "+SLIM+" scalef + x y range_half - - ? ?",chroma="copy first",use_expr=1,clamp_float=true) : mt_lutxy(last,sharpdiff,"y 128 "+SLIM+" + > x "+SLIM+" - y 128 "+SLIM+" - < x "+SLIM+" + x y 128 - - ? ?",chroma="copy first")
  97.  \        : avs26 ? mt_lutxy(last,sharpdiff,"y range_half == x x y range_half - abs 1 "+SlIM+" / ^ y range_half - y range_half - abs / * - ?",chroma="copy first",use_expr=1,clamp_float=true) : mt_lutxy(last,sharpdiff,"y 128 == x x y 128 - abs 1 "+SlIM+" / ^ y 128 - y 128 - abs / * - ?",chroma="copy first")
  98.  
  99. sislumaonly ? last : sisphbd ? CombinePlanes(last,input,planes="YUV",sample_clip=input) : ytouv(input.utoy8(),input.vtoy8(),last)
  100.  
  101. return( last )
  102. }
  103.  
  104.  
  105. # =======  Modified sharpening function  =======
  106.  
  107. function sharpen2(clip clp, float strength, float power, float zp, float lodmp, float hidmp, int rgmode, bool "sharpen22")
  108. {
  109. avs26= !(VersionNumber() < 2.60)
  110. STR  = string( strength )
  111. PWR  = string( 1.0/float(power) )
  112. ZRP  = string(    ZP    )
  113. DMP  = string(   lodmp  )
  114. HDMP = (hidmp==0) ? "1" : avs26 ? "1 scaleb x y - abs "+string(hidmp)+" / 4 ^ +" : "1 x y - abs "+string(hidmp)+" / 4 ^ +"
  115. sharpen22 = default(sharpen22, false )
  116. !sharpen22 ? Assert(power>0,"SeeSaw::Sharpen2: Power must be value 1 or more") : nop()
  117.  
  118. avs26 ? mt_lutxy( clp, clp.RemoveGrain(rgmode,-1,-1), \
  119.            "x y == x x x y - abs "+ZRP+" / "+PWR+" ^ "+ZRP+" * "+STR+" * x y - 2 ^ x y - 2 ^ "+DMP+" scalef + / * x y - x y - abs / * "+HDMP+" / + ?",chroma="copy first",use_expr=1,clamp_float=true) : \
  120. mt_lutxy( clp, clp.RemoveGrain(rgmode,-1,-1), \
  121.            "x y == x x x y - abs "+ZRP+" / "+PWR+" ^ "+ZRP+" * "+STR+" * x y - 2 ^ x y - 2 ^ "+DMP+" + / * x y - x y - abs / * "+HDMP+" / + ?",chroma="copy first")
  122. return( last )
  123. }
  124.  
  125.  
  126. # =======  Soothe() function to stabilze sharpening  =======
  127.  
  128. function SootheSS(clip sharp, clip orig, int "sootheT", int "sootheS", bool "SootheSS2", int "uv")
  129. {
  130. avs26   = !(VersionNumber() < 2.60)
  131. sootheT  = default(sootheT, 25 )
  132. sootheS  = default(sootheS,  0 )
  133. sootheT  = (sootheT > 100) ? 100 : (sootheT < -100) ? -100 : sootheT
  134. sootheS  = (sootheS > 100) ? 100 : (sootheS < 0) ? 0 : sootheS
  135. ST    = string( 100 - abs(sootheT))
  136. SSPT  = string( 100 - abs(sootheS))
  137. SootheSS2 = default(SootheSS2, false)
  138.  
  139. mt_makediff(orig,sharp)
  140.  
  141. (sootheS==0) ? last
  142.  \           : avs26 ? mt_lutxy( last, last.removegrain(SootheSS2 ? 1 : 20,-1,-1),
  143.  \             "x range_half - y range_half - * 0 < x range_half - 100 / "+SSPT+" * range_half + x range_half - abs y range_half - abs > x "+SSPT+" * y 100 "+SSPT+" - * + 100 / x ? ?", chroma="ignore",use_expr=1,clamp_float=true) : \
  144. mt_lutxy( last, last.removegrain(SootheSS2 ? 1 : 20,-1,-1),
  145.  \             "x 128 - y 128 - * 0 < x 128 - 100 / "+SSPT+" * 128 + x 128 - abs y 128 - abs > x "+SSPT+" * y 100 "+SSPT+" - * + 100 / x ? ?", chroma="ignore")
  146.  
  147. (sootheT==0) ? last
  148.  \           : avs26 ? mt_lutxy( last, last.temporalsoften(1,255,0,32,2),
  149.  \           "x range_half - y range_half - * 0 < x range_half - 100 / "+ST+" * range_half + x range_half - abs y range_half - abs > x "+ST+" * y 100 "+ST+" - * + 100 / x ? ?", chroma="ignore",use_expr=1,clamp_float=true) : \
  150. mt_lutxy( last, last.temporalsoften(1,255,0,32,2),
  151.  \           "x 128 - y 128 - * 0 < x 128 - 100 / "+ST+" * 128 + x 128 - abs y 128 - abs > x "+ST+" * y 100 "+ST+" - * + 100 / x ? ?", chroma="ignore")
  152.  
  153. (sootheT > -1) ? last
  154.  \             : avs26 ? mt_lutxy( last, last.temporalsoften(1,255,0,32,2),
  155.  \             "x range_half - y range_half - * 0 < x range_half - 100 / "+ST+" * range_half + x range_half - abs y range_half - abs > x "+ST+" * y 100 "+ST+" - * + 100 / x ? ?",chroma="ignore",use_expr=1,clamp_float=true) : \
  156. mt_lutxy( last, last.temporalsoften(1,255,0,32,2),
  157.  \             "x 128 - y 128 - * 0 < x 128 - 100 / "+ST+" * 128 + x 128 - abs y 128 - abs > x "+ST+" * y 100 "+ST+" - * + 100 / x ? ?",chroma="ignore")
  158.  
  159. mt_makediff(orig,last,u=uv,v=uv)
  160. return( last )
  161. }
  162.  
  163.  
  164. # =======  MOD4-and-atleast-16 helper function  =======
  165.  
  166. function m4_seesaw(float x) {
  167. nx=VersionNumber() < 2.6 ? x<16?16:int(round(x/4.0)*4) : round(x)
  168. return(nx)
  169. }
  170.  
  171. #  SeeSaw2 ( modified for standard definition dvds. by jeremy duncan november 11, 2008 )
  172. # this is fix ver. in 2018 9 07
  173. #    yet another one in 2019-07-30 for avs+
  174. #
  175. #  (Full Name: "Denoiser-and-Sharpener-are-riding-the-SeeSaw" )
  176. #
  177. #  This function provides a (simple) implementation of the "crystality sharpen" principle.
  178. #  In conjunction with a user-specified denoised clip, the aim is to enhance
  179. #  weak detail, hopefully without oversharpening or creating jaggies on strong
  180. #  detail, and produce a result that is temporally stable without detail shimmering,
  181. #  while keeping everything within reasonable bitrate requirements.
  182. #  This is done by intermixing source, denoised source and a modified sharpening process,
  183. #  in a seesaw-like manner.
  184. #
  185. #  This version is considered alpha.  
  186. #
  187. #  Usage:
  188. #
  189. #  a = TheNoisySource
  190. #  b = a.YourPreferredDenoising()
  191. #  SeeSaw( a, b, [parameters] )
  192. #
  193. #  You're very much encouraged to feed your own custom denoised clip into SeeSaw.
  194. #  If the "denoised" clip parameter is omitted, a simple "spatial pressdown" filter is used.
  195. #
  196. #
  197. #  Fiddled together by Didée, for your pleasure.
  198. #
  199. # - If you use a different resolution than standard definition dvd. Disable supersampling ssx by setting it to 1.0
  200.  
  201.  
  202. # =======  Main function  =======
  203.  
  204. function SeeSaw2( clip  input, clip "denoised",
  205.  \               int   "NRlimit",int "NRlimit2",
  206.  \               float "Sstr",   int "Slimit", float "Spower", float "SdampLo", float "SdampHi", float "Szp",
  207.  \               float "bias",   int "Smode",    int "sootheT",  int "sootheS", float "ssx",     float "ssy")
  208. {
  209. avs26   = !(VersionNumber() < 2.60)
  210. sisphbd = AvsPlusVersionNumber > 2294
  211. sislumaonly = sisphbd ? input.isy() : VersionNumber() < 2.6 ? true : input.isy8()
  212. clp = sislumaonly ? input : sisphbd ? input.converttoy() : input.converttoy8()
  213. denoised = defined(denoised) ? sislumaonly ? denoised : sisphbd ? denoised.converttoy() : denoised.converttoy8() : denoised
  214. ssx      = default( ssx,      1.0 )       # supersampling factor x  /  Set this to 1.24 for ntsc supersampling. Set this to 1.17 for Pal supersampling.
  215. # for a superspeed, set ssx to 1.00
  216.  
  217. ssy      = default( ssy,      ssx )       # supersampling factor y  /  if at all, small values ~1.25 seem to be enough.
  218. NRlimit  = default( NRlimit,    0 )       # absolute limit for pixel change by denoising
  219. NRlimit2 = default( NRlimit2, 5 )  # limit for intermediate denoising
  220. Sstr     = default( Sstr,     1.30 )       # Sharpening strength (don't touch this too much)
  221. Slimit   = default( Slimit, 40 )   # positive: absolute limit for pixel change by sharpening
  222.                                           # negative: pixel's sharpening difference is reduced to diff=pow(diff,1/abs(limit))
  223. Spower   = default( Spower,     1.0 )       # exponent for modified sharpener
  224. Szp      = default( Szp,     1 )       # zero point - below: overdrive sharpening - above: reduced sharpening
  225. SdampLo  = default( SdampLo, 25 )   # reduces overdrive sharpening for very small changes
  226. SdampHi  = default( SdampHi,   52 )       # further reduces sharpening for big sharpening changes. Try 15~30. "0" disables.
  227. bias     = default( bias,      49 )       # bias towards detail ( >= 50 ) ,  or towards calm result ( < 50 )
  228. Smode    = default( Smode,     ssx<1.35 ? 11 : ssx<1.51 ? 20 : 19 )
  229. sootheT  = default( sootheT,  55 )        # 0=minimum, 100=maximum soothing of sharpener's temporal instableness.
  230.                                           # (-100 .. -1 : will chain 2 instances of temporal soothing.)
  231. sootheS  = default( sootheS,   0 )        # 0=minimum, 100=maximum smoothing of sharpener's spatial effect.
  232.  
  233. Szp   = Szp / pow(Sstr, 1.0/4.0) / pow( (ssx+ssy)/2.0, 1.0/2.0 )
  234. SdampLo = SdampLo / pow(Sstr, 1.0/4.0) / pow( (ssx+ssy)/2.0, 1.0/2.0 )
  235.  
  236. ox=clp.width
  237. oy=clp.height
  238. xss = avs26 ? m4_seesaw(ox*ssx) : md_seesaw(ox*ssx)
  239. yss = avs26 ? m4_seesaw(oy*ssy) : md_seesaw(oy*ssy)
  240. NRL   = string( NRlimit  )
  241. NRL2  = string( NRlimit2 )
  242. NRLL  = string( int(round( NRlimit2 * 100.0/bias - 1.0 )) )
  243. SLIM  = string( abs(Slimit) )
  244. BIAS1 = string( bias )
  245. BIAS2 = string( 100-bias )
  246. #ZRP   = string( abs(Szp) )
  247. #PWR   = string( abs(Spower) )
  248. #DMP   = string( SdampLo )
  249.  
  250. denoised = defined(denoised) ? denoised : avs26 ? mt_lutxy(clp,clp.removegrain(1,-1),"x "+NRL+" scalef + y < x "+NRL+" scalef + x "+NRL+" scalef - y > x "+NRL+" scalef - y ? ?",chroma="copy first",use_expr=1,clamp_float=true) : mt_lutxy(clp,clp.removegrain(1,-1),"x "+NRL+" + y < x "+NRL+" + x "+NRL+" - y > x "+NRL+" - y ? ?",chroma="copy first")
  251.  
  252. NRdiff = mt_makediff(clp,denoised,chroma="process")
  253. tame   = avs26 ? mt_lutxy(clp,denoised,"x "+NRLL+" scalef + y < x "+NRL2+" scalef + x "+NRLL+" scalef - y > x "+NRL2+" scalef - x "+BIAS1+" * y "+BIAS2+" * + 100 / ? ?",use_expr=2,clamp_float=true) : mt_lutxy(clp,denoised,"x "+NRLL+" + y < x "+NRL2+" + x "+NRLL+" - y > x "+NRL2+" - x "+BIAS1+" * y "+BIAS2+" * + 100 / ? ?")
  254. head   = tame.sharpen2(Sstr,Spower,Szp,SdampLo,SdampHi,4,true)
  255. head   = head.mt_merge(tame,tame.mt_edge("prewitt",0,255,0,0,U=1,V=1).mt_expand().removegrain(1,-1))
  256.  
  257. (ssx==1.0 && ssy==1.0) ? repair(tame.sharpen2(Sstr,Spower,Szp,SdampLo,SdampHi,Smode,true),head,1,-1,-1)
  258.  \                     : repair(tame.spline64resize(xss, yss).sharpen2(Sstr,Spower,Szp,SdampLo,SdampHi,Smode,true),head.bicubicresize(xss, yss,0.75,1.0),1,-1,-1).lanczosresize(ox,oy)
  259.  
  260. SootheSS(last,tame,sootheT,sootheS,true)
  261. sharpdiff= mt_makediff(tame,last)
  262.  
  263. (NRlimit==0) ? clp : \
  264. avs26 ? mt_lutxy(clp,NRdiff, "y range_half "+NRL+" scalef + > x "+NRL+" scalef - y range_half "+NRL+" scalef - < x "+NRL+" scalef + x y range_half - - ? ?",chroma="process",use_expr=1,clamp_float=true) : mt_lutxy(clp,NRdiff, "y 128 "+NRL+" + > x "+NRL+" - y 128 "+NRL+" - < x "+NRL+" + x y 128 - - ? ?",chroma="process")
  265.  
  266. Slimit>=0 ? avs26 ? mt_lutxy(last,sharpdiff,"y range_half "+SLIM+" scalef + > x "+SLIM+" scalef - y range_half "+SLIM+" scalef - < x "+SLIM+" scalef + x y range_half - - ? ?",chroma="copy first",use_expr=1,clamp_float=true) : mt_lutxy(last,sharpdiff,"y 128 "+SLIM+" + > x "+SLIM+" - y 128 "+SLIM+" - < x "+SLIM+" + x y 128 - - ? ?",chroma="copy first")
  267.  \        : avs26 ? mt_lutxy(last,sharpdiff,"y range_half == x x y range_half - abs 1 "+SlIM+" / ^ y range_half - y range_half - abs / * - ?",chroma="copy first",use_expr=1,clamp_float=true) : mt_lutxy(last,sharpdiff,"y 128 == x x y 128 - abs 1 "+SlIM+" / ^ y 128 - y 128 - abs / * - ?",chroma="copy first")
  268.  
  269. sislumaonly ? last : sisphbd ? CombinePlanes(last,input,planes="YUV",sample_clip=input) : ytouv(input.utoy8(),input.vtoy8(),last)
  270.  
  271. return( last )
  272. }
  273.  
  274. # =======  MODd-and-atleast-16 helper function  =======
  275.  
  276. function md_seesaw(float x) {x<16?16:int(round(x/5)*12)} # for pal supersampling set the number 5 to 4.
Advertisement
Add Comment
Please, Sign In to add comment