Last active
December 28, 2015 18:59
-
-
Save line0/7547526 to your computer and use it in GitHub Desktop.
nnedi3_resize16 v3.0 by mawen1250, modded to use nnedi3ocl with gpu=true
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
###### nnedi3_resize16 v3.0 ###### by mawen1250 ###### 2013.11.15 ###### | |
###### from v2.8 on, nnedi3_resize16 only supports AviSynth v2.6 ###### | |
###### Requirements: masktools v2.0a48, dither v1.24.0, nnedi3 v0.9.4, ###### | |
###### SmoothAdjust v2.90, Contra-Sharpen mod 3.4, ###### | |
###### FTurn v1.3.5 (not necessarily required but will improve speed) ###### | |
###### Accept Y8, YV12, YV16, YV24 input, use nnedi3 and Dither_resize16(nr) for scaling ###### | |
###### output format can be 8bit/16bit Y8/YV12/YV16/YV24 or RGB32/RGB24/RGB48YV12/RGB48Y ###### | |
Function nnedi3_resize16(clip input, int "target_width", int "target_height", float "src_left", float "src_top", | |
\ float "src_width", float "src_height", string "kernel_d", string "kernel_u", bool "noring", | |
\ int "nsize", int "nns", int "qual", int "etype", int "pscrn", int "threads", float "ratiothr", | |
\ float "sharp", bool "mixed", float "thr", float "elast", | |
\ string "output", string "curve", float "gcor", string "matrix", bool "tv_range", string "cplace", | |
\ int "Y", int "U", int "V", bool "lsb_in", bool "lsb", int "dither", bool "gpu") | |
{ | |
# Parameters for merging edge&flat upscaled clip | |
mixed = Default(mixed, True ) # whether to use 16bit resizer in flat area when upscaling, which achieves better quality at the cost of speed. | |
thr = Default(thr, 1.0 ) # the same with "thr" in Dither_limit_dif16, valid value range is [0, 10.0], set to 0 will disable the limiting process. | |
elast = Default(elast, 1.0 ) # the same with "elast" in Dither_limit_dif16, valid value range is [1, 10.0]. | |
# PDiff: pixel value diff between flat clip and edge clip | |
# ODiff: pixel value diff between merged clip and edge clip | |
# ODiff = PDiff when (PDiff <= thr) | |
# ODiff gradually decreases from thr to 0 when (thr <= PDiff <= thr * elast) | |
# ODiff = 0 when (PDiff >= thr * elast) | |
# | |
# Larger "thr" will result in more pixels been taken from flat area upscaled clip(Dither_resize16) | |
# Larger "thr" will result in less pixels been taken from edge area upscaled clip(nnedi3+Dither_resize16) | |
# Larger "elast" will result in more pixels been blended from edge&flat area upscaled clip | |
# Parameters for nnedi3 | |
nsize = Default(nsize, 0 ) | |
nns = Default(nns, 3 ) | |
qual = Default(qual, 2 ) | |
etype = Default(etype, 0 ) | |
pscrn = Default(pscrn, 2 ) | |
threads = Default(threads, 0 ) | |
gpu = Default(gpu, false ) | |
# Parameters for Dither_resize16 | |
kernel_d = Default(kernel_d, "Spline36" ) # kernel of Dither_resize16 used in flat area downscaling & edge area rescaling | |
kernel_u = Default(kernel_u, "Spline64" ) # kernel of Dither_resize16 used in flat area upscaling | |
noring = Default(noring, False ) # True use non-ringing algorithm of Dither_resize16 in flat area scaling | |
# *It actually doesn't make sense for nnedi3_resize16(which uses nnedi3 for upscaling), * | |
# *while it'll produce blurring and aliasing when downscaling. * | |
# *You'd better not set it to True unless you know what you are doing. * | |
resste = "Dither_resize16" | |
resstf = noring ? "Dither_resize16nr" : "Dither_resize16" | |
# Post-Process | |
sharp = Default(sharp, 0 ) # Strength of Contra-Sharpen mod, for sharper edge. 0 means no sharpening, common value is about 100. | |
# *Only* when {horrizontal or vertical}{scale ratio}>{ratiothr} will sharpening take effect. | |
# Parameters for input/output | |
Y = Default(Y, 3 ) | |
U = Default(U, 3 ) | |
V = Default(V, 3 ) | |
lsb_in = Default(lsb_in, False ) # input clip is 16-bit stacked or not | |
lsb = Default(lsb, False ) # output clip is 16-bit stacked or not | |
dither = Default(dither, 6 ) # dither mode for conversion from 16-bit to 8-bit (same as "mode" in DitherPost) | |
sCSP = input.nnedi3_resize16_GetCSP() | |
Assert(sCSP=="Y8" || sCSP=="YV12" || sCSP=="YV16" || sCSP=="YV24", """nnedi3_resize16: only accept Y8, YV12, YV16, YV24 input""") | |
output = Default(output, sCSP ) | |
# Output format. Possible values are: | |
# "Y8" : Regular Y8 colorspace. Parameter "lsb" works on this output mode. | |
# "YV12" : Regular YV12 colorspace. Parameter "lsb" works on this output mode. | |
# "YV16" : Regular YV16 colorspace. Parameter "lsb" works on this output mode. | |
# "YV24" : Regular YV24 colorspace. Parameter "lsb" works on this output mode. | |
# "RGB32" : Regular RGB32 colorspace. | |
# "RGB24" : Regular RGB24 colorspace. | |
# "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). | |
# "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. | |
IsY8 = sCSP == "Y8" || output == "Y8" | |
sCSP = IsY8 ? "YV24" : sCSP | |
IsRGB = LeftStr(output, 3) == "RGB" | |
oCSP = IsRGB || IsY8 ? "YV24" : output | |
Y = min(Y, 4) | |
U = IsY8 ? 1 : min(U, 4) | |
V = IsY8 ? 1 : min(V, 4) | |
Yt = Y == 3 || Y == 4 | |
Ut = U == 3 || U == 4 | |
Vt = V == 3 || V == 4 | |
Y31 = Yt ? 3 : 1 | |
U31 = Ut ? 3 : 1 | |
V31 = Vt ? 3 : 1 | |
Y32 = Yt ? 3 : Y | |
U32 = Ut ? 3 : U | |
V32 = Vt ? 3 : V | |
Y21 = Yt ? 2 : Y | |
U21 = Ut ? 2 : U | |
V21 = Vt ? 2 : V | |
Y321 = Y > 1 ? 3 : Y | |
U321 = U > 1 ? 3 : U | |
V321 = V > 1 ? 3 : V | |
sw = input.Width () | |
sh = input.Height() | |
sh = lsb_in ? sh/2 : sh | |
swc = sCSP=="YV24" ? sw : sw/2 | |
shc = sCSP=="YV12" ? sh/2 : sh | |
HD = (sw > 1024 || sh > 576) ? True : False | |
matrix = Default(matrix, HD ? "709" : "601") | |
# The matrix used to convert the YUV pixels to computer RGB. Possible values are: | |
# "601" : ITU-R BT.601 / ITU-R BT.470-2 / SMPTE 170M. For Standard Definition content. | |
# "709" : ITU-R BT.709. For High Definition content. | |
# "240" : SMPTE 240M | |
# "FCC" : FCC | |
# "YCgCo": YCgCo | |
# When the parameter is not defined, ITU-R BT.601 and ITU-R BT.709 are automatically selected depending on the clip definition. | |
curve = Default(curve, "linear" ) | |
# type of gamma mapping(transfer characteristic) | |
# possible values: | |
# "709" - ITU-R BT.709 transfer curve for digital video | |
# "601" - ITU-R BT.601 transfer curve, same as "709" | |
# "170" - SMPTE 170M, same as "709" | |
# "240" - SMPTE 240M (1987) | |
# "srgb" - sRGB curve | |
# "2020" - ITU-R BT.2020 transfer curve, for 12-bit content. For sources of lower bitdepth, use the "709" curve. | |
# "linear" - linear curve without gamma-aware processing | |
curve = Yt ? curve : "linear" | |
gcor = Default(gcor, 1.0 ) # Gamma correction, applied on the linear part. | |
tv_range = Default(tv_range, True ) # define source's level | |
cplace = Default(cplace, "MPEG2") | |
# Placement of the chroma subsamples. Can be one of these strings: | |
# "MPEG1": 4:2:0 subsampling used in MPEG-1. Chroma samples are located on the center of each group of 4 pixels. | |
# "MPEG2": Subsampling used in MPEG-2 4:2:x. Chroma samples are located on the left pixel column of the group. | |
# Parameters for resizer | |
ow = Default(target_width, sw) | |
oh = Default(target_height, sh) | |
owc = oCSP=="YV24" ? ow : ow/2 | |
ohc = oCSP=="YV12" ? oh/2 : oh | |
Assert(!(output=="YV16" && ow!=owc*2), """nnedi3_resize16: width of YV16 output clip must be MOD2!""") | |
Assert(!(output=="YV12" && ow!=owc*2), """nnedi3_resize16: width of YV12 output clip must be MOD2!""") | |
Assert(!(output=="YV12" && oh!=ohc*2), """nnedi3_resize16: height of YV12 output clip must be MOD2!""") | |
src_left = Default(src_left, 0 ) | |
src_top = Default(src_top, 0 ) | |
src_width = Default(src_width, sw) | |
src_height = Default(src_height, sh) | |
# Pre-cropping/padding | |
prel = int(src_left/2) * 2 | |
pret = int(src_top /2) * 2 | |
prer = int((src_width > 0 ? -sw+src_left+src_width : src_width )/2) * 2 | |
preb = int((src_height> 0 ? -sh+src_top+src_height : src_height)/2) * 2 | |
prew = sw - prel + prer | |
preh = sh - pret + preb | |
sCSP=="YV24" ? \ | |
Eval(""" | |
swmod2 = sw /2*2 == sw | |
pwmod2 = prew/2*2 == prew | |
wpre = prew < sw | |
prel = wpre ? prel : 0 | |
prer = wpre ? pwmod2 ? prer : prer + 1 : swmod2 ? 0 : 1 | |
prew = sw - prel + prer | |
wpre = prew < sw || !swmod2 | |
""") : \ | |
Eval(""" | |
swmod4 = sw /4*4 == sw | |
pwmod4 = prew/4*4 == prew | |
wpre = prew < sw | |
prel = wpre ? prel : 0 | |
prer = wpre ? pwmod4 ? prer : prer + 2 : swmod4 ? 0 : 2 | |
prew = sw - prel + prer | |
wpre = prew < sw || !swmod4 | |
""") | |
sCSP=="YV12" ? \ | |
Eval(""" | |
shmod4 = sh /4*4 == sh | |
phmod4 = preh/4*4 == preh | |
hpre = preh < sh | |
pret = hpre ? pret : 0 | |
preb = hpre ? phmod4 ? preb : preb + 2 : shmod4 ? 0 : 2 | |
preh = sh - pret + preb | |
hpre = preh < sh || !shmod4 | |
""") : \ | |
Eval(""" | |
shmod2 = sh /2*2 == sh | |
phmod2 = preh/2*2 == preh | |
hpre = preh < sh | |
pret = hpre ? pret : 0 | |
preb = hpre ? phmod2 ? preb : preb + 1 : shmod2 ? 0 : 1 | |
preh = sh - pret + preb | |
hpre = preh < sh || !shmod2 | |
""") | |
src_width = src_width <=0 ? +sw-src_left+src_width : src_width | |
src_height = src_height<=0 ? +sh-src_top+src_height : src_height | |
src_left = wpre ? src_left-prel : src_left | |
src_top = hpre ? src_top -pret : src_top | |
src_leftc = sCSP=="YV24" ? src_left : src_left /2. | |
src_topc = sCSP=="YV12" ? src_top /2. : src_top | |
src_widthc = sCSP=="YV24" ? src_width : src_width /2. | |
src_heightc = sCSP=="YV12" ? src_height/2. : src_height | |
# Scale & Shift Calculation | |
ratiothr = Default(ratiothr, 1.125) # When scale ratio is larger than ratiothr, use nnedi3+Dither_resize16 upscale method instead of pure Dither_resize16. | |
yhratio = float(ow ) / float(src_width ) | |
yvratio = float(oh ) / float(src_height ) | |
chratio = float(owc) / float(src_widthc ) | |
cvratio = float(ohc) / float(src_heightc) | |
enable = yhratio!=1 || yvratio!=1 || chratio!=1 || cvratio!=1 || | |
\ src_width !=int(src_width ) || src_height !=int(src_height ) || src_widthc !=int(src_widthc ) || src_heightc!=int(src_heightc) || | |
\ src_left !=int(src_left ) || src_top !=int(src_top ) || src_leftc !=int(src_leftc ) || src_topc !=int(src_topc ) | |
yhct = yhratio>ratiothr ? Ceil( log(yhratio/ratiothr) / log(2) ) : 0 | |
yhrf = int(Pow(2, yhct)) | |
yrhratio = yhratio/yhrf | |
yvct = yvratio>ratiothr ? Ceil( log(yvratio/ratiothr) / log(2) ) : 0 | |
yvrf = int(Pow(2, yvct)) | |
yrvratio = yvratio/yvrf | |
yhshift = yhrf>=2 ? 0.5 : 0 | |
yvshift = yvrf>=2 ? 0.5 : 0 | |
yhfix = -yhshift | |
yvfix = -yvshift | |
chct = chratio>ratiothr ? Ceil( log(chratio/ratiothr) / log(2) ) : 0 | |
chrf = int(Pow(2, chct)) | |
crhratio = chratio/chrf | |
cvct = cvratio>ratiothr ? Ceil( log(cvratio/ratiothr) / log(2) ) : 0 | |
cvrf = int(Pow(2, cvct)) | |
crvratio = cvratio/cvrf | |
nonenny = yhct<=0 && yvct<=0 | |
nonennc = chct<=0 && cvct<=0 | |
nonenn = nonenny || nonennc | |
Ynnt = Yt&&!nonenny | |
Unnt = Ut&&!nonennc | |
Vnnt = Vt&&!nonennc | |
Ynn31 = Ynnt ? 3 : 1 | |
Unn31 = Unnt ? 3 : 1 | |
Vnn31 = Vnnt ? 3 : 1 | |
Ynn = Yt&&nonenny ? 2 : Y | |
Unn = Ut&&nonennc ? 2 : U | |
Vnn = Vt&&nonennc ? 2 : V | |
nnt = Ynnt || Unnt || Vnnt | |
mixed = !nnt || !enable ? False : mixed | |
chshift = oCSP=="YV24" ? sCSP=="YV24" ? chrf>=2 ? 0.50 : 0 | |
\ : cplace=="MPEG1" ? chrf>=2 ? 0.50 : 0 | |
\ : chrf>=2 ? 0.50-chrf/4. : -0.25 | |
\ : sCSP=="YV24" ? cplace=="MPEG1" ? chrf>=2 ? 0.50 : 0 | |
\ : chrf>=2 ? 0.75 : 0.25 | |
\ : cplace=="MPEG1" ? chrf>=2 ? 0.50 : 0 | |
\ : chrf>=2 ? 0.75-chrf/4. : 0 | |
# (chrf/2-0.5)/2-(chrf/2-1) | |
cvshift = cvrf>=2 ? 0.5 : 0 | |
chfix = -chshift | |
cvfix = -cvshift | |
cphfixe = oCSP=="YV24" ? 0 | |
\ : cplace=="MPEG1" ? 0 | |
\ : 0.25-0.25/crhratio | |
cphfix = oCSP=="YV24" ? sCSP=="YV24" ? 0 | |
\ : cplace=="MPEG1" ? 0 | |
\ : 0.25 | |
\ : sCSP=="YV24" ? cplace=="MPEG1" ? 0 | |
\ : -0.5 | |
\ : cplace=="MPEG1" ? 0 | |
\ : 0.25-0.25/chratio | |
# Pre-process | |
input = wpre || hpre ? lsb_in ? input.Dither_resize16(wpre?prew:sw, hpre?preh:sh, wpre?prel:0, hpre?pret:0, | |
\ wpre?prew:sw, hpre?preh:sh, kernel="point") | |
\ : input.PointResize(wpre?prew:sw, hpre?preh:sh, wpre?prel:0, hpre?pret:0, wpre?prew:sw, hpre?preh:sh) | |
\ : input | |
input8 = lsb_in ? input.nnedi3_resize16_Down8(tv_range, Yt, Ut, Vt, -1) : input | |
input16 = lsb_in ? input : input.nnedi3_resize16_U16(tv_range, True, True, True) | |
# nnedi3 upscale for edge area | |
!(enable && nnt) ? NOP() : \ | |
yhct==chct && yvct==cvct && sCSP=="YV12" ? \ | |
Eval(""" | |
edgenn = input8.nnedi3_resize16_rpow2(yvct, yhct, 1, 1, Yt, Ut, Vt, gpu, nsize, nns, qual, etype, pscrn, threads) | |
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) | |
\ .nnedi3_resize16_U16(tv_range, Yt, Ut, Vt) | |
\ : edgenn.nnedi3_resize16_U16(tv_range, Yt, Ut, Vt) | |
edgennU = edgennY.UToY8() | |
edgennV = edgennY.VToY8() | |
edgennY = edgennY.ConvertToY8() | |
""") : \ | |
Eval(""" | |
edgennY = !Ynnt ? NOP() | |
\ : sharp>0 ? input8.ConvertToYV12().nnedi3_resize16_rpow2(yvct, yhct, 1, 1, Yt, False, False, gpu, | |
\ nsize, nns, qual, etype, pscrn, threads) | |
\ .CSmod(chroma=False, ss_w=1.00, ss_h=1.00, preblur=-4, Smethod=1, kernel=6, strength=sharp, Soothe=-1) | |
\ .ConvertToY8 ().nnedi3_resize16_U16(tv_range, Yt, False, False) | |
\ : input8.ConvertToYV12().nnedi3_resize16_rpow2(yvct, yhct, 1, 1, Yt, False, False, gpu | |
\ nsize, nns, qual, etype, pscrn, threads) | |
\ .ConvertToY8 ().nnedi3_resize16_U16(tv_range, Yt, False, False) | |
edgennU = !Unnt ? NOP() | |
\ : input8.UToY ().nnedi3_resize16_rpow2(cvct, chct, 1, 1, Ut, False, False, gpu | |
\ nsize, nns, qual, etype, pscrn, threads) | |
\ .ConvertToY8().nnedi3_resize16_U16(tv_range, Ut, False, False) | |
edgennV = !Vnnt ? NOP() | |
\ : input8.VToY ().nnedi3_resize16_rpow2(cvct, chct, 1, 1, Vt, False, False, gpu | |
\ nsize, nns, qual, etype, pscrn, threads) | |
\ .ConvertToY8().nnedi3_resize16_U16(tv_range, Vt, False, False) | |
""") | |
# edge area resize & fix center shift | |
!(enable && nnt) ? NOP() : \ | |
Eval(""" | |
edgennY = !Ynnt ? NOP() | |
\ : curve=="linear" ? edgennY | |
\ : edgennY.Dither_y_gamma_to_linear(tv_range, tv_range, curve, u=1, v=1, gcor=gcor) | |
edgeY = !Ynnt ? input16.BlankClip(width=ow , height=oh *2, pixel_type="Y8", color_yuv=$008080) | |
\ : edgennY.""" + resste + """(ow , oh , src_left *yhrf+yhfix , src_top *yvrf+yvfix, | |
\ src_width *yhrf, src_height *yvrf, | |
\ kernel=kernel_d, y=Y31, u=1, v=1) | |
edgeY = !Ynnt ? edgeY | |
\ : curve=="linear" ? edgeY | |
\ : edgeY .Dither_y_linear_to_gamma(tv_range, tv_range, curve, u=1, v=1, gcor=gcor) | |
edgeU = !Unnt ? input16.BlankClip(width=owc, height=ohc*2, pixel_type="Y8", color_yuv=$008080) | |
\ : edgennU.""" + resste + """(owc, ohc, src_leftc*chrf+chfix+cphfixe, src_topc*cvrf+cvfix, | |
\ src_widthc*chrf, src_heightc*cvrf, | |
\ kernel=kernel_d, y=U31, u=1, v=1) | |
edgeV = !Vnnt ? input16.BlankClip(width=owc, height=ohc*2, pixel_type="Y8", color_yuv=$008080) | |
\ : edgennV.""" + resste + """(owc, ohc, src_leftc*chrf+chfix+cphfixe, src_topc*cvrf+cvfix, | |
\ src_widthc*chrf, src_heightc*cvrf, | |
\ kernel=kernel_d, y=V31, u=1, v=1) | |
edge16 = IsY8 ? edgeY : YToUV(edgeU, edgeV, edgeY) | |
""") | |
# flat area resize | |
!(enable && (mixed || !(Ynnt && Unnt && Vnnt))) ? NOP() : \ | |
yhratio==chratio && yvratio==cvratio && (!mixed || (Ynnt && Unnt && Vnnt)) ? \ | |
Eval(""" | |
flat16 = curve=="linear" ? input16 | |
\ : input16.Dither_y_gamma_to_linear(tv_range, tv_range, curve, u=U21, v=V21, gcor=gcor) | |
flat16 = flat16. """ + resstf + """(ow , oh , src_left , src_top , | |
\ src_width , src_height , cplace=cplace, | |
\ kernel=(yhratio>1||yvratio>1) ? kernel_u : kernel_d, y=Y32, u=U32, v=V32) | |
flat16 = curve=="linear" ? flat16 | |
\ : flat16 .Dither_y_linear_to_gamma(tv_range, tv_range, curve, u=U21, v=V21, gcor=gcor) | |
""") : \ | |
Eval(""" | |
flatY = curve=="linear" ? input16.ConvertToY8() : | |
\ (mixed || !Ynnt) && Yt ? input16.ConvertToY8().Dither_y_gamma_to_linear(tv_range, tv_range, curve, u=1 , v=1 , gcor=gcor) | |
\ : input16.ConvertToY8() | |
flatY = (mixed || !Ynnt) && Yt ? flatY .""" + resstf + """(ow , oh , src_left , src_top , | |
\ src_width , src_height , | |
\ kernel=(yhratio>1||yvratio>1) ? kernel_u : kernel_d, y=Y32, u=1 , v=1 ) | |
\ : input16.BlankClip(width=ow , height=oh *2, pixel_type="Y8", color_yuv=$008080) | |
flatY = curve=="linear" ? flatY : | |
\ (mixed || !Ynnt) && Yt ? flatY .Dither_y_linear_to_gamma(tv_range, tv_range, curve, u=1 , v=1 , gcor=gcor) | |
\ : flatY | |
flatU = (mixed || !Unnt) && Ut ? input16.UToY8().""" + resstf + """(owc, ohc, src_leftc +cphfix , src_topc , | |
\ src_widthc , src_heightc , | |
\ kernel=(chratio>1||cvratio>1) ? kernel_u : kernel_d, y=U32, u=1 , v=1 ) | |
\ : input16.BlankClip(width=owc, height=ohc*2, pixel_type="Y8", color_yuv=$008080) | |
flatV = (mixed || !Vnnt) && Vt ? input16.VToY8().""" + resstf + """(owc, ohc, src_leftc +cphfix , src_topc , | |
\ src_widthc , src_heightc , | |
\ kernel=(chratio>1||cvratio>1) ? kernel_u : kernel_d, y=V32, u=1 , v=1 ) | |
\ : input16.BlankClip(width=owc, height=ohc*2, pixel_type="Y8", color_yuv=$008080) | |
flat16 = IsY8 ? flatY : YToUV(flatU, flatV, flatY) | |
""") | |
# merge & output | |
enable ? \ | |
Eval(""" | |
merge16 = !nnt ? flat16 | |
\ : mixed ? Dither_limit_dif16(flat16, edge16, thr=thr, elast=elast, y=Ynn, u=Unn, v=Vnn) | |
\ : Ynnt==Unnt && Unnt==Vnnt || IsY8 ? edge16 | |
\ : mt_lutxy(edge16, flat16, Y=Yt?Ynnt?2:4:1, U=Ut?Unnt?2:4:1, V=Vt?Vnnt?2:4:1) | |
merge16 = IsY8 ? output=="Y8" ? merge16.ConvertToY8() : Eval("merge16.ConvertTo"+oCSP).Dither_lut16(Y=2, U=-32768, V=-32768) : merge16 | |
final = IsRGB ? merge16.Dither_convert_yuv_to_rgb(matrix=matrix, tv_range=tv_range, lsb_in=True, mode=dither, output=output) | |
\ : lsb ? merge16 | |
\ : merge16.nnedi3_resize16_Down8(tv_range, True, !IsY8, !IsY8, dither) | |
""") : \ | |
Eval(""" | |
shift16 = input16.Dither_resize16(ow, oh, src_left, src_top, src_width, src_height, kernel="point", y=Y, u=U, v=V) | |
shift16 = IsY8 ? output=="Y8" ? merge16.ConvertToY8() : Eval("shift16.ConvertTo"+oCSP).Dither_lut16(Y=2, U=-32768, V=-32768) : shift16 | |
final = IsRGB ? shift16.Dither_convert_yuv_to_rgb(matrix=matrix, tv_range=tv_range, lsb_in=True, mode=dither, output=output) | |
\ : lsb ? shift16 | |
\ : shift16.nnedi3_resize16_Down8(tv_range, True, !IsY8, !IsY8, dither) | |
""") | |
return final | |
} | |
Function nnedi3_resize16_rpow2(clip input, int "vct", int "hct", int "vfield", int "hfield", bool "Y", bool "U", bool "V", bool "gpu", | |
\ int "nsize", int "nns", int "qual", int "etype", int "pscrn", int "threads", bool "honly") | |
{ | |
vct = Default(vct, 1 ) | |
hct = Default(hct, 1 ) | |
vfield = Default(vfield, 1 ) | |
hfield = Default(hfield, 1 ) | |
Y = Default(Y, True ) | |
U = Default(U, False ) | |
V = Default(V, False ) | |
honly = Default(honly, False ) | |
gpu = Default(gpu, False ) | |
input | |
hct >= 1 ? \ | |
Eval(""" | |
(honly) ? last | |
\ : Eval(" try { fturnright(chroma=U||V, mt=threads!=1) } catch(error_msg) { TurnRight() } ") | |
(!gpu) ? nnedi3(hfield, True, Y, U, V, nsize, nns, qual, etype, pscrn, threads) : nnedi3ocl(hfield, True, Y, U, V, nsize, nns, qual, etype) | |
hct = hct - 1 | |
honly = hct >= 1 | |
hfield = 0 | |
(honly) ? last | |
\ : Eval(" try { fturnleft (chroma=U||V, mt=threads!=1) } catch(error_msg) { TurnLeft () } ") | |
""") : NOP() | |
vct >= 1 && !honly ? \ | |
Eval(""" | |
(!gpu) ? nnedi3(vfield, True, Y, U, V, nsize, nns, qual, etype, pscrn, threads) : nnedi3ocl(vfield, True, Y, U, V, nsize, nns, qual, etype) | |
vct = vct - 1 | |
vfield = 0 | |
""") : NOP() | |
return Y||U||V ? vct <= 0 && hct <= 0 ? last | |
\ : last.nnedi3_resize16_rpow2(vct, hct, vfield, hfield, Y, U, V, gpu, nsize, nns, qual, etype, pscrn, threads, honly) | |
\ : input | |
} | |
Function nnedi3_resize16_GetCSP(clip c) | |
{ | |
return c.IsPlanar ? c.IsYV12 ? "YV12" : | |
\ c.IsYV16 ? "YV16" : | |
\ c.IsYV24 ? "YV24" : c.GetCSP_Y8_YV411 : | |
\ c.IsYUY2 ? "YUY2" : | |
\ c.IsRGB32 ? "RGB32" : | |
\ c.IsRGB24 ? "RGB24" : "Unknown" | |
Function GetCSP_Y8_YV411(clip c) { | |
try { | |
c.UtoY | |
csp = "YV411" | |
} catch ( error_msg ) { | |
csp = "Y8" | |
} | |
return csp | |
} | |
} | |
/* | |
Function nnedi3_resize16_RemoveGrain(clip input, int "mode", int "modeU", int "modeV") | |
{ | |
mode = Default(mode, 1 ) | |
modeU = Default(modeU, mode ) | |
modeV = Default(modeV, modeU ) | |
iCSP = input.nnedi3_resize16_GetCSP() | |
isYV12 = iCSP == "YV12" | |
isY8 = iCSP == "Y8" | |
sw = input.Width () | |
sh = input.Height() | |
wmod4 = sw/4*4 == sw | |
hmod4 = sh/4*4 == sh | |
mod4 = wmod4 && hmod4 | |
padw = wmod4 ? sw : (sw/4+1)*4 | |
padh = hmod4 ? sh : (sh/4+1)*4 | |
input_m4 = mod4 ? input : input.PointResize(padw, padh, 0, 0, padw, padh) | |
return isYV12 ? input.RemoveGrain(mode, modeU, modeV) | |
\ : isY8 ? input_m4.ConvertToYV12().RemoveGrain(mode , -1, -1).ConvertToY8() | |
\ .Crop(0, 0, sw, sh) | |
\ : YToUV(input_m4.UToY() .RemoveGrain(modeU, -1, -1), | |
\ input_m4.VToY() .RemoveGrain(modeV, -1, -1), | |
\ input_m4.ConvertToYV12().RemoveGrain(mode , -1, -1)) | |
\ .Crop(0, 0, sw, sh) | |
} | |
*/ | |
Function nnedi3_resize16_U16(clip c, bool "tv_range", bool "Y", bool "U", bool "V") | |
{ | |
tv_range = Default(tv_range, True ) # define if input clip is of tv range(limited range) | |
Y = Default(Y, True ) | |
U = Default(U, True ) | |
V = Default(V, True ) | |
yExpr = "0-0;255-65535;65535-65535" | |
cExpr = "0-0;128-32768;255-65535;65535-65535" | |
DfExpr = "0-0;65535-65535" | |
Yexpr = Y ? yExpr : DfExpr | |
Uexpr = U ? cExpr : DfExpr | |
Vexpr = V ? cExpr : DfExpr | |
up = tv_range ? c.Dither_convert_8_to_16() | |
\ : Y||U||V ? StackVertical(c.Dither_gen_null_lsb(), c).SmoothCurve16(Ycurve=Yexpr, Ucurve=Uexpr, Vcurve=Vexpr, mode=0, | |
\ limiter=False, TVrange=0, interp=0, dither=-1) | |
\ : StackVertical(c.Dither_gen_null_lsb(), c) | |
return up | |
} | |
Function nnedi3_resize16_Down8(clip c, bool "tv_range", bool "Y", bool "U", bool "V", int "dither", int "interp", bool "HQ") | |
{ | |
tv_range = Default(tv_range, True ) # define if input clip is of tv range(limited range) | |
interp = Default( interp, 0 ) # use interp or not for SmoothCurve/SmoothCurve16 | |
HQ = Default( HQ, False ) # enable high quality interpolation (slower) | |
dither = tv_range ? Default(dither, 6) : Default(dither, 50) # dither mode | |
Y = Default(Y, True ) | |
U = Default(U, True ) | |
V = Default(V, True ) | |
Assert(dither>=-1 && dither<=100 , """nnedi3_resize16_Down8: "dither" ranges from -1 to 100!""") | |
yExpr = "0-0;65535-255" | |
cExpr = "0-0;32768-128;65535-255" | |
DfExpr = "0-0;65535-65535" | |
Yexpr = Y ? yExpr : DfExpr | |
Uexpr = U ? cExpr : DfExpr | |
Vexpr = V ? cExpr : DfExpr | |
sDown = tv_range ? NOP() | |
\ : c.SmoothCurve16(Ycurve=Yexpr, Ucurve=Uexpr, Vcurve=Vexpr, mode=0, interp=interp, HQ=HQ, | |
\ dither=dither, limiter=False, TVrange=0) | |
down = tv_range ? c.DitherPost(mode=dither, y=Y?3:1, u=U?3:1, v=V?3:1) | |
\ : Y||U||V ? sDown.Dither_get_lsb() | |
\ : c.Dither_get_msb() | |
return down | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment