Skip to content

Experimental HDR support on macOS using colorSpace approach#8404

Closed
anastasiuspernat wants to merge 5 commits intompv-player:masterfrom
anastasiuspernat:macoshdr
Closed

Experimental HDR support on macOS using colorSpace approach#8404
anastasiuspernat wants to merge 5 commits intompv-player:masterfrom
anastasiuspernat:macoshdr

Conversation

@anastasiuspernat
Copy link

Enabling support for HDR monitors (in manual mode).

Main discussion is here: #7341

Added a new switch called --macos_hdr_csp (similar to d3d color space approach at #5804). It specifies the extended HDR color space. Doesn't require any shader or video render pipeline modification. Auto-activates EDR/HDR mode on the video layer.

Currently supported color spaces (passed to --macos_hdr_csp switch):

  • displayP3_PQ_EOTF DCI P3 primaries, PQ transfer function - that's the most common color profile for most videos
  • displayP3_HLG DCI P3 primaries, and the HLG gamma
  • extendedLinearDisplayP3 P3 primaries and linear transfer function
  • itur_2020 BT.2020
  • itur_2020_PQ_EOTF BT.2020 with PQ transfer function

This can (and probably must) be set together with target-trc switch. Both values must correspond to video's color primaries/transfer characteristics in order to get the correct color reproduction.

Tested on varios video footage on macOS Catalina 10.15.5. Produces same results as QuickTime player on the same footage.

@Akemi
Copy link
Member

Akemi commented Dec 19, 2020

i added a few comments. it would also be nice to have some mpv option examples and some screenshots with before, now and quicktime. mostly out of curiosity and for future reference.

@anastasiuspernat
Copy link
Author

anastasiuspernat commented Dec 22, 2020

I hope I fixed it! Updated everything by your comments and added new info to options.rst.

Also here are reference images from my tests. They don't fully show the dynamic range of the image (this part is done on the monitor side) but they do give some idea:
HDR_mpv_illustration

P.S. Unfortunately I couldn't find a way to compare the exact same frames from QuickTime and mpv so they look a little bit different because they come from slightly different timecodes.

@avih
Copy link
Member

avih commented Dec 23, 2020

The commits add (and update) .DS_Store, is that intentional? (should we add it to .gitignore?)

@anastasiuspernat
Copy link
Author

Sorry about .DS_Stores. No it's not intentional, they were auto-added by Finder. When I develop on Mac I usually add .DS_Store to .gitignore. I think I removed them all in the final commit, no?

@avih
Copy link
Member

avih commented Dec 23, 2020

I think I removed them all in the final commit, no?

I didn't look at the commits one by one, but assuming you intend to keep the commits incremental without squashing, then removing them only at the last commit is not enough - you'll need to remove them from all commits (might be slightly painful, but not too much, though the pain would also make it easier to avoid next time :p).

@anastasiuspernat
Copy link
Author

No I don't intend to keep all the commits. Would be happy if you could help me to squash them the right way if that's necessary. I think only the current final stage is important, thank you.

@avih
Copy link
Member

avih commented Dec 23, 2020

Not necessary to squash them right away, depends on how you want to present it - incremental changes or one commit which changes over time. But you can do that it you prefer to work only on one commit.

This is git territory, but you can squash commits locally by running git rebase -i and replace pick with s (for squash) for all but the first new commit, then save the file and quit the editor, edit the combined commit message, and save and quit again. If you're happy with the result - git push -f would update the PR with the squashed commit.

For fllowup changes, you can either do the same with new commits before you push them, or just amend the existing commit, and again force push will update the PR. Force (-f) is required because it rewrites the history - acceptable in a PR but generally highly discouraged elsewhere.

@anastasiuspernat
Copy link
Author

OK great! Thanks for the details. One commit per major followup would definitely look better. Will squash it on next iteration.

@avih
Copy link
Member

avih commented Dec 23, 2020

There are no hard rules, but the convention is that you can force push in a PR (change existing commits), but preferably keep a set of commits which is intended to be pushed individually, where each commit does one "thing". Deciding what a "thing" is is subjective, but if your code does two unrelated things for instance, then it's preferable to keep those in two distinct commits.

I just briefly looked at the combined diff of this PR, and indeed I agree it should probably end up as one commit.

// HDR is supported fully only on macOS 10.15 and higher
// some of the HDR color spaces are available on 10.14 though
#if HAVE_MACOS_10_15_FEATURES
switch (mpv?.macOpts.macos_hdr_csp)
Copy link
Member

Choose a reason for hiding this comment

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

the indentation is wrong here.

Copy link
Author

@anastasiuspernat anastasiuspernat Dec 28, 2020

Choose a reason for hiding this comment

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

Fixed it! Also some color spaces are declared as supported by 10.14.X SDKs, like 10.14.6 etc but seems HAVE_MACOS_10_14_FEATURES checking for 14.0 and Travis won't let me to go through if I put them inside HAVE_MACOS_10_14_FEATURES.

Here's PR: 427d81c
Travis build log: https://travis-ci.org/github/mpv-player/mpv/jobs/751898275
(the error is "'CGColorSpace' has no member 'displayP3_HLG'", with the check for #available(macOS 10.14.6...).

In any case only macOS Catalina 10.15 supports HDR output and it's better be compiled against this version. So most of those are now inside HAVE_MACOS_10_15_FEATURES.

Copy link
Member

Choose a reason for hiding this comment

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

no those color spaces are specifically defined to be available on 10.14.6 and not 10.14.X/any 10.14 SDK. it needs a specific minor configure check too.

HAVE_MACOS_10_14_FEATURES only checks for 10.14(.0) features but not for 10.14.6. this needs a separate configure check.

(view?.layer as! CAOpenGLLayer).colorspace = CGColorSpace(name: name)
} else
{
(view?.layer as! CAOpenGLLayer).colorspace = windowColorSpace.cgColorSpace
Copy link
Member

Choose a reason for hiding this comment

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

no forced casts. i still dislike the else case here.

Copy link
Author

Choose a reason for hiding this comment

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

Need some help here. How else can I set color space on the view.layer? It's CALayer, right? Doesn't have colorspace property.

Also what's wrong with else, you mean the curly bracket?

Copy link
Member

Choose a reason for hiding this comment

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

typecasting is okay, but you should not force typecasting by any means, eg as? instead of as!. the latter case will crash when failing. see https://docs.swift.org/swift-book/LanguageGuide/TypeCasting.html

the else is unnecessary there.

colorSpcace = defaultColorSpace
if someCondtion == true {
    colorSpcace = differentColorSpace
} 

Copy link
Author

Choose a reason for hiding this comment

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

Unfortunately this way typecasting doesn't work. With as? I receive a compilation error about unwrapping CAOpenGLLayer, probably view.layer has a wrong type definition?

Copy link
Member

Choose a reason for hiding this comment

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

something like this should haves worked. though i would need to see the specific error.

guard let newlayer = layer as? CAOpenGLLayer else {
      return
 }
// access newlayer here

@Akemi
Copy link
Member

Akemi commented Dec 27, 2020

i am not sure if what we see on those screenshots is 'intended' or more likely a 'coincidence' that it looks somewhat similar/okay'ish.


var cursorVisibilityWanted: Bool = true

var colorSpace: NSColorSpace?
Copy link
Member

Choose a reason for hiding this comment

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

i believe this variable is not needed.

Copy link
Author

Choose a reason for hiding this comment

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

It's used in updateICCProfile for setRenderICCProfile in cocoa_cb_common.swift.

Copy link
Member

Choose a reason for hiding this comment

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

what i meant here, is that we don't need a variable that is available to the whole object of that class but just a function local variable would have done perfectly fine.

@anastasiuspernat anastasiuspernat force-pushed the macoshdr branch 3 times, most recently from d53d47c to 266f583 Compare December 28, 2020 00:51
Anastasy and others added 2 commits December 27, 2020 17:10
Experimental HDR support on macOS using colorSpace approach

Experimental HDR support on macOS using colorSpace approach

Experimental HDR support on macOS using colorSpace approach

Experimental HDR support on macOS using colorSpace approach

Experimental HDR support on macOS using colorSpace approach

Experimental HDR support on macOS using colorSpace approach

Experimental HDR support on macOS using colorSpace approach

Added macOS version check

Added macOS version check for Catalina

Stricter check for macOS Catalina features

Switched from runtime check to compile time check of macOS features

Made macOS HDR implementation more consistent

Made macOS HDR implementation more consistent

Made macOS HDR implementation more consistent

Made macOS HDR implementation more consistent

Made macOS HDR implementation more consistent

Improved macOS HDR-related code by PR comments

Improved macOS HDR-related code by PR comments

Improved macOS HDR-related code by PR comments

Improved macOS HDR-related code by PR comments

Improved macOS HDR-related code by PR comments
@anastasiuspernat
Copy link
Author

anastasiuspernat commented Dec 28, 2020

i am not sure if what we see on those screenshots is 'intended' or more likely a 'coincidence' that it looks somewhat similar/okay'ish.

@Akemi
They do look similar. The HDR is hardware-reproduced by monitor, there's no way on the software side to take screenshots that precisely show extended dynamic range. Probably the screenshots should be taken as pictures from the monitor using a good camera. I have one, but I don't have enough time, I'll see what I can do about it!

Also squashed the PR and updated code by our conversation. Answered on the rest of the issues.

@skrew
Copy link

skrew commented Dec 29, 2020

Please don't forget iOS version for the HDR support, i think it's almost the same as MacOS

@Akemi
Copy link
Member

Akemi commented Dec 29, 2020

Please don't forget iOS version for the HDR support, i think it's almost the same as MacOS

this has nothing to do with iOS though. there is no iOS backend in mpv. on iOS you will use libmpv and there the layer is handled by the libmpv user. so it's entirely in the hands of the one using libmpv on iOS to support this.

@Akemi Akemi mentioned this pull request Jan 14, 2021
@anastasiuspernat
Copy link
Author

@Akemi
Are there still any problems? Do I need to fix anything? Thank you and Happy New Year!

@Akemi
Copy link
Member

Akemi commented Jan 15, 2021

i just didn't get around looking at it again.

@Akemi
Copy link
Member

Akemi commented Jan 19, 2021

i opened my own PR #8485. i don't mean this to be disrespectful towards you and i hope it isn't seen as this. but what i had in mind and what you did here was a bit too far apart for me to explain everything i wanted. it would be nice if you could also test my PR.

nevertheless i will give you some more feedback on this PR for the future.

{"macos-output-csp", OPT_CHOICE(macos_output_csp,
{"auto", -1}, {"displayP3_HLG", 0}, {"displayP3_PQ_EOTF", 1},
{"extendedLinearDisplayP3", 2}, {"itur_2020", 3}, {"itur_2020_HLG", 4},
{"itur_2020_PQ_EOTF", 5}, {"extendedSRGB",6}, {"extendedLinearSRGB",7}
Copy link
Member

Choose a reason for hiding this comment

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

the extended color spaces expect component values below 0 and above 1.0. that's what mpv can't do atm and with that those color spaces are a bit useless atm.

layer?.colorspace = colorSpace.cgColorSpace
}
super.updateICCProfile()
libmpv.setRenderICCProfile(colorSpace!)
Copy link
Member

Choose a reason for hiding this comment

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

never use a forced unwrapping in swift if not necessary. always use a guard, if let or ? instead of !. otherwise it will only end up in hard crashes.


if (mpv?.macOpts.macos_output_csp != -1) {
(view.layer as! CAOpenGLLayer).wantsExtendedDynamicRangeContent = true
view.wantsExtendedDynamicRangeOpenGLSurface = true;
Copy link
Member

Choose a reason for hiding this comment

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

semicolon not needed

if #available(macOS 10.12, *) {
switch (mpv?.macOpts.macos_output_csp)
{
case 6: name = CGColorSpace.extendedSRGB; break
Copy link
Member

@Akemi Akemi Jan 19, 2021

Choose a reason for hiding this comment

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

breaks are not necessary on swift switch case. it will only fall through if an explicit fallthrough keyword is used. also the semicolons.

@anastasiuspernat
Copy link
Author

anastasiuspernat commented Jan 19, 2021

@Akemi

i opened my own PR #8485. i don't mean this to be disrespectful towards you and i hope it isn't seen as this. but what i had in mind and what you did here was a bit too far apart for me to explain everything i wanted. it would be nice if you could also test my PR.

nevertheless i will give you some more feedback on this PR for the future.

No worries haha! You better know your codebase, also Swift is a new language to me.

In any case my main intention was to make IINA (mpv-based video player) play with my HDR monitor on macOS. I thought mpv is the main culprit but now I see it's not. My fork of IINA now plays HDR videos perfectly so my main goal is achieved and I just wanted to give back my findings (which are based on your knowledge) to the world.

I rarely take part in opensource and it was a lot of fun.

I'll try to test and answer when I have time! Thanks again for the guidance.

@DavidSchechter
Copy link

@Akemi @anastasiuspernat any progress on this?

@JulyIghor
Copy link

any progress on this?

@Akemi Akemi added the os:mac label Mar 16, 2024
@Akemi
Copy link
Member

Akemi commented Apr 19, 2024

sorry for stalling this for so long. i am going to close this one i favour of #8485. i will also revisit #8485 and see how we will proceed with it. also see the last comment und the linked issue for discussion (#7341 (comment)).

@Akemi Akemi closed this Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants