Skip to content

video: camera MJPEG is full-range BT.601, and a camera keeps up when score renders slower than it captures - #2417

Open
jcelerier wants to merge 3 commits into
masterfrom
split/video-camera-input
Open

jcelerier wants to merge 3 commits into
masterfrom
split/video-camera-input

Conversation

@jcelerier

@jcelerier jcelerier commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Two camera input bugs, both found while calibrating with a Logitech C930e on the CAVE.

  • Colour. UVC cameras send JPEG frames, and JPEG (JFIF) is full-range BT.601 at every size. The camera input never read a colour description, so the BT.601/709 conversion took the frames as limited range, stretching every code by 255/219 (JPEG black came out at -0.073, everything above code 235 went over 1.0), and picked the matrix from the frame size, BT.709 from 1280 wide.
    The drivers do not help. V4L2/uvcvideo never sets a quantization, and V4L2's default rule then says limited for YUV unless the colorspace is V4L2_COLORSPACE_JPEG, which uvcvideo does not report. dshow reports a range only when the driver fills its colour info, and FFmpeg's v4l2 and avfoundation inputs report nothing. FFmpeg's MJPEG decoder tags every frame as yuvj4xxp, pc, bt470bg (or limited for streams carrying CS=ITU601), so the decoded frame is the reliable source. This is the same bug as crbug 40144925; Chromium's Media Foundation path makes the same call ("0..255 range NV12 is usually just an unpacked MJPG").
    The renderer now completes what a source does not declare from the decoded frame: range, matrix, transfer and primaries. With nothing anywhere, YUVJ formats are full range and BT.601. This applies to every live source that declares nothing: cameras, network streams (LibavStreamInput), gphoto2 (YUVJ422P), the depth-camera add-on's MJPEG colour stream and the AJA CPU path of the videoio add-on (whose BT.2020/PQ/HLG tagging was lost). The file decoder now defaults an unspecified MJPEG or YUVJ stream to full range. Sources that declare their colour are unchanged.
  • Latency. The camera thread stopped reading once its queue held 4 frames. Because the renderer always takes the newest frame, the queue only fills when score renders slower than the camera captures. The device then kept capturing into its own buffer (dshow's real-time buffer, up to 32 V4L2 buffers) with nothing reading it, and the latency grew past a second and never came back down. Structured light reads the camera back every frame: a 30 fps camera fell several patterns behind and nothing decoded at 1/32 s exposure.
    The thread now always reads, and drops the oldest queued frame when the queue is full (FrameQueue::enqueue_latest). For intra-only codecs (MJPEG, raw video) it reads up to the newest buffered packet and decodes only that one (readNewestPacket), so a decoder slower than the camera (4K MJPEG decodes single-threaded in live mode) no longer leaves a backlog in the driver. Devices that block instead of answering EAGAIN are detected by how long the read took.
    A consumer is idle once it stops asking for frames. The queue is then emptied, and intra-only packets are read without decoding. A consumer that comes back gets the next captured frame, not the ones queued when it left (before this, it got a frame about 4 s old after a 5 s pause).

Commits

  • 1376c8259c video: MJPEG from a camera is decoded as full range
  • bb8fd88def video: a camera keeps reading frames while score renders slower than it captures
  • 5a27bcdc3c video: MJPEG frames are converted with BT.601, and a camera is read up to its newest frame

Tests

  • VideoRebuildTest (in score-plugin-gfx/tests) checks effectiveColorRange and effectiveColorSpace: an HD MJPEG frame is converted with full-range BT.601, a frame's own description is used when the source declares nothing (including HDR transfer and primaries), a declared description wins, and plain YUV with nothing anywhere keeps the size-based guess.
  • test_unit_video_frame_queue covers enqueue_latest, the poll count, discard_queued, idle detection, and readNewestPacket against a fake device (backlog drained to the newest packet, other streams skipped, waiting then giving up, blocking devices, the backlog cap).
  • Checked against a fake MJPEG camera (ffmpeg writing 1280x720 25 fps MJPEG into a fifo, read by CameraInput): frames arrive tagged yuvj420p pc bt470bg, steady and 5 Hz consumers stay at the live frame, and after 5 s without a consumer the next frame is live. With only the first two commits, the same run returns a frame 98 frames old.
  • Built with clang 23, lld 23, Qt 6.12, Debug, shared plug-ins. ctest -R "VideoRebuildTest|video|camera|gpu_formats|decoder|media": everything passes except test_integration_pipewire_video_output, which fails the same way without these commits.

Related add-on PRs

Not tested

  • No real camera; Windows and macOS not built for the last commit.

Notes for review

  • Visible to users: network streams and gphoto2 sources that declare nothing now follow their frames' range, matrix and transfer. HD MJPEG cameras change matrix. Content graded against the old look will change.

🤖 Generated with Claude Code

https://claude.ai/code/session_01KdPguixxwrpgc8UWhoHDeQ

jcelerier and others added 2 commits November 25, 2026 10:28
A UVC camera's MJPEG frames are full-range JPEG, but neither dshow nor
v4l2 declare a range for them, and the camera input never read one: the
frames reached the conversion as unspecified, which the BT.601/709 path
reads as limited, stretching every code by 255/219. On the CAVE's C930e,
JPEG black came out at -0.073 and everything above code 235 over 1.0.

The renderer now resolves the range from what the source declares, else
the decoded frame's own range, else full range for the YUVJ formats. This
applies to every live source that declares no range: cameras, network
streams (LibavStreamInput) and gphoto2 (YUVJ422P), which now show full-range
frames as full range. The file decoder defaults an unspecified MJPEG or
YUVJ stream to full range instead of limited; files that declare a range
are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KdPgufk1W2MBbpcjraruZq
…it captures

The camera thread stopped reading once its queue held 4 frames. The
renderer always takes the newest one, so the queue only fills when renders
come slower than camera frames, and then the device kept capturing into
its own buffer (dshow's real-time buffer) while nothing read it: the
latency grew to a second and more and never came back down. On the CAVE,
Structured Light Calibration reads the camera back every frame and a C930e
at 30 fps went several patterns late, so nothing decoded at 1/32 s while
1/8 s (8 fps) worked.

The thread now always reads, and drops the oldest queued frame when the
queue is full (FrameQueue::enqueue_latest). A camera nobody reads (its
queue full and nothing taken for a second of frames) still drains the
device, but for intra-only codecs (MJPEG, raw video) it drops the packets
without decoding them, as the old code did by not reading at all.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KdPgufk1W2MBbpcjraruZq
…p to its newest frame

Follow-up to the two camera commits.

Colour. A decoded MJPEG frame says full range and BT.601 (JFIF), and
libavcodec tags it so (yuvj4xxp, pc, bt470bg). Only the range was read
from the frame; the matrix stayed unspecified and fell back to the
size-based guess, BT.709 from 1280 wide, so an HD MJPEG camera was
converted with the wrong matrix. The range, matrix, transfer and
primaries an input does not declare are now all completed from the
frame, and a YUVJ format with no matrix anywhere is BT.601. Inputs that
only tag their frames (AJA staging path of the videoio add-on, the
depth-camera add-on's MJPEG colour stream) get their description too.

Latency. Reading one packet per iteration keeps up only while decoding
is faster than the camera; past that the backlog sits in the driver
(v4l2 queues up to 32 buffers, dshow its rtbufsize) and the latency
stays. For intra-only codecs the thread now reads up to the newest
buffered packet and decodes that one. Devices that block instead of
answering EAGAIN end the backlog by the time the read took.

An idle consumer is now one that stopped asking for frames, rather than
one that stopped getting them, so the queue can be emptied when it goes
idle: a consumer that comes back gets the next captured frame instead of
the one queued when it left.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KdPguixxwrpgc8UWhoHDeQ
@jcelerier jcelerier changed the title video: camera MJPEG is full range, and a camera keeps up when score renders slower than it captures video: camera MJPEG is full-range BT.601, and a camera keeps up when score renders slower than it captures Nov 26, 2026

This branch is waiting to be deployed

1 waiting deployment
full-ci — 5a27bcdc Waiting Nov 26, 2026 by jcelerier via Approve full PR suite #24
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.

1 participant