-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsmdegrain.py
More file actions
327 lines (284 loc) · 16.1 KB
/
Copy pathsmdegrain.py
File metadata and controls
327 lines (284 loc) · 16.1 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import vapoursynth as vs
from vapoursynth import core
from typing import Sequence, Union, Optional
import math
from helpers import scale, Padding, DitherLumaRebuild, DFTTest, GetPlane, KNLMeansCL
from misc import MV, MinBlur
from sharpen import LSFmod, ContraSharpening
from nnedi3_resample 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
is_large = w > 1024 or h > 576
if pel is None:
pel = 1 if is_large else 2
if pel < 2:
subpixel = min(subpixel, 2)
pelclip = pel > 1 and subpixel >= 3
if blksize is None:
blksize = 16 if is_large else 8
blk2 = blksize // 2
if overlap is None:
overlap = blk2
ovl2 = overlap // 2
if truemotion is None:
truemotion = not is_large
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 = scale(limit, peak)
if limitc is None:
limitc = limit
else:
limitc = 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')
# not sure whether this is still true, so I disabled it
#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
EXPR = core.akarin.Expr if hasattr(core, 'akarin') else core.cranexpr.Expr if hasattr(core, 'cranexpr') else core.std.Expr
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=scale(16, peak), j=scale(75, peak), peak=peak)
# Takes the first DFTTest implementation that is loaded, GPU ones first.
filtered = DFTTest(inputP, tbsize=1, slocation=[0.0,4.0, 0.2,9.0, 1.0,15.0], planes=planes)
pref = core.std.MaskedMerge(filtered, inputP, EXPR(GetPlane(inputP, 0), expr=[expr]), planes=planes)
elif prefilter >= 4:
# Takes the first NLMeans implementation that is loaded: nlm_ispc (CPU),
# nlm_cuda (CUDA), knlm (KNLMeansCL, OpenCL). Usually exactly one of
# them is loaded for this filter - nlm_ispc when 'opencl' is off,
# otherwise whichever GPU port was chosen.
# device_type/device_id only reach knlm and nlm_cuda; 'device' is -1 or None
# when no device was pinned, and neither plugin takes that as an index.
pref = KNLMeansCL(inputP, d=1, a=1, h=7, device_type='gpu' if opencl else None,
device_id=device if isinstance(device, int) and device >= 0 else None)
else:
pref = 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 = DitherLumaRebuild(pref, s0=Str, c=Amp, chroma=chroma)
# Motion vectors search
global bv6, bv4, bv3, bv2, bv1, fv1, fv2, fv3, fv4, fv6
super_args = dict(hpad=hpad, vpad=vpad, pel=pel, blksize=blksize, overlap=overlap)
# Subpixel 3
if pelclip:
nnediMode = 'nnedi3cl' if opencl else 'znedi3'
cshift = 0.25 if pel == 2 else 0.375
pclip = 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(inputP, w * pel, h * pel, src_left=cshift, src_top=cshift, nns=4, mode=nnediMode, device=device)
super_search = MV.Super(pref, chroma=chroma, rfilter=4, pelclip=pclip, **super_args)
else:
super_search = MV.Super(pref, chroma=chroma, sharp=subpixel, rfilter=4, **super_args)
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 not GlobalR:
if pelclip:
super_render = MV.Super(inputP, levels=1, chroma=plane0, pelclip=pclip2, **super_args)
if RefineMotion:
Recalculate = MV.Super(pref, levels=1, chroma=chroma, pelclip=pclip, **super_args)
else:
super_render = MV.Super(inputP, levels=1, chroma=plane0, sharp=subpixel, **super_args)
if RefineMotion:
Recalculate = MV.Super(pref, levels=1, chroma=chroma, sharp=subpixel, **super_args)
if interlaced:
if tr > 2:
bv6 = MV.Analyse(super_search, isb=True, delta=6, **analyse_args)
fv6 = MV.Analyse(super_search, isb=False, delta=6, **analyse_args)
if RefineMotion:
bv6 = MV.Recalculate(Recalculate, bv6, **recalculate_args)
fv6 = MV.Recalculate(Recalculate, fv6, **recalculate_args)
if tr > 1:
bv4 = MV.Analyse(super_search, isb=True, delta=4, **analyse_args)
fv4 = MV.Analyse(super_search, isb=False, delta=4, **analyse_args)
if RefineMotion:
bv4 = MV.Recalculate(Recalculate, bv4, **recalculate_args)
fv4 = MV.Recalculate(Recalculate, fv4, **recalculate_args)
else:
if tr > 2:
bv3 = MV.Analyse(super_search, isb=True, delta=3, **analyse_args)
fv3 = MV.Analyse(super_search, isb=False, delta=3, **analyse_args)
if RefineMotion:
bv3 = MV.Recalculate(Recalculate, bv3, **recalculate_args)
fv3 = MV.Recalculate(Recalculate, fv3, **recalculate_args)
bv1 = MV.Analyse(super_search, isb=True, delta=1, **analyse_args)
fv1 = MV.Analyse(super_search, isb=False, delta=1, **analyse_args)
if RefineMotion:
bv1 = MV.Recalculate(Recalculate, bv1, **recalculate_args)
fv1 = MV.Recalculate(Recalculate, fv1, **recalculate_args)
if interlaced or tr > 1:
bv2 = MV.Analyse(super_search, isb=True, delta=2, **analyse_args)
fv2 = MV.Analyse(super_search, isb=False, delta=2, **analyse_args)
if RefineMotion:
bv2 = MV.Recalculate(Recalculate, bv2, **recalculate_args)
fv2 = MV.Recalculate(Recalculate, fv2, **recalculate_args)
else:
super_render = super_search
# Finally, MDegrain
search_params = dict(blksize=blksize, search=search, chroma=chroma, truemotion=truemotion, global_=MVglobal, overlap=overlap, dct=dct)
refine_params = dict(thsad=thSAD2, blksize=blksize // 2, search=search, chroma=chroma, truemotion=truemotion, overlap=overlap // 2, dct=dct) if RefineMotion else None
refine_super = None
if RefineMotion:
# If Recalculate was created earlier in the function it will be in locals()
refine_super = Recalculate if 'Recalculate' in locals() else super_render
vectors = get_motion_vectors(super_search, refine_super, search_params, refine_params, tr, interlaced)
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 = MV.Degrain3(mfilter, super_render, bv2, fv2, bv4, fv4, bv6, fv6, **degrain_args)
elif tr == 2:
output = MV.Degrain2(mfilter, super_render, bv2, fv2, bv4, fv4, **degrain_args)
else:
output = MV.Degrain1(mfilter, super_render, bv2, fv2, **degrain_args)
else:
if tr >= 6:
output = MV.Degrain6(inputP, super_render, vectors['bv1'], vectors['fv1'], vectors['bv2'], vectors['fv2'], vectors['bv3'], vectors['fv3'], vectors['bv4'], vectors['fv4'], vectors['bv5'], vectors['fv5'], vectors['bv6'], vectors['fv6'], **degrain_args)
elif tr == 5:
output = MV.Degrain5(inputP, super_render, vectors['bv1'], vectors['fv1'], vectors['bv2'], vectors['fv2'], vectors['bv3'], vectors['fv3'], vectors['bv4'], vectors['fv4'], vectors['bv5'], vectors['fv5'], **degrain_args)
elif tr == 4:
output = MV.Degrain4(inputP, super_render, vectors['bv1'], vectors['fv1'], vectors['bv2'], vectors['fv2'], vectors['bv3'], vectors['fv3'], vectors['bv4'], vectors['fv4'], **degrain_args)
elif tr == 3:
output = MV.Degrain3(inputP, super_render, vectors['bv1'], vectors['fv1'], vectors['bv2'], vectors['fv2'], vectors['bv3'], vectors['fv3'], **degrain_args)
elif tr == 2:
output = MV.Degrain2(inputP, super_render, vectors['bv1'], vectors['fv1'], vectors['bv2'], vectors['fv2'], **degrain_args)
else:
output = MV.Degrain1(inputP, super_render, vectors['bv1'], vectors['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 Weave(ContraSharpening(output, CClip, planes=planes), tff=tff)
else:
return Weave(LSFmod(output, strength=contrasharp, source=CClip, Lmode=0, soothe=False, defaults='slow'), tff=tff)
elif ifC:
return ContraSharpening(output, CClip, planes=planes)
else:
return LSFmod(output, strength=contrasharp, source=CClip, Lmode=0, soothe=False, defaults='slow')
elif interlaced:
return Weave(output, tff=tff)
else:
return output
else:
return input
# Helpers
def get_motion_vectors(super_search, refine, search_params, refine_params, tr, interlaced):
vectors = {}
if tr >= 1:
vectors['bv1'] = MV.Analyse(super_search, isb=True, delta=1, **search_params)
vectors['fv1'] = MV.Analyse(super_search, isb=False, delta=1, **search_params)
if refine:
vectors['bv1'] = MV.Recalculate(refine, vectors['bv1'], **refine_params)
vectors['fv1'] = MV.Recalculate(refine, vectors['fv1'], **refine_params)
if interlaced or tr >= 2:
vectors['bv2'] = MV.Analyse(super_search, isb=True, delta=2, **search_params)
vectors['fv2'] = MV.Analyse(super_search, isb=False, delta=2, **search_params)
if refine:
vectors['bv2'] = MV.Recalculate(refine, vectors['bv2'], **refine_params)
vectors['fv2'] = MV.Recalculate(refine, vectors['fv2'], **refine_params)
if tr >= 3:
for i in range(3, tr + 1):
vectors[f'bv{i}'] = MV.Analyse(super_search, isb=True, delta=i, **search_params)
vectors[f'fv{i}'] = MV.Analyse(super_search, isb=False, delta=i, **search_params)
if refine:
vectors[f'bv{i}'] = MV.Recalculate(refine, vectors[f'bv{i}'], **refine_params)
vectors[f'fv{i}'] = MV.Recalculate(refine, vectors[f'fv{i}'], **refine_params)
return vectors
def Weave(clip: vs.VideoNode, tff: Optional[bool] = None) -> vs.VideoNode:
if not isinstance(clip, vs.VideoNode):
raise vs.Error('Weave: this is not a clip')
if tff is None:
with clip.get_frame(0) as f:
if f.props.get('_Field') not in [1, 2]:
raise vs.Error('Weave: tff was not specified and field order could not be determined from frame properties')
return clip.std.DoubleWeave(tff=tff)[::2]