fix(player): keep HDR/Dolby Vision through zoom on iOS and tvOS

Nonzero mpv video-zoom flips vo_avfoundation into a per-frame Core
Image re-render that destroys HDR/DV passthrough - DV frames render
near-black on tvOS (verified on Apple TV 4K, DV P7->8.1 content:
panel luma mean 0.0 zoomed vs 87-103 unzoomed at locked exposure).

Zoom now scales the AVSampleBufferDisplayLayer itself (a
sublayerTransform on the container is ignored by the video plane)
via the existing Player.setVideoZoom seam, and VideoFilterManager
pins the mpv property to 0 on backends with native zoom. The layer
tree at 100% stays identical to before: clipping engages only while
zoomed, and updateFrame sizes the layer via bounds/position, which
frame= decomposes to anyway.

macOS keeps the property path (gpu-next zooms losslessly in-shader);
Android is untouched.
This commit is contained in:
edde746
2026-08-10 14:17:40 +02:00
parent f2ef15806a
commit a0269c6feb
8 changed files with 106 additions and 7 deletions
+41 -3
View File
@@ -86,14 +86,12 @@ class MpvPlayerCore: MpvPlayerCoreBase {
withoutLayerAnimations { withoutLayerAnimations {
if let frame { if let frame {
containerView.frame = frame containerView.frame = frame
videoLayer.frame = containerView.bounds
} else if let superview = containerView.superview { } else if let superview = containerView.superview {
containerView.frame = superview.bounds containerView.frame = superview.bounds
videoLayer.frame = containerView.bounds
} else if let window { } else if let window {
containerView.frame = window.bounds containerView.frame = window.bounds
videoLayer.frame = containerView.bounds
} }
fitVideoLayer(videoLayer, in: containerView)
mainBlankView?.frame = window?.bounds ?? .zero mainBlankView?.frame = window?.bounds ?? .zero
@@ -107,6 +105,46 @@ class MpvPlayerCore: MpvPlayerCoreBase {
#endif #endif
} }
/// Size the video layer to the container via bounds/position, never `frame`:
/// `frame` is undefined while the custom-zoom transform is non-identity.
private func fitVideoLayer(_ layer: MpvVideoLayer, in container: UIView) {
layer.bounds = CGRect(origin: .zero, size: container.bounds.size)
layer.position = CGPoint(x: container.bounds.midX, y: container.bounds.midY)
}
/// Apply custom viewer zoom by scaling the video layer about its center.
///
/// Zoom must never reach mpv's `video-zoom` on this VO: any nonzero zoom
/// flips vo_avfoundation into a Core Image re-render of every frame, which
/// destroys HDR/Dolby Vision passthrough (DV frames render near-black on
/// tvOS). A CALayer transform keeps the sample-buffer scanout path intact.
/// The transform must sit on the display layer itself a
/// `sublayerTransform` on the container is ignored by the video plane.
/// The layer's bounds never change, so the VO's bounds KVO stays quiet, and
/// the inline OSD sibling layer keeps its own geometry. PiP is unaffected:
/// the Dart side resets zoom before entry, and the system presents the
/// layer's buffers, not its on-screen transform.
func setVideoZoom(_ scale: Double) {
let clamped = min(max(scale, 0.25), 4.0)
Self.log("setVideoZoom(\(scale)) -> \(clamped)")
DispatchQueue.main.async { [weak self] in
guard let self, let container = self.containerView, let videoLayer = self.videoLayer else {
return
}
let zoomed = abs(clamped - 1.0) >= 0.0005
self.withoutLayerAnimations {
// Clip only while zoomed: at 100% the layer tree stays identical to a
// build without custom zoom, so the unzoomed path cannot regress
// (e.g. hardware-plane promotion of the sample-buffer layer).
container.clipsToBounds = zoomed
videoLayer.transform =
zoomed
? CATransform3DMakeScale(CGFloat(clamped), CGFloat(clamped), 1)
: CATransform3DIdentity
}
}
}
func externalDisplayDidChange() { func externalDisplayDidChange() {
refreshExternalDisplayAttachment() refreshExternalDisplayAttachment()
} }
@@ -91,6 +91,8 @@ class MpvPlayerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, MpvPluginS
handleSetDisplayCriteria(call: call, result: result) handleSetDisplayCriteria(call: call, result: result)
case "setVisible": case "setVisible":
handleSetVisible(call: call, result: result) handleSetVisible(call: call, result: result)
case "setVideoZoom":
handleSetVideoZoom(call: call, result: result)
case "isInitialized": case "isInitialized":
result(playerCore?.isInitialized ?? false) result(playerCore?.isInitialized ?? false)
case "updateFrame": case "updateFrame":
@@ -451,6 +453,17 @@ class MpvPlayerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, MpvPluginS
} }
} }
private func handleSetVideoZoom(call: FlutterMethodCall, result: @escaping FlutterResult) {
guard let args = call.arguments as? [String: Any],
let scale = doubleValue(args["scale"])
else {
result(FlutterError(code: "INVALID_ARGS", message: "setVideoZoom requires scale", details: nil))
return
}
playerCore?.setVideoZoom(scale)
result(nil)
}
private func int64Value(_ value: Any?) -> Int64? { private func int64Value(_ value: Any?) -> Int64? {
switch value { switch value {
case let value as Int64: case let value as Int64:
+6 -2
View File
@@ -326,8 +326,12 @@ abstract class Player {
/// here and scale via `panscan`/`video-aspect-override` properties instead. /// here and scale via `panscan`/`video-aspect-override` properties instead.
Future<void> setBoxFitMode(int mode); Future<void> setBoxFitMode(int mode);
/// Apply custom zoom to the native video layer. No-op on mpv backends, /// Apply custom zoom to the native video layer.
/// which zoom via the `video-zoom` property. ///
/// ExoPlayer scales its frame layout; iOS/tvOS scale the AVFoundation video
/// container (mpv's `video-zoom` would force vo_avfoundation's Core Image
/// path and destroy HDR/Dolby Vision passthrough). Other mpv backends are a
/// no-op here and zoom via the `video-zoom` property.
Future<void> setVideoZoom(double scale); Future<void> setVideoZoom(double scale);
/// Aggregated native playback stats (codecs, dimensions, dropped frames…). /// Aggregated native playback stats (codecs, dimensions, dropped frames…).
+1 -1
View File
@@ -1100,7 +1100,7 @@ abstract class PlayerBase with PlayerStreamControllersMixin implements Player {
Future<void> setBoxFitMode(int mode) async {} Future<void> setBoxFitMode(int mode) async {}
@override @override
// ignore: no-empty-block - base no-op, mpv zooms via the video-zoom property // ignore: no-empty-block - base no-op, non-Apple mpv zooms via the video-zoom property
Future<void> setVideoZoom(double scale) async {} Future<void> setVideoZoom(double scale) async {}
@override @override
+11
View File
@@ -1013,6 +1013,17 @@ class PlayerNative extends PlayerBase {
} }
} }
/// iOS/tvOS scale the native video container instead of mpv's `video-zoom`:
/// on the avfoundation VO a nonzero zoom re-renders every frame through
/// Core Image, which destroys HDR/Dolby Vision passthrough (DV renders
/// near-black on tvOS). macOS keeps the property path — gpu-next zooms
/// losslessly in-shader.
@override
Future<void> setVideoZoom(double scale) async {
if (_nativeCoreUnavailable || audioOnly || !Platform.isIOS || !initialized) return;
await invoke('setVideoZoom', {'scale': scale});
}
@override @override
Future<bool> setVideoFrameRate( Future<bool> setVideoFrameRate(
double fps, double fps,
+3
View File
@@ -37,6 +37,9 @@ extension _VideoPlayerPipMethods on VideoPlayerScreenState {
if (needsVideoFilter && _videoFilterManager == null && settings != null) { if (needsVideoFilter && _videoFilterManager == null && settings != null) {
_videoFilterManager = VideoFilterManager( _videoFilterManager = VideoFilterManager(
player: currentPlayer, player: currentPlayer,
// iOS and tvOS zoom the native video layer; mpv's video-zoom would
// force vo_avfoundation's Core Image path and kill HDR/DV passthrough.
nativeVideoZoom: Platform.isIOS,
initialBoxFitMode: settings.read(SettingsService.defaultBoxFitMode), initialBoxFitMode: settings.read(SettingsService.defaultBoxFitMode),
initialPlayerSize: initialPlayerSize, initialPlayerSize: initialPlayerSize,
onBoxFitModeChanged: (mode) => settings.write(SettingsService.defaultBoxFitMode, mode), onBoxFitModeChanged: (mode) => settings.write(SettingsService.defaultBoxFitMode, mode),
+8 -1
View File
@@ -46,6 +46,12 @@ class VideoFilterManager {
final Player player; final Player player;
/// Whether [Player.setVideoZoom] scales the native video layer itself, so
/// the mpv `video-zoom` property must stay 0. On iOS/tvOS the avfoundation
/// VO re-renders zoomed frames through Core Image, which destroys HDR and
/// Dolby Vision passthrough — zoom must stay out of mpv's pipeline there.
final bool nativeVideoZoom;
/// BoxFit mode state: 0=contain (letterbox), 1=cover (fill screen), 2=fill (stretch) /// BoxFit mode state: 0=contain (letterbox), 1=cover (fill screen), 2=fill (stretch)
int _boxFitMode; int _boxFitMode;
@@ -85,6 +91,7 @@ class VideoFilterManager {
VideoFilterManager({ VideoFilterManager({
required this.player, required this.player,
this.nativeVideoZoom = false,
int initialBoxFitMode = 0, int initialBoxFitMode = 0,
Size? initialPlayerSize, Size? initialPlayerSize,
this.onBoxFitModeChanged, this.onBoxFitModeChanged,
@@ -268,7 +275,7 @@ class VideoFilterManager {
} }
await _applyProperty('sub-ass-force-margins', coverMode || zoomScale > 1.0001 ? 'yes' : 'no'); await _applyProperty('sub-ass-force-margins', coverMode || zoomScale > 1.0001 ? 'yes' : 'no');
await _applyProperty('panscan', coverMode ? '1.0' : '0'); await _applyProperty('panscan', coverMode ? '1.0' : '0');
await _applyProperty('video-zoom', videoZoomPropertyForScale(zoomScale).toString()); await _applyProperty('video-zoom', nativeVideoZoom ? '0.0' : videoZoomPropertyForScale(zoomScale).toString());
} catch (e) { } catch (e) {
appLogger.w('Failed to update video filter', error: e); appLogger.w('Failed to update video filter', error: e);
} }
@@ -77,6 +77,29 @@ void main() {
expect(player.writes.single.value, VideoFilterManager.videoZoomPropertyForScale(1.5).toString()); expect(player.writes.single.value, VideoFilterManager.videoZoomPropertyForScale(1.5).toString());
}); });
test('native zoom keeps mpv video-zoom at zero and forwards the scale', () async {
final player = _RecordingPlayer();
final manager = VideoFilterManager(player: player, nativeVideoZoom: true);
addTearDown(manager.dispose);
await manager.updateVideoFilter();
player.clearRecords();
manager.setZoomScale(1.5);
await Future<void>.delayed(Duration.zero);
// The native layer gets the real scale; mpv must never see a nonzero
// video-zoom — on vo_avfoundation it would trigger the Core Image
// re-render that destroys HDR/Dolby Vision passthrough.
expect(player.zoomCalls, [1.5]);
expect(player.writes.where((write) => write.key == 'video-zoom'), isEmpty);
// Margin forcing still follows the zoom state.
final marginWrites = player.writes.where((write) => write.key == 'sub-ass-force-margins').toList();
expect(marginWrites, hasLength(1));
expect(marginWrites.single.value, 'yes');
});
test('repeated run with unchanged state writes nothing', () async { test('repeated run with unchanged state writes nothing', () async {
final player = _RecordingPlayer(); final player = _RecordingPlayer();
final manager = VideoFilterManager(player: player); final manager = VideoFilterManager(player: player);