← Documents Documentation/userspace-api/media/v4l/vidioc-g-selection.rst GitHub 원문 ↗

Linux 6.18.37 · 사용자 공간 API

VIDIOC_G_SELECTION·VIDIOC_S_SELECTION ioctl

crop·compose selection rectangle을 조회·설정하고 GE·LE 크기 제약, 하드웨어 조정 우선순위와 MPLANE 호환 규칙을 설명합니다.

Source pathDocumentation/userspace-api/media/v4l/vidioc-g-selection.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약·해설

vidioc-g-selection.rst:1-190

selection rectangle과 조정 constraint 및 오류 협상을 다룹니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 .. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
2 .. c:namespace:: V4L
3
4 .. _VIDIOC_G_SELECTION:
5
6 ********************************************
7 ioctl VIDIOC_G_SELECTION, VIDIOC_S_SELECTION
8 ********************************************
9
10 Name
11 ====
12
13 VIDIOC_G_SELECTION - VIDIOC_S_SELECTION - Get or set one of the selection rectangles
14
15 Synopsis
16 ========
17
18 .. c:macro:: VIDIOC_G_SELECTION
19
20 ``int ioctl(int fd, VIDIOC_G_SELECTION, struct v4l2_selection *argp)``
21
22 .. c:macro:: VIDIOC_S_SELECTION
23
24 ``int ioctl(int fd, VIDIOC_S_SELECTION, struct v4l2_selection *argp)``
25
26 Arguments
27 =========
28
29 ``fd``
30 File descriptor returned by :c:func:`open()`.
31
32 ``argp``
33 Pointer to struct :c:type:`v4l2_selection`.
34
35 Description
36 ===========
37
38 The ioctls are used to query and configure selection rectangles.
39
40 To query the cropping (composing) rectangle set struct
41 :c:type:`v4l2_selection` ``type`` field to the
42 respective buffer type. The next step is setting the
43 value of struct :c:type:`v4l2_selection` ``target``
44 field to ``V4L2_SEL_TGT_CROP`` (``V4L2_SEL_TGT_COMPOSE``). Please refer
45 to table :ref:`v4l2-selections-common` or :ref:`selection-api` for
46 additional targets. The ``flags`` and ``reserved`` fields of struct
47 :c:type:`v4l2_selection` are ignored and they must be
48 filled with zeros. The driver fills the rest of the structure or returns
49 EINVAL error code if incorrect buffer type or target was used. If
50 cropping (composing) is not supported then the active rectangle is not
51 mutable and it is always equal to the bounds rectangle. Finally, the
52 struct :c:type:`v4l2_rect` ``r`` rectangle is filled with
53 the current cropping (composing) coordinates. The coordinates are
54 expressed in driver-dependent units. The only exception are rectangles
55 for images in raw formats, whose coordinates are always expressed in
56 pixels.
57
58 To change the cropping (composing) rectangle set the struct
59 :c:type:`v4l2_selection` ``type`` field to the
60 respective buffer type. The next step is setting the
61 value of struct :c:type:`v4l2_selection` ``target`` to
62 ``V4L2_SEL_TGT_CROP`` (``V4L2_SEL_TGT_COMPOSE``). Please refer to table
63 :ref:`v4l2-selections-common` or :ref:`selection-api` for additional
64 targets. The struct :c:type:`v4l2_rect` ``r`` rectangle need
65 to be set to the desired active area. Field struct
66 :c:type:`v4l2_selection` ``reserved`` is ignored and
67 must be filled with zeros. The driver may adjust coordinates of the
68 requested rectangle. An application may introduce constraints to control
69 rounding behaviour. The struct :c:type:`v4l2_selection`
70 ``flags`` field must be set to one of the following:
71
72 - ``0`` - The driver can adjust the rectangle size freely and shall
73 choose a crop/compose rectangle as close as possible to the requested
74 one.
75
76 - ``V4L2_SEL_FLAG_GE`` - The driver is not allowed to shrink the
77 rectangle. The original rectangle must lay inside the adjusted one.
78
79 - ``V4L2_SEL_FLAG_LE`` - The driver is not allowed to enlarge the
80 rectangle. The adjusted rectangle must lay inside the original one.
81
82 - ``V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE`` - The driver must choose the
83 size exactly the same as in the requested rectangle.
84
85 Please refer to :ref:`sel-const-adjust`.
86
87 The driver may have to adjusts the requested dimensions against hardware
88 limits and other parts as the pipeline, i.e. the bounds given by the
89 capture/output window or TV display. The closest possible values of
90 horizontal and vertical offset and sizes are chosen according to
91 following priority:
92
93 1. Satisfy constraints from struct
94 :c:type:`v4l2_selection` ``flags``.
95
96 2. Adjust width, height, left, and top to hardware limits and
97 alignments.
98
99 3. Keep center of adjusted rectangle as close as possible to the
100 original one.
101
102 4. Keep width and height as close as possible to original ones.
103
104 5. Keep horizontal and vertical offset as close as possible to original
105 ones.
106
107 On success the struct :c:type:`v4l2_rect` ``r`` field
108 contains the adjusted rectangle. When the parameters are unsuitable the
109 application may modify the cropping (composing) or image parameters and
110 repeat the cycle until satisfactory parameters have been negotiated. If
111 constraints flags have to be violated at then ``ERANGE`` is returned. The
112 error indicates that *there exist no rectangle* that satisfies the
113 constraints.
114
115 Selection targets and flags are documented in
116 :ref:`v4l2-selections-common`.
117
118 .. _sel-const-adjust:
119
120 .. kernel-figure:: constraints.svg
121 :alt: constraints.svg
122 :align: center
123
124 Size adjustments with constraint flags.
125
126 Behaviour of rectangle adjustment for different constraint flags.
127
128
129
130 .. c:type:: v4l2_selection
131
132 .. tabularcolumns:: |p{4.4cm}|p{4.4cm}|p{8.5cm}|
133
134 .. flat-table:: struct v4l2_selection
135 :header-rows: 0
136 :stub-columns: 0
137 :widths: 1 1 2
138
139 * - __u32
140 - ``type``
141 - Type of the buffer (from enum
142 :c:type:`v4l2_buf_type`).
143 * - __u32
144 - ``target``
145 - Used to select between
146 :ref:`cropping and composing rectangles <v4l2-selections-common>`.
147 * - __u32
148 - ``flags``
149 - Flags controlling the selection rectangle adjustments, refer to
150 :ref:`selection flags <v4l2-selection-flags>`.
151 * - struct :c:type:`v4l2_rect`
152 - ``r``
153 - The selection rectangle.
154 * - __u32
155 - ``reserved[9]``
156 - Reserved fields for future use. Drivers and applications must zero
157 this array.
158
159 .. note::
160 Unfortunately in the case of multiplanar buffer types
161 (``V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE`` and ``V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE``)
162 this API was messed up with regards to how the :c:type:`v4l2_selection` ``type`` field
163 should be filled in. Some drivers only accepted the ``_MPLANE`` buffer type while
164 other drivers only accepted a non-multiplanar buffer type (i.e. without the
165 ``_MPLANE`` at the end).
166
167 Starting with kernel 4.13 both variations are allowed.
168
169 Return Value
170 ============
171
172 On success 0 is returned, on error -1 and the ``errno`` variable is set
173 appropriately. The generic error codes are described at the
174 :ref:`Generic Error Codes <gen-errors>` chapter.
175
176 EINVAL
177 Given buffer type ``type`` or the selection target ``target`` is not
178 supported, or the ``flags`` argument is not valid.
179
180 ERANGE
181 It is not possible to adjust struct :c:type:`v4l2_rect`
182 ``r`` rectangle to satisfy all constraints given in the ``flags``
183 argument.
184
185 ENODATA
186 Selection is not supported for this input or output.
187
188 EBUSY
189 It is not possible to apply change of the selection rectangle at the
190 moment. Usually because streaming is in progress.
191

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

목적, 호출 형식과 인자

1-34

`VIDIOC_G_SELECTION`과 `VIDIOC_S_SELECTION`은 cropping·composing 등 selection rectangle을 조회하거나 설정합니다. 두 명령은 `struct v4l2_selection *argp`를 사용합니다.

`fd`는 `open()`이 반환한 파일 디스크립터이며 구조체는 buffer type, selection target, 조정 flag와 `v4l2_rect`를 담습니다.

.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
.. c:namespace:: V4L

.. _VIDIOC_G_SELECTION:

********************************************
ioctl VIDIOC_G_SELECTION, VIDIOC_S_SELECTION
********************************************

Name
====

VIDIOC_G_SELECTION - VIDIOC_S_SELECTION - Get or set one of the selection rectangles

Synopsis
========

.. c:macro:: VIDIOC_G_SELECTION

``int ioctl(int fd, VIDIOC_G_SELECTION, struct v4l2_selection *argp)``

.. c:macro:: VIDIOC_S_SELECTION

``int ioctl(int fd, VIDIOC_S_SELECTION, struct v4l2_selection *argp)``

Arguments
=========

``fd``
    File descriptor returned by :c:func:`open()`.

``argp``
    Pointer to struct :c:type:`v4l2_selection`.

selection rectangle 조회

35-57

crop 또는 compose rectangle을 조회하려면 `type`을 해당 buffer type, `target`을 `V4L2_SEL_TGT_CROP` 또는 `V4L2_SEL_TGT_COMPOSE`로 설정합니다. 추가 target은 공통 selection 표와 Selection API 문서에 정의됩니다.

조회에서는 `flags`와 `reserved`가 무시되지만 반드시 0으로 채워야 합니다. 잘못된 buffer type 또는 target이면 `EINVAL`입니다.

cropping 또는 composing을 지원하지 않으면 active rectangle은 변경할 수 없고 항상 bounds rectangle과 같습니다. 성공하면 현재 좌표가 `r`에 돌아옵니다.

좌표 단위는 드라이버에 따라 다릅니다. 예외적으로 raw format 영상의 rectangle 좌표는 항상 pixel 단위입니다.

Description
===========

The ioctls are used to query and configure selection rectangles.

To query the cropping (composing) rectangle set struct
:c:type:`v4l2_selection` ``type`` field to the
respective buffer type. The next step is setting the
value of struct :c:type:`v4l2_selection` ``target``
field to ``V4L2_SEL_TGT_CROP`` (``V4L2_SEL_TGT_COMPOSE``). Please refer
to table :ref:`v4l2-selections-common` or :ref:`selection-api` for
additional targets. The ``flags`` and ``reserved`` fields of struct
:c:type:`v4l2_selection` are ignored and they must be
filled with zeros. The driver fills the rest of the structure or returns
EINVAL error code if incorrect buffer type or target was used. If
cropping (composing) is not supported then the active rectangle is not
mutable and it is always equal to the bounds rectangle. Finally, the
struct :c:type:`v4l2_rect` ``r`` rectangle is filled with
the current cropping (composing) coordinates. The coordinates are
expressed in driver-dependent units. The only exception are rectangles
for images in raw formats, whose coordinates are always expressed in
pixels.

rectangle 설정, 제약과 조정 순서

58-116

설정할 때도 `type`과 `target`을 지정하고 원하는 active area를 `r`에 넣으며 `reserved`를 0으로 만듭니다. 드라이버는 하드웨어와 pipeline 제약에 맞춰 요청 좌표를 조정할 수 있습니다.

