Path: blob/master/modules/superres/src/opencl/superres_btvl1.cl
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) 2010-2012, Multicoreware, Inc., all rights reserved.13// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// @Authors17// Jin Ma jin@multicorewareinc.com18//19// Redistribution and use in source and binary forms, with or without modification,20// are permitted provided that the following conditions are met:21//22// * Redistribution's of source code must retain the above copyright notice,23// this list of conditions and the following disclaimer.24//25// * Redistribution's in binary form must reproduce the above copyright notice,26// this list of conditions and the following disclaimer in the documentation27// and/or other materials provided with the distribution.28//29// * The name of the copyright holders may not be used to endorse or promote products30// derived from this software without specific prior written permission.31//32// This software is provided by the copyright holders and contributors as is and33// any express or implied warranties, including, but not limited to, the implied34// warranties of merchantability and fitness for a particular purpose are disclaimed.35// In no event shall the Intel Corporation or contributors be liable for any direct,36// indirect, incidental, special, exemplary, or consequential damages37// (including, but not limited to, procurement of substitute goods or services;38// loss of use, data, or profits; or business interruption) however caused39// and on any theory of liability, whether in contract, strict liability,40// or tort (including negligence or otherwise) arising in any way out of41// the use of this software, even if advised of the possibility of such damage.42//43//M*/4445#ifndef cn46#define cn 147#endif4849#define sz (int)sizeof(float)50#define src_elem_at(_src, y, step, x) *(__global const float *)(_src + mad24(y, step, (x) * sz))51#define dst_elem_at(_dst, y, step, x) *(__global float *)(_dst + mad24(y, step, (x) * sz))5253__kernel void buildMotionMaps(__global const uchar * forwardMotionPtr, int forwardMotion_step, int forwardMotion_offset,54__global const uchar * backwardMotionPtr, int backwardMotion_step, int backwardMotion_offset,55__global const uchar * forwardMapPtr, int forwardMap_step, int forwardMap_offset,56__global const uchar * backwardMapPtr, int backwardMap_step, int backwardMap_offset,57int rows, int cols)58{59int x = get_global_id(0);60int y = get_global_id(1);6162if (x < cols && y < rows)63{64int forwardMotion_index = mad24(forwardMotion_step, y, (int)sizeof(float2) * x + forwardMotion_offset);65int backwardMotion_index = mad24(backwardMotion_step, y, (int)sizeof(float2) * x + backwardMotion_offset);66int forwardMap_index = mad24(forwardMap_step, y, (int)sizeof(float2) * x + forwardMap_offset);67int backwardMap_index = mad24(backwardMap_step, y, (int)sizeof(float2) * x + backwardMap_offset);6869float2 forwardMotion = *(__global const float2 *)(forwardMotionPtr + forwardMotion_index);70float2 backwardMotion = *(__global const float2 *)(backwardMotionPtr + backwardMotion_index);71__global float2 * forwardMap = (__global float2 *)(forwardMapPtr + forwardMap_index);72__global float2 * backwardMap = (__global float2 *)(backwardMapPtr + backwardMap_index);7374float2 basePoint = (float2)(x, y);7576forwardMap[0] = basePoint + backwardMotion;77backwardMap[0] = basePoint + forwardMotion;78}79}8081__kernel void upscale(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols,82__global uchar * dstptr, int dst_step, int dst_offset, int scale)83{84int x = get_global_id(0);85int y = get_global_id(1);8687if (x < src_cols && y < src_rows)88{89int src_index = mad24(y, src_step, sz * x * cn + src_offset);90int dst_index = mad24(y * scale, dst_step, sz * x * scale * cn + dst_offset);9192__global const float * src = (__global const float *)(srcptr + src_index);93__global float * dst = (__global float *)(dstptr + dst_index);9495#pragma unroll96for (int c = 0; c < cn; ++c)97dst[c] = src[c];98}99}100101102inline float diffSign1(float a, float b)103{104return a > b ? 1.0f : a < b ? -1.0f : 0.0f;105}106107inline float3 diffSign3(float3 a, float3 b)108{109float3 pos;110pos.x = a.x > b.x ? 1.0f : a.x < b.x ? -1.0f : 0.0f;111pos.y = a.y > b.y ? 1.0f : a.y < b.y ? -1.0f : 0.0f;112pos.z = a.z > b.z ? 1.0f : a.z < b.z ? -1.0f : 0.0f;113return pos;114}115116__kernel void diffSign(__global const uchar * src1, int src1_step, int src1_offset,117__global const uchar * src2, int src2_step, int src2_offset,118__global uchar * dst, int dst_step, int dst_offset, int rows, int cols)119{120int x = get_global_id(0);121int y = get_global_id(1);122123if (x < cols && y < rows)124*(__global float *)(dst + mad24(y, dst_step, sz * x + dst_offset)) =125diffSign1(*(__global const float *)(src1 + mad24(y, src1_step, sz * x + src1_offset)),126*(__global const float *)(src2 + mad24(y, src2_step, sz * x + src2_offset)));127}128129__kernel void calcBtvRegularization(__global const uchar * src, int src_step, int src_offset,130__global uchar * dst, int dst_step, int dst_offset, int dst_rows, int dst_cols,131int ksize, __constant float * c_btvRegWeights)132{133int x = get_global_id(0) + ksize;134int y = get_global_id(1) + ksize;135136if (y < dst_rows - ksize && x < dst_cols - ksize)137{138src += src_offset;139140#if cn == 1141const float srcVal = src_elem_at(src, y, src_step, x);142float dstVal = 0.0f;143144for (int m = 0, count = 0; m <= ksize; ++m)145for (int l = ksize; l + m >= 0; --l, ++count)146{147dstVal += c_btvRegWeights[count] * (diffSign1(srcVal, src_elem_at(src, y + m, src_step, x + l))148- diffSign1(src_elem_at(src, y - m, src_step, x - l), srcVal));149}150151dst_elem_at(dst, y, dst_step, x) = dstVal;152#elif cn == 3153__global const float * src0ptr = (__global const float *)(src + mad24(y, src_step, 3 * sz * x + src_offset));154float3 srcVal = (float3)(src0ptr[0], src0ptr[1], src0ptr[2]), dstVal = 0.f;155156for (int m = 0, count = 0; m <= ksize; ++m)157{158for (int l = ksize; l + m >= 0; --l, ++count)159{160__global const float * src1ptr = (__global const float *)(src + mad24(y + m, src_step, 3 * sz * (x + l) + src_offset));161__global const float * src2ptr = (__global const float *)(src + mad24(y - m, src_step, 3 * sz * (x - l) + src_offset));162163float3 src1 = (float3)(src1ptr[0], src1ptr[1], src1ptr[2]);164float3 src2 = (float3)(src2ptr[0], src2ptr[1], src2ptr[2]);165166dstVal += c_btvRegWeights[count] * (diffSign3(srcVal, src1) - diffSign3(src2, srcVal));167}168}169170__global float * dstptr = (__global float *)(dst + mad24(y, dst_step, 3 * sz * x + dst_offset + 0));171dstptr[0] = dstVal.x;172dstptr[1] = dstVal.y;173dstptr[2] = dstVal.z;174#else175#error "Number of channels should be either 1 of 3"176#endif177}178}179180181