summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/capture.h36
-rw-r--r--include/hm12.h88
-rw-r--r--include/pvrusb2.h231
3 files changed, 355 insertions, 0 deletions
diff --git a/include/capture.h b/include/capture.h
new file mode 100644
index 0000000..928ed0c
--- /dev/null
+++ b/include/capture.h
@@ -0,0 +1,36 @@
+/*
+ * capture.h - capture API
+ */
+#ifndef PVR2_CAPTURE_H
+#define PVR2_CAPTURE_H
+
+#include "pvrusb2.h"
+
+typedef struct pvr2_capture pvr2_capture_t;
+
+/* USB stream health. short/zero completions and stalls indicate the
+ * encoder is not keeping up, which makes the player lag behind live. */
+typedef struct {
+ long packets; /* completed non-empty bulk transfers */
+ long short_xfer; /* completed, but fewer bytes than requested */
+ long zero_xfer; /* completed with 0 bytes */
+ long stall; /* LIBUSB_TRANSFER_STALL */
+ long overflow; /* LIBUSB_TRANSFER_OVERFLOW */
+ long errors; /* hard errors */
+} pvr2_capture_stats_t;
+
+pvr2_capture_t *pvr2_capture_new(pvr2_dev *p, pvr2_data_cb cb, void *user);
+int pvr2_capture_run(pvr2_capture_t *c);
+void pvr2_capture_stop(pvr2_capture_t *c);
+void pvr2_capture_free(pvr2_capture_t *c);
+void pvr2_capture_stats(pvr2_capture_t *c, pvr2_capture_stats_t *st);
+/* Tell the capture context which (type, subtype) the encoder was started
+ * with, so it can send CX2341X_ENC_STOP_CAPTURE (0x82) BEFORE switching
+ * USB streaming off -- the vendor order is STOP_CAPTURE then 0x37. Not
+ * calling it leaves the encoder holding the previous mode/buffer, which is
+ * what made a later mode switch (MPEG -> raw) come up stuck on the old
+ * buffer. */
+void pvr2_capture_set_enc_stop(pvr2_capture_t *c, uint32_t captype,
+ uint32_t subtype);
+
+#endif
diff --git a/include/hm12.h b/include/hm12.h
new file mode 100644
index 0000000..c88a730
--- /dev/null
+++ b/include/hm12.h
@@ -0,0 +1,88 @@
+/*
+ * hm12.h - HM12 raw YUV 4:2:0 handling
+ */
+#ifndef PVR2_HM12_H
+#define PVR2_HM12_H
+
+#include <stdio.h>
+#include <stddef.h>
+
+/* 1 = apply the light luma filter to the raw planes (see hm12.c). The -N
+ * command-line flag clears it so the filter can be A/B'd against the
+ * unfiltered picture on the same source. */
+extern int hm12_filter;
+extern int hm12_hfilter;
+
+/* y4m interlace tag: 0 = progressive (Ip), 1 = top field first (It),
+ * 2 = bottom field first (Ib). Measured on this hardware the raw stream
+ * is progressive - lines within a frame differ by 3.95 while the same
+ * field across two frames differs by 13.98, i.e. the fields are not
+ * separated in time - so Ip is the default. Selectable for testing. */
+extern int hm12_interlace;
+
+/* Distance used by the vertical 1-2-1 pass. 1 = neighbouring lines
+ * (right for progressive material), 2 = same field, two lines away
+ * (right if the stream really were interlaced). */
+extern int hm12_vstep;
+
+/*
+ * Luma-only output, for checking vertical-line artefacts.
+ *
+ * The user wants to confirm whether the "jaggedness" is really vertical
+ * lines by looking at the Y plane on its own. This writes a plain
+ * greyscale YUV4MPEG2 at full 720x480: the luma goes in the Y slot and
+ * both chroma planes are held neutral, so VLC (or ffmpeg) shows it as a
+ * greyscale picture with no colour cast to confuse the reading.
+ *
+ * Returns 0 on success, -1 on failure.
+ */
+int hm12_luma_open(const char *path);
+int hm12_luma_write(const unsigned char *y, int w, int h);
+void hm12_luma_close(void);
+
+/*
+ * Per-plane debug output.
+ *
+ * An HM12 conversion can be subtly wrong - a swapped U/V pair, a chroma
+ * tile of the wrong size, a missing de-interleave - and the composite
+ * still looks like video, just with the wrong colours. Dumping each
+ * plane separately makes that obvious: the luma should be a sharp
+ * picture, and the two chroma planes should trace the same shapes as the
+ * luma (at half resolution) rather than scattering.
+ *
+ * YUV4MPEG2 is used rather than PGM so the result can just be opened in
+ * VLC (which PGM is not). Each plane is written as its own video with
+ * the other two held at neutral, so a viewer shows it as greyscale:
+ *
+ * PFX.Y.y4m luma in the Y slot, U=V=128
+ * PFX.U.y4m Cb in the U slot, Y=128, V=128
+ * PFX.V.y4m Cr in the V slot, Y=128, U=128
+ * PFX.uv.y4m (U-128)*4+128 in U and (V-128)*4+128 in V, so a single
+ * view shows the colour cast direction: too blue means the
+ * U/V pair is the wrong way round
+ *
+ * All four are full 720x480 YUV4MPEG2 files: the chroma planes are
+ * upsampled to luma size so each file is a well-formed video of the same
+ * dimensions (a 360x240 header carrying 720x480 data would be rejected by
+ * the demuxer).
+ *
+ * Every frame is appended, so the files play straight through.
+ *
+ * Returns 0 on success, -1 on failure.
+ */
+int hm12_planes_open(const char *prefix);
+int hm12_planes_write(const unsigned char *y, const unsigned char *u,
+ const unsigned char *v, int w, int h);
+void hm12_planes_close(void);
+
+void hm12_de_macro_y(unsigned char *dst, unsigned char *src,
+ int dstride, int w, int h);
+void hm12_de_macro_uv(unsigned char *dstu, unsigned char *dstv,
+ unsigned char *src, int dstride, int w, int h);
+size_t hm12_frame_size(int w, int h);
+int hm12_write_y4m_header(FILE *f, int w, int h, unsigned int fps_num,
+ unsigned int fps_den);
+int hm12_write_frame_y4m(FILE *f, const unsigned char *hm12, int w, int h,
+ unsigned char *scratch);
+
+#endif
diff --git a/include/pvrusb2.h b/include/pvrusb2.h
new file mode 100644
index 0000000..47237f4
--- /dev/null
+++ b/include/pvrusb2.h
@@ -0,0 +1,231 @@
+/*
+ * pvrusb2.h - Conexant CX23416 + EZ-USB FX2 based USB MPEG capture
+ *
+ * Cross-platform (Linux / Windows / FreeBSD / macOS) libusb-1.0 backend.
+ *
+ * This is an independent C re-implementation of the protocol used by the
+ * Conexant reference USB2 MPEG capture modules (GV-MVP/RZ2, Terratec
+ * Grabster AV400, Hauppauge WinTV PVR-USB2, ...).
+ *
+ * Protocol sources:
+ * - Linux kernel drivers/media/usb/pvrusb2/ (FX2 command layer)
+ * - Linux kernel include/media/drv-intf/cx2341x.h (encoder API)
+ * - yamori813 "mpeg-usb" mpegcapt.c (libusb reference, Mac OS X)
+ * - Qiita: "USB MPEGモジュール" https://qiita.com/yamori813/items/ba488fb0bab96191ff11
+ */
+#ifndef PVRUSB2_H
+#define PVRUSB2_H
+
+#include <stdint.h>
+#include <stddef.h>
+#include <libusb.h>
+
+/* ------------------------------------------------------------------ */
+/* USB endpoints (after FX2 firmware is running) */
+/* ------------------------------------------------------------------ */
+#define EP_CMD_OUT 0x01 /* bulk OUT : FX2 command / register write */
+#define EP_CMD_IN 0x81 /* bulk IN : command response */
+#define EP_FW_OUT 0x02 /* bulk OUT : CX23416 firmware download */
+#define EP_VIDEO_IN 0x84 /* bulk IN : MPEG video stream */
+#define EP_VBI_IN 0x88 /* bulk IN : VBI stream */
+
+/* ------------------------------------------------------------------ */
+/* Cold-state FX2 (no firmware loaded yet) - Cypress default IDs */
+/* ------------------------------------------------------------------ */
+#define FX2_COLD_VID 0x04b4
+#define FX2_COLD_PID 0x8613
+
+/* Known warm (firmware loaded) device IDs. A device specific VID/PID
+ * can be supplied on the command line to override. */
+/*
+ * 04bb:0514 is the I-O DATA GV-MVP/RZ2 and is what the unit enumerates as
+ * when its own firmware is resident - which is how it was captured from
+ * the start, and how it still works.
+ *
+ * A change to 0547:1002 was made on the strength of a line in the INF
+ * (USB\VID_0547&PID_1002, labelled CONEXANT) and a momentary sighting of
+ * that pair on the bus. That was wrong. 0547:1002 is the ID the FX2
+ * presents mid-reset, not the one it settles on, and switching to it is
+ * what made the unit unreachable afterwards. Reverted.
+ */
+#define DEFAULT_VID 0x04bb /* I-O DATA GV-MVP/RZ2 */
+#define DEFAULT_PID 0x0514
+
+/* ------------------------------------------------------------------ */
+/* FX2 command codes (pvrusb2-fx2-cmd.h) */
+/* ------------------------------------------------------------------ */
+#define FX2CMD_MEM_WRITE_DWORD 0x01u
+#define FX2CMD_MEM_READ_DWORD 0x02u
+#define FX2CMD_REG_WRITE 0x04u
+#define FX2CMD_REG_READ 0x05u
+#define FX2CMD_MEMSEL 0x06u
+#define FX2CMD_I2C_WRITE 0x08u
+#define FX2CMD_I2C_READ 0x09u
+#define FX2CMD_GET_USB_SPEED 0x0bu
+#define FX2CMD_MEM_READ_64BYTES 0x28u
+#define FX2CMD_STREAMING_ON 0x36u
+#define FX2CMD_STREAMING_OFF 0x37u
+#define FX2CMD_FWPOST1 0x52u
+#define FX2CMD_POWER_OFF 0xdcu
+#define FX2CMD_POWER_ON 0xdeu
+#define FX2CMD_DEEP_RESET 0xddu
+#define FX2CMD_GET_EEPROM_ADDR 0xebu
+/* Block I2C write: [0x0c][dev][count][bytes_per_entry=3] then
+ * count x (reg_hi, reg_lo, value). This is how the Windows driver writes
+ * every multi-register block -- the whole 0x0800-0x091b audio DSP block
+ * including the 0x0900-0x0903 words goes out as ONE of these, never as a
+ * sequence of single-register 0x08 writes. Recovered from a USB capture of
+ * the driver (see docs/NOTES.md). */
+#define FX2CMD_I2C_WRITE_BLOCK 0x0cu
+
+/* ------------------------------------------------------------------ */
+/* CX23416 encoder MBOX (ivtv / cx2341x heritage) */
+/* ------------------------------------------------------------------ */
+#define CX_MBOX_BASE 0x44
+#define CX_MBOX_DRIVER_BUSY 0x01
+#define CX_MBOX_DRIVER_DONE 0x02
+#define CX_MBOX_FIRMWARE_DONE 0x04
+
+/* CX23416 register addresses used during init */
+#define CX_REG_INTR_MASK 0x0048
+#define CX_REG_SDRAM_REFRESH 0x07f8
+#define CX_REG_SDRAM_PRECHARGE 0x07fc
+#define CX_REG_I2C_CLOCK 0x0700
+#define CX_REG_GPIO_OUT 0x900c
+#define CX_REG_GPIO_DIR 0x9020
+#define CX_REG_VPU_RESET 0x9054
+#define CX_REG_VPU_CTRL 0x9058
+#define CX_REG_APU_CMD 0xa064
+
+/* ------------------------------------------------------------------ */
+/* Video decoder (CX25843) I2C device address */
+/* ------------------------------------------------------------------ */
+#define CX2584X_I2C_ADDR 0x44
+
+/* Encoder API commands (cx2341x.h) */
+#define CX2341X_ENC_PING_FW 0x80
+#define CX2341X_ENC_START_CAPTURE 0x81
+#define CX2341X_ENC_STOP_CAPTURE 0x82
+#define CX2341X_ENC_SET_FRAME_RATE 0x8f
+#define CX2341X_ENC_SET_AUDIO_ID 0x89
+#define CX2341X_ENC_SET_FRAME_SIZE 0x91
+#define CX2341X_ENC_SET_BIT_RATE 0x95
+#define CX2341X_ENC_SET_GOP_PROPERTIES 0x97
+#define CX2341X_ENC_SET_ASPECT_RATIO 0x99
+#define CX2341X_ENC_SET_SPATIAL_FILTER 0xa1
+#define CX2341X_ENC_SET_3_2_PULLDOWN 0xb1
+#define CX2341X_ENC_SET_VBI_LINE 0xb7
+#define CX2341X_ENC_SET_STREAM_TYPE 0xb9
+#define CX2341X_ENC_SET_OUTPUT_PORT 0xbb
+#define CX2341X_ENC_SET_AUDIO_PROP 0xbd
+#define CX2341X_ENC_GET_VERSION 0xc4
+#define CX2341X_ENC_SET_GOP_CLOSURE 0xc5
+#define CX2341X_ENC_SET_PGM_INDEX_INFO 0xc7
+#define CX2341X_ENC_INITIALIZE_INPUT 0xcd
+#define CX2341X_ENC_SET_EVENT_NOTIFY 0xd5
+#define CX2341X_ENC_SET_NUM_VSYNC_LINES 0xd6
+#define CX2341X_ENC_MUTE_VIDEO 0xd9
+#define CX2341X_ENC_MUTE_AUDIO 0xda
+#define CX2341X_ENC_MISC 0xdc
+
+/* ------------------------------------------------------------------ */
+/* Context */
+/* ------------------------------------------------------------------ */
+typedef struct {
+ libusb_context *ctx;
+ libusb_device_handle *dev;
+ uint16_t vid;
+ uint16_t pid;
+ int verbose;
+} pvr2_dev;
+
+/* ---- bulk helpers (exposed for the encoder layer) ---- */
+int bulk_cmd_out(pvr2_dev *p, const uint8_t *buf, int len);
+int bulk_cmd_in(pvr2_dev *p, uint8_t *buf, int len);
+int pvr2_raw_in(pvr2_dev *p, uint8_t ep, uint8_t *buf, int len);
+
+/* ---- device management ---- */
+int pvr2_init(pvr2_dev *p);
+void pvr2_exit(pvr2_dev *p);
+int pvr2_find_device(pvr2_dev *p, uint16_t vid, uint16_t pid);
+int pvr2_scan(pvr2_dev *p);
+int pvr2_get_usb_speed(pvr2_dev *p);
+
+/* ---- FX2 firmware (cold state only) ---- */
+int pvr2_upload_fx2(pvr2_dev *p, const uint8_t *fw, size_t len);
+
+/* ---- low level FX2 commands ---- */
+int pvr2_simple_cmd(pvr2_dev *p, uint8_t cmd, const uint8_t *arg, int arglen);
+int pvr2_write_reg(pvr2_dev *p, uint16_t reg, uint32_t val);
+int pvr2_read_reg(pvr2_dev *p, uint16_t reg, uint32_t *val);
+int pvr2_gpio_dir(pvr2_dev *p, uint32_t mask, uint32_t val);
+int pvr2_gpio_out(pvr2_dev *p, uint32_t mask, uint32_t val);
+
+/* ---- I2C (relayed through FX2) ---- */
+int pvr2_i2c_write(pvr2_dev *p, uint8_t dev_addr, uint16_t reg,
+ const uint8_t *data, int len);
+int pvr2_i2c_write_block(pvr2_dev *p, uint8_t dev_addr,
+ const uint16_t *regs, const uint8_t *vals, int n);
+int pvr2_audio_block_write(pvr2_dev *p);
+int pvr2_i2c_read(pvr2_dev *p, uint8_t dev_addr, uint16_t reg,
+ uint8_t *data, int len);
+
+/* WM873x (Wolfson) packed-register I2C access (7-bit reg + 9-bit data) */
+int pvr2_wm873x_write(pvr2_dev *p, uint8_t dev_addr, uint8_t reg, uint16_t data);
+int pvr2_wm873x_read(pvr2_dev *p, uint8_t dev_addr, uint8_t reg, uint16_t *out);
+
+/* ---- CX23416 firmware ---- */
+int pvr2_upload_firmware2(pvr2_dev *p, const uint8_t *fw, size_t len);
+
+/* ---- encoder MBOX ---- */
+int pvr2_mbox_write(pvr2_dev *p, uint32_t addr, const uint32_t *data, int count);
+int pvr2_mbox_read(pvr2_dev *p, uint32_t addr, uint32_t *data, int count);
+int pvr2_enc_cmd(pvr2_dev *p, uint32_t cmd, const uint32_t *args, int nargs);
+int pvr2_enc_get_version(pvr2_dev *p, uint32_t *ver);
+int pvr2_enc_ping(pvr2_dev *p);
+int pvr2_enc_clear_flag(pvr2_dev *p, uint32_t bits);
+
+/* ---- high level setup ---- */
+int pvr2_encoder_preconfig(pvr2_dev *p);
+int pvr2_encoder_config(pvr2_dev *p, int sample_rate);
+int pvr2_encoder_config_driver(pvr2_dev *p, int sample_rate);
+int pvr2_encoder_start(pvr2_dev *p);
+int pvr2_encoder_start_ex(pvr2_dev *p, uint32_t captype, uint32_t subtype);
+int pvr2_encoder_stop(pvr2_dev *p, uint32_t captype, uint32_t subtype,
+ uint32_t when);
+int pvr2_cx2584x_init(pvr2_dev *p);
+int pvr2_cx2584x_input_change(pvr2_dev *p, int japan_audio);
+int pvr2_cx2584x_audio_config(pvr2_dev *p, int sample_rate);
+int pvr2_cx2584x_audio_config_opt(pvr2_dev *p, int sample_rate, int skipmask);
+#define AUD_SKIP_90X 0x1 /* 0x0900-0x0903 */
+#define AUD_SKIP_PLL 0x2 /* 0x0108-0x0113 + 0x0127 */
+#define AUD_SKIP_FRAC 0x4 /* 0x0128-0x012e */
+
+/* CX25837 init table extracted from p2usbRZ2.sys (apply=0 => read-only compare) */
+int pvr2_cx25837_ser_init(pvr2_dev *p, int apply);
+
+/* narrow 0x0400-0x040f slice, 1 byte per register (apply=0 => read-only) */
+int pvr2_cx25837_ser_test(pvr2_dev *p, int apply);
+
+/* CX25837 PLL/clock block 0x0130-0x0150, 1 byte per register */
+int pvr2_cx25837_pll_test(pvr2_dev *p, int apply);
+int pvr2_cx2584x_write_tables(pvr2_dev *p, int japan_audio);
+int pvr2_cx2584x_write_dec_table(pvr2_dev *p);
+int pvr2_cx2584x_write_audio_mode(pvr2_dev *p, int on);
+int pvr2_cx2584x_write_audio_table(pvr2_dev *p, int japan_audio);
+int pvr2_cx2584x_write_audio_dsp(pvr2_dev *p, int japan_audio);
+int pvr2_video_input(pvr2_dev *p, int composite);
+int pvr2_video_input_sel(pvr2_dev *p, int mode, int index);
+int pvr2_audio_init(pvr2_dev *p, int sample_rate);
+int pvr2_streaming_on(pvr2_dev *p);
+int pvr2_streaming_off(pvr2_dev *p);
+
+/* ---- capture loop (see capture.h) ---- */
+typedef void (*pvr2_data_cb)(const uint8_t *buf, size_t len, void *user);
+
+/* utilities */
+uint32_t pvr2_swab32(uint32_t v);
+void pvr2_swap32_buf(uint8_t *buf, size_t nblocks);
+int pvr2_load_file(const char *path, uint8_t **out, size_t *len);
+
+#endif /* PVRUSB2_H */