Skip to content

Commit 995c5c6

Browse files
committed
Add memory allocator check and renamed files
1 parent f1b67da commit 995c5c6

File tree

9 files changed

+66
-43
lines changed

9 files changed

+66
-43
lines changed

modules/fastcv/include/opencv2/fastcv.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "opencv2/fastcv/warp.hpp"
3333
#include "opencv2/fastcv/allocator.hpp"
3434
#include "opencv2/fastcv/dsp_init.hpp"
35-
#include "opencv2/fastcv/sad.hpp"
35+
#include "opencv2/fastcv/sad_dsp.hpp"
3636

3737
/**
3838
* @defgroup fastcv Module-wrapper for FastCV hardware accelerated functions

modules/fastcv/include/opencv2/fastcv/allocator.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
2+
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

modules/fastcv/include/opencv2/fastcv/sad.hpp renamed to modules/fastcv/include/opencv2/fastcv/sad_dsp.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
2+
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

modules/fastcv/perf/perf_sad.cpp renamed to modules/fastcv/perf/perf_sad_dsp.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
2+
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

modules/fastcv/src/allocator.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@ cv::UMatData* FastCVAllocator::allocate(int dims, const int* sizes, int type,
4949
}
5050
total *= sizes[i];
5151
}
52-
uchar* data = data0 ? (uchar*)data0 : (uchar*)fcvMemAlloc(total, 16);
52+
uchar* data = data0 ? (uchar*)data0 : (uchar*)fcvHwMemAlloc(total, 16);
5353
cv::UMatData* u = new cv::UMatData(this);
5454
u->data = u->origdata = data;
5555
u->size = total;
5656
if(data0)
5757
u->flags |= cv::UMatData::USER_ALLOCATED;
58-
58+
59+
u->userdata = new std::string("QCOM");
5960
return u;
6061
}
6162

@@ -76,9 +77,16 @@ void FastCVAllocator::deallocate(cv::UMatData* u) const
7677
CV_Assert(u->refcount == 0);
7778
if( !(u->flags & cv::UMatData::USER_ALLOCATED) )
7879
{
79-
fcvMemFree(u->origdata);
80+
fcvHwMemFree(u->origdata);
8081
u->origdata = 0;
8182
}
83+
84+
if (u->userdata)
85+
{
86+
delete static_cast<std::string*>(u->userdata);
87+
u->userdata = nullptr;
88+
}
89+
8290
delete u;
8391
}
8492

modules/fastcv/src/precomp.hpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
2+
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

@@ -76,6 +76,13 @@ struct FastCvContext
7676

7777
namespace dsp {
7878

79+
#define IS_FASTCV_ALLOCATED(mat) \
80+
((mat.u && mat.u->userdata && \
81+
*static_cast<std::string*>(mat.u->userdata) == "QCOM") ? true : \
82+
(std::cerr << "Allocation check failed for " #mat \
83+
<< ". Please ensure that cv::fastcv::dsp::fastcvq6init() has been called." \
84+
<< std::endl, false))
85+
7986
struct FastCvDspContext
8087
{
8188
public:

modules/fastcv/src/sad.cpp

-34
This file was deleted.

modules/fastcv/src/sad_dsp.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "precomp.hpp"
7+
8+
namespace cv {
9+
namespace fastcv {
10+
namespace dsp {
11+
12+
void sumOfAbsoluteDiffs(cv::InputArray _patch, cv::InputArray _src, cv::OutputArray _dst) {
13+
cv::Mat patch = _patch.getMat();
14+
cv::Mat src = _src.getMat();
15+
16+
// Check if matrices are allocated by the fastcv allocator
17+
CV_Assert(IS_FASTCV_ALLOCATED(patch));
18+
CV_Assert(IS_FASTCV_ALLOCATED(src));
19+
20+
CV_Assert(!_src.empty() && "src is empty");
21+
CV_Assert(_src.type() == CV_8UC1 && "src type is not CV_8UC1");
22+
CV_Assert(_src.step() * _src.rows() > MIN_REMOTE_BUF_SIZE && "src buffer size is too small");
23+
CV_Assert(!_patch.empty() && "patch is empty");
24+
CV_Assert(_patch.type() == CV_8UC1 && "patch type is not CV_8UC1");
25+
CV_Assert(_patch.size() == cv::Size(8, 8) && "patch size is not 8x8");
26+
27+
cv::Size size = _src.size();
28+
_dst.create(size, CV_16UC1);
29+
cv::Mat dst = _dst.getMat();
30+
31+
CV_Assert(((intptr_t)src.data & 0x7) == 0 && "src data is not 8-byte aligned");
32+
CV_Assert(((intptr_t)dst.data & 0x7) == 0 && "dst data is not 8-byte aligned");
33+
34+
// Check if dst is allocated by the fastcv allocator
35+
CV_Assert(IS_FASTCV_ALLOCATED(dst));
36+
37+
fcvSumOfAbsoluteDiffs8x8u8_v2Q((uint8_t*)patch.data, patch.step, (uint8_t*)src.data, src.cols, src.rows, src.step, (uint16_t*)dst.data, dst.step);
38+
}
39+
40+
} // dsp::
41+
} // fastcv::
42+
} // cv::

modules/fastcv/test/test_sad.cpp renamed to modules/fastcv/test/test_sad_dsp.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
2+
* Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

0 commit comments

Comments
 (0)