optical_flow_farneback.cl 15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// @Authors
//    Sen Liu, swjtuls1987@126.com
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
Andrey Pavlenko's avatar
Andrey Pavlenko committed
28
//     and/or other materials provided with the distribution.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors as is and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/


47
#define tx  (int)get_local_id(0)
48 49
#define ty  get_local_id(1)
#define bx  get_group_id(0)
50
#define bdx (int)get_local_size(0)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

#define BORDER_SIZE 5
#define MAX_KSIZE_HALF 100

#ifndef polyN
#define polyN 5
#endif

__kernel void polynomialExpansion(__global float * dst,
                                  __global __const float * src,
                                  __global __const float * c_g,
                                  __global __const float * c_xg,
                                  __global __const float * c_xxg,
                                  __local float * smem,
                                  const float4 ig,
                                  const int height, const int width,
                                  int dstStep, int srcStep)
{
    const int y = get_global_id(1);
    const int x = bx * (bdx - 2*polyN) + tx - polyN;

    dstStep /= sizeof(*dst);
    srcStep /= sizeof(*src);
peng xiao's avatar
peng xiao committed
74

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    int xWarped;
    __local float *row = smem + tx;

    if (y < height && y >= 0)
    {
        xWarped = min(max(x, 0), width - 1);

        row[0] = src[mad24(y, srcStep, xWarped)] * c_g[0];
        row[bdx] = 0.f;
        row[2*bdx] = 0.f;

#pragma unroll
        for (int k = 1; k <= polyN; ++k)
        {
            float t0 = src[mad24(max(y - k, 0), srcStep, xWarped)];
            float t1 = src[mad24(min(y + k, height - 1), srcStep, xWarped)];

            row[0] += c_g[k] * (t0 + t1);
            row[bdx] += c_xg[k] * (t1 - t0);
            row[2*bdx] += c_xxg[k] * (t0 + t1);
        }
    }

    barrier(CLK_LOCAL_MEM_FENCE);

    if (y < height && y >= 0 && tx >= polyN && tx + polyN < bdx && x < width)
    {
        float b1 = c_g[0] * row[0];
        float b3 = c_g[0] * row[bdx];
        float b5 = c_g[0] * row[2*bdx];
        float b2 = 0, b4 = 0, b6 = 0;

#pragma unroll
        for (int k = 1; k <= polyN; ++k)
        {
            b1 += (row[k] + row[-k]) * c_g[k];
            b4 += (row[k] + row[-k]) * c_xxg[k];
            b2 += (row[k] - row[-k]) * c_xg[k];
            b3 += (row[k + bdx] + row[-k + bdx]) * c_g[k];
            b6 += (row[k + bdx] - row[-k + bdx]) * c_xg[k];
            b5 += (row[k + 2*bdx] + row[-k + 2*bdx]) * c_g[k];
        }

        dst[mad24(y, dstStep, xWarped)] = b3*ig.s0;
        dst[mad24(height + y, dstStep, xWarped)] = b2*ig.s0;
        dst[mad24(2*height + y, dstStep, xWarped)] = b1*ig.s1 + b5*ig.s2;
        dst[mad24(3*height + y, dstStep, xWarped)] = b1*ig.s1 + b4*ig.s2;
        dst[mad24(4*height + y, dstStep, xWarped)] = b6*ig.s3;
    }
}

inline int idx_row_low(const int y, const int last_row)
{
    return abs(y) % (last_row + 1);
}

inline int idx_row_high(const int y, const int last_row)
{
    return abs(last_row - abs(last_row - y)) % (last_row + 1);
}

inline int idx_row(const int y, const int last_row)
{
    return idx_row_low(idx_row_high(y, last_row), last_row);
}

inline int idx_col_low(const int x, const int last_col)
{
    return abs(x) % (last_col + 1);
}

inline int idx_col_high(const int x, const int last_col)
{
    return abs(last_col - abs(last_col - x)) % (last_col + 1);
}

inline int idx_col(const int x, const int last_col)
{
    return idx_col_low(idx_col_high(x, last_col), last_col);
}

