Rename backend to PCIES2MM and handle events

This commit is contained in:
Mark.lin
2026-07-13 16:56:03 +08:00
parent 3e02a374e0
commit 9639a797aa
174 changed files with 754 additions and 3999 deletions
@@ -0,0 +1,141 @@
#pragma once
#include <stdint.h>
#include <stddef.h>
#define PCIES2MM_MAX_PLANES 3
typedef enum pcies2mm_status_t
{
PCIES2MM_OK = 0,
PCIES2MM_EINVAL,
PCIES2MM_ENODEV,
PCIES2MM_ESTATE,
PCIES2MM_ENOTSUP,
PCIES2MM_ETIMEOUT,
PCIES2MM_EIO
} pcies2mm_status_t;
typedef enum pcies2mm_input_t
{
PCIES2MM_INPUT_UNKNOWN = 0,
PCIES2MM_INPUT_SDI = 1,
PCIES2MM_INPUT_HDMI = 2
} pcies2mm_input_t;
typedef enum pcies2mm_pixel_format_t
{
PCIES2MM_PIXFMT_UNKNOWN = 0,
PCIES2MM_PIXFMT_YUY2 = 1,
PCIES2MM_PIXFMT_UYVY = 2,
PCIES2MM_PIXFMT_RGB24 = 3,
PCIES2MM_PIXFMT_BGRX32 = 4,
PCIES2MM_PIXFMT_NV12 = 5,
PCIES2MM_PIXFMT_P010 = 6,
PCIES2MM_PIXFMT_Y210 = 7,
PCIES2MM_PIXFMT_YUV444 = 8
} pcies2mm_pixel_format_t;
typedef enum pcies2mm_stream_state_t
{
PCIES2MM_STREAM_STOPPED = 0,
PCIES2MM_STREAM_CONFIGURED = 1,
PCIES2MM_STREAM_RUNNING = 2
} pcies2mm_stream_state_t;
typedef struct pcies2mm_stream_desc_t
{
pcies2mm_input_t input;
uint32_t width;
uint32_t height;
pcies2mm_pixel_format_t pixel_format;
uint32_t buffer_count;
uint32_t flags;
} pcies2mm_stream_desc_t;
typedef struct pcies2mm_signal_status_t
{
int signal_locked;
pcies2mm_input_t input;
uint32_t width;
uint32_t height;
pcies2mm_pixel_format_t pixel_format;
uint32_t bit_depth;
uint32_t fpga_valid_mask; /* bit0:0x0c, bit1:0x18, bit2:0x1c, bit3:0x180 */
uint32_t fpga_width_valid; /* Non-zero when FPGA 0x10 read succeeded. */
uint32_t fpga_height_valid; /* Non-zero when FPGA 0x14 read succeeded. */
uint32_t fpga_width_raw; /* Raw FPGA 0x10 width register. */
uint32_t fpga_height_raw; /* Raw FPGA 0x14 height register. */
uint32_t fpga_video_format_raw; /* 0x0c: 0=yuv422, 1=rgb, 2=yuv444, 3=yuv420 */
uint32_t fpga_frame_rate_raw; /* 0x18 low nibble: frame-rate code */
uint32_t fpga_bit_depth_raw; /* 0x1c: 8 or 10 */
uint32_t fpga_status_raw; /* 0x180: bit0 SDI lock, bit1 SDI DDR, bit2 HDMI lock, bit3 HDMI DDR */
} pcies2mm_signal_status_t;
typedef struct pcies2mm_frame_t
{
const void *data; /* Valid until the next PcieS2mmCaptureSession::wait_frame() or close(). */
size_t data_size_bytes;
uint64_t frame_id;
uint32_t width;
uint32_t height;
pcies2mm_pixel_format_t pixel_format;
uint32_t bit_depth;
} pcies2mm_frame_t;
typedef struct pcies2mm_stream_stats_t
{
pcies2mm_stream_state_t state;
uint64_t frames_captured;
uint64_t frames_delivered;
uint64_t frames_dropped;
uint64_t dma_errors;
uint64_t interrupt_count;
} pcies2mm_stream_stats_t;
typedef struct pcies2mm_debug_state_t
{
int running;
int capture_active;
int data_worker_stop;
uint32_t pending_events;
uint64_t latest_sequence;
uint64_t delivered_sequence;
uint64_t active_delivery_slot;
uint64_t next_write_slot;
uint64_t ring_size;
} pcies2mm_debug_state_t;
typedef enum pcies2mm_event_type_t
{
PCIES2MM_EVENT_VIDEO_IRQ = 1,
PCIES2MM_EVENT_PLUG_IN = 2,
PCIES2MM_EVENT_PLUG_OUT = 3,
PCIES2MM_EVENT_CAPTURE_PAUSED = 4,
PCIES2MM_EVENT_CAPTURE_RESUMED = 5
} pcies2mm_event_type_t;
enum
{
PCIES2MM_EVENT_MASK_VIDEO_IRQ = 1u << 0,
PCIES2MM_EVENT_MASK_PLUG_IN = 1u << 1,
PCIES2MM_EVENT_MASK_PLUG_OUT = 1u << 2,
PCIES2MM_EVENT_MASK_CAPTURE_PAUSED = 1u << 3,
PCIES2MM_EVENT_MASK_CAPTURE_RESUMED = 1u << 4,
PCIES2MM_EVENT_MASK_HOTPLUG = PCIES2MM_EVENT_MASK_PLUG_IN |
PCIES2MM_EVENT_MASK_PLUG_OUT |
PCIES2MM_EVENT_MASK_CAPTURE_PAUSED |
PCIES2MM_EVENT_MASK_CAPTURE_RESUMED,
PCIES2MM_EVENT_MASK_DEFAULT = PCIES2MM_EVENT_MASK_HOTPLUG,
PCIES2MM_EVENT_MASK_ALL = PCIES2MM_EVENT_MASK_VIDEO_IRQ | PCIES2MM_EVENT_MASK_HOTPLUG
};
typedef struct pcies2mm_event_t
{
pcies2mm_event_type_t type;
uint32_t irq_bit;
uint32_t irq_mask;
uint64_t timestamp_ns;
} pcies2mm_event_t;
typedef void (*pcies2mm_event_callback_t)(const pcies2mm_event_t *event, void *user);
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,133 @@
#pragma once
#include "pcies2mm_backend_types.h"
#include <windows.h>
#include <atomic>
#include <condition_variable>
#include <cstddef>
#include <cstdint>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
namespace gvfg::internal
{
struct PcieS2mmDevice
{
std::wstring interface_path;
std::wstring friendly_name;
};
std::vector<PcieS2mmDevice> enumerate_pcies2mm_devices();
class PcieS2mmCaptureSession
{
public:
PcieS2mmCaptureSession();
PcieS2mmCaptureSession(const PcieS2mmCaptureSession &) = delete;
PcieS2mmCaptureSession &operator=(const PcieS2mmCaptureSession &) = delete;
~PcieS2mmCaptureSession();
pcies2mm_status_t open_device_index(size_t deviceIndex);
pcies2mm_status_t close();
pcies2mm_status_t set_input(pcies2mm_input_t input);
pcies2mm_status_t get_signal_status(pcies2mm_signal_status_t &out) const;
pcies2mm_status_t set_event_callback(pcies2mm_event_callback_t callback, void *user, uint32_t eventMask);
pcies2mm_status_t configure_stream(const pcies2mm_stream_desc_t &desc);
pcies2mm_status_t start_stream();
pcies2mm_status_t stop_stream();
pcies2mm_status_t wait_frame(uint32_t timeoutMs, pcies2mm_frame_t &out);
pcies2mm_status_t release_frame(const pcies2mm_frame_t &frame);
const char *last_error() const;
void get_debug_stats(pcies2mm_stream_stats_t &outStats,
uint64_t &outWaitTimeouts,
pcies2mm_debug_state_t &outDebugState) const;
private:
pcies2mm_status_t open_device(const PcieS2mmDevice &device);
void close_handles();
bool read_reg(uint32_t offset, uint32_t &out) const;
bool write_reg(uint32_t offset, uint32_t value) const;
bool read_reg_bar1(uint32_t offset, uint32_t &out) const;
bool write_reg_bar1(uint32_t offset, uint32_t value) const;
bool register_event(uint32_t channelIndex, uint32_t eventType, HANDLE eventHandle);
void unregister_event(uint32_t channelIndex, uint32_t eventType);
bool create_and_register_events(uint32_t channelIndex);
void unregister_events(uint32_t channelIndex);
void close_event_handles();
bool get_video_done_index(uint32_t channelIndex, uint32_t &doneIndex) const;
int get_frame(uint32_t channelIndex, uint32_t frameIndex, uint8_t *buffer, DWORD bufferSize) const;
void capture_thread_proc();
void handle_dma_event(uint32_t channel);
void handle_format_change_event(uint32_t channel);
void handle_plugin_event(uint32_t channel);
void handle_unplug_event(uint32_t channel);
bool refresh_stream_from_signal(bool resizeRing);
void publish_frame(size_t slotIndex, size_t bytes);
void emit_event(pcies2mm_event_type_t type, uint32_t irqBit, uint32_t irqMask) const;
pcies2mm_status_t fail(pcies2mm_status_t status, const char *where, DWORD winerr = GetLastError()) const;
void set_last_error(const std::string &message) const;
void clear_last_error() const;
uint32_t active_channel() const;
uint32_t video_event_mask() const;
uint32_t video_base() const;
uint32_t video_irq_mask_bit() const;
size_t frame_size_bytes() const;
struct FrameSlot
{
std::vector<uint8_t> data; // Frame byte storage for this ring slot.
size_t bytes = 0; // Number of valid bytes read into data.
uint64_t sequence = 0; // Monotonic frame sequence assigned on publish.
bool ready = false; // True after the data thread publishes a frame.
bool in_use = false; // True while writing or while the caller holds the slot.
};
std::wstring base_path_;
std::wstring friendly_name_;
HANDLE device_ = INVALID_HANDLE_VALUE;
HANDLE dma_event_ = nullptr;
HANDLE format_change_event_ = nullptr;
HANDLE plug_in_event_ = nullptr;
HANDLE plug_out_event_ = nullptr;
pcies2mm_stream_desc_t stream_desc_{};
uint32_t stream_bit_depth_ = 8;
pcies2mm_input_t input_ = PCIES2MM_INPUT_SDI;
bool opened_ = false;
bool configured_ = false;
std::atomic<bool> running_{false};
std::atomic<bool> capture_active_{false};
std::thread capture_thread_;
mutable std::mutex mutex_;
mutable std::mutex event_callback_mutex_;
std::condition_variable frame_cv_;
std::condition_variable data_cv_;
pcies2mm_event_callback_t event_callback_ = nullptr;
void *event_callback_user_ = nullptr;
uint32_t event_mask_filter_ = PCIES2MM_EVENT_MASK_DEFAULT;
uint32_t pending_events_ = 0;
std::vector<FrameSlot> frame_ring_;
size_t next_write_slot_ = 0;
size_t active_delivery_slot_ = static_cast<size_t>(-1);
uint64_t latest_sequence_ = 0;
uint64_t delivered_sequence_ = 0;
uint64_t wait_timeout_count_ = 0;
bool stream_error_ = false;
pcies2mm_stream_stats_t stats_{};
mutable std::string last_error_;
};
}
@@ -0,0 +1,41 @@
/*
* PCIES2MM driver public contract used by the GVFG backend.
*/
#pragma once
#include <stdint.h>
#include <windows.h>
#define PCIES2MM_IOCTL(index, method, access) CTL_CODE(FILE_DEVICE_UNKNOWN, index, method, access)
#define IOCTL_PCIES2MM_WRITE_REG PCIES2MM_IOCTL(0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_PCIES2MM_READ_REG PCIES2MM_IOCTL(0x801, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_PCIES2MM_REGISTER_EVENT PCIES2MM_IOCTL(0x802, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_PCIES2MM_GET_FRAME PCIES2MM_IOCTL(0x803, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
#define IOCTL_PCIES2MM_UNREGISTER_EVENT PCIES2MM_IOCTL(0x804, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_PCIES2MM_WRITE_REG_BAR1 PCIES2MM_IOCTL(0x805, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_PCIES2MM_READ_REG_BAR1 PCIES2MM_IOCTL(0x806, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_PCIES2MM_GET_VIDEO_DONE_INDEX PCIES2MM_IOCTL(0x807, METHOD_BUFFERED, FILE_ANY_ACCESS)
enum
{
PCIES2MM_EVENT_TYPE_VIDEO_DMA = 0,
PCIES2MM_EVENT_TYPE_VIDEO_FORMAT_CHANGE = 1,
PCIES2MM_EVENT_TYPE_VIDEO_PLUGIN = 2,
PCIES2MM_EVENT_TYPE_VIDEO_UNPLUG = 3,
PCIES2MM_EVENT_TYPE_AUDIO_DMA = 4
};
typedef struct PCIES2MM_REG_ACCESS
{
ULONG Offset;
ULONG Value;
} PCIES2MM_REG_ACCESS;
typedef struct PCIES2MM_EVENT_REG
{
ULONG Type;
ULONG ChannelIndex;
HANDLE EventHandle;
} PCIES2MM_EVENT_REG;