Skip to content

Commit

Permalink
mac: add support to set color space for layer and macOS transformation
Browse files Browse the repository at this point in the history
By default utilises the color space of the screen on which the window is
located. If a specific value is defined, it will be instead be utilised.

Depending on the chosen color space the macOS EDR (HDR) support is
activated and that OS's transformation is used.

Fixes mpv-player#7341
  • Loading branch information
Akemi authored and tangalbert919 committed Jul 20, 2023
1 parent 15252e1 commit ca210da
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 10 deletions.
29 changes: 29 additions & 0 deletions DOCS/man/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6244,6 +6244,35 @@ them.

macOS only.

``--macos-output-csp=<csp>``
This sets the color space of the layer to activate the macOS color
transformation. Depending on the color space used the system's EDR (HDT)
support will be activated. To get correct results, this needs to be set to
the color primaries/transfer characteristics of the video. It is recommended
to use this switch together with ``--target-trc``.

``<csp>`` can be one of the following:

:auto: Sets the color space to the icc profile of the
screen (default).
:display_p3: DCI P3 primaries, a D65 white point, and the sRGB
transfer function.
:display_p3_hlg: DCI P3 primaries, a D65 white point, and the Hybrid
Log-Gamma (HLG) transfer function.
:display_p3_pq: DCI P3 primaries, a D65 white point, and the Perceptual
Quantizer (PQ) transfer function.
:display_p3_pq_eotf: DCI P3 primaries, a D65 white point, and the Perceptual
Quantizer (PQ) transfer function.
:dcip3: DCI P3 color space.
:itur_2020: ITU BT.2020 color space.
:itur_709: ITU BT.709 color space.
:srgb: sRGB colorimetry and non-linear transfer function.
:linear_srgb: Same as sRGB but linear transfer function.
:generic_rgb_linear: RGB and linear transfer function.
:adobe_rgb1998: Adobe RGB (1998) color space.

macOS only.

``--android-surface-size=<WxH>``
Set dimensions of the rendering surface used by the Android gpu context.
Needs to be set by the embedding application if the dimensions change during
Expand Down
10 changes: 6 additions & 4 deletions osdep/macos/swift_extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ extension NSDeviceDescriptionKey {
}

