Advertisement
feos

Deinterlace+DeblinkTheConsoleOutput

Oct 15th, 2012
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. LoadPlugin("mvtools2.dll")
  2. LoadPlugin("mt_masktools-26.dll")
  3.  
  4. DirectShowSource("Nemo1.mpg").SeparateFields().ConvertToRGB32
  5. pointresize(last.width, last.height*2)
  6. clip1=SelectOdd.Crop(0,0,0,last.height).AddBorders(0,0,0,0)
  7. clip2=SelectEven.Crop(0,0,0,last.height-2).AddBorders(0,2,0,0)
  8. Interleave(clip1,clip2)
  9.  
  10. ng_deblink(0.5).SelectOdd
  11.  
  12. function ng_deblink(clip clp,
  13. \ float "ratio",
  14. \ int "level",
  15. \ clip "blinkmask"
  16. \){
  17. #Version 10 2012.04.22
  18.  
  19.     blink = default(blinkmask, clp.ng_blinkmask())
  20.  
  21.     ratio = default(ratio, 2.0 /3)
  22.     assert(ratio >= 0.0 && 1.0 >= ratio,
  23.     \      "[ng_deblink] 1.0 >= ratio >= 0.0, it was " + string(ratio))
  24.  
  25.     level = default(level, round(ratio * 257))
  26.     assert(level >= 0 && 257 >= level,
  27.     \      "[ng_deblink] 257 >= level >= 0, it was " + string(level))
  28.  
  29.     m01=mt_logic(blink.SelectEvery(4,0),
  30. \                blink.SelectEvery(4,1),
  31. \                mode="or").ConvertToRGB32()
  32.     m23=mt_logic(blink.SelectEvery(4,2),
  33. \                blink.SelectEvery(4,3),
  34. \                mode="or").ConvertToRGB32()
  35.  
  36.     f0=Layer(clp.SelectEvery(4,0),
  37. \            clp.SelectEvery(4,1).Mask(m01),
  38. \            level=level)
  39.     f1=Layer(clp.SelectEvery(4,1),
  40. \            clp.SelectEvery(4,0).Mask(m01),
  41. \            level=level)
  42.     f2=Layer(clp.SelectEvery(4,2),
  43. \            clp.SelectEvery(4,3).Mask(m23),
  44. \            level=(257-level) )
  45.     f3=Layer(clp.SelectEvery(4,3),
  46. \            clp.SelectEvery(4,2).Mask(m23),
  47. \            level=(257-level) )
  48.  
  49.     Interleave(f0,f1,f2,f3)
  50. }
  51.  
  52. function ng_blinkmask(clip clp,
  53. \ bool "TEST",
  54. \ bool "STABILIZE",
  55. \ bool "SHARP",
  56. \ bool "HYSTER",
  57. \ int "inpand",
  58. \ int "expand",
  59. \ int "ml"
  60. \){
  61. #Version 10 2012.04.22
  62.  
  63. #BLINK
  64. # Blinking is a block that alternates on/off each frame
  65. # SelectEven would only see either the on or the off
  66.  
  67. #FLASH
  68. # Flashing is a block that is only on for a single frame
  69. # SelectEven might miss the flash
  70.  
  71. #SHAKE
  72. # Shaking is a block that moves back/forth each frame
  73. # SelectEven would only see one position
  74.  
  75. # The goal of this function is to make a blink mask for use with
  76. # ng_deblink. For overly complicated scenes where a clean blinkmask
  77. # can't be found, just use TASBlend. Uniform softness looks better
  78. # than sharp artifacts.
  79.  
  80. # This function calculates flash and shake info for the test script,
  81. # but those effects should be handled in different ways.
  82. # Flash - choose frames to make sure the flash is in your final clip.
  83. # Shake - SelectEvery(4,0,2,1,3) or SelectEvery(4,1,0,2,3)
  84. # SelectEvery doesn't generally work because it messes with the fluidity
  85. # of motion. But that won't be noticable on a shaking screen.
  86. # Be careful if 2 frame blinking is present, as the selectevery can turn
  87. # it into 1 frame blinking.
  88.  
  89.     TEST      = default(     TEST, false)
  90.     STABILIZE = default(STABILIZE, true)
  91.     SHARP     = default(    SHARP, true)
  92.     HYSTER    = default(   HYSTER, false)
  93.     inpand    = default(   inpand, 1)
  94.     expand    = default(   expand, 1)
  95.     ml        = default(       ml, 128)
  96.  
  97. # The functions used to make the masks work in the YV12 colorspace. Once
  98. # the masks are created they can be used in the RGB32 colorspace direcly
  99.     src=clp.ConvertToYV12()
  100.  
  101. # Blinking is located by looking for blocks that don't exist in
  102. # consecutive frames. The motion vector will match blocks that exist in
  103. # both frames. The blocks that aren't in both will end up with huge
  104. # values that are picked out by the motion mask.
  105.     super = MSuper(src, pel=1)
  106.     fvec  = MAnalyse(super, isb=false, blksize=4)
  107.     bvec  = MAnalyse(super, isb=true , blksize=4)
  108.     fmask = Mmask(src, fvec, kind=1, ml=ml).mt_binarize()
  109.     bmask = Mmask(src, bvec, kind=1, ml=ml).mt_binarize()
  110.     blink = mt_logic(fmask, bmask, mode="and")
  111.  
  112. # Blinking usually occurs against a stable background. This is found
  113. # by looking at blocks 2 frames apart. This distinguishes a blink from
  114. # blocks that are just changing every frame.
  115.     ee_src   = src.SelectEven()
  116.     ee_super = MSuper(ee_src, pel=1)
  117.     ee_fvec  = MAnalyse(ee_super, isb=false, blksize=4)
  118.     ee_bvec  = MAnalyse(ee_super, isb=true , blksize=4)
  119.     ee_fmask = Mmask(ee_src, ee_fvec, kind=1, ml=ml).mt_binarize()
  120.     ee_bmask = Mmask(ee_src, ee_bvec, kind=1, ml=ml).mt_binarize()
  121.  
  122.     oo_src   = src.SelectOdd()
  123.     oo_super = MSuper(oo_src, pel=1)
  124.     oo_fvec  = MAnalyse(oo_super, isb=false, blksize=4)
  125.     oo_bvec  = MAnalyse(oo_super, isb=true , blksize=4)
  126.     oo_fmask = Mmask(oo_src, oo_fvec, kind=1, ml=ml).mt_binarize()
  127.     oo_bmask = Mmask(oo_src, oo_bvec, kind=1, ml=ml).mt_binarize()
  128.  
  129.     fmask_2   = Interleave(ee_fmask, oo_fmask)
  130.     bmask_2   = Interleave(ee_bmask, oo_bmask)
  131.     background = mt_logic(fmask_2.SelectEvery(1,1),
  132. \                         bmask_2.SelectEvery(1,-1),
  133. \                         mode="or")
  134.     stable_blink = mt_hysteresis(background.mt_invert, blink)
  135.     blink2 = (STABILIZE) ? stable_blink : blink
  136.  
  137. # Shrinking the blink mask can get rid of noise,
  138. # too much will lose signal as well.
  139.     blink3 = blink2.mt_inpand(mode=mt_diamond(inpand))
  140.    
  141. # Using just pixels that changed helps sharpen the mask
  142.     diff   = ng_diff(clp.SelectEvery(1,-1), clp)
  143.     diff_2 = mt_logic(diff, diff.SelectEvery(1,1), mode="and")
  144.  
  145. #Hysteresis
  146. # Matches continuous blocks of pixels.
  147. # Use with care, will match the whole screen on fades.
  148.     hyster_blink = mt_hysteresis(blink3, diff_2)
  149.  
  150. # Expand the mask to make up for shrinking it (or just use hysteresis)
  151.     blink4 = blink3.mt_expand(mode=mt_circle(expand))
  152.     sharp_blink = mt_logic(blink4, diff_2, mode="and")
  153.  
  154.     blink5 = (HYSTER) ? hyster_blink :
  155. \            (SHARP)  ? sharp_blink  : blink4
  156.  
  157.    
  158. # A flash won't match blocks 1 or 2 frames away.
  159.     sub_flash = mt_logic(fmask_2, bmask_2, mode="and")
  160.     flash     = mt_logic(blink, sub_flash, mode="and")
  161.    
  162. # A shake changes in one frame and changes back in the next.
  163. # This isn't detected by the motion vectors because the blocks exist in
  164. # both frames, they are just shifting around.
  165.     same   = ng_same(clp.SelectEvery(1,-1), clp.SelectEvery(1,1))
  166.     shake  = mt_logic(same, diff_2, mode="and")
  167.  
  168.     (TEST) ? stackhorizontal(clp, mergeRGB(blink5, flash, shake))
  169. \          : blink5.GreyScale()
  170. }
  171.  
  172. function ng_diff(clip A, clip B, int "thr"){
  173.     thr=default(thr,0)
  174.     TAD=ng_TAD(A,B)
  175.     return mt_binarize(TAD, threshold=thr)
  176. }
  177.  
  178. function ng_same(clip A, clip B, int "thr"){
  179.     thr=default(thr,0)
  180.     TAD=ng_TAD(A,B)
  181.     return mt_binarize(TAD, threshold=thr, upper=true)
  182. }
  183.  
  184. function ng_TAD(clip A, clip B){
  185.     R=ng_AD(A  .showRed("YV12"),B  .showRed("YV12"))
  186.     G=ng_AD(A.showGreen("YV12"),B.showGreen("YV12"))
  187.     B=ng_AD(A .showBlue("YV12"),B .showBlue("YV12"))
  188.     return ng_plus(R, ng_plus(G, B))
  189. }
  190.  
  191. function ng_AD(clip A, clip B){
  192.     return mt_lutxy(A,B,"x y - abs")
  193. }
  194.  
  195. function ng_plus(clip A, clip B){
  196.     return mt_lutxy(A,B,"x y +")
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement