Add standard v210 preview support

This commit is contained in:
Mark.lin
2026-07-16 11:05:37 +08:00
parent 9639a797aa
commit 1719053b3d
7 changed files with 139 additions and 3 deletions
@@ -25,6 +25,14 @@ static inline uint16_t normalize_y210_word_for_upload(uint16_t v)
return static_cast<uint16_t>((v >> 6) & 0x03FFu);
}
static inline uint32_t read_le32(const uint8_t *p)
{
return static_cast<uint32_t>(p[0]) |
(static_cast<uint32_t>(p[1]) << 8) |
(static_cast<uint32_t>(p[2]) << 16) |
(static_cast<uint32_t>(p[3]) << 24);
}
static const char *ss_dxgi_format_name(DXGI_FORMAT fmt)
{
switch (fmt)
@@ -1103,6 +1111,105 @@ bool D3DPreviewPipeline::upload_y210_frame(const uint8_t *data, int src_stride,
return true;
}
bool D3DPreviewPipeline::upload_v210_frame(const uint8_t *data, int src_stride, int frame_w, int frame_h)
{
if (!ctx_ || !d3d_ || !data || frame_w <= 0 || frame_h <= 0)
return false;
const int groupsPerRow = (frame_w + 5) / 6;
const int minStride = groupsPerRow * 16;
const int effectiveStride = (src_stride > 0) ? src_stride : minStride;
if (effectiveStride < minStride)
return false;
const int w2 = (frame_w + 1) / 2;
if (upload_y210_packed_)
{
D3D11_TEXTURE2D_DESC desc{};
upload_y210_packed_->GetDesc(&desc);
if ((int)desc.Width != w2 || (int)desc.Height != frame_h || desc.Format != DXGI_FORMAT_R16G16B16A16_UINT)
upload_y210_packed_.Reset();
}
if (!upload_y210_packed_)
{
D3D11_TEXTURE2D_DESC td{};
td.Width = (UINT)w2;
td.Height = (UINT)frame_h;
td.MipLevels = 1;
td.ArraySize = 1;
td.SampleDesc.Count = 1;
td.Format = DXGI_FORMAT_R16G16B16A16_UINT;
td.Usage = D3D11_USAGE_DYNAMIC;
td.BindFlags = D3D11_BIND_SHADER_RESOURCE;
td.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
if (FAILED(d3d_->CreateTexture2D(&td, nullptr, &upload_y210_packed_)))
return false;
}
D3D11_MAPPED_SUBRESOURCE m{};
if (FAILED(ctx_->Map(upload_y210_packed_.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &m)))
return false;
for (int row = 0; row < frame_h; ++row)
{
const uint8_t *srcRow = data + (size_t)row * (size_t)effectiveStride;
uint8_t *dstRow = static_cast<uint8_t *>(m.pData) + (size_t)row * (size_t)m.RowPitch;
uint16_t *dst16 = reinterpret_cast<uint16_t *>(dstRow);
for (int group = 0; group < groupsPerRow; ++group)
{
const uint8_t *p = srcRow + group * 16;
const uint32_t w0 = read_le32(p + 0);
const uint32_t w1 = read_le32(p + 4);
const uint32_t w2v = read_le32(p + 8);
const uint32_t w3 = read_le32(p + 12);
const uint16_t u01 = static_cast<uint16_t>((w0 >> 0) & 0x3ffu);
const uint16_t y0 = static_cast<uint16_t>((w0 >> 10) & 0x3ffu);
const uint16_t v01 = static_cast<uint16_t>((w0 >> 20) & 0x3ffu);
const uint16_t y1 = static_cast<uint16_t>((w1 >> 0) & 0x3ffu);
const uint16_t u23 = static_cast<uint16_t>((w1 >> 10) & 0x3ffu);
const uint16_t y2 = static_cast<uint16_t>((w1 >> 20) & 0x3ffu);
const uint16_t v23 = static_cast<uint16_t>((w2v >> 0) & 0x3ffu);
const uint16_t y3 = static_cast<uint16_t>((w2v >> 10) & 0x3ffu);
const uint16_t u45 = static_cast<uint16_t>((w2v >> 20) & 0x3ffu);
const uint16_t y4 = static_cast<uint16_t>((w3 >> 0) & 0x3ffu);
const uint16_t v45 = static_cast<uint16_t>((w3 >> 10) & 0x3ffu);
const uint16_t y5 = static_cast<uint16_t>((w3 >> 20) & 0x3ffu);
const int pair = group * 3;
if (pair < w2)
{
uint16_t *d4 = dst16 + pair * 4;
d4[0] = y0;
d4[1] = u01;
d4[2] = y1;
d4[3] = v01;
}
if (pair + 1 < w2)
{
uint16_t *d4 = dst16 + (pair + 1) * 4;
d4[0] = y2;
d4[1] = u23;
d4[2] = y3;
d4[3] = v23;
}
if (pair + 2 < w2)
{
uint16_t *d4 = dst16 + (pair + 2) * 4;
d4[0] = y4;
d4[1] = u45;
d4[2] = y5;
d4[3] = v45;
}
}
}
ctx_->Unmap(upload_y210_packed_.Get(), 0);
return true;
}
bool D3DPreviewPipeline::render_uploaded_yuv_to_fp16(gvfg_render_pixfmt_t fmt, int frame_w, int frame_h)
{
if (!ctx_ || !vs_ || !il_ || !vb_ || !rtv_fp16_ || !rt_fp16_ || frame_w <= 0 || frame_h <= 0)
@@ -70,6 +70,7 @@ public:
bool upload_p010_frame(const uint8_t *y, int stride_y, const uint8_t *uv, int stride_uv, int frame_w, int frame_h);
bool upload_yuy2_frame(const uint8_t *data, int src_stride, int frame_w, int frame_h);
bool upload_y210_frame(const uint8_t *data, int src_stride, int frame_w, int frame_h);
bool upload_v210_frame(const uint8_t *data, int src_stride, int frame_w, int frame_h);
bool render_uploaded_yuv_to_fp16(gvfg_render_pixfmt_t fmt, int frame_w, int frame_h);
bool copy_fp16_to_scene();
bool readback_to_frame(int frame_w, int frame_h, uint64_t pts_ns, uint64_t frame_id,
+10 -1
View File
@@ -65,6 +65,10 @@ public:
renderFmt = gvfg::internal::GVFG_RENDER_FMT_Y210;
stride = frame.width * 4;
break;
case GVFG_PIXFMT_V210:
renderFmt = gvfg::internal::GVFG_RENDER_FMT_Y210;
stride = ((frame.width + 5) / 6) * 16;
break;
default:
return false;
}
@@ -88,6 +92,9 @@ public:
case GVFG_PIXFMT_Y210:
uploaded = pipeline_->upload_y210_frame(base, stride, frame.width, frame.height);
break;
case GVFG_PIXFMT_V210:
uploaded = pipeline_->upload_v210_frame(base, stride, frame.width, frame.height);
break;
default:
return false;
}
@@ -377,7 +384,9 @@ extern "C"
return GVFG_PREVIEW_EINVAL;
if (!frame->data || frame->width <= 0 || frame->height <= 0)
return GVFG_PREVIEW_EINVAL;
if (frame->pixel_format != GVFG_PIXFMT_YUY2 && frame->pixel_format != GVFG_PIXFMT_Y210)
if (frame->pixel_format != GVFG_PIXFMT_YUY2 &&
frame->pixel_format != GVFG_PIXFMT_Y210 &&
frame->pixel_format != GVFG_PIXFMT_V210)
return GVFG_PREVIEW_ENOTSUP;
return handle->renderer.render(*frame) ? GVFG_PREVIEW_OK : GVFG_PREVIEW_ERENDER;
}
+2 -1
View File
@@ -101,6 +101,7 @@ typedef enum
GVFG_PIXFMT_P010 = 6,
GVFG_PIXFMT_Y210 = 7,
GVFG_PIXFMT_YUV444 = 8,
GVFG_PIXFMT_V210 = 9,
GVFG_PIXFMT_BGRA8 = 100
} gvfg_pixel_format_t;
@@ -132,7 +133,7 @@ typedef struct
int width; /* Width of the most recent frame returned by gvfg_read_frame(). */
int height; /* Height of the most recent frame returned by gvfg_read_frame(). */
int bit_depth; /* Bits per color channel of the frame buffer. */
char pixel_format[32]; /* Native frame buffer format, for example YUY2, Y210, NV12, or P010. */
char pixel_format[32]; /* Native frame buffer format, for example YUY2, Y210, V210, NV12, or P010. */
int valid; /* Non-zero while capture is running after at least one frame read. */
} gvfg_last_frame_info_t;
@@ -33,7 +33,8 @@ typedef enum pcies2mm_pixel_format_t
PCIES2MM_PIXFMT_NV12 = 5,
PCIES2MM_PIXFMT_P010 = 6,
PCIES2MM_PIXFMT_Y210 = 7,
PCIES2MM_PIXFMT_YUV444 = 8
PCIES2MM_PIXFMT_YUV444 = 8,
PCIES2MM_PIXFMT_V210 = 9
} pcies2mm_pixel_format_t;
typedef enum pcies2mm_stream_state_t
@@ -164,6 +164,8 @@ namespace
return PCIES2MM_PIXFMT_UYVY;
case fourcc('N', 'V', '1', '2'):
return PCIES2MM_PIXFMT_NV12;
case fourcc('v', '2', '1', '0'):
return PCIES2MM_PIXFMT_V210;
case fourcc('Y', '2', '1', '0'):
return PCIES2MM_PIXFMT_Y210;
case fourcc('P', '0', '1', '0'):
@@ -201,6 +203,7 @@ namespace
case PCIES2MM_PIXFMT_YUY2:
case PCIES2MM_PIXFMT_UYVY:
case PCIES2MM_PIXFMT_Y210:
case PCIES2MM_PIXFMT_V210:
default:
return 0;
}
@@ -212,6 +215,7 @@ namespace
{
case PCIES2MM_PIXFMT_P010:
case PCIES2MM_PIXFMT_Y210:
case PCIES2MM_PIXFMT_V210:
return 10;
default:
return 8;
@@ -232,6 +236,8 @@ namespace
return bitDepth > 8u ? pixels * 6u : pixels * 3u;
case PCIES2MM_PIXFMT_Y210:
return pixels * 4u;
case PCIES2MM_PIXFMT_V210:
return ((static_cast<size_t>(width) + 5u) / 6u) * 16u * static_cast<size_t>(height);
case PCIES2MM_PIXFMT_YUY2:
case PCIES2MM_PIXFMT_UYVY:
default:
@@ -450,6 +456,7 @@ namespace gvfg::internal
{
case PCIES2MM_PIXFMT_YUY2:
case PCIES2MM_PIXFMT_UYVY:
case PCIES2MM_PIXFMT_V210:
break;
default:
return fail(PCIES2MM_ENOTSUP, "configure_stream(pixel_format)", ERROR_NOT_SUPPORTED);
+10
View File
@@ -142,6 +142,8 @@ namespace
return GVFG_PIXFMT_Y210;
case PCIES2MM_PIXFMT_YUV444:
return GVFG_PIXFMT_YUV444;
case PCIES2MM_PIXFMT_V210:
return GVFG_PIXFMT_V210;
default:
return GVFG_PIXFMT_UNKNOWN;
}
@@ -167,6 +169,8 @@ namespace
return "Y210";
case GVFG_PIXFMT_YUV444:
return "YUV444";
case GVFG_PIXFMT_V210:
return "V210";
case GVFG_PIXFMT_BGRA8:
return "BGRA8";
default:
@@ -282,6 +286,12 @@ namespace
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_V210:
if (!checked_mul_u64((width + 5u) / 6u, 16u, 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) ||