View difference between Paste ID: bkcbZrzD and rSR9wJA5
SHOW: | | - or go back to the newest paste.
1
# AnimeIVTC() by thetoof
2-
# v2.00 2015-10-12 mod
2+
# v2.00 2015-10-14 mod
3
# last update by AmjadSONY was:- make "precision=1" use "nnedi3" only for "Toriko Anime" by using it like this:- AnimeIVTC(1, precision=1)
4
# add mode22 bool to use it in mode 2 for Spatial field-blend (field-blend in a part of the picture), ex:- AnimeIVTC(2, bbob=1, mode22=true)
5
# TC file now will out in V2 ;) , add ediext clip to use it with QTGMC and add bob4p to change QTGMC Preset , add extbob clip to use an External bobber with bbob/cbob=5 , chrfix in mode 1 and other things fixed by doom9 member
6
# make chrfix work. add dchr bool, can help chrfix with some source, add palf bool for no decimate in mode 1 and make fps 24 instead of 25 for mode 2 in pal source, add tcfv1 bool to made TC file v1 and make it true by default
7
# add nvfr bool for VFR noisy sources, add real30p bool to not use daa with 30p, and some fix in for PAL cases and other things
8
# add TFM (mode, cthresh, blockx, blocky, MI) and some fix
9
# add extbobf string to use external bob function, old external bob clip (extbob) still exist, and if you use both the old extbob will win
10
# make precision=0 use pp=0 in tfm for speed and quality too, add precision=4 same as precision=3 but no blend, add precision=5 to use yadifmod (usefull for very old anime)
11
# add precision=6 to use extbobf\extbob with ConvertFPS to back to original fps, add precision=7 to use extbobf\extbob with SelectEven to back to original fps (no blend), and precision=8 for already half bob fps
12
# add autoAssuf to auto set field to match from in tfm to help with noisy old telecined source, add tfm pp and metric
13
# tweak autoAssuf in mode=1 with another method, others modifications
14
# add tfm micmatching for it important to set it to 0 (zero) in many dvd 20XX anime (maybe other source need that too)
15
# add ediandnn to chose between nnedi3 if set to 1 or eedi3 if set to 2 or eedi3+nnedi3 if set to 3, others modifications
16
# add yadifrepwithtdeint to use yadifmod with less Artefacts
17
# yadifrepwithtdeint more tweak
18
# add o3025cfr to out 25 in PAL and 30 in NTSC, and it useful to avoid blended with omode=1
19
# palf and o3025cfr more tweak, now palf work in pal only and in mode 1 and 3 will out 24
20
# fix nnedi3_rpow2 and make credconv accept external clip for credits
21
# workaround for RgTools Repair in yadifrepwithtdeint
22
# add bobpresmooth for more stability
23
# add bbob4_params and cbob4_params to use more settings for QTGMC, add precision=9 to use bbob (maybe you need it with bbob=4 to use QTGMC with TFM)
24
25
# Read the documentation for all the required info to know how to use this function, With note that there are some things have changed in this MOD
26
27
function AnimeIVTC (clip i, int "mode", int "aa", int "precision", int "killcomb", int "cache", bool "ifade", float "sfthr", bool "sfshow"\
28
		, bool "chrfix" \
29
		, bool "blend" \  
30
		, bool "normconv" \
31
		, int "pattern" \
32
		, int "pass", bool "rendering" \
33-
		, int "i1", int "i2", int "e1", int "e2", int "e3", int "p1", int "p2", bool "dchr", bool "palf", bool "tcfv1", bool "nvfr", bool "real30p", bool "autoAssuf", int "ediandnn", bool "o3025cfr" \
33+
34-
		, int "overlap", int "pel", int "search", bool "nnedi3pel", val "credconv", bool "mode22", clip "ediext", string "bob4p", clip "extbob", string "extbobf", bool "yadifrepwithtdeint", bool "bobpresmooth" \
34+
		, int "i1", int "i2", int "e1", int "e2", int "e3", int "p1", int "p2", bool "dchr", bool "palf", bool "tcfv1", bool "nvfr", bool "real30p", bool "autoAssuf", int "ediandnn", bool "o3025cfr", string "bbob4_params" \
35
		, int "overlap", int "pel", int "search", bool "nnedi3pel", val "credconv", bool "mode22", clip "ediext", string "bob4p", clip "extbob", string "extbobf", bool "yadifrepwithtdeint", bool "bobpresmooth", string "cbob4_params" \
36
		, float "dark", int "thin", int "sharp", int "smooth", bool "stabilize", int "tradius", int "aapel", int "aaov", int "aablk", string "aatype"){
37
38
###General arguments###
39
Assert(defined(mode), "Specify a mode... please read the documentation.")
40
41
#Highest quality by default
42
precision 	= default ( precision, 		  3 ) 
43
44
#change pal fps
45
palf	= default ( palf,         false )
46
o3025cfr	= default ( o3025cfr,       false )
47
48
#VFR default optional
49
nvfr	= default ( nvfr,         false )
50
51
#boolean "creds" is set to true if any of the i/p arguments are defined (i.e. if there's a 30i or 30p section to process)
52
creds = defined(i1) ? true : false
53
creds = defined(i2) ? true : creds
54
creds = defined(p1) ? true : creds
55
creds = defined(p2) ? true : creds
56
creds = defined(e1) ? true : creds
57
creds = defined(e2) ? true : creds
58
creds = defined(e3) ? true : creds
59
60
# real30p
61
real30p	 = default( real30p,        false)
62
63
# auto set field to match from to help with noisy old telecined source
64
autoAssuf= default( autoAssuf,      false)
65
66
###Framerate preparation###
67
normconv = default( normconv, false)
68
#Determine source region by analysing framerate
69
Ifrate 	= round(framerate(i))==30 ? 1 : framerate(i)==25 ? 2 : 3
70
assert(ifrate!=3, "What is your input clip?? It's not 30fps NTSC nor 25fps PAL... maybe you're using the function for a purpose it was not meant to. Please post in the AnimeIVTC thread.")
71
72
#Set the output to the other region if normconv (norm-conversion) is set to true, use same as input if not
73
Ofrate 	= Ifrate==1 && normconv ? 2 : Ifrate==2 && normconv ? 1 : Ifrate
74
75
#prepare arguments for srestore and tdecimate based on user settings (as seen previously)
76
frate  	= Ofrate==1 ? o3025cfr ? 29.970 : 23.976 : palf ? 24 : 25
77
tnum	= Ofrate==1 ? o3025cfr ? 30000 : 24000 : mode!=4 && palf ? 24 : ((mode==1 || mode==3) && o3025cfr) || mode==2 ? 25 : 20
78
tden	= Ofrate==1 ? 1001 : 1
79
80
81
82
#Set pattern=0 (tdecimate) for DHT (safest)
83
pattern		= default ( pattern,		  0 )
84
85
#Set defaults of bobbing filters to use to general optimal quality
86
ediandnn	= default ( ediandnn, 	          1 )
87
yadifreptdei    = default (yadifrepwithtdeint, false)
88
bobpresmooth    = default (bobpresmooth      , false)
89
bbob		= default ( bbob, 	          3 )
90
cbob		= default ( cbob, 	          4 )
91
92-
bob5_with_extbob= (cbob >= 5 || bbob >= 5 || precision >= 6) ? (defined(extbobf) || defined(extbob)) ? 1 : 0 : 2
92+
93
bob5_with_extbob= (cbob >= 5 || bbob >= 5 || (precision >= 6 && precision != 9)) ? (defined(extbobf) || defined(extbob)) ? 1 : 0 : 2
94
Assert(bob5_with_extbob!=0 , "please specify extbobf or extbob if you use bbob=5 or cbob=5.")
95
extbob = defined(extbobf) && !defined(extbob) ? Eval("iforbob." + extbobf) : extbob
96
97
#Sets output mode to 1 (23.976fps for NTSC and 25fps for PAL) when no 30i or 30p sections are specified and sets it to 0 to use NOP() for every operation where it needs to be specified by the user
98
omode = (creds==false && mode!=4) || ofrate==2 || normconv ? 1 : default (omode, 0)
99
Assert(omode!=0, "Specify an output mode (omode)... please read the documentation.")
100
normconv ? assert(mode==2, "Why would you use normconv=true with a telecined source?") : nop()
101
102
###Framerate conversion arguments###
103
bs		= (i.width>1100) ? 16 : 8
104
overlap 	= default ( overlap,	       bs/2 )
105
pel       	= default ( pel, 		  2 )
106
search 		= default ( search, 		  4 )
107
assert(search >= 2, "Using a lower search than 2 will most probably lead to weird artifacts")
108
nnedi3pel 	= default ( nnedi3pel, 	      false )
109
credconv	= default ( credconv, 	    omode==2 ? "a" : "blah" )
110
creds && !isclip(credconv) ? assert(credconv!= "blah", "Please specify credconv. Check in the docs to see which is more appropriate to your source.") : nop()
111
112
#VFR - If this is pass=2 and that the user made a rendering pass out of pass=1, use input directly for decimation instead of recomputing everything
113
pass		= default (pass,		  1 )
114
rendering 	= default ( rendering,	      false )
115
116
#Ask if hard telecine (mode=1) is actually blend telecine (patter = 3 clean frames followed by 2 blended ones)
117
blend		= default ( blend, 	      false )
118
119
#Ask if double chroma blend removal is needed (see guide for more info about this problem)
120
chrfix	= default ( chrfix,       false )
121
dchr	= default ( dchr,       false )
122
123
#mode22 for Spatial field-blend
124
mode22	= default ( mode22,       false )
125
126
#Set cache to 10 frames for all functions using scriptclip to prevent seeking issues + frame order mess-up with temporal filters
127
cache		= default ( cache,		 10 )
128
129
#Let the user decide which edimode (optimal varies greatly depending on source) and degrain (++ affects quality vs speed) settings to use in qTGMC
130
degrain         = default ( degrain,              1 )
131
bob4p		= default ( bob4p, 	   "Slower" )
132
bbob4_params    = default (bbob4_params,          "")
133
cbob4_params    = default (cbob4_params,          "")
134
135
#Set decombing and antialiasing filters to off by default
136
killcomb	= default ( killcomb,		  0 )
137
aa		= default ( aa,			  0 )
138
aa = killcomb==3 && aa==2 ? 0 : aa
139
140
#Set interlaced fade ins detection (and correction to off by default)
141
ifade		= default ( ifade,	       false )
142
#Also turn it off if aa=2 (use daa()), since it applies the same operation on the whole clip as ifade would have had selectively)
143
ifade		= aa==2 || killcomb==3 ? false : ifade
144
sfthr	= default ( sfthr,  0.4 ) 
145
sfshow = default ( sfshow, false )
146
147
###Alright, let's start doing something :p ###
148
149
150
#########
151
#Bobbing#
152
#########
153
154
#Assign clip to prevent double computation
155-
usebbob = chrfix && blend || mode==2
155+
156
emask = (bbob==2 || (bbob==3 && yadifreptdei)) || (cbob==2 || (cbob==3 && yadifreptdei)) || precision==3 ? iforbob.tmm(1) : nop()
157
158
#For later NOP() use if blendbob is not needed
159
usebbob = (chrfix && blend) || mode==2 || precision==9
160
161
162
#Use non-motion-compensated bobber for blended clips since blends have a negative impact on motion detection (if blends are averaged with frames, they can be harder to detect/remove) (mode=2 and mode=1 with blend and chrfix because blend telecine... well has a lot of blends)
163
blendbob 	= usebbob ?	bbob<=0 ? iforbob.tdeint(1) 			 		 	                : 
164-
\				bbob==4 ? iforbob.QTGMC(Preset=bob4p,edimode=edimode,tr2=degrain,ediext= !defined(edimode) && !defined(ediext) ? edeint : ediext)	:
164+
165
\				bbob==2 ? iforbob.tdeint(1,		edeint=edeint,emask=emask)	 	        : 
166
\				bbob==3 ? yadifreptdei ? isyuy2(i) ? i.yadifmod(mode=3,edeint=edeint).Interleaved2Planar.Repair(i.tdeint(1
167
\         ).Interleaved2Planar,Planar=true).Planar2Interleaved : i.yadifmod(mode=3,edeint=edeint).Repair(i.tdeint(1)) : iforbob.yadifmod(mode=1,edeint=edeint) 		 		        :
168
\				bbob==4 ? Eval("iforbob.QTGMC(Preset=bob4p,edimode=edimode,ediext= !defined(edimode) && !defined(ediext) ? edeint : ediext,tr2=degrain" + bbob4_params + ")")	:
169
\				          extbob                      	                                        : NOP()
170
171
#For later NOP() use if clearbob is not needed
172
usecbob = mode==3 || defined(i1) || chrfix && blend==false
173
174
#Allow usage of mo-comp bobber (TempGaussMC) when source is not blended. Highly recommended for stability. (mode=3, interlaced credits, used in chrfix because impact is minimal compared to bob flicker on the whole clip)
175
clearbob 	= usecbob ?	cbob<=0 ? iforbob.tdeint(1)						                : 
176-
\			        cbob==4 ? iforbob.QTGMC(Preset=bob4p,edimode=edimode,tr2=degrain,ediext= !defined(edimode) && !defined(ediext) ? edeint : ediext)	:
176+
177
\				cbob==2 ? iforbob.tdeint(1,		edeint=edeint,emask=emask)	                : 
178
\				cbob==3 ? yadifreptdei ? isyuy2(i) ? i.yadifmod(mode=3,edeint=edeint).Interleaved2Planar.Repair(i.tdeint(1
179
\         ).Interleaved2Planar,Planar=true).Planar2Interleaved : i.yadifmod(mode=3,edeint=edeint).Repair(i.tdeint(1)) : iforbob.yadifmod(mode=1,edeint=edeint)			                :
180
\			        cbob==4 ? Eval("iforbob.QTGMC(Preset=bob4p,edimode=edimode,ediext= !defined(edimode) && !defined(ediext) ? edeint : ediext,tr2=degrain" + cbob4_params + ")")	:
181
\				          extbob                                                        	: NOP()
182
183
############################
184
#Chroma swap between fields# chrfix
185
############################
186
187
#Repair double chroma blending if requested by user using xbob depending on source (known by the mode)
188
chrdeb = chrfix ? blend || mode==2 ? dchr ? blendbob.chroma_rep2(cache) : blendbob.chroma_rep(cache) : dchr ? clearbob.chroma_rep2(cache) : clearbob.chroma_rep(cache) : nop()
189
190
#Remove duplicates (like a safe selecteven/odd) for further processing before final decimation (hybrid decimation (except blend decimation to prevent having 2 calls of tdecimate) or blend telecine IVTC)
191
#Once bobbed, in mode=1 (tdecimate(1,3,5), in mode=1 + blend = back to 30fps for further deblending, in mode=2 srestore will do the job, in mode=3 tdecimate(1,3,5), in mode 4 VFR needs 30, CFR can do it (personal notes)
192
debprep = chrfix ? mode==1 && blend || mode==4 && omode==2 ? chrdeb.tdecimate(1,1,2) : chrdeb : i
193
194
################
195
#Field matching# MODE=1 AND 4
196
################
197
198
usefm = mode==1 || (mode==4 && (pass==2 || creds || omode==1))
199
200
usehfm = mode==4 && pass==1 && usefm==false
201
202
#Field match for mode 1, pass 2 of mode 4 (hybrid without tfm.txt output) or mode=4 with blend decimation (no need for txt file) or mode=4 with interlaced credits (txt file unusable)
203
204
tfmcli2=(precision==0 && ((usefm && !blend) || usehfm)) ? undefined : 
205
\	(precision==1 && ((usefm && !blend) || usehfm)) ? edeint.SelectEven() : 
206
\	(precision==2 && ((usefm && !blend) || usehfm)) ? iforbob.tdeint(2,edeint=edeint) : 
207
\	(precision==3 && ((usefm && !blend) || usehfm)) ? iforbob.tdeint(2,edeint=edeint,emask=emask) : 
208
\	(precision==4 && ((usefm && !blend) || usehfm)) ? iforbob.tdeint(0,edeint=edeint.SelectEven(),emask=iforbob.tmm(0)) : 
209
\	(precision==5 && ((usefm && !blend) || usehfm)) ? yadifreptdei ? isyuy2(i) ? i.yadifmod(mode=2,edeint=edeint.SelectEven()).Interleaved2Planar.Repair(i.tdeint(0).Interleaved2Planar,Planar=true).Planar2Interleaved : i.yadifmod(mode=2,edeint=edeint.SelectEven()).Repair(i.tdeint(0 
210
\         )) : iforbob.yadifmod(edeint=edeint.SelectEven()) : 
211
\	(precision==6 && ((usefm && !blend) || usehfm)) ? Merge(SelectEven(extbob),SelectOdd(extbob)) : 
212
\	(precision==7 && ((usefm && !blend) || usehfm)) ? extbob.SelectEven : 
213
\	(precision==8 && ((usefm && !blend) || usehfm)) ? extbob : 
214
\	(precision==9 && ((usefm && !blend) || usehfm)) ? blendbob.SelectEven : 
215
\	nop()
216
217
#field match for hard telecine or blend telecine restoration for mode==1
218
219
global fm=(precision==0 && usefm) ? blend ? debprep.srestore(omode="pp0") : i.tfm(slow=2,mode=tfmm,cthresh=cthresh,blockx=blockx,blocky=blocky,MI=MI,metric=metric,micmatching=micmatching,pp=0) : 
220
\	  (precision==1 && usefm) ? blend ? debprep.srestore(omode="pp1") : i.tfm(slow=2,mode=tfmm,cthresh=cthresh,blockx=blockx,blocky=blocky,MI=MI,metric=metric,micmatching=micmatching,pp=pp,clip2=tfmcli2) : 
221
\	  (precision==2 && usefm) ? blend ? debprep.srestore(omode="pp2") : i.tfm(slow=2,mode=tfmm,cthresh=cthresh,blockx=blockx,blocky=blocky,MI=MI,metric=metric,micmatching=micmatching,pp=pp,clip2=tfmcli2) : 
222
\	  (precision==3 && usefm) ? blend ? debprep.srestore(omode="pp3") : i.tfm(slow=2,mode=tfmm,cthresh=cthresh,blockx=blockx,blocky=blocky,MI=MI,metric=metric,micmatching=micmatching,pp=pp,clip2=tfmcli2) : 
223
\	  (precision==4 && usefm) ? blend ? debprep.srestore(omode="pp3") : i.tfm(slow=2,mode=tfmm,cthresh=cthresh,blockx=blockx,blocky=blocky,MI=MI,metric=metric,micmatching=micmatching,pp=pp,clip2=tfmcli2) : 
224
\	  (precision==5 && usefm) ? blend ? debprep.srestore(omode="pp3") : i.tfm(slow=2,mode=tfmm,cthresh=cthresh,blockx=blockx,blocky=blocky,MI=MI,metric=metric,micmatching=micmatching,pp=pp,clip2=tfmcli2) : 
225
\	  (precision==6 && usefm) ? blend ? debprep.srestore(omode="pp3") : i.tfm(slow=2,mode=tfmm,cthresh=cthresh,blockx=blockx,blocky=blocky,MI=MI,metric=metric,micmatching=micmatching,pp=pp,clip2=tfmcli2) : 
226
\	  (precision==7 && usefm) ? blend ? debprep.srestore(omode="pp3") : i.tfm(slow=2,mode=tfmm,cthresh=cthresh,blockx=blockx,blocky=blocky,MI=MI,metric=metric,micmatching=micmatching,pp=pp,clip2=tfmcli2) : 
227
\	  (precision==8 && usefm) ? blend ? debprep.srestore(omode="pp3") : i.tfm(slow=2,mode=tfmm,cthresh=cthresh,blockx=blockx,blocky=blocky,MI=MI,metric=metric,micmatching=micmatching,pp=pp,clip2=tfmcli2) : 
228
\	  nop()
229
230
autoAssuf ? eval("""
231
sourford = i.getparity() ? 1 : 0
232
global iyadifmc = i.yadifmod(mode=3, edeint=edeint)
233
234
global invfm = usefm ? autoAssuf ? i.Tfm(field=sourford==1 ? 0 : 1,slow=2,mode=tfmm,cthresh=cthresh,blockx=blockx,blocky=blocky,MI=defined(MI) ? Int(MI*5/4) : undefined,metric=metric,pp=precision==0 ? 0 : pp,clip2=tfmcli2,micmatching=0, mchroma=false) : nop() : nop()
235
fminv2       = usefm ? autoAssuf ? ScriptClip(Interleave(fm,fm), "
236
						invfv=Interleave(iyadifmc.DeleteFrame(0).SelectEven(),invfm).crop(16,16,-16,-16).cFrameDiff(prevf=true, chroma=false, denoise=true, nt=1)
237
						fv   =Interleave(iyadifmc.SelectEven(),                  fm).crop(16,16,-16,-16).cFrameDiff(prevf=true, chroma=false, denoise=true, nt=1)
238
						fv > 0.5 && invfv < 2 ? fv > invfv ? Interleave(invfm,invfm) : last : last
239
						").Selectodd() : fm : nop()
240
fm       = usefm ? autoAssuf ? conditionalfilter(fm,fminv2,fm,"cFrameDiff(prevf=false, chroma=false, denoise=true, nt=2)","<","6") : fm : nop() 
241
""") : nop()
242
243
fm = usefm ? ifade ? fm.smartfademod(sfthr, sfshow, cache) : fm : nop()
244
245
#Field match + tfm.txt output for VFR clips (mode=4, pass=1)
246
247
global hybridfm=(usehfm && precision==0) ? i.tfm(slow=2,mode=tfmm,cthresh=cthresh,blockx=blockx,blocky=blocky,MI=MI, output="tfm.txt",metric=metric,micmatching=micmatching,pp=0) : 
248
\		(usehfm && precision==1) ? i.tfm(slow=2,mode=tfmm,cthresh=cthresh,blockx=blockx,blocky=blocky,MI=MI, output="tfm.txt",metric=metric,micmatching=micmatching,pp=pp,clip2=tfmcli2) : 
249
\		(usehfm && precision==2) ? i.tfm(slow=2,mode=tfmm,cthresh=cthresh,blockx=blockx,blocky=blocky,MI=MI, output="tfm.txt",metric=metric,micmatching=micmatching,pp=pp,clip2=tfmcli2) : 
250
\		(usehfm && precision==3) ? i.tfm(slow=2,mode=tfmm,cthresh=cthresh,blockx=blockx,blocky=blocky,MI=MI, output="tfm.txt",metric=metric,micmatching=micmatching,pp=pp,clip2=tfmcli2) : 
251
\		(usehfm && precision==4) ? i.tfm(slow=2,mode=tfmm,cthresh=cthresh,blockx=blockx,blocky=blocky,MI=MI, output="tfm.txt",metric=metric,micmatching=micmatching,pp=pp,clip2=tfmcli2) : 
252
\		(usehfm && precision==5) ? i.tfm(slow=2,mode=tfmm,cthresh=cthresh,blockx=blockx,blocky=blocky,MI=MI, output="tfm.txt",metric=metric,micmatching=micmatching,pp=pp,clip2=tfmcli2) : 
253
\		(usehfm && precision==6) ? i.tfm(slow=2,mode=tfmm,cthresh=cthresh,blockx=blockx,blocky=blocky,MI=MI, output="tfm.txt",metric=metric,micmatching=micmatching,pp=pp,clip2=tfmcli2) : 
254
\		(usehfm && precision==7) ? i.tfm(slow=2,mode=tfmm,cthresh=cthresh,blockx=blockx,blocky=blocky,MI=MI, output="tfm.txt",metric=metric,micmatching=micmatching,pp=pp,clip2=tfmcli2) : 
255
\		(usehfm && precision==8) ? i.tfm(slow=2,mode=tfmm,cthresh=cthresh,blockx=blockx,blocky=blocky,MI=MI, output="tfm.txt",metric=metric,micmatching=micmatching,pp=pp,clip2=tfmcli2) : 
256
\		nop()
257
258
invhybridfm    = usehfm ? autoAssuf ? conditionalfilter(hybridfm.crop(16,16,-16,-16),i.Tfm(field=sourford==1 ? 0 : 1,slow=2,mode=tfmm,cthresh=cthresh,blockx=blockx,blocky=blocky,MI=defined(MI) ? Int(MI*5/4) : undefined,metric=metric,pp=precision==0 ? 0 : pp,clip2=tfmcli2,micmatching=0, mchroma=false),hybridfm,"IsCombedTIVTC(15,13,false,8,4,1)","=","true") : hybridfm : nop()
259
hybridfm       = usehfm ? autoAssuf ? conditionalfilter(blankclip(hybridfm,length=1).crop(16,16,-16,-16)+invhybridfm.crop(16,16,-16,-16),hybridfm,invhybridfm,"IsCombedTIVTC(10,7,false,4,4,0)","=","true") : hybridfm : nop()
260
261
hybridfm = usehfm ? ifade ? hybridfm.smartfademod(sfthr, sfshow, cache) : hybridfm : nop()
262
263
###############        ######################
264
#Field blended# MODE=2 #Double hard telecine# MODE=3
265
###############	       ######################
266
267
#Since during double chroma blend removal it was checked whether to use blendbob or clearbob depending on the source (blendbob was used for field blended and clearbob was used for double hard telecine), use chrbob when chrfix=true, else use the appropriate one
268
269
fbbob = mode==2 ? chrfix ? debprep : blendbob : nop()
270
271
dhtbob = mode==3 ? chrfix ? debprep : clearbob : nop()
272
273
################
274
#CFR decimation# OMODE=1
275
################
276
277
#HT decimation (mode=1), remove 3 frames out of 5 when chrfix=true since the clip has been bobbed to 60fps
278
fmdecim = mode==1 ? chrfix ? (palf && Ofrate==2) ? debprep.tdecimate(1,13,25) : (o3025cfr) ? debprep.tdecimate(1,3,6) : debprep.tdecimate(1,3,5) : (palf && Ofrate==2) ? fm.tdecimate(1,1,25) : (o3025cfr) ? fm : (autoAssuf) ? fm.tdecimate(1) : fm.tdecimate() : nop()
279
280
#Field blended blend removal & decimation (mode=2) No need to check for chrfix since it was done in the previous step
281
fbdecim = mode==2 ? mode22 ? chrfix ? fbbob.srestore(omode=2, mode=4, thresh=44, cache=cache, dclip=fbbob.crop(16,16,-16,-16)).srestore(frate=frate, mode=4, speed=-4, cache=cache) : 	fbbob.srestore(omode=2, mode=4, thresh=44, cache=cache, dclip=fbbob.crop(16,16,-16,-16)).srestore(frate=frate, mode=-4, speed=-4, cache=cache) 	:
282
\chrfix ? fbbob.srestore(frate=frate, cache=cache) : 	fbbob.srestore(frate=frate, cache=cache, dclip=i.bob(-0.2,0.6).reduceflicker(strength=1)) 	: nop()
283
284
#Pattern finding and decimation for double hard telecine (mode=3) No need to check for chrfix since it was done in the previous step
285
a= mode==3 && (pattern==1 || pattern==-1) ? palf && Ofrate==2 ? dhtbob.selectevery(4,0,2).tdecimate(1,1,25) : o3025cfr ? dhtbob.selectevery(4,0,2) : dhtbob.selectevery(5,0,3) : NOP()
286
b= mode==3 && (pattern==2 || pattern==-1) ? palf && Ofrate==2 ? dhtbob.selectevery(4,1,3).tdecimate(1,1,25) : o3025cfr ? dhtbob.selectevery(4,1,3) : dhtbob.selectevery(5,1,4) : NOP()
287
c= mode==3 && (pattern==3 || pattern==-1) ? palf && Ofrate==2 ? dhtbob.selectevery(4,0,1).tdecimate(1,1,25) : o3025cfr ? dhtbob.selectevery(4,0,1) : dhtbob.selectevery(5,0,2) : NOP()
288
d= mode==3 && (pattern==4 || pattern==-1) ? palf && Ofrate==2 ? dhtbob.selectevery(4,1,2).tdecimate(1,1,25) : o3025cfr ? dhtbob.selectevery(4,1,2) : dhtbob.selectevery(5,1,3) : NOP()
289
e= mode==3 && (pattern==5 || pattern==-1) ? palf && Ofrate==2 ? dhtbob.selectevery(4,2,3).tdecimate(1,1,25) : o3025cfr ? dhtbob.selectevery(4,2,3) : dhtbob.selectevery(5,2,4) : NOP()
290
dhtdecim = mode==3 ? 			pattern==-1 ? stackvertical(stackhorizontal(a.subtitle("pattern=1"),b.subtitle("pattern=2"),c.subtitle("pattern=3")),stackhorizontal(d.subtitle("pattern=4"),e.subtitle("pattern=5"),e.blackness())) : 
291
\					pattern==1 ? a : 
292
\					pattern==2 ? b : 
293
\					pattern==3 ? c : 
294
\					pattern==4 ? d : 
295
\					pattern==5 ? e : 
296
\ palf && Ofrate==2 ? tdecimate(1,13,25) : o3025cfr ? dhtbob.tdecimate(1,3,6) : dhtbob.tdecimate(1,3,5) : nop()
297
298
#Hybrid CFR blend decimation of 30p into 24p
299
blenddecim = (mode==4 && omode==1) ? chrfix ? (palf && Ofrate==2) ? debprep.tdecimate(1,13,25,hybrid=1) : o3025cfr ? debprep.tdecimate(1,3,6) : debprep.tdecimate(1,3,5,hybrid=1) : o3025cfr ? fm : (palf && Ofrate==2) ? fm.tdecimate(1,1,25,hybrid=1) : fm.tdecimate(hybrid=1) : nop()
300
301
#########
302
#Credits# CREDS=TRUE (USER SPECIFIES I1, I2, P1, P2, E1, E2 OR E3)
303
#########
304
305
#Interlaced : use the same smart-bobbed clip as before; we don't care if the background is blended or not since it is unrecoverable anyways (at least for now... see developments about using masking to isolate creds from background)
306
credbob = defined(i1) ? clearbob.selecteven() : nop()
307
308
#Progressive : Blend deinterlace the 24t background while doing minimal damage on the 30p credits
309
blendbg= defined(p1) ? real30p ? i : i.daa3() : nop()
310
311
###CFR conversion###
312
#######################
313
###Framerate conversion (recommended for static or low-motion backgrounds since it is prone artifacts... it reduces framerate/motion based on motion vectors)
314
315
####Convert the framerate of the interlaced credits
316
#Determine if usage of the following conversion is required
317
usemocompi = isclip(credconv) ? defined(i1) : defined(i1) && credconv=="mocomp"
318
319
#External pelclip on source with nnedi, if requested
320
upsi = 		nnedi3pel && usemocompi ? pel==2 ? credbob.nnedi3_rpow2(rfactor=2) : credbob.nnedi3_rpow2(rfactor=2).nnedi3_rpow2(rfactor=2) : nop()
321
322
#Prepare super clip for the interlaced credits
323
credbobsuper	= usemocompi ? 			credbob.	MSuper(pel=pel, levels=0) 					 : nop()
324
credbobupsuper	= usemocompi && nnedi3pel ? 	credbob.	MSuper(pel=pel, levels=0, pelclip=upsi) 			 : nop()
325
326
#Calculate vectors
327
bvi = 		usemocompi ? nnedi3pel ? 	credbobupsuper.	MAnalyse(isb = true,search=search, blksize=bs, overlap=overlap)  : 
328
\						credbobsuper.	MAnalyse(isb = true,search=search, blksize=bs, overlap=overlap)  : nop()
329
330
fvi = 		usemocompi ? nnedi3pel ? 	credbobupsuper.	MAnalyse(isb = false,search=search, blksize=bs, overlap=overlap) : 
331
\						credbobsuper.	MAnalyse(isb = false,search=search, blksize=bs, overlap=overlap) : nop()
332
333
#Apply framerate conversion
334
fpsconvi =  	usemocompi ? nnedi3pel ? 	credbob.	MFlowFps(credbobupsuper,bvi, fvi, tnum, tden) 			 : 
335
\						credbob.	MFlowFps(credbobsuper,  bvi, fvi, tnum, tden) 			 : nop()
336
337
###Convert the framerate of the progressive credits
338
#Determine if usage of the following conversion is required
339
usemocompp = isclip(credconv) ? defined(p1) : defined(p1) && credconv=="mocomp"
340
341
#External pelclip on source with nnedi, if requested
342
upsp = 		nnedi3pel && usemocompp ? pel==2 ? blendbg.nnedi3_rpow2(rfactor=2) : blendbg.nnedi3_rpow2(rfactor=2).nnedi3_rpow2(rfactor=2) : nop()
343
344
#Prepare super clip for the progressive credits
345
blendbgsuper	= usemocompp ?			blendbg.MSuper(pel=pel, levels=0) 						 : nop()
346
blendbgsuperup	= usemocompp && nnedi3pel ? 	blendbg.MSuper(pel=pel, levels=0, pelclip=upsp) 				 : nop()
347
348
#Calculate vectors
349
bvp = 		usemocompp ? nnedi3pel ? 	blendbgsuperup.	MAnalyse(isb = true,search=search, blksize=bs, overlap=overlap)  : 
350
\						blendbgsuper.	MAnalyse(isb = true,search=search, blksize=bs, overlap=overlap)  : nop()
351
352
fvp = 		usemocompp ? nnedi3pel ? 	blendbgsuperup.	MAnalyse(isb = false,search=search, blksize=bs, overlap=overlap) : 
353
\						blendbgsuper.	MAnalyse(isb = false,search=search, blksize=bs, overlap=overlap) : nop()
354
355
#Apply framerate conversion
356
fpsconvp = 	usemocompp ? nnedi3pel ?	blendbg.MFlowFps(blendbgsuperup,bvp, fvp, tnum, tden) 				 : 
357
\						blendbg.MFlowFps(blendbgsuper,  bvp, fvp, tnum, tden) 				 : nop()
358
359
#######################
360
###Blend 30p creds into 24t (ugly and fast, but can lead to less disturbing artifacts when you have a high motion background)
361
blendi = isclip(credconv) ? nop() : defined(i1) && credconv=="blend" ? o3025cfr ? credbob : credbob.convertfps(tnum,tden) : nop()
362
363
blendp = isclip(credconv) ? nop() : defined(p1) && credconv=="blend" ? o3025cfr ? blendbg : blendbg.convertfps(tnum,tden) : nop()
364
365
#######################
366
###Drop frames 30p creds into 24t (ugly and fast, will lead to stuttery motion)
367
drop_credconv = isclip(credconv) ? nop() : (credconv=="drop" && normconv) ? 1 : 0
368
369
assert(drop_credconv!=1, "Please choose another credconv. drop not compatible with normconv=true.")
370
371
dropi = isclip(credconv) ? nop() : defined(i1) && credconv=="drop" ? (mode!=4 && palf && Ofrate==2) ? credbob.tdecimate(1,1,25) : o3025cfr ? credbob : credbob.tdecimate(1) : nop()
372
373
dropp = isclip(credconv) ? nop() : defined(p1) && credconv=="drop" ? (mode!=4 && palf && Ofrate==2) ? blendbg.tdecimate(1,1,25) : o3025cfr ? blendbg : blendbg.tdecimate(1) : nop()
374
375
########
376
#SPLICE# CREDS=TRUE (USER SPECIFIES I1, I2, P1, P2, E1, E2 OR E3)
377
########
378
379
#Determine the "mix" mode (how the different sections (30i, 24t, 30p) are spliced together)
380
#Also check for user error when entering the frame numbers
381
382
mix22a17	= defined(e1) && defined(e2) && defined(e3) && defined(i1) && defined(p1)	? true : false
383
mix7 		= defined(e1) && defined(e2) && defined(e3) && defined(i1) && defined(i2)	? true : false
384
mix14 		= defined(e1) && defined(e2) && defined(e3) && defined(p1) && defined(p2)	? true : false
385
386
mix21a20a16a15 	= defined(e1) && defined(e2) && defined(i1) && defined(p1) && mix22a17==false	? true : false
387
mix6a3 		= defined(e1) && defined(e2) && defined(i1) && defined(i2) && mix7==false	? true : false
388
mix13a10	= defined(e1) && defined(e2) && defined(p1) && defined(p2) && mix14==false	? true : false
389
390
mix19a18	= defined(e1) && defined(p1) && defined(i1) && mix21a20a16a15==false	? true : false 
391
mix9 		= defined(e1) && defined(p1) && defined(p2) && mix13a10==false		? true : false
392
mix2 		= defined(e1) && defined(i1) && defined(i2) && mix6a3==false		? true : false
393
mix4 		= defined(e1) && defined(e2) && defined(i1) && mix21a20a16a15==false && mix6a3==false	? true : false
394
mix11 		= defined(e1) && defined(e2) && defined(p1) && mix21a20a16a15==false && mix13a10==false	? true : false
395
396
mix1a5 	= defined(e1) && defined(i1) && mix19a18==false && mix2==false && mix4==false		? true : false
397
mix8a12 = defined(e1) && defined(p1) && mix19a18==false && mix11==false && mix9==false		? true : false
398
399
400
#Getting the "mix" mode based on user input
401
mix = 0
402
mix = mix1a5 		&& i1<e1 		? 1 : mix
403
mix = mix2 		&& i1<e1<i2 		? 2 : mix
404
mix = mix6a3 		&& i1<e1<i2<e2 		? 3 : mix
405
mix = mix4 		&& e1<i1<e2 		? 4 : mix
406
mix = mix1a5 		&& e1<i1 		? 5 : mix
407
mix = mix6a3 		&& e1<i1<e2<i2 		? 6 : mix
408
mix = mix7 		&& e1<i1<e2<i2<e3 	? 7 : mix
409
mix = mix8a12 		&& p1<e1 		? 8 : mix
410
mix = mix9 		&& p1<e1<p2 		? 9 : mix
411
mix = mix13a10 		&& p1<e1<p2<e2 		? 10 : mix
412
mix = mix11 		&& e1<p1<e2 		? 11 : mix
413
mix = mix8a12 		&& e1<p1 		? 12 : mix
414
mix = mix13a10 		&& e1<p1<e2<p2 		? 13 : mix
415
mix = mix14 		&& e1<p1<e2<p2<e3 	? 14 : mix
416
mix = mix21a20a16a15 	&& e1<i1<e2<p1 		? 15 : mix
417
mix = mix21a20a16a15 	&& i1<e1<p1<e2 		? 16 : mix
418
mix = mix22a17 		&& e1<i1<e2<p1<e3 	? 17 : mix
419
mix = mix19a18 		&& i1<e1<p1 		? 18 : mix
420
mix = mix19a18 		&& p1<e1<i1 		? 19 : mix
421
mix = mix21a20a16a15 	&& e1<p1<e2<i1 		? 20 : mix
422
mix = mix21a20a16a15 	&& p1<e1<i1<e2 		? 21 : mix
423
mix = mix22a17 		&& e1<p1<e2<i1<e3 	? 22 : mix
424
mix = defined(i2) && defined(i1)==false ? 0 : mix
425
mix = defined(p2) && defined(p1)==false ? 0 : mix
426
mix = defined(e2) && defined(e1)==false ? 0 : mix
427
mix = defined(e3) && defined(e2)==false ? 0 : mix
428
mix = defined(e3) && defined(e1)==false ? 0 : mix
429
mix = defined(i1) && defined(e1)==false ? 0 : mix
430
mix = defined(p1) && defined(e1)==false ? 0 : mix
431
creds ? assert(mix!=0, "Mistake in entering i1, e1.... parameters. Please check.") : nop()
432
433
#Determine frame numbers based on mix mode and user input of i/p/e1/2/3
434
istart1=mix==1 || mix==2 || mix==3 || mix==16 || mix==18 					? 0 	: \
435
	mix==4 || mix==5 || mix==6 || mix==7 || mix==15 || mix==17 || mix==19 || mix==21  	? e1+1 	: \
436
	mix==20	|| mix==22									? e2+1  : nop()
437
438
istart2=mix==2 || mix==3 ? e1+1 : mix==6 || mix==7 ? e2+1 : nop()
439
440
pstart1=mix==8 || mix==9 || mix==10 || mix==19 || mix==21 					? 0 	: \
441
	mix==11 || mix==12 || mix==13 || mix==14 || mix==16 || mix==18 || mix==20 || mix==22  	? e1+1 	: \
442
	mix==15	|| mix==17									? e2+1  : nop()
443
444
pstart2=mix==9 || mix==10 ? e1+1 : mix==13 || mix==14 ? e2+1 : nop()
445
446
estart1=mix==4 || mix==5 || mix==6 || mix==7 || mix==11 || mix==12 || mix==13 || mix==14 || mix==15 || mix==17 || mix==20 || mix==22	? 0 	: \
447
	mix==1 || mix==2 || mix==3 || mix==16 || mix==18  										? i1+1 	: \
448
	mix==8 || mix==9 || mix==10 || mix==19 || mix==21										? p1+1  : nop()
449
450
estart2=mix==4 || mix==6 || mix==7 || mix==15 || mix==17 || mix==21  	? i1+1 	: \
451
	mix==3 								? i2+1  : \
452
	mix==11 || mix==13 || mix==14 || mix==16 || mix==20 || mix==22 	? p1+1 	: \
453
	mix==10								? p2+1  : nop()
454
455
estart3=mix==7 ? i2+1 : mix==14 ? p2+1 : mix==17 ? p1+1 : mix==22 ? i1+1 : nop()
456
457
maxfps = defined(i1) ? framerate(i) : defined(p1) ? framerate(i) : nop()
458
459
#Final 24fps ivtced clip and deinterlaced + framerate converted for CFR (omode=1)
460
tocfrfinalt = omode==1 ? (mode==1) ? fmdecim : (mode==2) ? fbdecim : (mode==3) ? dhtdecim : (mode==4 && omode==1) ? blenddecim : nop() : nop()
461
tocfrfinali = omode==1 && creds ? isclip(credconv) ? credconv : (credconv=="mocomp") ? o3025cfr ? credbob : fpsconvi : (credconv=="blend") ? blendi : credconv=="drop" ? dropi : nop() : nop()
462
tocfrfinalp = omode==1 && creds ? isclip(credconv) ? credconv : (credconv=="mocomp") ? o3025cfr ? blendbg : fpsconvp : (credconv=="blend") ? blendp : credconv=="drop" ? dropp : nop() : nop()
463
464
#Final 30fps clips (prepare for hybrid decimation, make everything 30fps) for hybrid use fm since it will be spliced (txt file will be useless) (used when creds=true)
465
tovfrfinalt = omode==2 && creds ? (mode==1) ? fm : (mode==2) ? !(Ofrate==2 && palf) || !normconv ? fbdecim : fbdecim.changefps(maxfps) : (mode==3) ? (o3025cfr && !(palf && Ofrate==2)) ? dhtdecim : dhtdecim.changefps(maxfps) : (mode==4 && omode==2) ? fm : nop() : nop()
466
tovfrfinali = omode==2 && creds ? credbob : nop()
467
tovfrfinalp = omode==2 && creds ? blendbg : nop()
468
469
#Final clip to splice
470
spliceprept = mix!=0 ? omode==1 ? tocfrfinalt : omode==2 ? tovfrfinalt : nop() : nop()
471
spliceprepi = mix!=0 ? omode==1 ? tocfrfinali : omode==2 ? tovfrfinali : nop() : nop()
472
spliceprepp = mix!=0 ? omode==1 ? tocfrfinalp : omode==2 ? tovfrfinalp : nop() : nop()
473
474
#######################
475
###Trim
476
#Factor to get the proper trim values after decimating
477
factor = omode==1 ? defined(i1) ? float(framerate(tocfrfinali)/framerate(i)) : defined(p1) ? float(framerate(tocfrfinalp)/framerate(i)) : nop() : nop()
478
479
et1 = 	defined(e1) ? omode==2 ? spliceprept.trim(estart1,e1) : spliceprept.trim(int(estart1*factor),int(e1*factor)) : nop()
480
et2 = 	defined(e2) ? omode==2 ? spliceprept.trim(estart2,e2) : spliceprept.trim(int(estart2*factor),int(e2*factor)) : nop()
481
et3 = 	defined(e3) ? omode==2 ? spliceprept.trim(estart3,e3) : spliceprept.trim(int(estart3*factor),int(e3*factor)) : nop()
482
483
ci1 = 	defined(i1) ? omode==2 ? spliceprepi.trim(istart1,i1) : spliceprepi.trim(int(istart1*factor),int(i1*factor)) : nop()
484
ci2 = 	defined(i2) ? omode==2 ? spliceprepi.trim(istart2,i2) : spliceprepi.trim(int(istart2*factor),int(i2*factor)) : nop()
485
486
#Prepare 30p sections for splicing
487
cp1 = 	defined(p1) ? omode==2 ? spliceprepp.trim(pstart1,p1) : spliceprepp.trim(int(pstart1*factor),int(p1*factor)) : nop()
488
cp2 = 	defined(p2) ? omode==2 ? spliceprepp.trim(pstart2,p2) : spliceprepp.trim(int(pstart2*factor),int(p2*factor)) : nop()
489
490
spliced=(mix==1) ? 	ci1 + 	et1 : \
491
	(mix==2) ? 	ci1 + 	et1 + 	ci2 : \
492
	(mix==3) ? 	ci1 + 	et1 + 	ci2 + 	et2 : \ 
493
	(mix==4) ? 	et1 + 	ci1 + 	et2 : \
494
	(mix==5) ? 	et1 + 	ci1 : \ 
495
	(mix==6) ? 	et1 + 	ci1 + 	et2 + 	ci2 : \
496
	(mix==7) ? 	et1 + 	ci1 + 	et2 + 	ci2 + 	et3 : \
497
	(mix==8) ? 	cp1 + 	et1 : \
498
	(mix==9) ? 	cp1 + 	et1 + 	cp2 : \
499
	(mix==10) ? 	cp1 + 	et1 + 	cp2 + 	et2 : \ 
500
	(mix==11) ? 	et1 + 	cp1 + 	et2 : \
501
	(mix==12) ? 	et1 + 	cp1 : \ 
502
	(mix==13) ? 	et1 + 	cp1 + 	et2 + 	cp2 : \
503
	(mix==14) ? 	et1 + 	cp1 + 	et2 + 	cp2 + 	et3 : \
504
	(mix==15) ? 	et1 + 	ci1 + 	et2 + 	cp1 : \
505
	(mix==16) ? 	ci1 + 	et1 + 	cp1 + 	et2 : \
506
	(mix==17) ?	et1 + 	ci1 + 	et2 + 	cp1 +	et3 : \
507
	(mix==18) ? 	ci1 + 	et1 + 	cp1 : \
508
	(mix==19) ? 	cp1 + 	et1 + 	ci1 : \ 
509
	(mix==20) ? 	et1 + 	cp1 + 	et2 + 	ci1 : \
510
	(mix==21) ? 	cp1 + 	et1 + 	ci1 + 	et2 : \
511
	(mix==22) ? 	et1 + 	cp1 + 	et2 + 	ci1 +	et3 : \
512
	NOP()
513
514
finalcfr =  omode==1 ? creds ? spliced : tocfrfinalt : nop()
515
tovfrdec1 = omode==2 ? creds ? spliced : chrfix ? debprep : hybridfm : nop()
516
tovfrdec2 = omode==2 ? rendering ? i : creds ? spliced : chrfix ? debprep : fm : nop()
517
518
finalvfr = 	omode==2 && pass==1 ? nvfr ? tovfrdec1.TDecimate(4, output="stats.txt",denoise=true,vidThresh=0.8,dupThresh=0.9) : tovfrdec1.TDecimate(4, output="stats.txt") :
519
\		omode==2 && pass==2 ? nvfr ? tovfrdec2.TDecimate(5,hybrid=2,tfmin=creds ? "" : "tfm.txt",input="stats.txt",mkvout="timecodes.txt",tcfv1=tcfv1,batch=true,vidThresh=0.4,dupThresh=0.6) : tovfrdec2.TDecimate(5,hybrid=2,tfmin=creds ? "" : "tfm.txt",input="stats.txt",mkvout="timecodes.txt",tcfv1=tcfv1,batch=true) : nop()
520
521
topp = omode==1 ? finalcfr : omode==2 ? finalvfr : nop()
522
523
#Kill combing depending on user settings
524
dec = 	killcomb==0 ? topp : 
525
\	killcomb==1 ? topp.vinverse() : 
526
\	killcomb==2 ? topp.vinverseD() : 
527
\	killcomb==3 ? topp.daa3() : 
528
\		interleave(	topp.		 subtitle("killcomb=0",align=9),
529
\				topp.vinverse(). subtitle("killcomb=1",align=9),
530
\				topp.vinverseD().subtitle("killcomb=2",align=9),
531
\				topp.daa3().subtitle("killcomb=3",align=9))
532
533
aaed = 	aa==0 ? dec : 
534
\	aa==1 ? dec.ediaa() : 
535
\	aa==2 ? dec.daa3() : 
536
\	aa==3 ? dec.maa() : 
537
\	aa==4 ? dec.sharpaamcmod(dark,thin,sharp,smooth,stabilize,tradius,aapel,aaov,aablk,aatype) : 
538
\			interleave(	dec.		subtitle("aa=0",align=9),
539
\					dec.ediaa().	subtitle("aa=1",align=9),
540
\					dec.daa3().	subtitle("aa=2",align=9),
541
\					dec.maa().	subtitle("aa=3",align=9),
542
\					dec.sharpaamcmod(dark,thin,sharp,smooth,stabilize,tradius,aapel,aaov,aablk,aatype).subtitle("aa=4",align=9))
543
544
return omode==2 && pass==1 ? finalvfr : aaed
545
}
546
547
548
function chroma_rep(clip source, int "cache")
549
{
550
###### parameters & other necessary vars ######
551
    cache   = default(cache,-1)
552
553
554
###### source preparation & lut ######
555
global  out1    = source
556
global  out2    = mergechroma(source,isyuy2(source) && !(VersionNumber() < 2.60) ? mt_adddiff(source.ConvertToYV16.trim(1,0),mt_makediff(source.ConvertToYV16.trim(1,0),source.ConvertToYV16.trim(2,0),y=1,u=3,v=3),y=1,u=3,v=3).ConvertToYuy2.blur(1) : mt_adddiff(source.trim(1,0),mt_makediff(source.trim(1,0),source.trim(2,0),y=1,u=3,v=3),y=1,u=3,v=3).blur(1))
557
558
559
###### initialise variables ######
560
global  alfr     = -100
561
562
563
###### evaluation call & output calculation ######
564
scriptclip( source, """
565
566
 ### preparation ###
567
        cfr = current_frame
568
        ajmp = alfr+1==cfr
569
        
570
    global  alfr = cfr
571
572
        al_v = LumaDifference(out1.trim(1,0),out1.trim(2,0))
573
        ac_v = ChromaUDifference(out1.trim(1,0),out1.trim(2,0)) + ChromaVDifference(out1.trim(1,0),out1.trim(2,0))
574
575
  ## luma value shifting ##
576
    l10 = ajmp ? l01 : al_v
577
global 		l01	= ajmp ? l12 : al_v
578
global 		l12	= al_v
579
  
580
  ## chroma value shifting ##
581
    c10 = ajmp ? c01 : ac_v
582
global 		c01 = ajmp ? c12 : ac_v
583
global 		c12 = ac_v
584
	
585
 ### condition ###
586
    change  = l12*3<l01 && c01*1.5<c12
587
    chango  = l01*3<l10 && c10*1.5<c01
588
    
589
    change ? out2 : chango ? out1.trim(1,0) : out1
590
591
""")
592
593
594
###### final decimation & caching ######
595
cache<0 ? last : last.RequestLinear(8, cache, 5, false, false)
596
597
return last
598
}
599
600
601
function chroma_rep2(clip source, int "cache")
602
{
603
###### parameters & other necessary vars ######
604
    cache   = default(cache,-1)
605
606
607
###### source preparation & lut ######
608
global  out1    = source
609
global  out2    = mergechroma(source,isyuy2(source) && !(VersionNumber() < 2.60) ? mt_adddiff(source.ConvertToYV16.trim(1,0),mt_makediff(source.ConvertToYV16.trim(1,0),source.ConvertToYV16.trim(2,0),y=1,u=3,v=3),y=1,u=3,v=3).ConvertToYuy2.blur(1) : mt_adddiff(source.trim(1,0),mt_makediff(source.trim(1,0),source.trim(2,0),y=1,u=3,v=3),y=1,u=3,v=3).blur(1))
610
611
612
###### initialise variables ######
613
global  alfr     = -100
614
615
616
###### evaluation call & output calculation ######
617
chrixout = scriptclip( source, """
618
619
 ### preparation ###
620
        cfr = current_frame
621
        ajmp = alfr+1==cfr
622
        
623
    global  alfr = cfr
624
625
        al_v = LumaDifference(out1,out1.trim(1,0))
626
        ac_v = ChromaUDifference(out1,out1.trim(1,0)) + ChromaVDifference(out1,out1.trim(1,0))
627
628
  ## luma value shifting ##
629
    l10 = ajmp ? l01 : al_v
630
global 		l01	= ajmp ? l12 : al_v
631
global 		l12	= al_v
632
  
633
  ## chroma value shifting ##
634
    c10 = ajmp ? c01 : ac_v
635
global 		c01 = ajmp ? c12 : ac_v
636
global 		c12 = ac_v
637
	
638
 ### condition ###
639
    change  = l12*3<l01 && c01*1.5<c12
640
    chango  = l01*3<l10 && c10*1.5<c01
641
    
642
    change ? out2 : chango ? out1.trim(1,0) : out1
643
644
""")
645
646
647
###### final decimation & caching ######
648
chrixout2 = cache<0 ? source.mergechroma(chrixout) : source.mergechroma(chrixout).RequestLinear(8, cache, 5, false, false)
649
650
return chrixout2
651
}
652
653
654
#based on smartfade v0.2 by martino - Aimed at removing interlaced fades in anime. Uses luma difference between two fields as activation threshold.
655
#mod by thetoof : removed degrainmedian post-processing, 
656
#		changed how the fields are blended together (average of 2 nnedi interpolations + contra-sharpening to retain more detail) and 
657
#		added cache parameter to forward the setting of animeivtc to this function
658
#		renamed global parameter to something less general than "threshold"
659
660
function smartfademod(clip c, float "sfthr", bool "sfshow", int "cache") {
661
    
662
global sfthr	= default ( sfthr,  0.4 ) # Threshold for fade detection.
663
show		= default ( sfshow, false ) # Displays luma difference between fields without processing anything.
664
cache		= default ( cache,   10 ) # Cache the frames to help seeking and other functions requesting many frames
665
666
show ? scriptclip(c, "subtitle(string(abs(	  separatefields().selectodd().averageluma() \
667
            				- separatefields().selecteven().averageluma())))") : \
668
       scriptclip(c, "sep = separatefields() \
669
       avg = sep.selectodd().averageluma() - sep.selecteven().averageluma() \
670
       abs(avg) > sfthr ? interleave(sep.selecteven(),sep.selectodd()).weave().daa3() : last")
671
672
cache<0 ? last : last.RequestLinear(8, cache, 5, false, false)
673
674
    return last
675
676
}
677
678
#Suggested by Mystery Keeper in "Denoise of tv-anime" thread
679
function ediaa(clip a) {return a.EEDI2(field=1).TurnRight().EEDI2(field=1).TurnLeft().spline36resize(a.width,a.height,-0.5,-0.5)}
680
681
#Anti-aliasing with contra-sharpening by Didée
682
function daa(clip c) {
683
nn	= c.nnedi2(field=-2)
684
dbl   	= mt_average(selecteven(nn),selectodd(nn),U=3,V=3)
685
dblD 	= mt_makediff(c,dbl,U=3,V=3)
686
shrpD 	= mt_makediff(dbl,dbl.removegrain((width(c)>1100) ? 20 : 11),U=3,V=3)
687
DD 	= shrpD.repair(dblD,13)
688
return dbl.mt_adddiff(DD,U=3,V=3) }
689
690
#Anti-aliasing with edge masking by martino, mask using "sobel" taken from Kintaro's useless filterscripts and modded by thetoof for spline36
691
function maa(clip input, int "mask") {
692
mask = input.mt_edge("sobel",7,7,5,5).mt_inflate()
693
aa_clip=input.spline36Resize(width(input)*2,height(input)*2).TurnLeft().SangNom().TurnRight().SangNom().spline36Resize(width(input),height(input)).MergeChroma(input) 
694
return mt_merge(input,aa_clip,mask) }
695
696
#Developped in the "fine anime antialiasing thread"
697
function SharpAAMCmod(clip orig, float "dark", int "thin", int "sharp", int "smooth", bool "stabilize", int "tradius", int "aapel", int "aaov", int "aablk", string "aatype")
698
{
699
dark	= default ( dark,     0.2 ) # strokes darkening strength
700
thin	= default ( thin,      10 ) # Presharpening
701
sharp	= default ( sharp,    150 ) # Postsharpening
702
smooth	= default ( smooth,    -1 ) # Postsmoothing
703
stabilize= default ( stabilize,false ) # Use post stabilization with Motion Compensation
704
Tradius	= default ( Tradius,    2 ) # 2 = MDegrain2 / 3 = MDegrain3 
705
aapel	= default ( aapel,      1 ) # accuracy of the motion estimation (Value can only be 1, 2 or 4. 1 means a precision to the pixel. 2 means a precision to half a pixel, 4 means a precision to quarter a pixel, produced by spatial interpolation (better but slower).)
706
aaov	= default ( aaov,       (orig.width>1100) ? 8 : 4 ) # block overlap value (horizontal). Must be even and less than block size. (Higher = more precise & slower)
707
aablk	= default ( aablk,      (orig.width>1100) ? 16 : 8 ) # Size of a block (horizontal). It's either 4, 8 or 16 ( default is 8 ). Larger blocks are less sensitive to noise, are faster, but also less accurate.
708
aatype	= default ( aatype, "Sangnom" ) # Use Sangnom() or EEDI2() for anti-aliasing
709
710
w=width(orig)
711
h=height(orig)
712
m=mt_logic( orig.DEdgeMask(0,255,0,255,"5 10 5 0 0 0 -5 -10 -5", divisor=4,Y=3,U=3,V=3)
713
 \       ,orig.DEdgeMask(0,255,0,255,"5 0 -5 10 0 -10 5 0 -5", divisor=4,Y=3,U=3,V=3)
714
 \       ,"max").mt_lut("x 128 / 0.86 ^ 255 *")
715
preaa=(thin==0 && dark==0) ? orig : (thin==0) ? orig.Toon(dark) : (dark==0) ? orig.awarpsharp2(depth=thin) : orig.Toon(dark).awarpsharp2(depth=thin)
716
aa= (aatype=="Sangnom") ? preaa.spline36resize(w*2,h*2).TurnLeft().SangNom().TurnRight().SangNom().spline36resize(w,h) : (aatype=="EEDI2") ? preaa.ediaa() : blankclip(pixel_type="YV12").subtitle("Please use Sangnom or EEDI2 for aatype")
717
postsh=(sharp==0 && smooth==0) ? aa : aa.lsfmod(edgemode=1,strength=sharp,overshoot=1,soft=smooth)
718
merged=mt_merge(orig,postsh,m,Y=3,U=3,V=3)
719
720
sD=mt_makediff(orig,merged)
721
722
origsuper= orig.MSuper(pel=aapel)
723
sDsuper  = sD.  MSuper(pel=aapel, levels=1)
724
725
726
fv3 = tradius==3 ? origsuper.MAnalyse(isb=false,delta=3,overlap=aaov,blksize=aablk) : nop()
727
fv2 = tradius>=2 ? origsuper.MAnalyse(isb=false,delta=2,overlap=aaov,blksize=aablk) : nop()
728
fv1 = tradius>=1 ? origsuper.MAnalyse(isb=false,delta=1,overlap=aaov,blksize=aablk) : nop()
729
bv1 = tradius>=1 ? origsuper.MAnalyse(isb=true, delta=1,overlap=aaov,blksize=aablk) : nop()
730
bv2 = tradius>=2 ? origsuper.MAnalyse(isb=true, delta=2,overlap=aaov,blksize=aablk) : nop()
731
bv3 = tradius==3 ? origsuper.MAnalyse(isb=true, delta=3,overlap=aaov,blksize=aablk) : nop()
732
sDD = tradius==1 ? sD.MDegrain1(sDsuper,bv1,fv1) : tradius==2 ? sD.MDegrain2(sDsuper,bv1,fv1,bv2,fv2) : sD.MDegrain3(sDsuper,bv1,fv1,bv2,fv2,bv3,fv3)
733
734
reduc = 0.4
735
sDD = mt_lutxy(sD,sDD,"x 128 - abs y 128 - abs < x y ?").mergeluma(sDD,1.0-reduc)
736
737
return stabilize ? orig.mt_makediff(sDD,U=2,V=2) : merged
738
}
739
740
# Kill Combing Function from mcbob
741
function VinverseD(clip clp, float "sstr", int "amnt", int "uv")
742
{
743
uv   = default(uv,3)
744
sstr = default(sstr,2.7)
745
amnt = default(amnt,255)
746
uv2  = (uv==2) ? 1 : uv
747
STR  = string(sstr)
748
AMN  = string(amnt)
749
vblur  = clp.mt_convolution("1","50 99 50",U=uv,V=uv)
750
vblurD = mt_makediff(clp,vblur,U=uv2,V=uv2)
751
Vshrp  = mt_lutxy(vblur,vblur.mt_convolution("1","1 4 6 4 1",U=uv2,V=uv2),expr="x x y - "+STR+" * +",U=uv2,V=uv2)
752
VshrpD = mt_makediff(Vshrp,vblur,U=uv2,V=uv2)
753
VlimD  = mt_lutxy(VshrpD,VblurD,expr="x 128 - y 128 - * 0 < x 128 - abs y 128 - abs < x y ? 128 - 0.25 * 128 + x 128 - abs y 128 - abs < x y ? ?",U=uv2,V=uv2)
754
mt_adddiff(Vblur,VlimD,U=uv,V=uv)
755
(amnt>254) ? last : (amnt==0) ? clp : mt_lutxy(clp,last,expr="x "+AMN+" + y < x "+AMN+" + x "+AMN+" - y > x "+AMN+" - y ? ?",U=uv,V=uv) 
756
return(last)
757
}