Skip to content

Commit

Permalink
eq 滤镜;
Browse files Browse the repository at this point in the history
  • Loading branch information
RealChuan committed Nov 26, 2023
1 parent 0dfb7ca commit 9adb728
Show file tree
Hide file tree
Showing 17 changed files with 308 additions and 216 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- macos-latest
- ubuntu-latest
qt_ver:
- 6.6.0
- 6.6.1
build_type:
- "RelWithDebInfo"
generators:
Expand All @@ -53,7 +53,7 @@ jobs:
choco install ninja
ninja --version
cmake --version
vcpkg install breakpad ffmpeg[opengl,ass,bzip2,freetype,fribidi,zlib] --triplet x64-windows
vcpkg install breakpad ffmpeg[opengl,ass,bzip2,freetype,fribidi,zlib,gpl] --triplet x64-windows
- name: Install dependencies on macos
if: startsWith(matrix.os, 'macos')
shell: bash
Expand All @@ -62,7 +62,7 @@ jobs:
ninja --version
cmake --version
clang --version
vcpkg install breakpad ffmpeg[opengl,ass,bzip2,freetype,fribidi,zlib] --triplet x64-osx
vcpkg install breakpad ffmpeg[opengl,ass,bzip2,freetype,fribidi,zlib,gpl] --triplet x64-osx
- name: Install dependencies on ubuntu
if: startsWith(matrix.os, 'ubuntu')
shell: bash
Expand All @@ -72,7 +72,7 @@ jobs:
ninja --version
cmake --version
gcc --version
vcpkg install breakpad ffmpeg[opengl,ass,bzip2,freetype,fribidi,zlib] --triplet x64-linux
vcpkg install breakpad ffmpeg[opengl,ass,bzip2,freetype,fribidi,zlib,gpl] --triplet x64-linux
- name: Install Qt
uses: jurplel/install-qt-action@v3
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/qmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- macos-latest
- ubuntu-latest
qt_ver:
- 6.6.0
- 6.6.1

steps:
- name: cache vcpkg
Expand All @@ -49,7 +49,7 @@ jobs:
choco install ninja
ninja --version
cmake --version
vcpkg install breakpad ffmpeg[opengl,ass,bzip2,freetype,fribidi,zlib] --triplet x64-windows
vcpkg install breakpad ffmpeg[opengl,ass,bzip2,freetype,fribidi,zlib,gpl] --triplet x64-windows
- name: Install dependencies on macos
if: startsWith(matrix.os, 'macos')
shell: bash
Expand All @@ -58,7 +58,7 @@ jobs:
ninja --version
cmake --version
clang --version
vcpkg install breakpad ffmpeg[opengl,ass,bzip2,freetype,fribidi,zlib] --triplet x64-osx
vcpkg install breakpad ffmpeg[opengl,ass,bzip2,freetype,fribidi,zlib,gpl] --triplet x64-osx
- name: Install dependencies on ubuntu
if: startsWith(matrix.os, 'ubuntu')
shell: bash
Expand All @@ -68,7 +68,7 @@ jobs:
ninja --version
cmake --version
gcc --version
vcpkg install breakpad ffmpeg[opengl,ass,bzip2,freetype,fribidi,zlib] --triplet x64-linux
vcpkg install breakpad ffmpeg[opengl,ass,bzip2,freetype,fribidi,zlib,gpl] --triplet x64-linux
- name: Install Qt
uses: jurplel/install-qt-action@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/toolchain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- macos-latest
- ubuntu-latest
qt_ver:
- 6.6.0
- 6.6.1

steps:
- name: Install Qt
Expand Down
30 changes: 15 additions & 15 deletions examples/player/colorspacedialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,31 @@ class ColorSpaceDialog::ColorSpaceDialogPrivate

contrastSlider = new Slider(q_ptr);
contrastSlider->setRange(colorTrc.contrast_min * multiple, colorTrc.contrast_max * multiple);
contrastSlider->setValue(colorTrc.contrast * multiple);
contrastSlider->setValue(colorTrc.contrast() * multiple);
saturationSlider = new Slider(q_ptr);
saturationSlider->setRange(colorTrc.saturation_min * multiple,
colorTrc.saturation_max * multiple);
saturationSlider->setValue(colorTrc.saturation * multiple);
saturationSlider->setValue(colorTrc.saturation() * multiple);
brightnessSlider = new Slider(q_ptr);
brightnessSlider->setRange(colorTrc.brightness_min * multiple,
colorTrc.brightness_max * multiple);
brightnessSlider->setValue(colorTrc.brightness * multiple);
brightnessSlider->setValue(colorTrc.brightness() * multiple);

contrastSpinBox = new QSpinBox(q_ptr);
contrastSpinBox->setKeyboardTracking(false);
contrastSpinBox->setRange(colorTrc.contrast_min * multiple,
colorTrc.contrast_max * multiple);
contrastSpinBox->setValue(colorTrc.contrast * multiple);
contrastSpinBox->setValue(colorTrc.contrast() * multiple);
saturationSpinBox = new QSpinBox(q_ptr);
saturationSpinBox->setKeyboardTracking(false);
saturationSpinBox->setRange(colorTrc.saturation_min * multiple,
colorTrc.saturation_max * multiple);
saturationSpinBox->setValue(colorTrc.saturation * multiple);
saturationSpinBox->setValue(colorTrc.saturation() * multiple);
brightnessSpinBox = new QSpinBox(q_ptr);
brightnessSpinBox->setKeyboardTracking(false);
brightnessSpinBox->setRange(colorTrc.brightness_min * multiple,
colorTrc.brightness_max * multiple);
brightnessSpinBox->setValue(colorTrc.brightness * multiple);
brightnessSpinBox->setValue(colorTrc.brightness() * multiple);

resetButton = new QToolButton(q_ptr);
resetButton->setText("Reset");
Expand Down Expand Up @@ -98,20 +98,20 @@ ColorSpaceDialog::~ColorSpaceDialog() = default;

void ColorSpaceDialog::setColorSpace(const Ffmpeg::ColorUtils::ColorSpaceTrc &colorTrc)
{
setBlockValue(d_ptr->contrastSpinBox, colorTrc.contrast * d_ptr->multiple);
setBlockValue(d_ptr->saturationSpinBox, colorTrc.saturation * d_ptr->multiple);
setBlockValue(d_ptr->brightnessSpinBox, colorTrc.brightness * d_ptr->multiple);
setBlockValue(d_ptr->contrastSlider, colorTrc.contrast * d_ptr->multiple);
setBlockValue(d_ptr->saturationSlider, colorTrc.saturation * d_ptr->multiple);
setBlockValue(d_ptr->brightnessSlider, colorTrc.brightness * d_ptr->multiple);
setBlockValue(d_ptr->contrastSpinBox, colorTrc.contrast() * d_ptr->multiple);
setBlockValue(d_ptr->saturationSpinBox, colorTrc.saturation() * d_ptr->multiple);
setBlockValue(d_ptr->brightnessSpinBox, colorTrc.brightness() * d_ptr->multiple);
setBlockValue(d_ptr->contrastSlider, colorTrc.contrast() * d_ptr->multiple);
setBlockValue(d_ptr->saturationSlider, colorTrc.saturation() * d_ptr->multiple);
setBlockValue(d_ptr->brightnessSlider, colorTrc.brightness() * d_ptr->multiple);
}

Ffmpeg::ColorUtils::ColorSpaceTrc ColorSpaceDialog::colorSpace() const
{
Ffmpeg::ColorUtils::ColorSpaceTrc colorTrc;
colorTrc.contrast = d_ptr->contrastSlider->value() / d_ptr->multiple;
colorTrc.saturation = d_ptr->saturationSlider->value() / d_ptr->multiple;
colorTrc.brightness = d_ptr->brightnessSlider->value() / d_ptr->multiple;
colorTrc.setContrast(d_ptr->contrastSlider->value() / d_ptr->multiple);
colorTrc.setSaturation(d_ptr->saturationSlider->value() / d_ptr->multiple);
colorTrc.setBrightness(d_ptr->brightnessSlider->value() / d_ptr->multiple);
return colorTrc;
}

