Advertisement
Guest User

DecombUCF

a guest
Feb 8th, 2017
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Avisynth 27.73 KB | None | 0 0
  1. function DecombUCF(clip s, float "y1",float "y2",float "y3",float "y4",float "y5",float "x1",float "x2",float "x3",float "x4",float "x5",float "off_t",float "off_b"
  2.                                              \ ,int "th_mode", float "fd_thresh", float "namax_thresh",int "namax_diff"
  3.                                              \ ,float "nrt1y",float "nrt2y",float "nrt2x",float "nrw",string "nr", int "chroma", int "debug", string "txt_file", int "frame_cache") {
  4. ## Decomb using clean field v3.13 ## #mod to use tmm2 in avs2.6 by A.SONY, need Animeivtc 2017-2-8 or up
  5. # needed dlls = Tdeint.dll, TMM.dll, NNEDI3.dll, TIVTC.dll, variableblur.dll, SmoothAdjust.dll, WarpSharp.dll
  6. #正確な逆テレシネ後にも残ってしまう圧縮ノイズ由来の縞を、綺麗なフィールドを用いて補間・除去します。フィールドが共に汚い場合はBobではなく、任意のフィルタを掛けられます。
  7. #補完対象フレームを増やしすぎると反ってファイルサイズ増えます(x264の動き予測に悪影響)。動きすぎて汚いフレームのみ推奨。
  8. #未実装 : 高輝度は補完しない(高輝度ノイズは気付きにくい), 追加判定(輝度変化/field diff/FrameDiff),細かい模様が多いと共通部分が少なくなりnavgが急増・diff減少,nr機能向上
  9.     [*    1st-stage analysis    *]
  10.     fd_thresh = Default(fd_thresh, 128) #[0-] #threshold of FieldDiff #fd_thresh = FieldDiff * 100 / (Width * Height)
  11.     [*    2nd-stage analysis    *]
  12.     chroma = Default(chroma,1) #[0-2] #(0:Y),(1:UV),(2:YUV) for noise detection
  13.     ## threshold
  14.     th_mode = Default(th_mode, 0) #[1-2:debug][3-7:normal][8-10:restricted] #preset of diff threshold. you can also specify threshold by x1-x5 y1-y5(need th_mode=0).
  15.     off_t = Default(off_t,0) #offset for diff threshold of top field (first field, top,diff<0)
  16.     off_b = Default(off_b,0) #offset for diff threshold of bottom field (second field, botom, 0<diff)
  17.     ## reverse (chroma=0のみで機能。ノイズ量の絶対値が多過ぎる場合、映像効果と考えノイズの大きいフィールドを残す(小さいほうはブロックノイズによる平坦化))
  18.     namax_thresh = Default(namax_thresh, 82) #82 #MX:90 #disabled with chroma=1 #[0-256] #upper limit of max noise for Noise detaction (75-80-83)
  19.     namax_diff = Default(namax_diff, 38) #30-40 #disabled with chroma=1 #If average noise >= namax_thresh,  use namax_diff as diff threshold.
  20.     ## NR
  21.     nrt1y = Default(nrt1y, 28)#28-29-30 #threshold for nr
  22.     nrt2y = Default(nrt2y, 36)#36-36.5-37 #exclusion range
  23.     nrt2x = Default(nrt2x, 53.5)#53-54-55 #exclusion range
  24.     nrw = Default(nrw, 2)#1-2#diff weight for nr threshold
  25.     global DecombUCF_nr = Default(nr, """NOOP()""")#TNLMeans_SSE4_TNLMeans(Ax=5,Ay=5,Az=0,Sx=6,Sy=6,Bx=5,By=5,a=0.5,h=Spline(nmax ,0,2 ,28,2 ,38,6 ,100,7 ,false))""")
  26.         #VerticalCleaner(mode=1,modeY=1,modeU=1,modeV=1).#閾値以上かつBOBが適用されないフレームに掛けるフィルタ(非常に汚いフレームのみ推奨)#NOOP()
  27.     ## others
  28.     nmin_y = 1         #[0-128] #lower limmit of luma noise for Noise detaction (1)
  29.     nrange_y = 128   #[0-128] #luma range of luma noise for Noise detaction (128)
  30.     nmin_uv = 1       #[0-128]
  31.     nrange_uv = 128 #[0-128]
  32.     debug = Default(debug,0) #[0-6] #(0:none),(1:Multi),(2:FieldDiff),(3:Show Noise),(4:Output frame list to txt in format for ConditionalReader),(5:Output frame list & stacked [C]fields as jpg),(6:Execute formal bob using external frame list),(7:Execute formal bob & output frame list)
  33.     frame_cache = Default(frame_cache, 10) #逆テレシネ関数はフレーム再取得(第一、二判定、BOB)すると結果が変わる場合があるのでCache必須 #(3-5-10) #WriteFile(txt_file,"current_frame")
  34.     Assert(debug!=4&&debug!=5&&debug!=6&&debug!=7||Defined(txt_file), "If you use debug4,5,6,7 in DecombUCF(), you need to set txt_file.")
  35.     txt_file = Default(txt_file, "nul")
  36.     global chroma_y = (chroma==1) ? 2 : 3
  37.     global chroma_uv = (chroma==0) ? 2 : 3
  38.     c = s.FrameCache(frame_cache)
  39.     ## diff threshold ## (x=nmin, y=diff)
  40.     #                                                                    5      1     2     3     4    5     6     7     8    9    10  
  41.     global y1=Select(th_mode, Default(y1, 15), 13, 14, 15, 15, 15, 15, 15, 17, 20, 22)
  42.     global y2=Select(th_mode, Default(y2, 20), 17, 18, 19, 20, 20, 21, 22, 25, 32, 37)
  43.     global y3=Select(th_mode, Default(y3, 23), 17, 20, 21, 23, 23, 23, 24, 28, 38, 44)
  44.     global y4=Select(th_mode, Default(y4, 45), 20, 40, 43, 43, 45, 45, 45, 47, 52, 52)
  45.     global y5=Select(th_mode, Default(y5, 63), 50, 50, 63, 63, 63, 63, 63, 64, 66, 66)
  46.    
  47.     global x1=Select(th_mode, Default(x1, 20), 20, 19, 20, 20, 20, 20, 20, 20, 21, 22)
  48.     global x2=Select(th_mode, Default(x2, 28), 28, 28, 28, 28, 28, 28, 28, 28, 30, 32)
  49.     global x3=Select(th_mode, Default(x3, 36), 32, 36, 36, 36, 36, 36, 35, 33, 36, 35)
  50.     global x4=Select(th_mode, Default(x4, 41), 37, 42, 41, 41, 41, 41, 41, 41, 40, 40)
  51.     global x5=Select(th_mode, Default(x5, 50), 50, 50, 53, 53, 50, 50, 50, 48, 48, 48)
  52.     function th_score(float "x"){
  53.         y =   (x<x1) ? y1
  54.             \ :(x<x2) ? ((y2-y1)*x+x2*y1-x1*y2)/(x2-x1)
  55.             \ :(x<x3) ? ((y3-y2)*x+x3*y2-x2*y3)/(x3-x2)
  56.             \ :(x<x4) ? ((y4-y3)*x+x4*y3-x3*y4)/(x4-x3)
  57.             \ :(x<x5) ? ((y5-y4)*x+x5*y4-x4*y5)/(x5-x4)
  58.             \             : y5
  59.         return y
  60.     }
  61.     function NOOP(clip c){c}
  62.     function DecombUCF_nr(clip c, float "nmax"){c Eval(DecombUCF_nr)}
  63.     ## Bob ##
  64.     c
  65.     TDeint(chroma=true,mode=1,edeint=nnedi3(field=-2),emask=tmm2_ortmm1(length=6,mode=1,nt=0,minthresh=0,maxthresh=75),map=0) #好きなBob関数(BOB関数によってはノイズ残る。comb-motion-noiseの3種類のマスクの方が良い) #判定結果に影響しません
  66.     global bob_t = SelectEven() #keep 1st field
  67.     global bob_b = SelectOdd() #keep 2nd field
  68.     ## Noise detection ##
  69.         # make diff # (検出したいノイズを特異的に変化させる)
  70.         function Affect_Noise(clip c){
  71.             c.binomialblur(3,3,chroma_y,chroma_uv,chroma_uv) #0.5,1.5が早い #好きなNRもしくはSharp。検出したいノイズに特異的に反応するものが良い。
  72.         }
  73.         function Limitter(clip c, int "nmin_y", int "nrange_y", int "nmin_uv", int "nrange_uv", int "chroma"){
  74.             c
  75.             range_y = nmin_y+nrange_y
  76.             range_uv = nmin_uv+nrange_uv
  77.             str_y = "(Y==128)? 128 : (Y<128)? ( ((127-"+String(range_y)+"<Y)(Y<128-"+String(nmin_y)+"))? 0 : 56 ) : ( ((128+"+String(nmin_y)+"<Y)(Y<129+"+String(range_y)+")) ? 255 : 199 )"
  78.             str_u = "(U==128)? 128 : (U<128)? ( ((127-"+String(range_uv)+"<U)(U<128-"+String(nmin_uv)+"))? 0 : 56 ) : ( ((128+"+String(nmin_uv)+"<U)(U<129+"+String(range_uv)+")) ? 255 : 199)"
  79.             str_v = "(V==128)? 128 : (V<128)? ( ((127-"+String(range_uv)+"<V)(V<128-"+String(nmin_uv)+"))? 0 : 56 ) : ( ((128+"+String(nmin_uv)+"<V)(V<129+"+String(range_uv)+")) ? 255 : 199)"
  80.             eval_y = Select(chroma, str_y, "Y", str_y)
  81.             eval_u = Select(chroma, "U", str_u, str_u)
  82.             eval_v = Select(chroma, "V", str_v, str_v)
  83.             return c.SmoothCustom(eval_y, eval_u, eval_v, false, 0, 0, -1)
  84.         }
  85.         c.SeparateFields()
  86.         global ori = Crop(4,4,-4,-4,true).BlankClip(color_yuv=$808080)
  87.         global con = merge( Affect_Noise().invert() ).Crop(4,4,-4,-4,true) .Limitter(nmin_y,nrange_y,nmin_uv,nrange_uv,chroma).FrameCache(2)
  88.         # calc diff # (元フレームからの変化を絶対値で数値化)
  89.             string1 = Select(chroma
  90.                                        \,"""c = last
  91.                            #絶対ノイズ量
  92.                            noise_t_y = LumaDifference( ori.SelectEven(),con.SelectEven() )
  93.                            noise_t_uv = -1
  94.                            noise_b_y = LumaDifference( ori.SelectOdd(),con.SelectOdd() )
  95.                            noise_b_uv = -1
  96.                            #絶対ノイズ-平均(reverseで利用)
  97.                            navg1_y =  ( noise_t_y + noise_b_y )/2 #Max(noise_t_y,noise_b_y)
  98.                            #相対ノイズ-平均 [comp t,b](diff計算で利用)
  99.                            navg2_y = LumaDifference( con.SelectEven(),con.SelectOdd() )/2
  100.                            #絶対ノイズ-符号付差分(diff計算で利用)
  101.                            diff1_y = noise_t_y - noise_b_y
  102.                            #パラメタ
  103.                            diff1 = diff1_y #絶対ノイズ-符号付差分
  104.                            navg1 = navg1_y #絶対ノイズ平均(総ノイズ量判定用)
  105.                            navg1_d = navg1_y #debug用
  106.                            navg2 = navg2_y #相対ノイズ-平均
  107.                          """
  108.                                        \,"""c = last
  109.                            #絶対ノイズ量
  110.                            noise_t_y = -1
  111.                            noise_t_uv = ( ChromaUDifference( ori.SelectEven(),con.SelectEven() ) + ChromaVDifference( ori.SelectEven(),con.SelectEven() ) )/2
  112.                            noise_b_y = -1
  113.                            noise_b_uv = ( ChromaUDifference( ori.SelectOdd(),con.SelectOdd() ) + ChromaVDifference( ori.SelectOdd(),con.SelectOdd() ) )/2
  114.                            #絶対ノイズ-平均(reverseで利用)
  115.                            navg1_uv = ( noise_t_uv + noise_b_uv )/2 #Max(noise_t_uv,noise_b_uv)
  116.                            #相対ノイズ-平均 [comp t,b](diff計算で利用)
  117.                            navg2_uv = ( ChromaUDifference( con.SelectEven(),con.SelectOdd() ) + ChromaVDifference( con.SelectEven(),con.SelectOdd() ) )/4
  118.                            #絶対ノイズ-符号付差分(diff計算で利用)
  119.                            diff1_uv = noise_t_uv - noise_b_uv
  120.                            #パラメタ
  121.                            diff1 = diff1_uv #絶対ノイズ-符号付差分
  122.                            navg1 = -1 #navg1_uv #絶対ノイズ平均(総ノイズ量判定用,色差の細かい模様は滅多に見ない)
  123.                            navg1_d = navg1_uv #debug用
  124.                            navg2 = navg2_uv #相対ノイズ-平均
  125.                          """
  126.                                        \,"""c = last
  127.                            #絶対ノイズ量
  128.                            noise_t_y = LumaDifference( ori.SelectEven(),con.SelectEven() )
  129.                            noise_t_uv = ( ChromaUDifference( ori.SelectEven(),con.SelectEven() ) + ChromaVDifference( ori.SelectEven(),con.SelectEven() ) )/2
  130.                            noise_b_y = LumaDifference( ori.SelectOdd(),con.SelectOdd() )
  131.                            noise_b_uv = ( ChromaUDifference( ori.SelectOdd(),con.SelectOdd() ) + ChromaVDifference( ori.SelectOdd(),con.SelectOdd() ) )/2
  132.                            #絶対ノイズ-平均(reverseで利用)
  133.                            navg1_y =  ( noise_t_y + noise_b_y )/2 #Max(noise_t_y,noise_b_y)
  134.                            navg1_uv = ( noise_t_uv + noise_b_uv )/2 #Max(noise_t_uv,noise_b_uv)
  135.                            navg1_yuv = ( navg1_y + navg1_uv )/2
  136.                            #相対ノイズ-平均 [comp t,b](diff計算で利用)
  137.                            navg2_y = LumaDifference( con.SelectEven(),con.SelectOdd() )/2
  138.                            navg2_uv = ( ChromaUDifference( con.SelectEven(),con.SelectOdd() ) + ChromaVDifference( con.SelectEven(),con.SelectOdd() ) )/4
  139.                            navg2_yuv = ( navg2_y + navg2_uv )/2
  140.                            #絶対ノイズ-符号付差分(diff計算で利用)
  141.                            diff1_y = noise_t_y - noise_b_y
  142.                            diff1_uv = noise_t_uv - noise_b_uv
  143.                            diff1_yuv = ( diff1_y + diff1_uv )/2
  144.                            #パラメタ
  145.                            diff1 = diff1_yuv #絶対ノイズ-符号付差分
  146.                            navg1 = navg1_yuv #-1 #絶対ノイズ平均(総ノイズ量判定用,色差の細かい模様は滅多に見ない)
  147.                            navg1_d = navg1_yuv #debug用
  148.                            navg2 = navg2_yuv #相対ノイズ-平均
  149.                          """
  150.                                        \)
  151.  
  152.             string2 = """
  153.                            nmin1 = navg2-Abs(diff1)/2
  154.                            nmin = (nmin1<7) ? nmin1*4 : nmin1+21
  155.                            nmax = navg2+Abs(diff1)*"""+String(nrw)+"""
  156.                            off_thresh = (diff1<0)? """+String(off_t)+""" : """+String(off_b)+"""
  157.                            min_thresh = (navg1<"""+String(namax_thresh)+""")? th_score(nmin) + off_thresh : """+String(namax_diff)+""" + off_thresh
  158.                            #符号付補正差分
  159.                            diff =    Abs(diff1)<1.8 ? diff1*10
  160.                                   \ :Abs(diff1)<5   ? diff1*5 +(diff1/Abs(diff1))*9
  161.                                   \ :Abs(diff1)<10  ? diff1*2 +(diff1/Abs(diff1))*24
  162.                                   \                 : diff1   +(diff1/Abs(diff1))*34
  163.                 """
  164.  
  165.             string3 = Select(debug
  166.                                        \,"""
  167.                            result = (Abs(diff) < min_thresh)                ? ((nmax<"""+String(nrt1y)+""")||("""+String(nrt2x)+"""<navg1_d && nmax<"""+String(nrt2y)+"""))
  168.                                                                           \ ? c : c.DecombUCF_nr(nmax)
  169.                                     \ : (navg1<"""+String(namax_thresh)+""") ? (diff<0) ? bob_t : bob_b
  170.                                                                           \ : (diff<0) ? bob_b : bob_t
  171.                            return result
  172.                          """
  173.                                        \,"""
  174.                            debug1_n_t = Select("""+String(chroma)+""", " Noise  [Y : "+LeftStr(String(noise_t_y),8)+"]"
  175.                                                                   \ , " Noise  [UV : "+LeftStr(String(noise_t_uv),8)+"]"
  176.                                                                   \ , " Noise  [Y : "+LeftStr(String(noise_t_y),8)+"]  [UV : "+LeftStr(String(noise_t_uv),8)+"]" )
  177.                            debug1_n_b = Select("""+String(chroma)+""", " Noise  [Y : "+LeftStr(String(noise_b_y),8)+"]"
  178.                                                                   \ , " Noise  [UV : "+LeftStr(String(noise_b_uv),8)+"]"
  179.                                                                   \ , " Noise  [Y : "+LeftStr(String(noise_b_y),8)+"]  [UV : "+LeftStr(String(noise_b_uv),8)+"]" )
  180.                            debug1_o = c.Subtitle("[B] notbob  //  diff =  "+String(diff)+"  (|NoiseDiff| < "+String(min_thresh,"%.2f")+")")
  181.                                      \ .Subtitle("Noise // Top "+MidStr(debug1_n_t,8)+" / Bottom "+MidStr(debug1_n_b,8),y=24)
  182.                                      \ .Subtitle("navg1 : "+String(navg1_d,"%.2f")+" / navg2 : "+String(navg2,"%.2f")+" / nmin : "+String(nmin,"%.2f")+" / diff1 : "+String(diff1,"%.3f")+" / nrt : "+String(nmax,"%.1f") ,y=44)
  183.                            debug1_t = StackHorizontal( bob_t.Subtitle(" [C] top        // diff : "+LeftStr(String(diff),8)+" ("
  184.                                                                       \ +String(min_thresh,"%.2f")+") / Navg: "+String(navg1,"%.2f")+" ("""+String(namax_thresh)+""")",font_width=16)
  185.                                                           \ .Subtitle(debug1_n_t,y=24,font_width=16)
  186.                                                           \ .Subtitle(" navg1 : "+String(navg1_d,"%.2f")+" / navg2 : "+String(navg2,"%.2f")+" / nmin : "+String(nmin,"%.2f")+" / diff1 : "+String(diff1,"%.3f")+" / nrt : "+String(nmax,"%.1f") ,y=44,font_width=16)
  187.                                                    \ , bob_b.Subtitle(" deleted",font_width=16).Subtitle(debug1_n_b,y=24,font_width=16) ).HorizontalReduceBy2()
  188.                            debug1_b = StackHorizontal( bob_b.Subtitle(" [C] bottom // diff : "+LeftStr(String(diff),8)+" ("
  189.                                                                       \ +String(min_thresh,"%.2f")+") / Navg: "+String(navg1,"%.2f")+" ("""+String(namax_thresh)+""")",font_width=16)
  190.                                                           \ .Subtitle(debug1_n_b,y=24,font_width=16)
  191.                                                           \ .Subtitle(" navg1 : "+String(navg1_d,"%.2f")+" / navg2 : "+String(navg2,"%.2f")+" / nmin : "+String(nmin,"%.2f")+" / diff1 : "+String(diff1,"%.3f")+" / nrt : "+String(nmax,"%.1f") ,y=44,font_width=16)
  192.                                                    \ , bob_t.Subtitle(" deleted",font_width=16).Subtitle(debug1_n_t,y=24,font_width=16) ).HorizontalReduceBy2()
  193.                            debug1 = (Abs(diff) < min_thresh)                ? ((nmax<"""+String(nrt1y)+""")||("""+String(nrt2x)+"""<navg1_d && nmax<"""+String(nrt2y)+"""))
  194.                                                                           \ ? debug1_o : debug1_o.DecombUCF_nr(nmax).Subtitle("NR",y=62)
  195.                                     \ : (navg1<"""+String(namax_thresh)+""") ? (diff<0) ? debug1_t : debug1_b
  196.                                                                           \ : (diff<0) ? debug1_b.Subtitle("Reversed",y=62) : debug1_t.Subtitle("Reversed",y=62)
  197.                            return debug1
  198.                          """
  199.                                        \,"return c"
  200.                                        \,"return c"
  201.                                        \,"""
  202.                            float1 = "%5.1f"
  203.                            float4 = "%8.2f"
  204.                            debug4_nr = c.WriteFile(""""+txt_file+"""","current_frame","Chr(9)","3","Chr(9)+Chr(35)+String(diff,float4)+Chr(9)+String(diff1,float1)"
  205.                                                                   \ ,"Chr(9)+String(navg1_d,float1)+Chr(9)+String(nmin,float1)+Chr(9)+String(AverageLuma,float1)+Chr(9)+String(diff/Abs(diff)*min_thresh,float1)+Chr(9)"
  206.                                                                   \ ,"String(nmax,float1)+Chr(129)+Chr(161)+Chr(9)")
  207.                            debug4_t = c.WriteFile(""""+txt_file+"""","current_frame","Chr(9)","1","Chr(9)+Chr(35)+String(diff,float4)+Chr(9)+String(diff1,float1)"
  208.                                                                   \ ,"Chr(9)+String(navg1_d,float1)+Chr(9)+String(nmin,float1)+Chr(9)+String(AverageLuma,float1)+Chr(9)+String(-min_thresh,float1)+Chr(9)")
  209.                            debug4_b = c.WriteFile(""""+txt_file+"""","current_frame","Chr(9)","2","Chr(9)+Chr(35)+String(diff,float4)+Chr(9)+String(diff1,float1)"
  210.                                                                   \ ,"Chr(9)+String(navg1_d,float1)+Chr(9)+String(nmin,float1)+Chr(9)+String(AverageLuma,float1)+Chr(9)+String(min_thresh,float1)+Chr(9)")
  211.                            debug4 = (Abs(diff) < min_thresh)                ? ((nmax<"""+String(nrt1y)+""")||("""+String(nrt2x)+"""<navg1_d && nmax<"""+String(nrt2y)+"""))
  212.                                                                           \ ? c : debug4_nr
  213.                                     \ : (navg1<"""+String(namax_thresh)+""") ? (diff<0) ? debug4_t : debug4_b
  214.                                                                           \ : (diff<0) ? debug4_b : debug4_t
  215.                            return debug4
  216.                          """
  217.                                        \,"""
  218.                            float1 = "%5.1f"
  219.                            float4 = "%8.2f"
  220.                            debug5_img = RevStr(MidStr(RevStr(""""+txt_file+""""),5)) + "_img_"
  221.                            debug5_nr = c.Subtitle(String(current_frame)).WriteFile(""""+txt_file+"""","current_frame","Chr(9)","3","Chr(9)+Chr(35)+String(diff,float4)+Chr(9)+String(diff1,float1)+Chr(9)+String(navg1_d,float1)+Chr(9)"
  222.                                        \ ,"String(nmin,float1)+Chr(9)+String(AverageLuma,float1)+Chr(9)+String(diff/Abs(diff)*min_thresh,float1)+Chr(9)+String(nmax,float1)+Chr(129)+Chr(161)+Chr(9)")
  223.                                        \.ConvertToRGB32(matrix="Rec709").ImageWriter(debug5_img,current_frame,-1,"jpg").ConvertToYV12(matrix="Rec709")
  224.                            debug5_t = StackHorizontal(c.Bob().SelectEven(),c.Bob().SelectOdd()).HorizontalReduceBy2().Subtitle(String(current_frame))
  225.                                       \ .WriteFile(""""+txt_file+"""","current_frame","Chr(9)","1","Chr(9)+Chr(35)+String(diff,float4)+Chr(9)+String(diff1,float1)+Chr(9)+String(navg1_d,float1)+Chr(9)+String(nmin,float1)"
  226.                                       \ ,"Chr(9)+String(AverageLuma,float1)+Chr(9)+String(-min_thresh,float1)+Chr(9)").ConvertToRGB32(matrix="Rec709").ImageWriter(debug5_img,current_frame,-1,"jpg").ConvertToYV12(matrix="Rec709")
  227.                            debug5_b = StackHorizontal(c.Bob().SelectOdd(),c.Bob().SelectEven()).HorizontalReduceBy2().Subtitle(String(current_frame))
  228.                                       \ .WriteFile(""""+txt_file+"""","current_frame","Chr(9)","2","Chr(9)+Chr(35)+String(diff,float4)+Chr(9)+String(diff1,float1)+Chr(9)+String(navg1_d,float1)+Chr(9)+String(nmin,float1)"
  229.                                       \ ,"Chr(9)+String(AverageLuma,float1)+Chr(9)+String(min_thresh,float1)+Chr(9)").ConvertToRGB32(matrix="Rec709").ImageWriter(debug5_img,current_frame,-1,"jpg").ConvertToYV12(matrix="Rec709")
  230.                            debug5 = (Abs(diff) < min_thresh)                ? ((nmax<"""+String(nrt1y)+""")||("""+String(nrt2x)+"""<navg1_d && nmax<"""+String(nrt2y)+"""))
  231.                                                                           \ ? c : debug5_nr
  232.                                     \ : (navg1<"""+String(namax_thresh)+""") ? (diff<0) ? debug5_t : debug5_b
  233.                                                                           \ : (diff<0) ? debug5_b : debug5_t
  234.                            return debug5
  235.                          """
  236.                                        \,"return c"
  237.                                        \,"""
  238.                            float1 = "%5.1f"
  239.                            float4 = "%8.2f"
  240.                            debug7_nr = c.WriteFile(""""+txt_file+"""","current_frame","Chr(9)","3","Chr(9)+Chr(35)+String(diff,float4)+Chr(9)+String(diff1,float1)"
  241.                                                                   \ ,"Chr(9)+String(navg1_d,float1)+Chr(9)+String(nmin,float1)+Chr(9)+String(AverageLuma,float1)+Chr(9)+String(nmax,float1)+Chr(129)+Chr(161)+Chr(9)")
  242.                            debug7_t = bob_t.WriteFile(""""+txt_file+"""","current_frame","Chr(9)","1","Chr(9)+Chr(35)+String(diff,float4)+Chr(9)+String(diff1,float1)"
  243.                                                                   \ ,"Chr(9)+String(navg1_d,float1)+Chr(9)+String(nmin,float1)+Chr(9)+String(AverageLuma,float1)+Chr(9)+String(-min_thresh,float1)+Chr(9)")
  244.                            debug7_b = bob_b.WriteFile(""""+txt_file+"""","current_frame","Chr(9)","2","Chr(9)+Chr(35)+String(diff,float4)+Chr(9)+String(diff1,float1)"
  245.                                                                   \ ,"Chr(9)+String(navg1_d,float1)+Chr(9)+String(nmin,float1)+Chr(9)+String(AverageLuma,float1)+Chr(9)+String(min_thresh,float1)+Chr(9)")
  246.                            debug7 = (Abs(diff) < min_thresh)                ? ((nmax<"""+String(nrt1y)+""")||("""+String(nrt2x)+"""<navg1_d && nmax<"""+String(nrt2y)+"""))
  247.                                                                           \ ? c : debug7_nr.DecombUCF_nr(nmax)
  248.                                     \ : (navg1<"""+String(namax_thresh)+""") ? (diff<0) ? debug7_t : debug7_b
  249.                                                                           \ : (diff<0) ? debug7_b : debug7_t
  250.                            return debug7
  251.                          """
  252.                                        \)
  253.             # using FrameDiff (TIVTC)
  254.     ## Apply to dirty frame ##
  255.         # dmode1 : field diff detection #
  256.         conditionalfilter(c
  257.                             \, (debug==1)? c.ScriptClip("""subtitle("[A] notbob  //  Fdiff =  " + string(CFieldDiff(nt=0,chroma=true)*100.0/"""+String(Width(c)*Height(c))+""")+"  (FieldDiff < """+String(fd_thresh)+""")")""") : c
  258.                             \, c.ScriptClip(string1+string2+string3)
  259.                             \, "CFieldDiff(nt=0,chroma=true)*100.0/"+String(Width(c)*Height(c)),"<", String(fd_thresh) )
  260.  
  261.         # addtional luma detection #第一判定に 輝度変化利用
  262.     ## Debug ##
  263.     #cleaned
  264.     #(debug==1)? last.subtitle(" [A] : clean frame / [B] : noisy and both fields too (recommend only NR) / [C] : noisy but found clean field (BOB)",size=14,y=44,text_color=$00ff00) : NOP()
  265.     (debug==2)? c.ScriptClip("""Subtitle(String(CFieldDiff(nt=0,chroma=true)*100.0/"""+String(Width(c)*Height(c))+""" ),y=40)""").FieldDiff(nt=0, chroma=true, display=true) : last
  266.     (debug==3)? c.Crop(8,16,0,0,true).Loop(2).ScriptClip("""c = con
  267.                             debug3_t = Interleave(c.SelectEven(),c.SelectEven())
  268.                             debug3_b = Interleave(c.SelectOdd(),c.SelectOdd())
  269.                             navg2_y = LumaDifference(debug3_t,debug3_b)/2
  270.                             navg2_uv = ( ChromaUDifference(debug3_t,debug3_b) + ChromaVDifference(debug3_t,debug3_b) )/4
  271.                             navg2_yuv = ( navg2_y+navg2_uv )/2
  272.                             debug3_n2 = Select("""+String(chroma)+""", navg2_y, navg2_uv, navg2_yuv )
  273.                             debug3_2 = Merge(debug3_t, debug3_b.Invert()).Subtitle("navg2 : "+String(debug3_n2,"%.2f"))
  274.                             noise_y = LumaDifference(ori,c)
  275.                             noise_uv = ( ChromaUDifference(ori,c) + ChromaVDifference(ori,c) )/2
  276.                             debug3_n1 = Select("""+String(chroma)+""", " Noise  [Y : "+LeftStr(String( noise_y ),8)+"]"
  277.                                                                  \ , " Noise  [UV : "+LeftStr(String( noise_uv ),8)+"]"
  278.                                                                  \ , " Noise  [Y : "+LeftStr(String( noise_y ),8)+"]  [UV : "+LeftStr(String( noise_uv ),8)+"]" )
  279.                             StackVertical(c,debug3_2).subtitle(debug3_n1,y=22)""").ScriptClip("""subtitle("frame = " + string(Float(current_frame)/2.0,"%.1f"))""")  : last
  280.     (debug==4||debug==5||debug==7)? last.WriteFileStart(txt_file,""" "TYPE INT"+Chr(10)+"DEFAULT 0" """
  281.                                         \ ,""" Chr(10)+"#flag:[0:NOP] [1:Bob 1st field] [2:Bob 2nd f.] [3:NR] [4:Copy bef. Frame] [5:Copy next F.] [6:Black16(TV)] [7:White235(TV)] [8:Black0(PC)] [9:White255(PC)]" """
  282.                                         \ ,""" Chr(10)+"#"+Chr(34)+ScriptFile()+Chr(34) """
  283.                                         \ ,""" Chr(10)+"#[chroma:"""+String(chroma)+"""] [nrt(nmax):"""+String(nrt1y,"%.1f")+"""]" ""","Chr(10)"
  284.                                         \ ,""" Chr(10)+"#frame"+Chr(9)+"flag"+Chr(9)+"#diff"+Chr(9)+Chr(9)+"diff1"+Chr(9)+"navg1"+Chr(9)+"nmin"+Chr(9)+"luma"+Chr(9)+"diff_th"+Chr(9)+"nrt" """) : last
  285.     (debug==6)? c.ScriptClip("""c = last
  286.                             return Select(debug6_flag, c, bob_t, bob_b, c.DecombUCF_nr(), c, c
  287.                                                    \ , c.BlankClip(color_yuv=$108080), c.BlankClip(color_yuv=$EB8080), c.BlankClip(color_yuv=$008080), c.BlankClip(color_yuv=$FF8080) ) """)
  288.                    \    .ConditionalReader(txt_file,"debug6_flag").ScriptClip("""c = last
  289.                             return Select(debug6_flag, c, c, c, c, c.Trim(current_frame-1,-1), c.Trim(current_frame+1,-1), c, c, c, c)""")
  290.                    \    .ConditionalReader(txt_file,"debug6_flag") : last
  291.     return last
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement