From f7e5d2006800dbfe844a8e152f3f4595550b04ce Mon Sep 17 00:00:00 2001 From: "Mark.lin" Date: Mon, 20 Jul 2026 16:24:06 +0800 Subject: [PATCH] fix: handle FPGA Y210 payload and correct chroma order - treat FPGA v210 format marker as Y210 payload - calculate Y210 DMA frame size as width * height * 4 - route CaptureDemo preview frames through the Y210 path - normalize FPGA Y0/V/Y1/U data to standard Y0/U/Y1/V - apply the same chroma correction to preview and frame conversion - reallocate capture buffers using the detected pixel format --- CaptureDemo/CaptureDemo/mainwindow.cpp | 8 ++-- CaptureDemo/CaptureDemo/video_card.cpp | 43 +++++++++++++++---- helpers/gvfg_convert/src/gvfg_convert.cpp | 7 ++- .../gvfg_preview/src/d3d_preview_pipeline.cpp | 6 ++- .../pcies2mm/pcies2mm_capture_session.cpp | 5 ++- 5 files changed, 52 insertions(+), 17 deletions(-) diff --git a/CaptureDemo/CaptureDemo/mainwindow.cpp b/CaptureDemo/CaptureDemo/mainwindow.cpp index 7504def..f57e1f0 100644 --- a/CaptureDemo/CaptureDemo/mainwindow.cpp +++ b/CaptureDemo/CaptureDemo/mainwindow.cpp @@ -23,7 +23,6 @@ constexpr ULONG FOURCC_V210 = 0x76323130; constexpr int GVFG_PIXFMT_Y210 = 7; -constexpr int GVFG_PIXFMT_V210 = 9; struct GvfgFrame { @@ -599,12 +598,15 @@ void MainWindow::init() auto cb = [=](uchar *data, int video_width, int video_height) { if (fourcc == FOURCC_V210 && preview_runtime_[ch]) { - const uint64_t frameSize = (uint64_t)(((video_width + 5) / 6) * 16) * (uint64_t)video_height; + // The FPGA format register currently reports v210, + // but its DMA payload is Y210 (4 bytes per pixel). + const uint64_t frameSize = + (uint64_t)video_width * 4u * (uint64_t)video_height; preview_runtime_[ch]->renderFrameData(data, frameSize, video_width, video_height, - GVFG_PIXFMT_V210, + GVFG_PIXFMT_Y210, 10, 0); } else { diff --git a/CaptureDemo/CaptureDemo/video_card.cpp b/CaptureDemo/CaptureDemo/video_card.cpp index 31a20cd..fce895d 100644 --- a/CaptureDemo/CaptureDemo/video_card.cpp +++ b/CaptureDemo/CaptureDemo/video_card.cpp @@ -31,7 +31,13 @@ int calcFrameBytes(int width, int height, UINT32 fourcc) } else if (fourcc == 0x76323130) // v210 { - return ((width + 5) / 6) * 16 * height; + // Temporary FPGA contract: the format register still reports v210, + // but the DMA payload uses Y210 (16 bits per component) layout. + return width * height * 4; + } + else if (fourcc == 0x59323130) // Y210 + { + return width * height * 4; } return 0; } @@ -429,6 +435,7 @@ void VideoCard::captureThread(ULONG channelIndex) ULONG irq_mask = 0; ULONG video_en = 0; ULONG dma_en = 0; + ULONG dma_bytes = 0; ULONG done_ptr = 0; ULONG lost_frame = 0; ULONG video_irq = 0; @@ -438,6 +445,7 @@ void VideoCard::captureThread(ULONG channelIndex) readReg(INTERRUPT_BASE + IRQ_MASK_STATUS_OFFSET, irq_mask); readReg(ch.video_base + VIDEO_EN_OFFSET, video_en); readReg(ch.video_base + VIDEO_DMA_EN_OFFSET, dma_en); + readReg(ch.video_base + VIDEO_DMA_BYTES_OFFSET, dma_bytes); readReg(ch.video_base + VIDEO_DMA_DONE_PTR_OFFSET, done_ptr); readReg(ch.video_base + VIDEO_LOST_FRAME_CNT_OFFSET, lost_frame); readReg(ch.video_base + VIDEO_IRQ_STATUS_OFFSET, video_irq); @@ -449,7 +457,8 @@ void VideoCard::captureThread(ULONG channelIndex) << "irq_mask=0x" << irq_mask << "video_en=0x" << video_en << "dma_en=0x" << dma_en - << "done_ptr=0x" << done_ptr + << "dma_bytes=" << Qt::dec << dma_bytes + << "done_ptr=0x" << Qt::hex << done_ptr << "lost=0x" << lost_frame << "video_irq=0x" << video_irq << "video_dma_irq=0x" << video_dma_irq @@ -521,7 +530,7 @@ void VideoCard::captureThread(ULONG channelIndex) if (ch.frame_count == 10) { std::ofstream of("./v210.yuv", std::ios::out | std::ios::binary); - of.write((const char*)ch.frame_buffer, frame_size); + of.write((const char *)ch.frame_buffer, frame_size); of.close(); qDebug() << "save v210.yuv done"; } @@ -568,10 +577,18 @@ void VideoCard::videoModuleThread(ULONG channelIndex) writeReg(ch.video_base + VIDEO_EN_OFFSET, 0); readReg(ch.video_base + VIDEO_HSIZE_OFFSET, ch.video_width); readReg(ch.video_base + VIDEO_VSIZE_OFFSET, ch.video_height); - ULONG frame_size = ch.video_width * ch.video_height * 2; - ch.frame_buffer = new unsigned char[ch.video_width * ch.video_height * 2]; + ULONG detected_fourcc = 0; + readReg(ch.video_base + VIDEO_FORMAT_OFFSET, detected_fourcc); + ch.fourcc = detected_fourcc; + const ULONG frame_size = calcFrameBytes(ch.video_width, ch.video_height, ch.fourcc); + delete[] ch.frame_buffer; + ch.frame_buffer = new unsigned char[frame_size]; memset(ch.frame_buffer, 0, frame_size); - qDebug() << "ch" << channelIndex << "video format change event received, new resolution:" << ch.video_width << "x" << ch.video_height; + qDebug() << "ch" << channelIndex + << "video format change event received, new resolution:" + << ch.video_width << "x" << ch.video_height + << "fourcc=0x" << Qt::hex << ch.fourcc << Qt::dec + << "frame_size:" << frame_size; continue; } @@ -581,10 +598,18 @@ void VideoCard::videoModuleThread(ULONG channelIndex) writeReg(ch.video_base + VIDEO_EN_OFFSET, 0); readReg(ch.video_base + VIDEO_HSIZE_OFFSET, ch.video_width); readReg(ch.video_base + VIDEO_VSIZE_OFFSET, ch.video_height); - ULONG frame_size = ch.video_width * ch.video_height * 2; - ch.frame_buffer = new unsigned char[ch.video_width * ch.video_height * 2]; + ULONG detected_fourcc = 0; + readReg(ch.video_base + VIDEO_FORMAT_OFFSET, detected_fourcc); + ch.fourcc = detected_fourcc; + const ULONG frame_size = calcFrameBytes(ch.video_width, ch.video_height, ch.fourcc); + delete[] ch.frame_buffer; + ch.frame_buffer = new unsigned char[frame_size]; memset(ch.frame_buffer, 0, frame_size); - qDebug() << "ch" << channelIndex << "video plugin event received, width:" << ch.video_width << ", height:" << ch.video_height; + qDebug() << "ch" << channelIndex + << "video plugin event received, width:" << ch.video_width + << ", height:" << ch.video_height + << ", fourcc=0x" << Qt::hex << ch.fourcc << Qt::dec + << "frame_size:" << frame_size; writeReg(ch.video_base + VIDEO_DMA_EN_OFFSET, 1); writeReg(ch.video_base + VIDEO_EN_OFFSET, 1); continue; diff --git a/helpers/gvfg_convert/src/gvfg_convert.cpp b/helpers/gvfg_convert/src/gvfg_convert.cpp index 3157c37..4b904d8 100644 --- a/helpers/gvfg_convert/src/gvfg_convert.cpp +++ b/helpers/gvfg_convert/src/gvfg_convert.cpp @@ -275,10 +275,13 @@ namespace for (int x = 0; x < src.width; x += 2) { const int sx = x * 2; + // Standard Y210 is Y0, Cb(U), Y1, Cr(V). Normalize the + // current FPGA DMA order Y0, Cr(V), Y1, Cb(U) before RGB + // conversion so snapshots match the preview path. const uint16_t y0 = y210_word_to_10bit(srcRow[sx + 0]); - const uint16_t u = y210_word_to_10bit(srcRow[sx + 1]); + const uint16_t v = y210_word_to_10bit(srcRow[sx + 1]); const uint16_t y1 = (x + 1 < src.width) ? y210_word_to_10bit(srcRow[sx + 2]) : y0; - const uint16_t v = (x + 1 < src.width) ? y210_word_to_10bit(srcRow[sx + 3]) : u; + const uint16_t u = (x + 1 < src.width) ? y210_word_to_10bit(srcRow[sx + 3]) : v; double r = 0.0, g = 0.0, b = 0.0; yuv709_limited_to_rgb01(static_cast(y0) / 1023.0, diff --git a/helpers/gvfg_preview/src/d3d_preview_pipeline.cpp b/helpers/gvfg_preview/src/d3d_preview_pipeline.cpp index c26f636..b4935a1 100644 --- a/helpers/gvfg_preview/src/d3d_preview_pipeline.cpp +++ b/helpers/gvfg_preview/src/d3d_preview_pipeline.cpp @@ -1094,10 +1094,12 @@ bool D3DPreviewPipeline::upload_y210_frame(const uint8_t *data, int src_stride, for (int x = 0; x < w2; ++x) { const int srcX = x * 4; + // Standard Y210 is Y0, Cb(U), Y1, Cr(V). The current FPGA DMA + // payload arrives as Y0, Cr(V), Y1, Cb(U), so normalize it here. const uint16_t Y0 = normalize_y210_word_for_upload(src16[srcX + 0]); - const uint16_t U = normalize_y210_word_for_upload(src16[srcX + 1]); + const uint16_t V = normalize_y210_word_for_upload(src16[srcX + 1]); const uint16_t Y1 = (srcX + 2 < rowWords) ? normalize_y210_word_for_upload(src16[srcX + 2]) : Y0; - const uint16_t V = (srcX + 3 < rowWords) ? normalize_y210_word_for_upload(src16[srcX + 3]) : U; + const uint16_t U = (srcX + 3 < rowWords) ? normalize_y210_word_for_upload(src16[srcX + 3]) : V; uint16_t *d4 = dst16 + x * 4; d4[0] = Y0; diff --git a/sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.cpp b/sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.cpp index 1228580..5b83695 100644 --- a/sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.cpp +++ b/sdk/gvfg/src/backend/pcies2mm/pcies2mm_capture_session.cpp @@ -165,7 +165,9 @@ namespace case fourcc('N', 'V', '1', '2'): return PCIES2MM_PIXFMT_NV12; case fourcc('v', '2', '1', '0'): - return PCIES2MM_PIXFMT_V210; + // Temporary FPGA contract: FOURCC remains v210 while the DMA + // payload is laid out as Y210. + return PCIES2MM_PIXFMT_Y210; case fourcc('Y', '2', '1', '0'): return PCIES2MM_PIXFMT_Y210; case fourcc('P', '0', '1', '0'): @@ -456,6 +458,7 @@ namespace gvfg::internal { case PCIES2MM_PIXFMT_YUY2: case PCIES2MM_PIXFMT_UYVY: + case PCIES2MM_PIXFMT_Y210: case PCIES2MM_PIXFMT_V210: break; default: