Guest User

LSFmod

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