Zastin

mvmulti.py

Jan 16th, 2018
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.20 KB | None | 0 0
  1. """ Since feisty2's only supports float, this is a drop-in replacement.
  2.    That said, there were some small changes made and it now uses vs.core over vs.get_core()
  3.    
  4.    All:         can use floats for thsad, etc without integer mvtools throwing errors
  5.                 everything has **args, so now the opt parameter can be changed
  6.    Analyze:     badrange defaults to -24 when search is 3, 6, or 7
  7.                 both 'mvmulti.Analyse' and 'mvmulti.Analyze' work now
  8.    Recalculate: using blksize=2 on integer clips just passes the vectors unprocessed to avoid error messages
  9.    DegrainN:    setting i.e. planes=[0,1,2] will work in place of plane=4
  10.                 for float 32, setting thsadc/limitc overrides array settings: (thsad=[1,2,2], thsadc=1) --> (thsad=[1,1,1])
  11.                 for integers, not setting thsadc/limitc will use the trailing (param[-1]) values from thsad/limit instead
  12.                 trying to use large temporal radii on integer clips will silently fall back to mv.Degrain3
  13. """
  14.  
  15. import vapoursynth as vs
  16.  
  17. def Analyse(super, blksize=8, blksizev=None, levels=0, search=4, searchparam=2, pelsearch=0, _lambda=None, chroma=True, tr=3, truemotion=True, lsad=None, plevel=None, _global=None, pnew=None, pzero=None, pglobal=0, overlap=0, overlapv=None, divide=0, badsad=10000.0, badrange=None, meander=True, trymany=False, fields=False, tff=None, search_coarse=3, dct=0, **args):
  18.     core         = vs.core
  19.     if super.format.sample_type==vs.FLOAT:
  20.         mv       = core.mvsf
  21.     else:
  22.         mv       = core.mv
  23.         badsad   = round(badsad)
  24.     if badrange is None:
  25.         badrange = -24 if search in [3,6,7] else 24
  26.     def getvecs(isb, delta):
  27.         vectors  = mv.Analyse(super, isb=isb, blksize=blksize, blksizev=blksizev, levels=levels, search=search, searchparam=searchparam, pelsearch=pelsearch, _lambda=_lambda, chroma=chroma, delta=delta, truemotion=truemotion, lsad=lsad, plevel=plevel, _global=_global, pnew=pnew, pzero=pzero, pglobal=pglobal, overlap=overlap, overlapv=overlapv, divide=divide, badsad=badsad, badrange=badrange, meander=meander, trymany=trymany, fields=fields, tff=tff, search_coarse=search_coarse, dct=dct, **args)
  28.         return vectors
  29.     bv           = [getvecs(True, i) for i in range(tr, 0, -1)]
  30.     fv           = [getvecs(False, i) for i in range(1, tr+1)]
  31.     vmulti       = bv + fv
  32.     vmulti       = core.std.Interleave(vmulti)
  33.     return vmulti
  34.  
  35. Analyze = Analyse
  36.  
  37. def Recalculate(super, vectors, thsad=200.0, smooth=1, blksize=8, blksizev=None, search=4, searchparam=2, _lambda=None, chroma=True, truemotion=True, pnew=None, overlap=0, overlapv=None, divide=0, meander=True, fields=False, tff=None, dct=0, tr=3, **args):
  38.     core         = vs.core
  39.     if super.format.sample_type==vs.FLOAT:
  40.         mv       = core.mvsf
  41.     elif blksize==2:
  42.         return vectors
  43.     else:
  44.         mv       = core.mv
  45.         thsad    = round(thsad)
  46.     def refine(delta):
  47.         analyzed = vectors[delta::2*tr]
  48.         refined  = mv.Recalculate(super, analyzed, thsad=thsad, smooth=smooth, blksize=blksize, blksizev=blksizev, search=search, searchparam=searchparam, _lambda=_lambda, chroma=chroma, truemotion=truemotion, pnew=pnew, overlap=overlap, overlapv=overlapv, divide=divide, meander=meander, fields=fields, tff=tff, dct=dct, **args)
  49.         return refined
  50.     vmulti       = [refine(i) for i in range(0, 2*tr)]
  51.     vmulti       = core.std.Interleave(vmulti)
  52.     return vmulti
  53.  
  54. def StoreVect(vectors, log):
  55.     core         = vs.core
  56.     w            = vectors.get_frame(0).width
  57.     with open(log, "w") as f:
  58.          print(w, file=f)
  59.     vectors      = core.std.CropAbs(vectors, width=w, height=1)
  60.     return vectors
  61.  
  62. def RestoreVect(store, log):
  63.     core         = vs.core
  64.     with open(log, "r") as f:
  65.          w       = int(f.readline())
  66.     vectors      = core.raws.Source(store, w, 1, src_fmt="Y8")
  67.     blank        = core.std.BlankClip(vectors, width=1, length=1)
  68.     vectors      = core.std.Splice([blank, vectors], mismatch=True)
  69.     vectors      = core.std.Trim(vectors, 1)
  70.     return vectors
  71.  
  72. def Compensate(clip, super, vectors, scbehavior=1, thsad=10000.0, fields=False, time=100.0, thscd1=400.0, thscd2=130.0, tff=None, tr=3, cclip=None, **args):
  73.     core         = vs.core
  74.     if super.format.sample_type==vs.FLOAT:
  75.         mv       = core.mvsf
  76.     else:
  77.         mv       = core.mv
  78.         thsad    = round(thsad)
  79.         thscd1   = round(thscd1)
  80.         thscd2   = round(thscd2)
  81.     cclip        = clip if cclip is None else cclip
  82.     def comp(delta):
  83.         v        = vectors[delta::2*tr]
  84.         mc       = mv.Compensate(clip, super, v, scbehavior=scbehavior, thsad=thsad, fields=fields, time=time, thscd1=thscd1, thscd2=thscd2, tff=tff, **args)
  85.         return mc
  86.     bcomp        = [comp(i) for i in range(0, tr)]
  87.     fcomp        = [comp(i) for i in range(tr, 2*tr)]
  88.     compmulti    = bcomp + [cclip] + fcomp
  89.     compmulti    = core.std.Interleave(compmulti)
  90.     return compmulti
  91.  
  92. def Flow(clip, super, vectors, time=100.0, mode=0, fields=False, thscd1=400.0, thscd2=130.0, tff=None, tr=3, cclip=None, **args):
  93.     core         = vs.core
  94.     if super.format.sample_type==vs.FLOAT:
  95.         mv       = core.mvsf
  96.     else:
  97.         mv       = core.mv
  98.         thscd1   = round(thscd1)
  99.         thscd2   = round(thscd2)
  100.     cclip        = clip if cclip is None else cclip
  101.     def flow(delta):
  102.         v        = vectors[delta::2*tr]
  103.         mc       = mv.Flow(clip, super, v, time=time, mode=mode, fields=fields, thscd1=thscd1, thscd2=thscd2, tff=tff)
  104.         return mc
  105.     bflow        = [flow(i) for i in range(0, tr)]
  106.     fflow        = [flow(i) for i in range(tr, 2*tr)]
  107.     flowmulti    = bflow + [cclip] + fflow
  108.     flowmulti    = core.std.Interleave(flowmulti)
  109.     return flowmulti
  110.  
  111. def DegrainN(clip, super, mvmulti, tr=3, thsad=400.0, plane=4, limit=None, thscd1=400.0, thscd2=130.0, **args):
  112.     core         = vs.core
  113.    
  114.     if tr > 24:
  115.         raise ValueError("DegrainN: dude, tr gotta be an int between 1-24, try something less wild maybe?")
  116.    
  117.     # Some parameter parsing to deal with mvtools using chroma parameters and mvtools-sf using int arrays
  118.     thsad    = thsad if isinstance(thsad, list) else [thsad]
  119.     while len(thsad) < 3:
  120.         thsad += [thsad[-1]]
  121.     limit    = limit if isinstance(limit, list) else [limit] if limit is not None else [1] if clip.format.sample_type==vs.FLOAT else [255]
  122.     while len(limit) < 3:
  123.         limit += [limit[-1]]
  124.    
  125.     # Just 'cuz
  126.     if 'planes' in args:
  127.         if planes in [0,[0],(0)]:
  128.             plane = 0
  129.         elif planes in [1,[1],(1)]:
  130.             plane = 1
  131.         elif planes in [2,[2],(2)]:
  132.             plane = 2
  133.         elif planes in [[1,2],(1,2)]:
  134.             plane = 3
  135.         else:
  136.             plane = 4
  137.    
  138.     if clip.format.sample_type==vs.FLOAT:
  139.         mv       = core.mvsf
  140.         tr_ne    = tr
  141.         if 'limitc' in args:
  142.             limit = [limit[0]] + [args['limitc']]*2
  143.         if 'thsadc' in args:
  144.             thsad = [thsad[0]] + [args['thsadc']]*2
  145.     else:
  146.         mv       = core.mv
  147.         tr_ne    = min(3, tr)
  148.         if 'limitc' not in args:
  149.             args.update(thsadc=round(limit[-1]))
  150.         else:
  151.             args['limitc'] = round(args['limitc'])
  152.         if 'thsadc' not in args:
  153.             args.update(thsadc=round(thsad[-1]))
  154.         else:
  155.             args['thsadc'] = round(args['thsadc'])
  156.         if tr > 3:
  157.             mvmulti = core.std.SelectEvery(mvmulti, tr*2, list(range(tr-3,tr*3-3)))
  158.             tr = 3
  159.         thsad    = round(thsad[0])
  160.         limit    = round(limit[0])
  161.         thscd1   = round(thscd1)
  162.         thscd2   = round(thscd2)
  163.    
  164.     def bvn(n): return mvmulti[tr-n::tr*2]
  165.     def fvn(n): return mvmulti[tr+n-1::tr*2]
  166.    
  167.     vectors = dict(mvbw=bvn(1), mvfw=fvn(1))
  168.     for i in range(2, tr_ne+1):
  169.         vectors.update({'mvbw{}'.format(i): bvn(i), 'mvfw{}'.format(i): fvn(i)})
  170.  
  171.     dgn = eval('mv.Degrain{}(clip, super, thsad=thsad, plane=plane, limit=limit, thscd1=thscd1, thscd2=thscd2, **vectors, **args)'.format(tr_ne))
  172.  
  173.     return dgn
  174.  
  175. ### TODO: make Analyse and Recalculate output arrays like [[b1,b2],[f1,f2]] so you can use mixed tr settings with the same set of vectors
Add Comment
Please, Sign In to add comment