Selection constraint flags
flags드라이버 제약
`0`크기를 자유롭게 조정하고 요청에 가장 가까운 crop·compose rectangle 선택
`V4L2_SEL_FLAG_GE`rectangle을 줄일 수 없음. 원본이 조정된 rectangle 안에 있어야 함
`V4L2_SEL_FLAG_LE`rectangle을 늘릴 수 없음. 조정된 rectangle이 원본 안에 있어야 함
`V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE`요청과 정확히 같은 크기를 선택해야 함

rectangle 크기를 반올림·정렬할 때 허용되는 방향을 제한합니다.

조정은 하드웨어 한계뿐 아니라 capture·output window나 TV display가 정하는 bounds 등 pipeline의 다른 요소도 고려합니다.

rectangle 조정 우선순위
1. flags의 제약 충족2. width·height·left·top을 하드웨어 한계와 alignment에 맞춤3. 조정된 중심을 원본 중심에 최대한 가깝게 유지4. width·height를 원본에 최대한 가깝게 유지5. 수평·수직 offset을 원본에 최대한 가깝게 유지

드라이버는 아래 순서대로 가장 가까운 유효 rectangle을 선택합니다.

성공하면 조정된 rectangle이 `r`에 반환됩니다. 적합하지 않으면 crop·compose 또는 영상 매개변수를 수정해 만족할 때까지 협상을 반복할 수 있습니다.

constraint flag를 지키는 rectangle이 하나도 없으면 `ERANGE`입니다. 단순히 요청값이 조정됐다는 뜻이 아니라 모든 제약을 동시에 만족하는 rectangle이 존재하지 않는다는 뜻입니다.

To change the cropping (composing) rectangle set the struct
:c:type:`v4l2_selection` ``type`` field to the
respective buffer type. The next step is setting the
value of struct :c:type:`v4l2_selection` ``target`` to
``V4L2_SEL_TGT_CROP`` (``V4L2_SEL_TGT_COMPOSE``). Please refer to table
:ref:`v4l2-selections-common` or :ref:`selection-api` for additional
targets. The struct :c:type:`v4l2_rect` ``r`` rectangle need
to be set to the desired active area. Field struct
:c:type:`v4l2_selection` ``reserved`` is ignored and
must be filled with zeros. The driver may adjust coordinates of the
requested rectangle. An application may introduce constraints to control
rounding behaviour. The struct :c:type:`v4l2_selection`
``flags`` field must be set to one of the following:

-  ``0`` - The driver can adjust the rectangle size freely and shall
   choose a crop/compose rectangle as close as possible to the requested
   one.

-  ``V4L2_SEL_FLAG_GE`` - The driver is not allowed to shrink the
   rectangle. The original rectangle must lay inside the adjusted one.

-  ``V4L2_SEL_FLAG_LE`` - The driver is not allowed to enlarge the
   rectangle. The adjusted rectangle must lay inside the original one.

-  ``V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE`` - The driver must choose the
   size exactly the same as in the requested rectangle.

Please refer to :ref:`sel-const-adjust`.

The driver may have to adjusts the requested dimensions against hardware
limits and other parts as the pipeline, i.e. the bounds given by the
capture/output window or TV display. The closest possible values of
horizontal and vertical offset and sizes are chosen according to
following priority:

1. Satisfy constraints from struct
   :c:type:`v4l2_selection` ``flags``.

2. Adjust width, height, left, and top to hardware limits and
   alignments.

3. Keep center of adjusted rectangle as close as possible to the
   original one.

4. Keep width and height as close as possible to original ones.

5. Keep horizontal and vertical offset as close as possible to original
   ones.

On success the struct :c:type:`v4l2_rect` ``r`` field
contains the adjusted rectangle. When the parameters are unsuitable the
application may modify the cropping (composing) or image parameters and
repeat the cycle until satisfactory parameters have been negotiated. If
constraints flags have to be violated at then ``ERANGE`` is returned. The
error indicates that *there exist no rectangle* that satisfies the
constraints.

Selection targets and flags are documented in
:ref:`v4l2-selections-common`.

제약 flag에 따른 크기 조정 도식

117-129

원문의 `constraints.svg` 도식은 `GE`, `LE`, 두 flag의 결합에 따라 요청 rectangle과 조정 rectangle의 포함 관계가 어떻게 달라지는지 보여 줍니다.

포함 관계 요약
조건포함 관계
GER이 A 안에 포함
LEA가 R 안에 포함
GE | LEA와 R의 크기가 동일

원본 R과 조정 결과 A의 크기 관계입니다.


.. _sel-const-adjust:

.. kernel-figure::  constraints.svg
    :alt:    constraints.svg
    :align:  center

    Size adjustments with constraint flags.

    Behaviour of rectangle adjustment for different constraint flags.


struct v4l2_selection과 MPLANE 호환

130-168
v4l2_selection 필드
형식필드의미
`__u32``type``enum v4l2_buf_type`의 buffer type
`__u32``target`crop·compose 등 selection rectangle 종류
`__u32``flags`selection rectangle 조정 제약 flag
`struct v4l2_rect``r`selection rectangle
`__u32[9]``reserved[9]`미래 확장용. 드라이버와 애플리케이션 모두 0

selection 대상과 조정 결과 rectangle을 전달합니다.

multi-planar buffer type의 `type` 처리에는 역사적 불일치가 있습니다. 일부 드라이버는 `_MPLANE` type만, 다른 드라이버는 접미사가 없는 non-multiplanar type만 받았습니다.

Linux kernel 4.13부터 `V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE`·`V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE`과 대응 non-multiplanar 변형을 모두 허용합니다.

.. c:type:: v4l2_selection

.. tabularcolumns:: |p{4.4cm}|p{4.4cm}|p{8.5cm}|

.. flat-table:: struct v4l2_selection
    :header-rows:  0
    :stub-columns: 0
    :widths:       1 1 2

    * - __u32
      - ``type``
      - Type of the buffer (from enum
	:c:type:`v4l2_buf_type`).
    * - __u32
      - ``target``
      - Used to select between
	:ref:`cropping and composing rectangles <v4l2-selections-common>`.
    * - __u32
      - ``flags``
      - Flags controlling the selection rectangle adjustments, refer to
	:ref:`selection flags <v4l2-selection-flags>`.
    * - struct :c:type:`v4l2_rect`
      - ``r``
      - The selection rectangle.
    * - __u32
      - ``reserved[9]``
      - Reserved fields for future use. Drivers and applications must zero
	this array.

.. note::
   Unfortunately in the case of multiplanar buffer types
   (``V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE`` and ``V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE``)
   this API was messed up with regards to how the :c:type:`v4l2_selection` ``type`` field
   should be filled in. Some drivers only accepted the ``_MPLANE`` buffer type while
   other drivers only accepted a non-multiplanar buffer type (i.e. without the
   ``_MPLANE`` at the end).

   Starting with kernel 4.13 both variations are allowed.

반환값과 오류

169-190

성공하면 0을 반환합니다. 오류가 발생하면 -1을 반환하고 `errno`를 설정하며 Generic Error Codes 장의 공통 오류도 적용됩니다.

selection 오류
errno조건
`EINVAL`buffer type 또는 target을 지원하지 않거나 flags가 유효하지 않음
`ERANGE`flags의 모든 제약을 만족하도록 `r`을 조정할 수 없음
`ENODATA`현재 input 또는 output에서 selection을 지원하지 않음
`EBUSY`현재 selection 변경을 적용할 수 없음. 일반적으로 streaming 진행 중

type·target·flag와 현재 streaming 상태에 따른 오류입니다.

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
    Given buffer type ``type`` or the selection target ``target`` is not
    supported, or the ``flags`` argument is not valid.

ERANGE
    It is not possible to adjust struct :c:type:`v4l2_rect`
    ``r`` rectangle to satisfy all constraints given in the ``flags``
    argument.

ENODATA
    Selection is not supported for this input or output.

EBUSY
    It is not possible to apply change of the selection rectangle at the
    moment. Usually because streaming is in progress.