__kernel void gaussianBlur(__global float * dst,
                           __global const float * src,
                           __global const float * c_gKer,
                           __local float * smem,
                           const int height,  const int width,
                           int dstStep, int srcStep,
                           const int ksizeHalf)
{
    const int y = get_global_id(1);
    const int x = get_global_id(0);

    dstStep /= sizeof(*dst);
    srcStep /= sizeof(*src);

    __local float *row = smem + ty * (bdx + 2*ksizeHalf);
peng xiao's avatar
peng xiao committed
171

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    if (y < height)
    {
        // Vertical pass
        for (int i = tx; i < bdx + 2*ksizeHalf; i += bdx)
        {
            int xExt = (int)(bx * bdx) + i - ksizeHalf;
            xExt = idx_col(xExt, width - 1);
            row[i] = src[mad24(y, srcStep, xExt)] * c_gKer[0];
            for (int j = 1; j <= ksizeHalf; ++j)
                row[i] += (src[mad24(idx_row_low(y - j, height - 1), srcStep, xExt)]
                           + src[mad24(idx_row_high(y + j, height - 1), srcStep, xExt)]) * c_gKer[j];
        }
    }

    barrier(CLK_LOCAL_MEM_FENCE);
peng xiao's avatar
peng xiao committed
187

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    if (y < height && y >= 0 && x < width && x >= 0)
    {
        // Horizontal pass
        row += tx + ksizeHalf;
        float res = row[0] * c_gKer[0];
        for (int i = 1; i <= ksizeHalf; ++i)
            res += (row[-i] + row[i]) * c_gKer[i];

        dst[mad24(y, dstStep, x)] = res;
    }
}

__constant float c_border[BORDER_SIZE + 1] = { 0.14f, 0.14f, 0.4472f, 0.4472f, 0.4472f, 1.f };

__kernel void updateMatrices(__global float * M,
                             __global const float * flowx, __global const float * flowy,
                             __global const float * R0, __global const float * R1,
                             const int height, const int width,
                             int mStep, int xStep,  int yStep, int R0Step, int R1Step)
{
    const int y = get_global_id(1);
    const int x = get_global_id(0);
peng xiao's avatar
peng xiao committed
210

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    mStep /= sizeof(*M);
    xStep /= sizeof(*flowx);
    yStep /= sizeof(*flowy);
    R0Step /= sizeof(*R0);
    R1Step /= sizeof(*R1);

    if (y < height && y >= 0 && x < width && x >= 0)
    {
        float dx = flowx[mad24(y, xStep, x)];
        float dy = flowy[mad24(y, yStep, x)];
        float fx = x + dx;
        float fy = y + dy;

        int x1 = convert_int(floor(fx));
        int y1 = convert_int(floor(fy));
peng xiao's avatar
peng xiao committed
226 227
        fx -= x1;
        fy -= y1;
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

        float r2, r3, r4, r5, r6;

        if (x1 >= 0 && y1 >= 0 && x1 < width - 1 && y1 < height - 1)
        {
            float a00 = (1.f - fx) * (1.f - fy);
            float a01 = fx * (1.f - fy);
            float a10 = (1.f - fx) * fy;
            float a11 = fx * fy;

            r2 = a00 * R1[mad24(y1, R1Step, x1)] +
                 a01 * R1[mad24(y1, R1Step, x1 + 1)] +
                 a10 * R1[mad24(y1 + 1, R1Step, x1)] +
                 a11 * R1[mad24(y1 + 1, R1Step, x1 + 1)];

            r3 = a00 * R1[mad24(height + y1, R1Step, x1)] +
                 a01 * R1[mad24(height + y1, R1Step, x1 + 1)] +
                 a10 * R1[mad24(height + y1 + 1, R1Step, x1)] +
                 a11 * R1[mad24(height + y1 + 1, R1Step, x1 + 1)];

            r4 = a00 * R1[mad24(2*height + y1, R1Step, x1)] +
                 a01 * R1[mad24(2*height + y1, R1Step, x1 + 1)] +
                 a10 * R1[mad24(2*height + y1 + 1, R1Step, x1)] +
                 a11 * R1[mad24(2*height + y1 + 1, R1Step, x1 + 1)];

            r5 = a00 * R1[mad24(3*height + y1, R1Step, x1)] +
                 a01 * R1[mad24(3*height + y1, R1Step, x1 + 1)] +
                 a10 * R1[mad24(3*height + y1 + 1, R1Step, x1)] +
                 a11 * R1[mad24(3*height + y1 + 1, R1Step, x1 + 1)];

            r6 = a00 * R1[mad24(4*height + y1, R1Step, x1)] +
                 a01 * R1[mad24(4*height + y1, R1Step, x1 + 1)] +
                 a10 * R1[mad24(4*height + y1 + 1, R1Step, x1)] +
                 a11 * R1[mad24(4*height + y1 + 1, R1Step, x1 + 1)];

            r4 = (R0[mad24(2*height + y, R0Step, x)] + r4) * 0.5f;
            r5 = (R0[mad24(3*height + y, R0Step, x)] + r5) * 0.5f;
            r6 = (R0[mad24(4*height + y, R0Step, x)] + r6) * 0.25f;
        }
        else
        {
            r2 = r3 = 0.f;
            r4 = R0[mad24(2*height + y, R0Step, x)];
            r5 = R0[mad24(3*height + y, R0Step, x)];
            r6 = R0[mad24(4*height + y, R0Step, x)] * 0.5f;
        }

        r2 = (R0[mad24(y, R0Step, x)] - r2) * 0.5f;
        r3 = (R0[mad24(height + y, R0Step, x)] - r3) * 0.5f;

        r2 += r4*dy + r6*dx;
        r3 += r6*dy + r5*dx;

        float scale =
peng xiao's avatar
peng xiao committed
282 283 284 285
            c_border[min(x, BORDER_SIZE)] *
            c_border[min(y, BORDER_SIZE)] *
            c_border[min(width - x - 1, BORDER_SIZE)] *
            c_border[min(height - y - 1, BORDER_SIZE)];
286

peng xiao's avatar
peng xiao committed
287 288 289 290 291
        r2 *= scale;
        r3 *= scale;
        r4 *= scale;
        r5 *= scale;
        r6 *= scale;
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309

        M[mad24(y, mStep, x)] = r4*r4 + r6*r6;
        M[mad24(height + y, mStep, x)] = (r4 + r5)*r6;
        M[mad24(2*height + y, mStep, x)] = r5*r5 + r6*r6;
        M[mad24(3*height + y, mStep, x)] = r4*r2 + r6*r3;
        M[mad24(4*height + y, mStep, x)] = r6*r2 + r5*r3;
    }
}

__kernel void boxFilter5(__global float * dst,
                         __global const float * src,
                         __local float * smem,
                         const int height,  const int width,
                         int dstStep, int srcStep,
                         const int ksizeHalf)
{
    const int y = get_global_id(1);
    const int x = get_global_id(0);
peng xiao's avatar
peng xiao committed
310

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    const float boxAreaInv = 1.f / ((1 + 2*ksizeHalf) * (1 + 2*ksizeHalf));
    const int smw = bdx + 2*ksizeHalf; // shared memory "width"
    __local float *row = smem + 5 * ty * smw;

    dstStep /= sizeof(*dst);
    srcStep /= sizeof(*src);

    if (y < height)
    {
        // Vertical pass
        for (int i = tx; i < bdx + 2*ksizeHalf; i += bdx)
        {
            int xExt = (int)(bx * bdx) + i - ksizeHalf;
            xExt = min(max(xExt, 0), width - 1);

peng xiao's avatar
peng xiao committed
326
#pragma unroll
327 328 329 330
            for (int k = 0; k < 5; ++k)
                row[k*smw + i] = src[mad24(k*height + y, srcStep, xExt)];

            for (int j = 1; j <= ksizeHalf; ++j)
peng xiao's avatar
peng xiao committed
331
#pragma unroll
332 333
                for (int k = 0; k < 5; ++k)
                    row[k*smw + i] +=
peng xiao's avatar
peng xiao committed
334 335
                        src[mad24(k*height + max(y - j, 0), srcStep, xExt)] +
                        src[mad24(k*height + min(y + j, height - 1), srcStep, xExt)];
336 337 338 339 340 341 342 343 344 345 346 347
        }
    }

    barrier(CLK_LOCAL_MEM_FENCE);

    if (y < height && y >= 0 && x < width && x >= 0)
    {
        // Horizontal pass

        row += tx + ksizeHalf;
        float res[5];

peng xiao's avatar
peng xiao committed
348
#pragma unroll
349 350 351 352
        for (int k = 0; k < 5; ++k)
            res[k] = row[k*smw];

        for (int i = 1; i <= ksizeHalf; ++i)
peng xiao's avatar
peng xiao committed
353
#pragma unroll
354 355 356
            for (int k = 0; k < 5; ++k)
                res[k] += row[k*smw - i] + row[k*smw + i];

peng xiao's avatar
peng xiao committed
357
#pragma unroll
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
        for (int k = 0; k < 5; ++k)
            dst[mad24(k*height + y, dstStep, x)] = res[k] * boxAreaInv;
    }
}

