Skip to content

Commit c524d83

Browse files
committed
Fix overflow when clip2 is used
1 parent 4e52050 commit c524d83

File tree

7 files changed

+31
-35
lines changed

7 files changed

+31
-35
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
##### 0.9.4:
2+
Fixed overflow when `clip2` is used.
3+
14
##### 0.9.3:
25
Added parameter `thr`. (vinverse only)
36

vinverse/vinverse.cpp

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <algorithm>
2-
#include <emmintrin.h>
32
#include <string>
43

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

151150
if constexpr (eclip)
152151
{
152+
constexpr auto peak = std::numeric_limits<T>::max();
153+
153154
for (int y = 0; y < height; ++y)
154155
{
155156
for (int x = 0; x < width; ++x)
@@ -173,8 +174,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_c(void* __restrict dstp_,
173174
const int minm = srcp[x] - amnt_;
174175
const int maxf = srcp[x] + amnt_;
175176

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

193-
df = std::max(df, minm);
194-
dstp[x] = std::min(df, maxf);
193+
dstp[x] = std::clamp(std::clamp(df, minm, maxf), 0, static_cast<int>(peak));
195194
}
196195
}
197196

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

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

248-
df = std::max(df, minm);
249-
dstp[x] = std::min(df, maxf);
246+
dstp[x] = std::clamp(df, minm, maxf);
250247
}
251248
}
252249

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

261258
template <typename T, VinverseMode mode, bool eclip, bool thresh>
262259
Vinverse<T, mode, eclip, thresh>::Vinverse(PClip child, float sstr, int amnt, int uv, float scl, int opt, PClip clip2, int thr, IScriptEnvironment* env)
263-
: GenericVideoFilter(child), sstr_(sstr), amnt_(amnt), uv_(uv), scl_(scl), opt_(opt), clip2_(clip2), thr_(thr), blur3_buffer(nullptr), blur6_buffer(nullptr)
260+
: GenericVideoFilter(child), sstr_(sstr), amnt_(amnt), uv_(uv), scl_(scl), clip2_(clip2), thr_(thr), blur3_buffer(nullptr), blur6_buffer(nullptr)
264261
{
265262
if (!vi.IsPlanar())
266263
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
274271
env->ThrowError("Vinverse: amnt must be greater than 0 and less than or equal to %s!", std::to_string(peak).c_str());
275272
if (uv < 1 || uv > 3)
276273
env->ThrowError("Vinverse: uv must be set to 1, 2, or 3!");
277-
if (opt_ < -1 || opt_ > 3)
274+
if (opt < -1 || opt > 3)
278275
env->ThrowError("Vinverse: opt must be between -1..3.");
279276

280-
avx512 = !!(env->GetCPUFlags() & CPUF_AVX512F);
281-
avx2 = !!(env->GetCPUFlags() & CPUF_AVX2);
282-
sse2 = !!(env->GetCPUFlags() & CPUF_SSE2);
277+
const bool avx512 = !!(env->GetCPUFlags() & CPUF_AVX512F);
278+
const bool avx2 = !!(env->GetCPUFlags() & CPUF_AVX2);
279+
const bool sse2 = !!(env->GetCPUFlags() & CPUF_SSE2);
283280

284-
if (!avx512 && opt_ == 3)
281+
if (!avx512 && opt == 3)
285282
env->ThrowError("Vinverse: opt=3 requires AVX512F.");
286-
if (!avx2 && opt_ == 2)
283+
if (!avx2 && opt == 2)
287284
env->ThrowError("Vinverse: opt=2 requires AVX2.");
288-
if (!sse2 && opt_ == 1)
285+
if (!sse2 && opt == 1)
289286
env->ThrowError("Vinverse: opt=1 requires SSE2.");
290287

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

306-
if ((avx512 && opt_ < 0) || opt_ == 3)
303+
if ((avx512 && opt < 0) || opt == 3)
307304
{
308305
pb_pitch = (vi.width + 63) & ~63;
309306

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

351348
fin_plane = &Vinverse::finalize_plane_avx512;
352349
}
353-
else if ((avx2 && opt_ < 0) || opt_ == 2)
350+
else if ((avx2 && opt < 0) || opt == 2)
354351
{
355352
pb_pitch = (vi.width + 31) & ~31;
356353

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

398395
fin_plane = &Vinverse::finalize_plane_avx2;
399396
}
400-
else if ((sse2 && opt_ < 0) || opt_ == 1)
397+
else if ((sse2 && opt < 0) || opt == 1)
401398
{
402399
pb_pitch = (vi.width + 15) & ~15;
403400

vinverse/vinverse.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class Vinverse : public GenericVideoFilter
2727
int amnt_;
2828
int uv_;
2929
float scl_;
30-
int opt_;
3130
PClip clip2_;
3231
int thr_;
3332

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

40-
bool avx512;
41-
bool avx2;
42-
bool sse2;
4339
bool v8;
4440

4541
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;

vinverse/vinverse.rc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include <winver.h>
22

33
VS_VERSION_INFO VERSIONINFO
4-
FILEVERSION 0,9,3,0
5-
PRODUCTVERSION 0,9,3,0
4+
FILEVERSION 0,9,4,0
5+
PRODUCTVERSION 0,9,4,0
66
FILEFLAGSMASK VS_FFI_FILEFLAGSMASK
77
FILEFLAGS 0x0L
88
FILEOS VOS__WINDOWS32
@@ -15,11 +15,11 @@ BEGIN
1515
BEGIN
1616
VALUE "Comments", "A simple filter to remove residual combing."
1717
VALUE "FileDescription", "vinverse for AviSynth 2.6 / AviSynth+"
18-
VALUE "FileVersion", "0.9.3"
18+
VALUE "FileVersion", "0.9.4"
1919
VALUE "InternalName", "vinverse"
2020
VALUE "OriginalFilename", "vinverse.dll"
2121
VALUE "ProductName", "vinverse"
22-
VALUE "ProductVersion", "0.9.3"
22+
VALUE "ProductVersion", "0.9.4"
2323
END
2424
END
2525
BLOCK "VarFileInfo"

vinverse/vinverse_avx2.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_avx2(void* __restrict dstp
564564
df_h = select(ch_hi, min(df_h, maxf_h), src_hi);
565565
}
566566
//
567-
auto result = compress_saturated(df_l, df_h);
567+
auto result = compress_saturated_s2u(df_l, df_h);
568568
result.store(dstp + x);
569569
}
570570
else
@@ -638,7 +638,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_avx2(void* __restrict dstp
638638
df_h = max(df_h, minm_h);
639639
df_h = min(df_h, maxf_h);
640640
//
641-
auto result = compress_saturated(df_l, df_h);
641+
auto result = compress_saturated_s2u(df_l, df_h);
642642
result.store(dstp + x);
643643
}
644644
}
@@ -921,7 +921,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_avx2(void* __restrict dstp
921921

922922
Vec8i df_h;
923923

924-
auto ch_hi = abs(d1i_hi) >= Vec16s(thr_);
924+
auto ch_hi = abs(d1i_hi) >= Vec8i(thr_);
925925

926926
int64_t check1_lo;
927927
int64_t check1_hi;

vinverse/vinverse_avx512.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_avx512(void* __restrict ds
576576
df_h = select(ch_hi, min(df_h, maxf_h), src_hi);
577577
}
578578
//
579-
auto result = compress_saturated(df_l, df_h);
579+
auto result = compress_saturated_s2u(df_l, df_h);
580580
result.store(dstp + x);
581581
}
582582
else
@@ -650,7 +650,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_avx512(void* __restrict ds
650650
df_h = max(df_h, minm_h);
651651
df_h = min(df_h, maxf_h);
652652
//
653-
auto result = compress_saturated(df_l, df_h);
653+
auto result = compress_saturated_s2u(df_l, df_h);
654654
result.store(dstp + x);
655655
}
656656
}

vinverse/vinverse_sse2.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ void Vinverse<T, mode, eclip, thresh>::finalize_plane_sse2(void* __restrict dstp
480480
df = max(df, minm);
481481
df = min(df, maxf);
482482

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

520-
auto result = compress_saturated(df, zero);
520+
auto result = compress_saturated_s2u(df, zero);
521521
result.storel(dstp + x);
522522
}
523523
}

0 commit comments

Comments
 (0)