~paparazzi-uav/paparazzi/v5.0-manual

« back to all changes in this revision

Viewing changes to sw/ext/opencv_bebop/opencv/modules/core/src/va_intel.cpp

  • Committer: Paparazzi buildbot
  • Date: 2016-05-18 15:00:29 UTC
  • Revision ID: felix.ruess+docbot@gmail.com-20160518150029-e8lgzi5kvb4p7un9
Manual import commit 4b8bbb730080dac23cf816b98908dacfabe2a8ec from v5.0 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// This file is part of OpenCV project.
 
2
// It is subject to the license terms in the LICENSE file found in the top-level directory
 
3
// of this distribution and at http://opencv.org/license.html.
 
4
 
 
5
// Copyright (C) 2015, Itseez, Inc., all rights reserved.
 
6
// Third party copyrights are property of their respective owners.
 
7
 
 
8
#include "precomp.hpp"
 
9
 
 
10
#ifdef HAVE_VA
 
11
#  include <va/va.h>
 
12
#else  // HAVE_VA
 
13
#  define NO_VA_SUPPORT_ERROR CV_ErrorNoReturn(cv::Error::StsBadFunc, "OpenCV was build without VA support (libva)")
 
14
#endif // HAVE_VA
 
15
 
 
16
using namespace cv;
 
17
 
 
18
////////////////////////////////////////////////////////////////////////
 
19
// CL-VA Interoperability
 
20
 
 
21
#ifdef HAVE_OPENCL
 
22
#  include "opencv2/core/opencl/runtime/opencl_core.hpp"
 
23
#  include "opencv2/core.hpp"
 
24
#  include "opencv2/core/ocl.hpp"
 
25
#  include "opencl_kernels_core.hpp"
 
26
#endif // HAVE_OPENCL
 
27
 
 
28
#if defined(HAVE_VA_INTEL) && defined(HAVE_OPENCL)
 
29
#  include <CL/va_ext.h>
 
30
#endif // HAVE_VA_INTEL && HAVE_OPENCL
 
31
 
 
32
namespace cv { namespace va_intel {
 
33
 
 
34
#if defined(HAVE_VA_INTEL) && defined(HAVE_OPENCL)
 
35
 
 
36
static clGetDeviceIDsFromVA_APIMediaAdapterINTEL_fn clGetDeviceIDsFromVA_APIMediaAdapterINTEL = NULL;
 
37
static clCreateFromVA_APIMediaSurfaceINTEL_fn       clCreateFromVA_APIMediaSurfaceINTEL       = NULL;
 
38
static clEnqueueAcquireVA_APIMediaSurfacesINTEL_fn  clEnqueueAcquireVA_APIMediaSurfacesINTEL  = NULL;
 
39
static clEnqueueReleaseVA_APIMediaSurfacesINTEL_fn  clEnqueueReleaseVA_APIMediaSurfacesINTEL  = NULL;
 
40
 
 
41
static bool contextInitialized = false;
 
42
 
 
43
#endif // HAVE_VA_INTEL && HAVE_OPENCL
 
44
 
 
45
namespace ocl {
 
46
 
 
47
Context& initializeContextFromVA(VADisplay display, bool tryInterop)
 
48
{
 
49
    (void)display; (void)tryInterop;
 
50
#if !defined(HAVE_VA)
 
51
    NO_VA_SUPPORT_ERROR;
 
52
#else  // !HAVE_VA
 
53
# if (defined(HAVE_VA_INTEL) && defined(HAVE_OPENCL))
 
54
    contextInitialized = false;
 
55
    if (tryInterop)
 
56
    {
 
57
        cl_uint numPlatforms;
 
58
        cl_int status = clGetPlatformIDs(0, NULL, &numPlatforms);
 
59
        if (status != CL_SUCCESS)
 
60
            CV_Error(cv::Error::OpenCLInitError, "OpenCL: Can't get number of platforms");
 
61
        if (numPlatforms == 0)
 
62
            CV_Error(cv::Error::OpenCLInitError, "OpenCL: No available platforms");
 
63
 
 
64
        std::vector<cl_platform_id> platforms(numPlatforms);
 
65
        status = clGetPlatformIDs(numPlatforms, &platforms[0], NULL);
 
66
        if (status != CL_SUCCESS)
 
67
            CV_Error(cv::Error::OpenCLInitError, "OpenCL: Can't get platform Id list");
 
68
 
 
69
        // For CL-VA interop, we must find platform/device with "cl_intel_va_api_media_sharing" extension.
 
70
        // With standard initialization procedure, we should examine platform extension string for that.
 
71
        // But in practice, the platform ext string doesn't contain it, while device ext string does.
 
72
        // Follow Intel procedure (see tutorial), we should obtain device IDs by extension call.
 
73
        // Note that we must obtain function pointers using specific platform ID, and can't provide pointers in advance.
 
74
        // So, we iterate and select the first platform, for which we got non-NULL pointers, device, and CL context.
 
75
 
 
76
        int found = -1;
 
77
        cl_context context = 0;
 
78
        cl_device_id device = 0;
 
79
 
 
80
        for (int i = 0; i < (int)numPlatforms; ++i)
 
81
        {
 
82
            // Get extension function pointers
 
83
 
 
84
            clGetDeviceIDsFromVA_APIMediaAdapterINTEL = (clGetDeviceIDsFromVA_APIMediaAdapterINTEL_fn)
 
85
                clGetExtensionFunctionAddressForPlatform(platforms[i], "clGetDeviceIDsFromVA_APIMediaAdapterINTEL");
 
86
            clCreateFromVA_APIMediaSurfaceINTEL       = (clCreateFromVA_APIMediaSurfaceINTEL_fn)
 
87
                clGetExtensionFunctionAddressForPlatform(platforms[i], "clCreateFromVA_APIMediaSurfaceINTEL");
 
88
            clEnqueueAcquireVA_APIMediaSurfacesINTEL  = (clEnqueueAcquireVA_APIMediaSurfacesINTEL_fn)
 
89
                clGetExtensionFunctionAddressForPlatform(platforms[i], "clEnqueueAcquireVA_APIMediaSurfacesINTEL");
 
90
            clEnqueueReleaseVA_APIMediaSurfacesINTEL  = (clEnqueueReleaseVA_APIMediaSurfacesINTEL_fn)
 
91
                clGetExtensionFunctionAddressForPlatform(platforms[i], "clEnqueueReleaseVA_APIMediaSurfacesINTEL");
 
92
 
 
93
            if (((void*)clGetDeviceIDsFromVA_APIMediaAdapterINTEL == NULL) ||
 
94
                ((void*)clCreateFromVA_APIMediaSurfaceINTEL == NULL) ||
 
95
                ((void*)clEnqueueAcquireVA_APIMediaSurfacesINTEL == NULL) ||
 
96
                ((void*)clEnqueueReleaseVA_APIMediaSurfacesINTEL == NULL))
 
97
            {
 
98
                continue;
 
99
            }
 
100
 
 
101
            // Query device list
 
102
 
 
103
            cl_uint numDevices = 0;
 
104
 
 
105
            status = clGetDeviceIDsFromVA_APIMediaAdapterINTEL(platforms[i], CL_VA_API_DISPLAY_INTEL, display,
 
106
                                                               CL_PREFERRED_DEVICES_FOR_VA_API_INTEL, 0, NULL, &numDevices);
 
107
            if ((status != CL_SUCCESS) || !(numDevices > 0))
 
108
                continue;
 
109
            numDevices = 1; // initializeContextFromHandle() expects only 1 device
 
110
            status = clGetDeviceIDsFromVA_APIMediaAdapterINTEL(platforms[i], CL_VA_API_DISPLAY_INTEL, display,
 
111
                                                               CL_PREFERRED_DEVICES_FOR_VA_API_INTEL, numDevices, &device, NULL);
 
112
            if (status != CL_SUCCESS)
 
113
                continue;
 
114
 
 
115
            // Creating CL-VA media sharing OpenCL context
 
116
 
 
117
            cl_context_properties props[] = {
 
118
                CL_CONTEXT_VA_API_DISPLAY_INTEL, (cl_context_properties) display,
 
119
                CL_CONTEXT_INTEROP_USER_SYNC, CL_FALSE, // no explicit sync required
 
120
                0
 
121
            };
 
122
 
 
123
            context = clCreateContext(props, numDevices, &device, NULL, NULL, &status);
 
124
            if (status != CL_SUCCESS)
 
125
            {
 
126
                clReleaseDevice(device);
 
127
            }
 
128
            else
 
129
            {
 
130
                found = i;
 
131
                break;
 
132
            }
 
133
        }
 
134
 
 
135
        if (found >= 0)
 
136
        {
 
137
            contextInitialized = true;
 
138
            Context& ctx = Context::getDefault(false);
 
139
            initializeContextFromHandle(ctx, platforms[found], context, device);
 
140
            return ctx;
 
141
        }
 
142
    }
 
143
# endif // HAVE_VA_INTEL && HAVE_OPENCL
 
144
    {
 
145
        Context& ctx = Context::getDefault(true);
 
146
        return ctx;
 
147
    }
 
148
#endif  // !HAVE_VA
 
149
}
 
150
 
 
151
#if defined(HAVE_VA_INTEL) && defined(HAVE_OPENCL)
 
152
static bool ocl_convert_nv12_to_bgr(cl_mem clImageY, cl_mem clImageUV, cl_mem clBuffer, int step, int cols, int rows)
 
153
{
 
154
    ocl::Kernel k;
 
155
    k.create("YUV2BGR_NV12_8u", cv::ocl::core::cvtclr_dx_oclsrc, "");
 
156
    if (k.empty())
 
157
        return false;
 
158
 
 
159
    k.args(clImageY, clImageUV, clBuffer, step, cols, rows);
 
160
 
 
161
    size_t globalsize[] = { (size_t)cols, (size_t)rows };
 
162
    return k.run(2, globalsize, 0, false);
 
163
}
 
164
 
 
165
static bool ocl_convert_bgr_to_nv12(cl_mem clBuffer, int step, int cols, int rows, cl_mem clImageY, cl_mem clImageUV)
 
166
{
 
167
    ocl::Kernel k;
 
168
    k.create("BGR2YUV_NV12_8u", cv::ocl::core::cvtclr_dx_oclsrc, "");
 
169
    if (k.empty())
 
170
        return false;
 
171
 
 
172
    k.args(clBuffer, step, cols, rows, clImageY, clImageUV);
 
173
 
 
174
    size_t globalsize[] = { (size_t)cols, (size_t)rows };
 
175
    return k.run(2, globalsize, 0, false);
 
176
}
 
177
#endif // HAVE_VA_INTEL && HAVE_OPENCL
 
178
 
 
179
} // namespace cv::va_intel::ocl
 
180
 
 
181
#if defined(HAVE_VA)
 
182
const int NCHANNELS = 3;
 
183
 
 
184
static void copy_convert_nv12_to_bgr(const VAImage& image, const unsigned char* buffer, Mat& bgr)
 
185
{
 
186
    const float d1 = 16.0f;
 
187
    const float d2 = 128.0f;
 
188
 
 
189
    static const float coeffs[5] =
 
190
        {
 
191
            1.163999557f,
 
192
            2.017999649f,
 
193
            -0.390999794f,
 
194
            -0.812999725f,
 
195
            1.5959997177f
 
196
        };
 
197
 
 
198
    const size_t srcOffsetY = image.offsets[0];
 
199
    const size_t srcOffsetUV = image.offsets[1];
 
200
 
 
201
    const size_t srcStepY = image.pitches[0];
 
202
    const size_t srcStepUV = image.pitches[1];
 
203
 
 
204
    const size_t dstStep = bgr.step;
 
205
 
 
206
    const unsigned char* srcY0 = buffer + srcOffsetY;
 
207
    const unsigned char* srcUV = buffer + srcOffsetUV;
 
208
 
 
209
    unsigned char* dst0 = bgr.data;
 
210
 
 
211
    for (int y = 0; y < bgr.rows; y += 2)
 
212
    {
 
213
        const unsigned char* srcY1 = srcY0 + srcStepY;
 
214
        unsigned char *dst1 = dst0 + dstStep;
 
215
 
 
216
        for (int x = 0; x < bgr.cols; x += 2)
 
217
        {
 
218
            float Y0 = float(srcY0[x+0]);
 
219
            float Y1 = float(srcY0[x+1]);
 
220
            float Y2 = float(srcY1[x+0]);
 
221
            float Y3 = float(srcY1[x+1]);
 
222
 
 
223
            float U = float(srcUV[2*(x/2)+0]) - d2;
 
224
            float V = float(srcUV[2*(x/2)+1]) - d2;
 
225
 
 
226
            Y0 = std::max(0.0f, Y0 - d1) * coeffs[0];
 
227
            Y1 = std::max(0.0f, Y1 - d1) * coeffs[0];
 
228
            Y2 = std::max(0.0f, Y2 - d1) * coeffs[0];
 
229
            Y3 = std::max(0.0f, Y3 - d1) * coeffs[0];
 
230
 
 
231
            float ruv = coeffs[4]*V;
 
232
            float guv = coeffs[3]*V + coeffs[2]*U;
 
233
            float buv = coeffs[1]*U;
 
234
 
 
235
            dst0[(x+0)*NCHANNELS+0] = saturate_cast<unsigned char>(Y0 + buv);
 
236
            dst0[(x+0)*NCHANNELS+1] = saturate_cast<unsigned char>(Y0 + guv);
 
237
            dst0[(x+0)*NCHANNELS+2] = saturate_cast<unsigned char>(Y0 + ruv);
 
238
 
 
239
            dst0[(x+1)*NCHANNELS+0] = saturate_cast<unsigned char>(Y1 + buv);
 
240
            dst0[(x+1)*NCHANNELS+1] = saturate_cast<unsigned char>(Y1 + guv);
 
241
            dst0[(x+1)*NCHANNELS+2] = saturate_cast<unsigned char>(Y1 + ruv);
 
242
 
 
243
            dst1[(x+0)*NCHANNELS+0] = saturate_cast<unsigned char>(Y2 + buv);
 
244
            dst1[(x+0)*NCHANNELS+1] = saturate_cast<unsigned char>(Y2 + guv);
 
245
            dst1[(x+0)*NCHANNELS+2] = saturate_cast<unsigned char>(Y2 + ruv);
 
246
 
 
247
            dst1[(x+1)*NCHANNELS+0] = saturate_cast<unsigned char>(Y3 + buv);
 
248
            dst1[(x+1)*NCHANNELS+1] = saturate_cast<unsigned char>(Y3 + guv);
 
249
            dst1[(x+1)*NCHANNELS+2] = saturate_cast<unsigned char>(Y3 + ruv);
 
250
        }
 
251
 
 
252
        srcY0 = srcY1 + srcStepY;
 
253
        srcUV += srcStepUV;
 
254
        dst0 = dst1 + dstStep;
 
255
    }
 
256
}
 
257
 
 
258
static void copy_convert_bgr_to_nv12(const VAImage& image, const Mat& bgr, unsigned char* buffer)
 
259
{
 
260
    const float d1 = 16.0f;
 
261
    const float d2 = 128.0f;
 
262
 
 
263
    static const float coeffs[8] =
 
264
        {
 
265
            0.256999969f,  0.50399971f,   0.09799957f,   -0.1479988098f,
 
266
            -0.2909994125f, 0.438999176f, -0.3679990768f, -0.0709991455f
 
267
        };
 
268
 
 
269
    const size_t dstOffsetY = image.offsets[0];
 
270
    const size_t dstOffsetUV = image.offsets[1];
 
271
 
 
272
    const size_t dstStepY = image.pitches[0];
 
273
    const size_t dstStepUV = image.pitches[1];
 
274
 
 
275
    const size_t srcStep = bgr.step;
 
276
 
 
277
    const unsigned char* src0 = bgr.data;
 
278
 
 
279
    unsigned char* dstY0 = buffer + dstOffsetY;
 
280
    unsigned char* dstUV = buffer + dstOffsetUV;
 
281
 
 
282
    for (int y = 0; y < bgr.rows; y += 2)
 
283
    {
 
284
        const unsigned char *src1 = src0 + srcStep;
 
285
        unsigned char* dstY1 = dstY0 + dstStepY;
 
286
 
 
287
        for (int x = 0; x < bgr.cols; x += 2)
 
288
        {
 
289
            float B0 = float(src0[(x+0)*NCHANNELS+0]);
 
290
            float G0 = float(src0[(x+0)*NCHANNELS+1]);
 
291
            float R0 = float(src0[(x+0)*NCHANNELS+2]);
 
292
 
 
293
            float B1 = float(src0[(x+1)*NCHANNELS+0]);
 
294
            float G1 = float(src0[(x+1)*NCHANNELS+1]);
 
295
            float R1 = float(src0[(x+1)*NCHANNELS+2]);
 
296
 
 
297
            float B2 = float(src1[(x+0)*NCHANNELS+0]);
 
298
            float G2 = float(src1[(x+0)*NCHANNELS+1]);
 
299
            float R2 = float(src1[(x+0)*NCHANNELS+2]);
 
300
 
 
301
            float B3 = float(src1[(x+1)*NCHANNELS+0]);
 
302
            float G3 = float(src1[(x+1)*NCHANNELS+1]);
 
303
            float R3 = float(src1[(x+1)*NCHANNELS+2]);
 
304
 
 
305
            float Y0 = coeffs[0]*R0 + coeffs[1]*G0 + coeffs[2]*B0 + d1;
 
306
            float Y1 = coeffs[0]*R1 + coeffs[1]*G1 + coeffs[2]*B1 + d1;
 
307
            float Y2 = coeffs[0]*R2 + coeffs[1]*G2 + coeffs[2]*B2 + d1;
 
308
            float Y3 = coeffs[0]*R3 + coeffs[1]*G3 + coeffs[2]*B3 + d1;
 
309
 
 
310
            float U = coeffs[3]*R0 + coeffs[4]*G0 + coeffs[5]*B0 + d2;
 
311
            float V = coeffs[5]*R0 + coeffs[6]*G0 + coeffs[7]*B0 + d2;
 
312
 
 
313
            dstY0[x+0] = saturate_cast<unsigned char>(Y0);
 
314
            dstY0[x+1] = saturate_cast<unsigned char>(Y1);
 
315
            dstY1[x+0] = saturate_cast<unsigned char>(Y2);
 
316
            dstY1[x+1] = saturate_cast<unsigned char>(Y3);
 
317
 
 
318
            dstUV[2*(x/2)+0] = saturate_cast<unsigned char>(U);
 
319
            dstUV[2*(x/2)+1] = saturate_cast<unsigned char>(V);
 
320
        }
 
321
 
 
322
        src0 = src1 + srcStep;
 
323
        dstY0 = dstY1 + dstStepY;
 
324
        dstUV += dstStepUV;
 
325
    }
 
326
}
 
327
#endif // HAVE_VA
 
328
 
 
329
void convertToVASurface(VADisplay display, InputArray src, VASurfaceID surface, Size size)
 
330
{
 
331
    (void)display; (void)src; (void)surface; (void)size;
 
332
#if !defined(HAVE_VA)
 
333
    NO_VA_SUPPORT_ERROR;
 
334
#else  // !HAVE_VA
 
335
    const int stype = CV_8UC3;
 
336
 
 
337
    int srcType = src.type();
 
338
    CV_Assert(srcType == stype);
 
339
 
 
340
    Size srcSize = src.size();
 
341
    CV_Assert(srcSize.width == size.width && srcSize.height == size.height);
 
342
 
 
343
# if (defined(HAVE_VA_INTEL) && defined(HAVE_OPENCL))
 
344
    if (contextInitialized)
 
345
    {
 
346
        UMat u = src.getUMat();
 
347
 
 
348
        // TODO Add support for roi
 
349
        CV_Assert(u.offset == 0);
 
350
        CV_Assert(u.isContinuous());
 
351
 
 
352
        cl_mem clBuffer = (cl_mem)u.handle(ACCESS_READ);
 
353
 
 
354
        using namespace cv::ocl;
 
355
        Context& ctx = Context::getDefault();
 
356
        cl_context context = (cl_context)ctx.ptr();
 
357
 
 
358
        cl_int status = 0;
 
359
 
 
360
        cl_mem clImageY = clCreateFromVA_APIMediaSurfaceINTEL(context, CL_MEM_WRITE_ONLY, &surface, 0, &status);
 
361
        if (status != CL_SUCCESS)
 
362
            CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clCreateFromVA_APIMediaSurfaceINTEL failed (Y plane)");
 
363
        cl_mem clImageUV = clCreateFromVA_APIMediaSurfaceINTEL(context, CL_MEM_WRITE_ONLY, &surface, 1, &status);
 
364
        if (status != CL_SUCCESS)
 
365
            CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clCreateFromVA_APIMediaSurfaceINTEL failed (UV plane)");
 
366
 
 
367
        cl_command_queue q = (cl_command_queue)Queue::getDefault().ptr();
 
368
 
 
369
        cl_mem images[2] = { clImageY, clImageUV };
 
370
        status = clEnqueueAcquireVA_APIMediaSurfacesINTEL(q, 2, images, 0, NULL, NULL);
 
371
        if (status != CL_SUCCESS)
 
372
            CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clEnqueueAcquireVA_APIMediaSurfacesINTEL failed");
 
373
        if (!ocl::ocl_convert_bgr_to_nv12(clBuffer, (int)u.step[0], u.cols, u.rows, clImageY, clImageUV))
 
374
            CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: ocl_convert_bgr_to_nv12 failed");
 
375
        clEnqueueReleaseVA_APIMediaSurfacesINTEL(q, 2, images, 0, NULL, NULL);
 
376
        if (status != CL_SUCCESS)
 
377
            CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clEnqueueReleaseVA_APIMediaSurfacesINTEL failed");
 
378
 
 
379
        status = clFinish(q); // TODO Use events
 
380
        if (status != CL_SUCCESS)
 
381
            CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clFinish failed");
 
382
 
 
383
        status = clReleaseMemObject(clImageY); // TODO RAII
 
384
        if (status != CL_SUCCESS)
 
385
            CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clReleaseMem failed (Y plane)");
 
386
        status = clReleaseMemObject(clImageUV);
 
387
        if (status != CL_SUCCESS)
 
388
            CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clReleaseMem failed (UV plane)");
 
389
    }
 
390
    else
 
391
# endif // HAVE_VA_INTEL && HAVE_OPENCL
 
392
    {
 
393
        Mat m = src.getMat();
 
394
 
 
395
        // TODO Add support for roi
 
396
        CV_Assert(m.data == m.datastart);
 
397
        CV_Assert(m.isContinuous());
 
398
 
 
399
        VAStatus status = 0;
 
400
 
 
401
        status = vaSyncSurface(display, surface);
 
402
        if (status != VA_STATUS_SUCCESS)
 
403
            CV_Error(cv::Error::StsError, "VA-API: vaSyncSurface failed");
 
404
 
 
405
        VAImage image;
 
406
        status = vaDeriveImage(display, surface, &image);
 
407
        if (status != VA_STATUS_SUCCESS)
 
408
            CV_Error(cv::Error::StsError, "VA-API: vaDeriveImage failed");
 
409
 
 
410
        unsigned char* buffer = 0;
 
411
        status = vaMapBuffer(display, image.buf, (void **)&buffer);
 
412
        if (status != VA_STATUS_SUCCESS)
 
413
            CV_Error(cv::Error::StsError, "VA-API: vaMapBuffer failed");
 
414
 
 
415
        CV_Assert(image.format.fourcc == VA_FOURCC_NV12);
 
416
 
 
417
        copy_convert_bgr_to_nv12(image, m, buffer);
 
418
 
 
419
        status = vaUnmapBuffer(display, image.buf);
 
420
        if (status != VA_STATUS_SUCCESS)
 
421
            CV_Error(cv::Error::StsError, "VA-API: vaUnmapBuffer failed");
 
422
 
 
423
        status = vaDestroyImage(display, image.image_id);
 
424
        if (status != VA_STATUS_SUCCESS)
 
425
            CV_Error(cv::Error::StsError, "VA-API: vaDestroyImage failed");
 
426
    }
 
427
#endif  // !HAVE_VA
 
428
}
 
429
 
 
430
void convertFromVASurface(VADisplay display, VASurfaceID surface, Size size, OutputArray dst)
 
431
{
 
432
    (void)display; (void)surface; (void)dst; (void)size;
 
433
#if !defined(HAVE_VA)
 
434
    NO_VA_SUPPORT_ERROR;
 
435
#else  // !HAVE_VA
 
436
    const int dtype = CV_8UC3;
 
437
 
 
438
    // TODO Need to specify ACCESS_WRITE here somehow to prevent useless data copying!
 
439
    dst.create(size, dtype);
 
440
 
 
441
# if (defined(HAVE_VA_INTEL) && defined(HAVE_OPENCL))
 
442
    if (contextInitialized)
 
443
    {
 
444
        UMat u = dst.getUMat();
 
445
 
 
446
        // TODO Add support for roi
 
447
        CV_Assert(u.offset == 0);
 
448
        CV_Assert(u.isContinuous());
 
449
 
 
450
        cl_mem clBuffer = (cl_mem)u.handle(ACCESS_WRITE);
 
451
 
 
452
        using namespace cv::ocl;
 
453
        Context& ctx = Context::getDefault();
 
454
        cl_context context = (cl_context)ctx.ptr();
 
455
 
 
456
        cl_int status = 0;
 
457
 
 
458
        cl_mem clImageY = clCreateFromVA_APIMediaSurfaceINTEL(context, CL_MEM_READ_ONLY, &surface, 0, &status);
 
459
        if (status != CL_SUCCESS)
 
460
            CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clCreateFromVA_APIMediaSurfaceINTEL failed (Y plane)");
 
461
        cl_mem clImageUV = clCreateFromVA_APIMediaSurfaceINTEL(context, CL_MEM_READ_ONLY, &surface, 1, &status);
 
462
        if (status != CL_SUCCESS)
 
463
            CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clCreateFromVA_APIMediaSurfaceINTEL failed (UV plane)");
 
464
 
 
465
        cl_command_queue q = (cl_command_queue)Queue::getDefault().ptr();
 
466
 
 
467
        cl_mem images[2] = { clImageY, clImageUV };
 
468
        status = clEnqueueAcquireVA_APIMediaSurfacesINTEL(q, 2, images, 0, NULL, NULL);
 
469
        if (status != CL_SUCCESS)
 
470
            CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clEnqueueAcquireVA_APIMediaSurfacesINTEL failed");
 
471
        if (!ocl::ocl_convert_nv12_to_bgr(clImageY, clImageUV, clBuffer, (int)u.step[0], u.cols, u.rows))
 
472
            CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: ocl_convert_nv12_to_bgr failed");
 
473
        status = clEnqueueReleaseVA_APIMediaSurfacesINTEL(q, 2, images, 0, NULL, NULL);
 
474
        if (status != CL_SUCCESS)
 
475
            CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clEnqueueReleaseVA_APIMediaSurfacesINTEL failed");
 
476
 
 
477
        status = clFinish(q); // TODO Use events
 
478
        if (status != CL_SUCCESS)
 
479
            CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clFinish failed");
 
480
 
 
481
        status = clReleaseMemObject(clImageY); // TODO RAII
 
482
        if (status != CL_SUCCESS)
 
483
            CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clReleaseMem failed (Y plane)");
 
484
        status = clReleaseMemObject(clImageUV);
 
485
        if (status != CL_SUCCESS)
 
486
            CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clReleaseMem failed (UV plane)");
 
487
    }
 
488
    else
 
489
# endif // HAVE_VA_INTEL && HAVE_OPENCL
 
490
    {
 
491
        Mat m = dst.getMat();
 
492
 
 
493
        // TODO Add support for roi
 
494
        CV_Assert(m.data == m.datastart);
 
495
        CV_Assert(m.isContinuous());
 
496
 
 
497
        VAStatus status = 0;
 
498
 
 
499
        status = vaSyncSurface(display, surface);
 
500
        if (status != VA_STATUS_SUCCESS)
 
501
            CV_Error(cv::Error::StsError, "VA-API: vaSyncSurface failed");
 
502
 
 
503
        VAImage image;
 
504
        status = vaDeriveImage(display, surface, &image);
 
505
        if (status != VA_STATUS_SUCCESS)
 
506
            CV_Error(cv::Error::StsError, "VA-API: vaDeriveImage failed");
 
507
 
 
508
        unsigned char* buffer = 0;
 
509
        status = vaMapBuffer(display, image.buf, (void **)&buffer);
 
510
        if (status != VA_STATUS_SUCCESS)
 
511
            CV_Error(cv::Error::StsError, "VA-API: vaMapBuffer failed");
 
512
 
 
513
        CV_Assert(image.format.fourcc == VA_FOURCC_NV12);
 
514
 
 
515
        copy_convert_nv12_to_bgr(image, buffer, m);
 
516
 
 
517
        status = vaUnmapBuffer(display, image.buf);
 
518
        if (status != VA_STATUS_SUCCESS)
 
519
            CV_Error(cv::Error::StsError, "VA-API: vaUnmapBuffer failed");
 
520
 
 
521
        status = vaDestroyImage(display, image.image_id);
 
522
        if (status != VA_STATUS_SUCCESS)
 
523
            CV_Error(cv::Error::StsError, "VA-API: vaDestroyImage failed");
 
524
    }
 
525
#endif  // !HAVE_VA
 
526
}
 
527
 
 
528
}} // namespace cv::va_intel