Advertisement
Guest User

FixBlendIVTC

a guest
Aug 28th, 2016
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ################################
  2. ## FixBlendIVTC  by MOmonster ##
  3. ################################
  4.  
  5.  
  6. ## FixBlendIVTC is a blend replacing / frame restoring function for doubleblends, caused by blenddeinterlacing
  7. ## of telecined sources. It will only work for this special case and is not created for any other conversions.
  8. ##
  9. ## Use import("FixBlendIVTC.avs") in your script and load the necessary filters to be able using this function
  10. ##
  11. ## required filters:
  12. ##      - Average
  13. ##      - mt_masktools
  14. ##      - removegrain
  15. ##      - TIVTC or Decomb for decimating (external)
  16. ## version:
  17. ##      - v0.9b  - 29.08.2016 for new Average
  18. ##
  19. ## Thanks to Manao, Clouded and Kassandro for the really useful plugins,
  20. ## and of course also to foxyshadis, who gives me the idea and many inspirations.
  21.  
  22.  
  23.  
  24. ## sample1: source              #progressive
  25. ##      FixBlendIVTC(sbd=true)
  26. ##      decimate(cycle=5,quality=0) #recommed decimating
  27. ##
  28. ## sample2: source
  29. ##      FixBlendIVTC(post="""pp2.deen("a2d")""")
  30. ##      tdecimate(rate=23.976,mode=7)
  31.  
  32.  
  33. ## parameter description:
  34. ##  post
  35. ##      It's the parameter for the postprocessing. These are the modes:
  36. ##          0 ->    the fastest mode, no postprocessing
  37. ##          1 ->    like post=0 but with chromablurring
  38. ##          2 ->    use difference masking, higher quality and still good speed         [0...6  ->2]
  39. ##          3 ->    like post=2 but with chromablurring
  40. ##          4 ->    use a special blurring mask on luma and chroma that reduces artefacts
  41. ##          5 ->    combines post 2 and 4 but without chromapostprocessing
  42. ##          6 ->    like post=5 but with extra chromaprocessing (slowest)
  43. ##      If these postprocessing modes aren't strong or individual enough for you you can also use your own
  44. ##      favourit filter on the restored frames. Therefore set a string with your setted filters.
  45. ##      Use pp0 till pp6 for the postprocessing mode and add your filter like this:
  46. ##          post="pp4.blur(1)"      #post=4 + blurring
  47. ##      If you want to use a filter with inputstrings use three quotation marks like in the second example.
  48. ##  bthresh
  49. ##      The blendthreshold can be used to make the blenddetection less aggressive if there are some wrong detections.
  50. ##                                                      [0...2.0  ->0.1]
  51. ##  mthresh
  52. ##      ItΒ΄s used for (m)otion (thresh)olding. It regulates the blenddetection of frames with small pixelvaluedifferences.
  53. ##      A better quality of the source allows lower values and a more accurate detection. DonΒ΄t change this parameter too
  54. ##      much. It has a high influence on the double and single blenddetection.              [0...1.0  ->0.1]
  55. ##  sbd
  56. ##      The (s)ingle(b)lend(d)etection value is a boolean, Set it true for 12fps animations and so on.  [bool  ->false]
  57. ##  dclip
  58. ##      The (d)etection(clip) you can set to improve the blenddetection (cleaning the clip before).
  59. ##      This clip is only used for the blenddetection and not for output.
  60.  
  61.  
  62.  
  63.  
  64. Function FixBlendIVTC(clip input, "post", float "bthresh", "mthresh", bool "sbd", clip "dclip")
  65. {
  66. ###### PREPARATION ######
  67. global pp   = default(post, 6)                      #(p)ost(p)rocessing string
  68. global mthresh  = default(mthresh, 0.1)                     #(m)otion(thresh)old
  69. global thresh   = 1.01
  70. global sbd  = default(sbd,false)                        #(s)ingle(b)lend(d)etection
  71.  
  72. rate    = framerate(input)
  73.  
  74.  
  75. ###### DETECTION CLIPS ######
  76. dclip   = default(dclip,input)                          #(d)etection clip
  77.  
  78. o_diff  = mt_makediff(dclip.trim(1,0), dclip.trim(4,0))
  79. c_diff  = mt_makediff(dclip.trim(2,0), dclip.trim(3,0))
  80. global re_lut   = mt_lutxy(o_diff, c_diff, yexpr="x 128 - abs 126 > y 128 - abs 63 > & 0 x 128 + y 2 * - 2 ^ x 128 + y 2 * - abs 0.8 ^ - y 128 - abs 1 + 0.5 ^ / ?", uexpr="x", vexpr="x")
  81. global sdiff    = mt_lut(c_diff, yexpr="x 128 - 2 ^ 12 -", uexpr="x", vexpr="x").mt_inpand()
  82.  
  83.  
  84. ###### POSTPROCESSING ######
  85. unblend1 = Average(input, 2.0, input.duplicateframe(0), -1.0)
  86. unblend2 = Average(input.trim(2,0), -1.0, input.trim(1,0), 2.0)
  87.  
  88. qmask1  = mt_makediff(unblend1.RemoveGrain(mode=19, modeU=-1, modeV=-1), unblend1)
  89. qmask2  = mt_makediff(unblend2.RemoveGrain(mode=19, modeU=-1, modeV=-1), unblend2)
  90. bmask   = mt_lutxy(qmask1, qmask2, yexpr="x 128 - abs y 128 - abs == 128 x 128 - abs 3 + y 128 - abs < 0 y 128 - abs 3 + x 128 - abs < 255 x 128 - abs y 128 - abs < 1 254 ? ? ? ?", uexpr="x", vexpr="x")
  91.  
  92. diff    = mt_lutxy(input.duplicateframe(0), input, yexpr="x y - abs", uexpr="x", vexpr="x").mt_expand()
  93. dmask   = mt_lutxy(diff,diff.trim(2,0), yexpr="x 2 * y < x 4 < & 0 y 2 * x < y 4 < & 255 x x y + / 200 * 28 + ? ?", uexpr="x", vexpr="x")
  94. pmask   = mt_lutxy(dmask, bmask, yexpr="y 0 > y 255 < & x 0 == x 255 == | & x y ?", uexpr="x", vexpr="x")
  95.  
  96. pp0     = Average(input.trim(2,0), -0.5, input.trim(1,0), 1.0, input, 1.0, input.duplicateframe(0), -0.5)
  97. pp1     = pp0.RemoveGrain(mode=0, modeU=12, modeV=12)
  98. pp2     = mt_merge(unblend1, unblend2, dmask.RemoveGrain(mode=12, modeU=-1, modeV=-1).greyscale(), Y=3, U=3, V=3)
  99. pp3     = pp2.RemoveGrain(mode=0, modeU=12, modeV=12)
  100. pp4     = mt_merge(unblend1, unblend2, bmask.RemoveGrain(mode=12, modeU=-1, modeV=-1), luma=true)
  101. pp5     = mt_merge(unblend1, unblend2, pmask.RemoveGrain(mode=12, modeU=-1, modeV=-1).greyscale(), Y=3, U=3, V=3)
  102. pp6 = mt_merge(pp5, unblend2, bmask.RemoveGrain(mode=12, modeU=-1, modeV=-1), Y=3, U=2, V=2).RemoveGrain(mode=0, modeU=12, modeV=12)
  103.  
  104.  
  105. ###### OUTPUT ######
  106. global source   = input
  107. global final    = IsString(pp) ? Eval(pp) : pp==1 ? pp1 : pp==2 ? pp2 : pp==3 ? pp3 : pp==4 ? pp4 : pp==5 ? pp5 : pp==6 ? pp6 : pp0
  108.  
  109.  
  110. ###### VAR.. ######
  111. global fdc0 = 1.0                               #(f)rame(d)ifference
  112. global fdn1     = 1.0
  113. global fdn2     = 1.0
  114.  
  115. global rvp1 = 1.0                               #(r)estore(v)alue
  116. global rvc0     = 1.0
  117. global rvn1     = 1.0
  118. global rvn2     = 1.0
  119.  
  120. global counter  = 2                             #pattern count variable
  121.  
  122.  
  123. ###### Conditional Function Chain, evaluated from bottom to top (!) ######
  124. ScriptClip(source, " counter==0 ? final : counter==1 ? source.trim(1,0) : source ")
  125.  
  126. FrameEvaluate(last, "
  127.     global fdp1 = fdc0
  128.     global fdc0 = fdn1
  129.     global fdn1 = fdn2
  130.     global fdn2 = AverageLuma(sdiff)
  131.  
  132.     global rvp2 = rvp1
  133.     global rvp1 = rvc0
  134.     global rvc0 = rvn1
  135.     global rvn1 = rvn2
  136.     global rvn2     = AverageLuma(re_lut)
  137.  
  138.     bcalc   = rvc0<rvp2 && rvc0<rvp1 && rvc0<rvn1 && rvc0<rvn2 ?
  139.         \ (rvp2<rvp1 && rvp2<rvn1 && rvp2<rvn2 ? rvp2 : rvp1<rvn1 && rvp1<rvn2 ? rvp1 : rvn1<rvn2 ? rvn1 : rvn2) / rvc0 :
  140.         \ rvc0<0.25*rvp1 && rvc0<0.25*rvn1 && (rvc0<0.5*rvp2 || rvc0<0.5*rvn2) ?
  141.         \ -0.25 * (2*rvp2<rvp1 && 2*rvp2<rvn1 && rvp2>rvn2 ? 2*rvp2 : 2*rvn2<rvp1 && 2*rvn2<rvn1 && rvn2>rvp2 ? 2*rvn2 : rvp1<rvn1 ? rvp1 : rvn1) / rvc0 : 0
  142.  
  143.     global counter  = counter==-1 ? 1 : (abs(bcalc)>1.0 && rvc0<10*mthresh || abs(bcalc)>thresh) &&
  144.             \ (counter>3 || bcalc>0) && (fdp1>mthresh || fdn1>mthresh || fdp1>0.5*fdc0 || fdn1>0.5*fdc0) ? 0 : counter+1
  145.  
  146.     global counter  = counter!=0 || sbd==false ? counter : fdp1<fdn1 && fdp1<mthresh ? -1 : fdn1<mthresh || fdc0<mthresh ? 1 : 0
  147.         ")
  148.            
  149. last.changefps(rate*2).changefps(rate,linear=true).addborders(8,0,0,0).crop(8,0,-0,-0)
  150.  
  151. return(last)  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement