forked from Selur/VapoursynthScriptsInHybrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nnedi3_rpow2.py
92 lines (73 loc) · 3.21 KB
/
nnedi3_rpow2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import vapoursynth as vs
def nnedi3_rpow2(clip, rfactor=2, width=None, height=None, correct_shift=True,
kernel="spline36", nsize=0, nns=3, qual=None, etype=None, pscrn=None,
opt=True, int16_prescreener=None, int16_predictor=None, exp=None):
"""nnedi3_rpow2 is for enlarging images by powers of 2.
Args:
rfactor (int): Image enlargement factor.
Must be a power of 2 in the range [2 to 1024].
correct_shift (bool): If False, the shift is not corrected.
The correction is accomplished by using the subpixel
cropping capability of fmtc's resizers.
width (int): If correcting the image center shift by using the
"correct_shift" parameter, width/height allow you to set a
new output resolution.
kernel (string): Sets the resizer used for correcting the image
center shift that nnedi3_rpow2 introduces. This can be any of
fmtc kernels, such as "cubic", "spline36", etc.
spline36 is the default one.
nnedi3_args (mixed): For help with nnedi3 args
refert to nnedi3 documentation.
"""
core = vs.core
# Setting up variables
plugins = core.get_plugins()
if width is None:
width = clip.width*rfactor
if height is None:
height = clip.height*rfactor
hshift = 0.0
vshift = -0.5
pkdnnedi = dict(dh=True, nsize=nsize, nns=nns, qual=qual, etype=etype,
pscrn=pscrn, opt=opt, int16_prescreener=int16_prescreener,
int16_predictor=int16_predictor, exp=exp)
pkdchroma = dict(kernel=kernel, sy=-0.5, planes=[2, 3, 3])
tmp = 1
times = 0
while tmp < rfactor:
tmp *= 2
times += 1
# Checks
if rfactor < 2 or rfactor > 1024:
raise ValueError("nnedi3_rpow2: rfactor must be between 2 and 1024")
if tmp != rfactor:
raise ValueError("nnedi3_rpow2: rfactor must be a power of 2")
if 'com.deinterlace.nnedi3' not in plugins:
raise RuntimeError("nnedi3_rpow2: nnedi3 plugin is required")
if correct_shift or clip.format.subsampling_h:
if 'fmtconv' not in plugins:
raise RuntimeError("nnedi3_rpow2: fmtconv plugin is required")
# Processing
last = clip
for i in range(times):
field = 1 if i == 0 else 0
last = core.nnedi3.nnedi3(last, field=field, **pkdnnedi)
last = core.std.Transpose(last)
if last.format.subsampling_w:
# Apparently always using field=1 for the horizontal pass somehow
# keeps luma/chroma alignment.
field = 1
hshift = hshift*2 - 0.5
else:
hshift = -0.5
last = core.nnedi3.nnedi3(last, field=field, **pkdnnedi)
last = core.std.Transpose(last)
# Correct vertical shift of the chroma.
if clip.format.subsampling_h:
last = core.fmtc.resample(last, w=last.width, h=last.height, **pkdchroma)
if correct_shift is True:
last = core.fmtc.resample(last, w=width, h=height, kernel=kernel,
sx=hshift, sy=vshift)
if last.format.id != clip.format.id:
last = core.fmtc.bitdepth(last, csp=clip.format.id)
return last