Advertisement
Guest User

Advanced Denoising

a guest
Sep 3rd, 2019
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Advanced Denoising and anime bob v1.81
  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 motinc, 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. sisphbd = AvsPlusVersionNumber > 2294
  14.  
  15. sislumaonly = !(VersionNumber() < 2.60) ? sisphbd ? motinc.isy() : motinc.isy8() : nop()
  16.  
  17. motinc = fast && !sislumaonly ? sisphbd ? motinc.converttoy() : motinc.converttoy8() : motinc
  18. black  = !fast ? BlankClip(motinc, width=16, height=16               ) : BlankClip(motinc, width=16, height=16, Color_yuv=color_black)
  19. white  = !fast ? BlankClip(motinc, width=16, height=16, color=$FFFFFF) : BlankClip(motinc, width=16, height=16, Color_yuv=color_white)
  20. cond1  = fast ? nop() : ConditionalFilter(motinc, white, black, "YDifferenceFromPrevious()", "greaterthan", String(thresh))
  21. cond2  = fast ? nop() : ConditionalFilter(motinc, white, black, "YDifferenceToNext()", "greaterthan", String(thresh))
  22. fast ? eval("""
  23. global MotionThreshblack = black
  24. global MotionThreshthresh = thresh
  25. global MotionThreshmotinc = motinc
  26. global mtcav=0
  27. global mtnav=0
  28. """) : nop()
  29. fast ? white.ScriptClip("""
  30. MotionThreshwhite = last
  31. motinc = MotionThreshmotinc
  32. cfr=current_frame
  33.  
  34. mtpav=cfr<3 ? 0 : mtcav
  35. global mtcav=cfr<2 ? 0 : mtnav
  36. global mtnav=motinc.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 : Eval("try { PointResizemt(motinc.width, motinc.height) } catch(error_msg) { PointResize(motinc.width, motinc.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 morinc, 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. sisphbd = AvsPlusVersionNumber > 2294
  64.  
  65. sislumaonly = fast ? sisphbd ? morinc.isy() : morinc.isy8() : false
  66.  
  67. fast && !(VersionNumber() < 2.60) && !sislumaonly ? sisphbd ? morinc.converttoy() : morinc.converttoy8() : morinc
  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 MotionThreshmorinc = last
  78. global mtnav=0
  79. """) : last
  80. fast ? white.ScriptClip("""
  81. morinc = MotionThreshmorinc
  82. MotionThreshwhite = last
  83. cfr=current_frame
  84.  
  85. mtpav=cfr<1 ? 0 : mtnav
  86. global mtnav=morinc.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 : Eval("try { PointResizemt(morinc.width, morinc.height) } catch(error_msg) { PointResize(morinc.width, morinc.height) }")
  116. }
  117.  
  118.  
  119. ##########
  120. function admfilter(clip oin, 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", bool "mcf", clip "ppsuper", clip "super", clip "exmcclip", bool "nlsb") {
  121. d = default (DarkPreserve      , true)
  122. mc = default (mc      , true)
  123. sisphbd = AvsPlusVersionNumber > 2294
  124. f = Default(f, 16.0)
  125. lsb = default (lsb, sisphbd ? oin.BitsPerComponent() > 8 ? false : true : true)
  126. nlsb = default (nlsb, sisphbd && lsb)
  127. lsb_in = default (lsb_in      , false)
  128. u = default (u      , true)
  129. v = default (v      , true)
  130. exmc = defined(exmcclip)
  131. mcf = default (mcf, exmc || (nlsb && lsb_in))
  132. oin8 = lsb_in ? oin.ditherpost(mode=7,slice=false,u=u ? 3 : 1,v=v ? 3 : 1) : oin
  133.     ox = oin8.width()
  134.     oy = oin8.height()
  135.     HD = (ox > 1099 || oy > 599)
  136. lsb_out = default (lsb_out      , lsb_in)
  137. lsb = lsb_in || lsb_out ? true : lsb
  138. luma_rebuild = default (luma_rebuild      , true)
  139. dfttest_params = default(dfttest_params, "")
  140. mcdfttest_params = default(mcdfttest_params, "")
  141. pp = default (pp, "oin8.blur(1.53)")
  142.  
  143. pel = HD ? 1 : 2
  144.  
  145. super = defined(super) ? super : oin8.MSuper(pel=pel, levels=1, chroma=(U || V))
  146. ppsu = defined(ppsuper) ? ppsuper : oin.spp_super(pp=Eval(pp),rStr=rStr,amp=amp,lsb_in=lsb_in,lsb=lsb,luma_rebuild=luma_rebuild,u=u,v=v,hd=hd)
  147. exmcclip = mcf ? exmc ? exmcclip : nlsb && lsb_in ? oin.ConvertFromStacked().sMCclips(ppsu,super.convertbits(16),hd=hd).ConvertToStacked() : oin8.sMCclips(ppsu,super,hd=hd) : nop()
  148.  
  149. custom_filter = defined(custom_filter) ? custom_filter : \
  150.                             mcf ? "dfttestmc(pp=oin8,lsb=lsb, Sigma=adSigma+1.0/f,lsb_in=lsb_in,u=u,v=v,slices=false,super=super,input8=oin8,hd=hd,exmcclip=exmcclip,dfttest_params=dfttest_params" + mcdfttest_params + ")" : \
  151.                                                          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 + ")" : \
  152.                                                               "dfttest(lsb=lsb, Sigma=adSigma+1.0/f, lsb_in=lsb_in,u=u,v=v" + dfttest_params + ")"
  153.  
  154. oin
  155. MotionRampadc = defined(MotionRampadc) ? MotionRampadc : oin8.MotionRamp(5,10,15,20,25,Max=255,fast=True)
  156. lsb_out && !lsb_in ? Dither_convert_8_to_16() : lsb_in && !lsb_out ? Ditherpost(mode=-1, y=1, u=1,v=1) : last
  157. return GScriptClip("""
  158.                     adfloat = AverageLuma(MotionRampadc)
  159.                     adSigmb = sisphbd ? BitsPerComponent() > 8 ? BitsPerComponent() == 10 ? 4 : BitsPerComponent() == 12 ? 8 : BitsPerComponent() == 14 ? 64 : nop() : nop() : nop()
  160.                     adSigma = sisphbd ? isvideofloat() ? adfloat*255.0 : BitsPerComponent() > 8 ? BitsPerComponent() < 16 ? adfloat/adSigmb : sqrt(adfloat) : adfloat : adfloat
  161.                     (lsb_out && !lsb_in) || (lsb_in && !lsb_out) ? oin : last
  162.                     adden = Eval(custom_filter)
  163.                     d ? DarkPreserve_function(filtered=nlsb && lsb ? adden.ConvertFromStacked() : adden, original=nlsb && lsb_in ? last.ConvertFromStacked() : nlsb && lsb ? last.Convertbits(16) : last, u=u ? 3 : 4, v=v ? 3 : 4, lsb=lsb && !nlsb, lsb_in=lsb_in && !nlsb, lsb_out=lsb_out && !nlsb) : adden
  164.                     d && lsb && nlsb ? ConvertToStacked() : last
  165.                     d && lsb && nlsb && !lsb_out ? Ditherpost(mode=7, slice=false) : last
  166.                     !d && !lsb_out && lsb ? Ditherpost(mode=7, slice=false) : last
  167.                     """, args="MotionRampadc, f, d, rStr, amp, lsb_in, lsb_out, lsb, luma_rebuild, u, v,dfttest_params,mcdfttest_params,pp,oin,custom_filter,sisphbd,super,oin8,hd,exmcclip,nlsb")
  168. }
  169.  
  170.  
  171. #######
  172. function DarkPreserve_function(clip "filtered", clip "original", bool "merge16_8", int "u", int "v", bool "lsb", bool "lsb_in", bool "lsb_out") {
  173. lsb_in    = default (lsb_in                       , false) #for original
  174. F_lsb_in  = lsb_in ? (Height(filtered)) == (Height(original)) : (Height(filtered)) == (Height(original)*2)
  175. lsb_out   = default (lsb_out         , F_lsb_in || lsb_in)
  176. lsb       = default (lsb ,  lsb_in || lsb_out || F_lsb_in)
  177. merge16_8 = default (merge16_8                     , true)
  178. sisphbd = AvsPlusVersionNumber > 2294
  179. sislumaonly = sisphbd ? original.isy() : VersionNumber() < 2.6 ? nop() : original.isy8()
  180. u         = default (u                                , VersionNumber() < 2.6 ? 3 : sislumaonly ? 1 : 3)
  181. v         = default (v                                , u)
  182. chroma    = !(u != 3 && v != 3)
  183.  
  184. F_lsb_in        ? Assert(lsb,                          "16stacked filtered clip requires: lsb=true")                : nop()
  185. lsb_in          ? Assert(lsb,                          "lsb_in  requires: lsb=true")                                : nop()
  186. lsb_out         ? Assert(lsb,                          "lsb_out requires: lsb=true")                                : nop()
  187.  
  188. dp_lut = lsb_in ? original.Dither_lut16("x 4096 < 65535 x 19200 > 0 65535 x 4096 - 4.338916843220339 * - ? ?",u=1,v=1) : VersionNumber() < 2.6 ? original.mt_lut("x 16 < 255 x 75 > 0 255 x 16 - 4.322033898305085 * - ? ?",u=1,v=1)
  189.                                                                  \             : original.mt_lut("x 16 scalef < range_max x 75 scalef > 0 range_max x 16 scalef - range_max 75 scalef 16 scalef - / * - ? ?",use_expr=2,u=1,v=1)
  190.  
  191. dp_lut = lsb_in && merge16_8 ? dp_lut.Ditherpost(mode=6, slice=false, u=1, v=1) : dp_lut
  192.  
  193. 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) : \
  194.                              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) : \
  195.                              mt_merge(filtered, original, dp_lut, luma=chroma, u=u,v=v)
  196. lsb ? lsb_out ? dp_merge : dp_merge.Ditherpost(mode=7, slice=false) : dp_merge
  197. }
  198.  
  199. function lightPreserve_function(clip "filtered", clip "original", bool "merge16_8", int "u", int "v", bool "lsb", bool "lsb_in", bool "lsb_out") {
  200. lsb_in    = default (lsb_in                       , false) #for original
  201. F_lsb_in  = lsb_in ? (Height(filtered)) == (Height(original)) : (Height(filtered)) == (Height(original)*2)
  202. lsb_out   = default (lsb_out         , F_lsb_in || lsb_in)
  203. lsb       = default (lsb ,  lsb_in || lsb_out || F_lsb_in)
  204. merge16_8 = default (merge16_8                     , true)
  205. sisphbd = AvsPlusVersionNumber > 2294
  206. sislumaonly = sisphbd ? original.isy() : VersionNumber() < 2.6 ? nop() : original.isy8()
  207. u         = default (u                                , VersionNumber() < 2.6 ? 3 : sislumaonly ? 1 : 3)
  208. v         = default (v                                , u)
  209. chroma    = !(u != 3 && v != 3)
  210.  
  211. F_lsb_in        ? Assert(lsb,                          "16stacked filtered clip requires: lsb=true")                : nop()
  212. lsb_in          ? Assert(lsb,                          "lsb_in  requires: lsb=true")                                : nop()
  213. lsb_out         ? Assert(lsb,                          "lsb_out requires: lsb=true")                                : nop()
  214.  
  215. dp_lut = lsb_in ? original.Dither_lut16("x 60160 > 65535 x 45056 < 0 65535 60160 x - 4.338916843220339 * - ? ?",u=1,v=1) : VersionNumber() < 2.6 ? original.mt_lut("x 235 > 255 x 176 < 0 255 235 x - 4.322033898305085 * - ? ?",u=1,v=1)
  216.                                                                    \                 : original.mt_lut("x 235 scalef > range_max x 176 scalef < 0 range_max x 235 scalef - range_max 176 scalef 235 scalef - / * - ? ?",use_expr=2,u=1,v=1)
  217.  
  218. dp_lut = lsb_in && merge16_8 ? dp_lut.Ditherpost(mode=6, slice=false, u=1, v=1) : dp_lut
  219.  
  220. 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) : \
  221.                              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) : \
  222.                              mt_merge(filtered, original, dp_lut, luma=chroma, u=u,v=v)
  223. lsb ? lsb_out ? dp_merge : dp_merge.Ditherpost(mode=7, slice=false) : dp_merge
  224. }
  225.  
  226.  
  227. ##########
  228. #edge side bleed
  229. #original idea by "colours", this function mod by A.SONY
  230. ##########
  231. function edgesidebleed(clip input, float "w32", float "w16", float "w8", float "w4", float "w2", bool "bleed", bool "chroma", int "mode") {
  232. w2     = Default(w2,     0.001)
  233. w4     = Default(w4,     0.055)
  234. w8     = Default(w8,     0.015)
  235. w16    = Default(w16,     0.02)
  236. w32    = Default(w32,    0.001)
  237. chroma = default(chroma, false)
  238. mode   = default(mode,       0)
  239. avs26  = !(VersionNumber() < 2.6)
  240. bleed  = default(bleed, avs26)
  241.  
  242. sisyuy2 = input.isyuy2()
  243. input = sisyuy2 && avs26 ? input.converttoyv16() : input
  244.  
  245. sisphbd = !chroma ? AvsPlusVersionNumber > 2294 : nop()
  246. sislumaonly = !chroma ? sisphbd ? input.isy() : !avs26 ? true : input.isy8() : nop()
  247. !chroma ? sislumaonly ? input : sisphbd ? input.converttoy() : input.converttoy8() : input
  248. w = bleed ?  Width() : nop()
  249. h = bleed ? Height() : nop()
  250.  
  251. shift32 = w32!=0 ? !bleed ? Crop(Width()-32,0,32,0,true).StackHorizontal(Crop(0,0,-32,0,true)) : nop() : nop()
  252. shift32 = w32!=0 ? bleed ? BicubicResize (w / 32, h, 1, 0,src_left=-32).BicubicResize (w, h, 1, 0) : shift32 : nop()
  253. 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)
  254. shift16 = w16!=0 ? !bleed ? Crop(Width()-16,0,16,0,true).StackHorizontal(Crop(0,0,-16,0,true)) : nop() : nop()
  255. shift16 = w16!=0 ? bleed ? BicubicResize (w / 16, h, 1, 0,src_left=-16).BicubicResize (w, h, 1, 0) : shift16 : nop()
  256. 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)
  257. shift8 = w8!=0 ? !bleed ? Crop(Width()-8,0,8,0,true).StackHorizontal(Crop(0,0,-8,0,true)) : nop() : nop()
  258. shift8 = w8!=0 ? bleed ? BicubicResize (w / 8, h, 1, 0,src_left=-8).BicubicResize (w, h, 1, 0) : shift8 : nop()
  259. 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)
  260. shift4 = w4!=0 ? !bleed ? Crop(Width()-4,0,4,0,true).StackHorizontal(Crop(0,0,-4,0,true)) : nop() : nop()
  261. shift4 = w4!=0 ? bleed ? BicubicResize (w / 4, h, 1, 0,src_left=-4).BicubicResize (w, h, 1, 0) : shift4 : nop()
  262. 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)
  263. shift2 = w2!=0 ? !bleed ? Crop(Width()-2,0,2,0,true).StackHorizontal(Crop(0,0,-2,0,true)) : nop() : nop()
  264. shift2 = w2!=0 ? bleed ? BicubicResize (w / 2, h, 1, 0,src_left=-2).BicubicResize (w, h, 1, 0) : shift2 : nop()
  265. 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)
  266.  
  267. !chroma ? sislumaonly ? last : sisphbd ? CombinePlanes(last,input,planes="YUV",sample_clip=input) : ytouv(input.utoy8(),input.vtoy8(),last) : last
  268. sisyuy2 ? converttoyuy2() : last
  269. }
  270.  
  271.  
  272. ##########
  273. #chroma edge side bleed (aka pink artifacts) fix, by A.SONY
  274. ##########
  275. function chromasidebleed(clip input, int "diameter", float "sDev", float "iDev", float "cs", bool "bic", string "cplace", bool "d2", int "kernS", int "kernI", int "resType")
  276. {
  277. diameter  = default (diameter,    11)
  278. sDev      = default (sDev,      31.1)
  279. iDev      = default (iDev,      15.1)
  280. d2        = default (d2,        true)
  281. kernS     = default (kernS,        8)
  282. kernI     = default (kernI,        3)
  283. restype   = default (restype,      0)
  284. bic       = default (bic,       true)
  285. cplace    = default (cplace, "MPEG2")
  286.  
  287. avs25   = VersionNumber() < 2.6
  288. sisyuy2 = input.isyuy2()
  289. input = sisyuy2 && !avs25 ? input.converttoyv16() : input
  290.  
  291. chroma = false
  292.  
  293. sisphbd = AvsPlusVersionNumber > 2294
  294. sisfullchr = sisphbd ? input.is444() : avs25 ? false : input.isyv24()
  295. sislumaonly = sisphbd ? input.isy() : avs25 ? false : input.isy8()
  296. Assert(!sislumaonly, "why you use this on no chroma clip?!")
  297.  
  298. y=avs25 ? input : sisphbd ? input.converttoy() : input.converttoy8()
  299. u=avs25 ? input.utoy() : input.utoy8()
  300. v=avs25 ? input.vtoy() : input.vtoy8()
  301. cshift = (cplace == "MPEG1" && IsYV12(input)) || IsYV24(input) ? 0 : -0.5
  302. ych = sisfullchr ? y : bic ? input.BicubicResize(u.width(), v.height(), 1, 0, src_left=cshift) : input.BilinearResize(u.width(), v.height(), src_left=cshift)
  303. 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)
  304. 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)
  305. YToUV(u,v,y)
  306. sisyuy2 ? converttoyuy2() : last
  307. }
  308.  
  309.  
  310. #########
  311. # Comb wrapper function
  312.  
  313. function sComb(clip clip, bool "DeCrawing", bool "DeRainbowing", int "LRepair", int "CRepair", bool "ntsccombmask") {
  314. DeCrawing = default (DeCrawing, true)
  315. ntsccombmask = default (ntsccombmask, false)
  316. DeRainbowing = default (DeRainbowing, true)
  317. LRepair = default (LRepair, ntsccombmask ? 7 : DeCrawing ? 2 : 0)
  318. CRepair = default (CRepair, ntsccombmask ? 5 : 0)
  319. sisphbd = AvsPlusVersionNumber > 2294
  320. avs26   = !(VersionNumber() < 2.6)
  321. clip
  322. sis422  = avs26 ? isyv16() : false
  323. Assert((isyv12 || sis422), "sComb: only yv12 and yv16 is supported")
  324. prefil = ntsccombmask ? (VersionNumber() < 2.60) && isyv12(input) ? last.HQdn3d().FFT3DFilter().tweak(sat=1.5) : last.HQdn3d_2().FFT3DFilter().tweak(sat=1.5) : nop()
  325. iqchr  = ntsccombmask ? prefil.SwapUV().Tweak(hue=-33) : nop()
  326. I = ntsccombmask ? sisphbd ? iqchr.ExtractU().sypixsub(2,2) : avs26 ? iqchr.utoy8().sypixsub(2,2) : iqchr.utoy().sypixsub(2,2) : nop()
  327. Q = ntsccombmask ? sisphbd ? iqchr.ExtractV().sypixsub(2,2) : avs26 ? iqchr.Vtoy8().sypixsub(2,2) : iqchr.Vtoy().sypixsub(2,2) : nop()
  328. m = ntsccombmask ? ytouv(i,q,prefil).sypixsub(2).mt_edge("min/max", 0, 15, 0, 10,u=3,v=3).mt_expand(128,128,y=2,u=3,v=3).mt_expand(128,mode="horizontal",u=2,v=2).mt_inpand(128,mode="horizontal",u=2,v=2) : nop()
  329. mU = ntsccombmask ? sisphbd ? m.ExtractU() : avs26 ? m.utoy8() : m.utoy() : nop()
  330. mV = ntsccombmask ? sisphbd ? m.ExtractV() : avs26 ? m.vtoy8() : m.vtoy() : nop()
  331. y=ntsccombmask ? Overlay(mu,mv,mode="add").Spline36Resize(width(),Height(),0.5) : nop()
  332. m = ntsccombmask ? sisphbd ? m.ConvertToY() : avs26 ? m.ConvertToY8() : m : nop()
  333. uv=ntsccombmask ? m.Spline36Resize(mu.width(),mu.Height(),-0.5) : nop()
  334. m=ntsccombmask ? ytouv(uv,uv,y) : nop()
  335.  
  336. DeRainbowing && ntsccombmask ? TComb(mode=1) : last
  337. SeparateFields()
  338. oriFiel=last
  339. DeCrawing ? DDComb(static=true,strong=true,checkmate=ntsccombmask) : last
  340. DeRainbowing ? ASTDRmc(edgemprefil=oriFiel,chroma=true, nomask=ntsccombmask) : last
  341. ntsccombmask ? mt_merge(oriFiel,last,m.SeparateFields(),u=3,v=3) : last
  342. Weave()
  343. LRepair==0 && CRepair==0 ? last : last.Repair(clip,LRepair,CRepair)
  344. }
  345.  
  346.  
  347. # Padding by Didée
  348. function Padding(clip c, int left, int top, int right, int bottom, int "threads")
  349. {
  350. threads = Default(threads, 0)
  351. w = c.width()
  352. h = c.height()
  353. threads!=1 ? Eval("try { pointresizemt(w+left+right, h+top+bottom, -left, -top, w+left+right, h+top+bottom+0.0001, threads=threads) } catch(error_msg) { c.pointresize( w+left+right, h+top+bottom, -left, -top, w+left+right, h+top+bottom+0.0001 ) }") : c.pointresize( w+left+right, h+top+bottom, -left, -top, w+left+right, h+top+bottom+0.0001 )
  354. }
  355.  
  356.  
  357. # filtering with some borders by A.SONY
  358. Function filtering_wsb (clip src, string "filter", int "oneborder", clip "linesm", clip "clip4lines", int "mmy", bool "mmLuma", int "mmuv", bool "minflate", int "bordertype")
  359. {
  360. oneborder = Default (oneborder,   2)
  361. mmLuma    = Default (mmLuma,  false)
  362. minflate  = Default (minflate, true)
  363. mmuv      = Default (mmuv, mmLuma ? 3 : 2)
  364. mmy       = Default (mmy,         3)
  365. bordertyp = Default (bordertype,  1)
  366. filter    = Default (filter, defined(linesm) ? "yahr3.TBilateral(5,5,0.9,0.9,5,5,0.7,chroma=false)" : \
  367.                                              """FineDehaloanalog(exdehalo="VHSHaloremover(2,2,200,100,0.5).yahr2(32)")""")
  368. minf = defined(linesm) ? minflate ? linesm.mt_inflate(155,155,u=mmLuma || mmuv!=3 ? 1 : 3 ,v=mmLuma || mmuv!=3 ? 1 : 3) : linesm : nop()
  369.  
  370. src
  371. bordertyp==0 ? Padding(oneborder,oneborder,oneborder,oneborder) : AddBorders(oneborder,oneborder,oneborder,oneborder,bordertyp==1 ? undefined : bordertyp==2 ? color_white : bordertyp)
  372. eval(filter)
  373. Crop(oneborder, oneborder, -oneborder, -oneborder, align=true)
  374.  
  375. defined(linesm) ? mt_Merge(last, defined(clip4lines) ? clip4lines : src, minf,y=mmy,luma=mmLuma,u=mmuv,v=mmuv) : last
  376. }
  377.  
  378.  
  379. ###############
  380. # Hqdn3d_2
  381.  
  382. function Hqdn3d_2(clip clip, float "ls", float "cs", float "lt", float "ct", int "UV", bool "lsb", bool "lsb_in", bool "i16", int "Y") {
  383. avs26     = !(VersionNumber() < 2.60)
  384. sisphbd   = AvsPlusVersionNumber > 2294
  385. cs        = default (cs              , 3.0)
  386. ct        = default (ct              , 4.5)
  387. UV        = default (UV               , 3)
  388. Y         = default (Y                , 3)
  389. UV        = avs26 ? isY8(clip) ? 1 : UV : UV
  390. lsb_in    = default (lsb_in          , false)
  391. i16       = default (i16             , false)
  392. lsb_out   = default (lsb             , lsb_in)
  393.  
  394. i16b      = sisphbd ? clip.BitsPerComponent() > 8 : (lsb_out || i16 || lsb_in)
  395.  
  396. isrgb(clip) ? Assert(!i16b, "Hqdn3d_2: no 16bit for RGB, but you can use some trickes for that (see dither doc)") : nop()
  397. Assert(!(sisphbd && i16b && clip.BitsPerComponent() != 16), "Hqdn3d_2: only HBD 16bit Supported")
  398. Assert(!(sisphbd && i16b && (i16 || lsb_in || lsb_out)), "Hqdn3d_2: lsb hack is not Compatible with HBD")
  399.  
  400. yString = sisphbd ? "Y" : !avs26 ? "yv12" : "Y8"
  401.  
  402. pclip   = isYUV(clip) && i16b && isYUY2(clip) && avs26 ? clip.ConvertToYV16() : clip
  403.  
  404. i16clip = isYUV(clip) ? i16b ? sisphbd ? pclip.ConvertToDoubleWidth() : lsb_in ? pclip.Bitdepth(from=88, to=16) : i16 ? clip : pclip.Bitdepth(from=8, to=16) : nop() : nop()
  405.  
  406.     yclip = isYUV(clip) ? !avs26 ? i16b ? i16clip : clip : i16b ? ConvertToY8(i16clip) : ConvertToY8(clip) : nop()
  407.     u = isYUV(clip) && UV != 1 ? !avs26 ? i16b ? UToY(i16clip) : UToY(clip) : i16b ? UToY8(i16clip) : UToY8(clip) : nop()
  408.     v = isYUV(clip) && UV != 1 ? !avs26 ? i16b ? VToY(i16clip) : VToY(clip) : i16b ? VToY8(i16clip) : VToY8(clip) : nop()
  409.     yclip = isYUV(clip) ? Y == 3 ? !avs26 ? isclip(i16clip) ? yclip.converttoyv12().Hqdn3d16Y(sp=ls, tp=lt) : yclip.converttoyv12().Hqdn3dY(sp=ls, tp=lt) : isclip(i16clip) ? yclip.Hqdn3d16Y(sp=ls, tp=lt) : yclip.Hqdn3dY(sp=ls, tp=lt) : yclip : nop()
  410.     u = isYUV(clip) ? UV == 3 ? !avs26 ? 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()
  411.     v = isYUV(clip) ? UV == 3 ? !avs26 ? 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()
  412.     isYUV(clip) ? !avs26 && isYUY2(clip) ? UV == 1 ? yclip : YToUV(u.converttoyuy2(),v.converttoyuy2(),yclip.converttoyuy2()) : UV == 1 ? yclip : YToUV(u,v,yclip) : nop()
  413.         isYUV(clip) ? i16b && !i16 && lsb_out ? Bitdepth(from=16, to=88) : last : last
  414.     isYUV(clip) ? sisphbd && i16b ? ConvertFromDoubleWidth() : !i16b || lsb_out || i16 ? last : Bitdepth(from=16, to=8) : last
  415.     isYUY2(clip) ? !avs26 ? last : converttoyuy2() : last
  416.  
  417.     A = isrgb32(clip) ? clip.ShowAlpha(yString) : nop()
  418.     r = isrgb(clip) ? clip.ShowRed(yString) : nop()
  419.     g = isrgb(clip) ? clip.ShowGreen(yString) : nop()
  420.     b = isrgb(clip) ? clip.ShowBlue(yString) : nop()
  421.     A = isrgb32(clip) ? A.Hqdn3dY(sp=ls, tp=lt) : nop()
  422.     r = isrgb(clip) ? r.Hqdn3dY(sp=ls, tp=lt) : nop()
  423.     g = isrgb(clip) ? g.Hqdn3dY(sp=ls, tp=lt) : nop()
  424.     b = isrgb(clip) ? b.Hqdn3dY(sp=ls, tp=lt) : nop()
  425.     isYUV(clip) ? last : isrgb32(clip) ? MergeARGB(A,r,g,b) : MergeRGB(r,g,b,"RGB24")
  426. }
  427.  
  428.  
  429. ###############
  430. # F3KDB_3
  431.  
  432. function F3KDB_3(clip clip, int "range", int "Y", int "Cb", int "Cr", \
  433.         int "grainY", int "grainC", int "sample_mode", int "seed", \
  434.         bool "blur_first", bool "dynamic_grain", int "opt", bool "mt", \
  435.         int "dither_algo", bool "keep_tv_range", int "input_mode", \
  436.         int "input_depth", int "output_mode", int "output_depth", \
  437.         int "random_algo_ref", int "random_algo_grain", \
  438.         float "random_param_ref", float "random_param_grain", bool "chroma", int "pass", bool "lastpassgrain", bool "masking", float "mask_thr", int "mask_radius", bool "lastpassmask") {
  439. avs26     = !(VersionNumber() < 2.60)
  440. sample_mode = default (sample_mode, 4)
  441. pass = default (pass, sample_mode==4 ? 2 : 1)
  442. isgrYd = defined(grainY)
  443. lpgra  = default (lastpassgrain, !isgrYd && pass > 1)
  444. lpmsk  = default (lastpassmask, lpgra && pass > 1)
  445. grainY = isgrYd && !lpgra ? grainY : pass > 1 ? 0 : pass < 2 && lpgra ? undefined : 0
  446. grainC = default (grainC, grainY)
  447. sisphbd   = AvsPlusVersionNumber > 2294
  448. sishbd  = sisphbd ? clip.BitsPerComponent() > 8 : false
  449. input_mode  = default (input_mode, 0)
  450. output_mode = default (output_mode, sishbd ? 3 : input_mode)
  451. sm4lsb = output_mode != 0 && output_mode != 3 && sample_mode == 4
  452. sm4i16 = sm4lsb && output_mode == 2
  453. output_mode = sm4i16 ? 1 : output_mode
  454. sislumaonly = sisphbd ? clip.isy() : avs26 ? clip.isy8() : true
  455. chroma      = default (chroma, true)
  456. masking      = default (masking, true)
  457. Assert(!(!chroma && output_mode != input_mode && !(sishbd && output_mode == 3 && input_mode == 0)), "F3KDB_3: chroma=false don't work if output_mode different from input_mode")
  458. Assert(!(chroma && !avs26), "F3KDB_3: chroma=false don't work in avs 2.5")
  459. Assert(masking && output_mode==2 ? sample_mode==4 : true, "F3KDB_3: when using output_mode=2 masking only work with sample_mode=4")
  460. sisyuy2 = clip.isyuy2()
  461. pclip   = sislumaonly ? clip : !chroma ? sisphbd ? clip.converttoy() : clip.converttoy8() : clip
  462. pclip   = chroma && sisyuy2 && avs26 ? clip.ConvertToYV16() : pclip
  463.  
  464. sample_mode != 4 ? Eval("""
  465. i_mode  = sishbd ? 2 : input_mode
  466. o_mode = output_mode == 3 ? 2 : output_mode
  467. i_depth   = sishbd ? clip.BitsPerComponent() : defined(input_depth) ? input_depth : 8
  468.  
  469. Assert(!(sishbd && (input_mode>0)), "F3KDB_3: lsb/interleaved hack is not Compatible with HBD")
  470. Assert(isYUV(clip), "F3KDB_3: YUV only")
  471. noSquarechr = sisphbd ? (is422(clip) || isyv411(clip)) : avs26 ? (isyv16(clip) || isyv411(clip)) : false
  472. Assert(!(sample_mode==3 && chroma && (sisyuy2 || noSquarechr)), "F3KDB_3: Square chroma only for sample_mode > 2")
  473.  
  474. o_depth     = defined(output_depth) ? output_depth : i_depth
  475. smode       = sample_mode > 2 ? 1 : sample_mode
  476.  
  477. pclip2   = sample_mode == 3 ? pclip.sTryFTurnLeft(chroma,input_mode==2 ? 16 : input_mode==1 ? 88 : 8) : pclip
  478.  
  479. i16clip = sishbd ? i_depth != 8 ? pclip2.convertbits(16,false).ConvertToDoubleWidth() : pclip2.ConvertToDoubleWidth() : pclip2
  480.  
  481. F3KDB_clip = i16clip.F3KDB(range, Y, Cb, Cr, \
  482.         grainY, grainC, smode, seed, \
  483.         blur_first, dynamic_grain, opt, mt, \
  484.         dither_algo, keep_tv_range, i_mode, \
  485.         i_depth, o_mode, o_depth, \
  486.         random_algo_ref, random_algo_grain, \
  487.         random_param_ref, random_param_grain)
  488.  
  489. F3KDB_clip = output_mode == 3 && sishbd ? F3KDB_clip.ConvertFromDoubleWidth(o_depth) : F3KDB_clip
  490.  
  491. sample_mode == 3 ? F3KDB_clip.sTryFTurnRight(chroma,output_mode==2 ? 16 : output_mode==1 ? 88 : 8) : F3KDB_clip
  492.  
  493. """) : sm4lsb ? Dither_merge16(pclip.F3KDB_3(range, Y, Cb, Cr, \
  494.         grainY, grainC, 1, seed, \
  495.         blur_first, dynamic_grain, opt, mt, \
  496.         dither_algo, keep_tv_range, input_mode, \
  497.         input_depth, 1, output_depth, \
  498.         random_algo_ref, random_algo_grain, \
  499.         random_param_ref, random_param_grain, chroma, 1, pass == 1 ? lpgra : false, false),pclip.F3KDB_3(range, Y, Cb, Cr, \
  500.         grainY, grainC, 3, seed, \
  501.         blur_first, dynamic_grain, opt, mt, \
  502.         dither_algo, keep_tv_range, input_mode, \
  503.         input_depth, 1, output_depth, \
  504.         random_algo_ref, random_algo_grain, \
  505.         random_param_ref, random_param_grain, chroma, 1, pass == 1 ? lpgra : false, false),BlankClip(input_mode != 1 ? pclip.Bitdepth(from=input_mode == 2 ? 16 : 8, to=88) : pclip,color_yuv=color_gray).BitdepthMsb(bitdepth=88).Bitdepth(from=8, to=88),luma=chroma) : \
  506.         Merge(pclip.F3KDB_3(range, Y, Cb, Cr, \
  507.         grainY, grainC, 1, seed, \
  508.         blur_first, dynamic_grain, opt, mt, \
  509.         dither_algo, keep_tv_range, input_mode, \
  510.         input_depth, output_mode, output_depth, \
  511.         random_algo_ref, random_algo_grain, \
  512.         random_param_ref, random_param_grain, chroma, 1, pass == 1 ? lpgra : false, false),pclip.F3KDB_3(range, Y, Cb, Cr, \
  513.         grainY, grainC, 3, seed, \
  514.         blur_first, dynamic_grain, opt, mt, \
  515.         dither_algo, keep_tv_range, input_mode, \
  516.         input_depth, output_mode, output_depth, \
  517.         random_algo_ref, random_algo_grain, \
  518.         random_param_ref, random_param_grain, chroma, 1, pass == 1 ? lpgra : false, false))
  519.  
  520. masking && (lpmsk ? pass < 2 : true) ? sm4i16 || output_mode == 1 ? Dither_merge16_8(last,input_mode!=1 ? pclip.Bitdepth(from=input_mode==2 ? 16 : 8, to=88) : pclip,input_mode!=0 ? pclip.Bitdepth(from=input_mode==2 ? 16 : 88, to=8).F3KDB_3_masking(mask_thr, mask_radius) : pclip.F3KDB_3_masking(mask_thr, mask_radius), luma=chroma) : mt_merge(last,pclip,pclip.F3KDB_3_masking(mask_thr, mask_radius), u=chroma ? 3 : 2, v=chroma ? 3 : 2, luma=chroma) : last
  521. output_mode = sm4i16 ? 2 : output_mode
  522. sm4i16 ? Bitdepth(from=88, to=16) : last
  523.  
  524.         pass > 1 ? last.F3KDB_3(range, Y, Cb, Cr, \
  525.         grainY, grainC, sample_mode, seed, \
  526.         blur_first, dynamic_grain, opt, mt, \
  527.         dither_algo, keep_tv_range, input_mode, \
  528.         input_depth, output_mode, output_depth, \
  529.         random_algo_ref, random_algo_grain, \
  530.         random_param_ref, random_param_grain, chroma, pass - 1, lpgra, masking, mask_thr, mask_radius) : last
  531.  
  532. yuy26 = sisyuy2 && avs26
  533. clpp = yuy26 && !chroma && !sislumaonly ? clip.ConvertToYV16() : clip
  534. sislumaonly ? last : !chroma ? sisphbd ? CombinePlanes(last,clpp,planes="YUV",sample_clip=clpp) : ytouv(clpp.utoy8(),clpp.vtoy8(),last) : last
  535.  
  536. yuy26 ? ConvertToYUY2() : last
  537.  
  538. }
  539.  
  540.  
  541. function F3KDB_3_masking(clip c, float "mask_thr", int "mask_radius")
  542. {
  543. avs26     = !(VersionNumber() < 2.60)
  544. mthr = default(mask_thr, 2)
  545. mrad = default(mask_radius, 2)
  546. Assert(mrad > 0, "F3KDB_3_masking: mask_radius must be more than 0")
  547.  
  548. thr_lo = string(max(mthr * 0.75, 1) - 0.0001)
  549. thr_hi = string(max(mthr,         1) + 0.0001)
  550.  
  551. c
  552. mrad > 1 ? avs26 ? mt_lutxy (mt_expand_multi (sw=mrad, sh=mrad, mode="ellipse"), mt_inpand_multi (sw=mrad, sh=mrad, mode="ellipse"), "x y -",clamp_float=true, use_expr=1) : mt_lutxy (mt_expand_multi (sw=mrad, sh=mrad, mode="ellipse"), mt_inpand_multi (sw=mrad, sh=mrad, mode="ellipse"), "x y -") : mt_edge (mode="min/max", thY1=0, thY2=255)
  553. avs26 ? mt_lut (expr="x "+thr_lo+" scalef - "+thr_hi+" scalef "+thr_lo+" scalef - / 0 1 clip range_max *", use_expr=2) : mt_lut (expr="x "+thr_lo+" - "+thr_hi+" "+thr_lo+" - / 0 1 clip 255 *")
  554. removegrain (22, -1)
  555. mrad > 1 ? removegrain (11, -1) : last
  556. mrad > 2 ? removegrain (20, -1) : last
  557. }
  558.  
  559.  
  560. ###############
  561. #motion adaptive by A.SONY
  562.  
  563. 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", bool "repblend", val "dslow", bool "qtgmc_n16") {
  564.  
  565. sisphbd = AvsPlusVersionNumber > 2294
  566. qtgmc_lsb = default(qtgmc_lsb, sisphbd ? false : true)
  567. qtgmc_n16 = default(qtgmc_n16, sisphbd ? input.BitsPerComponent() > 8 ? false : true : false)
  568. repblend  = default(repblend, false)
  569.  
  570. defined(filter) ? Assert((Isclip(filter) || IsString(filter)),        "'filter' only accepts clip or string") : nop()
  571.  
  572. Str             = default (Str, 1.5)
  573. exfilclp        = isclip(filter)
  574. sisbob          = round(framerate(input))==60 || framerate(input)==50
  575. tr2             = default (tr2, sisbob ? 3 : 1)
  576. usedaa3mod      = default (usedaa3mod, exfilclp ? 0 : sisbob ? 1 : 2)
  577.  
  578. infiltr = usedaa3mod == 1 ? input.daa3mod(dslow) : input
  579. filclip = defined(filter) ? exfilclp ? filter : eval("infiltr." + filter) : infiltr.QTGMC(InputType=1, tr0=0, tr1=sisbob ? undefined : 1, tr2=tr2, lsb=qtgmc_lsb, n16=qtgmc_n16, rep1=sisbob ? undefined : 11, rep2=sisbob ? undefined : 11, Sharpness=0.0, TV_range=TV_range, Str=Str, Amp=Amp)
  580. filclip = usedaa3mod == 2 ? filclip.daa3mod(dslow) : filclip
  581.  
  582. 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
  583. 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)
  584. repblend ? isyuy2(input) && VersionNumber() < 2.60 ? Interleaved2Planar() : isyuy2(input) ? converttoyv16() : last : last
  585. repblend ? isyuy2(input) && VersionNumber() < 2.60 ? Repair(Repair(input.Interleaved2Planar(),16,Planar=true),12,Planar=true).Planar2Interleaved() : isyuy2(input) ? Repair(Repair(input.converttoyv16(),16),12).converttoyuy2() : Repair(Repair(input,16),12) : last
  586. }
  587.  
  588.  
  589. ###############
  590. #smam_mask
  591. # if input is YUY2 the output will be Planar YUY2 in avs 2.5 and yv16 in avs 2.6
  592.  
  593. function smam_mask(clip input, clip "prefilter", int "pel", int "blksize", bool "chroma", int "dct") {
  594.     ox = input.width()
  595.     oy = input.height()
  596.     HD = (ox > 1099 || oy > 599)
  597.     sisbob  = round(framerate(input))==60 || framerate(input)==50
  598.     pel = default( pel,     HD ? 1 : 2 )
  599.     dct = default( dct, sisbob ? 0 : 2 )
  600.     blksize = default(blksize,HD ? sisbob ? 8 : 16 : sisbob ? 4 : 8)
  601. preclip= defined(prefilter) ? prefilter : (VersionNumber() < 2.60) && isyv12(input) ? input.HQdn3d().FFT3DFilter() : input.HQdn3d_2().FFT3DFilter()
  602. sup    = preclip.MSuper(pel=pel,sharp=1)
  603. fv1    = sup.MAnalyse(isb=false,delta=1,DCT=dct,Truemotion=false,blksize=blksize,chroma=chroma)
  604. fv2    = sup.MAnalyse(isb=true,delta=1,DCT=dct,Truemotion=true,blksize=blksize,chroma=chroma)
  605.  
  606. momask1 = input.MMask(fv1, kind = 1, ml=2)
  607. momask2 = input.MMask(fv2, kind = 1, ml=3)
  608. momask1 =isyuy2(input) && VersionNumber() < 2.60 ? momask1.Interleaved2Planar() : isyuy2(input) ? momask1.converttoyv16() : momask1
  609. momask2 =isyuy2(input) && VersionNumber() < 2.60 ? momask2.Interleaved2Planar() : isyuy2(input) ? momask2.converttoyv16() : momask2
  610. mt_average(momask1,momask2,u=3,v=3)
  611. }
  612.  
  613.  
  614. ##############
  615. # sanimebob by A.SONY
  616.  
  617. function sanimebob(clip i, val "useqtgmc", val "bobpresmooth", float "Str", float "Amp", bool "TV_range", bool "qtgmc_lsb", bool "usedaa3mod", bool "usesmam", int "tr2", bool "repblend", val "dslow", bool "qtgmc_n16", bool "nnrep", bool "nnedi3pad") {
  618.  
  619. sisphbd = AvsPlusVersionNumber > 2294
  620. qtgmc_lsb = default(qtgmc_lsb, sisphbd ? false : true)
  621. qtgmc_n16 = default(qtgmc_n16, sisphbd ? i.BitsPerComponent() > 8 ? false : true : false)
  622.  
  623. useq                  = default (useqtgmc,                                                                                                           0)
  624. Stringuseq        = IsString(useq                                                                                                                 )
  625. bobpresmbool = default (isbool(bobpresmooth) ? bobpresmooth : !Stringuseq ? (useq==8 || defined(bobpresmooth)) : defined(bobpresmooth), false)
  626. Str                     = default (Str,                                                                                                              1.5)
  627. tr2                     = default (tr2,                                                                                !Stringuseq ? useq==8 ? 3 : 1 : 1)
  628. usedaa3mod   = default (usedaa3mod,                                            !Stringuseq ? useq==4 || useq==5 || useq==7 || useq==9 : false)
  629. usesmam        = default (usesmam,                                                       !Stringuseq ? !(useq==0 || useq==1 || useq==8) : false)
  630. nnrep               = default (nnrep, false)
  631. nnedi3pad       = default (nnedi3pad, false)
  632.  
  633. Assert(Isint(useq) || Stringuseq, "'useqtgmc' only accepts int or string")
  634.  
  635. prefiltered_i   = defined(bobpresmooth) ? isclip(bobpresmooth) ? bobpresmooth : IsString(bobpresmooth) ? Eval("i." + bobpresmooth) : undefined() : undefined()
  636.  
  637. nonyuy2=!(VersionNumber() < 2.6) && i.isyuy2()
  638. iforbob   = bobpresmbool ? defined(prefiltered_i) ? prefiltered_i : i.QTGMC_bob(0,0.5).nonyuy2clipin(nonyuy2).reduceflicker(strength=2).nonyuy2clipout(nonyuy2).interlaced60or50(BFF=!(GetParity(i))) : i
  639. 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
  640.  
  641. pyi       = i.Padding(0,4,0,4)
  642. pni       = nnedi3pad ? iforbob.Padding(2,4,2,4).nnedi3(-2).crop(2,4,-2,-4,true) : iforbob.nnedi3(-2)
  643. pei       = i.eedi3(-2,sclip=pni)
  644. pei       = nnrep ? isyuy2(i) ? pei.Interleaved2Planar().Repair(pni.Interleaved2Planar(),9,Planar=true).Planar2Interleaved().Padding(0,4,0,4) : pei.Repair(pni,9).Padding(0,4,0,4) : pei.Padding(0,4,0,4)
  645.  
  646. ymodclip  = VersionNumber() < 2.60 ? pyi.yadifmod(mode=3, edeint=pei).crop(0,4,-0,-4) : pyi.nonyuy2clipin(nonyuy2).yadifmod2(mode=3, edeint=pei.nonyuy2clipin(nonyuy2)).crop(0,4,-0,-4)
  647. yadifclip = isyuy2(i) && VersionNumber() < 2.60 ? ymodclip.Interleaved2Planar().Repair(i.TDeint(1,emask=iforbob.tmm2_ortmm1(1)).Interleaved2Planar(),Planar=true).Planar2Interleaved() : \
  648.                                                   ymodclip.Repair(i.TDeint(1,emask=iforbob.tmm2_ortmm1(1)).nonyuy2clipin(nonyuy2)).nonyuy2clipout(nonyuy2)
  649.  
  650. # for custom qtgmc, don't forget to put the input clip like this:- iforbob.QTGMC(... or yadifclip.QTGMC(... or i.QTGMC(...
  651. QTGMCclip = isstring(useq) ? eval(useq) : \
  652.                              useq==0 ? yadifclip : \
  653.        useq==1 || useq==2 || useq==4 ? iforbob.QTGMC(SourceMatch=3, Lossless=2, EdiExt=yadifclip, tr0=1, tr1=1, tr2=tr2, lsb=qtgmc_lsb, n16=qtgmc_n16, rep0=11, rep1=11, rep2=11, Sharpness=0.1, TV_range=TV_range, Str=Str, Amp=Amp) : \
  654.                   useq==3 || useq==5 ? yadifclip.QTGMC(InputType=1, tr0=1, tr1=1, tr2=tr2, lsb=qtgmc_lsb, n16=qtgmc_n16, rep0=11, rep1=11, rep2=11, Sharpness=0.1, TV_range=TV_range, Str=Str, Amp=Amp) : \
  655.                   useq==6 || useq==7 ? yadifclip.QTGMC(InputType=1, tr0=0, lsb=qtgmc_lsb, n16=qtgmc_n16, Sharpness=usedaa3mod ? 0.0 : 0.1, TV_range=TV_range, Str=Str, Amp=Amp) : \
  656.                                        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, n16=qtgmc_n16, Sharpness=0.0, TV_range=TV_range, Str=Str, Amp=Amp)
  657.  
  658. daa3mclip = usedaa3mod ? QTGMCclip.daa3mod(dslow) : QTGMCclip
  659.  
  660. !usesmam ? daa3mclip : \
  661.            yadifclip.smam(filter=daa3mclip,repblend=repblend)
  662. }
  663.  
  664.  
  665. ##############
  666. # sypixsub 1.3, Subtracting one luma pixel with the neighboring pixel on the left or the top
  667. Function sypixsub(clip clip, int "left", int "top", bool "expr")
  668. {
  669. left=Default(left, 1) #set the "left" to 2 will make it 2 pass since the subtract mode in overlay ignore the negative result of subtracting, set it to -1 and will show the negative result only
  670. top=Default(top, 0) #same as "left" parameter
  671. expr=Default(expr, true)
  672. sisphbd = AvsPlusVersionNumber > 2294
  673. expr=sisphbd ? expr : false
  674.  
  675. Assert(!(left>2 || left<-1 || top>2 || top<-1), "sypixsub: left and top modes must be between -1 to 2")
  676. Assert(!(left==0 && top==0), "sypixsub: why you use sypixsub then?!!!!")
  677.  
  678. sislumaonly = sisphbd ? clip.isy() : VersionNumber() < 2.6 ? true : clip.isy8()
  679. c= sislumaonly ? clip : sisphbd ? clip.converttoy() : clip.converttoy8()
  680. lexp = left==-1 ? "x[-1,0] x -" : left==0 ? "" : "x x[-1,0] -" + string(left>1 ? " abs" : "")
  681. texp = top==-1 ? " x[0,-1] x -" : top==0 ? "" : " x x[0,-1] -" + string(top>1 ? " abs" : "")
  682. expr ? c.Expr(lexp + texp + (top != 0 && left != 0 ? " +" : "")) : eval("""
  683. c
  684. left != 0 ? PointResize(Width(), Height(), src_left=-1) : last
  685. leftsub1 = left != 0 ? overlay(c,last,mode="Subtract") : last
  686. leftsub2 = left == -1 || left > 1 ? overlay(last,c,mode="Subtract") : nop()
  687. left > 1 ? overlay(leftsub1,leftsub2,mode="add") : left == -1 ? leftsub2 : leftsub1
  688. h=last
  689. top != 0 ? c : last
  690. top != 0 ? PointResize(Width(), Height(), src_top=-1) : last
  691. topsub1 = top != 0 ? overlay(c,last,mode="Subtract") : last
  692. topsub2 = top == -1 || top > 1 ? overlay(last,c,mode="Subtract") : nop()
  693. top > 1 ? overlay(topsub1,topsub2,mode="add") : top == -1 ? topsub2 : topsub1
  694.  
  695. top != 0 && left != 0 ? overlay(h,last,mode="add") : last
  696. """)
  697. sislumaonly ? last : sisphbd ? CombinePlanes(last,clip,planes="YUV",sample_clip=clip) : YToUV(clip.UToY8(),clip.VToY8(),last)
  698. }
  699.  
  700. function sTryFTurnRight(clip c, bool "chroma", int "bitdepth") {
  701. bitdepth=Default(bitdepth, 8)
  702. bitdepth==8 ? eval("""
  703. try {
  704.    IsAvsPlus ? dontdoft : nop()
  705.    return c.FTurnRight(chroma=chroma)
  706.    }
  707. catch(err_msg) {
  708.    return c.TurnRight()
  709.    }
  710.    """) : BitdepthMsbLsb(c.BitdepthMsb(bitdepth=bitdepth).sTryFTurnRight(chroma), c.BitdepthLsb(bitdepth=bitdepth).sTryFTurnRight(chroma), bitdepth=bitdepth)
  711. }
  712.  
  713. function sTryFTurnLeft(clip c, bool "chroma", int "bitdepth") {
  714. bitdepth=Default(bitdepth, 8)
  715. bitdepth==8 ? eval("""
  716. try {
  717.    IsAvsPlus ? dontdoft : nop()
  718.    return c.FTurnLeft(chroma=chroma)
  719.    }
  720. catch(err_msg) {
  721.    return c.TurnLeft()
  722.    }
  723.    """) : BitdepthMsbLsb(c.BitdepthMsb(bitdepth=bitdepth).sTryFTurnLeft(chroma), c.BitdepthLsb(bitdepth=bitdepth).sTryFTurnLeft(chroma), bitdepth=bitdepth)
  724. }
  725.  
  726. function sTryFTurn180(clip c, bool "chroma", int "bitdepth") {
  727. bitdepth=Default(bitdepth, 8)
  728. bitdepth==8 ? eval("""
  729. try {
  730.    IsAvsPlus ? dontdoft : nop()
  731.    return c.FTurn180(chroma=chroma)
  732.    }
  733. catch(err_msg) {
  734.    return c.Turn180()
  735.    }
  736.    """) : BitdepthMsbLsb(c.BitdepthMsb(bitdepth=bitdepth).sTryFTurn180(chroma), c.BitdepthLsb(bitdepth=bitdepth).sTryFTurn180(chroma), bitdepth=bitdepth)
  737. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement