Use XDMA ring slots for frame delivery
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
build*/
|
build*/
|
||||||
out/
|
out/
|
||||||
|
demosrc/
|
||||||
.vs/
|
.vs/
|
||||||
.vscode/
|
.vscode/
|
||||||
.qtcreator/
|
.qtcreator/
|
||||||
|
|||||||
@@ -190,15 +190,10 @@ typedef struct
|
|||||||
{
|
{
|
||||||
const void *data; /* Native frame buffer. Valid only during the frame callback. */
|
const void *data; /* Native frame buffer. Valid only during the frame callback. */
|
||||||
uint64_t data_size; /* Total bytes available from data. */
|
uint64_t data_size; /* Total bytes available from data. */
|
||||||
int stride; /* Bytes per row for plane 0. */
|
|
||||||
int width; /* Frame width in pixels. */
|
int width; /* Frame width in pixels. */
|
||||||
int height; /* Frame height in pixels. */
|
int height; /* Frame height in pixels. */
|
||||||
int pixel_format; /* gvfg_pixel_format_t value. */
|
int pixel_format; /* gvfg_pixel_format_t value. */
|
||||||
int bit_depth; /* Bits per color channel of the native frame. */
|
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. */
|
uint64_t frame_id; /* Monotonic frame identifier from the backend. */
|
||||||
} gvfg_frame_t;
|
} gvfg_frame_t;
|
||||||
|
|
||||||
|
|||||||
@@ -36,13 +36,6 @@ typedef enum xdma_pixel_format_t
|
|||||||
XDMA_PIXFMT_YUV444 = 8
|
XDMA_PIXFMT_YUV444 = 8
|
||||||
} xdma_pixel_format_t;
|
} xdma_pixel_format_t;
|
||||||
|
|
||||||
typedef enum xdma_memory_kind_t
|
|
||||||
{
|
|
||||||
XDMA_MEMORY_DRIVER_COPY = 0,
|
|
||||||
XDMA_MEMORY_SHARED_SECTION = 1,
|
|
||||||
XDMA_MEMORY_DMA_RING = 2
|
|
||||||
} xdma_memory_kind_t;
|
|
||||||
|
|
||||||
typedef enum xdma_stream_state_t
|
typedef enum xdma_stream_state_t
|
||||||
{
|
{
|
||||||
XDMA_STREAM_STOPPED = 0,
|
XDMA_STREAM_STOPPED = 0,
|
||||||
@@ -57,7 +50,6 @@ typedef struct xdma_stream_desc_t
|
|||||||
uint32_t height;
|
uint32_t height;
|
||||||
xdma_pixel_format_t pixel_format;
|
xdma_pixel_format_t pixel_format;
|
||||||
uint32_t buffer_count;
|
uint32_t buffer_count;
|
||||||
xdma_memory_kind_t memory_kind;
|
|
||||||
uint32_t flags;
|
uint32_t flags;
|
||||||
} xdma_stream_desc_t;
|
} xdma_stream_desc_t;
|
||||||
|
|
||||||
@@ -85,16 +77,10 @@ typedef struct xdma_frame_t
|
|||||||
const void *data; /* Valid until the next XdmaCaptureSession::wait_frame() or close(). */
|
const void *data; /* Valid until the next XdmaCaptureSession::wait_frame() or close(). */
|
||||||
size_t data_size_bytes;
|
size_t data_size_bytes;
|
||||||
uint64_t frame_id;
|
uint64_t frame_id;
|
||||||
uint64_t timestamp_ns;
|
|
||||||
uint32_t width;
|
uint32_t width;
|
||||||
uint32_t height;
|
uint32_t height;
|
||||||
xdma_pixel_format_t pixel_format;
|
xdma_pixel_format_t pixel_format;
|
||||||
uint32_t bit_depth;
|
uint32_t bit_depth;
|
||||||
uint32_t plane_count;
|
|
||||||
uint32_t plane_offset_bytes[XDMA_MAX_PLANES];
|
|
||||||
uint32_t plane_stride_bytes[XDMA_MAX_PLANES];
|
|
||||||
uint32_t driver_buffer_index;
|
|
||||||
uint32_t flags;
|
|
||||||
} xdma_frame_t;
|
} xdma_frame_t;
|
||||||
|
|
||||||
typedef struct xdma_stream_stats_t
|
typedef struct xdma_stream_stats_t
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ namespace
|
|||||||
constexpr int kPlugInScanFrames = 5;
|
constexpr int kPlugInScanFrames = 5;
|
||||||
constexpr uint32_t kDefaultWidth = 1920;
|
constexpr uint32_t kDefaultWidth = 1920;
|
||||||
constexpr uint32_t kDefaultHeight = 1080;
|
constexpr uint32_t kDefaultHeight = 1080;
|
||||||
|
constexpr uint32_t kDefaultRingBufferCount = 3;
|
||||||
|
constexpr uint32_t kMaxRingBufferCount = 16;
|
||||||
constexpr DWORD kMaxBytesPerTransfer = 0x800000;
|
constexpr DWORD kMaxBytesPerTransfer = 0x800000;
|
||||||
|
|
||||||
// FPGA user BAR register offsets. These match the old CaptureDemo
|
// FPGA user BAR register offsets. These match the old CaptureDemo
|
||||||
@@ -155,31 +157,6 @@ namespace
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t plane_count_for_pixfmt(xdma_pixel_format_t fmt)
|
|
||||||
{
|
|
||||||
return (fmt == XDMA_PIXFMT_NV12 || fmt == XDMA_PIXFMT_P010) ? 2u : 1u;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint32_t plane_stride_for_pixfmt(uint32_t width, xdma_pixel_format_t fmt, uint32_t bitDepth)
|
|
||||||
{
|
|
||||||
switch (fmt)
|
|
||||||
{
|
|
||||||
case XDMA_PIXFMT_NV12:
|
|
||||||
return width;
|
|
||||||
case XDMA_PIXFMT_P010:
|
|
||||||
return width * 2u;
|
|
||||||
case XDMA_PIXFMT_RGB24:
|
|
||||||
case XDMA_PIXFMT_YUV444:
|
|
||||||
return bitDepth > 8u ? width * 6u : width * 3u;
|
|
||||||
case XDMA_PIXFMT_Y210:
|
|
||||||
return width * 4u;
|
|
||||||
case XDMA_PIXFMT_YUY2:
|
|
||||||
case XDMA_PIXFMT_UYVY:
|
|
||||||
default:
|
|
||||||
return width * 2u;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(GVFG_XDMA_DEBUG_LOG)
|
#if defined(GVFG_XDMA_DEBUG_LOG)
|
||||||
static void xdma_debug_log(const char *fmt, ...)
|
static void xdma_debug_log(const char *fmt, ...)
|
||||||
{
|
{
|
||||||
@@ -348,8 +325,7 @@ namespace gvfg::internal
|
|||||||
stream_desc_.height = kDefaultHeight;
|
stream_desc_.height = kDefaultHeight;
|
||||||
stream_desc_.pixel_format = XDMA_PIXFMT_YUY2;
|
stream_desc_.pixel_format = XDMA_PIXFMT_YUY2;
|
||||||
stream_bit_depth_ = 8;
|
stream_bit_depth_ = 8;
|
||||||
stream_desc_.buffer_count = 1;
|
stream_desc_.buffer_count = kDefaultRingBufferCount;
|
||||||
stream_desc_.memory_kind = XDMA_MEMORY_DRIVER_COPY;
|
|
||||||
reset_stats(stats_, XDMA_STREAM_STOPPED);
|
reset_stats(stats_, XDMA_STREAM_STOPPED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -577,13 +553,12 @@ namespace gvfg::internal
|
|||||||
desc.pixel_format != XDMA_PIXFMT_UNKNOWN)
|
desc.pixel_format != XDMA_PIXFMT_UNKNOWN)
|
||||||
return fail(XDMA_ENOTSUP, "configure_stream(pixel_format)", ERROR_NOT_SUPPORTED);
|
return fail(XDMA_ENOTSUP, "configure_stream(pixel_format)", ERROR_NOT_SUPPORTED);
|
||||||
|
|
||||||
XDMA_LOG("configure: request input=%u %ux%u fmt=%u buffers=%u mem=%u flags=0x%x",
|
XDMA_LOG("configure: request input=%u %ux%u fmt=%u buffers=%u flags=0x%x",
|
||||||
static_cast<unsigned>(desc.input),
|
static_cast<unsigned>(desc.input),
|
||||||
desc.width,
|
desc.width,
|
||||||
desc.height,
|
desc.height,
|
||||||
static_cast<unsigned>(desc.pixel_format),
|
static_cast<unsigned>(desc.pixel_format),
|
||||||
desc.buffer_count,
|
desc.buffer_count,
|
||||||
static_cast<unsigned>(desc.memory_kind),
|
|
||||||
desc.flags);
|
desc.flags);
|
||||||
|
|
||||||
// Normalize the requested stream. The render path may support fewer
|
// Normalize the requested stream. The render path may support fewer
|
||||||
@@ -593,28 +568,29 @@ namespace gvfg::internal
|
|||||||
stream_desc_.width = desc.width ? desc.width : kDefaultWidth;
|
stream_desc_.width = desc.width ? desc.width : kDefaultWidth;
|
||||||
stream_desc_.height = desc.height ? desc.height : kDefaultHeight;
|
stream_desc_.height = desc.height ? desc.height : kDefaultHeight;
|
||||||
stream_desc_.pixel_format = desc.pixel_format == XDMA_PIXFMT_UNKNOWN ? XDMA_PIXFMT_YUY2 : desc.pixel_format;
|
stream_desc_.pixel_format = desc.pixel_format == XDMA_PIXFMT_UNKNOWN ? XDMA_PIXFMT_YUY2 : desc.pixel_format;
|
||||||
uint32_t rawBitDepth = 0;
|
|
||||||
stream_bit_depth_ = bit_depth_for_pixfmt(stream_desc_.pixel_format);
|
stream_bit_depth_ = bit_depth_for_pixfmt(stream_desc_.pixel_format);
|
||||||
stream_desc_.buffer_count = desc.buffer_count ? desc.buffer_count : 1;
|
stream_desc_.buffer_count = (std::max)(2u, (std::min)(desc.buffer_count ? desc.buffer_count : kDefaultRingBufferCount,
|
||||||
stream_desc_.memory_kind = XDMA_MEMORY_DRIVER_COPY;
|
kMaxRingBufferCount));
|
||||||
configured_ = true;
|
configured_ = true;
|
||||||
|
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
reset_stats(stats_, XDMA_STREAM_CONFIGURED);
|
reset_stats(stats_, XDMA_STREAM_CONFIGURED);
|
||||||
latest_frame_.clear();
|
frame_ring_.clear();
|
||||||
delivery_frame_.clear();
|
next_write_slot_ = 0;
|
||||||
|
active_delivery_slot_ = static_cast<size_t>(-1);
|
||||||
latest_sequence_ = 0;
|
latest_sequence_ = 0;
|
||||||
delivered_sequence_ = 0;
|
delivered_sequence_ = 0;
|
||||||
wait_timeout_count_ = 0;
|
wait_timeout_count_ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
XDMA_LOG("configure: effective input=%u path=%u %ux%u fmt=%u frame_bytes=%zu",
|
XDMA_LOG("configure: effective input=%u path=%u %ux%u fmt=%u ring_buffers=%u frame_bytes=%zu",
|
||||||
static_cast<unsigned>(stream_desc_.input),
|
static_cast<unsigned>(stream_desc_.input),
|
||||||
active_input_path(),
|
active_input_path(),
|
||||||
stream_desc_.width,
|
stream_desc_.width,
|
||||||
stream_desc_.height,
|
stream_desc_.height,
|
||||||
static_cast<unsigned>(stream_desc_.pixel_format),
|
static_cast<unsigned>(stream_desc_.pixel_format),
|
||||||
|
stream_desc_.buffer_count,
|
||||||
frame_size_bytes());
|
frame_size_bytes());
|
||||||
return XDMA_OK;
|
return XDMA_OK;
|
||||||
}
|
}
|
||||||
@@ -678,9 +654,11 @@ namespace gvfg::internal
|
|||||||
|
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
dma_buffer_.assign(bytes, 0);
|
frame_ring_.assign(stream_desc_.buffer_count, FrameSlot{});
|
||||||
latest_frame_.clear();
|
for (FrameSlot &slot : frame_ring_)
|
||||||
delivery_frame_.clear();
|
slot.data.assign(bytes, 0);
|
||||||
|
next_write_slot_ = 0;
|
||||||
|
active_delivery_slot_ = static_cast<size_t>(-1);
|
||||||
pending_events_ = 0;
|
pending_events_ = 0;
|
||||||
latest_sequence_ = 0;
|
latest_sequence_ = 0;
|
||||||
delivered_sequence_ = 0;
|
delivered_sequence_ = 0;
|
||||||
@@ -822,7 +800,12 @@ namespace gvfg::internal
|
|||||||
|
|
||||||
const auto hasFrame = [this]()
|
const auto hasFrame = [this]()
|
||||||
{
|
{
|
||||||
return latest_sequence_ > delivered_sequence_ || stream_error_ || !running_;
|
for (const FrameSlot &slot : frame_ring_)
|
||||||
|
{
|
||||||
|
if (slot.ready && slot.sequence > delivered_sequence_)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return stream_error_ || !running_;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (timeoutMs == 0)
|
if (timeoutMs == 0)
|
||||||
@@ -843,55 +826,53 @@ namespace gvfg::internal
|
|||||||
return XDMA_ETIMEOUT;
|
return XDMA_ETIMEOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!running_ && latest_sequence_ <= delivered_sequence_)
|
size_t readySlot = frame_ring_.size();
|
||||||
|
uint64_t readySequence = delivered_sequence_;
|
||||||
|
for (size_t i = 0; i < frame_ring_.size(); ++i)
|
||||||
|
{
|
||||||
|
const FrameSlot &slot = frame_ring_[i];
|
||||||
|
if (slot.ready && slot.sequence > readySequence)
|
||||||
|
{
|
||||||
|
readySlot = i;
|
||||||
|
readySequence = slot.sequence;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!running_ && readySlot == frame_ring_.size())
|
||||||
{
|
{
|
||||||
XDMA_LOG("wait_frame: stopped without pending frame");
|
XDMA_LOG("wait_frame: stopped without pending frame");
|
||||||
return XDMA_ESTATE;
|
return XDMA_ESTATE;
|
||||||
}
|
}
|
||||||
if (stream_error_ && latest_sequence_ <= delivered_sequence_)
|
if (stream_error_ && readySlot == frame_ring_.size())
|
||||||
{
|
{
|
||||||
XDMA_LOG("wait_frame: stream error latest=%llu delivered=%llu",
|
XDMA_LOG("wait_frame: stream error latest=%llu delivered=%llu",
|
||||||
static_cast<unsigned long long>(latest_sequence_),
|
static_cast<unsigned long long>(latest_sequence_),
|
||||||
static_cast<unsigned long long>(delivered_sequence_));
|
static_cast<unsigned long long>(delivered_sequence_));
|
||||||
return XDMA_EIO;
|
return XDMA_EIO;
|
||||||
}
|
}
|
||||||
if (latest_sequence_ <= delivered_sequence_)
|
if (readySlot == frame_ring_.size())
|
||||||
return XDMA_ETIMEOUT;
|
return XDMA_ETIMEOUT;
|
||||||
|
|
||||||
// Return a stable pointer to delivery_frame_. The pointer is valid
|
FrameSlot &slot = frame_ring_[readySlot];
|
||||||
// until the next wait_frame() on this same session or close().
|
slot.ready = false;
|
||||||
delivery_frame_ = latest_frame_;
|
slot.in_use = true;
|
||||||
delivered_sequence_ = latest_sequence_;
|
active_delivery_slot_ = readySlot;
|
||||||
|
delivered_sequence_ = slot.sequence;
|
||||||
++stats_.frames_delivered;
|
++stats_.frames_delivered;
|
||||||
|
|
||||||
out.data = delivery_frame_.data();
|
out.data = slot.data.data();
|
||||||
out.data_size_bytes = delivery_frame_.size();
|
out.data_size_bytes = slot.bytes;
|
||||||
out.frame_id = delivered_sequence_;
|
out.frame_id = delivered_sequence_;
|
||||||
out.timestamp_ns = static_cast<uint64_t>(
|
|
||||||
std::chrono::duration_cast<std::chrono::nanoseconds>(
|
|
||||||
std::chrono::steady_clock::now().time_since_epoch())
|
|
||||||
.count());
|
|
||||||
out.width = stream_desc_.width;
|
out.width = stream_desc_.width;
|
||||||
out.height = stream_desc_.height;
|
out.height = stream_desc_.height;
|
||||||
out.pixel_format = stream_desc_.pixel_format;
|
out.pixel_format = stream_desc_.pixel_format;
|
||||||
out.bit_depth = stream_bit_depth_;
|
out.bit_depth = stream_bit_depth_;
|
||||||
out.plane_count = plane_count_for_pixfmt(stream_desc_.pixel_format);
|
|
||||||
out.plane_offset_bytes[0] = 0;
|
|
||||||
out.plane_stride_bytes[0] = plane_stride_for_pixfmt(stream_desc_.width, stream_desc_.pixel_format, stream_bit_depth_);
|
|
||||||
if (out.plane_count > 1)
|
|
||||||
{
|
|
||||||
out.plane_offset_bytes[1] = out.plane_stride_bytes[0] * stream_desc_.height;
|
|
||||||
out.plane_stride_bytes[1] = out.plane_stride_bytes[0];
|
|
||||||
}
|
|
||||||
out.driver_buffer_index = 0;
|
|
||||||
out.flags = 0;
|
|
||||||
if (should_log_counter(out.frame_id))
|
if (should_log_counter(out.frame_id))
|
||||||
XDMA_LOG("wait_frame: deliver id=%llu bytes=%zu %ux%u stride=%u captured=%llu delivered=%llu",
|
XDMA_LOG("wait_frame: deliver id=%llu bytes=%zu %ux%u captured=%llu delivered=%llu",
|
||||||
static_cast<unsigned long long>(out.frame_id),
|
static_cast<unsigned long long>(out.frame_id),
|
||||||
out.data_size_bytes,
|
out.data_size_bytes,
|
||||||
out.width,
|
out.width,
|
||||||
out.height,
|
out.height,
|
||||||
out.plane_stride_bytes[0],
|
|
||||||
static_cast<unsigned long long>(stats_.frames_captured),
|
static_cast<unsigned long long>(stats_.frames_captured),
|
||||||
static_cast<unsigned long long>(stats_.frames_delivered));
|
static_cast<unsigned long long>(stats_.frames_delivered));
|
||||||
return XDMA_OK;
|
return XDMA_OK;
|
||||||
@@ -899,6 +880,13 @@ namespace gvfg::internal
|
|||||||
|
|
||||||
xdma_status_t XdmaCaptureSession::release_frame(const xdma_frame_t &)
|
xdma_status_t XdmaCaptureSession::release_frame(const xdma_frame_t &)
|
||||||
{
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
if (active_delivery_slot_ < frame_ring_.size())
|
||||||
|
{
|
||||||
|
frame_ring_[active_delivery_slot_].in_use = false;
|
||||||
|
active_delivery_slot_ = static_cast<size_t>(-1);
|
||||||
|
}
|
||||||
|
data_cv_.notify_all();
|
||||||
return XDMA_OK;
|
return XDMA_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1065,32 +1053,75 @@ namespace gvfg::internal
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
const DWORD bytes = static_cast<DWORD>(frame_size_bytes());
|
const DWORD bytes = static_cast<DWORD>(frame_size_bytes());
|
||||||
if (dma_buffer_.size() < bytes)
|
size_t slotIndex = frame_ring_.size();
|
||||||
|
uint8_t *slotData = nullptr;
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
std::unique_lock<std::mutex> lock(mutex_);
|
||||||
if (dma_buffer_.size() < bytes)
|
data_cv_.wait(lock, [this]()
|
||||||
dma_buffer_.resize(bytes);
|
{
|
||||||
|
if (!running_ || data_worker_stop_)
|
||||||
|
return true;
|
||||||
|
for (const FrameSlot &slot : frame_ring_)
|
||||||
|
{
|
||||||
|
if (!slot.in_use)
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
const int ret = read_device(c2h_device_[active_input_path()], 0, bytes, dma_buffer_.data());
|
return false;
|
||||||
|
});
|
||||||
|
if (!running_ || data_worker_stop_)
|
||||||
|
break;
|
||||||
|
|
||||||
|
for (size_t attempt = 0; attempt < frame_ring_.size(); ++attempt)
|
||||||
|
{
|
||||||
|
const size_t candidate = (next_write_slot_ + attempt) % frame_ring_.size();
|
||||||
|
if (!frame_ring_[candidate].in_use)
|
||||||
|
{
|
||||||
|
slotIndex = candidate;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (slotIndex == frame_ring_.size())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
FrameSlot &slot = frame_ring_[slotIndex];
|
||||||
|
if (slot.ready && slot.sequence > delivered_sequence_)
|
||||||
|
++stats_.frames_dropped;
|
||||||
|
slot.ready = false;
|
||||||
|
slot.in_use = true;
|
||||||
|
if (slot.data.size() < bytes)
|
||||||
|
slot.data.resize(bytes);
|
||||||
|
slotData = slot.data.data();
|
||||||
|
next_write_slot_ = (slotIndex + 1) % frame_ring_.size();
|
||||||
|
}
|
||||||
|
const int ret = read_device(c2h_device_[active_input_path()], 0, bytes, slotData);
|
||||||
|
|
||||||
if (!running_)
|
if (!running_)
|
||||||
break;
|
break;
|
||||||
if (!capture_active_)
|
if (!capture_active_)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
if (slotIndex < frame_ring_.size())
|
||||||
|
frame_ring_[slotIndex].in_use = false;
|
||||||
|
data_cv_.notify_all();
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
if (ret < 0 || static_cast<DWORD>(ret) != bytes)
|
if (ret < 0 || static_cast<DWORD>(ret) != bytes)
|
||||||
{
|
{
|
||||||
fail(XDMA_EIO, "ReadFile(c2h)");
|
fail(XDMA_EIO, "ReadFile(c2h)");
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
if (slotIndex < frame_ring_.size())
|
||||||
|
frame_ring_[slotIndex].in_use = false;
|
||||||
stream_error_ = true;
|
stream_error_ = true;
|
||||||
++stats_.dma_errors;
|
++stats_.dma_errors;
|
||||||
|
data_cv_.notify_all();
|
||||||
frame_cv_.notify_all();
|
frame_cv_.notify_all();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
handle_plug_in_frame_fix(dma_buffer_.data(), static_cast<size_t>(ret));
|
handle_plug_in_frame_fix(slotData, static_cast<size_t>(ret));
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
publish_frame(dma_buffer_.data(), static_cast<size_t>(ret));
|
publish_frame(slotIndex, static_cast<size_t>(ret));
|
||||||
}
|
}
|
||||||
XDMA_LOG("data_thread: exit running=%d", running_ ? 1 : 0);
|
XDMA_LOG("data_thread: exit running=%d", running_ ? 1 : 0);
|
||||||
}
|
}
|
||||||
@@ -1203,9 +1234,17 @@ namespace gvfg::internal
|
|||||||
uint64_t deliveredAfterPause = 0;
|
uint64_t deliveredAfterPause = 0;
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
dma_buffer_.clear();
|
for (FrameSlot &slot : frame_ring_)
|
||||||
latest_frame_.clear();
|
{
|
||||||
delivery_frame_.clear();
|
if (!slot.in_use)
|
||||||
|
{
|
||||||
|
slot.bytes = 0;
|
||||||
|
slot.sequence = 0;
|
||||||
|
slot.ready = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
next_write_slot_ = 0;
|
||||||
|
active_delivery_slot_ = static_cast<size_t>(-1);
|
||||||
latest_sequence_ = delivered_sequence_;
|
latest_sequence_ = delivered_sequence_;
|
||||||
latestAfterPause = latest_sequence_;
|
latestAfterPause = latest_sequence_;
|
||||||
deliveredAfterPause = delivered_sequence_;
|
deliveredAfterPause = delivered_sequence_;
|
||||||
@@ -1263,7 +1302,26 @@ namespace gvfg::internal
|
|||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
const size_t bytes = frame_size_bytes();
|
const size_t bytes = frame_size_bytes();
|
||||||
dma_buffer_.assign(bytes, 0);
|
bool anyInUse = false;
|
||||||
|
for (FrameSlot &slot : frame_ring_)
|
||||||
|
anyInUse = anyInUse || slot.in_use;
|
||||||
|
if (!anyInUse && frame_ring_.size() != stream_desc_.buffer_count)
|
||||||
|
frame_ring_.assign(stream_desc_.buffer_count, FrameSlot{});
|
||||||
|
if (frame_ring_.empty())
|
||||||
|
frame_ring_.assign(stream_desc_.buffer_count, FrameSlot{});
|
||||||
|
for (FrameSlot &slot : frame_ring_)
|
||||||
|
{
|
||||||
|
if (!slot.in_use)
|
||||||
|
{
|
||||||
|
slot.data.assign(bytes, 0);
|
||||||
|
slot.bytes = 0;
|
||||||
|
slot.sequence = 0;
|
||||||
|
slot.ready = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
next_write_slot_ = 0;
|
||||||
|
if (active_delivery_slot_ >= frame_ring_.size())
|
||||||
|
active_delivery_slot_ = static_cast<size_t>(-1);
|
||||||
allocatedBytes = bytes;
|
allocatedBytes = bytes;
|
||||||
pending_events_ = 0;
|
pending_events_ = 0;
|
||||||
stream_error_ = false;
|
stream_error_ = false;
|
||||||
@@ -1381,20 +1439,30 @@ namespace gvfg::internal
|
|||||||
statusAfterLow);
|
statusAfterLow);
|
||||||
}
|
}
|
||||||
|
|
||||||
void XdmaCaptureSession::publish_frame(const uint8_t *data, size_t bytes)
|
void XdmaCaptureSession::publish_frame(size_t slotIndex, size_t bytes)
|
||||||
{
|
{
|
||||||
// Keep only the newest frame. This makes wait_frame() simple and avoids
|
if (slotIndex >= frame_ring_.size())
|
||||||
// unbounded queue growth if the consumer is slower than the device.
|
return;
|
||||||
latest_frame_.assign(data, data + bytes);
|
|
||||||
|
// Keep only ring-backed frames. Older ready slots may be overwritten by
|
||||||
|
// the data thread if the consumer is slower than the device.
|
||||||
|
FrameSlot &slot = frame_ring_[slotIndex];
|
||||||
|
slot.bytes = bytes;
|
||||||
|
slot.sequence = latest_sequence_ + 1;
|
||||||
|
slot.ready = true;
|
||||||
|
slot.in_use = false;
|
||||||
++latest_sequence_;
|
++latest_sequence_;
|
||||||
++stats_.frames_captured;
|
++stats_.frames_captured;
|
||||||
stats_.state = XDMA_STREAM_RUNNING;
|
stats_.state = XDMA_STREAM_RUNNING;
|
||||||
if (should_log_counter(latest_sequence_))
|
if (should_log_counter(latest_sequence_))
|
||||||
XDMA_LOG("publish_frame: id=%llu bytes=%zu captured=%llu",
|
XDMA_LOG("publish_frame: id=%llu slot=%zu bytes=%zu captured=%llu dropped=%llu",
|
||||||
static_cast<unsigned long long>(latest_sequence_),
|
static_cast<unsigned long long>(latest_sequence_),
|
||||||
|
slotIndex,
|
||||||
bytes,
|
bytes,
|
||||||
static_cast<unsigned long long>(stats_.frames_captured));
|
static_cast<unsigned long long>(stats_.frames_captured),
|
||||||
|
static_cast<unsigned long long>(stats_.frames_dropped));
|
||||||
frame_cv_.notify_one();
|
frame_cv_.notify_one();
|
||||||
|
data_cv_.notify_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
int XdmaCaptureSession::read_device(HANDLE device, long address, DWORD size, uint8_t *buffer) const
|
int XdmaCaptureSession::read_device(HANDLE device, long address, DWORD size, uint8_t *buffer) const
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ namespace gvfg::internal
|
|||||||
void data_thread_proc();
|
void data_thread_proc();
|
||||||
bool start_data_worker();
|
bool start_data_worker();
|
||||||
void stop_data_worker();
|
void stop_data_worker();
|
||||||
void publish_frame(const uint8_t *data, size_t bytes);
|
void publish_frame(size_t slotIndex, size_t bytes);
|
||||||
void pause_capture_for_plug_out();
|
void pause_capture_for_plug_out();
|
||||||
void resume_capture_after_plug_in();
|
void resume_capture_after_plug_in();
|
||||||
void handle_plug_in_frame_fix(const uint8_t *data, size_t bytes);
|
void handle_plug_in_frame_fix(const uint8_t *data, size_t bytes);
|
||||||
@@ -82,6 +82,15 @@ namespace gvfg::internal
|
|||||||
long capture_enable_reg() const;
|
long capture_enable_reg() const;
|
||||||
size_t frame_size_bytes() const;
|
size_t frame_size_bytes() const;
|
||||||
|
|
||||||
|
struct FrameSlot
|
||||||
|
{
|
||||||
|
std::vector<uint8_t> data; // Frame byte storage for this ring slot.
|
||||||
|
size_t bytes = 0; // Number of valid bytes read into data.
|
||||||
|
uint64_t sequence = 0; // Monotonic frame sequence assigned on publish.
|
||||||
|
bool ready = false; // True after the data thread publishes a frame.
|
||||||
|
bool in_use = false; // True while writing or while the caller holds the slot.
|
||||||
|
};
|
||||||
|
|
||||||
std::wstring base_path_;
|
std::wstring base_path_;
|
||||||
std::wstring friendly_name_;
|
std::wstring friendly_name_;
|
||||||
|
|
||||||
@@ -112,9 +121,9 @@ namespace gvfg::internal
|
|||||||
void *event_callback_user_ = nullptr;
|
void *event_callback_user_ = nullptr;
|
||||||
uint32_t event_mask_filter_ = XDMA_EVENT_MASK_DEFAULT;
|
uint32_t event_mask_filter_ = XDMA_EVENT_MASK_DEFAULT;
|
||||||
uint32_t pending_events_ = 0;
|
uint32_t pending_events_ = 0;
|
||||||
std::vector<uint8_t> dma_buffer_;
|
std::vector<FrameSlot> frame_ring_;
|
||||||
std::vector<uint8_t> latest_frame_;
|
size_t next_write_slot_ = 0;
|
||||||
std::vector<uint8_t> delivery_frame_;
|
size_t active_delivery_slot_ = static_cast<size_t>(-1);
|
||||||
uint64_t latest_sequence_ = 0;
|
uint64_t latest_sequence_ = 0;
|
||||||
uint64_t delivered_sequence_ = 0;
|
uint64_t delivered_sequence_ = 0;
|
||||||
uint64_t wait_timeout_count_ = 0;
|
uint64_t wait_timeout_count_ = 0;
|
||||||
|
|||||||
@@ -607,8 +607,7 @@ struct gvfg_handle_t
|
|||||||
desc.width = width;
|
desc.width = width;
|
||||||
desc.height = height;
|
desc.height = height;
|
||||||
desc.pixel_format = pixelFormat;
|
desc.pixel_format = pixelFormat;
|
||||||
desc.buffer_count = 1;
|
desc.buffer_count = 3;
|
||||||
desc.memory_kind = XDMA_MEMORY_DRIVER_COPY;
|
|
||||||
|
|
||||||
const xdma_status_t st = backend->configure_stream(desc);
|
const xdma_status_t st = backend->configure_stream(desc);
|
||||||
if (st != XDMA_OK)
|
if (st != XDMA_OK)
|
||||||
@@ -714,7 +713,7 @@ struct gvfg_handle_t
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
updateRuntimeFps(frame.timestamp_ns ? frame.timestamp_ns : now_ns());
|
updateRuntimeFps(now_ns());
|
||||||
renderGpuFrame(frame);
|
renderGpuFrame(frame);
|
||||||
emitNativeFrame(frame);
|
emitNativeFrame(frame);
|
||||||
backend->release_frame(frame);
|
backend->release_frame(frame);
|
||||||
@@ -748,44 +747,19 @@ struct gvfg_handle_t
|
|||||||
{
|
{
|
||||||
case XDMA_PIXFMT_YUY2:
|
case XDMA_PIXFMT_YUY2:
|
||||||
{
|
{
|
||||||
const int stride = frame.plane_stride_bytes[0] ? static_cast<int>(frame.plane_stride_bytes[0]) : w * 2;
|
|
||||||
if (stride < w * 2)
|
|
||||||
return false;
|
|
||||||
renderFmt = GVFG_RENDER_FMT_YUY2;
|
renderFmt = GVFG_RENDER_FMT_YUY2;
|
||||||
uploaded = pipeline->upload_yuy2_frame(base + frame.plane_offset_bytes[0], stride, w, h);
|
uploaded = pipeline->upload_yuy2_frame(base, w * 2, w, h);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case XDMA_PIXFMT_Y210:
|
case XDMA_PIXFMT_Y210:
|
||||||
{
|
{
|
||||||
const int stride = frame.plane_stride_bytes[0] ? static_cast<int>(frame.plane_stride_bytes[0]) : w * 4;
|
|
||||||
if (stride < w * 4)
|
|
||||||
return false;
|
|
||||||
renderFmt = GVFG_RENDER_FMT_Y210;
|
renderFmt = GVFG_RENDER_FMT_Y210;
|
||||||
uploaded = pipeline->upload_y210_frame(base + frame.plane_offset_bytes[0], stride, w, h);
|
uploaded = pipeline->upload_y210_frame(base, w * 4, w, h);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case XDMA_PIXFMT_NV12:
|
case XDMA_PIXFMT_NV12:
|
||||||
{
|
|
||||||
if (frame.plane_count < 2)
|
|
||||||
return false;
|
|
||||||
const int strideY = frame.plane_stride_bytes[0] ? static_cast<int>(frame.plane_stride_bytes[0]) : w;
|
|
||||||
const int strideUV = frame.plane_stride_bytes[1] ? static_cast<int>(frame.plane_stride_bytes[1]) : strideY;
|
|
||||||
renderFmt = GVFG_RENDER_FMT_NV12;
|
|
||||||
uploaded = pipeline->upload_nv12_frame(base + frame.plane_offset_bytes[0], strideY,
|
|
||||||
base + frame.plane_offset_bytes[1], strideUV, w, h);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case XDMA_PIXFMT_P010:
|
case XDMA_PIXFMT_P010:
|
||||||
{
|
|
||||||
if (frame.plane_count < 2)
|
|
||||||
return false;
|
return false;
|
||||||
const int strideY = frame.plane_stride_bytes[0] ? static_cast<int>(frame.plane_stride_bytes[0]) : w * 2;
|
|
||||||
const int strideUV = frame.plane_stride_bytes[1] ? static_cast<int>(frame.plane_stride_bytes[1]) : strideY;
|
|
||||||
renderFmt = GVFG_RENDER_FMT_P010;
|
|
||||||
uploaded = pipeline->upload_p010_frame(base + frame.plane_offset_bytes[0], strideY,
|
|
||||||
base + frame.plane_offset_bytes[1], strideUV, w, h);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -810,24 +784,16 @@ struct gvfg_handle_t
|
|||||||
if (!shouldEmitFrameCallback(frame.frame_id))
|
if (!shouldEmitFrameCallback(frame.frame_id))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (frame.width == 0 || frame.height == 0 || frame.plane_count == 0)
|
if (frame.width == 0 || frame.height == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
gvfg_frame_t out{};
|
gvfg_frame_t out{};
|
||||||
out.data = frame.data;
|
out.data = frame.data;
|
||||||
out.data_size = static_cast<uint64_t>(frame.data_size_bytes);
|
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.width = static_cast<int>(frame.width);
|
||||||
out.height = static_cast<int>(frame.height);
|
out.height = static_cast<int>(frame.height);
|
||||||
out.pixel_format = to_gvfg_pixel_format(frame.pixel_format);
|
out.pixel_format = to_gvfg_pixel_format(frame.pixel_format);
|
||||||
out.bit_depth = static_cast<int>(frame.bit_depth);
|
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;
|
out.frame_id = frame.frame_id;
|
||||||
onFrame(&out, callbackUser);
|
onFrame(&out, callbackUser);
|
||||||
noteDeliveredFrame(out.width, out.height, out.bit_depth, out.pixel_format);
|
noteDeliveredFrame(out.width, out.height, out.bit_depth, out.pixel_format);
|
||||||
|
|||||||
Reference in New Issue
Block a user