Advertisement
Guest User

dfttestMC

a guest
Sep 4th, 2015
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.1 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. # add slice bool to use it to avoid artifacts with avstp
  12. #
  13. # Requirements:
  14. # dfttest
  15. # MVTools2
  16. #
  17. # Suggested:
  18. # Dither (for stack16 processing)
  19. #
  20. # Description of function parameters:
  21. #
  22. # pp = Clip to calculate vectors from (default input)
  23. # mc = Number of frames in each direction to compensate (default 2, max 5)
  24. # mdg = Run MDeGrain before dfttest (default false)
  25. # mdgSAD = thSAD for MDeGrain (default undefined)
  26. # lsb = stack16 output and processing (default false)
  27. #
  28. # dfttest Y, U, V, sigma, sbsize, sosize, tbsize, and dither are supported.
  29. # Extra dfttest parameters may be passed via dfttest_params.
  30. # MVTools2 pel, thSCD, thSAD, blksize, overlap, dct, search, and
  31. # searchparam are also supported.
  32. #
  33. # sigma is the main control of dfttest strength.
  34. # tbsize should not be set higher than mc * 2 + 1.
  35.  
  36. 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",
  37.     \ 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",
  38.         \ int "dct", bool "slices", bool "luma_rebuild")
  39. {
  40.     # Set default options. Most external parameters are passed valueless.
  41.    
  42.     mc = default(mc, 2).min(5)
  43.     mdg = default(mdg, false)
  44.     lsb = default(lsb, false)
  45.     lsb_in = default(lsb_in, false)
  46.     input8 = lsb_in ? input.ditherpost(mode=7,slice=slices) : input
  47.     super_filter = default(super_filter,4)
  48.     pp_enabled = defined(pp)
  49.     pp_lsb_in = default(pp_lsb_in,(pp_enabled ? lsb_in ? (height(input) == height(pp)) ? true : false : false : false ))
  50.     lsb_enable = false
  51.     recalculate = default(recalculate,true)
  52.     truemotion = default(truemotion,true)
  53.     Y = default(Y, true)
  54.     U = default(U, true)
  55.     V = default(V, true)
  56.     tbsize = default(tbsize, mc * 2 + 1)
  57.     dfttest_params = default(dfttest_params, "")
  58.     blksize = default(blksize,16)
  59.     blksize2 = int(blksize/2) + (int(blksize/2)%2)
  60.     overlap = default(overlap,blksize2)
  61.     overlap2 = int(overlap/2) + (int(overlap/2)%2)
  62.     rStr          = default( rStr, 1.0 )
  63.     Amp          = default( Amp, 0.0625 )
  64.     slices       = default(slices, true)
  65.     luma_rebuild = default(luma_rebuild, true)
  66.     # Set chroma parameters.
  67.     chroma = U || V
  68.     plane = U && !Y && !V ? 1 : V && !Y && !U ? 2 : chroma && !Y ? 3 : Y && chroma ? 4 : 0
  69.  
  70.     # Prepare supersampled clips.
  71.    
  72.     pp = (pp_enabled) ? luma_rebuild ? pp.dither_luma_rebuild(S0=rStr,c=Amp,slice=slices,lsb_in=pp_lsb_in,lsb_out=false,uv=((U&&v)?3:1)) : pp : nop()
  73.     pp_super = pp_enabled ? MSuper(pp, pel=pel, chroma=chroma,rfilter=super_filter) : MSuper(input8, pel=pel, chroma=chroma)
  74.     super = pp_enabled ? input8.MSuper(pel=pel, levels=1, chroma=chroma) : pp_super
  75.     recalc_sup =  pp_enabled ? MSuper(pp,pel=pel, levels=1, chroma=chroma) : pp_super
  76.    
  77.     # Motion vector search.
  78.     b5vec = MAnalyse(pp_super, delta=5, isb=true, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap, blksize=blksize, dct=dct,truemotion=truemotion)
  79.     b4vec = MAnalyse(pp_super, delta=4, isb=true, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap, blksize=blksize, dct=dct,truemotion=truemotion)
  80.     b3vec = MAnalyse(pp_super, delta=3, isb=true, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap, blksize=blksize, dct=dct,truemotion=truemotion)
  81.     b2vec = MAnalyse(pp_super, delta=2, isb=true, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap, blksize=blksize, dct=dct,truemotion=truemotion)
  82.     b1vec = MAnalyse(pp_super, delta=1, isb=true, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap, blksize=blksize, dct=dct,truemotion=truemotion)
  83.     f1vec = MAnalyse(pp_super, delta=1, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap, blksize=blksize, dct=dct,truemotion=truemotion)
  84.     f2vec = MAnalyse(pp_super, delta=2, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap, blksize=blksize, dct=dct,truemotion=truemotion)
  85.     f3vec = MAnalyse(pp_super, delta=3, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap, blksize=blksize, dct=dct,truemotion=truemotion)
  86.     f4vec = MAnalyse(pp_super, delta=4, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap, blksize=blksize, dct=dct,truemotion=truemotion)
  87.     f5vec = MAnalyse(pp_super, delta=5, chroma=chroma, search=search, searchparam=searchparam, overlap=overlap, blksize=blksize, dct=dct,truemotion=truemotion)
  88.  
  89.    
  90.     recalculate ? Eval("""
  91.     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)
  92.     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)
  93.     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)
  94.     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)
  95.     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)
  96.     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)
  97.     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)
  98.     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)
  99.     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)
  100.     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)
  101.     """) : nop()
  102.     # Optional MDegrain.
  103.     Eval("""
  104.     try {
  105.         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) :
  106.             \ mc == 2 && mdg ? MDeGrain2(input8, super, b1vec, f1vec, b2vec, f2vec, thSAD=mdgSAD, plane=plane, thSCD1=thSCD1, thSCD2=thSCD2, lsb=true) :
  107.             \ 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 :
  108.             \ errmsggonext
  109.             degrained8 = lsb ? degrained.dither_get_msb() : degrained.ditherpost(mode=7,slice=slices)
  110.             lsb_enable = true
  111.         } catch(err_msg)
  112.         {
  113.         degrained8 = mc >= 3 && mdg ? MDeGrain3(input8, super, b1vec, f1vec, b2vec, f2vec, b3vec, f3vec, thSAD=mdgSAD, plane=plane, thSCD1=thSCD1, thSCD2=thSCD2) :
  114.             \ mc == 2 && mdg ? MDeGrain2(input8, super, b1vec, f1vec, b2vec, f2vec, thSAD=mdgSAD, plane=plane, thSCD1=thSCD1, thSCD2=thSCD2) :
  115.             \ mdg ? MDeGrain1(input8, super, b1vec, f1vec, thSAD=mdgSAD, plane=plane, thSCD1=thSCD1, thSCD2=thSCD2) : input8
  116.         lsb_enable = false
  117.         }
  118.     """)
  119.     degrained_super = mdg ? MSuper(degrained8, pel=pel, levels=1, chroma=chroma) : super
  120.     b5clip = MCompensate(degrained8, degrained_super, b5vec, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  121.     b4clip = MCompensate(degrained8, degrained_super, b4vec, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  122.     b3clip = MCompensate(degrained8, degrained_super, b3vec, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  123.     b2clip = MCompensate(degrained8, degrained_super, b2vec, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  124.     b1clip = MCompensate(degrained8, degrained_super, b1vec, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  125.     f1clip = MCompensate(degrained8, degrained_super, f1vec, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  126.     f2clip = MCompensate(degrained8, degrained_super, f2vec, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  127.     f3clip = MCompensate(degrained8, degrained_super, f3vec, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  128.     f4clip = MCompensate(degrained8, degrained_super, f4vec, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  129.     f5clip = MCompensate(degrained8, degrained_super, f5vec, thSAD=thSAD, thSCD1=thSCD1, thSCD2=thSCD2)
  130.  
  131.     # Create compensated clip.
  132.     interleaved = mc >= 5 ? Interleave(f5clip, f4clip, f3clip, f2clip, f1clip, degrained8, b1clip, b2clip, b3clip, b4clip, b5clip) :
  133.         \ mc == 4 ? Interleave(f4clip, f3clip, f2clip, f1clip, degrained8, b1clip, b2clip, b3clip, b4clip) :
  134.         \ mc == 3 ? Interleave(f3clip, f2clip, f1clip, degrained8, b1clip, b2clip, b3clip) :
  135.         \ mc == 2 ? Interleave(f2clip, f1clip, degrained8, b1clip, b2clip) :
  136.         \ Interleave(f1clip, degrained8, b1clip)
  137.         degrainedmc= lsb_enable ? degrained.changefps(interleaved,linear=true) : degrained8.changefps(interleaved,linear=true)
  138.         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)
  139.         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
  140.         #interleaved = lsb_enable ? dither_merge16_8(degrainedmc,interleaved.dither_convert_8_to_16(),mt_Lutxy(degrainedmc.ditherpost(mode=7,slice=slices),interleaved,"x y - 0 != 255 0 ?",u=3,v=3),u=3,v=3) : interleaved
  141.  
  142.        
  143.     # Perform dfttest. Exception handling required for official dfttest.
  144.     Eval("""
  145.     try {
  146.         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 + ")")
  147.         } catch(err_msg)
  148.         {
  149.         filtered = Eval("dfttest(interleaved, Y=Y, U=U, V=V, sigma=sigma, sbsize=sbsize, sosize=sosize, tbsize=tbsize, dither=dither" + dfttest_params + ")")
  150.         }
  151.     """)
  152.        
  153.     return selectEvery(filtered, mc * 2 + 1, mc)
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement