Advertisement
Guest User

Advanced Denoising

a guest
Feb 8th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Advanced Denoising and anime bob v1.39
  2.  
  3. # MotionThresh by mf, mod by A.SONY
  4. # Simple scenechange-proof motion threshold
  5. # Tile outputs 16x16 clip for speed
  6. # Use tile=true for conditional filtering, tile=false for masking
  7.  
  8. function MotionThresh(clip input, float thresh, bool "tile", bool "fast", int "cache") {
  9. fast = Default(fast, false)
  10. tile = Default(tile,  fast)
  11. cache= default(cache,10)
  12.  
  13. ssispmt   = Findstr(VersionString(), "AviSynth+") != 0 && Findstr(VersionString(), "r1576") == 0
  14.  
  15. sislumaonly = !(VersionNumber() < 2.60) ? ssispmt ? input.isy() : input.isy8() : nop()
  16.  
  17. input = fast && !sislumaonly ? ssispmt ? input.converttoy() : input.converttoy8() : input
  18. black = !fast ? BlankClip(input, width=16, height=16               ) : BlankClip(input, width=16, height=16, Color_yuv=color_black)
  19. white = !fast ? BlankClip(input, width=16, height=16, color=$FFFFFF) : BlankClip(input, width=16, height=16, Color_yuv=color_white)
  20. cond1 = fast ? nop() : ConditionalFilter(input, white, black, "YDifferenceFromPrevious()", "greaterthan", String(thresh))
  21. cond2 = fast ? nop() : ConditionalFilter(input, white, black, "YDifferenceToNext()", "greaterthan", String(thresh))
  22. fast ? eval("""
  23. global MotionThreshblack = black
  24. global MotionThreshthresh = thresh
  25. global MotionThreshinput = input
  26. global mtcav=0
  27. global mtnav=0
  28. """) : nop()
  29. fast ? white.ScriptClip("""
  30. MotionThreshwhite = last
  31. input = MotionThreshinput
  32. cfr=current_frame
  33.  
  34. mtpav=cfr<3 ? 0 : mtcav
  35. global mtcav=cfr<2 ? 0 : mtnav
  36. global mtnav=input.trim(1,0).AverageLuma()
  37.  
  38. cond1 = abs(mtcav-mtpav) > MotionThreshthresh ? MotionThreshwhite : MotionThreshblack
  39. cond2 = abs(mtcav-mtnav) > MotionThreshthresh ? MotionThreshwhite : MotionThreshblack
  40. Overlay(cond1, cond2, mode="darken")
  41. """) : nop()
  42. fast ? last : Overlay(cond1, cond2, mode="darken")
  43. tile ? last : PointResize(input.width, input.height)
  44. fast ? cache<0 ? last : last.RequestLinear(8, cache, 5, false, false) : last
  45. }
  46.  
  47.  
  48. ######################################################################
  49. # MotionRamp by mf, mod for speed by A.SONY
  50. # Average motion soft-thresholding based on 5 thresholds
  51. # Dependancies: MotionThresh, ParameterisedBlend or Average if avs is 2.6 or up
  52.  
  53. function MotionRamp(clip input, int thresh1, int thresh2, int thresh3, int thresh4, int thresh5, int "min", int "max", int "floor", int "ceil", int "radius", bool "tile", bool "fast", int "cache") {
  54. min = Default(min, 0)
  55. max = Default(max, 255)
  56. floor = Default(floor, 0)
  57. ceil  = Default(ceil, 255)
  58. radius = Default(radius, 2)
  59. fast = Default(fast, false)
  60. tile = Default(tile,  fast)
  61. cache= default(cache,10)
  62.  
  63. ssispmt   = Findstr(VersionString(), "AviSynth+") != 0 && Findstr(VersionString(), "r1576") == 0
  64.  
  65. sislumaonly = fast ? ssispmt ? input.isy() : input.isy8() : false
  66.  
  67. fast && !(VersionNumber() < 2.60) && !sislumaonly ? ssispmt ? input.converttoy() : input.converttoy8() : input
  68.  
  69. fast ? eval("""
  70. white = BlankClip(last, width=16, height=16, Color_yuv=color_white)
  71. global MotionThreshblack = BlankClip(last, width=16, height=16, Color_yuv=color_black)
  72. global MotionThreshthresh1 = thresh1
  73. global MotionThreshthresh2 = thresh2
  74. global MotionThreshthresh3 = thresh3
  75. global MotionThreshthresh4 = thresh4
  76. global MotionThreshthresh5 = thresh5
  77. global MotionThreshinput = last
  78. global mtnav=0
  79. """) : last
  80. fast ? white.ScriptClip("""
  81. input = MotionThreshinput
  82. MotionThreshwhite = last
  83. cfr=current_frame
  84.  
  85. mtpav=cfr<1 ? 0 : mtnav
  86. global mtnav=input.YDifferenceToNext()
  87.  
  88. cond11 = mtpav > MotionThreshthresh1 ? MotionThreshwhite : MotionThreshblack
  89. cond21 = mtnav > MotionThreshthresh1 ? MotionThreshwhite : MotionThreshblack
  90. c1=Overlay(cond11, cond21, mode="darken")
  91.  
  92. cond12 = mtpav > MotionThreshthresh2 ? MotionThreshwhite : MotionThreshblack
  93. cond22 = mtnav > MotionThreshthresh2 ? MotionThreshwhite : MotionThreshblack
  94. c2=Overlay(cond12, cond22, mode="darken")
  95.  
  96. cond13 = mtpav > MotionThreshthresh3 ? MotionThreshwhite : MotionThreshblack
  97. cond23 = mtnav > MotionThreshthresh3 ? MotionThreshwhite : MotionThreshblack
  98. c3=Overlay(cond13, cond23, mode="darken")
  99.  
  100. cond14 = mtpav > MotionThreshthresh4 ? MotionThreshwhite : MotionThreshblack
  101. cond24 = mtnav > MotionThreshthresh4 ? MotionThreshwhite : MotionThreshblack
  102. c4=Overlay(cond14, cond24, mode="darken")
  103.  
  104. cond15 = mtpav > MotionThreshthresh5 ? MotionThreshwhite : MotionThreshblack
  105. cond25 = mtnav > MotionThreshthresh5 ? MotionThreshwhite : MotionThreshblack
  106. c5=Overlay(cond15, cond25, mode="darken")
  107.  
  108. VersionNumber() < 2.6 ? Interleave(c3, c2, c4, c1, c5).ParameterisedBlend(0.20, 0.20, 0.20, 0.20, 0.20, gamma=1, scaleweights=false).SelectEvery(5,0) : Average(c3, 0.20, c2, 0.20, c4, 0.20, c1, 0.20, c5, 0.20)
  109. """) : VersionNumber() < 2.6 ? Interleave(MotionThresh(thresh3, tile=true), MotionThresh(thresh2, tile=true), MotionThresh(thresh4, tile=true), MotionThresh(thresh1, tile=true), MotionThresh(thresh5, tile=true)).ParameterisedBlend(0.20, 0.20, 0.20, 0.20, 0.20, gamma=1, scaleweights=false).SelectEvery(5,0) : \
  110.                                Average(MotionThresh(thresh3, tile=true), 0.20, MotionThresh(thresh2, tile=true), 0.20, MotionThresh(thresh4, tile=true), 0.20, MotionThresh(thresh1, tile=true), 0.20, MotionThresh(thresh5, tile=true), 0.20)
  111. fast ? cache<0 ? last : last.RequestLinear(8, cache, 5, false, false) : last
  112. TemporalSoften(radius,255,0,255,2)
  113. fast ? last : Levels(floor, 1, ceil, min, max)
  114. fast ? last : ColorYUV(levels="TV->PC")
  115. tile ? last : PointResize(input.width, input.height)
  116. }
  117.  
  118.  
  119. ##########
  120. function admfilter(clip input, bool "mc", float "f", bool "DarkPreserve", float "rStr", float "amp", bool "lsb", bool "lsb_in", bool "lsb_out", bool "luma_rebuild", bool "u", bool "v", string "pp", string "dfttest_params", string "mcdfttest_params", string "custom_filter", clip "MotionRampadc") {
  121. d = default (DarkPreserve      , true)
  122. mc = default (mc      , true)
  123. ssispmt = Findstr(VersionString(), "AviSynth+") != 0 && Findstr(VersionString(), "r1576") == 0
  124. f = Default(f, 16.0)
  125. lsb = default (lsb, ssispmt ? input.BitsPerComponent() > 8 ? false : true : true)
  126. lsb_in = default (lsb_in      , false)
  127. lsb_out = default (lsb_out      , lsb_in)
  128. lsb = lsb_in || lsb_out ? true : lsb
  129. luma_rebuild = default (luma_rebuild      , true)
  130. u = default (u      , true)
  131. v = default (v      , true)
  132. dfttest_params = default(dfttest_params, "")
  133. mcdfttest_params = default(mcdfttest_params, "")
  134. pp = default (pp, "blur(1.53)")
  135.  
  136. custom_filter = defined(custom_filter) ? custom_filter : \
  137.                                                          mc ? "dfttestmc(pp=Eval(pp),lsb=lsb, Sigma=adSigma+1.0/f,rStr=rStr,amp=amp,lsb_in=lsb_in,luma_rebuild=luma_rebuild,u=u,v=v,dfttest_params=dfttest_params" + mcdfttest_params + ")" : \
  138.                                                               "dfttest(lsb=lsb, Sigma=adSigma+1.0/f, lsb_in=lsb_in,u=u,v=v" + dfttest_params + ")"
  139.  
  140. input
  141. MotionRampadc = defined(MotionRampadc) ? MotionRampadc : last.MotionRamp(5,10,15,20,25,Max=255,fast=True)
  142. lsb_out && !lsb_in ? Dither_convert_8_to_16() : lsb_in && !lsb_out ? Ditherpost(mode=-1, y=1, u=1,v=1) : last
  143. return GScriptClip("""
  144.                     adfloat = AverageLuma(MotionRampadc)
  145.                     adSigmb = ssispmt ? BitsPerComponent() > 8 ? BitsPerComponent() == 10 ? 4 : BitsPerComponent() == 12 ? 8 : BitsPerComponent() == 14 ? 64 : nop() : nop() : nop()
  146.                     adSigma = ssispmt ? BitsPerComponent() > 8 ? BitsPerComponent() < 16 ? adfloat/adSigmb : BitsPerComponent() > 30 ? adfloat*255.0 : sqrt(adfloat) : adfloat : adfloat
  147.                     (lsb_out && !lsb_in) || (lsb_in && !lsb_out) ? input : last
  148.                     adden = Eval(custom_filter)
  149.                     d ? DarkPreserve_function(filtered=adden, original=last, u=u ? 3 : 4, v=v ? 3 : 4, lsb=lsb, lsb_in=lsb_in, lsb_out=lsb_out) : adden
  150.                     !d && !lsb_out && lsb ? Ditherpost(mode=7, slice=false) : last
  151.                     """, args="MotionRampadc, f, d, rStr, amp, lsb_in, lsb_out, lsb, luma_rebuild, u, v,dfttest_params,mcdfttest_params,pp,input,custom_filter,ssispmt")
  152. }
  153.  
  154.  
  155. #######
  156. function DarkPreserve_function(clip "filtered", clip "original", bool "merge16_8", int "u", int "v", bool "lsb", bool "lsb_in", bool "lsb_out") {
  157. lsb_in    = default (lsb_in                       , false) #for original
  158. F_lsb_in  = lsb_in ? (Height(filtered)) == (Height(original)) : (Height(filtered)) == (Height(original)*2)
  159. lsb_out   = default (lsb_out         , F_lsb_in || lsb_in)
  160. lsb       = default (lsb ,  lsb_in || lsb_out || F_lsb_in)
  161. merge16_8 = default (merge16_8                     , true)
  162. ssispmt = Findstr(VersionString(), "AviSynth+") != 0 && Findstr(VersionString(), "r1576") == 0
  163. sislumaonly = ssispmt ? original.isy() : VersionNumber() < 2.6 ? nop() : original.isy8()
  164. u         = default (u                                , VersionNumber() < 2.6 ? 3 : sislumaonly ? 1 : 3)
  165. v         = default (v                                , u)
  166. chroma    = !(u != 3 && v != 3)
  167.  
  168. F_lsb_in        ? Assert(lsb,                          "16stacked filtered clip requires: lsb=true")                : nop()
  169. lsb_in          ? Assert(lsb,                          "lsb_in  requires: lsb=true")                                : nop()
  170. lsb_out         ? Assert(lsb,                          "lsb_out requires: lsb=true")                                : nop()
  171.  
  172. dp_lut = lsb_in ? original.Dither_lut16("x 4096 < 65535 x 19200 > 0 65535 x 4096 - 4.338916843220339 * - ? ?",u=1,v=1) : original.mt_lut("x 16 < 255 x 75 > 0 255 x 16 - 4.322033898305085 * - ? ?",u=1,v=1)
  173.  
  174. dp_lut = lsb_in && merge16_8 ? dp_lut.Ditherpost(mode=6, slice=false, u=1, v=1) : dp_lut
  175.  
  176. dp_merge = lsb ? merge16_8 ? Dither_merge16_8(F_lsb_in ? filtered : filtered.Dither_convert_8_to_16(), lsb_in ? original : original.Dither_convert_8_to_16(), dp_lut, luma=chroma, u=u,v=v) : \
  177.                              Dither_merge16(F_lsb_in ? filtered : filtered.Dither_convert_8_to_16(), lsb_in ? original : original.Dither_convert_8_to_16(), dp_lut, luma=chroma, u=u,v=v) : \
  178.                              mt_merge(filtered, original, dp_lut, luma=chroma, u=u,v=v)
  179. lsb ? lsb_out ? dp_merge : dp_merge.Ditherpost(mode=7, slice=false) : dp_merge
  180. }
  181.  
  182. function lightPreserve_function(clip "filtered", clip "original", bool "merge16_8", int "u", int "v", bool "lsb", bool "lsb_in", bool "lsb_out") {
  183. lsb_in    = default (lsb_in                       , false) #for original
  184. F_lsb_in  = lsb_in ? (Height(filtered)) == (Height(original)) : (Height(filtered)) == (Height(original)*2)
  185. lsb_out   = default (lsb_out         , F_lsb_in || lsb_in)
  186. lsb       = default (lsb ,  lsb_in || lsb_out || F_lsb_in)
  187. merge16_8 = default (merge16_8                     , true)
  188. ssispmt = Findstr(VersionString(), "AviSynth+") != 0 && Findstr(VersionString(), "r1576") == 0
  189. sislumaonly = ssispmt ? original.isy() : VersionNumber() < 2.6 ? nop() : original.isy8()
  190. u         = default (u                                , VersionNumber() < 2.6 ? 3 : sislumaonly ? 1 : 3)
  191. v         = default (v                                , u)
  192. chroma    = !(u != 3 && v != 3)
  193.  
  194. F_lsb_in        ? Assert(lsb,                          "16stacked filtered clip requires: lsb=true")                : nop()
  195. lsb_in          ? Assert(lsb,                          "lsb_in  requires: lsb=true")                                : nop()
  196. lsb_out         ? Assert(lsb,                          "lsb_out requires: lsb=true")                                : nop()
  197.  
  198. dp_lut = lsb_in ? original.Dither_lut16("x 60160 > 65535 x 45056 < 0 65535 60160 x - 4.338916843220339 * - ? ?",u=1,v=1) : original.mt_lut("x 235 > 255 x 176 < 0 255 235 x - 4.322033898305085 * - ? ?",u=1,v=1)
  199.  
  200. dp_lut = lsb_in && merge16_8 ? dp_lut.Ditherpost(mode=6, slice=false, u=1, v=1) : dp_lut
  201.  
  202. dp_merge = lsb ? merge16_8 ? Dither_merge16_8(F_lsb_in ? filtered : filtered.Dither_convert_8_to_16(), lsb_in ? original : original.Dither_convert_8_to_16(), dp_lut, luma=chroma, u=u,v=v) : \
  203.                              Dither_merge16(F_lsb_in ? filtered : filtered.Dither_convert_8_to_16(), lsb_in ? original : original.Dither_convert_8_to_16(), dp_lut, luma=chroma, u=u,v=v) : \
  204.                              mt_merge(filtered, original, dp_lut, luma=chroma, u=u,v=v)
  205. lsb ? lsb_out ? dp_merge : dp_merge.Ditherpost(mode=7, slice=false) : dp_merge
  206. }
  207.  
  208.  
  209. ##########
  210. #edge side bleed
  211. #original idea by "colours", this function mod by A.SONY
  212. ##########
  213. function edgesidebleed(clip input, float "w32", float "w16", float "w8", float "w4", float "w2", bool "bleed", bool "chroma", int "mode") {
  214. w2     = Default(w2,     0.001)
  215. w4     = Default(w4,     0.055)
  216. w8     = Default(w8,     0.015)
  217. w16    = Default(w16,     0.02)
  218. w32    = Default(w32,    0.001)
  219. chroma = default(chroma, false)
  220. mode   = default(mode,       0)
  221. bleed  = default(bleed, !(VersionNumber() < 2.6))
  222.  
  223. ssispmt = !chroma ? Findstr(VersionString(), "AviSynth+") != 0 && Findstr(VersionString(), "r1576") == 0 : nop()
  224. sislumaonly = !chroma ? ssispmt ? input.isy() : VersionNumber() < 2.6 ? true : input.isy8() : nop()
  225. !chroma ? sislumaonly ? input : ssispmt ? input.converttoy() : input.converttoy8() : input
  226. w = bleed ?  Width() : nop()
  227. h = bleed ? Height() : nop()
  228.  
  229. shift32 = w32!=0 ? !bleed ? Crop(Width()-32,0,32,0,true).StackHorizontal(Crop(0,0,-32,0,true)) : nop() : nop()
  230. shift32 = w32!=0 ? bleed ? BicubicResize (w / 32, h, 1, 0,src_left=-32).BicubicResize (w, h, 1, 0) : shift32 : nop()
  231. w32==0 ? last : mode == 0 ? average(last,1+w32,shift32,-w32) : mode==1 ? lightPreserve_function(average(last,1+w32,shift32,-w32),last) : DarkPreserve_function(average(last,1+w32,shift32,-w32),last)
  232. shift16 = w16!=0 ? !bleed ? Crop(Width()-16,0,16,0,true).StackHorizontal(Crop(0,0,-16,0,true)) : nop() : nop()
  233. shift16 = w16!=0 ? bleed ? BicubicResize (w / 16, h, 1, 0,src_left=-16).BicubicResize (w, h, 1, 0) : shift16 : nop()
  234. w16==0 ? last : mode == 0 ? average(last,1+w16,shift16,-w16) : mode==1 ? lightPreserve_function(average(last,1+w16,shift16,-w16),last) : DarkPreserve_function(average(last,1+w16,shift16,-w16),last)
  235. shift8 = w8!=0 ? !bleed ? Crop(Width()-8,0,8,0,true).StackHorizontal(Crop(0,0,-8,0,true)) : nop() : nop()
  236. shift8 = w8!=0 ? bleed ? BicubicResize (w / 8, h, 1, 0,src_left=-8).BicubicResize (w, h, 1, 0) : shift8 : nop()
  237. w8==0 ? last : mode == 0 ? average(last,1+w8,shift8,-w8) : mode==1 ? lightPreserve_function(average(last,1+w8,shift8,-w8),last) : DarkPreserve_function(average(last,1+w8,shift8,-w8),last)
  238. shift4 = w4!=0 ? !bleed ? Crop(Width()-4,0,4,0,true).StackHorizontal(Crop(0,0,-4,0,true)) : nop() : nop()
  239. shift4 = w4!=0 ? bleed ? BicubicResize (w / 4, h, 1, 0,src_left=-4).BicubicResize (w, h, 1, 0) : shift4 : nop()
  240. w4==0 ? last : mode == 0 ? average(last,1+w4,shift4,-w4) : mode==1 ? lightPreserve_function(average(last,1+w4,shift4,-w4),last) : DarkPreserve_function(average(last,1+w4,shift4,-w4),last)
  241. shift2 = w2!=0 ? !bleed ? Crop(Width()-2,0,2,0,true).StackHorizontal(Crop(0,0,-2,0,true)) : nop() : nop()
  242. shift2 = w2!=0 ? bleed ? BicubicResize (w / 2, h, 1, 0,src_left=-2).BicubicResize (w, h, 1, 0) : shift2 : nop()
  243. w2==0 ? last : mode == 0 ? average(last,1+w2,shift2,-w2) : mode==1 ? lightPreserve_function(average(last,1+w2,shift2,-w2),last) : DarkPreserve_function(average(last,1+w2,shift2,-w2),last)
  244.  
  245. !chroma ? sislumaonly ? last : ssispmt ? CombinePlanes(last,input,planes="YUV",sample_clip=input) : ytouv(input.utoy8(),input.vtoy8(),last) : last
  246. }
  247.  
  248.  
  249. ##########
  250. #chroma edge side bleed (aka pink artifacts) fix, by A.SONY
  251. ##########
  252. function chromasidebleed(clip input, int "diameter", float "sDev", float "iDev", float "cs", bool "bic", string "cplace", bool "d2", int "kernS", int "kernI", int "resType")
  253. {
  254. diameter  = default (diameter,    11)
  255. sDev      = default (sDev,      31.1)
  256. iDev      = default (iDev,      15.1)
  257. d2        = default (d2,        true)
  258. kernS     = default (kernS,        8)
  259. kernI     = default (kernI,        3)
  260. restype   = default (restype,      0)
  261. bic       = default (bic,       true)
  262. cplace    = default (cplace, "MPEG2")
  263.  
  264. chroma = false
  265. gui = false
  266.  
  267. ssispmt   = Findstr(VersionString(), "AviSynth+") != 0 && Findstr(VersionString(), "r1576") == 0
  268. sisfullchr = ssispmt ? input.is444() : VersionNumber() < 2.6 ? false : input.isyv24()
  269. sislumaonly = ssispmt ? input.isy() : VersionNumber() < 2.6 ? false : input.isy8()
  270. Assert(!sislumaonly, "why you use this on no chroma clip?!")
  271.  
  272. y=VersionNumber() < 2.6 ? input : ssispmt ? input.converttoy() : input.converttoy8()
  273. u=VersionNumber() < 2.6 ? input.utoy() : input.utoy8()
  274. v=VersionNumber() < 2.6 ? input.vtoy() : input.vtoy8()
  275. cshift = (cplace == "MPEG1" && IsYV12(input)) || IsYV24(input) ? 0 : -0.5
  276. ych = sisfullchr ? y : bic ? input.BicubicResize(u.width(), v.height(), 1, 0, src_left=cshift) : input.BilinearResize(u.width(), v.height(), src_left=cshift)
  277. u=u.ConvertToYV12().TBilateral(diameterL=diameter, sDevL=sDev, iDevL=iDev, csL=cs, d2=d2, chroma=chroma, ppclip=ych.ConvertToYV12(), kernS=kernS, kernI=kernI, resType=resType)
  278. v=v.ConvertToYV12().TBilateral(diameterL=diameter, sDevL=sDev, iDevL=iDev, csL=cs, d2=d2, chroma=chroma, ppclip=ych.ConvertToYV12(), kernS=kernS, kernI=kernI, resType=resType)
  279. YToUV(u,v,y)
  280. }
  281.  
  282.  
  283. ###############
  284. # Hqdn3d_2
  285.  
  286. function Hqdn3d_2(clip clip, float "ls", float "cs", float "lt", float "ct", int "UV", bool "lsb", bool "lsb_in", bool "i16") {
  287. cs        = default (cs              , 3.0)
  288. ct        = default (ct              , 4.5)
  289. UV        = default (UV               , 3)
  290. UV        = !(VersionNumber() < 2.60) ? isY8(clip) ? 1 : UV : UV
  291. lsb_in    = default (lsb_in          , false)
  292. i16       = default (i16             , false)
  293. lsb_out   = default (lsb             , lsb_in)
  294.  
  295. i16b      = lsb_out || i16 || lsb_in
  296.  
  297. isrgb(clip) ? Assert(!i16b, "no 16bit for RGB, but you can use some trickes for that (see dither doc)") : nop()
  298.  
  299. yString = VersionNumber() < 2.60 ? "yv12" : "Y8"
  300.  
  301. pclip   = isYUV(clip) && i16b && isYUY2(clip) && !(VersionNumber() < 2.60) ? clip.ConvertToYV16() : clip
  302.  
  303. i16clip = isYUV(clip) ? i16b ? lsb_in ? pclip.Bitdepth(from=88, to=16) : i16 ? clip : pclip.Bitdepth(from=8, to=16) : nop() : nop()
  304.  
  305.     y = isYUV(clip) ? VersionNumber() < 2.60 ? i16b ? i16clip : clip : i16b ? ConvertToY8(i16clip) : ConvertToY8(clip) : nop()
  306.     u = isYUV(clip) && UV != 1 ? VersionNumber() < 2.60 ? i16b ? UToY(i16clip) : UToY(clip) : i16b ? UToY8(i16clip) : UToY8(clip) : nop()
  307.     v = isYUV(clip) && UV != 1 ? VersionNumber() < 2.60 ? i16b ? VToY(i16clip) : VToY(clip) : i16b ? VToY8(i16clip) : VToY8(clip) : nop()
  308.     y = isYUV(clip) ? VersionNumber() < 2.60 ? isclip(i16clip) ? y.converttoyv12().Hqdn3d16Y(sp=ls, tp=lt) : y.converttoyv12().Hqdn3dY(sp=ls, tp=lt) : isclip(i16clip) ? y.Hqdn3d16Y(sp=ls, tp=lt) : y.Hqdn3dY(sp=ls, tp=lt) : nop()
  309.     u = isYUV(clip) ? UV == 3 ? VersionNumber() < 2.60 ? isclip(i16clip) ? u.converttoyv12().Hqdn3d16Y(sp=cs, tp=ct) : u.converttoyv12().Hqdn3dY(sp=cs, tp=ct) : isclip(i16clip) ? u.Hqdn3d16Y(sp=cs, tp=ct) : u.Hqdn3dY(sp=cs, tp=ct) : u : nop()
  310.     v = isYUV(clip) ? UV == 3 ? VersionNumber() < 2.60 ? isclip(i16clip) ? v.converttoyv12().Hqdn3d16Y(sp=cs, tp=ct) : v.converttoyv12().Hqdn3dY(sp=cs, tp=ct) : isclip(i16clip) ? v.Hqdn3d16Y(sp=cs, tp=ct) : v.Hqdn3dY(sp=cs, tp=ct) : v : nop()
  311.     isYUV(clip) ? (VersionNumber() < 2.60) && isYUY2(clip) ? UV == 1 ? y : YToUV(u.converttoyuy2(),v.converttoyuy2(),y.converttoyuy2()) : UV == 1 ? y : YToUV(u,v,y) : nop()
  312.         isYUV(clip) ? i16b && !i16 && lsb_out ? Bitdepth(from=16, to=88) : last : last
  313.     isYUV(clip) ? !i16b || lsb_out || i16 ? last : Bitdepth(from=16, to=8) : last
  314.     isYUY2(clip) ? VersionNumber() < 2.60 ? last : converttoyuy2() : last
  315.  
  316.     A = isrgb32(clip) ? clip.ShowAlpha(yString) : nop()
  317.     r = isrgb(clip) ? clip.ShowRed(yString) : nop()
  318.     g = isrgb(clip) ? clip.ShowGreen(yString) : nop()
  319.     b = isrgb(clip) ? clip.ShowBlue(yString) : nop()
  320.     A = isrgb32(clip) ? A.Hqdn3dY(sp=ls, tp=lt) : nop()
  321.     r = isrgb(clip) ? r.Hqdn3dY(sp=ls, tp=lt) : nop()
  322.     g = isrgb(clip) ? g.Hqdn3dY(sp=ls, tp=lt) : nop()
  323.     b = isrgb(clip) ? b.Hqdn3dY(sp=ls, tp=lt) : nop()
  324.     isYUV(clip) ? last : isrgb32(clip) ? MergeARGB(A,r,g,b) : MergeRGB(r,g,b,"RGB24")
  325. }
  326.  
  327.  
  328. ###############
  329. #motion adaptive by A.SONY
  330.  
  331. function smam(clip input, clip "prefilter", val "filter", int "pel", int "blksize", clip "motionmask", float "Str", float "Amp", bool "TV_range", bool "qtgmc_lsb", int "tr2", int "usedaa3mod") {
  332.  
  333. ssispmt   = Findstr(VersionString(), "AviSynth+") != 0 && Findstr(VersionString(), "r1576") == 0
  334. qtgmc_lsb = default(qtgmc_lsb, ssispmt ? input.BitsPerComponent() > 8 ? false : true : true)
  335.  
  336. defined(filter) ? Assert((Isclip(filter) || IsString(filter)),        "'filter' only accepts clip or string") : nop()
  337.  
  338. Str             = default (Str, 1.5)
  339. exfilclp        = isclip(filter)
  340. sisbob          = round(framerate(input))==60 || framerate(input)==50
  341. tr2             = default (tr2, sisbob ? 3 : 1)
  342. usedaa3mod      = default (usedaa3mod, exfilclp ? 0 : sisbob ? 1 : 2)
  343.  
  344. infiltr = usedaa3mod == 1 ? input.daa3mod() : input
  345. filclip = defined(filter) ? exfilclp ? filter : eval("infiltr." + filter) : infiltr.QTGMC(InputType=1, tr0=0, tr1=sisbob ? undefined : 1, tr2=tr2, lsb=qtgmc_lsb, rep1=sisbob ? undefined : 11, rep2=sisbob ? undefined : 11, Sharpness=0.0, TV_range=TV_range, Str=Str, Amp=Amp)
  346. filclip = usedaa3mod == 2 ? filclip.daa3mod() : filclip
  347.  
  348. momask  = !defined(motionmask) ? input.smam_mask(prefilter,pel,blksize) : motionmask #if you deal with YUY2 then it should be Planar YUY2 in avs2.5 and yv16 in avs2.6
  349. isyuy2(input) && VersionNumber() < 2.60 ? mt_merge(input.Interleaved2Planar(),filclip.Interleaved2Planar(),momask,u=3,v=3).Planar2Interleaved() : isyuy2(input) ? mt_merge(input.converttoyv16(),filclip.converttoyv16(),momask,u=3,v=3).converttoyuy2() : mt_merge(input,filclip,momask,u=3,v=3)
  350. }
  351.  
  352.  
  353. ###############
  354. #smam_mask
  355. # if input is YUY2 the output will be Planar YUY2 in avs 2.5 and yv16 in avs 2.6
  356.  
  357. function smam_mask(clip input, clip "prefilter", int "pel", int "blksize", bool "chroma", int "dct") {
  358.     ox = input.width()
  359.     oy = input.height()
  360.     HD = (ox > 1099 || oy > 599)
  361.     sisbob  = round(framerate(input))==60 || framerate(input)==50
  362.     pel = default( pel,     HD ? 1 : 2 )
  363.     dct = default( dct, sisbob ? 0 : 2 )
  364.     blksize = default(blksize,HD ? sisbob ? 8 : 16 : sisbob ? 4 : 8)
  365. preclip= defined(prefilter) ? prefilter : (VersionNumber() < 2.60) && isyv12(input) ? input.HQdn3d().FFT3DFilter() : input.HQdn3d_2().FFT3DFilter()
  366. sup    = preclip.MSuper(pel=pel,sharp=1)
  367. fv1    = sup.MAnalyse(isb=false,delta=1,DCT=dct,Truemotion=false,blksize=blksize,chroma=chroma)
  368. fv2    = sup.MAnalyse(isb=true,delta=1,DCT=dct,Truemotion=true,blksize=blksize,chroma=chroma)
  369.  
  370. momask1 = input.MMask(fv1, kind = 1, ml=2)
  371. momask2 = input.MMask(fv2, kind = 1, ml=3)
  372. momask1 =isyuy2(input) && VersionNumber() < 2.60 ? momask1.Interleaved2Planar() : isyuy2(input) ? momask1.converttoyv16() : momask1
  373. momask2 =isyuy2(input) && VersionNumber() < 2.60 ? momask2.Interleaved2Planar() : isyuy2(input) ? momask2.converttoyv16() : momask2
  374. mt_average(momask1,momask2,u=3,v=3)
  375. }
  376.  
  377.  
  378. ##############
  379. # sanimebob by A.SONY
  380.  
  381. function sanimebob(clip i, val "useqtgmc", val "bobpresmooth", float "Str", float "Amp", bool "TV_range", bool "qtgmc_lsb", bool "usedaa3mod", bool "usesmam", int "tr2") {
  382.  
  383. ssispmt   = Findstr(VersionString(), "AviSynth+") != 0 && Findstr(VersionString(), "r1576") == 0
  384. qtgmc_lsb = default(qtgmc_lsb, ssispmt ? i.BitsPerComponent() > 8 ? false : true : true)
  385.  
  386. useq         = default (useqtgmc,                                                                                                           0)
  387. Stringuseq   = IsString(useq                                                                                                                 )
  388. bobpresmbool = default (isbool(bobpresmooth) ? bobpresmooth : !Stringuseq ? (useq==8 || defined(bobpresmooth)) : defined(bobpresmooth), false)
  389. Str          = default (Str,                                                                                                              1.5)
  390. tr2          = default (tr2,                                                                                !Stringuseq ? useq==8 ? 3 : 1 : 1)
  391. usedaa3mod   = default (usedaa3mod,                                            !Stringuseq ? useq==4 || useq==5 || useq==7 || useq==9 : false)
  392. usesmam      = default (usesmam,                                                       !Stringuseq ? !(useq==0 || useq==1 || useq==8) : false)
  393.  
  394. Assert(Isint(useq) || Stringuseq, "'useqtgmc' only accepts int or string")
  395.  
  396. prefiltered_i   = defined(bobpresmooth) ? isclip(bobpresmooth) ? bobpresmooth : IsString(bobpresmooth) ? Eval("i." + bobpresmooth) : undefined() : undefined()
  397.  
  398. iforbob   = bobpresmbool ? defined(prefiltered_i) ? prefiltered_i : i.QTGMC_bob(0,0.5).reduceflicker(strength=2).interlaced60or50(BFF=!(GetParity(i))) : i
  399. iforbob   = bobpresmbool && !defined(prefiltered_i) ? isyuy2(i) ? iforbob.SeparateFields().Interleaved2Planar().Repair(i.SeparateFields().Interleaved2Planar(),Planar=true).Planar2Interleaved().weave() : iforbob.SeparateFields().Repair(i.SeparateFields()).weave() : iforbob
  400.  
  401. pyi       = i.pointresize(width(i),height(i)+8,0,-4,width(i),height(i)+8.0001)
  402. pei       = i.eedi3(-2,sclip=iforbob.nnedi3(-2)).pointresize(width(i),height(i)+8,0,-4,width(i),height(i)+8.0001)
  403.  
  404. ymodclip  = VersionNumber() < 2.60 ? pyi.yadifmod(mode=3, edeint=pei).crop(0,4,-0,-4) : isyuy2(i) ? pyi.converttoyv16().yadifmod2(mode=3, edeint=pei.converttoyv16()).crop(0,4,-0,-4) : pyi.yadifmod2(mode=3, edeint=pei).crop(0,4,-0,-4)
  405. yadifclip = isyuy2(i) ? VersionNumber() < 2.60 ? ymodclip.Interleaved2Planar().Repair(i.TDeint(1,emask=iforbob.tmm2_ortmm1(1)).Interleaved2Planar(),Planar=true).Planar2Interleaved() : \
  406.                                                  ymodclip.Repair(i.TDeint(1,emask=iforbob.tmm2_ortmm1(1)).converttoyv16()).converttoyuy2() : \
  407.                         ymodclip.Repair(i.TDeint(1,emask=iforbob.tmm2_ortmm1(1)))
  408.  
  409. # for custom qtgmc, don't forget to put the input clip like this:- iforbob.QTGMC(... or yadifclip.QTGMC(... or i.QTGMC(...
  410. QTGMCclip = isstring(useq) ? eval(useq) : \
  411.                              useq==0 ? yadifclip : \
  412.        useq==1 || useq==2 || useq==4 ? iforbob.QTGMC(SourceMatch=3, Lossless=2, EdiExt=yadifclip, tr0=1, tr1=1, tr2=tr2, lsb=qtgmc_lsb, rep0=11, rep1=11, rep2=11, Sharpness=0.1, TV_range=TV_range, Str=Str, Amp=Amp) : \
  413.                   useq==3 || useq==5 ? yadifclip.QTGMC(InputType=1, tr0=1, tr1=1, tr2=tr2, lsb=qtgmc_lsb, rep0=11, rep1=11, rep2=11, Sharpness=0.1, TV_range=TV_range, Str=Str, Amp=Amp) : \
  414.                   useq==6 || useq==7 ? yadifclip.QTGMC(InputType=1, tr0=0, lsb=qtgmc_lsb, Sharpness=usedaa3mod ? 0.0 : 0.1, TV_range=TV_range, Str=Str, Amp=Amp) : \
  415.                                        i.QTGMC(SourceMatch=3, Lossless=2, EdiExt=yadifclip, useEdiExt=!(useq==9), tr0=useq==9 ? -1 : undefined(), rep0=useq==9 ? undefined() : 11, tr2=tr2, lsb=qtgmc_lsb, Sharpness=0.0, TV_range=TV_range, Str=Str, Amp=Amp)
  416.  
  417. daa3mclip = usedaa3mod ? QTGMCclip.daa3mod() : QTGMCclip
  418.  
  419. !usesmam ? daa3mclip : \
  420.            yadifclip.smam(filter=QTGMCclip)
  421. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement