code wiki / _hdl_build / nx_vsubpel_gate.nx
nx_vsubpel_gate.nx source
↩ module page · 46 lines · 2883 B
1// nx_vsubpel_gate.nx -- proves + MEASURES half-pel motion (nx_vsubpel): when content moves by a genuine 0.5 pixel,
2// integer-pel motion leaves a large residual but half-pel refinement lands on the true position with ~zero residual.
3// Lower residual = fewer bits at identical quality = the sub-pixel quality-per-bit win.
4import "nx_syscalls.nx"
5import "nx_gate_emit_lib.nx"
6import "nx_vsubpel.nx"
7
8func main() -> i64 {
9 g_puts("nx_vsubpel gate (half-pel motion cuts the residual, MEASURED)\n" as *u8)
10 var pass: i64 = 0; var total: i64 = 0
11 let W: i64 = 24; let H: i64 = 24; let T: i64 = 16
12 let cx: i64 = 4; let cy: i64 = 4 // block position (leaves a guard border)
13
14 let A: *u8 = sys_mmap(W*H) as *u8 // reference (prev)
15 let B: *u8 = sys_mmap(W*H) as *u8 // current = A shifted by EXACTLY 0.5px in x
16 let tmp: *i64 = sys_mmap(T*T*8) as *i64
17 let mvh: *i64 = sys_mmap(2*8) as *i64
18
19 // textured reference so a sub-pixel shift actually changes pixels (a flat region has no sub-pixel residual)
20 var y: i64 = 0
21 while y < H { var x: i64 = 0
22 while x < W { A[y*W+x] = (40 + ((x*11 + y*7) % 120)) as u8; x = x + 1 } y = y + 1 }
23 // B = A moved +0.5px in x (B[x] = average of A[x] and A[x+1]) -- a genuine half-pel motion
24 y = 0
25 while y < H { var x: i64 = 0
26 while x < W - 1 { B[y*W+x] = (((A[y*W+x] as i64) + (A[y*W+(x+1)] as i64) + 1) / 2) as u8; x = x + 1 }
27 B[y*W + (W-1)] = A[y*W + (W-1)]; y = y + 1 }
28
29 // INTEGER-pel residual at MV (0,0): the best integer match can't represent the 0.5 shift
30 sp_interp_block(A, W, cx, cy, 0, 0, T, tmp)
31 let sad_int: i64 = sp_sad(B, W, cx, cy, tmp, T)
32 // HALF-pel refine around (0,0): should find (hx=1,hy=0) = the true 0.5px-x shift
33 let sad_half: i64 = sp_refine(B, A, W, H, cx, cy, 0, 0, T, tmp, mvh)
34
35 g_puts(" [measure] residual SAD: integer-pel=" as *u8); g_pn(sad_int); g_puts(" half-pel=" as *u8); g_pn(sad_half)
36 g_puts(" best half-pel MV=(" as *u8); g_pn(mvh[0]); g_puts("," as *u8); g_pn(mvh[1]); g_puts(")/2px\n" as *u8)
37 if sad_int > 0 { g_puts(" [measure] residual cut by half-pel = " as *u8); g_pn((sad_int - sad_half) * 100 / sad_int); g_puts("%\n" as *u8) }
38
39 pass = pass + g_check("half-pel residual << integer-pel residual (>= 8x smaller)" as *u8, sad_half * 8 < sad_int); total=total+1
40 pass = pass + g_check("half-pel found the true (0.5, 0) shift" as *u8, (mvh[0]==1) & (mvh[1]==0)); total=total+1
41 pass = pass + g_check("integer-pel genuinely cannot match (residual non-trivial)" as *u8, sad_int > 256); total=total+1
42
43 g_puts("---- vsubpel gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
44 if pass == total { g_puts("verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
45 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1
46}