Skip to content

Commit

Permalink
修改片段着色器代码生成;
Browse files Browse the repository at this point in the history
  • Loading branch information
RealChuan committed Nov 16, 2023
1 parent 67e7cfe commit 8822623
Show file tree
Hide file tree
Showing 23 changed files with 356 additions and 204 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@
void mp_get_csp_matrix(struct mp_csp_params *params, struct mp_cmat *m);
```

4. HDR metadata获取

```cpp
AVFrameSideData *mdm = av_frame_get_side_data(src, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
AVFrameSideData *clm = av_frame_get_side_data(src, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL);
AVFrameSideData *dhp = av_frame_get_side_data(src, AV_FRAME_DATA_DYNAMIC_HDR_PLUS);
pl_map_hdr_metadata(&dst->params.color.hdr, &(struct pl_av_hdr_metadata) {
.mdm = (void *)(mdm ? mdm->data : NULL),
.clm = (void *)(clm ? clm->data : NULL),
.dhp = (void *)(dhp ? dhp->data : NULL),
});
```

### OpenGL 渲染图像,怎么实现画质增强的效果?

### Ffmpeg(5.0)在解码字幕与4.4.3不太一样
Expand Down
2 changes: 1 addition & 1 deletion examples/player/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ MainWindow::MainWindow(QWidget *parent)
d_ptr->playlistView->installEventFilter(this);
installEventFilter(this);

resize(1000, 650);
resize(1100, 680);
}

MainWindow::~MainWindow()
Expand Down
2 changes: 2 additions & 0 deletions ffmpeg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ set(PROJECT_SOURCES
videorender/openglshader.hpp
videorender/openglshaderprogram.cc
videorender/openglshaderprogram.hpp
videorender/shaderutils.cc
videorender/shaderutils.hpp
videorender/videopreviewwidget.cc
videorender/videopreviewwidget.hpp
videorender/videorender.cc
Expand Down
16 changes: 3 additions & 13 deletions ffmpeg/videorender/openglrender.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,6 @@ void OpenglRender::initSubTexture()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}

void OpenglRender::setColorTrc()
{
auto *avFrame = d_ptr->framePtr->avFrame();
switch (avFrame->color_trc) {
case AVCOL_TRC_SMPTE2084: break;
default: break;
}
}

auto OpenglRender::fitToScreen(const QSize &size) -> QMatrix4x4
{
auto factor_w = static_cast<qreal>(width()) / size.width();
Expand All @@ -216,13 +207,13 @@ void OpenglRender::cleanup()
}
}

void OpenglRender::resetShader(int format)
void OpenglRender::resetShader(Frame *frame)
{
makeCurrent();
cleanup();
d_ptr->programPtr->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shader/video.vert");
OpenglShader shader;
d_ptr->programPtr->addShaderFromSourceCode(QOpenGLShader::Fragment, shader.generate(format));
d_ptr->programPtr->addShaderFromSourceCode(QOpenGLShader::Fragment, shader.generate(frame));
glBindVertexArray(d_ptr->vao);
d_ptr->programPtr->link();
d_ptr->programPtr->bind();
Expand All @@ -239,7 +230,7 @@ void OpenglRender::onUpdateFrame(const QSharedPointer<Frame> &framePtr)
{
if (d_ptr->framePtr.isNull()
|| d_ptr->framePtr->avFrame()->format != framePtr->avFrame()->format) {
resetShader(framePtr->avFrame()->format);
resetShader(framePtr.data());
d_ptr->frameChanged = true;
} else if (d_ptr->framePtr->avFrame()->width != framePtr->avFrame()->width
|| d_ptr->framePtr->avFrame()->height != framePtr->avFrame()->height) {
Expand Down Expand Up @@ -295,7 +286,6 @@ void OpenglRender::paintVideoFrame()
auto param = Ffmpeg::ColorSpace::getYuvToRgbParam(d_ptr->framePtr.data());
d_ptr->programPtr->setUniformValue("offset", param.offset);
d_ptr->programPtr->setUniformValue("colorConversion", param.matrix);
setColorTrc();
draw();
d_ptr->programPtr->release();
d_ptr->frameChanged = false;
Expand Down
3 changes: 1 addition & 2 deletions ffmpeg/videorender/openglrender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ class FFMPEG_EXPORT OpenglRender : public VideoRender,
void initSubTexture();
auto fitToScreen(const QSize &size) -> QMatrix4x4;
void cleanup();
void resetShader(int format);
void resetShader(Frame *frame);

void onUpdateFrame(const QSharedPointer<Frame> &framePtr);
void onUpdateSubTitleFrame(const QSharedPointer<Subtitle> &framePtr);

void paintVideoFrame();
void paintSubTitleFrame();
void setColorTrc();

void updateYUV420P();
void updateYUYV422();
Expand Down
49 changes: 22 additions & 27 deletions ffmpeg/videorender/openglshader.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include "openglshader.hpp"
#include "shaderutils.hpp"

#include <ffmpeg/frame.hpp>
#include <utils/utils.h>

#include <QFile>

extern "C" {
#include <libavutil/pixfmt.h>
#include <libavutil/frame.h>
}

namespace Ffmpeg {
Expand All @@ -27,35 +29,28 @@ OpenglShader::OpenglShader(QObject *parent)

OpenglShader::~OpenglShader() = default;

QByteArray OpenglShader::generate(int format)
auto OpenglShader::generate(Frame *frame) -> QByteArray
{
auto *avFrame = frame->avFrame();
auto format = avFrame->format;
qInfo() << "Generate Shader:" << format;
auto frag = Utils::readAllFile(":/shader/video_header.frag");
frag.append("\n");
frag.append(Utils::readAllFile(":/shader/video_color.frag"));
frag.append("\n");
switch (format) {
case AV_PIX_FMT_YUV420P:
case AV_PIX_FMT_YUV422P:
case AV_PIX_FMT_YUV444P:
case AV_PIX_FMT_YUV410P:
case AV_PIX_FMT_YUV411P: frag.append(Utils::readAllFile(":/shader/video_yuv420p.frag")); break;
case AV_PIX_FMT_YUYV422: frag.append(Utils::readAllFile(":/shader/video_yuyv422.frag")); break;
case AV_PIX_FMT_RGB24:
case AV_PIX_FMT_BGR8:
case AV_PIX_FMT_RGB8: frag.append(Utils::readAllFile(":/shader/video_rgb24.frag")); break;
case AV_PIX_FMT_BGR24: frag.append(Utils::readAllFile(":/shader/video_bgr24.frag")); break;
case AV_PIX_FMT_UYVY422: frag.append(Utils::readAllFile(":/shader/video_uyvy422.frag")); break;
case AV_PIX_FMT_NV12:
case AV_PIX_FMT_P010LE: frag.append(Utils::readAllFile(":/shader/video_nv12.frag")); break;
case AV_PIX_FMT_NV21: frag.append(Utils::readAllFile(":/shader/video_nv21.frag")); break;
case AV_PIX_FMT_ARGB: frag.append(Utils::readAllFile(":/shader/video_argb.frag")); break;
case AV_PIX_FMT_RGBA: frag.append(Utils::readAllFile(":/shader/video_rgba.frag")); break;
case AV_PIX_FMT_ABGR: frag.append(Utils::readAllFile(":/shader/video_abgr.frag")); break;
case AV_PIX_FMT_BGRA: frag.append(Utils::readAllFile(":/shader/video_bgra.frag")); break;
default: qWarning() << "UnSupported format:" << format; break;
auto frag = ShaderUtils::header();
if (!ShaderUtils::beginFragment(frag, format)) {
return {};
}
frag.append("\n");
ShaderUtils::passLinearize(frag, avFrame->color_trc);

auto temp = QString("color.rgb *= vec3(%1);\n").arg(ShaderUtils::trcNomPeak(avFrame->color_trc));
frag.append(temp.toUtf8());

// HDR
// ShaderUtils::toneMap(frag, avFrame);

auto color_trc = ShaderUtils::trcIsHdr(avFrame->color_trc) ? AVCOL_TRC_GAMMA22
: avFrame->color_trc;
ShaderUtils::passDeLinearize(frag, color_trc);
ShaderUtils::finishFragment(frag);
ShaderUtils::printShader(frag);
return frag;
}

Expand Down
3 changes: 2 additions & 1 deletion ffmpeg/videorender/openglshader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

namespace Ffmpeg {

class Frame;
class OpenglShader : public QObject
{
public:
explicit OpenglShader(QObject *parent = nullptr);
~OpenglShader() override;

auto generate(int format) -> QByteArray;
auto generate(Frame *frame) -> QByteArray;

private:
class OpenglShaderPrivate;
Expand Down
10 changes: 0 additions & 10 deletions ffmpeg/videorender/shader/video_abgr.frag

This file was deleted.

10 changes: 0 additions & 10 deletions ffmpeg/videorender/shader/video_argb.frag

This file was deleted.

11 changes: 0 additions & 11 deletions ffmpeg/videorender/shader/video_bgr24.frag

This file was deleted.

10 changes: 0 additions & 10 deletions ffmpeg/videorender/shader/video_bgra.frag

This file was deleted.

20 changes: 6 additions & 14 deletions ffmpeg/videorender/shader/video_nv12.frag
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
void main()
{
vec3 yuv;
vec4 color = vec4(0.0, 0.0, 0.0, 1.0);

yuv.x = texture(tex_y, TexCord).r;
yuv.yz = texture(tex_u, TexCord).rg;
vec3 yuv;
vec4 color = vec4(0.0, 0.0, 0.0, 1.0);

yuv += offset;
color.rgb = yuv * colorConversion;
yuv.x = texture(tex_y, TexCord).r;
yuv.yz = texture(tex_u, TexCord).rg;

color.rgb = adjustContrast(color.rgb, contrast);
color.rgb = adjustSaturation(color.rgb, saturation);
color.rgb = adjustBrightness(color.rgb, brightness);

FragColor = color;
}
yuv += offset;
color.rgb = yuv * colorConversion;
20 changes: 6 additions & 14 deletions ffmpeg/videorender/shader/video_nv21.frag
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
void main()
{
vec3 yuv;
vec4 color = vec4(0.0, 0.0, 0.0, 1.0);

yuv.x = texture(tex_y, TexCord).r;
yuv.yz = texture(tex_u, TexCord).gr;
vec3 yuv;
vec4 color = vec4(0.0, 0.0, 0.0, 1.0);

yuv += offset;
color.rgb = yuv * colorConversion;
yuv.x = texture(tex_y, TexCord).r;
yuv.yz = texture(tex_u, TexCord).gr;

color.rgb = adjustContrast(color.rgb, contrast);
color.rgb = adjustSaturation(color.rgb, saturation);
color.rgb = adjustBrightness(color.rgb, brightness);

FragColor = color;
}
yuv += offset;
color.rgb = yuv * colorConversion;
20 changes: 6 additions & 14 deletions ffmpeg/videorender/shader/video_p010le.frag
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
void main()
{
vec3 yuv;
vec4 color = vec4(0.0, 0.0, 0.0, 1.0);

yuv.x = texture(tex_y, TexCord).r;
yuv.yz = texture(tex_u, TexCord).rg;
vec3 yuv;
vec4 color = vec4(0.0, 0.0, 0.0, 1.0);

yuv += offset;
color.rgb = yuv * colorConversion;
yuv.x = texture(tex_y, TexCord).r;
yuv.yz = texture(tex_u, TexCord).rg;

color.rgb = adjustContrast(color.rgb, contrast);
color.rgb = adjustSaturation(color.rgb, saturation);
color.rgb = adjustBrightness(color.rgb, brightness);

FragColor = color;
}
yuv += offset;
color.rgb = yuv * colorConversion;
11 changes: 0 additions & 11 deletions ffmpeg/videorender/shader/video_rgb24.frag

This file was deleted.

10 changes: 0 additions & 10 deletions ffmpeg/videorender/shader/video_rgba.frag

This file was deleted.

36 changes: 14 additions & 22 deletions ffmpeg/videorender/shader/video_uyvy422.frag
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
void main()
{
vec3 yuv;
vec4 color = vec4(0.0, 0.0, 0.0, 1.0);

int width = textureSize(tex_y, 0).x * 2;
float tex_x = TexCord.x;
int pixel = int(floor(width * tex_x)) % 2;
vec4 tc = texture(tex_y, TexCord).rgba;
float cb = tc.r;
float y1 = tc.g;
float cr = tc.b;
float y2 = tc.a;
float y = (pixel == 1) ? y2 : y1;
yuv = vec3(y, cb, cr);
vec3 yuv;
vec4 color = vec4(0.0, 0.0, 0.0, 1.0);

yuv += offset;
color.rgb = yuv * colorConversion;
int width = textureSize(tex_y, 0).x * 2;
float tex_x = TexCord.x;
int pixel = int(floor(width * tex_x)) % 2;
vec4 tc = texture(tex_y, TexCord).rgba;
float cb = tc.r;
float y1 = tc.g;
float cr = tc.b;
float y2 = tc.a;
float y = (pixel == 1) ? y2 : y1;
yuv = vec3(y, cb, cr);

color.rgb = adjustContrast(color.rgb, contrast);
color.rgb = adjustSaturation(color.rgb, saturation);
color.rgb = adjustBrightness(color.rgb, brightness);

FragColor = color;
}
yuv += offset;
color.rgb = yuv * colorConversion;
22 changes: 7 additions & 15 deletions ffmpeg/videorender/shader/video_yuv420p.frag
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
void main()
{
vec3 yuv;
vec4 color = vec4(0.0, 0.0, 0.0, 1.0);

yuv.x = texture(tex_y, TexCord).r;
yuv.y = texture(tex_u, TexCord).r;
yuv.z = texture(tex_v, TexCord).r;
vec3 yuv;
vec4 color = vec4(0.0, 0.0, 0.0, 1.0);

yuv += offset;
color.rgb = yuv * colorConversion;
yuv.x = texture(tex_y, TexCord).r;
yuv.y = texture(tex_u, TexCord).r;
yuv.z = texture(tex_v, TexCord).r;

color.rgb = adjustContrast(color.rgb, contrast);
color.rgb = adjustSaturation(color.rgb, saturation);
color.rgb = adjustBrightness(color.rgb, brightness);

FragColor = color;
}
yuv += offset;
color.rgb = yuv * colorConversion;
Loading

0 comments on commit 8822623

Please sign in to comment.