View difference between Paste ID: Ti8eJz2p and ALAkX78b
SHOW: | | - or go back to the newest paste.
1
#########################################################################################################################
2
###                                                                                                                   ###
3
###                                                  RemoveDirtMC                                                     ###
4-
###                                                      v1.48                                                        ###
4+
###                                                      v1.49                                                        ###
5
###                                               **SPECIAL EDITION**                                                 ###
6
###                                           	      By NEPHILIS                                                     ###
7
###                                                      @2011                                                        ###
8
###                                    mod by Great Dragon to work with NLMeansCL2                                    ###
9
###                                 mod2 by A.SONY to make it more friendly in motions                                ###
10
#########################################################################################################################
11
12
##################################################################
13
##                                                              ##
14
## --------------                                               ##
15
## Requirements : - MVTools      (v2.5.11.2)                    ##
16
## --------------                                               ##
17
##         - RemoveGrainT (v1.0) package                        ##
18
##         - RemoveDirt   (v0.9) package                        ##
19
##         - AvsRecursion (v0.2)                                ##
20
##         - FluxSmooth   (v1.1)                                ##
21
##                                                              ##
22
##           As Additional                                      ##
23
##              --------                                        ##
24
##        => To Prefiltering with FFT3DFilter                   ##
25
##         - FFT3DFilter  (v2.1.1)                              ##
26
##         - Dither_Luma_Rebuild from SMDegrain or elsewhere    ##
27
##              --------                                        ##
28
##        => To Prefiltering with NLMeansCL2                    ##
29
##         - NLMeansCL2.dll v0.4.0 (2015-01-08)                 ##
30
##                                                              ##
31
##################################################################
32
33
##########################################################################################################################
34
# -------                                                                                                                #
35
# Usage :                                                                                                                #
36
# -------                                                                                                                #
37
#                                                                                                                        #
38
# RemoveDirtMC_SE(clip input, bool"GPU", int"radius", int"repmode", int"clmode", bool"TwoPass", int"thSAD", int"thSADC") #
39
#                                                                                                                        #
40
##########################################################################################################################
41
42
##################
43
# -----------    #
44
# PARAMETERS:    #
45
# -----------    #
46
##################
47
48
##########################################################################################################################
49
#
50
# ----------
51
# GPU       : -- To choose NLMeansCL or FFT3Dfilter for PreFiltering process.
52
# ----------     ATI GPU owners should use "GPU=false" 
53
#	     	      
54
# ----------
55
# Radius    : -- Temporal Radius for MDeGrain at second stage DeNoising.
56
# ----------
57
#
58
# ----------
59
# repmode   : -- Repair mode value in main RemoveDirtSE function.
60
# ----------     
61
#
62
# ----------
63
# clmode    : -- RemoveGrain mode value in main RemoveDirtSE function.
64
# ----------
65
#
66
# ----------
67
# Twopass   : -- To make a Temporal Denoising (MDeGrain) after Spatial Denoising (RemoveDirtSE).   
68
# ----------   
69
#
70
# ----------
71
# thSAD, 
72
# thSADC    : -- MDeGrain's thSAD and thSADC parameters.
73
# ---------- 
74
#
75
##########################################################################################################################
76
77-
Function RemoveDirtMC_SE(clip input, bool"GPU", int"radius", int"repmode", int"clmode", bool"TwoPass", int"thSAD", int"thSADC",float "rStr",float "Amp", bool "luma_rebuild", clip "PreNR", bool "recalculate", int "limit", bool "_grey", int "noisy", int "dist", int "gmthreshold", string "RDSE_params", int "mcthSAD")
77+
Function RemoveDirtMC_SE(clip input, bool"GPU", int"radius", int"repmode", int"clmode", bool"TwoPass", int"thSAD", int"thSADC",float "rStr",float "Amp", bool "luma_rebuild", clip "PreNR", bool "recalculate", int "limit", bool "_grey", int "noisy", int "dist", int "gmthreshold", string "RDSE_params", int "mcthSAD", int "pel", int "thSCD1", int "thSCD2")
78
79
{
80
81-
version  = "v1.48"
81+
version  = "v1.49"
82
83
     GPU = default(GPU,     true)
84
   _grey = default(_grey,  false)
85
  radius = default(radius,     2)
86
 repmode = default(repmode,   16)
87
  clmode = default(clmode,     5)
88
 TwoPass = default(TwoPass, true)
89
   thSAD = default(thSAD ,   150)
90
  thSADC = default(thSADC, thSAD)
91
   rStr  = default( rStr,    1.0)
92
   Amp   = default( Amp,  0.0625)
93-
luma_rebuild = default(luma_rebuild,   true)
93+
 luma_rebuild = default(luma_rebuild,   true)
94-
recalculate  = default(recalculate, TwoPass)
94+
 recalculate  = default(recalculate, TwoPass)
95
    
96
      ox = input.width()
97
      oy = input.height()
98
      HD = (ox > 1099 || oy > 599)
99-
 blksize = (HD==true) ? 16:8
99+
     pel = default( pel, HD ? 1 : 2 )
100
 blksize = HD ? 16 : 8
101-
  search = (HD==true) ?  4:5
101+
102
  search = HD ? 4 : 5
103
 overlap = blksize/2
104
 overlap2= int(overlap/2) + (int(overlap/2)%2)
105
106
         src = input
107
       PreNR = defined(PreNR) ? PreNR : (GPU==true) ? src.NLMeansCL2(h=radius*1.5,u=3,v=3).TemporalDeGrain_SE(5,9,_grey=_grey) \
108
                           : src.FFT3DFilter(sigma=radius*2,plane=_grey ? 0 : 4,bw=16,bh=16,ow=8,oh=8,bt=3,wintype=1).TemporalDeGrain_SE(5,9,_grey=_grey)
109
       PreNR = luma_rebuild ? isyuy2(input) ? PreNR.Interleaved2Planar(_grey).dither_luma_rebuild(S0=rStr,c=Amp,uv=_grey ? 1 : 3).Planar2Interleaved(_grey) \
110-
 PreNR_super = MSuper(PreNR,hpad=4,vpad=4,pel=2,sharp=2,chroma=!_grey)
110+
111-
   src_super = MSuper(src  ,hpad=4,vpad=4,pel=2,sharp=2,levels=1,chroma=!_grey)
111+
 PreNR_super = MSuper(PreNR,hpad=4,vpad=4,pel=pel,sharp=2,chroma=!_grey)
112-
   recalc_sup = recalculate ? MSuper(PreNR  ,hpad=4,vpad=4,pel=2,sharp=2,levels=1,chroma=!_grey) : nop()
112+
   src_super = MSuper(src  ,hpad=4,vpad=4,pel=pel,sharp=2,levels=1,chroma=!_grey)
113
  recalc_sup = recalculate ? MSuper(PreNR  ,hpad=4,vpad=4,pel=pel,sharp=2,levels=1,chroma=!_grey) : nop()
114
115
 bwv1 = MAnalyse(PreNR_super, blksize=blksize, search=search, isb=true,  delta=1, overlap=overlap, truemotion=true,chroma=!_grey) 
116
 bwv2 = (radius>=2) ? \
117
	MAnalyse(PreNR_super, blksize=blksize, search=search, isb=true,  delta=2, overlap=overlap, truemotion=true,chroma=!_grey) : nop()
118
 bwv3 = (radius==3) ? \
119
	MAnalyse(PreNR_super, blksize=blksize, search=search, isb=true,  delta=3, overlap=overlap, truemotion=true,chroma=!_grey) : nop()
120
 fwv1 = MAnalyse(PreNR_super, blksize=blksize, search=search, isb=false, delta=1, overlap=overlap, truemotion=true,chroma=!_grey) 
121
 fwv2 = (radius>=2) ? \
122
	MAnalyse(PreNR_super, blksize=blksize, search=search, isb=false, delta=2, overlap=overlap, truemotion=true,chroma=!_grey) : nop()
123
 fwv3 = (radius==3) ? \
124
	MAnalyse(PreNR_super, blksize=blksize, search=search, isb=false, delta=3, overlap=overlap, truemotion=true,chroma=!_grey) : nop()
125
	
126
	
127
	recalculate ? Eval("""
128
 bwv1 = MRecalculate(recalc_sup,bwv1, blksize=blksize2, search=search, overlap=overlap2,thSAD=thSAD/2, truemotion=true,chroma=!_grey) 
129
 bwv2 = (radius>=2) ? \
130
	MRecalculate(recalc_sup,bwv2, blksize=blksize2, search=search, overlap=overlap2,thSAD=thSAD/2, truemotion=true,chroma=!_grey) : nop()
131
 bwv3 = (radius==3) ? \
132
	MRecalculate(recalc_sup,bwv3, blksize=blksize2, search=search, overlap=overlap2,thSAD=thSAD/2, truemotion=true,chroma=!_grey) : nop()
133
 fwv1 = MRecalculate(src_super,fwv1, blksize=blksize2, search=search, overlap=overlap2,thSAD=thSAD/2, truemotion=true) 
134
 fwv2 = (radius>=2) ? \
135
	MRecalculate(recalc_sup,fwv2, blksize=blksize2, search=search, overlap=overlap2,thSAD=thSAD/2, truemotion=true,chroma=!_grey) : nop()
136
 fwv3 = (radius==3) ? \
137
	MRecalculate(recalc_sup,fwv3, blksize=blksize2, search=search, overlap=overlap2,thSAD=thSAD/2, truemotion=true,chroma=!_grey) : nop()
138
                  """) : nop()
139
	
140-
 bcomp = MCompensate(src,src_super,bwv1,thSAD=TwoPass ? mcthSAD : thSAD) 
140+
141-
 fcomp = MCompensate(src,src_super,fwv1,thSAD=TwoPass ? mcthSAD : thSAD)
141+
 bcomp = MCompensate(src,src_super,bwv1,thSAD=TwoPass ? mcthSAD : thSAD, thSCD1=thSCD1, thSCD2=thSCD2) 
142
 fcomp = MCompensate(src,src_super,fwv1,thSAD=TwoPass ? mcthSAD : thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
143
  
144-
 NR1_super = ( TwoPass==true  ) ? MSuper(NR1,hpad=4,vpad=4,pel=2,sharp=2,levels=1,chroma=!_grey) : nop()
144+
145
 NR1_super = ( TwoPass==true  ) ? MSuper(NR1,hpad=4,vpad=4,pel=pel,sharp=2,levels=1,chroma=!_grey) : nop()
146-
\   	   : (  radius==1  ) ? MDeGrain1(NR1, NR1_super, bwv1, fwv1, thSAD=thSAD, thSADC=thSADC,plane=_grey ? 0 : 4)
146+
147-
\   	   : (  radius==2  ) ? MDeGrain2(NR1, NR1_super, bwv1, bwv2, fwv1, fwv2, thSAD=thSAD, thSADC=thSADC,plane=_grey ? 0 : 4)
147+
\   	   : (  radius==1  ) ? MDeGrain1(NR1, NR1_super, bwv1, fwv1, thSAD=thSAD, thSADC=thSADC,plane=_grey ? 0 : 4, thSCD1=thSCD1, thSCD2=thSCD2)
148-
\   	   : 		       MDeGrain3(NR1, NR1_super, bwv1, bwv2, bwv3, fwv1, fwv2, fwv3, thSAD=thSAD, thSADC=thSADC,plane=_grey ? 0 : 4)
148+
\   	   : (  radius==2  ) ? MDeGrain2(NR1, NR1_super, bwv1, bwv2, fwv1, fwv2, thSAD=thSAD, thSADC=thSADC,plane=_grey ? 0 : 4, thSCD1=thSCD1, thSCD2=thSCD2)
149
\   	   : 		           MDeGrain3(NR1, NR1_super, bwv1, bwv2, bwv3, fwv1, fwv2, fwv3, thSAD=thSAD, thSADC=thSADC,plane=_grey ? 0 : 4, thSCD1=thSCD1, thSCD2=thSCD2)
150
 	Return(NR2)
151
152
}
153
154
function TemporalDeGrain_SE(clip input, int "mode", int "thr", bool "_grey")
155
156
{
157
  _grey       = default(_grey, false)
158
input = isYUY2(input) ? input.Interleaved2Planar(_grey) : input
159
	mode = default(mode, 5)
160
	thr  = default(thr,  8)
161
	rg = RemoveGrain(input, mode=mode, modeU=_grey ? -1 : mode, Planar=true)
162
	tr = TemporalRepair(rg, input, grey=_grey, Planar=true)
163
tr = isYUY2(input) ? tr.Planar2Interleaved(_grey) : tr
164
        fs = FluxSmoothT(tr,thr)
165
	Return(fs)
166
	 
167
}
168
169
function RemoveDirtSE(clip input, clip fcomp, clip bcomp, int "limit", bool "_grey", int "noisy", int "dist", int "gmthreshold", int "repmode", int "clmode", string "RDSE_params")
170
171
{
172
  _grey       = default(_grey, false)
173
  limit       = default(limit,8)
174
  noisy       = default(noisy,12)
175
  dist        = default(dist,1)
176
  gmthreshold = default(gmthreshold,50)
177
  RDSE_params = default(RDSE_params, "") # RestoreMotionBlocks
178
input = isYUY2(input) ? input.Interleaved2Planar(_grey) : input
179
fcomp = isYUY2(input) ? fcomp.Interleaved2Planar(_grey) : fcomp
180
bcomp = isYUY2(input) ? bcomp.Interleaved2Planar(_grey) : bcomp
181
      
182
	repmode = default(repmode, 16)
183
	 clmode = default(clmode,   5)
184-
      clensedn2 = Clense(input, cache=-1, Planar=true, grey=_grey)
184+
185
      clensedn2 = Clense(input, cache=-1, grey=_grey, Planar=true)
186
	 sbegin = ForwardClense(input, cache=-1, grey=_grey, Planar=true)
187
	   send = BackwardClense(input, cache=-1, grey=_grey, Planar=true)
188
	    alt = Repair(SCSelect(input, sbegin, send, clensed, debug=false, Planar=true), input, mode=repmode, modeU=_grey ? -1 : repmode, Planar=true) 
189
	restore = Repair(clensed, input, mode=repmode, modeU=_grey ? -1 : repmode, Planar=true)
190
      corrected = Eval("RestoreMotionBlocks(clensed, restore, neighbour=input, neighbour2=clensedn2, alternative=alt, gmthreshold=gmthreshold, \
191
				      dist=dist, dmode=2, debug=false, noise=limit, noisy=noisy, grey=_grey, Planar=true" + RDSE_params + ")")
192
	 postNR0 = clmode == 0 ? corrected : RemoveGrain(corrected, mode=clmode, modeU=_grey ? -1 : clmode, Planar=true)
193
	 postNR1 = isYUY2(input) ? postNR0.Planar2Interleaved(_grey) : postNR0
194
	 Return(postNR1)
195
	
196
}
197
198
199
function RemoveDirtMC_SE_nofade(clip input, int "cache")
200
{
201
    cache   = default(cache,10)
202
input
203
global rdcpm=0
204
global rdnpm=0
205
global rdcav=0
206
global rdnav=0
207
ScriptClip("""
208
preb=Blur(1.5)
209
RemoveDirtclip=RemoveDirtMC_SE(twopass=false,clmode=0,rstr=1.5,PreNR=FFT3DFilter(sigma=4,plane=4,bw=16,bh=16,ow=8,oh=8,bt=3,wintype=1).ColorYUV(autogain=true).TemporalDeGrain_SE(5,9))
210
211
cfr=current_frame
212
 rdppm=cfr<3 ? 0 : rdcpm
213
global rdcpm=cfr<2 ? 0 : rdnpm
214
global rdnpm=preb.trim(1,0).YPlaneMedian()
215
216
 rdpav=cfr<3 ? 0 : rdcav
217
global rdcav=cfr<2 ? 0 : rdnav
218
global rdnav=preb.trim(1,0).AverageLuma()
219
220
fadedec=(rdcpm > rdnpm && rdcpm < rdppm) || (rdcpm < rdnpm && rdcpm > rdppm)
221
222
fadedec && (abs(rdcav-rdnav) < 12 && abs(rdcav-rdpav) < 12) ? last : RemoveDirtclip
223
224
""")
225
cache<0 ? last : last.RequestLinear(8, cache, 5, false, false)
226
}
227
228
function snofade(clip "o", clip "filtered", int "cache")
229
{
230
    cache   = default(cache,10)
231
o
232
global rdcpm=0
233
global rdnpm=0
234
global rdcav=0
235
global rdnav=0
236
global snofadeclip = filtered
237
ScriptClip("""
238
preb=Blur(1.5)
239
240
cfr=current_frame
241
 rdppm=cfr<3 ? 0 : rdcpm
242
global rdcpm=cfr<2 ? 0 : rdnpm
243
global rdnpm=preb.trim(1,0).YPlaneMedian()
244
245
 rdpav=cfr<3 ? 0 : rdcav
246
global rdcav=cfr<2 ? 0 : rdnav
247
global rdnav=preb.trim(1,0).AverageLuma()
248
249
fadedec=(rdcpm > rdnpm && rdcpm < rdppm) || (rdcpm < rdnpm && rdcpm > rdppm)
250
251
fadedec && (abs(rdcav-rdnav) < 12 && abs(rdcav-rdpav) < 12) ? last : snofadeclip
252
253
""")
254
cache<0 ? last : last.RequestLinear(8, cache, 5, false, false)
255
}
256
257
function snofadeg(clip "o", clip "filtered")
258
{
259
o
260
gScriptClip("""
261
preb=Blur(1.5)
262
263
pav=preb.YPlaneMedian(-1)
264
cav=preb.YPlaneMedian()
265
nav=preb.YPlaneMedian(1)
266
 
267
fadedec=(cav > nav && cav < pav) || (cav < nav && cav > pav)
268
 
269
fadedec && (preb.YDifferenceToNext < 12 && preb.YDifferenceFromPrevious < 12) ? last : filtered
270
 
271
""", args="filtered")
272
}