Refine GVFG capture API

This commit is contained in:
Mark.lin
2026-07-28 17:38:44 +08:00
parent 5662d74df9
commit d9f194c1ab
6 changed files with 375 additions and 410 deletions
+4 -20
View File
@@ -16,7 +16,7 @@ preview failures,不顯示硬體或 backend implementation details。
```text ```text
gvfg_enumerate_devices gvfg_enumerate_devices
-> gvfg_create -> gvfg_create
-> gvfg_open -> gvfg_open_channel
-> gvfg_start -> gvfg_start
-> loop: -> loop:
gvfg_read_frame gvfg_read_frame
@@ -146,13 +146,10 @@ Memory layout、color metadata 與 timestamp metadata 不會繼續塞進這個 s
typedef struct typedef struct
{ {
uint32_t struct_size; uint32_t struct_size;
uint32_t layout_flags;
int row_bytes;
int plane_count; int plane_count;
const void *plane_data[GVFG_MAX_PLANES]; const void *plane_data[GVFG_MAX_PLANES];
int plane_stride[GVFG_MAX_PLANES]; int plane_stride[GVFG_MAX_PLANES];
uint64_t plane_size[GVFG_MAX_PLANES]; uint64_t plane_size[GVFG_MAX_PLANES];
uint64_t plane_offset[GVFG_MAX_PLANES];
} gvfg_frame_layout_t; } gvfg_frame_layout_t;
``` ```
@@ -160,15 +157,6 @@ typedef struct
`gvfg_get_frame_layout()`,並使用 `plane_data[]`、`plane_stride[]`、 `gvfg_get_frame_layout()`,並使用 `plane_data[]`、`plane_stride[]`、
`plane_size[]`,不要自己用 width 和 pixel format 猜 stride。 `plane_size[]`,不要自己用 width 和 pixel format 猜 stride。
`gvfg_frame_t::data` 和 `data_size` 仍然描述整個 native buffer。`plane_offset[]`
是從 `data` 開始算的 byte offset,未來 driver 如果回報 padded 或 non-default
DMA layout,也可以在不改 `gvfg_frame_t` 的情況下支援。
`layout_flags` 是 `gvfg_frame_layout_flags_t` bitmask。目前 backend 會設
`GVFG_FRAME_LAYOUT_CONTIGUOUS` 和 `GVFG_FRAME_LAYOUT_SDK_DERIVED`,表示 planes
位在同一個 native DMA buffer 裡,而且 pitch 是 SDK 依照 known native format
推導出來的。
### ABI 與 metadata 擴充規則 ### ABI 與 metadata 擴充規則
- `gvfg_handle` 永遠保持 opaque。 - `gvfg_handle` 永遠保持 opaque。
@@ -240,9 +228,6 @@ Customer event 保持 driver-neutral:
- `GVFG_EVENT_CAPTURE_PAUSED` - `GVFG_EVENT_CAPTURE_PAUSED`
- `GVFG_EVENT_CAPTURE_RESUMED` - `GVFG_EVENT_CAPTURE_RESUMED`
`GVFG_EVENT_PLUG_IN` 與 `GVFG_EVENT_PLUG_OUT` 保留為 source-compatible aliases;
它們表示所選 channel 的 input signal 插拔,不是 PCIe capture device 的實體插拔。
Frame interrupts、IRQ bit numbers、IRQ masks 都是 internal details,不應出現在 Frame interrupts、IRQ bit numbers、IRQ masks 都是 internal details,不應出現在
`gvfg_capture.h`。 `gvfg_capture.h`。
@@ -269,17 +254,16 @@ gvfg_status_t gvfg_destroy(gvfg_handle handle);
建立或銷毀 session handle。銷毀 running handle 時會先 stop capture。 建立或銷毀 session handle。銷毀 running handle 時會先 stop capture。
### `gvfg_open` / `gvfg_open_channel` ### `gvfg_open_channel`
```c ```c
gvfg_status_t gvfg_open(gvfg_handle handle, int device_index);
gvfg_status_t gvfg_open_channel(gvfg_handle handle, gvfg_status_t gvfg_open_channel(gvfg_handle handle,
int device_index, int device_index,
int channel_index); int channel_index);
``` ```
用 `gvfg_enumerate_devices()` 得到的 device index 開啟 device。`gvfg_open()` 預設 用 `gvfg_enumerate_devices()` 得到的 device index 開啟 device,並明確選擇
開啟 CH0;需要明確選擇 CaptureDemo 的 CH0/CH1 時使用 `gvfg_open_channel()`。 `GVFG_CHANNEL_0` 或 `GVFG_CHANNEL_1`。
選定的 channel 會一致套用到 signal status、signal 插拔事件、DMA done index 與 選定的 channel 會一致套用到 signal status、signal 插拔事件、DMA done index 與
frame buffer。 frame buffer。
+3 -7
View File
@@ -110,7 +110,7 @@ Core capture API 的基本模式是 FFmpeg-style pull model:
```text ```text
gvfg_enumerate_devices gvfg_enumerate_devices
-> gvfg_create -> gvfg_create
-> gvfg_open / gvfg_open_channel(CH0 or CH1) -> gvfg_open_channel(CH0 or CH1)
-> gvfg_start -> gvfg_start
-> loop: -> loop:
gvfg_read_frame gvfg_read_frame
@@ -142,9 +142,8 @@ event 或成功取得第一張完整 frame 後,public signal status 才能回
- `gvfg_handle` 永遠 opaque;stable release 後 `gvfg_frame_t` 凍結,只有 major - `gvfg_handle` 永遠 opaque;stable release 後 `gvfg_frame_t` 凍結,只有 major
version 可以破壞既有 ABI。 version 可以破壞既有 ABI。
- `gvfg_get_frame_layout()` 回傳 SDK-filled layout metadata:`plane_data`、 - `gvfg_get_frame_layout()` 回傳 SDK-filled layout metadata:`plane_data`、
`plane_stride`、`plane_size`、`plane_offset`。目前 PCIES2MM backend 先用 `plane_stride`、`plane_size`。目前 PCIES2MM backend 使用
width/height/format 推導 tightly packed layout;未來 driver 如果能回報真實 width/height/format 推導 tightly packed layout。
pitch 或 plane offsets,應該更新 layout query path,而不是改既有 frame struct。
- 未來 color/timestamp metadata 應新增各自帶 `struct_size` 的 query output,例如 - 未來 color/timestamp metadata 應新增各自帶 `struct_size` 的 query output,例如
`gvfg_get_frame_color_info()` 與 `gvfg_get_frame_timestamp_info()`。不要預先在每個 `gvfg_get_frame_color_info()` 與 `gvfg_get_frame_timestamp_info()`。不要預先在每個
struct 放大型 reserved array;等 metadata 類型真的很多再考慮 side data。 struct 放大型 reserved array;等 metadata 類型真的很多再考慮 side data。
@@ -179,9 +178,6 @@ GVFG_EVENT_CAPTURE_PAUSED
GVFG_EVENT_CAPTURE_RESUMED GVFG_EVENT_CAPTURE_RESUMED
``` ```
舊名稱 `GVFG_EVENT_PLUG_IN/OUT` 僅為 aliases;事件代表 input signal cable,
不是 PCIe capture device hotplug。
Video IRQ handling 是 `sdk/gvfg/src/backend/pcies2mm` 內部細節。IRQ bit numbers、 Video IRQ handling 是 `sdk/gvfg/src/backend/pcies2mm` 內部細節。IRQ bit numbers、
IRQ masks 與 DMA counters 不應出現在 IRQ masks 與 DMA counters 不應出現在
`gvfg_capture.h`。 `gvfg_capture.h`。
@@ -161,13 +161,10 @@ struct gvfg_convert_frame_t
layout = {}; layout = {};
layout.struct_size = sizeof(layout); layout.struct_size = sizeof(layout);
layout.layout_flags = GVFG_FRAME_LAYOUT_CONTIGUOUS | GVFG_FRAME_LAYOUT_SDK_DERIVED;
layout.row_bytes = actualStride;
layout.plane_count = 1; layout.plane_count = 1;
layout.plane_data[0] = buffer.data(); layout.plane_data[0] = buffer.data();
layout.plane_stride[0] = actualStride; layout.plane_stride[0] = actualStride;
layout.plane_size[0] = size; layout.plane_size[0] = size;
layout.plane_offset[0] = 0;
return GVFG_OK; return GVFG_OK;
} }
}; };
+6 -2
View File
@@ -297,7 +297,7 @@ bool MainWindow::openDevice()
st = gvfg_open_channel(handle_, deviceIndex, channelIndex); st = gvfg_open_channel(handle_, deviceIndex, channelIndex);
if (st != GVFG_OK) if (st != GVFG_OK)
{ {
showError(QStringLiteral("gvfg_open"), st); showError(QStringLiteral("gvfg_open_channel"), st);
closeDevice(); closeDevice();
return false; return false;
} }
@@ -795,9 +795,13 @@ void MainWindow::captureReadLoop()
if (gvfg_get_frame_layout(&frame, &layout) == GVFG_OK && if (gvfg_get_frame_layout(&frame, &layout) == GVFG_OK &&
layout.plane_count > 0 && layout.plane_count > 0 &&
layout.plane_data[0] && layout.plane_data[0] &&
layout.plane_stride[0] > 0) layout.plane_stride[0] > 0 &&
layout.plane_size[0] >=
static_cast<uint64_t>(layout.plane_stride[0]) *
static_cast<uint64_t>(frame.height))
{ {
previewFrame.data = layout.plane_data[0]; previewFrame.data = layout.plane_data[0];
previewFrame.data_size = layout.plane_size[0];
previewFrame.row_bytes = layout.plane_stride[0]; previewFrame.row_bytes = layout.plane_stride[0];
} }
+322 -356
View File
@@ -16,7 +16,7 @@
* gvfg_handle h = NULL; * gvfg_handle h = NULL;
* if (gvfg_create(&h) != GVFG_OK) * if (gvfg_create(&h) != GVFG_OK)
* return; * return;
* if (gvfg_open(h, devices[0].index) != GVFG_OK || * if (gvfg_open_channel(h, devices[0].index, GVFG_CHANNEL_0) != GVFG_OK ||
* gvfg_start(h) != GVFG_OK) { * gvfg_start(h) != GVFG_OK) {
* gvfg_destroy(h); * gvfg_destroy(h);
* return; * return;
@@ -39,18 +39,6 @@
* - The frame data pointer remains valid until gvfg_release_frame() is called. * - The frame data pointer remains valid until gvfg_release_frame() is called.
* Copy the data if it must outlive that call. * Copy the data if it must outlive that call.
* - At most one frame may be held by a handle at a time. * - At most one frame may be held by a handle at a time.
* - Query functions and gvfg_poll_event() may run concurrently with the single
* frame-reading thread. Lifecycle calls must be serialized by the application.
* gvfg_stop() may cancel a blocked read/poll, but no thread may access a held
* frame while stop/destroy invalidates it.
*
* ABI policy:
* - gvfg_handle remains opaque.
* - gvfg_frame_t contains only mandatory per-frame fields and is frozen after
* the first stable SDK release.
* - Extensible metadata is returned by dedicated query APIs whose output
* structures begin with struct_size.
* - Breaking existing ABI requires a new major SDK version.
*/ */
#ifdef _WIN32 #ifdef _WIN32
@@ -80,9 +68,6 @@
#ifndef _Out_writes_to_opt_ #ifndef _Out_writes_to_opt_
#define _Out_writes_to_opt_(size, count) #define _Out_writes_to_opt_(size, count)
#endif #endif
#ifndef _Out_writes_z_
#define _Out_writes_z_(size)
#endif
#endif #endif
#include <stdint.h> #include <stdint.h>
@@ -91,374 +76,355 @@
#define GVFG_TIMEOUT_INFINITE UINT32_MAX #define GVFG_TIMEOUT_INFINITE UINT32_MAX
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C"
{
#endif #endif
enum enum
{ {
GVFG_MAX_DEVICES = 16, GVFG_MAX_DEVICES = 16,
GVFG_MAX_PLANES = 1 GVFG_MAX_PLANES = 1
}; };
typedef enum typedef enum
{ {
GVFG_OK = 0, GVFG_OK = 0,
GVFG_EINVAL = -1, /* Invalid argument, such as NULL handle/output pointer. */ GVFG_EINVAL = -1, /* Invalid argument, such as NULL handle/output pointer. */
GVFG_ENODEV = -2, /* No GVFG capture device, or the device cannot be opened. */ GVFG_ENODEV = -2, /* No GVFG capture device, or the device cannot be opened. */
GVFG_ESTATE = -3, /* API was called in the wrong state. */ GVFG_ESTATE = -3, /* API was called in the wrong state. */
GVFG_EIO = -4, /* Driver/backend I/O failure. */ GVFG_EIO = -4, /* Driver/backend I/O failure. */
GVFG_ENOTSUP = -5, /* Requested feature or format is not supported. */ GVFG_ENOTSUP = -5, /* Requested feature or format is not supported. */
GVFG_ETIMEOUT = -6 /* Timed out waiting for driver/backend work. */ GVFG_ETIMEOUT = -6 /* Timed out waiting for driver/backend work. */
} gvfg_status_t; } gvfg_status_t;
typedef enum typedef enum
{ {
GVFG_PIXFMT_UNKNOWN = 0, GVFG_PIXFMT_UNKNOWN = 0,
GVFG_PIXFMT_YUY2 = 1, GVFG_PIXFMT_YUY2 = 1,
GVFG_PIXFMT_Y210 = 2 GVFG_PIXFMT_Y210 = 2
} gvfg_pixel_format_t; } gvfg_pixel_format_t;
typedef enum typedef enum
{ {
GVFG_CHANNEL_0 = 0, GVFG_CHANNEL_0 = 0,
GVFG_CHANNEL_1 = 1 GVFG_CHANNEL_1 = 1
} gvfg_channel_t; } gvfg_channel_t;
typedef enum typedef struct
{ {
GVFG_FRAME_LAYOUT_CONTIGUOUS = 1u << 0, /* Planes are contained inside one contiguous data buffer. */ int index; /* Device index to pass to gvfg_open_channel(). */
GVFG_FRAME_LAYOUT_SDK_DERIVED = 1u << 1 /* Layout was derived by the SDK from the native format. */ char name[128]; /* Display name for UI/logging. UTF-8, null-terminated. */
} gvfg_frame_layout_flags_t; } gvfg_device_info_t;
typedef struct typedef struct
{ {
int index; /* Device index to pass to gvfg_open(). */ uint32_t struct_size; /* Set to sizeof(gvfg_signal_status_t) before calling. */
char name[128]; /* Display name for UI/logging. UTF-8, null-terminated. */ int connected; /* Non-zero while the selected channel has a valid input signal. */
} gvfg_device_info_t; int channel; /* gvfg_channel_t selected when the device was opened. */
int width; /* Signal width in pixels when connected. */
int height; /* Signal height in pixels when connected. */
int pixel_format; /* gvfg_pixel_format_t value for the actual DMA payload. */
int bit_depth; /* Signal bit depth derived from the payload format. */
} gvfg_signal_status_t;
typedef struct typedef struct
{ {
uint32_t struct_size; /* Set to sizeof(gvfg_signal_status_t) before calling. */ int width; /* Width of the most recent frame returned by gvfg_read_frame(). */
int connected; /* Non-zero while the selected channel has a valid input signal. */ int height; /* Height of the most recent frame returned by gvfg_read_frame(). */
int channel; /* gvfg_channel_t selected when the device was opened. */ int bit_depth; /* Bits per color channel of the frame buffer. */
int width; /* Signal width in pixels when connected. */ int pixel_format; /* gvfg_pixel_format_t value: YUY2 or Y210. */
int height; /* Signal height in pixels when connected. */ int valid; /* Non-zero while capture is running after at least one frame read. */
int pixel_format; /* gvfg_pixel_format_t value for the actual DMA payload. */ } gvfg_last_frame_info_t;
int bit_depth; /* Signal bit depth derived from the payload format. */
} gvfg_signal_status_t;
typedef struct typedef struct
{ {
int width; /* Width of the most recent frame returned by gvfg_read_frame(). */ uint32_t struct_size; /* Set to sizeof(gvfg_runtime_info_t) before calling. */
int height; /* Height of the most recent frame returned by gvfg_read_frame(). */ gvfg_last_frame_info_t last_frame; /* Last frame returned by gvfg_read_frame(). */
int bit_depth; /* Bits per color channel of the frame buffer. */ double capture_fps; /* Runtime FPS measured from frames returned by gvfg_read_frame(). */
int pixel_format; /* gvfg_pixel_format_t value: YUY2 or Y210. */ uint64_t delivered_frames; /* Number of frames returned by gvfg_read_frame(). */
int valid; /* Non-zero while capture is running after at least one frame read. */ } gvfg_runtime_info_t;
} gvfg_last_frame_info_t;
typedef struct typedef struct
{ {
uint32_t struct_size; /* Set to sizeof(gvfg_runtime_info_t) before calling. */ const void *data; /* Native frame buffer. Valid until gvfg_release_frame() is called. */
gvfg_last_frame_info_t last_frame; /* Last frame returned by gvfg_read_frame(). */ uint64_t data_size; /* Total bytes available from data. */
double capture_fps; /* Runtime FPS measured from frames returned by gvfg_read_frame(). */ int width; /* Frame width in pixels. */
uint64_t delivered_frames; /* Number of frames returned by gvfg_read_frame(). */ int height; /* Frame height in pixels. */
} gvfg_runtime_info_t; int pixel_format; /* gvfg_pixel_format_t value. */
int bit_depth; /* Bits per color channel of the native frame. */
uint64_t frame_id; /* Monotonic identifier within the current gvfg_start()/stop() run. */
} gvfg_frame_t;
typedef struct typedef struct
{ {
const void *data; /* Native frame buffer. Valid until gvfg_release_frame() is called. */ uint32_t struct_size; /* Set to sizeof(gvfg_frame_layout_t) before calling. */
uint64_t data_size; /* Total bytes available from data. */ int plane_count; /* Number of valid entries in plane_data/plane_stride/plane_size. */
int width; /* Frame width in pixels. */ const void *plane_data[GVFG_MAX_PLANES]; /* Plane pointers inside frame.data, valid until gvfg_release_frame(). */
int height; /* Frame height in pixels. */ int plane_stride[GVFG_MAX_PLANES]; /* Bytes from one row to the next for each plane. */
int pixel_format; /* gvfg_pixel_format_t value. */ uint64_t plane_size[GVFG_MAX_PLANES]; /* Bytes available in each plane. */
int bit_depth; /* Bits per color channel of the native frame. */ } gvfg_frame_layout_t;
uint64_t frame_id; /* Monotonic identifier within the current gvfg_start()/stop() run. */
} gvfg_frame_t;
typedef struct typedef enum
{ {
uint32_t struct_size; /* Set to sizeof(gvfg_frame_layout_t) before calling. */ GVFG_EVENT_UNKNOWN = 0,
uint32_t layout_flags; /* Bitmask of gvfg_frame_layout_flags_t values. */ GVFG_EVENT_SIGNAL_CONNECTED = 1,
int row_bytes; /* Bytes per row for plane 0. Prefer plane_stride[] for new code. */ GVFG_EVENT_SIGNAL_DISCONNECTED = 2,
int plane_count; /* Number of valid entries in plane_data/plane_stride/plane_size. */ GVFG_EVENT_CAPTURE_PAUSED = 3,
const void *plane_data[GVFG_MAX_PLANES]; /* Plane pointers inside frame.data, valid until gvfg_release_frame(). */ GVFG_EVENT_CAPTURE_RESUMED = 4
int plane_stride[GVFG_MAX_PLANES]; /* Bytes from one row to the next for each plane. */ } gvfg_event_type_t;
uint64_t plane_size[GVFG_MAX_PLANES]; /* Bytes available in each plane. */
uint64_t plane_offset[GVFG_MAX_PLANES]; /* Byte offset from frame.data to each plane. */
} gvfg_frame_layout_t;
typedef enum typedef struct
{ {
GVFG_EVENT_UNKNOWN = 0, gvfg_event_type_t type;
GVFG_EVENT_SIGNAL_CONNECTED = 1, uint64_t timestamp_ns; /* Monotonic timestamp for ordering; not Unix/wall-clock time. */
GVFG_EVENT_SIGNAL_DISCONNECTED = 2, } gvfg_event_t;
GVFG_EVENT_CAPTURE_PAUSED = 3,
GVFG_EVENT_CAPTURE_RESUMED = 4,
/* Backward-compatible aliases. These events describe the input signal, /* Opaque session handle created by gvfg_create() and released by gvfg_destroy(). */
not physical insertion/removal of the PCIe capture device. */ typedef struct gvfg_handle_t *gvfg_handle;
GVFG_EVENT_PLUG_IN = GVFG_EVENT_SIGNAL_CONNECTED,
GVFG_EVENT_PLUG_OUT = GVFG_EVENT_SIGNAL_DISCONNECTED
} gvfg_event_type_t;
typedef struct /*
{ * Enumerate GVFG capture devices.
gvfg_event_type_t type; *
uint64_t timestamp_ns; /* Monotonic timestamp for ordering; not Unix/wall-clock time. */ * Parameters:
} gvfg_event_t; * - out_devices: Output array that receives device entries. Pass NULL to query
* the device count only.
* - max_devices: Number of entries available in out_devices. Values larger
* than GVFG_MAX_DEVICES are clamped. Pass 0 when out_devices is NULL.
*
* Returns:
* - When out_devices is non-NULL, returns the number of entries written.
* - When out_devices is NULL or max_devices is 0, returns the number of
* available devices.
* - Returns <= 0 when no device is available.
*/
GVFG_API int gvfg_enumerate_devices(
_Out_writes_to_opt_(max_devices, return) gvfg_device_info_t *out_devices,
_In_ int max_devices);
/* Opaque session handle created by gvfg_create() and released by gvfg_destroy(). */ /*
typedef struct gvfg_handle_t *gvfg_handle; * Create a GVFG capture session.
*
* Parameters:
* - out_handle: Receives the new session handle. Must not be NULL.
*
* Returns:
* - GVFG_OK on success.
* - GVFG_EINVAL if out_handle is NULL.
*
* The returned handle starts in the closed state. Release it with
* gvfg_destroy().
*/
GVFG_API gvfg_status_t gvfg_create(
_Outptr_ gvfg_handle *out_handle);
/* /*
* Enumerate GVFG capture devices. * Destroy a GVFG capture session.
* *
* Parameters: * Parameters:
* - out_devices: Output array that receives device entries. Pass NULL to query * - handle: Session handle returned by gvfg_create().
* the device count only. *
* - max_devices: Number of entries available in out_devices. Values larger * Returns:
* than GVFG_MAX_DEVICES are clamped. Pass 0 when out_devices is NULL. * - GVFG_OK.
* *
* Returns: * If capture is still running, it is stopped first. After this call, the handle
* - When out_devices is non-NULL, returns the number of entries written. * must not be used again.
* - When out_devices is NULL or max_devices is 0, returns the number of */
* available devices. GVFG_API gvfg_status_t gvfg_destroy(
* - Returns <= 0 when no device is available. _In_opt_ gvfg_handle handle);
*/
GVFG_API int gvfg_enumerate_devices(
_Out_writes_to_opt_(max_devices, return) gvfg_device_info_t *out_devices,
_In_ int max_devices);
/* /*
* Create a GVFG capture session. * Open a device and select its capture channel.
* *
* Parameters: * Parameters:
* - out_handle: Receives the new session handle. Must not be NULL. * - handle: Session handle returned by gvfg_create().
* * - device_index: Device index from gvfg_device_info_t::index.
* Returns: * - channel_index: GVFG_CHANNEL_0 or GVFG_CHANNEL_1.
* - GVFG_OK on success. *
* - GVFG_EINVAL if out_handle is NULL. * Returns:
* * - GVFG_OK on success.
* The returned handle starts in the closed state. Release it with * - GVFG_EINVAL if handle is NULL or channel_index is invalid.
* gvfg_destroy(). * - GVFG_ENODEV if the device cannot be opened.
*/ * - GVFG_EIO for driver/backend failures.
GVFG_API gvfg_status_t gvfg_create( */
_Outptr_ gvfg_handle *out_handle); GVFG_API gvfg_status_t gvfg_open_channel(
_In_ gvfg_handle handle,
_In_ int device_index,
_In_ int channel_index);
/* /*
* Destroy a GVFG capture session. * Configure and start capture on an opened device.
* *
* Parameters: * Parameters:
* - handle: Session handle returned by gvfg_create(). * - handle: Running session handle.
* *
* Returns: * Returns:
* - GVFG_OK. * - GVFG_OK on success, including when capture is already running or the SDK
* * has entered signal-monitoring mode while no input is connected.
* If capture is still running, it is stopped first. After this call, the handle * - GVFG_EINVAL if handle is NULL.
* must not be used again. * - GVFG_ESTATE if no device is open.
*/ * - GVFG_EIO or another status code if stream configuration/start fails.
GVFG_API gvfg_status_t gvfg_destroy( *
_In_opt_ gvfg_handle handle); * After success, call gvfg_read_frame() to receive frames and gvfg_poll_event()
* to receive capture events. With no input signal, frame reads time out; capture
* starts automatically after a signal-connected event.
*/
GVFG_API gvfg_status_t gvfg_start(
_In_ gvfg_handle handle);
/* /*
* Open a device by index from gvfg_enumerate_devices(). * Read one captured frame.
* *
* Parameters: * Parameters:
* - handle: Session handle returned by gvfg_create(). * - handle: Running capture session.
* - device_index: Device index from gvfg_device_info_t::index. * - out_frame: Receives a frame descriptor. Must not be NULL.
* * - timeout_ms: Maximum time to wait. Use 0 for a non-blocking read or
* Returns: * GVFG_TIMEOUT_INFINITE to wait indefinitely.
* - GVFG_OK on success. *
* - GVFG_EINVAL if handle is NULL. * Returns:
* - GVFG_ENODEV if the device cannot be opened. * - GVFG_OK on success.
* - GVFG_EIO for driver/backend failures. * - GVFG_EINVAL if handle or out_frame is NULL.
* * - GVFG_ESTATE if capture is not running or a previous frame has not been
* This is a backward-compatible shorthand for opening CH0. * released.
*/ * - GVFG_ETIMEOUT if no frame is ready before timeout_ms expires.
GVFG_API gvfg_status_t gvfg_open( * - GVFG_EIO for driver/backend failures.
_In_ gvfg_handle handle, *
_In_ int device_index); * The returned data pointer is owned by the SDK and remains valid until
* gvfg_release_frame() is called. A handle may hold only one frame at a time.
*/
GVFG_API gvfg_status_t gvfg_read_frame(
_In_ gvfg_handle handle,
_Out_ gvfg_frame_t *out_frame,
_In_ uint32_t timeout_ms);
/* Open CH0 or CH1 explicitly on a physical capture device. */ /*
GVFG_API gvfg_status_t gvfg_open_channel( * Query per-plane layout for a frame returned by gvfg_read_frame().
_In_ gvfg_handle handle, *
_In_ int device_index, * Parameters:
_In_ int channel_index); * - frame: Frame descriptor returned by gvfg_read_frame(). Must not be NULL.
* - out_layout: Receives plane pointers, strides, and sizes. Must not
* be NULL. Set out_layout->struct_size to sizeof(gvfg_frame_layout_t) before
* calling so future SDKs can safely extend this struct.
*
* Returns:
* - GVFG_OK on success.
* - GVFG_EINVAL if frame/out_layout is NULL or struct_size is too small.
* - GVFG_ENOTSUP if the SDK cannot describe the frame layout.
*
* The returned plane_data pointers are owned by the SDK and remain valid only
* until gvfg_release_frame() is called for the source frame.
*/
GVFG_API gvfg_status_t gvfg_get_frame_layout(
_In_ const gvfg_frame_t *frame,
_Inout_ gvfg_frame_layout_t *out_layout);
/* /*
* Configure and start capture on an opened device. * Release a frame returned by gvfg_read_frame().
* *
* Parameters: * Parameters:
* - handle: Running session handle. * - handle: Running capture session.
* * - frame: Frame previously returned by gvfg_read_frame(). The SDK currently
* Returns: * uses this as a lifetime token; pass the same descriptor back.
* - GVFG_OK on success, including when capture is already running or the SDK *
* has entered signal-monitoring mode while no input is connected. * Returns:
* - GVFG_EINVAL if handle is NULL. * - GVFG_OK on success.
* - GVFG_ESTATE if no device is open. * - GVFG_EINVAL if handle or frame is NULL.
* - GVFG_EIO or another status code if stream configuration/start fails. * - GVFG_ESTATE if no frame is currently held.
* */
* After success, call gvfg_read_frame() to receive frames and gvfg_poll_event() GVFG_API gvfg_status_t gvfg_release_frame(
* to receive capture events. With no input signal, frame reads time out; capture _In_ gvfg_handle handle,
* starts automatically after a signal-connected event. _In_ const gvfg_frame_t *frame);
*/
GVFG_API gvfg_status_t gvfg_start(
_In_ gvfg_handle handle);
/* /*
* Read one captured frame. * Poll one capture event.
* *
* Parameters: * Parameters:
* - handle: Running capture session. * - handle: Opened session handle.
* - out_frame: Receives a frame descriptor. Must not be NULL. * - out_event: Receives the event. Must not be NULL.
* - timeout_ms: Maximum time to wait. Use 0 for a non-blocking read or * - timeout_ms: Maximum time to wait. Use 0 to return immediately or
* GVFG_TIMEOUT_INFINITE to wait indefinitely. * GVFG_TIMEOUT_INFINITE to wait indefinitely.
* *
* Returns: * Returns:
* - GVFG_OK on success. * - GVFG_OK on success.
* - GVFG_EINVAL if handle or out_frame is NULL. * - GVFG_EINVAL if handle or out_event is NULL.
* - GVFG_ESTATE if capture is not running or a previous frame has not been * - GVFG_ESTATE if capture is not running or is stopped while waiting.
* released. * - GVFG_ETIMEOUT if no event is available before timeout_ms expires.
* - GVFG_ETIMEOUT if no frame is ready before timeout_ms expires. */
* - GVFG_EIO for driver/backend failures. GVFG_API gvfg_status_t gvfg_poll_event(
* _In_ gvfg_handle handle,
* The returned data pointer is owned by the SDK and remains valid until _Out_ gvfg_event_t *out_event,
* gvfg_release_frame() is called. A handle may hold only one frame at a time. _In_ uint32_t timeout_ms);
*/
GVFG_API gvfg_status_t gvfg_read_frame(
_In_ gvfg_handle handle,
_Out_ gvfg_frame_t *out_frame,
_In_ uint32_t timeout_ms);
/* /*
* Query per-plane layout for a frame returned by gvfg_read_frame(). * Stop capture.
* *
* Parameters: * Parameters:
* - frame: Frame descriptor returned by gvfg_read_frame(). Must not be NULL. * - handle: Session handle returned by gvfg_create().
* - out_layout: Receives plane pointers, strides, sizes, and offsets. Must not *
* be NULL. Set out_layout->struct_size to sizeof(gvfg_frame_layout_t) before * Returns:
* calling so future SDKs can safely extend this struct. * - GVFG_OK on success, including when capture is already stopped.
* * - GVFG_EINVAL if handle is NULL.
* Returns: *
* - GVFG_OK on success. * This stops backend capture and invalidates any unreleased frame.
* - GVFG_EINVAL if frame/out_layout is NULL or struct_size is too small. */
* - GVFG_ENOTSUP if the SDK cannot describe the frame layout. GVFG_API gvfg_status_t gvfg_stop(
* _In_ gvfg_handle handle);
* The returned plane_data pointers are owned by the SDK and remain valid only
* until gvfg_release_frame() is called for the source frame.
*/
GVFG_API gvfg_status_t gvfg_get_frame_layout(
_In_ const gvfg_frame_t *frame,
_Inout_ gvfg_frame_layout_t *out_layout);
/* /*
* Release a frame returned by gvfg_read_frame(). * Query current signal information and delivered buffer format.
* *
* Parameters: * Parameters:
* - handle: Running capture session. * - handle: Opened session handle.
* - frame: Frame previously returned by gvfg_read_frame(). The SDK currently * - out_status: Receives signal status. Must not be NULL.
* uses this as a lifetime token; pass the same descriptor back. *
* * Returns:
* Returns: * - GVFG_OK on success.
* - GVFG_OK on success. * - GVFG_EINVAL if handle or out_status is NULL.
* - GVFG_EINVAL if handle or frame is NULL. * - GVFG_EINVAL if out_status->struct_size is too small.
* - GVFG_ESTATE if no frame is currently held. * - GVFG_ESTATE if no capture device is open.
*/ *
GVFG_API gvfg_status_t gvfg_release_frame( * No input signal is a normal state: the function returns GVFG_OK with
_In_ gvfg_handle handle, * out_status->connected set to 0. Set out_status->struct_size before calling.
_In_ const gvfg_frame_t *frame); */
GVFG_API gvfg_status_t gvfg_get_signal_status(
_In_ gvfg_handle handle,
_Inout_ gvfg_signal_status_t *out_status);
/* /*
* Poll one capture event. * Query runtime frame-delivery diagnostics.
* *
* Parameters: * Parameters:
* - handle: Opened session handle. * - handle: Opened or running session handle.
* - out_event: Receives the event. Must not be NULL. * - out_info: Receives runtime information. Must not be NULL.
* - timeout_ms: Maximum time to wait. Use 0 to return immediately or *
* GVFG_TIMEOUT_INFINITE to wait indefinitely. * Returns:
* * - GVFG_OK on success.
* Returns: * - GVFG_EINVAL if handle or out_info is NULL.
* - GVFG_OK on success. * - GVFG_EINVAL if out_info->struct_size is too small.
* - GVFG_EINVAL if handle or out_event is NULL. *
* - GVFG_ESTATE if capture is not running or is stopped while waiting. * The result includes the latest delivered frame, SDK-measured capture FPS,
* - GVFG_ETIMEOUT if no event is available before timeout_ms expires. * and the number of frames delivered by the SDK. Query current input signal
*/ * metadata separately with gvfg_get_signal_status(). Set out_info->struct_size
GVFG_API gvfg_status_t gvfg_poll_event( * before calling.
_In_ gvfg_handle handle, */
_Out_ gvfg_event_t *out_event, GVFG_API gvfg_status_t gvfg_get_runtime_info(
_In_ uint32_t timeout_ms); _In_ gvfg_handle handle,
_Inout_ gvfg_runtime_info_t *out_info);
/* /* Convert a gvfg_pixel_format_t value to a static English format name. */
* Stop capture. GVFG_API const char *gvfg_pixel_format_name(
* _In_ int pixel_format);
* Parameters:
* - handle: Session handle returned by gvfg_create().
*
* Returns:
* - GVFG_OK on success, including when capture is already stopped.
* - GVFG_EINVAL if handle is NULL.
*
* This stops backend capture and invalidates any unreleased frame.
*/
GVFG_API gvfg_status_t gvfg_stop(
_In_ gvfg_handle handle);
/* /*
* Query current signal information and delivered buffer format. * Convert a GVFG status code to a static English error string.
* *
* Parameters: * Parameters:
* - handle: Opened session handle. * - status: Status code returned by a GVFG API.
* - out_status: Receives signal status. Must not be NULL. *
* * Returns:
* Returns: * - Static null-terminated English string. The caller must not free it.
* - GVFG_OK on success. */
* - GVFG_EINVAL if handle or out_status is NULL. GVFG_API const char *gvfg_strerror(
* - GVFG_EINVAL if out_status->struct_size is too small. _In_ gvfg_status_t status);
* - GVFG_ESTATE if no capture device is open.
*
* No input signal is a normal state: the function returns GVFG_OK with
* out_status->connected set to 0. Set out_status->struct_size before calling.
*/
GVFG_API gvfg_status_t gvfg_get_signal_status(
_In_ gvfg_handle handle,
_Inout_ gvfg_signal_status_t *out_status);
/*
* Query runtime frame-delivery diagnostics.
*
* Parameters:
* - handle: Opened or running session handle.
* - out_info: Receives runtime information. Must not be NULL.
*
* Returns:
* - GVFG_OK on success.
* - GVFG_EINVAL if handle or out_info is NULL.
* - GVFG_EINVAL if out_info->struct_size is too small.
*
* The result includes the latest delivered frame, SDK-measured capture FPS,
* and the number of frames delivered by the SDK. Query current input signal
* metadata separately with gvfg_get_signal_status(). Set out_info->struct_size
* before calling.
*/
GVFG_API gvfg_status_t gvfg_get_runtime_info(
_In_ gvfg_handle handle,
_Inout_ gvfg_runtime_info_t *out_info);
/* Convert a gvfg_pixel_format_t value to a static English format name. */
GVFG_API const char *gvfg_pixel_format_name(
_In_ int pixel_format);
/*
* Convert a GVFG status code to a static English error string.
*
* Parameters:
* - status: Status code returned by a GVFG API.
*
* Returns:
* - Static null-terminated English string. The caller must not free it.
*/
GVFG_API const char *gvfg_strerror(
_In_ gvfg_status_t status);
#ifdef __cplusplus #ifdef __cplusplus
} }
+40 -22
View File
@@ -34,6 +34,14 @@ namespace
static_assert(offsetof(gvfg_frame_t, pixel_format) == 24); static_assert(offsetof(gvfg_frame_t, pixel_format) == 24);
static_assert(offsetof(gvfg_frame_t, bit_depth) == 28); static_assert(offsetof(gvfg_frame_t, bit_depth) == 28);
static_assert(offsetof(gvfg_frame_t, frame_id) == 32); static_assert(offsetof(gvfg_frame_t, frame_id) == 32);
static_assert(std::is_standard_layout_v<gvfg_frame_layout_t>);
static_assert(sizeof(gvfg_frame_layout_t) == 32, "gvfg_frame_layout_t x64 ABI must remain stable");
static_assert(offsetof(gvfg_frame_layout_t, struct_size) == 0);
static_assert(offsetof(gvfg_frame_layout_t, plane_count) == 4);
static_assert(offsetof(gvfg_frame_layout_t, plane_data) == 8);
static_assert(offsetof(gvfg_frame_layout_t, plane_stride) == 16);
static_assert(offsetof(gvfg_frame_layout_t, plane_size) == 24);
#endif #endif
void copy_cstr(char *dst, size_t dstSize, const char *src) void copy_cstr(char *dst, size_t dstSize, const char *src)
@@ -168,13 +176,10 @@ namespace
gvfg_status_t populate_frame_layout(const gvfg_frame_t &frame, gvfg_frame_layout_t &layout) gvfg_status_t populate_frame_layout(const gvfg_frame_t &frame, gvfg_frame_layout_t &layout)
{ {
layout.layout_flags = 0;
layout.row_bytes = 0;
layout.plane_count = 0; layout.plane_count = 0;
std::memset(layout.plane_data, 0, sizeof(layout.plane_data)); std::memset(layout.plane_data, 0, sizeof(layout.plane_data));
std::memset(layout.plane_stride, 0, sizeof(layout.plane_stride)); std::memset(layout.plane_stride, 0, sizeof(layout.plane_stride));
std::memset(layout.plane_size, 0, sizeof(layout.plane_size)); std::memset(layout.plane_size, 0, sizeof(layout.plane_size));
std::memset(layout.plane_offset, 0, sizeof(layout.plane_offset));
if (!frame.data || frame.data_size == 0 || frame.width <= 0 || frame.height <= 0) if (!frame.data || frame.data_size == 0 || frame.width <= 0 || frame.height <= 0)
return GVFG_EINVAL; return GVFG_EINVAL;
@@ -196,13 +201,10 @@ namespace
size > frame.data_size) size > frame.data_size)
return GVFG_EINVAL; return GVFG_EINVAL;
layout.row_bytes = static_cast<int>(row);
layout.plane_count = 1; layout.plane_count = 1;
layout.plane_data[0] = frame.data; layout.plane_data[0] = frame.data;
layout.plane_stride[0] = static_cast<int>(row); layout.plane_stride[0] = static_cast<int>(row);
layout.plane_size[0] = size; layout.plane_size[0] = size;
layout.plane_offset[0] = 0;
layout.layout_flags = GVFG_FRAME_LAYOUT_CONTIGUOUS | GVFG_FRAME_LAYOUT_SDK_DERIVED;
return GVFG_OK; return GVFG_OK;
} }
@@ -317,6 +319,9 @@ struct gvfg_handle_t
gvfg_status_t getRuntimeInfo(gvfg_runtime_info_t &out) gvfg_status_t getRuntimeInfo(gvfg_runtime_info_t &out)
{ {
if (!backend)
return GVFG_ESTATE;
std::memset(&out, 0, sizeof(out)); std::memset(&out, 0, sizeof(out));
out.struct_size = sizeof(out); out.struct_size = sizeof(out);
const uint64_t frames = deliveredFrames.load(std::memory_order_relaxed); const uint64_t frames = deliveredFrames.load(std::memory_order_relaxed);
@@ -445,8 +450,6 @@ struct gvfg_handle_t
return map_status(st); return map_status(st);
} }
updateRuntimeFps(now_ns());
if (!frame.data || frame.width == 0 || frame.height == 0) if (!frame.data || frame.width == 0 || frame.height == 0)
{ {
backend->release_frame(frame); backend->release_frame(frame);
@@ -455,6 +458,29 @@ struct gvfg_handle_t
return GVFG_EIO; return GVFG_EIO;
} }
bool stoppedWhileWaiting = false;
{
std::lock_guard<std::mutex> lock(frameMutex);
if (!running.load(std::memory_order_acquire))
{
readInProgress = false;
stoppedWhileWaiting = true;
}
else
{
heldBackendFrame = frame;
readInProgress = false;
frameHeld = true;
}
}
if (stoppedWhileWaiting)
{
backend->release_frame(frame);
return GVFG_ESTATE;
}
updateRuntimeFps(now_ns());
out.data = frame.data; out.data = frame.data;
out.data_size = static_cast<uint64_t>(frame.data_size_bytes); out.data_size = static_cast<uint64_t>(frame.data_size_bytes);
out.width = static_cast<int>(frame.width); out.width = static_cast<int>(frame.width);
@@ -464,10 +490,6 @@ struct gvfg_handle_t
out.frame_id = frame.frame_id; out.frame_id = frame.frame_id;
noteDeliveredFrame(out.width, out.height, out.bit_depth, out.pixel_format); noteDeliveredFrame(out.width, out.height, out.bit_depth, out.pixel_format);
std::lock_guard<std::mutex> lock(frameMutex);
heldBackendFrame = frame;
readInProgress = false;
frameHeld = true;
return GVFG_OK; return GVFG_OK;
} }
@@ -690,7 +712,8 @@ extern "C"
{ {
int gvfg_enumerate_devices(gvfg_device_info_t *out_devices, int max_devices) int gvfg_enumerate_devices(gvfg_device_info_t *out_devices, int max_devices)
{ {
const std::vector<gvfg::internal::PcieS2mmDevice> devices = gvfg::internal::enumerate_pcies2mm_devices(); const std::vector<gvfg::internal::PcieS2mmDevice> devices =
gvfg::internal::enumerate_pcies2mm_devices();
const int n = static_cast<int>(devices.size()); const int n = static_cast<int>(devices.size());
if (n <= 0) if (n <= 0)
return n; return n;
@@ -698,7 +721,8 @@ extern "C"
if (!out_devices || max_devices <= 0) if (!out_devices || max_devices <= 0)
return n; return n;
const int written = (std::min)(n, max_devices); const int written = (std::min)((std::min)(n, max_devices),
static_cast<int>(GVFG_MAX_DEVICES));
for (int i = 0; i < written; ++i) for (int i = 0; i < written; ++i)
{ {
out_devices[i] = {}; out_devices[i] = {};
@@ -718,6 +742,7 @@ extern "C"
{ {
if (!out_handle) if (!out_handle)
return GVFG_EINVAL; return GVFG_EINVAL;
auto h = std::make_unique<gvfg_handle_t>(); auto h = std::make_unique<gvfg_handle_t>();
*out_handle = h.release(); *out_handle = h.release();
return GVFG_OK; return GVFG_OK;
@@ -729,13 +754,6 @@ extern "C"
return GVFG_OK; return GVFG_OK;
} }
gvfg_status_t gvfg_open(gvfg_handle handle, int device_index)
{
if (!handle)
return GVFG_EINVAL;
return handle->open(device_index, GVFG_CHANNEL_0);
}
gvfg_status_t gvfg_open_channel(gvfg_handle handle, int device_index, int channel_index) gvfg_status_t gvfg_open_channel(gvfg_handle handle, int device_index, int channel_index)
{ {
if (!handle) if (!handle)
@@ -763,7 +781,7 @@ extern "C"
if (!frame || !out_layout) if (!frame || !out_layout)
return GVFG_EINVAL; return GVFG_EINVAL;
constexpr uint32_t kFrameLayoutV1Size = constexpr uint32_t kFrameLayoutV1Size =
static_cast<uint32_t>(offsetof(gvfg_frame_layout_t, plane_offset) + sizeof(out_layout->plane_offset)); static_cast<uint32_t>(offsetof(gvfg_frame_layout_t, plane_size) + sizeof(out_layout->plane_size));
const uint32_t callerSize = out_layout->struct_size; const uint32_t callerSize = out_layout->struct_size;
if (callerSize < kFrameLayoutV1Size) if (callerSize < kFrameLayoutV1Size)
return GVFG_EINVAL; return GVFG_EINVAL;