View difference between Paste ID: RKKQESew and CRRTw661
SHOW: | | - or go back to the newest paste.
1
################################################################################################
2
###                                                                                          ###
3
###                           Simple MDegrain Mod - SMDegrain()                              ###
4
###                                                                                          ###
5
###                       Mod by Dogway - Original idea by Caroliano                         ###
6
###                                                                                          ###
7
###          Special Thanks: Sagekilla, Didée, cretindesalpes, Gavino and MVtools people     ###
8
###                                                                                          ###
9
###                       v3.1.2d (Dogway's mod) - 21 July 2015                              ###
10-
###    v3.1.2.94s mod for v3.1.2d to fix subpixel=3 and add Prefilter auto 16 (lsb) support  ###
10+
###    v3.1.2.95s mod for v3.1.2d to fix subpixel=3 and add Prefilter auto 16 (lsb) support  ###
11
###                     and yuy2 support in avs 2.6 with masktools 2.6                       ###
12
### and medianblur2 now will be used in Minblur instead of Quantile RemoveGrainHD in avs 2.6 ###
13
###                                    and add TV_range bool                                 ###
14
###               less lsb if not use lsb things and get ready for yv16 and yv24             ###
15
###              add dct, device_id and move all prefilters to external function             ###
16
###                                some cleanup and others                                   ###
17
###                        add MDegrain4 and MDegrain5 and MDegrain6                         ###
18
###                               add always_MDegrainN bool                                  ###
19
###                                  SMDegrain_KNLMeansCL                                    ###
20
###                 masktools 2.2.5 or up now required in avs26 and avs+                     ###
21
###                  now work basically with new high bit depth in avs+                      ###
22
###                                                                                          ###
23
################################################################################################
24
###
25
### General purpose simple degrain function. Pure temporal denoiser. Basically a wrapper(function)/frontend of mvtools2+mdegrain
26
### with some added common related options. Goal is accessibility and quality but not targeted to any specific kind of source.
27
### The reason behind is to keep it simple so aside masktools2 you will only need MVTools2.
28
###
29
### Check documentation for deep explanation on settings and defaults.
30
###
31
################################################################################################
32
33
function SMDegrain (clip input   , int "tr"      , int "thSAD"    , int  "thSADC" , bool "RefineMotion",  val  "contrasharp", clip "CClip"      , bool "interlaced" ,  int   "plane" ,  int   "Globals",                                  \
34
                     int "pel"   , int "subpixel", val "prefilter", clip "mfilter", int  "blksize"     , int  "overlap"     ,  int  "search"    , bool "truemotion" , int  "limit"   ,  int   "limitc" ,  int   "thSCD1" , int "thSCD2",  \
35-
                    bool "chroma", int "hpad"    , int "vpad"     , bool "lsb"    , bool "lsb_in"      ,  bool "lsb_out"    , int  "mode"       , bool  "slices"    , val  "Show"    ,  float "Str"    ,  float "Amp" , bool "TV_range", int "dct", int "device_id", int "searchparam", int "pelsearch", bool "trymany", bool "always_MDegrainN"){
35+
                    bool "chroma", int "hpad"    , int "vpad"     , bool "lsb"    , bool "lsb_in"      ,  bool "lsb_out"    , int  "mode"       , bool  "slices"    , val  "Show"    ,  float "Str"    ,  float "Amp" , bool "TV_range", int "dct", int "device_id", int "searchparam", int "pelsearch", bool "trymany", bool "always_MDegrainN", bool "n16", bool "n16_out"){
36
37
38
39
# Defaults & Conditionals
40
41
42
tr           = default( tr,       2 )
43
thSAD        = default( thSAD,  300 )
44
thSAD2=int(thSAD/2)
45
thSADC       = default( thSADC, thSAD2)
46
RefineMotion = default( RefineMotion, false)
47
alway_grainN = default( always_MDegrainN, false)
48
49
lsb_in       = default( lsb_in,  false)
50
lsb_out      = default( lsb_out, false)
51
lsb          = default( lsb    , lsb_in || lsb_out)
52
mode         = default( mode, 0 )
53
mode         = mode != 9 ? mode : -1
54
ampn         = mode != 9 ? 0.0  : 1.0
55
slices       = default(slices, true)
56
57
Globals      = default( Globals,  0 )
58
GlobalR = Globals == 1 
59
GlobalO = Globals == 3 
60
if1     = Defined(CClip)
61
62
Contrasharp  = default( Contrasharp, !GlobalO && if1)
63
Interlaced   = default( Interlaced,  false)
64
65
w       = input.width ()
66
h       = input.height()
67
sShow   = IsString(show)
68
preclip = IsClip(prefilter)
69
planar  = IsYUY2(input)
70
ifC     = IsBool(Contrasharp)
71
if0     = ifC ? (Contrasharp  ? true : false) : (Contrasharp > 0.0 ? true : false)
72
if2     = if0 && lsb && !GlobalO
73
avs26   = VersionNumber() < 2.60 ? false : true
74
ssispmt = Findstr(VersionString(), "AviSynth+") != 0 && Findstr(VersionString(), "r1576") == 0
75
SBitsn  = ssispmt ? input.BitsPerComponent() : 8
76
n16_out = default( n16_out,  false)
77
n16     = default( n16,  n16_out)
78
if3     = alway_grainN ? true : !avs26 ? tr > 3 : tr > 6
79
if4     = (w > 1099 ||  h > (lsb_in ? 1199 : 599))
80
if5     = interlaced ? (GetParity(input)                            ? true : false) : nop()
81
82
83
pel          = default( pel, if4 ? 1 : 2 )
84
subpixel     = default( subpixel,  2 )
85
prefilter    = default( prefilter, -1 )
86
MFilterB     = defined(mfilter)
87
pelclip      = pel>1 && subpixel == 3
88
Str          = default( Str, 1.0 )
89
Amp          = default( Amp, 0.0625 )
90
TV_range     = default( TV_range, !(Isrgb(input)))
91
92
blksize      = default( blksize,  if4 ? 16 : 8 )
93
blk2=blksize/2
94
overlap      = default( overlap,  blk2 )
95
ovl2=overlap/2
96
search       = default( search,   4 )
97
truemotion   = default( truemotion, !if4)
98
dct          = default( dct,   0 )
99
thSCD1       = default( thSCD1,  int(pow((blksize*2.5),2)))
100
thSCD2       = default( thSCD2,  130 )
101
102
103
plane        = default( plane,    4 )
104
chroma       = default( chroma, true)
105
Chr          = chroma ? 3 : 1
106
Chr2         = chroma ? 3 : preclip ? 1 : (prefilter==3 ? 2 : 1)
107
plane0       = plane!=0
108
U            = plane0 && plane!=2 ? 3 : 2
109
V            = plane0 && plane!=1 ? 3 : 2
110
Uin          = lsb_in ? 3 : u
111
Vin          = lsb_in ? 3 : v
112
113
114
hpad         = blksize
115
vpad         = blksize
116
limit        = default( limit,  255 )
117
limitc       = default( limitc,limit)
118
119
Show         = default( Show,  false)
120
121
122
123
124
# Error Report
125
126
                  Assert(blksize==4 || blksize==8 || blksize==16 || blksize==32,"MAnalyse: Block's size must be 4x4, 8x4, 8x8, 16x2, 16x8, 16x16, 32x16, 32x32")
127
                  Assert(overlap <= blk2,              "Overlap must be at least half blksize or less")
128
                  Assert(overlap == overlap-overlap%2, "MAnalyse: overlap must be an even figure")
129
                  Assert(IsBool(Show) || sShow,        "'Show' only accepts bool inputs (true,false), or string modes ('Speed','Memory',Quality')")
130
                  Assert(preclip || IsInt(prefilter),  "'prefilter' only accepts integers and clip inputs")
131
                  Assert(ifC     || IsInt(Contrasharp),"'Contrasharp' only accepts integers and bool inputs")
132
MFilterB        ? Assert(IsClip(mfilter),              "'mfilter' only accepts clip inputs")                        : nop()
133
interlaced      ? Assert(h%4==0,                       "Interlaced sources require mod 4 height sizes")             : nop()
134
lsb_in          ? Assert(!interlaced,                  "Interlaced 16 bit stacked clip is not a recognized format") : nop()
135
planar && if0   ? Assert(ifC,                          "LSFmod doesn't support YUY2 sources")                       : nop()
136
RefineMotion    ? Assert(blksize > 7,                  "For RefineMotion you need a blksize of at least 8")         : nop()
137
lsb_in          ? Assert(lsb,                          "lsb_in  requires: lsb=true")                                : nop()
138
lsb_out         ? Assert(lsb,                          "lsb_out requires: lsb=true")                                : nop()
139
n16_out         ? Assert(n16,                          "n16_out requires: n16=true")                                : nop()
140
141
142
143
# RefineMotion Variables
144
145
halfblksize  = RefineMotion ?  blk2                                     : nop() # MRecalculate works with half block size
146
halfoverlap  = RefineMotion ? (overlap == 2 ? overlap : ovl2+ovl2%2)    : nop() # Halve the overlap to suit the halved block size
147
halfthSAD    = RefineMotion || if3 ?     thSAD2                         : nop() # MRecalculate uses a more strict thSAD, which defaults to 150 (half of function's default of 300)
148
halfthSADC   =                 if3 ? int(thSADC/2)                      : nop() # For MDegrainN()
149
150
151
152
153
154
155
# Input preparation for: LSB_IN, Interlacing, Planar and MSuper optimization when pel=2
156
157
inputY  = planar      ? (lsb_in   ? Dither_YUY2toPlanar16(input)          : Interleaved2planar(input))                          : input
158
159
inputP  = !interlaced ? (pel == 2 ? inputY.AssumeFrameBased()             : inputY)                                             : \
160
                        (if5      ? inputY.AssumeTFF().SeparateFields()   : inputY.AssumeBFF().SeparateFields())
161
162
input8h = lsb_in      ?             inputP. Ditherpost(mode=6, slice=slices)                                                    : nop()
163
input8y = planar      ? (lsb_in   ? input8h.Dither_YUY2toInterleaved()    : inputP)                                             : inputP
164
input8  = lsb_in      ? (planar   ? input8y.Interleaved2planar()                                                                : \
165
                                    input8h)                                                                                    : input8y
166
167
168
# Prefilter & Motion Filter
169
170
Mfilter = MFilterB ? Mfilter : input8
171
172
bug_wa = interlaced && planar && chroma && !avs26 ? 2 : Chr # bug: crash prevention workaround
173
174
pref   = !GlobalR ? preclip ? prefilter                                                                                                                                   : \
175
                              input.SMDegrain_prefilters(inputP=inputP,input8=input8,input8y=input8y\
176
                                                                      ,inputY=inputY,input8h=input8h,prefilter=prefilter,chroma=chroma\
177
                                                                      ,Chr=Chr,Chr2=Chr2,bug_wa=bug_wa,lsb=lsb,lsb_in=lsb_in,Interlaced=Interlaced\
178
                                                                      ,if5=if5,pel=pel,device_type="GPU",device_id=device_id,a=1,d=1,h=7.0,slices=slices,planar=planar)   : \
179
                    input8
180
181
182
183
184
185
# Default Auto-Prefilter - Luma expansion TV->PC (up to 16% more values for motion estimation)
186
187
pref16 = (Height(pref)) == (Height(input8)*2) ? true : false
188
pref   = !GlobalR ? (!planar && lsb ? ((!preclip ? ((lsb_in && prefilter==-1) || prefilter==3 || prefilter==4 ) : false)          ? \
189
                     !TV_range ? pref.Ditherpost(mode=6,ampn=0.0,slice=slices,staticnoise=false,u=Chr2,v=Chr2)                                      : \
190
                     pref.Dither_Luma_Rebuild(S0=Str,c=Amp,slice=slices,lsb_in=true,uv=Chr2)                                      : \
191
                     !TV_range ? pref16 ? pref.Ditherpost(mode=6,ampn=0.0,slice=slices,staticnoise=false,u=Chr2,v=Chr2) : pref                                      : \
192
                     pref.Dither_Luma_Rebuild(S0=Str,c=Amp,slice=slices,lsb=true,lsb_in=pref16,uv=Chr2))                       : \
193
                                      ((!preclip ?  (lsb_in && prefilter==-1)                                   : false)                                 ? \
194
                     !TV_range ? !planar ? pref.Ditherpost(mode=6,slice=slices,u=Chr,v=Chr) : pref.Ditherpost(mode=6,slice=slices,u=Chr,v=Chr).Dither_YUY2toInterleaved().Interleaved2planar(!chroma)   : \
195
                                 !planar ? pref.Dither_Luma_Rebuild(S0=Str,c=Amp,uv=chr, lsb_in=true) : pref.Dither_Luma_Rebuild(S0=Str,c=Amp,uv=chr, lsb_in=true).Dither_YUY2toInterleaved().Interleaved2planar(!chroma) : \
196
                     !TV_range ? pref16 ? !planar ? pref.Ditherpost(mode=6,slice=slices,u=Chr,v=Chr) : pref.planar2Interleaved(!chroma).Dither_YUY2toPlanar16().Ditherpost(mode=6,slice=slices,u=chr,v=chr).Dither_YUY2toInterleaved().Interleaved2planar(!chroma) : pref                                  : \
197
                     pref.Dither_Luma_Rebuild(S0=Str,lsb=lsb,lsb_in=pref16,slice=slices,c=Amp,uv=bug_wa)))                                  : \
198
                     pref
199
200
pref = ssispmt ? n16 && sbitsn != pref.BitsPerComponent() ? pref.ConvertBits(sbitsn,dither=mode) : pref : pref
201
202
203
204
205
206
(sShow ? false : !Show) ? eval("""
207
208
209
# Subpixel 3
210
211
pclip  = pelclip              ?    pel==4 ? pref.nnedi3_rpow2(rfactor=2,nns=4).nnedi3_rpow2(rfactor=2,nns=4) :   pref.nnedi3_rpow2(rfactor=2,nns=4)  : nop()
212
pclip2 = pelclip && !GlobalR  ?  pel==4 ? input8.nnedi3_rpow2(rfactor=2,nns=4).nnedi3_rpow2(rfactor=2,nns=4) : input8.nnedi3_rpow2(rfactor=2,nns=4)  : nop()
213
214
215
216
217
# Motion vectors search
218
219
220
super_search =                             pelclip ? MSuper(pref,     pel=pel,               chroma=chroma, hpad=hpad, vpad=vpad,          pelclip=pclip,  rfilter=4, planar=planar) : \
221
                                                     MSuper(pref,     pel=pel,sharp=subpixel,chroma=chroma, hpad=hpad, vpad=vpad                        ,  rfilter=4, planar=planar)
222
super_render = !GlobalR ?                 (pelclip ? MSuper(input8,   pel=pel,               chroma=plane0, hpad=hpad, vpad=vpad,levels=1, pelclip=pclip2           , planar=planar) : \
223
                                                     MSuper(input8,   pel=pel,sharp=subpixel,chroma=plane0, hpad=hpad, vpad=vpad,levels=1                           , planar=planar)): super_search
224
Recalculate  = !GlobalR && RefineMotion ? (pelclip ? MSuper(pref,     pel=pel,               chroma=chroma, hpad=hpad, vpad=vpad,levels=1, pelclip=pclip            , planar=planar) : \
225
                                                     MSuper(pref,     pel=pel,sharp=subpixel,chroma=chroma, hpad=hpad, vpad=vpad,levels=1                           , planar=planar)): nop()
226
!if3  ? eval("
227
t2 = interlaced ? tr > 1 ? tr*2 : tr : tr
228
bv12 =   interlaced && (tr > 5)                  ? (!GlobalR  ? super_search.MAnalyse(isb = true, delta =12,  overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion, dct=dct,searchparam=searchparam,pelsearch=pelsearch, trymany=trymany) : bv12) : nop()
229
bv12 =   interlaced && (tr > 5) && RefineMotion &&  !GlobalR  ?   MRecalculate           (Recalculate,bv12,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion, dct=dct) :                                                              bv12
230
bv10 =   interlaced && (tr > 4)                  ? (!GlobalR  ? super_search.MAnalyse(isb = true, delta =10,  overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion, dct=dct,searchparam=searchparam,pelsearch=pelsearch, trymany=trymany) : bv10) : nop()
231
bv10 =   interlaced && (tr > 4) && RefineMotion &&  !GlobalR  ?   MRecalculate           (Recalculate,bv10,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion, dct=dct) :                                                              bv10
232
bv8  =   interlaced && (tr > 3)                  ? (!GlobalR  ? super_search.MAnalyse(isb = true, delta = 8,  overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion, dct=dct,searchparam=searchparam,pelsearch=pelsearch, trymany=trymany) : bv8)  : nop()
233
bv8  =   interlaced && (tr > 3) && RefineMotion &&  !GlobalR  ?   MRecalculate           (Recalculate, bv8,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion, dct=dct) :                                                              bv8
234
bv6  =                  t2 > 5                   ? (!GlobalR  ? super_search.MAnalyse(isb = true, delta = 6,  overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion, dct=dct,searchparam=searchparam,pelsearch=pelsearch, trymany=trymany) : bv6)  : nop()
235
bv6  =                  t2 > 5  && RefineMotion &&  !GlobalR  ?   MRecalculate           (Recalculate, bv6,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion, dct=dct) :                                                              bv6
236
bv5  =  !interlaced ? ((tr > 4)                  ? (!GlobalR  ? super_search.MAnalyse(isb = true, delta = 5,  overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion, dct=dct,searchparam=searchparam,pelsearch=pelsearch, trymany=trymany) : bv5)  : nop()) : nop()
237
bv5  =  !interlaced ? ((tr > 4  && RefineMotion &&  !GlobalR) ?   MRecalculate           (Recalculate, bv5,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion, dct=dct) :                                                              bv5)  : nop()
238
bv4  =                  t2 > 3                   ? (!GlobalR  ? super_search.MAnalyse(isb = true, delta = 4,  overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion, dct=dct,searchparam=searchparam,pelsearch=pelsearch, trymany=trymany) : bv4)  : nop()
239
bv4  =                  t2 > 3  && RefineMotion &&  !GlobalR  ?   MRecalculate           (Recalculate, bv4,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion, dct=dct) :                                                              bv4
240
bv3  =  !interlaced ? ((tr > 2)                  ? (!GlobalR  ? super_search.MAnalyse(isb = true, delta = 3,  overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion, dct=dct,searchparam=searchparam,pelsearch=pelsearch, trymany=trymany) : bv3)  : nop()) : nop()
241
bv3  =  !interlaced ? ((tr > 2  && RefineMotion &&  !GlobalR) ?   MRecalculate           (Recalculate, bv3,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion, dct=dct) :                                                              bv3)  : nop()
242
bv2  =  (interlaced ||  tr > 1)                  ? (!GlobalR  ? super_search.MAnalyse(isb = true, delta = 2,  overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion, dct=dct,searchparam=searchparam,pelsearch=pelsearch, trymany=trymany) : bv2)  : nop()
243
bv2  = ((interlaced ||  tr > 1) && RefineMotion &&  !GlobalR) ?   MRecalculate           (Recalculate, bv2,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion, dct=dct) :                                                              bv2
244
bv1  =  !interlaced ?                              (!GlobalR  ? super_search.MAnalyse(isb = true, delta = 1,  overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion, dct=dct,searchparam=searchparam,pelsearch=pelsearch, trymany=trymany) : bv1)  : nop()
245
bv1  =  !interlaced ?            ( RefineMotion &&  !GlobalR  ?   MRecalculate           (Recalculate, bv1,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion, dct=dct) :                                                              bv1)  : nop()
246
247
fv1  =  !interlaced ?                              (!GlobalR  ? super_search.MAnalyse(isb = false, delta = 1, overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion, dct=dct,searchparam=searchparam,pelsearch=pelsearch, trymany=trymany) : fv1)  : nop()
248
fv1  =  !interlaced ?            ( RefineMotion &&  !GlobalR  ?   MRecalculate           (Recalculate, fv1,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion, dct=dct) :                                                              fv1)  : nop()
249
fv2  =  (interlaced ||  tr > 1)                  ? (!GlobalR  ? super_search.MAnalyse(isb = false, delta = 2, overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion, dct=dct,searchparam=searchparam,pelsearch=pelsearch, trymany=trymany) : fv2)  : nop()
250
fv2  = ((interlaced ||  tr > 1) && RefineMotion &&  !GlobalR) ?   MRecalculate           (Recalculate, fv2,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion, dct=dct) :                                                              fv2
251
fv3  =  !interlaced ? ((tr > 2)                  ? (!GlobalR  ? super_search.MAnalyse(isb = false, delta = 3, overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion, dct=dct,searchparam=searchparam,pelsearch=pelsearch, trymany=trymany) : fv3)  : nop()) : nop()
252
fv3  =  !interlaced ? ((tr > 2  && RefineMotion &&  !GlobalR) ?   MRecalculate           (Recalculate, fv3,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion, dct=dct) :                                                              fv3)  : nop()
253
fv4  =                  t2 > 3                   ? (!GlobalR  ? super_search.MAnalyse(isb = false, delta = 4, overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion, dct=dct,searchparam=searchparam,pelsearch=pelsearch, trymany=trymany) : fv4)  : nop()
254
fv4  =                  t2 > 3  && RefineMotion &&  !GlobalR  ?   MRecalculate           (Recalculate, fv4,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion, dct=dct) :                                                              fv4
255
fv5  =  !interlaced ? ((tr > 4)                  ? (!GlobalR  ? super_search.MAnalyse(isb = false, delta = 5, overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion, dct=dct,searchparam=searchparam,pelsearch=pelsearch, trymany=trymany) : fv5)  : nop()) : nop()
256
fv5  =  !interlaced ? ((tr > 4  && RefineMotion &&  !GlobalR) ?   MRecalculate           (Recalculate, fv5,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion, dct=dct) :                                                              fv5)  : nop()
257
fv6  =                  t2 > 5                   ? (!GlobalR  ? super_search.MAnalyse(isb = false, delta = 6, overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion, dct=dct,searchparam=searchparam,pelsearch=pelsearch, trymany=trymany) : fv6)  : nop()
258
fv6  =                  t2 > 5  && RefineMotion &&  !GlobalR  ?   MRecalculate           (Recalculate, fv6,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion, dct=dct) :                                                              fv6
259
fv8  =   interlaced && (tr > 3)                  ? (!GlobalR  ? super_search.MAnalyse(isb = false, delta = 8, overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion, dct=dct,searchparam=searchparam,pelsearch=pelsearch, trymany=trymany) : fv8)  : nop()
260
fv8  =   interlaced && (tr > 3) && RefineMotion &&  !GlobalR  ?   MRecalculate           (Recalculate, fv8,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion, dct=dct) :                                                              fv8
261
fv10 =   interlaced && (tr > 4)                  ? (!GlobalR  ? super_search.MAnalyse(isb = false, delta =10, overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion, dct=dct,searchparam=searchparam,pelsearch=pelsearch, trymany=trymany) : fv10) : nop()
262
fv10 =   interlaced && (tr > 4) && RefineMotion &&  !GlobalR  ?   MRecalculate           (Recalculate,fv10,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion, dct=dct) :                                                              fv10
263
fv12 =   interlaced && (tr > 5)                  ? (!GlobalR  ? super_search.MAnalyse(isb = false, delta =12, overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion, dct=dct,searchparam=searchparam,pelsearch=pelsearch, trymany=trymany) : fv12) : nop()
264
fv12 =   interlaced && (tr > 5) && RefineMotion &&  !GlobalR  ?   MRecalculate           (Recalculate,fv12,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion, dct=dct) :                                                              fv12              
265
              ") : eval("
266
tr2    = tr*2
267
vmulti =                   !GlobalR ? super_search.MAnalyse( multi=true,overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma,truemotion=truemotion, delta = interlaced ? tr2 : tr, dct=dct,searchparam=searchparam,pelsearch=pelsearch, trymany=trymany) : vmulti
268
vmulti = RefineMotion   ? (!GlobalR ? MRecalculate (Recalculate, vmulti,overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma,truemotion=truemotion,    tr = interlaced ? tr2 : tr, dct=dct) :                                                    vmulti) : vmulti
269
vmulti =                   !GlobalR ? (interlaced ? vmulti.SelectEvery (4, 2, 3) : vmulti) : vmulti.SelectRangeEvery (Rtr*2, tr2, 0, false)
270
                         ")
271-
 \        ((lsb ?     ( if3  ? mfilter.MDegrainN(super_render, vmulti, tr, thSAD2=halfthSAD, thSADC2=halfthSADC,   thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=true) :
271+
272-
 \                 (tr == 6) ? mfilter.MDegrain6(super_render, bv2, fv2, bv4, fv4, bv6, fv6, bv8, fv8, bv10, fv10, bv12, fv12, thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=true) :
272+
273-
 \                 (tr == 5) ? mfilter.MDegrain5(super_render, bv2, fv2, bv4, fv4, bv6, fv6, bv8, fv8, bv10, fv10,             thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=true) :
273+
274-
 \                 (tr == 4) ? mfilter.MDegrain4(super_render, bv2, fv2, bv4, fv4, bv6, fv6, bv8, fv8,                         thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=true) :
274+
275-
 \                 (tr == 3) ? mfilter.MDegrain3(super_render, bv2, fv2, bv4, fv4, bv6, fv6,                                   thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=true) :
275+
276-
 \                 (tr == 2) ? mfilter.MDegrain2(super_render, bv2, fv2, bv4, fv4,                                             thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=true) :
276+
MDeg16 = n16 || lsb
277-
 \                             mfilter.MDegrain1(super_render, bv2, fv2,                                                       thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=true)):
277+
278-
 \                    ( if3  ? mfilter.MDegrainN(super_render, vmulti, tr, thSAD2=halfthSAD, thSADC2=halfthSADC,               thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=false):
278+
279
output  = !GlobalO ? (interlaced ?
280
 \        ((MDeg16 ?     ( if3  ? mfilter.MDegrainN(super_render, vmulti, tr, thSAD2=halfthSAD, thSADC2=halfthSADC,   thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=lsb,out16=n16) :
281
 \                 (tr == 6) ? mfilter.MDegrain6(super_render, bv2, fv2, bv4, fv4, bv6, fv6, bv8, fv8, bv10, fv10, bv12, fv12, thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=lsb,out16=n16) :
282
 \                 (tr == 5) ? mfilter.MDegrain5(super_render, bv2, fv2, bv4, fv4, bv6, fv6, bv8, fv8, bv10, fv10,             thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=lsb,out16=n16) :
283
 \                 (tr == 4) ? mfilter.MDegrain4(super_render, bv2, fv2, bv4, fv4, bv6, fv6, bv8, fv8,                         thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=lsb,out16=n16) :
284
 \                 (tr == 3) ? mfilter.MDegrain3(super_render, bv2, fv2, bv4, fv4, bv6, fv6,                                   thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=lsb,out16=n16) :
285-
 \         (lsb ?     ( if3  ? mfilter.MDegrainN(super_render, vmulti, tr, thSAD2=halfthSAD, thSADC2=halfthSADC,               thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=true) :
285+
 \                 (tr == 2) ? mfilter.MDegrain2(super_render, bv2, fv2, bv4, fv4,                                             thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=lsb,out16=n16) :
286-
 \                 (tr == 6) ? mfilter.MDegrain6(super_render, bv1, fv1, bv2, fv2, bv3, fv3, bv4, fv4, bv5, fv5, bv6, fv6,     thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=true) :
286+
 \                             mfilter.MDegrain1(super_render, bv2, fv2,                                                       thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=lsb,out16=n16)):
287-
 \                 (tr == 5) ? mfilter.MDegrain5(super_render, bv1, fv1, bv2, fv2, bv3, fv3, bv4, fv4, bv5, fv5,               thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=true) :
287+
 \                    ( if3  ? mfilter.MDegrainN(super_render, vmulti, tr, thSAD2=halfthSAD, thSADC2=halfthSADC,               thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar)          :
288-
 \                 (tr == 4) ? mfilter.MDegrain4(super_render, bv1, fv1, bv2, fv2, bv3, fv3, bv4, fv4,                         thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=true) :
288+
289-
 \                 (tr == 3) ? mfilter.MDegrain3(super_render, bv1, fv1, bv2, fv2, bv3, fv3,                                   thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=true) :
289+
290-
 \                 (tr == 2) ? mfilter.MDegrain2(super_render, bv1, fv1, bv2, fv2,                                             thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=true) :
290+
291-
 \                             mfilter.MDegrain1(super_render, bv1, fv1,                                                       thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=true)):
291+
292-
 \                    ( if3  ? mfilter.MDegrainN(super_render, vmulti, tr, thSAD2=halfthSAD, thSADC2=halfthSADC,               thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=false):
292+
293
 \                             mfilter.MDegrain1(super_render, bv2, fv2,                                                       thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar))))       :
294
 \         (MDeg16 ?     ( if3  ? mfilter.MDegrainN(super_render, vmulti, tr, thSAD2=halfthSAD, thSADC2=halfthSADC,               thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=lsb,out16=n16) :
295
 \                 (tr == 6) ? mfilter.MDegrain6(super_render, bv1, fv1, bv2, fv2, bv3, fv3, bv4, fv4, bv5, fv5, bv6, fv6,     thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=lsb,out16=n16) :
296
 \                 (tr == 5) ? mfilter.MDegrain5(super_render, bv1, fv1, bv2, fv2, bv3, fv3, bv4, fv4, bv5, fv5,               thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=lsb,out16=n16) :
297
 \                 (tr == 4) ? mfilter.MDegrain4(super_render, bv1, fv1, bv2, fv2, bv3, fv3, bv4, fv4,                         thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=lsb,out16=n16) :
298
 \                 (tr == 3) ? mfilter.MDegrain3(super_render, bv1, fv1, bv2, fv2, bv3, fv3,                                   thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=lsb,out16=n16) :
299
 \                 (tr == 2) ? mfilter.MDegrain2(super_render, bv1, fv1, bv2, fv2,                                             thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=lsb,out16=n16) :
300
 \                             mfilter.MDegrain1(super_render, bv1, fv1,                                                       thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=lsb,out16=n16)):
301
 \                    ( if3  ? mfilter.MDegrainN(super_render, vmulti, tr, thSAD2=halfthSAD, thSADC2=halfthSADC,               thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar)          :
302
 \                 (tr == 6) ? mfilter.MDegrain6(super_render, bv1, fv1, bv2, fv2, bv3, fv3, bv4, fv4, bv5, fv5, bv6, fv6,     thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar)          :
303
 \                 (tr == 5) ? mfilter.MDegrain5(super_render, bv1, fv1, bv2, fv2, bv3, fv3, bv4, fv4, bv5, fv5,               thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar)          :
304
 \                 (tr == 4) ? mfilter.MDegrain4(super_render, bv1, fv1, bv2, fv2, bv3, fv3, bv4, fv4,                         thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar)          :
305
 \                 (tr == 3) ? mfilter.MDegrain3(super_render, bv1, fv1, bv2, fv2, bv3, fv3,                                   thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar)          :
306
 \                 (tr == 2) ? mfilter.MDegrain2(super_render, bv1, fv1, bv2, fv2,                                             thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar)          :
307
 \                             mfilter.MDegrain1(super_render, bv1, fv1,                                                       thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar))))       :    nop()
308
309
310
# LSB_IN merging
311
312
type  = avs26 ? input.PixelType() : "YV12"
313
314
outputI= !GlobalO && planar && (lsb || ifC) ? output.Planar2Interleaved()                                                                                             : nop()
315
!GlobalO && lsb   ? eval("
316
blnkclp= lsb_in  || (if2 && !planar) ? BlankClip (lsb_in?input8h:input8, pixel_type=type, color_yuv=0)                                          : nop()
317
output = lsb && planar               ? Dither_YUY2toPlanar16(outputI)                                                                                                 : output
318
output = lsb_in                      ? inputY.Dither_add16 (Dither_sub16(output, planar?inputY:input8.Dither_convert_8_to_16 (), dif=true,u=u,v=v), dif=true,u=u,v=v) : output") : nop()
319
320
321
322
323
# Contrasharp (only sharpens luma)
324
325
ifC && if0 && if4 ? eval("
326
global Super           = super_render
327
global cb1             = !if3 ? (interlaced ? bv2 : bv1) : vmulti.SelectEvery (tr2, 0)
328
global cf1             = !if3 ? (interlaced ? fv2 : fv1) : vmulti.SelectEvery (tr2, 1)") : nop()
329
330
!GlobalO && if0   ? eval("
331
332
CCh   =       if1 ? CClip.height() : nop()
333
ref8  =       if1 ? (h == CCh*2)   : nop()
334
ref16 =       if1 ? (h == CCh/2)   : nop()
335
336
interlaced && if1 ? Assert(!ref16, "+Chr(34)+"Interlaced 16 bit stacked CClip is not a recognized format"+Chr(34)+")         : nop()
337
338
CClip = planar ?  (if1 ?  (lsb_in ? (ref8 ? CClip.Interleaved2planar(true) : CClip.Dither_YUY2toPlanar16().DitherPost(mode=-1,u=1,v=1).Dither_YUY2toInterleaved().Interleaved2planar(true))                         : \
339
                                     (interlaced ? (if5 ? CClip.AssumeTFF().SeparateFields().Interleaved2planar(true) : CClip.AssumeBFF().SeparateFields().Interleaved2planar(true))                                : \
340
                                     (ref16 ? CClip.Dither_YUY2toPlanar16().DitherPost(mode=-1,u=1,v=1).Dither_YUY2toInterleaved().Interleaved2planar(true) : CClip.Interleaved2planar(true))))                     : \
341
                          (lsb_in ?  inputY.DitherPost(mode=-1,u=1,v=1).Dither_YUY2toInterleaved().Interleaved2planar(true)                                                                                         : \
342
                                     inputP))                                                                                                                                                                       : \
343
                  (if1 ?  (lsb_in ? (ref8 ? CClip : CClip.DitherPost(mode=-1,u=1,v=1))                                                                                                                              : \
344
                                     (interlaced ? (if5 ? CClip.AssumeTFF().SeparateFields()                          : CClip.AssumeBFF().SeparateFields()) : (ref16 ? CClip.DitherPost(mode=-1,u=1,v=1) : CClip))) : \
345
                          (lsb_in ?  input.DitherPost(mode=-1,u=1,v=1)                                                                                                                                              : \
346
                                     inputP))")                                                                                                                                                                     : nop()
347
348
349
!GlobalO && if2   ? eval("
350
OutTO8  = output.DitherPost(mode=-1,u=1,v=1)
351
OutTO8P = planar  ? OutTO8.Dither_YUY2toInterleaved()                                                                                               : nop()
352
ctr16   = ifC     ? ContrasharpeningHD (planar ? OutTO8P.Interleaved2planar(true):OutTO8, CClip, planar=planar, HD=if4)                             : \
353
                    LSFmod(OutTO8,source=CClip,defaults="+Chr(34)+"slow"+Chr(34)+",strength=Contrasharp,soothe=false,Lmode=0,soft=-2,keep=0)
354
355
356
ctr16P  = planar  ? ctr16.Planar2Interleaved(true)  : ctr16
357
ctr16   = StackVertical (ctr16P, planar ? BlankClip  (ctr16P, pixel_type="+Chr(34)+"YUY2"+Chr(34)+", color_yuv=0,length=1) : blnkclp )
358
ctr16   = Dither_add16 (output,mt_makediff(!planar?ctr16P:ctr16P.Dither_YUY2toPlanar(), OutTO8,u=2,v=2).Dither_convert_8_to_16(), dif=true,u=2,v=2)
359
ctr16   = MergeChroma(ctr16, output)                                                                                                     ")         : nop()
360
361
362
363
# Globals Output
364
365
GlobalO || Globals == 2  ? (!if3 ? eval("
366
global Super           = super_render
367
global bv12            = bv12
368
global bv10            = bv10
369
global bv8             = bv8
370
global bv6             = bv6
371
global bv5             = bv5
372
global bv4             = bv4
373
global bv3             = bv3
374
global bv2             = bv2
375
global bv1             = bv1
376
377
global fv1             = fv1
378
global fv2             = fv2
379
global fv3             = fv3
380
global fv4             = fv4
381
global fv5             = fv5
382
global fv6             = fv6
383
global fv8             = fv8
384
global fv10            = fv10
385
global fv12            = fv12
386
") : avs26 ? eval("
387
global bv1             =                                             vmulti.SelectEvery (tr2,  0)
388
global bv2             = interlaced ? bv1                          : vmulti.SelectEvery (tr2,  2)
389
global bv3             =                                             vmulti.SelectEvery (tr2,  4)
390
global bv4             = interlaced ? vmulti.SelectEvery (tr2,  2) : vmulti.SelectEvery (tr2,  6)
391
global bv5             =                                             vmulti.SelectEvery (tr2,  8)
392
global bv6             = interlaced ? bv3                          : vmulti.SelectEvery (tr2, 10)
393
global bv8             = interlaced ? vmulti.SelectEvery (tr2,  6) : nop()
394
global bv10            = interlaced ? bv5                          : nop()
395
global bv12            = interlaced ? vmulti.SelectEvery (tr2, 10) : nop()
396
397
global fv1             =                                             vmulti.SelectEvery (tr2,  1)
398
global fv2             = interlaced ? fv1                          : vmulti.SelectEvery (tr2,  3)
399
global fv3             =                                             vmulti.SelectEvery (tr2,  5)
400
global fv4             = interlaced ? vmulti.SelectEvery (tr2,  3) : vmulti.SelectEvery (tr2,  7)
401
global fv5             =                                             vmulti.SelectEvery (tr2,  9)
402
global fv6             = interlaced ? fv3                          : vmulti.SelectEvery (tr2, 11)
403
global fv8             = interlaced ? vmulti.SelectEvery (tr2,  7) : nop()
404
global fv10            = interlaced ? fv5                          : nop()
405
global fv12            = interlaced ? vmulti.SelectEvery (tr2, 11) : nop()
406
global Super           = super_render
407
global vmulti          = vmulti
408
global Rtr             = tr
409
") : eval("
410
global bv1             =                                            vmulti.SelectEvery (tr2, 0)
411
global bv2             = interlaced ? bv1                         : vmulti.SelectEvery (tr2, 2)
412
global bv3             =                                            vmulti.SelectEvery (tr2, 4)
413
global bv4             = interlaced ? vmulti.SelectEvery (tr2, 2) : nop()
414
global bv6             = interlaced ? bv3                         : nop()
415
416
global fv1             =                                            vmulti.SelectEvery (tr2, 1)
417
global fv2             = interlaced ? fv1                         : vmulti.SelectEvery (tr2, 3)
418
global fv3             =                                            vmulti.SelectEvery (tr2, 5)
419
global fv4             = interlaced ? vmulti.SelectEvery (tr2, 3) : nop()
420-
!GlobalO ? (lsb_out ? (if0 ? ctr16 : output)  : \
420+
421-
                      (if0 ? (lsb ?  (interlaced ?  ctr16.weave().DitherPost(u=u,v=v,mode=6,slice=slices,interlaced=true)               :  ctr16.DitherPost(mode=mode,slice=slices,ampn=ampn,u=Uin,v=Vin))                                              : \
421+
422-
                                     (interlaced ? (ifC ? ContraSharpeningHD(output, CClip, planar=planar, HD=if4).weave()              : LSFmod(output,source=CClip,defaults="slow",strength=Contrasharp,soothe=false,Lmode=0,soft=-2,keep=0).weave()) : \
422+
423-
                                                   (ifC ? ContraSharpeningHD(output, CClip, planar=planar, HD=if4)                      : LSFmod(output,source=CClip,defaults="slow",strength=Contrasharp,soothe=false,Lmode=0,soft=-2,keep=0))))       : \
423+
424-
                             (lsb ?  (interlaced ? output.weave().DitherPost(u=u,v=v,mode=6,slice=slices,interlaced=true)               : output.DitherPost(mode=mode,slice=slices,ampn=ampn,u=Uin,v=Vin)) : (interlaced ? output.weave() : output))))    \
424+
425-
         :  input
425+
426
427
# Output
428
429
output = !GlobalO && !lsb && if0 ? ifC ? ContraSharpeningHD(output, CClip, planar=planar, HD=if4) : \
430
			               LSFmod(output,source=CClip,defaults="slow",strength=Contrasharp,soothe=false,Lmode=0,soft=-2,keep=0) \
431
			    : output
432
433
!GlobalO ? n16_out ? output : n16 ? output.ConvertBits(sbitsn,dither=mode) : \
434
	 	  lsb_out ? (if0 ? ctr16 : output)  : \
435
                      		 if0 && lsb ?  (interlaced ?  ctr16.weave().DitherPost(u=u,v=v,mode=6,slice=slices,interlaced=true)               :  ctr16.DitherPost(mode=mode,slice=slices,ampn=ampn,u=Uin,v=Vin)) : \
436
                             		 lsb ?             (interlaced ? output.weave().DitherPost(u=u,v=v,mode=6,slice=slices,interlaced=true)              : output.DitherPost(mode=mode,slice=slices,ampn=ampn,u=Uin,v=Vin)) : \
437
                                                                           (interlaced ? output.weave() : output)    \
438
	:  input
439
440
!GlobalO ? (planar ? (lsb ? Dither_YUY2toInterleaved() : (if0 ? MergeChroma(Planar2Interleaved(true), interlaced ? outputI.weave() : outputI) : Planar2Interleaved())) : last) : last """) : \
441
 \
442
 \
443
 \
444
 \
445
 \
446
eval("""
447
448
##############
449
# SHOW PANEL #
450
##############
451
452
453
  # Common Variables
454
455
h     = lsb_in ? h/2 : h
456
w2    = w*1.0
457
h2    = h*1.0
458
hi    = interlaced ? round(h/2.0) : h
459
AR    = w2/h2
460
lsp   = interlaced?15:30
461
lsp   = AR < 1.9 ? lsp : floor(AR < 1.9 ? lsp : ((AR>2.4) ? 0 : lsp-((AR-1.9)*(lsp/(2.4-1.9)))))
462
ys    = round(lsb_in ? h2/6 : (interlaced?h2/15:h2/6))
463
s     = floor(((interlaced?w2/1.6:w2)/720.0)*16.0)
464
type  = avs26 ? input.PixelType() : planar?"YUY2":"YV12"
465
466
Wadd  = w2/40
467
Wadd2 = Wadd/2
468
w1    = round(w2+Wadd+Wadd2)
469
w3    = (w2/3)+Wadd/2.1
470
w2    = round(w1+w3)
471
w3    = round(w1+(w3+Wadd2)*1.96)
472
w3sq  = string(round(w3*1.01))
473
w3s   = string(w3)
474
475
  # Panel Separator
476
477
blkclp = BlankClip(interlaced?assumeframebased(pref):pref,length=1,pixel_type=type,color=$161616)
478
         \.crop(0,interlaced?h/2%8:h%8,0,0).separatefields().assumeframebased().separatefields()
479
480
Interleave(selecteven(blkclp).addborders(2,0,0,0,color=$7f7f7f).crop(0,0,-2,0),selectodd(blkclp))
481
482
WOut=weave().assumefieldbased().weave()
483
sep=round((hi-height(WOut))/2.0)
484
485
StackHorizontal(planar? pref.Planar2Interleaved() : pref,WOut.addborders(0,sep-sep%2,0,hi-(height(WOut)+(sep-sep%2)),color=$161616))
486
487
488
489
  # Color Guide Squares
490
491
a=interlaced?h*0.37:h*0.74
492
b=interlaced?h*0.40:h*0.80
493
c=interlaced?h*0.43:h*0.86
494
495
!planar ? avs26 ? mt_merge(mt_lut(yexpr="127 scalef",uexpr="183 scalef",vexpr="47 scalef" ,u=3,v=3) ,mt_lutspa(mode="absolute", yexpr="x "+w3s+" >= x "+w3sq+" <= & y "+string(a)+" >= y "+string(interlaced?h*0.3835:h*0.767)+" <= & & 255 0 ?").trim(0,-1).FreezeFrame(0, FrameCount(pref)-1, 0),luma=true)
496
          \     : mt_merge(mt_lut(yexpr="127",uexpr="183",vexpr="47" ,u=3,v=3) ,mt_lutspa(mode="absolute", yexpr="x "+w3s+" >= x "+w3sq+" <= & y "+string(a)+" >= y "+string(interlaced?h*0.3835:h*0.767)+" <= & & 255 0 ?").trim(0,-1).FreezeFrame(0, FrameCount(pref)-1, 0),luma=true) : nop()
497
!planar ? avs26 ? mt_merge(mt_lut(yexpr="81 scalef" ,uexpr="91 scalef" ,vexpr="240 scalef",u=3,v=3) ,mt_lutspa(mode="absolute", yexpr="x "+w3s+" >= x "+w3sq+" <= & y "+string(b)+" >= y "+string(interlaced?h*0.4135:h*0.827)+" <= & & 255 0 ?").trim(0,-1).FreezeFrame(0, FrameCount(pref)-1, 0),luma=true)
498
          \     : mt_merge(mt_lut(yexpr="81" ,uexpr="91" ,vexpr="240",u=3,v=3) ,mt_lutspa(mode="absolute", yexpr="x "+w3s+" >= x "+w3sq+" <= & y "+string(b)+" >= y "+string(interlaced?h*0.4135:h*0.827)+" <= & & 255 0 ?").trim(0,-1).FreezeFrame(0, FrameCount(pref)-1, 0),luma=true) : nop()
499
!planar ? avs26 ? mt_merge(mt_lut(yexpr="210 scalef",uexpr="16 scalef" ,vexpr="146 scalef",u=3,v=3) ,mt_lutspa(mode="absolute", yexpr="x "+w3s+" >= x "+w3sq+" <= & y "+string(c)+" >= y "+string(interlaced?h*0.4435:h*0.887)+" <= & & 255 0 ?").trim(0,-1).FreezeFrame(0, FrameCount(pref)-1, 0),luma=true)
500
          \     : mt_merge(mt_lut(yexpr="210",uexpr="16" ,vexpr="146",u=3,v=3) ,mt_lutspa(mode="absolute", yexpr="x "+w3s+" >= x "+w3sq+" <= & y "+string(c)+" >= y "+string(interlaced?h*0.4435:h*0.887)+" <= & & 255 0 ?").trim(0,-1).FreezeFrame(0, FrameCount(pref)-1, 0),luma=true) : nop()
501
502
503
  # Parameters List
504
505
blue  = sShow ? (show=="Speed"   ? true : false) : false
506
red   = sShow ? (show=="Memory"  ? true : false) : false
507
yellow= sShow ? (show=="Quality" ? true : false) : false
508
509
Color = sShow ? (blue   ? $00aef0 : \
510
                 red    ? $ff0000 : \
511
                 yellow ? $ffff00 : \
512
                 Assert(false,    "Revise typo for possible color guide modes: 'Speed','Memory','Quality'")) : nop()
513
514
515
516
varn = "\n\n\n\n\n"
517
sp   = !GlobalR && str==1.0 ? "  " : ""
518
519
Subtitle("SMDegrain Mod v3.1.2",text_color=red||blue||yellow?Color:$7f7f7f,halo_color=$000000,font="VERDANA",size=s*2,x=w1,y=lsb_in?h/40:(interlaced?h/50:h/20))
520
521
Subtitle("BASIC"   ,lsp=lsp,text_color=$ffffff,font="COURIER NEW",size=s,x=w1,y=ys)
522
Subtitle(            "\n\ntr           = " + string(tr)                                                                              + \
523
         varn+varn+      "chroma       = " + string(chroma)                                                                          ,lsp=lsp,text_color=red||blue               ?Color:$ffffff,font="COURIER NEW",size=s,x=w1,y=ys)
524
Subtitle(          "\n\n\nthSAD(C)     = " + string( GlobalO?"bypass":string(thSAD)     + "(" + string(plane0?thSADC:"bypass") + ")")+ \
525
         varn+           "CClip        = " + string( GlobalO?"bypass":(defined(CClip)?"defined":"undefined"))                        + \
526
                     "\n\nInterlaced   = " + string(interlaced)                                                                      ,lsp=lsp,text_color=                               $ffffff,font="COURIER NEW",size=s,x=w1,y=ys)
527
Subtitle(varn+           "RefineMotion = " + string( GlobalO?"bypass":RefineMotion)                                                  ,lsp=lsp,text_color=yellow                  ?Color:$ffffff,font="COURIER NEW",size=s,x=w1,y=ys)
528
Subtitle(varn+       "\n\nContrasharp  = " + string( GlobalO?"bypass":contrasharp)                                                   ,lsp=lsp,text_color=yellow&&!GlobalO        ?Color:$ffffff,font="COURIER NEW",size=s,x=w1,y=ys)
529
Subtitle(varn+varn+    "\nplane        = " + string( GlobalO?"bypass":(plane==0?"0 (Luma)":plane==1?"1 (U plane)":plane==2?"2 (V plane)":plane==3?"3 (Chroma)":"4(Luma+Chroma)")),lsp=lsp,text_color=(red||blue)&&!GlobalO?Color:$ffffff,font="COURIER NEW",size=s,x=w1,y=ys)
530
Subtitle("<-- "+string(IsClip(prefilter)||prefilter!=-1?"Prefiltered clip converted\n    to PC levels":"Clip converted to PC levels\n   ")+" for the motion search",lsp=lsp,text_color=$ffffff,font="COURIER NEW",size=s,x=w1-10,y=(interlaced?h/2:h)-int(ys/1.5))
531
532
Subtitle("ADVANCED",lsp=lsp,text_color=$7f7f7f,font="COURIER NEW",size=s,x=w2,y=ys)
533
Subtitle(              "\n\npel        = " + string(pel)                                                                             ,lsp=lsp,text_color=red                     ?Color:$7f7f7f,font="COURIER NEW",size=s,x=w2,y=ys)
534
Subtitle(            "\n\n\nsubpixel   = " + string( pel==1 ?"bypass":subpixel)                                                      ,lsp=lsp,text_color=(yellow||blue)&&pel!=1  ?Color:$7f7f7f,font="COURIER NEW",size=s,x=w2,y=ys)
535
Subtitle(varn+varn+  "\n\n\ntruemotion = " + string( GlobalR?"bypass":truemotion)                                                    ,lsp=lsp,text_color=(yellow||blue)&&!GlobalR?Color:$7f7f7f,font="COURIER NEW",size=s,x=w2,y=ys)
536
Subtitle(varn+             "mfilter    = " + string( GlobalR?"bypass":MFilterB?"Clip"+sp+" Variable":"OFF")                          ,lsp=lsp,text_color=                               $7f7f7f,font="COURIER NEW",size=s,x=w2,y=ys)
537
Subtitle(varn+         "\n\nStr        = " + (GlobalR?string("bypass"):string(Str,"%1.2f"))+sp+" Brightness"                         + \
538
         varn+             "search     = " + string( GlobalR?"bypass":search)                                                        ,lsp=lsp,text_color=yellow&&!GlobalR        ?Color:$7f7f7f,font="COURIER NEW",size=s,x=w2,y=ys)
539
Subtitle(varn+varn+        "blksize    = " + string( GlobalR?"bypass":blksize)                                                       + \
540
                         "\noverlap    = " + string( GlobalR?"bypass":overlap)                                                       ,lsp=lsp,text_color=(red||blue)&&!GlobalR   ?Color:$7f7f7f,font="COURIER NEW",size=s,x=w2,y=ys)
541
Subtitle(varn+           "\nprefilter  = " + (GlobalR?string("bypass"):(preclip?"Clip"+sp+" Variable":(prefilter==0?"0"+sp+"    MinBlur(0)":prefilter==1?"1"+sp+"    MinBlur(1)":prefilter==2?"2"+sp+"    MinBlur(2)":prefilter==3?"3"+sp+"    Dfttest":prefilter==4?"4"+sp+"    KNLmeans":"OFF"+sp+"    (-1)"))) + \
542
                       "\n\nAmp        = " + (GlobalR||str==1.0?string("bypass"):string(Amp,"%1.2f"))+" Amplitude"                   + \
543
         varn+           "\nlimit(C)   = " + (GlobalO?string("bypass"):string(limit)     + "(" + string(limitc) + ")")               ,lsp=lsp,text_color=                               $7f7f7f,font="COURIER NEW",size=s,x=w2,y=ys)
544
545
Subtitle("OTHERS"  ,lsp=lsp,text_color=$494949,font="COURIER NEW",size=s,x=w3,y=ys)
546
Subtitle(             "\n\n\nthSCD1(2) = " + (GlobalO?string("bypass"):string(thSCD1) + "(" + string(thSCD2) + ")")                  + \
547
                   varn+"\n\nGlobals   = " + string(Globals==0 ? "0 (OFF)" : Globals==1 ? "1 (Import)" : Globals==2 ? "2 (Export)" : "3 (E.Only)"),lsp=lsp,text_color=                  $494949,font="COURIER NEW",size=s,x=w3,y=ys)
548
Subtitle(          varn+"\n\nslices    = " + string(slices)                                                                          ,lsp=lsp,text_color=blue                    ?Color:$494949,font="COURIER NEW",size=s,x=w3,y=ys)
549
Subtitle(               "\n\nhpad(v)   = " + string(hpad)   + "(" + string(vpad)   + ")"                                             ,lsp=lsp,text_color=red||blue               ?Color:$494949,font="COURIER NEW",size=s,x=w3,y=ys)
550
Subtitle(varn+              "lsb       = " + string(lsb)                                                                             ,lsp=lsp,text_color=yellow                  ?Color:$494949,font="COURIER NEW",size=s,x=w3,y=ys)
551
Subtitle(varn+            "\nlsb_in/out= " + string(lsb_in) +"/"+ string(lsb_out)                                                    ,lsp=lsp,text_color=red&&lsb_in             ?Color:$494949,font="COURIER NEW",size=s,x=w3,y=ys)
552
Subtitle(varn+        "\n\n\nmode      = " + (GlobalO||(lsb_out||!lsb)?string("bypass"):interlaced?"6 (locked)":string(mode))        ,lsp=lsp,text_color=yellow&&!GlobalO&&lsb   ?Color:$494949,font="COURIER NEW",size=s,x=w3,y=ys)
553
554
Subtitle("   Speed"   ,lsp=interlaced?15:30,text_color=planar?$00aef0:$7f7f7f,font="COURIER NEW",size=s,x=w3,y=int(a-1))
555
Subtitle("   Memory"  ,lsp=interlaced?17:34,text_color=planar?$ff0000:$7f7f7f,font="COURIER NEW",size=s,x=w3,y=int(b-1))
556
Subtitle("   Quality" ,lsp=interlaced?19:38,text_color=planar?$ffff00:$7f7f7f,font="COURIER NEW",size=s,x=w3,y=int(c-1))
557
558
lsb_out ? StackVertical (last, BlankClip (last, pixel_type=type, color_yuv=0,length=1)) : last""")}
559
560
561
562
563
564
########################################################
565
#                                                      #
566
# HELPER FUNCTIONS: For Contrasharp and YUY2 handling  #
567
#                                                      #
568
########################################################
569
570
########################################
571
## Didée's functions:
572
573
 # contra-sharpening: sharpen the denoised clip, but don't add more to any pixel than what was removed previously.
574
 # script function from Didée, at the VERY GRAINY thread
575
576
 # In final version 2.0d ContraHD() was merged, to allow proper HD sharpening.
577
 # In this case global variables of (before denoising) source MSuper and forward and backward
578
 # compensated motion vectors are necessary as: Super, cb1 and cf1, if used as a stand alone function.
579
 # Don't know who made (mod) it, so I can't give proper credits, sorry.
580
581
FUNCTION ContraSharpeningHD(clip denoised, clip original, bool "HD", bool "planar", int "overshoot"){
582
HD        = default(HD    ,false)
583
planar    = default(planar,false)
584
overshoot = default(overshoot,0)
585
avs26 = VersionNumber() < 2.60 ? false : true
586
yuy26 = avs26 && Isyuy2(denoised) && planar
587
588
HD ? eval("""
589
cb1=original.MCompensate(Super, cb1, planar=planar)
590
cf1=original.MCompensate(Super, cf1, planar=planar)
591
pmax = yuy26 ? original.Planar2Interleaved().ConvertToYV16().mt_logic(cb1.Planar2Interleaved().ConvertToYV16(), "max").mt_logic(cf1.Planar2Interleaved().ConvertToYV16(), "max") : original.mt_logic(cb1, "max").mt_logic(cf1, "max")
592
pmin = yuy26 ? original.Planar2Interleaved().ConvertToYV16().mt_logic(cb1.Planar2Interleaved().ConvertToYV16(), "min").mt_logic(cf1.Planar2Interleaved().ConvertToYV16(), "min") : original.mt_logic(cb1, "min").mt_logic(cf1, "min")""") : nop()
593
594
s    = denoised.MinBlur(HD?2:1,1,planar=planar)                                   # Damp down remaining spots of the denoised clip.
595
s    = yuy26 ? s.Planar2Interleaved().ConvertToYV16() : s
596
original = yuy26 ? original.Planar2Interleaved().ConvertToYV16() : original
597
denoised = yuy26 ? denoised.Planar2Interleaved().ConvertToYV16() : denoised
598
allD = mt_makediff(original,denoised)                                             # The difference achieved by the denoising.
599
ssD  = mt_makediff(s,HD?s.removegrain(20,-1,planar=planar).\
600
                          removegrain(20,-1,planar=planar):\
601
                        s.removegrain(11,-1,planar=planar))                       # The difference of a simple kernel blur.
602
allD = yuy26 ? allD.ConvertToYUY2().Interleaved2planar(true) : allD
603
ssD  = yuy26 ? ssD.ConvertToYUY2().Interleaved2planar(true) : ssD
604
ssDD = ssD.repair(HD?ssD.repair(allD,1,planar=planar):allD,HD?12:1,planar=planar) # Limit the difference to the max of what the denoising removed locally.
605
ssDD = yuy26 ? ssDD.Planar2Interleaved().ConvertToYV16() : ssDD
606
ssD  = yuy26 ? ssD.Planar2Interleaved().ConvertToYV16() : ssD
607
ssDD = avs26 ? SSDD.mt_lutxy(ssD,"x range_half - abs y range_half - abs < x y ?")
608
       \     : SSDD.mt_lutxy(ssD,"x 128 - abs y 128 - abs < x y ?")                       # abs(diff) after limiting may not be bigger than before.
609
610
denoised.mt_adddiff(ssDD,U=2,V=2)                                                 # Apply the limited difference. (Sharpening is just inverse blurring)
611
HD ? mt_clamp(last,pmax,pmin,overshoot,overshoot,chroma="copy first") : last
612
yuy26 ? last.ConvertToYUY2().Interleaved2planar() : last
613
}
614
615
 # MinBlur   by Didée
616
 # Nifty Gauss/Median combination (Modified for Planar input)
617
618
function MinBlur(clip clp, int "r", int "uv", bool "planar", string "mode", int "abtype", bool "blurrep"){
619
620
r      = default(r,1)
621
uv     = default(uv,3)
622
planar = default(planar,false)
623
defmod = defined(mode)
624
mode   = default(mode,"Median_Both")
625
avs26 = VersionNumber() < 2.60 ? false : true
626
yuy26 = avs26 && Isyuy2(clp) && planar
627
628
uv2    = (uv==2) ? 1  : uv
629
rg4    = (uv==3) ? 4  : -1
630
rg11   = (uv==3) ? 11 : -1
631
rg20   = (uv==3) ? 20 : -1
632
uvm2   = r==2 ? (uv==3?2:uv==2?0:-1) : nop()
633
uvm3   = r==3 ? (uv==3?3:uv==2?0:-1) : nop()
634
medf   = (uv==3) ? 1 : -200
635
rg4v   = !(defmod && r==1) && (r<=1)
636
637
blurrep= default(blurrep,false)
638
639
blurstr= mode == "Median_Both" ? "medianblur(r,r*medf,r*medf)" : mode == "average_Both" ? "aBlur(r, abtype, chroma=uv)" : mode == "average_horizontal" ? "aBlur(r, abtype, blurv="+String(r==1 ? 0 : 1)+", chroma=uv)" : mode == "average_vertical" ? "aBlur("+String(r==1 ? 0 : 1)+", abtype, blurv=r, chroma=uv)" : mode
640
641
RG11D = (r==0) ? yuy26 ? mt_makediff(clp.Planar2Interleaved(uv2==1).ConvertToYV16(),clp.sbr(planar=planar).Planar2Interleaved(uv2==1).ConvertToYV16(),U=uv2,V=uv2).ConvertToYUY2().Interleaved2planar(uv2==1) : mt_makediff(clp,clp.sbr(planar=planar),U=uv2,V=uv2)
642
 \    : (r==1) ? yuy26 ? mt_makediff(clp.Planar2Interleaved(uv2==1).ConvertToYV16(),clp.removegrain(11,rg11,planar=planar).Planar2Interleaved(uv2==1).ConvertToYV16(),U=uv2,V=uv2).ConvertToYUY2().Interleaved2planar(uv2==1) : mt_makediff(clp,clp.removegrain(11,rg11,planar=planar),U=uv2,V=uv2)
643
 \    : (r==2) ? yuy26 ? mt_makediff(clp.Planar2Interleaved(uv2==1).ConvertToYV16(),clp.removegrain(11,rg11,planar=planar).removegrain(20,rg20,planar=planar).Planar2Interleaved(uv2==1).ConvertToYV16(),U=uv2,V=uv2).ConvertToYUY2().Interleaved2planar(uv2==1) : mt_makediff(clp,clp.removegrain(11,rg11,planar=planar).removegrain(20,rg20,planar=planar),U=uv2,V=uv2)
644
 \    :          yuy26 ? mt_makediff(clp.Planar2Interleaved(uv2==1).ConvertToYV16(),clp.removegrain(11,rg11,planar=planar).removegrain(20,rg20,planar=planar).removegrain(20,rg20,planar=planar).Planar2Interleaved(uv2==1).ConvertToYV16(),U=uv2,V=uv2).ConvertToYUY2().Interleaved2planar(uv2==1) : mt_makediff(clp,clp.removegrain(11,rg11,planar=planar).removegrain(20,rg20,planar=planar).removegrain(20,rg20,planar=planar),U=uv2,V=uv2)
645
RG4D  = (rg4v) ? yuy26 ? mt_makediff(clp.Planar2Interleaved(uv2==1).ConvertToYV16(),clp.removegrain(4,rg4,planar=planar).Planar2Interleaved(uv2==1).ConvertToYV16(),U=uv2,V=uv2).ConvertToYUY2().Interleaved2planar(uv2==1) : mt_makediff(clp,clp.removegrain(4,rg4,planar=planar),U=uv2,V=uv2)
646
 \    : (r==2) ? avs26 ? yuy26 ? eval("mt_makediff(clp.Planar2Interleaved(uv2==1).ConvertToYV16(),clp.Planar2Interleaved(uv2==1).ConvertToYV16()."+blurstr+",U=uv2,V=uv2).ConvertToYUY2().Interleaved2planar(uv2==1)") : eval("mt_makediff(clp,clp."+blurstr+",U=uv2,V=uv2)") : mt_makediff(clp,clp.Quantile(radius_y=2, radius_u=uvm2, radius_v=uvm2, planar=planar), U=uv2,V=uv2)
647
 \    :          avs26 ? yuy26 ? eval("mt_makediff(clp.Planar2Interleaved(uv2==1).ConvertToYV16(),clp.Planar2Interleaved(uv2==1).ConvertToYV16()."+blurstr+",U=uv2,V=uv2).ConvertToYUY2().Interleaved2planar(uv2==1)") : eval("mt_makediff(clp,clp."+blurstr+",U=uv2,V=uv2)") : mt_makediff(clp,clp.Quantile(radius_y=3, radius_u=uvm3, radius_v=uvm3, planar=planar), U=uv2,V=uv2)
648
#RG4D  = blurrep ? RG4D.Repair(clp.removegrain(3,uv==3 ? 0 : -1,planar=planar),9,uv==3 ? 0 : -1,planar=planar) : RG4D
649
DD    = yuy26 ? mt_lutxy(RG11D.Planar2Interleaved(uv2==1).ConvertToYV16(),RG4D.Planar2Interleaved(uv2==1).ConvertToYV16(),"x 128 - y 128 - * 0 < 128 x 128 - abs y 128 - abs < x y ? ?",U=uv2,V=uv2).ConvertToYUY2().Interleaved2planar(uv==1)
650
    \ : avs26 ? mt_lutxy(RG11D,RG4D,"x range_half - y range_half - * 0 < range_half x range_half - abs y range_half - abs < x y ? ?",U=uv2,V=uv2)
651
    \         : mt_lutxy(RG11D,RG4D,"x 128 - y 128 - * 0 < 128 x 128 - abs y 128 - abs < x y ? ?",U=uv2,V=uv2)
652
yuy26 ? clp.Planar2Interleaved(uv==1).ConvertToYV16().mt_makediff(DD.Planar2Interleaved(uv==1).ConvertToYV16(),U=uv,V=uv).ConvertToYUY2().Interleaved2planar(uv==1) : clp.mt_makediff(DD,U=uv,V=uv)
653
blurrep ? Repair(clp.removegrain(17,uv==3 ? 0 : -1,planar=planar),9,uv==3 ? 0 : -1,planar=planar) : last
654
 }
