Skip to content

Commit

Permalink
VideoColourSpace: Convert to QMatrix4x4
Browse files Browse the repository at this point in the history
- for direct compatibilty with the QOpenGL classes and removal of custom
matrix code
- this MAY BREAK VDPAU colour rendition - not yet tested

refs #13376
  • Loading branch information
mark-kendall committed Jan 24, 2019
1 parent 44a0963 commit 8cdaab8
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 1,288 deletions.
6 changes: 2 additions & 4 deletions mythtv/libs/libmythtv/openglvideo.cpp
Expand Up @@ -954,11 +954,9 @@ void OpenGLVideo::PrepareFrame(bool topfieldfirst, FrameScanType scan,
program = filter->fragmentPrograms[prog_ref];
}

if (type == kGLFilterYUV2RGB || type == kGLFilterYV12RGB)
if ((type == kGLFilterYUV2RGB || type == kGLFilterYV12RGB))
{
gl_context->SetShaderProgramParams(program,
GLMatrix4x4(reinterpret_cast<float*>(colourSpace->GetMatrix())),
COLOUR_UNIFORM);
gl_context->SetShaderProgramParams(program, *colourSpace, COLOUR_UNIFORM);
}

gl_context->DrawBitmap(textures, texture_count, target, trect, vrect,
Expand Down
159 changes: 51 additions & 108 deletions mythtv/libs/libmythtv/videocolourspace.cpp
@@ -1,93 +1,19 @@
#include <cmath>

#include "compat.h"
#include "mythcorecontext.h"
#include "mythlogging.h"
#include "videocolourspace.h"


#define LOC QString("ColourSpace: ")

Matrix::Matrix(float m11, float m12, float m13, float m14,
float m21, float m22, float m23, float m24,
float m31, float m32, float m33, float m34)
{
m[0][0] = m11; m[0][1] = m12; m[0][2] = m13; m[0][3] = m14;
m[1][0] = m21; m[1][1] = m22; m[1][2] = m23; m[1][3] = m24;
m[2][0] = m31; m[2][1] = m32; m[2][2] = m33; m[2][3] = m34;
m[3][0] = m[3][1] = m[3][2] = m[3][3] = 1.0f;
}

Matrix::Matrix()
{
setToIdentity();
}

void Matrix::setToIdentity(void)
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 4; j++)
m[i][j] = (i == j) ? 1.0f : 0.0f;
}

void Matrix::scale(float val1, float val2, float val3)
{
Matrix scale;
scale.m[0][0] = val1;
scale.m[1][1] = val2;
scale.m[2][2] = val3;
this->operator *=(scale);
}

void Matrix::translate(float val1, float val2, float val3)
{
Matrix translate;
translate.m[0][3] = val1;
translate.m[1][3] = val2;
translate.m[2][3] = val3;
this->operator *=(translate);
}

Matrix & Matrix::operator*=(const Matrix &r)
{
for (int i = 0; i < 3; i++)
product(i, r);
return *this;
}

void Matrix::product(int row, const Matrix &r)
{
float t0, t1, t2, t3;
t0 = m[row][0] * r.m[0][0] + m[row][1] * r.m[1][0] + m[row][2] * r.m[2][0];
t1 = m[row][0] * r.m[0][1] + m[row][1] * r.m[1][1] + m[row][2] * r.m[2][1];
t2 = m[row][0] * r.m[0][2] + m[row][1] * r.m[1][2] + m[row][2] * r.m[2][2];
t3 = m[row][0] * r.m[0][3] + m[row][1] * r.m[1][3] + m[row][2] * r.m[2][3] + m[row][3];
m[row][0] = t0; m[row][1] = t1; m[row][2] = t2; m[row][3] = t3;
}

void Matrix::debug(void)
{
LOG(VB_PLAYBACK, LOG_DEBUG, LOC + QString("%1 %2 %3 %4")
.arg(m[0][0], 4, 'f', 4, QLatin1Char('0'))
.arg(m[0][1], 4, 'f', 4, QLatin1Char('0'))
.arg(m[0][2], 4, 'f', 4, QLatin1Char('0'))
.arg(m[0][3], 4, 'f', 4, QLatin1Char('0')));
LOG(VB_PLAYBACK, LOG_DEBUG, LOC + QString("%1 %2 %3 %4")
.arg(m[1][0], 4, 'f', 4, QLatin1Char('0'))
.arg(m[1][1], 4, 'f', 4, QLatin1Char('0'))
.arg(m[1][2], 4, 'f', 4, QLatin1Char('0'))
.arg(m[1][3], 4, 'f', 4, QLatin1Char('0')));
LOG(VB_PLAYBACK, LOG_DEBUG, LOC + QString("%1 %2 %3 %4")
.arg(m[2][0], 4, 'f', 4, QLatin1Char('0'))
.arg(m[2][1], 4, 'f', 4, QLatin1Char('0'))
.arg(m[2][2], 4, 'f', 4, QLatin1Char('0'))
.arg(m[2][3], 4, 'f', 4, QLatin1Char('0')));
}

VideoColourSpace::VideoColourSpace(VideoCStd colour_std)
: m_supported_attributes(kPictureAttributeSupported_None),
m_changed(false), m_studioLevels(false), m_brightness(0.0f),
m_contrast(1.0f), m_saturation(1.0f), m_hue(0.0f),
: QMatrix4x4(),
m_supported_attributes(kPictureAttributeSupported_None),
m_changed(false),
m_studioLevels(false),
m_brightness(0.0f),
m_contrast(1.0f),
m_saturation(1.0f),
m_hue(0.0f),
m_colourSpace(colour_std)
{
m_db_settings[kPictureAttribute_Brightness] =
Expand All @@ -108,6 +34,18 @@ VideoColourSpace::VideoColourSpace(VideoCStd colour_std)
SetStudioLevels(m_db_settings[kPictureAttribute_StudioLevels]);
}

bool VideoColourSpace::HasChanged(void)
{
bool result = m_changed;
m_changed = false;
return result;
}

PictureAttributeSupported VideoColourSpace::SupportedAttributes(void) const
{
return m_supported_attributes;
}

void VideoColourSpace::SetSupportedAttributes(PictureAttributeSupported supported)
{
m_supported_attributes = supported;
Expand Down Expand Up @@ -170,43 +108,41 @@ void VideoColourSpace::Update(void)
float luma_scale = 255.0f / luma_range;
float chroma_scale = 255.0f / chroma_range;

Matrix csc;
QMatrix4x4 csc;
switch (m_colourSpace)
{
case kCSTD_SMPTE_240M:
csc = Matrix(1.000f, ( 0.0000f * uvcos) + ( 1.5756f * uvsin),
( 1.5756f * uvcos) - ( 0.0000f * uvsin), 0.0f,
1.000f, (-0.2253f * uvcos) + ( 0.5000f * uvsin),
( 0.5000f * uvcos) - (-0.2253f * uvsin), 0.0f,
1.000f, ( 1.8270f * uvcos) + ( 0.0000f * uvsin),
( 0.0000f * uvcos) - ( 1.8270f * uvsin), 0.0f);
csc = QMatrix4x4(
1.000f, ( 1.5756f * uvsin), ( 1.5756f * uvcos) , 0.000f,
1.000f, (-0.2253f * uvcos) + ( 0.5000f * uvsin), ( 0.5000f * uvcos) - (-0.2253f * uvsin), 0.000f,
1.000f, ( 1.8270f * uvcos) , - ( 1.8270f * uvsin), 0.000f,
0.000f, 0.000f, 0.000f, 1.000f);
break;

case kCSTD_ITUR_BT_709:
csc = Matrix(1.000f, ( 0.0000f * uvcos) + ( 1.5701f * uvsin),
( 1.5701f * uvcos) - ( 0.0000f * uvsin), 0.0f,
1.000f, (-0.1870f * uvcos) + (-0.4664f * uvsin),
(-0.4664f * uvcos) - (-0.1870f * uvsin), 0.0f,
1.000f, ( 1.8556f * uvcos) + ( 0.0000f * uvsin),
( 0.0000f * uvcos) - ( 1.8556f * uvsin), 0.0f);
csc = QMatrix4x4(
1.000f, ( 1.5701f * uvsin), ( 1.5701f * uvcos) , 0.000f,
1.000f, (-0.1870f * uvcos) + (-0.4664f * uvsin), (-0.4664f * uvcos) - (-0.1870f * uvsin), 0.000f,
1.000f, ( 1.8556f * uvcos) , - ( 1.8556f * uvsin), 0.000f,
0.000f, 0.000f, 0.000f, 1.000f);
break;

case kCSTD_ITUR_BT_601:
default:
csc = Matrix(1.000f, ( 0.0000f * uvcos) + ( 1.4030f * uvsin),
( 1.4030f * uvcos) - ( 0.0000f * uvsin), 0.0f,
1.000f, (-0.3440f * uvcos) + (-0.7140f * uvsin),
(-0.7140f * uvcos) - (-0.3440f * uvsin), 0.0f,
1.000f, ( 1.7730f * uvcos) + ( 0.0000f * uvsin),
( 0.0000f * uvcos) - ( 1.7730f * uvsin), 0.0f);
csc = QMatrix4x4(
1.000f, ( 1.4030f * uvsin), ( 1.4030f * uvcos) , 0.000f,
1.000f, (-0.3440f * uvcos) + (-0.7140f * uvsin), (-0.7140f * uvcos) - (-0.3440f * uvsin), 0.000f,
1.000f, ( 1.7730f * uvcos) , - ( 1.7730f * uvsin), 0.000f,
0.000f, 0.000f, 0.000f, 1.000f);
}

m_matrix.setToIdentity();
m_matrix.translate(brightness, brightness, brightness);
m_matrix.scale(m_contrast, m_contrast, m_contrast);
m_matrix *= csc;
m_matrix.scale(luma_scale, chroma_scale, chroma_scale);
m_matrix.translate(luma_offset, chroma_offset, chroma_offset);
setToIdentity();
translate(brightness, brightness, brightness);
scale(m_contrast);
this->operator *= (csc);
scale(luma_scale, chroma_scale, chroma_scale);
translate(luma_offset, chroma_offset, chroma_offset);
static_cast<QMatrix4x4*>(this)->operator = (this->transposed());
m_changed = true;
Debug();
}
Expand All @@ -221,7 +157,14 @@ void VideoColourSpace::Debug(void)
.arg(m_saturation, 2, 'f', 4, QLatin1Char('0'))
.arg(m_hue , 2, 'f', 4, QLatin1Char('0'))
.arg(m_studioLevels));
m_matrix.debug();

if (VERBOSE_LEVEL_CHECK(VB_PLAYBACK, LOG_DEBUG))
{
QString stream;
QDebug debug(&stream);
debug << this;
LOG(VB_PLAYBACK, LOG_DEBUG, stream);
}
}

void VideoColourSpace::SetColourSpace(VideoCStd csp)
Expand Down
29 changes: 4 additions & 25 deletions mythtv/libs/libmythtv/videocolourspace.h
Expand Up @@ -2,25 +2,9 @@
#define VIDEOCOLOURSPACE_H

#include <QMap>
#include <QMatrix4x4>
#include "videoouttypes.h"

class Matrix
{
public:
Matrix(float m11, float m12, float m13, float m14,
float m21, float m22, float m23, float m24,
float m31, float m32, float m33, float m34);
Matrix();

void setToIdentity(void);
void scale(float val1, float val2, float val3);
void translate(float val1, float val2, float val3);
Matrix & operator*=(const Matrix &r);
void product(int row, const Matrix &r);
void debug(void);
float m[4][4];
};

typedef enum VideoCStd
{
kCSTD_Unknown = 0,
Expand All @@ -29,19 +13,15 @@ typedef enum VideoCStd
kCSTD_SMPTE_240M,
} VideoCStd;

class VideoColourSpace
class VideoColourSpace : public QMatrix4x4
{
public:
explicit VideoColourSpace(VideoCStd colour_std = kCSTD_ITUR_BT_601);
~VideoColourSpace() = default;

PictureAttributeSupported SupportedAttributes(void) const
{ return m_supported_attributes; }
PictureAttributeSupported SupportedAttributes(void) const;
void SetSupportedAttributes(PictureAttributeSupported supported);

void* GetMatrix(void) { return &m_matrix.m; }
bool HasChanged(void) const { return m_changed; }

bool HasChanged(void);
int GetPictureAttribute(PictureAttribute attribute);
int SetPictureAttribute(PictureAttribute attribute, int value);
void SetColourSpace(VideoCStd csp = kCSTD_Unknown);
Expand All @@ -68,7 +48,6 @@ class VideoColourSpace
float m_saturation;
float m_hue;
VideoCStd m_colourSpace;
Matrix m_matrix;
};

#endif

0 comments on commit 8cdaab8

Please sign in to comment.