Skip to content

Wayland: target_ref_luma support - #18917

Open
kasper93 wants to merge 20 commits into
mpv-player:masterfrom
kasper93:ref_luma_wayland
Open

kasper93 wants to merge 20 commits into
mpv-player:masterfrom
kasper93:ref_luma_wayland

Conversation

@kasper93

Copy link
Copy Markdown
Member

No description provided.

@kasper93

Copy link
Copy Markdown
Member Author

cc @mahkoh

@mahkoh

mahkoh commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

AIUI libplacebo assumes that the reference luminance is 203. That was the reason for doing the transformation. Has this changed? Or do you use some other method for telling libplacebo about the luminance to tonemap to?

@kasper93

Copy link
Copy Markdown
Member Author

AIUI libplacebo assumes that the reference luminance is 203.

It does it only as the default for SDR content. libplacebo did not change, mpv now tells it the reference white.

  • libplacebo has no reference luminance for HDR. PQ is in nits as defined in BT.2100, and it tone maps from the range of the source to min_luma / max_luma of the target, also in nits. 203 (PL_COLOR_SDR_WHITE) is only where SDR content goes when nothing else is known, and a frame overrides it with its own max_luma.
  • target_ref_luma uses exactly that (it is what --hdr-reference-white=auto reads). SDR frames, OSD and subtitles get the compositor's reference white as max_luma. It is the path Windows already takes with its SDR white level.
  • So the preferred description goes to the renderer as it was sent, and its min and max are what we tone map to. The surface is then declared with the compositor's own reference white (set_luminances(min, max, ref), for PQ too). The anchoring factor is 1, and the compositor shows what we rendered.

Pass-through did not change. Hint mode source and dmabuf-wayland declare the description of the source, with 203 for PQ, and the compositor anchors it. mpv draws OSD and subtitles at 203 there.

One known limitation. Without set_luminances support a PQ surface cannot declare its reference white, and we no longer convert for that case. It is right when the compositor's reference white is 203 and off by reference / 203 otherwise. But I don't care about this case, if compositor cares it should implement set_luminances.

@mahkoh

mahkoh commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

The important consideration is this: Ignoring minimum luminances for a moment, if mpv receives either of the following descriptions

  • ref: 80, target_max: 160
  • ref: 203, target_max: 406

the values it stores in the pixel buffer it gives to the compositor should be exactly the same. IOW, the tone mapping operations should be identical.

If mpv telling libplacebo the reference white ensures that, then I guess it's fine.

@kasper93

Copy link
Copy Markdown
Member Author

the values it stores in the pixel buffer it gives to the compositor should be exactly the same

That holds for SDR content. It does not hold for HDR content, and that is the intended change.

  • SDR content (SDR video, OSD, subtitles) is placed at ref. With a relative transfer like KWin's gamma 2.2 the buffer then only depends on ref / target_max, so 300 / 600 and 203 / 406 give the same values. Your 80 / 160 does not, but only because libplacebo treats a peak up to 203 as plain SDR. We use 80 / 80 there and leave the headroom unused. On screen SDR white is at the reference white in every case.
  • HDR content is in nits, and so is the description now. A 100 nits pixel stays at 100 nits, which is 100 / 600 of one target and 100 / 406 of the other. A movie that peaks at 500 fits the first output and is tone mapped on the second. So the tone mapping differs, because the two descriptions say different things about the output, a peak of 600 nits and one of 406.

The two descriptions are only equivalent for a relative signal. PQ content and its metadata are absolute, and scaling the result after we tone mapped it gives a different picture than the one we made. #18857 is that case, HDR at 46 % with KDE's SDR brightness at 94.

Additionally, we no longer lie about the capabilities of the monitor. KWin, for example, reports the HDR values from its calibration in nits. For my monitor that is 465 nits, as expected, and not the 307 nits that current master reports.

@mahkoh

mahkoh commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Your 80 / 160 does not, but only because libplacebo treats a peak up to 203 as plain SDR.

If libplacebo hard codes 203 anywhere instead of using the reference white you pass in, then this patch is not correct.

@kasper93

Copy link
Copy Markdown
Member Author

Your 80 / 160 does not, but only because libplacebo treats a peak up to 203 as plain SDR.

If libplacebo hard codes 203 anywhere instead of using the reference white you pass in, then this patch is not correct.

I don't consider this case particularly important, it's not really a "HDR". I can improve it separately, but for now anything < 203 is SDR and rendered at reference luminance.

@Zamundaaa

Copy link
Copy Markdown
Contributor

If apps start ignoring the reference luminance like this, I will have to fix it on the compositor side by not telling apps about the real reference luminance anymore. That would be incredibly stupid and annoying, but if apps can't be trusted to follow the absolute basics of the color management protocol, I can't see another choice.

Maybe Apple wasn't so wrong with their "EDR" concept. While it's more limiting in what you can do for meaningful color management, it also helps prevent apps from pulling stunts like this.

@mahkoh

mahkoh commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

I can improve it separately

I don't know why you need to improve anything. The current code (without this PR) is the result of days of discussions and already works.

