diff --git a/src/backend.cpp b/src/backend.cpp index 4d25514..cc3170a 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -182,6 +182,10 @@ void Backend::receiveVideo(QVideoFrame frame) { auto format=frame.surfaceFormat(); format.setColorSpace(m_colorSpace); frame=media::withFormat(frame,format); } + // uvcvideo flags a frame the camera cut short, but Qt passes it on regardless, + // and it decodes as a coloured grid. Skip it, and the recording holds the frame + // before. A camera that sends nothing else still times out as stalled. + if(!media::completeFrame(frame)) return; m_lastVideoAt=now(); if(frame.size()!=m_cameraFormat.resolution()) { sourceFailed(tr("The camera did not provide its maximum resolution. Choose another source or Retry.")); return; diff --git a/src/mediautils.cpp b/src/mediautils.cpp index b85c2ba..845df4b 100644 --- a/src/mediautils.cpp +++ b/src/mediautils.cpp @@ -72,6 +72,22 @@ QString media::chooseSource(const QStringList &connected, const QString &preferr if (connected.isEmpty() || connected.contains(remembered)) return remembered; return connected.contains(preferred) ? preferred : connected.first(); } +bool media::completeJpeg(const uchar *data, qsizetype size) { + if (size < 4 || data[0] != 0xFF || data[1] != 0xD8) return false; + // Some cameras pad the buffer past EOI, so look for it near the end rather than at it. + // Up to 64 bytes of padding: EOI may start 66 bytes before the end. + for (qsizetype i = size - 2; i >= std::max(2, size - 66); --i) + if (data[i] == 0xFF && data[i + 1] == 0xD9) return true; + return false; +} +bool media::completeFrame(const QVideoFrame &frame) { + if (frame.pixelFormat() != QVideoFrameFormat::Format_Jpeg) return true; + QVideoFrame mapped(frame); + if (!mapped.map(QVideoFrame::ReadOnly)) return false; + const bool complete = completeJpeg(mapped.bits(0), mapped.mappedBytes(0)); + mapped.unmap(); + return complete; +} void media::useFfmpeg() { qputenv("QT_MEDIA_BACKEND", "ffmpeg"); // Loading the backend tries every hardware API before returning, about two diff --git a/src/mediautils.h b/src/mediautils.h index e305ad9..d05cd51 100644 --- a/src/mediautils.h +++ b/src/mediautils.h @@ -18,6 +18,10 @@ QVideoFrameFormat::ColorSpace colorSpace(int v4l2Colorspace, int v4l2Encoding); QVideoFrameFormat::ColorSpace cameraColorSpace(const QByteArray &device); // 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); +// A whole JPEG: starts with SOI and ends with EOI, allowing a little padding after it. +bool completeJpeg(const uchar *data, qsizetype size); +// False for an MJPEG frame the camera cut short; other frames always pass. +bool completeFrame(const QVideoFrame &frame); // Chooses Qt's FFmpeg backend, which takes custom timestamped frames, before anything loads it. void useFfmpeg(); double targetFps(double minimum, double maximum); diff --git a/tests/backend_tests.cpp b/tests/backend_tests.cpp index 9c0e13a..78f145e 100644 --- a/tests/backend_tests.cpp +++ b/tests/backend_tests.cpp @@ -68,6 +68,25 @@ private slots: // A camera whose every mode is slower than 15 fps still records at its largest size. QCOMPARE(media::bestFormat({{{1280,720},10,10,0}, {{640,480},10,12,0}}), 0); } + void truncatedJpegIsSkipped() { + // An MJPEG frame the camera cut short has no end-of-image marker. + QTemporaryDir directory; + const auto path=directory.filePath("frame.jpg"); + QProcess encode; encode.start("ffmpeg",{"-v","error","-f","lavfi","-i","color=red:s=320x240","-frames:v","1",path}); + QVERIFY(encode.waitForFinished(20000)); QCOMPARE(encode.exitCode(),0); + QFile file(path); QVERIFY(file.open(QIODevice::ReadOnly)); + const auto jpeg=file.readAll(); + auto complete=[](const QByteArray &bytes) { return media::completeJpeg(reinterpret_cast(bytes.constData()),bytes.size()); }; + QVERIFY(complete(jpeg)); + QVERIFY(complete(jpeg+QByteArray(64,'\0'))); + QVERIFY(!complete(jpeg+QByteArray(65,'\0'))); + QVERIFY(!complete(jpeg.left(jpeg.size()*2/3))); + QVERIFY(!complete(QByteArray("\xFF\xD9",2))); + const auto frame=[](const QByteArray &bytes) { return QVideoFrame(std::make_unique(bytes,QSize(320,240))); }; + QVERIFY(media::completeFrame(frame(jpeg))); + QVERIFY(!media::completeFrame(frame(jpeg.left(jpeg.size()/2)))); + QImage image(4,4,QImage::Format_RGB32); QVERIFY(media::completeFrame(QVideoFrame(image))); + } void cameraColorSpace() { // Cam Link 4K: sRGB colorspace, explicit BT.709 encoding. QCOMPARE(media::colorSpace(V4L2_COLORSPACE_SRGB,V4L2_YCBCR_ENC_709),QVideoFrameFormat::ColorSpace_BT709);