Video on Linux went through a Flutter texture: 8-bit sRGB, which cannot carry HDR at all, and which forced a whole-window Flutter recomposite for every video frame. This moves it onto a wl_subsurface stacked below the Flutter surface, with mpv rendering into an EGL window surface on it through the libmpv render API. The subsurface is desynchronized, so video and UI now present independently. With the plane in place HDR follows: the surface is described to the compositor through wp_color_manager_v1 as the source's own curve and gamut - PQ or HLG, BT.2020 - carrying whatever HDR10 static metadata the stream actually declares. The description and the buffer it describes land on the same commit, staged and validated before mpv is switched, so a PQ frame is never presented labelled sRGB. A five-second watchdog bounds the one wait a compositor could otherwise leave hanging. A session that cannot host the plane - X11, or a compositor without wl_subcompositor - fails initialize with VIDEO_PLANE_UNSUPPORTED naming the reason: the texture path is gone, and refusing by name beats degrading to something the user cannot see. An SDR output, a missing capability or an 8-bit config keep the plane and simply leave it undescribed. The output's colour state is trusted only when it has been earned. Every landed property step records itself as it lands; a reset or sequence that cannot finish downgrades its result to unknown and marks the applied-output cache untrusted until a clean apply earns it back. A plane whose output state cannot be named is quarantined - hidden, its description withdrawn - and the quarantine is recorded state: an unrelated visibility change cannot put a mislabelled plane back on screen, and only a commit that resolves to a nameable outcome lifts it. A rect collapsing to zero detaches the buffer exactly as hiding does, a refused setVideoRect drops the Dart-side sent-rect cache so the next layout pass retries for free, and a refused tone-mapping pick tells the user instead of dying in a log. NVIDIA's Wayland EGL (through at least 610.xx) offers no 10-bit unorm window configs, so the plane takes half-float as the tier between 10-bit unorm and 8-bit, declares the whole surface opaque so the compositor never reads the alpha those configs carry, and states GL_RGBA16F rather than a 10-bit lie. Whether the output is in HDR is read from luminance headroom above its own reference white rather than from the preferred transfer function, which current KWin no longer answers PQ for; the margin is half a stop, because KWin reports an undimmed maximum over a software-dimmed SDR white. Validated on an RTX 4090 (driver 610.57.04) under KWin 6.7.4 with locked-exposure photographs. Who tone-maps is a user choice. The default is the compositor: photographed on a 400-nit HDR output against a PQ chart it keeps 400 -> 1000 nits monotonic and separated where the player leg flattens them, because the player path drives mpv's legacy vo_gpu, whose own standalone output scores the same. The gap is the renderer, not the wiring. The decision itself - what the source carries, what the output supports, what to tell mpv and what to tell the compositor - lives in hdr_metadata.h, free of Wayland and GTK so its luminance validation can be tested without a display server. Sending an incoherent luminance set is a protocol error that disconnects the client, so the rules are worth a unit test. The deb, rpm and pacman packages now declare wayland-client, wayland-egl and EGL: the plane links them directly and bundle-libs.sh deliberately never bundles them, since they are coupled to the running compositor and GPU driver. lib/dev/harness_main.dart is a second entrypoint for measuring this on hardware - it drives one clip with scripted mpv properties and reports the colour state mpv actually settled on. Nothing imports it, so it is tree-shaken out of the app. Verified on a Steam Deck against an external 400-nit HDR display: the compositor reports PQ / BT.2020, the connector carries HDR_OUTPUT_METADATA, and against mpv vo=gpu-next on the same frame the shipped build sits 4.90 counts away overall - closer to the reference HDR player than to its own SDR fallback.
280 lines
10 KiB
C++
280 lines
10 KiB
C++
#include "video_params.h"
|
|
|
|
#include <iostream>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
int failures = 0;
|
|
|
|
void Expect(bool condition, const char* expression, int line) {
|
|
if (condition) return;
|
|
std::cerr << "line " << line << ": check failed: " << expression << '\n';
|
|
++failures;
|
|
}
|
|
|
|
#define EXPECT(condition) Expect(static_cast<bool>(condition), #condition, __LINE__)
|
|
|
|
mpv_node Text(const char* value) {
|
|
mpv_node node{};
|
|
node.format = MPV_FORMAT_STRING;
|
|
node.u.string = const_cast<char*>(value);
|
|
return node;
|
|
}
|
|
|
|
mpv_node Number(double value) {
|
|
mpv_node node{};
|
|
node.format = MPV_FORMAT_DOUBLE;
|
|
node.u.double_ = value;
|
|
return node;
|
|
}
|
|
|
|
mpv_node Whole(int64_t value) {
|
|
mpv_node node{};
|
|
node.format = MPV_FORMAT_INT64;
|
|
node.u.int64 = value;
|
|
return node;
|
|
}
|
|
|
|
// Builds the node shape mpv delivers for an observed `video-params`: a map whose
|
|
// absent fields are missing keys rather than zeroed ones. Node() borrows the
|
|
// builder's storage, so nothing may be added after it is called.
|
|
class Params {
|
|
public:
|
|
Params& Add(const char* key, mpv_node value) {
|
|
keys_.push_back(const_cast<char*>(key));
|
|
values_.push_back(value);
|
|
return *this;
|
|
}
|
|
|
|
mpv_node Node() {
|
|
list_.num = static_cast<int>(values_.size());
|
|
list_.keys = keys_.empty() ? nullptr : keys_.data();
|
|
list_.values = values_.empty() ? nullptr : values_.data();
|
|
mpv_node node{};
|
|
node.format = MPV_FORMAT_NODE_MAP;
|
|
node.u.list = &list_;
|
|
return node;
|
|
}
|
|
|
|
private:
|
|
std::vector<char*> keys_;
|
|
std::vector<mpv_node> values_;
|
|
mpv_node_list list_{};
|
|
};
|
|
|
|
// A fully described HDR10 master: both names and all four luminances, mixed in
|
|
// among the fields the HDR decision has no use for, since mpv sends the whole
|
|
// map every time and the keys are in mpv's order, not ours.
|
|
void TestEveryFieldPresentIsRead() {
|
|
Params params;
|
|
params.Add("pixelformat", Text("yuv420p10"))
|
|
.Add("w", Whole(3840))
|
|
.Add("gamma", Text("pq"))
|
|
.Add("primaries", Text("bt.2020"))
|
|
.Add("min-luma", Number(0.0001))
|
|
.Add("max-luma", Number(1000.0))
|
|
.Add("max-cll", Number(999.0))
|
|
.Add("max-fall", Number(400.0))
|
|
.Add("chroma-location", Text("mpeg2/4/h264"));
|
|
const mpv_node node = params.Node();
|
|
const mpv::SourceHdrMetadata metadata = mpv::ParseSourceHdrMetadata(&node);
|
|
|
|
EXPECT(metadata.transfer == "pq");
|
|
EXPECT(metadata.primaries == "bt.2020");
|
|
EXPECT(metadata.max_cll == 999.0);
|
|
EXPECT(metadata.max_fall == 400.0);
|
|
EXPECT(metadata.max_luminance == 1000.0);
|
|
EXPECT(metadata.min_luminance == 0.0001);
|
|
}
|
|
|
|
// The common HDR10 case: a PQ / BT.2020 stream that states no static metadata at
|
|
// all. mpv omits the keys entirely, and every luminance has to stay at zero -
|
|
// which is how the rest of the pipeline spells "not stated" - while both names
|
|
// still come through, because they are what decides whether the plane may be
|
|
// described as HDR in the first place.
|
|
void TestNamesSurviveWithNoLuminances() {
|
|
Params params;
|
|
params.Add("gamma", Text("hlg")).Add("primaries", Text("bt.2020"));
|
|
const mpv_node node = params.Node();
|
|
const mpv::SourceHdrMetadata metadata = mpv::ParseSourceHdrMetadata(&node);
|
|
|
|
EXPECT(metadata.transfer == "hlg");
|
|
EXPECT(metadata.primaries == "bt.2020");
|
|
EXPECT(metadata.max_cll == 0.0);
|
|
EXPECT(metadata.max_fall == 0.0);
|
|
EXPECT(metadata.max_luminance == 0.0);
|
|
EXPECT(metadata.min_luminance == 0.0);
|
|
}
|
|
|
|
// Each luminance is independent: a source may state MaxCLL and nothing else, or
|
|
// a mastering range and no light levels. An absent field must never pick up its
|
|
// neighbour's value or a default.
|
|
void TestEachLuminanceIsAbsentOnItsOwn() {
|
|
{
|
|
Params params;
|
|
params.Add("max-cll", Number(1200.0));
|
|
const mpv_node node = params.Node();
|
|
const mpv::SourceHdrMetadata metadata = mpv::ParseSourceHdrMetadata(&node);
|
|
EXPECT(metadata.max_cll == 1200.0);
|
|
EXPECT(metadata.max_fall == 0.0);
|
|
EXPECT(metadata.max_luminance == 0.0);
|
|
EXPECT(metadata.min_luminance == 0.0);
|
|
}
|
|
{
|
|
Params params;
|
|
params.Add("max-fall", Number(250.0));
|
|
const mpv_node node = params.Node();
|
|
const mpv::SourceHdrMetadata metadata = mpv::ParseSourceHdrMetadata(&node);
|
|
EXPECT(metadata.max_cll == 0.0);
|
|
EXPECT(metadata.max_fall == 250.0);
|
|
EXPECT(metadata.max_luminance == 0.0);
|
|
}
|
|
{
|
|
Params params;
|
|
params.Add("min-luma", Number(0.005));
|
|
const mpv_node node = params.Node();
|
|
const mpv::SourceHdrMetadata metadata = mpv::ParseSourceHdrMetadata(&node);
|
|
EXPECT(metadata.max_luminance == 0.0);
|
|
EXPECT(metadata.min_luminance == 0.005);
|
|
}
|
|
{
|
|
Params params;
|
|
params.Add("max-luma", Number(4000.0));
|
|
const mpv_node node = params.Node();
|
|
const mpv::SourceHdrMetadata metadata = mpv::ParseSourceHdrMetadata(&node);
|
|
EXPECT(metadata.max_luminance == 4000.0);
|
|
EXPECT(metadata.min_luminance == 0.0);
|
|
}
|
|
}
|
|
|
|
// The mastering floor is the one luminance a source may legitimately state as
|
|
// zero, so a present zero has to be taken rather than dropped. The others treat
|
|
// zero as no statement, which is the same thing they do with an absent key -
|
|
// and since the field's absent value *is* zero, only a negative can tell the
|
|
// two rules apart from the outside.
|
|
void TestMinLumaAcceptsAZeroTheOthersRefuse() {
|
|
Params params;
|
|
params.Add("gamma", Text("pq"))
|
|
.Add("min-luma", Number(0.0))
|
|
.Add("max-luma", Number(0.0))
|
|
.Add("max-cll", Number(0.0))
|
|
.Add("max-fall", Number(0.0));
|
|
const mpv_node node = params.Node();
|
|
const mpv::SourceHdrMetadata metadata = mpv::ParseSourceHdrMetadata(&node);
|
|
|
|
EXPECT(metadata.min_luminance == 0.0);
|
|
EXPECT(metadata.max_luminance == 0.0);
|
|
EXPECT(metadata.max_cll == 0.0);
|
|
EXPECT(metadata.max_fall == 0.0);
|
|
|
|
// No luminance may be negative. The floor would scale into a mastering
|
|
// minimum the protocol rejects, and a negative peak would sail through the
|
|
// containment checks that only ever compare upwards.
|
|
Params negative;
|
|
negative.Add("min-luma", Number(-1.0))
|
|
.Add("max-luma", Number(-4000.0))
|
|
.Add("max-cll", Number(-1000.0))
|
|
.Add("max-fall", Number(-400.0));
|
|
const mpv_node negative_node = negative.Node();
|
|
const mpv::SourceHdrMetadata refused = mpv::ParseSourceHdrMetadata(&negative_node);
|
|
EXPECT(refused.min_luminance == 0.0);
|
|
EXPECT(refused.max_luminance == 0.0);
|
|
EXPECT(refused.max_cll == 0.0);
|
|
EXPECT(refused.max_fall == 0.0);
|
|
}
|
|
|
|
// The names are mpv's own and this parse does not judge them: SDR curves, gamuts
|
|
// with no protocol counterpart, and anything a future mpv adds all pass through
|
|
// verbatim for the caller to classify. Copying them into an enum here would put
|
|
// the same table in two places.
|
|
void TestUnknownNamesPassThroughVerbatim() {
|
|
Params params;
|
|
params.Add("gamma", Text("bt.1886")).Add("primaries", Text("display-p3")).Add("max-cll", Number(120.0));
|
|
const mpv_node node = params.Node();
|
|
const mpv::SourceHdrMetadata metadata = mpv::ParseSourceHdrMetadata(&node);
|
|
|
|
EXPECT(metadata.transfer == "bt.1886");
|
|
EXPECT(metadata.primaries == "display-p3");
|
|
EXPECT(metadata.max_cll == 120.0);
|
|
}
|
|
|
|
// Everything that is not the map mpv documents has to read as "no stream": the
|
|
// property is unavailable between files and while an audio-only core runs, and
|
|
// the event carries MPV_FORMAT_NONE then. Deciding from a half-read map would
|
|
// describe the plane in a colour space nothing is emitting.
|
|
void TestNonMapNodesYieldNothing() {
|
|
EXPECT(mpv::ParseSourceHdrMetadata(nullptr).transfer.empty());
|
|
|
|
mpv_node none{};
|
|
none.format = MPV_FORMAT_NONE;
|
|
EXPECT(mpv::ParseSourceHdrMetadata(&none).transfer.empty());
|
|
|
|
// The shape an observation of the same property in another format delivers.
|
|
const mpv_node string_node = Text("pq");
|
|
const mpv::SourceHdrMetadata from_string = mpv::ParseSourceHdrMetadata(&string_node);
|
|
EXPECT(from_string.transfer.empty());
|
|
EXPECT(from_string.primaries.empty());
|
|
|
|
// An array is a map's near neighbour and has no keys to walk.
|
|
mpv_node_list list{};
|
|
list.num = 1;
|
|
mpv_node array_node{};
|
|
array_node.format = MPV_FORMAT_NODE_ARRAY;
|
|
array_node.u.list = &list;
|
|
EXPECT(mpv::ParseSourceHdrMetadata(&array_node).transfer.empty());
|
|
|
|
// A map claiming entries it does not carry, which is what a truncated or
|
|
// hostile payload looks like.
|
|
mpv_node empty_map{};
|
|
empty_map.format = MPV_FORMAT_NODE_MAP;
|
|
empty_map.u.list = &list;
|
|
EXPECT(mpv::ParseSourceHdrMetadata(&empty_map).transfer.empty());
|
|
|
|
mpv_node null_map{};
|
|
null_map.format = MPV_FORMAT_NODE_MAP;
|
|
EXPECT(mpv::ParseSourceHdrMetadata(&null_map).transfer.empty());
|
|
}
|
|
|
|
// A key whose value is not the format mpv documents for it is ignored rather
|
|
// than reinterpreted, and it must not stop the rest of the map being read. An
|
|
// integer luminance is the one exception: the blocking sub-property read this
|
|
// replaced asked for a double and libmpv would have converted it.
|
|
void TestMistypedValuesAreSkippedButIntegersAreNot() {
|
|
Params params;
|
|
params.Add("gamma", Number(2.2))
|
|
.Add("primaries", Text("bt.2020"))
|
|
.Add("max-cll", Text("1000"))
|
|
.Add("max-luma", Whole(4000))
|
|
.Add("min-luma", Text("0.0001"));
|
|
const mpv_node node = params.Node();
|
|
const mpv::SourceHdrMetadata metadata = mpv::ParseSourceHdrMetadata(&node);
|
|
|
|
EXPECT(metadata.transfer.empty());
|
|
EXPECT(metadata.primaries == "bt.2020");
|
|
EXPECT(metadata.max_cll == 0.0);
|
|
EXPECT(metadata.max_luminance == 4000.0);
|
|
EXPECT(metadata.min_luminance == 0.0);
|
|
|
|
// mpv owns the strings, and a null one is not a name.
|
|
Params null_name;
|
|
null_name.Add("gamma", Text(nullptr)).Add("primaries", Text("bt.2020"));
|
|
const mpv_node null_name_node = null_name.Node();
|
|
const mpv::SourceHdrMetadata from_null = mpv::ParseSourceHdrMetadata(&null_name_node);
|
|
EXPECT(from_null.transfer.empty());
|
|
EXPECT(from_null.primaries == "bt.2020");
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
TestEveryFieldPresentIsRead();
|
|
TestNamesSurviveWithNoLuminances();
|
|
TestEachLuminanceIsAbsentOnItsOwn();
|
|
TestMinLumaAcceptsAZeroTheOthersRefuse();
|
|
TestUnknownNamesPassThroughVerbatim();
|
|
TestNonMapNodesYieldNothing();
|
|
TestMistypedValuesAreSkippedButIntegersAreNot();
|
|
return failures == 0 ? 0 : 1;
|
|
}
|