View difference between Paste ID: AbwDXQER and U3necCrZ
SHOW: | | - or go back to the newest paste.
1
# AnimeIVTC() by thetoof
2-
# v2.00 2014-3-10 mod
2+
# v2.00 2014-3-12 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
# 2014-3-11 make chrfix work
7
8
# 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
9
10
function AnimeIVTC (clip i, int "mode", int "aa", int "precision", int "killcomb", int "cache", bool "ifade", float "sfthr", bool "sfshow"\
11
		, bool "chrfix"\
12
		, bool "blend"\  
13
		, bool "normconv"\
14
		, int "pattern"\
15
		, int "pass", bool "rendering" \
16
		, int "bbob", int "cbob", string "edimode", int "degrain", int "omode"\
17
		, int "i1", int "i2", int "e1", int "e2", int "e3", int "p1", int "p2" \
18
		, int "overlap", int "pel", int "search", bool "nnedi3pel", string "credconv", bool "mode22", clip "ediext", string "bob4p", clip "extbob"\
19
		, float "dark", int "thin", int "sharp", int "smooth", bool "stabilize", int "tradius", int "aapel", int "aaov", int "aablk", string "aatype"){
20
21
###General arguments###
22
Assert(defined(mode), "Specify a mode... please read the documentation.")
23
24
#Highest quality by default
25
precision 	= default ( precision, 		  3 ) 
26
27
#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)
28
creds = defined(i1) ? true : false
29
creds = defined(i2) ? true : creds
30
creds = defined(p1) ? true : creds
31
creds = defined(p2) ? true : creds
32
creds = defined(e1) ? true : creds
33
creds = defined(e2) ? true : creds
34
creds = defined(e3) ? true : creds
35
36
###Framerate preparation###
37
normconv = default( normconv, false)
38
#Determine source region by analysing framerate
39
Ifrate 	= round(framerate(i))==30 ? 1 : framerate(i)==25 ? 2 : 3
40
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.")
41
42
#Set the output to the other region if normconv (norm-conversion) is set to true, use same as input if not
43
Ofrate 	= Ifrate==1 && normconv ? 2 : Ifrate==2 && normconv ? 1 : Ifrate
44
45
#prepare arguments for srestore and tdecimate based on user settings (as seen previously)
46
frate  	= Ofrate==1 ? 23.976 : 25 
47
tnum	= Ofrate==1 ? 24000 : 25
48
tden	= Ofrate==1 ? 1001 : 1
49
50
51
52
#Set pattern=0 (tdecimate) for DHT (safest)
53
pattern		= default ( pattern,		  0 )
54
55
#Set defaults of bobbing filters to use to general optimal quality
56
bbob		= default ( bbob, 	          3 )
57
cbob		= default ( cbob, 	          4 )
58
bob4p		= default ( bob4p, 	   "Slower" )
59
60
#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
61
omode = (creds==false && mode!=4) || ofrate==2 || normconv ? 1 : default (omode, 0)
62
Assert(omode!=0, "Specify an output mode... please read the documentation.")
63
normconv ? assert(mode==2, "Why would you use normconv=true with a telecined source?") : nop()
64
65
###Framerate conversion arguments###
66
bs		= (i.width>1100) ? 16 : 8
67
overlap 	= default ( overlap,	       bs/2 )
68
pel       	= default ( pel, 		  2 )
69
search 		= default ( search, 		  4 )
70
assert(search >= 2, "Using a lower search than 2 will most probably lead to weird artifacts")
71
nnedi3pel 	= default ( nnedi3pel, 	      false )
72
credconv	= default ( credconv, 	    omode==2 ? "a" : "blah" )
73
creds ? assert(credconv!= "blah", "Please specify credconv. Check in the docs to see which is more appropriate to your source.") : nop()
74
75
#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
76
pass		= default (pass,		  1 )
77
rendering 	= default ( rendering,	      false )
78
79
#Ask if hard telecine (mode=1) is actually blend telecine (patter = 3 clean frames followed by 2 blended ones)
80
blend		= default ( blend, 	      false )
81
82
#Ask if double chroma blend removal is needed (see guide for more info about this problem)
83
chrfix	= default ( chrfix,       false )
84
85
#mode22 for Spatial field-blend
86
mode22	= default ( mode22,       false )
87
88
#Set cache to 10 frames for all functions using scriptclip to prevent seeking issues + frame order mess-up with temporal filters
89
cache		= default ( cache,		 10 )
90
91
#Let the user decide which edimode (optimal varies greatly depending on source) and degrain (++ affects quality vs speed) settings to use in qTGMC
92
edimode		= default ( edimode, 	   "nnedi3" )
93
degrain		= default ( degrain, 		  1 ) 
94
95
#Set decombing and antialiasing filters to off by default
96
killcomb	= default ( killcomb,		  0 )
97
aa		= default ( aa,			  0 )
98
aa = killcomb==3 && aa==2 ? 0 : aa
99
100
#Set interlaced fade ins detection (and correction to off by default)
101
ifade		= default ( ifade,	       false )
102
#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)
103
ifade		= aa==2 ? false : ifade
104
sfthr	= default ( sfthr,  0.4 ) 
105
sfshow = default ( sfshow, false )
106
107
###Alright, let's start doing something :p ###
108
109
110
#########
111
#Bobbing#
112
#########
113
114
#Assign clip to prevent double computation
115
edeint= i.nnedi3(-2)
116
emask = i.tmm(1)
117
118
#For later NOP() use if blendbob is not needed
119
usebbob = chrfix && blend || mode==2
120
121
122
#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)
123
blendbob 	= usebbob ?	bbob<=0 ? i.tdeint(1) 					 	                : 
124
\				bbob==1 ? i.tdeint(1,		edeint=edeint	) 		                : 
125
\				bbob==2 ? i.tdeint(1,		edeint=edeint,emask=emask) 	                : 
126
\				bbob==3 ? i.yadifmod(mode=1,	edeint=edeint)			                :
127
\				bbob==4 ? i.QTGMC(Preset=bob4p,edimode=edimode,tr2=degrain,ediext=ediext)	:
128
\				          extbob                                                      	        : NOP()
129
130
#For later NOP() use if clearbob is not needed
131
usecbob = mode==3 || defined(i1) || chrfix && blend==false
132
133
#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)
134
clearbob 	= usecbob ?	cbob<=0 ? i.tdeint(1)						                : 
135
\				cbob==1 ? i.tdeint(1,		edeint=edeint)			                : 
136
\				cbob==2 ? i.tdeint(1,		edeint=edeint,emask=emask)	                : 
137
\				cbob==3 ? i.yadifmod(mode=1,	edeint=edeint)			                :
138
\			        cbob==4 ? i.QTGMC(Preset=bob4p,edimode=edimode,tr2=degrain,ediext=ediext)	:
139
\				          extbob                                                      	        : NOP()
140
141
############################
142
#Chroma swap between fields# chrfix
143
############################
144
145
#Repair double chroma blending if requested by user using xbob depending on source (known by the mode)
146
chrdeb = chrfix ? blend || mode==2 ? blendbob.chroma_rep(cache) : clearbob.chroma_rep(cache) : nop()
147
148
#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)
149
#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)
150
debprep = chrfix ? mode==1 && blend || mode==4 && omode==2 ? chrdeb.tdecimate(1,1,2) : chrdeb : i
151
152
################
153
#Field matching# MODE=1 AND 4
154
################
155
156
#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)
157
usefm = mode==1 || (mode==4 && (pass==2 || creds || omode==1))
158
159
#field match for hard telecine or blend telecine restoration for mode==1
160
fm = 	(precision==0 && usefm) ? blend ? debprep.srestore(omode="pp0") : i.tfm(slow=2) : 
161
\	(precision==1 && usefm) ? blend ? debprep.srestore(omode="pp1") : i.tfm(slow=2,clip2=i.nnedi3) : 
162
\	(precision==2 && usefm) ? blend ? debprep.srestore(omode="pp2") : i.tfm(slow=2,clip2=i.tdeint(2,edeint=edeint)) : 
163
\	(precision==3 && usefm) ? blend ? debprep.srestore(omode="pp3") : i.tfm(slow=2,clip2=i.tdeint(2,edeint=edeint,emask=emask)) : 
164
\	nop()
165
fm = usefm ? ifade ? fm.smartfademod(sfthr, sfshow, cache) : fm : nop()
166
167
#Field match + tfm.txt output for VFR clips (mode=4, pass=1)
168
usehfm = mode==4 && pass==1 && usefm==false
169
170
hybridfm = 	(usehfm && precision==0) ? i.tfm(slow=2, output="tfm.txt") : 
171
\		(usehfm && precision==1) ? i.tfm(slow=2, output="tfm.txt",clip2=i.nnedi3) : 
172
\		(usehfm && precision==2) ? i.tfm(slow=2, output="tfm.txt",clip2=i.tdeint(2,edeint=edeint)) : 
173
\		(usehfm && precision==3) ? i.tfm(slow=2, output="tfm.txt",clip2=i.tdeint(2,edeint=edeint,emask=emask)) : 
174
\		nop()
175
hybridfm = usehfm ? ifade ? hybridfm.smartfademod(sfthr, sfshow, cache) : hybridfm : nop()
176
177
###############        ######################
178
#Field blended# MODE=2 #Double hard telecine# MODE=3
179
###############	       ######################
180
181
#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
182
183
fbbob = mode==2 ? chrfix ? debprep : blendbob : nop()
184
185
dhtbob = mode==3 ? chrfix ? debprep : clearbob : nop()
186
187
################
188
#CFR decimation# OMODE=1
189
################
190
191
#HT decimation (mode=1), remove 3 frames out of 5 when chrfix=true since the clip has been bobbed to 60fps
192
fmdecim = mode==1 ? chrfix ? debprep.tdecimate(1,3,5) : 			fm.tdecimate() 											: nop()
193
194
#Field blended blend removal & decimation (mode=2) No need to check for chrfix since it was done in the previous step
195
fbdecim = mode==2 ? mode22 ? chrfix ? fbbob.srestore(omode=2, mode=-2, thresh=44, cache=cache, dclip=fbbob.crop(10,0,-10,0)).srestore(frate=frate, mode=-2, speed=-4, cache=cache) : 	fbbob.srestore(omode=2, mode=2, thresh=44, cache=cache, dclip=fbbob.crop(10,0,-10,0)).srestore(frate=frate, mode=2, speed=-4, cache=cache) 	:
196
\chrfix ? fbbob.srestore(frate=frate, cache=cache) : 	fbbob.srestore(frate=frate, cache=cache, dclip=i.bob(-0.2,0.6).reduceflicker(strength=1)) 	: nop()
197
198
#Pattern finding and decimation for double hard telecine (mode=3) No need to check for chrfix since it was done in the previous step
199
a= mode==3 && (pattern==1 || pattern==-1) ? dhtbob.selectevery(5,0,3) : NOP()
200
b= mode==3 && (pattern==2 || pattern==-1) ? dhtbob.selectevery(5,1,4) : NOP()
201
c= mode==3 && (pattern==3 || pattern==-1) ? dhtbob.selectevery(5,0,2) : NOP()
202
d= mode==3 && (pattern==4 || pattern==-1) ? dhtbob.selectevery(5,1,3) : NOP()
203
e= mode==3 && (pattern==5 || pattern==-1) ? dhtbob.selectevery(5,2,4) : NOP()
204
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())) : 
205
\					pattern==1 ? a : 
206
\					pattern==2 ? b : 
207
\					pattern==3 ? c : 
208
\					pattern==4 ? d : 
209
\					pattern==5 ? e : 
210
\					dhtbob.tdecimate(1,3,5) : nop()
211
212
#Hybrid CFR blend decimation of 30p into 24p
213
blenddecim = (mode==4 && omode==1) ? chrfix ? debprep.tdecimate(1,3,5,hybrid=1) : 	fm.tdecimate(hybrid=1) 				: nop()
214
215
#########
216
#Credits# CREDS=TRUE (USER SPECIFIES I1, I2, P1, P2, E1, E2 OR E3)
217
#########
218
219
#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)
220
credbob = defined(i1) ? clearbob.selecteven() : nop()
221
222
#Progressive : Blend deinterlace the 24t background while doing minimal damage on the 30p credits
223
blendbg= defined(p1) ? i.daa() : nop()
224
225
###CFR conversion###
226
#######################
227
###Framerate conversion (recommended for static or low-motion backgrounds since it is prone artifacts... it reduces framerate/motion based on motion vectors)
228
229
####Convert the framerate of the interlaced credits
230
#Determine if usage of the following conversion is required
231
usemocompi = defined(i1) && credconv=="mocomp"
232
233
#External pelclip on source with nnedi, if requested
234
upsi = 		nnedi3pel && usemocompi ? 	credbob.	nnedi3_rpow2(rfactor=pel,cshift="spline36resize") 		 : nop()
235
upsp = 		nnedi3pel && usemocompi ? 	blendbg.	nnedi3_rpow2(rfactor=pel,cshift="spline36resize") 		 : nop()
236
237
#Prepare super clip for the interlaced credits
238
credbobsuper	= usemocompi ? 			credbob.	MSuper(pel=pel, levels=0) 					 : nop()
239
credbobupsuper	= usemocompi && nnedi3pel ? 	credbob.	MSuper(pel=pel, levels=0, pelclip=upsi) 			 : nop()
240
241
#Calculate vectors
242
bvi = 		usemocompi ? nnedi3pel ? 	credbobupsuper.	MAnalyse(isb = true,search=search, blksize=bs, overlap=overlap)  : 
243
\						credbobsuper.	MAnalyse(isb = true,search=search, blksize=bs, overlap=overlap)  : nop()
244
245
fvi = 		usemocompi ? nnedi3pel ? 	credbobupsuper.	MAnalyse(isb = false,search=search, blksize=bs, overlap=overlap) : 
246
\						credbobsuper.	MAnalyse(isb = false,search=search, blksize=bs, overlap=overlap) : nop()
247
248
#Apply framerate conversion
249
fpsconvi =  	usemocompi ? nnedi3pel ? 	credbob.	MFlowFps(credbobupsuper,bvi, fvi, tnum, tden) 			 : 
250
\						credbob.	MFlowFps(credbobsuper,  bvi, fvi, tnum, tden) 			 : nop()
251
252
###Convert the framerate of the progressive credits
253
#Determine if usage of the following conversion is required
254
usemocompp = defined(p1) && credconv=="mocomp"
255
256
#Prepare super clip for the progressive credits
257
blendbgsuper	= usemocompp ?			blendbg.MSuper(pel=pel, levels=0) 						 : nop()
258
blendbgsuperup	= usemocompp && nnedi3pel ? 	blendbg.MSuper(pel=pel, levels=0, pelclip=upsp) 				 : nop()
259
260
#Calculate vectors
261
bvp = 		usemocompp ? nnedi3pel ? 	blendbgsuperup.	MAnalyse(isb = true,search=search, blksize=bs, overlap=overlap)  : 
262
\						blendbgsuper.	MAnalyse(isb = true,search=search, blksize=bs, overlap=overlap)  : nop()
263
264
fvp = 		usemocompp ? nnedi3pel ? 	blendbgsuperup.	MAnalyse(isb = false,search=search, blksize=bs, overlap=overlap) : 
265
\						blendbgsuper.	MAnalyse(isb = false,search=search, blksize=bs, overlap=overlap) : nop()
266
267
#Apply framerate conversion
268
fpsconvp = 	usemocompp ? nnedi3pel ?	blendbg.MFlowFps(blendbgsuperup,bvp, fvp, tnum, tden) 				 : 
269
\						blendbg.MFlowFps(blendbgsuper,  bvp, fvp, tnum, tden) 				 : nop()
270
271
#######################
272
###Blend 30p creds into 24t (ugly and fast, but can lead to less disturbing artifacts when you have a high motion background)
273
blendi = defined(i1) && credconv=="blend" ? credbob.convertfps(tnum,tden) : nop()
274
275
blendp = defined(p1) && credconv=="blend" ? blendbg.convertfps(tnum,tden) : nop()
276
277
#######################
278
###Drop frames 30p creds into 24t (ugly and fast, will lead to stuttery motion)
279
dropi = defined(i1) && credconv=="drop" ? credbob.tdecimate(1) : nop()
280
281
dropp = defined(p1) && credconv=="drop" ? blendbg.tdecimate(1) : nop()
282
283
########
284
#SPLICE# CREDS=TRUE (USER SPECIFIES I1, I2, P1, P2, E1, E2 OR E3)
285
########
286
287
#Determine the "mix" mode (how the different sections (30i, 24t, 30p) are spliced together)
288
#Also check for user error when entering the frame numbers
289
290
mix22a17	= defined(e1) && defined(e2) && defined(e3) && defined(i1) && defined(p1)	? true : false
291
mix7 		= defined(e1) && defined(e2) && defined(e3) && defined(i1) && defined(i2)	? true : false
292
mix14 		= defined(e1) && defined(e2) && defined(e3) && defined(p1) && defined(p2)	? true : false
293
294
mix21a20a16a15 	= defined(e1) && defined(e2) && defined(i1) && defined(p1) && mix22a17==false	? true : false
295
mix6a3 		= defined(e1) && defined(e2) && defined(i1) && defined(i2) && mix7==false	? true : false
296
mix13a10	= defined(e1) && defined(e2) && defined(p1) && defined(p2) && mix14==false	? true : false
297
298
mix19a18	= defined(e1) && defined(p1) && defined(i1) && mix21a20a16a15==false	? true : false 
299
mix9 		= defined(e1) && defined(p1) && defined(p2) && mix13a10==false		? true : false
300
mix2 		= defined(e1) && defined(i1) && defined(i2) && mix6a3==false		? true : false
301
mix4 		= defined(e1) && defined(e2) && defined(i1) && mix21a20a16a15==false && mix6a3==false	? true : false
302
mix11 		= defined(e1) && defined(e2) && defined(p1) && mix21a20a16a15==false && mix13a10==false	? true : false
303
304
mix1a5 	= defined(e1) && defined(i1) && mix19a18==false && mix2==false && mix4==false		? true : false
305
mix8a12 = defined(e1) && defined(p1) && mix19a18==false && mix11==false && mix9==false		? true : false
306
307
308
#Getting the "mix" mode based on user input
309
mix = 0
310
mix = mix1a5 		&& i1<e1 		? 1 : mix
311
mix = mix2 		&& i1<e1<i2 		? 2 : mix
312
mix = mix6a3 		&& i1<e1<i2<e2 		? 3 : mix
313
mix = mix4 		&& e1<i1<e2 		? 4 : mix
314
mix = mix1a5 		&& e1<i1 		? 5 : mix
315
mix = mix6a3 		&& e1<i1<e2<i2 		? 6 : mix
316
mix = mix7 		&& e1<i1<e2<i2<e3 	? 7 : mix
317
mix = mix8a12 		&& p1<e1 		? 8 : mix
318
mix = mix9 		&& p1<e1<p2 		? 9 : mix
319
mix = mix13a10 		&& p1<e1<p2<e2 		? 10 : mix
320
mix = mix11 		&& e1<p1<e2 		? 11 : mix
321
mix = mix8a12 		&& e1<p1 		? 12 : mix
322
mix = mix13a10 		&& e1<p1<e2<p2 		? 13 : mix
323
mix = mix14 		&& e1<p1<e2<p2<e3 	? 14 : mix
324
mix = mix21a20a16a15 	&& e1<i1<e2<p1 		? 15 : mix
325
mix = mix21a20a16a15 	&& i1<e1<p1<e2 		? 16 : mix
326
mix = mix22a17 		&& e1<i1<e2<p1<e3 	? 17 : mix
327
mix = mix19a18 		&& i1<e1<p1 		? 18 : mix
328
mix = mix19a18 		&& p1<e1<i1 		? 19 : mix
329
mix = mix21a20a16a15 	&& e1<p1<e2<i1 		? 20 : mix
330
mix = mix21a20a16a15 	&& p1<e1<i1<e2 		? 21 : mix
331
mix = mix22a17 		&& e1<p1<e2<i1<e3 	? 22 : mix
332
mix = defined(i2) && defined(i1)==false ? 0 : mix
333
mix = defined(p2) && defined(p1)==false ? 0 : mix
334
mix = defined(e2) && defined(e1)==false ? 0 : mix
335
mix = defined(e3) && defined(e2)==false ? 0 : mix
336
mix = defined(e3) && defined(e1)==false ? 0 : mix
337
mix = defined(i1) && defined(e1)==false ? 0 : mix
338
mix = defined(p1) && defined(e1)==false ? 0 : mix
339
creds ? assert(mix!=0, "Mistake in entering i1, e1.... parameters. Please check.") : nop()
340
341
#Determine frame numbers based on mix mode and user input of i/p/e1/2/3
342
istart1=mix==1 || mix==2 || mix==3 || mix==16 || mix==18 					? 0 	: \
343
	mix==4 || mix==5 || mix==6 || mix==7 || mix==15 || mix==17 || mix==19 || mix==21  	? e1+1 	: \