__kernel void updateFlow(__global float4 * flowx, __global float4 * flowy,
                         __global const float4 * M,
                         const int height, const int width,
                         int xStep, int yStep, int mStep)
{
    const int y = get_global_id(1);
    const int x = get_global_id(0);

    xStep /= sizeof(*flowx);
    yStep /= sizeof(*flowy);
    mStep /= sizeof(*M);

    if (y < height && y >= 0 && x < width && x >= 0)
    {
        float4 g11 = M[mad24(y, mStep, x)];
        float4 g12 = M[mad24(height + y, mStep, x)];
peng xiao's avatar
peng xiao committed
379
        float4 g22 = M[mad24(2*height + y, mStep, x)];
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
        float4 h1 =  M[mad24(3*height + y, mStep, x)];
        float4 h2 =  M[mad24(4*height + y, mStep, x)];

        float4 detInv = (float4)(1.f) / (g11*g22 - g12*g12 + (float4)(1e-3f));

        flowx[mad24(y, xStep, x)] = (g11*h2 - g12*h1) * detInv;
        flowy[mad24(y, yStep, x)] = (g22*h1 - g12*h2) * detInv;
    }
}

__kernel void gaussianBlur5(__global float * dst,
                            __global const float * src,
                            __global const float * c_gKer,
                            __local float * smem,
                            const int height,  const int width,
                            int dstStep, int srcStep,
                            const int ksizeHalf)
{
    const int y = get_global_id(1);
    const int x = get_global_id(0);

    const int smw = bdx + 2*ksizeHalf; // shared memory "width"
    __local volatile float *row = smem + 5 * ty * smw;

    dstStep /= sizeof(*dst);
    srcStep /= sizeof(*src);

    if (y < height)
    {
        // Vertical pass
        for (int i = tx; i < bdx + 2*ksizeHalf; i += bdx)
        {
            int xExt = (int)(bx * bdx) + i - ksizeHalf;
            xExt = idx_col(xExt, width - 1);

peng xiao's avatar
peng xiao committed
415
#pragma unroll
416 417 418 419
            for (int k = 0; k < 5; ++k)
                row[k*smw + i] = src[mad24(k*height + y, srcStep, xExt)] * c_gKer[0];

            for (int j = 1; j <= ksizeHalf; ++j)
peng xiao's avatar
peng xiao committed
420
#pragma unroll
421 422
                for (int k = 0; k < 5; ++k)
                    row[k*smw + i] +=
peng xiao's avatar
peng xiao committed
423 424
                        (src[mad24(k*height + idx_row_low(y - j, height - 1), srcStep, xExt)] +
                         src[mad24(k*height + idx_row_high(y + j, height - 1), srcStep, xExt)]) * c_gKer[j];
425 426 427 428 429 430 431 432 433 434 435 436
        }
    }

    barrier(CLK_LOCAL_MEM_FENCE);

    if (y < height && y >= 0 && x < width && x >= 0)
    {
        // Horizontal pass

        row += tx + ksizeHalf;
        float res[5];

peng xiao's avatar
peng xiao committed
437
#pragma unroll
438 439 440 441
        for (int k = 0; k < 5; ++k)
            res[k] = row[k*smw] * c_gKer[0];

        for (int i = 1; i <= ksizeHalf; ++i)
peng xiao's avatar
peng xiao committed
442
#pragma unroll
443 444 445
            for (int k = 0; k < 5; ++k)
                res[k] += (row[k*smw - i] + row[k*smw + i]) * c_gKer[i];

peng xiao's avatar
peng xiao committed
446
#pragma unroll
447 448 449 450
        for (int k = 0; k < 5; ++k)
            dst[mad24(k*height + y, dstStep, x)] = res[k];
    }
}