Path: blob/master/modules/superres/src/cuda/btv_l1_gpu.cu
16358 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "opencv2/opencv_modules.hpp"4344#if defined(HAVE_OPENCV_CUDAARITHM) && defined(HAVE_OPENCV_CUDAWARPING) && defined(HAVE_OPENCV_CUDAFILTERS)4546#include "opencv2/core/cuda/common.hpp"47#include "opencv2/core/cuda/transform.hpp"48#include "opencv2/core/cuda/vec_traits.hpp"49#include "opencv2/core/cuda/vec_math.hpp"5051using namespace cv::cuda;52using namespace cv::cuda::device;5354namespace btv_l1_cudev55{56void buildMotionMaps(PtrStepSzf forwardMotionX, PtrStepSzf forwardMotionY,57PtrStepSzf backwardMotionX, PtrStepSzf bacwardMotionY,58PtrStepSzf forwardMapX, PtrStepSzf forwardMapY,59PtrStepSzf backwardMapX, PtrStepSzf backwardMapY);6061template <int cn>62void upscale(const PtrStepSzb src, PtrStepSzb dst, int scale, cudaStream_t stream);6364void diffSign(PtrStepSzf src1, PtrStepSzf src2, PtrStepSzf dst, cudaStream_t stream);6566void loadBtvWeights(const float* weights, size_t count);67template <int cn> void calcBtvRegularization(PtrStepSzb src, PtrStepSzb dst, int ksize);68}6970namespace btv_l1_cudev71{72__global__ void buildMotionMapsKernel(const PtrStepSzf forwardMotionX, const PtrStepf forwardMotionY,73PtrStepf backwardMotionX, PtrStepf backwardMotionY,74PtrStepf forwardMapX, PtrStepf forwardMapY,75PtrStepf backwardMapX, PtrStepf backwardMapY)76{77const int x = blockIdx.x * blockDim.x + threadIdx.x;78const int y = blockIdx.y * blockDim.y + threadIdx.y;7980if (x >= forwardMotionX.cols || y >= forwardMotionX.rows)81return;8283const float fx = forwardMotionX(y, x);84const float fy = forwardMotionY(y, x);8586const float bx = backwardMotionX(y, x);87const float by = backwardMotionY(y, x);8889forwardMapX(y, x) = x + bx;90forwardMapY(y, x) = y + by;9192backwardMapX(y, x) = x + fx;93backwardMapY(y, x) = y + fy;94}9596void buildMotionMaps(PtrStepSzf forwardMotionX, PtrStepSzf forwardMotionY,97PtrStepSzf backwardMotionX, PtrStepSzf bacwardMotionY,98PtrStepSzf forwardMapX, PtrStepSzf forwardMapY,99PtrStepSzf backwardMapX, PtrStepSzf backwardMapY)100{101const dim3 block(32, 8);102const dim3 grid(divUp(forwardMapX.cols, block.x), divUp(forwardMapX.rows, block.y));103104buildMotionMapsKernel<<<grid, block>>>(forwardMotionX, forwardMotionY,105backwardMotionX, bacwardMotionY,106forwardMapX, forwardMapY,107backwardMapX, backwardMapY);108cudaSafeCall( cudaGetLastError() );109110cudaSafeCall( cudaDeviceSynchronize() );111}112113template <typename T>114__global__ void upscaleKernel(const PtrStepSz<T> src, PtrStep<T> dst, const int scale)115{116const int x = blockIdx.x * blockDim.x + threadIdx.x;117const int y = blockIdx.y * blockDim.y + threadIdx.y;118119if (x >= src.cols || y >= src.rows)120return;121122dst(y * scale, x * scale) = src(y, x);123}124125template <int cn>126void upscale(const PtrStepSzb src, PtrStepSzb dst, int scale, cudaStream_t stream)127{128typedef typename TypeVec<float, cn>::vec_type src_t;129130const dim3 block(32, 8);131const dim3 grid(divUp(src.cols, block.x), divUp(src.rows, block.y));132133upscaleKernel<src_t><<<grid, block, 0, stream>>>((PtrStepSz<src_t>) src, (PtrStepSz<src_t>) dst, scale);134cudaSafeCall( cudaGetLastError() );135136if (stream == 0)137cudaSafeCall( cudaDeviceSynchronize() );138}139140template void upscale<1>(const PtrStepSzb src, PtrStepSzb dst, int scale, cudaStream_t stream);141template void upscale<3>(const PtrStepSzb src, PtrStepSzb dst, int scale, cudaStream_t stream);142template void upscale<4>(const PtrStepSzb src, PtrStepSzb dst, int scale, cudaStream_t stream);143144__device__ __forceinline__ float diffSign(float a, float b)145{146return a > b ? 1.0f : a < b ? -1.0f : 0.0f;147}148__device__ __forceinline__ float3 diffSign(const float3& a, const float3& b)149{150return make_float3(151a.x > b.x ? 1.0f : a.x < b.x ? -1.0f : 0.0f,152a.y > b.y ? 1.0f : a.y < b.y ? -1.0f : 0.0f,153a.z > b.z ? 1.0f : a.z < b.z ? -1.0f : 0.0f154);155}156__device__ __forceinline__ float4 diffSign(const float4& a, const float4& b)157{158return make_float4(159a.x > b.x ? 1.0f : a.x < b.x ? -1.0f : 0.0f,160a.y > b.y ? 1.0f : a.y < b.y ? -1.0f : 0.0f,161a.z > b.z ? 1.0f : a.z < b.z ? -1.0f : 0.0f,1620.0f163);164}165166struct DiffSign : binary_function<float, float, float>167{168__device__ __forceinline__ float operator ()(float a, float b) const169{170return diffSign(a, b);171}172};173}174175namespace cv { namespace cuda { namespace device176{177template <> struct TransformFunctorTraits<btv_l1_cudev::DiffSign> : DefaultTransformFunctorTraits<btv_l1_cudev::DiffSign>178{179enum { smart_block_dim_y = 8 };180enum { smart_shift = 4 };181};182}}}183184namespace btv_l1_cudev185{186void diffSign(PtrStepSzf src1, PtrStepSzf src2, PtrStepSzf dst, cudaStream_t stream)187{188transform(src1, src2, dst, DiffSign(), WithOutMask(), stream);189}190191__constant__ float c_btvRegWeights[16*16];192193template <typename T>194__global__ void calcBtvRegularizationKernel(const PtrStepSz<T> src, PtrStep<T> dst, const int ksize)195{196const int x = blockIdx.x * blockDim.x + threadIdx.x + ksize;197const int y = blockIdx.y * blockDim.y + threadIdx.y + ksize;198199if (y >= src.rows - ksize || x >= src.cols - ksize)200return;201202const T srcVal = src(y, x);203204T dstVal = VecTraits<T>::all(0);205206for (int m = 0, count = 0; m <= ksize; ++m)207{208for (int l = ksize; l + m >= 0; --l, ++count)209dstVal = dstVal + c_btvRegWeights[count] * (diffSign(srcVal, src(y + m, x + l)) - diffSign(src(y - m, x - l), srcVal));210}211212dst(y, x) = dstVal;213}214215void loadBtvWeights(const float* weights, size_t count)216{217cudaSafeCall( cudaMemcpyToSymbol(c_btvRegWeights, weights, count * sizeof(float)) );218}219220template <int cn>221void calcBtvRegularization(PtrStepSzb src, PtrStepSzb dst, int ksize)222{223typedef typename TypeVec<float, cn>::vec_type src_t;224225const dim3 block(32, 8);226const dim3 grid(divUp(src.cols, block.x), divUp(src.rows, block.y));227228calcBtvRegularizationKernel<src_t><<<grid, block>>>((PtrStepSz<src_t>) src, (PtrStepSz<src_t>) dst, ksize);229cudaSafeCall( cudaGetLastError() );230231cudaSafeCall( cudaDeviceSynchronize() );232}233234template void calcBtvRegularization<1>(PtrStepSzb src, PtrStepSzb dst, int ksize);235template void calcBtvRegularization<3>(PtrStepSzb src, PtrStepSzb dst, int ksize);236template void calcBtvRegularization<4>(PtrStepSzb src, PtrStepSzb dst, int ksize);237}238239#endif240241242