Skip to content

Commit

Permalink
Fix overflow when clip2 is used
Browse files Browse the repository at this point in the history
Asd-g committed Apr 22, 2022
1 parent 4e52050 commit c524d83
Showing 7 changed files with 31 additions and 35 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
##### 0.9.4:
Fixed overflow when `clip2` is used.

##### 0.9.3:
Added parameter `thr`. (vinverse only)

37 changes: 17 additions & 20 deletions vinverse/vinverse.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <algorithm>
#include <emmintrin.h>
#include <string>

#include "vinverse.h"
@@ -150,6 +149,8 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_c(void* __restrict dstp_,

if constexpr (eclip)
{
constexpr auto peak = std::numeric_limits<T>::max();

for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
@@ -173,8 +174,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_c(void* __restrict dstp_,
const int minm = srcp[x] - amnt_;
const int maxf = srcp[x] + amnt_;

df = std::max(df, minm);
dstp[x] = std::min(df, maxf);
dstp[x] = std::clamp(std::clamp(df, minm, maxf), 0, static_cast<int>(peak));
}
}
else
@@ -190,8 +190,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_c(void* __restrict dstp_,
const int minm = srcp[x] - amnt_;
const int maxf = srcp[x] + amnt_;

df = std::max(df, minm);
dstp[x] = std::min(df, maxf);
dstp[x] = std::clamp(std::clamp(df, minm, maxf), 0, static_cast<int>(peak));
}
}

@@ -227,8 +226,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_c(void* __restrict dstp_,
const int minm = srcp[x] - amnt_;
const int maxf = srcp[x] + amnt_;

df = std::max(df, minm);
dstp[x] = std::min(df, maxf);
dstp[x] = std::clamp(df, minm, maxf);
}
}
else
@@ -245,8 +243,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_c(void* __restrict dstp_,
const int minm = srcp[x] - amnt_;
const int maxf = srcp[x] + amnt_;

df = std::max(df, minm);
dstp[x] = std::min(df, maxf);
dstp[x] = std::clamp(df, minm, maxf);
}
}

