Skip to content

Commit

Permalink
BT2020,shader该怎么修改?
Browse files Browse the repository at this point in the history
shader新增对比度,饱和度,亮度调节;
  • Loading branch information
RealChuan committed Oct 31, 2023
1 parent 05a5a9e commit 5e6cf11
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,30 @@
2. 在WidgetRender中,尽可能使用QImage::Format_RGB32和QImage::Format_ARGB32_Premultiplied图像格式。如下原因:
1. Avoid most rendering directly to most of these formats using QPainter. Rendering is best optimized to the Format_RGB32 and Format_ARGB32_Premultiplied formats, and secondarily for rendering to the Format_RGB16, Format_RGBX8888, Format_RGBA8888_Premultiplied, Format_RGBX64 and Format_RGBA64_Premultiplied formats.

### OpenGL 怎么渲染BT.2020色彩空间的图像?

现在调整了对比度,饱和度,亮度;效果并不是很好。

```cpp
contrast = 1.6;
saturation = 1.0;
brightness = 0;
```
如果不调整,就跟ffplay输出图像效果一致,整体颜色偏暗。Netflix的视频,视频开头的N,显示的颜色偏暗黄色。

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

### Ffmpeg(5.0)在解码字幕与4.4.3不太一样

### 解码字幕(ffmpeg-n5.0):
#### 解码字幕(ffmpeg-n5.0):

```
0,,en,,0000,0000,0000,,Peek-a-boo!
```

你必须使用 ``ass_process_chunk`` 并设置 pts 和持续时间, 如在 libavfilter/vf_subtitles.c 中一样。

ASS 标准格式应为(ffmpeg-n4.4.3) :
#### ASS 标准格式应为(ffmpeg-n4.4.3) :

```
Dialogue: 0,0:01:06.77,0:01:08.00,en,,0000,0000,0000,,Peek-a-boo!\r\n
Expand Down
2 changes: 1 addition & 1 deletion ffmpeg/colorspace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static constexpr std::array<float, 9> kBT709Matrix
static constexpr QVector3D kBT2020ffset = {-0.0627451017, -0.501960814, -0.501960814};

static constexpr std::array<float, 9> kBT2020Matrix
= {1.1644, 1.1644, 1.1644, 0.000, -0.187326f, 2.141772f, 1.678674f, -0.650424f, 0.000};
= {1.1678, 1.1678, 1.1678, 0.0000, -0.1879, 2.1481, 1.6836, -0.6523, 0.0000};

} // namespace Ffmpeg::ColorSpace

Expand Down
9 changes: 9 additions & 0 deletions ffmpeg/videorender/openglrender.cc
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ void OpenglRender::initSubTexture()

void OpenglRender::setColorSpace()
{
GLfloat contrast = 1.0;
GLfloat saturation = 0;
GLfloat brightness = 0;
auto *avFrame = d_ptr->framePtr->avFrame();
switch (avFrame->colorspace) {
case AVCOL_SPC_BT470BG:
Expand All @@ -196,6 +199,9 @@ void OpenglRender::setColorSpace()
d_ptr->programPtr->setUniformValue("offset", ColorSpace::kBT2020ffset);
d_ptr->programPtr->setUniformValue("colorConversion",
QMatrix3x3(ColorSpace::kBT2020Matrix.data()));
contrast = 1.6;
saturation = 1.0;
brightness = 0;
break;
//case AVCOL_SPC_BT709:
default:
Expand All @@ -204,6 +210,9 @@ void OpenglRender::setColorSpace()
QMatrix3x3(ColorSpace::kBT709Matrix.data()));
break;
}
d_ptr->programPtr->setUniformValue("contrast", contrast);
d_ptr->programPtr->setUniformValue("saturation", saturation);
d_ptr->programPtr->setUniformValue("brightness", brightness);
}

auto OpenglRender::fitToScreen(const QSize &size) -> QMatrix4x4
Expand Down
35 changes: 33 additions & 2 deletions ffmpeg/videorender/shader/video_nv12.frag
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
#version 330 core

in vec2 TexCord; // 纹理坐标
out vec4 FragColor; // 输出颜色
in vec2 TexCord; // 纹理坐标
out vec4 FragColor; // 输出颜色

uniform sampler2D tex_y;
uniform sampler2D tex_u;

uniform vec3 offset;
uniform mat3 colorConversion;

uniform float contrast;
uniform float saturation;
uniform float brightness;

vec3 adjustContrast(vec3 rgb, float contrast) // 对比度调整函数
{
vec3 temp = rgb - vec3(0.5);
temp = temp * contrast;
temp = temp + vec3(0.5);
temp = clamp(temp, vec3(0.0), vec3(1.0));
return temp;
}

vec3 adjustSaturation(vec3 rgb, float saturation) // 饱和度调整函数
{
const vec3 luminosityFactor = vec3(0.2126, 0.7152, 0.0722);
vec3 grayscale = vec3(dot(rgb, luminosityFactor));
return mix(grayscale, rgb, 1.0 + saturation);
}

vec3 adjustBrightness(vec3 rgb, float brightness) // 亮度调整函数
{
vec3 temp = rgb + vec3(brightness);
temp = clamp(temp, vec3(0.0), vec3(1.0));
return temp;
}

void main()
{
vec3 yuv;
Expand All @@ -20,5 +47,9 @@ void main()
yuv += offset;
rgb = yuv * colorConversion;

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

FragColor = vec4(rgb, 1.0);
}

0 comments on commit 5e6cf11

Please sign in to comment.