From c68f68fdf69a39048b621f6358ff87774cf4be85 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 10 Nov 2026 17:56:31 +0100 Subject: [PATCH 1/2] Decode V4L2 cameras with the YCbCr encoding they report Qt Multimedia's V4L2 camera maps V4L2_COLORSPACE_SRGB to BT.601 and never reads ycbcr_enc. Capture cards such as the Elgato Cam Link 4K report sRGB with an explicit BT.709 encoding, so Monologue decoded them with the wrong matrix: colors came out muted compared to browsers and other apps. Ask the device for its encoding on the first frame and, when Qt guessed BT.601 but the driver says otherwise, retag frames with the correct color space. Preview and recordings both use the corrected frames. Cameras that really deliver BT.601 (like most MJPEG webcams) are untouched. Co-Authored-By: Claude Opus 5.5 --- src/backend.cpp | 10 +++++++- src/backend.h | 3 ++- src/mediautils.cpp | 53 +++++++++++++++++++++++++++++++++++++++++ src/mediautils.h | 7 ++++++ src/writer.cpp | 33 +++---------------------- tests/backend_tests.cpp | 13 ++++++++++ 6 files changed, 87 insertions(+), 32 deletions(-) diff --git a/src/backend.cpp b/src/backend.cpp index 9c50603..4f9fd58 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -156,8 +156,16 @@ void Backend::updateReady() { m_state="ready"; m_message.clear(); emit changed(); } } -void Backend::receiveVideo(const QVideoFrame &frame) { +void Backend::receiveVideo(QVideoFrame frame) { if(!m_camera || !frame.isValid()) return; + // Qt maps V4L2's sRGB colorspace to BT.601 and ignores the YCbCr encoding the + // driver reports, so HDMI capture cards sending BT.709 decode with skewed colors. + if(!m_cameraHealthy) m_colorSpace=media::cameraColorSpace(m_camera->cameraDevice().id()); + if(frame.surfaceFormat().colorSpace()==QVideoFrameFormat::ColorSpace_BT601 && + m_colorSpace!=QVideoFrameFormat::ColorSpace_Undefined && m_colorSpace!=QVideoFrameFormat::ColorSpace_BT601) { + auto format=frame.surfaceFormat(); format.setColorSpace(m_colorSpace); + frame=media::withFormat(frame,format); + } m_lastVideoAt=now(); if(frame.size()!=m_cameraFormat.resolution()) { sourceFailed("The camera did not provide its maximum resolution. Choose another source or Retry."); return; diff --git a/src/backend.h b/src/backend.h index e8eaceb..3e19ef4 100644 --- a/src/backend.h +++ b/src/backend.h @@ -115,7 +115,7 @@ class Backend : public QObject { void readAudio(); void receiveAudio(const QByteArray &data, qint64 capturedAt); void finishWhenCaptured(); - void receiveVideo(const QVideoFrame &frame); + void receiveVideo(QVideoFrame frame); void tick(); void sourceFailed(const QString &message); void startTake(); @@ -142,6 +142,7 @@ class Backend : public QObject { QAudioFormat m_audioFormat; QVideoFrame m_lastFrame; QCameraFormat m_cameraFormat; + QVideoFrameFormat::ColorSpace m_colorSpace = QVideoFrameFormat::ColorSpace_Undefined; QElapsedTimer m_wall; QTimer m_tick; QTimer m_finishTimeout; diff --git a/src/mediautils.cpp b/src/mediautils.cpp index 190212f..ac35eaa 100644 --- a/src/mediautils.cpp +++ b/src/mediautils.cpp @@ -9,6 +9,59 @@ #include #include #include +#include +#include +#include +#include +#include + +namespace { +class FrameBuffer : public QAbstractVideoBuffer { +public: + FrameBuffer(const QVideoFrame &source, const QVideoFrameFormat &format) : m_source(source), m_format(format) {} + ~FrameBuffer() override { unmap(); } + QVideoFrameFormat format() const override { return m_format; } + MapData map(QVideoFrame::MapMode mode) override { + MapData result; + if(!m_source.map(mode)) return result; + m_mapped=true; + result.planeCount=m_source.planeCount(); + for(int i=0;i(source, format)); +} +QVideoFrameFormat::ColorSpace media::colorSpace(int v4l2Colorspace, int v4l2Encoding) { + if (v4l2Encoding == V4L2_YCBCR_ENC_DEFAULT) v4l2Encoding = V4L2_MAP_YCBCR_ENC_DEFAULT(v4l2Colorspace); + switch (v4l2Encoding) { + case V4L2_YCBCR_ENC_601: case V4L2_YCBCR_ENC_XV601: return QVideoFrameFormat::ColorSpace_BT601; + case V4L2_YCBCR_ENC_709: case V4L2_YCBCR_ENC_XV709: return QVideoFrameFormat::ColorSpace_BT709; + case V4L2_YCBCR_ENC_BT2020: case V4L2_YCBCR_ENC_BT2020_CONST_LUM: return QVideoFrameFormat::ColorSpace_BT2020; + default: return QVideoFrameFormat::ColorSpace_Undefined; + } +} +QVideoFrameFormat::ColorSpace media::cameraColorSpace(const QByteArray &device) { + const int fd = ::open(device.constData(), O_RDONLY | O_NONBLOCK | O_CLOEXEC); + if (fd < 0) return QVideoFrameFormat::ColorSpace_Undefined; + v4l2_format format{}; + format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + const bool ok = ::ioctl(fd, VIDIOC_G_FMT, &format) == 0; + ::close(fd); + return ok ? colorSpace(format.fmt.pix.colorspace, format.fmt.pix.ycbcr_enc) : QVideoFrameFormat::ColorSpace_Undefined; +} double media::targetFps(double minimum, double maximum) { return std::clamp(30.0, minimum, maximum); } int media::bestFormat(const QList &formats) { diff --git a/src/mediautils.h b/src/mediautils.h index 17645d3..a45ddc1 100644 --- a/src/mediautils.h +++ b/src/mediautils.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "edit.h" @@ -11,6 +12,12 @@ namespace media { struct Format { QSize size; double minFps; double maxFps; int pixelFormat; }; int bestFormat(const QList &formats); QCameraFormat bestCameraFormat(const QCameraDevice &device); +// V4L2 colorimetry to Qt: the YCbCr encoding the driver reports, falling back to its colorspace default. +QVideoFrameFormat::ColorSpace colorSpace(int v4l2Colorspace, int v4l2Encoding); +// Asks the V4L2 device (e.g. "/dev/video4") which YCbCr encoding it is delivering. +QVideoFrameFormat::ColorSpace cameraColorSpace(const QByteArray &device); +// Shares the source's image planes under a new format, with independent frame metadata. +QVideoFrame withFormat(const QVideoFrame &source, const QVideoFrameFormat &format); double targetFps(double minimum, double maximum); double peak(const QByteArray &data, const QAudioFormat &format); QString copyAtomically(const QString &source, const QString &destination); diff --git a/src/writer.cpp b/src/writer.cpp index 3a29664..e8e4c37 100644 --- a/src/writer.cpp +++ b/src/writer.cpp @@ -1,37 +1,9 @@ #include "writer.h" #include #include -#include +#include "mediautils.h" #include -namespace { -// QVideoFrame copies share timestamp metadata as well as pixels. Give recording -// frames independent metadata without copying their image planes or altering -// the live preview (or an already queued copy of a pause's final frame). -class FrameBuffer : public QAbstractVideoBuffer { -public: - explicit FrameBuffer(const QVideoFrame &source) : m_source(source) {} - ~FrameBuffer() override { unmap(); } - QVideoFrameFormat format() const override { return m_source.surfaceFormat(); } - MapData map(QVideoFrame::MapMode mode) override { - MapData result; - if(!m_source.map(mode)) return result; - m_mapped=true; - result.planeCount=m_source.planeCount(); - for(int i=0;i::max(), 0}}; @@ -138,7 +110,8 @@ void Writer::videoAt(QVideoFrame frame, qint64 position) { const qint64 timestamp = qRound64(frameIndex * 1000000.0 / m_fps); if (timestamp <= m_lastVideo) return; m_tailFrame = frame; - frame = QVideoFrame(std::make_unique(frame)); + // Independent timestamps for the recording copy, without touching the preview's frame. + frame = media::withFormat(frame, frame.surfaceFormat()); frame.setStartTime(timestamp); frame.setEndTime(qRound64((frameIndex + 1) * 1000000.0 / m_fps)); frame.setStreamFrameRate(m_fps); diff --git a/tests/backend_tests.cpp b/tests/backend_tests.cpp index 19745fe..55dc2eb 100644 --- a/tests/backend_tests.cpp +++ b/tests/backend_tests.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -43,6 +44,18 @@ private slots: QCOMPARE(media::bestFormat({}),-1); QCOMPARE(media::targetFps(15,15),15.0); } + void cameraColorSpace() { + // Cam Link 4K: sRGB colorspace, explicit BT.709 encoding. + QCOMPARE(media::colorSpace(V4L2_COLORSPACE_SRGB,V4L2_YCBCR_ENC_709),QVideoFrameFormat::ColorSpace_BT709); + QCOMPARE(media::colorSpace(V4L2_COLORSPACE_SRGB,V4L2_YCBCR_ENC_DEFAULT),QVideoFrameFormat::ColorSpace_BT601); + QCOMPARE(media::colorSpace(V4L2_COLORSPACE_REC709,V4L2_YCBCR_ENC_DEFAULT),QVideoFrameFormat::ColorSpace_BT709); + QCOMPARE(media::cameraColorSpace("/nonexistent"),QVideoFrameFormat::ColorSpace_Undefined); + QImage image(4,4,QImage::Format_RGB32); + auto format=QVideoFrame(image).surfaceFormat(); format.setColorSpace(QVideoFrameFormat::ColorSpace_BT709); + const auto frame=media::withFormat(QVideoFrame(image),format); + QCOMPARE(frame.surfaceFormat().colorSpace(),QVideoFrameFormat::ColorSpace_BT709); + QCOMPARE(frame.toImage().size(),QSize(4,4)); + } void clockExcludesPauses() { TakeClock c; c.start(1000000); QVERIFY(!c.position(999999)); From 653c79b07a5189ae9eb831f628ffb63c8e8bbd1e Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 10 Nov 2026 18:00:11 +0100 Subject: [PATCH 2/2] Keep capture times on color-corrected frames Co-Authored-By: Claude Opus 5.5 --- src/mediautils.cpp | 5 ++++- src/mediautils.h | 2 +- tests/backend_tests.cpp | 4 +++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/mediautils.cpp b/src/mediautils.cpp index ac35eaa..6de08fe 100644 --- a/src/mediautils.cpp +++ b/src/mediautils.cpp @@ -42,7 +42,10 @@ class FrameBuffer : public QAbstractVideoBuffer { } QVideoFrame media::withFormat(const QVideoFrame &source, const QVideoFrameFormat &format) { - return QVideoFrame(std::make_unique(source, format)); + QVideoFrame frame(std::make_unique(source, format)); + frame.setStartTime(source.startTime()); + frame.setEndTime(source.endTime()); + return frame; } QVideoFrameFormat::ColorSpace media::colorSpace(int v4l2Colorspace, int v4l2Encoding) { if (v4l2Encoding == V4L2_YCBCR_ENC_DEFAULT) v4l2Encoding = V4L2_MAP_YCBCR_ENC_DEFAULT(v4l2Colorspace); diff --git a/src/mediautils.h b/src/mediautils.h index a45ddc1..ebb8a39 100644 --- a/src/mediautils.h +++ b/src/mediautils.h @@ -16,7 +16,7 @@ QCameraFormat bestCameraFormat(const QCameraDevice &device); QVideoFrameFormat::ColorSpace colorSpace(int v4l2Colorspace, int v4l2Encoding); // Asks the V4L2 device (e.g. "/dev/video4") which YCbCr encoding it is delivering. QVideoFrameFormat::ColorSpace cameraColorSpace(const QByteArray &device); -// Shares the source's image planes under a new format, with independent frame metadata. +// Shares the source's image planes and capture times under a new format; later metadata changes stay independent. QVideoFrame withFormat(const QVideoFrame &source, const QVideoFrameFormat &format); double targetFps(double minimum, double maximum); double peak(const QByteArray &data, const QAudioFormat &format); diff --git a/tests/backend_tests.cpp b/tests/backend_tests.cpp index 55dc2eb..f5b4b7a 100644 --- a/tests/backend_tests.cpp +++ b/tests/backend_tests.cpp @@ -52,8 +52,10 @@ private slots: QCOMPARE(media::cameraColorSpace("/nonexistent"),QVideoFrameFormat::ColorSpace_Undefined); QImage image(4,4,QImage::Format_RGB32); auto format=QVideoFrame(image).surfaceFormat(); format.setColorSpace(QVideoFrameFormat::ColorSpace_BT709); - const auto frame=media::withFormat(QVideoFrame(image),format); + QVideoFrame source(image); source.setStartTime(1234); source.setEndTime(5678); + const auto frame=media::withFormat(source,format); QCOMPARE(frame.surfaceFormat().colorSpace(),QVideoFrameFormat::ColorSpace_BT709); + QCOMPARE(frame.startTime(),1234); QCOMPARE(frame.endTime(),5678); QCOMPARE(frame.toImage().size(),QSize(4,4)); } void clockExcludesPauses() {