View difference between Paste ID: ALAkX78b and aJAVJt38
SHOW: | | - or go back to the newest paste.
1
#########################################################################################################################
2
###                                                                                                                   ###
3
###                                                  RemoveDirtMC                                                     ###
4-
###                                                      v1.47                                                        ###
4+
###                                                      v1.48                                                        ###
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")
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")
78
79
{
80
81-
version  = "v1.45"
81+
version  = "v1.48"
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-
   rStr  = default( rStr, 1.0 )
90+
91-
   Amp   = default( Amp, 0.0625 )
91+
   rStr  = default( rStr,    1.0)
92-
luma_rebuild = default(luma_rebuild, true)
92+
   Amp   = default( Amp,  0.0625)
93
luma_rebuild = default(luma_rebuild,   true)
94
recalculate  = default(recalculate, TwoPass)
95
    
96-
      HD = (ox== 1920 || oy>= 800) ? true : false
96+
97
      oy = input.height()
98
      HD = (ox > 1099 || oy > 599)
99
 blksize = (HD==true) ? 16:8
100
 blksize2= int(blksize/2) + (int(blksize/2)%2)
101
  search = (HD==true) ?  4:5
102-
       PreNR = defined(PreNR) ? PreNR : (GPU==true) ? src.NLMeansCL2(h=radius*1.5,u=3,v=3).TemporalDeGrain_SE(5,9) \
102+
103-
                           : src.FFT3DFilter(sigma=radius*2,plane=4,bw=16,bh=16,ow=8,oh=8,bt=3,wintype=1).TemporalDeGrain_SE(5,9)
103+
 overlap2= int(overlap/2) + (int(overlap/2)%2)
104-
       PreNR = luma_rebuild ? isyuy2(input) ? PreNR.Interleaved2Planar().dither_luma_rebuild(S0=rStr,c=Amp).Planar2Interleaved() \
104+
105-
                           : PreNR.dither_luma_rebuild(S0=rStr,c=Amp) : PreNR
105+
106-
 PreNR_super = MSuper(PreNR,hpad=4,vpad=4,pel=2,sharp=2)
106+
       PreNR = defined(PreNR) ? PreNR : (GPU==true) ? src.NLMeansCL2(h=radius*1.5,u=3,v=3).TemporalDeGrain_SE(5,9,_grey=_grey) \
107-
   src_super = MSuper(src  ,hpad=4,vpad=4,pel=2,sharp=2,levels=1)
107+
                           : 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)
108
       PreNR = luma_rebuild ? isyuy2(input) ? PreNR.Interleaved2Planar(_grey).dither_luma_rebuild(S0=rStr,c=Amp,uv=_grey ? 1 : 3).Planar2Interleaved(_grey) \
109-
 bwv1 = MAnalyse(PreNR_super, blksize=blksize, search=search, isb=true,  delta=1, overlap=overlap, truemotion=true) 
109+
                           : PreNR.dither_luma_rebuild(S0=rStr,c=Amp,uv=_grey ? 1 : 3) : PreNR
110
 PreNR_super = MSuper(PreNR,hpad=4,vpad=4,pel=2,sharp=2,chroma=!_grey)
111-
	MAnalyse(PreNR_super, blksize=blksize, search=search, isb=true,  delta=2, overlap=overlap, truemotion=true) : BlankClip()
111+
   src_super = MSuper(src  ,hpad=4,vpad=4,pel=2,sharp=2,levels=1,chroma=!_grey)
112
   recalc_sup = recalculate ? MSuper(PreNR  ,hpad=4,vpad=4,pel=2,sharp=2,levels=1,chroma=!_grey) : nop()
113-
	MAnalyse(PreNR_super, blksize=blksize, search=search, isb=true,  delta=3, overlap=overlap, truemotion=true) : BlankClip()
113+
114-
 fwv1 = MAnalyse(PreNR_super, blksize=blksize, search=search, isb=false, delta=1, overlap=overlap, truemotion=true) 
114+
 bwv1 = MAnalyse(PreNR_super, blksize=blksize, search=search, isb=true,  delta=1, overlap=overlap, truemotion=true,chroma=!_grey) 
115
 bwv2 = (radius>=2) ? \
