/*
    Auto_VFR  ver 0.1.0.4
    written by putin
    
    <解説>
        AutoVfr.exeに入力するログを作るための関数です。

    <必要なプラグイン>
        TIVTC              必須
        mt_masktools     IsDup=trueの場合

    <使用例>
        (1)普通に使う
        Auto_VFR("logfile")

        (2)豚を隠したい
        TBSCrop()
        Auto_VFR("logfile")

        (3)テロップを24fps化させたい(※上手くいかない場合もあります)
        Auto_VFR("logfile", IsCrop=true, show_crop=false)

        (4)フレーム数を削減してみたい
        Auto_VFR("logfile", IsDup=true)

        (5) (3)と(4)を両方やりたい
        Auto_VFR("logfile", IsCrop=true, IsDup=true)
*/

function Auto_VFR(clip c,string file,
                        \int "cthresh",int "mi", bool "chroma", int "blockx", int "blocky"
                        \,bool "IsCrop",int "crop_height",bool "IsTop",bool "IsBottom",bool "show_crop"
                        \,bool "IsDup",int "thr_m",float "thr_luma"){  
                        
    h=c.height()        
    IsDup=default(IsDup,false)   
    
    HDsize=720
    cthresh_default=(IsDup==true)?7:(h>=HDsize)?9:7
    mi_default=(IsDup==true)?50:(h>=HDsize)?80:50
    blocks_default=(h>=HDsize)?16:32    
    
    cthresh=default(cthresh,cthresh_default)
    mi=default(mi,mi_default)
    chroma=default(chroma,false)
    blockx=default(blockx,blocks_default)
    blocky=default(blocky,blocks_default)    
 
    crop_height=default(crop_height,(h==HDsize)?h-(16*50):80)
    mod=(IsYV12(c)==true)?4:2
    Assert((h-crop_height)%mod==0,"crop_height is invalid.")
    IsCrop=default(IsCrop,false)
    IsTop=default(IsTop,false)
    IsBottom=default(IsBottom,true)    
    show_crop=Default(show_crop,false)    

    thr_m=default(thr_m,10)
    thr_luma=default(thr_luma,0.010)
    
    int_t=( IsCrop==true && IsTop==true)?crop_height:0
    int_b=( IsCrop==true && IsBottom==true)?crop_height:0   
    
    count=c.framecount()    
    dwc=c.DoubleWeave()
    dcount=dwc.framecount()
       
   ScriptClip(dwc,"""
       prg="0"
       cmb="1"
       count="""+string(count)+"""
       dcount="""+string(dcount)+"""
       file=""""+file+""""       
       cth="""+string(cthresh)+"""
       mi="""+string(mi)+"""
       chrm="""+string(chroma)+"""
       bx="""+string(blockx)+"""
       by="""+string(blocky)+"""       
       int_t="""+string(int_t)+""" 
       int_b="""+string(int_b)+"""
       show="""+string(show_crop)+"""
       isDup="""+string(IsDup)+"""  
       thr_m="""+string(thr_m)+"""
       thr_luma="""+string(thr_luma)+"""     
       crnt=current_frame       
       str_num=string(crnt)    
       str_line=string("-")          
       str_total=string("total_f-")+string(count)
       str_dtotal=string("doubleweave_f-")+string(dcount)       
       (crnt==0)?WriteFile(last,file,"str_total",append=false):nop
       (crnt==0)?WriteFile(last,file,"str_dtotal",append=true):nop                     
       str_main=(IsCombedTIVTC(last,
       \cthresh=cth,MI=mi,chroma=chrm,blockx=bx,blocky=by)==true)?cmb:prg     
       str_crop=(int_t>0 || int_b>0)?(IsCombedTIVTC(last.crop(0,int_t,0,-int_b),
       \cthresh=cth,MI=mi,chroma=chrm,blockx=bx,blocky=by)==true)?cmb:prg
       \:str_main
       str_motion=(isDup==true && crnt%2 == 0)?
       \(Interleave(SelectEven().mt_motion(thY2=thr_m),SelectOdd()).AverageLuma() >thr_luma)?cmb:prg
       \:prg                   
       result=str_num+str_line+str_main+str_line+str_crop+str_line+str_motion
       (show==false)?WriteFile(last,file,"string(result)",append=true,flush=false):last
       """)    

       return (show_crop==true)?last.crop(0,int_t,0,-int_b):last
}
function TBSCrop(clip c,int "crop_wide",int "crop_height"){ 
    #~ 豚とテロップ前に一瞬出てくる"<"マークをカットしてログが荒れるのを防ぐ。
    #~ ログの精度にはほぼ支障がない・・・と思う。             
    w=c.width()
    h=c.height()
    top=0
    wide=default(crop_wide,380)
    btm=default(crop_height,800)
    left=(wide>w/2)?0:w/2-wide
    c_clip=c.crop(left,top,-wide,-btm)
    c_clip=StackHorizontal(c_clip,c_clip)
    b_clip=c.crop(0,h-btm,0,0)
    return StackVertical(c_clip,b_clip)
}