요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.
1. 요약·해설
원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.
2. 영어 원문 전체
번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.
원문 전체 펼치기
.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
.. c:namespace:: V4L
.. _VIDIOC_EXPBUF:
*******************
ioctl VIDIOC_EXPBUF
*******************
Name
====
VIDIOC_EXPBUF - Export a buffer as a DMABUF file descriptor.
Synopsis
========
.. c:macro:: VIDIOC_EXPBUF
``int ioctl(int fd, VIDIOC_EXPBUF, struct v4l2_exportbuffer *argp)``
Arguments
=========
``fd``
File descriptor returned by :c:func:`open()`.
``argp``
Pointer to struct :c:type:`v4l2_exportbuffer`.
Description
===========
This ioctl is an extension to the :ref:`memory mapping <mmap>` I/O
method, therefore it is available only for ``V4L2_MEMORY_MMAP`` buffers.
It can be used to export a buffer as a DMABUF file at any time after
buffers have been allocated with the
:ref:`VIDIOC_REQBUFS` ioctl.
To export a buffer, applications fill struct
:c:type:`v4l2_exportbuffer`. The ``type`` field is
set to the same buffer type as was previously used with struct
:c:type:`v4l2_requestbuffers` ``type``.
Applications must also set the ``index`` field. Valid index numbers
range from zero to the number of buffers allocated with
:ref:`VIDIOC_REQBUFS` (struct
:c:type:`v4l2_requestbuffers` ``count``) minus
one. For the multi-planar API, applications set the ``plane`` field to
the index of the plane to be exported. Valid planes range from zero to
the maximal number of valid planes for the currently active format. For
the single-planar API, applications must set ``plane`` to zero.
Additional flags may be posted in the ``flags`` field. Refer to a manual
for open() for details. Currently only O_CLOEXEC, O_RDONLY, O_WRONLY,
and O_RDWR are supported. All other fields must be set to zero. In the
case of multi-planar API, every plane is exported separately using
multiple :ref:`VIDIOC_EXPBUF` calls.
After calling :ref:`VIDIOC_EXPBUF` the ``fd`` field will be set by a
driver. This is a DMABUF file descriptor. The application may pass it to
other DMABUF-aware devices. Refer to :ref:`DMABUF importing <dmabuf>`
for details about importing DMABUF files into V4L2 nodes. It is
recommended to close a DMABUF file when it is no longer used to allow
the associated memory to be reclaimed.
Examples
========
.. code-block:: c
int buffer_export(int v4lfd, enum v4l2_buf_type bt, int index, int *dmafd)
{
struct v4l2_exportbuffer expbuf;
memset(&expbuf, 0, sizeof(expbuf));
expbuf.type = bt;
expbuf.index = index;
if (ioctl(v4lfd, VIDIOC_EXPBUF, &expbuf) == -1) {
perror("VIDIOC_EXPBUF");
return -1;
}
*dmafd = expbuf.fd;
return 0;
}
.. code-block:: c
int buffer_export_mp(int v4lfd, enum v4l2_buf_type bt, int index,
int dmafd[], int n_planes)
{
int i;
for (i = 0; i < n_planes; ++i) {
struct v4l2_exportbuffer expbuf;
memset(&expbuf, 0, sizeof(expbuf));
expbuf.type = bt;
expbuf.index = index;
expbuf.plane = i;
if (ioctl(v4lfd, VIDIOC_EXPBUF, &expbuf) == -1) {
perror("VIDIOC_EXPBUF");
while (i)
close(dmafd[--i]);
return -1;
}
dmafd[i] = expbuf.fd;
}
return 0;
}
.. c:type:: v4l2_exportbuffer
.. tabularcolumns:: |p{4.4cm}|p{4.4cm}|p{8.5cm}|
.. flat-table:: struct v4l2_exportbuffer
:header-rows: 0
:stub-columns: 0
:widths: 1 1 2
* - __u32
- ``type``
- Type of the buffer, same as struct
:c:type:`v4l2_format` ``type`` or struct
:c:type:`v4l2_requestbuffers` ``type``, set
by the application. See :c:type:`v4l2_buf_type`
* - __u32
- ``index``
- Number of the buffer, set by the application. This field is only
used for :ref:`memory mapping <mmap>` I/O and can range from
zero to the number of buffers allocated with the
:ref:`VIDIOC_REQBUFS` and/or
:ref:`VIDIOC_CREATE_BUFS` ioctls.
* - __u32
- ``plane``
- Index of the plane to be exported when using the multi-planar API.
Otherwise this value must be set to zero.
* - __u32
- ``flags``
- Flags for the newly created file, currently only ``O_CLOEXEC``,
``O_RDONLY``, ``O_WRONLY``, and ``O_RDWR`` are supported, refer to
the manual of open() for more details.
* - __s32
- ``fd``
- The DMABUF file descriptor associated with a buffer. Set by the
driver.
* - __u32
- ``reserved[11]``
- Reserved field for future use. Drivers and applications must set
the array to zero.
Return Value
============
On success 0 is returned, on error -1 and the ``errno`` variable is set
appropriately. The generic error codes are described at the
:ref:`Generic Error Codes <gen-errors>` chapter.
EINVAL
A queue is not in MMAP mode or DMABUF exporting is not supported or
``flags`` or ``type`` or ``index`` or ``plane`` fields are invalid.
3. 한국어 전문 번역
영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.
목적, 호출 형식과 인자
1-30`VIDIOC_EXPBUF`는 V4L2 버퍼를 DMABUF 파일 디스크립터로 내보내는 ioctl입니다. 호출 형식은 `int ioctl(int fd, VIDIOC_EXPBUF, struct v4l2_exportbuffer *argp)`입니다.
`fd`는 `open()`이 반환한 V4L2 장치 파일 디스크립터이고, `argp`는 내보낼 버퍼·plane·파일 플래그와 반환되는 DMABUF fd를 담는 `struct v4l2_exportbuffer`를 가리킵니다.
.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
.. c:namespace:: V4L
.. _VIDIOC_EXPBUF:
*******************
ioctl VIDIOC_EXPBUF
*******************
Name
====
VIDIOC_EXPBUF - Export a buffer as a DMABUF file descriptor.
Synopsis
========
.. c:macro:: VIDIOC_EXPBUF
``int ioctl(int fd, VIDIOC_EXPBUF, struct v4l2_exportbuffer *argp)``
Arguments
=========
``fd``
File descriptor returned by :c:func:`open()`.
``argp``
Pointer to struct :c:type:`v4l2_exportbuffer`.
DMABUF 내보내기 절차와 제약
31-64이 ioctl은 memory mapping I/O 방식의 확장이므로 `V4L2_MEMORY_MMAP` 버퍼에만 사용할 수 있습니다. `VIDIOC_REQBUFS`로 버퍼를 할당한 뒤에는 언제든 해당 버퍼를 DMABUF 파일로 내보낼 수 있습니다.
응용 프로그램은 `v4l2_exportbuffer.type`을 앞서 `v4l2_requestbuffers.type`에 사용한 것과 같은 buffer type으로 설정합니다. `index`는 0부터 `VIDIOC_REQBUFS`가 반환한 `count - 1`까지의 할당된 버퍼 번호입니다.
multi-planar API에서는 현재 활성 format이 허용하는 plane 범위 안에서 내보낼 `plane` index를 설정합니다. 각 plane은 별도의 `VIDIOC_EXPBUF` 호출로 하나씩 내보내야 합니다.
single-planar API에서는 `plane`을 반드시 0으로 설정합니다. 이 차이를 무시하면 `EINVAL`이 반환됩니다.
`flags`에는 새 DMABUF 파일에 적용할 `O_CLOEXEC`, `O_RDONLY`, `O_WRONLY`, `O_RDWR`만 사용할 수 있습니다. 자세한 의미는 `open()` 매뉴얼을 따르며, 그 밖의 필드는 모두 0으로 초기화해야 합니다.
호출이 성공하면 드라이버가 `fd` 필드에 DMABUF 파일 디스크립터를 기록합니다. 응용 프로그램은 이를 다른 DMABUF 인식 장치에 전달하거나 V4L2 DMABUF importing 절차에 사용할 수 있습니다.
DMABUF 파일을 더 이상 사용하지 않을 때는 연결된 메모리를 회수할 수 있도록 fd를 닫는 것이 권장됩니다.
MMAP 버퍼의 plane을 파일 디스크립터로 변환하는 순서입니다.
Description
===========
This ioctl is an extension to the :ref:`memory mapping <mmap>` I/O
method, therefore it is available only for ``V4L2_MEMORY_MMAP`` buffers.
It can be used to export a buffer as a DMABUF file at any time after
buffers have been allocated with the
:ref:`VIDIOC_REQBUFS` ioctl.
To export a buffer, applications fill struct
:c:type:`v4l2_exportbuffer`. The ``type`` field is
set to the same buffer type as was previously used with struct
:c:type:`v4l2_requestbuffers` ``type``.
Applications must also set the ``index`` field. Valid index numbers
range from zero to the number of buffers allocated with
:ref:`VIDIOC_REQBUFS` (struct
:c:type:`v4l2_requestbuffers` ``count``) minus
one. For the multi-planar API, applications set the ``plane`` field to
the index of the plane to be exported. Valid planes range from zero to
the maximal number of valid planes for the currently active format. For
the single-planar API, applications must set ``plane`` to zero.
Additional flags may be posted in the ``flags`` field. Refer to a manual
for open() for details. Currently only O_CLOEXEC, O_RDONLY, O_WRONLY,
and O_RDWR are supported. All other fields must be set to zero. In the
case of multi-planar API, every plane is exported separately using
multiple :ref:`VIDIOC_EXPBUF` calls.
After calling :ref:`VIDIOC_EXPBUF` the ``fd`` field will be set by a
driver. This is a DMABUF file descriptor. The application may pass it to
other DMABUF-aware devices. Refer to :ref:`DMABUF importing <dmabuf>`
for details about importing DMABUF files into V4L2 nodes. It is
recommended to close a DMABUF file when it is no longer used to allow
the associated memory to be reclaimed.
single-planar와 multi-planar 예제
65-112`buffer_export()` 예제는 `v4l2_exportbuffer` 전체를 `memset()`으로 0으로 만든 뒤 `type`과 `index`를 설정합니다. ioctl이 실패하면 오류를 출력하고 -1을 반환하며, 성공하면 `expbuf.fd`를 호출자에게 돌려줍니다.
`buffer_export_mp()`는 `n_planes`만큼 반복하면서 매 plane마다 새 구조체를 0으로 초기화하고 `type`, `index`, `plane`을 설정해 각각 내보냅니다.
multi-planar 예제에서 중간 plane의 ioctl이 실패하면 `while (i) close(dmafd[--i])`로 앞서 성공한 모든 DMABUF fd를 닫은 뒤 -1을 반환합니다. 모든 plane이 성공하면 fd 배열을 유지하고 0을 반환합니다.
Examples
========
.. code-block:: c
int buffer_export(int v4lfd, enum v4l2_buf_type bt, int index, int *dmafd)
{
struct v4l2_exportbuffer expbuf;
memset(&expbuf, 0, sizeof(expbuf));
expbuf.type = bt;
expbuf.index = index;
if (ioctl(v4lfd, VIDIOC_EXPBUF, &expbuf) == -1) {
perror("VIDIOC_EXPBUF");
return -1;
}
*dmafd = expbuf.fd;
return 0;
}
.. code-block:: c
int buffer_export_mp(int v4lfd, enum v4l2_buf_type bt, int index,
int dmafd[], int n_planes)
{
int i;
for (i = 0; i < n_planes; ++i) {
struct v4l2_exportbuffer expbuf;
memset(&expbuf, 0, sizeof(expbuf));
expbuf.type = bt;
expbuf.index = index;
expbuf.plane = i;
if (ioctl(v4lfd, VIDIOC_EXPBUF, &expbuf) == -1) {
perror("VIDIOC_EXPBUF");
while (i)
close(dmafd[--i]);
return -1;
}
dmafd[i] = expbuf.fd;
}
return 0;
}
v4l2_exportbuffer 구조체
113-152응용 프로그램 입력과 드라이버 반환 필드를 원문 순서대로 정리합니다.
`type`은 `enum v4l2_buf_type` 값이며, 버퍼를 만들고 format을 설정할 때 사용한 queue type과 일치해야 합니다.
`index`는 MMAP I/O에서만 사용됩니다. 유효 범위는 `VIDIOC_REQBUFS`뿐 아니라 필요에 따라 `VIDIOC_CREATE_BUFS`로 추가 할당한 버퍼까지 포함합니다.
`fd`는 입력값이 아니라 드라이버의 출력값입니다. 반면 `reserved[11]`은 양쪽 모두 0으로 유지해야 ABI의 미래 확장과 호환됩니다.
.. c:type:: v4l2_exportbuffer
.. tabularcolumns:: |p{4.4cm}|p{4.4cm}|p{8.5cm}|
.. flat-table:: struct v4l2_exportbuffer
:header-rows: 0
:stub-columns: 0
:widths: 1 1 2
* - __u32
- ``type``
- Type of the buffer, same as struct
:c:type:`v4l2_format` ``type`` or struct
:c:type:`v4l2_requestbuffers` ``type``, set
by the application. See :c:type:`v4l2_buf_type`
* - __u32
- ``index``
- Number of the buffer, set by the application. This field is only
used for :ref:`memory mapping <mmap>` I/O and can range from
zero to the number of buffers allocated with the
:ref:`VIDIOC_REQBUFS` and/or
:ref:`VIDIOC_CREATE_BUFS` ioctls.
* - __u32
- ``plane``
- Index of the plane to be exported when using the multi-planar API.
Otherwise this value must be set to zero.
* - __u32
- ``flags``
- Flags for the newly created file, currently only ``O_CLOEXEC``,
``O_RDONLY``, ``O_WRONLY``, and ``O_RDWR`` are supported, refer to
the manual of open() for more details.
* - __s32
- ``fd``
- The DMABUF file descriptor associated with a buffer. Set by the
driver.
* - __u32
- ``reserved[11]``
- Reserved field for future use. Drivers and applications must set
the array to zero.
반환값과 EINVAL
153-162성공하면 0을 반환합니다. 오류가 발생하면 -1을 반환하고 `errno`를 적절한 값으로 설정하며, 공통 오류 코드는 Generic Error Codes 장을 따릅니다.
MMAP 상태, 기능 지원 또는 구조체 필드가 잘못된 경우입니다.
Return Value
============
On success 0 is returned, on error -1 and the ``errno`` variable is set
appropriately. The generic error codes are described at the
:ref:`Generic Error Codes <gen-errors>` chapter.
EINVAL
A queue is not in MMAP mode or DMABUF exporting is not supported or
``flags`` or ``type`` or ``index`` or ``plane`` fields are invalid.
요약·해설
vidioc-expbuf.rst:1-162V4L2 MMAP 버퍼의 single-planar·multi-planar plane을 DMABUF fd로 내보내는 절차, 허용 플래그, 구조체 필드와 정리·오류 규칙을 설명합니다.