116-
	MAnalyse(PreNR_super, blksize=blksize, search=search, isb=false, delta=2, overlap=overlap, truemotion=true) : BlankClip()
116+
	MAnalyse(PreNR_super, blksize=blksize, search=search, isb=true,  delta=2, overlap=overlap, truemotion=true,chroma=!_grey) : nop()
117
 bwv3 = (radius==3) ? \
118-
	MAnalyse(PreNR_super, blksize=blksize, search=search, isb=false, delta=3, overlap=overlap, truemotion=true) : BlankClip()
118+
	MAnalyse(PreNR_super, blksize=blksize, search=search, isb=true,  delta=3, overlap=overlap, truemotion=true,chroma=!_grey) : nop()
119
 fwv1 = MAnalyse(PreNR_super, blksize=blksize, search=search, isb=false, delta=1, overlap=overlap, truemotion=true,chroma=!_grey) 
120-
 bcomp = MCompensate(src,src_super,bwv1) 
120+
121-
 fcomp = MCompensate(src,src_super,fwv1)
121+
	MAnalyse(PreNR_super, blksize=blksize, search=search, isb=false, delta=2, overlap=overlap, truemotion=true,chroma=!_grey) : nop()
122
 fwv3 = (radius==3) ? \
123-
 NR1 = src.RemoveDirtSE(fcomp, bcomp, repmode=repmode, clmode=clmode)
123+
	MAnalyse(PreNR_super, blksize=blksize, search=search, isb=false, delta=3, overlap=overlap, truemotion=true,chroma=!_grey) : nop()
124-
 NR1_super = ( TwoPass==true  ) ? MSuper(NR1,hpad=4,vpad=4,pel=2,sharp=2,levels=1) : BlankClip()
124+
125
	
126-
\   	   : (  radius==1  ) ? MDeGrain1(NR1, NR1_super, bwv1, fwv1, thSAD=thSAD, thSADC=thSADC)
126+
	recalculate ? Eval("""
127-
\   	   : (  radius==2  ) ? MDeGrain2(NR1, NR1_super, bwv1, bwv2, fwv1, fwv2, thSAD=thSAD, thSADC=thSADC)
127+
 bwv1 = MRecalculate(recalc_sup,bwv1, blksize=blksize2, search=search, overlap=overlap2,thSAD=thSAD/2, truemotion=true,chroma=!_grey) 
128-
\   	   : 		       MDeGrain3(NR1, NR1_super, bwv1, bwv2, bwv3, fwv1, fwv2, fwv3, thSAD=thSAD, thSADC=thSADC)
128+
129
	MRecalculate(recalc_sup,bwv2, blksize=blksize2, search=search, overlap=overlap2,thSAD=thSAD/2, truemotion=true,chroma=!_grey) : nop()
130
 bwv3 = (radius==3) ? \
131
	MRecalculate(recalc_sup,bwv3, blksize=blksize2, search=search, overlap=overlap2,thSAD=thSAD/2, truemotion=true,chroma=!_grey) : nop()
132
 fwv1 = MRecalculate(src_super,fwv1, blksize=blksize2, search=search, overlap=overlap2,thSAD=thSAD/2, truemotion=true) 
133-
function TemporalDeGrain_SE(clip input, int "mode", int "thr")
133+
134
	MRecalculate(recalc_sup,fwv2, blksize=blksize2, search=search, overlap=overlap2,thSAD=thSAD/2, truemotion=true,chroma=!_grey) : nop()
135
 fwv3 = (radius==3) ? \
136-
input = isYUY2(input) ? input.Interleaved2Planar() : input
136+
	MRecalculate(recalc_sup,fwv3, blksize=blksize2, search=search, overlap=overlap2,thSAD=thSAD/2, truemotion=true,chroma=!_grey) : nop()
137
                  """) : nop()
138
	
139-
	rg = RemoveGrain(input, mode=mode, Planar=true)
139+
140-
	tr = TemporalRepair(rg, input, Planar=true)
140+
 bcomp = MCompensate(src,src_super,bwv1,thSAD=TwoPass ? mcthSAD : thSAD) 
141-
tr = isYUY2(input) ? tr.Planar2Interleaved() : tr
141+
 fcomp = MCompensate(src,src_super,fwv1,thSAD=TwoPass ? mcthSAD : thSAD)
142
  
143
 NR1 = src.RemoveDirtSE(fcomp, bcomp, limit=limit, _grey=_grey, noisy=noisy, dist=dist, gmthreshold=gmthreshold, repmode=repmode, clmode=clmode, RDSE_params=RDSE_params)
144
 NR1_super = ( TwoPass==true  ) ? MSuper(NR1,hpad=4,vpad=4,pel=2,sharp=2,levels=1,chroma=!_grey) : nop()
145
       NR2 = ( TwoPass==false ) ? NR1
146
\   	   : (  radius==1  ) ? MDeGrain1(NR1, NR1_super, bwv1, fwv1, thSAD=thSAD, thSADC=thSADC,plane=_grey ? 0 : 4)
147-
function RemoveDirtSE(clip input, clip fcomp, clip bcomp, int "repmode", int "clmode")
147+
\   	   : (  radius==2  ) ? MDeGrain2(NR1, NR1_super, bwv1, bwv2, fwv1, fwv2, thSAD=thSAD, thSADC=thSADC,plane=_grey ? 0 : 4)
148
\   	   : 		       MDeGrain3(NR1, NR1_super, bwv1, bwv2, bwv3, fwv1, fwv2, fwv3, thSAD=thSAD, thSADC=thSADC,plane=_grey ? 0 : 4)
149
 	Return(NR2)
