# This script was generated by SVP 4 Manager.
# Check https://www.svp-team.com for more details.

import vapoursynth as vs
core = vs.core
core.num_threads = 8

if not hasattr(core,'svp2'):
    core.std.LoadPlugin("C:\\Program Files (x86)\\SVP 4\\plugins64\\svpflow2_vs.dll")
if not hasattr(core,'trt'):
    core.std.LoadPlugin("C:\\Program Files (x86)\\SVP 4\\rife\\vstrt.dll")
import sys
sys.path.append("C:\\Program Files (x86)\\SVP 4\\rife")
from vsmlrt import RIFE, RIFEModel, Backend

clip = core.std.BlankClip(width=1920,height=1080,length=1000,format=vs.YUV420P8)

smoothfps_params = "{rate:{num:1}}"

src_fps     = 24
demo_mode   = 0
stereo_type = 0
nvof = 0
rife = 1
rife_num = 2
rife_den = 1
rife_threads = 4
rife_gpu = 0
rife_sc = 0.06
rife_mpath = "C:\\Program Files (x86)\\SVP 4\\rife\\rife-v4"
rife_trt = 1

########## BEGIN OF base.py ##########
# This file is a part of SmoothVideo Project (SVP) ver.4
# This is NOT the full Vapoursynth script, all used variables are defined via
# JScript code that generates the full script text.

def interpolate(clip):
# input_um - original frame in 4:2:0
# input_m  - cropped and resized (if needed) frame
# input_m8 - input_m converted to 8-bit
    input_um = clip.resize.Point(format=vs.YUV420P8,dither_type="random")
    input_m = input_um
    input_m8 = input_m

    if nvof:
        smooth  = core.svp2.SmoothFps_NVOF(input_m,smoothfps_params,nvof_src=input_m8,src=input_um,fps=src_fps)
    elif not rife:
        super   = core.svp1.Super(input_m8,super_params)
        vectors = core.svp1.Analyse(super["clip"],super["data"],input_m8,analyse_params)
        smooth  = core.svp2.SmoothFps(input_m,super["clip"],super["data"],vectors["clip"],vectors["data"],smoothfps_params,src=input_um,fps=src_fps)
    elif rife:
        input_m = input_m.misc.SCDetect(threshold=rife_sc)
        input_m = input_m.resize.Bicubic(format = vs.RGBS, matrix_in_s="709")
        if not rife_trt:
                smooth = core.rife.RIFE(input_m,factor_num=rife_num,factor_den=rife_den,model_path=rife_mpath,gpu_id=rife_gpu,gpu_thread=rife_threads,tta=False,sc=True)
        else:
                input_pad = input_m
                pw = ((input_m.width - 1) // 32 + 1) * 32 - input_m.width
                ph = ((input_m.height - 1) // 32 + 1) * 32 - input_m.height
                if pw>0 or ph>0:
                       input_pad = input_m.std.AddBorders(0,pw,0,ph);
                smooth = RIFE(input_pad, multi=rife_num, model=RIFEModel.v4_6, backend=Backend.TRT(fp16=True, device_id=0, num_streams=rife_threads))
                if rife_den>1:
                       smooth = smooth.std.SelectEvery(cycle=rife_den,offsets=0)
                if pw>0 or ph>0:
                       smooth = smooth.std.Crop(0,pw,0,ph);
        smooth = smooth.resize.Point(format=input_um.format.id, matrix_s="709")
        smooth = core.svp2.SmoothFps_RIFE(smooth,smoothfps_params,src=input_um,multi=rife_num/rife_den,fps=src_fps)


    if demo_mode==1:
        return demo(input_m,smooth)
    else:
        return smooth

if stereo_type == 1:
    lf = interpolate(core.std.CropRel(clip,0,(int)(clip.width/2),0,0))
    rf = interpolate(core.std.CropRel(clip,(int)(clip.width/2),0,0,0))
    smooth = core.std.StackHorizontal([lf, rf])
elif stereo_type == 2:
    lf = interpolate(core.std.CropRel(clip,0,0,0,(int)(clip.height/2)))
    rf = interpolate(core.std.CropRel(clip,0,0,(int)(clip.height/2),0))
    smooth = core.std.StackVertical([lf, rf])
else:
    smooth =  interpolate(clip)
########### END OF base.py ###########


smooth = smooth.resize.Point(format=vs.YUV420P8)
smooth.set_output()