ioctl VIDIOC_EXPBUF

&manvol;

VIDIOC_EXPBUF

Export a buffer as a DMABUF file descriptor.

    	int ioctl
    	int fd
    	int request
    	struct v4l2_exportbuffer *argp

## Arguments

`fd`

- &fd;

`request`

- VIDIOC_EXPBUF

`argp`

## Description

### Experimental

This is an experimental interface and may change in the future.

This ioctl is an extension to the memory mapping I/O method therefore it is available only for `V4L2_MEMORY_MMAP` buffers. It can be used to export a buffer as DMABUF file at any time after buffers have been allocated with the &VIDIOC-REQBUFS; ioctl.

Prior to exporting an application calls VIDIOC_QUERYBUF to obtain memory offsets. When using the multi-planar API every plane has own offset.

To export a buffer, the application fills &v4l2-exportbuffer;. The mem_offset field is set to the offset obtained from `VIDIOC_QUERYBUF` . Additional flags may be posted in the flags field. Refer to manual for open syscall for details. Currently only O_CLOEXEC is guaranteed to be supported. All other fields must be set to zero. In a case of multi-planar API, every plane is exported separately using multiple `VIDIOC_EXPBUF` calls.

After calling `VIDIOC_EXPBUF` the fd field will be set by a driver. This is a DMABUF file descriptor. The application may pass it to other API. Refer to DMABUF importing for details about importing DMABUF files into V4L2 nodes. A developer is encouraged to close a DMABUF file when it is no longer used.

### Examples

#### Exporting a buffer.

    int buffer_export(int v4lfd, &v4l2-buf-type; bt, int index, int *dmafd)
    {
    	&v4l2-buffer; buf;
    	&v4l2-exportbuffer; expbuf;

    	memset(&buf, 0, sizeof buf);
    	buf.type = bt;
    	buf.memory = V4L2_MEMORY_MMAP;
    	buf.index = index;

    	if (ioctl (v4lfd, &VIDIOC-QUERYBUF;, &buf) == -1) {
    		perror ("VIDIOC_QUERYBUF");
    		return -1;
    	}

    	memset(&expbuf, 0, sizeof expbuf);
    	expbuf.mem_offset = buf.m.offset;
    	if (ioctl (v4lfd, &VIDIOC-EXPBUF;, &expbuf) == -1) {
    		perror ("VIDIOC_EXPBUF");
    		return -1;
    	}

    	*dmafd = expbuf.fd;

    	return 0;
    }

#### Exporting a buffer using multi plane API.

    int buffer_export_mp(int v4lfd, &v4l2-buf-type; bt, int index,
    	int dmafd[], int n_planes)
    {
    	&v4l2-buffer; buf;
    	&v4l2-plane; planes[VIDEO_MAX_PLANES];
    	int i;

    	memset(&buf, 0, sizeof buf);
    	buf.type = bt;
    	buf.memory = V4L2_MEMORY_MMAP;
    	buf.index = index;
    	buf.m.planes = planes;
    	buf.length = n_planes;
    	memset(&planes, 0, sizeof planes);

    	if (ioctl (v4lfd, &VIDIOC-QUERYBUF;, &buf) == -1) {
    		perror ("VIDIOC_QUERYBUF");
    		return -1;
    	}

    	for (i = 0; i < n_planes; ++i) {
    		&v4l2-exportbuffer; expbuf;

    		memset(&expbuf, 0, sizeof expbuf);
    		expbuf.mem_offset = plane[i].m.offset;
    		if (ioctl (v4lfd, &VIDIOC-EXPBUF;, &expbuf) == -1) {
    			perror ("VIDIOC_EXPBUF");
    			while (i)
    				close(dmafd[--i]);
    			return -1;
    		}
    		dmafd[i] = expbuf.fd;
    	}

    	return 0;
    }

## struct v4l2_exportbuffer

&cs-str;
__u32 | fd | The DMABUF file descriptor associated with a buffer. Set by a driver.
__u32 | reserved0 | Reserved field for future use. Must be set to zero.
__u32 | mem_offset | Buffer memory offset as returned by `VIDIOC_QUERYBUF` in &v4l2-buffer; ::m.offset (for single-plane formats) or &v4l2-plane; ::m.offset (for multi-planar formats)
__u32 | flags | Flags for newly created file, currently only `O_CLOEXEC` is supported, refer to manual of open syscall for more details.
__u32 | reserved[12] | Reserved field for future use. Must be set to zero.

&return-value;

EINVAL

- A queue is not in MMAP mode or DMABUF exporting is not supported or flag or mem_offset fields are invalid.