150-
input = isYUY2(input) ? input.Interleaved2Planar() : input
150+
151-
fcomp = isYUY2(input) ? fcomp.Interleaved2Planar() : fcomp
151+
152-
bcomp = isYUY2(input) ? bcomp.Interleaved2Planar() : bcomp
152+
153
function TemporalDeGrain_SE(clip input, int "mode", int "thr", bool "_grey")
154
155
{
156-
	clensed = interleave(fcomp,input,bcomp).Clense(cache=-1, Planar=true).selectevery(3,1)
156+
  _grey       = default(_grey, false)
157-
      clensedn2 = Clense(input, cache=-1, Planar=true)
157+
input = isYUY2(input) ? input.Interleaved2Planar(_grey) : input
158-
	 sbegin = ForwardClense(input, cache=-1, Planar=true)
158+
159-
	   send = BackwardClense(input, cache=-1, Planar=true)
159+
160-
	    alt = Repair(SCSelect(input, sbegin, send, clensed, debug=false, Planar=true), input, mode=repmode, Planar=true) 
160+
	rg = RemoveGrain(input, mode=mode, modeU=_grey ? -1 : mode, Planar=true)
161-
	restore = Repair(clensed, input, mode=repmode, Planar=true)
161+
	tr = TemporalRepair(rg, input, grey=_grey, Planar=true)
162-
      corrected = RestoreMotionBlocks(clensed, restore, neighbour=input, neighbour2=clensedn2, alternative=alt, gmthreshold=50, \
162+
tr = isYUY2(input) ? tr.Planar2Interleaved(_grey) : tr
163-
				      dist=1, dmode=2, debug=false, noise=8, noisy=12, Planar=true)
163+
164-
	 postNR0 = clmode == 0 ? corrected : RemoveGrain(corrected, mode=clmode, Planar=true)
164+
165-
	 postNR1 = isYUY2(input) ? postNR0.Planar2Interleaved() : postNR0
165+
166
}
167
168
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")
169
170
{
171
  _grey       = default(_grey, false)
172
  limit       = default(limit,8)
173
  noisy       = default(noisy,12)
174
  dist        = default(dist,1)
175
  gmthreshold = default(gmthreshold,50)
176
  RDSE_params = default(RDSE_params, "") # RestoreMotionBlocks
177
input = isYUY2(input) ? input.Interleaved2Planar(_grey) : input
178
fcomp = isYUY2(input) ? fcomp.Interleaved2Planar(_grey) : fcomp
179
bcomp = isYUY2(input) ? bcomp.Interleaved2Planar(_grey) : bcomp
180
      
181
	repmode = default(repmode, 16)
182
	 clmode = default(clmode,   5)
183
	clensed = interleave(fcomp,input,bcomp).Clense(cache=-1, grey=_grey, Planar=true).selectevery(3,1)
184
      clensedn2 = Clense(input, cache=-1, Planar=true, grey=_grey)
185
	 sbegin = ForwardClense(input, cache=-1, grey=_grey, Planar=true)
186
	   send = BackwardClense(input, cache=-1, grey=_grey, Planar=true)
187
	    alt = Repair(SCSelect(input, sbegin, send, clensed, debug=false, Planar=true), input, mode=repmode, modeU=_grey ? -1 : repmode, Planar=true) 
188
	restore = Repair(clensed, input, mode=repmode, modeU=_grey ? -1 : repmode, Planar=true)
189
      corrected = Eval("RestoreMotionBlocks(clensed, restore, neighbour=input, neighbour2=clensedn2, alternative=alt, gmthreshold=gmthreshold, \
190
				      dist=dist, dmode=2, debug=false, noise=limit, noisy=noisy, grey=_grey, Planar=true" + RDSE_params + ")")
191
	 postNR0 = clmode == 0 ? corrected : RemoveGrain(corrected, mode=clmode, modeU=_grey ? -1 : clmode, Planar=true)
192
	 postNR1 = isYUY2(input) ? postNR0.Planar2Interleaved(_grey) : postNR0
193
	 Return(postNR1)
194
	
195
}
196
197
198
function RemoveDirtMC_SE_nofade(clip input, int "cache")
199
{
200
    cache   = default(cache,10)
201
input
202
global rdcpm=0
203
global rdnpm=0
204
global rdcav=0
205
global rdnav=0
206
ScriptClip("""
207
preb=Blur(1.5)
208
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))
209
210
cfr=current_frame
211
 rdppm=cfr<3 ? 0 : rdcpm
212
global rdcpm=cfr<2 ? 0 : rdnpm
213
global rdnpm=preb.trim(1,0).YPlaneMedian()
214
215
 rdpav=cfr<3 ? 0 : rdcav
216
global rdcav=cfr<2 ? 0 : rdnav
217
global rdnav=preb.trim(1,0).AverageLuma()
218
219
fadedec=(rdcpm > rdnpm && rdcpm < rdppm) || (rdcpm < rdnpm && rdcpm > rdppm)
220
221
fadedec && (abs(rdcav-rdnav) < 12 && abs(rdcav-rdpav) < 12) ? last : RemoveDirtclip
222
223
""")
224
cache<0 ? last : last.RequestLinear(8, cache, 5, false, false)
225
}
226
227
function snofade(clip "o", clip "filtered", int "cache")
228
{
229
    cache   = default(cache,10)
230
o
231
global rdcpm=0
232
global rdnpm=0
233
global rdcav=0
234
global rdnav=0
235
global snofadeclip = filtered
236
ScriptClip("""
237
preb=Blur(1.5)
238
239
cfr=current_frame
240
 rdppm=cfr<3 ? 0 : rdcpm
241
global rdcpm=cfr<2 ? 0 : rdnpm
242
global rdnpm=preb.trim(1,0).YPlaneMedian()
243
244
 rdpav=cfr<3 ? 0 : rdcav
245
global rdcav=cfr<2 ? 0 : rdnav
246
global rdnav=preb.trim(1,0).AverageLuma()
247
248
fadedec=(rdcpm > rdnpm && rdcpm < rdppm) || (rdcpm < rdnpm && rdcpm > rdppm)
249
250
fadedec && (abs(rdcav-rdnav) < 12 && abs(rdcav-rdpav) < 12) ? last : snofadeclip
251
252
""")
253
cache<0 ? last : last.RequestLinear(8, cache, 5, false, false)
254
}
255
256
function snofadeg(clip "o", clip "filtered")
257
{
258
o
259
gScriptClip("""
260
preb=Blur(1.5)
261
262
pav=preb.YPlaneMedian(-1)
263
cav=preb.YPlaneMedian()
264
nav=preb.YPlaneMedian(1)
265
 
266
fadedec=(cav > nav && cav < pav) || (cav < nav && cav > pav)
267
 
268
fadedec && (preb.YDifferenceToNext < 12 && preb.YDifferenceFromPrevious < 12) ? last : filtered
269
 
270
""", args="filtered")
271
}