diff options
| author | Charlie Root <root@dangofactory.local> | 2026-10-25 10:15:31 +0900 |
|---|---|---|
| committer | Charlie Root <root@dangofactory.local> | 2026-10-25 10:15:31 +0900 |
| commit | 7318435b737394904bb1c5ce9fe013b0eee1882f (patch) | |
| tree | 41adf14909f5f8d15c8e1c5db361e6ea0ac97598 /src/capture.c | |
| download | mpegcap-baseline-1016.tar.gz mpegcap-baseline-1016.zip | |
kare / spoon - CX23416 + FX2 USB MPEG capture toolHEADbaseline-1016
Capture tool for the I-O DATA GV-MVP/RZ2 (04bb:0514): Conexant CX23416 MPEG
encoder behind an EZ-USB FX2 bridge, with a CX25837 video decoder (I2C 0x44)
and a WM8739 audio ADC (I2C 0x1a). libusb-1.0, C99, built with GNU make on
FreeBSD/Linux.
kare capture: MPEG-2 PS or raw HM12 (y4m), MPEG audio, --pcm WAV,
live parameter control through CTLFILE
spoon live viewer: pipe -> ffmpeg -> SDL2, clickable control bar,
resizable window, raw PCM playback on -r
State at this commit:
- MPEG-2 PS 720x480 ~6.5 Mbit/s, colour, mp2 48 kHz stereo 224 kbit/s
- raw HM12 (NV12_16L16, 518,416-byte frames) with selective median denoise
- S-Video works: luma In1 + chroma In5 (0x103=0xd0); RCA is In2+In6 (0xe1)
- VBI cropped: 0x0474=24 with 0x0477=28 (they must move together, +4)
- audio: never issue an I2C READ to the WM8739 (write-only part)
History note: this repository was squashed to a single commit on 2026-10-25
because the earlier history carried the CX23416 firmware images and the vendor
trees. Those still exist on the working copy (firmware/, ref/) and are in
.gitignore; only src/, include/, Makefile, README.md and .gitignore are
versioned. The pre-squash history is preserved locally as a bundle.
Diffstat (limited to 'src/capture.c')
| -rw-r--r-- | src/capture.c | 210 |
1 files changed, 210 insertions, 0 deletions
diff --git a/src/capture.c b/src/capture.c new file mode 100644 index 0000000..6063991 --- /dev/null +++ b/src/capture.c @@ -0,0 +1,210 @@ +/* + * capture.c - USB bulk stream capture from EP 0x84 + * + * Part of mpegcap. + * + * The MPEG-2 program stream arrives on bulk endpoint 0x84. We use + * libusb's async API with a small pool of transfers so that there is + * always a pending read (required for reliable high-bandwidth capture). + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "pvrusb2.h" +#include "capture.h" + +#define NUM_TRANSFERS 4 +#define TRANSFER_SIZE 0x20000 + +struct pvr2_capture { + pvr2_dev *p; + pvr2_data_cb cb; + void *user; + libusb_transfer *xfer[NUM_TRANSFERS]; + uint8_t *buf[NUM_TRANSFERS]; + volatile int stop; + volatile int errors; + /* diagnostics: USB stream health. Short/zero-length completions mean + * the encoder is not keeping up and frames are being lost, which shows + * up as a large latency in the player because the PTS still advances at + * the nominal frame rate. */ + volatile long pkts; /* completed non-empty transfers */ + volatile long short_xfer; /* completed, but < TRANSFER_SIZE */ + volatile long zero_xfer; /* completed with 0 bytes */ + volatile long stall; /* LIBUSB_TRANSFER_STALL */ + volatile long overflow; /* LIBUSB_TRANSFER_OVERFLOW */ + /* encoder (type, subtype) this capture was started with, so the stop + * command can be sent with the same values before streaming off */ + int enc_stop_valid; + uint32_t enc_type, enc_subtype; +}; + +static void LIBUSB_CALL capture_cb(struct libusb_transfer *tr) +{ + struct pvr2_capture *c = tr->user_data; + + if (tr->status == LIBUSB_TRANSFER_COMPLETED && tr->actual_length > 0) { + c->pkts++; + if (tr->actual_length < TRANSFER_SIZE) + c->short_xfer++; + if (c->cb) + c->cb(tr->buffer, tr->actual_length, c->user); + } else if (tr->status == LIBUSB_TRANSFER_COMPLETED) { + c->zero_xfer++; /* empty completion: no data, not an error */ + } else if (tr->status == LIBUSB_TRANSFER_STALL) { + c->stall++; + c->errors++; + if (c->errors > 100) + c->stop = 1; + return; + } else if (tr->status == LIBUSB_TRANSFER_OVERFLOW) { + c->overflow++; + c->errors++; + if (c->errors > 100) + c->stop = 1; + return; + } else if (tr->status == LIBUSB_TRANSFER_NO_DEVICE || + tr->status == LIBUSB_TRANSFER_ERROR) { + c->errors++; + if (c->errors > 100) + c->stop = 1; + return; + } + + if (!c->stop) { + if (libusb_submit_transfer(tr) != 0) { + c->errors++; + if (c->errors > 100) + c->stop = 1; + } + } +} + +pvr2_capture_t *pvr2_capture_new(pvr2_dev *p, pvr2_data_cb cb, void *user) +{ + struct pvr2_capture *c = calloc(1, sizeof(*c)); + int i; + + if (!c) + return NULL; + c->p = p; + c->cb = cb; + c->user = user; + + for (i = 0; i < NUM_TRANSFERS; i++) { + c->buf[i] = malloc(TRANSFER_SIZE); + c->xfer[i] = libusb_alloc_transfer(0); + if (!c->buf[i] || !c->xfer[i]) { + pvr2_capture_free(c); + return NULL; + } + libusb_fill_bulk_transfer(c->xfer[i], p->dev, EP_VIDEO_IN, + c->buf[i], TRANSFER_SIZE, + capture_cb, c, 0); + } + return c; +} + +void pvr2_capture_stop(pvr2_capture_t *c) +{ + if (c) + c->stop = 1; +} + +void pvr2_capture_set_enc_stop(pvr2_capture_t *c, uint32_t captype, + uint32_t subtype) +{ + if (!c) + return; + c->enc_type = captype; + c->enc_subtype = subtype; + c->enc_stop_valid = 1; +} + +int pvr2_capture_run(pvr2_capture_t *c) +{ + int i, r; + + for (i = 0; i < NUM_TRANSFERS; i++) { + r = libusb_submit_transfer(c->xfer[i]); + if (r != 0) { + fprintf(stderr, "submit transfer failed: %s\n", + libusb_error_name(r)); + return -1; + } + } + + if (pvr2_streaming_on(c->p) < 0) { + fprintf(stderr, "streaming on (0x36) failed\n"); + return -1; + } + + while (!c->stop) { + struct timeval tv = { 0, 250000 }; + r = libusb_handle_events_timeout_completed(c->p->ctx, &tv, NULL); + if (r != 0 && r != LIBUSB_ERROR_TIMEOUT) { + if (r == LIBUSB_ERROR_INTERRUPTED) + continue; + fprintf(stderr, "handle_events: %s\n", libusb_error_name(r)); + break; + } + } + + /* + * ★ Tell the encoder to end the capture BEFORE switching USB + * streaming off (vendor order: 0x82 then 0x37; yacxm does the same + * and then waits for the EOS interrupt). Without this the encoder + * keeps its mode and frame buffer, so the next run -- especially one + * that changes capture type (MPEG -> raw) -- came up stuck showing + * the previous buffer. NOENCSTOP=1 skips it, which is how that + * symptom is reproduced on purpose (A/B). + */ + if (c->enc_stop_valid && !getenv("NOENCSTOP")) { + uint32_t when = (c->enc_type == 0) ? 0 : 1; /* MPEG: end of GOP */ + pvr2_encoder_stop(c->p, c->enc_type, c->enc_subtype, when); + /* let the firmware deliver the tail / EOS */ + { + struct timeval tv = { 0, 300000 }; + libusb_handle_events_timeout_completed(c->p->ctx, &tv, + NULL); + } + } + + pvr2_streaming_off(c->p); + for (i = 0; i < NUM_TRANSFERS; i++) + libusb_cancel_transfer(c->xfer[i]); + { + struct timeval tv = { 0, 200000 }; + libusb_handle_events_timeout_completed(c->p->ctx, &tv, NULL); + } + return c->errors ? -1 : 0; +} + +void pvr2_capture_stats(pvr2_capture_t *c, pvr2_capture_stats_t *st) +{ + if (!st) + return; + memset(st, 0, sizeof(*st)); + if (!c) + return; + st->packets = c->pkts; + st->short_xfer = c->short_xfer; + st->zero_xfer = c->zero_xfer; + st->stall = c->stall; + st->overflow = c->overflow; + st->errors = c->errors; +} + +void pvr2_capture_free(pvr2_capture_t *c) +{ + int i; + if (!c) + return; + for (i = 0; i < NUM_TRANSFERS; i++) { + if (c->xfer[i]) + libusb_free_transfer(c->xfer[i]); + free(c->buf[i]); + } + free(c); +} |
