Advertisement
sonkai

AutoBlockNR.avsi

Dec 22nd, 2014
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #----------------------------------------------------------------------------------------------------------
  2. #
  3. # AutoBlockNR.avsi
  4. #
  5. # 概要:
  6. #  フィールド単位の色差成分の情報を利用して、MPEG2のブロックノイズを低減する
  7. #
  8. # 説明:
  9. #  MPEG2のブロックノイズはフィールド単位のノイズで、かつ色差成分に顕著に反映されるノイズのようです。
  10. #  この特性を利用して、色差成分のノイズが少ないフィールドをBOB化することにより良好なソースを得られる可能性があります。
  11. #
  12. # 使い方:
  13. #  Usageを参照してください。
  14. #
  15. # 動作に必要なDLL:
  16. #  TDeint.dll
  17. #  nnedi3.dll
  18. #  TMM.dll
  19. #  Masktools
  20. #  Masktools v2
  21. #----------------------------------------------------------------------------------------------------------
  22.  
  23. #----------------------------------------
  24. # 関数サマリ
  25. #----------------------------------------
  26. #Function AutoBlockNR() : クリップを受け取り、ブロックノイズを低減して返す
  27. #Function AutoBlockNR_InterpolateByField() : 指定されたフレームを、片フィールド補間して返す
  28. #Function AutoBlockNR_Bob() : 片フィールド補間に使うBob化関数。本番用
  29. #Function AutoBlockNR_BobForTest() : 片フィールド補間に使うBob化関数。テスト用
  30. #Function Combcheck(clip c,bool "test",int "thY") : ノイズの強調表示関数。「にわとり遊び」のささみし様の関数を流用させて頂きました。
  31.  
  32. #----------------------------------------
  33. # AutoBlockNR
  34. #----------------------------------------
  35. # 処理:クリップを受け取り、ブロックノイズを低減して返す
  36. # 引数:
  37. # ・th_noise_diff フィールド間にどれだけノイズ差があったら補間を行うか。値が小さいほど補間フレームは多くなりますが、誤爆も多くなります。デフォルトは12.0
  38. # ・tff トップフィールドファーストならtrue・・・のつもり。BOB化して汚くなるようならfalseに設定してみてください。デフォルトはtrue
  39. # ・test テストモードならtrue。テストモードでは各種情報が表示されるほか、BOB化に軽量関数を使うのでプレビューが早くなります。デフォルトはfalse
  40. # 返し値:ブロックノイズを低減したクリップ
  41. Function AutoBlockNR(clip c,float "th_noise_diff",bool "tff",bool "test")
  42. {
  43.   # 各種グローバル関数。ScriptClipに渡すため
  44.   global g_autoblocknr_th_noise_diff=Default(th_noise_diff,12.0)
  45.   global g_autoblocknr_tff=Default(tff,true)
  46.   global g_autoblocknr_test=Default(test,false)
  47.  
  48.   # メイン処理
  49.   c.ScriptClip("""
  50.    src=last
  51.  
  52.    # 色差ノイズを強調表示し、CombCheck()でノイズの量を測定する
  53.    SeparateFields()
  54.    Tweak(sat=5.0)
  55.    UToY()
  56.    CombCheck(test=false)
  57.    noise_t=Trim(current_frame*2,-1).AverageLuma()
  58.    noise_b=Trim(current_frame*2+1,-1).AverageLuma()
  59.    noise_diff=Abs(noise_t-noise_b)
  60.  
  61.    # フィールド間のノイズの差がしきい値を越えていたら片フィールド補間。越えていなければ素通し
  62.    src
  63.    (noise_diff<g_autoblocknr_th_noise_diff)
  64.    \? last
  65.    \: (noise_t > noise_b)
  66.      \? AutoBlockNR_InterpolateByField(current_frame,true,g_autoblocknr_tff,g_autoblocknr_test)
  67.      \: AutoBlockNR_InterpolateByField(current_frame,false,g_autoblocknr_tff,g_autoblocknr_test)
  68.  
  69.    # テストモード用の各種情報の用意
  70.    offset_x=10
  71.    offset_y=10
  72.    op=(noise_diff<g_autoblocknr_th_noise_diff)
  73.    \? "none"
  74.    \: (noise_t > noise_b)
  75.      \? "bottom field"
  76.      \: "top field"
  77.  
  78.    # テストモードでの各種情報の表示。色々表示したいが、載せるほど動作が重く・・・
  79.    (g_autoblocknr_test==true)
  80.    \?
  81.      \ SubTitle("noise_diff "+String(noise_diff,"%.2f"),offset_x,offset_y,font="lucida console")
  82.      \.SubTitle("bob        "+op,offset_x,offset_y+20,font="lucida console")
  83.    \: last
  84.    
  85.    return last
  86.  """)
  87.  
  88.   return last
  89. }
  90.  
  91. #----------------------------------------
  92. # AutoBlockNR_InterpolateByField
  93. #----------------------------------------
  94. # 処理:指定されたフレームを、片フィールド補間して返す
  95. # 引数:
  96. # ・frame 処理したいフレーム番号
  97. # ・top トップフィールドが汚れているならtrue
  98. # ・tff トップフィールドファーストならtrue
  99. # ・test テストモードならtrue
  100. # 返し値: BOB化クリップ
  101. Function AutoBlockNR_InterpolateByField(clip c,int frame,bool top,bool tff,bool test)
  102. {
  103.   # トップフィールドとボトムフィールド、どちらをBob化するか判断。引数で指定されるのは汚れたフィールドであることに注意
  104.   offset=(tff==true)
  105.   \? (top==true) ? 1 : 0
  106.   \: (top==true) ? 0 : 1
  107.  
  108.   # フィールドのBOB化
  109.   c
  110.   bobed=(test==true)?AutoBlockNR_BobForTest():AutoBlockNR_Bob()
  111.   field=frame*2+offset
  112.   fixed=bobed.Trim(field,-1)
  113.   fixed=fixed.AssumeFPS(c.FrameRate)
  114.  
  115.   return fixed
  116. }
  117.  
  118. #----------------------------------------
  119. # AutoBlockNR_BobForTest
  120. #----------------------------------------
  121. # 処理:テストモード用のBOB化関数
  122. # 引数:
  123. # 返し値: BOB化フレーム
  124. Function AutoBlockNR_BobForTest(clip c)
  125. {
  126.   return c.Bob()
  127. }
  128.  
  129. #----------------------------------------
  130. # AutoBlockNR_BobForTest
  131. #----------------------------------------
  132. # 処理:本番用のBOB化関数
  133. # 引数:
  134. # 返し値: BOB化フレーム
  135. Function AutoBlockNR_Bob(clip c)
  136. {
  137.   c
  138.  
  139.   interp=nnedi3(field=-2)
  140.   mask=tmm(mode=1)
  141.   TDeint(mode=1, edeint=interp, emask=mask)
  142.  
  143.   return last
  144. }
  145.  
  146. #----------------------------------------
  147. # CombCheck
  148. #----------------------------------------
  149. # 処理:ノイズの強調表示関数。「にわとり遊び」のささみし様の関数を流用させて頂きました。
  150. Function Combcheck(clip c,bool "test",int "thY")
  151. {
  152.   test=Default(test,false)
  153.   thY=Default(thY,16)
  154.  
  155.   c
  156.   c1=(test==true)?Grayscale():c.BlankClip(color=$000000)
  157.   c2=(test==true)?Grayscale().ColorYUV(gain_u=100,gain_v=100):c.BlankClip(color=$FFFFFF)
  158.   mask1=CombMask(thY1=thY,thY2=thY)
  159.   mt_merge(c1,c2,mask1,luma=true)
  160.  
  161.   return last
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement