videonode: imx6: Ensure the video node excludes padding pixels
Padding rows & columns are included in the video frame that is sent to the VIV direct texture. Scale the UV coordinates to ensure the padding pixels aren't shown. Change-Id: I8b870a95ff786f9f80f42f0cc33f468b0c9c3863 Reviewed-by: Laszlo Agocs <laszlo.agocs@theqtcompany.com> Reviewed-by: Yoann Lopes <yoann.lopes@theqtcompany.com>
This commit is contained in:
committed by
Yoann Lopes
parent
a3f82a8995
commit
288d549474
@@ -60,6 +60,8 @@ QSGVivanteVideoMaterial::QSGVivanteVideoMaterial() :
|
||||
#endif
|
||||
|
||||
setFlag(Blending, false);
|
||||
|
||||
mShader = new QSGVivanteVideoMaterialShader;
|
||||
}
|
||||
|
||||
QSGVivanteVideoMaterial::~QSGVivanteVideoMaterial()
|
||||
@@ -73,7 +75,7 @@ QSGMaterialType *QSGVivanteVideoMaterial::type() const {
|
||||
}
|
||||
|
||||
QSGMaterialShader *QSGVivanteVideoMaterial::createShader() const {
|
||||
return new QSGVivanteVideoMaterialShader;
|
||||
return mShader;
|
||||
}
|
||||
|
||||
int QSGVivanteVideoMaterial::compare(const QSGMaterial *other) const {
|
||||
@@ -175,18 +177,49 @@ GLuint QSGVivanteVideoMaterial::vivanteMapping(QVideoFrame vF)
|
||||
glGenTextures(1, &tmpTexId);
|
||||
mBitsToTextureMap.insert(vF.bits(), tmpTexId);
|
||||
|
||||
// Determine the full width & height. Full means: actual width/height plus extra padding pixels.
|
||||
// The full width can be deduced from the bytesPerLine value. The full height is calculated
|
||||
// by calculating the distance between the start of the first and second planes, and dividing
|
||||
// it by the stride (= the bytesPerLine). If there is only one plane, we don't worry about
|
||||
// extra padding rows, since there are no adjacent extra planes.
|
||||
// XXX: This assumes the distance between bits(1) and bits(0) is exactly the size of the first
|
||||
// plane (the Y plane in the case of YUV data). A better way would be to have a dedicated
|
||||
// planeSize() or planeOffset() getter.
|
||||
// Also, this assumes that planes are tightly packed, that is, there is no space between them.
|
||||
// It is okay to assume this here though, because the Vivante direct textures also assume that.
|
||||
// In other words, if the planes aren't tightly packed, then the direct textures won't be able
|
||||
// to render the frame correctly anyway.
|
||||
int fullWidth = vF.bytesPerLine() / QSGVivanteVideoNode::getBytesForPixelFormat(vF.pixelFormat());
|
||||
int fullHeight = (vF.planeCount() > 1) ? ((vF.bits(1) - vF.bits(0)) / vF.bytesPerLine()) : vF.height();
|
||||
|
||||
// The uscale is the ratio of actual width to the full width (same for vscale and height).
|
||||
// Since the vivante direct textures do not offer a way to explicitly specify the amount of padding
|
||||
// columns and rows, we use a trick. We show the full frame - including the padding pixels - in the
|
||||
// texture, but render only a subset of that texture. This subset goes from (0,0) to (uScale, vScale).
|
||||
// In the shader, the texture coordinates (which go from (0.0, 0.0) to (1.0, 1.0)) are multiplied by
|
||||
// the u/v scale values. Since 1.0 * x = x, this effectively limits the texture coordinates from
|
||||
// (0.0, 0.0) - (1.0, 1.0) to (0.0, 0.0) - (uScale, vScale).
|
||||
float uScale = float(vF.width()) / float(fullWidth);
|
||||
float vScale = float(vF.height()) / float(fullHeight);
|
||||
mShader->setUVScale(uScale, vScale);
|
||||
|
||||
const uchar *constBits = vF.bits();
|
||||
void *bits = (void*)constBits;
|
||||
|
||||
#ifdef QT_VIVANTE_VIDEO_DEBUG
|
||||
qDebug() << Q_FUNC_INFO << "new texture, texId: " << tmpTexId << "; constBits: " << constBits;
|
||||
qDebug() << Q_FUNC_INFO
|
||||
<< "new texture, texId: " << tmpTexId
|
||||
<< "; constBits: " << constBits
|
||||
<< "; actual/full width: " << vF.width() << "/" << fullWidth
|
||||
<< "; actual/full height: " << vF.height() << "/" << fullHeight
|
||||
<< "; UV scale: U " << uScale << " V " << vScale;
|
||||
#endif
|
||||
|
||||
GLuint physical = ~0U;
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, tmpTexId);
|
||||
glTexDirectVIVMap_LOCAL(GL_TEXTURE_2D,
|
||||
vF.width(), vF.height(),
|
||||
fullWidth, fullHeight,
|
||||
QSGVivanteVideoNode::getVideoFormat2GLFormatMap().value(vF.pixelFormat()),
|
||||
&bits, &physical);
|
||||
|
||||
|
||||
@@ -43,6 +43,8 @@
|
||||
|
||||
#include <private/qsgvideonode_p.h>
|
||||
|
||||
class QSGVivanteVideoMaterialShader;
|
||||
|
||||
class QSGVivanteVideoMaterial : public QSGMaterial
|
||||
{
|
||||
public:
|
||||
@@ -78,6 +80,8 @@ private:
|
||||
|
||||
GLuint mTexDirectTexture;
|
||||
GLvoid *mTexDirectPlanes[3];
|
||||
|
||||
QSGVivanteVideoMaterialShader *mShader;
|
||||
};
|
||||
|
||||
#endif // QSGVIDEOMATERIAL_VIVMAP_H
|
||||
|
||||
@@ -35,6 +35,13 @@
|
||||
#include "qsgvivantevideonode.h"
|
||||
#include "qsgvivantevideomaterial.h"
|
||||
|
||||
QSGVivanteVideoMaterialShader::QSGVivanteVideoMaterialShader() :
|
||||
mUScale(1),
|
||||
mVScale(1),
|
||||
mNewUVScale(true)
|
||||
{
|
||||
}
|
||||
|
||||
void QSGVivanteVideoMaterialShader::updateState(const RenderState &state,
|
||||
QSGMaterial *newMaterial,
|
||||
QSGMaterial *oldMaterial)
|
||||
@@ -48,6 +55,10 @@ void QSGVivanteVideoMaterialShader::updateState(const RenderState &state,
|
||||
mat->setOpacity(state.opacity());
|
||||
program()->setUniformValue(mIdOpacity, state.opacity());
|
||||
}
|
||||
if (mNewUVScale) {
|
||||
program()->setUniformValue(mIdUVScale, mUScale, mVScale);
|
||||
mNewUVScale = false;
|
||||
}
|
||||
if (state.isMatrixDirty())
|
||||
program()->setUniformValue(mIdMatrix, state.combinedMatrix());
|
||||
}
|
||||
@@ -61,6 +72,13 @@ const char * const *QSGVivanteVideoMaterialShader::attributeNames() const {
|
||||
return names;
|
||||
}
|
||||
|
||||
void QSGVivanteVideoMaterialShader::setUVScale(float uScale, float vScale)
|
||||
{
|
||||
mUScale = uScale;
|
||||
mVScale = vScale;
|
||||
mNewUVScale = true;
|
||||
}
|
||||
|
||||
const char *QSGVivanteVideoMaterialShader::vertexShader() const {
|
||||
static const char *shader =
|
||||
"uniform highp mat4 qt_Matrix; \n"
|
||||
@@ -78,12 +96,13 @@ const char *QSGVivanteVideoMaterialShader::fragmentShader() const {
|
||||
static const char *shader =
|
||||
"uniform sampler2D texture;"
|
||||
"uniform lowp float opacity;"
|
||||
"uniform highp vec2 uvScale;"
|
||||
""
|
||||
"varying highp vec2 qt_TexCoord;"
|
||||
""
|
||||
"void main()"
|
||||
"{"
|
||||
" gl_FragColor = vec4(texture2D( texture, qt_TexCoord ).rgb, 1.0) * opacity;\n"
|
||||
" gl_FragColor = vec4(texture2D( texture, qt_TexCoord * uvScale ).rgb, 1.0) * opacity;\n"
|
||||
"}";
|
||||
return shader;
|
||||
}
|
||||
@@ -93,4 +112,5 @@ void QSGVivanteVideoMaterialShader::initialize() {
|
||||
mIdMatrix = program()->uniformLocation("qt_Matrix");
|
||||
mIdTexture = program()->uniformLocation("texture");
|
||||
mIdOpacity = program()->uniformLocation("opacity");
|
||||
mIdUVScale = program()->uniformLocation("uvScale");
|
||||
}
|
||||
|
||||
@@ -39,9 +39,13 @@
|
||||
class QSGVivanteVideoMaterialShader : public QSGMaterialShader
|
||||
{
|
||||
public:
|
||||
QSGVivanteVideoMaterialShader();
|
||||
|
||||
void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial);
|
||||
virtual char const *const *attributeNames() const;
|
||||
|
||||
void setUVScale(float uScale, float vScale);
|
||||
|
||||
protected:
|
||||
virtual const char *vertexShader() const;
|
||||
virtual const char *fragmentShader() const;
|
||||
@@ -51,6 +55,11 @@ private:
|
||||
int mIdMatrix;
|
||||
int mIdTexture;
|
||||
int mIdOpacity;
|
||||
int mIdUVScale;
|
||||
|
||||
float mUScale;
|
||||
float mVScale;
|
||||
bool mNewUVScale;
|
||||
};
|
||||
|
||||
#endif // QSGVIDEOMATERIALSHADER_VIVANTE_H
|
||||
|
||||
@@ -78,4 +78,23 @@ const QMap<QVideoFrame::PixelFormat, GLenum>& QSGVivanteVideoNode::getVideoForma
|
||||
}
|
||||
|
||||
|
||||
int QSGVivanteVideoNode::getBytesForPixelFormat(QVideoFrame::PixelFormat pixelformat)
|
||||
{
|
||||
switch (pixelformat) {
|
||||
case QVideoFrame::Format_YUV420P: return 1;
|
||||
case QVideoFrame::Format_YV12: return 1;
|
||||
case QVideoFrame::Format_NV12: return 1;
|
||||
case QVideoFrame::Format_NV21: return 1;
|
||||
case QVideoFrame::Format_UYVY: return 2;
|
||||
case QVideoFrame::Format_YUYV: return 2;
|
||||
case QVideoFrame::Format_RGB32: return 4;
|
||||
case QVideoFrame::Format_ARGB32: return 4;
|
||||
case QVideoFrame::Format_BGR32: return 4;
|
||||
case QVideoFrame::Format_BGRA32: return 4;
|
||||
case QVideoFrame::Format_RGB565: return 2;
|
||||
default: return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -49,6 +49,7 @@ public:
|
||||
void setCurrentFrame(const QVideoFrame &frame, FrameFlags flags);
|
||||
|
||||
static const QMap<QVideoFrame::PixelFormat, GLenum>& getVideoFormat2GLFormatMap();
|
||||
static int getBytesForPixelFormat(QVideoFrame::PixelFormat pixelformat);
|
||||
|
||||
private:
|
||||
QVideoSurfaceFormat mFormat;
|
||||
|
||||
Reference in New Issue
Block a user