Skip to content

Commit

Permalink
Separate BM3D_VAggregate from BM3DCPU_AVS
Browse files Browse the repository at this point in the history
  • Loading branch information
WolframRhodium committed Aug 6, 2021
1 parent 28db68e commit 8339115
Show file tree
Hide file tree
Showing 3 changed files with 341 additions and 119 deletions.
119 changes: 0 additions & 119 deletions cpu_source/source_avs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,121 +449,6 @@ AVSValue __cdecl BM3DFilter::Create(
return new BM3DFilter(args, env);
}

class VAggregateFilter : public GenericVideoFilter {
std::vector<int> planes_id;
int radius;

public:
VAggregateFilter(AVSValue args, IScriptEnvironment* env);
PVideoFrame __stdcall GetFrame(int n, IScriptEnvironment* env) override;
static AVSValue __cdecl Create(
AVSValue args, void* user_data, IScriptEnvironment* env);
int __stdcall SetCacheHints(int cachehints, int frame_range) override {
return cachehints == CACHE_GET_MTMODE ? MT_NICE_FILTER : 0;
}
};

PVideoFrame __stdcall VAggregateFilter::GetFrame(int n, IScriptEnvironment* env) {
int temporal_width = 2 * radius + 1;

const std::vector srcs = [&](){
std::vector<PVideoFrame> temp;

temp.reserve(temporal_width);

for (int i = -radius; i <= radius; ++i) {
int clamped_n = std::clamp(n + i, 0, vi.num_frames - 1);
temp.push_back(child->GetFrame(clamped_n, env));
}

return temp;
}();

PVideoFrame dst = env->NewVideoFrameP(vi, const_cast<PVideoFrame *>(&srcs[radius]));

std::vector<float> buffer;
buffer.reserve(2 * vi.height * vi.width);

for (unsigned plane = 0; plane < std::size(planes_id); ++plane) {
int width {
vi.IsYUV() && plane != 0 ?
vi.width >> vi.GetPlaneWidthSubsampling(PLANAR_U) :
vi.width
};
int height {
vi.IsYUV() && plane != 0 ?
vi.height >> vi.GetPlaneHeightSubsampling(PLANAR_U) :
vi.height
};

memset(buffer.data(), 0, 2 * height * vi.width * sizeof(float));

for (int i = 0; i < temporal_width; ++i) {
int stride = srcs[i]->GetPitch(planes_id[plane]) / sizeof(float);

const float * agg_src {
reinterpret_cast<const float *>(srcs[i]->GetReadPtr(planes_id[plane])) +
(temporal_width - 1 - i) * 2 * height * stride
};
float * agg_dst = buffer.data();

for (int y = 0; y < 2 * height; ++y) {
for (int x = 0; x < width; ++x) {
agg_dst[x] += agg_src[x];
}
agg_dst += vi.width;
agg_src += stride;
}
}

auto dstp = reinterpret_cast<float *>(dst->GetWritePtr(planes_id[plane]));
Aggregation(
dstp, dst->GetPitch(planes_id[plane]) / sizeof(float),
buffer.data(), vi.width,
width, height);
}

return dst;
}

VAggregateFilter::VAggregateFilter(AVSValue args, IScriptEnvironment* env)
: GenericVideoFilter(args[0].AsClip())
{
env->CheckVersion(8);

if (
vi.BitsPerComponent() != 32 ||
!vi.IsPlanar() ||
!(vi.IsY() || vi.IsYUV() || vi.IsRGB())
) {
env->ThrowError("BM3D_VAggregate: only 32bit float planar Y/YUV/RGB input supported");
}

if (vi.IsY()) {
planes_id = { PLANAR_Y };
} else if (vi.IsYUV()) {
planes_id = { PLANAR_Y, PLANAR_U, PLANAR_V };
} else if (vi.IsRGB()) {
planes_id = { PLANAR_R, PLANAR_G, PLANAR_B };
} else {
env->ThrowError("BM3D_VAggregate: Unknown sample type");
}

radius = args[1].AsInt(0);
if (radius <= 0) {
env->ThrowError("BM3D_VAggregate: \"radius\" must be positive");
}

vi.height /= 2 * (2 * radius + 1);
}

AVSValue __cdecl VAggregateFilter::Create(
AVSValue args, void* user_data, IScriptEnvironment* env
) {

return new VAggregateFilter(args, env);
}

const AVS_Linkage *AVS_linkage {};

extern "C" __declspec(dllexport)
Expand All @@ -579,9 +464,5 @@ const char* __stdcall AvisynthPluginInit3(
"i[radius]i[ps_num]i[ps_range]i[chroma]b"
, BM3DFilter::Create, nullptr);

env->AddFunction("BM3D_VAggregate",
"c[radius]i",
VAggregateFilter::Create, nullptr);

return "BM3D algorithm (AVX2 version)";
}
166 changes: 166 additions & 0 deletions msvc/BM3D_VAggregate_AVS.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\vaggregate\source_avs.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{BFC1DF98-1AED-4FD8-8369-4F31ED1657E1}</ProjectGuid>
<RootNamespace>BM3DCUDA</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<ProjectName>BM3D_VAggregate_AVS</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings" />
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>D:\AviSynth+\FilterSDK\include;$(VC_IncludePath);$(WindowsSDK_IncludePath);</IncludePath>
<IntDir>$(Platform)\$(Configuration)\$(ProjectName)\</IntDir>
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>D:\AviSynth+\FilterSDK\include;$(VC_IncludePath);$(WindowsSDK_IncludePath);</IncludePath>
<LibraryPath>$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64)</LibraryPath>
<IntDir>$(Platform)\$(Configuration)\$(ProjectName)\</IntDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpplatest</LanguageStandard>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<EnableEnhancedInstructionSet>AdvancedVectorExtensions</EnableEnhancedInstructionSet>
<FloatingPointModel>Fast</FloatingPointModel>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>false</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpplatest</LanguageStandard>
<EnableEnhancedInstructionSet>AdvancedVectorExtensions</EnableEnhancedInstructionSet>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<AssemblerOutput>NoListing</AssemblerOutput>
<FloatingPointModel>Fast</FloatingPointModel>
<DebugInformationFormat>None</DebugInformationFormat>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>false</GenerateDebugInformation>
<AdditionalDependencies>%(AdditionalDependencies);</AdditionalDependencies>
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets" />
</Project>
Loading

0 comments on commit 8339115

Please sign in to comment.