extension NSScreen {

public var displayID: CGDirectDisplayID {
get {
return deviceDescription[.screenNumber] as? CGDirectDisplayID ?? 0
Expand Down Expand Up @@ -62,7 +61,6 @@ extension NSScreen {
}

extension NSColor {

convenience init(hex: String) {
let int = Int(hex.dropFirst(), radix: 16) ?? 0
let alpha = CGFloat((int >> 24) & 0x000000FF)/255
Expand All @@ -75,15 +73,19 @@ extension NSColor {
}

extension Bool {

init(_ int32: Int32) {
self.init(int32 != 0)
}
}

extension Int32 {

init(_ bool: Bool) {
self.init(bool ? 1 : 0)
}
}

protocol VideoLayer: class {
var colorspace: CGColorSpace? { get set }
}

extension CAOpenGLLayer: VideoLayer {}
34 changes: 34 additions & 0 deletions osdep/macosx_application.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,39 @@ enum {
FRAME_WHOLE,
};

enum {
MAC_CSP_AUTO = -1,
MAC_CSP_DISPLAY_P3, //macOS 10.11.2+
MAC_CSP_DISPLAY_P3_HLG, //macOS 10.14.6+
MAC_CSP_DISPLAY_P3_PQ_EOTF, //macOS 10.14.6–10.15.4
MAC_CSP_DCIP3, //macOS 10.11+
MAC_CSP_ITUR_2020, //macOS 10.11+
MAC_CSP_ITUR_709, //macOS 10.11+

MAC_CSP_SRGB, //macOS 10.5+
MAC_CSP_LINEAR_SRGB, //macOS 10.12+
MAC_CSP_GENERIC_RGB_LINEAR, //macOS 10.5+
MAC_CSP_ADOBE_RGB1998, //macOS 10.5+

// no documentation?
MAC_CSP_DISPLAY_P3_PQ, //macOS 10.15.4+


// extended formats with values below 0.0 and above 1.0, useless?
MAC_CSP_EXTENDED_LINEAR_DISPLAY_P3, //macOS 10.14.3+
MAC_CSP_EXTENDED_SRGB, //macOS 10.12+
MAC_CSP_EXTENDED_LINEAR_SRGB, //macOS 10.12+
MAC_CSP_EXTENDED_LINEAR_ITUR_2020, //macOS 10.14.3+
// pixel values between 0.0 and 12.0
MAC_CSP_ITUR_2020_HLG, //macOS 10.15.6–11.0
// pixel value of 1.0 is assumed to be 100 nits
MAC_CSP_ITUR_2020_PQ_EOTF, //macOS 10.14.6–10.15.4

// no documentation?
MAC_CSP_EXTENDED_DISPLAY_P3, //macOS 11.0+
MAC_CSP_EXTENDED_ITUR_2020, //macOS 11.0+
};

struct macos_opts {
int macos_title_bar_style;
int macos_title_bar_appearance;
Expand All @@ -35,6 +68,7 @@ struct macos_opts {
bool macos_force_dedicated_gpu;
int macos_app_activation_policy;
int macos_geometry_calculation;
int macos_output_csp;
int cocoa_cb_sw_renderer;
bool cocoa_cb_10bit_context;
};
Expand Down
14 changes: 14 additions & 0 deletions osdep/macosx_application.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@
{"regular", 0}, {"accessory", 1}, {"prohibited", 2})},
{"macos-geometry-calculation", OPT_CHOICE(macos_geometry_calculation,
{"visible", FRAME_VISIBLE}, {"whole", FRAME_WHOLE})},
{"macos-output-csp", OPT_CHOICE(macos_output_csp,
{"auto", MAC_CSP_AUTO},
{"display_p3", MAC_CSP_DISPLAY_P3},
{"display_p3_hlg", MAC_CSP_DISPLAY_P3_HLG},
{"display_p3_pq", MAC_CSP_DISPLAY_P3_PQ},
{"display_p3_pq_eotf", MAC_CSP_DISPLAY_P3_PQ_EOTF},
{"dcip3", MAC_CSP_DCIP3},
{"itur_2020", MAC_CSP_ITUR_2020},
{"itur_709", MAC_CSP_ITUR_709},
{"srgb", MAC_CSP_SRGB},
{"linear_srgb", MAC_CSP_LINEAR_SRGB},
{"generic_rgb_linear", MAC_CSP_GENERIC_RGB_LINEAR},
{"adobe_rgb1998", MAC_CSP_ADOBE_RGB1998})},
{"cocoa-cb-sw-renderer", OPT_CHOICE(cocoa_cb_sw_renderer,
{"auto", -1}, {"no", 0}, {"yes", 1})},
{"cocoa-cb-10bit-context", OPT_BOOL(cocoa_cb_10bit_context)},
Expand All @@ -78,6 +91,7 @@
.defaults = &(const struct macos_opts){
.macos_title_bar_color = {0, 0, 0, 0},
.macos_fs_animation_duration = -1,
.macos_output_csp = MAC_CSP_AUTO,
.cocoa_cb_sw_renderer = -1,
.cocoa_cb_10bit_context = true
},
Expand Down
8 changes: 5 additions & 3 deletions video/out/cocoa_cb_common.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class CocoaCB: Common {
var libmpv: LibmpvHelper
var layer: GLLayer?

override var videoLayer: VideoLayer? {
get { return layer }
}

@objc var isShuttingDown: Bool = false

enum State {
Expand Down Expand Up @@ -121,9 +125,7 @@ class CocoaCB: Common {
}

libmpv.setRenderICCProfile(colorSpace)
if #available(macOS 10.11, *) {
layer?.colorspace = colorSpace.cgColorSpace
}
super.updateICCProfile()
}

override func windowDidEndAnimation() {
Expand Down
93 changes: 92 additions & 1 deletion video/out/mac/common.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Common: NSObject {
var window: Window?
var view: View?
var titleBar: TitleBar?
private(set) var videoLayer: VideoLayer?

var link: CVDisplayLink?

Expand Down Expand Up @@ -396,7 +397,95 @@ class Common: NSObject {
}

func updateICCProfile() {
log.sendWarning("updateICCProfile not implemented")
layerGuard: if #available(macOS 10.11, *) {
guard let layer = videoLayer else {
log.sendWarning("No layer found, macOS color transformation deactivated")
break layerGuard
}

layer.colorspace = getColorSpace()
}

flagEvents(VO_EVENT_ICC_PROFILE_CHANGED)
}

@available(macOS 10.11, *)
func getColorSpace() -> CGColorSpace? {
guard let colorSpace = window?.screen?.colorSpace?.cgColorSpace else {
log.sendWarning("Couldn't retrieve ICC Profile, no color space available")
return nil
}

let outputCsp = Int(mpv?.macOpts.macos_output_csp ?? Int32(MAC_CSP_AUTO))

switch outputCsp {
case MAC_CSP_AUTO:
return colorSpace
case MAC_CSP_SRGB:
return CGColorSpace(name: CGColorSpace.sRGB)
case MAC_CSP_GENERIC_RGB_LINEAR:
return CGColorSpace(name: CGColorSpace.genericRGBLinear)
case MAC_CSP_ADOBE_RGB1998:
return CGColorSpace(name: CGColorSpace.adobeRGB1998)
#if HAVE_MACOS_10_11_FEATURES
case MAC_CSP_DCIP3:
return CGColorSpace(name: CGColorSpace.dcip3)
case MAC_CSP_ITUR_2020:
return CGColorSpace(name: CGColorSpace.itur_2020)
case MAC_CSP_ITUR_709:
return CGColorSpace(name: CGColorSpace.itur_709)
#endif
default: break
}

#if HAVE_MACOS_10_11_2_FEATURES
if #available(macOS 10.11.2, *) {
switch outputCsp {
case MAC_CSP_DISPLAY_P3:
return CGColorSpace(name: CGColorSpace.displayP3)
default: break
}
}
#endif

#if HAVE_MACOS_10_12_FEATURES
if #available(macOS 10.12, *) {
switch outputCsp {
case MAC_CSP_LINEAR_SRGB:
return CGColorSpace(name: CGColorSpace.linearSRGB)
default: break
}
}
#endif

#if HAVE_MACOS_10_14_6_FEATURES
// these color spaces are defined from SDK 10.14.6 onwards but throws a
// null pointer exception when accessing on 10.14.6, but not on 10.15.x
// most likely an OS bug
if #available(macOS 10.15, *) {
switch outputCsp {
case MAC_CSP_DISPLAY_P3_HLG:
return CGColorSpace(name: CGColorSpace.displayP3_HLG)
case MAC_CSP_DISPLAY_P3_PQ_EOTF:
return CGColorSpace(name: CGColorSpace.displayP3_PQ_EOTF) // deprecated
default: break
}
}
#endif

#if HAVE_MACOS_10_15_4_FEATURES
if #available(macOS 10.15.4, *) {
switch outputCsp {
case MAC_CSP_DISPLAY_P3_PQ:
return CGColorSpace(name: CGColorSpace.displayP3_PQ)
default: break
}
}
#endif

log.sendWarning("Couldn't retrieve configured color space, falling back to auto")

return colorSpace
}

func getScreenBy(id screenID: Int) -> NSScreen? {
Expand Down Expand Up @@ -698,6 +787,8 @@ class Common: NSObject {
titleBar?.set(material: Int(mpv.macOpts.macos_title_bar_material))
case MPVHelper.getPointer(&mpv.macOptsPtr.pointee.macos_title_bar_color):
titleBar?.set(color: mpv.macOpts.macos_title_bar_color)
case MPVHelper.getPointer(&mpv.macOptsPtr.pointee.macos_output_csp):
updateICCProfile()
default:
break
}
Expand Down
2 changes: 2 additions & 0 deletions video/out/mac/gl_layer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ class GLLayer: CAOpenGLLayer {
super.init()
autoresizingMask = [.layerWidthSizable, .layerHeightSizable]
backgroundColor = NSColor.black.cgColor
// that this to true by default?
wantsExtendedDynamicRangeContent = true

if #available(macOS 10.12, *), bufferDepth > 8 {
contentsFormat = .RGBA16Float
Expand Down
1 change: 0 additions & 1 deletion video/out/mac/title_bar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class TitleBar: NSVisualEffectView {
} else {
state = .followsWindowActiveState
}

}
}

Expand Down
1 change: 1 addition & 0 deletions video/out/mac/view.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class View: NSView {
super.init(frame: frame)
autoresizingMask = [.width, .height]
wantsBestResolutionOpenGLSurface = true
wantsExtendedDynamicRangeOpenGLSurface = true
registerForDraggedTypes([ .fileURLCompat, .URLCompat, .string ])
}

Expand Down
22 changes: 21 additions & 1 deletion wscript
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,16 @@ standalone_features = [
'desc': 'macOS 10.11 SDK Features',
'deps': 'cocoa',
'func': check_macos_sdk('10.11')
}, {
'name': '--macos-10-11-2-features',
'desc': 'macOS 10.11.2 SDK Features',
'deps': 'cocoa',
'func': check_macos_sdk('10.11.2')
}, {
'name': '--macos-10-12-features',
'desc': 'macOS 10.12 SDK Features',
'deps': 'cocoa',
'func': check_macos_sdk('10.12')
}, {
'name': '--macos-10-12-2-features',
'desc': 'macOS 10.12.2 SDK Features',
Expand All @@ -946,7 +956,17 @@ standalone_features = [
'desc': 'macOS 10.14 SDK Features',
'deps': 'cocoa',
'func': check_macos_sdk('10.14')
},{
}, {
'name': '--macos-10-14-6-features',
'desc': 'macOS 10.14.6 SDK Features',
'deps': 'cocoa',
'func': check_macos_sdk('10.14.6')
}, {
'name': '--macos-10-15-4-features',
'desc': 'macOS 10.15.4 SDK Features',
'deps': 'cocoa',
'func': check_macos_sdk('10.15.4')
}, {
'name': '--macos-media-player',
'desc': 'macOS Media Player support',
'deps': 'macos-10-12-2-features && swift',
Expand Down

0 comments on commit ca210da

Please sign in to comment.