344
	mix==20	|| mix==22									? e2+1  : nop()
345
346
istart2=mix==2 || mix==3 ? e1+1 : mix==6 || mix==7 ? e2+1 : nop()
347
348
pstart1=mix==8 || mix==9 || mix==10 || mix==19 || mix==21 					? 0 	: \
349
	mix==11 || mix==12 || mix==13 || mix==14 || mix==16 || mix==18 || mix==20 || mix==22  	? e1+1 	: \
350
	mix==15	|| mix==17									? e2+1  : nop()
351
352
pstart2=mix==9 || mix==10 ? e1+1 : mix==13 || mix==14 ? e2+1 : nop()
353
354
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 	: \
355
	mix==1 || mix==2 || mix==3 || mix==16 || mix==18  										? i1+1 	: \
356
	mix==8 || mix==9 || mix==10 || mix==19 || mix==21										? p1+1  : nop()
357
358
estart2=mix==4 || mix==6 || mix==7 || mix==15 || mix==17 || mix==21  	? i1+1 	: \
359
	mix==3 								? i2+1  : \
360
	mix==11 || mix==13 || mix==14 || mix==16 || mix==20 || mix==22 	? p1+1 	: \
361
	mix==10								? p2+1  : nop()
362
363
estart3=mix==7 ? i2+1 : mix==14 ? p2+1 : mix==17 ? p1+1 : mix==22 ? i1+1 : nop()
364
365
maxfps = defined(i1) ? framerate(i) : defined(p1) ? framerate(i) : nop()
366
367
#Final 24fps ivtced clip and deinterlaced + framerate converted for CFR (omode=1)
368
tocfrfinalt = omode==1 ? (mode==1) ? fmdecim : (mode==2) ? fbdecim : (mode==3) ? dhtdecim : (mode==4 && omode==1) ? blenddecim : nop() : nop()
369
tocfrfinali = omode==1 && creds ? (credconv=="mocomp") ? fpsconvi : (credconv=="blend") ? blendi : credconv=="drop" ? dropi : nop() : nop()
370
tocfrfinalp = omode==1 && creds ? (credconv=="mocomp") ? fpsconvp : (credconv=="blend") ? blendp : credconv=="drop" ? dropp : nop() : nop()
371
372
#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)
373
tovfrfinalt = omode==2 && creds ? (mode==1) ? fm : (mode==2) ? fbdecim.changefps(maxfps) : (mode==3) ? dhtdecim.changefps(maxfps) : (mode==4 && omode==2) ? fm : nop() : nop()
374
tovfrfinali = omode==2 && creds ? credbob : nop()
375
tovfrfinalp = omode==2 && creds ? blendbg : nop()
376
377
#Final clip to splice
378
spliceprept = mix!=0 ? omode==1 ? tocfrfinalt : omode==2 ? tovfrfinalt : nop() : nop()
379
spliceprepi = mix!=0 ? omode==1 ? tocfrfinali : omode==2 ? tovfrfinali : nop() : nop()
380
spliceprepp = mix!=0 ? omode==1 ? tocfrfinalp : omode==2 ? tovfrfinalp : nop() : nop()
381
382
#######################
383
###Trim
384
#Factor to get the proper trim values after decimating
385
factor = omode==1 ? defined(i1) ? float(framerate(tocfrfinali)/framerate(i)) : defined(p1) ? float(framerate(tocfrfinalp)/framerate(i)) : nop() : nop()
386
387
et1 = 	defined(e1) ? omode==2 ? spliceprept.trim(estart1,e1) : spliceprept.trim(int(estart1*factor),int(e1*factor)) : nop()
388
et2 = 	defined(e2) ? omode==2 ? spliceprept.trim(estart2,e2) : spliceprept.trim(int(estart2*factor),int(e2*factor)) : nop()
389
et3 = 	defined(e3) ? omode==2 ? spliceprept.trim(estart3,e3) : spliceprept.trim(int(estart3*factor),int(e3*factor)) : nop()
390
391
ci1 = 	defined(i1) ? omode==2 ? spliceprepi.trim(istart1,i1) : spliceprepi.trim(int(istart1*factor),int(i1*factor)) : nop()
392
ci2 = 	defined(i2) ? omode==2 ? spliceprepi.trim(istart2,i2) : spliceprepi.trim(int(istart2*factor),int(i2*factor)) : nop()
393
394
#Prepare 30p sections for splicing
395
cp1 = 	defined(p1) ? omode==2 ? spliceprepp.trim(pstart1,p1) : spliceprepp.trim(int(pstart1*factor),int(p1*factor)) : nop()
396
cp2 = 	defined(p2) ? omode==2 ? spliceprepp.trim(pstart2,p2) : spliceprepp.trim(int(pstart2*factor),int(p2*factor)) : nop()
397
398
spliced=(mix==1) ? 	ci1 + 	et1 : \
399
	(mix==2) ? 	ci1 + 	et1 + 	ci2 : \
