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