Skip to content

Commit

Permalink
[新增与改进]: 引入色相调整功能并优化Equalizer配置
Browse files Browse the repository at this point in the history
- 在filter.cc中将`ep`函数重命名为`eq`,以提供更清晰的命名,并添加了gamma和hue的调整支持。
- 添加了新的`hue`函数,允许用户通过FFmpeg滤镜直接调整色相。
- 在filter.hpp中声明了新的`eq`和`hue`函数,提供外部访问。
- 在openglrender.cc中更新了`paintVideoFrame`方法,增加了对gamma和hue uniform值的支持。
- video_color.frag着色器中新增了gamma和hue的调整代码段,允许更细致的颜色调整。
- shaderutils.cc更新,将gamma和hue调整逻辑加入到片段着色器代码生成过程中。
- widgetrender.cc中调整了构造滤镜字符串的逻辑,包括hue的调整。
- 在equalizer.cc中为Equalizer类新增了gamma和hue的调整逻辑,包括ffGamma、eqGamma、ffHue和eqHue方法。
- 为equalizer.hpp添加了gamma和hue相关的方法声明,以便支持新的颜色调整功能。
  • Loading branch information
RealChuan committed Jun 14, 2024
1 parent b4518fb commit 4ebe753
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 46 deletions.
12 changes: 9 additions & 3 deletions src/ffmpeg/filter/filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,18 @@ auto Filter::scale(const QSize &size) -> QString
return QString("scale=%1:%2").arg(QString::number(size.width()), QString::number(size.height()));
}

auto Filter::ep(const MediaConfig::Equalizer &equalizer) -> QString
auto Filter::eq(const MediaConfig::Equalizer &equalizer) -> QString
{
return QString("eq=contrast=%1:saturation=%2:brightness=%3")
return QString("eq=contrast=%1:saturation=%2:brightness=%3:gamma=%4")
.arg(QString::number(equalizer.eqContrast()),
QString::number(equalizer.eqSaturation()),
QString::number(equalizer.eqBrightness()));
QString::number(equalizer.eqBrightness()),
QString::number(equalizer.eqGamma()));
}

auto Filter::hue(int value) -> QString
{
return QString("hue=%1").arg(QString::number(value));
}

// need z.lib libzimg support zscale
Expand Down
3 changes: 2 additions & 1 deletion src/ffmpeg/filter/filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class Filter : public QObject
auto buffersinkCtx() -> FilterContext *;

static auto scale(const QSize &size) -> QString;
static auto ep(const MediaConfig::Equalizer &equalizer) -> QString;
static auto eq(const MediaConfig::Equalizer &equalizer) -> QString;
static auto hue(int value) -> QString;
static auto zscale(ColorUtils::Primaries::Type destPrimaries, Tonemap::Type type) -> QString;

private:
Expand Down
2 changes: 2 additions & 0 deletions src/ffmpeg/videorender/openglrender.cc
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ void OpenglRender::paintVideoFrame()
d_ptr->programPtr->setUniformValue("contrast", m_equalizer.ffContrast());
d_ptr->programPtr->setUniformValue("saturation", m_equalizer.ffSaturation());
d_ptr->programPtr->setUniformValue("brightness", m_equalizer.ffBrightness());
d_ptr->programPtr->setUniformValue("gamma", m_equalizer.ffGamma());
d_ptr->programPtr->setUniformValue("hue", m_equalizer.ffHue());
draw();
d_ptr->programPtr->release();
d_ptr->frameChanged = false;
Expand Down
15 changes: 15 additions & 0 deletions src/ffmpeg/videorender/shader/video_color.frag
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
uniform float contrast;
uniform float saturation;
uniform float brightness;
uniform float gamma;
uniform float hue;

vec3 adjustBrightness(vec3 rgb, float brightness) // 调整亮度
{
Expand Down Expand Up @@ -41,3 +43,16 @@ vec3 adjustSaturation(vec3 rgb, float saturation) // 调整饱和度
hsv.y = hsv.y * saturation;
return hsv2Rgb(hsv);
}

vec3 adjustGamma(vec3 rgb, float gamma) // 调整gamma
{
gamma = clamp(gamma, 0.1, 10.0);
return pow(rgb, vec3(1.0 / gamma));
}

vec3 adjustHue(vec3 color, float hue)
{
vec3 hsv = rgb2Hsv(color);
hsv.x = hsv.x + hue / 360.0;
return hsv2Rgb(hsv);
}
2 changes: 2 additions & 0 deletions src/ffmpeg/videorender/shaderutils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ void finishFragment(QByteArray &frag)
frag.append(GLSL(color.rgb = adjustContrast(color.rgb, contrast);\n));
frag.append(GLSL(color.rgb = adjustSaturation(color.rgb, saturation);\n));
frag.append(GLSL(color.rgb = adjustBrightness(color.rgb, brightness);\n));
frag.append(GLSL(color.rgb = adjustGamma(color.rgb, gamma);\n));
frag.append(GLSL(color.rgb = adjustHue(color.rgb, hue);\n));
frag.append(GLSL(FragColor = color;\n));
}

Expand Down
5 changes: 4 additions & 1 deletion src/ffmpeg/videorender/widgetrender.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ class WidgetRender::WidgetRenderPrivate
reinterpret_cast<uint8_t *>(&pix_fmt),
sizeof(pix_fmt),
AV_OPT_SEARCH_CHILDREN);
auto filterSpec = QString("%1,%2").arg(Filter::scale(size), Filter::ep(equalizer));
auto filterSpec = QString("%1,%2,%3")
.arg(Filter::scale(size),
Filter::eq(equalizer),
Filter::hue(equalizer.eqHue()));
filterPtr->config(filterSpec);
}
auto framePtrs = filterPtr->filterFrame(framePtr.data());
Expand Down
95 changes: 58 additions & 37 deletions src/mediaconfig/equalizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace MediaConfig {
class Equalizer::EqualizerPrivate
{
public:
EqualizerPrivate(Equalizer *q)
explicit EqualizerPrivate(Equalizer *q)
: q_ptr(q)
, contrastRange(-100, 100)
, saturationRange(-100, 100)
Expand Down Expand Up @@ -45,7 +45,7 @@ Equalizer::Equalizer(const Equalizer &other)
d_ptr->hueRange = other.d_ptr->hueRange;
}

Equalizer &Equalizer::operator=(const Equalizer &other)
auto Equalizer::operator=(const Equalizer &other) -> Equalizer &
{
d_ptr->contrastRange = other.d_ptr->contrastRange;
d_ptr->saturationRange = other.d_ptr->saturationRange;
Expand All @@ -56,9 +56,9 @@ Equalizer &Equalizer::operator=(const Equalizer &other)
return *this;
}

Equalizer::~Equalizer() {}
Equalizer::~Equalizer() = default;

bool Equalizer::operator==(const Equalizer &other) const
auto Equalizer::operator==(const Equalizer &other) const -> bool
{
return d_ptr->contrastRange == other.d_ptr->contrastRange
&& d_ptr->saturationRange == other.d_ptr->saturationRange
Expand All @@ -67,94 +67,115 @@ bool Equalizer::operator==(const Equalizer &other) const
&& d_ptr->hueRange == other.d_ptr->hueRange;
}

bool Equalizer::operator!=(const Equalizer &other) const
auto Equalizer::operator!=(const Equalizer &other) const -> bool
{
return !(*this == other);
}

Equalizer::EqualizerRange &Equalizer::contrastRange() const
auto Equalizer::contrastRange() const -> Equalizer::EqualizerRange &
{
return d_ptr->contrastRange;
}

float Equalizer::ffContrast() const
auto Equalizer::ffContrast() const -> float
{
return Utils::rangeMap(d_ptr->contrastRange.value(),
d_ptr->contrastRange.min(),
d_ptr->contrastRange.max(),
0.0,
2.0);
return eqContrast();
}

float Equalizer::eqContrast() const
auto Equalizer::eqContrast() const -> float
{
// The value must be a float value in range -1000.0 to 1000.0. The default value is "1".
return Utils::rangeMap(d_ptr->contrastRange.value(),
d_ptr->contrastRange.min(),
d_ptr->contrastRange.max(),
0.0,
-0.0,
2.0);
// The value must be a float value in range -1000.0 to 1000.0. The default value is "1".
}

Equalizer::EqualizerRange &Equalizer::saturationRange() const
auto Equalizer::saturationRange() const -> Equalizer::EqualizerRange &
{
return d_ptr->saturationRange;
}

float Equalizer::ffSaturation() const
auto Equalizer::ffSaturation() const -> float
{
return Utils::rangeMap(d_ptr->saturationRange.value(),
d_ptr->saturationRange.min(),
d_ptr->saturationRange.max(),
0.0,
2.0);
return eqSaturation();
}

float Equalizer::eqSaturation() const
auto Equalizer::eqSaturation() const -> float
{
// The value must be a float in range 0.0 to 3.0. The default value is "1".
auto value = d_ptr->saturationRange.value();
if (value == 0) {
return 1.0;
}
if (value > 1) {
if (value > 0) {
return Utils::rangeMap(value, 0, d_ptr->saturationRange.max(), 1.0, 3.0);
}
return Utils::rangeMap(value, d_ptr->saturationRange.min(), 0, 0, 1.0);
}

Equalizer::EqualizerRange &Equalizer::brightnessRange() const
auto Equalizer::brightnessRange() const -> Equalizer::EqualizerRange &
{
return d_ptr->brightnessRange;
}

float Equalizer::ffBrightness() const
auto Equalizer::ffBrightness() const -> float
{
return Utils::rangeMap(d_ptr->brightnessRange.value(),
d_ptr->brightnessRange.min(),
d_ptr->brightnessRange.max(),
-1,
1.0);
return eqBrightness();
}

float Equalizer::eqBrightness() const
auto Equalizer::eqBrightness() const -> float
{
// The value must be a float value in range -1.0 to 1.0. The default value is "0".
return Utils::rangeMap(d_ptr->brightnessRange.value(),
d_ptr->brightnessRange.min(),
d_ptr->brightnessRange.max(),
-1,
1);
// The value must be a float value in range -1000.0 to 1000.0. The default value is "1".
-1.0,
1.0);
}

Equalizer::EqualizerRange &Equalizer::gammaRange() const
auto Equalizer::gammaRange() const -> Equalizer::EqualizerRange &
{
return d_ptr->gammaRange;
}

Equalizer::EqualizerRange &Equalizer::hueRange() const
auto Equalizer::ffGamma() const -> float
{
return eqGamma();
}

auto Equalizer::eqGamma() const -> float
{
// The value must be a float in range 0.1 to 10.0. The default value is "1".
auto value = d_ptr->gammaRange.value();
if (value == 0) {
return 1.0;
}
if (value > 0) {
return Utils::rangeMap(value, 0, d_ptr->gammaRange.max(), 1.0, 10.0);
}
return Utils::rangeMap(value, d_ptr->gammaRange.min(), 0, 0.1, 1.0);
}

auto Equalizer::hueRange() const -> Equalizer::EqualizerRange &
{
return d_ptr->hueRange;
}

auto Equalizer::ffHue() const -> float
{
return eqHue();
}

auto Equalizer::eqHue() const -> float
{
// Set the hue shift in degrees to apply. Default is 0. Allowed range is from -180 to 180.
return Utils::rangeMap(d_ptr->hueRange.value(),
d_ptr->hueRange.min(),
d_ptr->hueRange.max(),
-180.0,
180.0);
}

} // namespace MediaConfig
4 changes: 4 additions & 0 deletions src/mediaconfig/equalizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ class MEDIACONFIG_EXPORT Equalizer
[[nodiscard]] auto eqBrightness() const -> float;

