Add HDMI audio input queries and split OSD into a helper in GVFG SDK 3.2

- Expose HDMI audio input information through the capture API and refresh the bundled Gigabyte library.
- Move OSD rendering into gvfg_osd.dll, connect channel OSD through the capture frame processor, and remove the experimental in-place zero-copy OSD path.
- Update playback integration, the customer API documentation, and the internal build guide.

Validation: git diff --cached --check passed. The MSVC Release SDK build passed. Capture hardware and runtime behavior were not tested.
This commit is contained in:
2026-11-25 09:35:49 +08:00
parent 7a7027391e
commit a47ab190bc
26 changed files with 1080 additions and 867 deletions
@@ -0,0 +1,656 @@
#include "gvfg_gpu_osd.h"
#include "gvfg_osd_core.h"
#include <d3d11_4.h>
#include <d3dcompiler.h>
#include <wrl/client.h>
#include <algorithm>
#include <array>
#include <chrono>
#include <cstdint>
#include <cstring>
#include <iterator>
#include <limits>
#include <mutex>
#include <utility>
using Microsoft::WRL::ComPtr;
namespace gvfg::internal
{
namespace
{
constexpr char kOsdBlendShader[] = R"(
Texture2D<uint4> sourceTexture : register(t0);
Texture2D<uint4> osdTexture : register(t1);
Texture2D<uint2> maskTexture : register(t2);
Texture2D<uint4> osdTexture1 : register(t3);
Texture2D<uint2> maskTexture1 : register(t4);
RWTexture2D<uint4> outputTexture : register(u0);
cbuffer BlendParameters : register(b0)
{
uint outputValueMask;
uint visibleSlots;
uint2 padding;
};
uint blendComponent(uint sourceValue, uint osdValue, uint alpha)
{
return ((sourceValue * (255u - alpha) + osdValue * alpha + 127u) / 255u) &
outputValueMask;
}
uint4 blendLayer(uint4 sourceValue, uint4 osdValue, uint2 maskValue)
{
uint pairAlpha = max(maskValue.x, maskValue.y);
uint4 result;
result.x = blendComponent(sourceValue.x, osdValue.x, maskValue.x);
result.y = blendComponent(sourceValue.y, osdValue.y, pairAlpha);
result.z = blendComponent(sourceValue.z, osdValue.z, maskValue.y);
result.w = blendComponent(sourceValue.w, osdValue.w, pairAlpha);
return result;
}
[numthreads(16, 16, 1)]
void main(uint3 id : SV_DispatchThreadID)
{
uint width = 0;
uint height = 0;
outputTexture.GetDimensions(width, height);
if (id.x >= width || id.y >= height)
return;
const uint3 location = uint3(id.xy, 0);
uint4 result = sourceTexture.Load(location);
if ((visibleSlots & 1u) != 0u)
result = blendLayer(result, osdTexture.Load(location), maskTexture.Load(location));
if ((visibleSlots & 2u) != 0u)
result = blendLayer(result, osdTexture1.Load(location), maskTexture1.Load(location));
outputTexture[id.xy] = result;
}
)";
bool copyRowsToMapped(const uint8_t *source, int sourceStride,
uint32_t rowBytes, uint32_t height,
const D3D11_MAPPED_SUBRESOURCE &mapped)
{
if (!source || sourceStride < 0 || static_cast<uint32_t>(sourceStride) < rowBytes ||
mapped.RowPitch < rowBytes)
return false;
if (static_cast<uint32_t>(sourceStride) == rowBytes && mapped.RowPitch == rowBytes)
{
std::memcpy(mapped.pData, source, static_cast<size_t>(rowBytes) * height);
return true;
}
for (uint32_t y = 0; y < height; ++y)
{
std::memcpy(static_cast<uint8_t *>(mapped.pData) + static_cast<size_t>(y) * mapped.RowPitch,
source + static_cast<size_t>(y) * static_cast<size_t>(sourceStride),
rowBytes);
}
return true;
}
struct OsdRegion
{
uint32_t left = 0;
uint32_t top = 0;
uint32_t width = 0;
uint32_t height = 0;
bool empty() const { return width == 0 || height == 0; }
};
OsdRegion findCoveredRegion(const std::vector<uint8_t> &mask,
uint32_t frameWidth, uint32_t frameHeight)
{
uint32_t left = frameWidth;
uint32_t top = frameHeight;
uint32_t right = 0;
uint32_t bottom = 0;
for (uint32_t row = 0; row < frameHeight; ++row)
{
const uint8_t *maskRow = mask.data() + static_cast<size_t>(row) * frameWidth;
for (uint32_t column = 0; column < frameWidth; column += 2u)
{
// Keep both luma samples and their shared U/V inside the region.
if (maskRow[column] == 0 && maskRow[column + 1u] == 0)
continue;
left = (std::min)(left, column);
right = (std::max)(right, column + 2u);
top = (std::min)(top, row);
bottom = row + 1u;
}
}
return right > left ? OsdRegion{left, top, right - left, bottom - top}
: OsdRegion{};
}
void copyFrameRows(const gvfg_frame_t &source, uint8_t *destination,
uint32_t rowBytes)
{
const auto *sourceData = static_cast<const uint8_t *>(source.data);
if (static_cast<uint32_t>(source.row_stride_bytes) == rowBytes)
{
std::memcpy(destination, sourceData,
static_cast<size_t>(rowBytes) * static_cast<size_t>(source.height));
return;
}
for (int row = 0; row < source.height; ++row)
{
std::memcpy(destination + static_cast<size_t>(row) * rowBytes,
sourceData + static_cast<size_t>(row) * source.row_stride_bytes,
rowBytes);
}
}
}
struct GpuOsdProcessor::Impl
{
struct Slot
{
bool configured = false;
bool visible = false;
bool dirty = true;
std::string text;
std::string fontFamily;
int32_t x = 0, y = 0;
uint32_t fontSize = 0, colorRgb = 0xffffffu;
uint32_t width = 0, height = 0;
int format = GVFG_PIXFMT_UNKNOWN;
OsdRegion region;
std::vector<uint8_t> yuv, mask;
ComPtr<ID3D11Texture2D> texture, maskTexture;
ComPtr<ID3D11ShaderResourceView> srv, maskSrv;
};
gvfg_status_t configure(uint32_t index, const gvfg_channel_osd_config_t &value)
{
if (index >= slots.size())
return GVFG_EINVAL;
uint32_t width = 0, height = 0;
const auto status = gvfg_measure_osd_text(&value, &width, &height);
if (status != GVFG_OK)
return status;
// Allocate copies before changing state so failures retain the old slot.
std::string text(value.text), family(value.font_family);
std::lock_guard<std::mutex> lock(mutex);
auto &slot = slots[index];
const bool changed = !slot.configured || slot.text != text || slot.fontFamily != family ||
slot.x != value.x || slot.y != value.y || slot.fontSize != value.font_size_px ||
slot.colorRgb != (value.color_rgb & 0x00ffffffu);
slot.text.swap(text);
slot.fontFamily.swap(family);
slot.x = value.x;
slot.y = value.y;
slot.fontSize = value.font_size_px;
slot.colorRgb = value.color_rgb & 0x00ffffffu;
slot.configured = slot.visible = true;
slot.dirty = slot.dirty || changed;
surfaceDirty = surfaceDirty || changed;
return GVFG_OK;
}
gvfg_status_t clear(uint32_t index)
{
if (index >= slots.size())
return GVFG_EINVAL;
std::lock_guard<std::mutex> lock(mutex);
slots[index] = Slot{};
surfaceDirty = true;
return GVFG_OK;
}
gvfg_status_t setVisible(uint32_t index, int visible)
{
if (index >= slots.size() || (visible != 0 && visible != 1))
return GVFG_EINVAL;
std::lock_guard<std::mutex> lock(mutex);
if (!slots[index].configured && visible)
return GVFG_ESTATE;
slots[index].visible = visible != 0;
// Keep rasterized surfaces and GPU textures intact.
return GVFG_OK;
}
bool ensureDevice()
{
if (device && context && shader && blendParameters)
return true;
UINT flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
#ifdef _DEBUG
flags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
const D3D_FEATURE_LEVEL levels[] = {
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
};
D3D_FEATURE_LEVEL actual{};
HRESULT hr = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr,
flags, levels, static_cast<UINT>(std::size(levels)),
D3D11_SDK_VERSION, &device, &actual, &context);
#ifdef _DEBUG
if (FAILED(hr))
{
flags &= ~D3D11_CREATE_DEVICE_DEBUG;
hr = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr,
flags, levels, static_cast<UINT>(std::size(levels)),
D3D11_SDK_VERSION, &device, &actual, &context);
}
#endif
if (FAILED(hr) || !device || !context)
return false;
ComPtr<ID3DBlob> bytecode;
ComPtr<ID3DBlob> errors;
hr = D3DCompile(kOsdBlendShader, sizeof(kOsdBlendShader) - 1,
"gvfg_osd_blend", nullptr, nullptr, "main", "cs_5_0",
D3DCOMPILE_ENABLE_STRICTNESS, 0, &bytecode, &errors);
if (FAILED(hr) || !bytecode)
return false;
if (FAILED(device->CreateComputeShader(bytecode->GetBufferPointer(),
bytecode->GetBufferSize(),
nullptr, &shader)))
return false;
D3D11_BUFFER_DESC parameterDesc{};
parameterDesc.ByteWidth = 16;
parameterDesc.Usage = D3D11_USAGE_DEFAULT;
parameterDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
return SUCCEEDED(device->CreateBuffer(&parameterDesc, nullptr, &blendParameters));
}
bool ensureTexture(ComPtr<ID3D11Texture2D> &texture,
uint32_t textureWidth, uint32_t textureHeight,
DXGI_FORMAT format, D3D11_USAGE usage,
UINT bindFlags, UINT cpuAccess)
{
if (texture)
{
D3D11_TEXTURE2D_DESC current{};
texture->GetDesc(&current);
if (current.Width == textureWidth && current.Height == textureHeight &&
current.Format == format && current.Usage == usage &&
current.BindFlags == bindFlags && current.CPUAccessFlags == cpuAccess)
return true;
texture.Reset();
}
D3D11_TEXTURE2D_DESC desc{};
desc.Width = textureWidth;
desc.Height = textureHeight;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = format;
desc.SampleDesc.Count = 1;
desc.Usage = usage;
desc.BindFlags = bindFlags;
desc.CPUAccessFlags = cpuAccess;
return SUCCEEDED(device->CreateTexture2D(&desc, nullptr, &texture));
}
bool ensureResources(uint32_t regionWidth, uint32_t regionHeight, int pixelFormat)
{
const DXGI_FORMAT packedFormat = pixelFormat == GVFG_PIXFMT_YUY2
? DXGI_FORMAT_R8G8B8A8_UINT : DXGI_FORMAT_R16G16B16A16_UINT;
const bool changed = resourceWidth != regionWidth || resourceHeight != regionHeight ||
resourceFormat != pixelFormat;
if (changed)
{
sourceSrv.Reset();
outputUav.Reset();
for (auto &slot : slots) { slot.srv.Reset(); slot.maskSrv.Reset(); }
resourceWidth = resourceHeight = 0;
resourceFormat = GVFG_PIXFMT_UNKNOWN;
}
const uint32_t pairWidth = regionWidth / 2u;
if (!ensureTexture(sourceTexture, pairWidth, regionHeight, packedFormat,
D3D11_USAGE_DYNAMIC, D3D11_BIND_SHADER_RESOURCE, D3D11_CPU_ACCESS_WRITE) ||
!ensureTexture(outputTexture, pairWidth, regionHeight, packedFormat,
D3D11_USAGE_DEFAULT, D3D11_BIND_UNORDERED_ACCESS, 0) ||
!ensureTexture(readbackTexture, pairWidth, regionHeight, packedFormat,
D3D11_USAGE_STAGING, 0, D3D11_CPU_ACCESS_READ))
return false;
for (auto &slot : slots)
{
if (!ensureTexture(slot.texture, pairWidth, regionHeight, packedFormat,
D3D11_USAGE_DYNAMIC, D3D11_BIND_SHADER_RESOURCE, D3D11_CPU_ACCESS_WRITE) ||
!ensureTexture(slot.maskTexture, pairWidth, regionHeight, DXGI_FORMAT_R8G8_UINT,
D3D11_USAGE_DYNAMIC, D3D11_BIND_SHADER_RESOURCE, D3D11_CPU_ACCESS_WRITE))
return false;
if ((!slot.srv && FAILED(device->CreateShaderResourceView(slot.texture.Get(), nullptr, &slot.srv))) ||
(!slot.maskSrv && FAILED(device->CreateShaderResourceView(slot.maskTexture.Get(), nullptr, &slot.maskSrv))))
return false;
}
if ((!sourceSrv && FAILED(device->CreateShaderResourceView(sourceTexture.Get(), nullptr, &sourceSrv))) ||
(!outputUav && FAILED(device->CreateUnorderedAccessView(outputTexture.Get(), nullptr, &outputUav))))
return false;
resourceWidth = regionWidth;
resourceHeight = regionHeight;
resourceFormat = pixelFormat;
return true;
}
bool rebuildSlot(Slot &slot, uint32_t width, uint32_t height, int format)
{
if (!slot.dirty && slot.width == width && slot.height == height && slot.format == format)
return true;
const uint32_t stride = width * (format == GVFG_PIXFMT_YUY2 ? 2u : 4u);
slot.yuv.assign(static_cast<size_t>(stride) * height, 0);
slot.mask.assign(static_cast<size_t>(width) * height, 0);
slot.region = {};
if (slot.configured)
{
gvfg_osd_handle raw = nullptr;
if (gvfg_osd_create(&raw) != GVFG_OSD_OK)
return false;
std::unique_ptr<void, decltype(&gvfg_osd_destroy)> helper(raw, &gvfg_osd_destroy);
uint32_t textWidth = 0, textHeight = 0;
const auto measured = gvfg_osd_measure(raw, slot.text.c_str(),
static_cast<uint32_t>(slot.text.size()), slot.fontSize, slot.fontFamily.c_str(),
&textWidth, &textHeight);
if (measured != GVFG_OSD_OK)
return false;
// Oversized text skips only this slot, preserving capture and the other slot.
if (textWidth <= width && textHeight <= height)
{
gvfg_osd_config_t config{};
config.struct_size = sizeof(config);
config.width = width;
config.height = height;
config.pixel_format = format == GVFG_PIXFMT_YUY2 ? GVFG_OSD_PIXFMT_YUY2 : GVFG_OSD_PIXFMT_Y210;
config.text = slot.text.c_str();
config.text_length = static_cast<uint32_t>(slot.text.size());
config.font_family = slot.fontFamily.c_str();
config.font_size_px = slot.fontSize;
config.color_rgb = slot.colorRgb;
config.x = slot.x;
config.y = slot.y;
gvfg_osd_output_t output{};
output.struct_size = sizeof(output);
output.yuv_data = slot.yuv.data();
output.yuv_size = slot.yuv.size();
output.yuv_stride = stride;
output.mask_data = slot.mask.data();
output.mask_size = slot.mask.size();
output.mask_stride = width;
if (gvfg_osd_render(raw, &config, &output) != GVFG_OSD_OK)
return false;
slot.region = findCoveredRegion(slot.mask, width, height);
}
}
slot.width = width;
slot.height = height;
slot.format = format;
slot.dirty = false;
return true;
}
bool rebuildSurface(uint32_t width, uint32_t height, int format)
{
surfaceDirty = true; // Retry partial failures, including upload failures.
region = {};
for (auto &slot : slots)
{
if (!rebuildSlot(slot, width, height, format))
return false;
if (slot.region.empty())
continue;
if (region.empty())
region = slot.region;
else
{
const uint32_t right = (std::max)(region.left + region.width, slot.region.left + slot.region.width);
const uint32_t bottom = (std::max)(region.top + region.height, slot.region.top + slot.region.height);
region.left = (std::min)(region.left, slot.region.left);
region.top = (std::min)(region.top, slot.region.top);
region.width = right - region.left;
region.height = bottom - region.top;
}
}
// Include hidden slots in the shared ROI so show/hide never rerasterizes
// glyphs or recreates textures. Visibility is a shader constant.
if (!region.empty())
{
if (!ensureDevice() || !ensureResources(region.width, region.height, format))
return false;
const uint32_t bpp = format == GVFG_PIXFMT_YUY2 ? 2u : 4u;
const size_t yuvOffset = (static_cast<size_t>(region.top) * width + region.left) * bpp;
const size_t maskOffset = static_cast<size_t>(region.top) * width + region.left;
for (auto &slot : slots)
{
D3D11_MAPPED_SUBRESOURCE mapped{};
if (FAILED(context->Map(slot.texture.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)))
return false;
const bool yuvOk = copyRowsToMapped(slot.yuv.data() + yuvOffset,
static_cast<int>(width * bpp), region.width * bpp, region.height, mapped);
context->Unmap(slot.texture.Get(), 0);
if (!yuvOk || FAILED(context->Map(slot.maskTexture.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)))
return false;
const bool maskOk = copyRowsToMapped(slot.mask.data() + maskOffset,
static_cast<int>(width), region.width, region.height, mapped);
context->Unmap(slot.maskTexture.Get(), 0);
if (!maskOk)
return false;
}
}
surfaceWidth = width;
surfaceHeight = height;
surfaceFormat = format;
surfaceDirty = false;
return true;
}
gvfg_status_t process(const gvfg_frame_t &source,
std::vector<uint8_t> &destination,
int &destinationStride,
bool &applied,
gvfg_debug_osd_frame_timing_t *timing,
uint8_t *writableSource,
bool *inPlaceApplied)
{
if (inPlaceApplied)
*inPlaceApplied = false;
using Clock = std::chrono::steady_clock;
auto stageStart = timing ? Clock::now() : Clock::time_point{};
const auto finishStage = [timing, &stageStart](gvfg_debug_osd_stage_t stage) {
if (timing)
{
const auto end = Clock::now();
timing->stage_ms[stage] = std::chrono::duration<double, std::milli>(end - stageStart).count();
stageStart = end;
}
};
std::lock_guard<std::mutex> lock(mutex);
finishStage(GVFG_DEBUG_OSD_LOCK_WAIT);
applied = false;
uint32_t visibleSlots = 0;
for (uint32_t i = 0; i < slots.size(); ++i)
if (slots[i].configured && slots[i].visible)
visibleSlots |= 1u << i;
if (!visibleSlots)
return GVFG_OK;
if (!source.data || source.width <= 0 || source.height <= 0 ||
(source.width & 1) != 0 || source.row_stride_bytes <= 0 ||
(source.pixel_format != GVFG_PIXFMT_YUY2 &&
source.pixel_format != GVFG_PIXFMT_Y210))
return GVFG_EINVAL;
const uint64_t rowBytes64 = static_cast<uint64_t>(source.width) *
(source.pixel_format == GVFG_PIXFMT_YUY2 ? 2u : 4u);
if (rowBytes64 > static_cast<uint64_t>((std::numeric_limits<int>::max)()) ||
static_cast<uint64_t>(source.row_stride_bytes) < rowBytes64)
return GVFG_EINVAL;
const uint64_t requiredSource =
static_cast<uint64_t>(source.row_stride_bytes) *
static_cast<uint64_t>(source.height - 1) +
rowBytes64;
if (source.data_size < requiredSource)
return GVFG_EINVAL;
if (writableSource && static_cast<const void *>(writableSource) != source.data)
return GVFG_EINVAL;
const bool rebuild = surfaceDirty || surfaceWidth != static_cast<uint32_t>(source.width) ||
surfaceHeight != static_cast<uint32_t>(source.height) ||
surfaceFormat != source.pixel_format;
if (timing)
timing->surface_rebuilt = rebuild ? 1 : 0;
if (rebuild &&
!rebuildSurface(static_cast<uint32_t>(source.width),
static_cast<uint32_t>(source.height), source.pixel_format))
return GVFG_EIO;
finishStage(GVFG_DEBUG_OSD_SURFACE);
// Invisible text needs no processed buffer or GPU work.
bool hasVisibleInk = false;
for (const auto &slot : slots)
hasVisibleInk = hasVisibleInk || (slot.visible && !slot.region.empty());
if (region.empty() || !hasVisibleInk)
return GVFG_OK;
const uint32_t rowBytes = static_cast<uint32_t>(rowBytes64);
const uint32_t bytesPerPixel = source.pixel_format == GVFG_PIXFMT_YUY2 ? 2u : 4u;
const uint32_t regionRowBytes = region.width * bytesPerPixel;
const uint64_t sourceOffset64 = static_cast<uint64_t>(region.top) * source.row_stride_bytes +
static_cast<uint64_t>(region.left) * bytesPerPixel;
if (sourceOffset64 > (std::numeric_limits<size_t>::max)())
return GVFG_EINVAL;
const size_t sourceOffset = static_cast<size_t>(sourceOffset64);
if (!writableSource)
destination.resize(static_cast<size_t>(rowBytes) * static_cast<size_t>(source.height));
uint8_t *outputData = writableSource ? writableSource : destination.data();
const uint32_t outputStride = writableSource ? static_cast<uint32_t>(source.row_stride_bytes) : rowBytes;
if (timing)
{
timing->roi_width = region.width;
timing->roi_height = region.height;
}
finishStage(GVFG_DEBUG_OSD_PREPARE);
D3D11_MAPPED_SUBRESOURCE mapped{};
if (FAILED(context->Map(sourceTexture.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)))
return GVFG_EIO;
const bool copiedSource = copyRowsToMapped(
static_cast<const uint8_t *>(source.data) + sourceOffset,
source.row_stride_bytes, regionRowBytes, region.height, mapped);
context->Unmap(sourceTexture.Get(), 0);
if (!copiedSource)
return GVFG_EIO;
finishStage(GVFG_DEBUG_OSD_UPLOAD);
const uint32_t blendValues[4] = {
source.pixel_format == GVFG_PIXFMT_Y210 ? 0xffc0u : 0x00ffu, visibleSlots, 0u, 0u};
context->UpdateSubresource(blendParameters.Get(), 0, nullptr, blendValues, 0, 0);
ID3D11ShaderResourceView *srvs[] = {sourceSrv.Get(), slots[0].srv.Get(), slots[0].maskSrv.Get(),
slots[1].srv.Get(), slots[1].maskSrv.Get()};
ID3D11UnorderedAccessView *uavs[] = {outputUav.Get()};
context->CSSetShader(shader.Get(), nullptr, 0);
ID3D11Buffer *constantBuffers[] = {blendParameters.Get()};
context->CSSetConstantBuffers(0, 1, constantBuffers);
context->CSSetShaderResources(0, 5, srvs);
context->CSSetUnorderedAccessViews(0, 1, uavs, nullptr);
context->Dispatch((region.width / 2u + 15u) / 16u,
(region.height + 15u) / 16u, 1);
ID3D11ShaderResourceView *nullSrvs[] = {nullptr, nullptr, nullptr, nullptr, nullptr};
ID3D11UnorderedAccessView *nullUav = nullptr;
context->CSSetShaderResources(0, 5, nullSrvs);
context->CSSetUnorderedAccessViews(0, 1, &nullUav, nullptr);
ID3D11Buffer *nullBuffer = nullptr;
context->CSSetConstantBuffers(0, 1, &nullBuffer);
context->CSSetShader(nullptr, nullptr, 0);
context->CopyResource(readbackTexture.Get(), outputTexture.Get());
finishStage(GVFG_DEBUG_OSD_SUBMIT);
// Driver zero-copy input requires an independent frame by default.
// SDK-owned copy input already contains the untouched full frame, so
// only its ROI is overwritten after GPU readback; no full copy/resize.
if (!writableSource)
{
copyFrameRows(source, outputData, rowBytes);
finishStage(GVFG_DEBUG_OSD_FRAME_COPY);
}
if (FAILED(context->Map(readbackTexture.Get(), 0, D3D11_MAP_READ, 0, &mapped)))
return GVFG_EIO;
finishStage(GVFG_DEBUG_OSD_READBACK_WAIT);
if (mapped.RowPitch < regionRowBytes)
{
context->Unmap(readbackTexture.Get(), 0);
return GVFG_EIO;
}
const size_t destinationOffset = static_cast<size_t>(region.top) * outputStride +
static_cast<size_t>(region.left) * bytesPerPixel;
for (uint32_t row = 0; row < region.height; ++row)
{
std::memcpy(outputData + destinationOffset + static_cast<size_t>(row) * outputStride,
static_cast<const uint8_t *>(mapped.pData) +
static_cast<size_t>(row) * mapped.RowPitch,
regionRowBytes);
}
context->Unmap(readbackTexture.Get(), 0);
finishStage(GVFG_DEBUG_OSD_ROI_COPY);
destinationStride = static_cast<int>(outputStride);
applied = true;
if (inPlaceApplied)
*inPlaceApplied = writableSource != nullptr;
if (timing)
timing->osd_applied = 1;
return GVFG_OK;
}
mutable std::mutex mutex;
std::array<Slot, GVFG_OSD_SLOT_COUNT> slots;
bool surfaceDirty = true;
OsdRegion region;
uint32_t surfaceWidth = 0;
uint32_t surfaceHeight = 0;
int surfaceFormat = GVFG_PIXFMT_UNKNOWN;
uint32_t resourceWidth = 0;
uint32_t resourceHeight = 0;
int resourceFormat = GVFG_PIXFMT_UNKNOWN;
ComPtr<ID3D11Device> device;
ComPtr<ID3D11DeviceContext> context;
ComPtr<ID3D11ComputeShader> shader;
ComPtr<ID3D11Buffer> blendParameters;
ComPtr<ID3D11Texture2D> sourceTexture;
ComPtr<ID3D11Texture2D> outputTexture;
ComPtr<ID3D11Texture2D> readbackTexture;
ComPtr<ID3D11ShaderResourceView> sourceSrv;
ComPtr<ID3D11UnorderedAccessView> outputUav;
};
GpuOsdProcessor::GpuOsdProcessor() : impl_(std::make_unique<Impl>()) {}
GpuOsdProcessor::~GpuOsdProcessor() = default;
gvfg_status_t GpuOsdProcessor::configure(uint32_t index, const gvfg_channel_osd_config_t &config)
{
try
{
return impl_->configure(index, config);
}
catch (...)
{
return GVFG_EIO;
}
}
gvfg_status_t GpuOsdProcessor::clear(uint32_t index) { return impl_->clear(index); }
gvfg_status_t GpuOsdProcessor::setVisible(uint32_t index, int visible) { return impl_->setVisible(index, visible); }
gvfg_status_t GpuOsdProcessor::process(const gvfg_frame_t &source,
std::vector<uint8_t> &destination,
int &destinationStride,
bool &applied,
gvfg_debug_osd_frame_timing_t *timing,
uint8_t *writableSource,
bool *inPlaceApplied)
{
try
{
return impl_->process(source, destination, destinationStride, applied, timing, writableSource, inPlaceApplied);
}
catch (...)
{
applied = false;
if (inPlaceApplied)
*inPlaceApplied = false;
destinationStride = 0;
return GVFG_EIO;
}
}
}
@@ -0,0 +1,36 @@
#pragma once
#include "gvfg_osd.h"
#include "gvfg_debug.h"
#include <memory>
#include <string>
#include <vector>
namespace gvfg::internal
{
class GpuOsdProcessor
{
public:
GpuOsdProcessor();
~GpuOsdProcessor();
gvfg_status_t configure(uint32_t index, const gvfg_channel_osd_config_t &config);
gvfg_status_t clear(uint32_t index);
gvfg_status_t setVisible(uint32_t index, int visible);
// writableSource must equal source.data. Only the backend can grant this
// writable SDK-owned storage, held until the caller releases the frame.
// nullptr preserves the separate-output path used for driver zero-copy.
gvfg_status_t process(const gvfg_frame_t &source,
std::vector<uint8_t> &destination,
int &destinationStride,
bool &applied,
gvfg_debug_osd_frame_timing_t *timing = nullptr,
uint8_t *writableSource = nullptr,
bool *inPlaceApplied = nullptr);
private:
struct Impl;
std::unique_ptr<Impl> impl_;
};
}
+1 -1
View File
@@ -1,4 +1,4 @@
#include "gvfg_osd.h"
#include "gvfg_osd_core.h"
#include <d2d1.h>
#include <dwrite.h>
@@ -0,0 +1,240 @@
#include "gvfg_osd.h"
#include "gvfg_osd_core.h"
#include "gvfg_gpu_osd.h"
#include "gvfg_frame_processor.h"
#include <algorithm>
#include <climits>
#include <cstring>
#include <limits>
#include <memory>
#include <new>
#include <vector>
namespace
{
struct OsdFrameProcessor
{
gvfg::internal::GpuOsdProcessor gpu;
std::vector<uint8_t> pixels;
};
const gvfg_frame_processor processor{
[](void **context) -> gvfg_status_t {
try
{
*context = new OsdFrameProcessor;
return GVFG_OK;
}
catch (const std::bad_alloc &)
{
return GVFG_EIO;
}
},
[](void *context) { delete static_cast<OsdFrameProcessor *>(context); },
[](void *context, const gvfg_frame_t &source, gvfg_frame_t &output,
bool &applied, gvfg_debug_osd_frame_timing_t *timing, uint8_t *writable,
bool *inPlace) {
auto &state = *static_cast<OsdFrameProcessor *>(context);
int stride = 0;
const auto status = state.gpu.process(source, state.pixels, stride, applied, timing,
writable, inPlace);
if (status == GVFG_OK && applied && !*inPlace)
{
output.data = state.pixels.data();
output.data_size = state.pixels.size();
output.row_stride_bytes = stride;
}
return status;
}
};
struct SlotCommand
{
uint32_t index;
const gvfg_channel_osd_config_t *config;
int visible;
};
}
extern "C"
{
gvfg_status_t gvfg_get_osd_fonts(gvfg_osd_font_list_t *out_list)
{
if (!out_list)
return GVFG_EINVAL;
*out_list = {};
const auto status = gvfg_osd_get_fonts(&out_list->count, &out_list->names);
return status == GVFG_OSD_OK ? GVFG_OK : GVFG_EIO;
}
gvfg_status_t gvfg_measure_osd_text(const gvfg_channel_osd_config_t *config,
uint32_t *out_width, uint32_t *out_height)
{
if (out_width) *out_width = 0;
if (out_height) *out_height = 0;
if (!config || !config->text || !out_width || !out_height)
return GVFG_EINVAL;
const size_t length = strnlen_s(config->text, GVFG_OSD_MAX_TEXT_CHARACTERS + 1u);
if (length == 0 || length > GVFG_OSD_MAX_TEXT_CHARACTERS)
return GVFG_EINVAL;
try
{
gvfg_osd_handle rawHandle = nullptr;
if (gvfg_osd_create(&rawHandle) != GVFG_OSD_OK)
return GVFG_EIO;
const std::unique_ptr<void, decltype(&gvfg_osd_destroy)> helper(rawHandle, &gvfg_osd_destroy);
const auto status = gvfg_osd_measure(helper.get(), config->text,
static_cast<uint32_t>(length), config->font_size_px, config->font_family,
out_width, out_height);
return status == GVFG_OSD_OK ? GVFG_OK :
status == GVFG_OSD_EINVAL ? GVFG_EINVAL : GVFG_EIO;
}
catch (...) { return GVFG_EIO; }
}
static gvfg_status_t renderOsdBgraTile(const gvfg_channel_osd_config_t *config,
gvfg_osd_bitmap_t *out_bitmap,
std::vector<uint8_t> &pixels)
{
if (!config || !config->text)
return GVFG_EINVAL;
const size_t length = strnlen_s(config->text, GVFG_OSD_MAX_TEXT_CHARACTERS + 1u);
if (length == 0 || length > GVFG_OSD_MAX_TEXT_CHARACTERS)
return GVFG_EINVAL;
try
{
gvfg_osd_handle rawHandle = nullptr;
if (gvfg_osd_create(&rawHandle) != GVFG_OSD_OK)
return GVFG_EIO;
const std::unique_ptr<void, decltype(&gvfg_osd_destroy)> helper(rawHandle, &gvfg_osd_destroy);
uint32_t width = 0, height = 0;
auto status = gvfg_osd_measure(helper.get(), config->text,
static_cast<uint32_t>(length), config->font_size_px, config->font_family,
&width, &height);
if (status != GVFG_OSD_OK)
return status == GVFG_OSD_EINVAL ? GVFG_EINVAL : GVFG_EIO;
const uint64_t stride = static_cast<uint64_t>(width) * 4u;
// WIC GetDataPointer reports its byte count as UINT.
if (width > INT_MAX || height > INT_MAX || stride > UINT_MAX ||
stride * height > UINT_MAX ||
stride * height > (std::numeric_limits<size_t>::max)())
return GVFG_EINVAL;
pixels.resize(static_cast<size_t>(stride * height));
gvfg_osd_config_t render{};
render.struct_size = sizeof(render);
render.width = width;
render.height = height;
render.text = config->text;
render.font_family = config->font_family;
render.text_length = static_cast<uint32_t>(length);
render.font_size_px = config->font_size_px;
render.color_rgb = config->color_rgb;
status = gvfg_osd_render_bgra(helper.get(), &render, pixels.data(), pixels.size(),
static_cast<uint32_t>(stride));
if (status != GVFG_OSD_OK)
return status == GVFG_OSD_EINVAL ? GVFG_EINVAL : GVFG_EIO;
out_bitmap->data = pixels.data();
out_bitmap->data_size = pixels.size();
out_bitmap->width = width;
out_bitmap->height = height;
out_bitmap->stride = static_cast<uint32_t>(stride);
return GVFG_OK;
}
catch (...) { return GVFG_EIO; }
}
gvfg_status_t gvfg_render_osd_frame_bgra(uint32_t video_width, uint32_t video_height,
const gvfg_channel_osd_config_t *slot0,
const gvfg_channel_osd_config_t *slot1,
gvfg_osd_bitmap_t *out_bitmap)
{
thread_local std::vector<uint8_t> frame;
frame.clear();
if (!out_bitmap)
return GVFG_EINVAL;
*out_bitmap = {};
const uint64_t stride = static_cast<uint64_t>(video_width) * 4u;
if (!video_width || !video_height || stride > UINT32_MAX ||
stride * video_height > UINT32_MAX ||
stride * video_height > (std::numeric_limits<size_t>::max)())
return GVFG_EINVAL;
try
{
frame.assign(static_cast<size_t>(stride * video_height), 0);
std::vector<uint8_t> tilePixels;
for (const auto *slot : {slot0, slot1})
{
if (!slot)
continue;
gvfg_osd_bitmap_t tile{};
const auto status = renderOsdBgraTile(slot, &tile, tilePixels);
if (status != GVFG_OK)
return status;
if (tile.width > video_width || tile.height > video_height)
continue;
const size_t x = std::clamp<int64_t>(slot->x, 0, video_width - tile.width);
const size_t y = std::clamp<int64_t>(slot->y, 0, video_height - tile.height);
for (uint32_t row = 0; row < tile.height; ++row)
{
const auto *src = tile.data + static_cast<size_t>(row) * tile.stride;
auto *dst = frame.data() + (y + row) * static_cast<size_t>(stride) + x * 4u;
for (uint32_t col = 0; col < tile.width; ++col, src += 4, dst += 4)
{
if (!src[3])
continue;
// Both layers are premultiplied, including at glyph edges.
const unsigned remaining = 255u - src[3];
for (unsigned c = 0; c < 4; ++c)
dst[c] = static_cast<uint8_t>(src[c] + (dst[c] * remaining + 127u) / 255u);
}
}
}
*out_bitmap = {frame.data(), frame.size(), video_width, video_height,
static_cast<uint32_t>(stride)};
return GVFG_OK;
}
catch (...) { return GVFG_EIO; }
}
gvfg_status_t gvfg_set_channel_osd(gvfg_handle handle, int channel_index,
uint32_t index, const gvfg_channel_osd_config_t *config)
{
if (!config || index >= GVFG_OSD_SLOT_COUNT)
return gvfg_control_frame_processor(handle, channel_index, &processor, false,
nullptr, nullptr, "OSD config or index is invalid");
const SlotCommand command{index, config, 0};
return gvfg_control_frame_processor(handle, channel_index, &processor, true,
[](void *context, const void *argument) {
const auto &slot = *static_cast<const SlotCommand *>(argument);
return static_cast<OsdFrameProcessor *>(context)->gpu.configure(slot.index, *slot.config);
}, &command, "Could not configure OSD slot");
}
gvfg_status_t gvfg_set_channel_osd_visible(gvfg_handle handle, int channel_index,
uint32_t index, int visible)
{
if (index >= GVFG_OSD_SLOT_COUNT || (visible != 0 && visible != 1))
return gvfg_control_frame_processor(handle, channel_index, &processor, false,
nullptr, nullptr, "OSD index or visibility is invalid");
const SlotCommand command{index, nullptr, visible};
return gvfg_control_frame_processor(handle, channel_index, &processor, true,
[](void *context, const void *argument) {
const auto &slot = *static_cast<const SlotCommand *>(argument);
return static_cast<OsdFrameProcessor *>(context)->gpu.setVisible(slot.index, slot.visible);
}, &command, "Could not change OSD visibility");
}
gvfg_status_t gvfg_clear_channel_osd(gvfg_handle handle, int channel_index, uint32_t index)
{
if (index >= GVFG_OSD_SLOT_COUNT)
return gvfg_control_frame_processor(handle, channel_index, &processor, false,
nullptr, nullptr, "OSD index is invalid");
return gvfg_control_frame_processor(handle, channel_index, &processor, false,
[](void *context, const void *argument) {
return static_cast<OsdFrameProcessor *>(context)->gpu.clear(*static_cast<const uint32_t *>(argument));
}, &index, "Could not clear OSD slot");
}
}
@@ -0,0 +1,106 @@
#ifndef GVFG_OSD_H
#define GVFG_OSD_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef GVFG_OSD_MAX_TEXT_CHARACTERS
#define GVFG_OSD_MAX_TEXT_CHARACTERS 32u
#endif
typedef void *gvfg_osd_handle;
typedef enum gvfg_osd_status_t
{
GVFG_OSD_OK = 0,
GVFG_OSD_EINVAL = -1,
GVFG_OSD_ENOMEM = -2,
GVFG_OSD_ERENDER = -3
} gvfg_osd_status_t;
typedef enum gvfg_osd_pixel_format_t
{
GVFG_OSD_PIXFMT_YUY2 = 1,
GVFG_OSD_PIXFMT_Y210 = 2
} gvfg_osd_pixel_format_t;
/*
* Generates an off-line OSD surface using the requested installed system font. Text is
* restricted to printable ASCII and at most GVFG_OSD_MAX_TEXT_CHARACTERS
* characters on one line without wrapping. color_rgb is 0xRRGGBB.
* x/y identify the top-left of the conservative measured text box (including
* glyph overhang and a one-pixel guard). They are clamped to keep that box
* inside the frame. Rendering returns EINVAL if the box cannot fit the frame.
* Output YUV uses BT.709 limited range. The mask is one coverage byte per
* pixel: 0 is transparent and 255 is fully covered.
*/
typedef struct gvfg_osd_config_t
{
uint32_t struct_size;
uint32_t width;
uint32_t height;
uint32_t pixel_format;
const char *text;
const char *font_family; /* UTF-8, NUL-terminated. */
uint32_t text_length;
int32_t x;
int32_t y;
uint32_t font_size_px;
uint32_t color_rgb;
} gvfg_osd_config_t;
/* All buffers are caller-owned and are used only for the duration of render. */
typedef struct gvfg_osd_output_t
{
uint32_t struct_size;
void *yuv_data;
uint64_t yuv_size;
uint32_t yuv_stride;
void *mask_data;
uint64_t mask_size;
uint32_t mask_stride;
} gvfg_osd_output_t;
gvfg_osd_status_t gvfg_osd_create(
gvfg_osd_handle *out_handle);
void gvfg_osd_destroy(
gvfg_osd_handle handle);
/* Conservative whole-text bounds in pixels, not pixel-tight raster bounds. */
gvfg_osd_status_t gvfg_osd_measure(
gvfg_osd_handle handle,
const char *text,
uint32_t text_length,
uint32_t font_size_px,
const char *font_family,
uint32_t *out_width,
uint32_t *out_height);
/* SDK-owned snapshot, invalidated by the next call. Serialize call/read access. */
gvfg_osd_status_t gvfg_osd_get_fonts(uint32_t *out_count, const char *const **out_names);
gvfg_osd_status_t gvfg_osd_render(
gvfg_osd_handle handle,
const gvfg_osd_config_t *config,
const gvfg_osd_output_t *output);
/* Caller-owned, top-down BGRA8888 with premultiplied alpha. Ignores pixel_format.
* Unlike YUV output, width may be odd. No RGB/YUV conversion is performed.
*/
gvfg_osd_status_t gvfg_osd_render_bgra(
gvfg_osd_handle handle,
const gvfg_osd_config_t *config,
void *data, uint64_t data_size, uint32_t stride);
const char *gvfg_osd_strerror(
gvfg_osd_status_t status);
#ifdef __cplusplus
}
#endif
#endif