-
Notifications
You must be signed in to change notification settings - Fork 4
/
smdegrain.py
283 lines (254 loc) · 13.8 KB
/
smdegrain.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
################################################################################################
### ###
### Simple MDegrain Mod - SMDegrain() ###
### ###
### Mod by Dogway - Original idea by Caroliano ###
### ###
### Special Thanks: Sagekilla, Didée, cretindesalpes, Gavino and MVtools people ###
### ###
### v3.1.2d (Dogway's mod) - 21 July 2015 ###
### ###
################################################################################################
###
### General purpose simple degrain function. Pure temporal denoiser. Basically a wrapper(function)/frontend of mvtools2+mdegrain
### with some added common related options. Goal is accessibility and quality but not targeted to any specific kind of source.
### The reason behind is to keep it simple so aside masktools2 you will only need MVTools2.
###
### Check documentation for deep explanation on settings and defaults.
### VideoHelp thread: (http://forum.videohelp.com/threads/369142)
###
################################################################################################
import vapoursynth as vs
core = vs.core
import havsfunc
import mvsfunc as mvf
import nnedi3_resample
################################################################################################
### ###
### Simple MDegrain Mod - SMDegrain() ###
### ###
### Mod by Dogway - Original idea by Caroliano ###
### ###
### Special Thanks: Sagekilla, Didée, cretindesalpes, Gavino and MVtools people ###
### ###
### v3.1.2d (Dogway's mod) - 21 July 2015 ###
### ###
################################################################################################
###
### General purpose simple degrain function. Pure temporal denoiser. Basically a wrapper(function)/frontend of mvtools2+mdegrain
### with some added common related options. Goal is accessibility and quality but not targeted to any specific kind of source.
### The reason behind is to keep it simple so aside masktools2 you will only need MVTools2.
###
### Check documentation for deep explanation on settings and defaults.
### VideoHelp thread: (http://forum.videohelp.com/threads/369142)
###
################################################################################################
# Globals
bv6 = bv4 = bv3 = bv2 = bv1 = fv1 = fv2 = fv3 = fv4 = fv6 = None
def SMDegrain(input, tr=2, thSAD=300, thSADC=None, RefineMotion=False, contrasharp=None, CClip=None, interlaced=False, tff=None, plane=4, Globals=0, pel=None, subpixel=2, prefilter=-1, mfilter=None,
blksize=None, overlap=None, search=4, truemotion=None, MVglobal=None, dct=0, limit=255, limitc=None, thSCD1=400, thSCD2=130, chroma=True, hpad=None, vpad=None, Str=1.0, Amp=0.0625, opencl=False, device=None):
if not isinstance(input, vs.VideoNode):
raise vs.Error('SMDegrain: This is not a clip')
if input.format.color_family == vs.GRAY:
plane = 0
chroma = False
peak = (1 << input.format.bits_per_sample) - 1
# Defaults & Conditionals
thSAD2 = thSAD // 2
if thSADC is None:
thSADC = thSAD2
GlobalR = (Globals == 1)
GlobalO = (Globals >= 3)
if1 = CClip is not None
if contrasharp is None:
contrasharp = not GlobalO and if1
w = input.width
h = input.height
preclip = isinstance(prefilter, vs.VideoNode)
ifC = isinstance(contrasharp, bool)
if0 = contrasharp if ifC else contrasharp > 0
if4 = w > 1024 or h > 576
if pel is None:
pel = 1 if if4 else 2
if pel < 2:
subpixel = min(subpixel, 2)
pelclip = pel > 1 and subpixel >= 3
if blksize is None:
blksize = 16 if if4 else 8
blk2 = blksize // 2
if overlap is None:
overlap = blk2
ovl2 = overlap // 2
if truemotion is None:
truemotion = not if4
if MVglobal is None:
MVglobal = truemotion
planes = [0, 1, 2] if chroma else [0]
plane0 = (plane != 0)
if hpad is None:
hpad = blksize
if vpad is None:
vpad = blksize
limit = havsfunc.scale(limit, peak)
if limitc is None:
limitc = limit
else:
limitc = havsfunc.scale(limitc, peak)
# Error Report
if not (ifC or isinstance(contrasharp, int)):
raise vs.Error("SMDegrain: 'contrasharp' only accepts bool and integer inputs")
if if1 and (not isinstance(CClip, vs.VideoNode) or CClip.format.id != input.format.id):
raise vs.Error("SMDegrain: 'CClip' must be the same format as input")
if interlaced and h & 3:
raise vs.Error('SMDegrain: Interlaced source requires mod 4 height sizes')
if interlaced and not isinstance(tff, bool):
raise vs.Error("SMDegrain: 'tff' must be set if source is interlaced. Setting tff to true means top field first and false means bottom field first")
if not (isinstance(prefilter, int) or preclip):
raise vs.Error("SMDegrain: 'prefilter' only accepts integer and clip inputs")
if preclip and prefilter.format.id != input.format.id:
raise vs.Error("SMDegrain: 'prefilter' must be the same format as input")
if mfilter is not None and (not isinstance(mfilter, vs.VideoNode) or mfilter.format.id != input.format.id):
raise vs.Error("SMDegrain: 'mfilter' must be the same format as input")
if RefineMotion and blksize < 8:
raise vs.Error('SMDegrain: For RefineMotion you need a blksize of at least 8')
if not chroma and plane != 0:
raise vs.Error('SMDegrain: Denoising chroma with luma only vectors is bugged in mvtools and thus unsupported')
# RefineMotion Variables
if RefineMotion:
halfblksize = blk2 # MRecalculate works with half block size
halfoverlap = overlap if overlap <= 2 else ovl2 + ovl2 % 2 # Halve the overlap to suit the halved block size
halfthSAD = thSAD2 # MRecalculate uses a more strict thSAD, which defaults to 150 (half of function's default of 300)
# Input preparation for Interlacing
if not interlaced:
inputP = input
else:
inputP = input.std.SeparateFields(tff=tff)
h = h/2
# Prefilter & Motion Filter
if mfilter is None:
mfilter = inputP
if not GlobalR:
if preclip:
pref = prefilter
elif prefilter <= -1:
pref = inputP
elif prefilter == 3:
expr = 'x {i} < {peak} x {j} > 0 {peak} x {i} - {peak} {j} {i} - / * - ? ?'.format(i=havsfunc
.scale(16, peak), j=havsfunc.scale(75, peak), peak=peak)
pref = core.std.MaskedMerge(inputP.dfttest.DFTTest(tbsize=1, slocation=[0.0,4.0, 0.2,9.0, 1.0,15.0], planes=planes),
inputP,
mvf.GetPlane(inputP, 0).std.Expr(expr=[expr]),
planes=planes)
elif prefilter >= 4:
if chroma:
pref = havsfunc.KNLMeansCL(inputP, d=1, a=1, h=7)
else:
pref = inputP.knlm.KNLMeansCL(d=1, a=1, h=7)
else:
pref = havsfunc.MinBlur(inputP, r=prefilter, planes=planes)
else:
pref = inputP
# Default Auto-Prefilter - Luma expansion TV->PC (up to 16% more values for motion estimation)
if not GlobalR:
pref = havsfunc.DitherLumaRebuild(pref, s0=Str, c=Amp, chroma=chroma)
# Subpixel 3
if pelclip:
nnediMode = 'nnedi3cl' if opencl else 'znedi3'
cshift = 0.25 if pel == 2 else 0.375
pclip = nnedi3_resample.nnedi3_resample(pref, w * pel, h * pel, src_left=cshift, src_top=cshift, nns=4, mode=nnediMode, device=device)
if not GlobalR:
pclip2 = nnedi3_resample.nnedi3_resample(inputP, w * pel, h * pel, src_left=cshift, src_top=cshift, nns=4, mode=nnediMode, device=device)
# Motion vectors search
global bv6, bv4, bv3, bv2, bv1, fv1, fv2, fv3, fv4, fv6
super_args = dict(hpad=hpad, vpad=vpad, pel=pel)
analyse_args = dict(blksize=blksize, search=search, chroma=chroma, truemotion=truemotion, global_=MVglobal, overlap=overlap, dct=dct)
if RefineMotion:
recalculate_args = dict(thsad=halfthSAD, blksize=halfblksize, search=search, chroma=chroma, truemotion=truemotion, overlap=halfoverlap, dct=dct)
if pelclip:
super_search = pref.mv.Super(chroma=chroma, rfilter=4, pelclip=pclip, **super_args)
else:
super_search = pref.mv.Super(chroma=chroma, sharp=subpixel, rfilter=4, **super_args)
if not GlobalR:
if pelclip:
super_render = inputP.mv.Super(levels=1, chroma=plane0, pelclip=pclip2, **super_args)
if RefineMotion:
Recalculate = pref.mv.Super(levels=1, chroma=chroma, pelclip=pclip, **super_args)
else:
super_render = inputP.mv.Super(levels=1, chroma=plane0, sharp=subpixel, **super_args)
if RefineMotion:
Recalculate = pref.mv.Super(levels=1, chroma=chroma, sharp=subpixel, **super_args)
if interlaced:
if tr > 2:
bv6 = super_search.mv.Analyse(isb=True, delta=6, **analyse_args)
fv6 = super_search.mv.Analyse(isb=False, delta=6, **analyse_args)
if RefineMotion:
bv6 = core.mv.Recalculate(Recalculate, bv6, **recalculate_args)
fv6 = core.mv.Recalculate(Recalculate, fv6, **recalculate_args)
if tr > 1:
bv4 = super_search.mv.Analyse(isb=True, delta=4, **analyse_args)
fv4 = super_search.mv.Analyse(isb=False, delta=4, **analyse_args)
if RefineMotion:
bv4 = core.mv.Recalculate(Recalculate, bv4, **recalculate_args)
fv4 = core.mv.Recalculate(Recalculate, fv4, **recalculate_args)
else:
if tr > 2:
bv3 = super_search.mv.Analyse(isb=True, delta=3, **analyse_args)
fv3 = super_search.mv.Analyse(isb=False, delta=3, **analyse_args)
if RefineMotion:
bv3 = core.mv.Recalculate(Recalculate, bv3, **recalculate_args)
fv3 = core.mv.Recalculate(Recalculate, fv3, **recalculate_args)
bv1 = super_search.mv.Analyse(isb=True, delta=1, **analyse_args)
fv1 = super_search.mv.Analyse(isb=False, delta=1, **analyse_args)
if RefineMotion:
bv1 = core.mv.Recalculate(Recalculate, bv1, **recalculate_args)
fv1 = core.mv.Recalculate(Recalculate, fv1, **recalculate_args)
if interlaced or tr > 1:
bv2 = super_search.mv.Analyse(isb=True, delta=2, **analyse_args)
fv2 = super_search.mv.Analyse(isb=False, delta=2, **analyse_args)
if RefineMotion:
bv2 = core.mv.Recalculate(Recalculate, bv2, **recalculate_args)
fv2 = core.mv.Recalculate(Recalculate, fv2, **recalculate_args)
else:
super_render = super_search
# Finally, MDegrain
degrain_args = dict(thsad=thSAD, thsadc=thSADC, plane=plane, limit=limit, limitc=limitc, thscd1=thSCD1, thscd2=thSCD2)
if not GlobalO:
if interlaced:
if tr >= 3:
output = core.mv.Degrain3(mfilter, super_render, bv2, fv2, bv4, fv4, bv6, fv6, **degrain_args)
elif tr == 2:
output = core.mv.Degrain2(mfilter, super_render, bv2, fv2, bv4, fv4, **degrain_args)
else:
output = core.mv.Degrain1(mfilter, super_render, bv2, fv2, **degrain_args)
else:
if tr >= 3:
output = core.mv.Degrain3(mfilter, super_render, bv1, fv1, bv2, fv2, bv3, fv3, **degrain_args)
elif tr == 2:
output = core.mv.Degrain2(mfilter, super_render, bv1, fv1, bv2, fv2, **degrain_args)
else:
output = core.mv.Degrain1(mfilter, super_render, bv1, fv1, **degrain_args)
# Contrasharp (only sharpens luma)
if not GlobalO and if0:
if if1:
if interlaced:
CClip = CClip.std.SeparateFields(tff=tff)
else:
CClip = inputP
# Output
if not GlobalO:
if if0:
if interlaced:
if ifC:
return havsfunc.Weave(ContraSharpening(output, CClip, planes=planes), tff=tff)
else:
return havsfunc.Weave(LSFmod(output, strength=contrasharp, source=CClip, Lmode=0, soothe=False, defaults='slow'), tff=tff)
elif ifC:
return havsfunc.ContraSharpening(output, CClip, planes=planes)
else:
return havsfunc.LSFmod(output, strength=contrasharp, source=CClip, Lmode=0, soothe=False, defaults='slow')
elif interlaced:
return havsfunc.Weave(output, tff=tff)
else:
return output
else:
return input