code wiki / _hdl_build / nx_nv1_transcode.nx
nx_nv1_transcode.nx source
↩ module page · 88 lines · 3331 B
1// nx_nv1_transcode.nx -- NV1 raw->LPC transcoder probe (REAL-DATA evidence
2// for the LPC rung): reads an NLC1 container from /tmp/nv1_in.bin, rewrites
3// every 'A' (raw PCM) chunk as an 'L' (LPC) chunk, copies V chunks and the
4// terminator, writes /tmp/nv1_out.bin, validates the output, then SELF-
5// CHECKS by decoding every L chunk back and byte-comparing against the
6// original PCM. Prints TRANSCODE markers (judge by markers, not $?-echo).
7// license_tier: ORIGINAL
8
9import "nx_nv1_lpc.nx"
10const K_MAGIC_65536: i64 = 65536
11
12func tc_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
13func tc_dec(v: i64) -> i64 {
14 let d: *u8 = sys_mmap(32)
15 let t: *u8 = sys_mmap(32)
16 if v == 0 { d[0] = 48 as u8; sys_write(1, d, 1); return 0 }
17 var x: i64 = v
18 var n: i64 = 0
19 while x > 0 { t[n] = (48 + x % 10) as u8; x = x / 10; n = n + 1 }
20 var i: i64 = 0
21 while i < n { d[i] = t[n - 1 - i]; i = i + 1 }
22 sys_write(1, d, n)
23 return 0
24}
25
26func main() -> i64 {
27 let nlen: *i64 = sys_mmap(64) as *i64
28 let inb: *u8 = sys_read_file("/tmp/nv1_in.bin" as *u8, nlen)
29 let n: i64 = nlen[0]
30 if n < 33 { tc_puts("TRANSCODE-FAIL input-too-short\n" as *u8); return 1 }
31 let info: *i64 = sys_mmap(64) as *i64
32 if nv1_validate(inb, n, info) != 0 {
33 tc_puts("TRANSCODE-FAIL input-invalid\n" as *u8)
34 return 1
35 }
36
37 let outb: *u8 = sys_mmap(n + K_MAGIC_65536)
38 var w: i64 = 0
39 var i: i64 = 0
40 while i < 24 { outb[i] = inb[i]; i = i + 1 }
41 w = 24
42
43 var off: i64 = 24
44 var ended: i64 = 0
45 var exact: i64 = 1
46 let dec: *u8 = sys_mmap(n + K_MAGIC_65536)
47 let pay: *u8 = sys_mmap(n + K_MAGIC_65536)
48 while ended == 0 {
49 let k: i64 = inb[off] & 0xff
50 let ln: i64 = nv1_rd_u32(inb, off + 1)
51 let tm: i64 = nv1_rd_u32(inb, off + 5)
52 if k == 65 {
53 let ns: i64 = ln / 2
54 let el: i64 = nv1l_encode(inb + (off + 9), ns, pay, 6 + ln + 64)
55 if el < 0 { tc_puts("TRANSCODE-FAIL encode\n" as *u8); return 1 }
56 w = nv1_write_chunk(outb, w, 76, tm, pay, el)
57 let dn: i64 = nv1l_decode(pay, el, dec, ln + 64)
58 if dn != ns { exact = 0 }
59 var j: i64 = 0
60 while j < ln {
61 if dec[j] != inb[off + 9 + j] { exact = 0; j = ln } else { j = j + 1 }
62 }
63 }
64 if k == 86 { w = nv1_write_chunk(outb, w, 86, tm, inb + (off + 9), ln) }
65 if k == 69 { w = nv1_write_end(outb, w, tm); ended = 1 }
66 off = off + 9 + ln
67 }
68
69 if nv1_validate(outb, w, info) != 0 {
70 tc_puts("TRANSCODE-FAIL output-invalid\n" as *u8)
71 return 1
72 }
73 let fd: i64 = sys_openat_wr("/tmp/nv1_out.bin" as *u8, 0x1a4)
74 if fd < 0 { tc_puts("TRANSCODE-FAIL open-out\n" as *u8); return 1 }
75 sys_write(fd, outb, w)
76 sys_close(fd)
77
78 tc_puts("TRANSCODE in=" as *u8); tc_dec(n)
79 tc_puts("B out=" as *u8); tc_dec(w)
80 tc_puts("B permil=" as *u8); tc_dec((w * 1000) / n)
81 tc_puts(" audio_chunks=" as *u8); tc_dec(info[1])
82 tc_puts(" samples=" as *u8); tc_dec(info[2])
83 tc_puts(" exact=" as *u8); tc_dec(exact)
84 tc_puts("\n" as *u8)
85 if exact == 1 { tc_puts("TRANSCODE-OK\n" as *u8); return 0 }
86 tc_puts("TRANSCODE-FAIL pcm-mismatch\n" as *u8)
87 return 1
88}