-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
TransformsPack - Main.avsi
2807 lines (2364 loc) · 179 KB
/
TransformsPack - Main.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
###########################################################
### ##
### ##
### Transforms Pack - Main v2.2.1 (13-02-2024) ##
### ##
### https://forum.doom9.org/showthread.php?t=182881 ##
### ##
### by Dogway (Jose Linares) ##
### ##
### MAIN: ##
### Display_Referred ##
### MatchClip ##
### ConvertFormat ##
### DATA RETRIEVAL: ##
### color_Fuzzy_Search ##
### color_propGet ##
### format_Fuzzy_Search ##
### chroma_placement ##
### color_coef ##
### moncurve_coef ##
### MATRIX HELPERS: ##
### ExtractClip ##
### MatrixClip ##
### DotClip ##
### DotClipA ##
### MatrixDot ##
### MatrixInvert ##
### MatrixTranspose ##
### Broadcast ##
### Cross ##
### Dot ##
### ##
###########################################################
###
### Pack of tools for proper color managing AviSynth+.
### From function transforms like performant piecewise gamma functions,
### to color gamut converters and a list of building block functions
### for matrix operations.
###
### All of them converge into ConvertFormat(). An accurate and simple to use
### All-In-One (AIO) filter, to scale, convert and transform colors.
###
### ConvertFormat() supports color space, color model, transfer function, color range,
### CAT, chroma placement and chroma subsampling conversions among other features
### like chroma reconstruction, 'no ring' scaling, luma space scaling, and no-moirée scaling.
###
### ConvertFormat() is the spiritual successor of LinearResizer()
### and is clearly inspired by cretindesalpes' fmtconv and avsresize's z_ConvertFormat().
### For a "no-fuss" high quality resizer you can use deep_resize() from ResizersPack.
###
###
### Dependencies: AviSynth+ 3.7.3+
###
### Main features (not available in other color management AVS+ solutions):
### - Many more scaling kernels to choose from (~97 kernels)
### - GPU support for all scalers
### - Many more color model conversion functions
### - Convert/Simulate/Compensate from any illuminant to any illuminant
### - Color Space conversion with optional Gamut Compression
### - Full coefficient derivations
### - Ratio resize (ie. 0.5 to halve size instead of absolute dimensions)
### - Relative resize (ie. 1920 to resize to 1920 width, keeping height ratio)
### - Aliases and fuzzy search for any given string argument
### - Preset support
### - Scale spaces (ie. linear scaling, sigmoid scaling...)
### - Filtering extras: noring, nomoiree, chroma reconstruction
### - Show panel
###
###
####################################
### Notes about the JPEG format ###
###
### JPEGs (JPE, IJPG, JIF, etc) uses Rec.601 primaries*, PC Levels, MPEG1 (center) chroma placement, and can be many formats; from RGB to 420, 422 being to spec, but also 444, 411 or 440 (YUV440 or YUVJ440 are "rotated" versions of 422)
### *Photoshop automatically decodes JPEG as Rec.601 for JPEGs without an embedded profile at least up to version "Photoshop CC 2019"
### *Photoshop saves as 420 up to level 6, 444 for above (old versions as 422 for above level 6)
### *Photoshop quality levels vs JPEG qual (at default quantization tables)
### level 12 -> quality 98
### level 11 -> quality 97
### level 10 -> quality 94 (Default for Photoshop)
### *Other High Quality nominal values for quality settings in JPEG-scale are 95 and 90
### *JPEG2000 (and sometimes JPEG) are encoded in PhotoYCC (aka PhotoCD)
###
### Display_Referred()
###
### Performance friendly viewing transform
### Note: If you are taking snapshots (ie. with AvsPmod), disable Display_Referred() so you don't burn your monitor view transform into the image
###
### Example:
###
### display_referred(disp_ref="709",profile="MyDisplayProfile.Rec709.cube") # Define 'disp_ref' even if profile LUT is specified
###
function Display_Referred (clip clp, string "source", string "disp_ref", string "kernel", bool "gamut", bool "gamma", string "profile", bool "tv_range", string "LMS", float "b", float "c", float "p") {
img = FrameCount(clp) < 2
rgb = isRGB(clp)
propIDs = color_propGet(clp)
bi = propIDs[7]
fs = !propIDs[6]
LUT = Default (profile, Undefined()) # Display profile LUT (the path string of your monitor .cube 3D LUT, if calibrated)
gamut = Default (gamut, true) # convert gamut
gamma = Default (gamma, true) # convert gamma
source = Default (source, img ? "jpg" : propIDs[2]) # Source Color Space
disp_ref = Default (disp_ref,Defined(LUT) ? "709" : "sRGB") # Display Referred Color Space: sRGB for typical uncalibrated monitor, 709 (with 1886 transfer) for calibrated monitors or SmartTVs
kernel = Default (kernel, "Precise")
tv = Default (tv_range, !fs )
LMS = Default (LMS, "CAT16")
b = Default (b, 0.00) # Bicubic's 'b' arg or 'taps' for sinc kernels
c = Default (c, 0.75) # Precise Bicubic
p = Default (p, 0.25)
Assert(IsVersionOrGreater(3,7,3), "Display_Referred: Update AviSynth+ version")
clp.ConvertBits(32, fulls=fs)
src = color_Fuzzy_Search (source)
tgt = color_Fuzzy_Search (disp_ref)
src = src[0]=="709" || src[0]=="170M" && !img || src[0]=="240M" || src[0]=="Display-P3" || \
FindStr(src[0], "470")>0 && !img || FindStr(src[0],"2020")>0 ? [src[0],src[1],"1886"] : [src[0],src[1],img?"sRGB":src[2]]
tgt = tgt[0]=="709" || tgt[0]=="170M" || tgt[0]=="240M" || tgt[0]=="Display-P3" || \
FindStr(tgt[0], "470")>0 || FindStr(tgt[0],"2020")>0 ? [tgt[0],tgt[1],"1886"] : tgt
gamut = !(src[0] == tgt[0] || !gamut)
rgb ? ConvertToPlanarRGB(src[0]) : \
YUV_to_RGB(src[0], tv_in=tv, kernel=kernel, b=b, c=c, p=p)
CCTF(src[2], true, false, false)
if (gamut) {
mata = RGB_to_XYZ (src[0], src[1], LMS=LMS, list=true, adaptXYZ=false)
matw = CAT (src[1], tgt[1], LMS=LMS, list=true)
matb = XYZ_to_RGB (tgt[0], tgt[1], LMS=LMS, list=true, adaptXYZ=false)
MatrixClip(MatrixDot(src[1]!=tgt[1] ? MatrixDot(mata, MatrixTranspose(matw)) : mata, matb) ) }
Defined(LUT) || gamma ? CCTF(tgt[2], false, false, false) : \
gamut ? CCTF(src[2], false, false, false) : last
Defined(LUT) ? ConvertBits(16, dither=-1, fulls=true) : last
Defined(LUT) ? Cube(LUT, 3, true) : last
ConvertBits(8, dither=1, fulls=true) }
# Matches 'a' to 'b' clip format (size, length, color model, range, Jab, bitdepth) *Interlacing not supported
function MatchClip (clip a, clip b, string "kernel", string "matrix", bool "size", bool "props", bool "length", bool "Internal") {
fst = Default (Internal, false) # Set to true to use internal ConvertTo... filters instead of ConvertFormat(). Might be slower though.
fp = Default (props, true) # Assign frame properties
sz = Default (size, true) # Match dimensions
mfc = Default (length, false) # Match also framecount
krn = Default (kernel, "CatRom")
Assert(IsVersionOrGreater(3,7,3), "MatchClip: Update AviSynth+ version")
propIDs= color_propGet(b)
YUY2 = b.IsYUY2()
rgbb = b.isRGB() rgba = a.isRGB()
isyb = b.isY() isya = a.isY()
w = b.width() wa = a.width()
h = b.height() ha = a.height()
alpha = b.HasAlpha() alphaa = a.HasAlpha()
fc = b.FrameCount() fca = a.FrameCount()
p_type = b.PixelType() p_typea = a.PixelType()
bib = propIDs[7] bia = a.BitsPerComponent()
fs = propNumElements(a,"_ColorRange") > 0 ? \
propGetInt (a,"_ColorRange") == 0 : rgba
fd = !propIDs[6]
isHDa = (wa > 1099 || ha > 599 )
isUHD = (w > 2599 || h > 1499)
isUHDa = (wa > 2599 || ha > 1499)
mat = Default(matrix, propIDs[2])
a
yv411 = b.isYV411() yv411a = a.isYV411()
yv444 = b.is444() yv444a = a.is444()
!yv411 && bib > bia ? ConvertBits(bib, fulls=fs, fulld=fs) : last
rat = min(w/float(wa),h/float(ha))
rgbp = rgbb && b.isPlanar()
mod = rgba && rgbb || isya && isyb || yv444 && yv444a ? 1 : 2
isScl = (w != wa || h != ha) && sz
fmt = format_Fuzzy_Search(b, p_type, bib)
fmta = format_Fuzzy_Search(a, p_typea, bia)
krnc = Eval(fmta[1])>Eval(fmt[1]) ? "Didee" : krn
if (!fst) {
ConvertFormat(isScl ? rat : 1, fmt_in=p_typea, fmt_out=p_type, cs_in=mat, cs_out="", kernel=krn, kernel_c=krnc, tv_in=!fs, tv_out=!fd)
isScl ? PadResize(w, h, mode="dilate", mod=mod) : last
alpha ? AddAlphaPlane(b) : last
} else {
# Match dimensions
isScl ? RatioResize(rat, mode="%",kernel=w>wa?krn:"Didee",mod=mod) : last
isScl ? PadResize (w, h, mode="dilate", mod=mod) : last
cplace = rgbb || yv444 || yv411 ? "MPEG1" : isUHD ? "top_left" : "MPEG2"
cplacea = rgba || yv444a || yv411a ? "MPEG1" : isUHDa ? "top_left" : "MPEG2"
mat = mat+(fd!=fs?":l":":f")
bc = bicubic_coeffs(krnc) # Hardcode 'Didee' when downscaling chroma
krn = bc[0]>-2 ? "Bicubic" : krn
not42 = LeftStr(fmta[1],2) != "42"
# ConvertBackToYUY2 bug: https://forum.doom9.org/showthread.php?p=1038027
isyb ? rgba ? isUHDa ? DotClip([0.262372,0.678464,0.059164]) : /* Assumes Rec2020 for UHD */
\ isHDa ? DotClip([0.212600,0.715179,0.072221]) : /* Assumes Rec709 for HD */
\ DotClip([0.298912,0.586603,0.114485]) : ConvertToY(mat) : \
rgbb ? rgbp ? Eval( "ConvertToPlanarRGB (mat,interlaced=false"+(yv411a || rgba ? ")" : ",ChromaInPlacement=cplacea, chromaresample=krn,param1=bc[0],param2=bc[1])")) : \
Eval( "ConvertTo" +string(fmt[3])+"(mat,interlaced=false"+(yv411a || rgba ? ")" : ",ChromaInPlacement=cplacea, chromaresample=krn,param1=bc[0],param2=bc[1])")) : \
yv411 ? Eval( "ConvertToYV411 (false,mat," +(not42 ? "" : """ChromaInPlacement=cplacea, """)+"chromaresample=krn,param1=bc[0],param2=bc[1])") : \
YUY2 && rgba ? Eval("""ConvertBackToYUY2 ( mat """) : \
Eval( "ConvertToYUV"+string(fmt[1])+"(false,mat" +(not42 ? "" : ",ChromaInPlacement=cplacea")+(yv444 ? ")" : ",chromaresample=krn,param1=bc[0],param2=bc[1], ChromaOutPlacement=cplace)"))
}
clr = !rgbb ? fd ? $008080 : $108080 : \
fd ? $000000 : $101010
Und = bia > bib ? Undefined() : 1
isyb || !yv411 ? ConvertBits(bib, dither=Und, fulls=isyb?fs:fd, fulld=fd) : last
alpha ? AddAlphaPlane(b) : last
mfc ? fca > fc ? Trim(last, 0, fc==1 ? -1 : fc-1) : \
last+BlankClip(a,fc-fca,color_yuv=clr,pixel_type=p_type,width=sz?w:wa,height=sz?h:ha) : last
# Planar to Interleaved
YUY2 && !rgba ? ConvertToYUY2() : last
# Frame Properties
if (fst && fp) {
propCopy(b,true,props=["_Matrix","_Primaries"],exclude=true)
fd != fs ? propSet("_ColorRange", fd ? 0 : 1) : last # Overrides the ConvertTo... props
} }
###
### ConvertFormat()
###
### Dependencies: AviSynth+ 3.7.3 and over
### ExTools
### GradePack (for show=true or 'sigmoid' scaling)
### MasksPack (for show=true)
### ResizersPack (for show=true or UVRecon=true or NN scalers)
### KNLMeansCL (for UVRecon=true)
### nnedi3(CL) (for UVRecon=true)
### Optional resizer plugins (check links)
###
### Example 1 - Limited YUV to full RGB:
### ConvertBits(10,fulls=false) # RGB conversion from YUV requires at least 10-bit precision, but fairly enough for transfer/color space conversion tasks I wouldn't go any lower than 16-bit, unless you are curious on low bitdepth side-effects.
### ConvertFormat(1,1,"YUV","RGB")
###
### Example 2 - Downscale HD Rec.709 YUV to SD 170M YUV:
### ConvertFormat(720,kernel="SSIM2") # Undefined or 0 stands for automatic size given width/height ratio
### or
### ConvertFormat(preset="DVD-NTSC") # Work-in-Progress
###
### Example 3 - YUV420 to YUV444 and scale up by 2 in sigmoid space with chroma reconstruction and noring (cs_out to "" or "none" to skip color space autoconversion):
### ConvertFormat(2,fmt_out="44",cs_out="",kernel="FSRCNN",scale_space="sigmoid",noring=true,UVRecon=true,cplace_in="MPEG2")
###
### Example 4 - Scale down by 2 with no moirée in linear light space (typically downscaling as well as pre-blurring -for nomoiree- are done in linear light):
### ConvertFormat(0.5,kernel="RobiSharp",scale_space="linear",nomoiree=true)
###
### Example 5 - Scale width to 1280 and height to half with different kernels and taps for Luma/Chroma planes:
### ConvertFormat(1280,0.5,kernel="EWA6",kernel_c="Blackman8")
###
### Example 6 - Convert from YUV444 to YUV422 and TV range to PC range:
### ConvertFormat(1,fmt_out="422",tv_out=false,cplace_out="MPEG2")
###
### Example 7: High quality JPEG decoding and conversion to RGB (sRGB primaries)
### MP_Pipeline("""
### ### platform: win32
### JPEGSource("source.jpg",rec=3)
### ### ###
### """)
### ConvertBits(16)
### Implicit:
### ConvertFormat(1,1,"YUV","RGB","jpeg","sRGB",UVRecon=true) # 'jpeg' as an alias for '170M'
### Explicit:
### ConvertFormat(1,1,"YUV","RGB",cs_in="170M",cs_out="sRGB",tv_in=false,tv_out=false,cplace_in="MPEG1",UVRecon=true)
### fmtc_bitdepth(bits=8, dmode=7) # Ostromoukhov error diffusion
###
### Example 8: Another JPEG conversion to RGB while keeping primaries
### ConvertFormat(1,1,"YUV","RGB","470BG") # In this case the jpeg was 470BG encoded
###
###
### Function Definition:
### (
### clip,
### float width=1.0 (0.1 to 1920.0),
### float height=1.0 (0.1 to 1080.0),
### [string preset="" (""/ "none"/ "DoVi/DVp5/DVp7"/ "PQ10/UHD"/ "HLG10"/ "HD"/ "DVD-NTSC"/ "DVD-PAL"/ "DV"/ "CRT-NTSC"/ "CRT-PAL"/ "Master"/ "Archival"/ "Grade"/ "Theater"/ "JPEGXL"/ "JPEG2000"/ "JPG_old"/ "JPG")],
### [string "fmt_in"="" (""/ "YCbCr411"/ "YCbCr420"/ "YCbCr422"/ "YCbCr444"/ "YcCbcCrc"/ "YUVr"/ "YCoCg"/ "YCoCgR"/ "RGB"/ "HSV"/ "OPP"/ "XYZ"/ "Yxy"/ "YDzDx"/ "Duv"/ "CIELab"/ "CIELuv"/ "IPT"/ "Oklab")],
### [string "fmt_out"="" (""/ "YCbCr411"/ "YCbCr420"/ "YCbCr422"/ "YCbCr444"/ "YcCbcCrc"/ "YUVr"/ "YCoCg"/ "YCoCgR"/ "RGB"/ "HSV"/ "OPP"/ "XYZ"/ "Yxy"/ "YDzDx"/ "Duv"/ "CIELab"/ "CIELuv"/ "IPT"/ "Oklab")],
### [string "cs_in"="" (""/ "709"/ "470M"/ "470BG"/ "170M"/ "NTSC-J"/ "240M"/ "2020NCL"/ "2020CL"/ "XYZ"/ "DCI-P3"/ "Display-P3"/ "ACEScg"/ "ACES2065"/ "ACESCCt"/ "AdobeRGB"/ "AWG"/ "WideGamut"/ "AppleRGB"/ "ProPhoto"/ "AllColorsRGB"/ "MaxRGB"/ "xTremeRGB"/ "ColorMatch"/ "BruceRGB"/ "DonRGB4"/ "BestRGB"/ "BetaRGB"/ "RussellRGB"/ "ECI-RGB"/ "EktaSpacePS5"/ "ChromeSpace100"/ "CIE RGB")],
### [string "cs_out"="" (""/ "709"/ "470M"/ "470BG"/ "170M"/ "NTSC-J"/ "240M"/ "2020NCL"/ "2020CL"/ "XYZ"/ "DCI-P3"/ "Display-P3"/ "ACEScg"/ "ACES2065"/ "ACESCCt"/ "AdobeRGB"/ "AWG"/ "WideGamut"/ "AppleRGB"/ "ProPhoto"/ "AllColorsRGB"/ "MaxRGB"/ "xTremeRGB"/ "ColorMatch"/ "BruceRGB"/ "DonRGB4"/ "BestRGB"/ "BetaRGB"/ "RussellRGB"/ "ECI-RGB"/ "EktaSpacePS5"/ "ChromeSpace100"/ "CIE RGB")],
### [string "OETF" ="" (""/ "linear"/ "1886a"/ "1886"/ "470M"/ "470BG"/ "240M"/ ""170M"/ 709-Camera"/ "sRGB"/ "AdobeRGB"/ "CIELab"/ "Apple"/ "Chrome"/ "xvYCC"/ "PhotoCD"/ "2020NCL"/ "2020CL"/ "DCIXYZ"/ "PQ"/ "HLG"/ "HLG ARIB"/ "ACESCCt"/ "log100"/ "log316"/ "1361")],
### [string "OETFi"="" (""/ "linear"/ "1886a"/ "1886"/ "470M"/ "470BG"/ "240M"/ ""170M"/ 709-Camera"/ "sRGB"/ "AdobeRGB"/ "CIELab"/ "Apple"/ "Chrome"/ "xvYCC"/ "PhotoCD"/ "2020NCL"/ "2020CL"/ "DCIXYZ"/ "PQ"/ "HLG"/ "HLG ARIB"/ "ACESCCt"/ "log100"/ "log316"/ "1361")],
### [string "EOTFi"="" (""/ "linear"/ "1886a"/ "1886"/ "470M"/ "470BG"/ "240M"/ ""170M"/ 709-Camera"/ "sRGB"/ "AdobeRGB"/ "CIELab"/ "Apple"/ "Chrome"/ "xvYCC"/ "PhotoCD"/ "2020NCL"/ "2020CL"/ "DCIXYZ"/ "PQ"/ "HLG"/ "HLG ARIB"/ "ACESCCt"/ "log100"/ "log316"/ "1361")],
### [string "D_in"="" (""/ "A"/ "B"/ "D50"/ "E"/ "D55"/ "ACES"/ "D60"/ "DCI"/ "D65"/ "C"/ "D75"/ "D93"/ "ID50"/ "ID65"/ "Sunlight"/ "Overcast")],
### [string "D_out"="" (""/ "A"/ "B"/ "D50"/ "E"/ "D55"/ "ACES"/ "D60"/ "DCI"/ "D65"/ "C"/ "D75"/ "D93"/ "ID50"/ "ID65"/ "Sunlight"/ "Overcast")],
### [string "cplace_in"="" (""/ "center"/ "left"/ "top_left"/ "top"/ "bottom_left"/ "bottom")],
### [string "cplace_out"="" (""/ "center"/ "left"/ "top_left"/ "top"/ "bottom_left"/ "bottom")],
### [string "CAT"="CAT" ("CAT"/ "Simulation"/ "Compensation")],
### [bool "tv_in"=true],
### [bool "tv_out"=true],
### [string "scale_space"="gamma" ("gamma"/ "linear"/ "sigmoid"/ "log")],
### [string "kernel"="spline36" ("nnedi3"/ "EEDI3"/ "deep"/ "FCBI"/ "SuperResXBR"/ "Waifu2x"/ "RAVU"/ "FSR1"/ "Krig"/ "FSRCNN"/ "DPID"/ "SSIM"/ "SSIM2"/ "--"/ "Point"/ "Nearest"/ "Box"/ "Bilinear"/ "Bicubic"/ "Quadratic"/ "Gauss"/ "--"/ "Wiener"/ "Spline"/ "Spline16"/ "Spline36"/ "Spline64"/ "Spline100"/ "Spline144"/ "Spline196"/ "Spline256"/ "--"/ "Jinc"/ "Jinc16"/ "Jinc36"/ "Jinc64"/ "Jinc100"/ "Jinc144"/ "Jinc196"/ "Jinc256"/ "EWASharp"/ "EWASharp2"/ "EWASharp4"/ "EWASharper"/ "EWASharper2"/ "EWASharper4"/ "EWASharpest"/ "EWASoft"/ "HaasnSoft"/ "Tukey"/ "--"/ "Sinc"/ "SincLin"/ "SinPow"/ "--"/ "Welch"/ "Cosine"/ "Bessel"/ "Wiener"/ "Hamming"/ "Hann"/ "EWA_Hann"/ "Kaiser"/ "Blackman"/ "Black-Harris"/ "Black-Nuttall"/ "Nuttall"/ "Bohman"/ "Parzen"/ "Lanczos"/ "EWA_Lanczos"/ "Ginseng"/ "EWA_Ginseng"/ "Flat-Top"/ "MinSide"/ "--"/ "Notch"/ "SoftCubic100"/ "Robidoux Soft"/ "SoftCubic75"/ "BilcubicD"/ "BilcubicU"/ "Hermite"/ "Robidoux"/ "EWA_Robidoux"/ "Centroid"/ "Mitchell-Netravali"/ "Robidoux Sharp"/ "EWA_RobidouxSharp"/ "SoftCubic50"/ "CatMule-Dog"/ "Cub-grange"/ "Catmull-Rom"/ "Didee"/ "Zopti"/ "ZoptiN"/ "ZoptiH"/ "Zopti720"/ "Zopti720U"/ "Zopti1080"/ "Precise"/ "Sharp"/ "Hatch")],
### [string "kernel_c"="spline36" ("nnedi3"/ "EEDI3"/ "deep"/ "FCBI"/ "SuperResXBR"/ "Waifu2x"/ "RAVU"/ "FSR1"/ "Krig"/ "FSRCNN"/ "DPID"/ "SSIM"/ "SSIM2"/ "--"/ "Point"/ "Nearest"/ "Box"/ "Bilinear"/ "Bicubic"/ "Quadratic"/ "Gauss"/ "--"/ "Wiener"/ "Spline"/ "Spline16"/ "Spline36"/ "Spline64"/ "Spline100"/ "Spline144"/ "Spline196"/ "Spline256"/ "--"/ "Jinc"/ "Jinc16"/ "Jinc36"/ "Jinc64"/ "Jinc100"/ "Jinc144"/ "Jinc196"/ "Jinc256"/ "EWASharp"/ "EWASharp2"/ "EWASharp4"/ "EWASharper"/ "EWASharper2"/ "EWASharper4"/ "EWASharpest"/ "EWASoft"/ "HaasnSoft"/ "Tukey"/ "--"/ "Sinc"/ "SincLin"/ "SinPow"/ "--"/ "Welch"/ "Cosine"/ "Bessel"/ "Wiener"/ "Hamming"/ "Hann"/ "EWA_Hann"/ "Kaiser"/ "Blackman"/ "Black-Harris"/ "Black-Nuttall"/ "Nuttall"/ "Bohman"/ "Parzen"/ "Lanczos"/ "EWA_Lanczos"/ "Ginseng"/ "EWA_Ginseng"/ "Flat-Top"/ "MinSide"/ "--"/ "Notch"/ "SoftCubic100"/ "Robidoux Soft"/ "SoftCubic75"/ "BilcubicD"/ "BilcubicU"/ "Hermite"/ "Robidoux"/ "EWA_Robidoux"/ "Centroid"/ "Mitchell-Netravali"/ "Robidoux Sharp"/ "EWA_RobidouxSharp"/ "SoftCubic50"/ "CatMule-Dog"/ "Cub-grange"/ "Catmull-Rom"/ "Didee"/ "Zopti"/ "ZoptiN"/ "ZoptiH"/ "Zopti720"/ "Zopti720U"/ "Zopti1080"/ "Precise"/ "Sharp"/ "Hatch")],
### [int "taps"=[0,0] ([0,0] to [12,12] by [1,1])],
### [bool "nomoiree"=false],
### [bool "noring"=false],
### [bool "UVrecon"=false],
### [bool "show"=false]
### )
###
###
####################################
# Resizer Plugins:
#
# SimpleResize (2 tap lin) (8-bit) (dw) https://avisynth.nl/index.php/SimpleResize (same than Box/Area but without pixel centering, very fast)
# Box/Dirichlet/Area/Average (dw) https://github.com/EleonoreMizo/fmtconv or https://github.com/Aktanusa/AreaResize
# SSIM (dw) https://github.com/mysteryx93/AviSynthShader/releases
# SSIM_downsample (dw) https://github.com/Dogway/Avisynth-Scripts/blob/master/EX%20mods/SimilarityMetrics.avsi (HQ settings: SSIM_downsample(w,h,smooth=0.43,resample_args="-0.6,0.4") )
# DPID (dw) https://github.com/Asd-g/AviSynth-DPID (requires: http://avisynth.nl/index.php/Avsresize) (Ranking: Zopti1080 (for UHD) >= SSIM_downsample > DPID > SSIM >>> SincLin > Zopti)
# Spline100/Spline144 (up) https://github.com/EleonoreMizo/fmtconv
# Wiener (up) https://github.com/EleonoreMizo/fmtconv (Sharp as Blackman6 but much more aliasing, and a bit more ringing)
# Jinc (EWA Lanczos) (up) https://github.com/Asd-g/AviSynth-JincResize/releases
# FCBI (Fast Curve Based Interp) (up) https://github.com/Asd-g/AviSynth-FCBI
# SuperResXBR (up) https://github.com/mysteryx93/AviSynthShader/releases
# AiUpscale/FSRCNN (FastSuperResolutionConvo) (up) https://github.com/Alexkral/AviSynthAiUpscale/tree/master/Shaders (lineart / photo folders), designed for clean sources and at least x1.42 upscaling
# Waifu2x (up) https://github.com/Asd-g/AviSynthPlus-w2xncnnvk (best upscaler among the no-hallucination types)
# NNEDI3CL (up) https://github.com/Asd-g/AviSynthPlus-NNEDI3CL
# RAVU (or gpuid>-1) (up) https://github.com/Asd-g/avslibplacebo
# KrigBilateral (up) https://github.com/Alexkral/AviSynthAiUpscale/tree/master/Shaders/KrigBilateral
# NoHalo https://github.com/libvips/libvips/blob/master/libvips/resample/nohalo.cpp (NicolasRobidoux special up/downscalers)
# LoHalo https://legacy.imagemagick.org/discourse-server/viewtopic.php?p=90433&sid=9b1885ea67cf86554709da0282c14796#p90433
# EWA_RobidouxSharp https://github.com/Asd-g/avslibplacebo
# Internal:
#
# Point / Nearest (actually slower than bicubic!)
# Bilinear / Triangular
# Bicubic / Cubic (not recommended for highly quantized content: JPEGs, H.264, etc)
# Spline / Cubic Polynomials (spline16 -2 lobes- spline36 -3 lobes- spline64 -4 lobes-)
# Sinc is a box-windowed sinc filter (Default 4 lobes).
# Lanczos is a Lanczos or sinc-windowed sinc filter (Default 4 lobes).
# Blackman is a Blackman-windowed sinc filter (Default 6 lobes). (Improved lanczos with less ringing)
# SincLin2Resize (up,dw) like a sharper lanczos or a -moderate- ring suppresed Sinc (Doom9 thread: https://forum.doom9.org/showthread.php?p=1919769)
# SinPowerResize (dw) (Doom9 thread: https://forum.doom9.org/showthread.php?p=1918124)
# (-) Missing. ref: https://github.com/mpv-player/mpv/blob/master/video/out/filter_kernels.c
# ref: https://artoriuz.github.io/blog/mpv_upscaling.html
# ref: https://en.wikipedia.org/wiki/Window_function
# ref: https://legacy.imagemagick.org/Usage/filter/nicolas/
# ref: https://legacy.imagemagick.org/discourse-server/viewtopic.php?f=22&t=20942
# -- untapered decay
# Welch/Welsh is a Welch-windowed sinc filter (parabolic window, 3 lobes)
# Cosine is a Cosine-windowed sinc filter (3 lobes) (Good for downscaling but also upscaling. Same as Wiener but without aliasing. Therefore very similar to Blackman6 but with a bit of ringing)
# Bessel is a unnormalized sinc filter (3 lobes)
# -- tapered decay (in order)
# Bartlett is a Bartlett-windowed sinc filter (4 lobes) (actually the same mathematical function used for a 'Triangle' filter, as well as the 'Bilinear' interpolation filter)
# Sinc is a Sinc-windowed sinc filter (4 lobes)
# Hamming is a Hamming-windowed sinc filter (4 lobes) (has a gradient discontinuity so not recommended)
# Hann(ing) is a Hanning-windowed sinc filter (4 lobes) (2-lobes can be used for upscaling sharp images) (Just like Blackman6 bit a tiny bit softer)
# Kaiser is a Kaiser-windowed sinc filter (4 lobes) (also a small gradient discontinuity so not recommended)
# Bohman is a Bohman-windowed sinc filter (6 lobes) (Blackman would fall between Kaiser and Bohman)
# Parzen is a Parzen-windowed sinc filter (8 lobes) (Very similar to Blackman6)
# Ginseng is a Jinc-windowed sinc filter (3 lobes). Better for HBD than ewa_lanczos3. Very similar to spline36 aliasing wise, but sharper. Also adds some bloating.
# -Lagrange is a Lagrange cubic polynomial filter that approximates a windowed filter https://legacy.imagemagick.org/Usage/filter/#lagrange
# -Jinc/Bessel is a Box-windowed jinc filter (3.2383154841662362 lobes). Jinc filters are not separable so they're usually slower than sinc, cubic, etc but more pleasant artifacts. See here: https://legacy.imagemagick.org/Usage/filter/#cylindrical
# ewa_hanning is a Hanning-windowed jinc filter (3.238 lobes).
# ewa_ginseng is a sinc-windowed jinc filter (3.238 lobes).
# ewa_lanczos is a lanczos-windowed jinc filter (3.238 lobes). Under same circumstances -in this case 'taps'- Jinc (radial) is always sharper than their Sinc (orthogonal) equivalent. Check ewa_lanczosradius3 below.
# ewa_lanczossharp is a lanczos-windowed jinc filter (3.238 lobes). Slightly sharpened as per calculations by Nicolas Robidoux, his preferred all-around scaler. Can be replicated with Jinc filter and blur = 0.9812505644269356
# ewa_lanczossharpest is a lanczos-windowed jinc filter (3.238 lobes). Very sharp, technically not recommended by Nicolas Robidoux except when downscaling. Can be replicated with Jinc filter and blur = 0.88549061701764
# ewa_lanczos2sharp is a lanczos-windowed jinc filter (2.2331305943815286 lobes). Good alternative to the other EWAs when you don't want the moiree or "hash pattern blur" artifact, so use this for dithered content or RobiSharp.
# ewa_lanczosradius3 is a lanczos-windowed jinc filter (3 lobes). More sharpened than ewa_lanczossharp and very similar to Ginseng tensor, good for up and downscale. Can be replicated with Jinc filter and blur = 0.9264075766146068 (scales lobes from 3.238 to 3)
# ewa_lanczos4 is a lanczos-windowed jinc filter (4.2410628637960699 lobes).
# ewa_lanczossoft is a lanczos-windowed jinc filter (3.238 lobes). Softened instead. This one makes hash patterns disappear completely (blur determined by trial and error). Can be replicated with Jinc filter and blur = 1.015. https://github.com/mpv-player/mpv/blob/27c38eac1040cd781f39d977ce53adcd65ddcfb6/video/out/filter_kernels.c#L380
#-ewa_catrom
#-ewa_QuadraticJinc 3-taps
# Haasnsoft is a Hanning-windowed jinc filter (3.238 lobes). Removes almost all aliasing. Blur value to match orthogonal and diagonal contributions on a regular grid. Can be replicated with Jinc filter and blur = 1.11
# Tukey is a Hanning-windowed jinc filter taper = 0.5
# Quadratic/Quadric/Bell like Cubic but 66% faster (1.5 taps/lobes). Also known as 'Bell' -> https://clouard.users.greyc.fr/Pantheon/experiments/rescaling/index-en.html#bell
# RAVU / RAISR RAVU (Rapid and Accurate Video Upscaling) is a set of prescalers inspired by RAISR (Rapid and Accurate Image Super Resolution). Similar in quality to XBR and nnedi3. RAVU 4 Lite looks very much like spline36 though.
# -NGU Next Generation Upscaler. madVR edge directed NN algo (closed source, similar to FSRCNN)
# EASU Edge Adaptive Spatial Upscaling (AMD's FSR 1.0) (up) (Uses a custom (McLaurin approx) 2-tap Lanczos in a single pass (EWA?), and clamped to nearest neighbour upscale to eliminate ringing) (https://gist.github.com/agyild/82219c545228d70c5604f865ce0b0ce5)
# -FSR2.1 AMD FidelityFX Super Resolution v2.1.1 (https://github.com/GPUOpen-Effects/FidelityFX-FSR2)
# Waifu2x Image Super-Resolution for Anime-style art (also includes a photo model) using Deep Convolutional Neural Networks running on Vulkan (uses the ncnn framework). Inspired by SRCNN (http://arxiv.org/abs/1501.00092)
# -Sphinx Continuation of Sinc and Jinc, interpolating all three dimensions at the same time using a carefully constructed filter that has a perfectly spherical frequency response. 1.4302966531242027, sphinx window.
# -ICBI (Iterative Curve Based Interpolation) is a single image superresolution technique described in Fast artifact-free image interpolation by Andrea Giachetti and Nicola Asuni, presented at BMVC 2008
#
#
# Understanding an end-to-end transfer function pipeline for a system:
# _ _\/_
# [|_| OETF (Input Device) -> OETFi (Input transfer decode) -> OOTF* (Rendering Intent) -> EETF (HDR PQ Peak Luminance scaling) -> EOTFi (Display transfer encode) -> EOTF (Output Device, ie. Display) | |
# /|\ |____|
#
# * The OOTF can be implemented within the pipeline for a 1:1 scene referred output (assumes EOTFi transfer matches the inverse of EOTF)
# or added directly in the EOTFi. Example: moncurve_r(1.2).moncurve_r(2.2) -> moncurve_r(1.2*2.2)
#
# * If you define the argument 'OETF' (also known as OECF) it will override 'EOTFi'
#
function ConvertFormat (clip clp, val "width", val "height", string "fmt_in", string "fmt_out", string "cs_in", string "cs_out", string "OETFi", string "EOTFi", string "OETF", \
string "D_in", string "D_out", bool "tv_in", bool "tv_out", string "cplace_in", string "cplace_out", string "CAT", string "LMS", bool "GC", \
string "scale_space", string "kernel", string "kernel_c", float_array "src_left", float_array "src_top", float_array "src_width", float_array "src_height", \
string "preset", float_array "taps", int "mod", bool "nomoiree", bool "noring", bool "UVrecon", bool "show", int "threads", int "cores", val "gpuid") {
IsPackedRGB(clp) ? ConvertToPlanarRGB(clp) : clp
wu = !Defined(width)
hu = !Defined(height)
w = width ()
h = height()
fc = FrameCount() < 2
isy = isy()
p_type = PixelType()
propIDs = color_propGet()
bi = propIDs[7]
nw = Default (width, 1) # 0.0 to 10.0 is a multiplier. 12 to inf is absolute target width
nh = Default (height, nw) # 0.0 to 10.0 is a multiplier. 12 to inf is absolute target height
i_fmt = format_Fuzzy_Search (Default (fmt_in, p_type), bi)
o_fmt = format_Fuzzy_Search (Default (fmt_out, p_type), bi)
isRGBi = i_fmt[0]=="RGB"
isRGBo = o_fmt[0]=="RGB"
RT = isRunTime(clp,i_fmt[3]=="RGB")
isyv = !isy && !isRGBi
fch = isy || isRGBo || o_fmt[1]=="444"
lut1 = bi == 32 || RT ? 0 : 1
lut2 = bi > 12 || RT ? 0 : 2
mod = Default (mod, fch ? 1 : 2) # Rounding target size to mod. Powers of 2 ie: 1, 2, 4, 8, 16...
Assert(isFloat(nw) || isFloat(nh), "ConvertFormat: Width/Height type not supported.") # isFloat(int) also returns true, so good for 'number' type evaluation
nw = isInt(nw) && nw<=10 ? float(nw) : isFloat(nw) && nw>10. ? int(nw) : nw
nh = isInt(nh) && nh<=10 ? float(nh) : isFloat(nh) && nh>10. ? int(nh) : nh
nw = nw<=10. ? w*nw : nw nw = nmod(nw,mod)
nh = nh<=10. ? h*nh : nh nh = nmod(nh,mod)
nw = nw == 0 || wu ? nmod((w*(nh/float(h))),mod) : nw
nh = nh == 0 || hu ? nmod((h*(nw/float(w))),mod) : nh
isUHD = ( w > 2599 || h > 1499) isnUHD = (nw > 2599 || nh > 1499)
isHD = ( w > 1099 || h > 599 ) isnHD = (nw > 1099 || nh > 599 )
wr = float(nw) / w hr = float(nh) / h # for nomoiree
rat = max(abs(wr - 1),abs(hr - 1)) == abs(wr - 1) ? wr : hr # for nomoiree
# DEFAULTS
Und = Undefined()
cs_in_d = Defined (cs_in)
cs_in = cs_in_d ? cs_in =="" ? propIDs[2] : cs_in : fc ? "jpeg" : propIDs[2]
cs_inFA = color_Fuzzy_Search (cs_in)
cs_inF = cs_inFA[0]
cs_out = Defined (cs_out) ? cs_out =="" ? cs_in : cs_out : cs_in_d ? cs_in : (cs_in == "jpeg" || cs_in == "jpg" || cs_inF == "sRGB") && isRGBo ? "sRGB" : isnUHD ? "2020NCL" : isnHD ? "709" : "170M"
cs_outFA = color_Fuzzy_Search (cs_out)
cs_outF = cs_outFA[0]
OETFd = Defined(OETF) ? OETF =="" ? false : true : false
OETFid = propIDs[5]!="linear" ? cs_inF =="709" || cs_inF =="170M" && !fc || cs_inF =="240M" || cs_inF =="Display-P3" || FindStr(cs_inF, "470")>0 && !fc || FindStr(cs_inF, "2020")>0 ? "1886" : cs_inF =="ACEScg" || cs_inF =="ACES2065" || cs_inF =="XYZ" ? "linear" : fc ? "sRGB" : propIDs[5] : propIDs[5]
OETFi = Defined(OETFi) ? OETFi=="" ? OETFid : OETFi : OETFid # By default undoes encoded display referred transfer (1886 EOTFi) for consumer media instead of scene-referred camera transfer (OETFi)
EOTFid = cs_outF=="709" || cs_outF=="170M" && !fc || cs_outF=="240M" || cs_outF=="Display-P3" || FindStr(cs_outF,"470")>0 && !fc || FindStr(cs_outF,"2020")>0 ? "1886" : cs_outF =="ACEScg" || cs_outF =="ACES2065" || cs_outF =="XYZ" ? "linear" : fc ? "sRGB" : cs_outF
EOTFi = OETFd ? OETF : Defined(EOTFi) ? EOTFi =="" ? OETFi : EOTFi : EOTFid
OETFi_i = color_Fuzzy_Search (OETFi)
EOTFi_i = color_Fuzzy_Search (EOTFi)
EOTF = EOTFi_i[2]
tv_in_d = Defined (tv_in)
tv_in = Default (tv_in, !(isRGBi || cs_inF =="sRGB" || cs_in =="jpeg" || cs_in =="jpg") && propIDs[6] )
tv_out = Default (tv_out, (isRGBo || cs_outF=="sRGB" || cs_out=="jpeg" || cs_out=="jpg") ? false : i_fmt[0]!=o_fmt[0] && o_fmt[1]!="444" ? true : tv_in )
kernel = Default (kernel, (w>nw||h>nh)?"Didee":"Precise")
Lkrn = LCase (kernel)
deep1 = FindStr(Lkrn, "deep")>0 || FindStr(Lkrn, "nnedi3")>0
deep2 = deep1 || FindStr(Lkrn, "ravu")>0 || FindStr(Lkrn, "xbr")>0 || FindStr(Lkrn, "waifu")>0 || FindStr(Lkrn, "fsr")>0 || FindStr(Lkrn, "fsrcnn")>0 || FindStr(Lkrn, "fcbi")>0
gpu = deep1 || FindStr(Lkrn, "jinc")>0 || FindStr(Lkrn, "ewa")>0 ? 0 : -1
kernel_c = Default (kernel_c, deep2 ? "lanczos3" : kernel)
Lkrnc = LCase (kernel_c)
deep1c = FindStr(Lkrnc, "deep")>0 || FindStr(Lkrnc, "nnedi3")>0
gpuc = deep1c || FindStr(Lkrnc, "jinc")>0 || FindStr(Lkrnc, "ewa")>0 ? 0 : -1
D_out_d = color_propGet(cs_outFA[4])[3]
D_in = Defined(D_in) ? D_in =="" ? propIDs[2] : D_in : cs_in_d ? cs_in =="" ? propIDs[2] : cs_in : propIDs[2]
D_out = Defined(D_out) ? D_out =="" ? D_in : D_out : D_out_d
D_inA = color_Fuzzy_Search (D_in )
D_outA = color_Fuzzy_Search (D_out)
D_out = D_outA[1]
p = Defined (taps) ? IsArray(taps) ? ArraySize(taps) > 1 ? taps : ArrayAdd(taps,0) : [taps,0] : [0,0]
pd = p[0] > 0
pdc = p[1] > 0
prp = color_propGet (cs_inFA[4], D_inA[5], cs_inFA[4], OETFi_i[6])
cs_in = prp[2]
cs_out = cs_outF
D_in = prp[3]
OETF = prp[5]
LMS = Default (LMS, "CAT16" )
CAT = Default (CAT, "CAT" ) # 'CAT', 'Simulation' or 'Compensation'. Perform CAT (Chromatic Adaptation Transform), illuminant simulation or compensation (full White Point conversion)
space = Default (scale_space, OETF=="linear"?"linear":"gamma") # resizing space: "gamma", "linear" (best for down), "sigmoid" (best for up) or "log" for quasilog -log(x+a)-. ("luminance weighted gamma resampling/sharpening"" Improvement over sigmoid: https://forum.doom9.org/showthread.php?p=1687374) or (https://web.archive.org/web/20150420001849/http://forum.luminous-landscape.com/index.php?topic=91754.msg763345)
cplace_in = Default (cplace_in, fc ? "center" : "") # Assumes default from format
cplace_out = Default (cplace_out, cplace_in) # Assumes default from format
src_left = Defined (src_left) ? IsArray(src_left) ? ArraySize(src_left) > 1 ? src_left : ArrayAdd(src_left,0.0) : [src_left,0.0] : [0.0,0.0]
# src_left/top for chroma is autoscaled from luma units, therefore input [1,1] will be converted to [1,0.5] for 420
src_leftc = src_left[1] src_left = src_left[0]
src_top = Defined (src_top) ? IsArray(src_top) ? ArraySize(src_top) > 1 ? src_top : ArrayAdd(src_top, 0.0) : [src_top, 0.0] : [0.0,0.0]
src_topc = src_top [1] src_top = src_top [0]
noring = Default (noring, false)
nomoir = Default (nomoiree,false)
recon = Default (UVrecon, false)
dg = Default (show, false)
th = Default (threads, 8) # for kernel CPU nnedi3 (gpu=-1) or deep
co = Default (cores,nmod(th/2,2,4)) # for kernel CPU nnedi3 (gpu=-1) or deep
gpu = Defined(gpuid) ? IsInt(gpuid) ? [gpuid,gpuc] : ArraySize(gpuid)==1 ? ArrayAdd(gpuid,gpuc) : gpuid : [gpu,gpuc]
# Set gpuid to 0 or above to run scalers over the GPU. Supported kernels are nnedi3 and those supported by avslibplacebo (bicubics, splines, sincs, and some jincs).
# Waifu, RAVU, SSIM, FSRCNN, Krig, SuperResXBR, FCBI and Tukey are GPU only. GPU scaling for anything other than jincs is surprisingly slower though.
# PRESETS (maps outputs format)
preset = Default(preset, "none")
num = preset == "" ? 0 : preset == "NTSC" ? 8 : \
preset == "none" ? 0 : FindStr(preset,"525")>0 ? 8 : \
preset == "DoVi" ? 1 : preset == "CRT-NTSC" ? 8 : \
preset == "DVp5" ? 1 : preset == "PAL" ? 9 : \
preset == "DVp7" ? 1 : preset == "470" ? 9 : \
preset == "DoVi/DVp5/DVp7"?1 : FindStr(preset,"625")>0 ? 9 : \
preset == "PQ10" ? 2 : preset == "CRT-PAL" ? 9 : \
preset == "UHD" ? 2 : preset == "Master" ? 10 : \
FindStr(preset,"2100")>0 ? 2 : preset == "Archival" ? 11 : \
preset == "PQ10/UHD" ? 2 : preset == "GAM" ? 11 : \
preset == "HLG10" ? 3 : preset == "Grade" ? 12 : \
preset == "HD" ? 4 : preset == "Theater" ? 13 : \
FindStr(preset,"1886")>0 ? 4 : preset == "JXL" ? 14 : \
preset == "DVD" ? 5 : preset == "JPEGXL" ? 14 : \
preset == "DVD-NTSC" ? 5 : preset == "JPEG-XL" ? 14 : \
preset == "DVD-PAL" ? 6 : preset == "JPEG2000" ? 15 : \
preset == "DV" ? 7 : preset == "JP2" ? 15 : \
preset == "CRT" ? 8 : preset == "JPG_old" ? 16 : \
preset == "601" ? 8 : preset == "JPG" ? 17 : 0
# Default DoVi/DVp5/DVp7 PQ10/UHD/2100 HLG10 HD/1886 DVD-NTSC/601 DVD-PAL/470BG DV-NTSC CRT-NTSC CRT-PAL Master GAM*/Archival Grade Theater JPEG-XL JPEG2000 JPG_old JPG/sRGB
o_fmt0 = Select (num, o_fmt[0], "IPTPQc2", "YCbCr", "YCbCr", "YCbCr", "YCbCr", "YCbCr", "YCbCr", "YUV", "YUV", "RGB", "RGB", "RGB", "YUVr", "XYB", "YUVr", "YCbCr", "sYCC") # Model. sYCC is basically sRGB (prims, transfer, range) over YCbCr, but uses true Rec709 derived matrix coeffs, not from 470M
cs_out = Select (num, cs_out, "2020", "2020", "2020", "709", "170M", "470BG", "170M", "170M", "470BG", "DCIXYZ", "AP0", "AP1", "DCIXYZ", "sRGB", "sRGB", "170M", "sRGB") # Primaries. JPG_old standardized decoding (PS, mozjpeg, cjxl, etc) is always treated as encoded with 170M (470M derived) matrix, regardless of image/profile primaries, a mess: https://en.wikipedia.org/wiki/YCbCr#Chromaticity-derived_luminance_systems
o_fmt1 = Select (num, o_fmt[1], "420", "420", "420", "420", "420", "420", "410", "411", "411", "444", "444", "444", "444", "444", "444", "420", "422") # [J:a:b]
tv_out = Select (num, tv_out, false, true, true, true, true, true, false, true, false, false, false, false, false, false, false, false, false)
cplace_out = Select (num, cplace_out, "top_left", "top_left", "top_left", "left", "left", "left", "DV", "center", "center", "", "", "", "", "", "", "center", "center")
D_out = Select (num, D_out, "D65", "D65", "D65", "D65", "D65", "D65", "D65", "D65", "D65", "E", "ACES", "ACES", "E", "D65", "D65", "D65", "D65")
EOTF = Select (num, EOTF, "PQ", "PQ", "HLG", "1886", "1886", "1886", "170M", "170M", "170M", "DCIXYZ", "linear", "ACESCCt", "DCIXYZ", "sRGB", "sRGB", "170M", "sRGB")
PAR = Select (num, 1, 1, 1, 1, 1, "601", "470", "601", "601", "601", 1, 1, 1, 1, 1, 1, 1, 1)
cdc = Select (num, "", "H265", "H265", "H265", "H264", "H262", "H262", "H261", "", "", "TIFF", "PIZ", "PIZ","MJPEG2000", "JPEGXL", "JPEG2000", "JPG", "JPG")
cont = Select (num, "", "TS", "TS", "TS", "TS", "VOB", "VOB", "MPEG2", "", "", "TIFF", "EXR", "EXR", "mj2", "jxl", "jp2", "jpg", "jpg")
bit = Select (num, bi, 12, 10, 10, 8, 8, 8, 8, 0, 0, 16, 16, 32, 12, 8, 8, 8, 8)
# * DVD-PAL and DVD-NTSC employ 1886 as EOTFi since the start of digital mastering (early 2000s), until then 170M transfer function was prevalent.
# * GAM (Graded Archival Master)
############ Data extraction and normalization + Asserts ############
Assert(IsVersionOrGreater(3,7,3), "ConvertFormat: Update AviSynth+ version")
!(space=="sigmoid" || space=="gamma" || space=="linear" || space=="log") ? \
Assert(false, "ConvertFormat: Invalid scale space type") : nop()
Comp = Default (GC, cs_in != cs_out && cs_out=="709") # Gamut compression
space = space != "" && space != "none" ? space : "gamma"
# Constants for logic flow
isGamma = OETF != EOTF
isFrmt = i_fmt[0] != o_fmt0
isSpace = cs_in != cs_out
isIllu = D_in != D_out
isSpaIllu= isSpace || isIllu
isScale = nw!=w || nh!=h
isJab = i_fmt[1] != o_fmt1
isChP = cplace_in != cplace_out
coef_i = color_coef(mat=cs_in)
coef_o = isSpace ? color_coef(mat=cs_out) : coef_i
EOTF2 = isGamma ? EOTF : OETF
i_type4 = i_fmt[1] == "444" o_type4 = o_fmt1 == "444"
i_type2 = i_fmt[1] == "422" o_type2 = o_fmt1 == "422"
i_type1 = i_fmt[1] == "411" o_type1 = o_fmt1 == "411"
i_type0 = i_fmt[1] == "420" o_type0 = o_fmt1 == "420"
i_type10 = i_fmt[1] == "410" o_type10 = o_fmt1 == "410"
cow = i_type4 ? w : i_type1 || i_type10 ? round( w/4.0) : round( w/2.0)
coh = i_type4 || i_type1 || i_type2 ? h : round( h/2.0)
cnw = o_type4 ? nw : o_type1 || o_type10 ? round(nw/4.0) : round(nw/2.0)
cnh = o_type4 || o_type1 || o_type2 ? nh : round(nh/2.0)
# Parsing bicubics coefficients
kernel = kernel == "Bilcubic" ? w*h < nw*nh ? "BilcubicU" : "BilcubicD" : kernel
kernel_c = kernel_c == "Bilcubic" ? cow*coh < cnw*cnh ? "BilcubicU" : "BilcubicD" : kernel_c
bc = bicubic_coeffs(kernel) bch = bicubic_coeffs(kernel_c)
bc_b = bc[0]>-2.?bc[0]:1/3. bch_b = bch[0]>-2.?bch[0]:1/3.
bc_c = bc[0]>-2.?bc[1]:1/3. bch_c = bch[0]>-2.?bch[1]:1/3.
kernel = bc[0]>-2.? "Bicubic" : kernel kernel_c = bch[0]>-2.? "Bicubic" : kernel_c
src_width = Defined (src_width) ? IsArray(src_width) ? ArraySize(src_width) > 1 ? src_width : ArrayAdd(src_width, cow) : [src_width, cow] : [w,cow]
src_widthc = src_width[1] src_width = src_width[0]
src_height = Defined (src_height) ? IsArray(src_height) ? ArraySize(src_height) > 1 ? src_height : ArrayAdd(src_height,coh) : [src_height,coh] : [h,coh]
src_heightc= src_height[1] src_height = src_height[0]
i_type1 || o_type1 ? Assert( bi==8, "ConvertFormat: Unsupported Pixel Type: HBD YUV411") : nop()
i_type10 || o_type10 ? Assert(false, "ConvertFormat: Unsupported Pixel Type: YUV410") : nop()
!fc && !isy && (i_type2 || i_type0) ? Assert( w%2==0, "ConvertFormat: Clip Width is not mod2" ) : nop()
!fc && i_type1 ? Assert( w%4==0, "ConvertFormat: Clip Width is not mod4" ) : nop()
o_type1 ? Assert(nw%4==0, "ConvertFormat: Target Width is not mod4" ) : nop()
!(isRGBo || o_type4 || (cnw>cow || cnh>coh)) ? Assert(!recon, "ConvertFormat: Invalid settings for chroma reconstruction") : nop()
# Overriding 'taps' arg by kernel name (ie. 'blackman8' means taps=8)
function StrNumber(string str) {
for (i = 16, 0, -1) {
num = str == string(i)
i = num ? 0 : i
num} } # Checks if string is a number between 1 and 16
# Exclusion list
nope = FindStr(Lkrn, "spline") > 0 && Lkrn !="spline" && kernel !="Bicubic" || FindStr(Lkrn, "jinc") > 0 && Lkrn !="jinc" || FindStr(Lkrn, "ewasharp") > 0 || FindStr(Lkrn, "nnedi3") > 0 || FindStr(Lkrn, "ssim2") > 0 || FindStr(Lkrn, "ewas") > 0
nopc = FindStr(Lkrnc, "spline") > 0 && Lkrnc !="spline" && kernel_c!="Bicubic" || FindStr(Lkrnc, "jinc") > 0 && Lkrnc !="jinc" || FindStr(Lkrnc,"ewasharp") > 0 || FindStr(Lkrnc,"nnedi3") > 0 || FindStr(Lkrn, "ssim2") > 0 || FindStr(Lkrn, "ewas") > 0
tapFinder1 = RightStr(kernel,1) StrN1 = StrNumber(tapFinder1) && !nope
tapFinder2 = RightStr(kernel,2) StrN2 = StrNumber(tapFinder2) && !nope
taps = StrN2 ? Eval(tapFinder2) : StrN1 ? Eval(tapFinder1) : p[0]
kernel = !nope ? ReplaceStr(kernel , string(taps), "") : kernel
pd = StrN1 || StrN2 ? true : pd
tapFinder1 = RightStr(kernel_c,1) StrN1 = StrNumber(tapFinder1) && !nopc
tapFinder2 = RightStr(kernel_c,2) StrN2 = StrNumber(tapFinder2) && !nopc
tapsc = StrN2 ? Eval(tapFinder2) : StrN1 ? Eval(tapFinder1) : p[1]
kernel_c = !nopc ? ReplaceStr(kernel_c , string(tapsc), "") : kernel_c
pdc = StrN1 || StrN2 ? true : pdc
bessel3 = [1.5708,1.525858,1.39627,1.196854,0.94988,0.681773,0.419361,0.186267,0,-0.129804,-0.201802,-0.222051,-0.202077,-0.156337,-0.0995575,-0.044427,0,0.029012,0.0420623,0.041868,0.0330862,0.020877,0.00967627,0.002371]
quadratic15 = [0.75,0.746094,0.734375,0.714844,0.6875,0.652344,0.609375,0.558594,0.5,0.439453,0.382813,0.330078,0.28125,0.236328,0.195313,0.158203,0.125,0.095703,0.070313,0.048828,0.03125,0.017578,0.007813,0.001953]
wiener3 = [1,0.928293,0.847768,0.749610,0.625,0.470033,0.300445,0.136885,0,-0.094068,-0.147204,-0.165801,-0.15625,-0.1255,-0.082723,-0.037647,0,0.022825,0.032783,0.034161,0.03125,0.02491,0.017308,0.008864]
welch3 = [1,0.972804,0.894064,0.771960,0.618936,0.450106,0.281349,0.127371,0,-0.093051,-0.148802,-0.168948,-0.159155,-0.127874,-0.0848512,-0.039589,0,0.028562,0.0437654,0.046219,0.0389045,0.026257,0.0130728,0.003457]
cosine3 = [1,0.972409,0.892614,0.769145,0.614928,0.445557,0.277261,0.124857,-0,-0.090029,-0.142854,-0.160801,-0.150053,-0.119324,-0.0782968,-0.036093,0,0.025353,0.0382818,0.039802,0.0329539,0.021856,0.0106832,0.002771]
ginseng3 = [1,0.9706924517023813,0.8863037293563394,0.7568846746026882,0.5974484350594943,0.42568771143242046,0.2593638071484244,0.1138155395729318,0,-0.07663423509063601,-0.11630395745498025,-0.12412241952630289,\
-0.10861320529064979,-0.07983137551487393,-0.04746371544549015,-0.01925303598542219,0,0.008737966998564498,0.00827789347498015,0.0019289633237434013,-0.006160413752293629,-0.012153940393777712,-0.013434473237575344,-0.00910161101088654]
hann4 = [1,0.9721491276507364,0.8916666533754284,0.7673293227933666,0.6123898750249921,0.44274830753566713,0.27481699575031854,0.12341360408603762,0,-0.08848381495693607,-0.14005052617158592,-0.1573484857988253,-0.14670726866144826,-0.11675289338122011,-0.0768542368708979,-0.035667086889988917,0,0.025852297356152947,\
0.040259628673424544,0.043939594567039456,0.039299593276647324,0.029609836521073,0.018187607754470616,0.007745261904000362,0,-0.0044240051920867865,-0.005835794531242358,-0.005144384187084287,-0.0034614139060841795,-0.0017466187017072979,-0.0005766441854451786,-0.00007568486302246578]
hamming4 = [1,0.9723291304741598,0.8923302555040387,0.7686246617990514,0.6142487927491155,0.4448795641717659,0.27675712509487843,0.1246257815086694,0,-0.0900023682175275,-0.1431203033588165,-0.16168530396390082,-0.1517323766550889,-0.12167980632033411,-0.0808254466432445,-0.0379149269102415,0,0.02826674933221172,\
0.044845606381107544,0.05006826118507596,0.0460528222676711,0.03593314767037512,0.02307155155805429,0.010401622971455375,0,-0.00707512686962141,-0.010701323742157228,-0.011434689771183611,-0.010173206936358085,-0.00783656756150458,-0.005137221895242707,-0.002481597155711647]
blacknuttall4 = [1,0.9691956173858006,0.8808777891436077,0.7465903227462598,0.583261858851737,0.41026820009726694,0.2462414690601752,0.10626931490160989,0,-0.06906778816628918,-0.10311203169060508,-0.10858461954425613,-0.09429622625282544,-0.06945329948290131,-0.04204507233452078,-0.0178310531150637,0,0.010590373575929674,\
0.014791046691013261,0.0143917812832477,0.01141108308851331,0.007582722772125722,0.004090031528918704,0.0015247187900517072,0,-0.0006668666328159042,-0.0007753811435345259,-0.0006111170869562098,-0.0003782503724444364,-0.00018625686680715366,-0.00006947677421552765,-0.00001734615796446616]
blackharris4 = [1,0.9690685474542851,0.8804147429536661,0.7457038324654484,0.5820238498592893,0.408897820926439,0.24504664162741924,0.10556008266546878,0,-0.06828556374209023,-0.10164720692199704,-0.10668443324736704,-0.09229279162100884,-0.0676826118865924,-0.040770939188391156,-0.01719366607497128,0,0.010072521093743276,\
0.013950171901372979,0.013442938661666109,0.010539924532807445,0.006912782290973393,0.0036717082778625404,0.001343980906503044,0,-0.0005599450449479095,-0.0006299356259012779,-0.00047599901604884737,-0.0002782179944466215,-0.0001254132897686219,-0.00003940328671242334,-0.000006272327028117027]
minside4 = [1,0.9677067397427601,0.8754735424031383,0.7363116111669247,0.5690387749427253,0.3947100931903589,0.2328726279464744,0.09846997346812622,0,-0.060828473848914016,-0.08807741082408858,-0.08963798222912361,-0.07495103916223587,-0.05294982732350461,-0.030621864426414024,-0.012354100121804074,0,0.006550750370351611,\
0.008581357979868063,0.007789505667884757,0.00572824890289049,0.0035077922683009735,0.001731217648211203,0.0005857853596332941,0,-0.0002050370640200731,-0.0002095361090755842,-0.00014308227339085642,-0.00007543851733140218,-0.000030927452169915216,-0.000009168350688844679,-0.00000158174014270345]
flattop4 = [1,0.9633262343597194,0.859593850914732,0.7061738614429447,0.5274619393571068,0.3494083785508531,0.1941326535234994,0.07599859616307823,0,-0.03742790728859844,-0.045746423402100424,-0.03680885348596081,-0.021591047728118973,-0.007969308213425627,0.00010539639481656004,0.002167459237080162,0,-0.003812769923009659,\
-0.007053787251496119,-0.008498312606009256,-0.007982808809703638,-0.0060878619550795154,-0.003677603986271232,-0.0015053481284463162,0,0.0007565960905941711,0.0009239058962157,0.0007553677173860026,0.0004791071697109673,0.00023850930336092043,0.00008824527524187423,0.000021135402405053327]
kaiser8 = [1,0.973784,0.897692,0.779078,0.629225,0.462012,0.29231,0.134312,0,-0.102037,-0.167326,-0.195688,-0.190881,-0.159791,-0.111303,-0.055016,0,0.046264,0.0786285,0.094631,0.0944555,0.080540,0.0569238,0.028458,0,-0.024290,-0.0414535,-0.050006,-0.0499464,-0.042553,-0.0300094,-0.014951,0,0.012629,\
0.0214054,0.025619,0.0253622,0.021395,0.0149254,0.007348,0,-0.006043,-0.0100898,-0.011883,-0.0115622,-0.009575,-0.00654779,-0.003156,0,0.002475,0.00402406,0.004606,0.00434445,0.003478,0.00229289,0.001061,0,-0.000757,-0.0011651,-0.001252,-0.00109846,-0.000808,-0.000481802,-0.000197]
bohman8 = [1,0.973334,0.896071,0.77599,0.624897,0.457161,0.287989,0.131668,0,-0.098853,-0.161007,-0.186917,-0.180888,-0.150148,-0.103645,-0.050742,0,0.04179,0.0702274,0.083521,0.0823302,0.069284,0.0482961,0.023797,0,-0.019685,-0.0330319,-0.039145,-0.0383729,-0.032053,-0.0221387,-0.010789,0,\
0.008687,0.0143432,0.016696,0.0160458,0.013115,0.00884562,0.0042,0,-0.003187,-0.00508814,-0.005708,-0.00526944,-0.004121,-0.00264747,-0.001192,0,0.000798,0.00118257,0.001221,0.00102559,0.00072,0.000408479,0.000159,0,-0.000073,-0.0000834291,-0.000062,-0.000033957,-0.000013,-0.00000291302,0]
parzen8 = [0.666667,0.648727,0.596804,0.51624,0.415088,0.303095,0.190509,0.086876,0,-0.064824,-0.105205,-0.121655,-0.117225,-0.096848,-0.0665116,-0.032382,0,0.026335,0.0439404,0.051856,0.0506892,0.042271,0.0291773,0.014224,0,-0.011489,-0.0190243,-0.022227,-0.0214634,-0.017647,-0.0119891,-0.005745,0,\
0.004475,0.00727292,0.008338,0.00789788,0.006366,0.00423604,0.001986,0,-0.001471,-0.00232189,-0.002577,-0.00235492,-0.001824,-0.00116114,-0.000518,0,0.000341,0.000502619,0.000515,0.000430408,0.000301,0.000169602,0.000066,0,-0.00003,-0.0000341078,-0.000025,-0.0000138158,-0.000005,-0.00000118185,0]
function KernStr(string kernel, int nw, int nh, float bc_b, float bc_c, float p, bool pd, bool show, int gpuid) {
shpath = "C:\Program Files (x86)\AviSynth+\plugins64+\Shaders\avslibplacebo\"
ewarad = pd ? int(p) + 0.24 : 3.2383154841662362
resampler = Format( kernel == "nnedi3" || kernel == "nnedi" ? \
"""nnedi3resize({nw},{nh},cshift="spline36",gpuid={gpuid},HBD=space!="gamma"?true:Undefined(),threads=th,cores=co""" : \
kernel == "EEDI3" || kernel == "EEDI" ? \
"""eedi3resize ({nw},{nh},cshift="spline36",gpuid={gpuid},HBD=space!="gamma"?true:Undefined()""" : \
kernel == "Bicubic" ? \
gpuid<0 ? "BicubicResize ( {nw},{nh},b={bc_b},c={bc_c}" : \
"""libplacebo_Resample({nw},{nh},"bicubic",param1={bc_b},param2={bc_c},device={gpuid}""" : \
kernel == "Bilinear" || kernel == "Triangle" || kernel == "Bartlett" ? \
gpuid<0 ? "BilinearResize( {nw},{nh}" : \
"""libplacebo_Resample({nw},{nh},"bilinear",device={gpuid}""" : \
kernel == "Nearest" || kernel == "NearestNeighbour" || kernel == "NN" ? \
gpuid<0 ? "GaussResize({nw},{nh},p=100" : \
"""libplacebo_Resample({nw},{nh},"gaussian",param1=100,device={gpuid}""" : \
kernel == "Point" || kernel == "Pointer" ? \
gpuid<0 ? "PointResize({nw},{nh}" : \
"""libplacebo_Resample({nw},{nh},"nearest", device={gpuid}""" : \
kernel == "Gauss" ? \
gpuid<0 ? "GaussResize({nw},{nh},p={pd}?{p}:9" : \
"""libplacebo_Resample({nw},{nh},"gaussian",param1={pd}?{p}:9,device={gpuid}""" : \
kernel == "deep" ? """deep_resize({nw},{nh},grain=0,gpuid={gpuid},HBD=space!="gamma"?true:Undefined(),threads=th,cores=co""" : \
kernel == "Average" || kernel == "Box" || kernel == "Dirichlet" || kernel == "Area" || kernel == "Simple" ? \
"""fmtc_resample({nw},{nh},kernel="box" """ : \
kernel == "Spline" ? """fmtc_resample({nw},{nh},kernel="Spline",taps={pd}?int({p}):4""": \
kernel == "Spline16" && gpuid>-1 ? """libplacebo_Resample({nw},{nh},"spline16",device={gpuid}""": \
kernel == "Spline36" && gpuid>-1 ? """libplacebo_Resample({nw},{nh},"spline36",device={gpuid}""": \
kernel == "Spline64" && gpuid>-1 ? """libplacebo_Resample({nw},{nh},"spline64",device={gpuid}""": \
kernel == "Spline100" ? """fmtc_resample({nw},{nh},kernel="Spline",taps=5""" : \
kernel == "Spline144" ? """fmtc_resample({nw},{nh},kernel="Spline",taps=6""" : \
kernel == "Spline196" ? """fmtc_resample({nw},{nh},kernel="Spline",taps=7""" : \
kernel == "Spline256" ? """fmtc_resample({nw},{nh},kernel="Spline",taps=8""" : \
kernel == "Wiener" ? """fmtc_resample({nw},{nh},kernel="impulse",kovrspl=8,cnorm=true,center=true,impulse=ArraySym(Wiener3) """ : \
kernel == "Hamming" ? """fmtc_resample({nw},{nh},kernel="impulse",kovrspl=8,cnorm=true,center=true,impulse=ArraySym(Hamming4) """ : \
kernel == "Black-Harris" ? """fmtc_resample({nw},{nh},kernel="impulse",kovrspl=8,cnorm=true,center=true,impulse=ArraySym(BlackHarris4) """ : \
kernel == "Black-Nuttall"? """fmtc_resample({nw},{nh},kernel="impulse",kovrspl=8,cnorm=true,center=true,impulse=ArraySym(BlackNuttall4) """ : \
kernel == "Flat-Top" ? """fmtc_resample({nw},{nh},kernel="impulse",kovrspl=8,cnorm=true,center=true,impulse=ArraySym(FlatTop4) """ : \
kernel == "MinSide" ? """fmtc_resample({nw},{nh},kernel="impulse",kovrspl=8,cnorm=true,center=true,impulse=ArraySym(MinSide4) """ : \
kernel == "Welch" ? """fmtc_resample({nw},{nh},kernel="impulse",kovrspl=8,cnorm=true,center=true,impulse=ArraySym(Welch3) """ : \
kernel == "Cosine" ? """fmtc_resample({nw},{nh},kernel="impulse",kovrspl=8,cnorm=true,center=true,impulse=ArraySym(Cosine3) """ : \
kernel == "Bessel" ? """fmtc_resample({nw},{nh},kernel="impulse",kovrspl=8,cnorm=true,center=true,impulse=ArraySym(Bessel3) """ : \
kernel == "Parzen" ? """fmtc_resample({nw},{nh},kernel="impulse",kovrspl=8,cnorm=true,center=true,impulse=ArraySym(Parzen8) """ : \
kernel == "Kaiser" ? """fmtc_resample({nw},{nh},kernel="impulse",kovrspl=8,cnorm=true,center=true,impulse=ArraySym(Kaiser8) """ : \
kernel == "Bohman" ? """fmtc_resample({nw},{nh},kernel="impulse",kovrspl=8,cnorm=true,center=true,impulse=ArraySym(Bohman8) """ : \
kernel == "Ginseng" ? \
gpuid<0 ? """fmtc_resample({nw},{nh},kernel="impulse",kovrspl=8,cnorm=true,center=true,impulse=ArraySym(Ginseng3) """ : \
"""libplacebo_Resample({nw},{nh},"ginseng",device={gpuid},radius={pd}?int({p}):3""" : \
kernel == "Hann" || kernel == "Hanning" ? \
"""fmtc_resample({nw},{nh},kernel="impulse",kovrspl=8,cnorm=true,center=true,impulse=ArraySym(Hann4) """ : \
kernel == "Quadratic" || kernel == "Quadric" || kernel == "Bell" ? \
"""fmtc_resample({nw},{nh},kernel="impulse",kovrspl=16,cnorm=true,center=true,impulse=ArraySym(quadratic15) """ : \
kernel == "Nuttall" || kernel == "BlackmanMinLobe" ? \
"""fmtc_resample({nw},{nh},kernel="blackmanminlobe" """: \
kernel == "SincLin" || kernel == "SincLin2ResizeMT" ? \
"SincLin2Resize({nw},{nh},taps={pd}?int({p}):15" : \
kernel == "SinPow" || kernel == "SinPowResizeMT" ? \
"SinPowerResize({nw},{nh},p={pd}?{p}:2.5" : \
kernel == "SSIM" || kernel == "ResizeShader" ? \
"""ResizeShader({nw},{nh},kernel="SSIM",b=0.1""" : \
kernel == "SSIM2" ? \
"""SSIM_downsample({nw},{nh},smooth=0.43,resample_args="-0.6,0.4" """: \
kernel == "Sinc" ? \
gpuid<0 ? "SincResize( {nw},{nh},taps={pd}?int({p}):4" : \
"""libplacebo_Resample({nw},{nh},"sinc",device={gpuid},radius={pd}?int({p}):4""" : \
kernel == "Blackman" ? "BlackmanResize({nw},{nh},taps={pd}?int({p}):6" : \
kernel == "Lanczos" ? \
gpuid<0 ? "LanczosResize( {nw},{nh},taps={pd}?int({p}):4" : \
"""libplacebo_Resample({nw},{nh},"lanczos",device={gpuid},radius={pd}?int({p}):4""" : \
kernel == "DPID" ? "DPID({nw},{nh},lambdaY=0.5" : \
kernel == "Jinc16" ? \
gpuid<0 ? "JincResize({nw},{nh},tap=2" : \
"""libplacebo_Resample({nw},{nh},"ewa_lanczos",device={gpuid},radius=2.2331305943815286""" : \
kernel == "Jinc36" ? \
gpuid<0 ? "Jinc36Resize({nw},{nh}" : \
"""libplacebo_Resample({nw},{nh},"ewa_lanczos",device={gpuid},radius=3.2383154841662362""" : \
kernel == "Jinc64" ? \
gpuid<0 ? "Jinc64Resize({nw},{nh}" : \
"""libplacebo_Resample({nw},{nh},"ewa_lanczos",device={gpuid},radius=4.2410628637960699""" : \
kernel == "Jinc100" ? \
gpuid<0 ? "JincResize({nw},{nh},tap=5" : \
"""libplacebo_Resample({nw},{nh},"ewa_lanczos",device={gpuid},radius=5.2427643768701817""" : \
kernel == "Jinc144" ? \
gpuid<0 ? "Jinc144Resize({nw},{nh}" : \
"""libplacebo_Resample({nw},{nh},"ewa_lanczos",device={gpuid},radius=6.2439216898644877""" : \
kernel == "Jinc196" ? \
gpuid<0 ? "JincResize({nw},{nh},tap=7" : \
"""libplacebo_Resample({nw},{nh},"ewa_lanczos",device={gpuid},radius=7.2447598687199570""" : \
kernel == "Jinc256" ? \
gpuid<0 ? "Jinc256Resize({nw},{nh}" : \
"""libplacebo_Resample({nw},{nh},"ewa_lanczos",device={gpuid},radius=8.2453949139520427""" : \
kernel == "EWA" || kernel == "Jinc" || kernel == "ewa_lanczos" || kernel == "LoBlur" ? \
gpuid<0 ? "JincResize({nw},{nh},tap={pd}?int({p}):3" : \
"""libplacebo_Resample({nw},{nh},"ewa_lanczos",device={gpuid},radius={ewarad}""" : \
kernel == "EWASharp" || kernel == "JincSharp" || kernel == "ewa_lanczossharp" || kernel == "LoJaggy" ? \
gpuid<0 ? "JincResize({nw},{nh},tap=3,blur=0.9812505644269356" : \
"""libplacebo_Resample({nw},{nh},"ewa_lanczos",blur=0.9812505644269356,device={gpuid},radius=3.2383154841662362""" : \
kernel == "EWASharp2" || kernel == "JincSharp2" || kernel == "ewa_lanczos2sharp" ? \
gpuid<0 ? "JincResize({nw},{nh},tap=2,blur=0.9549963639785485" : \
"""libplacebo_Resample({nw},{nh},"ewa_lanczos",blur=0.9549963639785485,device={gpuid},radius=2.2331305943815286""" : \
kernel == "EWASharp4" || kernel == "JincSharp4" || kernel == "ewa_lanczos4sharp" ? \
gpuid<0 ? "JincResize({nw},{nh},tap=4,blur=0.9885973386557245" : \
"""libplacebo_Resample({nw},{nh},"ewa_lanczos",blur=0.9885973386557245,device={gpuid},radius=4.2410628637960699""" : \
kernel == "EWASharper" || kernel == "JincSharper"|| kernel =="ewa_lanczosradius3" ? \
gpuid<0 ? "JincResize({nw},{nh},tap=3,blur=0.9264075766146068" : \
"""libplacebo_Resample({nw},{nh},"ewa_lanczos",blur=0.9264075766146068,device={gpuid},radius=3.2383154841662362""" : \
kernel == "EWASharper2" || kernel == "JincSharper2"||kernel =="ewa_lanczosradius2" ? \
gpuid<0 ? "JincResize({nw},{nh},tap=2,blur=0.895603689740279282": \
"""libplacebo_Resample({nw},{nh},"ewa_lanczos",blur=0.895603689740279282,device={gpuid},radius=2.2331305943815286""" : \
kernel == "EWASharper4" || kernel == "JincSharper4"||kernel =="ewa_lanczosradius4" ? \
gpuid<0 ? "JincResize({nw},{nh},tap=4,blur=0.943159799432847707": \
"""libplacebo_Resample({nw},{nh},"ewa_lanczos",blur=0.943159799432847707,device={gpuid},radius=4.2410628637960699""" : \
kernel == "EWASharpest" || kernel == "JincSharpest"|| kernel =="ewa_lanczossharpest"? \
gpuid<0 ? "JincResize({nw},{nh},tap=3,blur=0.88549061701764" : \
"""libplacebo_Resample({nw},{nh},"ewa_lanczos",blur=0.88549061701764,device={gpuid},radius=3.2383154841662362""" : \
kernel == "EWASoft" || kernel == "JincSoft" || kernel == "ewa_lanczossoft" ? \
gpuid<0 ? "JincResize({nw},{nh},tap=3,blur=1.015" : \
"""libplacebo_Resample({nw},{nh},"ewa_lanczos",blur=1.015 ,device={gpuid},radius=3.2383154841662362""" : \
kernel == "Haasnsoft" ? \
gpuid<0 ? "JincResize({nw},{nh},tap=3,blur=1.11" : \
"""libplacebo_Resample({nw},{nh},"ewa_lanczos",blur=1.11 ,device={gpuid},radius=3.2383154841662362""" : \
kernel == "Tukey" ? \
"""libplacebo_Resample({nw},{nh},"ewa_lanczos",blur=1.11,taper=0.5,device=max(0,{gpuid}),radius=3.2383154841662362""" : \
kernel == "ewa_robidoux" ? \
"""libplacebo_Resample({nw},{nh},"ewa_robidoux" ,device=max(0,{gpuid})""" : \
kernel == "ewa_robidouxsharp" ? \
"""libplacebo_Resample({nw},{nh},"ewa_robidouxsharp",device=max(0,{gpuid})""" : \
kernel == "ewa_hann" || kernel == "ewa_hanning" ? \
"""libplacebo_Resample({nw},{nh},"ewa_hann" ,device=max(0,{gpuid}),radius={ewarad}""" : \
kernel == "ewa_ginseng" ? \
"""libplacebo_Resample({nw},{nh},"ewa_ginseng" ,device=max(0,{gpuid}),radius={ewarad}""" : \
kernel == "FCBI" ? "FCBI().deep_resize({nw},{nh},grain=0,gpuid={gpuid}" : \
kernel == "SuperResXBR" || kernel == "XBR" ? \
"SuperResXBR(factor=2).deep_resize({nw},{nh},grain=0,gpuid={gpuid}" : \
FindStr(kernel,"aifu")>0 ? \
"waifu2xresize({nw},{nh},1,0,grain=0,soothe=false,gpuid={gpuid}" : \
kernel == "RAVU" || kernel == "RAISR" ? \
"""RAVUresize({nw},{nh},path=""""+string(shpath+"ravu-r4.hook")+"""",gpuid={gpuid},grain=0""" : \
kernel == "FSR" || kernel == "EASU" ? \
"""FSRresize( {nw},{nh},path=""""+string(shpath+"FSR.glsl")+"""" ,gpuid={gpuid},grain=0""" : \
kernel == "Krig" || kernel == "KrigBilateral" ? \
"KrigBilateral().deep_resize({nw},{nh},grain=0,gpuid={gpuid}" : \
kernel == "FSRCNN" || kernel == "AiUpscale" ? \
"""AiUpscale(2,Luma="Medium",Chroma="Lanczos4",Mode="Photo").""" \
+"BicubicResize({nw},{nh},b=0.378216,c=0.310892" : \
kernel + "Resize({nw},{nh}" )
# Exclusion of additional resize arguments
src_nul = FindStr(resampler,"grain")>0 || FindStr(UCase(resampler),"AIUPSCALE")>0 || FindStr(UCase(resampler),"SSIM")>0 || kernel == "DPID"
src_box = FindStr(resampler,"fmtc_resample")>0 || FindStr(resampler,"libplacebo")>0
# Retrieving taps for 'show'
if (show) {
resampler = TrimAll(resampler)
taps = FindStr(resampler, "placebo") > 0 ? FindStr(resampler, "radius=")>0 ? MidStr(resampler,FindStr(resampler,"radius=")+7) : "0" : \
FindStr(resampler, "inPow") > 0 ? MidStr (resampler,FindStr(resampler, "p=")+2) : \
FindStr(resampler, "impulse") > 0 ? MidStr (resampler,StrLen (resampler)-1,1) : \
FindStr(resampler, "tap") > 0 ? FindStr(resampler, "Jinc") > 0 ? \
kernel == "Jinc" ? MidStr(resampler,FindStr(resampler, "tap=") +4) : \
MidStr(resampler,FindStr(resampler, "tap=") +4,1) : \
MidStr(resampler,FindStr(resampler, "taps=")+5) : "0"
taps = kernel == "Jinc16" || kernel == "spline16" ? "2" : \
kernel == "Jinc36" || kernel == "spline36" ? "3" : \
kernel == "Jinc64" || kernel == "spline64" ? "4" : \
kernel == "Jinc100" || kernel == "spline100" ? "5" : \
kernel == "Jinc144" || kernel == "spline144" ? "6" : \
kernel == "Jinc196" || kernel == "spline196" ? "7" : \
kernel == "Jinc256" || kernel == "spline256" ? "8" : \
kernel == "Quadratic" || kernel == "Bell" || kernel == "Quadric" ? "1.5" : taps
} else { taps = "0" }
[resampler, src_nul, src_box, Eval(taps)] }
krny = KernStr(kernel , nw, nh, bc_b, bc_c, taps, pd, dg, gpu[0]) resampler = krny[0] src_nuly = krny[1] src_boxy = krny[2]
krnc = KernStr(kernel_c, cnw, cnh, bch_b, bch_c, tapsc, pdc, dg, gpu[1]) resampler_c = krnc[0] src_nulc = krnc[1] src_boxc = krnc[2]
src_lbp = FindStr(resampler, "placebo") > 0 src_nn = FindStr(resampler, "nnedi3resize") > 0 || FindStr(resampler, "eedi3resize") > 0
src_lbpc= FindStr(resampler_c, "placebo") > 0 src_nnc = FindStr(resampler_c, "nnedi3resize") > 0 || FindStr(resampler_c, "eedi3resize") > 0
src_wh = src_boxy && !src_lbp ? Format(",sw={src_width}, sh={src_height}") : src_nn ? "" : Format(",src_width={src_width}, src_height={src_height}")
src_whc = src_boxc && !src_lbpc ? Format(",sw={src_widthc}, sh={src_heightc}") : src_nnc ? "" : Format(",src_width={src_widthc},src_height={src_heightc}")
src_whc4= src_boxc && !src_lbpc ? Format(",sw={nw}, sh={nh}") : src_nnc ? "" : Format(",src_width={nw},src_height={nh}")
fmtcb = src_lbp ? "" : src_boxy ? Format(".ConvertBits({bi})") : ""
fmtcbc = src_lbpc ? "" : src_boxc ? Format(".ConvertBits({bi})") : ""
src_nr = src_nuly ? ")" : src_boxy ? ")" : Format(",src_left={src_left},src_top={src_top}"+src_wh+")")
cplaceY = src_nuly ? ")" : src_boxy ? Format(",sx={src_left},sy={src_top}"+src_wh+")"+fmtcb) : src_nr
Ynr = deep2 ? Format("""ex_luts(Y,Y.Repair(kernel!="FSRCNN"?BicubicResize(Ym,{nw},{nh},0,0"""+src_nr+""":waifu2xresize(Ym,{nw},{nh}"""+src_nr+""",1),mode="clamp")""") : \
Format(".Repair(BicubicResize(Ym,{nw},{nh},0,0"+src_nr+",1)") # noring uses Hermite kernel. Check here if I need -1 for chroma
# BEWARE! Might not work for edge directed kernels!!
nnpln = o_fmt1=="420" ? 2 : o_type2 ? 3 : o_type4 || isy ? 4 : 1
CPB = """src_leftc = i_type4 ? src_leftc : i_type1 || i_type10 ? 0.625-0.375*(src_leftc*float(cow)/cnw) : src_leftc*0.5
src_topc = i_type4 || i_type1 || i_type2 ? src_topc : src_topc*0.5
cplaceH1 = (src_boxc ? ",sx=" : ",src_left=")+string(cplace1[0]+src_leftc)
cplaceV1 = (src_boxc ? ",sy=" : ",src_top =")+string(cplace1[1]+src_topc)"""
if (isSpaIllu && !isFrmt && !isRGBi) {
cplace2 = chroma_placement(nw, nh, nw, nh, cs_in, cs_out, "RGB", o_fmt0, "444", o_fmt1, "center", cplace_out )
cplaceH2 = (src_boxc ? ",sx=" : ",src_left=")+string(cplace2[0])
cplaceV2 = (src_boxc ? ",sy=" : ",src_top =")+string(cplace2[1])
cplaceC2 = (src_nulc ? "" : src_nnc ? Format(",planes=[2,{nnpln}]") : (cplaceH2 + cplaceV2 + src_whc4)) + ")" + fmtcbc
cplace1 = chroma_placement( w, h, nw, nh, cs_in, cs_out, i_fmt[0], "RGB", i_fmt[1], "444", cplace_in, "center" )
Eval(CPB)
cplaceC1 = (src_nulc ? "" : src_nnc ? Format(",planes=[2,4]") : (cplaceH1 + cplaceV1 + src_whc)) + ")" + fmtcbc
} else {
cplace1 = chroma_placement(w, h, nw, nh, cs_in, cs_out, i_fmt[0], o_fmt0, i_fmt[1], o_fmt1, cplace_in, cplace_out )
Eval(CPB)
cplaceC1 = (src_nulc ? "" : src_nnc ? Format(",planes=[2,{nnpln}]") : (cplaceH1 + cplaceV1 + src_whc)) + ")" + fmtcbc
cplace2 = cplace1
cplaceC2 = cplaceC1
}
c_recon = """c_cplace= chroma_placement(w, h, nw, nh, cs_in, cs_out, i_fmt[0], o_fmt0, i_fmt[1], o_fmt1, cplace_in, cplace_out )
ref = Y.KNLMeansCL(0, 12, 0, 31.078131515593690, wref=1) # The default float value derives from pow(1.464968620512209618455732713658, 6.4) where 6.4 is the warp strength
Luma = ref.nnedi3wrap(2, nns=1, qual=1, etype=1, nsize=0, HBD=false, gpuid=gpu[0])
Uu = Cb.nnedi3wrap(i_type4?2:i_type1||o_type10?8:4, i_type10?8:i_type0?4:2, nns=1, qual=1, etype=1, nsize=0, HBD=false, gpuid=gpu[1], fwidth=nw*2, fheight=nh*2) # This means double than original luma to match 'Luma' dimensions
Vu = Cr.nnedi3wrap(i_type4?2:i_type1||o_type10?8:4, i_type10?8:i_type0?4:2, nns=1, qual=1, etype=1, nsize=0, HBD=false, gpuid=gpu[1], fwidth=nw*2, fheight=nh*2) # This means double than original luma to match 'Luma' dimensions
Unew = Uu.KNLMeansCL(0, 12, 0, 9, wref=0, rclip=Luma).BicubicResize(nw, nh, b=0.0, c=0.5, src_left=c_cplace[0]+src_leftc, src_top=c_cplace[1]+src_topc)
Vnew = Vu.KNLMeansCL(0, 12, 0, 9, wref=0, rclip=Luma).BicubicResize(nw, nh, b=0.0, c=0.5, src_left=c_cplace[0]+src_leftc, src_top=c_cplace[1]+src_topc)
Cb = ex_makeadddiff(Unew, ex_boxblur(Unew), Uu.BicubicResize(nw, nh, b=0.0, c=0.5, src_left=c_cplace[0]+src_leftc, src_top=c_cplace[1]+src_topc))
Cr = ex_makeadddiff(Vnew, ex_boxblur(Vnew), Vu.BicubicResize(nw, nh, b=0.0, c=0.5, src_left=c_cplace[0]+src_leftc, src_top=c_cplace[1]+src_topc))"""
# First block is for basic ops; everything except color model/space/illuminant change,
# in other words any of the following:
# scaling
# color range
# gamma
# chroma placement
# Jab
# subpixel shift
# odd cropping
if (isy || !isSpaIllu && !isFrmt) {
Y = isyv ? ExtractY() : last
Y = \
space == "linear" ? Y.CCTF(OETF, true, tv_in, false) : \
space == "sigmoid" ? Y.CCTF(OETF, true, tv_in, false).ex_contrast(-1.15,1,191,false) : \
space == "log" ? Y.CCTF(OETF, true, tv_in, false).ACEScct_r() : \
OETF == "linear" ? \
EOTF != "linear" ? Y.CCTF(EOTF, false, tv_in, tv_out) : Y.CCTF("470M", false, tv_in, false) : Y
Ym = nomoir ? Y.ex_blur((1. / rat) / 2., mode="gaussian") : Y
Y = Eval("Ym." + resampler + cplaceY + (noring && !deep2 ? Ynr : ""))
Y = noring && deep2 ? Eval(Ynr) : Y
# 'SSIM' kernel requires YUV (3 planes). It changes colors so debug!
if (isRGBi || FindStr(resampler,"ResizeShader")>0 ) {
Y
} else {
if (isyv) {
Cb = ExtractU()
Cr = ExtractV()
Y
if (recon && !i_type4) {
Eval(c_recon)
} else {
if (kernel_c == "SSIM") {
Cb = ExtractY(Eval("ConvertToYUV420(Cb)." + resampler_c + cplaceC1))
Cr = ExtractY(Eval("ConvertToYUV420(Cr)." + resampler_c + cplaceC1))
} else {
Cb = Eval("Cb." + resampler_c + cplaceC1)
Cr = Eval("Cr." + resampler_c + cplaceC1)
}
}
} else { Y } }
space == "linear" ? CCTF(EOTF, false, false, tv_out) : \
space == "sigmoid" ? ex_contrast(1.15,1,191,false,3).CCTF(EOTF, false, false, tv_out) : \
space == "log" ? ACEScct_f(UV=3). CCTF(EOTF, false, false, tv_out) : \
OETF == "linear" ? \
EOTF != "linear" ? last : CCTF("470M", true, false, tv_out) : \
isGamma ? CCTF(OETF, true, tv_in, false ). \
CCTF(EOTF, false, false, tv_out) : !isyv && tv_in != tv_out ? SMPTE_legal(tv_out) : last
if (isyv) {
CombinePlanes(last, Cb, Cr, planes="YUV", pixel_type="YUV"+o_fmt1+o_fmt[2])
tv_in != tv_out ? SMPTE_legal(tv_out, space=="gamma" && !isGamma && (!tv_out || tv_out && EOTF!="linear") ? 3 : 1, 3) : last
}
} else {
propSet("_ColorRange", tv_in ? 1 : 0)
if (!isRGBi) {
Y = ExtractY()
Cb = ExtractU()
Cr = ExtractV()
if (isScale || isGamma) {
Y = \
space == "linear" ? Y.CCTF(OETF, true, tv_in, false) : \