Skip to content

Commit

Permalink
Use per thread buffer allocation instead of per frame
Browse files Browse the repository at this point in the history
  • Loading branch information
mawen1250 committed Aug 27, 2016
1 parent d040cdd commit 9b4c106
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 132 deletions.
57 changes: 21 additions & 36 deletions include/BM3D_Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#define BM3D_BASE_H_


#include <unordered_map>
#include <thread>
#include "BM3D.h"


Expand All @@ -52,6 +54,8 @@ class BM3D_Data_Base
_Mypara para;
std::vector<BM3D_FilterData> f;

std::unordered_map<std::thread::id, FLType *> buffer0, buffer1, buffer2;

public:
explicit BM3D_Data_Base(bool _wiener,
const VSAPI *_vsapi = nullptr, std::string _FunctionName = "Base", std::string _NameSpace = "bm3d")
Expand All @@ -60,45 +64,26 @@ class BM3D_Data_Base
{}

BM3D_Data_Base(const _Myt &right) = delete;

BM3D_Data_Base(_Myt &&right)
: _Mybase(std::move(right)),
rdef(right.rdef), rnode(right.rnode), rvi(right.rvi),
para_default(right.para_default), para(right.para),
f(std::move(right.f))
{
right.rdef = false;
right.rnode = nullptr;
right.rvi = nullptr;
}

BM3D_Data_Base(_Myt &&right) = delete;
_Myt &operator=(const _Myt &right) = delete;

_Myt &operator=(_Myt &&right)
{
_Mybase::operator=(std::move(right));

if (rdef && rnode) vsapi->freeNode(rnode);

rdef = right.rdef;
rnode = right.rnode;
rvi = right.rvi;

para_default = right.para_default;
para = right.para;

f = std::move(right.f);

right.rdef = false;
right.rnode = nullptr;
right.rvi = nullptr;

return *this;
}
_Myt &operator=(_Myt &&right) = delete;

virtual ~BM3D_Data_Base() override
{
if (rdef && rnode) vsapi->freeNode(rnode);

for (auto &e : buffer0)
{
AlignedFree(e.second);
}
for (auto &e : buffer1)
{
AlignedFree(e.second);
}
for (auto &e : buffer2)
{
AlignedFree(e.second);
}
}

virtual int arguments_process(const VSMap *in, VSMap *out) override;
Expand Down Expand Up @@ -135,7 +120,7 @@ class BM3D_Process_Base
typedef BlockGroup<FLType, FLType> block_group;

private:
const _Mydata &d;
_Mydata &d;

protected:
const VSFrameRef *ref = nullptr;
Expand Down Expand Up @@ -167,7 +152,7 @@ class BM3D_Process_Base
virtual void process_coreS() override;

