From c3d217e51c58b3570876da40fcf8c6f4d46421c6 Mon Sep 17 00:00:00 2001 From: andres mena Date: Mon, 5 Oct 2026 23:44:36 -0500 Subject: [PATCH] Encode MJPEG cameras as 4:2:0 and add a resolution menu Cameras that offer their top resolution only as MJPEG delivered Format_Jpeg frames, which Qt's FFmpeg encoder decoded to 4:4:4. Hardware H.264 encoders reject that, so Qt fell back to software x264 at 4:4:4 10-bit, which cannot keep up at 1080p on modest CPUs and produced files most players cannot play. Decode those frames in Writer on a small worker pool (libavcodec MJPEG, then libswscale to NV12) and keep them ordered before they reach the encoder, so the GUI thread and camera delivery are no longer blocked. Output is standard High-profile yuv420p. Add a Resolution menu beside Camera and Microphone listing the sizes the camera offers. The maximum stays the default; an explicit choice is stored in settings and falls back to the maximum if the camera lacks it. Fixes #3 Co-Authored-By: Claude Sonnet 5.5 --- README.md | 3 +- src/Main.qml | 9 ++++ src/backend.cpp | 29 ++++++++++++- src/backend.h | 10 ++++- src/common.pri | 2 +- src/mediautils.cpp | 24 +++++++++-- src/mediautils.h | 3 +- src/writer.cpp | 103 +++++++++++++++++++++++++++++++++++++++++---- src/writer.h | 10 ++++- tests/ui_tests.cpp | 3 ++ 10 files changed, 178 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index d25a272..923e889 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ A simple webcam recorder for Omarchy. Choose your camera and microphone once, then press **Space** to record. Press again to pause or resume the same take. Stop the take and it opens right away in a built-in editor: trim either end, cut ranges out of the middle, then **Save**. - Remembers camera and microphone by device ID; missing inputs never silently switch. -- Automatically selects the camera's maximum advertised video resolution, preferring 30 fps at that resolution. Preview preserves the whole frame. +- Automatically selects the camera's maximum advertised video resolution, preferring 30 fps at that resolution. Preview preserves the whole frame. A resolution menu next to the camera lets slower machines choose a smaller size, which is remembered. +- MJPEG cameras are decoded off the GUI thread and encoded as ordinary 4:2:0 H.264, so high resolutions stay playable and affordable on modest CPUs. - Live microphone meter with peak hold and clipping indication, including while paused. No microphone playback through your speakers. - Explicit No audio option for silent recordings. - Live Omarchy accent syncing, including theme symlink switches. Controls and dialogs follow Hyprland's active corner rounding, including personal overrides. Dark chrome and a yellow fallback follow Omacut; button foregrounds adapt for contrast. diff --git a/src/Main.qml b/src/Main.qml index 762e683..1905889 100644 --- a/src/Main.qml +++ b/src/Main.qml @@ -160,6 +160,7 @@ ApplicationWindow { readonly property var icons: ({ camera: "M4.5 6h9a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2h-9a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2z M15.5 10.5l6-3.5v10l-6-3.5z", microphone: "M12 3a3 3 0 0 1 3 3v5a3 3 0 0 1-6 0V6a3 3 0 0 1 3-3z M5.5 11a6.5 6.5 0 0 0 13 0 M12 17.5V21", + resolution: "M4 9V5h4 M20 9V5h-4 M4 15v4h4 M20 15v4h-4", chevron: "M6 9l6 6 6-6", back: "M4 12a8 8 0 1 0 2.4-5.7 M4 4v4.5h4.5", play: "M8 4.5v15l12.5-7.5z", @@ -419,6 +420,14 @@ ApplicationWindow { onActivated: index => { backend.selectCamera(index); liveVideo.forceActiveFocus() } Accessible.name: "Camera" } + SourceChoice { + id: resolutionChoice; iconPath: win.icons.resolution + maximumTextWidth: Math.min(260, sources.width - 44) + model: backend.resolutions; currentIndex: backend.resolutionIndex + enabled: !backend.takeActive && !win.busy && backend.resolutions.length > 1 + onActivated: index => { backend.selectResolution(index); liveVideo.forceActiveFocus() } + Accessible.name: "Resolution" + } SourceChoice { id: microphoneChoice; iconPath: win.icons.microphone maximumTextWidth: Math.min(260, sources.width - 44) diff --git a/src/backend.cpp b/src/backend.cpp index 58a78fc..8227f86 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -31,6 +31,10 @@ Backend::Backend(FilePicker *picker, bool activateHardware, QObject *parent) m_root = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/recordings"; m_cameraId=m_settings.value("camera/id").toString(); m_audioId=m_settings.value("microphone/id").toString(); + { + const auto parts=m_settings.value("camera/resolution").toString().split('x'); + if(parts.size()==2) m_chosenResolution=QSize(parts[0].toInt(),parts[1].toInt()); + } m_capture.setVideoSink(&m_sink); connect(&m_sink,&QVideoSink::videoFrameChanged,this,&Backend::receiveVideo); if(m_activateHardware) { @@ -58,6 +62,10 @@ Backend::~Backend() { if(m_export) { m_export->disconnect(this); m_export->kill(); m_export->waitForFinished(); QFile::remove(m_exportPartial); } } int Backend::cameraIndex() const { return indexOf(m_cameras,m_cameraId); } +int Backend::resolutionIndex() const { + for(int i=0;i=m_cameras.size() || m_state=="saving" || m_probing) return; m_cameraId=m_cameras[index].toMap()["id"].toString(); activateSources(); } +void Backend::selectResolution(int index) { + if(m_writer || index<0 || index>=m_resolutions.size() || m_state=="saving" || m_probing) return; + const auto choice=m_resolutions[index].toMap(); + // The first entry is the maximum, which is stored as "no preference". + m_chosenResolution=index==0 ? QSize() : QSize(choice["width"].toInt(),choice["height"].toInt()); + if(index==0) m_settings.remove("camera/resolution"); + else m_settings.setValue("camera/resolution",QString("%1x%2").arg(m_chosenResolution.width()).arg(m_chosenResolution.height())); + activateSources(); +} void Backend::selectMicrophone(int index) { if(m_writer || index<0 || index>=m_microphones.size() || m_state=="saving" || m_probing) return; m_audioId=m_microphones[index].toMap()["id"].toString(); activateSources(); diff --git a/src/backend.h b/src/backend.h index 569b755..bda4847 100644 --- a/src/backend.h +++ b/src/backend.h @@ -1,4 +1,5 @@ #pragma once +#include #include #include #include @@ -25,6 +26,8 @@ class Backend : public QObject { Q_PROPERTY(QVariantList cameras READ cameras NOTIFY devicesChanged) Q_PROPERTY(QVariantList microphones READ microphones NOTIFY devicesChanged) Q_PROPERTY(int cameraIndex READ cameraIndex NOTIFY devicesChanged) + Q_PROPERTY(QVariantList resolutions READ resolutions NOTIFY resolutionsChanged) + Q_PROPERTY(int resolutionIndex READ resolutionIndex NOTIFY resolutionsChanged) Q_PROPERTY(int microphoneIndex READ microphoneIndex NOTIFY devicesChanged) Q_PROPERTY(bool ready READ ready NOTIFY changed) Q_PROPERTY(bool audioEnabled READ audioEnabled NOTIFY changed) @@ -55,6 +58,8 @@ class Backend : public QObject { QString message() const { return m_message; } QString formatLabel() const { return m_formatLabel; } QVariantList cameras() const { return m_cameras; } + QVariantList resolutions() const { return m_resolutions; } + int resolutionIndex() const; QVariantList microphones() const { return m_microphones; } int cameraIndex() const; int microphoneIndex() const; @@ -81,6 +86,7 @@ class Backend : public QObject { edit::Clips currentEdit() const { return m_edit; } Q_INVOKABLE void setPreview(QObject *sink); Q_INVOKABLE void selectCamera(int index); + Q_INVOKABLE void selectResolution(int index); Q_INVOKABLE void selectMicrophone(int index); Q_INVOKABLE void retry(); Q_INVOKABLE void toggleRecording(); @@ -108,6 +114,7 @@ class Backend : public QObject { signals: void changed(); void devicesChanged(); + void resolutionsChanged(); void meterChanged(); void recordingsChanged(); void editChanged(); @@ -155,7 +162,8 @@ class Backend : public QObject { bool m_activateHardware; QString m_pendingDestination; Writer *m_writer = nullptr; - QVariantList m_cameras, m_microphones, m_recordings; + QVariantList m_cameras, m_microphones, m_recordings, m_resolutions; + QSize m_chosenResolution; // empty means the camera's maximum QString m_cameraId, m_audioId, m_state = "starting", m_message, m_formatLabel; QString m_root, m_takeId, m_clipPath, m_clipFileName; QString m_interruption, m_status; diff --git a/src/common.pri b/src/common.pri index f7936b5..b10b4e8 100644 --- a/src/common.pri +++ b/src/common.pri @@ -1,5 +1,5 @@ CONFIG += link_pkgconfig -PKGCONFIG += libpulse +PKGCONFIG += libpulse libswscale libavcodec libavutil HEADERS += $$PWD/theme.h $$PWD/mediautils.h $$PWD/writer.h $$PWD/backend.h \ $$PWD/filepicker.h $$PWD/portalfilepicker.h $$PWD/audiocapture.h \ $$PWD/edit.h $$PWD/thumbnails.h diff --git a/src/mediautils.cpp b/src/mediautils.cpp index 37fb5eb..f88001d 100644 --- a/src/mediautils.cpp +++ b/src/mediautils.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -28,12 +29,29 @@ int media::bestFormat(const QList &formats) { } return best; } -QCameraFormat media::bestCameraFormat(const QCameraDevice &device) { +QCameraFormat media::bestCameraFormat(const QCameraDevice &device, const QSize &preferred) { const auto formats = device.videoFormats(); QList choices; - for (const auto &f : formats) choices.append({f.resolution(), f.minFrameRate(), f.maxFrameRate(), int(f.pixelFormat())}); + QList origin; + for (int i = 0; i < formats.size(); ++i) { + const auto &f = formats[i]; + if (!preferred.isEmpty() && f.resolution() != preferred) continue; + choices.append({f.resolution(), f.minFrameRate(), f.maxFrameRate(), int(f.pixelFormat())}); + origin.append(i); + } + // An explicit resolution that this camera lacks falls back to the maximum. + if (choices.isEmpty() && !preferred.isEmpty()) return bestCameraFormat(device); const int index = bestFormat(choices); - return index < 0 ? QCameraFormat() : formats[index]; + return index < 0 ? QCameraFormat() : formats[origin[index]]; +} +QList media::cameraResolutions(const QCameraDevice &device) { + QList sizes; + for (const auto &f : device.videoFormats()) + if (!f.resolution().isEmpty() && f.maxFrameRate() > 0 && !sizes.contains(f.resolution())) sizes.append(f.resolution()); + std::sort(sizes.begin(), sizes.end(), [](const QSize &a, const QSize &b) { + return qint64(a.width()) * a.height() > qint64(b.width()) * b.height(); + }); + return sizes; } double media::peak(const QByteArray &data, const QAudioFormat &format) { double result = 0; diff --git a/src/mediautils.h b/src/mediautils.h index 17645d3..d6bbae7 100644 --- a/src/mediautils.h +++ b/src/mediautils.h @@ -10,7 +10,8 @@ namespace media { struct Format { QSize size; double minFps; double maxFps; int pixelFormat; }; int bestFormat(const QList &formats); -QCameraFormat bestCameraFormat(const QCameraDevice &device); +QCameraFormat bestCameraFormat(const QCameraDevice &device, const QSize &preferred = {}); +QList cameraResolutions(const QCameraDevice &device); 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 8d07beb..daf8a5a 100644 --- a/src/writer.cpp +++ b/src/writer.cpp @@ -1,4 +1,12 @@ #include "writer.h" +#include +#include +#include +extern "C" { +#include +#include +#include +} #include #include #include @@ -30,6 +38,54 @@ class FrameBuffer : public QAbstractVideoBuffer { QVideoFrame m_source; bool m_mapped=false; }; + +// Webcams deliver MJPEG, which Qt's encoder decodes to 4:4:4 that hardware H.264 +// encoders reject and software x264 struggles with at 1080p. Hand the encoder +// plain 8-bit 4:2:0 NV12 instead, which also plays everywhere. Runs on a worker. +QVideoFrame jpegToNv12(const QByteArray &jpeg, const QSize &size) { + const AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG); + if (!codec) return {}; + AVCodecContext *decoder = avcodec_alloc_context3(codec); + AVPacket *packet = av_packet_alloc(); + AVFrame *decoded = av_frame_alloc(); + QVideoFrame output; + if (decoder && packet && decoded && avcodec_open2(decoder, codec, nullptr) == 0 + && av_new_packet(packet, jpeg.size()) == 0) { + memcpy(packet->data, jpeg.constData(), jpeg.size()); + if (avcodec_send_packet(decoder, packet) == 0 && avcodec_receive_frame(decoder, decoded) == 0 + && decoded->width == size.width() && decoded->height == size.height()) { + QVideoFrameFormat format(size, QVideoFrameFormat::Format_NV12); + format.setColorSpace(QVideoFrameFormat::ColorSpace_BT601); + format.setColorRange(QVideoFrameFormat::ColorRange_Video); + QVideoFrame frame(format); + if (frame.map(QVideoFrame::WriteOnly)) { + thread_local SwsContext *context = nullptr; + const bool fullRange = decoded->color_range == AVCOL_RANGE_JPEG || decoded->format == AV_PIX_FMT_YUVJ420P + || decoded->format == AV_PIX_FMT_YUVJ422P || decoded->format == AV_PIX_FMT_YUVJ444P; + AVPixelFormat source = AVPixelFormat(decoded->format); + if (source == AV_PIX_FMT_YUVJ420P) source = AV_PIX_FMT_YUV420P; + else if (source == AV_PIX_FMT_YUVJ422P) source = AV_PIX_FMT_YUV422P; + else if (source == AV_PIX_FMT_YUVJ444P) source = AV_PIX_FMT_YUV444P; + context = sws_getCachedContext(context, size.width(), size.height(), source, + size.width(), size.height(), AV_PIX_FMT_NV12, SWS_BILINEAR, nullptr, nullptr, nullptr); + uint8_t *planes[2] = {frame.bits(0), frame.bits(1)}; + const int strides[2] = {frame.bytesPerLine(0), frame.bytesPerLine(1)}; + int rows = 0; + if (context) { + const int *coefficients = sws_getCoefficients(SWS_CS_ITU601); + sws_setColorspaceDetails(context, coefficients, fullRange ? 1 : 0, coefficients, 0, 0, 1 << 16, 1 << 16); + rows = sws_scale(context, decoded->data, decoded->linesize, 0, size.height(), planes, strides); + } + frame.unmap(); + if (rows == size.height()) output = frame; + } + } + } + av_frame_free(&decoded); + av_packet_free(&packet); + avcodec_free_context(&decoder); + return output; +} } void TakeClock::start(qint64 now) { @@ -75,6 +131,7 @@ QList TakeClock::joins() const { } Writer::Writer(QObject *parent) : QObject(parent) { + m_convertPool.setMaxThreadCount(3); m_session.setRecorder(&m_recorder); m_flushTimeout.setSingleShot(true); m_flushTimeout.setInterval(10000); @@ -93,6 +150,7 @@ static QMediaFormat recordingFormat(bool audio) { if (audio) f.setAudioCodec(QMediaFormat::AudioCodec::AAC); return f; } +Writer::~Writer() { m_convertPool.waitForDone(); } bool Writer::supported(bool audio) { return recordingFormat(audio).isSupported(QMediaFormat::Encode); } bool Writer::start(const QString &path, const QVideoFrameFormat &format, double fps, const QAudioFormat &audioFormat, qint64 now) { @@ -137,16 +195,32 @@ void Writer::videoAt(QVideoFrame frame, qint64 position) { const qint64 frameIndex = qRound64(position * m_fps / 1000000.0); const qint64 timestamp = qRound64(frameIndex * 1000000.0 / m_fps); if (timestamp <= m_lastVideo) return; - m_tailFrame = frame; - frame = QVideoFrame(std::make_unique(frame)); - frame.setStartTime(timestamp); - frame.setEndTime(qRound64((frameIndex + 1) * 1000000.0 / m_fps)); - frame.setStreamFrameRate(m_fps); - m_lastVideo = timestamp; // Bounded native-frame references; never build an unbounded 4K frame queue. if (m_frames.size() >= 6) { fail("Video encoding cannot keep up at this camera's maximum resolution. The take has been stopped and kept."); return; } - m_frames.enqueue(frame); - drain(); + m_tailFrame = frame; + m_lastVideo = timestamp; + auto job = std::make_shared(); + bool queued = false; + if (frame.pixelFormat() == QVideoFrameFormat::Format_Jpeg) { + QByteArray jpeg; + if (frame.map(QVideoFrame::ReadOnly)) { + jpeg = QByteArray(reinterpret_cast(frame.bits(0)), frame.mappedBytes(0)); + frame.unmap(); + } + if (jpeg.isEmpty()) return; + const QSize size = frame.size(); + queued = true; + m_convertPool.start([this, job, jpeg, size] { + job->frame = jpegToNv12(jpeg, size); + job->ready.store(true, std::memory_order_release); + QMetaObject::invokeMethod(this, &Writer::drain, Qt::QueuedConnection); + }); + } else { + job->frame = QVideoFrame(std::make_unique(frame)); + job->ready.store(true, std::memory_order_release); + } + m_frames.enqueue({job, timestamp, qRound64((frameIndex + 1) * 1000000.0 / m_fps), {}}); + if (!queued) drain(); } void Writer::audio(QByteArray data, qint64 capturedAt) { if (m_stopping || m_failed || !m_audio || data.isEmpty()) return; @@ -194,7 +268,18 @@ void Writer::appendAudio(const QByteArray &data) { void Writer::drain() { if (m_draining || m_failed) return; m_draining = true; - while (!m_frames.isEmpty() && m_video->sendVideoFrame(m_frames.head())) m_frames.dequeue(); + while (!m_frames.isEmpty() && m_frames.head().job->ready.load(std::memory_order_acquire)) { + auto &head = m_frames.head(); + if (!head.job->frame.isValid()) { m_frames.dequeue(); continue; } // undecodable camera frame + if (!head.frame.isValid()) { + head.frame = QVideoFrame(std::make_unique(head.job->frame)); + head.frame.setStartTime(head.start); + head.frame.setEndTime(head.end); + head.frame.setStreamFrameRate(m_fps); + } + if (!m_video->sendVideoFrame(head.frame)) break; + m_frames.dequeue(); + } while (!m_buffers.isEmpty() && m_audio->sendAudioBuffer(m_buffers.head())) { m_queuedAudioUs -= m_buffers.head().duration(); m_buffers.dequeue(); } diff --git a/src/writer.h b/src/writer.h index 9838276..cb8bd53 100644 --- a/src/writer.h +++ b/src/writer.h @@ -6,6 +6,9 @@ #include #include #include +#include +#include +#include #include #include @@ -34,6 +37,7 @@ class Writer : public QObject { Q_OBJECT public: explicit Writer(QObject *parent = nullptr); + ~Writer() override; bool start(const QString &path, const QVideoFrameFormat &videoFormat, double fps, const QAudioFormat &audioFormat, qint64 now); void video(QVideoFrame frame, qint64 capturedAt); @@ -58,7 +62,11 @@ class Writer : public QObject { QVideoFrameInput *m_video = nullptr; QAudioBufferInput *m_audio = nullptr; QAudioFormat m_audioFormat; - QQueue m_frames; + // A video frame waiting for its (possibly off-thread) conversion to finish. + struct Job { std::atomic ready{false}; QVideoFrame frame; }; + struct Pending { std::shared_ptr job; qint64 start = 0, end = 0; QVideoFrame frame; }; + QQueue m_frames; + QThreadPool m_convertPool; QVideoFrame m_tailFrame; QQueue m_buffers; QTimer m_flushTimeout; diff --git a/tests/ui_tests.cpp b/tests/ui_tests.cpp index 9649ba1..67439c6 100644 --- a/tests/ui_tests.cpp +++ b/tests/ui_tests.cpp @@ -28,6 +28,8 @@ QtObject { property var cameras: [{label: "Test camera"}] property var microphones: [{label: "Test microphone"}] property int cameraIndex: 0 + property var resolutions: [{label: "1920 × 1080 maximum"}, {label: "1280 × 720"}] + property int resolutionIndex: 0 property int microphoneIndex: 0 property bool ready: state === "ready" property bool audioEnabled: true @@ -63,6 +65,7 @@ QtObject { function toggleRecording() { toggles++; state = state === "ready" || state === "paused" ? "recording" : "paused"; changed() } function finish() { state = "finished"; changed() } function selectCamera(i) {} + function selectResolution(i) {} function selectMicrophone(i) {} function newRecording() { newRecordings++; state = "ready"; changed() } function closeTake() {}