Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Use the truly native pixel format on iOS if available #2200

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 8 additions & 13 deletions package/ios/Core/CameraConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,17 @@ extension CameraConfiguration.Video {
Returns the pixel format that should be used for the given AVCaptureVideoDataOutput.
If HDR is enabled, this will return YUV 4:2:0 10-bit.
If HDR is disabled, this will return whatever the user specified as a pixelFormat, or the most efficient format as a fallback.
If no options are enabled and the truly native format can be used, this returns nil.
*/
func getPixelFormat(for videoOutput: AVCaptureVideoDataOutput) throws -> OSType {
func getPixelFormat(for videoOutput: AVCaptureVideoDataOutput) throws -> OSType? {
// as per documentation, the first value is always the most efficient format
var defaultFormat = videoOutput.availableVideoPixelFormatTypes.first!
var defaultFormat: OSType?
if enableBufferCompression {
// use compressed format instead if we enabled buffer compression
if defaultFormat == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange &&
videoOutput.availableVideoPixelFormatTypes.contains(kCVPixelFormatType_Lossy_420YpCbCr8BiPlanarVideoRange) {
// YUV 4:2:0 8-bit (limited video colors; compressed)
defaultFormat = kCVPixelFormatType_Lossy_420YpCbCr8BiPlanarVideoRange
}
if defaultFormat == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange &&
videoOutput.availableVideoPixelFormatTypes.contains(kCVPixelFormatType_Lossy_420YpCbCr8BiPlanarFullRange) {
// YUV 4:2:0 8-bit (full video colors; compressed)
defaultFormat = kCVPixelFormatType_Lossy_420YpCbCr8BiPlanarFullRange
}
// try to use a compressed format instead if we enabled buffer compression
defaultFormat = videoOutput.findPixelFormat(firstOf: [
kCVPixelFormatType_Lossy_420YpCbCr8BiPlanarVideoRange,
kCVPixelFormatType_Lossy_420YpCbCr8BiPlanarFullRange,
])
}

// If the user enabled HDR, we can only use the YUV 4:2:0 10-bit pixel format.
Expand Down
14 changes: 10 additions & 4 deletions package/ios/Core/CameraSession+Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,16 @@ extension CameraSession {

// Configure the VideoOutput Settings to use the given Pixel Format.
// We need to run this after device.activeFormat has been set, otherwise the VideoOutput can't stream the given Pixel Format.
let pixelFormatType = try video.getPixelFormat(for: videoOutput)
videoOutput.videoSettings = [
String(kCVPixelBufferPixelFormatTypeKey): pixelFormatType,
]
if let pixelFormatType = try video.getPixelFormat(for: videoOutput) {
// Use a custom passed pixelFormat (yuv, rgb, HDR, compressed, ...)
videoOutput.videoSettings = [
String(kCVPixelBufferPixelFormatTypeKey): pixelFormatType,
]
} else {
// According to https://developer.apple.com/documentation/avfoundation/avcapturevideodataoutput/1389945-videosettings#discussion,
// setting `videoSettings` to an empty dictionary configures the output to use the "device-native format".
videoOutput.videoSettings = [:]
}
}

// pragma MARK: Side-Props
Expand Down