public:
BM3D_Process_Base(const _Mydata &_d, int _n, VSFrameContext *_frameCtx, VSCore *_core, const VSAPI *_vsapi)
BM3D_Process_Base(_Mydata &_d, int _n, VSFrameContext *_frameCtx, VSCore *_core, const VSAPI *_vsapi)
: _Mybase(_d, _n, _frameCtx, _core, _vsapi), d(_d)
{
if (d.rdef)
Expand Down
16 changes: 3 additions & 13 deletions include/BM3D_Basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,9 @@ class BM3D_Basic_Data
{}

BM3D_Basic_Data(const _Myt &right) = delete;

BM3D_Basic_Data(_Myt &&right)
: _Mybase(std::move(right))
{}

BM3D_Basic_Data(_Myt &&right) = delete;
_Myt &operator=(const _Myt &right) = delete;

_Myt &operator=(_Myt &&right)
{
_Mybase::operator=(std::move(right));

return *this;
}
_Myt &operator=(_Myt &&right) = delete;

virtual ~BM3D_Basic_Data() override {}

Expand All @@ -80,7 +70,7 @@ class BM3D_Basic_Process
const _Mydata &d;

public:
BM3D_Basic_Process(const _Mydata &_d, int _n, VSFrameContext *_frameCtx, VSCore *_core, const VSAPI *_vsapi)
BM3D_Basic_Process(_Mydata &_d, int _n, VSFrameContext *_frameCtx, VSCore *_core, const VSAPI *_vsapi)
: _Mybase(_d, _n, _frameCtx, _core, _vsapi), d(_d)
{}

Expand Down
16 changes: 3 additions & 13 deletions include/BM3D_Final.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,9 @@ class BM3D_Final_Data
{}

BM3D_Final_Data(const _Myt &right) = delete;

BM3D_Final_Data(_Myt &&right)
: _Mybase(std::move(right))
{}

BM3D_Final_Data(_Myt &&right) = delete;
_Myt &operator=(const _Myt &right) = delete;

_Myt &operator=(_Myt &&right)
{
_Mybase::operator=(std::move(right));

return *this;
}
_Myt &operator=(_Myt &&right) = delete;

virtual ~BM3D_Final_Data() override {}

Expand All @@ -80,7 +70,7 @@ class BM3D_Final_Process
const _Mydata &d;

public:
BM3D_Final_Process(const _Mydata &_d, int _n, VSFrameContext *_frameCtx, VSCore *_core, const VSAPI *_vsapi)
BM3D_Final_Process(_Mydata &_d, int _n, VSFrameContext *_frameCtx, VSCore *_core, const VSAPI *_vsapi)
: _Mybase(_d, _n, _frameCtx, _core, _vsapi), d(_d)
{}

Expand Down
36 changes: 2 additions & 34 deletions include/VBM3D_Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,41 +78,9 @@ class VBM3D_Data_Base
{}

VBM3D_Data_Base(const _Myt &right) = delete;

VBM3D_Data_Base(_Myt &&right)
: _Mybase(std::move(right)),
rdef(right.rdef), rnode(right.rnode), rvi(right.rvi),
para_default(right.para_default), para(right.para),
f(std::move(right.f))
{
right.rdef = false;
right.rnode = nullptr;
right.rvi = nullptr;
}

VBM3D_Data_Base(_Myt &&right) = delete;
_Myt &operator=(const _Myt &right) = delete;

_Myt &operator=(_Myt &&right)
{
_Mybase::operator=(std::move(right));

if (rdef && rnode) vsapi->freeNode(rnode);

rdef = right.rdef;
rnode = right.rnode;
rvi = right.rvi;

para_default = right.para_default;
para = right.para;

f = std::move(right.f);

right.rdef = false;
right.rnode = nullptr;
right.rvi = nullptr;

return *this;
}
_Myt &operator=(_Myt &&right) = delete;

virtual ~VBM3D_Data_Base() override
{
Expand Down
14 changes: 2 additions & 12 deletions include/VBM3D_Basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,9 @@ class VBM3D_Basic_Data
{}

VBM3D_Basic_Data(const _Myt &right) = delete;

VBM3D_Basic_Data(_Myt &&right)
: _Mybase(std::move(right))
{}

VBM3D_Basic_Data(_Myt &&right) = delete;
_Myt &operator=(const _Myt &right) = delete;

_Myt &operator=(_Myt &&right)
{
_Mybase::operator=(std::move(right));

return *this;
}
_Myt &operator=(_Myt &&right) = delete;

virtual ~VBM3D_Basic_Data() override {}

Expand Down
14 changes: 2 additions & 12 deletions include/VBM3D_Final.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,9 @@ class VBM3D_Final_Data
{}

VBM3D_Final_Data(const _Myt &right) = delete;

VBM3D_Final_Data(_Myt &&right)
: _Mybase(std::move(right))
{}

VBM3D_Final_Data(_Myt &&right) = delete;
_Myt &operator=(const _Myt &right) = delete;

_Myt &operator=(_Myt &&right)
{
_Mybase::operator=(std::move(right));

return *this;
}
_Myt &operator=(_Myt &&right) = delete;

virtual ~VBM3D_Final_Data() override {}

Expand Down
52 changes: 42 additions & 10 deletions source/BM3D_Base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,19 @@ void BM3D_Data_Base::init_filter_data()

void BM3D_Process_Base::Kernel(FLType *dst, const FLType *src, const FLType *ref) const
{
std::thread::id threadId = std::this_thread::get_id();
FLType *ResNum = dst, *ResDen = nullptr;

AlignedMalloc(ResDen, dst_pcount[0]);
if (!d.buffer0.count(threadId))
{
AlignedMalloc(ResDen, dst_pcount[0]);
d.buffer0.emplace(threadId, ResDen);
}
else
{
ResDen = d.buffer0.at(threadId);
}

memset(ResNum, 0, sizeof(FLType) * dst_pcount[0]);
memset(ResDen, 0, sizeof(FLType) * dst_pcount[0]);

Expand Down Expand Up @@ -339,36 +349,62 @@ void BM3D_Process_Base::Kernel(FLType *dst, const FLType *src, const FLType *ref
{
dst[i] = ResNum[i] / ResDen[i];
});

AlignedFree(ResDen);
}


void BM3D_Process_Base::Kernel(FLType *dstY, FLType *dstU, FLType *dstV,
const FLType *srcY, const FLType *srcU, const FLType *srcV,
const FLType *refY, const FLType *refU, const FLType *refV) const
{
std::thread::id threadId = std::this_thread::get_id();
FLType *ResNumY = dstY, *ResDenY = nullptr;
FLType *ResNumU = dstU, *ResDenU = nullptr;
FLType *ResNumV = dstV, *ResDenV = nullptr;

if (d.process[0])
{
AlignedMalloc(ResDenY, dst_pcount[0]);
if (!d.buffer0.count(threadId))
{
AlignedMalloc(ResDenY, dst_pcount[0]);
d.buffer0.emplace(threadId, ResDenY);
}
else
{
ResDenY = d.buffer0.at(threadId);
}

memset(ResNumY, 0, sizeof(FLType) * dst_pcount[0]);
memset(ResDenY, 0, sizeof(FLType) * dst_pcount[0]);
}

if (d.process[1])
{
AlignedMalloc(ResDenU, dst_pcount[1]);
if (!d.buffer1.count(threadId))
{
AlignedMalloc(ResDenU, dst_pcount[1]);
d.buffer1.emplace(threadId, ResDenU);
}
else
{
ResDenU = d.buffer1.at(threadId);
}

memset(ResNumU, 0, sizeof(FLType) * dst_pcount[1]);
memset(ResDenU, 0, sizeof(FLType) * dst_pcount[1]);
}

if (d.process[2])
{
AlignedMalloc(ResDenV, dst_pcount[2]);
if (!d.buffer2.count(threadId))
{
AlignedMalloc(ResDenV, dst_pcount[2]);
d.buffer2.emplace(threadId, ResDenV);
}
else
{
ResDenV = d.buffer2.at(threadId);
}

memset(ResNumV, 0, sizeof(FLType) * dst_pcount[2]);
memset(ResDenV, 0, sizeof(FLType) * dst_pcount[2]);
}
Expand Down Expand Up @@ -425,10 +461,6 @@ void BM3D_Process_Base::Kernel(FLType *dstY, FLType *dstU, FLType *dstV,
{
dstV[i] = ResNumV[i] / ResDenV[i];
});

if (d.process[0]) AlignedFree(ResDenY);
if (d.process[1]) AlignedFree(ResDenU);
if (d.process[2]) AlignedFree(ResDenV);
}


Expand Down
4 changes: 2 additions & 2 deletions source/VSPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ static void VS_CC BM3D_Basic_Init(VSMap *in, VSMap *out, void **instanceData, VS

static const VSFrameRef *VS_CC BM3D_Basic_GetFrame(int n, int activationReason, void **instanceData, void **frameData, VSFrameContext *frameCtx, VSCore *core, const VSAPI *vsapi)
{
const BM3D_Basic_Data *d = reinterpret_cast<BM3D_Basic_Data *>(*instanceData);
BM3D_Basic_Data *d = reinterpret_cast<BM3D_Basic_Data *>(*instanceData);

if (activationReason == arInitial)
{
Expand Down Expand Up @@ -207,7 +207,7 @@ static void VS_CC BM3D_Final_Init(VSMap *in, VSMap *out, void **instanceData, VS

static const VSFrameRef *VS_CC BM3D_Final_GetFrame(int n, int activationReason, void **instanceData, void **frameData, VSFrameContext *frameCtx, VSCore *core, const VSAPI *vsapi)
{
const BM3D_Final_Data *d = reinterpret_cast<BM3D_Final_Data *>(*instanceData);
BM3D_Final_Data *d = reinterpret_cast<BM3D_Final_Data *>(*instanceData);

if (activationReason == arInitial)
{
Expand Down

0 comments on commit 9b4c106

Please sign in to comment.