forked from Dogway/Avisynth-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransformsPack.v1.0.RC22.avsi
2308 lines (1725 loc) · 116 KB
/
TransformsPack.v1.0.RC22.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 v1.0 RC22 (01-10-2021) ##
### ##
### https://forum.doom9.org/showthread.php?t=182825 ##
### https://forum.doom9.org/showthread.php?t=182881 ##
### ##
### by Dogway (Jose Linares) ##
### ##
###########################################################
###
### 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 in ColorSpace(). An accurate and simple to use
### color space and gamma conversion filter.
###
### With color management in place, I took it one step further and made ConvertFormat(),
### replacement for (Dither Tools based) LinearResizer() and clearly inspired
### by avsresize's z_ConvertFormat() with more features, accuracy and easier to use.
###
###
### Dependencies: AviSynth+ 3.7.1 and over
###
####################################
function ColorSpace (clip clp, string "source", string "target", bool "gamut", bool "gamma", bool "tv_range_in", bool "tv_range_out", bool "linear_in", bool "linear_out", string "format_out") {
isRGBc = isRGB(clp)
source = Default (source, "Rec709")
target = Default (target, source)
gamut = Default (gamut, source != target) # convert gamut
gamma = Default (gamma, true) # convert gamma
tv_in = Default (tv_range_in, !isRGBc )
tv_out = Default (tv_range_out, tv_in )
lin_in = Default (linear_in, false )
lin_out = Default (linear_out, false )
f_out = Default (format_out, "" )
Assert(IsVersionOrGreater(3,5,0), "Update AviSynth+ version")
clp
# Data extraction and normalization
pxtyp = PixelType(clp)
p_typ = f_out == "" ? pxtyp : Format_fuzzy_search (f_out, BitsPerComponent(clp))
f_ker = f_out != "" && pxtyp != p_typ ? "Bicubic" : "Point"
f_pla = f_out != "" && pxtyp != p_typ ? "" : "MPEG1"
src = Matrix_fuzzy_search (source)
tgt = Matrix_fuzzy_search (target)
s_gam = moncurve_coef (src)
t_gam = moncurve_coef (tgt)
gamut = gamut && src != tgt
# RGB conversion and gamma decode
!isRGBc && lin_in ? moncurve_r(s_gam[0], s_gam[1], tv_in, tv_in) : last
isRGBc ? ConvertToPlanarRGB(src) : \
YUV_to_RGB(src, tv_range_in=tv_in, kernel=f_ker, cplace=f_pla)
isRGBc && lin_in ? tv_in ? SMPTE_legal(false) : last : \
moncurve_f(s_gam[0], s_gam[1],isRGBc?tv_in:false,false)
# color space conversion
if (gamut) {
mata = RGB_to_XYZ (src, list=true)
matw = c_adaptation(src, tgt, list=true)
matb = XYZ_to_RGB (tgt, list=true)
MatrixClip( MatrixDot(MatrixDot(mata, matw), matb) ) }
# gamma reencode
FindStr(target,"ACES")>0 || lin_out ? last : \
gamma ? moncurve_r(t_gam[0], t_gam[1],false,false) : \
gamut || !lin_out ? moncurve_r(s_gam[0], s_gam[1],false,false) : \
last
# YUV conversion
isRGBc && FindStr(p_typ, "RGB")>0 ? \
tv_out ? SMPTE_legal() : last : \
lin_out ? moncurve_r(t_gam[0], t_gam[1]).RGB_to_YUV( tgt, tv_range_out=tv_out, kernel=f_ker, pixel_type=p_typ, cplace=f_pla).moncurve_f(t_gam[0], t_gam[1],tv_out,tv_out) : \
RGB_to_YUV( tgt, tv_range_out=tv_out, kernel=f_ker, pixel_type=p_typ, cplace=f_pla) }
function Display_Referred (clip clp, string "source", string "disp_ref", string "kernel", bool "gamut", bool "gamma", string "LUT", bool "tv_range", float "b", float "c", float "p") {
isRGBsrc = isRGB(clp)
gamut = Default (gamut, true) # convert gamut
gamma = Default (gamma, true) # convert gamma
disp_ref = Default (disp_ref, "sRGB") # Display Referred Color Space
kernel = Default (kernel, "Bicubic")
LUT = Default (LUT, Undefined()) # Display LUT (the path string of your monitor .cube 3D LUT, if calibrated)
tv_range = Default (tv_range, !isRGBsrc )
b = Default (b, 0.00)
c = Default (c, 0.75) # Precise Bicubic
p = Default (p, 0.25)
Assert(IsVersionOrGreater(3,5,0), "Update AviSynth+ version")
BitsPerComponent(clp) < 32 ? clp.ConvertBits(16) : clp
src = Matrix_fuzzy_search (source)
tgt = Matrix_fuzzy_search (disp_ref)
s_gam = moncurve_coef (src)
t_gam = moncurve_coef (tgt)
gamut = gamut && src != tgt
isRGBsrc ? ConvertToPlanarRGB(src) : \
YUV_to_RGB(src, tv_range_in=tv_range, kernel=kernel, b=b, c=c, p=p)
moncurve_f(s_gam[0], s_gam[1],isRGBsrc?tv_range:false,false)
if (gamut) {
mata = RGB_to_XYZ (src, list=true)
matw = c_adaptation(src, tgt, list=true)
matb = XYZ_to_RGB (tgt, list=true)
MatrixClip( MatrixDot(MatrixDot(mata, matw), matb) ) }
Defined(LUT) || gamma ? moncurve_r(t_gam[0], t_gam[1],false,false) : \
gamut ? moncurve_r(s_gam[0], s_gam[1],false,false) : last
Defined(LUT) ? Cube(LUT, 4, true) : last
ConvertBits(8, dither=1) }
###
### ConvertFormat()
###
###
### Similar to avsresize's z_ConvertFormat() but more flexible, more options and more features:
### Chroma reconstruction, scale_space, noring, mod size, ratio resize, kernel/matrix aliases (supports any resizer), presets, show settings, etc
###
### Dependencies: AviSynth+ 3.7.1 and over
###
###
### Example 1 - Limited YUV to full RGB:
### ConvertFormat(1,1,"YUV","RGB")
###
### Example 2 - Scale HD Rec.709 YUV to SD Rec.601 YUV:
### ConvertFormat(720,0) # 0 stands for automatic size given width/height ratio
### or
### ConvertFormat(preset="DVD-NTSC")
###
### Example 3 - YUV420 to YUV444 and scale up by 2x with chroma reconstruction and noring (cs_out to "" or "none" to skip color space autoconversion):
### ConvertFormat(2,2,fmt_o="444",cs_out="",kernel="FSRCC",noring=true,UVRecon=true)
###
### Example 4 - Scale down by 2 an image with no moirée in sigmoid space:
### ConvertFormat(0.5,scale_space="sigmoid",kernel="RobiSharp",nomoiree=true)
###
### Example 5 - Scale width to 1280 and height to half:
### ConvertFormat(1280,0.5)
###
### Example 6 - Convert from YUV444 to YUV422 and TV range to PC range:
### ConvertFormat(1,1,fmt_o="422",tv_range_out=false)
###
### Example 7: High quality JPEG decoding and conversion to RGB
### MP_Pipeline("""
### ### platform: win32
### JPEGSource("source.jpg",rec=3)
### ### ###
### """)
### ConvertBits(16)
### Implicit:
### ConvertFormat(1,1,"RGB","jpeg","sRGB",UVRecon=true)
### Explicit:
### ConvertFormat(1,1,"RGB","jpeg","sRGB",false,false,true,cplace_in="MPEG1",UVRecon=true)
### ConvertBits(8, dither=1)
###
####################################
# List of Color Models:
# With *: Color Appearance Models (CAM) (Perceptually Uniform to various degrees)
# With +: HDR aware
# With -: Implemented
#
#-RGB+ - RGB color model for channels Red, Green, and Blue. Normally found in interleaved packing format. AviSynth works best with planar RGB so a conversion is first needed.
#-YCbCr - Color format based on human perception of luma and chroma contribution. Uses 3 planes for Luma, opposing blue-yellow, and opposing red-green.
#-YUVr - "YUV RCT". Same as YUV but fully reversible to RGB
# sYUV - "Sharp YUV". Same as YUV but luma-weighted chroma subsampling, retaining chroma fidelity.
#-YcCbcCrc -
# xvYCC -
# OPP -
#-YCgCo - Better luma/chroma decorrelation and compression efficiency than YCbCr
#-YCgCoR - Same as YCgCo but losslessly reversible to RGB
#-XYZ -
#-YDzDx+ - HDR version of XYZ. Requires at least 10-bit for no visible quantization.
# YCxCz - Color Space for image fidelity metrics. It's a first step space, you have to "filter" it and then convert to Lab to retrieve error delta.
# DCI XYZ - XYZ space in DCI illuminant. In effect it's just a container for DCI-P3 in XYZ coordinates for delivery to theaters.
# Yxy/xyY - Chroma normalized XYZ space which gives full luma/chroma decorrelation.
#-HSV - Cylindrical representation of RGB. Not recommended as it doesn't take into account human luminance perception
#-Duv* -
# HunterLab* - A more perceptually uniform version of CIELab
#-CIELab* -
# CIELch(ab)* - HCL (polar coordinates or cylindrical) version of CIELab
# CIEsLab* - CIE simple Lab
# R-Lab* - Ruderman Lab space, using log luma and different weights for ab
#-CIELuv* -
# CIELch(uv)* - HCL (polar coordinates or cylindrical) version of CIELuv
# HSLuv* - A cylindrical HSL type adaptation of CIELuv
# CIEDuDv*+ - or YDuDv. HDR version of CIE Luv. Requires only 8-bit for no visible quantization.
# CIECAM97s* -
# JzAzBz*+ - A uniform color space designed for HDR color.
# JzCzhz*+ - HCL (polar coordinates or cylindrical) version of JzAzBz
#-IPT* - A more uniform color space in SDR colors. There's a version called hdrIPT
# ICtCp+ - BT.2100 (HDR + Rec.2020) version of IPT. HDR version of YCbCr.
# ITP*+ - Perceptually uniform color space version of ICtCp, used extensively in error metrics by scaling Ct by 0.5. This transformation is the basis of the Rec.2124 wide gamut color difference metric D.itp
# IPTPQc2*+ - Improved HDR10 version of ICtCp. Since it's a propietary space no much information is known.
# CIECAM02* -
# CIECAM02Jch* -
# CAM16* - Not yet a CIE standard. Improves on CIECAM02 and can be made uniform with CAM16-UCS.
#-Oklab* - An uniform (UCS) space for SDR color. Simple to compute yet powerful. Takes CAM16 lightness and chroma, and IPT for hue.
#-OkLch* - The cylindrical form of Oklab
# iCAM - An image (not only color) appearance metric model to not only compute perceived difference in lightness, hue or chroma, but also contrast, sharpness or graininess. Involves converting to IPT, low-pass filtering in linear space, convert to non-linearity, then to Jch model and QM if necessary for the metrics.
# Resizer Plugins:
#
# SimpleResize (2 tap lin) (8-bit) (dw) http://avisynth.nl/index.php/SimpleResize (same than Area and ~SSIM but without pixel centering, very fast)
# Box/Area Average (dw) https://github.com/EleonoreMizo/fmtconv
# SSIM (dw) https://github.com/mysteryx93/AviSynthShader/releases
# SincLin2ResizeMT (up) https://github.com/jpsdr/ResampleMT/releases (like a sharper lanczos)
# Spline100/Spline144 (up) https://github.com/EleonoreMizo/fmtconv
# SinPowResizeMT (dw) https://github.com/jpsdr/ResampleMT/releases
# Jinc (EWA Lanczos) (up) https://github.com/Asd-g/AviSynth-JincResize/releases
# FCBI (Fast Curve Based Interp) (8-bit) (up) https://github.com/chikuzen/FCBI/releases
# SuperResXBR (up) https://github.com/mysteryx93/AviSynthShader/releases
# AiUpscale/FSRCNN (FastSuperResolutionConvo) (up) https://github.com/Alexkral/AviSynthAiUpscale/tree/master/Shaders (lineart / photo folders)
# KrigBilateral (up) https://github.com/Alexkral/AviSynthAiUpscale/tree/master/Shaders/KrigBilateral
# Internal:
# Point / Nearest (actually slower than bicubic!)
# Bilinear / Triangular
# Bicubic / Cubic
# Spline / Cubic Polynomials
# Sinc is a box-windowed sinc filter (2 lobes).
# Lanczos is a Lanczos-windowed sinc filter (3 lobes).
# Blackman is a Blackman-windowed sinc filter (3 lobes). (improved lanczos with less ringing)
# Missing. ref: https://github.com/mpv-player/mpv/blob/master/video/out/filter_kernels.c
# ref: https://artoriuz.github.io/blog/mpv_upscaling.html
# Bartlett is a Bartlett-windowed sinc filter (3 lobes).
# Hann(ing) is a Hanning-windowed sinc filter (3 lobes).
# Hamming is a Hamming-windowed sinc filter (3 lobes).
# Welch is a Welch-windowed sinc filter (parabolic window, 3 lobes).
# Cosine is a Cosine-windowed sinc filter (3 lobes).
# Bessel is a unnormalized sinc filter (3 lobes).
# Ginseng is a Jinc-windowed sinc filter (3 lobes).
# Quadratic / Quadric, like Cubic but 66% faster
# RAVU / RAISR (similar to XBR and nnedi3)
# NGU - Next Generation Upscaler. madVR edge directed NN algo (closed source, similar to FSRCNN)
# Jinc is a Box-windowed jinc filter (3 lobes).
# ewa_hanning is a Hanning-windowed jinc filter (3 lobes).
# ewa_ginseng is a sinc-windowed jinc filter (3 lobes).
# -ewa_lanczos is a lanczos-windowed jinc filter (3 lobes).
# -ewa_lanczossharp is a lanczos-windowed jinc filter (3.238 lobes). Slightly sharpened as per calculations by Nicolas Robidoux. 3.2383154841662362, jinc, .blur = 0.9812505644269356
# -ewa_lanczossoft is a lanczos-windowed jinc filter (3.238 lobes). Softened instead. This one makes hash patterns disappear completely. 3.2383154841662362, jinc, .blur = 1.015
# -Haasnsoft is a Hanning-windowed jinc filter (3.238 lobes). Removes almost all aliasing. Blur value to match orthogonal and diagonal contributions. 3.2383154841662362, jinc, .blur = 1.11
# Tukey - hanning window, taper = 0.5
# 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
function ConvertFormat (clip clp, val "width", val "height", string "fmt_i", string "fmt_o", string "cs_in", string "cs_out", bool "tv_range_in", bool "tv_range_out", bool "linear_in", bool "linear_out", \
bool "gamma", string "cplace_in", string "cplace_out", string "kernel", string "kernel_c", float "src_left", float "src_top", float "src_width", float "src_height", \
string "scale_space", float "p", int "mod", bool "nomoiree", bool "noring", bool "UVrecon", bool "fulls", bool "fulld", bool "show") {
clp
w = width ()
h = height()
fc = FrameCount() < 2
p_type = PixelType()
bdpth = BitsPerComponent()
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
fmt_i = Default (fmt_i, "")
fmt_o = Default (fmt_o, fmt_i)
i_fmt = Format_fuzzy_search (fmt_i=="" ? p_type : fmt_i, bdpth)
o_fmt = Format_fuzzy_search (fmt_o=="" ? p_type : fmt_o, bdpth)
isRGBi = i_fmt[0]=="RGB"
isRGBo = o_fmt[0]=="RGB"
isRGBi ? ConvertToPlanarRGB() : last
mod = Default (mod, isRGBo ? 1 : 2) # mod size for resizing. ie: 1, 2, 4, 8, 16...
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 = round(nw/mod)*mod
nh = nh<=10. ? h*nh : nh nh = round(nh/mod)*mod
nw = nw == 0 ? round((w*(nh/float(h)))/mod)*mod : nw
nh = nh == 0 ? round((h*(nw/float(w)))/mod)*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
rat = max(abs(wr - 1),abs(hr - 1)) == abs(wr - 1) ? wr : hr
cs_in = Default (cs_in, isRGBi || fc ? "jpeg" : isUHD ? "2020" : isHD ? "709" : "601")
cs_out = Default (cs_out, (cs_in == "jpeg" || cs_in == "srgb") && isRGBo ? "srgb" : isnUHD ? "2020" : isnHD ? "709" : "601")
linei = Default (linear_in, false)
lineo = Default (linear_out, false)
tv_in = Default (tv_range_in, !(isRGBi || cs_in =="sRGB" || cs_in =="jpeg" || cs_in =="jpg") )
tv_out = Default (tv_range_out, cs_out == "" ? tv_in : !(isRGBo || cs_out=="sRGB" || cs_out=="jpeg" || cs_out=="jpg") )
kernel = Default (kernel, (w>nw||h>nh)?"Didee":"Precise")
kernel_c = Default (kernel_c, kernel)
space = Default (scale_space, "gamma") # resizing space: "gamma", "linear", "sigmoid" or "log" for quasi-log. Assumes gamma encoded input.
cplace_in = Default (cplace_in, "") # Assumes default from format
cplace_out = Default (cplace_out, "") # Assumes default from format
p = Default (p, 15)
src_left = Default (src_left, 0.0)
src_top = Default (src_top, 0.0)
src_width = Default (src_width, w)
src_height = Default (src_height, h)
noring = Default (noring, false)
nomoir = Default (nomoiree,false)
recon = Default (UVrecon, false)
fs = Default (fulls, false)
dg = Default (show, false)
# PRESETS
# UHD HD DVD-NTSC DVD-PAL DV CRT-NTSC CRT-PAL Master Archival Grade Theater JPEG2000 JPG
# fmt_o = Select (num, "ICtCp", "YCbCr", "YCbCr", "YCbCr", "YCbCr", "YIQ", "YUV", "RGB", "RGB", "RGB", "YUVr", "YUVr", "YCbCr")
# cs_out = Select (num, "2020", "709", "601", "470BG", 4, "601", "601", "DCI-P3", "ACES2065", "ACESCCt", "DCI-P3", "sRGB", "601")
# Jab = Select (num, "420", "420", "420", "420", "410", "411", "411", "444", "444", "444", "444", "444", "422")
# tv_out = Select (num, true, true, true, true, false, true, true, false, false, false, false, false, false)
# cplace_out = Select (num, "top_left", "MPEG2", "MPEG2", "MPEG2", "DV", "MPEG1", "MPEG1", "center", "center", "center", "MPEG1", "MPEG1", "MPEG1")
# D = Select (num, "D65", "D65", "D65", "D65", 4, "601", "601", "E", "D60", "D60", "E", "D65", "D65")
# EOTF = Select (num, "PQ", "709", "601", "470", 4, "601", "601", "DCI", "linear", "log", "DCI", "sRGB", "601")
# PAR = Select (num, 1, 1, "601", "470", 4, "601", "601", 1, 1, 1, 1, 1, 1)
# cdc = Select (num, "H265", "H264", "H262", "H262", "H261", "", "", "TIFF", "PIZ", "PIZ", "JPEG2000", "JPEG2000", "JPG")
# cont = Select (num, "TS", "TS", "VOB", "VOB", "MPEG2", "", "", "TIFF", "EXR", "EXR", "JPEG2000", "JPEG2000", "JPG")
# bit = Select (num, 10, 8, 8, 8, 8, "", "", 16, 16, 32, 12, 8, 8)
############ Data extraction and normalization + Asserts ############
Assert(IsVersionOrGreater(3,5,0), "Update AviSynth+ version")
!(isInt(nw) || isInt(nh) || isFloat(nw) || isFloat(nh)) ? \
Assert(false, "Width/Height type not supported.") : nop()
!(space=="sigmoid" || space=="gamma" || space=="linear" || space=="log") ? \
Assert(false, "Invalid space type.") : nop()
cs_in = cs_in != "" && cs_in != "none" ? Matrix_fuzzy_search (cs_in) : "Rec709"
cs_out = cs_out != "" && cs_out != "none" ? Matrix_fuzzy_search (cs_out) : cs_in
space = space != "" && space != "none" ? space : "gamma"
isGamma = Default (gamma, (cs_in != cs_out) || (linei!=lineo))
isSpace = cs_in != cs_out
isFrmt = i_fmt[0] != o_fmt[0]
isScale = nw!=w || nh!=h
isJab = i_fmt[1] != o_fmt[1]
coef_i = Matrix_coef(cs_in)
coef_o = isSpace ? Matrix_coef(cs_out) : coef_i
s_gam = moncurve_coef (linei?"linear":cs_in)
t_gam = isGamma ? moncurve_coef (lineo?"linear":cs_out) : s_gam
bc = bicubic_coeffs(kernel)
bc_b = bc[1]>=0.?bc[0]:1/3.
bc_c = bc[1]>=0.?bc[1]:1/3.
kernel = bc[1]>=0. ? "Bicubic" : kernel
i_type4 = i_fmt[1] == "444" o_type4 = o_fmt[1] == "444"
i_type2 = i_fmt[1] == "422" o_type2 = o_fmt[1] == "422"
i_type1 = i_fmt[1] == "411" o_type1 = o_fmt[1] == "411"
i_type10 = i_fmt[1] == "410" o_type10 = o_fmt[1] == "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)
i_type1 || o_type1 ? Assert(bdpth == 8, "Unsupported Pixel Type: HBD YUV411") : nop()
i_type10 || o_type10 ? Assert(false, "Unsupported Pixel Type: YUV410") : nop()
!isRGBi ? Assert(h%2==0, "Clip Height is not mod2." ) : nop()
i_type2||i_fmt[1]=="420" ? Assert(w%2==0, "Clip Width is not mod2." ) : nop()
i_type1 ? Assert(w%4==0, "Clip Width is not mod4." ) : nop()
o_type1 ? Assert(nw%4==0, "Target Width is not mod4." ) : nop()
!(isRGBo || o_type4 || (cnw>cow || cnh>coh)) ? \
Assert(!recon, "Invalid settings for chroma reconstruction.") : nop()
resampler = Format( kernel == "nnedi3" ?"nnedi3_resize16({nw},{nh}" : \
kernel == "Bicubic" ? "BicubicResize({nw},{nh},b={bc_b},c={bc_c}" : \
kernel == "Spline100" ? """fmtc_resample({nw},{nh},kernel="Spline",taps=5""" : \
kernel == "Spline144" ? """fmtc_resample({nw},{nh},kernel="Spline",taps=6""" : \
kernel == "SincLin" || kernel == "SincLin2ResizeMT" ? \
"SincLin2ResizeMT({nw},{nh},taps=16" : \
kernel == "SinPow" || kernel == "SinPowResizeMT" ? \
"SinPowResizeMT({nw},{nh}" : \
kernel == "ResizeShader" || kernel == "SSIM" ? \
"""ResizeShader({nw},{nh},kernel="SSIM",b=0.1""" : \
kernel == "Gauss" ? "GaussResize({nw},{nh},p=p" : \
kernel == "Average" || kernel == "Box" || kernel == "Area" ? \
"""fmtc_resample({nw},{nh},kernel="box" """ : \
kernel == "Sinc" ? "SincResize({nw},{nh},taps=2" : \
kernel == "EWA" || kernel == "Jinc" || kernel == "ewa_lanczos" ? \
"JincResize({nw},{nh},tap=3" : \
kernel == "EWASharp" || kernel == "JincSharp" || kernel == "ewa_lanczossharp" ? \
"JincResize({nw},{nh},tap=3,blur=0.9812505644269356" : \
kernel == "EWASoft" || kernel == "JincSoft" || kernel == "ewa_lanczossoft" ? \
"JincResize({nw},{nh},tap=3,blur=1.015" : \
kernel == "Haasn" || kernel == "HaasnSoft" || kernel == "ewa_haasn" ? \
"JincResize({nw},{nh},tap=3,blur=1.11" : \
kernel == "FCBI" ? "FCBI().BicubicResize({nw},{nh},b=-0.5,c=0.25" : \
kernel == "SuperResXBR" || kernel == "XBR" ? \
"SuperResXBR(factor=2)." \
+"BicubicResize({nw},{nh},b=-0.5,c=0.25" : \
kernel == "Krig" || kernel == "KrigBilateral" ? \
"KrigBilateral().BicubicResize({nw},{nh},b=-0.5,c=0.25" : \
kernel == "FSRCC" || kernel == "AiUpscale" ? \
"""AiUpscale(2,Luma="Medium",Chroma="Spline36",Mode="Photo").""" \
+"BicubicResize({nw},{nh},b=-0.5,c=0.25" : \
kernel + "Resize({nw},{nh}" )
src_nul = kernel == "ResizeShader" || kernel == "SSIM"
src_box = kernel == "Average" || kernel == "Box" || kernel == "Area" || kernel == "Spline100" || kernel == "Spline144"
src_wh = src_box ? Format(",sw={src_width},sh={src_height}") : Format(",src_width={src_width},src_height={src_height}")
src_whc = src_box ? Format(",sw={cow}, sh={coh}") : Format(",src_width={cow}, src_height={coh}")
fmtcb = Format(".ConvertBits({bdpth},fulls={fs})")
src_nr = Format(",src_left={src_left},src_top={src_top}"+src_wh+")")
src_c = src_nul ? "" : src_box ? Format(",sx={src_left},sy={src_top}"+src_wh+")"+fmtcb) : src_nr
Ynr = Format(".Repair(BicubicResize(Y,{nw},{nh},0,0"+src_nr+",1)") # check here if I need -1 for chroma
cplacei = chroma_placement(cow, coh, cs_in, i_fmt[0]+i_fmt[1], cplace_in )
cplaceo = chroma_placement(cnw, cnh, cs_out, o_fmt[0]+o_fmt[1], cplace_out)
if (!isSpace && !isFrmt && !isJab) {
UVr = isRGBi ? 3 : 1
space == "linear" ? moncurve_f(s_gam[0], s_gam[1], tv_in, false, UVr, fs) : \
space == "sigmoid" ? moncurve_f(s_gam[0], s_gam[1], tv_in, false, UVr, fs).ex_contrast(-1.15,0,191,false,UVr,fs) : \
space == "log" ? moncurve_f(s_gam[0], s_gam[1], tv_in, false, UVr, fs).ACEScct_f(UV=UVr,fulls=fs) : \
isGamma ? moncurve_f(s_gam[0], s_gam[1], tv_in, false, UVr, fs) : last
nomoir ? ex_blur((1. / rat) / 2., bifit=false) : last
Ynr = ReplaceStr(Ynr, "(Y,", "(")
Eval("" + resampler + src_c + (noring ? Ynr : ""))
space == "linear" ? moncurve_r(t_gam[0], t_gam[1], false, tv_out, UVr, fs) : \
space == "sigmoid" ? ex_contrast(1.15,0,191,false,UVr,fs).moncurve_r(t_gam[0], t_gam[1], false, tv_out, UVr, fs) : \
space == "log" ? ACEScct_r(UV=UVr,fulls=fs). moncurve_r(t_gam[0], t_gam[1], false, tv_out, UVr, fs) : \
isGamma ? moncurve_r(t_gam[0], t_gam[1], false, tv_out, UVr, fs) : last
tv_in != tv_out ? space == "gamma" ? SMPTE_legal(tv_out,3, 3,fs) : \
isScale && !isRGBi ? SMPTE_legal(tv_out,1, 3,fs) : \
!isScale && !isGamma ? clp.SMPTE_legal(tv_out,UVr,3,fs) : last : last
} else if (!isRGBi) {
if (isScale && (isRGBo || !isSpace)) {
space == "linear" ? moncurve_f(s_gam[0], s_gam[1], tv_in, false, 1, fs) : \
space == "sigmoid" ? moncurve_f(s_gam[0], s_gam[1], tv_in, false, 1, fs).ex_contrast(-1.15,0,191,false,1,fs) : \
space == "log" ? moncurve_f(s_gam[0], s_gam[1], tv_in, false, 1, fs).ACEScct_f(UV=1,fulls=fs) : \
last
}
Y = ExtractY()
Cb = ExtractU()
Cr = ExtractV()
fcnw = isSpace && !isRGBo ? w : cnw
fcnh = isSpace && !isRGBo ? h : cnh
if (recon && !i_type4) {
threads = 4
ref = Y.KNLMeansCL(0, 16, 0, pow(1.464968620512209618455732713658, 6.4), "auto", wref=1)
Luma = ref.nnedi3_rpow2(rfactor=2, nns=1, qual=1, etype=1, nsize=0, threads=threads/2, prefetch=threads, SetAffinity=true, MaxPhysCore=false)
Uu = Cb.nnedi3_rpow2(rfactor=2, nns=1, qual=1, etype=1, nsize=0, threads=threads/2, prefetch=threads, SetAffinity=true, MaxPhysCore=false, fwidth=w*2, fheight=h*2, ep0=bc_b, ep1=bc_c, cshift=kernel+"resize", mpeg2=false)
Vu = Cr.nnedi3_rpow2(rfactor=2, nns=1, qual=1, etype=1, nsize=0, threads=threads/2, prefetch=threads, SetAffinity=true, MaxPhysCore=false, fwidth=w*2, fheight=h*2, ep0=bc_b, ep1=bc_c, cshift=kernel+"resize", mpeg2=false)
Unew = Uu.KNLMeansCL(0, 16, 0, 6.4, "auto", wref=0, rclip=Luma).BicubicResize(fcnw, fcnh, b=-0.5, c=0.25)
Vnew = Vu.KNLMeansCL(0, 16, 0, 6.4, "auto", wref=0, rclip=Luma).BicubicResize(fcnw, fcnh, b=-0.5, c=0.25)
Cb = ex_makeadddiff(Unew, Removegrain(Unew, 19), Uu.BicubicResize(fcnw, fcnh, b=-0.5, c=0.25), fulls=fs)
Cr = ex_makeadddiff(Vnew, Removegrain(Vnew, 19), Vu.BicubicResize(fcnw, fcnh, b=-0.5, c=0.25), fulls=fs)
} else {
point = kernel=="Point" ? "+1.0" : ""
cplaceH = (src_box ? ",sx=" : ",src_left=")+string(cplaceo[0]+cplacei[0]+src_left)+point
cplaceV = (src_box ? ",sy=" : ",src_top=")+string(cplaceo[1]+cplacei[1]+src_top)
str = ReplaceStr(ReplaceStr(resampler, "("+string(nw), "("+string(fcnw)), ","+string(nh), ","+string(fcnh))
str = (fcnw==cow && fcnh==coh) ? Format("PointResize({fcnw},{fcnh}") : str
Cb = Eval("Cb." + str + (src_nul ? "" : (cplaceH + cplaceV + src_whc)) + ")" + fmtcb)
Cr = Eval("Cr." + str + (src_nul ? "" : (cplaceH + cplaceV + src_whc)) + ")" + fmtcb)
}
if (isScale && (isRGBo || !isSpace)) {
Y = nomoir ? Y.ex_blur((1. / rat) / 2., bifit=false) : Y
Y = Eval("Y." + resampler + src_c + (noring ? Ynr : ""))
Y = space == "linear" ? Y. moncurve_r(s_gam[0], s_gam[1], false, !(isRGBo || !tv_out), 1, fs) : \
space == "sigmoid" ? Y.ex_contrast(1.15,0,191,false,1,fs).moncurve_r(s_gam[0], s_gam[1], false, !(isRGBo || !tv_out), 1, fs) : \
space == "log" ? Y.ACEScct_r(UV=1,fulls=fs). moncurve_r(s_gam[0], s_gam[1], false, !(isRGBo || !tv_out), 1, fs) : \
Y
}
if (!isRGBo && !isSpace) {
CombinePlanes(Y, Cb, Cr, planes="YUV", pixel_type="YUV"+o_fmt[1]+o_fmt[2])
tv_in != tv_out ? space == "gamma" ? SMPTE_legal(tv_out,3, 3,fs) : \
isScale ? SMPTE_legal(tv_out,1, 3,fs) : \
!isScale ? SMPTE_legal(tv_out,UVr,3,fs) : last : last
}
if (isRGBo || isSpace) {
if (i_fmt[0]=="YcCbcCrc") {
RGB = YcCbcCrc_to_RGB( Y, Cb, Cr, cs_in, coef_i, !(isScale || !tv_in), !(isSpace || !tv_out), fs)
R = RGB[0] G = RGB[1] B = RGB[2]
} else if (i_fmt[0]=="YCgCo") {
} else if (i_fmt[0]=="YCgCor") {
} else if (i_fmt[0]=="YUVr") {
} else if (i_fmt[0]=="ICtCp") {
} else if (i_fmt[0]=="YCbCr") {
scale_y = tv_in && (isSpace || !tv_out) ? (255 / 219.) : !tv_in && !(isSpace || !tv_out) ? (219 / 255.) : 1.0
scale_uv = tv_in && (isSpace || !tv_out) ? (255 / 112.) : !tv_in && !(isSpace || !tv_out) ? (224 / 128.) : 2.0
Kr = 1. - coef_i[0]
Kb = 1. - coef_i[2]
m0 = scale_y m1 = 0.0 m2 = scale_uv * Kr
m3 = scale_y m4 = scale_uv * ( -Kb * coef_i[2] / coef_i[1]) m5 = scale_uv * ( -Kr * coef_i[0] / coef_i[1])
m6 = scale_y m7 = scale_uv * Kb m8 = 0.0
range_PC = scale_y == 255/219. ? "ymin - " : ""
range_TV = scale_y == 219/255. ? "ymin + " : ""
UVf = bdpth < 32 ? "range_half - " : ""
R = Expr(Y, Cr, ex_dlut("x "+range_PC+" "+string(m0)+" * "+range_TV+" y "+UVf + string(m2)+" * + ", bdpth, fs), optSingleMode=false)
G = Expr(Y, Cb, Cr, ex_dlut("x "+range_PC+" "+string(m0)+" * "+range_TV+" y "+UVf + string(m4)+" * + z "+UVf + string(m5)+" * + ", bdpth, fs), optSingleMode=true )
B = Expr(Y, Cb, ex_dlut("x "+range_PC+" "+string(m0)+" * "+range_TV+" y "+UVf + string(m7)+" * + ", bdpth, fs), optSingleMode=false)
}
!isSpace ? CombinePlanes(R, G, B, planes="RGB") : last } }
if (isRGBi) {
isSpace || isGamma ? moncurve_f(s_gam[0], s_gam[1], tv_in, false, 3, fs) : last
R = ExtractR()
G = ExtractG()
B = ExtractB()
} else if (isSpace || isGamma) {
R = R.moncurve_f(s_gam[0], s_gam[1], false, false, 1, fs)
G = G.moncurve_f(s_gam[0], s_gam[1], false, false, 1, fs)
B = B.moncurve_f(s_gam[0], s_gam[1], false, false, 1, fs)
}
if (isSpace || isGamma) {
mata = RGB_to_XYZ (cs_in, list=true)
matw = c_adaptation(cs_in, cs_out, list=true)
matb = XYZ_to_RGB (cs_out, list=true)
mat = MatrixDot(MatrixDot(mata, matw), matb)
Ro = Expr(R, G, B, "x "+string(mat[0])+" * y "+string(mat[3])+" * + z "+string(mat[6])+" * +", optSingleMode=true)
Go = Expr(R, G, B, "x "+string(mat[1])+" * y "+string(mat[4])+" * + z "+string(mat[7])+" * +", optSingleMode=true)
Bo = Expr(R, G, B, "x "+string(mat[2])+" * y "+string(mat[5])+" * + z "+string(mat[8])+" * +", optSingleMode=true)
R = Ro G = Go B = Bo
isRGBo ? CombinePlanes(Ro, Go, Bo, planes="RGB") : last
if (isRGBi && isRGBo && isScale) {
RGB = space == "linear" ? isSpace || isGamma ? last : moncurve_f(s_gam[0], s_gam[1], false, false, 3, fs) : \
space == "sigmoid" ? isSpace || isGamma ? ex_contrast(-1.15,0,191,false,3,fs) : moncurve_f(s_gam[0], s_gam[1], false, false, 3, fs).ex_contrast(-1.15,0,191,false,3,fs) : \
space == "log" ? isSpace || isGamma ? ACEScct_f(UV=3,fulls=fs) : moncurve_f(s_gam[0], s_gam[1], false, false, 3, fs).ACEScct_f(UV=3,fulls=fs) : \
isSpace ? moncurve_r(t_gam[0], t_gam[1], false, tv_out, 3, fs) : \
last
RGB = nomoir ? RGB.ex_blur((1. / rat) / 2.,bifit=false) : RGB
RGBnr = ReplaceStr(Ynr, "(Y,", "(RGB,")
RGB = Eval("RGB." + resampler + src_c + (noring ? RGBnr : ""))
space == "linear" ? RGB. moncurve_r(t_gam[0], t_gam[1], false, tv_out, 3, fs) : \
space == "sigmoid" ? RGB.ex_contrast(1.15,0,191,false,3,fs).moncurve_r(t_gam[0], t_gam[1], false, tv_out, 3, fs) : \
space == "log" ? RGB.ACEScct_r(UV=3,fulls=fs). moncurve_r(t_gam[0], t_gam[1], false, tv_out, 3, fs) : \
RGB
} else if (isRGBo) {
moncurve_r(t_gam[0], t_gam[1], false, tv_out, 3, fs)
} }
if (!isRGBo && isSpace || isRGBi && isFrmt) {
R = isSpace || isGamma ? R.moncurve_r(t_gam[0], t_gam[1], isRGBi && !isSpace && tv_in, false, 1, fs) : R
G = isSpace || isGamma ? G.moncurve_r(t_gam[0], t_gam[1], isRGBi && !isSpace && tv_in, false, 1, fs) : G
B = isSpace || isGamma ? B.moncurve_r(t_gam[0], t_gam[1], isRGBi && !isSpace && tv_in, false, 1, fs) : B
if (o_fmt[0]=="YcCbcCrc") {
YCbCr = RGB_to_YcCbcCrc( R, G, B, cs_out, coef_o, !(isSpace || !tv_in), tv_out, fs)
Y = YCbCr[0] Cb = YCbCr[1] Cr = YCbCr[2]
} else if (o_fmt[0]=="YCgCo"){
} else if (o_fmt[0]=="YCgCor"){
} else if (o_fmt[0]=="YUVr"){
} else if (o_fmt[0]=="ICtCp"){
} else if (o_fmt[0]=="YCbCr"){
scale_y = !(isSpace || !tv_in) && (isScale || !tv_out) ? ( 255 / 219.) : (isSpace || !tv_in) && !(isScale || !tv_out) ? ( 219 / 255.) : 1.0
scale_uv = !(isSpace || !tv_in) && (isScale || !tv_out) ? ( 255 / 112.) : (isSpace || !tv_in) && !(isScale || !tv_out) ? ( 112 / 255.) : 0.5
Kr = 1. - coef_o[0]
Kb = 1. - coef_o[2]
m0 = scale_y * coef_o[0] m1 = scale_y * coef_o[1] m2 = scale_y * coef_o[2]
m3 = scale_uv * (-coef_o[0] / Kb) m4 = scale_uv * (-coef_o[1] / Kb) m5 = scale_uv
m6 = scale_uv m7 = scale_uv * (-coef_o[1] / Kr) m8 = scale_uv * (-coef_o[2] / Kr)
rangeY_TV = scale_y==219/255. ? "ymin +" : \
scale_y==255/219. ? "ymin -" : ""
rangeC_TV = bdpth < 32 ? "range_half +" : ""
Y = Expr(R, G, B, ex_dlut("x "+rangeY_TV+" "+string(m0)+" * y "+rangeY_TV+" "+string(m1)+" * + z "+rangeY_TV+" "+string(m2)+" * + ", bdpth, fs), optSingleMode=true)
Cb = Expr(R, G, B, ex_dlut("x "+rangeY_TV+" "+string(m3)+" * y "+rangeY_TV+" "+string(m4)+" * + z "+rangeY_TV+" "+string(m5)+" * + "+rangeC_TV+"", bdpth, fs), optSingleMode=true)
Cr = Expr(R, G, B, ex_dlut("x "+rangeY_TV+" "+string(m6)+" * y "+rangeY_TV+" "+string(m7)+" * + z "+rangeY_TV+" "+string(m8)+" * + "+rangeC_TV+"", bdpth, fs), optSingleMode=true)
}
if (isScale) {
Y = space == "linear" ? Y.moncurve_f(t_gam[0], t_gam[1], false, false, 1, fs) : \
space == "sigmoid" ? Y.moncurve_f(t_gam[0], t_gam[1], false, false, 1, fs).ex_contrast(-1.15,0,191,false,1,fs) : \
space == "log" ? Y.moncurve_f(t_gam[0], t_gam[1], false, false, 1, fs).ACEScct_f(UV=1,fulls=fs) : \
Y
}
point = kernel=="Point" ? "+1.0" : ""
cplaceH = (src_box ? ",sx=" : ",src_left=")+string(cplaceo[0]+cplacei[0]+src_left)+point
cplaceV = (src_box ? ",sy=" : ",src_top=")+string(cplaceo[1]+cplacei[1]+src_top)
str = ReplaceStr(ReplaceStr(resampler, "("+string(nw), "("+string(cnw)), ","+string(nh), ","+string(cnh))
str = (cnw==cow && cnh==coh) ? Format("PointResize({cnw},{cnh}") : str
Cb = Eval("Cb." + str + (src_nul ? "" : (cplaceH + cplaceV + src_wh)) + ")" + fmtcb)
Cr = Eval("Cr." + str + (src_nul ? "" : (cplaceH + cplaceV + src_wh)) + ")" + fmtcb)
if (isScale) {
Y = nomoir ? Y.ex_blur((1. / rat) / 2., bifit=false) : Y
Y = Eval("Y." + resampler + src_c + (noring ? Ynr : ""))
Y = space == "linear" ? Y. moncurve_r(t_gam[0], t_gam[1], false, tv_out, 1, fs) : \
space == "sigmoid" ? Y.ex_contrast(1.15,0,191,false,1,fs).moncurve_r(t_gam[0], t_gam[1], false, tv_out, 1, fs) : \
space == "log" ? Y.ACEScct_r(UV=1,fulls=fs). moncurve_r(t_gam[0], t_gam[1], false, tv_out, 1, fs) : \
tv_out ? Y.SMPTE_legal(tv_out,3,1,fs) : \
Y
}
CombinePlanes(Y, Cb, Cr, planes="YUV", pixel_type="YUV"+o_fmt[1]+o_fmt[2])
}
if (dg) {
i_jab = i_fmt[1]
i_fmt = i_fmt[0]
o_jab = o_fmt[1]
o_fmt = o_fmt[0]
tv_in = tv_in ? "Lim" : "Full"
tv_out = tv_out ? "Lim" : "Full"
Subtitle( "\n SETTINGS:", size=nw/50,lsp=4)
Subtitle(Format("\n\n\n\n IN OUT \n " \
+"colorspace: {cs_in} {cs_out} \n " \
+"format: {i_fmt} {o_fmt} \n " \
+"jab: {i_jab} {o_jab} \n " \
+"range: {tv_in} {tv_out} \n " \
+"cplace: {cplace_in} {cplace_out} \n " \
+"kernel: {kernel} \n noring: {noring} \n nomoir: {nomoir} \n recon: {recon} "),size=nw/75,lsp=4)
} }
####### FORMAT CONVERSION FUNCTIONS #######
# YCbCr to RGB function
function YUV_to_RGB (clip yuv, string "matrix", bool "tv_range_in", bool "tv_range_out", string "kernel", float "b", float "c", float "p", string "cplace", bool "UVrecon", bool "fulls") {
matrix = Default (matrix, "709")
kernel = Default (kernel, "Bicubic")
tv_in = Default (tv_range_in, !(matrix=="JPEG" || matrix=="JPG"))
tv_out = Default (tv_range_out, false)
cplace = Default (cplace, "")
recon = Default (UVrecon, false)
b = Default (b, 0.0)
c = Default (c, 0.75) # Precise Bicubic
p = Default (p, 0.25)
fs = Default (fulls, false)
Assert(IsVersionOrGreater(3,5,0), "Update AviSynth+ version")
Assert(isRGB(yuv)==false, "Only YUV format supported.")
bdpth = BitsPerComponent(yuv)
p_type = Format_fuzzy_search(yuv, PixelType(yuv), bdpth)
matrix = Matrix_fuzzy_search (matrix)
coef = Matrix_coef(matrix)
bc = bicubic_coeffs(kernel)
bc_b = string(bc[1]>=0.?bc[0]:b)
bc_c = string(bc[1]>=0.?bc[1]:c)
kernel = bc[1]>=0. ? "Bicubic" : kernel
p_type4 = p_type[1] == "444"
p_type2 = p_type[1] == "422"
p_type1 = p_type[1] == "411"
p_type10 = p_type[1] == "410"
w = width (yuv)
h = height(yuv)
nw = p_type4 ? w : p_type1 || p_type10 ? round(w/4.0) : round(w/2.0)
nh = p_type4 || p_type1 || p_type2 ? h : round(h/2.0)
cplace = chroma_placement(yuv, w, h, matrix, "YUV"+p_type[1], cplace)
p_type1 ? Assert(bdpth == 8, "Unsupported Pixel Type: HBD YUV411") : nop()
Assert(!p_type10, "Unsupported Pixel Type: YUV410")
Assert(h%2==0,"Height is not mod2." )
!p_type1 || !p_type4 ? Assert(w%2==0, "Width is not mod2." ) : nop()
p_type1 ? Assert(w%4==0, "Width is not mod4." ) : nop()
point = kernel=="Point" ? "+1.0" : ""
cplaceH = ",src_left="+string(cplace[0])+point
cplaceV = ",src_top ="+string(cplace[1])
resampler = kernel == "nnedi3" ? "nnedi3_resize16(" + String(w) + "," + String(h) + cplaceH + cplaceV +")" : \
kernel == "bicubic"? "BicubicResize(" + String(w) + "," + String(h) + cplaceH + cplaceV +",b="+bc_b+",c="+bc_c+")" : \
kernel == "gauss" ? "GaussResize(" + String(w) + "," + String(h) + cplaceH + cplaceV +",p=p)" : \
kernel + "Resize(" + String(w) + "," + String(h) + cplaceH + cplaceV +")"
Y = ExtractY(yuv)
Uor = ExtractU(yuv)
Vor = ExtractV(yuv)
# feisty2's ChromaReconstructor_faster v3.0 HBD mod
if (recon && !p_type4) {
threads = 4
ref = Y.KNLMeansCL(0, 16, 0, pow(1.464968620512209618455732713658, 6.4), "auto", wref=1)
Luma = ref.nnedi3_rpow2(rfactor=2, nns=1, qual=1, etype=1, nsize=0, threads=threads/2, prefetch=threads, SetAffinity=true, MaxPhysCore=false)
Uu = Uor.nnedi3_rpow2(rfactor=2, nns=1, qual=1, etype=1, nsize=0, threads=threads/2, prefetch=threads, SetAffinity=true, MaxPhysCore=false, fwidth=w*2, fheight=h*2, ep0=0.0, ep1=0.75, cshift="bicubicresize",mpeg2=false)
Vu = Vor.nnedi3_rpow2(rfactor=2, nns=1, qual=1, etype=1, nsize=0, threads=threads/2, prefetch=threads, SetAffinity=true, MaxPhysCore=false, fwidth=w*2, fheight=h*2, ep0=0.0, ep1=0.75, cshift="bicubicresize",mpeg2=false)
Unew = Uu.KNLMeansCL(0, 16, 0, 6.4, "auto", wref=0, rclip=Luma).BicubicResize(w, h, b=-0.5, c=0.25)
Vnew = Vu.KNLMeansCL(0, 16, 0, 6.4, "auto", wref=0, rclip=Luma).BicubicResize(w, h, b=-0.5, c=0.25)
U = ex_makeadddiff(Unew, Removegrain(Unew, 19), Uu.BicubicResize(w, h, b=-0.5, c=0.25), fulls=fs)
V = ex_makeadddiff(Vnew, Removegrain(Vnew, 19), Vu.BicubicResize(w, h, b=-0.5, c=0.25), fulls=fs)
} else {
U = p_type4 ? Uor : Eval("Uor." + resampler)
V = p_type4 ? Vor : Eval("Vor." + resampler)
}
if (matrix=="Rec2020CL" || matrix=="DCI-P3" || matrix=="Display-P3") {
RGB = YcCbcCrc_to_RGB( Y, U, V, matrix, coef, tv_in, tv_out, fs)
R = RGB[0] G = RGB[1] B = RGB[2]
} else {
scale_y = tv_in ? (255 / 219.) : 1.0
scale_uv = tv_in ? (255 / 112.) : 2.0
Kr = 1. - coef[0]
Kb = 1. - coef[2]
m0 = scale_y m1 = 0.0 m2 = scale_uv * Kr
m3 = scale_y m4 = scale_uv * ( -Kb * coef[2] / coef[1]) m5 = scale_uv * ( -Kr * coef[0] / coef[1])
m6 = scale_y m7 = scale_uv * Kb m8 = 0.0
rangeY = tv_in ? "ymin - " : ""
range_TV = tv_out ? "ymax ymin - range_max / * ymin +" : ""
UVf = bdpth < 32 ? "range_half - " : ""
R = Expr(Y, V, ex_dlut( "x "+rangeY+" "+string(m0)+" * y "+UVf + string(m2)+" * + "+range_TV+"", bdpth, fs), optSingleMode=false)
G = Expr(Y, U, V, ex_dlut( "x "+rangeY+" "+string(m3)+" * y "+UVf + string(m4)+" * + z "+UVf + string(m5)+" * + "+range_TV+"", bdpth, fs), optSingleMode=true)
B = Expr(Y, U, ex_dlut( "x "+rangeY+" "+string(m6)+" * y "+UVf + string(m7)+" * + "+range_TV+"", bdpth, fs), optSingleMode=false)
}
CombinePlanes(R, G, B, planes="RGB") }
# RGB to YCbCr function
function RGB_to_YUV (clip rgb, string "matrix", bool "tv_range_in", bool "tv_range_out", string "pixel_type", string "kernel", float "b", float "c", float "p", string "cplace", bool "fulls") {
matrix = Default (matrix, "sRGB")
p_type = Default (pixel_type, "420") # target YUV pixel_type
kernel = Default (kernel, "Bicubic")
tv_in = Default (tv_range_in, false)
tv_out = Default (tv_range_out, true)
cplace = Default (cplace, "")
b = Default (b, -0.5)
c = Default (c, 0.25) # Didée's Bicubic
p = Default (p, 0.25)
fs = Default (fulls, false)
Assert(IsVersionOrGreater(3,5,0), "Update AviSynth+ version")
Assert(isRGB(rgb)==true && IsPlanar(rgb)==true, "Only Planar RGB format supported.")
bdpth = BitsPerComponent(rgb)
p_type = Format_fuzzy_search (rgb, p_type, bdpth)
matrix = Matrix_fuzzy_search (matrix)
coef = Matrix_coef(matrix)
bc = bicubic_coeffs(kernel)
bc_b = string(bc[1]>=0.?bc[0]:b)
bc_c = string(bc[1]>=0.?bc[1]:c)
kernel = bc[1]>=0. ? "Bicubic" : kernel
p_type4 = p_type[1] == "444"
p_type2 = p_type[1] == "422"
p_type1 = p_type[1] == "411"
p_type10 = p_type[1] == "410"
w = width (rgb)
h = height(rgb)
nw = p_type4 ? w : p_type1 || p_type10 ? round(w/4.0) : round(w/2.0)
nh = p_type4 || p_type1 || p_type2 ? h : round(h/2.0)
cplace = chroma_placement(rgb, nw, nh, matrix, "YUV"+p_type[1], cplace)
p_type1 ? Assert(bdpth == 8, "Unsupported Pixel Type: HBD YUV411") : nop()
Assert(!p_type10, "Unsupported Pixel Type: YUV410")
Assert(h%2==0,"Height is not mod2." )
!p_type1 || !p_type4 ? Assert(w%2==0, "Width is not mod2." ) : nop()
p_type1 ? Assert(w%4==0, "Width is not mod4." ) : nop()
Rx = ExtractR(rgb)
Gx = ExtractG(rgb)
Bx = ExtractB(rgb)
if (matrix=="Rec2020CL" || matrix=="DCI-P3" || matrix=="Display-P3") {
YCbCr = RGB_to_YcCbcCrc( Rx, Gx, Bx, matrix, coef, tv_in, tv_out, fs)
Y = YCbCr[0] Cb = YCbCr[1] Cr = YCbCr[2]
} else {
scale_y = !tv_in && tv_out ? ( 219 / 255.) : 1.0
scale_uv = !tv_in && tv_out ? ( 112 / 255.) : 0.5
Kr = 1. - coef[0]
Kb = 1. - coef[2]
m0 = scale_y * coef[0] m1 = scale_y * coef[1] m2 = scale_y * coef[2]
m3 = scale_uv * (-coef[0] / Kb) m4 = scale_uv * (-coef[1] / Kb) m5 = scale_uv
m6 = scale_uv m7 = scale_uv * (-coef[1] / Kr) m8 = scale_uv * (-coef[2] / Kr)
UVf = bdpth < 32 ? "" : "range_half -"
rangeY_TV = tv_out ? tv_in ? "" : "ymin +" : tv_in ? "ymin - range_max ymax ymin - / *" : ""
rangeC_TV = !tv_out && tv_in ? "range_half - range_max cmax cmin - / * range_half + "+UVf+"" : UVf
Y = Expr(Rx, Gx, Bx, ex_dlut(" x "+string(m0)+" * y "+string(m1)+" * + z "+string(m2)+" * + "+rangeY_TV+"", bdpth, fs), optSingleMode=true)
Cb = Expr(Rx, Gx, Bx, ex_dlut("range_half x "+string(m3)+" * + y "+string(m4)+" * + z "+string(m5)+" * + "+rangeC_TV+"", bdpth, fs), optSingleMode=true)
Cr = Expr(Rx, Gx, Bx, ex_dlut("range_half x "+string(m6)+" * + y "+string(m7)+" * + z "+string(m8)+" * + "+rangeC_TV+"", bdpth, fs), optSingleMode=true)
}
point = kernel=="Point" ? "+1.0" : ""
cplaceH = ",src_left="+string(cplace[0])+point
cplaceV = ",src_top="+string(cplace[1])
resampler = kernel == "nnedi3" ? "nnedi3_resize16(" + String(nw+nw%2) + "," + String(nh+nh%2) + cplaceH + cplaceV + ")" : \
kernel == "bicubic"? "BicubicResize(" + String(nw+nw%2) + "," + String(nh+nh%2) + cplaceH + cplaceV + ",b="+bc_b+",c="+bc_c+")" : \
kernel == "gauss" ? "GaussResize(" + String(nw+nw%2) + "," + String(nh+nh%2) + cplaceH + cplaceV + ",p=p)" : \
kernel + "Resize(" + String(nw+nw%2) + "," + String(nh+nh%2) + cplaceH + cplaceV + ")"
Cb = p_type4 ? Cb : Eval("Cb." + resampler)
Cr = p_type4 ? Cr : Eval("Cr." + resampler)
CombinePlanes(Y, Cb, Cr, planes="YUV", pixel_type="YUV"+p_type[1]) }
# RGB to YcCbcCrc function (for Rec.2020CL)
function RGB_to_YcCbcCrc (clip Rx, clip Gx, clip Bx, string matrix, float_array coef, bool tv_in, bool tv_out, bool fulls) {
bi = BitsPerComponent(Rx)
UVf = bi < 32 ? "range_half +" : ""
rangeC_PC = tv_in ? "range_half - range_max cmax cmin - / * range_half + " : ""
rangeC_TV = tv_out ? "range_half + cmax cmin - range_max / * cmin + " : UVf
s_gam = moncurve_coef(matrix)
a = 1.0 + s_gam[1]
b = 1.0 / s_gam[0]
Pb = 1 / (2. * (a*(1.0 - pow(coef[2], b))))
Pr = 1 / (2. * (a*(1.0 - pow(coef[0], b))))
Nb = 1 / (-2. * (a*(1.0 - pow(coef[0] + coef[1], b)) - 1.0))
Nr = 1 / (-2. * (a*(1.0 - pow(coef[1] + coef[2], b)) - 1.0))
Rln = moncurve_f(Rx, s_gam[0], s_gam[1], tv_in, false, 1, fulls)
Gln = moncurve_f(Gx, s_gam[0], s_gam[1], tv_in, false, 1, fulls)
Bln = moncurve_f(Bx, s_gam[0], s_gam[1], tv_in, false, 1, fulls)
Y = Expr(Rln, Gln, Bln, "x "+string(coef[0])+" * y "+string(coef[1])+" * + z "+string(coef[2])+" * + ")
Yx = moncurve_r(Y, s_gam[0], s_gam[1], false, false, 1, fulls)
Cb = Expr(Bx, Yx, ex_dlut(Format("x "+rangeC_PC+" y - A@ 0 <= A {Nb} * A {Pb} * ? "+rangeC_TV+" "), bi, fulls))
Cr = Expr(Rx, Yx, ex_dlut(Format("x "+rangeC_PC+" y - A@ 0 <= A {Nr} * A {Pr} * ? "+rangeC_TV+" "), bi, fulls))
Y = !tv_out ? Yx : moncurve_r(Y, s_gam[0], s_gam[1], false, true, 1, fulls)
[Y, Cb, Cr] }
# YcCbcCrc to RGB function (for Rec.2020CL)
function YcCbcCrc_to_RGB (clip Y, clip Cb, clip Cr, string matrix, float_array coef, bool tv_in, bool tv_out, bool fulls) {
bi = BitsPerComponent(Y)
bdpth = bi < 32
UVf = bdpth ? "range_half -" : ""
UVf2 = bdpth ? "range_half +" : ""
rangeC_PC = tv_in ? ""+UVf+" range_max cmax cmin - / *" : UVf
s_gam = moncurve_coef(matrix)
a = 1.0 + s_gam[1]
b = 1.0 / s_gam[0]
Pb = 2. * (a*(1.0 - pow(coef[2], b)))
Pr = 2. * (a*(1.0 - pow(coef[0], b)))
Nb = -2. * (a*(1.0 - pow(coef[0] + coef[1], b)) - 1.0)
Nr = -2. * (a*(1.0 - pow(coef[1] + coef[2], b)) - 1.0)
Y = moncurve_f(Y, s_gam[0], s_gam[1], tv_in, false, 1, fulls)
Bx = Expr(Cb, Y, ex_dlut(Format("x "+rangeC_PC+" A@ 0 <= A {Nb} * y "+UVf+" B@ + A {Pb} * B + ? "+UVf2+""), bi, fulls))
Rx = Expr(Cr, Y, ex_dlut(Format("x "+rangeC_PC+" A@ 0 <= A {Nr} * y "+UVf+" B@ + A {Pr} * B + ? "+UVf2+""), bi, fulls))
Gx = Expr(Y, Bx, Rx, "x "+string(coef[2])+" y * - "+string(coef[0])+" z * - "+string(coef[1])+" / ", optSingleMode=true)
B = moncurve_r(Bx, s_gam[0], s_gam[1], false, tv_out, 1, fulls)
R = moncurve_r(Rx, s_gam[0], s_gam[1], false, tv_out, 1, fulls)
G = moncurve_r(Gx, s_gam[0], s_gam[1], false, tv_out, 1, fulls)
[R, G, B] }