Expand Down
88 changes: 83 additions & 5 deletions ffmpeg/colorutils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "colorutils.hpp"
#include "frame.hpp"

#include <utils/utils.h>

extern "C" {
#include <libavutil/frame.h>
#include <libavutil/pixdesc.h>
Expand Down Expand Up @@ -309,25 +311,101 @@ auto Primaries::getAVColorPrimaries(Type type) -> AVColorPrimaries
return AVCOL_PRI_RESERVED0;
}

const float ColorSpaceTrc::contrast_min = 0.0;
const float ColorSpaceTrc::contrast_max = 2.0;
const float ColorSpaceTrc::contrast_default = 1.0;
const float ColorSpaceTrc::saturation_min = 0.0;
const float ColorSpaceTrc::saturation_max = 2.0;
const float ColorSpaceTrc::saturation_default = 1.0;
const float ColorSpaceTrc::brightness_min = -1.0;
const float ColorSpaceTrc::brightness_max = 1.0;
const float ColorSpaceTrc::brightness_default = 0.0;

ColorSpaceTrc::ColorSpaceTrc()
: m_contrast(contrast_default)
, m_saturation(saturation_default)
, m_brightness(brightness_default)
{}

ColorSpaceTrc::ColorSpaceTrc(const ColorSpaceTrc &other)
{
if (this != &other) {
m_contrast = other.m_contrast;
m_saturation = other.m_saturation;
m_brightness = other.m_brightness;
}
}

auto ColorSpaceTrc::operator=(const ColorSpaceTrc &other) -> ColorSpaceTrc &
{
contrast = other.contrast;
saturation = other.saturation;
brightness = other.brightness;
if (this != &other) {
m_contrast = other.m_contrast;
m_saturation = other.m_saturation;
m_brightness = other.m_brightness;
}
return *this;
}

auto ColorSpaceTrc::operator==(const ColorSpaceTrc &other) const -> bool
{
return contrast == other.contrast && saturation == other.saturation
&& brightness == other.brightness;
return m_contrast == other.m_contrast && m_saturation == other.m_saturation
&& m_brightness == other.m_brightness;
}

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

void ColorSpaceTrc::setContrast(float contrast)
{
Q_ASSERT(contrast_min <= contrast && contrast <= contrast_max);
m_contrast = contrast;
}

auto ColorSpaceTrc::eqContrast() const -> float
{
return m_contrast;
// The value must be a float value in range -1000.0 to 1000.0. The default value is "1".
// if (m_contrast == contrast_default) {
// return m_contrast;
// }
// if (m_contrast > 1) {
// return Utils::rangeMap(m_contrast, contrast_default, contrast_max, 1.0, 1000.0);
// }
// return Utils::rangeMap(m_contrast, contrast_min, contrast_default, -1000.0, 1.0);
}

void ColorSpaceTrc::setSaturation(float saturation)
{
Q_ASSERT(saturation_min <= saturation && saturation <= saturation_max);
m_saturation = saturation;
}

auto ColorSpaceTrc::eqSaturation() const -> float
{
// The value must be a float in range 0.0 to 3.0. The default value is "1".
if (m_saturation == saturation_default) {
return m_saturation;
}
if (m_saturation > 1) {
return Utils::rangeMap(m_saturation, saturation_default, saturation_max, 1.0, 3.0);
}
return m_saturation;
}

void ColorSpaceTrc::setBrightness(float brightness)
{
Q_ASSERT(brightness_min <= brightness && brightness <= brightness_max);
m_brightness = brightness;
}

auto ColorSpaceTrc::eqBrightness() const -> float
{
// The value must be a float value in range -1.0 to 1.0. The default value is "0".
return m_brightness;
}

} // namespace ColorUtils

} // namespace Ffmpeg
43 changes: 30 additions & 13 deletions ffmpeg/colorutils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,44 @@ class FFMPEG_EXPORT Primaries : public QObject
static auto getAVColorPrimaries(Type type) -> AVColorPrimaries;
};

struct ColorSpaceTrc
struct FFMPEG_EXPORT ColorSpaceTrc
{
ColorSpaceTrc();
ColorSpaceTrc(const ColorSpaceTrc &other);
auto operator=(const ColorSpaceTrc &other) -> ColorSpaceTrc &;
~ColorSpaceTrc() = default;

auto operator==(const ColorSpaceTrc &other) const -> bool;
auto operator!=(const ColorSpaceTrc &other) const -> bool;

const float contrast_min = 0.0;
const float contrast_max = 2.0;
const float contrast_default = 1.0;
float contrast = 1.0;
void setContrast(float contrast);
[[nodiscard]] auto contrast() const -> float { return m_contrast; }
[[nodiscard]] auto eqContrast() const -> float;

const float saturation_min = 0.0;
const float saturation_max = 2.0;
const float saturation_default = 1.0;
float saturation = 1.0;
void setSaturation(float saturation);
[[nodiscard]] auto saturation() const -> float { return m_saturation; }
[[nodiscard]] auto eqSaturation() const -> float;

const float brightness_min = -1.0;
const float brightness_max = 1.0;
const float brightness_default = 0.0;
float brightness = 0.0;
void setBrightness(float brightness);
[[nodiscard]] auto brightness() const -> float { return m_brightness; }
[[nodiscard]] auto eqBrightness() const -> float;

static const float contrast_min;
static const float contrast_max;
static const float contrast_default;

static const float saturation_min;
static const float saturation_max;
static const float saturation_default;

static const float brightness_min;
static const float brightness_max;
static const float brightness_default;

private:
float m_contrast;
float m_saturation;
float m_brightness;
};

} // namespace ColorUtils
Expand Down
52 changes: 45 additions & 7 deletions ffmpeg/filter/filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ class Filter::FilterPrivate
}
char buf[64];
av_channel_layout_describe(&avFrame->ch_layout, buf, sizeof(buf));
auto args = QString::asprintf(
"time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=%s" PRIx64,
1,
avFrame->sample_rate,
avFrame->sample_rate,
av_get_sample_fmt_name(static_cast<AVSampleFormat>(avFrame->format)),
buf);
auto args
= QString::asprintf("time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=%s",
1,
avFrame->sample_rate,
avFrame->sample_rate,
av_get_sample_fmt_name(static_cast<AVSampleFormat>(avFrame->format)),
buf);
qDebug() << "Audio filter in args:" << args;

create(args);
Expand Down Expand Up @@ -154,4 +154,42 @@ auto Filter::buffersinkCtx() -> FilterContext *
return d_ptr->buffersinkCtx;
}

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 ColorUtils::ColorSpaceTrc &trc) -> QString
{
return QString("eq=contrast=%1:saturation=%2:brightness=%3")
.arg(QString::number(trc.eqContrast()),
QString::number(trc.eqSaturation()),
QString::number(trc.eqBrightness()));
}

// need z.lib libzimg support zscale
auto Filter::zscale(ColorUtils::Primaries::Type destPrimaries, Tonemap::Type type) -> QString
{
QString primaries;
switch (destPrimaries) {
case ColorUtils::Primaries::Type::BT709: primaries = "709"; break;
case ColorUtils::Primaries::Type::SMPTE170M: primaries = "170m"; break;
case ColorUtils::Primaries::Type::SMPTE240M: primaries = "240m"; break;
case ColorUtils::Primaries::Type::BT2020: primaries = "2020"; break;
default: primaries = "input"; break;
};

QString tonemap;
switch (type) {
case Tonemap::Type::CLIP: tonemap = "clip"; break;
case Tonemap::Type::LINEAR: tonemap = "linear"; break;
case Tonemap::Type::GAMMA: tonemap = "gamma"; break;
case Tonemap::Type::REINHARD: tonemap = "reinhard"; break;
case Tonemap::Type::HABLE: tonemap = "hable"; break;
case Tonemap::Type::MOBIUS: tonemap = "mobius"; break;
default: return {};
}
return QString("zscale=t=linear:p=%1,tonemap=%2").arg(primaries, tonemap);
}

} // namespace Ffmpeg
Loading

0 comments on commit 9adb728

Please sign in to comment.