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
+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)