Skip to content

Sustain 4K30 recording: FFmpeg encode, H.264 level, and a safe JPEG decode - #16

Closed
gigasolo wants to merge 4 commits into
omacom:masterfrom
gigasolo:sustain-4k30-recording
Closed

gigasolo wants to merge 4 commits into
omacom:masterfrom
gigasolo:sustain-4k30-recording

Conversation

@gigasolo

@gigasolo gigasolo commented Nov 3, 2026

Copy link
Copy Markdown

Fixes #8. Also addresses the encoder half of #3: its 4:4:4 output and software-only encoding.

This is #11 plus two commits. The first two commits are Merovex's, unchanged, from fix-4k-recording. If #11 takes the last two, this pull request can close.

What changed

From #11:

  • JPEG decode to 4:2:0. Webcams deliver 4K30 as MJPEG. Each frame is decoded with libjpeg-turbo straight to 8-bit 4:2:0, so the recording is H.264 High yuv420p instead of 10-bit 4:4:4.
  • FFmpeg encoding instead of QMediaRecorder. A worker thread encodes H.264/AAC into the MP4 with h264_vaapi when available, otherwise libx264 at veryfast. Writer keeps its timing model, failure messages, and backpressure limits.
  • New dependency: libjpeg-turbo (README, PKGBUILD).

On top of that:

  • Level and B-frames. H.264 level 5.1 above 1080p, and 4.2 otherwise, with no B-frames, on both h264_vaapi and libx264. A failed GPU open frees the codec before the libx264 fallback. Same-size frames are copied or point-sampled instead of bilinear-scaled, and the file records the encoder name.
  • JPEG copy, and a bad frame. Each camera JPEG is copied on the capture thread and decoded on a pool of two threads, so a worker never maps the preview's frame. One bad JPEG is skipped. Fifteen in a row stop the take with the existing message. VAAPI is not flushed when that stop happens before any frame has been encoded, which faults.

The frame-grid drops in #9 are not in this pull request. That fix is #10. Short takes on this branch can still come in under 30 fps for that reason.

Verification

The first two commits were checked by Merovex on a Beelink SER8 (Ryzen 7 8745HS, Radeon 780M), Omarchy 4.0.4, Qt 6.11.2, FFmpeg 9.0.1, Logitech MX Brio at 3840×2160 MJPEG 30 fps. Details are on #11: tests passed, and a 59 s 4K take completed where takes had been stopping after 1–3 s.

The last two commits were checked on an Intel Core Ultra X7 358H with Arc B390 (FFmpeg 9.0.2):

Merovex and others added 4 commits October 29, 2026 20:03
Webcams deliver 4K30 as MJPEG. Qt decoded each frame to RGB and then
encoded it as 10-bit 4:4:4 H.264, which most players cannot open and
which is far slower to encode. Decode with libjpeg-turbo straight to
video-range YUV 4:2:0 on the thread pool, keeping frames in capture order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Qt's recorder always runs libx264 at its medium preset, which manages
about 17 fps at 4K, and never uses VAAPI for frames in system memory, so
4K takes stopped within seconds. Encode on a worker thread with FFmpeg:
the GPU's h264_vaapi encoder when available, otherwise libx264 at
veryfast. Takes keep the same timing model and MP4 output.

Fixes omacom#8

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Level 5.1 above 1080p, and 4.2 otherwise, on VAAPI and libx264. No B-frames,
so the software fallback stays low-delay. A failed GPU open frees the codec
before that fallback, including when the frame buffer or video stream cannot
be created.
The capture thread copies the compressed bytes and decodes them on a pool
of two threads, so a worker never maps the preview frame and a 4K decode
cannot occupy every core. One bad JPEG is skipped. Fifteen in a row stop
the take. VAAPI is not flushed when that stop happens before any frame,
which faults. The file records the encoder name, and the level check reads
ffprobe's numeric JSON field.
@greptile-apps

greptile-apps Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 2/5

[Medium impact] The PR changes the recording and finalizing path used by Monologue takes.

The PR needs fixes before merging because the encoder timeout, unsupported microphone rates, and native-frame colors remain unresolved.

Findings

  1. P1 Timeout leaves the encoder running ▶
  2. P1 Some microphones cannot record ▶
  3. P1 Native camera colors change ▶
  4. P2 Bad frames count out of order ▶

Summary

Replaces Qt recording with an FFmpeg worker and adds a bounded JPEG decode queue.

  • Recordings use an FFmpeg worker with hardware and software H.264 encoders.
  • Camera JPEGs become 8-bit 4:2:0 frames before encoding.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
    Camera[Camera frame] --> Kind{JPEG?}
    Kind -->|Yes| Copy[Copy JPEG bytes]
    Copy --> Decode[Decode on two-thread pool]
    Kind -->|No| Ready[Wrap native frame]
    Decode --> Queue[Capture-order frame queue]
    Ready --> Queue
    Queue --> Worker[FFmpeg worker]
    Microphone[Microphone PCM] --> Timing[Remove pauses and fill audio gaps]
    Timing --> Worker
    Worker --> Codec{VAAPI available?}
    Codec -->|Yes| GPU[h264_vaapi]
    Codec -->|No| CPU[libx264]
    GPU --> MP4[H.264 and AAC MP4]
    CPU --> MP4
Loading

Reviews (3) · Last reviewed commit: "Decode camera JPEGs from a copy and skip..." · Reviewed by Greptile

Comment thread src/writer.cpp
Comment on lines +81 to +83
connect(&m_flushTimeout, &QTimer::timeout, this, [this] {
if (!m_failed) { m_failed = true; emit failed("The encoder stopped responding, so the take was stopped."); }
emitFinished();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Timeout leaves the encoder running

When the timeout fires, it emits finished before the encoder has stopped. Backend::writerFinished() then starts reading the MP4 while the worker may still be writing it or may not have written its trailer. Deleting the writer also waits for that worker on the GUI thread, so a stuck encoder can freeze the window.

Keep worker shutdown off the GUI thread, and emit finished only after the file is closed.

Comment thread src/encoder.cpp
Comment on lines +164 to +169
m_audio->sample_rate = format.sampleRate();
av_channel_layout_default(&m_audio->ch_layout, format.channelCount());
m_audio->bit_rate = 192000;
m_audio->time_base = {1, format.sampleRate()};
if (m_format->oformat->flags & AVFMT_GLOBALHEADER) m_audio->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
int result = avcodec_open2(m_audio, codec, nullptr);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Some microphones cannot record

If a microphone reports a 192 kHz preferred format, openAudio() tries to open AAC at that unsupported rate and the whole take fails to start. The backend passes preferredFormat() through unchanged, and the new audio conversion uses the same input and output rate.

Choose a rate supported by AAC and resample the captured audio to it.

Comment thread src/encoder.cpp
Comment on lines +104 to +105
m_video->color_range = AVCOL_RANGE_MPEG;
m_video->colorspace = AVCOL_SPC_BT470BG;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Native camera colors change

If a camera supplies native BT.709 or full-range YUV frames, the encoder marks them as limited-range BT.601 without converting their values to match. Non-JPEG frames bypass decodeJpeg(), and the encoder never reads their colorRange() or colorSpace(). Recordings can therefore have wrong colors or clipped dark and bright areas.

Honor the source settings when converting pixels or writing the output color settings.

Comment thread src/writer.cpp
Comment on lines +137 to +140
if (!frame.map(QVideoFrame::ReadOnly)) { noteBadFrame(); return; }
const QByteArray jpeg(reinterpret_cast<const char *>(frame.bits(0)), frame.mappedBytes(0));
frame.unmap();
if (jpeg.isEmpty()) { noteBadFrame(); return; }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Bad frames count out of order

Map failures update m_badDecodes immediately, but successful JPEGs reset it only after their background decode finishes. After 14 bad decodes, a good JPEG still waiting to finish followed by one map failure stops the take, even though the bad frames were not consecutive.

Put map and empty-buffer failures into m_frames as failed results too, so all counting happens in capture order.

@gigasolo
gigasolo force-pushed the sustain-4k30-recording branch from 4276b75 to d33f7e5 Compare November 6, 2026 06:13
@kjeldahl

kjeldahl commented Nov 6, 2026

Copy link
Copy Markdown

Tested this PR (d33f7e5) on an Intel MacBook Pro 13" 2019 (T2, i5-8259U, Iris Plus 655, intel-media-driver 26.3.5 / iHD), Omarchy, kernel 7.2.11-t2, Hyprland 0.56.2, Qt 6.12.0, FFmpeg 9.0.2, with a Logitech BRIO (4K) on a Thunderbolt dock.

0.3.1 (packaged): every take stopped within ~3 s, at 4K and also at 1080p/720p fed through a v4l2loopback device. The log showed Cannot find software target format. sourceSWFormat: bgra|nv12 targetFormat: vaapi, so Qt fell back to libx264 and managed ~27–28 fps at 1080p. Switching to the i965 VA-API driver didn't help. ffmpeg -c:v h264_vaapi on the same machine encodes 1080p and 4096×2160 without trouble.

This PR:

  • BRIO directly (4096×2160 MJPEG): the clip is now h264_vaapi, High yuv420p, 4096×2160. That's a big step up. The take still stopped after 7.4 s with "Audio capture fell behind the recording", averaging ~24 fps. I believe the JPEG decode of 4K frames on this 4-core CPU is the bottleneck. The machine was also being throttled by the platform during the test (BD PROCHOT from the dock power supply), so treat that number as pessimistic.
  • BRIO as raw 1080p NV12 via v4l2loopback: a 61 s take with no failures, 1920×1080 h264_vaapi, and the fans stayed quiet. 👍

The VAAPI path works well on Intel Gen9 here. A format/resolution picker (#14/#18) would let people on older CPUs pick 1080p from the camera directly instead of going through v4l2loopback.

On one earlier run of this build (before I added logging), a ~4 s take ended with "The encoded clip did not match the selected resolution or audio mode". I couldn't reproduce it afterwards, so I don't know the cause.

@dhh

dhh commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Thanks! This builds on #11, and the 4:2:0 decode it carries is now covered by #32, which keeps Qt's recorder and lets it choose the hardware encoder. Closing in favour of that. If the H.264 level / no-B-frames change or the bad-JPEG handling still matter on top of #32, they'd be welcome as a small follow-up.

@dhh dhh closed this Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4K30 recording stops within seconds: Qt's recorder can't encode it fast enough, even on fast hardware

4 participants