655
656
657
 # helper function: make a highpass on a blur's difference (well, kind of that)
658
 # Highpass of spatial r=1 Gaussian (like blur(1.0)) (Modified for Planar input)
659
660
function sbr(clip c, bool "planar") {
661
662
planar = default(planar,false)
663
avs26 = VersionNumber() < 2.60 ? false : true
664
yuy26 = avs26 && Isyuy2(c) && planar
665
666
rg11   = c.removegrain(11,-1,planar=planar)
667
rg11D  = yuy26 ? mt_makediff(c.Planar2Interleaved(true).ConvertToYV16(),rg11.Planar2Interleaved(true).ConvertToYV16()).ConvertToYUY2().Interleaved2planar(true) : mt_makediff(c,rg11)
668
rg11DD = yuy26 ? mt_makediff(rg11D.Planar2Interleaved(true).ConvertToYV16(),rg11D.removegrain(11,-1,planar=planar).Planar2Interleaved(true).ConvertToYV16()).mt_lutxy(rg11D.Planar2Interleaved(true).ConvertToYV16(),"x 128 - y 128 - * 0 < 128 x 128 - abs y 128 - abs < x y ? ?").ConvertToYUY2().Interleaved2planar(true)
669
     \ : avs26 ? mt_makediff(rg11D,rg11D.removegrain(11,-1,planar=planar)).mt_lutxy(rg11D,"x range_half - y range_half - * 0 < range_half x range_half - abs y range_half - abs < x y ? ?")
670
     \         : mt_makediff(rg11D,rg11D.removegrain(11,-1,planar=planar)).mt_lutxy(rg11D,"x 128 - y 128 - * 0 < 128 x 128 - abs y 128 - abs < x y ? ?")
671
yuy26 ? c.Planar2Interleaved().ConvertToYV16().mt_makediff(rg11DD.Planar2Interleaved().ConvertToYV16(),U=2,V=2).ConvertToYUY2().Interleaved2planar() : c.mt_makediff(rg11DD,U=2,V=2) }
672
673
########################################
674
## cretindesalpes' functions
675
676
# Converts 16 bit interleaved YUY2 to 16bit planar YUY2 suitable for Ditherpost. (Use right before Ditherpost; this format is not suitable for filtering)
677
678
function Dither_YUY2toPlanar16(c){c
679
yuy26   = !(VersionNumber() < 2.60) && isyuy2(c)
680
my = !yuy26 ? Dither_get_msb().ConvertToYV12() : nop()
681
ly = !yuy26 ? Dither_get_lsb().ConvertToYV12() : nop()
682
u = !yuy26 ? UToY ().ConvertToYV12 () : nop()
683
v = !yuy26 ? VToY ().ConvertToYV12 () : nop()
684
yuy26 ? ConvertToYV16() : YToUV (u, v, StackVertical (StackVertical (my, my), StackVertical (ly, ly)))}
685
686
687
# Converts 8 bit interleaved YUY2 to 8 bit planar YUY2 ready for either 8 or 16 bit processing also suitable for Ditherpost
688
689
function Dither_YUY2toPlanar(c){c
690
yuy26   = !(VersionNumber() < 2.60) && isyuy2(c)
691
y = !yuy26 ? ConvertToYV12 () : nop()
692
u = !yuy26 ? UToY ().ConvertToYV12 () : nop()
693
v = !yuy26 ? VToY ().ConvertToYV12 () : nop()
694
yuy26 ? ConvertToYV16() : YToUV (u, v, StackVertical (y, y))}
695
696
697
# Converts back the above special planar formats to normal interleaved 16 bit YUY2
698
699
function Dither_YUY2toInterleaved16(c){c
700
yuy26   = !(VersionNumber() < 2.60)
701
y = !yuy26 ? StackVertical (Crop (0, 0, 0, (Height () /(4))), Crop (0, (Height () /(2))+(Height () /(4)), 0, 0)).ConvertToYUY2 () : nop()
702
u = !yuy26 ? UToY ().ConvertToYUY2 () : nop()
703
v = !yuy26 ? VToY ().ConvertToYUY2 () : nop()
704
yuy26 ? ConvertToYUY2() : YToUV (u, v, y)}
705
706
# Converts back the above special planar formats to normal interleaved YUY2
707
708
function Dither_YUY2toInterleaved(c){c
709
yuy26   = !(VersionNumber() < 2.60)
710
y = !yuy26 ? Crop (0, 0, 0, Height () /(2)).ConvertToYUY2 () : nop()
711
u = !yuy26 ? UToY ().ConvertToYUY2 () : nop()
712
v = !yuy26 ? VToY ().ConvertToYUY2 () : nop()
713
yuy26 ? ConvertToYUY2() : YToUV (u, v, y)}
714
715
716
717
# Converts luma (and chroma) to PC levels, and optionally allows tweaking for pumping up the darks. (for the clip to be fed to motion search only)
718
# By courtesy of cretindesalpes.
719
720
function Dither_Luma_Rebuild (clip src, float "s0", float "c",int "uv", bool "lsb", bool "lsb_in", bool "lsb_out", int "mode", float "ampn", bool "slice"){
721
lsb_in  = Default( lsb_in  ,false)
722
lsb_out = Default( lsb_out ,false)
723
lsb     = Default( lsb     ,lsb_in || lsb_out)
724
lsb     = !lsb && (lsb_in || lsb_out) ? true : lsb
725
mode    = Default( mode ,6)
726
uv      = Default(uv,    3)
727
s0      = Default(s0,  2.0)
728
c       = Default(c,  1.0/16)
729
ampn    = Default(ampn, 0.0)
730
slice   = Default(slice, true)
731
yuy2    = isyuy2(src)
732
avs26   = VersionNumber() < 2.60 ? false : true
733
ssispmt = Findstr(VersionString(), "AviSynth+") != 0 && Findstr(VersionString(), "r1576") == 0
734-
lsb ? (lsb_in ? Dither_lut16  (yexpr=e,expr="x 32768 - 32768 * 28672 / 32768 +",y=3, u=uv, v=uv)                 : \
734+
735-
                Dither_lut8   (yexpr=e,expr="x 128 - 32768 * 112 / 32768 +"    ,y=3, u=uv, v=uv))                : \
735+
736
ssispmt ? Assert(!(src.isYUVA() && lsb), "lsb hack is not Compatible with YUVA" ) : nop()
737
Assert(!(src.isrgb()), "Dither_Luma_Rebuild: RGB Color formats is not supported" )
738
src     = yuy2 ? lsb_in ? src.Planar2Interleaved(uv==1).Dither_YUY2toPlanar16() : avs26 ? src.Planar2Interleaved(uv==1).ConvertToYV16() : lsb ? src.Planar2Interleaved(uv==1).Dither_YUY2toPlanar() : src : src
739
740
    k = (s0 - 1) * c
741
    t = lsb_in ? "x 4096 - 56064 / 0 1 clip"
742
     \ : avs26 ? "x 16 scalef - 219 scalef / 0 1 clip"
743
             \ : "x 16 - 219 / 0 1 clip"
744
    e = String(k)+" "+String(1+c)+" "+String((1+c)*c)+" "+t+" "+String(c)
745
\       +" + / - * "+t+" 1 "+String(k)+" - * + "+String(lsb?65536:avs26?"range_size":255)+" *"
746
src
747
lsb ? (lsb_in ? Dither_lut16  (yexpr=ReplaceStr(e,".",","),expr="x 32768 - 32768 * 28672 / 32768 +",y=3, u=uv, v=uv)                 : \
748
                Dither_lut8   (yexpr=ReplaceStr(e,".",","),expr="x 128 - 32768 * 112 / 32768 +"    ,y=3, u=uv, v=uv))                : \
749
                avs26 ? mt_lut(yexpr=e,expr="x range_half - range_half * 112 scalef / range_half +",y=3, u=uv, v=uv) : \
750
                        mt_lut(yexpr=e,expr="x 128 - 128 * 112 / 128 +"        ,y=3, u=uv, v=uv)
751
752
lsb_out && !yuy2 ? last : (lsb && !lsb_out ? yuy2 ? Ditherpost(mode=mode,ampn=ampn,slice=slice,staticnoise=ampn!=0.0?true:false,u=uv,v=uv).Dither_YUY2toInterleaved().Interleaved2planar(uv==1) : Ditherpost(mode=mode,ampn=ampn,slice=slice,staticnoise=ampn!=0.0?true:false,u=uv,v=uv) : last)
753
return yuy2 && lsb_out ? last.Dither_YUY2toInterleaved16().Interleaved2planar(uv==1) : yuy2 && avs26 && !lsb ? converttoyuy2().Interleaved2planar(uv==1) : last
754
}
755
756
757
758
# SMDegrain prefilters
759
760
function SMDegrain_prefilters (clip input, int "prefilter", bool "chroma", int "Chr", int "Chr2", int "bug_wa", bool "lsb", bool "lsb_in", bool "Interlaced", val "if5", int "pel", String "device_type", int "device_id", int "d", int "a", bool "slices", bool "planar", clip "inputP", clip "input8", clip "input8y", clip "inputY", val "input8h", float "h", String "knlm_params")
761
{
762
Interlaced   = Default( Interlaced  ,false)
763
slices       = default(slices, true)
764
if5          = Default( if5  ,interlaced ? (GetParity(input)                            ? true : false) : nop())
765
lsb_in       = Default( lsb_in  ,false)
766
lsb          = Default( lsb     ,lsb_in)
767
pel          = default( pel, (input.width () > 1099 ||  input.height() > (lsb_in ? 1199 : 599)) ? 1 : 2 )
768
ssispmt      = Findstr(VersionString(), "AviSynth+") != 0 && Findstr(VersionString(), "r1576") == 0
769
chroma       = default( chroma, true)
770
planar       = Default( planar , input.isyuy2())
771
prefilter    = Default( prefilter, 3)
772
773
Chr          = Default(Chr,     chroma ? 3 : 1)
774
Chr2         = Default(Chr2,    chroma ? 3 : (prefilter==3 ? 2 : 1))
775
avs26        = !(VersionNumber() < 2.60)
776
777
Assert(!(!defined(inputP) && prefilter==-1), "prefilter must be between 0~4: "+string(prefilter))
778
lsb_native = ssispmt ? !(Input.BitsPerComponent() > 8 && (lsb)) : true
779
ssispmt ? Assert(lsb_native, "lsb hack is not Compatible with native high bit depth" ) : nop()
780
781
# Input preparation for: LSB_IN, Interlacing, Planar and MSuper optimization when pel=2
782
783
inputY  = defined(inputY )                    ? inputY  : planar      ? (lsb_in   ? Dither_YUY2toPlanar16(input)         : Interleaved2planar(input))                           : input
784
785
inputP  = defined(inputP )                    ? inputP  : !interlaced ? (pel == 2 ? inputY.AssumeFrameBased()            : inputY)                                             : \
786
                                                                        (if5      ? inputY.AssumeTFF().SeparateFields()  : inputY.AssumeBFF().SeparateFields())
787
788
input8h = defined(input8h) && isclip(input8h) ? input8h : lsb_in      ?             inputP. Ditherpost(mode=6, slice=slices)                                                    : nop()
789
input8y = defined(input8y)                    ? input8y : planar      ? (lsb_in   ? input8h.Dither_YUY2toInterleaved()   :  inputP)                                             : inputP
790
input8  = defined(input8 )                    ? input8  : lsb_in      ? (planar   ? input8y.Interleaved2planar()         : input8h)                                             : input8y
791
792
inputd  = (prefilter == 3 || prefilter == 4)  ? Interlaced ? if5 ? input.AssumeTFF().SeparateFields() : \
793
                                                                   input.AssumeBFF().SeparateFields() : \
794
                                                             input : nop()
795
796
bug_wa    = defined(bug_wa) ? bug_wa : interlaced && planar && chroma && !avs26 ? 2 : Chr # bug: crash prevention workaround
797
798
# The mt_merge() line for prefilter=3 should be swapped with a high bitdepth variant (Dither_merge16_8() ?) due to a 1 point limited range
799
# in both range ends, but then it won't work with planar sources. This isn't as critical since we are only trying to find motion vectors.
800
                     (prefilter==-1) ?  inputP                                                                                                                                      : \
801
                     (prefilter==0)  ?  input8.MinBlur(0,Chr,planar)                                               : \
802
                     (prefilter==1)  ?  input8.MinBlur(1,Chr,planar)                                               : \
803
                     (prefilter==2)  ?  input8.MinBlur(2,Chr,planar)                                               : \
804
                     (prefilter==3)  ?  (!planar && lsb ? Dither_merge16_8( inputd.Dfttest(sstring="0.0:4.0 0.2:9.0 1.0:15.0",tbsize=1,u=chroma,v=chroma,lsb=true,lsb_in=lsb_in,quiet=true), lsb_in?inputP:inputP.Dither_convert_8_to_16(),                                \
805
                                                                   lsb_in?inputP.Dither_lut16("x 4096 < 65535 x 19200 > 0 65535 x 4096 - 4.338916843220339 * - ? ?",u=1,v=1).Ditherpost(mode=6, slice=slices, u=Chr,   v=Chr)                                           \
806
                                                                         :inputd.mt_lut(      "x 16 < 255 x 75 > 0 255 x 16 - 255 75 16 - / * - ? ?",u=1,v=1), luma=chroma,                       u=Chr2,  v=Chr2)                                                       : \
807
                                 avs26 && planar && lsb ? Dither_merge16_8( inputd.Dfttest(sstring="0.0:4.0 0.2:9.0 1.0:15.0",tbsize=1,u=chroma,v=chroma,lsb=true,lsb_in=lsb_in,quiet=true).ConvertToYV16(), lsb_in?inputP:inputd.ConvertToYV16().Dither_convert_8_to_16(),                                \
808
                                                                   lsb_in?inputP.Dither_lut16("x 4096 < 65535 x 19200 > 0 65535 x 4096 - 4.338916843220339 * - ? ?",u=1,v=1).Ditherpost(mode=6, slice=slices, u=Chr,   v=Chr)                                           \
809
                                                                         :inputd.ConvertToYV16().mt_lut(      "x 16 < 255 x 75 > 0 255 x 16 - 255 75 16 - / * - ? ?",u=1,v=1), luma=chroma,                       u=Chr2,  v=Chr2).ConvertToYUY2().Interleaved2planar(!chroma)                                                       : \
810
                                           avs26 && planar ? mt_merge     (        Dfttest(!lsb_in?inputd:input8y,sstring="0.0:4.0 0.2:9.0 1.0:15.0",tbsize=1,u=chroma,v=chroma,dither=1).ConvertToYV16(),!lsb_in?input8y.Planar2Interleaved(!chroma).ConvertToYV16():input8y.ConvertToYV16(),                                                   \
811
                                                                          input8.Planar2Interleaved(!chroma).ConvertToYV16().mt_lut(      "x 16 < 255 x 75 > 0 255 x 16 - 4.322033898305085 * - ? ?",u=1,v=1), luma=planar?false:chroma,          u=bug_wa,v=bug_wa).ConvertToYUY2().Interleaved2planar(!chroma)                                                    : \
812
                                                     avs26 ? mt_merge     (Dfttest(inputd,sstring="0.0:4.0 0.2:9.0 1.0:15.0",tbsize=1,u=chroma,v=chroma,dither=1),input8,   \
813
                                                                          input8.mt_lut("x 16 scalef < range_max x 75 scalef > 0 range_max x 16 scalef - range_max 75 scalef 16 scalef - / * - ? ?",u=1,v=1), luma=chroma, u=chr,v=chr)                                                   : \
814
                                                             mt_merge     (planar ? Dfttest(!lsb_in?inputd:input8y,sstring="0.0:4.0 0.2:9.0 1.0:15.0",tbsize=1,u=chroma,v=chroma,dither=1).Interleaved2planar(!chroma)                          : \
815
                                                                                    Dfttest(!lsb_in?inputd:input8 ,sstring="0.0:4.0 0.2:9.0 1.0:15.0",tbsize=1,u=chroma,v=chroma,dither=1),input8,   \
816
                                                                          input8.mt_lut(      "x 16 < 255 x 75 > 0 255 x 16 - 4.322033898305085 * - ? ?",u=1,v=1), luma=chroma,          u=bug_wa,v=bug_wa))                                                   : \
817
                     (prefilter==4)  ?  planar ? inputd.SMDegrain_KNLMeansCL(lsb=lsb, lsb_in=lsb_in, device_type=device_type, device_id=device_id, chroma=chroma, h=h, d=d, a=a, knlm_params=knlm_params).Interleaved2planar(!chroma) : \ 
818
                                                 inputd.SMDegrain_KNLMeansCL(lsb=lsb, lsb_in=lsb_in, device_type=device_type, device_id=device_id, chroma=chroma, h=h, d=d, a=a, knlm_params=knlm_params) : \
819
                                         Assert(false,    "prefilter must be between -1~4: "+string(prefilter))
820
}
821
822
823
824
# SMDegrain_KNLMeansCL
825
826
function SMDegrain_KNLMeansCL (clip input, String "device_type", int "device_id", bool "chroma", bool "lsb", bool "lsb_in", float "h", int "d", int "a", String "knlm_params")
827
{
828
d            = Default( d ,0)
829
a            = Default( a ,1)
830
h            = Default( h ,7.0)
831
deviceid     = Default( device_id ,0)
832
knlm_params  = default(knlm_params, "")
833
chroma       = Default( chroma  ,true)
834
lsb_in       = Default( lsb_in  ,false)
835
lsb          = Default( lsb     ,lsb_in)
836
837
                                              avspmt  = Findstr(VersionString(), "AviSynth+") != 0 && Findstr(VersionString(), "r1576") == 0
838
                                              fullchr = avspmt ? input.is444() : input.isyv24()
839
                                              chr420  = avspmt ? input.is420() : input.isyv12()
840
                                              nochr   = avspmt ?   input.isy() : input.isy8()
841
                                              chrlsb  = chroma && !fullchr && !nochr
842
                                              NL_in   = lsb && !lsb_in ? input.Dither_convert_8_to_16() : input
843
                                              cnl     = chrlsb ? "Y" : input.isrgb() ? "auto" : chroma ? "YUV" : "Y"
844
845
NL_in = !chrlsb && input.isyuy2() ? NL_in.converttoyv16() : NL_in
846
847
                               chrlsb ? eval("""
848
                                              # In a more lucid state I could probably have laid out this block much better... or not...
849
850
                                              NL_W    = width(NL_in)
851
                                              Uclip   = avspmt ? ExtractU(NL_in) : UToY8(NL_in)
852
                                              Vclip   = avspmt ? ExtractV(NL_in) : VToY8(NL_in)
853
                                              NL_lsb  = (chr420 ? StackVertical(  lsb ? StackVertical(Dither_get_msb(uclip),Dither_get_msb(vclip)) : uclip,\
854
                                                                                  lsb ? StackVertical(Dither_get_lsb(uclip),Dither_get_lsb(vclip)) : vclip) : \
855
                                                                  StackHorizontal(uclip,vclip))
856
857
                                              nlc     = StackHorizontal(avspmt ? ConvertToY(NL_in) : ConvertToY8(NL_in),NL_lsb)
858
859
                                              nlc     = Eval("nlc.KNLMeansCL(D=d, A=a, h=h,stacked=lsb_in || lsb,device_type=device_type,device_id=deviceid,channels=cnl" + knlm_params + ")")
860
861
                                              uvh = lsb_in || lsb ? uclip.height()/2 : uclip.height()
862
                                              uvw = uclip.width()
863
864
                                              nly = nlc.crop(0,0,chr420 ? -uvw : -(uvw+uvw),0)
865
866
                                              nlu = chr420 ? lsb_in || lsb ? StackVertical(Dither_get_msb(nlc).crop(NL_W,0,0,-uvh),Dither_get_lsb(nlc).crop(NL_W,0,0,-uvh)) : nlc.crop(NL_W,0,0,-uvh)                : \
867
                                                              nlc.crop(NL_W    ,0,-uvw,0)
868
                                              nlv = chr420 ? lsb_in || lsb ? StackVertical(Dither_get_msb(nlc).crop(NL_W,uvh, 0,0),Dither_get_lsb(nlc).crop(NL_W,uvh, 0,0)) : nlc.crop(NL_W,uvh, 0,0)                : \
869
                                                              nlc.crop(NL_W+uvw,0,   0,0)
870
                                              YToUV(nlu, nlv, nly)
871
                                             """) :   Eval("NL_in.KNLMeansCL(D=d, A=a, h=h,stacked=lsb_in || lsb,device_type=device_type,device_id=deviceid,channels=cnl" + knlm_params + ")")
872
873
!lsb && lsb_in ? Ditherpost(mode=6,slice=false) : last
874
input.isyuy2() ? converttoyuy2() : last
875
}