Advertisement
Guest User

SeeSaw

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