nx_vcodec_stream.nx source
↩ module page · 50 lines · 2431 B
1// nx_vcodec_stream.nx -- RUNG H8 support: serialize the codec's quantized coefficients to a byte
2// bitstream and back, so a compressed frame can ride the sovereign QUIC/FEC transport. encode_plane_stream
3// transforms+quantizes each 4x4 block and writes its 16 coeffs as int16; decode_plane_stream reads them
4// back and reconstructs. This is the bridge between the codec (H5/H6) and the transport (H7). Pure integer,
5// sovereign. license_tier: ORIGINAL
6import "nx_vcodec_color_frame.nx"
7const K_MAGIC_32768: i64 = 32768
8const K_MAGIC_65536: i64 = 65536
9
10func wr16(b: *u8, off: i64, v: i64) -> i64 { b[off]=(v & 0xff) as u8; b[off+1]=((v >> 8) & 0xff) as u8; return 0 }
11func rd16(b: *u8, off: i64) -> i64 { var v: i64=(b[off] as i64) | ((b[off+1] as i64) << 8); if v >= K_MAGIC_32768 { v = v - K_MAGIC_65536 } return v }
12
13// encode a W x H plane to a coefficient bitstream (16 int16 per 4x4 block, raster block order). returns nbytes.
14func encode_plane_stream(plane: *u8, W: i64, H: i64, QSTEP: i64, out: *u8) -> i64 {
15 let blk: *i64 = sys_mmap(8*16) as *i64; let q: *i64 = sys_mmap(8*16) as *i64
16 let tmp: *i64 = sys_mmap(8*16) as *i64; let T: *i64 = sys_mmap(8*16) as *i64
17 let BX: i64=W/4; let BY: i64=H/4
18 var off: i64=0
19 var by: i64=0
20 while by<BY { var bx: i64=0
21 while bx<BX {
22 var i: i64=0; while i<4 { var j: i64=0; while j<4 { blk[i*4+j]=plane[(by*4+i)*W+(bx*4+j)] as i64; j=j+1 } i=i+1 }
23 enc_block(blk, tmp, T, q, QSTEP)
24 i=0; while i<16 { wr16(out, off, q[i]); off=off+2; i=i+1 }
25 bx=bx+1
26 }
27 by=by+1
28 }
29 return off
30}
31// decode a plane from the coefficient bitstream into rec.
32func decode_plane_stream(inb: *u8, W: i64, H: i64, QSTEP: i64, rec: *u8) -> i64 {
33 let q: *i64 = sys_mmap(8*16) as *i64; let tmp: *i64 = sys_mmap(8*16) as *i64
34 let T: *i64 = sys_mmap(8*16) as *i64; let rb: *i64 = sys_mmap(8*16) as *i64
35 let BX: i64=W/4; let BY: i64=H/4
36 var off: i64=0
37 var by: i64=0
38 while by<BY { var bx: i64=0
39 while bx<BX {
40 var i: i64=0; while i<16 { q[i]=rd16(inb, off); off=off+2; i=i+1 }
41 dec_block(q, tmp, T, rb, QSTEP)
42 i=0; while i<4 { var j: i64=0; while j<4 { var v: i64=rb[i*4+j]; if v<0{v=0} if v>255{v=255} rec[(by*4+i)*W+(bx*4+j)]=v as u8; j=j+1 } i=i+1 }
43 bx=bx+1
44 }
45 by=by+1
46 }
47 return 0
48}
49
50func main() -> i64 { return 0 }