Add frame layout API and split conversion helper

- Add gvfg_get_frame_layout() and gvfg_frame_layout_t for per-plane
  frame metadata without extending gvfg_frame_t
- Update gvfg_preview to consume gvfg_get_frame_layout() before
  falling back to width-derived stride
- Add optional gvfg_convert helper DLL for explicit snapshot/export
  conversion outside the core capture API
- Support YUY2/Y210 conversion to BGRA8, RGB48, and RGBA64 helper
  frames
- Add SAL annotations to public capture, preview, convert, and debug
  headers
- Update Traditional Chinese API/package documentation for core,
  preview, and convert helper split
This commit is contained in:
Mark.lin
2026-06-16 16:59:39 +08:00
parent d2657a69cb
commit fd0272b623
13 changed files with 1203 additions and 159 deletions
+1
View File
@@ -24,6 +24,7 @@ endif()
add_subdirectory(sdk/gvfg)
add_subdirectory(helpers/gvfg_preview)
add_subdirectory(helpers/gvfg_convert)
if (BUILD_GVFG_SAMPLES)
add_subdirectory(samples/gvfg_qt_preview)
+30 -22
View File
@@ -1,54 +1,55 @@
# GVFG Standalone SDK
This is the standalone source tree for the GVFG capture SDK.
這是 GVFG capture SDK 的 standalone source tree。
GVFG is the source of truth here. Other projects should consume GVFG through
the exported public header, import library, and runtime DLL instead of compiling
the GVFG source directly.
這個 repo 以 GVFG 為 source of truth。其他專案應該透過 public header、
import library、runtime DLL 來使用 GVFG,不要直接把 GVFG source 編進去。
## Contents
## 內容
- `sdk/gvfg`: GVFG customer C API, internal debug API, and XDMA backend.
- `helpers/gvfg_preview`: optional preview helper DLL used after gvfg_read_frame().
- `samples/gvfg_qt_preview`: internal debug Qt preview tool.
- `docs`: API and integration notes.
- `sdk/gvfg`:GVFG customer C API、internal debug API、XDMA backend。
- `helpers/gvfg_preview`:可選的 preview helper DLL,在 `gvfg_read_frame()` 後使用。
- `helpers/gvfg_convert`:可選的 snapshot/export conversion helper DLL。
- `samples/gvfg_qt_preview`:內部 debug Qt preview tool。
- `docs`:API 與整合說明。
Customer-facing API details are in `docs/GVFG_CUSTOMER_API.md`. Internal
architecture, package split, threading, and frame ownership notes are in
`docs/GVFG_INTERNAL_NOTES.md`.
客戶端 API 細節在 `docs/GVFG_CUSTOMER_API.md`。內部架構、package
切分、threading、frame ownership 說明在 `docs/GVFG_INTERNAL_NOTES.md`。
## Build
Open this folder's `CMakeLists.txt` in Qt Creator with a Windows MSVC Qt kit, or
configure from a Visual Studio developer shell.
可以用 Windows MSVC Qt kit 在 Qt Creator 打開此資料夾的 `CMakeLists.txt`,
或在 Visual Studio developer shell 裡 configure。
Example:
範例:
```bat
cmake -S . -B build -DBUILD_GVFG_SAMPLES=ON -DCMAKE_PREFIX_PATH=C:\Qt\6.10.2\msvc2022_64
cmake --build build --target gvfg_qt_preview --config Release
```
Useful CMake options:
常用 CMake options:
```text
BUILD_GVFG_SAMPLES=ON
GVFG_XDMA_DEBUG_LOG=OFF
```
Build outputs:
Build 產物:
```text
build/.../bin/gvfg.dll
build/.../bin/gvfg_preview.dll
build/.../bin/gvfg_convert.dll
build/.../lib/gvfg.lib
build/.../lib/gvfg_preview.lib
build/.../lib/gvfg_convert.lib
build/.../bin/gvfg_qt_preview.exe
```
## Consumer Layout
Applications should consume the SDK with:
一般 application 使用 core SDK 只需要:
```text
include/gvfg_capture.h
@@ -56,7 +57,7 @@ lib/gvfg.lib
bin/gvfg.dll
```
Applications that want the optional display helper can also consume:
需要 optional display helper 時,再加:
```text
include/gvfg_preview.h
@@ -64,7 +65,14 @@ lib/gvfg_preview.lib
bin/gvfg_preview.dll
```
Customer/demo packages should not include `gvfg_debug.h`, SDK source, XDMA
backend headers, IRQ details, raw FPGA values, or preview helper source. Those
belong in the internal debug/full application packages.
需要 snapshot/export conversion 時,再加:
```text
include/gvfg_convert.h
lib/gvfg_convert.lib
bin/gvfg_convert.dll
```
Customer/demo package 不應包含 `gvfg_debug.h`、SDK source、XDMA backend
headers、IRQ details、raw FPGA values 或 helper source。這些只屬於 internal
debug/full application package。
+223 -56
View File
@@ -1,13 +1,13 @@
# GVFG Customer API
# GVFG 客戶端 API
This document describes the customer-facing GVFG capture API in
`sdk/gvfg/include/gvfg_capture.h`.
本文說明客戶端使用的 GVFG capture API,主要 public header 是
`sdk/gvfg/include/gvfg_capture.h`。
The customer SDK is capture-only. It does not expose SDK-managed preview,
driver registers, DMA internals, or FPGA debug controls. Preview rendering is
handled by internal applications or tools on top of frames returned by the SDK.
Customer SDK 的 core API 只負責 capture。它不 expose SDK-managed preview、
driver registers、DMA internals 或 FPGA debug controls。Preview rendering、
snapshot conversion 這類功能放在 helper DLL,使用者需要時再另外 link。
## Basic Flow
## 基本流程
```text
gvfg_enumerate_devices
@@ -23,12 +23,12 @@ gvfg_enumerate_devices
-> gvfg_destroy
```
## Threading
## 執行緒模型
The frame API is pull-based. The SDK does not start a customer frame thread or
preview thread from the public API layer.
Frame API 是 pull-based。Public API layer 不會替 customer 啟動 frame thread
或 preview thread。
Applications decide where to call `gvfg_read_frame()`:
Application 自己決定在哪裡呼叫 `gvfg_read_frame()`:
```text
UI app:
@@ -40,25 +40,25 @@ console/service app:
call gvfg_read_frame() directly from its processing loop
```
The backend may still use internal driver I/O workers while capture is running.
Those are implementation details behind `gvfg.dll`.
Backend 在 capture running 時可以有自己的 internal driver I/O workers,但那是
`gvfg.dll` 背後的 implementation detail。
## Frame Ownership
## Frame 所有權
`gvfg_read_frame()` returns one SDK-owned frame buffer.
`gvfg_read_frame()` 會回傳一個 SDK-owned frame buffer。
Rules:
規則:
- `frame.data` is valid until `gvfg_release_frame()` is called.
- A handle may hold only one frame at a time.
- If the application needs the data after release, it must copy the frame.
- `gvfg_stop()` invalidates any unreleased frame.
- `frame.data` 在呼叫 `gvfg_release_frame()` 前有效。
- 同一個 handle 一次最多只能 hold 一個 frame。
- Application 如果需要在 release 後繼續使用資料,必須自己 copy frame。
- `gvfg_stop()` 會讓尚未 release 的 frame 失效。
## Public Types
## 公開型別
### `gvfg_handle`
Opaque session handle.
Opaque session handle。
```c
typedef struct gvfg_handle_t *gvfg_handle;
@@ -66,7 +66,7 @@ typedef struct gvfg_handle_t *gvfg_handle;
### `gvfg_status_t`
Common return status.
共用 return status。
```c
typedef enum
@@ -83,11 +83,11 @@ typedef enum
### `gvfg_device_info_t`
Device entry returned by `gvfg_enumerate_devices()`.
`gvfg_enumerate_devices()` 回傳的 device entry。
### `gvfg_signal_status_t`
Customer-readable input signal status:
Customer-readable input signal status:
- width / height
- video format text
@@ -95,13 +95,12 @@ Customer-readable input signal status:
- bit depth
- SDI / HDMI lock status
Raw FPGA values, register-like values, and validity masks are internal debug
data and are available only through `gvfg_debug.h` in the internal debug
package.
Raw FPGA values、register-like values、validity masks 都是 internal debug data,
只應透過 internal debug package 裡的 `gvfg_debug.h` 提供。
### `gvfg_frame_t`
Frame returned by `gvfg_read_frame()`.
`gvfg_read_frame()` 回傳的 frame descriptor。
```c
typedef struct
@@ -116,21 +115,110 @@ typedef struct
} gvfg_frame_t;
```
### `gvfg_frame_layout_t`
由 `gvfg_get_frame_layout()` 回傳的 optional per-plane layout。
```c
typedef struct
{
uint32_t struct_size;
uint32_t layout_flags;
int row_bytes;
int plane_count;
const void *plane_data[GVFG_MAX_PLANES];
int plane_stride[GVFG_MAX_PLANES];
uint64_t plane_size[GVFG_MAX_PLANES];
uint64_t plane_offset[GVFG_MAX_PLANES];
uint64_t reserved[8];
} gvfg_frame_layout_t;
```
需要 row pitch 或 plane offset 的 application,應該在 `gvfg_read_frame()` 後呼叫
`gvfg_get_frame_layout()`,並使用 `plane_data[]`、`plane_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
推導出來的。Application 必須忽略 `reserved[]`。
目前 tightly packed layout:
- `YUY2` / `UYVY`:one plane,`plane_stride[0] = width * 2`。
- `Y210`:one plane,`plane_stride[0] = width * 4`。
- `RGB24`:one plane,`plane_stride[0] = width * 3`。
- `YUV444`:one plane,8-bit 時 `width * 3`,>8-bit 時 `width * 6`。
- `NV12`:two planes,Y 後接 interleaved UV,
`plane_stride[0] = plane_stride[1] = width`。
- `P010`:two planes,Y 後接 interleaved UV,
`plane_stride[0] = plane_stride[1] = width * 2`。
### `gvfg_convert_frame`
Optional `gvfg_convert.dll` 使用的 helper-owned destination frame。
```c
typedef struct gvfg_convert_frame_t *gvfg_convert_frame;
typedef struct
{
uint32_t struct_size;
int width;
int height;
int pixel_format;
int row_bytes;
uint32_t flags;
uint32_t reserved0;
uint64_t data_size;
uint64_t reserved[8];
} gvfg_convert_frame_desc_t;
```
這個設計接近 Blackmagic-style conversion:application 先建立 destination
frame,再明確把 source frame convert 進去。Core capture API 仍然回傳
hardware/native buffer,不會在 `gvfg_read_frame()` 裡偷偷轉格式。
Snapshot 用途可以把 `width`、`height`、`row_bytes` 設成 0,讓 helper 依照
captured source frame 自動決定大小。
第一版 destination formats:
- `GVFG_CONVERT_FMT_BGRA8`:8-bit BGRA,alpha 255。
- `GVFG_CONVERT_FMT_RGB48`:16-bit RGB container,適合 10-bit-friendly snapshot。
- `GVFG_CONVERT_FMT_RGBA64`:16-bit RGBA container,alpha 65535。
第一版 conversions:
- `YUY2 -> BGRA8 / RGB48 / RGBA64`
- `Y210 -> BGRA8 / RGB48 / RGBA64`
Conversion 使用 BT.709 limited-range YUV to RGB,和 preview path 對齊。其他
source 或 destination format 會回傳 `GVFG_ENOTSUP`。
### `gvfg_event_t`
Capture event returned by `gvfg_poll_event()`.
`gvfg_poll_event()` 回傳的 capture event。
Customer events are driver-neutral:
Customer event 保持 driver-neutral:
- `GVFG_EVENT_PLUG_IN`
- `GVFG_EVENT_PLUG_OUT`
- `GVFG_EVENT_CAPTURE_PAUSED`
- `GVFG_EVENT_CAPTURE_RESUMED`
Frame interrupts, IRQ bit numbers, and IRQ masks are internal details and are
not exposed through `gvfg_capture.h`.
Frame interrupts、IRQ bit numbers、IRQ masks 都是 internal details,不應出現在
`gvfg_capture.h`。
## Main APIs
## 主要 API
Public header 內的 function prototype 會使用 Windows SAL annotation,例如
`_In_`、`_Out_`、`_Inout_` 來標示參數方向。以下文件中的 prototype 為了閱讀性會省略
SAL;實際呼叫 API 時,application 不需要也不能額外傳入這些 annotation。
### `gvfg_enumerate_devices`
@@ -138,7 +226,7 @@ not exposed through `gvfg_capture.h`.
int gvfg_enumerate_devices(gvfg_device_info_t *out_devices, int max_devices);
```
Enumerates GVFG capture devices. Pass `NULL, 0` to query the device count.
列舉 GVFG capture devices。傳 `NULL, 0` 可以只查詢 device count。
### `gvfg_create` / `gvfg_destroy`
@@ -147,8 +235,7 @@ gvfg_status_t gvfg_create(gvfg_handle *out_handle);
gvfg_status_t gvfg_destroy(gvfg_handle handle);
```
Creates or destroys a session handle. Destroying a running handle stops capture
first.
建立或銷毀 session handle。銷毀 running handle 時會先 stop capture。
### `gvfg_open`
@@ -156,7 +243,7 @@ first.
gvfg_status_t gvfg_open(gvfg_handle handle, int device_index);
```
Opens a device by index from `gvfg_enumerate_devices()`.
用 `gvfg_enumerate_devices()` 得到的 device index 開啟 device。
### `gvfg_start` / `gvfg_stop`
@@ -165,8 +252,7 @@ gvfg_status_t gvfg_start(gvfg_handle handle);
gvfg_status_t gvfg_stop(gvfg_handle handle);
```
Starts or stops capture. After `gvfg_start()`, call `gvfg_read_frame()` to
receive frames.
開始或停止 capture。`gvfg_start()` 成功後,用 `gvfg_read_frame()` 取 frame。
### `gvfg_read_frame`
@@ -176,13 +262,95 @@ gvfg_status_t gvfg_read_frame(gvfg_handle handle,
uint32_t timeout_ms);
```
Reads one frame. Use `timeout_ms == 0` to wait indefinitely.
讀取一個 frame。`timeout_ms == 0` 表示 indefinite wait。
Returns:
回傳值:
- `GVFG_OK` when a frame is available.
- `GVFG_ETIMEOUT` when no frame arrives before the timeout.
- `GVFG_ESTATE` when capture is not running or a previous frame is still held.
- `GVFG_OK`:取得 frame。
- `GVFG_ETIMEOUT`:timeout 前沒有 frame。
- `GVFG_ESTATE`:capture 尚未 running,或上一個 frame 還沒 release。
### `gvfg_get_frame_layout`
```c
gvfg_status_t gvfg_get_frame_layout(const gvfg_frame_t *frame,
gvfg_frame_layout_t *out_layout);
```
回傳 `gvfg_read_frame()` 取得 frame 的 per-plane layout。
呼叫前先初始化 `out_layout->struct_size`:
```c
gvfg_frame_layout_t layout = {};
layout.struct_size = sizeof(layout);
gvfg_get_frame_layout(&frame, &layout);
```
回傳的 `plane_data[]` pointers 只在 `gvfg_release_frame()` 前有效。
### `gvfg_convert_create_frame` / `gvfg_convert_destroy_frame`
```c
gvfg_status_t gvfg_convert_create_frame(const gvfg_convert_frame_desc_t *desc,
gvfg_convert_frame *out_frame);
gvfg_status_t gvfg_convert_destroy_frame(gvfg_convert_frame frame);
```
建立或銷毀 helper-owned destination frame。Caller 主要設定
`desc.pixel_format`;`width`、`height`、`row_bytes` 可以是 0,讓
`gvfg_convert_frame_from_capture()` 依照 source frame 自動 configure。
### `gvfg_convert_frame_from_capture`
```c
gvfg_status_t gvfg_convert_frame_from_capture(const gvfg_frame_t *src,
gvfg_convert_frame dst_frame);
```
把 captured native frame 轉進 helper-owned destination frame。這不會改變
capture output format。
Snapshot buffer flow 範例:
```c
#include <gvfg_capture.h>
#include <gvfg_convert.h>
gvfg_frame_t frame = {};
if (gvfg_read_frame(h, &frame, 1000) == GVFG_OK) {
gvfg_convert_frame_desc_t desc = {};
desc.struct_size = sizeof(desc);
desc.pixel_format = GVFG_CONVERT_FMT_RGB48;
gvfg_convert_frame image = NULL;
if (gvfg_convert_create_frame(&desc, &image) == GVFG_OK &&
gvfg_convert_frame_from_capture(&frame, image) == GVFG_OK) {
const void *data = NULL;
uint64_t size = 0;
gvfg_convert_get_buffer(image, &data, &size);
/* data contains RGB48 rows; query layout for row_bytes. */
}
gvfg_convert_destroy_frame(image);
gvfg_release_frame(h, &frame);
}
```
### `gvfg_convert_get_frame_desc` / `gvfg_convert_get_buffer` / `gvfg_convert_get_layout`
```c
gvfg_status_t gvfg_convert_get_frame_desc(gvfg_convert_frame frame,
gvfg_convert_frame_desc_t *out_desc);
gvfg_status_t gvfg_convert_get_buffer(gvfg_convert_frame frame,
const void **out_data,
uint64_t *out_size);
gvfg_status_t gvfg_convert_get_layout(gvfg_convert_frame frame,
gvfg_frame_layout_t *out_layout);
```
在 `gvfg_convert_frame_from_capture()` 後,用這些 API 取得 converted image
buffer、metadata 和 row stride。
### `gvfg_release_frame`
@@ -191,7 +359,7 @@ gvfg_status_t gvfg_release_frame(gvfg_handle handle,
const gvfg_frame_t *frame);
```
Releases the frame returned by `gvfg_read_frame()`.
Release `gvfg_read_frame()` 回傳的 frame。
### `gvfg_poll_event`
@@ -201,7 +369,7 @@ gvfg_status_t gvfg_poll_event(gvfg_handle handle,
uint32_t timeout_ms);
```
Polls one event. Use `timeout_ms == 0` for a non-blocking poll.
Poll 一個 event。`timeout_ms == 0` 表示 non-blocking poll。
### `gvfg_get_signal_status`
@@ -210,7 +378,7 @@ gvfg_status_t gvfg_get_signal_status(gvfg_handle handle,
gvfg_signal_status_t *out_status);
```
Queries current input signal metadata.
查詢目前 input signal metadata。
### `gvfg_get_runtime_info`
@@ -219,14 +387,13 @@ gvfg_status_t gvfg_get_runtime_info(gvfg_handle handle,
gvfg_runtime_info_t *out_info);
```
Queries current signal status, last read frame format, FPS, and delivered frame
count.
查詢目前 signal status、last read frame format、FPS、delivered frame count。
## Preview Boundary
## Preview 邊界
Preview is not part of the core capture SDK API.
Preview 不是 core capture SDK API 的一部分。
Applications have two choices after `gvfg_read_frame()` returns a frame:
Application 在 `gvfg_read_frame()` 回傳 frame 後有幾種選擇:
```text
gvfg_read_frame
@@ -235,6 +402,6 @@ gvfg_read_frame
-> gvfg_release_frame
```
Customer demo source may include `gvfg_preview.h` and link `gvfg_preview.dll`
when it needs a simple display path. The preview helper source remains private;
customer code only sees the helper API and binary.
Customer demo source 需要簡單 display path 時,可以 include `gvfg_preview.h`
並 link `gvfg_preview.dll`。Preview helper source 保持 private;customer code
只看到 helper API 和 binary。
+70 -42
View File
@@ -1,11 +1,11 @@
# GVFG Internal Notes
This document is the internal engineering map for the standalone GVFG SDK.
Customer-facing API details live in `docs/GVFG_CUSTOMER_API.md`.
這份文件是 standalone GVFG SDK 的內部工程地圖。客戶端 API 細節請看
`docs/GVFG_CUSTOMER_API.md`。
## Current Direction
## 目前方向
GVFG is split into three clear pieces:
GVFG 目前切成幾個清楚的模組:
```text
sdk/gvfg/
@@ -16,15 +16,19 @@ helpers/gvfg_preview/
gvfg_preview.dll
optional display helper; renders gvfg_frame_t after gvfg_read_frame()
helpers/gvfg_convert/
gvfg_convert.dll
optional snapshot/export helper; converts native capture frames on request
samples/gvfg_qt_preview/
internal debug Qt tool
uses gvfg.dll + gvfg_preview.dll + gvfg_debug.h
```
The core SDK must stay driver-neutral from the customer's point of view.
XDMA, IRQ, DMA counters, raw FPGA values, and backend details are internal.
從 customer 角度看,core SDK 必須維持 driver-neutral。XDMA、IRQ、DMA counters、
raw FPGA values、backend details 都是 internal。
## Architecture
## 架構
```mermaid
flowchart TD
@@ -38,6 +42,11 @@ flowchart TD
PreviewPipe["private D3D preview pipeline"]
end
subgraph Convert["Optional Convert Helper: gvfg_convert.dll"]
ConvertApi["helpers/gvfg_convert/include/gvfg_convert.h"]
ConvertPipe["private CPU/GPU conversion path"]
end
subgraph SDK["Core SDK: gvfg.dll"]
CaptureApi["sdk/gvfg/include/gvfg_capture.h"]
DebugApi["sdk/gvfg/include/gvfg_debug.h\ninternal only"]
@@ -52,10 +61,13 @@ flowchart TD
CustomerApp --> CaptureApi
CustomerApp -. optional display .-> PreviewApi
CustomerApp -. optional snapshot .-> ConvertApi
QtApp --> CaptureApi
QtApp --> DebugApi
QtApp --> PreviewApi
PreviewApi --> PreviewPipe
ConvertApi --> ConvertPipe
ConvertApi --> CaptureApi
CaptureApi --> Facade
DebugApi --> Facade
Facade --> Xdma
@@ -63,9 +75,9 @@ flowchart TD
Workers --> Ring
```
## Capture Flow
## Capture 流程
The capture API follows an FFmpeg-style pull model:
Capture API 採 FFmpeg-style pull model:
```text
gvfg_enumerate_devices
@@ -81,19 +93,27 @@ gvfg_enumerate_devices
-> gvfg_destroy
```
`gvfg.dll` does not own the application's public read thread. UI apps should
create their own worker thread and call `gvfg_read_frame()` there.
`gvfg.dll` 不擁有 application 的 public read thread。UI app 應該自己建立 worker
thread,並在那個 thread 呼叫 `gvfg_read_frame()`。
## Frame Ownership
## Frame 所有權
- `gvfg_read_frame()` returns one SDK-owned frame buffer.
- `frame.data` remains valid until `gvfg_release_frame()` is called.
- A handle may hold only one frame at a time.
- If the application needs data after release, it must copy the frame.
- `gvfg_preview_render_frame()` is synchronous and should be called before
`gvfg_release_frame()`.
- `gvfg_read_frame()` 回傳一個 SDK-owned frame buffer。
- `frame.data` 在 `gvfg_release_frame()` 前有效。
- `gvfg_frame_t` 要保持 ABI-stable;不要為了 layout 直接 append fields。
- `gvfg_get_frame_layout()` 回傳 SDK-filled layout metadata:`plane_data`、
`plane_stride`、`plane_size`、`plane_offset`。目前 XDMA backend 先用
width/height/format 推導 tightly packed layout;未來 driver 如果能回報真實
pitch 或 plane offsets,應該更新 layout query path,而不是改既有 frame struct。
- `gvfg_preview.dll` 應優先吃 `gvfg_get_frame_layout()`;layout query 不可用時才
fallback 到 width-derived stride。
- `gvfg_convert.dll` 負責 explicit snapshot/export conversion。不要把 color
conversion、image export、GPU conversion policy 搬進 `gvfg.dll`。
- 同一個 handle 一次最多 hold 一個 frame。
- Application 如果 release 後還要用 data,必須自己 copy frame。
- `gvfg_preview_render_frame()` 是 synchronous,應該在 `gvfg_release_frame()` 前呼叫。
Typical two-way use:
Typical two-way use:
```text
gvfg_read_frame
@@ -102,9 +122,9 @@ gvfg_read_frame
-> gvfg_release_frame
```
## Event Boundary
## Event 邊界
Customer events stay driver-neutral:
Customer event 保持 driver-neutral:
```text
GVFG_EVENT_PLUG_IN
@@ -113,20 +133,21 @@ GVFG_EVENT_CAPTURE_PAUSED
GVFG_EVENT_CAPTURE_RESUMED
```
Video IRQ handling is internal to `sdk/gvfg/src/backend/xdma`. IRQ bit numbers,
IRQ masks, DMA counters, and raw FPGA register-like values should not appear in
`gvfg_capture.h`.
Video IRQ handling 是 `sdk/gvfg/src/backend/xdma` 內部細節。IRQ bit numbers、
IRQ masks、DMA counters、raw FPGA register-like values 不應出現在
`gvfg_capture.h`。
## API Surfaces
Customer/demo visible:
Customer/demo 可見:
```text
include/gvfg_capture.h
include/gvfg_preview.h when display helper is used
include/gvfg_convert.h when snapshot/export conversion is used
```
Internal debug only:
Internal debug only:
```text
include/gvfg_debug.h
@@ -139,29 +160,32 @@ PDB symbols
internal diagnostic tools
```
## Package Split
## Package 切分
### Demo / Customer Package
Include:
Include:
```text
include/gvfg_capture.h
include/gvfg_preview.h when the demo shows video
include/gvfg_convert.h when the demo exports snapshots
lib/gvfg.lib
lib/gvfg_preview.lib when the demo shows video
lib/gvfg_convert.lib when the demo exports snapshots
bin/gvfg.dll
bin/gvfg_preview.dll when the demo shows video
bin/gvfg_convert.dll when the demo exports snapshots
samples/customer-facing source
docs/GVFG_CUSTOMER_API.md
```
Do not include:
Do not include:
```text
include/gvfg_debug.h
SDK source
preview helper source
helper source
XDMA backend headers
PDB symbols
register / DMA / IRQ debug docs
@@ -170,16 +194,19 @@ internal diagnostic tools
### Internal Debug Package
Include:
Include:
```text
include/gvfg_capture.h
include/gvfg_debug.h
include/gvfg_preview.h
include/gvfg_convert.h
lib/gvfg.lib
lib/gvfg_preview.lib
lib/gvfg_convert.lib
bin/gvfg.dll
bin/gvfg_preview.dll
bin/gvfg_convert.dll
bin/gvfg_qt_preview.exe
PDB symbols
internal debug notes
@@ -187,41 +214,42 @@ internal debug notes
### Full Release Package
The full application release can use `gvfg.dll` and `gvfg_preview.dll`, plus
licensing and closed-source application integration. Do not use the full release
package as the daily driver/FPGA bring-up vehicle.
Full application release 可以使用 `gvfg.dll`、`gvfg_preview.dll` 和
`gvfg_convert.dll`,再加上 licensing 與 closed-source application integration。
不要把 full release package 當成 daily driver/FPGA bring-up vehicle。
## Build
Top-level CMake builds the core SDK, preview helper, and optional sample:
Top-level CMake 會 build core SDK、helpers 和 optional sample:
```text
BUILD_GVFG_SAMPLES=ON
GVFG_XDMA_DEBUG_LOG=OFF
```
Outputs:
輸出產物:
```text
bin/gvfg.dll
bin/gvfg_preview.dll
bin/gvfg_convert.dll
lib/gvfg.lib
lib/gvfg_preview.lib
lib/gvfg_convert.lib
bin/gvfg_qt_preview.exe
```
`GVFG_XDMA_DEBUG_LOG=ON` enables verbose XDMA flow logging in the internal
backend.
`GVFG_XDMA_DEBUG_LOG=ON` 會打開 internal backend 的 verbose XDMA flow logging。
## Draw.io Files
Keep draw.io files for visual discussions:
保留 draw.io files 方便討論圖:
```text
docs/GVFG_ARCHITECTURE_OVERVIEW.drawio
docs/GVFG_DMA_RING_DATA_FLOW.drawio
```
When code changes only affect API order, package boundaries, or module
ownership, update this Markdown file. Update draw.io only when the deeper data
flow or frame ownership diagram changes.
如果 code change 只影響 API order、package boundary 或 module ownership,更新這份
Markdown 即可。只有 deeper data flow 或 frame ownership diagram 改變時,才需要更新
draw.io。
+32
View File
@@ -0,0 +1,32 @@
project(gvfg_convert LANGUAGES CXX)
add_library(gvfg_convert SHARED
include/gvfg_convert.h
src/gvfg_convert.cpp
)
set_target_properties(gvfg_convert PROPERTIES
AUTOMOC OFF
AUTOUIC OFF
AUTORCC OFF
)
target_include_directories(gvfg_convert
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/sdk/gvfg/include
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
)
target_compile_definitions(gvfg_convert PRIVATE
GVFG_CONVERT_BUILD
)
target_link_libraries(gvfg_convert PRIVATE
gvfg
)
if (MSVC)
target_compile_options(gvfg_convert PRIVATE /utf-8 /EHsc)
endif()
@@ -0,0 +1,78 @@
#pragma once
/*
* Optional GVFG frame conversion helper.
*
* Link this helper only when the application needs snapshot/export-friendly
* buffers. Core capture frames from gvfg.dll remain native hardware buffers.
*/
#include <gvfg_capture.h>
#include <stdint.h>
#ifdef _WIN32
#ifdef GVFG_CONVERT_BUILD
#define GVFG_CONVERT_API __declspec(dllexport)
#else
#define GVFG_CONVERT_API __declspec(dllimport)
#endif
#else
#define GVFG_CONVERT_API
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef enum
{
GVFG_CONVERT_FMT_BGRA8 = 1, /* 8-bit BGRA, alpha 255. */
GVFG_CONVERT_FMT_RGB48 = 2, /* 16-bit RGB container, useful for 10-bit snapshots. */
GVFG_CONVERT_FMT_RGBA64 = 3 /* 16-bit RGBA container, alpha 65535. */
} gvfg_convert_format_t;
typedef struct gvfg_convert_frame_t *gvfg_convert_frame;
typedef struct
{
uint32_t struct_size; /* Set to sizeof(gvfg_convert_frame_desc_t) before calling. */
int width; /* Frame width in pixels. Use 0 to size from source during conversion. */
int height; /* Frame height in pixels. Use 0 to size from source during conversion. */
int pixel_format; /* gvfg_convert_format_t destination format. */
int row_bytes; /* Destination row stride in bytes. Use 0 for helper default. */
uint32_t flags; /* Reserved for future conversion options. Must be 0. */
uint32_t reserved0; /* Reserved. Must be 0. */
uint64_t data_size; /* Output buffer size in bytes, filled after create/convert. */
uint64_t reserved[8]; /* Reserved for future helper metadata. Must be ignored. */
} gvfg_convert_frame_desc_t;
GVFG_CONVERT_API gvfg_status_t gvfg_convert_create_frame(
_In_ const gvfg_convert_frame_desc_t *desc,
_Outptr_ gvfg_convert_frame *out_frame);
GVFG_CONVERT_API gvfg_status_t gvfg_convert_destroy_frame(
_In_opt_ gvfg_convert_frame frame);
GVFG_CONVERT_API gvfg_status_t gvfg_convert_frame_from_capture(
_In_ const gvfg_frame_t *src,
_In_ gvfg_convert_frame dst_frame);
GVFG_CONVERT_API gvfg_status_t gvfg_convert_get_frame_desc(
_In_ gvfg_convert_frame frame,
_Inout_ gvfg_convert_frame_desc_t *out_desc);
GVFG_CONVERT_API gvfg_status_t gvfg_convert_get_buffer(
_In_ gvfg_convert_frame frame,
_Out_ const void **out_data,
_Out_ uint64_t *out_size);
GVFG_CONVERT_API gvfg_status_t gvfg_convert_get_layout(
_In_ gvfg_convert_frame frame,
_Inout_ gvfg_frame_layout_t *out_layout);
GVFG_CONVERT_API const char *gvfg_convert_strerror(
_In_ gvfg_status_t status);
#ifdef __cplusplus
}
#endif
+458
View File
@@ -0,0 +1,458 @@
#include "gvfg_convert.h"
#include <algorithm>
#include <climits>
#include <cstdint>
#include <cstring>
#include <limits>
#include <memory>
#include <vector>
namespace
{
bool checked_mul_u64(uint64_t a, uint64_t b, uint64_t &out)
{
if (a != 0 && b > UINT64_MAX / a)
return false;
out = a * b;
return true;
}
int convert_format_bytes_per_pixel(int fmt)
{
switch (fmt)
{
case GVFG_CONVERT_FMT_BGRA8:
return 4;
case GVFG_CONVERT_FMT_RGB48:
return 6;
case GVFG_CONVERT_FMT_RGBA64:
return 8;
default:
return 0;
}
}
uint8_t clamp_u8(double v)
{
if (v <= 0.0)
return 0;
if (v >= 255.0)
return 255;
return static_cast<uint8_t>(v + 0.5);
}
uint16_t clamp_u16(double v)
{
if (v <= 0.0)
return 0;
if (v >= 65535.0)
return 65535;
return static_cast<uint16_t>(v + 0.5);
}
void yuv709_limited_to_rgb01(double y01,
double u01,
double v01,
double &r,
double &g,
double &b)
{
const double y = y01 * 255.0;
const double u = (u01 - 0.5) * 255.0;
const double v = (v01 - 0.5) * 255.0;
const double c = y - 16.0;
r = (1.164383 * c + 1.792741 * v) / 255.0;
g = (1.164383 * c - 0.213249 * u - 0.532909 * v) / 255.0;
b = (1.164383 * c + 2.112402 * u) / 255.0;
}
void store_converted_pixel(uint8_t *dst,
int dstFormat,
double r,
double g,
double b)
{
switch (dstFormat)
{
case GVFG_CONVERT_FMT_BGRA8:
dst[0] = clamp_u8(b * 255.0);
dst[1] = clamp_u8(g * 255.0);
dst[2] = clamp_u8(r * 255.0);
dst[3] = 255;
break;
case GVFG_CONVERT_FMT_RGB48:
{
auto *out = reinterpret_cast<uint16_t *>(dst);
out[0] = clamp_u16(r * 65535.0);
out[1] = clamp_u16(g * 65535.0);
out[2] = clamp_u16(b * 65535.0);
break;
}
case GVFG_CONVERT_FMT_RGBA64:
{
auto *out = reinterpret_cast<uint16_t *>(dst);
out[0] = clamp_u16(r * 65535.0);
out[1] = clamp_u16(g * 65535.0);
out[2] = clamp_u16(b * 65535.0);
out[3] = 65535;
break;
}
default:
break;
}
}
uint16_t y210_word_to_10bit(uint16_t value)
{
return static_cast<uint16_t>((value >> 6) & 0x03ffu);
}
}
struct gvfg_convert_frame_t
{
gvfg_convert_frame_desc_t request{};
gvfg_convert_frame_desc_t desc{};
gvfg_frame_layout_t layout{};
std::vector<uint8_t> buffer;
gvfg_status_t configure(int width, int height, int pixelFormat, int rowBytes)
{
if (width <= 0 || height <= 0 || convert_format_bytes_per_pixel(pixelFormat) <= 0)
return GVFG_EINVAL;
const int bytesPerPixel = convert_format_bytes_per_pixel(pixelFormat);
uint64_t minStride64 = 0;
if (!checked_mul_u64(static_cast<uint64_t>(width),
static_cast<uint64_t>(bytesPerPixel),
minStride64) ||
minStride64 > static_cast<uint64_t>(INT_MAX))
return GVFG_EINVAL;
const int minStride = static_cast<int>(minStride64);
const int actualStride = rowBytes > 0 ? rowBytes : minStride;
if (actualStride < minStride)
return GVFG_EINVAL;
uint64_t size = 0;
if (!checked_mul_u64(static_cast<uint64_t>(actualStride),
static_cast<uint64_t>(height),
size) ||
size > static_cast<uint64_t>((std::numeric_limits<size_t>::max)()))
return GVFG_EINVAL;
desc = request;
desc.width = width;
desc.height = height;
desc.pixel_format = pixelFormat;
desc.row_bytes = actualStride;
desc.data_size = size;
try
{
buffer.resize(static_cast<size_t>(size));
}
catch (...)
{
buffer.clear();
desc.data_size = 0;
return GVFG_EIO;
}
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_data[0] = buffer.data();
layout.plane_stride[0] = actualStride;
layout.plane_size[0] = size;
layout.plane_offset[0] = 0;
return GVFG_OK;
}
};
namespace
{
gvfg_status_t create_or_reconfigure_frame(const gvfg_frame_t &src,
gvfg_convert_frame_t &dst)
{
const int requestedWidth = dst.request.width;
const int requestedHeight = dst.request.height;
const int targetWidth = requestedWidth > 0 ? requestedWidth : src.width;
const int targetHeight = requestedHeight > 0 ? requestedHeight : src.height;
if (targetWidth != src.width || targetHeight != src.height)
return GVFG_ENOTSUP;
if (!dst.buffer.empty() &&
dst.desc.width == targetWidth &&
dst.desc.height == targetHeight &&
dst.desc.pixel_format == dst.request.pixel_format &&
dst.desc.row_bytes > 0)
return GVFG_OK;
return dst.configure(targetWidth, targetHeight, dst.request.pixel_format, dst.request.row_bytes);
}
gvfg_status_t convert_yuy2_to_frame(const gvfg_frame_t &src,
const gvfg_frame_layout_t &srcLayout,
gvfg_convert_frame_t &dst)
{
if (srcLayout.plane_count < 1 || !srcLayout.plane_data[0] || srcLayout.plane_stride[0] < src.width * 2)
return GVFG_EINVAL;
const auto *srcBase = static_cast<const uint8_t *>(srcLayout.plane_data[0]);
const int srcStride = srcLayout.plane_stride[0];
const int bytesPerPixel = convert_format_bytes_per_pixel(dst.desc.pixel_format);
if (bytesPerPixel <= 0)
return GVFG_ENOTSUP;
for (int y = 0; y < src.height; ++y)
{
const uint8_t *srcRow = srcBase + static_cast<size_t>(y) * static_cast<size_t>(srcStride);
uint8_t *dstRow = dst.buffer.data() + static_cast<size_t>(y) * static_cast<size_t>(dst.desc.row_bytes);
for (int x = 0; x < src.width; x += 2)
{
const int sx = x * 2;
const uint8_t y0 = srcRow[sx + 0];
const uint8_t u = srcRow[sx + 1];
const uint8_t y1 = (x + 1 < src.width) ? srcRow[sx + 2] : y0;
const uint8_t v = (x + 1 < src.width) ? srcRow[sx + 3] : srcRow[sx + 1];
double r = 0.0, g = 0.0, b = 0.0;
yuv709_limited_to_rgb01(static_cast<double>(y0) / 255.0,
static_cast<double>(u) / 255.0,
static_cast<double>(v) / 255.0,
r,
g,
b);
store_converted_pixel(dstRow + static_cast<size_t>(x) * static_cast<size_t>(bytesPerPixel),
dst.desc.pixel_format,
r,
g,
b);
if (x + 1 < src.width)
{
yuv709_limited_to_rgb01(static_cast<double>(y1) / 255.0,
static_cast<double>(u) / 255.0,
static_cast<double>(v) / 255.0,
r,
g,
b);
store_converted_pixel(dstRow + static_cast<size_t>(x + 1) * static_cast<size_t>(bytesPerPixel),
dst.desc.pixel_format,
r,
g,
b);
}
}
}
return GVFG_OK;
}
gvfg_status_t convert_y210_to_frame(const gvfg_frame_t &src,
const gvfg_frame_layout_t &srcLayout,
gvfg_convert_frame_t &dst)
{
if (srcLayout.plane_count < 1 || !srcLayout.plane_data[0] || srcLayout.plane_stride[0] < src.width * 4)
return GVFG_EINVAL;
const auto *srcBase = static_cast<const uint8_t *>(srcLayout.plane_data[0]);
const int srcStride = srcLayout.plane_stride[0];
const int bytesPerPixel = convert_format_bytes_per_pixel(dst.desc.pixel_format);
if (bytesPerPixel <= 0)
return GVFG_ENOTSUP;
for (int y = 0; y < src.height; ++y)
{
const auto *srcRow = reinterpret_cast<const uint16_t *>(srcBase + static_cast<size_t>(y) * static_cast<size_t>(srcStride));
uint8_t *dstRow = dst.buffer.data() + static_cast<size_t>(y) * static_cast<size_t>(dst.desc.row_bytes);
for (int x = 0; x < src.width; x += 2)
{
const int sx = x * 2;
const uint16_t y0 = y210_word_to_10bit(srcRow[sx + 0]);
const uint16_t u = y210_word_to_10bit(srcRow[sx + 1]);
const uint16_t y1 = (x + 1 < src.width) ? y210_word_to_10bit(srcRow[sx + 2]) : y0;
const uint16_t v = (x + 1 < src.width) ? y210_word_to_10bit(srcRow[sx + 3]) : u;
double r = 0.0, g = 0.0, b = 0.0;
yuv709_limited_to_rgb01(static_cast<double>(y0) / 1023.0,
static_cast<double>(u) / 1023.0,
static_cast<double>(v) / 1023.0,
r,
g,
b);
store_converted_pixel(dstRow + static_cast<size_t>(x) * static_cast<size_t>(bytesPerPixel),
dst.desc.pixel_format,
r,
g,
b);
if (x + 1 < src.width)
{
yuv709_limited_to_rgb01(static_cast<double>(y1) / 1023.0,
static_cast<double>(u) / 1023.0,
static_cast<double>(v) / 1023.0,
r,
g,
b);
store_converted_pixel(dstRow + static_cast<size_t>(x + 1) * static_cast<size_t>(bytesPerPixel),
dst.desc.pixel_format,
r,
g,
b);
}
}
}
return GVFG_OK;
}
}
extern "C"
{
gvfg_status_t gvfg_convert_create_frame(const gvfg_convert_frame_desc_t *desc,
gvfg_convert_frame *out_frame)
{
if (!desc || !out_frame)
return GVFG_EINVAL;
*out_frame = nullptr;
if (desc->struct_size < sizeof(gvfg_convert_frame_desc_t) ||
desc->flags != 0 ||
desc->reserved0 != 0 ||
convert_format_bytes_per_pixel(desc->pixel_format) <= 0)
return GVFG_EINVAL;
if (desc->width < 0 || desc->height < 0 || desc->row_bytes < 0)
return GVFG_EINVAL;
auto frame = std::make_unique<gvfg_convert_frame_t>();
frame->request = {};
frame->request.struct_size = sizeof(gvfg_convert_frame_desc_t);
frame->request.width = desc->width;
frame->request.height = desc->height;
frame->request.pixel_format = desc->pixel_format;
frame->request.row_bytes = desc->row_bytes;
frame->desc = frame->request;
if (desc->width > 0 || desc->height > 0 || desc->row_bytes > 0)
{
if (desc->width <= 0 || desc->height <= 0)
return GVFG_EINVAL;
const gvfg_status_t st = frame->configure(desc->width,
desc->height,
desc->pixel_format,
desc->row_bytes);
if (st != GVFG_OK)
return st;
}
*out_frame = frame.release();
return GVFG_OK;
}
gvfg_status_t gvfg_convert_destroy_frame(gvfg_convert_frame frame)
{
delete frame;
return GVFG_OK;
}
gvfg_status_t gvfg_convert_frame_from_capture(const gvfg_frame_t *src,
gvfg_convert_frame dst_frame)
{
if (!src || !dst_frame)
return GVFG_EINVAL;
const gvfg_status_t cfg = create_or_reconfigure_frame(*src, *dst_frame);
if (cfg != GVFG_OK)
return cfg;
gvfg_frame_layout_t srcLayout{};
srcLayout.struct_size = sizeof(srcLayout);
const gvfg_status_t layoutStatus = gvfg_get_frame_layout(src, &srcLayout);
if (layoutStatus != GVFG_OK)
return layoutStatus;
switch (src->pixel_format)
{
case GVFG_PIXFMT_YUY2:
return convert_yuy2_to_frame(*src, srcLayout, *dst_frame);
case GVFG_PIXFMT_Y210:
return convert_y210_to_frame(*src, srcLayout, *dst_frame);
default:
return GVFG_ENOTSUP;
}
}
gvfg_status_t gvfg_convert_get_frame_desc(gvfg_convert_frame frame,
gvfg_convert_frame_desc_t *out_desc)
{
if (!frame || !out_desc)
return GVFG_EINVAL;
if (out_desc->struct_size < sizeof(gvfg_convert_frame_desc_t))
return GVFG_EINVAL;
const uint32_t callerSize = out_desc->struct_size;
std::memset(out_desc, 0, sizeof(*out_desc));
*out_desc = frame->desc;
out_desc->struct_size = callerSize;
return GVFG_OK;
}
gvfg_status_t gvfg_convert_get_buffer(gvfg_convert_frame frame,
const void **out_data,
uint64_t *out_size)
{
if (!frame || !out_data || !out_size)
return GVFG_EINVAL;
*out_data = frame->buffer.empty() ? nullptr : frame->buffer.data();
*out_size = static_cast<uint64_t>(frame->buffer.size());
return frame->buffer.empty() ? GVFG_ESTATE : GVFG_OK;
}
gvfg_status_t gvfg_convert_get_layout(gvfg_convert_frame frame,
gvfg_frame_layout_t *out_layout)
{
if (!frame || !out_layout)
return GVFG_EINVAL;
if (out_layout->struct_size < sizeof(gvfg_frame_layout_t))
return GVFG_EINVAL;
if (frame->buffer.empty() || frame->layout.plane_count <= 0)
return GVFG_ESTATE;
const uint32_t callerSize = out_layout->struct_size;
std::memset(out_layout, 0, sizeof(*out_layout));
*out_layout = frame->layout;
out_layout->struct_size = callerSize;
return GVFG_OK;
}
const char *gvfg_convert_strerror(gvfg_status_t status)
{
switch (status)
{
case GVFG_OK:
return "ok";
case GVFG_EINVAL:
return "invalid argument";
case GVFG_ENODEV:
return "device not found";
case GVFG_ESTATE:
return "invalid state";
case GVFG_EIO:
return "i/o error";
case GVFG_ENOTSUP:
return "not supported";
case GVFG_ETIMEOUT:
return "timeout";
default:
return "unknown";
}
}
}
+1
View File
@@ -26,6 +26,7 @@ target_compile_definitions(gvfg_preview PRIVATE
)
target_link_libraries(gvfg_preview PRIVATE
gvfg
user32
dxgi
d3d11
+18 -10
View File
@@ -46,16 +46,20 @@ typedef struct
char pixel_format[32];
} gvfg_preview_info_t;
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_create(gvfg_preview_handle *out_handle);
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_destroy(gvfg_preview_handle handle);
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_create(
_Outptr_ gvfg_preview_handle *out_handle);
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_destroy(
_In_opt_ gvfg_preview_handle handle);
/*
* Attach a native window handle for preview output.
*
* On Windows, native_window_handle is an HWND.
*/
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_attach_window(gvfg_preview_handle handle,
void *native_window_handle);
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_attach_window(
_In_ gvfg_preview_handle handle,
_In_ void *native_window_handle);
/*
* Synchronously render one frame.
@@ -63,15 +67,19 @@ GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_attach_window(gvfg_preview_h
* The frame remains owned by gvfg.dll. The caller should call this between
* gvfg_read_frame() and gvfg_release_frame().
*/
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_render_frame(gvfg_preview_handle handle,
const gvfg_frame_t *frame);
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_render_frame(
_In_ gvfg_preview_handle handle,
_In_ const gvfg_frame_t *frame);
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_get_info(gvfg_preview_handle handle,
gvfg_preview_info_t *out_info);
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_get_info(
_In_ gvfg_preview_handle handle,
_Out_ gvfg_preview_info_t *out_info);
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_shutdown(gvfg_preview_handle handle);
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_shutdown(
_In_ gvfg_preview_handle handle);
GVFG_PREVIEW_API const char *gvfg_preview_strerror(gvfg_preview_status_t status);
GVFG_PREVIEW_API const char *gvfg_preview_strerror(
_In_ gvfg_preview_status_t status);
#ifdef __cplusplus
}
+27 -3
View File
@@ -42,19 +42,43 @@ public:
if (!ensureDevice() || !ensurePipeline(frame.width, frame.height, frame.bit_depth > 0 ? frame.bit_depth : 8))
return false;
const auto *base = static_cast<const uint8_t *>(frame.data);
gvfg::internal::gvfg_render_pixfmt_t renderFmt = gvfg::internal::GVFG_RENDER_FMT_YUY2;
const uint8_t *base = static_cast<const uint8_t *>(frame.data);
int stride = 0;
bool uploaded = false;
switch (frame.pixel_format)
{
case GVFG_PIXFMT_YUY2:
renderFmt = gvfg::internal::GVFG_RENDER_FMT_YUY2;
uploaded = pipeline_->upload_yuy2_frame(base, frame.width * 2, frame.width, frame.height);
stride = frame.width * 2;
break;
case GVFG_PIXFMT_Y210:
renderFmt = gvfg::internal::GVFG_RENDER_FMT_Y210;
uploaded = pipeline_->upload_y210_frame(base, frame.width * 4, frame.width, frame.height);
stride = frame.width * 4;
break;
default:
return false;
}
gvfg_frame_layout_t layout{};
layout.struct_size = sizeof(layout);
if (gvfg_get_frame_layout(&frame, &layout) == GVFG_OK &&
layout.plane_count > 0 &&
layout.plane_data[0] &&
layout.plane_stride[0] > 0)
{
base = static_cast<const uint8_t *>(layout.plane_data[0]);
stride = layout.plane_stride[0];
}
switch (frame.pixel_format)
{
case GVFG_PIXFMT_YUY2:
uploaded = pipeline_->upload_yuy2_frame(base, stride, frame.width, frame.height);
break;
case GVFG_PIXFMT_Y210:
uploaded = pipeline_->upload_y210_frame(base, stride, frame.width, frame.height);
break;
default:
return false;
+97 -19
View File
@@ -35,9 +35,8 @@
* - At most one frame may be held by a handle at a time.
*/
#include <stdint.h>
#ifdef _WIN32
#include <sal.h>
#ifdef GVFG_BUILD
#define GVFG_API __declspec(dllexport)
#else
@@ -45,7 +44,30 @@
#endif
#else
#define GVFG_API
#ifndef _In_
#define _In_
#endif
#ifndef _In_opt_
#define _In_opt_
#endif
#ifndef _Inout_
#define _Inout_
#endif
#ifndef _Out_
#define _Out_
#endif
#ifndef _Outptr_
#define _Outptr_
#endif
#ifndef _Out_writes_to_opt_
#define _Out_writes_to_opt_(size, count)
#endif
#ifndef _Out_writes_z_
#define _Out_writes_z_(size)
#endif
#endif
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
@@ -82,6 +104,12 @@ typedef enum
GVFG_PIXFMT_BGRA8 = 100
} gvfg_pixel_format_t;
typedef enum
{
GVFG_FRAME_LAYOUT_CONTIGUOUS = 1u << 0, /* Planes are contained inside one contiguous data buffer. */
GVFG_FRAME_LAYOUT_SDK_DERIVED = 1u << 1 /* Layout was derived by the SDK from the native format. */
} gvfg_frame_layout_flags_t;
typedef struct
{
int index; /* Device index to pass to gvfg_open(). */
@@ -127,6 +155,19 @@ typedef struct
uint64_t frame_id; /* Monotonic frame identifier from the backend. */
} gvfg_frame_t;
typedef struct
{
uint32_t struct_size; /* Set to sizeof(gvfg_frame_layout_t) before calling. */
uint32_t layout_flags; /* Bitmask of gvfg_frame_layout_flags_t values. */
int row_bytes; /* Bytes per row for plane 0. Prefer plane_stride[] for new code. */
int plane_count; /* Number of valid entries in plane_data/plane_stride/plane_size. */
const void *plane_data[GVFG_MAX_PLANES]; /* Plane pointers inside frame.data, valid until gvfg_release_frame(). */
int plane_stride[GVFG_MAX_PLANES]; /* Bytes from one row to the next for each plane. */
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. */
uint64_t reserved[8]; /* Reserved for future SDK/driver layout metadata. Must be ignored. */
} gvfg_frame_layout_t;
typedef enum
{
GVFG_EVENT_UNKNOWN = 0,
@@ -160,7 +201,9 @@ typedef struct gvfg_handle_t *gvfg_handle;
* available devices.
* - Returns <= 0 when no device is available.
*/
GVFG_API int gvfg_enumerate_devices(gvfg_device_info_t *out_devices, int max_devices);
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.
@@ -175,7 +218,8 @@ GVFG_API int gvfg_enumerate_devices(gvfg_device_info_t *out_devices, int max_dev
* The returned handle starts in the closed state. Release it with
* gvfg_destroy().
*/
GVFG_API gvfg_status_t gvfg_create(gvfg_handle *out_handle);
GVFG_API gvfg_status_t gvfg_create(
_Outptr_ gvfg_handle *out_handle);
/*
* Destroy a GVFG capture session.
@@ -189,7 +233,8 @@ GVFG_API gvfg_status_t gvfg_create(gvfg_handle *out_handle);
* If capture is still running, it is stopped first. After this call, the handle
* must not be used again.
*/
GVFG_API gvfg_status_t gvfg_destroy(gvfg_handle handle);
GVFG_API gvfg_status_t gvfg_destroy(
_In_opt_ gvfg_handle handle);
/*
* Open a device by index from gvfg_enumerate_devices().
@@ -206,7 +251,9 @@ GVFG_API gvfg_status_t gvfg_destroy(gvfg_handle handle);
*
* The current implementation selects the SDI input internally.
*/
GVFG_API gvfg_status_t gvfg_open(gvfg_handle handle, int device_index);
GVFG_API gvfg_status_t gvfg_open(
_In_ gvfg_handle handle,
_In_ int device_index);
/*
* Configure and start capture on an opened device.
@@ -223,7 +270,8 @@ GVFG_API gvfg_status_t gvfg_open(gvfg_handle handle, int device_index);
* After success, call gvfg_read_frame() to receive frames and gvfg_poll_event()
* to receive capture events.
*/
GVFG_API gvfg_status_t gvfg_start(gvfg_handle handle);
GVFG_API gvfg_status_t gvfg_start(
_In_ gvfg_handle handle);
/*
* Read one captured frame.
@@ -244,9 +292,31 @@ GVFG_API gvfg_status_t gvfg_start(gvfg_handle handle);
* 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(gvfg_handle handle,
gvfg_frame_t *out_frame,
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().
*
* Parameters:
* - frame: Frame descriptor returned by gvfg_read_frame(). Must not be NULL.
* - 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
* 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);
/*
* Release a frame returned by gvfg_read_frame().
@@ -261,8 +331,9 @@ GVFG_API gvfg_status_t gvfg_read_frame(gvfg_handle handle,
* - GVFG_EINVAL if handle or frame is NULL.
* - GVFG_ESTATE if no frame is currently held.
*/
GVFG_API gvfg_status_t gvfg_release_frame(gvfg_handle handle,
const gvfg_frame_t *frame);
GVFG_API gvfg_status_t gvfg_release_frame(
_In_ gvfg_handle handle,
_In_ const gvfg_frame_t *frame);
/*
* Poll one capture event.
@@ -277,9 +348,10 @@ GVFG_API gvfg_status_t gvfg_release_frame(gvfg_handle handle,
* - GVFG_EINVAL if handle or out_event is NULL.
* - GVFG_ETIMEOUT if no event is available before timeout_ms expires.
*/
GVFG_API gvfg_status_t gvfg_poll_event(gvfg_handle handle,
gvfg_event_t *out_event,
uint32_t timeout_ms);
GVFG_API gvfg_status_t gvfg_poll_event(
_In_ gvfg_handle handle,
_Out_ gvfg_event_t *out_event,
_In_ uint32_t timeout_ms);
/*
* Stop capture.
@@ -293,7 +365,8 @@ GVFG_API gvfg_status_t gvfg_poll_event(gvfg_handle handle,
*
* This stops backend capture and invalidates any unreleased frame.
*/
GVFG_API gvfg_status_t gvfg_stop(gvfg_handle handle);
GVFG_API gvfg_status_t gvfg_stop(
_In_ gvfg_handle handle);
/*
* Query current signal information and delivered buffer format.
@@ -309,7 +382,9 @@ GVFG_API gvfg_status_t gvfg_stop(gvfg_handle handle);
*
* Use gvfg_runtime_info_t::last_frame for the frame buffer format.
*/
GVFG_API gvfg_status_t gvfg_get_signal_status(gvfg_handle handle, gvfg_signal_status_t *out_status);
GVFG_API gvfg_status_t gvfg_get_signal_status(
_In_ gvfg_handle handle,
_Out_ gvfg_signal_status_t *out_status);
/*
* Query runtime capture diagnostics.
@@ -325,7 +400,9 @@ GVFG_API gvfg_status_t gvfg_get_signal_status(gvfg_handle handle, gvfg_signal_st
* The result includes current signal status, SDK-measured capture FPS, and the
* number of frames delivered by the SDK.
*/
GVFG_API gvfg_status_t gvfg_get_runtime_info(gvfg_handle handle, gvfg_runtime_info_t *out_info);
GVFG_API gvfg_status_t gvfg_get_runtime_info(
_In_ gvfg_handle handle,
_Out_ gvfg_runtime_info_t *out_info);
/*
* Convert a GVFG status code to a static English error string.
@@ -336,7 +413,8 @@ GVFG_API gvfg_status_t gvfg_get_runtime_info(gvfg_handle handle, gvfg_runtime_in
* Returns:
* - Static null-terminated English string. The caller must not free it.
*/
GVFG_API const char *gvfg_strerror(gvfg_status_t status);
GVFG_API const char *gvfg_strerror(
_In_ gvfg_status_t status);
#ifdef __cplusplus
}
+10 -7
View File
@@ -56,25 +56,28 @@ typedef struct
* This exposes implementation-level counters for internal tools only. Customer
* applications should use gvfg_get_runtime_info() instead.
*/
GVFG_API gvfg_status_t gvfg_debug_get_backend_stats(gvfg_handle handle,
gvfg_debug_backend_stats_t *out_stats);
GVFG_API gvfg_status_t gvfg_debug_get_backend_stats(
_In_ gvfg_handle handle,
_Out_ gvfg_debug_backend_stats_t *out_stats);
/*
* Query raw FPGA signal values for internal hardware/FPGA debugging.
*
* These values are intentionally excluded from gvfg_capture.h.
*/
GVFG_API gvfg_status_t gvfg_debug_get_fpga_signal_raw(gvfg_handle handle,
gvfg_debug_fpga_signal_raw_t *out_raw);
GVFG_API gvfg_status_t gvfg_debug_get_fpga_signal_raw(
_In_ gvfg_handle handle,
_Out_ gvfg_debug_fpga_signal_raw_t *out_raw);
/*
* Copy the latest backend error detail into out_message.
*
* The message is UTF-8 and null-terminated when out_message_size is non-zero.
*/
GVFG_API gvfg_status_t gvfg_debug_get_last_error_detail(gvfg_handle handle,
char *out_message,
uint32_t out_message_size);
GVFG_API gvfg_status_t gvfg_debug_get_last_error_detail(
_In_ gvfg_handle handle,
_Out_writes_to_opt_(out_message_size, out_message_size) char *out_message,
_In_ uint32_t out_message_size);
#ifdef __cplusplus
}
+158
View File
@@ -6,6 +6,7 @@
#include <algorithm>
#include <atomic>
#include <chrono>
#include <climits>
#include <condition_variable>
#include <cstdio>
#include <cstring>
@@ -172,6 +173,149 @@ namespace
}
}
bool checked_mul_u64(uint64_t a, uint64_t b, uint64_t &out)
{
if (a != 0 && b > UINT64_MAX / a)
return false;
out = a * b;
return true;
}
bool checked_add_u64(uint64_t a, uint64_t b, uint64_t &out)
{
if (b > UINT64_MAX - a)
return false;
out = a + b;
return true;
}
bool set_frame_plane(const gvfg_frame_t &frame,
gvfg_frame_layout_t &layout,
int index,
uint64_t offset,
uint64_t size,
int stride)
{
if (index < 0 || index >= GVFG_MAX_PLANES || !frame.data || stride < 0)
return false;
uint64_t end = 0;
if (!checked_add_u64(offset, size, end) || end > frame.data_size)
return false;
layout.plane_data[index] = static_cast<const uint8_t *>(frame.data) + offset;
layout.plane_offset[index] = offset;
layout.plane_size[index] = size;
layout.plane_stride[index] = stride;
return true;
}
void set_fallback_frame_layout(const gvfg_frame_t &frame, gvfg_frame_layout_t &layout)
{
layout.row_bytes = 0;
layout.plane_count = frame.data ? 1 : 0;
layout.layout_flags = 0;
if (layout.plane_count == 0)
return;
int stride = 0;
if (frame.height > 0)
{
const uint64_t guessedStride = frame.data_size / static_cast<uint64_t>(frame.height);
if (guessedStride <= static_cast<uint64_t>(INT_MAX))
stride = static_cast<int>(guessedStride);
}
layout.row_bytes = stride;
if (set_frame_plane(frame, layout, 0, 0, frame.data_size, stride))
layout.layout_flags = GVFG_FRAME_LAYOUT_CONTIGUOUS | GVFG_FRAME_LAYOUT_SDK_DERIVED;
}
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;
std::memset(layout.plane_data, 0, sizeof(layout.plane_data));
std::memset(layout.plane_stride, 0, sizeof(layout.plane_stride));
std::memset(layout.plane_size, 0, sizeof(layout.plane_size));
std::memset(layout.plane_offset, 0, sizeof(layout.plane_offset));
std::memset(layout.reserved, 0, sizeof(layout.reserved));
if (!frame.data || frame.data_size == 0 || frame.width <= 0 || frame.height <= 0)
return GVFG_EINVAL;
const uint64_t width = static_cast<uint64_t>(frame.width);
const uint64_t height = static_cast<uint64_t>(frame.height);
uint64_t row = 0;
uint64_t size0 = 0;
uint64_t size1 = 0;
switch (frame.pixel_format)
{
case GVFG_PIXFMT_YUY2:
case GVFG_PIXFMT_UYVY:
if (!checked_mul_u64(width, 2u, row) || !checked_mul_u64(row, height, size0))
break;
if (row <= static_cast<uint64_t>(INT_MAX) && set_frame_plane(frame, layout, 0, 0, size0, static_cast<int>(row)))
layout.plane_count = 1;
break;
case GVFG_PIXFMT_Y210:
case GVFG_PIXFMT_BGRX32:
case GVFG_PIXFMT_BGRA8:
if (!checked_mul_u64(width, 4u, row) || !checked_mul_u64(row, height, size0))
break;
if (row <= static_cast<uint64_t>(INT_MAX) && set_frame_plane(frame, layout, 0, 0, size0, static_cast<int>(row)))
layout.plane_count = 1;
break;
case GVFG_PIXFMT_RGB24:
if (!checked_mul_u64(width, 3u, row) || !checked_mul_u64(row, height, size0))
break;
if (row <= static_cast<uint64_t>(INT_MAX) && set_frame_plane(frame, layout, 0, 0, size0, static_cast<int>(row)))
layout.plane_count = 1;
break;
case GVFG_PIXFMT_YUV444:
if (!checked_mul_u64(width, frame.bit_depth > 8 ? 6u : 3u, row) ||
!checked_mul_u64(row, height, size0))
break;
if (row <= static_cast<uint64_t>(INT_MAX) && set_frame_plane(frame, layout, 0, 0, size0, static_cast<int>(row)))
layout.plane_count = 1;
break;
case GVFG_PIXFMT_NV12:
row = width;
if (!checked_mul_u64(row, height, size0) ||
!checked_mul_u64(row, height / 2u, size1))
break;
if (row <= static_cast<uint64_t>(INT_MAX) &&
set_frame_plane(frame, layout, 0, 0, size0, static_cast<int>(row)) &&
set_frame_plane(frame, layout, 1, size0, size1, static_cast<int>(row)))
layout.plane_count = 2;
break;
case GVFG_PIXFMT_P010:
if (!checked_mul_u64(width, 2u, row) ||
!checked_mul_u64(row, height, size0) ||
!checked_mul_u64(row, height / 2u, size1))
break;
if (row <= static_cast<uint64_t>(INT_MAX) &&
set_frame_plane(frame, layout, 0, 0, size0, static_cast<int>(row)) &&
set_frame_plane(frame, layout, 1, size0, size1, static_cast<int>(row)))
layout.plane_count = 2;
break;
default:
break;
}
if (layout.plane_count > 0)
{
layout.row_bytes = layout.plane_stride[0];
layout.layout_flags = GVFG_FRAME_LAYOUT_CONTIGUOUS | GVFG_FRAME_LAYOUT_SDK_DERIVED;
return GVFG_OK;
}
set_fallback_frame_layout(frame, layout);
return layout.plane_count > 0 ? GVFG_OK : GVFG_ENOTSUP;
}
const char *fpga_video_format_name(uint32_t value)
{
switch (value & 0x3u)
@@ -830,6 +974,20 @@ extern "C"
return handle->readFrame(*out_frame, timeout_ms);
}
gvfg_status_t gvfg_get_frame_layout(const gvfg_frame_t *frame,
gvfg_frame_layout_t *out_layout)
{
if (!frame || !out_layout)
return GVFG_EINVAL;
if (out_layout->struct_size < sizeof(gvfg_frame_layout_t))
return GVFG_EINVAL;
const uint32_t callerSize = out_layout->struct_size;
std::memset(out_layout, 0, sizeof(*out_layout));
out_layout->struct_size = callerSize;
return populate_frame_layout(*frame, *out_layout);
}
gvfg_status_t gvfg_release_frame(gvfg_handle handle, const gvfg_frame_t *frame)
{
if (!handle || !frame)