set_mastering_luminance belongs to the set_mastering_display_primaries
feature.
Both are optional and stay undefined unless they are set. A 0 is a set
value, and version 1 of the protocol requires a set value to be above
the minimum of the mastering luminance range.
So it matches mastering luminance which is also rounded.
Use effective reference white, instead of hardcoded one.
It tells if the params are made for the target device or are those of
the source. The distinction is unfortunatelly needed, becasue Wayland
has HDR10 reference white scaling, which we have to undo.
It tells if the params are made for the output or are those of the
source, as for set_color. vo_dmabuf_wayland and vo_wlshm pass the
params of the source through.
An SDR or linear signal was declared with 203 nits as its reference
white, also where it peaks below that, as with --target-peak=100. By
the protocol a reference white above the maximum needs the
extended_target_volume feature, and Mutter raises a protocol error for
it. The white of such a signal is at its peak.
The luminances of the preferred image description are converted to the
scale of 203 nits and the surface declares 203 as its reference white.
The compositor then shows all of it brighter or darker by the ratio of
its own reference white to 203, which is the model of the protocol and
stays the default.

With the option the luminances of the output are used as they are, and
the surface declares the reference white of the compositor, which shows
it as it is. HDR content keeps its luminance. SDR content, the OSD and
subtitles are at the reference white of the compositor in both cases. A
signal of the source, as with vo_dmabuf_wayland or
--target-colorspace-hint-mode=source, stays in the scale of 203 nits.
vo_gpu_next encodes HLG for the black and the peak of its target, while
an image description without luminances stands for a display of 1000
nits with the reference white at 203. Declare the range of the signal
with its reference white, as for PQ. HLG of the source keeps the
defaults.
Two places take the target as HDR by its transfer function. scRGB ends
at the reference white on a target that is not HDR, and inverse tone
mapping takes the transfer function and the luminances of one that is.

A Wayland compositor can prefer an SDR transfer function with a peak
above the reference white for an HDR output, KWin does. That was hidden
as long as such a transfer function is replaced by PQ.
With --wayland-respect-pq-scale an SDR or linear signal was declared
with its reference white at the peak, which is where vo_gpu_next
matches. An HDR output also shows such a signal above its reference
white. Gamma 2.2 with the white at 400 nits and the peak at 1000 nits is
the description that KWin prefers there. Report that through
sdr_above_ref_luma and declare the reference white of the output for
such a signal, capped at its peak.

Note that I don't endorse using Gamma 2.2 transfer for HDR signals, but
this is what Wayland forces on us, and undoing it causes more pain that
it's worth.
The peak of an SDR target is set to the reference white once the system
reports one. Windows needs that, it shows the peak of an SDR swapchain
at its SDR white level. A Wayland compositor shows an SDR signal with
the white where the image description has it, so --target-trc=gamma2.2
on an HDR output can go up to the peak of the output, as it did before
the reference white was reported there.
Now that we plug the reference white into the SDR transfers, we can try
to match the descriptors directly. For better or for worse. It's on
Wayland then to provide sane luminance ranges. They say everything is
relative, but the HDR10 metadata is not. We just follow what the
compositor says, let's see how it ends up.
With the hint off the hint mode has no meaning, the frame is rendered
for the target in every mode.
It tells if the signal is made for the target device or is the one of
the source, as for set_color. Wayland anchors a signal of the source at
its own reference white, so the one of the output doesn't apply to it.
The compositor anchors a signal of the source at its own reference
white, so the one of the output doesn't apply to what the renderer
draws into it. With --wayland-respect-pq-scale the OSD and subtitles
on a passed through HDR video were off by the ratio of the two.
@kasper93

Copy link
Copy Markdown
Member Author

If apps start ignoring the reference luminance like this

It was not ignored, but I see the point about the default, so I changed the PR.

  • By default nothing changes for luminance. Everything stays relative to the reference white as on master, and the compositor scales it with the brightness.
  • Rendering PQ as mastered is an option now, --wayland-respect-pq-scale, off by default. With it the luminances you report are used as they are. SDR video, OSD and subtitles are rendered at the reference luminance of the output and the surface is declared with that same reference luminance, so the anchoring holds as the protocol describes it. Only HDR video keeps its absolute luminance.
  • --target-colorspace-hint-mode=source and dmabuf-wayland are untouched. There the compositor does the anchoring and all the tone mapping.

The option is there because a movie carries no reference white. There is no metadata, and the protocol itself calls the 203 for PQ "a protocol addition" that is "not part of ST 2084 or BT.2100". With the scaling a movie is shown as mastered only while the slider sits at exactly 203. Whoever lowers it for the desktop, as in #18857, dims every movie with it. Some users want that and some don't, it is a choice.

I don't think it's on compositor to impose such choice and force users to manually move slider to some magic position to have their movies rendered correctly. At the same time 203 is often to bright for SDR/UI content, so it's unrealistic to make users use it by default.

I don't know why you need to improve anything.

That was about your 80 / 160 case. With the default back in the scale of 203 it works as on master. Only the option needs help from libplacebo there, and I have that as a patch.

The current code (without this PR) is the result of days of discussions and already works.

It stays the default. What the PR still changes by default is that the preferred transfer function is used as it is, so KWin gets its extended range gamma 2.2 instead of forced PQ, with the same luminances as before. The rest are fixes, like a reference white above the maximum of an SDR signal, which Mutter throws protocol error.

To show what the option is for, take my display. KWin's preferred description has a peak of 465 and a reference white of 265, and 465 is what the panel really reaches. The table shows where things land on screen, in nits.

default with the option
peak the renderer is given 356 465
SDR white 265 265
PQ patch of 99 132 102
PQ patch of 201 268 206
PQ patch of 401 465 404

It's generally not expected to ignore PQ curve and many purist may prefer movies to be rendered as mastered.

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.

3 participants