81229dfb41ac1bda80bc43a93a675d2de3da65de
[platform/upstream/opencv.git] / modules / softcascade / src / cuda_invoker.hpp
1 //M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2008-2012, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and / or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M
42
43
44 #ifndef __OPENCV_ICF_HPP__
45 #define __OPENCV_ICF_HPP__
46
47 #include "opencv2/core/cuda_types.hpp"
48 #include "cuda_runtime_api.h"
49
50 #if defined __CUDACC__
51 # define __device_inline__ __device__ __forceinline__
52 #else
53 # define __device_inline__
54 #endif
55
56
57 namespace cv { namespace softcascade { namespace cudev {
58
59 typedef unsigned char uchar;
60 typedef unsigned int uint;
61 typedef unsigned short ushort;
62
63 struct Octave
64 {
65     float scale;
66     ushort2 size;
67     ushort index;
68     ushort stages;
69     ushort shrinkage;
70
71     Octave(const ushort i, const ushort s, const ushort sh, const ushort2 sz, const float sc)
72     : scale(sc), size(sz), index(i), stages(s), shrinkage(sh) {}
73 };
74
75 struct Level
76 {
77     int octave;
78     int step;
79
80     float relScale;
81     float scaling[2];// calculated according to Dollar paper
82
83     uchar2 workRect;
84     uchar2 objSize;
85
86     Level(int idx, const Octave& oct, const float scale, const int w, const int h);
87     __device_inline__ Level(){}
88 };
89
90 struct Node
91 {
92     uchar4 rect;
93     // ushort channel;
94     unsigned int threshold;
95
96     enum { THRESHOLD_MASK = 0x0FFFFFFF };
97
98     Node(const uchar4 r, const unsigned int ch, const unsigned int t) : rect(r), threshold(t + (ch << 28)) {}
99 };
100
101 struct Detection
102 {
103     ushort x;
104     ushort y;
105     ushort w;
106     ushort h;
107
108     float confidence;
109     int kind;
110
111     Detection(){}
112     __device_inline__ Detection(int _x, int _y, uchar _w, uchar _h, float c)
113     : x(static_cast<ushort>(_x)), y(static_cast<ushort>(_y)), w(_w), h(_h), confidence(c), kind(0) {}
114 };
115
116 struct GK107PolicyX4
117 {
118     enum {WARP = 32, STA_X = WARP, STA_Y = 8, SHRINKAGE = 4};
119     typedef float2 roi_type;
120     static const dim3 block()
121     {
122         return dim3(STA_X, STA_Y);
123     }
124 };
125
126 template<typename Policy>
127 struct CascadeInvoker
128 {
129     CascadeInvoker(): levels(0), stages(0), nodes(0), leaves(0), scales(0) {}
130
131     CascadeInvoker(const cv::cuda::PtrStepSzb& _levels, const cv::cuda::PtrStepSzf& _stages,
132                    const cv::cuda::PtrStepSzb& _nodes,  const cv::cuda::PtrStepSzf& _leaves)
133     : levels((const Level*)_levels.ptr()),
134       stages((const float*)_stages.ptr()),
135       nodes((const Node*)_nodes.ptr()), leaves((const float*)_leaves.ptr()),
136       scales(_levels.cols / sizeof(Level))
137     {}
138
139     const Level*  levels;
140     const float*  stages;
141
142     const Node*   nodes;
143     const float*  leaves;
144
145     int scales;
146
147     void operator()(const cv::cuda::PtrStepSzb& roi, const cv::cuda::PtrStepSzi& hogluv, cv::cuda::PtrStepSz<uchar4> objects,
148         const int downscales, const cudaStream_t& stream = 0) const;
149
150     template<bool isUp>
151     __device_inline__ void detect(Detection* objects, const unsigned int ndetections, unsigned int* ctr, const int downscales) const;
152 };
153
154 }}}
155
156 #endif