View difference between Paste ID: hLtRabPq and VJyX8Teu
SHOW: | | - or go back to the newest paste.
1
#################################################################################################
2-
###### eedi3_resize16 v3.3.05 ###### by mawen1250 mod by A.SONY ######      2018.03.25      ######
2+
###### eedi3_resize16 v3.3.06 ###### by mawen1250 mod by A.SONY ######      2018.04.10      ######
3
#################################################################################################
4
###### from v2.8 on, eedi3_resize16 only supports AviSynth v2.6+                          ######
5
###### Requirements: masktools v2.0a48, dither v1.25.1, eedi3 v0.9.2.3,                     ######
6
######               SmoothAdjust v3.00, Contra-Sharpen mod 3.4 (only for sharp>0),        ######
7
######               FTurn v1.4 (not necessarily required but will improve speed)          ######
8
######               nnedi3 v0.9.4.19 (only for nnsclip=true)                              ######
9
#################################################################################################
10
###### accept Y8, YV12, YV16, YV24 input, use eedi3 and Dither_resize16(nr) for scaling   ######
11
###### output format can be 8bit/16bit Y8/YV12/YV16/YV24 or RGB32/RGB24/RGB48YV12/RGB48Y   ######
12
#################################################################################################
13
###### "lsb_in"/"lsb" defines if input/output clip is stacked-16bit, default is False      ######
14
###### "output" defines colorspace of output clip, for RGB output "lsb" is ignored         ######
15
#################################################################################################
16
###### when horizontal/vertical scale ratio >  "ratiothr", we assume it's upscaling        ######
17
###### when horizontal/vertical scale ratio <= "ratiothr", we assume it's downscaling      ######
18
###### by default ratiothr=1.125                                                           ######
19
###### eedi3 is only applied when upscaling                                               ######
20
###### Dither_resize16 is used after eedi3 upscaling to fix shift and scale to target res ######
21
###### we call the eedi3+Dither_resize16 scaling method "edge area resize"                ######
22
###### for downscaling, Dither_resize16 is applied instead, we call it "flat area resize"  ######
23
###### when mixed=True, eedi3 (edge area) is combined with Dither_resize16 (flat area)    ######
24
#################################################################################################
25
26
27
Function eedi3_resize16(clip input, int "target_width", int "target_height", float "src_left", float "src_top",
28
\                        float "src_width", float "src_height",
29
\                        string "kernel_d", string "kernel_u", float "f_d", float "f_u", int "taps",
30
\                        float "a1", float "a2", float "a3", bool "invks_d", bool "invks_u", int "invkstaps", bool "noring",
31
\                        int "nsize", int "nns", int "qual", int "etype", int "pscrn", int "threads",
32
\                        float "ratiothr", bool "mixed", float "thr", float "elast", float "sharp",
33
\                        string "output", bool "tv_range", string "cplace", string "matrix", string "curve", float "gcor",
34-
\                        int "Y", int "U", int "V", bool "lsb_in", bool "lsb", int "dither", bool "nnsclip")
34+
\                        int "Y", int "U", int "V", bool "lsb_in", bool "lsb", int "dither", bool "nnsclip", float "alpha",
35
\                        float "beta", float "gamma", int "nrad", int "mdis", bool "hp", bool "ucubic", bool "cost3", 
36
\                        int "vcheck", float "vthresh0", float "vthresh1", float "vthresh2", bool "nnrep")
37
{
38
    ###############################################################################################################################
39
    ###############################################################################################################################
40
    # Parameters for merging edge&flat upscaled clip
41
	
42
    mixed    = Default(mixed,    True   )   # eedi3_resize16 uses eedi3+Dither_resize16 for edge upscaling, this parameter defines whether to combine eedi3+Dither_resize16(edge area) and Dither_resize16(flat area) in upscaling, which achieves higher precision upscaling result(mainly for flat area).
43
    thr      = Default(thr,      1.0    )   # the same with "thr"   in Dither_limit_dif16, valid value range is (0, 10.0].
44
    elast    = Default(elast,    1.5    )   # the same with "elast" in Dither_limit_dif16, valid value range is [1, 10.0].
45
                                            # PDiff: pixel value diff between flat   clip and edge clip (edge clip as reference)
46
                                            # ODiff: pixel value diff between merged clip and edge clip (edge clip as reference)
47
                                            # PDiff, thr and elast is used to calculate ODiff:
48
                                            # ODiff = PDiff when [PDiff <= thr]
49
                                            # ODiff gradually smooths from thr to 0 when [thr <= PDiff <= thr * elast]
50
                                            # for elast>2.0, ODiff reaches maximum when [PDiff == thr * elast / 2]
51
                                            # ODiff = 0     when [PDiff >= thr * elast]
52
                                            #
53
                                            # Larger "thr"   will result in more pixels being taken from flat area upscaled clip (Dither_resize16)
54
                                            # Larger "thr"   will result in less pixels being taken from edge area upscaled clip (eedi3+Dither_resize16)
55
                                            # Larger "elast" will result in more pixels being blended from edge&flat area upscaled clip, for smoother merging
56
    
57
    ###############################################################################################################################
58
	###############################################################################################################################
59
    # Parameters for nnedi3
60
	
61
    nsize    = Default(nsize,    0      )
62
    nns      = Default(nns,      3      )
63
    qual     = Default(qual,     2      )
64
    etype    = Default(etype,    0      )
65
    pscrn    = Default(pscrn,    2      )
66
    threads  = Default(threads,  0      )
67
    nnsclip  = Default(nnsclip,  false  )
68
    
69
    ###############################################################################################################################
70
	###############################################################################################################################
71
    # Parameters for Dither_resize16
72
	
73
    kernel_d = Default(kernel_d, "Spline36" )   # "kernelh","kernelv" of Dither_resize16 used in downscaling
74
    kernel_u = Default(kernel_u, "Spline64" )   # "kernelh","kernelv" of Dither_resize16 used in upscaling
75
	f_d      = Default(f_d,      1.0        )   # "fh","fv" of Dither_resize16 used in downscaling
76
	f_u      = Default(f_u,      1.0        )   # "fh","fv" of Dither_resize16 used in upscaling
77
	taps     = Default(taps,     4          )   # "taps"    of Dither_resize16
78
	# a1, a2, a3                                # "a1","a2","a3"      of Dither_resize16
79
	invks_d  = Default(invks_d,  False      )   # "invksh","invksv"   of Dither_resize16 used in downscaling
80
	invks_u  = Default(invks_u,  False      )   # "invksh","invksv"   of Dither_resize16 used in upscaling
81
	invkstaps= Default(invkstaps,5          )   # "invkstaps"         of Dither_resize16
82
	
83
    noring   = Default(noring,   False      )   # True use non-ringing algorithm of Dither_resize16 in flat area scaling
84
                                                # It actually doesn't make much sense for eedi3_resize16(which uses eedi3 for upscaling),
85
                                                # while it may produce blurring and aliasing when downscaling.
86
                                                # You'd better not set it to True unless you know what you are doing.
87
    resste   = "Dither_resize16"
88
    resstf   = noring ? "Dither_resize16nr" : "Dither_resize16"
89
    
90
    ###############################################################################################################################
91
	###############################################################################################################################
92
    # Post-Process
93
	
94
    sharp    = Default(sharp,    0      )   # Strength of Contra-Sharpen mod, for sharper edge. 0 means no sharpening, common value is about 100.
95
                                            # *Only* when {horrizontal or vertical}{scale ratio}>{ratiothr} will sharpening take effect (when eedi3 is used for upscaling).
96
    
97
    ###############################################################################################################################
98
	###############################################################################################################################
99
    # Parameters for input/output
100
	
101
    Y        = Default(Y,        3      )
102
    U        = Default(U,        3      )
103
    V        = Default(V,        3      )
104
    lsb_in   = Default(lsb_in,   False  )   # input  clip is 16-bit stacked or not
105
    lsb      = Default(lsb,      False  )   # output clip is 16-bit stacked or not
106
    tv_range = Default(tv_range, True   )   # input  clip is TV-range or PC-range
107
    dither   = tv_range ? Default(dither, 6) : Default(dither, 50)    # dither mode for 16-bit to 8-bit conversion
108
    
109
    sCSP     = input.eedi3_resize16_GetCSP()
110
    Assert(sCSP=="Y8" || sCSP=="YV12" || sCSP=="YV16" || sCSP=="YV24", """eedi3_resize16: only accept Y8, YV12, YV16, YV24 input""")
111
    
112
    output   = Default(output,   sCSP   )
113
    # Output format. Possible values are:
114
    # "Y8"       : Regular Y8    colorspace. Parameter "lsb" works on this output mode.
115
    # "YV12"     : Regular YV12  colorspace. Parameter "lsb" works on this output mode.
116
    # "YV16"     : Regular YV16  colorspace. Parameter "lsb" works on this output mode.
117
    # "YV24"     : Regular YV24  colorspace. Parameter "lsb" works on this output mode.
118
    # "RGB32"    : Regular RGB32 colorspace.
119
    # "RGB24"    : Regular RGB24 colorspace.
120
    # "RGB48YV12": 48-bit RGB conveyed on YV12. Use it for rawvideo export only. Not suitable for display or further processing (it will look like garbage).
121
    # "RGB48Y"   : 48-bit RGB. The components R, G and B are conveyed on three YV12 or Y8 (if supported) stack16 clips interleaved on a frame basis.
122
    
123
	###############################################################################################################################
124
	
125
    IsY8     = sCSP == "Y8" || output == "Y8"
126
    sCSP     = IsY8 ? "YV24" : sCSP
127
    
128
    IsRGB    = LeftStr(output, 3) == "RGB"
129
    oCSP     = IsRGB || IsY8 ? "YV24" : output
130
    
131
    Y        = min(Y, 4)
132
    U        = IsY8 ? 1 : min(U, 4)
133
    V        = IsY8 ? 1 : min(V, 4)
134
    Yt       = Y == 3 || Y == 4
135
    Ut       = U == 3 || U == 4
136
    Vt       = V == 3 || V == 4
137
    Y31      = Yt ? 3 : 1
138
    U31      = Ut ? 3 : 1
139
    V31      = Vt ? 3 : 1
140
    Y32      = Yt ? 3 : Y
141
    U32      = Ut ? 3 : U
142
    V32      = Vt ? 3 : V
143
    Y21      = Yt ? 2 : Y
144
    U21      = Ut ? 2 : U
145
    V21      = Vt ? 2 : V
146
    Y321     = Y > 1 ? 3 : Y
147
    U321     = U > 1 ? 3 : U
148
    V321     = V > 1 ? 3 : V
149
    
150
    sw       = input.Width ()
151
    sh       = input.Height()
152
    sh       = lsb_in ? sh/2 : sh
153
    swc      = sCSP=="YV24" ? sw   : sw/2
154
    shc      = sCSP=="YV12" ? sh/2 : sh
155
    HD       = (sw > 1024 || sh > 576) ? True : False
156
    
157
	###############################################################################################################################
158
	
159
    cplace   = Default(cplace,   "MPEG2")
160
    # Placement of the chroma subsamples. Can be one of these strings:
161
    # "MPEG1": 4:2:0 subsampling used in MPEG-1. Chroma samples are located on the center of each group of 4 pixels.
162
    # "MPEG2": Subsampling used in MPEG-2 4:2:x. Chroma samples are located on the left pixel column of the group.
163
    
164
    matrix   = Default(matrix,   HD ? "709" : "601")
165
    # The matrix used to convert the YUV pixels to computer RGB. Possible values are:
166
    # "601"  : ITU-R BT.601 / ITU-R BT.470-2 / SMPTE 170M. For Standard Definition content.
167
    # "709"  : ITU-R BT.709. For High Definition content.
168
    # "240"  : SMPTE 240M
169
    # "FCC"  : FCC
170
    # "YCgCo": YCgCo
171
    # When the parameter is not defined, ITU-R BT.601 and ITU-R BT.709 are automatically selected depending on the clip definition.
172
    
173
    curve    = Default(curve,    "linear"   )
174
    # type of gamma mapping(transfer characteristic) for gamma-aware resize(only take effects for Dither_resize16 processing parts)
175
    # possible values:
176
    # "709"    - ITU-R BT.709 transfer curve for digital video
177
    # "601"    - ITU-R BT.601 transfer curve, same as "709"
178
    # "170"    - SMPTE 170M, same as "709"
179
    # "240"    - SMPTE 240M (1987)
180
    # "srgb"   - sRGB curve
181
    # "2020"   - ITU-R BT.2020 transfer curve, for 12-bit content. For sources of lower bitdepth, use the "709" curve.
182
    # "linear" - linear curve without gamma-aware processing
183
    curve    = Yt ? curve : "linear"
184
    gcor     = Default(gcor,     1.0    )   # Gamma correction, applied on the linear part.
185
    
186
    ###############################################################################################################################
187
	###############################################################################################################################
188
    # Parameters for scaling
189
	
190
    ow       = Default(target_width,  sw)
191
    oh       = Default(target_height, sh)
192
    owc      = oCSP=="YV24" ? ow   : ow/2
193
    ohc      = oCSP=="YV12" ? oh/2 : oh
194
    Assert(!(output=="YV16" && ow!=owc*2), """eedi3_resize16: width  of YV16 output clip must be MOD2!""")
195
    Assert(!(output=="YV12" && ow!=owc*2), """eedi3_resize16: width  of YV12 output clip must be MOD2!""")
196
    Assert(!(output=="YV12" && oh!=ohc*2), """eedi3_resize16: height of YV12 output clip must be MOD2!""")
197
    
198
    src_left    = Default(src_left,   0 )
199
    src_top     = Default(src_top,    0 )
200
    src_width   = Default(src_width,  sw)
201
    src_height  = Default(src_height, sh)
202
    
203
    ###############################################################################################################################
204
	###############################################################################################################################
205
    # Pre-Cropping/Padding Calculation
206
	
207
    prel        = int(src_left/2) * 2
208
    pret        = int(src_top /2) * 2
209
    prer        = int((src_width > 0 ? -sw+src_left+src_width : src_width )/2) * 2
210
    preb        = int((src_height> 0 ? -sh+src_top+src_height : src_height)/2) * 2
211
    prew        = sw - prel + prer
212
    preh        = sh - pret + preb
213
    
214
    sCSP=="YV24" ? \
215
    Eval("""
216
        swmod2      = sw  /2*2 == sw
217
        pwmod2      = prew/2*2 == prew
218
        wpre        = prew < sw
219
        prel        = wpre ?          prel            :          0
220
        prer        = wpre ? pwmod2 ? prer : prer + 1 : swmod2 ? 0 : 1
221
        prew        = sw - prel + prer
222
        wpre        = prew < sw || !swmod2
223
    """) : \
224
    Eval("""
225
        swmod4      = sw  /4*4 == sw
226
        pwmod4      = prew/4*4 == prew
227
        wpre        = prew < sw
228
        prel        = wpre ?          prel            :          0
229
        prer        = wpre ? pwmod4 ? prer : prer + 2 : swmod4 ? 0 : 2
230
        prew        = sw - prel + prer
231
        wpre        = prew < sw || !swmod4
232
    """)
233
    
234
    sCSP=="YV12" ? \
235
    Eval("""
236
        shmod4      = sh  /4*4 == sh
237
        phmod4      = preh/4*4 == preh
238
        hpre        = preh < sh
239
        pret        = hpre ?          pret            :          0
240
        preb        = hpre ? phmod4 ? preb : preb + 2 : shmod4 ? 0 : 2
241
        preh        = sh - pret + preb
242
        hpre        = preh < sh || !shmod4
243
    """) : \
244
    Eval("""
245
        shmod2      = sh  /2*2 == sh
246
        phmod2      = preh/2*2 == preh
247
        hpre        = preh < sh
248
        pret        = hpre ?          pret            :          0
249
        preb        = hpre ? phmod2 ? preb : preb + 1 : shmod2 ? 0 : 1
250
        preh        = sh - pret + preb
251
        hpre        = preh < sh || !shmod2
252
    """)
253
    
254
    src_width   = src_width <=0 ? +sw-src_left+src_width : src_width
255
    src_height  = src_height<=0 ? +sh-src_top+src_height : src_height
256
    src_left    = wpre ? src_left-prel : src_left
257
    src_top     = hpre ? src_top -pret : src_top
258
    
259
    src_leftc   = sCSP=="YV24" ? src_left      : src_left  /2.
260
    src_topc    = sCSP=="YV12" ? src_top   /2. : src_top
261
    src_widthc  = sCSP=="YV24" ? src_width     : src_width /2.
262
    src_heightc = sCSP=="YV12" ? src_height/2. : src_height
263
    
264
    ###############################################################################################################################
265
	###############################################################################################################################
266
    # Scaling Ratio Calculation
267
	
268
    ratiothr = Default(ratiothr,   1.125)    # When scale ratio is larger than ratiothr, use eedi3+Dither_resize16 upscale method instead of pure Dither_resize16.
269
	# when horizontal/vertical scale ratio >  "ratiothr", we assume it's upscaling
270
    # when horizontal/vertical scale ratio <= "ratiothr", we assume it's downscaling
271
    
272
	###############################################################################################################################
273
	
274
    yhratio  = float(ow ) / float(src_width  )
275
    yvratio  = float(oh ) / float(src_height )
276
    chratio  = float(owc) / float(src_widthc )
277
    cvratio  = float(ohc) / float(src_heightc)
278
    
279
    enable   = yhratio!=1 || yvratio!=1 || chratio!=1 || cvratio!=1 ||
280
    \          src_width  !=int(src_width  ) || src_height !=int(src_height ) || src_widthc !=int(src_widthc ) || src_heightc!=int(src_heightc) ||
281
    \          src_left   !=int(src_left   ) || src_top    !=int(src_top    ) || src_leftc  !=int(src_leftc  ) || src_topc   !=int(src_topc   )
282
    
283
    yhct     = yhratio>ratiothr ? Ceil( log(yhratio/ratiothr) / log(2) ) : 0
284
    yhrf     = int(Pow(2, yhct))
285
    yrhratio = yhratio/yhrf
286
    yvct     = yvratio>ratiothr ? Ceil( log(yvratio/ratiothr) / log(2) ) : 0
287
    yvrf     = int(Pow(2, yvct))
288
    yrvratio = yvratio/yvrf
289
    
290
    chct     = chratio>ratiothr ? Ceil( log(chratio/ratiothr) / log(2) ) : 0
291
    chrf     = int(Pow(2, chct))
292
    crhratio = chratio/chrf
293
    cvct     = cvratio>ratiothr ? Ceil( log(cvratio/ratiothr) / log(2) ) : 0
294
    cvrf     = int(Pow(2, cvct))
295
    crvratio = cvratio/cvrf
296
    
297
    nonenny  = yhct<=0 && yvct<=0
298
    nonennc  = chct<=0 && cvct<=0
299
    nonenn   = nonenny || nonennc
300
    
301
    Ynnt     = Yt&&!nonenny
302
    Unnt     = Ut&&!nonennc
303
    Vnnt     = Vt&&!nonennc
304
    Ynn31    = Ynnt ? 3 : 1
305
    Unn31    = Unnt ? 3 : 1
306
    Vnn31    = Vnnt ? 3 : 1
307
    Ynn      = Yt&&nonenny ? 2 : Y
308
    Unn      = Ut&&nonennc ? 2 : U
309
    Vnn      = Vt&&nonennc ? 2 : V
310
    
311
    nnt      = Ynnt || Unnt || Vnnt
312
    mixed    = !nnt || !enable ? False : mixed
313
    
314
    ###############################################################################################################################
315
    ###############################################################################################################################
316
	# Shift Calculation
317
	
318
    yhshift  = yhrf>=2 ? 0.5 : 0       #              luma   horizontal shift introduced by eedi3_resize16_rpow2 (edge area resize)
319
    yvshift  = yvrf>=2 ? 0.5 : 0       #              luma     vertical shift introduced by eedi3_resize16_rpow2 (edge area resize)
320
    yhfix    = -yhshift                # value to fix luma   horizontal shift introduced by eedi3_resize16_rpow2 (edge area resize)
321
    yvfix    = -yvshift                # value to fix luma     vertical shift introduced by eedi3_resize16_rpow2 (edge area resize)
322
    
323
    chshift  = oCSP=="YV24" ? sCSP=="YV24" ?                   chrf>=2 ? 0.50         :  0
324
    \                                      : cplace=="MPEG1" ? chrf>=2 ? 0.50         :  0
325
    \                                                        : chrf>=2 ? 0.50-chrf/4. : -0.25
326
    \                       : sCSP=="YV24" ? cplace=="MPEG1" ? chrf>=2 ? 0.50         :  0
327
    \                                                        : chrf>=2 ? 0.75         :  0.25
328
    \                                      : cplace=="MPEG1" ? chrf>=2 ? 0.50         :  0
329
    \                                                        : chrf>=2 ? 0.75-chrf/4. :  0
330
	#                                                         (chrf/2-0.5)/2-(chrf/2-1)
331
	#                                                 chroma horizontal shift introduced by eedi3_resize16_rpow2 (edge area resize)
332
    cvshift  = cvrf>=2 ? 0.5 : 0       #              chroma   vertical shift introduced by eedi3_resize16_rpow2 (edge area resize)
333
    chfix    = -chshift                # value to fix chroma horizontal shift introduced by eedi3_resize16_rpow2 (edge area resize)
334
    cvfix    = -cvshift                # value to fix chroma   vertical shift introduced by eedi3_resize16_rpow2 (edge area resize)
335
    
336
    cphfixe  = oCSP=="YV24" ?                   0
337
    \                       : cplace=="MPEG1" ? 0
338
    \                                         : 0.25-0.25/crhratio
339
	# value to fix chroma horizontal shift introduced by Dither_resize16 after eedi3_resize16_rpow2 processing   (edge area resize)
340
    
341
    cphfix   = oCSP=="YV24" ? sCSP=="YV24" ?                   0
342
    \                                      : cplace=="MPEG1" ? 0
343
    \                                                        : 0.25
344
    \                       : sCSP=="YV24" ? cplace=="MPEG1" ? 0
345
    \                                                        : -0.5
346
    \                                      : cplace=="MPEG1" ? 0
347
    \                                                        : 0.25-0.25/chratio
348
	# value to fix chroma horizontal shift introduced by Dither_resize16 (flat area resize)
349
	# this fixing only take effects when Y/U/V is processed separately(as Y8) in flat area scaling (colorspace transferring)
350
	# Dither_resize16 accepts "cplace" and correctly processes chroma placement for YUV input
351
    
352
    ###############################################################################################################################
353
	###############################################################################################################################
354
    # Pre-Process
355
	
356
    input    = wpre || hpre ? lsb_in ? input.Dither_resize16(wpre?prew:sw, hpre?preh:sh, wpre?prel:0, hpre?pret:0,
357
    \                                                        wpre?prew:sw, hpre?preh:sh, kernel="point")
358
    \                                : input.PointResize(wpre?prew:sw, hpre?preh:sh, wpre?prel:0, hpre?pret:0, wpre?prew:sw, hpre?preh:sh)
359
    \                       : input
360
    input8   = lsb_in ? input.eedi3_resize16_Down8(tv_range, Yt, Ut, Vt, mixed ? -1 : dither) : input
361
    input16  = lsb_in ? input : input.eedi3_resize16_U16(tv_range, True, True, True)
362
    
363
    ###############################################################################################################################
364
	###############################################################################################################################
365
    # eedi3 upscale for edge area
366
	
367
    !(enable && nnt) ? NOP() : \
368-
        edgenn  = input8.eedi3_resize16_rpow2(yvct, yhct, 1, 1, Yt, Ut, Vt, nsize, nns, qual, etype, pscrn, threads, nnsclip=nnsclip)
368+
369
    Eval("""
370
        edgenn  = input8.eedi3_resize16_rpow2(yvct, yhct, 1, 1, Yt, Ut, Vt, nsize, nns, qual, etype, pscrn, threads, false, nnsclip, alpha, beta, gamma, nrad, mdis, hp, ucubic, cost3, vcheck, vthresh0, vthresh1, vthresh2, nnrep)
371
    	edgennY = sharp>0 ? edgenn.CSmod(chroma=False, ss_w=1.00, ss_h=1.00, preblur=-4, Smethod=1, kernel=6, strength=sharp, Soothe=-1)
372
        \                         .eedi3_resize16_U16(tv_range, Yt, Ut, Vt)
373
    	\                 : edgenn.eedi3_resize16_U16(tv_range, Yt, Ut, Vt)
374
    	edgennU = edgennY.UToY8()
375
    	edgennV = edgennY.VToY8()
376
    	edgennY = edgennY.ConvertToY8()
377
    """) : \
378
    Eval("""
379-
        \                                                                        nsize, nns, qual, etype, pscrn, threads, nnsclip=nnsclip, ryv12=isyv12(input8))
379+
380
    	\               : sharp>0 ? input8.ConvertToY8().eedi3_resize16_rpow2(yvct, yhct, 1, 1, Yt, False, False,
381
        \                                                                        nsize, nns, qual, etype, pscrn, threads, false, nnsclip, alpha, beta, gamma, nrad, mdis, hp, ucubic, cost3, vcheck, vthresh0, vthresh1, vthresh2, nnrep)
382
        \                                 .CSmod(chroma=False, ss_w=1.00, ss_h=1.00, preblur=-4, Smethod=1, kernel=6, strength=sharp, Soothe=-1)
383-
        \                                                                        nsize, nns, qual, etype, pscrn, threads, nnsclip=nnsclip, ryv12=isyv12(input8))
383+
384
        \                         : input8.ConvertToY8().eedi3_resize16_rpow2(yvct, yhct, 1, 1, Yt, False, False,
385
        \                                                                        nsize, nns, qual, etype, pscrn, threads, false, nnsclip, alpha, beta, gamma, nrad, mdis, hp, ucubic, cost3, vcheck, vthresh0, vthresh1, vthresh2, nnrep)
386
    	\                                 .eedi3_resize16_U16(tv_range, Yt, False, False)
387-
        \                                                            nsize, nns, qual, etype, pscrn, threads, nnsclip=nnsclip, ryv12=isyv12(input8))
387+
388
    	\               : input8.UToY8().eedi3_resize16_rpow2(cvct, chct, 1, 1, Ut, False, False,
389
        \                                                            nsize, nns, qual, etype, pscrn, threads, false, nnsclip, alpha, beta, gamma, nrad, mdis, hp, ucubic, cost3, vcheck, vthresh0, vthresh1, vthresh2, nnrep)
390
    	\                       .eedi3_resize16_U16(tv_range, Ut, False, False)
391-
        \                                                            nsize, nns, qual, etype, pscrn, threads, nnsclip=nnsclip, ryv12=isyv12(input8))
391+
392
    	\               : input8.VToY8().eedi3_resize16_rpow2(cvct, chct, 1, 1, Vt, False, False,
393
        \                                                            nsize, nns, qual, etype, pscrn, threads, false, nnsclip, alpha, beta, gamma, nrad, mdis, hp, ucubic, cost3, vcheck, vthresh0, vthresh1, vthresh2, nnrep)
394
    	\                       .eedi3_resize16_U16(tv_range, Vt, False, False)
395
    """)
396
    
397
    ###############################################################################################################################
398
	###############################################################################################################################
399
    # edge area resize & fix center shift
400
	
401
	yrh      = yrhratio>ratiothr
402
	yrv      = yrvratio>ratiothr
403
	crh      = crhratio>ratiothr
404
	crv      = crvratio>ratiothr
405
	
406
    !(enable && nnt) ? NOP() : \
407
    Eval("""
408
        edgennY = !Ynnt ? NOP()
409
        \               : curve=="linear" ? edgennY
410
        \                                 : edgennY.Dither_y_gamma_to_linear(tv_range, tv_range, curve, u=1, v=1, gcor=gcor)
411
        edgeY   = !Ynnt ? input16.BlankClip(width=ow , height=oh *2, pixel_type="Y8", color_yuv=$008080)
412
    	\               : edgennY.""" + resste + """(ow , oh , src_left *yhrf+yhfix        , src_top *yvrf+yvfix,
413
        \                                            src_width *yhrf, src_height *yvrf, y=Y31, u=1  , v=1  ,
414
        \                                            kernelh=yrh?kernel_u:kernel_d, kernelv=yrv?kernel_u:kernel_d,
415
		\                                            fh=yrh?f_u:f_d, fv=yrv?f_u:f_d,
416
		\                                            invksh=yrh?invks_u:invks_d, invksv=yrv?invks_u:invks_d,
417
		\                                            taps=taps, a1=a1, a2=a2, a3=a3, invkstaps=invkstaps
418
		\                                           )
419
        edgeY   = !Ynnt ? edgeY
420
        \               : curve=="linear" ? edgeY
421
        \                                 : edgeY  .Dither_y_linear_to_gamma(tv_range, tv_range, curve, u=1, v=1, gcor=gcor)
422
        
423
    	edgeU   = !Unnt ? input16.BlankClip(width=owc, height=ohc*2, pixel_type="Y8", color_yuv=$008080)
424
    	\               : edgennU.""" + resste + """(owc, ohc, src_leftc*chrf+chfix+cphfixe, src_topc*cvrf+cvfix,
425
        \                                            src_widthc*chrf, src_heightc*cvrf, y=U31, u=1  , v=1  ,
426
        \                                            kernelh=crh?kernel_u:kernel_d, kernelv=crv?kernel_u:kernel_d,
427
		\                                            fh=crh?f_u:f_d, fv=crv?f_u:f_d,
428
		\                                            invksh=crh?invks_u:invks_d, invksv=crv?invks_u:invks_d,
429
		\                                            taps=taps, a1=a1, a2=a2, a3=a3, invkstaps=invkstaps
430
		\                                           )
431
		
432
    	edgeV   = !Vnnt ? input16.BlankClip(width=owc, height=ohc*2, pixel_type="Y8", color_yuv=$008080)
433
    	\               : edgennV.""" + resste + """(owc, ohc, src_leftc*chrf+chfix+cphfixe, src_topc*cvrf+cvfix,
434
        \                                            src_widthc*chrf, src_heightc*cvrf, y=V31, u=1  , v=1  ,
435
        \                                            kernelh=crh?kernel_u:kernel_d, kernelv=crv?kernel_u:kernel_d,
436
		\                                            fh=crh?f_u:f_d, fv=crv?f_u:f_d,
437
		\                                            invksh=crh?invks_u:invks_d, invksv=crv?invks_u:invks_d,
438
		\                                            taps=taps, a1=a1, a2=a2, a3=a3, invkstaps=invkstaps
439
		\                                           )
440
		
441
    	edge16  = IsY8 ? edgeY : YToUV(edgeU, edgeV, edgeY)
442
    """)
443
    
444
    ###############################################################################################################################
445
	###############################################################################################################################
446
    # flat area resize
447
448
	yh       = yhratio>ratiothr
449
	yv       = yvratio>ratiothr
450
	ch       = chratio>ratiothr
451
	cv       = cvratio>ratiothr
452
	
453
    !(enable && (mixed || !(Ynnt && Unnt && Vnnt))) ? NOP() : \
454
    yhratio==chratio && yvratio==cvratio && (!mixed || (Ynnt && Unnt && Vnnt)) ? \
455
    Eval("""
456
        flat16  = curve=="linear" ? input16
457
        \                         : input16.Dither_y_gamma_to_linear(tv_range, tv_range, curve, u=U21, v=V21, gcor=gcor)
458
        flat16  =                   flat16.      """ + resstf + """
459
		\                                           (ow , oh , src_left                    , src_top            ,
460
        \                                            src_width      , src_height      , y=Y32, u=U32, v=V32, cplace=cplace,
461
        \                                            kernelh=yh?kernel_u:kernel_d, kernelv=yv?kernel_u:kernel_d,
462
		\                                            fh=yh?f_u:f_d, fv=yv?f_u:f_d,
463
		\                                            invksh=yh?invks_u:invks_d, invksv=yv?invks_u:invks_d,
464
		\                                            taps=taps, a1=a1, a2=a2, a3=a3, invkstaps=invkstaps
465
		\                                           )
466
        flat16  = curve=="linear" ? flat16
467
        \                         : flat16 .Dither_y_linear_to_gamma(tv_range, tv_range, curve, u=U21, v=V21, gcor=gcor)
468
    """) : \
469
    Eval("""
470
        flatY   = curve=="linear"        ? input16.ConvertToY8() :
471
        \         (mixed || !Ynnt) && Yt ? input16.ConvertToY8().Dither_y_gamma_to_linear(tv_range, tv_range, curve, u=1  , v=1  , gcor=gcor)
472
        \                                : input16.ConvertToY8()
473
        flatY   = (mixed || !Ynnt) && Yt ? flatY          .""" + resstf + """
474
		\                                           (ow , oh , src_left                    , src_top            ,
475
        \                                            src_width      , src_height      , y=Y32, u=1  , v=1  ,
476
        \                                            kernelh=yh?kernel_u:kernel_d, kernelv=yv?kernel_u:kernel_d,
477
		\                                            fh=yh?f_u:f_d, fv=yv?f_u:f_d,
478
		\                                            invksh=yh?invks_u:invks_d, invksv=yv?invks_u:invks_d,
479
		\                                            taps=taps, a1=a1, a2=a2, a3=a3, invkstaps=invkstaps
480
		\                                           )
481
        \                                : input16.BlankClip(width=ow , height=oh *2, pixel_type="Y8", color_yuv=$008080)
482
        flatY   = curve=="linear"        ? flatY                 :
483
        \         (mixed || !Ynnt) && Yt ? flatY                .Dither_y_linear_to_gamma(tv_range, tv_range, curve, u=1  , v=1  , gcor=gcor)
484
        \                                : flatY
485
        
486
        flatU   = (mixed || !Unnt) && Ut ? input16.UToY8().""" + resstf + """
487
		\                                           (owc, ohc, src_leftc           +cphfix , src_topc           ,
488
        \                                            src_widthc     , src_heightc     , y=U32, u=1  , v=1  ,
489
        \                                            kernelh=ch?kernel_u:kernel_d, kernelv=cv?kernel_u:kernel_d,
490
		\                                            fh=ch?f_u:f_d, fv=cv?f_u:f_d,
491
		\                                            invksh=ch?invks_u:invks_d, invksv=cv?invks_u:invks_d,
492
		\                                            taps=taps, a1=a1, a2=a2, a3=a3, invkstaps=invkstaps
493
		\                                           )
494
        \                                : input16.BlankClip(width=owc, height=ohc*2, pixel_type="Y8", color_yuv=$008080)
495
		
496
        flatV   = (mixed || !Vnnt) && Vt ? input16.VToY8().""" + resstf + """
497
		\                                           (owc, ohc, src_leftc           +cphfix , src_topc           ,
498
        \                                            src_widthc     , src_heightc     , y=V32, u=1  , v=1  ,
499
        \                                            kernelh=ch?kernel_u:kernel_d, kernelv=cv?kernel_u:kernel_d,
500
		\                                            fh=ch?f_u:f_d, fv=cv?f_u:f_d,
501
		\                                            invksh=ch?invks_u:invks_d, invksv=cv?invks_u:invks_d,
502
		\                                            taps=taps, a1=a1, a2=a2, a3=a3, invkstaps=invkstaps
503
		\                                           )
504
        \                                : input16.BlankClip(width=owc, height=ohc*2, pixel_type="Y8", color_yuv=$008080)
505
		
506
    	flat16  = IsY8 ? flatY : YToUV(flatU, flatV, flatY)
507
    """)
508
    
509
    ###############################################################################################################################
510
	###############################################################################################################################
511
    # Threshold Merging & Output
512
	
513
    enable ? \
514
    Eval("""
515
        merge16  = !nnt ? flat16
516
        \               : mixed ? Dither_limit_dif16(flat16, edge16, thr=thr, elast=elast, y=Ynn, u=Unn, v=Vnn)
517
        \                       : Ynnt==Unnt && Unnt==Vnnt || IsY8 ? edge16
518
        \                                                          : mt_lutxy(edge16, flat16, Y=Yt?Ynnt?2:4:1, U=Ut?Unnt?2:4:1, V=Vt?Vnnt?2:4:1)
519
        merge16  = IsY8 ? output=="Y8" ? merge16.ConvertToY8() : Eval("merge16.ConvertTo"+oCSP).Dither_lut16(Y=2, U=-32768, V=-32768) : merge16
520
        
521
        final    = IsRGB ? merge16.Dither_convert_yuv_to_rgb(matrix=matrix, tv_range=tv_range, lsb_in=True, mode=dither, output=output)
522
        \                : lsb ? merge16
523
        \                      : merge16.eedi3_resize16_Down8(tv_range, True, !IsY8, !IsY8, dither)
524
    """) : \
525
    Eval("""
526
        shift16  = input16.Dither_resize16(ow, oh, src_left, src_top, src_width, src_height, kernel="point", y=Y, u=U, v=V)
527
        shift16  = IsY8 ? output=="Y8" ? shift16.ConvertToY8() : Eval("shift16.ConvertTo"+oCSP).Dither_lut16(Y=2, U=-32768, V=-32768) : shift16
528
        
529
        final    = IsRGB ? shift16.Dither_convert_yuv_to_rgb(matrix=matrix, tv_range=tv_range, lsb_in=True, mode=dither, output=output)
530
        \                : lsb ? shift16
531
        \                      : shift16.eedi3_resize16_Down8(tv_range, True, !IsY8, !IsY8, dither)
532
    """)
533
    
534
    
535
    return final
536
}
537
538-
\                              int "nsize", int "nns", int "qual", int "etype", int "pscrn", int "threads", bool "honly", bool "nnsclip", bool "ryv12")
538+
539
Function eedi3_resize16_rpow2(clip input, int "vct", int "hct", int "vfield", int "hfield", bool "Y", bool "U", bool "V",
540
\                              int "nsize", int "nns", int "qual", int "etype", int "pscrn", int "threads", bool "honly", bool "nnsclip", float "alpha", float "beta", float "gamma", int "nrad", int "mdis", bool "hp", bool "ucubic", bool "cost3", int "vcheck", float "vthresh0", float "vthresh1", float "vthresh2", bool "nnrep")
541
{
542
    vct      = Default(vct,      1      )
543
    hct      = Default(hct,      1      )
544
    vfield   = Default(vfield,   1      )
545
    hfield   = Default(hfield,   1      )
546
    Y        = Default(Y,        True   )
547
    U        = Default(U,        False  )
548
    V        = Default(V,        False  )
549-
    ryv12    = Default(ryv12,    True   )
549+
550
    nnsclip  = Default(nnsclip,  false  )
551
    nnrep    = Default(nnrep,    false  )
552
    
553
    
554
    input
555
    
556
    hct >= 1 ? \
557
    Eval("""
558-
        (nnsclip) ? eedi3(hfield, True, Y, U, V, threads=threads, sclip= ryv12 ? nnedi3(hfield, True, Y, U, V, nsize, nns, qual, etype, pscrn, threads) : nnedi3(hfield, True, Y, U, V, nsize, nns, qual, etype, pscrn, threads)) : eedi3(hfield, True, Y, U, V, threads=threads)
558+
559
        \       : Eval(" try { fturnright(chroma=U||V, mt=threads!=1) } catch(error_msg) { TurnRight() } ")
560
        nnec = (nnrep || nnsclip) ? nnedi3(hfield, True, Y, U, V, nsize, nns, qual, etype, pscrn, threads) : nop()
561
        eedi3(hfield, True, Y, U, V, alpha, beta, gamma, nrad, mdis, hp, ucubic, cost3, vcheck, vthresh0, vthresh1, vthresh2, nnsclip ? nnec : undefined, threads)
562
        nnrep ? Repair(nnec,9) : last
563
        
564
    	hct    = hct - 1
565
        honly  = hct >= 1
566
        hfield = 0
567
        
568
    	(honly) ? last
569
        \       : Eval(" try { fturnleft (chroma=U||V, mt=threads!=1) } catch(error_msg) { TurnLeft () } ")
570-
        (nnsclip) ? eedi3(vfield, True, Y, U, V, threads=threads, sclip= ryv12 ? nnedi3(vfield, True, Y, U, V, nsize, nns, qual, etype, pscrn, threads) : nnedi3(vfield, True, Y, U, V, nsize, nns, qual, etype, pscrn, threads)) : eedi3(vfield, True, Y, U, V, threads=threads)
570+
571
    
572
    vct >= 1 && !honly ? \
573
    Eval("""
574
        nnec = (nnrep || nnsclip) ? nnedi3(vfield, True, Y, U, V, nsize, nns, qual, etype, pscrn, threads) : nop()
575
        eedi3(vfield, True, Y, U, V, alpha, beta, gamma, nrad, mdis, hp, ucubic, cost3, vcheck, vthresh0, vthresh1, vthresh2, nnsclip ? nnec : undefined, threads)
576
        nnrep ? Repair(nnec,9) : last
577
        
578-
    \                                     : last.eedi3_resize16_rpow2(vct, hct, vfield, hfield, Y, U, V, nsize, nns, qual, etype, pscrn, threads, honly, nnsclip)
578+
579
        vfield = 0
580
    """) : NOP()
581
    
582
    
583
    return Y||U||V ? vct <= 0 && hct <= 0 ? last
584
    \                                     : last.eedi3_resize16_rpow2(vct, hct, vfield, hfield, Y, U, V, nsize, nns, qual, etype, pscrn, threads, honly, nnsclip, alpha, beta, gamma, nrad, mdis, hp, ucubic, cost3, vcheck, vthresh0, vthresh1, vthresh2, nnrep)
585
    \              : input
586
}
587
588
589
Function eedi3_resize16_GetCSP(clip c)
590
{
591
    return c.IsPlanar ? c.IsYV12 ? "YV12" :
592
    \                   c.IsYV16 ? "YV16" :
593
    \                   c.IsYV24 ? "YV24" : c.GetCSP_Y8_YV411fed :
594
    \      c.IsYUY2   ? "YUY2"   :
595
    \      c.IsRGB32  ? "RGB32"  :
596
    \      c.IsRGB24  ? "RGB24"  : "Unknown"
597
    
598
    
599
    Function GetCSP_Y8_YV411fed(clip c) {
600
        try {
601
            c.UtoY
602
            csp = "YV411"
603
        } catch ( error_msg ) {
604
        csp = "Y8"
605
        }
606
        return csp
607
    }
608
}
609
610
/*
611
Function eedi3_resize16_RemoveGrain(clip input, int "mode", int "modeU", int "modeV")
612
{
613
    mode     = Default(mode,     1      )
614
    modeU    = Default(modeU,    mode   )
615
    modeV    = Default(modeV,    modeU  )
616
    
617
    
618
    iCSP     = input.eedi3_resize16_GetCSP()
619
    isYV12   = iCSP == "YV12"
620
    isY8     = iCSP == "Y8"
621
    
622
    sw       = input.Width ()
623
    sh       = input.Height()
624
    wmod4    = sw/4*4 == sw
625
    hmod4    = sh/4*4 == sh
626
    mod4     = wmod4 && hmod4
627
    padw     = wmod4 ? sw : (sw/4+1)*4
628
    padh     = hmod4 ? sh : (sh/4+1)*4
629
    
630
    
631
    input_m4 = mod4 ? input : input.PointResize(padw, padh, 0, 0, padw, padh)
632
    
633
    return isYV12 ? input.RemoveGrain(mode, modeU, modeV)
634
    \             : isY8 ? input_m4                      .RemoveGrain(mode , -1, -1)
635
    \                              .Crop(0, 0, sw, sh)
636
    \                    : YToUV(input_m4.UToY8()        .RemoveGrain(modeU, -1, -1),
637
    \                            input_m4.VToY8()        .RemoveGrain(modeV, -1, -1),
638
    \                            input_m4.ConvertToY8()  .RemoveGrain(mode , -1, -1))
639
    \                     .Crop(0, 0, sw, sh)
640
}
641
*/
642
643
Function eedi3_resize16_U16(clip input, bool "tv_range", bool "Y", bool "U", bool "V", int "dither", int "interp", bool "HQ")
644
{ 
645
    tv_range = Default(tv_range, True   ) # define if input clip is of tv range(limited range)
646
    interp   = Default(interp,   0      ) # use interp or not for SmoothCurve/SmoothCurve16
647
    HQ       = Default(HQ,       False  ) # enable high quality interpolation (slower)
648
    dither   = Default(dither,   -1     ) # -1 for no dither, 0 for ordered-dither, 1-100 for noise strength
649
    Y        = Default(Y,        True   )
650
    U        = Default(U,        True   )
651
    V        = Default(V,        True   )
652
    
653
    
654
    Assert(dither>=-1 && dither<=100 , """eedi3_resize16_U16: "dither" ranges from -1 to 100!""")
655
    
656
    oCceil   = (255-128) / (255.5-128) * (65535.5-32768) + 32768
657
    
658
    Yexpr    = "0-0  ;                  255-65535             ;65535-65535          "
659
    Cexpr    = "0-0.5;0.5-0.5;128-32768;255-"+String(oCceil)+";65535-"+String(oCceil)
660
    DfExpr   = "0-0;65535-65535"
661
    Yexpr    = Y ? Yexpr : DfExpr
662
    Uexpr    = U ? Cexpr : DfExpr
663
    Vexpr    = V ? Cexpr : DfExpr
664
      
665
    up       =  tv_range ? input.Dither_convert_8_to_16()
666
    \                    : Y||U||V ? StackVertical(input.Dither_gen_null_lsb(), input)
667
	\                               .SmoothCurve16(Ycurve=Yexpr, Ucurve=Uexpr, Vcurve=Vexpr, mode=0, interp=interp, HQ=HQ,
668
    \                                              dither=dither, limiter=False, TVrange=0)
669
    \                              : StackVertical(input.Dither_gen_null_lsb(), input)
670
    
671
    
672
    return up
673
}
674
675
676
Function eedi3_resize16_Down8(clip input, bool "tv_range", bool "Y", bool "U", bool "V", int "dither", int "interp", bool "HQ")
677
{
678
    tv_range = Default(tv_range, True   ) # define if input clip is of tv range(limited range)
679
    interp   = Default(interp,   0      ) # use interp or not for SmoothCurve/SmoothCurve16
680
    HQ       = Default(HQ,       False  ) # enable high quality interpolation (slower)
681
    dither   = tv_range ? Default(dither, 6) : Default(dither, 50) # dither mode
682
    Y        = Default(Y,        True   )
683
    U        = Default(U,        True   )
684
    V        = Default(V,        True   )
685
    
686
    
687
    Assert(dither>=-1 && dither<=100 , """eedi3_resize16_Down8: "dither" ranges from -1 to 100!""")
688
    
689
    iCceil   = (255-128) / (255.5-128) * (65535.5-32768) + 32768
690
    
691
    Yexpr    = "0-0;                                           65535-255"
692
    Cexpr    = "0-0.5;0.5-0.5;32768-128;"+String(iCceil)+"-255;65535-255"
693
    DfExpr   = "0-0;65535-65535"
694
    Yexpr    = Y ? Yexpr : DfExpr
695
    Uexpr    = U ? Cexpr : DfExpr
696
    Vexpr    = V ? Cexpr : DfExpr
697
    
698
    sDown    = tv_range ? NOP()
699
    \                   : input.SmoothCurve16(Ycurve=Yexpr, Ucurve=Uexpr, Vcurve=Vexpr, mode=0, interp=interp, HQ=HQ,
700
    \                                         dither=dither, limiter=False, TVrange=0)
701
    
702
    down     = tv_range ? input.DitherPost(mode=dither, y=Y?3:1, u=U?3:1, v=V?3:1)
703
    \                   : Y||U||V ? sDown.Dither_get_lsb()
704
    \                             : input.Dither_get_msb()
705
    
706
    
707
    return down
708
}