Advertisement
Guest User

SMDegrain

a guest
Nov 16th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. ###          mod in 16 11 2015 to fix subpixel=3 and add Prefilter auto 16 (lsb) support      ###
  11. ###                           and yuy2 support in avs 2.6 with masktools 2.6                 ###
  12. ###                                                                                          ###
  13. ################################################################################################
  14. ###
  15. ### General purpose simple degrain function. Pure temporal denoiser. Basically a wrapper(function)/frontend of mvtools2+mdegrain
  16. ### with some added common related options. Goal is accessibility and quality but not targeted to any specific kind of source.
  17. ### The reason behind is to keep it simple so aside masktools2 you will only need MVTools2.
  18. ###
  19. ### Check documentation for deep explanation on settings and defaults.
  20. ### VideoHelp thread: (http://forum.videohelp.com/threads/369142)
  21. ###
  22. ################################################################################################
  23.  
  24. function SMDegrain (clip input   , int "tr"      , int "thSAD"    , int  "thSADC" , bool "RefineMotion",  val  "contrasharp", clip "CClip"      , bool "interlaced" ,  int   "plane" ,  int   "Globals",                                  \
  25.                      int "pel"   , int "subpixel", val "prefilter", clip "mfilter", int  "blksize"     , int  "overlap"     ,  int  "search"    , bool "truemotion" , int  "limit"   ,  int   "limitc" ,  int   "thSCD1" , int "thSCD2",  \
  26.                     bool "chroma", int "hpad"    , int "vpad"     , bool "lsb"    , bool "lsb_in"      ,  bool "lsb_out"    , int  "mode"       , bool  "slices"    , val  "Show"    ,  float "Str"    ,  float "Amp"){
  27.  
  28.  
  29.  
  30. # Defaults & Conditionals
  31.  
  32.  
  33. tr           = default( tr,       2 )
  34. thSAD        = default( thSAD,  300 )
  35. thSAD2=int(thSAD/2)
  36. thSADC       = default( thSADC, thSAD2)
  37. RefineMotion = default( RefineMotion,false)
  38.  
  39. lsb_in       = default( lsb_in,  false)
  40. lsb_out      = default( lsb_out, false)
  41. lsb          = default( lsb    , lsb_in || lsb_out)
  42. mode         = default( mode, 0 )
  43. mode         = mode != 9 ? mode : -1
  44. ampn         = mode != 9 ? 0.0  : 1.0
  45. slices       = default(slices, true)
  46.  
  47. Globals      = default( Globals,  0 )
  48. GlobalR = Globals == 1        ? true : false
  49. GlobalO = Globals == 3        ? true : false
  50. if1     = Defined(CClip)      ? true : false
  51.  
  52. Contrasharp  = default( Contrasharp, !GlobalO && if1 ? true : false)
  53. Interlaced   = default( Interlaced,  false)
  54.  
  55. w       = input.width ()
  56. h       = input.height()
  57. sShow   = IsString(show)      ? true : false
  58. preclip = IsClip(prefilter)   ? true : false
  59. planar  = IsYUY2(input)       ? true : false
  60. ifC     = IsBool(Contrasharp) ? true : false
  61. if0     = ifC ? (Contrasharp  ? true : false) : (Contrasharp > 0.0 ? true : false)
  62. if2     = if0 && lsb && !GlobalO                                    ? true : false
  63. if3     = tr > 3                                                    ? true : false
  64. if4     = (w > 1099 ||  h > (lsb_in ? 1199 : 599))                  ? true : false
  65. if5     = interlaced ? (GetParity(input)                            ? true : false) : nop()
  66. avs26   = VersionNumber() < 2.60 ? false : true
  67. yuy25   = !avs26 && planar
  68.  
  69.  
  70. pel          = default( pel, if4 ? 1 : 2 )
  71. subpixel     = default( subpixel,  2 )
  72. prefilter    = default( prefilter, -1 )
  73. MFilterB     = defined(mfilter) ? true : false
  74. pelclip      = pel>1 && subpixel == 3 ? true : false
  75. Str          = default( Str, 1.0 )
  76. Amp          = default( Amp, 0.0625 )
  77.  
  78. blksize      = default( blksize,  if4 ? 16 : 8 )
  79. blk2=blksize/2
  80. overlap      = default( overlap,  blk2 )
  81. ovl2=overlap/2
  82. search       = default( search,   4 )
  83. truemotion   = default( truemotion, !if4)
  84. thSCD1       = default( thSCD1,  int(pow((blksize*2.5),2)))
  85. thSCD2       = default( thSCD2,  130 )
  86.  
  87.  
  88. plane        = default( plane,    4 )
  89. chroma       = default( chroma,    true)
  90. Chr          = chroma ? 3 : 1
  91. Chr2         = chroma ? 3 : (prefilter==3 ? 2 : 1)
  92. plane0       = plane!=0        ? true : false
  93. U            = plane0 && plane!=2 ? 3 : 2
  94. V            = plane0 && plane!=1 ? 3 : 2
  95. Uin          = lsb_in ? 3 : u
  96. Vin          = lsb_in ? 3 : v
  97.  
  98.  
  99. hpad         = blksize
  100. vpad         = blksize
  101. limit        = default( limit,  255 )
  102. limitc       = default( limitc,limit)
  103.  
  104. Show         = default( Show,  false)
  105.  
  106.  
  107.  
  108.  
  109. # Error Report
  110.  
  111.                   Assert(blksize==4 || blksize==8 || blksize==16 || blksize==32,"MAnalyse: Block's size must be 4x4, 8x4, 8x8, 16x2, 16x8, 16x16, 32x16, 32x32")
  112.                   Assert(overlap <= blk2,              "Overlap must be at least half blksize or less")
  113.                   Assert(overlap == overlap-overlap%2, "MAnalyse: overlap must be an even figure")
  114.                   Assert(IsBool(Show) || sShow,        "'Show' only accepts bool inputs (true,false), or string modes ('Speed','Memory',Quality')")
  115.                   Assert(preclip || IsInt(prefilter),  "'prefilter' only accepts integers and clip inputs")
  116.                   Assert(ifC     || IsInt(Contrasharp),"'Contrasharp' only accepts integers and bool inputs")
  117. !chroma         ? Assert(plane==0,                     "Denoising chroma with luma only vectors is bugged in mvtools and thus unsupported") : nop()
  118. MFilterB        ? Assert(IsClip(mfilter),              "'mfilter' only accepts clip inputs")                        : nop()
  119. interlaced      ? Assert(h%4==0,                       "Interlaced sources require mod 4 height sizes")             : nop()
  120. lsb_in          ? Assert(!interlaced,                  "Interlaced 16 bit stacked clip is not a recognized format") : nop()
  121. planar && if0   ? Assert(ifC,                          "LSFmod doesn't support YUY2 sources")                       : nop()
  122. RefineMotion    ? Assert(blksize > 7,                  "For RefineMotion you need a blksize of at least 8")         : nop()
  123. lsb_in          ? Assert(lsb,                          "lsb_in  requires: lsb=true")                                : nop()
  124. lsb_out         ? Assert(lsb,                          "lsb_out requires: lsb=true")                                : nop()
  125.  
  126.  
  127.  
  128. # RefineMotion Variables
  129.  
  130. halfblksize  = RefineMotion ?  blk2                                     : nop() # MRecalculate works with half block size
  131. halfoverlap  = RefineMotion ? (overlap == 2 ? overlap : ovl2+ovl2%2)    : nop() # Halve the overlap to suit the halved block size
  132. halfthSAD    = RefineMotion || if3 ?     thSAD2                         : nop() # MRecalculate uses a more strict thSAD, which defaults to 150 (half of function's default of 300)
  133. halfthSADC   =                 if3 ? int(thSADC/2)                      : nop() # For MDegrainN()
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140. # Input preparation for: LSB_IN, Interlacing, Planar and MSuper optimization when pel=2
  141.  
  142. inputY  = planar      ? (lsb_in   ? Dither_YUY2toPlanar16(input)          : Interleaved2planar(input))                          : input
  143.  
  144. inputP  = !interlaced ? (pel == 2 ? inputY.AssumeFrameBased()             : inputY)                                             : \
  145.                         (if5      ? inputY.AssumeTFF().SeparateFields()   : inputY.AssumeBFF().SeparateFields())
  146.  
  147. input8h = lsb_in      ?             inputP. Ditherpost(mode=6, slice=slices)                                                    : nop()
  148. input8y = planar      ? (lsb_in   ? input8h.Dither_YUY2toInterleaved()    : inputP)                                             : inputP
  149. input8  = lsb_in      ? (planar   ? input8y.Interleaved2planar()                                                                : \
  150.                                     input8h)                                                                                    : input8y
  151.  
  152.  
  153. # Prefilter & Motion Filter
  154.  
  155. Mfilter = MFilterB ? Mfilter : input8
  156.  
  157. bug_wa = interlaced && planar && chroma && !avs26 ? 2 : Chr # bug: crash prevention workaround (http://forum.doom9.org/showthread.php?p=1546684#post1546684)
  158.  
  159. # 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
  160. # 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.
  161. pref   = !GlobalR ? ( preclip        ?  prefilter                                                                  : \
  162.                     ((prefilter==-1) ?  inputP                                                                     : \
  163.                      (prefilter==0)  ?  input8.MinBlur(0,Chr,planar)                                               : \
  164.                      (prefilter==1)  ?  input8.MinBlur(1,Chr,planar)                                               : \
  165.                      (prefilter==2)  ?  input8.MinBlur(2,Chr,planar)                                               : \
  166.                      (prefilter==3)  ?  (!planar && lsb ? Dither_merge16_8(inputP.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(),                                \
  167.                                                                    lsb_in?inputP.Dither_lut16("x 4096 < 65535 x 19200 > 0 65535 x 4096 - 65535 19200 4096 - / * - ? ?",u=1,v=1).Ditherpost(mode=6, slice=slices, u=Chr,   v=Chr)                                           \
  168.                                                                          :inputP.mt_lut(      "x 16 < 255 x 75 > 0 255 x 16 - 255 75 16 - / * - ? ?",u=1,v=1), luma=chroma,                       u=Chr2,  v=Chr2)                                                       : \
  169.                                  avs26 && planar && lsb ? Dither_merge16_8(inputP.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(),                                \
  170.                                                                    lsb_in?inputP.Dither_lut16("x 4096 < 65535 x 19200 > 0 65535 x 4096 - 65535 19200 4096 - / * - ? ?",u=1,v=1).Ditherpost(mode=6, slice=slices, u=Chr,   v=Chr)                                           \
  171.                                                                          :inputP.planar2interleaved.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                                                       : \
  172.                                            planar && avs26 ? mt_merge(           Dfttest(!lsb_in?inputP.Planar2Interleaved(!chroma):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.ConvertToYV16:input8y.ConvertToYV16,                                                   \
  173.                                                                           input8y.planar2interleaved.ConvertToYV16.mt_lut(      "x 16 < 255 x 75 > 0 255 x 16 - 255 75 16 - / * - ? ?",u=1,v=1), luma=planar?false:chroma,          u=bug_wa,v=bug_wa).ConvertToYuy2.interleaved2planar                                                    : \
  174.                                                              mt_merge( planar ?  Dfttest(!lsb_in?inputP.Planar2Interleaved(!chroma):input8y,sstring="0.0:4.0 0.2:9.0 1.0:15.0",tbsize=1,u=chroma,v=chroma,dither=1).Interleaved2Planar(!chroma)                          : \
  175.                                                                                  Dfttest(        inputP,                                    sstring="0.0:4.0 0.2:9.0 1.0:15.0",tbsize=1,u=chroma,v=chroma,dither=1),!lsb_in?input8y:input8y.Interleaved2Planar(!chroma),   \
  176.                                                                           input8y.mt_lut(      "x 16 < 255 x 75 > 0 255 x 16 - 255 75 16 - / * - ? ?",u=1,v=1), luma=planar?false:chroma,          u=bug_wa,v=bug_wa))                                                   : \
  177.                      (prefilter==4)  ?  eval("""
  178.                                              # In a more lucid state I could probably have laid out this block much better... or not...
  179.                                              NL_in   = !planar ? (lsb_in ? inputP : input8.Dither_convert_8_to_16()) : Interlaced ? (if5 ? input.AssumeTFF().SeparateFields().Dither_convert_8_to_16() : input.AssumeBFF().SeparateFields().Dither_convert_8_to_16()) : (lsb_in ? input : input.Dither_convert_8_to_16())
  180.                                              NL_W    = width(NL_in)
  181.                                              U8      = chroma || planar ? UToY8(NL_in) : nop()
  182.                                              V8      = chroma || planar ? VToY8(NL_in) : nop()
  183.                                              NL_lsb  = chroma ? (!planar ?           StackVertical(StackVertical(Dither_get_msb(u8),Dither_get_msb(v8)),\
  184.                                                                                      StackVertical(Dither_get_lsb(u8),Dither_get_lsb(v8)))              :\
  185.                                                                                      StackHorizontal(u8.ConvertToYV12(),v8.ConvertToYV12())) : nop()
  186.  
  187.                                              Y8      = chroma ? (!planar ? StackHorizontal(ConvertToY8(NL_in),NL_lsb) : \
  188.                                                                            StackHorizontal(StackVertical(Dither_get_msb(ConvertToY8(NL_in)),Dither_get_lsb(ConvertToY8(NL_in))).ConvertToYV12(),NL_lsb)) : \
  189.                                                                 (!planar ? NL_in : NL_in.converttoY8())
  190.  
  191.                                              Y8      = Y8.KNLMeansCL(D=1, A=1, h=7.0,lsb_inout=true,device_type="GPU")
  192.  
  193.                                              my      = planar ? Y8.Dither_get_msb().crop(0,0,chroma?-NL_W:0,0) : nop()
  194.                                              ly      = planar ? Y8.Dither_get_lsb().crop(0,0,chroma?-NL_W:0,0) : nop()
  195.  
  196.                                              chroma ? eval ("
  197.                                               u_h = u8.height()/2
  198.                                               v_h = v8.height()/2
  199.  
  200.                                               u16 = !planar ? StackVertical(Dither_get_msb(Y8).crop(NL_W,0,0,-v_h),Dither_get_lsb(Y8).crop(NL_W,0,0,-v_h))                : \
  201.                                                               StackVertical(Dither_get_msb(Y8).crop(NL_W,0,-NL_W/2 ,0),Dither_get_lsb(Y8).crop(NL_W,0,-NL_W/2 ,0))
  202.                                               v16 = !planar ? StackVertical(Dither_get_msb(Y8).crop(NL_W,u_h, 0,0),Dither_get_lsb(Y8).crop(NL_W,u_h, 0,0))                : \
  203.                                                               StackVertical(Dither_get_msb(Y8).crop(NL_W+NL_W/2,0,0,0),Dither_get_lsb(Y8).crop(NL_W+NL_W/2,0,0,0))
  204.  
  205.                                               !planar ? YToUV(u16,v16,Y8.crop(0,0,-u8.width(),0)) : avs26 ? YToUV(u16, v16, StackVertical(my, ly)) : YToUV(u16, v16, StackVertical(StackVertical (my,my), StackVertical (ly,ly))) ") : \
  206.                                            ( !planar || avs26 ? Y8 : YToUV (u8, v8, StackVertical (StackVertical (my, my), StackVertical (ly, ly)))) """)                                   : \
  207.                       Assert(false,    "Mode must be between -1~3: "+string(prefilter))))                                                                                           : \
  208.                       input8
  209.  
  210.  
  211.  
  212.  
  213.  
  214. # Default Auto-Prefilter - Luma expansion TV->PC (up to 16% more values for motion estimation)
  215.  
  216. pref   = avs26 && planar && ((lsb_in && prefilter==-1) || prefilter==4) ? pref.Dither_YUY2toInterleaved16().Interleaved2planar() : pref
  217. pref16 = (Height(pref)) == (Height(input8)*2) ? true : false
  218. pref   = !GlobalR ? (!yuy25 && (lsb || (!preclip ? prefilter==4 : false) ) ? ((!preclip ? ((lsb_in && prefilter==-1) || prefilter==3 || prefilter==4 ) : false)          ? \
  219.                      pref.Dither_Luma_Rebuild(S0=Str,c=Amp,slice=slices,lsb_in=true,uv=Chr2)                                      : \
  220.                      pref.Dither_Luma_Rebuild(S0=Str,c=Amp,slice=slices,lsb   =true,lsb_in=pref16,uv=Chr2))                       : \
  221.                                       ((!preclip ?  ((lsb_in && prefilter==-1) || prefilter==4)                                   : false)                                 ? \
  222.                      pref.Ditherpost(mode=6,slice=slices,u=Chr,v=Chr).Dither_YUY2toInterleaved().Interleaved2planar(!chroma).Dither_Luma_Rebuild(S0=Str,c=Amp,uv=Chr   )   : \
  223.                      pref.                                                    Dither_Luma_Rebuild(S0=Str,lsb_in=pref16,slice=slices,c=Amp,uv=bug_wa)))                                  : \
  224.                      pref
  225.  
  226.  
  227.  
  228.  
  229.  
  230. (sShow ? false : !Show) ? eval("""
  231.  
  232.  
  233. # Subpixel 3
  234.  
  235. 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()
  236. 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()
  237.  
  238.  
  239.  
  240.  
  241. # Motion vectors search
  242.  
  243.  
  244. super_search =                             pelclip ? MSuper(pref,     pel=pel,               chroma=chroma, hpad=hpad, vpad=vpad,          pelclip=pclip,  rfilter=4, planar=planar) : \
  245.                                                     MSuper(pref,     pel=pel,sharp=subpixel,chroma=chroma, hpad=hpad, vpad=vpad                        ,  rfilter=4, planar=planar)
  246. super_render = !GlobalR ?                 (pelclip ? MSuper(input8,   pel=pel,               chroma=plane0, hpad=hpad, vpad=vpad,levels=1, pelclip=pclip2           , planar=planar) : \
  247.                                                     MSuper(input8,   pel=pel,sharp=subpixel,chroma=plane0, hpad=hpad, vpad=vpad,levels=1                           , planar=planar)): super_search
  248. Recalculate  = !GlobalR && RefineMotion ? (pelclip ? MSuper(pref,     pel=pel,               chroma=chroma, hpad=hpad, vpad=vpad,levels=1, pelclip=pclip            , planar=planar) : \
  249.                                                     MSuper(pref,     pel=pel,sharp=subpixel,chroma=chroma, hpad=hpad, vpad=vpad,levels=1                           , planar=planar)): nop()
  250. !if3  ? eval("
  251. bv6  =   interlaced && (tr > 2)                  ? (!GlobalR  ? super_search.MAnalyse(isb = true, delta = 6,  overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion) :         bv6) :  nop()
  252. bv6  =   interlaced && (tr > 2) && RefineMotion &&  !GlobalR  ?   MRecalculate           (Recalculate, bv6,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion) :  bv6
  253. bv4  =   interlaced && (tr > 1)                  ? (!GlobalR  ? super_search.MAnalyse(isb = true, delta = 4,  overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion) :         bv4) :  nop()
  254. bv4  =   interlaced && (tr > 1) && RefineMotion &&  !GlobalR  ?   MRecalculate           (Recalculate, bv4,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion) :  bv4
  255. bv3  =  !interlaced ? ((tr > 2)                  ? (!GlobalR  ? super_search.MAnalyse(isb = true, delta = 3,  overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion) :         bv3) :  nop()) : nop()
  256. bv3  =  !interlaced ? ((tr > 2  && RefineMotion &&  !GlobalR) ?   MRecalculate           (Recalculate, bv3,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion) :  bv3               )   : nop()
  257. bv2  =  (interlaced ||  tr > 1)                  ? (!GlobalR  ? super_search.MAnalyse(isb = true, delta = 2,  overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion) :         bv2) :  nop()
  258. bv2  = ((interlaced ||  tr > 1) && RefineMotion &&  !GlobalR) ?   MRecalculate           (Recalculate, bv2,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion) :  bv2
  259. bv1  =  !interlaced ?                              (!GlobalR  ? super_search.MAnalyse(isb = true, delta = 1,  overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion) :         bv1        ) : nop()
  260. bv1  =  !interlaced ?            ( RefineMotion &&  !GlobalR  ?   MRecalculate           (Recalculate, bv1,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion) :  bv1               ) : nop()
  261.  
  262. fv1  =  !interlaced ?                              (!GlobalR  ? super_search.MAnalyse(isb = false, delta = 1, overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion) :         fv1        ) : nop()
  263. fv1  =  !interlaced ?            ( RefineMotion &&  !GlobalR  ?   MRecalculate           (Recalculate, fv1,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion) :  fv1               ) : nop()
  264. fv2  =  (interlaced ||  tr > 1)                  ? (!GlobalR  ? super_search.MAnalyse(isb = false, delta = 2, overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion) :         fv2) :  nop()
  265. fv2  = ((interlaced ||  tr > 1) && RefineMotion &&  !GlobalR) ?   MRecalculate           (Recalculate, fv2,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion) :  fv2
  266. fv3  =  !interlaced ? ((tr > 2)                  ? (!GlobalR  ? super_search.MAnalyse(isb = false, delta = 3, overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion) :         fv3) :  nop()) : nop()
  267. fv3  =  !interlaced ? ((tr > 2  && RefineMotion &&  !GlobalR) ?   MRecalculate           (Recalculate, fv3,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion) :  fv3               )   : nop()
  268. fv4  =   interlaced && (tr > 1)                  ? (!GlobalR  ? super_search.MAnalyse(isb = false, delta = 4, overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion) :         fv4) :  nop()
  269. fv4  =   interlaced && (tr > 1) && RefineMotion &&  !GlobalR  ?   MRecalculate           (Recalculate, fv4,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion) :  fv4
  270. fv6  =   interlaced && (tr > 2)                  ? (!GlobalR  ? super_search.MAnalyse(isb = false, delta = 6, overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma, truemotion=truemotion) :         fv6) :  nop()
  271. fv6  =   interlaced && (tr > 2) && RefineMotion &&  !GlobalR  ?   MRecalculate           (Recalculate, fv6,   overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma, truemotion=truemotion) :  fv6 " )     : \
  272. \
  273. eval("
  274. tr2    = tr*2
  275. vmulti =                   !GlobalR ? super_search.MAnalyse( multi=true,overlap=    overlap,blksize=    blksize,   search=search,chroma=chroma,truemotion=truemotion, delta = interlaced ? tr2 : tr) : vmulti
  276. vmulti = RefineMotion   ? (!GlobalR ? MRecalculate (Recalculate, vmulti,overlap=halfoverlap,blksize=halfblksize, thSAD=halfthSAD,chroma=chroma,truemotion=truemotion,    tr = interlaced ? tr2 : tr) : vmulti) : vmulti
  277. vmulti =                   !GlobalR ? (interlaced ? vmulti.SelectEvery (4, 2, 3) : vmulti) : vmulti.SelectRangeEvery (Rtr*2, tr2, 0, false)")
  278.  
  279.  
  280.  
  281. # Finally, MDegrain
  282.  
  283.  
  284. output  = !GlobalO ? (interlaced ?
  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) :
  286. \                 (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) :
  287. \                 (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) :
  288. \                             mfilter.MDegrain1(super_render, bv2, fv2,                                         thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=true)):
  289. \                    ( 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):
  290. \                 (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)          :
  291. \                 (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)          :
  292. \                             mfilter.MDegrain1(super_render, bv2, fv2,                                         thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar))))       :
  293. \         (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) :
  294. \                 (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) :
  295. \                 (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) :
  296. \                             mfilter.MDegrain1(super_render, bv1, fv1,                                         thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar,lsb=true)):
  297. \                    ( 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):
  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)          :
  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)          :
  300. \                             mfilter.MDegrain1(super_render, bv1, fv1,                                         thSAD=thSAD, thSADC=thSADC, thSCD1=thSCD1, thSCD2=thSCD2, limit=limit, limitc=limitc, plane=plane, planar=planar))))       : nop()
  301.  
  302.  
  303. # LSB_IN merging
  304.  
  305. outputI= !GlobalO && planar && (lsb || ifC) ? output.Planar2Interleaved()                                                                                             : nop()
  306. !GlobalO && lsb   ? eval("
  307. blnkclp= lsb_in  || (if2 && !planar) ? BlankClip (lsb_in?input8h:input8, pixel_type="+Chr(34)+"YV12"+Chr(34)+", color_yuv=0)                                          : nop()
  308. output = lsb && planar               ? Dither_YUY2toPlanar16(outputI)                                                                                                 : output
  309. 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()
  310.  
  311.  
  312.  
  313.  
  314. # Contrasharp (only sharpens luma)
  315.  
  316. ifC && if0 && if4 ? eval("
  317. global Super           = super_render
  318. global cb1             = !if3 ? (interlaced ? bv2 : bv1) : vmulti.SelectEvery (tr2, 0)
  319. global cf1             = !if3 ? (interlaced ? fv2 : fv1) : vmulti.SelectEvery (tr2, 1)") : nop()
  320.  
  321. !GlobalO && if0   ? eval("
  322.  
  323. CCh   =       if1 ? CClip.height() : nop()
  324. ref8  =       if1 ? (h == CCh*2)   : nop()
  325. ref16 =       if1 ? (h == CCh/2)   : nop()
  326.  
  327. interlaced && if1 ? Assert(!ref16, "+Chr(34)+"Interlaced 16 bit stacked CClip is not a recognized format"+Chr(34)+")         : nop()
  328.  
  329. CClip = planar ?  (if1 ?  (lsb_in ? (ref8 ? CClip.Interleaved2planar(true) : CClip.Dither_YUY2toPlanar16().DitherPost(mode=-1,u=1,v=1).Dither_YUY2toInterleaved().Interleaved2planar(true))                         : \
  330.                                      (interlaced ? (if5 ? CClip.AssumeTFF().SeparateFields().Interleaved2planar(true) : CClip.AssumeBFF().SeparateFields().Interleaved2planar(true))                                : \
  331.                                      (ref16 ? CClip.Dither_YUY2toPlanar16().DitherPost(mode=-1,u=1,v=1).Dither_YUY2toInterleaved().Interleaved2planar(true) : CClip.Interleaved2planar(true))))                     : \
  332.                           (lsb_in ?  inputY.DitherPost(mode=-1,u=1,v=1).Dither_YUY2toInterleaved().Interleaved2planar(true)                                                                                         : \
  333.                                      inputP))                                                                                                                                                                       : \
  334.                   (if1 ?  (lsb_in ? (ref8 ? CClip : CClip.DitherPost(mode=-1,u=1,v=1))                                                                                                                              : \
  335.                                      (interlaced ? (if5 ? CClip.AssumeTFF().SeparateFields()                          : CClip.AssumeBFF().SeparateFields()) : (ref16 ? CClip.DitherPost(mode=-1,u=1,v=1) : CClip))) : \
  336.                           (lsb_in ?  input.DitherPost(mode=-1,u=1,v=1)                                                                                                                                              : \
  337.                                      inputP))")                                                                                                                                                                     : nop()
  338.  
  339.  
  340. !GlobalO && if2   ? eval("
  341. OutTO8  = output.DitherPost(mode=-1,u=1,v=1)
  342. OutTO8P = planar  ? OutTO8.Dither_YUY2toInterleaved()                                                                                               : nop()
  343. ctr16   = ifC     ? ContrasharpeningHD (planar ? OutTO8P.Interleaved2planar(true):OutTO8, CClip, planar=planar, HD=if4)                             : \
  344.                     LSFmod(OutTO8,source=CClip,defaults="+Chr(34)+"slow"+Chr(34)+",strength=Contrasharp,soothe=false,Lmode=0,soft=-2,keep=0)
  345.  
  346.  
  347. ctr16P  = planar  ? ctr16.Planar2Interleaved(true)  : ctr16
  348. ctr16   = StackVertical (ctr16P, planar ? BlankClip  (ctr16P, pixel_type="+Chr(34)+"YUY2"+Chr(34)+", color_yuv=0,length=1) : blnkclp )
  349. 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)
  350. ctr16   = MergeChroma(ctr16, output)                                                                                                     ")         : nop()
  351.  
  352.  
  353.  
  354. # Globals Output
  355.  
  356. GlobalO || Globals == 2  ? (!if3 ? eval("
  357. global Super           = super_render
  358. global bv6             = bv6
  359. global bv4             = bv4
  360. global bv3             = bv3
  361. global bv2             = bv2
  362. global bv1             = bv1
  363.  
  364. global fv1             = fv1
  365. global fv2             = fv2
  366. global fv3             = fv3
  367. global fv4             = fv4
  368. global fv6             = fv6
  369. ") : eval("
  370. global bv1             =                                            vmulti.SelectEvery (tr2, 0)
  371. global bv2             = interlaced ? bv1                         : vmulti.SelectEvery (tr2, 2)
  372. global bv3             =                                            vmulti.SelectEvery (tr2, 4)
  373. global bv4             = interlaced ? vmulti.SelectEvery (tr2, 2) : nop()
  374. global bv6             = interlaced ? bv3                         : nop()
  375.  
  376. global fv1             =                                            vmulti.SelectEvery (tr2, 1)
  377. global fv2             = interlaced ? fv1                         : vmulti.SelectEvery (tr2, 3)
  378. global fv3             =                                            vmulti.SelectEvery (tr2, 5)
  379. global fv4             = interlaced ? vmulti.SelectEvery (tr2, 3) : nop()
  380. global fv6             = interlaced ? fv3                         : nop()
  381. global Super           = super_render
  382. global vmulti          = vmulti
  383. global Rtr             = tr")) : nop()
  384.  
  385.  
  386.  
  387. # Output
  388.  
  389. !GlobalO ? (lsb_out ? (if0 ? ctr16 : output)  : \
  390.                      (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))                                              : \
  391.                                     (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()) : \
  392.                                                   (ifC ? ContraSharpeningHD(output, CClip, planar=planar, HD=if4)                      : LSFmod(output,source=CClip,defaults="slow",strength=Contrasharp,soothe=false,Lmode=0,soft=-2,keep=0))))       : \
  393.                             (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))))    \
  394.         :  input
  395.  
  396. !GlobalO ? (planar ? (lsb ? Dither_YUY2toInterleaved() : (if0 ? MergeChroma(Planar2Interleaved(true), interlaced ? outputI.weave() : outputI) : Planar2Interleaved())) : last) : last """) : \
  397.  \
  398.  \
  399.  \
  400.  \
  401.  \
  402. eval("""
  403.  
  404. ##############
  405. # SHOW PANEL #
  406. ##############
  407.  
  408.  
  409.  # Common Variables
  410.  
  411. h     = lsb_in ? h/2 : h
  412. w2    = w*1.0
  413. h2    = h*1.0
  414. hi    = interlaced ? round(h/2.0) : h
  415. AR    = w2/h2
  416. lsp   = interlaced?15:30
  417. lsp   = AR < 1.9 ? lsp : floor(AR < 1.9 ? lsp : ((AR>2.4) ? 0 : lsp-((AR-1.9)*(lsp/(2.4-1.9)))))
  418. ys    = round(lsb_in ? h2/6 : (interlaced?h2/15:h2/6))
  419. s     = floor(((interlaced?w2/1.6:w2)/720.0)*16.0)
  420. type  = planar?"YUY2":"YV12"
  421.  
  422. Wadd  = w2/40
  423. Wadd2 = Wadd/2
  424. w1    = round(w2+Wadd+Wadd2)
  425. w3    = (w2/3)+Wadd/2.1
  426. w2    = round(w1+w3)
  427. w3    = round(w1+(w3+Wadd2)*1.96)
  428. w3sq  = string(round(w3*1.01))
  429. w3s   = string(w3)
  430.  
  431.  # Panel Separator
  432.  
  433. blkclp = BlankClip(interlaced?assumeframebased(pref):pref,length=1,pixel_type=type,color=$161616)
  434.         \.crop(0,interlaced?h/2%8:h%8,0,0).separatefields().assumeframebased().separatefields()
  435.  
  436. Interleave(selecteven(blkclp).addborders(2,0,0,0,color=$7f7f7f).crop(0,0,-2,0),selectodd(blkclp))
  437.  
  438. WOut=weave().assumefieldbased().weave()
  439. sep=round((hi-height(WOut))/2.0)
  440.  
  441. StackHorizontal(planar? pref.Planar2Interleaved : pref,WOut.addborders(0,sep-sep%2,0,hi-(height(WOut)+(sep-sep%2)),color=$161616))
  442.  
  443.  
  444.  
  445.  # Color Guide Squares
  446.  
  447. a=interlaced?h*0.37:h*0.74
  448. b=interlaced?h*0.40:h*0.80
  449. c=interlaced?h*0.43:h*0.86
  450.  
  451. !planar ? 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()
  452. !planar ? 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()
  453. !planar ? 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()
  454.  
  455.  
  456.  # Parameters List
  457.  
  458. blue  = sShow ? (show=="Speed"   ? true : false) : false
  459. red   = sShow ? (show=="Memory"  ? true : false) : false
  460. yellow= sShow ? (show=="Quality" ? true : false) : false
  461.  
  462. Color = sShow ? (blue   ? $00aef0 : \
  463.                 red    ? $ff0000 : \
  464.                 yellow ? $ffff00 : \
  465.                 Assert(false,    "Revise typo for possible color guide modes: 'Speed','Memory','Quality'")) : nop()
  466.  
  467.  
  468.  
  469. varn = "\n\n\n\n\n"
  470. sp   = !GlobalR && str==1.0 ? "  " : ""
  471.  
  472. 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))
  473.  
  474. Subtitle("BASIC"   ,lsp=lsp,text_color=$ffffff,font="COURIER NEW",size=s,x=w1,y=ys)
  475. Subtitle(            "\n\ntr           = " + string(tr)                                                                              + \
  476.         varn+varn+      "chroma       = " + string(chroma)                                                                          ,lsp=lsp,text_color=red||blue               ?Color:$ffffff,font="COURIER NEW",size=s,x=w1,y=ys)
  477. Subtitle(          "\n\n\nthSAD(C)     = " + string( GlobalO?"bypass":string(thSAD)     + "(" + string(plane0?thSADC:"bypass") + ")")+ \
  478.         varn+           "CClip        = " + string( GlobalO?"bypass":(defined(CClip)?"defined":"undefined"))                        + \
  479.                     "\n\nInterlaced   = " + string(interlaced)                                                                      ,lsp=lsp,text_color=                               $ffffff,font="COURIER NEW",size=s,x=w1,y=ys)
  480. Subtitle(varn+           "RefineMotion = " + string( GlobalO?"bypass":RefineMotion)                                                  ,lsp=lsp,text_color=yellow                  ?Color:$ffffff,font="COURIER NEW",size=s,x=w1,y=ys)
  481. 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)
  482. 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)
  483. 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))
  484.  
  485. Subtitle("ADVANCED",lsp=lsp,text_color=$7f7f7f,font="COURIER NEW",size=s,x=w2,y=ys)
  486. Subtitle(              "\n\npel        = " + string(pel)                                                                             ,lsp=lsp,text_color=red                     ?Color:$7f7f7f,font="COURIER NEW",size=s,x=w2,y=ys)
  487. 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)
  488. 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)
  489. 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)
  490. Subtitle(varn+         "\n\nStr        = " + (GlobalR?string("bypass"):string(Str,"%1.2f"))+sp+" Brightness"                         + \
  491.         varn+             "search     = " + string( GlobalR?"bypass":search)                                                        ,lsp=lsp,text_color=yellow&&!GlobalR        ?Color:$7f7f7f,font="COURIER NEW",size=s,x=w2,y=ys)
  492. Subtitle(varn+varn+        "blksize    = " + string( GlobalR?"bypass":blksize)                                                       + \
  493.                         "\noverlap    = " + string( GlobalR?"bypass":overlap)                                                       ,lsp=lsp,text_color=(red||blue)&&!GlobalR   ?Color:$7f7f7f,font="COURIER NEW",size=s,x=w2,y=ys)
  494. 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)"))) + \
  495.                       "\n\nAmp        = " + (GlobalR||str==1.0?string("bypass"):string(Amp,"%1.2f"))+" Amplitude"                   + \
  496.         varn+           "\nlimit(C)   = " + (GlobalO?string("bypass"):string(limit)     + "(" + string(limitc) + ")")               ,lsp=lsp,text_color=                               $7f7f7f,font="COURIER NEW",size=s,x=w2,y=ys)
  497.  
  498. Subtitle("OTHERS"  ,lsp=lsp,text_color=$494949,font="COURIER NEW",size=s,x=w3,y=ys)
  499. Subtitle(             "\n\n\nthSCD1(2) = " + (GlobalO?string("bypass"):string(thSCD1) + "(" + string(thSCD2) + ")")                  + \
  500.                   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)
  501. Subtitle(          varn+"\n\nslices    = " + string(slices)                                                                          ,lsp=lsp,text_color=blue                    ?Color:$494949,font="COURIER NEW",size=s,x=w3,y=ys)
  502. 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)
  503. Subtitle(varn+              "lsb       = " + string(lsb)                                                                             ,lsp=lsp,text_color=yellow                  ?Color:$494949,font="COURIER NEW",size=s,x=w3,y=ys)
  504. 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)
  505. 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)
  506.  
  507. Subtitle("   Speed"   ,lsp=interlaced?15:30,text_color=planar?$00aef0:$7f7f7f,font="COURIER NEW",size=s,x=w3,y=int(a-1))
  508. Subtitle("   Memory"  ,lsp=interlaced?17:34,text_color=planar?$ff0000:$7f7f7f,font="COURIER NEW",size=s,x=w3,y=int(b-1))
  509. Subtitle("   Quality" ,lsp=interlaced?19:38,text_color=planar?$ffff00:$7f7f7f,font="COURIER NEW",size=s,x=w3,y=int(c-1))
  510.  
  511. lsb_out ? StackVertical (last, BlankClip (last, pixel_type=type, color_yuv=0,length=1)) : last""")}
  512.  
  513.  
  514.  
  515.  
  516.  
  517. ########################################################
  518. #                                                      #
  519. # HELPER FUNCTIONS: For Contrasharp and YUY2 handling  #
  520. #                                                      #
  521. ########################################################
  522.  
  523. ########################################
  524. ## Didée's functions:
  525.  
  526.  # contra-sharpening: sharpen the denoised clip, but don't add more to any pixel than what was removed previously.
  527.  # script function from Didée, at the VERY GRAINY thread (http://forum.doom9.org/showthread.php?p=1076491#post1076491)
  528.  
  529.  # In final version 2.0d ContraHD() was merged, to allow proper HD sharpening.
  530.  # In this case global variables of (before denoising) source MSuper and forward and backward
  531.  # compensated motion vectors are necessary as: Super, cb1 and cf1, if used as a stand alone function.
  532.  # Don't know who made (mod) it, so I can't give proper credits, sorry.
  533.  
  534. FUNCTION ContraSharpeningHD(clip denoised, clip original, bool "HD", bool "planar", int "overshoot"){
  535. HD        = default(HD    ,false)
  536. planar    = default(planar,false)
  537. overshoot = default(overshoot,0)
  538. avs26 = VersionNumber() < 2.60 ? false : true
  539. yuy26 = avs26 && Isyuy2(denoised) && planar
  540.  
  541. HD ? eval("""
  542. cb1=original.MCompensate(Super, cb1, planar=planar)
  543. cf1=original.MCompensate(Super, cf1, planar=planar)
  544. 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").ConvertToYuy2.interleaved2planar
  545. 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").ConvertToYuy2.interleaved2planar""") : nop()
  546.  
  547. s    = denoised.MinBlur(HD?2:1,1,planar=planar)                                   # Damp down remaining spots of the denoised clip.
  548. s    = yuy26 ? s.planar2interleaved.ConvertToYV16 : s
  549. original = yuy26 ? original.planar2interleaved.ConvertToYV16 : original
  550. denoised = yuy26 ? denoised.planar2interleaved.ConvertToYV16 : denoised
  551. allD = mt_makediff(original,denoised)                                             # The difference achieved by the denoising.
  552. ssD  = mt_makediff(s,HD?s.removegrain(20,-1,planar=planar).\
  553.                           removegrain(20,-1,planar=planar):\
  554.                         s.removegrain(11,-1,planar=planar))                       # The difference of a simple kernel blur.
  555. allD = yuy26 ? allD.ConvertToYuy2.interleaved2planar : allD
  556. ssD  = yuy26 ? ssD.ConvertToYuy2.interleaved2planar : ssD
  557. 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.
  558. ssDD = yuy26 ? ssDD.planar2interleaved.ConvertToYV16 : ssDD
  559. ssD  = yuy26 ? ssD.planar2interleaved.ConvertToYV16 : ssD
  560. ssDD = SSDD.mt_lutxy(ssD,"x 128 - abs y 128 - abs < x y ?")                       # abs(diff) after limiting may not be bigger than before.
  561.  
  562. denoised.mt_adddiff(ssDD,U=2,V=2)                                                 # Apply the limited difference. (Sharpening is just inverse blurring)
  563. HD ? mt_clamp(last,pmax,pmin,overshoot,overshoot,chroma="copy first") : last
  564. yuy26 ? last.ConvertToYuy2.interleaved2planar : last
  565. }
  566.  
  567.  # MinBlur   by Didée (http://avisynth.org/mediawiki/MinBlur)
  568.  # Nifty Gauss/Median combination (Modified for Planar input)
  569.  
  570. function MinBlur(clip clp, int "r", int "uv", bool "planar"){
  571.  
  572. r      = default(r,1)
  573. uv     = default(uv,3)
  574. planar = default(planar,false)
  575. avs26 = VersionNumber() < 2.60 ? false : true
  576. yuy26 = avs26 && Isyuy2(clp) && planar
  577.  
  578. uv2    = (uv==2) ? 1  : uv
  579. rg4    = (uv==3) ? 4  : -1
  580. rg11   = (uv==3) ? 11 : -1
  581. rg20   = (uv==3) ? 20 : -1
  582. uvm2   = r==2 ? (uv==3?2:uv==2?0:-1) : nop()
  583. uvm3   = r==3 ? (uv==3?3:uv==2?0:-1) : nop()
  584.  
  585. RG11D = (r==0) ? yuy26 ? mt_makediff(clp.planar2interleaved.ConvertToYV16,clp.sbr(planar=planar).planar2interleaved.ConvertToYV16,U=uv2,V=uv2).ConvertToYuy2.interleaved2planar : mt_makediff(clp,clp.sbr(planar=planar),U=uv2,V=uv2)
  586.  \    : (r==1) ? yuy26 ? mt_makediff(clp.planar2interleaved.ConvertToYV16,clp.removegrain(11,rg11,planar=planar).planar2interleaved.ConvertToYV16,U=uv2,V=uv2).ConvertToYuy2.interleaved2planar : mt_makediff(clp,clp.removegrain(11,rg11,planar=planar),U=uv2,V=uv2)
  587.  \    : (r==2) ? yuy26 ? mt_makediff(clp.planar2interleaved.ConvertToYV16,clp.removegrain(11,rg11,planar=planar).removegrain(20,rg20,planar=planar).planar2interleaved.ConvertToYV16,U=uv2,V=uv2).ConvertToYuy2.interleaved2planar : mt_makediff(clp,clp.removegrain(11,rg11,planar=planar).removegrain(20,rg20,planar=planar),U=uv2,V=uv2)
  588.  \    :          yuy26 ? mt_makediff(clp.planar2interleaved.ConvertToYV16,clp.removegrain(11,rg11,planar=planar).removegrain(20,rg20,planar=planar).removegrain(20,rg20,planar=planar).planar2interleaved.ConvertToYV16,U=uv2,V=uv2).ConvertToYuy2.interleaved2planar : mt_makediff(clp,clp.removegrain(11,rg11,planar=planar).removegrain(20,rg20,planar=planar).removegrain(20,rg20,planar=planar),U=uv2,V=uv2)
  589. RG4D  = (r<=1) ? yuy26 ? mt_makediff(clp.planar2interleaved.ConvertToYV16,clp.removegrain(4,rg4,planar=planar).planar2interleaved.ConvertToYV16,U=uv2,V=uv2).ConvertToYuy2.interleaved2planar : mt_makediff(clp,clp.removegrain(4,rg4,planar=planar),U=uv2,V=uv2)
  590.  \    : (r==2) ? yuy26 ? mt_makediff(clp.planar2interleaved.ConvertToYV16,clp.Quantile(radius_y=2, radius_u=uvm2, radius_v=uvm2 ,planar=planar).planar2interleaved.ConvertToYV16, U=uv2,V=uv2).ConvertToYuy2.interleaved2planar : mt_makediff(clp,clp.Quantile(radius_y=2, radius_u=uvm2, radius_v=uvm2 ,planar=planar), U=uv2,V=uv2)
  591.  \    :          yuy26 ? mt_makediff(clp.planar2interleaved.ConvertToYV16,clp.Quantile(radius_y=3, radius_u=uvm3, radius_v=uvm3, planar=planar).planar2interleaved.ConvertToYV16, U=uv2,V=uv2).ConvertToYuy2.interleaved2planar : mt_makediff(clp,clp.Quantile(radius_y=3, radius_u=uvm3, radius_v=uvm3, planar=planar), U=uv2,V=uv2)
  592. DD    = yuy26 ? mt_lutxy(RG11D.planar2interleaved.ConvertToYV16,RG4D.planar2interleaved.ConvertToYV16,"x 128 - y 128 - * 0 < 128 x 128 - abs y 128 - abs < x y ? ?",U=uv2,V=uv2).ConvertToYuy2.interleaved2planar : mt_lutxy(RG11D,RG4D,"x 128 - y 128 - * 0 < 128 x 128 - abs y 128 - abs < x y ? ?",U=uv2,V=uv2)
  593. yuy26 ? clp.planar2interleaved.ConvertToYV16.mt_makediff(DD.planar2interleaved.ConvertToYV16,U=uv,V=uv).ConvertToYuy2.interleaved2planar : clp.mt_makediff(DD,U=uv,V=uv) }
  594.  
  595.  
  596.  # helper function: make a highpass on a blur's difference (well, kind of that)
  597.  # Highpass of spatial r=1 Gaussian (like blur(1.0)) (Modified for Planar input)
  598.  
  599. function sbr(clip c, bool "planar") {
  600.  
  601. planar = default(planar,false)
  602. avs26 = VersionNumber() < 2.60 ? false : true
  603. yuy26 = avs26 && Isyuy2(c) && planar
  604.  
  605. rg11   = c.removegrain(11,-1,planar=planar)
  606. rg11D  = yuy26 ? mt_makediff(c.planar2interleaved.ConvertToYV16,rg11.planar2interleaved.ConvertToYV16).ConvertToYuy2.interleaved2planar : mt_makediff(c,rg11)
  607. rg11DD = yuy26 ? mt_makediff(rg11D.planar2interleaved.ConvertToYV16,rg11D.removegrain(11,-1,planar=planar).planar2interleaved.ConvertToYV16).mt_lutxy(rg11D.planar2interleaved.ConvertToYV16,"x 128 - y 128 - * 0 < 128 x 128 - abs y 128 - abs < x y ? ?").ConvertToYuy2.interleaved2planar : 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 ? ?")
  608. yuy26 ? c.planar2interleaved.ConvertToYV16.mt_makediff(rg11DD.planar2interleaved.ConvertToYV16,U=2,V=2).ConvertToYuy2.interleaved2planar : c.mt_makediff(rg11DD,U=2,V=2) }
  609.  
  610. ########################################
  611. ## cretindesalpes' functions: (http://forum.doom9.org/showthread.php?p=1505297#post1505297)
  612.  
  613. # Converts 16 bit interleaved YUY2 to 16bit planar YUY2 suitable for Ditherpost. (Use right before Ditherpost; this format is not suitable for filtering)
  614.  
  615. function Dither_YUY2toPlanar16(c){c
  616. yuy26   = !(VersionNumber() < 2.60) && isyuy2(c)
  617. my = !yuy26 ? Dither_get_msb().ConvertToYV12() : nop()
  618. ly = !yuy26 ? Dither_get_lsb().ConvertToYV12() : nop()
  619. u = !yuy26 ? UToY ().ConvertToYV12 () : nop()
  620. v = !yuy26 ? VToY ().ConvertToYV12 () : nop()
  621. yuy26 ? ConvertToYV16() : YToUV (u, v, StackVertical (StackVertical (my, my), StackVertical (ly, ly)))}
  622.  
  623.  
  624. # Converts 8 bit interleaved YUY2 to 8 bit planar YUY2 ready for either 8 or 16 bit processing also suitable for Ditherpost
  625.  
  626. function Dither_YUY2toPlanar(c){c
  627. yuy26   = !(VersionNumber() < 2.60) && isyuy2(c)
  628. y = !yuy26 ? ConvertToYV12 () : nop()
  629. u = !yuy26 ? UToY ().ConvertToYV12 () : nop()
  630. v = !yuy26 ? VToY ().ConvertToYV12 () : nop()
  631. yuy26 ? ConvertToYV16() : YToUV (u, v, StackVertical (y, y))}
  632.  
  633.  
  634. # Converts back the above special planar formats to normal interleaved 16 bit YUY2
  635.  
  636. function Dither_YUY2toInterleaved16(c){c
  637. yuy26   = !(VersionNumber() < 2.60)
  638. y = !yuy26 ? StackVertical (Crop (0, 0, 0, (Height () /(4))), Crop (0, (Height () /(2))+(Height () /(4)), 0, 0)).ConvertToYUY2 () : nop()
  639. u = !yuy26 ? UToY ().ConvertToYUY2 () : nop()
  640. v = !yuy26 ? VToY ().ConvertToYUY2 () : nop()
  641. yuy26 ? ConvertToYUY2() : YToUV (u, v, y)}
  642.  
  643. # Converts back the above special planar formats to normal interleaved YUY2
  644.  
  645. function Dither_YUY2toInterleaved(c){c
  646. yuy26   = !(VersionNumber() < 2.60)
  647. y = !yuy26 ? Crop (0, 0, 0, Height () /(2)).ConvertToYUY2 () : nop()
  648. u = !yuy26 ? UToY ().ConvertToYUY2 () : nop()
  649. v = !yuy26 ? VToY ().ConvertToYUY2 () : nop()
  650. yuy26 ? ConvertToYUY2() : YToUV (u, v, y)}
  651.  
  652.  
  653.  
  654. # 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)
  655. # By courtesy of cretindesalpes. (http://forum.doom9.org/showthread.php?p=1548318#post1548318)
  656.  
  657. 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"){
  658. lsb_in  = Default( lsb_in  ,false)
  659. lsb_out = Default( lsb_out ,false)
  660. lsb     = Default( lsb     ,lsb_in || lsb_out)
  661. mode    = Default( mode ,6)
  662. uv      = Default(uv,    3)
  663. s0      = Default(s0,  2.0)
  664. c       = Default(c,  1.0/16)
  665. ampn    = Default(ampn, 0.0)
  666. slice   = Default(slice, true)
  667. yuy26   = !(VersionNumber() < 2.60) && isyuy2(src)
  668. src     = yuy26 ? src.planar2interleaved.ConvertToYV16 : src
  669.  
  670.     k = (s0 - 1) * c
  671.     t = lsb_in ? "x 4096 - 56064 / 0 1 clip" : "x 16 - 219 / 0 1 clip"
  672.     e = String(k)+" "+String(1+c)+" "+String((1+c)*c)+" "+t+" "+String(c)
  673. \       +" + / - * "+t+" 1 "+String(k)+" - * + "+String(lsb?65536:255)+" *"
  674. src
  675. lsb ? (lsb_in ? Dither_lut16 (yexpr=e,expr="x 32768 - 32768 * 28672 / 32768 +",y=3, u=uv, v=uv)   : \
  676.                 Dither_lut8  (yexpr=e,expr="x 128 - 32768 * 112 / 32768 +"    ,y=3, u=uv, v=uv))  : \
  677.                 mt_lut       (yexpr=e,expr="x 128 - 128 * 112 / 128 +"        ,y=3, u=uv, v=uv)
  678. lsb_out ? last : (lsb ? Ditherpost(mode=mode,ampn=ampn,slice=slice,staticnoise=ampn!=0.0?true:false,u=uv,v=uv) : last)
  679. return yuy26 ? last.ConvertToYUY2.interleaved2planar : last
  680. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement