Skip to content

Commit

Permalink
OpenGLVideo: Move colourspace primary conversion into subroutine
Browse files Browse the repository at this point in the history
- in preparation for extending functionality
  • Loading branch information
mark-kendall committed Jul 4, 2019
1 parent defa406 commit 8a9f4a8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
12 changes: 5 additions & 7 deletions mythtv/libs/libmythtv/openglvideo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,6 @@ bool OpenGLVideo::CreateVideoShader(VideoShaderType Type, MythDeintType Deint)
defines << "YUY2";
}

// if primaries are disabled, the primaries conversion matrix will be identity
// and we can optimise out the conversion.
QMatrix4x4 primaries = m_videoColourSpace->GetPrimaryMatrix();
if (!primaries.isIdentity())
defines << "PRIMARIES";

#ifdef USING_VTB
// N.B. Rectangular texture support is only currently used for VideoToolBox
// video frames which are NV12. Do not use rectangular textures for the 'default'
Expand Down Expand Up @@ -380,6 +374,9 @@ bool OpenGLVideo::CreateVideoShader(VideoShaderType Type, MythDeintType Deint)
}
}

// Retrieve colour mappping defines
defines << m_videoColourSpace->GetColourMappingDefines();

// Add defines
QString glsldefines;
foreach (QString define, defines)
Expand All @@ -399,8 +396,9 @@ bool OpenGLVideo::CreateVideoShader(VideoShaderType Type, MythDeintType Deint)
QString glslsamplers;
for (int i = start; i < end; ++i)
glslsamplers += QString("uniform sampler2D s_texture%1;\n").arg(i);
fragment = glsldefines + YUVFragmentExtensions + glslsamplers + fragment;

// construct the final shader string
fragment = glsldefines + YUVFragmentExtensions + glslsamplers + fragment;
}

m_shaderCost[Type] = cost;
Expand Down
15 changes: 10 additions & 5 deletions mythtv/libs/libmythtv/openglvideoshaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,17 @@ static const QString YUVFragmentShader =
"uniform highp mat4 m_colourMatrix;\n"
"uniform highp vec4 m_frameData;\n"
"varying highp vec2 v_texcoord0;\n"
"#ifdef MYTHTV_PRIMARIES\n"
"#ifdef MYTHTV_COLOURMAPPING\n"
"uniform highp mat4 m_primaryMatrix;\n"
"uniform highp float m_colourGamma;\n"
"uniform highp float m_displayGamma;\n"
"highp vec4 ColourMap(highp vec4 color)\n"
"{\n"
" highp vec4 res = clamp(color, 0.0, 1.0);\n"
" res.rgb = pow(res.rgb, vec3(m_colourGamma));\n"
" res = m_primaryMatrix * res;\n"
" return vec4(pow(res.rgb, vec3(m_displayGamma)), res.a);\n"
"}\n"
"#endif\n"

// Chroma for lines 1 and 3 comes from line 1-2
Expand Down Expand Up @@ -174,10 +181,8 @@ static const QString YUVFragmentShader =
" gl_FragColor = vec4(mix(yuv.arb, yuv.grb, step(fract(v_texcoord0.x * m_frameData.y), 0.5)), 1.0) * m_colourMatrix;\n"
"#else\n"
" gl_FragColor = yuv * m_colourMatrix;\n"
"#ifdef MYTHTV_PRIMARIES\n"
" gl_FragColor.rgb = pow(max(vec3(0), gl_FragColor.rgb), vec3(m_colourGamma));\n"
" gl_FragColor = max(vec4(0), m_primaryMatrix * gl_FragColor);\n"
" gl_FragColor.rgb = pow(gl_FragColor.rgb, vec3(m_displayGamma));\n"
"#ifdef MYTHTV_COLOURMAPPING\n"
" gl_FragColor = ColourMap(gl_FragColor);\n"
"#endif\n"
"#endif\n"
"}\n";
Expand Down
12 changes: 12 additions & 0 deletions mythtv/libs/libmythtv/videocolourspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,16 @@ void VideoColourSpace::SetAlpha(int Value)
Update();
}

QStringList VideoColourSpace::GetColourMappingDefines(void)
{
QStringList result;
if (m_primaryMatrix.isIdentity())
return result;

result << "COLOURMAPPING";
return result;
}

QMatrix4x4 VideoColourSpace::GetPrimaryMatrix(void)
{
return m_primaryMatrix;
Expand Down Expand Up @@ -549,6 +559,8 @@ inline float CalcRy(const float By, const float Gy)
*
* This is a joyous mindbender. There are various explanations on the interweb
* but this is based on the Kodi implementation - with due credit to Team Kodi.
*
* \note We use QMatrix4x4 because QMatrix3x3 has no inverted method.
*/
QMatrix4x4 VideoColourSpace::RGBtoXYZ(ColourPrimaries Primaries)
{
Expand Down
15 changes: 8 additions & 7 deletions mythtv/libs/libmythtv/videocolourspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ class VideoColourSpace : public QObject, public QMatrix4x4, public ReferenceCoun
public:
explicit VideoColourSpace(VideoColourSpace *Parent = nullptr);

bool UpdateColourSpace(const VideoFrame *Frame);
bool UpdateColourSpace(const VideoFrame *Frame);
PictureAttributeSupported SupportedAttributes(void) const;
void SetSupportedAttributes(PictureAttributeSupported Supported);
int GetPictureAttribute(PictureAttribute Attribute);
void SetAlpha(int Value);
QMatrix4x4 GetPrimaryMatrix(void);
float GetColourGamma(void);
float GetDisplayGamma(void);
void SetSupportedAttributes(PictureAttributeSupported Supported);
int GetPictureAttribute(PictureAttribute Attribute);
void SetAlpha(int Value);
QMatrix4x4 GetPrimaryMatrix(void);
QStringList GetColourMappingDefines(void);
float GetColourGamma(void);
float GetDisplayGamma(void);
PrimariesMode GetPrimariesMode(void);

struct ColourPrimaries
Expand Down

0 comments on commit 8a9f4a8

Please sign in to comment.