Guest User

LSFmod

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