删除 src/videorenderwidget.cpp
This commit is contained in:
parent
141d1b98d9
commit
3fb913e859
@ -1,307 +0,0 @@
|
||||
#include "videorenderwidget.h"
|
||||
|
||||
#include <QOpenGLShaderProgram>
|
||||
#include <QPainter>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
namespace {
|
||||
const char *vertexShaderSource = R"(
|
||||
attribute vec2 position;
|
||||
attribute vec2 texCoord;
|
||||
varying vec2 textureCoordinate;
|
||||
uniform vec2 contentScale;
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(position * contentScale, 0.0, 1.0);
|
||||
textureCoordinate = texCoord;
|
||||
}
|
||||
)";
|
||||
|
||||
const char *fragmentShaderSource = R"(
|
||||
#ifdef GL_ES
|
||||
precision mediump float;
|
||||
#endif
|
||||
varying vec2 textureCoordinate;
|
||||
uniform sampler2D textureY;
|
||||
uniform sampler2D textureU;
|
||||
uniform sampler2D textureV;
|
||||
uniform float pixelFormat;
|
||||
uniform float scaleY;
|
||||
uniform float scaleU;
|
||||
uniform float scaleV;
|
||||
void main()
|
||||
{
|
||||
if (pixelFormat > 1.5) {
|
||||
gl_FragColor = texture2D(textureY, textureCoordinate);
|
||||
return;
|
||||
}
|
||||
|
||||
float y = texture2D(textureY,
|
||||
vec2(textureCoordinate.x * scaleY, textureCoordinate.y)).r;
|
||||
float u;
|
||||
float v;
|
||||
if (pixelFormat > 0.5) {
|
||||
vec4 uv = texture2D(textureU,
|
||||
vec2(textureCoordinate.x * scaleU, textureCoordinate.y));
|
||||
u = uv.r - 0.5;
|
||||
v = uv.a - 0.5;
|
||||
} else {
|
||||
u = texture2D(textureU,
|
||||
vec2(textureCoordinate.x * scaleU, textureCoordinate.y)).r - 0.5;
|
||||
v = texture2D(textureV,
|
||||
vec2(textureCoordinate.x * scaleV, textureCoordinate.y)).r - 0.5;
|
||||
}
|
||||
y = 1.16438356 * (y - 0.0625);
|
||||
gl_FragColor = vec4(y + 1.59602678 * v,
|
||||
y - 0.39176229 * u - 0.81296764 * v,
|
||||
y + 2.01723214 * u, 1.0);
|
||||
}
|
||||
)";
|
||||
}
|
||||
|
||||
VideoRenderWidget::VideoRenderWidget(QWidget *parent)
|
||||
: QOpenGLWidget(parent)
|
||||
{
|
||||
setAutoFillBackground(false);
|
||||
setUpdateBehavior(QOpenGLWidget::NoPartialUpdate);
|
||||
}
|
||||
|
||||
VideoRenderWidget::~VideoRenderWidget()
|
||||
{
|
||||
if (context()) {
|
||||
makeCurrent();
|
||||
releaseTextures();
|
||||
doneCurrent();
|
||||
}
|
||||
}
|
||||
|
||||
void VideoRenderWidget::setFrameAspectLayoutEnabled(bool enabled)
|
||||
{
|
||||
m_frameAspectLayout = enabled;
|
||||
QSizePolicy policy(QSizePolicy::Expanding, enabled ? QSizePolicy::Fixed : QSizePolicy::Expanding);
|
||||
policy.setHeightForWidth(enabled);
|
||||
setSizePolicy(policy);
|
||||
if (enabled) updateAspectHeight();
|
||||
else { setMinimumHeight(0); setMaximumHeight(QWIDGETSIZE_MAX); }
|
||||
updateGeometry();
|
||||
}
|
||||
|
||||
int VideoRenderWidget::heightForWidth(int width) const
|
||||
{
|
||||
return m_frameAspectLayout ? qMax(1, qRound(double(width) * m_layoutFrameSize.height()
|
||||
/ m_layoutFrameSize.width())) : -1;
|
||||
}
|
||||
|
||||
QSize VideoRenderWidget::sizeHint() const
|
||||
{
|
||||
return m_frameAspectLayout ? QSize(640,heightForWidth(640)) : QOpenGLWidget::sizeHint();
|
||||
}
|
||||
|
||||
void VideoRenderWidget::updateAspectHeight()
|
||||
{
|
||||
if (!m_frameAspectLayout) return;
|
||||
const int wanted = heightForWidth(width());
|
||||
if (minimumHeight() != wanted || maximumHeight() != wanted) setFixedHeight(wanted);
|
||||
}
|
||||
|
||||
void VideoRenderWidget::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
QOpenGLWidget::resizeEvent(event);
|
||||
updateAspectHeight();
|
||||
}
|
||||
|
||||
void VideoRenderWidget::setFrame(const VideoFrame &frame)
|
||||
{
|
||||
if (!frame.isValid())
|
||||
return;
|
||||
m_frame = frame;
|
||||
if (m_layoutFrameSize != QSize(frame.width, frame.height)) {
|
||||
m_layoutFrameSize = QSize(frame.width, frame.height);
|
||||
updateAspectHeight();
|
||||
if (m_frameAspectLayout) updateGeometry();
|
||||
}
|
||||
m_message.clear();
|
||||
m_frameDirty = true;
|
||||
update();
|
||||
}
|
||||
|
||||
void VideoRenderWidget::setImage(const QImage &image)
|
||||
{
|
||||
if (image.isNull())
|
||||
return;
|
||||
const QImage converted = image.convertToFormat(QImage::Format_RGBA8888);
|
||||
VideoFrame frame;
|
||||
frame.width = converted.width();
|
||||
frame.height = converted.height();
|
||||
frame.format = VideoPixelFormat::RGBA;
|
||||
frame.stride0 = frame.width * 4;
|
||||
frame.plane0.resize(frame.width * frame.height * 4);
|
||||
for (int row = 0; row < frame.height; ++row) {
|
||||
std::memcpy(frame.plane0.data() + row * frame.width * 4,
|
||||
converted.constScanLine(row), static_cast<size_t>(frame.width * 4));
|
||||
}
|
||||
setFrame(frame);
|
||||
}
|
||||
|
||||
void VideoRenderWidget::clearFrame(const QString &message)
|
||||
{
|
||||
m_frame = VideoFrame();
|
||||
m_message = message;
|
||||
m_frameDirty = false;
|
||||
update();
|
||||
}
|
||||
|
||||
void VideoRenderWidget::initializeGL()
|
||||
{
|
||||
initializeOpenGLFunctions();
|
||||
glClearColor(0.055f, 0.067f, 0.078f, 1.0f);
|
||||
glGenTextures(3, m_textures);
|
||||
|
||||
m_program.reset(new QOpenGLShaderProgram);
|
||||
if (!m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource) ||
|
||||
!m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource) ||
|
||||
!m_program->link()) {
|
||||
m_message = QStringLiteral("OpenGL 视频着色器初始化失败:%1").arg(m_program->log());
|
||||
m_program.reset();
|
||||
return;
|
||||
}
|
||||
m_glReady = true;
|
||||
}
|
||||
|
||||
void VideoRenderWidget::uploadPlane(unsigned int texture, int unit, int width,
|
||||
int height, unsigned int format,
|
||||
const QByteArray &data, int textureIndex)
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0 + unit);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
if (m_textureWidths[textureIndex] != width ||
|
||||
m_textureHeights[textureIndex] != height ||
|
||||
m_textureFormats[textureIndex] != format) {
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, static_cast<int>(format), width, height, 0,
|
||||
format, GL_UNSIGNED_BYTE, data.constData());
|
||||
m_textureWidths[textureIndex] = width;
|
||||
m_textureHeights[textureIndex] = height;
|
||||
m_textureFormats[textureIndex] = format;
|
||||
} else {
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format,
|
||||
GL_UNSIGNED_BYTE, data.constData());
|
||||
}
|
||||
}
|
||||
|
||||
void VideoRenderWidget::paintGL()
|
||||
{
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
if (!m_glReady || !m_program || !m_frame.isValid()) {
|
||||
drawMessage(m_message);
|
||||
return;
|
||||
}
|
||||
|
||||
// Qt 5.12's ANGLE backend can upload RGBA QImage data with an incorrect
|
||||
// channel layout. Draw still RGBA frames with the CPU painter instead.
|
||||
if (m_frame.format == VideoPixelFormat::RGBA) {
|
||||
const QImage image(reinterpret_cast<const uchar *>(m_frame.plane0.constData()),
|
||||
m_frame.width, m_frame.height, m_frame.stride0,
|
||||
QImage::Format_RGBA8888);
|
||||
QPainter painter(this);
|
||||
painter.fillRect(rect(), QColor(QStringLiteral("#0e1115")));
|
||||
QSize target = image.size();
|
||||
target.scale(size(), Qt::KeepAspectRatio);
|
||||
const QRect destination((width() - target.width()) / 2,
|
||||
(height() - target.height()) / 2,
|
||||
target.width(), target.height());
|
||||
painter.drawImage(destination, image);
|
||||
m_frameDirty = false;
|
||||
return;
|
||||
}
|
||||
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
if (m_frameDirty) {
|
||||
if (m_frame.format == VideoPixelFormat::I420) {
|
||||
uploadPlane(m_textures[0], 0, m_frame.stride0, m_frame.height,
|
||||
GL_LUMINANCE, m_frame.plane0, 0);
|
||||
uploadPlane(m_textures[1], 1, m_frame.stride1,
|
||||
(m_frame.height + 1) / 2, GL_LUMINANCE, m_frame.plane1, 1);
|
||||
uploadPlane(m_textures[2], 2, m_frame.stride2,
|
||||
(m_frame.height + 1) / 2, GL_LUMINANCE, m_frame.plane2, 2);
|
||||
} else if (m_frame.format == VideoPixelFormat::NV12) {
|
||||
uploadPlane(m_textures[0], 0, m_frame.stride0, m_frame.height,
|
||||
GL_LUMINANCE, m_frame.plane0, 0);
|
||||
uploadPlane(m_textures[1], 1, m_frame.stride1 / 2,
|
||||
(m_frame.height + 1) / 2, GL_LUMINANCE_ALPHA,
|
||||
m_frame.plane1, 1);
|
||||
} else {
|
||||
uploadPlane(m_textures[0], 0, m_frame.width, m_frame.height,
|
||||
GL_RGBA, m_frame.plane0, 0);
|
||||
}
|
||||
m_frameDirty = false;
|
||||
}
|
||||
|
||||
const GLfloat vertices[] = {
|
||||
-1.0f, -1.0f, 0.0f, 1.0f,
|
||||
1.0f, -1.0f, 1.0f, 1.0f,
|
||||
-1.0f, 1.0f, 0.0f, 0.0f,
|
||||
1.0f, 1.0f, 1.0f, 0.0f
|
||||
};
|
||||
float scaleX = 1.0f;
|
||||
float scaleY = 1.0f;
|
||||
const float frameAspect = static_cast<float>(m_frame.width) / m_frame.height;
|
||||
const float widgetAspect = width() > 0 && height() > 0
|
||||
? static_cast<float>(width()) / height() : frameAspect;
|
||||
if (frameAspect > widgetAspect)
|
||||
scaleY = widgetAspect / frameAspect;
|
||||
else
|
||||
scaleX = frameAspect / widgetAspect;
|
||||
|
||||
m_program->bind();
|
||||
m_program->setUniformValue("contentScale", scaleX, scaleY);
|
||||
m_program->setUniformValue("textureY", 0);
|
||||
m_program->setUniformValue("textureU", 1);
|
||||
m_program->setUniformValue("textureV", 2);
|
||||
m_program->setUniformValue("pixelFormat",
|
||||
m_frame.format == VideoPixelFormat::NV12 ? 1.0f :
|
||||
m_frame.format == VideoPixelFormat::RGBA ? 2.0f : 0.0f);
|
||||
m_program->setUniformValue("scaleY", m_frame.format == VideoPixelFormat::RGBA
|
||||
? 1.0f : static_cast<float>(m_frame.width) / m_frame.stride0);
|
||||
m_program->setUniformValue("scaleU", m_frame.format == VideoPixelFormat::NV12
|
||||
? static_cast<float>((m_frame.width + 1) / 2) / (m_frame.stride1 / 2)
|
||||
: static_cast<float>((m_frame.width + 1) / 2) / m_frame.stride1);
|
||||
m_program->setUniformValue("scaleV", m_frame.format == VideoPixelFormat::I420
|
||||
? static_cast<float>((m_frame.width + 1) / 2) / m_frame.stride2 : 1.0f);
|
||||
const int position = m_program->attributeLocation("position");
|
||||
const int texCoord = m_program->attributeLocation("texCoord");
|
||||
m_program->enableAttributeArray(position);
|
||||
m_program->enableAttributeArray(texCoord);
|
||||
m_program->setAttributeArray(position, GL_FLOAT, vertices, 2, 4 * sizeof(GLfloat));
|
||||
m_program->setAttributeArray(texCoord, GL_FLOAT, vertices + 2, 2,
|
||||
4 * sizeof(GLfloat));
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
m_program->disableAttributeArray(position);
|
||||
m_program->disableAttributeArray(texCoord);
|
||||
m_program->release();
|
||||
}
|
||||
|
||||
void VideoRenderWidget::drawMessage(const QString &message)
|
||||
{
|
||||
if (message.isEmpty())
|
||||
return;
|
||||
QPainter painter(this);
|
||||
painter.setPen(QColor(QStringLiteral("#8d98a5")));
|
||||
painter.drawText(rect(), Qt::AlignCenter | Qt::TextWordWrap, message);
|
||||
}
|
||||
|
||||
void VideoRenderWidget::releaseTextures()
|
||||
{
|
||||
if (m_textures[0]) {
|
||||
glDeleteTextures(3, m_textures);
|
||||
m_textures[0] = m_textures[1] = m_textures[2] = 0;
|
||||
std::fill(m_textureWidths, m_textureWidths + 3, 0);
|
||||
std::fill(m_textureHeights, m_textureHeights + 3, 0);
|
||||
std::fill(m_textureFormats, m_textureFormats + 3, 0);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user