Advertisement
Guest User

LSFmod

a guest
Sep 19th, 2019
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ################################################################################################
  2. ###                                                                                          ###
  3. ###                       LimitedSharpenFaster MOD : function LSFmod()                       ###
  4. ###                                                                                          ###
  5. ###                                Modded Version by LaTo INV.                               ###
  6. ###                                                                                          ###
  7. ###                                  v1.9 - 05 October 2009                                  ###
  8. ###                                                                                          ###
  9. ###                            another Modded Version by A.SONY.                             ###
  10. ###                                                                                          ###
  11. ###                                v2.185 - 19 September 2019                                ###
  12. ###                                                                                          ###
  13. ################################################################################################
  14. ###
  15. ### +-----------+
  16. ### | CHANGELOG |
  17. ### +-----------+
  18. ###
  19. ### v2.185 : - new avs26_optimize (bool) true by default, you can set it to false for smode=1 and smode=2 since not all filter updated to avs 2.6 colors
  20. ###
  21. ### v2.184 : - some fix when using soruce and make sure it will not work with RGB
  22. ###
  23. ### v2.183 : - fix bugs when show=true cases
  24. ###
  25. ### v2.18 : - fix upscale, i.e. dest_x or dest_y are larger than the input clip
  26. ###
  27. ### v2.12 : - HBD in avs+
  28. ###         - some fixes
  29. ###
  30. ### v1.9 : - tweaked settings
  31. ###        - default preset is now defaults="fast" /!\
  32. ###
  33. ### v1.8 : - changed preblur to allow more tweaking (bool->string)
  34. ###        - tweaked settings
  35. ###        - cleaned the code
  36. ###        - updated documentation
  37. ###
  38. ### v1.7 : - changed Smethod=4 to "source"
  39. ###
  40. ### v1.6 : - added preblur option
  41. ###        - added new Smethod=4
  42. ###
  43. ### v1.5 : - fixed LUT expression (thanks to Didée)
  44. ###        - changed Smethod to Smethod+secure
  45. ###
  46. ### v1.4 : - changed defaults="new" to defaults="slow" & defaults="fast"
  47. ###        - added show parameter
  48. ###        - cleaned a little the code
  49. ###
  50. ### v1.3 : - changed a little Smethod=3&5 (same effect, but more precise)
  51. ###        - added new calculation for soft (soft=-2) [default on]
  52. ###        - added warning about bad settings (no more silent)
  53. ###        - updated the documentation
  54. ###
  55. ### v1.2 : - added new Lmode<0 (limit with repair)
  56. ###        - added 2 new Smode (unsharp masking)
  57. ###        - changed Smode order: now old Smode3-4 is new Smode3-4 to avoid mistake
  58. ###
  59. ### v1.1 : - fixed a bug with dest_x!=ox or dest_y!=oy
  60. ###        - replaced Lfactor by over/undershoot2
  61. ###
  62. ### v1.0 : - deleted old Smode(1-4), added new Smode(1-3) & Smethod(1-5)
  63. ###        - added parameters for nonlinear sharpening (S2zp,S2pwr,S2dmpLo,S2dmpHi)
  64. ###        - corrected the nonlinear formula
  65. ###        - added new Lmode 2 & 4 + fixed Lmode 0
  66. ###        - added faster edgemask
  67. ###        - added soothe temporal stabilization, 2 parameters: soothe & keep
  68. ###        - replaced lanczosresize by spline36resize
  69. ###        - moved "strength" parameter (first place)
  70. ###        - deleted wide, special and exborder
  71. ###        - changed some code (cosmetic)
  72. ###        - added "defaults" parameter (to switch between original and modded version)
  73. ###        - added documentation
  74. ###
  75. ###
  76. ###
  77. ### +--------------+
  78. ### | DEPENDENCIES |
  79. ### +--------------+
  80. ###
  81. ### -> Masktools    (v2.2.15 or up)
  82. ### -> Removegrain  (v1.0pre or up)
  83. ### -> Warpsharp    (2003.11.03 or up) [for Smode=1]
  84. ### -> Variableblur (v0.30 or up)      [for Smode=2]
  85. ### -> if you use avs+ then make sure it's the last one
  86. ###
  87. ###
  88. ###
  89. ###
  90. ### +-------+
  91. ### | USAGE |
  92. ### +-------+
  93. ###
  94. ### LSFmod( strength, Smode, Smethod, kernel,
  95. ###         preblur, secure, source,
  96. ###         Szrp, Spwr, SdmpLo, SdmpHi,
  97. ###         Lmode, overshoot, undershoot, overshoot2, undershoot2,
  98. ###         soft, soothe, keep,
  99. ###         edgemode, edgemaskHQ,
  100. ###         ss_x, ss_y, dest_x, dest_y,
  101. ###         show, screenW, screenH,
  102. ###         defaults )
  103. ###
  104. ###
  105. ###
  106. ### +---------+
  107. ### | GENERAL |
  108. ### +---------+
  109. ###
  110. ### strength [int]
  111. ### --------------
  112. ### Strength of the sharpening
  113. ###
  114. ### Smode [int: 1,2,3,4,5]
  115. ### ----------------------
  116. ### Sharpen mode:
  117. ###    =1 : Unsharp masking (from warpsharp)
  118. ###    =2 : Unsharp masking (from variableblur)
  119. ###    =3 : Range sharpening
  120. ###    =4 : Nonlinear sharpening (original  version)
  121. ###    =5 : Nonlinear sharpening (corrected version)
  122. ###
  123. ### Smethod [int: 1,2,3]
  124. ### --------------------
  125. ### Sharpen method: (only used in Smode=3,4,5)
  126. ###    =1 : 3x3 kernel
  127. ###    =2 : Min/Max
  128. ###    =3 : Min/Max + 3x3 kernel
  129. ###
  130. ### kernel [int: 11,12,19,20]
  131. ### -------------------------
  132. ### Kernel used in Smethod=1&3
  133. ### In strength order: + 19 > 12 >> 20 > 11 -
  134. ###
  135. ###
  136. ###
  137. ### +---------+
  138. ### | SPECIAL |
  139. ### +---------+
  140. ###
  141. ### preblur [string: "ON","OFF",...]
  142. ### --------------------------------
  143. ### Mode to avoid noise sharpening & ringing (only used in Smode=3,4,5)
  144. ### "ON" is sufficient to prevent ringing, but to prevent noise sharpening you should set your own denoiser
  145. ###    Usage:   LSFmod(preblur="YourFavoriteDenoiser()")
  146. ###    Example: LSFmod(preblur="FFT3Dfilter(sigma=4,plane=0)")
  147. ###
  148. ###
  149. ### secure [bool]
  150. ### -------------
  151. ### Mode to avoid banding & oil painting (or face wax) effect of sharpening
  152. ### (only used in Smode=3,4,5)
  153. ###
  154. ### source [clip]
  155. ### -------------
  156. ### If source is defined, LSFmod doesn't sharp more a denoised clip than this source clip
  157. ### In this mode, you can safely set Lmode=0 & PP=off
  158. ###    Usage:   denoised.LSFmod(source=source)
  159. ###    Example: last.FFT3Dfilter().LSFmod(source=last,Lmode=0,soft=0)
  160. ###
  161. ###
  162. ###
  163. ### +----------------------+
  164. ### | NONLINEAR SHARPENING |
  165. ### +----------------------+
  166. ###
  167. ### Szrp [int]
  168. ### ----------
  169. ### Zero Point:
  170. ###    - differences below Szrp are amplified (overdrive sharpening)
  171. ###    - differences above Szrp are reduced   (reduced sharpening)
  172. ###
  173. ### Spwr [int]
  174. ### ----------
  175. ### Power: exponent for sharpener
  176. ###
  177. ### SdmpLo [int]
  178. ### ------------
  179. ### Damp Low: reduce sharpening for small changes [0:disable]
  180. ###
  181. ### SdmpHi [int]
  182. ### ------------
  183. ### Damp High: reduce sharpening for big changes [0:disable]
  184. ###
  185. ###
  186. ###
  187. ### +----------+
  188. ### | LIMITING |
  189. ### +----------+
  190. ###
  191. ### Lmode [int: ...,0,1,2,3,4]
  192. ### --------------------------
  193. ### Limit mode:
  194. ###    <0 : Limit with repair (ex: Lmode=-1 --> repair(1), Lmode=-5 --> repair(5)...)
  195. ###    =0 : No limit
  196. ###    =1 : Limit to over/undershoot
  197. ###    =2 : Limit to over/undershoot on edges and no limit on not-edges
  198. ###    =3 : Limit to zero on edges and to over/undershoot on not-edges
  199. ###    =4 : Limit to over/undershoot on edges and to over/undershoot2 on not-edges
  200. ###
  201. ### overshoot [int]
  202. ### ---------------
  203. ### Limit for pixels that get brighter during sharpening
  204. ###
  205. ### undershoot [int]
  206. ### ----------------
  207. ### Limit for pixels that get darker during sharpening
  208. ###
  209. ### overshoot2 [int]
  210. ### ----------------
  211. ### Same as overshoot, only for Lmode=4
  212. ###
  213. ### undershoot2 [int]
  214. ### -----------------
  215. ### Same as undershoot, only for Lmode=4
  216. ###
  217. ###
  218. ###
  219. ### +-----------------+
  220. ### | POST-PROCESSING |
  221. ### +-----------------+
  222. ###
  223. ### soft [int: -2,-1,0...100]
  224. ### -------------------------
  225. ### Soft the sharpening effect (-1 = old autocalculate, -2 = new autocalculate)
  226. ###
  227. ### soothe [bool]
  228. ### -------------
  229. ###    =true  : Enable soothe temporal stabilization
  230. ###    =false : Disable soothe temporal stabilization
  231. ###
  232. ### keep [int: 0...100]
  233. ### -------------------
  234. ### Minimum percent of the original sharpening to keep (only with soothe=true)
  235. ###
  236. ###
  237. ###
  238. ### +-------+
  239. ### | EDGES |
  240. ### +-------+
  241. ###
  242. ### edgemode [int: -1,0,1,2]
  243. ### ------------------------
  244. ###    =-1 : Show edgemask
  245. ###    = 0 : Sharpening all
  246. ###    = 1 : Sharpening only edges
  247. ###    = 2 : Sharpening only not-edges
  248. ###
  249. ### edgemaskHQ [bool]
  250. ### -----------------
  251. ###    =true  : Original edgemask
  252. ###    =false : Faster edgemask
  253. ###
  254. ###
  255. ###
  256. ### +------------+
  257. ### | UPSAMPLING |
  258. ### +------------+
  259. ###
  260. ### ss_x ; ss_y [float]
  261. ### -------------------
  262. ### Supersampling factor (reduce aliasing on edges)
  263. ###
  264. ### dest_x ; dest_y [int]
  265. ### ---------------------
  266. ### Output resolution after sharpening (avoid a resizing step)
  267. ###
  268. ###
  269. ###
  270. ### +-------+
  271. ### | DEBUG |
  272. ### +-------+
  273. ###
  274. ### show [bool]
  275. ### -----------
  276. ### Show debug clip & informations
  277. ###
  278. ### screenW ; screenH [int]
  279. ### -----------------------
  280. ### Screen resolution (for show clip)
  281. ###
  282. ###
  283. ###
  284. ### +----------+
  285. ### | SETTINGS |
  286. ### +----------+
  287. ###
  288. ### defaults [string: "old" or "slow" or "fast"]
  289. ### --------------------------------------------
  290. ###    = "old"  : Reset settings to original version (output will be THE SAME AS LSF)
  291. ###    = "slow" : Enable SLOW modded version settings
  292. ###    = "fast" : Enable FAST modded version settings
  293. ###  --> /!\ [default:"fast"]
  294. ###
  295. ###
  296. ### defaults="old" :  - strength    = 100
  297. ### ----------------  - Smode       = 3
  298. ###                   - Smethod     = Smode==3?2:1
  299. ###                   - kernel      = 11
  300. ###
  301. ###                   - preblur     = "OFF"
  302. ###                   - secure      = false
  303. ###                   - source      = undefined
  304. ###
  305. ###                   - Szrp        = 16
  306. ###                   - Spwr        = 2
  307. ###                   - SdmpLo      = strength/25
  308. ###                   - SdmpHi      = 0
  309. ###
  310. ###                   - Lmode       = 1
  311. ###                   - overshoot   = 1
  312. ###                   - undershoot  = overshoot
  313. ###                   - overshoot2  = overshoot*2
  314. ###                   - undershoot2 = overshoot2
  315. ###
  316. ###                   - soft        = 0
  317. ###                   - soothe      = false
  318. ###                   - keep        = 25
  319. ###
  320. ###                   - edgemode    = 0
  321. ###                   - edgemaskHQ  = true
  322. ###
  323. ###                   - ss_x        = Smode==3?1.50:1.25
  324. ###                   - ss_y        = ss_x
  325. ###                   - dest_x      = ox
  326. ###                   - dest_y      = oy
  327. ###
  328. ###                   - show        = false
  329. ###                   - screenW     = 1280
  330. ###                   - screenH     = 1024
  331. ###
  332. ###
  333. ### defaults="slow" : - strength    = 100
  334. ### ----------------- - Smode       = 5
  335. ###                   - Smethod     = 3
  336. ###                   - kernel      = 11
  337. ###
  338. ###                   - preblur     = "OFF"
  339. ###                   - secure      = true
  340. ###                   - source      = undefined
  341. ###
  342. ###                   - Szrp        = 16
  343. ###                   - Spwr        = 4
  344. ###                   - SdmpLo      = 4
  345. ###                   - SdmpHi      = 48
  346. ###
  347. ###                   - Lmode       = 4
  348. ###                   - overshoot   = strength/100
  349. ###                   - undershoot  = overshoot
  350. ###                   - overshoot2  = overshoot*2
  351. ###                   - undershoot2 = overshoot2
  352. ###
  353. ###                   - soft        = -2
  354. ###                   - soothe      = true
  355. ###                   - keep        = 20
  356. ###
  357. ###                   - edgemode    = 0
  358. ###                   - edgemaskHQ  = true
  359. ###
  360. ###                   - ss_x        = 1.50
  361. ###                   - ss_y        = ss_x
  362. ###                   - dest_x      = ox
  363. ###                   - dest_y      = oy
  364. ###
  365. ###                   - show        = false
  366. ###                   - screenW     = 1280
  367. ###                   - screenH     = 1024
  368. ###
  369. ###
  370. ### defaults="fast" : - strength    = 100
  371. ### ----------------- - Smode       = 3
  372. ###                   - Smethod     = 2
  373. ###                   - kernel      = 11
  374. ###
  375. ###                   - preblur     = "OFF"
  376. ###                   - secure      = true
  377. ###                   - source      = undefined
  378. ###
  379. ###                   - Szrp        = 16
  380. ###                   - Spwr        = 4
  381. ###                   - SdmpLo      = 4
  382. ###                   - SdmpHi      = 48
  383. ###
  384. ###                   - Lmode       = 1
  385. ###                   - overshoot   = strength/100
  386. ###                   - undershoot  = overshoot
  387. ###                   - overshoot2  = overshoot*2
  388. ###                   - undershoot2 = overshoot2
  389. ###
  390. ###                   - soft        = 0
  391. ###                   - soothe      = true
  392. ###                   - keep        = 20
  393. ###
  394. ###                   - edgemode    = 0
  395. ###                   - edgemaskHQ  = false
  396. ###
  397. ###                   - ss_x        = 1.25
  398. ###                   - ss_y        = ss_x
  399. ###                   - dest_x      = ox
  400. ###                   - dest_y      = oy
  401. ###
  402. ###                   - show        = false
  403. ###                   - screenW     = 1280
  404. ###                   - screenH     = 1024
  405. ###
  406. ################################################################################################
  407.  
  408. function LSFmod( clip inclip, float "strength", int "Smode", int "Smethod", int "kernel",
  409. \                string "preblur", bool "secure", clip "source",
  410. \                int "Szrp", int "Spwr", float "SdmpLo", float "SdmpHi",
  411. \                int "Lmode", int "overshoot", int "undershoot", int "overshoot2", int "undershoot2",
  412. \                int "soft", bool "soothe", int "keep",
  413. \                int "edgemode", bool "edgemaskHQ",
  414. \                float "ss_x", float "ss_y", int "dest_x", int "dest_y",
  415. \                bool "show", int "screenW", int "screenH",
  416. \                string "defaults", bool "avs26_optimize" )
  417. {
  418.  
  419. sisphbd = AvsPlusVersionNumber > 2294
  420.  
  421. ### DEFAULTS
  422. version  = "v2.185"
  423. defaults = default(defaults,"fast")
  424. num      = defaults=="old" ? 0 : defaults=="slow" ? 1 : defaults=="fast" ? 2 : 3
  425.  
  426. ox = inclip.width
  427. oy = inclip.height
  428.  
  429. Assert(num         != 3      ? true : false, chr(10) + """Defaults must be "old" or "slow" or "fast" !""" + chr(10))
  430.  
  431. strength    = default( strength,    Select(num,                100 ,                100 ,                100 ) )
  432. Smode       = default( Smode,       Select(num,                  3 ,                  5 ,                  3 ) )
  433. Smethod     = default( Smethod,     Select(num,       Smode==3?2:1 ,                  3 ,                  2 ) )
  434. kernel      = default( kernel,      Select(num,                 11 ,                 11 ,                 11 ) )
  435.  
  436. preblur     = default( preblur,     Select(num,              "OFF" ,              "OFF" ,              "OFF" ) )
  437. secure      = default( secure,      Select(num,              false ,               true ,               true ) )
  438.  
  439. Szrp        = default( Szrp,        Select(num,                 16 ,                 16 ,                 16 ) )
  440. Spwr        = default( Spwr,        Select(num,                  2 ,                  4 ,                  4 ) )
  441. SdmpLo      = default( SdmpLo,      Select(num,        strength/25 ,                  4 ,                  4 ) )
  442. SdmpHi      = default( SdmpHi,      Select(num,                  0 ,                 48 ,                 48 ) )
  443.  
  444. Lmode       = default( Lmode,       Select(num,                  1 ,                  4 ,                  1 ) )
  445. overshoot   = default( overshoot,   Select(num,                  1 ,       strength/100 ,       strength/100 ) )
  446. undershoot  = default( undershoot,  Select(num,          overshoot ,          overshoot ,          overshoot ) )
  447. overshoot2  = default( overshoot2,  Select(num,        overshoot*2 ,        overshoot*2 ,        overshoot*2 ) )
  448. undershoot2 = default( undershoot2, Select(num,         overshoot2 ,         overshoot2 ,         overshoot2 ) )
  449.  
  450. soft        = default( soft,        Select(num,                  0 ,                 -2 ,                  0 ) )
  451. soothe      = default( soothe,      Select(num,              false ,               true ,               true ) )
  452. keep        = default( keep,        Select(num,                 25 ,                 20 ,                 20 ) )
  453.  
  454. edgemode    = default( edgemode,    Select(num,                  0 ,                  0 ,                  0 ) )
  455. edgemaskHQ  = default( edgemaskHQ,  Select(num,               true ,               true ,              false ) )
  456.  
  457. ss_x        = default( ss_x,        Select(num, Smode==3?1.50:1.25 ,               1.50 ,               1.25 ) )
  458. ss_y        = default( ss_y,        Select(num,               ss_x ,               ss_x ,               ss_x ) )
  459. dest_x      = default( dest_x,      Select(num,                 ox ,                 ox ,                 ox ) )
  460. dest_y      = default( dest_y,      Select(num,                 oy ,                 oy ,                 oy ) )
  461.  
  462. show        = default( show,        Select(num,              false ,              false ,              false ) )
  463. screenW     = default( screenW,     Select(num,               1280 ,               1280 ,               1280 ) )
  464. screenH     = default( screenH,     Select(num,               1024 ,               1024 ,               1024 ) )
  465.  
  466. Assert(!isRGB(inclip), chr(10) + "LSFmod: not work with an RGB clip !" + chr(10))
  467. Assert( ( strength >= 0 )                                                ? true : false, chr(10) + "'strength' have not a correct value! [>=0]" + chr(10))
  468. Assert( ( Smode >= 1 && Smode <= 5 )                                     ? true : false, chr(10) + "'Smode' have not a correct value! [1,2,3,4,5]" + chr(10))
  469. Assert( ( Smethod >= 1 && Smethod <= 4 )                                 ? true : false, chr(10) + "'Smethod' have not a correct value! [1,2,3,4]" + chr(10))
  470. Assert( ( kernel == 19 || kernel == 20 || kernel == 11 || kernel == 12 ) ? true : false, chr(10) + "'kernel' have not a correct value! [19,20,11,12]" + chr(10))
  471. Assert( ( Szrp >= 1 && Szrp <= 255 )                                     ? true : false, chr(10) + "'Szrp' have not a correct value! [1...255]" + chr(10))
  472. Assert( ( Spwr >= 1 )                                                    ? true : false, chr(10) + "'Spwr' have not a correct value! [>=1]" + chr(10))
  473. Assert( ( SdmpLo >= 0 && SdmpLo <= 255 )                                 ? true : false, chr(10) + "'SdmpLo' have not a correct value! [0...255]" + chr(10))
  474. Assert( ( SdmpHi >= 0 && SdmpHi <= 255 )                                 ? true : false, chr(10) + "'SdmpHi' have not a correct value! [0...255]" + chr(10))
  475. Assert( ( Lmode <= 4 )                                                   ? true : false, chr(10) + "'Lmode' have not a correct value! [...-1,0,1,2,3,4]" + chr(10))
  476. Assert( ( overshoot >= 0 && overshoot <= 255 )                           ? true : false, chr(10) + "'overshoot' have not a correct value! [0...255]" + chr(10))
  477. Assert( ( undershoot >= 0 && undershoot <= 255 )                         ? true : false, chr(10) + "'undershoot' have not a correct value! [0...255]" + chr(10))
  478. Assert( ( overshoot2 >= 0 && overshoot2 <= 255 )                         ? true : false, chr(10) + "'overshoot2' have not a correct value! [0...255]" + chr(10))
  479. Assert( ( undershoot2 >= 0 && undershoot2 <= 255 )                       ? true : false, chr(10) + "'undershoot2' have not a correct value! [0...255]" + chr(10))
  480. Assert( ( soft >= -2 && soft <= 100 )                                    ? true : false, chr(10) + "'soft' have not a correct value! [-2,-1,0,1...100]" + chr(10))
  481. Assert( ( keep >= 0 && keep <= 100 )                                     ? true : false, chr(10) + "'keep' have not a correct value! [0...100]" + chr(10))
  482. Assert( ( edgemode >= -1 && edgemode <= 2 )                              ? true : false, chr(10) + "'edgemode' have not a correct value! [-1,0,1,2]" + chr(10))
  483. Assert( ( ss_x >= 1.0 )                                                  ? true : false, chr(10) + "'ss_x' have not a correct value! [>=1.0]" + chr(10))
  484. Assert( ( ss_y >= 1.0 )                                                  ? true : false, chr(10) + "'ss_y' have not a correct value! [>=1.0]" + chr(10))
  485.  
  486. soft = soft!=-1  ? soft : sqrt( (((ss_x+ss_y)/2.0-1.0)*100.0) ) * 10
  487. soft = soft!=-2  ? soft : int( (1.0+(2.0/(ss_x+ss_y))) * sqrt(strength) )
  488. soft = soft<=100 ? soft : 100
  489.  
  490. xxs  = round(ox*ss_x/8)*8
  491. yys  = round(oy*ss_y/8)*8
  492.  
  493. str  = float(strength)/100.0
  494.  
  495. sisyuy2 = inclip.isyuy2()
  496.  
  497. sisavs26 = !(VersionNumber() < 2.6)
  498.  
  499. avs26op  = default(avs26_optimize, true)
  500.  
  501. sisupscale = (dest_x != ox || dest_y != oy)
  502.  
  503. inclip = !sisupscale && sisyuy2 && sisphbd ? inclip.converttoyv16() : inclip
  504.  
  505. sislumaonly = avs26op ? sisphbd ? inclip.isy() : sisavs26 ? inclip.isy8() : true : true
  506.  
  507. input = sisupscale || sislumaonly ? inclip : sisphbd ? inclip.converttoy() : inclip.converttoy8()
  508.  
  509. sode  = defined(source)
  510.  
  511. source = sode ? sisupscale || sislumaonly ? source : sisphbd ? source.converttoy() : source.converttoy8() : source
  512.  
  513. Assert(!isYUY2(input), chr(10) + "LSFmod: not work with an YUY2 clip !" + chr(10))
  514.  
  515. ### SHARP
  516. tmp  = ss_x > 1.0 || ss_y > 1.0 ? input.spline36resize(xxs,yys) : input
  517. pre  = preblur=="OFF" ? tmp
  518. \    : preblur=="ON"  ? !sisavs26 ? tmp.mt_makediff( mt_lutxy(mt_makediff(tmp,tmp.removegrain(11,-1),U=1,V=1)
  519. \                                                   ,mt_makediff(tmp,tmp.removegrain(4,-1),U=1,V=1)
  520. \                                                   ,"x 128 - y 128 - * 0 < 128 x 128 - abs y 128 - abs < x y ? ?",U=1,V=1) ,U=1,V=1) \
  521.                   : tmp.mt_makediff( mt_lutxy(mt_makediff(tmp,tmp.removegrain(11,-1),U=1,V=1)
  522. \                                                   ,mt_makediff(tmp,tmp.removegrain(4,-1),U=1,V=1)
  523. \                                                   ,"x range_half - y range_half - * 0 < range_half x range_half - abs y range_half - abs < x y ? ?",use_expr=2,U=1,V=1) ,U=1,V=1)
  524. \    : eval("tmp."+preblur)
  525.  
  526. dark_limit   = pre.mt_inpand(U=1,V=1)
  527. bright_limit = pre.mt_expand(U=1,V=1)
  528. minmaxavg    = mt_average(dark_limit,bright_limit,U=1,V=1)
  529.  
  530. method = Smethod==1   ? pre.removegrain(kernel,-1)
  531. \      : Smethod==2   ? minmaxavg
  532. \      :                minmaxavg.removegrain(kernel,-1)
  533.  
  534. method = secure==true ? !sisavs26 ? method.mt_lutxy(pre,"x y < x 1 + x y > x 1 - x ? ?",U=1,V=1) \
  535.                   : method.mt_lutxy(pre,"x y < x 1 scalef + x y > x 1 scalef - x ? ?",use_expr=2,U=1,V=1)
  536. \      :                method
  537.  
  538. method = preblur=="OFF" ? method : tmp.mt_makediff(mt_makediff(pre,method,U=1,V=1),U=1,V=1)
  539.  
  540. normsharp = Smode==1 ? tmp.unsharpmask(strength=int(str*100),radius=round(1.0+((ss_x+ss_y)/2.0)),threshold=0)
  541. \         : Smode==2 ? tmp.unsharp(strength=str,vary=((ss_x+ss_y)/2.0),varc=1,border=3,U=1,V=1)
  542. \         : Smode==3 ? !sisavs26 ? mt_lutxy(tmp, method, yexpr="x x y - "+string(str)+" * +",U=1,V=1) \
  543.                          : mt_lutxy(tmp, method, yexpr="x x y - "+string(str)+" * +",use_expr=2,clamp_float=true,U=1,V=1)
  544. \         : Smode==4 ? !sisavs26 ? mt_lutxy(tmp, method, yexpr="x y == x x x y - abs "+string(Szrp)+" / 1 "+string(Spwr)+" / ^ "+string(Szrp)+" * "+string(str)+" * x y - 2 ^ x y - 2 ^ "
  545. \                                                                   +string(SdmpLo)+" + / * x y - x y - abs / * 1 "+string(SdmpHi)+" 0 == 0 x y - abs "+string(SdmpHi)+" / 4 ^ ? + / + ?",U=1,V=1) \
  546.                  : mt_lutxy(tmp, method, yexpr="x y == x x x y - abs "+string(Szrp) +" scalef / 1 "+string(Spwr)+" / ^ "+string(Szrp) +" scalef * "+string(str)+" * x y - 2 ^ x y - 2 ^ "
  547. \                                                                   +string(SdmpLo)+" scalef scalef + / * x y - x y - abs / * 1 "+string(SdmpHi)+" scalef 0 == 0 x y - abs "+string(SdmpHi)+" scalef / 4 ^ ? + / + ?",use_expr=2,U=1,V=1)
  548. \         :            !sisavs26 ? mt_lutxy(tmp, method, yexpr="x y == x x x y - abs "+string(Szrp)+" / 1 "+string(Spwr)+" / ^ "+string(Szrp)+" * "+string(str)+" * x y - x y - abs / * x y - 2 ^ "
  549. \                                                                   +string(Szrp)+" 2 ^ "+string(SdmpLo)+" + * x y - 2 ^ "+string(SdmpLo)+" + "+string(Szrp)+" 2 ^ * / * 1 "
  550. \                                                                   +string(SdmpHi)+" 0 == 0 "+string(Szrp)+" "+string(SdmpHi)+" / 4 ^ ? + 1 "+string(SdmpHi)+" 0 == 0 x y - abs "+string(SdmpHi)+" / 4 ^ ? + / * + ?",U=1,V=1) \
  551.                  : mt_lutxy(tmp, method, yexpr="x y == x x x y - abs "+string(Szrp)+" / 1 "+string(Spwr)+" / ^ "+string(Szrp)+" * "+string(str)+" * x y - x y - abs / * x y - 2 ^ "
  552. \                                                                   +string(Szrp)+" 2 ^ "+string(SdmpLo)+" + * x y - 2 ^ "+string(SdmpLo)+" + "+string(Szrp)+" 2 ^ * / * 1 "
  553. \                                                                   +string(SdmpHi)+" 0 == 0 "+string(Szrp)+" "+string(SdmpHi)+" / 4 ^ ? + 1 "+string(SdmpHi)+" 0 == 0 x y - abs "+string(SdmpHi)+" / 4 ^ ? + / * + ?",U=1,V=1,use_expr=2,scale_inputs="allf")
  554.  
  555.  
  556. ### LIMIT
  557. normal = mt_clamp(normsharp, bright_limit, dark_limit, overshoot,  undershoot,  U=1, V=1)
  558. second = mt_clamp(normsharp, bright_limit, dark_limit, overshoot2, undershoot2, U=1, V=1)
  559. zero   = mt_clamp(normsharp, bright_limit, dark_limit, 0,          0,           U=1, V=1)
  560.  
  561. edge  = edgemaskHQ ? mt_logic( tmp.mt_edge(thY1=0,thY2=255,"8 16 8 0 0 0 -8 -16 -8 4",U=1,V=1)
  562. \                             ,tmp.mt_edge(thY1=0,thY2=255,"8 0 -8 16 0 -16 8 0 -8 4",U=1,V=1)
  563. \                             ,"max",U=1,V=1)
  564. \     :              tmp.mt_edge(thY1=0,thY2=255,mode="min/max",U=1,V=1)
  565.  
  566. edge  = edgemaskHQ ? !sisavs26 ? edge.mt_lut("x 128 / 0.86 ^ 255 *",U=1,V=1) \
  567.                    : edge.mt_lut("x range_half / 0.86 ^ range_max *",use_expr=2,scale_inputs="floatf",U=1,V=1)
  568. \     :              !sisavs26 ? edge.mt_lut("x 32 / 0.86 ^ range_max *",U=1,V=1) \
  569.                    : edge.mt_lut("x 32 scalef / 0.86 ^ range_max *",use_expr=2,scale_inputs="floatf",U=1,V=1)
  570.  
  571. limit1 = Lmode<0  ? normsharp.repair(tmp,abs(Lmode),-1)
  572. \      : Lmode==0 ? normsharp
  573. \      : Lmode==1 ? normal
  574. \      : Lmode==2 ? mt_merge(normsharp, normal, edge.mt_inflate(U=1,V=1), U=1, V=1)
  575. \      : Lmode==3 ? mt_merge(normal,    zero,   edge.mt_inflate(U=1,V=1), U=1, V=1)
  576. \      :            mt_merge(second,    normal, edge.mt_inflate(U=1,V=1), U=1, V=1)
  577.  
  578. limit2 = edgemode==0 ? limit1
  579. \      : edgemode==1 ? mt_merge(tmp,limit1,edge.mt_inflate(U=1,V=1).mt_inflate(U=1,V=1).removegrain(11,-1),U=1,V=1)
  580. \      :               mt_merge(limit1,tmp,edge.mt_inflate(U=1,V=1).mt_inflate(U=1,V=1).removegrain(11,-1),U=1,V=1)
  581.  
  582.  
  583. ### SOFT
  584. sharpdiff = mt_makediff(tmp,limit2,U=1,V=1)
  585. sharpdiff = !sisavs26 ? mt_lutxy(sharpdiff,sharpdiff.removegrain(19,-1),
  586. \                                    "x 128 - abs y 128 - abs > y "+string(soft)+" * x "+string(100-soft)+" * + 100 / x ?",U=1,V=1) \
  587.               : mt_lutxy(sharpdiff,sharpdiff.removegrain(19,-1),
  588. \                                    "x range_half - abs y range_half - abs > y "+string(soft)+" * x "+string(100-soft)+" * + 100 / x ?",use_expr=2,clamp_float=true,U=1,V=1)
  589.  
  590. PP1 = soft==0 ? limit2 : mt_makediff(tmp,sharpdiff,U=1,V=1)
  591.  
  592.  
  593. ### SOOTHE
  594. diff  = mt_makediff(tmp,PP1,U=1,V=1)
  595. diff2 = diff.temporalsoften(1,255,0,32,2)
  596. diff3 = !sisavs26 ? mt_lutxy(diff,diff2,"x 128 - y 128 - * 0 < x 128 - 100 / "+string(keep)+
  597. \                                           " * 128 + x 128 - abs y 128 - abs > x "+string(keep)+" * y 100 "+string(keep)+" - * + 100 / x ? ?",U=1,V=1) \
  598.           : mt_lutxy(diff,diff2,"x range_half - y range_half - * 0 < x range_half - 100 / "+string(keep)+
  599. \                                           " * range_half + x range_half - abs y range_half - abs > x "+string(keep)+" * y 100 "+string(keep)+" - * + 100 / x ? ?",use_expr=2,clamp_float=true,U=1,V=1)
  600.  
  601. PP2 = soothe ? mt_makediff(tmp,diff3,U=1,V=1) : PP1
  602.  
  603.  
  604. ### OUTPUT
  605. out = sisupscale ? tmp.mergeluma(PP2).spline36resize(dest_x,dest_y)
  606. \   : (ss_x > 1.0 || ss_y > 1.0)     ? sisavs26 ? PP2.spline36resize(dest_x,dest_y) : input.mergeluma(PP2.spline36resize(dest_x,dest_y))
  607. \   :                                  sisavs26 ? PP2 : input.mergeluma(PP2)
  608.  
  609. src = !sode      ? nop()
  610. \   : sisupscale ? source.spline36resize(dest_x,dest_y)
  611. \   :                                  source
  612. in  = sisupscale ? input.spline36resize(dest_x,dest_y)
  613. \   :                                  input
  614.  
  615. shrpD  = mt_makediff(in,out,U=1,V=1)
  616. shrpL  = sode ? !sisavs26 ? shrpD.Padding(4,4,4,4).repair(mt_makediff(in,src,U=1,V=1).Padding(4,4,4,4),1,-1,-1).crop(4,4,-4,-4,true).mt_lutxy(shrpD,"x 128 - abs y 128 - abs < x y ?",U=1,V=1) \
  617.               : shrpD.Padding(4,4,4,4).repair(mt_makediff(in,src,U=1,V=1).Padding(4,4,4,4),1,-1,-1).crop(4,4,-4,-4,true).mt_lutxy(shrpD,"x range_half - abs y range_half - abs < x y ?",use_expr=1,U=1,V=1) : nop()
  618. output = sode ? mt_makediff(in,shrpL,U=2,V=2)
  619. \      :                   out
  620.  
  621. output = edgemode!=-1    ? output
  622. \      :                   sisavs26 && !sisupscale ? edge.spline36resize(dest_x,dest_y) : edge.spline36resize(dest_x,dest_y).greyscale()
  623.  
  624.  
  625. ### SHOW
  626.    function LSFM_Show(clip input, clip output, string "version", string "defaults",
  627.    \                  float "strength", int "Smode", int "Smethod", int "kernel",
  628.    \                  string "preblur", bool "secure", string "source",
  629.    \                  int "Szrp", int "Spwr", float "SdmpLo", float "SdmpHi",
  630.    \                  int "Lmode", int "overshoot", int "undershoot", int "overshoot2", int "undershoot2",
  631.    \                  int "soft", bool "soothe", int "keep",
  632.    \                  int "edgemode", bool "edgemaskHQ",
  633.    \                  float "ss_x", float "ss_y", int "dest_x", int "dest_y",
  634.    \                  bool "show", int "screenW", int "screenH")
  635.    {
  636.    resoW = screenW - 384
  637.    resoH = screenH
  638.  
  639.    inW   = input.width()
  640.    inH   = input.height()
  641.    outW  = round( resoW / 4 ) * 4
  642.    outH  = round( (outW * inH) / (4 * inW) ) * 4
  643.    scale = resoH - ( 2 * outH )
  644.  
  645.    i = input.spline36resize(outW,outH)
  646.    i = scale > 0 ? i.addborders(0,scale/4,0,scale/4,true) : i
  647.    i = scale < 0 ? i.crop(0,-scale/4,0,scale/4,true) : i
  648.  
  649.    o = output.spline36resize(outW,outH)
  650.    o = scale > 0 ? o.addborders(0,scale/4,0,scale/4,true) : o
  651.    o = scale < 0 ? o.crop(0,-scale/4,0,scale/4,true) : o
  652.  
  653.    e = blankclip(input, width=384, height=resoH)
  654.    e = e.SubTitle("LSFmod " + string(version)                ,text_color=$FFFFFF,font="COURIER NEW",size=24,x=40,y=40)
  655.    e = e.SubTitle("DEFAULTS      = " + string(defaults)      ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=100)
  656.  
  657.    e = e.SubTitle("GENERAL:"                                 ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=150)
  658.    e = e.SubTitle("--------"                                 ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=160)
  659.    e = e.SubTitle("strength      = " + string(strength)      ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=180)
  660.    e = e.SubTitle("Smode         = " + string(Smode)         ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=190)
  661.    e = e.SubTitle("Smethod       = " + string(Smethod)       ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=200)
  662.    e = e.SubTitle("kernel        = " + string(kernel)        ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=210)
  663.  
  664.    e = e.SubTitle("SPECIAL:"                                 ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=250)
  665.    e = e.SubTitle("--------"                                 ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=260)
  666.    e = e.SubTitle("preblur       = " + string(preblur)       ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=280)
  667.    e = e.SubTitle("secure        = " + string(secure)        ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=290)
  668.    e = e.SubTitle("source        = " + string(source)        ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=300)
  669.  
  670.    e = e.SubTitle("NONLINEAR SHARPENING:"                    ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=340)
  671.    e = e.SubTitle("---------------------"                    ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=350)
  672.    e = e.SubTitle("Szrp          = " + string(Szrp)          ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=370)
  673.    e = e.SubTitle("Spwr          = " + string(Spwr)          ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=380)
  674.    e = e.SubTitle("SdmpLo        = " + string(SdmpLo)        ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=390)
  675.    e = e.SubTitle("SdmpHi        = " + string(SdmpHi)        ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=400)
  676.  
  677.    e = e.SubTitle("LIMITING:"                                ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=440)
  678.    e = e.SubTitle("---------"                                ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=450)
  679.    e = e.SubTitle("Lmode         = " + string(Lmode)         ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=470)
  680.    e = e.SubTitle("overshoot     = " + string(overshoot)     ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=480)
  681.    e = e.SubTitle("undershoot    = " + string(undershoot)    ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=490)
  682.    e = e.SubTitle("overshoot2    = " + string(overshoot2)    ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=500)
  683.    e = e.SubTitle("undershoot2   = " + string(undershoot2)   ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=510)
  684.  
  685.    e = e.SubTitle("POST-PROCESSING:"                         ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=550)
  686.    e = e.SubTitle("----------------"                         ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=560)
  687.    e = e.SubTitle("soft          = " + string(soft)          ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=580)
  688.    e = e.SubTitle("soothe        = " + string(soothe)        ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=590)
  689.    e = e.SubTitle("keep          = " + string(keep)          ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=600)
  690.  
  691.    e = e.SubTitle("EDGES:"                                   ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=640)
  692.    e = e.SubTitle("------"                                   ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=650)
  693.    e = e.SubTitle("edgemode      = " + string(edgemode)      ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=670)
  694.    e = e.SubTitle("edgemaskHQ    = " + string(edgemaskHQ)    ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=680)
  695.  
  696.    e = e.SubTitle("UPSAMPLING:"                              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=720)
  697.    e = e.SubTitle("-----------"                              ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=730)
  698.    e = e.SubTitle("ss_x          = " + string(ss_x)          ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=750)
  699.    e = e.SubTitle("ss_y          = " + string(ss_y)          ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=760)
  700.    e = e.SubTitle("dest_x        = " + string(dest_x)        ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=770)
  701.    e = e.SubTitle("dest_y        = " + string(dest_y)        ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=780)
  702.  
  703.    e = e.SubTitle("DEBUG:"                                   ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=820)
  704.    e = e.SubTitle("------"                                   ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=830)
  705.    e = e.SubTitle("show          = " + string(show)          ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=850)
  706.    e = e.SubTitle("screenW       = " + string(screenW)       ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=860)
  707.    e = e.SubTitle("screenH       = " + string(screenH)       ,text_color=$FFFFFF,font="COURIER NEW",size=12,x=40,y=870)
  708.  
  709.    debug = stackhorizontal( e ,
  710.    \                        stackvertical( i.subtitle("Input",text_color=$FFFFFF,size=20,x=40,y=20) ,
  711.    \                                       o.subtitle("Output",text_color=$FFFFFF,size=20,x=40,y=20) )
  712.    \                      )
  713.  
  714.    return debug
  715.    }
  716.  
  717. output = sisupscale || sislumaonly ? output : sisphbd ? CombinePlanes(output,inclip,planes="YUV",sample_clip=inclip) : ytouv(inclip.utoy8(),inclip.vtoy8(),output)
  718.  
  719. output = show==false ? output
  720. \                    : LSFM_Show(inclip, output, version, defaults,
  721. \                                strength, Smode, Smethod, kernel,
  722. \                                preblur, secure, sode?"defined":"undefined",
  723. \                                Szrp, Spwr, SdmpLo, SdmpHi,
  724. \                                Lmode, overshoot, undershoot, overshoot2, undershoot2,
  725. \                                soft, soothe, keep,
  726. \                                edgemode, edgemaskHQ,
  727. \                                ss_x, ss_y, dest_x, dest_y,
  728. \                                show, screenW, screenH)
  729.  
  730. output = sisyuy2 ? output.converttoyuy2() : output
  731.  
  732. return output
  733. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement