Advertisement
Guest User

dfttestMC

a guest
Jun 4th, 2015
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Avisynth 11.84 KB | None | 0 0
  1. # Motion-compensated dfttest by twc
  2. # Aka: Really Really Really Slow
  3. #
  4. # v2 by BakaProxy
  5. # Aka: Even more Really Really Really slow
  6. # Added: Recalculate, (proper) 16 bit input handling, dither_luma_rebuild and some masking to prevent 16 bit data loss from 8 bit Mdegrain and Mcompensate
  7. #
  8. #
  9. # v3 by A.SONY
  10. # make it work in normal avs again and some fix, and not that slow if you use it without lsb/lsb_in
  11. #
  12. # Requirements:
  13. # dfttest
  14. # MVTools2
  15. #
  16. # Suggested:
  17. # Dither (for stack16 processing)
  18. #
  19. # Description of function parameters:
  20. #
  21. # pp = Clip to calculate vectors from (default input)
  22. # mc = Number of frames in each direction to compensate (default 2, max 5)
  23. # mdg = Run MDeGrain before dfttest (default false)
  24. # mdgSAD = thSAD for MDeGrain (default undefined)
  25. # lsb = stack16 output and processing (default false)
  26. #
  27. # dfttest Y, U, V, sigma, sbsize, sosize, tbsize, and dither are supported.
  28. # Extra dfttest parameters may be passed via dfttest_params.
  29. # MVTools2 pel, thSCD, thSAD, blksize, overlap, dct, search, and
  30. # searchparam are also supported.
  31. #
  32. # sigma is the main control of dfttest strength.
  33. # tbsize should not be set higher than mc * 2 + 1.
  34.  
  35. function dfttestMC(clip input, clip "pp", int "mc", bool "mdg", bool "Y", bool "U", bool "V", float "sigma", int "sbsize", int "sosize", int "tbsize", int "dither", bool "lsb",bool "lsb_in",bool "pp_lsb_in",int "super_filter",
  36.     \ string "dfttest_params",float "rStr",float "Amp", int "mdgSAD", int "thSAD", int "thSCD1", int "thSCD2", bool "recalculate", bool "truemotion", int "pel", int "blksize", int "search", int "searchparam", int "overlap",
  37.         \ int "dct")
  38. {
  39.     # Set default options. Most external parameters are passed valueless.
  40.    
  41.     mc = default(mc, 2).min(5)
  42.     mdg = default(mdg, false)
  43.     lsb = default(lsb, false)
  44.     lsb_in = default(lsb_in, false)
  45.     input8 = lsb_in ? input.ditherpost(mode=7) : input
  46.     super_filter = default(super_filter,4)
  47.     pp_enabled = defined(pp)
  48.     pp_lsb_in = default(pp_lsb_in,(pp_enabled ? lsb_in ? (height(input) == height(pp)) ? true : false : false : false ))
  49.     lsb_enable = false
  50.     recalculate = default(recalculate,true)
  51.     truemotion = default(truemotion,true)
  52.     Y = default(Y, true)
  53.     U = default(U, true)
  54.     V = default(V, true)
  55.     tbsize = default(tbsize, mc * 2 + 1)
  56.     dfttest_params = default(dfttest_params, "")
  57.     blksize = default(blksize,16)
  58.     blksize2 = int(blksize/2) + (int(blksize/2)%2)
  59.     overlap = default(overlap,blksize2)
  60.     overlap2 = int(overlap/2) + (int(overlap/2)%2)
  61.     rStr          = default( rStr, 1.0 )
  62.     Amp          = default( Amp, 0.0625 )
  63.     # Set chroma parameters.
  64.     chroma = U || V
  65.     plane = U && !Y && !V ? 1 : V && !Y && !U ? 2 : chroma && !Y ? 3 : Y && chroma ? 4 : 0
  66.  
  67.     # Prepare supersampled clips.
  68.    
  69.     pp = (pp_enabled) ? pp.dither_luma_rebuild(S0=rStr,c=Amp,lsb_in=pp_lsb_in,lsb_out=false,uv=((U&&v)?3:1)) : nop()
  70.     pp_super = pp_enabled ? MSuper(pp, pel=pel, chroma=chroma,rfilter=super_filter) : MSuper(input8, pel=pel, chroma=chroma)
  71.     super = pp_enabled ? input8.MSuper(pel=pel, levels=1, chroma=chroma) : pp_super
  72.     recalc_sup =  pp_enabled ? MSuper(pp,pel=pel, levels=1, chroma=chroma) : pp_super
  73.    
  74.     # Motion vector search.
  75.     b5vec = MAnalyse(pp_super, delta=5, isb=true, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap, blksize=blksize, dct=dct,truemotion=truemotion)
  76.     b4vec = MAnalyse(pp_super, delta=4, isb=true, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap, blksize=blksize, dct=dct,truemotion=truemotion)
  77.     b3vec = MAnalyse(pp_super, delta=3, isb=true, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap, blksize=blksize, dct=dct,truemotion=truemotion)
  78.     b2vec = MAnalyse(pp_super, delta=2, isb=true, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap, blksize=blksize, dct=dct,truemotion=truemotion)
  79.     b1vec = MAnalyse(pp_super, delta=1, isb=true, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap, blksize=blksize, dct=dct,truemotion=truemotion)
  80.     f1vec = MAnalyse(pp_super, delta=1, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap, blksize=blksize, dct=dct,truemotion=truemotion)
  81.     f2vec = MAnalyse(pp_super, delta=2, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap, blksize=blksize, dct=dct,truemotion=truemotion)
  82.     f3vec = MAnalyse(pp_super, delta=3, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap, blksize=blksize, dct=dct,truemotion=truemotion)
  83.     f4vec = MAnalyse(pp_super, delta=4, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap, blksize=blksize, dct=dct,truemotion=truemotion)
  84.     f5vec = MAnalyse(pp_super, delta=5, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap, blksize=blksize, dct=dct,truemotion=truemotion)
  85.  
  86.    
  87.     recalculate ? Eval("""
  88.     b5vec = MRecalculate(recalc_sup,b5vec , chroma=chroma, search=search, searchparam=searchparam, overlap=overlap2, blksize=blksize2, dct=dct,thSAD=defined(mdgSAD) ? int(mdgSAD/2) : 200,truemotion=truemotion)
  89.     b4vec = MRecalculate(recalc_sup,b4vec , chroma=chroma, search=search, searchparam=searchparam, overlap=overlap2, blksize=blksize2, dct=dct,thSAD=defined(mdgSAD) ? int(mdgSAD/2) : 200,truemotion=truemotion)
  90.     b3vec = MRecalculate(recalc_sup,b3vec , chroma=chroma, search=search, searchparam=searchparam, overlap=overlap2, blksize=blksize2, dct=dct,thSAD=defined(mdgSAD) ? int(mdgSAD/2) : 200,truemotion=truemotion)
  91.     b2vec = MRecalculate(recalc_sup,b2vec , chroma=chroma, search=search, searchparam=searchparam, overlap=overlap2, blksize=blksize2, dct=dct,thSAD=defined(mdgSAD) ? int(mdgSAD/2) : 200,truemotion=truemotion)
  92.     b1vec = MRecalculate(recalc_sup,b1vec , chroma=chroma, search=search, searchparam=searchparam, overlap=overlap2, blksize=blksize2, dct=dct,thSAD=defined(mdgSAD) ? int(mdgSAD/2) : 200,truemotion=truemotion)
  93.     f1vec = MRecalculate(recalc_sup,f1vec, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap2, blksize=blksize2, dct=dct,thSAD=defined(mdgSAD) ? int(mdgSAD/2) : 200,truemotion=truemotion)
  94.     f2vec = MRecalculate(recalc_sup,f2vec, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap2, blksize=blksize2, dct=dct,thSAD=defined(mdgSAD) ? int(mdgSAD/2) : 200,truemotion=truemotion)
  95.     f3vec = MRecalculate(recalc_sup,f3vec, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap2, blksize=blksize2, dct=dct,thSAD=defined(mdgSAD) ? int(mdgSAD/2) : 200,truemotion=truemotion)
  96.     f4vec = MRecalculate(recalc_sup,f4vec, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap2, blksize=blksize2, dct=dct,thSAD=defined(mdgSAD) ? int(mdgSAD/2) : 200,truemotion=truemotion)
  97.     f5vec = MRecalculate(recalc_sup,f5vec, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap2, blksize=blksize2, dct=dct,thSAD=defined(mdgSAD) ? int(mdgSAD/2) : 200,truemotion=truemotion)
  98.     """) : nop()
  99.     # Optional MDegrain.
  100.     Eval("""
  101.     try {
  102.         degrained = lsb || lsb_in ? mc >= 3 && mdg ? MDeGrain3(input8, super, b1vec, f1vec, b2vec, f2vec, b3vec, f3vec, thSAD=mdgSAD, plane=plane, thSCD1=thSCD1, thSCD2=thSCD2, lsb=true) :
  103.             \ mc == 2 && mdg ? MDeGrain2(input8, super, b1vec, f1vec, b2vec, f2vec, thSAD=mdgSAD, plane=plane, thSCD1=thSCD1, thSCD2=thSCD2, lsb=true) :
  104.             \ mdg ? MDeGrain1(input8, super, b1vec, f1vec, thSAD=mdgSAD, plane=plane, thSCD1=thSCD1, thSCD2=thSCD2, lsb=true) : lsb_in ? input : input.Dither_convert_8_to_16 :
  105.             \ errmsggonext
  106.             degrained8 = lsb ? degrained.dither_get_msb() : degrained.ditherpost(mode=7)
  107.             lsb_enable = true
  108.         } catch(err_msg)
  109.         {
  110.         degrained8 = mc >= 3 && mdg ? MDeGrain3(input8, super, b1vec, f1vec, b2vec, f2vec, b3vec, f3vec, thSAD=mdgSAD, plane=plane, thSCD1=thSCD1, thSCD2=thSCD2) :
  111.             \ mc == 2 && mdg ? MDeGrain2(input8, super, b1vec, f1vec, b2vec, f2vec, thSAD=mdgSAD, plane=plane, thSCD1=thSCD1, thSCD2=thSCD2) :
  112.             \ mdg ? MDeGrain1(input8, super, b1vec, f1vec, thSAD=mdgSAD, plane=plane, thSCD1=thSCD1, thSCD2=thSCD2) : input8
  113.         lsb_enable = false
  114.         }
  115.     """)
  116.     degrained_super = mdg ? MSuper(degrained8, pel=pel, levels=1, chroma=chroma) : super
  117.     b5clip = MCompensate(degrained8, degrained_super, b5vec, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  118.     b4clip = MCompensate(degrained8, degrained_super, b4vec, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  119.     b3clip = MCompensate(degrained8, degrained_super, b3vec, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  120.     b2clip = MCompensate(degrained8, degrained_super, b2vec, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  121.     b1clip = MCompensate(degrained8, degrained_super, b1vec, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  122.     f1clip = MCompensate(degrained8, degrained_super, f1vec, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  123.     f2clip = MCompensate(degrained8, degrained_super, f2vec, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  124.     f3clip = MCompensate(degrained8, degrained_super, f3vec, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  125.     f4clip = MCompensate(degrained8, degrained_super, f4vec, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  126.     f5clip = MCompensate(degrained8, degrained_super, f5vec, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  127.  
  128.     # Create compensated clip.
  129.     interleaved = mc >= 5 ? Interleave(f5clip, f4clip, f3clip, f2clip, f1clip, degrained8, b1clip, b2clip, b3clip, b4clip, b5clip) :
  130.         \ mc == 4 ? Interleave(f4clip, f3clip, f2clip, f1clip, degrained8, b1clip, b2clip, b3clip, b4clip) :
  131.         \ mc == 3 ? Interleave(f3clip, f2clip, f1clip, degrained8, b1clip, b2clip, b3clip) :
  132.         \ mc == 2 ? Interleave(f2clip, f1clip, degrained8, b1clip, b2clip) :
  133.         \ Interleave(f1clip, degrained8, b1clip)
  134.         degrainedmc= lsb_enable ? degrained.changefps(interleaved,linear=true) : degrained8.changefps(interleaved,linear=true)
  135.         mask= lsb_enable ? mt_logic(interleaved,degrainedmc.dither_get_msb(), "xor",y=3,u=3,v=3).mt_binarize(0,y=3,u=3,v=3) : mt_logic(interleaved,degrainedmc, "xor",y=3,u=3,v=3).mt_binarize(0,y=3,u=3,v=3)
  136.         interleaved = lsb_enable || lsb ? dither_merge16(degrainedmc,interleaved.dither_convert_8_to_16(),stackvertical(mask,mask),u=3,v=3) : interleaved #more optimized version than the one below
  137.         #interleaved = lsb_enable ? dither_merge16_8(degrainedmc,interleaved.dither_convert_8_to_16(),mt_Lutxy(degrainedmc.ditherpost(mode=7),interleaved,"x y - 0 != 255 0 ?",u=3,v=3),u=3,v=3) : interleaved
  138.  
  139.        
  140.     # Perform dfttest. Exception handling required for official dfttest.
  141.     Eval("""
  142.     try {
  143.         filtered = Eval("dfttest(interleaved, Y=Y, U=U, V=V, sigma=sigma, sbsize=sbsize, sosize=sosize, tbsize=tbsize, dither=dither, lsb=lsb,lsb_in=lsb_enable" + dfttest_params + ")")
  144.         } catch(err_msg)
  145.         {
  146.         filtered = Eval("dfttest(interleaved, Y=Y, U=U, V=V, sigma=sigma, sbsize=sbsize, sosize=sosize, tbsize=tbsize, dither=dither" + dfttest_params + ")")
  147.         }
  148.     """)
  149.        
  150.     return selectEvery(filtered, mc * 2 + 1, mc)
  151. }
  152.  
  153. # 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)
  154. # By courtesy of cretindesalpes. (http://forum.doom9.org/showthread.php?p=1548318#post1548318)
  155.  
  156. function Dither_Luma_Rebuild (clip src, float "s0", float "c",int "uv", bool "lsb", bool "lsb_in", bool "lsb_out", int "mode"){
  157. lsb_in  = Default( lsb_in  ,false)
  158. lsb_out = Default( lsb_out ,false)
  159. lsb     = Default( lsb     ,lsb_in || lsb_out)
  160. mode    = Default( mode ,6)
  161. uv      = Default(uv,    3)
  162. s0      = Default(s0,  2.0)
  163. c       = Default(c,  1.0/16)
  164.  
  165.     k = (s0 - 1) * c
  166.     t = lsb_in ? "x 4096 - 56064 / 0 1 clip" : "x 16 - 219 / 0 1 clip"
  167.     e = String(k)+" "+String(1+c)+" "+String((1+c)*c)+" "+t+" "+String(c)
  168. \       +" + / - * "+t+" 1 "+String(k)+" - * + "+String(lsb?65536:255)+" *"
  169. src
  170. lsb ? (lsb_in ? Dither_lut16 (yexpr=e,expr="x 32768 - 32768 * 28672 / 32768 +",y=3, u=uv, v=uv)  : \
  171.                 Dither_lut8  (yexpr=e,expr="x 128 - 32768 * 112 / 32768 +"    ,y=3, u=uv, v=uv)) : \
  172.                 mt_lut       (yexpr=e,expr="x 128 - 128 * 112 / 128 +"        ,y=3, u=uv, v=uv)
  173. lsb_out ? last : (lsb ? Ditherpost(mode=mode,u=uv,v=uv) : last)}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement