Skip to content

Sustain 4K30 recording: decode MJPEG to 4:2:0 and encode with FFmpeg (VAAPI) - #11

Closed
Merovex wants to merge 2 commits into
omacom:masterfrom
Merovex:fix-4k-recording
Closed

Merovex wants to merge 2 commits into
omacom:masterfrom
Merovex:fix-4k-recording

Conversation

@Merovex

@Merovex Merovex commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

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

What changed

  • JPEG decode to 4:2:0 (media::decodeJpeg): webcams deliver 4K30 as MJPEG. Each frame is decoded with libjpeg-turbo straight to the JPEG's own planes, box-filtered to 8-bit 4:2:0 and converted to video range, on the Qt thread pool, keeping capture order. Output is standard H.264 High profile yuv420p instead of 10-bit 4:4:4.
  • FFmpeg encoding instead of QMediaRecorder (src/encoder.cpp): a worker thread encodes H.264/AAC into the MP4 with h264_vaapi (CQP 20) when available, otherwise libx264 -preset veryfast -crf 19. Writer keeps its timing model (pause intervals, sample-counted audio with padding and trimming), its failure messages and its backpressure limits; Backend is unchanged.
  • New dependency: libjpeg-turbo (README, PKGBUILD). FFmpeg's libraries were already required.

Verification

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.

  • ./bin/test: all pass (22 backend + 3 UI). This includes the A/V sync test (within 10 ms), new decoder tests for 4:2:2, 4:2:0, 4:4:4 and grayscale JPEGs, and a check that the encoded output is High yuv420p.
  • ./bin/test-camera and a 59 s real 4K take: completed without stopping, High yuv420p. Before this change, takes stopped after 1–3 s.
  • Manual takes in the app with the microphone: 4K30, about 25 Mbit/s video, 192 kb/s AAC.

Frame counts on this branch alone can still be low in some takes; that's #9, fixed separately in its own PR. Both PRs touch the first lines of Writer::videoAt, so whichever merges second will need a small rebase.

🤖 Generated with Claude Code

Merovex and others added 2 commits October 9, 2026 16:39
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>
@gigasolo

Copy link
Copy Markdown

Thanks for this. I ran the branch on an Intel Core Ultra X7 358H with Arc B390 (FFmpeg 9.0.2), and a 4K take holds on h264_vaapi.

I have two small commits that apply on top of f9f346e. I would rather offer them for this branch than open a second pull request:

  1. Level and B-frames. H.264 level 5.1 above 1080p, and 4.2 otherwise, with max_b_frames set to 0, 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.
  2. 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.

./bin/test passed here: 24 backend tests and 3 UI tests, including the bad-JPEG cases, level 4.2 with no B-frames at 320×240, and a forced libx264 fallback. ./bin/test-camera produced a 4.0 s 3840×2160 clip: High, yuv420p, level 5.1, no B-frames, encoder tag h264_vaapi, with AAC for the whole take. The frame count was 100, about 25 fps. That matches what you already noted: this branch alone can still drop frames on the slot grid, which is #9.

Happy to open a pull request against fix-4k-recording with just those two commits if you want them here.

@dhh

dhh commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Thanks for digging into this! The 4:4:4 / software-encode problem is being fixed in #32, which hands Qt's recorder 8-bit 4:2:0 frames (MJPEG decoded with libjpeg-turbo, like here) so it picks a hardware H.264 encoder, without replacing QMediaRecorder. Closing this in favour of that. The dropped-frames fix in #10 is separate and still open.

@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

3 participants