/
SharpenersPack.avsi
3066 lines (2463 loc) · 141 KB
/
SharpenersPack.avsi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#############################################################################
### ###
### Sharpeners Pack v4.4 (13-09-2022) ###
### ###
### Collection of high quality sharpeners for AviSynth+ ###
### ###
### ###
### bacondither: ###
### Adaptive Sharpen ###
### Dogway: ###
### UnsharpMask_HBD ###
### ex_unsharp ###
### CASP ###
### NVSharpen ###
### RSharpen ###
### Didée: ###
### ex_ContraSharpening ###
### SeeSaw ###
### SeeSawMulti ###
### FineSharpPlus ###
### NonlinUSM ###
### SlopeBend ###
### DetailSharpen ###
### MedianSharp ###
### LSFplus -mod of LaTo's mod of Didée's LSF- (separate .avsi) ###
### *.mp4 guy: ###
### SSSharpFaster ###
### SSSharp ###
### ReCon ###
### blah ###
### MedSharp ###
### feisty2: ###
### Plum ###
### DelicateSharp ###
### RegularSharp ###
### Other: ###
### LindaSharp ###
### DGSharpen2 ###
### SharpenComplex2 (uncredited) ###
### MultiSharpen2 ###
### XSharpenPlus ###
### pSharpen ###
### CASm ###
### ###
#############################################################################
###
###
### Adaptive Sharpen (2018-04-14)
###
### Copyright (c) 2015-2021, bacondither
### All rights reserved. BSD 2 license.
###
### Redistribution and use in source and binary forms, with or without
### modification, are permitted provided that the following conditions
### are met:
### 1. Redistributions of source code must retain the above copyright
### notice, this list of conditions and the following disclaimer
### in this position and unchanged.
### 2. Redistributions in binary form must reproduce the above copyright
### notice, this list of conditions and the following disclaimer in the
### documentation and/or other materials provided with the distribution.
###
### THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
### IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
### OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
### IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
### INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
### NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
### DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
### THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
### (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
### THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###
###########################################################################
###
### The shader tries to sharpen somewhat blurry edges the most, it avoids to sharpen near-flat areas and very sharp edges.
### The outer weights of the laplace matrix is variable to mitigate ringing on relative sharp edges and to provide more sharpening on wider and blurrier edges.
###
### The final stage is a soft limiter that confines overshoots based on local near min/max values.
### Light overshoots are limited more due to it's worse appearance compared to dark undershoots in most cases.
###
###########################################################################
###
###
### Adaptive_Sharpen() - v1.7 (06-01-2022)
###
### https://forum.doom9.org/showthread.php?t=182881
###
### by Dogway (Jose Linares)
###
###
### AviSynth+ port of HLSL Adaptive Sharpen shader by bacondither
### Process with all threads -Prefetch(threads)- in any bitdepth ( although 32-bit is 2% faster than 16-bit).
### Internally it is partially processed in 32-bit.
### Since this is CPU based it's 7 times slower compared to the GPU shader.
###
### The premise is similar to SlopeBend() by Didée, it reads:
### "It sharpens the picture by adjusting the slope of the gradient values inbetween the local min/max interval.
### Effectively, edges are sharpened without oversharpening (halos) (but risk to make them jaggy)."
###
### Dependencies: ExTools
###
### Example: Adaptive_Sharpen(1.0)
###
###
### Function Definition:
### (
### clip,
### float "str"=1.0 (0.3 to 2.0),
### float "slope"=0.50 (0.00 to 2.00),
### [float "H_over"=0.765 (0.01 to 255.0)],
### [float "L_over"=2.295 (0.01 to 255.0)],
### [float "H_compr_lo"=0.17 (0.00 to 1.00)],
### [float "H_compr_hi"=0.33 (0.00 to 1.00)],
### [float "L_compr_lo"=0.25 (0.00 to 1.00)],
### [float "L_compr_hi"=0.50 (0.00 to 1.00)],
### [float "scale_lim"=0.1 (0.0 to 1.0)],
### [float "scale_cs"=0.06 (0.00 to 1.00)],
### [float "dW_lothr"=76.5 (0.0 to 255.0 by 0.5)],
### [float "dW_hithr"=204.0 (0.0 to 255.0 by 0.5)],
### [float "lowthr_mxw"=0.1 (0.0 to 255.0)],
### [float "pm_p"=0.7 (0.0 to 1.0)],
### [float "a_offset"=2.0 (0.0 to 255.0 by 0.5)]
### )
###
####################################
function Adaptive_Sharpen(clip a, float "str", float "slope", float "H_over", float "L_over", float "H_compr_lo", float "H_compr_hi", float "L_compr_lo", float "L_compr_hi", float "scale_lim", float "scale_cs", \
float "dW_lothr", float "dW_hithr", float "lowthr_mxw", float "pm_p", float "a_offset", float "UV") {
rgb = isRGB(a)
isy = isy(a)
bi = BitsPerComponent(a)
bi2 = bi
fs = propNumElements (a,"_ColorRange") > 0 ? \
propGetInt (a,"_ColorRange") == 0 : rgb
curve_height = Default(str, 1.0) # Main control of sharpening strength [0.3 <-> 2.0]
## Defined values under this row are "optimal" DO NOT CHANGE IF YOU DO NOT KNOW WHAT YOU ARE DOING!
curveslope = Default(slope, 0.5 ) # Sharpening curve slope, high edge values
L_overshoot = Default(H_over, 0.765) # Max light overshoot before compression [>0.001] [0-255]
D_overshoot = Default(L_over, 2.295) # Max dark overshoot before compression [>0.001] [0-255]
L_compr_low = Default(H_compr_lo, 0.167) # Light compression, default (0.167=~6x)
L_compr_high = Default(H_compr_hi, 0.334) # Light compression, surrounded by edges (0.334=~3x)
D_compr_low = Default(L_compr_lo, 0.250) # Dark compression, default (0.250=4x)
D_compr_high = Default(L_compr_hi, 0.500) # Dark compression, surrounded by edges (0.500=2x)
scale_lim = Default(scale_lim, 0.1 ) # Abs max change before compression [>0.01]
scale_cs = Default(scale_cs, 0.056) # Compression slope above scale_lim
dW_lothr = Default(dW_lothr, 76.5 ) # Start interpolating between W1 and W2
dW_hithr = Default(dW_hithr, 204.0) # When dW is equal to W2
lowthr_mxw = Default(lowthr_mxw, 0.1 ) # Edge value for max lowthr weight [>0.01]
pm_p = Default(pm_p, 0.7 ) # Power mean p-value [>0-1.0]
a_offset = Default(a_offset, 2.0 ) # Edge channel offset, MUST BE THE SAME IN ALL PASSES [0-255]
UV = Default(UV, rgb ? 3 : 1)
si = ex_UVf(rgb, bi)
r_max1 = 1. / ex_bs(255, 8, bi, fulls=fs)
r_max = bi == 32 ? "" : Format("{r_max1} *")
f_max = bi == 32 ? "" : Format("range_max *")
a_epsb = ex_bs(2.55, 8, bi, fulls=fs, flt=true)
a_eps = ex_bs(2.55, 8, 32, fulls=fs)
a_offset = ex_bs(a_offset, 8, 32, fulls=fs)
a_off_ep = a_offset - a_eps
lthr_m = ex_bs(lowthr_mxw, 8, 32, fulls=fs)
lthr_r = (1. / (0.1 - 0.01)) # (~10 * r_max)
sbe_h = ex_bs(216.75, 8, 32)
sbe_m = ex_bs(14.28, 8, 32, fulls=fs) - a_offset * 3
sbe_l = ex_bs(7.65, 8, 32)
sd_off = ex_bs(5.1, 8, 32, fulls=fs)
L_over = ex_bs(L_overshoot, 8, 32)
D_over = ex_bs(D_overshoot, 8, 32)
dW_lothr = ex_bs(dW_lothr, 8, 32, fulls=fs)
dW_hithr = ex_bs(dW_hithr, 8, 32, fulls=fs)
dW = 1. / (dW_hithr - dW_lothr)
# Labels
Transition_to_a_concave_kernel_if_the_center_edge_val_is_above_thr = ""
Precalculated_default_squared_kernel_weights = ""
Calc_weights__Use_lower_weights_for_pixels_in_a_more_active_area_relative_to_center_pixel_area = ""
This_results_in_narrower_and_less_visible_overshoots_around_sharp_edges = ""
Calculate_the_negative_part_of_the_laplace_kernel_and_the_low_threshold_weight = ""
neg_laplace_div_weightsum___new_neg_laplace = ""
sharpen_val__Compute_sharpening_magnitude_function = ""
Calculate_local_near_min_n_max__partial_sort_order_0_1_2_22_23_24 = ""
mindist__Calculate_tanh_scale_factors = ""
Allow_for_higher_overshoot_if_the_current_edge_pixel_is_surrounded_by_similar_edge_pixels = ""
Soft_limited_antiringing_with_tanh__wpmean_to_control_compression_slope = ""
# Custom weighted DoG edge detection
edge = Format("x[0,0] A@ 2 * x[0,1] B@ x[0,-1] E@ x[1,0] D@ x[-1,0] C@ + + + + 2 * x[-1,1] J@ x[-1,-1] K@ x[1,1] L@ x[1,-1] M@ + + + + 0.0625 *
S@ x[0,2] - abs S x[0,-2] - abs S x[2,0] - abs S x[-2,0] - abs + + + 0.23 *
S J - abs S K - abs S L - abs S M - abs + + + 0.92 * +
S B - abs S E - abs S D - abs S C - abs + + + 1.15 * +
S A - abs 1.38 * +
"+r_max+" 0.65 ^ 2 S {r_max1} -2.466666681 1.442695040888963 * * * ^ 0.90 * 0.266666666 + 1 min * "+f_max+" {a_epsb} +")
cstr = ex_UVexpr( edge, UV, bi, rgb, fs, si)
edge = ex_dlut ( edge, bi, fs)
edge = isy ? Expr(a, edge , optSingleMode=false) : \
UV == 1 ? Expr(a, edge, "" , optSingleMode=false) : \
Expr(a, edge, cstr, scale_inputs=si, optSingleMode=false)
edge = edge.ConvertBits(32,fulls=true)
a = a.ConvertBits(32,fulls=fs)
bi = 32
# Similar Bound Edge (soft_if(), fast linear approx)
sbe = Format("
x[-1,1] A@ x[0,1] B@ max x[1,1] C@ x[-1,0] D@ max max x[1,0] E@ x[-1,-1] F@ max x[0,-1] G@ x[1,-1] H@ max max max
x[0,2] I@ x[-2,0] J@ max x[2,0] K@ x[0,-2] L@ max max x[0,0] X@ max max {sbe_l} + 1 swap / M^
x[0,3] I B {sbe_m} + + + M * {sbe_h} -
x[0,-3] G L {sbe_m} + + + M * {sbe_h} - *
x[-3,0] J D {sbe_m} + + + M * {sbe_h} -
x[3,0] E K {sbe_m} + + + M * {sbe_h} - * +
x[-1,2] x[-2,1] A {sbe_m} + + + M * {sbe_h} -
x[2,-1] x[1,-2] H {sbe_m} + + + M * {sbe_h} - * +
x[1,2] x[2,1] C {sbe_m} + + + M * {sbe_h} -
x[-2,-1] x[-1,-2] F {sbe_m} + + + M * {sbe_h} - * +
2 - 0.909090909 * 0 1 clip dup dup * swap 2 * 3 swap - * ")
cstr = ex_UVexpr( sbe, UV, 32, rgb, fs, si)
sbe = ex_dlut ( sbe, 32, fs)
sbe = isy ? Expr(edge, sbe , optSingleMode=false) : \
UV == 1 ? Expr(edge, sbe, "" , optSingleMode=false) : \
Expr(edge, sbe, cstr, scale_inputs=si, optSingleMode=false)
# Sharp difference
sharpdiff = Format("
x[0,0] dup dup dup dup dup dup dup
x[0,1] B@ - abs
swap7 x[-1,0] C@ - abs
swap6 x[1,0] D@ - abs
swap5 x[0,-1] E@ - abs + + +
swap4 x[-1,1] J@ - abs
swap3 x[1,1] L@ - abs
swap2 x[-1,-1] K@ - abs
swap x[1,-1] M@ - abs + + + 0.25 * + 3 * {sd_off} + A^
"+Transition_to_a_concave_kernel_if_the_center_edge_val_is_above_thr+"
y {a_offset} {dW_lothr} + - {dW} * 0 1 clip
"+Precalculated_default_squared_kernel_weights+"
dup dup * swap 2 * 3 swap - * Z^
Z dup 1 swap - + dup * Y^
"+Calc_weights__Use_lower_weights_for_pixels_in_a_more_active_area_relative_to_center_pixel_area+"
"+This_results_in_narrower_and_less_visible_overshoots_around_sharp_edges+"
J dup dup dup dup dup
x[-1,2] - abs
swap5 x[-2,1] N@ - abs
swap4 B - abs
swap3 C - abs + + +
swap2 x[0,2] F@ - abs T@
swap x[-2,0] I@ - abs W@ + 0.5 * + A swap / Y min C0^
L dup dup dup dup dup
x[1,2] O@ - abs
swap5 x[2,1] P@ - abs
swap4 B - abs
swap3 D - abs + + +
swap2 F - abs U@
swap x[2,0] H@ - abs V@ + 0.5 * + A swap / Y min C2^
K dup dup dup dup dup
x[-1,-2] - abs
swap5 x[2,-1] Q@ - abs
swap4 E - abs
swap3 C - abs + + +
swap2 x[0,-2] G@ - abs JJ@
swap I - abs LL@ + 0.5 * + A swap / Y min C5^
M dup dup dup dup dup
x[1,-2] R@ - abs
swap5 Q - abs
swap4 E - abs
swap3 D - abs + + +
swap2 G - abs KK@
swap H - abs MM@ + 0.5 * + A swap / Y min C7^
Z dup 0.54772255751 * swap 1 swap - 1.41421356237 * + dup * ZZ^
F dup dup dup O - abs
swap3 N - abs
swap2 B - abs
swap x[0,3] - abs + + +
T
U + 0.5 * + A swap / ZZ min C8^
I dup dup dup
x[-2,-1] S@ - abs
swap3 N - abs
swap2 C - abs
swap x[-3,0] - abs + + +
W
LL + 0.5 * + A swap / ZZ min C10^
G dup dup dup R - abs
swap3 S - abs
swap2 E - abs
swap x[0,-3] - abs + + +
JJ
KK + 0.5 * + A swap / ZZ min C9^
H dup dup dup Q - abs
swap3 P - abs
swap2 D - abs
swap x[3,0] - abs + + +
V
MM + 0.5 * + A swap / ZZ min C11^
C8 C9 0.25 * + C0 max 0.25 max C0 + 0.5 * C0^
C8 C10 0.25 * + C2 max 0.25 max C2 + 0.5 * C2^
C9 C11 0.25 * C11q@ + C5 max 0.25 max C5 + 0.5 * C5^
C10 C11q + C7 max 0.25 max C7 + 0.5 * C7^
Z dup 0.86602540378 * swap 1 swap - 0.5 * + dup * X^
"+Calculate_the_negative_part_of_the_laplace_kernel_and_the_low_threshold_weight+"
y[-1,1] C0 * WA@ a[-1,1] *
y[0,1] X * WB@ a[0,1] * +
y[1,1] C2 * WC@ a[1,1] * +
y[-1,0] X * WD@ a[-1,0] * +
y[1,0] X * WE@ a[1,0] * +
y[-1,-1] C5 * WF@ a[-1,-1] * +
y[0,-1] X * WG@ a[0,-1] * +
y[1,-1] C7 * WH@ a[1,-1] * +
y[0,2] C8 * WI@ a[0,2] * +
y[-2,0] C9 * WJ@ a[-2,0] * +
y[2,0] C10 * WK@ a[2,0] * +
y[0,-2] C11 * WL@ a[0,-2] * +
"+neg_laplace_div_weightsum___new_neg_laplace+"
WA WB WC WD WE WF WG WH WI WJ WK WL + + + + + + + + + + + / abs 0.416666666 ^ 0.06 -")
eexp = ex_lut(edge,Format("x {a_offset} - {lthr_r} * 1 min dup dup 1.98 * 2.97 swap - * * 0.01 +"), UV=uv)
a2 = ex_lut(a, "x 0.06 + 2.4 ^", UV=uv)
cstr = ex_UVexpr( sharpdiff, UV, bi, rgb, fs, si)
sharpdiff = isy ? Expr(a, eexp, edge, a2, sharpdiff , optSingleMode=false) : \
UV == 1 ? Expr(a, eexp, edge, a2, sharpdiff, "" , optSingleMode=false) : \
Expr(a, eexp, edge, a2, sharpdiff, cstr, scale_inputs=si, optSingleMode=false)
# Mean blur of eexp with 'circle' kernel
eexp = ex_luts(eexp, mode="avg", pixels=ex_shape(2,mode="circle",center=false), UV=uv)
sharpen_val = Format("
"+sharpen_val__Compute_sharpening_magnitude_function+"
z {a_offset} - abs 3.5 ^ {curveslope} {curve_height} * * 0.625 + {curve_height} swap /
y * 0.01 + x a - * ")
cstr = ex_UVexpr( sharpen_val, UV, bi, rgb, fs, si)
sharpdiff = isy ? Expr(a, eexp, edge, sharpdiff, sharpen_val , optSingleMode=false) : \
UV == 1 ? Expr(a, eexp, edge, sharpdiff, sharpen_val, "" , optSingleMode=false) : \
Expr(a, eexp, edge, sharpdiff, sharpen_val, cstr, scale_inputs=si, optSingleMode=false)
# Soft limiter. Anti-ringing block
sharp = Format("
"+Calculate_local_near_min_n_max__partial_sort_order_0_1_2_22_23_24+"
x[0,3] x[-1,2] dup1 dup1 min W^ max Y^
x[0,2] x[1,2] dup1 dup1 min Q^ max X^
x[-2,1] x[-1,1] dup1 dup1 min G^ max V^
x[0,1] x[1,1] dup1 dup1 min H^ max U^
x[2,1] x[-3,0] dup1 dup1 min E^ max T^
x[-2,0] x[-1,0] dup1 dup1 min F^ max S^
x[0,0] x[1,0] dup1 dup1 min P^ max R^
x[2,0] x[3,0] dup1 dup1 min N^ max O^
x[-2,-1] x[-1,-1] dup1 dup1 min L^ max M^
x[0,-1] x[1,-1] dup1 dup1 min I^ max K^
x[2,-1] x[-1,-2] dup1 dup1 min C^ max J^
x[0,-2] x[1,-2] dup1 dup1 min B^ max D^
V Y dup1 dup1 min V^ max Y^
J X dup1 dup1 min J^ max X^
G W dup1 dup1 min G^ max W^
M U dup1 dup1 min M^ max U^
D T dup1 dup1 min D^ max T^
O S dup1 dup1 min O^ max S^
K R dup1 dup1 min K^ max R^
C Q dup1 dup1 min C^ max Q^
I P dup1 dup1 min I^ max P^
F N dup1 dup1 min F^ max N^
H L dup1 dup1 min H^ max L^
B E dup1 dup1 min B^ max E^
U Y dup1 dup1 min U^ max Y^
R X dup1 dup1 min R^ max X^
L W dup1 dup1 min L^ max W^
M V dup1 dup1 min M^ max V^
S T dup1 dup1 min S^ max T^
K Q dup1 dup1 min K^ max Q^
J P dup1 dup1 min J^ max P^
D O dup1 dup1 min D^ max O^
E N dup1 dup1 min E^ max N^
C I dup1 dup1 min C^ max I^
G H dup1 dup1 min G^ max H^
B F dup1 dup1 min B^ max F^
T Y dup1 dup1 min T^ max Y^
N W dup1 dup1 min N^ max W^
S V dup1 dup1 min S^ max V^
O U dup1 dup1 min O^ max U^
I R dup1 dup1 min I^ max R^
P Q dup1 dup1 min P^ max Q^
D M min D^
F L dup1 dup1 min F^ max L^
J K dup1 dup1 min J^ max K^
E H dup1 dup1 min E^ max H^
B G dup1 dup1 min B^ max G^
R W dup1 dup1 min R^ max W^
P S max S^
N Q max Q^
K x[0,-3] dup1 dup1 min A^ max K^
D G dup1 dup1 min D^ max G^
Q V max V^
O R min O^
K L max L^
D J min D^
E G dup1 dup1 min E^ max G^
A C dup1 dup1 min A^ max C^
L U dup1 dup1 min L^ max U^
I O min I^
A G min A^
C F min C^
U X dup1 dup1 min U^ max X^
H L max L^
E I min E^
A B dup1 dup1 min A^ max B^
X Y dup1 dup1 min X^ max Y^
T U max U^
L S max S^
D E dup1 dup1 min D^ max E^
B C dup1 dup1 min B^ max C^
S W max W^
U V dup1 dup1 min U^ max V^
C E min C^
B D dup1 dup1 min B^ max D^
W X dup1 dup1 min W^ max X^
C D min C^
V X dup1 dup1 min V^ max X^
V U W max max W^
W X 2 * + x 3 * X3@ max Y + 0.25 * M^
C B 2 * + X3 min A + 0.25 * N^
"+mindist__Calculate_tanh_scale_factors+"
M x - abs x N - abs min D^
1.0001 D - x - {L_over} min D + P^
0.0001 D - x + {D_over} min D + N^
1 {scale_cs} - {scale_lim} * P {scale_cs} * + P min P^
1 {scale_cs} - {scale_lim} * N {scale_cs} * + N min N^
"+Allow_for_higher_overshoot_if_the_current_edge_pixel_is_surrounded_by_similar_edge_pixels+"
{L_compr_high} {L_compr_low} - z * {L_compr_low} + CX@ y 0 max Y@ dup {pm_p} ^ swap swap2 *
"+Soft_limited_antiringing_with_tanh__wpmean_to_control_compression_slope+"
swap P 24 * min 2 * P / "+expT(3)+" dup 1 - swap 1 + / P *
abs {pm_p} ^ 1 CX - abs * + 1 {pm_p} / ^
{D_compr_high} {D_compr_low} - z * {D_compr_low} + CY@ Y 0 min dup {pm_p} ^ swap swap2 *
swap N 24 * min 2 * N / "+expT(3)+" dup 1 - swap 1 + / N *
abs {pm_p} ^ 1 CY - abs * + 1 {pm_p} / ^ -
x + ")
cstr = ex_UVexpr( sharp, UV, bi, rgb, fs, si)
isy ? Expr(a, sharpdiff, sbe, sharp , optSingleMode=false) : \
UV == 1 ? Expr(a, sharpdiff, sbe, sharp, "" , optSingleMode=false) : \
Expr(a, sharpdiff, sbe, sharp, cstr, scale_inputs=si, optSingleMode=false)
bi2 != 32 ? ConvertBits(bi2, dither=1, fulls=fs) : last }
#######################
### ###
### Dogway's ###
### ###
#######################
######################################################
###
### ex_unsharp() by Dogway (15-03-2022)
###
### Replacement and practical match to variableblur's unsharp()...
### ...but with 20% of sharpness weight from a discreet lower frequency cutoff for structural support.
### Replaces WarpSharp's unsharpmask, UnsharpHQ, UnsharpMask_avsi and VariableBlur's Unsharp.
###
### The Fc argument allows to sharpen on a subpixel or macropixel basis.
### Good values span from 1920 for a 1080p clip, to width()*1.5 or *2.0.
###
### Benchmark:
### 100% UnsharpMask_HBD(128*n,1,0) (344 fps)
### 91% ex_unsharp(1, Fc=width())
### 8% unsharp(vary=1, varc=1, strength=1) # no HBD support, % scaled down
###
### Equivalences:
### unsharpmask(strength=str,radius=rad,threshold=0) --> ex_unsharp(str=str*0.007, Fc=width()/max(1,0.76*exp(0.26*rad),th=0)
### unsharp (strength=str,vary=rad) --> ex_unsharp(str=str/(1.489*pow(rad,-1.112)+0.51), Fc=width()*0.8, th=0)
### Internal's Sharpen(0.75) roughly matches ex_unsharp(2.5,width()*1.5,0,UV=3)
###
###
### Dependencies:
### ExTools
### ResizersPack
###
### Function Definition:
### (
### clip,
### float str=1.0 (-1.0 to 2.0),
### [float strv=1.0 (-1.0 to 2.0)],
### float "Fc"=1920.0 (0.0 to 3840.0 by 1),
### float "th"=0.0 (0.0 to 255.0 by 1),
### bool "safe"=false
### )
###
function ex_unsharp(clip a, float "str", float "strv", float "Fc", float "th", bool "safe", int "Y", int "UV") {
rgb = isRGB(a)
isy = isy(a)
bi = BitsPerComponent(a)
fulls = propNumElements (a,"_ColorRange") > 0 ? \
propGetInt (a,"_ColorRange") == 0 : rgb
w = a.width()
h = a.height()
Fs = max(w,h)*2.
strh = Default(str, 1) # Use negative values for frequency based blurring (deconvolution), works up to a certain extent (up to -0.5)
strv = Default(strv, strh)
Fc = Default(Fc, Fs/2.) # Frequency cutoff in Hz for sharpening, by default same to sample rate (ie 1920Hz for 1080p, or 1 pixel accuracy)
th = Default(th, 0) # Soft threshold. Good values are between str*10 and str*20
sf = Default(safe,false) # Enable for halo protection (idea from CTools' CSharpen)
Y = Default( Y, 3)
UV = Default(UV,rgb ? 3 : 1)
Fc = (Fs/Fc)
rad = ceil(Fc)-1
strv = sf ? strv*0.5 + strv : strv
strh = sf ? strh*0.5 + strh : strh
dcnv1 = strv < 0
dcnv2 = strh < 0
strv = abs(strv)
strh = abs(strh)
fs = fulls
krnlsz = ceil(2 * max(rad,1))
krnlrd = krnlsz/2
krnh = "" krnv = "" plsh = "" plsv = "" divx=0 divy=0
for (px = -krnlrd, krnlrd, 1) {
krnh = Format(krnh + "x[{px},0] ")
plsh = px == -krnlrd ? plsh : plsh + "+ "
divx = divx + 1
}
for (py = -krnlrd, krnlrd, 1) {
krnv = Format(krnv + "x[0,{py}] ")
plsv = py == -krnlrd ? plsv : plsv + "+ "
divy = divy + 1
}
krnlsz2 = ceil(2 * (max(rad,1) + 1))
krnlrd2 = krnlsz2/2
krnh2 = "" krnv2 = "" plsh2 = "" plsv2 = "" divx2=0 divy2=0
for (px2 = -krnlrd2, krnlrd2, 1) {
krnh2 = abs(px2) <= krnlrd ? krnh2 : Format(krnh2 + "x[{px2},0] ")
plsh2 = abs(px2) <= krnlrd || px2 == -krnlrd2 ? plsh2 : plsh2 + "+ "
divx2 = divx2 + 1
}
for (py2 = -krnlrd2, krnlrd2, 1) {
krnv2 = abs(py2) <= krnlrd ? krnv2 : Format(krnv2 + "x[0,{py2}] ")
plsv2 = abs(py2) <= krnlrd || py2 == -krnlrd2 ? plsv2 : plsv2 + "+ "
divy2 = divy2 + 1
}
# Soft threshold
bias = min(2*cos(pi+2*pi*th/255.)+2,1)*10 # Minimize soft threshold spread on extremes
thr1 = ex_bs( max(th-20 - bias, 0), 8, bi, fulls=true, flt=true)
thr2 = ex_bs( min(th+5 + bias, 255), 8, bi, fulls=true, flt=true)
mx = ex_bs( 255, 8, bi, fulls=true)
thrd = mx / (thr1 - thr2)
mx = bi > 14 ? "range_max /" : string(1. / Eval(ex_dlut("range_max", bi, true))) + " *"
thsv = Format("dup x + swap abs {thr1} - {thrd} * neg range_max min range_max swap - "+mx+" swap dup x - swap swap2 * - ")
thsh = Format("dup x + swap abs {thr1} - {thrd} * neg range_max min range_max swap - "+mx+" swap dup x - swap swap2 * - ")
# Safe
scl = BitSal(1,bi-8)
sclf = bi == 8 ? "" : (bi == 32 ? "0.003921568" : string( scl)) + " * "
sclr = bi == 8 ? "" : (bi == 32 ? "255" : string(1. / scl)) + " * "
sfs = !sf ? "" : sclr + " dup abs dup "+atanT()+" swap sqrt * swap sgn * " + sclf
# Deconvolution
dcnv1 = dcnv1 ? "" : "swap"
dcnv2 = dcnv2 ? "" : "swap"
strd = strh != strv
th1 = !strd ? "" : "x "+dcnv1+" - {strv} * " + sfs + (th > 0 ? thsv : " x +")
th2 = !strd ? "y "+dcnv2+" - {strh} * " + sfs + (th > 0 ? ReplaceStr(thsh, " x ", " y ") : " y +") : \
"x "+dcnv2+" - {strh} * " + sfs + (th > 0 ? thsh : " x +")
strv1 = krnv + plsv + " Z@ " + string((1./divy) *0.8) + " * "
strh1 = krnh + plsh + " Z@ " + string((1./divx) *0.8) + " * "
strv2 = krnv2 + " Z + " + plsv2 + string((1./divy2)*0.2) + " * + "
strh2 = krnh2 + " Z + " + plsh2 + string((1./divx2)*0.2) + " * + "
strvf = strv1 + strv2 + Format(th1)
strhf = strh1 + strh2 + Format(th2)
a = Fc < 2.0 ? a.blackmanresize(nmod(w*(2./Fc),2),nmod(h*(2./Fc),2),taps=6) : a # replace with butterworth fractional rad (n=300 or so) for subpixel sharpening
ystrv = ex_Yexpr (strvf, Y, bi, rgb, true)
ystrh = ex_Yexpr (strhf, Y, bi, rgb, true)
strv==0 ? a : \
isy ? Expr(a, ystrv ) : \
UV == 1 ? Expr(a, ystrv, "" ) : \
Expr(a, ystrv, ex_UVexpr(strvf, UV, bi, rgb, !(UV!=3 && !fs)), scale_inputs="none")
strd ? \
strh==0 ? last : \
isy ? Expr(last, ystrh ) : \
UV == 1 ? Expr(last, ystrh, "" ) : \
Expr(last, ystrh, ex_UVexpr(strhf, UV, bi, rgb, !(UV!=3 && !fs)), scale_inputs="none") : \
isy ? Expr(last, a, ystrh ) : \
UV == 1 ? Expr(last, a, ystrh, "" ) : \
Expr(last, a, ystrh, ex_UVexpr(strhf, UV, bi, rgb, !(UV!=3 && !fs)), scale_inputs="none")
Fc < 2.0 ? bicubicresize(w,h,-0.5,0.25) : last }
# Port and optimization of UnsharpMask_avsi.avsi by real.finder (84% faster with rad<3)
function UnsharpMask_HBD(clip clp, float "str", int "rad", float "thres", int "UV") {
bi = BitsPerComponent(clp)
rgb = isRGB(clp)
str = Default(str, 128)
rd = Default(rad, 1)
thres = Default(thres, 0)
UV = Default(UV, rgb ? 3 : 1)
rd = max(rd - 1, 1)
str = str/128.
thres = ex_bs(thres, 8, bi, fulls=rgb, flt=true)
blurclip = rd == 1 ? clp.removegrain(20, UV == 3 ? 20 : -1) : \
clp.ex_boxblur(rd, mode="mean", UV=UV)
ex_lutxy(clp, blurclip, Format(thres > 0 ? "x y - A@ abs {thres} > A {str} * x + x ?" : "x dup y - {str} * +"), UV=UV) }
/*
// LICENSE
// =======
// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved.
// -------
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
// -------
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
// Software.
// -------
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
*/
#
# CAS+ - by Dogway (19-10-2021)
#
# AviSynth+ port of AMD's "CAS" (Contrast Adaptive Sharpening) sharpener: https://gpuopen.com/fidelityfx-cas/
# Performs 10% slower with Prefetch(6) than Asd-g's AVS+ port of VapourSynth plugin.
#
function CASP(clip a, float "strength", int "UV") {
rgb = a.IsRGB()
bi = BitsPerComponent(a)
str = Default(strength, 0.5) # 0: no sharpening, to 1: full sharpening. Scaled by the sharpness knob while being transformed to a negative lobe (values from -1/5 to -1/8 for A=1)
UV = Default(UV, rgb ? 3 : 1)
str = clamp(str, 0, 1)
str = 1. / (8+str*(5-8)) # reciprocal of lerp
do = 2. * ex_bs(255, 8, bi, fulls=true)
ex_lut(a, Format("x[-1,0] D@ x[0,0] E@ max x[1,0] F@ max x[0,-1] B@ max x[0,1] H@ max dup x[-1,-1] A@ max x[1,-1] C@ max x[-1,1] G@ max x[1,1] I@ max + M^
D E min F min B min H min dup A min C min G min I min +
{do} M - min M / sqrt -{str} * dup B D F H + + + * E + swap 4 * 1 + /"), UV=UV) }
/*
// The MIT License(MIT)
//
// Copyright(c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files(the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#
# NVSharpen - by Dogway (11-12-2021)
#
# AviSynth+ port of NVidias's 'NIS Sharpen' sharpener: https://github.com/NVIDIAGameWorks/NVIDIAImageScaling/tree/main/NIS
#
function NVSharpen(clip a, float "str", float "th", float "rat", float "boost", float "start", float "end", int "UV") {
isy = isy(a)
bi = BitsPerComponent(a)
str = Default(str, 0.5) # 0: no sharpening, to 1: full sharpening.
UV = Default(UV, 1)
sharpness = clamp(str, 0, 1)
sharpen_slider = sharpness - 0.5 # Map 0 to 1 to -0.5 to +0.5
# Different range for 0 to 50% vs 50% to 100%
# The idea is to make sure sharpness of 0% map to no-sharpening,
# while also ensuring that sharpness of 100% doesn't cause too much over-sharpening.
MaxScale = (sharpen_slider >= 0.0) ? 1.25 : 1.75 # only used for HDR
MinScale = (sharpen_slider >= 0.0) ? 1.25 : 1.0
LimitScale = (sharpen_slider >= 0.0) ? 1.25 : 1.0
kDetectRatio = Default(rat, 1127 / 1024.) # 1.0 - 2.0 (Default: 1.1)
# Params for SDR
kDetectThres = Default(th, 16.) # 0.0 - 255.0 (Default: 16.)
kDetectThres = ex_bs(kDetectThres, 8, 32, true)
kMinContrastRatio = 2
kMaxContrastRatio = 10
kContrastBoost = Default(boost, 1.0)
kRatioNorm = 1.0 / (kMaxContrastRatio - kMinContrastRatio)
kSharpStartY = Default(start, 0.45)
kSharpEndY = Default(end, 0.9)
kSharpStrengthMin = max(0.0, 0.4 + sharpen_slider * MinScale * 1.2)
kSharpStrengthMax = 1.6 + sharpen_slider * 1.8
kSharpLimitMin = max(0.1, 0.14 + sharpen_slider * LimitScale * 0.32)
kSharpLimitMax = 0.5 + sharpen_slider * LimitScale * 0.6
kSharpLimitScale = kSharpLimitMax - kSharpLimitMin
kSharpStrengthScale = kSharpStrengthMax - kSharpStrengthMin
kSharpScaleY = 1.0 / (kSharpEndY - kSharpStartY)
KEps = 1 # 0.001 for HDR
eps = KEps*1/255. # 0.004 for SDR, but I see that 0.1 works better though
con = kMinContrastRatio
# Labels
Limiting1 = "" Unsharp_mul_Limiting = "" Unsharp_mul_Gradient = ""
Limiting2 = "" Gradient = ""
str = Format("f32
x[-1,-1] A^ x[0,-1] B^ x[1,-1] C^
x[-1,0] D^ x[0,0] X^ x[1,0] F^
x[-1,1] G^ x[0,1] H^ x[1,1] I^
"+Limiting2+"
x[-2,0] J@ D max X max J D min X min -
X F max x[2,0] K@ max X F min K min -
dup1 dup1 max swap2 min {eps} + / {con} {kRatioNorm} * - 0 1 clip 1 swap - {kContrastBoost} * L1^
x[0,-2] J@ B max X max J B min X min -
X H max x[0,2] K@ max X H min K min -
dup1 dup1 max swap2 min {eps} + / {con} {kRatioNorm} * - 0 1 clip 1 swap - {kContrastBoost} * L2^
A D B + 0.5 * DB@ max X max A DB min X min -
X F H + 0.5 * FH@ max I max X FH min I min -
dup1 dup1 max swap2 min {eps} + / {con} {kRatioNorm} * - 0 1 clip 1 swap - {kContrastBoost} * L3^
C B F + 0.5 * BF@ max X max C BF min X min -
X D H + 0.5 * DH@ max G max X DH min G min -
dup1 dup1 max swap2 min {eps} + / {con} {kRatioNorm} * - 0 1 clip 1 swap - {kContrastBoost} * L4^
"+Limiting1+"
1 {kSharpStartY} x - {kSharpScaleY} * 0 1 clip - dup
{kSharpStrengthScale} * {kSharpStrengthMin} + STR^
{kSharpLimitScale} * {kSharpLimitMin} + x * LIM^
"+Unsharp_mul_Limiting+"
X 1.2 * X@ D F + 0.6 * - STR * LIM neg LIM clip L1 * USM1^
X B H + 0.6 * - STR * LIM neg LIM clip L2 * USM2^
X I BF DH + 0.5 * S@ + 0.6 * - STR * LIM neg LIM clip L3 * USM3^
X G S + 0.6 * - STR * LIM neg LIM clip L4 * USM4^
"+Gradient+"
A D G + + C - F - I - abs W@
A B C + + G - H - I - abs dup1 dup1 max M^ min N^
A B D + + F - H - I - abs Y@
B C F + + D - G - H - abs dup1 dup1 max D^ min E^
M D + MD@ 0 == Q@
0 M MD / 1 min ? e_0_90^
Q 0 1 e_0_90 - ? e_45_135^
D E {kDetectRatio} * > D {kDetectThres} > & D N > & Q^
D Y == T@ Q 1 0 ? 0 ? T Q 0 1 ? 0 ? e_135^ e_45^
M N {kDetectRatio} * > M {kDetectThres} > & M E > & Q^
M W == T@ Q 1 0 ? 0 ? T Q 0 1 ? 0 ? e_90@ swap e_0@
"+Unsharp_mul_Gradient+"
e_45 e_135 + + + S@ 1 >= T2^
S 2 >= Q^
e_0 1 == T@ Q e_0_90 T2 e_0 0 ? T0@ ? Q 0 T0 ? ? USM1 *
T Q 0 T2 e_90 0 ? T9@ ? Q e_0_90 T9 ? ? USM2 * +
e_45 1 == T@ Q e_45_135 T2 e_45 0 ? T4@ ? Q 0 T4 ? ? USM3 * +
T Q 0 T2 e_135 0 ? T3@ ? Q e_45_135 T3 ? ? USM4 * + x +")
ex_lut(a, str, UV=uv, fulls=true, scale_inputs="int") }
# Port of RemoveGrain's RSharpen function (WIP; only mode=25 is implemented so far)
#
# Modes from 10 to 15 and 19 and 20 are noop
# Mode 1 followed by modes 5, 2 are the most aggressive modes as far as sharpening is concerned.
# Modes 4,6,7,8,9,18,21,22 preserve thin lines in RSharpen.
# Modes 6,7,8,18,21,22 preserve also thin lines in DenoiseSharpen.
# Mode 6, followed by modes 7, 8, are the most aggressive line preserving modes
function RSharpen(clip clp, float "str", int "mode", float "strUV", int "modeUV", int "UV") {
bi = BitsPerComponent(clp)
rgb = isRGB(clp)
str = max(1,Default(str, 1)) # From 1 to inf
struv = max(1,Default(strUV, str))
md = Default(mode, 2)
mduv = Default(modeUV, md)
UV = Default(UV, rgb ? 3 : 1)
Assert(!(9 < md < 16) || !(9 < mduv < 16), "RSharpen: invalid RSharpen mode")
Assert(!(18 < md < 21) || !(18 < mduv < 21), "RSharpen: invalid RSharpen mode")
m25 = "x[-1,-1] A^ x[0,-1] B^ x[1,-1] C^
x[-1,0] D^ x[0,0] E^ x[1,0] F^
x[-1,1] G^ x[0,1] H^ x[1,1] I^
E D < Z@ range_max D E == W@ 0 E D - ? ? SSE4^ Z W 0 D E - ? range_max ? SSE5^
E F < Z@ range_max F E == W@ 0 E F - ? ? SSE4 min SSE4^ Z W 0 F E - ? range_max ? SSE5 min SSE5^
E A < Z@ range_max A E == W@ 0 E A - ? ? SSE4 min SSE4^ Z W 0 A E - ? range_max ? SSE5 min SSE5^
E B < Z@ range_max B E == W@ 0 E B - ? ? SSE4 min SSE4^ Z W 0 B E - ? range_max ? SSE5 min SSE5^
E C < Z@ range_max C E == W@ 0 E C - ? ? SSE4 min SSE4^ Z W 0 C E - ? range_max ? SSE5 min SSE5^
E G < Z@ range_max G E == W@ 0 E G - ? ? SSE4 min SSE4^ Z W 0 G E - ? range_max ? SSE5 min SSE5^
E H < Z@ range_max H E == W@ 0 E H - ? ? SSE4 min SSE4^ Z W 0 H E - ? range_max ? SSE5 min SSE5^
E I < Z@ range_max I E == W@ 0 E I - ? ? SSE4 min SSE4^ Z W 0 I E - ? range_max ? SSE5 min SSE5^
x SSE4 SSE5 - 0 max SSE5 0.5 * min range_max min +
SSE5 SSE4 - 0 max SSE4 0.5 * min 0 max -"
ex_lut(clp, m25, UV=UV) }
#######################
### ###
### DIDÉE's ###
### ###
#######################
######################################################
###
### Contra() (15 Dec 2007) by Didée
###
### https://forum.doom9.org/showthread.php?p=1076276#post1076276
### https://forum.doom9.org/showthread.php?p=1076491#post1076491
###
### ex_ContraSharpening() v3.5 (12 Dec 2021)
### - Mod and port to ExTools by Dogway
###
###
### Contra(): sharpen the denoised clip, but don't add more to any pixel than what was removed previously.
### Enable 'MC' (default false) for temporal limiting sharpening versus the default spatial limiting. (Requires motion vectors globals)
###
### In final version 2.0d ContraHD() was merged, to allow proper HD sharpening.
### When using 'MC' (temporal limiting) global variables of (before denoising), source MSuper and forward and
### backward compensated motion vectors are necessary as: Super, cb1 and cf1, if used as a standalone function.
### Don't know who made (mod) it, so I can't give proper credits, sorry.
function ex_ContraSharpening(clip denoised, clip original, bool "HD", bool "MC", int "overshoot") {
rgb = isRGB(denoised)
bi = BitsPerComponent(denoised)
fs = propNumElements (denoised,"_ColorRange") > 0 ? \
propGetInt (denoised,"_ColorRange") == 0 : rgb
HD = Default(HD, false)
MC = Default(MC, false) # Enable to switch from spatial to temporal limiting (output a bit sharper than original though)
os = Default(overshoot, 0) # Raise to allow more sharpening (over/undershoots) when temporal limiting (MC=true). Also required if 'original' is too noisy
os = ex_bs(os, 8, bi, fulls=fs)
isg = MC ? VarExist("cb1") && VarExist("cf1") : false # if MC=true; reuse motion vector frames if created (in SMDegrain or elsewhere with same global var name)
isc = MC ? VarExist("b1c") && VarExist("f1c") : false # if MC=true; reuse compensated frames if created (in SMDegrain or elsewhere with same global var name)
s = denoised.ex_MinBlur(HD?2:1,UV=1) # Damp down remaining spots of the denoised clip.
ssD = ex_makediff(s,HD?s.removegrain(12,-1).\
removegrain(20,-1):\
s.removegrain(12,-1),UV=1,fulls=fs) # The difference of a simple kernel blur.
if (!(MC && (isg || isc))) {
allD = ex_makediff(original,denoised,UV=1,fulls=fs) # The difference achieved by the denoising.
ssDD = ssD.repair(HD?ssD.repair(allD,1,-1):allD,1,-1) # Limit the difference to the max of what the denoising removed locally.
ex_lutxyz (denoised,ssDD,ssD,"y range_half - Y@ abs z range_half - Z@ abs < Y Z ? x +",UV=1,fulls=fs) # abs(diff) after limiting may not be bigger than before.
# Apply the limited difference " x +" (Sharpening is just inverse blurring)
} else {
cb1 = isc ? b1c.ConvertBits(bi, fulls=fs) : original.MCompensate(Super, cb1, mt=true).ConvertBits(bi, fulls=fs)
cf1 = isc ? f1c.ConvertBits(bi, fulls=fs) : original.MCompensate(Super, cf1, mt=true).ConvertBits(bi, fulls=fs)
addif = " x y + range_half - "
pmax = " z a max b max "
pmin = " z a min b min "
Tlim0 = addif+pmax+" min "+pmin+" max"
Tlim1 = Format(addif+pmax+" {os} + min "+pmin+" {os} - max")
Expr(denoised, ssD, original, cb1, cf1, ex_dlut(os==0 ? Tlim0 : Tlim1, bi, fs), isy(denoised) ? Undefined() : "")
} }
######################################################
###
### SeeSaw v0.3i (02 Jan 2006) by Didée
###
### https://forum.doom9.org/showthread.php?p=760935#post760935
###
### 0.3f (13 Dec 2006) by foxyshadis (port to masktools2)
### 0.3g (15 Dec 2015) by StainlessS (Force int call arguments to user script function float params to be explicit floats)
### 0.3h (31 Jan 2020) by real.finder (Update to avs+ and HBD)
### 0.3i (29 Jan 2022) by Dogway (Sanitize, port to ExTools -MIX mod-, optimize expressions, add HD support)
###
### (Full Name: "Denoiser-and-Sharpener-are-riding-the-SeeSaw" )
###
### This function provides a (simple) implementation of the "crystality sharpen" principle.
### In conjunction with a user-specified denoised clip, the aim is to enhance
### weak detail, hopefully without oversharpening or creating jaggies on strong
### detail, and produce a result that is temporally stable without detail shimmering,
### while keeping everything within reasonable bitrate requirements.
### This is done by intermixing source, denoised source and a modified sharpening process,
### in a seesaw-like manner.
###
### Dependencies:
### ExTools
### ResizersPack
### MaskTools
### RgTools
###
### Usage:
###
### a = TheNoisySource
### b = a.YourPreferredDenoising()
### SeeSaw( a, b, [parameters] )
###
### You're very much encouraged to feed your own custom denoised clip into SeeSaw.
### If the "denoised" clip parameter is omitted, a simple "spatial pressdown" filter is used.
###
###
### Fiddled together by Didée, for your pleasure.
###
# ======= Main function =======
function SeeSaw( clip input, clip "denoised",
\ int "NRlimit",int "NRlimit2",
\ float "Sstr", int "Slimit", float "Spower", float "SdampLo", float "SdampHi", float "Szp",
\ float "bias", int "Smode", int "sootheT", int "sootheS", float "ssx", float "ssy") {
isy = input.isy()
clp = isy ? input : input.ExtractY()