Advertisement
Guest User

Untitled

a guest
Nov 14th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Avisynth 11.70 KB | None | 0 0
  1. # Multisegment AVI import
  2. function AppendSegment(
  3. \   clip sample,
  4. \ string base,
  5. \    int first_val,
  6. \    int last_val,
  7. \ string format,
  8. \   bool hd
  9. \){
  10.     filename = base + string(first_val, format) + ".avi"
  11.     result   = hd \
  12.         ? AviSource(filename).PointResize  (sample.width, sample.height) \
  13.         : AviSource(filename).LanczosResize(sample.width, sample.height)
  14.     return (first_val < last_val) \
  15.         ? result + sample.AppendSegment(base, first_val+1, last_val, format, hd) \
  16.         : result
  17. }
  18.  
  19. function Remove(
  20. \   clip c,
  21. \    int start,
  22. \    int end
  23. \){
  24.     c
  25.     Trim(0, -start) + Trim(end, 0)
  26. }
  27.  
  28. function Replace(
  29. \   clip dest,
  30. \    int start,
  31. \    int end,
  32. \   clip src
  33. \){
  34.     Assert(start >= 0, "Replace: parameter 'start' is negative")
  35.     Assert(  end >= 0, "Replace: parameter 'end' is negative")
  36.     p1 = dest.Trim(0, -start)
  37.     p2 =  src.Trim(start, end)
  38.     p3 = dest.Trim(end + 1, 0)
  39.     p1 = (start == 0) ? dest.Trim(0, -1).DeleteFrame(0) : p1
  40.     p3 = (  end == 0) ? dest.Trim(0, -1).DeleteFrame(0) : p3
  41.     p1 + p2 + p3
  42.     return (dest.HasAudio) ? last.AudioDub(dest) : last
  43. }
  44.  
  45. function TASBlend(
  46. \   clip c,
  47. \  float "ratio"
  48. \){
  49.     # reduces framerate to 1/2 but leaves flicker effects partly visible
  50.     # blends frame pairs with alternating opacity
  51.     # The default for this is 2/3+1/3;1/3+2/3)
  52.     # optional "ratio" is the opacity of the first frame out of the four
  53.     ratio    = default(ratio, 2.0 / 3)
  54.     opacity1 = round((1 - ratio) * 257)
  55.     opacity2 = round((ratio) * 257)
  56.     c
  57.     Interleave( \
  58.         Layer(SelectEvery(4, 0), SelectEvery(4, 1), level=opacity1), \
  59.         Layer(SelectEvery(4, 2), SelectEvery(4, 3), level=opacity2) \
  60.     )
  61. }
  62.  
  63. # HD encodes need big subtitle fonts, but AviSynth can't enlarge the halo.
  64. # Here's the custom subtitle function by nanogyth
  65. # It's slow, but we have subtitles for a limited time only
  66. function ng_bighalo(
  67. \   clip clp,
  68. \ string text,
  69. \  float "x",
  70. \  float "y",
  71. \    int "first_frame",
  72. \    int "last_frame",
  73. \ string "font",
  74. \  float "size",
  75. \    int "text_color",
  76. \    int "halo_color",
  77. \    int "align",
  78. \    int "spc",
  79. \    int "lsp",
  80. \  float "font_width",
  81. \  float "font_angle",
  82. \    int "halo_radius",
  83. \ string "mode",
  84. \ string "halo",
  85. \ string "shadow"
  86. \){
  87.  
  88. #Version 16-17 2012/12/1
  89. # - added circle_12 as default
  90. # - reworked math to avoid bitwise functions that are only
  91. #   availible in experimental builds
  92. #Version 15 2012/12/1
  93. # - drop shadow added
  94. # - halo_radius still works, but calculating the points of
  95. #   a hollow circle for halo is much faster
  96. # - mode is depricated since memo-ing isn't as needed
  97.  
  98.     first_frame = default(first_frame, 0)
  99.     last_frame  = default( last_frame, first_frame + 299)
  100.     font        = default(       font, "Arial")
  101.     size        = default(       size, 112)
  102.     x           = default(          x, -1)
  103.     y           = default(          y, -1)
  104.     text_color  = default( text_color, $00DDDDDD)
  105.     halo_color  = default( halo_color, $80000000)
  106.     align       = default(      align, 5)
  107.     spc         = default(        spc, 0)
  108.     lsp         = default(        lsp, 0)
  109.     font_width  = default( font_width, 0)
  110.     font_angle  = default( font_angle, 0)
  111.     shadow      = default(     shadow, "")
  112.     w           = clp.width
  113.     h           = clp.height
  114. ###
  115.    ###
  116.       ##
  117.         #
  118.          #
  119.           #
  120.           #
  121.            #
  122.            #
  123.            #
  124.             #
  125.             #
  126. #           #
  127. circle_12="0 0 "+\
  128. "12 0 0 12 -12 0 0 -12 "+\
  129. "1 12 2 12 3 11 4 11 5 11 6 10 7 10 8 9 "+\
  130. "12 1 12 2 11 3 11 4 11 5 10 6 10 7 9 8 "+\
  131. "-1 12 -2 12 -3 11 -4 11 -5 11 -6 10 -7 10 -8 9 "+\
  132. "-12 1 -12 2 -11 3 -11 4 -11 5 -10 6 -10 7 -9 8 "+\
  133. "-1 -12 -2 -12 -3 -11 -4 -11 -5 -11 -6 -10 -7 -10 -8 -9 "+\
  134. "-12 -1 -12 -2 -11 -3 -11 -4 -11 -5 -10 -6 -10 -7 -9 -8 "+\
  135. "1 -12  2 -12 3 -11 4 -11 5 -11 6 -10 7 -10 8 -9 "+\
  136. "12 -1  12 -2 11 -3 11 -4 11 -5 10 -6 10 -7 9 -8 "
  137.     halo = (defined(halo_radius) && halo_radius != 0) ? mt_circle(halo_radius) : circle_12
  138.  
  139.     invis = BlankClip(1, w, h, pixel_type="YV12")
  140.     tm = Subtitle(invis, text, x, y, 0, 0, font, size, $00FFFFFF,\
  141.         0, align, spc, lsp, font_width, font_angle)
  142.     text_mask = tm.mt_expand(mode="0 0 "+shadow, chroma="-128")
  143.     halo_mask = text_mask.mt_expand(mode=halo, chroma="-128")
  144.    
  145.     h0      = halo_color%$01000000
  146.     h_color = (h0>=0) ? h0 : $01000000+h0
  147.     t0      = text_color%$01000000
  148.     t_color = (t0>=0) ? t0 : $01000000+t0
  149.     h_alpha = (halo_color>=0) ? 255-(halo_color/$01000000) : (-halo_color-1)/$01000000
  150.     t_alpha = (text_color>=0) ? 255-(text_color/$01000000) : (-text_color-1)/$01000000
  151.     lut_str = string(h_alpha)+ " x * 255 / " +string(t_alpha)+\
  152.               " " +string(h_alpha)+ " - y * 255 / +"
  153.  
  154.     alpha_mask = mt_lutxy(halo_mask, text_mask, lut_str)
  155.  
  156.     hc = BlankClip(1, w, h, color=h_color)
  157.     tc = Subtitle(hc, text, x, y, 0, 0, font, size, t_color,\
  158.         0, align, spc, lsp, font_width, font_angle)
  159.     overlay = tc.Mask(alpha_mask.ConvertToRGB32())
  160.    
  161.     clp.ApplyRange(first_frame, last_frame, "Layer", overlay)
  162. }
  163.  
  164. # nanogyth's deblink.
  165. function ng_deblink(
  166. \   clip clp,
  167. \  float "ratio",
  168. \    int "level",
  169. \   clip "blinkmask"
  170. \){
  171. #Version 10 2012.04.22
  172.  
  173.     blink = default(blinkmask, clp.ng_blinkmask())
  174.  
  175.     ratio = default(ratio, 2.0 /3)
  176.     assert(ratio >= 0.0 && 1.0 >= ratio,
  177.     \      "[ng_deblink] 1.0 >= ratio >= 0.0, it was " + string(ratio))
  178.  
  179.     level = default(level, round(ratio * 257))
  180.     assert(level >= 0 && 257 >= level,
  181.     \      "[ng_deblink] 257 >= level >= 0, it was " + string(level))
  182.  
  183.     m01=mt_logic(blink.SelectEvery(4,0),
  184. \                blink.SelectEvery(4,1),
  185. \                mode="or").ConvertToRGB32()
  186.     m23=mt_logic(blink.SelectEvery(4,2),
  187. \                blink.SelectEvery(4,3),
  188. \                mode="or").ConvertToRGB32()
  189.  
  190.     f0=Layer(clp.SelectEvery(4,0),
  191. \            clp.SelectEvery(4,1).Mask(m01),
  192. \            level=level)
  193.     f1=Layer(clp.SelectEvery(4,1),
  194. \            clp.SelectEvery(4,0).Mask(m01),
  195. \            level=level)
  196.     f2=Layer(clp.SelectEvery(4,2),
  197. \            clp.SelectEvery(4,3).Mask(m23),
  198. \            level=(257-level) )
  199.     f3=Layer(clp.SelectEvery(4,3),
  200. \            clp.SelectEvery(4,2).Mask(m23),
  201. \            level=(257-level) )
  202.  
  203.     Interleave(f0,f1,f2,f3)
  204. }
  205.  
  206. function ng_blinkmask(clip clp,
  207. \   bool "TEST",
  208. \   bool "STABILIZE",
  209. \   bool "SHARP",
  210. \   bool "HYSTER",
  211. \    int "inpand",
  212. \    int "expand",
  213. \    int "ml"
  214. \){
  215. #Version 10 2012.04.22
  216.  
  217. #BLINK
  218. # Blinking is a block that alternates on/off each frame
  219. # SelectEven would only see either the on or the off
  220.  
  221. #FLASH
  222. # Flashing is a block that is only on for a single frame
  223. # SelectEven might miss the flash
  224.  
  225. #SHAKE
  226. # Shaking is a block that moves back/forth each frame
  227. # SelectEven would only see one position
  228.  
  229. # The goal of this function is to make a blink mask for use with
  230. # ng_deblink. For overly complicated scenes where a clean blinkmask
  231. # can't be found, just use TASBlend. Uniform softness looks better
  232. # than sharp artifacts.
  233.  
  234. # This function calculates flash and shake info for the test script,
  235. # but those effects should be handled in different ways.
  236. # Flash - choose frames to make sure the flash is in your final clip.
  237. # Shake - SelectEvery(4,0,2,1,3) or SelectEvery(4,1,0,2,3)
  238. # SelectEvery doesn't generally work because it messes with the fluidity
  239. # of motion. But that won't be noticable on a shaking screen.
  240. # Be careful if 2 frame blinking is present, as the selectevery can turn
  241. # it into 1 frame blinking.
  242.  
  243.     TEST      = default(     TEST, false)
  244.     STABILIZE = default(STABILIZE, true)
  245.     SHARP     = default(    SHARP, true)
  246.     HYSTER    = default(   HYSTER, false)
  247.     inpand    = default(   inpand, 1)
  248.     expand    = default(   expand, 1)
  249.     ml        = default(       ml, 128)
  250.  
  251. # The functions used to make the masks work in the YV12 colorspace. Once
  252. # the masks are created they can be used in the RGB32 colorspace direcly
  253.     src=clp.ConvertToYV12()
  254.  
  255. # Blinking is located by looking for blocks that don't exist in
  256. # consecutive frames. The motion vector will match blocks that exist in
  257. # both frames. The blocks that aren't in both will end up with huge
  258. # values that are picked out by the motion mask.
  259.     super = MSuper(src, pel=1)
  260.     fvec  = MAnalyse(super, isb=false, blksize=4)
  261.     bvec  = MAnalyse(super, isb=true , blksize=4)
  262.     fmask = Mmask(src, fvec, kind=1, ml=ml).mt_binarize()
  263.     bmask = Mmask(src, bvec, kind=1, ml=ml).mt_binarize()
  264.     blink = mt_logic(fmask, bmask, mode="and")
  265.  
  266. # Blinking usually occurs against a stable background. This is found
  267. # by looking at blocks 2 frames apart. This distinguishes a blink from
  268. # blocks that are just changing every frame.
  269.     ee_src   = src.SelectEven()
  270.     ee_super = MSuper(ee_src, pel=1)
  271.     ee_fvec  = MAnalyse(ee_super, isb=false, blksize=4)
  272.     ee_bvec  = MAnalyse(ee_super, isb=true , blksize=4)
  273.     ee_fmask = Mmask(ee_src, ee_fvec, kind=1, ml=ml).mt_binarize()
  274.     ee_bmask = Mmask(ee_src, ee_bvec, kind=1, ml=ml).mt_binarize()
  275.  
  276.     oo_src   = src.SelectOdd()
  277.     oo_super = MSuper(oo_src, pel=1)
  278.     oo_fvec  = MAnalyse(oo_super, isb=false, blksize=4)
  279.     oo_bvec  = MAnalyse(oo_super, isb=true , blksize=4)
  280.     oo_fmask = Mmask(oo_src, oo_fvec, kind=1, ml=ml).mt_binarize()
  281.     oo_bmask = Mmask(oo_src, oo_bvec, kind=1, ml=ml).mt_binarize()
  282.  
  283.     fmask_2   = Interleave(ee_fmask, oo_fmask)
  284.     bmask_2   = Interleave(ee_bmask, oo_bmask)
  285.     background = mt_logic(fmask_2.SelectEvery(1,1),
  286. \                         bmask_2.SelectEvery(1,-1),
  287. \                         mode="or")
  288.     stable_blink = mt_hysteresis(background.mt_invert, blink)
  289.     blink2 = (STABILIZE) ? stable_blink : blink
  290.  
  291. # Shrinking the blink mask can get rid of noise,
  292. # too much will lose signal as well.
  293.     blink3 = blink2.mt_inpand(mode=mt_diamond(inpand))
  294.    
  295. # Using just pixels that changed helps sharpen the mask
  296.     diff   = ng_diff(clp.SelectEvery(1,-1), clp)
  297.     diff_2 = mt_logic(diff, diff.SelectEvery(1,1), mode="and")
  298.  
  299. #Hysteresis
  300. # Matches continuous blocks of pixels.
  301. # Use with care, will match the whole screen on fades.
  302.     hyster_blink = mt_hysteresis(blink3, diff_2)
  303.  
  304. # Expand the mask to make up for shrinking it (or just use hysteresis)
  305.     blink4 = blink3.mt_expand(mode=mt_circle(expand))
  306.     sharp_blink = mt_logic(blink4, diff_2, mode="and")
  307.  
  308.     blink5 = (HYSTER) ? hyster_blink :
  309. \            (SHARP)  ? sharp_blink  : blink4
  310.  
  311.    
  312. # A flash won't match blocks 1 or 2 frames away.
  313.     sub_flash = mt_logic(fmask_2, bmask_2, mode="and")
  314.     flash     = mt_logic(blink, sub_flash, mode="and")
  315.    
  316. # A shake changes in one frame and changes back in the next.
  317. # This isn't detected by the motion vectors because the blocks exist in
  318. # both frames, they are just shifting around.
  319.     same   = ng_same(clp.SelectEvery(1,-1), clp.SelectEvery(1,1))
  320.     shake  = mt_logic(same, diff_2, mode="and")
  321.  
  322.     (TEST) ? stackhorizontal(clp, mergeRGB(blink5, flash, shake))
  323. \          : blink5.GreyScale()
  324. }
  325.  
  326. function ng_diff(clip A, clip B, int "thr"){
  327.     thr=default(thr,0)
  328.     TAD=ng_TAD(A,B)
  329.     return mt_binarize(TAD, threshold=thr)
  330. }
  331.  
  332. function ng_same(clip A, clip B, int "thr"){
  333.     thr=default(thr,0)
  334.     TAD=ng_TAD(A,B)
  335.     return mt_binarize(TAD, threshold=thr, upper=true)
  336. }
  337.  
  338. function ng_TAD(clip A, clip B){
  339.     R=ng_AD(A  .showRed("YV12"),B  .showRed("YV12"))
  340.     G=ng_AD(A.showGreen("YV12"),B.showGreen("YV12"))
  341.     B=ng_AD(A .showBlue("YV12"),B .showBlue("YV12"))
  342.     return ng_plus(R, ng_plus(G, B))
  343. }
  344.  
  345. function ng_AD(clip A, clip B){
  346.     return mt_lutxy(A,B,"x y - abs")
  347. }
  348.  
  349. function ng_plus(clip A, clip B){
  350.     return mt_lutxy(A,B,"x y +")
  351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement