# kare / spoon C implementation of a capture tool for **Conexant CX23416 + EZ-USB FX2** based USB2 video capture modules, using **libusb-1.0**. Built and run on **FreeBSD** (developed and verified there) and Linux. Developed and verified against the **I-O DATA GV-MVP/RZ2** (VID 0x04bb / PID 0x0514), using the original Python reference `pvrusb2_capture.py`, yamori813's `mpegcapt.c`, the Linux `pvrusb2` driver, the official Windows driver (`p2usbRZ2.sys`, reverse-engineered with Ghidra) and the WM8739 / CX25836-7 datasheets. ## Status | Feature | State | |---|---| | MPEG-2 program-stream capture | **works** (720x480, ~6.5 Mbit/s, real video) | | Colour | **works** - full chroma (see "Colour" below) | | HM12 raw YUV capture (`-r`, YUV4MPEG2 output) | **works** | | Audio in the MPEG stream | **works** - mp2 48 kHz stereo 224 kbit/s | | Live viewing (`spoon`) | **works** - picture + sound, resizable window | | S-Video input | **works** - luma In1 + chroma In5 (`0x103 = 0xd0`), measured 2026-10-25 | ## Colour Colour works, and the one setting that matters is the CX25837 video-input register encoding. **The jack in use on this unit is the yellow RCA (composite): `0x0103 = 0xe1` (luma In2 / chroma In6) with `0x0401` bits 1-2 set to `2`.** That pair is measured from the vendor driver's own I2C writes (pcap, 2026-10-15) and is the default. The board routes the RCA through the NEC 3-D Y/C separator (uPD64084), so the decoder runs in Y/C mode even for composite - that is the vendor's configuration, not an S-Video assumption. **S-Video works: the S jack is luma In1 + chroma In5, `0x0103 = 0xd0`.** That is measured (2026-10-25, a recorder in the S jack): `0xd0` shows a real picture, `0xc3` - the value this repo used to send, which puts CH{2} on the same pin as CH{1} - is a dead flat black frame. General Status 2 (`0x040e`) reads `0x7d` for luma In1 (VPRES|AGC_LOCK|CSC_LOCK|VLOCK|HLOCK) and only `0x00`-`0x09` for In2..In8, and the chroma plane carries signal on In5 (U sd 9.85) while In6/In7/In8 are exactly 128.00 (dead). It is also the datasheet's canonical Y1/C1 pair (Table 3-5). `-S ` picks the chroma pin (0=In5 .. 3=In8; only In5 is measured), `--svideo` = `-S 0`. Getting `0x0103`/`0x0401` wrong silently disables the chroma path and you get a greyscale picture. Verify any change with `scripts/check_chroma.py ` (raw YUV U/V plane spread; 0 = grey). ## The top of the picture: VBI is cropped by default The first four rows of a capture used to be the source's **VBI waveform** — a fixed row of white dashes across the top (the user's "上によくわからない線"). `0x0474` (vertical blanking delay) reads back 20 and one count = one output row: 21 blanks row 0 only, 22 rows 0-1, 23 rows 0-2, **24 blanks all four**. **`0x0474` and `0x0477` must move together: `0x0477 = 0x0474 + 4`.** The datasheet (5-77) requires it, and `0x0477` (V656BLANK_CNT) is what sets the V-bit on the 656 bus — the line where the *encoder* stops treating rows as active video. Raising `0x0474` alone leaves the encoder sampling the ADC during blanking, which produced **pink then green horizontal stripes** at the top (Y=1 with U=V=141 renders as dark magenta, Y=16 with U=V=116 as green): ``` (0x0474,0x0477) luma rows 0-3 chroma saturation 20,24 54,54,39,47 (VBI) 2 24,24 1, 1, 1, 1 254 <- pink/green 24,26 1, 1,16,16 180 24,28 16,16,16,16 4 <- correct 24,30 16,16,16,16 10 <- window slides ``` With 24/28 the film's letterbox edge moves from row 14 to row 10 only because the window now really starts at NTSC line 21 instead of line 17 (and the source's own bottom letterbox becomes visible). `VBLANK=` overrides both, `VBLANK=0` leaves the decoder alone. The vendor table's **26 is not used**: it blanks two rows of real picture as well, which is invisible on a letterboxed film and wrong on a full-frame source. ## Hardware notes (GV-MVP/RZ2) * CX23416 MPEG encoder + EZ-USB FX2 bridge (firmware loaded from EEPROM). * Video decoder is a **CX25837** (CX25836/7 family) on I2C `0x44`; its device ID reads `0x8373` (`0x0101 = 0x83`, `0x0100 = 0x73`). The code and comments inherited the name "CX25843" from the driver it was adapted from - **the register map that applies here is the CX25836/7 one**. * Audio ADC is a **Wolfson WM8739** on I2C `0x1a` (2-wire, `MODE` strapped low). It feeds the encoder over I2S and takes its master clock from the CX25837's SA_MCLK output. * **`0x51` on that bus is the 24C64WP EEPROM, NOT an audio part.** Writing it once bricked this unit (recovered with a CH341A). `kare` refuses the whole `0x50`-`0x5f` range for the codec for that reason. * The CX23416 firmware must be uploaded as the **raw** blob (`cx_1_raw.bin`); `*_swapped.bin` gets swapped again and will not boot. * A normal re-run does **not** need a re-plug. The re-plug is only for a wedged encoder - i.e. after CX23416 registers were written *before* the firmware image, where every later command returns `LIBUSB_ERROR_IO` and no software reset (FX2 CPUCS toggle, `libusb_reset_device`, `usbconfig reset`, FX2 POWER_OFF) recovers it. ## Build ```sh gmake # FreeBSD; plain `make` on Linux. Builds both kare and spoon. ``` Dependencies: libusb-1.0, and for `spoon` SDL2 plus the ffmpeg libraries, all found through `pkg-config`. ## Usage ```sh # MPEG-2 capture from the yellow RCA (composite) for 12 s - the default ./kare -c firmware/cx23416/cx_1_raw.bin -o out.mpeg -t 12 # S-Video: -S , 0=In5 .. 3=In8 (luma is In1 for all four). # Measured 2026-10-25: 0=In5 is the S jack. ./kare -c firmware/cx23416/cx_1_raw.bin -o out.mpeg -S 0 -t 12 # HM12 raw YUV -> YUV4MPEG2 (.y4m), playable directly in VLC ./kare -c firmware/cx23416/cx_1_raw.bin -o out.y4m -r -t 8 # probe only (load firmware, print version) ./kare -c firmware/cx23416/cx_1_raw.bin -p ``` ## Live viewing: spoon ```sh INPUT=svideo MPEGVIEW_DSP=/dev/dsp11 ./spoon -m # MPEG: picture + sound ./spoon -m # same, composite input ./spoon -r # raw HM12 video (y4m path) MPEGVIEW_AUDIO=1 MPEGVIEW_DSP=/dev/dsp11 ./spoon -r # raw + PCM audio ``` `spoon` starts `kare` as a child, reads the stream from a pipe (no temporary file), decodes with ffmpeg and draws with SDL2, plus a clickable control bar across the top. `-m` = MPEG (audio comes from the stream), `-r` = raw HM12, `-f FILE` = an existing file. Options and the bar are described under "Viewer" below. Two measured traps worth knowing before changing anything here: * **A pipe is fine now, a growing file was not.** The retired path (`scripts/live-play.sh` + `scripts/mpegcap-live.py`) had to serve the live edge over HTTP because a player opening the growing file replays it from byte 0, and a FIFO (`S_IFIFO`, stat size 0) makes VLC sit on its analysis buffer. spoon avoids both by reading the pipe as it fills - but it must **drain that pipe in its own thread**: 15.5 MB/s of raw video through a 64 KB FreeBSD pipe while the main loop decodes and renders makes `kare` block, the USB reads stop, the FX2 FIFO overruns inside the device (libusb reports nothing) and the encoder falls to 1 fps. A 16 MB ring WITH back-pressure is what keeps it at 29.8 fps. * **Do not probe a y4m before a whole frame has arrived.** One raw frame is 518,400 bytes; waiting for only 64 KB makes ffmpeg's rawvideo demuxer frame the stream wrongly and the picture comes out shredded with colour noise. spoon waits for 524,288. ### The retired VLC/HTTP path (reference only) `scripts/live-play.sh` is kept for reference, not for use: it ran `kare` into a growing file, served `(end - EDGE)` over HTTP on port 8908, and pointed VLC at `http://127.0.0.1:8908/live`. Three things it measured that still apply to any VLC use on this host: * **`--vout=xcb_xv` is required.** The default `gl` vout (and `xcb_x11`) asks the decoder for an opaque VAOP surface and then fails "Failed to create video converter" ~400 times; `xcb_xv` initialises with I420. Reproduces on a stock ffmpeg MPEG-2, so it is a host/VLC property, not a kare one. * **Never add `--no-playlist-autostart`** - VLC then starts its interface and never opens the input. * **`--demux=ps` is required for MPEG**; without it VLC guesses MJPEG and prints "mjpeg demux error: cannot peek". ### Bitrate `BR` / `BRPEAK` (default 4000000 / 6000000, what the official INF pins). If the picture blocks up, raise them and confirm with `scripts/mpgqual.py FILE`, which prints `quantiser_scale_code` per picture type: a starved encoder pins it at its coarsest value (31), which is what mosaic looks like. Measured at 720x480: the stock 4 Mbit/s reached quantiser 31; the same setting with the bitrate fields in the right order reaches 7; 8 Mbit/s reaches 5. ### Recording to a file ```sh ./kare -c firmware/cx23416/cx_1_raw.bin -o /tmp/out.mpeg -C -t 30 vlc /tmp/out.mpeg ``` ### `-o -` (stdout) is still there for scripts `-o -` sends the stream to stdout (all status text goes to stderr so the pipe stays clean). `--demux=ps` is **required** for MPEG; without it VLC guesses MJPEG and prints "mjpeg demux error: cannot peek". Options: `-o` output, `-c` CX firmware, `-C` composite (**the default**), `-S`/`--svideo` S-Video (`-S 0` = luma In1 + chroma In5 = `0x103=0xd0`, measured), `-r` raw HM12, `-3/-4/-8` audio sample rate, `-t` seconds, `--live FILE`, `--codec ADDR` / `--no-codec` (WM873x audio codec), `-D` official-driver command sequence, `-d` verbose, `-s` scan, `-p` probe. `-L` (luma), `-B` (plane dumps) and `-R` (raw HM12 dump) are fed by the HM12 frame writer, so any of them implies `-r`; `--live` works in raw mode too (the y4m stream is written to the live file). ## Layout Only the C sources, the headers, the Makefile, this README and .gitignore are **tracked in git**. Everything else lives on the working copy and is listed in `.gitignore`, because it is either large (the vendor trees and datasheets under `ref/`), a runtime blob (the firmware image), or rewritten constantly while a capture is being reverse-engineered (the analysis scripts and the NOTES ledger). ``` TRACKED include/ pvrusb2.h capture.h hm12.h src/ kare.c the capture tool (-c firmware ... -o out.mpeg) spoon.c the viewer (-r / -m / -f FILE), with its control bar device.c firmware.c encoder.c capture.c hm12.c cx2584x_audio.c cx2584x_tables.c driver_seq.c test_cx.c test_mbox.c cx2584x_{ser,pll}_test.c cx2584x_ser_init.c Makefile / fx2load.c ON DISK, NOT TRACKED (see .gitignore) firmware/ cx23416/cx_{1,2,3}_raw.bin , fx2/* <- required at run time scripts/ capture analysis + the retired VLC/XP helpers - see scripts/README.md tools/ Ghidra scripts (ghidra/), C probes (c/), Python analysis - see tools/README.md ref/ pvrusb2/ , drivervista/ , magictv/ , datasheets/ docs/ NOTES.md , driver_capture_seq.txt , AUDIO_*.md , TESTER_CHECKLIST.md ``` A Windows build (`win/`, `Makefile.win`, MinGW) existed and was **removed on 2026-10-25**: it was never built or tested on this host, and it had already rotted (it still referenced `src/main.c` and `src/mpegview.c`). The `_WIN32` blocks left in `spoon.c`/`device.c`/`firmware.c`/`encoder.c` are inert on FreeBSD/Linux. Everything is in git history (`git log --all -- win/`). Build: `gmake` produces both binaries. Running as the desktop user needs the source tree writable by that user (the repo is group `operator`). Viewer: ``` MPEGVIEW_AUDIO=1 MPEGVIEW_DSP=/dev/dsp4 ./spoon -r ``` `MPEGVIEW_DSP` is required on this host: `/dev/dsp` is `hw.snd.default_unit`, and this unit defaults to a **digital** output with nothing plugged into it (measured 2026-10-25: unit 9 = X-Fi Rear Digital / S/PDIF, so the picture came up and there was no sound). Point it at whatever the speakers are on — `MPEGVIEW_DSP=/dev/dsp11` is the value this machine has used most (`cat /dev/sndstat` lists the units). **Never use `mixer -d N` to inspect this: it rewrites `hw.snd.default_unit`.** MPEG mode sounds out of the stream's own audio, so `MPEGVIEW_AUDIO` is only needed for `-r` (raw PCM). The bar across the top of the window changes the live options (volume, low-pass, notch, pad, input, Y/C settings) by clicking. The window is **resizable, and resizing enlarges or shrinks the picture**: the frame is scaled to fit the area under the bar with its aspect ratio kept and centred (black letterbox), so dragging the window bigger is the zoom. The bar stays at its native pixel size and the chips re-wrap to the window width, so clicks keep landing where the labels are. Measured: 720x532 -> x1.000 (native 720x480), 1440x900 -> x1.817 (1308x872 at 66,28), 1280x720 -> x1.442, 480x320 -> x0.508, minimum 400x300. `SPOON_SCALEDBG=1` prints the decision each time the window size changes. Input: `INPUT=composite` (default) or `INPUT=svideo` (add `INPUTPIN=<0..3>`). `INPUT=svideo` passes `-S ` -> `0x103 = 0xd0` (luma In1 + chroma In5, measured 2026-10-25); composite is `0x103 = 0xe1` (luma In2 + chroma In6, through the NEC pair). Both use `0x401` bits 1-2 = 2 (Y/C mode). The bar's IN chip toggles between `COMP` and `SVID`. ## Audio: works The MPEG stream carries **mp2, 48 kHz, stereo, 224 kbit/s** (`ffprobe` reports `Stream #0:1`). It is the real program audio, not hum: the per-second RMS follows the source and the spectrum sits in 120 Hz-8 kHz. **The bug that hid it: a READ transaction to a write-only codec.** The WM8739 datasheet says it "will only respond to the R/W bit indicating a write". For a long time every register write was followed by a read-back at the same address, and that read wrecked the codec's control state, discarding the registers just written - so R6 took effect (the analogue side came up, VMID at AVDD/2) while R7/R9 were dropped and BCLK/LRCK stayed tri-stated. Removing the read-back produced audio immediately (75 PES in the first run, 395 over 20 s). * `WM8739READ=1` restores the read-back for diagnostics, and it brings the zero back - **never re-introduce it**, including in any "does the part answer?" probe. A write ACK is the way to test this part (`I2CPROBE` sends both directions and shows 0x1a ACKing a write while the reserved 0x01/0x7f NACK as a control). * The codec is brought up on **every** capture (`--codec ADDR`, default `0x1a`); `--no-codec` opts out. * `R8` must be `0x02` (384 fs, from SA_MCLK = 18.43 MHz). At `0x00` (256 fs) LRCK comes out at 72 kHz and the encoder's PTS clock runs ~1.45x fast, which is audible as sped-up audio. `WM8739R8=` overrides. * `R6` must be `0x5A`: the datasheet requires the unused bits to be set, so `0x09` powers down the line input and `0x00` clears required bits. * **Never write `0x51`.** It is the 24C64WP EEPROM, not an audio part; one write bricked this unit (CH341A recovery). The codec range `0x50`-`0x5f` is refused by `kare`. `sound is missing but the picture works` is normally the OSS **output** device, not the capture - see the `MPEGVIEW_DSP` note under "Viewer". The full investigation, including what was ruled out and how (MUTE_AUDIO, ENCDIAG, the CX23416 GPIO sweeps, the Windows driver's captured sequence, the NJM2750/UPD64031A/UPD64084 analysis) is in `docs/NOTES.md` - that file is a dated ledger and is **not tracked in git**, so it lives only on this machine. The audio codec bring-up is worth knowing about too: the WM8739 is a **write-only** 2-wire slave (datasheet: "will only respond to the R/W bit indicating a write"), so a read NACK is *not* evidence it is absent. `I2CPROBE=1` measures both directions and shows 0x1a answering a write ACK while the I2C-reserved addresses 0x01/0x7f NACK a write as a control.