code wiki / (root) / nx_media_budget.nx

nx_media_budget.nx source

↩ module page · 121 lines · 6769 B

1// nx_media_budget.nx -- Maps budget parameters to media quality settings ensuring monotonic quality and envelope-safe bitrate scaling. 2const K_MAGIC_1080: i64 = 1080 3const K_MAGIC_6000: i64 = 6000 4const K_MAGIC_3500: i64 = 3500 5const K_MAGIC_2500: i64 = 2500 6const K_MAGIC_1800: i64 = 1800 7const K_MAGIC_1100: i64 = 1100 8const K_MAGIC_192000: i64 = 192000 9const K_MAGIC_96000: i64 = 96000 10const K_MAGIC_48000: i64 = 48000 11const K_MAGIC_8800: i64 = 8800 12// nx_media_budget.nx -- the SHARED RESOURCE-INTELLIGENCE controller for the whole Nishi media foundation 13// (operator 2026-07-03: "our whole video foundation whether game or video or whatever as well as the audio 14// ... resource intelligent like we are doing with our fonts, scale up or down on resources"). Generalizes 15// nx_font_device_ladder's data-driven `route(caps)->technique` selector: ONE policy maps a live BUDGET 16// (compute class + uplink + battery + room size) to a MEDIA PLAN (video fps/resolution/QP, audio 17// sample-rate/lossless, GPU detail) that EVERY consumer draws from -- video calls, game render, circle 18// recording, audio capture. Two hard doctrines, both gate-proven: 19// MONOTONE: more budget NEVER yields lower quality on any axis (no thrash, no inversion). 20// ENVELOPE-SAFE: the plan's implied bitrate x fan-out NEVER exceeds the MEASURED edge ceiling 21// (~1.1MB/s, nx_video_swarm_probe 2026-07-03) NOR the sender's uplink -- so it scales DOWN under 22// pressure BY CONSTRUCTION, never floods (the family never freezes because the policy overshot). 23// Data-driven (rule #11): all thresholds live in the tier tables below, each cited. Pure integer, no 24// syscalls, no alloc in the hot path -> reusable in the wasm core, native clients, and game loop alike. 25// license_tier: ORIGINAL 26 27// ---- the QUALITY-TIER ladder (10 rungs, 9=max .. 0=floor). One row = a coherent media operating point. 28// fps mirrors the deployed vc_ladder (60/45/30/20/15/12/10/8/5/3). kbps anchored to nx_vcodec_speed_bench 29// (320x224 qp32 P=5545B; ~1330kbps@30fps) scaled by resolution. audio 96k covers canine hearing (dogs to 30// ~45kHz -> 48kHz Nyquist); 192k covers ultrasonic ("and other things" -- bats/fine transients). ---- 31func mb_fps(t: i64) -> i64 { 32 if t>=9 {return 60} if t==8 {return 60} if t==7 {return 45} if t==6 {return 30} if t==5 {return 30} 33 if t==4 {return 20} if t==3 {return 15} if t==2 {return 12} if t==1 {return 8} return 5 } 34func mb_height(t: i64) -> i64 { 35 if t>=9 {return K_MAGIC_1080} if t==8 {return 720} if t==7 {return 720} if t==6 {return 720} if t==5 {return 480} 36 if t==4 {return 480} if t==3 {return 360} if t==2 {return 360} if t==1 {return 240} return 240 } 37func mb_qp(t: i64) -> i64 { // lower = higher quality 38 if t>=9 {return 24} if t==8 {return 26} if t==7 {return 28} if t==6 {return 30} if t==5 {return 32} 39 if t==4 {return 34} if t==3 {return 36} if t==2 {return 40} if t==1 {return 44} return 48 } 40func mb_kbps(t: i64) -> i64 { // estimated per-stream bitrate at this operating point 41 if t>=9 {return K_MAGIC_6000} if t==8 {return K_MAGIC_3500} if t==7 {return K_MAGIC_2500} if t==6 {return K_MAGIC_1800} if t==5 {return K_MAGIC_1100} 42 if t==4 {return 700} if t==3 {return 450} if t==2 {return 300} if t==1 {return 180} return 110 } 43func mb_arate(t: i64) -> i64 { // audio sample rate Hz 44 if t>=8 {return K_MAGIC_192000} if t>=6 {return K_MAGIC_96000} return K_MAGIC_48000 } 45func mb_lossless(t: i64) -> i64 { if t>=3 {return 1} return 0 } 46func mb_gpu_detail(t: i64) -> i64 { // 0=max(neural/RT) .. 4=raster-min (mirrors the font route ladder) 47 if t>=8 {return 0} if t>=6 {return 1} if t>=4 {return 2} if t>=2 {return 3} return 4 } 48 49// the MEASURED edge fan-out ceiling (nx_video_swarm_probe 2026-07-03: ~1.1MB/s clean = 8800 kbps). 50func mb_edge_ceil_kbps() -> i64 { return K_MAGIC_8800 } 51 52// ---- sub-tier derivations (each input -> the highest tier it alone permits) ---- 53// bandwidth: the highest tier whose per-stream bitrate fits BOTH the sender uplink (1 stream up) AND the 54// edge ceiling under fan-out (relay sends peers-1 copies). peers<=1 -> uplink only. 55func mb_bw_tier(uplink_kbps: i64, peers: i64) -> i64 { 56 var fanout: i64 = peers - 1 57 if fanout < 1 { fanout = 1 } 58 let ceil: i64 = mb_edge_ceil_kbps() 59 var t: i64 = 9 60 while t > 0 { 61 let k: i64 = mb_kbps(t) 62 if k <= uplink_kbps { if k * fanout <= ceil { return t } } 63 t = t - 1 64 } 65 return 0 66} 67// does ANY video tier fit the pipe? If even the floor (tier 0) bitrate exceeds the uplink or the 68// fan-out ceiling, video DROPS to audio-only (the correct scale-down: shed video before audio, never 69// flood a pipe too small even for the floor). Returns 1 = send video, 0 = audio-only. 70func mb_video_fits(uplink_kbps: i64, peers: i64) -> i64 { 71 var fanout: i64 = peers - 1 72 if fanout < 1 { fanout = 1 } 73 let k0: i64 = mb_kbps(0) 74 if k0 > uplink_kbps { return 0 } 75 if k0 * fanout > mb_edge_ceil_kbps() { return 0 } 76 return 1 77} 78// battery: low charge caps quality (resource-intelligent; 101 = plugged/unknown => no cap). Cited: mobile 79// thermal/energy budget shrinks as charge drops. 80func mb_batt_tier(battery: i64) -> i64 { 81 if battery >= 101 { return 9 } 82 if battery >= 80 { return 9 } 83 if battery >= 50 { return 7 } 84 if battery >= 30 { return 5 } 85 if battery >= 15 { return 3 } 86 return 1 87} 88 89// ---- THE controller: budget -> chosen tier = the BINDING constraint (min of all sub-tiers) ---- 90// cpu_class: 0..9 device compute class (device-detect maps real CPU->class; anchor: nx_vcodec_speed_bench 91// native encode fps -- 60fps-capable encode ~ class 8-9, a thermal-throttled phone ~ class 2-4). 92func mb_tier(cpu_class: i64, uplink_kbps: i64, battery: i64, peers: i64) -> i64 { 93 var c: i64 = cpu_class 94 if c > 9 { c = 9 } 95 if c < 0 { c = 0 } 96 var t: i64 = c 97 let bw: i64 = mb_bw_tier(uplink_kbps, peers) 98 if bw < t { t = bw } 99 let bt: i64 = mb_batt_tier(battery) 100 if bt < t { t = bt } 101 return t 102} 103 104// fill a media PLAN for the caller (any consumer): plan[0]=fps [1]=height [2]=qp [3]=audio_rate 105// [4]=lossless [5]=gpu_detail [6]=est_kbps_per_stream [7]=tier [8]=video_on (0 => audio-only). 106// Audio ALWAYS survives (voice is the last thing shed); only video scales to zero under extreme scarcity. 107func mb_plan(cpu_class: i64, uplink_kbps: i64, battery: i64, peers: i64, plan: *i64) -> i64 { 108 let t: i64 = mb_tier(cpu_class, uplink_kbps, battery, peers) 109 plan[0] = mb_fps(t) 110 plan[1] = mb_height(t) 111 plan[2] = mb_qp(t) 112 plan[3] = mb_arate(t) 113 plan[4] = mb_lossless(t) 114 plan[5] = mb_gpu_detail(t) 115 plan[6] = mb_kbps(t) 116 plan[7] = t 117 plan[8] = mb_video_fits(uplink_kbps, peers) 118 return t 119} 120 121func main() -> i64 { return 0 }