@@ -260,7 +257,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_c(void* __restrict dstp_,

template <typename T, VinverseMode mode, bool eclip, bool thresh>
Vinverse<T, mode, eclip, thresh>::Vinverse(PClip child, float sstr, int amnt, int uv, float scl, int opt, PClip clip2, int thr, IScriptEnvironment* env)
: GenericVideoFilter(child), sstr_(sstr), amnt_(amnt), uv_(uv), scl_(scl), opt_(opt), clip2_(clip2), thr_(thr), blur3_buffer(nullptr), blur6_buffer(nullptr)
: GenericVideoFilter(child), sstr_(sstr), amnt_(amnt), uv_(uv), scl_(scl), clip2_(clip2), thr_(thr), blur3_buffer(nullptr), blur6_buffer(nullptr)
{
if (!vi.IsPlanar())
env->ThrowError("Vinverse: only planar input is supported!");
@@ -274,18 +271,18 @@ Vinverse<T, mode, eclip, thresh>::Vinverse(PClip child, float sstr, int amnt, in
env->ThrowError("Vinverse: amnt must be greater than 0 and less than or equal to %s!", std::to_string(peak).c_str());
if (uv < 1 || uv > 3)
env->ThrowError("Vinverse: uv must be set to 1, 2, or 3!");
if (opt_ < -1 || opt_ > 3)
if (opt < -1 || opt > 3)
env->ThrowError("Vinverse: opt must be between -1..3.");

avx512 = !!(env->GetCPUFlags() & CPUF_AVX512F);
avx2 = !!(env->GetCPUFlags() & CPUF_AVX2);
sse2 = !!(env->GetCPUFlags() & CPUF_SSE2);
const bool avx512 = !!(env->GetCPUFlags() & CPUF_AVX512F);
const bool avx2 = !!(env->GetCPUFlags() & CPUF_AVX2);
const bool sse2 = !!(env->GetCPUFlags() & CPUF_SSE2);

if (!avx512 && opt_ == 3)
if (!avx512 && opt == 3)
env->ThrowError("Vinverse: opt=3 requires AVX512F.");
if (!avx2 && opt_ == 2)
if (!avx2 && opt == 2)
env->ThrowError("Vinverse: opt=2 requires AVX2.");
if (!sse2 && opt_ == 1)
if (!sse2 && opt == 1)
env->ThrowError("Vinverse: opt=1 requires SSE2.");

if constexpr (eclip)
@@ -303,7 +300,7 @@ Vinverse<T, mode, eclip, thresh>::Vinverse(PClip child, float sstr, int amnt, in
if (thr_ < 0 || thr_ > peak)
env->ThrowError("Vinverse: thr must be between 0..%s!", std::to_string(peak).c_str());

if ((avx512 && opt_ < 0) || opt_ == 3)
if ((avx512 && opt < 0) || opt == 3)
{
pb_pitch = (vi.width + 63) & ~63;

@@ -350,7 +347,7 @@ Vinverse<T, mode, eclip, thresh>::Vinverse(PClip child, float sstr, int amnt, in

fin_plane = &Vinverse::finalize_plane_avx512;
}
else if ((avx2 && opt_ < 0) || opt_ == 2)
else if ((avx2 && opt < 0) || opt == 2)
{
pb_pitch = (vi.width + 31) & ~31;

@@ -397,7 +394,7 @@ Vinverse<T, mode, eclip, thresh>::Vinverse(PClip child, float sstr, int amnt, in

fin_plane = &Vinverse::finalize_plane_avx2;
}
else if ((sse2 && opt_ < 0) || opt_ == 1)
else if ((sse2 && opt < 0) || opt == 1)
{
pb_pitch = (vi.width + 15) & ~15;

4 changes: 0 additions & 4 deletions vinverse/vinverse.h
Original file line number Diff line number Diff line change
@@ -27,7 +27,6 @@ class Vinverse : public GenericVideoFilter
int amnt_;
int uv_;
float scl_;
int opt_;
PClip clip2_;
int thr_;

@@ -37,9 +36,6 @@ class Vinverse : public GenericVideoFilter
int pb_pitch;
std::unique_ptr<T[]> buffer;

bool avx512;
bool avx2;
bool sse2;
bool v8;

void finalize_plane_c(void* __restrict dstp_, const void* srcp_, const void* pb3_, const void* pb6_, int src_pitch, int dst_pitch, int pb_pitch, int clip2_pitch, int width, int height) noexcept;
8 changes: 4 additions & 4 deletions vinverse/vinverse.rc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <winver.h>

VS_VERSION_INFO VERSIONINFO
FILEVERSION 0,9,3,0
PRODUCTVERSION 0,9,3,0
FILEVERSION 0,9,4,0
PRODUCTVERSION 0,9,4,0
FILEFLAGSMASK VS_FFI_FILEFLAGSMASK
FILEFLAGS 0x0L
FILEOS VOS__WINDOWS32
@@ -15,11 +15,11 @@ BEGIN
BEGIN
VALUE "Comments", "A simple filter to remove residual combing."
VALUE "FileDescription", "vinverse for AviSynth 2.6 / AviSynth+"
VALUE "FileVersion", "0.9.3"
VALUE "FileVersion", "0.9.4"
VALUE "InternalName", "vinverse"
VALUE "OriginalFilename", "vinverse.dll"
VALUE "ProductName", "vinverse"
VALUE "ProductVersion", "0.9.3"
VALUE "ProductVersion", "0.9.4"
END
END
BLOCK "VarFileInfo"
6 changes: 3 additions & 3 deletions vinverse/vinverse_avx2.cpp
Original file line number Diff line number Diff line change
@@ -564,7 +564,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_avx2(void* __restrict dstp
df_h = select(ch_hi, min(df_h, maxf_h), src_hi);
}
//
auto result = compress_saturated(df_l, df_h);
auto result = compress_saturated_s2u(df_l, df_h);
result.store(dstp + x);
}
else
@@ -638,7 +638,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_avx2(void* __restrict dstp
df_h = max(df_h, minm_h);
df_h = min(df_h, maxf_h);
//
auto result = compress_saturated(df_l, df_h);
auto result = compress_saturated_s2u(df_l, df_h);
result.store(dstp + x);
}
}
@@ -921,7 +921,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_avx2(void* __restrict dstp

Vec8i df_h;

auto ch_hi = abs(d1i_hi) >= Vec16s(thr_);
auto ch_hi = abs(d1i_hi) >= Vec8i(thr_);

int64_t check1_lo;
int64_t check1_hi;
4 changes: 2 additions & 2 deletions vinverse/vinverse_avx512.cpp
Original file line number Diff line number Diff line change
@@ -576,7 +576,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_avx512(void* __restrict ds
df_h = select(ch_hi, min(df_h, maxf_h), src_hi);
}
//
auto result = compress_saturated(df_l, df_h);
auto result = compress_saturated_s2u(df_l, df_h);
result.store(dstp + x);
}
else
@@ -650,7 +650,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_avx512(void* __restrict ds
df_h = max(df_h, minm_h);
df_h = min(df_h, maxf_h);
//
auto result = compress_saturated(df_l, df_h);
auto result = compress_saturated_s2u(df_l, df_h);
result.store(dstp + x);
}
}
4 changes: 2 additions & 2 deletions vinverse/vinverse_sse2.cpp
Original file line number Diff line number Diff line change
@@ -480,7 +480,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_sse2(void* __restrict dstp
df = max(df, minm);
df = min(df, maxf);

select(Vec16cb(ch), compress_saturated(df, zero), Vec16uc().loadl(srcp + x)).storel(dstp + x);
select(Vec16cb(ch), compress_saturated_s2u(df, zero), Vec16uc().loadl(srcp + x)).storel(dstp + x);
}
}
else
@@ -517,7 +517,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_sse2(void* __restrict dstp
df = max(df, minm);
df = min(df, maxf);

auto result = compress_saturated(df, zero);
auto result = compress_saturated_s2u(df, zero);
result.storel(dstp + x);
}
}

0 comments on commit c524d83

Please sign in to comment.