code wiki / (root) / nx_vmvpred.nx

nx_vmvpred.nx source

↩ module page · 18 lines · 1063 B

1// nx_vmvpred.nx -- MOTION-VECTOR PREDICTION (the H.264/VP9 MVP): instead of coding each block's absolute motion vector, 2// predict it from the median of the left / top / top-right neighbour MVs and code only the RESIDUAL (mv - pred). On a 3// camera pan or head-pan -- the dominant motion in a video call -- neighbouring blocks share motion, so the residual 4// MVs collapse to ~0 and cost almost no bits. Pure integer; the predictor the codec runs identically on encode+decode. 5// license_tier: ORIGINAL 6 7// median of three (= sum - max - min): the H.264 MV predictor component 8func mvp_median(a: i64, b: i64, c: i64) -> i64 { 9 var mx: i64 = a; if b > mx { mx = b } if c > mx { mx = c } 10 var mn: i64 = a; if b < mn { mn = b } if c < mn { mn = c } 11 return a + b + c - mx - mn 12} 13// predict a block's MV from neighbour MVs L (left), T (top), TR (top-right), each [dx,dy]; writes pred[2]. 14func mvp_predict(L: *i64, T: *i64, TR: *i64, pred: *i64) -> i64 { 15 pred[0] = mvp_median(L[0], T[0], TR[0]) 16 pred[1] = mvp_median(L[1], T[1], TR[1]) 17 return 0 18}