Skip to content

Instantly share code, notes, and snippets.

@Endilll
Created May 16, 2018 19:32
Show Gist options
  • Save Endilll/21f895c947075e95afb500143dd617d4 to your computer and use it in GitHub Desktop.
Save Endilll/21f895c947075e95afb500143dd617d4 to your computer and use it in GitHub Desktop.
recreatePreviewNode
bool VapourSynthScriptProcessor::recreatePreviewNode(NodePair & a_nodePair)
{
if(!a_nodePair.pOutputNode)
return false;
if(!m_cpVSAPI)
return false;
if(a_nodePair.pPreviewNode)
{
m_cpVSAPI->freeNode(a_nodePair.pPreviewNode);
a_nodePair.pPreviewNode = nullptr;
}
const VSVideoInfo * cpVideoInfo =
m_cpVSAPI->getVideoInfo(a_nodePair.pOutputNode);
if(!cpVideoInfo)
return false;
const VSFormat * cpFormat = cpVideoInfo->format;
if(cpFormat->id == pfCompatBGR32)
{
a_nodePair.pPreviewNode =
m_cpVSAPI->cloneNodeRef(a_nodePair.pOutputNode);
return true;
}
bool isYUV = ((cpFormat->colorFamily == cmYUV) ||
(cpFormat->id == pfCompatYUY2));
bool canSubsample = (isYUV || (cpFormat->colorFamily == cmYCoCg));
VSCore * pCore = m_pVSScriptLibrary->getCore(m_pVSScript);
VSPlugin * pResizePlugin = m_cpVSAPI->getPluginById(
"com.vapoursynth.resize", pCore);
const char * resizeName = "Point";
VSMap * pArgumentMap = m_cpVSAPI->createMap();
m_cpVSAPI->propSetNode(pArgumentMap, "clip", a_nodePair.pOutputNode,
paReplace);
m_cpVSAPI->propSetInt(pArgumentMap, "format", pfCompatBGR32, paReplace);
if(canSubsample)
{
switch(m_chromaResamplingFilter)
{
case ResamplingFilter::Point:
resizeName = "Point";
break;
case ResamplingFilter::Bilinear:
resizeName = "Bilinear";
break;
case ResamplingFilter::Bicubic:
resizeName = "Bicubic";
m_cpVSAPI->propSetFloat(pArgumentMap, "filter_param_a_uv",
m_resamplingFilterParameterA, paReplace);
m_cpVSAPI->propSetFloat(pArgumentMap, "filter_param_b_uv",
m_resamplingFilterParameterB, paReplace);
break;
case ResamplingFilter::Lanczos:
resizeName = "Lanczos";
m_cpVSAPI->propSetFloat(pArgumentMap, "filter_param_a_uv",
m_resamplingFilterParameterA, paReplace);
break;
case ResamplingFilter::Spline16:
resizeName = "Spline16";
break;
case ResamplingFilter::Spline36:
resizeName = "Spline36";
break;
default:
assert(false);
}
}
m_cpVSAPI->propSetInt(pArgumentMap, "prefer_props", 1, paReplace);
if(isYUV)
{
const char * matrixInS = nullptr;
switch(m_yuvMatrix)
{
case YuvMatrixCoefficients::m709:
matrixInS = "709";
break;
case YuvMatrixCoefficients::m470BG:
matrixInS = "470bg";
break;
case YuvMatrixCoefficients::m170M:
matrixInS = "170m";
break;
case YuvMatrixCoefficients::m2020_NCL:
matrixInS = "2020ncl";
break;
case YuvMatrixCoefficients::m2020_CL:
matrixInS = "2020cl";
break;
default:
assert(false);
}
int matrixStringLength = (int)strlen(matrixInS);
m_cpVSAPI->propSetData(pArgumentMap, "matrix_in_s",
matrixInS, matrixStringLength, paReplace);
if(m_yuvMatrix == YuvMatrixCoefficients::m2020_CL)
{
const char * transferIn = "709";
const char * transferOut = "2020_10";
m_cpVSAPI->propSetData(pArgumentMap, "transfer_in_s",
transferIn, (int)strlen(transferIn), paReplace);
m_cpVSAPI->propSetData(pArgumentMap, "transfer_s",
transferOut, (int)strlen(transferOut), paReplace);
}
}
if(canSubsample)
{
int64_t chromaLoc = 0;
switch(m_chromaPlacement)
{
case ChromaPlacement::MPEG1:
chromaLoc = 1;
break;
case ChromaPlacement::MPEG2:
chromaLoc = 0;
break;
case ChromaPlacement::DV:
chromaLoc = 2;
break;
default:
assert(false);
}
m_cpVSAPI->propSetInt(pArgumentMap, "chromaloc",
chromaLoc, paReplace);
}
VSMap * pResultMap = m_cpVSAPI->invoke(pResizePlugin, resizeName,
pArgumentMap);
m_cpVSAPI->freeMap(pArgumentMap);
const char * cpResultError = m_cpVSAPI->getError(pResultMap);
if(cpResultError)
{
m_error = trUtf8("Failed to convert to RGB:\n");
m_error += cpResultError;
emit signalWriteLogMessage(mtCritical, m_error);
m_cpVSAPI->freeMap(pResultMap);
return false;
}
VSNodeRef * pPreviewNode = m_cpVSAPI->propGetNode(pResultMap, "clip", 0,
nullptr);
assert(pPreviewNode);
a_nodePair.pPreviewNode = pPreviewNode;
m_cpVSAPI->freeMap(pResultMap);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment