Guest User

LSFmod

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