From 305588ef6b2fdf897eab12c7ae3a01aba9a2c3c5 Mon Sep 17 00:00:00 2001 From: merovex Date: Fri, 9 Oct 2026 16:41:41 -0400 Subject: [PATCH 1/2] Stop dropping frames when camera jitter straddles the frame grid The output frame grid's phase is set by when Record is pressed, not by the camera. When the camera's frames land near the midpoint between output frames, normal jitter rounds consecutive frames onto one slot and the second is dropped, silently losing about a third of the take's frames. Such a frame now takes the next slot when that is at most three quarters of a frame early; a camera faster than the output rate still has its extra frames dropped. Fixes #9 Co-Authored-By: Claude Opus 5.5 --- src/writer.cpp | 8 +++++++- src/writer.h | 2 +- tests/backend_tests.cpp | 20 ++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/writer.cpp b/src/writer.cpp index 3a29664..93a73c3 100644 --- a/src/writer.cpp +++ b/src/writer.cpp @@ -134,9 +134,15 @@ void Writer::video(QVideoFrame frame, qint64 capturedAt) { void Writer::videoAt(QVideoFrame frame, qint64 position) { // Quantize to the output rate instead of rejecting short callback intervals: // real webcams deliver jittery batches, which must not halve the frame rate. - const qint64 frameIndex = qRound64(position * m_fps / 1000000.0); + qint64 frameIndex = qRound64(position * m_fps / 1000000.0); + // When the camera's phase sits near the midpoint between output frames, its + // jitter rounds consecutive frames onto one slot. Take the next slot instead, + // provided that is at most three quarters of a frame early. + if (frameIndex <= m_lastIndex && (m_lastIndex + 1) * 1000000.0 / m_fps - position <= 0.75 * 1000000.0 / m_fps) + frameIndex = m_lastIndex + 1; const qint64 timestamp = qRound64(frameIndex * 1000000.0 / m_fps); if (timestamp <= m_lastVideo) return; + m_lastIndex = frameIndex; m_tailFrame = frame; frame = QVideoFrame(std::make_unique(frame)); frame.setStartTime(timestamp); diff --git a/src/writer.h b/src/writer.h index 9838276..76382b5 100644 --- a/src/writer.h +++ b/src/writer.h @@ -63,7 +63,7 @@ class Writer : public QObject { QQueue m_buffers; QTimer m_flushTimeout; TakeClock m_clock; - qint64 m_lastVideo = -1; + qint64 m_lastVideo = -1, m_lastIndex = -1; qint64 m_frameDuration = 33333; double m_fps = 30; qint64 m_queuedAudioUs = 0; diff --git a/tests/backend_tests.cpp b/tests/backend_tests.cpp index 19745fe..0ca7544 100644 --- a/tests/backend_tests.cpp +++ b/tests/backend_tests.cpp @@ -324,6 +324,26 @@ private slots: QCOMPARE(backend.clips().size(),1); backend.closeTake(); } + void jitteryCameraKeepsEveryFrame() { + // A 30 fps camera whose frames fall midway between output slots, with + // the alternating 28/36 ms intervals real webcams show. + QTemporaryDir directory; + const auto path=directory.filePath("jitter.mp4"); + Writer writer; QSignalSpy ended(&writer,&Writer::finished), errors(&writer,&Writer::failed); + QVERIFY(writer.start(path,QVideoFrameFormat(QSize(64,64),QVideoFrameFormat::Format_BGRA8888),30,QAudioFormat(),0)); + QImage image(64,64,QImage::Format_RGB32); image.fill(Qt::gray); + qint64 captured=15000; + for(int i=0;i<60;++i) { + writer.video(QVideoFrame(image),captured); + captured+=(i%3==2)?28000:36000; + QTest::qWait(5); + } + writer.finish(); QTRY_VERIFY_WITH_TIMEOUT(!ended.isEmpty(),15000); + QVERIFY2(errors.isEmpty(),errors.isEmpty()?"":qPrintable(errors.first().first().toString())); + QProcess streams; streams.start("ffprobe",{"-v","error","-select_streams","v:0","-show_entries","stream=nb_frames","-of","csv=p=0",path}); + QVERIFY(streams.waitForFinished()); + QCOMPARE(streams.readAllStandardOutput().trimmed().toInt(),60); + } void delayedCaptureStaysInSync() { QTemporaryDir directory; const auto path=directory.filePath("sync.mp4"); From 9d66b42874b98b17c917618b131907751d13a7bd Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 12 Nov 2026 17:21:35 +0100 Subject: [PATCH 2/2] Keep the repeated tail frame from moving past the end of the take Only camera frames may take the next slot after a collision. finish() repeats the last frame to fill the take's final slot, and moving that repeat forward added a frame after the audio ended. Co-Authored-By: Claude Opus 5.5 --- src/writer.cpp | 6 +++--- src/writer.h | 3 ++- tests/backend_tests.cpp | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/writer.cpp b/src/writer.cpp index 7af757d..6fa6d6c 100644 --- a/src/writer.cpp +++ b/src/writer.cpp @@ -290,16 +290,16 @@ void Writer::video(QVideoFrame frame, qint64 capturedAt) { if (m_stopping || m_failed || !frame.isValid()) return; const auto time = m_clock.position(capturedAt); if (!time) return; - videoAt(frame, *time); + videoAt(frame, *time, true); } -void Writer::videoAt(QVideoFrame frame, qint64 position) { +void Writer::videoAt(QVideoFrame frame, qint64 position, bool fromCamera) { // Quantize to the output rate instead of rejecting short callback intervals: // real webcams deliver jittery batches, which must not halve the frame rate. qint64 frameIndex = qRound64(position * m_fps / 1000000.0); // When the camera's phase sits near the midpoint between output frames, its // jitter rounds consecutive frames onto one slot. Take the next slot instead, // provided that is at most three quarters of a frame early. - if (frameIndex <= m_lastIndex && (m_lastIndex + 1) * 1000000.0 / m_fps - position <= 0.75 * 1000000.0 / m_fps) + if (fromCamera && frameIndex <= m_lastIndex && (m_lastIndex + 1) * 1000000.0 / m_fps - position <= 0.75 * 1000000.0 / m_fps) frameIndex = m_lastIndex + 1; const qint64 timestamp = qRound64(frameIndex * 1000000.0 / m_fps); if (timestamp <= m_lastVideo) return; diff --git a/src/writer.h b/src/writer.h index 76382b5..961f327 100644 --- a/src/writer.h +++ b/src/writer.h @@ -52,7 +52,8 @@ class Writer : public QObject { void fail(const QString &message); void appendAudio(const QByteArray &data); void padAudioTo(qint64 time); - void videoAt(QVideoFrame frame, qint64 position); + // fromCamera lets a jittered frame take the next free slot; the repeated tail frame must not. + void videoAt(QVideoFrame frame, qint64 position, bool fromCamera = false); QMediaCaptureSession m_session; QMediaRecorder m_recorder; QVideoFrameInput *m_video = nullptr; diff --git a/tests/backend_tests.cpp b/tests/backend_tests.cpp index 19b61d0..2de6fc1 100644 --- a/tests/backend_tests.cpp +++ b/tests/backend_tests.cpp @@ -467,6 +467,24 @@ private slots: QVERIFY(streams.waitForFinished()); QCOMPARE(streams.readAllStandardOutput().trimmed().toInt(),60); } + void stoppingAddsNoFrameAfterTheTake() { + // Finishing repeats the last camera frame to fill the take's final slot. + // That repeat must never move into a later slot, past where the take ends. + QTemporaryDir directory; + const auto path=directory.filePath("tail.mp4"); + Writer writer; QSignalSpy ended(&writer,&Writer::finished), errors(&writer,&Writer::failed); + QVERIFY(writer.start(path,QVideoFrameFormat(QSize(320,240),QVideoFrameFormat::Format_BGRA8888),30,QAudioFormat(),0)); + QImage image(320,240,QImage::Format_RGB32); image.fill(Qt::gray); + // The first frame opens the encoder; give it that moment before the rest arrive. + writer.video(QVideoFrame(image),0); QTest::qWait(300); + for(int i=1;i<29;++i) { writer.video(QVideoFrame(image),qRound64(i*1000000.0/30)); QTest::qWait(33); } + writer.video(QVideoFrame(image),974000); QTest::qWait(33); + writer.pause(1009000); writer.finish(); QTRY_VERIFY_WITH_TIMEOUT(!ended.isEmpty(),15000); + QVERIFY2(errors.isEmpty(),errors.isEmpty()?"":qPrintable(errors.first().first().toString())); + QProcess streams; streams.start("ffprobe",{"-v","error","-select_streams","v:0","-count_frames","-show_entries","stream=nb_read_frames","-of","csv=p=0",path}); + QVERIFY(streams.waitForFinished()); + QCOMPARE(streams.readAllStandardOutput().trimmed().toInt(),30); + } void jpegFramesRecordAs420() { // Webcams send their largest modes as 4:2:2 MJPEG. Recording them as // RGB made Qt pick a 12-bit 4:4:4 target that NVENC rejects, leaving