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