Skip to content

Commit

Permalink
Fix invalid array param-type str in AVS+
Browse files Browse the repository at this point in the history
  • Loading branch information
WolframRhodium committed Sep 9, 2021
1 parent f41d4bc commit 611f566
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
22 changes: 8 additions & 14 deletions cpu_source/source_avs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ PVideoFrame __stdcall BM3DFilter::GetFrame(int n, IScriptEnvironment* env) {
PVideoFrame dst = env->NewVideoFrameP(vi, const_cast<PVideoFrame *>(&src_frame));

const auto cast_fp = [](auto * p) {
if constexpr (std::is_same_v<std::decay_t<decltype(p)>, decltype(p)>)
if constexpr (std::is_const_v<std::remove_pointer_t<decltype(p)>>)
return reinterpret_cast<const float *>(p);
else
return reinterpret_cast<float *>(p);
Expand Down Expand Up @@ -148,7 +148,7 @@ PVideoFrame __stdcall BM3DFilter::GetFrame(int n, IScriptEnvironment* env) {
32,
AVS_POOLED_ALLOC
);
return reinterpret_cast<float *>(p);
return cast_fp(p);
} else {
return nullptr;
}
Expand Down Expand Up @@ -233,7 +233,7 @@ PVideoFrame __stdcall BM3DFilter::GetFrame(int n, IScriptEnvironment* env) {
}();

std::array<float * __restrict, 1> dstps {
reinterpret_cast<float *>(dst->GetWritePtr(planes_id[plane]))
cast_fp(dst->GetWritePtr(planes_id[plane]))
};

const int width = src_frame->GetRowSize(planes_id[plane]) / sizeof(float);
Expand All @@ -246,7 +246,7 @@ PVideoFrame __stdcall BM3DFilter::GetFrame(int n, IScriptEnvironment* env) {
sizeof(float) * stride * height * 2 * num_planes(chroma),
32,
AVS_POOLED_ALLOC);
return reinterpret_cast<float *>(p);
return cast_fp(p);
} else {
return nullptr;
}
Expand Down Expand Up @@ -362,9 +362,9 @@ BM3DFilter::BM3DFilter(AVSValue args, IScriptEnvironment* env)
auto array_loader = [](const AVSValue & arg, const auto default_value) {
using T = std::remove_const_t<decltype(default_value)>;
std::array<T, 3> ret;
if (!arg.Defined()) {
if (!arg.Defined() || arg.ArraySize() == 0) {
ret.fill(default_value);
} else if (arg.IsArray()) {
} else {
int length = std::min(arg.ArraySize(), 3);
for (int i = 0; i < length; ++i) {
if constexpr (std::is_same_v<T, float>) {
Expand All @@ -376,12 +376,6 @@ BM3DFilter::BM3DFilter(AVSValue args, IScriptEnvironment* env)
for (int i = length; i < 3; ++i) {
ret[i] = ret[i - 1];
}
} else {
if constexpr (std::is_same_v<T, float>) {
ret.fill(static_cast<float>(arg.AsFloat(default_value)));
} else if (std::is_same_v<T, int>) {
ret.fill(arg.AsInt(default_value));
}
}
return ret;
};
Expand Down Expand Up @@ -460,8 +454,8 @@ const char* __stdcall AvisynthPluginInit3(

env->AddFunction("BM3D_CPU",
"c[ref]c"
"[sigma]f[block_step]i[bm_range]"
"i[radius]i[ps_num]i[ps_range]i[chroma]b"
"[sigma]f+[block_step]i+[bm_range]i+"
"[radius]i[ps_num]i+[ps_range]i+[chroma]b"
, BM3DFilter::Create, nullptr);

return "BM3D algorithm (AVX2 version)";
Expand Down
19 changes: 7 additions & 12 deletions source/source_avs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,12 @@ BM3DFilter::BM3DFilter(AVSValue args, IScriptEnvironment* env)
}
}

auto array_loader = []<typename T>(const AVSValue & arg, T default_value) {
auto array_loader = [](const AVSValue & arg, const auto default_value) {
using T = std::remove_const_t<decltype(default_value)>;
std::array<T, 3> ret;
if (!arg.Defined()) {
if (!arg.Defined() || arg.ArraySize() == 0) {
ret.fill(default_value);
} else if (arg.IsArray()) {
} else {
int length = std::min(arg.ArraySize(), 3);
for (int i = 0; i < length; ++i) {
if constexpr (std::is_same_v<T, float>) {
Expand All @@ -412,12 +413,6 @@ BM3DFilter::BM3DFilter(AVSValue args, IScriptEnvironment* env)
for (int i = length; i < 3; ++i) {
ret[i] = ret[i - 1];
}
} else {
if constexpr (std::is_same_v<T, float>) {
ret.fill(static_cast<float>(arg.AsFloat(default_value)));
} else if (std::is_same_v<T, int>) {
ret.fill(arg.AsInt(default_value));
}
}
return ret;
};
Expand Down Expand Up @@ -607,9 +602,9 @@ const char* __stdcall AvisynthPluginInit3(

env->AddFunction("BM3D_CUDA",
"c[ref]c"
"[sigma]f[block_step]i[bm_range]"
"i[radius]i[ps_num]i[ps_range]"
"i[chroma]b[device_id]i[fast]b[extractor_exp]i"
"[sigma]f+[block_step]i+[bm_range]i+"
"[radius]i[ps_num]i+[ps_range]i+"
"[chroma]b[device_id]i[fast]b[extractor_exp]i"
, BM3DFilter::Create, nullptr);

return "BM3D algorithm";
Expand Down

0 comments on commit 611f566

Please sign in to comment.