[[nodiscard]] auto gammaRange() const -> EqualizerRange &;
[[nodiscard]] auto ffGamma() const -> float;
[[nodiscard]] auto eqGamma() const -> float;

[[nodiscard]] auto hueRange() const -> EqualizerRange &;
[[nodiscard]] auto ffHue() const -> float;
[[nodiscard]] auto eqHue() const -> float;

private:
class EqualizerPrivate;
Expand Down
7 changes: 3 additions & 4 deletions src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void Utils::setSurfaceFormatVersion(int major, int minor)
QSurfaceFormat::setDefaultFormat(surfaceFormat);
}

QByteArray Utils::readAllFile(const QString &filePath)
auto Utils::readAllFile(const QString &filePath) -> QByteArray
{
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly)) {
Expand All @@ -30,10 +30,9 @@ QByteArray Utils::readAllFile(const QString &filePath)
return buf;
}

float Utils::rangeMap(float value, float min, float max, float newMin, float newMax)
auto Utils::rangeMap(float value, float min, float max, float newMin, float newMax) -> float
{
Q_ASSERT(min <= max);
Q_ASSERT(newMin <= newMax);
Q_ASSERT(min <= max && newMin <= newMax);
Q_ASSERT(min <= value && value <= max);
return (value - min) * (newMax - newMin) / (max - min) + newMin;
}
Expand Down

0 comments on commit 4ebe753

Please sign in to comment.