400
	(mix==3) ? 	ci1 + 	et1 + 	ci2 + 	et2 : \ 
401
	(mix==4) ? 	et1 + 	ci1 + 	et2 : \
402
	(mix==5) ? 	et1 + 	ci1 : \ 
403
	(mix==6) ? 	et1 + 	ci1 + 	et2 + 	ci2 : \
404
	(mix==7) ? 	et1 + 	ci1 + 	et2 + 	ci2 + 	et3 : \
405
	(mix==8) ? 	cp1 + 	et1 : \
406
	(mix==9) ? 	cp1 + 	et1 + 	cp2 : \
407
	(mix==10) ? 	cp1 + 	et1 + 	cp2 + 	et2 : \ 
408
	(mix==11) ? 	et1 + 	cp1 + 	et2 : \
409
	(mix==12) ? 	et1 + 	cp1 : \ 
410
	(mix==13) ? 	et1 + 	cp1 + 	et2 + 	cp2 : \
411
	(mix==14) ? 	et1 + 	cp1 + 	et2 + 	cp2 + 	et3 : \
412
	(mix==15) ? 	et1 + 	ci1 + 	et2 + 	cp1 : \
413
	(mix==16) ? 	ci1 + 	et1 + 	cp1 + 	et2 : \
414
	(mix==17) ?	et1 + 	ci1 + 	et2 + 	cp1 +	et3 : \
