# Quantified Motion Filter
# Versione n.3 (5/9/2005)
# Made by Bistecc1

function Motion()
{
# Modifica: Variabile differenza: differenza tra il valore di moto dell'attuale frame e di quello successivo
global differenza = (diff-next)
# Modifica: Confronto con i threshold modificato in modo da fare il confronto prima con i Scene Change.
# Modifica: Se la variabile differenza supera il limite di Scene Change c'è un cambio di scena
global motion_level = (differenza >= threshold_sc && next < threshold_0m) ? 5 : (differenza >= threshold_sc && next >= threshold_0m && next < threshold_1m) ? 6 : (differenza >= threshold_sc && next >= threshold_1m && next < threshold_2m) ? 7 : (differenza >= threshold_sc && next >= threshold_2m && next < threshold_3m) ? 8 : (differenza >= threshold_sc && next >= threshold_3m) ? 9 : (diff < threshold_0m) ? 0 : (diff >= threshold_0m && diff < threshold_1m) ? 1 : (diff >= threshold_1m && diff < threshold_2m) ? 2 : (diff >= threshold_2m && diff < threshold_3m ) ? 3 : (diff >= threshold_3m) ? 4 : motion_level
}

function QMF(clip c, float "threshold_0m", float "threshold_1m", float "threshold_2m", float "threshold_3m", float "threshold_sc", bool "debug")
{
# Limite di MOvimento Nullo
global threshold_0m = default(threshold_0m,2)
# Limite di Movimento Basso
global threshold_1m = default(threshold_1m,4)
# Limite di MOvimento Medio
global threshold_2m = default(threshold_2m,7)
# Limite di MOvimento Alto, superato questo limito c'è il movimento Molto Alto
global threshold_3m = default(threshold_3m,10)
# Limite di Scene Change (8 sembra che vada bene per qualsiasi film -controllare cmq-)
global threshold_sc = default(threshold_sc,8)
debug = default(debug, false)
global motion_level = 0
global clip = c

c1 = ConditionalFilter(c,Filter_Motion_Nil(c), Filter_Motion_Nil(c), "motion_level", "=", "0")
c2 = ConditionalFilter(c,Filter_Motion_Min(c), c1, "motion_level", "=", "1")
c3 = ConditionalFilter(c,Filter_Motion_Mid(c), c2, "motion_level", "=", "2")
c4 = ConditionalFilter(c,Filter_Motion_High(c), c3, "motion_level", "=", "3")
c5 = ConditionalFilter(c,Filter_Motion_Very_High(c), c4, "motion_level", "=", "4")
c6 = ConditionalFilter(c,Filter_Motion_Nil(c), c5, "motion_level", "=", "5")
c7 = ConditionalFilter(c,Filter_Motion_Min(c), c6, "motion_level", "=", "6")
c8 = ConditionalFilter(c,Filter_Motion_Mid(c), c7, "motion_level", "=", "7")
c9 = ConditionalFilter(c,Filter_Motion_High(c), c8, "motion_level", "=", "8")
c10 = ConditionalFilter(c,Filter_Motion_Very_High(c), c9, "motion_level", "=", "9")


c10 = (debug == true) ? ScriptClip(c8, "Debug()") : c10
c10 = FrameEvaluate(c10, "Motion()")
c10 = FrameEvaluate(c10, "global next = 0.50 * YDifferenceToNext(clip) + 0.24 * UDifferenceToNext(clip) + 0.26 * VDifferenceToNext(clip)")
c10 = FrameEvaluate(c10, "global diff = 0.50 * YDifferenceFromPrevious(clip) + 0.26 * UDifferenceFromPrevious(clip) + 0.24 * VDifferenceFromPrevious(clip)")
return c10
}

function Debug(clip c)
{
c = Subtitle(c, "Questo Frame: "  + string(diff),        x=25, y=75, font="courier new",size=24)
c = Subtitle(c, "Successivo: "    + string(next),        x=25, y=100, font="courier new",size=24)
c = Subtitle(c, "Differenza: "    + string(differenza),  x=25, y=125, font="courier new",size=24,text_color=$FFFFFE)
c = Subtitle(c, "Motion Level: "  + string(motion_level),x=25, y=150, font="courier new",size=24,text_color=$FFFFFF)

c = (motion_level == 0) ? Subtitle(c, "Zero Motion Frame c0", x=25, y=200, font="courier new", size=32, text_color=$FFFFFF) : c
c = (motion_level == 1) ? Subtitle(c, "Low Motion Frame c1", x=25, y=200, font="courier new", size=32, text_color=$FFFFFF) : c
c = (motion_level == 2) ? Subtitle(c, "Medium Motion Frame c2", x=25, y=200, font="courier new", size=32, text_color=$FFFFFF) : c
c = (motion_level == 3) ? Subtitle(c, "High Motion Frame c3", x=25, y=200, font="courier new", size=32, text_color=$FFFFFF) : c
c = (motion_level == 4) ? Subtitle(c, "Very High Motion Frame c4", x=25, y=200, font="courier new", size=32, text_color=$FFFFFF) : c

c = (motion_level == 5) ? Subtitle(c, "Scene Change (Zero) c5", x=25, y=200, font="courier new", size=32, text_color=$FFFFFF) : c
c = (motion_level == 6) ? Subtitle(c, "Scene Change (Low) c6", x=25, y=200, font="courier new", size=32, text_color=$FFFFFF) : c
c = (motion_level == 7) ? Subtitle(c, "Scene Change (Medium) c7", x=25, y=200, font="courier new", size=32, text_color=$FFFFFF) : c
c = (motion_level == 8) ? Subtitle(c, "Scene Change (High) c8", x=25, y=200, font="courier new", size=32, text_color=$FFFFFF) : c
c = (motion_level == 8) ? Subtitle(c, "Scene Change (Very High) c9", x=25, y=200, font="courier new", size=32, text_color=$FFFFFF) : c
return c
}