From c6130449bb1f3df9f0bc14526d09d95696714e0d Mon Sep 17 00:00:00 2001 From: Aakash Preetam Date: Thu, 20 Mar 2025 15:48:48 +0530 Subject: [PATCH 1/4] Add FastCV Custom Allocator --- modules/fastcv/include/opencv2/fastcv.hpp | 1 + .../include/opencv2/fastcv/allocator.hpp | 30 ++++++ modules/fastcv/src/allocator.cpp | 92 +++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 modules/fastcv/include/opencv2/fastcv/allocator.hpp create mode 100644 modules/fastcv/src/allocator.cpp diff --git a/modules/fastcv/include/opencv2/fastcv.hpp b/modules/fastcv/include/opencv2/fastcv.hpp index 292e83a2dc3..f177a7f6e2a 100644 --- a/modules/fastcv/include/opencv2/fastcv.hpp +++ b/modules/fastcv/include/opencv2/fastcv.hpp @@ -30,6 +30,7 @@ #include "opencv2/fastcv/thresh.hpp" #include "opencv2/fastcv/tracking.hpp" #include "opencv2/fastcv/warp.hpp" +#include "opencv2/fastcv/allocator.hpp" /** * @defgroup fastcv Module-wrapper for FastCV hardware accelerated functions diff --git a/modules/fastcv/include/opencv2/fastcv/allocator.hpp b/modules/fastcv/include/opencv2/fastcv/allocator.hpp new file mode 100644 index 00000000000..d56edd04e99 --- /dev/null +++ b/modules/fastcv/include/opencv2/fastcv/allocator.hpp @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 +*/ + +#ifndef OPENCV_FASTCV_ALLOCATOR_HPP +#define OPENCV_FASTCV_ALLOCATOR_HPP + +#include + +namespace cv { +namespace fastcv { + +//! @addtogroup fastcv +//! @{ + +/** + * @brief Gets the default FastCV allocator. + * This function returns a pointer to the default FastCV allocator, which is optimized + * for use with DSP. + * + * @return Pointer to the default FastCV allocator. + */ +CV_EXPORTS cv::MatAllocator* getDefaultFastCVAllocator(); +//! @} + +} // fastcv:: +} // cv:: + +#endif // OPENCV_FASTCV_ALLOCATOR_HPP diff --git a/modules/fastcv/src/allocator.cpp b/modules/fastcv/src/allocator.cpp new file mode 100644 index 00000000000..1fb9c258bd2 --- /dev/null +++ b/modules/fastcv/src/allocator.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 +*/ + +#include "precomp.hpp" + +namespace cv { +namespace fastcv { + +struct FastCVAllocator: public cv::MatAllocator +{ +public: + FastCVAllocator(); + ~FastCVAllocator(); + + cv::UMatData* allocate(int dims, const int* sizes, int type, void* data, size_t* step, cv::AccessFlag flags, cv::UMatUsageFlags usageFlags) const CV_OVERRIDE; + bool allocate(cv::UMatData* u, cv::AccessFlag accessFlags, cv::UMatUsageFlags usageFlags) const CV_OVERRIDE; + void deallocate(cv::UMatData* u) const CV_OVERRIDE; +}; + +FastCVAllocator::FastCVAllocator() +{ +} + +FastCVAllocator::~FastCVAllocator() +{ +} + +cv::UMatData* FastCVAllocator::allocate(int dims, const int* sizes, int type, + void* data0, size_t* step, cv::AccessFlag flags, + cv::UMatUsageFlags usageFlags) const +{ + CV_UNUSED(flags); + CV_UNUSED(usageFlags); + + size_t total = CV_ELEM_SIZE(type); + for( int i = dims-1; i >= 0; i-- ) + { + if( step ) + { + if( data0 && step[i] != CV_AUTOSTEP ) + { + CV_Assert(total <= step[i]); + total = step[i]; + } + else + step[i] = total; + } + total *= sizes[i]; + } + uchar* data = data0 ? (uchar*)data0 : (uchar*)fcvMemAlloc(total, 16); + cv::UMatData* u = new cv::UMatData(this); + u->data = u->origdata = data; + u->size = total; + if(data0) + u->flags |= cv::UMatData::USER_ALLOCATED; + + return u; +} + +bool FastCVAllocator::allocate(cv::UMatData* u, cv::AccessFlag accessFlags, cv::UMatUsageFlags usageFlags) const +{ + CV_UNUSED(accessFlags); + CV_UNUSED(usageFlags); + + return u != nullptr; +} + +void FastCVAllocator::deallocate(cv::UMatData* u) const +{ + if(!u) + return; + + CV_Assert(u->urefcount == 0); + CV_Assert(u->refcount == 0); + if( !(u->flags & cv::UMatData::USER_ALLOCATED) ) + { + fcvMemFree(u->origdata); + u->origdata = 0; + } + delete u; +} + +cv::MatAllocator* getDefaultFastCVAllocator() +{ + static cv::MatAllocator* allocator = new FastCVAllocator; + return allocator; +} + +} +} From d75fd5fb82066f3d4c952832b19549e9916e7d35 Mon Sep 17 00:00:00 2001 From: Aakash Preetam Date: Thu, 20 Mar 2025 17:45:27 +0530 Subject: [PATCH 2/4] Add DSP initialization --- modules/fastcv/include/opencv2/fastcv.hpp | 1 + .../include/opencv2/fastcv/dsp_init.hpp | 42 ++++++++++++++++ modules/fastcv/src/dsp_init.cpp | 49 +++++++++++++++++++ modules/fastcv/src/precomp.hpp | 37 +++++++++++++- 4 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 modules/fastcv/include/opencv2/fastcv/dsp_init.hpp create mode 100644 modules/fastcv/src/dsp_init.cpp diff --git a/modules/fastcv/include/opencv2/fastcv.hpp b/modules/fastcv/include/opencv2/fastcv.hpp index f177a7f6e2a..55cd768d219 100644 --- a/modules/fastcv/include/opencv2/fastcv.hpp +++ b/modules/fastcv/include/opencv2/fastcv.hpp @@ -31,6 +31,7 @@ #include "opencv2/fastcv/tracking.hpp" #include "opencv2/fastcv/warp.hpp" #include "opencv2/fastcv/allocator.hpp" +#include "opencv2/fastcv/dsp_init.hpp" /** * @defgroup fastcv Module-wrapper for FastCV hardware accelerated functions diff --git a/modules/fastcv/include/opencv2/fastcv/dsp_init.hpp b/modules/fastcv/include/opencv2/fastcv/dsp_init.hpp new file mode 100644 index 00000000000..0251dc8545a --- /dev/null +++ b/modules/fastcv/include/opencv2/fastcv/dsp_init.hpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 +*/ + +#ifndef OPENCV_FASTCV_DSP_INIT_HPP +#define OPENCV_FASTCV_DSP_INIT_HPP + +#include + +namespace cv { +namespace fastcv { +namespace dsp { + + //! @addtogroup fastcv + //! @{ + + /** + * @brief Initializes the FastCV DSP environment. + * + * This function sets up the necessary environment for FastCV DSP operations. + * It ensures that the DSP context is initialized and sets the custom memory allocator + * for FastCV operations. + * + * @return int Returns 0 on success, and a non-zero value on failure. + */ + CV_EXPORTS_W int fastcvq6init(); + + /** + * @brief Deinitializes the FastCV DSP environment. + * + * This function cleans up the FastCV DSP environment, releasing any resources + * that were allocated during initialization. + */ + CV_EXPORTS_W void fastcvq6deinit(); + //! @} + +} // dsp:: +} // fastcv:: +} // cv:: + +#endif // OPENCV_FASTCV_DSP_INIT_HPP diff --git a/modules/fastcv/src/dsp_init.cpp b/modules/fastcv/src/dsp_init.cpp new file mode 100644 index 00000000000..a956abeeddf --- /dev/null +++ b/modules/fastcv/src/dsp_init.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 +*/ + +#include "precomp.hpp" + +namespace cv { +namespace fastcv { +namespace dsp { + +int fastcvq6init() +{ + // Get the DSP context + FastCvDspContext& context = FastCvDspContext::getContext(); + + // Check if DSP is initialized + if (!context.isDspInitialized) + { + CV_Error(cv::Error::StsBadArg, cv::format("DSP initialization failed!")); + return 1; + } + + // Get custom allocator + cv::MatAllocator* allocator = cv::fastcv::getDefaultFastCVAllocator(); + + // Ensure the allocator is not the standard one + CV_Assert(allocator != cv::Mat::getStdAllocator()); + + // Check if the current allocator is already set to the custom allocator + if (cv::Mat::getDefaultAllocator() != allocator) + { + // Set the custom allocator + cv::Mat::setDefaultAllocator(allocator); + } + + return 0; +} + +void fastcvq6deinit() +{ + // Deinitialize the DSP environment + fcvQ6DeInit(); +} + + +} // namespace dsp +} // namespace fastcv +} // namespace cv \ No newline at end of file diff --git a/modules/fastcv/src/precomp.hpp b/modules/fastcv/src/precomp.hpp index c2929d76cc1..9a0e8cd2e68 100644 --- a/modules/fastcv/src/precomp.hpp +++ b/modules/fastcv/src/precomp.hpp @@ -10,11 +10,12 @@ #include #include "opencv2/core/private.hpp" #include "opencv2/core/utils/logger.hpp" - +#include #include #include #include "fastcv.h" +#include "fastcvDsp.h" namespace cv { namespace fastcv { @@ -30,6 +31,7 @@ namespace fastcv { #define FCV_KernelSize_SHIFT 3 #define FCV_MAKETYPE(ksize,depth) ((ksize< fcvStatusStrings = { @@ -72,6 +74,39 @@ struct FastCvContext bool isInitialized; }; +namespace dsp { + + struct FastCvDspContext + { + public: + // Initialize at first call + // Defines a static local variable context. + static FastCvDspContext& getContext() + { + //Instance is created only once. + static FastCvDspContext context; + return context; + } + + //Constructor is called when the FastCvDspContext instance is created + FastCvDspContext() + { + if (fcvQ6Init() != 0) + { + CV_LOG_WARNING(NULL, "Failed to switch FastCV DSP operation mode"); + isDspInitialized = false; + } + else + { + CV_LOG_INFO(NULL, "FastCV DSP Operation Mode Switched"); + isDspInitialized = true; + } + } + + bool isDspInitialized; + }; + +} // namespace dsp } // namespace fastcv } // namespace cv From f1b67da6adf919690672e1ee29a8d927e0c5585f Mon Sep 17 00:00:00 2001 From: Aakash Preetam Date: Thu, 20 Mar 2025 17:56:38 +0530 Subject: [PATCH 3/4] Added sumOfAbsoluteDiffs DSP API with Accuracy and Perf Test --- modules/fastcv/include/opencv2/fastcv.hpp | 1 + modules/fastcv/include/opencv2/fastcv/sad.hpp | 34 +++++++++++++++ modules/fastcv/perf/perf_sad.cpp | 42 +++++++++++++++++++ modules/fastcv/src/sad.cpp | 34 +++++++++++++++ modules/fastcv/test/test_sad.cpp | 39 +++++++++++++++++ 5 files changed, 150 insertions(+) create mode 100644 modules/fastcv/include/opencv2/fastcv/sad.hpp create mode 100644 modules/fastcv/perf/perf_sad.cpp create mode 100644 modules/fastcv/src/sad.cpp create mode 100644 modules/fastcv/test/test_sad.cpp diff --git a/modules/fastcv/include/opencv2/fastcv.hpp b/modules/fastcv/include/opencv2/fastcv.hpp index 55cd768d219..a895e838059 100644 --- a/modules/fastcv/include/opencv2/fastcv.hpp +++ b/modules/fastcv/include/opencv2/fastcv.hpp @@ -32,6 +32,7 @@ #include "opencv2/fastcv/warp.hpp" #include "opencv2/fastcv/allocator.hpp" #include "opencv2/fastcv/dsp_init.hpp" +#include "opencv2/fastcv/sad.hpp" /** * @defgroup fastcv Module-wrapper for FastCV hardware accelerated functions diff --git a/modules/fastcv/include/opencv2/fastcv/sad.hpp b/modules/fastcv/include/opencv2/fastcv/sad.hpp new file mode 100644 index 00000000000..1959da793e3 --- /dev/null +++ b/modules/fastcv/include/opencv2/fastcv/sad.hpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 +*/ + +#ifndef OPENCV_SAD_HPP +#define OPENCV_SAD_HPP + +#include + +namespace cv { +namespace fastcv { +namespace dsp { + +/** + * @defgroup fastcv Module-wrapper for FastCV hardware accelerated functions + */ + +//! @addtogroup fastcv +//! @{ +/** + * @brief Sum of absolute differences of an image against an 8x8 template. + * @param _patch The first input image data, type CV_8UC1 + * @param _src The input image data, type CV_8UC1 + * @param _dst The output image data, type CV_16UC1 +*/ +CV_EXPORTS_W void sumOfAbsoluteDiffs(cv::InputArray _patch, cv::InputArray _src, cv::OutputArray _dst); +//! @} + +} +} +} + +#endif diff --git a/modules/fastcv/perf/perf_sad.cpp b/modules/fastcv/perf/perf_sad.cpp new file mode 100644 index 00000000000..ad98fb483d3 --- /dev/null +++ b/modules/fastcv/perf/perf_sad.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 +*/ + +#include "perf_precomp.hpp" + +namespace opencv_test { + +typedef std::tuple SumOfAbsDiffsPerfParams; +typedef perf::TestBaseWithParam SumOfAbsDiffsPerfTest; + +PERF_TEST_P(SumOfAbsDiffsPerfTest, run, + ::testing::Values(cv::Size(640, 480), // VGA + cv::Size(1280, 720), // 720p + cv::Size(1920, 1080)) // 1080p +) +{ + // Initialize FastCV DSP + int initStatus = cv::fastcv::dsp::fastcvq6init(); + ASSERT_EQ(initStatus, 0) << "Failed to initialize FastCV DSP"; + + auto p = GetParam(); + cv::Size srcSize = std::get<0>(p); + + RNG& rng = cv::theRNG(); + cv::Mat patch(8, 8, CV_8UC1); + cv::Mat src(srcSize, CV_8UC1); + cvtest::randUni(rng, patch, cv::Scalar::all(0), cv::Scalar::all(255)); + cvtest::randUni(rng, src, cv::Scalar::all(0), cv::Scalar::all(255)); + + cv::Mat dst; + while(next()) + { + startTimer(); + cv::fastcv::dsp::sumOfAbsoluteDiffs(patch, src, dst); + stopTimer(); + } + SANITY_CHECK_NOTHING(); +} + +} // namespace diff --git a/modules/fastcv/src/sad.cpp b/modules/fastcv/src/sad.cpp new file mode 100644 index 00000000000..ed9bc427c3d --- /dev/null +++ b/modules/fastcv/src/sad.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 +*/ + +#include "precomp.hpp" + +namespace cv { +namespace fastcv { +namespace dsp { + +void sumOfAbsoluteDiffs(cv::InputArray _patch, cv::InputArray _src, cv::OutputArray _dst) +{ + CV_Assert(!_src.empty() && _src.type() == CV_8UC1); + CV_Assert(_src.step() * _src.rows() > MIN_REMOTE_BUF_SIZE); + CV_Assert(!_patch.empty() && _patch.type() == CV_8UC1); + CV_Assert(_patch.size() == Size(8, 8)); + + Size size = _src.size(); + _dst.create(size, CV_16UC1); + + Mat patch = _patch.getMat(); + Mat src = _src.getMat(); + CV_Assert(((intptr_t)src.data & 0x7) == 0); + + Mat dst = _dst.getMat(); + CV_Assert(((intptr_t)dst.data & 0x7) == 0); + + fcvSumOfAbsoluteDiffs8x8u8_v2Q((uint8_t*)patch.data, patch.step, (uint8_t*)src.data, src.cols, src.rows, src.step, (uint16_t*)dst.data, dst.step); +} + +} // dsp:: +} // fastcv:: +} // cv:: diff --git a/modules/fastcv/test/test_sad.cpp b/modules/fastcv/test/test_sad.cpp new file mode 100644 index 00000000000..2bbdff380cd --- /dev/null +++ b/modules/fastcv/test/test_sad.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 +*/ + +#include "test_precomp.hpp" + +using namespace cv::fastcv::dsp; + +namespace opencv_test { namespace { + +TEST(SadTest, accuracy) +{ + // Initialize FastCV DSP + int initStatus = cv::fastcv::dsp::fastcvq6init(); + ASSERT_EQ(initStatus, 0) << "Failed to initialize FastCV DSP"; + + // Create an 8x8 template patch + cv::Mat patch = cv::Mat::zeros(8, 8, CV_8UC1); + + // Create a source image + cv::Mat src = cv::Mat::ones(512, 512, CV_8UC1) * 255; + + cv::Mat dst; + + cv::fastcv::dsp::sumOfAbsoluteDiffs(patch, src, dst); + + EXPECT_FALSE(dst.empty()); + + // Explicitly deallocate memory + patch.release(); + src.release(); + dst.release(); + + cv::fastcv::dsp::fastcvq6deinit(); +} + +} +} From 995c5c67e748812b615a8be45805aef0eb4dd921 Mon Sep 17 00:00:00 2001 From: Aakash Preetam Date: Fri, 21 Mar 2025 17:08:17 +0530 Subject: [PATCH 4/4] Add memory allocator check and renamed files --- modules/fastcv/include/opencv2/fastcv.hpp | 2 +- .../include/opencv2/fastcv/allocator.hpp | 2 +- .../opencv2/fastcv/{sad.hpp => sad_dsp.hpp} | 2 +- .../perf/{perf_sad.cpp => perf_sad_dsp.cpp} | 2 +- modules/fastcv/src/allocator.cpp | 14 +++++-- modules/fastcv/src/precomp.hpp | 9 +++- modules/fastcv/src/sad.cpp | 34 --------------- modules/fastcv/src/sad_dsp.cpp | 42 +++++++++++++++++++ .../test/{test_sad.cpp => test_sad_dsp.cpp} | 2 +- 9 files changed, 66 insertions(+), 43 deletions(-) rename modules/fastcv/include/opencv2/fastcv/{sad.hpp => sad_dsp.hpp} (91%) rename modules/fastcv/perf/{perf_sad.cpp => perf_sad_dsp.cpp} (94%) delete mode 100644 modules/fastcv/src/sad.cpp create mode 100644 modules/fastcv/src/sad_dsp.cpp rename modules/fastcv/test/{test_sad.cpp => test_sad_dsp.cpp} (92%) diff --git a/modules/fastcv/include/opencv2/fastcv.hpp b/modules/fastcv/include/opencv2/fastcv.hpp index a895e838059..18a26e78f63 100644 --- a/modules/fastcv/include/opencv2/fastcv.hpp +++ b/modules/fastcv/include/opencv2/fastcv.hpp @@ -32,7 +32,7 @@ #include "opencv2/fastcv/warp.hpp" #include "opencv2/fastcv/allocator.hpp" #include "opencv2/fastcv/dsp_init.hpp" -#include "opencv2/fastcv/sad.hpp" +#include "opencv2/fastcv/sad_dsp.hpp" /** * @defgroup fastcv Module-wrapper for FastCV hardware accelerated functions diff --git a/modules/fastcv/include/opencv2/fastcv/allocator.hpp b/modules/fastcv/include/opencv2/fastcv/allocator.hpp index d56edd04e99..92c29c73000 100644 --- a/modules/fastcv/include/opencv2/fastcv/allocator.hpp +++ b/modules/fastcv/include/opencv2/fastcv/allocator.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + * Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved. * SPDX-License-Identifier: Apache-2.0 */ diff --git a/modules/fastcv/include/opencv2/fastcv/sad.hpp b/modules/fastcv/include/opencv2/fastcv/sad_dsp.hpp similarity index 91% rename from modules/fastcv/include/opencv2/fastcv/sad.hpp rename to modules/fastcv/include/opencv2/fastcv/sad_dsp.hpp index 1959da793e3..f3037dc237f 100644 --- a/modules/fastcv/include/opencv2/fastcv/sad.hpp +++ b/modules/fastcv/include/opencv2/fastcv/sad_dsp.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + * Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved. * SPDX-License-Identifier: Apache-2.0 */ diff --git a/modules/fastcv/perf/perf_sad.cpp b/modules/fastcv/perf/perf_sad_dsp.cpp similarity index 94% rename from modules/fastcv/perf/perf_sad.cpp rename to modules/fastcv/perf/perf_sad_dsp.cpp index ad98fb483d3..a2c6bf743b0 100644 --- a/modules/fastcv/perf/perf_sad.cpp +++ b/modules/fastcv/perf/perf_sad_dsp.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + * Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved. * SPDX-License-Identifier: Apache-2.0 */ diff --git a/modules/fastcv/src/allocator.cpp b/modules/fastcv/src/allocator.cpp index 1fb9c258bd2..6d4e5d6d425 100644 --- a/modules/fastcv/src/allocator.cpp +++ b/modules/fastcv/src/allocator.cpp @@ -49,13 +49,14 @@ cv::UMatData* FastCVAllocator::allocate(int dims, const int* sizes, int type, } total *= sizes[i]; } - uchar* data = data0 ? (uchar*)data0 : (uchar*)fcvMemAlloc(total, 16); + uchar* data = data0 ? (uchar*)data0 : (uchar*)fcvHwMemAlloc(total, 16); cv::UMatData* u = new cv::UMatData(this); u->data = u->origdata = data; u->size = total; if(data0) u->flags |= cv::UMatData::USER_ALLOCATED; - + + u->userdata = new std::string("QCOM"); return u; } @@ -76,9 +77,16 @@ void FastCVAllocator::deallocate(cv::UMatData* u) const CV_Assert(u->refcount == 0); if( !(u->flags & cv::UMatData::USER_ALLOCATED) ) { - fcvMemFree(u->origdata); + fcvHwMemFree(u->origdata); u->origdata = 0; } + + if (u->userdata) + { + delete static_cast(u->userdata); + u->userdata = nullptr; + } + delete u; } diff --git a/modules/fastcv/src/precomp.hpp b/modules/fastcv/src/precomp.hpp index 9a0e8cd2e68..2c394055b4b 100644 --- a/modules/fastcv/src/precomp.hpp +++ b/modules/fastcv/src/precomp.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + * Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved. * SPDX-License-Identifier: Apache-2.0 */ @@ -76,6 +76,13 @@ struct FastCvContext namespace dsp { + #define IS_FASTCV_ALLOCATED(mat) \ + ((mat.u && mat.u->userdata && \ + *static_cast(mat.u->userdata) == "QCOM") ? true : \ + (std::cerr << "Allocation check failed for " #mat \ + << ". Please ensure that cv::fastcv::dsp::fastcvq6init() has been called." \ + << std::endl, false)) + struct FastCvDspContext { public: diff --git a/modules/fastcv/src/sad.cpp b/modules/fastcv/src/sad.cpp deleted file mode 100644 index ed9bc427c3d..00000000000 --- a/modules/fastcv/src/sad.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 -*/ - -#include "precomp.hpp" - -namespace cv { -namespace fastcv { -namespace dsp { - -void sumOfAbsoluteDiffs(cv::InputArray _patch, cv::InputArray _src, cv::OutputArray _dst) -{ - CV_Assert(!_src.empty() && _src.type() == CV_8UC1); - CV_Assert(_src.step() * _src.rows() > MIN_REMOTE_BUF_SIZE); - CV_Assert(!_patch.empty() && _patch.type() == CV_8UC1); - CV_Assert(_patch.size() == Size(8, 8)); - - Size size = _src.size(); - _dst.create(size, CV_16UC1); - - Mat patch = _patch.getMat(); - Mat src = _src.getMat(); - CV_Assert(((intptr_t)src.data & 0x7) == 0); - - Mat dst = _dst.getMat(); - CV_Assert(((intptr_t)dst.data & 0x7) == 0); - - fcvSumOfAbsoluteDiffs8x8u8_v2Q((uint8_t*)patch.data, patch.step, (uint8_t*)src.data, src.cols, src.rows, src.step, (uint16_t*)dst.data, dst.step); -} - -} // dsp:: -} // fastcv:: -} // cv:: diff --git a/modules/fastcv/src/sad_dsp.cpp b/modules/fastcv/src/sad_dsp.cpp new file mode 100644 index 00000000000..1637a377f89 --- /dev/null +++ b/modules/fastcv/src/sad_dsp.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 +*/ + +#include "precomp.hpp" + +namespace cv { +namespace fastcv { +namespace dsp { + +void sumOfAbsoluteDiffs(cv::InputArray _patch, cv::InputArray _src, cv::OutputArray _dst) { + cv::Mat patch = _patch.getMat(); + cv::Mat src = _src.getMat(); + + // Check if matrices are allocated by the fastcv allocator + CV_Assert(IS_FASTCV_ALLOCATED(patch)); + CV_Assert(IS_FASTCV_ALLOCATED(src)); + + CV_Assert(!_src.empty() && "src is empty"); + CV_Assert(_src.type() == CV_8UC1 && "src type is not CV_8UC1"); + CV_Assert(_src.step() * _src.rows() > MIN_REMOTE_BUF_SIZE && "src buffer size is too small"); + CV_Assert(!_patch.empty() && "patch is empty"); + CV_Assert(_patch.type() == CV_8UC1 && "patch type is not CV_8UC1"); + CV_Assert(_patch.size() == cv::Size(8, 8) && "patch size is not 8x8"); + + cv::Size size = _src.size(); + _dst.create(size, CV_16UC1); + cv::Mat dst = _dst.getMat(); + + CV_Assert(((intptr_t)src.data & 0x7) == 0 && "src data is not 8-byte aligned"); + CV_Assert(((intptr_t)dst.data & 0x7) == 0 && "dst data is not 8-byte aligned"); + + // Check if dst is allocated by the fastcv allocator + CV_Assert(IS_FASTCV_ALLOCATED(dst)); + + fcvSumOfAbsoluteDiffs8x8u8_v2Q((uint8_t*)patch.data, patch.step, (uint8_t*)src.data, src.cols, src.rows, src.step, (uint16_t*)dst.data, dst.step); +} + +} // dsp:: +} // fastcv:: +} // cv:: diff --git a/modules/fastcv/test/test_sad.cpp b/modules/fastcv/test/test_sad_dsp.cpp similarity index 92% rename from modules/fastcv/test/test_sad.cpp rename to modules/fastcv/test/test_sad_dsp.cpp index 2bbdff380cd..ab2aa723f8a 100644 --- a/modules/fastcv/test/test_sad.cpp +++ b/modules/fastcv/test/test_sad_dsp.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. + * Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved. * SPDX-License-Identifier: Apache-2.0 */