415
	(mix==18) ? 	ci1 + 	et1 + 	cp1 : \
416
	(mix==19) ? 	cp1 + 	et1 + 	ci1 : \ 
417
	(mix==20) ? 	et1 + 	cp1 + 	et2 + 	ci1 : \
418
	(mix==21) ? 	cp1 + 	et1 + 	ci1 + 	et2 : \
419
	(mix==22) ? 	et1 + 	cp1 + 	et2 + 	ci1 +	et3 : \
420
	NOP()
421
422
finalcfr =  omode==1 ? creds ? spliced : tocfrfinalt : nop()
423
tovfrdec1 = omode==2 ? creds ? spliced : chrfix ? debprep : hybridfm : nop()
424
tovfrdec2 = omode==2 ? rendering ? i : creds ? spliced : chrfix ? debprep : fm : nop()
425
426
finalvfr = 	omode==2 && pass==1 ? tovfrdec1.TDecimate(4, output="stats.txt",denoise=true,vidThresh=0.8) : 
427
\		omode==2 && pass==2 ? tovfrdec2.TDecimate(5,hybrid=2,tfmin=creds ? "" : "tfm.txt",input="stats.txt",mkvout="timecodes.txt",tcfv1=false,batch=true,vidThresh=0.2) : nop()
428
429
topp = omode==1 ? finalcfr : omode==2 ? finalvfr : nop()
430
431
#Kill combing depending on user settings
432
dec = 	killcomb==0 ? topp : 
433
\	killcomb==1 ? topp.vinverse() : 
434
\	killcomb==2 ? topp.vinverseD() : 
435
\	killcomb==3 ? topp.daa() : 
436
\		interleave(	topp.		 subtitle("killcomb=0",align=9),
437
\				topp.vinverse(). subtitle("killcomb=1",align=9),
438
\				topp.vinverseD().subtitle("killcomb=2",align=9),
439
\				topp.daa().subtitle("killcomb=3",align=9))
440
441
aaed = 	aa==0 ? dec : 
442
\	aa==1 ? dec.ediaa() : 
443
\	aa==2 ? dec.daa() : 
444
\	aa==3 ? dec.maa() : 
445
\	aa==4 ? dec.sharpaamcmod(dark,thin,sharp,smooth,stabilize,tradius,aapel,aaov,aablk,aatype) : 
446
\			interleave(	dec.		subtitle("aa=0",align=9),
447
\					dec.ediaa().	subtitle("aa=1",align=9),
448
\					dec.daa().	subtitle("aa=2",align=9),
449
\					dec.maa().	subtitle("aa=3",align=9),
450
\					dec.sharpaamcmod(dark,thin,sharp,smooth,stabilize,tradius,aapel,aaov,aablk,aatype).subtitle("aa=4",align=9))
451
452
return omode==2 && pass==1 ? finalvfr : aaed
453
}
454
455
456
function chroma_rep(clip source, int "cache")
457
{
458
###### parameters & other necessary vars ######
459
    cache   = default(cache,-1)
460
461
462
###### source preparation & lut ######
463
global  out1    = source
464
global  out2    = mergechroma(source,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))
465
466
467
###### initialise variables ######
468
global  alfr     = -100
469
470
471-
scriptclip( source, """
471+
472
chrixout = scriptclip( source, """
473
474
 ### preparation ###
475
        cfr = current_frame
476
        ajmp = alfr+1==cfr
477
        
478
    global  alfr = cfr
479-
        al_v = LumaDifference(out1.trim(1,0),out1.trim(2,0))
479+
480-
        ac_v = ChromaUDifference(out1.trim(1,0),out1.trim(2,0)) + ChromaVDifference(out1.trim(1,0),out1.trim(2,0))
480+
        al_v = LumaDifference(out1,out1.trim(1,0))
481
        ac_v = ChromaUDifference(out1,out1.trim(1,0)) + ChromaVDifference(out1,out1.trim(1,0))
482
483-
	l12	= al_v
483+
484-
	l01	= ajmp ? l12 : al_v
484+
485
global 		l01	= ajmp ? l12 : al_v
486
global 		l12	= al_v
487
  
488-
	c12 = ac_v
488+
489-
	c01 = ajmp ? c12 : ac_v
489+
490
global 		c01 = ajmp ? c12 : ac_v
491
global 		c12 = ac_v
492
	
493
 ### condition ###
494
    change  = l12*3<l01 && c01*1.5<c12
495
    chango  = l01*3<l10 && c10*1.5<c01
496
    
497
    change ? out2 : chango ? out1.trim(1,0) : out1
498
499
""")
500
501
502
###### final decimation & caching ######
503
chrixout2 = cache<0 ? source.mergechroma(chrixout) : source.mergechroma(chrixout).RequestLinear(8, cache, 5, false, false)
504-
return last
504+
505
return chrixout2
506
}
507
508
509
#based on smartfade v0.2 by martino - Aimed at removing interlaced fades in anime. Uses luma difference between two fields as activation threshold.
510
#mod by thetoof : removed degrainmedian post-processing, 
511
#		changed how the fields are blended together (average of 2 nnedi interpolations + contra-sharpening to retain more detail) and 
512
#		added cache parameter to forward the setting of animeivtc to this function
513
#		renamed global parameter to something less general than "threshold"
514
515
function smartfademod(clip c, float "sfthr", bool "sfshow", int "cache") {
516
    
517
global sfthr	= default ( sfthr,  0.4 ) # Threshold for fade detection.
518
show		= default ( sfshow, false ) # Displays luma difference between fields without processing anything.
519
cache		= default ( cache,   10 ) # Cache the frames to help seeking and other functions requesting many frames
520
521
show ? scriptclip(c, "subtitle(string(abs(	  separatefields().selectodd().averageluma() \
522
            				- separatefields().selecteven().averageluma())))") : \
523
       scriptclip(c, "sep = separatefields() \
524
       avg = sep.selectodd().averageluma() - sep.selecteven().averageluma() \
525
       abs(avg) > sfthr ? interleave(sep.selecteven(),sep.selectodd()).weave().daa() : last")
526
527
cache<0 ? last : last.RequestLinear(8, cache, 5, false, false)
528
529
    return last
530
531
}
532
533
#Suggested by Mystery Keeper in "Denoise of tv-anime" thread
534
function ediaa(clip a) {return a.EEDI2(field=1).TurnRight().EEDI2(field=1).TurnLeft().spline36resize(a.width,a.height,-0.5,-0.5)}
535
536
#Anti-aliasing with contra-sharpening by Didée
537
function daa(clip c) {
538
nn	= c.nnedi2(field=-2)
539
dbl   	= mt_average(selecteven(nn),selectodd(nn),U=3,V=3)
540
dblD 	= mt_makediff(c,dbl,U=3,V=3)
541
shrpD 	= mt_makediff(dbl,dbl.removegrain((width(c)>1100) ? 20 : 11),U=3,V=3)
542
DD 	= shrpD.repair(dblD,13)
543
return dbl.mt_adddiff(DD,U=3,V=3) }
544
545
#Anti-aliasing with edge masking by martino, mask using "sobel" taken from Kintaro's useless filterscripts and modded by thetoof for spline36
546
function maa(clip input, int "mask") {
547
mask = input.mt_edge("sobel",7,7,5,5).mt_inflate()
548
aa_clip=input.spline36Resize(width(input)*2,height(input)*2).TurnLeft().SangNom().TurnRight().SangNom().spline36Resize(width(input),height(input)).MergeChroma(input) 
549
return mt_merge(input,aa_clip,mask) }
550
551
#Developped in the "fine anime antialiasing thread"
552
function SharpAAMCmod(clip orig, float "dark", int "thin", int "sharp", int "smooth", bool "stabilize", int "tradius", int "aapel", int "aaov", int "aablk", string "aatype")
553
{
554
dark	= default ( dark,     0.2 ) # strokes darkening strength
555
thin	= default ( thin,      10 ) # Presharpening
556
sharp	= default ( sharp,    150 ) # Postsharpening
557
smooth	= default ( smooth,    -1 ) # Postsmoothing
558
stabilize= default ( stabilize,false ) # Use post stabilization with Motion Compensation
559
Tradius	= default ( Tradius,    2 ) # 2 = MDegrain2 / 3 = MDegrain3 
560
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).)
561
aaov	= default ( aaov,       (orig.width>1100) ? 8 : 4 ) # block overlap value (horizontal). Must be even and less than block size. (Higher = more precise & slower)
562
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.
563
aatype	= default ( aatype, "Sangnom" ) # Use Sangnom() or EEDI2() for anti-aliasing
564
565
w=width(orig)
566
h=height(orig)
567
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)
568
 \       ,orig.DEdgeMask(0,255,0,255,"5 0 -5 10 0 -10 5 0 -5", divisor=4,Y=3,U=3,V=3)
569
 \       ,"max").mt_lut("x 128 / 0.86 ^ 255 *")
570
preaa=(thin==0 && dark==0) ? orig : (thin==0) ? orig.Toon(dark) : (dark==0) ? orig.awarpsharp2(depth=thin) : orig.Toon(dark).awarpsharp2(depth=thin)
571
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")
572
postsh=(sharp==0 && smooth==0) ? aa : aa.lsfmod(edgemode=1,strength=sharp,overshoot=1,soft=smooth)
573
merged=mt_merge(orig,postsh,m,Y=3,U=3,V=3)
574
575
sD=mt_makediff(orig,merged)
576
577
origsuper= orig.MSuper(pel=aapel)
578
sDsuper  = sD.  MSuper(pel=aapel, levels=1)
579
580
581
fv3 = tradius==3 ? origsuper.MAnalyse(isb=false,delta=3,overlap=aaov,blksize=aablk) : nop()
582
fv2 = tradius>=2 ? origsuper.MAnalyse(isb=false,delta=2,overlap=aaov,blksize=aablk) : nop()
583
fv1 = tradius>=1 ? origsuper.MAnalyse(isb=false,delta=1,overlap=aaov,blksize=aablk) : nop()
584
bv1 = tradius>=1 ? origsuper.MAnalyse(isb=true, delta=1,overlap=aaov,blksize=aablk) : nop()
585
bv2 = tradius>=2 ? origsuper.MAnalyse(isb=true, delta=2,overlap=aaov,blksize=aablk) : nop()
586
bv3 = tradius==3 ? origsuper.MAnalyse(isb=true, delta=3,overlap=aaov,blksize=aablk) : nop()
587
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)
588
589
reduc = 0.4
590
sDD = mt_lutxy(sD,sDD,"x 128 - abs y 128 - abs < x y ?").mergeluma(sDD,1.0-reduc)
591
592
return stabilize ? orig.mt_makediff(sDD,U=2,V=2) : merged
593
}
594
595
# Kill Combing Function from mcbob
596
function VinverseD(clip clp, float "sstr", int "amnt", int "uv")
597
{
598
uv   = default(uv,3)
599
sstr = default(sstr,2.7)
600
amnt = default(amnt,255)
601
uv2  = (uv==2) ? 1 : uv
602
STR  = string(sstr)
603
AMN  = string(amnt)
604
vblur  = clp.mt_convolution("1","50 99 50",U=uv,V=uv)
605
vblurD = mt_makediff(clp,vblur,U=uv2,V=uv2)
606
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)
607
VshrpD = mt_makediff(Vshrp,vblur,U=uv2,V=uv2)
608
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)
609
mt_adddiff(Vblur,VlimD,U=uv,V=uv)
610
(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) 
611
return(last)
612
}