Deliver native callback frames

This commit is contained in:
Mark.lin
2026-06-09 16:05:27 +08:00
parent 31ace3693f
commit 739d1fcf32
4 changed files with 198 additions and 143 deletions
+40 -12
View File
@@ -341,12 +341,14 @@ typedef struct
Use `gvfg_signal_status_t` fields for display-ready signal data.
Use `gvfg_signal_status_t::fpga` for FPGA raw register values and read-valid flags.
For delivered frame format, prefer `gvfg_runtime_info_t::delivered_frame`.
For callback frame format, prefer `gvfg_runtime_info_t::callback_frame`.
For SDK-managed preview output format, use `gvfg_runtime_info_t::preview_output`.
For example, if FPGA reports `video_format=YUV422` and `bit_depth=10`, the
hardware signal should be treated as YUV422 10-bit. The current GVFG callback
buffer is a rendered BGRA8 frame, reported through
`gvfg_runtime_info_t::delivered_frame`.
hardware signal should be treated as YUV422 10-bit. The GVFG callback buffer is
delivered in the native backend format, reported through
`gvfg_runtime_info_t::callback_frame`. The preview swapchain is reported
separately through `gvfg_runtime_info_t::preview_output`.
FPGA frame-rate code table:
@@ -378,12 +380,23 @@ typedef struct
int bit_depth;
char pixel_format[32];
int valid;
} gvfg_delivered_frame_info_t;
} gvfg_callback_frame_info_t;
typedef struct
{
int enabled;
int active;
int width;
int height;
int bit_depth;
char pixel_format[32];
} gvfg_preview_output_info_t;
typedef struct
{
gvfg_signal_status_t input_signal;
gvfg_delivered_frame_info_t delivered_frame;
gvfg_preview_output_info_t preview_output;
gvfg_callback_frame_info_t callback_frame;
double capture_fps;
uint64_t delivered_frames;
} gvfg_runtime_info_t;
@@ -392,14 +405,16 @@ typedef struct
| 欄位 | 意義 |
| --- | --- |
| `input_signal` | 目前 input signal 狀態 |
| `delivered_frame` | Frame buffer delivered by gvfg.dll to the app callback: valid, width, height, pixel format, bit depth |
| `preview_output` | SDK-managed preview output: enabled, active, width, height, pixel format, bit depth |
| `callback_frame` | Frame buffer delivered by gvfg.dll to the app callback: valid, width, height, pixel format, bit depth |
| `capture_fps` | SDK capture thread 從 backend frame 測到的 FPS |
| `delivered_frames` | SDK 已送到 app callback 的 frame 數 |
Source ownership:
- FPGA reported signal: use `input_signal.fpga.*`.
- Delivered callback buffer: use `delivered_frame.*`. `valid=0` means no callback frame is currently available/reported.
- Preview output: use `preview_output.*`. `active=0` means no preview swapchain is currently active.
- Delivered callback buffer: use `callback_frame.*`. `valid=0` means no callback frame is currently available/reported.
- App/SDK runtime counters: use `capture_fps` and `delivered_frames`.
這個 struct 適合顯示在:
@@ -455,9 +470,15 @@ frame callback 的 payload。
typedef struct
{
const void *data;
uint64_t data_size;
int stride;
int width;
int height;
int pixel_format;
int bit_depth;
int plane_count;
uint32_t plane_offset_bytes[GVFG_MAX_PLANES];
uint32_t plane_stride_bytes[GVFG_MAX_PLANES];
uint64_t pts_ns;
uint64_t frame_id;
} gvfg_frame_t;
@@ -465,10 +486,16 @@ typedef struct
| 欄位 | 意義 |
| --- | --- |
| `data` | pixel data pointer |
| `data` | native pixel data pointer |
| `data_size` | total bytes available from `data` |
| `stride` | 每一列 bytes |
| `width` | frame 寬度 |
| `height` | frame 高度 |
| `pixel_format` | `gvfg_pixel_format_t` value |
| `bit_depth` | native frame bit depth |
| `plane_count` | number of valid planes |
| `plane_offset_bytes` | byte offset for each plane from `data` |
| `plane_stride_bytes` | row stride for each plane |
| `pts_ns` | timestamp,單位 ns |
| `frame_id` | frame counter |
@@ -477,7 +504,7 @@ typedef struct
- `data` 只在 callback 當下有效。
- callback return 後不能再使用這個 pointer。
- 如果客戶要存圖、做 snapshot、或丟給其他 thread,要自己 copy 一份。
- 目前 callback 輸出是 8-bit BGRA-compatible 格式,在 Windows/Qt 可用 `QImage::Format_ARGB32` 接。
- callback buffer uses the native backend format. Use `pixel_format`, `bit_depth`, and plane fields to interpret `data`.
- callback 由 SDK worker thread 呼叫,不是在 UI thread。
UI 程式要注意:
@@ -1274,6 +1301,7 @@ wait_frame: deliver
## Current XDMA Notes
- The current XDMA path exposes decoded FPGA signal metadata through `sig.fpga`.
- The delivered customer callback buffer is reported by `gvfg_runtime_info_t::delivered_frame`.
- `sig.bit_depth=10` does not by itself mean the delivered callback buffer is `Y210`; the current callback path reports BGRA8.
- The SDK-managed preview swapchain is reported by `gvfg_runtime_info_t::preview_output`.
- The delivered customer callback buffer is reported by `gvfg_runtime_info_t::callback_frame`.
- The callback buffer is delivered in the native backend format, such as `YUY2`, `Y210`, `NV12`, or `P010`.
- `sig.frame_rate_bits=0001` is not in the supported table, so the UI/API reports it as `--`.
+19 -10
View File
@@ -334,7 +334,8 @@ void MainWindow::updateSignalStatus(bool writeLog)
const auto &signal = info.input_signal;
const auto &fpga = signal.fpga;
const auto &delivered = info.delivered_frame;
const auto &preview = info.preview_output;
const auto &callback = info.callback_frame;
const bool frameRateValid = fpgaMaskValid(fpga.valid_mask, GVFG_FPGA_SIGNAL_VALID_FRAME_RATE);
const bool videoFormatValid = fpgaMaskValid(fpga.valid_mask, GVFG_FPGA_SIGNAL_VALID_VIDEO_FORMAT);
const bool bitDepthValid = fpgaMaskValid(fpga.valid_mask, GVFG_FPGA_SIGNAL_VALID_BIT_DEPTH);
@@ -360,19 +361,27 @@ void MainWindow::updateSignalStatus(bool writeLog)
.arg(fpgaFieldText(statusValid, QString::number(signal.sdi_ddr_ok)))
.arg(fpgaFieldText(statusValid, QString::number(signal.hdmi_locked)))
.arg(fpgaFieldText(statusValid, QString::number(signal.hdmi_ddr_ok)));
const QString line3 = delivered.valid
? QStringLiteral("Delivered frame | frame=%1x%2 format=%3 bitdepth=%4")
.arg(delivered.width)
.arg(delivered.height)
.arg(QString::fromUtf8(delivered.pixel_format))
.arg(delivered.bit_depth)
: QStringLiteral("Delivered frame | --");
const QString line4 = QStringLiteral("App runtime | capture=%1 fps delivered=%2")
const QString line3 = preview.active
? QStringLiteral("Preview output | frame=%1x%2 format=%3 bitdepth=%4")
.arg(preview.width)
.arg(preview.height)
.arg(QString::fromUtf8(preview.pixel_format))
.arg(preview.bit_depth)
: (preview.enabled ? QStringLiteral("Preview output | configured, inactive")
: QStringLiteral("Preview output | disabled"));
const QString line4 = callback.valid
? QStringLiteral("Callback frame | frame=%1x%2 format=%3 bitdepth=%4")
.arg(callback.width)
.arg(callback.height)
.arg(QString::fromUtf8(callback.pixel_format))
.arg(callback.bit_depth)
: QStringLiteral("Callback frame | --");
const QString line5 = QStringLiteral("App runtime | capture=%1 fps delivered=%2")
.arg(info.capture_fps > 0.0 ? QString::number(info.capture_fps, 'f', 2)
: QStringLiteral("--"))
.arg(static_cast<qulonglong>(info.delivered_frames));
const QString statusText = line0 + QLatin1Char('\n') + line2 + QLatin1Char('\n') + line3 + QLatin1Char('\n') + line4;
const QString statusText = line0 + QLatin1Char('\n') + line2 + QLatin1Char('\n') + line3 + QLatin1Char('\n') + line4 + QLatin1Char('\n') + line5;
if (lastSignalStatusText_ != statusText)
{
ui_->statusLabel->setText(statusText);
+40 -8
View File
@@ -59,7 +59,8 @@ extern "C" {
enum
{
GVFG_MAX_DEVICES = 16
GVFG_MAX_DEVICES = 16,
GVFG_MAX_PLANES = 4
};
typedef enum
@@ -80,6 +81,20 @@ typedef enum
GVFG_PREVIEW_BITDEPTH_8BIT = 8 /* Force an 8-bit preview swapchain. */
} gvfg_preview_bitdepth_t;
typedef enum
{
GVFG_PIXFMT_UNKNOWN = 0,
GVFG_PIXFMT_YUY2 = 1,
GVFG_PIXFMT_UYVY = 2,
GVFG_PIXFMT_RGB24 = 3,
GVFG_PIXFMT_BGRX32 = 4,
GVFG_PIXFMT_NV12 = 5,
GVFG_PIXFMT_P010 = 6,
GVFG_PIXFMT_Y210 = 7,
GVFG_PIXFMT_YUV444 = 8,
GVFG_PIXFMT_BGRA8 = 100
} gvfg_pixel_format_t;
typedef struct
{
int index; /* Device index to pass to gvfg_open(). */
@@ -135,15 +150,26 @@ typedef struct
{
int width; /* Width of frames delivered to the customer callback. */
int height; /* Height of frames delivered to the customer callback. */
int bit_depth; /* Bit depth of the delivered callback buffer. */
char pixel_format[32]; /* Delivered callback buffer format, for example BGRA8. */
int bit_depth; /* Bits per color channel of the callback buffer. */
char pixel_format[32]; /* Native callback buffer format, for example YUY2, Y210, NV12, or P010. */
int valid; /* Non-zero while capture is running after at least one callback frame. */
} gvfg_delivered_frame_info_t;
} gvfg_callback_frame_info_t;
typedef struct
{
int enabled; /* Non-zero when SDK-managed preview was requested. */
int active; /* Non-zero when the preview pipeline is active. */
int width; /* Preview render width. */
int height; /* Preview render height. */
int bit_depth; /* Preview output bit depth, usually 8 or 10. */
char pixel_format[32]; /* Preview output format, for example BGRA8 or RGB10A2. */
} gvfg_preview_output_info_t;
typedef struct
{
gvfg_signal_status_t input_signal; /* FPGA-reported signal metadata. */
gvfg_delivered_frame_info_t delivered_frame; /* Frame buffer delivered by gvfg.dll to the app. */
gvfg_preview_output_info_t preview_output; /* SDK-managed preview output, separate from callbacks. */
gvfg_callback_frame_info_t callback_frame; /* Frame buffer delivered by gvfg.dll to the app callback. */
double capture_fps; /* Runtime FPS measured from backend frames seen by the SDK worker. */
uint64_t delivered_frames; /* Number of frames delivered to the app callback. */
} gvfg_runtime_info_t;
@@ -162,10 +188,16 @@ typedef struct
typedef struct
{
const void *data; /* Frame pixels. Valid only during the frame callback. */
int stride; /* Bytes per row for data. */
const void *data; /* Native frame buffer. Valid only during the frame callback. */
uint64_t data_size; /* Total bytes available from data. */
int stride; /* Bytes per row for plane 0. */
int width; /* Frame width in pixels. */
int height; /* Frame height in pixels. */
int pixel_format; /* gvfg_pixel_format_t value. */
int bit_depth; /* Bits per color channel of the native frame. */
int plane_count; /* Number of valid planes. */
uint32_t plane_offset_bytes[GVFG_MAX_PLANES];
uint32_t plane_stride_bytes[GVFG_MAX_PLANES];
uint64_t pts_ns; /* Presentation timestamp in nanoseconds. */
uint64_t frame_id; /* Monotonic frame identifier from the backend. */
} gvfg_frame_t;
@@ -405,7 +437,7 @@ GVFG_API gvfg_status_t gvfg_stop(gvfg_handle handle);
* - GVFG_ENODEV if no valid signal information is available.
*
* The FPGA metadata describes the hardware signal. Use
* gvfg_runtime_info_t::delivered_frame for the callback buffer format.
* gvfg_runtime_info_t::callback_frame for the callback buffer format.
*/
GVFG_API gvfg_status_t gvfg_get_signal_status(gvfg_handle handle, gvfg_signal_status_t *out_status);
+95 -109
View File
@@ -163,6 +163,58 @@ namespace
return (mask & (1u << bit)) != 0;
}
int to_gvfg_pixel_format(gdriver_pixel_format_t fmt)
{
switch (fmt)
{
case GDRIVER_PIXFMT_YUY2:
return GVFG_PIXFMT_YUY2;
case GDRIVER_PIXFMT_UYVY:
return GVFG_PIXFMT_UYVY;
case GDRIVER_PIXFMT_RGB24:
return GVFG_PIXFMT_RGB24;
case GDRIVER_PIXFMT_BGRX32:
return GVFG_PIXFMT_BGRX32;
case GDRIVER_PIXFMT_NV12:
return GVFG_PIXFMT_NV12;
case GDRIVER_PIXFMT_P010:
return GVFG_PIXFMT_P010;
case GDRIVER_PIXFMT_Y210:
return GVFG_PIXFMT_Y210;
case GDRIVER_PIXFMT_YUV444:
return GVFG_PIXFMT_YUV444;
default:
return GVFG_PIXFMT_UNKNOWN;
}
}
const char *gvfg_pixel_format_name(int fmt)
{
switch (fmt)
{
case GVFG_PIXFMT_YUY2:
return "YUY2";
case GVFG_PIXFMT_UYVY:
return "UYVY";
case GVFG_PIXFMT_RGB24:
return "RGB24";
case GVFG_PIXFMT_BGRX32:
return "BGRX32";
case GVFG_PIXFMT_NV12:
return "NV12";
case GVFG_PIXFMT_P010:
return "P010";
case GVFG_PIXFMT_Y210:
return "Y210";
case GVFG_PIXFMT_YUV444:
return "YUV444";
case GVFG_PIXFMT_BGRA8:
return "BGRA8";
default:
return "UNKNOWN";
}
}
const char *fpga_video_format_name(uint32_t value)
{
switch (value & 0x3u)
@@ -223,20 +275,6 @@ namespace
copy_cstr(dst, dstSize, bits);
}
int clamp_u8(int v)
{
return (std::max)(0, (std::min)(255, v));
}
void yuv_to_rgb(int y, int u, int v, uint8_t &r, uint8_t &g, uint8_t &b)
{
const int c = y - 16;
const int d = u - 128;
const int e = v - 128;
r = static_cast<uint8_t>(clamp_u8((298 * c + 409 * e + 128) >> 8));
g = static_cast<uint8_t>(clamp_u8((298 * c - 100 * d - 208 * e + 128) >> 8));
b = static_cast<uint8_t>(clamp_u8((298 * c + 516 * d + 128) >> 8));
}
}
struct gvfg_handle_t
@@ -388,13 +426,26 @@ struct gvfg_handle_t
getSignalStatus(out.input_signal);
const uint64_t frames = deliveredFrames.load(std::memory_order_relaxed);
const bool deliveredValid = running.load(std::memory_order_relaxed) && frames > 0;
out.delivered_frame.valid = deliveredValid ? 1 : 0;
out.preview_output.enabled = previewHwnd ? 1 : 0;
out.preview_output.active = (pipeline && previewHwnd) ? 1 : 0;
if (out.preview_output.active)
{
out.preview_output.width = pipeline->preview_w_;
out.preview_output.height = pipeline->preview_h_;
out.preview_output.bit_depth = pipeline->preview_swapchain_10bit() ? 10 : 8;
copy_cstr(out.preview_output.pixel_format, sizeof(out.preview_output.pixel_format),
pipeline->preview_swapchain_10bit() ? "RGB10A2" : "BGRA8");
}
out.callback_frame.valid = deliveredValid ? 1 : 0;
if (deliveredValid)
{
out.delivered_frame.width = static_cast<int>(deliveredWidth.load(std::memory_order_relaxed));
out.delivered_frame.height = static_cast<int>(deliveredHeight.load(std::memory_order_relaxed));
out.delivered_frame.bit_depth = 8;
copy_cstr(out.delivered_frame.pixel_format, sizeof(out.delivered_frame.pixel_format), "BGRA8");
out.callback_frame.width = static_cast<int>(deliveredWidth.load(std::memory_order_relaxed));
out.callback_frame.height = static_cast<int>(deliveredHeight.load(std::memory_order_relaxed));
out.callback_frame.bit_depth = static_cast<int>(deliveredBitDepth.load(std::memory_order_relaxed));
copy_cstr(out.callback_frame.pixel_format,
sizeof(out.callback_frame.pixel_format),
gvfg_pixel_format_name(deliveredPixelFormat.load(std::memory_order_relaxed)));
}
out.capture_fps = runtimeFps.load(std::memory_order_relaxed);
out.delivered_frames = frames;
@@ -666,11 +717,8 @@ struct gvfg_handle_t
}
updateRuntimeFps(frame.timestamp_ns ? frame.timestamp_ns : now_ns());
const bool rendered = renderGpuFrame(frame);
if (rendered)
emitReadbackFrame(frame);
else
emitCpuFallbackFrame(frame);
renderGpuFrame(frame);
emitNativeFrame(frame);
backend->release_frame(frame);
}
}
@@ -757,100 +805,34 @@ struct gvfg_handle_t
return ok;
}
void emitReadbackFrame(const xdma_frame_t &frame)
void emitNativeFrame(const xdma_frame_t &frame)
{
if (!onFrame || !pipeline || !ctx)
return;
if (!shouldEmitFrameCallback(frame.frame_id))
return;
const int w = static_cast<int>(frame.width);
const int h = static_cast<int>(frame.height);
if (!pipeline->blit_fp16_to_rgba8(w, h))
return;
gvfg_render_frame_t readback{};
if (pipeline->readback_to_frame(w, h, frame.timestamp_ns ? frame.timestamp_ns : now_ns(), frame.frame_id, &readback))
{
gvfg_frame_t out{};
out.data = readback.data[0];
out.stride = readback.stride[0];
out.width = readback.width;
out.height = readback.height;
out.pts_ns = readback.pts_ns;
out.frame_id = readback.frame_id;
onFrame(&out, callbackUser);
noteDeliveredFrame(out.width, out.height);
ctx->Unmap(pipeline->rt_stage_.Get(), 0);
}
}
void emitCpuFallbackFrame(const xdma_frame_t &frame)
{
if (frame.pixel_format != GDRIVER_PIXFMT_YUY2)
{
if (!fallbackLogged)
{
emitError(GVFG_ENOTSUP, "GVFG preview currently supports YUV422/YUV420 GPU paths only; RGB/YUV444 input is not rendered");
fallbackLogged = true;
}
return;
}
if (!onFrame || !frame.data)
return;
if (!shouldEmitFrameCallback(frame.frame_id))
return;
const int w = static_cast<int>(frame.width);
const int h = static_cast<int>(frame.height);
const int srcStride = frame.plane_stride_bytes[0] ? static_cast<int>(frame.plane_stride_bytes[0]) : w * 2;
if (w <= 0 || h <= 0 || srcStride < w * 2)
if (frame.width == 0 || frame.height == 0 || frame.plane_count == 0)
return;
if (!fallbackLogged)
{
emitError(GVFG_ENOTSUP, "GPU preview/readback unavailable; using CPU YUY2 preview fallback");
fallbackLogged = true;
}
fallbackBgra.resize(static_cast<size_t>(w) * static_cast<size_t>(h) * 4u);
const auto *srcBase = static_cast<const uint8_t *>(frame.data);
for (int y = 0; y < h; ++y)
{
const uint8_t *src = srcBase + static_cast<size_t>(y) * static_cast<size_t>(srcStride);
uint8_t *dst = fallbackBgra.data() + static_cast<size_t>(y) * static_cast<size_t>(w) * 4u;
for (int x = 0; x + 1 < w; x += 2)
{
const int y0 = src[x * 2 + 0];
const int u = src[x * 2 + 1];
const int y1 = src[x * 2 + 2];
const int v = src[x * 2 + 3];
uint8_t r0 = 0, g0 = 0, b0 = 0;
uint8_t r1 = 0, g1 = 0, b1 = 0;
yuv_to_rgb(y0, u, v, r0, g0, b0);
yuv_to_rgb(y1, u, v, r1, g1, b1);
dst[x * 4 + 0] = b0;
dst[x * 4 + 1] = g0;
dst[x * 4 + 2] = r0;
dst[x * 4 + 3] = 255;
dst[(x + 1) * 4 + 0] = b1;
dst[(x + 1) * 4 + 1] = g1;
dst[(x + 1) * 4 + 2] = r1;
dst[(x + 1) * 4 + 3] = 255;
}
}
gvfg_frame_t out{};
out.data = fallbackBgra.data();
out.stride = w * 4;
out.width = w;
out.height = h;
out.data = frame.data;
out.data_size = static_cast<uint64_t>(frame.data_size_bytes);
out.stride = static_cast<int>(frame.plane_stride_bytes[0]);
out.width = static_cast<int>(frame.width);
out.height = static_cast<int>(frame.height);
out.pixel_format = to_gvfg_pixel_format(frame.pixel_format);
out.bit_depth = static_cast<int>(frame.bit_depth);
out.plane_count = static_cast<int>((std::min)(frame.plane_count, static_cast<uint32_t>(GVFG_MAX_PLANES)));
for (int i = 0; i < out.plane_count; ++i)
{
out.plane_offset_bytes[i] = frame.plane_offset_bytes[i];
out.plane_stride_bytes[i] = frame.plane_stride_bytes[i];
}
out.pts_ns = frame.timestamp_ns ? frame.timestamp_ns : now_ns();
out.frame_id = frame.frame_id;
onFrame(&out, callbackUser);
noteDeliveredFrame(out.width, out.height);
noteDeliveredFrame(out.width, out.height, out.bit_depth, out.pixel_format);
}
bool shouldEmitFrameCallback(uint64_t frameId) const
@@ -888,13 +870,17 @@ struct gvfg_handle_t
deliveredFrames.store(0, std::memory_order_relaxed);
deliveredWidth.store(0, std::memory_order_relaxed);
deliveredHeight.store(0, std::memory_order_relaxed);
deliveredBitDepth.store(0, std::memory_order_relaxed);
deliveredPixelFormat.store(GVFG_PIXFMT_UNKNOWN, std::memory_order_relaxed);
runtimeFps.store(0.0, std::memory_order_relaxed);
}
void noteDeliveredFrame(int frameWidth, int frameHeight)
void noteDeliveredFrame(int frameWidth, int frameHeight, int bitDepth, int pixelFormat)
{
deliveredWidth.store(frameWidth > 0 ? static_cast<uint32_t>(frameWidth) : 0, std::memory_order_relaxed);
deliveredHeight.store(frameHeight > 0 ? static_cast<uint32_t>(frameHeight) : 0, std::memory_order_relaxed);
deliveredBitDepth.store(bitDepth > 0 ? static_cast<uint32_t>(bitDepth) : 0, std::memory_order_relaxed);
deliveredPixelFormat.store(pixelFormat, std::memory_order_relaxed);
deliveredFrames.fetch_add(1, std::memory_order_relaxed);
}
@@ -922,6 +908,8 @@ struct gvfg_handle_t
std::atomic<uint64_t> deliveredFrames{0};
std::atomic<uint32_t> deliveredWidth{0};
std::atomic<uint32_t> deliveredHeight{0};
std::atomic<uint32_t> deliveredBitDepth{0};
std::atomic<int> deliveredPixelFormat{GVFG_PIXFMT_UNKNOWN};
std::atomic<double> runtimeFps{0.0};
gvfg_on_frame_cb onFrame = nullptr;
@@ -931,8 +919,6 @@ struct gvfg_handle_t
gvfg_on_event_cb onEvent = nullptr;
void *eventCallbackUser = nullptr;
uint32_t eventMask = GVFG_EVENT_MASK_DEFAULT;
bool fallbackLogged = false;
std::vector<uint8_t> fallbackBgra;
std::atomic<bool> running{false};
std::thread captureThread;