Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 52 additions & 4 deletions src/lib/ip/OCIONodes/OCIOIPNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ namespace IPCore
declareProperty<StringProperty>("ocio.inColorSpace", "");

declareProperty<StringProperty>("ocio_color.outColorSpace", "");
declareProperty<IntProperty>("ocio_color.invertView", 0);

declareProperty<StringProperty>("ocio_look.look", "");
declareProperty<IntProperty>("ocio_look.direction", 0);
Expand Down Expand Up @@ -544,11 +545,53 @@ namespace IPCore
//

string outName = stringProp("ocio_color.outColorSpace", m_state->linear);
OCIO::ConstColorSpaceRcPtr dstCS = m_state->config->getColorSpace(outName.c_str());
processor = m_state->config->getProcessor(m_state->context, srcCS, dstCS);

size_t hashValue = string_hash(inName + outName);
shaderName << "OCIO_c_" << shaderLegal(inName) << "_2_" << shaderLegal(outName) << "_" << name() << "_" << hex << hashValue;
if (intProp("ocio_color.invertView", 0) != 0)
{
//
// Display-referred input (graded video) that will be
// shown through ocio_display.display/view: convert it
// to that display's color space, then invert the view
// into outName. The display node's forward view then
// gives the input back unchanged, wherever the view's
// gamut and peak can show it.
//

string display = stringProp("ocio_display.display", m_state->display);
string view = stringProp("ocio_display.view", m_state->view);
string displayCS = m_state->config->getDisplayViewColorSpaceName(display.c_str(), view.c_str());
if (displayCS == OCIO::OCIO_VIEW_USE_DISPLAY_NAME)
{
displayCS = display;
}

OCIO::ColorSpaceTransformRcPtr toDisplay = OCIO::ColorSpaceTransform::Create();
toDisplay->setSrc(inName.c_str());
toDisplay->setDst(displayCS.c_str());

OCIO::DisplayViewTransformRcPtr fromView = OCIO::DisplayViewTransform::Create();
fromView->setSrc(outName.c_str());
fromView->setDisplay(display.c_str());
fromView->setView(view.c_str());
fromView->setDirection(OCIO::TRANSFORM_DIR_INVERSE);

OCIO::GroupTransformRcPtr transform = OCIO::GroupTransform::Create();
transform->appendTransform(toDisplay);
transform->appendTransform(fromView);
processor = m_state->config->getProcessor(m_state->context, transform, OCIO::TRANSFORM_DIR_FORWARD);

size_t hashValue = string_hash(inName + display + view + outName);
shaderName << "OCIO_cv_" << shaderLegal(inName) << "_" << shaderLegal(view) << "_" << name() << "_" << hex << hashValue;
}
else
{
OCIO::ConstColorSpaceRcPtr dstCS = m_state->config->getColorSpace(outName.c_str());
processor = m_state->config->getProcessor(m_state->context, srcCS, dstCS);

size_t hashValue = string_hash(inName + outName);
shaderName << "OCIO_c_" << shaderLegal(inName) << "_2_" << shaderLegal(outName) << "_" << name() << "_" << hex
<< hashValue;
}
}
else if (ociofunction == "look")
{
Expand Down Expand Up @@ -684,6 +727,11 @@ namespace IPCore
shaderName << "OCIO_sd_" << name() << "_" << hex << hashValue;
}

if (!processor)
{
TWK_THROW_EXC_STREAM("unknown ocio.function '" << ociofunction << "'");
}

OCIO::GpuShaderDescRcPtr shaderDesc = OCIO::GpuShaderDesc::CreateShaderDesc();
shaderDesc->setFunctionName(shaderName.str().c_str());
shaderDesc->setLanguage(GPULanguage);
Expand Down
173 changes: 163 additions & 10 deletions src/plugins/rv-packages/ocio_source_setup/ocio_source_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ def ocio_colorspace_from_metadata(config, attributes):
SDR_SCENE_VIEWS = ["ACES 2.0 - SDR 100 nits (Rec.709)"]
HDR_SCENE_VIEWS = ["ACES 2.0 - HDR 1000 nits (P3 D65)"]

# Graded video goes back through the inverse of the view, so the view's
# gamut has to hold it: Rec.2020 HDR video in the session gets the
# Rec.2020-limited view, or its colors outside P3 are compressed.
REC2020_HDR_SCENE_VIEWS = ["ACES 2.0 - HDR 1000 nits (Rec.2020)"] + HDR_SCENE_VIEWS
REC2020_HDR_VIDEO = (
METADATA_COLORSPACES[("SMPTE-2084", "Rec2020")] + METADATA_COLORSPACES[("Hybrid Log-Gamma", "Rec2020")]
)


def read_presentation_mode():
'The macOS presentation mode (sdr, edr, pq or hlg) as MetalView reads it, or "" off macOS.'
Expand Down Expand Up @@ -226,11 +234,12 @@ def warnAboutDisplay(config, display):
PRESENTATION_MODE = read_presentation_mode()


def auto_display_view(config, mode, colorspace):
def auto_display_view(config, mode, colorspace, videoColorSpaces=()):
"""
The OCIO display for the presentation mode and the view for a source
in the given color space. Falls back to the config's defaults for
anything the config doesn't have.
in the given color space, with the session's graded video in
videoColorSpaces. Falls back to the config's defaults for anything
the config doesn't have.
"""
display = PRESENTATION_DISPLAYS.get(mode, "")
if display not in config.getDisplays():
Expand All @@ -242,6 +251,8 @@ def auto_display_view(config, mode, colorspace):
encoding = ""
if encoding in VIDEO_ENCODINGS:
candidates = VIDEO_VIEWS
elif mode in ("pq", "hlg") and any(c in REC2020_HDR_VIDEO for c in videoColorSpaces):
candidates = REC2020_HDR_SCENE_VIEWS
elif mode in ("pq", "hlg"):
candidates = HDR_SCENE_VIEWS
else:
Expand All @@ -267,7 +278,12 @@ def ocio_node_from_media(config, node, default, media=None, attributes={}):
nodeType = commands.nodeType(node)

if nodeType == "RVDisplayPipelineGroup":
display, view = auto_display_view(config, PRESENTATION_MODE, attributes.get("input_colorspace", ""))
display, view = auto_display_view(
config,
PRESENTATION_MODE,
attributes.get("input_colorspace", ""),
attributes.get("video_colorspaces", ()),
)

result = [
{
Expand Down Expand Up @@ -361,6 +377,15 @@ def F():
".ocio_display.view",
".ocio_look.look",
)
DISPLAY_PROPERTIES = (".ocio.active", ".ocio_display.display", ".ocio_display.view")


def displayValue(node):
"(display, view) of an OCIO node."
return (
commands.getStringProperty(node + ".ocio_display.display")[0],
commands.getStringProperty(node + ".ocio_display.view")[0],
)


def currentSourceOCIOValue(nodeType, prop, unmanaged=BUILT_IN):
Expand Down Expand Up @@ -512,6 +537,27 @@ def restorePipelineProperties(pipeline, saved):
pass


def allSources():
return commands.nodesOfType("RVFileSource") + commands.nodesOfType("RVImageSource")


def sourceOCIOFile(source):
srcPipeline = groupMemberOfType(commands.nodeGroup(source), OCIO_ROLES["OCIOFile"])
return groupMemberOfType(srcPipeline, "OCIOFile")


def displayOCIONode(group):
return groupMemberOfType(groupMemberOfType(group, "RVDisplayPipelineGroup"), "OCIODisplay")


def isVideoColorSpace(config, colorspace):
"True for a display-referred (graded video) color space."
try:
return config.getColorSpace(colorspace).getEncoding() in VIDEO_ENCODINGS
except Exception:
return False


def currentSource():
"The file or image source at the current frame, else the first one, else None."
for sourceType in ("RVFileSource", "RVImageSource"):
Expand Down Expand Up @@ -724,8 +770,7 @@ def useDisplayOCIO(self, group, source=None):
DEFAULT_PIPE[groupName] = DEFAULT_RV_PIPE[groupName]
else:
DEFAULT_PIPE[groupName] = currentPipelineNodes
attributes = {"input_colorspace": self.inputColorSpace(source)}
pipelineList = ocio_node_from_media(self.config, dpipeline, DEFAULT_PIPE[groupName], None, attributes)
pipelineList = self.displayPipeline(dpipeline, source)
except Exception as inst:
print(("ERROR: Problem occurred while loading OCIO settings for OCIODisplay: %s" % inst))
return
Expand Down Expand Up @@ -766,10 +811,53 @@ def useDisplayOCIO(self, group, source=None):
self.usingOCIOForDisplay[group] = True
dOCIO = groupMemberOfType(dpipeline, "OCIODisplay")
if dOCIO is not None:
warnAboutDisplay(self.config, commands.getStringProperty(dOCIO + ".ocio_display.display")[0])
self.autoViews[group] = displayValue(dOCIO)
warnAboutDisplay(self.config, self.autoViews[group][0])
self.useSourceOCIOInputs()
commands.redraw()

def displayPipeline(self, dpipeline, source=None):
attributes = {
"input_colorspace": self.viewColorSpace(source),
"video_colorspaces": [
c for c in map(self.inputColorSpace, allSources()) if isVideoColorSpace(self.config, c)
],
}
return ocio_node_from_media(self.config, dpipeline, DEFAULT_PIPE["RVDisplayPipelineGroup"], None, attributes)

def viewColorSpace(self, source=None):
"""
The input color space to choose the view for. Graded video comes
through any view unchanged (see syncVideoInputs), so a scene-referred
source anywhere in the session decides; otherwise source, or the
current one.
"""
for s in allSources():
colorspace = self.inputColorSpace(s)
if colorspace and not isVideoColorSpace(self.config, colorspace):
return colorspace
return self.inputColorSpace(source)

def updateAutoViews(self):
"""
A source added later can change the view to choose: switch the
displays whose view UTV chose and the user hasn't changed since.
"""
for group, chosen in list(self.autoViews.items()):
dOCIO = displayOCIONode(group) if self.usingOCIOForDisplay.get(group, False) else None
if dOCIO is None or displayValue(dOCIO) != chosen:
del self.autoViews[group]
continue
try:
properties = self.displayPipeline(groupMemberOfType(group, "RVDisplayPipelineGroup"))[0]["properties"]
wanted = (properties["ocio_display.display"], properties["ocio_display.view"])
except Exception:
continue
if wanted != chosen:
print("INFO: OCIO display view is now %s · %s" % wanted)
self.autoViews[group] = wanted
ocioDisplayEvent(group, wanted[0], wanted[1])(None)

def disableDisplayOCIO(self, group):
"""
This reverts the DisplayGroup's display pipeline back to using
Expand All @@ -791,8 +879,11 @@ def disableDisplayOCIO(self, group):
print(("INFO: using RVDisplayColor for display: %s" % device))

self.usingOCIOForDisplay[group] = False
self.autoViews.pop(group, None)
if not any(self.usingOCIOForDisplay.values()):
self.disableSourceOCIOInputs()
else:
self.syncVideoInputs()
commands.redraw()

def sourceUsesOCIOInput(self, source):
Expand Down Expand Up @@ -857,15 +948,69 @@ def useSourceOCIOInputs(self, sources=None):
continue
self.useSourceOCIO(source, "OCIOFile", colorspace)
if self.sourceUsesOCIOInput(source):
self.autoInputSources.add(source)
self.autoInputSources[source] = colorspace
print("INFO: %s is %s for the OCIO display" % (source, colorspace))
self.syncVideoInputs()

def disableSourceOCIOInputs(self):
for source in self.autoInputSources:
if source in commands.nodesOfType("RVFileSource") + commands.nodesOfType("RVImageSource"):
if source in allSources():
self.disableSourceOCIO(source, "OCIOFile")
self.autoInputSources.clear()

def mainDisplayView(self):
"The display and view of the main window's OCIO display (else any OCIO display's), or None."
groups = [g for g in commands.nodesOfType("RVDisplayGroup") if self.usingOCIOForDisplay.get(g, False)]
groups.sort(key=lambda g: commands.getStringProperty(g + ".device.moduleName")[0] != "Desktop")
for group in groups:
dOCIO = displayOCIONode(group)
if dOCIO is not None:
return displayValue(dOCIO)
return None

def syncVideoInputs(self):
"""
Show graded video as graded whatever the view. Each display-referred
source UTV gave an input color space goes through the inverse of
the main display's view (the OCIO node's ocio_color.invertView), so
the view gives the video back unchanged where it can show it, and
only scene-referred media gets the view's tone mapping. A source
whose input the user has changed since is theirs and left alone.
"""
if self.config is None:
return
displayView = self.mainDisplayView()
for source, colorspace in list(self.autoInputSources.items()):
ocioFile = sourceOCIOFile(source) if source in allSources() else None
invertProp = None if ocioFile is None else ocioFile + ".ocio_color.invertView"
if invertProp is None or not commands.propertyExists(invertProp):
del self.autoInputSources[source]
continue
if commands.getStringProperty(ocioFile + ".ocio.inColorSpace")[0] != colorspace:
del self.autoInputSources[source]
commands.setIntProperty(invertProp, [0], True)
continue
if displayView is not None and isVideoColorSpace(self.config, colorspace):
wanted = (1,) + displayView
else:
wanted = (0,) + displayValue(ocioFile)
if (commands.getIntProperty(invertProp)[0],) + displayValue(ocioFile) == wanted:
continue
commands.setIntProperty(ocioFile + ".ocio.active", [0], True)
commands.setStringProperty(ocioFile + ".ocio_display.display", [wanted[1]], True)
commands.setStringProperty(ocioFile + ".ocio_display.view", [wanted[2]], True)
commands.setIntProperty(invertProp, [wanted[0]], True)
commands.setIntProperty(ocioFile + ".ocio.active", [1], True)

def scheduleVideoSync(self):
if not self.videoSyncPending:
self.videoSyncPending = True
QTimer.singleShot(0, self.runVideoSync)

def runVideoSync(self):
self.videoSyncPending = False
self.syncVideoInputs()

def sourceSetup(self, event):
"""
This function should be bound to the "source-group-complete" event. It
Expand All @@ -887,6 +1032,7 @@ def sourceSetup(self, event):

if any(self.usingOCIOForDisplay.values()):
self.useSourceOCIOInputs([source])
self.updateAutoViews()

#
# If this is the first OCIO color pipeline for a source assume
Expand Down Expand Up @@ -948,6 +1094,11 @@ def maybeUpdateViews(self, event):
event.reject()
if event.contents().endswith(MENU_PROPERTIES):
self.scheduleMenuUpdate()
if (
event.contents().endswith(DISPLAY_PROPERTIES)
and commands.nodeType(event.contents().split(".")[0]) == "OCIODisplay"
):
self.scheduleVideoSync()

def updateMenuForCurrentSource(self, event):
event.reject()
Expand Down Expand Up @@ -1170,7 +1321,9 @@ def __init__(self):
self.usingOCIOForDisplay = {}
self.readingSession = False
self.config = None
self.autoInputSources = set()
self.autoInputSources = {}
self.autoViews = {}
self.videoSyncPending = False
self.builtInProperties = {}
self.menuUpdatePending = False
self.menuSourceState = None
Expand Down
Loading