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