1.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
2
3.. _VIDIOC_G_CROP:
4
5**********************************
6ioctl VIDIOC_G_CROP, VIDIOC_S_CROP
7**********************************
8
9Name
10====
11
12VIDIOC_G_CROP - VIDIOC_S_CROP - Get or set the current cropping rectangle
13
14
15Synopsis
16========
17
18.. c:function:: int ioctl( int fd, VIDIOC_G_CROP, struct v4l2_crop *argp )
19    :name: VIDIOC_G_CROP
20
21.. c:function:: int ioctl( int fd, VIDIOC_S_CROP, const struct v4l2_crop *argp )
22    :name: VIDIOC_S_CROP
23
24
25Arguments
26=========
27
28``fd``
29    File descriptor returned by :ref:`open() <func-open>`.
30
31``argp``
32    Pointer to struct :c:type:`v4l2_crop`.
33
34
35Description
36===========
37
38To query the cropping rectangle size and position applications set the
39``type`` field of a struct :c:type:`v4l2_crop` structure to the
40respective buffer (stream) type and call the :ref:`VIDIOC_G_CROP <VIDIOC_G_CROP>` ioctl
41with a pointer to this structure. The driver fills the rest of the
42structure or returns the ``EINVAL`` error code if cropping is not supported.
43
44To change the cropping rectangle applications initialize the ``type``
45and struct :c:type:`v4l2_rect` substructure named ``c`` of a
46v4l2_crop structure and call the :ref:`VIDIOC_S_CROP <VIDIOC_G_CROP>` ioctl with a pointer
47to this structure.
48
49The driver first adjusts the requested dimensions against hardware
50limits, i. e. the bounds given by the capture/output window, and it
51rounds to the closest possible values of horizontal and vertical offset,
52width and height. In particular the driver must round the vertical
53offset of the cropping rectangle to frame lines modulo two, such that
54the field order cannot be confused.
55
56Second the driver adjusts the image size (the opposite rectangle of the
57scaling process, source or target depending on the data direction) to
58the closest size possible while maintaining the current horizontal and
59vertical scaling factor.
60
61Finally the driver programs the hardware with the actual cropping and
62image parameters. :ref:`VIDIOC_S_CROP <VIDIOC_G_CROP>` is a write-only ioctl, it does not
63return the actual parameters. To query them applications must call
64:ref:`VIDIOC_G_CROP <VIDIOC_G_CROP>` and :ref:`VIDIOC_G_FMT`. When the
65parameters are unsuitable the application may modify the cropping or
66image parameters and repeat the cycle until satisfactory parameters have
67been negotiated.
68
69When cropping is not supported then no parameters are changed and
70:ref:`VIDIOC_S_CROP <VIDIOC_G_CROP>` returns the ``EINVAL`` error code.
71
72
73.. c:type:: v4l2_crop
74
75.. tabularcolumns:: |p{4.4cm}|p{4.4cm}|p{8.7cm}|
76
77.. flat-table:: struct v4l2_crop
78    :header-rows:  0
79    :stub-columns: 0
80    :widths:       1 1 2
81
82    * - __u32
83      - ``type``
84      - Type of the data stream, set by the application. Only these types
85	are valid here: ``V4L2_BUF_TYPE_VIDEO_CAPTURE``, ``V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE``,
86	``V4L2_BUF_TYPE_VIDEO_OUTPUT``, ``V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE`` and
87	``V4L2_BUF_TYPE_VIDEO_OVERLAY``. See :c:type:`v4l2_buf_type` and the note below.
88    * - struct :c:type:`v4l2_rect`
89      - ``c``
90      - Cropping rectangle. The same co-ordinate system as for struct
91	:c:type:`v4l2_cropcap` ``bounds`` is used.
92
93.. note::
94   Unfortunately in the case of multiplanar buffer types
95   (``V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE`` and ``V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE``)
96   this API was messed up with regards to how the :c:type:`v4l2_crop` ``type`` field
97   should be filled in. Some drivers only accepted the ``_MPLANE`` buffer type while
98   other drivers only accepted a non-multiplanar buffer type (i.e. without the
99   ``_MPLANE`` at the end).
100
101   Starting with kernel 4.13 both variations are allowed.
102
103
104Return Value
105============
106
107On success 0 is returned, on error -1 and the ``errno`` variable is set
108appropriately. The generic error codes are described at the
109:ref:`Generic Error Codes <gen-errors>` chapter.
110
111ENODATA
112    Cropping is not supported for this input or output.
113