# Zero-Copy v4l2 capture

**URL:** <https://forums.imgtec.com/t/zero-copy-v4l2-capture/3278>\
**Category:** PowerVR Insider\
**Created:** [September 13, 2020, 7:37am UTC](https://forums.imgtec.com/t/zero-copy-v4l2-capture/3278 "2020-09-13T07:37:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![supreeth](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@supreeth](https://forums.imgtec.com/u/supreeth)\
**Post date:** [September 13, 2020, 7:37am UTC](https://forums.imgtec.com/t/zero-copy-v4l2-capture/3278/1 "2020-09-13T07:37:55Z")

</div>

I am building an application which needs to capture an image from 4 different cameras, process image using OpenCV build with OpenCL and stitches 4 Images at the end.

I am new to this stream, I was able to build an application using v4l2 to capture Images. I am creating cv::UMat using OpenCL Buffer Created with clCreateBuffer. But transfer captured frame form v4l2 Buffer to OpenCL Buffer, I am using memcpy which is consuming more time (~15ms using OpenMP for-loop for 4 Images).

Code Snippet for memcpy operation  
cl\_mem newBuffer[4] ;  
unsigned char \*cDataIn[4];  
cv::UMat UmatFullRes[4];

/\* Init (only once) _/  
for (int j = 0; j \< 4; j++) {  
newBuffer[j] = clCreateBuffer(context, CL\_MEM\_READ\_WRITE, (1280_800_4), NULL, NULL);  
cDataIn[j] = (unsigned char_)clEnqueueMapBuffer(cmdq[j], newBuffer[j],CL\_FALSE, CL\_MAP\_WRITE | CL\_MAP\_READ, 0, (1280_800_4), 0,NULL, NULL, NULL);  
}  
for (int j = 0; j \< 4; j++) {  
memcpy(cDataIn[j], buf[j], (1280 \* 800 \* 4));  
cv::ocl::convertFromBuffer(newBuffer[j], 1280 \* 4, 800, 1280, CV\_8UC4, UmatFullRes[j]);  
}  
In the above code snippet buf[j] is the unsigned char buffer returned from captureFrame function in the capture.cpp file.

I am trying to implement the capture process with Zero-Copy (VIDIOC\_EXPBUF) to OpenCL buffer to avoid memcpy operation.

I am searching for a person who could help me to understand “How to use VIDIOC\_EXPBUF in my case?”, and help me in building application.

---

<div class="post-metadata">

**Author:** ![David.Hamill](https://avatars.discourse-cdn.com/v4/letter/d/c57346/32.png) [@David.Hamill](https://forums.imgtec.com/u/David.Hamill)\
**Post date:** [September 15, 2020, 2:16pm UTC](https://forums.imgtec.com/t/zero-copy-v4l2-capture/3278/2 "2020-09-15T14:16:44Z")

</div>

Hi supreeth

PowerVR drivers unfortunately don’t support zero-copy buffer creation in OpenCL 1.2. If you specify CL\_MEM\_USE\_HOST\_PTR, then the copy will be delayed until the GPU needs to read the contents. The only way to avoid the copy is to allocate the host pointer using clSVMAlloc(), which is an OpenCL 2.0 feature.

Kind Regards,  
David
