Skip to content

Pass 10-bit bi-planar buffers through BufferCapturer - #1232

Open
OleksandrHaidamaka wants to merge 1 commit into
livekit:mainfrom
OleksandrHaidamaka:tenbit-buffer-capture
Open

OleksandrHaidamaka wants to merge 1 commit into
livekit:mainfrom
OleksandrHaidamaka:tenbit-buffer-capture

Conversation

@OleksandrHaidamaka

@OleksandrHaidamaka OleksandrHaidamaka commented Nov 23, 2026 •

Copy link
Copy Markdown

VideoCapturer skips any pixel buffer whose format isn't in LKRTCCVPixelBuffer.supportedPixelFormats(). That list has no 10-bit formats, so the frames an iPhone camera gives when it films HLG (kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange, or FullRange) are dropped before the capturer resolves its dimensions. A buffer track fed only such frames never gets dimensions, and publish waits out its timeout without sending a frame.

WebRTC doesn't need the format to be on that list to carry the frame: an RTCCVPixelBuffer reaches the encoder untouched, and an encoder installed with LiveKitSDK.set(videoEncoderFactory:) can code it. We send HEVC Main10 HLG from a VideoToolbox encoder this way.

WebRTC's own encoders and its scaling are another matter. They read a frame through toI420() and cropAndScaleTo(_:withTempBuffer:), and RTCCVPixelBuffer implements both only for its supported formats: for anything else it hits a RTC_DCHECK_NOTREACHED and, in release, returns planes it never wrote. So 10-bit frames are wrapped in TenBitPixelBuffer, a subclass that answers both from an 8-bit NV12 copy (each sample's high byte, video range 64...940 becoming 16...235) and returns itself from cropAndScaleWith, so a built-in encoder on the same track (VP8, H.264, a software fallback) gets the picture rather than heap bytes. A custom encoder still gets the 10-bit pixelBuffer as it is.

Changes:

  • VideoCapturer.tenBitPixelFormats (the two 4:2:0 bi-planar 10-bit formats) pass the format check; any other unsupported format is still skipped.
  • TenBitPixelBuffer wraps them.

Tests in BufferCapturerPixelFormatTests:

  • a 1280x720 buffer of each 10-bit format resolves the capturer's dimensions (both cases time out on main);
  • an unsupported format (422YpCbCr8) is still skipped;
  • toI420() of a 10-bit buffer gives 8-bit planes with the expected values, for both ranges;
  • cropped and scaled, it stays a TenBitPixelBuffer with the expected values, and cropAndScaleTo writes an NV12 output buffer.

@CLAassistant

CLAassistant commented Nov 23, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Devin Review found 1 potential issue.

5 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)

Devin Review

Comment on lines +227 to +228
guard VideoCapturer.supportedPixelFormats.contains(where: { $0.uint32Value == pixelFormat })
|| VideoCapturer.tenBitPixelFormats.contains(pixelFormat)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟥 Uninitialized memory in published video

VideoCapturer forwards 10-bit frames even when a buffer track uses built-in VP8. WebRTC’s release converter leaves the I420 planes unwritten, and VP8 encodes those heap bytes for room subscribers.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Right, the release path of RTCCVPixelBuffer.toI420() returns unwritten planes for a format it does not handle. Fixed in 041ce54: 10-bit frames are wrapped in TenBitPixelBuffer, whose toI420() and cropAndScaleTo(_:withTempBuffer:) read an 8-bit NV12 copy, and which stays itself through cropAndScaleWith. A built-in encoder now gets the picture; a custom one still gets the 10-bit buffer. Both paths are covered in BufferCapturerPixelFormatTests.

An HDR camera's HLG frames (kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange) were skipped as an
unsupported pixel format, so a buffer track fed by one never resolved its dimensions and its publish
waited out its timeout with no frame sent. WebRTC carries such a buffer untouched to an encoder set
with LiveKitSDK.set(videoEncoderFactory:), which codes it (HEVC Main10).

WebRTC's own encoders and its scaling read a frame through toI420() and cropAndScaleTo(_:withTempBuffer:),
which RTCCVPixelBuffer implements only for its supported formats; for any other format they return
planes they never wrote. 10-bit frames are wrapped in TenBitPixelBuffer, which answers both from an
8-bit NV12 copy (each sample's high byte) and stays itself when WebRTC crops or scales it.
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.

2 participants